superlocalmemory 3.6.4 → 3.6.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,53 @@ All notable changes to SuperLocalMemory V3 will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [3.6.6] - 2026-06-10 — Recall precision & memory hygiene
9
+
10
+ Memory means providing the best data, not the most data. v3.6.6 makes recall
11
+ output disciplined and the write path quality-gated, with full parity across
12
+ MCP, CLI, the daemon HTTP route, the in-process adapter, and the WorkerPool
13
+ fallback (identical output regardless of surface or mode A/B).
14
+
15
+ ### Added
16
+
17
+ - **Evidence floor (recall):** results must earn retrieval evidence — semantic
18
+ cosine ≥ 0.60, or BM25 / entity-graph / temporal signal, or a pinned fact.
19
+ Associative-only channels (spreading-activation, hopfield) no longer fabricate
20
+ matches. A query with no confident match returns an empty result set plus
21
+ `no_confident_match: true` instead of filler. Config:
22
+ `retrieval.evidence_floor_enabled`, `retrieval.min_semantic_evidence`.
23
+ Kill-switch: `SLM_RECALL_NO_FLOOR=1`.
24
+ - **Recall output budget:** per-fact content clamp (default 2,400 chars,
25
+ head+tail preserved) and per-response budget (default 12,000 chars; remaining
26
+ results returned as stubs). New optional `full=true` parameter bypasses
27
+ clamping; clamped results carry `truncated: true`. Config:
28
+ `retrieval.recall_per_fact_max_chars`, `retrieval.recall_total_max_chars`.
29
+ - **source_content discipline:** `source_content` defaults to a ≤280-char
30
+ preview (`include_source=true` restores full); internal prompt-template
31
+ content is never returned.
32
+ - **Ingest gate (remember):** content over 24,000 chars is stored as a
33
+ head+tail-clamped fact while the full original is preserved in the source
34
+ memory record; content over 1MB is rejected. Prompt-template text can no
35
+ longer be stored as a memory. Config: `store.max_verbatim_chars`,
36
+ `store.max_ingest_bytes`. Kill-switch: `SLM_INGEST_NO_GATE=1`.
37
+ - **Core-block hygiene:** core memory blocks now deduplicate lines at compile
38
+ time, drop low-quality/template source facts, enforce a per-block char cap,
39
+ and recompile on the daily maintenance schedule.
40
+
41
+ ### Compatibility
42
+
43
+ - All MCP/HTTP/CLI signatures unchanged; new parameters and response fields are
44
+ additive. No database schema migrations. Every new behavior has a config
45
+ field and an environment kill-switch.
46
+
47
+ ## [3.6.5] - 2026-06-09 — Dependency-check hardening
48
+
49
+ ### Fixed
50
+
51
+ - Dependency version check no longer eager-imports `torch` into every process
52
+ (caused a Python 3.14 test segfault and Apple-Silicon memory blow-up); now
53
+ uses `importlib.metadata.version()`.
54
+
8
55
  ## [3.6.4] - 2026-06-09 — Memory-integrity & reliability hardening
9
56
 
10
57
  ### Fixed
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "superlocalmemory",
3
- "version": "3.6.4",
3
+ "version": "3.6.6",
4
4
  "description": "Information-geometric agent memory with mathematical guarantees. 4-channel retrieval, Fisher-Rao similarity, zero-LLM mode, EU AI Act compliant. Works with Claude, Cursor, Windsurf, and 17+ AI tools.",
5
5
  "keywords": [
6
6
  "ai-memory",
package/pyproject.toml CHANGED
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "superlocalmemory"
3
- version = "3.6.4"
3
+ version = "3.6.6"
4
4
  description = "Information-geometric agent memory with mathematical guarantees"
5
5
  readme = "README.md"
6
6
  license = {text = "AGPL-3.0-or-later"}
@@ -28,7 +28,7 @@ if "OMP_NUM_THREADS" not in os.environ:
28
28
  os.environ["OMP_NUM_THREADS"] = "2"
29
29
  # ---------------------------------------------------------------------------
30
30
 
31
- __version__ = "3.6.3"
31
+ __version__ = "3.6.6"
32
32
 
33
33
  _REQUIRED_VERSIONS = {
34
34
  "sentence_transformers": "5.3.0",
@@ -36,22 +36,35 @@ _REQUIRED_VERSIONS = {
36
36
  }
37
37
 
38
38
 
39
+ # Module name -> distribution name for metadata lookup.
40
+ _DIST_NAMES = {
41
+ "sentence_transformers": "sentence-transformers",
42
+ "onnxruntime": "onnxruntime",
43
+ }
44
+
45
+
39
46
  def _check_critical_deps() -> None:
40
- """Warn if embedding-critical packages have wrong versions."""
47
+ """Warn if embedding-critical packages have wrong versions.
48
+
49
+ Reads installed versions from package metadata — does NOT import the
50
+ packages. Importing sentence_transformers here would eagerly load torch
51
+ (native) into every process: a memory blow-up on Apple Silicon and, on
52
+ some interpreters, a source of native-heap instability at teardown.
53
+ """
41
54
  import warnings
55
+ from importlib import metadata
42
56
  for mod_name, expected in _REQUIRED_VERSIONS.items():
43
57
  try:
44
- mod = __import__(mod_name)
45
- actual = getattr(mod, "__version__", None)
46
- if actual and actual != expected:
47
- warnings.warn(
48
- f"SuperLocalMemory requires {mod_name}=={expected} but "
49
- f"{actual} is installed. This causes memory blow-up on "
50
- f"Apple Silicon. Fix: pip install {mod_name}=={expected}",
51
- stacklevel=2,
52
- )
53
- except ImportError:
54
- pass
58
+ actual = metadata.version(_DIST_NAMES[mod_name])
59
+ except metadata.PackageNotFoundError:
60
+ continue
61
+ if actual != expected:
62
+ warnings.warn(
63
+ f"SuperLocalMemory requires {mod_name}=={expected} but "
64
+ f"{actual} is installed. This causes memory blow-up on "
65
+ f"Apple Silicon. Fix: pip install {mod_name}=={expected}",
66
+ stacklevel=2,
67
+ )
55
68
 
56
69
 
57
70
  # Only run the dep check when a full (non-LIGHT) engine is in use.
@@ -1001,7 +1001,9 @@ def cmd_recall(args: Namespace) -> None:
1001
1001
  ])
1002
1002
  return
1003
1003
  if not result["results"]:
1004
- print("No matching memories found.")
1004
+ print("No confident match."
1005
+ if result.get("no_confident_match")
1006
+ else "No matching memories found.")
1005
1007
  return
1006
1008
  # Text output
1007
1009
  print(f"SpreadingActivation.search completed via daemon ({result.get('retrieval_time_ms', 0):.0f}ms)")
@@ -1030,20 +1032,38 @@ def cmd_recall(args: Namespace) -> None:
1030
1032
  sys.exit(1)
1031
1033
  raise
1032
1034
 
1035
+ # v3.6.6: route the direct-fallback path through the SAME shared
1036
+ # serializer the daemon uses, so CLI-without-daemon output is identical
1037
+ # to CLI/MCP-with-daemon (budget + source discipline + no_confident_match).
1038
+ from superlocalmemory.server.recall_serializer import serialize_recall_response
1039
+ _rc = getattr(config, "retrieval", None)
1040
+ _ser, _no_match = serialize_recall_response(
1041
+ response,
1042
+ limit=args.limit,
1043
+ per_fact_max=getattr(_rc, "recall_per_fact_max_chars", 2400),
1044
+ total_max=getattr(_rc, "recall_total_max_chars", 12000),
1045
+ full=getattr(args, "full", False),
1046
+ )
1047
+
1033
1048
  if use_json:
1034
1049
  from superlocalmemory.cli.json_output import json_print
1035
1050
  items = []
1036
- for r in response.results:
1051
+ for d in _ser:
1037
1052
  item = {
1038
- "fact_id": r.fact.fact_id, "content": r.fact.content,
1039
- "score": round(r.score, 3),
1053
+ "fact_id": d["fact_id"], "content": d["content"],
1054
+ "score": round(d["score"], 3),
1040
1055
  }
1041
- if hasattr(r, "channel_scores") and r.channel_scores:
1042
- item["channel_scores"] = {k: round(v, 3) for k, v in r.channel_scores.items()}
1056
+ if d.get("channel_scores"):
1057
+ item["channel_scores"] = {k: round(v, 3) for k, v in d["channel_scores"].items()}
1058
+ if d.get("truncated"):
1059
+ item["truncated"] = True
1060
+ if d.get("stub"):
1061
+ item["stub"] = True
1043
1062
  items.append(item)
1044
1063
  json_print("recall", data={
1045
1064
  "results": items, "count": len(items),
1046
1065
  "query_type": getattr(response, "query_type", "unknown"),
1066
+ "no_confident_match": _no_match,
1047
1067
  }, next_actions=[
1048
1068
  {"command": "slm list --json", "description": "List recent memories"},
1049
1069
  ])
@@ -1055,11 +1075,11 @@ def cmd_recall(args: Namespace) -> None:
1055
1075
  except Exception:
1056
1076
  pass
1057
1077
 
1058
- if not response.results:
1059
- print("No memories found.")
1078
+ if not _ser:
1079
+ print("No confident match." if _no_match else "No memories found.")
1060
1080
  return
1061
- for i, r in enumerate(response.results, 1):
1062
- print(f" {i}. [{r.score:.2f}] {r.fact.content[:120]}")
1081
+ for i, d in enumerate(_ser, 1):
1082
+ print(f" {i}. [{d['score']:.2f}] {d['content']}")
1063
1083
 
1064
1084
 
1065
1085
  def _cli_record_signals(config, query, results):
@@ -0,0 +1,147 @@
1
+ # Copyright (c) 2026 Varun Pratap Bhardwaj / Qualixar
2
+ # Licensed under AGPL-3.0-or-later - see LICENSE file
3
+ # Part of SuperLocalMemory V3 — Core Block Hygiene (v3.6.6)
4
+
5
+ """Core Memory Block hygiene helpers (v3.6.6 F-5).
6
+
7
+ Three pure functions used by the block compiler:
8
+ - dedupe_block_content: normalized-line dedup within a block
9
+ - filter_low_quality_block_facts: drop is_low_quality facts
10
+ - compile_block_content: full compile pipeline (filter + dedup + cap)
11
+
12
+ Also exports: _recompile_core_blocks — the hook called by MaintenanceScheduler.
13
+
14
+ These are pure functions (no I/O). All I/O lives in the scheduler / consolidation
15
+ engine that calls them.
16
+ """
17
+
18
+ from __future__ import annotations
19
+
20
+ import logging
21
+
22
+ logger = logging.getLogger(__name__)
23
+
24
+ _BLOCK_SEPARATOR = "\n---\n"
25
+ _PLACEHOLDER = "No data available."
26
+
27
+
28
+ # ---------------------------------------------------------------------------
29
+ # dedupe_block_content
30
+ # ---------------------------------------------------------------------------
31
+
32
+ def dedupe_block_content(lines: list[str]) -> list[str]:
33
+ """Remove duplicate and empty lines from a block content line-list.
34
+
35
+ Normalization: lowercased, whitespace-collapsed.
36
+ Order is preserved; only the FIRST occurrence is kept.
37
+
38
+ Returns a new list — never mutates input.
39
+ """
40
+ seen: set[str] = set()
41
+ result: list[str] = []
42
+ for line in lines:
43
+ stripped = line.strip()
44
+ if not stripped:
45
+ continue
46
+ key = " ".join(stripped.lower().split())
47
+ if key in seen:
48
+ continue
49
+ seen.add(key)
50
+ result.append(line)
51
+ return result
52
+
53
+
54
+ # ---------------------------------------------------------------------------
55
+ # filter_low_quality_block_facts
56
+ # ---------------------------------------------------------------------------
57
+
58
+ def filter_low_quality_block_facts(facts: list[dict]) -> list[dict]:
59
+ """Filter fact dicts whose content is low-quality or prompt-template.
60
+
61
+ Delegates to injection.is_low_quality and injection.is_prompt_template.
62
+ Returns a new list — never mutates input.
63
+ """
64
+ try:
65
+ from superlocalmemory.core.injection import is_low_quality, is_prompt_template
66
+ except Exception:
67
+ return list(facts)
68
+
69
+ result: list[dict] = []
70
+ for f in facts:
71
+ content = f.get("content", "") or ""
72
+ if is_low_quality(content):
73
+ continue
74
+ if is_prompt_template(content):
75
+ continue
76
+ result.append(f)
77
+ return result
78
+
79
+
80
+ # ---------------------------------------------------------------------------
81
+ # compile_block_content
82
+ # ---------------------------------------------------------------------------
83
+
84
+ def compile_block_content(
85
+ facts: list[dict],
86
+ max_chars: int = 2000,
87
+ ) -> str:
88
+ """Compile facts into block content with hygiene and char cap.
89
+
90
+ Pipeline:
91
+ 1. filter_low_quality_block_facts
92
+ 2. Extract content lines from each fact (split by newline / separator)
93
+ 3. dedupe_block_content across all lines
94
+ 4. Join with separator, truncate to max_chars
95
+
96
+ Returns a string ≤ max_chars. Returns empty string if all facts filtered.
97
+ """
98
+ clean_facts = filter_low_quality_block_facts(facts)
99
+ if not clean_facts:
100
+ return ""
101
+
102
+ all_lines: list[str] = []
103
+ for f in clean_facts:
104
+ content = (f.get("content") or "").strip()
105
+ if not content:
106
+ continue
107
+ # Split by separator or newline to treat each line independently
108
+ for line in content.replace(_BLOCK_SEPARATOR, "\n").split("\n"):
109
+ all_lines.append(line)
110
+
111
+ deduped = dedupe_block_content(all_lines)
112
+ if not deduped:
113
+ return ""
114
+
115
+ joined = _BLOCK_SEPARATOR.join(deduped)
116
+ return joined[:max_chars]
117
+
118
+
119
+ # ---------------------------------------------------------------------------
120
+ # _recompile_core_blocks — scheduler hook (F-5 daily recompile)
121
+ # ---------------------------------------------------------------------------
122
+
123
+ def _recompile_core_blocks(
124
+ db,
125
+ config,
126
+ profile_id: str,
127
+ ) -> dict:
128
+ """Recompile core memory blocks with hygiene applied.
129
+
130
+ Called by MaintenanceScheduler._run() on the daily cycle.
131
+ Delegates to ConsolidationEngine.compile_core_blocks_mode_a() with
132
+ the hygiene improvements applied at the _facts_to_content step.
133
+
134
+ Returns a dict with stats: {blocks_compiled, profile_id}.
135
+ """
136
+ try:
137
+ from superlocalmemory.core.consolidation_engine import ConsolidationEngine
138
+ engine = ConsolidationEngine(db=db, config=config.consolidation)
139
+ result = engine.compile_core_blocks_mode_a(profile_id)
140
+ logger.info(
141
+ "Daily core-block recompile: profile=%s blocks=%s",
142
+ profile_id, result.get("blocks_compiled", 0),
143
+ )
144
+ return {**result, "profile_id": profile_id}
145
+ except Exception as exc:
146
+ logger.warning("Core-block recompile failed: %s", exc)
147
+ return {"blocks_compiled": 0, "profile_id": profile_id, "error": str(exc)}
@@ -197,6 +197,17 @@ class RetrievalConfig:
197
197
  # Used by s19_runner for ablation experiments. Empty = all channels active.
198
198
  disabled_channels: list[str] = field(default_factory=list)
199
199
 
200
+ # v3.6.6: Evidence floor — gate on per-channel scores, not fused/RRF score.
201
+ # Nonsense queries earn 0.0 on every primary channel; real matches earn
202
+ # semantic >= 0.85 or bm25 > 0. The discriminator is earned channel evidence.
203
+ # Env kill-switch: SLM_RECALL_NO_FLOOR=1 disables without release.
204
+ evidence_floor_enabled: bool = True
205
+ min_semantic_evidence: float = 0.60 # Minimum cosine similarity to keep a result
206
+
207
+ # v3.6.6: Recall output budget — protect consuming agents from 585KB responses.
208
+ recall_per_fact_max_chars: int = 2400 # ~600 tokens; head 70% + tail 30%
209
+ recall_total_max_chars: int = 12000 # ~3K tokens; stubs beyond this
210
+
200
211
 
201
212
  # ---------------------------------------------------------------------------
202
213
  # Math Config
@@ -233,6 +244,30 @@ class MathConfig:
233
244
  # Rate-Distortion (production only, disabled for benchmarks)
234
245
 
235
246
 
247
+ # ---------------------------------------------------------------------------
248
+ # Store Config (v3.6.6)
249
+ # ---------------------------------------------------------------------------
250
+
251
+ @dataclass(frozen=True)
252
+ class StoreConfig:
253
+ """Configuration for the remember/store write path (v3.6.6).
254
+
255
+ Ingest gate: protect the DB from oversized facts and prompt-template
256
+ pollution. Defaults ON; env kill-switch SLM_INGEST_NO_GATE=1.
257
+ """
258
+
259
+ # Max chars for the FACT content stored in atomic_facts.content.
260
+ # Content above this is clamped to head 70% + tail 30% + truncation marker.
261
+ # The FULL original is preserved in the memories table row. Set high (24K
262
+ # ≈ 6K tokens) so only pathological pastes are touched; normal dense
263
+ # session-handoff memories (6-15K chars) are stored 100% intact.
264
+ max_verbatim_chars: int = 24000
265
+
266
+ # Hard upper bound in bytes. Content above this is rejected outright.
267
+ # Nobody's "memory" is a megabyte (MCP: success=False, HTTP: 413).
268
+ max_ingest_bytes: int = 1_048_576 # 1 MB
269
+
270
+
236
271
  # ---------------------------------------------------------------------------
237
272
  # Context Injection (v3.4.65)
238
273
  # ---------------------------------------------------------------------------
@@ -658,6 +693,7 @@ class SLMConfig:
658
693
  default_factory=ParameterizationConfig,
659
694
  )
660
695
  injection: InjectionConfig = field(default_factory=InjectionConfig)
696
+ store: StoreConfig = field(default_factory=StoreConfig)
661
697
  # v3.5.0: scaling backends — "sqlite" / "cozo" / "auto" / "lancedb" / "sqlite-vec" / "auto".
662
698
  graph_backend: str = "auto" # "auto" = cozo if pycozo installed, else sqlite
663
699
  vector_backend: str = "auto" # "auto" = lancedb if installed, else sqlite-vec
@@ -808,22 +808,25 @@ class ConsolidationEngine:
808
808
  def _facts_to_content(
809
809
  self, facts: list[dict], char_limit: int,
810
810
  ) -> str:
811
- """Join fact contents with separators, capped at char_limit."""
812
- parts = [f.get("content", "") for f in facts if f.get("content")]
813
- joined = "\n---\n".join(parts)
814
- return joined[:char_limit] if joined else "No data available."
811
+ """Compile fact contents into a block with hygiene (v3.6.6 F-5).
812
+
813
+ Filters low-quality/template facts, dedupes lines WITHIN the block
814
+ (fixes the "same fixture ×5" core-block bug), caps at char_limit.
815
+ """
816
+ from superlocalmemory.core.block_hygiene import compile_block_content
817
+ compiled = compile_block_content(facts, max_chars=char_limit)
818
+ return compiled if compiled else "No data available."
815
819
 
816
820
  def _rows_to_content(
817
821
  self, rows: list | None, char_limit: int,
818
822
  ) -> str:
819
- """Convert DB rows to content string."""
823
+ """Convert DB rows to a hygienic block content string (v3.6.6 F-5)."""
820
824
  if not rows:
821
825
  return "No data available."
822
- parts = [
823
- dict(r).get("content", "") for r in rows if dict(r).get("content")
824
- ]
825
- joined = "\n---\n".join(parts)
826
- return joined[:char_limit] if joined else "No data available."
826
+ from superlocalmemory.core.block_hygiene import compile_block_content
827
+ facts = [dict(r) for r in rows]
828
+ compiled = compile_block_content(facts, max_chars=char_limit)
829
+ return compiled if compiled else "No data available."
827
830
 
828
831
  def _compile_behavioral_block(
829
832
  self, profile_id: str, char_limit: int,
@@ -430,6 +430,25 @@ class MemoryEngine:
430
430
  return []
431
431
  except Exception:
432
432
  pass
433
+ # v3.6.6 ingest gate: reject 1MB monsters + prompt-template pollution;
434
+ # clamp the searchable FACT copy (head+tail) while the memories row keeps
435
+ # the FULL original. Embedding/BM25 use the clamped copy so a 167KB paste
436
+ # never produces a garbage vector. Env kill-switch: SLM_INGEST_NO_GATE=1.
437
+ fact_text = content
438
+ try:
439
+ from superlocalmemory.core.ingest_gate import apply_ingest_gate
440
+ _sc = getattr(self._config, "store", None)
441
+ gate = apply_ingest_gate(
442
+ content,
443
+ max_verbatim_chars=getattr(_sc, "max_verbatim_chars", 24000),
444
+ max_ingest_bytes=getattr(_sc, "max_ingest_bytes", 1_048_576),
445
+ )
446
+ if gate.rejected:
447
+ logger.debug("store_fast ingest gate rejected: %s", gate.rejection_reason)
448
+ return []
449
+ fact_text = gate.fact_content
450
+ except ImportError:
451
+ pass # gate module missing → store verbatim (never block a write)
433
452
  now = datetime.now(timezone.utc).isoformat()
434
453
  record = MemoryRecord(
435
454
  profile_id=self._profile_id, content=content,
@@ -440,8 +459,8 @@ class MemoryEngine:
440
459
  # the entity_graph channel has something to work with before enrichment.
441
460
  ents = sorted(
442
461
  {m.group(1) for m in _re.finditer(
443
- r"\b([A-Z][a-z]+(?:\s[A-Z][a-z]+){0,3})\b", content)}
444
- | {m.group(1) for m in _re.finditer(r"\b([A-Z]{2,})\b", content)}
462
+ r"\b([A-Z][a-z]+(?:\s[A-Z][a-z]+){0,3})\b", fact_text)}
463
+ | {m.group(1) for m in _re.finditer(r"\b([A-Z]{2,})\b", fact_text)}
445
464
  )
446
465
  # v3.5.5: compute the embedding SYNCHRONOUSLY. A single warm embed is
447
466
  # ~22ms (the 30-180s of full store() was LLM fact-extraction + graph,
@@ -452,14 +471,14 @@ class MemoryEngine:
452
471
  emb = None
453
472
  fmean = fvar = None
454
473
  try:
455
- emb = self._embedder.embed(content) if self._embedder else None
474
+ emb = self._embedder.embed(fact_text) if self._embedder else None
456
475
  if emb:
457
476
  fmean, fvar = self._embedder.compute_fisher_params(emb)
458
477
  except Exception:
459
478
  emb = None
460
479
  fact = AtomicFact(
461
480
  fact_id=_uuid.uuid4().hex[:16], memory_id=record.memory_id,
462
- profile_id=self._profile_id, content=content,
481
+ profile_id=self._profile_id, content=fact_text,
463
482
  fact_type=FactType.EPISODIC, entities=ents,
464
483
  observation_date=now[:10], confidence=0.7, importance=0.5,
465
484
  embedding=emb, fisher_mean=fmean, fisher_variance=fvar,
@@ -477,7 +496,7 @@ class MemoryEngine:
477
496
  try:
478
497
  bm25 = getattr(self._retrieval_engine, "_bm25", None)
479
498
  if bm25:
480
- bm25.add(fact.fact_id, content, self._profile_id)
499
+ bm25.add(fact.fact_id, fact_text, self._profile_id)
481
500
  except Exception:
482
501
  pass
483
502
  return [fact.fact_id]
@@ -0,0 +1,133 @@
1
+ # Copyright (c) 2026 Varun Pratap Bhardwaj / Qualixar
2
+ # Licensed under AGPL-3.0-or-later - see LICENSE file
3
+ # Part of SuperLocalMemory V3 — Ingest Gate (v3.6.6)
4
+
5
+ """Ingest gate for the remember/store write path (v3.6.6 F-4).
6
+
7
+ Responsibilities:
8
+ 1. Hard reject content > 1MB (nobody's memory is a megabyte).
9
+ 2. Clamp content > 24000 chars to head 70% + tail 30% + truncation marker.
10
+ Head+tail (not head-only) preserves OPEN ITEMS that session-close facts put
11
+ at the END. Only pathological pastes (167KB JSON, 50KB logs) are touched;
12
+ normal dense memories (6-15K session handoffs) pass through intact.
13
+ Full original preserved in result.full_content for storage in memories table.
14
+ 3. Prompt-template firewall: reject content matching _PROMPT_TEMPLATE_PATTERNS
15
+ (extends v3.6.4 remember-write-02 quality gate).
16
+
17
+ Kill-switch: SLM_INGEST_NO_GATE=1 bypasses rules 2 and 3 (but NOT the 1MB hard cap).
18
+
19
+ Usage::
20
+ from superlocalmemory.core.ingest_gate import apply_ingest_gate
21
+ result = apply_ingest_gate(content)
22
+ if result.rejected:
23
+ return {"success": False, "error": result.rejection_reason}
24
+ store(result.fact_content, full_content=result.full_content)
25
+ """
26
+
27
+ from __future__ import annotations
28
+
29
+ import os
30
+ from dataclasses import dataclass, field
31
+
32
+ # v3.6.6: clamp only pathological monsters; preserve normal dense memories.
33
+ # 24K chars ≈ 6K tokens — well beyond any legitimate single memory.
34
+ _MAX_VERBATIM_CHARS = 24000
35
+ _HEAD_FRACTION = 0.70 # head 70% + tail 30% so OPEN ITEMS at the end survive
36
+ _MAX_INGEST_BYTES = 1_048_576 # 1MB — hard cap, NOT bypassed by kill-switch
37
+ _TRUNCATION_MARKER = "\n…[content truncated at ingest; full text in source memory]…\n"
38
+
39
+
40
+ @dataclass
41
+ class IngestGateResult:
42
+ """Result of applying the ingest gate to content.
43
+
44
+ fact_content: Content to store in atomic_facts.content (may be truncated).
45
+ full_content: Original unmodified content (for memories table storage).
46
+ rejected: True if content should be rejected outright.
47
+ rejection_reason: Human-readable reason for rejection (when rejected=True).
48
+ truncated: True if fact_content was head-sliced.
49
+ """
50
+ fact_content: str
51
+ full_content: str
52
+ rejected: bool = False
53
+ rejection_reason: str = ""
54
+ truncated: bool = False
55
+
56
+
57
+ def apply_ingest_gate(
58
+ content: str,
59
+ max_verbatim_chars: int = _MAX_VERBATIM_CHARS,
60
+ max_ingest_bytes: int = _MAX_INGEST_BYTES,
61
+ ) -> IngestGateResult:
62
+ """Apply the ingest quality gate to content before storing.
63
+
64
+ Returns IngestGateResult. Callers MUST check result.rejected before
65
+ proceeding with storage.
66
+
67
+ Kill-switch SLM_INGEST_NO_GATE=1 bypasses the verbatim size clamp and
68
+ template firewall but NOT the 1MB hard cap (safety boundary).
69
+ """
70
+ gate_active = os.environ.get("SLM_INGEST_NO_GATE", "0") != "1"
71
+
72
+ # --- Hard cap: 1MB regardless of kill-switch ---
73
+ try:
74
+ byte_len = len(content.encode("utf-8", errors="replace"))
75
+ except Exception:
76
+ byte_len = len(content)
77
+ if byte_len > max_ingest_bytes:
78
+ return IngestGateResult(
79
+ fact_content=content,
80
+ full_content=content,
81
+ rejected=True,
82
+ rejection_reason=(
83
+ f"Content size {byte_len} bytes exceeds maximum "
84
+ f"{max_ingest_bytes} bytes (1MB). "
85
+ "Nobody's memory is a megabyte."
86
+ ),
87
+ )
88
+
89
+ # --- Gate bypassed ---
90
+ if not gate_active:
91
+ return IngestGateResult(
92
+ fact_content=content,
93
+ full_content=content,
94
+ rejected=False,
95
+ truncated=False,
96
+ )
97
+
98
+ # --- Prompt-template firewall ---
99
+ # Extends the v3.6.4 remember-write-02 quality gate.
100
+ try:
101
+ from superlocalmemory.core.injection import is_prompt_template
102
+ if is_prompt_template(content):
103
+ return IngestGateResult(
104
+ fact_content=content,
105
+ full_content=content,
106
+ rejected=True,
107
+ rejection_reason=(
108
+ "Content matches internal prompt-template patterns "
109
+ "(low-quality gate). Prompt machinery must not be stored as memory."
110
+ ),
111
+ )
112
+ except Exception:
113
+ pass # Defensive: gate failure must never block a store
114
+
115
+ # --- Verbatim size clamp (head 70% + tail 30%) ---
116
+ if len(content) > max_verbatim_chars:
117
+ budget = max_verbatim_chars - len(_TRUNCATION_MARKER)
118
+ head_len = int(budget * _HEAD_FRACTION)
119
+ tail_len = budget - head_len
120
+ fact_content = content[:head_len] + _TRUNCATION_MARKER + content[-tail_len:]
121
+ return IngestGateResult(
122
+ fact_content=fact_content,
123
+ full_content=content,
124
+ rejected=False,
125
+ truncated=True,
126
+ )
127
+
128
+ return IngestGateResult(
129
+ fact_content=content,
130
+ full_content=content,
131
+ rejected=False,
132
+ truncated=False,
133
+ )
@@ -35,6 +35,33 @@ _LOW_QUALITY_PATTERNS = (
35
35
  re.compile(r"the first line must be exactly", re.IGNORECASE),
36
36
  )
37
37
 
38
+ # v3.6.6: Prompt-template firewall patterns (F-3, F-4).
39
+ # These patterns identify content that is internal LLM prompt machinery,
40
+ # not genuine user memories. Used at ingest (F-4) and recall serialization (F-3).
41
+ # Stdlib-only (re module) so hooks import chain stays light.
42
+ _PROMPT_TEMPLATE_PATTERNS = (
43
+ re.compile(r"you are summarizing a claude code session", re.IGNORECASE),
44
+ re.compile(r"you are a memory consolidation agent", re.IGNORECASE),
45
+ re.compile(r"apply maximum non-destructive compression", re.IGNORECASE),
46
+ re.compile(r"<task-notification\b", re.IGNORECASE),
47
+ )
48
+
49
+
50
+ def is_prompt_template(content: str) -> bool:
51
+ """True if *content* matches internal prompt-template patterns.
52
+
53
+ Used by:
54
+ - F-3: drop source_content that is prompt machinery
55
+ - F-4: reject template content at ingest (extends v3.6.4 quality gate)
56
+
57
+ Separate from is_low_quality() so the firewall can be applied without
58
+ the full quality-check heuristics (e.g. at raw ingest before any
59
+ other processing).
60
+ """
61
+ if not content:
62
+ return False
63
+ return any(pat.search(content) for pat in _PROMPT_TEMPLATE_PATTERNS)
64
+
38
65
  # Leading category tag like "[active_decisions] " / "[learned_preferences] ".
39
66
  _CATEGORY_TAG_RE = re.compile(r"^\s*\[[a-z0-9_]+\]\s*", re.IGNORECASE)
40
67
 
@@ -124,6 +124,15 @@ class MaintenanceScheduler:
124
124
  except Exception as exc:
125
125
  logger.debug("Pending cleanup skipped: %s", exc)
126
126
 
127
+ # v3.6.6 F-5: Daily core-block recompile with hygiene (dedup + char cap).
128
+ # Ensures blocks stay clean even when purge or new facts arrive between
129
+ # session-init recompiles.
130
+ try:
131
+ from superlocalmemory.core.block_hygiene import _recompile_core_blocks
132
+ _recompile_core_blocks(self._db, self._config, self._profile_id)
133
+ except Exception as exc:
134
+ logger.debug("Core-block recompile skipped: %s", exc)
135
+
127
136
  self._schedule_next()
128
137
 
129
138
  def _sync_cloud_destinations(self, manager: object) -> None:
@@ -324,6 +324,8 @@ def apply_adaptive_ranking(
324
324
  channel_weights=response.channel_weights,
325
325
  total_candidates=response.total_candidates,
326
326
  retrieval_time_ms=response.retrieval_time_ms,
327
+ # v3.6.6: preserve evidence-floor signal across reranking rebuilds.
328
+ no_confident_match=(len(new_results) == 0) and response.no_confident_match,
327
329
  )
328
330
 
329
331
 
@@ -426,6 +428,8 @@ def apply_v2_adaptive_ranking(
426
428
  channel_weights=response.channel_weights,
427
429
  total_candidates=response.total_candidates,
428
430
  retrieval_time_ms=response.retrieval_time_ms,
431
+ # v3.6.6: preserve evidence-floor signal across reranking rebuilds.
432
+ no_confident_match=(len(new_results) == 0) and response.no_confident_match,
429
433
  )
430
434
  except Exception as exc: # pragma: no cover — defensive
431
435
  logger.debug("apply_v2_adaptive_ranking skipped: %s", exc)
@@ -554,6 +558,8 @@ def apply_v2_bandit_ensemble(
554
558
  channel_weights=response.channel_weights,
555
559
  total_candidates=response.total_candidates,
556
560
  retrieval_time_ms=response.retrieval_time_ms,
561
+ # v3.6.6: preserve evidence-floor signal across ensemble rebuilds.
562
+ no_confident_match=(len(final_results) == 0) and response.no_confident_match,
557
563
  )
558
564
  except Exception as exc: # pragma: no cover — defensive top-level
559
565
  logger.debug("apply_v2_bandit_ensemble skipped: %s", exc)
@@ -675,6 +681,9 @@ def run_recall(
675
681
  channel_weights=response.channel_weights,
676
682
  total_candidates=response.total_candidates + len(enhanced_facts),
677
683
  retrieval_time_ms=response.retrieval_time_ms,
684
+ # v3.6.6: agentic round-2 may add facts; recompute flag.
685
+ no_confident_match=(len(enhanced_results[:limit]) == 0)
686
+ and response.no_confident_match,
678
687
  )
679
688
  except Exception as exc:
680
689
  logger.debug("Agentic sufficiency skipped: %s", exc)
@@ -71,26 +71,17 @@ def _handle_recall(
71
71
  memory_ids = list({r.fact.memory_id for r in response.results[:limit] if r.fact.memory_id})
72
72
  memory_map = engine._db.get_memory_content_batch(memory_ids) if memory_ids else {}
73
73
 
74
- results = []
75
- for r in response.results[:limit]:
76
- fact_type = getattr(r.fact, "fact_type", None)
77
- lifecycle = getattr(r.fact, "lifecycle", None)
78
- results.append({
79
- "fact_id": r.fact.fact_id,
80
- "memory_id": r.fact.memory_id,
81
- "content": r.fact.content[:300],
82
- "source_content": memory_map.get(r.fact.memory_id, ""),
83
- "score": round(r.score, 4),
84
- "confidence": round(r.confidence, 4),
85
- "trust_score": round(r.trust_score, 4),
86
- "channel_scores": {
87
- k: round(v, 4) for k, v in (r.channel_scores or {}).items()
88
- },
89
- "fact_type": fact_type.value if fact_type and hasattr(fact_type, "value") else "",
90
- "lifecycle": lifecycle.value if lifecycle and hasattr(lifecycle, "value") else "",
91
- "access_count": getattr(r.fact, "access_count", 0),
92
- "evidence_chain": list(getattr(r, "evidence_chain", []) or []),
93
- })
74
+ # v3.6.6: same shared chokepoint as the daemon HTTP route + CLI fallback,
75
+ # so the MCP WorkerPool subprocess path returns identical budgeted output.
76
+ from superlocalmemory.server.recall_serializer import serialize_recall_response
77
+ _rc = getattr(engine._config, "retrieval", None)
78
+ results, no_confident_match = serialize_recall_response(
79
+ response,
80
+ limit=limit,
81
+ memory_map=memory_map,
82
+ per_fact_max=getattr(_rc, "recall_per_fact_max_chars", 2400),
83
+ total_max=getattr(_rc, "recall_total_max_chars", 12000),
84
+ )
94
85
  return {
95
86
  "ok": True,
96
87
  "query": query,
@@ -102,6 +93,7 @@ def _handle_recall(
102
93
  },
103
94
  "total_candidates": getattr(response, "total_candidates", 0),
104
95
  "results": results,
96
+ "no_confident_match": no_confident_match,
105
97
  }
106
98
 
107
99
 
@@ -236,6 +236,8 @@ def register_core_tools(server, get_engine: Callable) -> None:
236
236
  "query_type": result.get("query_type", "unknown"),
237
237
  "channel_weights": result.get("channel_weights", {}),
238
238
  "retrieval_time_ms": result.get("retrieval_time_ms", 0),
239
+ # v3.6.6: surface evidence-floor signal to MCP clients.
240
+ "no_confident_match": result.get("no_confident_match", False),
239
241
  }
240
242
  return {"success": False, "error": result.get("error", "Recall failed")}
241
243
  except Exception as exc:
@@ -295,15 +295,73 @@ class RetrievalEngine:
295
295
  if len(final_top) > len(top[:effective_limit]):
296
296
  facts = self._load_facts(final_top, profile_id)
297
297
 
298
+ # v3.6.6: Evidence floor — gate on per-channel scores (NOT fused/RRF score).
299
+ # Nonsense queries fuse at 0.75-0.78 because RRF is rank-derived and
300
+ # uncalibrated. The discriminator is EARNED CHANNEL EVIDENCE:
301
+ # semantic >= min_semantic_evidence (0.60) OR bm25 > 0
302
+ # OR entity_graph > 0 OR temporal > 0 OR fact is pinned.
303
+ # spreading_activation and hopfield do NOT count — they are associative
304
+ # amplifiers that fabricated the nonsense results in calibration tests.
305
+ # Kill-switch: SLM_RECALL_NO_FLOOR=1 bypasses the floor.
306
+ import os as _os_floor
307
+ floor_enabled = (
308
+ getattr(self._config, "evidence_floor_enabled", True)
309
+ and _os_floor.environ.get("SLM_RECALL_NO_FLOOR", "0") != "1"
310
+ )
311
+ if floor_enabled:
312
+ min_sem = getattr(self._config, "min_semantic_evidence", 0.60)
313
+ final_top = self._apply_evidence_floor(final_top, facts, min_sem)
314
+ # Trim facts dict to match filtered final_top
315
+ filtered_ids = {fr.fact_id for fr in final_top}
316
+ facts = {fid: f for fid, f in facts.items() if fid in filtered_ids}
317
+
298
318
  # 6. Build response
299
319
  results = self._build_results(final_top, facts, strat)
300
320
  ms = (time.monotonic() - t0) * 1000.0
321
+ no_match = floor_enabled and len(results) == 0
301
322
  return RecallResponse(
302
323
  query=query, mode=mode, results=results,
303
324
  query_type=strat.query_type, channel_weights=strat.weights,
304
325
  total_candidates=total, retrieval_time_ms=ms,
326
+ no_confident_match=no_match,
305
327
  )
306
328
 
329
+ # -- Evidence floor (v3.6.6) -------------------------------------------
330
+
331
+ @staticmethod
332
+ def _apply_evidence_floor(
333
+ final_top: list[FusionResult],
334
+ facts: dict[str, AtomicFact],
335
+ min_semantic: float,
336
+ ) -> list[FusionResult]:
337
+ """Filter results that earned no channel evidence.
338
+
339
+ Keep a result only if it earned:
340
+ - semantic cosine >= min_semantic (default 0.60), OR
341
+ - bm25 > 0, OR entity_graph > 0, OR temporal > 0, OR
342
+ - the underlying fact is pinned.
343
+
344
+ spreading_activation and hopfield do NOT count as primary evidence.
345
+ Empty result after filtering is a success (no_confident_match=True).
346
+ """
347
+ kept: list[FusionResult] = []
348
+ for fr in final_top:
349
+ cs = fr.channel_scores or {}
350
+ # Primary channel evidence check
351
+ if (
352
+ cs.get("semantic", 0.0) >= min_semantic
353
+ or cs.get("bm25", 0.0) > 0.0
354
+ or cs.get("entity_graph", 0.0) > 0.0
355
+ or cs.get("temporal", 0.0) > 0.0
356
+ ):
357
+ kept.append(fr)
358
+ continue
359
+ # Pinned fact bypass — always pass regardless of channel scores
360
+ fact = facts.get(fr.fact_id)
361
+ if fact is not None and getattr(fact, "pinned", False):
362
+ kept.append(fr)
363
+ return kept
364
+
307
365
  # -- Cross-channel intersection boost -----------------------------------
308
366
 
309
367
  @staticmethod
@@ -0,0 +1,225 @@
1
+ # Copyright (c) 2026 Varun Pratap Bhardwaj / Qualixar
2
+ # Licensed under AGPL-3.0-or-later - see LICENSE file
3
+ # Part of SuperLocalMemory V3 — Recall Serializer (v3.6.6)
4
+
5
+ """Recall output budget and source_content discipline helpers (v3.6.6).
6
+
7
+ F-2: Per-fact content clamp + total budget stubs.
8
+ F-3: source_content preview + template firewall.
9
+
10
+ THE single shared serialization chokepoint. Every surface that turns a
11
+ RecallResponse into transport dicts goes through ``serialize_recall_response``
12
+ so MCP, CLI, the daemon HTTP route, the in-process queue adapter, and the
13
+ WorkerPool fallback all return byte-for-byte identical output (parity across
14
+ surfaces AND modes A/B). The evidence floor lives upstream in
15
+ RetrievalEngine.recall (also shared); this layer owns presentation only.
16
+
17
+ Pure functions — no side effects, no DB access. Stdlib-only at import
18
+ (hooks import chain must stay light).
19
+ """
20
+
21
+ from __future__ import annotations
22
+
23
+ import re
24
+ from typing import Any
25
+
26
+
27
+ # ---------------------------------------------------------------------------
28
+ # F-2: Per-fact content clamp
29
+ # ---------------------------------------------------------------------------
30
+
31
+ def clamp_fact_content(
32
+ content: str,
33
+ max_chars: int = 2400,
34
+ ) -> tuple[str, bool]:
35
+ """Clamp a single fact's content to max_chars.
36
+
37
+ Strategy: head 70% + "\\n…[truncated N chars]…\\n" + tail 30%.
38
+ The tail is kept because session-close facts put OPEN ITEMS at the end.
39
+
40
+ Returns:
41
+ (clamped_content, was_truncated)
42
+ """
43
+ if not content or len(content) <= max_chars:
44
+ return content, False
45
+
46
+ head_len = int(max_chars * 0.70)
47
+ tail_len = max_chars - head_len
48
+ dropped = len(content) - max_chars
49
+ marker = f"\n…[truncated {dropped} chars]…\n"
50
+
51
+ result = content[:head_len] + marker + content[-tail_len:]
52
+ return result, True
53
+
54
+
55
+ def apply_recall_budget(
56
+ results: list[dict],
57
+ per_fact_max: int = 2400,
58
+ total_max: int = 12000,
59
+ full: bool = False,
60
+ ) -> list[dict]:
61
+ """Apply per-fact clamp and total budget to a list of result dicts.
62
+
63
+ Args:
64
+ results: List of result dicts (must have at minimum 'fact_id',
65
+ 'score', 'content' keys).
66
+ per_fact_max: Maximum chars for a single fact's content.
67
+ total_max: Maximum total content chars before remaining results
68
+ become stubs.
69
+ full: If True, bypasses all clamping (escape hatch for tools/CLI
70
+ that need full content — additive backward-compat param).
71
+
72
+ Returns:
73
+ New list of result dicts with potentially clamped/stubbed content.
74
+ Mutates nothing — returns new dicts.
75
+ """
76
+ if not results:
77
+ return []
78
+
79
+ if full:
80
+ # full=True: return everything as-is, no clamping, no stubs
81
+ return [dict(r) for r in results]
82
+
83
+ out: list[dict] = []
84
+ cumulative_chars = 0
85
+
86
+ for r in results:
87
+ content = r.get("content", "") or ""
88
+
89
+ # Check if we're already over total budget
90
+ if cumulative_chars >= total_max:
91
+ # Emit stub: fact_id, score, first 120 chars + "…"
92
+ stub_content = content[:120] + ("…" if len(content) > 120 else "")
93
+ stub = {k: v for k, v in r.items() if k not in ("content",)}
94
+ stub["content"] = stub_content
95
+ stub["stub"] = True
96
+ out.append(stub)
97
+ continue
98
+
99
+ # Per-fact clamp
100
+ clamped, was_truncated = clamp_fact_content(content, max_chars=per_fact_max)
101
+ new_r = dict(r)
102
+ new_r["content"] = clamped
103
+ if was_truncated:
104
+ new_r["truncated"] = True
105
+
106
+ cumulative_chars += len(clamped)
107
+ out.append(new_r)
108
+
109
+ return out
110
+
111
+
112
+ # ---------------------------------------------------------------------------
113
+ # F-3: source_content discipline
114
+ # ---------------------------------------------------------------------------
115
+
116
+ def apply_source_content_discipline(
117
+ result: dict,
118
+ include_source: bool = False,
119
+ ) -> dict:
120
+ """Apply source_content discipline to a single result dict.
121
+
122
+ Default behavior:
123
+ - Trim source_content to ≤ 280 chars
124
+ - Drop entirely if it matches prompt-template patterns
125
+
126
+ include_source=True:
127
+ - Returns full source_content (unless it's a template, always dropped)
128
+
129
+ Returns a new dict — never mutates input.
130
+ """
131
+ from superlocalmemory.core.injection import is_prompt_template
132
+
133
+ if "source_content" not in result:
134
+ return dict(result)
135
+
136
+ src = result.get("source_content") or ""
137
+
138
+ # Template firewall: drop regardless of include_source
139
+ if src and is_prompt_template(src):
140
+ new_r = dict(result)
141
+ new_r["source_content"] = ""
142
+ return new_r
143
+
144
+ # Empty source: return unchanged
145
+ if not src:
146
+ return dict(result)
147
+
148
+ if include_source:
149
+ return dict(result)
150
+
151
+ # Default: preview ≤ 280 chars
152
+ new_r = dict(result)
153
+ new_r["source_content"] = src[:280]
154
+ return new_r
155
+
156
+
157
+ # ---------------------------------------------------------------------------
158
+ # THE shared chokepoint: RecallResponse -> transport dicts (all surfaces)
159
+ # ---------------------------------------------------------------------------
160
+
161
+ def serialize_recall_response(
162
+ response: Any,
163
+ *,
164
+ limit: int = 10,
165
+ memory_map: dict[str, str] | None = None,
166
+ per_fact_max: int = 2400,
167
+ total_max: int = 12000,
168
+ full: bool = False,
169
+ include_source: bool = False,
170
+ ) -> tuple[list[dict], bool]:
171
+ """Convert a RecallResponse into budgeted, source-disciplined dicts.
172
+
173
+ This is the ONE function every recall surface calls (daemon HTTP route,
174
+ in-process queue adapter, CLI direct-fallback, WorkerPool). Guarantees
175
+ identical output regardless of surface or mode.
176
+
177
+ Args:
178
+ response: A RecallResponse (engine result objects in .results).
179
+ limit: Max results to serialize.
180
+ memory_map: fact.memory_id -> source memory content (optional).
181
+ per_fact_max: Per-fact content char cap (config-driven).
182
+ total_max: Total content char budget before stubs (config-driven).
183
+ full: Bypass clamping/stubs (additive escape hatch).
184
+ include_source: Return full source_content (else ≤280-char preview).
185
+
186
+ Returns:
187
+ (results, no_confident_match) — results is a list of dicts; the bool
188
+ is the evidence-floor signal lifted from the response (additive).
189
+ """
190
+ memory_map = memory_map or {}
191
+ raw: list[dict] = []
192
+ for r in (response.results or [])[:limit]:
193
+ fact = r.fact
194
+ fact_type = getattr(fact, "fact_type", None)
195
+ lifecycle = getattr(fact, "lifecycle", None)
196
+ raw.append({
197
+ "fact_id": fact.fact_id,
198
+ "memory_id": fact.memory_id,
199
+ "content": fact.content or "",
200
+ "source_content": memory_map.get(fact.memory_id, "") or "",
201
+ "score": round(r.score, 4),
202
+ "confidence": round(getattr(r, "confidence", 0.0), 4),
203
+ "trust_score": round(getattr(r, "trust_score", 0.0), 4),
204
+ "channel_scores": {
205
+ k: round(v, 4) for k, v in (getattr(r, "channel_scores", None) or {}).items()
206
+ },
207
+ "fact_type": fact_type.value
208
+ if fact_type is not None and hasattr(fact_type, "value")
209
+ else (getattr(fact, "fact_type", "") or ""),
210
+ "lifecycle": lifecycle.value
211
+ if lifecycle is not None and hasattr(lifecycle, "value")
212
+ else (lifecycle or ""),
213
+ "access_count": getattr(fact, "access_count", 0),
214
+ "created_at": getattr(fact, "created_at", "") or "",
215
+ "evidence_chain": list(getattr(r, "evidence_chain", []) or []),
216
+ })
217
+
218
+ # F-3 source discipline, then F-2 budget — order matters (discipline first
219
+ # so the template firewall runs before any preview slicing).
220
+ disciplined = [apply_source_content_discipline(d, include_source=include_source) for d in raw]
221
+ budgeted = apply_recall_budget(
222
+ disciplined, per_fact_max=per_fact_max, total_max=total_max, full=full,
223
+ )
224
+ no_confident_match = bool(getattr(response, "no_confident_match", False))
225
+ return budgeted, no_confident_match
@@ -95,32 +95,20 @@ class EngineRecallAdapter:
95
95
  self._engine._db.get_memory_content_batch(memory_ids)
96
96
  if memory_ids else {}
97
97
  )
98
- results = []
99
- for r in response.results[:limit]:
100
- fact_type = getattr(r.fact, "fact_type", None)
101
- lifecycle = getattr(r.fact, "lifecycle", None)
102
- results.append({
103
- "fact_id": r.fact.fact_id,
104
- "memory_id": r.fact.memory_id,
105
- "content": _sanitize_json_text(r.fact.content[:300]),
106
- "source_content": _sanitize_json_text(memory_map.get(r.fact.memory_id, "")),
107
- "score": round(r.score, 4),
108
- "confidence": round(r.confidence, 4),
109
- "trust_score": round(r.trust_score, 4),
110
- "channel_scores": {
111
- k: round(v, 4)
112
- for k, v in (r.channel_scores or {}).items()
113
- },
114
- "fact_type": fact_type.value
115
- if fact_type and hasattr(fact_type, "value") else "",
116
- "lifecycle": lifecycle.value
117
- if lifecycle and hasattr(lifecycle, "value") else "",
118
- "access_count": getattr(r.fact, "access_count", 0),
119
- "created_at": getattr(r.fact, "created_at", "") or "",
120
- "evidence_chain": list(
121
- getattr(r, "evidence_chain", []) or []
122
- ),
123
- })
98
+ # v3.6.6: same shared chokepoint as the HTTP route — identical output.
99
+ from superlocalmemory.server.recall_serializer import (
100
+ serialize_recall_response,
101
+ )
102
+ _rc = getattr(self._engine._config, "retrieval", None)
103
+ results, no_confident_match = serialize_recall_response(
104
+ response,
105
+ limit=limit,
106
+ memory_map={k: _sanitize_json_text(v) for k, v in memory_map.items()},
107
+ per_fact_max=getattr(_rc, "recall_per_fact_max_chars", 2400),
108
+ total_max=getattr(_rc, "recall_total_max_chars", 12000),
109
+ )
110
+ for _r in results:
111
+ _r["content"] = _sanitize_json_text(_r.get("content", ""))
124
112
  return {
125
113
  "ok": True,
126
114
  "query": query,
@@ -133,6 +121,7 @@ class EngineRecallAdapter:
133
121
  },
134
122
  "total_candidates": getattr(response, "total_candidates", 0),
135
123
  "results": results,
124
+ "no_confident_match": no_confident_match,
136
125
  }
137
126
 
138
127
 
@@ -1563,6 +1552,8 @@ def _register_daemon_routes(application: FastAPI) -> None:
1563
1552
  q: str = "", query: str = "", limit: int = 20,
1564
1553
  session_id: str = "",
1565
1554
  fast: bool = False,
1555
+ full: bool = False,
1556
+ include_source: bool = False,
1566
1557
  ):
1567
1558
  _update_activity()
1568
1559
  search_query = q or query # Accept both ?q= and ?query= for compatibility
@@ -1610,33 +1601,23 @@ def _register_daemon_routes(application: FastAPI) -> None:
1610
1601
  engine._db.get_memory_content_batch(memory_ids)
1611
1602
  if memory_ids else {}
1612
1603
  )
1613
- results = []
1614
- for r in response.results[:limit]:
1615
- fact_type = getattr(r.fact, "fact_type", None)
1616
- lifecycle = getattr(r.fact, "lifecycle", None)
1617
- results.append({
1618
- "fact_id": r.fact.fact_id,
1619
- "memory_id": r.fact.memory_id,
1620
- "content": _sanitize_json_text(r.fact.content),
1621
- "source_content": _sanitize_json_text(memory_map.get(r.fact.memory_id, "")),
1622
- "score": round(r.score, 4),
1623
- "confidence": round(r.confidence, 4),
1624
- "trust_score": round(r.trust_score, 4),
1625
- "channel_scores": {
1626
- k: round(v, 4)
1627
- for k, v in (r.channel_scores or {}).items()
1628
- },
1629
- "fact_type": fact_type.value
1630
- if fact_type and hasattr(fact_type, "value")
1631
- else getattr(r.fact, "fact_type", ""),
1632
- "lifecycle": lifecycle.value
1633
- if lifecycle and hasattr(lifecycle, "value") else "",
1634
- "access_count": getattr(r.fact, "access_count", 0),
1635
- "created_at": getattr(r.fact, "created_at", "") or "",
1636
- "evidence_chain": list(
1637
- getattr(r, "evidence_chain", []) or []
1638
- ),
1639
- })
1604
+ # v3.6.6: single shared serialization chokepoint — budget + source
1605
+ # discipline + no_confident_match, identical across every surface.
1606
+ from superlocalmemory.server.recall_serializer import (
1607
+ serialize_recall_response,
1608
+ )
1609
+ _rc = getattr(engine._config, "retrieval", None)
1610
+ results, no_confident_match = serialize_recall_response(
1611
+ response,
1612
+ limit=limit,
1613
+ memory_map={k: _sanitize_json_text(v) for k, v in memory_map.items()},
1614
+ per_fact_max=getattr(_rc, "recall_per_fact_max_chars", 2400),
1615
+ total_max=getattr(_rc, "recall_total_max_chars", 12000),
1616
+ full=full,
1617
+ include_source=include_source,
1618
+ )
1619
+ for _r in results:
1620
+ _r["content"] = _sanitize_json_text(_r.get("content", ""))
1640
1621
  return {
1641
1622
  "ok": True,
1642
1623
  "query": search_query,
@@ -1650,6 +1631,7 @@ def _register_daemon_routes(application: FastAPI) -> None:
1650
1631
  "total_candidates": getattr(response, "total_candidates", 0),
1651
1632
  "results": results,
1652
1633
  "count": len(results),
1634
+ "no_confident_match": no_confident_match,
1653
1635
  }
1654
1636
  except Exception as exc:
1655
1637
  raise HTTPException(500, detail=str(exc))
@@ -411,3 +411,6 @@ class RecallResponse:
411
411
  channel_weights: dict[str, float] = field(default_factory=dict)
412
412
  total_candidates: int = 0
413
413
  retrieval_time_ms: float = 0.0
414
+ # v3.6.6: Evidence floor. True when floor gates out ALL results.
415
+ # Additive field — backward compatible (defaults to False).
416
+ no_confident_match: bool = False
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: superlocalmemory
3
- Version: 3.6.4
3
+ Version: 3.6.6
4
4
  Summary: Information-geometric agent memory with mathematical guarantees
5
5
  Author-email: Varun Pratap Bhardwaj <admin@superlocalmemory.com>
6
6
  License: AGPL-3.0-or-later
@@ -73,6 +73,7 @@ src/superlocalmemory/compliance/retention.py
73
73
  src/superlocalmemory/compliance/scheduler.py
74
74
  src/superlocalmemory/core/__init__.py
75
75
  src/superlocalmemory/core/backend_orchestrator.py
76
+ src/superlocalmemory/core/block_hygiene.py
76
77
  src/superlocalmemory/core/clock_monitor.py
77
78
  src/superlocalmemory/core/config.py
78
79
  src/superlocalmemory/core/consolidation_engine.py
@@ -92,6 +93,7 @@ src/superlocalmemory/core/graph_analyzer.py
92
93
  src/superlocalmemory/core/graph_pruner.py
93
94
  src/superlocalmemory/core/health_monitor.py
94
95
  src/superlocalmemory/core/hooks.py
96
+ src/superlocalmemory/core/ingest_gate.py
95
97
  src/superlocalmemory/core/injection.py
96
98
  src/superlocalmemory/core/loop_watchdog.py
97
99
  src/superlocalmemory/core/maintenance.py
@@ -358,6 +360,7 @@ src/superlocalmemory/retrieval/vector_store.py
358
360
  src/superlocalmemory/server/__init__.py
359
361
  src/superlocalmemory/server/api.py
360
362
  src/superlocalmemory/server/bandit_loops.py
363
+ src/superlocalmemory/server/recall_serializer.py
361
364
  src/superlocalmemory/server/security_middleware.py
362
365
  src/superlocalmemory/server/ui.py
363
366
  src/superlocalmemory/server/unified_daemon.py