ltcai 11.1.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 (289) hide show
  1. package/README.md +47 -54
  2. package/docs/CHANGELOG.md +94 -0
  3. package/docs/COMMUNITY_AND_PLUGINS.md +1 -1
  4. package/docs/DEVELOPMENT.md +1 -1
  5. package/docs/FEATURE_AUDIT_v11.2.0.md +393 -0
  6. package/docs/LAYOUT_REBUILD_SPEC.md +9 -1
  7. package/docs/MULTI_AGENT_RUNTIME.md +1 -1
  8. package/docs/ONBOARDING.md +1 -1
  9. package/docs/OPERATIONS.md +6 -2
  10. package/docs/PERMISSION_MODE.md +1 -1
  11. package/docs/TRUST_MODEL.md +1 -1
  12. package/docs/WHY_LATTICE.md +1 -1
  13. package/docs/architecture.md +6 -2
  14. package/docs/kg-schema.md +2 -2
  15. package/docs/v11.3.0_PLAN.md +202 -0
  16. package/docs/v11.4.0_RUST_FOUNDATION_PLAN.md +176 -0
  17. package/lattice_brain/__init__.py +1 -1
  18. package/lattice_brain/gates.py +125 -0
  19. package/lattice_brain/graph/_kg_common/__init__.py +287 -0
  20. package/lattice_brain/graph/_kg_common/extraction.py +516 -0
  21. package/lattice_brain/graph/_kg_common/relations.py +161 -0
  22. package/lattice_brain/graph/_kg_common/text.py +479 -0
  23. package/lattice_brain/graph/discovery_index/__init__.py +35 -0
  24. package/lattice_brain/graph/discovery_index/cleanup.py +182 -0
  25. package/lattice_brain/graph/discovery_index/extract.py +137 -0
  26. package/lattice_brain/graph/discovery_index/scan.py +411 -0
  27. package/lattice_brain/graph/discovery_index/upsert.py +495 -0
  28. package/lattice_brain/graph/fusion.py +35 -4
  29. package/lattice_brain/graph/projection/__init__.py +42 -0
  30. package/lattice_brain/graph/projection/curation.py +500 -0
  31. package/lattice_brain/graph/{projection.py → projection/v2_schema.py} +81 -485
  32. package/lattice_brain/graph/retrieval/__init__.py +54 -0
  33. package/lattice_brain/graph/retrieval/context.py +197 -0
  34. package/lattice_brain/graph/retrieval/graph_view.py +319 -0
  35. package/lattice_brain/graph/retrieval/hybrid.py +488 -0
  36. package/lattice_brain/graph/retrieval/maintenance.py +121 -0
  37. package/lattice_brain/graph/retrieval/signals.py +95 -0
  38. package/lattice_brain/graph/retrieval_vector/__init__.py +42 -0
  39. package/lattice_brain/graph/retrieval_vector/fingerprint.py +97 -0
  40. package/lattice_brain/graph/retrieval_vector/indexing.py +347 -0
  41. package/lattice_brain/graph/retrieval_vector/search.py +560 -0
  42. package/lattice_brain/graph/retrieval_vector/status.py +374 -0
  43. package/lattice_brain/graph/schema.py +9 -0
  44. package/lattice_brain/graph/store.py +9 -0
  45. package/lattice_brain/graph/vector_index/selector.py +32 -2
  46. package/lattice_brain/ingestion/__init__.py +130 -0
  47. package/lattice_brain/ingestion/_contract.py +90 -0
  48. package/lattice_brain/ingestion/constants.py +127 -0
  49. package/lattice_brain/ingestion/folder_scan.py +57 -0
  50. package/lattice_brain/ingestion/folders.py +258 -0
  51. package/lattice_brain/ingestion/hashing.py +26 -0
  52. package/lattice_brain/ingestion/jobs_api.py +107 -0
  53. package/lattice_brain/ingestion/models.py +80 -0
  54. package/lattice_brain/ingestion/pipeline.py +486 -0
  55. package/lattice_brain/ingestion/quality.py +209 -0
  56. package/lattice_brain/ingestion/routing.py +295 -0
  57. package/lattice_brain/multimodal/__init__.py +164 -0
  58. package/lattice_brain/multimodal/audio.py +77 -0
  59. package/lattice_brain/multimodal/common.py +118 -0
  60. package/lattice_brain/{multimodal.py → multimodal/images.py} +22 -262
  61. package/lattice_brain/multimodal/ports.py +169 -0
  62. package/lattice_brain/multimodal/video.py +410 -0
  63. package/lattice_brain/portability/__init__.py +90 -0
  64. package/lattice_brain/portability/_contract.py +42 -0
  65. package/lattice_brain/portability/backups.py +338 -0
  66. package/lattice_brain/portability/bundles.py +136 -0
  67. package/lattice_brain/portability/constants.py +93 -0
  68. package/lattice_brain/portability/fsops.py +138 -0
  69. package/lattice_brain/portability/service.py +41 -0
  70. package/lattice_brain/portability/sharing.py +714 -0
  71. package/lattice_brain/runtime/__init__.py +1 -1
  72. package/lattice_brain/runtime/multi_agent.py +1 -1
  73. package/lattice_brain/sealed_box.py +244 -0
  74. package/lattice_brain/synthesis.py +24 -1
  75. package/latticeai/__init__.py +1 -1
  76. package/latticeai/api/brain_intelligence.py +4 -0
  77. package/latticeai/api/chat.py +11 -0
  78. package/latticeai/api/chat_helpers.py +16 -3
  79. package/latticeai/api/chat_hybrid.py +32 -1
  80. package/latticeai/api/chronicle.py +63 -0
  81. package/latticeai/api/features.py +70 -0
  82. package/latticeai/api/local_files.py +102 -0
  83. package/latticeai/api/portability.py +39 -4
  84. package/latticeai/api/review_queue.py +126 -0
  85. package/latticeai/api/search.py +16 -2
  86. package/latticeai/core/agent/__init__.py +93 -0
  87. package/latticeai/core/agent/_contract.py +79 -0
  88. package/latticeai/core/agent/context.py +57 -0
  89. package/latticeai/core/agent/deps.py +125 -0
  90. package/latticeai/core/agent/execution.py +622 -0
  91. package/latticeai/core/agent/planning.py +145 -0
  92. package/latticeai/core/agent/recovery.py +157 -0
  93. package/latticeai/core/agent/runtime.py +210 -0
  94. package/latticeai/core/agent/verification.py +231 -0
  95. package/latticeai/core/config.py +4 -1
  96. package/latticeai/core/context_builder.py +6 -3
  97. package/latticeai/core/embedding_providers/__init__.py +151 -0
  98. package/latticeai/core/embedding_providers/base.py +199 -0
  99. package/latticeai/core/embedding_providers/captions.py +162 -0
  100. package/latticeai/core/embedding_providers/profiles.py +126 -0
  101. package/latticeai/core/embedding_providers/text.py +350 -0
  102. package/latticeai/core/embedding_providers/vision.py +352 -0
  103. package/latticeai/core/file_generation/__init__.py +115 -0
  104. package/latticeai/core/file_generation/bundles.py +76 -0
  105. package/latticeai/core/file_generation/extraction.py +154 -0
  106. package/latticeai/core/file_generation/inference.py +235 -0
  107. package/latticeai/core/file_generation/orchestration.py +152 -0
  108. package/latticeai/core/file_generation/prompting.py +117 -0
  109. package/latticeai/core/file_generation/repair.py +114 -0
  110. package/latticeai/core/file_generation/sanitize.py +61 -0
  111. package/latticeai/core/file_generation/validation.py +201 -0
  112. package/latticeai/core/legacy_compatibility.py +1 -1
  113. package/latticeai/core/marketplace.py +1 -1
  114. package/latticeai/core/messages.py +152 -0
  115. package/latticeai/core/model_compat.py +73 -2
  116. package/latticeai/core/workspace_os_constants.py +1 -1
  117. package/latticeai/integrations/telegram_bot/__init__.py +123 -0
  118. package/latticeai/integrations/telegram_bot/__main__.py +17 -0
  119. package/latticeai/integrations/telegram_bot/config.py +86 -0
  120. package/latticeai/integrations/telegram_bot/dispatch.py +311 -0
  121. package/latticeai/integrations/telegram_bot/flows.py +478 -0
  122. package/latticeai/integrations/telegram_bot/helpers.py +322 -0
  123. package/latticeai/integrations/telegram_bot/screens.py +394 -0
  124. package/latticeai/models/model_providers.py +12 -4
  125. package/latticeai/models/router/__init__.py +88 -0
  126. package/latticeai/models/router/_contract.py +66 -0
  127. package/latticeai/models/router/branding.py +56 -0
  128. package/latticeai/models/router/catalog.py +69 -0
  129. package/latticeai/models/router/documents.py +199 -0
  130. package/latticeai/models/router/errors.py +37 -0
  131. package/latticeai/models/router/generation.py +258 -0
  132. package/latticeai/models/router/loading.py +291 -0
  133. package/latticeai/models/router/local_models.py +85 -0
  134. package/latticeai/models/router/registry.py +147 -0
  135. package/latticeai/runtime/build_phases/__init__.py +82 -0
  136. package/latticeai/runtime/build_phases/features.py +407 -0
  137. package/latticeai/runtime/build_phases/foundation.py +555 -0
  138. package/latticeai/runtime/build_phases/web.py +492 -0
  139. package/latticeai/runtime/chat_wiring.py +4 -0
  140. package/latticeai/runtime/feature_toggle_wiring.py +163 -0
  141. package/latticeai/runtime/router_registration.py +11 -0
  142. package/latticeai/runtime/runtime_context.py +1 -0
  143. package/latticeai/services/app_context.py +8 -0
  144. package/latticeai/services/architecture_readiness.py +48 -19
  145. package/latticeai/services/automation_intelligence.py +22 -2
  146. package/latticeai/services/brain_intelligence/__init__.py +58 -0
  147. package/latticeai/services/brain_intelligence/_contract.py +71 -0
  148. package/latticeai/services/brain_intelligence/consistency.py +193 -0
  149. package/latticeai/services/brain_intelligence/constants.py +47 -0
  150. package/latticeai/services/brain_intelligence/digest.py +258 -0
  151. package/latticeai/services/brain_intelligence/health.py +331 -0
  152. package/latticeai/services/brain_intelligence/proposals.py +264 -0
  153. package/latticeai/services/brain_intelligence/sampling.py +84 -0
  154. package/latticeai/services/brain_intelligence/service.py +48 -0
  155. package/latticeai/services/chronicle.py +557 -0
  156. package/latticeai/services/command_center.py +10 -4
  157. package/latticeai/services/feature_toggles.py +502 -0
  158. package/latticeai/services/folder_watch.py +122 -1
  159. package/latticeai/services/hybrid_chat.py +56 -5
  160. package/latticeai/services/interop_bridges.py +978 -0
  161. package/latticeai/services/memory_service/__init__.py +52 -0
  162. package/latticeai/services/memory_service/_contract.py +100 -0
  163. package/latticeai/services/memory_service/brief.py +431 -0
  164. package/latticeai/services/memory_service/constants.py +57 -0
  165. package/latticeai/services/memory_service/maintenance.py +138 -0
  166. package/latticeai/services/memory_service/manager.py +186 -0
  167. package/latticeai/services/memory_service/proof.py +136 -0
  168. package/latticeai/services/memory_service/recall.py +225 -0
  169. package/latticeai/services/memory_service/service.py +48 -0
  170. package/latticeai/services/memory_service/stores.py +110 -0
  171. package/latticeai/services/model_capability_registry.py +434 -261
  172. package/latticeai/services/model_catalog.py +95 -61
  173. package/latticeai/services/model_recommendation.py +18 -11
  174. package/latticeai/services/model_runtime/__init__.py +322 -0
  175. package/latticeai/services/model_runtime/cloud.py +87 -0
  176. package/latticeai/services/model_runtime/download.py +282 -0
  177. package/latticeai/services/model_runtime/engines.py +341 -0
  178. package/latticeai/services/model_runtime/loading.py +178 -0
  179. package/latticeai/services/model_runtime/service.py +129 -0
  180. package/latticeai/services/model_runtime/state.py +131 -0
  181. package/latticeai/services/model_runtime/status.py +255 -0
  182. package/latticeai/services/multimodal_ports.py +26 -1
  183. package/latticeai/services/obsidian_bridge.py +16 -25
  184. package/latticeai/services/product_readiness.py +15 -7
  185. package/latticeai/services/search_service.py +149 -2
  186. package/latticeai/services/tool_dispatch.py +4 -0
  187. package/latticeai/setup/auto_setup.py +27 -30
  188. package/latticeai/setup/wizard/__init__.py +126 -0
  189. package/latticeai/setup/wizard/catalog.py +172 -0
  190. package/latticeai/setup/wizard/detect.py +323 -0
  191. package/latticeai/setup/wizard/install.py +348 -0
  192. package/latticeai/setup/wizard/paths.py +168 -0
  193. package/latticeai/setup/wizard/plans.py +74 -0
  194. package/latticeai/setup/wizard/recommend.py +320 -0
  195. package/package.json +6 -2
  196. package/scripts/bump_version.py +14 -0
  197. package/scripts/capture_release_evidence.mjs +33 -21
  198. package/scripts/check_current_release_docs.mjs +1 -1
  199. package/scripts/check_i18n_namespace_coverage.mjs +41 -4
  200. package/scripts/check_max_file_lines.mjs +102 -0
  201. package/scripts/check_release_evidence_bound.mjs +30 -15
  202. package/scripts/check_screenshot_pixel_delta.py +34 -4
  203. package/scripts/check_server_i18n.mjs +2 -0
  204. package/scripts/generate_rust_parity_fixtures.py +562 -0
  205. package/scripts/lib/mock_server_fingerprint.mjs +94 -0
  206. package/scripts/release_screen_claims.json +44 -2
  207. package/scripts/verify_hf_model_registry.py +253 -218
  208. package/src-tauri/Cargo.lock +361 -3
  209. package/src-tauri/Cargo.toml +6 -1
  210. package/src-tauri/src/backend.rs +349 -0
  211. package/src-tauri/src/folder.rs +33 -0
  212. package/src-tauri/src/main.rs +97 -399
  213. package/src-tauri/tauri.conf.json +1 -1
  214. package/static/app/asset-manifest.json +41 -37
  215. package/static/app/assets/Act-yYpYnn0v.js +1 -0
  216. package/static/app/assets/AdminConsole-DL3Cr5pL.js +1 -0
  217. package/static/app/assets/{Brain-CzCsI1mi.js → Brain-C1HBN0Wf.js} +2 -2
  218. package/static/app/assets/BrainHome-DoXRhUUC.js +2 -0
  219. package/static/app/assets/BrainSignals-6yR6ir5t.js +1 -0
  220. package/static/app/assets/Capture-CFIRsFNE.js +1 -0
  221. package/static/app/assets/Chronicle-BZbEgiwN.js +1 -0
  222. package/static/app/assets/CommandPalette-D2pMxC2I.js +1 -0
  223. package/static/app/assets/Library-DwO3yZST.js +1 -0
  224. package/static/app/assets/{LivingBrain-BXMWIK_2.js → LivingBrain-Jn1GK0-S.js} +1 -1
  225. package/static/app/assets/ProductFlow-B-w1R4Oo.js +1 -0
  226. package/static/app/assets/ReviewCard-6B27X8Vg.js +3 -0
  227. package/static/app/assets/System-DW8F-2xL.js +1 -0
  228. package/static/app/assets/arrow-left-DXvKg9U6.js +1 -0
  229. package/static/app/assets/{bot-4BvN07ux.js → bot-IM_E_Y12.js} +1 -1
  230. package/static/app/assets/brain-Ci1CkWjM.js +1 -0
  231. package/static/app/assets/{button-CDjtnAoU.js → button-COwyqfHM.js} +1 -1
  232. package/static/app/assets/circle-check-DfInj-qD.js +1 -0
  233. package/static/app/assets/{circle-pause-D_RMn7tp.js → circle-pause-DEM4A1Y5.js} +1 -1
  234. package/static/app/assets/{circle-play-B5OpB8ae.js → circle-play-C9djDuLd.js} +1 -1
  235. package/static/app/assets/{cpu-BIlWInHf.js → cpu-DFdo1gw-.js} +1 -1
  236. package/static/app/assets/{download-BtjXfL3z.js → download-SnJL6oqk.js} +1 -1
  237. package/static/app/assets/{folder-open-DefMpxI2.js → folder-open-CqZeDkjE.js} +1 -1
  238. package/static/app/assets/{hard-drive-BQ8NZVkw.js → hard-drive-j1jJXYYf.js} +1 -1
  239. package/static/app/assets/{index-vtEfYvQY.css → index-BLPb5lmE.css} +1 -1
  240. package/static/app/assets/index-_u5iUHDr.js +10 -0
  241. package/static/app/assets/input-B0lPdRQZ.js +1 -0
  242. package/static/app/assets/link-2-CoFbooHS.js +1 -0
  243. package/static/app/assets/{permissionCopy-BqZ5tsgL.js → permissionCopy-BsyLxtao.js} +1 -1
  244. package/static/app/assets/primitives-DEbN-d6p.js +1 -0
  245. package/static/app/assets/search-BybIWPNd.js +1 -0
  246. package/static/app/assets/{share-2-D5zg_0fY.js → share-2-CVtZ_ewX.js} +1 -1
  247. package/static/app/assets/{shield-alert-B5pZzkUb.js → shield-alert-CBi2GNWM.js} +1 -1
  248. package/static/app/assets/{textarea-nEVIweKY.js → textarea-DNMpB5ih.js} +1 -1
  249. package/static/app/assets/{useFocusTrap-Cm99AHlz.js → useFocusTrap-C83t3GXF.js} +1 -1
  250. package/static/app/assets/useMutation-DtbJDoyz.js +1 -0
  251. package/static/app/assets/{useQuery-Dm__N6bL.js → useQuery-Dcp1OChy.js} +1 -1
  252. package/static/app/assets/utils-BlZr7Pd4.js +4 -0
  253. package/static/app/assets/workspace-jJY4RuAV.js +1 -0
  254. package/static/app/index.html +4 -4
  255. package/static/sw.js +1 -1
  256. package/lattice_brain/graph/_kg_common.py +0 -1331
  257. package/lattice_brain/graph/discovery_index.py +0 -1141
  258. package/lattice_brain/graph/retrieval.py +0 -1120
  259. package/lattice_brain/graph/retrieval_vector.py +0 -1293
  260. package/lattice_brain/ingestion.py +0 -1377
  261. package/lattice_brain/portability.py +0 -1210
  262. package/latticeai/core/agent.py +0 -1412
  263. package/latticeai/core/embedding_providers.py +0 -1196
  264. package/latticeai/core/file_generation.py +0 -1047
  265. package/latticeai/integrations/telegram_bot.py +0 -1390
  266. package/latticeai/models/router.py +0 -1007
  267. package/latticeai/runtime/build_phases.py +0 -1422
  268. package/latticeai/services/brain_intelligence.py +0 -967
  269. package/latticeai/services/memory_service.py +0 -1177
  270. package/latticeai/services/model_runtime.py +0 -1281
  271. package/latticeai/setup/wizard.py +0 -1277
  272. package/static/app/assets/Act-D0HWqtn0.js +0 -1
  273. package/static/app/assets/AdminConsole-D-QDW-A4.js +0 -1
  274. package/static/app/assets/BrainHome-Btns-_TA.js +0 -2
  275. package/static/app/assets/BrainSignals-2dHQNkns.js +0 -1
  276. package/static/app/assets/Capture-CT8v1StE.js +0 -1
  277. package/static/app/assets/CommandPalette-DoLXC2KH.js +0 -1
  278. package/static/app/assets/Library-DDoxFE5c.js +0 -1
  279. package/static/app/assets/ProductFlow-DOYf7JIs.js +0 -1
  280. package/static/app/assets/ReviewCard-COQsqidK.js +0 -3
  281. package/static/app/assets/System-BRllvYXd.js +0 -1
  282. package/static/app/assets/arrow-left-DnyMzss-.js +0 -1
  283. package/static/app/assets/brain-uMb_5hnO.js +0 -1
  284. package/static/app/assets/index-0AvoEBzJ.js +0 -10
  285. package/static/app/assets/input-B_5ZJ9oy.js +0 -1
  286. package/static/app/assets/primitives-CVwew78r.js +0 -1
  287. package/static/app/assets/search-DkhnOKZt.js +0 -1
  288. package/static/app/assets/utils-DcDMoZIe.js +0 -4
  289. package/static/app/assets/workspace-LtRRSKTf.js +0 -1
@@ -1,967 +0,0 @@
1
- """Proactive Brain Intelligence service (v9.3.0).
2
-
3
- The Brain graduates from a passive store to an active steward of its own
4
- knowledge. This service wires the previously dormant quality layer
5
- (:mod:`lattice_brain.quality` — dedupe, merge, conflict and temporal
6
- contradiction detection, retention) into router-facing capabilities:
7
-
8
- * **health_report** — scored diagnosis of the Brain across freshness,
9
- connectivity, embedding coverage, and contradiction pressure, with
10
- recommended next actions. Every number is read from the live stores;
11
- a missing store degrades the dimension to ``unavailable``, never a guess.
12
- * **insights** — a proactive digest: recent knowledge growth, most active
13
- types, stale knowledge, orphan (disconnected) nodes, and suggested
14
- questions grounded in real node titles.
15
- * **contradictions** — surfaced conflicts across workspace memories
16
- (negation/preference conflicts, temporal contradictions) plus explicit
17
- CONTRADICTS edges already recorded in the graph.
18
- * **consolidate** — duplicate-memory and duplicate-edge detection. Dry-run
19
- by default (consent-first, like every Brain automation); ``apply=True``
20
- prunes only exact duplicate workspace memories through the audited
21
- MemoryService path and never touches graph content.
22
-
23
- Pure service: no FastAPI, no globals. Collaborators are injected.
24
- """
25
-
26
- from __future__ import annotations
27
-
28
- import logging
29
- from datetime import datetime, timedelta, timezone
30
- from typing import Any, Dict, List, Optional
31
-
32
- from lattice_brain.quality import GraphEdgeQualityManager, MemoryQualityManager
33
- from latticeai.core.timeutil import now_iso as _now
34
-
35
- LOGGER = logging.getLogger(__name__)
36
-
37
- _STALE_DAYS = 45
38
- _RECENT_DAYS = 7
39
- _GRAPH_SAMPLE_LIMIT = 800
40
-
41
-
42
- def _parse_ts(value: Any) -> Optional[datetime]:
43
- text = str(value or "").strip()
44
- if not text:
45
- return None
46
- try:
47
- parsed = datetime.fromisoformat(text.replace("Z", "+00:00"))
48
- except ValueError:
49
- return None
50
- if parsed.tzinfo is None:
51
- parsed = parsed.replace(tzinfo=timezone.utc)
52
- return parsed
53
-
54
-
55
- class BrainIntelligenceService:
56
- def __init__(
57
- self,
58
- *,
59
- knowledge_graph: Any = None,
60
- memory_service: Any = None,
61
- enable_graph: bool = True,
62
- review_queue: Any = None,
63
- ) -> None:
64
- self._kg = knowledge_graph
65
- self._memory = memory_service
66
- self._enable_graph = bool(enable_graph and knowledge_graph is not None)
67
- self._memory_quality = MemoryQualityManager()
68
- self._edge_quality = GraphEdgeQualityManager()
69
- self._proactive_brain: Any = None
70
- self._review_queue_service = review_queue
71
- self._synthesizer: Any = None
72
-
73
- # ── proposal path (v11.1.0) ───────────────────────────────────────────
74
- #
75
- # Synthesis never writes: it proposes, and the Review Center decides. That
76
- # makes the review queue a hard dependency of every method below — when it
77
- # is absent they report ``available: False`` rather than falling back to a
78
- # direct write.
79
-
80
- def attach_review_queue(self, review_queue: Any) -> None:
81
- """Bind the review queue proposals are written to (composition root)."""
82
- self._review_queue_service = review_queue
83
- self._synthesizer = None
84
-
85
- def _review_queue(self) -> Any:
86
- """The queue to propose into, or ``None`` when there is none.
87
-
88
- Prefers an explicitly injected service. Otherwise it builds one over
89
- the workspace store the memory service already holds — the same
90
- ``WorkspaceOSStore`` every other ``ReviewQueueService`` in the process
91
- is constructed over, so proposals land in the one inbox the user reads
92
- even before the composition root injects the service directly.
93
- """
94
- if self._review_queue_service is not None:
95
- return self._review_queue_service
96
- store = getattr(self._memory, "_store", None)
97
- if store is None or not hasattr(store, "create_review_item"):
98
- return None
99
- from latticeai.services.review_queue import ReviewQueueService
100
-
101
- self._review_queue_service = ReviewQueueService(store=store)
102
- return self._review_queue_service
103
-
104
- def _synthesis(self) -> Any:
105
- """Lazy :class:`BrainSynthesizer` over the graph + review queue."""
106
- if self._synthesizer is not None:
107
- return self._synthesizer
108
- queue = self._review_queue()
109
- if not self._enable_graph or queue is None:
110
- return None
111
- from lattice_brain.synthesis import BrainSynthesizer
112
-
113
- self._synthesizer = BrainSynthesizer(self._kg, queue)
114
- return self._synthesizer
115
-
116
- @staticmethod
117
- def _no_queue(detail: str) -> Dict[str, Any]:
118
- return {"available": False, "detail": detail, "generated_at": _now()}
119
-
120
- def synthesize(
121
- self, *, user_email: Optional[str] = None, workspace_id: Optional[str] = None
122
- ) -> Dict[str, Any]:
123
- """Run one synthesis pass; every finding becomes a review proposal."""
124
- synthesizer = self._synthesis()
125
- if synthesizer is None:
126
- return self._no_queue(
127
- "synthesis needs both the knowledge graph and the review queue"
128
- )
129
- try:
130
- result = dict(
131
- synthesizer.run(workspace_id=workspace_id, user_email=user_email)
132
- )
133
- except Exception as exc: # noqa: BLE001 — a failed pass is reported, not raised
134
- LOGGER.exception("brain synthesis failed")
135
- return {"available": False, "error": str(exc), "generated_at": _now()}
136
- result["available"] = True
137
- return result
138
-
139
- def note_ingest(
140
- self,
141
- result: Any,
142
- *,
143
- user_email: Optional[str] = None,
144
- workspace_id: Optional[str] = None,
145
- ) -> Optional[Dict[str, Any]]:
146
- """Ingest-driven trigger seam.
147
-
148
- Hand every ingest result here; synthesis runs only once the configured
149
- number of genuinely new nodes has accumulated. Returns the run result
150
- when one fired, ``None`` otherwise — so an ingest path can call this
151
- unconditionally and cheaply.
152
- """
153
- synthesizer = self._synthesis()
154
- if synthesizer is None:
155
- return None
156
- try:
157
- return synthesizer.run_if_due(
158
- result, workspace_id=workspace_id, user_email=user_email
159
- )
160
- except Exception: # noqa: BLE001 — synthesis must never break an ingest
161
- LOGGER.exception("ingest-triggered synthesis failed")
162
- return None
163
-
164
- def propose_contradictions(
165
- self, *, user_email: Optional[str] = None, workspace_id: Optional[str] = None
166
- ) -> Dict[str, Any]:
167
- """Raise a review proposal for every contradicting pair of memories."""
168
- queue = self._review_queue()
169
- if not self._enable_graph or queue is None:
170
- return self._no_queue(
171
- "contradiction proposals need both the knowledge graph and the review queue"
172
- )
173
- from lattice_brain.synthesis import propose_contradictions
174
-
175
- try:
176
- return propose_contradictions(
177
- self._kg, queue, workspace_id=workspace_id, user_email=user_email
178
- )
179
- except Exception as exc: # noqa: BLE001
180
- LOGGER.exception("contradiction proposal pass failed")
181
- return {"available": False, "error": str(exc), "generated_at": _now()}
182
-
183
- def resolve_contradiction(
184
- self,
185
- item_id: str,
186
- *,
187
- resolution: str,
188
- workspace_id: Optional[str] = None,
189
- ) -> Dict[str, Any]:
190
- """Approve a contradiction proposal and apply its temporal stamps.
191
-
192
- Raises ``ContradictionResolutionError`` (a ``ValueError``) for an
193
- unknown resolution or a review item that is not a contradiction — the
194
- router turns those into 400s.
195
- """
196
- queue = self._review_queue()
197
- if not self._enable_graph or queue is None:
198
- return self._no_queue(
199
- "resolving a contradiction needs both the knowledge graph and the review queue"
200
- )
201
- from lattice_brain.synthesis import resolve_contradiction
202
-
203
- result = dict(
204
- resolve_contradiction(
205
- self._kg,
206
- queue,
207
- item_id,
208
- resolution=resolution,
209
- workspace_id=workspace_id,
210
- )
211
- )
212
- result["available"] = True
213
- return result
214
-
215
- def importance_report(
216
- self, *, user_email: Optional[str] = None, workspace_id: Optional[str] = None
217
- ) -> Dict[str, Any]:
218
- """Which memories are load-bearing, and which have decayed into noise."""
219
- proactive = self._proactive()
220
- if proactive is None:
221
- return {"available": False, "candidates": [], "generated_at": _now()}
222
- try:
223
- report = dict(proactive.importance_report(workspace_id=workspace_id))
224
- except Exception as exc: # noqa: BLE001
225
- LOGGER.exception("importance report failed")
226
- return {
227
- "available": False,
228
- "error": str(exc),
229
- "candidates": [],
230
- "generated_at": _now(),
231
- }
232
- report["available"] = True
233
- return report
234
-
235
- def proactive_brief(
236
- self, *, user_email: Optional[str] = None, workspace_id: Optional[str] = None
237
- ) -> Dict[str, Any]:
238
- """The Brain Brief's proactive section: what the Brain noticed on its own.
239
-
240
- Read-only. It counts the proposals already waiting in the Review Center
241
- rather than raising new ones, so opening the home screen never mutates
242
- anything — and reports honestly when there is no queue to read.
243
- """
244
- section: Dict[str, Any] = {
245
- "available": False,
246
- "pending": {"total": 0, "by_kind": {}},
247
- "tidying": False,
248
- "headline": "",
249
- "lines": [],
250
- "generated_at": _now(),
251
- }
252
- queue = self._review_queue()
253
- if queue is None:
254
- section["detail"] = "the review queue is not available on this deployment"
255
- return section
256
- pending = self._pending_synthesis_items(queue, workspace_id=workspace_id)
257
- by_kind: Dict[str, int] = {}
258
- for item in pending:
259
- kind = str(item.get("kind") or "suggestion")
260
- by_kind[kind] = by_kind.get(kind, 0) + 1
261
- synthesizer = self._synthesis()
262
- brief: Dict[str, Any] = {}
263
- if synthesizer is not None:
264
- try:
265
- brief = dict(
266
- synthesizer.brief_section(
267
- counts=by_kind, workspace_id=workspace_id
268
- )
269
- )
270
- except Exception: # noqa: BLE001 — the section degrades, never raises
271
- LOGGER.exception("synthesis brief section failed")
272
- section.update(
273
- {
274
- "available": True,
275
- "pending": {"total": len(pending), "by_kind": by_kind},
276
- "items": [
277
- {
278
- "id": item.get("id"),
279
- "kind": item.get("kind"),
280
- "title": item.get("title"),
281
- "summary": item.get("summary"),
282
- }
283
- for item in pending[:5]
284
- ],
285
- "tidying": by_kind.get("consolidation", 0) > 0,
286
- "headline": str(brief.get("headline") or ""),
287
- "lines": list(brief.get("lines") or []),
288
- "recent_nodes": brief.get("recent_nodes"),
289
- }
290
- )
291
- return section
292
-
293
- @staticmethod
294
- def _pending_synthesis_items(
295
- queue: Any, *, workspace_id: Optional[str]
296
- ) -> List[Dict[str, Any]]:
297
- from lattice_brain.synthesis import SYNTHESIS_REVIEW_SOURCE
298
-
299
- try:
300
- listing = queue.list(
301
- workspace_id=workspace_id, source=SYNTHESIS_REVIEW_SOURCE
302
- )
303
- except Exception: # noqa: BLE001 — an unreadable inbox reads as empty
304
- LOGGER.exception("review queue listing failed")
305
- return []
306
- return [
307
- item
308
- for item in listing.get("items") or []
309
- if str(item.get("effective_status") or item.get("status")) == "pending"
310
- ]
311
-
312
- def _proactive(self) -> Any:
313
- """Lazy graph-layer ProactiveBrain over the injected store (or None)."""
314
- if not self._enable_graph:
315
- return None
316
- if self._proactive_brain is None:
317
- try:
318
- from lattice_brain.graph.proactive import ProactiveBrain
319
-
320
- self._proactive_brain = ProactiveBrain(
321
- self._kg, sample_limit=_GRAPH_SAMPLE_LIMIT
322
- )
323
- except Exception:
324
- LOGGER.exception("proactive brain initialization failed")
325
- return None
326
- return self._proactive_brain
327
-
328
- # ── shared graph sampling ─────────────────────────────────────────────
329
-
330
- def _graph_sample(self, *, workspace_id: Optional[str]) -> Dict[str, Any]:
331
- """Recent graph slice with scoping applied. Empty when graph is off."""
332
- if not self._enable_graph:
333
- return {"nodes": [], "edges": [], "available": False}
334
- try:
335
- kwargs: Dict[str, Any] = {}
336
- if workspace_id is not None:
337
- kwargs["allowed_workspaces"] = {workspace_id}
338
- data = self._kg.graph(_GRAPH_SAMPLE_LIMIT, **kwargs)
339
- edges = []
340
- for edge in data.get("edges") or []:
341
- # Store emits "from"/"to"; the quality layer expects
342
- # "source"/"target". Normalize once here.
343
- normalized = dict(edge)
344
- normalized.setdefault("source", edge.get("from"))
345
- normalized.setdefault("target", edge.get("to"))
346
- edges.append(normalized)
347
- return {
348
- "nodes": list(data.get("nodes") or []),
349
- "edges": edges,
350
- "available": True,
351
- }
352
- except Exception as exc:
353
- LOGGER.exception("brain intelligence graph sample failed")
354
- return {"nodes": [], "edges": [], "available": False, "error": str(exc)}
355
-
356
- def _workspace_memories(
357
- self, *, user_email: Optional[str], workspace_id: Optional[str]
358
- ) -> List[Dict[str, Any]]:
359
- if self._memory is None:
360
- return []
361
- try:
362
- inspected = self._memory.inspect(
363
- "workspace",
364
- user_email=user_email,
365
- workspace_id=workspace_id or "personal",
366
- limit=500,
367
- )
368
- return list(inspected.get("items") or [])
369
- except Exception:
370
- LOGGER.exception("brain intelligence memory read failed")
371
- return []
372
-
373
- # ── health report ─────────────────────────────────────────────────────
374
-
375
- def health_report(
376
- self, *, user_email: Optional[str] = None, workspace_id: Optional[str] = None
377
- ) -> Dict[str, Any]:
378
- sample = self._graph_sample(workspace_id=workspace_id)
379
- nodes, edges = sample["nodes"], sample["edges"]
380
- now = datetime.now(timezone.utc)
381
- dimensions: Dict[str, Dict[str, Any]] = {}
382
-
383
- # Freshness — how much of the sampled knowledge saw recent updates.
384
- if sample["available"] and nodes:
385
- stale_cutoff = now - timedelta(days=_STALE_DAYS)
386
- dated = [(_parse_ts(n.get("updated_at")), n) for n in nodes]
387
- known = [pair for pair in dated if pair[0] is not None]
388
- stale = [n for ts, n in known if ts is not None and ts < stale_cutoff]
389
- fresh_ratio = 1.0 - (len(stale) / len(known)) if known else 0.0
390
- dimensions["freshness"] = {
391
- "status": "ok",
392
- "score": round(fresh_ratio * 100),
393
- "sampled": len(nodes),
394
- "stale_nodes": len(stale),
395
- "stale_threshold_days": _STALE_DAYS,
396
- }
397
- else:
398
- dimensions["freshness"] = {"status": "unavailable", "score": None}
399
-
400
- # Connectivity — orphan nodes are knowledge the Brain cannot reason
401
- # across; a well-tended graph keeps them rare.
402
- if sample["available"] and nodes:
403
- connected = set()
404
- for edge in edges:
405
- connected.add(str(edge.get("source") or edge.get("from_node") or ""))
406
- connected.add(str(edge.get("target") or edge.get("to_node") or ""))
407
- orphans = [n for n in nodes if str(n.get("id")) not in connected]
408
- ratio = 1.0 - (len(orphans) / len(nodes))
409
- dimensions["connectivity"] = {
410
- "status": "ok",
411
- "score": round(ratio * 100),
412
- "sampled": len(nodes),
413
- "orphan_nodes": len(orphans),
414
- "edges": len(edges),
415
- }
416
- else:
417
- dimensions["connectivity"] = {"status": "unavailable", "score": None}
418
-
419
- # Embedding coverage — semantic recall only works for indexed items.
420
- index_status: Dict[str, Any] = {}
421
- if self._enable_graph and hasattr(self._kg, "index_status"):
422
- try:
423
- index_status = self._kg.index_status()
424
- except Exception as exc:
425
- LOGGER.exception("brain intelligence index status failed")
426
- index_status = {"error": str(exc)}
427
- scale = index_status.get("scale") or {}
428
- if "coverage_ratio" in scale:
429
- dimensions["embedding_coverage"] = {
430
- "status": "ok",
431
- "score": round(float(scale["coverage_ratio"]) * 100),
432
- "ready_items": scale.get("ready_items"),
433
- "pending_items": scale.get("pending_items"),
434
- "needs_reindex": index_status.get("status") == "needs_reindex",
435
- }
436
- else:
437
- dimensions["embedding_coverage"] = {"status": "unavailable", "score": None}
438
-
439
- # Edge quality + contradiction pressure — reuses the quality layer.
440
- if sample["available"] and edges:
441
- metrics = self._edge_quality.compute_quality_metrics(edges)
442
- contradiction_edges = [
443
- e for e in edges if "CONTRADICT" in str(e.get("type") or "").upper()
444
- ]
445
- pressure = min(1.0, metrics.get("dup_rate", 0.0) + len(contradiction_edges) / max(len(edges), 1))
446
- dimensions["consistency"] = {
447
- "status": "ok",
448
- "score": round((1.0 - pressure) * 100),
449
- "edge_metrics": metrics,
450
- "contradiction_edges": len(contradiction_edges),
451
- }
452
- else:
453
- dimensions["consistency"] = {"status": "unavailable", "score": None}
454
-
455
- scores = [d["score"] for d in dimensions.values() if d.get("score") is not None]
456
- overall = round(sum(scores) / len(scores)) if scores else None
457
- grade = (
458
- None if overall is None
459
- else "excellent" if overall >= 85
460
- else "good" if overall >= 70
461
- else "attention" if overall >= 50
462
- else "critical"
463
- )
464
-
465
- actions: List[Dict[str, str]] = []
466
- emb = dimensions["embedding_coverage"]
467
- if emb.get("needs_reindex"):
468
- actions.append({
469
- "id": "rebuild_vector_index",
470
- "reason": f"{emb.get('pending_items', 0)} items are missing or stale in the vector index.",
471
- })
472
- conn_dim = dimensions["connectivity"]
473
- conn_score = conn_dim.get("score")
474
- if conn_score is not None and conn_score < 70:
475
- actions.append({
476
- "id": "review_orphans",
477
- "reason": f"{conn_dim.get('orphan_nodes', 0)} nodes have no relationships.",
478
- })
479
- fresh_dim = dimensions["freshness"]
480
- fresh_score = fresh_dim.get("score")
481
- if fresh_score is not None and fresh_score < 60:
482
- actions.append({
483
- "id": "refresh_stale_knowledge",
484
- "reason": f"{fresh_dim.get('stale_nodes', 0)} nodes untouched for over {_STALE_DAYS} days.",
485
- })
486
- cons_dim = dimensions["consistency"]
487
- if cons_dim.get("contradiction_edges"):
488
- actions.append({
489
- "id": "resolve_contradictions",
490
- "reason": f"{cons_dim['contradiction_edges']} contradiction edges recorded in the graph.",
491
- })
492
-
493
- return {
494
- "overall_score": overall,
495
- "grade": grade,
496
- "dimensions": dimensions,
497
- "recommended_actions": actions,
498
- "graph_available": sample["available"],
499
- "generated_at": _now(),
500
- }
501
-
502
- # ── vector freshness (v9.8.0) ────────────────────────────────────────
503
-
504
- def vector_freshness(
505
- self, *, user_email: Optional[str] = None, workspace_id: Optional[str] = None
506
- ) -> Dict[str, Any]:
507
- """Fixed-contract vector index freshness for ``/api/brain/vector-freshness``.
508
-
509
- Always returns ``{"status": "ready"|"pending"|"unavailable",
510
- "pending_items": int, "total_items": int, "detail": str}`` and never
511
- raises. The vector index is store-global (not workspace-partitioned);
512
- scope arguments are accepted for router symmetry but do not narrow
513
- the report.
514
- """
515
-
516
- def _unavailable(detail: str) -> Dict[str, Any]:
517
- return {
518
- "status": "unavailable",
519
- "pending_items": 0,
520
- "total_items": 0,
521
- "detail": detail,
522
- }
523
-
524
- if not self._enable_graph or self._kg is None:
525
- return _unavailable("knowledge graph is disabled; no vector index is configured")
526
-
527
- freshness_fn = getattr(self._kg, "vector_freshness", None)
528
- if callable(freshness_fn):
529
- try:
530
- raw = freshness_fn() or {}
531
- except Exception as exc:
532
- LOGGER.exception("vector freshness read failed")
533
- return _unavailable(f"vector freshness read failed: {exc}")
534
- status = str(raw.get("status") or "unavailable")
535
- if status == "needs_reindex":
536
- status = "pending"
537
- if status not in {"ready", "pending", "unavailable"}:
538
- status = "unavailable"
539
- return {
540
- "status": status,
541
- "pending_items": int(raw.get("pending_items") or 0),
542
- "total_items": int(raw.get("total_items") or 0),
543
- "detail": str(raw.get("detail") or ""),
544
- }
545
-
546
- # Older/lighter stores: summarize index_status directly.
547
- status_fn = getattr(self._kg, "index_status", None)
548
- if callable(status_fn):
549
- try:
550
- raw = status_fn() or {}
551
- except Exception as exc:
552
- LOGGER.exception("vector index status read failed")
553
- return _unavailable(f"vector index status unavailable: {exc}")
554
- pending = int(raw.get("pending_items") or 0)
555
- total = int(raw.get("source_items") or 0)
556
- if pending > 0:
557
- return {
558
- "status": "pending",
559
- "pending_items": pending,
560
- "total_items": total,
561
- "detail": (
562
- f"{pending} of {total} items are missing or stale in the vector index"
563
- ),
564
- }
565
- return {
566
- "status": "ready",
567
- "pending_items": 0,
568
- "total_items": total,
569
- "detail": "vector index is up to date",
570
- }
571
-
572
- return _unavailable("this knowledge store does not expose a vector index")
573
-
574
- # ── insights digest ──────────────────────────────────────────────────
575
-
576
- def insights(
577
- self, *, user_email: Optional[str] = None, workspace_id: Optional[str] = None
578
- ) -> Dict[str, Any]:
579
- sample = self._graph_sample(workspace_id=workspace_id)
580
- nodes, edges = sample["nodes"], sample["edges"]
581
- now = datetime.now(timezone.utc)
582
- recent_cutoff = now - timedelta(days=_RECENT_DAYS)
583
- stale_cutoff = now - timedelta(days=_STALE_DAYS)
584
-
585
- recent_nodes: List[Dict[str, Any]] = []
586
- stale_nodes: List[Dict[str, Any]] = []
587
- type_counts: Dict[str, int] = {}
588
- for node in nodes:
589
- node_type = str(node.get("type") or "node")
590
- ts = _parse_ts(node.get("updated_at"))
591
- if ts is not None and ts >= recent_cutoff:
592
- recent_nodes.append(node)
593
- type_counts[node_type] = type_counts.get(node_type, 0) + 1
594
- elif ts is not None and ts < stale_cutoff:
595
- stale_nodes.append(node)
596
-
597
- connected = set()
598
- for edge in edges:
599
- connected.add(str(edge.get("source") or edge.get("from_node") or ""))
600
- connected.add(str(edge.get("target") or edge.get("to_node") or ""))
601
- orphans = [n for n in nodes if str(n.get("id")) not in connected]
602
-
603
- def _slim(node: Dict[str, Any]) -> Dict[str, Any]:
604
- return {
605
- "id": node.get("id"),
606
- "type": node.get("type"),
607
- "title": str(node.get("title") or "")[:120],
608
- "updated_at": node.get("updated_at"),
609
- }
610
-
611
- trending = sorted(type_counts.items(), key=lambda kv: kv[1], reverse=True)[:5]
612
- suggested_questions = [
613
- f"{str(node.get('title') or '').strip()[:60]}에 대해 지금까지 알고 있는 것을 정리해줘"
614
- for node in recent_nodes[:3]
615
- if str(node.get("title") or "").strip()
616
- ]
617
-
618
- return {
619
- "window_days": _RECENT_DAYS,
620
- "activity": {
621
- "recent_nodes": len(recent_nodes),
622
- "recent_samples": [_slim(n) for n in recent_nodes[:8]],
623
- "trending_types": [{"type": t, "count": c} for t, c in trending],
624
- },
625
- "attention": {
626
- "stale_nodes": len(stale_nodes),
627
- "stale_samples": [_slim(n) for n in stale_nodes[:8]],
628
- "orphan_nodes": len(orphans),
629
- "orphan_samples": [_slim(n) for n in orphans[:8]],
630
- },
631
- "suggested_questions": suggested_questions,
632
- "graph_available": sample["available"],
633
- "generated_at": _now(),
634
- }
635
-
636
- def garden_overview(
637
- self, *, user_email: Optional[str] = None, workspace_id: Optional[str] = None,
638
- limit: int = 8,
639
- ) -> Dict[str, Any]:
640
- """The knowledge garden in four beds (v9.9.7).
641
-
642
- Living Brain answers "how healthy is my knowledge?" in aggregate.
643
- A gardener asks four concrete questions instead, and this answers all
644
- four from one workspace-scoped graph sample plus the memory tier:
645
-
646
- * **recent** — what came in lately (the garden's new growth);
647
- * **contradictions** — what disagrees with itself (needs weeding);
648
- * **stale** — what has not been touched in a long time;
649
- * **frequent** — what the rest of the graph leans on most (by degree).
650
-
651
- Read-only and honest: when the graph is unavailable every bed is empty
652
- and ``available`` is false — the view never invents plants.
653
- """
654
- # Explicit 0 clamps to 1 — `limit or 8` would silently re-expand it.
655
- try:
656
- limit = max(1, min(int(limit), 50))
657
- except (TypeError, ValueError):
658
- limit = 8
659
- sample = self._graph_sample(workspace_id=workspace_id)
660
- nodes, edges = sample["nodes"], sample["edges"]
661
- now = datetime.now(timezone.utc)
662
- recent_cutoff = now - timedelta(days=_RECENT_DAYS)
663
- stale_cutoff = now - timedelta(days=_STALE_DAYS)
664
-
665
- def _slim(node: Dict[str, Any], **extra: Any) -> Dict[str, Any]:
666
- return {
667
- "id": node.get("id"),
668
- "type": node.get("type"),
669
- "title": str(node.get("title") or "")[:120],
670
- "updated_at": node.get("updated_at"),
671
- **extra,
672
- }
673
-
674
- recent: List[Dict[str, Any]] = []
675
- stale: List[Dict[str, Any]] = []
676
- for node in nodes:
677
- # Chunks are retrieval plumbing, not knowledge a gardener tends.
678
- if str(node.get("type") or "") == "Chunk":
679
- continue
680
- ts = _parse_ts(node.get("updated_at"))
681
- if ts is None:
682
- continue
683
- if ts >= recent_cutoff:
684
- recent.append(node)
685
- elif ts < stale_cutoff:
686
- stale.append(node)
687
- recent.sort(key=lambda n: str(n.get("updated_at") or ""), reverse=True)
688
- stale.sort(key=lambda n: str(n.get("updated_at") or ""))
689
-
690
- # "Frequent" is degree, not a guess: how many relations actually point
691
- # at a node. Chunks are retrieval plumbing, never garden plants.
692
- degree: Dict[str, int] = {}
693
- for edge in edges:
694
- for key in ("source", "target"):
695
- node_id = str(edge.get(key) or "")
696
- if node_id:
697
- degree[node_id] = degree.get(node_id, 0) + 1
698
- by_id = {str(node.get("id")): node for node in nodes}
699
- frequent = [
700
- _slim(by_id[node_id], degree=count)
701
- for node_id, count in sorted(degree.items(), key=lambda kv: kv[1], reverse=True)
702
- if node_id in by_id and str(by_id[node_id].get("type") or "") != "Chunk"
703
- ][:limit]
704
-
705
- contradiction_items: List[Dict[str, Any]] = []
706
- contradiction_count = 0
707
- try:
708
- found = self.contradictions(user_email=user_email, workspace_id=workspace_id)
709
- items = found.get("items") if isinstance(found, dict) else None
710
- if isinstance(items, list):
711
- contradiction_count = len(items)
712
- contradiction_items = items[:limit]
713
- except Exception: # noqa: BLE001 — one empty bed, never a broken view
714
- LOGGER.exception("garden overview contradictions failed")
715
-
716
- return {
717
- "available": sample["available"],
718
- "window_days": _RECENT_DAYS,
719
- "stale_threshold_days": _STALE_DAYS,
720
- "beds": {
721
- "recent": {
722
- "count": len(recent),
723
- "items": [_slim(node) for node in recent[:limit]],
724
- },
725
- "contradictions": {
726
- "count": contradiction_count,
727
- "items": contradiction_items,
728
- },
729
- "stale": {
730
- "count": len(stale),
731
- "items": [_slim(node) for node in stale[:limit]],
732
- },
733
- "frequent": {
734
- "count": len(frequent),
735
- "items": frequent,
736
- },
737
- },
738
- "generated_at": _now(),
739
- }
740
-
741
- # ── graph-layer proactive quality (v9.6.x) ───────────────────────────
742
-
743
- def graph_duplicates(
744
- self, *, user_email: Optional[str] = None, workspace_id: Optional[str] = None
745
- ) -> Dict[str, Any]:
746
- """Duplicate graph nodes (exact groups + near pairs) — read only."""
747
- proactive = self._proactive()
748
- if proactive is None:
749
- return {
750
- "available": False,
751
- "exact_groups": [],
752
- "near_pairs": [],
753
- "exact_duplicate_nodes": 0,
754
- "nodes_scanned": 0,
755
- "generated_at": _now(),
756
- }
757
- try:
758
- result = dict(proactive.find_duplicates(workspace_id=workspace_id))
759
- except Exception as exc:
760
- LOGGER.exception("graph duplicates scan failed")
761
- return {
762
- "available": False,
763
- "error": str(exc),
764
- "exact_groups": [],
765
- "near_pairs": [],
766
- "exact_duplicate_nodes": 0,
767
- "nodes_scanned": 0,
768
- "generated_at": _now(),
769
- }
770
- result["available"] = True
771
- result["generated_at"] = _now()
772
- return result
773
-
774
- def quality_report(
775
- self, *, user_email: Optional[str] = None, workspace_id: Optional[str] = None
776
- ) -> Dict[str, Any]:
777
- """Combined graph quality report: duplicates, contradictions, stale
778
- nodes, edge quality — one workspace-scoped graph sample."""
779
- proactive = self._proactive()
780
- if proactive is None:
781
- return {"available": False, "generated_at": _now()}
782
- try:
783
- result = dict(proactive.quality_report(workspace_id=workspace_id))
784
- except Exception as exc:
785
- LOGGER.exception("graph quality report failed")
786
- return {"available": False, "error": str(exc), "generated_at": _now()}
787
- result["available"] = True
788
- # v11.1.0: decay is part of quality, and "the Brain is tidying up" is a
789
- # state the user is entitled to see rather than a background surprise.
790
- importance = self.importance_report(
791
- user_email=user_email, workspace_id=workspace_id
792
- )
793
- candidates = len(importance.get("candidates") or [])
794
- result["importance"] = importance
795
- result["tidying"] = bool(importance.get("available")) and candidates > 0
796
- summary = dict(result.get("summary") or {})
797
- summary["consolidation_candidates"] = candidates
798
- result["summary"] = summary
799
- result["generated_at"] = _now()
800
- return result
801
-
802
- # ── contradictions ───────────────────────────────────────────────────
803
-
804
- def contradictions(
805
- self, *, user_email: Optional[str] = None, workspace_id: Optional[str] = None
806
- ) -> Dict[str, Any]:
807
- memories = self._workspace_memories(user_email=user_email, workspace_id=workspace_id)
808
- memory_rows = [
809
- {
810
- "id": str(m.get("id") or f"mem-{i}"),
811
- "content": str(m.get("content") or ""),
812
- "score": 0.6,
813
- "source": "workspace",
814
- "timestamp": m.get("created_at") or m.get("timestamp") or 0,
815
- }
816
- for i, m in enumerate(memories)
817
- if str(m.get("content") or "").strip()
818
- ]
819
-
820
- conflicts: List[Dict[str, Any]] = []
821
- candidates = self._memory_quality.extract_candidates(memory_rows)
822
- for candidate in self._memory_quality.detect_conflicts(candidates):
823
- pair_conflicts = [c for c in candidate.conflicts if c.startswith("conflict:contradicts:")]
824
- for marker in pair_conflicts:
825
- other_id = marker.rsplit(":", 1)[-1]
826
- if any(
827
- c["kind"] == "memory_pair"
828
- and {c["left_id"], c["right_id"]} == {candidate.id, other_id}
829
- for c in conflicts
830
- ):
831
- continue
832
- other: Dict[str, Any] | None = next(
833
- (r for r in memory_rows if r["id"] == other_id), None
834
- )
835
- conflicts.append({
836
- "kind": "memory_pair",
837
- "left_id": candidate.id,
838
- "left_content": candidate.content[:200],
839
- "right_id": other_id,
840
- "right_content": (other or {}).get("content", "")[:200],
841
- "signal": "preference_negation",
842
- })
843
-
844
- temporal = self._memory_quality.detect_temporal_contradictions(memory_rows)
845
- temporal_items = [
846
- {
847
- "kind": "temporal",
848
- "id": item.get("id"),
849
- "content": str(item.get("content") or "")[:200],
850
- "signal": item.get("proactive_flag"),
851
- }
852
- for item in temporal
853
- ]
854
-
855
- edge_items: List[Dict[str, Any]] = []
856
- sample = self._graph_sample(workspace_id=workspace_id)
857
- for edge in sample["edges"]:
858
- if "CONTRADICT" in str(edge.get("type") or "").upper():
859
- edge_items.append({
860
- "kind": "graph_edge",
861
- "id": edge.get("id"),
862
- "source": edge.get("source") or edge.get("from_node"),
863
- "target": edge.get("target") or edge.get("to_node"),
864
- "signal": "contradicts_edge",
865
- })
866
-
867
- # v9.6.x additive: graph-layer node-content contradictions (proactive
868
- # detector over node title/summary), on top of the memory + edge scans.
869
- graph_pair_items: List[Dict[str, Any]] = []
870
- proactive = self._proactive()
871
- if proactive is not None:
872
- try:
873
- graph_result = proactive.detect_contradictions(workspace_id=workspace_id)
874
- graph_pair_items = [
875
- {"kind": "graph_node_pair", **pair}
876
- for pair in graph_result.get("node_pairs") or []
877
- ]
878
- except Exception:
879
- LOGGER.exception("graph contradiction scan failed")
880
-
881
- items = conflicts + temporal_items + edge_items + graph_pair_items
882
- return {
883
- "items": items,
884
- "count": len(items),
885
- "sources": {
886
- "memory_pairs": len(conflicts),
887
- "temporal": len(temporal_items),
888
- "graph_edges": len(edge_items),
889
- "graph_node_pairs": len(graph_pair_items),
890
- },
891
- "memories_scanned": len(memory_rows),
892
- "generated_at": _now(),
893
- }
894
-
895
- # ── consolidation ────────────────────────────────────────────────────
896
-
897
- def consolidate(
898
- self,
899
- *,
900
- apply: bool = False,
901
- user_email: Optional[str] = None,
902
- workspace_id: Optional[str] = None,
903
- ) -> Dict[str, Any]:
904
- memories = self._workspace_memories(user_email=user_email, workspace_id=workspace_id)
905
- memory_rows = [
906
- {
907
- "id": str(m.get("id") or f"mem-{i}"),
908
- "content": str(m.get("content") or ""),
909
- "score": 0.6,
910
- "source": "workspace",
911
- }
912
- for i, m in enumerate(memories)
913
- if str(m.get("content") or "").strip()
914
- ]
915
- candidates = self._memory_quality.extract_candidates(memory_rows)
916
- kept = self._memory_quality.dedupe(candidates)
917
- kept_ids = {c.id for c in kept}
918
- duplicate_memory_ids = [c.id for c in candidates if c.id not in kept_ids]
919
-
920
- sample = self._graph_sample(workspace_id=workspace_id)
921
- duplicate_edge_ids = [
922
- edge_id
923
- for edge_id in self._edge_quality.detect_duplicate_edges(sample["edges"])
924
- if edge_id
925
- ]
926
-
927
- pruned = 0
928
- if apply and duplicate_memory_ids and self._memory is not None:
929
- try:
930
- result = self._memory.prune(
931
- ids=duplicate_memory_ids,
932
- user_email=user_email,
933
- workspace_id=workspace_id,
934
- )
935
- pruned = int(result.get("count") or 0)
936
- except Exception:
937
- LOGGER.exception("consolidation prune failed")
938
-
939
- # v9.6.x additive: graph-layer node merge plan. Always dry-run from
940
- # this service — graph content changes stay proposal-first; the plan
941
- # is surfaced so a governed apply path can adopt it later.
942
- graph_consolidation: Optional[Dict[str, Any]] = None
943
- proactive = self._proactive()
944
- if proactive is not None:
945
- try:
946
- graph_consolidation = proactive.consolidate_duplicates(
947
- workspace_id=workspace_id, dry_run=True
948
- )
949
- except Exception:
950
- LOGGER.exception("graph consolidation plan failed")
951
-
952
- return {
953
- "mode": "applied" if apply else "dry_run",
954
- "memories_scanned": len(memory_rows),
955
- "duplicate_memories": duplicate_memory_ids,
956
- "duplicate_memory_count": len(duplicate_memory_ids),
957
- "pruned": pruned,
958
- # Graph edges are reported for review only; consolidation never
959
- # mutates graph content directly.
960
- "duplicate_edges": duplicate_edge_ids[:50],
961
- "duplicate_edge_count": len(duplicate_edge_ids),
962
- "graph_consolidation": graph_consolidation,
963
- "generated_at": _now(),
964
- }
965
-
966
-
967
- __all__ = ["BrainIntelligenceService"]