fix: enforce max turns from claude args (#1607)

This commit is contained in:
ulofiai
2026-08-07 07:55:06 -07:00
committed by GitHub
parent 1623c36729
commit 6ef6450f51
4 changed files with 111 additions and 1 deletions
+67
View File
@@ -128,4 +128,71 @@ describe("runClaudeWithSdk", () => {
coreErrorSpy.mockRestore();
}
});
test("fails closed when a successful result exceeds maxTurns", async () => {
const consoleErrorSpy = spyOn(console, "error").mockImplementation(
() => {},
);
const consoleLogSpy = spyOn(console, "log").mockImplementation(() => {});
const coreErrorSpy = spyOn(
await import("@actions/core"),
"error",
).mockImplementation(() => {});
tempDir = await mkdtemp(join(tmpdir(), "claude-sdk-"));
process.env.RUNNER_TEMP = tempDir;
const promptPath = join(tempDir, "prompt.txt");
await writeFile(promptPath, "test prompt");
const initMessage = {
type: "system",
subtype: "init",
session_id: "session-123",
model: "claude-opus-4-7",
};
const successResultMessage = {
type: "result",
subtype: "success",
is_error: false,
duration_ms: 960000,
num_turns: 73,
total_cost_usd: 0,
permission_denials: [],
};
mock.module("@anthropic-ai/claude-agent-sdk", () => ({
query: async function* () {
yield initMessage;
yield successResultMessage;
},
}));
try {
const { runClaudeWithSdk } = await import("../src/run-claude-sdk");
await expect(
runClaudeWithSdk(promptPath, {
sdkOptions: { maxTurns: 60 },
showFullOutput: false,
hasJsonSchema: false,
}),
).rejects.toThrow(
"Claude reported a successful result after 73 turns, exceeding the configured maximum of 60",
);
const executionFile = join(tempDir, "claude-execution-output.json");
await expect(readFile(executionFile, "utf-8")).resolves.toBe(
JSON.stringify([initMessage, successResultMessage], null, 2),
);
expect(coreErrorSpy).toHaveBeenCalledWith(
"Claude reported a successful result after 73 turns, exceeding the configured maximum of 60",
);
} finally {
consoleErrorSpy.mockRestore();
consoleLogSpy.mockRestore();
coreErrorSpy.mockRestore();
}
});
});