ltcai 11.1.0 → 11.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +53 -53
- package/docs/CHANGELOG.md +33 -0
- package/docs/COMMUNITY_AND_PLUGINS.md +1 -1
- package/docs/DEVELOPMENT.md +1 -1
- package/docs/FEATURE_AUDIT_v11.2.0.md +393 -0
- package/docs/LAYOUT_REBUILD_SPEC.md +9 -1
- package/docs/ONBOARDING.md +1 -1
- package/docs/OPERATIONS.md +1 -1
- package/docs/TRUST_MODEL.md +1 -1
- package/docs/WHY_LATTICE.md +1 -1
- package/docs/architecture.md +6 -2
- package/docs/kg-schema.md +1 -1
- package/lattice_brain/__init__.py +1 -1
- package/lattice_brain/gates.py +125 -0
- package/lattice_brain/graph/fusion.py +35 -4
- package/lattice_brain/graph/projection.py +66 -8
- package/lattice_brain/graph/schema.py +9 -0
- package/lattice_brain/graph/store.py +9 -0
- package/lattice_brain/graph/vector_index/selector.py +32 -2
- package/lattice_brain/ingestion.py +175 -27
- package/lattice_brain/multimodal.py +525 -5
- package/lattice_brain/portability.py +169 -32
- package/lattice_brain/runtime/multi_agent.py +1 -1
- package/lattice_brain/sealed_box.py +244 -0
- package/lattice_brain/synthesis.py +24 -1
- package/latticeai/__init__.py +1 -1
- package/latticeai/api/brain_intelligence.py +4 -0
- package/latticeai/api/chat.py +11 -0
- package/latticeai/api/chat_helpers.py +16 -3
- package/latticeai/api/chat_hybrid.py +32 -1
- package/latticeai/api/features.py +70 -0
- package/latticeai/api/local_files.py +102 -0
- package/latticeai/api/portability.py +39 -4
- package/latticeai/api/review_queue.py +126 -0
- package/latticeai/api/search.py +16 -2
- package/latticeai/core/agent.py +55 -2
- package/latticeai/core/config.py +4 -1
- package/latticeai/core/context_builder.py +6 -3
- package/latticeai/core/legacy_compatibility.py +1 -1
- package/latticeai/core/marketplace.py +1 -1
- package/latticeai/core/messages.py +143 -0
- package/latticeai/core/model_compat.py +73 -2
- package/latticeai/core/workspace_os_constants.py +1 -1
- package/latticeai/models/model_providers.py +12 -4
- package/latticeai/runtime/build_phases.py +28 -0
- package/latticeai/runtime/chat_wiring.py +4 -0
- package/latticeai/runtime/feature_toggle_wiring.py +163 -0
- package/latticeai/runtime/router_registration.py +11 -0
- package/latticeai/services/app_context.py +8 -0
- package/latticeai/services/architecture_readiness.py +1 -1
- package/latticeai/services/automation_intelligence.py +22 -2
- package/latticeai/services/brain_intelligence.py +123 -7
- package/latticeai/services/command_center.py +10 -4
- package/latticeai/services/feature_toggles.py +502 -0
- package/latticeai/services/folder_watch.py +122 -1
- package/latticeai/services/hybrid_chat.py +56 -5
- package/latticeai/services/interop_bridges.py +978 -0
- package/latticeai/services/model_capability_registry.py +434 -261
- package/latticeai/services/model_catalog.py +95 -61
- package/latticeai/services/model_recommendation.py +18 -11
- package/latticeai/services/model_runtime.py +1 -1
- package/latticeai/services/multimodal_ports.py +26 -1
- package/latticeai/services/obsidian_bridge.py +16 -25
- package/latticeai/services/product_readiness.py +1 -1
- package/latticeai/services/search_service.py +149 -2
- package/latticeai/services/tool_dispatch.py +4 -0
- package/latticeai/setup/auto_setup.py +27 -30
- package/latticeai/setup/wizard.py +77 -44
- package/package.json +1 -1
- package/scripts/check_current_release_docs.mjs +1 -1
- package/scripts/check_server_i18n.mjs +1 -0
- package/scripts/release_screen_claims.json +13 -0
- package/scripts/verify_hf_model_registry.py +253 -218
- package/src-tauri/Cargo.lock +1 -1
- package/src-tauri/Cargo.toml +1 -1
- package/src-tauri/tauri.conf.json +1 -1
- package/static/app/asset-manifest.json +37 -37
- package/static/app/assets/{Act-D0HWqtn0.js → Act-AWf0SAKp.js} +1 -1
- package/static/app/assets/{AdminConsole-D-QDW-A4.js → AdminConsole-D0u8Tiyj.js} +1 -1
- package/static/app/assets/{Brain-CzCsI1mi.js → Brain-tuhI4sOC.js} +1 -1
- package/static/app/assets/BrainHome-Ts7G_Ila.js +2 -0
- package/static/app/assets/{BrainSignals-2dHQNkns.js → BrainSignals-jMYgQ2Ar.js} +1 -1
- package/static/app/assets/{Capture-CT8v1StE.js → Capture-CqOSzyPr.js} +1 -1
- package/static/app/assets/{CommandPalette-DoLXC2KH.js → CommandPalette-DC0Bzh-I.js} +1 -1
- package/static/app/assets/{Library-DDoxFE5c.js → Library-CX-bbhmK.js} +1 -1
- package/static/app/assets/{LivingBrain-BXMWIK_2.js → LivingBrain-DBwhto14.js} +1 -1
- package/static/app/assets/{ProductFlow-DOYf7JIs.js → ProductFlow-BHA2cfKI.js} +1 -1
- package/static/app/assets/{ReviewCard-COQsqidK.js → ReviewCard-BUhCKRNM.js} +1 -1
- package/static/app/assets/{System-BRllvYXd.js → System-Bu2t5hn1.js} +1 -1
- package/static/app/assets/arrow-left-Dzwa5zRb.js +1 -0
- package/static/app/assets/{bot-4BvN07ux.js → bot-Cia42c2h.js} +1 -1
- package/static/app/assets/brain-DJMoqrwx.js +1 -0
- package/static/app/assets/{button-CDjtnAoU.js → button-2j2Ijzgq.js} +1 -1
- package/static/app/assets/{circle-pause-D_RMn7tp.js → circle-pause-BEFeWpVW.js} +1 -1
- package/static/app/assets/{circle-play-B5OpB8ae.js → circle-play-ujXMcHxl.js} +1 -1
- package/static/app/assets/{cpu-BIlWInHf.js → cpu-k4awryFq.js} +1 -1
- package/static/app/assets/{download-BtjXfL3z.js → download-DFbLJ_ig.js} +1 -1
- package/static/app/assets/{folder-open-DefMpxI2.js → folder-open-7y_b6xkM.js} +1 -1
- package/static/app/assets/{hard-drive-BQ8NZVkw.js → hard-drive-Bidh02Kr.js} +1 -1
- package/static/app/assets/{index-0AvoEBzJ.js → index-BpYkzcVm.js} +3 -3
- package/static/app/assets/{index-vtEfYvQY.css → index-DwDl9-8Y.css} +1 -1
- package/static/app/assets/{input-B_5ZJ9oy.js → input-DSlJJxRs.js} +1 -1
- package/static/app/assets/{permissionCopy-BqZ5tsgL.js → permissionCopy-Bpb83Hx9.js} +1 -1
- package/static/app/assets/{primitives-CVwew78r.js → primitives-BCx6TvfG.js} +1 -1
- package/static/app/assets/search-Cgy8cCFJ.js +1 -0
- package/static/app/assets/{share-2-D5zg_0fY.js → share-2-BH1M-WNi.js} +1 -1
- package/static/app/assets/{shield-alert-B5pZzkUb.js → shield-alert-BlKdBXcG.js} +1 -1
- package/static/app/assets/{textarea-nEVIweKY.js → textarea-CCWbUfFB.js} +1 -1
- package/static/app/assets/{useFocusTrap-Cm99AHlz.js → useFocusTrap-YdHQ7pJ1.js} +1 -1
- package/static/app/assets/{useQuery-Dm__N6bL.js → useQuery-CXQiwbVT.js} +1 -1
- package/static/app/assets/{utils-DcDMoZIe.js → utils-zqPZJxdx.js} +2 -2
- package/static/app/assets/{workspace-LtRRSKTf.js → workspace-DXTihhfU.js} +1 -1
- package/static/app/index.html +4 -4
- package/static/sw.js +1 -1
- package/static/app/assets/BrainHome-Btns-_TA.js +0 -2
- package/static/app/assets/arrow-left-DnyMzss-.js +0 -1
- package/static/app/assets/brain-uMb_5hnO.js +0 -1
- package/static/app/assets/search-DkhnOKZt.js +0 -1
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
"""One user-facing switchboard for every opt-in feature (v11.2.0).
|
|
2
|
+
|
|
3
|
+
11.2.0 replaced the frozen ``os.getenv`` reads behind each opt-in feature with
|
|
4
|
+
:class:`lattice_brain.gates.FeatureGate`, which answers *when it is asked* and
|
|
5
|
+
lets an app-layer resolver win over the environment. That made the features
|
|
6
|
+
movable at runtime. It did not make them reachable: a person still had to know
|
|
7
|
+
an environment variable's name, edit a shell profile, and restart the server.
|
|
8
|
+
|
|
9
|
+
This service is the reachable half.
|
|
10
|
+
|
|
11
|
+
* **The server renders the catalog.** Ids, labels, one-line explanations,
|
|
12
|
+
defaults, and which choices are even installable all come from here, so the
|
|
13
|
+
panel cannot drift from what the server actually honours (the 10.1.1 rule:
|
|
14
|
+
no hardcoded catalogs in the client).
|
|
15
|
+
* **Precedence is user → env → default.** An untouched install follows its
|
|
16
|
+
environment exactly as before and says so (``source: "env"``). The first time
|
|
17
|
+
a person moves a switch, their choice is persisted and wins from then on.
|
|
18
|
+
* **Persistence is a small atomic JSON file** under the data dir — the same
|
|
19
|
+
shape ``PermissionModeService`` uses. Nothing here is derived state worth a
|
|
20
|
+
table, and a settings file that can be read with ``cat`` is a feature.
|
|
21
|
+
* **Every string a person reads comes from the message catalog**, so the panel
|
|
22
|
+
is Korean or English because of the request, not because of who wrote it.
|
|
23
|
+
|
|
24
|
+
The gates themselves are bound to this service in
|
|
25
|
+
``latticeai/runtime/feature_toggle_wiring.py``. With no service bound, every
|
|
26
|
+
gate keeps reading its environment variable exactly as it did — which is what
|
|
27
|
+
makes this whole surface additive.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
from __future__ import annotations
|
|
31
|
+
|
|
32
|
+
import json
|
|
33
|
+
import os
|
|
34
|
+
import threading
|
|
35
|
+
from dataclasses import dataclass
|
|
36
|
+
from pathlib import Path
|
|
37
|
+
from typing import Any, Callable, Dict, List, Mapping, Optional, Tuple
|
|
38
|
+
|
|
39
|
+
from lattice_brain.gates import FALSY, TRUTHY
|
|
40
|
+
from latticeai.core.io_utils import atomic_write_json
|
|
41
|
+
from latticeai.core.messages import DEFAULT_LANGUAGE, translate
|
|
42
|
+
|
|
43
|
+
#: A two-state switch.
|
|
44
|
+
TOGGLE = "toggle"
|
|
45
|
+
#: A pick-one-of-N setting (``vector_backend``), where some options may not be
|
|
46
|
+
#: installable on this machine.
|
|
47
|
+
CHOICE = "choice"
|
|
48
|
+
|
|
49
|
+
#: File under the data dir. Deliberately human-readable and hand-editable.
|
|
50
|
+
STORE_FILENAME = "feature_toggles.json"
|
|
51
|
+
STORE_VERSION = 1
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class UnknownFeature(KeyError):
|
|
55
|
+
"""Raised for a feature id this build does not have."""
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class InvalidFeatureValue(ValueError):
|
|
59
|
+
"""Raised for a value this feature cannot take (wrong type, or not installed)."""
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@dataclass(frozen=True)
|
|
63
|
+
class FeatureChoice:
|
|
64
|
+
"""One option of a ``choice`` feature.
|
|
65
|
+
|
|
66
|
+
``probe`` names an availability check: an option whose optional dependency
|
|
67
|
+
is missing is *shown*, disabled, with the reason — hiding it would leave a
|
|
68
|
+
person wondering why the docs mention a backend their product does not.
|
|
69
|
+
"""
|
|
70
|
+
|
|
71
|
+
id: str
|
|
72
|
+
label_key: str
|
|
73
|
+
probe: Optional[str] = None
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@dataclass(frozen=True)
|
|
77
|
+
class FeatureDefinition:
|
|
78
|
+
"""One row of the switchboard.
|
|
79
|
+
|
|
80
|
+
``env_var`` is the seed this feature followed before there was a panel, and
|
|
81
|
+
still follows until someone moves it. ``live`` records whether the switch
|
|
82
|
+
takes effect immediately: every feature here does, because each is backed by
|
|
83
|
+
a :class:`FeatureGate` (or the equivalent resolver seam) that is asked per
|
|
84
|
+
call — the catalog reports it rather than asserting it, so a future feature
|
|
85
|
+
that genuinely needs a restart can say so instead of lying.
|
|
86
|
+
"""
|
|
87
|
+
|
|
88
|
+
id: str
|
|
89
|
+
kind: str
|
|
90
|
+
env_var: str
|
|
91
|
+
default: Any
|
|
92
|
+
caution: bool = False
|
|
93
|
+
parent: Optional[str] = None
|
|
94
|
+
choices: Tuple[FeatureChoice, ...] = ()
|
|
95
|
+
live: bool = True
|
|
96
|
+
|
|
97
|
+
@property
|
|
98
|
+
def label_key(self) -> str:
|
|
99
|
+
return f"features.{self.id}.label"
|
|
100
|
+
|
|
101
|
+
@property
|
|
102
|
+
def summary_key(self) -> str:
|
|
103
|
+
return f"features.{self.id}.summary"
|
|
104
|
+
|
|
105
|
+
@property
|
|
106
|
+
def caution_key(self) -> str:
|
|
107
|
+
return f"features.{self.id}.caution"
|
|
108
|
+
|
|
109
|
+
def choice_ids(self) -> Tuple[str, ...]:
|
|
110
|
+
return tuple(choice.id for choice in self.choices)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _hnsw_probe() -> Tuple[bool, str]:
|
|
114
|
+
"""Whether the optional ANN engine is importable here, and why not."""
|
|
115
|
+
from lattice_brain.graph.vector_index.hnsw import load_hnswlib
|
|
116
|
+
|
|
117
|
+
module, reason = load_hnswlib()
|
|
118
|
+
return module is not None, reason or ""
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
#: Availability probes for ``choice`` options, injectable so a test never has to
|
|
122
|
+
#: install a compiled extension to exercise both branches.
|
|
123
|
+
DEFAULT_PROBES: Dict[str, Callable[[], Tuple[bool, str]]] = {"hnsw": _hnsw_probe}
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
#: The switchboard. Order is the order the panel renders, grouped by what a
|
|
127
|
+
#: person is deciding: what the Brain takes in, what leaves it, then how it
|
|
128
|
+
#: searches. ``video_ingest`` declares ``allow_multimodal`` as its parent so the
|
|
129
|
+
#: panel can indent it rather than presenting a sub-switch as a peer.
|
|
130
|
+
CATALOG: Tuple[FeatureDefinition, ...] = (
|
|
131
|
+
FeatureDefinition(
|
|
132
|
+
id="allow_multimodal",
|
|
133
|
+
kind=TOGGLE,
|
|
134
|
+
env_var="LATTICEAI_ALLOW_MULTIMODAL",
|
|
135
|
+
default=False,
|
|
136
|
+
),
|
|
137
|
+
FeatureDefinition(
|
|
138
|
+
id="video_ingest",
|
|
139
|
+
kind=TOGGLE,
|
|
140
|
+
env_var="LATTICEAI_ALLOW_VIDEO",
|
|
141
|
+
default=True,
|
|
142
|
+
parent="allow_multimodal",
|
|
143
|
+
),
|
|
144
|
+
FeatureDefinition(
|
|
145
|
+
id="vault_watch",
|
|
146
|
+
kind=TOGGLE,
|
|
147
|
+
env_var="LATTICEAI_VAULT_WATCH",
|
|
148
|
+
default=False,
|
|
149
|
+
),
|
|
150
|
+
FeatureDefinition(
|
|
151
|
+
id="brain_network",
|
|
152
|
+
kind=TOGGLE,
|
|
153
|
+
env_var="LATTICEAI_BRAIN_NETWORK",
|
|
154
|
+
default=False,
|
|
155
|
+
caution=True,
|
|
156
|
+
),
|
|
157
|
+
FeatureDefinition(
|
|
158
|
+
id="synthesis",
|
|
159
|
+
kind=TOGGLE,
|
|
160
|
+
env_var="LATTICEAI_SYNTHESIS",
|
|
161
|
+
default=True,
|
|
162
|
+
),
|
|
163
|
+
FeatureDefinition(
|
|
164
|
+
id="auto_vector_index",
|
|
165
|
+
kind=TOGGLE,
|
|
166
|
+
env_var="LATTICEAI_AUTO_VECTOR_INDEX",
|
|
167
|
+
default=True,
|
|
168
|
+
),
|
|
169
|
+
FeatureDefinition(
|
|
170
|
+
id="auto_late_fusion",
|
|
171
|
+
kind=TOGGLE,
|
|
172
|
+
env_var="LATTICEAI_TEXT_IMAGE_FUSION",
|
|
173
|
+
default=False,
|
|
174
|
+
),
|
|
175
|
+
FeatureDefinition(
|
|
176
|
+
id="fusion_rrf",
|
|
177
|
+
kind=TOGGLE,
|
|
178
|
+
env_var="LATTICEAI_FUSION_RRF",
|
|
179
|
+
default=False,
|
|
180
|
+
),
|
|
181
|
+
FeatureDefinition(
|
|
182
|
+
id="graph_expansion",
|
|
183
|
+
kind=TOGGLE,
|
|
184
|
+
env_var="LATTICEAI_GRAPH_EXPANSION",
|
|
185
|
+
default=False,
|
|
186
|
+
),
|
|
187
|
+
FeatureDefinition(
|
|
188
|
+
id="vector_backend",
|
|
189
|
+
kind=CHOICE,
|
|
190
|
+
env_var="LATTICEAI_VECTOR_INDEX",
|
|
191
|
+
default="brute",
|
|
192
|
+
choices=(
|
|
193
|
+
FeatureChoice("brute", "features.vector_backend.choice.brute"),
|
|
194
|
+
FeatureChoice("quantized", "features.vector_backend.choice.quantized"),
|
|
195
|
+
FeatureChoice("hnsw", "features.vector_backend.choice.hnsw", probe="hnsw"),
|
|
196
|
+
),
|
|
197
|
+
),
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
CATALOG_BY_ID: Dict[str, FeatureDefinition] = {item.id: item for item in CATALOG}
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
class FeatureToggleService:
|
|
204
|
+
"""Resolve, persist, and describe every opt-in feature.
|
|
205
|
+
|
|
206
|
+
Thread-safe by a single lock around the file, which is all the contention
|
|
207
|
+
this can see: the store is written only when a person moves a switch.
|
|
208
|
+
"""
|
|
209
|
+
|
|
210
|
+
def __init__(
|
|
211
|
+
self,
|
|
212
|
+
*,
|
|
213
|
+
data_dir: Path,
|
|
214
|
+
probes: Optional[Mapping[str, Callable[[], Tuple[bool, str]]]] = None,
|
|
215
|
+
audit: Optional[Callable[..., None]] = None,
|
|
216
|
+
) -> None:
|
|
217
|
+
self.data_dir = Path(data_dir)
|
|
218
|
+
self.probes: Mapping[str, Callable[[], Tuple[bool, str]]] = (
|
|
219
|
+
dict(DEFAULT_PROBES) if probes is None else probes
|
|
220
|
+
)
|
|
221
|
+
self.audit = audit
|
|
222
|
+
self._lock = threading.Lock()
|
|
223
|
+
|
|
224
|
+
# ── plumbing ────────────────────────────────────────────────────────────
|
|
225
|
+
@property
|
|
226
|
+
def path(self) -> Path:
|
|
227
|
+
return self.data_dir / STORE_FILENAME
|
|
228
|
+
|
|
229
|
+
def rebind_data_dir(self, data_dir: Path) -> None:
|
|
230
|
+
"""Point the store at the app's real data dir (see permission mode)."""
|
|
231
|
+
with self._lock:
|
|
232
|
+
self.data_dir = Path(data_dir)
|
|
233
|
+
|
|
234
|
+
def rebind_audit(self, audit: Callable[..., None]) -> None:
|
|
235
|
+
"""Attach the real audit sink once app wiring provides one."""
|
|
236
|
+
with self._lock:
|
|
237
|
+
self.audit = audit
|
|
238
|
+
|
|
239
|
+
def _read(self) -> Dict[str, Any]:
|
|
240
|
+
"""Stored user choices. A missing or unreadable file means "none yet"."""
|
|
241
|
+
path = self.path
|
|
242
|
+
if not path.exists():
|
|
243
|
+
return {}
|
|
244
|
+
try:
|
|
245
|
+
data = json.loads(path.read_text(encoding="utf-8"))
|
|
246
|
+
except Exception: # noqa: BLE001 — a corrupt file must not break the app
|
|
247
|
+
return {}
|
|
248
|
+
features = data.get("features") if isinstance(data, dict) else None
|
|
249
|
+
return dict(features) if isinstance(features, dict) else {}
|
|
250
|
+
|
|
251
|
+
def _write(self, features: Mapping[str, Any]) -> None:
|
|
252
|
+
atomic_write_json(
|
|
253
|
+
self.path, {"version": STORE_VERSION, "features": dict(features)}
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
# ── resolution ──────────────────────────────────────────────────────────
|
|
257
|
+
@staticmethod
|
|
258
|
+
def _definition(feature_id: str) -> FeatureDefinition:
|
|
259
|
+
definition = CATALOG_BY_ID.get(str(feature_id))
|
|
260
|
+
if definition is None:
|
|
261
|
+
raise UnknownFeature(str(feature_id))
|
|
262
|
+
return definition
|
|
263
|
+
|
|
264
|
+
@staticmethod
|
|
265
|
+
def _coerce(definition: FeatureDefinition, value: Any) -> Optional[Any]:
|
|
266
|
+
"""A stored/env value as this feature's type, or ``None`` if it is not.
|
|
267
|
+
|
|
268
|
+
Anything unrecognised resolves to ``None`` rather than raising, so a
|
|
269
|
+
hand-edited file with a typo falls through to the next layer instead of
|
|
270
|
+
taking the settings panel down.
|
|
271
|
+
"""
|
|
272
|
+
if definition.kind == CHOICE:
|
|
273
|
+
text = str(value).strip().lower()
|
|
274
|
+
return text if text in definition.choice_ids() else None
|
|
275
|
+
if isinstance(value, bool):
|
|
276
|
+
return value
|
|
277
|
+
text = str(value).strip().lower()
|
|
278
|
+
if text in TRUTHY:
|
|
279
|
+
return True
|
|
280
|
+
if text in FALSY:
|
|
281
|
+
return False
|
|
282
|
+
return None
|
|
283
|
+
|
|
284
|
+
def _env_value(self, definition: FeatureDefinition) -> Optional[Any]:
|
|
285
|
+
"""What the environment seeds this feature with (``None`` = nothing)."""
|
|
286
|
+
raw = os.getenv(definition.env_var, "").strip()
|
|
287
|
+
if not raw:
|
|
288
|
+
return None
|
|
289
|
+
return self._coerce(definition, raw)
|
|
290
|
+
|
|
291
|
+
def _resolve(
|
|
292
|
+
self, definition: FeatureDefinition, stored: Mapping[str, Any]
|
|
293
|
+
) -> Tuple[Any, str]:
|
|
294
|
+
"""``(value, source)`` under user → env → default precedence."""
|
|
295
|
+
if definition.id in stored:
|
|
296
|
+
user = self._coerce(definition, stored[definition.id])
|
|
297
|
+
if user is not None:
|
|
298
|
+
return user, "user"
|
|
299
|
+
seeded = self._env_value(definition)
|
|
300
|
+
if seeded is not None:
|
|
301
|
+
return seeded, "env"
|
|
302
|
+
return definition.default, "default"
|
|
303
|
+
|
|
304
|
+
def value(self, feature_id: str) -> Any:
|
|
305
|
+
"""The effective value of one feature right now."""
|
|
306
|
+
definition = self._definition(feature_id)
|
|
307
|
+
with self._lock:
|
|
308
|
+
stored = self._read()
|
|
309
|
+
return self._resolve(definition, stored)[0]
|
|
310
|
+
|
|
311
|
+
def user_value(self, feature_id: str) -> Optional[Any]:
|
|
312
|
+
"""What *this person* chose, or ``None`` if they never touched it.
|
|
313
|
+
|
|
314
|
+
The difference from :meth:`value` is the whole precedence story: the
|
|
315
|
+
switchboard speaks only for the switches someone actually moved, and
|
|
316
|
+
stays quiet about the rest so an operator's environment variable — and
|
|
317
|
+
the honest reporting that hangs off it, like an unknown backend name —
|
|
318
|
+
keeps working underneath.
|
|
319
|
+
"""
|
|
320
|
+
definition = self._definition(feature_id)
|
|
321
|
+
with self._lock:
|
|
322
|
+
stored = self._read()
|
|
323
|
+
if definition.id not in stored:
|
|
324
|
+
return None
|
|
325
|
+
return self._coerce(definition, stored[definition.id])
|
|
326
|
+
|
|
327
|
+
def enabled(self, feature_id: str) -> bool:
|
|
328
|
+
"""The effective value of one *toggle*, as a bool for gate binding."""
|
|
329
|
+
return bool(self.value(feature_id))
|
|
330
|
+
|
|
331
|
+
def resolver(
|
|
332
|
+
self, feature_id: str, fallback: Optional[Callable[[], bool]] = None
|
|
333
|
+
) -> Callable[[], bool]:
|
|
334
|
+
"""A zero-argument callable to hand :meth:`FeatureGate.bind`.
|
|
335
|
+
|
|
336
|
+
``fallback`` answers for a feature this person never touched — pass the
|
|
337
|
+
gate's own :meth:`FeatureGate.local` and an untouched switch behaves
|
|
338
|
+
byte-identically to an unbound gate, override and all. Without one, the
|
|
339
|
+
service answers from env → default itself.
|
|
340
|
+
"""
|
|
341
|
+
self._definition(feature_id) # fail loudly at wiring time, not per call
|
|
342
|
+
|
|
343
|
+
def _resolve() -> bool:
|
|
344
|
+
chosen = self.user_value(feature_id)
|
|
345
|
+
if chosen is not None:
|
|
346
|
+
return bool(chosen)
|
|
347
|
+
if fallback is not None:
|
|
348
|
+
return bool(fallback())
|
|
349
|
+
return self.enabled(feature_id)
|
|
350
|
+
|
|
351
|
+
return _resolve
|
|
352
|
+
|
|
353
|
+
def choice_resolver(self, feature_id: str) -> Callable[[], Optional[str]]:
|
|
354
|
+
"""A zero-argument callable for a *string* seam (the vector backend).
|
|
355
|
+
|
|
356
|
+
``None`` means "this person has not chosen", which the seam reads as
|
|
357
|
+
"ask the environment" — so an install that never opened the panel still
|
|
358
|
+
gets the env var's answer *and* its diagnostics (a typo'd backend name
|
|
359
|
+
is reported rather than quietly resolved to the default).
|
|
360
|
+
"""
|
|
361
|
+
self._definition(feature_id)
|
|
362
|
+
|
|
363
|
+
def _resolve() -> Optional[str]:
|
|
364
|
+
chosen = self.user_value(feature_id)
|
|
365
|
+
return None if chosen is None else str(chosen)
|
|
366
|
+
|
|
367
|
+
return _resolve
|
|
368
|
+
|
|
369
|
+
# ── availability ────────────────────────────────────────────────────────
|
|
370
|
+
def _availability(self, choice: FeatureChoice) -> Tuple[bool, str]:
|
|
371
|
+
"""``(installable, reason)`` for one option of a choice feature."""
|
|
372
|
+
if choice.probe is None:
|
|
373
|
+
return True, ""
|
|
374
|
+
probe = self.probes.get(choice.probe)
|
|
375
|
+
if probe is None:
|
|
376
|
+
# An install that registered no probe for this option cannot prove
|
|
377
|
+
# it is missing, and "we could not check" is not "not installed".
|
|
378
|
+
return True, ""
|
|
379
|
+
try:
|
|
380
|
+
return probe()
|
|
381
|
+
except Exception as exc: # noqa: BLE001 — a probe failure is "not installed"
|
|
382
|
+
return False, str(exc)
|
|
383
|
+
|
|
384
|
+
# ── rendering ───────────────────────────────────────────────────────────
|
|
385
|
+
def _render_choices(
|
|
386
|
+
self, definition: FeatureDefinition, language: str
|
|
387
|
+
) -> List[Dict[str, Any]]:
|
|
388
|
+
rendered: List[Dict[str, Any]] = []
|
|
389
|
+
for choice in definition.choices:
|
|
390
|
+
available, reason = self._availability(choice)
|
|
391
|
+
rendered.append(
|
|
392
|
+
{
|
|
393
|
+
"id": choice.id,
|
|
394
|
+
"label": translate(choice.label_key, language),
|
|
395
|
+
"available": available,
|
|
396
|
+
# An unavailable option says "install required" *and* what
|
|
397
|
+
# the import actually complained about, so the answer is
|
|
398
|
+
# actionable rather than a shrug.
|
|
399
|
+
"detail": (
|
|
400
|
+
None
|
|
401
|
+
if available
|
|
402
|
+
else translate(
|
|
403
|
+
"features.choice.install_required", language, reason=reason
|
|
404
|
+
)
|
|
405
|
+
),
|
|
406
|
+
}
|
|
407
|
+
)
|
|
408
|
+
return rendered
|
|
409
|
+
|
|
410
|
+
def _render(
|
|
411
|
+
self, definition: FeatureDefinition, stored: Mapping[str, Any], language: str
|
|
412
|
+
) -> Dict[str, Any]:
|
|
413
|
+
current, source = self._resolve(definition, stored)
|
|
414
|
+
return {
|
|
415
|
+
"id": definition.id,
|
|
416
|
+
"kind": definition.kind,
|
|
417
|
+
"label": translate(definition.label_key, language),
|
|
418
|
+
"summary": translate(definition.summary_key, language),
|
|
419
|
+
"default": definition.default,
|
|
420
|
+
"current": current,
|
|
421
|
+
"source": source,
|
|
422
|
+
"env_var": definition.env_var,
|
|
423
|
+
"live": definition.live,
|
|
424
|
+
"restart_required": not definition.live,
|
|
425
|
+
"caution": (
|
|
426
|
+
translate(definition.caution_key, language) if definition.caution else None
|
|
427
|
+
),
|
|
428
|
+
"parent": definition.parent,
|
|
429
|
+
"choices": self._render_choices(definition, language),
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
def catalog(self, language: str = DEFAULT_LANGUAGE) -> Dict[str, Any]:
|
|
433
|
+
"""The whole switchboard, localized, with each feature's live value."""
|
|
434
|
+
with self._lock:
|
|
435
|
+
stored = self._read()
|
|
436
|
+
return {
|
|
437
|
+
"features": [self._render(item, stored, language) for item in CATALOG],
|
|
438
|
+
"note": translate("features.note", language),
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
# ── writing ─────────────────────────────────────────────────────────────
|
|
442
|
+
def set(
|
|
443
|
+
self,
|
|
444
|
+
feature_id: str,
|
|
445
|
+
value: Any,
|
|
446
|
+
*,
|
|
447
|
+
language: str = DEFAULT_LANGUAGE,
|
|
448
|
+
user_email: Optional[str] = None,
|
|
449
|
+
) -> Dict[str, Any]:
|
|
450
|
+
"""Persist one person's choice and return the feature as it now reads.
|
|
451
|
+
|
|
452
|
+
Refuses a value the feature cannot take, including an option whose
|
|
453
|
+
optional dependency is not installed — accepting it would produce a
|
|
454
|
+
panel that shows ``hnsw`` while every search quietly ran brute force.
|
|
455
|
+
"""
|
|
456
|
+
definition = self._definition(feature_id)
|
|
457
|
+
coerced = self._coerce(definition, value)
|
|
458
|
+
if coerced is None:
|
|
459
|
+
raise InvalidFeatureValue(
|
|
460
|
+
translate("features.invalid_value", language, value=str(value))
|
|
461
|
+
)
|
|
462
|
+
if definition.kind == CHOICE:
|
|
463
|
+
choice = next(item for item in definition.choices if item.id == coerced)
|
|
464
|
+
available, reason = self._availability(choice)
|
|
465
|
+
if not available:
|
|
466
|
+
raise InvalidFeatureValue(
|
|
467
|
+
translate("features.choice.install_required", language, reason=reason)
|
|
468
|
+
)
|
|
469
|
+
with self._lock:
|
|
470
|
+
stored = self._read()
|
|
471
|
+
previous = self._resolve(definition, stored)[0]
|
|
472
|
+
stored[definition.id] = coerced
|
|
473
|
+
self._write(stored)
|
|
474
|
+
audit = self.audit
|
|
475
|
+
# Rendered outside the lock: it may run an availability probe, and the
|
|
476
|
+
# first one imports an optional compiled extension.
|
|
477
|
+
rendered = self._render(definition, stored, language)
|
|
478
|
+
if audit is not None:
|
|
479
|
+
audit(
|
|
480
|
+
"feature_toggle_changed",
|
|
481
|
+
feature=definition.id,
|
|
482
|
+
previous=previous,
|
|
483
|
+
value=coerced,
|
|
484
|
+
user_email=user_email,
|
|
485
|
+
)
|
|
486
|
+
return rendered
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
__all__ = [
|
|
490
|
+
"CATALOG",
|
|
491
|
+
"CATALOG_BY_ID",
|
|
492
|
+
"CHOICE",
|
|
493
|
+
"DEFAULT_PROBES",
|
|
494
|
+
"STORE_FILENAME",
|
|
495
|
+
"STORE_VERSION",
|
|
496
|
+
"TOGGLE",
|
|
497
|
+
"FeatureChoice",
|
|
498
|
+
"FeatureDefinition",
|
|
499
|
+
"FeatureToggleService",
|
|
500
|
+
"InvalidFeatureValue",
|
|
501
|
+
"UnknownFeature",
|
|
502
|
+
]
|