ashishcode 0.1.0

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