Repository navigation
[lib] Write the Codex session cache atomically (#689) - #1102
Conversation
(cherry picked from commit 0911062)
(cherry picked from commit 25cf716)
(cherry picked from commit 3d34b96)
… write
Both community fixes wrote the temp file and renamed it inline. The repo
already has lib/atomic-write.ts for exactly this, used by a dozen other
state files, so writeCacheEntry now calls writeJsonAtomically. It also
gives the temp file a random suffix beside the pid, and makes the cache
0600 like the rest of ~/.failproofai/state.
Neither PR tested the bug itself: that a write which fails halfway leaves
the previous cache whole. The new test makes writeFileSync write half its
data and then fail with ENOSPC. It fails on main, where the half-written
JSON makes readCache() start over from {}, and passes with the fix.
The two happy-path tests from the cache test file are dropped. The test in
codex-sessions.test.ts already checks the same things: the entry is
readable and no .tmp file is left. The afterEach also restores
FAILPROOFAI_HOME rather than deleting it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Caution Review failedThe pull request is closed. No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
In a read-only state dir the temp file is never created, so the test passed even with the helper's cleanup removed. What it does check is that the lookup still returns the transcript when the cache cannot be written. The test is renamed to say that, and asserts the return value. The halfway-write and rename-failure tests are the ones that catch a missing cleanup. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…3b4b # Conflicts: # CHANGELOG.md
Closes #689
findCodexTranscriptcaches the sessionId → transcript path map in~/.failproofai/state/codex-session-paths.json.writeCacheEntrywrote that file in place, so a write that died partway (a full disk, a killed hook process) left half a JSON document. The nextreadCache()failed to parse it and returned{}, and the next write replaced the file with only the new entry, which lost every earlier one. The cache is now written to a temp file in the same directory and renamed over the old one.This combines the two community PRs for the issue. Their commits are cherry-picked (
-x) with their authors kept.From @AshSgDe29071999 (#692)
.tmpbeside the cache, rename it over the cache, and remove the temp file if anything fails.__tests__/lib/codex-sessions.test.ts: the cache entry is readable after a lookup and no.tmpfile is left. This test is unchanged in the final diff.From @lakshya-dhariwal (#847)
__tests__/lib/codex-sessions-cache.test.tsand its setup, with the rename-failure test: a directory sits where the cache file belongs, the rename fails after the temp write, and no.tmpfile may be left.Both inline fixes are in the branch history. In the final diff,
writeCacheEntryinstead calls the existing shared helper (next section).Added here
writeCacheEntrycallswriteJsonAtomicallyfromlib/atomic-write.ts. That is the same temp-file-and-rename, already used by 11 other modules, and the file's header says it exists so inline copies stop drifting. The helper adds a random suffix to the temp name.writeFileSyncis made to write half its data and fail withENOSPC, and the earlier cache must still parse with its entries intact.afterEachnow restoresFAILPROOFAI_HOMEinstead of deleting it.CHANGELOG.mdentry.How the claims were checked
main, the regression test fails withSyntaxError: Unterminated string in JSON, so the cache file held half a document. It passes with the contributors' inline version and with the helper.0500directory, so as root it passes trivially.lib/codex-sessions.tsand both test files passeslint, plus atscrun scoped to those files under the repo'stsconfig.json.git merge-treeagainst the open PRs that also touchlib/codex-sessions.ts: [hooks] Block agent edits to its own hook settings (#1794) #1053 merges cleanly. feat(audit): detect leaked credentials and get them in front of the user #789 has no conflict in this file, and its conflicts elsewhere are withmainas well.Worth a look: file modes
On an actual write at umask
0002,mainleaves the cache0664and a newly created state dir0775. Through the helper they are0600and0700, and so is~/.failproofaiif the cache write is what creates it. That matchesbackfill-request.json(written through the same helper) andtelemetry-id(written0600) in the same directory. The JSON is now pretty-printed, andreadCache()parses an existing compact file the same way.Merging: the
mainruleset allows only squash merges, so the squash commit should carryCo-authored-bytrailers for AshSgDe29071999 and lakshya-dhariwal, as #1067's did. Their original commits stay in this PR's history.Type of Change
Checklist
All four were confirmed by CI on
ff122c45(thequality,test×3,test-e2eandbuildjobs).npm run lintpassesnpx tsc --noEmitpassesnpm run test:runpassesnpm run buildsucceeds🤖 Generated with Claude Code