Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- One directory `repo_search` could not read (a database volume owned by another user, for one) failed the whole search with the raw ripgrep command line, and the matches found everywhere else were thrown away. Those matches are now returned.
- A broad `repo_search` (a few thousand matching lines) failed as "returned too many matches" instead of returning its first 100 marked `truncated: true`. A query containing a line break failed with the raw ripgrep command line; it now says that a search matches one line at a time.
- `repo_read_file` and the repo write tools refused a top-level file whose name starts with two dots, such as `..notes.md`, as escaping the repository.
- Reading a task, session or receipt key while another prodex process updated it could fail as "Target path changed during read file operation", without naming the file. Records are replaced by renaming a new file over them, and the read refused when the file it had opened was no longer the one at the path. Found 2026-11-09: with one process rewriting a session, 171 of about 1,100 reads from another failed; `prodex tasks list`, `sessions show` and MCP `bridge_list_tasks` failed the same way while a consult was updating its records. Such a read is now made again, up to five times, with every check repeated; a file swapped for a symlink is still refused at once.

## 0.40.50

Expand Down
30 changes: 26 additions & 4 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1062,9 +1062,11 @@ export class BridgeStore {
// receipts, later lines are legacy keys kept so pre-rotation receipts verify.
private async readReceiptIntegrityKeys(): Promise<string[]> {
const lines = (
await readVerifiedUtf8File(this.receiptIntegrityKeyPath(), () => this.assertReceiptIntegrityKeyTargetSafe({ allowMissing: false }), {
mode: BRIDGE_FILE_MODE
})
await readReplacedFileText(() =>
readVerifiedUtf8File(this.receiptIntegrityKeyPath(), () => this.assertReceiptIntegrityKeyTargetSafe({ allowMissing: false }), {
mode: BRIDGE_FILE_MODE
})
)
)
.split(/\r?\n/)
.map((line) => line.trim())
Expand Down Expand Up @@ -1188,7 +1190,7 @@ export class BridgeStore {
if (options.cleanupTempHardLinks ?? true) {
await this.cleanupRecordTempHardLinks(kind, filePath);
}
const raw = await readVerifiedUtf8File(filePath, () => this.assertRecordTargetInside(kind, filePath));
const raw = await readReplacedFileText(() => readVerifiedUtf8File(filePath, () => this.assertRecordTargetInside(kind, filePath)));
try {
return JSON.parse(raw);
} catch (error) {
Expand Down Expand Up @@ -1675,6 +1677,26 @@ function recordLabel(kind: BridgeRecordKind): string {
}
}

// Records and the receipt key are replaced by renaming a new file over them,
// and a read that opened the old file and then found the new one at the path
// failed as "Target path changed during read file operation": found
// 2026-11-09, 115 of 908 reads failed while another process rewrote a
// session, and `tasks list` failed the same way during a consult. Reading
// again opens the new file and runs every check again; any other failure,
// a symlink swap among them, is not retried.
const REPLACED_FILE_READ_ATTEMPTS = 5;

export async function readReplacedFileText(read: () => Promise<string>): Promise<string> {
for (let attempt = 1; ; attempt += 1) {
try {
return await read();
} catch (error) {
const replaced = error instanceof Error && error.message === "Target path changed during read file operation";
if (!replaced || attempt >= REPLACED_FILE_READ_ATTEMPTS) throw error;
}
}
}

function isErrorCode(error: unknown, code: string): boolean {
return typeof error === "object" && error !== null && "code" in error && (error as { code?: unknown }).code === code;
}
Expand Down
73 changes: 73 additions & 0 deletions tests/record-read-during-replace.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { mkdtemp } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
import { BridgeStore, readReplacedFileText } from "../src/store.js";

// Found 2026-11-09: records are replaced by renaming a new file over them, and
// a read that opened the old file then found the new one at the path failed as
// "Target path changed during read file operation". With another process
// rewriting a session, 115 of 908 reads failed; `tasks list` and MCP
// bridge_list_tasks failed the same way during a consult.
const replaced = () => new Error("Target path changed during read file operation");

describe("reading a record that is being replaced", () => {
it("reads again when the file was replaced during the read", async () => {
let calls = 0;
const text = await readReplacedFileText(async () => {
calls += 1;
if (calls < 3) throw replaced();
return "new record";
});
expect(text).toBe("new record");
expect(calls).toBe(3);
});

it("does not read again for any other failure", async () => {
let calls = 0;
await expect(
readReplacedFileText(async () => {
calls += 1;
throw new Error("Target path is a symlink or changed during read file operation");
})
).rejects.toThrow(/symlink/);
expect(calls).toBe(1);
});

it("gives up after a few attempts", async () => {
let calls = 0;
await expect(
readReplacedFileText(async () => {
calls += 1;
throw replaced();
})
).rejects.toThrow(/changed during read/);
expect(calls).toBe(5);
});

// POSIX renames over a file another handle is reading. Windows refuses that
// rename with EPERM while the reader has the file open, so there the writer
// fails instead (a separate limitation, not this one).
it.skipIf(process.platform === "win32")("lists sessions while another store keeps rewriting one", async () => {
const root = await mkdtemp(path.join(tmpdir(), "prodex-record-replace-"));
const writer = new BridgeStore(root, { registerRoot: false });
const reader = new BridgeStore(root, { registerRoot: false });
const session = await writer.writeSession({ direction: "codex_to_chatgpt", backend: "chatgpt-control", status: "running" });
let writing = true;
const rewrites = (async () => {
for (let tick = 0; writing; tick += 1) {
await writer.writeSession({ id: session.id, direction: "codex_to_chatgpt", backend: "chatgpt-control", status: "running", warnings: [`tick ${tick}`] });
}
})();
const failures: string[] = [];
try {
for (let read = 0; read < 150; read += 1) {
await reader.getSessionReadOnly(session.id).catch((error: Error) => failures.push(error.message));
}
} finally {
writing = false;
await rewrites;
}
expect(failures).toEqual([]);
});
});
Loading