import { mkdtempSync, rmSync } from "node:fs"; import { readFile, rename } from "node:fs/promises"; import { tmpdir } from "node:os"; import { dirname, join } from "node:path"; import { afterEach, expect, it, vi } from "vitest"; vi.mock("node:fs/promises", async (importOriginal) => { const actual = await importOriginal(); return { ...actual, readFile: vi.fn(async (path, options) => { await pauseRead?.(String(path)); return actual.readFile(path, options); }), rename: vi.fn(async (source, target) => { await pauseRename?.(String(source), String(target)); return actual.rename(source, target); }), }; }); type Storage = typeof import("../storage.ts"); const roots: string[] = []; let pauseRename: | ((source: string, target: string) => Promise) | undefined; let pauseRead: ((path: string) => Promise) | undefined; function temporaryRoot(): string { const root = mkdtempSync(join(tmpdir(), "good-job-claim-test-")); roots.push(root); return root; } function job() { return { version: 2 as const, id: "gj-0000000001", createdAt: "2026-01-01T00:00:00.000Z", kind: "praise" as const, feedback: "gj", source: { type: "session" as const, cwd: "/repo", sessionPath: "/sessions/one.jsonl", sessionId: "session-1", sessionName: "Shared route", leafId: "leaf-1", agentModel: { provider: "provider", id: "worker" }, }, analysisModel: { provider: "provider", id: "worker" }, }; } const storage = () => import("../storage.js") as Promise; async function storagePair(): Promise<[Storage, Storage]> { const first = await storage(); vi.resetModules(); return [first, await storage()]; } afterEach(async () => { pauseRename = undefined; pauseRead = undefined; vi.mocked(rename).mockClear(); vi.mocked(readFile).mockClear(); for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); }); it("shares claim ownership across re-evaluated storage modules during processing read", async () => { const [first, second] = await storagePair(); const paths = first.layout(temporaryRoot()); await first.ensureLayout(paths); await first.enqueueJob(job(), paths); let releaseRead!: () => void; const reading = new Promise((resolve) => { releaseRead = resolve; }); let startedRead!: () => void; const readStarted = new Promise((resolve) => { startedRead = resolve; }); pauseRead = async (path) => { if (dirname(path) !== paths.processing) return; startedRead(); await reading; }; const claiming = first.claimJob("gj-0000000001", paths); await readStarted; await second.recoverProcessing(paths); expect(await second.counts(paths)).toMatchObject({ pending: 0, processing: 1, }); releaseRead(); const claimed = await claiming; expect(claimed?.job).toEqual(job()); if (!claimed) throw new Error("missing claim"); first.releaseClaim(claimed.path); await second.recoverProcessing(paths); expect(await second.counts(paths)).toMatchObject({ pending: 1, processing: 0, }); }); it("allows only one claim while actual rename is delayed", async () => { const [first, second] = await storagePair(); const paths = first.layout(temporaryRoot()); await first.ensureLayout(paths); await first.enqueueJob(job(), paths); let releaseRename!: () => void; const renaming = new Promise((resolve) => { releaseRename = resolve; }); let startedRename!: () => void; const renameStarted = new Promise((resolve) => { startedRename = resolve; }); let paused = false; pauseRename = async (source, target) => { if (paused || source !== join(paths.pending, "gj-0000000001.json")) return; if (dirname(target) !== paths.processing) return; paused = true; startedRename(); await renaming; }; const firstClaim = first.claimJob("gj-0000000001", paths); await renameStarted; let secondClaim: Awaited>; try { secondClaim = await second.claimJob("gj-0000000001", paths); await second.recoverProcessing(paths); } finally { releaseRename(); } const claims = [await firstClaim, secondClaim]; expect(claims.filter((claim) => claim !== undefined)).toHaveLength(1); const claimed = claims.find((claim) => claim !== undefined); if (!claimed) throw new Error("missing claim"); first.releaseClaim(claimed.path); }); it("tolerates a processing claim disappearing during recovery", async () => { const store = await storage(); const paths = store.layout(temporaryRoot()); await store.ensureLayout(paths); await store.enqueueJob(job(), paths); const claimed = await store.claimJob("gj-0000000001", paths); if (!claimed) throw new Error("missing claim"); store.releaseClaim(claimed.path); pauseRename = async (source, target) => { if ( source !== claimed.path || target !== join(paths.pending, "gj-0000000001.json") ) return; const error = new Error( "claim completed concurrently", ) as NodeJS.ErrnoException; error.code = "ENOENT"; throw error; }; await expect(store.recoverProcessing(paths)).resolves.toBeUndefined(); pauseRename = undefined; await store.recoverProcessing(paths); expect(await store.counts(paths)).toMatchObject({ pending: 1, processing: 0, }); }); it("releases ownership when processing read fails", async () => { const store = await storage(); const paths = store.layout(temporaryRoot()); await store.ensureLayout(paths); await store.enqueueJob(job(), paths); pauseRead = async (path) => { if (dirname(path) === paths.processing) throw new Error("read failed"); }; await expect(store.claimJob("gj-0000000001", paths)).rejects.toThrow( "read failed", ); pauseRead = undefined; await store.recoverProcessing(paths); expect(await store.counts(paths)).toMatchObject({ pending: 1, processing: 0, }); });