mirror of
https://github.com/anthropics/claude-code-action.git
synced 2026-08-21 19:08:57 +08:00
fix: write execution file when SDK throws (#1255)
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
import * as core from "@actions/core";
|
||||
import { existsSync } from "fs";
|
||||
import { writeFile } from "fs/promises";
|
||||
import { join } from "path";
|
||||
|
||||
const EXECUTION_FILENAME = "claude-execution-output.json";
|
||||
|
||||
export function getExecutionFilePath(): string | undefined {
|
||||
if (!process.env.RUNNER_TEMP) {
|
||||
return undefined;
|
||||
}
|
||||
return join(process.env.RUNNER_TEMP, EXECUTION_FILENAME);
|
||||
}
|
||||
|
||||
export async function writeExecutionFile(
|
||||
messages: unknown[],
|
||||
): Promise<string | undefined> {
|
||||
const executionFile = getExecutionFilePath();
|
||||
if (!executionFile) {
|
||||
core.warning("Failed to write execution file: RUNNER_TEMP is not set");
|
||||
return undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
await writeFile(executionFile, JSON.stringify(messages, null, 2));
|
||||
console.log(`Log saved to ${executionFile}`);
|
||||
return executionFile;
|
||||
} catch (error) {
|
||||
core.warning(`Failed to write execution file: ${error}`);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
export function setExecutionFileOutputIfPresent(): string | undefined {
|
||||
const executionFile = getExecutionFilePath();
|
||||
if (!executionFile || !existsSync(executionFile)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
core.setOutput("execution_file", executionFile);
|
||||
return executionFile;
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import { runClaude } from "./run-claude";
|
||||
import { setupClaudeCodeSettings } from "./setup-claude-code-settings";
|
||||
import { validateEnvironmentVariables } from "./validate-env";
|
||||
import { installPlugins } from "./install-plugins";
|
||||
import { setExecutionFileOutputIfPresent } from "./execution-file";
|
||||
|
||||
async function run() {
|
||||
try {
|
||||
@@ -62,6 +63,7 @@ async function run() {
|
||||
core.setOutput("structured_output", result.structuredOutput);
|
||||
}
|
||||
} catch (error) {
|
||||
setExecutionFileOutputIfPresent();
|
||||
core.setFailed(`Action failed with error: ${error}`);
|
||||
core.setOutput("conclusion", "failure");
|
||||
process.exit(1);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import * as core from "@actions/core";
|
||||
import { readFile, writeFile, access } from "fs/promises";
|
||||
import { readFile, access } from "fs/promises";
|
||||
import { dirname, join } from "path";
|
||||
import { query } from "@anthropic-ai/claude-agent-sdk";
|
||||
import type {
|
||||
@@ -8,6 +8,7 @@ import type {
|
||||
SDKUserMessage,
|
||||
} from "@anthropic-ai/claude-agent-sdk";
|
||||
import type { ParsedSdkOptions } from "./parse-sdk-options";
|
||||
import { writeExecutionFile } from "./execution-file";
|
||||
|
||||
export type ClaudeRunResult = {
|
||||
executionFile?: string;
|
||||
@@ -16,8 +17,6 @@ export type ClaudeRunResult = {
|
||||
structuredOutput?: string;
|
||||
};
|
||||
|
||||
const EXECUTION_FILE = `${process.env.RUNNER_TEMP}/claude-execution-output.json`;
|
||||
|
||||
/** Filename for the user request file, written by prompt generation */
|
||||
const USER_REQUEST_FILENAME = "claude-user-request.txt";
|
||||
|
||||
@@ -172,6 +171,7 @@ export async function runClaudeWithSdk(
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("SDK execution error:", error);
|
||||
await writeExecutionFile(messages);
|
||||
throw new Error(`SDK execution error: ${error}`);
|
||||
}
|
||||
|
||||
@@ -179,13 +179,9 @@ export async function runClaudeWithSdk(
|
||||
conclusion: "failure",
|
||||
};
|
||||
|
||||
// Write execution file
|
||||
try {
|
||||
await writeFile(EXECUTION_FILE, JSON.stringify(messages, null, 2));
|
||||
console.log(`Log saved to ${EXECUTION_FILE}`);
|
||||
result.executionFile = EXECUTION_FILE;
|
||||
} catch (error) {
|
||||
core.warning(`Failed to write execution file: ${error}`);
|
||||
const executionFile = await writeExecutionFile(messages);
|
||||
if (executionFile) {
|
||||
result.executionFile = executionFile;
|
||||
}
|
||||
|
||||
// Extract session_id from system.init message
|
||||
|
||||
Reference in New Issue
Block a user