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