superlocalmemory 4.0.3 → 4.0.5

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 (74) hide show
  1. package/CHANGELOG.md +55 -0
  2. package/README.md +19 -13
  3. package/ide/configs/codex-mcp.toml +2 -2
  4. package/package.json +1 -1
  5. package/plugin/.claude-plugin/plugin.json +1 -1
  6. package/plugin/.mcp.json +1 -0
  7. package/plugin/CLAUDE.md +3 -3
  8. package/plugin/agents/slm-governance-advisor.md +1 -1
  9. package/plugin/agents/slm-loop-runner.md +1 -1
  10. package/plugin/agents/slm-memory-advisor.md +1 -1
  11. package/plugin/agents/slm-optimize-advisor.md +1 -1
  12. package/plugin/requirements.txt +1 -1
  13. package/plugin/skills/slm-cache/SKILL.md +1 -1
  14. package/plugin/skills/slm-compress/SKILL.md +1 -1
  15. package/plugin/skills/slm-governance/SKILL.md +1 -1
  16. package/plugin/skills/slm-graph/SKILL.md +3 -2
  17. package/plugin/skills/slm-loop/SKILL.md +1 -1
  18. package/plugin/skills/slm-mesh/SKILL.md +1 -1
  19. package/plugin/skills/slm-profile/SKILL.md +5 -4
  20. package/plugin/skills/slm-recall/SKILL.md +1 -1
  21. package/plugin/skills/slm-remember/SKILL.md +1 -1
  22. package/plugin/skills/slm-scope/SKILL.md +1 -1
  23. package/plugin/skills/slm-session/SKILL.md +1 -1
  24. package/plugin/skills/slm-status/SKILL.md +1 -1
  25. package/plugin-src/rules/AGENTS.md +7 -6
  26. package/plugin-src/skills/slm-cache/SKILL.md +1 -1
  27. package/plugin-src/skills/slm-compress/SKILL.md +1 -1
  28. package/plugin-src/skills/slm-governance/SKILL.md +1 -1
  29. package/plugin-src/skills/slm-graph/SKILL.md +3 -2
  30. package/plugin-src/skills/slm-loop/SKILL.md +1 -1
  31. package/plugin-src/skills/slm-mesh/SKILL.md +1 -1
  32. package/plugin-src/skills/slm-profile/SKILL.md +5 -4
  33. package/plugin-src/skills/slm-recall/SKILL.md +1 -1
  34. package/plugin-src/skills/slm-remember/SKILL.md +1 -1
  35. package/plugin-src/skills/slm-scope/SKILL.md +1 -1
  36. package/plugin-src/skills/slm-session/SKILL.md +1 -1
  37. package/plugin-src/skills/slm-status/SKILL.md +1 -1
  38. package/pyproject.toml +1 -1
  39. package/src/superlocalmemory/__init__.py +1 -1
  40. package/src/superlocalmemory/brain/__init__.py +5 -0
  41. package/src/superlocalmemory/brain/truth.py +348 -0
  42. package/src/superlocalmemory/cli/commands.py +82 -25
  43. package/src/superlocalmemory/cli/main.py +12 -0
  44. package/src/superlocalmemory/core/context_cache.py +58 -1
  45. package/src/superlocalmemory/core/mutations.py +155 -25
  46. package/src/superlocalmemory/core/recall_pipeline.py +6 -10
  47. package/src/superlocalmemory/core/remember_runtime.py +271 -2
  48. package/src/superlocalmemory/core/store_pipeline.py +100 -38
  49. package/src/superlocalmemory/encoding/consolidator.py +17 -47
  50. package/src/superlocalmemory/encoding/temporal_validator.py +14 -18
  51. package/src/superlocalmemory/hooks/user_prompt_hook.py +1 -1
  52. package/src/superlocalmemory/integrations/bounded_loops_mcp.py +185 -0
  53. package/src/superlocalmemory/learning/database.py +2 -1
  54. package/src/superlocalmemory/mcp/profiles.py +25 -7
  55. package/src/superlocalmemory/mcp/server.py +7 -2
  56. package/src/superlocalmemory/mcp/tools_brain.py +138 -9
  57. package/src/superlocalmemory/mcp/tools_core.py +88 -3
  58. package/src/superlocalmemory/retrieval/engine.py +7 -10
  59. package/src/superlocalmemory/retrieval/temporal_validity_filter.py +119 -19
  60. package/src/superlocalmemory/server/routes/brain.py +21 -1
  61. package/src/superlocalmemory/server/routes/memories.py +129 -3
  62. package/src/superlocalmemory/storage/_migration_internals.py +8 -0
  63. package/src/superlocalmemory/storage/_schema_version.py +2 -2
  64. package/src/superlocalmemory/storage/agent_experience.py +26 -4
  65. package/src/superlocalmemory/storage/correction_cases.py +670 -0
  66. package/src/superlocalmemory/storage/database.py +194 -24
  67. package/src/superlocalmemory/storage/external_evidence.py +359 -0
  68. package/src/superlocalmemory/storage/migration_runner.py +12 -0
  69. package/src/superlocalmemory/storage/migrations/M041_external_evidence_receipts.py +189 -0
  70. package/src/superlocalmemory/storage/migrations/M042_correction_case_ledger.py +245 -0
  71. package/src/superlocalmemory/storage/migrations/__init__.py +4 -0
  72. package/src/superlocalmemory/storage/write_coordinator.py +4 -0
  73. package/src/superlocalmemory/ui/js/brain.js +43 -7
  74. package/src/superlocalmemory/ui/js/od-brain.js +44 -19
@@ -14,6 +14,7 @@ from __future__ import annotations
14
14
  import hashlib
15
15
  import json
16
16
  import logging
17
+ import sqlite3
17
18
  import uuid
18
19
  from typing import TYPE_CHECKING, Any
19
20
 
@@ -51,6 +52,72 @@ def _ingestion_effect_id(operation_id: str, *parts: object) -> str:
51
52
  return hashlib.sha256(payload.encode("utf-8")).hexdigest()[:32]
52
53
 
53
54
 
55
+ def _record_correction_candidate(
56
+ db: DatabaseManager,
57
+ *,
58
+ operation_id: str,
59
+ profile_id: str,
60
+ scope: str,
61
+ predecessor_fact_id: str,
62
+ successor_fact_id: str,
63
+ reason_code: str,
64
+ trusted_actor_id: str,
65
+ ) -> None:
66
+ """Append a review candidate through the current canonical transaction.
67
+
68
+ This intentionally carries identifiers and a controlled reason code only.
69
+ It must not use detector prose because that can contain user memory text.
70
+ If the current path is bound to the canonical coordinator,
71
+ ``raw_connection`` yields its already-open transaction; otherwise the
72
+ database manager owns the short transaction. Either path is atomic.
73
+ """
74
+ if not trusted_actor_id or not predecessor_fact_id or not successor_fact_id:
75
+ return
76
+ if predecessor_fact_id == successor_fact_id:
77
+ return
78
+ from superlocalmemory.storage.correction_cases import (
79
+ CorrectionActor,
80
+ CorrectionCaseError,
81
+ propose_on_connection,
82
+ )
83
+
84
+ case_id = _ingestion_effect_id(
85
+ operation_id, "correction-case", profile_id, predecessor_fact_id,
86
+ successor_fact_id, reason_code,
87
+ )
88
+ idempotency_key = _ingestion_effect_id(
89
+ operation_id, "correction-proposal", profile_id, predecessor_fact_id,
90
+ successor_fact_id, reason_code,
91
+ )
92
+ actor = CorrectionActor(
93
+ actor_id=trusted_actor_id,
94
+ actor_kind="host_attested",
95
+ trust_tier="canonical_writer",
96
+ )
97
+ try:
98
+ with db.raw_connection() as conn:
99
+ propose_on_connection(
100
+ conn,
101
+ case_id=case_id,
102
+ profile_id=profile_id,
103
+ scope=scope,
104
+ predecessor_fact_id=predecessor_fact_id,
105
+ successor_fact_id=successor_fact_id,
106
+ reason_code=reason_code,
107
+ actor=actor,
108
+ idempotency_key=idempotency_key,
109
+ # Candidate detection observes a possible correction. It does
110
+ # not claim an event-time boundary from heuristic evidence.
111
+ is_profile_active=lambda candidate_profile: candidate_profile == profile_id,
112
+ is_actor_trusted=lambda candidate_actor: candidate_actor == actor,
113
+ )
114
+ except (CorrectionCaseError, sqlite3.Error, ValueError) as exc:
115
+ # A missing/hot-upgrading M042 ledger must not make memory ingestion
116
+ # unavailable. The candidate is advisory and has no retrieval effect;
117
+ # the warning is the operational signal that an operator must inspect.
118
+ logger.warning("Correction candidate not recorded for %s: %s", successor_fact_id, exc)
119
+
120
+
54
121
  def _record_fact_entity_association(
55
122
  db: DatabaseManager,
56
123
  *,
@@ -731,47 +798,30 @@ def run_store(
731
798
  continue
732
799
  fact = existing_fact
733
800
 
734
- # Opinion confidence tracking: reinforce or decay
735
- if fact.fact_type == FactType.OPINION and action.action_type.value == "update":
736
- try:
737
- existing = db.get_fact(
738
- action.existing_fact_id or action.new_fact_id
739
- )
740
- if existing and existing.fact_type == FactType.OPINION:
741
- new_conf = min(1.0, existing.confidence + 0.1)
742
- db.update_fact(existing.fact_id, {"confidence": new_conf})
743
- except Exception:
744
- pass
745
- elif fact.fact_type == FactType.OPINION and action.action_type.value == "supersede":
746
- try:
747
- old_id = getattr(action, "old_fact_id", None)
748
- if old_id:
749
- old_fact = db.get_fact(old_id)
750
- if old_fact:
751
- new_conf = max(0.0, old_fact.confidence - 0.2)
752
- db.update_fact(old_id, {"confidence": new_conf})
753
- except Exception:
754
- pass
755
-
756
801
  if action.action_type.value in ("update", "supersede"):
757
- target_id = (
758
- (action.existing_fact_id or action.new_fact_id)
759
- if action.action_type.value == "update"
760
- else action.new_fact_id
761
- )
762
- if is_queryable_promotion and target_id != fact.fact_id:
763
- db.delete_fact(fact.fact_id)
764
- updated_fact = db.get_fact(target_id)
765
- if updated_fact is None:
802
+ # A consolidator UPDATE/SUPERSEDE is a review-required
803
+ # proposal. It persists the incoming successor and does
804
+ # not mutate, delete, archive, or change trust on the
805
+ # matched predecessor. Continue materializing the
806
+ # incoming fact through the common projection pipeline.
807
+ proposed_fact = db.get_fact(action.new_fact_id)
808
+ if proposed_fact is None:
766
809
  raise RuntimeError(
767
- f"consolidation {action.action_type.value} produced "
768
- f"missing fact {target_id}"
810
+ f"consolidation {action.action_type.value} proposal produced "
811
+ f"missing fact {action.new_fact_id}"
812
+ )
813
+ fact = proposed_fact
814
+ if action.existing_fact_id:
815
+ _record_correction_candidate(
816
+ db,
817
+ operation_id=ingestion_operation_id,
818
+ profile_id=profile_id,
819
+ scope=scope,
820
+ predecessor_fact_id=action.existing_fact_id,
821
+ successor_fact_id=action.new_fact_id,
822
+ reason_code=f"consolidation_{action.action_type.value}",
823
+ trusted_actor_id=trusted_actor_id,
769
824
  )
770
- # Continue through the shared index/graph/temporal/
771
- # provenance stages. The previous early continue made
772
- # UPDATE/SUPERSEDE facts look stored while skipping half of
773
- # canonical materialization.
774
- fact = updated_fact
775
825
  # ADD case: consolidator already stored the fact (F8 fix)
776
826
  # Fall through to post-processing below
777
827
  else:
@@ -860,6 +910,18 @@ def run_store(
860
910
  "Temporal: %d facts invalidated by new fact %s",
861
911
  len(invalidations), fact.fact_id,
862
912
  )
913
+ for candidate in invalidations:
914
+ predecessor_fact_id = str(candidate.get("old_fact_id") or "")
915
+ _record_correction_candidate(
916
+ db,
917
+ operation_id=ingestion_operation_id,
918
+ profile_id=profile_id,
919
+ scope=scope,
920
+ predecessor_fact_id=predecessor_fact_id,
921
+ successor_fact_id=fact.fact_id,
922
+ reason_code="temporal_contradiction",
923
+ trusted_actor_id=trusted_actor_id,
924
+ )
863
925
  except Exception as exc:
864
926
  temporal_complete = False
865
927
  logger.debug(
@@ -4,10 +4,13 @@
4
4
 
5
5
  """SuperLocalMemory V3 — Memory Consolidator.
6
6
 
7
- Mem0-style ADD/UPDATE/SUPERSEDE/NOOP logic for incoming facts.
8
- V1 was append-only (never updated, never deleted, never merged).
9
- This module gives a ~26% uplift by deduplicating, updating, and
10
- resolving contradictions at encoding time.
7
+ Mem0-style ADD/UPDATE/SUPERSEDE/NOOP classification for incoming facts.
8
+
9
+ UPDATE and SUPERSEDE are now *proposal classifications*: they persist the
10
+ incoming fact but never rewrite, archive, lower trust for, or otherwise mutate
11
+ the matched fact. A reviewed correction owner may later apply a case through
12
+ the correction ledger. This keeps ingestion useful while preventing an
13
+ automatic model judgement from changing the historical record.
11
14
 
12
15
  Mode A: keyword-based contradiction detection (zero LLM).
13
16
  Mode B/C: LLM-assisted contradiction detection when available.
@@ -20,7 +23,7 @@ from __future__ import annotations
20
23
 
21
24
  import logging
22
25
  import math
23
- from typing import Any, Protocol
26
+ from typing import Protocol
24
27
 
25
28
  from superlocalmemory.core.config import EncodingConfig
26
29
  from superlocalmemory.storage.database import DatabaseManager
@@ -30,7 +33,6 @@ from superlocalmemory.storage.models import (
30
33
  ConsolidationActionType,
31
34
  EdgeType,
32
35
  GraphEdge,
33
- MemoryLifecycle,
34
36
  )
35
37
 
36
38
  logger = logging.getLogger(__name__)
@@ -310,29 +312,17 @@ class MemoryConsolidator:
310
312
  *,
311
313
  reason: str,
312
314
  ) -> ConsolidationAction:
313
- """Update existing fact: bump evidence, optionally merge content."""
314
- new_evidence = existing.evidence_count + 1
315
- new_confidence = min(1.0, existing.confidence + 0.05)
316
- updates: dict[str, Any] = {
317
- "evidence_count": new_evidence,
318
- "confidence": new_confidence,
319
- }
320
-
321
- # If LLM available, merge content for a richer fact
322
- if self._llm is not None and self._llm.is_available():
323
- merged = self._merge_facts(existing.content, new_fact.content)
324
- if merged:
325
- updates["content"] = merged
326
-
327
- self._db.update_fact(existing.fact_id, updates)
315
+ """Persist a proposed refinement without rewriting the predecessor."""
316
+ self._db.store_fact(new_fact)
317
+ self._create_semantic_edges(new_fact, profile_id)
328
318
  action = self._log_action(
329
319
  ConsolidationActionType.UPDATE,
330
320
  new_fact.fact_id, existing.fact_id,
331
321
  profile_id, reason,
332
322
  )
333
323
  logger.debug(
334
- "UPDATE fact %s (evidence=%d): %s",
335
- existing.fact_id, new_evidence, reason,
324
+ "UPDATE proposal %s -> %s: %s",
325
+ existing.fact_id, new_fact.fact_id, reason,
336
326
  )
337
327
  return action
338
328
 
@@ -344,37 +334,17 @@ class MemoryConsolidator:
344
334
  *,
345
335
  reason: str,
346
336
  ) -> ConsolidationAction:
347
- """Archive old fact, store new, create contradiction edge."""
348
- # Archive old fact (keep for history but deprioritize in retrieval)
349
- self._db.update_fact(
350
- existing.fact_id,
351
- {"lifecycle": MemoryLifecycle.ARCHIVED},
352
- )
353
- # Store new fact
337
+ """Persist a proposed successor without changing the predecessor."""
354
338
  self._db.store_fact(new_fact)
355
- # Create contradiction + supersedes edges
356
- self._db.store_edge(GraphEdge(
357
- profile_id=profile_id,
358
- source_id=new_fact.fact_id,
359
- target_id=existing.fact_id,
360
- edge_type=EdgeType.CONTRADICTION,
361
- weight=1.0,
362
- ))
363
- self._db.store_edge(GraphEdge(
364
- profile_id=profile_id,
365
- source_id=new_fact.fact_id,
366
- target_id=existing.fact_id,
367
- edge_type=EdgeType.SUPERSEDES,
368
- weight=1.0,
369
- ))
339
+ self._create_semantic_edges(new_fact, profile_id)
370
340
  action = self._log_action(
371
341
  ConsolidationActionType.SUPERSEDE,
372
342
  new_fact.fact_id, existing.fact_id,
373
343
  profile_id, reason,
374
344
  )
375
345
  logger.debug(
376
- "SUPERSEDE %s %s: %s",
377
- new_fact.fact_id, existing.fact_id, reason,
346
+ "SUPERSEDE proposal %s -> %s: %s",
347
+ existing.fact_id, new_fact.fact_id, reason,
378
348
  )
379
349
  return action
380
350
 
@@ -2,7 +2,7 @@
2
2
  # Licensed under AGPL-3.0-or-later - see LICENSE file
3
3
  # Part of SuperLocalMemory V3
4
4
 
5
- """Temporal Intelligence -- contradiction detection and fact invalidation.
5
+ """Temporal Intelligence -- contradiction detection and reviewable proposals.
6
6
 
7
7
  Implements full bi-temporal validity tracking with 4 timestamps (L8 fix).
8
8
  Contradiction detection via sheaf cohomology (Mode A: pure math) or
@@ -36,7 +36,7 @@ logger = logging.getLogger(__name__)
36
36
 
37
37
 
38
38
  class TemporalValidator:
39
- """Validates temporal consistency and manages fact invalidation.
39
+ """Validates temporal consistency and proposes fact corrections.
40
40
 
41
41
  Components received via __init__ (NOT the engine -- Rule 06):
42
42
  - db: DatabaseManager
@@ -75,15 +75,20 @@ class TemporalValidator:
75
75
  new_fact: AtomicFact,
76
76
  profile_id: str,
77
77
  ) -> list[dict]:
78
- """Check new fact for contradictions and invalidate old facts.
78
+ """Check new fact for contradictions without mutating old facts.
79
79
 
80
80
  Algorithm:
81
81
  1. Detect contradictions (sheaf or LLM).
82
- 2. For each contradiction: invalidate old fact (set valid_until + system_expired_at).
83
- 3. Apply trust penalty to invalidated facts.
84
- 4. Return list of invalidation actions.
82
+ 2. Exclude valid historical progressions using explicit event anchors.
83
+ 3. Return review-required correction candidates.
85
84
 
86
- Returns list of dicts: {old_fact_id, new_fact_id, reason, severity}
85
+ This legacy method name is intentionally retained for API compatibility.
86
+ It no longer expires facts, changes trust, or changes retrieval state.
87
+ The reviewed-correction owner is the only layer permitted to apply a
88
+ candidate to bi-temporal validity.
89
+
90
+ Returns list of dicts: {old_fact_id, new_fact_id, reason, severity,
91
+ status="proposed"}.
87
92
  """
88
93
  contradictions = self.detect_contradiction(new_fact, profile_id)
89
94
 
@@ -108,25 +113,16 @@ class TemporalValidator:
108
113
  )
109
114
  continue
110
115
 
111
- # Step 1: Invalidate the old fact (bi-temporal)
112
- self.invalidate_fact(
113
- fact_id=old_fact_id,
114
- invalidated_by=new_fact.fact_id,
115
- reason=reason,
116
- )
117
-
118
- # Step 2: Apply trust penalty
119
- self._apply_trust_penalty(old_fact_id, profile_id)
120
-
121
116
  actions.append({
122
117
  "old_fact_id": old_fact_id,
123
118
  "new_fact_id": new_fact.fact_id,
124
119
  "reason": reason,
125
120
  "severity": severity,
121
+ "status": "proposed",
126
122
  })
127
123
 
128
124
  logger.info(
129
- "Temporal: invalidated %d facts due to new fact %s",
125
+ "Temporal: proposed %d correction(s) due to new fact %s",
130
126
  len(actions), new_fact.fact_id,
131
127
  )
132
128
  return actions
@@ -98,7 +98,7 @@ def main() -> int:
98
98
 
99
99
  try:
100
100
  topic_sig = compute_topic_signature(prompt, entity_hits=entity_hits)
101
- entry = read_entry_fast(session_id, topic_sig)
101
+ entry = read_entry_fast(session_id, topic_sig, require_current_admission=True)
102
102
  except Exception:
103
103
  sys.stdout.write("{}")
104
104
  return 0
@@ -0,0 +1,185 @@
1
+ """Versioned, read-only contract boundary for Bounded Loops MCP evidence."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import asyncio
6
+ import json
7
+ import os
8
+ import shutil
9
+ import stat
10
+ from collections.abc import Awaitable, Callable
11
+ from copy import deepcopy
12
+ from pathlib import Path
13
+ from typing import Any
14
+
15
+ CONTRACT_ID = "bounded-loops.dev/slm-bridge/v1"
16
+ _OBSERVATION_TIMEOUT_SECONDS = 5.0
17
+ _MAX_MCP_TEXT_BYTES = 2 * 1024 * 1024
18
+ _ADVERTISEMENT = {
19
+ "id": CONTRACT_ID,
20
+ "tool": "bl_graph_evidence",
21
+ "operation": "observe_terminal_run",
22
+ }
23
+
24
+
25
+ class BridgeUnavailable(ValueError):
26
+ """The installed producer does not advertise a compatible bridge contract."""
27
+
28
+
29
+ def supports_bridge(capabilities: dict[str, Any]) -> bool:
30
+ """Negotiate on the declared public contract, never producer semver."""
31
+ advertised = capabilities.get("evidence_contracts")
32
+ return isinstance(advertised, list) and any(
33
+ isinstance(item, dict)
34
+ and all(item.get(key) == value for key, value in _ADVERTISEMENT.items())
35
+ for item in advertised
36
+ )
37
+
38
+
39
+ def bridge_payload(evidence: dict[str, Any], *, profile_id: str) -> dict[str, Any]:
40
+ """Attach active-profile identity after refusing incompatible evidence."""
41
+ if evidence.get("contract") != CONTRACT_ID:
42
+ raise BridgeUnavailable("unsupported bounded-loops evidence contract")
43
+ if evidence.get("eligible_for_learning") is not False:
44
+ raise BridgeUnavailable("bounded-loops evidence is not observation-only")
45
+ # The producer has organisation/project metadata for its own control plane.
46
+ # SLM stores only the v1 observation receipt needed by its profile-scoped
47
+ # learning database; retaining arbitrary producer extensions would turn a
48
+ # versioned contract into an unbounded schema sink.
49
+ fields = (
50
+ "contract",
51
+ "workspace_id",
52
+ "run_ref",
53
+ "run_id",
54
+ "outcome",
55
+ "run_state",
56
+ "demonstration",
57
+ "eligible_for_learning",
58
+ "terminal_at",
59
+ "graph_digest",
60
+ "plan_digest",
61
+ "policy_digest",
62
+ "receipt",
63
+ "nodes",
64
+ )
65
+ if any(field not in evidence for field in fields):
66
+ raise BridgeUnavailable("bounded-loops evidence is missing required v1 fields")
67
+ return {field: deepcopy(evidence[field]) for field in fields} | {"profile_id": profile_id}
68
+
69
+
70
+ async def observe_terminal_runs(
71
+ call_tool: Callable[[str, dict[str, Any]], Awaitable[dict[str, Any]]], *, profile_id: str
72
+ ) -> list[dict[str, Any]]:
73
+ """Collect only producer-advertised terminal evidence over an injected MCP transport."""
74
+ discovery = await call_tool("bl_capabilities", {})
75
+ if discovery.get("status") != "ok" or not supports_bridge(discovery.get("capabilities", {})):
76
+ raise BridgeUnavailable("bounded-loops does not advertise slm-bridge/v1")
77
+ listing = await call_tool("bl_graph_terminal_runs", {"limit": 100})
78
+ if listing.get("status") != "ok" or listing.get("contract") != CONTRACT_ID:
79
+ raise BridgeUnavailable("bounded-loops terminal listing is unavailable")
80
+ runs = listing.get("runs")
81
+ if not isinstance(runs, list):
82
+ raise BridgeUnavailable("bounded-loops terminal listing is malformed")
83
+ # The producer's limit is advisory. Keep this explicit operation bounded
84
+ # even against a compatible but faulty/malicious producer.
85
+ runs = runs[:100]
86
+ observed: list[dict[str, Any]] = []
87
+ for run in runs:
88
+ if not isinstance(run, dict) or not isinstance(run.get("run_ref"), str):
89
+ raise BridgeUnavailable("bounded-loops terminal listing is malformed")
90
+ response = await call_tool("bl_graph_evidence", {"run_ref": run["run_ref"]})
91
+ if response.get("status") == "unavailable":
92
+ continue
93
+ if response.get("status") != "ok" or not isinstance(response.get("evidence"), dict):
94
+ raise BridgeUnavailable("bounded-loops evidence response is malformed")
95
+ observed.append(bridge_payload(response["evidence"], profile_id=profile_id))
96
+ return observed
97
+
98
+
99
+ async def observe_from_stdio(*, command: str, cwd: str, profile_id: str) -> list[dict[str, Any]]:
100
+ """Run one bounded, explicit MCP 2 observation; never call from recall or remember."""
101
+ executable, workspace = Path(command), Path(cwd)
102
+ if (
103
+ not executable.is_absolute()
104
+ or not executable.is_file()
105
+ or not workspace.is_absolute()
106
+ or not workspace.is_dir()
107
+ or workspace.is_symlink()
108
+ ):
109
+ raise BridgeUnavailable(
110
+ "bounded-loops bridge requires an approved executable and workspace"
111
+ )
112
+ try:
113
+ executable = executable.resolve(strict=True)
114
+ workspace = workspace.resolve(strict=True)
115
+ mode = executable.stat().st_mode
116
+ except OSError as exc:
117
+ raise BridgeUnavailable("bounded-loops bridge path is unavailable") from exc
118
+ if not stat.S_ISREG(mode) or (
119
+ os.name != "nt" and mode & (stat.S_IWGRP | stat.S_IWOTH)
120
+ ):
121
+ raise BridgeUnavailable("bounded-loops executable is not a trusted regular file")
122
+ if executable.stat().st_uid not in {0, os.geteuid()}:
123
+ raise BridgeUnavailable("bounded-loops executable owner is not trusted")
124
+
125
+ from mcp import ClientSession, StdioServerParameters
126
+ from mcp.client.stdio import stdio_client
127
+
128
+ try:
129
+ parameters = StdioServerParameters(
130
+ command=str(executable), args=[], cwd=str(workspace)
131
+ )
132
+ async with stdio_client(parameters) as (read, write):
133
+ async with ClientSession(
134
+ read,
135
+ write,
136
+ # MCP 2.x passes this directly to AnyIO's timeout machinery,
137
+ # which accepts a numeric duration rather than timedelta.
138
+ read_timeout_seconds=_OBSERVATION_TIMEOUT_SECONDS,
139
+ ) as session:
140
+ async def observe() -> list[dict[str, Any]]:
141
+ await session.initialize()
142
+
143
+ async def call(name: str, arguments: dict[str, Any]) -> dict[str, Any]:
144
+ result = await session.call_tool(name, arguments)
145
+ if result.is_error:
146
+ raise BridgeUnavailable(
147
+ "bounded-loops rejected the observation request"
148
+ )
149
+ texts = [item.text for item in result.content if hasattr(item, "text")]
150
+ if len(texts) != 1 or len(texts[0].encode("utf-8")) > _MAX_MCP_TEXT_BYTES:
151
+ raise BridgeUnavailable("bounded-loops returned an invalid MCP payload")
152
+ try:
153
+ payload = json.loads(texts[0])
154
+ except json.JSONDecodeError as exc:
155
+ raise BridgeUnavailable("bounded-loops returned invalid JSON") from exc
156
+ if not isinstance(payload, dict):
157
+ raise BridgeUnavailable("bounded-loops returned an invalid MCP payload")
158
+ return payload
159
+
160
+ return await observe_terminal_runs(call, profile_id=profile_id)
161
+ return await asyncio.wait_for(observe(), timeout=_OBSERVATION_TIMEOUT_SECONDS)
162
+ except BridgeUnavailable:
163
+ raise
164
+ except Exception as exc:
165
+ raise BridgeUnavailable("bounded-loops observation timed out or could not start") from exc
166
+
167
+
168
+ async def observe_installed(*, workspace: str, profile_id: str) -> list[dict[str, Any]]:
169
+ """Observe a user-installed producer without accepting an agent command.
170
+
171
+ Discovery deliberately resolves exactly the public ``bounded-loops-mcp``
172
+ executable. It does not accept a command, shell fragment, or arguments
173
+ from an MCP caller; the only caller-supplied value is the existing project
174
+ workspace whose Bounded Loops state is to be read.
175
+ """
176
+ command = shutil.which("bounded-loops-mcp")
177
+ if command is None:
178
+ raise BridgeUnavailable("bounded-loops-mcp is not installed")
179
+ if not Path(command).is_absolute():
180
+ raise BridgeUnavailable("bounded-loops-mcp discovery returned an unsafe path")
181
+ if Path(command).resolve().name not in {"bounded-loops-mcp", "bounded-loops-mcp.exe"}:
182
+ raise BridgeUnavailable("bounded-loops-mcp discovery returned an unsafe executable")
183
+ return await observe_from_stdio(
184
+ command=str(Path(command).resolve()), cwd=workspace, profile_id=profile_id
185
+ )
@@ -613,7 +613,8 @@ class LearningDatabase:
613
613
  row[0]
614
614
  for row in conn.execute(
615
615
  "SELECT name FROM sqlite_master WHERE type='table' "
616
- "AND name IN ('agent_experiences', 'cognitive_turn_receipts')"
616
+ "AND name IN ('agent_experiences', 'cognitive_turn_receipts', "
617
+ "'external_evidence_receipts')"
617
618
  )
618
619
  }
619
620
  if profile_id is None:
@@ -17,10 +17,13 @@ from __future__ import annotations
17
17
  # v3.6.14 WP-01: Named profile definitions
18
18
  # ---------------------------------------------------------------------------
19
19
 
20
- _PROFILE_CORE: frozenset[str] = frozenset({ # 14
20
+ _PROFILE_CORE: frozenset[str] = frozenset({ # 16
21
21
  "remember", "recall", "search", "fetch", "list_recent", "update_memory", "forget",
22
22
  "session_init", "close_session",
23
23
  "slm_compress", "slm_retrieve", "slm_cache_set", "slm_cache_get", "slm_optimize_stats",
24
+ # A client that can propose a correction must be able to inspect and
25
+ # authenticate its review; otherwise the core lifecycle is incomplete.
26
+ "review_correction", "list_corrections",
24
27
  })
25
28
 
26
29
  # Portable Brain evidence must reach the coding-host profile shipped by the
@@ -28,9 +31,10 @@ _PROFILE_CORE: frozenset[str] = frozenset({ # 14
28
31
  _PROFILE_BRAIN: frozenset[str] = frozenset({
29
32
  "get_brain_evidence_status", "record_agent_experience",
30
33
  "record_cognitive_turn", "finalize_cognitive_turn",
34
+ "observe_bounded_loop_evidence",
31
35
  })
32
36
 
33
- _PROFILE_CODE: frozenset[str] = _PROFILE_CORE | _PROFILE_BRAIN | frozenset({ # 28
37
+ _PROFILE_CODE: frozenset[str] = _PROFILE_CORE | _PROFILE_BRAIN | frozenset({ # 31
34
38
  "build_code_graph", "get_blast_radius", "query_graph",
35
39
  "semantic_search_code", "get_review_context", "detect_changes",
36
40
  # switch_profile lets a plugin/IDE session change the active workspace over
@@ -47,21 +51,23 @@ _PROFILE_FULL_MESH: frozenset[str] = frozenset({ # 8
47
51
  "mesh_state", "mesh_lock", "mesh_events", "mesh_status",
48
52
  })
49
53
 
50
- _PROFILE_FULL: frozenset[str] = frozenset({ # 38 base — EXPLICIT literal, NOT runtime _ESSENTIAL_TOOLS (OQ-2)
54
+ # 41 base — explicit literal, not runtime _ESSENTIAL_TOOLS (OQ-2).
55
+ _PROFILE_FULL: frozenset[str] = frozenset({
51
56
  "remember", "recall", "search", "fetch", "list_recent", "delete_memory", "update_memory",
52
57
  "get_status", "session_init", "observe", "close_session", "report_feedback", "forget",
53
58
  "run_maintenance", "consolidate_cognitive", "get_soft_prompts", "set_mode", "report_outcome",
54
59
  "log_tool_event", "get_assertions", "reinforce_assertion", "contradict_assertion",
55
60
  "get_brain_evidence_status", "record_agent_experience",
56
61
  "record_cognitive_turn", "finalize_cognitive_turn",
62
+ "observe_bounded_loop_evidence", "review_correction", "list_corrections",
57
63
  "evolve_skill", "skill_health", "skill_lineage", "switch_profile",
58
64
  "slm_compress", "slm_retrieve", "slm_cache_set", "slm_cache_get", "slm_optimize_stats",
59
65
  # v3.8.0: bounded-loop tools (CLI + /slm-loop command + MCP).
60
66
  "slm_loop_run", "slm_loop_history", "slm_loop_show",
61
67
  # prestage_context remains registered but deliberately raw-server-only.
62
- }) | _PROFILE_FULL_MESH # 46
68
+ }) | _PROFILE_FULL_MESH # 49
63
69
 
64
- _PROFILE_POWER: frozenset[str] = _PROFILE_FULL | frozenset({ # 58
70
+ _PROFILE_POWER: frozenset[str] = _PROFILE_FULL | frozenset({ # 61
65
71
  "get_version", "get_mode", "health", "consistency_check", "recall_trace",
66
72
  "get_lifecycle_status", "set_retention_policy", "compact_memories",
67
73
  "get_behavioral_patterns", "audit_trail", "quantize", "get_retention_stats",
@@ -84,25 +90,34 @@ _PROFILE_DEFINITIONS: dict[str, frozenset[str]] = {
84
90
  # at server startup. Any other value is a configuration error (fail closed).
85
91
  _PROFILE_ALIASES: dict[str, str] = {
86
92
  "core14": "core",
87
- # 3.8.0: switch_profile (+1) then bounded-loop tools (+3) grew code/full/
93
+ "core16": "core",
94
+ # 3.8.0 and later additions grew code/full/power; every historical count
88
95
  # power. Every historical count-suffixed name is kept so a v3.6/3.7/early-
89
96
  # 3.8 config still resolves (back-compat); new 3.8.0 counts added alongside.
90
97
  "code20": "code",
91
98
  "code21": "code",
92
99
  "code24": "code",
93
100
  "code28": "code",
101
+ "code29": "code",
102
+ "code31": "code",
94
103
  "full38": "full",
95
104
  "full39": "full",
96
105
  "full42": "full",
97
106
  "full46": "full",
107
+ "full47": "full",
108
+ "full49": "full",
98
109
  "power50": "power",
99
110
  "power51": "power",
100
111
  "power54": "power",
101
112
  "power58": "power",
113
+ "power59": "power",
114
+ "power61": "power",
102
115
  "mesh8": "mesh",
103
116
  "whole81": "whole",
104
117
  "whole84": "whole",
105
118
  "whole91": "whole",
119
+ "whole92": "whole",
120
+ "whole94": "whole",
106
121
  }
107
122
 
108
123
  # Plain-English descriptions for UI display.
@@ -110,7 +125,10 @@ _PROFILE_ALIASES: dict[str, str] = {
110
125
  # one sentence, user-facing language only.
111
126
  PROFILE_DESCRIPTIONS: dict[str, str] = {
112
127
  "core": "Essential memory: store, recall, search, sessions",
113
- "code": "Core + code graph, portable Brain evidence, and profile switching (default for IDE coding agents)",
128
+ "code": (
129
+ "Core + code graph, portable Brain evidence, and profile switching "
130
+ "(default for IDE coding agents)"
131
+ ),
114
132
  "full": "All everyday memory, portable Brain evidence, optimization, and mesh tools",
115
133
  "power": "Everything in full plus advanced governance and behavioral tools",
116
134
  "mesh": "Cross-device mesh coordination only",