diff --git a/plugins/bstack/scripts/context.mjs b/plugins/bstack/scripts/context.mjs index fd870fd..499e588 100644 --- a/plugins/bstack/scripts/context.mjs +++ b/plugins/bstack/scripts/context.mjs @@ -11,7 +11,7 @@ const mode = process.argv[2] === 'subagent' ? 'subagent' : 'session'; const read = name => fs.readFileSync(path.join(root, 'rules', name), 'utf8').trim(); let text = read('core.md').replaceAll('{{PLUGIN_ROOT}}', root.replace(/\\/g, '/')); -if (process.env.CARGO_TARGET_DIR) text += `\n\nCargo: CARGO_TARGET_DIR is shared, so put \`node "${root.replace(/\\/g, '/')}/scripts/cargo-lock.mjs"\` right before every cargo build, test and clippy.`; +if (process.env.CARGO_TARGET_DIR) text += `\n\nCargo: CARGO_TARGET_DIR is shared, so put \`node "${root.replace(/\\/g, '/')}/scripts/cargo-lock.mjs"\` right before every cargo build, test and clippy, with run_in_background or a timeout of at most 240000 ms.`; if (mode === 'session') text += `\n\n${read('style.md')}`; if (mode === 'session') { try { startPrune(); } catch { /* pruning old lanes waits for the next session */ } diff --git a/plugins/bstack/scripts/guard.mjs b/plugins/bstack/scripts/guard.mjs index edf0d1e..5b048c9 100644 --- a/plugins/bstack/scripts/guard.mjs +++ b/plugins/bstack/scripts/guard.mjs @@ -4,7 +4,7 @@ // branches, merges without green CI and a judge verdict for the head SHA (or one carried across // merge-ins of the base branch that left the PR diff unchanged), merges through side doors // (gh api, MCP), hand-written statuses and user-OK lines, cargo builds into a shared target dir -// without the target lock, a second judge on a judged head, and a judge after three blocking rounds. +// without the target lock, foreground waits on cargo builds past the cache lifetime, a second judge on a judged head, and a judge after three blocking rounds. // Exit 2 with a message that names the right alternative. A judge or worker that may start gets its // model from MODEL_TABLE. import { execFileSync } from 'node:child_process'; @@ -46,7 +46,7 @@ export function decide(input, deps) { const key = JSON.stringify([c.scope, c.text, c.cwd, c.cwdKnown]); if (checked.has(key)) continue; checked.add(key); - const reason = hiddenRule(c) ?? pendingRule(c) ?? userOkRule(c, hostDeps, line) ?? cargoLockRule(c, hostDeps, commands.slice(0, i + 1)) ?? (c.cmd === 'git' ? gitRule(c, cwd, hostDeps) : null) ?? (c.cmd === 'gh' ? ghRule(c, cwd, hostDeps, line) : null); + const reason = hiddenRule(c) ?? pendingRule(c) ?? userOkRule(c, hostDeps, line) ?? cargoLockRule(c, hostDeps, commands.slice(0, i + 1)) ?? foregroundWaitRule(c, input.tool_input) ?? (c.cmd === 'git' ? gitRule(c, cwd, hostDeps) : null) ?? (c.cmd === 'gh' ? ghRule(c, cwd, hostDeps, line) : null); if (reason) return reason; } } @@ -337,6 +337,15 @@ function cargoLockRule(c, deps, lineSoFar) { return `A target dir is set (CARGO_TARGET_DIR, --target-dir, --config build.target-dir or a .cargo/config.toml), so another worktree sharing it can rebuild these binaries between build and run. Run it under the target lock: put ${lock} right before cargo in your own command and keep the rest of it as it is, so the lock reads the same target dir: ${lock} cargo ${c.args.join(' ')}`; } +const MAX_FOREGROUND_MS = 240000; +const WAITS_ON_BUILDS = /(?:^|[\\/])(?:cargo-lock|checks)\.mjs$/; +const waitsOnBuilds = c => (c.cmd === 'node' && WAITS_ON_BUILDS.test(c.args[0] ?? '')) || (c.cmd === 'cargo' && /^(check|c)$/.test(cargoSubcommand(c.args) ?? '')); + +function foregroundWaitRule(c, { timeout, run_in_background: background } = {}) { + if (background || !(Number(timeout) > MAX_FOREGROUND_MS) || !waitsOnBuilds(c)) return null; + return `This call waits on cargo builds in the foreground for up to ${Math.round(Number(timeout) / 1000)} s, and an agent's context is cached for five minutes, so a longer wait reloads all of it. Run it with run_in_background and poll with sleeps of at most 240 s, or give it a timeout of at most ${MAX_FOREGROUND_MS} ms.`; +} + // --- saved verdicts and the user-OK line -------------------------------------------------------- const SAVED_VERDICTS = 'Saved verdicts are written only by the bstack hook when GitHub does not answer; the guard posts them at the next merge. Run the bstack:judge agent instead.'; const USER_OK_MARKER = /bstack-user-ok/i; diff --git a/tests/guard.test.mjs b/tests/guard.test.mjs index 7814b2d..092bf1b 100644 --- a/tests/guard.test.mjs +++ b/tests/guard.test.mjs @@ -1301,6 +1301,19 @@ test('#153: on a shared CARGO_TARGET_DIR, cargo builds go through the target loc allowed(shell('cargo test'), onHost({ platform: 'linux', env: {}, tmpdir: () => '/tmp' })); }); +test('a call that waits on cargo builds runs in the background or with a timeout inside the cache lifetime', () => { + const shared = onHost({ platform: 'linux', env: { CARGO_TARGET_DIR: '/home/u/.cargo-target' }, tmpdir: () => '/tmp' }); + const call = (command, tool_input, tool_name = 'Bash') => ({ ...shell(command), tool_name, tool_input: { command, ...tool_input } }); + const waiting = [`node "${CARGO_LOCK}" cargo test --workspace`, `cd crates/a && node ${CARGO_LOCK} cargo build 2>&1 | tail -3`, `node "${CARGO_LOCK.replace('cargo-lock', 'checks')}"`, 'cargo check --workspace']; + for (const command of waiting) { + for (const tool of ['Bash', 'PowerShell']) { + assert.match(decide(call(command, { timeout: 600000 }, tool), shared) ?? '', /in the foreground for up to 600 s.* run_in_background .* at most 240000 ms\.$/, `${tool}: ${command}`); + } + for (const tool_input of [{}, { timeout: 240000 }, { timeout: 600000, run_in_background: true }]) allowed(call(command, tool_input), shared); + } + for (const command of ['npm test', 'cargo fmt --all', 'node scripts/build.mjs', 'git status']) allowed(call(command, { timeout: 600000 }), shared); +}); + test('#260: inside an agent worktree the lane keeps the shared target dir, so cargo builds go to the lock in every mode and shell', () => { const repo = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'bstack-guard-lane-'))); after(() => fs.rmSync(repo, { recursive: true, force: true }));