Repository navigation
fix(core): serialize whole-file writes with edits - #67091
Conversation
There was a problem hiding this comment.
The fix is right. write now holds the same lock edit, apply_patch and codemode use while reading, approving and writing. Using #60509's reproduction (adapted to fork the write), the base silently loses the write while an edit waits for approval. With this PR the write waits, then lands after the edit. Neighbouring core tests (tool-edit, tool-write, file-mutation, codemode-script-diffs) pass and typecheck:tsc is clean. No caller of write already holds the lock, so it can't deadlock on itself.
Public API is unchanged.
The new regression test is the blocker: it times out after 5s on every run, and on the base too, so it can't catch a regression (see inline).
| const secondApproval = yield* Deferred.make<void>() | ||
| const holdFirst = yield* Deferred.make<void>() | ||
| const assertions: Permission.AssertInput[] = [] | ||
| const memoMap = yield* Layer.makeMemoMap |
There was a problem hiding this comment.
This test times out after 5s every run, both with this PR and on dev. Both writeLayer(...) graphs are built with this one shared memo map, so the second graph ends up using the first graph's Permission and Environment. I logged both handlers: the second write's approval goes to the first handler, which then waits forever on holdFirst. synchronousReads is never used either. The isDone(secondApproval) === false check passes on dev for the same reason, so the test can't tell the bug from the fix.
The locks are host-wide anyway, so one graph is enough. Use a permission handler that holds only the first call. Then wait on firstApproval, fork the second write, check secondApproval isn't done, interrupt the first, join the second, and check the patch and the file contents. That version passes in about 300ms with this PR and fails on dev. It also makes synchronousReads and the memo-map setup unnecessary, which roughly halves the test.
20e192f to
3394fc1
Compare
There was a problem hiding this comment.
The test hang is fixed. Each graph now shares only the global nodes, so each has its own Permission and Environment. The test passes reliably (8/8, ~95ms) and fails on dev at the concurrent-approval assertion, so it now catches the regression. I re-ran the #60509 reproduction on the new head and the write is still serialized behind the pending edit. Neighbouring core tests and typecheck:tsc pass. Public API is unchanged. No new concerns.
There was a problem hiding this comment.
The test fix holds. With each graph's own Permission and Environment, the regression test passes reliably (~28ms) and fails on dev at the concurrent-approval assertion. The #60509 reproduction still shows the write serialized behind a pending edit. Neighbouring core tests and typecheck:tsc pass. Public API is unchanged. No new concerns.
Issue for this PR
Whole-file writes could read and approve a preview while an edit of the same file was still in progress, making that preview stale before the write committed.
Type of change
What does this PR do?
FileWrite.writenow uses the existing host-wideFileMutation.withLockfor the resolved destination. The lock covers reading the current text, building and approving the diff, writing, formatting, and BOM synchronization.resolve the destination +with the destination's mutation lock: read current text → approve preview → write → format → sync BOMFileWrite.commitremains unchanged and non-reentrant for callers that already hold the lock. Machine transfer behavior and public APIs are unchanged.The regression builds two independent Location graphs (with independent
Permission,Environment,Flag, andFormatterbindings) sharing only the host-wideFileMutation.TransactionLocksstate. It holds the first write at approval, verifies the second cannot approve concurrently, interrupts the first, then verifies the second acquires the lock and writes the approved content.How did you verify your code works?
bun run script/test.ts test/file-write.test.ts --rerun-each 10 --randomizepassed (10/10) inpackages/core.bunx oxlint packages/core/src/file-write.ts packages/core/test/file-write.test.tsandgit diff --checkpassed.bun run typecheckandbun run typecheck:tscinpackages/corereported no diagnostics infile-write.tsorfile-write.test.ts.Screenshots / recordings
Not applicable.
Checklist
Added by the opencode bot (AI-generated).
Fixes #60509
Related: #60595