superlocalmemory 4.0.10 → 4.1.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 (143) hide show
  1. package/.claude-plugin/marketplace.json +1 -1
  2. package/CHANGELOG.md +170 -0
  3. package/README.md +7 -7
  4. package/package.json +4 -2
  5. package/plugin/.claude-plugin/plugin.json +2 -2
  6. package/plugin/CLAUDE.md +3 -3
  7. package/plugin/agents/slm-governance-advisor.md +1 -1
  8. package/plugin/agents/slm-loop-runner.md +4 -4
  9. package/plugin/agents/slm-memory-advisor.md +1 -1
  10. package/plugin/agents/slm-optimize-advisor.md +1 -1
  11. package/plugin/requirements.txt +1 -1
  12. package/plugin/skills/slm-cache/SKILL.md +1 -1
  13. package/plugin/skills/slm-compress/SKILL.md +1 -1
  14. package/plugin/skills/slm-governance/SKILL.md +1 -1
  15. package/plugin/skills/slm-graph/SKILL.md +1 -1
  16. package/plugin/skills/slm-loop/SKILL.md +2 -2
  17. package/plugin/skills/slm-mesh/SKILL.md +1 -1
  18. package/plugin/skills/slm-profile/SKILL.md +5 -5
  19. package/plugin/skills/slm-recall/SKILL.md +102 -15
  20. package/plugin/skills/slm-remember/SKILL.md +35 -3
  21. package/plugin/skills/slm-scope/SKILL.md +1 -1
  22. package/plugin/skills/slm-session/SKILL.md +29 -3
  23. package/plugin/skills/slm-status/SKILL.md +1 -1
  24. package/plugin-src/rules/AGENTS.md +16 -8
  25. package/plugin-src/skills/slm-cache/SKILL.md +1 -1
  26. package/plugin-src/skills/slm-compress/SKILL.md +1 -1
  27. package/plugin-src/skills/slm-governance/SKILL.md +1 -1
  28. package/plugin-src/skills/slm-graph/SKILL.md +1 -1
  29. package/plugin-src/skills/slm-loop/SKILL.md +2 -2
  30. package/plugin-src/skills/slm-mesh/SKILL.md +1 -1
  31. package/plugin-src/skills/slm-profile/SKILL.md +5 -5
  32. package/plugin-src/skills/slm-recall/SKILL.md +102 -15
  33. package/plugin-src/skills/slm-remember/SKILL.md +35 -3
  34. package/plugin-src/skills/slm-scope/SKILL.md +1 -1
  35. package/plugin-src/skills/slm-session/SKILL.md +29 -3
  36. package/plugin-src/skills/slm-status/SKILL.md +1 -1
  37. package/pyproject.toml +1 -1
  38. package/src/superlocalmemory/__init__.py +1 -1
  39. package/src/superlocalmemory/cli/commands.py +263 -18
  40. package/src/superlocalmemory/cli/daemon.py +30 -0
  41. package/src/superlocalmemory/cli/db_migrate.py +71 -1
  42. package/src/superlocalmemory/cli/gdpr_cmd.py +15 -2
  43. package/src/superlocalmemory/cli/main.py +24 -2
  44. package/src/superlocalmemory/code_graph/database.py +44 -0
  45. package/src/superlocalmemory/compliance/gdpr.py +449 -39
  46. package/src/superlocalmemory/core/admission.py +231 -11
  47. package/src/superlocalmemory/core/backend_orchestrator.py +190 -84
  48. package/src/superlocalmemory/core/config.py +90 -11
  49. package/src/superlocalmemory/core/consolidation_engine.py +34 -0
  50. package/src/superlocalmemory/core/engine.py +140 -11
  51. package/src/superlocalmemory/core/graph_analyzer.py +76 -112
  52. package/src/superlocalmemory/core/graph_metrics.py +597 -0
  53. package/src/superlocalmemory/core/graph_pruner.py +121 -0
  54. package/src/superlocalmemory/core/maintenance_scheduler.py +205 -0
  55. package/src/superlocalmemory/core/mode_capability.py +111 -0
  56. package/src/superlocalmemory/core/ollama_validator.py +315 -0
  57. package/src/superlocalmemory/core/projection_drain.py +380 -0
  58. package/src/superlocalmemory/core/recall_pipeline.py +390 -3
  59. package/src/superlocalmemory/core/recall_worker.py +6 -3
  60. package/src/superlocalmemory/core/scale_autopromote.py +196 -0
  61. package/src/superlocalmemory/core/scale_engine.py +16 -2
  62. package/src/superlocalmemory/core/score_contract.py +21 -1
  63. package/src/superlocalmemory/core/session_identity.py +85 -0
  64. package/src/superlocalmemory/core/status_contract.py +108 -0
  65. package/src/superlocalmemory/core/worker_pool.py +4 -4
  66. package/src/superlocalmemory/core/working_memory.py +288 -0
  67. package/src/superlocalmemory/encoding/cognitive_consolidator.py +36 -6
  68. package/src/superlocalmemory/encoding/context_generator.py +1 -1
  69. package/src/superlocalmemory/encoding/entity_resolver.py +38 -0
  70. package/src/superlocalmemory/encoding/fact_extractor.py +18 -14
  71. package/src/superlocalmemory/encoding/prospective_markers.py +262 -0
  72. package/src/superlocalmemory/encoding/type_router.py +12 -12
  73. package/src/superlocalmemory/evolution/mutation_generator.py +30 -4
  74. package/src/superlocalmemory/graph/cozo_adjacency.py +122 -0
  75. package/src/superlocalmemory/graph/cozo_backend.py +103 -138
  76. package/src/superlocalmemory/hooks/portable_kit.py +10 -2
  77. package/src/superlocalmemory/learning/bandit.py +43 -0
  78. package/src/superlocalmemory/learning/consolidation_worker.py +54 -0
  79. package/src/superlocalmemory/learning/database.py +60 -3
  80. package/src/superlocalmemory/learning/entity_compiler.py +21 -58
  81. package/src/superlocalmemory/learning/feedback.py +3 -1
  82. package/src/superlocalmemory/learning/outcomes.py +47 -16
  83. package/src/superlocalmemory/learning/pattern_miner.py +28 -3
  84. package/src/superlocalmemory/learning/pattern_miner_constants.py +43 -0
  85. package/src/superlocalmemory/learning/pcos.py +291 -0
  86. package/src/superlocalmemory/learning/reward_from_outcomes.py +365 -0
  87. package/src/superlocalmemory/learning/reward_proxy.py +100 -10
  88. package/src/superlocalmemory/learning/signal_kinds.py +79 -0
  89. package/src/superlocalmemory/mcp/profiles.py +14 -2
  90. package/src/superlocalmemory/mcp/tools_active.py +2 -1
  91. package/src/superlocalmemory/mcp/tools_core.py +31 -3
  92. package/src/superlocalmemory/mcp/tools_v28.py +20 -1
  93. package/src/superlocalmemory/parameterization/pattern_extractor.py +14 -1
  94. package/src/superlocalmemory/parameterization/soft_prompt_generator.py +98 -0
  95. package/src/superlocalmemory/retrieval/bm25_channel.py +64 -3
  96. package/src/superlocalmemory/retrieval/channel_status.py +117 -0
  97. package/src/superlocalmemory/retrieval/engine.py +106 -11
  98. package/src/superlocalmemory/retrieval/entity_channel.py +210 -256
  99. package/src/superlocalmemory/retrieval/graph_adjacency.py +219 -0
  100. package/src/superlocalmemory/retrieval/scope_policy.py +20 -0
  101. package/src/superlocalmemory/retrieval/semantic_channel.py +47 -5
  102. package/src/superlocalmemory/retrieval/spreading.py +288 -0
  103. package/src/superlocalmemory/server/api.py +24 -5
  104. package/src/superlocalmemory/server/bandit_loops.py +17 -1
  105. package/src/superlocalmemory/server/rbac_enforce.py +26 -6
  106. package/src/superlocalmemory/server/recall_serializer.py +9 -0
  107. package/src/superlocalmemory/server/routes/behavioral.py +75 -10
  108. package/src/superlocalmemory/server/routes/compliance.py +98 -18
  109. package/src/superlocalmemory/server/routes/config_api.py +186 -4
  110. package/src/superlocalmemory/server/routes/evolution.py +178 -0
  111. package/src/superlocalmemory/server/routes/ingest.py +8 -0
  112. package/src/superlocalmemory/server/routes/learning_telemetry.py +2 -1
  113. package/src/superlocalmemory/server/routes/memories.py +49 -7
  114. package/src/superlocalmemory/server/routes/timeline.py +4 -0
  115. package/src/superlocalmemory/server/routes/v3_api.py +191 -15
  116. package/src/superlocalmemory/server/ui.py +20 -4
  117. package/src/superlocalmemory/server/unified_daemon.py +186 -5
  118. package/src/superlocalmemory/storage/_migration_internals.py +31 -0
  119. package/src/superlocalmemory/storage/_schema_version.py +24 -3
  120. package/src/superlocalmemory/storage/database.py +477 -59
  121. package/src/superlocalmemory/storage/embedding_codec.py +71 -0
  122. package/src/superlocalmemory/storage/lineage_retention.py +236 -0
  123. package/src/superlocalmemory/storage/logical_edges.py +43 -2
  124. package/src/superlocalmemory/storage/migration_runner.py +119 -0
  125. package/src/superlocalmemory/storage/migrations/M044_play_carries_its_own_evidence.py +127 -0
  126. package/src/superlocalmemory/storage/migrations/M045_fact_outcome_score.py +158 -0
  127. package/src/superlocalmemory/storage/migrations/M046_prospective_memory_has_its_own_name.py +620 -0
  128. package/src/superlocalmemory/storage/migrations/M047_fisher_vectors_are_stored_like_every_other_vector.py +306 -0
  129. package/src/superlocalmemory/storage/migrations/M048_upcoming_holds_only_what_is_upcoming.py +207 -0
  130. package/src/superlocalmemory/storage/migrations/M049_a_schema_version_marker_is_one_row.py +201 -0
  131. package/src/superlocalmemory/storage/migrations.py +18 -2
  132. package/src/superlocalmemory/storage/models.py +40 -1
  133. package/src/superlocalmemory/storage/projection_outbox.py +346 -0
  134. package/src/superlocalmemory/storage/retention_policy.py +860 -0
  135. package/src/superlocalmemory/storage/schema.py +12 -1
  136. package/src/superlocalmemory/storage/write_coordinator.py +19 -2
  137. package/src/superlocalmemory/trust/scorer.py +43 -1
  138. package/src/superlocalmemory/ui/index.html +9 -18
  139. package/src/superlocalmemory/ui/js/event-delegation.js +12 -1
  140. package/src/superlocalmemory/ui/js/od-health.js +28 -6
  141. package/src/superlocalmemory/ui/js/od-memories.js +19 -0
  142. package/src/superlocalmemory/ui/js/od-settings.js +87 -1
  143. package/src/superlocalmemory/ui/js/recall-lab.js +78 -3
@@ -21,6 +21,27 @@ from pathlib import Path
21
21
  logger = logging.getLogger(__name__)
22
22
 
23
23
 
24
+ def _cli_projection_queue_depth(db_path: object) -> int:
25
+ """Facts queued for the graph and vector projections, read straight from disk.
26
+
27
+ The no-daemon branch of ``slm status`` has no engine, so it opens the store
28
+ read-only rather than building one. A store that predates the queue reports
29
+ zero, which is the truth for it.
30
+ """
31
+ import sqlite3
32
+
33
+ from superlocalmemory.core.status_contract import projection_queue_depth
34
+
35
+ try:
36
+ conn = sqlite3.connect(f"file:{db_path}?mode=ro", uri=True)
37
+ except sqlite3.Error:
38
+ return 0
39
+ try:
40
+ return projection_queue_depth(conn)
41
+ finally:
42
+ conn.close()
43
+
44
+
24
45
  def _daemon_unavailable(command: str, use_json: bool) -> None:
25
46
  """Exit a mutation client without opening a process-local writer.
26
47
 
@@ -70,14 +91,127 @@ def _cmd_db_dispatch(args: Namespace) -> None:
70
91
  if sub == "reembed":
71
92
  _cmd_db_reembed(args)
72
93
  return
94
+ if sub == "regraph":
95
+ _cmd_db_regraph(args)
96
+ return
73
97
  print(
74
98
  "Usage: slm db migrate [--status] [--dry-run] "
75
99
  "| slm db scale <action> "
100
+ "| slm db regraph [--check] [--profile NAME] "
76
101
  "| slm db reembed [--missing-only] [--all-profiles] [--limit N]"
77
102
  )
78
103
  sys.exit(2)
79
104
 
80
105
 
106
+ def _cmd_db_regraph(args: Namespace) -> None:
107
+ """Re-derive the graph copy of your memories from the store.
108
+
109
+ Your memories live in one place; a second copy of the connections between
110
+ them is kept in a graph store so that searching them is fast. The two are
111
+ kept in step by a queue. If the queue is empty and the two still disagree
112
+ -- after a crash, a disk problem, or a copy restored from elsewhere --
113
+ nothing notices, because "the queue is empty" is what "in step" looks like.
114
+
115
+ This is how you say so out loud. ``--check`` reports the difference and
116
+ changes nothing; without it, every memory is queued to be re-derived and
117
+ the background worker rebuilds the copy from the store, which is the one
118
+ that is authoritative.
119
+ """
120
+ from superlocalmemory.core.config import SLMConfig
121
+ from superlocalmemory.storage import projection_outbox
122
+ from superlocalmemory.storage.database import DatabaseManager
123
+ from superlocalmemory.storage.logical_edges import count_logical_edges
124
+
125
+ config = SLMConfig.load()
126
+ db = DatabaseManager(config.db_path)
127
+ wanted = str(getattr(args, "profile", "") or "").strip()
128
+ try:
129
+ rows = db.execute(
130
+ "SELECT DISTINCT profile_id FROM atomic_facts ORDER BY profile_id"
131
+ )
132
+ profiles = [dict(r)["profile_id"] for r in rows]
133
+ except Exception as exc: # noqa: BLE001
134
+ print(f"[slm] could not list workspaces: {exc}")
135
+ sys.exit(1)
136
+ if wanted:
137
+ profiles = [p for p in profiles if p == wanted]
138
+ if not profiles:
139
+ print(f"[slm] no workspace named {wanted!r}")
140
+ sys.exit(1)
141
+
142
+ backend = None
143
+ try:
144
+ from superlocalmemory.core.backend_orchestrator import get_orchestrator
145
+
146
+ orchestrator = get_orchestrator()
147
+ backend = orchestrator.get_graph_backend() if orchestrator else None
148
+ except Exception as exc: # noqa: BLE001
149
+ print(f"[slm] no graph copy is configured on this store ({exc}).")
150
+ return
151
+ if backend is None:
152
+ print("[slm] no graph copy is configured on this store; nothing to do.")
153
+ return
154
+
155
+ total_queued = 0
156
+ for profile_id in profiles:
157
+ with db.raw_connection() as conn:
158
+ stored = count_logical_edges(conn, profile_id)
159
+ try:
160
+ result = backend._db.run(
161
+ "?[count(a)] := *edge{from_id: a, profile_id: $pid}",
162
+ {"pid": profile_id},
163
+ )
164
+ copied = int(result.values.tolist()[0][0]) if len(result) else 0
165
+ except Exception as exc: # noqa: BLE001
166
+ print(f" {profile_id}: the graph copy could not be read ({exc})")
167
+ continue
168
+ drift = abs(copied - stored) / float(stored) if stored else 0.0
169
+ pending = 0
170
+ try:
171
+ pending_rows = db.execute(
172
+ "SELECT COUNT(*) AS c FROM projection_outbox WHERE profile_id = ?",
173
+ (profile_id,),
174
+ )
175
+ pending = int(dict(pending_rows[0])["c"]) if pending_rows else 0
176
+ except Exception: # noqa: BLE001
177
+ pending = -1
178
+ print(
179
+ f" {profile_id}: {stored} connections stored, {copied} in the "
180
+ f"graph copy ({drift:.1%} apart), {pending} waiting to be applied"
181
+ )
182
+ if getattr(args, "check", False):
183
+ continue
184
+
185
+ fact_rows = db.execute(
186
+ "SELECT fact_id FROM atomic_facts WHERE profile_id = ?", (profile_id,)
187
+ )
188
+ fact_ids = [dict(r)["fact_id"] for r in fact_rows]
189
+ if not fact_ids:
190
+ continue
191
+ if not projection_outbox.is_available(db):
192
+ print(" (this store has no queue to rebuild through)")
193
+ continue
194
+ for start in range(0, len(fact_ids), 500):
195
+ projection_outbox.enqueue_many(
196
+ db, fact_ids[start:start + 500], profile_id,
197
+ op=projection_outbox.OP_UPSERT,
198
+ )
199
+ total_queued += len(fact_ids)
200
+ print(f" {profile_id}: queued {len(fact_ids)} memories to re-derive")
201
+
202
+ db.close()
203
+ if getattr(args, "check", False):
204
+ return
205
+ if total_queued:
206
+ print(
207
+ f"[slm] {total_queued} memories queued. The background worker "
208
+ f"rebuilds the copy from the store; watch `slm status` until the "
209
+ f"waiting count reaches zero."
210
+ )
211
+ else:
212
+ print("[slm] nothing to re-derive.")
213
+
214
+
81
215
  def _cmd_db_reembed(args: Namespace) -> None:
82
216
  """Backfill NULL embeddings on atomic_facts.
83
217
 
@@ -424,7 +558,21 @@ def dispatch(args: Namespace) -> None:
424
558
  }
425
559
  handler = handlers.get(args.command)
426
560
  if handler:
427
- handler(args)
561
+ from superlocalmemory.cli.daemon import DaemonRefused
562
+
563
+ try:
564
+ handler(args)
565
+ except DaemonRefused as refusal:
566
+ # One place, because a command added next year will not remember to
567
+ # do this. A refusal is an answer: say so and stop, rather than
568
+ # printing a traceback or -- worse -- letting a command fall back to
569
+ # writing locally what the workspace has just declined.
570
+ print(
571
+ f"[slm] {refusal}. This workspace requires authentication; "
572
+ "set SLM_USER_SESSION or log in, then try again.",
573
+ flush=True,
574
+ )
575
+ sys.exit(1)
428
576
  else:
429
577
  print(f"Unknown command: {args.command}")
430
578
  sys.exit(1)
@@ -821,7 +969,13 @@ def cmd_config(args: Namespace) -> None:
821
969
  "evolution.mutation_model", "evolution.verify_model",
822
970
  "evolution.confirm_model",
823
971
  "mesh_enabled", "daemon_idle_timeout", "entity_compilation_enabled",
824
- "graph_backend", "vector_backend", "scale_engine_state",
972
+ "graph_backend", "vector_backend",
973
+ # scale_engine_state is deliberately NOT settable. It is a record of
974
+ # what has been done to the store, not a preference: writing
975
+ # "promoted" by hand makes the daemon skip projecting a store that
976
+ # was never projected, and writing "local_core" onto a promoted one
977
+ # makes it project again over live backends. A real installation was
978
+ # found declaring two backends it had never built.
825
979
  "scope.default_scope", "scope.recall_include_global",
826
980
  "scope.recall_include_shared",
827
981
  }
@@ -1339,7 +1493,7 @@ def cmd_migrate(args: Namespace) -> None:
1339
1493
 
1340
1494
  def cmd_list(args: Namespace) -> None:
1341
1495
  """List recent memories chronologically."""
1342
- from superlocalmemory.core.config import SLMConfig
1496
+ from superlocalmemory.core.config import CANONICAL_LIST_LIMIT, SLMConfig
1343
1497
  from superlocalmemory.core.engine import MemoryEngine
1344
1498
 
1345
1499
  use_json = getattr(args, 'json', False)
@@ -1348,10 +1502,10 @@ def cmd_list(args: Namespace) -> None:
1348
1502
  engine = MemoryEngine(config)
1349
1503
  engine.initialize()
1350
1504
 
1351
- limit = getattr(args, "limit", 20)
1352
- facts = engine._db.get_all_facts(engine.profile_id)
1353
- facts.sort(key=lambda f: f.created_at or "", reverse=True)
1354
- facts = facts[:limit]
1505
+ limit = getattr(args, "limit", CANONICAL_LIST_LIMIT)
1506
+ # The query already returns newest-first; pushing the bound into SQL
1507
+ # keeps this from deserializing the whole table to show twenty rows.
1508
+ facts = engine._db.get_all_facts(engine.profile_id, limit=limit)
1355
1509
  except Exception as exc:
1356
1510
  if use_json:
1357
1511
  from superlocalmemory.cli.json_output import json_print
@@ -1879,6 +2033,9 @@ def cmd_review_correction(args: Namespace) -> None:
1879
2033
  def cmd_status(args: Namespace) -> None:
1880
2034
  """Show system status."""
1881
2035
  from superlocalmemory.core.config import SLMConfig
2036
+ # Same source the other two status surfaces read, so all three cannot
2037
+ # disagree about which version answered.
2038
+ from superlocalmemory.server.routes.helpers import SLM_VERSION
1882
2039
 
1883
2040
  config = SLMConfig.load()
1884
2041
  daemon_status = None
@@ -1903,7 +2060,10 @@ def cmd_status(args: Namespace) -> None:
1903
2060
 
1904
2061
  if daemon_status is not None:
1905
2062
  data = {
1906
- "mode": str(daemon_status.get("mode", "unknown")).upper(),
2063
+ # Lowercase is what the daemon, the HTTP surface and MCP all
2064
+ # report. Uppercasing it here made one field disagree across
2065
+ # surfaces for anyone comparing the two answers.
2066
+ "mode": str(daemon_status.get("mode", "unknown")),
1907
2067
  "provider": daemon_status.get("provider", "none"),
1908
2068
  "profile": daemon_status["profile"],
1909
2069
  "base_dir": daemon_status.get("base_dir", str(config.base_dir)),
@@ -1915,6 +2075,10 @@ def cmd_status(args: Namespace) -> None:
1915
2075
  "profile_generation": int(
1916
2076
  daemon_status.get("profile_generation", 0)
1917
2077
  ),
2078
+ "version": SLM_VERSION,
2079
+ "projection_queue_depth": int(
2080
+ daemon_status.get("projection_queue_depth", 0)
2081
+ ),
1918
2082
  }
1919
2083
  json_print("status", data=data, next_actions=[
1920
2084
  {"command": "slm health --json", "description": "Check math layer health"},
@@ -1965,7 +2129,7 @@ def cmd_status(args: Namespace) -> None:
1965
2129
  pass
1966
2130
 
1967
2131
  data = {
1968
- "mode": config.mode.value.upper(),
2132
+ "mode": config.mode.value,
1969
2133
  "provider": config.llm.provider or "none",
1970
2134
  "profile": config.active_profile,
1971
2135
  "base_dir": str(config.base_dir),
@@ -1975,6 +2139,8 @@ def cmd_status(args: Namespace) -> None:
1975
2139
  "entity_count": entity_count,
1976
2140
  "edge_count": edge_count,
1977
2141
  "profile_generation": 0,
2142
+ "version": SLM_VERSION,
2143
+ "projection_queue_depth": _cli_projection_queue_depth(config.db_path),
1978
2144
  }
1979
2145
  json_print("status", data=data, next_actions=[
1980
2146
  {"command": "slm health --json", "description": "Check math layer health"},
@@ -2068,7 +2234,7 @@ def cmd_health(args: Namespace) -> None:
2068
2234
  "total_facts": len(facts),
2069
2235
  "similarity_indexed": fisher_count,
2070
2236
  "lifecycle_positioned": langevin_count,
2071
- "mode": config.mode.value.upper(),
2237
+ "mode": config.mode.value,
2072
2238
  }, next_actions=[
2073
2239
  {"command": "slm status --json", "description": "Check system status"},
2074
2240
  {"command": "slm recall '<query>' --json", "description": "Test retrieval"},
@@ -2567,14 +2733,22 @@ def cmd_doctor(args: Namespace) -> None:
2567
2733
  "pip install " + " ".join(core_modules[m] for m in missing))
2568
2734
 
2569
2735
  # 3. Search deps
2736
+ #
2737
+ # Presence only. Importing these executes torch, which cost ~4 s of every
2738
+ # doctor run — a quarter of the whole command — to learn something the
2739
+ # embedding-worker check below already proves by actually running them.
2740
+ # find_spec answers "is it installed" without executing a line of it.
2741
+ # A package that is present but broken on import is caught by check 7,
2742
+ # which spawns the worker and waits for it to answer.
2570
2743
  search_mods = {"sentence_transformers": "sentence-transformers", "torch": "torch",
2571
2744
  "sklearn": "scikit-learn"}
2572
2745
  search_ok = []
2573
2746
  for mod, pkg in search_mods.items():
2574
2747
  try:
2575
- __import__(mod)
2576
- search_ok.append(mod)
2577
- except Exception: # dependency import may fail after module discovery
2748
+ import importlib.util as _ilu
2749
+ if _ilu.find_spec(mod) is not None:
2750
+ search_ok.append(mod)
2751
+ except Exception: # a broken meta-path finder must not fail the check
2578
2752
  pass
2579
2753
  if len(search_ok) == len(search_mods):
2580
2754
  _check("Search deps", "PASS", "sentence-transformers, torch, sklearn")
@@ -2761,19 +2935,74 @@ def cmd_doctor(args: Namespace) -> None:
2761
2935
  try:
2762
2936
  from superlocalmemory.storage.memory_write import memory_read
2763
2937
 
2938
+ # integrity_check reads every page. On a 610 MB store that is
2939
+ # 10-15 s — over half of this command — and it ran on every
2940
+ # invocation, including the ones a user makes twice in a row while
2941
+ # fixing something else. quick_check walks the same B-trees and
2942
+ # catches structural damage; what it skips is page-level checksum
2943
+ # work that only finds hardware bit rot, which surfaces as read
2944
+ # errors long before anyone runs a doctor.
2945
+ #
2946
+ # The trade is named rather than hidden: the output says which
2947
+ # check ran, and --deep runs the exhaustive one.
2948
+ deep = bool(getattr(args, "deep", False))
2949
+ pragma = "integrity_check" if deep else "quick_check"
2764
2950
  with memory_read(db_path) as conn:
2765
- result = conn.execute("PRAGMA integrity_check").fetchone()
2951
+ result = conn.execute(f"PRAGMA {pragma}").fetchone()
2766
2952
  if result and result[0] == "ok":
2767
2953
  size_mb = db_path.stat().st_size / (1024 * 1024)
2768
- _check("Database", "PASS", f"OK ({size_mb:.2f} MB)")
2954
+ detail = f"OK ({size_mb:.2f} MB, {pragma})"
2955
+ if not deep:
2956
+ detail += " — run `slm doctor --deep` for a full page scan"
2957
+ _check("Database", "PASS", detail)
2769
2958
  else:
2770
- _check("Database", "FAIL", f"integrity check: {result}",
2959
+ _check("Database", "FAIL", f"{pragma}: {result}",
2771
2960
  "Backup and recreate database")
2772
2961
  except Exception as exc:
2773
2962
  _check("Database", "FAIL", str(exc))
2774
2963
  else:
2775
2964
  _check("Database", "PASS", "not yet created (will initialize on first use)")
2776
2965
 
2966
+ # 10b. Projection queue. The graph and the vectors live in other storage
2967
+ # engines, and a queue that stops draining is the one failure that produces
2968
+ # no error at all: the memory is safely in SQLite, nothing raises, and
2969
+ # recall quietly stops finding it. Depth alone cannot distinguish a busy
2970
+ # queue from a wedged one, so the check keys on attempts — a row that has
2971
+ # been tried and refused is a defect with a fact id attached.
2972
+ if db_path.exists():
2973
+ try:
2974
+ from superlocalmemory.storage.memory_write import memory_read
2975
+ from superlocalmemory.storage.projection_outbox import DEPTH_SQL
2976
+
2977
+ with memory_read(db_path) as conn:
2978
+ depth = int(conn.execute(DEPTH_SQL).fetchone()[0])
2979
+ stalled = [
2980
+ row[0] for row in conn.execute(
2981
+ "SELECT fact_id FROM projection_outbox "
2982
+ "WHERE attempts >= 3 ORDER BY attempts DESC LIMIT 5"
2983
+ )
2984
+ ]
2985
+ if stalled:
2986
+ _check(
2987
+ "Projection queue", "FAIL",
2988
+ f"{len(stalled)}+ memories refused by the graph or vector "
2989
+ f"store (e.g. {', '.join(f[:12] for f in stalled)})",
2990
+ "Check `slm logs` for the projection error, then restart "
2991
+ "the daemon to retry",
2992
+ )
2993
+ elif depth:
2994
+ _check(
2995
+ "Projection queue", "PASS",
2996
+ f"{depth} memory/memories queued — the worker drains these "
2997
+ "in the background",
2998
+ )
2999
+ else:
3000
+ _check("Projection queue", "PASS", "empty (graph is up to date)")
3001
+ except Exception:
3002
+ # No table means a store that predates the queue. Nothing is
3003
+ # pending on it by definition, so this is not a finding.
3004
+ _check("Projection queue", "PASS", "not applicable to this store")
3005
+
2777
3006
  # 11. PEP 668 advisory: detect EXTERNALLY-MANAGED marker and
2778
3007
  # recommend pipx when the system Python is managed by the OS package
2779
3008
  # manager (e.g. Homebrew, Debian/Ubuntu, Fedora 38+).
@@ -3387,7 +3616,12 @@ def cmd_profile(args: Namespace) -> None:
3387
3616
  if action in ("switch", "create"):
3388
3617
  from superlocalmemory.core.admission import gate_cli_mutation
3389
3618
  from superlocalmemory.core.operation_request import OperationKind
3390
- gate_cli_mutation(OperationKind.PROFILE_SWITCH)
3619
+ # The role that decides this is the one held on the workspace being
3620
+ # switched to or created, not on whichever one happens to be active.
3621
+ gate_cli_mutation(
3622
+ OperationKind.PROFILE_SWITCH,
3623
+ profile=str(getattr(args, "name", "") or ""),
3624
+ )
3391
3625
 
3392
3626
  from superlocalmemory.core.config import SLMConfig
3393
3627
  from superlocalmemory.storage.database import DatabaseManager
@@ -3901,7 +4135,7 @@ def cmd_session_context(args: Namespace) -> None:
3901
4135
  json_print("session-context", data={
3902
4136
  "context": context,
3903
4137
  "memory_count": len(inj_mems),
3904
- "mode": config.mode.value.upper(),
4138
+ "mode": config.mode.value,
3905
4139
  }, next_actions=[
3906
4140
  {"command": "slm recall --json <query>", "description": "Search memories"},
3907
4141
  ])
@@ -3989,6 +4223,7 @@ def cmd_observe(args: Namespace) -> None:
3989
4223
 
3990
4224
  # V3.3.28: Route through daemon (singleton engine, single embedding worker).
3991
4225
  # This is the P0 fix for the memory blast incident of April 7, 2026.
4226
+ from superlocalmemory.cli.daemon import DaemonRefused
3992
4227
  try:
3993
4228
  from superlocalmemory.cli.daemon import is_daemon_running, daemon_request, ensure_daemon
3994
4229
  if is_daemon_running() or ensure_daemon():
@@ -4002,6 +4237,16 @@ def cmd_observe(args: Namespace) -> None:
4002
4237
  reason = result.get("reason", "no patterns matched")
4003
4238
  print(f"Not captured: {reason}")
4004
4239
  return
4240
+ except DaemonRefused as refusal:
4241
+ # The workspace declined this write. The fallback below exists for a
4242
+ # daemon that is not running -- not for one that answered no.
4243
+ print(
4244
+ f"[slm] Not captured: {refusal}. "
4245
+ "This workspace requires authentication; set SLM_USER_SESSION or "
4246
+ "log in, then try again.",
4247
+ flush=True,
4248
+ )
4249
+ sys.exit(1)
4005
4250
  except Exception:
4006
4251
  pass # Fall through to direct engine
4007
4252
 
@@ -322,6 +322,25 @@ def _get_port() -> int:
322
322
  return _DEFAULT_PORT
323
323
 
324
324
 
325
+ class DaemonRefused(RuntimeError):
326
+ """The daemon answered, and the answer was no.
327
+
328
+ Raised for HTTP 401 and 403 only. Distinct from ``daemon_request``
329
+ returning ``None``, which means the daemon could not be reached or did not
330
+ answer usefully. Callers that fall back to a direct engine write MUST let
331
+ this propagate or exit on it: falling back after a refusal performs, as the
332
+ machine owner, exactly the write the workspace just declined.
333
+ """
334
+
335
+ def __init__(self, status: int, path: str = "") -> None:
336
+ self.status = int(status)
337
+ self.path = path
338
+ super().__init__(
339
+ f"the daemon refused this request (HTTP {status})"
340
+ + (f" for {path}" if path else "")
341
+ )
342
+
343
+
325
344
  def daemon_request(
326
345
  method: str,
327
346
  path: str,
@@ -376,6 +395,7 @@ def daemon_request(
376
395
  return {key: value for key, value in legacy.items() if key != "_legacy_port"}
377
396
  port = int(legacy["_legacy_port"])
378
397
  try:
398
+ import urllib.error
379
399
  import urllib.request
380
400
  url = f"http://127.0.0.1:{port}{path}"
381
401
  data = json.dumps(body).encode() if body else None
@@ -393,6 +413,16 @@ def daemon_request(
393
413
  req = urllib.request.Request(url, data=data, headers=headers, method=method)
394
414
  resp = urllib.request.urlopen(req, timeout=timeout_seconds)
395
415
  return json.loads(resp.read().decode())
416
+ except urllib.error.HTTPError as exc:
417
+ # A refusal is an answer, not a failure to get one. Returning None here
418
+ # made "you are not allowed to do this" indistinguishable from "the
419
+ # daemon is not running", and every caller that falls back to a local
420
+ # engine write treated the first as the second -- so a workspace that
421
+ # required a login refused the write over HTTP and then performed it
422
+ # locally as the machine owner.
423
+ if exc.code in (401, 403):
424
+ raise DaemonRefused(exc.code, path) from exc
425
+ return None
396
426
  except Exception:
397
427
  return None
398
428
 
@@ -35,6 +35,59 @@ def _resolve_paths(args: Namespace) -> tuple[Path, Path]:
35
35
  return Path(learning), Path(memory)
36
36
 
37
37
 
38
+ def _end_state_disagreements(learning_db, memory_db) -> dict[str, str]:
39
+ """Migrations recorded complete whose own verification no longer passes.
40
+
41
+ ``--status`` reads ``migration_log``, which records what was *done*. The
42
+ daemon re-runs each completed migration's ``verify()`` on every start, which
43
+ checks whether what was done still *holds*. When those two answers differ
44
+ the log says ``complete`` and the health endpoint says the migration failed,
45
+ and until now nothing on any surface showed the two were even asking
46
+ different questions -- so the only available reading was that one of them
47
+ was lying. Reported as #125.
48
+
49
+ Read-only and fail-quiet: this is a diagnostic printed beside a status line,
50
+ and it must never be the reason a status command exits non-zero.
51
+ """
52
+ import sqlite3
53
+
54
+ from superlocalmemory.storage._migration_internals import _MODULES
55
+ from superlocalmemory.storage.migration_runner import MIGRATIONS
56
+
57
+ try:
58
+ from superlocalmemory.storage.migration_runner import DEFERRED_MIGRATIONS
59
+ except ImportError: # pragma: no cover — older layouts
60
+ DEFERRED_MIGRATIONS = ()
61
+
62
+ out: dict[str, str] = {}
63
+ for migration in list(MIGRATIONS) + list(DEFERRED_MIGRATIONS):
64
+ verify_fn = getattr(_MODULES.get(migration.name), "verify", None)
65
+ if not callable(verify_fn):
66
+ continue
67
+ db_path = memory_db if migration.db_target != "learning" else learning_db
68
+ try:
69
+ conn = sqlite3.connect(f"file:{db_path}?mode=ro", uri=True)
70
+ except sqlite3.Error:
71
+ continue
72
+ try:
73
+ row = conn.execute(
74
+ "SELECT status FROM migration_log WHERE name = ? LIMIT 1",
75
+ (migration.name,),
76
+ ).fetchone()
77
+ if not row or row[0] != "complete":
78
+ continue
79
+ if not bool(verify_fn(conn)):
80
+ out[migration.name] = " <- recorded complete, end-state no longer holds"
81
+ except Exception: # noqa: BLE001 — a diagnostic must not break status
82
+ pass
83
+ finally:
84
+ try:
85
+ conn.close()
86
+ except sqlite3.Error: # pragma: no cover
87
+ pass
88
+ return out
89
+
90
+
38
91
  def cmd_db_migrate(args: Namespace) -> int:
39
92
  """Apply pending migrations or report status.
40
93
 
@@ -57,8 +110,25 @@ def cmd_db_migrate(args: Namespace) -> int:
57
110
  if not report:
58
111
  print("(no migrations registered)")
59
112
  else:
113
+ disagreements = _end_state_disagreements(learning_db, memory_db)
60
114
  for name, state in report.items():
61
- print(f" {name}: {state}")
115
+ note = disagreements.get(name, "")
116
+ print(f" {name}: {state}{note}")
117
+ if disagreements:
118
+ print()
119
+ print(
120
+ " A migration marked complete is re-checked on every start "
121
+ "by its own\n"
122
+ " verification. The ones flagged above are recorded as done "
123
+ "and their\n"
124
+ " end-state no longer holds, which is what the daemon "
125
+ "reports as a\n"
126
+ " migration failure while this log still reads complete. "
127
+ "Run `slm db\n"
128
+ " migrate` to let each one try to repair itself, and see "
129
+ "`migration_failure_reasons`\n"
130
+ " in `slm health --json` for what specifically did not hold."
131
+ )
62
132
  return 0
63
133
 
64
134
  dry_run = bool(getattr(args, "dry_run", False))
@@ -427,9 +427,14 @@ def _cmd_gdpr_erase(args: Namespace) -> None:
427
427
  sys.exit(1)
428
428
 
429
429
  complete = counts.get("erasure_complete", 0)
430
+ # Whether it can be SHOWN to have happened is a separate answer, and
431
+ # printing COMPLETE while the tamper-evident receipt failed to persist is
432
+ # how the command line came to disagree with the API about the same erasure.
433
+ provable = counts.get("erasure_provable", complete)
430
434
  result_data = {
431
435
  "profile": profile,
432
436
  "erasure_complete": bool(complete),
437
+ "erasure_provable": bool(provable),
433
438
  "counts": counts,
434
439
  "note": (
435
440
  "Erasure recorded in audit_chain.db. "
@@ -441,7 +446,15 @@ def _cmd_gdpr_erase(args: Namespace) -> None:
441
446
  if use_json:
442
447
  _print_json(_json_envelope("gdpr-erase", data=result_data))
443
448
  else:
444
- status_str = "COMPLETE" if complete else "INCOMPLETE (check counts)"
449
+ if not complete:
450
+ status_str = "INCOMPLETE (check counts)"
451
+ elif not provable:
452
+ status_str = (
453
+ "COMPLETE, BUT NOT PROVABLE — the data is gone and the "
454
+ "tamper-evident receipt did not persist"
455
+ )
456
+ else:
457
+ status_str = "COMPLETE"
445
458
  print(f"Art.17 erasure for profile '{profile}': {status_str}")
446
459
  for k, v in counts.items():
447
460
  print(f" {k}: {v}")
@@ -449,7 +462,7 @@ def _cmd_gdpr_erase(args: Namespace) -> None:
449
462
  print(" Backups/ contain outstanding obligation — see C1 gap.")
450
463
  print(" Verify receipts: slm gdpr verify --profile", profile)
451
464
 
452
- if not complete:
465
+ if not complete or not provable:
453
466
  sys.exit(1)
454
467
 
455
468
 
@@ -24,7 +24,10 @@ _os.environ.setdefault('TORCH_DEVICE', 'cpu')
24
24
  import argparse
25
25
  import sys
26
26
 
27
- from superlocalmemory.core.config import CANONICAL_RECALL_LIMIT
27
+ from superlocalmemory.core.config import (
28
+ CANONICAL_LIST_LIMIT,
29
+ CANONICAL_RECALL_LIMIT,
30
+ )
28
31
 
29
32
  _HELP_EPILOG = """\
30
33
  operating modes:
@@ -344,6 +347,19 @@ def main() -> None:
344
347
  db_scale_p.add_argument("--stage-id", help="Stage identifier required by verify/promote")
345
348
  db_scale_p.add_argument("--backup-id", help="Backup identifier required by rollback")
346
349
 
350
+ db_regraph_p = db_sub.add_parser(
351
+ "regraph",
352
+ help="Re-derive the graph copy of your memories from the store",
353
+ )
354
+ db_regraph_p.add_argument(
355
+ "--check", action="store_true",
356
+ help="Report how far the graph copy has drifted; change nothing",
357
+ )
358
+ db_regraph_p.add_argument(
359
+ "--profile", default="",
360
+ help="Workspace to re-derive. Default: all of them",
361
+ )
362
+
347
363
  db_reembed_p = db_sub.add_parser(
348
364
  "reembed",
349
365
  help="Backfill NULL embeddings (facts never embedded)",
@@ -503,7 +519,8 @@ def main() -> None:
503
519
 
504
520
  list_p = sub.add_parser("list", help="List recent memories chronologically (shows IDs for delete/update)")
505
521
  list_p.add_argument(
506
- "--limit", "-n", type=int, default=20, help="Number of entries (default 20)",
522
+ "--limit", "-n", type=int, default=CANONICAL_LIST_LIMIT,
523
+ help=f"Number of entries (default {CANONICAL_LIST_LIMIT})",
507
524
  )
508
525
  list_p.add_argument("--json", action="store_true", help="Output structured JSON (agent-native)")
509
526
 
@@ -530,6 +547,11 @@ def main() -> None:
530
547
  "--quick", action="store_true",
531
548
  help="Run only the fast checks (deps + config); skip daemon/embedding probes",
532
549
  )
550
+ doctor_p.add_argument(
551
+ "--deep", action="store_true",
552
+ help="Read every database page (PRAGMA integrity_check) instead of the "
553
+ "structural check; slow on a large store",
554
+ )
533
555
  doctor_p.add_argument(
534
556
  "--fix", action="store_true",
535
557
  help="Auto-repair fixable components (re-download missing models, "