Skip to content

Commit bf7ba42

Browse files
chore: sync actions from [email protected] (#183)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent cec6394 commit bf7ba42

9 files changed

Lines changed: 603 additions & 39 deletions

‎setup/js/claude_harness.cjs‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ const MAX_TURNS_EXIT_PATTERN = /"subtype"\s*:\s*"error_max_turns"/;
7878
// this path must not be retried via --continue (fall back to a fresh run if budget remains).
7979
const NO_DEFERRED_MARKER_PATTERN = /No deferred tool marker found/i;
8080
const SIGNAL_TERMINATION_EXIT_CODES = new Set([137, 143]);
81+
const MAX_STARTUP_RETRIES = 2;
8182

8283
/**
8384
* Emit a timestamped diagnostic log line to stderr.
@@ -99,6 +100,35 @@ function resolveRetryConfig(env = process.env, logger = log) {
99100
return resolveSharedRetryConfig(env, logger);
100101
}
101102

103+
/**
104+
* Parse bounded startup retries for zero-output startup failures.
105+
* Retries are always fresh runs (never --continue).
106+
*
107+
* @param {NodeJS.ProcessEnv} [env]
108+
* @param {(message: string) => void} [logger]
109+
* @returns {number}
110+
*/
111+
function resolveStartupRetryLimit(env = process.env, logger = log) {
112+
const raw = env.GH_AW_CLAUDE_STARTUP_RETRIES;
113+
if (raw == null || raw === "") {
114+
return 1;
115+
}
116+
if (!/^[+-]?\d+$/.test(raw)) {
117+
logger(`invalid GH_AW_CLAUDE_STARTUP_RETRIES='${raw}' (expected integer); using default startup retries=1`);
118+
return 1;
119+
}
120+
const parsed = Number.parseInt(raw, 10);
121+
if (!Number.isFinite(parsed)) {
122+
logger(`invalid GH_AW_CLAUDE_STARTUP_RETRIES='${raw}' (not finite); using default startup retries=1`);
123+
return 1;
124+
}
125+
const clamped = Math.min(Math.max(parsed, 0), MAX_STARTUP_RETRIES);
126+
if (clamped !== parsed) {
127+
logger(`GH_AW_CLAUDE_STARTUP_RETRIES=${parsed} out of range; clamped to ${clamped}`);
128+
}
129+
return clamped;
130+
}
131+
102132
/**
103133
* Determines if the collected output contains an Anthropic overload error.
104134
* @param {string} output - Collected stdout+stderr from the process
@@ -297,6 +327,7 @@ async function main() {
297327
const [, , command, ...args] = process.argv;
298328
const retryConfig = resolveRetryConfig(process.env, log);
299329
const { maxRetries, initialDelayMs, backoffMultiplier, maxDelayMs } = retryConfig;
330+
const startupRetryLimit = resolveStartupRetryLimit(process.env, log);
300331

301332
if (!command) {
302333
process.stderr.write("claude-harness: Usage: node claude_harness.cjs <command> [args...]\n");
@@ -347,6 +378,7 @@ async function main() {
347378
let lastExitCode = 1;
348379
let useContinueOnRetry = false;
349380
let continueDisabledPermanently = false;
381+
let startupRetriesUsed = 0;
350382
const driverStartTime = Date.now();
351383
// Soft-timeout guard: polled at the top of the retry loop and after each backoff sleep.
352384
// It does not preempt a running attempt — if a single invocation runs past the soft
@@ -519,7 +551,19 @@ async function main() {
519551
if (attempt >= maxRetries) {
520552
log(`all ${maxRetries} retries exhausted — giving up (exitCode=${lastExitCode})`);
521553
} else {
522-
log(`attempt ${attempt + 1}: no output produced — not retrying` + ` (possible causes: binary not found, permission denied, auth failure, or silent startup crash)`);
554+
if (startupRetriesUsed < startupRetryLimit) {
555+
startupRetriesUsed++;
556+
useContinueOnRetry = false;
557+
delay = initialDelayMs;
558+
// attempt is 0-based; logs are 1-based, so "next attempt" is +2.
559+
const nextAttemptNumber = attempt + 2;
560+
const totalAttempts = maxRetries + 1;
561+
log(`attempt ${attempt + 1}: no output produced — retrying startup as fresh run ` + `(startup retry ${startupRetriesUsed}/${startupRetryLimit}, next attempt ${nextAttemptNumber} of ${totalAttempts} total attempts)`);
562+
continue;
563+
}
564+
log(
565+
`attempt ${attempt + 1}: no output produced — not retrying (startup retry budget exhausted: ${startupRetriesUsed}/${startupRetryLimit}; possible causes: binary not found, permission denied, auth failure, or silent startup crash)`
566+
);
523567
}
524568

525569
break;
@@ -551,6 +595,7 @@ if (typeof module !== "undefined" && module.exports) {
551595
hasNoopInSafeOutputs,
552596
hasExpectedSafeOutputs,
553597
resolveRetryConfig,
598+
resolveStartupRetryLimit,
554599
};
555600
}
556601

‎setup/js/copilot_sdk_session.cjs‎

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,19 @@ const SDK_SEND_TIMEOUT_MS_DEFAULT = 10 * 60 * 1000;
5050
// Keep in sync with SDK_SESSION_IDLE_TIMEOUT_PATTERN in copilot_harness.cjs.
5151
const SDK_IDLE_TIMEOUT_PATTERN = /Timeout after \d+ms waiting for session\.idle/;
5252

53-
// Default idle period for the post-completion watchdog: 5 minutes.
53+
// Default idle period for the post-completion watchdog: 30 seconds.
5454
// When the agent has produced output and all tracked tool calls have completed,
5555
// the driver arms a watchdog timer. If no new SDK events arrive within this
5656
// window, the driver force-disconnects the session and treats it as a successful
5757
// completion — covering the SDK driver bug where sendAndWait never resolves after
5858
// the final tool result is returned.
59+
// 30 s is chosen to be well under typical step timeouts (≥ 5 min) while still
60+
// being long enough to avoid false-positives during legitimate LLM inference gaps
61+
// (the session.on handler disarms the watchdog on every assistant.turn_start event,
62+
// so the timer only fires when the session is truly quiescent: output collected,
63+
// no tool calls in flight, and no active inference turn).
5964
// Override via the GH_AW_SDK_IDLE_MS environment variable.
60-
const SDK_POST_COMPLETION_IDLE_MS_DEFAULT = 5 * 60 * 1000;
65+
const SDK_POST_COMPLETION_IDLE_MS_DEFAULT = 30 * 1000;
6166

6267
/**
6368
* Extract the prompt text from a resolved args array.
@@ -479,16 +484,51 @@ async function runWithCopilotSDK({ sdkUri, prompt, logger, attempt = 0, model, c
479484
if (stream) {
480485
await new Promise(resolve => stream.end(resolve));
481486
}
487+
// Timeout budget for each cleanup operation. Prevents the driver from
488+
// hanging indefinitely when the server is unresponsive after sendAndWait
489+
// times out or the watchdog force-disconnects the session.
490+
// The helper resolves normally on success and also on timeout (signalling
491+
// timeout via the returned boolean) so the caller can log a warning.
492+
const CLEANUP_TIMEOUT_MS = 5_000;
493+
/**
494+
* Race a cleanup promise against a fixed deadline.
495+
* Resolves to true when the promise settled in time, false on timeout.
496+
* @param {Promise<unknown>} p
497+
* @returns {Promise<boolean>}
498+
*/
499+
const withCleanupTimeout = p => {
500+
let timeoutId = null;
501+
const deadline = new Promise(resolve => {
502+
timeoutId = setTimeout(() => {
503+
timeoutId = null;
504+
resolve(false);
505+
}, CLEANUP_TIMEOUT_MS);
506+
if (typeof timeoutId?.unref === "function") timeoutId.unref();
507+
});
508+
return Promise.race([
509+
p.then(
510+
() => true,
511+
() => true
512+
),
513+
deadline,
514+
]).then(settled => {
515+
if (timeoutId) clearTimeout(timeoutId);
516+
if (!settled) {
517+
log(`warning: cleanup operation timed out after ${CLEANUP_TIMEOUT_MS}ms`);
518+
}
519+
return settled;
520+
});
521+
};
482522
if (session) {
483523
try {
484-
await session.disconnect();
524+
await withCleanupTimeout(session.disconnect());
485525
} catch {
486526
// best-effort cleanup
487527
}
488528
}
489529
if (clientStarted) {
490530
try {
491-
await client.stop();
531+
await withCleanupTimeout(client.stop());
492532
} catch {
493533
// best-effort cleanup
494534
}

‎setup/js/glob_pattern_helpers.cjs‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ function escapeRegexChars(pattern) {
2020
* @param {Object} [options] - Options for pattern conversion
2121
* @param {boolean} [options.pathMode=true] - If true, * matches non-slash chars; if false, * matches any char
2222
* @param {boolean} [options.caseSensitive=true] - Whether matching should be case-sensitive
23+
* @param {boolean} [options.matchSubfolderRoot=false] - If true and the pattern contains no "/",
24+
* the pattern is required to be prefixed by exactly one path segment (i.e. matches only at the
25+
* root of a single memory subfolder: "subfolder/file.json"). Files at the artifact root (depth 0)
26+
* and files deeper than one level (depth 2+) are not matched.
2327
* @returns {RegExp} - Regular expression that matches the pattern
2428
*
2529
* Supports:
@@ -37,9 +41,15 @@ function escapeRegexChars(pattern) {
3741
* const regex = globPatternToRegex("metrics/**");
3842
* regex.test("metrics/data.json"); // true
3943
* regex.test("metrics/daily/data.json"); // true
44+
*
45+
* @example
46+
* const regex = globPatternToRegex("*.json", { matchSubfolderRoot: true });
47+
* regex.test("discussion-task-miner/processed-discussions.json"); // true (depth 1)
48+
* regex.test("file.json"); // false (depth 0 – not matched)
49+
* regex.test("discussion-task-miner/archive/old.json"); // false (depth 2 – not matched)
4050
*/
4151
function globPatternToRegex(pattern, options) {
42-
const { pathMode = true, caseSensitive = true } = options || {};
52+
const { pathMode = true, caseSensitive = true, matchSubfolderRoot = false } = options || {};
4353

4454
let regexPattern = escapeRegexChars(pattern);
4555

@@ -54,6 +64,13 @@ function globPatternToRegex(pattern, options) {
5464
regexPattern = regexPattern.replace(/\*/g, ".*");
5565
}
5666

67+
// matchSubfolderRoot: slashless patterns must match exactly at the root of one subfolder
68+
// (depth 1 only – neither depth 0 nor depth 2+).
69+
// Patterns that already contain a "/" are matched against the full relative path unchanged.
70+
if (matchSubfolderRoot && !pattern.includes("/")) {
71+
regexPattern = `[^/]+/${regexPattern}`;
72+
}
73+
5774
return new RegExp(`^${regexPattern}$`, caseSensitive ? "" : "i");
5875
}
5976

‎setup/js/log_parser_bootstrap.cjs‎

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,90 @@
44
const { generatePlainTextSummary, generateCopilotCliStyleSummary, wrapAgentLogInSection, formatSafeOutputsPreview } = require("./log_parser_shared.cjs");
55
const { getErrorMessage } = require("./error_helpers.cjs");
66
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 = { '"': "&quot;", "'": "&#39;", "&": "&amp;", "<": "&lt;", ">": "&gt;" };
89+
return text.replace(/["'&<>]/g, char => htmlEntities[char]);
90+
}
791

892
/**
993
* Bootstrap helper for log parser entry points.
@@ -344,7 +428,26 @@ async function runLogParser(options) {
344428
`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`
345429
);
346430
} 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}.`);
348451
}
349452
}
350453

0 commit comments

Comments
 (0)