fix: persistence safety — resume status, test isolation, write race - #134
Merged
Conversation
Three critical fixes for production data loss: 1. Resume doesn't clear exited status: createSession() now passes status: "running" to persistSession(), so the merge logic no longer preserves the stale "exited" status on resume. 2. Test isolation: Tests from PR #131 wrote to production sessions.json. All test files now use isolated temp directories via _setConfigDirForTesting() / _resetCacheForTesting(). Added getConfigDir() with test override support to configDir.ts. 3. Write race on sessions.json: Added in-memory cache to persisted.ts — readSessions() populates once, all mutations go through the cached array, eliminating TOCTOU races. Added backup-on-shrink before destructive writes. getPersistedSessions() returns a defensive copy to prevent external cache corruption. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
aterrylu
force-pushed
the
terry/persistence-safety
branch
from
April 14, 2026 08:15
1bc9c29 to
b67aae8
Compare
aterrylu
marked this pull request as ready for review
April 14, 2026 08:15
nox-0x
approved these changes
Apr 14, 2026
nox-0x
left a comment
Collaborator
There was a problem hiding this comment.
Good, thorough fix. The in-memory cache is the right approach for the TOCTOU race, and explicitly setting status: "running" closes the resume bug cleanly. Test isolation to temp dirs is much safer — good call.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three critical persistence fixes that caused data loss in production:
createSession()now explicitly setsstatus: "running"when callingpersistSession(), so resumed sessions clear their stale "exited" status instead of preserving it via the??merge fallback_setConfigDirForTesting()instead of writing to production~/.autonomos/sessions.json(which corrupted real agent data)persisted.ts— eliminates TOCTOU races where concurrentmarkSessionExited()andpersistSession()calls could overwrite each other. Added backup-on-shrink before destructive writesChanges
sessions.tsstatus: "running"topersistSession()callconfigDir.tsgetConfigDir()with test override supportpersisted.tsgetPersistedSessions()backward-compat.test.tsorgChart.test.tsduplicate-name.test.tsArchitecture
graph TD A[createSession] -->|status: running| B[persistSession] C[markSessionExited] --> D[readSessions - cached] B --> D D -->|first call| E[loadFromDisk] D -->|subsequent| F[return sessionsCache] B --> G[writeSessions] C --> G G -->|count decreased| H[copyFileSync backup] G --> I[writeFileSync] I --> J[sessionsCache = sessions]Test plan
make check)sessions.jsonnot modifiedstatus: "running"explicitly set on session creation/resumegetPersistedSessions()returns defensive copy🤖 Generated with Claude Code