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
5 changes: 5 additions & 0 deletions .changeset/claude-code-nonzero-exit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@gemstack/framework': patch
---

Fix the Claude Code driver treating a non-zero agent exit as success when the agent had already streamed some text. A crash mid-build now fails the turn (surfacing stderr or the partial text) instead of resolving as a result the loop can score production-grade.
21 changes: 21 additions & 0 deletions packages/framework/src/driver/claude-code.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,27 @@ test('runClaude rejects on a non-zero exit with no result text', async () => {
)
})

test('runClaude rejects on a non-zero exit even when the agent streamed text', async () => {
const events: DriverEvent[] = []
const lines = [JSON.stringify({ type: 'assistant', message: { content: [{ type: 'text', text: 'started building' }] } })]
await assert.rejects(
() =>
runClaude({
bin: 'claude',
args: [],
cwd: '/ws',
env: {},
prompt: 'x',
spawn: fakeSpawn(lines, 1),
emit: e => events.push(e),
signals: [],
}),
/exited \(1\): started building/,
)
assert.equal(events.at(-1)!.type, 'error')
assert.ok(!events.some(e => e.type === 'result'))
})

test('ClaudeCodeDriver builds correct CLI args (permission mode, system, model)', async () => {
let captured: string[] = []
const spawn: SpawnLike = (_cmd, args) => {
Expand Down
9 changes: 6 additions & 3 deletions packages/framework/src/driver/claude-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,13 @@ export function runClaude(opts: RunClaudeOptions): Promise<DriverTurn> {

child.on('close', code => {
const turn = parser.result()
if (code !== 0 && !turn.text) {
const detail = stderrChunks.join('').trim() || `exit code ${code ?? 'null'}`
// A non-zero exit is a failed turn even when the agent streamed some text
// first: the loop gates on the outcome, so a crash mid-build must not pass
// as a result. Surface stderr, else the partial text, as context.
if (code !== 0) {
const detail = stderrChunks.join('').trim() || turn.text.trim() || `exit code ${code ?? 'null'}`
opts.emit({ type: 'error', message: detail })
finish(() => rejectPromise(new Error(`[framework] claude-code exited: ${detail}`)))
finish(() => rejectPromise(new Error(`[framework] claude-code exited (${code ?? 'null'}): ${detail}`)))
return
}
opts.emit({ type: 'result', text: turn.text, ...(turn.sessionId ? { sessionId: turn.sessionId } : {}) })
Expand Down
Loading