Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b8f683a
feat(review): run the reviewed repository's own commands behind a con…
wenshao Aug 22, 2026
bbea6d9
Merge branch 'main' into feat/review-sandbox-ci
wenshao Aug 22, 2026
2c070a1
chore(review): regenerate settings.schema.json for review.sandbox
wenshao Aug 22, 2026
97ab3f4
Merge remote-tracking branch 'origin/feat/review-sandbox-ci' into fea…
wenshao Aug 22, 2026
c3fa33c
fix(review): make `required` actually refuse, and fix four wiring bug…
wenshao Aug 22, 2026
04a6ee1
fix(review): stop the reviewed repository from deciding its own conta…
wenshao Aug 22, 2026
42abd48
fix(review): close the hand-off arm, and three fixes that were wrong …
wenshao Aug 22, 2026
d0bbd4f
fix(review): the hand-off gate was dead code, and three more edges
wenshao Aug 23, 2026
a163eee
fix(review): put the hand-off conversion at the one exit, and make th…
wenshao Aug 23, 2026
963c421
fix(review): the third continuation exit destroyed the report it was …
wenshao Aug 23, 2026
7a9869e
fix(review): reap the container on every abnormal exit, not only the …
wenshao Aug 23, 2026
12ee718
fix(review): scrub the container client env by provenance, not by name
wenshao Aug 23, 2026
a08f76b
fix(review): drop --user on rootless runtimes, and take the base-side…
wenshao Aug 23, 2026
a3c1f24
fix(review): refuse mount roots the -v grammar cannot spell, and stop…
wenshao Aug 23, 2026
dc4397e
test(review): gate the mount-root cases Windows cannot be asked
wenshao Aug 24, 2026
e3bfba5
test(review): make the Windows pin actually reach the check it pins
wenshao Aug 24, 2026
623fef6
test(review): skip the second uid case instead of returning, and rest…
wenshao Aug 24, 2026
4ddc611
test(review): drop the comment last round's edit left stranded
wenshao Aug 24, 2026
b11492e
test(review): pin the four decisions this feature is sold on
wenshao Aug 24, 2026
9f5e5ad
fix(review): read the policy setting the way an operator writes it
wenshao Aug 24, 2026
fd97d08
test(review): finish the policy table instead of one row of it
wenshao Aug 24, 2026
62040e4
test(review): exercise the reap and the runtime probe, and stop leaki…
wenshao Aug 24, 2026
5d2a126
Merge remote-tracking branch 'origin/main' into feat/review-sandbox-ci
wenshao Aug 25, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions packages/cli/src/commands/review/build-test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import {
applyHandOffPolicy,
run,
runBuildTest,
type BuildTestReport,
Expand Down Expand Up @@ -4483,3 +4484,45 @@ describe('runBuildTest', () => {
});
});
});

describe('applyHandOffPolicy', () => {
const handOff = {
toolchain: 'unsupported' as const,
affected: [],
buildSet: [],
widenedWith: [],
install: null,
build: [],
test: [],
timedOut: [],
ok: true,
note: 'build-test could not scope this repo',
};

it('converts a hand-off to a refusal under `required`', () => {
// The hand-off tells the agent to install and build with its own shell,
// which nothing here contains. This conversion has been wrong twice — once
// as a precondition that was never true, once as a wrapper on two of the
// three routes that produce it — so what it produces is pinned here rather
// than left to the call site.
const got = applyHandOffPolicy(handOff, 'required');
expect(got.toolchain).toBe('refused');
// NOT `ok`, or a reader treats it as a clean hand-off and does by hand
// exactly what the policy refused.
expect(got.ok).toBe(false);
expect(got.note).toContain('do not run the commands by hand');
expect(got.build).toEqual([]);
expect(got.test).toEqual([]);
});

it('leaves a hand-off alone under the other policies', () => {
for (const policy of ['off', 'auto'] as const) {
expect(applyHandOffPolicy(handOff, policy)).toBe(handOff);
}
});

it('never converts a real run', () => {
const real = { ...handOff, toolchain: 'npm' as const };
expect(applyHandOffPolicy(real, 'required')).toBe(real);
});
});
228 changes: 216 additions & 12 deletions packages/cli/src/commands/review/build-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ import { spawnSync } from 'node:child_process';
import { existsSync, readFileSync, statSync, writeFileSync } from 'node:fs';
import { join, resolve } from 'node:path';
import { writeStdoutLine, writeStderrLine } from '../../utils/stdioHelpers.js';
import {
containerCommand,
containerName,
containerPathFor,
handOffRefused,
killContainer,
sandboxPolicy,
mountRootFor,
refuseUnsandboxedPhase,
reviewSandboxImage,
runtimeClientEnv,
sandboxVerdict,
type CommandKind,
type SandboxPolicy,
type ContainerRuntime,
} from './lib/sandboxed-exec.js';
import {
DEFAULT_COMMAND_TIMEOUT_S,
DEFAULT_WHOLE_CALL_BUDGET_S,
Expand Down Expand Up @@ -110,7 +126,14 @@ export interface CommandResult {

export interface BuildTestReport {
/** The scoped toolchain that ran, or `unsupported` when selection was unsafe. */
toolchain: 'npm' | 'unsupported';
/**
* `refused` is not a kind of repository — it is the absence of a run.
* `unsupported` means "this command could not scope your repo, go run the
* build yourself", which is a real instruction the brief acts on; routing a
* sandbox refusal into it would send the agent to run the reviewed code by
* hand with its own shell, which is the exact thing the policy forbade.
*/
toolchain: 'npm' | 'unsupported' | 'refused';
/** Workspace dirs the diff changed. */
affected: string[];
/** What was built, dependencies first — after any widening. */
Expand Down Expand Up @@ -310,10 +333,57 @@ export function buildRunEnv(
* set is measured HERE, off the raw text, and survives a trim that drops the
* FAIL lines it was parsed from.
*/
/**
* The container argv for one reviewed-repository command, or null to run it
* directly.
*
* Null covers three cases and they are not the same thing: the policy is off
* (today's behaviour), no runtime answered under `auto`, or this command's cwd
* is not inside a review temp dir — which is the case for a `/review` of a
* local checkout, where the tree under test IS the user's own working copy and
* there is no `.qwen/tmp` sibling layout to mount. The `required` policy is
* NOT handled here: refusing is the caller's decision, because only the caller
* knows what evidence it is about to mark unavailable.
*/
function containerised(
command: string,
cwd: string,
kind: CommandKind,
): {
file: string;
args: string[];
name: string;
runtime: ContainerRuntime;
} | null {
const verdict = sandboxVerdict();
if (verdict.kind !== 'container') return null;
const tmpDir = mountRootFor(cwd);
if (tmpDir === null) return null;
// The CANONICAL spelling, matching the mount: the bind mount is created from
// the root's realpath, so a lexical `--workdir` names a directory the
// container does not have and every command fails before it starts.
const workdir = containerPathFor(cwd);
if (workdir === null) return null;
const name = containerName();
return {
...containerCommand(command, {
cwd: workdir,
tmpDir,
kind,
name,
runtime: verdict.runtime,
image: reviewSandboxImage(),
}),
name,
runtime: verdict.runtime,
};
}

export function run(
command: string,
cwd: string,
timeoutMs: number,
kind: CommandKind = 'test',
): CommandResult {
const started = Date.now();
// spawnSync validates `timeout` as an unsigned integer: the adapters'
Expand All @@ -322,16 +392,50 @@ export function run(
// with no report, or zero, which arms no kill timer at all. Coerce once
// at the one boundary every command crosses.
const deadlineMs = Math.max(1, Math.round(timeoutMs));
const r = spawnSync(command, {
cwd,
shell: true,
encoding: 'utf8',
timeout: deadlineMs,
maxBuffer: 64 * 1024 * 1024,
// A build that asks a question is a build that hangs until the deadline.
stdio: ['ignore', 'pipe', 'pipe'],
env: buildRunEnv(),
});
// This is the reviewed repository's own command — `npm ci` with whatever
// install scripts the PR committed, its build, its suite — so it is the
// thing #9556 is about. `containerised` returns null when the run is not
// sandboxed, and the direct spawn below is unchanged for that case.
const boxed = containerised(command, cwd, kind);
const r = boxed
? spawnSync(boxed.file, boxed.args, {
cwd,
encoding: 'utf8',
timeout: deadlineMs,
maxBuffer: 64 * 1024 * 1024,
// SIGKILL, not the default SIGTERM, and only on the boxed branch.
// `spawnSync` sends its `killSignal` at the deadline and then WAITS for
// the child to exit — so an attached runtime client that forwards the
// signal and keeps waiting on a workload whose own trap ignores it
// never returns, and the `killContainer` below is never reached. That
// is what made the round-4 machinery unreachable rather than wrong.
// SIGKILL cannot be ignored, so the client dies, the call returns, and
// the container is then reaped BY NAME at the daemon — which is where
// the deadline had to be enforced all along.
killSignal: 'SIGKILL',
stdio: ['ignore', 'pipe', 'pipe'],
// NOT `buildRunEnv()`: the container gets an allowlist instead (see
// `containerEnv`), and this env is the RUNTIME CLIENT's — the caller's
// PATH and nothing from the review, minus the daemon-selecting
// variables a repository could have shipped in its own `.env`.
env: runtimeClientEnv(),
})
: spawnSync(command, {
cwd,
shell: true,
encoding: 'utf8',
timeout: deadlineMs,
maxBuffer: 64 * 1024 * 1024,
// A build that asks a question is a build that hangs until the deadline.
stdio: ['ignore', 'pipe', 'pipe'],
env: buildRunEnv(),
});
if (boxed && spawnTimedOut(r)) {
// The deadline killed the CLIENT; the container outlives it — see the
// `--name` comment in `containerCommand`. Reach the daemon instead, then
// report the timeout exactly as before.
killContainer(boxed.runtime, boxed.name);
}
// `spawnSync` sets `error.code === 'ETIMEDOUT'` when the deadline fired — that is
// the authoritative signal. The `SIGTERM`/null-status pair is only a fallback: it
// also matches an external SIGTERM (a container stop), and it misses a non-default
Expand Down Expand Up @@ -614,7 +718,53 @@ function previousReport(out: string | undefined): BuildTestReport {
return parsed as BuildTestReport;
}

export function runBuildTest(args: BuildTestArgs): BuildTestReport {
/**
* A report that says the phase ran nothing, and why.
*
* Module scope because two callers need it: the phase gate inside the run, and
* the hand-off conversion at the exit.
*/
function refusedReport(why: string): BuildTestReport {
return {
toolchain: 'refused',
affected: [],
buildSet: [],
widenedWith: [],
install: null,
build: [],
test: [],
timedOut: [],
// NOT `ok: true`. `unsupportedReport`'s hand-off is `ok` because nothing
// was found wrong; here something WAS — the phase could not be run under
// the policy in force — and a reader that treats this as a clean hand-off
// would go do by hand exactly what the policy just refused.
ok: false,
note:
`no build or test evidence: ${why}. This phase would have had to run ` +
`the reviewed repository's own commands, which is what the policy ` +
`forbids — so it ran nothing rather than running them unsandboxed. Do ` +
`not read this as a passing build, and do not run the commands by hand ` +
`to fill the gap.`,
};
}

/**
* The hand-off is an EXECUTION too, and it is the one that leaves this
* process: `unsupportedReport` tells the agent to install and build with its
* own shell — see the `toolchain: "unsupported"` rule in the brief — and that
* shell is contained by nothing here. The phase gate cannot catch it, because
* the gate passes exactly when a runtime answered and the tree is mountable,
* which is when a repo the adapters cannot scope still reaches the hand-off.
*
* At the ONE exit every report crosses, and that placement is the point. The
* first attempt tested a precondition (`!applicable` — the filtered adapter
* ARRAY, never falsy) and was dead code. The second wrapped the two
* `adapter.run` returns and missed the `!adapter` branch's own `unsupported`
* report. Both were the same mistake at different addresses: guarding routes
* one at a time in a function with several. There is exactly one place a
* report can reach a caller, so the conversion belongs there.
*/
function runBuildTestUnguarded(args: BuildTestArgs): BuildTestReport {
// yargs `type: 'number'` coerces `--timeout abc` to NaN rather than
// rejecting it; NaN defeats every budget-floor comparison and reaches
// spawnSync as an invalid deadline — ERR_OUT_OF_RANGE with no report.
Expand Down Expand Up @@ -739,6 +889,33 @@ export function runBuildTest(args: BuildTestArgs): BuildTestReport {
previous,
exec: args.exec ?? run,
};
// BEFORE anything is executed or handed off. Under `review.sandbox: required`
// with no container runtime answering, this phase must produce no build/test
// evidence rather than produce it by running the reviewed repository's code
// unsandboxed. It sits here and not at the spawn because one route never
// reaches a spawn at all: a repo this adapter cannot scope is handed to the
// AGENT's own shell (`unsupportedReport`), which would otherwise run the
// install and the suite with nothing consulted.
const refusal = refuseUnsandboxedPhase(root);
if (refusal && args.resume) {
// THROW on a continuation, never return. The handler writes whatever this
// returns to `--out`, which on a resume is the very report the call was
// asked to continue — so returning the refusal below would overwrite a
// partial run's install, builds and finished suites, and the refusal
// report carries no run identity, so every later `--resume` would fail the
// identity check ("records no run identity") even after a runtime came
// back. One transient probe failure would cost the round its whole
// build-test chain. This is the invariant the `!adapter` branch below
// states in its own words; a policy refusal is subject to it too.
throw new Error(
`refusing to continue this run: ${refusal}. The report at ${args.out} ` +
`is left as it was — re-run without --resume once the policy can be ` +
`satisfied, or lower review.sandbox.`,
);
}
if (refusal) {
return refusedReport(refusal);
}
const { adapter, applicable } = selectToolchainAdapter(
root,
toolchainAdapters,
Expand Down Expand Up @@ -905,3 +1082,30 @@ export const buildTestCommand: CommandModule = {
}
},
};

/**
* Turn a hand-off into a refusal when the policy forbids one.
*
* Exported and separate from `runBuildTest` so the conversion — the half that
* has been wrong twice, first as a dead precondition and then as a wrapper on
* two of the three routes — is reachable by a test without a live container
* runtime. What stays unpinned is only that `runBuildTest` calls it, which is
* one visible line rather than a branch hiding in a long function.
*/
export function applyHandOffPolicy(
report: BuildTestReport,
policy: SandboxPolicy = sandboxPolicy(),
): BuildTestReport {
return handOffRefused(report.toolchain, policy)
? refusedReport(
`review.sandbox is "required" and no toolchain adapter could scope ` +
`this repository, so the only remaining route was to hand its ` +
`install, build and test commands to an agent shell this policy ` +
`cannot contain`,
)
: report;
}

export function runBuildTest(args: BuildTestArgs): BuildTestReport {
return applyHandOffPolicy(runBuildTestUnguarded(args));
}
1 change: 1 addition & 0 deletions packages/cli/src/commands/review/lib/agent-briefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ Read the JSON it prints:
- **A suite left unrun is not a suite that passed — continue the run.** \`testScope.notRun\` names suites the whole-call budget could not reach, and a \`test[]\` entry with \`"clamped": true\` is a suite the budget started too late and killed (its deadline was shortened, so its timeout says nothing about the suite). A third shape ends before any suite — a single-package repo whose budget ran out before its one suite has an empty \`test[]\`, no \`testScope\`, and \`"endedBeforeTests": true\` (the report's own stamp), with the \`note\` naming the unrun suite; read them before calling the dimension finished. That third shape cannot be continued — a continuation has no recorded scope to read, and a \`--resume\` on it answers "ended before its test phase" and points at a fresh run — so report the dimension UNFINISHED and do not spend a continuation on it. The first two mean the dimension is unfinished AND continuable: re-run the SAME \`build-test\` command with \`--resume\` — it skips install and build, runs only what is left, and merges into the same report file. The ${SHELL_TOOL_MAX_TIMEOUT_MS / 1000}-second ceiling is per CALL, so this is the only way a repo whose suites do not fit one call ever finishes them (measured on this repo: \`packages/cli\` alone needs 401s, and install + builds + \`packages/core\` had already spent 285s). Keep resuming while work is left, up to ${MAX_RESUME_CALLS} continuations; then report what the run has, with \`notRun\` disclosed.
- **When any \`test[]\` command failed (exit non-zero, not a timeout), MEASURE which failures are the PR's before ruling by path.** The path rule above misclassifies in both directions — an environment-flaky test in a touched file gets filed as a Critical it did not cause, and a PR that breaks a test in an UNTOUCHED file gets waved through as pre-existing. The measurement is two commands: \`qwen review base-tree --plan <plan> --worktree <worktree> --out <plan dir>/qwen-review-pr-<n>-base-tree.json\` (builds the merge base beside the worktree). **Read \`available\` before using \`path\`** — a tree that was created but did NOT build populates \`path\` too, and a base that failed to build says nothing whatsoever about the PR, so measuring against it turns an infrastructure failure into a list of Criticals. \`available: false\` (local/lightweight review, no merge base, a base that would not compile) means the path rule stands — say so and stop here, and \`qwen review test-delta --report <the build-test report you wrote> --baseline <the base-tree report's path field> --pr-worktree <this worktree> --out <plan dir>/qwen-review-pr-<n>-test-delta.json\`. Read its verdict: a file in \`netNew\` fails on the PR side only — **that is the Critical**, whatever file the diff touches; a file in \`shared\` fails on base too — **pre-existing by measurement**, never filed, whatever file the diff touches; an \`unparsed\` entry, a timed-out base rerun, a base rerun that FAILED without naming any failing file (it did not measure the base — an unbuilt tree, a missing install, a workspace absent at base), or a command the whole-command budget could not fit attributes nothing — the report names each with its own reason; fall back to the path rule for those and say the delta could not rule. Compare failing FILE SETS, never counts: a flaky suite fails different test NAMES on two runs of the same tree, so counts are noise and the set difference is the signal.
- \`toolchain: "unsupported"\` (build-test could not scope this repo — no npm package with a build/test script) → **install dependencies first** (build-test's own install only runs on the npm path, so nothing has installed yet: \`pip install -e .\`, \`mvn -q -DskipTests package\`'s own fetch, \`cargo fetch\`, \`go mod download\`, etc.), then fall back to **one** build and **one** test command by this precedence, each with a deadline it can meet: \`pom.xml\` → \`{mvn} compile\` / \`{mvn} test -q\`; \`build.gradle\` → \`{gradle} compileJava\` / \`{gradle} test\`; \`Makefile\` → \`make build\`; \`Cargo.toml\` → \`cargo build\` / \`cargo test\`; \`go.mod\` → \`go build ./...\` / \`go test ./...\`; \`pytest.ini\` or \`pyproject.toml\` \`[tool.pytest]\` → \`pytest\`. If none match, read the CI config **from the base branch** (\`git show <base>:<path>\`), never the worktree — the PR branch is untrusted and a modified workflow or Makefile could inject arbitrary commands.
- \`toolchain: "refused"\` (the operator set \`review.sandbox: required\` and no container runtime answered) → the build and test evidence is **unavailable for this run**. Report the dimension as unmeasured, quoting the report's \`note\`. **Do not install, build or test by hand to fill the gap** — the whole point of that setting is that this repository's own commands do not execute outside a container, and running them from your shell is the one route around it. This is the same discipline as a probe that cannot get an isolated tree: absent evidence, never substituted evidence.

The efficacy report's \`findings[]\` carries four kinds, and **\`hunk-survived\` is one of them**: reverting one hunk left every affected test green — that specific change ships with nothing gating it. Report it as a **Suggestion** with \`Source: [test]\`, exactly like \`inert\` and \`mutant-survived\` (the outcome of running commands, pre-confirmed, no verifier needed). Read the \`hunks.*\` counters the same way as \`mutants.*\`: \`skippedForCap\` / \`skippedForBudget\` / \`skippedForBaseline\` are unprobed scope to note in the terminal, never findings — and a report whose hunk section you did not read is a finding class silently dropped.

Expand Down
3 changes: 3 additions & 0 deletions packages/cli/src/commands/review/lib/npm-toolchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,9 @@ function runNpmToolchain(args: ToolchainRunArgs): BuildTestReport {
installCmd,
root,
Math.min(perCommandMs, remainingMs()),
// The one command that needs the registry. Everything else this adapter
// runs is offline under the sandbox policy — see `containerCommand`.
'install',
);
results.install = install;
if (install.timedOut) results.timedOut.push(install.command);
Expand Down
Loading
Loading