OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

[Bug]: Fresh pending replay claims inherit the stale backlog timestamp

Open ktz03 opened this issue 1 month ago • 0 comments

Issue Origin

Observed or reproduced in a real environment

Bug Description

A freshly claimed local pending operation can be recovered and sent again by another replay immediately. claimForReplay() renames .json to .processing, preserving the file's old mtime, while recoverStaleProcessing() interprets that mtime as the age of the claim. A backlog file older than the ten-minute recovery threshold consequently looks abandoned during its first active HTTP request.

This is reproducible for both addMessage and commitSession through the shared plugin queue's public replayPending() entrypoint. With the first loopback request held open, two separate Node processes send an eleven-minute-old queue item twice. Otherwise identical zero- and nine-minute fixtures send once.

Steps to Reproduce

  1. Use current main ee2bb95e75251a3b44b9751377354fbe6708428b and Node 24.21.0.
  2. Enqueue one operation in an isolated pending directory and set its .json mtime to eleven minutes ago with fs.utimes().
  3. Start replay and hold its first real HTTP response open.
  4. Start a second replay using the same pending directory, then release the first response.
  5. Observe two identical write requests. Use a zero- or nine-minute mtime as a normal control: only one request is sent.

The fixture timestamp avoids an eleven-minute sleep; it does not replace the real rename, replay or HTTP operations.

Expected Behavior

A newly acquired claim should be considered fresh independently of how long the operation waited in the offline backlog. A concurrent replay should not recover it based on its pre-claim file age. Genuine stale processing orphans should remain recoverable.

Actual Behavior

The new .processing file retains the old backlog mtime. The second replay renames that active claim back to .json, claims it and sends the same payload before the first request finishes. Both replay runs report success.

Minimal Reproducible Example

Save this script outside the checkout and run it from the repository root. It uses the existing test fixture helpers for an isolated directory and real loopback HTTP server:

// Run from the OpenViking repository root: node /path/to/minimal-repro.mjs
import { utimes } from "node:fs/promises";
import { join, resolve } from "node:path";
import { pathToFileURL } from "node:url";
const fromRepo = (path) => pathToFileURL(resolve(path));
const { enqueue, replayPending } = await import(fromRepo("examples/memory-plugin-shared/lib/pending-queue.mjs"));
const { createOvHttp } = await import(fromRepo("examples/memory-plugin-shared/lib/ov-http.mjs"));
const { withPendingDir, withMockOpenViking, writeJson } = await import(fromRepo("examples/memory-plugin-shared/testing/support.mjs"));

await withPendingDir(async (dir) => {
  const queued = await enqueue("addMessage", "old-backlog", { role: "user", content: "one queued message" });
  const old = new Date(Date.now() - 11 * 60_000);
  await utimes(join(dir, queued.path), old, old); // Isolated fixture; no eleven-minute sleep.
  let arrived, release;
  const firstArrived = new Promise((resolve) => { arrived = resolve; });
  const held = new Promise((resolve) => { release = resolve; });
  let count = 0;
  await withMockOpenViking(async (req, res) => {
    for await (const chunk of req) {} // Drain the actual HTTP body.
    if (++count === 1) { arrived(); await held; }
    writeJson(res, { status: "ok", result: {} });
  }, async (baseUrl) => {
    const fetchJSON = createOvHttp({ baseUrl }, { defaultTimeoutMs: 10_000 });
    const first = replayPending(fetchJSON, () => {});
    await firstArrived;
    try { await replayPending(fetchJSON, () => {}); } finally { release(); }
    await first;
    console.log({ requests: count, expected: 1 });
  });
});

Error Logs

No transport error is required. Current main prints:

{ requests: 2, expected: 1 }

OpenViking Version

main ee2bb95e75251a3b44b9751377354fbe6708428b

Python Version

Not used by this reproduction; Node.js 24.21.0

Operating System

Windows

Model Backend

Other — local HTTP fixture, no model invocation

Additional Context

Proposed narrow correction: refresh the pending file's mtime immediately before the existing atomic rename, and send only after a successful claim. Keep JSON createdAt ordering/TTL, retry counters, payload, identity and the ten-minute orphan-recovery policy unchanged. Refreshing the processing file only after rename would expose an old-timestamp recovery window. This does not add exactly-once delivery or extend claims held beyond the existing lease.

Related parked draft #3251 changes queue scope and lifecycle barriers; its current 7600e305 canonical queue still claims with rename alone. The same aged-claim cases also duplicate using that unchanged queue source. #5666/#5674 concern OpenCode host flush serialization and do not change local pending-file claim timestamps.

Integration / Agent memory plugins is the owning area. This report precedes implementation because the contributing guide requests an issue for queue-ownership changes. Validation so far uses actual filesystem and loopback HTTP on Windows, including two independent worker processes; no host UI, deployed server, model, Linux result or performance claim is made.

ktz03 avatar Oct 06 '26 06:10 ktz03