Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
66 changes: 66 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,7 +17,9 @@ import {
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import {
applyHandOffPolicy,
run,
resumeWouldDestroyReport,
runBuildTest,
type BuildTestReport,
trimOutput,
Expand Down Expand Up @@ -4483,3 +4485,67 @@ 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('refuses to convert on a --resume, which would destroy the report', () => {
// The invariant the other two continuation exits enforce with a throw:
// "a continuation must never answer with a FRESH report". This conversion
// was added after both and returns one — which the handler writes over the
// report the call was asked to continue, and that refusal carries no run
// identity, so every later resume fails the identity check. A policy
// tightened between the first call and the resume is enough to trigger it,
// on the unscopeable repo shapes that reach a hand-off in the first place.
expect(resumeWouldDestroyReport(handOff, true, 'required')).toBe(true);
// A fresh call converts normally — that is the whole point of the
// conversion.
expect(resumeWouldDestroyReport(handOff, false, 'required')).toBe(false);
// And a resume of a real run is never touched.
expect(
resumeWouldDestroyReport(
{ ...handOff, toolchain: 'npm' },
true,
'required',
),
).toBe(false);
});

it('never converts a real run', () => {
const real = { ...handOff, toolchain: 'npm' as const };
expect(applyHandOffPolicy(real, 'required')).toBe(real);
});
});
270 changes: 258 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,72 @@ 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.
*/
/**
* Whether converting this report would destroy the run it was asked to
* continue.
*
* A predicate for the same reason `applyHandOffPolicy` is one: the conversion
* it guards returns a report, the handler writes whatever is returned, and a
* fresh refusal carries no run identity — so on a `--resume` it replaces the
* in-flight report and every later resume fails the identity check. The other
* two continuation exits enforce that invariant with a throw; this one was
* added after both and did not.
*/
export function resumeWouldDestroyReport(
report: BuildTestReport,
resume: boolean,
policy: SandboxPolicy = sandboxPolicy(),
): boolean {
return resume && handOffRefused(report.toolchain, policy);
}

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 {
const report = runBuildTestUnguarded(args);
// The THIRD continuation exit, and the one the invariant had not reached.
// "A continuation must never answer with a FRESH report" is enforced by a
// throw at the refusal gate and at `!adapter`; this conversion was added
// after both and returns a report of its own, which the handler writes
// unconditionally — so a policy that tightened between the first call and
// the resume would replace the in-flight report with an identity-less
// refusal, and every later `--resume` would fail the identity check. That
// costs the round its whole build-test chain over a setting change.
//
// The trigger is ordinary: the policy is read per call, so an operator
// raising it — or a workflow's `env:` — between call one and the resume is
// enough, on the unscopeable repo shapes (yarn/pnpm/bun) that reach a
// hand-off in the first place.
if (resumeWouldDestroyReport(report, args.resume === true)) {
throw new Error(
`refusing to continue this run: this repository's toolchain cannot be ` +
`scoped, and review.sandbox is now "required", so continuing would ` +
`replace the report at ${args.out} with a refusal that records no run ` +
`identity — killing the resume chain. Re-run without --resume under ` +
`the new policy.`,
);
}
return applyHandOffPolicy(report);
}
Loading
Loading