From a6fa4701d8af4ab894265770486cd396156471dc Mon Sep 17 00:00:00 2001 From: Brendan Date: Wed, 16 Sep 2026 15:37:13 +0000 Subject: [PATCH] fix Codex startup trust handling --- packages/app/src/exe.ts | 10 ++++++++- packages/app/test/agent-launch.e2e.test.ts | 24 ++++++++++++++++++++- packages/app/test/execution-backend.test.ts | 2 +- packages/app/test/fixtures/fake-herdr.sh | 18 +++++++++++++--- packages/app/test/workspace.test.ts | 6 ++++-- 5 files changed, 52 insertions(+), 8 deletions(-) diff --git a/packages/app/src/exe.ts b/packages/app/src/exe.ts index b523839b..3b9421b9 100644 --- a/packages/app/src/exe.ts +++ b/packages/app/src/exe.ts @@ -62,10 +62,18 @@ export function launchAgentCommand(agentName: string, connection: ExeConnection, `workspace_id=$(printf '%s' "$workspaces" | jq -r --arg label ${shellAtom(workspaceName)} '.result.workspaces[]? | select(.label == $label) | .workspace_id' | head -n1)`, `if [ -z "$workspace_id" ]; then created=$(${herdr} workspace create --cwd ${shellAtom(runPath)} --label ${shellAtom(workspaceName)} --no-focus) && workspace_id=$(printf '%s' "$created" | jq -er '.result.workspace.workspace_id') && tab_id=$(printf '%s' "$created" | jq -er '.result.tab.tab_id') && pane=$(printf '%s' "$created" | jq -er '.result.root_pane.pane_id') && ${herdr} tab rename "$tab_id" ${shellAtom(name)} >/dev/null; else tabs=$(${herdr} tab list --workspace "$workspace_id") && tab_id=$(printf '%s' "$tabs" | jq -r --arg label ${shellAtom(name)} '.result.tabs[]? | select(.label == $label) | .tab_id' | head -n1); if [ -z "$tab_id" ]; then created=$(${herdr} tab create --workspace "$workspace_id" --cwd ${shellAtom(runPath)} --label ${shellAtom(name)} --no-focus) && tab_id=$(printf '%s' "$created" | jq -er '.result.tab.tab_id') && pane=$(printf '%s' "$created" | jq -er '.result.root_pane.pane_id'); else panes=$(${herdr} pane list --workspace "$workspace_id") && pane=$(printf '%s' "$panes" | jq -r --arg tab "$tab_id" '.result.panes[]? | select(.tab_id == $tab) | .pane_id' | head -n1); test -n "$pane"; extras=$(printf '%s' "$panes" | jq -r --arg tab "$tab_id" --arg keep "$pane" '.result.panes[]? | select(.tab_id == $tab and .pane_id != $keep) | .pane_id'); for extra in $extras; do ${herdr} pane close "$extra" >/dev/null; done; fi; fi`, `existing=$(${herdr} agent get ${shellAtom(name)} 2>/dev/null || true)`, - `if [ -n "$existing" ]; then printf '%s\\n' "$existing"; else ${herdr} agent start ${shellAtom(name)} --kind ${shellAtom(connection.agentKind)} --pane "$pane"${agentCommand(connection, launchInstruction, runPath)} && ${herdr} agent get ${shellAtom(name)}; fi`, + `if [ -n "$existing" ]; then printf '%s\\n' "$existing"; else ${herdr} agent start ${shellAtom(name)} --kind ${shellAtom(connection.agentKind)} --pane "$pane"${agentCommand(connection, launchInstruction, runPath)}${startupTrustCommand(connection, name)} && ${herdr} agent get ${shellAtom(name)}; fi`, ].join(" && "); } +/** Codex evaluates directory trust before applying CLI project config overrides. + * Herdr reports the chooser as interactive-ready, so automation must answer it in the pane. */ +function startupTrustCommand(connection: ExeConnection, agentName: string): string { + if (connection.agentKind !== "codex") return ""; + const herdr = herdrBinary(connection), name = shellAtom(agentName); + return ` && startup_output=$(${herdr} agent read ${name} --source recent --format text) && case "$startup_output" in *"Do you trust the contents of this directory?"*) ${herdr} agent prompt ${name} '1' --wait --until working --until idle --timeout 15000 >/dev/null ;; esac`; +} + export function promptAgentCommand(connection: ExeConnection, agentName: string, prompt: string): string { const encodedPrompt = base64(prompt), herdr = herdrBinary(connection); const name = shellAtom(agentName); diff --git a/packages/app/test/agent-launch.e2e.test.ts b/packages/app/test/agent-launch.e2e.test.ts index 5463155b..c8b7bdcd 100644 --- a/packages/app/test/agent-launch.e2e.test.ts +++ b/packages/app/test/agent-launch.e2e.test.ts @@ -41,10 +41,32 @@ describe("agent launch and prompt delivery", () => { await expect(readFile(resolve(promptDirectory, "prompt.md"), "utf8")).resolves.toBe(prompt); await expect(readFile(resolve(stateDir, "launch-instruction"), "utf8")).resolves.toBe(`Read and follow the complete task instructions in ${promptDirectory}/prompt.md`); await expect(readFile(resolve(stateDir, "status"), "utf8")).resolves.toBe("working"); - await expect(readFile(resolve(stateDir, "events"), "utf8")).resolves.toBe("workspace list\nworkspace create\ntab rename\nagent get\nagent start\nagent get\n"); + await expect(readFile(resolve(stateDir, "events"), "utf8")).resolves.toBe("workspace list\nworkspace create\ntab rename\nagent get\nagent start\nagent read\nagent get\n"); } finally { await rm(root, { recursive: true, force: true }); await rm("/tmp/factorize-prompts/run-e2e", { recursive: true, force: true }); } }); + + it("accepts Codex's startup trust chooser before declaring launch successful", async () => { + const root = await mkdtemp(`${tmpdir()}/factorize-agent-trust-e2e-`); + try { + const repository = resolve(root, "repo"); + const runPath = resolve(repository, ".factorize-runs/run-trust-e2e"); + const stateDir = resolve(root, "fake-herdr-state"); + await mkdir(repository, { recursive: true }); + await chmod(fixture, 0o755); + const connection: ExeConnection = { vmName: "unused", apiToken: "unused", agentKind: "codex", cwd: repository, herdrCommand: fixture }; + const environment = { ...process.env, FAKE_HERDR_STATE_DIR: stateDir, FAKE_HERDR_RUN_PATH: runPath, FAKE_HERDR_TRUST_PROMPT: "1" }; + + const launched = await runFile("bash", ["-c", launchAgentCommand("run-trust-e2e", connection, "flow-e2e", runPath, "lease-e2e", "Fix permissions")], { env: environment }); + + expect(jsonLines(launched.stdout).at(-1)?.result.agent).toMatchObject({ agent_status: "working", interactive_ready: true }); + await expect(readFile(resolve(stateDir, "trusted"), "utf8")).resolves.toBe(""); + await expect(readFile(resolve(stateDir, "events"), "utf8")).resolves.toContain("agent read\nagent prompt\nagent get\n"); + } finally { + await rm(root, { recursive: true, force: true }); + await rm("/tmp/factorize-prompts/run-trust-e2e", { recursive: true, force: true }); + } + }); }); diff --git a/packages/app/test/execution-backend.test.ts b/packages/app/test/execution-backend.test.ts index 91f8ffd2..afb3eb88 100644 --- a/packages/app/test/execution-backend.test.ts +++ b/packages/app/test/execution-backend.test.ts @@ -16,7 +16,7 @@ describe("ExeHerdrBackend", () => { expect(requests[0]).toContain("agent start"); expect(requests[0]).toContain("ZG8gdGhlIHdvcms="); expect(requests[0]).toContain("Read and follow the complete task instructions in /tmp/factorize-prompts/run-1/prompt.md"); - expect(requests[0]).not.toContain("agent prompt"); + expect(requests[0]).not.toContain("agent prompt 'run-1' 'do the work'"); const delivered = await backend.deliverPrompt(launched.handle, "do the work"); expect(delivered.state).toBe("accepted"); expect(requests[1]).toContain("agent prompt"); diff --git a/packages/app/test/fixtures/fake-herdr.sh b/packages/app/test/fixtures/fake-herdr.sh index 91cc594b..fea2885b 100755 --- a/packages/app/test/fixtures/fake-herdr.sh +++ b/packages/app/test/fixtures/fake-herdr.sh @@ -33,14 +33,26 @@ case "${1:-} ${2:-}" in printf working > "$state_dir/status" agent_json ;; + "agent read") + if [ "${FAKE_HERDR_TRUST_PROMPT:-}" = 1 ] && [ ! -f "$state_dir/trusted" ]; then + printf 'Do you trust the contents of this directory? 1. Yes, continue 2. No, quit\n' + else + printf 'Agent ready\n' + fi + ;; "agent wait") test "$(cat "$state_dir/status")" = idle agent_json ;; "agent prompt") - test "$(cat "$state_dir/status")" = idle - printf '%s' "${4-}" > "$state_dir/prompt" - printf working > "$state_dir/status" + if [ "${FAKE_HERDR_TRUST_PROMPT:-}" = 1 ] && [ ! -f "$state_dir/trusted" ]; then + test "${4-}" = 1 + : > "$state_dir/trusted" + else + test "$(cat "$state_dir/status")" = idle + printf '%s' "${4-}" > "$state_dir/prompt" + printf working > "$state_dir/status" + fi agent_json ;; *) diff --git a/packages/app/test/workspace.test.ts b/packages/app/test/workspace.test.ts index 5bec5da3..3c175f46 100644 --- a/packages/app/test/workspace.test.ts +++ b/packages/app/test/workspace.test.ts @@ -45,7 +45,9 @@ describe("flow workspaces", () => { expect(launch).toContain("--dangerously-bypass-hook-trust"); expect(launch).toContain("--cd '/repo/.factorize-runs/run'"); expect(launch).toContain('projects."/repo/.factorize-runs/run".trust_level="trusted"'); - expect(launch).not.toContain("agent prompt"); + expect(launch).toContain("agent read 'factorize-1' --source recent --format text"); + expect(launch).toContain("agent prompt 'factorize-1' '1' --wait --until working --until idle"); + expect(launch).not.toContain("agent prompt 'factorize-1' 'fix it'"); expect(delivery).toContain("agent prompt 'factorize-1'"); expect(delivery).toContain("agent wait 'factorize-1' --until idle --until done --timeout 15000"); expect(delivery).toContain("agent prompt 'factorize-1' \"$prompt\" --wait --until working --until blocked --timeout 7000"); @@ -55,7 +57,7 @@ describe("flow workspaces", () => { it("passes the initial prompt only when starting a new idempotent agent", () => { const command = startAgentCommand("factorize-1", { vmName: "vm", apiToken: "token", agentKind: "codex", cwd: "/repo" }, "fix it", "factorize", "/repo/.factorize-runs/run", "lease"); - expect(command).not.toContain("agent prompt"); + expect(command).not.toContain("agent prompt 'factorize-1' 'fix it'"); expect(command).toContain("else herdr agent start 'factorize-1'"); expect(command).toContain("/tmp/factorize-prompts/factorize-1/prompt.md"); });