Skip to content

Commit 09da801

Browse files
byapparovclaude
andcommitted
fix(run): keep the headless-run mark in memory, not the environment (#155 review)
- Replace process.env.AICTRL_HEADLESS_RUN with an in-process HeadlessRun mark, so processes a run spawns (e.g. a nested `aictrl acp` from bash) and stray shell exports cannot drop todowrite elsewhere. Regression test added. - Explain why AICTRL_ENABLE_TODO_TOOL is read at access time; the second copy-pasted getter is gone with the env mark. - Budget test clears an inherited AICTRL_ENABLE_TODO_TOOL; the e2e helper reports the captured request instead of a TypeError when tools are missing. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 09902e5 commit 09da801

7 files changed

Lines changed: 62 additions & 37 deletions

File tree

‎packages/cli/src/cli/cmd/run.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { terminal } from "./run.terminal"
1010
import { buildToolCatalogItems } from "./tool-catalog"
1111
import { withTimeout } from "../../util/timeout"
1212
import { Flag } from "../../flag/flag"
13+
import { HeadlessRun } from "../../flag/headless-run"
1314
import { bootstrap } from "../bootstrap"
1415
import { EOL } from "os"
1516
import { Filesystem } from "../../util/filesystem"
@@ -392,7 +393,7 @@ export const RunCommand = cmd({
392393
})
393394
},
394395
handler: async (args) => {
395-
process.env.AICTRL_HEADLESS_RUN = "true"
396+
HeadlessRun.enable()
396397
const invocation = createRunInvocation(args.format === "json")
397398

398399
async function fail(message: string, code: string, exit = 1): Promise<never> {

‎packages/cli/src/flag/flag.ts‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export namespace Flag {
2323
export declare const AICTRL_CLIENT: string
2424
export const AICTRL_ENABLE_QUESTION_TOOL = truthy("AICTRL_ENABLE_QUESTION_TOOL")
2525
export declare const AICTRL_ENABLE_TODO_TOOL: boolean
26-
export declare const AICTRL_HEADLESS_RUN: boolean
2726
export declare const AICTRL_MODEL_STREAM_IDLE_TIMEOUT_MS: number
2827

2928
// Experimental
@@ -97,22 +96,12 @@ Object.defineProperty(Flag, "AICTRL_CLIENT", {
9796
})
9897

9998
// Dynamic getter for AICTRL_ENABLE_TODO_TOOL
100-
// Evaluated at access time, like AICTRL_HEADLESS_RUN, which it overrides
99+
// Evaluated at access time, not module load time, so tests and embedders
100+
// can toggle the opt-in after the registry has been imported
101101
Object.defineProperty(Flag, "AICTRL_ENABLE_TODO_TOOL", {
102102
get() {
103103
return truthy("AICTRL_ENABLE_TODO_TOOL")
104104
},
105105
enumerable: true,
106106
configurable: false,
107107
})
108-
109-
// Dynamic getter for AICTRL_HEADLESS_RUN
110-
// Set by the `run` command at runtime, whichever entry point or build started it,
111-
// so it must be evaluated at access time
112-
Object.defineProperty(Flag, "AICTRL_HEADLESS_RUN", {
113-
get() {
114-
return truthy("AICTRL_HEADLESS_RUN")
115-
},
116-
enumerable: true,
117-
configurable: false,
118-
})
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Whether this process is serving a headless `aictrl run`. The run command marks it
3+
* at runtime, whichever entry point or build started it. The mark lives in memory
4+
* rather than in the environment, so it is not inherited by processes a run
5+
* spawns and cannot be switched on by a stray exported variable (#155).
6+
*/
7+
export namespace HeadlessRun {
8+
let active = false
9+
10+
export function enable() {
11+
active = true
12+
}
13+
14+
export function disable() {
15+
active = false
16+
}
17+
18+
export function enabled() {
19+
return active
20+
}
21+
}

‎packages/cli/src/tool/registry.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { Plugin } from "../plugin"
2323
import { WebSearchTool } from "./websearch"
2424
import { CodeSearchTool } from "./codesearch"
2525
import { Flag } from "@/flag/flag"
26+
import { HeadlessRun } from "@/flag/headless-run"
2627
import { Log } from "@/util/log"
2728
import { LspTool } from "./lsp"
2829
import { Truncate } from "./truncation"
@@ -118,7 +119,7 @@ export namespace ToolRegistry {
118119
*/
119120
function todoEnabled() {
120121
if (Flag.AICTRL_ENABLE_TODO_TOOL) return true
121-
return !Flag.AICTRL_HEADLESS_RUN
122+
return !HeadlessRun.enabled()
122123
}
123124

124125
async function all(): Promise<Tool.Info[]> {

‎packages/cli/test/cli/run-default-tools.test.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ async function requestedTools(entry: string, env: Record<string, string> = {}) {
4545
AICTRL_DISABLE_AUTOCOMPACT: "true",
4646
...env,
4747
}
48-
// The parent test process may itself run inside a headless run; only the spawned command decides.
49-
delete environment.AICTRL_HEADLESS_RUN
48+
// Only the opt-in passed by the test counts, not one inherited from the test process.
5049
if (!("AICTRL_ENABLE_TODO_TOOL" in env)) delete environment.AICTRL_ENABLE_TODO_TOOL
5150
const proc = Bun.spawn(
5251
["bun", "run", "--conditions=browser", entry, "run", "--model", "fixture/gemini-fixture", "Check this input."],
@@ -61,7 +60,10 @@ async function requestedTools(entry: string, env: Record<string, string> = {}) {
6160
])
6261
expect(exit, stderr + stdout).toBe(0)
6362
expect(requests.length, stderr + stdout).toBeGreaterThan(0)
64-
const declarations: { name: string }[] = requests[0].tools?.flatMap((tool: any) => tool.functionDeclarations ?? [])
63+
const declarations: { name: string }[] = (requests[0].tools ?? []).flatMap(
64+
(tool: any) => tool.functionDeclarations ?? [],
65+
)
66+
expect(declarations.length, JSON.stringify(requests[0])).toBeGreaterThan(0)
6567
return declarations.map((declaration) => declaration.name)
6668
} finally {
6769
clearTimeout(timeout)

‎packages/cli/test/session/headless-prompt-budget.test.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Skill } from "../../src/skill"
99
import { SystemPrompt } from "../../src/session/system"
1010
import { InstructionPrompt } from "../../src/session/instruction"
1111
import { ToolRegistry } from "../../src/tool/registry"
12+
import { HeadlessRun } from "../../src/flag/headless-run"
1213

1314
/**
1415
* Characters in the fixed prefix (system prompt plus tool definitions) that a
@@ -18,7 +19,11 @@ import { ToolRegistry } from "../../src/tool/registry"
1819
*/
1920
const HEADLESS_PREFIX_BUDGET = 35_500
2021

21-
const HEADLESS_ENV = { AICTRL_HEADLESS: "true", AICTRL_HEADLESS_RUN: "true" }
22+
// The lite build's prompt contract, with any inherited todo opt-in cleared.
23+
const HEADLESS_ENV: Record<string, string | undefined> = {
24+
AICTRL_HEADLESS: "true",
25+
AICTRL_ENABLE_TODO_TOOL: undefined,
26+
}
2227

2328
type Piece = { name: string; chars: number }
2429

@@ -58,13 +63,16 @@ describe("headless request prefix budget (#160)", () => {
5863
const previous: Record<string, string | undefined> = {}
5964

6065
beforeAll(() => {
66+
HeadlessRun.enable()
6167
for (const [key, value] of Object.entries(HEADLESS_ENV)) {
6268
previous[key] = process.env[key]
63-
process.env[key] = value
69+
if (value === undefined) delete process.env[key]
70+
else process.env[key] = value
6471
}
6572
})
6673

6774
afterAll(() => {
75+
HeadlessRun.disable()
6876
for (const [key, value] of Object.entries(previous)) {
6977
if (value === undefined) delete process.env[key]
7078
else process.env[key] = value

‎packages/cli/test/tool/registry.test.ts‎

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { afterEach, describe, expect, test } from "bun:test"
1+
import { afterEach, beforeEach, describe, expect, test } from "bun:test"
22
import path from "path"
33
import fs from "fs/promises"
44
import os from "os"
@@ -7,6 +7,7 @@ import { Instance } from "../../src/project/instance"
77
import { ToolRegistry } from "../../src/tool/registry"
88
import { Config } from "../../src/config/config"
99
import { Agent } from "../../src/agent/agent"
10+
import { HeadlessRun } from "../../src/flag/headless-run"
1011

1112
describe("tool.registry", () => {
1213
test("loads tools from .aictrl/tool (singular)", async () => {
@@ -182,22 +183,18 @@ describe("tool.registry", () => {
182183
})
183184

184185
describe("tool.registry todo tools (#155)", () => {
185-
const keys = ["AICTRL_HEADLESS_RUN", "AICTRL_ENABLE_TODO_TOOL"] as const
186-
const previous = Object.fromEntries(keys.map((key) => [key, process.env[key]]))
186+
const previous = process.env.AICTRL_ENABLE_TODO_TOOL
187187

188-
afterEach(() => {
189-
for (const key of keys) {
190-
if (previous[key] === undefined) delete process.env[key]
191-
else process.env[key] = previous[key]
192-
}
188+
beforeEach(() => {
189+
delete process.env.AICTRL_ENABLE_TODO_TOOL
193190
})
194191

195-
function setEnv(env: Partial<Record<(typeof keys)[number], string>>) {
196-
for (const key of keys) {
197-
if (env[key] === undefined) delete process.env[key]
198-
else process.env[key] = env[key]
199-
}
200-
}
192+
afterEach(() => {
193+
HeadlessRun.disable()
194+
delete process.env.AICTRL_HEADLESS_RUN
195+
if (previous === undefined) delete process.env.AICTRL_ENABLE_TODO_TOOL
196+
else process.env.AICTRL_ENABLE_TODO_TOOL = previous
197+
})
201198

202199
async function toolIDs() {
203200
await using tmp = await tmpdir()
@@ -212,22 +209,28 @@ describe("tool.registry todo tools (#155)", () => {
212209
}
213210

214211
test("a headless run gets neither todowrite nor todoread", async () => {
215-
setEnv({ AICTRL_HEADLESS_RUN: "true" })
212+
HeadlessRun.enable()
216213
const ids = await toolIDs()
217214
expect(ids).toContain("bash")
218215
expect(ids).not.toContain("todowrite")
219216
expect(ids).not.toContain("todoread")
220217
})
221218

222219
test("AICTRL_ENABLE_TODO_TOOL brings todowrite back to a headless run", async () => {
223-
setEnv({ AICTRL_HEADLESS_RUN: "true", AICTRL_ENABLE_TODO_TOOL: "1" })
220+
HeadlessRun.enable()
221+
process.env.AICTRL_ENABLE_TODO_TOOL = "1"
224222
const ids = await toolIDs()
225223
expect(ids).toContain("todowrite")
226224
expect(ids).not.toContain("todoread")
227225
})
228226

229227
test("sessions outside a headless run keep todowrite", async () => {
230-
setEnv({})
228+
expect(await toolIDs()).toContain("todowrite")
229+
})
230+
231+
test("the environment cannot mark a session as a headless run", async () => {
232+
// A run's child processes and stray shell exports must not drop the tool elsewhere.
233+
process.env.AICTRL_HEADLESS_RUN = "true"
231234
expect(await toolIDs()).toContain("todowrite")
232235
})
233236
})

0 commit comments

Comments
 (0)