Skip to content

Preserve proposal order during file reads - #1763

Open
VIOLETee0 wants to merge 1 commit into
CodebuffAI:mainfrom
VIOLETee0:fix/proposal-write-order
Open

VIOLETee0 wants to merge 1 commit into
CodebuffAI:mainfrom
VIOLETee0:fix/proposal-write-order

Conversation

@VIOLETee0

Copy link
Copy Markdown

What

Register the complete propose_write_file replacement before awaiting its base-content read. The read still supplies that call's diff, and previousToolCallFinished still gates its result. Use satisfies to check the handler contract, matching propose_str_replace, so the regression can inject its actual dependencies without asserting incomplete handler parameters.

Why

Native tool batches can prepare successive proposals while an earlier read is pending. Previously, a following propose_str_replace missed the write's proposed content, read the old disk contents again, and reported that the replacement text was absent. The final proposal state then stayed at the first write instead of including the replacement.

How verified

One regression calls both real handlers with a deferred requestOptionalFile dependency, without sleeps or module mocks. It starts the replacement before releasing the write's read, applies both generated diffs to their expected base content, and checks the final proposal state and a single disk read.

  • Unchanged main 0239a27: 40 passed, 1 failed across the four affected test files; only the new regression failed.
  • Fixed: 41 passed, 0 failed, 162 assertions. The focused probe changed from two disk reads and a missing-text error to one read and the correct stacked proposal.
  • Agent-runtime and SDK typechecks, frozen-lockfile install, SDK build including declarations, Windows Freebuff binary build, version, and smoke passed with Bun 1.3.14.
  • A separate injected read-failure probe confirmed both dependent results reject with the original error, and run-state cleanup removes the reserved proposal.
  • ESLint: 0 errors, 4 local TypeScript resolver warnings. The new test is Prettier-formatted; the handler's existing formatting differences are unchanged from main.

Scope and tradeoffs

The full replacement is already known, so it can be reserved before the old contents arrive. This preserves concurrent preparation and ordered results without serializing all tools. No files are written by these proposal tools. This does not change the executor, path normalization, replacement matching, retry behavior, or the SDK's public API. Validation used injected file reads and actual handlers/diff logic on Windows; a live-model run, Linux/macOS, and the full monorepo suite were not run.

@codebuff-team

Copy link
Copy Markdown
Contributor

Good catch and a genuinely useful fix. In propose-write-file.ts, the old code awaited getProposedOrDiskContent() before calling setProposedContent(runId, path, ...). Because getProposedOrDiskContent only synchronously reads the current proposed-content map before yielding on the disk read, any propose_str_replace call that starts while the write's disk read is still pending would see nothing registered yet and fall back to stale disk contents — exactly the bug described. Moving setProposedContent ahead of the await closes that window without serializing unrelated tool calls, which is the right fix at the right layer (the store, not the executor).

The new test in propose-write-file.test.ts is well constructed: it uses a manually-released promise to simulate a slow disk read, starts both handlers, and verifies that the replacement correctly builds on the write's content (one disk read, correct final diff) instead of regressing to two reads and a missing-text error. That's exactly the kind of targeted regression test this repo would want for a timing bug — no sleeps, no mocked modules, just real handlers with an injected dependency.

The as → satisfies change to match propose_str_replace's handler-contract style is a nice, low-risk cleanup that makes the test's partial dependency object type-check properly.

Nothing else to flag — the diff is small, in-scope (agent-runtime), doesn't touch the executor or SDK public API, and the PR description documents verification thoroughly (typecheck, lint, cross-file test run before/after). Would want a maintainer to confirm the cleanup-on-failure path mentioned in the write-up is actually covered somewhere, since it's not visible in this diff, but that's a minor follow-up rather than a blocker.

@codebuff-team codebuff-team added bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree labels Nov 29, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants