|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +vi.mock("@jsonbored/gittensory-engine", async () => { |
| 4 | + return import("../../packages/gittensory-engine/src/index"); |
| 5 | +}); |
| 6 | + |
| 7 | +import { createRealCliSubprocessSpawn, constructProductionCodingAgentDriver } from "../../packages/gittensory-miner/lib/coding-agent-construction.js"; |
| 8 | +import type { AgentSdkQueryFn, CodingAgentDriverTask } from "../../packages/gittensory-engine/src/index"; |
| 9 | + |
| 10 | +const task: CodingAgentDriverTask = { |
| 11 | + attemptId: "attempt-1", |
| 12 | + workingDirectory: "/tmp/worktrees/attempt-1", |
| 13 | + acceptanceCriteriaPath: "/tmp/worktrees/attempt-1/ACCEPTANCE-CRITERIA.md", |
| 14 | + instructions: "Apply the fix described in ACCEPTANCE-CRITERIA.md.", |
| 15 | + maxTurns: 4, |
| 16 | +}; |
| 17 | + |
| 18 | +function assistantResult(): Record<string, unknown> { |
| 19 | + return { type: "result", subtype: "success", is_error: false, num_turns: 1, result: "done" }; |
| 20 | +} |
| 21 | + |
| 22 | +function queryCapturing(captured: { input?: Parameters<AgentSdkQueryFn>[0] }): AgentSdkQueryFn { |
| 23 | + return (input) => { |
| 24 | + captured.input = input; |
| 25 | + return (async function* () { |
| 26 | + yield assistantResult(); |
| 27 | + })(); |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +describe("createRealCliSubprocessSpawn (#5131)", () => { |
| 32 | + it("captures stdout and a zero exit code from a real short-lived process", async () => { |
| 33 | + const spawnFn = createRealCliSubprocessSpawn(); |
| 34 | + const result = await spawnFn(process.execPath, ["-e", "process.stdout.write('hello')"], { |
| 35 | + cwd: process.cwd(), |
| 36 | + env: process.env, |
| 37 | + timeoutMs: 5000, |
| 38 | + }); |
| 39 | + expect(result).toEqual({ stdout: "hello", code: 0, stderr: "" }); |
| 40 | + }); |
| 41 | + |
| 42 | + it("captures stderr and a non-zero exit code", async () => { |
| 43 | + const spawnFn = createRealCliSubprocessSpawn(); |
| 44 | + const result = await spawnFn(process.execPath, ["-e", "process.stderr.write('oops'); process.exit(2)"], { |
| 45 | + cwd: process.cwd(), |
| 46 | + env: process.env, |
| 47 | + timeoutMs: 5000, |
| 48 | + }); |
| 49 | + expect(result.code).toBe(2); |
| 50 | + expect(result.stderr).toBe("oops"); |
| 51 | + }); |
| 52 | + |
| 53 | + it("resolves (never rejects) with code:null and the error message on stderr when the command doesn't exist", async () => { |
| 54 | + const spawnFn = createRealCliSubprocessSpawn(); |
| 55 | + const result = await spawnFn("this-command-definitely-does-not-exist-xyz", [], { |
| 56 | + cwd: process.cwd(), |
| 57 | + env: process.env, |
| 58 | + timeoutMs: 5000, |
| 59 | + }); |
| 60 | + expect(result.code).toBeNull(); |
| 61 | + expect(result.stderr).toContain("this-command-definitely-does-not-exist-xyz"); |
| 62 | + }); |
| 63 | + |
| 64 | + it("kills a long-lived process and resolves with timedOut:true when the caller-supplied timeout elapses", async () => { |
| 65 | + const spawnFn = createRealCliSubprocessSpawn(); |
| 66 | + const result = await spawnFn(process.execPath, ["-e", "setInterval(() => {}, 50)"], { |
| 67 | + cwd: process.cwd(), |
| 68 | + env: process.env, |
| 69 | + timeoutMs: 100, |
| 70 | + }); |
| 71 | + expect(result.timedOut).toBe(true); |
| 72 | + expect(result.code).toBeNull(); |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | +describe("constructProductionCodingAgentDriver (#5131)", () => { |
| 77 | + it("fails closed (throws) when MINER_CODING_AGENT_PROVIDER is unset", () => { |
| 78 | + expect(() => constructProductionCodingAgentDriver({})).toThrow(/unconfigured_coding_agent_driver/); |
| 79 | + }); |
| 80 | + |
| 81 | + it("fails closed when every configured name is unknown (deny-by-default)", () => { |
| 82 | + expect(() => constructProductionCodingAgentDriver({ MINER_CODING_AGENT_PROVIDER: "bogus" })).toThrow( |
| 83 | + /unconfigured_coding_agent_driver/, |
| 84 | + ); |
| 85 | + }); |
| 86 | + |
| 87 | + it("resolves the FIRST configured name from a comma-separated list, skipping unknown entries", async () => { |
| 88 | + const driver = constructProductionCodingAgentDriver({ MINER_CODING_AGENT_PROVIDER: "bogus,noop" }); |
| 89 | + const result = await driver.run(task); |
| 90 | + expect(result.ok).toBe(true); |
| 91 | + }); |
| 92 | + |
| 93 | + it("constructs a real, working driver for the noop provider (no spawn required)", async () => { |
| 94 | + const driver = constructProductionCodingAgentDriver({ MINER_CODING_AGENT_PROVIDER: "noop" }); |
| 95 | + const result = await driver.run(task); |
| 96 | + expect(result.ok).toBe(true); |
| 97 | + expect(result.changedFiles).toEqual([]); |
| 98 | + }); |
| 99 | + |
| 100 | + it("constructs a claude-cli driver wired to an injected spawn, without invoking it during construction", async () => { |
| 101 | + const calls: Array<{ cmd: string; args: readonly string[] }> = []; |
| 102 | + const driver = constructProductionCodingAgentDriver( |
| 103 | + { MINER_CODING_AGENT_PROVIDER: "claude-cli" }, |
| 104 | + { |
| 105 | + spawn: async (cmd, args) => { |
| 106 | + calls.push({ cmd, args }); |
| 107 | + return { stdout: "done", code: 0 }; |
| 108 | + }, |
| 109 | + }, |
| 110 | + ); |
| 111 | + expect(calls).toHaveLength(0); // construction alone must not spawn anything |
| 112 | + const result = await driver.run(task); |
| 113 | + expect(calls).toHaveLength(1); |
| 114 | + expect(calls[0]!.cmd).toBe("claude"); |
| 115 | + expect(result.ok).toBe(true); |
| 116 | + }); |
| 117 | + |
| 118 | + it("defaults to a real (non-injected) spawn for a CLI provider when the caller supplies none", () => { |
| 119 | + // Construction alone must succeed without ever invoking the real spawn (a real "claude" binary is not |
| 120 | + // present in CI) — proving the `options.spawn ?? createRealCliSubprocessSpawn()` default branch is taken. |
| 121 | + const driver = constructProductionCodingAgentDriver({ MINER_CODING_AGENT_PROVIDER: "claude-cli" }); |
| 122 | + expect(typeof driver.run).toBe("function"); |
| 123 | + }); |
| 124 | + |
| 125 | + it("wires house-rule enforcement into the agent-sdk provider's hooks by default", async () => { |
| 126 | + const captured: { input?: Parameters<AgentSdkQueryFn>[0] } = {}; |
| 127 | + const driver = constructProductionCodingAgentDriver( |
| 128 | + { MINER_CODING_AGENT_PROVIDER: "agent-sdk" }, |
| 129 | + { query: queryCapturing(captured) }, |
| 130 | + ); |
| 131 | + const result = await driver.run(task); |
| 132 | + expect(result.ok).toBe(true); |
| 133 | + |
| 134 | + const hooks = captured.input!.options.hooks as { PreToolUse: Array<{ hooks: Array<(input: unknown) => Promise<unknown>> }> }; |
| 135 | + expect(Object.keys(hooks)).toEqual(["PreToolUse"]); |
| 136 | + // Prove it's a REAL, enforcing hook, not an empty placeholder shape. |
| 137 | + const callback = hooks.PreToolUse[0]!.hooks[0]!; |
| 138 | + const denied = await callback({ tool_name: "Read", tool_input: { file_path: ".env" } }); |
| 139 | + expect(denied).toMatchObject({ hookSpecificOutput: { permissionDecision: "deny" } }); |
| 140 | + }); |
| 141 | + |
| 142 | + it("threads houseRulesConfig/houseRulesOptions into the defaulted hook", async () => { |
| 143 | + const append = vi.fn(); |
| 144 | + const captured: { input?: Parameters<AgentSdkQueryFn>[0] } = {}; |
| 145 | + const driver = constructProductionCodingAgentDriver( |
| 146 | + { MINER_CODING_AGENT_PROVIDER: "agent-sdk" }, |
| 147 | + { |
| 148 | + query: queryCapturing(captured), |
| 149 | + houseRulesConfig: { repoFullName: "acme/widgets" }, |
| 150 | + houseRulesOptions: { append }, |
| 151 | + }, |
| 152 | + ); |
| 153 | + await driver.run(task); |
| 154 | + |
| 155 | + const hooks = captured.input!.options.hooks as { PreToolUse: Array<{ hooks: Array<(input: unknown) => Promise<unknown>> }> }; |
| 156 | + await hooks.PreToolUse[0]!.hooks[0]!({ tool_name: "Read", tool_input: { file_path: ".env" } }); |
| 157 | + expect(append).toHaveBeenCalledWith(expect.objectContaining({ repoFullName: "acme/widgets" })); |
| 158 | + }); |
| 159 | +}); |
0 commit comments