symfonic.capabilities.memory.phases.merge¶
merge ¶
Phase 13: LLM-judged semantic-duplicate merge (true supersede).
Moved here from symfonic.capabilities.memory.phases.merge, which now
imports it back: phase 13 is on the quick roster -- v8.17 put it there
because quick is the only cadence the engine schedules in-process -- and a
roster the kernel composes cannot reach into the legacy package.
Closes the write-time dedup's documented misses: restatements that
reword a fact's OPENING ("Uses pytest and ruff" vs "The user uses pytest
and ruff"), front rewording, and clause reordering all escape the
prefix-gated string match in symfonic.memory.dedup -- see
tests/memory/test_restatement_corpus.py for the full matrix. Those
classes need semantic-equivalence judgment, which is deliberately kept
OFF the per-turn write path and run here, offline, during consolidation.
Safety model (why an LLM judge and not an embedding threshold): cosine similarity cannot distinguish "deploys Tuesdays, never Fridays" from "deploys Fridays, never Tuesdays", and a false merge OVERWRITES a real memory. Embeddings (or a lexical ratio when no embeddings are available) only PREFILTER candidate pairs; every merge requires the judge to confirm the two phrasings state the same fact and to produce the merged phrasing.
Supersede mechanics: the canonical node (higher importance, else the
older row) absorbs the pair -- merged phrasing, max importance, union
of tags -- and the absorbed node is SOFT-RETRACTED with the namespaced
marker (symfonic.memory.retraction), so it vanishes from every read
path immediately, remains auditable, and is hard-deleted by the
existing prune_retracted phase after the grace window.
Exclusions: only nodes materialised at exactly the run scope are
eligible (visibility != ownership); AGENT_IDENTITY, SOUL and
entity nodes are never merged (they have their own identity rules).
SOUL: is excluded for the same reason as AGENT_IDENTITY: once a
block layer renders these nodes into a cached prompt prefix, a false
merge here would silently corrupt the user's profile for the whole
session rather than just producing one wrong MEMORY_CONTEXT line
(see .claude/docs/2026-08-04-prompt-part-blocks-architecture.md §6).
merge_semantic_duplicates
async
¶
merge_semantic_duplicates(graph: Any, scope: TenantScope, *, chat_model: Any, embedding_provider: Any | None = None, embedding_threshold: float = DEFAULT_EMBEDDING_THRESHOLD, lexical_threshold: float = DEFAULT_LEXICAL_THRESHOLD, max_pairs_per_run: int = 10, on_candidates: Callable[[Iterable[Any]], None] | None = None, on_supersede: Callable[[Any, Any], None] | None = None) -> int
Merge LLM-confirmed duplicate semantic facts; return merge count.
Per-pair failures are logged and skipped -- one bad judge response must never abort the phase.
on_candidates and on_supersede report node ids, and nothing else,
to a caller keeping a cycle ledger. The supersede hook fires where the
retraction is written rather than being derived from the return value, so
the count stays true if one merge ever absorbs more than one row. Both are
optional and the legacy consolidator passes neither.
Source code in src/symfonic/capabilities/memory/phases/merge.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | |