Implements fixes from issue #1880 and #1934 to prevent exit code 130 timeout in CI environments: - Add lastEventTimestamp to EventState for tracking when events were last received - Add event watchdog: if no events for 30s, verify session status via direct API call - Add secondary timeout: after 60s without meaningful work events, check for active children/todos and assume work is in progress This prevents the poll loop from waiting for full 600s timeout when: 1. Event stream drops silently (common in CI with network instability) 2. Main session delegates to children without producing meaningful work on main session
55 lines
1.5 KiB
TypeScript
55 lines
1.5 KiB
TypeScript
import pc from "picocolors"
|
|
import type { RunContext, EventPayload } from "./types"
|
|
import type { EventState } from "./event-state"
|
|
import { logEventVerbose } from "./event-formatting"
|
|
import {
|
|
handleSessionError,
|
|
handleSessionIdle,
|
|
handleSessionStatus,
|
|
handleMessagePartUpdated,
|
|
handleMessagePartDelta,
|
|
handleMessageUpdated,
|
|
handleToolExecute,
|
|
handleToolResult,
|
|
handleTuiToast,
|
|
} from "./event-handlers"
|
|
|
|
export async function processEvents(
|
|
ctx: RunContext,
|
|
stream: AsyncIterable<unknown>,
|
|
state: EventState
|
|
): Promise<void> {
|
|
for await (const event of stream) {
|
|
if (ctx.abortController.signal.aborted) break
|
|
|
|
try {
|
|
const payload = event as EventPayload
|
|
if (!payload?.type) {
|
|
if (ctx.verbose) {
|
|
console.error(pc.dim(`[event] no type: ${JSON.stringify(event)}`))
|
|
}
|
|
continue
|
|
}
|
|
|
|
if (ctx.verbose) {
|
|
logEventVerbose(ctx, payload)
|
|
}
|
|
|
|
// Update last event timestamp for watchdog detection
|
|
state.lastEventTimestamp = Date.now()
|
|
|
|
handleSessionError(ctx, payload, state)
|
|
handleSessionIdle(ctx, payload, state)
|
|
handleSessionStatus(ctx, payload, state)
|
|
handleMessagePartUpdated(ctx, payload, state)
|
|
handleMessagePartDelta(ctx, payload, state)
|
|
handleMessageUpdated(ctx, payload, state)
|
|
handleToolExecute(ctx, payload, state)
|
|
handleToolResult(ctx, payload, state)
|
|
handleTuiToast(ctx, payload, state)
|
|
} catch (err) {
|
|
console.error(pc.red(`[event error] ${err}`))
|
|
}
|
|
}
|
|
}
|