diff --git a/.github/actions/report-lane-outcome/action.yml b/.github/actions/report-lane-outcome/action.yml index b588fb17..330f14ff 100644 --- a/.github/actions/report-lane-outcome/action.yml +++ b/.github/actions/report-lane-outcome/action.yml @@ -82,7 +82,9 @@ inputs: fallback-reason: description: >- Why this run used a fallback credential: `credit-stop` (the key job - credit-stopped) or `no-key` (the key was empty). Printed as + credit-stopped), `auth` (the API refused the key job's key with status + 401, disabled or deleted; 402, billing; or 403, permission) or `no-key` + (the key was empty). Printed as `fallback=` right after `credential=`. Empty prints nothing; any other value fails the step before it reports. required: false diff --git a/.github/actions/report-lane-outcome/classify.cjs b/.github/actions/report-lane-outcome/classify.cjs index bf2951f2..0565bf8b 100644 --- a/.github/actions/report-lane-outcome/classify.cjs +++ b/.github/actions/report-lane-outcome/classify.cjs @@ -262,8 +262,8 @@ const MODEL_PATTERN = /^[\w.:@[\]-]{1,100}$/u; const COUNT_PATTERN = /^\d{1,6}$/u; const WINDOW_PATTERN = /^[a-z0-9_]{1,40}$/u; // Why a subscription job ran in the two-job pattern: the key job's credit -// stop, or no key at all (ci-workflows#806). -const FALLBACK_REASONS = ["credit-stop", "no-key"]; +// stop, its key refused with API status 401, 402 or 403, or no key at all. +const FALLBACK_REASONS = ["credit-stop", "auth", "no-key"]; // SDK `resetsAt` is epoch time; a value past 1e12 can only be milliseconds. function isoFromEpoch(value) { diff --git a/.github/actions/report-lane-outcome/classify.test.cjs b/.github/actions/report-lane-outcome/classify.test.cjs index 3bd05b50..234f42ff 100644 --- a/.github/actions/report-lane-outcome/classify.test.cjs +++ b/.github/actions/report-lane-outcome/classify.test.cjs @@ -486,9 +486,13 @@ test("record tokens: a fallback reason prints right after the credential, only f recordTokens(run, { credential, fallbackReason: "no-key" }), "credential=AUTOMATIONS_CLAUDE_CODE_OAUTH_TOKEN fallback=no-key turns=4", ); + assert.equal( + recordTokens(run, { credential, fallbackReason: "auth" }), + "credential=AUTOMATIONS_CLAUDE_CODE_OAUTH_TOKEN fallback=auth turns=4", + ); for (const forged of [ "", - "auth", + "Auth", "Credit-Stop", " no-key", "credit-stop\n::error::x", diff --git a/.github/actions/report-lane-outcome/outcome-step.test.cjs b/.github/actions/report-lane-outcome/outcome-step.test.cjs index e253240f..1a44f9f2 100644 --- a/.github/actions/report-lane-outcome/outcome-step.test.cjs +++ b/.github/actions/report-lane-outcome/outcome-step.test.cjs @@ -468,12 +468,30 @@ test("a fallback reason rides beside the credential on the notice and the class assert.doesNotMatch(none.notices[0], /fallback=/u); }); -test("a fallback reason outside credit-stop and no-key fails the step and reports nothing", async () => { +test("an auth fallback reason, a rejected key, is recorded beside the credential", async () => { + const succeeded = writeExecutionFile([ + { type: "result", subtype: "success", is_error: false, num_turns: 2 }, + ]); + const run = await runOutcome({ + outcome: "success", + executionFile: succeeded, + credential: "AUTOMATIONS_CLAUDE_CODE_OAUTH_TOKEN", + fallbackReason: "auth", + }); + assert.deepEqual(run.failed, []); + assert.equal(run.notices.length, 1); + assert.match( + run.notices[0], + / credential=AUTOMATIONS_CLAUDE_CODE_OAUTH_TOKEN fallback=auth turns=2\b/u, + ); +}); + +test("a fallback reason outside credit-stop, auth and no-key fails the step and reports nothing", async () => { const file = writeExecutionFile([ { type: "result", subtype: "success", is_error: false, num_turns: 5 }, ]); for (const outcome of ["success", "failure"]) { - for (const fallbackReason of ["auth", "Credit-Stop", " credit-stop"]) { + for (const fallbackReason of ["Auth", "Credit-Stop", " credit-stop"]) { const run = await runOutcome({ outcome, executionFile: file, @@ -483,6 +501,7 @@ test("a fallback reason outside credit-stop and no-key fails the step and report assert.equal(run.failed.length, 1, label); assert.match(run.failed[0], /credit-stop/u, label); assert.match(run.failed[0], /no-key/u, label); + assert.match(run.failed[0], /\bauth\b/u, label); assert.deepEqual(run.notices, [], label); assert.deepEqual(run.errors, [], label); assert.deepEqual(run.warnings, [], label); diff --git a/.github/actions/run-claude-cli/action.yml b/.github/actions/run-claude-cli/action.yml index bd6642b8..653297ad 100644 --- a/.github/actions/run-claude-cli/action.yml +++ b/.github/actions/run-claude-cli/action.yml @@ -3,10 +3,12 @@ description: >- Run one prompt with headless `claude -p` for an agent-mode CI lane, on a Linux runner, on exactly one credential: set `api-key` or `subscription-token`, never both (the action fails closed otherwise). A - key leg that ends in a credit stop (classify.cjs isCreditStop) sets - lane-class `credit-stop`; a caller that falls back to the subscription - runs it as a second job on a fixed GitHub-hosted runner, gated on that - exact value (see the README's two-job pattern), never in this job. + key leg that ends in a credit stop (classify.cjs isCreditStop, lane-class + `credit-stop`) or a key the API refused with status 401, 402 or 403 + (lane-class `auth`) sets handoff `true`; + a caller that falls back to the subscription runs it as a second job on a + fixed GitHub-hosted runner, gated on that exact value (see the README's + two-job pattern), never in this job. The leg's CLI environment has every credential and file-command variable removed and its one credential set, and the CLI runs with `--restricted` and `--strict-mcp-config`; the step's GITHUB_ENV, GITHUB_PATH, GITHUB_OUTPUT, @@ -122,15 +124,14 @@ inputs: default: "" cli-version: description: >- - An exact major.minor.patch, or `latest`, which resolves the newest - @anthropic-ai/claude-code release with `npm view` at run time. The - default is pinned to 2.1.304 (`npm view @anthropic-ai/claude-code - version`, 2026-10-20). The resolved version is logged and output. The + `latest` (the default) resolves the newest @anthropic-ai/claude-code + release with `npm view` at run time; an exact major.minor.patch + reproduces a run. The resolved version is logged and output. The install fails unless `npm audit signatures` verifies the wrapper and platform packages and the binary matches the release's GPG-signed manifest.json, so the version must be 2.1.89 or later. required: false - default: 2.1.304 + default: latest watchdog-max-wait-ms: description: CLAUDE_CODE_RETRY_WATCHDOG_MAX_WAIT_MS, the most each request waits out 429 and 529 errors. required: false @@ -146,11 +147,12 @@ outputs: lane-class: description: >- The leg's failure class; empty on success. `credit-stop` (key leg only) - means the caller may run the subscription leg in a separate job; a - caller that does not reports it as `rate-limit`, the class - report-lane-outcome knows. Otherwise it is report-lane-outcome's - class: a failed install or integrity check reports `install-failed`, - and any other failure before the leg ran, or a leg whose stdout passes + means the key's credits are used up; a caller that does not fall back + reports it as `rate-limit`, the class report-lane-outcome knows. + `auth` means the credential is unusable: API status 401 (disabled or + deleted), 402 (billing) or 403 (permission). Otherwise it is + report-lane-outcome's class: a failed install or integrity check + reports `install-failed`, and any other failure before the leg ran, or a leg whose stdout passes 256 MiB, reports `other`. Report this class; do not reclassify execution-file, which cannot see the credit-stop or leg-timeout rules. Set after the step files are restored, so the leg cannot set it. @@ -159,6 +161,18 @@ outputs: || (steps.isolation.outcome == 'success' && steps.install.outcome == 'failure' && 'install-failed') || (steps.run.outcome != 'success' && 'other') || '' }} + handoff: + description: >- + `true` when a key leg ended in `credit-stop`, or in `auth` with the + numeric API status 401, 402 or 403, so the caller may run the + subscription leg in a separate job; `false` otherwise; empty when the + step failed before the leg ran. A 403 hands off too: lane role keys + are whole-account Console keys with no per-key scope, so a 403 means + the key or its workspace is misconfigured; the run hands off so the + lane keeps running, and the caller's status job warns on every such + run until the owner replaces the key. lane-class keeps the reason. + Set after the step files are restored, so the leg cannot set it. + value: ${{ steps.run.outputs.handoff }} lane-resets: description: ISO 8601 time the blocking limit lifts, when the CLI's structured output gives it; else empty. value: ${{ steps.run.outputs.lane-resets }} diff --git a/.github/actions/run-claude-cli/lane.cjs b/.github/actions/run-claude-cli/lane.cjs index c8281a7b..4f3c85ea 100644 --- a/.github/actions/run-claude-cli/lane.cjs +++ b/.github/actions/run-claude-cli/lane.cjs @@ -15,8 +15,9 @@ // Security contract: // - One leg per invocation: readConfig fails closed unless exactly one of // the two credentials is set. A caller that falls back to the -// subscription after a key-leg credit stop runs a second job, on a fresh -// runner, gated on this one's lane-class output being `credit-stop`. +// subscription after a key leg hands off (a credit stop, or class auth +// with api_error_status 401, 402 or 403, NEXT_STEPS) runs a second job, +// on a fresh runner, gated on this one's handoff output being `true`. // - legEnv copies the step environment and removes every credential and // file-command VARIABLE, then sets the leg's one credential. That is a // denylist, not a sandbox: code the leg runs (a Bash tool call, a hook) @@ -130,18 +131,51 @@ const UNSAFE_VARIABLES = new Set([ "NODE_EXTRA_CA_CERTS", "NODE_TLS_REJECT_UNAUTHORIZED", ]); -// The job summary's closed token per class: the named stop and its recovery. -const SUMMARY_NOTES = { - "": "", - "credit-stop": "credit stop: add Console credit or raise the spend limit", - "rate-limit": "limit stop: rerun after the reset", - auth: "credential unusable: replace the secret", - overloaded: "API overloaded: rerun later", - "max-turns": "turn cap: raise max-turns or narrow the task", - timeout: "leg timeout: raise leg-timeout-minutes or narrow the task", - other: "see the step log", +// What each class means for the run. `handoff`: a key leg that ends in it +// hands the run to the caller's subscription job; `statuses`, when set, +// narrows that to these numeric api_error_status values, never an error-type +// string, which errors[] text could carry. `note`: the job summary's closed +// token, the named stop and what happens next. +const NEXT_STEPS = { + "": { handoff: false, note: "" }, + "credit-stop": { + handoff: true, + note: "credits used up: the subscription job runs; the owner rotates a key by hand when needed", + }, + // 401 (key disabled or deleted), 402 (billing) and 403 (permission) hand + // off; an auth class with no numeric status does not. + auth: { + handoff: true, + statuses: [401, 402, 403], + note: "credential unusable (rejected, unpaid or not permitted): the owner replaces the secret by hand", + }, + "rate-limit": { handoff: false, note: "limit stop: rerun after the reset" }, + overloaded: { handoff: false, note: "API overloaded: rerun later" }, + "max-turns": { + handoff: false, + note: "turn cap: raise max-turns or narrow the task", + }, + timeout: { + handoff: false, + note: "leg timeout: raise leg-timeout-minutes or narrow the task", + }, + other: { handoff: false, note: "see the step log" }, }; +function nextStep(failureClass) { + return Object.hasOwn(NEXT_STEPS, failureClass) + ? NEXT_STEPS[failureClass] + : NEXT_STEPS.other; +} + +// Only a key leg hands off: the subscription leg is the last one. +// `apiErrorStatus` is the last message's numeric api_error_status. +function handsOff(leg, failureClass, apiErrorStatus) { + const step = nextStep(failureClass); + if (leg.leg !== "api-key" || !step.handoff) return false; + return step.statuses === undefined || step.statuses.includes(apiErrorStatus); +} + class LaneError extends Error { constructor(message) { super(message); @@ -262,8 +296,7 @@ function readConfig(env) { } // `latest` asks the registry for the newest release; anything else must be an -// exact version (the action's cli-version input defaults to a pinned one). -// Either way the value passes the shared exact-version check before it is +// exact version. Either way the value passes the shared exact-version check before it is // installed. function resolveCliVersion(input, npmView) { const wanted = (input ?? "").trim() || "latest"; @@ -637,6 +670,7 @@ function runLeg( const modes = []; let record; let judged; + let handoff = false; try { const readOnly = readOnlyPaths( config, @@ -681,6 +715,13 @@ function runLeg( maxTurns: config.maxTurns, }); } + // The same message classify.cjs reads its status from. + const status = judged.messages.at(-1)?.api_error_status; + handoff = handsOff( + leg, + judged.failureClass, + Number.isInteger(status) ? status : null, + ); record = legRecord({ config, leg, @@ -690,8 +731,7 @@ function runLeg( messages: judged.messages, failureClass: judged.failureClass, resets: judged.resets, - // A credit stop hands the run to the caller's subscription job. - next: judged.failureClass === "credit-stop" ? "subscription" : null, + next: handoff ? "subscription" : null, message, }); } finally { @@ -706,6 +746,7 @@ function runLeg( records: [record], outputs: { "lane-class": judged.failureClass, + handoff: String(handoff), "lane-resets": judged.resets ?? "", "lane-turns": run.turns === null ? "" : String(run.turns), "lane-models": run.models.join(","), @@ -721,11 +762,9 @@ function summaryTable(records) { String(value ?? "") .replaceAll("|", "\\|") .replaceAll("`", "'"); - const note = (failureClass) => - SUMMARY_NOTES[failureClass] ?? SUMMARY_NOTES.other; const rows = records.map( (r) => - `| ${r.leg} | ${cell(r.secret)} | ${cell(r.class || "success")} | ${cell(r.num_turns)} | ${cell(r.denials.count)} | ${cell(r.resets)} | ${cell(r.next)} | ${cell(note(r.class))} |`, + `| ${r.leg} | ${cell(r.secret)} | ${cell(r.class || "success")} | ${cell(r.num_turns)} | ${cell(r.denials.count)} | ${cell(r.resets)} | ${cell(r.next)} | ${cell(nextStep(r.class).note)} |`, ); return [ "| Leg | Secret | Class | Turns | Denials | Resets | Next | Stop |", diff --git a/.github/actions/run-claude-cli/lane.test.cjs b/.github/actions/run-claude-cli/lane.test.cjs index 4e2e7a6d..3dc93f8e 100644 --- a/.github/actions/run-claude-cli/lane.test.cjs +++ b/.github/actions/run-claude-cli/lane.test.cjs @@ -308,6 +308,7 @@ test("a key-leg credit stop ends the run with lane-class credit-stop and runs no [["KEY", ["ANTHROPIC_API_KEY"]]], ); assert.equal(outputs["lane-class"], "credit-stop"); + assert.equal(outputs.handoff, "true"); assert.equal(outputs.credential, KEY_NAME); assert.deepEqual( records.map((r) => [r.leg, r.secret, r.class, r.next]), @@ -334,14 +335,89 @@ test("a credit-stop-shaped failure on the subscription leg is never credit-stop" assert.equal(outputs.credential, TOKEN_NAME); }); -test("an auth failure on the key leg ends the run with its class", () => { - const { outputs, calls } = runCase({ key: AUTH }); +test("a rejected key hands off to the subscription job and keeps class auth", () => { + const { outputs, calls, records } = runCase({ key: AUTH }); assert.deepEqual( calls.map((c) => c.leg), ["KEY"], ); assert.equal(outputs["lane-class"], "auth"); + assert.equal(outputs.handoff, "true"); assert.equal(outputs.credential, KEY_NAME); + assert.deepEqual( + records.map((r) => [r.class, r.next]), + [["auth", "subscription"]], + ); +}); + +// An auth leg with the given numeric status, or none, and errors[] entries. +// The 402 and 403 bodies follow the published error table +// (platform.claude.com/docs/en/api/errors): billing_error and +// permission_error. +function authLeg({ status, errors }) { + return [ + result({ + ...(errors ? { subtype: "error_during_execution", errors } : {}), + is_error: true, + num_turns: 0, + result: "API Error", + ...(status === undefined ? {} : { api_error_status: status }), + usage: { input_tokens: 0, output_tokens: 0 }, + }), + ]; +} + +test("a key refused with 401, 402 or 403 hands off and keeps class auth", () => { + for (const status of [401, 402, 403]) { + const { outputs, records } = runCase({ key: authLeg({ status }) }); + assert.equal(outputs["lane-class"], "auth", String(status)); + assert.equal(outputs.handoff, "true", String(status)); + assert.equal(records[0].next, "subscription", String(status)); + } +}); + +test("a 403 whose errors[] text names another type still hands off by its status", () => { + const { outputs } = runCase({ + key: authLeg({ + status: 403, + errors: ['{"type":"error","error":{"type":"rate_limit_error"}}'], + }), + }); + assert.equal(outputs["lane-class"], "auth"); + assert.equal(outputs.handoff, "true"); +}); + +test("permission_error in errors[] text alone, with no numeric status, does not hand off", () => { + const { outputs, records } = runCase({ + key: authLeg({ + errors: [ + 'model text: {"type":"error","error":{"type":"permission_error"}}', + ], + }), + }); + assert.equal(outputs["lane-class"], "auth"); + assert.equal(outputs.handoff, "false"); + assert.equal(records[0].next, null); +}); + +test("a rejected subscription token, the last leg, hands off nothing", () => { + const { outputs, records } = runCase({ sub: AUTH }); + assert.equal(outputs["lane-class"], "auth"); + assert.equal(outputs.handoff, "false"); + assert.equal(records[0].next, null); +}); + +test("a key leg hands off on no other failure class", () => { + for (const [name, key, extra] of [ + ["max-turns", MAX_TURNS, {}], + ["rate-limit", SESSION_LIMIT, {}], + ["timeout", SUCCESS, { keySleep: true, legTimeoutMs: 300 }], + ]) { + const { outputs, records } = runCase({ key, ...extra }); + assert.equal(outputs["lane-class"], name, name); + assert.equal(outputs.handoff, "false", name); + assert.equal(records[0].next, null, name); + } }); test("max-turns and timeout on the key leg end the run with their class", () => { @@ -365,6 +441,7 @@ test("a key-leg success writes the outputs from that leg", () => { ); assert.deepEqual(outputs, { "lane-class": "", + handoff: "false", "lane-resets": "", "lane-turns": "3", "lane-models": "claude-sonnet-5", @@ -767,7 +844,7 @@ test("a process the leg leaves behind cannot add a lane-class after the launcher test("stdout is captured in memory and a maxBuffer overflow is class other", () => { const seen = []; - const spawn = (command, args, options) => { + const spawn = (_command, _args, options) => { seen.push(options); const overflow = new Error("spawnSync ENOBUFS"); overflow.code = "ENOBUFS"; @@ -891,6 +968,20 @@ test("cli-version latest resolves from npm view; exact versions pass; others fai ); }); +test("the action's cli-version default resolves the newest release from npm view", () => { + const { parseWorkflow } = require("../../scripts/workflow-yaml.cjs"); + const action = parseWorkflow( + fs.readFileSync(path.join(__dirname, "action.yml"), "utf8"), + ); + let viewed = 0; + const version = resolveCliVersion(action.inputs["cli-version"].default, () => { + viewed += 1; + return "2.1.999\n"; + }); + assert.equal(version, "2.1.999"); + assert.equal(viewed, 1); +}); + function configEnv(overrides = {}) { const prompt = path.join(root, "config-prompt.md"); fs.writeFileSync(prompt, "Do the task.\n"); @@ -1136,9 +1227,35 @@ test("run on a key-leg credit stop sets lane-class credit-stop and starts no sub const summaryText = fs.readFileSync(env.GITHUB_STEP_SUMMARY, "utf8"); assert.match( summaryText, - /\| 1 \| AUTOMATIONS_CLAUDE_API_KEY \| credit-stop \| .* \| subscription \| credit stop: add Console credit or raise the spend limit \|/u, + /\| 1 \| AUTOMATIONS_CLAUDE_API_KEY \| credit-stop \| .* \| subscription \| credits used up: the subscription job runs; the owner rotates a key by hand when needed \|/u, ); assert.equal(summaryText.includes("Credit balance"), false); + assert.equal(readOutputs(env.GITHUB_OUTPUT).handoff, "true"); +}); + +test("run on a rejected key sets handoff and a summary row that names the dead key", () => { + const dir = path.join(root, "run-auth"); + const env = runEnv(dir, {}); + env.FAKE_KEY = legFile(dir, "key.jsonl", AUTH); + env.FAKE_KEY_EXIT = "1"; + assert.throws( + () => run(env), + /intake ended class=auth on AUTOMATIONS_CLAUDE_API_KEY/u, + ); + const outputs = readOutputs(env.GITHUB_OUTPUT); + assert.equal(outputs["lane-class"], "auth"); + assert.equal(outputs.handoff, "true"); + const summaryText = fs.readFileSync(env.GITHUB_STEP_SUMMARY, "utf8"); + assert.match( + summaryText, + /\| 1 \| AUTOMATIONS_CLAUDE_API_KEY \| auth \| .* \| subscription \| credential unusable \(rejected, unpaid or not permitted\): the owner replaces the secret by hand \|/u, + ); + const [record] = fs + .readFileSync(outputs["log-file"], "utf8") + .trim() + .split("\n") + .map((l) => JSON.parse(l)); + assert.deepEqual([record.class, record.next], ["auth", "subscription"]); }); test("run refuses both credentials before any leg starts", () => { @@ -1162,15 +1279,29 @@ test("the summary shows a closed token per class and never a record's message", next: null, message: "::error::planted CLI text", }); - const table = summaryTable( - ["", "auth", "timeout", "max-turns", "overloaded", "other", "unknown"].map( - record, - ), - ); + const classes = [ + "", + "credit-stop", + "auth", + "rate-limit", + "timeout", + "max-turns", + "overloaded", + "other", + "unknown", + "constructor", + ]; + const table = summaryTable(classes.map(record)); assert.equal(table.includes("planted"), false); assert.match(table, /\| success \| 0 \| 0 \| \| \| \|$/mu); - assert.match(table, /\| auth \| .* \| credential unusable: replace the secret \|/u); + assert.match( + table, + /\| auth \| .* \| credential unusable \(rejected, unpaid or not permitted\): the owner replaces the secret by hand \|/u, + ); assert.match(table, /\| unknown \| .* \| see the step log \|/u); + assert.match(table, /\| constructor \| .* \| see the step log \|/u); + // No note tells the owner to raise a spend limit: the owner runs none. + assert.doesNotMatch(table, /spend limit/iu); }); test("run refuses a loader, TLS or proxy variable before any leg", () => { @@ -1557,6 +1688,26 @@ test("install verifies both packages and the signed binary, then writes the outp assert.ok(out.includes(`cli-path=${cliPath}\n`)); }); +test("install with an exact cli-version installs that version and asks npm view nothing", () => { + const world = installWorld({ input: "2.1.305" }); + world.run(); + const npm = world.calls.filter((c) => c.command === "npm"); + assert.deepEqual( + npm.map((c) => c.args[0]), + ["--version", "install", "audit"], + ); + assert.equal(npm[1].args.at(-1), "@anthropic-ai/claude-code@2.1.305"); + assert.ok( + world.calls.some( + (c) => + c.command === "curl" && + c.args.at(-1) === + "https://downloads.claude.ai/claude-code-releases/2.1.305/manifest.json", + ), + ); + assert.match(fs.readFileSync(world.output, "utf8"), /^cli-version=2\.1\.305$/mu); +}); + // Each case fails the install step, so no output is written and the run step, // which runs the leg, never starts. for (const [name, options, pattern] of [ diff --git a/.github/scripts/intake-triage-legs.test.cjs b/.github/scripts/intake-triage-legs.test.cjs index b92c7ff0..3523a7ce 100644 --- a/.github/scripts/intake-triage-legs.test.cjs +++ b/.github/scripts/intake-triage-legs.test.cjs @@ -3,8 +3,9 @@ // The intake lane runs the API key and the subscription token in separate // jobs (ci-workflows#799). The claude job holds only the key; the // claude-subscription job holds only the token, runs on a fixed GitHub-hosted -// runner only after a key credit stop (or with no key), repeats the claude -// job's hardening, and takes nothing from it but two compared outputs. Both +// runner only after the key job hands off (a credit stop or a rejected key) +// or with no key, repeats the claude job's hardening, and takes nothing from +// it but compared outputs. Both // model jobs are read-only (ci-workflows#804): one apply job with no model // writes the triage of whichever leg triaged. The status job reports one // outcome: the subscription job's when it ran, red when the apply job failed. @@ -60,31 +61,47 @@ test("the subscription job runs on a fixed GitHub-hosted label, after the claude assert.equal(subscription.name, "claude-intake-triage-subscription"); }); -test("the subscription job starts only on an exact credit-stop or an empty key", () => { +test("the subscription job starts only on an exact handoff or an empty key", () => { const starts = (outputs) => Boolean(evaluate(subscription.if, { needs: { claude: { outputs } } })); + for (const laneClass of ["credit-stop", "auth"]) { + assert.equal( + starts({ + handoff: "true", + "lane-class": laneClass, + "credential-present": "true", + }), + true, + laneClass, + ); + } assert.equal( - starts({ "lane-class": "credit-stop", "credential-present": "true" }), - true, - ); - assert.equal( - starts({ "lane-class": "", "credential-present": "false" }), + starts({ handoff: "", "lane-class": "", "credential-present": "false" }), true, ); - for (const laneClass of [ - "", - "rate-limit", - "auth", - "other", - "credit-stop-x", - " credit-stop", - ]) { + // The handoff alone decides: a lane-class with no handoff starts nothing. + for (const laneClass of ["credit-stop", "auth", "rate-limit", "other", ""]) { assert.equal( - starts({ "lane-class": laneClass, "credential-present": "true" }), + starts({ + handoff: "false", + "lane-class": laneClass, + "credential-present": "true", + }), false, laneClass, ); } + for (const handoff of ["", "True", "true ", "1"]) { + assert.equal( + starts({ + handoff, + "lane-class": "auth", + "credential-present": "true", + }), + false, + JSON.stringify(handoff), + ); + } // A failed or cancelled claude job never starts it: the if has no status // function, so success() applies. assert.doesNotMatch(subscription.if, /always\(\)|failure\(\)|cancelled\(\)/u); @@ -105,6 +122,7 @@ test("the subscription job downloads nothing from the claude job and never write // The subscription job's outcome step names why it ran (ci-workflows#806). const FALLBACK_REASON = "${{ needs.claude.outputs.lane-class == 'credit-stop' && 'credit-stop' || " + + "needs.claude.outputs.lane-class == 'auth' && 'auth' || " + "needs.claude.outputs.credential-present == 'false' && 'no-key' || '' }}"; test("the subscription job repeats the claude job's steps but for the credential and the fallback reason", () => { @@ -148,7 +166,7 @@ test("the subscription job repeats the claude job's steps but for the credential ); }); -test("the subscription job's lane record names the credit stop or the empty key it ran for", () => { +test("the subscription job's lane record names the credit stop, the rejected key or the empty key it ran for", () => { const value = subscription.steps.find((step) => step.id === "review-outcome") .with["fallback-reason"]; const reason = (outputs) => @@ -163,34 +181,33 @@ test("the subscription job's lane record names the credit stop or the empty key ); assert.equal( reason({ "lane-class": "auth", "credential-present": "true" }), - "", + "auth", ); }); -test("a key credit stop skips the claude job's outcome and extract steps", () => { +test("a handoff skips the claude job's outcome and extract steps", () => { const byId = (id) => claude.steps.find((step) => step.id === id); const extract = byId("extract"); - const steps = (laneClass, present = "true") => ({ + const steps = (handoff, present = "true") => ({ compose: { outcome: "success" }, credential: { outputs: { present } }, - claude: { outputs: { "lane-class": laneClass } }, + claude: { outputs: { handoff } }, }); for (const step of [byId("review-outcome"), extract]) { assert.equal( - Boolean(evaluate(step.if, { steps: steps("credit-stop") })), + Boolean(evaluate(step.if, { steps: steps("true") })), false, step.name, ); - assert.equal( - Boolean(evaluate(step.if, { steps: steps("") })), - true, - step.name, - ); - assert.equal( - Boolean(evaluate(step.if, { steps: steps("auth") })), - true, - step.name, - ); + // No handoff, or a step that failed before the leg ran: the claude job + // reports. + for (const handoff of ["false", ""]) { + assert.equal( + Boolean(evaluate(step.if, { steps: steps(handoff) })), + true, + `${step.name} ${JSON.stringify(handoff)}`, + ); + } assert.equal( Boolean(evaluate(step.if, { steps: steps("", "false") })), false, @@ -284,6 +301,17 @@ test("an empty secret in the job that should triage fails with a fixed class", ( "triage-failed": "true", "failure-class": "rate-limit", }); + // A rejected key with no subscription token stays an auth failure. + const deadKeyNoToken = report({ + KEY_CLASS: "auth", + SUB_RESULT: "success", + SUB_PRESENT: "false", + }); + assert.equal(deadKeyNoToken.code, 1); + assert.deepEqual(deadKeyNoToken.outputs, { + "triage-failed": "true", + "failure-class": "auth", + }); // Both secrets empty. const neither = report({ KEY_PRESENT: "false", @@ -297,6 +325,31 @@ test("an empty secret in the job that should triage fails with a fixed class", ( }); }); +test("an unusable key warns the owner to replace the secret, even when the subscription job triaged", () => { + const warning = /^::warning[^:]*::class=auth: .*replace the secret/mu; + const fellBack = report({ + KEY_CLASS: "auth", + SUB_RESULT: "success", + SUB_PRESENT: "true", + SUB_FAILED: "false", + }); + assert.equal(fellBack.code, 0); + assert.match(fellBack.stdout, warning); + // An auth class with no numeric status does not hand off: the key job + // alone reports, red, and still warns. + const refused = report({ + KEY_RESULT: "success", + KEY_FAILED: "true", + KEY_CLASS: "auth", + }); + assert.equal(refused.code, 1); + assert.match(refused.stdout, warning); + for (const keyClass of ["", "credit-stop", "rate-limit", "other"]) { + const run = report({ KEY_CLASS: keyClass, KEY_FAILED: "false" }); + assert.doesNotMatch(run.stdout, /::warning/u, keyClass); + } +}); + test("the status job prints and writes only closed values", () => { const forged = report({ KEY_RESULT: "failure", diff --git a/.github/workflows/intake-triage.yml b/.github/workflows/intake-triage.yml index d79b7e9e..33ce1f43 100644 --- a/.github/workflows/intake-triage.yml +++ b/.github/workflows/intake-triage.yml @@ -43,14 +43,17 @@ name: intake-triage # without all three. It turns on the CLI's subprocess credential scrub # and puts exactly one Claude credential in the CLI environment. # - The API key runs in the claude job. The subscription token runs only in -# the claude-subscription job, and only when the claude job's lane-class -# output is exactly `credit-stop` or its key was empty. That job runs on -# a fresh ubuntu-24.04 GitHub-hosted runner, never the caller's runner -# input, repeats every hardening step, and takes nothing from the claude -# job but that output and the credential-present flag: no artifact, no -# cache. lane-class is set by run-claude-cli after it restores the step -# files, so the model cannot set it; a forged credit stop would cost only -# subscription usage. Either secret may be empty, not both. +# the claude-subscription job, and only when the claude job's handoff +# output is exactly `true` (the key's credits are used up, lane-class +# `credit-stop`, or the API refused the key with status 401, 402 or +# 403, lane-class `auth`) or its key was empty. That job runs on a +# fresh ubuntu-24.04 GitHub-hosted runner, never the caller's runner +# input, repeats every hardening step, and +# takes nothing from the claude job but those outputs and the +# credential-present flag: no artifact, no cache. handoff and lane-class +# are set by run-claude-cli after it restores the step files, so the +# model cannot set them; a forged handoff would cost only subscription +# usage. Either secret may be empty, not both. # - The model jobs cannot write: their token has contents: read and # issues: read, and `cache-mode: none` closes the Actions cache, which # a workflow_dispatch run could otherwise write. The run-claude-cli step's @@ -82,7 +85,7 @@ name: intake-triage # comment, needs_human) checked against the job's allowlist and enums. # - Only the claude-intake-triage-apply job writes, and it runs no model. # It takes the subscription job's outputs when that job ran, else the -# claude job's, so a credit stop applies once. It takes the issue from +# claude job's, so a handoff applies once. It takes the issue from # the event or the issue-number input, never from a job output, and # rebuilds the allowlist, Issue Types and work classes itself. It keeps # labels that are in the caller's allowlist AND exist in the repository @@ -126,8 +129,7 @@ name: intake-triage # # CLI VERSION: run-claude-cli installs the Claude Code CLI at run time. # `cli-version`, else the caller repo's CLAUDE_LANE_CLI_VERSION variable, -# else the pinned 2.1.304 (`npm view @anthropic-ai/claude-code version`, -# 2026-10-20), selects it: `latest` or an exact version. `stable` and +# else `latest`, selects it: `latest` or an exact version. `stable` and # `bundled`, which the claude-code-action lane took, run `latest` with a # warning. # @@ -138,9 +140,10 @@ name: intake-triage # legs' outputs in that run hold, so a rerun of it alone reapplies the same # triage, editing the lane's earlier comment. # -# `claude-intake-triage` triages on the API key; after a credit stop (or with -# no key) `claude-intake-triage-subscription` triages on the subscription -# token instead. `claude-intake-triage-apply` applies the triage of the job +# `claude-intake-triage` triages on the API key; after it hands off (a credit +# stop, or the key refused with API status 401, 402 or 403) or with no +# key, `claude-intake-triage-subscription` triages on the subscription token +# instead. `claude-intake-triage-apply` applies the triage of the job # that triaged, and `claude-intake-triage-status` reads all three and goes red # when the triage or its write failed. Never make that check required. # `claude-intake-triage-skipped` runs only when the author gate skipped the @@ -193,7 +196,7 @@ on: description: >- Claude Code CLI to run: latest or an exact major.minor.patch (stable and bundled run latest with a warning). Empty falls back to the - CLAUDE_LANE_CLI_VERSION variable, then the pinned 2.1.304. + CLAUDE_LANE_CLI_VERSION variable, then latest. type: string default: "" claude-args: @@ -333,6 +336,7 @@ jobs: cache-mode: none outputs: lane-class: ${{ steps.claude.outputs.lane-class }} + handoff: ${{ steps.claude.outputs.handoff }} credential-present: ${{ steps.credential.outputs.present }} triage-failed: ${{ steps.review-outcome.outputs.review-failed }} failure-class: ${{ steps.claude.outputs.lane-class || steps.review-outcome.outputs.failure-class }} @@ -519,7 +523,7 @@ jobs: ESCALATION_LABEL: ${{ inputs.escalation-label }} FLOOR_LABEL: ${{ inputs.floor-label }} BASE_ARGS: ${{ inputs.claude-args }} - CLI_VERSION: ${{ inputs.cli-version || vars.CLAUDE_LANE_CLI_VERSION || '2.1.304' }} + CLI_VERSION: ${{ inputs.cli-version || vars.CLAUDE_LANE_CLI_VERSION || 'latest' }} REPOSITORY: ${{ github.repository }} ISSUE_NUMBER: ${{ steps.issue.outputs.number }} ISSUE_FILE: ${{ steps.fetch.outputs.issue-file }} @@ -711,9 +715,9 @@ jobs: # continue-on-error keeps an infrastructure failure, which run-claude-cli # reports by failing this step, from ending the job before the outcome - # and extract steps run; the status job carries the red. A credit stop - # (key only) skips the outcome and extract steps: the subscription job - # triages instead. The outcome step's step-timeout-minutes repeats + # and extract steps run; the status job carries the red. A handoff (key + # only: a credit stop, or class auth with API status 401-403) skips + # the outcome and extract steps: the subscription job triages instead. The outcome step's step-timeout-minutes repeats # timeout-minutes. The step's env holds no GitHub token, and the tools # are Read, Grep and Glob only: no Bash, so no gh and no commands. - name: Claude intake triage @@ -749,7 +753,7 @@ jobs: - name: Report triage outcome id: review-outcome - if: steps.credential.outputs.present == 'true' && steps.claude.outputs.lane-class != 'credit-stop' + if: steps.credential.outputs.present == 'true' && steps.claude.outputs.handoff != 'true' uses: $/.github/actions/report-lane-outcome with: outcome: ${{ steps.claude.outcome }} @@ -768,7 +772,7 @@ jobs: # reason, turns and denied tool names go to this job's summary only. - name: Extract the triage id: extract - if: always() && steps.compose.outcome == 'success' && steps.credential.outputs.present == 'true' && steps.claude.outputs.lane-class != 'credit-stop' + if: always() && steps.compose.outcome == 'success' && steps.credential.outputs.present == 'true' && steps.claude.outputs.handoff != 'true' uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 env: EXECUTION_FILE: ${{ steps.claude.outputs.execution-file }} @@ -840,14 +844,14 @@ jobs: # The subscription leg, as its own job on a fresh VM: the claude job's # processes, files and environment do not reach it. Its steps repeat the # claude job's but for the credential (a test holds them equal), so the - # hardening runs again before the checkout. It reads two outputs of the + # hardening runs again before the checkout. It reads three outputs of the # claude job, each compared to a fixed string here and never written into # a run: script, and nothing else from that job. It does not use # inputs.runner: a caller's runner could keep state between jobs. subscription: name: claude-intake-triage-subscription needs: claude - if: needs.claude.outputs.lane-class == 'credit-stop' || needs.claude.outputs.credential-present == 'false' + if: needs.claude.outputs.handoff == 'true' || needs.claude.outputs.credential-present == 'false' runs-on: ubuntu-24.04 timeout-minutes: 16 # The model's job reads and never writes: the token can read the issue @@ -858,6 +862,7 @@ jobs: cache-mode: none outputs: lane-class: ${{ steps.claude.outputs.lane-class }} + handoff: ${{ steps.claude.outputs.handoff }} credential-present: ${{ steps.credential.outputs.present }} triage-failed: ${{ steps.review-outcome.outputs.review-failed }} failure-class: ${{ steps.claude.outputs.lane-class || steps.review-outcome.outputs.failure-class }} @@ -1044,7 +1049,7 @@ jobs: ESCALATION_LABEL: ${{ inputs.escalation-label }} FLOOR_LABEL: ${{ inputs.floor-label }} BASE_ARGS: ${{ inputs.claude-args }} - CLI_VERSION: ${{ inputs.cli-version || vars.CLAUDE_LANE_CLI_VERSION || '2.1.304' }} + CLI_VERSION: ${{ inputs.cli-version || vars.CLAUDE_LANE_CLI_VERSION || 'latest' }} REPOSITORY: ${{ github.repository }} ISSUE_NUMBER: ${{ steps.issue.outputs.number }} ISSUE_FILE: ${{ steps.fetch.outputs.issue-file }} @@ -1236,9 +1241,9 @@ jobs: # continue-on-error keeps an infrastructure failure, which run-claude-cli # reports by failing this step, from ending the job before the outcome - # and extract steps run; the status job carries the red. A credit stop - # (key only) skips the outcome and extract steps: the subscription job - # triages instead. The outcome step's step-timeout-minutes repeats + # and extract steps run; the status job carries the red. A handoff (key + # only: a credit stop, or class auth with API status 401-403) skips + # the outcome and extract steps: the subscription job triages instead. The outcome step's step-timeout-minutes repeats # timeout-minutes. The step's env holds no GitHub token, and the tools # are Read, Grep and Glob only: no Bash, so no gh and no commands. - name: Claude intake triage @@ -1274,7 +1279,7 @@ jobs: - name: Report triage outcome id: review-outcome - if: steps.credential.outputs.present == 'true' && steps.claude.outputs.lane-class != 'credit-stop' + if: steps.credential.outputs.present == 'true' && steps.claude.outputs.handoff != 'true' uses: $/.github/actions/report-lane-outcome with: outcome: ${{ steps.claude.outcome }} @@ -1286,7 +1291,7 @@ jobs: max-turns: ${{ steps.compose.outputs.max-turns }} known-failure-class: ${{ steps.claude.outputs.lane-class }} # Why this job ran, for the run's one lane record (ci-workflows#806). - fallback-reason: ${{ needs.claude.outputs.lane-class == 'credit-stop' && 'credit-stop' || needs.claude.outputs.credential-present == 'false' && 'no-key' || '' }} + fallback-reason: ${{ needs.claude.outputs.lane-class == 'credit-stop' && 'credit-stop' || needs.claude.outputs.lane-class == 'auth' && 'auth' || needs.claude.outputs.credential-present == 'false' && 'no-key' || '' }} # No model runs here. It reduces the run to the job outputs the apply # job reads: the result subtype, the error flag and the denial count, @@ -1295,7 +1300,7 @@ jobs: # reason, turns and denied tool names go to this job's summary only. - name: Extract the triage id: extract - if: always() && steps.compose.outcome == 'success' && steps.credential.outputs.present == 'true' && steps.claude.outputs.lane-class != 'credit-stop' + if: always() && steps.compose.outcome == 'success' && steps.credential.outputs.present == 'true' && steps.claude.outputs.handoff != 'true' uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 env: EXECUTION_FILE: ${{ steps.claude.outputs.execution-file }} @@ -1659,11 +1664,21 @@ jobs: else job=$KEY_RESULT present=$KEY_PRESENT failed=$KEY_FAILED class=$KEY_CLASS fi + # An unusable key stays visible even when the subscription job then + # triaged and the run is green. + if [ "$KEY_CLASS" = auth ]; then + echo "::warning title=claude-intake-triage key::class=auth: the API refused the key; the owner must replace the secret." + fi # A triage job with an empty secret did not triage: after a key credit - # stop that is a limit stop, otherwise both secrets were empty. + # stop that is a limit stop, after an unusable key it is that auth + # failure, otherwise both secrets were empty. if [ "$present" = false ]; then failed=true - if [ "$KEY_CLASS" = credit-stop ]; then class=rate-limit; else class=other; fi + case "$KEY_CLASS" in + credit-stop) class=rate-limit ;; + auth) class=auth ;; + *) class=other ;; + esac fi # A failed or cancelled write fails the record like a failed leg. if [ "$job" = success ]; then diff --git a/README.md b/README.md index 3f428e17..c6f703e3 100644 --- a/README.md +++ b/README.md @@ -603,13 +603,22 @@ consumer to audit it. so none of the three lanes double-report). - `.github/actions/run-claude-cli` — runs one prompt with headless `claude -p` for an agent-mode lane on a Linux runner, installing the Claude Code CLI - version `cli-version` names (an exact version, pinned by default, or - `latest`). Each invocation runs one leg on one + version `cli-version` names (`latest` by default, or an exact version). + Each invocation runs one leg on one credential: pass exactly one of `api-key` and `subscription-token`, or - the action fails closed. A key leg that ends in a credit stop - (report-lane-outcome's opt-in `credit-stop` class) sets `lane-class` to - `credit-stop`; any other failure sets its own class, so a broken key stays - visible instead of spending the subscription. A caller that falls back to + the action fails closed. A key leg hands off to the subscription, output + `handoff` set to `true`, when it ends in a credit stop (report-lane-outcome's + opt-in `credit-stop` class: the role's credits are used up) or with its key + refused by the API with numeric status 401 (disabled or deleted), 402 + (billing) or 403 (permission), class `auth`; an error-type string with no + numeric status hands off nothing. Lane role keys are whole-account Console + keys with no per-key scope, so a 403 means the key or its workspace is + misconfigured: the run hands off so the lane keeps running, and the status + job warns on every such run until the owner replaces the key. + `lane-class` keeps `credit-stop` or `auth`, so the job summary and the log + name the reason and a dead key stays visible. + Any other failure hands off nothing. Rotating a + key stays the owner's manual step. A caller that falls back to the subscription does so in a second job (the two-job pattern below); a caller that does not reports `credit-stop` as `rate-limit`. The leg's CLI environment has every credential and workflow file-command variable @@ -655,12 +664,12 @@ consumer to audit it. **Two-job pattern for the subscription fallback.** Two legs in one job would share the runner user, the filesystem and the environment, so the fallback runs on a new VM. Job A runs the key leg with `api-key` only - (`continue-on-error: true` on the step) and exposes `lane-class` as a job - output. Job B runs the subscription leg with `subscription-token` only. - Only job B references the OAuth secret, and it: + (`continue-on-error: true` on the step) and exposes `handoff` and + `lane-class` as job outputs. Job B runs the subscription leg with + `subscription-token` only. Only job B references the OAuth secret, and it: - declares `needs: A` and - `if: needs.A.outputs.lane-class == 'credit-stop'`, an exact compare, + `if: needs.A.outputs.handoff == 'true'`, an exact compare, and never writes job A's outputs into a `run:` script; - runs on a fixed GitHub-hosted label (`runs-on: ubuntu-24.04`), never a caller-supplied runner; @@ -669,11 +678,11 @@ consumer to audit it. - reads only trusted inputs: no artifact download from job A and no cache restore. - Job A skips its outcome step on a credit stop, so whichever job triaged + Job A skips its outcome step on a handoff, so whichever job triaged reports. Job B passes report-lane-outcome's `fallback-reason`, - `credit-stop` or `no-key` from job A's `lane-class` and key presence, so - its record says why it ran. A status job that needs both reads job B's - outcome when it ran, else job A's: one lane record per run. A lane that writes does it in a + `credit-stop`, `auth` or `no-key` from job A's `lane-class` and key + presence, so its record says why it ran, a dead key included. + A status job that needs both reads job B's outcome when it ran, else job A's: one lane record per run. A lane that writes does it in a separate job with no model that needs both and reads job B's outputs when it ran, else job A's, so the write happens once. `intake-triage.yml` is the reference implementation. @@ -1095,14 +1104,13 @@ GitHub continues the normal weekly patching of each hosted image generation. is missing, so the result does not depend on whether `intake-label-needs-triage` ran first. It runs through `.github/actions/run-claude-cli` (lane `intake`, funding `role` default - `automations`), which installs the Claude Code CLI at run time - (`cli-version`, else the `CLAUDE_LANE_CLI_VERSION` variable, else the - pinned version in the workflow). + `automations`), which installs the newest Claude Code CLI at run time + (`cli-version`, else the `CLAUDE_LANE_CLI_VERSION` variable, else `latest`). `claude-args` may set only `--model`, `--effort` and `--max-turns`. The caller grants `contents: read` and `issues: write` and passes the role's `CLAUDE_API_KEY` and `CLAUDE_CODE_OAUTH_TOKEN` (at least one). The key - runs in the `claude-intake-triage` job; after its credit stop, or with no - key, the token runs in the `claude-intake-triage-subscription` job on + runs in the `claude-intake-triage` job; after it hands off (a credit stop + or a rejected key), or with no key, the token runs in the `claude-intake-triage-subscription` job on `ubuntu-24.04` (the two-job pattern above); `claude-intake-triage-apply` writes whichever job triaged, and `claude-intake-triage-status` reports the one outcome. The workflow @@ -1268,8 +1276,8 @@ reached; re-run after it resets" (the `class=rate-limit` token is unchanged). Every outcome annotation, and a notice on a run that completed, also carries the run record as bare tokens, each omitted when unknown: `turns=`, `max-turns=` (the `max-turns` input), `credential=` (the -`credential` input), `fallback=credit-stop` or `fallback=no-key` (the -`fallback-reason` input, which rejects any other value), +`credential` input), `fallback=credit-stop`, `fallback=auth` or +`fallback=no-key` (the `fallback-reason` input, which rejects any other value), `models=` and, on `class=rate-limit`, `resets=` and `resets-window=` from the run's newest `rejected` `rate_limit_event` (none without one), the time also