Skip to content

Commit 9d19eb5

Browse files
byapparovBulat Yapparovclaude
authored
fix(run): exit non-zero on session.error so CI wrappers see failures (#71)
The `aictrl run` command's session.error event handler logged the error and accumulated it into a local string but never set `process.exitCode`. On auth/provider failures, the subsequent `session.status: idle` broke the run loop into the success branch and the process exited with code 0 — while stdout printed "Authentication Failed" or similar. This silently broke CI workflows wrapping `aictrl run` (observed on aictrl-dev/aictrl's "AI Review" workflow on 2026-05-20): step conclusion `success`, no actual work, badge stays green. Fix: set `process.exitCode = 1` when the session.error event fires for the primary session. Sub-agent failures keep their existing behavior of flowing into the parent agent. Use `exitCode` (not `process.exit`) so the event loop drains naturally — session_complete still emits before the process terminates. Regression test (test/cli/run-session-error-exit.test.ts) statically asserts the handler block sets a non-zero exit code, mirroring the existing run-event-race.test.ts style. Closes #70 Co-authored-by: Bulat Yapparov <[email protected]> Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
1 parent ba8244f commit 9d19eb5

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

‎packages/cli/src/cli/cmd/run.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,11 @@ export const RunCommand = cmd({
566566
}
567567
if (props.sessionID === sessionID) {
568568
error = error ? error + EOL + err : err
569+
// #70: propagate primary-session failures (auth, provider, retries-exhausted)
570+
// to a non-zero exit code so CI wrappers see the failure instead of a
571+
// spuriously-green job. process.exitCode (not process.exit) lets the
572+
// loop drain to session.status idle and emit session_complete first.
573+
process.exitCode = 1
569574
}
570575
if (emit("error", { error: props.error, sourceSessionID: props.sessionID })) continue
571576
UI.error(err)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import path from "path"
2+
import { describe, expect, test } from "bun:test"
3+
4+
const RUN_SRC = path.resolve(import.meta.dir, "../../src/cli/cmd/run.ts")
5+
6+
// Regression lock for aictrl-dev/cli#70:
7+
// `aictrl run` was exiting 0 even when the provider rejected authentication.
8+
// Root cause: the run-command's `session.error` event handler logged the
9+
// error but did not set `process.exitCode`, so the subsequent
10+
// `session.status: idle` broke the loop into the success branch and the
11+
// process terminated with code 0 — masking broken CI workflows wrapping it.
12+
describe("run.ts session.error → non-zero exit (#70)", () => {
13+
test("session.error handler sets process.exitCode for the primary session", async () => {
14+
const source = await Bun.file(RUN_SRC).text()
15+
const handlerIdx = source.indexOf('if (event.type === "session.error")')
16+
expect(handlerIdx).toBeGreaterThan(-1)
17+
18+
// Bound the block to the next `if (event.type === ` so we only inspect
19+
// this handler — not whatever happens to follow it in source.
20+
const after = source.slice(handlerIdx + 1)
21+
const nextHandlerOffset = after.indexOf('if (event.type === "')
22+
const handlerBlock =
23+
nextHandlerOffset === -1 ? after : after.slice(0, nextHandlerOffset)
24+
25+
// The handler must mark the process as failed. Either form is acceptable:
26+
// - process.exitCode = N (preferred — lets loop drain cleanly)
27+
// - process.exit(N)
28+
const setsExitCode =
29+
/process\.exitCode\s*=\s*[1-9]/.test(handlerBlock) ||
30+
/process\.exit\s*\(\s*[1-9]/.test(handlerBlock)
31+
expect(setsExitCode).toBe(true)
32+
})
33+
34+
test("exit propagation is gated on the primary session, not subagents", async () => {
35+
// A subagent's session.error must NOT take the whole process down;
36+
// only the primary session's failure should propagate to the exit code.
37+
const source = await Bun.file(RUN_SRC).text()
38+
const handlerIdx = source.indexOf('if (event.type === "session.error")')
39+
const after = source.slice(handlerIdx + 1)
40+
const nextHandlerOffset = after.indexOf('if (event.type === "')
41+
const handlerBlock =
42+
nextHandlerOffset === -1 ? after : after.slice(0, nextHandlerOffset)
43+
44+
// The block must reference the primary `sessionID` check before exit propagation —
45+
// the existing handler already gates the local `error` string on
46+
// `props.sessionID === sessionID`, and the exit propagation must use the
47+
// same gate.
48+
expect(handlerBlock).toContain("props.sessionID === sessionID")
49+
})
50+
})

0 commit comments

Comments
 (0)