OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

[Bug] write_stored_links silently drops memory cross-links: "pathlock lease ref ... does not cover the requested operation"

Open neurosovereign opened this issue 3 months ago • 2 comments

[Bug] Memory extraction silently drops trajectory cross-links: "pathlock lease ref ... does not cover the requested operation"

Environment

  • OpenViking version: 0.4.12 (installed wheel; source inspected from the matching 0.4.12 tree)
  • Deployment: single-node local server (openviking-server, systemd user unit), AGFS path locking active
  • Observed on: Linux, Python 3.12

Symptoms

During post-commit memory extraction, write_stored_links repeatedly fails to apply links/backlinks to endpoint memory files. The affected files (typically trajectories) are written correctly, but their links / backlinks fields are never updated — silently, since the failure is only logged as an error inside a broad per-URI exception handler and the overall extraction still reports success.

Exact log signature (reproduces dozens of times per day under normal multi-session usage):

ERROR | openviking.session.memory.memory_updater:write_stored_links:153 - Failed to apply links to <account>/memories/trajectories/<name>.md: internal error: lock lease error: invalid lock request: pathlock lease ref '<uuid>' does not cover the requested operation

In our deployment this occurs ~30–50×/day. Every occurrence is a permanently lost cross-link (no retry happens).

Root cause analysis

The extraction transaction's pathlock lease is acquired over the memory schema directories (exact file paths + tree dirs), but write_stored_links then writes to link endpoint URIs that are outside that locked scope.

Call chain:

  1. SessionCompressorV2 acquires the lease:
    • compressor_v2.py (~line 355): lease = await viking_fs._async_agfs.pathlock_acquire_exact_tree_batch(exact_lock_paths, tree_lock_dirs, timeout_secs=0.1) where the paths come from _render_memory_schema_locks() (line 81) — i.e., schema directory/file paths for the user's memory types.
    • The lease becomes transaction_handle and is passed into MemoryUpdater.
  2. MemoryUpdater.apply_operations → _apply_links_to_existing_files (memory_updater.py line 1153) → write_stored_links (line 74), passing lease_ref=self._transaction_handle.
  3. write_stored_links performs read_file(uri) + write_file(uri, ..., lease_ref=lease_ref) for each link endpoint URI (line 123).
  4. VikingFS.write_file (storage/viking_fs.py line 3206) forwards the lease via fs_ctx=self._pathlock_fs_ctx(ctx, lease_ref) into the Rust AGFS layer.
  5. The AGFS pathlock implementation rejects the write because the endpoint path is not covered by the paths locked in step 1:
lock lease error: invalid lock request: pathlock lease ref '<uuid>' does not cover the requested operation

The mismatch: the lease is scoped to schema-rendered paths, while resolved link endpoints can be arbitrary memory files (e.g. memories/trajectories/<slug>.md, whose filenames are generated at runtime and therefore not present in the exact-lock set). When the endpoint filename template contains variables, _render_memory_schema_locks falls back to tree locks — but trajectories with variable filenames can still end up outside the acquired tree scope depending on isolation-handler rendering, and any endpoint in a different memory type directory than those rendered is never covered.

Note the same lease is also passed to _apply_delete and _sync_resource_refs_for_result, so deletes of out-of-scope URIs will hit the identical error.

Impact

  • Cross-link/backlink graph for memories is silently incomplete.
  • No error surfaces to the caller; result.summary() counts look healthy.
  • Frequency scales with session volume (we see 30–50/day).

Suggested fixes

Either:

  1. Acquire the lease over the union of actual write targets: after operations resolve, compute all endpoint URIs of resolved_links (+ delete URIs) and extend the exact-lock set before applying writes (second acquire phase or pathlock_acquire_batch with owner_lease_ref); or
  2. Degrade gracefully per-write: in write_stored_links, if a lease-scoped write is rejected as "does not cover", re-acquire a fresh narrow lock for that URI instead of failing; or
  3. At minimum, treat this as a hard error in MemoryUpdateResult.errors so callers/retries can see it rather than swallowing it in the per-URI try/except.

Option 2 seems smallest and preserves current concurrency semantics.

Happy to provide additional logs or test the fix.

neurosovereign avatar Aug 25 '26 22:08 neurosovereign

This report is against v0.4.12 and traces SessionCompressorV2. That path was removed in v0.4.14; #4031 was closed after the maintainer confirmed the V2 lease-coverage fix and recommended upgrading. The latest release is v0.4.16, so upgrading and retesting should be the first step here. If the same error still appears on V3, #4329 already tracks the current policy-trainer path and would be the better place for the new trace.

ranxi2001 avatar Aug 26 '26 02:08 ranxi2001

Thanks for reporting this and for the detailed investigation.

This report is against OpenViking v0.4.12 and traces SessionCompressorV2. That pipeline was removed in v0.4.14; the V2 lease-coverage issue was tracked in #4031. The current V3 policy-trainer path is separately tracked in #4329.

To avoid tracking the removed V2 path and the current V3 path in parallel, we are closing this issue. Please use a current OpenViking release; #4329 is the active issue for the V3 failure.

qin-ctx avatar Aug 26 '26 03:08 qin-ctx