ummaya 0.2.13 → 0.2.15

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 (566) hide show
  1. package/npm-shrinkwrap.json +2 -2
  2. package/package.json +7 -4
  3. package/pyproject.toml +2 -2
  4. package/tui/package.json +1 -1
  5. package/tui/src/buddy/CompanionSprite.tsx +1 -2
  6. package/tui/src/buddy/useBuddyNotification.tsx +1 -2
  7. package/tui/src/cli/controlAuth.ts +20 -0
  8. package/tui/src/cli/handlers/auth.ts +7 -7
  9. package/tui/src/cli/handlers/mcp.tsx +0 -1
  10. package/tui/src/cli/handlers/util.tsx +2 -3
  11. package/tui/src/cli/print.ts +7 -4
  12. package/tui/src/commands/add-dir/add-dir.tsx +1 -2
  13. package/tui/src/commands/agents/agents.tsx +1 -2
  14. package/tui/src/commands/bridge/bridge.tsx +1 -2
  15. package/tui/src/commands/btw/btw.tsx +1 -2
  16. package/tui/src/commands/config/config.tsx +1 -2
  17. package/tui/src/commands/context/context.tsx +1 -2
  18. package/tui/src/commands/copy/copy.tsx +1 -2
  19. package/tui/src/commands/desktop/desktop.tsx +1 -2
  20. package/tui/src/commands/diff/diff.tsx +1 -2
  21. package/tui/src/commands/doctor/doctor.tsx +1 -2
  22. package/tui/src/commands/effort/effort.tsx +1 -2
  23. package/tui/src/commands/exit/exit.tsx +1 -2
  24. package/tui/src/commands/export/export.tsx +1 -2
  25. package/tui/src/commands/extra-usage/extra-usage.tsx +1 -2
  26. package/tui/src/commands/fast/fast.tsx +0 -1
  27. package/tui/src/commands/feedback/feedback.tsx +1 -2
  28. package/tui/src/commands/help/help.tsx +1 -2
  29. package/tui/src/commands/hooks/hooks.tsx +1 -2
  30. package/tui/src/commands/ide/ide.tsx +0 -1
  31. package/tui/src/commands/install-github-app/OAuthFlowStep.tsx +1 -2
  32. package/tui/src/commands/install-github-app/install-github-app.tsx +4 -4
  33. package/tui/src/commands/install.tsx +0 -1
  34. package/tui/src/commands/login/login.tsx +1 -2
  35. package/tui/src/commands/logout/logout.tsx +0 -1
  36. package/tui/src/commands/mcp/mcp.tsx +1 -2
  37. package/tui/src/commands/memory/memory.tsx +0 -1
  38. package/tui/src/commands/mobile/mobile.tsx +0 -1
  39. package/tui/src/commands/model/model.tsx +0 -1
  40. package/tui/src/commands/output-style/output-style.tsx +1 -2
  41. package/tui/src/commands/passes/passes.tsx +1 -2
  42. package/tui/src/commands/permissions/permissions.tsx +1 -2
  43. package/tui/src/commands/plan/plan.tsx +1 -2
  44. package/tui/src/commands/plugin/AddMarketplace.tsx +1 -2
  45. package/tui/src/commands/plugin/BrowseMarketplace.tsx +1 -2
  46. package/tui/src/commands/plugin/DiscoverPlugins.tsx +0 -1
  47. package/tui/src/commands/plugin/ManageMarketplaces.tsx +0 -1
  48. package/tui/src/commands/plugin/ManagePlugins.tsx +1 -2
  49. package/tui/src/commands/plugin/PluginErrors.tsx +1 -2
  50. package/tui/src/commands/plugin/PluginOptionsDialog.tsx +1 -2
  51. package/tui/src/commands/plugin/PluginOptionsFlow.tsx +1 -2
  52. package/tui/src/commands/plugin/PluginSettings.tsx +1 -2
  53. package/tui/src/commands/plugin/PluginTrustWarning.tsx +0 -1
  54. package/tui/src/commands/plugin/UnifiedInstalledCell.tsx +1 -2
  55. package/tui/src/commands/plugin/ValidatePlugin.tsx +1 -2
  56. package/tui/src/commands/plugin/index.tsx +0 -1
  57. package/tui/src/commands/plugin/plugin.tsx +1 -2
  58. package/tui/src/commands/plugin/pluginDetailsHelpers.tsx +1 -2
  59. package/tui/src/commands/privacy-settings/privacy-settings.tsx +0 -1
  60. package/tui/src/commands/rate-limit-options/rate-limit-options.tsx +1 -2
  61. package/tui/src/commands/remote-env/remote-env.tsx +1 -2
  62. package/tui/src/commands/remote-setup/remote-setup.tsx +0 -1
  63. package/tui/src/commands/resume/resume.tsx +1 -2
  64. package/tui/src/commands/review/UltrareviewOverageDialog.tsx +1 -2
  65. package/tui/src/commands/review/ultrareviewCommand.tsx +0 -1
  66. package/tui/src/commands/sandbox-toggle/sandbox-toggle.tsx +1 -2
  67. package/tui/src/commands/session/session.tsx +0 -1
  68. package/tui/src/commands/skills/skills.tsx +1 -2
  69. package/tui/src/commands/stats/stats.tsx +1 -2
  70. package/tui/src/commands/status/status.tsx +1 -2
  71. package/tui/src/commands/statusline.tsx +0 -1
  72. package/tui/src/commands/tag/tag.tsx +1 -2
  73. package/tui/src/commands/tasks/tasks.tsx +1 -2
  74. package/tui/src/commands/terminalSetup/terminalSetup.tsx +1 -2
  75. package/tui/src/commands/theme/theme.tsx +1 -2
  76. package/tui/src/commands/thinkback/thinkback.tsx +0 -1
  77. package/tui/src/commands/ultraplan.tsx +0 -1
  78. package/tui/src/commands/usage/usage.tsx +1 -2
  79. package/tui/src/commands/voice/voice.ts +2 -2
  80. package/tui/src/components/AgentProgressLine.tsx +1 -2
  81. package/tui/src/components/App.tsx +1 -2
  82. package/tui/src/components/ApproveApiKey.tsx +1 -2
  83. package/tui/src/components/AutoModeOptInDialog.tsx +0 -1
  84. package/tui/src/components/AutoUpdater.tsx +1 -2
  85. package/tui/src/components/AutoUpdaterWrapper.tsx +1 -2
  86. package/tui/src/components/AwsAuthStatusBox.tsx +1 -2
  87. package/tui/src/components/BaseTextInput.tsx +0 -1
  88. package/tui/src/components/BashModeProgress.tsx +1 -2
  89. package/tui/src/components/BridgeDialog.tsx +1 -2
  90. package/tui/src/components/BypassPermissionsModeDialog.tsx +0 -1
  91. package/tui/src/components/ChannelDowngradeDialog.tsx +1 -2
  92. package/tui/src/components/ClaudeCodeHint/PluginHintMenu.tsx +1 -2
  93. package/tui/src/components/ClaudeMdExternalIncludesDialog.tsx +0 -1
  94. package/tui/src/components/ClickableImageRef.tsx +1 -2
  95. package/tui/src/components/CompactSummary.tsx +1 -2
  96. package/tui/src/components/ConfigurableShortcutHint.tsx +1 -2
  97. package/tui/src/components/ContextSuggestions.tsx +1 -2
  98. package/tui/src/components/ContextVisualization.tsx +1 -2
  99. package/tui/src/components/CoordinatorAgentStatus.tsx +1 -2
  100. package/tui/src/components/CostThresholdDialog.tsx +0 -1
  101. package/tui/src/components/CtrlOToExpand.tsx +1 -2
  102. package/tui/src/components/CustomSelect/SelectMulti.tsx +1 -2
  103. package/tui/src/components/CustomSelect/select-input-option.tsx +1 -2
  104. package/tui/src/components/CustomSelect/select-option.tsx +1 -2
  105. package/tui/src/components/CustomSelect/select.tsx +1 -2
  106. package/tui/src/components/DesktopHandoff.tsx +0 -1
  107. package/tui/src/components/DesktopUpsell/DesktopUpsellStartup.tsx +0 -1
  108. package/tui/src/components/DevBar.tsx +1 -2
  109. package/tui/src/components/DevChannelsDialog.tsx +1 -2
  110. package/tui/src/components/DiagnosticsDisplay.tsx +1 -2
  111. package/tui/src/components/EffortCallout.tsx +1 -2
  112. package/tui/src/components/ExitFlow.tsx +1 -2
  113. package/tui/src/components/ExportDialog.tsx +1 -2
  114. package/tui/src/components/FallbackToolUseErrorMessage.tsx +0 -1
  115. package/tui/src/components/FallbackToolUseRejectedMessage.tsx +1 -2
  116. package/tui/src/components/FastIcon.tsx +1 -2
  117. package/tui/src/components/Feedback.tsx +0 -1
  118. package/tui/src/components/FeedbackSurvey/FeedbackSurvey.tsx +1 -2
  119. package/tui/src/components/FeedbackSurvey/FeedbackSurveyView.tsx +0 -1
  120. package/tui/src/components/FeedbackSurvey/TranscriptSharePrompt.tsx +0 -1
  121. package/tui/src/components/FeedbackSurvey/useFeedbackSurvey.tsx +0 -1
  122. package/tui/src/components/FeedbackSurvey/useMemorySurvey.tsx +0 -1
  123. package/tui/src/components/FeedbackSurvey/usePostCompactSurvey.tsx +0 -1
  124. package/tui/src/components/FeedbackSurvey/useSurveyState.tsx +1 -2
  125. package/tui/src/components/FileEditToolDiff.tsx +1 -2
  126. package/tui/src/components/FileEditToolUpdatedMessage.tsx +1 -2
  127. package/tui/src/components/FileEditToolUseRejectedMessage.tsx +1 -2
  128. package/tui/src/components/FilePathLink.tsx +1 -2
  129. package/tui/src/components/FullscreenLayout.tsx +1 -2
  130. package/tui/src/components/GlobalSearchDialog.tsx +1 -2
  131. package/tui/src/components/HelpV2/Commands.tsx +1 -2
  132. package/tui/src/components/HelpV2/General.tsx +0 -1
  133. package/tui/src/components/HelpV2/HelpV2.tsx +0 -1
  134. package/tui/src/components/HighlightedCode/Fallback.tsx +1 -2
  135. package/tui/src/components/HighlightedCode.tsx +1 -2
  136. package/tui/src/components/HistorySearchDialog.tsx +1 -2
  137. package/tui/src/components/IdeAutoConnectDialog.tsx +1 -2
  138. package/tui/src/components/IdeOnboardingDialog.tsx +0 -1
  139. package/tui/src/components/IdeStatusIndicator.tsx +1 -2
  140. package/tui/src/components/IdleReturnDialog.tsx +1 -2
  141. package/tui/src/components/InterruptedByUser.tsx +0 -1
  142. package/tui/src/components/InvalidConfigDialog.tsx +1 -2
  143. package/tui/src/components/InvalidSettingsDialog.tsx +1 -2
  144. package/tui/src/components/KeybindingWarnings.tsx +1 -2
  145. package/tui/src/components/LanguagePicker.tsx +1 -2
  146. package/tui/src/components/LogSelector.tsx +0 -1
  147. package/tui/src/components/LogoV2/AnimatedAsterisk.tsx +1 -2
  148. package/tui/src/components/LogoV2/AnimatedClawd.tsx +1 -2
  149. package/tui/src/components/LogoV2/ChannelsNotice.tsx +0 -1
  150. package/tui/src/components/LogoV2/Clawd.tsx +1 -2
  151. package/tui/src/components/LogoV2/CondensedLogo.tsx +0 -1
  152. package/tui/src/components/LogoV2/EmergencyTip.tsx +0 -1
  153. package/tui/src/components/LogoV2/Feed.tsx +1 -2
  154. package/tui/src/components/LogoV2/FeedColumn.tsx +1 -2
  155. package/tui/src/components/LogoV2/GuestPassesUpsell.tsx +0 -1
  156. package/tui/src/components/LogoV2/LogoV2.tsx +0 -1
  157. package/tui/src/components/LogoV2/Opus1mMergeNotice.tsx +1 -2
  158. package/tui/src/components/LogoV2/OverageCreditUpsell.tsx +1 -2
  159. package/tui/src/components/LogoV2/VoiceModeNotice.tsx +1 -2
  160. package/tui/src/components/LogoV2/feedConfigs.tsx +0 -1
  161. package/tui/src/components/LspRecommendation/LspRecommendationMenu.tsx +1 -2
  162. package/tui/src/components/MCPServerApprovalDialog.tsx +1 -2
  163. package/tui/src/components/MCPServerDesktopImportDialog.tsx +0 -1
  164. package/tui/src/components/MCPServerDialogCopy.tsx +0 -1
  165. package/tui/src/components/MCPServerMultiselectDialog.tsx +1 -2
  166. package/tui/src/components/ManagedSettingsSecurityDialog/ManagedSettingsSecurityDialog.tsx +0 -1
  167. package/tui/src/components/Markdown.tsx +1 -2
  168. package/tui/src/components/MarkdownTable.tsx +1 -2
  169. package/tui/src/components/MemoryUsageIndicator.tsx +1 -2
  170. package/tui/src/components/Message.tsx +1 -2
  171. package/tui/src/components/MessageModel.tsx +1 -2
  172. package/tui/src/components/MessageResponse.tsx +1 -2
  173. package/tui/src/components/MessageRow.tsx +1 -2
  174. package/tui/src/components/MessageSelector.tsx +1 -2
  175. package/tui/src/components/MessageTimestamp.tsx +1 -2
  176. package/tui/src/components/Messages.tsx +0 -1
  177. package/tui/src/components/ModelPicker.tsx +0 -1
  178. package/tui/src/components/NativeAutoUpdater.tsx +1 -2
  179. package/tui/src/components/NotebookEditToolUseRejectedMessage.tsx +1 -2
  180. package/tui/src/components/OffscreenFreeze.tsx +1 -2
  181. package/tui/src/components/Onboarding.tsx +2 -3
  182. package/tui/src/components/OutputStylePicker.tsx +0 -1
  183. package/tui/src/components/PackageManagerAutoUpdater.tsx +0 -1
  184. package/tui/src/components/Passes/Passes.tsx +0 -1
  185. package/tui/src/components/PrBadge.tsx +1 -2
  186. package/tui/src/components/PressEnterToContinue.tsx +1 -2
  187. package/tui/src/components/PromptInput/HistorySearchInput.tsx +1 -2
  188. package/tui/src/components/PromptInput/IssueFlagBanner.tsx +1 -2
  189. package/tui/src/components/PromptInput/Notifications.tsx +1 -2
  190. package/tui/src/components/PromptInput/PromptInput.tsx +0 -1
  191. package/tui/src/components/PromptInput/PromptInputFooter.tsx +1 -2
  192. package/tui/src/components/PromptInput/PromptInputFooterLeftSide.tsx +1 -2
  193. package/tui/src/components/PromptInput/PromptInputFooterSuggestions.tsx +1 -2
  194. package/tui/src/components/PromptInput/PromptInputHelpMenu.tsx +1 -2
  195. package/tui/src/components/PromptInput/PromptInputModeIndicator.tsx +1 -2
  196. package/tui/src/components/PromptInput/PromptInputQueuedCommands.tsx +1 -2
  197. package/tui/src/components/PromptInput/PromptInputStashNotice.tsx +1 -2
  198. package/tui/src/components/PromptInput/SandboxPromptFooterHint.tsx +1 -2
  199. package/tui/src/components/PromptInput/ShimmeredInput.tsx +1 -2
  200. package/tui/src/components/PromptInput/VoiceIndicator.tsx +1 -2
  201. package/tui/src/components/QuickOpenDialog.tsx +1 -2
  202. package/tui/src/components/RemoteCallout.tsx +0 -1
  203. package/tui/src/components/RemoteEnvironmentDialog.tsx +0 -1
  204. package/tui/src/components/ResumeTask.tsx +0 -1
  205. package/tui/src/components/SandboxViolationExpandedView.tsx +1 -2
  206. package/tui/src/components/ScrollKeybindingHandler.tsx +0 -1
  207. package/tui/src/components/SearchBox.tsx +1 -2
  208. package/tui/src/components/SessionBackgroundHint.tsx +1 -2
  209. package/tui/src/components/SessionPreview.tsx +1 -2
  210. package/tui/src/components/Settings/Config.tsx +0 -1
  211. package/tui/src/components/Settings/Settings.tsx +1 -2
  212. package/tui/src/components/Settings/Status.tsx +1 -2
  213. package/tui/src/components/Settings/Usage.tsx +1 -2
  214. package/tui/src/components/ShowInIDEPrompt.tsx +1 -2
  215. package/tui/src/components/SkillImprovementSurvey.tsx +1 -2
  216. package/tui/src/components/Spinner/FlashingChar.tsx +1 -2
  217. package/tui/src/components/Spinner/GlimmerMessage.tsx +1 -2
  218. package/tui/src/components/Spinner/ShimmerChar.tsx +1 -2
  219. package/tui/src/components/Spinner/SpinnerAnimationRow.tsx +1 -2
  220. package/tui/src/components/Spinner/SpinnerGlyph.tsx +1 -2
  221. package/tui/src/components/Spinner/TeammateSpinnerLine.tsx +1 -2
  222. package/tui/src/components/Spinner/TeammateSpinnerTree.tsx +1 -2
  223. package/tui/src/components/Spinner.tsx +0 -1
  224. package/tui/src/components/Stats.tsx +0 -1
  225. package/tui/src/components/StatusLine.tsx +1 -2
  226. package/tui/src/components/StatusNotices.tsx +1 -2
  227. package/tui/src/components/StructuredDiff/Fallback.tsx +1 -2
  228. package/tui/src/components/StructuredDiff.tsx +1 -2
  229. package/tui/src/components/StructuredDiffList.tsx +1 -2
  230. package/tui/src/components/TagTabs.tsx +1 -2
  231. package/tui/src/components/TaskListV2.tsx +1 -2
  232. package/tui/src/components/TeammateViewHeader.tsx +1 -2
  233. package/tui/src/components/TeleportError.tsx +0 -1
  234. package/tui/src/components/TeleportProgress.tsx +1 -2
  235. package/tui/src/components/TeleportRepoMismatchDialog.tsx +0 -1
  236. package/tui/src/components/TeleportStash.tsx +1 -2
  237. package/tui/src/components/TextInput.tsx +1 -2
  238. package/tui/src/components/ThemePicker.tsx +0 -1
  239. package/tui/src/components/ThinkingToggle.tsx +0 -1
  240. package/tui/src/components/TokenWarning.tsx +1 -2
  241. package/tui/src/components/ToolUseLoader.tsx +1 -2
  242. package/tui/src/components/TrustDialog/TrustDialog.tsx +0 -1
  243. package/tui/src/components/ValidationErrorsList.tsx +1 -2
  244. package/tui/src/components/VimTextInput.tsx +1 -2
  245. package/tui/src/components/VirtualMessageList.tsx +1 -2
  246. package/tui/src/components/WorktreeExitDialog.tsx +1 -2
  247. package/tui/src/components/agents/AgentDetail.tsx +0 -1
  248. package/tui/src/components/agents/AgentEditor.tsx +1 -2
  249. package/tui/src/components/agents/AgentNavigationFooter.tsx +1 -2
  250. package/tui/src/components/agents/AgentsList.tsx +0 -1
  251. package/tui/src/components/agents/AgentsMenu.tsx +1 -2
  252. package/tui/src/components/agents/ColorPicker.tsx +1 -2
  253. package/tui/src/components/agents/ModelSelector.tsx +1 -2
  254. package/tui/src/components/agents/ToolSelector.tsx +1 -2
  255. package/tui/src/components/agents/new-agent-creation/CreateAgentWizard.tsx +1 -2
  256. package/tui/src/components/agents/new-agent-creation/wizard-steps/ColorStep.tsx +1 -2
  257. package/tui/src/components/agents/new-agent-creation/wizard-steps/ConfirmStep.tsx +0 -1
  258. package/tui/src/components/agents/new-agent-creation/wizard-steps/ConfirmStepWrapper.tsx +1 -2
  259. package/tui/src/components/agents/new-agent-creation/wizard-steps/DescriptionStep.tsx +0 -1
  260. package/tui/src/components/agents/new-agent-creation/wizard-steps/GenerateStep.tsx +1 -2
  261. package/tui/src/components/agents/new-agent-creation/wizard-steps/LocationStep.tsx +1 -2
  262. package/tui/src/components/agents/new-agent-creation/wizard-steps/MemoryStep.tsx +1 -2
  263. package/tui/src/components/agents/new-agent-creation/wizard-steps/MethodStep.tsx +0 -1
  264. package/tui/src/components/agents/new-agent-creation/wizard-steps/ModelStep.tsx +1 -2
  265. package/tui/src/components/agents/new-agent-creation/wizard-steps/PromptStep.tsx +1 -2
  266. package/tui/src/components/agents/new-agent-creation/wizard-steps/ToolsStep.tsx +1 -2
  267. package/tui/src/components/agents/new-agent-creation/wizard-steps/TypeStep.tsx +1 -2
  268. package/tui/src/components/design-system/Byline.tsx +1 -2
  269. package/tui/src/components/design-system/Dialog.tsx +1 -2
  270. package/tui/src/components/design-system/Divider.tsx +1 -2
  271. package/tui/src/components/design-system/FuzzyPicker.tsx +1 -2
  272. package/tui/src/components/design-system/KeyboardShortcutHint.tsx +1 -2
  273. package/tui/src/components/design-system/ListItem.tsx +1 -2
  274. package/tui/src/components/design-system/LoadingState.tsx +0 -1
  275. package/tui/src/components/design-system/Pane.tsx +1 -2
  276. package/tui/src/components/design-system/ProgressBar.tsx +1 -2
  277. package/tui/src/components/design-system/Ratchet.tsx +1 -2
  278. package/tui/src/components/design-system/StatusIcon.tsx +1 -2
  279. package/tui/src/components/design-system/Tabs.tsx +1 -2
  280. package/tui/src/components/design-system/ThemeProvider.tsx +1 -2
  281. package/tui/src/components/design-system/ThemedBox.tsx +1 -2
  282. package/tui/src/components/design-system/ThemedText.tsx +1 -2
  283. package/tui/src/components/diff/DiffDetailView.tsx +1 -2
  284. package/tui/src/components/diff/DiffDialog.tsx +1 -2
  285. package/tui/src/components/diff/DiffFileList.tsx +1 -2
  286. package/tui/src/components/grove/Grove.tsx +0 -1
  287. package/tui/src/components/hooks/HooksConfigMenu.tsx +0 -1
  288. package/tui/src/components/hooks/PromptDialog.tsx +1 -2
  289. package/tui/src/components/hooks/SelectEventMode.tsx +0 -1
  290. package/tui/src/components/hooks/SelectHookMode.tsx +0 -1
  291. package/tui/src/components/hooks/SelectMatcherMode.tsx +0 -1
  292. package/tui/src/components/hooks/ViewHookMode.tsx +0 -1
  293. package/tui/src/components/mcp/CapabilitiesSection.tsx +1 -2
  294. package/tui/src/components/mcp/ElicitationDialog.tsx +0 -1
  295. package/tui/src/components/mcp/MCPAgentServerMenu.tsx +1 -2
  296. package/tui/src/components/mcp/MCPListPanel.tsx +0 -1
  297. package/tui/src/components/mcp/MCPReconnect.tsx +1 -2
  298. package/tui/src/components/mcp/MCPRemoteServerMenu.tsx +2 -3
  299. package/tui/src/components/mcp/MCPSettings.tsx +0 -1
  300. package/tui/src/components/mcp/MCPStdioServerMenu.tsx +1 -2
  301. package/tui/src/components/mcp/MCPToolDetailView.tsx +1 -2
  302. package/tui/src/components/mcp/MCPToolListView.tsx +1 -2
  303. package/tui/src/components/mcp/McpParsingWarnings.tsx +0 -1
  304. package/tui/src/components/mcp/utils/reconnectHelpers.tsx +1 -2
  305. package/tui/src/components/memory/MemoryFileSelector.tsx +1 -2
  306. package/tui/src/components/memory/MemoryUpdateNotification.tsx +1 -2
  307. package/tui/src/components/messageActions.tsx +1 -2
  308. package/tui/src/components/messages/AdvisorMessage.tsx +1 -2
  309. package/tui/src/components/messages/AssistantRedactedThinkingMessage.tsx +1 -2
  310. package/tui/src/components/messages/AssistantThinkingMessage.tsx +1 -2
  311. package/tui/src/components/messages/AssistantToolUseMessage.tsx +0 -1
  312. package/tui/src/components/messages/AttachmentMessage.tsx +1 -2
  313. package/tui/src/components/messages/CollapsedReadSearchContent.tsx +1 -2
  314. package/tui/src/components/messages/CompactBoundaryMessage.tsx +1 -2
  315. package/tui/src/components/messages/GroupedToolUseContent.tsx +1 -2
  316. package/tui/src/components/messages/HighlightedThinkingText.tsx +1 -2
  317. package/tui/src/components/messages/HookProgressMessage.tsx +1 -2
  318. package/tui/src/components/messages/PlanApprovalMessage.tsx +1 -2
  319. package/tui/src/components/messages/RateLimitMessage.tsx +1 -2
  320. package/tui/src/components/messages/ShutdownMessage.tsx +1 -2
  321. package/tui/src/components/messages/SystemAPIErrorMessage.tsx +1 -2
  322. package/tui/src/components/messages/SystemTextMessage.tsx +1 -2
  323. package/tui/src/components/messages/TaskAssignmentMessage.tsx +1 -2
  324. package/tui/src/components/messages/UserAgentNotificationMessage.tsx +1 -2
  325. package/tui/src/components/messages/UserBashInputMessage.tsx +1 -2
  326. package/tui/src/components/messages/UserBashOutputMessage.tsx +1 -2
  327. package/tui/src/components/messages/UserChannelMessage.tsx +1 -2
  328. package/tui/src/components/messages/UserCommandMessage.tsx +1 -2
  329. package/tui/src/components/messages/UserImageMessage.tsx +1 -2
  330. package/tui/src/components/messages/UserLocalCommandOutputMessage.tsx +1 -2
  331. package/tui/src/components/messages/UserMemoryInputMessage.tsx +1 -2
  332. package/tui/src/components/messages/UserPlanMessage.tsx +1 -2
  333. package/tui/src/components/messages/UserPromptMessage.tsx +1 -2
  334. package/tui/src/components/messages/UserResourceUpdateMessage.tsx +1 -2
  335. package/tui/src/components/messages/UserTeammateMessage.tsx +1 -2
  336. package/tui/src/components/messages/UserTextMessage.tsx +1 -2
  337. package/tui/src/components/messages/UserToolResultMessage/RejectedPlanMessage.tsx +0 -1
  338. package/tui/src/components/messages/UserToolResultMessage/RejectedToolUseMessage.tsx +1 -2
  339. package/tui/src/components/messages/UserToolResultMessage/UserToolCanceledMessage.tsx +1 -2
  340. package/tui/src/components/messages/UserToolResultMessage/UserToolErrorMessage.tsx +1 -2
  341. package/tui/src/components/messages/UserToolResultMessage/UserToolRejectMessage.tsx +1 -2
  342. package/tui/src/components/messages/UserToolResultMessage/UserToolResultMessage.tsx +1 -2
  343. package/tui/src/components/messages/UserToolResultMessage/UserToolSuccessMessage.tsx +1 -2
  344. package/tui/src/components/messages/UserToolResultMessage/utils.tsx +0 -1
  345. package/tui/src/components/messages/teamMemCollapsed.tsx +1 -2
  346. package/tui/src/components/permissions/AskUserQuestionPermissionRequest/AskUserQuestionPermissionRequest.tsx +1 -2
  347. package/tui/src/components/permissions/AskUserQuestionPermissionRequest/PreviewBox.tsx +1 -2
  348. package/tui/src/components/permissions/AskUserQuestionPermissionRequest/PreviewQuestionView.tsx +1 -2
  349. package/tui/src/components/permissions/AskUserQuestionPermissionRequest/QuestionNavigationBar.tsx +1 -2
  350. package/tui/src/components/permissions/AskUserQuestionPermissionRequest/QuestionView.tsx +1 -2
  351. package/tui/src/components/permissions/AskUserQuestionPermissionRequest/SubmitQuestionsView.tsx +1 -2
  352. package/tui/src/components/permissions/BashPermissionRequest/BashPermissionRequest.tsx +1 -2
  353. package/tui/src/components/permissions/BashPermissionRequest/bashToolUseOptions.tsx +0 -1
  354. package/tui/src/components/permissions/ComputerUseApproval/ComputerUseApproval.tsx +0 -1
  355. package/tui/src/components/permissions/EnterPlanModePermissionRequest/EnterPlanModePermissionRequest.tsx +0 -1
  356. package/tui/src/components/permissions/ExitPlanModePermissionRequest/ExitPlanModePermissionRequest.tsx +0 -1
  357. package/tui/src/components/permissions/FallbackPermissionRequest.tsx +1 -2
  358. package/tui/src/components/permissions/FileEditPermissionRequest/FileEditPermissionRequest.tsx +1 -2
  359. package/tui/src/components/permissions/FilePermissionDialog/FilePermissionDialog.tsx +1 -2
  360. package/tui/src/components/permissions/FilePermissionDialog/permissionOptions.tsx +0 -1
  361. package/tui/src/components/permissions/FileWritePermissionRequest/FileWritePermissionRequest.tsx +1 -2
  362. package/tui/src/components/permissions/FileWritePermissionRequest/FileWriteToolDiff.tsx +1 -2
  363. package/tui/src/components/permissions/FilesystemPermissionRequest/FilesystemPermissionRequest.tsx +1 -2
  364. package/tui/src/components/permissions/NotebookEditPermissionRequest/NotebookEditPermissionRequest.tsx +1 -2
  365. package/tui/src/components/permissions/NotebookEditPermissionRequest/NotebookEditToolDiff.tsx +1 -2
  366. package/tui/src/components/permissions/PermissionDecisionDebugInfo.tsx +1 -2
  367. package/tui/src/components/permissions/PermissionDialog.tsx +1 -2
  368. package/tui/src/components/permissions/PermissionExplanation.tsx +1 -2
  369. package/tui/src/components/permissions/PermissionPrompt.tsx +0 -1
  370. package/tui/src/components/permissions/PermissionRequest.tsx +0 -1
  371. package/tui/src/components/permissions/PermissionRequestTitle.tsx +1 -2
  372. package/tui/src/components/permissions/PermissionRuleExplanation.tsx +1 -2
  373. package/tui/src/components/permissions/PowerShellPermissionRequest/PowerShellPermissionRequest.tsx +1 -2
  374. package/tui/src/components/permissions/PowerShellPermissionRequest/powershellToolUseOptions.tsx +0 -1
  375. package/tui/src/components/permissions/SandboxPermissionRequest.tsx +0 -1
  376. package/tui/src/components/permissions/SedEditPermissionRequest/SedEditPermissionRequest.tsx +1 -2
  377. package/tui/src/components/permissions/SkillPermissionRequest/SkillPermissionRequest.tsx +0 -1
  378. package/tui/src/components/permissions/WebFetchPermissionRequest/WebFetchPermissionRequest.tsx +0 -1
  379. package/tui/src/components/permissions/WorkerBadge.tsx +1 -2
  380. package/tui/src/components/permissions/WorkerPendingPermission.tsx +1 -2
  381. package/tui/src/components/permissions/rules/AddPermissionRules.tsx +0 -1
  382. package/tui/src/components/permissions/rules/AddWorkspaceDirectory.tsx +0 -1
  383. package/tui/src/components/permissions/rules/PermissionRuleDescription.tsx +1 -2
  384. package/tui/src/components/permissions/rules/PermissionRuleInput.tsx +1 -2
  385. package/tui/src/components/permissions/rules/PermissionRuleList.tsx +0 -1
  386. package/tui/src/components/permissions/rules/RecentDenialsTab.tsx +1 -2
  387. package/tui/src/components/permissions/rules/RemoveWorkspaceDirectory.tsx +0 -1
  388. package/tui/src/components/permissions/rules/WorkspaceTab.tsx +1 -2
  389. package/tui/src/components/permissions/shellPermissionHelpers.tsx +1 -2
  390. package/tui/src/components/sandbox/SandboxConfigTab.tsx +1 -2
  391. package/tui/src/components/sandbox/SandboxDependenciesTab.tsx +1 -2
  392. package/tui/src/components/sandbox/SandboxDoctorSection.tsx +1 -2
  393. package/tui/src/components/sandbox/SandboxOverridesTab.tsx +0 -1
  394. package/tui/src/components/sandbox/SandboxSettings.tsx +0 -1
  395. package/tui/src/components/shell/ExpandShellOutputContext.tsx +1 -2
  396. package/tui/src/components/shell/OutputLine.tsx +1 -2
  397. package/tui/src/components/shell/ShellProgressMessage.tsx +1 -2
  398. package/tui/src/components/shell/ShellTimeDisplay.tsx +1 -2
  399. package/tui/src/components/skills/SkillsMenu.tsx +0 -1
  400. package/tui/src/components/tasks/AsyncAgentDetailDialog.tsx +1 -2
  401. package/tui/src/components/tasks/BackgroundTask.tsx +1 -2
  402. package/tui/src/components/tasks/BackgroundTaskStatus.tsx +1 -2
  403. package/tui/src/components/tasks/BackgroundTasksDialog.tsx +1 -2
  404. package/tui/src/components/tasks/DreamDetailDialog.tsx +1 -2
  405. package/tui/src/components/tasks/InProcessTeammateDetailDialog.tsx +1 -2
  406. package/tui/src/components/tasks/RemoteSessionDetailDialog.tsx +0 -1
  407. package/tui/src/components/tasks/RemoteSessionProgress.tsx +1 -2
  408. package/tui/src/components/tasks/ShellDetailDialog.tsx +1 -2
  409. package/tui/src/components/tasks/ShellProgress.tsx +1 -2
  410. package/tui/src/components/tasks/renderToolActivity.tsx +1 -2
  411. package/tui/src/components/tasks/taskStatusUtils.tsx +1 -2
  412. package/tui/src/components/teams/TeamStatus.tsx +1 -2
  413. package/tui/src/components/teams/TeamsDialog.tsx +1 -2
  414. package/tui/src/components/ui/OrderedList.tsx +1 -2
  415. package/tui/src/components/ui/OrderedListItem.tsx +1 -2
  416. package/tui/src/components/ui/TreeSelect.tsx +1 -2
  417. package/tui/src/components/wizard/WizardDialogLayout.tsx +1 -2
  418. package/tui/src/components/wizard/WizardNavigationFooter.tsx +1 -2
  419. package/tui/src/components/wizard/WizardProvider.tsx +1 -2
  420. package/tui/src/context/QueuedMessageContext.tsx +1 -2
  421. package/tui/src/context/fpsMetrics.tsx +1 -2
  422. package/tui/src/context/mailbox.tsx +1 -2
  423. package/tui/src/context/modalContext.tsx +1 -2
  424. package/tui/src/context/notifications.tsx +1 -2
  425. package/tui/src/context/overlayContext.tsx +1 -2
  426. package/tui/src/context/promptOverlayContext.tsx +1 -2
  427. package/tui/src/context/stats.tsx +1 -2
  428. package/tui/src/context/voice.tsx +1 -2
  429. package/tui/src/dialogLaunchers.tsx +0 -1
  430. package/tui/src/entrypoints/cli.tsx +0 -1
  431. package/tui/src/hooks/notifs/useCanSwitchToExistingSubscription.tsx +0 -1
  432. package/tui/src/hooks/notifs/useDeprecationWarningNotification.tsx +1 -2
  433. package/tui/src/hooks/notifs/useFastModeNotification.tsx +1 -2
  434. package/tui/src/hooks/notifs/useIDEStatusIndicator.tsx +1 -2
  435. package/tui/src/hooks/notifs/useInstallMessages.tsx +1 -2
  436. package/tui/src/hooks/notifs/useLspInitializationNotification.tsx +1 -2
  437. package/tui/src/hooks/notifs/useMcpConnectivityStatus.tsx +0 -1
  438. package/tui/src/hooks/notifs/useModelMigrationNotifications.tsx +1 -2
  439. package/tui/src/hooks/notifs/useNpmDeprecationNotification.tsx +1 -2
  440. package/tui/src/hooks/notifs/usePluginAutoupdateNotification.tsx +1 -2
  441. package/tui/src/hooks/notifs/usePluginInstallationStatus.tsx +1 -2
  442. package/tui/src/hooks/notifs/useRateLimitWarningNotification.tsx +1 -2
  443. package/tui/src/hooks/notifs/useSettingsErrors.tsx +1 -2
  444. package/tui/src/hooks/useApiKeyVerification.ts +7 -7
  445. package/tui/src/hooks/useArrowKeyHistory.tsx +1 -2
  446. package/tui/src/hooks/useCanUseTool.tsx +0 -1
  447. package/tui/src/hooks/useChromeExtensionNotification.tsx +0 -1
  448. package/tui/src/hooks/useClaudeCodeHintRecommendation.tsx +1 -2
  449. package/tui/src/hooks/useCommandKeybindings.tsx +1 -2
  450. package/tui/src/hooks/useGlobalKeybindings.tsx +1 -2
  451. package/tui/src/hooks/useIDEIntegration.tsx +1 -2
  452. package/tui/src/hooks/useLspPluginRecommendation.tsx +1 -2
  453. package/tui/src/hooks/useOfficialMarketplaceNotification.tsx +0 -1
  454. package/tui/src/hooks/usePluginRecommendationBase.tsx +1 -2
  455. package/tui/src/hooks/usePromptsFromClaudeInChrome.tsx +1 -2
  456. package/tui/src/hooks/useReplBridge.tsx +1 -2
  457. package/tui/src/hooks/useTypeahead.tsx +0 -1
  458. package/tui/src/hooks/useVoiceIntegration.tsx +1 -2
  459. package/tui/src/ink/Ansi.tsx +1 -2
  460. package/tui/src/ink/components/AlternateScreen.tsx +1 -2
  461. package/tui/src/ink/components/App.tsx +0 -1
  462. package/tui/src/ink/components/Box.tsx +1 -2
  463. package/tui/src/ink/components/Button.tsx +1 -2
  464. package/tui/src/ink/components/ClockContext.tsx +1 -2
  465. package/tui/src/ink/components/ErrorOverview.tsx +1 -2
  466. package/tui/src/ink/components/Link.tsx +1 -2
  467. package/tui/src/ink/components/Newline.tsx +1 -2
  468. package/tui/src/ink/components/NoSelect.tsx +1 -2
  469. package/tui/src/ink/components/RawAnsi.tsx +1 -2
  470. package/tui/src/ink/components/ScrollBox.tsx +1 -2
  471. package/tui/src/ink/components/Spacer.tsx +1 -2
  472. package/tui/src/ink/components/TerminalFocusContext.tsx +1 -2
  473. package/tui/src/ink/components/TerminalSizeContext.tsx +1 -2
  474. package/tui/src/ink/components/Text.tsx +1 -2
  475. package/tui/src/ink/ink.tsx +0 -1
  476. package/tui/src/interactiveHelpers.tsx +0 -1
  477. package/tui/src/keybindings/KeybindingContext.tsx +1 -2
  478. package/tui/src/keybindings/KeybindingProviderSetup.tsx +1 -2
  479. package/tui/src/main.tsx +0 -1
  480. package/tui/src/moreright/useMoreRight.tsx +1 -2
  481. package/tui/src/replLauncher.tsx +1 -2
  482. package/tui/src/screens/Doctor.tsx +0 -1
  483. package/tui/src/screens/REPL.tsx +0 -1
  484. package/tui/src/screens/ResumeConversation.tsx +0 -1
  485. package/tui/src/services/mcp/MCPConnectionManager.tsx +1 -2
  486. package/tui/src/services/mcp/claudeai.ts +1 -1
  487. package/tui/src/services/mcpServerApproval.tsx +1 -2
  488. package/tui/src/services/oauth/client.ts +16 -14
  489. package/tui/src/services/oauth/getOauthProfile.ts +2 -2
  490. package/tui/src/services/oauth/index.ts +12 -16
  491. package/tui/src/services/policyLimits/index.ts +3 -3
  492. package/tui/src/services/remoteManagedSettings/index.ts +3 -3
  493. package/tui/src/services/remoteManagedSettings/securityCheck.tsx +1 -2
  494. package/tui/src/services/remoteManagedSettings/syncCache.ts +3 -3
  495. package/tui/src/services/voiceStreamSTT.ts +2 -2
  496. package/tui/src/state/AppState.tsx +1 -2
  497. package/tui/src/tasks/InProcessTeammateTask/InProcessTeammateTask.tsx +1 -2
  498. package/tui/src/tasks/LocalAgentTask/LocalAgentTask.tsx +1 -2
  499. package/tui/src/tasks/LocalShellTask/LocalShellTask.tsx +1 -2
  500. package/tui/src/tasks/RemoteAgentTask/RemoteAgentTask.tsx +0 -1
  501. package/tui/src/tools/AgentTool/UI.tsx +1 -2
  502. package/tui/src/tools/BashTool/BashToolResultMessage.tsx +0 -1
  503. package/tui/src/tools/BashTool/UI.tsx +1 -2
  504. package/tui/src/tools/BriefTool/UI.tsx +0 -1
  505. package/tui/src/tools/ConfigTool/ConfigTool.ts +2 -2
  506. package/tui/src/tools/ConfigTool/UI.tsx +1 -2
  507. package/tui/src/tools/EnterPlanModeTool/UI.tsx +0 -1
  508. package/tui/src/tools/EnterWorktreeTool/UI.tsx +1 -2
  509. package/tui/src/tools/ExitPlanModeTool/UI.tsx +0 -1
  510. package/tui/src/tools/ExitWorktreeTool/UI.tsx +1 -2
  511. package/tui/src/tools/FileEditTool/UI.tsx +1 -2
  512. package/tui/src/tools/FileReadTool/UI.tsx +1 -2
  513. package/tui/src/tools/FileWriteTool/UI.tsx +1 -2
  514. package/tui/src/tools/GlobTool/UI.tsx +1 -2
  515. package/tui/src/tools/GrepTool/UI.tsx +1 -2
  516. package/tui/src/tools/LSPTool/UI.tsx +1 -2
  517. package/tui/src/tools/ListMcpResourcesTool/UI.tsx +1 -2
  518. package/tui/src/tools/MCPTool/UI.tsx +1 -2
  519. package/tui/src/tools/NotebookEditTool/UI.tsx +1 -2
  520. package/tui/src/tools/PowerShellTool/UI.tsx +0 -1
  521. package/tui/src/tools/ReadMcpResourceTool/UI.tsx +1 -2
  522. package/tui/src/tools/RemoteTriggerTool/UI.tsx +1 -2
  523. package/tui/src/tools/ScheduleCronTool/UI.tsx +1 -2
  524. package/tui/src/tools/SendMessageTool/UI.tsx +1 -2
  525. package/tui/src/tools/SkillTool/UI.tsx +1 -2
  526. package/tui/src/tools/TaskStopTool/UI.tsx +1 -2
  527. package/tui/src/tools/TeamCreateTool/UI.tsx +1 -2
  528. package/tui/src/tools/TeamDeleteTool/UI.tsx +1 -2
  529. package/tui/src/tools/WebFetchTool/UI.tsx +1 -2
  530. package/tui/src/tools/testing/TestingPermissionTool.tsx +1 -2
  531. package/tui/src/utils/auth.ts +8 -8
  532. package/tui/src/utils/autoRunIssue.tsx +1 -2
  533. package/tui/src/utils/billing.ts +2 -2
  534. package/tui/src/utils/computerUse/toolRendering.tsx +1 -2
  535. package/tui/src/utils/computerUse/wrapper.tsx +0 -1
  536. package/tui/src/utils/exportRenderer.tsx +1 -2
  537. package/tui/src/utils/fastMode.ts +2 -2
  538. package/tui/src/utils/highlightMatch.tsx +1 -2
  539. package/tui/src/utils/http.ts +2 -2
  540. package/tui/src/utils/managedEnv.ts +1 -1
  541. package/tui/src/utils/messages/systemInit.ts +2 -2
  542. package/tui/src/utils/plugins/performStartupChecks.tsx +1 -2
  543. package/tui/src/utils/preflightChecks.tsx +0 -1
  544. package/tui/src/utils/processUserInput/processBashCommand.tsx +1 -2
  545. package/tui/src/utils/processUserInput/processSlashCommand.tsx +0 -1
  546. package/tui/src/utils/staticRender.tsx +1 -2
  547. package/tui/src/utils/status.tsx +1 -2
  548. package/tui/src/utils/statusNoticeDefinitions.tsx +5 -6
  549. package/tui/src/utils/swarm/It2SetupPrompt.tsx +1 -2
  550. package/tui/src/utils/teleport.tsx +0 -1
  551. package/tui/src/voice/voiceModeEnabled.ts +3 -3
  552. package/uv.lock +1 -1
  553. package/src/ummaya/llm/_cc_reference/README.md +0 -104
  554. package/src/ummaya/llm/_cc_reference/api.ts +0 -721
  555. package/src/ummaya/llm/_cc_reference/claude.ts +0 -3422
  556. package/src/ummaya/llm/_cc_reference/client.ts +0 -392
  557. package/src/ummaya/llm/_cc_reference/emptyUsage.ts +0 -25
  558. package/src/ummaya/llm/_cc_reference/errors.ts +0 -1210
  559. package/src/ummaya/llm/_cc_reference/messages.ts +0 -5515
  560. package/src/ummaya/llm/_cc_reference/permissions.ts +0 -1489
  561. package/src/ummaya/llm/_cc_reference/prompts.ts +0 -917
  562. package/src/ummaya/llm/_cc_reference/query.ts +0 -1732
  563. package/src/ummaya/llm/_cc_reference/toolExecution.ts +0 -1748
  564. package/src/ummaya/llm/_cc_reference/toolOrchestration.ts +0 -191
  565. package/src/ummaya/llm/_cc_reference/toolResultStorage.ts +0 -1043
  566. package/src/ummaya/llm/_cc_reference/tools.ts +0 -392
@@ -1,1732 +0,0 @@
1
- // SPDX-License-Identifier: Apache-2.0 (Anthropic upstream) — research-use mirror
2
- // Source: .references/claude-code-sourcemap/restored-src/src/query.ts (CC 2.1.88)
3
-
4
- // biome-ignore-all assist/source/organizeImports: ANT-ONLY import markers must not be reordered
5
- import type {
6
- ToolResultBlockParam,
7
- ToolUseBlock,
8
- } from '@anthropic-ai/sdk/resources/index.mjs'
9
- import type { CanUseToolFn } from './hooks/useCanUseTool.js'
10
- import { FallbackTriggeredError } from './services/api/withRetry.js'
11
- import {
12
- calculateTokenWarningState,
13
- isAutoCompactEnabled,
14
- type AutoCompactTrackingState,
15
- } from './services/compact/autoCompact.js'
16
- import { buildPostCompactMessages } from './services/compact/compact.js'
17
- /* eslint-disable @typescript-eslint/no-require-imports */
18
- const reactiveCompact = feature('REACTIVE_COMPACT')
19
- ? (require('./services/compact/reactiveCompact.js') as typeof import('./services/compact/reactiveCompact.js'))
20
- : null
21
- const contextCollapse = feature('CONTEXT_COLLAPSE')
22
- ? (require('./services/contextCollapse/index.js') as typeof import('./services/contextCollapse/index.js'))
23
- : null
24
- /* eslint-enable @typescript-eslint/no-require-imports */
25
- import {
26
- logEvent,
27
- type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
28
- } from 'src/services/analytics/index.js'
29
- import { ImageSizeError } from './utils/imageValidation.js'
30
- import { ImageResizeError } from './utils/imageResizer.js'
31
- import { findToolByName, type ToolUseContext } from './Tool.js'
32
- import { asSystemPrompt, type SystemPrompt } from './utils/systemPromptType.js'
33
- import type {
34
- AssistantMessage,
35
- AttachmentMessage,
36
- Message,
37
- RequestStartEvent,
38
- StreamEvent,
39
- ToolUseSummaryMessage,
40
- UserMessage,
41
- TombstoneMessage,
42
- } from './types/message.js'
43
- import { logError } from './utils/log.js'
44
- import {
45
- PROMPT_TOO_LONG_ERROR_MESSAGE,
46
- isPromptTooLongMessage,
47
- } from './services/api/errors.js'
48
- import { logAntError, logForDebugging } from './utils/debug.js'
49
- import {
50
- createUserMessage,
51
- createUserInterruptionMessage,
52
- normalizeMessagesForAPI,
53
- createSystemMessage,
54
- createAssistantAPIErrorMessage,
55
- getMessagesAfterCompactBoundary,
56
- createToolUseSummaryMessage,
57
- createMicrocompactBoundaryMessage,
58
- stripSignatureBlocks,
59
- } from './utils/messages.js'
60
- import { generateToolUseSummary } from './services/toolUseSummary/toolUseSummaryGenerator.js'
61
- import { prependUserContext, appendSystemContext } from './utils/api.js'
62
- import {
63
- createAttachmentMessage,
64
- filterDuplicateMemoryAttachments,
65
- getAttachmentMessages,
66
- startRelevantMemoryPrefetch,
67
- } from './utils/attachments.js'
68
- /* eslint-disable @typescript-eslint/no-require-imports */
69
- const skillPrefetch = feature('EXPERIMENTAL_SKILL_SEARCH')
70
- ? (require('./services/skillSearch/prefetch.js') as typeof import('./services/skillSearch/prefetch.js'))
71
- : null
72
- const jobClassifier = feature('TEMPLATES')
73
- ? (require('./jobs/classifier.js') as typeof import('./jobs/classifier.js'))
74
- : null
75
- /* eslint-enable @typescript-eslint/no-require-imports */
76
- import {
77
- remove as removeFromQueue,
78
- getCommandsByMaxPriority,
79
- isSlashCommand,
80
- } from './utils/messageQueueManager.js'
81
- import { notifyCommandLifecycle } from './utils/commandLifecycle.js'
82
- import { headlessProfilerCheckpoint } from './utils/headlessProfiler.js'
83
- import {
84
- getRuntimeMainLoopModel,
85
- renderModelName,
86
- } from './utils/model/model.js'
87
- import {
88
- doesMostRecentAssistantMessageExceed200k,
89
- finalContextTokensFromLastResponse,
90
- tokenCountWithEstimation,
91
- } from './utils/tokens.js'
92
- import { ESCALATED_MAX_TOKENS } from './utils/context.js'
93
- import { getFeatureValue_CACHED_MAY_BE_STALE } from './services/analytics/growthbook.js'
94
- import { SLEEP_TOOL_NAME } from './tools/SleepTool/prompt.js'
95
- import { executePostSamplingHooks } from './utils/hooks/postSamplingHooks.js'
96
- import { executeStopFailureHooks } from './utils/hooks.js'
97
- import type { QuerySource } from './constants/querySource.js'
98
- import { createDumpPromptsFetch } from './services/api/dumpPrompts.js'
99
- import { StreamingToolExecutor } from './services/tools/StreamingToolExecutor.js'
100
- import { queryCheckpoint } from './utils/queryProfiler.js'
101
- import { runTools } from './services/tools/toolOrchestration.js'
102
- import { applyToolResultBudget } from './utils/toolResultStorage.js'
103
- import { recordContentReplacement } from './utils/sessionStorage.js'
104
- import { handleStopHooks } from './query/stopHooks.js'
105
- import { buildQueryConfig } from './query/config.js'
106
- import { productionDeps, type QueryDeps } from './query/deps.js'
107
- import type { Terminal, Continue } from './query/transitions.js'
108
- import { feature } from 'bun:bundle'
109
- import {
110
- getCurrentTurnTokenBudget,
111
- getTurnOutputTokens,
112
- incrementBudgetContinuationCount,
113
- } from './bootstrap/state.js'
114
- import { createBudgetTracker, checkTokenBudget } from './query/tokenBudget.js'
115
- import { count } from './utils/array.js'
116
-
117
- /* eslint-disable @typescript-eslint/no-require-imports */
118
- const snipModule = feature('HISTORY_SNIP')
119
- ? (require('./services/compact/snipCompact.js') as typeof import('./services/compact/snipCompact.js'))
120
- : null
121
- const taskSummaryModule = feature('BG_SESSIONS')
122
- ? (require('./utils/taskSummary.js') as typeof import('./utils/taskSummary.js'))
123
- : null
124
- /* eslint-enable @typescript-eslint/no-require-imports */
125
-
126
- function* yieldMissingToolResultBlocks(
127
- assistantMessages: AssistantMessage[],
128
- errorMessage: string,
129
- ) {
130
- for (const assistantMessage of assistantMessages) {
131
- // Extract all tool use blocks from this assistant message
132
- const toolUseBlocks = assistantMessage.message.content.filter(
133
- content => content.type === 'tool_use',
134
- ) as ToolUseBlock[]
135
-
136
- // Emit an interruption message for each tool use
137
- for (const toolUse of toolUseBlocks) {
138
- yield createUserMessage({
139
- content: [
140
- {
141
- type: 'tool_result',
142
- content: errorMessage,
143
- is_error: true,
144
- tool_use_id: toolUse.id,
145
- },
146
- ],
147
- toolUseResult: errorMessage,
148
- sourceToolAssistantUUID: assistantMessage.uuid,
149
- })
150
- }
151
- }
152
- }
153
-
154
- /**
155
- * The rules of thinking are lengthy and fortuitous. They require plenty of thinking
156
- * of most long duration and deep meditation for a wizard to wrap one's noggin around.
157
- *
158
- * The rules follow:
159
- * 1. A message that contains a thinking or redacted_thinking block must be part of a query whose max_thinking_length > 0
160
- * 2. A thinking block may not be the last message in a block
161
- * 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)
162
- *
163
- * Heed these rules well, young wizard. For they are the rules of thinking, and
164
- * the rules of thinking are the rules of the universe. If ye does not heed these
165
- * rules, ye will be punished with an entire day of debugging and hair pulling.
166
- */
167
- const MAX_OUTPUT_TOKENS_RECOVERY_LIMIT = 3
168
-
169
- /**
170
- * Is this a max_output_tokens error message? If so, the streaming loop should
171
- * withhold it from SDK callers until we know whether the recovery loop can
172
- * continue. Yielding early leaks an intermediate error to SDK callers (e.g.
173
- * cowork/desktop) that terminate the session on any `error` field — the
174
- * recovery loop keeps running but nobody is listening.
175
- *
176
- * Mirrors reactiveCompact.isWithheldPromptTooLong.
177
- */
178
- function isWithheldMaxOutputTokens(
179
- msg: Message | StreamEvent | undefined,
180
- ): msg is AssistantMessage {
181
- return msg?.type === 'assistant' && msg.apiError === 'max_output_tokens'
182
- }
183
-
184
- export type QueryParams = {
185
- messages: Message[]
186
- systemPrompt: SystemPrompt
187
- userContext: { [k: string]: string }
188
- systemContext: { [k: string]: string }
189
- canUseTool: CanUseToolFn
190
- toolUseContext: ToolUseContext
191
- fallbackModel?: string
192
- querySource: QuerySource
193
- maxOutputTokensOverride?: number
194
- maxTurns?: number
195
- skipCacheWrite?: boolean
196
- // API task_budget (output_config.task_budget, beta task-budgets-2026-03-13).
197
- // Distinct from the tokenBudget +500k auto-continue feature. `total` is the
198
- // budget for the whole agentic turn; `remaining` is computed per iteration
199
- // from cumulative API usage. See configureTaskBudgetParams in claude.ts.
200
- taskBudget?: { total: number }
201
- deps?: QueryDeps
202
- }
203
-
204
- // -- query loop state
205
-
206
- // Mutable state carried between loop iterations
207
- type State = {
208
- messages: Message[]
209
- toolUseContext: ToolUseContext
210
- autoCompactTracking: AutoCompactTrackingState | undefined
211
- maxOutputTokensRecoveryCount: number
212
- hasAttemptedReactiveCompact: boolean
213
- maxOutputTokensOverride: number | undefined
214
- pendingToolUseSummary: Promise<ToolUseSummaryMessage | null> | undefined
215
- stopHookActive: boolean | undefined
216
- turnCount: number
217
- // Why the previous iteration continued. Undefined on first iteration.
218
- // Lets tests assert recovery paths fired without inspecting message contents.
219
- transition: Continue | undefined
220
- }
221
-
222
- export async function* query(
223
- params: QueryParams,
224
- ): AsyncGenerator<
225
- | StreamEvent
226
- | RequestStartEvent
227
- | Message
228
- | TombstoneMessage
229
- | ToolUseSummaryMessage,
230
- Terminal
231
- > {
232
- const consumedCommandUuids: string[] = []
233
- const terminal = yield* queryLoop(params, consumedCommandUuids)
234
- // Only reached if queryLoop returned normally. Skipped on throw (error
235
- // propagates through yield*) and on .return() (Return completion closes
236
- // both generators). This gives the same asymmetric started-without-completed
237
- // signal as print.ts's drainCommandQueue when the turn fails.
238
- for (const uuid of consumedCommandUuids) {
239
- notifyCommandLifecycle(uuid, 'completed')
240
- }
241
- return terminal
242
- }
243
-
244
- async function* queryLoop(
245
- params: QueryParams,
246
- consumedCommandUuids: string[],
247
- ): AsyncGenerator<
248
- | StreamEvent
249
- | RequestStartEvent
250
- | Message
251
- | TombstoneMessage
252
- | ToolUseSummaryMessage,
253
- Terminal
254
- > {
255
- // Immutable params — never reassigned during the query loop.
256
- const {
257
- systemPrompt,
258
- userContext,
259
- systemContext,
260
- canUseTool,
261
- fallbackModel,
262
- querySource,
263
- maxTurns,
264
- skipCacheWrite,
265
- } = params
266
- const deps = params.deps ?? productionDeps()
267
-
268
- // Mutable cross-iteration state. The loop body destructures this at the top
269
- // of each iteration so reads stay bare-name (`messages`, `toolUseContext`).
270
- // Continue sites write `state = { ... }` instead of 9 separate assignments.
271
- let state: State = {
272
- messages: params.messages,
273
- toolUseContext: params.toolUseContext,
274
- maxOutputTokensOverride: params.maxOutputTokensOverride,
275
- autoCompactTracking: undefined,
276
- stopHookActive: undefined,
277
- maxOutputTokensRecoveryCount: 0,
278
- hasAttemptedReactiveCompact: false,
279
- turnCount: 1,
280
- pendingToolUseSummary: undefined,
281
- transition: undefined,
282
- }
283
- const budgetTracker = feature('TOKEN_BUDGET') ? createBudgetTracker() : null
284
-
285
- // task_budget.remaining tracking across compaction boundaries. Undefined
286
- // until first compact fires — while context is uncompacted the server can
287
- // see the full history and handles the countdown from {total} itself (see
288
- // api/api/sampling/prompt/renderer.py:292). After a compact, the server sees
289
- // only the summary and would under-count spend; remaining tells it the
290
- // pre-compact final window that got summarized away. Cumulative across
291
- // multiple compacts: each subtracts the final context at that compact's
292
- // trigger point. Loop-local (not on State) to avoid touching the 7 continue
293
- // sites.
294
- let taskBudgetRemaining: number | undefined = undefined
295
-
296
- // Snapshot immutable env/statsig/session state once at entry. See QueryConfig
297
- // for what's included and why feature() gates are intentionally excluded.
298
- const config = buildQueryConfig()
299
-
300
- // Fired once per user turn — the prompt is invariant across loop iterations,
301
- // so per-iteration firing would ask sideQuery the same question N times.
302
- // Consume point polls settledAt (never blocks). `using` disposes on all
303
- // generator exit paths — see MemoryPrefetch for dispose/telemetry semantics.
304
- using pendingMemoryPrefetch = startRelevantMemoryPrefetch(
305
- state.messages,
306
- state.toolUseContext,
307
- )
308
-
309
- // eslint-disable-next-line no-constant-condition
310
- while (true) {
311
- // Destructure state at the top of each iteration. toolUseContext alone
312
- // is reassigned within an iteration (queryTracking, messages updates);
313
- // the rest are read-only between continue sites.
314
- let { toolUseContext } = state
315
- const {
316
- messages,
317
- autoCompactTracking,
318
- maxOutputTokensRecoveryCount,
319
- hasAttemptedReactiveCompact,
320
- maxOutputTokensOverride,
321
- pendingToolUseSummary,
322
- stopHookActive,
323
- turnCount,
324
- } = state
325
-
326
- // Skill discovery prefetch — per-iteration (uses findWritePivot guard
327
- // that returns early on non-write iterations). Discovery runs while the
328
- // model streams and tools execute; awaited post-tools alongside the
329
- // memory prefetch consume. Replaces the blocking assistant_turn path
330
- // that ran inside getAttachmentMessages (97% of those calls found
331
- // nothing in prod). Turn-0 user-input discovery still blocks in
332
- // userInputAttachments — that's the one signal where there's no prior
333
- // work to hide under.
334
- const pendingSkillPrefetch = skillPrefetch?.startSkillDiscoveryPrefetch(
335
- null,
336
- messages,
337
- toolUseContext,
338
- )
339
-
340
- yield { type: 'stream_request_start' }
341
-
342
- queryCheckpoint('query_fn_entry')
343
-
344
- // Record query start for headless latency tracking (skip for subagents)
345
- if (!toolUseContext.agentId) {
346
- headlessProfilerCheckpoint('query_started')
347
- }
348
-
349
- // Initialize or increment query chain tracking
350
- const queryTracking = toolUseContext.queryTracking
351
- ? {
352
- chainId: toolUseContext.queryTracking.chainId,
353
- depth: toolUseContext.queryTracking.depth + 1,
354
- }
355
- : {
356
- chainId: deps.uuid(),
357
- depth: 0,
358
- }
359
-
360
- const queryChainIdForAnalytics =
361
- queryTracking.chainId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS
362
-
363
- toolUseContext = {
364
- ...toolUseContext,
365
- queryTracking,
366
- }
367
-
368
- let messagesForQuery = [...getMessagesAfterCompactBoundary(messages)]
369
-
370
- let tracking = autoCompactTracking
371
-
372
- // Enforce per-message budget on aggregate tool result size. Runs BEFORE
373
- // microcompact — cached MC operates purely by tool_use_id (never inspects
374
- // content), so content replacement is invisible to it and the two compose
375
- // cleanly. No-ops when contentReplacementState is undefined (feature off).
376
- // Persist only for querySources that read records back on resume: agentId
377
- // routes to sidechain file (AgentTool resume) or session file (/resume).
378
- // Ephemeral runForkedAgent callers (agent_summary etc.) don't persist.
379
- const persistReplacements =
380
- querySource.startsWith('agent:') ||
381
- querySource.startsWith('repl_main_thread')
382
- messagesForQuery = await applyToolResultBudget(
383
- messagesForQuery,
384
- toolUseContext.contentReplacementState,
385
- persistReplacements
386
- ? records =>
387
- void recordContentReplacement(
388
- records,
389
- toolUseContext.agentId,
390
- ).catch(logError)
391
- : undefined,
392
- new Set(
393
- toolUseContext.options.tools
394
- .filter(t => !Number.isFinite(t.maxResultSizeChars))
395
- .map(t => t.name),
396
- ),
397
- )
398
-
399
- // Apply snip before microcompact (both may run — they are not mutually exclusive).
400
- // snipTokensFreed is plumbed to autocompact so its threshold check reflects
401
- // what snip removed; tokenCountWithEstimation alone can't see it (reads usage
402
- // from the protected-tail assistant, which survives snip unchanged).
403
- let snipTokensFreed = 0
404
- if (feature('HISTORY_SNIP')) {
405
- queryCheckpoint('query_snip_start')
406
- const snipResult = snipModule!.snipCompactIfNeeded(messagesForQuery)
407
- messagesForQuery = snipResult.messages
408
- snipTokensFreed = snipResult.tokensFreed
409
- if (snipResult.boundaryMessage) {
410
- yield snipResult.boundaryMessage
411
- }
412
- queryCheckpoint('query_snip_end')
413
- }
414
-
415
- // Apply microcompact before autocompact
416
- queryCheckpoint('query_microcompact_start')
417
- const microcompactResult = await deps.microcompact(
418
- messagesForQuery,
419
- toolUseContext,
420
- querySource,
421
- )
422
- messagesForQuery = microcompactResult.messages
423
- // For cached microcompact (cache editing), defer boundary message until after
424
- // the API response so we can use actual cache_deleted_input_tokens.
425
- // Gated behind feature() so the string is eliminated from external builds.
426
- const pendingCacheEdits = feature('CACHED_MICROCOMPACT')
427
- ? microcompactResult.compactionInfo?.pendingCacheEdits
428
- : undefined
429
- queryCheckpoint('query_microcompact_end')
430
-
431
- // Project the collapsed context view and maybe commit more collapses.
432
- // Runs BEFORE autocompact so that if collapse gets us under the
433
- // autocompact threshold, autocompact is a no-op and we keep granular
434
- // context instead of a single summary.
435
- //
436
- // Nothing is yielded — the collapsed view is a read-time projection
437
- // over the REPL's full history. Summary messages live in the collapse
438
- // store, not the REPL array. This is what makes collapses persist
439
- // across turns: projectView() replays the commit log on every entry.
440
- // Within a turn, the view flows forward via state.messages at the
441
- // continue site (query.ts:1192), and the next projectView() no-ops
442
- // because the archived messages are already gone from its input.
443
- if (feature('CONTEXT_COLLAPSE') && contextCollapse) {
444
- const collapseResult = await contextCollapse.applyCollapsesIfNeeded(
445
- messagesForQuery,
446
- toolUseContext,
447
- querySource,
448
- )
449
- messagesForQuery = collapseResult.messages
450
- }
451
-
452
- const fullSystemPrompt = asSystemPrompt(
453
- appendSystemContext(systemPrompt, systemContext),
454
- )
455
-
456
- queryCheckpoint('query_autocompact_start')
457
- const { compactionResult, consecutiveFailures } = await deps.autocompact(
458
- messagesForQuery,
459
- toolUseContext,
460
- {
461
- systemPrompt,
462
- userContext,
463
- systemContext,
464
- toolUseContext,
465
- forkContextMessages: messagesForQuery,
466
- },
467
- querySource,
468
- tracking,
469
- snipTokensFreed,
470
- )
471
- queryCheckpoint('query_autocompact_end')
472
-
473
- if (compactionResult) {
474
- const {
475
- preCompactTokenCount,
476
- postCompactTokenCount,
477
- truePostCompactTokenCount,
478
- compactionUsage,
479
- } = compactionResult
480
-
481
- logEvent('tengu_auto_compact_succeeded', {
482
- originalMessageCount: messages.length,
483
- compactedMessageCount:
484
- compactionResult.summaryMessages.length +
485
- compactionResult.attachments.length +
486
- compactionResult.hookResults.length,
487
- preCompactTokenCount,
488
- postCompactTokenCount,
489
- truePostCompactTokenCount,
490
- compactionInputTokens: compactionUsage?.input_tokens,
491
- compactionOutputTokens: compactionUsage?.output_tokens,
492
- compactionCacheReadTokens:
493
- compactionUsage?.cache_read_input_tokens ?? 0,
494
- compactionCacheCreationTokens:
495
- compactionUsage?.cache_creation_input_tokens ?? 0,
496
- compactionTotalTokens: compactionUsage
497
- ? compactionUsage.input_tokens +
498
- (compactionUsage.cache_creation_input_tokens ?? 0) +
499
- (compactionUsage.cache_read_input_tokens ?? 0) +
500
- compactionUsage.output_tokens
501
- : 0,
502
-
503
- queryChainId: queryChainIdForAnalytics,
504
- queryDepth: queryTracking.depth,
505
- })
506
-
507
- // task_budget: capture pre-compact final context window before
508
- // messagesForQuery is replaced with postCompactMessages below.
509
- // iterations[-1] is the authoritative final window (post server tool
510
- // loops); see #304930.
511
- if (params.taskBudget) {
512
- const preCompactContext =
513
- finalContextTokensFromLastResponse(messagesForQuery)
514
- taskBudgetRemaining = Math.max(
515
- 0,
516
- (taskBudgetRemaining ?? params.taskBudget.total) - preCompactContext,
517
- )
518
- }
519
-
520
- // Reset on every compact so turnCounter/turnId reflect the MOST RECENT
521
- // compact. recompactionInfo (autoCompact.ts:190) already captured the
522
- // old values for turnsSincePreviousCompact/previousCompactTurnId before
523
- // the call, so this reset doesn't lose those.
524
- tracking = {
525
- compacted: true,
526
- turnId: deps.uuid(),
527
- turnCounter: 0,
528
- consecutiveFailures: 0,
529
- }
530
-
531
- const postCompactMessages = buildPostCompactMessages(compactionResult)
532
-
533
- for (const message of postCompactMessages) {
534
- yield message
535
- }
536
-
537
- // Continue on with the current query call using the post compact messages
538
- messagesForQuery = postCompactMessages
539
- } else if (consecutiveFailures !== undefined) {
540
- // Autocompact failed — propagate failure count so the circuit breaker
541
- // can stop retrying on the next iteration.
542
- tracking = {
543
- ...(tracking ?? { compacted: false, turnId: '', turnCounter: 0 }),
544
- consecutiveFailures,
545
- }
546
- }
547
-
548
- //TODO: no need to set toolUseContext.messages during set-up since it is updated here
549
- toolUseContext = {
550
- ...toolUseContext,
551
- messages: messagesForQuery,
552
- }
553
-
554
- const assistantMessages: AssistantMessage[] = []
555
- const toolResults: (UserMessage | AttachmentMessage)[] = []
556
- // @see https://docs.claude.com/en/docs/build-with-claude/tool-use
557
- // Note: stop_reason === 'tool_use' is unreliable -- it's not always set correctly.
558
- // Set during streaming whenever a tool_use block arrives — the sole
559
- // loop-exit signal. If false after streaming, we're done (modulo stop-hook retry).
560
- const toolUseBlocks: ToolUseBlock[] = []
561
- let needsFollowUp = false
562
-
563
- queryCheckpoint('query_setup_start')
564
- const useStreamingToolExecution = config.gates.streamingToolExecution
565
- let streamingToolExecutor = useStreamingToolExecution
566
- ? new StreamingToolExecutor(
567
- toolUseContext.options.tools,
568
- canUseTool,
569
- toolUseContext,
570
- )
571
- : null
572
-
573
- const appState = toolUseContext.getAppState()
574
- const permissionMode = appState.toolPermissionContext.mode
575
- let currentModel = getRuntimeMainLoopModel({
576
- permissionMode,
577
- mainLoopModel: toolUseContext.options.mainLoopModel,
578
- exceeds200kTokens:
579
- permissionMode === 'plan' &&
580
- doesMostRecentAssistantMessageExceed200k(messagesForQuery),
581
- })
582
-
583
- queryCheckpoint('query_setup_end')
584
-
585
- // Create fetch wrapper once per query session to avoid memory retention.
586
- // Each call to createDumpPromptsFetch creates a closure that captures the request body.
587
- // Creating it once means only the latest request body is retained (~700KB),
588
- // instead of all request bodies from the session (~500MB for long sessions).
589
- // Note: agentId is effectively constant during a query() call - it only changes
590
- // between queries (e.g., /clear command or session resume).
591
- const dumpPromptsFetch = config.gates.isAnt
592
- ? createDumpPromptsFetch(toolUseContext.agentId ?? config.sessionId)
593
- : undefined
594
-
595
- // Block if we've hit the hard blocking limit (only applies when auto-compact is OFF)
596
- // This reserves space so users can still run /compact manually
597
- // Skip this check if compaction just happened - the compaction result is already
598
- // validated to be under the threshold, and tokenCountWithEstimation would use
599
- // stale input_tokens from kept messages that reflect pre-compaction context size.
600
- // Same staleness applies to snip: subtract snipTokensFreed (otherwise we'd
601
- // falsely block in the window where snip brought us under autocompact threshold
602
- // but the stale usage is still above blocking limit — before this PR that
603
- // window never existed because autocompact always fired on the stale count).
604
- // Also skip for compact/session_memory queries — these are forked agents that
605
- // inherit the full conversation and would deadlock if blocked here (the compact
606
- // agent needs to run to REDUCE the token count).
607
- // Also skip when reactive compact is enabled and automatic compaction is
608
- // allowed — the preempt's synthetic error returns before the API call,
609
- // so reactive compact would never see a prompt-too-long to react to.
610
- // Widened to walrus so RC can act as fallback when proactive fails.
611
- //
612
- // Same skip for context-collapse: its recoverFromOverflow drains
613
- // staged collapses on a REAL API 413, then falls through to
614
- // reactiveCompact. A synthetic preempt here would return before the
615
- // API call and starve both recovery paths. The isAutoCompactEnabled()
616
- // conjunct preserves the user's explicit "no automatic anything"
617
- // config — if they set DISABLE_AUTO_COMPACT, they get the preempt.
618
- let collapseOwnsIt = false
619
- if (feature('CONTEXT_COLLAPSE')) {
620
- collapseOwnsIt =
621
- (contextCollapse?.isContextCollapseEnabled() ?? false) &&
622
- isAutoCompactEnabled()
623
- }
624
- // Hoist media-recovery gate once per turn. Withholding (inside the
625
- // stream loop) and recovery (after) must agree; CACHED_MAY_BE_STALE can
626
- // flip during the 5-30s stream, and withhold-without-recover would eat
627
- // the message. PTL doesn't hoist because its withholding is ungated —
628
- // it predates the experiment and is already the control-arm baseline.
629
- const mediaRecoveryEnabled =
630
- reactiveCompact?.isReactiveCompactEnabled() ?? false
631
- if (
632
- !compactionResult &&
633
- querySource !== 'compact' &&
634
- querySource !== 'session_memory' &&
635
- !(
636
- reactiveCompact?.isReactiveCompactEnabled() && isAutoCompactEnabled()
637
- ) &&
638
- !collapseOwnsIt
639
- ) {
640
- const { isAtBlockingLimit } = calculateTokenWarningState(
641
- tokenCountWithEstimation(messagesForQuery) - snipTokensFreed,
642
- toolUseContext.options.mainLoopModel,
643
- )
644
- if (isAtBlockingLimit) {
645
- yield createAssistantAPIErrorMessage({
646
- content: PROMPT_TOO_LONG_ERROR_MESSAGE,
647
- error: 'invalid_request',
648
- })
649
- return { reason: 'blocking_limit' }
650
- }
651
- }
652
-
653
- let attemptWithFallback = true
654
-
655
- queryCheckpoint('query_api_loop_start')
656
- try {
657
- while (attemptWithFallback) {
658
- attemptWithFallback = false
659
- try {
660
- let streamingFallbackOccured = false
661
- queryCheckpoint('query_api_streaming_start')
662
- for await (const message of deps.callModel({
663
- messages: prependUserContext(messagesForQuery, userContext),
664
- systemPrompt: fullSystemPrompt,
665
- thinkingConfig: toolUseContext.options.thinkingConfig,
666
- tools: toolUseContext.options.tools,
667
- signal: toolUseContext.abortController.signal,
668
- options: {
669
- async getToolPermissionContext() {
670
- const appState = toolUseContext.getAppState()
671
- return appState.toolPermissionContext
672
- },
673
- model: currentModel,
674
- ...(config.gates.fastModeEnabled && {
675
- fastMode: appState.fastMode,
676
- }),
677
- toolChoice: undefined,
678
- isNonInteractiveSession:
679
- toolUseContext.options.isNonInteractiveSession,
680
- fallbackModel,
681
- onStreamingFallback: () => {
682
- streamingFallbackOccured = true
683
- },
684
- querySource,
685
- agents: toolUseContext.options.agentDefinitions.activeAgents,
686
- allowedAgentTypes:
687
- toolUseContext.options.agentDefinitions.allowedAgentTypes,
688
- hasAppendSystemPrompt:
689
- !!toolUseContext.options.appendSystemPrompt,
690
- maxOutputTokensOverride,
691
- fetchOverride: dumpPromptsFetch,
692
- mcpTools: appState.mcp.tools,
693
- hasPendingMcpServers: appState.mcp.clients.some(
694
- c => c.type === 'pending',
695
- ),
696
- queryTracking,
697
- effortValue: appState.effortValue,
698
- advisorModel: appState.advisorModel,
699
- skipCacheWrite,
700
- agentId: toolUseContext.agentId,
701
- addNotification: toolUseContext.addNotification,
702
- ...(params.taskBudget && {
703
- taskBudget: {
704
- total: params.taskBudget.total,
705
- ...(taskBudgetRemaining !== undefined && {
706
- remaining: taskBudgetRemaining,
707
- }),
708
- },
709
- }),
710
- },
711
- })) {
712
- // We won't use the tool_calls from the first attempt
713
- // We could.. but then we'd have to merge assistant messages
714
- // with different ids and double up on full the tool_results
715
- if (streamingFallbackOccured) {
716
- // Yield tombstones for orphaned messages so they're removed from UI and transcript.
717
- // These partial messages (especially thinking blocks) have invalid signatures
718
- // that would cause "thinking blocks cannot be modified" API errors.
719
- for (const msg of assistantMessages) {
720
- yield { type: 'tombstone' as const, message: msg }
721
- }
722
- logEvent('tengu_orphaned_messages_tombstoned', {
723
- orphanedMessageCount: assistantMessages.length,
724
- queryChainId: queryChainIdForAnalytics,
725
- queryDepth: queryTracking.depth,
726
- })
727
-
728
- assistantMessages.length = 0
729
- toolResults.length = 0
730
- toolUseBlocks.length = 0
731
- needsFollowUp = false
732
-
733
- // Discard pending results from the failed streaming attempt and create
734
- // a fresh executor. This prevents orphan tool_results (with old tool_use_ids)
735
- // from being yielded after the fallback response arrives.
736
- if (streamingToolExecutor) {
737
- streamingToolExecutor.discard()
738
- streamingToolExecutor = new StreamingToolExecutor(
739
- toolUseContext.options.tools,
740
- canUseTool,
741
- toolUseContext,
742
- )
743
- }
744
- }
745
- // Backfill tool_use inputs on a cloned message before yield so
746
- // SDK stream output and transcript serialization see legacy/derived
747
- // fields. The original `message` is left untouched for
748
- // assistantMessages.push below — it flows back to the API and
749
- // mutating it would break prompt caching (byte mismatch).
750
- let yieldMessage: typeof message = message
751
- if (message.type === 'assistant') {
752
- let clonedContent: typeof message.message.content | undefined
753
- for (let i = 0; i < message.message.content.length; i++) {
754
- const block = message.message.content[i]!
755
- if (
756
- block.type === 'tool_use' &&
757
- typeof block.input === 'object' &&
758
- block.input !== null
759
- ) {
760
- const tool = findToolByName(
761
- toolUseContext.options.tools,
762
- block.name,
763
- )
764
- if (tool?.backfillObservableInput) {
765
- const originalInput = block.input as Record<string, unknown>
766
- const inputCopy = { ...originalInput }
767
- tool.backfillObservableInput(inputCopy)
768
- // Only yield a clone when backfill ADDED fields; skip if
769
- // it only OVERWROTE existing ones (e.g. file tools
770
- // expanding file_path). Overwrites change the serialized
771
- // transcript and break VCR fixture hashes on resume,
772
- // while adding nothing the SDK stream needs — hooks get
773
- // the expanded path via toolExecution.ts separately.
774
- const addedFields = Object.keys(inputCopy).some(
775
- k => !(k in originalInput),
776
- )
777
- if (addedFields) {
778
- clonedContent ??= [...message.message.content]
779
- clonedContent[i] = { ...block, input: inputCopy }
780
- }
781
- }
782
- }
783
- }
784
- if (clonedContent) {
785
- yieldMessage = {
786
- ...message,
787
- message: { ...message.message, content: clonedContent },
788
- }
789
- }
790
- }
791
- // Withhold recoverable errors (prompt-too-long, max-output-tokens)
792
- // until we know whether recovery (collapse drain / reactive
793
- // compact / truncation retry) can succeed. Still pushed to
794
- // assistantMessages so the recovery checks below find them.
795
- // Either subsystem's withhold is sufficient — they're
796
- // independent so turning one off doesn't break the other's
797
- // recovery path.
798
- //
799
- // feature() only works in if/ternary conditions (bun:bundle
800
- // tree-shaking constraint), so the collapse check is nested
801
- // rather than composed.
802
- let withheld = false
803
- if (feature('CONTEXT_COLLAPSE')) {
804
- if (
805
- contextCollapse?.isWithheldPromptTooLong(
806
- message,
807
- isPromptTooLongMessage,
808
- querySource,
809
- )
810
- ) {
811
- withheld = true
812
- }
813
- }
814
- if (reactiveCompact?.isWithheldPromptTooLong(message)) {
815
- withheld = true
816
- }
817
- if (
818
- mediaRecoveryEnabled &&
819
- reactiveCompact?.isWithheldMediaSizeError(message)
820
- ) {
821
- withheld = true
822
- }
823
- if (isWithheldMaxOutputTokens(message)) {
824
- withheld = true
825
- }
826
- if (!withheld) {
827
- yield yieldMessage
828
- }
829
- if (message.type === 'assistant') {
830
- assistantMessages.push(message)
831
-
832
- const msgToolUseBlocks = message.message.content.filter(
833
- content => content.type === 'tool_use',
834
- ) as ToolUseBlock[]
835
- if (msgToolUseBlocks.length > 0) {
836
- toolUseBlocks.push(...msgToolUseBlocks)
837
- needsFollowUp = true
838
- }
839
-
840
- if (
841
- streamingToolExecutor &&
842
- !toolUseContext.abortController.signal.aborted
843
- ) {
844
- for (const toolBlock of msgToolUseBlocks) {
845
- streamingToolExecutor.addTool(toolBlock, message)
846
- }
847
- }
848
- }
849
-
850
- if (
851
- streamingToolExecutor &&
852
- !toolUseContext.abortController.signal.aborted
853
- ) {
854
- for (const result of streamingToolExecutor.getCompletedResults()) {
855
- if (result.message) {
856
- yield result.message
857
- toolResults.push(
858
- ...normalizeMessagesForAPI(
859
- [result.message],
860
- toolUseContext.options.tools,
861
- ).filter(_ => _.type === 'user'),
862
- )
863
- }
864
- }
865
- }
866
- }
867
- queryCheckpoint('query_api_streaming_end')
868
-
869
- // Yield deferred microcompact boundary message using actual API-reported
870
- // token deletion count instead of client-side estimates.
871
- // Entire block gated behind feature() so the excluded string
872
- // is eliminated from external builds.
873
- if (feature('CACHED_MICROCOMPACT') && pendingCacheEdits) {
874
- const lastAssistant = assistantMessages.at(-1)
875
- // The API field is cumulative/sticky across requests, so we
876
- // subtract the baseline captured before this request to get the delta.
877
- const usage = lastAssistant?.message.usage
878
- const cumulativeDeleted = usage
879
- ? ((usage as unknown as Record<string, number>)
880
- .cache_deleted_input_tokens ?? 0)
881
- : 0
882
- const deletedTokens = Math.max(
883
- 0,
884
- cumulativeDeleted - pendingCacheEdits.baselineCacheDeletedTokens,
885
- )
886
- if (deletedTokens > 0) {
887
- yield createMicrocompactBoundaryMessage(
888
- pendingCacheEdits.trigger,
889
- 0,
890
- deletedTokens,
891
- pendingCacheEdits.deletedToolIds,
892
- [],
893
- )
894
- }
895
- }
896
- } catch (innerError) {
897
- if (innerError instanceof FallbackTriggeredError && fallbackModel) {
898
- // Fallback was triggered - switch model and retry
899
- currentModel = fallbackModel
900
- attemptWithFallback = true
901
-
902
- // Clear assistant messages since we'll retry the entire request
903
- yield* yieldMissingToolResultBlocks(
904
- assistantMessages,
905
- 'Model fallback triggered',
906
- )
907
- assistantMessages.length = 0
908
- toolResults.length = 0
909
- toolUseBlocks.length = 0
910
- needsFollowUp = false
911
-
912
- // Discard pending results from the failed attempt and create a
913
- // fresh executor. This prevents orphan tool_results (with old
914
- // tool_use_ids) from leaking into the retry.
915
- if (streamingToolExecutor) {
916
- streamingToolExecutor.discard()
917
- streamingToolExecutor = new StreamingToolExecutor(
918
- toolUseContext.options.tools,
919
- canUseTool,
920
- toolUseContext,
921
- )
922
- }
923
-
924
- // Update tool use context with new model
925
- toolUseContext.options.mainLoopModel = fallbackModel
926
-
927
- // Thinking signatures are model-bound: replaying a protected-thinking
928
- // block (e.g. capybara) to an unprotected fallback (e.g. opus) 400s.
929
- // Strip before retry so the fallback model gets clean history.
930
- if (process.env.USER_TYPE === 'ant') {
931
- messagesForQuery = stripSignatureBlocks(messagesForQuery)
932
- }
933
-
934
- // Log the fallback event
935
- logEvent('tengu_model_fallback_triggered', {
936
- original_model:
937
- innerError.originalModel as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
938
- fallback_model:
939
- fallbackModel as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
940
- entrypoint:
941
- 'cli' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
942
- queryChainId: queryChainIdForAnalytics,
943
- queryDepth: queryTracking.depth,
944
- })
945
-
946
- // Yield system message about fallback — use 'warning' level so
947
- // users see the notification without needing verbose mode
948
- yield createSystemMessage(
949
- `Switched to ${renderModelName(innerError.fallbackModel)} due to high demand for ${renderModelName(innerError.originalModel)}`,
950
- 'warning',
951
- )
952
-
953
- continue
954
- }
955
- throw innerError
956
- }
957
- }
958
- } catch (error) {
959
- logError(error)
960
- const errorMessage =
961
- error instanceof Error ? error.message : String(error)
962
- logEvent('tengu_query_error', {
963
- assistantMessages: assistantMessages.length,
964
- toolUses: assistantMessages.flatMap(_ =>
965
- _.message.content.filter(content => content.type === 'tool_use'),
966
- ).length,
967
-
968
- queryChainId: queryChainIdForAnalytics,
969
- queryDepth: queryTracking.depth,
970
- })
971
-
972
- // Handle image size/resize errors with user-friendly messages
973
- if (
974
- error instanceof ImageSizeError ||
975
- error instanceof ImageResizeError
976
- ) {
977
- yield createAssistantAPIErrorMessage({
978
- content: error.message,
979
- })
980
- return { reason: 'image_error' }
981
- }
982
-
983
- // Generally queryModelWithStreaming should not throw errors but instead
984
- // yield them as synthetic assistant messages. However if it does throw
985
- // due to a bug, we may end up in a state where we have already emitted
986
- // a tool_use block but will stop before emitting the tool_result.
987
- yield* yieldMissingToolResultBlocks(assistantMessages, errorMessage)
988
-
989
- // Surface the real error instead of a misleading "[Request interrupted
990
- // by user]" — this path is a model/runtime failure, not a user action.
991
- // SDK consumers were seeing phantom interrupts on e.g. Node 18's missing
992
- // Array.prototype.with(), masking the actual cause.
993
- yield createAssistantAPIErrorMessage({
994
- content: errorMessage,
995
- })
996
-
997
- // To help track down bugs, log loudly for ants
998
- logAntError('Query error', error)
999
- return { reason: 'model_error', error }
1000
- }
1001
-
1002
- // Execute post-sampling hooks after model response is complete
1003
- if (assistantMessages.length > 0) {
1004
- void executePostSamplingHooks(
1005
- [...messagesForQuery, ...assistantMessages],
1006
- systemPrompt,
1007
- userContext,
1008
- systemContext,
1009
- toolUseContext,
1010
- querySource,
1011
- )
1012
- }
1013
-
1014
- // We need to handle a streaming abort before anything else.
1015
- // When using streamingToolExecutor, we must consume getRemainingResults() so the
1016
- // executor can generate synthetic tool_result blocks for queued/in-progress tools.
1017
- // Without this, tool_use blocks would lack matching tool_result blocks.
1018
- if (toolUseContext.abortController.signal.aborted) {
1019
- if (streamingToolExecutor) {
1020
- // Consume remaining results - executor generates synthetic tool_results for
1021
- // aborted tools since it checks the abort signal in executeTool()
1022
- for await (const update of streamingToolExecutor.getRemainingResults()) {
1023
- if (update.message) {
1024
- yield update.message
1025
- }
1026
- }
1027
- } else {
1028
- yield* yieldMissingToolResultBlocks(
1029
- assistantMessages,
1030
- 'Interrupted by user',
1031
- )
1032
- }
1033
- // chicago MCP: auto-unhide + lock release on interrupt. Same cleanup
1034
- // as the natural turn-end path in stopHooks.ts. Main thread only —
1035
- // see stopHooks.ts for the subagent-releasing-main's-lock rationale.
1036
- if (feature('CHICAGO_MCP') && !toolUseContext.agentId) {
1037
- try {
1038
- const { cleanupComputerUseAfterTurn } = await import(
1039
- './utils/computerUse/cleanup.js'
1040
- )
1041
- await cleanupComputerUseAfterTurn(toolUseContext)
1042
- } catch {
1043
- // Failures are silent — this is dogfooding cleanup, not critical path
1044
- }
1045
- }
1046
-
1047
- // Skip the interruption message for submit-interrupts — the queued
1048
- // user message that follows provides sufficient context.
1049
- if (toolUseContext.abortController.signal.reason !== 'interrupt') {
1050
- yield createUserInterruptionMessage({
1051
- toolUse: false,
1052
- })
1053
- }
1054
- return { reason: 'aborted_streaming' }
1055
- }
1056
-
1057
- // Yield tool use summary from previous turn — haiku (~1s) resolved during model streaming (5-30s)
1058
- if (pendingToolUseSummary) {
1059
- const summary = await pendingToolUseSummary
1060
- if (summary) {
1061
- yield summary
1062
- }
1063
- }
1064
-
1065
- if (!needsFollowUp) {
1066
- const lastMessage = assistantMessages.at(-1)
1067
-
1068
- // Prompt-too-long recovery: the streaming loop withheld the error
1069
- // (see withheldByCollapse / withheldByReactive above). Try collapse
1070
- // drain first (cheap, keeps granular context), then reactive compact
1071
- // (full summary). Single-shot on each — if a retry still 413's,
1072
- // the next stage handles it or the error surfaces.
1073
- const isWithheld413 =
1074
- lastMessage?.type === 'assistant' &&
1075
- lastMessage.isApiErrorMessage &&
1076
- isPromptTooLongMessage(lastMessage)
1077
- // Media-size rejections (image/PDF/many-image) are recoverable via
1078
- // reactive compact's strip-retry. Unlike PTL, media errors skip the
1079
- // collapse drain — collapse doesn't strip images. mediaRecoveryEnabled
1080
- // is the hoisted gate from before the stream loop (same value as the
1081
- // withholding check — these two must agree or a withheld message is
1082
- // lost). If the oversized media is in the preserved tail, the
1083
- // post-compact turn will media-error again; hasAttemptedReactiveCompact
1084
- // prevents a spiral and the error surfaces.
1085
- const isWithheldMedia =
1086
- mediaRecoveryEnabled &&
1087
- reactiveCompact?.isWithheldMediaSizeError(lastMessage)
1088
- if (isWithheld413) {
1089
- // First: drain all staged context-collapses. Gated on the PREVIOUS
1090
- // transition not being collapse_drain_retry — if we already drained
1091
- // and the retry still 413'd, fall through to reactive compact.
1092
- if (
1093
- feature('CONTEXT_COLLAPSE') &&
1094
- contextCollapse &&
1095
- state.transition?.reason !== 'collapse_drain_retry'
1096
- ) {
1097
- const drained = contextCollapse.recoverFromOverflow(
1098
- messagesForQuery,
1099
- querySource,
1100
- )
1101
- if (drained.committed > 0) {
1102
- const next: State = {
1103
- messages: drained.messages,
1104
- toolUseContext,
1105
- autoCompactTracking: tracking,
1106
- maxOutputTokensRecoveryCount,
1107
- hasAttemptedReactiveCompact,
1108
- maxOutputTokensOverride: undefined,
1109
- pendingToolUseSummary: undefined,
1110
- stopHookActive: undefined,
1111
- turnCount,
1112
- transition: {
1113
- reason: 'collapse_drain_retry',
1114
- committed: drained.committed,
1115
- },
1116
- }
1117
- state = next
1118
- continue
1119
- }
1120
- }
1121
- }
1122
- if ((isWithheld413 || isWithheldMedia) && reactiveCompact) {
1123
- const compacted = await reactiveCompact.tryReactiveCompact({
1124
- hasAttempted: hasAttemptedReactiveCompact,
1125
- querySource,
1126
- aborted: toolUseContext.abortController.signal.aborted,
1127
- messages: messagesForQuery,
1128
- cacheSafeParams: {
1129
- systemPrompt,
1130
- userContext,
1131
- systemContext,
1132
- toolUseContext,
1133
- forkContextMessages: messagesForQuery,
1134
- },
1135
- })
1136
-
1137
- if (compacted) {
1138
- // task_budget: same carryover as the proactive path above.
1139
- // messagesForQuery still holds the pre-compact array here (the
1140
- // 413-failed attempt's input).
1141
- if (params.taskBudget) {
1142
- const preCompactContext =
1143
- finalContextTokensFromLastResponse(messagesForQuery)
1144
- taskBudgetRemaining = Math.max(
1145
- 0,
1146
- (taskBudgetRemaining ?? params.taskBudget.total) -
1147
- preCompactContext,
1148
- )
1149
- }
1150
-
1151
- const postCompactMessages = buildPostCompactMessages(compacted)
1152
- for (const msg of postCompactMessages) {
1153
- yield msg
1154
- }
1155
- const next: State = {
1156
- messages: postCompactMessages,
1157
- toolUseContext,
1158
- autoCompactTracking: undefined,
1159
- maxOutputTokensRecoveryCount,
1160
- hasAttemptedReactiveCompact: true,
1161
- maxOutputTokensOverride: undefined,
1162
- pendingToolUseSummary: undefined,
1163
- stopHookActive: undefined,
1164
- turnCount,
1165
- transition: { reason: 'reactive_compact_retry' },
1166
- }
1167
- state = next
1168
- continue
1169
- }
1170
-
1171
- // No recovery — surface the withheld error and exit. Do NOT fall
1172
- // through to stop hooks: the model never produced a valid response,
1173
- // so hooks have nothing meaningful to evaluate. Running stop hooks
1174
- // on prompt-too-long creates a death spiral: error → hook blocking
1175
- // → retry → error → … (the hook injects more tokens each cycle).
1176
- yield lastMessage
1177
- void executeStopFailureHooks(lastMessage, toolUseContext)
1178
- return { reason: isWithheldMedia ? 'image_error' : 'prompt_too_long' }
1179
- } else if (feature('CONTEXT_COLLAPSE') && isWithheld413) {
1180
- // reactiveCompact compiled out but contextCollapse withheld and
1181
- // couldn't recover (staged queue empty/stale). Surface. Same
1182
- // early-return rationale — don't fall through to stop hooks.
1183
- yield lastMessage
1184
- void executeStopFailureHooks(lastMessage, toolUseContext)
1185
- return { reason: 'prompt_too_long' }
1186
- }
1187
-
1188
- // Check for max_output_tokens and inject recovery message. The error
1189
- // was withheld from the stream above; only surface it if recovery
1190
- // exhausts.
1191
- if (isWithheldMaxOutputTokens(lastMessage)) {
1192
- // Escalating retry: if we used the capped 8k default and hit the
1193
- // limit, retry the SAME request at 64k — no meta message, no
1194
- // multi-turn dance. This fires once per turn (guarded by the
1195
- // override check), then falls through to multi-turn recovery if
1196
- // 64k also hits the cap.
1197
- // 3P default: false (not validated on Bedrock/Vertex)
1198
- const capEnabled = getFeatureValue_CACHED_MAY_BE_STALE(
1199
- 'tengu_otk_slot_v1',
1200
- false,
1201
- )
1202
- if (
1203
- capEnabled &&
1204
- maxOutputTokensOverride === undefined &&
1205
- !process.env.CLAUDE_CODE_MAX_OUTPUT_TOKENS
1206
- ) {
1207
- logEvent('tengu_max_tokens_escalate', {
1208
- escalatedTo: ESCALATED_MAX_TOKENS,
1209
- })
1210
- const next: State = {
1211
- messages: messagesForQuery,
1212
- toolUseContext,
1213
- autoCompactTracking: tracking,
1214
- maxOutputTokensRecoveryCount,
1215
- hasAttemptedReactiveCompact,
1216
- maxOutputTokensOverride: ESCALATED_MAX_TOKENS,
1217
- pendingToolUseSummary: undefined,
1218
- stopHookActive: undefined,
1219
- turnCount,
1220
- transition: { reason: 'max_output_tokens_escalate' },
1221
- }
1222
- state = next
1223
- continue
1224
- }
1225
-
1226
- if (maxOutputTokensRecoveryCount < MAX_OUTPUT_TOKENS_RECOVERY_LIMIT) {
1227
- const recoveryMessage = createUserMessage({
1228
- content:
1229
- `Output token limit hit. Resume directly — no apology, no recap of what you were doing. ` +
1230
- `Pick up mid-thought if that is where the cut happened. Break remaining work into smaller pieces.`,
1231
- isMeta: true,
1232
- })
1233
-
1234
- const next: State = {
1235
- messages: [
1236
- ...messagesForQuery,
1237
- ...assistantMessages,
1238
- recoveryMessage,
1239
- ],
1240
- toolUseContext,
1241
- autoCompactTracking: tracking,
1242
- maxOutputTokensRecoveryCount: maxOutputTokensRecoveryCount + 1,
1243
- hasAttemptedReactiveCompact,
1244
- maxOutputTokensOverride: undefined,
1245
- pendingToolUseSummary: undefined,
1246
- stopHookActive: undefined,
1247
- turnCount,
1248
- transition: {
1249
- reason: 'max_output_tokens_recovery',
1250
- attempt: maxOutputTokensRecoveryCount + 1,
1251
- },
1252
- }
1253
- state = next
1254
- continue
1255
- }
1256
-
1257
- // Recovery exhausted — surface the withheld error now.
1258
- yield lastMessage
1259
- }
1260
-
1261
- // Skip stop hooks when the last message is an API error (rate limit,
1262
- // prompt-too-long, auth failure, etc.). The model never produced a
1263
- // real response — hooks evaluating it create a death spiral:
1264
- // error → hook blocking → retry → error → …
1265
- if (lastMessage?.isApiErrorMessage) {
1266
- void executeStopFailureHooks(lastMessage, toolUseContext)
1267
- return { reason: 'completed' }
1268
- }
1269
-
1270
- const stopHookResult = yield* handleStopHooks(
1271
- messagesForQuery,
1272
- assistantMessages,
1273
- systemPrompt,
1274
- userContext,
1275
- systemContext,
1276
- toolUseContext,
1277
- querySource,
1278
- stopHookActive,
1279
- )
1280
-
1281
- if (stopHookResult.preventContinuation) {
1282
- return { reason: 'stop_hook_prevented' }
1283
- }
1284
-
1285
- if (stopHookResult.blockingErrors.length > 0) {
1286
- const next: State = {
1287
- messages: [
1288
- ...messagesForQuery,
1289
- ...assistantMessages,
1290
- ...stopHookResult.blockingErrors,
1291
- ],
1292
- toolUseContext,
1293
- autoCompactTracking: tracking,
1294
- maxOutputTokensRecoveryCount: 0,
1295
- // Preserve the reactive compact guard — if compact already ran and
1296
- // couldn't recover from prompt-too-long, retrying after a stop-hook
1297
- // blocking error will produce the same result. Resetting to false
1298
- // here caused an infinite loop: compact → still too long → error →
1299
- // stop hook blocking → compact → … burning thousands of API calls.
1300
- hasAttemptedReactiveCompact,
1301
- maxOutputTokensOverride: undefined,
1302
- pendingToolUseSummary: undefined,
1303
- stopHookActive: true,
1304
- turnCount,
1305
- transition: { reason: 'stop_hook_blocking' },
1306
- }
1307
- state = next
1308
- continue
1309
- }
1310
-
1311
- if (feature('TOKEN_BUDGET')) {
1312
- const decision = checkTokenBudget(
1313
- budgetTracker!,
1314
- toolUseContext.agentId,
1315
- getCurrentTurnTokenBudget(),
1316
- getTurnOutputTokens(),
1317
- )
1318
-
1319
- if (decision.action === 'continue') {
1320
- incrementBudgetContinuationCount()
1321
- logForDebugging(
1322
- `Token budget continuation #${decision.continuationCount}: ${decision.pct}% (${decision.turnTokens.toLocaleString()} / ${decision.budget.toLocaleString()})`,
1323
- )
1324
- state = {
1325
- messages: [
1326
- ...messagesForQuery,
1327
- ...assistantMessages,
1328
- createUserMessage({
1329
- content: decision.nudgeMessage,
1330
- isMeta: true,
1331
- }),
1332
- ],
1333
- toolUseContext,
1334
- autoCompactTracking: tracking,
1335
- maxOutputTokensRecoveryCount: 0,
1336
- hasAttemptedReactiveCompact: false,
1337
- maxOutputTokensOverride: undefined,
1338
- pendingToolUseSummary: undefined,
1339
- stopHookActive: undefined,
1340
- turnCount,
1341
- transition: { reason: 'token_budget_continuation' },
1342
- }
1343
- continue
1344
- }
1345
-
1346
- if (decision.completionEvent) {
1347
- if (decision.completionEvent.diminishingReturns) {
1348
- logForDebugging(
1349
- `Token budget early stop: diminishing returns at ${decision.completionEvent.pct}%`,
1350
- )
1351
- }
1352
- logEvent('tengu_token_budget_completed', {
1353
- ...decision.completionEvent,
1354
- queryChainId: queryChainIdForAnalytics,
1355
- queryDepth: queryTracking.depth,
1356
- })
1357
- }
1358
- }
1359
-
1360
- return { reason: 'completed' }
1361
- }
1362
-
1363
- let shouldPreventContinuation = false
1364
- let updatedToolUseContext = toolUseContext
1365
-
1366
- queryCheckpoint('query_tool_execution_start')
1367
-
1368
-
1369
- if (streamingToolExecutor) {
1370
- logEvent('tengu_streaming_tool_execution_used', {
1371
- tool_count: toolUseBlocks.length,
1372
- queryChainId: queryChainIdForAnalytics,
1373
- queryDepth: queryTracking.depth,
1374
- })
1375
- } else {
1376
- logEvent('tengu_streaming_tool_execution_not_used', {
1377
- tool_count: toolUseBlocks.length,
1378
- queryChainId: queryChainIdForAnalytics,
1379
- queryDepth: queryTracking.depth,
1380
- })
1381
- }
1382
-
1383
- const toolUpdates = streamingToolExecutor
1384
- ? streamingToolExecutor.getRemainingResults()
1385
- : runTools(toolUseBlocks, assistantMessages, canUseTool, toolUseContext)
1386
-
1387
- for await (const update of toolUpdates) {
1388
- if (update.message) {
1389
- yield update.message
1390
-
1391
- if (
1392
- update.message.type === 'attachment' &&
1393
- update.message.attachment.type === 'hook_stopped_continuation'
1394
- ) {
1395
- shouldPreventContinuation = true
1396
- }
1397
-
1398
- toolResults.push(
1399
- ...normalizeMessagesForAPI(
1400
- [update.message],
1401
- toolUseContext.options.tools,
1402
- ).filter(_ => _.type === 'user'),
1403
- )
1404
- }
1405
- if (update.newContext) {
1406
- updatedToolUseContext = {
1407
- ...update.newContext,
1408
- queryTracking,
1409
- }
1410
- }
1411
- }
1412
- queryCheckpoint('query_tool_execution_end')
1413
-
1414
- // Generate tool use summary after tool batch completes — passed to next recursive call
1415
- let nextPendingToolUseSummary:
1416
- | Promise<ToolUseSummaryMessage | null>
1417
- | undefined
1418
- if (
1419
- config.gates.emitToolUseSummaries &&
1420
- toolUseBlocks.length > 0 &&
1421
- !toolUseContext.abortController.signal.aborted &&
1422
- !toolUseContext.agentId // subagents don't surface in mobile UI — skip the Haiku call
1423
- ) {
1424
- // Extract the last assistant text block for context
1425
- const lastAssistantMessage = assistantMessages.at(-1)
1426
- let lastAssistantText: string | undefined
1427
- if (lastAssistantMessage) {
1428
- const textBlocks = lastAssistantMessage.message.content.filter(
1429
- block => block.type === 'text',
1430
- )
1431
- if (textBlocks.length > 0) {
1432
- const lastTextBlock = textBlocks.at(-1)
1433
- if (lastTextBlock && 'text' in lastTextBlock) {
1434
- lastAssistantText = lastTextBlock.text
1435
- }
1436
- }
1437
- }
1438
-
1439
- // Collect tool info for summary generation
1440
- const toolUseIds = toolUseBlocks.map(block => block.id)
1441
- const toolInfoForSummary = toolUseBlocks.map(block => {
1442
- // Find the corresponding tool result
1443
- const toolResult = toolResults.find(
1444
- result =>
1445
- result.type === 'user' &&
1446
- Array.isArray(result.message.content) &&
1447
- result.message.content.some(
1448
- content =>
1449
- content.type === 'tool_result' &&
1450
- content.tool_use_id === block.id,
1451
- ),
1452
- )
1453
- const resultContent =
1454
- toolResult?.type === 'user' &&
1455
- Array.isArray(toolResult.message.content)
1456
- ? toolResult.message.content.find(
1457
- (c): c is ToolResultBlockParam =>
1458
- c.type === 'tool_result' && c.tool_use_id === block.id,
1459
- )
1460
- : undefined
1461
- return {
1462
- name: block.name,
1463
- input: block.input,
1464
- output:
1465
- resultContent && 'content' in resultContent
1466
- ? resultContent.content
1467
- : null,
1468
- }
1469
- })
1470
-
1471
- // Fire off summary generation without blocking the next API call
1472
- nextPendingToolUseSummary = generateToolUseSummary({
1473
- tools: toolInfoForSummary,
1474
- signal: toolUseContext.abortController.signal,
1475
- isNonInteractiveSession: toolUseContext.options.isNonInteractiveSession,
1476
- lastAssistantText,
1477
- })
1478
- .then(summary => {
1479
- if (summary) {
1480
- return createToolUseSummaryMessage(summary, toolUseIds)
1481
- }
1482
- return null
1483
- })
1484
- .catch(() => null)
1485
- }
1486
-
1487
- // We were aborted during tool calls
1488
- if (toolUseContext.abortController.signal.aborted) {
1489
- // chicago MCP: auto-unhide + lock release when aborted mid-tool-call.
1490
- // This is the most likely Ctrl+C path for CU (e.g. slow screenshot).
1491
- // Main thread only — see stopHooks.ts for the subagent rationale.
1492
- if (feature('CHICAGO_MCP') && !toolUseContext.agentId) {
1493
- try {
1494
- const { cleanupComputerUseAfterTurn } = await import(
1495
- './utils/computerUse/cleanup.js'
1496
- )
1497
- await cleanupComputerUseAfterTurn(toolUseContext)
1498
- } catch {
1499
- // Failures are silent — this is dogfooding cleanup, not critical path
1500
- }
1501
- }
1502
- // Skip the interruption message for submit-interrupts — the queued
1503
- // user message that follows provides sufficient context.
1504
- if (toolUseContext.abortController.signal.reason !== 'interrupt') {
1505
- yield createUserInterruptionMessage({
1506
- toolUse: true,
1507
- })
1508
- }
1509
- // Check maxTurns before returning when aborted
1510
- const nextTurnCountOnAbort = turnCount + 1
1511
- if (maxTurns && nextTurnCountOnAbort > maxTurns) {
1512
- yield createAttachmentMessage({
1513
- type: 'max_turns_reached',
1514
- maxTurns,
1515
- turnCount: nextTurnCountOnAbort,
1516
- })
1517
- }
1518
- return { reason: 'aborted_tools' }
1519
- }
1520
-
1521
- // If a hook indicated to prevent continuation, stop here
1522
- if (shouldPreventContinuation) {
1523
- return { reason: 'hook_stopped' }
1524
- }
1525
-
1526
- if (tracking?.compacted) {
1527
- tracking.turnCounter++
1528
- logEvent('tengu_post_autocompact_turn', {
1529
- turnId:
1530
- tracking.turnId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
1531
- turnCounter: tracking.turnCounter,
1532
-
1533
- queryChainId: queryChainIdForAnalytics,
1534
- queryDepth: queryTracking.depth,
1535
- })
1536
- }
1537
-
1538
- // Be careful to do this after tool calls are done, because the API
1539
- // will error if we interleave tool_result messages with regular user messages.
1540
-
1541
- // Instrumentation: Track message count before attachments
1542
- logEvent('tengu_query_before_attachments', {
1543
- messagesForQueryCount: messagesForQuery.length,
1544
- assistantMessagesCount: assistantMessages.length,
1545
- toolResultsCount: toolResults.length,
1546
- queryChainId: queryChainIdForAnalytics,
1547
- queryDepth: queryTracking.depth,
1548
- })
1549
-
1550
- // Get queued commands snapshot before processing attachments.
1551
- // These will be sent as attachments so Claude can respond to them in the current turn.
1552
- //
1553
- // Drain pending notifications. LocalShellTask completions are 'next'
1554
- // (when MONITOR_TOOL is on) and drain without Sleep. Other task types
1555
- // (agent/workflow/framework) still default to 'later' — the Sleep flush
1556
- // covers those. If all task types move to 'next', this branch could go.
1557
- //
1558
- // Slash commands are excluded from mid-turn drain — they must go through
1559
- // processSlashCommand after the turn ends (via useQueueProcessor), not be
1560
- // sent to the model as text. Bash-mode commands are already excluded by
1561
- // INLINE_NOTIFICATION_MODES in getQueuedCommandAttachments.
1562
- //
1563
- // Agent scoping: the queue is a process-global singleton shared by the
1564
- // coordinator and all in-process subagents. Each loop drains only what's
1565
- // addressed to it — main thread drains agentId===undefined, subagents
1566
- // drain their own agentId. User prompts (mode:'prompt') still go to main
1567
- // only; subagents never see the prompt stream.
1568
- // eslint-disable-next-line custom-rules/require-tool-match-name -- ToolUseBlock.name has no aliases
1569
- const sleepRan = toolUseBlocks.some(b => b.name === SLEEP_TOOL_NAME)
1570
- const isMainThread =
1571
- querySource.startsWith('repl_main_thread') || querySource === 'sdk'
1572
- const currentAgentId = toolUseContext.agentId
1573
- const queuedCommandsSnapshot = getCommandsByMaxPriority(
1574
- sleepRan ? 'later' : 'next',
1575
- ).filter(cmd => {
1576
- if (isSlashCommand(cmd)) return false
1577
- if (isMainThread) return cmd.agentId === undefined
1578
- // Subagents only drain task-notifications addressed to them — never
1579
- // user prompts, even if someone stamps an agentId on one.
1580
- return cmd.mode === 'task-notification' && cmd.agentId === currentAgentId
1581
- })
1582
-
1583
- for await (const attachment of getAttachmentMessages(
1584
- null,
1585
- updatedToolUseContext,
1586
- null,
1587
- queuedCommandsSnapshot,
1588
- [...messagesForQuery, ...assistantMessages, ...toolResults],
1589
- querySource,
1590
- )) {
1591
- yield attachment
1592
- toolResults.push(attachment)
1593
- }
1594
-
1595
- // Memory prefetch consume: only if settled and not already consumed on
1596
- // an earlier iteration. If not settled yet, skip (zero-wait) and retry
1597
- // next iteration — the prefetch gets as many chances as there are loop
1598
- // iterations before the turn ends. readFileState (cumulative across
1599
- // iterations) filters out memories the model already Read/Wrote/Edited
1600
- // — including in earlier iterations, which the per-iteration
1601
- // toolUseBlocks array would miss.
1602
- if (
1603
- pendingMemoryPrefetch &&
1604
- pendingMemoryPrefetch.settledAt !== null &&
1605
- pendingMemoryPrefetch.consumedOnIteration === -1
1606
- ) {
1607
- const memoryAttachments = filterDuplicateMemoryAttachments(
1608
- await pendingMemoryPrefetch.promise,
1609
- toolUseContext.readFileState,
1610
- )
1611
- for (const memAttachment of memoryAttachments) {
1612
- const msg = createAttachmentMessage(memAttachment)
1613
- yield msg
1614
- toolResults.push(msg)
1615
- }
1616
- pendingMemoryPrefetch.consumedOnIteration = turnCount - 1
1617
- }
1618
-
1619
-
1620
- // Inject prefetched skill discovery. collectSkillDiscoveryPrefetch emits
1621
- // hidden_by_main_turn — true when the prefetch resolved before this point
1622
- // (should be >98% at AKI@250ms / Haiku@573ms vs turn durations of 2-30s).
1623
- if (skillPrefetch && pendingSkillPrefetch) {
1624
- const skillAttachments =
1625
- await skillPrefetch.collectSkillDiscoveryPrefetch(pendingSkillPrefetch)
1626
- for (const att of skillAttachments) {
1627
- const msg = createAttachmentMessage(att)
1628
- yield msg
1629
- toolResults.push(msg)
1630
- }
1631
- }
1632
-
1633
- // Remove only commands that were actually consumed as attachments.
1634
- // Prompt and task-notification commands are converted to attachments above.
1635
- const consumedCommands = queuedCommandsSnapshot.filter(
1636
- cmd => cmd.mode === 'prompt' || cmd.mode === 'task-notification',
1637
- )
1638
- if (consumedCommands.length > 0) {
1639
- for (const cmd of consumedCommands) {
1640
- if (cmd.uuid) {
1641
- consumedCommandUuids.push(cmd.uuid)
1642
- notifyCommandLifecycle(cmd.uuid, 'started')
1643
- }
1644
- }
1645
- removeFromQueue(consumedCommands)
1646
- }
1647
-
1648
- // Instrumentation: Track file change attachments after they're added
1649
- const fileChangeAttachmentCount = count(
1650
- toolResults,
1651
- tr =>
1652
- tr.type === 'attachment' && tr.attachment.type === 'edited_text_file',
1653
- )
1654
-
1655
- logEvent('tengu_query_after_attachments', {
1656
- totalToolResultsCount: toolResults.length,
1657
- fileChangeAttachmentCount,
1658
- queryChainId: queryChainIdForAnalytics,
1659
- queryDepth: queryTracking.depth,
1660
- })
1661
-
1662
- // Refresh tools between turns so newly-connected MCP servers become available
1663
- if (updatedToolUseContext.options.refreshTools) {
1664
- const refreshedTools = updatedToolUseContext.options.refreshTools()
1665
- if (refreshedTools !== updatedToolUseContext.options.tools) {
1666
- updatedToolUseContext = {
1667
- ...updatedToolUseContext,
1668
- options: {
1669
- ...updatedToolUseContext.options,
1670
- tools: refreshedTools,
1671
- },
1672
- }
1673
- }
1674
- }
1675
-
1676
- const toolUseContextWithQueryTracking = {
1677
- ...updatedToolUseContext,
1678
- queryTracking,
1679
- }
1680
-
1681
- // Each time we have tool results and are about to recurse, that's a turn
1682
- const nextTurnCount = turnCount + 1
1683
-
1684
- // Periodic task summary for `claude ps` — fires mid-turn so a
1685
- // long-running agent still refreshes what it's working on. Gated
1686
- // only on !agentId so every top-level conversation (REPL, SDK, HFI,
1687
- // remote) generates summaries; subagents/forks don't.
1688
- if (feature('BG_SESSIONS')) {
1689
- if (
1690
- !toolUseContext.agentId &&
1691
- taskSummaryModule!.shouldGenerateTaskSummary()
1692
- ) {
1693
- taskSummaryModule!.maybeGenerateTaskSummary({
1694
- systemPrompt,
1695
- userContext,
1696
- systemContext,
1697
- toolUseContext,
1698
- forkContextMessages: [
1699
- ...messagesForQuery,
1700
- ...assistantMessages,
1701
- ...toolResults,
1702
- ],
1703
- })
1704
- }
1705
- }
1706
-
1707
- // Check if we've reached the max turns limit
1708
- if (maxTurns && nextTurnCount > maxTurns) {
1709
- yield createAttachmentMessage({
1710
- type: 'max_turns_reached',
1711
- maxTurns,
1712
- turnCount: nextTurnCount,
1713
- })
1714
- return { reason: 'max_turns', turnCount: nextTurnCount }
1715
- }
1716
-
1717
- queryCheckpoint('query_recursive_call')
1718
- const next: State = {
1719
- messages: [...messagesForQuery, ...assistantMessages, ...toolResults],
1720
- toolUseContext: toolUseContextWithQueryTracking,
1721
- autoCompactTracking: tracking,
1722
- turnCount: nextTurnCount,
1723
- maxOutputTokensRecoveryCount: 0,
1724
- hasAttemptedReactiveCompact: false,
1725
- pendingToolUseSummary: nextPendingToolUseSummary,
1726
- maxOutputTokensOverride: undefined,
1727
- stopHookActive,
1728
- transition: { reason: 'next_turn' },
1729
- }
1730
- state = next
1731
- } // while (true)
1732
- }