Repository navigation
fix(tools): reject stale whole-file writes - #3656
Conversation
Request changesThis closes a real hole: whole-file writes ( One thing to fix before merge: when a whole-file write is rejected, the error tells the model to "reissue the edit against" the content it returns — but for a whole-file write that content is only the first ~25 lines of the file, not the file. A model that follows the instruction literally rewrites a 500-line file from a 25-line excerpt, and because the rejection re-anchors the ledger, the truncating retry succeeds. That's the same data loss this PR exists to prevent, just one step later. The fix is wording: for a whole-file write, tell the model to re-read the file rather than to reuse the excerpt. Smaller points (details below): stale rejections don't reach the write audit log the way every other rejection in these tools does, and the spec page lists the guarded tools but leaves one out. Real-world evidenceN/A — no 🔍 Technical detailsIssues🟡 Stale-write guidance is edit-specific and invites a truncated overwrite ( For a whole-file write, 🟢 Stale rejections bypass the write audit log ( Every other rejection in these tools calls 🟢 Stale error from Every other error return in that tool carries 🟢 Spec omits
Strengths
|
# Conflicts: # src/gaia/agents/tools/file_io_tools.py
|
Merged current The conflicts were the three whole-file writers in 🔍 Technical details
|
|
Refreshed this branch against current The update was a clean merge: nothing was resolved by hand, and no commit on this branch was rewritten. CI now runs against an up-to-date base. |
A stale-write rejection returned without touching the write audit log, so an overwrite blocked by the read ledger left no trace next to every other denial. The search mixin's stale error also dropped the operation key its other errors carry, and the spec omitted edit_python_file from the guarded list.
|
Picked up the three open points from the review and refreshed the branch against current A blocked overwrite now leaves an audit trail. Previously, when the read ledger rejected a stale whole-file write, it returned without recording anything — so the one denial that means "someone else's work was nearly lost" was the only denial missing from the write audit log. It now records as The earlier truncated-overwrite concern was already handled by 🔍 Technical details
Verified locally on Python 3.12.3:
Also checked against |
itomek
left a comment
There was a problem hiding this comment.
Approving. This closes both halves of #3568 — the write tools now check the read ledger before overwriting, and replace_function / update_gaia_md now record their writes so the next edit isn't rejected as someone else's change.
CI matches main exactly: the two test_chat_system_prompt_owner failures are main's own breakage (a deleted openai_provider module still imported by that test, from #3899), not this branch. The py3.12 leg died on a runner shutdown signal at 38% with no test failures before it. py3.10 ran the full suite: 14312 passed, with only those two pre-existing failures.
main's amd#3656 added a content-hash stale-write guard to the very tools this branch re-implements. `FileReadRecord` already refuses both cases amd#3656 cared about — a file changed on disk since the read, and (unlike amd#3656) one never read at all — so the hash ledger goes and the read record stays. Two things amd#3656 contributed that the auto-merge would have dropped: - **The denied-write audit entries.** Every refusal now logs `audit_write(..., "denied", <error_type>)` the way the stale guard did, so a refused overwrite is still visible in the audit trail. Covered by TestRefusalIsAudited. - **Nothing else.** `tests/unit/agents/test_file_write_state.py` is deleted: it pins the old contract, including `test_unread_existing_file_still_writes`, which is exactly the behaviour this branch removes. Three call sites in file_io_tools.py and file_tools.py auto-merged main's `check_file_state` block in *alongside* the branch's `reads.refusal(...)` without conflicting — duplicate guards that only the import error surfaced. Docs: the "Concurrent file changes" section still described `stale: true` and content hashes, neither of which this branch returns; it now points at "Read before edit". Same for the file-search mixin's cross-reference.
Whole-file writes could overwrite changes made after an agent read a file. The existing stale-edit guard now covers every shared file mutation path, returning current content before any backup or write. Closes #3568.
Test plan
🔍 Evidence and limits
Real temporary files exercise external edits, rejected writes without side effects, corrected retries and ordinary creation. This reuses the existing process-local tracker; it does not claim cross-process atomic check-and-write.
Follow-up: stale-write errors explicitly identify bounded excerpts and require a full read before rewriting. Six new long-file cases pass; the relevant rerun reported 298 passed and 3 existing platform skips.