ltcai 11.2.0 → 11.4.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.
Files changed (247) hide show
  1. package/README.md +46 -53
  2. package/docs/CHANGELOG.md +61 -0
  3. package/docs/COMMUNITY_AND_PLUGINS.md +1 -1
  4. package/docs/DEVELOPMENT.md +1 -1
  5. package/docs/MULTI_AGENT_RUNTIME.md +1 -1
  6. package/docs/ONBOARDING.md +1 -1
  7. package/docs/OPERATIONS.md +6 -2
  8. package/docs/PERMISSION_MODE.md +1 -1
  9. package/docs/TRUST_MODEL.md +1 -1
  10. package/docs/WHY_LATTICE.md +1 -1
  11. package/docs/kg-schema.md +2 -2
  12. package/docs/v11.3.0_PLAN.md +202 -0
  13. package/docs/v11.4.0_RUST_FOUNDATION_PLAN.md +176 -0
  14. package/lattice_brain/__init__.py +1 -1
  15. package/lattice_brain/graph/_kg_common/__init__.py +287 -0
  16. package/lattice_brain/graph/_kg_common/extraction.py +516 -0
  17. package/lattice_brain/graph/_kg_common/relations.py +161 -0
  18. package/lattice_brain/graph/_kg_common/text.py +479 -0
  19. package/lattice_brain/graph/discovery_index/__init__.py +35 -0
  20. package/lattice_brain/graph/discovery_index/cleanup.py +182 -0
  21. package/lattice_brain/graph/discovery_index/extract.py +137 -0
  22. package/lattice_brain/graph/discovery_index/scan.py +411 -0
  23. package/lattice_brain/graph/discovery_index/upsert.py +495 -0
  24. package/lattice_brain/graph/projection/__init__.py +42 -0
  25. package/lattice_brain/graph/projection/curation.py +500 -0
  26. package/lattice_brain/graph/{projection.py → projection/v2_schema.py} +15 -477
  27. package/lattice_brain/graph/retrieval/__init__.py +54 -0
  28. package/lattice_brain/graph/retrieval/context.py +197 -0
  29. package/lattice_brain/graph/retrieval/graph_view.py +319 -0
  30. package/lattice_brain/graph/retrieval/hybrid.py +488 -0
  31. package/lattice_brain/graph/retrieval/maintenance.py +121 -0
  32. package/lattice_brain/graph/retrieval/signals.py +95 -0
  33. package/lattice_brain/graph/retrieval_vector/__init__.py +42 -0
  34. package/lattice_brain/graph/retrieval_vector/fingerprint.py +97 -0
  35. package/lattice_brain/graph/retrieval_vector/indexing.py +347 -0
  36. package/lattice_brain/graph/retrieval_vector/search.py +560 -0
  37. package/lattice_brain/graph/retrieval_vector/status.py +374 -0
  38. package/lattice_brain/ingestion/__init__.py +130 -0
  39. package/lattice_brain/ingestion/_contract.py +90 -0
  40. package/lattice_brain/ingestion/constants.py +127 -0
  41. package/lattice_brain/ingestion/folder_scan.py +57 -0
  42. package/lattice_brain/ingestion/folders.py +258 -0
  43. package/lattice_brain/ingestion/hashing.py +26 -0
  44. package/lattice_brain/ingestion/jobs_api.py +107 -0
  45. package/lattice_brain/ingestion/models.py +80 -0
  46. package/lattice_brain/ingestion/pipeline.py +486 -0
  47. package/lattice_brain/ingestion/quality.py +209 -0
  48. package/lattice_brain/ingestion/routing.py +295 -0
  49. package/lattice_brain/multimodal/__init__.py +164 -0
  50. package/lattice_brain/multimodal/audio.py +77 -0
  51. package/lattice_brain/multimodal/common.py +118 -0
  52. package/lattice_brain/multimodal/images.py +498 -0
  53. package/lattice_brain/multimodal/ports.py +169 -0
  54. package/lattice_brain/multimodal/video.py +410 -0
  55. package/lattice_brain/portability/__init__.py +90 -0
  56. package/lattice_brain/portability/_contract.py +42 -0
  57. package/lattice_brain/portability/backups.py +338 -0
  58. package/lattice_brain/portability/bundles.py +136 -0
  59. package/lattice_brain/portability/constants.py +93 -0
  60. package/lattice_brain/portability/fsops.py +138 -0
  61. package/lattice_brain/portability/service.py +41 -0
  62. package/lattice_brain/{portability.py → portability/sharing.py} +44 -677
  63. package/lattice_brain/runtime/__init__.py +1 -1
  64. package/lattice_brain/runtime/multi_agent.py +1 -1
  65. package/latticeai/__init__.py +1 -1
  66. package/latticeai/api/chronicle.py +63 -0
  67. package/latticeai/core/agent/__init__.py +93 -0
  68. package/latticeai/core/agent/_contract.py +79 -0
  69. package/latticeai/core/agent/context.py +57 -0
  70. package/latticeai/core/agent/deps.py +125 -0
  71. package/latticeai/core/agent/execution.py +622 -0
  72. package/latticeai/core/agent/planning.py +145 -0
  73. package/latticeai/core/agent/recovery.py +157 -0
  74. package/latticeai/core/agent/runtime.py +210 -0
  75. package/latticeai/core/agent/verification.py +231 -0
  76. package/latticeai/core/embedding_providers/__init__.py +151 -0
  77. package/latticeai/core/embedding_providers/base.py +199 -0
  78. package/latticeai/core/embedding_providers/captions.py +162 -0
  79. package/latticeai/core/embedding_providers/profiles.py +126 -0
  80. package/latticeai/core/embedding_providers/text.py +350 -0
  81. package/latticeai/core/embedding_providers/vision.py +352 -0
  82. package/latticeai/core/file_generation/__init__.py +115 -0
  83. package/latticeai/core/file_generation/bundles.py +76 -0
  84. package/latticeai/core/file_generation/extraction.py +154 -0
  85. package/latticeai/core/file_generation/inference.py +235 -0
  86. package/latticeai/core/file_generation/orchestration.py +152 -0
  87. package/latticeai/core/file_generation/prompting.py +117 -0
  88. package/latticeai/core/file_generation/repair.py +114 -0
  89. package/latticeai/core/file_generation/sanitize.py +61 -0
  90. package/latticeai/core/file_generation/validation.py +201 -0
  91. package/latticeai/core/legacy_compatibility.py +1 -1
  92. package/latticeai/core/marketplace.py +1 -1
  93. package/latticeai/core/messages.py +9 -0
  94. package/latticeai/core/workspace_os_constants.py +1 -1
  95. package/latticeai/integrations/telegram_bot/__init__.py +123 -0
  96. package/latticeai/integrations/telegram_bot/__main__.py +17 -0
  97. package/latticeai/integrations/telegram_bot/config.py +86 -0
  98. package/latticeai/integrations/telegram_bot/dispatch.py +311 -0
  99. package/latticeai/integrations/telegram_bot/flows.py +478 -0
  100. package/latticeai/integrations/telegram_bot/helpers.py +322 -0
  101. package/latticeai/integrations/telegram_bot/screens.py +394 -0
  102. package/latticeai/models/router/__init__.py +88 -0
  103. package/latticeai/models/router/_contract.py +66 -0
  104. package/latticeai/models/router/branding.py +56 -0
  105. package/latticeai/models/router/catalog.py +69 -0
  106. package/latticeai/models/router/documents.py +199 -0
  107. package/latticeai/models/router/errors.py +37 -0
  108. package/latticeai/models/router/generation.py +258 -0
  109. package/latticeai/models/router/loading.py +291 -0
  110. package/latticeai/models/router/local_models.py +85 -0
  111. package/latticeai/models/router/registry.py +147 -0
  112. package/latticeai/runtime/build_phases/__init__.py +82 -0
  113. package/latticeai/runtime/build_phases/features.py +407 -0
  114. package/latticeai/runtime/build_phases/foundation.py +555 -0
  115. package/latticeai/runtime/build_phases/web.py +492 -0
  116. package/latticeai/runtime/runtime_context.py +1 -0
  117. package/latticeai/services/architecture_readiness.py +48 -19
  118. package/latticeai/services/brain_intelligence/__init__.py +58 -0
  119. package/latticeai/services/brain_intelligence/_contract.py +71 -0
  120. package/latticeai/services/brain_intelligence/consistency.py +193 -0
  121. package/latticeai/services/brain_intelligence/constants.py +47 -0
  122. package/latticeai/services/brain_intelligence/digest.py +258 -0
  123. package/latticeai/services/brain_intelligence/health.py +331 -0
  124. package/latticeai/services/brain_intelligence/proposals.py +264 -0
  125. package/latticeai/services/brain_intelligence/sampling.py +84 -0
  126. package/latticeai/services/brain_intelligence/service.py +48 -0
  127. package/latticeai/services/chronicle.py +557 -0
  128. package/latticeai/services/memory_service/__init__.py +52 -0
  129. package/latticeai/services/memory_service/_contract.py +100 -0
  130. package/latticeai/services/memory_service/brief.py +431 -0
  131. package/latticeai/services/memory_service/constants.py +57 -0
  132. package/latticeai/services/memory_service/maintenance.py +138 -0
  133. package/latticeai/services/memory_service/manager.py +186 -0
  134. package/latticeai/services/memory_service/proof.py +136 -0
  135. package/latticeai/services/memory_service/recall.py +225 -0
  136. package/latticeai/services/memory_service/service.py +48 -0
  137. package/latticeai/services/memory_service/stores.py +110 -0
  138. package/latticeai/services/model_runtime/__init__.py +322 -0
  139. package/latticeai/services/model_runtime/cloud.py +87 -0
  140. package/latticeai/services/model_runtime/download.py +282 -0
  141. package/latticeai/services/model_runtime/engines.py +341 -0
  142. package/latticeai/services/model_runtime/loading.py +178 -0
  143. package/latticeai/services/model_runtime/service.py +129 -0
  144. package/latticeai/services/model_runtime/state.py +131 -0
  145. package/latticeai/services/model_runtime/status.py +255 -0
  146. package/latticeai/services/product_readiness.py +15 -7
  147. package/latticeai/setup/wizard/__init__.py +126 -0
  148. package/latticeai/setup/wizard/catalog.py +172 -0
  149. package/latticeai/setup/wizard/detect.py +323 -0
  150. package/latticeai/setup/wizard/install.py +348 -0
  151. package/latticeai/setup/wizard/paths.py +168 -0
  152. package/latticeai/setup/wizard/plans.py +74 -0
  153. package/latticeai/setup/wizard/recommend.py +320 -0
  154. package/package.json +6 -2
  155. package/scripts/bump_version.py +14 -0
  156. package/scripts/capture_release_evidence.mjs +33 -21
  157. package/scripts/check_current_release_docs.mjs +1 -1
  158. package/scripts/check_i18n_namespace_coverage.mjs +41 -4
  159. package/scripts/check_max_file_lines.mjs +102 -0
  160. package/scripts/check_release_evidence_bound.mjs +30 -15
  161. package/scripts/check_screenshot_pixel_delta.py +34 -4
  162. package/scripts/check_server_i18n.mjs +1 -0
  163. package/scripts/generate_rust_parity_fixtures.py +562 -0
  164. package/scripts/lib/mock_server_fingerprint.mjs +94 -0
  165. package/scripts/release_screen_claims.json +31 -2
  166. package/src-tauri/Cargo.lock +361 -3
  167. package/src-tauri/Cargo.toml +6 -1
  168. package/src-tauri/src/backend.rs +349 -0
  169. package/src-tauri/src/folder.rs +33 -0
  170. package/src-tauri/src/main.rs +97 -399
  171. package/src-tauri/tauri.conf.json +1 -1
  172. package/static/app/asset-manifest.json +41 -37
  173. package/static/app/assets/Act-yYpYnn0v.js +1 -0
  174. package/static/app/assets/AdminConsole-DL3Cr5pL.js +1 -0
  175. package/static/app/assets/{Brain-tuhI4sOC.js → Brain-C1HBN0Wf.js} +2 -2
  176. package/static/app/assets/BrainHome-DoXRhUUC.js +2 -0
  177. package/static/app/assets/BrainSignals-6yR6ir5t.js +1 -0
  178. package/static/app/assets/Capture-CFIRsFNE.js +1 -0
  179. package/static/app/assets/Chronicle-BZbEgiwN.js +1 -0
  180. package/static/app/assets/CommandPalette-D2pMxC2I.js +1 -0
  181. package/static/app/assets/Library-DwO3yZST.js +1 -0
  182. package/static/app/assets/{LivingBrain-DBwhto14.js → LivingBrain-Jn1GK0-S.js} +1 -1
  183. package/static/app/assets/ProductFlow-B-w1R4Oo.js +1 -0
  184. package/static/app/assets/ReviewCard-6B27X8Vg.js +3 -0
  185. package/static/app/assets/System-DW8F-2xL.js +1 -0
  186. package/static/app/assets/arrow-left-DXvKg9U6.js +1 -0
  187. package/static/app/assets/{bot-Cia42c2h.js → bot-IM_E_Y12.js} +1 -1
  188. package/static/app/assets/brain-Ci1CkWjM.js +1 -0
  189. package/static/app/assets/{button-2j2Ijzgq.js → button-COwyqfHM.js} +1 -1
  190. package/static/app/assets/circle-check-DfInj-qD.js +1 -0
  191. package/static/app/assets/{circle-pause-BEFeWpVW.js → circle-pause-DEM4A1Y5.js} +1 -1
  192. package/static/app/assets/{circle-play-ujXMcHxl.js → circle-play-C9djDuLd.js} +1 -1
  193. package/static/app/assets/{cpu-k4awryFq.js → cpu-DFdo1gw-.js} +1 -1
  194. package/static/app/assets/{download-DFbLJ_ig.js → download-SnJL6oqk.js} +1 -1
  195. package/static/app/assets/{folder-open-7y_b6xkM.js → folder-open-CqZeDkjE.js} +1 -1
  196. package/static/app/assets/{hard-drive-Bidh02Kr.js → hard-drive-j1jJXYYf.js} +1 -1
  197. package/static/app/assets/{index-DwDl9-8Y.css → index-BLPb5lmE.css} +1 -1
  198. package/static/app/assets/index-_u5iUHDr.js +10 -0
  199. package/static/app/assets/input-B0lPdRQZ.js +1 -0
  200. package/static/app/assets/link-2-CoFbooHS.js +1 -0
  201. package/static/app/assets/{permissionCopy-Bpb83Hx9.js → permissionCopy-BsyLxtao.js} +1 -1
  202. package/static/app/assets/primitives-DEbN-d6p.js +1 -0
  203. package/static/app/assets/search-BybIWPNd.js +1 -0
  204. package/static/app/assets/{share-2-BH1M-WNi.js → share-2-CVtZ_ewX.js} +1 -1
  205. package/static/app/assets/{shield-alert-BlKdBXcG.js → shield-alert-CBi2GNWM.js} +1 -1
  206. package/static/app/assets/{textarea-CCWbUfFB.js → textarea-DNMpB5ih.js} +1 -1
  207. package/static/app/assets/{useFocusTrap-YdHQ7pJ1.js → useFocusTrap-C83t3GXF.js} +1 -1
  208. package/static/app/assets/useMutation-DtbJDoyz.js +1 -0
  209. package/static/app/assets/{useQuery-CXQiwbVT.js → useQuery-Dcp1OChy.js} +1 -1
  210. package/static/app/assets/utils-BlZr7Pd4.js +4 -0
  211. package/static/app/assets/workspace-jJY4RuAV.js +1 -0
  212. package/static/app/index.html +4 -4
  213. package/static/sw.js +1 -1
  214. package/lattice_brain/graph/_kg_common.py +0 -1331
  215. package/lattice_brain/graph/discovery_index.py +0 -1141
  216. package/lattice_brain/graph/retrieval.py +0 -1120
  217. package/lattice_brain/graph/retrieval_vector.py +0 -1293
  218. package/lattice_brain/ingestion.py +0 -1525
  219. package/lattice_brain/multimodal.py +0 -1258
  220. package/latticeai/core/agent.py +0 -1465
  221. package/latticeai/core/embedding_providers.py +0 -1196
  222. package/latticeai/core/file_generation.py +0 -1047
  223. package/latticeai/integrations/telegram_bot.py +0 -1390
  224. package/latticeai/models/router.py +0 -1007
  225. package/latticeai/runtime/build_phases.py +0 -1450
  226. package/latticeai/services/brain_intelligence.py +0 -1083
  227. package/latticeai/services/memory_service.py +0 -1177
  228. package/latticeai/services/model_runtime.py +0 -1281
  229. package/latticeai/setup/wizard.py +0 -1310
  230. package/static/app/assets/Act-AWf0SAKp.js +0 -1
  231. package/static/app/assets/AdminConsole-D0u8Tiyj.js +0 -1
  232. package/static/app/assets/BrainHome-Ts7G_Ila.js +0 -2
  233. package/static/app/assets/BrainSignals-jMYgQ2Ar.js +0 -1
  234. package/static/app/assets/Capture-CqOSzyPr.js +0 -1
  235. package/static/app/assets/CommandPalette-DC0Bzh-I.js +0 -1
  236. package/static/app/assets/Library-CX-bbhmK.js +0 -1
  237. package/static/app/assets/ProductFlow-BHA2cfKI.js +0 -1
  238. package/static/app/assets/ReviewCard-BUhCKRNM.js +0 -3
  239. package/static/app/assets/System-Bu2t5hn1.js +0 -1
  240. package/static/app/assets/arrow-left-Dzwa5zRb.js +0 -1
  241. package/static/app/assets/brain-DJMoqrwx.js +0 -1
  242. package/static/app/assets/index-BpYkzcVm.js +0 -10
  243. package/static/app/assets/input-DSlJJxRs.js +0 -1
  244. package/static/app/assets/primitives-BCx6TvfG.js +0 -1
  245. package/static/app/assets/search-Cgy8cCFJ.js +0 -1
  246. package/static/app/assets/utils-zqPZJxdx.js +0 -4
  247. package/static/app/assets/workspace-DXTihhfU.js +0 -1
@@ -0,0 +1,331 @@
1
+ """The scored diagnosis, and the vector index's freshness.
2
+
3
+ :meth:`health_report` grades four dimensions — freshness, connectivity,
4
+ embedding coverage, consistency — from the live stores, and is deliberate about
5
+ what it refuses to grade. A dimension it cannot measure scores ``None`` with a
6
+ stated reason rather than a flattering number (an empty index covers 100% of
7
+ nothing; scoring that as a perfect 100 is how a brand-new Brain used to grade
8
+ itself "excellent"). The composite reports its own ``coverage``, and when
9
+ nothing could be measured it says why — the 9.9.7 rule that a "—" always states
10
+ its reason.
11
+
12
+ :meth:`vector_freshness` is a fixed four-key contract that never raises, plus
13
+ an additive ``breakdown`` when the store can split "never embedded" from
14
+ "embedded but stale".
15
+ """
16
+
17
+ from __future__ import annotations
18
+
19
+ from datetime import datetime, timedelta, timezone
20
+ from typing import Any, Dict, List, Optional
21
+
22
+ from latticeai.core.timeutil import now_iso as _now
23
+
24
+ from ._contract import BrainIntelligenceCore as _Core
25
+ from .constants import _STALE_DAYS, LOGGER, _no_graph_reason, _parse_ts
26
+
27
+
28
+ class BrainHealthMixin(_Core):
29
+ """Health report + vector freshness. Mixed into the service."""
30
+
31
+ # ── health report ─────────────────────────────────────────────────────
32
+
33
+ def health_report(
34
+ self, *, user_email: Optional[str] = None, workspace_id: Optional[str] = None
35
+ ) -> Dict[str, Any]:
36
+ sample = self._graph_sample(workspace_id=workspace_id)
37
+ nodes, edges = sample["nodes"], sample["edges"]
38
+ now = datetime.now(timezone.utc)
39
+ dimensions: Dict[str, Dict[str, Any]] = {}
40
+
41
+ # Freshness — how much of the sampled knowledge saw recent updates.
42
+ if sample["available"] and nodes:
43
+ stale_cutoff = now - timedelta(days=_STALE_DAYS)
44
+ dated = [(_parse_ts(n.get("updated_at")), n) for n in nodes]
45
+ known = [pair for pair in dated if pair[0] is not None]
46
+ stale = [n for ts, n in known if ts is not None and ts < stale_cutoff]
47
+ fresh_ratio = 1.0 - (len(stale) / len(known)) if known else 0.0
48
+ dimensions["freshness"] = {
49
+ "status": "ok",
50
+ "score": round(fresh_ratio * 100),
51
+ "sampled": len(nodes),
52
+ "stale_nodes": len(stale),
53
+ "stale_threshold_days": _STALE_DAYS,
54
+ }
55
+ else:
56
+ dimensions["freshness"] = {
57
+ "status": "unavailable",
58
+ "score": None,
59
+ "reason": _no_graph_reason(sample["available"]),
60
+ }
61
+
62
+ # Connectivity — orphan nodes are knowledge the Brain cannot reason
63
+ # across; a well-tended graph keeps them rare.
64
+ if sample["available"] and nodes:
65
+ connected = set()
66
+ for edge in edges:
67
+ connected.add(str(edge.get("source") or edge.get("from_node") or ""))
68
+ connected.add(str(edge.get("target") or edge.get("to_node") or ""))
69
+ orphans = [n for n in nodes if str(n.get("id")) not in connected]
70
+ ratio = 1.0 - (len(orphans) / len(nodes))
71
+ dimensions["connectivity"] = {
72
+ "status": "ok",
73
+ "score": round(ratio * 100),
74
+ "sampled": len(nodes),
75
+ "orphan_nodes": len(orphans),
76
+ "edges": len(edges),
77
+ }
78
+ else:
79
+ dimensions["connectivity"] = {
80
+ "status": "unavailable",
81
+ "score": None,
82
+ "reason": _no_graph_reason(sample["available"]),
83
+ }
84
+
85
+ # Embedding coverage — semantic recall only works for indexed items.
86
+ index_status: Dict[str, Any] = {}
87
+ if self._enable_graph and hasattr(self._kg, "index_status"):
88
+ try:
89
+ index_status = self._kg.index_status()
90
+ except Exception as exc:
91
+ LOGGER.exception("brain intelligence index status failed")
92
+ index_status = {"error": str(exc)}
93
+ scale = index_status.get("scale") or {}
94
+ indexable = scale.get("source_items", index_status.get("source_items"))
95
+ if "coverage_ratio" not in scale:
96
+ dimensions["embedding_coverage"] = {
97
+ "status": "unavailable",
98
+ "score": None,
99
+ "reason": "this knowledge store does not report vector index coverage",
100
+ }
101
+ elif indexable == 0:
102
+ # An empty index covers 100% of nothing. Scoring that as a perfect
103
+ # 100 is how a brand-new Brain used to grade itself "excellent"
104
+ # off its only measurable dimension (audit v11.2.0, Finding 3).
105
+ dimensions["embedding_coverage"] = {
106
+ "status": "unavailable",
107
+ "score": None,
108
+ "reason": "no indexable items yet",
109
+ }
110
+ else:
111
+ dimensions["embedding_coverage"] = {
112
+ "status": "ok",
113
+ "score": round(float(scale["coverage_ratio"]) * 100),
114
+ "ready_items": scale.get("ready_items"),
115
+ "pending_items": scale.get("pending_items"),
116
+ "needs_reindex": index_status.get("status") == "needs_reindex",
117
+ }
118
+
119
+ # Edge quality + contradiction pressure — reuses the quality layer.
120
+ if sample["available"] and edges:
121
+ metrics = self._edge_quality.compute_quality_metrics(edges)
122
+ contradiction_edges = [
123
+ e for e in edges if "CONTRADICT" in str(e.get("type") or "").upper()
124
+ ]
125
+ pressure = min(1.0, metrics.get("dup_rate", 0.0) + len(contradiction_edges) / max(len(edges), 1))
126
+ dimensions["consistency"] = {
127
+ "status": "ok",
128
+ "score": round((1.0 - pressure) * 100),
129
+ "edge_metrics": metrics,
130
+ "contradiction_edges": len(contradiction_edges),
131
+ }
132
+ else:
133
+ dimensions["consistency"] = {
134
+ "status": "unavailable",
135
+ "score": None,
136
+ "reason": (
137
+ _no_graph_reason(sample["available"])
138
+ if not (sample["available"] and nodes)
139
+ else "no relationships recorded yet"
140
+ ),
141
+ }
142
+
143
+ scores = [d["score"] for d in dimensions.values() if d.get("score") is not None]
144
+ overall = round(sum(scores) / len(scores)) if scores else None
145
+ grade = (
146
+ None if overall is None
147
+ else "excellent" if overall >= 85
148
+ else "good" if overall >= 70
149
+ else "attention" if overall >= 50
150
+ else "critical"
151
+ )
152
+ # What the verdict rests on. A composite averages only what could be
153
+ # measured, so the count of measured dimensions is part of the answer
154
+ # rather than a footnote — and when nothing could be measured the
155
+ # report says so instead of leaving a bare null (the 9.9.7 rule: a
156
+ # "—" always states why).
157
+ unmeasured = sorted(
158
+ name for name, dim in dimensions.items() if dim.get("score") is None
159
+ )
160
+ coverage: Dict[str, Any] = {
161
+ "measured": len(scores),
162
+ "total": len(dimensions),
163
+ "unavailable": unmeasured,
164
+ "partial": bool(unmeasured),
165
+ }
166
+ reason: Optional[str] = None
167
+ if overall is None:
168
+ reason = (
169
+ "no health dimension could be measured yet — "
170
+ + "; ".join(
171
+ f"{name}: {dimensions[name].get('reason') or 'unavailable'}"
172
+ for name in unmeasured
173
+ )
174
+ )
175
+
176
+ actions: List[Dict[str, str]] = []
177
+ emb = dimensions["embedding_coverage"]
178
+ if emb.get("needs_reindex"):
179
+ actions.append({
180
+ "id": "rebuild_vector_index",
181
+ "reason": f"{emb.get('pending_items', 0)} items are missing or stale in the vector index.",
182
+ })
183
+ conn_dim = dimensions["connectivity"]
184
+ conn_score = conn_dim.get("score")
185
+ if conn_score is not None and conn_score < 70:
186
+ actions.append({
187
+ "id": "review_orphans",
188
+ "reason": f"{conn_dim.get('orphan_nodes', 0)} nodes have no relationships.",
189
+ })
190
+ fresh_dim = dimensions["freshness"]
191
+ fresh_score = fresh_dim.get("score")
192
+ if fresh_score is not None and fresh_score < 60:
193
+ actions.append({
194
+ "id": "refresh_stale_knowledge",
195
+ "reason": f"{fresh_dim.get('stale_nodes', 0)} nodes untouched for over {_STALE_DAYS} days.",
196
+ })
197
+ cons_dim = dimensions["consistency"]
198
+ if cons_dim.get("contradiction_edges"):
199
+ actions.append({
200
+ "id": "resolve_contradictions",
201
+ "reason": f"{cons_dim['contradiction_edges']} contradiction edges recorded in the graph.",
202
+ })
203
+
204
+ report: Dict[str, Any] = {
205
+ "overall_score": overall,
206
+ "grade": grade,
207
+ "dimensions": dimensions,
208
+ "coverage": coverage,
209
+ "recommended_actions": actions,
210
+ "graph_available": sample["available"],
211
+ "generated_at": _now(),
212
+ }
213
+ if reason is not None:
214
+ report["reason"] = reason
215
+ return report
216
+
217
+ # ── vector freshness (v9.8.0) ────────────────────────────────────────
218
+
219
+ def vector_freshness(
220
+ self, *, user_email: Optional[str] = None, workspace_id: Optional[str] = None
221
+ ) -> Dict[str, Any]:
222
+ """Fixed-contract vector index freshness for ``/api/brain/vector-freshness``.
223
+
224
+ Always returns ``{"status": "ready"|"pending"|"unavailable",
225
+ "pending_items": int, "total_items": int, "detail": str}`` and never
226
+ raises. The vector index is store-global (not workspace-partitioned);
227
+ scope arguments are accepted for router symmetry but do not narrow
228
+ the report.
229
+
230
+ Since v11.2.0 a store that can split its backlog also gets a
231
+ ``breakdown`` key (see :meth:`_freshness_breakdown`). It is additive:
232
+ the four keys above keep their meaning and their types, so the
233
+ freshness chip that reads ``pending_items`` is untouched, and a store
234
+ without the split simply has no ``breakdown``.
235
+ """
236
+ payload = self._vector_freshness_contract()
237
+ breakdown = self._freshness_breakdown()
238
+ if breakdown is not None:
239
+ payload["breakdown"] = breakdown
240
+ return payload
241
+
242
+ def _freshness_breakdown(self) -> Optional[Dict[str, Any]]:
243
+ """``vector_freshness_breakdown()`` from the store, or ``None``.
244
+
245
+ "12 pending" hides two different situations — twelve items never
246
+ embedded (a new import) and twelve whose text changed under an
247
+ existing embedding (edits, where current answers are quietly wrong).
248
+ The store has always known the difference; until now nothing asked it.
249
+
250
+ ``None`` covers every reason the split is not available (graph off,
251
+ an older store, an unreadable index), because a caller can only act on
252
+ numbers that were really measured — never on zeros standing in for
253
+ them.
254
+ """
255
+ if not self._enable_graph or self._kg is None:
256
+ return None
257
+ breakdown_fn = getattr(self._kg, "vector_freshness_breakdown", None)
258
+ if not callable(breakdown_fn):
259
+ return None
260
+ try:
261
+ raw = breakdown_fn()
262
+ except Exception:
263
+ LOGGER.exception("vector freshness breakdown read failed")
264
+ return None
265
+ # An empty or unrecognisable answer is "not measured", which is what
266
+ # ``None`` already means here — publishing an empty block would claim
267
+ # a split nobody computed.
268
+ if not isinstance(raw, dict) or not raw:
269
+ return None
270
+ return dict(raw)
271
+
272
+ def _vector_freshness_contract(self) -> Dict[str, Any]:
273
+ """The four-key freshness payload, unchanged since v9.8.0."""
274
+
275
+ def _unavailable(detail: str) -> Dict[str, Any]:
276
+ return {
277
+ "status": "unavailable",
278
+ "pending_items": 0,
279
+ "total_items": 0,
280
+ "detail": detail,
281
+ }
282
+
283
+ if not self._enable_graph or self._kg is None:
284
+ return _unavailable("knowledge graph is disabled; no vector index is configured")
285
+
286
+ freshness_fn = getattr(self._kg, "vector_freshness", None)
287
+ if callable(freshness_fn):
288
+ try:
289
+ raw = freshness_fn() or {}
290
+ except Exception as exc:
291
+ LOGGER.exception("vector freshness read failed")
292
+ return _unavailable(f"vector freshness read failed: {exc}")
293
+ status = str(raw.get("status") or "unavailable")
294
+ if status == "needs_reindex":
295
+ status = "pending"
296
+ if status not in {"ready", "pending", "unavailable"}:
297
+ status = "unavailable"
298
+ return {
299
+ "status": status,
300
+ "pending_items": int(raw.get("pending_items") or 0),
301
+ "total_items": int(raw.get("total_items") or 0),
302
+ "detail": str(raw.get("detail") or ""),
303
+ }
304
+
305
+ # Older/lighter stores: summarize index_status directly.
306
+ status_fn = getattr(self._kg, "index_status", None)
307
+ if callable(status_fn):
308
+ try:
309
+ raw = status_fn() or {}
310
+ except Exception as exc:
311
+ LOGGER.exception("vector index status read failed")
312
+ return _unavailable(f"vector index status unavailable: {exc}")
313
+ pending = int(raw.get("pending_items") or 0)
314
+ total = int(raw.get("source_items") or 0)
315
+ if pending > 0:
316
+ return {
317
+ "status": "pending",
318
+ "pending_items": pending,
319
+ "total_items": total,
320
+ "detail": (
321
+ f"{pending} of {total} items are missing or stale in the vector index"
322
+ ),
323
+ }
324
+ return {
325
+ "status": "ready",
326
+ "pending_items": 0,
327
+ "total_items": total,
328
+ "detail": "vector index is up to date",
329
+ }
330
+
331
+ return _unavailable("this knowledge store does not expose a vector index")
@@ -0,0 +1,264 @@
1
+ """Synthesis never writes: it proposes, and the Review Center decides.
2
+
3
+ Everything the Brain notices on its own arrives as a review proposal — new
4
+ connections, contradictions, consolidation candidates, decayed importance. That
5
+ makes the review queue a hard dependency of every method here: when it is
6
+ absent they report ``available: False`` rather than falling back to a direct
7
+ write.
8
+
9
+ :meth:`proactive_brief` is the read-only counterpart. It counts what is already
10
+ waiting rather than raising anything new, so opening the home screen never
11
+ mutates the Brain.
12
+ """
13
+
14
+ from __future__ import annotations
15
+
16
+ from typing import Any, Dict, List, Optional
17
+
18
+ from latticeai.core.timeutil import now_iso as _now
19
+
20
+ from ._contract import BrainIntelligenceCore as _Core
21
+ from .constants import LOGGER
22
+
23
+
24
+ class BrainProposalsMixin(_Core):
25
+ """The proposal path and the proactive brief. Mixed into the service."""
26
+
27
+ # ── proposal path (v11.1.0) ───────────────────────────────────────────
28
+ #
29
+ # Synthesis never writes: it proposes, and the Review Center decides. That
30
+ # makes the review queue a hard dependency of every method below — when it
31
+ # is absent they report ``available: False`` rather than falling back to a
32
+ # direct write.
33
+
34
+ def attach_review_queue(self, review_queue: Any) -> None:
35
+ """Bind the review queue proposals are written to (composition root)."""
36
+ self._review_queue_service = review_queue
37
+ self._synthesizer = None
38
+
39
+ def _review_queue(self) -> Any:
40
+ """The queue to propose into, or ``None`` when there is none.
41
+
42
+ Prefers an explicitly injected service. Otherwise it builds one over
43
+ the workspace store the memory service already holds — the same
44
+ ``WorkspaceOSStore`` every other ``ReviewQueueService`` in the process
45
+ is constructed over, so proposals land in the one inbox the user reads
46
+ even before the composition root injects the service directly.
47
+ """
48
+ if self._review_queue_service is not None:
49
+ return self._review_queue_service
50
+ store = getattr(self._memory, "_store", None)
51
+ if store is None or not hasattr(store, "create_review_item"):
52
+ return None
53
+ from latticeai.services.review_queue import ReviewQueueService
54
+
55
+ self._review_queue_service = ReviewQueueService(store=store)
56
+ return self._review_queue_service
57
+
58
+ def _synthesis(self) -> Any:
59
+ """Lazy :class:`BrainSynthesizer` over the graph + review queue."""
60
+ if self._synthesizer is not None:
61
+ return self._synthesizer
62
+ queue = self._review_queue()
63
+ if not self._enable_graph or queue is None:
64
+ return None
65
+ from lattice_brain.synthesis import BrainSynthesizer
66
+
67
+ self._synthesizer = BrainSynthesizer(self._kg, queue)
68
+ return self._synthesizer
69
+
70
+ @staticmethod
71
+ def _no_queue(detail: str) -> Dict[str, Any]:
72
+ return {"available": False, "detail": detail, "generated_at": _now()}
73
+
74
+ def synthesize(
75
+ self, *, user_email: Optional[str] = None, workspace_id: Optional[str] = None
76
+ ) -> Dict[str, Any]:
77
+ """Run one synthesis pass; every finding becomes a review proposal."""
78
+ synthesizer = self._synthesis()
79
+ if synthesizer is None:
80
+ return self._no_queue(
81
+ "synthesis needs both the knowledge graph and the review queue"
82
+ )
83
+ try:
84
+ result = dict(
85
+ synthesizer.run(workspace_id=workspace_id, user_email=user_email)
86
+ )
87
+ except Exception as exc: # noqa: BLE001 — a failed pass is reported, not raised
88
+ LOGGER.exception("brain synthesis failed")
89
+ return {"available": False, "error": str(exc), "generated_at": _now()}
90
+ result["available"] = True
91
+ return result
92
+
93
+ def note_ingest(
94
+ self,
95
+ result: Any,
96
+ *,
97
+ user_email: Optional[str] = None,
98
+ workspace_id: Optional[str] = None,
99
+ ) -> Optional[Dict[str, Any]]:
100
+ """Ingest-driven trigger seam.
101
+
102
+ Hand every ingest result here; synthesis runs only once the configured
103
+ number of genuinely new nodes has accumulated. Returns the run result
104
+ when one fired, ``None`` otherwise — so an ingest path can call this
105
+ unconditionally and cheaply.
106
+ """
107
+ synthesizer = self._synthesis()
108
+ if synthesizer is None:
109
+ return None
110
+ try:
111
+ return synthesizer.run_if_due(
112
+ result, workspace_id=workspace_id, user_email=user_email
113
+ )
114
+ except Exception: # noqa: BLE001 — synthesis must never break an ingest
115
+ LOGGER.exception("ingest-triggered synthesis failed")
116
+ return None
117
+
118
+ def propose_contradictions(
119
+ self, *, user_email: Optional[str] = None, workspace_id: Optional[str] = None
120
+ ) -> Dict[str, Any]:
121
+ """Raise a review proposal for every contradicting pair of memories."""
122
+ queue = self._review_queue()
123
+ if not self._enable_graph or queue is None:
124
+ return self._no_queue(
125
+ "contradiction proposals need both the knowledge graph and the review queue"
126
+ )
127
+ from lattice_brain.synthesis import propose_contradictions
128
+
129
+ try:
130
+ return propose_contradictions(
131
+ self._kg, queue, workspace_id=workspace_id, user_email=user_email
132
+ )
133
+ except Exception as exc: # noqa: BLE001
134
+ LOGGER.exception("contradiction proposal pass failed")
135
+ return {"available": False, "error": str(exc), "generated_at": _now()}
136
+
137
+ def resolve_contradiction(
138
+ self,
139
+ item_id: str,
140
+ *,
141
+ resolution: str,
142
+ workspace_id: Optional[str] = None,
143
+ ) -> Dict[str, Any]:
144
+ """Approve a contradiction proposal and apply its temporal stamps.
145
+
146
+ Raises ``ContradictionResolutionError`` (a ``ValueError``) for an
147
+ unknown resolution or a review item that is not a contradiction — the
148
+ router turns those into 400s.
149
+ """
150
+ queue = self._review_queue()
151
+ if not self._enable_graph or queue is None:
152
+ return self._no_queue(
153
+ "resolving a contradiction needs both the knowledge graph and the review queue"
154
+ )
155
+ from lattice_brain.synthesis import resolve_contradiction
156
+
157
+ result = dict(
158
+ resolve_contradiction(
159
+ self._kg,
160
+ queue,
161
+ item_id,
162
+ resolution=resolution,
163
+ workspace_id=workspace_id,
164
+ )
165
+ )
166
+ result["available"] = True
167
+ return result
168
+
169
+ def importance_report(
170
+ self, *, user_email: Optional[str] = None, workspace_id: Optional[str] = None
171
+ ) -> Dict[str, Any]:
172
+ """Which memories are load-bearing, and which have decayed into noise."""
173
+ proactive = self._proactive()
174
+ if proactive is None:
175
+ return {"available": False, "candidates": [], "generated_at": _now()}
176
+ try:
177
+ report = dict(proactive.importance_report(workspace_id=workspace_id))
178
+ except Exception as exc: # noqa: BLE001
179
+ LOGGER.exception("importance report failed")
180
+ return {
181
+ "available": False,
182
+ "error": str(exc),
183
+ "candidates": [],
184
+ "generated_at": _now(),
185
+ }
186
+ report["available"] = True
187
+ return report
188
+
189
+ def proactive_brief(
190
+ self, *, user_email: Optional[str] = None, workspace_id: Optional[str] = None
191
+ ) -> Dict[str, Any]:
192
+ """The Brain Brief's proactive section: what the Brain noticed on its own.
193
+
194
+ Read-only. It counts the proposals already waiting in the Review Center
195
+ rather than raising new ones, so opening the home screen never mutates
196
+ anything — and reports honestly when there is no queue to read.
197
+ """
198
+ section: Dict[str, Any] = {
199
+ "available": False,
200
+ "pending": {"total": 0, "by_kind": {}},
201
+ "tidying": False,
202
+ "headline": "",
203
+ "lines": [],
204
+ "generated_at": _now(),
205
+ }
206
+ queue = self._review_queue()
207
+ if queue is None:
208
+ section["detail"] = "the review queue is not available on this deployment"
209
+ return section
210
+ pending = self._pending_synthesis_items(queue, workspace_id=workspace_id)
211
+ by_kind: Dict[str, int] = {}
212
+ for item in pending:
213
+ kind = str(item.get("kind") or "suggestion")
214
+ by_kind[kind] = by_kind.get(kind, 0) + 1
215
+ synthesizer = self._synthesis()
216
+ brief: Dict[str, Any] = {}
217
+ if synthesizer is not None:
218
+ try:
219
+ brief = dict(
220
+ synthesizer.brief_section(
221
+ counts=by_kind, workspace_id=workspace_id
222
+ )
223
+ )
224
+ except Exception: # noqa: BLE001 — the section degrades, never raises
225
+ LOGGER.exception("synthesis brief section failed")
226
+ section.update(
227
+ {
228
+ "available": True,
229
+ "pending": {"total": len(pending), "by_kind": by_kind},
230
+ "items": [
231
+ {
232
+ "id": item.get("id"),
233
+ "kind": item.get("kind"),
234
+ "title": item.get("title"),
235
+ "summary": item.get("summary"),
236
+ }
237
+ for item in pending[:5]
238
+ ],
239
+ "tidying": by_kind.get("consolidation", 0) > 0,
240
+ "headline": str(brief.get("headline") or ""),
241
+ "lines": list(brief.get("lines") or []),
242
+ "recent_nodes": brief.get("recent_nodes"),
243
+ }
244
+ )
245
+ return section
246
+
247
+ @staticmethod
248
+ def _pending_synthesis_items(
249
+ queue: Any, *, workspace_id: Optional[str]
250
+ ) -> List[Dict[str, Any]]:
251
+ from lattice_brain.synthesis import SYNTHESIS_REVIEW_SOURCE
252
+
253
+ try:
254
+ listing = queue.list(
255
+ workspace_id=workspace_id, source=SYNTHESIS_REVIEW_SOURCE
256
+ )
257
+ except Exception: # noqa: BLE001 — an unreadable inbox reads as empty
258
+ LOGGER.exception("review queue listing failed")
259
+ return []
260
+ return [
261
+ item
262
+ for item in listing.get("items") or []
263
+ if str(item.get("effective_status") or item.get("status")) == "pending"
264
+ ]
@@ -0,0 +1,84 @@
1
+ """How the service reaches the graph layer and the memory tier.
2
+
3
+ One workspace-scoped graph slice (:meth:`_graph_sample`) feeds the health
4
+ report, the insights digest, the garden and both consistency scans, so they all
5
+ measure the same knowledge. The slice normalizes the store's ``from``/``to``
6
+ edge keys to the ``source``/``target`` the quality layer expects — once, here,
7
+ instead of at four call sites.
8
+
9
+ Both readers degrade rather than raise: an unreadable graph reports
10
+ ``available: False`` and an unreadable memory tier reads as empty, because a
11
+ diagnosis that crashes is worse than one that says it could not measure.
12
+ """
13
+
14
+ from __future__ import annotations
15
+
16
+ from typing import Any, Dict, List, Optional
17
+
18
+ from ._contract import BrainIntelligenceCore as _Core
19
+ from .constants import _GRAPH_SAMPLE_LIMIT, LOGGER
20
+
21
+
22
+ class BrainSamplingMixin(_Core):
23
+ """The shared graph slice and memory read. Mixed into the service."""
24
+
25
+ def _proactive(self) -> Any:
26
+ """Lazy graph-layer ProactiveBrain over the injected store (or None)."""
27
+ if not self._enable_graph:
28
+ return None
29
+ if self._proactive_brain is None:
30
+ try:
31
+ from lattice_brain.graph.proactive import ProactiveBrain
32
+
33
+ self._proactive_brain = ProactiveBrain(
34
+ self._kg, sample_limit=_GRAPH_SAMPLE_LIMIT
35
+ )
36
+ except Exception:
37
+ LOGGER.exception("proactive brain initialization failed")
38
+ return None
39
+ return self._proactive_brain
40
+
41
+ # ── shared graph sampling ─────────────────────────────────────────────
42
+
43
+ def _graph_sample(self, *, workspace_id: Optional[str]) -> Dict[str, Any]:
44
+ """Recent graph slice with scoping applied. Empty when graph is off."""
45
+ if not self._enable_graph:
46
+ return {"nodes": [], "edges": [], "available": False}
47
+ try:
48
+ kwargs: Dict[str, Any] = {}
49
+ if workspace_id is not None:
50
+ kwargs["allowed_workspaces"] = {workspace_id}
51
+ data = self._kg.graph(_GRAPH_SAMPLE_LIMIT, **kwargs)
52
+ edges = []
53
+ for edge in data.get("edges") or []:
54
+ # Store emits "from"/"to"; the quality layer expects
55
+ # "source"/"target". Normalize once here.
56
+ normalized = dict(edge)
57
+ normalized.setdefault("source", edge.get("from"))
58
+ normalized.setdefault("target", edge.get("to"))
59
+ edges.append(normalized)
60
+ return {
61
+ "nodes": list(data.get("nodes") or []),
62
+ "edges": edges,
63
+ "available": True,
64
+ }
65
+ except Exception as exc:
66
+ LOGGER.exception("brain intelligence graph sample failed")
67
+ return {"nodes": [], "edges": [], "available": False, "error": str(exc)}
68
+
69
+ def _workspace_memories(
70
+ self, *, user_email: Optional[str], workspace_id: Optional[str]
71
+ ) -> List[Dict[str, Any]]:
72
+ if self._memory is None:
73
+ return []
74
+ try:
75
+ inspected = self._memory.inspect(
76
+ "workspace",
77
+ user_email=user_email,
78
+ workspace_id=workspace_id or "personal",
79
+ limit=500,
80
+ )
81
+ return list(inspected.get("items") or [])
82
+ except Exception:
83
+ LOGGER.exception("brain intelligence memory read failed")
84
+ return []