/** * Tracks in-flight background tasks (run_in_background Bash/Agent) for a single * persistent Claude session, by observing the CLI's stream-json `system` events. * * Observed lifecycle (real shapes, see /tmp/bg-capture.mjs): * - launch: { type:'system', subtype:'task_started', task_id, task_type } * - work done: { type:'system', subtype:'task_updated', task_id, patch:{status:'completed'|'failed'|'killed'} } * - delivery: { type:'system', subtype:'task_notification', task_id, status:'completed', summary } * * task_notification is the event that actually re-invokes the model (the spontaneous * continuation turn). BUT — 2026-07-10 investigation — the CLI does NOT always emit it: * - CC versions ≤ 2026-07-05: a task completing while its owning turn was still active got * task_updated{completed} and NEVER a notification (11 verified cases; sessions lived * 20+ min after, incl. further turns, with zero notifications). Fixed in CC ≥ 07-06, * but the contract cannot be trusted across CLI auto-updates. * - TaskStop-killed tasks (patch.status 'killed') never get a notification at all. * * The tracker therefore keeps TWO sets: * - `running` — started, work not yet finished. Snapshot as pendingBackgroundTasks. * - `undelivered` — work finished (task_updated terminal status) but the notification has * not been observed. The CLI may deliver it up to ~24s later (observed gap with several * parallel tasks) — or never. Snapshot as undeliveredBackgroundTasks; orchestration arms * a grace watchdog (bg-wait-guard) for these instead of waiting forever. * Killed tasks are dropped from both sets immediately (no notification will ever come). */ export declare class BgTaskTracker { private readonly running; private readonly undelivered; /** Set when a background task completes; the spontaneous continuation turn that * follows is recognized via this flag. Cleared by disarmContinuation(). */ continuationArmed: boolean; /** Observe one parsed stream-json event. Idempotent per task_id. */ observe(data: any): void; /** Tasks whose work is still executing. */ get pendingCount(): number; /** Tasks whose work finished but whose task_notification has not been observed. */ get undeliveredCount(): number; /** True while anything may still produce a continuation — the session must stay alive. */ hasPending(): boolean; disarmContinuation(): void; } /** A `result` event produced by a background-task continuation turn carries * origin.kind === 'task-notification'. Used to distinguish a spontaneous * continuation result from a normal turn result. */ export declare function isContinuationResult(data: any): boolean; /** How a parsed stream-json line should be routed by the session's handleLine. */ export type LineRoute = 'normal' | 'open-continuation' | 'ignore'; /** * Decide how a parsed line should be routed given whether a turn is currently active. * Pure: depends only on the tracker's armed state and the event type. */ export declare function routeLine(tracker: BgTaskTracker, data: any, hasActiveTurn: boolean): LineRoute;