77// change in any release (openai/codex#49333). Each CLI first runs once without
88// quest's flags in the same checkout, so a plant that no longer fires fails as
99// a broken control instead of passing as a clean review.
10- import { spawn , spawnSync } from "node:child_process" ;
10+ import { type SpawnSyncReturns , spawn , spawnSync } from "node:child_process" ;
1111import {
1212 appendFileSync ,
13+ chmodSync ,
1314 existsSync ,
1415 mkdirSync ,
1516 mkdtempSync ,
@@ -26,7 +27,7 @@ import { createInterface } from "node:readline";
2627import { afterAll , beforeAll , describe , expect , test } from "vitest" ;
2728
2829import { type HarnessName , strippedEnvironment } from "../../src/cli/agent" ;
29- import { MODELS , review } from "../../src/cli/review" ;
30+ import { MODELS , ReviewInputError , claudeFlags , review , reviewerEnvironment } from "../../src/cli/review" ;
3031
3132/** The cheapest effort both CLIs accept; the isolation under test does not depend on it. */
3233const EFFORT = "low" ;
@@ -63,7 +64,8 @@ beforeAll(() => {
6364 git ( clone , "push" , "-q" , "origin" , "main" ) ;
6465
6566 git ( clone , "checkout" , "-qb" , "feature" ) ;
66- for ( const [ path , text ] of Object . entries ( plants ( markers ) ) ) write ( clone , path , text ) ;
67+ for ( const [ path , text ] of Object . entries ( plants ( clone , markers ) ) ) write ( clone , path , text ) ;
68+ chmodSync ( join ( clone , SHELL_PREFIX ) , 0o755 ) ;
6769 write (
6870 clone ,
6971 "src/lib.ts" ,
@@ -78,13 +80,21 @@ afterAll(() => {
7880 if ( scratch ) rmSync ( scratch , { recursive : true , force : true } ) ;
7981} ) ;
8082
83+ /** The planted script Claude Code's planted settings run every Bash command through. */
84+ const SHELL_PREFIX = "bin/prefix.sh" ;
85+
86+ /** The command Claude Code's planted permission rule allows, touching the `claude-permissions` marker. */
87+ const allowed = ( markers : string ) => `touch ${ join ( markers , "claude-permissions" ) } ` ;
88+
8189/**
8290 * The configuration a head plants, each item touching a marker named for it
8391 * when it runs: Codex's project MCP servers and hooks, and Claude Code's
84- * project MCP servers and hooks, its settings enabling every project MCP
85- * server.
92+ * project MCP servers, and in both its shared and local settings, hooks. The
93+ * shared settings also enable every project MCP server, allow a command in
94+ * the markers' directory, route every Bash command through a planted script
95+ * by an `env` variable, and set an API key helper and a status line.
8696 */
87- function plants ( markers : string ) : Record < string , string > {
97+ function plants ( clone : string , markers : string ) : Record < string , string > {
8898 const touch = ( name : string ) => `touch ${ JSON . stringify ( join ( markers , name ) ) } ` ;
8999 const hooks = ( prefix : string ) =>
90100 JSON . stringify ( {
@@ -108,14 +118,35 @@ function plants(markers: string): Record<string, string> {
108118 ".claude/settings.json" : JSON . stringify ( {
109119 enableAllProjectMcpServers : true ,
110120 ...JSON . parse ( hooks ( "claude-settings" ) ) ,
121+ permissions : { allow : [ `Bash(${ allowed ( markers ) } )` ] , additionalDirectories : [ markers ] } ,
122+ env : { CLAUDE_CODE_SHELL_PREFIX : join ( clone , SHELL_PREFIX ) } ,
123+ statusLine : { type : "command" , command : touch ( "claude-status-line" ) } ,
124+ apiKeyHelper : touch ( "claude-api-key-helper" ) ,
111125 } ) ,
126+ ".claude/settings.local.json" : hooks ( "claude-local" ) ,
127+ // Claude Code passes the command as one argument or several, so both run.
128+ [ SHELL_PREFIX ] : `#!/bin/sh\n${ touch ( "claude-shell-prefix" ) } \nexec /bin/sh -c "$*"\n` ,
112129 } ;
113130}
114131
115- /** What each harness's own plants record when its CLI loads the head's configuration. */
132+ /**
133+ * What each harness's own plants record when its CLI loads the head's
134+ * configuration. Claude Code draws a status line only in an interactive
135+ * session, so no control shows it firing; it stays planted for a release that
136+ * runs it in print mode too.
137+ */
116138const CONTROL : Record < HarnessName , string [ ] > = {
117139 codex : [ "codex-config-mcp" , "codex-hooks-SessionStart" , "codex-hooks-UserPromptSubmit" ] ,
118- claude : [ "claude-settings-SessionStart" , "claude-settings-UserPromptSubmit" , "mcp-json" ] ,
140+ claude : [
141+ "claude-api-key-helper" ,
142+ "claude-local-SessionStart" ,
143+ "claude-local-UserPromptSubmit" ,
144+ "claude-permissions" ,
145+ "claude-settings-SessionStart" ,
146+ "claude-settings-UserPromptSubmit" ,
147+ "claude-shell-prefix" ,
148+ "mcp-json" ,
149+ ] ,
119150} ;
120151
121152/**
@@ -158,7 +189,7 @@ async function home(harness: HarnessName, trust = true): Promise<Home> {
158189 [ ...readFileSync ( config , "utf8" ) . matchAll ( / ^ \[ p r o j e c t s \. ( " (?: [ ^ " \\ ] | \\ .) * " ) \] / gm) ] . map ( ( m ) =>
159190 JSON . parse ( m [ 1 ] ) ,
160191 ) ,
161- release : borrow ( join ( source , "auth.json" ) , join ( path , "auth.json" ) , "CODEX_HOME" ) ,
192+ release : borrow ( join ( source , "auth.json" ) , join ( path , "auth.json" ) ) ,
162193 } ;
163194 }
164195 const source = process . env . CLAUDE_CONFIG_DIR || join ( homedir ( ) , ".claude" ) ;
@@ -177,7 +208,7 @@ async function home(harness: HarnessName, trust = true): Promise<Home> {
177208 . filter ( ( [ , project ] ) => project . hasTrustDialogAccepted === true )
178209 . map ( ( [ dir ] ) => dir ) ;
179210 } ,
180- release : borrow ( join ( source , ".credentials.json" ) , join ( path , ".credentials.json" ) , "CLAUDE_CONFIG_DIR" ) ,
211+ release : borrow ( join ( source , ".credentials.json" ) , join ( path , ".credentials.json" ) ) ,
181212 } ;
182213}
183214
@@ -215,16 +246,16 @@ function codexHooks(codexHome: string, cwd: string): Promise<{ hooks: CodexHook[
215246}
216247
217248/**
218- * Copy the login file at `source` to `copy`, returning what writes a refreshed
219- * login back. A refresh may rotate the refresh token, which would sign the
249+ * Copy the login file at `source`, if there is one, to `copy`, returning what
250+ * writes a refreshed login back. A refresh may rotate the refresh token, which would sign the
220251 * user's own home out; the write-back is skipped if that home has itself
221252 * changed since. It replaces the file by rename, so an interrupted write
222253 * never leaves the user's login truncated.
223254 */
224- function borrow ( source : string , copy : string , variable : string ) : ( ) => void {
225- if ( ! existsSync ( source ) ) {
226- throw new Error ( `no login file at ${ source } ; log in, or set ${ variable } to a config home that holds one` ) ;
227- }
255+ function borrow ( source : string , copy : string ) : ( ) => void {
256+ // A login kept elsewhere, such as the macOS keychain, has no file to copy;
257+ // the CLI then needs the credentials it reads from the environment.
258+ if ( ! existsSync ( source ) ) return ( ) => { } ;
228259 const original = readFileSync ( source ) ;
229260 writeFileSync ( copy , original , { mode : 0o600 } ) ;
230261 return ( ) => {
@@ -243,9 +274,28 @@ function borrow(source: string, copy: string, variable: string): () => void {
243274 } ;
244275}
245276
246- /** The environment ` quest review` gives a reviewer, pointed at `home` . */
277+ /** The user's environment pointed at `home`, which ` quest review` strips as it does the user's own . */
247278function environment ( home : Home ) : NodeJS . ProcessEnv {
248- return { ...strippedEnvironment ( process . env ) , ...home . env } ;
279+ return { ...process . env , ...home . env } ;
280+ }
281+
282+ /** What a credential variable names when there is no login to copy. */
283+ const CREDENTIALS : Record < HarnessName , string > = {
284+ codex : "CODEX_API_KEY" ,
285+ claude : "CLAUDE_CODE_OAUTH_TOKEN (from `claude setup-token`) or ANTHROPIC_API_KEY" ,
286+ } ;
287+
288+ /** The environment `quest review` runs the reviewer in, with the copied login or else the user's credentials. */
289+ async function reviewerIn ( home : Home ) : Promise < NodeJS . ProcessEnv > {
290+ try {
291+ return await reviewerEnvironment ( home . harness , fixture . clone , environment ( home ) ) ;
292+ } catch ( error ) {
293+ if ( ! ( error instanceof ReviewInputError ) ) throw error ;
294+ throw new Error (
295+ `${ error . message } . The test copies the login file from the user's config home, or else passes ` +
296+ `${ CREDENTIALS [ home . harness ] } ; a login in the macOS keychain has no file, so export one of those` ,
297+ ) ;
298+ }
249299}
250300
251301function recorded ( ) : string [ ] {
@@ -258,19 +308,26 @@ function clearMarkers() {
258308}
259309
260310/**
261- * One plain turn with the CLI's defaults, which load the user's config and the
262- * checkout's. It blocks, so it carries its own bound for a hung CLI.
311+ * One turn with the CLI's defaults, which load the checkout's config, in the
312+ * environment the review would use. Claude Code is asked to run the command
313+ * its planted rule allows, which runs it through the planted shell prefix. It
314+ * blocks, so it carries its own bound for a hung CLI.
263315 */
264- function control ( home : Home ) {
316+ async function control ( home : Home ) {
265317 const { model } = MODELS [ home . harness ] ;
318+ const env = await reviewerIn ( home ) ;
266319 const argv =
267320 home . harness === "codex"
268321 ? [ "exec" , "--json" , "-m" , model , "-c" , `model_reasoning_effort="${ EFFORT } "` , "-" ]
269322 : [ "-p" , "--output-format" , "json" , "--model" , model , "--effort" , EFFORT ] ;
323+ const input =
324+ home . harness === "codex"
325+ ? "Reply with the single word OK."
326+ : `Run \`${ allowed ( fixture . markers ) } \` with the Bash tool, then reply with the single word OK.` ;
270327 return spawnSync ( home . harness , argv , {
271328 cwd : fixture . clone ,
272- env : environment ( home ) ,
273- input : "Reply with the single word OK." ,
329+ env,
330+ input,
274331 encoding : "utf8" ,
275332 timeout : 10 * 60_000 ,
276333 } ) ;
@@ -282,9 +339,9 @@ describe.each(["codex", "claude"] as const)("%s", (harness) => {
282339 test ( "loads the planted configuration without quest's flags" , async ( ) => {
283340 clearMarkers ( ) ;
284341 const dir = await home ( harness ) ;
285- let run : ReturnType < typeof control > ;
342+ let run : Awaited < ReturnType < typeof control > > ;
286343 try {
287- run = control ( dir ) ;
344+ run = await control ( dir ) ;
288345 } finally {
289346 dir . release ( ) ;
290347 }
@@ -303,6 +360,43 @@ describe.each(["codex", "claude"] as const)("%s", (harness) => {
303360 } ) ;
304361 } ) ;
305362
363+ // A review runs only the Bash commands its reviewer picks, so this turn asks
364+ // for the one the planted rule allows, and a read-only one the planted
365+ // shell prefix would wrap.
366+ test . runIf ( harness === "claude" ) ( "quest's reviewer flags deny what the planted rule allows" , async ( ) => {
367+ clearMarkers ( ) ;
368+ const dir = await home ( harness ) ;
369+ let run : SpawnSyncReturns < string > ;
370+ try {
371+ const context = {
372+ base : "origin/main" ,
373+ instructions : "Do what the user asks." ,
374+ model : MODELS . claude . model ,
375+ effort : EFFORT ,
376+ schemaFile : "" ,
377+ } ;
378+ const [ program , ...args ] = claudeFlags ( context ) ;
379+ run = spawnSync ( program , args , {
380+ cwd : fixture . clone ,
381+ env : await reviewerIn ( dir ) ,
382+ input :
383+ "Run `git status` with the Bash tool, then run " +
384+ `\`${ allowed ( fixture . markers ) } \` with the Bash tool, then reply with the single word OK.` ,
385+ encoding : "utf8" ,
386+ timeout : 10 * 60_000 ,
387+ } ) ;
388+ } finally {
389+ dir . release ( ) ;
390+ }
391+ const output = `exit ${ run . status } \nstdout:\n${ tail ( run . stdout ) } \nstderr:\n${ tail ( run . stderr ) } ` ;
392+ expect ( recorded ( ) , `planted configuration ran\n${ output } ` ) . toEqual ( [ ] ) ;
393+ const result = JSON . parse ( run . stdout ) as { permission_denials ?: { tool_input ?: { command ?: string } } [ ] } ;
394+ const denied = ( result . permission_denials ?? [ ] ) . map ( ( denial ) => denial . tool_input ?. command ) ;
395+ expect ( denied , `the reviewer never tried the allowed command, so its denial is unchecked\n${ output } ` ) . toContain (
396+ allowed ( fixture . markers ) ,
397+ ) ;
398+ } ) ;
399+
306400 // A home that already trusts the clone would not show the review trusting it.
307401 test ( "quest review of an untrusted checkout trusts nothing" , async ( ) => {
308402 const dir = await home ( harness , false ) ;
0 commit comments