MemOS icon indicating copy to clipboard operation
MemOS copied to clipboard

[RFC] A span-level update contract for textual memory: untouched-span verification, scoped rollback, and explicit deletions

Open tanbopiao opened this issue 1 week ago • 3 comments

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)

  1. 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_update in src/memos/mem_feedback/feedback.py only make whole-string decisions).
  2. 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_update passes it as UPDATE. #2544's order-sensitive fix does not change this: the ratio is still global.
  3. 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).
  4. The version fallback writes no history. With memory_version_switch on but no provider for memory_version.apply_feedback_update, the fallback assigns the whole new string and writes no history entry (repro step 6 in #2527).

Proposal: a four-part span-level update contract

  1. 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.
  2. Explicit deletion operations. Deletions become explicit DELETE/FILL span operations instead of silent rewrites, making each change auditable.
  3. 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.
  4. History completeness. The version fallback writes a history entry; 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, default off; warn logs violations without blocking, so behavior can be observed before tightening.
  • should_keep_update is 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.

tanbopiao avatar Nov 23 '26 05:11 tanbopiao

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?

wzxsdf avatar Nov 25 '26 09:11 wzxsdf

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_switch on 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.

tanbopiao avatar Nov 25 '26 10:11 tanbopiao

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.

tanbopiao avatar Nov 26 '26 11:11 tanbopiao