Part II · The Prompt, the Model, and the Engine
Chapter 07
The Heartbeat: query.ts and the Agentic Turn State Machine
Layer: Layer 2 (The Agent Engine)
Prerequisites: Chapter 01, Chapter 02, Chapter 03, Chapter 04, Chapter 05, Chapter 06
Primary Source Files:
query.ts—query()wrapper andqueryLoop()single-turn, multi-stepAsyncGeneratorstate machine (1,729 lines)query/config.ts—QueryConfigandbuildQueryConfig()immutable per-turn runtime gate snapshot (46 lines)query/deps.ts—QueryDepsandproductionDeps()narrow I/O dependency injection seam (40 lines)query/stopHooks.ts—handleStopHooks()end-of-turn hooks,CacheSafeParamssnapshot, and background forks (473 lines)query/tokenBudget.ts—BudgetTracker,createBudgetTracker(), andcheckTokenBudget()auto-continue guard (93 lines)query/transitions.ts(absent from this snapshot —ContinueandTerminalunions are reconstructed verbatim fromquery.tscall sites only)
Why This Layer Exists
Chapter 06 delivered the tool primitives: applyToolResultBudget(), StreamingToolExecutor, and runTools(). Earlier chapters built normalized messages (Chapter 03), cache-stable prompts (Chapter 04), and streaming model I/O with FallbackTriggeredError (Chapter 05).
query.ts and its four query/ companions (2,381 lines total) stitch those pieces into a single-turn, multi-step AsyncGenerator state machine. One prompt can span ten model calls, overlapped tools, mid-stream fallbacks, silent overflow or truncation recovery, and stop-hook or budget continuations—driving every REPL turn, SDK request, subagent, fork, background task, and agent hook.
1. The Wrapper, the Flat State Machine, and Absent query/transitions.ts
The Problem
An early agent loop starts as a recursive AsyncGenerator: call the model, run tools, yield* query(next). At scale, recursion retains O(turns) nested frames holding stale messagesForQuery arrays, complicates one-time setup, and scatters flag resets across closures.
The Mechanism: QueryParams, No-finally query(), and Whole-Object State
QueryParams (query.ts:181-199) defines 13 fields: 7 required (messages, systemPrompt, userContext, systemContext, canUseTool, toolUseContext, querySource) and 6 optional (fallbackModel, maxOutputTokensOverride, maxTurns, skipCacheWrite, taskBudget, deps). Notably, abortController lives on toolUseContext, and queued commands live in utils/messageQueueManager.ts:525-547.
Cross-iteration state lives in a 10-field State record (query.ts:204-217), wrapped by the public query() entrypoint (query.ts:219-239):
// query.ts:204-217
type State = {
messages: Message[]
toolUseContext: ToolUseContext
autoCompactTracking: AutoCompactTrackingState | undefined
maxOutputTokensRecoveryCount: number
hasAttemptedReactiveCompact: boolean
maxOutputTokensOverride: number | undefined
pendingToolUseSummary: Promise<ToolUseSummaryMessage | null> | undefined
stopHookActive: boolean | undefined
turnCount: number
// Why the previous iteration continued. Undefined on first iteration.
// Lets tests assert recovery paths fired without inspecting message contents.
transition: Continue | undefined
}
// query.ts:219-239
export async function* query(
params: QueryParams,
): AsyncGenerator<
| StreamEvent
| RequestStartEvent
| Message
| TombstoneMessage
| ToolUseSummaryMessage,
Terminal
> {
const consumedCommandUuids: string[] = []
const terminal = yield* queryLoop(params, consumedCommandUuids)
// Only reached if queryLoop returned normally. Skipped on throw (error
// propagates through yield*) and on .return() (Return completion closes
// both generators). This gives the same asymmetric started-without-completed
// signal as print.ts's drainCommandQueue when the turn fails.
for (const uuid of consumedCommandUuids) {
notifyCommandLifecycle(uuid, 'completed')
}
return terminal
}
At query.ts:229-238, there is deliberately no finally block. Mid-turn consumed commands push their UUIDs into consumedCommandUuids and emit notifyCommandLifecycle(cmd.uuid, 'started') (query.ts:1632-1643). Omitting finally fires 'completed' only on a normal Terminal return; throws or .return() closes preserve the asymmetric started-without-completed failure signal from cli/print.ts:2006-2008 and cli/print.ts:2248-2250.
Contrast that with using pendingMemoryPrefetch = startRelevantMemoryPrefetch(...) (query.ts:301-304): TC39 using invokes [Symbol.dispose]() (utils/attachments.ts:2410-2418) on every exit path (return, throw, .return()), aborting prefetches across all 12 return sites automatically.
queryLoop() (query.ts:241-1729) runs a flat while (true) loop (query.ts:307-1728) retaining recursion fossils (query.ts:1411, query.ts:1678, queryCheckpoint('query_recursive_call') at query.ts:1714). Each iteration destructures State (query.ts:311-321), reassigning only toolUseContext mid-iteration (query.ts:360, query.ts:546) and omitting transition.
Every continue writes a full State literal with no { ...state } spread (state = next at six sites; direct literal at query.ts:1321), forcing explicit reset choices on all 10 fields. Five bindings stay off State (query.ts:243, query.ts:280-304): consumedCommandUuids, budgetTracker, taskBudgetRemaining (kept loop-local at query.ts:289-291), config, and using pendingMemoryPrefetch.
Reconstructing Snapshot-Absent query/transitions.ts from Call Sites
query.ts:104 executes import type { Terminal, Continue } from './query/transitions.js', the sole code import in the repo. query/transitions.ts is absent from this snapshot. Every variant below is observed directly at query.ts's 7 transition writes and 12 return sites; any unused union members or helper guards in the absent file are unobservable here.
| # | Continue literal (query.ts lines) |
Trigger & key State updates |
|---|---|---|
| 1 | { reason: 'collapse_drain_retry', committed } (query.ts:1109-1112) |
feature('CONTEXT_COLLAPSE') PTL drain (committed > 0); turnCount unchanged |
| 2 | { reason: 'reactive_compact_retry' } (query.ts:1162) |
feature('REACTIVE_COMPACT') PTL/media compact; latches hasAttemptedReactiveCompact: true, resets autoCompactTracking |
| 3 | { reason: 'max_output_tokens_escalate' } (query.ts:1217) |
'tengu_otk_slot_v1' 8k-to-64k clean retry; sets maxOutputTokensOverride: ESCALATED_MAX_TOKENS |
| 4 | { reason: 'max_output_tokens_recovery', attempt } (query.ts:1245-1248) |
Truncation nudge (attempt 1..3); increments maxOutputTokensRecoveryCount, clears maxOutputTokensOverride |
| 5 | { reason: 'stop_hook_blocking' } (query.ts:1302) |
Stop/teammate hook blocking errors; sets stopHookActive: true, preserves hasAttemptedReactiveCompact |
| 6 | { reason: 'token_budget_continuation' } (query.ts:1338) |
feature('TOKEN_BUDGET') under 90%; resets hasAttemptedReactiveCompact: false and stopHookActive: undefined |
| 7 | { reason: 'next_turn' } (query.ts:1725) |
Tools done; sole site incrementing turnCount (turnCount + 1), preserves stopHookActive |
All 10 Terminal variants appear across 12 return sites in queryLoop() (line 1175 uses a ternary across two variants, while 'image_error', 'prompt_too_long', and 'completed' each have two sites):
Variant (Terminal.reason) |
Return line(s) in query.ts |
Exit condition & payload |
|---|---|---|
'blocking_limit' |
query.ts:646 |
Pre-API hard token limit (skipped when compact/collapse/reactive owns recovery) |
'image_error' |
query.ts:977, query.ts:1175 |
ImageSizeError/ImageResizeError throw, or unrecovered withheld media error (query.ts:1175 ternary) |
'model_error' |
query.ts:996 |
Uncaught callModel throw (carries error); pairs missing tool_results first |
'aborted_streaming' |
query.ts:1051 |
Signal aborted after stream; drains executor or emits cancelled tool_results |
'prompt_too_long' |
query.ts:1175, query.ts:1182 |
Unrecovered withheld 413 after reactiveCompact (query.ts:1175 ternary) or CONTEXT_COLLAPSE-only fallback |
'completed' |
query.ts:1264, query.ts:1357 |
Unrecovered API error bailout (query.ts:1264) or normal turn end (query.ts:1357) |
'stop_hook_prevented' |
query.ts:1279 |
handleStopHooks() returned preventContinuation: true (or aborted mid-hook) |
'aborted_tools' |
query.ts:1515 |
Signal aborted after tool drain; mirrors maxTurns attachment check |
'hook_stopped' |
query.ts:1520 |
Tool update yielded hook_stopped_continuation (query.ts:1388-1393) |
'max_turns' |
query.ts:1711 |
nextTurnCount > maxTurns after tools (carries turnCount: nextTurnCount) |
Hidden Invariants
- Only
next_turnincrementsturnCount: Starting at1(query.ts:276), recoveries, stop hooks, and budget nudges never consumemaxTurns; only tool rollovers incrementturnCount(query.ts:1679,query.ts:1719). - One runtime reader of
state.transition: EveryContinuelogs provenance for tests, yetstate.transition?.reason !== 'collapse_drain_retry'(query.ts:1092) is the sole runtime branch reading it. Terminalis an exhaustiveness & test contract: All six production callers usefor await, discardingTReturn.Terminalenforces exhaustive exits inqueryLoop(), supports.next()assertions, and scaffoldsstep()extraction (query/config.ts:8-11).
2. Decoupling I/O and Runtime Gates: QueryDeps, QueryConfig, and Bun DCE
The Problem
Unit-testing a 1,700-line loop that imports model streaming, compaction, and global flags directly forced 6–8 test files to duplicate per-module spyOn boilerplate (query/deps.ts:8-11). Yet bundling every gate into a config struct breaks Bun's dead-code elimination (DCE) and risks dragging heavy transitive graphs into isolated test shards.
The Mechanism: Narrow QueryDeps and DCE-Safe QueryConfig
query/deps.ts (query/deps.ts:21-31) exposes four narrow I/O seams, paired with productionDeps() (query/deps.ts:33-40) and wired via params.deps ?? productionDeps() (query.ts:263):
// query/deps.ts:21-31
export type QueryDeps = {
// -- model
callModel: typeof queryModelWithStreaming
// -- compaction
microcompact: typeof microcompactMessages
autocompact: typeof autoCompactIfNeeded
// -- platform
uuid: () => string
}
Typing each field with typeof fn keeps fakes synchronized with production signatures automatically (query/deps.ts:13-17) with zero added module-graph cost for tests already importing query.ts. Paired with State.transition, tests drive deterministic callModel/autocompact sequences without ES-module mocking. The four seams plug in at query.ts:353, query.ts:414-418, query.ts:454-467, query.ts:523, and query.ts:659-708.
query/config.ts (query/config.ts:13-46) snapshots sessionId and four runtime gates once at queryLoop() entry (query.ts:295):
// query/config.ts:13-46
// Intentionally excludes feature() gates — those are tree-shaking boundaries
// and must stay inline at the guarded blocks for dead-code elimination.
export type QueryConfig = {
sessionId: SessionId
// Runtime gates (env/statsig). NOT feature() gates — see above.
gates: {
// Statsig — CACHED_MAY_BE_STALE already admits staleness, so snapshotting
// once per query() call stays within the existing contract.
streamingToolExecution: boolean
emitToolUseSummaries: boolean
isAnt: boolean
fastModeEnabled: boolean
}
}
export function buildQueryConfig(): QueryConfig {
return {
sessionId: getSessionId(),
gates: {
streamingToolExecution: checkStatsigFeatureGate_CACHED_MAY_BE_STALE(
'tengu_streaming_tool_execution2',
),
emitToolUseSummaries: isEnvTruthy(
process.env.CLAUDE_CODE_EMIT_TOOL_USE_SUMMARIES,
),
isAnt: process.env.USER_TYPE === 'ant',
// Inlined from fastMode.ts to avoid pulling its heavy module graph
// (axios, settings, auth, model, oauth, config) into test shards that
// didn't previously load it — changes init order and breaks unrelated tests.
fastModeEnabled: !isEnvTruthy(process.env.CLAUDE_CODE_DISABLE_FAST_MODE),
},
}
}
Production Scars & Snapshot Absences
Two hard-won scars live inside query/config.ts:
- Bun DCE exclusion (
query/config.ts:13-14,query.ts:796-798): Per Chapter 00, Bun only tree-shakesfeature('...')branches whenfeature()sits directly inside anifor ternary condition. Movingfeature()intoQueryConfigwould retain internal code in external builds. Note also thatquery.ts:588readsconfig.gates.isAnt, whilequery.ts:927still checksprocess.env.USER_TYPE === 'ant'inline beforestripSignatureBlocks(). - Inlining
fastModeEnabled(query/config.ts:40-43):utils/fastMode.ts:38-40exports the identical one-lineisFastModeEnabled(). Importingutils/fastMode.ts(utils/fastMode.ts:1-36) pullsaxios,oauth.js,auth.js,config.js,model.js, andsettings.js, shifting init order and breaking unrelated test shards—soquery/config.tsinlines the check directly.
Snapshot Absences Across Chapter 07's Five Files: Exactly 9 imported modules are absent (none in
config.ts,deps.ts, ortokenBudget.ts):
- 3 unconditional
import typeabsences:query/transitions.js(query.ts:104),types/message.js(query.ts:30-39,query/stopHooks.ts:10-18),constants/querySource.js(query.ts:94,query/stopHooks.ts:51).- 6
feature()-gatedrequire()absences:services/compact/reactiveCompact.js(REACTIVE_COMPACT,query.ts:15-17),services/contextCollapse/index.js(CONTEXT_COLLAPSE,query.ts:18-20),services/skillSearch/prefetch.js(EXPERIMENTAL_SKILL_SEARCH,query.ts:66-68),jobs/classifier.js(TEMPLATES, live atquery/stopHooks.ts:45-47; unusedjobClassifierleftover atquery.ts:69-71),services/compact/snipCompact.js(HISTORY_SNIP,query.ts:115-117),utils/taskSummary.js(BG_SESSIONS,query.ts:118-120).- 2
feature()-gated modules ARE present on disk:utils/computerUse/cleanup.ts(CHICAGO_MCP) andservices/extractMemories/extractMemories.ts(EXTRACT_MEMORIES).
3. Streaming Dispatch, needsFollowUp, Dual Fallbacks, and Turn Rollover
The Problem
Streaming into live tools poses four hazards: stop_reason === 'tool_use' is unreliable, SDK field backfills must not mutate prompt-cache bytes, streaming and model fallbacks clean up orphans differently, and queued commands cannot interleave with tool_result blocks.
The Mechanism: needsFollowUp, Executor Bridging, and Dual Fallbacks
Before streaming (query.ts:551-568), queryLoop() defines needsFollowUp and instantiates StreamingToolExecutor behind config.gates.streamingToolExecution:
// query.ts:551-568
const assistantMessages: AssistantMessage[] = []
const toolResults: (UserMessage | AttachmentMessage)[] = []
// @see https://docs.claude.com/en/docs/build-with-claude/tool-use
// Note: stop_reason === 'tool_use' is unreliable -- it's not always set correctly.
// Set during streaming whenever a tool_use block arrives — the sole
// loop-exit signal. If false after streaming, we're done (modulo stop-hook retry).
const toolUseBlocks: ToolUseBlock[] = []
let needsFollowUp = false
queryCheckpoint('query_setup_start')
const useStreamingToolExecution = config.gates.streamingToolExecution
let streamingToolExecutor = useStreamingToolExecution
? new StreamingToolExecutor(
toolUseContext.options.tools,
canUseTool,
toolUseContext,
)
: null
Three production failure modes disqualify stop_reason === 'tool_use':
- Per-block emission with late
message_deltamutation (services/api/claude.ts:2229-2248):queryModelWithStreaming()yields eachAssistantMessageatcontent_block_stopwithstop_reason: null, mutating onlynewMessages.at(-1).message.stop_reasonwhenmessage_deltaarrives. A mid-stream drop leavesstop_reasonnulldespite an emittedtool_use. - Mixed
tool_use+max_tokenstruncation (services/api/claude.ts:2266-2292): If a completedtool_useblock precedes amax_tokenscutoff,claude.tsappends a syntheticapiError: 'max_output_tokens'message. BecauseneedsFollowUpflippedtrueon thetool_useblock (query.ts:829-835),queryLoop()runs the tool to keep Chapter 03's pairing intact whilenormalizeMessagesForAPI()dropsisSyntheticApiErrorMessage(utils/messages.ts:2069) next turn. - 3P provider drift: Bedrock, Vertex, Foundry, and gateways occasionally emit
end_turnor omitstop_reasonalongsidetool_useblocks. DerivingneedsFollowUpstrictly from observedcontent.type === 'tool_use'blocks locks loop continuation to the exact set ofToolUseBlocks needing pairedtool_results.
Inside the streaming loop (query.ts:659-863), three seams connect Ch 05 to Ch 06:
- Yield-isolated
backfillObservableInput(query.ts:742-787):queryLoop()runstool.backfillObservableInput(inputCopy)on a shallow copy and clonesyieldMessageonly when backfill adds new keys (query.ts:771-773). In-place overwrites (e.g.,~path expansion) are skipped to keep transcript hashes stable, and unmodifiedmessageis always pushed intoassistantMessages(query.ts:827) to preserve prompt-cache bytes. StreamingToolExecutorvs.runTools()(query.ts:837-862,query.ts:1366-1408): Mid-stream,queryLoop()enqueues blocks viaaddTool()and drainsgetCompletedResults()intotoolResults(skippingresult.newContext). On abort (query.ts:1016-1029), it drainsgetRemainingResults()or runsyieldMissingToolResultBlocks(). Normally,query.ts:1380-1408drainsgetRemainingResults()orrunTools(), flagshook_stopped_continuation, and mergesupdate.newContext;getUpdatedContext()is never called.- Dual fallbacks inside
while (attemptWithFallback)(query.ts:654-954):
| Mechanism | Streaming-to-non-streaming (query.ts:678-741) |
Model fallback (query.ts:894-953) |
|---|---|---|
| Trigger | onStreamingFallback() (query.ts:678-680) sets streamingFallbackOccured = true |
FallbackTriggeredError caught if fallbackModel is set (query.ts:894; else rethrows to model_error) |
| Orphan cleanup | Yields { type: 'tombstone', message } (query.ts:716-718) to drop broken thinking signatures |
Calls yield* yieldMissingToolResultBlocks(assistantMessages, 'Model fallback triggered') (query.ts:900-903) |
| Reset & retry | Clears arrays/needsFollowUp, calls discard() + fresh executor (query.ts:725-740), stays in stream |
Same reset (query.ts:904-919), swaps currentModel & mainLoopModel (query.ts:896, query.ts:922), USER_TYPE === 'ant' stripSignatureBlocks() (query.ts:927-929), yields 'warning', continues inner loop (query.ts:950) |
Post-Tool Housekeeping, Exit Guards, and Turn Rollover (query.ts:1411-1728)
After tools finish (query.ts:1409), queryLoop() runs an exact four-part housekeeping pass:
- Latency-hidden summaries (
query.ts:1412-1482,query.ts:1055-1060): Whenconfig.gates.emitToolUseSummariesis true,!aborted, and!agentId, fires HaikugenerateToolUseSummary()withoutawaiting intonextPendingToolUseSummary, awaiting and yielding it after the next iteration's 5–30s stream. - Abort & hook-stop guards (
query.ts:1485-1521): Likeaborted_streaming(query.ts:1033-1051),aborted_toolsrunsfeature('CHICAGO_MCP') && !agentIdcleanup (query.ts:1489-1498) and skips interruption text whensignal.reason === 'interrupt'; unlikeaborted_streaming(toolUse: false),aborted_toolsyieldscreateUserInterruptionMessage({ toolUse: true }), mirrors themaxTurnsattachment check (query.ts:1507-1514), and returns{ reason: 'aborted_tools' }.- Next,
shouldPreventContinuationreturns{ reason: 'hook_stopped' }(query.ts:1519-1521).
- Next,
- Post-tool queue & attachment drain (
query.ts:1535-1643): Delayed after tools so user messages never interleave withtool_results (query.ts:1535-1536).sleepRan(SLEEP_TOOL_NAME,query.ts:1566) bumps max priority to'later'(utils/messageQueueManager.ts:525-532);isSlashCommand(utils/messageQueueManager.ts:541-547) skips slash commands; main threads drainagentId === undefinedwhile subagents drain matchingtask-notifications (query.ts:1567-1578). Consumed commands then log'started'(query.ts:1632-1643). - Tool refresh & 1-indexed
maxTurns(query.ts:1660-1727):refreshTools()exposes newly connected MCP servers mid-turn;feature('BG_SESSIONS')with!toolUseContext.agentIdrunsmaybeGenerateTaskSummary()(query.ts:1685-1702,utils/taskSummary.jsabsent); thennextTurnCount = turnCount + 1 > maxTurns(query.ts:1679andquery.ts:1705-1712) exits{ reason: 'max_turns', turnCount: nextTurnCount }(somaxTurns: 1allows one model+tool step) or writestransition: { reason: 'next_turn' }.
4. Withheld Error Recovery: Suppress, Recover, Surface Once
The Problem
queryModelWithStreaming() reports prompt-too-long (services/api/errors.ts:62-77), media-size rejections, and max_tokens cutoffs (services/api/claude.ts:2266-2292) by yielding synthetic AssistantMessages with isApiErrorMessage: true. SDK clients like Cowork and Desktop tear down the session on any yielded error (query.ts:167-172), so forwarding recoverable errors mid-stream disconnects the consumer while recovery retries into a dead pipe.
The Mechanism: Pre-API Skip, Streaming Withholding, and the 4-Stage Ladder
Pre-API (query.ts:612-648), calculateTokenWarningState(...).isAtBlockingLimit normally returns { reason: 'blocking_limit' } (query.ts:646). queryLoop() skips that preempt when compaction just ran, querySource is 'compact' or 'session_memory', or reactiveCompact or contextCollapse owns real-API 413 recovery while isAutoCompactEnabled() (services/compact/autoCompact.ts:147-158) is true—honoring DISABLE_AUTO_COMPACT.
Immediately above (query.ts:621-627), queryLoop() hoists const mediaRecoveryEnabled = reactiveCompact?.isReactiveCompactEnabled() ?? false once per turn. Because CACHED_MAY_BE_STALE can flip during a 5–30s stream, unhoisted reads could withhold a media error mid-stream and then skip post-stream recovery, silently dropping the error.
During streaming (query.ts:796-827), four predicates suppress yield yieldMessage while retaining message in assistantMessages:
// query.ts:796-827
// feature() only works in if/ternary conditions (bun:bundle
// tree-shaking constraint), so the collapse check is nested
// rather than composed.
let withheld = false
if (feature('CONTEXT_COLLAPSE')) {
if (
contextCollapse?.isWithheldPromptTooLong(
message,
isPromptTooLongMessage,
querySource,
)
) {
withheld = true
}
}
if (reactiveCompact?.isWithheldPromptTooLong(message)) {
withheld = true
}
if (
mediaRecoveryEnabled &&
reactiveCompact?.isWithheldMediaSizeError(message)
) {
withheld = true
}
if (isWithheldMaxOutputTokens(message)) {
withheld = true
}
if (!withheld) {
yield yieldMessage
}
if (message.type === 'assistant') {
assistantMessages.push(message)
Post-stream inside if (!needsFollowUp) (query.ts:1062), queryLoop() checks lastMessage = assistantMessages.at(-1) (query.ts:1063) against a strict 4-stage recovery ladder (query.ts:1070-1265):
collapse_drain_retry(query.ts:1085-1118): Behindfeature('CONTEXT_COLLAPSE')(services/contextCollapse/index.jsabsent), handlesisWithheld413only (media skips collapse because collapse does not strip images,query.ts:1075-1076). Guarded one-shot bystate.transition?.reason !== 'collapse_drain_retry'(query.ts:1092); retries whendrained.committed > 0.reactive_compact_retry(query.ts:1119-1183): Behindfeature('REACTIVE_COMPACT')(services/compact/reactiveCompact.jsabsent), handles PTL and media errors usinghasAttemptedReactiveCompactas a one-shot guard. On success, decrementstaskBudgetRemaining(query.ts:1138-1146), yieldspostCompactMessages, setshasAttemptedReactiveCompact: true, clearsautoCompactTracking, and continues.max_output_tokens_escalate(query.ts:1188-1221): Fires onisWithheldMaxOutputTokens(lastMessage)(query.ts:175-179) when GrowthBook'tengu_otk_slot_v1'(defaultfalse,query.ts:1197,services/api/claude.ts:3396) is on,maxOutputTokensOverride === undefined, andCLAUDE_CODE_MAX_OUTPUT_TOKENSis unset;services/api/claude.ts:3394-3419caps defaults toCAPPED_DEFAULT_MAX_TOKENS = 8_000(utils/context.ts:24-25, p99 4,911), retryingmessages: messagesForQueryonce cleanly atESCALATED_MAX_TOKENS = 64_000.max_output_tokens_recovery(query.ts:1223-1256): Runs up toMAX_OUTPUT_TOKENS_RECOVERY_LIMIT = 3(query.ts:164) continuations, appending...assistantMessagesplus an unyieldedisMeta: trueresume nudge and clearingmaxOutputTokensOverride.services/api/claude.ts:2279-2292reusesapiError: 'max_output_tokens'formodel_context_window_exceeded, and Chapter 03'snormalizeMessagesForAPI()stripsisSyntheticApiErrorMessage(utils/messages.ts:2069) next call.
Three Recovery Invariants:
- Exactly-once surfacing: Withheld errors are either absorbed silently or yielded once on failure (
query.ts:1173,query.ts:1180, andquery.ts:1255).- No stop-hook death spirals: Every unrecovered API error fires
void executeStopFailureHooks(lastMessage, toolUseContext)and exits beforehandleStopHooks()(query.ts:1168-1183andquery.ts:1258-1265), preventing anerror -> hook blocking -> retry -> errorloop.- Asymmetric terminals: Unrecovered PTL/media exit
{ reason: 'prompt_too_long' }or{ reason: 'image_error' }(query.ts:1175,query.ts:1182), whereas exhaustedmax_output_tokensfalls through toif (lastMessage?.isApiErrorMessage)and returns{ reason: 'completed' }(query.ts:1262-1265).
5. End-of-Turn Orchestration: handleStopHooks and checkTokenBudget
The Problem
When needsFollowUp is false with no API error, three systems still run at turn end: background forks need a byte-identical cache snapshot, stop hooks may block or halt completion, and token budgets must nudge progress without looping when output stalls.
The Mechanism: Four-Phase handleStopHooks() (query/stopHooks.ts)
Invoked at query.ts:1267-1276, query/stopHooks.ts (query/stopHooks.ts:65-473) builds stopHookContext (query/stopHooks.ts:84-91) and runs four ordered phases:
CacheSafeParamssnapshot (query/stopHooks.ts:92-98): Forrepl_main_threadorsdk(never subagents), savescreateCacheSafeParams(stopHookContext)viasaveCacheSafeParams()(utils/forkedAgent.ts:57-81). Kept outside!isBareMode()andCLAUDE_CODE_ENABLE_PROMPT_SUGGESTIONso/btwand SDKside_questioninherit Chapter 04's prompt prefix and Chapter 06'scloneContentReplacementState()(utils/forkedAgent.ts:399-403).TEMPLATESjob classifier (query/stopHooks.ts:108-132): Underfeature('TEMPLATES') && process.env.CLAUDE_JOB_DIR && querySource.startsWith('repl_main_thread') && !agentId, runsclassifyAndWriteState()(jobs/classifier.js, absent) over all assistant messages instopHookContext.messages, raced against a 60ssetTimeout(...).unref()timer sostate.jsonflushes without blocking exit.!isBareMode()background fan-out (query/stopHooks.ts:133-157): When neitherCLAUDE_CODE_SIMPLEnor--bareis set (utils/envUtils.ts:60-65), fires three background forks:executePromptSuggestion()(!isEnvDefinedFalsy(process.env.CLAUDE_CODE_ENABLE_PROMPT_SUGGESTION)),executeExtractMemories()(feature('EXTRACT_MEMORIES') && !agentId && isExtractModeActive(), checking GrowthBook'tengu_passport_quail'and'tengu_slate_thimble'atmemdir/paths.ts:69-77), andexecuteAutoDream()(!agentId).- Session Memory runs elsewhere:
extractSessionMemoryregisters viaregisterPostSamplingHook()(services/SessionMemory/sessionMemory.ts:374) and runs per sampling step inquery.ts:1000-1009(executePostSamplingHooks()), not insidequery/stopHooks.ts. - Computer Use cleanup (
query/stopHooks.ts:164-173): Outside!isBareMode(),feature('CHICAGO_MCP') && !agentIdawaitscleanupComputerUseAfterTurn()(utils/computerUse/cleanup.ts), releasing the process-wide lock on main-thread exit only.
- Session Memory runs elsewhere:
- Stop, SubagentStop, and Teammate hooks (
query/stopHooks.ts:175-472): CallsexecuteStopHooks()(utils/hooks.ts:3639-3685, switching to'SubagentStop'whenagentIdis set and passingstop_hook_active: stopHookActive ?? false). Blocking errors become yieldedisMeta: trueuser messages;preventContinuationyieldshook_stopped_continuationand beatsblockingErrors(query/stopHooks.ts:325-332); mid-hook aborts returnpreventContinuation: true(query/stopHooks.ts:283-294).- Teammate pass & fail-open catch: If Stop hooks pass and
isTeammate()is true,TaskCompleted(for ownedin_progresstasks) andTeammateIdlerun sequentially (query/stopHooks.ts:335-453), sharing one accumulator with the same precedence. Unexpected throws logtengu_stop_hook_error, yield a'warning'system message, and fail open (query/stopHooks.ts:456-472).
- Teammate pass & fail-open catch: If Stop hooks pass and
Back in query.ts:1278-1306, preventContinuation returns { reason: 'stop_hook_prevented' }, while blockingErrors transitions via { reason: 'stop_hook_blocking' } with stopHookActive: true (query.ts:1300). That flag is preserved across next_turn (query.ts:1724) so hooks see stop_hook_active: true on re-entry and can self-terminate, since no hardcoded retry cap exists.
The
hasAttemptedReactiveCompactDeath-Spiral Scar (query.ts:1292-1297):stop_hook_blockingintentionally preserveshasAttemptedReactiveCompactinstead of resetting it tofalse. Resetting it previously caused an infinite loop (compact -> still too long -> error -> stop hook blocking -> compact -> ...) burning thousands of API calls.
The Mechanism: checkTokenBudget() vs. API taskBudget (query/tokenBudget.ts)
After handleStopHooks() passes cleanly, query.ts:1308-1355 runs checkTokenBudget() behind feature('TOKEN_BUDGET') (budgetTracker initialized at query.ts:280). Turn target and output deltas come from bootstrap/state.ts:724-743 (getCurrentTurnTokenBudget(), getTurnOutputTokens()), populated from user shorthands like +500k (utils/tokenBudget.ts:21-29).
// query/tokenBudget.ts:45-76
export function checkTokenBudget(
tracker: BudgetTracker,
agentId: string | undefined,
budget: number | null,
globalTurnTokens: number,
): TokenBudgetDecision {
if (agentId || budget === null || budget <= 0) {
return { action: 'stop', completionEvent: null }
}
const turnTokens = globalTurnTokens
const pct = Math.round((turnTokens / budget) * 100)
const deltaSinceLastCheck = globalTurnTokens - tracker.lastGlobalTurnTokens
const isDiminishing =
tracker.continuationCount >= 3 &&
deltaSinceLastCheck < DIMINISHING_THRESHOLD &&
tracker.lastDeltaTokens < DIMINISHING_THRESHOLD
if (!isDiminishing && turnTokens < budget * COMPLETION_THRESHOLD) {
tracker.continuationCount++
tracker.lastDeltaTokens = deltaSinceLastCheck
tracker.lastGlobalTurnTokens = globalTurnTokens
return {
action: 'continue',
nudgeMessage: getBudgetContinuationMessage(pct, turnTokens, budget),
continuationCount: tracker.continuationCount,
pct,
turnTokens,
budget,
}
}
Two constants govern continuation (query/tokenBudget.ts:3-4): COMPLETION_THRESHOLD = 0.9 (90%) and DIMINISHING_THRESHOLD = 500 output tokens; subagents always no-op. Once continuationCount >= 3, two consecutive deltas below 500 trip isDiminishing, logging tengu_token_budget_completed (query.ts:1343-1354) and returning { reason: 'completed' } (query.ts:1357).
Otherwise queryLoop() calls incrementBudgetContinuationCount(), appends the isMeta: true nudge (utils/tokenBudget.ts:66-73), resets hasAttemptedReactiveCompact: false and stopHookActive: undefined, and continues with { reason: 'token_budget_continuation' } (query.ts:1316-1340).
Keep this client-side feature('TOKEN_BUDGET') loop distinct from server-side params.taskBudget (query.ts:193-197, query.ts:699-706): params.taskBudget sends output_config.task_budget to the API while query.ts subtracts pre-compact tokens from loop-local taskBudgetRemaining (query.ts:291, query.ts:508-515, query.ts:1138-1146).
6. Forward Seams Inside queryLoop() (Compaction & Memory)
queryLoop() delegates Layer 3 compression and memory through two crisp seams without absorbing their internals:
- Compaction seam (Chapter 09): Slices via
getMessagesAfterCompactBoundary()(query.ts:365), then runsapplyToolResultBudget()(query.ts:376-394),feature('HISTORY_SNIP')snipCompactIfNeeded()(query.ts:400-410),deps.microcompact()+feature('CACHED_MICROCOMPACT')(query.ts:413-426,query.ts:870-892),feature('CONTEXT_COLLAPSE')applyCollapsesIfNeeded()(query.ts:440-447),deps.autocompact()(query.ts:453-543), and reactive 413 recovery (query.ts:1089-1166). - Memory seam (Chapter 10): Connects via
using pendingMemoryPrefetch(query.ts:301-304, gated onisAutoMemoryEnabled()and'tengu_moth_copse'atutils/attachments.ts:2361-2424, consumed zero-wait atquery.ts:1599-1614), per-samplingexecutePostSamplingHooks()(query.ts:1000-1009) for Session Memory, and end-of-turn forks (executeExtractMemories,executeAutoDream) inquery/stopHooks.ts:141-156.
Putting the Layer Together
All 7 Continue transitions and all 10 Terminal exits (12 return sites) compose into one state machine inside queryLoop():
flowchart TD Entry["query() wrapper: no-finally lifecycle sweep"] --> Init["queryLoop(): State, config, using memoryPrefetch"] Init --> PreAPI["Pre-API seams (Ch 09) + blocking_limit guard"] PreAPI -->|"blocking limit"| TermBlock["Terminal: blocking_limit"] PreAPI -->|"ok"| Stream["deps.callModel() + fallbacks + withholding"] Stream -->|"ImageSize/ResizeError"| TermImg["Terminal: image_error"] Stream -->|"uncaught throw"| TermModel["Terminal: model_error"] Stream -->|"aborted mid-stream"| TermAbortS["Terminal: aborted_streaming"] Stream -->|"needsFollowUp == false"| Recover["4-stage withheld error recovery ladder"] Recover -->|"collapse / reactive / escalate / resume"| PreAPI Recover -->|"unrecovered 413 / media"| TermPTL["Terminal: prompt_too_long / image_error"] Recover -->|"other API error / exhausted"| TermErrDone["Terminal: completed (StopFailure hooks)"] Recover -->|"clean reply"| StopHooks["handleStopHooks(): CacheSafeParams + hooks"] StopHooks -->|"preventContinuation"| TermStopPrev["Terminal: stop_hook_prevented"] StopHooks -->|"blockingErrors"| PreAPI StopHooks -->|"clean + TOKEN_BUDGET"| Budget["checkTokenBudget(): < 90% and active"] Budget -->|"continue nudge"| PreAPI Budget -->|"stop"| TermDone["Terminal: completed"] StopHooks -->|"clean (no budget)"| TermDone Stream -->|"needsFollowUp == true"| Tools["StreamingToolExecutor / runTools() + housekeeping"] Tools -->|"aborted mid-tools"| TermAbortT["Terminal: aborted_tools"] Tools -->|"hook_stopped_continuation"| TermHookStop["Terminal: hook_stopped"] Tools -->|"nextTurnCount > maxTurns"| TermMax["Terminal: max_turns"] Tools -->|"next_turn (turnCount + 1)"| PreAPI
And the exact ownership split across query(), loop-local resources, and 10-field State:
query(params) [query.ts:219-239]
| yield* queryLoop(params, consumedCommandUuids)
| NO finally -> notifyCommandLifecycle(uuid, 'completed') on normal return
v
queryLoop() [query.ts:241-1729]
| Once: deps, config (QueryConfig), budgetTracker, taskBudgetRemaining
| Once: using pendingMemoryPrefetch <-- Symbol.dispose on ALL exits
|
+-> while (true) over 10-field State (whole-object state = next)
1. Pre-API compaction seams (Ch 09) -> [blocking_limit]
2. deps.callModel() + fallbacks -> [image_error | model_error]
- withhold 413/media/max_output_tokens; retain in assistantMessages
- needsFollowUp = any content.type === 'tool_use'
3. executePostSamplingHooks() -> [aborted_streaming]
4a. !needsFollowUp:
413/media: collapse_drain_retry -> reactive_compact_retry
-> unrecovered? StopFailure -> [prompt_too_long | image_error]
max_output_tokens_escalate -> max_output_tokens_recovery
-> exhausted / API error? StopFailure -> [completed]
-> handleStopHooks() -> [stop_hook_prevented] | stop_hook_blocking
-> checkTokenBudget() -> token_budget_continuation | [completed]
4b. needsFollowUp:
drain tools -> [aborted_tools | hook_stopped]
-> attachments/queue/prefetches/refreshTools -> [max_turns]
-> next_turn (SOLE turnCount + 1 transition)
How This Snaps Into the Next Layer
query() owns exactly one user turn, from accepted input through all intermediate tool calls and recoveries until a Terminal exit. Conversation-lifetime state belongs to QueryEngine.ts in Chapter 08:
- Conversation envelope (
QueryEngine.ts:184-207):class QueryEnginepersistsmutableMessages,readFileState,permissionDenials,totalUsage,hasHandledOrphanedPermission, andloadedNestedMemoryPathsacross consecutivesubmitMessage()calls (QueryEngine.ts:209-1156), while clearing turn-scopeddiscoveredSkillNamesat the start of eachsubmitMessage()(QueryEngine.ts:192-197,QueryEngine.ts:238). - Consuming
query()(QueryEngine.ts:675-1049):submitMessage()consumesfor await (const message of query({ ..., querySource: 'sdk', maxTurns, taskBudget })), writing transcripts viarecordTranscript()(fire-and-forget forassistantmessages somessage_deltamutations land before lazy 100ms JSON serialization,QueryEngine.ts:717-732) and framing events intoSDKMessages. - Post-turn result scan &
ask()(QueryEngine.ts:1058-1060,QueryEngine.ts:1186-1295): Scansmessages.findLast(m => m.type === 'assistant' || m.type === 'user')so trailinghandleStopHooks()attachments never blank out-presults, and enforcesmaxBudgetUsdand structured-output retry caps outsidequery(). One-shotask()wrapsQueryEngineand syncsreadFileStateinfinally. - The other five
query()callers: Interactive TUI (screens/REPL.tsx:2793, Chapter 21), subagents (tools/AgentTool/runAgent.ts:748) and cache-sharing forks (utils/forkedAgent.ts:545, Chapter 18), background main-session tasks (tasks/LocalMainSessionTask.ts:383, Chapter 17), and agent lifecycle hooks (utils/hooks/execAgentHook.ts:167, Chapter 13).
Reading Exercises & Code Checkpoints
- Reconstruct snapshot-absent
query/transitions.tsacross all 19 state-machine edges:Verify all 7rg -n "return \{ reason:|reason: '" query.tsContinuevariants and all 10Terminalvariants across 12returnsites—noting whyreturn \{ reason:is needed to catchquery.ts:1175's ternaryisWithheldMedia ? 'image_error' : 'prompt_too_long'. - Compare snapshotted
QueryConfigruntime gates andQueryDepsagainst inlinefeature()DCE boundaries:Inspectrg -n "feature\('|QueryConfig|buildQueryConfig|QueryDeps|productionDeps" query.ts query/config.ts query/deps.tsquery/config.ts:13-14andquery/deps.ts:8-20to see whyfeature('...')stays inline inquery.tswhile'tengu_streaming_tool_execution2'and env gates snapshot intoQueryConfig, and verify thatquery.ts:69(jobClassifier) has zero references inquery.ts. - Trace
needsFollowUp, withheld error suppression, andtengu_otk_slot_v18k-to-64k escalation:Followrg -n "needsFollowUp|let withheld|isWithheldMaxOutputTokens|tengu_otk_slot_v1|MAX_OUTPUT_TOKENS_RECOVERY_LIMIT" query.tswithheldfromquery.ts:799-825toquery.ts:1188-1256and confirm whyCLAUDE_CODE_MAX_OUTPUT_TOKENS(query.ts:1202) bypassesmax_output_tokens_escalateeven when'tengu_otk_slot_v1'is enabled. - Audit turn-end orchestration:
CacheSafeParams,!isBareMode(),stopHookActive, andcheckTokenBudget():Comparerg -n "saveCacheSafeParams|isBareMode|stopHookActive|COMPLETION_THRESHOLD|DIMINISHING_THRESHOLD|checkTokenBudget" query.ts query/stopHooks.ts query/tokenBudget.tsstopHookActiveacross all 7Continuesites inquery.ts(identifying the 2 sites that set or preserve it vs. the 5 sites that reset it toundefined), and tracecheckTokenBudget()atcontinuationCount === 3(after three continuations) whenlastDeltaTokensis400anddeltaSinceLastCheckis450output tokens.
Manuscript chapters/07-the-heartbeat-query-loop.md from AtlantisPleb/cc at 5aa286a, synced Nov 24, 2026.