Repository navigation
fix(agents): append file chunks in place to stop quadratic prefix recopying - #462
Merged
mikamikasuki merged 1 commit intoOct 27, 2026
Merged
Conversation
…opying write_file_chunk() copied the whole accumulated prefix into a fresh temporary file for every chunk, so writing an N-byte output moved O(N^2/4000) bytes: the issue's 4 MiB reproduction recopied about 524x the final file size across 1,048 copy operations (1.04 s locally). Chunks are now appended in place through an O_APPEND descriptor and fsynced, so total file I/O grows linearly (same reproduction: 0.02 s, no prefix bytes recopied). The crash contract keeps per-chunk fsync, with one documented narrowing: a process death between the chunk write and its fsync can leave a truncated final chunk. A replayed call repairs that clean torn tail by comparing the stored bytes against the chunk prefix, truncating back to the chunk offset and rewriting; a mismatching tail is refused so a concurrent edit is never silently overwritten. The directory fsync now happens only when a chunk creates the file, since appends no longer change directory entries. Owner-edit detection moved to an fstat of the descriptor the chunk is written through, taken before any bytes land, preserving the same detect-and-abort window as the previous stat-before-rename check. Fixes mikamikasuki#413 Co-authored-by: ZCode Scout <noreply@zcode.local> Signed-off-by: NefelibateLG <3453510037@qq.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.
Summary
write_file_chunk()copied the whole accumulated prefix into a fresh temporary file for every chunk, so writing an N-byte output moved O(N²/4000) bytes — the issue's 4 MiB reproduction recopied about 524× the final file size across 1,048 copy operations. This appends chunks in place and keeps total file I/O linear.What changes
O_APPENDdescriptor and are fsynced per chunk; no per-chunk temp file, prefix recopy, or rename.fstatof the same descriptor the chunk is written through, taken before any bytes land — the same detect-and-abort semantics as the previous stat-before-rename check, over a smaller window.Crash-safety tradeoff (explicit)
Previously a crash could never leave partial content in the target: the rename published only complete prefixes. With in-place appends, a process death between the chunk write and its
fsynccan leave a truncated final chunk. The replay protocol repairs that deterministically: a replayed call compares the stored tail against its own chunk prefix, truncates back to the chunk offset and rewrites. A tail that does not match is refused (Replayed chunk does not match the durable tail; read the file before retrying), so a concurrent edit is never silently overwritten. Serialreplay=Falsebehavior is unchanged: any size/offset mismatch still errors and asks for a fresh read.How verified
pip install -r requirements.lock.txt && pip install --no-deps -e .(via uv).ed62f2cbefore edits:python -m pytest -q tests/test_agent_output_recovery.py tests/test_agent_progress_recovery.py tests/test_agent_goal_provenance.py→ 20 passed; identical result on this branch.tests/test_agent_code_writes.py(12 tests): chunk reassembly, parent creation, mode preservation, multibyte byte-exact round-trip, offset/validation errors, replay skip of durable chunks, clean torn-tail repair, refused mismatching tail, torn tail withoutreplaystill rejected, owner-edit-between-calls detection, and an instrumented run asserting the writer never reopens the growing file for reading.Fixes #413 (serial-path I/O only; the concurrent owner-edit window tracked as #368 is intentionally untouched).
Co-authored-by: ZCode Scout noreply@zcode.local (commit)