OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

[Proposal] Add clean-replace semantics for memory content/write without preserving old MEMORY_FIELDS

Open edynasty opened this issue 2 months ago • 0 comments

Issue Origin

Observed or reproduced in a real environment.

Summary

This is an API design proposal, not a bug report. Current content/write replace semantics are intentional and upstream-tested (see intent evidence below). The gap: there is no supported way to perform a clean replacement of a memory file's content that also resets MEMORY_FIELDS metadata, so legitimate workflows — notably memory migration and scope correction — cannot clear stale metadata (including a stale peer_id) without delete+recreate, which also discards version history and resource refs.

A concrete metadata-consistency consequence: after a direct content/write replace on a user-global memory URI, the stored MEMORY_FIELDS can retain a foreign peer_id from the previous object, leaving the persisted scope metadata inconsistent with the URI's actual scope.

Environment

  • OpenViking v0.4.23 (Docker)
  • openviking/storage/content_write.py is byte-identical between v0.4.23 and current main at the time of writing.

Current semantics

ContentWriteCoordinator._write_in_place for memory URIs (openviking/storage/content_write.py):

if context_type_for_uri(uri) == "memory":
    if mode == "replace":
        existing_raw = await self._viking_fs.read_file(uri, ctx=ctx)
        mf = MemoryFileUtils.read(existing_raw, uri=uri)
        mf.content = content          # body replaced
                                     # extra_fields (peer_id, version, topic,
                                     # memory_type, source_extraction_id, ...)
                                     # all carry over untouched
    ...
    rendered = MemoryFileUtils.write(mf)

So content/write with mode: "replace" on memory URIs is effectively body-update semantics: the visible content is replaced while the previous object's MEMORY_FIELDS trailer is re-serialized verbatim.

This is intentional and asserted by tests/server/test_content_write_service.py::test_memory_replace_preserves_metadata (and the append variant), and #4339 / PR #4386 deliberately extended the same preservation contract to batch-write ("protected MEMORY_FIELDS"). I am not asking to change that default.

However, the public API docs (docs/en/api/12-content.md) document replace as "overwrites an existing file" and only document metadata preservation for .abstract.md/.overview.md sidecars ("A body-only request preserves stored OKF metadata") — the memory-file preservation behavior is currently undocumented, and WriteContentRequest offers no opt-out (processing_mode only controls semantic refresh; there is no metadata-reset flag).

Reproduction (deterministic, 3/3 runs)

Disposable user; synthetic markers; created via the public content/write API with mode: "create" and an explicit trailer, then replaced with mode: "replace" body-only:

STEP 1 — create viking://user/<user>/memories/preferences/user/evidence-write-01.md:

- ORIGINAL_CONTENT_MARKER

<!-- MEMORY_FIELDS
{
  "user": "user",
  "topic": "evidence-write-01",
  "peer_id": "evidence-peer-b",
  "version": 7,
  "memory_type": "preferences"
}
-->

STEP 2 — direct write (mode: "replace", content - REPLACEMENT_CONTENT_MARKER, no trailer):

content/read returns the new body, but raw storage (content/read?raw=true) is:

- REPLACEMENT_CONTENT_MARKER

<!-- MEMORY_FIELDS
{
  "user": "user",
  "topic": "evidence-write-01",
  "peer_id": "evidence-peer-b",
  "version": 7,
  "memory_type": "preferences"
}
-->

Old peer_id, version, topic, and memory_type all survived verbatim. Identical result on 3/3 fresh objects.

STEP 3 — delete + recreate control: after DELETE /api/v1/fs and re-create with the same replacement body, raw storage is clean: body + <!-- MEMORY_FIELDS {"version": 1} --> only. So delete+recreate achieves the clean replacement, at the cost of losing version continuity and any other preserved metadata (e.g. resource_refs).

Metadata-consistency consequence (scope mismatch)

Repeating STEP 1–2 on a user-global URI (a memory path with no peers/ segment) whose old trailer carried "peer_id": "evidence-peer-b", "version": 339:

  • after the direct replace, the file lives at viking://user/<user>/memories/... (user-global scope) while its persisted MEMORY_FIELDS still claims peer_id: evidence-peer-b;
  • fs stat / attrs.memory report the foreign peer for a user-global URI;
  • any tooling that keys on MEMORY_FIELDS.peer_id (audit, graph, migration tooling reading raw storage) sees a scope that disagrees with the URI.

This is a metadata consistency issue, not an access-control problem — routing and isolation are URI-based at read time — but it is exactly the state a memory-migration or scope-correction workflow needs to clear, and today the only way to clear it is delete+recreate.

Proposal

Add an explicit opt-in clean-replacement mode, e.g.:

POST /api/v1/content/write
{
  "uri": "viking://user/<user>/memories/...",
  "content": "...",
  "mode": "replace",
  "metadata_mode": "replace"
}
  • metadata_mode defaults to "preserve" (current behavior, unchanged — keeps #4386's contract and the existing tests green);
  • metadata_mode: "replace" treats the write as a fresh object: drop the previous MEMORY_FIELDS entirely and regenerate only what the server would generate on create (e.g. version: 1), or accept a caller-supplied trailer verbatim as mode: "create" already does;
  • mirror the option on batch-write for consistency;
  • add one doc line to docs/en/api/12-content.md stating that memory-file writes preserve the existing MEMORY_FIELDS by default and how to opt out.

Regression tests: parameterize test_memory_replace_preserves_metadata with metadata_mode="replace" asserting the trailer is reset (version: 1, no stale peer_id), plus one user-global-URI case asserting no foreign peer_id survives.

Related work

  • #4339 / PR #4386 — established MEMORY_FIELDS as protected metadata that replace must preserve (batch-write was fixed to match single-write). This proposal fully preserves that default and only adds an explicit opt-out.
  • #3239 — recall exposing hidden MEMORY_FIELDS in rendered output (different layer, same "hidden metadata can surprise consumers" theme).

Happy to contribute a focused PR if the direction sounds right.

edynasty avatar Oct 04 '26 13:10 edynasty