Conversation
Previously, `persist_conversation` wrote files directly into the
conversation directory and `write_json` wrote directly to the target
path. A process crash at any point during a persist could leave a
partially-written conversation on disk with no way to recover.
This change introduces two levels of atomicity:
At the file level, `write_json` now writes to a sibling `.tmp` file,
flushes, then renames over the target. If anything fails before the
rename, the original file is untouched and the temp file is removed.
At the directory level, `persist_conversation` now writes all managed
files into a `.staging-{name}` directory first, copies non-managed files
(e.g. `QUERY_MESSAGE.md`) from the existing conversation dir into the
staging dir, renames the existing dir to `.old-{name}`, then renames the
staging dir to the final name in a single syscall. Readers never see a
partially-written directory. The `.old-` backup is removed as a final
step.
On the next startup, the validation pass calls `cleanup_staging_dirs` to
detect and handle any crash remnants: orphaned staging dirs are removed,
orphaned `.old-` backups are removed, and a crash that occurred between
steps 3 and 4 (both `.old-X` and `.staging-X` exist, `X` missing) is
rolled back by renaming `.old-X` back to `X`. Orphaned `.tmp` files
inside conversation dirs are also cleaned up.
Signed-off-by: Jean Mertz <git@jeanmertz.com>
Signed-off-by: Jean Mertz <git@jeanmertz.com>
Signed-off-by: Jean Mertz <git@jeanmertz.com>
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.
Previously,
persist_conversationwrote files directly into the conversation directory andwrite_jsonwrote directly to the target path. A process crash at any point during a persist could leave a partially-written conversation on disk with no way to recover.This change introduces two levels of atomicity:
At the file level,
write_jsonnow writes to a sibling.tmpfile, flushes, then renames over the target. If anything fails before the rename, the original file is untouched and the temp file is removed.At the directory level,
persist_conversationnow writes all managed files into a.staging-{name}directory first, copies non-managed files (e.g.QUERY_MESSAGE.md) from the existing conversation dir into the staging dir, renames the existing dir to.old-{name}, then renames the staging dir to the final name in a single syscall. Readers never see a partially-written directory. The.old-backup is removed as a final step.On the next startup, the validation pass calls
cleanup_staging_dirsto detect and handle any crash remnants: orphaned staging dirs are removed, orphaned.old-backups are removed, and a crash that occurred between steps 3 and 4 (both.old-Xand.staging-Xexist,Xmissing) is rolled back by renaming.old-Xback toX. Orphaned.tmpfiles inside conversation dirs are also cleaned up.