Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions .github/scripts/run-agent-task/execute-native-agent-task.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 11 additions & 1 deletion tests/execute-native-agent-task-lifecycle.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down
Loading