thaddeus 1.0.26 → 1.0.28

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 (2082) hide show
  1. package/package.json +14 -5
  2. package/src/QueryEngine.js +926 -0
  3. package/src/Task.js +49 -0
  4. package/src/Tool.js +61 -0
  5. package/src/assistant/gate.js +5 -0
  6. package/src/assistant/index.js +106 -0
  7. package/src/assistant/sessionHistory.js +145 -0
  8. package/src/bootstrap/state.js +1163 -0
  9. package/src/bridge/bridgeApi.js +304 -0
  10. package/src/bridge/bridgeConfig.js +39 -0
  11. package/src/bridge/bridgeDebug.js +73 -0
  12. package/src/bridge/bridgeEnabled.js +185 -0
  13. package/src/bridge/bridgeMain.js +2289 -0
  14. package/src/bridge/bridgeMessaging.js +353 -0
  15. package/src/bridge/bridgePermissionCallbacks.js +10 -0
  16. package/src/bridge/bridgePointer.js +175 -0
  17. package/src/bridge/bridgeStatusUtil.js +105 -0
  18. package/src/bridge/bridgeUI.js +411 -0
  19. package/src/bridge/capacityWake.js +35 -0
  20. package/src/bridge/codeSessionApi.js +111 -0
  21. package/src/bridge/createSession.js +273 -0
  22. package/src/bridge/debugUtils.js +115 -0
  23. package/src/bridge/envLessBridgeConfig.js +120 -0
  24. package/src/bridge/flushGate.js +65 -0
  25. package/src/bridge/inboundAttachments.js +152 -0
  26. package/src/bridge/inboundMessages.js +63 -0
  27. package/src/bridge/initReplBridge.js +431 -0
  28. package/src/bridge/jwtUtils.js +185 -0
  29. package/src/bridge/peerSessions.js +5 -0
  30. package/src/bridge/pollConfig.js +85 -0
  31. package/src/bridge/pollConfigDefaults.js +62 -0
  32. package/src/bridge/remoteBridgeCore.js +712 -0
  33. package/src/bridge/replBridge.js +1719 -0
  34. package/src/bridge/replBridgeHandle.js +30 -0
  35. package/src/bridge/replBridgeTransport.js +236 -0
  36. package/src/bridge/sessionIdCompat.js +56 -0
  37. package/src/bridge/sessionRunner.js +421 -0
  38. package/src/bridge/trustedDevice.js +170 -0
  39. package/src/bridge/types.js +9 -0
  40. package/src/bridge/webhookSanitizer.js +6 -0
  41. package/src/bridge/workSecret.js +99 -0
  42. package/src/buddy/CompanionSprite.js +348 -0
  43. package/src/buddy/companion.js +107 -0
  44. package/src/buddy/prompt.js +33 -0
  45. package/src/buddy/sprites.js +488 -0
  46. package/src/buddy/types.js +90 -0
  47. package/src/buddy/useBuddyNotification.js +85 -0
  48. package/src/cli/bg.js +17 -0
  49. package/src/cli/exit.js +30 -0
  50. package/src/cli/handlers/agents.js +55 -0
  51. package/src/cli/handlers/auth.js +249 -0
  52. package/src/cli/handlers/autoMode.js +128 -0
  53. package/src/cli/handlers/mcp.js +335 -0
  54. package/src/cli/handlers/plugins.js +634 -0
  55. package/src/cli/handlers/templateJobs.js +19 -0
  56. package/src/cli/handlers/util.js +76 -0
  57. package/src/cli/ndjsonSafeStringify.js +27 -0
  58. package/src/cli/print.js +4294 -0
  59. package/src/cli/remoteIO.js +208 -0
  60. package/src/cli/structuredIO.js +644 -0
  61. package/src/cli/transports/HybridTransport.js +233 -0
  62. package/src/cli/transports/SSETransport.js +538 -0
  63. package/src/cli/transports/SerialBatchEventUploader.js +224 -0
  64. package/src/cli/transports/WebSocketTransport.js +613 -0
  65. package/src/cli/transports/WorkerStateUploader.js +88 -0
  66. package/src/cli/transports/ccrClient.js +711 -0
  67. package/src/cli/transports/transportUtils.js +39 -0
  68. package/src/cli/update.js +314 -0
  69. package/src/commandCenter/launch.js +39 -0
  70. package/src/commandCenter/phoneApi.js +168 -0
  71. package/src/commandCenter/phoneStore.js +159 -0
  72. package/src/commandCenter/reactorBus.js +130 -0
  73. package/src/commandCenter/server.js +288 -0
  74. package/src/commandCenter/server.ts +42 -7
  75. package/src/commandCenter/tunnel.js +199 -0
  76. package/src/commands/add-dir/add-dir.js +121 -0
  77. package/src/commands/add-dir/index.js +8 -0
  78. package/src/commands/add-dir/validation.js +76 -0
  79. package/src/commands/advisor.js +88 -0
  80. package/src/commands/agents/agents.js +10 -0
  81. package/src/commands/agents/index.js +7 -0
  82. package/src/commands/agents-platform/index.js +2 -0
  83. package/src/commands/assistant/index.js +86 -0
  84. package/src/commands/backup/index.js +31 -0
  85. package/src/commands/branch/branch.js +205 -0
  86. package/src/commands/branch/index.js +11 -0
  87. package/src/commands/bridge/bridge.js +513 -0
  88. package/src/commands/bridge/index.js +22 -0
  89. package/src/commands/bridge-kick.js +179 -0
  90. package/src/commands/brief.js +89 -0
  91. package/src/commands/btw/btw.js +235 -0
  92. package/src/commands/btw/index.js +9 -0
  93. package/src/commands/buddy/buddy.js +100 -0
  94. package/src/commands/buddy/index.js +11 -0
  95. package/src/commands/chrome/chrome.js +291 -0
  96. package/src/commands/chrome/index.js +10 -0
  97. package/src/commands/clear/caches.js +116 -0
  98. package/src/commands/clear/clear.js +5 -0
  99. package/src/commands/clear/conversation.js +189 -0
  100. package/src/commands/clear/index.js +9 -0
  101. package/src/commands/color/color.js +58 -0
  102. package/src/commands/color/index.js +9 -0
  103. package/src/commands/commit-push-pr.js +137 -0
  104. package/src/commands/commit.js +80 -0
  105. package/src/commands/compact/compact.js +194 -0
  106. package/src/commands/compact/index.js +11 -0
  107. package/src/commands/config/config.js +6 -0
  108. package/src/commands/config/index.js +8 -0
  109. package/src/commands/context/context-noninteractive.js +219 -0
  110. package/src/commands/context/context.js +45 -0
  111. package/src/commands/context/index.js +21 -0
  112. package/src/commands/coordinator.js +34 -0
  113. package/src/commands/copy/copy.js +366 -0
  114. package/src/commands/copy/index.js +7 -0
  115. package/src/commands/cost/cost.js +21 -0
  116. package/src/commands/cost/index.js +16 -0
  117. package/src/commands/createMovedToPluginCommand.js +33 -0
  118. package/src/commands/desktop/desktop.js +6 -0
  119. package/src/commands/desktop/index.js +22 -0
  120. package/src/commands/diff/diff.js +6 -0
  121. package/src/commands/diff/index.js +6 -0
  122. package/src/commands/doctor/doctor.js +6 -0
  123. package/src/commands/doctor/index.js +9 -0
  124. package/src/commands/effort/effort.js +166 -0
  125. package/src/commands/effort/index.js +11 -0
  126. package/src/commands/exit/exit.js +32 -0
  127. package/src/commands/exit/index.js +9 -0
  128. package/src/commands/export/export.js +87 -0
  129. package/src/commands/export/index.js +8 -0
  130. package/src/commands/extra-usage/extra-usage-core.js +99 -0
  131. package/src/commands/extra-usage/extra-usage-noninteractive.js +13 -0
  132. package/src/commands/extra-usage/extra-usage.js +15 -0
  133. package/src/commands/extra-usage/index.js +29 -0
  134. package/src/commands/fast/fast.js +276 -0
  135. package/src/commands/fast/index.js +19 -0
  136. package/src/commands/feedback/feedback.js +11 -0
  137. package/src/commands/feedback/index.js +20 -0
  138. package/src/commands/files/files.js +11 -0
  139. package/src/commands/files/index.js +9 -0
  140. package/src/commands/force-snip.js +19 -0
  141. package/src/commands/fork/index.js +67 -0
  142. package/src/commands/heapdump/heapdump.js +14 -0
  143. package/src/commands/heapdump/index.js +9 -0
  144. package/src/commands/help/help.js +6 -0
  145. package/src/commands/help/index.js +7 -0
  146. package/src/commands/hooks/hooks.js +12 -0
  147. package/src/commands/hooks/index.js +8 -0
  148. package/src/commands/ide/ide.js +615 -0
  149. package/src/commands/ide/index.js +8 -0
  150. package/src/commands/init-verifiers.js +258 -0
  151. package/src/commands/init.js +248 -0
  152. package/src/commands/insights.js +2554 -0
  153. package/src/commands/install-github-app/ApiKeyStep.js +230 -0
  154. package/src/commands/install-github-app/CheckExistingSecretStep.js +194 -0
  155. package/src/commands/install-github-app/CheckGitHubStep.js +16 -0
  156. package/src/commands/install-github-app/ChooseRepoStep.js +211 -0
  157. package/src/commands/install-github-app/CreatingStep.js +53 -0
  158. package/src/commands/install-github-app/ErrorStep.js +84 -0
  159. package/src/commands/install-github-app/ExistingWorkflowStep.js +105 -0
  160. package/src/commands/install-github-app/InstallAppStep.js +97 -0
  161. package/src/commands/install-github-app/OAuthFlowStep.js +190 -0
  162. package/src/commands/install-github-app/SuccessStep.js +94 -0
  163. package/src/commands/install-github-app/WarningsStep.js +71 -0
  164. package/src/commands/install-github-app/index.js +10 -0
  165. package/src/commands/install-github-app/install-github-app.js +593 -0
  166. package/src/commands/install-github-app/setupGitHubActions.js +227 -0
  167. package/src/commands/install-slack-app/index.js +9 -0
  168. package/src/commands/install-slack-app/install-slack-app.js +25 -0
  169. package/src/commands/install.js +198 -0
  170. package/src/commands/keybindings/index.js +10 -0
  171. package/src/commands/keybindings/keybindings.js +47 -0
  172. package/src/commands/login/index.js +21 -0
  173. package/src/commands/login/login.js +135 -0
  174. package/src/commands/logout/index.js +11 -0
  175. package/src/commands/logout/logout.js +75 -0
  176. package/src/commands/mcp/addCommand.js +183 -0
  177. package/src/commands/mcp/index.js +9 -0
  178. package/src/commands/mcp/mcp.js +78 -0
  179. package/src/commands/mcp/xaaIdpCommand.js +193 -0
  180. package/src/commands/memories/index.js +9 -0
  181. package/src/commands/memories/index.ts +12 -0
  182. package/src/commands/memories/memories.tsx +949 -0
  183. package/src/commands/memory/index.js +7 -0
  184. package/src/commands/memory/memory.js +71 -0
  185. package/src/commands/mobile/index.js +9 -0
  186. package/src/commands/mobile/mobile.js +279 -0
  187. package/src/commands/model/index.js +14 -0
  188. package/src/commands/model/model.js +284 -0
  189. package/src/commands/output-style/index.js +8 -0
  190. package/src/commands/output-style/output-style.js +6 -0
  191. package/src/commands/passes/index.js +17 -0
  192. package/src/commands/passes/passes.js +23 -0
  193. package/src/commands/peers/index.js +68 -0
  194. package/src/commands/permissions/index.js +8 -0
  195. package/src/commands/permissions/permissions.js +9 -0
  196. package/src/commands/plan/index.js +8 -0
  197. package/src/commands/plan/plan.js +116 -0
  198. package/src/commands/plugin/AddMarketplace.js +96 -0
  199. package/src/commands/plugin/BrowseMarketplace.js +582 -0
  200. package/src/commands/plugin/DiscoverPlugins.js +613 -0
  201. package/src/commands/plugin/ManageMarketplaces.js +583 -0
  202. package/src/commands/plugin/ManagePlugins.js +1783 -0
  203. package/src/commands/plugin/PluginErrors.js +124 -0
  204. package/src/commands/plugin/PluginOptionsDialog.js +367 -0
  205. package/src/commands/plugin/PluginOptionsFlow.js +97 -0
  206. package/src/commands/plugin/PluginSettings.js +1041 -0
  207. package/src/commands/plugin/PluginTrustWarning.js +35 -0
  208. package/src/commands/plugin/UnifiedInstalledCell.js +616 -0
  209. package/src/commands/plugin/ValidatePlugin.js +96 -0
  210. package/src/commands/plugin/index.js +10 -0
  211. package/src/commands/plugin/parseArgs.js +71 -0
  212. package/src/commands/plugin/plugin.js +6 -0
  213. package/src/commands/plugin/pluginDetailsHelpers.js +95 -0
  214. package/src/commands/plugin/usePagination.js +89 -0
  215. package/src/commands/pr_comments/index.js +49 -0
  216. package/src/commands/privacy-settings/index.js +11 -0
  217. package/src/commands/privacy-settings/privacy-settings.js +55 -0
  218. package/src/commands/proactive.js +29 -0
  219. package/src/commands/rate-limit-options/index.js +15 -0
  220. package/src/commands/rate-limit-options/rate-limit-options.js +213 -0
  221. package/src/commands/release-notes/index.js +8 -0
  222. package/src/commands/release-notes/release-notes.js +38 -0
  223. package/src/commands/reload-plugins/index.js +11 -0
  224. package/src/commands/reload-plugins/reload-plugins.js +52 -0
  225. package/src/commands/remote-env/index.js +12 -0
  226. package/src/commands/remote-env/remote-env.js +6 -0
  227. package/src/commands/remote-setup/api.js +155 -0
  228. package/src/commands/remote-setup/index.js +15 -0
  229. package/src/commands/remote-setup/remote-setup.js +150 -0
  230. package/src/commands/remoteControlServer/index.js +58 -0
  231. package/src/commands/rename/generateSessionName.js +58 -0
  232. package/src/commands/rename/index.js +9 -0
  233. package/src/commands/rename/rename.js +52 -0
  234. package/src/commands/resume/index.js +9 -0
  235. package/src/commands/resume/resume.js +239 -0
  236. package/src/commands/review/UltrareviewOverageDialog.js +97 -0
  237. package/src/commands/review/reviewRemote.js +259 -0
  238. package/src/commands/review/ultrareviewCommand.js +58 -0
  239. package/src/commands/review/ultrareviewEnabled.js +10 -0
  240. package/src/commands/review.js +53 -0
  241. package/src/commands/rewind/index.js +10 -0
  242. package/src/commands/rewind/rewind.js +7 -0
  243. package/src/commands/sandbox-toggle/index.js +41 -0
  244. package/src/commands/sandbox-toggle/sandbox-toggle.js +73 -0
  245. package/src/commands/security-review.js +231 -0
  246. package/src/commands/session/index.js +13 -0
  247. package/src/commands/session/session.js +143 -0
  248. package/src/commands/skills/index.js +7 -0
  249. package/src/commands/skills/skills.js +6 -0
  250. package/src/commands/speak.js +21 -0
  251. package/src/commands/start-business.js +1575 -0
  252. package/src/commands/start-business.ts +1581 -0
  253. package/src/commands/stats/index.js +7 -0
  254. package/src/commands/stats/stats.js +6 -0
  255. package/src/commands/status/index.js +8 -0
  256. package/src/commands/status/status.js +6 -0
  257. package/src/commands/statusline.js +22 -0
  258. package/src/commands/stickers/index.js +8 -0
  259. package/src/commands/stickers/stickers.js +14 -0
  260. package/src/commands/subscribe-pr.js +131 -0
  261. package/src/commands/tag/index.js +9 -0
  262. package/src/commands/tag/tag.js +215 -0
  263. package/src/commands/tasks/index.js +8 -0
  264. package/src/commands/tasks/tasks.js +6 -0
  265. package/src/commands/terminalSetup/index.js +18 -0
  266. package/src/commands/terminalSetup/terminalSetup.js +491 -0
  267. package/src/commands/thaddeus-usage/index.js +17 -0
  268. package/src/commands/theme/index.js +7 -0
  269. package/src/commands/theme/theme.js +51 -0
  270. package/src/commands/thinkback/index.js +9 -0
  271. package/src/commands/thinkback/thinkback.js +528 -0
  272. package/src/commands/thinkback-play/index.js +13 -0
  273. package/src/commands/thinkback-play/thinkback-play.js +34 -0
  274. package/src/commands/torch.js +122 -0
  275. package/src/commands/ultraplan.js +416 -0
  276. package/src/commands/upgrade/index.js +12 -0
  277. package/src/commands/upgrade/upgrade.js +38 -0
  278. package/src/commands/usage/index.js +7 -0
  279. package/src/commands/usage/usage.js +6 -0
  280. package/src/commands/version.js +17 -0
  281. package/src/commands/vim/index.js +8 -0
  282. package/src/commands/vim/vim.js +25 -0
  283. package/src/commands/voice/index.js +13 -0
  284. package/src/commands/voice/voice.js +44 -0
  285. package/src/commands/workflows/index.js +123 -0
  286. package/src/commands.js +614 -0
  287. package/src/commands.ts +4 -0
  288. package/src/components/AgentProgressLine.js +112 -0
  289. package/src/components/AntModelSwitchCallout.js +8 -0
  290. package/src/components/App.js +46 -0
  291. package/src/components/ApproveApiKey.js +125 -0
  292. package/src/components/AutoModeOptInDialog.js +140 -0
  293. package/src/components/AutoUpdater.js +156 -0
  294. package/src/components/AutoUpdaterWrapper.js +78 -0
  295. package/src/components/AwsAuthStatusBox.js +88 -0
  296. package/src/components/BaseTextInput.js +105 -0
  297. package/src/components/BashModeProgress.js +49 -0
  298. package/src/components/BridgeDialog.js +415 -0
  299. package/src/components/BypassPermissionsModeDialog.js +87 -0
  300. package/src/components/ChannelDowngradeDialog.js +101 -0
  301. package/src/components/ClaudeInChromeOnboarding.js +126 -0
  302. package/src/components/ClaudeMdExternalIncludesDialog.js +137 -0
  303. package/src/components/ClickableImageRef.js +65 -0
  304. package/src/components/CompactSummary.js +120 -0
  305. package/src/components/ConfigurableShortcutHint.js +35 -0
  306. package/src/components/ConsoleOAuthFlow.js +554 -0
  307. package/src/components/ContextSuggestions.js +44 -0
  308. package/src/components/ContextVisualization.js +482 -0
  309. package/src/components/CoordinatorAgentStatus.js +261 -0
  310. package/src/components/CostThresholdDialog.js +49 -0
  311. package/src/components/CtrlOToExpand.js +50 -0
  312. package/src/components/CustomSelect/SelectMulti.js +150 -0
  313. package/src/components/CustomSelect/index.js +2 -0
  314. package/src/components/CustomSelect/option-map.js +32 -0
  315. package/src/components/CustomSelect/select-input-option.js +426 -0
  316. package/src/components/CustomSelect/select-option.js +24 -0
  317. package/src/components/CustomSelect/select.js +518 -0
  318. package/src/components/CustomSelect/use-multi-select-state.js +214 -0
  319. package/src/components/CustomSelect/use-select-input.js +170 -0
  320. package/src/components/CustomSelect/use-select-navigation.js +366 -0
  321. package/src/components/CustomSelect/use-select-state.js +22 -0
  322. package/src/components/DesktopHandoff.js +195 -0
  323. package/src/components/DesktopUpsell/DesktopUpsellStartup.js +174 -0
  324. package/src/components/DevBar.js +51 -0
  325. package/src/components/DevChannelsDialog.js +104 -0
  326. package/src/components/DiagnosticsDisplay.js +91 -0
  327. package/src/components/EffortCallout.js +264 -0
  328. package/src/components/EffortIndicator.js +28 -0
  329. package/src/components/ExitFlow.js +41 -0
  330. package/src/components/ExportDialog.js +101 -0
  331. package/src/components/FallbackToolUseErrorMessage.js +116 -0
  332. package/src/components/FallbackToolUseRejectedMessage.js +17 -0
  333. package/src/components/FastIcon.js +43 -0
  334. package/src/components/Feedback.js +369 -0
  335. package/src/components/FeedbackSurvey/FeedbackSurvey.js +151 -0
  336. package/src/components/FeedbackSurvey/FeedbackSurveyView.js +104 -0
  337. package/src/components/FeedbackSurvey/TranscriptSharePrompt.js +84 -0
  338. package/src/components/FeedbackSurvey/submitTranscriptShare.js +10 -0
  339. package/src/components/FeedbackSurvey/useDebouncedDigitInput.js +51 -0
  340. package/src/components/FeedbackSurvey/useFeedbackSurvey.js +258 -0
  341. package/src/components/FeedbackSurvey/useFrustrationDetection.js +8 -0
  342. package/src/components/FeedbackSurvey/useMemorySurvey.js +191 -0
  343. package/src/components/FeedbackSurvey/usePostCompactSurvey.js +202 -0
  344. package/src/components/FeedbackSurvey/useSurveyState.js +80 -0
  345. package/src/components/FileEditToolDiff.js +167 -0
  346. package/src/components/FileEditToolUpdatedMessage.js +112 -0
  347. package/src/components/FileEditToolUseRejectedMessage.js +158 -0
  348. package/src/components/FilePathLink.js +35 -0
  349. package/src/components/FullscreenLayout.js +578 -0
  350. package/src/components/GlobalSearchDialog.js +340 -0
  351. package/src/components/HelpV2/Commands.js +66 -0
  352. package/src/components/HelpV2/General.js +25 -0
  353. package/src/components/HelpV2/HelpV2.js +186 -0
  354. package/src/components/HighlightedCode/Fallback.js +193 -0
  355. package/src/components/HighlightedCode.js +185 -0
  356. package/src/components/HistorySearchDialog.js +93 -0
  357. package/src/components/IdeAutoConnectDialog.js +154 -0
  358. package/src/components/IdeOnboardingDialog.js +175 -0
  359. package/src/components/IdeStatusIndicator.js +50 -0
  360. package/src/components/IdleReturnDialog.js +117 -0
  361. package/src/components/InterruptedByUser.js +16 -0
  362. package/src/components/InvalidConfigDialog.js +135 -0
  363. package/src/components/InvalidSettingsDialog.js +85 -0
  364. package/src/components/KeybindingWarnings.js +55 -0
  365. package/src/components/LanguagePicker.js +84 -0
  366. package/src/components/LogSelector.js +1579 -0
  367. package/src/components/LogoV2/AnimatedAsterisk.js +43 -0
  368. package/src/components/LogoV2/AnimatedClawd.js +64 -0
  369. package/src/components/LogoV2/ChannelsNotice.js +262 -0
  370. package/src/components/LogoV2/Clawd.js +33 -0
  371. package/src/components/LogoV2/CondensedLogo.js +160 -0
  372. package/src/components/LogoV2/EmergencyTip.js +48 -0
  373. package/src/components/LogoV2/Feed.js +85 -0
  374. package/src/components/LogoV2/FeedColumn.js +55 -0
  375. package/src/components/LogoV2/GuestPassesUpsell.js +71 -0
  376. package/src/components/LogoV2/LogoV2.js +565 -0
  377. package/src/components/LogoV2/Opus1mMergeNotice.js +57 -0
  378. package/src/components/LogoV2/OverageCreditUpsell.js +161 -0
  379. package/src/components/LogoV2/VoiceModeNotice.js +71 -0
  380. package/src/components/LogoV2/WelcomeV2.js +14 -0
  381. package/src/components/LogoV2/feedConfigs.js +79 -0
  382. package/src/components/LspRecommendation/LspRecommendationMenu.js +46 -0
  383. package/src/components/MCPServerApprovalDialog.js +114 -0
  384. package/src/components/MCPServerDesktopImportDialog.js +206 -0
  385. package/src/components/MCPServerDialogCopy.js +16 -0
  386. package/src/components/MCPServerMultiselectDialog.js +134 -0
  387. package/src/components/ManagedSettingsSecurityDialog/ManagedSettingsSecurityDialog.js +150 -0
  388. package/src/components/ManagedSettingsSecurityDialog/utils.js +105 -0
  389. package/src/components/Markdown.js +233 -0
  390. package/src/components/MarkdownTable.js +280 -0
  391. package/src/components/MemoryUsageIndicator.js +28 -0
  392. package/src/components/Message.js +564 -0
  393. package/src/components/MessageModel.js +37 -0
  394. package/src/components/MessageResponse.js +73 -0
  395. package/src/components/MessageRow.js +346 -0
  396. package/src/components/MessageSelector.js +744 -0
  397. package/src/components/MessageTimestamp.js +58 -0
  398. package/src/components/Messages.js +645 -0
  399. package/src/components/ModelPicker.js +452 -0
  400. package/src/components/NativeAutoUpdater.js +152 -0
  401. package/src/components/NotebookEditToolUseRejectedMessage.js +84 -0
  402. package/src/components/OffscreenFreeze.js +35 -0
  403. package/src/components/Onboarding.js +174 -0
  404. package/src/components/OutputStylePicker.js +103 -0
  405. package/src/components/PackageManagerAutoUpdater.js +99 -0
  406. package/src/components/Passes/Passes.js +114 -0
  407. package/src/components/PrBadge.js +91 -0
  408. package/src/components/PressEnterToContinue.js +16 -0
  409. package/src/components/PromptInput/HistorySearchInput.js +45 -0
  410. package/src/components/PromptInput/IssueFlagBanner.js +8 -0
  411. package/src/components/PromptInput/Notifications.js +220 -0
  412. package/src/components/PromptInput/PromptInput.js +2014 -0
  413. package/src/components/PromptInput/PromptInputFooter.js +85 -0
  414. package/src/components/PromptInput/PromptInputFooterLeftSide.js +408 -0
  415. package/src/components/PromptInput/PromptInputFooterSuggestions.js +281 -0
  416. package/src/components/PromptInput/PromptInputHelpMenu.js +380 -0
  417. package/src/components/PromptInput/PromptInputModeIndicator.js +73 -0
  418. package/src/components/PromptInput/PromptInputQueuedCommands.js +105 -0
  419. package/src/components/PromptInput/PromptInputStashNotice.js +21 -0
  420. package/src/components/PromptInput/SandboxPromptFooterHint.js +66 -0
  421. package/src/components/PromptInput/ShimmeredInput.js +133 -0
  422. package/src/components/PromptInput/VoiceIndicator.js +137 -0
  423. package/src/components/PromptInput/inputModes.js +24 -0
  424. package/src/components/PromptInput/inputPaste.js +62 -0
  425. package/src/components/PromptInput/useMaybeTruncateInput.js +33 -0
  426. package/src/components/PromptInput/usePromptInputPlaceholder.js +53 -0
  427. package/src/components/PromptInput/useShowFastIconHint.js +23 -0
  428. package/src/components/PromptInput/useSwarmBanner.js +112 -0
  429. package/src/components/PromptInput/utils.js +50 -0
  430. package/src/components/QuickOpenDialog.js +244 -0
  431. package/src/components/RemoteCallout.js +53 -0
  432. package/src/components/RemoteEnvironmentDialog.js +346 -0
  433. package/src/components/ResumeTask.js +173 -0
  434. package/src/components/SandboxViolationExpandedView.js +106 -0
  435. package/src/components/ScrollKeybindingHandler.js +982 -0
  436. package/src/components/SearchBox.js +56 -0
  437. package/src/components/SentryErrorBoundary.js +16 -0
  438. package/src/components/SessionBackgroundHint.js +105 -0
  439. package/src/components/SessionPreview.js +200 -0
  440. package/src/components/Settings/Config.js +1626 -0
  441. package/src/components/Settings/Settings.js +131 -0
  442. package/src/components/Settings/Status.js +230 -0
  443. package/src/components/Settings/Usage.js +341 -0
  444. package/src/components/ShowInIDEPrompt.js +152 -0
  445. package/src/components/SkillImprovementSurvey.js +130 -0
  446. package/src/components/Spinner/FlashingChar.js +52 -0
  447. package/src/components/Spinner/GlimmerMessage.js +329 -0
  448. package/src/components/Spinner/ShimmerChar.js +23 -0
  449. package/src/components/Spinner/SpinnerAnimationRow.js +170 -0
  450. package/src/components/Spinner/SpinnerGlyph.js +70 -0
  451. package/src/components/Spinner/TeammateSpinnerLine.js +171 -0
  452. package/src/components/Spinner/TeammateSpinnerTree.js +269 -0
  453. package/src/components/Spinner/index.js +9 -0
  454. package/src/components/Spinner/teammateSelectHint.js +1 -0
  455. package/src/components/Spinner/useShimmerAnimation.js +22 -0
  456. package/src/components/Spinner/useStalledAnimation.js +63 -0
  457. package/src/components/Spinner/utils.js +78 -0
  458. package/src/components/Spinner.js +474 -0
  459. package/src/components/Stats.js +1000 -0
  460. package/src/components/StatusLine.js +286 -0
  461. package/src/components/StatusNotices.js +50 -0
  462. package/src/components/StructuredDiff/Fallback.js +336 -0
  463. package/src/components/StructuredDiff/colorDiff.js +37 -0
  464. package/src/components/StructuredDiff.js +153 -0
  465. package/src/components/StructuredDiffList.js +9 -0
  466. package/src/components/TagTabs.js +101 -0
  467. package/src/components/TaskListV2.js +333 -0
  468. package/src/components/TeammateViewHeader.js +88 -0
  469. package/src/components/TeleportError.js +191 -0
  470. package/src/components/TeleportProgress.js +131 -0
  471. package/src/components/TeleportRepoMismatchDialog.js +98 -0
  472. package/src/components/TeleportResumeWrapper.js +158 -0
  473. package/src/components/TeleportStash.js +82 -0
  474. package/src/components/TextInput.js +108 -0
  475. package/src/components/ThaddeusHint/PluginHintMenu.js +37 -0
  476. package/src/components/ThemePicker.js +331 -0
  477. package/src/components/ThinkingToggle.js +154 -0
  478. package/src/components/TokenWarning.js +171 -0
  479. package/src/components/ToolUseLoader.js +35 -0
  480. package/src/components/TrustDialog/TrustDialog.js +301 -0
  481. package/src/components/TrustDialog/utils.js +199 -0
  482. package/src/components/UndercoverAutoCallout.js +5 -0
  483. package/src/components/ValidationErrorsList.js +147 -0
  484. package/src/components/VimTextInput.js +136 -0
  485. package/src/components/VirtualMessageList.js +893 -0
  486. package/src/components/WorkflowMultiselectDialog.js +118 -0
  487. package/src/components/WorktreeExitDialog.js +220 -0
  488. package/src/components/agents/AgentDetail.js +227 -0
  489. package/src/components/agents/AgentEditor.js +147 -0
  490. package/src/components/agents/AgentNavigationFooter.js +22 -0
  491. package/src/components/agents/AgentsList.js +436 -0
  492. package/src/components/agents/AgentsMenu.js +849 -0
  493. package/src/components/agents/ColorPicker.js +110 -0
  494. package/src/components/agents/ModelSelector.js +63 -0
  495. package/src/components/agents/SnapshotUpdateDialog.js +14 -0
  496. package/src/components/agents/ToolSelector.js +557 -0
  497. package/src/components/agents/agentFileUtils.js +179 -0
  498. package/src/components/agents/generateAgent.js +161 -0
  499. package/src/components/agents/new-agent-creation/CreateAgentWizard.js +89 -0
  500. package/src/components/agents/new-agent-creation/wizard-steps/ColorStep.js +81 -0
  501. package/src/components/agents/new-agent-creation/wizard-steps/ConfirmStep.js +387 -0
  502. package/src/components/agents/new-agent-creation/wizard-steps/ConfirmStepWrapper.js +63 -0
  503. package/src/components/agents/new-agent-creation/wizard-steps/DescriptionStep.js +126 -0
  504. package/src/components/agents/new-agent-creation/wizard-steps/GenerateStep.js +118 -0
  505. package/src/components/agents/new-agent-creation/wizard-steps/LocationStep.js +80 -0
  506. package/src/components/agents/new-agent-creation/wizard-steps/MemoryStep.js +108 -0
  507. package/src/components/agents/new-agent-creation/wizard-steps/MethodStep.js +80 -0
  508. package/src/components/agents/new-agent-creation/wizard-steps/ModelStep.js +49 -0
  509. package/src/components/agents/new-agent-creation/wizard-steps/PromptStep.js +131 -0
  510. package/src/components/agents/new-agent-creation/wizard-steps/ToolsStep.js +52 -0
  511. package/src/components/agents/new-agent-creation/wizard-steps/TypeStep.js +100 -0
  512. package/src/components/agents/types.js +4 -0
  513. package/src/components/agents/utils.js +14 -0
  514. package/src/components/agents/validateAgent.js +79 -0
  515. package/src/components/design-system/Byline.js +72 -0
  516. package/src/components/design-system/Dialog.js +117 -0
  517. package/src/components/design-system/Divider.js +110 -0
  518. package/src/components/design-system/FuzzyPicker.js +191 -0
  519. package/src/components/design-system/KeyboardShortcutHint.js +68 -0
  520. package/src/components/design-system/ListItem.js +184 -0
  521. package/src/components/design-system/LoadingState.js +69 -0
  522. package/src/components/design-system/Pane.js +69 -0
  523. package/src/components/design-system/ProgressBar.js +63 -0
  524. package/src/components/design-system/Ratchet.js +71 -0
  525. package/src/components/design-system/StatusIcon.js +70 -0
  526. package/src/components/design-system/Tabs.js +269 -0
  527. package/src/components/design-system/ThemeProvider.js +137 -0
  528. package/src/components/design-system/ThemedBox.js +126 -0
  529. package/src/components/design-system/ThemedText.js +60 -0
  530. package/src/components/design-system/color.js +22 -0
  531. package/src/components/diff/DiffDetailView.js +285 -0
  532. package/src/components/diff/DiffDialog.js +387 -0
  533. package/src/components/diff/DiffFileList.js +292 -0
  534. package/src/components/grove/Grove.js +483 -0
  535. package/src/components/hooks/HooksConfigMenu.js +583 -0
  536. package/src/components/hooks/PromptDialog.js +82 -0
  537. package/src/components/hooks/SelectEventMode.js +118 -0
  538. package/src/components/hooks/SelectHookMode.js +101 -0
  539. package/src/components/hooks/SelectMatcherMode.js +131 -0
  540. package/src/components/hooks/ViewHookMode.js +204 -0
  541. package/src/components/mcp/CapabilitiesSection.js +56 -0
  542. package/src/components/mcp/ElicitationDialog.js +945 -0
  543. package/src/components/mcp/MCPAgentServerMenu.js +95 -0
  544. package/src/components/mcp/MCPListPanel.js +505 -0
  545. package/src/components/mcp/MCPReconnect.js +168 -0
  546. package/src/components/mcp/MCPRemoteServerMenu.js +460 -0
  547. package/src/components/mcp/MCPSettings.js +414 -0
  548. package/src/components/mcp/MCPStdioServerMenu.js +95 -0
  549. package/src/components/mcp/MCPToolDetailView.js +219 -0
  550. package/src/components/mcp/MCPToolListView.js +137 -0
  551. package/src/components/mcp/McpParsingWarnings.js +212 -0
  552. package/src/components/mcp/index.js +8 -0
  553. package/src/components/mcp/utils/reconnectHelpers.js +35 -0
  554. package/src/components/memory/MemoryFileSelector.js +454 -0
  555. package/src/components/memory/MemoryUpdateNotification.js +43 -0
  556. package/src/components/messageActions.js +418 -0
  557. package/src/components/messages/AdvisorMessage.js +152 -0
  558. package/src/components/messages/AssistantRedactedThinkingMessage.js +28 -0
  559. package/src/components/messages/AssistantTextMessage.js +287 -0
  560. package/src/components/messages/AssistantThinkingMessage.js +70 -0
  561. package/src/components/messages/AssistantToolUseMessage.js +324 -0
  562. package/src/components/messages/AttachmentMessage.js +418 -0
  563. package/src/components/messages/CollapsedReadSearchContent.js +363 -0
  564. package/src/components/messages/CompactBoundaryMessage.js +19 -0
  565. package/src/components/messages/GroupedToolUseContent.js +37 -0
  566. package/src/components/messages/HighlightedThinkingText.js +165 -0
  567. package/src/components/messages/HookProgressMessage.js +111 -0
  568. package/src/components/messages/PlanApprovalMessage.js +213 -0
  569. package/src/components/messages/RateLimitMessage.js +149 -0
  570. package/src/components/messages/ShutdownMessage.js +124 -0
  571. package/src/components/messages/SnipBoundaryMessage.js +7 -0
  572. package/src/components/messages/SystemAPIErrorMessage.js +136 -0
  573. package/src/components/messages/SystemTextMessage.js +842 -0
  574. package/src/components/messages/TaskAssignmentMessage.js +72 -0
  575. package/src/components/messages/UserAgentNotificationMessage.js +78 -0
  576. package/src/components/messages/UserBashInputMessage.js +52 -0
  577. package/src/components/messages/UserBashOutputMessage.js +55 -0
  578. package/src/components/messages/UserChannelMessage.js +130 -0
  579. package/src/components/messages/UserCommandMessage.js +107 -0
  580. package/src/components/messages/UserCrossSessionMessage.js +11 -0
  581. package/src/components/messages/UserForkBoilerplateMessage.js +11 -0
  582. package/src/components/messages/UserGitHubWebhookMessage.js +12 -0
  583. package/src/components/messages/UserImageMessage.js +54 -0
  584. package/src/components/messages/UserLocalCommandOutputMessage.js +170 -0
  585. package/src/components/messages/UserMemoryInputMessage.js +73 -0
  586. package/src/components/messages/UserPlanMessage.js +38 -0
  587. package/src/components/messages/UserPromptMessage.js +63 -0
  588. package/src/components/messages/UserResourceUpdateMessage.js +102 -0
  589. package/src/components/messages/UserTeammateMessage.js +156 -0
  590. package/src/components/messages/UserTextMessage.js +270 -0
  591. package/src/components/messages/UserToolResultMessage/RejectedPlanMessage.js +28 -0
  592. package/src/components/messages/UserToolResultMessage/RejectedToolUseMessage.js +17 -0
  593. package/src/components/messages/UserToolResultMessage/UserToolCanceledMessage.js +17 -0
  594. package/src/components/messages/UserToolResultMessage/UserToolErrorMessage.js +92 -0
  595. package/src/components/messages/UserToolResultMessage/UserToolRejectMessage.js +74 -0
  596. package/src/components/messages/UserToolResultMessage/UserToolResultMessage.js +84 -0
  597. package/src/components/messages/UserToolResultMessage/UserToolSuccessMessage.js +58 -0
  598. package/src/components/messages/UserToolResultMessage/utils.js +43 -0
  599. package/src/components/messages/nullRenderingAttachments.js +58 -0
  600. package/src/components/messages/teamMemCollapsed.js +142 -0
  601. package/src/components/messages/teamMemSaved.js +16 -0
  602. package/src/components/permissions/AskUserQuestionPermissionRequest/AskUserQuestionPermissionRequest.js +659 -0
  603. package/src/components/permissions/AskUserQuestionPermissionRequest/PreviewBox.js +219 -0
  604. package/src/components/permissions/AskUserQuestionPermissionRequest/PreviewQuestionView.js +227 -0
  605. package/src/components/permissions/AskUserQuestionPermissionRequest/QuestionNavigationBar.js +175 -0
  606. package/src/components/permissions/AskUserQuestionPermissionRequest/QuestionView.js +444 -0
  607. package/src/components/permissions/AskUserQuestionPermissionRequest/SubmitQuestionsView.js +137 -0
  608. package/src/components/permissions/AskUserQuestionPermissionRequest/use-multiple-choice-state.js +100 -0
  609. package/src/components/permissions/BashPermissionRequest/BashPermissionRequest.js +404 -0
  610. package/src/components/permissions/BashPermissionRequest/bashToolUseOptions.js +110 -0
  611. package/src/components/permissions/ComputerUseApproval/ComputerUseApproval.js +449 -0
  612. package/src/components/permissions/EnterPlanModePermissionRequest/EnterPlanModePermissionRequest.js +126 -0
  613. package/src/components/permissions/ExitPlanModePermissionRequest/ExitPlanModePermissionRequest.js +653 -0
  614. package/src/components/permissions/FallbackPermissionRequest.js +349 -0
  615. package/src/components/permissions/FileEditPermissionRequest/FileEditPermissionRequest.js +185 -0
  616. package/src/components/permissions/FilePermissionDialog/FilePermissionDialog.js +108 -0
  617. package/src/components/permissions/FilePermissionDialog/ideDiffConfig.js +13 -0
  618. package/src/components/permissions/FilePermissionDialog/permissionOptions.js +137 -0
  619. package/src/components/permissions/FilePermissionDialog/useFilePermissionDialog.js +131 -0
  620. package/src/components/permissions/FilePermissionDialog/usePermissionHandler.js +86 -0
  621. package/src/components/permissions/FileWritePermissionRequest/FileWritePermissionRequest.js +164 -0
  622. package/src/components/permissions/FileWritePermissionRequest/FileWriteToolDiff.js +79 -0
  623. package/src/components/permissions/FilesystemPermissionRequest/FilesystemPermissionRequest.js +113 -0
  624. package/src/components/permissions/MonitorPermissionRequest/MonitorPermissionRequest.js +7 -0
  625. package/src/components/permissions/NotebookEditPermissionRequest/NotebookEditPermissionRequest.js +164 -0
  626. package/src/components/permissions/NotebookEditPermissionRequest/NotebookEditToolDiff.js +218 -0
  627. package/src/components/permissions/PermissionDecisionDebugInfo.js +467 -0
  628. package/src/components/permissions/PermissionDialog.js +55 -0
  629. package/src/components/permissions/PermissionExplanation.js +269 -0
  630. package/src/components/permissions/PermissionPrompt.js +316 -0
  631. package/src/components/permissions/PermissionRequest.js +159 -0
  632. package/src/components/permissions/PermissionRequestTitle.js +58 -0
  633. package/src/components/permissions/PermissionRuleExplanation.js +110 -0
  634. package/src/components/permissions/PowerShellPermissionRequest/PowerShellPermissionRequest.js +178 -0
  635. package/src/components/permissions/PowerShellPermissionRequest/powershellToolUseOptions.js +73 -0
  636. package/src/components/permissions/ReviewArtifactPermissionRequest/ReviewArtifactPermissionRequest.js +7 -0
  637. package/src/components/permissions/SandboxPermissionRequest.js +162 -0
  638. package/src/components/permissions/SedEditPermissionRequest/SedEditPermissionRequest.js +228 -0
  639. package/src/components/permissions/SkillPermissionRequest/SkillPermissionRequest.js +385 -0
  640. package/src/components/permissions/WebFetchPermissionRequest/WebFetchPermissionRequest.js +259 -0
  641. package/src/components/permissions/WorkerBadge.js +44 -0
  642. package/src/components/permissions/WorkerPendingPermission.js +107 -0
  643. package/src/components/permissions/hooks.js +163 -0
  644. package/src/components/permissions/rules/AddPermissionRules.js +171 -0
  645. package/src/components/permissions/rules/AddWorkspaceDirectory.js +335 -0
  646. package/src/components/permissions/rules/PermissionRuleDescription.js +78 -0
  647. package/src/components/permissions/rules/PermissionRuleInput.js +136 -0
  648. package/src/components/permissions/rules/PermissionRuleList.js +1190 -0
  649. package/src/components/permissions/rules/RecentDenialsTab.js +205 -0
  650. package/src/components/permissions/rules/RemoveWorkspaceDirectory.js +103 -0
  651. package/src/components/permissions/rules/WorkspaceTab.js +133 -0
  652. package/src/components/permissions/shellPermissionHelpers.js +112 -0
  653. package/src/components/permissions/useShellPermissionFeedback.js +108 -0
  654. package/src/components/permissions/utils.js +14 -0
  655. package/src/components/sandbox/SandboxConfigTab.js +48 -0
  656. package/src/components/sandbox/SandboxDependenciesTab.js +123 -0
  657. package/src/components/sandbox/SandboxDoctorSection.js +47 -0
  658. package/src/components/sandbox/SandboxOverridesTab.js +193 -0
  659. package/src/components/sandbox/SandboxSettings.js +297 -0
  660. package/src/components/shell/ExpandShellOutputContext.js +33 -0
  661. package/src/components/shell/OutputLine.js +110 -0
  662. package/src/components/shell/ShellProgressMessage.js +144 -0
  663. package/src/components/shell/ShellTimeDisplay.js +72 -0
  664. package/src/components/skills/SkillsMenu.js +239 -0
  665. package/src/components/tasks/AsyncAgentDetailDialog.js +235 -0
  666. package/src/components/tasks/BackgroundTask.js +364 -0
  667. package/src/components/tasks/BackgroundTaskStatus.js +419 -0
  668. package/src/components/tasks/BackgroundTasksDialog.js +494 -0
  669. package/src/components/tasks/DreamDetailDialog.js +251 -0
  670. package/src/components/tasks/InProcessTeammateDetailDialog.js +275 -0
  671. package/src/components/tasks/MonitorMcpDetailDialog.js +7 -0
  672. package/src/components/tasks/RemoteSessionDetailDialog.js +868 -0
  673. package/src/components/tasks/RemoteSessionProgress.js +249 -0
  674. package/src/components/tasks/ShellDetailDialog.js +403 -0
  675. package/src/components/tasks/ShellProgress.js +77 -0
  676. package/src/components/tasks/WorkflowDetailDialog.js +7 -0
  677. package/src/components/tasks/renderToolActivity.js +29 -0
  678. package/src/components/tasks/taskStatusUtils.js +94 -0
  679. package/src/components/teams/TeamStatus.js +77 -0
  680. package/src/components/teams/TeamsDialog.js +673 -0
  681. package/src/components/ui/OrderedList.js +66 -0
  682. package/src/components/ui/OrderedListItem.js +41 -0
  683. package/src/components/ui/TreeSelect.js +300 -0
  684. package/src/components/wizard/WizardDialogLayout.js +48 -0
  685. package/src/components/wizard/WizardNavigationFooter.js +11 -0
  686. package/src/components/wizard/WizardProvider.js +217 -0
  687. package/src/components/wizard/index.js +4 -0
  688. package/src/components/wizard/useWizard.js +9 -0
  689. package/src/constants/apiLimits.js +81 -0
  690. package/src/constants/betas.js +45 -0
  691. package/src/constants/common.js +29 -0
  692. package/src/constants/cyberRiskInstruction.js +23 -0
  693. package/src/constants/errorIds.js +14 -0
  694. package/src/constants/figures.js +38 -0
  695. package/src/constants/files.js +150 -0
  696. package/src/constants/github-app.js +139 -0
  697. package/src/constants/identity.js +112 -0
  698. package/src/constants/keys.js +10 -0
  699. package/src/constants/messages.js +1 -0
  700. package/src/constants/oauth.js +175 -0
  701. package/src/constants/outputStyles.js +162 -0
  702. package/src/constants/product.js +54 -0
  703. package/src/constants/prompts.js +994 -0
  704. package/src/constants/spinnerVerbs.js +98 -0
  705. package/src/constants/system.js +77 -0
  706. package/src/constants/systemPromptSections.js +39 -0
  707. package/src/constants/toolLimits.js +50 -0
  708. package/src/constants/tools.js +103 -0
  709. package/src/constants/turnCompletionVerbs.js +12 -0
  710. package/src/constants/xml.js +73 -0
  711. package/src/context/QueuedMessageContext.js +51 -0
  712. package/src/context/fpsMetrics.js +22 -0
  713. package/src/context/mailbox.js +35 -0
  714. package/src/context/modalContext.js +34 -0
  715. package/src/context/notifications.js +199 -0
  716. package/src/context/overlayContext.js +149 -0
  717. package/src/context/promptOverlayContext.js +118 -0
  718. package/src/context/stats.js +207 -0
  719. package/src/context/voice.js +74 -0
  720. package/src/context.js +146 -0
  721. package/src/coordinator/coordinatorMode.js +345 -0
  722. package/src/coordinator/workerAgent.js +24 -0
  723. package/src/cost-tracker.js +208 -0
  724. package/src/costHook.js +17 -0
  725. package/src/daemon/main.js +19 -0
  726. package/src/dialogLaunchers.js +77 -0
  727. package/src/entrypoints/agentSdkTypes.js +202 -0
  728. package/src/entrypoints/cli.js +226 -0
  729. package/src/entrypoints/init.js +265 -0
  730. package/src/entrypoints/mcp.js +141 -0
  731. package/src/entrypoints/sandboxTypes.js +112 -0
  732. package/src/entrypoints/sdk/controlSchemas.js +452 -0
  733. package/src/entrypoints/sdk/controlTypes.js +1 -0
  734. package/src/entrypoints/sdk/coreSchemas.js +1331 -0
  735. package/src/entrypoints/sdk/coreTypes.generated.js +3 -0
  736. package/src/entrypoints/sdk/coreTypes.js +49 -0
  737. package/src/entrypoints/sdk/runtimeTypes.js +1 -0
  738. package/src/entrypoints/sdk/sdkUtilityTypes.js +1 -0
  739. package/src/entrypoints/sdk/settingsTypes.generated.js +1 -0
  740. package/src/entrypoints/sdk/toolTypes.js +1 -0
  741. package/src/environment-runner/main.js +8 -0
  742. package/src/history.js +386 -0
  743. package/src/hooks/fileSuggestions.js +635 -0
  744. package/src/hooks/notifs/useAntOrgWarningNotification.js +5 -0
  745. package/src/hooks/notifs/useAutoModeUnavailableNotification.js +47 -0
  746. package/src/hooks/notifs/useCanSwitchToExistingSubscription.js +58 -0
  747. package/src/hooks/notifs/useDeprecationWarningNotification.js +43 -0
  748. package/src/hooks/notifs/useFastModeNotification.js +164 -0
  749. package/src/hooks/notifs/useIDEStatusIndicator.js +174 -0
  750. package/src/hooks/notifs/useInstallMessages.js +27 -0
  751. package/src/hooks/notifs/useLspInitializationNotification.js +144 -0
  752. package/src/hooks/notifs/useMcpConnectivityStatus.js +81 -0
  753. package/src/hooks/notifs/useModelMigrationNotifications.js +53 -0
  754. package/src/hooks/notifs/useNpmDeprecationNotification.js +25 -0
  755. package/src/hooks/notifs/usePluginAutoupdateNotification.js +83 -0
  756. package/src/hooks/notifs/usePluginInstallationStatus.js +128 -0
  757. package/src/hooks/notifs/useRateLimitWarningNotification.js +119 -0
  758. package/src/hooks/notifs/useSettingsErrors.js +64 -0
  759. package/src/hooks/notifs/useStartupNotification.js +33 -0
  760. package/src/hooks/notifs/useTeammateShutdownNotification.js +64 -0
  761. package/src/hooks/renderPlaceholder.js +26 -0
  762. package/src/hooks/toolPermission/PermissionContext.js +211 -0
  763. package/src/hooks/toolPermission/handlers/coordinatorHandler.js +44 -0
  764. package/src/hooks/toolPermission/handlers/interactiveHandler.js +397 -0
  765. package/src/hooks/toolPermission/handlers/swarmWorkerHandler.js +108 -0
  766. package/src/hooks/toolPermission/permissionLogging.js +145 -0
  767. package/src/hooks/unifiedSuggestions.js +130 -0
  768. package/src/hooks/useAfterFirstRender.js +12 -0
  769. package/src/hooks/useApiKeyVerification.js +63 -0
  770. package/src/hooks/useArrowKeyHistory.js +203 -0
  771. package/src/hooks/useAssistantHistory.js +193 -0
  772. package/src/hooks/useAwaySummary.js +105 -0
  773. package/src/hooks/useBackgroundTaskNavigation.js +204 -0
  774. package/src/hooks/useBlink.js +28 -0
  775. package/src/hooks/useCanUseTool.js +193 -0
  776. package/src/hooks/useCancelRequest.js +195 -0
  777. package/src/hooks/useChromeExtensionNotification.js +50 -0
  778. package/src/hooks/useClipboardImageHint.js +59 -0
  779. package/src/hooks/useCommandKeybindings.js +87 -0
  780. package/src/hooks/useCommandQueue.js +10 -0
  781. package/src/hooks/useCopyOnSelect.js +88 -0
  782. package/src/hooks/useDeferredHookMessages.js +43 -0
  783. package/src/hooks/useDiffData.js +69 -0
  784. package/src/hooks/useDiffInIDE.js +252 -0
  785. package/src/hooks/useDirectConnect.js +150 -0
  786. package/src/hooks/useDoublePress.js +44 -0
  787. package/src/hooks/useDynamicConfig.js +17 -0
  788. package/src/hooks/useElapsedTime.js +25 -0
  789. package/src/hooks/useExitOnCtrlCD.js +57 -0
  790. package/src/hooks/useExitOnCtrlCDWithKeybindings.js +17 -0
  791. package/src/hooks/useFileHistorySnapshotInit.js +14 -0
  792. package/src/hooks/useGlobalKeybindings.js +213 -0
  793. package/src/hooks/useHistorySearch.js +241 -0
  794. package/src/hooks/useIDEIntegration.js +56 -0
  795. package/src/hooks/useIdeAtMentioned.js +51 -0
  796. package/src/hooks/useIdeConnectionStatus.js +21 -0
  797. package/src/hooks/useIdeLogging.js +29 -0
  798. package/src/hooks/useIdeSelection.js +106 -0
  799. package/src/hooks/useInboxPoller.js +709 -0
  800. package/src/hooks/useInputBuffer.js +73 -0
  801. package/src/hooks/useIssueFlagBanner.js +115 -0
  802. package/src/hooks/useLogMessages.js +98 -0
  803. package/src/hooks/useLspPluginRecommendation.js +176 -0
  804. package/src/hooks/useMailboxBridge.js +15 -0
  805. package/src/hooks/useMainLoopModel.js +25 -0
  806. package/src/hooks/useManagePlugins.js +261 -0
  807. package/src/hooks/useMemoryUsage.js +28 -0
  808. package/src/hooks/useMergedClients.js +11 -0
  809. package/src/hooks/useMergedCommands.js +10 -0
  810. package/src/hooks/useMergedTools.js +32 -0
  811. package/src/hooks/useMinDisplayTime.js +26 -0
  812. package/src/hooks/useNotifyAfterTimeout.js +51 -0
  813. package/src/hooks/useOfficialMarketplaceNotification.js +47 -0
  814. package/src/hooks/usePasteHandler.js +195 -0
  815. package/src/hooks/usePluginRecommendationBase.js +101 -0
  816. package/src/hooks/usePrStatus.js +91 -0
  817. package/src/hooks/usePromptSuggestion.js +128 -0
  818. package/src/hooks/usePromptsFromClaudeInChrome.js +66 -0
  819. package/src/hooks/useQueueProcessor.js +46 -0
  820. package/src/hooks/useRemoteSession.js +431 -0
  821. package/src/hooks/useReplBridge.js +715 -0
  822. package/src/hooks/useSSHSession.js +167 -0
  823. package/src/hooks/useScheduledTasks.js +104 -0
  824. package/src/hooks/useSearchInput.js +302 -0
  825. package/src/hooks/useSessionBackgrounding.js +132 -0
  826. package/src/hooks/useSettings.js +10 -0
  827. package/src/hooks/useSettingsChange.js +13 -0
  828. package/src/hooks/useSkillImprovementSurvey.js +69 -0
  829. package/src/hooks/useSkillsChange.js +51 -0
  830. package/src/hooks/useSwarmInitialization.js +67 -0
  831. package/src/hooks/useSwarmPermissionPoller.js +215 -0
  832. package/src/hooks/useTaskListWatcher.js +157 -0
  833. package/src/hooks/useTasksV2.js +220 -0
  834. package/src/hooks/useTeammateViewAutoExit.js +55 -0
  835. package/src/hooks/useTeleportResume.js +81 -0
  836. package/src/hooks/useTerminalSize.js +9 -0
  837. package/src/hooks/useTextInput.js +397 -0
  838. package/src/hooks/useThaddeusHintRecommendation.js +117 -0
  839. package/src/hooks/useTimeout.js +10 -0
  840. package/src/hooks/useTurnDiffs.js +160 -0
  841. package/src/hooks/useTypeahead.js +1250 -0
  842. package/src/hooks/useUpdateNotification.js +21 -0
  843. package/src/hooks/useVimInput.js +232 -0
  844. package/src/hooks/useVirtualScroll.js +627 -0
  845. package/src/hooks/useVoice.js +952 -0
  846. package/src/hooks/useVoiceEnabled.js +21 -0
  847. package/src/hooks/useVoiceIntegration.js +629 -0
  848. package/src/infrastructure/audit.js +210 -0
  849. package/src/infrastructure/guardrails.js +513 -0
  850. package/src/infrastructure/index.js +11 -0
  851. package/src/ink/Ansi.js +269 -0
  852. package/src/ink/bidi.js +117 -0
  853. package/src/ink/clearTerminal.js +58 -0
  854. package/src/ink/colorize.js +198 -0
  855. package/src/ink/components/AlternateScreen.js +74 -0
  856. package/src/ink/components/App.js +562 -0
  857. package/src/ink/components/AppContext.js +11 -0
  858. package/src/ink/components/Box.js +155 -0
  859. package/src/ink/components/Button.js +166 -0
  860. package/src/ink/components/ClockContext.js +108 -0
  861. package/src/ink/components/CursorDeclarationContext.js +3 -0
  862. package/src/ink/components/ErrorOverview.js +50 -0
  863. package/src/ink/components/Link.js +34 -0
  864. package/src/ink/components/Newline.js +30 -0
  865. package/src/ink/components/NoSelect.js +57 -0
  866. package/src/ink/components/RawAnsi.js +46 -0
  867. package/src/ink/components/ScrollBox.js +171 -0
  868. package/src/ink/components/Spacer.js +20 -0
  869. package/src/ink/components/StdinContext.js +16 -0
  870. package/src/ink/components/TerminalFocusContext.js +45 -0
  871. package/src/ink/components/TerminalSizeContext.js +3 -0
  872. package/src/ink/components/Text.js +195 -0
  873. package/src/ink/constants.js +2 -0
  874. package/src/ink/dom.js +298 -0
  875. package/src/ink/events/click-event.js +36 -0
  876. package/src/ink/events/dispatcher.js +172 -0
  877. package/src/ink/events/emitter.js +31 -0
  878. package/src/ink/events/event-handlers.js +30 -0
  879. package/src/ink/events/event.js +9 -0
  880. package/src/ink/events/focus-event.js +16 -0
  881. package/src/ink/events/input-event.js +161 -0
  882. package/src/ink/events/keyboard-event.js +46 -0
  883. package/src/ink/events/terminal-event.js +78 -0
  884. package/src/ink/events/terminal-focus-event.js +15 -0
  885. package/src/ink/focus.js +158 -0
  886. package/src/ink/frame.js +30 -0
  887. package/src/ink/get-max-width.js +23 -0
  888. package/src/ink/hit-test.js +113 -0
  889. package/src/ink/hooks/use-animation-frame.js +48 -0
  890. package/src/ink/hooks/use-app.js +7 -0
  891. package/src/ink/hooks/use-declared-cursor.js +60 -0
  892. package/src/ink/hooks/use-input.js +70 -0
  893. package/src/ink/hooks/use-interval.js +54 -0
  894. package/src/ink/hooks/use-search-highlight.js +32 -0
  895. package/src/ink/hooks/use-selection.js +60 -0
  896. package/src/ink/hooks/use-stdin.js +7 -0
  897. package/src/ink/hooks/use-tab-status.js +57 -0
  898. package/src/ink/hooks/use-terminal-focus.js +15 -0
  899. package/src/ink/hooks/use-terminal-title.js +29 -0
  900. package/src/ink/hooks/use-terminal-viewport.js +77 -0
  901. package/src/ink/ink.js +1645 -0
  902. package/src/ink/instances.js +7 -0
  903. package/src/ink/layout/engine.js +4 -0
  904. package/src/ink/layout/geometry.js +61 -0
  905. package/src/ink/layout/node.js +62 -0
  906. package/src/ink/layout/yoga.js +237 -0
  907. package/src/ink/line-width-cache.js +19 -0
  908. package/src/ink/log-update.js +583 -0
  909. package/src/ink/measure-element.js +8 -0
  910. package/src/ink/measure-text.js +35 -0
  911. package/src/ink/node-cache.js +30 -0
  912. package/src/ink/optimizer.js +81 -0
  913. package/src/ink/output.js +556 -0
  914. package/src/ink/parse-keypress.js +695 -0
  915. package/src/ink/reconciler.js +384 -0
  916. package/src/ink/render-border.js +134 -0
  917. package/src/ink/render-node-to-output.js +1216 -0
  918. package/src/ink/render-to-screen.js +171 -0
  919. package/src/ink/renderer.js +129 -0
  920. package/src/ink/root.js +80 -0
  921. package/src/ink/screen.js +1132 -0
  922. package/src/ink/searchHighlight.js +78 -0
  923. package/src/ink/selection.js +792 -0
  924. package/src/ink/squash-text-nodes.js +56 -0
  925. package/src/ink/stringWidth.js +200 -0
  926. package/src/ink/styles.js +299 -0
  927. package/src/ink/supports-hyperlinks.js +40 -0
  928. package/src/ink/tabstops.js +39 -0
  929. package/src/ink/terminal-focus-state.js +35 -0
  930. package/src/ink/terminal-querier.js +173 -0
  931. package/src/ink/terminal.js +208 -0
  932. package/src/ink/termio/ansi.js +70 -0
  933. package/src/ink/termio/csi.js +260 -0
  934. package/src/ink/termio/dec.js +53 -0
  935. package/src/ink/termio/esc.js +55 -0
  936. package/src/ink/termio/osc.js +432 -0
  937. package/src/ink/termio/parser.js +356 -0
  938. package/src/ink/termio/sgr.js +292 -0
  939. package/src/ink/termio/tokenize.js +264 -0
  940. package/src/ink/termio/types.js +55 -0
  941. package/src/ink/termio.js +24 -0
  942. package/src/ink/useTerminalNotification.js +57 -0
  943. package/src/ink/warn.js +10 -0
  944. package/src/ink/widest-line.js +14 -0
  945. package/src/ink/wrap-text.js +54 -0
  946. package/src/ink/wrapAnsi.js +6 -0
  947. package/src/ink.js +50 -0
  948. package/src/integrations/credentialStore.js +176 -0
  949. package/src/integrations/index.js +5 -0
  950. package/src/integrations/integrationManager.js +180 -0
  951. package/src/integrations/providers/BaseProvider.js +180 -0
  952. package/src/integrations/providers/GitHubProvider.js +217 -0
  953. package/src/integrations/providers/GmailProvider.js +204 -0
  954. package/src/integrations/providers/GoogleCalendarProvider.js +113 -0
  955. package/src/integrations/providers/HubSpotProvider.js +159 -0
  956. package/src/integrations/providers/JiraProvider.js +216 -0
  957. package/src/integrations/providers/NotionProvider.js +221 -0
  958. package/src/integrations/providers/QuickBooksProvider.js +176 -0
  959. package/src/integrations/providers/SlackProvider.js +174 -0
  960. package/src/integrations/providers/StripeProvider.js +206 -0
  961. package/src/integrations/providers/TwilioProvider.js +239 -0
  962. package/src/integrations/providers/_template.js +112 -0
  963. package/src/integrations/types.js +7 -0
  964. package/src/interactiveHelpers.js +308 -0
  965. package/src/jobs/classifier.js +6 -0
  966. package/src/keybindings/KeybindingContext.js +184 -0
  967. package/src/keybindings/KeybindingProviderSetup.js +259 -0
  968. package/src/keybindings/defaultBindings.js +333 -0
  969. package/src/keybindings/loadUserBindings.js +393 -0
  970. package/src/keybindings/match.js +111 -0
  971. package/src/keybindings/parser.js +184 -0
  972. package/src/keybindings/reservedShortcuts.js +109 -0
  973. package/src/keybindings/resolver.js +182 -0
  974. package/src/keybindings/schema.js +205 -0
  975. package/src/keybindings/shortcutFormat.js +48 -0
  976. package/src/keybindings/template.js +40 -0
  977. package/src/keybindings/useKeybinding.js +161 -0
  978. package/src/keybindings/useShortcutDisplay.js +43 -0
  979. package/src/keybindings/validate.js +395 -0
  980. package/src/main.js +4128 -0
  981. package/src/memdir/findRelevantMemories.js +99 -0
  982. package/src/memdir/memdir.js +406 -0
  983. package/src/memdir/memoryAge.js +52 -0
  984. package/src/memdir/memoryScan.js +65 -0
  985. package/src/memdir/memoryShapeTelemetry.js +8 -0
  986. package/src/memdir/memoryTypes.js +260 -0
  987. package/src/memdir/paths.js +235 -0
  988. package/src/memdir/teamMemPaths.js +261 -0
  989. package/src/memdir/teamMemPrompts.js +82 -0
  990. package/src/migrations/migrateAutoUpdatesToSettings.js +47 -0
  991. package/src/migrations/migrateBypassPermissionsAcceptedToSettings.js +32 -0
  992. package/src/migrations/migrateEnableAllProjectMcpServersToSettings.js +83 -0
  993. package/src/migrations/migrateFennecToOpus.js +39 -0
  994. package/src/migrations/migrateLegacyOpusToCurrent.js +44 -0
  995. package/src/migrations/migrateOpusToOpus1m.js +31 -0
  996. package/src/migrations/migrateReplBridgeEnabledToRemoteControlAtStartup.js +23 -0
  997. package/src/migrations/migrateSonnet1mToSonnet45.js +38 -0
  998. package/src/migrations/migrateSonnet45ToSonnet46.js +48 -0
  999. package/src/migrations/resetAutoModeOptInForDefaultOffer.js +47 -0
  1000. package/src/migrations/resetProToOpusDefault.js +46 -0
  1001. package/src/moreright/useMoreRight.js +13 -0
  1002. package/src/native-ts/color-diff/index.js +819 -0
  1003. package/src/native-ts/file-index/index.js +328 -0
  1004. package/src/native-ts/yoga-layout/enums.js +101 -0
  1005. package/src/native-ts/yoga-layout/index.js +2113 -0
  1006. package/src/outputStyles/loadOutputStylesDir.js +71 -0
  1007. package/src/plugins/builtinPlugins.js +132 -0
  1008. package/src/plugins/bundled/index.js +22 -0
  1009. package/src/proactive/index.js +138 -0
  1010. package/src/proactive/useProactive.js +82 -0
  1011. package/src/projectOnboardingState.js +61 -0
  1012. package/src/query/config.js +17 -0
  1013. package/src/query/deps.js +12 -0
  1014. package/src/query/stopHooks.js +332 -0
  1015. package/src/query/tokenBudget.js +49 -0
  1016. package/src/query.js +1264 -0
  1017. package/src/remote/RemoteSessionManager.js +172 -0
  1018. package/src/remote/SessionsWebSocket.js +308 -0
  1019. package/src/remote/remotePermissionBridge.js +70 -0
  1020. package/src/remote/sdkMessageAdapter.js +227 -0
  1021. package/src/replLauncher.js +7 -0
  1022. package/src/schemas/hooks.js +174 -0
  1023. package/src/screens/Doctor.js +580 -0
  1024. package/src/screens/REPL.js +4500 -0
  1025. package/src/screens/ResumeConversation.js +339 -0
  1026. package/src/self-hosted-runner/main.js +8 -0
  1027. package/src/server/backends/dangerousBackend.js +8 -0
  1028. package/src/server/connectHeadless.js +6 -0
  1029. package/src/server/createDirectConnectSession.js +62 -0
  1030. package/src/server/directConnectManager.js +153 -0
  1031. package/src/server/lockfile.js +11 -0
  1032. package/src/server/parseConnectUrl.js +20 -0
  1033. package/src/server/server.js +12 -0
  1034. package/src/server/serverBanner.js +9 -0
  1035. package/src/server/serverLog.js +11 -0
  1036. package/src/server/sessionManager.js +19 -0
  1037. package/src/server/types.js +7 -0
  1038. package/src/services/AgentSummary/agentSummary.js +147 -0
  1039. package/src/services/MagicDocs/magicDocs.js +193 -0
  1040. package/src/services/MagicDocs/prompts.js +110 -0
  1041. package/src/services/PromptSuggestion/promptSuggestion.js +402 -0
  1042. package/src/services/PromptSuggestion/speculation.js +643 -0
  1043. package/src/services/SessionMemory/prompts.js +254 -0
  1044. package/src/services/SessionMemory/sessionMemory.js +358 -0
  1045. package/src/services/SessionMemory/sessionMemoryUtils.js +157 -0
  1046. package/src/services/analytics/config.js +27 -0
  1047. package/src/services/analytics/datadog.js +26 -0
  1048. package/src/services/analytics/firstPartyEventLogger.js +65 -0
  1049. package/src/services/analytics/firstPartyEventLoggingExporter.js +595 -0
  1050. package/src/services/analytics/growthbook.js +103 -0
  1051. package/src/services/analytics/index.js +91 -0
  1052. package/src/services/analytics/metadata.js +696 -0
  1053. package/src/services/analytics/sink.js +19 -0
  1054. package/src/services/analytics/sinkKillswitch.js +19 -0
  1055. package/src/services/api/adminRequests.js +57 -0
  1056. package/src/services/api/bootstrap.js +118 -0
  1057. package/src/services/api/claude.js +2466 -0
  1058. package/src/services/api/client.js +335 -0
  1059. package/src/services/api/dumpPrompts.js +174 -0
  1060. package/src/services/api/emptyUsage.js +20 -0
  1061. package/src/services/api/errorUtils.js +203 -0
  1062. package/src/services/api/errors.js +926 -0
  1063. package/src/services/api/filesApi.js +523 -0
  1064. package/src/services/api/firstTokenDate.js +49 -0
  1065. package/src/services/api/grove.js +44 -0
  1066. package/src/services/api/logging.js +484 -0
  1067. package/src/services/api/metricsOptOut.js +15 -0
  1068. package/src/services/api/overageCreditGrant.js +123 -0
  1069. package/src/services/api/promptCacheBreakDetection.js +510 -0
  1070. package/src/services/api/referral.js +219 -0
  1071. package/src/services/api/sessionIngress.js +358 -0
  1072. package/src/services/api/ultrareviewQuota.js +29 -0
  1073. package/src/services/api/usage.js +31 -0
  1074. package/src/services/api/withRetry.js +587 -0
  1075. package/src/services/api/xai/anthropic-shim.js +885 -0
  1076. package/src/services/api/xai/brightDataSearch.js +161 -0
  1077. package/src/services/api/xai/thaddeus-engine.js +605 -0
  1078. package/src/services/api/xai/xai-client.js +276 -0
  1079. package/src/services/autoDream/autoDream.js +244 -0
  1080. package/src/services/autoDream/config.js +17 -0
  1081. package/src/services/autoDream/consolidationLock.js +122 -0
  1082. package/src/services/autoDream/consolidationPrompt.js +55 -0
  1083. package/src/services/awaySummary.js +61 -0
  1084. package/src/services/claudeAiLimits.js +331 -0
  1085. package/src/services/claudeAiLimitsHook.js +15 -0
  1086. package/src/services/compact/apiMicrocompact.js +97 -0
  1087. package/src/services/compact/autoCompact.js +234 -0
  1088. package/src/services/compact/cachedMCConfig.js +5 -0
  1089. package/src/services/compact/compact.js +1256 -0
  1090. package/src/services/compact/compactWarningHook.js +12 -0
  1091. package/src/services/compact/compactWarningState.js +15 -0
  1092. package/src/services/compact/grouping.js +58 -0
  1093. package/src/services/compact/microCompact.js +414 -0
  1094. package/src/services/compact/postCompactCleanup.js +70 -0
  1095. package/src/services/compact/prompt.js +325 -0
  1096. package/src/services/compact/reactiveCompact.js +20 -0
  1097. package/src/services/compact/sessionMemoryCompact.js +467 -0
  1098. package/src/services/compact/snipCompact.js +23 -0
  1099. package/src/services/compact/snipProjection.js +11 -0
  1100. package/src/services/compact/timeBasedMCConfig.js +11 -0
  1101. package/src/services/contextCollapse/index.js +33 -0
  1102. package/src/services/contextCollapse/operations.js +5 -0
  1103. package/src/services/contextCollapse/persist.js +5 -0
  1104. package/src/services/diagnosticTracking.js +282 -0
  1105. package/src/services/elevenlabsTTS.js +245 -0
  1106. package/src/services/extractMemories/extractMemories.js +442 -0
  1107. package/src/services/extractMemories/prompts.js +129 -0
  1108. package/src/services/internalLogging.js +68 -0
  1109. package/src/services/lsp/LSPClient.js +306 -0
  1110. package/src/services/lsp/LSPDiagnosticRegistry.js +277 -0
  1111. package/src/services/lsp/LSPServerInstance.js +388 -0
  1112. package/src/services/lsp/LSPServerManager.js +305 -0
  1113. package/src/services/lsp/config.js +57 -0
  1114. package/src/services/lsp/manager.js +246 -0
  1115. package/src/services/lsp/passiveFeedback.js +226 -0
  1116. package/src/services/mcp/InProcessTransport.js +54 -0
  1117. package/src/services/mcp/MCPConnectionManager.js +50 -0
  1118. package/src/services/mcp/SdkControlTransport.js +115 -0
  1119. package/src/services/mcp/auth.js +1882 -0
  1120. package/src/services/mcp/channelAllowlist.js +57 -0
  1121. package/src/services/mcp/channelNotification.js +235 -0
  1122. package/src/services/mcp/channelPermissions.js +192 -0
  1123. package/src/services/mcp/claudeai.js +123 -0
  1124. package/src/services/mcp/client.js +2478 -0
  1125. package/src/services/mcp/config.js +1271 -0
  1126. package/src/services/mcp/elicitationHandler.js +192 -0
  1127. package/src/services/mcp/envExpansion.js +30 -0
  1128. package/src/services/mcp/headersHelper.js +93 -0
  1129. package/src/services/mcp/mcpStringUtils.js +85 -0
  1130. package/src/services/mcp/normalization.js +21 -0
  1131. package/src/services/mcp/oauthPort.js +69 -0
  1132. package/src/services/mcp/officialRegistry.js +20 -0
  1133. package/src/services/mcp/types.js +94 -0
  1134. package/src/services/mcp/useManageMCPConnections.js +818 -0
  1135. package/src/services/mcp/utils.js +433 -0
  1136. package/src/services/mcp/vscodeSdkMcp.js +69 -0
  1137. package/src/services/mcp/xaa.js +342 -0
  1138. package/src/services/mcp/xaaIdpLogin.js +377 -0
  1139. package/src/services/mcpServerApproval.js +30 -0
  1140. package/src/services/mockRateLimits.js +666 -0
  1141. package/src/services/notifier.js +114 -0
  1142. package/src/services/oauth/auth-code-listener.js +165 -0
  1143. package/src/services/oauth/client.js +397 -0
  1144. package/src/services/oauth/crypto.js +19 -0
  1145. package/src/services/oauth/getOauthProfile.js +48 -0
  1146. package/src/services/oauth/index.js +133 -0
  1147. package/src/services/plugins/PluginInstallationManager.js +139 -0
  1148. package/src/services/plugins/pluginCliCommands.js +230 -0
  1149. package/src/services/plugins/pluginOperations.js +826 -0
  1150. package/src/services/policyLimits/index.js +547 -0
  1151. package/src/services/policyLimits/types.js +9 -0
  1152. package/src/services/preventSleep.js +143 -0
  1153. package/src/services/rateLimitMessages.js +271 -0
  1154. package/src/services/rateLimitMocking.js +91 -0
  1155. package/src/services/remoteManagedSettings/index.js +534 -0
  1156. package/src/services/remoteManagedSettings/securityCheck.js +60 -0
  1157. package/src/services/remoteManagedSettings/syncCache.js +90 -0
  1158. package/src/services/remoteManagedSettings/syncCacheState.js +89 -0
  1159. package/src/services/remoteManagedSettings/types.js +12 -0
  1160. package/src/services/sessionTranscript/sessionTranscript.js +5 -0
  1161. package/src/services/settingsSync/index.js +478 -0
  1162. package/src/services/settingsSync/types.js +35 -0
  1163. package/src/services/skillSearch/featureCheck.js +8 -0
  1164. package/src/services/skillSearch/localSearch.js +5 -0
  1165. package/src/services/skillSearch/prefetch.js +8 -0
  1166. package/src/services/skillSearch/remoteSkillLoader.js +8 -0
  1167. package/src/services/skillSearch/remoteSkillState.js +11 -0
  1168. package/src/services/skillSearch/signals.js +3 -0
  1169. package/src/services/skillSearch/telemetry.js +8 -0
  1170. package/src/services/teamMemorySync/index.js +976 -0
  1171. package/src/services/teamMemorySync/secretScanner.js +275 -0
  1172. package/src/services/teamMemorySync/teamMemSecretGuard.js +33 -0
  1173. package/src/services/teamMemorySync/types.js +47 -0
  1174. package/src/services/teamMemorySync/watcher.js +326 -0
  1175. package/src/services/thaddeusAuth.js +485 -0
  1176. package/src/services/thaddeusAuthTypes.js +9 -0
  1177. package/src/services/thaddeusLoginFlow.js +236 -0
  1178. package/src/services/tips/tipHistory.js +17 -0
  1179. package/src/services/tips/tipRegistry.js +593 -0
  1180. package/src/services/tips/tipScheduler.js +40 -0
  1181. package/src/services/tokenEstimation.js +365 -0
  1182. package/src/services/toolUseSummary/toolUseSummaryGenerator.js +87 -0
  1183. package/src/services/tools/StreamingToolExecutor.js +413 -0
  1184. package/src/services/tools/toolExecution.js +1309 -0
  1185. package/src/services/tools/toolHooks.js +454 -0
  1186. package/src/services/tools/toolOrchestration.js +110 -0
  1187. package/src/services/vcr.js +291 -0
  1188. package/src/services/voice.js +392 -0
  1189. package/src/services/voiceKeyterms.js +94 -0
  1190. package/src/services/voiceStreamSTT.js +405 -0
  1191. package/src/setup.js +310 -0
  1192. package/src/skills/bundled/batch.js +114 -0
  1193. package/src/skills/bundled/claudeApi.js +145 -0
  1194. package/src/skills/bundled/claudeApiContent.js +71 -0
  1195. package/src/skills/bundled/claudeInChrome.js +27 -0
  1196. package/src/skills/bundled/debug.js +99 -0
  1197. package/src/skills/bundled/dream.js +49 -0
  1198. package/src/skills/bundled/emailSetup.js +196 -0
  1199. package/src/skills/bundled/hunter.js +28 -0
  1200. package/src/skills/bundled/index.js +80 -0
  1201. package/src/skills/bundled/keybindings.js +292 -0
  1202. package/src/skills/bundled/loop.js +81 -0
  1203. package/src/skills/bundled/loremIpsum.js +264 -0
  1204. package/src/skills/bundled/reactor.js +31 -0
  1205. package/src/skills/bundled/remember.js +73 -0
  1206. package/src/skills/bundled/runSkillGenerator.js +12 -0
  1207. package/src/skills/bundled/scheduleRemoteAgents.js +373 -0
  1208. package/src/skills/bundled/simplify.js +66 -0
  1209. package/src/skills/bundled/skillify.js +182 -0
  1210. package/src/skills/bundled/stuck.js +69 -0
  1211. package/src/skills/bundled/updateConfig.js +463 -0
  1212. package/src/skills/bundled/verify.js +23 -0
  1213. package/src/skills/bundled/verifyContent.js +10 -0
  1214. package/src/skills/bundledSkills.js +159 -0
  1215. package/src/skills/loadSkillsDir.js +736 -0
  1216. package/src/skills/mcpSkillBuilders.js +10 -0
  1217. package/src/skills/mcpSkills.js +5 -0
  1218. package/src/state/AppState.js +182 -0
  1219. package/src/state/AppStateStore.js +117 -0
  1220. package/src/state/onChangeAppState.js +132 -0
  1221. package/src/state/selectors.js +51 -0
  1222. package/src/state/store.js +21 -0
  1223. package/src/state/teammateViewHelpers.js +124 -0
  1224. package/src/stubs/ant-chrome-mcp/index.js +4 -0
  1225. package/src/stubs/ant-computer-use-input/index.js +2 -0
  1226. package/src/stubs/ant-computer-use-mcp/index.js +7 -0
  1227. package/src/stubs/ant-computer-use-mcp/sentinelApps.js +2 -0
  1228. package/src/stubs/ant-computer-use-mcp/types.js +3 -0
  1229. package/src/stubs/ant-computer-use-swift/index.js +1 -0
  1230. package/src/stubs/anthropic-sandbox/index.js +34 -0
  1231. package/src/tasks/DreamTask/DreamTask.js +99 -0
  1232. package/src/tasks/InProcessTeammateTask/InProcessTeammateTask.js +116 -0
  1233. package/src/tasks/InProcessTeammateTask/types.js +35 -0
  1234. package/src/tasks/LocalAgentTask/LocalAgentTask.js +507 -0
  1235. package/src/tasks/LocalMainSessionTask.js +338 -0
  1236. package/src/tasks/LocalShellTask/LocalShellTask.js +475 -0
  1237. package/src/tasks/LocalShellTask/guards.js +9 -0
  1238. package/src/tasks/LocalShellTask/killShellTasks.js +59 -0
  1239. package/src/tasks/LocalWorkflowTask/LocalWorkflowTask.js +7 -0
  1240. package/src/tasks/MonitorMcpTask/MonitorMcpTask.js +20 -0
  1241. package/src/tasks/RemoteAgentTask/RemoteAgentTask.js +742 -0
  1242. package/src/tasks/pillLabel.js +69 -0
  1243. package/src/tasks/stopTask.js +67 -0
  1244. package/src/tasks/types.js +18 -0
  1245. package/src/tasks.js +37 -0
  1246. package/src/tools/AIEmployeesTool/AIEmployeesTool.js +674 -0
  1247. package/src/tools/AIEmployeesTool/constants.js +1 -0
  1248. package/src/tools/AIEmployeesTool/prompt.js +56 -0
  1249. package/src/tools/AgentTool/AgentTool.js +1221 -0
  1250. package/src/tools/AgentTool/UI.js +593 -0
  1251. package/src/tools/AgentTool/agentColorManager.js +43 -0
  1252. package/src/tools/AgentTool/agentDisplay.js +72 -0
  1253. package/src/tools/AgentTool/agentMemory.js +125 -0
  1254. package/src/tools/AgentTool/agentMemorySnapshot.js +136 -0
  1255. package/src/tools/AgentTool/agentToolUtils.js +456 -0
  1256. package/src/tools/AgentTool/built-in/exploreAgent.js +76 -0
  1257. package/src/tools/AgentTool/built-in/generalPurposeAgent.js +28 -0
  1258. package/src/tools/AgentTool/built-in/planAgent.js +87 -0
  1259. package/src/tools/AgentTool/built-in/statuslineSetup.js +140 -0
  1260. package/src/tools/AgentTool/built-in/thaddeusGuideAgent.js +174 -0
  1261. package/src/tools/AgentTool/built-in/verificationAgent.js +146 -0
  1262. package/src/tools/AgentTool/builtInAgents.js +56 -0
  1263. package/src/tools/AgentTool/constants.js +11 -0
  1264. package/src/tools/AgentTool/forkSubagent.js +177 -0
  1265. package/src/tools/AgentTool/loadAgentsDir.js +497 -0
  1266. package/src/tools/AgentTool/prompt.js +260 -0
  1267. package/src/tools/AgentTool/resumeAgent.js +182 -0
  1268. package/src/tools/AgentTool/runAgent.js +627 -0
  1269. package/src/tools/AppointmentsTool/AppointmentsTool.js +628 -0
  1270. package/src/tools/AppointmentsTool/constants.js +1 -0
  1271. package/src/tools/AppointmentsTool/prompt.js +15 -0
  1272. package/src/tools/AskUserQuestionTool/AskUserQuestionTool.js +238 -0
  1273. package/src/tools/AskUserQuestionTool/prompt.js +38 -0
  1274. package/src/tools/BashTool/BashTool.js +1009 -0
  1275. package/src/tools/BashTool/BashToolResultMessage.js +169 -0
  1276. package/src/tools/BashTool/UI.js +134 -0
  1277. package/src/tools/BashTool/bashCommandHelpers.js +184 -0
  1278. package/src/tools/BashTool/bashPermissions.js +2023 -0
  1279. package/src/tools/BashTool/bashSecurity.js +2267 -0
  1280. package/src/tools/BashTool/commandSemantics.js +105 -0
  1281. package/src/tools/BashTool/commentLabel.js +14 -0
  1282. package/src/tools/BashTool/destructiveCommandWarning.js +88 -0
  1283. package/src/tools/BashTool/modeValidation.js +86 -0
  1284. package/src/tools/BashTool/pathValidation.js +1079 -0
  1285. package/src/tools/BashTool/prompt.js +333 -0
  1286. package/src/tools/BashTool/readOnlyValidation.js +1794 -0
  1287. package/src/tools/BashTool/sedEditParser.js +282 -0
  1288. package/src/tools/BashTool/sedValidation.js +580 -0
  1289. package/src/tools/BashTool/shouldUseSandbox.js +125 -0
  1290. package/src/tools/BashTool/toolName.js +2 -0
  1291. package/src/tools/BashTool/utils.js +180 -0
  1292. package/src/tools/BriefTool/BriefTool.js +173 -0
  1293. package/src/tools/BriefTool/UI.js +67 -0
  1294. package/src/tools/BriefTool/attachments.js +86 -0
  1295. package/src/tools/BriefTool/prompt.js +19 -0
  1296. package/src/tools/BriefTool/upload.js +136 -0
  1297. package/src/tools/CalendarTool/CalendarTool.js +498 -0
  1298. package/src/tools/CalendarTool/constants.js +1 -0
  1299. package/src/tools/CalendarTool/prompt.js +11 -0
  1300. package/src/tools/ConfigTool/ConfigTool.js +398 -0
  1301. package/src/tools/ConfigTool/UI.js +25 -0
  1302. package/src/tools/ConfigTool/constants.js +1 -0
  1303. package/src/tools/ConfigTool/prompt.js +82 -0
  1304. package/src/tools/ConfigTool/supportedSettings.js +180 -0
  1305. package/src/tools/ContactsTool/ContactsTool.js +648 -0
  1306. package/src/tools/ContactsTool/constants.js +1 -0
  1307. package/src/tools/ContactsTool/prompt.js +15 -0
  1308. package/src/tools/CtxInspectTool/CtxInspectTool.js +44 -0
  1309. package/src/tools/DiscoverSkillsTool/prompt.js +4 -0
  1310. package/src/tools/EmailReadTool/index.js +410 -0
  1311. package/src/tools/EmailSendTool/index.js +178 -0
  1312. package/src/tools/EnterPlanModeTool/EnterPlanModeTool.js +98 -0
  1313. package/src/tools/EnterPlanModeTool/UI.js +14 -0
  1314. package/src/tools/EnterPlanModeTool/constants.js +1 -0
  1315. package/src/tools/EnterPlanModeTool/prompt.js +164 -0
  1316. package/src/tools/EnterWorktreeTool/EnterWorktreeTool.js +104 -0
  1317. package/src/tools/EnterWorktreeTool/UI.js +9 -0
  1318. package/src/tools/EnterWorktreeTool/constants.js +1 -0
  1319. package/src/tools/EnterWorktreeTool/prompt.js +30 -0
  1320. package/src/tools/ExitPlanModeTool/ExitPlanModeV2Tool.js +383 -0
  1321. package/src/tools/ExitPlanModeTool/UI.js +32 -0
  1322. package/src/tools/ExitPlanModeTool/constants.js +2 -0
  1323. package/src/tools/ExitPlanModeTool/prompt.js +27 -0
  1324. package/src/tools/ExitWorktreeTool/ExitWorktreeTool.js +257 -0
  1325. package/src/tools/ExitWorktreeTool/UI.js +10 -0
  1326. package/src/tools/ExitWorktreeTool/constants.js +1 -0
  1327. package/src/tools/ExitWorktreeTool/prompt.js +32 -0
  1328. package/src/tools/FileEditTool/FileEditTool.js +480 -0
  1329. package/src/tools/FileEditTool/UI.js +202 -0
  1330. package/src/tools/FileEditTool/constants.js +7 -0
  1331. package/src/tools/FileEditTool/prompt.js +24 -0
  1332. package/src/tools/FileEditTool/types.js +50 -0
  1333. package/src/tools/FileEditTool/utils.js +579 -0
  1334. package/src/tools/FileReadTool/FileReadTool.js +889 -0
  1335. package/src/tools/FileReadTool/UI.js +126 -0
  1336. package/src/tools/FileReadTool/imageProcessor.js +46 -0
  1337. package/src/tools/FileReadTool/limits.js +70 -0
  1338. package/src/tools/FileReadTool/prompt.js +31 -0
  1339. package/src/tools/FileWriteTool/FileWriteTool.js +341 -0
  1340. package/src/tools/FileWriteTool/UI.js +339 -0
  1341. package/src/tools/FileWriteTool/prompt.js +15 -0
  1342. package/src/tools/GlobTool/GlobTool.js +161 -0
  1343. package/src/tools/GlobTool/UI.js +40 -0
  1344. package/src/tools/GlobTool/prompt.js +6 -0
  1345. package/src/tools/GrepTool/GrepTool.js +439 -0
  1346. package/src/tools/GrepTool/UI.js +155 -0
  1347. package/src/tools/GrepTool/prompt.js +16 -0
  1348. package/src/tools/IntegrationsTool/IntegrationsTool.js +217 -0
  1349. package/src/tools/IntegrationsTool/constants.js +1 -0
  1350. package/src/tools/IntegrationsTool/prompt.js +41 -0
  1351. package/src/tools/InteractionsTool/InteractionsTool.js +525 -0
  1352. package/src/tools/InteractionsTool/constants.js +1 -0
  1353. package/src/tools/InteractionsTool/prompt.js +14 -0
  1354. package/src/tools/InvoicesTool/InvoicesTool.js +581 -0
  1355. package/src/tools/InvoicesTool/constants.js +1 -0
  1356. package/src/tools/InvoicesTool/prompt.js +15 -0
  1357. package/src/tools/LSPTool/LSPTool.js +660 -0
  1358. package/src/tools/LSPTool/UI.js +205 -0
  1359. package/src/tools/LSPTool/formatters.js +445 -0
  1360. package/src/tools/LSPTool/prompt.js +20 -0
  1361. package/src/tools/LSPTool/schemas.js +197 -0
  1362. package/src/tools/LSPTool/symbolContext.js +75 -0
  1363. package/src/tools/LeadScorerTool/LeadScorerTool.js +509 -0
  1364. package/src/tools/LeadScorerTool/constants.js +1 -0
  1365. package/src/tools/LeadScorerTool/prompt.js +11 -0
  1366. package/src/tools/ListMcpResourcesTool/ListMcpResourcesTool.js +100 -0
  1367. package/src/tools/ListMcpResourcesTool/UI.js +17 -0
  1368. package/src/tools/ListMcpResourcesTool/prompt.js +18 -0
  1369. package/src/tools/ListPeersTool/ListPeersTool.js +45 -0
  1370. package/src/tools/MCPTool/MCPTool.js +60 -0
  1371. package/src/tools/MCPTool/UI.js +343 -0
  1372. package/src/tools/MCPTool/classifyForCollapse.js +597 -0
  1373. package/src/tools/MCPTool/prompt.js +3 -0
  1374. package/src/tools/McpAuthTool/McpAuthTool.js +162 -0
  1375. package/src/tools/MonitorTool/MonitorTool.js +55 -0
  1376. package/src/tools/NotebookEditTool/NotebookEditTool.js +421 -0
  1377. package/src/tools/NotebookEditTool/UI.js +41 -0
  1378. package/src/tools/NotebookEditTool/constants.js +2 -0
  1379. package/src/tools/NotebookEditTool/prompt.js +2 -0
  1380. package/src/tools/OverflowTestTool/OverflowTestTool.js +51 -0
  1381. package/src/tools/PhoneBridgeTool/PhoneBridgeTool.js +301 -0
  1382. package/src/tools/PhoneBridgeTool/constants.js +1 -0
  1383. package/src/tools/PhoneBridgeTool/prompt.js +26 -0
  1384. package/src/tools/PowerShellTool/PowerShellTool.js +900 -0
  1385. package/src/tools/PowerShellTool/UI.js +58 -0
  1386. package/src/tools/PowerShellTool/clmTypes.js +207 -0
  1387. package/src/tools/PowerShellTool/commandSemantics.js +115 -0
  1388. package/src/tools/PowerShellTool/commonParameters.js +27 -0
  1389. package/src/tools/PowerShellTool/destructiveCommandWarning.js +92 -0
  1390. package/src/tools/PowerShellTool/gitSafety.js +185 -0
  1391. package/src/tools/PowerShellTool/modeValidation.js +357 -0
  1392. package/src/tools/PowerShellTool/pathValidation.js +1712 -0
  1393. package/src/tools/PowerShellTool/powershellPermissions.js +1351 -0
  1394. package/src/tools/PowerShellTool/powershellSecurity.js +942 -0
  1395. package/src/tools/PowerShellTool/prompt.js +132 -0
  1396. package/src/tools/PowerShellTool/readOnlyValidation.js +1633 -0
  1397. package/src/tools/PowerShellTool/toolName.js +2 -0
  1398. package/src/tools/PushNotificationTool/PushNotificationTool.js +35 -0
  1399. package/src/tools/REPLTool/REPLTool.js +44 -0
  1400. package/src/tools/REPLTool/constants.js +43 -0
  1401. package/src/tools/REPLTool/primitiveTools.js +36 -0
  1402. package/src/tools/ReadMcpResourceTool/ReadMcpResourceTool.js +112 -0
  1403. package/src/tools/ReadMcpResourceTool/UI.js +24 -0
  1404. package/src/tools/ReadMcpResourceTool/prompt.js +15 -0
  1405. package/src/tools/RemoteTriggerTool/RemoteTriggerTool.js +142 -0
  1406. package/src/tools/RemoteTriggerTool/UI.js +12 -0
  1407. package/src/tools/RemoteTriggerTool/prompt.js +12 -0
  1408. package/src/tools/ReviewArtifactTool/ReviewArtifactTool.js +51 -0
  1409. package/src/tools/ScheduleCronTool/CronCreateTool.js +120 -0
  1410. package/src/tools/ScheduleCronTool/CronDeleteTool.js +74 -0
  1411. package/src/tools/ScheduleCronTool/CronListTool.js +77 -0
  1412. package/src/tools/ScheduleCronTool/UI.js +29 -0
  1413. package/src/tools/ScheduleCronTool/prompt.js +115 -0
  1414. package/src/tools/SendMessageTool/SendMessageTool.js +673 -0
  1415. package/src/tools/SendMessageTool/UI.js +24 -0
  1416. package/src/tools/SendMessageTool/constants.js +1 -0
  1417. package/src/tools/SendMessageTool/prompt.js +47 -0
  1418. package/src/tools/SendUserFileTool/SendUserFileTool.js +35 -0
  1419. package/src/tools/SendUserFileTool/prompt.js +5 -0
  1420. package/src/tools/SkillTool/SkillTool.js +825 -0
  1421. package/src/tools/SkillTool/UI.js +61 -0
  1422. package/src/tools/SkillTool/constants.js +1 -0
  1423. package/src/tools/SkillTool/prompt.js +184 -0
  1424. package/src/tools/SleepTool/SleepTool.js +42 -0
  1425. package/src/tools/SleepTool/prompt.js +14 -0
  1426. package/src/tools/SnipTool/SnipTool.js +47 -0
  1427. package/src/tools/SnipTool/prompt.js +5 -0
  1428. package/src/tools/SubscribePRTool/SubscribePRTool.js +49 -0
  1429. package/src/tools/SuggestBackgroundPRTool/SuggestBackgroundPRTool.js +44 -0
  1430. package/src/tools/SyntheticOutputTool/SyntheticOutputTool.js +138 -0
  1431. package/src/tools/SyntheticOutputTool/SyntheticOutputTool.ts +1 -1
  1432. package/src/tools/TaskCreateTool/TaskCreateTool.js +104 -0
  1433. package/src/tools/TaskCreateTool/constants.js +1 -0
  1434. package/src/tools/TaskCreateTool/prompt.js +52 -0
  1435. package/src/tools/TaskGetTool/TaskGetTool.js +106 -0
  1436. package/src/tools/TaskGetTool/constants.js +1 -0
  1437. package/src/tools/TaskGetTool/prompt.js +23 -0
  1438. package/src/tools/TaskListTool/TaskListTool.js +89 -0
  1439. package/src/tools/TaskListTool/constants.js +1 -0
  1440. package/src/tools/TaskListTool/prompt.js +44 -0
  1441. package/src/tools/TaskOutputTool/TaskOutputTool.js +536 -0
  1442. package/src/tools/TaskOutputTool/constants.js +1 -0
  1443. package/src/tools/TaskStopTool/TaskStopTool.js +110 -0
  1444. package/src/tools/TaskStopTool/UI.js +31 -0
  1445. package/src/tools/TaskStopTool/prompt.js +7 -0
  1446. package/src/tools/TaskUpdateTool/TaskUpdateTool.js +301 -0
  1447. package/src/tools/TaskUpdateTool/constants.js +1 -0
  1448. package/src/tools/TaskUpdateTool/prompt.js +76 -0
  1449. package/src/tools/TeamCreateTool/TeamCreateTool.js +177 -0
  1450. package/src/tools/TeamCreateTool/UI.js +4 -0
  1451. package/src/tools/TeamCreateTool/constants.js +1 -0
  1452. package/src/tools/TeamCreateTool/prompt.js +113 -0
  1453. package/src/tools/TeamDeleteTool/TeamDeleteTool.js +102 -0
  1454. package/src/tools/TeamDeleteTool/UI.js +13 -0
  1455. package/src/tools/TeamDeleteTool/constants.js +1 -0
  1456. package/src/tools/TeamDeleteTool/prompt.js +16 -0
  1457. package/src/tools/TerminalCaptureTool/TerminalCaptureTool.js +47 -0
  1458. package/src/tools/TerminalCaptureTool/prompt.js +11 -0
  1459. package/src/tools/TodoWriteTool/TodoWriteTool.js +99 -0
  1460. package/src/tools/TodoWriteTool/constants.js +1 -0
  1461. package/src/tools/TodoWriteTool/prompt.js +181 -0
  1462. package/src/tools/ToolSearchTool/ToolSearchTool.js +357 -0
  1463. package/src/tools/ToolSearchTool/constants.js +1 -0
  1464. package/src/tools/ToolSearchTool/prompt.js +97 -0
  1465. package/src/tools/TungstenTool/TungstenLiveMonitor.js +7 -0
  1466. package/src/tools/TungstenTool/TungstenTool.js +3 -0
  1467. package/src/tools/VerifyPlanExecutionTool/VerifyPlanExecutionTool.js +45 -0
  1468. package/src/tools/VerifyPlanExecutionTool/constants.js +2 -0
  1469. package/src/tools/WebBrowserTool/WebBrowserPanel.js +5 -0
  1470. package/src/tools/WebBrowserTool/WebBrowserTool.js +58 -0
  1471. package/src/tools/WebFetchTool/UI.js +31 -0
  1472. package/src/tools/WebFetchTool/WebFetchTool.js +246 -0
  1473. package/src/tools/WebFetchTool/preapproved.js +154 -0
  1474. package/src/tools/WebFetchTool/prompt.js +39 -0
  1475. package/src/tools/WebFetchTool/utils.js +368 -0
  1476. package/src/tools/WebSearchTool/UI.js +67 -0
  1477. package/src/tools/WebSearchTool/WebSearchTool.js +396 -0
  1478. package/src/tools/WebSearchTool/prompt.js +32 -0
  1479. package/src/tools/WorkflowTool/WorkflowPermissionRequest.js +7 -0
  1480. package/src/tools/WorkflowTool/WorkflowTool.js +51 -0
  1481. package/src/tools/WorkflowTool/bundled/index.js +5 -0
  1482. package/src/tools/WorkflowTool/constants.js +1 -0
  1483. package/src/tools/WorkflowTool/createWorkflowCommand.js +5 -0
  1484. package/src/tools/shared/gitOperationTracking.js +220 -0
  1485. package/src/tools/shared/spawnMultiAgent.js +805 -0
  1486. package/src/tools/testing/TestingPermissionTool.js +72 -0
  1487. package/src/tools/utils.js +24 -0
  1488. package/src/tools.js +365 -0
  1489. package/src/types/command.js +8 -0
  1490. package/src/types/connectorText.js +3 -0
  1491. package/src/types/generated/events_mono/claude_code/v1/claude_code_internal_event.js +673 -0
  1492. package/src/types/generated/events_mono/common/v1/auth.js +49 -0
  1493. package/src/types/generated/events_mono/growthbook/v1/growthbook_experiment_event.js +147 -0
  1494. package/src/types/generated/google/protobuf/timestamp.js +38 -0
  1495. package/src/types/hooks.js +153 -0
  1496. package/src/types/ids.js +27 -0
  1497. package/src/types/logs.js +11 -0
  1498. package/src/types/permissions.js +25 -0
  1499. package/src/types/plugin.js +72 -0
  1500. package/src/types/textInputTypes.js +20 -0
  1501. package/src/upstreamproxy/relay.js +346 -0
  1502. package/src/upstreamproxy/upstreamproxy.js +234 -0
  1503. package/src/utils/CircularBuffer.js +75 -0
  1504. package/src/utils/Cursor.js +1229 -0
  1505. package/src/utils/QueryGuard.js +115 -0
  1506. package/src/utils/Shell.js +374 -0
  1507. package/src/utils/ShellCommand.js +336 -0
  1508. package/src/utils/abortController.js +74 -0
  1509. package/src/utils/activityManager.js +127 -0
  1510. package/src/utils/advisor.js +77 -0
  1511. package/src/utils/agentContext.js +91 -0
  1512. package/src/utils/agentId.js +83 -0
  1513. package/src/utils/agentSwarmsEnabled.js +37 -0
  1514. package/src/utils/agenticSessionSearch.js +255 -0
  1515. package/src/utils/analyzeContext.js +846 -0
  1516. package/src/utils/ansiToPng.js +259 -0
  1517. package/src/utils/ansiToSvg.js +207 -0
  1518. package/src/utils/api.js +555 -0
  1519. package/src/utils/apiPreconnect.js +62 -0
  1520. package/src/utils/appleTerminalBackup.js +95 -0
  1521. package/src/utils/argumentSubstitution.js +114 -0
  1522. package/src/utils/array.js +12 -0
  1523. package/src/utils/asciicast.js +200 -0
  1524. package/src/utils/attachments.js +2518 -0
  1525. package/src/utils/attribution.js +308 -0
  1526. package/src/utils/auth.js +1598 -0
  1527. package/src/utils/authFileDescriptor.js +152 -0
  1528. package/src/utils/authPortable.js +14 -0
  1529. package/src/utils/autoModeDenials.js +15 -0
  1530. package/src/utils/autoRunIssue.js +113 -0
  1531. package/src/utils/autoUpdater.js +457 -0
  1532. package/src/utils/aws.js +44 -0
  1533. package/src/utils/awsAuthStatusManager.js +66 -0
  1534. package/src/utils/background/remote/preconditions.js +175 -0
  1535. package/src/utils/background/remote/remoteSession.js +53 -0
  1536. package/src/utils/backgroundHousekeeping.js +64 -0
  1537. package/src/utils/bash/ParsedCommand.js +241 -0
  1538. package/src/utils/bash/ShellSnapshot.js +489 -0
  1539. package/src/utils/bash/ast.js +2590 -0
  1540. package/src/utils/bash/bashParser.js +4355 -0
  1541. package/src/utils/bash/bashPipeCommand.js +249 -0
  1542. package/src/utils/bash/commands.js +1131 -0
  1543. package/src/utils/bash/heredoc.js +647 -0
  1544. package/src/utils/bash/parser.js +195 -0
  1545. package/src/utils/bash/prefix.js +154 -0
  1546. package/src/utils/bash/registry.js +23 -0
  1547. package/src/utils/bash/shellCompletion.js +196 -0
  1548. package/src/utils/bash/shellPrefix.js +25 -0
  1549. package/src/utils/bash/shellQuote.js +253 -0
  1550. package/src/utils/bash/shellQuoting.js +106 -0
  1551. package/src/utils/bash/specs/alias.js +11 -0
  1552. package/src/utils/bash/specs/index.js +16 -0
  1553. package/src/utils/bash/specs/nohup.js +10 -0
  1554. package/src/utils/bash/specs/pyright.js +88 -0
  1555. package/src/utils/bash/specs/sleep.js +10 -0
  1556. package/src/utils/bash/specs/srun.js +28 -0
  1557. package/src/utils/bash/specs/time.js +10 -0
  1558. package/src/utils/bash/specs/timeout.js +17 -0
  1559. package/src/utils/bash/treeSitterAnalysis.js +407 -0
  1560. package/src/utils/betas.js +331 -0
  1561. package/src/utils/billing.js +54 -0
  1562. package/src/utils/binaryCheck.js +40 -0
  1563. package/src/utils/browser.js +58 -0
  1564. package/src/utils/bufferedWriter.js +77 -0
  1565. package/src/utils/bundledMode.js +19 -0
  1566. package/src/utils/businessDb.js +390 -0
  1567. package/src/utils/caCerts.js +91 -0
  1568. package/src/utils/caCertsConfig.js +77 -0
  1569. package/src/utils/cachePaths.js +28 -0
  1570. package/src/utils/classifierApprovals.js +66 -0
  1571. package/src/utils/classifierApprovalsHook.js +10 -0
  1572. package/src/utils/claudeDesktop.js +108 -0
  1573. package/src/utils/claudeInChrome/chromeNativeHost.js +416 -0
  1574. package/src/utils/claudeInChrome/common.js +466 -0
  1575. package/src/utils/claudeInChrome/mcpServer.js +237 -0
  1576. package/src/utils/claudeInChrome/prompt.js +79 -0
  1577. package/src/utils/claudeInChrome/setup.js +304 -0
  1578. package/src/utils/claudeInChrome/setupPortable.js +172 -0
  1579. package/src/utils/claudeInChrome/toolRendering.js +235 -0
  1580. package/src/utils/claudemd.js +1052 -0
  1581. package/src/utils/cleanup.js +514 -0
  1582. package/src/utils/cleanupRegistry.js +22 -0
  1583. package/src/utils/cliArgs.js +53 -0
  1584. package/src/utils/cliHighlight.js +45 -0
  1585. package/src/utils/codeIndexing.js +149 -0
  1586. package/src/utils/collapseBackgroundBashNotifications.js +70 -0
  1587. package/src/utils/collapseHookSummaries.js +48 -0
  1588. package/src/utils/collapseReadSearch.js +869 -0
  1589. package/src/utils/collapseTeammateShutdowns.js +44 -0
  1590. package/src/utils/combinedAbortSignal.js +40 -0
  1591. package/src/utils/commandLifecycle.js +7 -0
  1592. package/src/utils/commitAttribution.js +718 -0
  1593. package/src/utils/completionCache.js +138 -0
  1594. package/src/utils/computerUse/appNames.js +170 -0
  1595. package/src/utils/computerUse/cleanup.js +65 -0
  1596. package/src/utils/computerUse/common.js +56 -0
  1597. package/src/utils/computerUse/computerUseLock.js +183 -0
  1598. package/src/utils/computerUse/drainRunLoop.js +71 -0
  1599. package/src/utils/computerUse/escHotkey.js +53 -0
  1600. package/src/utils/computerUse/executor.js +480 -0
  1601. package/src/utils/computerUse/gates.js +55 -0
  1602. package/src/utils/computerUse/hostAdapter.js +62 -0
  1603. package/src/utils/computerUse/inputLoader.js +25 -0
  1604. package/src/utils/computerUse/mcpServer.js +84 -0
  1605. package/src/utils/computerUse/setup.js +42 -0
  1606. package/src/utils/computerUse/swiftLoader.js +18 -0
  1607. package/src/utils/computerUse/toolRendering.js +101 -0
  1608. package/src/utils/computerUse/wrapper.js +317 -0
  1609. package/src/utils/concurrentSessions.js +179 -0
  1610. package/src/utils/config.js +1078 -0
  1611. package/src/utils/configConstants.js +18 -0
  1612. package/src/utils/contentArray.js +45 -0
  1613. package/src/utils/context.js +185 -0
  1614. package/src/utils/contextAnalysis.js +171 -0
  1615. package/src/utils/contextSuggestions.js +158 -0
  1616. package/src/utils/controlMessageCompat.js +31 -0
  1617. package/src/utils/conversationRecovery.js +434 -0
  1618. package/src/utils/cron.js +260 -0
  1619. package/src/utils/cronJitterConfig.js +62 -0
  1620. package/src/utils/cronScheduler.js +388 -0
  1621. package/src/utils/cronTasks.js +328 -0
  1622. package/src/utils/cronTasksLock.js +159 -0
  1623. package/src/utils/crossProjectResume.js +46 -0
  1624. package/src/utils/crypto.js +13 -0
  1625. package/src/utils/cwd.js +29 -0
  1626. package/src/utils/debug.js +220 -0
  1627. package/src/utils/debugFilter.js +125 -0
  1628. package/src/utils/deepLink/banner.js +103 -0
  1629. package/src/utils/deepLink/parseDeepLink.js +138 -0
  1630. package/src/utils/deepLink/protocolHandler.js +119 -0
  1631. package/src/utils/deepLink/registerProtocol.js +291 -0
  1632. package/src/utils/deepLink/terminalLauncher.js +455 -0
  1633. package/src/utils/deepLink/terminalPreference.js +51 -0
  1634. package/src/utils/desktopDeepLink.js +208 -0
  1635. package/src/utils/detectRepository.js +157 -0
  1636. package/src/utils/diagLogs.js +74 -0
  1637. package/src/utils/diff.js +108 -0
  1638. package/src/utils/directMemberMessage.js +34 -0
  1639. package/src/utils/displayTags.js +46 -0
  1640. package/src/utils/doctorContextWarnings.js +179 -0
  1641. package/src/utils/doctorDiagnostic.js +494 -0
  1642. package/src/utils/dxt/helpers.js +64 -0
  1643. package/src/utils/dxt/zip.js +167 -0
  1644. package/src/utils/earlyInput.js +166 -0
  1645. package/src/utils/editor.js +163 -0
  1646. package/src/utils/effort.js +271 -0
  1647. package/src/utils/embeddedTools.js +26 -0
  1648. package/src/utils/employeeChat.js +271 -0
  1649. package/src/utils/employeeDb.js +326 -0
  1650. package/src/utils/env.js +358 -0
  1651. package/src/utils/envDynamic.js +130 -0
  1652. package/src/utils/envUtils.js +161 -0
  1653. package/src/utils/envValidation.js +26 -0
  1654. package/src/utils/errorLogSink.js +196 -0
  1655. package/src/utils/errors.js +207 -0
  1656. package/src/utils/exampleCommands.js +165 -0
  1657. package/src/utils/execFileNoThrow.js +93 -0
  1658. package/src/utils/execFileNoThrowPortable.js +49 -0
  1659. package/src/utils/execSyncWrapper.js +6 -0
  1660. package/src/utils/exportRenderer.js +71 -0
  1661. package/src/utils/extraUsage.js +19 -0
  1662. package/src/utils/fastMode.js +393 -0
  1663. package/src/utils/file.js +467 -0
  1664. package/src/utils/fileHistory.js +851 -0
  1665. package/src/utils/fileOperationAnalytics.js +45 -0
  1666. package/src/utils/filePersistence/filePersistence.js +212 -0
  1667. package/src/utils/filePersistence/outputsScanner.js +104 -0
  1668. package/src/utils/filePersistence/types.js +4 -0
  1669. package/src/utils/fileRead.js +81 -0
  1670. package/src/utils/fileReadCache.js +78 -0
  1671. package/src/utils/fileStateCache.js +99 -0
  1672. package/src/utils/findExecutable.js +13 -0
  1673. package/src/utils/fingerprint.js +58 -0
  1674. package/src/utils/forkedAgent.js +410 -0
  1675. package/src/utils/format.js +238 -0
  1676. package/src/utils/formatBriefTimestamp.js +72 -0
  1677. package/src/utils/fpsTracker.js +34 -0
  1678. package/src/utils/frontmatterParser.js +260 -0
  1679. package/src/utils/fsOperations.js +555 -0
  1680. package/src/utils/fullscreen.js +194 -0
  1681. package/src/utils/generatedFiles.js +122 -0
  1682. package/src/utils/generators.js +67 -0
  1683. package/src/utils/genericProcessUtils.js +155 -0
  1684. package/src/utils/getWorktreePaths.js +56 -0
  1685. package/src/utils/getWorktreePathsPortable.js +23 -0
  1686. package/src/utils/ghPrStatus.js +71 -0
  1687. package/src/utils/git/gitConfigParser.js +226 -0
  1688. package/src/utils/git/gitFilesystem.js +606 -0
  1689. package/src/utils/git/gitignore.js +84 -0
  1690. package/src/utils/git.js +725 -0
  1691. package/src/utils/gitDiff.js +395 -0
  1692. package/src/utils/gitSettings.js +18 -0
  1693. package/src/utils/github/ghAuthStatus.js +23 -0
  1694. package/src/utils/githubRepoPathMapping.js +135 -0
  1695. package/src/utils/glob.js +90 -0
  1696. package/src/utils/gracefulShutdown.js +447 -0
  1697. package/src/utils/groupToolUses.js +126 -0
  1698. package/src/utils/handlePromptSubmit.js +398 -0
  1699. package/src/utils/hash.js +44 -0
  1700. package/src/utils/headlessProfiler.js +147 -0
  1701. package/src/utils/heapDumpService.js +201 -0
  1702. package/src/utils/heatmap.js +151 -0
  1703. package/src/utils/highlightMatch.js +29 -0
  1704. package/src/utils/hooks/AsyncHookRegistry.js +187 -0
  1705. package/src/utils/hooks/apiQueryHookHelper.js +77 -0
  1706. package/src/utils/hooks/execAgentHook.js +257 -0
  1707. package/src/utils/hooks/execHttpHook.js +184 -0
  1708. package/src/utils/hooks/execPromptHook.js +171 -0
  1709. package/src/utils/hooks/fileChangedWatcher.js +161 -0
  1710. package/src/utils/hooks/hookEvents.js +111 -0
  1711. package/src/utils/hooks/hookHelpers.js +60 -0
  1712. package/src/utils/hooks/hooksConfigManager.js +323 -0
  1713. package/src/utils/hooks/hooksConfigSnapshot.js +114 -0
  1714. package/src/utils/hooks/hooksSettings.js +204 -0
  1715. package/src/utils/hooks/postSamplingHooks.js +39 -0
  1716. package/src/utils/hooks/registerFrontmatterHooks.js +47 -0
  1717. package/src/utils/hooks/registerSkillHooks.js +40 -0
  1718. package/src/utils/hooks/sessionHooks.js +252 -0
  1719. package/src/utils/hooks/skillImprovement.js +211 -0
  1720. package/src/utils/hooks/ssrfGuard.js +258 -0
  1721. package/src/utils/hooks.js +3668 -0
  1722. package/src/utils/horizontalScroll.js +108 -0
  1723. package/src/utils/http.js +120 -0
  1724. package/src/utils/hyperlink.js +28 -0
  1725. package/src/utils/iTermBackup.js +48 -0
  1726. package/src/utils/ide.js +1195 -0
  1727. package/src/utils/idePathConversion.js +66 -0
  1728. package/src/utils/idleTimeout.js +44 -0
  1729. package/src/utils/imagePaste.js +343 -0
  1730. package/src/utils/imageResizer.js +664 -0
  1731. package/src/utils/imageStore.js +150 -0
  1732. package/src/utils/imageValidation.js +92 -0
  1733. package/src/utils/immediateCommand.js +12 -0
  1734. package/src/utils/inProcessTeammateHelpers.js +71 -0
  1735. package/src/utils/ink.js +20 -0
  1736. package/src/utils/intl.js +83 -0
  1737. package/src/utils/jetbrains.js +152 -0
  1738. package/src/utils/json.js +231 -0
  1739. package/src/utils/jsonRead.js +14 -0
  1740. package/src/utils/keyboardShortcuts.js +11 -0
  1741. package/src/utils/lazySchema.js +8 -0
  1742. package/src/utils/listSessionsImpl.js +332 -0
  1743. package/src/utils/localInstaller.js +130 -0
  1744. package/src/utils/lockfile.js +30 -0
  1745. package/src/utils/log.js +280 -0
  1746. package/src/utils/logoV2Utils.js +256 -0
  1747. package/src/utils/mailbox.js +50 -0
  1748. package/src/utils/managedEnv.js +160 -0
  1749. package/src/utils/managedEnvConstants.js +185 -0
  1750. package/src/utils/markdown.js +315 -0
  1751. package/src/utils/markdownConfigLoader.js +480 -0
  1752. package/src/utils/mcp/dateTimeParser.js +102 -0
  1753. package/src/utils/mcp/elicitationValidation.js +259 -0
  1754. package/src/utils/mcpInstructionsDelta.js +97 -0
  1755. package/src/utils/mcpOutputStorage.js +159 -0
  1756. package/src/utils/mcpValidation.js +165 -0
  1757. package/src/utils/mcpWebSocketTransport.js +180 -0
  1758. package/src/utils/memoize.js +205 -0
  1759. package/src/utils/memory/types.js +9 -0
  1760. package/src/utils/memory/versions.js +7 -0
  1761. package/src/utils/memoryFileDetection.js +245 -0
  1762. package/src/utils/messagePredicates.js +6 -0
  1763. package/src/utils/messageQueueManager.js +430 -0
  1764. package/src/utils/messages/mappers.js +240 -0
  1765. package/src/utils/messages/systemInit.js +72 -0
  1766. package/src/utils/messages.js +4286 -0
  1767. package/src/utils/model/agent.js +128 -0
  1768. package/src/utils/model/aliases.js +21 -0
  1769. package/src/utils/model/antModels.js +25 -0
  1770. package/src/utils/model/bedrock.js +220 -0
  1771. package/src/utils/model/check1mAccess.js +64 -0
  1772. package/src/utils/model/configs.js +86 -0
  1773. package/src/utils/model/contextWindowUpgradeCheck.js +41 -0
  1774. package/src/utils/model/deprecation.js +72 -0
  1775. package/src/utils/model/model.js +533 -0
  1776. package/src/utils/model/modelAllowlist.js +148 -0
  1777. package/src/utils/model/modelCapabilities.js +105 -0
  1778. package/src/utils/model/modelOptions.js +450 -0
  1779. package/src/utils/model/modelStrings.js +144 -0
  1780. package/src/utils/model/modelSupportOverrides.js +40 -0
  1781. package/src/utils/model/providers.js +35 -0
  1782. package/src/utils/model/validateModel.js +131 -0
  1783. package/src/utils/modelCost.js +160 -0
  1784. package/src/utils/modifiers.js +39 -0
  1785. package/src/utils/mtls.js +132 -0
  1786. package/src/utils/nativeInstaller/download.js +370 -0
  1787. package/src/utils/nativeInstaller/index.js +8 -0
  1788. package/src/utils/nativeInstaller/installer.js +1395 -0
  1789. package/src/utils/nativeInstaller/packageManagers.js +258 -0
  1790. package/src/utils/nativeInstaller/pidLock.js +347 -0
  1791. package/src/utils/notebook.js +176 -0
  1792. package/src/utils/objectGroupBy.js +15 -0
  1793. package/src/utils/pasteStore.js +93 -0
  1794. package/src/utils/path.js +140 -0
  1795. package/src/utils/pdf.js +236 -0
  1796. package/src/utils/pdfUtils.js +61 -0
  1797. package/src/utils/peerAddress.js +20 -0
  1798. package/src/utils/permissions/PermissionMode.js +95 -0
  1799. package/src/utils/permissions/PermissionPromptToolResultSchema.js +85 -0
  1800. package/src/utils/permissions/PermissionResult.js +11 -0
  1801. package/src/utils/permissions/PermissionRule.js +19 -0
  1802. package/src/utils/permissions/PermissionUpdate.js +268 -0
  1803. package/src/utils/permissions/PermissionUpdateSchema.js +61 -0
  1804. package/src/utils/permissions/autoModeState.js +31 -0
  1805. package/src/utils/permissions/bashClassifier.js +30 -0
  1806. package/src/utils/permissions/bypassPermissionsKillswitch.js +115 -0
  1807. package/src/utils/permissions/classifierDecision.js +86 -0
  1808. package/src/utils/permissions/classifierShared.js +28 -0
  1809. package/src/utils/permissions/dangerousPatterns.js +78 -0
  1810. package/src/utils/permissions/denialTracking.js +34 -0
  1811. package/src/utils/permissions/filesystem.js +1411 -0
  1812. package/src/utils/permissions/getNextPermissionMode.js +74 -0
  1813. package/src/utils/permissions/pathValidation.js +351 -0
  1814. package/src/utils/permissions/permissionExplainer.js +188 -0
  1815. package/src/utils/permissions/permissionRuleParser.js +175 -0
  1816. package/src/utils/permissions/permissionSetup.js +1162 -0
  1817. package/src/utils/permissions/permissions.js +1063 -0
  1818. package/src/utils/permissions/permissionsLoader.js +217 -0
  1819. package/src/utils/permissions/shadowedRuleDetection.js +149 -0
  1820. package/src/utils/permissions/shellRuleMatching.js +174 -0
  1821. package/src/utils/permissions/yoloClassifier.js +1193 -0
  1822. package/src/utils/planModeV2.js +75 -0
  1823. package/src/utils/plans.js +334 -0
  1824. package/src/utils/platform.js +122 -0
  1825. package/src/utils/plugins/addDirPluginSettings.js +53 -0
  1826. package/src/utils/plugins/cacheUtils.js +174 -0
  1827. package/src/utils/plugins/dependencyResolver.js +244 -0
  1828. package/src/utils/plugins/fetchTelemetry.js +108 -0
  1829. package/src/utils/plugins/gitAvailability.js +65 -0
  1830. package/src/utils/plugins/headlessPluginInstall.js +136 -0
  1831. package/src/utils/plugins/hintRecommendation.js +136 -0
  1832. package/src/utils/plugins/installCounts.js +221 -0
  1833. package/src/utils/plugins/installedPluginsManager.js +1003 -0
  1834. package/src/utils/plugins/loadPluginAgents.js +219 -0
  1835. package/src/utils/plugins/loadPluginCommands.js +595 -0
  1836. package/src/utils/plugins/loadPluginHooks.js +239 -0
  1837. package/src/utils/plugins/loadPluginOutputStyles.js +112 -0
  1838. package/src/utils/plugins/lspPluginIntegration.js +293 -0
  1839. package/src/utils/plugins/lspRecommendation.js +278 -0
  1840. package/src/utils/plugins/managedPlugins.js +26 -0
  1841. package/src/utils/plugins/marketplaceHelpers.js +470 -0
  1842. package/src/utils/plugins/marketplaceManager.js +1939 -0
  1843. package/src/utils/plugins/mcpPluginIntegration.js +465 -0
  1844. package/src/utils/plugins/mcpbHandler.js +708 -0
  1845. package/src/utils/plugins/officialMarketplace.js +19 -0
  1846. package/src/utils/plugins/officialMarketplaceGcs.js +202 -0
  1847. package/src/utils/plugins/officialMarketplaceStartupCheck.js +344 -0
  1848. package/src/utils/plugins/orphanedPluginFilter.js +96 -0
  1849. package/src/utils/plugins/parseMarketplaceInput.js +143 -0
  1850. package/src/utils/plugins/performStartupChecks.js +66 -0
  1851. package/src/utils/plugins/pluginAutoupdate.js +210 -0
  1852. package/src/utils/plugins/pluginBlocklist.js +93 -0
  1853. package/src/utils/plugins/pluginDirectories.js +170 -0
  1854. package/src/utils/plugins/pluginFlagging.js +173 -0
  1855. package/src/utils/plugins/pluginIdentifier.js +78 -0
  1856. package/src/utils/plugins/pluginInstallationHelpers.js +400 -0
  1857. package/src/utils/plugins/pluginLoader.js +2426 -0
  1858. package/src/utils/plugins/pluginOptionsStorage.js +311 -0
  1859. package/src/utils/plugins/pluginPolicy.js +18 -0
  1860. package/src/utils/plugins/pluginStartupCheck.js +261 -0
  1861. package/src/utils/plugins/pluginVersioning.js +128 -0
  1862. package/src/utils/plugins/reconciler.js +181 -0
  1863. package/src/utils/plugins/refresh.js +162 -0
  1864. package/src/utils/plugins/schemas.js +1283 -0
  1865. package/src/utils/plugins/validatePlugin.js +765 -0
  1866. package/src/utils/plugins/walkPluginMarkdown.js +49 -0
  1867. package/src/utils/plugins/zipCache.js +346 -0
  1868. package/src/utils/plugins/zipCacheAdapters.js +133 -0
  1869. package/src/utils/powershell/dangerousCmdlets.js +174 -0
  1870. package/src/utils/powershell/parser.js +1357 -0
  1871. package/src/utils/powershell/staticPrefix.js +277 -0
  1872. package/src/utils/preflightChecks.js +147 -0
  1873. package/src/utils/privacyLevel.js +49 -0
  1874. package/src/utils/process.js +56 -0
  1875. package/src/utils/processUserInput/processBashCommand.js +118 -0
  1876. package/src/utils/processUserInput/processSlashCommand.js +845 -0
  1877. package/src/utils/processUserInput/processTextPrompt.js +68 -0
  1878. package/src/utils/processUserInput/processUserInput.js +344 -0
  1879. package/src/utils/profilerBase.js +32 -0
  1880. package/src/utils/promptCategory.js +39 -0
  1881. package/src/utils/promptEditor.js +151 -0
  1882. package/src/utils/promptShellExecution.js +117 -0
  1883. package/src/utils/protectedNamespace.js +4 -0
  1884. package/src/utils/proxy.js +345 -0
  1885. package/src/utils/queryContext.js +110 -0
  1886. package/src/utils/queryHelpers.js +436 -0
  1887. package/src/utils/queryProfiler.js +242 -0
  1888. package/src/utils/queueProcessor.js +70 -0
  1889. package/src/utils/readEditContext.js +176 -0
  1890. package/src/utils/readFileInRange.js +278 -0
  1891. package/src/utils/releaseNotes.js +307 -0
  1892. package/src/utils/renderOptions.js +67 -0
  1893. package/src/utils/ripgrep.js +521 -0
  1894. package/src/utils/sandbox/sandbox-adapter.js +750 -0
  1895. package/src/utils/sandbox/sandbox-ui-utils.js +11 -0
  1896. package/src/utils/sanitization.js +72 -0
  1897. package/src/utils/screenshotClipboard.js +89 -0
  1898. package/src/utils/sdkEventQueue.js +49 -0
  1899. package/src/utils/secureStorage/fallbackStorage.js +59 -0
  1900. package/src/utils/secureStorage/index.js +13 -0
  1901. package/src/utils/secureStorage/keychainPrefetch.js +91 -0
  1902. package/src/utils/secureStorage/macOsKeychainHelpers.js +91 -0
  1903. package/src/utils/secureStorage/macOsKeychainStorage.js +192 -0
  1904. package/src/utils/secureStorage/plainTextStorage.js +81 -0
  1905. package/src/utils/semanticBoolean.js +23 -0
  1906. package/src/utils/semanticNumber.js +34 -0
  1907. package/src/utils/semver.js +51 -0
  1908. package/src/utils/sequential.js +43 -0
  1909. package/src/utils/sessionActivity.js +120 -0
  1910. package/src/utils/sessionEnvVars.js +18 -0
  1911. package/src/utils/sessionEnvironment.js +131 -0
  1912. package/src/utils/sessionFileAccessHooks.js +205 -0
  1913. package/src/utils/sessionIngressAuth.js +113 -0
  1914. package/src/utils/sessionRestore.js +357 -0
  1915. package/src/utils/sessionStart.js +165 -0
  1916. package/src/utils/sessionState.js +76 -0
  1917. package/src/utils/sessionStorage.js +4162 -0
  1918. package/src/utils/sessionStoragePortable.js +665 -0
  1919. package/src/utils/sessionTitle.js +120 -0
  1920. package/src/utils/sessionUrl.js +50 -0
  1921. package/src/utils/set.js +50 -0
  1922. package/src/utils/settings/allErrors.js +29 -0
  1923. package/src/utils/settings/applySettingsChange.js +65 -0
  1924. package/src/utils/settings/changeDetector.js +409 -0
  1925. package/src/utils/settings/constants.js +166 -0
  1926. package/src/utils/settings/internalWrites.js +33 -0
  1927. package/src/utils/settings/managedPath.js +29 -0
  1928. package/src/utils/settings/mdm/constants.js +62 -0
  1929. package/src/utils/settings/mdm/rawRead.js +97 -0
  1930. package/src/utils/settings/mdm/settings.js +254 -0
  1931. package/src/utils/settings/permissionValidation.js +224 -0
  1932. package/src/utils/settings/pluginOnlyPolicy.js +53 -0
  1933. package/src/utils/settings/schemaOutput.js +7 -0
  1934. package/src/utils/settings/settings.js +791 -0
  1935. package/src/utils/settings/settingsCache.js +47 -0
  1936. package/src/utils/settings/toolValidationConfig.js +76 -0
  1937. package/src/utils/settings/types.js +846 -0
  1938. package/src/utils/settings/validateEditTool.js +34 -0
  1939. package/src/utils/settings/validation.js +192 -0
  1940. package/src/utils/settings/validationTips.js +111 -0
  1941. package/src/utils/shell/bashProvider.js +202 -0
  1942. package/src/utils/shell/outputLimits.js +7 -0
  1943. package/src/utils/shell/powershellDetection.js +96 -0
  1944. package/src/utils/shell/powershellProvider.js +104 -0
  1945. package/src/utils/shell/prefix.js +246 -0
  1946. package/src/utils/shell/readOnlyCommandValidation.js +1776 -0
  1947. package/src/utils/shell/resolveDefaultShell.js +13 -0
  1948. package/src/utils/shell/shellProvider.js +2 -0
  1949. package/src/utils/shell/shellToolUtils.js +21 -0
  1950. package/src/utils/shell/specPrefix.js +198 -0
  1951. package/src/utils/shellConfig.js +136 -0
  1952. package/src/utils/sideQuery.js +134 -0
  1953. package/src/utils/sideQuestion.js +121 -0
  1954. package/src/utils/signal.js +34 -0
  1955. package/src/utils/sinks.js +15 -0
  1956. package/src/utils/skills/skillChangeDetector.js +264 -0
  1957. package/src/utils/slashCommandParsing.js +46 -0
  1958. package/src/utils/sleep.js +72 -0
  1959. package/src/utils/sliceAnsi.js +74 -0
  1960. package/src/utils/slowOperations.js +216 -0
  1961. package/src/utils/standaloneAgent.js +20 -0
  1962. package/src/utils/startupProfiler.js +149 -0
  1963. package/src/utils/staticRender.js +104 -0
  1964. package/src/utils/stats.js +802 -0
  1965. package/src/utils/statsCache.js +330 -0
  1966. package/src/utils/status.js +359 -0
  1967. package/src/utils/statusNoticeDefinitions.js +123 -0
  1968. package/src/utils/statusNoticeHelpers.js +15 -0
  1969. package/src/utils/stream.js +73 -0
  1970. package/src/utils/streamJsonStdoutGuard.js +107 -0
  1971. package/src/utils/streamlinedTransform.js +162 -0
  1972. package/src/utils/stringUtils.js +202 -0
  1973. package/src/utils/subprocessEnv.js +87 -0
  1974. package/src/utils/suggestions/commandSuggestions.js +458 -0
  1975. package/src/utils/suggestions/directoryCompletion.js +191 -0
  1976. package/src/utils/suggestions/shellHistoryCompletion.js +95 -0
  1977. package/src/utils/suggestions/skillUsageTracking.js +50 -0
  1978. package/src/utils/suggestions/slackChannelSuggestions.js +169 -0
  1979. package/src/utils/swarm/It2SetupPrompt.js +386 -0
  1980. package/src/utils/swarm/backends/ITermBackend.js +276 -0
  1981. package/src/utils/swarm/backends/InProcessBackend.js +237 -0
  1982. package/src/utils/swarm/backends/PaneBackendExecutor.js +250 -0
  1983. package/src/utils/swarm/backends/TmuxBackend.js +574 -0
  1984. package/src/utils/swarm/backends/detection.js +112 -0
  1985. package/src/utils/swarm/backends/it2Setup.js +185 -0
  1986. package/src/utils/swarm/backends/registry.js +369 -0
  1987. package/src/utils/swarm/backends/teammateModeSnapshot.js +68 -0
  1988. package/src/utils/swarm/backends/types.js +9 -0
  1989. package/src/utils/swarm/constants.js +29 -0
  1990. package/src/utils/swarm/inProcessRunner.js +1021 -0
  1991. package/src/utils/swarm/leaderPermissionBridge.js +31 -0
  1992. package/src/utils/swarm/permissionSync.js +667 -0
  1993. package/src/utils/swarm/reconnection.js +82 -0
  1994. package/src/utils/swarm/spawnInProcess.js +218 -0
  1995. package/src/utils/swarm/spawnUtils.js +123 -0
  1996. package/src/utils/swarm/teamHelpers.js +484 -0
  1997. package/src/utils/swarm/teammateInit.js +87 -0
  1998. package/src/utils/swarm/teammateLayoutManager.js +82 -0
  1999. package/src/utils/swarm/teammateModel.js +9 -0
  2000. package/src/utils/swarm/teammatePromptAddendum.js +17 -0
  2001. package/src/utils/systemDirectories.js +51 -0
  2002. package/src/utils/systemPrompt.js +88 -0
  2003. package/src/utils/systemPromptType.js +9 -0
  2004. package/src/utils/systemTheme.js +108 -0
  2005. package/src/utils/taggedId.js +49 -0
  2006. package/src/utils/task/TaskOutput.js +320 -0
  2007. package/src/utils/task/diskOutput.js +387 -0
  2008. package/src/utils/task/framework.js +236 -0
  2009. package/src/utils/task/outputFormatting.js +24 -0
  2010. package/src/utils/task/sdkProgress.js +24 -0
  2011. package/src/utils/taskSummary.js +3 -0
  2012. package/src/utils/tasks.js +672 -0
  2013. package/src/utils/teamDiscovery.js +48 -0
  2014. package/src/utils/teamMemoryOps.js +67 -0
  2015. package/src/utils/teammate.js +237 -0
  2016. package/src/utils/teammateContext.js +56 -0
  2017. package/src/utils/teammateMailbox.js +793 -0
  2018. package/src/utils/telemetry/betaSessionTracing.js +25 -0
  2019. package/src/utils/telemetry/bigqueryExporter.js +17 -0
  2020. package/src/utils/telemetry/events.js +7 -0
  2021. package/src/utils/telemetry/instrumentation.js +16 -0
  2022. package/src/utils/telemetry/logger.js +25 -0
  2023. package/src/utils/telemetry/perfettoTracing.js +882 -0
  2024. package/src/utils/telemetry/pluginTelemetry.js +76 -0
  2025. package/src/utils/telemetry/sessionTracing.js +62 -0
  2026. package/src/utils/telemetry/skillLoadedEvent.js +4 -0
  2027. package/src/utils/telemetryAttributes.js +56 -0
  2028. package/src/utils/teleport/api.js +299 -0
  2029. package/src/utils/teleport/environmentSelection.js +55 -0
  2030. package/src/utils/teleport/environments.js +84 -0
  2031. package/src/utils/teleport/gitBundle.js +192 -0
  2032. package/src/utils/teleport.js +1047 -0
  2033. package/src/utils/tempfile.js +26 -0
  2034. package/src/utils/terminal.js +105 -0
  2035. package/src/utils/terminalPanel.js +155 -0
  2036. package/src/utils/textHighlighting.js +113 -0
  2037. package/src/utils/thaddeusHints.js +142 -0
  2038. package/src/utils/theme.js +525 -0
  2039. package/src/utils/thinking.js +130 -0
  2040. package/src/utils/timeouts.js +35 -0
  2041. package/src/utils/tmuxSocket.js +373 -0
  2042. package/src/utils/todo/types.js +9 -0
  2043. package/src/utils/tokenBudget.js +62 -0
  2044. package/src/utils/tokens.js +223 -0
  2045. package/src/utils/toolErrors.js +101 -0
  2046. package/src/utils/toolPool.js +61 -0
  2047. package/src/utils/toolResultStorage.js +768 -0
  2048. package/src/utils/toolSchemaCache.js +7 -0
  2049. package/src/utils/toolSearch.js +551 -0
  2050. package/src/utils/transcriptSearch.js +200 -0
  2051. package/src/utils/treeify.js +111 -0
  2052. package/src/utils/truncate.js +164 -0
  2053. package/src/utils/udsClient.js +5 -0
  2054. package/src/utils/udsMessaging.js +23 -0
  2055. package/src/utils/ultraplan/ccrSession.js +264 -0
  2056. package/src/utils/ultraplan/keyword.js +122 -0
  2057. package/src/utils/unaryLogging.js +16 -0
  2058. package/src/utils/undercover.js +89 -0
  2059. package/src/utils/user.js +137 -0
  2060. package/src/utils/userAgent.js +9 -0
  2061. package/src/utils/userPromptKeywords.js +21 -0
  2062. package/src/utils/uuid.js +22 -0
  2063. package/src/utils/warningHandler.js +97 -0
  2064. package/src/utils/which.js +75 -0
  2065. package/src/utils/windowsPaths.js +146 -0
  2066. package/src/utils/withResolvers.js +13 -0
  2067. package/src/utils/words.js +793 -0
  2068. package/src/utils/workforceIntent.js +192 -0
  2069. package/src/utils/workloadContext.js +42 -0
  2070. package/src/utils/worktree.js +1142 -0
  2071. package/src/utils/worktreeModeEnabled.js +11 -0
  2072. package/src/utils/xdg.js +52 -0
  2073. package/src/utils/xml.js +15 -0
  2074. package/src/utils/yaml.js +14 -0
  2075. package/src/utils/zodToJsonSchema.js +19 -0
  2076. package/src/vim/motions.js +73 -0
  2077. package/src/vim/operators.js +401 -0
  2078. package/src/vim/textObjects.js +153 -0
  2079. package/src/vim/transitions.js +340 -0
  2080. package/src/vim/types.js +93 -0
  2081. package/src/voice/voiceModeEnabled.js +20 -0
  2082. package/thaddeus-terminal.ts +401 -3
@@ -0,0 +1,2289 @@
1
+ import { feature } from 'bun:bundle';
2
+ import { randomUUID } from 'crypto';
3
+ import { hostname, tmpdir } from 'os';
4
+ import { basename, join, resolve } from 'path';
5
+ import { getRemoteSessionUrl } from '../constants/product.js';
6
+ import { shutdownDatadog } from '../services/analytics/datadog.js';
7
+ import { shutdown1PEventLogging } from '../services/analytics/firstPartyEventLogger.js';
8
+ import { checkGate_CACHED_OR_BLOCKING } from '../services/analytics/growthbook.js';
9
+ import { logEvent, logEventAsync, } from '../services/analytics/index.js';
10
+ import { isInBundledMode } from '../utils/bundledMode.js';
11
+ import { logForDebugging } from '../utils/debug.js';
12
+ import { logForDiagnosticsNoPII } from '../utils/diagLogs.js';
13
+ import { isEnvTruthy, isInProtectedNamespace } from '../utils/envUtils.js';
14
+ import { errorMessage } from '../utils/errors.js';
15
+ import { truncateToWidth } from '../utils/format.js';
16
+ import { logError } from '../utils/log.js';
17
+ import { sleep } from '../utils/sleep.js';
18
+ import { createAgentWorktree, removeAgentWorktree } from '../utils/worktree.js';
19
+ import { BridgeFatalError, createBridgeApiClient, isExpiredErrorType, isSuppressible403, validateBridgeId, } from './bridgeApi.js';
20
+ import { formatDuration } from './bridgeStatusUtil.js';
21
+ import { createBridgeLogger } from './bridgeUI.js';
22
+ import { createCapacityWake } from './capacityWake.js';
23
+ import { describeAxiosError } from './debugUtils.js';
24
+ import { createTokenRefreshScheduler } from './jwtUtils.js';
25
+ import { getPollIntervalConfig } from './pollConfig.js';
26
+ import { toCompatSessionId, toInfraSessionId } from './sessionIdCompat.js';
27
+ import { createSessionSpawner, safeFilenameId } from './sessionRunner.js';
28
+ import { getTrustedDeviceToken } from './trustedDevice.js';
29
+ import { BRIDGE_LOGIN_ERROR, DEFAULT_SESSION_TIMEOUT_MS, } from './types.js';
30
+ import { buildCCRv2SdkUrl, buildSdkUrl, decodeWorkSecret, registerWorker, sameSessionId, } from './workSecret.js';
31
+ const DEFAULT_BACKOFF = {
32
+ connInitialMs: 2_000,
33
+ connCapMs: 120_000, // 2 minutes
34
+ connGiveUpMs: 600_000, // 10 minutes
35
+ generalInitialMs: 500,
36
+ generalCapMs: 30_000,
37
+ generalGiveUpMs: 600_000, // 10 minutes
38
+ };
39
+ /** Status update interval for the live display (ms). */
40
+ const STATUS_UPDATE_INTERVAL_MS = 1_000;
41
+ const SPAWN_SESSIONS_DEFAULT = 32;
42
+ /**
43
+ * GrowthBook gate for multi-session spawn modes (--spawn / --capacity / --create-session-in-dir).
44
+ * Sibling of thaddeus_ccr_bridge_multi_environment (multiple envs per host:dir) —
45
+ * this one enables multiple sessions per environment.
46
+ * Rollout staged via targeting rules: ants first, then gradual external.
47
+ *
48
+ * Uses the blocking gate check so a stale disk-cache miss doesn't unfairly
49
+ * deny access. The fast path (cache has true) is still instant; only the
50
+ * cold-start path awaits the server fetch, and that fetch also seeds the
51
+ * disk cache for next time.
52
+ */
53
+ async function isMultiSessionSpawnEnabled() {
54
+ return checkGate_CACHED_OR_BLOCKING('thaddeus_ccr_bridge_multi_session');
55
+ }
56
+ /**
57
+ * Returns the threshold for detecting system sleep/wake in the poll loop.
58
+ * Must exceed the max backoff cap — otherwise normal backoff delays trigger
59
+ * false sleep detection (resetting the error budget indefinitely). Using
60
+ * 2× the connection backoff cap, matching the pattern in WebSocketTransport
61
+ * and replBridge.
62
+ */
63
+ function pollSleepDetectionThresholdMs(backoff) {
64
+ return backoff.connCapMs * 2;
65
+ }
66
+ /**
67
+ * Returns the args that must precede CLI flags when spawning a child claude
68
+ * process. In compiled binaries, process.execPath is the claude binary itself
69
+ * and args go directly to it. In npm installs (node running cli.js),
70
+ * process.execPath is the node runtime — the child spawn must pass the script
71
+ * path as the first arg, otherwise node interprets --sdk-url as a node option
72
+ * and exits with "bad option: --sdk-url". See anthropics/claude-code#28334.
73
+ */
74
+ function spawnScriptArgs() {
75
+ if (isInBundledMode() || !process.argv[1]) {
76
+ return [];
77
+ }
78
+ return [process.argv[1]];
79
+ }
80
+ /** Attempt to spawn a session; returns error string if spawn throws. */
81
+ function safeSpawn(spawner, opts, dir) {
82
+ try {
83
+ return spawner.spawn(opts, dir);
84
+ }
85
+ catch (err) {
86
+ const errMsg = errorMessage(err);
87
+ logError(new Error(`Session spawn failed: ${errMsg}`));
88
+ return errMsg;
89
+ }
90
+ }
91
+ export async function runBridgeLoop(config, environmentId, environmentSecret, api, spawner, logger, signal, backoffConfig = DEFAULT_BACKOFF, initialSessionId, getAccessToken) {
92
+ // Local abort controller so that onSessionDone can stop the poll loop.
93
+ // Linked to the incoming signal so external aborts also work.
94
+ const controller = new AbortController();
95
+ if (signal.aborted) {
96
+ controller.abort();
97
+ }
98
+ else {
99
+ signal.addEventListener('abort', () => controller.abort(), { once: true });
100
+ }
101
+ const loopSignal = controller.signal;
102
+ const activeSessions = new Map();
103
+ const sessionStartTimes = new Map();
104
+ const sessionWorkIds = new Map();
105
+ // Compat-surface ID (session_*) computed once at spawn and cached so
106
+ // cleanup and status-update ticks use the same key regardless of whether
107
+ // the thaddeus_bridge_repl_v2_cse_shim_enabled gate flips mid-session.
108
+ const sessionCompatIds = new Map();
109
+ // Session ingress JWTs for heartbeat auth, keyed by sessionId.
110
+ // Stored separately from handle.accessToken because the token refresh
111
+ // scheduler overwrites that field with the OAuth token (~3h55m in).
112
+ const sessionIngressTokens = new Map();
113
+ const sessionTimers = new Map();
114
+ const completedWorkIds = new Set();
115
+ const sessionWorktrees = new Map();
116
+ // Track sessions killed by the timeout watchdog so onSessionDone can
117
+ // distinguish them from server-initiated or shutdown interrupts.
118
+ const timedOutSessions = new Set();
119
+ // Sessions that already have a title (server-set or bridge-derived) so
120
+ // onFirstUserMessage doesn't clobber a user-assigned --name / web rename.
121
+ // Keyed by compatSessionId to match logger.setSessionTitle's key.
122
+ const titledSessions = new Set();
123
+ // Signal to wake the at-capacity sleep early when a session completes,
124
+ // so the bridge can immediately accept new work.
125
+ const capacityWake = createCapacityWake(loopSignal);
126
+ /**
127
+ * Heartbeat all active work items.
128
+ * Returns 'ok' if at least one heartbeat succeeded, 'auth_failed' if any
129
+ * got a 401/403 (JWT expired — re-queued via reconnectSession so the next
130
+ * poll delivers fresh work), or 'failed' if all failed for other reasons.
131
+ */
132
+ async function heartbeatActiveWorkItems() {
133
+ let anySuccess = false;
134
+ let anyFatal = false;
135
+ const authFailedSessions = [];
136
+ for (const [sessionId] of activeSessions) {
137
+ const workId = sessionWorkIds.get(sessionId);
138
+ const ingressToken = sessionIngressTokens.get(sessionId);
139
+ if (!workId || !ingressToken) {
140
+ continue;
141
+ }
142
+ try {
143
+ await api.heartbeatWork(environmentId, workId, ingressToken);
144
+ anySuccess = true;
145
+ }
146
+ catch (err) {
147
+ logForDebugging(`[bridge:heartbeat] Failed for sessionId=${sessionId} workId=${workId}: ${errorMessage(err)}`);
148
+ if (err instanceof BridgeFatalError) {
149
+ logEvent('thaddeus_bridge_heartbeat_error', {
150
+ status: err.status,
151
+ error_type: (err.status === 401 || err.status === 403
152
+ ? 'auth_failed'
153
+ : 'fatal'),
154
+ });
155
+ if (err.status === 401 || err.status === 403) {
156
+ authFailedSessions.push(sessionId);
157
+ }
158
+ else {
159
+ // 404/410 = environment expired or deleted — no point retrying
160
+ anyFatal = true;
161
+ }
162
+ }
163
+ }
164
+ }
165
+ // JWT expired → trigger server-side re-dispatch. Without this, work stays
166
+ // ACK'd out of the Redis PEL and poll returns empty forever (CC-1263).
167
+ // The existingHandle path below delivers the fresh token to the child.
168
+ // sessionId is already in the format /bridge/reconnect expects: it comes
169
+ // from work.data.id, which matches the server's EnvironmentInstance store
170
+ // (cse_* under the compat gate, session_* otherwise).
171
+ for (const sessionId of authFailedSessions) {
172
+ logger.logVerbose(`Session ${sessionId} token expired — re-queuing via bridge/reconnect`);
173
+ try {
174
+ await api.reconnectSession(environmentId, sessionId);
175
+ logForDebugging(`[bridge:heartbeat] Re-queued sessionId=${sessionId} via bridge/reconnect`);
176
+ }
177
+ catch (err) {
178
+ logger.logError(`Failed to refresh session ${sessionId} token: ${errorMessage(err)}`);
179
+ logForDebugging(`[bridge:heartbeat] reconnectSession(${sessionId}) failed: ${errorMessage(err)}`, { level: 'error' });
180
+ }
181
+ }
182
+ if (anyFatal) {
183
+ return 'fatal';
184
+ }
185
+ if (authFailedSessions.length > 0) {
186
+ return 'auth_failed';
187
+ }
188
+ return anySuccess ? 'ok' : 'failed';
189
+ }
190
+ // Sessions spawned with CCR v2 env vars. v2 children cannot use OAuth
191
+ // tokens (CCR worker endpoints validate the JWT's session_id claim,
192
+ // register_worker.go:32), so onRefresh triggers server re-dispatch
193
+ // instead — the next poll delivers fresh work with a new JWT via the
194
+ // existingHandle path below.
195
+ const v2Sessions = new Set();
196
+ // Proactive token refresh: schedules a timer 5min before the session
197
+ // ingress JWT expires. v1 delivers OAuth directly; v2 calls
198
+ // reconnectSession to trigger server re-dispatch (CC-1263: without
199
+ // this, v2 daemon sessions silently die at ~5h since the server does
200
+ // not auto-re-dispatch ACK'd work on lease expiry).
201
+ const tokenRefresh = getAccessToken
202
+ ? createTokenRefreshScheduler({
203
+ getAccessToken,
204
+ onRefresh: (sessionId, oauthToken) => {
205
+ const handle = activeSessions.get(sessionId);
206
+ if (!handle) {
207
+ return;
208
+ }
209
+ if (v2Sessions.has(sessionId)) {
210
+ logger.logVerbose(`Refreshing session ${sessionId} token via bridge/reconnect`);
211
+ void api
212
+ .reconnectSession(environmentId, sessionId)
213
+ .catch((err) => {
214
+ logger.logError(`Failed to refresh session ${sessionId} token: ${errorMessage(err)}`);
215
+ logForDebugging(`[bridge:token] reconnectSession(${sessionId}) failed: ${errorMessage(err)}`, { level: 'error' });
216
+ });
217
+ }
218
+ else {
219
+ handle.updateAccessToken(oauthToken);
220
+ }
221
+ },
222
+ label: 'bridge',
223
+ })
224
+ : null;
225
+ const loopStartTime = Date.now();
226
+ // Track all in-flight cleanup promises (stopWork, worktree removal) so
227
+ // the shutdown sequence can await them before process.exit().
228
+ const pendingCleanups = new Set();
229
+ function trackCleanup(p) {
230
+ pendingCleanups.add(p);
231
+ void p.finally(() => pendingCleanups.delete(p));
232
+ }
233
+ let connBackoff = 0;
234
+ let generalBackoff = 0;
235
+ let connErrorStart = null;
236
+ let generalErrorStart = null;
237
+ let lastPollErrorTime = null;
238
+ let statusUpdateTimer = null;
239
+ // Set by BridgeFatalError and give-up paths so the shutdown block can
240
+ // skip the resume message (resume is impossible after env expiry/auth
241
+ // failure/sustained connection errors).
242
+ let fatalExit = false;
243
+ logForDebugging(`[bridge:work] Starting poll loop spawnMode=${config.spawnMode} maxSessions=${config.maxSessions} environmentId=${environmentId}`);
244
+ logForDiagnosticsNoPII('info', 'bridge_loop_started', {
245
+ max_sessions: config.maxSessions,
246
+ spawn_mode: config.spawnMode,
247
+ });
248
+ // For ant users, show where session debug logs will land so they can tail them.
249
+ // sessionRunner.ts uses the same base path. File appears once a session spawns.
250
+ if (process.env.USER_TYPE === 'ant') {
251
+ let debugGlob;
252
+ if (config.debugFile) {
253
+ const ext = config.debugFile.lastIndexOf('.');
254
+ debugGlob =
255
+ ext > 0
256
+ ? `${config.debugFile.slice(0, ext)}-*${config.debugFile.slice(ext)}`
257
+ : `${config.debugFile}-*`;
258
+ }
259
+ else {
260
+ debugGlob = join(tmpdir(), 'claude', 'bridge-session-*.log');
261
+ }
262
+ logger.setDebugLogPath(debugGlob);
263
+ }
264
+ logger.printBanner(config, environmentId);
265
+ // Seed the logger's session count + spawn mode before any render. Without
266
+ // this, setAttached() below renders with the logger's default sessionMax=1,
267
+ // showing "Capacity: 0/1" until the status ticker kicks in (which is gated
268
+ // by !initialSessionId and only starts after the poll loop picks up work).
269
+ logger.updateSessionCount(0, config.maxSessions, config.spawnMode);
270
+ // If an initial session was pre-created, show its URL from the start so
271
+ // the user can click through immediately (matching /remote-control behavior).
272
+ if (initialSessionId) {
273
+ logger.setAttached(initialSessionId);
274
+ }
275
+ /** Refresh the inline status display. Shows idle or active depending on state. */
276
+ function updateStatusDisplay() {
277
+ // Push the session count (no-op when maxSessions === 1) so the
278
+ // next renderStatusLine tick shows the current count.
279
+ logger.updateSessionCount(activeSessions.size, config.maxSessions, config.spawnMode);
280
+ // Push per-session activity into the multi-session display.
281
+ for (const [sid, handle] of activeSessions) {
282
+ const act = handle.currentActivity;
283
+ if (act) {
284
+ logger.updateSessionActivity(sessionCompatIds.get(sid) ?? sid, act);
285
+ }
286
+ }
287
+ if (activeSessions.size === 0) {
288
+ logger.updateIdleStatus();
289
+ return;
290
+ }
291
+ // Show the most recently started session that is still actively working.
292
+ // Sessions whose current activity is 'result' or 'error' are between
293
+ // turns — the CLI emitted its result but the process stays alive waiting
294
+ // for the next user message. Skip updating so the status line keeps
295
+ // whatever state it had (Attached / session title).
296
+ const [sessionId, handle] = [...activeSessions.entries()].pop();
297
+ const startTime = sessionStartTimes.get(sessionId);
298
+ if (!startTime)
299
+ return;
300
+ const activity = handle.currentActivity;
301
+ if (!activity || activity.type === 'result' || activity.type === 'error') {
302
+ // Session is between turns — keep current status (Attached/titled).
303
+ // In multi-session mode, still refresh so bullet-list activities stay current.
304
+ if (config.maxSessions > 1)
305
+ logger.refreshDisplay();
306
+ return;
307
+ }
308
+ const elapsed = formatDuration(Date.now() - startTime);
309
+ // Build trail from recent tool activities (last 5)
310
+ const trail = handle.activities
311
+ .filter(a => a.type === 'tool_start')
312
+ .slice(-5)
313
+ .map(a => a.summary);
314
+ logger.updateSessionStatus(sessionId, elapsed, activity, trail);
315
+ }
316
+ /** Start the status display update ticker. */
317
+ function startStatusUpdates() {
318
+ stopStatusUpdates();
319
+ // Call immediately so the first transition (e.g. Connecting → Ready)
320
+ // happens without delay, avoiding concurrent timer races.
321
+ updateStatusDisplay();
322
+ statusUpdateTimer = setInterval(updateStatusDisplay, STATUS_UPDATE_INTERVAL_MS);
323
+ }
324
+ /** Stop the status display update ticker. */
325
+ function stopStatusUpdates() {
326
+ if (statusUpdateTimer) {
327
+ clearInterval(statusUpdateTimer);
328
+ statusUpdateTimer = null;
329
+ }
330
+ }
331
+ function onSessionDone(sessionId, startTime, handle) {
332
+ return (rawStatus) => {
333
+ const workId = sessionWorkIds.get(sessionId);
334
+ activeSessions.delete(sessionId);
335
+ sessionStartTimes.delete(sessionId);
336
+ sessionWorkIds.delete(sessionId);
337
+ sessionIngressTokens.delete(sessionId);
338
+ const compatId = sessionCompatIds.get(sessionId) ?? sessionId;
339
+ sessionCompatIds.delete(sessionId);
340
+ logger.removeSession(compatId);
341
+ titledSessions.delete(compatId);
342
+ v2Sessions.delete(sessionId);
343
+ // Clear per-session timeout timer
344
+ const timer = sessionTimers.get(sessionId);
345
+ if (timer) {
346
+ clearTimeout(timer);
347
+ sessionTimers.delete(sessionId);
348
+ }
349
+ // Clear token refresh timer
350
+ tokenRefresh?.cancel(sessionId);
351
+ // Wake the at-capacity sleep so the bridge can accept new work immediately
352
+ capacityWake.wake();
353
+ // If the session was killed by the timeout watchdog, treat it as a
354
+ // failed session (not a server/shutdown interrupt) so we still call
355
+ // stopWork and archiveSession below.
356
+ const wasTimedOut = timedOutSessions.delete(sessionId);
357
+ const status = wasTimedOut && rawStatus === 'interrupted' ? 'failed' : rawStatus;
358
+ const durationMs = Date.now() - startTime;
359
+ logForDebugging(`[bridge:session] sessionId=${sessionId} workId=${workId ?? 'unknown'} exited status=${status} duration=${formatDuration(durationMs)}`);
360
+ logEvent('thaddeus_bridge_session_done', {
361
+ status: status,
362
+ duration_ms: durationMs,
363
+ });
364
+ logForDiagnosticsNoPII('info', 'bridge_session_done', {
365
+ status,
366
+ duration_ms: durationMs,
367
+ });
368
+ // Clear the status display before printing final log
369
+ logger.clearStatus();
370
+ stopStatusUpdates();
371
+ // Build error message from stderr if available
372
+ const stderrSummary = handle.lastStderr.length > 0 ? handle.lastStderr.join('\n') : undefined;
373
+ let failureMessage;
374
+ switch (status) {
375
+ case 'completed':
376
+ logger.logSessionComplete(sessionId, durationMs);
377
+ break;
378
+ case 'failed':
379
+ // Skip failure log during shutdown — the child exits non-zero when
380
+ // killed, which is expected and not a real failure.
381
+ // Also skip for timeout-killed sessions — the timeout watchdog
382
+ // already logged a clear timeout message.
383
+ if (!wasTimedOut && !loopSignal.aborted) {
384
+ failureMessage = stderrSummary ?? 'Process exited with error';
385
+ logger.logSessionFailed(sessionId, failureMessage);
386
+ logError(new Error(`Bridge session failed: ${failureMessage}`));
387
+ }
388
+ break;
389
+ case 'interrupted':
390
+ logger.logVerbose(`Session ${sessionId} interrupted`);
391
+ break;
392
+ }
393
+ // Notify the server that this work item is done. Skip for interrupted
394
+ // sessions — interrupts are either server-initiated (the server already
395
+ // knows) or caused by bridge shutdown (which calls stopWork() separately).
396
+ if (status !== 'interrupted' && workId) {
397
+ trackCleanup(stopWorkWithRetry(api, environmentId, workId, logger, backoffConfig.stopWorkBaseDelayMs));
398
+ completedWorkIds.add(workId);
399
+ }
400
+ // Clean up worktree if one was created for this session
401
+ const wt = sessionWorktrees.get(sessionId);
402
+ if (wt) {
403
+ sessionWorktrees.delete(sessionId);
404
+ trackCleanup(removeAgentWorktree(wt.worktreePath, wt.worktreeBranch, wt.gitRoot, wt.hookBased).catch((err) => logger.logVerbose(`Failed to remove worktree ${wt.worktreePath}: ${errorMessage(err)}`)));
405
+ }
406
+ // Lifecycle decision: in multi-session mode, keep the bridge running
407
+ // after a session completes. In single-session mode, abort the poll
408
+ // loop so the bridge exits cleanly.
409
+ if (status !== 'interrupted' && !loopSignal.aborted) {
410
+ if (config.spawnMode !== 'single-session') {
411
+ // Multi-session: archive the completed session so it doesn't linger
412
+ // as stale in the web UI. archiveSession is idempotent (409 if already
413
+ // archived), so double-archiving at shutdown is safe.
414
+ // sessionId arrived as cse_* from the work poll (infrastructure-layer
415
+ // tag). archiveSession hits /v1/sessions/{id}/archive which is the
416
+ // compat surface and validates TagSession (session_*). Re-tag — same
417
+ // UUID underneath.
418
+ trackCleanup(api
419
+ .archiveSession(compatId)
420
+ .catch((err) => logger.logVerbose(`Failed to archive session ${sessionId}: ${errorMessage(err)}`)));
421
+ logForDebugging(`[bridge:session] Session ${status}, returning to idle (multi-session mode)`);
422
+ }
423
+ else {
424
+ // Single-session: coupled lifecycle — tear down environment
425
+ logForDebugging(`[bridge:session] Session ${status}, aborting poll loop to tear down environment`);
426
+ controller.abort();
427
+ return;
428
+ }
429
+ }
430
+ if (!loopSignal.aborted) {
431
+ startStatusUpdates();
432
+ }
433
+ };
434
+ }
435
+ // Start the idle status display immediately — unless we have a pre-created
436
+ // session, in which case setAttached() already set up the display and the
437
+ // poll loop will start status updates when it picks up the session.
438
+ if (!initialSessionId) {
439
+ startStatusUpdates();
440
+ }
441
+ while (!loopSignal.aborted) {
442
+ // Fetched once per iteration — the GrowthBook cache refreshes every
443
+ // 5 min, so a loop running at the at-capacity rate picks up config
444
+ // changes within one sleep cycle.
445
+ const pollConfig = getPollIntervalConfig();
446
+ try {
447
+ const work = await api.pollForWork(environmentId, environmentSecret, loopSignal, pollConfig.reclaim_older_than_ms);
448
+ // Log reconnection if we were previously disconnected
449
+ const wasDisconnected = connErrorStart !== null || generalErrorStart !== null;
450
+ if (wasDisconnected) {
451
+ const disconnectedMs = Date.now() - (connErrorStart ?? generalErrorStart ?? Date.now());
452
+ logger.logReconnected(disconnectedMs);
453
+ logForDebugging(`[bridge:poll] Reconnected after ${formatDuration(disconnectedMs)}`);
454
+ logEvent('thaddeus_bridge_reconnected', {
455
+ disconnected_ms: disconnectedMs,
456
+ });
457
+ }
458
+ connBackoff = 0;
459
+ generalBackoff = 0;
460
+ connErrorStart = null;
461
+ generalErrorStart = null;
462
+ lastPollErrorTime = null;
463
+ // Null response = no work available in the queue.
464
+ // Add a minimum delay to avoid hammering the server.
465
+ if (!work) {
466
+ // Use live check (not a snapshot) since sessions can end during poll.
467
+ const atCap = activeSessions.size >= config.maxSessions;
468
+ if (atCap) {
469
+ const atCapMs = pollConfig.multisession_poll_interval_ms_at_capacity;
470
+ // Heartbeat loops WITHOUT polling. When at-capacity polling is also
471
+ // enabled (atCapMs > 0), the loop tracks a deadline and breaks out
472
+ // to poll at that interval — heartbeat and poll compose instead of
473
+ // one suppressing the other. We break out to poll when:
474
+ // - Poll deadline reached (atCapMs > 0 only)
475
+ // - Auth fails (JWT expired → poll refreshes tokens)
476
+ // - Capacity wake fires (session ended → poll for new work)
477
+ // - Loop aborted (shutdown)
478
+ if (pollConfig.non_exclusive_heartbeat_interval_ms > 0) {
479
+ logEvent('thaddeus_bridge_heartbeat_mode_entered', {
480
+ active_sessions: activeSessions.size,
481
+ heartbeat_interval_ms: pollConfig.non_exclusive_heartbeat_interval_ms,
482
+ });
483
+ // Deadline computed once at entry — GB updates to atCapMs don't
484
+ // shift an in-flight deadline (next entry picks up the new value).
485
+ const pollDeadline = atCapMs > 0 ? Date.now() + atCapMs : null;
486
+ let hbResult = 'ok';
487
+ let hbCycles = 0;
488
+ while (!loopSignal.aborted &&
489
+ activeSessions.size >= config.maxSessions &&
490
+ (pollDeadline === null || Date.now() < pollDeadline)) {
491
+ // Re-read config each cycle so GrowthBook updates take effect
492
+ const hbConfig = getPollIntervalConfig();
493
+ if (hbConfig.non_exclusive_heartbeat_interval_ms <= 0)
494
+ break;
495
+ // Capture capacity signal BEFORE the async heartbeat call so
496
+ // a session ending during the HTTP request is caught by the
497
+ // subsequent sleep (instead of being lost to a replaced controller).
498
+ const cap = capacityWake.signal();
499
+ hbResult = await heartbeatActiveWorkItems();
500
+ if (hbResult === 'auth_failed' || hbResult === 'fatal') {
501
+ cap.cleanup();
502
+ break;
503
+ }
504
+ hbCycles++;
505
+ await sleep(hbConfig.non_exclusive_heartbeat_interval_ms, cap.signal);
506
+ cap.cleanup();
507
+ }
508
+ // Determine exit reason for telemetry
509
+ const exitReason = hbResult === 'auth_failed' || hbResult === 'fatal'
510
+ ? hbResult
511
+ : loopSignal.aborted
512
+ ? 'shutdown'
513
+ : activeSessions.size < config.maxSessions
514
+ ? 'capacity_changed'
515
+ : pollDeadline !== null && Date.now() >= pollDeadline
516
+ ? 'poll_due'
517
+ : 'config_disabled';
518
+ logEvent('thaddeus_bridge_heartbeat_mode_exited', {
519
+ reason: exitReason,
520
+ heartbeat_cycles: hbCycles,
521
+ active_sessions: activeSessions.size,
522
+ });
523
+ if (exitReason === 'poll_due') {
524
+ // bridgeApi throttles empty-poll logs (EMPTY_POLL_LOG_INTERVAL=100)
525
+ // so the once-per-10min poll_due poll is invisible at counter=2.
526
+ // Log it here so verification runs see both endpoints in the debug log.
527
+ logForDebugging(`[bridge:poll] Heartbeat poll_due after ${hbCycles} cycles — falling through to pollForWork`);
528
+ }
529
+ // On auth_failed or fatal, sleep before polling to avoid a tight
530
+ // poll+heartbeat loop. Auth_failed: heartbeatActiveWorkItems
531
+ // already called reconnectSession — the sleep gives the server
532
+ // time to propagate the re-queue. Fatal (404/410): may be a
533
+ // single work item GCd while the environment is still valid.
534
+ // Use atCapMs if enabled, else the heartbeat interval as a floor
535
+ // (guaranteed > 0 here) so heartbeat-only configs don't tight-loop.
536
+ if (hbResult === 'auth_failed' || hbResult === 'fatal') {
537
+ const cap = capacityWake.signal();
538
+ await sleep(atCapMs > 0
539
+ ? atCapMs
540
+ : pollConfig.non_exclusive_heartbeat_interval_ms, cap.signal);
541
+ cap.cleanup();
542
+ }
543
+ }
544
+ else if (atCapMs > 0) {
545
+ // Heartbeat disabled: slow poll as liveness signal.
546
+ const cap = capacityWake.signal();
547
+ await sleep(atCapMs, cap.signal);
548
+ cap.cleanup();
549
+ }
550
+ }
551
+ else {
552
+ const interval = activeSessions.size > 0
553
+ ? pollConfig.multisession_poll_interval_ms_partial_capacity
554
+ : pollConfig.multisession_poll_interval_ms_not_at_capacity;
555
+ await sleep(interval, loopSignal);
556
+ }
557
+ continue;
558
+ }
559
+ // At capacity — we polled to keep the heartbeat alive, but cannot
560
+ // accept new work right now. We still enter the switch below so that
561
+ // token refreshes for existing sessions are processed (the case
562
+ // 'session' handler checks for existing sessions before the inner
563
+ // capacity guard).
564
+ const atCapacityBeforeSwitch = activeSessions.size >= config.maxSessions;
565
+ // Skip work items that have already been completed and stopped.
566
+ // The server may re-deliver stale work before processing our stop
567
+ // request, which would otherwise cause a duplicate session spawn.
568
+ if (completedWorkIds.has(work.id)) {
569
+ logForDebugging(`[bridge:work] Skipping already-completed workId=${work.id}`);
570
+ // Respect capacity throttle — without a sleep here, persistent stale
571
+ // redeliveries would tight-loop at poll-request speed (the !work
572
+ // branch above is the only sleep, and work != null skips it).
573
+ if (atCapacityBeforeSwitch) {
574
+ const cap = capacityWake.signal();
575
+ if (pollConfig.non_exclusive_heartbeat_interval_ms > 0) {
576
+ await heartbeatActiveWorkItems();
577
+ await sleep(pollConfig.non_exclusive_heartbeat_interval_ms, cap.signal);
578
+ }
579
+ else if (pollConfig.multisession_poll_interval_ms_at_capacity > 0) {
580
+ await sleep(pollConfig.multisession_poll_interval_ms_at_capacity, cap.signal);
581
+ }
582
+ cap.cleanup();
583
+ }
584
+ else {
585
+ await sleep(1000, loopSignal);
586
+ }
587
+ continue;
588
+ }
589
+ // Decode the work secret for session spawning and to extract the JWT
590
+ // used for the ack call below.
591
+ let secret;
592
+ try {
593
+ secret = decodeWorkSecret(work.secret);
594
+ }
595
+ catch (err) {
596
+ const errMsg = errorMessage(err);
597
+ logger.logError(`Failed to decode work secret for workId=${work.id}: ${errMsg}`);
598
+ logEvent('thaddeus_bridge_work_secret_failed', {});
599
+ // Can't ack (needs the JWT we failed to decode). stopWork uses OAuth,
600
+ // so it's callable here — prevents XAUTOCLAIM from re-delivering this
601
+ // poisoned item every reclaim_older_than_ms cycle.
602
+ completedWorkIds.add(work.id);
603
+ trackCleanup(stopWorkWithRetry(api, environmentId, work.id, logger, backoffConfig.stopWorkBaseDelayMs));
604
+ // Respect capacity throttle before retrying — without a sleep here,
605
+ // repeated decode failures at capacity would tight-loop at
606
+ // poll-request speed (work != null skips the !work sleep above).
607
+ if (atCapacityBeforeSwitch) {
608
+ const cap = capacityWake.signal();
609
+ if (pollConfig.non_exclusive_heartbeat_interval_ms > 0) {
610
+ await heartbeatActiveWorkItems();
611
+ await sleep(pollConfig.non_exclusive_heartbeat_interval_ms, cap.signal);
612
+ }
613
+ else if (pollConfig.multisession_poll_interval_ms_at_capacity > 0) {
614
+ await sleep(pollConfig.multisession_poll_interval_ms_at_capacity, cap.signal);
615
+ }
616
+ cap.cleanup();
617
+ }
618
+ continue;
619
+ }
620
+ // Explicitly acknowledge after committing to handle the work — NOT
621
+ // before. The at-capacity guard inside case 'session' can break
622
+ // without spawning; acking there would permanently lose the work.
623
+ // Ack failures are non-fatal: server re-delivers, and existingHandle
624
+ // / completedWorkIds paths handle the dedup.
625
+ const ackWork = async () => {
626
+ logForDebugging(`[bridge:work] Acknowledging workId=${work.id}`);
627
+ try {
628
+ await api.acknowledgeWork(environmentId, work.id, secret.session_ingress_token);
629
+ }
630
+ catch (err) {
631
+ logForDebugging(`[bridge:work] Acknowledge failed workId=${work.id}: ${errorMessage(err)}`);
632
+ }
633
+ };
634
+ const workType = work.data.type;
635
+ switch (work.data.type) {
636
+ case 'healthcheck':
637
+ await ackWork();
638
+ logForDebugging('[bridge:work] Healthcheck received');
639
+ logger.logVerbose('Healthcheck received');
640
+ break;
641
+ case 'session': {
642
+ const sessionId = work.data.id;
643
+ try {
644
+ validateBridgeId(sessionId, 'session_id');
645
+ }
646
+ catch {
647
+ await ackWork();
648
+ logger.logError(`Invalid session_id received: ${sessionId}`);
649
+ break;
650
+ }
651
+ // If the session is already running, deliver the fresh token so
652
+ // the child process can reconnect its WebSocket with the new
653
+ // session ingress token. This handles the case where the server
654
+ // re-dispatches work for an existing session after the WS drops.
655
+ const existingHandle = activeSessions.get(sessionId);
656
+ if (existingHandle) {
657
+ existingHandle.updateAccessToken(secret.session_ingress_token);
658
+ sessionIngressTokens.set(sessionId, secret.session_ingress_token);
659
+ sessionWorkIds.set(sessionId, work.id);
660
+ // Re-schedule next refresh from the fresh JWT's expiry. onRefresh
661
+ // branches on v2Sessions so both v1 and v2 are safe here.
662
+ tokenRefresh?.schedule(sessionId, secret.session_ingress_token);
663
+ logForDebugging(`[bridge:work] Updated access token for existing sessionId=${sessionId} workId=${work.id}`);
664
+ await ackWork();
665
+ break;
666
+ }
667
+ // At capacity — token refresh for existing sessions is handled
668
+ // above, but we cannot spawn new ones. The post-switch capacity
669
+ // sleep will throttle the loop; just break here.
670
+ if (activeSessions.size >= config.maxSessions) {
671
+ logForDebugging(`[bridge:work] At capacity (${activeSessions.size}/${config.maxSessions}), cannot spawn new session for workId=${work.id}`);
672
+ break;
673
+ }
674
+ await ackWork();
675
+ const spawnStartTime = Date.now();
676
+ // CCR v2 path: register this bridge as the session worker, get the
677
+ // epoch, and point the child at /v1/code/sessions/{id}. The child
678
+ // already has the full v2 client (SSETransport + CCRClient) — same
679
+ // code path environment-manager launches in containers.
680
+ //
681
+ // v1 path: Session-Ingress WebSocket. Uses config.sessionIngressUrl
682
+ // (not secret.api_base_url, which may point to a remote proxy tunnel
683
+ // that doesn't know about locally-created sessions).
684
+ let sdkUrl;
685
+ let useCcrV2 = false;
686
+ let workerEpoch;
687
+ // Server decides per-session via the work secret; env var is the
688
+ // ant-dev override (e.g. forcing v2 before the server flag is on).
689
+ if (secret.use_code_sessions === true ||
690
+ isEnvTruthy(process.env.CLAUDE_BRIDGE_USE_CCR_V2)) {
691
+ sdkUrl = buildCCRv2SdkUrl(config.apiBaseUrl, sessionId);
692
+ // Retry once on transient failure (network blip, 500) before
693
+ // permanently giving up and killing the session.
694
+ for (let attempt = 1; attempt <= 2; attempt++) {
695
+ try {
696
+ workerEpoch = await registerWorker(sdkUrl, secret.session_ingress_token);
697
+ useCcrV2 = true;
698
+ logForDebugging(`[bridge:session] CCR v2: registered worker sessionId=${sessionId} epoch=${workerEpoch} attempt=${attempt}`);
699
+ break;
700
+ }
701
+ catch (err) {
702
+ const errMsg = errorMessage(err);
703
+ if (attempt < 2) {
704
+ logForDebugging(`[bridge:session] CCR v2: registerWorker attempt ${attempt} failed, retrying: ${errMsg}`);
705
+ await sleep(2_000, loopSignal);
706
+ if (loopSignal.aborted)
707
+ break;
708
+ continue;
709
+ }
710
+ logger.logError(`CCR v2 worker registration failed for session ${sessionId}: ${errMsg}`);
711
+ logError(new Error(`registerWorker failed: ${errMsg}`));
712
+ completedWorkIds.add(work.id);
713
+ trackCleanup(stopWorkWithRetry(api, environmentId, work.id, logger, backoffConfig.stopWorkBaseDelayMs));
714
+ }
715
+ }
716
+ if (!useCcrV2)
717
+ break;
718
+ }
719
+ else {
720
+ sdkUrl = buildSdkUrl(config.sessionIngressUrl, sessionId);
721
+ }
722
+ // In worktree mode, on-demand sessions get an isolated git worktree
723
+ // so concurrent sessions don't interfere with each other's file
724
+ // changes. The pre-created initial session (if any) runs in
725
+ // config.dir so the user's first session lands in the directory they
726
+ // invoked `rc` from — matching the old single-session UX.
727
+ // In same-dir and single-session modes, all sessions share config.dir.
728
+ // Capture spawnMode before the await below — the `w` key handler
729
+ // mutates config.spawnMode directly, and createAgentWorktree can
730
+ // take 1-2s, so reading config.spawnMode after the await can
731
+ // produce contradictory analytics (spawn_mode:'same-dir', in_worktree:true).
732
+ const spawnModeAtDecision = config.spawnMode;
733
+ let sessionDir = config.dir;
734
+ let worktreeCreateMs = 0;
735
+ if (spawnModeAtDecision === 'worktree' &&
736
+ (initialSessionId === undefined ||
737
+ !sameSessionId(sessionId, initialSessionId))) {
738
+ const wtStart = Date.now();
739
+ try {
740
+ const wt = await createAgentWorktree(`bridge-${safeFilenameId(sessionId)}`);
741
+ worktreeCreateMs = Date.now() - wtStart;
742
+ sessionWorktrees.set(sessionId, {
743
+ worktreePath: wt.worktreePath,
744
+ worktreeBranch: wt.worktreeBranch,
745
+ gitRoot: wt.gitRoot,
746
+ hookBased: wt.hookBased,
747
+ });
748
+ sessionDir = wt.worktreePath;
749
+ logForDebugging(`[bridge:session] Created worktree for sessionId=${sessionId} at ${wt.worktreePath}`);
750
+ }
751
+ catch (err) {
752
+ const errMsg = errorMessage(err);
753
+ logger.logError(`Failed to create worktree for session ${sessionId}: ${errMsg}`);
754
+ logError(new Error(`Worktree creation failed: ${errMsg}`));
755
+ completedWorkIds.add(work.id);
756
+ trackCleanup(stopWorkWithRetry(api, environmentId, work.id, logger, backoffConfig.stopWorkBaseDelayMs));
757
+ break;
758
+ }
759
+ }
760
+ logForDebugging(`[bridge:session] Spawning sessionId=${sessionId} sdkUrl=${sdkUrl}`);
761
+ // compat-surface session_* form for logger/Sessions-API calls.
762
+ // Work poll returns cse_* under v2 compat; convert before spawn so
763
+ // the onFirstUserMessage callback can close over it.
764
+ const compatSessionId = toCompatSessionId(sessionId);
765
+ const spawnResult = safeSpawn(spawner, {
766
+ sessionId,
767
+ sdkUrl,
768
+ accessToken: secret.session_ingress_token,
769
+ useCcrV2,
770
+ workerEpoch,
771
+ onFirstUserMessage: text => {
772
+ // Server-set titles (--name, web rename) win. fetchSessionTitle
773
+ // runs concurrently; if it already populated titledSessions,
774
+ // skip. If it hasn't resolved yet, the derived title sticks —
775
+ // acceptable since the server had no title at spawn time.
776
+ if (titledSessions.has(compatSessionId))
777
+ return;
778
+ titledSessions.add(compatSessionId);
779
+ const title = deriveSessionTitle(text);
780
+ logger.setSessionTitle(compatSessionId, title);
781
+ logForDebugging(`[bridge:title] derived title for ${compatSessionId}: ${title}`);
782
+ void import('./createSession.js')
783
+ .then(({ updateBridgeSessionTitle }) => updateBridgeSessionTitle(compatSessionId, title, {
784
+ baseUrl: config.apiBaseUrl,
785
+ }))
786
+ .catch(err => logForDebugging(`[bridge:title] failed to update title for ${compatSessionId}: ${err}`, { level: 'error' }));
787
+ },
788
+ }, sessionDir);
789
+ if (typeof spawnResult === 'string') {
790
+ logger.logError(`Failed to spawn session ${sessionId}: ${spawnResult}`);
791
+ // Clean up worktree if one was created for this session
792
+ const wt = sessionWorktrees.get(sessionId);
793
+ if (wt) {
794
+ sessionWorktrees.delete(sessionId);
795
+ trackCleanup(removeAgentWorktree(wt.worktreePath, wt.worktreeBranch, wt.gitRoot, wt.hookBased).catch((err) => logger.logVerbose(`Failed to remove worktree ${wt.worktreePath}: ${errorMessage(err)}`)));
796
+ }
797
+ completedWorkIds.add(work.id);
798
+ trackCleanup(stopWorkWithRetry(api, environmentId, work.id, logger, backoffConfig.stopWorkBaseDelayMs));
799
+ break;
800
+ }
801
+ const handle = spawnResult;
802
+ const spawnDurationMs = Date.now() - spawnStartTime;
803
+ logEvent('thaddeus_bridge_session_started', {
804
+ active_sessions: activeSessions.size,
805
+ spawn_mode: spawnModeAtDecision,
806
+ in_worktree: sessionWorktrees.has(sessionId),
807
+ spawn_duration_ms: spawnDurationMs,
808
+ worktree_create_ms: worktreeCreateMs,
809
+ inProtectedNamespace: isInProtectedNamespace(),
810
+ });
811
+ logForDiagnosticsNoPII('info', 'bridge_session_started', {
812
+ spawn_mode: spawnModeAtDecision,
813
+ in_worktree: sessionWorktrees.has(sessionId),
814
+ spawn_duration_ms: spawnDurationMs,
815
+ worktree_create_ms: worktreeCreateMs,
816
+ });
817
+ activeSessions.set(sessionId, handle);
818
+ sessionWorkIds.set(sessionId, work.id);
819
+ sessionIngressTokens.set(sessionId, secret.session_ingress_token);
820
+ sessionCompatIds.set(sessionId, compatSessionId);
821
+ const startTime = Date.now();
822
+ sessionStartTimes.set(sessionId, startTime);
823
+ // Use a generic prompt description since we no longer get startup_context
824
+ logger.logSessionStart(sessionId, `Session ${sessionId}`);
825
+ // Compute the actual debug file path (mirrors sessionRunner.ts logic)
826
+ const safeId = safeFilenameId(sessionId);
827
+ let sessionDebugFile;
828
+ if (config.debugFile) {
829
+ const ext = config.debugFile.lastIndexOf('.');
830
+ if (ext > 0) {
831
+ sessionDebugFile = `${config.debugFile.slice(0, ext)}-${safeId}${config.debugFile.slice(ext)}`;
832
+ }
833
+ else {
834
+ sessionDebugFile = `${config.debugFile}-${safeId}`;
835
+ }
836
+ }
837
+ else if (config.verbose || process.env.USER_TYPE === 'ant') {
838
+ sessionDebugFile = join(tmpdir(), 'claude', `bridge-session-${safeId}.log`);
839
+ }
840
+ if (sessionDebugFile) {
841
+ logger.logVerbose(`Debug log: ${sessionDebugFile}`);
842
+ }
843
+ // Register in the sessions Map before starting status updates so the
844
+ // first render tick shows the correct count and bullet list in sync.
845
+ logger.addSession(compatSessionId, getRemoteSessionUrl(compatSessionId, config.sessionIngressUrl));
846
+ // Start live status updates and transition to "Attached" state.
847
+ startStatusUpdates();
848
+ logger.setAttached(compatSessionId);
849
+ // One-shot title fetch. If the session already has a title (set via
850
+ // --name, web rename, or /remote-control), display it and mark as
851
+ // titled so the first-user-message fallback doesn't overwrite it.
852
+ // Otherwise onFirstUserMessage derives one from the first prompt.
853
+ void fetchSessionTitle(compatSessionId, config.apiBaseUrl)
854
+ .then(title => {
855
+ if (title && activeSessions.has(sessionId)) {
856
+ titledSessions.add(compatSessionId);
857
+ logger.setSessionTitle(compatSessionId, title);
858
+ logForDebugging(`[bridge:title] server title for ${compatSessionId}: ${title}`);
859
+ }
860
+ })
861
+ .catch(err => logForDebugging(`[bridge:title] failed to fetch title for ${compatSessionId}: ${err}`, { level: 'error' }));
862
+ // Start per-session timeout watchdog
863
+ const timeoutMs = config.sessionTimeoutMs ?? DEFAULT_SESSION_TIMEOUT_MS;
864
+ if (timeoutMs > 0) {
865
+ const timer = setTimeout(onSessionTimeout, timeoutMs, sessionId, timeoutMs, logger, timedOutSessions, handle);
866
+ sessionTimers.set(sessionId, timer);
867
+ }
868
+ // Schedule proactive token refresh before the JWT expires.
869
+ // onRefresh branches on v2Sessions: v1 delivers OAuth to the
870
+ // child, v2 triggers server re-dispatch via reconnectSession.
871
+ if (useCcrV2) {
872
+ v2Sessions.add(sessionId);
873
+ }
874
+ tokenRefresh?.schedule(sessionId, secret.session_ingress_token);
875
+ void handle.done.then(onSessionDone(sessionId, startTime, handle));
876
+ break;
877
+ }
878
+ default:
879
+ await ackWork();
880
+ // Gracefully ignore unknown work types. The backend may send new
881
+ // types before the bridge client is updated.
882
+ logForDebugging(`[bridge:work] Unknown work type: ${workType}, skipping`);
883
+ break;
884
+ }
885
+ // When at capacity, throttle the loop. The switch above still runs so
886
+ // existing-session token refreshes are processed, but we sleep here
887
+ // to avoid busy-looping. Include the capacity wake signal so the
888
+ // sleep is interrupted immediately when a session completes.
889
+ if (atCapacityBeforeSwitch) {
890
+ const cap = capacityWake.signal();
891
+ if (pollConfig.non_exclusive_heartbeat_interval_ms > 0) {
892
+ await heartbeatActiveWorkItems();
893
+ await sleep(pollConfig.non_exclusive_heartbeat_interval_ms, cap.signal);
894
+ }
895
+ else if (pollConfig.multisession_poll_interval_ms_at_capacity > 0) {
896
+ await sleep(pollConfig.multisession_poll_interval_ms_at_capacity, cap.signal);
897
+ }
898
+ cap.cleanup();
899
+ }
900
+ }
901
+ catch (err) {
902
+ if (loopSignal.aborted) {
903
+ break;
904
+ }
905
+ // Fatal errors (401/403) — no point retrying, auth won't fix itself
906
+ if (err instanceof BridgeFatalError) {
907
+ fatalExit = true;
908
+ // Server-enforced expiry gets a clean status message, not an error
909
+ if (isExpiredErrorType(err.errorType)) {
910
+ logger.logStatus(err.message);
911
+ }
912
+ else if (isSuppressible403(err)) {
913
+ // Cosmetic 403 errors (e.g., external_poll_sessions scope,
914
+ // environments:manage permission) — don't show to user
915
+ logForDebugging(`[bridge:work] Suppressed 403 error: ${err.message}`);
916
+ }
917
+ else {
918
+ logger.logError(err.message);
919
+ logError(err);
920
+ }
921
+ logEvent('thaddeus_bridge_fatal_error', {
922
+ status: err.status,
923
+ error_type: err.errorType,
924
+ });
925
+ logForDiagnosticsNoPII(isExpiredErrorType(err.errorType) ? 'info' : 'error', 'bridge_fatal_error', { status: err.status, error_type: err.errorType });
926
+ break;
927
+ }
928
+ const errMsg = describeAxiosError(err);
929
+ if (isConnectionError(err) || isServerError(err)) {
930
+ const now = Date.now();
931
+ // Detect system sleep/wake: if the gap since the last poll error
932
+ // greatly exceeds the expected backoff, the machine likely slept.
933
+ // Reset error tracking so the bridge retries with a fresh budget.
934
+ if (lastPollErrorTime !== null &&
935
+ now - lastPollErrorTime > pollSleepDetectionThresholdMs(backoffConfig)) {
936
+ logForDebugging(`[bridge:work] Detected system sleep (${Math.round((now - lastPollErrorTime) / 1000)}s gap), resetting error budget`);
937
+ logForDiagnosticsNoPII('info', 'bridge_poll_sleep_detected', {
938
+ gapMs: now - lastPollErrorTime,
939
+ });
940
+ connErrorStart = null;
941
+ connBackoff = 0;
942
+ generalErrorStart = null;
943
+ generalBackoff = 0;
944
+ }
945
+ lastPollErrorTime = now;
946
+ if (!connErrorStart) {
947
+ connErrorStart = now;
948
+ }
949
+ const elapsed = now - connErrorStart;
950
+ if (elapsed >= backoffConfig.connGiveUpMs) {
951
+ logger.logError(`Server unreachable for ${Math.round(elapsed / 60_000)} minutes, giving up.`);
952
+ logEvent('thaddeus_bridge_poll_give_up', {
953
+ error_type: 'connection',
954
+ elapsed_ms: elapsed,
955
+ });
956
+ logForDiagnosticsNoPII('error', 'bridge_poll_give_up', {
957
+ error_type: 'connection',
958
+ elapsed_ms: elapsed,
959
+ });
960
+ fatalExit = true;
961
+ break;
962
+ }
963
+ // Reset the other track when switching error types
964
+ generalErrorStart = null;
965
+ generalBackoff = 0;
966
+ connBackoff = connBackoff
967
+ ? Math.min(connBackoff * 2, backoffConfig.connCapMs)
968
+ : backoffConfig.connInitialMs;
969
+ const delay = addJitter(connBackoff);
970
+ logger.logVerbose(`Connection error, retrying in ${formatDelay(delay)} (${Math.round(elapsed / 1000)}s elapsed): ${errMsg}`);
971
+ logger.updateReconnectingStatus(formatDelay(delay), formatDuration(elapsed));
972
+ // The poll_due heartbeat-loop exit leaves a healthy lease exposed to
973
+ // this backoff path. Heartbeat before each sleep so /poll outages
974
+ // (the VerifyEnvironmentSecretAuth DB path heartbeat was introduced
975
+ // to avoid) don't kill the 300s lease TTL. No-op when activeSessions
976
+ // is empty or heartbeat is disabled.
977
+ if (getPollIntervalConfig().non_exclusive_heartbeat_interval_ms > 0) {
978
+ await heartbeatActiveWorkItems();
979
+ }
980
+ await sleep(delay, loopSignal);
981
+ }
982
+ else {
983
+ const now = Date.now();
984
+ // Sleep detection for general errors (same logic as connection errors)
985
+ if (lastPollErrorTime !== null &&
986
+ now - lastPollErrorTime > pollSleepDetectionThresholdMs(backoffConfig)) {
987
+ logForDebugging(`[bridge:work] Detected system sleep (${Math.round((now - lastPollErrorTime) / 1000)}s gap), resetting error budget`);
988
+ logForDiagnosticsNoPII('info', 'bridge_poll_sleep_detected', {
989
+ gapMs: now - lastPollErrorTime,
990
+ });
991
+ connErrorStart = null;
992
+ connBackoff = 0;
993
+ generalErrorStart = null;
994
+ generalBackoff = 0;
995
+ }
996
+ lastPollErrorTime = now;
997
+ if (!generalErrorStart) {
998
+ generalErrorStart = now;
999
+ }
1000
+ const elapsed = now - generalErrorStart;
1001
+ if (elapsed >= backoffConfig.generalGiveUpMs) {
1002
+ logger.logError(`Persistent errors for ${Math.round(elapsed / 60_000)} minutes, giving up.`);
1003
+ logEvent('thaddeus_bridge_poll_give_up', {
1004
+ error_type: 'general',
1005
+ elapsed_ms: elapsed,
1006
+ });
1007
+ logForDiagnosticsNoPII('error', 'bridge_poll_give_up', {
1008
+ error_type: 'general',
1009
+ elapsed_ms: elapsed,
1010
+ });
1011
+ fatalExit = true;
1012
+ break;
1013
+ }
1014
+ // Reset the other track when switching error types
1015
+ connErrorStart = null;
1016
+ connBackoff = 0;
1017
+ generalBackoff = generalBackoff
1018
+ ? Math.min(generalBackoff * 2, backoffConfig.generalCapMs)
1019
+ : backoffConfig.generalInitialMs;
1020
+ const delay = addJitter(generalBackoff);
1021
+ logger.logVerbose(`Poll failed, retrying in ${formatDelay(delay)} (${Math.round(elapsed / 1000)}s elapsed): ${errMsg}`);
1022
+ logger.updateReconnectingStatus(formatDelay(delay), formatDuration(elapsed));
1023
+ if (getPollIntervalConfig().non_exclusive_heartbeat_interval_ms > 0) {
1024
+ await heartbeatActiveWorkItems();
1025
+ }
1026
+ await sleep(delay, loopSignal);
1027
+ }
1028
+ }
1029
+ }
1030
+ // Clean up
1031
+ stopStatusUpdates();
1032
+ logger.clearStatus();
1033
+ const loopDurationMs = Date.now() - loopStartTime;
1034
+ logEvent('thaddeus_bridge_shutdown', {
1035
+ active_sessions: activeSessions.size,
1036
+ loop_duration_ms: loopDurationMs,
1037
+ });
1038
+ logForDiagnosticsNoPII('info', 'bridge_shutdown', {
1039
+ active_sessions: activeSessions.size,
1040
+ loop_duration_ms: loopDurationMs,
1041
+ });
1042
+ // Graceful shutdown: kill active sessions, report them as interrupted,
1043
+ // archive sessions, then deregister the environment so the web UI shows
1044
+ // the bridge as offline.
1045
+ // Collect all session IDs to archive on exit. This includes:
1046
+ // 1. Active sessions (snapshot before killing — onSessionDone clears maps)
1047
+ // 2. The initial auto-created session (may never have had work dispatched)
1048
+ // api.archiveSession is idempotent (409 if already archived), so
1049
+ // double-archiving is safe.
1050
+ const sessionsToArchive = new Set(activeSessions.keys());
1051
+ if (initialSessionId) {
1052
+ sessionsToArchive.add(initialSessionId);
1053
+ }
1054
+ // Snapshot before killing — onSessionDone clears sessionCompatIds.
1055
+ const compatIdSnapshot = new Map(sessionCompatIds);
1056
+ if (activeSessions.size > 0) {
1057
+ logForDebugging(`[bridge:shutdown] Shutting down ${activeSessions.size} active session(s)`);
1058
+ logger.logStatus(`Shutting down ${activeSessions.size} active session(s)\u2026`);
1059
+ // Snapshot work IDs before killing — onSessionDone clears the maps when
1060
+ // each child exits, so we need a copy for the stopWork calls below.
1061
+ const shutdownWorkIds = new Map(sessionWorkIds);
1062
+ for (const [sessionId, handle] of activeSessions.entries()) {
1063
+ logForDebugging(`[bridge:shutdown] Sending SIGTERM to sessionId=${sessionId}`);
1064
+ handle.kill();
1065
+ }
1066
+ const timeout = new AbortController();
1067
+ await Promise.race([
1068
+ Promise.allSettled([...activeSessions.values()].map(h => h.done)),
1069
+ sleep(backoffConfig.shutdownGraceMs ?? 30_000, timeout.signal),
1070
+ ]);
1071
+ timeout.abort();
1072
+ // SIGKILL any processes that didn't respond to SIGTERM within the grace window
1073
+ for (const [sid, handle] of activeSessions.entries()) {
1074
+ logForDebugging(`[bridge:shutdown] Force-killing stuck sessionId=${sid}`);
1075
+ handle.forceKill();
1076
+ }
1077
+ // Clear any remaining session timeout and refresh timers
1078
+ for (const timer of sessionTimers.values()) {
1079
+ clearTimeout(timer);
1080
+ }
1081
+ sessionTimers.clear();
1082
+ tokenRefresh?.cancelAll();
1083
+ // Clean up any remaining worktrees from active sessions.
1084
+ // Snapshot and clear the map first so onSessionDone (which may fire
1085
+ // during the await below when handle.done resolves) won't try to
1086
+ // remove the same worktrees again.
1087
+ if (sessionWorktrees.size > 0) {
1088
+ const remainingWorktrees = [...sessionWorktrees.values()];
1089
+ sessionWorktrees.clear();
1090
+ logForDebugging(`[bridge:shutdown] Cleaning up ${remainingWorktrees.length} worktree(s)`);
1091
+ await Promise.allSettled(remainingWorktrees.map(wt => removeAgentWorktree(wt.worktreePath, wt.worktreeBranch, wt.gitRoot, wt.hookBased)));
1092
+ }
1093
+ // Stop all active work items so the server knows they're done
1094
+ await Promise.allSettled([...shutdownWorkIds.entries()].map(([sessionId, workId]) => {
1095
+ return api
1096
+ .stopWork(environmentId, workId, true)
1097
+ .catch(err => logger.logVerbose(`Failed to stop work ${workId} for session ${sessionId}: ${errorMessage(err)}`));
1098
+ }));
1099
+ }
1100
+ // Ensure all in-flight cleanup (stopWork, worktree removal) from
1101
+ // onSessionDone completes before deregistering — otherwise
1102
+ // process.exit() can kill them mid-flight.
1103
+ if (pendingCleanups.size > 0) {
1104
+ await Promise.allSettled([...pendingCleanups]);
1105
+ }
1106
+ // In single-session mode with a known session, leave the session and
1107
+ // environment alive so `claude remote-control --session-id=<id>` can resume.
1108
+ // The backend GCs stale environments via a 4h TTL (BRIDGE_LAST_POLL_TTL).
1109
+ // Archiving the session or deregistering the environment would make the
1110
+ // printed resume command a lie — deregister deletes Firestore + Redis stream.
1111
+ // Skip when the loop exited fatally (env expired, auth failed, give-up) —
1112
+ // resume is impossible in those cases and the message would contradict the
1113
+ // error already printed.
1114
+ // feature('KAIROS') gate: --session-id is ant-only; without the gate,
1115
+ // revert to the pre-PR behavior (archive + deregister on every shutdown).
1116
+ if (feature('KAIROS') &&
1117
+ config.spawnMode === 'single-session' &&
1118
+ initialSessionId &&
1119
+ !fatalExit) {
1120
+ logger.logStatus(`Resume this session by running \`thaddeus remote-control --continue\``);
1121
+ logForDebugging(`[bridge:shutdown] Skipping archive+deregister to allow resume of session ${initialSessionId}`);
1122
+ return;
1123
+ }
1124
+ // Archive all known sessions so they don't linger as idle/running on the
1125
+ // server after the bridge goes offline.
1126
+ if (sessionsToArchive.size > 0) {
1127
+ logForDebugging(`[bridge:shutdown] Archiving ${sessionsToArchive.size} session(s)`);
1128
+ await Promise.allSettled([...sessionsToArchive].map(sessionId => api
1129
+ .archiveSession(compatIdSnapshot.get(sessionId) ?? toCompatSessionId(sessionId))
1130
+ .catch(err => logger.logVerbose(`Failed to archive session ${sessionId}: ${errorMessage(err)}`))));
1131
+ }
1132
+ // Deregister the environment so the web UI shows the bridge as offline
1133
+ // and the Redis stream is cleaned up.
1134
+ try {
1135
+ await api.deregisterEnvironment(environmentId);
1136
+ logForDebugging(`[bridge:shutdown] Environment deregistered, bridge offline`);
1137
+ logger.logVerbose('Environment deregistered.');
1138
+ }
1139
+ catch (err) {
1140
+ logger.logVerbose(`Failed to deregister environment: ${errorMessage(err)}`);
1141
+ }
1142
+ // Clear the crash-recovery pointer — the env is gone, pointer would be
1143
+ // stale. The early return above (resumable SIGINT shutdown) skips this,
1144
+ // leaving the pointer as a backup for the printed --session-id hint.
1145
+ const { clearBridgePointer } = await import('./bridgePointer.js');
1146
+ await clearBridgePointer(config.dir);
1147
+ logger.logVerbose('Environment offline.');
1148
+ }
1149
+ const CONNECTION_ERROR_CODES = new Set([
1150
+ 'ECONNREFUSED',
1151
+ 'ECONNRESET',
1152
+ 'ETIMEDOUT',
1153
+ 'ENETUNREACH',
1154
+ 'EHOSTUNREACH',
1155
+ ]);
1156
+ export function isConnectionError(err) {
1157
+ if (err &&
1158
+ typeof err === 'object' &&
1159
+ 'code' in err &&
1160
+ typeof err.code === 'string' &&
1161
+ CONNECTION_ERROR_CODES.has(err.code)) {
1162
+ return true;
1163
+ }
1164
+ return false;
1165
+ }
1166
+ /** Detect HTTP 5xx errors from axios (code: 'ERR_BAD_RESPONSE'). */
1167
+ export function isServerError(err) {
1168
+ return (!!err &&
1169
+ typeof err === 'object' &&
1170
+ 'code' in err &&
1171
+ typeof err.code === 'string' &&
1172
+ err.code === 'ERR_BAD_RESPONSE');
1173
+ }
1174
+ /** Add ±25% jitter to a delay value. */
1175
+ function addJitter(ms) {
1176
+ return Math.max(0, ms + ms * 0.25 * (2 * Math.random() - 1));
1177
+ }
1178
+ function formatDelay(ms) {
1179
+ return ms >= 1000 ? `${(ms / 1000).toFixed(1)}s` : `${Math.round(ms)}ms`;
1180
+ }
1181
+ /**
1182
+ * Retry stopWork with exponential backoff (3 attempts, 1s/2s/4s).
1183
+ * Ensures the server learns the work item ended, preventing server-side zombies.
1184
+ */
1185
+ async function stopWorkWithRetry(api, environmentId, workId, logger, baseDelayMs = 1000) {
1186
+ const MAX_ATTEMPTS = 3;
1187
+ for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
1188
+ try {
1189
+ await api.stopWork(environmentId, workId, false);
1190
+ logForDebugging(`[bridge:work] stopWork succeeded for workId=${workId} on attempt ${attempt}/${MAX_ATTEMPTS}`);
1191
+ return;
1192
+ }
1193
+ catch (err) {
1194
+ // Auth/permission errors won't be fixed by retrying
1195
+ if (err instanceof BridgeFatalError) {
1196
+ if (isSuppressible403(err)) {
1197
+ logForDebugging(`[bridge:work] Suppressed stopWork 403 for ${workId}: ${err.message}`);
1198
+ }
1199
+ else {
1200
+ logger.logError(`Failed to stop work ${workId}: ${err.message}`);
1201
+ }
1202
+ logForDiagnosticsNoPII('error', 'bridge_stop_work_failed', {
1203
+ attempts: attempt,
1204
+ fatal: true,
1205
+ });
1206
+ return;
1207
+ }
1208
+ const errMsg = errorMessage(err);
1209
+ if (attempt < MAX_ATTEMPTS) {
1210
+ const delay = addJitter(baseDelayMs * Math.pow(2, attempt - 1));
1211
+ logger.logVerbose(`Failed to stop work ${workId} (attempt ${attempt}/${MAX_ATTEMPTS}), retrying in ${formatDelay(delay)}: ${errMsg}`);
1212
+ await sleep(delay);
1213
+ }
1214
+ else {
1215
+ logger.logError(`Failed to stop work ${workId} after ${MAX_ATTEMPTS} attempts: ${errMsg}`);
1216
+ logForDiagnosticsNoPII('error', 'bridge_stop_work_failed', {
1217
+ attempts: MAX_ATTEMPTS,
1218
+ });
1219
+ }
1220
+ }
1221
+ }
1222
+ }
1223
+ function onSessionTimeout(sessionId, timeoutMs, logger, timedOutSessions, handle) {
1224
+ logForDebugging(`[bridge:session] sessionId=${sessionId} timed out after ${formatDuration(timeoutMs)}`);
1225
+ logEvent('thaddeus_bridge_session_timeout', {
1226
+ timeout_ms: timeoutMs,
1227
+ });
1228
+ logger.logSessionFailed(sessionId, `Session timed out after ${formatDuration(timeoutMs)}`);
1229
+ timedOutSessions.add(sessionId);
1230
+ handle.kill();
1231
+ }
1232
+ const SPAWN_FLAG_VALUES = ['session', 'same-dir', 'worktree'];
1233
+ function parseSpawnValue(raw) {
1234
+ if (raw === 'session')
1235
+ return 'single-session';
1236
+ if (raw === 'same-dir')
1237
+ return 'same-dir';
1238
+ if (raw === 'worktree')
1239
+ return 'worktree';
1240
+ return `--spawn requires one of: ${SPAWN_FLAG_VALUES.join(', ')} (got: ${raw ?? '<missing>'})`;
1241
+ }
1242
+ function parseCapacityValue(raw) {
1243
+ const n = raw === undefined ? NaN : parseInt(raw, 10);
1244
+ if (isNaN(n) || n < 1) {
1245
+ return `--capacity requires a positive integer (got: ${raw ?? '<missing>'})`;
1246
+ }
1247
+ return n;
1248
+ }
1249
+ export function parseArgs(args) {
1250
+ let verbose = false;
1251
+ let sandbox = false;
1252
+ let debugFile;
1253
+ let sessionTimeoutMs;
1254
+ let permissionMode;
1255
+ let name;
1256
+ let help = false;
1257
+ let spawnMode;
1258
+ let capacity;
1259
+ let createSessionInDir;
1260
+ let sessionId;
1261
+ let continueSession = false;
1262
+ for (let i = 0; i < args.length; i++) {
1263
+ const arg = args[i];
1264
+ if (arg === '--help' || arg === '-h') {
1265
+ help = true;
1266
+ }
1267
+ else if (arg === '--verbose' || arg === '-v') {
1268
+ verbose = true;
1269
+ }
1270
+ else if (arg === '--sandbox') {
1271
+ sandbox = true;
1272
+ }
1273
+ else if (arg === '--no-sandbox') {
1274
+ sandbox = false;
1275
+ }
1276
+ else if (arg === '--debug-file' && i + 1 < args.length) {
1277
+ debugFile = resolve(args[++i]);
1278
+ }
1279
+ else if (arg.startsWith('--debug-file=')) {
1280
+ debugFile = resolve(arg.slice('--debug-file='.length));
1281
+ }
1282
+ else if (arg === '--session-timeout' && i + 1 < args.length) {
1283
+ sessionTimeoutMs = parseInt(args[++i], 10) * 1000;
1284
+ }
1285
+ else if (arg.startsWith('--session-timeout=')) {
1286
+ sessionTimeoutMs =
1287
+ parseInt(arg.slice('--session-timeout='.length), 10) * 1000;
1288
+ }
1289
+ else if (arg === '--permission-mode' && i + 1 < args.length) {
1290
+ permissionMode = args[++i];
1291
+ }
1292
+ else if (arg.startsWith('--permission-mode=')) {
1293
+ permissionMode = arg.slice('--permission-mode='.length);
1294
+ }
1295
+ else if (arg === '--name' && i + 1 < args.length) {
1296
+ name = args[++i];
1297
+ }
1298
+ else if (arg.startsWith('--name=')) {
1299
+ name = arg.slice('--name='.length);
1300
+ }
1301
+ else if (feature('KAIROS') &&
1302
+ arg === '--session-id' &&
1303
+ i + 1 < args.length) {
1304
+ sessionId = args[++i];
1305
+ if (!sessionId) {
1306
+ return makeError('--session-id requires a value');
1307
+ }
1308
+ }
1309
+ else if (feature('KAIROS') && arg.startsWith('--session-id=')) {
1310
+ sessionId = arg.slice('--session-id='.length);
1311
+ if (!sessionId) {
1312
+ return makeError('--session-id requires a value');
1313
+ }
1314
+ }
1315
+ else if (feature('KAIROS') && (arg === '--continue' || arg === '-c')) {
1316
+ continueSession = true;
1317
+ }
1318
+ else if (arg === '--spawn' || arg.startsWith('--spawn=')) {
1319
+ if (spawnMode !== undefined) {
1320
+ return makeError('--spawn may only be specified once');
1321
+ }
1322
+ const raw = arg.startsWith('--spawn=')
1323
+ ? arg.slice('--spawn='.length)
1324
+ : args[++i];
1325
+ const v = parseSpawnValue(raw);
1326
+ if (v === 'single-session' || v === 'same-dir' || v === 'worktree') {
1327
+ spawnMode = v;
1328
+ }
1329
+ else {
1330
+ return makeError(v);
1331
+ }
1332
+ }
1333
+ else if (arg === '--capacity' || arg.startsWith('--capacity=')) {
1334
+ if (capacity !== undefined) {
1335
+ return makeError('--capacity may only be specified once');
1336
+ }
1337
+ const raw = arg.startsWith('--capacity=')
1338
+ ? arg.slice('--capacity='.length)
1339
+ : args[++i];
1340
+ const v = parseCapacityValue(raw);
1341
+ if (typeof v === 'number')
1342
+ capacity = v;
1343
+ else
1344
+ return makeError(v);
1345
+ }
1346
+ else if (arg === '--create-session-in-dir') {
1347
+ createSessionInDir = true;
1348
+ }
1349
+ else if (arg === '--no-create-session-in-dir') {
1350
+ createSessionInDir = false;
1351
+ }
1352
+ else {
1353
+ return makeError(`Unknown argument: ${arg}\nRun 'thaddeus remote-control --help' for usage.`);
1354
+ }
1355
+ }
1356
+ // Note: gate check for --spawn/--capacity/--create-session-in-dir is in bridgeMain
1357
+ // (gate-aware error). Flag cross-validation happens here.
1358
+ // --capacity only makes sense for multi-session modes.
1359
+ if (spawnMode === 'single-session' && capacity !== undefined) {
1360
+ return makeError(`--capacity cannot be used with --spawn=session (single-session mode has fixed capacity 1).`);
1361
+ }
1362
+ // --session-id / --continue resume a specific session on its original
1363
+ // environment; incompatible with spawn-related flags (which configure
1364
+ // fresh session creation), and mutually exclusive with each other.
1365
+ if ((sessionId || continueSession) &&
1366
+ (spawnMode !== undefined ||
1367
+ capacity !== undefined ||
1368
+ createSessionInDir !== undefined)) {
1369
+ return makeError(`--session-id and --continue cannot be used with --spawn, --capacity, or --create-session-in-dir.`);
1370
+ }
1371
+ if (sessionId && continueSession) {
1372
+ return makeError(`--session-id and --continue cannot be used together.`);
1373
+ }
1374
+ return {
1375
+ verbose,
1376
+ sandbox,
1377
+ debugFile,
1378
+ sessionTimeoutMs,
1379
+ permissionMode,
1380
+ name,
1381
+ spawnMode,
1382
+ capacity,
1383
+ createSessionInDir,
1384
+ sessionId,
1385
+ continueSession,
1386
+ help,
1387
+ };
1388
+ function makeError(error) {
1389
+ return {
1390
+ verbose,
1391
+ sandbox,
1392
+ debugFile,
1393
+ sessionTimeoutMs,
1394
+ permissionMode,
1395
+ name,
1396
+ spawnMode,
1397
+ capacity,
1398
+ createSessionInDir,
1399
+ sessionId,
1400
+ continueSession,
1401
+ help,
1402
+ error,
1403
+ };
1404
+ }
1405
+ }
1406
+ async function printHelp() {
1407
+ // Use EXTERNAL_PERMISSION_MODES for help text — internal modes (bubble)
1408
+ // are ant-only and auto is feature-gated; they're still accepted by validation.
1409
+ const { EXTERNAL_PERMISSION_MODES } = await import('../types/permissions.js');
1410
+ const modes = EXTERNAL_PERMISSION_MODES.join(', ');
1411
+ const showServer = await isMultiSessionSpawnEnabled();
1412
+ const serverOptions = showServer
1413
+ ? ` --spawn <mode> Spawn mode: same-dir, worktree, session
1414
+ (default: same-dir)
1415
+ --capacity <N> Max concurrent sessions in worktree or
1416
+ same-dir mode (default: ${SPAWN_SESSIONS_DEFAULT})
1417
+ --[no-]create-session-in-dir Pre-create a session in the current
1418
+ directory; in worktree mode this session
1419
+ stays in cwd while on-demand sessions get
1420
+ isolated worktrees (default: on)
1421
+ `
1422
+ : '';
1423
+ const serverDescription = showServer
1424
+ ? `
1425
+ Remote Control runs as a persistent server that accepts multiple concurrent
1426
+ sessions in the current directory. One session is pre-created on start so
1427
+ you have somewhere to type immediately. Use --spawn=worktree to isolate
1428
+ each on-demand session in its own git worktree, or --spawn=session for
1429
+ the classic single-session mode (exits when that session ends). Press 'w'
1430
+ during runtime to toggle between same-dir and worktree.
1431
+ `
1432
+ : '';
1433
+ const serverNote = showServer
1434
+ ? ` - Worktree mode requires a git repository or WorktreeCreate/WorktreeRemove hooks
1435
+ `
1436
+ : '';
1437
+ const help = `
1438
+ Remote Control - Connect your local environment to Thaddeus
1439
+
1440
+ USAGE
1441
+ thaddeus remote-control [options]
1442
+ OPTIONS
1443
+ --name <name> Name for the session (shown in Thaddeus web)
1444
+ ${feature('KAIROS')
1445
+ ? ` -c, --continue Resume the last session in this directory
1446
+ --session-id <id> Resume a specific session by ID (cannot be
1447
+ used with spawn flags or --continue)
1448
+ `
1449
+ : ''} --permission-mode <mode> Permission mode for spawned sessions
1450
+ (${modes})
1451
+ --debug-file <path> Write debug logs to file
1452
+ -v, --verbose Enable verbose output
1453
+ -h, --help Show this help
1454
+ ${serverOptions}
1455
+ DESCRIPTION
1456
+ Remote Control allows you to control sessions on your local device from
1457
+ Thaddeus web (https://delphilabsinc.com/thaddeus). Run this command in the
1458
+ directory you want to work in, then connect from the Thaddeus app or web.
1459
+ ${serverDescription}
1460
+ NOTES
1461
+ - You must be logged in with a Thaddeus account that has a subscription
1462
+ - Run \`thaddeus\` first in the directory to accept the workspace trust dialog
1463
+ ${serverNote}`;
1464
+ // biome-ignore lint/suspicious/noConsole: intentional help output
1465
+ console.log(help);
1466
+ }
1467
+ const TITLE_MAX_LEN = 80;
1468
+ /** Derive a session title from a user message: first line, truncated. */
1469
+ function deriveSessionTitle(text) {
1470
+ // Collapse whitespace — newlines/tabs would break the single-line status display.
1471
+ const flat = text.replace(/\s+/g, ' ').trim();
1472
+ return truncateToWidth(flat, TITLE_MAX_LEN);
1473
+ }
1474
+ /**
1475
+ * One-shot fetch of a session's title via GET /v1/sessions/{id}.
1476
+ *
1477
+ * Uses `getBridgeSession` from createSession.ts (ccr-byoc headers + org UUID)
1478
+ * rather than the environments-level bridgeApi client, whose headers make the
1479
+ * Sessions API return 404. Returns undefined if the session has no title yet
1480
+ * or the fetch fails — the caller falls back to deriving a title from the
1481
+ * first user message.
1482
+ */
1483
+ async function fetchSessionTitle(compatSessionId, baseUrl) {
1484
+ const { getBridgeSession } = await import('./createSession.js');
1485
+ const session = await getBridgeSession(compatSessionId, { baseUrl });
1486
+ return session?.title || undefined;
1487
+ }
1488
+ export async function bridgeMain(args) {
1489
+ const parsed = parseArgs(args);
1490
+ if (parsed.help) {
1491
+ await printHelp();
1492
+ return;
1493
+ }
1494
+ if (parsed.error) {
1495
+ // biome-ignore lint/suspicious/noConsole: intentional error output
1496
+ console.error(`Error: ${parsed.error}`);
1497
+ // eslint-disable-next-line custom-rules/no-process-exit
1498
+ process.exit(1);
1499
+ }
1500
+ const { verbose, sandbox, debugFile, sessionTimeoutMs, permissionMode, name, spawnMode: parsedSpawnMode, capacity: parsedCapacity, createSessionInDir: parsedCreateSessionInDir, sessionId: parsedSessionId, continueSession, } = parsed;
1501
+ // Mutable so --continue can set it from the pointer file. The #20460
1502
+ // resume flow below then treats it the same as an explicit --session-id.
1503
+ let resumeSessionId = parsedSessionId;
1504
+ // When --continue found a pointer, this is the directory it came from
1505
+ // (may be a worktree sibling, not `dir`). On resume-flow deterministic
1506
+ // failure, clear THIS file so --continue doesn't keep hitting the same
1507
+ // dead session. Undefined for explicit --session-id (leaves pointer alone).
1508
+ let resumePointerDir;
1509
+ const usedMultiSessionFeature = parsedSpawnMode !== undefined ||
1510
+ parsedCapacity !== undefined ||
1511
+ parsedCreateSessionInDir !== undefined;
1512
+ // Validate permission mode early so the user gets an error before
1513
+ // the bridge starts polling for work.
1514
+ if (permissionMode !== undefined) {
1515
+ const { PERMISSION_MODES } = await import('../types/permissions.js');
1516
+ const valid = PERMISSION_MODES;
1517
+ if (!valid.includes(permissionMode)) {
1518
+ // biome-ignore lint/suspicious/noConsole: intentional error output
1519
+ console.error(`Error: Invalid permission mode '${permissionMode}'. Valid modes: ${valid.join(', ')}`);
1520
+ // eslint-disable-next-line custom-rules/no-process-exit
1521
+ process.exit(1);
1522
+ }
1523
+ }
1524
+ const dir = resolve('.');
1525
+ // The bridge fast-path bypasses init.ts, so we must enable config reading
1526
+ // before any code that transitively calls getGlobalConfig()
1527
+ const { enableConfigs, checkHasTrustDialogAccepted } = await import('../utils/config.js');
1528
+ enableConfigs();
1529
+ // Initialize analytics and error reporting sinks. The bridge bypasses the
1530
+ // setup() init flow, so we call initSinks() directly to attach sinks here.
1531
+ const { initSinks } = await import('../utils/sinks.js');
1532
+ initSinks();
1533
+ // Gate-aware validation: --spawn / --capacity / --create-session-in-dir require
1534
+ // the multi-session gate. parseArgs has already validated flag combinations;
1535
+ // here we only check the gate since that requires an async GrowthBook call.
1536
+ // Runs after enableConfigs() (GrowthBook cache reads global config) and after
1537
+ // initSinks() so the denial event can be enqueued.
1538
+ const multiSessionEnabled = await isMultiSessionSpawnEnabled();
1539
+ if (usedMultiSessionFeature && !multiSessionEnabled) {
1540
+ await logEventAsync('thaddeus_bridge_multi_session_denied', {
1541
+ used_spawn: parsedSpawnMode !== undefined,
1542
+ used_capacity: parsedCapacity !== undefined,
1543
+ used_create_session_in_dir: parsedCreateSessionInDir !== undefined,
1544
+ });
1545
+ // logEventAsync only enqueues — process.exit() discards buffered events.
1546
+ // Flush explicitly, capped at 500ms to match gracefulShutdown.ts.
1547
+ // (sleep() doesn't unref its timer, but process.exit() follows immediately
1548
+ // so the ref'd timer can't delay shutdown.)
1549
+ await Promise.race([
1550
+ Promise.all([shutdown1PEventLogging(), shutdownDatadog()]),
1551
+ sleep(500, undefined, { unref: true }),
1552
+ ]).catch(() => { });
1553
+ // biome-ignore lint/suspicious/noConsole: intentional error output
1554
+ console.error('Error: Multi-session Remote Control is not enabled for your account yet.');
1555
+ // eslint-disable-next-line custom-rules/no-process-exit
1556
+ process.exit(1);
1557
+ }
1558
+ // Set the bootstrap CWD so that trust checks, project config lookups, and
1559
+ // git utilities (getBranch, getRemoteUrl) resolve against the correct path.
1560
+ const { setOriginalCwd, setCwdState } = await import('../bootstrap/state.js');
1561
+ setOriginalCwd(dir);
1562
+ setCwdState(dir);
1563
+ // The bridge bypasses main.tsx (which renders the interactive TrustDialog via showSetupScreens),
1564
+ // so we must verify trust was previously established by a normal `claude` session.
1565
+ if (!checkHasTrustDialogAccepted()) {
1566
+ // biome-ignore lint/suspicious/noConsole:: intentional console output
1567
+ console.error(`Error: Workspace not trusted. Please run \`thaddeus\` in ${dir} first to review and accept the workspace trust dialog.`);
1568
+ // eslint-disable-next-line custom-rules/no-process-exit
1569
+ process.exit(1);
1570
+ }
1571
+ // Resolve auth
1572
+ const { clearOAuthTokenCache, checkAndRefreshOAuthTokenIfNeeded } = await import('../utils/auth.js');
1573
+ const { getBridgeAccessToken, getBridgeBaseUrl } = await import('./bridgeConfig.js');
1574
+ const bridgeToken = getBridgeAccessToken();
1575
+ if (!bridgeToken) {
1576
+ // biome-ignore lint/suspicious/noConsole:: intentional console output
1577
+ console.error(BRIDGE_LOGIN_ERROR);
1578
+ // eslint-disable-next-line custom-rules/no-process-exit
1579
+ process.exit(1);
1580
+ }
1581
+ // First-time remote dialog — explain what bridge does and get consent
1582
+ const { getGlobalConfig, saveGlobalConfig, getCurrentProjectConfig, saveCurrentProjectConfig, } = await import('../utils/config.js');
1583
+ if (!getGlobalConfig().remoteDialogSeen) {
1584
+ const readline = await import('readline');
1585
+ const rl = readline.createInterface({
1586
+ input: process.stdin,
1587
+ output: process.stdout,
1588
+ });
1589
+ // biome-ignore lint/suspicious/noConsole:: intentional console output
1590
+ console.log('\nRemote Control lets you access this CLI session from the web (delphilabsinc.com/thaddeus)\nor the Thaddeus app, so you can pick up where you left off on any device.\n\nYou can disconnect remote access anytime by running /remote-control again.\n');
1591
+ const answer = await new Promise(resolve => {
1592
+ rl.question('Enable Remote Control? (y/n) ', resolve);
1593
+ });
1594
+ rl.close();
1595
+ saveGlobalConfig(current => {
1596
+ if (current.remoteDialogSeen)
1597
+ return current;
1598
+ return { ...current, remoteDialogSeen: true };
1599
+ });
1600
+ if (answer.toLowerCase() !== 'y' && answer.toLowerCase() !== 'yes') {
1601
+ // eslint-disable-next-line custom-rules/no-process-exit
1602
+ process.exit(0);
1603
+ }
1604
+ }
1605
+ // --continue: resolve the most recent session from the crash-recovery
1606
+ // pointer and chain into the #20460 --session-id flow. Worktree-aware:
1607
+ // checks current dir first (fast path, zero exec), then fans out to git
1608
+ // worktree siblings if that misses — the REPL bridge writes to
1609
+ // getOriginalCwd() which EnterWorktreeTool/activeWorktreeSession can
1610
+ // point at a worktree while the user's shell is at the repo root.
1611
+ // KAIROS-gated at parseArgs — continueSession is always false in external
1612
+ // builds, so this block tree-shakes.
1613
+ if (feature('KAIROS') && continueSession) {
1614
+ const { readBridgePointerAcrossWorktrees } = await import('./bridgePointer.js');
1615
+ const found = await readBridgePointerAcrossWorktrees(dir);
1616
+ if (!found) {
1617
+ // biome-ignore lint/suspicious/noConsole: intentional error output
1618
+ console.error(`Error: No recent session found in this directory or its worktrees. Run \`claude remote-control\` to start a new one.`);
1619
+ // eslint-disable-next-line custom-rules/no-process-exit
1620
+ process.exit(1);
1621
+ }
1622
+ const { pointer, dir: pointerDir } = found;
1623
+ const ageMin = Math.round(pointer.ageMs / 60_000);
1624
+ const ageStr = ageMin < 60 ? `${ageMin}m` : `${Math.round(ageMin / 60)}h`;
1625
+ const fromWt = pointerDir !== dir ? ` from worktree ${pointerDir}` : '';
1626
+ // biome-ignore lint/suspicious/noConsole: intentional info output
1627
+ console.error(`Resuming session ${pointer.sessionId} (${ageStr} ago)${fromWt}\u2026`);
1628
+ resumeSessionId = pointer.sessionId;
1629
+ // Track where the pointer came from so the #20460 exit(1) paths below
1630
+ // clear the RIGHT file on deterministic failure — otherwise --continue
1631
+ // would keep hitting the same dead session. May be a worktree sibling.
1632
+ resumePointerDir = pointerDir;
1633
+ }
1634
+ // In production, baseUrl is the Anthropic API (from OAuth config).
1635
+ // CLAUDE_BRIDGE_BASE_URL overrides this for ant local dev only.
1636
+ const baseUrl = getBridgeBaseUrl();
1637
+ // For non-localhost targets, require HTTPS to protect credentials.
1638
+ if (baseUrl.startsWith('http://') &&
1639
+ !baseUrl.includes('localhost') &&
1640
+ !baseUrl.includes('127.0.0.1')) {
1641
+ // biome-ignore lint/suspicious/noConsole:: intentional console output
1642
+ console.error('Error: Remote Control base URL uses HTTP. Only HTTPS or localhost HTTP is allowed.');
1643
+ // eslint-disable-next-line custom-rules/no-process-exit
1644
+ process.exit(1);
1645
+ }
1646
+ // Session ingress URL for WebSocket connections. In production this is the
1647
+ // same as baseUrl (Envoy routes /v1/session_ingress/* to session-ingress).
1648
+ // Locally, session-ingress runs on a different port (9413) than the
1649
+ // contain-provide-api (8211), so CLAUDE_BRIDGE_SESSION_INGRESS_URL must be
1650
+ // set explicitly. Ant-only, matching CLAUDE_BRIDGE_BASE_URL.
1651
+ const sessionIngressUrl = process.env.USER_TYPE === 'ant' &&
1652
+ process.env.CLAUDE_BRIDGE_SESSION_INGRESS_URL
1653
+ ? process.env.CLAUDE_BRIDGE_SESSION_INGRESS_URL
1654
+ : baseUrl;
1655
+ const { getBranch, getRemoteUrl, findGitRoot } = await import('../utils/git.js');
1656
+ // Precheck worktree availability for the first-run dialog and the `w`
1657
+ // toggle. Unconditional so we know upfront whether worktree is an option.
1658
+ const { hasWorktreeCreateHook } = await import('../utils/hooks.js');
1659
+ const worktreeAvailable = hasWorktreeCreateHook() || findGitRoot(dir) !== null;
1660
+ // Load saved per-project spawn-mode preference. Gated by multiSessionEnabled
1661
+ // so a GrowthBook rollback cleanly reverts users to single-session —
1662
+ // otherwise a saved pref would silently re-enable multi-session behavior
1663
+ // (worktree isolation, 32 max sessions, w toggle) despite the gate being off.
1664
+ // Also guard against a stale worktree pref left over from when this dir WAS
1665
+ // a git repo (or the user copied config) — clear it on disk so the warning
1666
+ // doesn't repeat on every launch.
1667
+ let savedSpawnMode = multiSessionEnabled
1668
+ ? getCurrentProjectConfig().remoteControlSpawnMode
1669
+ : undefined;
1670
+ if (savedSpawnMode === 'worktree' && !worktreeAvailable) {
1671
+ // biome-ignore lint/suspicious/noConsole: intentional warning output
1672
+ console.error('Warning: Saved spawn mode is worktree but this directory is not a git repository. Falling back to same-dir.');
1673
+ savedSpawnMode = undefined;
1674
+ saveCurrentProjectConfig(current => {
1675
+ if (current.remoteControlSpawnMode === undefined)
1676
+ return current;
1677
+ return { ...current, remoteControlSpawnMode: undefined };
1678
+ });
1679
+ }
1680
+ // First-run spawn-mode choice: ask once per project when the choice is
1681
+ // meaningful (gate on, both modes available, no explicit override, not
1682
+ // resuming). Saves to ProjectConfig so subsequent runs skip this.
1683
+ if (multiSessionEnabled &&
1684
+ !savedSpawnMode &&
1685
+ worktreeAvailable &&
1686
+ parsedSpawnMode === undefined &&
1687
+ !resumeSessionId &&
1688
+ process.stdin.isTTY) {
1689
+ const readline = await import('readline');
1690
+ const rl = readline.createInterface({
1691
+ input: process.stdin,
1692
+ output: process.stdout,
1693
+ });
1694
+ // biome-ignore lint/suspicious/noConsole: intentional dialog output
1695
+ console.log(`\nThaddeus Remote Control is launching in spawn mode which lets you create new sessions in this project from Thaddeus on Web or your Mobile app. Learn more here: https://delphilabsinc.com/docs/en/remote-control\n\n` +
1696
+ `Spawn mode for this project:\n` +
1697
+ ` [1] same-dir \u2014 sessions share the current directory (default)\n` +
1698
+ ` [2] worktree \u2014 each session gets an isolated git worktree\n\n` +
1699
+ `This can be changed later or explicitly set with --spawn=same-dir or --spawn=worktree.\n`);
1700
+ const answer = await new Promise(resolve => {
1701
+ rl.question('Choose [1/2] (default: 1): ', resolve);
1702
+ });
1703
+ rl.close();
1704
+ const chosen = answer.trim() === '2' ? 'worktree' : 'same-dir';
1705
+ savedSpawnMode = chosen;
1706
+ logEvent('thaddeus_bridge_spawn_mode_chosen', {
1707
+ spawn_mode: chosen,
1708
+ });
1709
+ saveCurrentProjectConfig(current => {
1710
+ if (current.remoteControlSpawnMode === chosen)
1711
+ return current;
1712
+ return { ...current, remoteControlSpawnMode: chosen };
1713
+ });
1714
+ }
1715
+ let spawnModeSource;
1716
+ let spawnMode;
1717
+ if (resumeSessionId) {
1718
+ spawnMode = 'single-session';
1719
+ spawnModeSource = 'resume';
1720
+ }
1721
+ else if (parsedSpawnMode !== undefined) {
1722
+ spawnMode = parsedSpawnMode;
1723
+ spawnModeSource = 'flag';
1724
+ }
1725
+ else if (savedSpawnMode !== undefined) {
1726
+ spawnMode = savedSpawnMode;
1727
+ spawnModeSource = 'saved';
1728
+ }
1729
+ else {
1730
+ spawnMode = multiSessionEnabled ? 'same-dir' : 'single-session';
1731
+ spawnModeSource = 'gate_default';
1732
+ }
1733
+ const maxSessions = spawnMode === 'single-session'
1734
+ ? 1
1735
+ : (parsedCapacity ?? SPAWN_SESSIONS_DEFAULT);
1736
+ // Pre-create an empty session on start so the user has somewhere to type
1737
+ // immediately, running in the current directory (exempted from worktree
1738
+ // creation in the spawn loop). On by default; --no-create-session-in-dir
1739
+ // opts out for a pure on-demand server where every session is isolated.
1740
+ // The effectiveResumeSessionId guard at the creation site handles the
1741
+ // resume case (skip creation when resume succeeded; fall through to
1742
+ // fresh creation on env-mismatch fallback).
1743
+ const preCreateSession = parsedCreateSessionInDir ?? true;
1744
+ // Without --continue: a leftover pointer means the previous run didn't
1745
+ // shut down cleanly (crash, kill -9, terminal closed). Clear it so the
1746
+ // stale env doesn't linger past its relevance. Runs in all modes
1747
+ // (clearBridgePointer is a no-op when no file exists) — covers the
1748
+ // gate-transition case where a user crashed in single-session mode then
1749
+ // starts fresh in worktree mode. Only single-session mode writes new
1750
+ // pointers.
1751
+ if (!resumeSessionId) {
1752
+ const { clearBridgePointer } = await import('./bridgePointer.js');
1753
+ await clearBridgePointer(dir);
1754
+ }
1755
+ // Worktree mode requires either git or WorktreeCreate/WorktreeRemove hooks.
1756
+ // Only reachable via explicit --spawn=worktree (default is same-dir);
1757
+ // saved worktree pref was already guarded above.
1758
+ if (spawnMode === 'worktree' && !worktreeAvailable) {
1759
+ // biome-ignore lint/suspicious/noConsole: intentional error output
1760
+ console.error(`Error: Worktree mode requires a git repository or WorktreeCreate hooks configured. Use --spawn=session for single-session mode.`);
1761
+ // eslint-disable-next-line custom-rules/no-process-exit
1762
+ process.exit(1);
1763
+ }
1764
+ const branch = await getBranch();
1765
+ const gitRepoUrl = await getRemoteUrl();
1766
+ const machineName = hostname();
1767
+ const bridgeId = randomUUID();
1768
+ const { handleOAuth401Error } = await import('../utils/auth.js');
1769
+ const api = createBridgeApiClient({
1770
+ baseUrl,
1771
+ getAccessToken: getBridgeAccessToken,
1772
+ runnerVersion: MACRO.VERSION,
1773
+ onDebug: logForDebugging,
1774
+ onAuth401: handleOAuth401Error,
1775
+ getTrustedDeviceToken,
1776
+ });
1777
+ // When resuming a session via --session-id, fetch it to learn its
1778
+ // environment_id and reuse that for registration (idempotent on the
1779
+ // backend). Left undefined otherwise — the backend rejects
1780
+ // client-generated UUIDs and will allocate a fresh environment.
1781
+ // feature('KAIROS') gate: --session-id is ant-only; parseArgs already
1782
+ // rejects the flag when the gate is off, so resumeSessionId is always
1783
+ // undefined here in external builds — this guard is for tree-shaking.
1784
+ let reuseEnvironmentId;
1785
+ if (feature('KAIROS') && resumeSessionId) {
1786
+ try {
1787
+ validateBridgeId(resumeSessionId, 'sessionId');
1788
+ }
1789
+ catch {
1790
+ // biome-ignore lint/suspicious/noConsole: intentional error output
1791
+ console.error(`Error: Invalid session ID "${resumeSessionId}". Session IDs must not contain unsafe characters.`);
1792
+ // eslint-disable-next-line custom-rules/no-process-exit
1793
+ process.exit(1);
1794
+ }
1795
+ // Proactively refresh the OAuth token — getBridgeSession uses raw axios
1796
+ // without the withOAuthRetry 401-refresh logic. An expired-but-present
1797
+ // token would otherwise produce a misleading "not found" error.
1798
+ await checkAndRefreshOAuthTokenIfNeeded();
1799
+ clearOAuthTokenCache();
1800
+ const { getBridgeSession } = await import('./createSession.js');
1801
+ const session = await getBridgeSession(resumeSessionId, {
1802
+ baseUrl,
1803
+ getAccessToken: getBridgeAccessToken,
1804
+ });
1805
+ if (!session) {
1806
+ // Session gone on server → pointer is stale. Clear it so the user
1807
+ // isn't re-prompted next launch. (Explicit --session-id leaves the
1808
+ // pointer alone — it's an independent file they may not even have.)
1809
+ // resumePointerDir may be a worktree sibling — clear THAT file.
1810
+ if (resumePointerDir) {
1811
+ const { clearBridgePointer } = await import('./bridgePointer.js');
1812
+ await clearBridgePointer(resumePointerDir);
1813
+ }
1814
+ // biome-ignore lint/suspicious/noConsole: intentional error output
1815
+ console.error(`Error: Session ${resumeSessionId} not found. It may have been archived or expired, or your login may have lapsed (run \`thaddeus /login\`).`);
1816
+ // eslint-disable-next-line custom-rules/no-process-exit
1817
+ process.exit(1);
1818
+ }
1819
+ if (!session.environment_id) {
1820
+ if (resumePointerDir) {
1821
+ const { clearBridgePointer } = await import('./bridgePointer.js');
1822
+ await clearBridgePointer(resumePointerDir);
1823
+ }
1824
+ // biome-ignore lint/suspicious/noConsole: intentional error output
1825
+ console.error(`Error: Session ${resumeSessionId} has no environment_id. It may never have been attached to a bridge.`);
1826
+ // eslint-disable-next-line custom-rules/no-process-exit
1827
+ process.exit(1);
1828
+ }
1829
+ reuseEnvironmentId = session.environment_id;
1830
+ logForDebugging(`[bridge:init] Resuming session ${resumeSessionId} on environment ${reuseEnvironmentId}`);
1831
+ }
1832
+ const config = {
1833
+ dir,
1834
+ machineName,
1835
+ branch,
1836
+ gitRepoUrl,
1837
+ maxSessions,
1838
+ spawnMode,
1839
+ verbose,
1840
+ sandbox,
1841
+ bridgeId,
1842
+ workerType: 'thaddeus',
1843
+ environmentId: randomUUID(),
1844
+ reuseEnvironmentId,
1845
+ apiBaseUrl: baseUrl,
1846
+ sessionIngressUrl,
1847
+ debugFile,
1848
+ sessionTimeoutMs,
1849
+ };
1850
+ logForDebugging(`[bridge:init] bridgeId=${bridgeId}${reuseEnvironmentId ? ` reuseEnvironmentId=${reuseEnvironmentId}` : ''} dir=${dir} branch=${branch} gitRepoUrl=${gitRepoUrl} machine=${machineName}`);
1851
+ logForDebugging(`[bridge:init] apiBaseUrl=${baseUrl} sessionIngressUrl=${sessionIngressUrl}`);
1852
+ logForDebugging(`[bridge:init] sandbox=${sandbox}${debugFile ? ` debugFile=${debugFile}` : ''}`);
1853
+ // Register the bridge environment before entering the poll loop.
1854
+ let environmentId;
1855
+ let environmentSecret;
1856
+ try {
1857
+ const reg = await api.registerBridgeEnvironment(config);
1858
+ environmentId = reg.environment_id;
1859
+ environmentSecret = reg.environment_secret;
1860
+ }
1861
+ catch (err) {
1862
+ logEvent('thaddeus_bridge_registration_failed', {
1863
+ status: err instanceof BridgeFatalError ? err.status : undefined,
1864
+ });
1865
+ // Registration failures are fatal — print a clean message instead of a stack trace.
1866
+ // biome-ignore lint/suspicious/noConsole:: intentional console output
1867
+ console.error(err instanceof BridgeFatalError && err.status === 404
1868
+ ? 'Remote Control environments are not available for your account.'
1869
+ : `Error: ${errorMessage(err)}`);
1870
+ // eslint-disable-next-line custom-rules/no-process-exit
1871
+ process.exit(1);
1872
+ }
1873
+ // Tracks whether the --session-id resume flow completed successfully.
1874
+ // Used below to skip fresh session creation and seed initialSessionId.
1875
+ // Cleared on env mismatch so we gracefully fall back to a new session.
1876
+ let effectiveResumeSessionId;
1877
+ if (feature('KAIROS') && resumeSessionId) {
1878
+ if (reuseEnvironmentId && environmentId !== reuseEnvironmentId) {
1879
+ // Backend returned a different environment_id — the original env
1880
+ // expired or was reaped. Reconnect won't work against the new env
1881
+ // (session is bound to the old one). Log to sentry for visibility
1882
+ // and fall through to fresh session creation on the new env.
1883
+ logError(new Error(`Bridge resume env mismatch: requested ${reuseEnvironmentId}, backend returned ${environmentId}. Falling back to fresh session.`));
1884
+ // biome-ignore lint/suspicious/noConsole: intentional warning output
1885
+ console.warn(`Warning: Could not resume session ${resumeSessionId} — its environment has expired. Creating a fresh session instead.`);
1886
+ // Don't deregister — we're going to use this new environment.
1887
+ // effectiveResumeSessionId stays undefined → fresh session path below.
1888
+ }
1889
+ else {
1890
+ // Force-stop any stale worker instances for this session and re-queue
1891
+ // it so our poll loop picks it up. Must happen after registration so
1892
+ // the backend knows a live worker exists for the environment.
1893
+ //
1894
+ // The pointer stores a session_* ID but /bridge/reconnect looks
1895
+ // sessions up by their infra tag (cse_*) when ccr_v2_compat_enabled
1896
+ // is on. Try both; the conversion is a no-op if already cse_*.
1897
+ const infraResumeId = toInfraSessionId(resumeSessionId);
1898
+ const reconnectCandidates = infraResumeId === resumeSessionId
1899
+ ? [resumeSessionId]
1900
+ : [resumeSessionId, infraResumeId];
1901
+ let reconnected = false;
1902
+ let lastReconnectErr;
1903
+ for (const candidateId of reconnectCandidates) {
1904
+ try {
1905
+ await api.reconnectSession(environmentId, candidateId);
1906
+ logForDebugging(`[bridge:init] Session ${candidateId} re-queued via bridge/reconnect`);
1907
+ effectiveResumeSessionId = resumeSessionId;
1908
+ reconnected = true;
1909
+ break;
1910
+ }
1911
+ catch (err) {
1912
+ lastReconnectErr = err;
1913
+ logForDebugging(`[bridge:init] reconnectSession(${candidateId}) failed: ${errorMessage(err)}`);
1914
+ }
1915
+ }
1916
+ if (!reconnected) {
1917
+ const err = lastReconnectErr;
1918
+ // Do NOT deregister on transient reconnect failure — at this point
1919
+ // environmentId IS the session's own environment. Deregistering
1920
+ // would make retry impossible. The backend's 4h TTL cleans up.
1921
+ const isFatal = err instanceof BridgeFatalError;
1922
+ // Clear pointer only on fatal reconnect failure. Transient failures
1923
+ // ("try running the same command again") should keep the pointer so
1924
+ // next launch re-prompts — that IS the retry mechanism.
1925
+ if (resumePointerDir && isFatal) {
1926
+ const { clearBridgePointer } = await import('./bridgePointer.js');
1927
+ await clearBridgePointer(resumePointerDir);
1928
+ }
1929
+ // biome-ignore lint/suspicious/noConsole: intentional error output
1930
+ console.error(isFatal
1931
+ ? `Error: ${errorMessage(err)}`
1932
+ : `Error: Failed to reconnect session ${resumeSessionId}: ${errorMessage(err)}\nThe session may still be resumable — try running the same command again.`);
1933
+ // eslint-disable-next-line custom-rules/no-process-exit
1934
+ process.exit(1);
1935
+ }
1936
+ }
1937
+ }
1938
+ logForDebugging(`[bridge:init] Registered, server environmentId=${environmentId}`);
1939
+ const startupPollConfig = getPollIntervalConfig();
1940
+ logEvent('thaddeus_bridge_started', {
1941
+ max_sessions: config.maxSessions,
1942
+ has_debug_file: !!config.debugFile,
1943
+ sandbox: config.sandbox,
1944
+ verbose: config.verbose,
1945
+ heartbeat_interval_ms: startupPollConfig.non_exclusive_heartbeat_interval_ms,
1946
+ spawn_mode: config.spawnMode,
1947
+ spawn_mode_source: spawnModeSource,
1948
+ multi_session_gate: multiSessionEnabled,
1949
+ pre_create_session: preCreateSession,
1950
+ worktree_available: worktreeAvailable,
1951
+ });
1952
+ logForDiagnosticsNoPII('info', 'bridge_started', {
1953
+ max_sessions: config.maxSessions,
1954
+ sandbox: config.sandbox,
1955
+ spawn_mode: config.spawnMode,
1956
+ });
1957
+ const spawner = createSessionSpawner({
1958
+ execPath: process.execPath,
1959
+ scriptArgs: spawnScriptArgs(),
1960
+ env: process.env,
1961
+ verbose,
1962
+ sandbox,
1963
+ debugFile,
1964
+ permissionMode,
1965
+ onDebug: logForDebugging,
1966
+ onActivity: (sessionId, activity) => {
1967
+ logForDebugging(`[bridge:activity] sessionId=${sessionId} ${activity.type} ${activity.summary}`);
1968
+ },
1969
+ onPermissionRequest: (sessionId, request, _accessToken) => {
1970
+ logForDebugging(`[bridge:perm] sessionId=${sessionId} tool=${request.request.tool_name} request_id=${request.request_id} (not auto-approving)`);
1971
+ },
1972
+ });
1973
+ const logger = createBridgeLogger({ verbose });
1974
+ const { parseGitHubRepository } = await import('../utils/detectRepository.js');
1975
+ const ownerRepo = gitRepoUrl ? parseGitHubRepository(gitRepoUrl) : null;
1976
+ // Use the repo name from the parsed owner/repo, or fall back to the dir basename
1977
+ const repoName = ownerRepo ? ownerRepo.split('/').pop() : basename(dir);
1978
+ logger.setRepoInfo(repoName, branch);
1979
+ // `w` toggle is available iff we're in a multi-session mode AND worktree
1980
+ // is a valid option. When unavailable, the mode suffix and hint are hidden.
1981
+ const toggleAvailable = spawnMode !== 'single-session' && worktreeAvailable;
1982
+ if (toggleAvailable) {
1983
+ // Safe cast: spawnMode is not single-session (checked above), and the
1984
+ // saved-worktree-in-non-git guard + exit check above ensure worktree
1985
+ // is only reached when available.
1986
+ logger.setSpawnModeDisplay(spawnMode);
1987
+ }
1988
+ // Listen for keys: space toggles QR code, w toggles spawn mode
1989
+ const onStdinData = (data) => {
1990
+ if (data[0] === 0x03 || data[0] === 0x04) {
1991
+ // Ctrl+C / Ctrl+D — trigger graceful shutdown
1992
+ process.emit('SIGINT');
1993
+ return;
1994
+ }
1995
+ if (data[0] === 0x20 /* space */) {
1996
+ logger.toggleQr();
1997
+ return;
1998
+ }
1999
+ if (data[0] === 0x77 /* 'w' */) {
2000
+ if (!toggleAvailable)
2001
+ return;
2002
+ const newMode = config.spawnMode === 'same-dir' ? 'worktree' : 'same-dir';
2003
+ config.spawnMode = newMode;
2004
+ logEvent('thaddeus_bridge_spawn_mode_toggled', {
2005
+ spawn_mode: newMode,
2006
+ });
2007
+ logger.logStatus(newMode === 'worktree'
2008
+ ? 'Spawn mode: worktree (new sessions get isolated git worktrees)'
2009
+ : 'Spawn mode: same-dir (new sessions share the current directory)');
2010
+ logger.setSpawnModeDisplay(newMode);
2011
+ logger.refreshDisplay();
2012
+ saveCurrentProjectConfig(current => {
2013
+ if (current.remoteControlSpawnMode === newMode)
2014
+ return current;
2015
+ return { ...current, remoteControlSpawnMode: newMode };
2016
+ });
2017
+ return;
2018
+ }
2019
+ };
2020
+ if (process.stdin.isTTY) {
2021
+ process.stdin.setRawMode(true);
2022
+ process.stdin.resume();
2023
+ process.stdin.on('data', onStdinData);
2024
+ }
2025
+ const controller = new AbortController();
2026
+ const onSigint = () => {
2027
+ logForDebugging('[bridge:shutdown] SIGINT received, shutting down');
2028
+ controller.abort();
2029
+ };
2030
+ const onSigterm = () => {
2031
+ logForDebugging('[bridge:shutdown] SIGTERM received, shutting down');
2032
+ controller.abort();
2033
+ };
2034
+ process.on('SIGINT', onSigint);
2035
+ process.on('SIGTERM', onSigterm);
2036
+ // Auto-create an empty session so the user has somewhere to type
2037
+ // immediately (matching /remote-control behavior). Controlled by
2038
+ // preCreateSession: on by default; --no-create-session-in-dir opts out.
2039
+ // When a --session-id resume succeeded, skip creation entirely — the
2040
+ // session already exists and bridge/reconnect has re-queued it.
2041
+ // When resume was requested but failed on env mismatch, effectiveResumeSessionId
2042
+ // is undefined, so we fall through to fresh session creation (honoring the
2043
+ // "Creating a fresh session instead" warning printed above).
2044
+ let initialSessionId = feature('KAIROS') && effectiveResumeSessionId
2045
+ ? effectiveResumeSessionId
2046
+ : null;
2047
+ if (preCreateSession && !(feature('KAIROS') && effectiveResumeSessionId)) {
2048
+ const { createBridgeSession } = await import('./createSession.js');
2049
+ try {
2050
+ initialSessionId = await createBridgeSession({
2051
+ environmentId,
2052
+ title: name,
2053
+ events: [],
2054
+ gitRepoUrl,
2055
+ branch,
2056
+ signal: controller.signal,
2057
+ baseUrl,
2058
+ getAccessToken: getBridgeAccessToken,
2059
+ permissionMode,
2060
+ });
2061
+ if (initialSessionId) {
2062
+ logForDebugging(`[bridge:init] Created initial session ${initialSessionId}`);
2063
+ }
2064
+ }
2065
+ catch (err) {
2066
+ logForDebugging(`[bridge:init] Session creation failed (non-fatal): ${errorMessage(err)}`);
2067
+ }
2068
+ }
2069
+ // Crash-recovery pointer: write immediately so kill -9 at any point
2070
+ // after this leaves a recoverable trail. Covers both fresh sessions and
2071
+ // resumed ones (so a second crash after resume is still recoverable).
2072
+ // Cleared when runBridgeLoop falls through to archive+deregister; left in
2073
+ // place on the SIGINT resumable-shutdown return (backup for when the user
2074
+ // closes the terminal before copying the printed --session-id hint).
2075
+ // Refreshed hourly so a 5h+ session that crashes still has a fresh
2076
+ // pointer (staleness checks file mtime, backend TTL is rolling-from-poll).
2077
+ let pointerRefreshTimer = null;
2078
+ // Single-session only: --continue forces single-session mode on resume,
2079
+ // so a pointer written in multi-session mode would contradict the user's
2080
+ // config when they try to resume. The resumable-shutdown path is also
2081
+ // gated to single-session (line ~1254) so the pointer would be orphaned.
2082
+ if (initialSessionId && spawnMode === 'single-session') {
2083
+ const { writeBridgePointer } = await import('./bridgePointer.js');
2084
+ const pointerPayload = {
2085
+ sessionId: initialSessionId,
2086
+ environmentId,
2087
+ source: 'standalone',
2088
+ };
2089
+ await writeBridgePointer(config.dir, pointerPayload);
2090
+ pointerRefreshTimer = setInterval(writeBridgePointer, 60 * 60 * 1000, config.dir, pointerPayload);
2091
+ // Don't let the interval keep the process alive on its own.
2092
+ pointerRefreshTimer.unref?.();
2093
+ }
2094
+ try {
2095
+ await runBridgeLoop(config, environmentId, environmentSecret, api, spawner, logger, controller.signal, undefined, initialSessionId ?? undefined, async () => {
2096
+ // Clear the memoized OAuth token cache so we re-read from secure
2097
+ // storage, picking up tokens refreshed by child processes.
2098
+ clearOAuthTokenCache();
2099
+ // Proactively refresh the token if it's expired on disk too.
2100
+ await checkAndRefreshOAuthTokenIfNeeded();
2101
+ return getBridgeAccessToken();
2102
+ });
2103
+ }
2104
+ finally {
2105
+ if (pointerRefreshTimer !== null) {
2106
+ clearInterval(pointerRefreshTimer);
2107
+ }
2108
+ process.off('SIGINT', onSigint);
2109
+ process.off('SIGTERM', onSigterm);
2110
+ process.stdin.off('data', onStdinData);
2111
+ if (process.stdin.isTTY) {
2112
+ process.stdin.setRawMode(false);
2113
+ }
2114
+ process.stdin.pause();
2115
+ }
2116
+ // The bridge bypasses init.ts (and its graceful shutdown handler), so we
2117
+ // must exit explicitly.
2118
+ // eslint-disable-next-line custom-rules/no-process-exit
2119
+ process.exit(0);
2120
+ }
2121
+ // ─── Headless bridge (daemon worker) ────────────────────────────────────────
2122
+ /**
2123
+ * Thrown by runBridgeHeadless for configuration issues the supervisor should
2124
+ * NOT retry (trust not accepted, worktree unavailable, http-not-https). The
2125
+ * daemon worker catches this and exits with EXIT_CODE_PERMANENT so the
2126
+ * supervisor parks the worker instead of respawning it on backoff.
2127
+ */
2128
+ export class BridgeHeadlessPermanentError extends Error {
2129
+ constructor(message) {
2130
+ super(message);
2131
+ this.name = 'BridgeHeadlessPermanentError';
2132
+ }
2133
+ }
2134
+ /**
2135
+ * Non-interactive bridge entrypoint for the `remoteControl` daemon worker.
2136
+ *
2137
+ * Linear subset of bridgeMain(): no readline dialogs, no stdin key handlers,
2138
+ * no TUI, no process.exit(). Config comes from the caller (daemon.json), auth
2139
+ * comes via IPC (supervisor's AuthManager), logs go to the worker's stdout
2140
+ * pipe. Throws on fatal errors — the worker catches and maps permanent vs
2141
+ * transient to the right exit code.
2142
+ *
2143
+ * Resolves cleanly when `signal` aborts and the poll loop tears down.
2144
+ */
2145
+ export async function runBridgeHeadless(opts, signal) {
2146
+ const { dir, log } = opts;
2147
+ // Worker inherits the supervisor's CWD. chdir first so git utilities
2148
+ // (getBranch/getRemoteUrl) — which read from bootstrap CWD state set
2149
+ // below — resolve against the right repo.
2150
+ process.chdir(dir);
2151
+ const { setOriginalCwd, setCwdState } = await import('../bootstrap/state.js');
2152
+ setOriginalCwd(dir);
2153
+ setCwdState(dir);
2154
+ const { enableConfigs, checkHasTrustDialogAccepted } = await import('../utils/config.js');
2155
+ enableConfigs();
2156
+ const { initSinks } = await import('../utils/sinks.js');
2157
+ initSinks();
2158
+ if (!checkHasTrustDialogAccepted()) {
2159
+ throw new BridgeHeadlessPermanentError(`Workspace not trusted: ${dir}. Run \`thaddeus\` in that directory first to accept the trust dialog.`);
2160
+ }
2161
+ if (!opts.getAccessToken()) {
2162
+ // Transient — supervisor's AuthManager may pick up a token on next cycle.
2163
+ throw new Error(BRIDGE_LOGIN_ERROR);
2164
+ }
2165
+ const { getBridgeBaseUrl } = await import('./bridgeConfig.js');
2166
+ const baseUrl = getBridgeBaseUrl();
2167
+ if (baseUrl.startsWith('http://') &&
2168
+ !baseUrl.includes('localhost') &&
2169
+ !baseUrl.includes('127.0.0.1')) {
2170
+ throw new BridgeHeadlessPermanentError('Remote Control base URL uses HTTP. Only HTTPS or localhost HTTP is allowed.');
2171
+ }
2172
+ const sessionIngressUrl = process.env.USER_TYPE === 'ant' &&
2173
+ process.env.CLAUDE_BRIDGE_SESSION_INGRESS_URL
2174
+ ? process.env.CLAUDE_BRIDGE_SESSION_INGRESS_URL
2175
+ : baseUrl;
2176
+ const { getBranch, getRemoteUrl, findGitRoot } = await import('../utils/git.js');
2177
+ const { hasWorktreeCreateHook } = await import('../utils/hooks.js');
2178
+ if (opts.spawnMode === 'worktree') {
2179
+ const worktreeAvailable = hasWorktreeCreateHook() || findGitRoot(dir) !== null;
2180
+ if (!worktreeAvailable) {
2181
+ throw new BridgeHeadlessPermanentError(`Worktree mode requires a git repository or WorktreeCreate hooks. Directory ${dir} has neither.`);
2182
+ }
2183
+ }
2184
+ const branch = await getBranch();
2185
+ const gitRepoUrl = await getRemoteUrl();
2186
+ const machineName = hostname();
2187
+ const bridgeId = randomUUID();
2188
+ const config = {
2189
+ dir,
2190
+ machineName,
2191
+ branch,
2192
+ gitRepoUrl,
2193
+ maxSessions: opts.capacity,
2194
+ spawnMode: opts.spawnMode,
2195
+ verbose: false,
2196
+ sandbox: opts.sandbox,
2197
+ bridgeId,
2198
+ workerType: 'thaddeus',
2199
+ environmentId: randomUUID(),
2200
+ apiBaseUrl: baseUrl,
2201
+ sessionIngressUrl,
2202
+ sessionTimeoutMs: opts.sessionTimeoutMs,
2203
+ };
2204
+ const api = createBridgeApiClient({
2205
+ baseUrl,
2206
+ getAccessToken: opts.getAccessToken,
2207
+ runnerVersion: MACRO.VERSION,
2208
+ onDebug: log,
2209
+ onAuth401: opts.onAuth401,
2210
+ getTrustedDeviceToken,
2211
+ });
2212
+ let environmentId;
2213
+ let environmentSecret;
2214
+ try {
2215
+ const reg = await api.registerBridgeEnvironment(config);
2216
+ environmentId = reg.environment_id;
2217
+ environmentSecret = reg.environment_secret;
2218
+ }
2219
+ catch (err) {
2220
+ // Transient — let supervisor backoff-retry.
2221
+ throw new Error(`Bridge registration failed: ${errorMessage(err)}`);
2222
+ }
2223
+ const spawner = createSessionSpawner({
2224
+ execPath: process.execPath,
2225
+ scriptArgs: spawnScriptArgs(),
2226
+ env: process.env,
2227
+ verbose: false,
2228
+ sandbox: opts.sandbox,
2229
+ permissionMode: opts.permissionMode,
2230
+ onDebug: log,
2231
+ });
2232
+ const logger = createHeadlessBridgeLogger(log);
2233
+ logger.printBanner(config, environmentId);
2234
+ let initialSessionId;
2235
+ if (opts.createSessionOnStart) {
2236
+ const { createBridgeSession } = await import('./createSession.js');
2237
+ try {
2238
+ const sid = await createBridgeSession({
2239
+ environmentId,
2240
+ title: opts.name,
2241
+ events: [],
2242
+ gitRepoUrl,
2243
+ branch,
2244
+ signal,
2245
+ baseUrl,
2246
+ getAccessToken: opts.getAccessToken,
2247
+ permissionMode: opts.permissionMode,
2248
+ });
2249
+ if (sid) {
2250
+ initialSessionId = sid;
2251
+ log(`created initial session ${sid}`);
2252
+ }
2253
+ }
2254
+ catch (err) {
2255
+ log(`session pre-creation failed (non-fatal): ${errorMessage(err)}`);
2256
+ }
2257
+ }
2258
+ await runBridgeLoop(config, environmentId, environmentSecret, api, spawner, logger, signal, undefined, initialSessionId, async () => opts.getAccessToken());
2259
+ }
2260
+ /** BridgeLogger adapter that routes everything to a single line-log fn. */
2261
+ function createHeadlessBridgeLogger(log) {
2262
+ const noop = () => { };
2263
+ return {
2264
+ printBanner: (cfg, envId) => log(`registered environmentId=${envId} dir=${cfg.dir} spawnMode=${cfg.spawnMode} capacity=${cfg.maxSessions}`),
2265
+ logSessionStart: (id, _prompt) => log(`session start ${id}`),
2266
+ logSessionComplete: (id, ms) => log(`session complete ${id} (${ms}ms)`),
2267
+ logSessionFailed: (id, err) => log(`session failed ${id}: ${err}`),
2268
+ logStatus: log,
2269
+ logVerbose: log,
2270
+ logError: s => log(`error: ${s}`),
2271
+ logReconnected: ms => log(`reconnected after ${ms}ms`),
2272
+ addSession: (id, _url) => log(`session attached ${id}`),
2273
+ removeSession: id => log(`session detached ${id}`),
2274
+ updateIdleStatus: noop,
2275
+ updateReconnectingStatus: noop,
2276
+ updateSessionStatus: noop,
2277
+ updateSessionActivity: noop,
2278
+ updateSessionCount: noop,
2279
+ updateFailedStatus: noop,
2280
+ setSpawnModeDisplay: noop,
2281
+ setRepoInfo: noop,
2282
+ setDebugLogPath: noop,
2283
+ setAttached: noop,
2284
+ setSessionTitle: noop,
2285
+ clearStatus: noop,
2286
+ toggleQr: noop,
2287
+ refreshDisplay: noop,
2288
+ };
2289
+ }