|
4 | 4 | const { generatePlainTextSummary, generateCopilotCliStyleSummary, wrapAgentLogInSection, formatSafeOutputsPreview } = require("./log_parser_shared.cjs"); |
5 | 5 | const { getErrorMessage } = require("./error_helpers.cjs"); |
6 | 6 | const { ERR_API, ERR_CONFIG, ERR_VALIDATION } = require("./error_codes.cjs"); |
| 7 | +const INFERENCE_ACCESS_ERROR_PATTERN = /Access denied by policy settings|invalid access to inference/i; |
| 8 | +const CLAUDE_RATE_LIMIT_PATTERN = /rate_limit_error|429 Too Many Requests|"api_error_status"\s*:\s*429|request rejected \(429\)|rate limit/i; |
| 9 | +const CLAUDE_OVERLOAD_PATTERN = /overloaded_error|"overloaded"/i; |
| 10 | +/** Matches HTTP protocol/status lines such as "HTTP/1.1 503". */ |
| 11 | +const CLAUDE_HTTP_5XX_PROTOCOL_PATTERN = /HTTP(?:\/\d\.\d)?\s+5\d{2}\b/; |
| 12 | +/** Matches structured status fields such as "status: 502" or "status code=500". */ |
| 13 | +const CLAUDE_HTTP_5XX_STATUS_FIELD_PATTERN = /status(?:\s+code)?\s*[:=]\s*5\d{2}\b/; |
| 14 | +/** Matches request/fetch/http error prose that carries an explicit 5xx code. */ |
| 15 | +const CLAUDE_HTTP_5XX_REQUEST_ERROR_PATTERN = /(?:http|fetch|request)\s+(?:failed|error)[^\n]*?\b5\d{2}\b/; |
| 16 | +const CLAUDE_HTTP_5XX_STATUS_PATTERN = new RegExp([CLAUDE_HTTP_5XX_PROTOCOL_PATTERN.source, CLAUDE_HTTP_5XX_STATUS_FIELD_PATTERN.source, CLAUDE_HTTP_5XX_REQUEST_ERROR_PATTERN.source].join("|"), "i"); |
| 17 | +const STARTUP_DIAGNOSTIC_LINE_PATTERN = /(?:ERR_|Error:|CAPIError|Authentication failed|rate[_ -]?limit|429|\b5\d{2}\b|overloaded|inference)/i; |
| 18 | +const MAX_DIAGNOSTIC_TAIL_LINES = 8; |
| 19 | + |
| 20 | +/** |
| 21 | + * Build startup diagnostics for Claude failures with no structured entries. |
| 22 | + * @param {string} rawContent |
| 23 | + * @returns {{ |
| 24 | + * exitCode: string, |
| 25 | + * inferenceAccessError: boolean, |
| 26 | + * aiCreditsRateLimitError: boolean, |
| 27 | + * transientInferenceAvailabilityError: boolean, |
| 28 | + * summaryLine: string, |
| 29 | + * summaryMarkdown: string |
| 30 | + * }} |
| 31 | + */ |
| 32 | +function buildClaudeStartupDiagnostics(rawContent) { |
| 33 | + const content = typeof rawContent === "string" ? rawContent : ""; |
| 34 | + const lines = content |
| 35 | + .split(/\r?\n/) |
| 36 | + .map(line => line.trim()) |
| 37 | + .filter(Boolean); |
| 38 | + const startupLines = lines.filter(line => line.includes("[claude-harness]") || STARTUP_DIAGNOSTIC_LINE_PATTERN.test(line)); |
| 39 | + // Intentionally avoid falling back to arbitrary raw log lines here. For empty-structured-log |
| 40 | + // failures, the unmatched tail can contain unrelated stdout/stderr, prompts, or secrets; when |
| 41 | + // no startup/diagnostic lines are recognized, we emit no tail section rather than risk leaking it. |
| 42 | + const tailLines = startupLines.slice(-MAX_DIAGNOSTIC_TAIL_LINES); |
| 43 | + const tailText = tailLines.join("\n"); |
| 44 | + const safeTailText = escapeHtml(tailText); |
| 45 | + |
| 46 | + let exitCode = "unknown"; |
| 47 | + const donePrefix = "done: exitCode="; |
| 48 | + const doneIdx = content.lastIndexOf(donePrefix); |
| 49 | + if (doneIdx !== -1) { |
| 50 | + const doneTail = content.slice(doneIdx + donePrefix.length); |
| 51 | + const doneCode = doneTail.match(/^(\d+)/); |
| 52 | + if (doneCode) { |
| 53 | + exitCode = doneCode[1]; |
| 54 | + } |
| 55 | + } else { |
| 56 | + const failedPrefix = "failed: exitCode="; |
| 57 | + const failedIdx = content.lastIndexOf(failedPrefix); |
| 58 | + if (failedIdx !== -1) { |
| 59 | + const failedTail = content.slice(failedIdx + failedPrefix.length); |
| 60 | + const failedCode = failedTail.match(/^(\d+)/); |
| 61 | + if (failedCode) { |
| 62 | + exitCode = failedCode[1]; |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + const inferenceAccessError = INFERENCE_ACCESS_ERROR_PATTERN.test(content); |
| 68 | + const aiCreditsRateLimitError = CLAUDE_RATE_LIMIT_PATTERN.test(content) || CLAUDE_OVERLOAD_PATTERN.test(content); |
| 69 | + const transientInferenceAvailabilityError = aiCreditsRateLimitError || CLAUDE_HTTP_5XX_STATUS_PATTERN.test(content); |
| 70 | + const summaryLine = `Claude startup failed before structured logging (exitCode=${exitCode}).`; |
| 71 | + const summaryMarkdown = safeTailText ? `<details><summary>Claude startup diagnostics</summary>\n\n<pre><code>${safeTailText}</code></pre>\n</details>` : ""; |
| 72 | + |
| 73 | + return { |
| 74 | + exitCode, |
| 75 | + inferenceAccessError, |
| 76 | + aiCreditsRateLimitError, |
| 77 | + transientInferenceAvailabilityError, |
| 78 | + summaryLine, |
| 79 | + summaryMarkdown, |
| 80 | + }; |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * @param {string} text |
| 85 | + * @returns {string} |
| 86 | + */ |
| 87 | +function escapeHtml(text) { |
| 88 | + const htmlEntities = { '"': """, "'": "'", "&": "&", "<": "<", ">": ">" }; |
| 89 | + return text.replace(/["'&<>]/g, char => htmlEntities[char]); |
| 90 | +} |
7 | 91 |
|
8 | 92 | /** |
9 | 93 | * Bootstrap helper for log parser entry points. |
@@ -344,7 +428,26 @@ async function runLogParser(options) { |
344 | 428 | `Claude produced no structured log entries, but agent completed with ${safeOutputEntriesCount} safe output ${safeOutputEntriesCount === 1 ? "entry" : "entries"} — treating as non-fatal post-completion infrastructure failure` |
345 | 429 | ); |
346 | 430 | } else { |
347 | | - core.setFailed(`${ERR_CONFIG}: Claude execution failed: no structured log entries were produced. This usually indicates a startup or configuration error before tool execution.`); |
| 431 | + const diagnostics = buildClaudeStartupDiagnostics(content); |
| 432 | + if (diagnostics.summaryMarkdown) { |
| 433 | + await core.summary.addRaw(diagnostics.summaryMarkdown).write(); |
| 434 | + } |
| 435 | + |
| 436 | + if (diagnostics.inferenceAccessError) { |
| 437 | + core.setOutput("inference_access_error", "true"); |
| 438 | + } |
| 439 | + if (diagnostics.aiCreditsRateLimitError) { |
| 440 | + core.setOutput("ai_credits_rate_limit_error", "true"); |
| 441 | + } |
| 442 | + |
| 443 | + const errorCode = diagnostics.inferenceAccessError || diagnostics.transientInferenceAvailabilityError ? ERR_API : ERR_CONFIG; |
| 444 | + const failureKind = diagnostics.inferenceAccessError |
| 445 | + ? "inference access denied by policy" |
| 446 | + : diagnostics.transientInferenceAvailabilityError |
| 447 | + ? "transient inference availability signal detected" |
| 448 | + : "startup/configuration failure detected"; |
| 449 | + |
| 450 | + core.setFailed(`${errorCode}: Claude execution failed: no structured log entries were produced. ${diagnostics.summaryLine} ${failureKind}.`); |
348 | 451 | } |
349 | 452 | } |
350 | 453 |
|
|
0 commit comments