[RFC] A span-level update contract for textual memory: untouched-span verification, scoped rollback, and explicit deletions
Thanks and positioning
Thanks to #2544 for upgrading the feedback update guard from character-set similarity to an order-sensitive difflib.SequenceMatcher (merged into dev-v2.0.37). This proposal does not duplicate that fix. It argues that even with an order-sensitive global ratio gate, the update contract for textual memory remains "one whole string replaces another whole string" — and four gaps remain unchanged on main (3a16e5f, v2.0.36).
This follows up #2527, which has been open and stale for five weeks with no design PR in flight.
Four remaining gaps (verified on main @ 3a16e5f)
- Whole-string replacement with no untouched-span verification. The feedback prompt instructs the model to "change only the wrong span and leave the other sentences unchanged", but the version layer never verifies that untouched sentences are actually preserved (
standard_operations/filter_fault_updateinsrc/memos/mem_feedback/feedback.pyonly make whole-string decisions). - A global ratio gate silently admits deletions. Deleting a single fact from a long memory (>=200 estimated tokens) changes the ratio by ~0.095, below the 0.2 gate, so
should_keep_updatepasses it as UPDATE. #2544's order-sensitive fix does not change this: the ratio is still global. - Rollback wipes interleaved corrections. History snapshots store one whole string (
ArchivedTextualMemory.memory); restoring a snapshot erases corrections made after it to other facts (repro step 4 in #2527). - The version fallback writes no history. With
memory_version_switchon but no provider formemory_version.apply_feedback_update, the fallback assigns the whole new string and writes nohistoryentry (repro step 6 in #2527).
Proposal: a four-part span-level update contract
- Untouched-span verification. UPDATE operations carry
changed_spans(or an untouched-span checksum). Before persisting, the version layer deterministically verifies that undeclared regions are identical after whitespace normalization; violations are rejected or downgraded with a structured error. - Explicit deletion operations. Deletions become explicit DELETE/FILL span operations instead of silent rewrites, making each change auditable.
- Span-scoped rollback. Restore targets a single fact/span, using a three-way merge (snapshot x current value x requested restore) so corrections made after the snapshot to other facts survive.
- History completeness. The version fallback writes a
historyentry; history entries migrate incrementally from whole-string copies to span-diff references (backward compatible, phased).
Compatibility and rollout
- A config switch
memory_update_contract: off | warn | enforce, defaultoff;warnlogs violations without blocking, so behavior can be observed before tightening. should_keep_updateis kept as a cheap pre-gate; nothing is removed.
Scope
This RFC intentionally constrains itself to the feedback update path for textual memory. Chunker and merge paths are out of scope to keep the change surface reviewable.
Happy to adjust the contract shape based on maintainer feedback — the goal is a minimal, verifiable invariant rather than a large refactor.
Thanks for following up on #2544, which I contributed. I agree that an order-sensitive similarity guard does not guarantee preservation of unrelated facts. I'd like to contribute in stages, starting with a focused fix for missing history and version advancement in the feedback version fallback, with regression tests. Span validation and scoped rollback could follow after agreement on the contract and conflict handling. Would this scope fit the maintainers' plans?
Thanks for the quick follow-up, @wzxsdf — and thanks again for #2544. Yes, the staged approach fits, and Phase 1 as you scoped it is exactly the right entry point.
Phase 1 (agreed): Fix the version fallback so that memory_version.apply_feedback_update (a) writes a history entry and (b) advances the version instead of silently assigning the whole new string. Acceptance criteria:
- Regression tests must cover the repro in #2527 (step 6): with
memory_version_switchon and no provider registered, an update must produce a visible history entry and a bumped version. - History entries stay backward compatible: whole-string copies are fine for now; span-diff references come later per the RFC's phased migration.
Later phases, suggested order: untouched-span verification (gap 1) → explicit deletion operations (gap 2) → span-scoped rollback with three-way merge (gap 3). Gap 3 is the trickiest, so let's align on the conflict-handling rules here in the issue before any implementation starts — a short design note (merge semantics, tie-breaking, error surface) would be enough.
One principle to keep across all phases: the memory_update_contract: off | warn | enforce switch ships with warn as the observability mode first; nothing blocks updates until the contract has soaked.
Happy to review the Phase 1 PR when it's ready — small and focused is perfect.
Follow-up on the reference implementation: we ran a red/blue adversarial validation pass against it.
Red team: 16 attack vectors — the original defect PoCs plus new ones (poisoned rows, double-apply, idempotency-key collisions, malformed JSONL, crash-stall, stale ACK, clock rollback) — hit 11 on the original copy. Blue team: after fixing 4 kernel-level defects plus a clock-rollback gap, the hardened version defends all 16/16, and the original test suite passes with zero regressions.
For this entry specifically: the ZeroDivisionError fix is a pure insertion (no behavior change elsewhere), and the patch applies to upstream main @ 3a16e5f with zero conflicts. We verified the enforce-mode gate intercepts span updates on a real update path, not just in synthetic tests.
Upstream status re-checked today: the unguarded span-update path is still present on current main, so the fix remains landable as-is.
Hardened code (this commit): https://github.com/tanbopiao/open-source-gap-fixes/commit/fed2441
This also fits the Phase 1 collaboration scope discussed above — happy to rebase the patch if the interface moves.