OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

[Bug]: Session compression silently drops trajectory backlinks - pathlock lease ref does not cover the requested operation (write_stored_links / compressor_v2)

Open neurosovereign opened this issue 3 months ago • 1 comments

Bug Description

During session commit (memory extraction + compression), OV silently drops the trajectory backlinks of experience→trajectory derived_from edges. The forward link lands on the experience file, but the reverse write into the trajectory file fails with a pathlock lease-coverage error, which write_stored_links swallows:

2026-08-08 18:48:39.746 | ERROR | openviking.session.memory.memory_updater:write_stored_links:127 - Failed to apply links to viking://user/<account>/memories/trajectories/<traj>.md: internal error: lock lease error: invalid lock request: pathlock lease ref '01d4e34d-6476-497f-8b54-5e8db3d69c34' does not cover the requested operation

One additional variant on a still-unwritten URI:

2026-08-15 18:04:54.218 | ERROR | openviking.session.memory.memory_updater:write_stored_links:127 - Failed to apply links to viking://user/<account>/memories/experiences/<exp>.md: File not found: viking://user/<account>/memories/experiences/<exp>.md

Frequency on our production instance: 47 lease-ref failures in 7 days (2026-08-08 → 2026-08-15), spread over 5 accounts; 100% of the failing writes target memories/trajectories/*. A File not found variant appeared once on an experience URI that was about to be written in the same batch.

Reproduction steps

Any session commit where:

  1. phase 1 (trajectory consolidation) writes new trajectory files, and
  2. phase 2 (experience extraction) links an experience to those trajectories, and
  3. post_apply runs while the phase-2 lease is still held.

Deterministic in our environment: "any session extraction that emits links to concurrently-written memory files" — i.e. whenever a link endpoint falls outside the current phase's lock scope.

Root cause (0.4.13 code)

The lease scope does not match the write set:

  1. SessionCompressorV2._run_extract_phase (session/compressor_v2.py:716) acquires a combined exact+tree lock over the current phase's memory-schema directories via pathlock_acquire_exact_tree_batch(exact_lock_paths, tree_lock_dirs, timeout_secs=0.1) (compressor_v2.py:784). Paths come from _render_memory_schema_locks (compressor_v2.py:81) — only the schema dirs of the phase's memory types.

  2. Before unlock, post_apply (_append_sources_before_unlock, compressor_v2.py:593) calls _append_trajectories_to_experiences (compressor_v2.py:994) → _append_trajectory_metadata (compressor_v2.py:1030), passing lease_ref=lease.

  3. _append_trajectory_metadata writes the experience file under that lease (covered → succeeds), then calls write_stored_links(links, ctx, viking_fs, skip_uris={exp_uri}, lease_ref=lease) (compressor_v2.py:1081) to write the trajectory backlinks.

  4. write_stored_links (session/memory/memory_updater.py:80-127) calls viking_fs.write_file(uri, ..., lease_ref=lease_ref) per endpoint → VikingFS.write_file (storage/viking_fs.py:3235) packs the lease into fs_ctx via _pathlock_fs_ctx (viking_fs.py:562).

  5. Rust side, resolve_auto_pathlock_action (crates/ragfs/src/lock/manager.rs:1517-1529): with an ambient lease_ref present, coverage is validated with requests.iter().all(|r| lease.lease.covers(r)). The phase-2 lease (experience/user-memory schema dirs) does not cover the /memories/trajectories/ paths written in phase 1 → PathLockError::InvalidRequest("pathlock lease ref '...' does not cover the requested operation").

The trajectory files were written under a different phase's lock scope, so the phase-2 lease structurally cannot cover them. The error is then swallowed by write_stored_links's broad except Exception (memory_updater.py:125-127): the commit reports success while the reverse edges are dropped — no retry, no visibility beyond the ERROR log line.

Impact

  • Asymmetric edges: on our instance 901/1220 experiences carry derived_from forward links but only 758/1120 trajectories carry the matching backlinks.
  • Silent data loss in the relationship graph; retrieval features that walk backlinks return incomplete results.
  • The File not found variant suggests write_stored_links can also run before a same-batch endpoint file exists (ordering issue inside the same apply path).

Suggested fixes (any of)

  1. Acquire the phase lease over the union of the write set — include trajectory dirs in tree_lock_dirs when links will be written; or
  2. In _append_trajectory_metadata, don't pass the phase lease to write_stored_links (let each endpoint auto-acquire, lease_ref=None); or
  3. Rust-side: when the ambient lease is owned by the same operation, extend/upgrade coverage instead of hard-failing; and/or
  4. At minimum: stop swallowing the exception in write_stored_links — surface a structured error and a retry path so dropped links are visible and recoverable.

OpenViking Version

v0.4.13 (pip wheel)

Python Version

3.12

Operating System

Linux x86_64 (Ubuntu 24.04), systemd user unit, single local server, 6 accounts / ~24k memory files

Model Backend

OpenAI-compatible; not relevant to this code path

neurosovereign avatar Aug 15 '26 16:08 neurosovereign

Thanks for the detailed report and thorough root-cause analysis. We confirmed that this issue existed in the SessionCompressorV2 pipeline used by v0.4.13. Since v0.4.14, session memory extraction has been migrated to the V3 pipeline, and the lock coverage for relationship-link updates has also been stabilized. The legacy V2 path described in this issue has been removed, so the trajectory backlink lease-coverage issue is resolved in newer versions. We have verified the relevant regression coverage. Please upgrade to v0.4.14 or later—preferably the latest release. Thanks again for providing the production observations and detailed investigation. If you encounter similar errors after upgrading, please let us know and we’ll continue investigating.

yeshion23333 avatar Aug 20 '26 09:08 yeshion23333