symfonic.core.learning.phases_semantic_merge¶
phases_semantic_merge ¶
Phase 13: LLM-judged semantic-duplicate merge (true supersede).
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 and entity nodes
are never merged (they have their own identity rules).
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,
) -> 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.
Source code in src/symfonic/core/learning/phases_semantic_merge.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 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 | |