From 3386346e46224b9e2566f6b9f469834ca2a2e72a Mon Sep 17 00:00:00 2001 From: Chris Huber Date: Sat, 5 Dec 2026 00:15:41 +0000 Subject: [PATCH] fix(run-agent-task): emit an error annotation with the failure reason when an agent task fails --- .../execute-native-agent-task.mjs | 47 ++++++++++++++++++- ...ecute-native-agent-task-lifecycle.test.mjs | 12 ++++- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/.github/scripts/run-agent-task/execute-native-agent-task.mjs b/.github/scripts/run-agent-task/execute-native-agent-task.mjs index 419eb09a..799ebca6 100644 --- a/.github/scripts/run-agent-task/execute-native-agent-task.mjs +++ b/.github/scripts/run-agent-task/execute-native-agent-task.mjs @@ -568,6 +568,36 @@ function failureClassification(error) { return { code: "wp-codebox.agent-task.execution", classification: "execution" } } +const MAX_FAILURE_ANNOTATION_CHARS = 1000 + +function escapeWorkflowCommandData(value) { + return String(value).replace(/%/g, "%25").replace(/\r/g, "%0D").replace(/\n/g, "%0A") +} + +function escapeWorkflowCommandProperty(value) { + return escapeWorkflowCommandData(value).replace(/:/g, "%3A").replace(/,/g, "%2C") +} + +// Render one GitHub Actions error annotation from a normalized failure so the +// failure reason is visible in the job log, run annotations and notifications +// without downloading the result artifact. +function failureAnnotation(failure = {}) { + const stage = typeof failure.stage === "string" && failure.stage ? failure.stage : (failure.classification || "execution") + const code = typeof failure.code === "string" && failure.code ? failure.code : (failure.classification || "wp-codebox.agent-task.failed") + const rawMessage = typeof failure.message === "string" && failure.message ? failure.message : "Agent task failed; see the workflow result artifact for details." + const safeMessage = sanitizeRuntimeSourceText(redact(rawMessage), privateRuntimeSourceRootForSanitization) + const message = safeMessage.length > MAX_FAILURE_ANNOTATION_CHARS ? `${safeMessage.slice(0, MAX_FAILURE_ANNOTATION_CHARS)}… [truncated]` : safeMessage + return `::error title=${escapeWorkflowCommandProperty(`Agent task failed (${stage})`)}::${escapeWorkflowCommandData(`${code}: ${message}`)}` +} + +function emitFailureAnnotation(failure) { + try { + console.log(failureAnnotation(failure)) + } catch { + // Annotation emission must never mask the underlying failure. + } +} + async function writeNormalizedFailure(error, request = {}) { const resultPath = join(workspace, ".codebox", "agent-task-workflow-result.json") const failure = failureClassification(error) @@ -589,6 +619,7 @@ async function writeNormalizedFailure(error, request = {}) { await writeFile(resultPath, `${JSON.stringify(sanitized, null, 2)}\n`) await output("job_status", "failed") await output("result_path", ".codebox/agent-task-workflow-result.json") + return sanitized.failure } function underRoot(root, path) { @@ -1030,13 +1061,25 @@ await output("credential_mode", result.access.credential_mode) await output("declared_artifacts_json", workflowOutputs.declared_artifacts_json.rendered) await output("result_path", ".codebox/agent-task-workflow-result.json") -if (!success) process.exitCode = 1 +if (!success) { + emitFailureAnnotation(sanitizedResult.failure || sanitizedResult.projection_error || { + stage: request.run_agent && !request.dry_run ? "agent" : "execution", + code: "wp-codebox.agent-task.failed", + message: sanitizedResult.publication_error || "", + }) + process.exitCode = 1 +} } try { await executeNativeAgentTask() } catch (error) { - await writeNormalizedFailure(error) + let normalizedFailure + try { + normalizedFailure = await writeNormalizedFailure(error) + } finally { + emitFailureAnnotation(normalizedFailure || { ...failureClassification(error), message: error instanceof Error ? error.message : String(error) }) + } console.error(bounded(error instanceof Error ? error.message : String(error), MAX_WORKFLOW_OUTPUT_BYTES)) process.exitCode = 1 } finally { diff --git a/tests/execute-native-agent-task-lifecycle.test.mjs b/tests/execute-native-agent-task-lifecycle.test.mjs index 067c3104..aa95bc73 100644 --- a/tests/execute-native-agent-task-lifecycle.test.mjs +++ b/tests/execute-native-agent-task-lifecycle.test.mjs @@ -229,8 +229,10 @@ process.stdout.write(JSON.stringify({ }, }) let stderr = "" + let stdout = "" + child.stdout.on("data", (chunk) => { stdout += chunk }) child.stderr.on("data", (chunk) => { stderr += chunk }) - child.on("close", (code) => done({ code, stderr })) + child.on("close", (code) => done({ code, stdout, stderr })) }) return { @@ -241,8 +243,11 @@ process.stdout.write(JSON.stringify({ } } +const failureAnnotations = (run) => run.stdout.split("\n").filter((line) => line.startsWith("::error")) + const success = await run() assert.equal(success.code, 0, `${success.stderr}\n${JSON.stringify(success.result)}`) +assert.deepEqual(failureAnnotations(success), [], "successful runs emit no failure annotation") assert.equal(JSON.parse(await readFile(join(success.workspace, ".codebox", "runtime-chat-input.json"), "utf8")).max_turns, 1, "the caller's max_turns reaches the runtime package agents/chat input") assert.equal(success.result.terminal_status, undefined) assert.equal(success.result.agent_turn_limit?.status ?? "within_limit", "within_limit") @@ -397,6 +402,8 @@ assert.equal(applyReject.code, 1) assert.equal(await readFile(join(applyReject.workspace, "README.md"), "utf8"), "OPENAI_API_KEY\nbefore\n", "a rejected patch does not partially mutate the matching host workspace") assert.equal(applyReject.result.failure.stage, "apply") assert.match(applyReject.result.failure.message, /Host git apply failed/) +assert.equal(failureAnnotations(applyReject).length, 1) +assert.match(failureAnnotations(applyReject)[0], /^::error title=Agent task failed \(apply\)::[^\r\n]+$/) assert.deepEqual(applyReject.result.failure.evidence.expected_identity, applyReject.result.failure.evidence.actual_identity, "the failure records the matching seed and host identities") assert.equal(applyReject.result.failure.evidence.patch.artifact_path, "files/patch.diff") assert.equal(applyReject.result.failure.evidence.changed_files.artifact_path, "files/changed-files.json") @@ -423,6 +430,9 @@ for (const mode of ["max-turns", "max-turns-maintenance"]) { assert.equal(capped.result.failure.classification, "max_turns") assert.equal(capped.result.failure.max_turns, 1) assert.match(capped.result.failure.message, /1-turn limit before producing a final answer/) + assert.deepEqual(failureAnnotations(capped), [ + `::error title=Agent task failed (agent)::max_turns_exceeded: ${capped.result.failure.message.replace(/%/g, "%25").replace(/\r/g, "%0D").replace(/\n/g, "%0A")}`, + ], "a failed run prints exactly one error annotation carrying the failure stage, code and message") assert.equal(capped.result.agent_turn_limit.status, "max_turns") assert.equal(capped.result.agent_turn_limit.source, mode === "max-turns" ? "terminal-result" : "tool-observability") assert.equal(capped.result.workspace_apply.status, "skipped", "partial work from a turn-capped agent is never applied")