Skip to content

Commit 3307261

Browse files
committed
fix(log-guard): key the inspection cache on file identity, not size+mtime
Re-review showed size:mtimeMs is not an identity: an atomic replace (write a new file, rename over the old) can preserve both, and the cache then served the previous schema/capability verdict indefinitely. That trades a repeated-scan cost for a persistent wrong answer, which is the worse failure. The key now includes dev+ino (a replaced file is a different inode) and nanosecond mtime/ctime (an in-place rewrite inside one millisecond still invalidates), via statSync(path, { bigint: true }). Regression drives the exact case: replace the database with a same-size non-database file, restore the original mtime, and assert the next inspection is neither the cached object nor still 'compatible'.
1 parent bd73793 commit 3307261

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

‎src/codex/log-guard/inspect.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,21 @@ let inspectionCache: InspectionCacheEntry | null = null;
181181
function inspectionCacheKey(databasePath: string): string {
182182
const stamp = (path: string): string => {
183183
try {
184-
const stat = statSync(path);
185-
return `${stat.size}:${stat.mtimeMs}`;
184+
// `size:mtimeMs` alone is not an identity. An atomic replace (write a new
185+
// file, rename over the old one) can preserve both, and the cache then
186+
// served the previous schema/capability verdict indefinitely — trading a
187+
// repeated-scan cost for a persistent wrong answer. Include the inode and
188+
// device so a replaced file is a different key even at identical size and
189+
// mtime, plus nanosecond ctime/mtime so an in-place rewrite inside one
190+
// millisecond still invalidates.
191+
const stat = statSync(path, { bigint: true });
192+
return [
193+
stat.dev,
194+
stat.ino,
195+
stat.size,
196+
stat.mtimeNs,
197+
stat.ctimeNs,
198+
].join(":");
186199
} catch {
187200
return "-";
188201
}

‎tests/codex-log-guard-inspect.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import {
44
mkdirSync,
55
mkdtempSync,
66
readdirSync,
7+
renameSync,
78
rmSync,
89
statSync,
10+
utimesSync,
911
unlinkSync,
1012
writeFileSync,
1113
} from "node:fs";
@@ -445,4 +447,28 @@ describe("Codex Log Guard inspection", () => {
445447
resetCodexLogGuardInspectionCache();
446448
expect(inspectCodexLogs({ codexHome: root })).not.toBe(first);
447449
});
450+
test("an atomic replacement at identical size and mtime still invalidates", () => {
451+
// size:mtimeMs is not an identity. Writing a new file and renaming it over
452+
// the old one can preserve both, and the cache then served the previous
453+
// schema verdict indefinitely - a persistent wrong answer, which is worse
454+
// than the repeated scan the cache exists to avoid.
455+
const root = makeRoot();
456+
const databasePath = join(root, "logs_2.sqlite");
457+
createCurrentLogsDb(databasePath);
458+
459+
resetCodexLogGuardInspectionCache();
460+
const before = inspectCodexLogs({ codexHome: root });
461+
expect(before.schema.state).toBe("compatible");
462+
463+
const original = statSync(databasePath);
464+
const replacement = join(root, "replacement.tmp");
465+
// A file of the same byte length that is NOT a usable database.
466+
writeFileSync(replacement, Buffer.alloc(original.size, 0x41));
467+
renameSync(replacement, databasePath);
468+
utimesSync(databasePath, original.atime, original.mtime);
469+
470+
const after = inspectCodexLogs({ codexHome: root });
471+
expect(after).not.toBe(before);
472+
expect(after.schema.state).not.toBe("compatible");
473+
});
448474
});

0 commit comments

Comments
 (0)