Skip to content

perf(cargo): checks run under the target lock, and the lock rebuilds only after another worktree built - #314

Merged
bfcarrasco merged 2 commits into
mainfrom
perf/cargo-lock-and-waiting
Dec 3, 2026
Merged

bfcarrasco merged 2 commits into
mainfrom
perf/cargo-lock-and-waiting

Conversation

@bfcarrasco

Copy link
Copy Markdown
Owner

Request: "implementer nu alle dine anbefalinger effektivt i bstack. skip gerne verificeringer osv så vi kan pushe denne opdatering hurtigst muligt, og jeg ønsker at trykke merge til sidst"

Why: one 17.7 hour run with 39 agents on a Rust repo sharing CARGO_TARGET_DIR spent about 60 % of agent time waiting on cargo, and about half its tokens re-reading whole contexts after waits over five minutes. 13 of 44 heads got both a failed and a passed bstack/checks on the same commit.

What changed

  • checks.mjs runs every cargo line of the checks file through cargo-lock.mjs, so the gate no longer tests a build another worktree left in the shared target dir.
  • cargo-lock.mjs marks the workspace sources as changed only when another worktree built into the target dir last (.bstack-cargo.last holds the last workspace root). Back-to-back runs in one worktree no longer rebuild everything.
  • cargo-lock.mjs takes no lock and marks nothing for commands that build nothing (fmt, machete, metadata, tree, fetch, update, audit, deny).
  • Session and subagent start context names the lock wrapper when CARGO_TARGET_DIR is set, so an agent does not learn it from a guard stop.
  • Worker: only the touched tests while working, the full suite once per head through checks.mjs; waits over five minutes run in the background and are polled in sleeps of at most 240 s.
  • Judge: a green bstack/checks on the head covers the full suite; the same waiting rule.
  • Mode: a new ticket gets a fresh worker; at most three builders where builds share a target dir.
  • Verify: start a copy of a binary from a shared target dir.

Scope: no machine cargo config (jobs, linker) and nothing in the repos that use bstack.

Tradeoffs: the plugin markdown budget in tools/check-plugin.mjs goes from 64169 to 64848 bytes, the size of the new instruction lines after trimming them; nothing was subtracted elsewhere.

Verification: node --test tests/cargo-lock.test.mjs tests/checks.test.mjs: pass 31, fail 0, with a new real-cargo test for the marking rule and one for the wrapped checks command. check-plugin, comment-ratchet, handback, judge tests: pass 70, fail 0. The full suite and a run on a real multi-agent Rust task were not run, at the user's request.

@bfcarrasco

Copy link
Copy Markdown
Owner Author

bstack-judge: FAIL tier=2 blocking=1 sha=56a2e7fa0f537915ecb8401b935fbef477f82d1b

Blocking: F1.

PR 314 judged at head 56a2e7f. The PR closes no issue, so I took its Request and "What changed" as the contract. Checks are green: ubuntu, windows, validate. risk-tier puts it at tier 2 (lib/cargo.mjs is a risk path). The only loosened check is the markdown budget in tools/check-plugin.mjs going from 64169 to 64848, and the PR body explains it.

Criteria and proof

  • Tests: node --test tests/cargo-lock.test.mjs tests/checks.test.mjs passes 31 of 31 on head. With the base versions of cargo-lock.mjs, checks.mjs and lib/cargo.mjs put back, 29 pass and 2 fail. The 2 failures are exactly the two new tests, so both fail on base.
  • checks.mjs wraps cargo lines (proven on real cargo through a real shell): underTargetLock('cargo test --offline -q') runs "<node>" "<repo>/plugins/bstack/scripts/cargo-lock.mjs" cargo test .... With CARGO_TARGET_DIR set it exits 101 on a broken crate and 0 on a good one, and it writes .bstack-cargo.last (base does not write it). Non-cargo lines are left as written.
  • Sources marked changed only after another worktree built: proven for runs one after another by the new real-cargo test (fails on base, passes on head). It breaks when runs overlap; see F1.
  • Commands that build nothing take no lock and mark nothing: cargo fmt leaves src/lib.rs untouched, shown by the new test.
  • Start context: CARGO_TARGET_DIR=C:\x node plugins/bstack/scripts/context.mjs subagent ends with the line "Cargo: CARGO_TARGET_DIR is shared, so put node \".../scripts/cargo-lock.mjs\" right before every cargo build, test and clippy."
  • The worker, judge, mode and verify markdown lines are in the diff as stated. They are instructions, so nothing can run them.

Findings
F1 (blocks: regression). The cargo run path takes no lock, yet it now writes .bstack-cargo.last. That record can then name a worktree whose build is not the one in the target dir. In markSourcesChanged(program, args, path.dirname(lock)), the call in the holdsNoLock branch overwrites the record while another worktree holds the lock and is still compiling.

Reproduction on fixtures: two copies of one crate, A returning "ok" and B returning "broken", with the same package name and a lib unit test asserting "ok". B's lib is large and B runs with -j1 so its build is slow. Sharing a temp CARGO_TARGET_DIR, all through the wrapper, the sequence is:

  1. Start cargo test -j1 in B.
  2. After 3 s, run cargo run in A. It records A as the last builder and blocks on cargo's own lock.
  3. Run cargo test in A.
  • Head: A's cargo test exits 101 with left: "broken". It ran B's lib test binary; reproduced 2 of 2 runs.
  • Base: A's cargo test exits 0 (1 run), because base marks the sources changed on every locked run.

This is the scenario the PR is built for: many agents sharing one target dir, where one runs the app while another tests. A run's verdict can then come from the other worktree's code. A possible fix: the unlocked cargo run path should delete the record or leave it alone rather than write its own root, or the record should be written only under the lock. A regression test for the overlapping case is needed.

Dropped hypotheses

  • An unlocked cargo test in B between two wrapped runs in A: A still rebuilt and passed on head.
  • A line such as cargo fmt --check && cargo test: only the leading cargo is wrapped. Base wrapped nothing, so this is not a regression.
  • A worktree root missing from cargo metadata: falls back to marking every time, as base did.
  • Several repos sharing one target dir: they only cause extra rebuilds, which base also had.
  • Running twice in the same worktree: no marking, and correct (the new test).
  • The buildsNothing list (fmt/machete/metadata/tree/fetch/update/audit/deny): none of these write build output to the target dir.
  • Windows quoting in cmd /s /c: works, shown by the real run.

The overlap reproduction is the script race4.mjs in my scratchpad. Every fixture was a temp crate under the OS temp dir. Nothing was committed or pushed, and the base files I swapped in are restored (git status is clean).

[{"id":"F1","title":"cargo run writes .bstack-cargo.last without the lock, so a concurrent locked build by another worktree leaves the record naming this worktree and its next cargo test skips marking and runs the other worktree's test binary","reproduction":"node --test tests/cargo-lock.test.mjs fixture variant: worktree B `cargo test -j1` (slow lib) via plugins/bstack/scripts/cargo-lock.mjs, after 3 s worktree A `cargo run` via the wrapper, then A `cargo test` via the wrapper: head exits 101 left: \"broken\", base exits 0","base":"pass","head":"fail","blocks":"regression","files":["plugins/bstack/scripts/cargo-lock.mjs"]}]

@bfcarrasco

Copy link
Copy Markdown
Owner Author

bstack-judge: PASS+NOTES tier=2 sha=02b6d16ca51114f238dcb2f6e112a12d02fd53ca

Fixed: F1.

PR 314: judge of the delta 56a2e7f..02b6d16 (head 02b6d16, checked out detached)

What the delta does: plugins/bstack/scripts/cargo-lock.mjs markSourcesChanged now takes { locked }. An unlocked cargo run always marks the sources and deletes .bstack-cargo.last instead of writing it. A locked run skips marking only when the record names its own workspace root. The delta also adds one real-cargo test in tests/cargo-lock.test.mjs.

F1 (posted): cargo run wrote .bstack-cargo.last without holding the lock.

  • Mechanism on head: the unlocked path never writes the record. It removes it, so the next locked cargo test in any worktree finds no record and marks its sources. This closes the stale "last builder = me" record the finding described.
  • The new regression test fails on the old code and passes on head. With 56a2e7f's cargo-lock.mjs and head's tests, node --test --test-name-pattern="leaves no last builder" tests/cargo-lock.test.mjs fails with an AssertionError: .bstack-cargo.last still exists after cargo run. With head code, node --test tests/cargo-lock.test.mjs tests/checks.test.mjs gives tests 32, pass 32, fail 0.
  • I could not make the posted failure happen on 56a2e7f with my fixtures, so neither base nor head showed the bug in my runs. I tried five variants of the posted reproduction:
    • B is a slow cargo test -j1 on a "broken" lib.
    • After 3 s, A runs cargo run through the wrapper, then cargo test through the wrapper.
    • Across the variants I tried with and without a bin target, a slow lib (3000 and 12000 fns), slow code under #[cfg(test)], and a prior A build.
    • On 56a2e7f, head and main, A's cargo test always exited 0. On 56a2e7f and head .bstack-cargo.last still existed afterwards; on main it did not.
  • So for F1 the head is fixed by mechanism plus a regression test that fails on the old code. The exact posted timing did not reproduce on 56a2e7f here.

Attack hypotheses on the delta

  • Two cargo run in a row, or none: rmSync uses force: true, so a missing file is fine. Dropped.
  • A cargo run in another worktree while A's locked test runs: the record is removed, A's next test marks and rebuilds. This is safe; the only cost is an extra rebuild. Dropped as a defect.
  • Locked run with no workspace root (cargo metadata fails): the old record is now removed instead of kept stale. This is safer than before. Dropped.
  • Commands that build nothing (fmt etc.): they return before markSourcesChanged through buildsNothing, so the delta does not touch them. Dropped.
  • Race window: A's locked run decides to skip marking, then another worktree's cargo run builds before A's cargo takes cargo's build lock. This window is milliseconds. It already existed at 56a2e7f, comes from the skip check the earlier round judged, and is not introduced by this delta. I did not reproduce it. Out of scope.

Notes (not blocking)

  • N1 (cost): every cargo run now forces the next locked cargo command in every worktree, including the same one, to re-mark the whole workspace. A worker that loops cargo run and then cargo test gets a full workspace rebuild each time. That partly undoes the PR's goal of avoiding back-to-back rebuilds, and the PR body does not mention it. It is a correctness-over-speed trade-off, not a defect.

Cleanup: git status is clean; the temporary swaps of cargo-lock.mjs were restored with git checkout. Fixture scripts are in the session scratchpad, outside the repo.

[{"id":"F1","title":"cargo run writes .bstack-cargo.last without the lock, so a concurrent locked build by another worktree leaves the record naming this worktree and its next cargo test skips marking and runs the other worktree's test binary","reproduction":"node --test --test-name-pattern=\"leaves no last builder\" tests/cargo-lock.test.mjs (fails with 56a2e7f plugins/bstack/scripts/cargo-lock.mjs, passes on head); posted multi-worktree timing variant did not reproduce on 56a2e7f or head here","base":"pass","head":"pass","blocks":"no","files":["plugins/bstack/scripts/cargo-lock.mjs"]}]

@bfcarrasco
bfcarrasco merged commit 813698f into main Dec 3, 2026
4 checks passed
@bfcarrasco
bfcarrasco deleted the perf/cargo-lock-and-waiting branch December 3, 2026 14:22
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.

1 participant