GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — claude-code-source-code/src/query.ts at main · memoryblade/claude-code-source-code Expand file tree/ Copy pathquery.tsMore file actions 1729 lines (1612 loc) · 67.1 KB / Copy pathquery.tsFile metadata and controls1729 lines (1612 loc) · 67.1 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 // biome-ignore-all assist/source/organizeImports: ANT-ONLY import markers must not be reordered import type { ToolResultBlockParam, ToolUseBlock, } from '@anthropic-ai/sdk/resources/index.mjs' import type { CanUseToolFn } from './hooks/useCanUseTool.js' import { FallbackTriggeredError } from './services/api/withRetry.js' import { calculateTokenWarningState, isAutoCompactEnabled, type AutoCompactTrackingState, } from './services/compact/autoCompact.js' import { buildPostCompactMessages } from './services/compact/compact.js' /* eslint-disable @typescript-eslint/no-require-imports */ const reactiveCompact = feature('REACTIVE_COMPACT') ? (require('./services/compact/reactiveCompact.js') as typeof import('./services/compact/reactiveCompact.js')) : null const contextCollapse = feature('CONTEXT_COLLAPSE') ? (require('./services/contextCollapse/index.js') as typeof import('./services/contextCollapse/index.js')) : null /* eslint-enable @typescript-eslint/no-require-imports */ import { logEvent, type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, } from 'src/services/analytics/index.js' import { ImageSizeError } from './utils/imageValidation.js' import { ImageResizeError } from './utils/imageResizer.js' import { findToolByName, type ToolUseContext } from './Tool.js' import { asSystemPrompt, type SystemPrompt } from './utils/systemPromptType.js' import type { AssistantMessage, AttachmentMessage, Message, RequestStartEvent, StreamEvent, ToolUseSummaryMessage, UserMessage, TombstoneMessage, } from './types/message.js' import { logError } from './utils/log.js' import { PROMPT_TOO_LONG_ERROR_MESSAGE, isPromptTooLongMessage, } from './services/api/errors.js' import { logAntError, logForDebugging } from './utils/debug.js' import { createUserMessage, createUserInterruptionMessage, normalizeMessagesForAPI, createSystemMessage, createAssistantAPIErrorMessage, getMessagesAfterCompactBoundary, createToolUseSummaryMessage, createMicrocompactBoundaryMessage, stripSignatureBlocks, } from './utils/messages.js' import { generateToolUseSummary } from './services/toolUseSummary/toolUseSummaryGenerator.js' import { prependUserContext, appendSystemContext } from './utils/api.js' import { createAttachmentMessage, filterDuplicateMemoryAttachments, getAttachmentMessages, startRelevantMemoryPrefetch, } from './utils/attachments.js' /* eslint-disable @typescript-eslint/no-require-imports */ const skillPrefetch = feature('EXPERIMENTAL_SKILL_SEARCH') ? (require('./services/skillSearch/prefetch.js') as typeof import('./services/skillSearch/prefetch.js')) : null const jobClassifier = feature('TEMPLATES') ? (require('./jobs/classifier.js') as typeof import('./jobs/classifier.js')) : null /* eslint-enable @typescript-eslint/no-require-imports */ import { remove as removeFromQueue, getCommandsByMaxPriority, isSlashCommand, } from './utils/messageQueueManager.js' import { notifyCommandLifecycle } from './utils/commandLifecycle.js' import { headlessProfilerCheckpoint } from './utils/headlessProfiler.js' import { getRuntimeMainLoopModel, renderModelName, } from './utils/model/model.js' import { doesMostRecentAssistantMessageExceed200k, finalContextTokensFromLastResponse, tokenCountWithEstimation, } from './utils/tokens.js' import { ESCALATED_MAX_TOKENS } from './utils/context.js' import { getFeatureValue_CACHED_MAY_BE_STALE } from './services/analytics/growthbook.js' import { SLEEP_TOOL_NAME } from './tools/SleepTool/prompt.js' import { executePostSamplingHooks } from './utils/hooks/postSamplingHooks.js' import { executeStopFailureHooks } from './utils/hooks.js' import type { QuerySource } from './constants/querySource.js' import { createDumpPromptsFetch } from './services/api/dumpPrompts.js' import { StreamingToolExecutor } from './services/tools/StreamingToolExecutor.js' import { queryCheckpoint } from './utils/queryProfiler.js' import { runTools } from './services/tools/toolOrchestration.js' import { applyToolResultBudget } from './utils/toolResultStorage.js' import { recordContentReplacement } from './utils/sessionStorage.js' import { handleStopHooks } from './query/stopHooks.js' import { buildQueryConfig } from './query/config.js' import { productionDeps, type QueryDeps } from './query/deps.js' import type { Terminal, Continue } from './query/transitions.js' import { feature } from 'bun:bundle' import { getCurrentTurnTokenBudget, getTurnOutputTokens, incrementBudgetContinuationCount, } from './bootstrap/state.js' import { createBudgetTracker, checkTokenBudget } from './query/tokenBudget.js' import { count } from './utils/array.js' /* eslint-disable @typescript-eslint/no-require-imports */ const snipModule = feature('HISTORY_SNIP') ? (require('./services/compact/snipCompact.js') as typeof import('./services/compact/snipCompact.js')) : null const taskSummaryModule = feature('BG_SESSIONS') ? (require('./utils/taskSummary.js') as typeof import('./utils/taskSummary.js')) : null /* eslint-enable @typescript-eslint/no-require-imports */ function* yieldMissingToolResultBlocks( assistantMessages: AssistantMessage[], errorMessage: string, ) { for (const assistantMessage of assistantMessages) { // Extract all tool use blocks from this assistant message const toolUseBlocks = assistantMessage.message.content.filter( content => content.type === 'tool_use', ) as ToolUseBlock[] // Emit an interruption message for each tool use for (const toolUse of toolUseBlocks) { yield createUserMessage({ content: [ { type: 'tool_result', content: errorMessage, is_error: true, tool_use_id: toolUse.id, }, ], toolUseResult: errorMessage, sourceToolAssistantUUID: assistantMessage.uuid, }) } } } /** * The rules of thinking are lengthy and fortuitous. They require plenty of thinking * of most long duration and deep meditation for a wizard to wrap one's noggin around. * * The rules follow: * 1. A message that contains a thinking or redacted_thinking block must be part of a query whose max_thinking_length > 0 * 2. A thinking block may not be the last message in a block * 3. Thinking blocks must be preserved for the duration of an assistant trajectory (a single turn, or if that turn includes a tool_use block then also its subsequent tool_result and the following assistant message) * * Heed these rules well, young wizard. For they are the rules of thinking, and * the rules of thinking are the rules of the universe. If ye does not heed these * rules, ye will be punished with an entire day of debugging and hair pulling. */ const MAX_OUTPUT_TOKENS_RECOVERY_LIMIT = 3 /** * Is this a max_output_tokens error message? If so, the streaming loop should * withhold it from SDK callers until we know whether the recovery loop can * continue. Yielding early leaks an intermediate error to SDK callers (e.g. * cowork/desktop) that terminate the session on any `error` field — the * recovery loop keeps running but nobody is listening. * * Mirrors reactiveCompact.isWithheldPromptTooLong. */ function isWithheldMaxOutputTokens( msg: Message | StreamEvent | undefined, ): msg is AssistantMessage { return msg?.type === 'assistant' && msg.apiError === 'max_output_tokens' } export type QueryParams = { messages: Message[] systemPrompt: SystemPrompt userContext: { [k: string]: string } systemContext: { [k: string]: string } canUseTool: CanUseToolFn toolUseContext: ToolUseContext fallbackModel?: string querySource: QuerySource maxOutputTokensOverride?: number maxTurns?: number skipCacheWrite?: boolean // API task_budget (output_config.task_budget, beta task-budgets-2026-03-13). // Distinct from the tokenBudget +500k auto-continue feature. `total` is the // budget for the whole agentic turn; `remaining` is computed per iteration // from cumulative API usage. See configureTaskBudgetParams in claude.ts. taskBudget?: { total: number } deps?: QueryDeps } // -- query loop state // Mutable state carried between loop iterations 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 } 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 } async function* queryLoop( params: QueryParams, consumedCommandUuids: string[], ): AsyncGenerator< | StreamEvent | RequestStartEvent | Message | TombstoneMessage | ToolUseSummaryMessage, Terminal > { // Immutable params — never reassigned during the query loop. const { systemPrompt, userContext, systemContext, canUseTool, fallbackModel, querySource, maxTurns, skipCacheWrite, } = params const deps = params.deps ?? productionDeps() // Mutable cross-iteration state. The loop body destructures this at the top // of each iteration so reads stay bare-name (`messages`, `toolUseContext`). // Continue sites write `state = { ... }` instead of 9 separate assignments. let state: State = { messages: params.messages, toolUseContext: params.toolUseContext, maxOutputTokensOverride: params.maxOutputTokensOverride, autoCompactTracking: undefined, stopHookActive: undefined, maxOutputTokensRecoveryCount: 0, hasAttemptedReactiveCompact: false, turnCount: 1, pendingToolUseSummary: undefined, transition: undefined, } const budgetTracker = feature('TOKEN_BUDGET') ? createBudgetTracker() : null // task_budget.remaining tracking across compaction boundaries. Undefined // until first compact fires — while context is uncompacted the server can // see the full history and handles the countdown from {total} itself (see // api/api/sampling/prompt/renderer.py:292). After a compact, the server sees // only the summary and would under-count spend; remaining tells it the // pre-compact final window that got summarized away. Cumulative across // multiple compacts: each subtracts the final context at that compact's // trigger point. Loop-local (not on State) to avoid touching the 7 continue // sites. let taskBudgetRemaining: number | undefined = undefined // Snapshot immutable env/statsig/session state once at entry. See QueryConfig // for what's included and why feature() gates are intentionally excluded. const config = buildQueryConfig() // Fired once per user turn — the prompt is invariant across loop iterations, // so per-iteration firing would ask sideQuery the same question N times. // Consume point polls settledAt (never blocks). `using` disposes on all // generator exit paths — see MemoryPrefetch for dispose/telemetry semantics. using pendingMemoryPrefetch = startRelevantMemoryPrefetch( state.messages, state.toolUseContext, ) // eslint-disable-next-line no-constant-condition while (true) { // Destructure state at the top of each iteration. toolUseContext alone // is reassigned within an iteration (queryTracking, messages updates); // the rest are read-only between continue sites. let { toolUseContext } = state const { messages, autoCompactTracking, maxOutputTokensRecoveryCount, hasAttemptedReactiveCompact, maxOutputTokensOverride, pendingToolUseSummary, stopHookActive, turnCount, } = state // Skill discovery prefetch — per-iteration (uses findWritePivot guard // that returns early on non-write iterations). Discovery runs while the // model streams and tools execute; awaited post-tools alongside the // memory prefetch consume. Replaces the blocking assistant_turn path // that ran inside getAttachmentMessages (97% of those calls found // nothing in prod). Turn-0 user-input discovery still blocks in // userInputAttachments — that's the one signal where there's no prior // work to hide under. const pendingSkillPrefetch = skillPrefetch?.startSkillDiscoveryPrefetch( null, messages, toolUseContext, ) yield { type: 'stream_request_start' } queryCheckpoint('query_fn_entry') // Record query start for headless latency tracking (skip for subagents) if (!toolUseContext.agentId) { headlessProfilerCheckpoint('query_started') } // Initialize or increment query chain tracking const queryTracking = toolUseContext.queryTracking ? { chainId: toolUseContext.queryTracking.chainId, depth: toolUseContext.queryTracking.depth + 1, } : { chainId: deps.uuid(), depth: 0, } const queryChainIdForAnalytics = queryTracking.chainId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS toolUseContext = { ...toolUseContext, queryTracking, } let messagesForQuery = [...getMessagesAfterCompactBoundary(messages)] let tracking = autoCompactTracking // Enforce per-message budget on aggregate tool result size. Runs BEFORE // microcompact — cached MC operates purely by tool_use_id (never inspects // content), so content replacement is invisible to it and the two compose // cleanly. No-ops when contentReplacementState is undefined (feature off). // Persist only for querySources that read records back on resume: agentId // routes to sidechain file (AgentTool resume) or session file (/resume). // Ephemeral runForkedAgent callers (agent_summary etc.) don't persist. const persistReplacements = querySource.startsWith('agent:') || querySource.startsWith('repl_main_thread') messagesForQuery = await applyToolResultBudget( messagesForQuery, toolUseContext.contentReplacementState, persistReplacements ? records => void recordContentReplacement( records, toolUseContext.agentId, ).catch(logError) : undefined, new Set( toolUseContext.options.tools .filter(t => !Number.isFinite(t.maxResultSizeChars)) .map(t => t.name), ), ) // Apply snip before microcompact (both may run — they are not mutually exclusive). // snipTokensFreed is plumbed to autocompact so its threshold check reflects // what snip removed; tokenCountWithEstimation alone can't see it (reads usage // from the protected-tail assistant, which survives snip unchanged). let snipTokensFreed = 0 if (feature('HISTORY_SNIP')) { queryCheckpoint('query_snip_start') const snipResult = snipModule!.snipCompactIfNeeded(messagesForQuery) messagesForQuery = snipResult.messages snipTokensFreed = snipResult.tokensFreed if (snipResult.boundaryMessage) { yield snipResult.boundaryMessage } queryCheckpoint('query_snip_end') } // Apply microcompact before autocompact queryCheckpoint('query_microcompact_start') const microcompactResult = await deps.microcompact( messagesForQuery, toolUseContext, querySource, ) messagesForQuery = microcompactResult.messages // For cached microcompact (cache editing), defer boundary message until after // the API response so we can use actual cache_deleted_input_tokens. // Gated behind feature() so the string is eliminated from external builds. const pendingCacheEdits = feature('CACHED_MICROCOMPACT') ? microcompactResult.compactionInfo?.pendingCacheEdits : undefined queryCheckpoint('query_microcompact_end') // Project the collapsed context view and maybe commit more collapses. // Runs BEFORE autocompact so that if collapse gets us under the // autocompact threshold, autocompact is a no-op and we keep granular // context instead of a single summary. // // Nothing is yielded — the collapsed view is a read-time projection // over the REPL's full history. Summary messages live in the collapse // store, not the REPL array. This is what makes collapses persist // across turns: projectView() replays the commit log on every entry. // Within a turn, the view flows forward via state.messages at the // continue site (query.ts:1192), and the next projectView() no-ops // because the archived messages are already gone from its input. if (feature('CONTEXT_COLLAPSE') && contextCollapse) { const collapseResult = await contextCollapse.applyCollapsesIfNeeded( messagesForQuery, toolUseContext, querySource, ) messagesForQuery = collapseResult.messages } const fullSystemPrompt = asSystemPrompt( appendSystemContext(systemPrompt, systemContext), ) queryCheckpoint('query_autocompact_start') const { compactionResult, consecutiveFailures } = await deps.autocompact( messagesForQuery, toolUseContext, { systemPrompt, userContext, systemContext, toolUseContext, forkContextMessages: messagesForQuery, }, querySource, tracking, snipTokensFreed, ) queryCheckpoint('query_autocompact_end') if (compactionResult) { const { preCompactTokenCount, postCompactTokenCount, truePostCompactTokenCount, compactionUsage, } = compactionResult logEvent('tengu_auto_compact_succeeded', { originalMessageCount: messages.length, compactedMessageCount: compactionResult.summaryMessages.length + compactionResult.attachments.length + compactionResult.hookResults.length, preCompactTokenCount, postCompactTokenCount, truePostCompactTokenCount, compactionInputTokens: compactionUsage?.input_tokens, compactionOutputTokens: compactionUsage?.output_tokens, compactionCacheReadTokens: compactionUsage?.cache_read_input_tokens ?? 0, compactionCacheCreationTokens: compactionUsage?.cache_creation_input_tokens ?? 0, compactionTotalTokens: compactionUsage ? compactionUsage.input_tokens + (compactionUsage.cache_creation_input_tokens ?? 0) + (compactionUsage.cache_read_input_tokens ?? 0) + compactionUsage.output_tokens : 0, queryChainId: queryChainIdForAnalytics, queryDepth: queryTracking.depth, }) // task_budget: capture pre-compact final context window before // messagesForQuery is replaced with postCompactMessages below. // iterations[-1] is the authoritative final window (post server tool // loops); see #304930. if (params.taskBudget) { const preCompactContext = finalContextTokensFromLastResponse(messagesForQuery) taskBudgetRemaining = Math.max( 0, (taskBudgetRemaining ?? params.taskBudget.total) - preCompactContext, ) } // Reset on every compact so turnCounter/turnId reflect the MOST RECENT // compact. recompactionInfo (autoCompact.ts:190) already captured the // old values for turnsSincePreviousCompact/previousCompactTurnId before // the call, so this reset doesn't lose those. tracking = { compacted: true, turnId: deps.uuid(), turnCounter: 0, consecutiveFailures: 0, } const postCompactMessages = buildPostCompactMessages(compactionResult) for (const message of postCompactMessages) { yield message } // Continue on with the current query call using the post compact messages messagesForQuery = postCompactMessages } else if (consecutiveFailures !== undefined) { // Autocompact failed — propagate failure count so the circuit breaker // can stop retrying on the next iteration. tracking = { ...(tracking ?? { compacted: false, turnId: '', turnCounter: 0 }), consecutiveFailures, } } //TODO: no need to set toolUseContext.messages during set-up since it is updated here toolUseContext = { ...toolUseContext, messages: messagesForQuery, } 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 const appState = toolUseContext.getAppState() const permissionMode = appState.toolPermissionContext.mode let currentModel = getRuntimeMainLoopModel({ permissionMode, mainLoopModel: toolUseContext.options.mainLoopModel, exceeds200kTokens: permissionMode === 'plan' && doesMostRecentAssistantMessageExceed200k(messagesForQuery), }) queryCheckpoint('query_setup_end') // Create fetch wrapper once per query session to avoid memory retention. // Each call to createDumpPromptsFetch creates a closure that captures the request body. // Creating it once means only the latest request body is retained (~700KB), // instead of all request bodies from the session (~500MB for long sessions). // Note: agentId is effectively constant during a query() call - it only changes // between queries (e.g., /clear command or session resume). const dumpPromptsFetch = config.gates.isAnt ? createDumpPromptsFetch(toolUseContext.agentId ?? config.sessionId) : undefined // Block if we've hit the hard blocking limit (only applies when auto-compact is OFF) // This reserves space so users can still run /compact manually // Skip this check if compaction just happened - the compaction result is already // validated to be under the threshold, and tokenCountWithEstimation would use // stale input_tokens from kept messages that reflect pre-compaction context size. // Same staleness applies to snip: subtract snipTokensFreed (otherwise we'd // falsely block in the window where snip brought us under autocompact threshold // but the stale usage is still above blocking limit — before this PR that // window never existed because autocompact always fired on the stale count). // Also skip for compact/session_memory queries — these are forked agents that // inherit the full conversation and would deadlock if blocked here (the compact // agent needs to run to REDUCE the token count). // Also skip when reactive compact is enabled and automatic compaction is // allowed — the preempt's synthetic error returns before the API call, // so reactive compact would never see a prompt-too-long to react to. // Widened to walrus so RC can act as fallback when proactive fails. // // Same skip for context-collapse: its recoverFromOverflow drains // staged collapses on a REAL API 413, then falls through to // reactiveCompact. A synthetic preempt here would return before the // API call and starve both recovery paths. The isAutoCompactEnabled() // conjunct preserves the user's explicit "no automatic anything" // config — if they set DISABLE_AUTO_COMPACT, they get the preempt. let collapseOwnsIt = false if (feature('CONTEXT_COLLAPSE')) { collapseOwnsIt = (contextCollapse?.isContextCollapseEnabled() ?? false) && isAutoCompactEnabled() } // Hoist media-recovery gate once per turn. Withholding (inside the // stream loop) and recovery (after) must agree; CACHED_MAY_BE_STALE can // flip during the 5-30s stream, and withhold-without-recover would eat // the message. PTL doesn't hoist because its withholding is ungated — // it predates the experiment and is already the control-arm baseline. const mediaRecoveryEnabled = reactiveCompact?.isReactiveCompactEnabled() ?? false if ( !compactionResult && querySource !== 'compact' && querySource !== 'session_memory' && !( reactiveCompact?.isReactiveCompactEnabled() && isAutoCompactEnabled() ) && !collapseOwnsIt ) { const { isAtBlockingLimit } = calculateTokenWarningState( tokenCountWithEstimation(messagesForQuery) - snipTokensFreed, toolUseContext.options.mainLoopModel, ) if (isAtBlockingLimit) { yield createAssistantAPIErrorMessage({ content: PROMPT_TOO_LONG_ERROR_MESSAGE, error: 'invalid_request', }) return { reason: 'blocking_limit' } } } let attemptWithFallback = true queryCheckpoint('query_api_loop_start') try { while (attemptWithFallback) { attemptWithFallback = false try { let streamingFallbackOccured = false queryCheckpoint('query_api_streaming_start') for await (const message of deps.callModel({ messages: prependUserContext(messagesForQuery, userContext), systemPrompt: fullSystemPrompt, thinkingConfig: toolUseContext.options.thinkingConfig, tools: toolUseContext.options.tools, signal: toolUseContext.abortController.signal, options: { async getToolPermissionContext() { const appState = toolUseContext.getAppState() return appState.toolPermissionContext }, model: currentModel, ...(config.gates.fastModeEnabled && { fastMode: appState.fastMode, }), toolChoice: undefined, isNonInteractiveSession: toolUseContext.options.isNonInteractiveSession, fallbackModel, onStreamingFallback: () => { streamingFallbackOccured = true }, querySource, agents: toolUseContext.options.agentDefinitions.activeAgents, allowedAgentTypes: toolUseContext.options.agentDefinitions.allowedAgentTypes, hasAppendSystemPrompt: !!toolUseContext.options.appendSystemPrompt, maxOutputTokensOverride, fetchOverride: dumpPromptsFetch, mcpTools: appState.mcp.tools, hasPendingMcpServers: appState.mcp.clients.some( c => c.type === 'pending', ), queryTracking, effortValue: appState.effortValue, advisorModel: appState.advisorModel, skipCacheWrite, agentId: toolUseContext.agentId, addNotification: toolUseContext.addNotification, ...(params.taskBudget && { taskBudget: { total: params.taskBudget.total, ...(taskBudgetRemaining !== undefined && { remaining: taskBudgetRemaining, }), }, }), }, })) { // We won't use the tool_calls from the first attempt // We could.. but then we'd have to merge assistant messages // with different ids and double up on full the tool_results if (streamingFallbackOccured) { // Yield tombstones for orphaned messages so they're removed from UI and transcript. // These partial messages (especially thinking blocks) have invalid signatures // that would cause "thinking blocks cannot be modified" API errors. for (const msg of assistantMessages) { yield { type: 'tombstone' as const, message: msg } } logEvent('tengu_orphaned_messages_tombstoned', { orphanedMessageCount: assistantMessages.length, queryChainId: queryChainIdForAnalytics, queryDepth: queryTracking.depth, }) assistantMessages.length = 0 toolResults.length = 0 toolUseBlocks.length = 0 needsFollowUp = false // Discard pending results from the failed streaming attempt and create // a fresh executor. This prevents orphan tool_results (with old tool_use_ids) // from being yielded after the fallback response arrives. if (streamingToolExecutor) { streamingToolExecutor.discard() streamingToolExecutor = new StreamingToolExecutor( toolUseContext.options.tools, canUseTool, toolUseContext, ) } } // Backfill tool_use inputs on a cloned message before yield so // SDK stream output and transcript serialization see legacy/derived // fields. The original `message` is left untouched for // assistantMessages.push below — it flows back to the API and // mutating it would break prompt caching (byte mismatch). let yieldMessage: typeof message = message if (message.type === 'assistant') { let clonedContent: typeof message.message.content | undefined for (let i = 0; i < message.message.content.length; i++) { const block = message.message.content[i]! if ( block.type === 'tool_use' && typeof block.input === 'object' && block.input !== null ) { const tool = findToolByName( toolUseContext.options.tools, block.name, ) if (tool?.backfillObservableInput) { const originalInput = block.input as Record<string, unknown> const inputCopy = { ...originalInput } tool.backfillObservableInput(inputCopy) // Only yield a clone when backfill ADDED fields; skip if // it only OVERWROTE existing ones (e.g. file tools // expanding file_path). Overwrites change the serialized // transcript and break VCR fixture hashes on resume, // while adding nothing the SDK stream needs — hooks get // the expanded path via toolExecution.ts separately. const addedFields = Object.keys(inputCopy).some( k => !(k in originalInput), ) if (addedFields) { clonedContent ??= [...message.message.content] clonedContent[i] = { ...block, input: inputCopy } } } } } if (clonedContent) { yieldMessage = { ...message, message: { ...message.message, content: clonedContent }, } } } // Withhold recoverable errors (prompt-too-long, max-output-tokens) // until we know whether recovery (collapse drain / reactive // compact / truncation retry) can succeed. Still pushed to // assistantMessages so the recovery checks below find them. // Either subsystem's withhold is sufficient — they're // independent so turning one off doesn't break the other's // recovery path. // // 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) const msgToolUseBlocks = message.message.content.filter( content => content.type === 'tool_use', ) as ToolUseBlock[] if (msgToolUseBlocks.length > 0) { toolUseBlocks.push(...msgToolUseBlocks) needsFollowUp = true } if ( streamingToolExecutor && !toolUseContext.abortController.signal.aborted ) { for (const toolBlock of msgToolUseBlocks) { streamingToolExecutor.addTool(toolBlock, message) } } } if ( streamingToolExecutor && !toolUseContext.abortController.signal.aborted ) { for (const result of streamingToolExecutor.getCompletedResults()) { if (result.message) { yield result.message toolResults.push( ...normalizeMessagesForAPI( [result.message], toolUseContext.options.tools, ).filter(_ => _.type === 'user'), ) } } } } queryCheckpoint('query_api_streaming_end') // Yield deferred microcompact boundary message using actual API-reported // token deletion count instead of client-side estimates. // Entire block gated behind feature() so the excluded string // is eliminated from external builds. if (feature('CACHED_MICROCOMPACT') && pendingCacheEdits) { const lastAssistant = assistantMessages.at(-1) // The API field is cumulative/sticky across requests, so we // subtract the baseline captured before this request to get the delta. const usage = lastAssistant?.message.usage const cumulativeDeleted = usage ? ((usage as unknown as Record<string, number>) .cache_deleted_input_tokens ?? 0) : 0 const deletedTokens = Math.max( 0, cumulativeDeleted - pendingCacheEdits.baselineCacheDeletedTokens, ) if (deletedTokens > 0) { yield createMicrocompactBoundaryMessage( pendingCacheEdits.trigger, 0, deletedTokens, pendingCacheEdits.deletedToolIds, [], ) } } } catch (innerError) { if (innerError instanceof FallbackTriggeredError && fallbackModel) { // Fallback was triggered - switch model and retry currentModel = fallbackModel attemptWithFallback = true // Clear assistant messages since we'll retry the entire request yield* yieldMissingToolResultBlocks( assistantMessages, 'Model fallback triggered', ) assistantMessages.length = 0 toolResults.length = 0 toolUseBlocks.length = 0 needsFollowUp = false // Discard pending results from the failed attempt and create a // fresh executor. This prevents orphan tool_results (with old // tool_use_ids) from leaking into the retry. if (streamingToolExecutor) { streamingToolExecutor.discard() streamingToolExecutor = new StreamingToolExecutor( toolUseContext.options.tools, canUseTool, toolUseContext, ) } // Update tool use context with new model toolUseContext.options.mainLoopModel = fallbackModel // Thinking signatures are model-bound: replaying a protected-thinking // block (e.g. capybara) to an unprotected fallback (e.g. opus) 400s. // Strip before retry so the fallback model gets clean history. if (process.env.USER_TYPE === 'ant') { messagesForQuery = stripSignatureBlocks(messagesForQuery) } // Log the fallback event logEvent('tengu_model_fallback_triggered', { original_model: innerError.originalModel as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, fallback_model: fallbackModel as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, entrypoint: 'cli' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, queryChainId: queryChainIdForAnalytics, queryDepth: queryTracking.depth, }) // Yield system message about fallback — use 'warning' level so // users see the notification without needing verbose mode yield createSystemMessage( `Switched to ${renderModelName(innerError.fallbackModel)} due to high demand for ${renderModelName(innerError.originalModel)}`, 'warning', ) continue } throw innerError } } } catch (error) { logError(error) const errorMessage = error instanceof Error ? error.message : String(error) logEvent('tengu_query_error', { assistantMessages: assistantMessages.length, toolUses: assistantMessages.flatMap(_ => _.message.content.filter(content => content.type === 'tool_use'), ).length, queryChainId: queryChainIdForAnalytics, queryDepth: queryTracking.depth, }) // Handle image size/resize errors with user-friendly messages if ( error instanceof ImageSizeError || error instanceof ImageResizeError ) { yield createAssistantAPIErrorMessage({ content: error.message, }) return { reason: 'image_error' } } // Generally queryModelWithStreaming should not throw errors but instead // yield them as synthetic assistant messages. However if it does throw // due to a bug, we may end up in a state where we have already emitted // a tool_use block but will stop before emitting the tool_result. yield* yieldMissingToolResultBlocks(assistantMessages, errorMessage) // Surface the real error instead of a misleading "[Request interrupted // by user]" — this path is a model/runtime failure, not a user action. // SDK consumers were seeing phantom interrupts on e.g. Node 18's missing // Array.prototype.with(), masking the actual cause. yield createAssistantAPIErrorMessage({ content: errorMessage, }) // To help track down bugs, log loudly for ants logAntError('Query error', error) return { reason: 'model_error', error } } // Execute post-sampling hooks after model response is complete if (assistantMessages.length > 0) { |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |