Repository navigation
fix(vfs): save through fs_put_if_unchanged so another connection's save is not overwritten - #3218
Merged
dylan-savage merged 3 commits intoNov 12, 2026
Conversation
The view checks the server copy, then writes without a condition. A save from another connection between the two is overwritten. The view tests fail with "Missing expected rejection". The store tests fail because the store has no conditional write yet. Refs #3215 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The writer sends the SHA-256 of the text it read, or null when it saw no file. The engine reads the file and its version, compares the hash, then calls the backend's write_file_atomic with that version, so a save from another connection can't land in between. On a mismatch it writes nothing and answers written: false. The TypeScript and Python clients get fsWriteStringIfUnchanged and fs_write_string_if_unchanged. Two filesystem-store changes keep workspace files as they were: - write_file_atomic writes with newline='', so Windows no longer turns '\n' into '\r\n' and the hash of what was written still matches. - Lock files move from next to each file to a .locks folder under the store root. Next to the file they showed up in the user's file tree. Refs #3215 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An auto-mode save read the server copy, compared hashes, then wrote with no condition. A save from another tab, device or teammate between the two was overwritten. writeNow now sends the hash of the copy the view read with the write, and the store refuses if the file moved. Commit all does the same with the hash its check saw. A refusal is the existing StaleWriteError, which the agent reports as "changed on the server since you read it". IVirtualFileSystem gets an optional writeIfUnchanged, which CachedVfs forwards and the workspace backend sends as fs_put_if_unchanged. Closes #3215 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dylan-savage
requested review from
Amitabh1998,
Rod-Christensen,
jmaionchi and
stepmikhaylov
as code owners
November 12, 2026 00:08
Contributor
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. 🗂️ Base branches to auto review: 2
Please check the settings in the CodeRabbit UI or the
|
Contributor
🤖 Internal: Discord sync markerAuto-managed by the Discord notification workflow. Stores the linked Discord message ID and forum thread ID. Do not edit or delete. |
dylan-savage
added a commit
that referenced
this pull request
Nov 18, 2026
This reverts 5df7ef4. An auto-mode save checks the server copy, then writes, as it did before #3218. A save from another connection that lands between the check and the write is overwritten again. We accept that rare race. Why: - #3218 added fsWriteStringIfUnchanged and fs_write_string_if_unchanged to both SDK clients' public API, which broke the shell's frozen API contract. - Saves needed the new fs_put_if_unchanged engine subcommand, so the engine had to deploy before the UI, with no fallback. - Deletes and Undo still checked, then acted, so they stayed racy anyway. The filesystem store's newline='' write and its .locks folder go too. Lock files sit next to each file again, as before #3218. Workspace saves don't take those locks once the conditional write is gone, so they stay out of the user's file tree. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.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
rrext_storesubcommandfs_put_if_unchanged, modeled onfs_put_json. One message carriespath,textandbase_hash, the SHA-256 of the text the writer read (null when it saw no file). The engine reads the file and its version, compares the hash, then calls the backend'swrite_file_atomicwith that version. On a mismatch it writes nothing and answerswritten: false. The clients getfsWriteStringIfUnchangedandfs_write_string_if_unchanged.writeNownow sends the hash of the copy it read with the write, in place of the separate check. Commit all sends the hash its own check saw. A refusal is the existingStaleWriteError, which the agent already reports as "changed on the server since you read it".IVirtualFileSystemgets an optionalwriteIfUnchanged, whichCachedVfsforwards to the workspace backend.write_file_atomicwrites withnewline='', so Windows doesn't turn\ninto\r\n. Lock files move to a.locksfolder under the store root. Without that move, every file this write saved got a.name.lockfile next to it in the user's file tree.Deletes and Undo in auto mode still check, then act. This PR leaves them as they are. A create has no store-level condition, so two engine processes that share one store can still race on one. The engine half, the
feat(store)commit, could go todevelopon its own later, minus its one-line entry in the agent's client policy. Against an older engine the view fails every auto-mode save with "Unknown subcommand", so the engine has to ship first.Type
Fix
Testing
./builder testpassesThe first commit adds the tests and fails against the current code. In
stagedFileSystem.test.ts, a write-through save and a Commit all each meet another connection's save just before the write. Both failed with "Missing expected rejection", because the other save was overwritten. The threeTestPutTextIfUnchangedcases intest_file_store.pyfailed because the store had no conditional write.With the fix, all five pass.
packages/ai/testspasses in full (4260 passed, 128 skipped). The shared vfs, agent and toolkit suites pass (75 tests).apps/rocket-uitypechecks clean.apps/sharedhas one type error that is already on this branch (eval/trial.ts:331, missingleasePreview). With that line patched locally,shell:checkreports "Up to date with v9".Two clients against a real local engine built from this branch, with a filesystem store. Client B saves between client A's read and A's write:
StagedFileSystemoverCachedVfs, on a backend that does what the workspace backend does) replaced it too.fsWriteStringIfUnchangedreturns false and B's save stays. A retry after a re-read lands. A create with no hash lands once, then refuses. The view throwsStaleWriteErrorand B's save stays. The Python client gives the same result.I did not run
./builder test, the builder in a real browser, the S3 and Azure stores, or Windows. The edit-05 runner holdsfs_openin modew, so it needs to holdfs_put_if_unchangedbefore it can test this.Checklist
Linked Issue
Closes #3215
🤖 Generated with Claude Code