diff --git a/packages/app/src/exe.ts b/packages/app/src/exe.ts index 3b9421b9..fa2ac5dc 100644 --- a/packages/app/src/exe.ts +++ b/packages/app/src/exe.ts @@ -62,7 +62,7 @@ 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)}${startupTrustCommand(connection, name)} && ${herdr} agent get ${shellAtom(name)}; fi`, + `if [ -n "$existing" ]; then printf '%s\\n' "$existing"; else ${startupTrustLock(connection)}${herdr} agent start ${shellAtom(name)} --kind ${shellAtom(connection.agentKind)} --pane "$pane"${agentCommand(connection, launchInstruction, runPath)}${startupTrustCommand(connection, name)}${startupTrustUnlock(connection)} && ${herdr} agent get ${shellAtom(name)}; fi`, ].join(" && "); } @@ -74,6 +74,17 @@ function startupTrustCommand(connection: ExeConnection, agentName: string): stri 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`; } +/** Every trust acceptance rewrites the shared Codex config. Serialize that brief handshake so + * simultaneous Factorize runs cannot make config/batchWrite race on config.toml. */ +function startupTrustLock(connection: ExeConnection): string { + if (connection.agentKind !== "codex") return ""; + return `trust_lock=/tmp/factorize-codex-trust-$(id -u).lock && exec 9>"$trust_lock" && flock 9 && `; +} + +function startupTrustUnlock(connection: ExeConnection): string { + return connection.agentKind === "codex" ? ` && flock -u 9` : ""; +} + 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/workspace.test.ts b/packages/app/test/workspace.test.ts index 3c175f46..e781b9e0 100644 --- a/packages/app/test/workspace.test.ts +++ b/packages/app/test/workspace.test.ts @@ -47,6 +47,9 @@ describe("flow workspaces", () => { expect(launch).toContain('projects."/repo/.factorize-runs/run".trust_level="trusted"'); 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).toContain('trust_lock=/tmp/factorize-codex-trust-$(id -u).lock'); + expect(launch).toContain('flock 9'); + expect(launch).toContain('flock -u 9'); 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"); @@ -58,7 +61,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 'factorize-1' 'fix it'"); - expect(command).toContain("else herdr agent start 'factorize-1'"); + expect(command).toContain("flock 9 && herdr agent start 'factorize-1'"); expect(command).toContain("/tmp/factorize-prompts/factorize-1/prompt.md"); }); @@ -68,6 +71,12 @@ describe("flow workspaces", () => { expect(defaultAgentCommand("pi")).toBe(""); }); + it("does not take the Codex trust lock for other agent kinds", () => { + const command = startAgentCommand("factorize-1", { vmName: "vm", apiToken: "token", agentKind: "claude", cwd: "/repo" }, "fix it", "factorize", "/repo/.factorize-runs/run", "lease"); + expect(command).not.toContain("factorize-codex-trust"); + expect(command).not.toContain("flock"); + }); + it("passes a configured agent command as safely quoted arguments", () => { const command = startAgentCommand("factorize-1", { vmName: "vm", apiToken: "token", agentKind: "codex", cwd: "/repo", agentCommand: "--model 'gpt 5'" }, "fix it", "factorize", "/repo/.factorize-runs/run", "lease"); expect(command).toContain("-- '--model' 'gpt 5'");