[Bug]: memory extraction deletes whole root memory files (profile.md) via delete_ids — no protection for builtin memory types
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 tagv0.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(nooperation_mode),identity.yaml(operation_mode: "upsert"),soul.yaml
Observed behaviour
viking://user/<user>/memories/profile.md(7905 bytes, valid single-H1 profile) andidentity.mdperiodically vanish from disk.- The extractor keeps writing session facts into
identity.md, so the files are actively maintained — then at some point the file is simply gone. - No log line reports the deletion at default log level:
_apply_deletelogs only theNotFoundErrorbranch. - Recovery is only possible from an external copy (
profile.mdis 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_idsusing its page_id.delete_idsdeletes 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_onlyis not a fix: it also forbids updates, so a profile that must evolve would freeze (andprofile.yamlhas nooperation_modefield at all, i.e. it defaults toupsert).openviking/storage/viking_fs/_sync.pyrewrites files asrm(target)followed bymv(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 onlylogger.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
- Refuse deletes for builtin/singleton memory pages, or require an explicit
allow_deleteflag in the memory type definition (memory_type_registry) for root pages such asprofile.md/identity.md/soul.md. - Log every delete at INFO with the URI and the batch reason (
delete_onlyvs merge), so silent removal is visible. - Make
_sync.pywrite atomically (temp file + rename, or keep a.bakcopy untilmvsucceeds). - Include root memory files in the
.ovgithistory (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.
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.
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)。
this issue has been fixed by #7099