fix(gateway): make rewrite_transcript() JSONL write atomic - #20012
Closed
vominh1919 wants to merge 1 commit into
Closed
fix(gateway): make rewrite_transcript() JSONL write atomic#20012vominh1919 wants to merge 1 commit into
vominh1919 wants to merge 1 commit into
Conversation
rewrite_transcript() overwrites the JSONL transcript file with a plain open(..., 'w') + loop. If the process is killed mid-write (SIGKILL, OOM, power loss), the file is left truncated, causing load_transcript() to load an incomplete conversation history. Use the same tempfile + fsync + atomic_replace pattern already used by _sessions_save() in the same file. This ensures the old file is preserved until the new one is fully written and synced to disk.
Collaborator
3 tasks
Contributor
|
This looks implemented on current main by the later DB-only transcript refactor. Automated hermes-sweeper review found that the JSONL rewrite path this PR hardens no longer exists.
Thanks for the crash-safety fix; the same failure mode is now addressed by removing the JSONL rewrite/load path entirely rather than making that legacy write atomic. |
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.
Problem
SessionStore.rewrite_transcript()overwrites the JSONL transcript file with a plainopen(..., 'w')+ loop. If the process is killed mid-write (SIGKILL, OOM, power loss), the file is left truncated.load_transcript()then loads incomplete conversation history, which can cause the agent to lose context.This function is called by
/retry,/undo, and/compress— operations where losing history is particularly harmful.Fix
Replace the direct
write_textwith the sametempfile+fsync+atomic_replacepattern already used by_sessions_save()in the same file. The old file is preserved until the new one is fully written and synced to disk.Before vs After
/retry/compressTests
This is a crash-safety fix. The atomic write pattern (
tempfile.mkstemp+os.fdopen+fsync+atomic_replace) is already battle-tested in_sessions_save()at line 722 of the same file.