Repository navigation
fix(checkpoint): tolerate corrupt checkpoints.jsonl lines and write it atomically - #70
Draft
posthog[bot] wants to merge 2 commits into
Draft
posthog[bot] wants to merge 2 commits into
posthog[bot] wants to merge 2 commits into
Conversation
…t atomically One bad line in checkpoints.jsonl failed every read of the working log, so every later checkpoint for the base commit failed and AI attribution was lost. - read_all_checkpoints skips a line that does not parse and logs a warning with the file path and line number. - write_all_checkpoints writes to a unique temp file, syncs it, and renames it over checkpoints.jsonl. - append_checkpoint no longer replaces a read error with an empty list, which silently dropped earlier checkpoints. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Generated-By: PostHog Desktop Task-Id: fa1ed62d-c93a-41fe-9a92-44e47a8f3ecb
- append_checkpoint uses mutate_all_checkpoints, so it has the same read error handling as the other read-modify-write path. - read_all_checkpoints uses split(b'\n').enumerate() and not a manual read_until loop. - The atomic write helper does the rename, and calls flush plus get_ref().sync_all(). - Remove a temp-file test assertion that cannot fail. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Generated-By: PostHog Desktop Task-Id: fa1ed62d-c93a-41fe-9a92-44e47a8f3ecb
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
checkpoints.jsonlstops all AI checkpoints for the base commit. Each checkpoint fails withIO error: expected value at line 1 column 1, so AI attribution for that commit is lost. Seen on one Windows install (CLI 2.1.2).read_all_checkpointsreturns an error on the first line that does not parse.statusand post-commit read the same file, so they fail too.append_checkpointcallsread_all_checkpoints().unwrap_or_default(). When the read fails, it writes back only the new checkpoint and silently drops all earlier checkpoints.write_all_checkpointstruncates the file withFile::createand rewrites it in place. A crash or a concurrent writer during the rewrite can leave junk bytes (for example NUL padding).Origin
ce859d0Changes
read_all_checkpointsreads raw bytes line by line. A line that does not parse (bad JSON, NUL padding, bad UTF-8) is skipped. Awarnlog records the file path, line number and parse error.write_all_checkpointswrites to a unique temp file (checkpoints.jsonl.<pid>.<n>.tmp), callssync_all, then renames it overcheckpoints.jsonl. If a step fails, it removes the temp file.append_checkpointnow returns the read error with?. A bad line no longer causes a read error, so only real IO failures get to the caller.checkpoint mock_aiappend_checkpointwith a bad lineWarning
Each checkpoint write now calls
fsync. This can add some latency per checkpoint on slow disks (mainly Windows). The write already rewrote the full file each time, so the change in I/O is small.Tests
tests/integration/repo_storage_unit.rs. All three fail without the fix:test_read_all_checkpoints_skips_corrupt_lines: NUL padding, bad UTF-8 and truncated JSON lines are skipped. Valid lines are kept.test_append_checkpoint_keeps_earlier_checkpoints_past_corrupt_line: earlier checkpoints are kept and the bad line is removed on rewrite.test_ai_checkpoint_succeeds_with_corrupt_checkpoint_line: end to end.checkpoint mock_aisucceeds and the commit has the correct AI attribution.cargo clippy --all-targets -- -D warningsandcargo fmtpass.repo_storage_unitpasses (18/18) in daemon mode.integrationbinary in this sandbox: 3052 passed, 81 failed. None of the failures are from this change:gitwrapper blocksgit commit(bash_tool_provenance, ignore_unit bare repo, commit_post_stats_benchmark, the doctor self-check).gtCLI, which the sandbox does not have.git commitreason (notes_api, notes_migrate, ci_context, repository). None of them touch checkpoints.CI=trueto stop the superuser warning from breaking blame output checks.Agent context
mdm::utils::write_atomic. It needs the full file in memory and uses a fixed.tmpname, so two concurrent writers could use the same temp file.Created with PostHog Desktop from this inbox report.
🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.