Skip to content

perf(guard): a call that waits on cargo builds runs in the background or with a timeout inside the cache lifetime - #315

Merged
bfcarrasco merged 1 commit into
mainfrom
perf/cargo-foreground-timeout
Dec 3, 2026
Merged

bfcarrasco merged 1 commit into
mainfrom
perf/cargo-foreground-timeout

Conversation

@bfcarrasco

Copy link
Copy Markdown
Owner

Request

After #314 a run on a Rust repo with a shared CARGO_TARGET_DIR followed the new rules except one: agents still started cargo calls in the foreground with a 600 s timeout. In 5.6 hours 20 such calls ran over five minutes, and 8 ran into the ten-minute limit. An agent's context is cached for five minutes, so each of those waits reloads the whole context. The instruction in worker.md and judge.md alone did not hold; the owner asked for a guard.

Change

  • The guard refuses a Bash or PowerShell call that runs cargo-lock.mjs, checks.mjs or cargo check with a timeout over 240000 ms, unless it has run_in_background. The message names both ways out.
  • A call without a timeout, with one of at most 240000 ms, or in the background passes as before. Commands that do not wait on cargo builds are not touched.
  • The session context line for a shared target dir now names the limit up front, so an agent does not need the refusal to learn it.

Trade-off

A foreground call with a short timeout relies on the harness moving a command to the background when it passes its timeout. That is what the transcripts of the measured run show ("did not complete within its 600s timeout and was moved to the background"). A harness that kills the command instead would stop a build after four minutes; the refusal therefore names run_in_background first.

Proof

  • node --test tests/guard.test.mjs: the new test covers the three kinds of call in both shells, the three ways that pass, and commands the rule must leave alone.
  • node --test tests/: 895 tests, 869 pass, 26 skipped, 0 fail.
  • node tools/check-plugin.mjs: ok, markdown budget unchanged.

Not run: a real multi-agent session on the new rule.

… or with a timeout inside the cache lifetime
@bfcarrasco

Copy link
Copy Markdown
Owner Author

bstack-judge: PASS+NOTES tier=2 sha=d222a4f231377af7a7db14011f6c8dff373fc3ec

PR 315 (perf(guard): foreground waits on cargo builds). Head d222a4f confirmed after gh pr checkout 315 --detach. Base is 813698f. The PR closes no issue, so I judged it against the PR's Request and Change. risk-tier.mjs --pr 315 gives tier 2 (guard.mjs is a risk path) and lists no weakened checks. CI check jobs were still pending when I looked; validate passed.

Criteria and proof:

  1. The guard refuses Bash and PowerShell calls running cargo-lock.mjs, checks.mjs or cargo check with a timeout over 240000 ms that are not in the background: PROVEN. I piped hook JSON into the real plugins/bstack/scripts/guard.mjs, once with CARGO_TARGET_DIR set and once without. The probe script is in my scratchpad, outside the repo; the runs are listed below.
    • Every one of these exits 2 with the new message, in both env modes:
      • node "<root>/scripts/cargo-lock.mjs" cargo test, plain and with & node in PowerShell
      • node.exe "<root>/scripts/checks.mjs"
      • bash -c 'node ".../checks.mjs"'
      • git status; node ".../checks.mjs"
      • cargo check, cargo +nightly check and cargo c
      • timeout given as the string "600000"
      • timeout 240001
  2. A call with no timeout, a timeout of at most 240000 ms, or run_in_background passes as before, and other commands are untouched: PROVEN. These exit 0:
    • no timeout, timeout 240000, timeout null or -1, and 600000 with run_in_background: true
    • node --test tests/ at 600000
    • cargo build and cargo clippy at 600000 with no shared dir
    • With a shared dir, cargo build and cargo clippy still get the existing target-lock refusal first, so that behaviour is unchanged.
  3. The session context line names the limit: PROVEN. With CARGO_TARGET_DIR set, node plugins/bstack/scripts/context.mjs subagent ends with "...build, test and clippy, with run_in_background or a timeout of at most 240000 ms." Without the variable there is no Cargo line.
  4. The new test fails on base: PROVEN. node --test --test-name-pattern="cache lifetime" tests/guard.test.mjs passes on head. With base guard.mjs put back it fails with ERR_ASSERTION (actual ''). I restored the file afterwards and the tree is clean.

Findings (notes only, none blocking):

  • F1: at timeout 240001 the message says "up to 240 s" and then asks for "at most 240000 ms", because Math.round rounds 240.001 s down. It is cosmetic, but it reads like a contradiction.
  • F2: a node flag placed before the script, such as node --max-old-space-size=4096 ".../checks.mjs", gets past the rule, because WAITS_ON_BUILDS only tests args[0]. Agents don't write calls like that normally, so this is not a regression.
  • F3: the rule also covers checks.mjs in repos without Rust and on machines without a shared target dir, yet the message still says "waits on cargo builds". The five-minute cache argument holds in those cases too. Only the wording is off.
  • F4: an agent that follows the target-lock refusal and retries with a 600000 ms foreground timeout now gets a second refusal. The context line states the limit up front, which mostly heads this off.

Dropped hypotheses:

  • timeout as a string: Number() handles it.
  • Infinity, null or negative timeout: all pass.
  • PowerShell & call operator, node.exe, a nested bash -c, a chained ; command, a toolchain +nightly, the c alias: all caught.
  • Unrelated commands (npm test, cargo fmt, git status) and non-cargo node scripts: untouched.
  • bstack's own docs (worker.md, ship.md, ci.md): none tells an agent to use a long foreground timeout.
[{"id":"F1","title":"Refusal at timeout 240001 says 'up to 240 s' while asking for at most 240000 ms (rounding)","reproduction":"plugins/bstack/scripts/guard.mjs foregroundWaitRule with tool_input {timeout:240001}","base":"not run","head":"pass","blocks":"no","files":["plugins/bstack/scripts/guard.mjs"]},{"id":"F2","title":"A node flag before the script (node --max-old-space-size=4096 checks.mjs) bypasses the rule; only args[0] is matched","reproduction":"plugins/bstack/scripts/guard.mjs stdin {tool_name:'PowerShell',tool_input:{command:'node --max-old-space-size=4096 scripts/checks.mjs',timeout:600000}}","base":"pass","head":"pass","blocks":"no","files":["plugins/bstack/scripts/guard.mjs"]},{"id":"F3","title":"Message says 'waits on cargo builds' even for checks.mjs in non-Rust repos or with no shared target dir","reproduction":"plugins/bstack/scripts/guard.mjs stdin without CARGO_TARGET_DIR, command node scripts/checks.mjs, timeout 600000","base":"pass","head":"pass","blocks":"no","files":["plugins/bstack/scripts/guard.mjs"]}]

@bfcarrasco
bfcarrasco merged commit 8003bf5 into main Dec 3, 2026
4 checks passed
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