OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

[Bug]: ingest watch + forked codex rollout files sharing the same session_meta.id thrash the cursor and flood the SessionCommit queue

Open zhangzhangco opened this issue 2 months ago • 0 comments

Summary

With ingest in watch mode for the codex harness, a single session whose rollout log is split across two files that share the same internal session_meta.id (codex fork / continued session) makes the ingest client replay the whole file on every poll and enqueue a SessionCommit task on every poll. The SessionCommit queue grows without bound (~22 tasks/min in my environment) and the session directory accumulates thousands of history/archive_NNN copies.

Two independent defects compound each other:

  1. Cursor thrashing — the byte-offset cursor is keyed by native_session_id, but that id is read from the file content (session_meta.id), so two different files map to the same key. JsonlLogSource treats an inode change as log rotation and resets the offset to 0, so each poll re-reads the file it did not read last time.
  2. No de-duplication before commit — SessionReplayer.commit_if_needed() enqueues a commit whenever the server still reports pending_tokens > 0. When the commit queue is backed up (e.g. queue_workers.session_commit.max_concurrent: 1 with a slow local VLM), pending_tokens never clears within the 5 s poll interval, so the sweep re-enqueues the same session forever.

Environment

  • OpenViking 0.4.20 (openviking-server, installed via uv tool install openviking[bot]==0.4.20), server on macOS (M1 Pro), Python 3.11
  • ingest.enabled: true, harnesses.codex: {enabled: true, mode: watch}
  • queue_workers.session_commit.max_concurrent: 1
  • VLM: local qwen3.8-27b-q5 (≈1 commit/min throughput)
  • ~/.codex/sessions: 183 rollout files

Root cause

A. openviking/ingest/sources/codex.py — session id comes from file content

def session_ref_for_file(self, path: Path) -> SessionRef:
    meta = self._peek_session_meta(path)
    return SessionRef(
        harness=self.name,
        native_session_id=meta.get("id") or path.stem,   # <-- same id for forked files
        ...

Codex writes a second rollout file when a session is forked/continued, and the new file keeps the original session_meta.id. Two files in my case:

rollout-2026-09-02T10-59-44-01a0600e-e891-7b42-8da9-b73b83d46acd.jsonl                          (5,031,397 B)
rollout-2026-09-02T16-05-07-01a0600e-e891-7b42-8da9-b73b83d46acd_01a06126-7ca2-7eb1-b795-40499a2b3ce6.jsonl (4,892,958 B)

Both contain session_meta.id == 01a0600e-e891-7b42-8da9-b73b83d46acd.

B. openviking/ingest/sources/base.py — inode change resets the offset

start = int(cur.value.get("offset", 0))
stored_inode = cur.value.get("inode")
# Rotation (file replaced) or truncation -> re-read from the top.
if (stored_inode is not None and stored_inode != st.st_ino) or start > st.st_size:
    start = 0

Because the one shared cursor row alternates between two files, the stored inode never matches the file being read → start = 0 every poll → the entire file is re-parsed and re-appended. Then the cursor alternates back, and the other file does the same.

C. openviking/ingest/replay.py — commit is not de-duplicated

rec = self.store.get(harness, ref.native_session_id)
need = bool(rec and rec.needs_commit)
pending = await self.client.pending_tokens(sid)
if not need and pending <= 0:
    return False
...
await self.client.commit(sid, keep_recent_count=keep_recent_count)

orchestrator.py calls this for every session on every poll — deliberately, including when nothing was appended:

# Commit even when added == 0: a prior crash may have left appended
# messages un-committed (needs_commit / server pending tokens).
if await self.replayer.commit_if_needed(name, ref, harness_cfg.commit.keep_recent_count):

So while a commit sits in the queue, pending_tokens > 0 and each 5 s sweep enqueues another commit for the same session. The queue can then never catch up.

Evidence

  • SessionCommit queue: 1,605 pending, of which 1,388 belonged to that single native session id. Measured enqueue rate: a steady 22 tasks/min over 15 minutes, all one session (drain rate ≈ 2/min → monotonically growing).
  • ingest_cursor.last_appended_count for that session reached 92,928 (cumulative — i.e. the same ~900-line file was replayed over and over).
  • Session directory grew to 1,388 history/archive_NNN directories / 84 MB; the root messages.jsonl was emptied by the repeated archiving.
  • cursor_value alternated between {"offset": 4892958, "inode": <B>} and {"offset": 5031397, "inode": <A>} — the two file sizes.
  • No extraction errors were reported (queue ... 0 errors); the only visible symptom was the growing queue and the ballooning archive directory.

Minimal reproduction

  1. Have two codex rollout files that share the same session_meta.id (fork/continue a codex session, or copy one rollout file and keep its session_meta as-is).
  2. Configure ingest with harnesses.codex.mode = watch and start openviking-server ingest run.
  3. Watch ~/.openviking/data/_system/queue/queue.db (queue_messages where queue_name='SessionCommit') and the session's history/ directory: both grow on every 5 s poll even though nothing in ~/.codex/sessions changed.

Suggested fixes

  1. Make the cursor key unique per file. Either derive native_session_id with a per-file discriminator (e.g. append the extra UUID that codex puts in the forked filename) or key the cursor by (locator, inode) rather than by session id.
  2. De-duplicate commits. commit_if_needed() should not enqueue a new commit while one for the same session may still be in flight — e.g. commit only when the local cursor store reports needs_commit, or record "commit requested at T for session S" and skip until it is confirmed (or a timeout expires). The current pending_tokens > 0 test is not a safe signal, because it is server-side and lags the queue by minutes.
  3. Add a safety valve / observability. Warn (or refuse) when the same session is committed more than N times in M minutes, and consider capping archive generations per session.

Workaround I applied locally

Two small patches that stop the runaway; happy to open a PR if useful:

  • sources/codex.py: when the rollout filename contains a second UUID besides session_meta.id, append it to the session id → each file gets its own cursor.
  • replay.py commit_if_needed(): return early unless the local cursor store has needs_commit, so the periodic sweep no longer re-commits only because the server still reports pending tokens.

After both patches the queue drains monotonically and the cursor stays stable ({"offset": 5031397, "inode": <A>}, unchanged across polls).

Related issues

  • #4333 (codex-memory-plugin re-sends whole transcript), #4029 (archive GC), #4494 (retry a session whose commit fails) — related symptoms, different root cause.

zhangzhangco avatar Sep 15 '26 06:09 zhangzhangco