ummaya 0.2.4 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (477) hide show
  1. package/README.md +15 -2
  2. package/bin/ummaya +10 -1
  3. package/npm-shrinkwrap.json +253 -2
  4. package/package.json +5 -1
  5. package/prompts/manifest.yaml +1 -1
  6. package/prompts/system_v1.md +1 -0
  7. package/pyproject.toml +26 -2
  8. package/specs/2803-document-production-hardening/contracts/document-tools.schema.json +1043 -0
  9. package/src/ummaya/_canonical/__init__.py +2 -0
  10. package/src/ummaya/engine/engine.py +29 -132
  11. package/src/ummaya/evidence/__init__.py +21 -2
  12. package/src/ummaya/evidence/dataset_contract.py +193 -0
  13. package/src/ummaya/evidence/document_authoring_cases.py +33 -0
  14. package/src/ummaya/evidence/document_harness.py +313 -0
  15. package/src/ummaya/evidence/document_viewer_ux.py +391 -0
  16. package/src/ummaya/evidence/gates.py +70 -0
  17. package/src/ummaya/evidence/json_types.py +20 -0
  18. package/src/ummaya/evidence/models.py +88 -1
  19. package/src/ummaya/evidence/output_payload.py +89 -0
  20. package/src/ummaya/evidence/payload_documents.py +233 -0
  21. package/src/ummaya/evidence/route_contracts.py +224 -0
  22. package/src/ummaya/evidence/route_helpers.py +150 -0
  23. package/src/ummaya/evidence/runner.py +81 -212
  24. package/src/ummaya/evidence/source_provenance.py +246 -0
  25. package/src/ummaya/evidence/source_provenance_redaction.py +176 -0
  26. package/src/ummaya/evidence/tool_layer.py +39 -0
  27. package/src/ummaya/evidence/tool_layer_models.py +151 -0
  28. package/src/ummaya/ipc/adapter_manifest_emitter.py +26 -10
  29. package/src/ummaya/ipc/document_intent_normalization.py +185 -0
  30. package/src/ummaya/ipc/frame_schema.py +5 -5
  31. package/src/ummaya/ipc/route_diagnostics.py +73 -0
  32. package/src/ummaya/ipc/stdio.py +1109 -477
  33. package/src/ummaya/llm/client.py +102 -3
  34. package/src/ummaya/llm/config.py +8 -3
  35. package/src/ummaya/primitives/__init__.py +6 -2
  36. package/src/ummaya/primitives/delegation.py +1 -1
  37. package/src/ummaya/primitives/document.py +28 -0
  38. package/src/ummaya/settings.py +0 -3
  39. package/src/ummaya/tools/discovery_bridge.py +17 -1
  40. package/src/ummaya/tools/documents/__init__.py +297 -0
  41. package/src/ummaya/tools/documents/adapter_registry.py +487 -0
  42. package/src/ummaya/tools/documents/archive_container_probe.py +167 -0
  43. package/src/ummaya/tools/documents/artifact_store.py +454 -0
  44. package/src/ummaya/tools/documents/authoring.py +283 -0
  45. package/src/ummaya/tools/documents/baselines.py +114 -0
  46. package/src/ummaya/tools/documents/capability.py +331 -0
  47. package/src/ummaya/tools/documents/contracts.py +112 -0
  48. package/src/ummaya/tools/documents/conversion.py +521 -0
  49. package/src/ummaya/tools/documents/diff.py +275 -0
  50. package/src/ummaya/tools/documents/engines.py +163 -0
  51. package/src/ummaya/tools/documents/evaluation.py +291 -0
  52. package/src/ummaya/tools/documents/explicit_values.py +108 -0
  53. package/src/ummaya/tools/documents/fixtures.py +174 -0
  54. package/src/ummaya/tools/documents/format_completion_audit.py +471 -0
  55. package/src/ummaya/tools/documents/formats/__init__.py +2 -0
  56. package/src/ummaya/tools/documents/formats/archive.py +528 -0
  57. package/src/ummaya/tools/documents/formats/base.py +41 -0
  58. package/src/ummaya/tools/documents/formats/code_file.py +211 -0
  59. package/src/ummaya/tools/documents/formats/data_file.py +272 -0
  60. package/src/ummaya/tools/documents/formats/hwp.py +284 -0
  61. package/src/ummaya/tools/documents/formats/hwpx.py +1837 -0
  62. package/src/ummaya/tools/documents/formats/odf.py +435 -0
  63. package/src/ummaya/tools/documents/formats/ooxml.py +1030 -0
  64. package/src/ummaya/tools/documents/formats/passive.py +766 -0
  65. package/src/ummaya/tools/documents/formats/pdf.py +702 -0
  66. package/src/ummaya/tools/documents/formats/text_web.py +268 -0
  67. package/src/ummaya/tools/documents/hwp_conversion_probe.py +178 -0
  68. package/src/ummaya/tools/documents/hwp_direct_candidate.py +141 -0
  69. package/src/ummaya/tools/documents/inspection.py +289 -0
  70. package/src/ummaya/tools/documents/intake.py +1079 -0
  71. package/src/ummaya/tools/documents/legacy_office_promotion_probe.py +366 -0
  72. package/src/ummaya/tools/documents/models.py +1598 -0
  73. package/src/ummaya/tools/documents/odf_promotion_probe.py +167 -0
  74. package/src/ummaya/tools/documents/orchestrator.py +96 -0
  75. package/src/ummaya/tools/documents/passive_capability_probe.py +251 -0
  76. package/src/ummaya/tools/documents/patch.py +170 -0
  77. package/src/ummaya/tools/documents/pdfa_conformance.py +284 -0
  78. package/src/ummaya/tools/documents/pdfa_promotion_probe.py +198 -0
  79. package/src/ummaya/tools/documents/permissions.py +110 -0
  80. package/src/ummaya/tools/documents/planner.py +616 -0
  81. package/src/ummaya/tools/documents/registry.py +2733 -0
  82. package/src/ummaya/tools/documents/render.py +978 -0
  83. package/src/ummaya/tools/documents/render_comparison.py +113 -0
  84. package/src/ummaya/tools/documents/render_comparison_models.py +74 -0
  85. package/src/ummaya/tools/documents/render_comparison_regions.py +73 -0
  86. package/src/ummaya/tools/documents/render_comparison_style.py +161 -0
  87. package/src/ummaya/tools/documents/reread.py +157 -0
  88. package/src/ummaya/tools/documents/runtime_authoring.py +244 -0
  89. package/src/ummaya/tools/documents/runtime_authoring_bundle.py +76 -0
  90. package/src/ummaya/tools/documents/scorecard.py +184 -0
  91. package/src/ummaya/tools/documents/socratic_planner.py +193 -0
  92. package/src/ummaya/tools/documents/style.py +48 -0
  93. package/src/ummaya/tools/documents/tool_defs.py +523 -0
  94. package/src/ummaya/tools/documents/validate.py +347 -0
  95. package/src/ummaya/tools/executor.py +29 -0
  96. package/src/ummaya/tools/live_proxy.py +0 -3
  97. package/src/ummaya/tools/models.py +5 -1
  98. package/src/ummaya/tools/register_all.py +8 -0
  99. package/src/ummaya/tools/registry.py +10 -1
  100. package/src/ummaya/tools/routing/__init__.py +59 -0
  101. package/src/ummaya/tools/routing/builder.py +105 -0
  102. package/src/ummaya/tools/routing/cards.py +29 -0
  103. package/src/ummaya/tools/routing/decision_service.py +534 -0
  104. package/src/ummaya/tools/routing/decision_types.py +74 -0
  105. package/src/ummaya/tools/routing/feasibility.py +122 -0
  106. package/src/ummaya/tools/routing/intent.py +17 -0
  107. package/src/ummaya/tools/routing/intent_extractor.py +207 -0
  108. package/src/ummaya/tools/routing/intent_patterns.py +160 -0
  109. package/src/ummaya/tools/routing/intent_public_data.py +150 -0
  110. package/src/ummaya/tools/routing/intent_types.py +48 -0
  111. package/src/ummaya/tools/routing/lint.py +78 -0
  112. package/src/ummaya/tools/routing/metadata.py +174 -0
  113. package/src/ummaya/tools/routing/projection.py +340 -0
  114. package/src/ummaya/tools/routing/retrieval_policy.py +629 -0
  115. package/src/ummaya/tools/routing/schema.py +81 -0
  116. package/src/ummaya/tools/routing/types.py +96 -0
  117. package/src/ummaya/tools/routing_index.py +2 -2
  118. package/src/ummaya/tools/search.py +34 -746
  119. package/tests/fixtures/documents/public_forms/baselines.yaml +113 -0
  120. package/tui/package.json +1 -1
  121. package/tui/src/.cc-byte-identical-whitelist.yaml +266 -0
  122. package/tui/src/QueryEngine.ts +12 -8
  123. package/tui/src/bridge/inboundAttachments.ts +3 -3
  124. package/tui/src/cli/handlers/auth.ts +3 -12
  125. package/tui/src/cli/print.ts +7 -7
  126. package/tui/src/commands/insights.ts +1 -1
  127. package/tui/src/commands/install-github-app/types.ts +8 -30
  128. package/tui/src/commands/plugin/types.ts +6 -28
  129. package/tui/src/commands/plugin/unifiedTypes.ts +4 -26
  130. package/tui/src/commands/rename/generateSessionName.ts +1 -1
  131. package/tui/src/components/Feedback.tsx +1 -1
  132. package/tui/src/components/LogoV2/EmergencyTip.tsx +11 -2
  133. package/tui/src/components/LogoV2/WelcomeV2.tsx +1 -3
  134. package/tui/src/components/ScrollKeybindingHandler.tsx +6 -6
  135. package/tui/src/components/Spinner/types.ts +6 -28
  136. package/tui/src/components/agents/generateAgent.ts +1 -1
  137. package/tui/src/components/agents/new-agent-creation/types.ts +4 -26
  138. package/tui/src/components/config/EnvSecretIsolatedEditor.tsx +1 -1
  139. package/tui/src/components/mcp/types.ts +16 -38
  140. package/tui/src/components/messages/AssistantToolUseMessage.tsx +3 -2
  141. package/tui/src/components/messages/UserCrossSessionMessage.ts +16 -4
  142. package/tui/src/components/messages/UserForkBoilerplateMessage.ts +16 -4
  143. package/tui/src/components/messages/UserGitHubWebhookMessage.ts +16 -4
  144. package/tui/src/components/messages/UserToolResultMessage/utils.tsx +3 -2
  145. package/tui/src/components/permissions/MonitorPermissionRequest/MonitorPermissionRequest.ts +9 -4
  146. package/tui/src/components/permissions/ReviewArtifactPermissionRequest/ReviewArtifactPermissionRequest.ts +9 -4
  147. package/tui/src/components/primitive/DocumentSocraticReviewBlock.tsx +129 -0
  148. package/tui/src/components/primitive/DocumentToolResultCard.tsx +224 -0
  149. package/tui/src/components/primitive/documentSocraticReview.ts +215 -0
  150. package/tui/src/components/primitive/index.tsx +43 -1
  151. package/tui/src/components/primitive/types.ts +137 -0
  152. package/tui/src/components/ui/option.ts +4 -26
  153. package/tui/src/constants/common.ts +0 -2
  154. package/tui/src/constants/prompts.ts +4 -3
  155. package/tui/src/constants/querySource.ts +4 -26
  156. package/tui/src/entrypoints/sdk/controlTypes.ts +26 -48
  157. package/tui/src/entrypoints/sdk/coreTypes.generated.ts +3 -25
  158. package/tui/src/entrypoints/sdk/runtimeTypes.ts +38 -60
  159. package/tui/src/entrypoints/sdk/sdkUtilityTypes.ts +4 -26
  160. package/tui/src/entrypoints/sdk/settingsTypes.generated.ts +3 -25
  161. package/tui/src/entrypoints/sdk/toolTypes.ts +3 -25
  162. package/tui/src/hooks/toolPermission/handlers/interactiveHandler.ts +10 -0
  163. package/tui/src/hooks/useApiKeyVerification.ts +1 -1
  164. package/tui/src/hooks/useVirtualScroll.ts +1 -1
  165. package/tui/src/ink/ink.tsx +33 -14
  166. package/tui/src/ink/reconciler.ts +2 -3
  167. package/tui/src/ink/render-to-screen.ts +30 -10
  168. package/tui/src/ipc/bridge.ts +62 -15
  169. package/tui/src/ipc/bridgeSingleton.ts +5 -1
  170. package/tui/src/ipc/codec.ts +3 -3
  171. package/tui/src/ipc/frames.generated.ts +12 -12
  172. package/tui/src/ipc/llmClient.ts +151 -27
  173. package/tui/src/ipc/schema/frame.schema.json +1 -1
  174. package/tui/src/keybindings/defaultBindings.ts +4 -0
  175. package/tui/src/main.tsx +29 -11
  176. package/tui/src/native-ts/file-index/index.ts +33 -3
  177. package/tui/src/observability/surface.ts +2 -2
  178. package/tui/src/probes/toolRegistryProbe.tsx +3 -1
  179. package/tui/src/projectOnboardingState.ts +7 -6
  180. package/tui/src/query/chatMessageTypes.ts +18 -0
  181. package/tui/src/query/chatMessagesBuilder.ts +1 -1
  182. package/tui/src/query/deps.ts +1 -1
  183. package/tui/src/query/messageGuards.ts +106 -0
  184. package/tui/src/query/publicDataTerminalRepair.ts +384 -0
  185. package/tui/src/query/run.ts +1075 -0
  186. package/tui/src/query/supportBoundary.ts +168 -0
  187. package/tui/src/query/toolResultErrors.ts +103 -0
  188. package/tui/src/query/toolRunner.ts +687 -0
  189. package/tui/src/query/unavailableToolRepair.ts +118 -0
  190. package/tui/src/query.ts +9 -2186
  191. package/tui/src/screens/REPL.tsx +40 -29
  192. package/tui/src/services/api/adapterManifest.ts +4 -0
  193. package/tui/src/services/api/backendChat/events.ts +117 -0
  194. package/tui/src/services/api/backendChat/finalMessage.ts +40 -0
  195. package/tui/src/services/api/backendChat/frame.ts +9 -0
  196. package/tui/src/services/api/backendChat/streaming.ts +430 -0
  197. package/tui/src/services/api/backendChat/types.ts +62 -0
  198. package/tui/src/services/api/backendChat.ts +1 -0
  199. package/tui/src/services/api/client.ts +65 -2
  200. package/tui/src/services/api/errorUtils.ts +5 -5
  201. package/tui/src/services/api/errors.ts +1 -1
  202. package/tui/src/services/api/logging.ts +1 -1
  203. package/tui/src/services/api/ummaya/evidence.ts +194 -0
  204. package/tui/src/services/api/ummaya/messages.ts +255 -0
  205. package/tui/src/services/api/ummaya/nonStreaming.ts +66 -0
  206. package/tui/src/services/api/ummaya/provider.ts +200 -0
  207. package/tui/src/services/api/ummaya/reasoning.ts +24 -0
  208. package/tui/src/services/api/ummaya/request.ts +200 -0
  209. package/tui/src/services/api/ummaya/selectionContext.ts +240 -0
  210. package/tui/src/services/api/ummaya/streaming.ts +365 -0
  211. package/tui/src/services/api/ummaya/streamingPayload.ts +129 -0
  212. package/tui/src/services/api/ummaya/streamingReader.ts +40 -0
  213. package/tui/src/services/api/ummaya/toolSelection.ts +217 -0
  214. package/tui/src/services/api/ummaya/types.ts +110 -0
  215. package/tui/src/services/api/ummaya/usage.ts +30 -0
  216. package/tui/src/services/api/ummaya.ts +26 -418
  217. package/tui/src/services/api/withRetry.ts +1 -1
  218. package/tui/src/services/awaySummary.ts +2 -2
  219. package/tui/src/services/claudeAiLimits.ts +1 -1
  220. package/tui/src/services/compact/autoCompact.ts +1 -1
  221. package/tui/src/services/compact/compact.ts +1 -1
  222. package/tui/src/services/lsp/types.ts +8 -30
  223. package/tui/src/services/tips/types.ts +6 -28
  224. package/tui/src/services/tokenEstimation.ts +1 -1
  225. package/tui/src/services/toolRegistry/bootGuard.ts +5 -5
  226. package/tui/src/services/toolUseSummary/toolUseSummaryGenerator.ts +1 -1
  227. package/tui/src/services/tools/toolExecution.ts +94 -1
  228. package/tui/src/store/pendingPermissionSlot.ts +1 -1
  229. package/tui/src/store/session-store.ts +10 -36
  230. package/tui/src/stubs/any-stub.ts +15 -10
  231. package/tui/src/stubs/color-diff-napi.ts +37 -23
  232. package/tui/src/stubs/globals.d.ts +3 -3
  233. package/tui/src/stubs/macro-preload.ts +23 -12
  234. package/tui/src/tools/AdapterTool/AdapterTool.ts +1207 -714
  235. package/tui/src/tools/AdapterTool/routeDiagnostics.ts +75 -0
  236. package/tui/src/tools/AgentTool/AgentTool.tsx +84 -1371
  237. package/tui/src/tools/AgentTool/agentToolHandoff.ts +114 -0
  238. package/tui/src/tools/AgentTool/agentToolPartialResult.ts +16 -0
  239. package/tui/src/tools/AgentTool/agentToolProgress.ts +32 -0
  240. package/tui/src/tools/AgentTool/agentToolResolver.ts +161 -0
  241. package/tui/src/tools/AgentTool/agentToolResult.ts +163 -0
  242. package/tui/src/tools/AgentTool/agentToolUtils.ts +14 -686
  243. package/tui/src/tools/AgentTool/asyncAgentLifecycle.ts +208 -0
  244. package/tui/src/tools/AgentTool/asyncLifecycle.ts +153 -0
  245. package/tui/src/tools/AgentTool/backgroundedCompletion.ts +126 -0
  246. package/tui/src/tools/AgentTool/backgroundedLifecycle.ts +174 -0
  247. package/tui/src/tools/AgentTool/foregroundBackground.ts +83 -0
  248. package/tui/src/tools/AgentTool/foregroundDrain.tsx +133 -0
  249. package/tui/src/tools/AgentTool/foregroundFinalize.ts +98 -0
  250. package/tui/src/tools/AgentTool/foregroundLifecycle.tsx +237 -0
  251. package/tui/src/tools/AgentTool/foregroundProgress.tsx +169 -0
  252. package/tui/src/tools/AgentTool/foregroundTask.ts +89 -0
  253. package/tui/src/tools/AgentTool/forkSubagent.ts +1 -12
  254. package/tui/src/tools/AgentTool/forkSubagentGate.ts +34 -0
  255. package/tui/src/tools/AgentTool/launchRouting.ts +203 -0
  256. package/tui/src/tools/AgentTool/lifecycle.ts +244 -0
  257. package/tui/src/tools/AgentTool/mcpRouting.ts +73 -0
  258. package/tui/src/tools/AgentTool/orchestrationSupport.ts +70 -0
  259. package/tui/src/tools/AgentTool/permissions.ts +39 -0
  260. package/tui/src/tools/AgentTool/promptSetup.ts +181 -0
  261. package/tui/src/tools/AgentTool/remoteRouting.ts +62 -0
  262. package/tui/src/tools/AgentTool/resultMapping.ts +116 -0
  263. package/tui/src/tools/AgentTool/resumeAgent.ts +39 -107
  264. package/tui/src/tools/AgentTool/resumeAgentHelpers.ts +140 -0
  265. package/tui/src/tools/AgentTool/runAgent.ts +1 -1
  266. package/tui/src/tools/AgentTool/runtimeConfig.ts +57 -0
  267. package/tui/src/tools/AgentTool/schemas.ts +196 -0
  268. package/tui/src/tools/AgentTool/sourceVerificationPropagation.ts +263 -0
  269. package/tui/src/tools/AgentTool/worktreeLifecycle.ts +105 -0
  270. package/tui/src/tools/AskUserQuestionTool/AskUserQuestionTool.tsx +174 -202
  271. package/tui/src/tools/BashTool/BashTool.tsx +71 -1072
  272. package/tui/src/tools/BashTool/bashCommandHelpers.ts +12 -12
  273. package/tui/src/tools/BashTool/bashPermissions/astPreflight.ts +173 -0
  274. package/tui/src/tools/BashTool/bashPermissions/classifierChecks.ts +199 -0
  275. package/tui/src/tools/BashTool/bashPermissions/compoundGuards.ts +53 -0
  276. package/tui/src/tools/BashTool/bashPermissions/constants.ts +99 -0
  277. package/tui/src/tools/BashTool/bashPermissions/index.ts +38 -0
  278. package/tui/src/tools/BashTool/bashPermissions/legacyMisparsing.ts +62 -0
  279. package/tui/src/tools/BashTool/bashPermissions/main.ts +135 -0
  280. package/tui/src/tools/BashTool/bashPermissions/normalizedCommands.ts +33 -0
  281. package/tui/src/tools/BashTool/bashPermissions/operatorFlow.ts +98 -0
  282. package/tui/src/tools/BashTool/bashPermissions/permissionChecks.ts +200 -0
  283. package/tui/src/tools/BashTool/bashPermissions/prefixSuggestions.ts +88 -0
  284. package/tui/src/tools/BashTool/bashPermissions/promptClassifierRules.ts +125 -0
  285. package/tui/src/tools/BashTool/bashPermissions/ruleDelegates.ts +19 -0
  286. package/tui/src/tools/BashTool/bashPermissions/ruleMatching.ts +145 -0
  287. package/tui/src/tools/BashTool/bashPermissions/sandboxAutoAllow.ts +75 -0
  288. package/tui/src/tools/BashTool/bashPermissions/subcommandFlow.ts +205 -0
  289. package/tui/src/tools/BashTool/bashPermissions/subcommandGuards.ts +73 -0
  290. package/tui/src/tools/BashTool/bashPermissions/subcommandResultHelpers.ts +116 -0
  291. package/tui/src/tools/BashTool/bashPermissions/types.ts +26 -0
  292. package/tui/src/tools/BashTool/bashPermissions/wrapperStripping.ts +139 -0
  293. package/tui/src/tools/BashTool/bashPermissions.ts +26 -2621
  294. package/tui/src/tools/BashTool/call.ts +202 -0
  295. package/tui/src/tools/BashTool/callLoader.ts +35 -0
  296. package/tui/src/tools/BashTool/commandClassification.ts +151 -0
  297. package/tui/src/tools/BashTool/commandClassificationLoader.ts +40 -0
  298. package/tui/src/tools/BashTool/cwdReset.ts +33 -0
  299. package/tui/src/tools/BashTool/lineTruncation.ts +11 -0
  300. package/tui/src/tools/BashTool/modeValidation.ts +13 -1
  301. package/tui/src/tools/BashTool/outputPersistence.ts +42 -0
  302. package/tui/src/tools/BashTool/permissionClassification.ts +66 -0
  303. package/tui/src/tools/BashTool/permissionLoader.ts +44 -0
  304. package/tui/src/tools/BashTool/resultLoader.ts +29 -0
  305. package/tui/src/tools/BashTool/resultMapping.ts +83 -0
  306. package/tui/src/tools/BashTool/sandboxPolicy.ts +79 -0
  307. package/tui/src/tools/BashTool/schemas.ts +65 -0
  308. package/tui/src/tools/BashTool/sedEditExecution.ts +59 -0
  309. package/tui/src/tools/BashTool/shellExecution.tsx +245 -0
  310. package/tui/src/tools/BashTool/shellOutputUtils.ts +85 -0
  311. package/tui/src/tools/BashTool/shellPermissionGauntlet.ts +97 -0
  312. package/tui/src/tools/BashTool/uiLoader.ts +37 -0
  313. package/tui/src/tools/BriefTool/upload.ts +1 -1
  314. package/tui/src/tools/CalculatorTool/parser.ts +2 -2
  315. package/tui/src/tools/DocumentPrimitive/DocumentPrimitive.ts +262 -0
  316. package/tui/src/tools/DocumentPrimitive/dispatchNormalization.ts +270 -0
  317. package/tui/src/tools/DocumentPrimitive/documentDestinationPath.ts +18 -0
  318. package/tui/src/tools/DocumentPrimitive/documentMutationGuard.ts +22 -0
  319. package/tui/src/tools/DocumentPrimitive/documentPatchNormalization.ts +248 -0
  320. package/tui/src/tools/DocumentPrimitive/documentSourceVerification.ts +245 -0
  321. package/tui/src/tools/DocumentPrimitive/documentSourceVerificationFields.ts +103 -0
  322. package/tui/src/tools/DocumentPrimitive/modelVisibleOutput.ts +40 -0
  323. package/tui/src/tools/DocumentPrimitive/prompt.ts +35 -0
  324. package/tui/src/tools/FileEditTool/FileEditTool.ts +9 -507
  325. package/tui/src/tools/FileEditTool/call.ts +228 -0
  326. package/tui/src/tools/FileEditTool/validateInput.ts +196 -0
  327. package/tui/src/tools/FileReadTool/imageProcessor.ts +13 -0
  328. package/tui/src/tools/FileWriteTool/FileWriteTool.ts +7 -300
  329. package/tui/src/tools/FileWriteTool/call.ts +223 -0
  330. package/tui/src/tools/FileWriteTool/validateInput.ts +80 -0
  331. package/tui/src/tools/ListMcpResourcesTool/ListMcpResourcesTool.ts +19 -3
  332. package/tui/src/tools/LookupPrimitive/LookupPrimitive.ts +25 -32
  333. package/tui/src/tools/LookupPrimitive/prompt.ts +0 -2
  334. package/tui/src/tools/MCPTool/trustPolicy.ts +118 -0
  335. package/tui/src/tools/McpAuthTool/McpAuthTool.ts +21 -3
  336. package/tui/src/tools/NotebookEditTool/NotebookEditTool.ts +7 -326
  337. package/tui/src/tools/NotebookEditTool/call.ts +254 -0
  338. package/tui/src/tools/NotebookEditTool/notebookModel.ts +51 -0
  339. package/tui/src/tools/NotebookEditTool/validateInput.ts +142 -0
  340. package/tui/src/tools/PowerShellTool/PowerShellTool.tsx +46 -937
  341. package/tui/src/tools/PowerShellTool/acceptEditsCommandValidation.ts +162 -0
  342. package/tui/src/tools/PowerShellTool/call.ts +179 -0
  343. package/tui/src/tools/PowerShellTool/callLoader.ts +37 -0
  344. package/tui/src/tools/PowerShellTool/commandClassification.ts +86 -0
  345. package/tui/src/tools/PowerShellTool/modeValidation.ts +25 -332
  346. package/tui/src/tools/PowerShellTool/outputPersistence.ts +42 -0
  347. package/tui/src/tools/PowerShellTool/permissionClassification.ts +28 -0
  348. package/tui/src/tools/PowerShellTool/resultLoader.ts +31 -0
  349. package/tui/src/tools/PowerShellTool/resultMapping.ts +75 -0
  350. package/tui/src/tools/PowerShellTool/schemas.ts +40 -0
  351. package/tui/src/tools/PowerShellTool/shellExecution.tsx +258 -0
  352. package/tui/src/tools/PowerShellTool/symlinkModeValidation.ts +44 -0
  353. package/tui/src/tools/PowerShellTool/uiLoader.ts +37 -0
  354. package/tui/src/tools/PowerShellTool/validation.ts +39 -0
  355. package/tui/src/tools/ReadMcpResourceTool/ReadMcpResourceTool.ts +19 -3
  356. package/tui/src/tools/ResolveLocationPrimitive/ResolveLocationPrimitive.ts +1 -11
  357. package/tui/src/tools/ResolveLocationPrimitive/prompt.ts +2 -6
  358. package/tui/src/tools/SkillTool/SkillTool.ts +2 -2
  359. package/tui/src/tools/SubmitPrimitive/SubmitPrimitive.ts +27 -10
  360. package/tui/src/tools/TaskCreateTool/TaskCreateTool.ts +16 -2
  361. package/tui/src/tools/TaskGetTool/TaskGetTool.ts +23 -3
  362. package/tui/src/tools/TaskListTool/TaskListTool.ts +22 -4
  363. package/tui/src/tools/TaskOutputTool/TaskOutputTool.tsx +46 -547
  364. package/tui/src/tools/TaskOutputTool/lookup.ts +216 -0
  365. package/tui/src/tools/TaskOutputTool/render.tsx +257 -0
  366. package/tui/src/tools/TaskOutputTool/schemas.ts +55 -0
  367. package/tui/src/tools/TaskOutputTool/serialization.ts +36 -0
  368. package/tui/src/tools/TaskStopTool/TaskStopTool.ts +10 -0
  369. package/tui/src/tools/TaskUpdateTool/TaskUpdateTool.ts +14 -364
  370. package/tui/src/tools/TaskUpdateTool/completion.ts +62 -0
  371. package/tui/src/tools/TaskUpdateTool/schemas.ts +62 -0
  372. package/tui/src/tools/TaskUpdateTool/serialization.ts +46 -0
  373. package/tui/src/tools/TaskUpdateTool/statusUpdate.ts +247 -0
  374. package/tui/src/tools/TodoWriteTool/TodoWriteTool.ts +21 -2
  375. package/tui/src/tools/ToolSearchTool/ToolSearchTool.ts +21 -302
  376. package/tui/src/tools/ToolSearchTool/ccSupportTools.ts +223 -0
  377. package/tui/src/tools/ToolSearchTool/descriptionCache.ts +50 -0
  378. package/tui/src/tools/ToolSearchTool/keywordSearch.ts +216 -0
  379. package/tui/src/tools/ToolSearchTool/prompt.ts +10 -4
  380. package/tui/src/tools/ToolSearchTool/resultMapping.ts +30 -0
  381. package/tui/src/tools/ToolSearchTool/schemas.ts +30 -0
  382. package/tui/src/tools/ToolSearchTool/searchPool.ts +47 -0
  383. package/tui/src/tools/ToolSearchTool/supportIntentHints.ts +140 -0
  384. package/tui/src/tools/TranslateTool/TranslateTool.ts +1 -1
  385. package/tui/src/tools/VerifyPrimitive/VerifyPrimitive.ts +2 -1
  386. package/tui/src/tools/WebFetchTool/WebFetchTool.ts +43 -138
  387. package/tui/src/tools/WebFetchTool/call.ts +227 -0
  388. package/tui/src/tools/WebFetchTool/resolvedAddressSafety.ts +78 -0
  389. package/tui/src/tools/WebFetchTool/sourceVerification.ts +204 -0
  390. package/tui/src/tools/WebFetchTool/types.ts +23 -0
  391. package/tui/src/tools/WebFetchTool/urlSafety.ts +181 -0
  392. package/tui/src/tools/WebFetchTool/utils.ts +1 -1
  393. package/tui/src/tools/WebSearchTool/UI.tsx +0 -1
  394. package/tui/src/tools/WebSearchTool/WebSearchTool.ts +9 -313
  395. package/tui/src/tools/WebSearchTool/call.ts +33 -0
  396. package/tui/src/tools/WebSearchTool/responseMapping.ts +190 -0
  397. package/tui/src/tools/WebSearchTool/resultBlock.ts +47 -0
  398. package/tui/src/tools/WebSearchTool/schemas.ts +47 -0
  399. package/tui/src/tools/WebSearchTool/toolSchema.ts +12 -0
  400. package/tui/src/tools/WorkspaceToolAdapter/WorkspaceToolAdapter.ts +79 -0
  401. package/tui/src/tools/WorkspaceToolAdapter/allowedRootPolicy.ts +85 -0
  402. package/tui/src/tools/WorkspaceToolAdapter/documentFormatGuards.ts +73 -0
  403. package/tui/src/tools/WorkspaceToolAdapter/inputNormalization.ts +105 -0
  404. package/tui/src/tools/WorkspaceToolAdapter/mcpExposurePolicy.ts +64 -0
  405. package/tui/src/tools/WorkspaceToolAdapter/toolDefFactory.ts +215 -0
  406. package/tui/src/tools/WorkspaceToolAdapter/toolNames.ts +6 -0
  407. package/tui/src/tools/WorkspaceToolAdapter/workspacePolicy.ts +15 -0
  408. package/tui/src/tools/_shared/dispatchPrimitive.ts +6 -6
  409. package/tui/src/tools/_shared/documentChangeToPatch.ts +125 -0
  410. package/tui/src/tools/_shared/documentDispatchArguments.ts +87 -0
  411. package/tui/src/tools/_shared/documentPrimitiveTimeout.ts +13 -0
  412. package/tui/src/tools/_shared/documentToolResultRender.ts +98 -0
  413. package/tui/src/tools/_shared/pendingCallRegistry.ts +1 -6
  414. package/tui/src/tools/_shared/rootPrimitiveInput.ts +1 -0
  415. package/tui/src/tools/_shared/toolChoiceRepair/documentCompletionPatterns.ts +58 -0
  416. package/tui/src/tools/_shared/toolChoiceRepair/documentCompletionPrompt.ts +271 -0
  417. package/tui/src/tools/_shared/toolChoiceRepair/documentRepair.ts +452 -0
  418. package/tui/src/tools/_shared/toolChoiceRepair/messageAccess.ts +80 -0
  419. package/tui/src/tools/_shared/toolChoiceRepair/publicDataRepair.ts +92 -0
  420. package/tui/src/tools/_shared/toolChoiceRepair/supportRepair.ts +135 -0
  421. package/tui/src/tools/_shared/toolChoiceRepair.ts +55 -860
  422. package/tui/src/tools/shared/mockDisclaimer.ts +1 -1
  423. package/tui/src/tools.ts +39 -190
  424. package/tui/src/types/fileSuggestion.ts +4 -26
  425. package/tui/src/types/generated/events_mono/claude_code/v1/claude_code_internal_event.ts +186 -148
  426. package/tui/src/types/generated/events_mono/common/v1/auth.ts +25 -11
  427. package/tui/src/types/generated/events_mono/growthbook/v1/growthbook_experiment_event.ts +47 -30
  428. package/tui/src/types/generated/google/protobuf/timestamp.ts +21 -7
  429. package/tui/src/types/message.ts +80 -102
  430. package/tui/src/types/messageQueueTypes.ts +6 -28
  431. package/tui/src/types/notebook.ts +16 -38
  432. package/tui/src/types/statusLine.ts +4 -26
  433. package/tui/src/types/tools.ts +24 -46
  434. package/tui/src/types/utils.ts +6 -28
  435. package/tui/src/upstreamproxy/relay.ts +7 -3
  436. package/tui/src/upstreamproxy/upstreamproxy.ts +1 -1
  437. package/tui/src/utils/assistantMessageFactories.ts +9 -3
  438. package/tui/src/utils/auth.ts +129 -139
  439. package/tui/src/utils/bash/ast.ts +23 -23
  440. package/tui/src/utils/bash/bashParser.ts +5 -5
  441. package/tui/src/utils/billing.ts +1 -1
  442. package/tui/src/utils/collapseReadSearch.ts +3 -3
  443. package/tui/src/utils/cronTasks.ts +1 -1
  444. package/tui/src/utils/execFileNoThrow.ts +1 -1
  445. package/tui/src/utils/filePersistence/types.ts +16 -38
  446. package/tui/src/utils/forkedAgent.ts +1 -1
  447. package/tui/src/utils/gracefulShutdown.ts +4 -4
  448. package/tui/src/utils/heapDumpService.ts +12 -8
  449. package/tui/src/utils/hooks/apiQueryHookHelper.ts +1 -1
  450. package/tui/src/utils/hooks/execPromptHook.ts +1 -1
  451. package/tui/src/utils/hooks/skillImprovement.ts +1 -1
  452. package/tui/src/utils/mcp/dateTimeParser.ts +1 -1
  453. package/tui/src/utils/messages.ts +18 -0
  454. package/tui/src/utils/migrateSessions.ts +3 -3
  455. package/tui/src/utils/model/model.ts +6 -6
  456. package/tui/src/utils/permissions/yoloClassifier.ts +1 -1
  457. package/tui/src/utils/plugins/headlessPluginInstall.ts +1 -1
  458. package/tui/src/utils/plugins/mcpPluginIntegration.ts +1 -1
  459. package/tui/src/utils/plugins/mcpbHandler.ts +1 -1
  460. package/tui/src/utils/plugins/pluginLoader.ts +8 -8
  461. package/tui/src/utils/protectedNamespace.ts +5 -3
  462. package/tui/src/utils/rawJsonToolCall.ts +242 -0
  463. package/tui/src/utils/ripgrep.ts +16 -7
  464. package/tui/src/utils/sessionTitle.ts +1 -1
  465. package/tui/src/utils/settings/permissionValidation.ts +14 -2
  466. package/tui/src/utils/shell/prefix.ts +1 -1
  467. package/tui/src/utils/sideQuery.ts +1 -1
  468. package/tui/src/utils/systemThemeWatcher.ts +13 -3
  469. package/tui/src/utils/teleport.tsx +1 -1
  470. package/uv.lock +400 -14
  471. package/tui/src/services/api/claude.ts +0 -3540
  472. package/tui/src/tools/_shared/directPublicDataGuard.ts +0 -362
  473. package/tui/src/tools/_shared/kmaAnalysisGuard.ts +0 -197
  474. package/tui/src/tools/_shared/kmaAviationGuard.ts +0 -70
  475. package/tui/src/tools/_shared/nmcAedGuard.ts +0 -234
  476. package/tui/src/tools/_shared/protectedCheckGuard.ts +0 -207
  477. package/tui/src/tools/_shared/textToolCallGuard.ts +0 -91
package/tui/src/query.ts CHANGED
@@ -1,215 +1,17 @@
1
- // biome-ignore-all assist/source/organizeImports: ANT-ONLY import markers must not be reordered
2
- import type {
3
- ToolResultBlockParam,
4
- ToolUseBlock,
5
- } from '@anthropic-ai/sdk/resources/index.mjs'
6
1
  import type { CanUseToolFn } from './hooks/useCanUseTool.js'
7
- import { FallbackTriggeredError } from './services/api/withRetry.js'
8
- import {
9
- calculateTokenWarningState,
10
- isAutoCompactEnabled,
11
- type AutoCompactTrackingState,
12
- } from './services/compact/autoCompact.js'
13
- import { buildPostCompactMessages } from './services/compact/compact.js'
14
- /* eslint-disable @typescript-eslint/no-require-imports */
15
- const reactiveCompact = feature('REACTIVE_COMPACT')
16
- ? (require('./services/compact/reactiveCompact.js') as typeof import('./services/compact/reactiveCompact.js'))
17
- : null
18
- const contextCollapse = feature('CONTEXT_COLLAPSE')
19
- ? (require('./services/contextCollapse/index.js') as typeof import('./services/contextCollapse/index.js'))
20
- : null
21
- /* eslint-enable @typescript-eslint/no-require-imports */
22
- import {
23
- logEvent,
24
- type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
25
- } from 'src/services/analytics/index.js'
26
- import { ImageSizeError } from './utils/imageValidation.js'
27
- import { ImageResizeError } from './utils/imageResizer.js'
28
- import { findToolByName, type ToolUseContext } from './Tool.js'
29
- import { asSystemPrompt, type SystemPrompt } from './utils/systemPromptType.js'
2
+ import type { QuerySource } from './constants/querySource.js'
3
+ import type { QueryDeps } from './query/deps.js'
4
+ import type { ToolUseContext } from './Tool.js'
5
+ import type { SystemPrompt } from './utils/systemPromptType.js'
30
6
  import type {
31
- AssistantMessage,
32
- AttachmentMessage,
33
7
  Message,
34
8
  RequestStartEvent,
35
9
  StreamEvent,
36
10
  ToolUseSummaryMessage,
37
- UserMessage,
38
11
  TombstoneMessage,
39
12
  } from './types/message.js'
40
- import { logError } from './utils/log.js'
41
- import {
42
- PROMPT_TOO_LONG_ERROR_MESSAGE,
43
- isPromptTooLongMessage,
44
- } from './services/api/errors.js'
45
- import { logAntError, logForDebugging } from './utils/debug.js'
46
- import {
47
- createUserMessage,
48
- createUserInterruptionMessage,
49
- normalizeMessagesForAPI,
50
- createSystemMessage,
51
- createAssistantAPIErrorMessage,
52
- getMessagesAfterCompactBoundary,
53
- createToolUseSummaryMessage,
54
- createMicrocompactBoundaryMessage,
55
- stripSignatureBlocks,
56
- } from './utils/messages.js'
57
- import { generateToolUseSummary } from './services/toolUseSummary/toolUseSummaryGenerator.js'
58
- import { prependUserContext, appendSystemContext } from './utils/api.js'
59
- import {
60
- createAttachmentMessage,
61
- filterDuplicateMemoryAttachments,
62
- getAttachmentMessages,
63
- startRelevantMemoryPrefetch,
64
- } from './utils/attachments.js'
65
- /* eslint-disable @typescript-eslint/no-require-imports */
66
- const skillPrefetch = feature('EXPERIMENTAL_SKILL_SEARCH')
67
- ? (require('./services/skillSearch/prefetch.js') as typeof import('./services/skillSearch/prefetch.js'))
68
- : null
69
- const jobClassifier = feature('TEMPLATES')
70
- ? (require('./jobs/classifier.js') as typeof import('./jobs/classifier.js'))
71
- : null
72
- /* eslint-enable @typescript-eslint/no-require-imports */
73
- import {
74
- remove as removeFromQueue,
75
- getCommandsByMaxPriority,
76
- isSlashCommand,
77
- } from './utils/messageQueueManager.js'
78
- import { notifyCommandLifecycle } from './utils/commandLifecycle.js'
79
- import { headlessProfilerCheckpoint } from './utils/headlessProfiler.js'
80
- import {
81
- getRuntimeMainLoopModel,
82
- renderModelName,
83
- } from './utils/model/model.js'
84
- import {
85
- doesMostRecentAssistantMessageExceed200k,
86
- finalContextTokensFromLastResponse,
87
- tokenCountWithEstimation,
88
- } from './utils/tokens.js'
89
- import { ESCALATED_MAX_TOKENS } from './utils/context.js'
90
- import { getFeatureValue_CACHED_MAY_BE_STALE } from './services/analytics/growthbook.js'
91
- import { SLEEP_TOOL_NAME } from './tools/SleepTool/prompt.js'
92
- import { executePostSamplingHooks } from './utils/hooks/postSamplingHooks.js'
93
- import { executeStopFailureHooks } from './utils/hooks.js'
94
- import type { QuerySource } from './constants/querySource.js'
95
- import { createDumpPromptsFetch } from './services/api/dumpPrompts.js'
96
- import { StreamingToolExecutor } from './services/tools/StreamingToolExecutor.js'
97
- import { queryCheckpoint } from './utils/queryProfiler.js'
98
- import { runTools } from './services/tools/toolOrchestration.js'
99
- import { applyToolResultBudget } from './utils/toolResultStorage.js'
100
- import { recordContentReplacement } from './utils/sessionStorage.js'
101
- import { handleStopHooks } from './query/stopHooks.js'
102
- import {
103
- buildNmcAedCompletionPromptIfNeeded,
104
- buildNmcAedFollowupPromptIfNeeded,
105
- } from './tools/_shared/nmcAedGuard.js'
106
- import {
107
- buildKmaAnalysisCompletionPromptIfNeeded,
108
- buildKmaAnalysisFinalAnswerRepairPromptIfNeeded,
109
- buildKmaAnalysisMissingToolPromptIfNeeded,
110
- shouldWithholdKmaAnalysisToolCallText,
111
- } from './tools/_shared/kmaAnalysisGuard.js'
112
- import {
113
- buildProtectedCheckCompletionPromptIfNeeded,
114
- buildProtectedCheckFinalAnswerRepairPromptIfNeeded,
115
- shouldWithholdProtectedCheckToolCallText,
116
- } from './tools/_shared/protectedCheckGuard.js'
117
- import {
118
- buildAirKoreaCompletionPromptIfNeeded,
119
- buildAirKoreaFinalAnswerRepairPromptIfNeeded,
120
- buildGenericPendingFinalAnswerRepairPromptIfNeeded,
121
- buildTagoBusCompletionPromptIfNeeded,
122
- buildTagoBusFinalAnswerRepairPromptIfNeeded,
123
- buildTagoBusFollowupPromptIfNeeded,
124
- selectUmmayaToolChoiceOverride,
125
- shouldWithholdAirKoreaFinalAnswer,
126
- shouldWithholdGenericPendingFinalAnswer,
127
- shouldWithholdTagoBusFinalAnswer,
128
- } from './tools/_shared/toolChoiceRepair.js'
129
- import {
130
- buildTextToolCallFinalAnswerRepairPromptIfNeeded,
131
- shouldWithholdTextToolCallFinalAnswer,
132
- } from './tools/_shared/textToolCallGuard.js'
133
- import { getAdapterToolByName } from './tools/AdapterTool/AdapterTool.js'
134
- import { buildQueryConfig } from './query/config.js'
135
- import { productionDeps, type QueryDeps } from './query/deps.js'
136
- import { ensureUmmayaAdapterManifest } from './ipc/bridgeSingleton.js'
137
- import type { Terminal, Continue } from './query/transitions.js'
138
- import { feature } from 'bun:bundle'
139
- import {
140
- getCurrentTurnTokenBudget,
141
- getTurnOutputTokens,
142
- incrementBudgetContinuationCount,
143
- } from './bootstrap/state.js'
144
- import { createBudgetTracker, checkTokenBudget } from './query/tokenBudget.js'
145
- import { count } from './utils/array.js'
146
-
147
- /* eslint-disable @typescript-eslint/no-require-imports */
148
- const snipModule = feature('HISTORY_SNIP')
149
- ? (require('./services/compact/snipCompact.js') as typeof import('./services/compact/snipCompact.js'))
150
- : null
151
- const taskSummaryModule = feature('BG_SESSIONS')
152
- ? (require('./utils/taskSummary.js') as typeof import('./utils/taskSummary.js'))
153
- : null
154
- /* eslint-enable @typescript-eslint/no-require-imports */
155
-
156
- function* yieldMissingToolResultBlocks(
157
- assistantMessages: AssistantMessage[],
158
- errorMessage: string,
159
- ) {
160
- for (const assistantMessage of assistantMessages) {
161
- // Extract all tool use blocks from this assistant message
162
- const toolUseBlocks = assistantMessage.message.content.filter(
163
- content => content.type === 'tool_use',
164
- ) as ToolUseBlock[]
165
-
166
- // Emit an interruption message for each tool use
167
- for (const toolUse of toolUseBlocks) {
168
- yield createUserMessage({
169
- content: [
170
- {
171
- type: 'tool_result',
172
- content: errorMessage,
173
- is_error: true,
174
- tool_use_id: toolUse.id,
175
- },
176
- ],
177
- toolUseResult: errorMessage,
178
- sourceToolAssistantUUID: assistantMessage.uuid,
179
- })
180
- }
181
- }
182
- }
183
-
184
- /**
185
- * The rules of thinking are lengthy and fortuitous. They require plenty of thinking
186
- * of most long duration and deep meditation for a wizard to wrap one's noggin around.
187
- *
188
- * The rules follow:
189
- * 1. A message that contains a thinking or redacted_thinking block must be part of a query whose max_thinking_length > 0
190
- * 2. A thinking block may not be the last message in a block
191
- * 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)
192
- *
193
- * Heed these rules well, young wizard. For they are the rules of thinking, and
194
- * the rules of thinking are the rules of the universe. If ye does not heed these
195
- * rules, ye will be punished with an entire day of debugging and hair pulling.
196
- */
197
- const MAX_OUTPUT_TOKENS_RECOVERY_LIMIT = 3
198
-
199
- /**
200
- * Is this a max_output_tokens error message? If so, the streaming loop should
201
- * withhold it from SDK callers until we know whether the recovery loop can
202
- * continue. Yielding early leaks an intermediate error to SDK callers (e.g.
203
- * cowork/desktop) that terminate the session on any `error` field — the
204
- * recovery loop keeps running but nobody is listening.
205
- *
206
- * Mirrors reactiveCompact.isWithheldPromptTooLong.
207
- */
208
- function isWithheldMaxOutputTokens(
209
- msg: Message | StreamEvent | undefined,
210
- ): msg is AssistantMessage {
211
- return msg?.type === 'assistant' && msg.apiError === 'max_output_tokens'
212
- }
13
+ import type { Terminal } from './query/transitions.js'
14
+ import { query } from './query/run.js'
213
15
 
214
16
  export type QueryParams = {
215
17
  messages: Message[]
@@ -223,1995 +25,16 @@ export type QueryParams = {
223
25
  maxOutputTokensOverride?: number
224
26
  maxTurns?: number
225
27
  skipCacheWrite?: boolean
226
- // API task_budget (output_config.task_budget, beta task-budgets-2026-03-13).
227
- // Distinct from the tokenBudget +500k auto-continue feature. `total` is the
228
- // budget for the whole agentic turn; `remaining` is computed per iteration
229
- // from cumulative API usage. See configureTaskBudgetParams in claude.ts.
230
28
  taskBudget?: { total: number }
231
29
  deps?: QueryDeps
232
30
  }
233
31
 
234
- // -- query loop state
235
-
236
- // Mutable state carried between loop iterations
237
- type State = {
238
- messages: Message[]
239
- toolUseContext: ToolUseContext
240
- autoCompactTracking: AutoCompactTrackingState | undefined
241
- maxOutputTokensRecoveryCount: number
242
- hasAttemptedReactiveCompact: boolean
243
- maxOutputTokensOverride: number | undefined
244
- pendingToolUseSummary: Promise<ToolUseSummaryMessage | null> | undefined
245
- stopHookActive: boolean | undefined
246
- turnCount: number
247
- // Why the previous iteration continued. Undefined on first iteration.
248
- // Lets tests assert recovery paths fired without inspecting message contents.
249
- transition: Continue | undefined
250
- }
251
-
252
- export async function* query(
253
- params: QueryParams,
254
- ): AsyncGenerator<
32
+ export { query }
33
+ export type QueryGenerator = AsyncGenerator<
255
34
  | StreamEvent
256
35
  | RequestStartEvent
257
36
  | Message
258
37
  | TombstoneMessage
259
38
  | ToolUseSummaryMessage,
260
39
  Terminal
261
- > {
262
- const consumedCommandUuids: string[] = []
263
- const terminal = yield* queryLoop(params, consumedCommandUuids)
264
- // Only reached if queryLoop returned normally. Skipped on throw (error
265
- // propagates through yield*) and on .return() (Return completion closes
266
- // both generators). This gives the same asymmetric started-without-completed
267
- // signal as print.ts's drainCommandQueue when the turn fails.
268
- for (const uuid of consumedCommandUuids) {
269
- notifyCommandLifecycle(uuid, 'completed')
270
- }
271
- return terminal
272
- }
273
-
274
- async function* queryLoop(
275
- params: QueryParams,
276
- consumedCommandUuids: string[],
277
- ): AsyncGenerator<
278
- | StreamEvent
279
- | RequestStartEvent
280
- | Message
281
- | TombstoneMessage
282
- | ToolUseSummaryMessage,
283
- Terminal
284
- > {
285
- // Immutable params — never reassigned during the query loop.
286
- const {
287
- systemPrompt,
288
- userContext,
289
- systemContext,
290
- canUseTool,
291
- fallbackModel,
292
- querySource,
293
- maxTurns,
294
- skipCacheWrite,
295
- } = params
296
- const deps = params.deps ?? productionDeps()
297
-
298
- // Mutable cross-iteration state. The loop body destructures this at the top
299
- // of each iteration so reads stay bare-name (`messages`, `toolUseContext`).
300
- // Continue sites write `state = { ... }` instead of 9 separate assignments.
301
- let state: State = {
302
- messages: params.messages,
303
- toolUseContext: params.toolUseContext,
304
- maxOutputTokensOverride: params.maxOutputTokensOverride,
305
- autoCompactTracking: undefined,
306
- stopHookActive: undefined,
307
- maxOutputTokensRecoveryCount: 0,
308
- hasAttemptedReactiveCompact: false,
309
- turnCount: 1,
310
- pendingToolUseSummary: undefined,
311
- transition: undefined,
312
- }
313
- const budgetTracker = feature('TOKEN_BUDGET') ? createBudgetTracker() : null
314
-
315
- if (
316
- params.deps === undefined &&
317
- process.env.UMMAYA_SKIP_ADAPTER_MANIFEST_BOOTSTRAP !== 'true'
318
- ) {
319
- const manifestSynced = await ensureUmmayaAdapterManifest(10_000)
320
- if (manifestSynced && state.toolUseContext.options.refreshTools) {
321
- const refreshedTools = state.toolUseContext.options.refreshTools()
322
- if (refreshedTools !== state.toolUseContext.options.tools) {
323
- state = {
324
- ...state,
325
- toolUseContext: {
326
- ...state.toolUseContext,
327
- options: {
328
- ...state.toolUseContext.options,
329
- tools: refreshedTools,
330
- },
331
- },
332
- }
333
- }
334
- }
335
- }
336
-
337
- // task_budget.remaining tracking across compaction boundaries. Undefined
338
- // until first compact fires — while context is uncompacted the server can
339
- // see the full history and handles the countdown from {total} itself (see
340
- // api/api/sampling/prompt/renderer.py:292). After a compact, the server sees
341
- // only the summary and would under-count spend; remaining tells it the
342
- // pre-compact final window that got summarized away. Cumulative across
343
- // multiple compacts: each subtracts the final context at that compact's
344
- // trigger point. Loop-local (not on State) to avoid touching the 7 continue
345
- // sites.
346
- let taskBudgetRemaining: number | undefined = undefined
347
-
348
- // Snapshot immutable env/statsig/session state once at entry. See QueryConfig
349
- // for what's included and why feature() gates are intentionally excluded.
350
- const config = buildQueryConfig()
351
-
352
- // Fired once per user turn — the prompt is invariant across loop iterations,
353
- // so per-iteration firing would ask sideQuery the same question N times.
354
- // Consume point polls settledAt (never blocks). `using` disposes on all
355
- // generator exit paths — see MemoryPrefetch for dispose/telemetry semantics.
356
- using pendingMemoryPrefetch = startRelevantMemoryPrefetch(
357
- state.messages,
358
- state.toolUseContext,
359
- )
360
-
361
- // eslint-disable-next-line no-constant-condition
362
- while (true) {
363
- // Destructure state at the top of each iteration. toolUseContext alone
364
- // is reassigned within an iteration (queryTracking, messages updates);
365
- // the rest are read-only between continue sites.
366
- let { toolUseContext } = state
367
- const {
368
- messages,
369
- autoCompactTracking,
370
- maxOutputTokensRecoveryCount,
371
- hasAttemptedReactiveCompact,
372
- maxOutputTokensOverride,
373
- pendingToolUseSummary,
374
- stopHookActive,
375
- turnCount,
376
- } = state
377
-
378
- // Skill discovery prefetch — per-iteration (uses findWritePivot guard
379
- // that returns early on non-write iterations). Discovery runs while the
380
- // model streams and tools execute; awaited post-tools alongside the
381
- // memory prefetch consume. Replaces the blocking assistant_turn path
382
- // that ran inside getAttachmentMessages (97% of those calls found
383
- // nothing in prod). Turn-0 user-input discovery still blocks in
384
- // userInputAttachments — that's the one signal where there's no prior
385
- // work to hide under.
386
- const pendingSkillPrefetch = skillPrefetch?.startSkillDiscoveryPrefetch(
387
- null,
388
- messages,
389
- toolUseContext,
390
- )
391
-
392
- yield { type: 'stream_request_start' }
393
-
394
- queryCheckpoint('query_fn_entry')
395
-
396
- // Record query start for headless latency tracking (skip for subagents)
397
- if (!toolUseContext.agentId) {
398
- headlessProfilerCheckpoint('query_started')
399
- }
400
-
401
- // Initialize or increment query chain tracking
402
- const queryTracking = toolUseContext.queryTracking
403
- ? {
404
- chainId: toolUseContext.queryTracking.chainId,
405
- depth: toolUseContext.queryTracking.depth + 1,
406
- }
407
- : {
408
- chainId: deps.uuid(),
409
- depth: 0,
410
- }
411
-
412
- const queryChainIdForAnalytics =
413
- queryTracking.chainId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS
414
-
415
- toolUseContext = {
416
- ...toolUseContext,
417
- queryTracking,
418
- }
419
-
420
- let messagesForQuery = [...getMessagesAfterCompactBoundary(messages)]
421
-
422
- let tracking = autoCompactTracking
423
-
424
- // Enforce per-message budget on aggregate tool result size. Runs BEFORE
425
- // microcompact — cached MC operates purely by tool_use_id (never inspects
426
- // content), so content replacement is invisible to it and the two compose
427
- // cleanly. No-ops when contentReplacementState is undefined (feature off).
428
- // Persist only for querySources that read records back on resume: agentId
429
- // routes to sidechain file (AgentTool resume) or session file (/resume).
430
- // Ephemeral runForkedAgent callers (agent_summary etc.) don't persist.
431
- const persistReplacements =
432
- querySource.startsWith('agent:') ||
433
- querySource.startsWith('repl_main_thread')
434
- messagesForQuery = await applyToolResultBudget(
435
- messagesForQuery,
436
- toolUseContext.contentReplacementState,
437
- persistReplacements
438
- ? records =>
439
- void recordContentReplacement(
440
- records,
441
- toolUseContext.agentId,
442
- ).catch(logError)
443
- : undefined,
444
- new Set(
445
- toolUseContext.options.tools
446
- .filter(t => !Number.isFinite(t.maxResultSizeChars))
447
- .map(t => t.name),
448
- ),
449
- )
450
-
451
- // Apply snip before microcompact (both may run — they are not mutually exclusive).
452
- // snipTokensFreed is plumbed to autocompact so its threshold check reflects
453
- // what snip removed; tokenCountWithEstimation alone can't see it (reads usage
454
- // from the protected-tail assistant, which survives snip unchanged).
455
- let snipTokensFreed = 0
456
- if (feature('HISTORY_SNIP')) {
457
- queryCheckpoint('query_snip_start')
458
- const snipResult = snipModule!.snipCompactIfNeeded(messagesForQuery)
459
- messagesForQuery = snipResult.messages
460
- snipTokensFreed = snipResult.tokensFreed
461
- if (snipResult.boundaryMessage) {
462
- yield snipResult.boundaryMessage
463
- }
464
- queryCheckpoint('query_snip_end')
465
- }
466
-
467
- // Apply microcompact before autocompact
468
- queryCheckpoint('query_microcompact_start')
469
- const microcompactResult = await deps.microcompact(
470
- messagesForQuery,
471
- toolUseContext,
472
- querySource,
473
- )
474
- messagesForQuery = microcompactResult.messages
475
- // For cached microcompact (cache editing), defer boundary message until after
476
- // the API response so we can use actual cache_deleted_input_tokens.
477
- // Gated behind feature() so the string is eliminated from external builds.
478
- const pendingCacheEdits = feature('CACHED_MICROCOMPACT')
479
- ? microcompactResult.compactionInfo?.pendingCacheEdits
480
- : undefined
481
- queryCheckpoint('query_microcompact_end')
482
-
483
- // Project the collapsed context view and maybe commit more collapses.
484
- // Runs BEFORE autocompact so that if collapse gets us under the
485
- // autocompact threshold, autocompact is a no-op and we keep granular
486
- // context instead of a single summary.
487
- //
488
- // Nothing is yielded — the collapsed view is a read-time projection
489
- // over the REPL's full history. Summary messages live in the collapse
490
- // store, not the REPL array. This is what makes collapses persist
491
- // across turns: projectView() replays the commit log on every entry.
492
- // Within a turn, the view flows forward via state.messages at the
493
- // continue site (query.ts:1192), and the next projectView() no-ops
494
- // because the archived messages are already gone from its input.
495
- if (feature('CONTEXT_COLLAPSE') && contextCollapse) {
496
- const collapseResult = await contextCollapse.applyCollapsesIfNeeded(
497
- messagesForQuery,
498
- toolUseContext,
499
- querySource,
500
- )
501
- messagesForQuery = collapseResult.messages
502
- }
503
-
504
- const fullSystemPrompt = asSystemPrompt(
505
- appendSystemContext(systemPrompt, systemContext),
506
- )
507
-
508
- queryCheckpoint('query_autocompact_start')
509
- const { compactionResult, consecutiveFailures } = await deps.autocompact(
510
- messagesForQuery,
511
- toolUseContext,
512
- {
513
- systemPrompt,
514
- userContext,
515
- systemContext,
516
- toolUseContext,
517
- forkContextMessages: messagesForQuery,
518
- },
519
- querySource,
520
- tracking,
521
- snipTokensFreed,
522
- )
523
- queryCheckpoint('query_autocompact_end')
524
-
525
- if (compactionResult) {
526
- const {
527
- preCompactTokenCount,
528
- postCompactTokenCount,
529
- truePostCompactTokenCount,
530
- compactionUsage,
531
- } = compactionResult
532
-
533
- logEvent('tengu_auto_compact_succeeded', {
534
- originalMessageCount: messages.length,
535
- compactedMessageCount:
536
- compactionResult.summaryMessages.length +
537
- compactionResult.attachments.length +
538
- compactionResult.hookResults.length,
539
- preCompactTokenCount,
540
- postCompactTokenCount,
541
- truePostCompactTokenCount,
542
- compactionInputTokens: compactionUsage?.input_tokens,
543
- compactionOutputTokens: compactionUsage?.output_tokens,
544
- compactionCacheReadTokens:
545
- compactionUsage?.cache_read_input_tokens ?? 0,
546
- compactionCacheCreationTokens:
547
- compactionUsage?.cache_creation_input_tokens ?? 0,
548
- compactionTotalTokens: compactionUsage
549
- ? compactionUsage.input_tokens +
550
- (compactionUsage.cache_creation_input_tokens ?? 0) +
551
- (compactionUsage.cache_read_input_tokens ?? 0) +
552
- compactionUsage.output_tokens
553
- : 0,
554
-
555
- queryChainId: queryChainIdForAnalytics,
556
- queryDepth: queryTracking.depth,
557
- })
558
-
559
- // task_budget: capture pre-compact final context window before
560
- // messagesForQuery is replaced with postCompactMessages below.
561
- // iterations[-1] is the authoritative final window (post server tool
562
- // loops); see #304930.
563
- if (params.taskBudget) {
564
- const preCompactContext =
565
- finalContextTokensFromLastResponse(messagesForQuery)
566
- taskBudgetRemaining = Math.max(
567
- 0,
568
- (taskBudgetRemaining ?? params.taskBudget.total) - preCompactContext,
569
- )
570
- }
571
-
572
- // Reset on every compact so turnCounter/turnId reflect the MOST RECENT
573
- // compact. recompactionInfo (autoCompact.ts:190) already captured the
574
- // old values for turnsSincePreviousCompact/previousCompactTurnId before
575
- // the call, so this reset doesn't lose those.
576
- tracking = {
577
- compacted: true,
578
- turnId: deps.uuid(),
579
- turnCounter: 0,
580
- consecutiveFailures: 0,
581
- }
582
-
583
- const postCompactMessages = buildPostCompactMessages(compactionResult)
584
-
585
- for (const message of postCompactMessages) {
586
- yield message
587
- }
588
-
589
- // Continue on with the current query call using the post compact messages
590
- messagesForQuery = postCompactMessages
591
- } else if (consecutiveFailures !== undefined) {
592
- // Autocompact failed — propagate failure count so the circuit breaker
593
- // can stop retrying on the next iteration.
594
- tracking = {
595
- ...(tracking ?? { compacted: false, turnId: '', turnCounter: 0 }),
596
- consecutiveFailures,
597
- }
598
- }
599
-
600
- //TODO: no need to set toolUseContext.messages during set-up since it is updated here
601
- toolUseContext = {
602
- ...toolUseContext,
603
- messages: messagesForQuery,
604
- }
605
-
606
- const assistantMessages: AssistantMessage[] = []
607
- const toolResults: (UserMessage | AttachmentMessage)[] = []
608
- // @see https://docs.claude.com/en/docs/build-with-claude/tool-use
609
- // Note: stop_reason === 'tool_use' is unreliable -- it's not always set correctly.
610
- // Set during streaming whenever a tool_use block arrives — the sole
611
- // loop-exit signal. If false after streaming, we're done (modulo stop-hook retry).
612
- const toolUseBlocks: ToolUseBlock[] = []
613
- let needsFollowUp = false
614
-
615
- queryCheckpoint('query_setup_start')
616
- const useStreamingToolExecution = config.gates.streamingToolExecution
617
- let streamingToolExecutor = useStreamingToolExecution
618
- ? new StreamingToolExecutor(
619
- toolUseContext.options.tools,
620
- canUseTool,
621
- toolUseContext,
622
- )
623
- : null
624
-
625
- const appState = toolUseContext.getAppState()
626
- const permissionMode = appState.toolPermissionContext.mode
627
- let currentModel = getRuntimeMainLoopModel({
628
- permissionMode,
629
- mainLoopModel: toolUseContext.options.mainLoopModel,
630
- exceeds200kTokens:
631
- permissionMode === 'plan' &&
632
- doesMostRecentAssistantMessageExceed200k(messagesForQuery),
633
- })
634
-
635
- queryCheckpoint('query_setup_end')
636
-
637
- // Create fetch wrapper once per query session to avoid memory retention.
638
- // Each call to createDumpPromptsFetch creates a closure that captures the request body.
639
- // Creating it once means only the latest request body is retained (~700KB),
640
- // instead of all request bodies from the session (~500MB for long sessions).
641
- // Note: agentId is effectively constant during a query() call - it only changes
642
- // between queries (e.g., /clear command or session resume).
643
- const dumpPromptsFetch = config.gates.isAnt
644
- ? createDumpPromptsFetch(toolUseContext.agentId ?? config.sessionId)
645
- : undefined
646
-
647
- // Block if we've hit the hard blocking limit (only applies when auto-compact is OFF)
648
- // This reserves space so users can still run /compact manually
649
- // Skip this check if compaction just happened - the compaction result is already
650
- // validated to be under the threshold, and tokenCountWithEstimation would use
651
- // stale input_tokens from kept messages that reflect pre-compaction context size.
652
- // Same staleness applies to snip: subtract snipTokensFreed (otherwise we'd
653
- // falsely block in the window where snip brought us under autocompact threshold
654
- // but the stale usage is still above blocking limit — before this PR that
655
- // window never existed because autocompact always fired on the stale count).
656
- // Also skip for compact/session_memory queries — these are forked agents that
657
- // inherit the full conversation and would deadlock if blocked here (the compact
658
- // agent needs to run to REDUCE the token count).
659
- // Also skip when reactive compact is enabled and automatic compaction is
660
- // allowed — the preempt's synthetic error returns before the API call,
661
- // so reactive compact would never see a prompt-too-long to react to.
662
- // Widened to walrus so RC can act as fallback when proactive fails.
663
- //
664
- // Same skip for context-collapse: its recoverFromOverflow drains
665
- // staged collapses on a REAL API 413, then falls through to
666
- // reactiveCompact. A synthetic preempt here would return before the
667
- // API call and starve both recovery paths. The isAutoCompactEnabled()
668
- // conjunct preserves the user's explicit "no automatic anything"
669
- // config — if they set DISABLE_AUTO_COMPACT, they get the preempt.
670
- let collapseOwnsIt = false
671
- if (feature('CONTEXT_COLLAPSE')) {
672
- collapseOwnsIt =
673
- (contextCollapse?.isContextCollapseEnabled() ?? false) &&
674
- isAutoCompactEnabled()
675
- }
676
- // Hoist media-recovery gate once per turn. Withholding (inside the
677
- // stream loop) and recovery (after) must agree; CACHED_MAY_BE_STALE can
678
- // flip during the 5-30s stream, and withhold-without-recover would eat
679
- // the message. PTL doesn't hoist because its withholding is ungated —
680
- // it predates the experiment and is already the control-arm baseline.
681
- const mediaRecoveryEnabled =
682
- reactiveCompact?.isReactiveCompactEnabled() ?? false
683
- if (
684
- !compactionResult &&
685
- querySource !== 'compact' &&
686
- querySource !== 'session_memory' &&
687
- !(
688
- reactiveCompact?.isReactiveCompactEnabled() && isAutoCompactEnabled()
689
- ) &&
690
- !collapseOwnsIt
691
- ) {
692
- const { isAtBlockingLimit } = calculateTokenWarningState(
693
- tokenCountWithEstimation(messagesForQuery) - snipTokensFreed,
694
- toolUseContext.options.mainLoopModel,
695
- )
696
- if (isAtBlockingLimit) {
697
- yield createAssistantAPIErrorMessage({
698
- content: PROMPT_TOO_LONG_ERROR_MESSAGE,
699
- error: 'invalid_request',
700
- })
701
- return { reason: 'blocking_limit' }
702
- }
703
- }
704
-
705
- let attemptWithFallback = true
706
-
707
- queryCheckpoint('query_api_loop_start')
708
- try {
709
- while (attemptWithFallback) {
710
- attemptWithFallback = false
711
- try {
712
- let streamingFallbackOccured = false
713
- queryCheckpoint('query_api_streaming_start')
714
- const toolChoiceOverride = selectUmmayaToolChoiceOverride({
715
- messages: messagesForQuery,
716
- tools: toolUseContext.options.tools,
717
- })
718
- if (toolChoiceOverride) {
719
- logForDebugging(
720
- `UMMAYA tool-choice override: ${toolChoiceOverride.name}`,
721
- )
722
- }
723
- for await (const message of deps.callModel({
724
- messages: prependUserContext(messagesForQuery, userContext),
725
- systemPrompt: fullSystemPrompt,
726
- thinkingConfig: toolUseContext.options.thinkingConfig,
727
- tools: toolUseContext.options.tools,
728
- signal: toolUseContext.abortController.signal,
729
- options: {
730
- async getToolPermissionContext() {
731
- const appState = toolUseContext.getAppState()
732
- return appState.toolPermissionContext
733
- },
734
- model: currentModel,
735
- ...(config.gates.fastModeEnabled && {
736
- fastMode: appState.fastMode,
737
- }),
738
- toolChoice: toolChoiceOverride,
739
- isNonInteractiveSession:
740
- toolUseContext.options.isNonInteractiveSession,
741
- fallbackModel,
742
- onStreamingFallback: () => {
743
- streamingFallbackOccured = true
744
- },
745
- querySource,
746
- agents: toolUseContext.options.agentDefinitions.activeAgents,
747
- allowedAgentTypes:
748
- toolUseContext.options.agentDefinitions.allowedAgentTypes,
749
- hasAppendSystemPrompt:
750
- !!toolUseContext.options.appendSystemPrompt,
751
- maxOutputTokensOverride,
752
- fetchOverride: dumpPromptsFetch,
753
- mcpTools: appState.mcp.tools,
754
- hasPendingMcpServers: appState.mcp.clients.some(
755
- c => c.type === 'pending',
756
- ),
757
- queryTracking,
758
- effortValue: appState.effortValue,
759
- reasoningMode: appState.reasoningMode,
760
- advisorModel: appState.advisorModel,
761
- skipCacheWrite,
762
- agentId: toolUseContext.agentId,
763
- addNotification: toolUseContext.addNotification,
764
- ...(params.taskBudget && {
765
- taskBudget: {
766
- total: params.taskBudget.total,
767
- ...(taskBudgetRemaining !== undefined && {
768
- remaining: taskBudgetRemaining,
769
- }),
770
- },
771
- }),
772
- },
773
- })) {
774
- // We won't use the tool_calls from the first attempt
775
- // We could.. but then we'd have to merge assistant messages
776
- // with different ids and double up on full the tool_results
777
- if (streamingFallbackOccured) {
778
- // Yield tombstones for orphaned messages so they're removed from UI and transcript.
779
- // These partial messages (especially thinking blocks) have invalid signatures
780
- // that would cause "thinking blocks cannot be modified" API errors.
781
- for (const msg of assistantMessages) {
782
- yield { type: 'tombstone' as const, message: msg }
783
- }
784
- logEvent('tengu_orphaned_messages_tombstoned', {
785
- orphanedMessageCount: assistantMessages.length,
786
- queryChainId: queryChainIdForAnalytics,
787
- queryDepth: queryTracking.depth,
788
- })
789
-
790
- assistantMessages.length = 0
791
- toolResults.length = 0
792
- toolUseBlocks.length = 0
793
- needsFollowUp = false
794
-
795
- // Discard pending results from the failed streaming attempt and create
796
- // a fresh executor. This prevents orphan tool_results (with old tool_use_ids)
797
- // from being yielded after the fallback response arrives.
798
- if (streamingToolExecutor) {
799
- streamingToolExecutor.discard()
800
- streamingToolExecutor = new StreamingToolExecutor(
801
- toolUseContext.options.tools,
802
- canUseTool,
803
- toolUseContext,
804
- )
805
- }
806
- }
807
- // Backfill tool_use inputs on a cloned message before yield so
808
- // SDK stream output and transcript serialization see legacy/derived
809
- // fields. The original `message` is left untouched for
810
- // assistantMessages.push below — it flows back to the API and
811
- // mutating it would break prompt caching (byte mismatch).
812
- let yieldMessage: typeof message = message
813
- if (message.type === 'assistant') {
814
- let clonedContent: typeof message.message.content | undefined
815
- for (let i = 0; i < message.message.content.length; i++) {
816
- const block = message.message.content[i]!
817
- if (
818
- block.type === 'tool_use' &&
819
- typeof block.input === 'object' &&
820
- block.input !== null
821
- ) {
822
- const tool = findToolByName(
823
- toolUseContext.options.tools,
824
- block.name,
825
- )
826
- if (tool?.backfillObservableInput) {
827
- const originalInput = block.input as Record<string, unknown>
828
- const inputCopy = { ...originalInput }
829
- tool.backfillObservableInput(inputCopy)
830
- // Only yield a clone when backfill ADDED fields; skip if
831
- // it only OVERWROTE existing ones (e.g. file tools
832
- // expanding file_path). Overwrites change the serialized
833
- // transcript and break VCR fixture hashes on resume,
834
- // while adding nothing the SDK stream needs — hooks get
835
- // the expanded path via toolExecution.ts separately.
836
- const addedFields = Object.keys(inputCopy).some(
837
- k => !(k in originalInput),
838
- )
839
- if (addedFields) {
840
- clonedContent ??= [...message.message.content]
841
- clonedContent[i] = { ...block, input: inputCopy }
842
- }
843
- }
844
- }
845
- }
846
- if (clonedContent) {
847
- yieldMessage = {
848
- ...message,
849
- message: { ...message.message, content: clonedContent },
850
- }
851
- }
852
- }
853
- // Withhold recoverable errors (prompt-too-long, max-output-tokens)
854
- // until we know whether recovery (collapse drain / reactive
855
- // compact / truncation retry) can succeed. Still pushed to
856
- // assistantMessages so the recovery checks below find them.
857
- // Either subsystem's withhold is sufficient — they're
858
- // independent so turning one off doesn't break the other's
859
- // recovery path.
860
- //
861
- // feature() only works in if/ternary conditions (bun:bundle
862
- // tree-shaking constraint), so the collapse check is nested
863
- // rather than composed.
864
- let withheld = false
865
- const assistantHasToolUse =
866
- message.type === 'assistant' &&
867
- message.message.content.some(
868
- content => content.type === 'tool_use',
869
- )
870
- if (feature('CONTEXT_COLLAPSE')) {
871
- if (
872
- contextCollapse?.isWithheldPromptTooLong(
873
- message,
874
- isPromptTooLongMessage,
875
- querySource,
876
- )
877
- ) {
878
- withheld = true
879
- }
880
- }
881
- if (reactiveCompact?.isWithheldPromptTooLong(message)) {
882
- withheld = true
883
- }
884
- if (
885
- mediaRecoveryEnabled &&
886
- reactiveCompact?.isWithheldMediaSizeError(message)
887
- ) {
888
- withheld = true
889
- }
890
- if (isWithheldMaxOutputTokens(message)) {
891
- withheld = true
892
- }
893
- if (
894
- message.type === 'assistant' &&
895
- !assistantHasToolUse &&
896
- shouldWithholdKmaAnalysisToolCallText({
897
- messages: messagesForQuery,
898
- candidate: message,
899
- })
900
- ) {
901
- withheld = true
902
- }
903
- if (
904
- message.type === 'assistant' &&
905
- !assistantHasToolUse &&
906
- shouldWithholdProtectedCheckToolCallText({
907
- messages: messagesForQuery,
908
- candidate: message,
909
- })
910
- ) {
911
- withheld = true
912
- }
913
- if (
914
- message.type === 'assistant' &&
915
- !assistantHasToolUse &&
916
- shouldWithholdTagoBusFinalAnswer({
917
- messages: messagesForQuery,
918
- candidate: message,
919
- })
920
- ) {
921
- withheld = true
922
- }
923
- if (
924
- message.type === 'assistant' &&
925
- !assistantHasToolUse &&
926
- shouldWithholdAirKoreaFinalAnswer({
927
- messages: messagesForQuery,
928
- candidate: message,
929
- })
930
- ) {
931
- withheld = true
932
- }
933
- if (
934
- message.type === 'assistant' &&
935
- !assistantHasToolUse &&
936
- shouldWithholdGenericPendingFinalAnswer({
937
- messages: messagesForQuery,
938
- candidate: message,
939
- })
940
- ) {
941
- withheld = true
942
- }
943
- if (
944
- message.type === 'assistant' &&
945
- !assistantHasToolUse &&
946
- shouldWithholdTextToolCallFinalAnswer({
947
- messages: messagesForQuery,
948
- candidate: message,
949
- })
950
- ) {
951
- withheld = true
952
- }
953
- // Claude Code streams native tool_use blocks as visible assistant
954
- // commits before tool execution. UMMAYA recovery/repair guards may
955
- // withhold prose, but they must never hide the structured tool_use
956
- // message that anchors the following tool_result.
957
- if (assistantHasToolUse) {
958
- withheld = false
959
- }
960
- if (!withheld) {
961
- yield yieldMessage
962
- }
963
- if (message.type === 'assistant') {
964
- assistantMessages.push(message)
965
-
966
- const msgToolUseBlocks = message.message.content.filter(
967
- content => content.type === 'tool_use',
968
- ) as ToolUseBlock[]
969
- if (msgToolUseBlocks.length > 0) {
970
- toolUseBlocks.push(...msgToolUseBlocks)
971
- needsFollowUp = true
972
- }
973
-
974
- if (
975
- streamingToolExecutor &&
976
- !toolUseContext.abortController.signal.aborted
977
- ) {
978
- for (const toolBlock of msgToolUseBlocks) {
979
- streamingToolExecutor.addTool(toolBlock, message)
980
- }
981
- }
982
- }
983
-
984
- if (
985
- streamingToolExecutor &&
986
- !toolUseContext.abortController.signal.aborted
987
- ) {
988
- for (const result of streamingToolExecutor.getCompletedResults()) {
989
- if (result.message) {
990
- yield result.message
991
- toolResults.push(
992
- ...normalizeMessagesForAPI(
993
- [result.message],
994
- toolUseContext.options.tools,
995
- ).filter(_ => _.type === 'user'),
996
- )
997
- }
998
- }
999
- }
1000
- }
1001
- queryCheckpoint('query_api_streaming_end')
1002
-
1003
- // Yield deferred microcompact boundary message using actual API-reported
1004
- // token deletion count instead of client-side estimates.
1005
- // Entire block gated behind feature() so the excluded string
1006
- // is eliminated from external builds.
1007
- if (feature('CACHED_MICROCOMPACT') && pendingCacheEdits) {
1008
- const lastAssistant = assistantMessages.at(-1)
1009
- // The API field is cumulative/sticky across requests, so we
1010
- // subtract the baseline captured before this request to get the delta.
1011
- const usage = lastAssistant?.message.usage
1012
- const cumulativeDeleted = usage
1013
- ? ((usage as unknown as Record<string, number>)
1014
- .cache_deleted_input_tokens ?? 0)
1015
- : 0
1016
- const deletedTokens = Math.max(
1017
- 0,
1018
- cumulativeDeleted - pendingCacheEdits.baselineCacheDeletedTokens,
1019
- )
1020
- if (deletedTokens > 0) {
1021
- yield createMicrocompactBoundaryMessage(
1022
- pendingCacheEdits.trigger,
1023
- 0,
1024
- deletedTokens,
1025
- pendingCacheEdits.deletedToolIds,
1026
- [],
1027
- )
1028
- }
1029
- }
1030
- } catch (innerError) {
1031
- if (innerError instanceof FallbackTriggeredError && fallbackModel) {
1032
- // Fallback was triggered - switch model and retry
1033
- currentModel = fallbackModel
1034
- attemptWithFallback = true
1035
-
1036
- // Clear assistant messages since we'll retry the entire request
1037
- yield* yieldMissingToolResultBlocks(
1038
- assistantMessages,
1039
- 'Model fallback triggered',
1040
- )
1041
- assistantMessages.length = 0
1042
- toolResults.length = 0
1043
- toolUseBlocks.length = 0
1044
- needsFollowUp = false
1045
-
1046
- // Discard pending results from the failed attempt and create a
1047
- // fresh executor. This prevents orphan tool_results (with old
1048
- // tool_use_ids) from leaking into the retry.
1049
- if (streamingToolExecutor) {
1050
- streamingToolExecutor.discard()
1051
- streamingToolExecutor = new StreamingToolExecutor(
1052
- toolUseContext.options.tools,
1053
- canUseTool,
1054
- toolUseContext,
1055
- )
1056
- }
1057
-
1058
- // Update tool use context with new model
1059
- toolUseContext.options.mainLoopModel = fallbackModel
1060
-
1061
- // Thinking signatures are model-bound: replaying a protected-thinking
1062
- // block (e.g. capybara) to an unprotected fallback (e.g. opus) 400s.
1063
- // Strip before retry so the fallback model gets clean history.
1064
- if (process.env.USER_TYPE === 'ant') {
1065
- messagesForQuery = stripSignatureBlocks(messagesForQuery)
1066
- }
1067
-
1068
- // Log the fallback event
1069
- logEvent('tengu_model_fallback_triggered', {
1070
- original_model:
1071
- innerError.originalModel as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
1072
- fallback_model:
1073
- fallbackModel as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
1074
- entrypoint:
1075
- 'cli' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
1076
- queryChainId: queryChainIdForAnalytics,
1077
- queryDepth: queryTracking.depth,
1078
- })
1079
-
1080
- // Yield system message about fallback — use 'warning' level so
1081
- // users see the notification without needing verbose mode
1082
- yield createSystemMessage(
1083
- `Switched to ${renderModelName(innerError.fallbackModel)} due to high demand for ${renderModelName(innerError.originalModel)}`,
1084
- 'warning',
1085
- )
1086
-
1087
- continue
1088
- }
1089
- throw innerError
1090
- }
1091
- }
1092
- } catch (error) {
1093
- logError(error)
1094
- const errorMessage =
1095
- error instanceof Error ? error.message : String(error)
1096
- logEvent('tengu_query_error', {
1097
- assistantMessages: assistantMessages.length,
1098
- toolUses: assistantMessages.flatMap(_ =>
1099
- _.message.content.filter(content => content.type === 'tool_use'),
1100
- ).length,
1101
-
1102
- queryChainId: queryChainIdForAnalytics,
1103
- queryDepth: queryTracking.depth,
1104
- })
1105
-
1106
- // Handle image size/resize errors with user-friendly messages
1107
- if (
1108
- error instanceof ImageSizeError ||
1109
- error instanceof ImageResizeError
1110
- ) {
1111
- yield createAssistantAPIErrorMessage({
1112
- content: error.message,
1113
- })
1114
- return { reason: 'image_error' }
1115
- }
1116
-
1117
- // Generally queryModelWithStreaming should not throw errors but instead
1118
- // yield them as synthetic assistant messages. However if it does throw
1119
- // due to a bug, we may end up in a state where we have already emitted
1120
- // a tool_use block but will stop before emitting the tool_result.
1121
- yield* yieldMissingToolResultBlocks(assistantMessages, errorMessage)
1122
-
1123
- // Surface the real error instead of a misleading "[Request interrupted
1124
- // by user]" — this path is a model/runtime failure, not a user action.
1125
- // SDK consumers were seeing phantom interrupts on e.g. Node 18's missing
1126
- // Array.prototype.with(), masking the actual cause.
1127
- yield createAssistantAPIErrorMessage({
1128
- content: errorMessage,
1129
- })
1130
-
1131
- // To help track down bugs, log loudly for ants
1132
- logAntError('Query error', error)
1133
- return { reason: 'model_error', error }
1134
- }
1135
-
1136
- // Execute post-sampling hooks after model response is complete
1137
- if (assistantMessages.length > 0) {
1138
- void executePostSamplingHooks(
1139
- [...messagesForQuery, ...assistantMessages],
1140
- systemPrompt,
1141
- userContext,
1142
- systemContext,
1143
- toolUseContext,
1144
- querySource,
1145
- )
1146
- }
1147
-
1148
- // We need to handle a streaming abort before anything else.
1149
- // When using streamingToolExecutor, we must consume getRemainingResults() so the
1150
- // executor can generate synthetic tool_result blocks for queued/in-progress tools.
1151
- // Without this, tool_use blocks would lack matching tool_result blocks.
1152
- if (toolUseContext.abortController.signal.aborted) {
1153
- if (streamingToolExecutor) {
1154
- // Consume remaining results - executor generates synthetic tool_results for
1155
- // aborted tools since it checks the abort signal in executeTool()
1156
- for await (const update of streamingToolExecutor.getRemainingResults()) {
1157
- if (update.message) {
1158
- yield update.message
1159
- }
1160
- }
1161
- } else {
1162
- yield* yieldMissingToolResultBlocks(
1163
- assistantMessages,
1164
- 'Interrupted by user',
1165
- )
1166
- }
1167
- // chicago MCP: auto-unhide + lock release on interrupt. Same cleanup
1168
- // as the natural turn-end path in stopHooks.ts. Main thread only —
1169
- // see stopHooks.ts for the subagent-releasing-main's-lock rationale.
1170
- if (feature('CHICAGO_MCP') && !toolUseContext.agentId) {
1171
- try {
1172
- const { cleanupComputerUseAfterTurn } = await import(
1173
- './utils/computerUse/cleanup.js'
1174
- )
1175
- await cleanupComputerUseAfterTurn(toolUseContext)
1176
- } catch {
1177
- // Failures are silent — this is dogfooding cleanup, not critical path
1178
- }
1179
- }
1180
-
1181
- // Skip the interruption message for submit-interrupts — the queued
1182
- // user message that follows provides sufficient context.
1183
- if (toolUseContext.abortController.signal.reason !== 'interrupt') {
1184
- yield createUserInterruptionMessage({
1185
- toolUse: false,
1186
- })
1187
- }
1188
- return { reason: 'aborted_streaming' }
1189
- }
1190
-
1191
- // Yield tool use summary from previous turn — haiku (~1s) resolved during model streaming (5-30s)
1192
- if (pendingToolUseSummary) {
1193
- const summary = await pendingToolUseSummary
1194
- if (summary) {
1195
- yield summary
1196
- }
1197
- }
1198
-
1199
- if (!needsFollowUp) {
1200
- const lastMessage = assistantMessages.at(-1)
1201
-
1202
- // Prompt-too-long recovery: the streaming loop withheld the error
1203
- // (see withheldByCollapse / withheldByReactive above). Try collapse
1204
- // drain first (cheap, keeps granular context), then reactive compact
1205
- // (full summary). Single-shot on each — if a retry still 413's,
1206
- // the next stage handles it or the error surfaces.
1207
- const isWithheld413 =
1208
- lastMessage?.type === 'assistant' &&
1209
- lastMessage.isApiErrorMessage &&
1210
- isPromptTooLongMessage(lastMessage)
1211
- // Media-size rejections (image/PDF/many-image) are recoverable via
1212
- // reactive compact's strip-retry. Unlike PTL, media errors skip the
1213
- // collapse drain — collapse doesn't strip images. mediaRecoveryEnabled
1214
- // is the hoisted gate from before the stream loop (same value as the
1215
- // withholding check — these two must agree or a withheld message is
1216
- // lost). If the oversized media is in the preserved tail, the
1217
- // post-compact turn will media-error again; hasAttemptedReactiveCompact
1218
- // prevents a spiral and the error surfaces.
1219
- const isWithheldMedia =
1220
- mediaRecoveryEnabled &&
1221
- reactiveCompact?.isWithheldMediaSizeError(lastMessage)
1222
- if (isWithheld413) {
1223
- // First: drain all staged context-collapses. Gated on the PREVIOUS
1224
- // transition not being collapse_drain_retry — if we already drained
1225
- // and the retry still 413'd, fall through to reactive compact.
1226
- if (
1227
- feature('CONTEXT_COLLAPSE') &&
1228
- contextCollapse &&
1229
- state.transition?.reason !== 'collapse_drain_retry'
1230
- ) {
1231
- const drained = contextCollapse.recoverFromOverflow(
1232
- messagesForQuery,
1233
- querySource,
1234
- )
1235
- if (drained.committed > 0) {
1236
- const next: State = {
1237
- messages: drained.messages,
1238
- toolUseContext,
1239
- autoCompactTracking: tracking,
1240
- maxOutputTokensRecoveryCount,
1241
- hasAttemptedReactiveCompact,
1242
- maxOutputTokensOverride: undefined,
1243
- pendingToolUseSummary: undefined,
1244
- stopHookActive: undefined,
1245
- turnCount,
1246
- transition: {
1247
- reason: 'collapse_drain_retry',
1248
- committed: drained.committed,
1249
- },
1250
- }
1251
- state = next
1252
- continue
1253
- }
1254
- }
1255
- }
1256
- if ((isWithheld413 || isWithheldMedia) && reactiveCompact) {
1257
- const compacted = await reactiveCompact.tryReactiveCompact({
1258
- hasAttempted: hasAttemptedReactiveCompact,
1259
- querySource,
1260
- aborted: toolUseContext.abortController.signal.aborted,
1261
- messages: messagesForQuery,
1262
- cacheSafeParams: {
1263
- systemPrompt,
1264
- userContext,
1265
- systemContext,
1266
- toolUseContext,
1267
- forkContextMessages: messagesForQuery,
1268
- },
1269
- })
1270
-
1271
- if (compacted) {
1272
- // task_budget: same carryover as the proactive path above.
1273
- // messagesForQuery still holds the pre-compact array here (the
1274
- // 413-failed attempt's input).
1275
- if (params.taskBudget) {
1276
- const preCompactContext =
1277
- finalContextTokensFromLastResponse(messagesForQuery)
1278
- taskBudgetRemaining = Math.max(
1279
- 0,
1280
- (taskBudgetRemaining ?? params.taskBudget.total) -
1281
- preCompactContext,
1282
- )
1283
- }
1284
-
1285
- const postCompactMessages = buildPostCompactMessages(compacted)
1286
- for (const msg of postCompactMessages) {
1287
- yield msg
1288
- }
1289
- const next: State = {
1290
- messages: postCompactMessages,
1291
- toolUseContext,
1292
- autoCompactTracking: undefined,
1293
- maxOutputTokensRecoveryCount,
1294
- hasAttemptedReactiveCompact: true,
1295
- maxOutputTokensOverride: undefined,
1296
- pendingToolUseSummary: undefined,
1297
- stopHookActive: undefined,
1298
- turnCount,
1299
- transition: { reason: 'reactive_compact_retry' },
1300
- }
1301
- state = next
1302
- continue
1303
- }
1304
-
1305
- // No recovery — surface the withheld error and exit. Do NOT fall
1306
- // through to stop hooks: the model never produced a valid response,
1307
- // so hooks have nothing meaningful to evaluate. Running stop hooks
1308
- // on prompt-too-long creates a death spiral: error → hook blocking
1309
- // → retry → error → … (the hook injects more tokens each cycle).
1310
- yield lastMessage
1311
- void executeStopFailureHooks(lastMessage, toolUseContext)
1312
- return { reason: isWithheldMedia ? 'image_error' : 'prompt_too_long' }
1313
- } else if (feature('CONTEXT_COLLAPSE') && isWithheld413) {
1314
- // reactiveCompact compiled out but contextCollapse withheld and
1315
- // couldn't recover (staged queue empty/stale). Surface. Same
1316
- // early-return rationale — don't fall through to stop hooks.
1317
- yield lastMessage
1318
- void executeStopFailureHooks(lastMessage, toolUseContext)
1319
- return { reason: 'prompt_too_long' }
1320
- }
1321
-
1322
- // Check for max_output_tokens and inject recovery message. The error
1323
- // was withheld from the stream above; only surface it if recovery
1324
- // exhausts.
1325
- if (isWithheldMaxOutputTokens(lastMessage)) {
1326
- // Escalating retry: if we used the capped 8k default and hit the
1327
- // limit, retry the SAME request at 64k — no meta message, no
1328
- // multi-turn dance. This fires once per turn (guarded by the
1329
- // override check), then falls through to multi-turn recovery if
1330
- // 64k also hits the cap.
1331
- // 3P default: false (not validated on Bedrock/Vertex)
1332
- const capEnabled = getFeatureValue_CACHED_MAY_BE_STALE(
1333
- 'tengu_otk_slot_v1',
1334
- false,
1335
- )
1336
- if (
1337
- capEnabled &&
1338
- maxOutputTokensOverride === undefined &&
1339
- !process.env.CLAUDE_CODE_MAX_OUTPUT_TOKENS
1340
- ) {
1341
- logEvent('tengu_max_tokens_escalate', {
1342
- escalatedTo: ESCALATED_MAX_TOKENS,
1343
- })
1344
- const next: State = {
1345
- messages: messagesForQuery,
1346
- toolUseContext,
1347
- autoCompactTracking: tracking,
1348
- maxOutputTokensRecoveryCount,
1349
- hasAttemptedReactiveCompact,
1350
- maxOutputTokensOverride: ESCALATED_MAX_TOKENS,
1351
- pendingToolUseSummary: undefined,
1352
- stopHookActive: undefined,
1353
- turnCount,
1354
- transition: { reason: 'max_output_tokens_escalate' },
1355
- }
1356
- state = next
1357
- continue
1358
- }
1359
-
1360
- if (maxOutputTokensRecoveryCount < MAX_OUTPUT_TOKENS_RECOVERY_LIMIT) {
1361
- const recoveryMessage = createUserMessage({
1362
- content:
1363
- `Output token limit hit. Resume directly — no apology, no recap of what you were doing. ` +
1364
- `Pick up mid-thought if that is where the cut happened. Break remaining work into smaller pieces.`,
1365
- isMeta: true,
1366
- })
1367
-
1368
- const next: State = {
1369
- messages: [
1370
- ...messagesForQuery,
1371
- ...assistantMessages,
1372
- recoveryMessage,
1373
- ],
1374
- toolUseContext,
1375
- autoCompactTracking: tracking,
1376
- maxOutputTokensRecoveryCount: maxOutputTokensRecoveryCount + 1,
1377
- hasAttemptedReactiveCompact,
1378
- maxOutputTokensOverride: undefined,
1379
- pendingToolUseSummary: undefined,
1380
- stopHookActive: undefined,
1381
- turnCount,
1382
- transition: {
1383
- reason: 'max_output_tokens_recovery',
1384
- attempt: maxOutputTokensRecoveryCount + 1,
1385
- },
1386
- }
1387
- state = next
1388
- continue
1389
- }
1390
-
1391
- // Recovery exhausted — surface the withheld error now.
1392
- yield lastMessage
1393
- }
1394
-
1395
- // Skip stop hooks when the last message is an API error (rate limit,
1396
- // prompt-too-long, auth failure, etc.). The model never produced a
1397
- // real response — hooks evaluating it create a death spiral:
1398
- // error → hook blocking → retry → error → …
1399
- if (lastMessage?.isApiErrorMessage) {
1400
- void executeStopFailureHooks(lastMessage, toolUseContext)
1401
- return { reason: 'completed' }
1402
- }
1403
-
1404
- const kmaAnalysisMissingToolPrompt =
1405
- buildKmaAnalysisMissingToolPromptIfNeeded({
1406
- messages: [...messagesForQuery, ...assistantMessages],
1407
- })
1408
- if (kmaAnalysisMissingToolPrompt) {
1409
- const kmaAnalysisChartTool = getAdapterToolByName(
1410
- 'kma_apihub_url_analysis_weather_chart_image',
1411
- )
1412
- let nextToolUseContext = toolUseContext
1413
- if (
1414
- kmaAnalysisChartTool &&
1415
- !toolUseContext.options.tools.some(
1416
- tool => tool.name === kmaAnalysisChartTool.name,
1417
- )
1418
- ) {
1419
- nextToolUseContext = {
1420
- ...toolUseContext,
1421
- options: {
1422
- ...toolUseContext.options,
1423
- tools: [...toolUseContext.options.tools, kmaAnalysisChartTool],
1424
- },
1425
- }
1426
- }
1427
- const next: State = {
1428
- messages: [
1429
- ...messagesForQuery,
1430
- ...assistantMessages,
1431
- createUserMessage({
1432
- content: kmaAnalysisMissingToolPrompt,
1433
- isMeta: true,
1434
- }),
1435
- ],
1436
- toolUseContext: nextToolUseContext,
1437
- autoCompactTracking: tracking,
1438
- maxOutputTokensRecoveryCount: 0,
1439
- hasAttemptedReactiveCompact,
1440
- maxOutputTokensOverride: undefined,
1441
- pendingToolUseSummary: undefined,
1442
- stopHookActive: true,
1443
- turnCount,
1444
- transition: { reason: 'stop_hook_blocking' },
1445
- }
1446
- state = next
1447
- continue
1448
- }
1449
-
1450
- const kmaAnalysisFinalAnswerRepairPrompt =
1451
- buildKmaAnalysisFinalAnswerRepairPromptIfNeeded({
1452
- messages: [...messagesForQuery, ...assistantMessages],
1453
- })
1454
- if (kmaAnalysisFinalAnswerRepairPrompt) {
1455
- const next: State = {
1456
- messages: [
1457
- ...messagesForQuery,
1458
- ...assistantMessages,
1459
- createUserMessage({
1460
- content: kmaAnalysisFinalAnswerRepairPrompt,
1461
- isMeta: true,
1462
- }),
1463
- ],
1464
- toolUseContext,
1465
- autoCompactTracking: tracking,
1466
- maxOutputTokensRecoveryCount: 0,
1467
- hasAttemptedReactiveCompact,
1468
- maxOutputTokensOverride: undefined,
1469
- pendingToolUseSummary: undefined,
1470
- stopHookActive: true,
1471
- turnCount,
1472
- transition: { reason: 'stop_hook_blocking' },
1473
- }
1474
- state = next
1475
- continue
1476
- }
1477
-
1478
- const protectedCheckFinalAnswerRepairPrompt =
1479
- buildProtectedCheckFinalAnswerRepairPromptIfNeeded({
1480
- messages: [...messagesForQuery, ...assistantMessages],
1481
- })
1482
- if (protectedCheckFinalAnswerRepairPrompt) {
1483
- const next: State = {
1484
- messages: [
1485
- ...messagesForQuery,
1486
- ...assistantMessages,
1487
- createUserMessage({
1488
- content: protectedCheckFinalAnswerRepairPrompt,
1489
- isMeta: true,
1490
- }),
1491
- ],
1492
- toolUseContext,
1493
- autoCompactTracking: tracking,
1494
- maxOutputTokensRecoveryCount: 0,
1495
- hasAttemptedReactiveCompact,
1496
- maxOutputTokensOverride: undefined,
1497
- pendingToolUseSummary: undefined,
1498
- stopHookActive: true,
1499
- turnCount,
1500
- transition: { reason: 'stop_hook_blocking' },
1501
- }
1502
- state = next
1503
- continue
1504
- }
1505
-
1506
- const airKoreaFinalAnswerRepairPrompt =
1507
- buildAirKoreaFinalAnswerRepairPromptIfNeeded({
1508
- messages: [...messagesForQuery, ...assistantMessages],
1509
- })
1510
- if (airKoreaFinalAnswerRepairPrompt) {
1511
- const next: State = {
1512
- messages: [
1513
- ...messagesForQuery,
1514
- ...assistantMessages,
1515
- createUserMessage({
1516
- content: airKoreaFinalAnswerRepairPrompt,
1517
- isMeta: true,
1518
- }),
1519
- ],
1520
- toolUseContext,
1521
- autoCompactTracking: tracking,
1522
- maxOutputTokensRecoveryCount: 0,
1523
- hasAttemptedReactiveCompact,
1524
- maxOutputTokensOverride: undefined,
1525
- pendingToolUseSummary: undefined,
1526
- stopHookActive: true,
1527
- turnCount,
1528
- transition: { reason: 'stop_hook_blocking' },
1529
- }
1530
- state = next
1531
- continue
1532
- }
1533
-
1534
- const tagoBusFinalAnswerRepairPrompt =
1535
- buildTagoBusFinalAnswerRepairPromptIfNeeded({
1536
- messages: [...messagesForQuery, ...assistantMessages],
1537
- })
1538
- if (tagoBusFinalAnswerRepairPrompt) {
1539
- const next: State = {
1540
- messages: [
1541
- ...messagesForQuery,
1542
- ...assistantMessages,
1543
- createUserMessage({
1544
- content: tagoBusFinalAnswerRepairPrompt,
1545
- isMeta: true,
1546
- }),
1547
- ],
1548
- toolUseContext,
1549
- autoCompactTracking: tracking,
1550
- maxOutputTokensRecoveryCount: 0,
1551
- hasAttemptedReactiveCompact,
1552
- maxOutputTokensOverride: undefined,
1553
- pendingToolUseSummary: undefined,
1554
- stopHookActive: true,
1555
- turnCount,
1556
- transition: { reason: 'stop_hook_blocking' },
1557
- }
1558
- state = next
1559
- continue
1560
- }
1561
-
1562
- const textToolCallFinalAnswerRepairPrompt =
1563
- buildTextToolCallFinalAnswerRepairPromptIfNeeded({
1564
- messages: [...messagesForQuery, ...assistantMessages],
1565
- })
1566
- if (textToolCallFinalAnswerRepairPrompt) {
1567
- const next: State = {
1568
- messages: [
1569
- ...messagesForQuery,
1570
- ...assistantMessages,
1571
- createUserMessage({
1572
- content: textToolCallFinalAnswerRepairPrompt,
1573
- isMeta: true,
1574
- }),
1575
- ],
1576
- toolUseContext,
1577
- autoCompactTracking: tracking,
1578
- maxOutputTokensRecoveryCount: 0,
1579
- hasAttemptedReactiveCompact,
1580
- maxOutputTokensOverride: undefined,
1581
- pendingToolUseSummary: undefined,
1582
- stopHookActive: true,
1583
- turnCount,
1584
- transition: { reason: 'stop_hook_blocking' },
1585
- }
1586
- state = next
1587
- continue
1588
- }
1589
-
1590
- const genericPendingFinalAnswerRepairPrompt =
1591
- buildGenericPendingFinalAnswerRepairPromptIfNeeded({
1592
- messages: [...messagesForQuery, ...assistantMessages],
1593
- })
1594
- if (genericPendingFinalAnswerRepairPrompt) {
1595
- const next: State = {
1596
- messages: [
1597
- ...messagesForQuery,
1598
- ...assistantMessages,
1599
- createUserMessage({
1600
- content: genericPendingFinalAnswerRepairPrompt,
1601
- isMeta: true,
1602
- }),
1603
- ],
1604
- toolUseContext,
1605
- autoCompactTracking: tracking,
1606
- maxOutputTokensRecoveryCount: 0,
1607
- hasAttemptedReactiveCompact,
1608
- maxOutputTokensOverride: undefined,
1609
- pendingToolUseSummary: undefined,
1610
- stopHookActive: true,
1611
- turnCount,
1612
- transition: { reason: 'stop_hook_blocking' },
1613
- }
1614
- state = next
1615
- continue
1616
- }
1617
-
1618
- const stopHookResult = yield* handleStopHooks(
1619
- messagesForQuery,
1620
- assistantMessages,
1621
- systemPrompt,
1622
- userContext,
1623
- systemContext,
1624
- toolUseContext,
1625
- querySource,
1626
- stopHookActive,
1627
- )
1628
-
1629
- if (stopHookResult.preventContinuation) {
1630
- return { reason: 'stop_hook_prevented' }
1631
- }
1632
-
1633
- if (stopHookResult.blockingErrors.length > 0) {
1634
- const next: State = {
1635
- messages: [
1636
- ...messagesForQuery,
1637
- ...assistantMessages,
1638
- ...stopHookResult.blockingErrors,
1639
- ],
1640
- toolUseContext,
1641
- autoCompactTracking: tracking,
1642
- maxOutputTokensRecoveryCount: 0,
1643
- // Preserve the reactive compact guard — if compact already ran and
1644
- // couldn't recover from prompt-too-long, retrying after a stop-hook
1645
- // blocking error will produce the same result. Resetting to false
1646
- // here caused an infinite loop: compact → still too long → error →
1647
- // stop hook blocking → compact → … burning thousands of API calls.
1648
- hasAttemptedReactiveCompact,
1649
- maxOutputTokensOverride: undefined,
1650
- pendingToolUseSummary: undefined,
1651
- stopHookActive: true,
1652
- turnCount,
1653
- transition: { reason: 'stop_hook_blocking' },
1654
- }
1655
- state = next
1656
- continue
1657
- }
1658
-
1659
- if (feature('TOKEN_BUDGET')) {
1660
- const decision = checkTokenBudget(
1661
- budgetTracker!,
1662
- toolUseContext.agentId,
1663
- getCurrentTurnTokenBudget(),
1664
- getTurnOutputTokens(),
1665
- )
1666
-
1667
- if (decision.action === 'continue') {
1668
- incrementBudgetContinuationCount()
1669
- logForDebugging(
1670
- `Token budget continuation #${decision.continuationCount}: ${decision.pct}% (${decision.turnTokens.toLocaleString()} / ${decision.budget.toLocaleString()})`,
1671
- )
1672
- state = {
1673
- messages: [
1674
- ...messagesForQuery,
1675
- ...assistantMessages,
1676
- createUserMessage({
1677
- content: decision.nudgeMessage,
1678
- isMeta: true,
1679
- }),
1680
- ],
1681
- toolUseContext,
1682
- autoCompactTracking: tracking,
1683
- maxOutputTokensRecoveryCount: 0,
1684
- hasAttemptedReactiveCompact: false,
1685
- maxOutputTokensOverride: undefined,
1686
- pendingToolUseSummary: undefined,
1687
- stopHookActive: undefined,
1688
- turnCount,
1689
- transition: { reason: 'token_budget_continuation' },
1690
- }
1691
- continue
1692
- }
1693
-
1694
- if (decision.completionEvent) {
1695
- if (decision.completionEvent.diminishingReturns) {
1696
- logForDebugging(
1697
- `Token budget early stop: diminishing returns at ${decision.completionEvent.pct}%`,
1698
- )
1699
- }
1700
- logEvent('tengu_token_budget_completed', {
1701
- ...decision.completionEvent,
1702
- queryChainId: queryChainIdForAnalytics,
1703
- queryDepth: queryTracking.depth,
1704
- })
1705
- }
1706
- }
1707
-
1708
- return { reason: 'completed' }
1709
- }
1710
-
1711
- let shouldPreventContinuation = false
1712
- let updatedToolUseContext = toolUseContext
1713
-
1714
- queryCheckpoint('query_tool_execution_start')
1715
-
1716
- if (streamingToolExecutor) {
1717
- logEvent('tengu_streaming_tool_execution_used', {
1718
- tool_count: toolUseBlocks.length,
1719
- queryChainId: queryChainIdForAnalytics,
1720
- queryDepth: queryTracking.depth,
1721
- })
1722
- } else {
1723
- logEvent('tengu_streaming_tool_execution_not_used', {
1724
- tool_count: toolUseBlocks.length,
1725
- queryChainId: queryChainIdForAnalytics,
1726
- queryDepth: queryTracking.depth,
1727
- })
1728
- }
1729
-
1730
- const toolUpdates = streamingToolExecutor
1731
- ? streamingToolExecutor.getRemainingResults()
1732
- : runTools(toolUseBlocks, assistantMessages, canUseTool, toolUseContext)
1733
-
1734
- for await (const update of toolUpdates) {
1735
- if (update.message) {
1736
- yield update.message
1737
-
1738
- if (
1739
- update.message.type === 'attachment' &&
1740
- update.message.attachment.type === 'hook_stopped_continuation'
1741
- ) {
1742
- shouldPreventContinuation = true
1743
- }
1744
-
1745
- toolResults.push(
1746
- ...normalizeMessagesForAPI(
1747
- [update.message],
1748
- toolUseContext.options.tools,
1749
- ).filter(_ => _.type === 'user'),
1750
- )
1751
- }
1752
- if (update.newContext) {
1753
- updatedToolUseContext = {
1754
- ...update.newContext,
1755
- queryTracking,
1756
- }
1757
- }
1758
- }
1759
- queryCheckpoint('query_tool_execution_end')
1760
-
1761
- // Generate tool use summary after tool batch completes — passed to next recursive call
1762
- let nextPendingToolUseSummary:
1763
- | Promise<ToolUseSummaryMessage | null>
1764
- | undefined
1765
- if (
1766
- config.gates.emitToolUseSummaries &&
1767
- toolUseBlocks.length > 0 &&
1768
- !toolUseContext.abortController.signal.aborted &&
1769
- !toolUseContext.agentId // subagents don't surface in mobile UI — skip the Haiku call
1770
- ) {
1771
- // Extract the last assistant text block for context
1772
- const lastAssistantMessage = assistantMessages.at(-1)
1773
- let lastAssistantText: string | undefined
1774
- if (lastAssistantMessage) {
1775
- const textBlocks = lastAssistantMessage.message.content.filter(
1776
- block => block.type === 'text',
1777
- )
1778
- if (textBlocks.length > 0) {
1779
- const lastTextBlock = textBlocks.at(-1)
1780
- if (lastTextBlock && 'text' in lastTextBlock) {
1781
- lastAssistantText = lastTextBlock.text
1782
- }
1783
- }
1784
- }
1785
-
1786
- // Collect tool info for summary generation
1787
- const toolUseIds = toolUseBlocks.map(block => block.id)
1788
- const toolInfoForSummary = toolUseBlocks.map(block => {
1789
- // Find the corresponding tool result
1790
- const toolResult = toolResults.find(
1791
- result =>
1792
- result.type === 'user' &&
1793
- Array.isArray(result.message.content) &&
1794
- result.message.content.some(
1795
- content =>
1796
- content.type === 'tool_result' &&
1797
- content.tool_use_id === block.id,
1798
- ),
1799
- )
1800
- const resultContent =
1801
- toolResult?.type === 'user' &&
1802
- Array.isArray(toolResult.message.content)
1803
- ? toolResult.message.content.find(
1804
- (c): c is ToolResultBlockParam =>
1805
- c.type === 'tool_result' && c.tool_use_id === block.id,
1806
- )
1807
- : undefined
1808
- return {
1809
- name: block.name,
1810
- input: block.input,
1811
- output:
1812
- resultContent && 'content' in resultContent
1813
- ? resultContent.content
1814
- : null,
1815
- }
1816
- })
1817
-
1818
- // Fire off summary generation without blocking the next API call
1819
- nextPendingToolUseSummary = generateToolUseSummary({
1820
- tools: toolInfoForSummary,
1821
- signal: toolUseContext.abortController.signal,
1822
- isNonInteractiveSession: toolUseContext.options.isNonInteractiveSession,
1823
- lastAssistantText,
1824
- })
1825
- .then(summary => {
1826
- if (summary) {
1827
- return createToolUseSummaryMessage(summary, toolUseIds)
1828
- }
1829
- return null
1830
- })
1831
- .catch(() => null)
1832
- }
1833
-
1834
- // We were aborted during tool calls
1835
- if (toolUseContext.abortController.signal.aborted) {
1836
- // chicago MCP: auto-unhide + lock release when aborted mid-tool-call.
1837
- // This is the most likely Ctrl+C path for CU (e.g. slow screenshot).
1838
- // Main thread only — see stopHooks.ts for the subagent rationale.
1839
- if (feature('CHICAGO_MCP') && !toolUseContext.agentId) {
1840
- try {
1841
- const { cleanupComputerUseAfterTurn } = await import(
1842
- './utils/computerUse/cleanup.js'
1843
- )
1844
- await cleanupComputerUseAfterTurn(toolUseContext)
1845
- } catch {
1846
- // Failures are silent — this is dogfooding cleanup, not critical path
1847
- }
1848
- }
1849
- // Skip the interruption message for submit-interrupts — the queued
1850
- // user message that follows provides sufficient context.
1851
- if (toolUseContext.abortController.signal.reason !== 'interrupt') {
1852
- yield createUserInterruptionMessage({
1853
- toolUse: true,
1854
- })
1855
- }
1856
- // Check maxTurns before returning when aborted
1857
- const nextTurnCountOnAbort = turnCount + 1
1858
- if (maxTurns && nextTurnCountOnAbort > maxTurns) {
1859
- yield createAttachmentMessage({
1860
- type: 'max_turns_reached',
1861
- maxTurns,
1862
- turnCount: nextTurnCountOnAbort,
1863
- })
1864
- }
1865
- return { reason: 'aborted_tools' }
1866
- }
1867
-
1868
- // If a hook indicated to prevent continuation, stop here
1869
- if (shouldPreventContinuation) {
1870
- return { reason: 'hook_stopped' }
1871
- }
1872
-
1873
- if (tracking?.compacted) {
1874
- tracking.turnCounter++
1875
- logEvent('tengu_post_autocompact_turn', {
1876
- turnId:
1877
- tracking.turnId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
1878
- turnCounter: tracking.turnCounter,
1879
-
1880
- queryChainId: queryChainIdForAnalytics,
1881
- queryDepth: queryTracking.depth,
1882
- })
1883
- }
1884
-
1885
- // Be careful to do this after tool calls are done, because the API
1886
- // will error if we interleave tool_result messages with regular user messages.
1887
-
1888
- // Instrumentation: Track message count before attachments
1889
- logEvent('tengu_query_before_attachments', {
1890
- messagesForQueryCount: messagesForQuery.length,
1891
- assistantMessagesCount: assistantMessages.length,
1892
- toolResultsCount: toolResults.length,
1893
- queryChainId: queryChainIdForAnalytics,
1894
- queryDepth: queryTracking.depth,
1895
- })
1896
-
1897
- // Get queued commands snapshot before processing attachments.
1898
- // These will be sent as attachments so Claude can respond to them in the current turn.
1899
- //
1900
- // Drain pending notifications. LocalShellTask completions are 'next'
1901
- // (when MONITOR_TOOL is on) and drain without Sleep. Other task types
1902
- // (agent/workflow/framework) still default to 'later' — the Sleep flush
1903
- // covers those. If all task types move to 'next', this branch could go.
1904
- //
1905
- // Slash commands are excluded from mid-turn drain — they must go through
1906
- // processSlashCommand after the turn ends (via useQueueProcessor), not be
1907
- // sent to the model as text. Bash-mode commands are already excluded by
1908
- // INLINE_NOTIFICATION_MODES in getQueuedCommandAttachments.
1909
- //
1910
- // Agent scoping: the queue is a process-global singleton shared by the
1911
- // coordinator and all in-process subagents. Each loop drains only what's
1912
- // addressed to it — main thread drains agentId===undefined, subagents
1913
- // drain their own agentId. User prompts (mode:'prompt') still go to main
1914
- // only; subagents never see the prompt stream.
1915
- // eslint-disable-next-line custom-rules/require-tool-match-name -- ToolUseBlock.name has no aliases
1916
- const sleepRan = toolUseBlocks.some(b => b.name === SLEEP_TOOL_NAME)
1917
- const isMainThread =
1918
- querySource.startsWith('repl_main_thread') || querySource === 'sdk'
1919
- const currentAgentId = toolUseContext.agentId
1920
- const queuedCommandsSnapshot = getCommandsByMaxPriority(
1921
- sleepRan ? 'later' : 'next',
1922
- ).filter(cmd => {
1923
- if (isSlashCommand(cmd)) return false
1924
- if (isMainThread) return cmd.agentId === undefined
1925
- // Subagents only drain task-notifications addressed to them — never
1926
- // user prompts, even if someone stamps an agentId on one.
1927
- return cmd.mode === 'task-notification' && cmd.agentId === currentAgentId
1928
- })
1929
-
1930
- for await (const attachment of getAttachmentMessages(
1931
- null,
1932
- updatedToolUseContext,
1933
- null,
1934
- queuedCommandsSnapshot,
1935
- [...messagesForQuery, ...assistantMessages, ...toolResults],
1936
- querySource,
1937
- )) {
1938
- yield attachment
1939
- toolResults.push(attachment)
1940
- }
1941
-
1942
- // Memory prefetch consume: only if settled and not already consumed on
1943
- // an earlier iteration. If not settled yet, skip (zero-wait) and retry
1944
- // next iteration — the prefetch gets as many chances as there are loop
1945
- // iterations before the turn ends. readFileState (cumulative across
1946
- // iterations) filters out memories the model already Read/Wrote/Edited
1947
- // — including in earlier iterations, which the per-iteration
1948
- // toolUseBlocks array would miss.
1949
- if (
1950
- pendingMemoryPrefetch &&
1951
- pendingMemoryPrefetch.settledAt !== null &&
1952
- pendingMemoryPrefetch.consumedOnIteration === -1
1953
- ) {
1954
- const memoryAttachments = filterDuplicateMemoryAttachments(
1955
- await pendingMemoryPrefetch.promise,
1956
- toolUseContext.readFileState,
1957
- )
1958
- for (const memAttachment of memoryAttachments) {
1959
- const msg = createAttachmentMessage(memAttachment)
1960
- yield msg
1961
- toolResults.push(msg)
1962
- }
1963
- pendingMemoryPrefetch.consumedOnIteration = turnCount - 1
1964
- }
1965
-
1966
-
1967
- // Inject prefetched skill discovery. collectSkillDiscoveryPrefetch emits
1968
- // hidden_by_main_turn — true when the prefetch resolved before this point
1969
- // (should be >98% at AKI@250ms / Haiku@573ms vs turn durations of 2-30s).
1970
- if (skillPrefetch && pendingSkillPrefetch) {
1971
- const skillAttachments =
1972
- await skillPrefetch.collectSkillDiscoveryPrefetch(pendingSkillPrefetch)
1973
- for (const att of skillAttachments) {
1974
- const msg = createAttachmentMessage(att)
1975
- yield msg
1976
- toolResults.push(msg)
1977
- }
1978
- }
1979
-
1980
- // Remove only commands that were actually consumed as attachments.
1981
- // Prompt and task-notification commands are converted to attachments above.
1982
- const consumedCommands = queuedCommandsSnapshot.filter(
1983
- cmd => cmd.mode === 'prompt' || cmd.mode === 'task-notification',
1984
- )
1985
- if (consumedCommands.length > 0) {
1986
- for (const cmd of consumedCommands) {
1987
- if (cmd.uuid) {
1988
- consumedCommandUuids.push(cmd.uuid)
1989
- notifyCommandLifecycle(cmd.uuid, 'started')
1990
- }
1991
- }
1992
- removeFromQueue(consumedCommands)
1993
- }
1994
-
1995
- // Instrumentation: Track file change attachments after they're added
1996
- const fileChangeAttachmentCount = count(
1997
- toolResults,
1998
- tr =>
1999
- tr.type === 'attachment' && tr.attachment.type === 'edited_text_file',
2000
- )
2001
-
2002
- logEvent('tengu_query_after_attachments', {
2003
- totalToolResultsCount: toolResults.length,
2004
- fileChangeAttachmentCount,
2005
- queryChainId: queryChainIdForAnalytics,
2006
- queryDepth: queryTracking.depth,
2007
- })
2008
-
2009
- // Refresh tools between turns so newly-connected MCP servers become available
2010
- if (updatedToolUseContext.options.refreshTools) {
2011
- const refreshedTools = updatedToolUseContext.options.refreshTools()
2012
- if (refreshedTools !== updatedToolUseContext.options.tools) {
2013
- updatedToolUseContext = {
2014
- ...updatedToolUseContext,
2015
- options: {
2016
- ...updatedToolUseContext.options,
2017
- tools: refreshedTools,
2018
- },
2019
- }
2020
- }
2021
- }
2022
-
2023
- const nmcAedMessages = [...messagesForQuery, ...assistantMessages, ...toolResults]
2024
- const nmcAedTool = getAdapterToolByName('nmc_aed_site_locate')
2025
- const nmcRegionTool = getAdapterToolByName('kakao_coord_to_region')
2026
- const nmcAedAvailableToolNames = new Set(
2027
- updatedToolUseContext.options.tools.map(tool => tool.name),
2028
- )
2029
- if (nmcAedTool) {
2030
- nmcAedAvailableToolNames.add(nmcAedTool.name)
2031
- }
2032
- if (nmcRegionTool) {
2033
- nmcAedAvailableToolNames.add(nmcRegionTool.name)
2034
- }
2035
- const missingNmcHelperTools = [nmcAedTool, nmcRegionTool].filter(
2036
- (tool): tool is NonNullable<ReturnType<typeof getAdapterToolByName>> =>
2037
- Boolean(tool) &&
2038
- !updatedToolUseContext.options.tools.some(existing => existing.name === tool.name),
2039
- )
2040
- if (missingNmcHelperTools.length > 0) {
2041
- updatedToolUseContext = {
2042
- ...updatedToolUseContext,
2043
- options: {
2044
- ...updatedToolUseContext.options,
2045
- tools: [...updatedToolUseContext.options.tools, ...missingNmcHelperTools],
2046
- },
2047
- }
2048
- }
2049
- const nmcAedFollowupPrompt = buildNmcAedFollowupPromptIfNeeded({
2050
- messages: nmcAedMessages,
2051
- availableToolNames: nmcAedAvailableToolNames,
2052
- })
2053
- if (nmcAedFollowupPrompt) {
2054
- toolResults.push(
2055
- createUserMessage({
2056
- content: nmcAedFollowupPrompt,
2057
- isMeta: true,
2058
- }),
2059
- )
2060
- } else {
2061
- const tagoBusMessages = [...messagesForQuery, ...assistantMessages, ...toolResults]
2062
- const tagoBusTools = [
2063
- getAdapterToolByName('tago_bus_route_search'),
2064
- getAdapterToolByName('tago_bus_route_station_search'),
2065
- getAdapterToolByName('tago_bus_arrival_search'),
2066
- ].filter(
2067
- (tool): tool is NonNullable<ReturnType<typeof getAdapterToolByName>> =>
2068
- Boolean(tool),
2069
- )
2070
- const tagoBusAvailableToolNames = new Set(
2071
- updatedToolUseContext.options.tools.map(tool => tool.name),
2072
- )
2073
- for (const tool of tagoBusTools) {
2074
- tagoBusAvailableToolNames.add(tool.name)
2075
- }
2076
- const tagoBusFollowupPrompt = buildTagoBusFollowupPromptIfNeeded({
2077
- messages: tagoBusMessages,
2078
- availableToolNames: tagoBusAvailableToolNames,
2079
- })
2080
- if (tagoBusFollowupPrompt) {
2081
- const existingToolNames = new Set(
2082
- updatedToolUseContext.options.tools.map(tool => tool.name),
2083
- )
2084
- const missingTagoBusTools = tagoBusTools.filter(
2085
- tool => !existingToolNames.has(tool.name),
2086
- )
2087
- if (missingTagoBusTools.length > 0) {
2088
- updatedToolUseContext = {
2089
- ...updatedToolUseContext,
2090
- options: {
2091
- ...updatedToolUseContext.options,
2092
- tools: [...updatedToolUseContext.options.tools, ...missingTagoBusTools],
2093
- },
2094
- }
2095
- }
2096
- toolResults.push(
2097
- createUserMessage({
2098
- content: tagoBusFollowupPrompt,
2099
- isMeta: true,
2100
- }),
2101
- )
2102
- } else {
2103
- const nmcAedCompletionPrompt = buildNmcAedCompletionPromptIfNeeded({
2104
- messages: [...messagesForQuery, ...assistantMessages, ...toolResults],
2105
- })
2106
- if (nmcAedCompletionPrompt) {
2107
- toolResults.push(
2108
- createUserMessage({
2109
- content: nmcAedCompletionPrompt,
2110
- isMeta: true,
2111
- }),
2112
- )
2113
- }
2114
- const tagoBusCompletionPrompt = buildTagoBusCompletionPromptIfNeeded({
2115
- messages: [...messagesForQuery, ...assistantMessages, ...toolResults],
2116
- })
2117
- if (tagoBusCompletionPrompt) {
2118
- toolResults.push(
2119
- createUserMessage({
2120
- content: tagoBusCompletionPrompt,
2121
- isMeta: true,
2122
- }),
2123
- )
2124
- }
2125
- const protectedCheckCompletionPrompt = buildProtectedCheckCompletionPromptIfNeeded({
2126
- messages: [...messagesForQuery, ...assistantMessages, ...toolResults],
2127
- })
2128
- if (protectedCheckCompletionPrompt) {
2129
- toolResults.push(
2130
- createUserMessage({
2131
- content: protectedCheckCompletionPrompt,
2132
- isMeta: true,
2133
- }),
2134
- )
2135
- }
2136
- const kmaAnalysisCompletionPrompt = buildKmaAnalysisCompletionPromptIfNeeded({
2137
- messages: [...messagesForQuery, ...assistantMessages, ...toolResults],
2138
- })
2139
- if (kmaAnalysisCompletionPrompt) {
2140
- toolResults.push(
2141
- createUserMessage({
2142
- content: kmaAnalysisCompletionPrompt,
2143
- isMeta: true,
2144
- }),
2145
- )
2146
- }
2147
- const airKoreaCompletionPrompt = buildAirKoreaCompletionPromptIfNeeded({
2148
- messages: [...messagesForQuery, ...assistantMessages, ...toolResults],
2149
- })
2150
- if (airKoreaCompletionPrompt) {
2151
- toolResults.push(
2152
- createUserMessage({
2153
- content: airKoreaCompletionPrompt,
2154
- isMeta: true,
2155
- }),
2156
- )
2157
- }
2158
- }
2159
- }
2160
-
2161
- const toolUseContextWithQueryTracking = {
2162
- ...updatedToolUseContext,
2163
- queryTracking,
2164
- }
2165
-
2166
- // Each time we have tool results and are about to recurse, that's a turn
2167
- const nextTurnCount = turnCount + 1
2168
-
2169
- // Periodic task summary for `claude ps` — fires mid-turn so a
2170
- // long-running agent still refreshes what it's working on. Gated
2171
- // only on !agentId so every top-level conversation (REPL, SDK, HFI,
2172
- // remote) generates summaries; subagents/forks don't.
2173
- if (feature('BG_SESSIONS')) {
2174
- if (
2175
- !toolUseContext.agentId &&
2176
- taskSummaryModule!.shouldGenerateTaskSummary()
2177
- ) {
2178
- taskSummaryModule!.maybeGenerateTaskSummary({
2179
- systemPrompt,
2180
- userContext,
2181
- systemContext,
2182
- toolUseContext,
2183
- forkContextMessages: [
2184
- ...messagesForQuery,
2185
- ...assistantMessages,
2186
- ...toolResults,
2187
- ],
2188
- })
2189
- }
2190
- }
2191
-
2192
- // Check if we've reached the max turns limit
2193
- if (maxTurns && nextTurnCount > maxTurns) {
2194
- yield createAttachmentMessage({
2195
- type: 'max_turns_reached',
2196
- maxTurns,
2197
- turnCount: nextTurnCount,
2198
- })
2199
- return { reason: 'max_turns', turnCount: nextTurnCount }
2200
- }
2201
-
2202
- queryCheckpoint('query_recursive_call')
2203
- const next: State = {
2204
- messages: [...messagesForQuery, ...assistantMessages, ...toolResults],
2205
- toolUseContext: toolUseContextWithQueryTracking,
2206
- autoCompactTracking: tracking,
2207
- turnCount: nextTurnCount,
2208
- maxOutputTokensRecoveryCount: 0,
2209
- hasAttemptedReactiveCompact: false,
2210
- pendingToolUseSummary: nextPendingToolUseSummary,
2211
- maxOutputTokensOverride: undefined,
2212
- stopHookActive,
2213
- transition: { reason: 'next_turn' },
2214
- }
2215
- state = next
2216
- } // while (true)
2217
- }
40
+ >