Skip to content

fix(core): serialize whole-file writes with edits - #67091

Merged
rekram1-node merged 2 commits into
devfrom
write-lock
Nov 30, 2026
Merged

rekram1-node merged 2 commits into
devfrom
write-lock

Conversation

@rekram1-node

@rekram1-node rekram1-node commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

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

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

FileWrite.write now uses the existing host-wide FileMutation.withLock for 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 BOM

FileWrite.commit remains 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, and Formatter bindings) sharing only the host-wide FileMutation.TransactionLocks state. 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 --randomize passed (10/10) in packages/core.
  • bunx oxlint packages/core/src/file-write.ts packages/core/test/file-write.test.ts and git diff --check passed.
  • bun run typecheck and bun run typecheck:tsc in packages/core reported no diagnostics in file-write.ts or file-write.test.ts.

Screenshots / recordings

Not applicable.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Fixes #60509

Related: #60595

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Fixes #60509
Related: #60595

const secondApproval = yield* Deferred.make<void>()
const holdFirst = yield* Deferred.make<void>()
const assertions: Permission.AssertInput[] = []
const memoMap = yield* Layer.makeMemoMap

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@rekram1-node
rekram1-node merged commit 9290af2 into dev Nov 30, 2026
12 checks passed
@rekram1-node
rekram1-node deleted the write-lock branch November 30, 2026 01:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

edit and write can silently lose a concurrent successful file update

1 participant