OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

[Bug]: memory extraction deletes whole root memory files (profile.md) via delete_ids — no protection for builtin memory types

Open reysonk opened this issue 3 weeks ago • 3 comments

Issue Origin

Observed in a real deployment on every update of the root memory files.

Summary

Memory extraction can delete a whole root memory file (profile.md, identity.md, soul.md, MEMORY-MAP.md) when the LLM puts the page into delete_ids. There is no guard on the delete path: MemoryUpdater._apply_delete() calls viking_fs.rm() unconditionally, so the file is removed silently, and (unlike per-type upsert) there is no notion of a protected / singleton builtin memory type. In our case viking://user/<user>/memories/profile.md disappeared several times; nothing in the API or logs said the file had been deleted.

The deletion is also one-way: root memory files under memories/*.md are not tracked in the .ovgit history (only experiences/ and entities/ are), so there is nothing to restore from.

Environment

  • OpenViking v0.5.2 (Docker image built from tag v0.5.2_pr6594), Python 3.13
  • Linux host, bind-mounted data directory (/app/.openviking)
  • Builtin memory types from system/memory/memory_templates/builtin/: profile.yaml (no operation_mode), identity.yaml (operation_mode: "upsert"), soul.yaml

Observed behaviour

  1. viking://user/<user>/memories/profile.md (7905 bytes, valid single-H1 profile) and identity.md periodically vanish from disk.
  2. The extractor keeps writing session facts into identity.md, so the files are actively maintained — then at some point the file is simply gone.
  3. No log line reports the deletion at default log level: _apply_delete logs only the NotFoundError branch.
  4. Recovery is only possible from an external copy (profile.md is not part of the ovgit history).

Root cause

The extractor's output protocol explicitly allows deleting whole items by id — openviking/session/memory/extraction_output_protocol/prompt_fragments.py:156-157:

To delete an existing item, add an entry to delete_ids using its page_id. delete_ids deletes the whole item: use it only if every substantive fact is in scope; never infer scope from the file name/topic.

openviking/session/memory/extract_loop.py:953-1000 resolves each delete_page_id into a delete_uri and appends the old content to ResolvedOperations(..., delete_file_contents, ...). openviking/session/memory/memory_updater.py then applies it through

async def _apply_delete(self, uri: str, ctx: RequestContext, lease_ref: Any = None) -> None:
    """Apply delete operation (uri is already a string)."""
    viking_fs = self._get_viking_fs()
    await viking_fs.rm(uri, recursive=False, ctx=ctx, lease_ref=lease_ref)

There is no check of the memory type, no check for builtin/singleton pages, and no dry-run. If a batch contains only deletes, it is executed as a single no_merge / delete_only step, so one bad extractor decision removes the file with no compensating write.

Two aggravating factors:

  • operation_mode: add_only is not a fix: it also forbids updates, so a profile that must evolve would freeze (and profile.yaml has no operation_mode field at all, i.e. it defaults to upsert).
  • openviking/storage/viking_fs/_sync.py rewrites files as rm(target) followed by mv(root_file, target_file). If the second step fails (e.g. the content exceeds the model context or the vector store rejects it), the target file is already gone and only logger.error("[SyncDiff] Failed to move updated file: ...") is logged.

Impact

Root memory files are the agent's identity/profile and are edited by the extractor on every session; losing them silently degrades the agent and is not recoverable from local history. For us it happened repeatedly (at least twice in a few hours).

Suggested fix

  1. Refuse deletes for builtin/singleton memory pages, or require an explicit allow_delete flag in the memory type definition (memory_type_registry) for root pages such as profile.md / identity.md / soul.md.
  2. Log every delete at INFO with the URI and the batch reason (delete_only vs merge), so silent removal is visible.
  3. Make _sync.py write atomically (temp file + rename, or keep a .bak copy until mv succeeds).
  4. Include root memory files in the .ovgit history (or at least snapshot them before delete), so an accidental removal is recoverable.

Workaround we currently use

A read-only bind mount of a patched memory_updater.py that skips rm() for profile.md, identity.md, soul.md, MEMORY-MAP.md. It works but it is a monkey-patch of the image and has to be re-applied after every image update — which is why we would prefer a proper guard upstream.

reysonk avatar Nov 05 '26 11:11 reysonk

I can take this. The plan: guard MemoryUpdater._apply_delete() so a builtin/protected root memory type (profile, identity, soul, MEMORY-MAP) is never passed to viking_fs.rm() when the extractor emits it in delete_ids. A delete targeting a protected type will be logged and skipped rather than silently removing the file, mirroring the protection that already exists on the per-type upsert path. I will add a regression test that asserts _apply_delete leaves the protected file on disk when asked to delete it.

Wang-Zephyr avatar Nov 05 '26 12:11 Wang-Zephyr

Cross-reference — same defect path, wider blast radius: #7504

We hit the same silent-deletion behavior beyond root memory files: entities / preferences / experiences / cases / peers files. Direct evidence obtained via session-archive memory_diff.json: three matching operations.deletes[] records (URI + deleted_content) for one entities file that vanished 3× within 2 hours — delete_ids → MemoryUpdater._apply_delete → viking_fs.rm, no memory-type whitelist, and no trace in compile bills (deletes=[]) or journal. Full ledger since Oct 10: 148 delete records, 116 URIs still missing on disk (incl. memories/profile.md, deleted 6×), while the index still lists them (recall → 404).

同一缺陷路径的扩大实证(证据、时间线、影响面与配置对照详见 #7504)。

zwg2026pc avatar Nov 12 '26 07:11 zwg2026pc

this issue has been fixed by #7099

fujiajie666 avatar Nov 15 '26 13:11 fujiajie666