Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(memory): preserve initial line identities during audit edits

dajiaohuange20001beb419
diff --git a/deeptutor/services/memory/consolidator/line_doc.py b/deeptutor/services/memory/consolidator/line_doc.py
index a14d1051e..0ee23ed1e 100644
--- a/deeptutor/services/memory/consolidator/line_doc.py
+++ b/deeptutor/services/memory/consolidator/line_doc.py
@@ -169,7 +169,9 @@ def render_view(doc: Document) -> LineView:
# ── Apply edits ─────────────────────────────────────────────────────────
-def apply_edits(doc: Document, edits: Iterable[Edit]) -> tuple[Document, EditReport]:
+def apply_edits(
+ doc: Document, edits: Iterable[Edit], *, view: LineView | None = None
+) -> tuple[Document, EditReport]:
"""Apply a batch of edits, in reverse line order, to a fresh copy.
Returns ``(new_doc, report)``. ``new_doc`` is always returned; if
@@ -180,7 +182,9 @@ def apply_edits(doc: Document, edits: Iterable[Edit]) -> tuple[Document, EditRep
Reverse order avoids line-number drift: removing line 5 does not
affect the meaning of "line 3" since 3 < 5 and we process 5 first.
"""
- view = render_view(doc)
+ # Chunked callers retain the view from which all model prompts were built.
+ # Its entry IDs keep later edits anchored after earlier lines are deleted.
+ view = view if view is not None else render_view(doc)
edit_list = _sort_reverse(list(edits))
report = EditReport()
diff --git a/deeptutor/services/memory/consolidator/modes/audit.py b/deeptutor/services/memory/consolidator/modes/audit.py
index 2f71397fc..73fd1c00f 100644
--- a/deeptutor/services/memory/consolidator/modes/audit.py
+++ b/deeptutor/services/memory/consolidator/modes/audit.py
@@ -8,10 +8,8 @@ Algorithm
contiguous slice of lines whose bullet entries get **annotated with
their full source content** (no truncation).
3. Per chunk: LLM call → parse edits → apply against the in-memory doc
- in reverse line order. Across chunks, edits stack — but because we
- slice on whole-line boundaries and apply per-chunk before the next
- chunk runs, the line numbers the next chunk sees are still the ones
- the LLM was given.
+ in reverse line order. All chunks use the original line view, whose
+ entry IDs anchor edits after earlier chunks change line positions.
4. Atomic flush.
The chunker is deliberately char-based (same as update) so chunk size
@@ -142,6 +140,7 @@ async def _run_audit_l2(
return AuditResult(layer="L2", key=surface, chunks_processed=0, no_doc=True)
doc = load_doc(l2_path, default_title=f"{surface} memory")
+ audit_view = render_view(doc)
entity_lookup = {ent.id: ent for ent in snap.read_snapshot(surface)}
prompt = load_prompt("audit_l2", language)
focus, _sections = surface_focus(language, surface)
@@ -194,7 +193,7 @@ async def _run_audit_l2(
{"stage": "facts_extracted", "turn": chunk.index + 1, "edits": 0},
)
continue
- doc, report = apply_edits(doc, edits)
+ doc, report = apply_edits(doc, edits, view=audit_view)
edits_applied += len(report.applied)
edits_rejected += len(report.rejected)
if report.applied:
@@ -228,8 +227,6 @@ async def _run_audit_l2(
"detail": res.detail,
},
)
- # Refresh annotation for the next chunk — line numbers shifted.
- annotated_text, line_ranges = _build_annotated_l2(doc, surface, entity_lookup)
await emit(
on_event,
@@ -283,6 +280,7 @@ async def _run_audit_l3(
return AuditResult(layer="L3", key=slot, chunks_processed=0, no_doc=True)
doc = load_doc(l3_path, default_title=f"{slot} memory")
+ audit_view = render_view(doc)
l2_lookup = _build_l2_entry_lookup()
prompt = load_prompt("audit_l3", language)
focus, _sections = slot_focus(language, slot)
@@ -335,7 +333,7 @@ async def _run_audit_l3(
{"stage": "facts_extracted", "turn": chunk.index + 1, "edits": 0},
)
continue
- doc, report = apply_edits(doc, edits)
+ doc, report = apply_edits(doc, edits, view=audit_view)
edits_applied += len(report.applied)
edits_rejected += len(report.rejected)
if report.applied:
@@ -369,7 +367,6 @@ async def _run_audit_l3(
"detail": res.detail,
},
)
- annotated_text, _ranges = _build_annotated_l3(doc, l2_lookup)
await emit(
on_event,
diff --git a/tests/services/memory/test_audit_line_identity.py b/tests/services/memory/test_audit_line_identity.py
new file mode 100644
index 000000000..c2ed9d15a
--- /dev/null
+++ b/tests/services/memory/test_audit_line_identity.py
@@ -0,0 +1,53 @@
+"""Chunked audit edits retain the line identities shown to the model."""
+
+import json
+from pathlib import Path
+import re
+
+import pytest
+
+from deeptutor.services.memory import paths
+from deeptutor.services.memory.consolidator.modes import audit
+from deeptutor.services.memory.document import Document, Entry, parse, serialize
+from deeptutor.services.memory.ids import new_entry_id
+from deeptutor.services.memory.settings import ChunkingSettings, MemorySettings, MergeSettings
+
+
[email protected]("layer,key", [("L2", "chat"), ("L3", "recent")])
+async def test_earlier_deletion_does_not_shift_later_chunk_target(
+ tmp_path: Path, monkeypatch, layer: str, key: str
+) -> None:
+ """Use real annotation, chunking, parsing and disk writes with fixed model edits."""
+ monkeypatch.setattr(paths, "memory_root", lambda: tmp_path)
+ monkeypatch.setattr(audit.snap, "read_snapshot", lambda surface: [])
+ settings = MemorySettings(
+ chunking=ChunkingSettings(min_chunk_chars=200, max_chunk_chars=400, overlap_ratio=0),
+ merge=MergeSettings(auto_after_audit=False),
+ )
+ monkeypatch.setattr(audit, "load_memory_settings", lambda: settings)
+ labels = ["delete-first", "keep-second", "delete-third", "keep-fourth"]
+ entries = [Entry(new_entry_id(), "Notes", label, ["chat:source"]) for label in labels]
+ document = tmp_path / layer / f"{key}.md"
+ document.parent.mkdir()
+ document.write_text(serialize(Document("Memory", [("Notes", entries)])), encoding="utf-8")
+ emitted = set()
+
+ async def fixed_edits(*, user_prompt, **kwargs):
+ for label in ("delete-first", "delete-third"):
+ if label in emitted:
+ continue
+ match = re.search(r"(?:line\s+|^\s*)(\d+): (?:- )?" + label, user_prompt, re.MULTILINE)
+ if match:
+ emitted.add(label)
+ return json.dumps({"edits": [{"op": "delete", "line": int(match[1])}]})
+ return '{"edits": []}'
+
+ monkeypatch.setattr(audit, "call_llm", fixed_edits)
+ result = await audit.run_audit(layer, key, budget=4)
+ assert result.chunks_processed > 1
+ assert emitted == {"delete-first", "delete-third"}
+ assert [entry.text for entry in parse(document.read_text(encoding="utf-8")).all_entries()] == [
+ "keep-second",
+ "keep-fourth",
+ ]