MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / queryLoop

Function queryLoop

source code/query.ts:243–1731  ·  view source on GitHub ↗
(
  params: QueryParams,
  consumedCommandUuids: string[],
)

Source from the content-addressed store, hash-verified

241}
242
243async function* queryLoop(
244 params: QueryParams,
245 consumedCommandUuids: string[],
246): AsyncGenerator<
247 | StreamEvent
248 | RequestStartEvent
249 | Message
250 | TombstoneMessage
251 | ToolUseSummaryMessage,
252 Terminal
253> {
254 // Immutable params — never reassigned during the query loop.
255 const {
256 systemPrompt,
257 userContext,
258 systemContext,
259 canUseTool,
260 fallbackModel,
261 querySource,
262 maxTurns,
263 skipCacheWrite,
264 } = params
265 const deps = params.deps ?? productionDeps()
266
267 // Mutable cross-iteration state. The loop body destructures this at the top
268 // of each iteration so reads stay bare-name (`messages`, `toolUseContext`).
269 // Continue sites write `state = { ... }` instead of 9 separate assignments.
270 let state: State = {
271 messages: params.messages,
272 toolUseContext: params.toolUseContext,
273 maxOutputTokensOverride: params.maxOutputTokensOverride,
274 autoCompactTracking: undefined,
275 stopHookActive: undefined,
276 maxOutputTokensRecoveryCount: 0,
277 hasAttemptedReactiveCompact: false,
278 turnCount: 1,
279 pendingToolUseSummary: undefined,
280 transition: undefined,
281 }
282 const budgetTracker = feature('TOKEN_BUDGET') ? createBudgetTracker() : null
283
284 // task_budget.remaining tracking across compaction boundaries. Undefined
285 // until first compact fires — while context is uncompacted the server can
286 // see the full history and handles the countdown from {total} itself (see
287 // api/api/sampling/prompt/renderer.py:292). After a compact, the server sees
288 // only the summary and would under-count spend; remaining tells it the
289 // pre-compact final window that got summarized away. Cumulative across
290 // multiple compacts: each subtracts the final context at that compact's
291 // trigger point. Loop-local (not on State) to avoid touching the 7 continue
292 // sites.
293 let taskBudgetRemaining: number | undefined = undefined
294
295 // Snapshot immutable env/statsig/session state once at entry. See QueryConfig
296 // for what's included and why feature() gates are intentionally excluded.
297 const config = buildQueryConfig()
298
299 // Fired once per user turn — the prompt is invariant across loop iterations,
300 // so per-iteration firing would ask sideQuery the same question N times.

Callers 1

queryFunction · 0.85

Calls 15

productionDepsFunction · 0.85
createBudgetTrackerFunction · 0.85
buildQueryConfigFunction · 0.85
queryCheckpointFunction · 0.85
applyToolResultBudgetFunction · 0.85
recordContentReplacementFunction · 0.85
asSystemPromptFunction · 0.85
appendSystemContextFunction · 0.85
logEventFunction · 0.85

Tested by

no test coverage detected