@xortex/xcode 3.0.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 (934) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +171 -0
  3. package/bin/xcode +127 -0
  4. package/bin/xcode-test +84 -0
  5. package/bin/xcode.cmd +31 -0
  6. package/constants/apiLimits.ts +94 -0
  7. package/constants/betas.ts +52 -0
  8. package/constants/common.ts +33 -0
  9. package/constants/cyberRiskInstruction.ts +24 -0
  10. package/constants/errorIds.ts +15 -0
  11. package/constants/figures.ts +45 -0
  12. package/constants/files.ts +156 -0
  13. package/constants/github-app.ts +144 -0
  14. package/constants/keys.ts +11 -0
  15. package/constants/messages.ts +1 -0
  16. package/constants/oauth.ts +234 -0
  17. package/constants/outputStyles.ts +216 -0
  18. package/constants/product.ts +76 -0
  19. package/constants/prompts.ts +939 -0
  20. package/constants/spinnerVerbs.ts +204 -0
  21. package/constants/system.ts +95 -0
  22. package/constants/systemPromptSections.ts +68 -0
  23. package/constants/toolLimits.ts +56 -0
  24. package/constants/tools.ts +112 -0
  25. package/constants/turnCompletionVerbs.ts +12 -0
  26. package/constants/xml.ts +86 -0
  27. package/entrypoints/agentSdkTypes.ts +443 -0
  28. package/entrypoints/cli.tsx +307 -0
  29. package/entrypoints/init.ts +340 -0
  30. package/entrypoints/mcp.ts +196 -0
  31. package/entrypoints/sandboxTypes.ts +156 -0
  32. package/entrypoints/sdk/controlSchemas.ts +663 -0
  33. package/entrypoints/sdk/coreSchemas.ts +1889 -0
  34. package/entrypoints/sdk/coreTypes.generated.ts +2 -0
  35. package/entrypoints/sdk/coreTypes.ts +62 -0
  36. package/entrypoints/sdk/runtimeTypes.ts +140 -0
  37. package/entrypoints/sdk/sdkUtilityTypes.ts +3 -0
  38. package/entrypoints/sdk/toolTypes.ts +90 -0
  39. package/main.tsx +4686 -0
  40. package/package.json +120 -0
  41. package/services/AgentSummary/agentSummary.ts +179 -0
  42. package/services/MagicDocs/magicDocs.ts +254 -0
  43. package/services/MagicDocs/prompts.ts +127 -0
  44. package/services/PromptSuggestion/promptSuggestion.ts +523 -0
  45. package/services/PromptSuggestion/speculation.ts +991 -0
  46. package/services/SessionMemory/prompts.ts +324 -0
  47. package/services/SessionMemory/sessionMemory.ts +495 -0
  48. package/services/SessionMemory/sessionMemoryUtils.ts +207 -0
  49. package/services/analytics/config.ts +38 -0
  50. package/services/analytics/datadog.ts +307 -0
  51. package/services/analytics/firstPartyEventLogger.ts +449 -0
  52. package/services/analytics/firstPartyEventLoggingExporter.ts +806 -0
  53. package/services/analytics/growthbook.ts +1155 -0
  54. package/services/analytics/index.ts +173 -0
  55. package/services/analytics/metadata.ts +973 -0
  56. package/services/analytics/sink.ts +114 -0
  57. package/services/analytics/sinkKillswitch.ts +25 -0
  58. package/services/api/adminRequests.ts +119 -0
  59. package/services/api/bootstrap.ts +141 -0
  60. package/services/api/claude.ts +3422 -0
  61. package/services/api/client.ts +406 -0
  62. package/services/api/dumpPrompts.ts +226 -0
  63. package/services/api/emptyUsage.ts +22 -0
  64. package/services/api/errorUtils.ts +260 -0
  65. package/services/api/errors.ts +1207 -0
  66. package/services/api/filesApi.ts +748 -0
  67. package/services/api/firstTokenDate.ts +60 -0
  68. package/services/api/gemini.ts +359 -0
  69. package/services/api/geminiAdapter.ts +123 -0
  70. package/services/api/geminiClient.ts +291 -0
  71. package/services/api/grove.ts +357 -0
  72. package/services/api/logging.ts +788 -0
  73. package/services/api/metricsOptOut.ts +159 -0
  74. package/services/api/openRouterClient.ts +453 -0
  75. package/services/api/overageCreditGrant.ts +137 -0
  76. package/services/api/promptCacheBreakDetection.ts +727 -0
  77. package/services/api/referral.ts +281 -0
  78. package/services/api/sessionIngress.ts +514 -0
  79. package/services/api/ultrareviewQuota.ts +38 -0
  80. package/services/api/usage.ts +63 -0
  81. package/services/api/withRetry.ts +822 -0
  82. package/services/autoDream/autoDream.ts +324 -0
  83. package/services/autoDream/config.ts +21 -0
  84. package/services/autoDream/consolidationLock.ts +140 -0
  85. package/services/autoDream/consolidationPrompt.ts +65 -0
  86. package/services/awaySummary.ts +74 -0
  87. package/services/claudeAiLimits.ts +515 -0
  88. package/services/claudeAiLimitsHook.ts +23 -0
  89. package/services/compact/apiMicrocompact.ts +153 -0
  90. package/services/compact/autoCompact.ts +351 -0
  91. package/services/compact/compact.ts +1705 -0
  92. package/services/compact/compactWarningHook.ts +16 -0
  93. package/services/compact/compactWarningState.ts +18 -0
  94. package/services/compact/grouping.ts +63 -0
  95. package/services/compact/microCompact.ts +530 -0
  96. package/services/compact/postCompactCleanup.ts +77 -0
  97. package/services/compact/prompt.ts +374 -0
  98. package/services/compact/sessionMemoryCompact.ts +630 -0
  99. package/services/compact/timeBasedMCConfig.ts +43 -0
  100. package/services/diagnosticTracking.ts +397 -0
  101. package/services/extractMemories/extractMemories.ts +517 -0
  102. package/services/extractMemories/prompts.ts +154 -0
  103. package/services/internalLogging.ts +90 -0
  104. package/services/lsp/LSPClient.ts +447 -0
  105. package/services/lsp/LSPDiagnosticRegistry.ts +386 -0
  106. package/services/lsp/LSPServerInstance.ts +511 -0
  107. package/services/lsp/LSPServerManager.ts +420 -0
  108. package/services/lsp/config.ts +79 -0
  109. package/services/lsp/manager.ts +289 -0
  110. package/services/lsp/passiveFeedback.ts +328 -0
  111. package/services/mcp/InProcessTransport.ts +63 -0
  112. package/services/mcp/MCPConnectionManager.tsx +73 -0
  113. package/services/mcp/SdkControlTransport.ts +136 -0
  114. package/services/mcp/auth.ts +2465 -0
  115. package/services/mcp/channelAllowlist.ts +76 -0
  116. package/services/mcp/channelNotification.ts +316 -0
  117. package/services/mcp/channelPermissions.ts +240 -0
  118. package/services/mcp/claudeai.ts +164 -0
  119. package/services/mcp/client.ts +3348 -0
  120. package/services/mcp/config.ts +1578 -0
  121. package/services/mcp/elicitationHandler.ts +313 -0
  122. package/services/mcp/envExpansion.ts +38 -0
  123. package/services/mcp/headersHelper.ts +138 -0
  124. package/services/mcp/mcpStringUtils.ts +106 -0
  125. package/services/mcp/normalization.ts +23 -0
  126. package/services/mcp/oauthPort.ts +78 -0
  127. package/services/mcp/officialRegistry.ts +72 -0
  128. package/services/mcp/types.ts +258 -0
  129. package/services/mcp/useManageMCPConnections.ts +1141 -0
  130. package/services/mcp/utils.ts +575 -0
  131. package/services/mcp/vscodeSdkMcp.ts +112 -0
  132. package/services/mcp/xaa.ts +511 -0
  133. package/services/mcp/xaaIdpLogin.ts +487 -0
  134. package/services/mcpServerApproval.tsx +41 -0
  135. package/services/mockRateLimits.ts +882 -0
  136. package/services/notifier.ts +156 -0
  137. package/services/oauth/auth-code-listener.ts +211 -0
  138. package/services/oauth/client.ts +566 -0
  139. package/services/oauth/crypto.ts +23 -0
  140. package/services/oauth/getOauthProfile.ts +53 -0
  141. package/services/oauth/index.ts +198 -0
  142. package/services/plugins/PluginInstallationManager.ts +184 -0
  143. package/services/plugins/pluginCliCommands.ts +344 -0
  144. package/services/plugins/pluginOperations.ts +1088 -0
  145. package/services/policyLimits/index.ts +663 -0
  146. package/services/policyLimits/types.ts +27 -0
  147. package/services/preventSleep.ts +165 -0
  148. package/services/rateLimitMessages.ts +344 -0
  149. package/services/rateLimitMocking.ts +144 -0
  150. package/services/remoteManagedSettings/index.ts +638 -0
  151. package/services/remoteManagedSettings/securityCheck.tsx +74 -0
  152. package/services/remoteManagedSettings/syncCache.ts +112 -0
  153. package/services/remoteManagedSettings/syncCacheState.ts +96 -0
  154. package/services/remoteManagedSettings/types.ts +31 -0
  155. package/services/settingsSync/index.ts +581 -0
  156. package/services/settingsSync/types.ts +67 -0
  157. package/services/teamMemorySync/index.ts +1256 -0
  158. package/services/teamMemorySync/secretScanner.ts +324 -0
  159. package/services/teamMemorySync/teamMemSecretGuard.ts +44 -0
  160. package/services/teamMemorySync/types.ts +156 -0
  161. package/services/teamMemorySync/watcher.ts +387 -0
  162. package/services/tips/tipHistory.ts +17 -0
  163. package/services/tips/tipRegistry.ts +686 -0
  164. package/services/tips/tipScheduler.ts +58 -0
  165. package/services/tokenEstimation.ts +495 -0
  166. package/services/toolUseSummary/toolUseSummaryGenerator.ts +112 -0
  167. package/services/tools/StreamingToolExecutor.ts +530 -0
  168. package/services/tools/toolExecution.ts +1745 -0
  169. package/services/tools/toolHooks.ts +650 -0
  170. package/services/tools/toolOrchestration.ts +188 -0
  171. package/services/vcr.ts +406 -0
  172. package/services/voice.ts +525 -0
  173. package/services/voiceKeyterms.ts +106 -0
  174. package/services/voiceStreamSTT.ts +544 -0
  175. package/tools/AgentTool/AgentTool.tsx +1398 -0
  176. package/tools/AgentTool/UI.tsx +872 -0
  177. package/tools/AgentTool/agentColorManager.ts +66 -0
  178. package/tools/AgentTool/agentDisplay.ts +104 -0
  179. package/tools/AgentTool/agentMemory.ts +177 -0
  180. package/tools/AgentTool/agentMemorySnapshot.ts +197 -0
  181. package/tools/AgentTool/agentToolUtils.ts +686 -0
  182. package/tools/AgentTool/built-in/claudeCodeGuideAgent.ts +205 -0
  183. package/tools/AgentTool/built-in/exploreAgent.ts +83 -0
  184. package/tools/AgentTool/built-in/generalPurposeAgent.ts +34 -0
  185. package/tools/AgentTool/built-in/planAgent.ts +92 -0
  186. package/tools/AgentTool/built-in/statuslineSetup.ts +144 -0
  187. package/tools/AgentTool/built-in/verificationAgent.ts +152 -0
  188. package/tools/AgentTool/builtInAgents.ts +72 -0
  189. package/tools/AgentTool/constants.ts +12 -0
  190. package/tools/AgentTool/forkSubagent.ts +210 -0
  191. package/tools/AgentTool/loadAgentsDir.ts +755 -0
  192. package/tools/AgentTool/prompt.ts +287 -0
  193. package/tools/AgentTool/resumeAgent.ts +265 -0
  194. package/tools/AgentTool/runAgent.ts +973 -0
  195. package/tools/AskUserQuestionTool/AskUserQuestionTool.tsx +266 -0
  196. package/tools/AskUserQuestionTool/prompt.ts +44 -0
  197. package/tools/BashTool/BashTool.tsx +1144 -0
  198. package/tools/BashTool/BashToolResultMessage.tsx +191 -0
  199. package/tools/BashTool/UI.tsx +185 -0
  200. package/tools/BashTool/bashCommandHelpers.ts +265 -0
  201. package/tools/BashTool/bashPermissions.ts +2621 -0
  202. package/tools/BashTool/bashSecurity.ts +2592 -0
  203. package/tools/BashTool/commandSemantics.ts +140 -0
  204. package/tools/BashTool/commentLabel.ts +13 -0
  205. package/tools/BashTool/destructiveCommandWarning.ts +102 -0
  206. package/tools/BashTool/modeValidation.ts +115 -0
  207. package/tools/BashTool/pathValidation.ts +1303 -0
  208. package/tools/BashTool/prompt.ts +369 -0
  209. package/tools/BashTool/readOnlyValidation.ts +1990 -0
  210. package/tools/BashTool/sedEditParser.ts +322 -0
  211. package/tools/BashTool/sedValidation.ts +684 -0
  212. package/tools/BashTool/shouldUseSandbox.ts +153 -0
  213. package/tools/BashTool/toolName.ts +2 -0
  214. package/tools/BashTool/utils.ts +223 -0
  215. package/tools/BriefTool/BriefTool.ts +204 -0
  216. package/tools/BriefTool/UI.tsx +101 -0
  217. package/tools/BriefTool/attachments.ts +110 -0
  218. package/tools/BriefTool/prompt.ts +22 -0
  219. package/tools/BriefTool/upload.ts +174 -0
  220. package/tools/ConfigTool/ConfigTool.ts +467 -0
  221. package/tools/ConfigTool/UI.tsx +38 -0
  222. package/tools/ConfigTool/constants.ts +1 -0
  223. package/tools/ConfigTool/prompt.ts +93 -0
  224. package/tools/ConfigTool/supportedSettings.ts +211 -0
  225. package/tools/EnterPlanModeTool/EnterPlanModeTool.ts +126 -0
  226. package/tools/EnterPlanModeTool/UI.tsx +33 -0
  227. package/tools/EnterPlanModeTool/constants.ts +1 -0
  228. package/tools/EnterPlanModeTool/prompt.ts +170 -0
  229. package/tools/EnterWorktreeTool/EnterWorktreeTool.ts +127 -0
  230. package/tools/EnterWorktreeTool/UI.tsx +20 -0
  231. package/tools/EnterWorktreeTool/constants.ts +1 -0
  232. package/tools/EnterWorktreeTool/prompt.ts +30 -0
  233. package/tools/ExitPlanModeTool/ExitPlanModeV2Tool.ts +493 -0
  234. package/tools/ExitPlanModeTool/UI.tsx +82 -0
  235. package/tools/ExitPlanModeTool/constants.ts +2 -0
  236. package/tools/ExitPlanModeTool/prompt.ts +29 -0
  237. package/tools/ExitWorktreeTool/ExitWorktreeTool.ts +329 -0
  238. package/tools/ExitWorktreeTool/UI.tsx +25 -0
  239. package/tools/ExitWorktreeTool/constants.ts +1 -0
  240. package/tools/ExitWorktreeTool/prompt.ts +32 -0
  241. package/tools/FileEditTool/FileEditTool.ts +625 -0
  242. package/tools/FileEditTool/UI.tsx +289 -0
  243. package/tools/FileEditTool/constants.ts +11 -0
  244. package/tools/FileEditTool/prompt.ts +28 -0
  245. package/tools/FileEditTool/types.ts +85 -0
  246. package/tools/FileEditTool/utils.ts +775 -0
  247. package/tools/FileReadTool/FileReadTool.ts +1183 -0
  248. package/tools/FileReadTool/UI.tsx +185 -0
  249. package/tools/FileReadTool/imageProcessor.ts +94 -0
  250. package/tools/FileReadTool/limits.ts +92 -0
  251. package/tools/FileReadTool/prompt.ts +49 -0
  252. package/tools/FileWriteTool/FileWriteTool.ts +434 -0
  253. package/tools/FileWriteTool/UI.tsx +405 -0
  254. package/tools/FileWriteTool/prompt.ts +18 -0
  255. package/tools/GlobTool/GlobTool.ts +198 -0
  256. package/tools/GlobTool/UI.tsx +63 -0
  257. package/tools/GlobTool/prompt.ts +7 -0
  258. package/tools/GrepTool/GrepTool.ts +577 -0
  259. package/tools/GrepTool/UI.tsx +201 -0
  260. package/tools/GrepTool/prompt.ts +18 -0
  261. package/tools/LSPTool/LSPTool.ts +860 -0
  262. package/tools/LSPTool/UI.tsx +228 -0
  263. package/tools/LSPTool/formatters.ts +592 -0
  264. package/tools/LSPTool/prompt.ts +21 -0
  265. package/tools/LSPTool/schemas.ts +215 -0
  266. package/tools/LSPTool/symbolContext.ts +90 -0
  267. package/tools/ListMcpResourcesTool/ListMcpResourcesTool.ts +123 -0
  268. package/tools/ListMcpResourcesTool/UI.tsx +29 -0
  269. package/tools/ListMcpResourcesTool/prompt.ts +20 -0
  270. package/tools/MCPTool/MCPTool.ts +77 -0
  271. package/tools/MCPTool/UI.tsx +403 -0
  272. package/tools/MCPTool/classifyForCollapse.ts +604 -0
  273. package/tools/MCPTool/prompt.ts +3 -0
  274. package/tools/McpAuthTool/McpAuthTool.ts +215 -0
  275. package/tools/NotebookEditTool/NotebookEditTool.ts +490 -0
  276. package/tools/NotebookEditTool/UI.tsx +93 -0
  277. package/tools/NotebookEditTool/constants.ts +2 -0
  278. package/tools/NotebookEditTool/prompt.ts +3 -0
  279. package/tools/PowerShellTool/PowerShellTool.tsx +1001 -0
  280. package/tools/PowerShellTool/UI.tsx +131 -0
  281. package/tools/PowerShellTool/clmTypes.ts +211 -0
  282. package/tools/PowerShellTool/commandSemantics.ts +142 -0
  283. package/tools/PowerShellTool/commonParameters.ts +30 -0
  284. package/tools/PowerShellTool/destructiveCommandWarning.ts +109 -0
  285. package/tools/PowerShellTool/gitSafety.ts +176 -0
  286. package/tools/PowerShellTool/modeValidation.ts +404 -0
  287. package/tools/PowerShellTool/pathValidation.ts +2049 -0
  288. package/tools/PowerShellTool/powershellPermissions.ts +1648 -0
  289. package/tools/PowerShellTool/powershellSecurity.ts +1090 -0
  290. package/tools/PowerShellTool/prompt.ts +145 -0
  291. package/tools/PowerShellTool/readOnlyValidation.ts +1823 -0
  292. package/tools/PowerShellTool/toolName.ts +2 -0
  293. package/tools/REPLTool/constants.ts +46 -0
  294. package/tools/REPLTool/primitiveTools.ts +39 -0
  295. package/tools/ReadMcpResourceTool/ReadMcpResourceTool.ts +158 -0
  296. package/tools/ReadMcpResourceTool/UI.tsx +37 -0
  297. package/tools/ReadMcpResourceTool/prompt.ts +16 -0
  298. package/tools/RemoteTriggerTool/RemoteTriggerTool.ts +161 -0
  299. package/tools/RemoteTriggerTool/UI.tsx +17 -0
  300. package/tools/RemoteTriggerTool/prompt.ts +15 -0
  301. package/tools/ScheduleCronTool/CronCreateTool.ts +157 -0
  302. package/tools/ScheduleCronTool/CronDeleteTool.ts +95 -0
  303. package/tools/ScheduleCronTool/CronListTool.ts +97 -0
  304. package/tools/ScheduleCronTool/UI.tsx +60 -0
  305. package/tools/ScheduleCronTool/prompt.ts +135 -0
  306. package/tools/SendMessageTool/SendMessageTool.ts +917 -0
  307. package/tools/SendMessageTool/UI.tsx +31 -0
  308. package/tools/SendMessageTool/constants.ts +1 -0
  309. package/tools/SendMessageTool/prompt.ts +49 -0
  310. package/tools/SkillTool/SkillTool.ts +1108 -0
  311. package/tools/SkillTool/UI.tsx +128 -0
  312. package/tools/SkillTool/constants.ts +1 -0
  313. package/tools/SkillTool/prompt.ts +241 -0
  314. package/tools/SleepTool/prompt.ts +17 -0
  315. package/tools/SyntheticOutputTool/SyntheticOutputTool.ts +163 -0
  316. package/tools/TaskCreateTool/TaskCreateTool.ts +138 -0
  317. package/tools/TaskCreateTool/constants.ts +1 -0
  318. package/tools/TaskCreateTool/prompt.ts +56 -0
  319. package/tools/TaskGetTool/TaskGetTool.ts +128 -0
  320. package/tools/TaskGetTool/constants.ts +1 -0
  321. package/tools/TaskGetTool/prompt.ts +24 -0
  322. package/tools/TaskListTool/TaskListTool.ts +116 -0
  323. package/tools/TaskListTool/constants.ts +1 -0
  324. package/tools/TaskListTool/prompt.ts +49 -0
  325. package/tools/TaskOutputTool/TaskOutputTool.tsx +584 -0
  326. package/tools/TaskOutputTool/constants.ts +1 -0
  327. package/tools/TaskStopTool/TaskStopTool.ts +131 -0
  328. package/tools/TaskStopTool/UI.tsx +41 -0
  329. package/tools/TaskStopTool/prompt.ts +8 -0
  330. package/tools/TaskUpdateTool/TaskUpdateTool.ts +406 -0
  331. package/tools/TaskUpdateTool/constants.ts +1 -0
  332. package/tools/TaskUpdateTool/prompt.ts +77 -0
  333. package/tools/TeamCreateTool/TeamCreateTool.ts +240 -0
  334. package/tools/TeamCreateTool/UI.tsx +6 -0
  335. package/tools/TeamCreateTool/constants.ts +1 -0
  336. package/tools/TeamCreateTool/prompt.ts +113 -0
  337. package/tools/TeamDeleteTool/TeamDeleteTool.ts +139 -0
  338. package/tools/TeamDeleteTool/UI.tsx +20 -0
  339. package/tools/TeamDeleteTool/constants.ts +1 -0
  340. package/tools/TeamDeleteTool/prompt.ts +16 -0
  341. package/tools/TodoWriteTool/TodoWriteTool.ts +115 -0
  342. package/tools/TodoWriteTool/constants.ts +1 -0
  343. package/tools/TodoWriteTool/prompt.ts +184 -0
  344. package/tools/ToolSearchTool/ToolSearchTool.ts +471 -0
  345. package/tools/ToolSearchTool/constants.ts +1 -0
  346. package/tools/ToolSearchTool/prompt.ts +121 -0
  347. package/tools/TungstenTool/TungstenTool.ts +4 -0
  348. package/tools/WebFetchTool/UI.tsx +72 -0
  349. package/tools/WebFetchTool/WebFetchTool.ts +318 -0
  350. package/tools/WebFetchTool/preapproved.ts +166 -0
  351. package/tools/WebFetchTool/prompt.ts +46 -0
  352. package/tools/WebFetchTool/utils.ts +530 -0
  353. package/tools/WebSearchTool/UI.tsx +101 -0
  354. package/tools/WebSearchTool/WebSearchTool.ts +435 -0
  355. package/tools/WebSearchTool/prompt.ts +34 -0
  356. package/tools/WorkflowTool/constants.ts +2 -0
  357. package/tools/XMemIngestTool/XMemIngestTool.ts +140 -0
  358. package/tools/XMemIngestTool/prompt.ts +13 -0
  359. package/tools/XMemRetrieveTool/XMemRetrieveTool.ts +177 -0
  360. package/tools/XMemRetrieveTool/prompt.ts +16 -0
  361. package/tools/XMemSearchTool/XMemSearchTool.ts +172 -0
  362. package/tools/XMemSearchTool/prompt.ts +11 -0
  363. package/tools/shared/gitOperationTracking.ts +277 -0
  364. package/tools/shared/spawnMultiAgent.ts +1093 -0
  365. package/tools/testing/TestingPermissionTool.tsx +74 -0
  366. package/tools/utils.ts +40 -0
  367. package/utils/CircularBuffer.ts +84 -0
  368. package/utils/Cursor.ts +1530 -0
  369. package/utils/QueryGuard.ts +121 -0
  370. package/utils/Shell.ts +474 -0
  371. package/utils/ShellCommand.ts +465 -0
  372. package/utils/abortController.ts +99 -0
  373. package/utils/activityManager.ts +164 -0
  374. package/utils/advisor.ts +145 -0
  375. package/utils/agentContext.ts +178 -0
  376. package/utils/agentId.ts +99 -0
  377. package/utils/agentSwarmsEnabled.ts +44 -0
  378. package/utils/agenticSessionSearch.ts +307 -0
  379. package/utils/analyzeContext.ts +1382 -0
  380. package/utils/ansiToPng.ts +334 -0
  381. package/utils/ansiToSvg.ts +272 -0
  382. package/utils/api.ts +718 -0
  383. package/utils/apiPreconnect.ts +71 -0
  384. package/utils/appleTerminalBackup.ts +124 -0
  385. package/utils/argumentSubstitution.ts +145 -0
  386. package/utils/array.ts +13 -0
  387. package/utils/asciicast.ts +239 -0
  388. package/utils/attachments.ts +4091 -0
  389. package/utils/attribution.ts +393 -0
  390. package/utils/auth.ts +2002 -0
  391. package/utils/authFileDescriptor.ts +196 -0
  392. package/utils/authPortable.ts +19 -0
  393. package/utils/autoModeDenials.ts +26 -0
  394. package/utils/autoRunIssue.tsx +122 -0
  395. package/utils/autoUpdater.ts +561 -0
  396. package/utils/aws.ts +74 -0
  397. package/utils/awsAuthStatusManager.ts +81 -0
  398. package/utils/axios.ts +8 -0
  399. package/utils/background/remote/preconditions.ts +235 -0
  400. package/utils/background/remote/remoteSession.ts +98 -0
  401. package/utils/backgroundHousekeeping.ts +94 -0
  402. package/utils/bash/ParsedCommand.ts +318 -0
  403. package/utils/bash/ShellSnapshot.ts +582 -0
  404. package/utils/bash/ast.ts +2679 -0
  405. package/utils/bash/bashParser.ts +4436 -0
  406. package/utils/bash/bashPipeCommand.ts +294 -0
  407. package/utils/bash/commands.ts +1339 -0
  408. package/utils/bash/heredoc.ts +733 -0
  409. package/utils/bash/parser.ts +230 -0
  410. package/utils/bash/prefix.ts +204 -0
  411. package/utils/bash/registry.ts +53 -0
  412. package/utils/bash/shellCompletion.ts +259 -0
  413. package/utils/bash/shellPrefix.ts +28 -0
  414. package/utils/bash/shellQuote.ts +304 -0
  415. package/utils/bash/shellQuoting.ts +128 -0
  416. package/utils/bash/specs/alias.ts +14 -0
  417. package/utils/bash/specs/index.ts +18 -0
  418. package/utils/bash/specs/nohup.ts +13 -0
  419. package/utils/bash/specs/pyright.ts +91 -0
  420. package/utils/bash/specs/sleep.ts +13 -0
  421. package/utils/bash/specs/srun.ts +31 -0
  422. package/utils/bash/specs/time.ts +13 -0
  423. package/utils/bash/specs/timeout.ts +20 -0
  424. package/utils/bash/treeSitterAnalysis.ts +506 -0
  425. package/utils/betas.ts +434 -0
  426. package/utils/billing.ts +78 -0
  427. package/utils/binaryCheck.ts +53 -0
  428. package/utils/browser.ts +68 -0
  429. package/utils/bufferedWriter.ts +100 -0
  430. package/utils/bundledMode.ts +22 -0
  431. package/utils/caCerts.ts +115 -0
  432. package/utils/caCertsConfig.ts +88 -0
  433. package/utils/cachePaths.ts +38 -0
  434. package/utils/classifierApprovals.ts +88 -0
  435. package/utils/classifierApprovalsHook.ts +17 -0
  436. package/utils/claudeCodeHints.ts +193 -0
  437. package/utils/claudeDesktop.ts +152 -0
  438. package/utils/claudeInChrome/chromeNativeHost.ts +527 -0
  439. package/utils/claudeInChrome/common.ts +540 -0
  440. package/utils/claudeInChrome/mcpServer.ts +292 -0
  441. package/utils/claudeInChrome/prompt.ts +83 -0
  442. package/utils/claudeInChrome/setup.ts +400 -0
  443. package/utils/claudeInChrome/setupPortable.ts +233 -0
  444. package/utils/claudeInChrome/toolRendering.tsx +262 -0
  445. package/utils/claudemd.ts +1479 -0
  446. package/utils/cleanup.ts +602 -0
  447. package/utils/cleanupRegistry.ts +25 -0
  448. package/utils/cliArgs.ts +60 -0
  449. package/utils/cliHighlight.ts +54 -0
  450. package/utils/codeIndexing.ts +206 -0
  451. package/utils/collapseBackgroundBashNotifications.ts +84 -0
  452. package/utils/collapseHookSummaries.ts +59 -0
  453. package/utils/collapseReadSearch.ts +1109 -0
  454. package/utils/collapseTeammateShutdowns.ts +55 -0
  455. package/utils/color-diff-mock.ts +27 -0
  456. package/utils/combinedAbortSignal.ts +47 -0
  457. package/utils/commandLifecycle.ts +21 -0
  458. package/utils/commitAttribution.ts +961 -0
  459. package/utils/completionCache.ts +166 -0
  460. package/utils/computerUse/appNames.ts +196 -0
  461. package/utils/computerUse/cleanup.ts +86 -0
  462. package/utils/computerUse/common.ts +61 -0
  463. package/utils/computerUse/computerUseLock.ts +215 -0
  464. package/utils/computerUse/drainRunLoop.ts +79 -0
  465. package/utils/computerUse/escHotkey.ts +54 -0
  466. package/utils/computerUse/executor.ts +658 -0
  467. package/utils/computerUse/gates.ts +72 -0
  468. package/utils/computerUse/hostAdapter.ts +69 -0
  469. package/utils/computerUse/inputLoader.ts +30 -0
  470. package/utils/computerUse/mcpServer.ts +106 -0
  471. package/utils/computerUse/setup.ts +53 -0
  472. package/utils/computerUse/swiftLoader.ts +23 -0
  473. package/utils/computerUse/toolRendering.tsx +125 -0
  474. package/utils/computerUse/wrapper.tsx +336 -0
  475. package/utils/concurrentSessions.ts +204 -0
  476. package/utils/config.ts +1817 -0
  477. package/utils/configConstants.ts +21 -0
  478. package/utils/contentArray.ts +51 -0
  479. package/utils/context.ts +221 -0
  480. package/utils/contextAnalysis.ts +272 -0
  481. package/utils/contextSuggestions.ts +235 -0
  482. package/utils/controlMessageCompat.ts +32 -0
  483. package/utils/conversationRecovery.ts +597 -0
  484. package/utils/cron.ts +308 -0
  485. package/utils/cronJitterConfig.ts +75 -0
  486. package/utils/cronScheduler.ts +565 -0
  487. package/utils/cronTasks.ts +458 -0
  488. package/utils/cronTasksLock.ts +195 -0
  489. package/utils/crossProjectResume.ts +75 -0
  490. package/utils/crypto.ts +13 -0
  491. package/utils/cwd.ts +32 -0
  492. package/utils/debug.ts +268 -0
  493. package/utils/debugFilter.ts +157 -0
  494. package/utils/deepLink/banner.ts +123 -0
  495. package/utils/deepLink/parseDeepLink.ts +170 -0
  496. package/utils/deepLink/protocolHandler.ts +136 -0
  497. package/utils/deepLink/registerProtocol.ts +348 -0
  498. package/utils/deepLink/terminalLauncher.ts +557 -0
  499. package/utils/deepLink/terminalPreference.ts +54 -0
  500. package/utils/desktopDeepLink.ts +236 -0
  501. package/utils/detectRepository.ts +178 -0
  502. package/utils/diagLogs.ts +94 -0
  503. package/utils/diff.ts +177 -0
  504. package/utils/directMemberMessage.ts +69 -0
  505. package/utils/displayTags.ts +51 -0
  506. package/utils/doctorContextWarnings.ts +265 -0
  507. package/utils/doctorDiagnostic.ts +625 -0
  508. package/utils/dxt/helpers.ts +88 -0
  509. package/utils/dxt/zip.ts +226 -0
  510. package/utils/earlyInput.ts +191 -0
  511. package/utils/editor.ts +183 -0
  512. package/utils/effort.ts +329 -0
  513. package/utils/embeddedTools.ts +29 -0
  514. package/utils/env.ts +347 -0
  515. package/utils/envDynamic.ts +151 -0
  516. package/utils/envUtils.ts +183 -0
  517. package/utils/envValidation.ts +38 -0
  518. package/utils/errorLogSink.ts +235 -0
  519. package/utils/errors.ts +238 -0
  520. package/utils/exampleCommands.ts +184 -0
  521. package/utils/execFileNoThrow.ts +150 -0
  522. package/utils/execFileNoThrowPortable.ts +89 -0
  523. package/utils/execSyncWrapper.ts +38 -0
  524. package/utils/exportRenderer.tsx +98 -0
  525. package/utils/extraUsage.ts +23 -0
  526. package/utils/fastMode.ts +532 -0
  527. package/utils/file.ts +584 -0
  528. package/utils/fileHistory.ts +1115 -0
  529. package/utils/fileOperationAnalytics.ts +71 -0
  530. package/utils/filePersistence/filePersistence.ts +287 -0
  531. package/utils/filePersistence/outputsScanner.ts +126 -0
  532. package/utils/fileRead.ts +102 -0
  533. package/utils/fileReadCache.ts +96 -0
  534. package/utils/fileStateCache.ts +142 -0
  535. package/utils/findExecutable.ts +17 -0
  536. package/utils/fingerprint.ts +76 -0
  537. package/utils/forkedAgent.ts +689 -0
  538. package/utils/format.ts +308 -0
  539. package/utils/formatBriefTimestamp.ts +81 -0
  540. package/utils/fpsTracker.ts +47 -0
  541. package/utils/frontmatterParser.ts +370 -0
  542. package/utils/fsOperations.ts +770 -0
  543. package/utils/fullscreen.ts +202 -0
  544. package/utils/generatedFiles.ts +136 -0
  545. package/utils/generators.ts +88 -0
  546. package/utils/genericProcessUtils.ts +184 -0
  547. package/utils/getWorktreePaths.ts +70 -0
  548. package/utils/getWorktreePathsPortable.ts +27 -0
  549. package/utils/ghPrStatus.ts +106 -0
  550. package/utils/git/gitConfigParser.ts +277 -0
  551. package/utils/git/gitFilesystem.ts +699 -0
  552. package/utils/git/gitignore.ts +99 -0
  553. package/utils/git.ts +926 -0
  554. package/utils/gitDiff.ts +532 -0
  555. package/utils/gitSettings.ts +18 -0
  556. package/utils/github/ghAuthStatus.ts +29 -0
  557. package/utils/githubRepoPathMapping.ts +162 -0
  558. package/utils/glob.ts +130 -0
  559. package/utils/gracefulShutdown.ts +529 -0
  560. package/utils/groupToolUses.ts +182 -0
  561. package/utils/handlePromptSubmit.ts +610 -0
  562. package/utils/hash.ts +46 -0
  563. package/utils/headlessProfiler.ts +178 -0
  564. package/utils/heapDumpService.ts +303 -0
  565. package/utils/heatmap.ts +198 -0
  566. package/utils/highlightMatch.tsx +28 -0
  567. package/utils/hooks/AsyncHookRegistry.ts +309 -0
  568. package/utils/hooks/apiQueryHookHelper.ts +141 -0
  569. package/utils/hooks/execAgentHook.ts +339 -0
  570. package/utils/hooks/execHttpHook.ts +242 -0
  571. package/utils/hooks/execPromptHook.ts +211 -0
  572. package/utils/hooks/fileChangedWatcher.ts +191 -0
  573. package/utils/hooks/hookEvents.ts +192 -0
  574. package/utils/hooks/hookHelpers.ts +83 -0
  575. package/utils/hooks/hooksConfigManager.ts +400 -0
  576. package/utils/hooks/hooksConfigSnapshot.ts +133 -0
  577. package/utils/hooks/hooksSettings.ts +271 -0
  578. package/utils/hooks/postSamplingHooks.ts +70 -0
  579. package/utils/hooks/registerFrontmatterHooks.ts +67 -0
  580. package/utils/hooks/registerSkillHooks.ts +64 -0
  581. package/utils/hooks/sessionHooks.ts +447 -0
  582. package/utils/hooks/skillImprovement.ts +267 -0
  583. package/utils/hooks/ssrfGuard.ts +294 -0
  584. package/utils/hooks.ts +5022 -0
  585. package/utils/horizontalScroll.ts +137 -0
  586. package/utils/http.ts +136 -0
  587. package/utils/hyperlink.ts +39 -0
  588. package/utils/iTermBackup.ts +73 -0
  589. package/utils/ide.ts +1494 -0
  590. package/utils/idePathConversion.ts +90 -0
  591. package/utils/idleTimeout.ts +53 -0
  592. package/utils/imagePaste.ts +416 -0
  593. package/utils/imageResizer.ts +880 -0
  594. package/utils/imageStore.ts +167 -0
  595. package/utils/imageValidation.ts +104 -0
  596. package/utils/immediateCommand.ts +15 -0
  597. package/utils/inProcessTeammateHelpers.ts +102 -0
  598. package/utils/ink.ts +26 -0
  599. package/utils/intl.ts +94 -0
  600. package/utils/jetbrains.ts +191 -0
  601. package/utils/json.ts +277 -0
  602. package/utils/jsonRead.ts +16 -0
  603. package/utils/keyboardShortcuts.ts +14 -0
  604. package/utils/lazySchema.ts +8 -0
  605. package/utils/listSessionsImpl.ts +454 -0
  606. package/utils/localInstaller.ts +162 -0
  607. package/utils/lockfile.ts +43 -0
  608. package/utils/log.ts +362 -0
  609. package/utils/logoV2Utils.ts +347 -0
  610. package/utils/mailbox.ts +73 -0
  611. package/utils/managedEnv.ts +199 -0
  612. package/utils/managedEnvConstants.ts +191 -0
  613. package/utils/markdown.ts +381 -0
  614. package/utils/markdownConfigLoader.ts +600 -0
  615. package/utils/mcp/dateTimeParser.ts +121 -0
  616. package/utils/mcp/elicitationValidation.ts +336 -0
  617. package/utils/mcpInstructionsDelta.ts +130 -0
  618. package/utils/mcpOutputStorage.ts +189 -0
  619. package/utils/mcpValidation.ts +208 -0
  620. package/utils/mcpWebSocketTransport.ts +200 -0
  621. package/utils/memoize.ts +269 -0
  622. package/utils/memory/types.ts +12 -0
  623. package/utils/memory/versions.ts +8 -0
  624. package/utils/memoryFileDetection.ts +289 -0
  625. package/utils/messagePredicates.ts +8 -0
  626. package/utils/messageQueueManager.ts +547 -0
  627. package/utils/messages/mappers.ts +290 -0
  628. package/utils/messages/systemInit.ts +96 -0
  629. package/utils/messages.ts +5520 -0
  630. package/utils/model/agent.ts +157 -0
  631. package/utils/model/aliases.ts +35 -0
  632. package/utils/model/antModels.ts +64 -0
  633. package/utils/model/bedrock.ts +265 -0
  634. package/utils/model/check1mAccess.ts +72 -0
  635. package/utils/model/configs.ts +158 -0
  636. package/utils/model/contextWindowUpgradeCheck.ts +47 -0
  637. package/utils/model/deprecation.ts +101 -0
  638. package/utils/model/model.ts +654 -0
  639. package/utils/model/modelAllowlist.ts +170 -0
  640. package/utils/model/modelCapabilities.ts +118 -0
  641. package/utils/model/modelOptions.ts +589 -0
  642. package/utils/model/modelStrings.ts +170 -0
  643. package/utils/model/modelSupportOverrides.ts +50 -0
  644. package/utils/model/providers.ts +42 -0
  645. package/utils/model/validateModel.ts +159 -0
  646. package/utils/modelCost.ts +231 -0
  647. package/utils/modifiers.ts +36 -0
  648. package/utils/mtls.ts +179 -0
  649. package/utils/nativeInstaller/download.ts +523 -0
  650. package/utils/nativeInstaller/index.ts +18 -0
  651. package/utils/nativeInstaller/installer.ts +1708 -0
  652. package/utils/nativeInstaller/packageManagers.ts +336 -0
  653. package/utils/nativeInstaller/pidLock.ts +433 -0
  654. package/utils/notebook.ts +224 -0
  655. package/utils/objectGroupBy.ts +18 -0
  656. package/utils/pasteStore.ts +104 -0
  657. package/utils/path.ts +155 -0
  658. package/utils/pdf.ts +300 -0
  659. package/utils/pdfUtils.ts +70 -0
  660. package/utils/peerAddress.ts +21 -0
  661. package/utils/permissions/PermissionMode.ts +141 -0
  662. package/utils/permissions/PermissionPromptToolResultSchema.ts +127 -0
  663. package/utils/permissions/PermissionResult.ts +35 -0
  664. package/utils/permissions/PermissionRule.ts +40 -0
  665. package/utils/permissions/PermissionUpdate.ts +389 -0
  666. package/utils/permissions/PermissionUpdateSchema.ts +78 -0
  667. package/utils/permissions/autoModeState.ts +39 -0
  668. package/utils/permissions/bashClassifier.ts +61 -0
  669. package/utils/permissions/bypassPermissionsKillswitch.ts +155 -0
  670. package/utils/permissions/classifierDecision.ts +98 -0
  671. package/utils/permissions/classifierShared.ts +39 -0
  672. package/utils/permissions/dangerousPatterns.ts +80 -0
  673. package/utils/permissions/denialTracking.ts +45 -0
  674. package/utils/permissions/filesystem.ts +1777 -0
  675. package/utils/permissions/getNextPermissionMode.ts +101 -0
  676. package/utils/permissions/pathValidation.ts +485 -0
  677. package/utils/permissions/permissionExplainer.ts +250 -0
  678. package/utils/permissions/permissionRuleParser.ts +198 -0
  679. package/utils/permissions/permissionSetup.ts +1532 -0
  680. package/utils/permissions/permissions.ts +1486 -0
  681. package/utils/permissions/permissionsLoader.ts +296 -0
  682. package/utils/permissions/shadowedRuleDetection.ts +234 -0
  683. package/utils/permissions/shellRuleMatching.ts +228 -0
  684. package/utils/permissions/yoloClassifier.ts +1495 -0
  685. package/utils/planModeV2.ts +95 -0
  686. package/utils/plans.ts +397 -0
  687. package/utils/platform.ts +150 -0
  688. package/utils/plugins/addDirPluginSettings.ts +71 -0
  689. package/utils/plugins/cacheUtils.ts +196 -0
  690. package/utils/plugins/dependencyResolver.ts +305 -0
  691. package/utils/plugins/fetchTelemetry.ts +135 -0
  692. package/utils/plugins/gitAvailability.ts +69 -0
  693. package/utils/plugins/headlessPluginInstall.ts +174 -0
  694. package/utils/plugins/hintRecommendation.ts +164 -0
  695. package/utils/plugins/installCounts.ts +292 -0
  696. package/utils/plugins/installedPluginsManager.ts +1268 -0
  697. package/utils/plugins/loadPluginAgents.ts +348 -0
  698. package/utils/plugins/loadPluginCommands.ts +946 -0
  699. package/utils/plugins/loadPluginHooks.ts +287 -0
  700. package/utils/plugins/loadPluginOutputStyles.ts +178 -0
  701. package/utils/plugins/lspPluginIntegration.ts +387 -0
  702. package/utils/plugins/lspRecommendation.ts +374 -0
  703. package/utils/plugins/managedPlugins.ts +27 -0
  704. package/utils/plugins/marketplaceHelpers.ts +592 -0
  705. package/utils/plugins/marketplaceManager.ts +2643 -0
  706. package/utils/plugins/mcpPluginIntegration.ts +634 -0
  707. package/utils/plugins/mcpbHandler.ts +968 -0
  708. package/utils/plugins/officialMarketplace.ts +25 -0
  709. package/utils/plugins/officialMarketplaceGcs.ts +216 -0
  710. package/utils/plugins/officialMarketplaceStartupCheck.ts +439 -0
  711. package/utils/plugins/orphanedPluginFilter.ts +114 -0
  712. package/utils/plugins/parseMarketplaceInput.ts +162 -0
  713. package/utils/plugins/performStartupChecks.tsx +70 -0
  714. package/utils/plugins/pluginAutoupdate.ts +284 -0
  715. package/utils/plugins/pluginBlocklist.ts +127 -0
  716. package/utils/plugins/pluginDirectories.ts +178 -0
  717. package/utils/plugins/pluginFlagging.ts +208 -0
  718. package/utils/plugins/pluginIdentifier.ts +123 -0
  719. package/utils/plugins/pluginInstallationHelpers.ts +595 -0
  720. package/utils/plugins/pluginLoader.ts +3302 -0
  721. package/utils/plugins/pluginOptionsStorage.ts +400 -0
  722. package/utils/plugins/pluginPolicy.ts +20 -0
  723. package/utils/plugins/pluginStartupCheck.ts +341 -0
  724. package/utils/plugins/pluginVersioning.ts +157 -0
  725. package/utils/plugins/reconciler.ts +265 -0
  726. package/utils/plugins/refresh.ts +215 -0
  727. package/utils/plugins/schemas.ts +1681 -0
  728. package/utils/plugins/validatePlugin.ts +903 -0
  729. package/utils/plugins/walkPluginMarkdown.ts +69 -0
  730. package/utils/plugins/zipCache.ts +406 -0
  731. package/utils/plugins/zipCacheAdapters.ts +164 -0
  732. package/utils/powershell/dangerousCmdlets.ts +185 -0
  733. package/utils/powershell/parser.ts +1804 -0
  734. package/utils/powershell/staticPrefix.ts +316 -0
  735. package/utils/preflightChecks.tsx +151 -0
  736. package/utils/privacyLevel.ts +55 -0
  737. package/utils/process.ts +68 -0
  738. package/utils/processUserInput/processBashCommand.tsx +140 -0
  739. package/utils/processUserInput/processSlashCommand.tsx +922 -0
  740. package/utils/processUserInput/processTextPrompt.ts +100 -0
  741. package/utils/processUserInput/processUserInput.ts +605 -0
  742. package/utils/profilerBase.ts +46 -0
  743. package/utils/promptCategory.ts +49 -0
  744. package/utils/promptEditor.ts +188 -0
  745. package/utils/promptShellExecution.ts +183 -0
  746. package/utils/proxy.ts +426 -0
  747. package/utils/queryContext.ts +179 -0
  748. package/utils/queryHelpers.ts +552 -0
  749. package/utils/queryProfiler.ts +301 -0
  750. package/utils/queueProcessor.ts +95 -0
  751. package/utils/readEditContext.ts +227 -0
  752. package/utils/readFileInRange.ts +383 -0
  753. package/utils/releaseNotes.ts +360 -0
  754. package/utils/renderOptions.ts +113 -0
  755. package/utils/ripgrep.ts +679 -0
  756. package/utils/sandbox/sandbox-adapter.ts +985 -0
  757. package/utils/sandbox/sandbox-ui-utils.ts +12 -0
  758. package/utils/sanitization.ts +91 -0
  759. package/utils/screenshotClipboard.ts +121 -0
  760. package/utils/sdkEventQueue.ts +134 -0
  761. package/utils/secureStorage/fallbackStorage.ts +70 -0
  762. package/utils/secureStorage/index.ts +17 -0
  763. package/utils/secureStorage/keychainPrefetch.ts +116 -0
  764. package/utils/secureStorage/macOsKeychainHelpers.ts +111 -0
  765. package/utils/secureStorage/macOsKeychainStorage.ts +231 -0
  766. package/utils/secureStorage/plainTextStorage.ts +84 -0
  767. package/utils/semanticBoolean.ts +29 -0
  768. package/utils/semanticNumber.ts +36 -0
  769. package/utils/semver.ts +59 -0
  770. package/utils/sequential.ts +56 -0
  771. package/utils/sessionActivity.ts +133 -0
  772. package/utils/sessionEnvVars.ts +22 -0
  773. package/utils/sessionEnvironment.ts +166 -0
  774. package/utils/sessionFileAccessHooks.ts +250 -0
  775. package/utils/sessionIngressAuth.ts +140 -0
  776. package/utils/sessionRestore.ts +551 -0
  777. package/utils/sessionStart.ts +232 -0
  778. package/utils/sessionState.ts +150 -0
  779. package/utils/sessionStorage.ts +5105 -0
  780. package/utils/sessionStoragePortable.ts +793 -0
  781. package/utils/sessionTitle.ts +129 -0
  782. package/utils/sessionUrl.ts +64 -0
  783. package/utils/set.ts +53 -0
  784. package/utils/settings/allErrors.ts +32 -0
  785. package/utils/settings/applySettingsChange.ts +92 -0
  786. package/utils/settings/changeDetector.ts +488 -0
  787. package/utils/settings/constants.ts +202 -0
  788. package/utils/settings/internalWrites.ts +37 -0
  789. package/utils/settings/managedPath.ts +34 -0
  790. package/utils/settings/mdm/constants.ts +81 -0
  791. package/utils/settings/mdm/rawRead.ts +130 -0
  792. package/utils/settings/mdm/settings.ts +316 -0
  793. package/utils/settings/permissionValidation.ts +262 -0
  794. package/utils/settings/pluginOnlyPolicy.ts +60 -0
  795. package/utils/settings/schemaOutput.ts +8 -0
  796. package/utils/settings/settings.ts +1015 -0
  797. package/utils/settings/settingsCache.ts +80 -0
  798. package/utils/settings/toolValidationConfig.ts +103 -0
  799. package/utils/settings/types.ts +1149 -0
  800. package/utils/settings/validateEditTool.ts +45 -0
  801. package/utils/settings/validation.ts +265 -0
  802. package/utils/settings/validationTips.ts +164 -0
  803. package/utils/shell/bashProvider.ts +255 -0
  804. package/utils/shell/outputLimits.ts +14 -0
  805. package/utils/shell/powershellDetection.ts +107 -0
  806. package/utils/shell/powershellProvider.ts +123 -0
  807. package/utils/shell/prefix.ts +367 -0
  808. package/utils/shell/readOnlyCommandValidation.ts +1893 -0
  809. package/utils/shell/resolveDefaultShell.ts +14 -0
  810. package/utils/shell/shellProvider.ts +33 -0
  811. package/utils/shell/shellToolUtils.ts +22 -0
  812. package/utils/shell/specPrefix.ts +241 -0
  813. package/utils/shellConfig.ts +167 -0
  814. package/utils/sideQuery.ts +222 -0
  815. package/utils/sideQuestion.ts +155 -0
  816. package/utils/signal.ts +43 -0
  817. package/utils/sinks.ts +16 -0
  818. package/utils/skills/skillChangeDetector.ts +311 -0
  819. package/utils/slashCommandParsing.ts +60 -0
  820. package/utils/sleep.ts +84 -0
  821. package/utils/sliceAnsi.ts +91 -0
  822. package/utils/slowOperations.ts +286 -0
  823. package/utils/standaloneAgent.ts +23 -0
  824. package/utils/startupProfiler.ts +194 -0
  825. package/utils/staticRender.tsx +116 -0
  826. package/utils/stats.ts +1061 -0
  827. package/utils/statsCache.ts +434 -0
  828. package/utils/status.tsx +362 -0
  829. package/utils/statusNoticeDefinitions.tsx +198 -0
  830. package/utils/statusNoticeHelpers.ts +20 -0
  831. package/utils/stream.ts +76 -0
  832. package/utils/streamJsonStdoutGuard.ts +123 -0
  833. package/utils/streamlinedTransform.ts +201 -0
  834. package/utils/stringUtils.ts +235 -0
  835. package/utils/subprocessEnv.ts +99 -0
  836. package/utils/suggestions/commandSuggestions.ts +567 -0
  837. package/utils/suggestions/directoryCompletion.ts +263 -0
  838. package/utils/suggestions/shellHistoryCompletion.ts +119 -0
  839. package/utils/suggestions/skillUsageTracking.ts +55 -0
  840. package/utils/suggestions/slackChannelSuggestions.ts +209 -0
  841. package/utils/swarm/It2SetupPrompt.tsx +380 -0
  842. package/utils/swarm/backends/ITermBackend.ts +370 -0
  843. package/utils/swarm/backends/InProcessBackend.ts +339 -0
  844. package/utils/swarm/backends/PaneBackendExecutor.ts +354 -0
  845. package/utils/swarm/backends/TmuxBackend.ts +764 -0
  846. package/utils/swarm/backends/detection.ts +128 -0
  847. package/utils/swarm/backends/it2Setup.ts +245 -0
  848. package/utils/swarm/backends/registry.ts +464 -0
  849. package/utils/swarm/backends/teammateModeSnapshot.ts +87 -0
  850. package/utils/swarm/backends/types.ts +311 -0
  851. package/utils/swarm/constants.ts +33 -0
  852. package/utils/swarm/inProcessRunner.ts +1552 -0
  853. package/utils/swarm/leaderPermissionBridge.ts +54 -0
  854. package/utils/swarm/permissionSync.ts +928 -0
  855. package/utils/swarm/reconnection.ts +119 -0
  856. package/utils/swarm/spawnInProcess.ts +328 -0
  857. package/utils/swarm/spawnUtils.ts +146 -0
  858. package/utils/swarm/teamHelpers.ts +683 -0
  859. package/utils/swarm/teammateInit.ts +129 -0
  860. package/utils/swarm/teammateLayoutManager.ts +107 -0
  861. package/utils/swarm/teammateModel.ts +10 -0
  862. package/utils/swarm/teammatePromptAddendum.ts +18 -0
  863. package/utils/systemDirectories.ts +74 -0
  864. package/utils/systemPrompt.ts +123 -0
  865. package/utils/systemPromptType.ts +14 -0
  866. package/utils/systemTheme.ts +119 -0
  867. package/utils/taggedId.ts +54 -0
  868. package/utils/task/TaskOutput.ts +390 -0
  869. package/utils/task/diskOutput.ts +451 -0
  870. package/utils/task/framework.ts +308 -0
  871. package/utils/task/outputFormatting.ts +38 -0
  872. package/utils/task/sdkProgress.ts +36 -0
  873. package/utils/tasks.ts +862 -0
  874. package/utils/teamDiscovery.ts +81 -0
  875. package/utils/teamMemoryOps.ts +88 -0
  876. package/utils/teammate.ts +292 -0
  877. package/utils/teammateContext.ts +96 -0
  878. package/utils/teammateMailbox.ts +1183 -0
  879. package/utils/telemetry/betaSessionTracing.ts +491 -0
  880. package/utils/telemetry/bigqueryExporter.ts +252 -0
  881. package/utils/telemetry/events.ts +75 -0
  882. package/utils/telemetry/instrumentation.ts +825 -0
  883. package/utils/telemetry/logger.ts +26 -0
  884. package/utils/telemetry/perfettoTracing.ts +1120 -0
  885. package/utils/telemetry/pluginTelemetry.ts +289 -0
  886. package/utils/telemetry/sessionTracing.ts +927 -0
  887. package/utils/telemetry/skillLoadedEvent.ts +39 -0
  888. package/utils/telemetryAttributes.ts +71 -0
  889. package/utils/teleport/api.ts +466 -0
  890. package/utils/teleport/environmentSelection.ts +77 -0
  891. package/utils/teleport/environments.ts +120 -0
  892. package/utils/teleport/gitBundle.ts +292 -0
  893. package/utils/teleport.tsx +1226 -0
  894. package/utils/tempfile.ts +31 -0
  895. package/utils/terminal.ts +131 -0
  896. package/utils/terminalPanel.ts +191 -0
  897. package/utils/textHighlighting.ts +166 -0
  898. package/utils/theme.ts +639 -0
  899. package/utils/thinking.ts +162 -0
  900. package/utils/timeouts.ts +39 -0
  901. package/utils/tmuxSocket.ts +427 -0
  902. package/utils/todo/types.ts +18 -0
  903. package/utils/tokenBudget.ts +73 -0
  904. package/utils/tokens.ts +261 -0
  905. package/utils/toolErrors.ts +132 -0
  906. package/utils/toolPool.ts +79 -0
  907. package/utils/toolResultStorage.ts +1040 -0
  908. package/utils/toolSchemaCache.ts +26 -0
  909. package/utils/toolSearch.ts +756 -0
  910. package/utils/transcriptSearch.ts +202 -0
  911. package/utils/treeify.ts +170 -0
  912. package/utils/truncate.ts +179 -0
  913. package/utils/ultraplan/ccrSession.ts +349 -0
  914. package/utils/ultraplan/keyword.ts +127 -0
  915. package/utils/ultraplan/prompt.txt +1 -0
  916. package/utils/unaryLogging.ts +39 -0
  917. package/utils/undercover.ts +89 -0
  918. package/utils/user.ts +194 -0
  919. package/utils/userAgent.ts +10 -0
  920. package/utils/userPromptKeywords.ts +27 -0
  921. package/utils/uuid.ts +27 -0
  922. package/utils/warningHandler.ts +121 -0
  923. package/utils/which.ts +82 -0
  924. package/utils/windowsPaths.ts +173 -0
  925. package/utils/withResolvers.ts +13 -0
  926. package/utils/words.ts +800 -0
  927. package/utils/workloadContext.ts +57 -0
  928. package/utils/worktree.ts +1519 -0
  929. package/utils/worktreeModeEnabled.ts +11 -0
  930. package/utils/xdg.ts +65 -0
  931. package/utils/xmem.ts +6 -0
  932. package/utils/xml.ts +16 -0
  933. package/utils/yaml.ts +15 -0
  934. package/utils/zodToJsonSchema.ts +23 -0
@@ -0,0 +1,4091 @@
1
+ // biome-ignore-all assist/source/organizeImports: ANT-ONLY import markers must not be reordered
2
+ import {
3
+ logEvent,
4
+ type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
5
+ } from 'src/services/analytics/index.js'
6
+ import {
7
+ toolMatchesName,
8
+ type Tools,
9
+ type ToolUseContext,
10
+ type ToolPermissionContext,
11
+ } from '../Tool.js'
12
+ import {
13
+ FileReadTool,
14
+ MaxFileReadTokenExceededError,
15
+ type Output as FileReadToolOutput,
16
+ readImageWithTokenBudget,
17
+ } from '../tools/FileReadTool/FileReadTool.js'
18
+ import { FileTooLargeError, readFileInRange } from './readFileInRange.js'
19
+ import { expandPath } from './path.js'
20
+ import { countCharInString } from './stringUtils.js'
21
+ import { count, uniq } from './array.js'
22
+ import { getFsImplementation } from './fsOperations.js'
23
+ import { readdir, stat } from 'fs/promises'
24
+ import type { IDESelection } from '../hooks/useIdeSelection.js'
25
+ import { TODO_WRITE_TOOL_NAME } from '../tools/TodoWriteTool/constants.js'
26
+ import { TASK_CREATE_TOOL_NAME } from '../tools/TaskCreateTool/constants.js'
27
+ import { TASK_UPDATE_TOOL_NAME } from '../tools/TaskUpdateTool/constants.js'
28
+ import { BASH_TOOL_NAME } from '../tools/BashTool/toolName.js'
29
+ import { SKILL_TOOL_NAME } from '../tools/SkillTool/constants.js'
30
+ import type { TodoList } from './todo/types.js'
31
+ import {
32
+ type Task,
33
+ listTasks,
34
+ getTaskListId,
35
+ isTodoV2Enabled,
36
+ } from './tasks.js'
37
+ import { getPlanFilePath, getPlan } from './plans.js'
38
+ import { getConnectedIdeName } from './ide.js'
39
+ import {
40
+ filterInjectedMemoryFiles,
41
+ getManagedAndUserConditionalRules,
42
+ getMemoryFiles,
43
+ getMemoryFilesForNestedDirectory,
44
+ getConditionalRulesForCwdLevelDirectory,
45
+ type MemoryFileInfo,
46
+ } from './claudemd.js'
47
+ import { dirname, parse, relative, resolve } from 'path'
48
+ import { getCwd } from 'src/utils/cwd.js'
49
+ import { getViewedTeammateTask } from '../state/selectors.js'
50
+ import { logError } from './log.js'
51
+ import { logAntError } from './debug.js'
52
+ import { isENOENT, toError } from './errors.js'
53
+ import type { DiagnosticFile } from '../services/diagnosticTracking.js'
54
+ import { diagnosticTracker } from '../services/diagnosticTracking.js'
55
+ import type {
56
+ AttachmentMessage,
57
+ Message,
58
+ MessageOrigin,
59
+ } from 'src/types/message.js'
60
+ import {
61
+ type QueuedCommand,
62
+ getImagePasteIds,
63
+ isValidImagePaste,
64
+ } from 'src/types/textInputTypes.js'
65
+ import { randomUUID, type UUID } from 'crypto'
66
+ import { getSettings_DEPRECATED } from './settings/settings.js'
67
+ import { getSnippetForTwoFileDiff } from 'src/tools/FileEditTool/utils.js'
68
+ import type {
69
+ ContentBlockParam,
70
+ ImageBlockParam,
71
+ Base64ImageSource,
72
+ } from '@anthropic-ai/sdk/resources/messages.mjs'
73
+ import { maybeResizeAndDownsampleImageBlock } from './imageResizer.js'
74
+ import type { PastedContent } from './config.js'
75
+ import { getGlobalConfig } from './config.js'
76
+ import {
77
+ getDefaultSonnetModel,
78
+ getDefaultHaikuModel,
79
+ getDefaultOpusModel,
80
+ } from './model/model.js'
81
+ import type { ReadResourceResult } from '@modelcontextprotocol/sdk/types.js'
82
+ import { getSkillToolCommands, getMcpSkillCommands } from '../commands.js'
83
+ import type { Command } from '../types/command.js'
84
+ import uniqBy from 'lodash-es/uniqBy.js'
85
+ import { getProjectRoot } from '../bootstrap/state.js'
86
+ import { formatCommandsWithinBudget } from '../tools/SkillTool/prompt.js'
87
+ import { getContextWindowForModel } from './context.js'
88
+ import type { DiscoverySignal } from '../services/skillSearch/signals.js'
89
+ // Conditional require for DCE. All skill-search string literals that would
90
+ // otherwise leak into external builds live inside these modules. The only
91
+ // surfaces in THIS file are: the maybe() call (gated via spread below) and
92
+ // the skill_listing suppression check (uses the same skillSearchModules null
93
+ // check). The type-only DiscoverySignal import above is erased at compile time.
94
+ /* eslint-disable @typescript-eslint/no-require-imports */
95
+ const skillSearchModules = feature('EXPERIMENTAL_SKILL_SEARCH')
96
+ ? {
97
+ featureCheck:
98
+ require('../services/skillSearch/featureCheck.js') as typeof import('../services/skillSearch/featureCheck.js'),
99
+ prefetch:
100
+ require('../services/skillSearch/prefetch.js') as typeof import('../services/skillSearch/prefetch.js'),
101
+ }
102
+ : null
103
+ const autoModeStateModule = feature('TRANSCRIPT_CLASSIFIER')
104
+ ? (require('./permissions/autoModeState.js') as typeof import('./permissions/autoModeState.js'))
105
+ : null
106
+ /* eslint-enable @typescript-eslint/no-require-imports */
107
+ import {
108
+ MAX_LINES_TO_READ,
109
+ FILE_READ_TOOL_NAME,
110
+ } from 'src/tools/FileReadTool/prompt.js'
111
+ import { getDefaultFileReadingLimits } from 'src/tools/FileReadTool/limits.js'
112
+ import { cacheKeys, type FileStateCache } from './fileStateCache.js'
113
+ import {
114
+ createAbortController,
115
+ createChildAbortController,
116
+ } from './abortController.js'
117
+ import { isAbortError } from './errors.js'
118
+ import {
119
+ getFileModificationTimeAsync,
120
+ isFileWithinReadSizeLimit,
121
+ } from './file.js'
122
+ import type { AgentDefinition } from '../tools/AgentTool/loadAgentsDir.js'
123
+ import { filterAgentsByMcpRequirements } from '../tools/AgentTool/loadAgentsDir.js'
124
+ import { AGENT_TOOL_NAME } from '../tools/AgentTool/constants.js'
125
+ import {
126
+ formatAgentLine,
127
+ shouldInjectAgentListInMessages,
128
+ } from '../tools/AgentTool/prompt.js'
129
+ import { filterDeniedAgents } from './permissions/permissions.js'
130
+ import { getSubscriptionType } from './auth.js'
131
+ import { mcpInfoFromString } from '../services/mcp/mcpStringUtils.js'
132
+ import {
133
+ matchingRuleForInput,
134
+ pathInAllowedWorkingPath,
135
+ } from './permissions/filesystem.js'
136
+ import {
137
+ generateTaskAttachments,
138
+ applyTaskOffsetsAndEvictions,
139
+ } from './task/framework.js'
140
+ import { getTaskOutputPath } from './task/diskOutput.js'
141
+ import { drainPendingMessages } from '../tasks/LocalAgentTask/LocalAgentTask.js'
142
+ import type { TaskType, TaskStatus } from '../Task.js'
143
+ import {
144
+ getOriginalCwd,
145
+ getSessionId,
146
+ getSdkBetas,
147
+ getTotalCostUSD,
148
+ getTotalOutputTokens,
149
+ getCurrentTurnTokenBudget,
150
+ getTurnOutputTokens,
151
+ hasExitedPlanModeInSession,
152
+ setHasExitedPlanMode,
153
+ needsPlanModeExitAttachment,
154
+ setNeedsPlanModeExitAttachment,
155
+ needsAutoModeExitAttachment,
156
+ setNeedsAutoModeExitAttachment,
157
+ getLastEmittedDate,
158
+ setLastEmittedDate,
159
+ getKairosActive,
160
+ } from '../bootstrap/state.js'
161
+ import type { QuerySource } from '../constants/querySource.js'
162
+ import {
163
+ getDeferredToolsDelta,
164
+ isDeferredToolsDeltaEnabled,
165
+ isToolSearchEnabledOptimistic,
166
+ isToolSearchToolAvailable,
167
+ modelSupportsToolReference,
168
+ type DeferredToolsDeltaScanContext,
169
+ } from './toolSearch.js'
170
+ import {
171
+ getMcpInstructionsDelta,
172
+ isMcpInstructionsDeltaEnabled,
173
+ type ClientSideInstruction,
174
+ } from './mcpInstructionsDelta.js'
175
+ import { CLAUDE_IN_CHROME_MCP_SERVER_NAME } from './claudeInChrome/common.js'
176
+ import { CHROME_TOOL_SEARCH_INSTRUCTIONS } from './claudeInChrome/prompt.js'
177
+ import type { MCPServerConnection } from '../services/mcp/types.js'
178
+ import type {
179
+ HookEvent,
180
+ SyncHookJSONOutput,
181
+ } from 'src/entrypoints/agentSdkTypes.js'
182
+ import {
183
+ checkForAsyncHookResponses,
184
+ removeDeliveredAsyncHooks,
185
+ } from './hooks/AsyncHookRegistry.js'
186
+ import {
187
+ checkForLSPDiagnostics,
188
+ clearAllLSPDiagnostics,
189
+ } from '../services/lsp/LSPDiagnosticRegistry.js'
190
+ import { logForDebugging } from './debug.js'
191
+ import {
192
+ extractTextContent,
193
+ getUserMessageText,
194
+ isThinkingMessage,
195
+ } from './messages.js'
196
+ import { isHumanTurn } from './messagePredicates.js'
197
+ import { isEnvTruthy, getClaudeConfigHomeDir } from './envUtils.js'
198
+ import { feature } from 'bun:bundle'
199
+ /* eslint-disable @typescript-eslint/no-require-imports */
200
+ const BRIEF_TOOL_NAME: string | null =
201
+ feature('KAIROS') || feature('KAIROS_BRIEF')
202
+ ? (
203
+ require('../tools/BriefTool/prompt.js') as typeof import('../tools/BriefTool/prompt.js')
204
+ ).BRIEF_TOOL_NAME
205
+ : null
206
+ const sessionTranscriptModule = feature('KAIROS')
207
+ ? (require('../services/sessionTranscript/sessionTranscript.js') as typeof import('../services/sessionTranscript/sessionTranscript.js'))
208
+ : null
209
+ /* eslint-enable @typescript-eslint/no-require-imports */
210
+ import { hasUltrathinkKeyword, isUltrathinkEnabled } from './thinking.js'
211
+ import {
212
+ tokenCountFromLastAPIResponse,
213
+ tokenCountWithEstimation,
214
+ } from './tokens.js'
215
+ import {
216
+ getEffectiveContextWindowSize,
217
+ isAutoCompactEnabled,
218
+ } from '../services/compact/autoCompact.js'
219
+ import { getFeatureValue_CACHED_MAY_BE_STALE } from '../services/analytics/growthbook.js'
220
+ import {
221
+ hasInstructionsLoadedHook,
222
+ executeInstructionsLoadedHooks,
223
+ type HookBlockingError,
224
+ type InstructionsMemoryType,
225
+ } from './hooks.js'
226
+ import { jsonStringify } from './slowOperations.js'
227
+ import { isPDFExtension } from './pdfUtils.js'
228
+ import { getLocalISODate } from '../constants/common.js'
229
+ import { getPDFPageCount } from './pdf.js'
230
+ import { PDF_AT_MENTION_INLINE_THRESHOLD } from '../constants/apiLimits.js'
231
+ import { isAgentSwarmsEnabled } from './agentSwarmsEnabled.js'
232
+ import { findRelevantMemories } from '../memdir/findRelevantMemories.js'
233
+ import { memoryAge, memoryFreshnessText } from '../memdir/memoryAge.js'
234
+ import { getAutoMemPath, isAutoMemoryEnabled } from '../memdir/paths.js'
235
+ import { getAgentMemoryDir } from '../tools/AgentTool/agentMemory.js'
236
+ import {
237
+ readUnreadMessages,
238
+ markMessagesAsReadByPredicate,
239
+ isShutdownApproved,
240
+ isStructuredProtocolMessage,
241
+ isIdleNotification,
242
+ } from './teammateMailbox.js'
243
+ import {
244
+ getAgentName,
245
+ getAgentId,
246
+ getTeamName,
247
+ isTeamLead,
248
+ } from './teammate.js'
249
+ import { isInProcessTeammate } from './teammateContext.js'
250
+ import { removeTeammateFromTeamFile } from './swarm/teamHelpers.js'
251
+ import { unassignTeammateTasks } from './tasks.js'
252
+ import { getCompanionIntroAttachment } from '../buddy/prompt.js'
253
+
254
+ export const TODO_REMINDER_CONFIG = {
255
+ TURNS_SINCE_WRITE: 10,
256
+ TURNS_BETWEEN_REMINDERS: 10,
257
+ } as const
258
+
259
+ export const PLAN_MODE_ATTACHMENT_CONFIG = {
260
+ TURNS_BETWEEN_ATTACHMENTS: 5,
261
+ FULL_REMINDER_EVERY_N_ATTACHMENTS: 5,
262
+ } as const
263
+
264
+ export const AUTO_MODE_ATTACHMENT_CONFIG = {
265
+ TURNS_BETWEEN_ATTACHMENTS: 5,
266
+ FULL_REMINDER_EVERY_N_ATTACHMENTS: 5,
267
+ } as const
268
+
269
+ const MAX_MEMORY_LINES = 200
270
+ // Line cap alone doesn't bound size (200 × 500-char lines = 100KB). The
271
+ // surfacer injects up to 5 files per turn via <system-reminder>, bypassing
272
+ // the per-message tool-result budget, so a tight per-file byte cap keeps
273
+ // aggregate injection bounded (5 × 4KB = 20KB/turn). Enforced via
274
+ // readFileInRange's truncateOnByteLimit option. Truncation means the
275
+ // most-relevant memory still surfaces: the frontmatter + opening context
276
+ // is usually what matters.
277
+ const MAX_MEMORY_BYTES = 4096
278
+
279
+ export const RELEVANT_MEMORIES_CONFIG = {
280
+ // Per-turn cap (5 × 4KB = 20KB) bounds a single injection, but over a
281
+ // long session the selector keeps surfacing distinct files — ~26K tokens/
282
+ // session observed in prod. Cap the cumulative bytes: once hit, stop
283
+ // prefetching entirely. Budget is ~3 full injections; after that the
284
+ // most-relevant memories are already in context. Scanning messages
285
+ // (rather than tracking in toolUseContext) means compact naturally
286
+ // resets the counter — old attachments are gone from context, so
287
+ // re-surfacing is valid.
288
+ MAX_SESSION_BYTES: 60 * 1024,
289
+ } as const
290
+
291
+ export const VERIFY_PLAN_REMINDER_CONFIG = {
292
+ TURNS_BETWEEN_REMINDERS: 10,
293
+ } as const
294
+
295
+ export type FileAttachment = {
296
+ type: 'file'
297
+ filename: string
298
+ content: FileReadToolOutput
299
+ /**
300
+ * Whether the file was truncated due to size limits
301
+ */
302
+ truncated?: boolean
303
+ /** Path relative to CWD at creation time, for stable display */
304
+ displayPath: string
305
+ }
306
+
307
+ export type CompactFileReferenceAttachment = {
308
+ type: 'compact_file_reference'
309
+ filename: string
310
+ /** Path relative to CWD at creation time, for stable display */
311
+ displayPath: string
312
+ }
313
+
314
+ export type PDFReferenceAttachment = {
315
+ type: 'pdf_reference'
316
+ filename: string
317
+ pageCount: number
318
+ fileSize: number
319
+ /** Path relative to CWD at creation time, for stable display */
320
+ displayPath: string
321
+ }
322
+
323
+ export type AlreadyReadFileAttachment = {
324
+ type: 'already_read_file'
325
+ filename: string
326
+ content: FileReadToolOutput
327
+ /**
328
+ * Whether the file was truncated due to size limits
329
+ */
330
+ truncated?: boolean
331
+ /** Path relative to CWD at creation time, for stable display */
332
+ displayPath: string
333
+ }
334
+
335
+ export type AgentMentionAttachment = {
336
+ type: 'agent_mention'
337
+ agentType: string
338
+ }
339
+
340
+ export type AsyncHookResponseAttachment = {
341
+ type: 'async_hook_response'
342
+ processId: string
343
+ hookName: string
344
+ hookEvent: HookEvent | 'StatusLine' | 'FileSuggestion'
345
+ toolName?: string
346
+ response: SyncHookJSONOutput
347
+ stdout: string
348
+ stderr: string
349
+ exitCode?: number
350
+ }
351
+
352
+ export type HookAttachment =
353
+ | HookCancelledAttachment
354
+ | {
355
+ type: 'hook_blocking_error'
356
+ blockingError: HookBlockingError
357
+ hookName: string
358
+ toolUseID: string
359
+ hookEvent: HookEvent
360
+ }
361
+ | HookNonBlockingErrorAttachment
362
+ | HookErrorDuringExecutionAttachment
363
+ | {
364
+ type: 'hook_stopped_continuation'
365
+ message: string
366
+ hookName: string
367
+ toolUseID: string
368
+ hookEvent: HookEvent
369
+ }
370
+ | HookSuccessAttachment
371
+ | {
372
+ type: 'hook_additional_context'
373
+ content: string[]
374
+ hookName: string
375
+ toolUseID: string
376
+ hookEvent: HookEvent
377
+ }
378
+ | HookSystemMessageAttachment
379
+ | HookPermissionDecisionAttachment
380
+
381
+ export type HookPermissionDecisionAttachment = {
382
+ type: 'hook_permission_decision'
383
+ decision: 'allow' | 'deny'
384
+ toolUseID: string
385
+ hookEvent: HookEvent
386
+ }
387
+
388
+ export type HookSystemMessageAttachment = {
389
+ type: 'hook_system_message'
390
+ content: string
391
+ hookName: string
392
+ toolUseID: string
393
+ hookEvent: HookEvent
394
+ }
395
+
396
+ export type HookCancelledAttachment = {
397
+ type: 'hook_cancelled'
398
+ hookName: string
399
+ toolUseID: string
400
+ hookEvent: HookEvent
401
+ command?: string
402
+ durationMs?: number
403
+ }
404
+
405
+ export type HookErrorDuringExecutionAttachment = {
406
+ type: 'hook_error_during_execution'
407
+ content: string
408
+ hookName: string
409
+ toolUseID: string
410
+ hookEvent: HookEvent
411
+ command?: string
412
+ durationMs?: number
413
+ }
414
+
415
+ export type HookSuccessAttachment = {
416
+ type: 'hook_success'
417
+ content: string
418
+ hookName: string
419
+ toolUseID: string
420
+ hookEvent: HookEvent
421
+ stdout?: string
422
+ stderr?: string
423
+ exitCode?: number
424
+ command?: string
425
+ durationMs?: number
426
+ }
427
+
428
+ export type HookNonBlockingErrorAttachment = {
429
+ type: 'hook_non_blocking_error'
430
+ hookName: string
431
+ stderr: string
432
+ stdout: string
433
+ exitCode: number
434
+ toolUseID: string
435
+ hookEvent: HookEvent
436
+ command?: string
437
+ durationMs?: number
438
+ }
439
+
440
+ export type Attachment =
441
+ /**
442
+ * User at-mentioned the file
443
+ */
444
+ | FileAttachment
445
+ | CompactFileReferenceAttachment
446
+ | PDFReferenceAttachment
447
+ | AlreadyReadFileAttachment
448
+ /**
449
+ * An at-mentioned file was edited
450
+ */
451
+ | {
452
+ type: 'edited_text_file'
453
+ filename: string
454
+ snippet: string
455
+ }
456
+ | {
457
+ type: 'edited_image_file'
458
+ filename: string
459
+ content: FileReadToolOutput
460
+ }
461
+ | {
462
+ type: 'directory'
463
+ path: string
464
+ content: string
465
+ /** Path relative to CWD at creation time, for stable display */
466
+ displayPath: string
467
+ }
468
+ | {
469
+ type: 'selected_lines_in_ide'
470
+ ideName: string
471
+ lineStart: number
472
+ lineEnd: number
473
+ filename: string
474
+ content: string
475
+ /** Path relative to CWD at creation time, for stable display */
476
+ displayPath: string
477
+ }
478
+ | {
479
+ type: 'opened_file_in_ide'
480
+ filename: string
481
+ }
482
+ | {
483
+ type: 'todo_reminder'
484
+ content: TodoList
485
+ itemCount: number
486
+ }
487
+ | {
488
+ type: 'task_reminder'
489
+ content: Task[]
490
+ itemCount: number
491
+ }
492
+ | {
493
+ type: 'nested_memory'
494
+ path: string
495
+ content: MemoryFileInfo
496
+ /** Path relative to CWD at creation time, for stable display */
497
+ displayPath: string
498
+ }
499
+ | {
500
+ type: 'relevant_memories'
501
+ memories: {
502
+ path: string
503
+ content: string
504
+ mtimeMs: number
505
+ /**
506
+ * Pre-computed header string (age + path prefix). Computed once
507
+ * at attachment-creation time so the rendered bytes are stable
508
+ * across turns — recomputing memoryAge(mtimeMs) at render time
509
+ * calls Date.now(), so "saved 3 days ago" becomes "saved 4 days
510
+ * ago" across turns → different bytes → prompt cache bust.
511
+ * Optional for backward compat with resumed sessions; render
512
+ * path falls back to recomputing if missing.
513
+ */
514
+ header?: string
515
+ /**
516
+ * lineCount when the file was truncated by readMemoriesForSurfacing,
517
+ * else undefined. Threaded to the readFileState write so
518
+ * getChangedFiles skips truncated memories (partial content would
519
+ * yield a misleading diff).
520
+ */
521
+ limit?: number
522
+ }[]
523
+ }
524
+ /**
525
+ * XMem vector memory context - retrieved automatically before each user turn
526
+ */
527
+ | {
528
+ type: 'xmem_context'
529
+ content: string
530
+ query: string
531
+ }
532
+ | {
533
+ type: 'dynamic_skill'
534
+ skillDir: string
535
+ skillNames: string[]
536
+ /** Path relative to CWD at creation time, for stable display */
537
+ displayPath: string
538
+ }
539
+ | {
540
+ type: 'skill_listing'
541
+ content: string
542
+ skillCount: number
543
+ isInitial: boolean
544
+ }
545
+ | {
546
+ type: 'skill_discovery'
547
+ skills: { name: string; description: string; shortId?: string }[]
548
+ signal: DiscoverySignal
549
+ source: 'native' | 'aki' | 'both'
550
+ }
551
+ | {
552
+ type: 'queued_command'
553
+ prompt: string | Array<ContentBlockParam>
554
+ source_uuid?: UUID
555
+ imagePasteIds?: number[]
556
+ /** Original queue mode — 'prompt' for user messages, 'task-notification' for system events */
557
+ commandMode?: string
558
+ /** Provenance carried from QueuedCommand so mid-turn drains preserve it */
559
+ origin?: MessageOrigin
560
+ /** Carried from QueuedCommand.isMeta — distinguishes human-typed from system-injected */
561
+ isMeta?: boolean
562
+ }
563
+ | {
564
+ type: 'output_style'
565
+ style: string
566
+ }
567
+ | {
568
+ type: 'diagnostics'
569
+ files: DiagnosticFile[]
570
+ isNew: boolean
571
+ }
572
+ | {
573
+ type: 'plan_mode'
574
+ reminderType: 'full' | 'sparse'
575
+ isSubAgent?: boolean
576
+ planFilePath: string
577
+ planExists: boolean
578
+ }
579
+ | {
580
+ type: 'plan_mode_reentry'
581
+ planFilePath: string
582
+ }
583
+ | {
584
+ type: 'plan_mode_exit'
585
+ planFilePath: string
586
+ planExists: boolean
587
+ }
588
+ | {
589
+ type: 'auto_mode'
590
+ reminderType: 'full' | 'sparse'
591
+ }
592
+ | {
593
+ type: 'auto_mode_exit'
594
+ }
595
+ | {
596
+ type: 'critical_system_reminder'
597
+ content: string
598
+ }
599
+ | {
600
+ type: 'plan_file_reference'
601
+ planFilePath: string
602
+ planContent: string
603
+ }
604
+ | {
605
+ type: 'mcp_resource'
606
+ server: string
607
+ uri: string
608
+ name: string
609
+ description?: string
610
+ content: ReadResourceResult
611
+ }
612
+ | {
613
+ type: 'command_permissions'
614
+ allowedTools: string[]
615
+ model?: string
616
+ }
617
+ | AgentMentionAttachment
618
+ | {
619
+ type: 'task_status'
620
+ taskId: string
621
+ taskType: TaskType
622
+ status: TaskStatus
623
+ description: string
624
+ deltaSummary: string | null
625
+ outputFilePath?: string
626
+ }
627
+ | AsyncHookResponseAttachment
628
+ | {
629
+ type: 'token_usage'
630
+ used: number
631
+ total: number
632
+ remaining: number
633
+ }
634
+ | {
635
+ type: 'budget_usd'
636
+ used: number
637
+ total: number
638
+ remaining: number
639
+ }
640
+ | {
641
+ type: 'output_token_usage'
642
+ turn: number
643
+ session: number
644
+ budget: number | null
645
+ }
646
+ | {
647
+ type: 'structured_output'
648
+ data: unknown
649
+ }
650
+ | TeammateMailboxAttachment
651
+ | TeamContextAttachment
652
+ | HookAttachment
653
+ | {
654
+ type: 'invoked_skills'
655
+ skills: Array<{
656
+ name: string
657
+ path: string
658
+ content: string
659
+ }>
660
+ }
661
+ | {
662
+ type: 'verify_plan_reminder'
663
+ }
664
+ | {
665
+ type: 'max_turns_reached'
666
+ maxTurns: number
667
+ turnCount: number
668
+ }
669
+ | {
670
+ type: 'current_session_memory'
671
+ content: string
672
+ path: string
673
+ tokenCount: number
674
+ }
675
+ | {
676
+ type: 'teammate_shutdown_batch'
677
+ count: number
678
+ }
679
+ | {
680
+ type: 'compaction_reminder'
681
+ }
682
+ | {
683
+ type: 'context_efficiency'
684
+ }
685
+ | {
686
+ type: 'date_change'
687
+ newDate: string
688
+ }
689
+ | {
690
+ type: 'ultrathink_effort'
691
+ level: 'high'
692
+ }
693
+ | {
694
+ type: 'deferred_tools_delta'
695
+ addedNames: string[]
696
+ addedLines: string[]
697
+ removedNames: string[]
698
+ }
699
+ | {
700
+ type: 'agent_listing_delta'
701
+ addedTypes: string[]
702
+ addedLines: string[]
703
+ removedTypes: string[]
704
+ /** True when this is the first announcement in the conversation */
705
+ isInitial: boolean
706
+ /** Whether to include the "launch multiple agents concurrently" note (non-pro subscriptions) */
707
+ showConcurrencyNote: boolean
708
+ }
709
+ | {
710
+ type: 'mcp_instructions_delta'
711
+ addedNames: string[]
712
+ addedBlocks: string[]
713
+ removedNames: string[]
714
+ }
715
+ | {
716
+ type: 'companion_intro'
717
+ name: string
718
+ species: string
719
+ }
720
+ | {
721
+ type: 'bagel_console'
722
+ errorCount: number
723
+ warningCount: number
724
+ sample: string
725
+ }
726
+
727
+ export type TeammateMailboxAttachment = {
728
+ type: 'teammate_mailbox'
729
+ messages: Array<{
730
+ from: string
731
+ text: string
732
+ timestamp: string
733
+ color?: string
734
+ summary?: string
735
+ }>
736
+ }
737
+
738
+ export type TeamContextAttachment = {
739
+ type: 'team_context'
740
+ agentId: string
741
+ agentName: string
742
+ teamName: string
743
+ teamConfigPath: string
744
+ taskListPath: string
745
+ }
746
+
747
+ /**
748
+ * This is janky
749
+ * TODO: Generate attachments when we create messages
750
+ */
751
+ export async function getAttachments(
752
+ input: string | null,
753
+ toolUseContext: ToolUseContext,
754
+ ideSelection: IDESelection | null,
755
+ queuedCommands: QueuedCommand[],
756
+ messages?: Message[],
757
+ querySource?: QuerySource,
758
+ options?: { skipSkillDiscovery?: boolean },
759
+ ): Promise<Attachment[]> {
760
+ if (
761
+ isEnvTruthy(process.env.CLAUDE_CODE_DISABLE_ATTACHMENTS) ||
762
+ isEnvTruthy(process.env.CLAUDE_CODE_SIMPLE)
763
+ ) {
764
+ // query.ts:removeFromQueue dequeues these unconditionally after
765
+ // getAttachmentMessages runs — returning [] here silently drops them.
766
+ // Coworker runs with --bare and depends on task-notification for
767
+ // mid-tool-call notifications from Local*Task/Remote*Task.
768
+ return getQueuedCommandAttachments(queuedCommands)
769
+ }
770
+
771
+ // This will slow down submissions
772
+ // TODO: Compute attachments as the user types, not here (though we use this
773
+ // function for slash command prompts too)
774
+ const abortController = createAbortController()
775
+ const timeoutId = setTimeout(ac => ac.abort(), 1000, abortController)
776
+ const context = { ...toolUseContext, abortController }
777
+
778
+ const isMainThread = !toolUseContext.agentId
779
+
780
+ // Attachments which are added in response to on user input
781
+ const userInputAttachments = input
782
+ ? [
783
+ maybe('at_mentioned_files', () =>
784
+ processAtMentionedFiles(input, context),
785
+ ),
786
+ maybe('mcp_resources', () =>
787
+ processMcpResourceAttachments(input, context),
788
+ ),
789
+ maybe('agent_mentions', () =>
790
+ Promise.resolve(
791
+ processAgentMentions(
792
+ input,
793
+ toolUseContext.options.agentDefinitions.activeAgents,
794
+ ),
795
+ ),
796
+ ),
797
+ // Skill discovery on turn 0 (user input as signal). Inter-turn
798
+ // discovery runs via startSkillDiscoveryPrefetch in query.ts,
799
+ // gated on write-pivot detection — see skillSearch/prefetch.ts.
800
+ // feature() here lets DCE drop the 'skill_discovery' string (and the
801
+ // function it calls) from external builds.
802
+ //
803
+ // skipSkillDiscovery gates out the SKILL.md-expansion path
804
+ // (getMessagesForPromptSlashCommand). When a skill is invoked, its
805
+ // SKILL.md content is passed as `input` here to extract @-mentions —
806
+ // but that content is NOT user intent and must not trigger discovery.
807
+ // Without this gate, a 110KB SKILL.md fires ~3.3s of chunked AKI
808
+ // queries on every skill invocation (session 13a9afae).
809
+ ...(feature('EXPERIMENTAL_SKILL_SEARCH') &&
810
+ skillSearchModules &&
811
+ !options?.skipSkillDiscovery
812
+ ? [
813
+ maybe('skill_discovery', () =>
814
+ skillSearchModules.prefetch.getTurnZeroSkillDiscovery(
815
+ input,
816
+ messages ?? [],
817
+ context,
818
+ ),
819
+ ),
820
+ ]
821
+ : []),
822
+ ]
823
+ : []
824
+
825
+ // Process user input attachments first (includes @mentioned files)
826
+ // This ensures files are added to nestedMemoryAttachmentTriggers before nested_memory processes them
827
+ const userAttachmentResults = await Promise.all(userInputAttachments)
828
+
829
+ // Thread-safe attachments available in sub-agents
830
+ // NOTE: These must be created AFTER userInputAttachments completes to ensure
831
+ // nestedMemoryAttachmentTriggers is populated before getNestedMemoryAttachments runs
832
+ const allThreadAttachments = [
833
+ // queuedCommands is already agent-scoped by the drain gate in query.ts —
834
+ // main thread gets agentId===undefined, subagents get their own agentId.
835
+ // Must run for all threads or subagent notifications drain into the void
836
+ // (removed from queue by removeFromQueue but never attached).
837
+ maybe('queued_commands', () => getQueuedCommandAttachments(queuedCommands)),
838
+ maybe('date_change', () =>
839
+ Promise.resolve(getDateChangeAttachments(messages)),
840
+ ),
841
+ maybe('ultrathink_effort', () =>
842
+ Promise.resolve(getUltrathinkEffortAttachment(input)),
843
+ ),
844
+ maybe('deferred_tools_delta', () =>
845
+ Promise.resolve(
846
+ getDeferredToolsDeltaAttachment(
847
+ toolUseContext.options.tools,
848
+ toolUseContext.options.mainLoopModel,
849
+ messages,
850
+ {
851
+ callSite: isMainThread
852
+ ? 'attachments_main'
853
+ : 'attachments_subagent',
854
+ querySource,
855
+ },
856
+ ),
857
+ ),
858
+ ),
859
+ maybe('agent_listing_delta', () =>
860
+ Promise.resolve(getAgentListingDeltaAttachment(toolUseContext, messages)),
861
+ ),
862
+ maybe('mcp_instructions_delta', () =>
863
+ Promise.resolve(
864
+ getMcpInstructionsDeltaAttachment(
865
+ toolUseContext.options.mcpClients,
866
+ toolUseContext.options.tools,
867
+ toolUseContext.options.mainLoopModel,
868
+ messages,
869
+ ),
870
+ ),
871
+ ),
872
+ ...(feature('BUDDY')
873
+ ? [
874
+ maybe('companion_intro', () =>
875
+ Promise.resolve(getCompanionIntroAttachment(messages)),
876
+ ),
877
+ ]
878
+ : []),
879
+ maybe('changed_files', () => getChangedFiles(context)),
880
+ maybe('nested_memory', () => getNestedMemoryAttachments(context)),
881
+ // relevant_memories moved to async prefetch (startRelevantMemoryPrefetch)
882
+ maybe('dynamic_skill', () => getDynamicSkillAttachments(context)),
883
+ maybe('skill_listing', () => getSkillListingAttachments(context)),
884
+ // Inter-turn skill discovery now runs via startSkillDiscoveryPrefetch
885
+ // (query.ts, concurrent with the main turn). The blocking call that
886
+ // previously lived here was the assistant_turn signal — 97% of those
887
+ // Haiku calls found nothing in prod. Prefetch + await-at-collection
888
+ // replaces it; see src/services/skillSearch/prefetch.ts.
889
+ maybe('plan_mode', () => getPlanModeAttachments(messages, toolUseContext)),
890
+ maybe('plan_mode_exit', () => getPlanModeExitAttachment(toolUseContext)),
891
+ ...(feature('TRANSCRIPT_CLASSIFIER')
892
+ ? [
893
+ maybe('auto_mode', () =>
894
+ getAutoModeAttachments(messages, toolUseContext),
895
+ ),
896
+ maybe('auto_mode_exit', () =>
897
+ getAutoModeExitAttachment(toolUseContext),
898
+ ),
899
+ ]
900
+ : []),
901
+ maybe('todo_reminders', () =>
902
+ isTodoV2Enabled()
903
+ ? getTaskReminderAttachments(messages, toolUseContext)
904
+ : getTodoReminderAttachments(messages, toolUseContext),
905
+ ),
906
+ ...(isAgentSwarmsEnabled()
907
+ ? [
908
+ // Skip teammate mailbox for the session_memory forked agent.
909
+ // It shares AppState.teamContext with the leader, so isTeamLead resolves
910
+ // true and it reads+marks-as-read the leader's DMs as ephemeral attachments,
911
+ // silently stealing messages that should be delivered as permanent turns.
912
+ ...(querySource === 'session_memory'
913
+ ? []
914
+ : [
915
+ maybe('teammate_mailbox', async () =>
916
+ getTeammateMailboxAttachments(toolUseContext),
917
+ ),
918
+ ]),
919
+ maybe('team_context', async () =>
920
+ getTeamContextAttachment(messages ?? []),
921
+ ),
922
+ ]
923
+ : []),
924
+ maybe('agent_pending_messages', async () =>
925
+ getAgentPendingMessageAttachments(toolUseContext),
926
+ ),
927
+ maybe('critical_system_reminder', () =>
928
+ Promise.resolve(getCriticalSystemReminderAttachment(toolUseContext)),
929
+ ),
930
+ ...(feature('COMPACTION_REMINDERS')
931
+ ? [
932
+ maybe('compaction_reminder', () =>
933
+ Promise.resolve(
934
+ getCompactionReminderAttachment(
935
+ messages ?? [],
936
+ toolUseContext.options.mainLoopModel,
937
+ ),
938
+ ),
939
+ ),
940
+ ]
941
+ : []),
942
+ ...(feature('HISTORY_SNIP')
943
+ ? [
944
+ maybe('context_efficiency', () =>
945
+ Promise.resolve(getContextEfficiencyAttachment(messages ?? [])),
946
+ ),
947
+ ]
948
+ : []),
949
+ ]
950
+
951
+ // Attachments which are semantically only for the main conversation or don't have concurrency-safe implementations
952
+ const mainThreadAttachments = isMainThread
953
+ ? [
954
+ maybe('ide_selection', async () =>
955
+ getSelectedLinesFromIDE(ideSelection, toolUseContext),
956
+ ),
957
+ maybe('ide_opened_file', async () =>
958
+ getOpenedFileFromIDE(ideSelection, toolUseContext),
959
+ ),
960
+ maybe('output_style', async () =>
961
+ Promise.resolve(getOutputStyleAttachment()),
962
+ ),
963
+ maybe('diagnostics', async () =>
964
+ getDiagnosticAttachments(toolUseContext),
965
+ ),
966
+ maybe('lsp_diagnostics', async () =>
967
+ getLSPDiagnosticAttachments(toolUseContext),
968
+ ),
969
+ maybe('unified_tasks', async () =>
970
+ getUnifiedTaskAttachments(toolUseContext),
971
+ ),
972
+ maybe('async_hook_responses', async () =>
973
+ getAsyncHookResponseAttachments(),
974
+ ),
975
+ maybe('token_usage', async () =>
976
+ Promise.resolve(
977
+ getTokenUsageAttachment(
978
+ messages ?? [],
979
+ toolUseContext.options.mainLoopModel,
980
+ ),
981
+ ),
982
+ ),
983
+ maybe('budget_usd', async () =>
984
+ Promise.resolve(
985
+ getMaxBudgetUsdAttachment(toolUseContext.options.maxBudgetUsd),
986
+ ),
987
+ ),
988
+ maybe('output_token_usage', async () =>
989
+ Promise.resolve(getOutputTokenUsageAttachment()),
990
+ ),
991
+ maybe('verify_plan_reminder', async () =>
992
+ getVerifyPlanReminderAttachment(messages, toolUseContext),
993
+ ),
994
+ ]
995
+ : []
996
+
997
+ // Process thread and main thread attachments in parallel (no dependencies between them)
998
+ const [threadAttachmentResults, mainThreadAttachmentResults] =
999
+ await Promise.all([
1000
+ Promise.all(allThreadAttachments),
1001
+ Promise.all(mainThreadAttachments),
1002
+ ])
1003
+
1004
+ clearTimeout(timeoutId)
1005
+ // Defensive: a getter leaking [undefined] crashes .map(a => a.type) below.
1006
+ return [
1007
+ ...userAttachmentResults.flat(),
1008
+ ...threadAttachmentResults.flat(),
1009
+ ...mainThreadAttachmentResults.flat(),
1010
+ ].filter(a => a !== undefined && a !== null)
1011
+ }
1012
+
1013
+ async function maybe<A>(label: string, f: () => Promise<A[]>): Promise<A[]> {
1014
+ const startTime = Date.now()
1015
+ try {
1016
+ const result = await f()
1017
+ const duration = Date.now() - startTime
1018
+ // Log only 5% of events to reduce volume
1019
+ if (Math.random() < 0.05) {
1020
+ // jsonStringify(undefined) returns undefined, so .length would throw
1021
+ const attachmentSizeBytes = result
1022
+ .filter(a => a !== undefined && a !== null)
1023
+ .reduce((total, attachment) => {
1024
+ return total + jsonStringify(attachment).length
1025
+ }, 0)
1026
+ logEvent('tengu_attachment_compute_duration', {
1027
+ label,
1028
+ duration_ms: duration,
1029
+ attachment_size_bytes: attachmentSizeBytes,
1030
+ attachment_count: result.length,
1031
+ } as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS)
1032
+ }
1033
+ return result
1034
+ } catch (e) {
1035
+ const duration = Date.now() - startTime
1036
+ // Log only 5% of events to reduce volume
1037
+ if (Math.random() < 0.05) {
1038
+ logEvent('tengu_attachment_compute_duration', {
1039
+ label,
1040
+ duration_ms: duration,
1041
+ error: true,
1042
+ } as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS)
1043
+ }
1044
+ logError(e)
1045
+ // For Ant users, log the full error to help with debugging
1046
+ logAntError(`Attachment error in ${label}`, e)
1047
+
1048
+ return []
1049
+ }
1050
+ }
1051
+
1052
+ const INLINE_NOTIFICATION_MODES = new Set(['prompt', 'task-notification'])
1053
+
1054
+ export async function getQueuedCommandAttachments(
1055
+ queuedCommands: QueuedCommand[],
1056
+ ): Promise<Attachment[]> {
1057
+ if (!queuedCommands) {
1058
+ return []
1059
+ }
1060
+ // Include both 'prompt' and 'task-notification' commands as attachments.
1061
+ // During proactive agentic loops, task-notification commands would otherwise
1062
+ // stay in the queue permanently (useQueueProcessor can't run while a query
1063
+ // is active), causing hasPendingNotifications() to return true and Sleep to
1064
+ // wake immediately with 0ms duration in an infinite loop.
1065
+ const filtered = queuedCommands.filter(_ =>
1066
+ INLINE_NOTIFICATION_MODES.has(_.mode),
1067
+ )
1068
+ return Promise.all(
1069
+ filtered.map(async _ => {
1070
+ const imageBlocks = await buildImageContentBlocks(_.pastedContents)
1071
+ let prompt: string | Array<ContentBlockParam> = _.value
1072
+ if (imageBlocks.length > 0) {
1073
+ // Build content block array with text + images so the model sees them
1074
+ const textValue =
1075
+ typeof _.value === 'string'
1076
+ ? _.value
1077
+ : extractTextContent(_.value, '\n')
1078
+ prompt = [{ type: 'text' as const, text: textValue }, ...imageBlocks]
1079
+ }
1080
+ return {
1081
+ type: 'queued_command' as const,
1082
+ prompt,
1083
+ source_uuid: _.uuid,
1084
+ imagePasteIds: getImagePasteIds(_.pastedContents),
1085
+ commandMode: _.mode,
1086
+ origin: _.origin,
1087
+ isMeta: _.isMeta,
1088
+ }
1089
+ }),
1090
+ )
1091
+ }
1092
+
1093
+ export function getAgentPendingMessageAttachments(
1094
+ toolUseContext: ToolUseContext,
1095
+ ): Attachment[] {
1096
+ const agentId = toolUseContext.agentId
1097
+ if (!agentId) return []
1098
+ const drained = drainPendingMessages(
1099
+ agentId,
1100
+ toolUseContext.getAppState,
1101
+ toolUseContext.setAppStateForTasks ?? toolUseContext.setAppState,
1102
+ )
1103
+ return drained.map(msg => ({
1104
+ type: 'queued_command' as const,
1105
+ prompt: msg,
1106
+ origin: { kind: 'coordinator' as const },
1107
+ isMeta: true,
1108
+ }))
1109
+ }
1110
+
1111
+ async function buildImageContentBlocks(
1112
+ pastedContents: Record<number, PastedContent> | undefined,
1113
+ ): Promise<ImageBlockParam[]> {
1114
+ if (!pastedContents) {
1115
+ return []
1116
+ }
1117
+ const imageContents = Object.values(pastedContents).filter(isValidImagePaste)
1118
+ if (imageContents.length === 0) {
1119
+ return []
1120
+ }
1121
+ const results = await Promise.all(
1122
+ imageContents.map(async img => {
1123
+ const imageBlock: ImageBlockParam = {
1124
+ type: 'image',
1125
+ source: {
1126
+ type: 'base64',
1127
+ media_type: (img.mediaType ||
1128
+ 'image/png') as Base64ImageSource['media_type'],
1129
+ data: img.content,
1130
+ },
1131
+ }
1132
+ const resized = await maybeResizeAndDownsampleImageBlock(imageBlock)
1133
+ return resized.block
1134
+ }),
1135
+ )
1136
+ return results
1137
+ }
1138
+
1139
+ function getPlanModeAttachmentTurnCount(messages: Message[]): {
1140
+ turnCount: number
1141
+ foundPlanModeAttachment: boolean
1142
+ } {
1143
+ let turnsSinceLastAttachment = 0
1144
+ let foundPlanModeAttachment = false
1145
+
1146
+ // Iterate backwards to find most recent plan_mode attachment.
1147
+ // Count HUMAN turns (non-meta, non-tool-result user messages), not assistant
1148
+ // messages — the tool loop in query.ts calls getAttachmentMessages on every
1149
+ // tool round, so counting assistant messages would fire the reminder every
1150
+ // 5 tool calls instead of every 5 human turns.
1151
+ for (let i = messages.length - 1; i >= 0; i--) {
1152
+ const message = messages[i]
1153
+
1154
+ if (
1155
+ message?.type === 'user' &&
1156
+ !message.isMeta &&
1157
+ !hasToolResultContent(message.message.content)
1158
+ ) {
1159
+ turnsSinceLastAttachment++
1160
+ } else if (
1161
+ message?.type === 'attachment' &&
1162
+ (message.attachment.type === 'plan_mode' ||
1163
+ message.attachment.type === 'plan_mode_reentry')
1164
+ ) {
1165
+ foundPlanModeAttachment = true
1166
+ break
1167
+ }
1168
+ }
1169
+
1170
+ return { turnCount: turnsSinceLastAttachment, foundPlanModeAttachment }
1171
+ }
1172
+
1173
+ /**
1174
+ * Count plan_mode attachments since the last plan_mode_exit (or from start if no exit).
1175
+ * This ensures the full/sparse cycle resets when re-entering plan mode.
1176
+ */
1177
+ function countPlanModeAttachmentsSinceLastExit(messages: Message[]): number {
1178
+ let count = 0
1179
+ // Iterate backwards - if we hit a plan_mode_exit, stop counting
1180
+ for (let i = messages.length - 1; i >= 0; i--) {
1181
+ const message = messages[i]
1182
+ if (message?.type === 'attachment') {
1183
+ if (message.attachment.type === 'plan_mode_exit') {
1184
+ break // Stop counting at the last exit
1185
+ }
1186
+ if (message.attachment.type === 'plan_mode') {
1187
+ count++
1188
+ }
1189
+ }
1190
+ }
1191
+ return count
1192
+ }
1193
+
1194
+ async function getPlanModeAttachments(
1195
+ messages: Message[] | undefined,
1196
+ toolUseContext: ToolUseContext,
1197
+ ): Promise<Attachment[]> {
1198
+ const appState = toolUseContext.getAppState()
1199
+ const permissionContext = appState.toolPermissionContext
1200
+ if (permissionContext.mode !== 'plan') {
1201
+ return []
1202
+ }
1203
+
1204
+ // Check if we should attach based on turn count (except for first turn)
1205
+ if (messages && messages.length > 0) {
1206
+ const { turnCount, foundPlanModeAttachment } =
1207
+ getPlanModeAttachmentTurnCount(messages)
1208
+ // Only throttle if we've already sent a plan_mode attachment before
1209
+ // On first turn in plan mode, always attach
1210
+ if (
1211
+ foundPlanModeAttachment &&
1212
+ turnCount < PLAN_MODE_ATTACHMENT_CONFIG.TURNS_BETWEEN_ATTACHMENTS
1213
+ ) {
1214
+ return []
1215
+ }
1216
+ }
1217
+
1218
+ const planFilePath = getPlanFilePath(toolUseContext.agentId)
1219
+ const existingPlan = getPlan(toolUseContext.agentId)
1220
+
1221
+ const attachments: Attachment[] = []
1222
+
1223
+ // Check for re-entry: flag is set AND plan file exists
1224
+ if (hasExitedPlanModeInSession() && existingPlan !== null) {
1225
+ attachments.push({ type: 'plan_mode_reentry', planFilePath })
1226
+ setHasExitedPlanMode(false) // Clear flag - one-time guidance
1227
+ }
1228
+
1229
+ // Determine if this should be a full or sparse reminder
1230
+ // Full reminder on 1st, 6th, 11th... (every Nth attachment)
1231
+ const attachmentCount =
1232
+ countPlanModeAttachmentsSinceLastExit(messages ?? []) + 1
1233
+ const reminderType: 'full' | 'sparse' =
1234
+ attachmentCount %
1235
+ PLAN_MODE_ATTACHMENT_CONFIG.FULL_REMINDER_EVERY_N_ATTACHMENTS ===
1236
+ 1
1237
+ ? 'full'
1238
+ : 'sparse'
1239
+
1240
+ // Always add the main plan_mode attachment
1241
+ attachments.push({
1242
+ type: 'plan_mode',
1243
+ reminderType,
1244
+ isSubAgent: !!toolUseContext.agentId,
1245
+ planFilePath,
1246
+ planExists: existingPlan !== null,
1247
+ })
1248
+
1249
+ return attachments
1250
+ }
1251
+
1252
+ /**
1253
+ * Returns a plan_mode_exit attachment if we just exited plan mode.
1254
+ * This is a one-time notification to tell the model it's no longer in plan mode.
1255
+ */
1256
+ async function getPlanModeExitAttachment(
1257
+ toolUseContext: ToolUseContext,
1258
+ ): Promise<Attachment[]> {
1259
+ // Only trigger if the flag is set (we just exited plan mode)
1260
+ if (!needsPlanModeExitAttachment()) {
1261
+ return []
1262
+ }
1263
+
1264
+ const appState = toolUseContext.getAppState()
1265
+ if (appState.toolPermissionContext.mode === 'plan') {
1266
+ setNeedsPlanModeExitAttachment(false)
1267
+ return []
1268
+ }
1269
+
1270
+ // Clear the flag - this is a one-time notification
1271
+ setNeedsPlanModeExitAttachment(false)
1272
+
1273
+ const planFilePath = getPlanFilePath(toolUseContext.agentId)
1274
+ const planExists = getPlan(toolUseContext.agentId) !== null
1275
+
1276
+ // Note: skill discovery does NOT fire on plan exit. By the time the plan is
1277
+ // written, it's too late — the model should have had relevant skills WHILE
1278
+ // planning. The user_message signal already fires on the request that
1279
+ // triggers planning ("plan how to deploy this"), which is the right moment.
1280
+ return [{ type: 'plan_mode_exit', planFilePath, planExists }]
1281
+ }
1282
+
1283
+ function getAutoModeAttachmentTurnCount(messages: Message[]): {
1284
+ turnCount: number
1285
+ foundAutoModeAttachment: boolean
1286
+ } {
1287
+ let turnsSinceLastAttachment = 0
1288
+ let foundAutoModeAttachment = false
1289
+
1290
+ // Iterate backwards to find most recent auto_mode attachment.
1291
+ // Count HUMAN turns (non-meta, non-tool-result user messages), not assistant
1292
+ // messages — the tool loop in query.ts calls getAttachmentMessages on every
1293
+ // tool round, so a single human turn with 100 tool calls would fire ~20
1294
+ // reminders if we counted assistant messages. Auto mode's target use case is
1295
+ // long agentic sessions, where this accumulated 60-105× per session.
1296
+ for (let i = messages.length - 1; i >= 0; i--) {
1297
+ const message = messages[i]
1298
+
1299
+ if (
1300
+ message?.type === 'user' &&
1301
+ !message.isMeta &&
1302
+ !hasToolResultContent(message.message.content)
1303
+ ) {
1304
+ turnsSinceLastAttachment++
1305
+ } else if (
1306
+ message?.type === 'attachment' &&
1307
+ message.attachment.type === 'auto_mode'
1308
+ ) {
1309
+ foundAutoModeAttachment = true
1310
+ break
1311
+ } else if (
1312
+ message?.type === 'attachment' &&
1313
+ message.attachment.type === 'auto_mode_exit'
1314
+ ) {
1315
+ // Exit resets the throttle — treat as if no prior attachment exists
1316
+ break
1317
+ }
1318
+ }
1319
+
1320
+ return { turnCount: turnsSinceLastAttachment, foundAutoModeAttachment }
1321
+ }
1322
+
1323
+ /**
1324
+ * Count auto_mode attachments since the last auto_mode_exit (or from start if no exit).
1325
+ * This ensures the full/sparse cycle resets when re-entering auto mode.
1326
+ */
1327
+ function countAutoModeAttachmentsSinceLastExit(messages: Message[]): number {
1328
+ let count = 0
1329
+ for (let i = messages.length - 1; i >= 0; i--) {
1330
+ const message = messages[i]
1331
+ if (message?.type === 'attachment') {
1332
+ if (message.attachment.type === 'auto_mode_exit') {
1333
+ break
1334
+ }
1335
+ if (message.attachment.type === 'auto_mode') {
1336
+ count++
1337
+ }
1338
+ }
1339
+ }
1340
+ return count
1341
+ }
1342
+
1343
+ async function getAutoModeAttachments(
1344
+ messages: Message[] | undefined,
1345
+ toolUseContext: ToolUseContext,
1346
+ ): Promise<Attachment[]> {
1347
+ const appState = toolUseContext.getAppState()
1348
+ const permissionContext = appState.toolPermissionContext
1349
+ const inAuto = permissionContext.mode === 'auto'
1350
+ const inPlanWithAuto =
1351
+ permissionContext.mode === 'plan' &&
1352
+ (autoModeStateModule?.isAutoModeActive() ?? false)
1353
+ if (!inAuto && !inPlanWithAuto) {
1354
+ return []
1355
+ }
1356
+
1357
+ // Check if we should attach based on turn count (except for first turn)
1358
+ if (messages && messages.length > 0) {
1359
+ const { turnCount, foundAutoModeAttachment } =
1360
+ getAutoModeAttachmentTurnCount(messages)
1361
+ // Only throttle if we've already sent an auto_mode attachment before
1362
+ // On first turn in auto mode, always attach
1363
+ if (
1364
+ foundAutoModeAttachment &&
1365
+ turnCount < AUTO_MODE_ATTACHMENT_CONFIG.TURNS_BETWEEN_ATTACHMENTS
1366
+ ) {
1367
+ return []
1368
+ }
1369
+ }
1370
+
1371
+ // Determine if this should be a full or sparse reminder
1372
+ const attachmentCount =
1373
+ countAutoModeAttachmentsSinceLastExit(messages ?? []) + 1
1374
+ const reminderType: 'full' | 'sparse' =
1375
+ attachmentCount %
1376
+ AUTO_MODE_ATTACHMENT_CONFIG.FULL_REMINDER_EVERY_N_ATTACHMENTS ===
1377
+ 1
1378
+ ? 'full'
1379
+ : 'sparse'
1380
+
1381
+ return [{ type: 'auto_mode', reminderType }]
1382
+ }
1383
+
1384
+ /**
1385
+ * Returns an auto_mode_exit attachment if we just exited auto mode.
1386
+ * This is a one-time notification to tell the model it's no longer in auto mode.
1387
+ */
1388
+ async function getAutoModeExitAttachment(
1389
+ toolUseContext: ToolUseContext,
1390
+ ): Promise<Attachment[]> {
1391
+ if (!needsAutoModeExitAttachment()) {
1392
+ return []
1393
+ }
1394
+
1395
+ const appState = toolUseContext.getAppState()
1396
+ // Suppress when auto is still active — covers both mode==='auto' and
1397
+ // plan-with-auto-active (where mode==='plan' but classifier runs).
1398
+ if (
1399
+ appState.toolPermissionContext.mode === 'auto' ||
1400
+ (autoModeStateModule?.isAutoModeActive() ?? false)
1401
+ ) {
1402
+ setNeedsAutoModeExitAttachment(false)
1403
+ return []
1404
+ }
1405
+
1406
+ setNeedsAutoModeExitAttachment(false)
1407
+ return [{ type: 'auto_mode_exit' }]
1408
+ }
1409
+
1410
+ /**
1411
+ * Detects when the local date has changed since the last turn (user coding
1412
+ * past midnight) and emits an attachment to notify the model.
1413
+ *
1414
+ * The date_change attachment is appended at the tail of the conversation,
1415
+ * so the model learns the new date without mutating the cached prefix.
1416
+ * messages[0] (from getUserContext → prependUserContext) intentionally
1417
+ * keeps the stale date — clearing that cache would regenerate the prefix
1418
+ * and turn the entire conversation into cache_creation on the next turn
1419
+ * (~920K effective tokens per midnight crossing per overnight session).
1420
+ *
1421
+ * Exported for testing — regression guard for the cache-clear removal.
1422
+ */
1423
+ export function getDateChangeAttachments(
1424
+ messages: Message[] | undefined,
1425
+ ): Attachment[] {
1426
+ const currentDate = getLocalISODate()
1427
+ const lastDate = getLastEmittedDate()
1428
+
1429
+ if (lastDate === null) {
1430
+ // First turn — just record, no attachment needed
1431
+ setLastEmittedDate(currentDate)
1432
+ return []
1433
+ }
1434
+
1435
+ if (currentDate === lastDate) {
1436
+ return []
1437
+ }
1438
+
1439
+ setLastEmittedDate(currentDate)
1440
+
1441
+ // Assistant mode: flush yesterday's transcript to the per-day file so
1442
+ // the /dream skill (1–5am local) finds it even if no compaction fires
1443
+ // today. Fire-and-forget; writeSessionTranscriptSegment buckets by
1444
+ // message timestamp so a multi-day gap flushes each day correctly.
1445
+ if (feature('KAIROS')) {
1446
+ if (getKairosActive() && messages !== undefined) {
1447
+ sessionTranscriptModule?.flushOnDateChange(messages, currentDate)
1448
+ }
1449
+ }
1450
+
1451
+ return [{ type: 'date_change', newDate: currentDate }]
1452
+ }
1453
+
1454
+ function getUltrathinkEffortAttachment(input: string | null): Attachment[] {
1455
+ if (!isUltrathinkEnabled() || !input || !hasUltrathinkKeyword(input)) {
1456
+ return []
1457
+ }
1458
+ logEvent('tengu_ultrathink', {})
1459
+ return [{ type: 'ultrathink_effort', level: 'high' }]
1460
+ }
1461
+
1462
+ // Exported for compact.ts — the gate must be identical at both call sites.
1463
+ export function getDeferredToolsDeltaAttachment(
1464
+ tools: Tools,
1465
+ model: string,
1466
+ messages: Message[] | undefined,
1467
+ scanContext?: DeferredToolsDeltaScanContext,
1468
+ ): Attachment[] {
1469
+ if (!isDeferredToolsDeltaEnabled()) return []
1470
+ // These three checks mirror the sync parts of isToolSearchEnabled —
1471
+ // the attachment text says "available via ToolSearch", so ToolSearch
1472
+ // has to actually be in the request. The async auto-threshold check
1473
+ // is not replicated (would double-fire tengu_tool_search_mode_decision);
1474
+ // in tst-auto below-threshold the attachment can fire while ToolSearch
1475
+ // is filtered out, but that's a narrow case and the tools announced
1476
+ // are directly callable anyway.
1477
+ if (!isToolSearchEnabledOptimistic()) return []
1478
+ if (!modelSupportsToolReference(model)) return []
1479
+ if (!isToolSearchToolAvailable(tools)) return []
1480
+ const delta = getDeferredToolsDelta(tools, messages ?? [], scanContext)
1481
+ if (!delta) return []
1482
+ return [{ type: 'deferred_tools_delta', ...delta }]
1483
+ }
1484
+
1485
+ /**
1486
+ * Diff the current filtered agent pool against what's already been announced
1487
+ * in this conversation (reconstructed from prior agent_listing_delta
1488
+ * attachments). Returns [] if nothing changed or the gate is off.
1489
+ *
1490
+ * The agent list was embedded in AgentTool's description, causing ~10.2% of
1491
+ * fleet cache_creation: MCP async connect, /reload-plugins, or
1492
+ * permission-mode change → description changes → full tool-schema cache bust.
1493
+ * Moving the list here keeps the tool description static.
1494
+ *
1495
+ * Exported for compact.ts — re-announces the full set after compaction eats
1496
+ * prior deltas.
1497
+ */
1498
+ export function getAgentListingDeltaAttachment(
1499
+ toolUseContext: ToolUseContext,
1500
+ messages: Message[] | undefined,
1501
+ ): Attachment[] {
1502
+ if (!shouldInjectAgentListInMessages()) return []
1503
+
1504
+ // Skip if AgentTool isn't in the pool — the listing would be unactionable.
1505
+ if (
1506
+ !toolUseContext.options.tools.some(t => toolMatchesName(t, AGENT_TOOL_NAME))
1507
+ ) {
1508
+ return []
1509
+ }
1510
+
1511
+ const { activeAgents, allowedAgentTypes } =
1512
+ toolUseContext.options.agentDefinitions
1513
+
1514
+ // Mirror AgentTool.prompt()'s filtering: MCP requirements → deny rules →
1515
+ // allowedAgentTypes restriction. Keep this in sync with AgentTool.tsx.
1516
+ const mcpServers = new Set<string>()
1517
+ for (const tool of toolUseContext.options.tools) {
1518
+ const info = mcpInfoFromString(tool.name)
1519
+ if (info) mcpServers.add(info.serverName)
1520
+ }
1521
+ const permissionContext = toolUseContext.getAppState().toolPermissionContext
1522
+ let filtered = filterDeniedAgents(
1523
+ filterAgentsByMcpRequirements(activeAgents, [...mcpServers]),
1524
+ permissionContext,
1525
+ AGENT_TOOL_NAME,
1526
+ )
1527
+ if (allowedAgentTypes) {
1528
+ filtered = filtered.filter(a => allowedAgentTypes.includes(a.agentType))
1529
+ }
1530
+
1531
+ // Reconstruct announced set from prior deltas in the transcript.
1532
+ const announced = new Set<string>()
1533
+ for (const msg of messages ?? []) {
1534
+ if (msg.type !== 'attachment') continue
1535
+ if (msg.attachment.type !== 'agent_listing_delta') continue
1536
+ for (const t of msg.attachment.addedTypes) announced.add(t)
1537
+ for (const t of msg.attachment.removedTypes) announced.delete(t)
1538
+ }
1539
+
1540
+ const currentTypes = new Set(filtered.map(a => a.agentType))
1541
+ const added = filtered.filter(a => !announced.has(a.agentType))
1542
+ const removed: string[] = []
1543
+ for (const t of announced) {
1544
+ if (!currentTypes.has(t)) removed.push(t)
1545
+ }
1546
+
1547
+ if (added.length === 0 && removed.length === 0) return []
1548
+
1549
+ // Sort for deterministic output — agent load order is nondeterministic
1550
+ // (plugin load races, MCP async connect).
1551
+ added.sort((a, b) => a.agentType.localeCompare(b.agentType))
1552
+ removed.sort()
1553
+
1554
+ return [
1555
+ {
1556
+ type: 'agent_listing_delta',
1557
+ addedTypes: added.map(a => a.agentType),
1558
+ addedLines: added.map(formatAgentLine),
1559
+ removedTypes: removed,
1560
+ isInitial: announced.size === 0,
1561
+ showConcurrencyNote: getSubscriptionType() !== 'pro',
1562
+ },
1563
+ ]
1564
+ }
1565
+
1566
+ // Exported for compact.ts / reactiveCompact.ts — single source of truth for the gate.
1567
+ export function getMcpInstructionsDeltaAttachment(
1568
+ mcpClients: MCPServerConnection[],
1569
+ tools: Tools,
1570
+ model: string,
1571
+ messages: Message[] | undefined,
1572
+ ): Attachment[] {
1573
+ if (!isMcpInstructionsDeltaEnabled()) return []
1574
+
1575
+ // The chrome ToolSearch hint is client-authored and ToolSearch-conditional;
1576
+ // actual server `instructions` are unconditional. Decide the chrome part
1577
+ // here, pass it into the pure diff as a synthesized entry.
1578
+ const clientSide: ClientSideInstruction[] = []
1579
+ if (
1580
+ isToolSearchEnabledOptimistic() &&
1581
+ modelSupportsToolReference(model) &&
1582
+ isToolSearchToolAvailable(tools)
1583
+ ) {
1584
+ clientSide.push({
1585
+ serverName: CLAUDE_IN_CHROME_MCP_SERVER_NAME,
1586
+ block: CHROME_TOOL_SEARCH_INSTRUCTIONS,
1587
+ })
1588
+ }
1589
+
1590
+ const delta = getMcpInstructionsDelta(mcpClients, messages ?? [], clientSide)
1591
+ if (!delta) return []
1592
+ return [{ type: 'mcp_instructions_delta', ...delta }]
1593
+ }
1594
+
1595
+ function getCriticalSystemReminderAttachment(
1596
+ toolUseContext: ToolUseContext,
1597
+ ): Attachment[] {
1598
+ const reminder = toolUseContext.criticalSystemReminder_EXPERIMENTAL
1599
+ if (!reminder) {
1600
+ return []
1601
+ }
1602
+ return [{ type: 'critical_system_reminder', content: reminder }]
1603
+ }
1604
+
1605
+ function getOutputStyleAttachment(): Attachment[] {
1606
+ const settings = getSettings_DEPRECATED()
1607
+ const outputStyle = settings?.outputStyle || 'default'
1608
+
1609
+ // Only show for non-default styles
1610
+ if (outputStyle === 'default') {
1611
+ return []
1612
+ }
1613
+
1614
+ return [
1615
+ {
1616
+ type: 'output_style',
1617
+ style: outputStyle,
1618
+ },
1619
+ ]
1620
+ }
1621
+
1622
+ async function getSelectedLinesFromIDE(
1623
+ ideSelection: IDESelection | null,
1624
+ toolUseContext: ToolUseContext,
1625
+ ): Promise<Attachment[]> {
1626
+ const ideName = getConnectedIdeName(toolUseContext.options.mcpClients)
1627
+ if (
1628
+ !ideName ||
1629
+ ideSelection?.lineStart === undefined ||
1630
+ !ideSelection.text ||
1631
+ !ideSelection.filePath
1632
+ ) {
1633
+ return []
1634
+ }
1635
+
1636
+ const appState = toolUseContext.getAppState()
1637
+ if (isFileReadDenied(ideSelection.filePath, appState.toolPermissionContext)) {
1638
+ return []
1639
+ }
1640
+
1641
+ return [
1642
+ {
1643
+ type: 'selected_lines_in_ide',
1644
+ ideName,
1645
+ lineStart: ideSelection.lineStart,
1646
+ lineEnd: ideSelection.lineStart + ideSelection.lineCount - 1,
1647
+ filename: ideSelection.filePath,
1648
+ content: ideSelection.text,
1649
+ displayPath: relative(getCwd(), ideSelection.filePath),
1650
+ },
1651
+ ]
1652
+ }
1653
+
1654
+ /**
1655
+ * Computes the directories to process for nested memory file loading.
1656
+ * Returns two lists:
1657
+ * - nestedDirs: Directories between CWD and targetPath (processed for CLAUDE.md + all rules)
1658
+ * - cwdLevelDirs: Directories from root to CWD (processed for conditional rules only)
1659
+ *
1660
+ * @param targetPath The target file path
1661
+ * @param originalCwd The original current working directory
1662
+ * @returns Object with nestedDirs and cwdLevelDirs arrays, both ordered from parent to child
1663
+ */
1664
+ export function getDirectoriesToProcess(
1665
+ targetPath: string,
1666
+ originalCwd: string,
1667
+ ): { nestedDirs: string[]; cwdLevelDirs: string[] } {
1668
+ // Build list of directories from original CWD to targetPath's directory
1669
+ const targetDir = dirname(resolve(targetPath))
1670
+ const nestedDirs: string[] = []
1671
+ let currentDir = targetDir
1672
+
1673
+ // Walk up from target directory to original CWD
1674
+ while (currentDir !== originalCwd && currentDir !== parse(currentDir).root) {
1675
+ if (currentDir.startsWith(originalCwd)) {
1676
+ nestedDirs.push(currentDir)
1677
+ }
1678
+ currentDir = dirname(currentDir)
1679
+ }
1680
+
1681
+ // Reverse to get order from CWD down to target
1682
+ nestedDirs.reverse()
1683
+
1684
+ // Build list of directories from root to CWD (for conditional rules only)
1685
+ const cwdLevelDirs: string[] = []
1686
+ currentDir = originalCwd
1687
+
1688
+ while (currentDir !== parse(currentDir).root) {
1689
+ cwdLevelDirs.push(currentDir)
1690
+ currentDir = dirname(currentDir)
1691
+ }
1692
+
1693
+ // Reverse to get order from root to CWD
1694
+ cwdLevelDirs.reverse()
1695
+
1696
+ return { nestedDirs, cwdLevelDirs }
1697
+ }
1698
+
1699
+ /**
1700
+ * Converts memory files to attachments, filtering out already-loaded files.
1701
+ *
1702
+ * @param memoryFiles The memory files to convert
1703
+ * @param toolUseContext The tool use context (for tracking loaded files)
1704
+ * @returns Array of nested memory attachments
1705
+ */
1706
+ function isInstructionsMemoryType(
1707
+ type: MemoryFileInfo['type'],
1708
+ ): type is InstructionsMemoryType {
1709
+ return (
1710
+ type === 'User' ||
1711
+ type === 'Project' ||
1712
+ type === 'Local' ||
1713
+ type === 'Managed'
1714
+ )
1715
+ }
1716
+
1717
+ /** Exported for testing — regression guard for LRU-eviction re-injection. */
1718
+ export function memoryFilesToAttachments(
1719
+ memoryFiles: MemoryFileInfo[],
1720
+ toolUseContext: ToolUseContext,
1721
+ triggerFilePath?: string,
1722
+ ): Attachment[] {
1723
+ const attachments: Attachment[] = []
1724
+ const shouldFireHook = hasInstructionsLoadedHook()
1725
+
1726
+ for (const memoryFile of memoryFiles) {
1727
+ // Dedup: loadedNestedMemoryPaths is a non-evicting Set; readFileState
1728
+ // is a 100-entry LRU that drops entries in busy sessions, so relying
1729
+ // on it alone re-injects the same CLAUDE.md on every eviction cycle.
1730
+ if (toolUseContext.loadedNestedMemoryPaths?.has(memoryFile.path)) {
1731
+ continue
1732
+ }
1733
+ if (!toolUseContext.readFileState.has(memoryFile.path)) {
1734
+ attachments.push({
1735
+ type: 'nested_memory',
1736
+ path: memoryFile.path,
1737
+ content: memoryFile,
1738
+ displayPath: relative(getCwd(), memoryFile.path),
1739
+ })
1740
+ toolUseContext.loadedNestedMemoryPaths?.add(memoryFile.path)
1741
+
1742
+ // Mark as loaded in readFileState — this provides cross-function and
1743
+ // cross-turn dedup via the .has() check above.
1744
+ //
1745
+ // When the injected content doesn't match disk (stripped HTML comments,
1746
+ // stripped frontmatter, truncated MEMORY.md), cache the RAW disk bytes
1747
+ // with `isPartialView: true`. Edit/Write see the flag and require a real
1748
+ // Read first; getChangedFiles sees real content + undefined offset/limit
1749
+ // so mid-session change detection still works.
1750
+ toolUseContext.readFileState.set(memoryFile.path, {
1751
+ content: memoryFile.contentDiffersFromDisk
1752
+ ? (memoryFile.rawContent ?? memoryFile.content)
1753
+ : memoryFile.content,
1754
+ timestamp: Date.now(),
1755
+ offset: undefined,
1756
+ limit: undefined,
1757
+ isPartialView: memoryFile.contentDiffersFromDisk,
1758
+ })
1759
+
1760
+
1761
+ // Fire InstructionsLoaded hook for audit/observability (fire-and-forget)
1762
+ if (shouldFireHook && isInstructionsMemoryType(memoryFile.type)) {
1763
+ const loadReason = memoryFile.globs
1764
+ ? 'path_glob_match'
1765
+ : memoryFile.parent
1766
+ ? 'include'
1767
+ : 'nested_traversal'
1768
+ void executeInstructionsLoadedHooks(
1769
+ memoryFile.path,
1770
+ memoryFile.type,
1771
+ loadReason,
1772
+ {
1773
+ globs: memoryFile.globs,
1774
+ triggerFilePath,
1775
+ parentFilePath: memoryFile.parent,
1776
+ },
1777
+ )
1778
+ }
1779
+ }
1780
+ }
1781
+
1782
+ return attachments
1783
+ }
1784
+
1785
+ /**
1786
+ * Loads nested memory files for a given file path and returns them as attachments.
1787
+ * This function performs directory traversal to find CLAUDE.md files and conditional rules
1788
+ * that apply to the target file path.
1789
+ *
1790
+ * Processing order (must be preserved):
1791
+ * 1. Managed/User conditional rules matching targetPath
1792
+ * 2. Nested directories (CWD → target): CLAUDE.md + unconditional + conditional rules
1793
+ * 3. CWD-level directories (root → CWD): conditional rules only
1794
+ *
1795
+ * @param filePath The file path to get nested memory files for
1796
+ * @param toolUseContext The tool use context
1797
+ * @param appState The app state containing tool permission context
1798
+ * @returns Array of nested memory attachments
1799
+ */
1800
+ async function getNestedMemoryAttachmentsForFile(
1801
+ filePath: string,
1802
+ toolUseContext: ToolUseContext,
1803
+ appState: { toolPermissionContext: ToolPermissionContext },
1804
+ ): Promise<Attachment[]> {
1805
+ const attachments: Attachment[] = []
1806
+
1807
+ try {
1808
+ // Early return if path is not in allowed working path
1809
+ if (!pathInAllowedWorkingPath(filePath, appState.toolPermissionContext)) {
1810
+ return attachments
1811
+ }
1812
+
1813
+ const processedPaths = new Set<string>()
1814
+ const originalCwd = getOriginalCwd()
1815
+
1816
+ // Phase 1: Process Managed and User conditional rules
1817
+ const managedUserRules = await getManagedAndUserConditionalRules(
1818
+ filePath,
1819
+ processedPaths,
1820
+ )
1821
+ attachments.push(
1822
+ ...memoryFilesToAttachments(managedUserRules, toolUseContext, filePath),
1823
+ )
1824
+
1825
+ // Phase 2: Get directories to process
1826
+ const { nestedDirs, cwdLevelDirs } = getDirectoriesToProcess(
1827
+ filePath,
1828
+ originalCwd,
1829
+ )
1830
+
1831
+ const skipProjectLevel = getFeatureValue_CACHED_MAY_BE_STALE(
1832
+ 'tengu_paper_halyard',
1833
+ false,
1834
+ )
1835
+
1836
+ // Phase 3: Process nested directories (CWD → target)
1837
+ // Each directory gets: CLAUDE.md + unconditional rules + conditional rules
1838
+ for (const dir of nestedDirs) {
1839
+ const memoryFiles = (
1840
+ await getMemoryFilesForNestedDirectory(dir, filePath, processedPaths)
1841
+ ).filter(
1842
+ f => !skipProjectLevel || (f.type !== 'Project' && f.type !== 'Local'),
1843
+ )
1844
+ attachments.push(
1845
+ ...memoryFilesToAttachments(memoryFiles, toolUseContext, filePath),
1846
+ )
1847
+ }
1848
+
1849
+ // Phase 4: Process CWD-level directories (root → CWD)
1850
+ // Only conditional rules (unconditional rules are already loaded eagerly)
1851
+ for (const dir of cwdLevelDirs) {
1852
+ const conditionalRules = (
1853
+ await getConditionalRulesForCwdLevelDirectory(
1854
+ dir,
1855
+ filePath,
1856
+ processedPaths,
1857
+ )
1858
+ ).filter(
1859
+ f => !skipProjectLevel || (f.type !== 'Project' && f.type !== 'Local'),
1860
+ )
1861
+ attachments.push(
1862
+ ...memoryFilesToAttachments(conditionalRules, toolUseContext, filePath),
1863
+ )
1864
+ }
1865
+ } catch (error) {
1866
+ logError(error)
1867
+ }
1868
+
1869
+ return attachments
1870
+ }
1871
+
1872
+ async function getOpenedFileFromIDE(
1873
+ ideSelection: IDESelection | null,
1874
+ toolUseContext: ToolUseContext,
1875
+ ): Promise<Attachment[]> {
1876
+ if (!ideSelection?.filePath || ideSelection.text) {
1877
+ return []
1878
+ }
1879
+
1880
+ const appState = toolUseContext.getAppState()
1881
+ if (isFileReadDenied(ideSelection.filePath, appState.toolPermissionContext)) {
1882
+ return []
1883
+ }
1884
+
1885
+ // Get nested memory files
1886
+ const nestedMemoryAttachments = await getNestedMemoryAttachmentsForFile(
1887
+ ideSelection.filePath,
1888
+ toolUseContext,
1889
+ appState,
1890
+ )
1891
+
1892
+ // Return nested memory attachments followed by the opened file attachment
1893
+ return [
1894
+ ...nestedMemoryAttachments,
1895
+ {
1896
+ type: 'opened_file_in_ide',
1897
+ filename: ideSelection.filePath,
1898
+ },
1899
+ ]
1900
+ }
1901
+
1902
+ async function processAtMentionedFiles(
1903
+ input: string,
1904
+ toolUseContext: ToolUseContext,
1905
+ ): Promise<Attachment[]> {
1906
+ const files = extractAtMentionedFiles(input)
1907
+ if (files.length === 0) return []
1908
+
1909
+ const appState = toolUseContext.getAppState()
1910
+ const results = await Promise.all(
1911
+ files.map(async file => {
1912
+ try {
1913
+ const { filename, lineStart, lineEnd } = parseAtMentionedFileLines(file)
1914
+ const absoluteFilename = expandPath(filename)
1915
+
1916
+ if (
1917
+ isFileReadDenied(absoluteFilename, appState.toolPermissionContext)
1918
+ ) {
1919
+ return null
1920
+ }
1921
+
1922
+ // Check if it's a directory
1923
+ try {
1924
+ const stats = await stat(absoluteFilename)
1925
+ if (stats.isDirectory()) {
1926
+ try {
1927
+ const entries = await readdir(absoluteFilename, {
1928
+ withFileTypes: true,
1929
+ })
1930
+ const MAX_DIR_ENTRIES = 1000
1931
+ const truncated = entries.length > MAX_DIR_ENTRIES
1932
+ const names = entries.slice(0, MAX_DIR_ENTRIES).map(e => e.name)
1933
+ if (truncated) {
1934
+ names.push(
1935
+ `\u2026 and ${entries.length - MAX_DIR_ENTRIES} more entries`,
1936
+ )
1937
+ }
1938
+ const stdout = names.join('\n')
1939
+ logEvent('tengu_at_mention_extracting_directory_success', {})
1940
+
1941
+ return {
1942
+ type: 'directory' as const,
1943
+ path: absoluteFilename,
1944
+ content: stdout,
1945
+ displayPath: relative(getCwd(), absoluteFilename),
1946
+ }
1947
+ } catch {
1948
+ return null
1949
+ }
1950
+ }
1951
+ } catch {
1952
+ // If stat fails, continue with file logic
1953
+ }
1954
+
1955
+ return await generateFileAttachment(
1956
+ absoluteFilename,
1957
+ toolUseContext,
1958
+ 'tengu_at_mention_extracting_filename_success',
1959
+ 'tengu_at_mention_extracting_filename_error',
1960
+ 'at-mention',
1961
+ {
1962
+ offset: lineStart,
1963
+ limit: lineEnd && lineStart ? lineEnd - lineStart + 1 : undefined,
1964
+ },
1965
+ )
1966
+ } catch {
1967
+ logEvent('tengu_at_mention_extracting_filename_error', {})
1968
+ }
1969
+ }),
1970
+ )
1971
+ return results.filter(Boolean) as Attachment[]
1972
+ }
1973
+
1974
+ function processAgentMentions(
1975
+ input: string,
1976
+ agents: AgentDefinition[],
1977
+ ): Attachment[] {
1978
+ const agentMentions = extractAgentMentions(input)
1979
+ if (agentMentions.length === 0) return []
1980
+
1981
+ const results = agentMentions.map(mention => {
1982
+ const agentType = mention.replace('agent-', '')
1983
+ const agentDef = agents.find(def => def.agentType === agentType)
1984
+
1985
+ if (!agentDef) {
1986
+ logEvent('tengu_at_mention_agent_not_found', {})
1987
+ return null
1988
+ }
1989
+
1990
+ logEvent('tengu_at_mention_agent_success', {})
1991
+
1992
+ return {
1993
+ type: 'agent_mention' as const,
1994
+ agentType: agentDef.agentType,
1995
+ }
1996
+ })
1997
+
1998
+ return results.filter(
1999
+ (result): result is NonNullable<typeof result> => result !== null,
2000
+ )
2001
+ }
2002
+
2003
+ async function processMcpResourceAttachments(
2004
+ input: string,
2005
+ toolUseContext: ToolUseContext,
2006
+ ): Promise<Attachment[]> {
2007
+ const resourceMentions = extractMcpResourceMentions(input)
2008
+ if (resourceMentions.length === 0) return []
2009
+
2010
+ const mcpClients = toolUseContext.options.mcpClients || []
2011
+
2012
+ const results = await Promise.all(
2013
+ resourceMentions.map(async mention => {
2014
+ try {
2015
+ const [serverName, ...uriParts] = mention.split(':')
2016
+ const uri = uriParts.join(':') // Rejoin in case URI contains colons
2017
+
2018
+ if (!serverName || !uri) {
2019
+ logEvent('tengu_at_mention_mcp_resource_error', {})
2020
+ return null
2021
+ }
2022
+
2023
+ // Find the MCP client
2024
+ const client = mcpClients.find(c => c.name === serverName)
2025
+ if (!client || client.type !== 'connected') {
2026
+ logEvent('tengu_at_mention_mcp_resource_error', {})
2027
+ return null
2028
+ }
2029
+
2030
+ // Find the resource in available resources to get its metadata
2031
+ const serverResources =
2032
+ toolUseContext.options.mcpResources?.[serverName] || []
2033
+ const resourceInfo = serverResources.find(r => r.uri === uri)
2034
+ if (!resourceInfo) {
2035
+ logEvent('tengu_at_mention_mcp_resource_error', {})
2036
+ return null
2037
+ }
2038
+
2039
+ try {
2040
+ const result = await client.client.readResource({
2041
+ uri,
2042
+ })
2043
+
2044
+ logEvent('tengu_at_mention_mcp_resource_success', {})
2045
+
2046
+ return {
2047
+ type: 'mcp_resource' as const,
2048
+ server: serverName,
2049
+ uri,
2050
+ name: resourceInfo.name || uri,
2051
+ description: resourceInfo.description,
2052
+ content: result,
2053
+ }
2054
+ } catch (error) {
2055
+ logEvent('tengu_at_mention_mcp_resource_error', {})
2056
+ logError(error)
2057
+ return null
2058
+ }
2059
+ } catch {
2060
+ logEvent('tengu_at_mention_mcp_resource_error', {})
2061
+ return null
2062
+ }
2063
+ }),
2064
+ )
2065
+
2066
+ return results.filter(
2067
+ (result): result is NonNullable<typeof result> => result !== null,
2068
+ ) as Attachment[]
2069
+ }
2070
+
2071
+ export async function getChangedFiles(
2072
+ toolUseContext: ToolUseContext,
2073
+ ): Promise<Attachment[]> {
2074
+ const filePaths = cacheKeys(toolUseContext.readFileState)
2075
+ if (filePaths.length === 0) return []
2076
+
2077
+ const appState = toolUseContext.getAppState()
2078
+ const results = await Promise.all(
2079
+ filePaths.map(async filePath => {
2080
+ const fileState = toolUseContext.readFileState.get(filePath)
2081
+ if (!fileState) return null
2082
+
2083
+ // TODO: Implement offset/limit support for changed files
2084
+ if (fileState.offset !== undefined || fileState.limit !== undefined) {
2085
+ return null
2086
+ }
2087
+
2088
+ const normalizedPath = expandPath(filePath)
2089
+
2090
+ // Check if file has a deny rule configured
2091
+ if (isFileReadDenied(normalizedPath, appState.toolPermissionContext)) {
2092
+ return null
2093
+ }
2094
+
2095
+ try {
2096
+ const mtime = await getFileModificationTimeAsync(normalizedPath)
2097
+ if (mtime <= fileState.timestamp) {
2098
+ return null
2099
+ }
2100
+
2101
+ const fileInput = { file_path: normalizedPath }
2102
+
2103
+ // Validate file path is valid
2104
+ const isValid = await FileReadTool.validateInput(
2105
+ fileInput,
2106
+ toolUseContext,
2107
+ )
2108
+ if (!isValid.result) {
2109
+ return null
2110
+ }
2111
+
2112
+ const result = await FileReadTool.call(fileInput, toolUseContext)
2113
+ // Extract only the changed section
2114
+ if (result.data.type === 'text') {
2115
+ const snippet = getSnippetForTwoFileDiff(
2116
+ fileState.content,
2117
+ result.data.file.content,
2118
+ )
2119
+
2120
+ // File was touched but not modified
2121
+ if (snippet === '') {
2122
+ return null
2123
+ }
2124
+
2125
+ return {
2126
+ type: 'edited_text_file' as const,
2127
+ filename: normalizedPath,
2128
+ snippet,
2129
+ }
2130
+ }
2131
+
2132
+ // For non-text files (images), apply the same token limit logic as FileReadTool
2133
+ if (result.data.type === 'image') {
2134
+ try {
2135
+ const data = await readImageWithTokenBudget(normalizedPath)
2136
+ return {
2137
+ type: 'edited_image_file' as const,
2138
+ filename: normalizedPath,
2139
+ content: data,
2140
+ }
2141
+ } catch (compressionError) {
2142
+ logError(compressionError)
2143
+ logEvent('tengu_watched_file_compression_failed', {
2144
+ file: normalizedPath,
2145
+ } as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS)
2146
+ return null
2147
+ }
2148
+ }
2149
+
2150
+ // notebook / pdf / parts — no diff representation; explicitly
2151
+ // null so the map callback has no implicit-undefined path.
2152
+ return null
2153
+ } catch (err) {
2154
+ // Evict ONLY on ENOENT (file truly deleted). Transient stat
2155
+ // failures — atomic-save races (editor writes tmp→rename and
2156
+ // stat hits the gap), EACCES churn, network-FS hiccups — must
2157
+ // NOT evict, or the next Edit fails code-6 even though the
2158
+ // file still exists and the model just read it. VS Code
2159
+ // auto-save/format-on-save hits this race especially often.
2160
+ // See regression analysis on PR #18525.
2161
+ if (isENOENT(err)) {
2162
+ toolUseContext.readFileState.delete(filePath)
2163
+ }
2164
+ return null
2165
+ }
2166
+ }),
2167
+ )
2168
+ return results.filter(result => result != null) as Attachment[]
2169
+ }
2170
+
2171
+ /**
2172
+ * Processes paths that need nested memory attachments and checks for nested CLAUDE.md files
2173
+ * Uses nestedMemoryAttachmentTriggers field from ToolUseContext
2174
+ */
2175
+ async function getNestedMemoryAttachments(
2176
+ toolUseContext: ToolUseContext,
2177
+ ): Promise<Attachment[]> {
2178
+ // Check triggers first — getAppState() waits for a React render cycle,
2179
+ // and the common case is an empty trigger set.
2180
+ if (
2181
+ !toolUseContext.nestedMemoryAttachmentTriggers ||
2182
+ toolUseContext.nestedMemoryAttachmentTriggers.size === 0
2183
+ ) {
2184
+ return []
2185
+ }
2186
+
2187
+ const appState = toolUseContext.getAppState()
2188
+ const attachments: Attachment[] = []
2189
+
2190
+ for (const filePath of toolUseContext.nestedMemoryAttachmentTriggers) {
2191
+ const nestedAttachments = await getNestedMemoryAttachmentsForFile(
2192
+ filePath,
2193
+ toolUseContext,
2194
+ appState,
2195
+ )
2196
+ attachments.push(...nestedAttachments)
2197
+ }
2198
+
2199
+ toolUseContext.nestedMemoryAttachmentTriggers.clear()
2200
+
2201
+ return attachments
2202
+ }
2203
+
2204
+ async function getRelevantMemoryAttachments(
2205
+ input: string,
2206
+ agents: AgentDefinition[],
2207
+ readFileState: FileStateCache,
2208
+ recentTools: readonly string[],
2209
+ signal: AbortSignal,
2210
+ alreadySurfaced: ReadonlySet<string>,
2211
+ ): Promise<Attachment[]> {
2212
+ // If an agent is @-mentioned, search only its memory dir (isolation).
2213
+ // Otherwise search the auto-memory dir.
2214
+ const memoryDirs = extractAgentMentions(input).flatMap(mention => {
2215
+ const agentType = mention.replace('agent-', '')
2216
+ const agentDef = agents.find(def => def.agentType === agentType)
2217
+ return agentDef?.memory
2218
+ ? [getAgentMemoryDir(agentType, agentDef.memory)]
2219
+ : []
2220
+ })
2221
+ const dirs = memoryDirs.length > 0 ? memoryDirs : [getAutoMemPath()]
2222
+
2223
+ const allResults = await Promise.all(
2224
+ dirs.map(dir =>
2225
+ findRelevantMemories(
2226
+ input,
2227
+ dir,
2228
+ signal,
2229
+ recentTools,
2230
+ alreadySurfaced,
2231
+ ).catch(() => []),
2232
+ ),
2233
+ )
2234
+ // alreadySurfaced is filtered inside the selector so Sonnet spends its
2235
+ // 5-slot budget on fresh candidates; readFileState catches files the
2236
+ // model read via FileReadTool. The redundant alreadySurfaced check here
2237
+ // is a belt-and-suspenders guard (multi-dir results may re-introduce a
2238
+ // path the selector filtered in a different dir).
2239
+ const selected = allResults
2240
+ .flat()
2241
+ .filter(m => !readFileState.has(m.path) && !alreadySurfaced.has(m.path))
2242
+ .slice(0, 5)
2243
+
2244
+ const memories = await readMemoriesForSurfacing(selected, signal)
2245
+
2246
+ if (memories.length === 0) {
2247
+ return []
2248
+ }
2249
+ return [{ type: 'relevant_memories' as const, memories }]
2250
+ }
2251
+
2252
+ /**
2253
+ * Scan messages for past relevant_memories attachments. Returns both the
2254
+ * set of surfaced paths (for selector de-dup) and cumulative byte count
2255
+ * (for session-total throttle). Scanning messages rather than tracking
2256
+ * in toolUseContext means compact naturally resets both — old attachments
2257
+ * are gone from the compacted transcript, so re-surfacing is valid again.
2258
+ */
2259
+ export function collectSurfacedMemories(messages: ReadonlyArray<Message>): {
2260
+ paths: Set<string>
2261
+ totalBytes: number
2262
+ } {
2263
+ const paths = new Set<string>()
2264
+ let totalBytes = 0
2265
+ for (const m of messages) {
2266
+ if (m.type === 'attachment' && m.attachment.type === 'relevant_memories') {
2267
+ for (const mem of m.attachment.memories) {
2268
+ paths.add(mem.path)
2269
+ totalBytes += mem.content.length
2270
+ }
2271
+ }
2272
+ }
2273
+ return { paths, totalBytes }
2274
+ }
2275
+
2276
+ /**
2277
+ * Reads a set of relevance-ranked memory files for injection as
2278
+ * <system-reminder> attachments. Enforces both MAX_MEMORY_LINES and
2279
+ * MAX_MEMORY_BYTES via readFileInRange's truncateOnByteLimit option.
2280
+ * Truncation surfaces partial
2281
+ * content with a note rather than dropping the file — findRelevantMemories
2282
+ * already picked this as most-relevant, so the frontmatter + opening context
2283
+ * is worth surfacing even if later lines are cut.
2284
+ *
2285
+ * Exported for direct testing without mocking the ranker + GB gates.
2286
+ */
2287
+ export async function readMemoriesForSurfacing(
2288
+ selected: ReadonlyArray<{ path: string; mtimeMs: number }>,
2289
+ signal?: AbortSignal,
2290
+ ): Promise<
2291
+ Array<{
2292
+ path: string
2293
+ content: string
2294
+ mtimeMs: number
2295
+ header: string
2296
+ limit?: number
2297
+ }>
2298
+ > {
2299
+ const results = await Promise.all(
2300
+ selected.map(async ({ path: filePath, mtimeMs }) => {
2301
+ try {
2302
+ const result = await readFileInRange(
2303
+ filePath,
2304
+ 0,
2305
+ MAX_MEMORY_LINES,
2306
+ MAX_MEMORY_BYTES,
2307
+ signal,
2308
+ { truncateOnByteLimit: true },
2309
+ )
2310
+ const truncated =
2311
+ result.totalLines > MAX_MEMORY_LINES || result.truncatedByBytes
2312
+ const content = truncated
2313
+ ? result.content +
2314
+ `\n\n> This memory file was truncated (${result.truncatedByBytes ? `${MAX_MEMORY_BYTES} byte limit` : `first ${MAX_MEMORY_LINES} lines`}). Use the ${FILE_READ_TOOL_NAME} tool to view the complete file at: ${filePath}`
2315
+ : result.content
2316
+ return {
2317
+ path: filePath,
2318
+ content,
2319
+ mtimeMs,
2320
+ header: memoryHeader(filePath, mtimeMs),
2321
+ limit: truncated ? result.lineCount : undefined,
2322
+ }
2323
+ } catch {
2324
+ return null
2325
+ }
2326
+ }),
2327
+ )
2328
+ return results.filter(r => r !== null)
2329
+ }
2330
+
2331
+ /**
2332
+ * Header string for a relevant-memory block. Exported so messages.ts
2333
+ * can fall back for resumed sessions where the stored header is missing.
2334
+ */
2335
+ export function memoryHeader(path: string, mtimeMs: number): string {
2336
+ const staleness = memoryFreshnessText(mtimeMs)
2337
+ return staleness
2338
+ ? `${staleness}\n\nMemory: ${path}:`
2339
+ : `Memory (saved ${memoryAge(mtimeMs)}): ${path}:`
2340
+ }
2341
+
2342
+ /**
2343
+ * A memory relevance-selector prefetch handle. The promise is started once
2344
+ * per user turn and runs while the main model streams and tools execute.
2345
+ * At the collect point (post-tools), the caller reads settledAt to
2346
+ * consume-if-ready or skip-and-retry-next-iteration — the prefetch never
2347
+ * blocks the turn.
2348
+ *
2349
+ * Disposable: query.ts binds with `using`, so [Symbol.dispose] fires on all
2350
+ * generator exit paths (return, throw, .return() closure) — aborting the
2351
+ * in-flight request and emitting terminal telemetry without instrumenting
2352
+ * each of the ~13 return sites inside the while loop.
2353
+ */
2354
+ export type MemoryPrefetch = {
2355
+ promise: Promise<Attachment[]>
2356
+ /** Set by promise.finally(). null until the promise settles. */
2357
+ settledAt: number | null
2358
+ /** Set by the collect point in query.ts. -1 until consumed. */
2359
+ consumedOnIteration: number
2360
+ [Symbol.dispose](): void
2361
+ }
2362
+
2363
+ /**
2364
+ * Starts the relevant memory search as an async prefetch.
2365
+ * Extracts the last real user prompt from messages (skipping isMeta system
2366
+ * injections) and kicks off a non-blocking search. Returns a Disposable
2367
+ * handle with settlement tracking. Bound with `using` in query.ts.
2368
+ */
2369
+ export function startRelevantMemoryPrefetch(
2370
+ messages: ReadonlyArray<Message>,
2371
+ toolUseContext: ToolUseContext,
2372
+ ): MemoryPrefetch | undefined {
2373
+ if (
2374
+ !isAutoMemoryEnabled() ||
2375
+ !getFeatureValue_CACHED_MAY_BE_STALE('tengu_moth_copse', false)
2376
+ ) {
2377
+ return undefined
2378
+ }
2379
+
2380
+ const lastUserMessage = messages.findLast(m => m.type === 'user' && !m.isMeta)
2381
+ if (!lastUserMessage) {
2382
+ return undefined
2383
+ }
2384
+
2385
+ const input = getUserMessageText(lastUserMessage)
2386
+ // Single-word prompts lack enough context for meaningful term extraction
2387
+ if (!input || !/\s/.test(input.trim())) {
2388
+ return undefined
2389
+ }
2390
+
2391
+ const surfaced = collectSurfacedMemories(messages)
2392
+ if (surfaced.totalBytes >= RELEVANT_MEMORIES_CONFIG.MAX_SESSION_BYTES) {
2393
+ return undefined
2394
+ }
2395
+
2396
+ // Chained to the turn-level abort so user Escape cancels the sideQuery
2397
+ // immediately, not just on [Symbol.dispose] when queryLoop exits.
2398
+ const controller = createChildAbortController(toolUseContext.abortController)
2399
+ const firedAt = Date.now()
2400
+ const promise = getRelevantMemoryAttachments(
2401
+ input,
2402
+ toolUseContext.options.agentDefinitions.activeAgents,
2403
+ toolUseContext.readFileState,
2404
+ collectRecentSuccessfulTools(messages, lastUserMessage),
2405
+ controller.signal,
2406
+ surfaced.paths,
2407
+ ).catch(e => {
2408
+ if (!isAbortError(e)) {
2409
+ logError(e)
2410
+ }
2411
+ return []
2412
+ })
2413
+
2414
+ const handle: MemoryPrefetch = {
2415
+ promise,
2416
+ settledAt: null,
2417
+ consumedOnIteration: -1,
2418
+ [Symbol.dispose]() {
2419
+ controller.abort()
2420
+ logEvent('tengu_memdir_prefetch_collected', {
2421
+ hidden_by_first_iteration:
2422
+ handle.settledAt !== null && handle.consumedOnIteration === 0,
2423
+ consumed_on_iteration: handle.consumedOnIteration,
2424
+ latency_ms: (handle.settledAt ?? Date.now()) - firedAt,
2425
+ })
2426
+ },
2427
+ }
2428
+ void promise.finally(() => {
2429
+ handle.settledAt = Date.now()
2430
+ })
2431
+ return handle
2432
+ }
2433
+
2434
+ /**
2435
+ * XMem Memory Prefetch - retrieves context from XMem for the current query.
2436
+ * Similar to startRelevantMemoryPrefetch but uses XMem vector memory instead of file-based memory.
2437
+ */
2438
+ export function startXMemPrefetch(
2439
+ messages: ReadonlyArray<Message>,
2440
+ toolUseContext: ToolUseContext,
2441
+ ): MemoryPrefetch | undefined {
2442
+ // Only prefetch if XMem is enabled (now true by default)
2443
+ const settings = toolUseContext.options.xmemSettings ?? { xmemMemoryEnabled: true }
2444
+ if (!settings.xmemMemoryEnabled) {
2445
+ return undefined
2446
+ }
2447
+
2448
+ const lastUserMessage = messages.findLast(m => m.type === 'user' && !m.isMeta)
2449
+ if (!lastUserMessage) {
2450
+ return undefined
2451
+ }
2452
+
2453
+ const input = getUserMessageText(lastUserMessage)
2454
+ // Skip very short queries
2455
+ if (!input || input.trim().length < 3) {
2456
+ return undefined
2457
+ }
2458
+
2459
+ // Chained to the turn-level abort
2460
+ const controller = createChildAbortController(toolUseContext.abortController)
2461
+ const firedAt = Date.now()
2462
+
2463
+ const promise = getXMemContextAttachment(input, controller.signal).catch(e => {
2464
+ if (!isAbortError(e)) {
2465
+ logError(e)
2466
+ }
2467
+ return []
2468
+ })
2469
+
2470
+ const handle: MemoryPrefetch = {
2471
+ promise,
2472
+ settledAt: null,
2473
+ consumedOnIteration: -1,
2474
+ [Symbol.dispose]() {
2475
+ controller.abort()
2476
+ logEvent('tengu_xmem_prefetch_collected', {
2477
+ hidden_by_first_iteration: handle.settledAt !== null && handle.consumedOnIteration === 0,
2478
+ consumed_on_iteration: handle.consumedOnIteration,
2479
+ latency_ms: (handle.settledAt ?? Date.now()) - firedAt,
2480
+ })
2481
+ },
2482
+ }
2483
+ void promise.finally(() => {
2484
+ handle.settledAt = Date.now()
2485
+ })
2486
+ return handle
2487
+ }
2488
+
2489
+ /**
2490
+ * Retrieve context from XMem and return as attachment.
2491
+ */
2492
+ async function getXMemContextAttachment(
2493
+ query: string,
2494
+ signal: AbortSignal,
2495
+ ): Promise<Attachment[]> {
2496
+ try {
2497
+ const { xmemClient } = await import('./xmem.js')
2498
+ const result = await xmemClient.retrieve({ query, user_id: 'default_user' })
2499
+
2500
+ if (signal.aborted) {
2501
+ return []
2502
+ }
2503
+
2504
+ if (!result.answer || result.answer.trim().length === 0) {
2505
+ return []
2506
+ }
2507
+
2508
+ // Return as a special xmem_context attachment type
2509
+ return [{
2510
+ type: 'xmem_context' as const,
2511
+ content: result.answer,
2512
+ query: query.substring(0, 100),
2513
+ }]
2514
+ } catch (error) {
2515
+ logForDebugging(`[XMem Prefetch] Error: ${error}`)
2516
+ return []
2517
+ }
2518
+ }
2519
+
2520
+ type ToolResultBlock = {
2521
+ type: 'tool_result'
2522
+ tool_use_id: string
2523
+ is_error?: boolean
2524
+ }
2525
+
2526
+ function isToolResultBlock(b: unknown): b is ToolResultBlock {
2527
+ return (
2528
+ typeof b === 'object' &&
2529
+ b !== null &&
2530
+ (b as ToolResultBlock).type === 'tool_result' &&
2531
+ typeof (b as ToolResultBlock).tool_use_id === 'string'
2532
+ )
2533
+ }
2534
+
2535
+ /**
2536
+ * Check whether a user message's content contains tool_result blocks.
2537
+ * This is more reliable than checking `toolUseResult === undefined` because
2538
+ * sub-agent tool result messages explicitly set `toolUseResult` to `undefined`
2539
+ * when `preserveToolUseResults` is false (the default for Explore agents).
2540
+ */
2541
+ function hasToolResultContent(content: unknown): boolean {
2542
+ return Array.isArray(content) && content.some(isToolResultBlock)
2543
+ }
2544
+
2545
+ /**
2546
+ * Tools that succeeded (and never errored) since the previous real turn
2547
+ * boundary. The memory selector uses this to suppress docs about tools
2548
+ * that are working — surfacing reference material for a tool the model
2549
+ * is already calling successfully is noise.
2550
+ *
2551
+ * Any error → tool excluded (model is struggling, docs stay available).
2552
+ * No result yet → also excluded (outcome unknown).
2553
+ *
2554
+ * tool_use lives in assistant content; tool_result in user content
2555
+ * (toolUseResult set, isMeta undefined). Both are within the scan window.
2556
+ * Backward scan sees results before uses so we collect both by id and
2557
+ * resolve after.
2558
+ */
2559
+ export function collectRecentSuccessfulTools(
2560
+ messages: ReadonlyArray<Message>,
2561
+ lastUserMessage: Message,
2562
+ ): readonly string[] {
2563
+ const useIdToName = new Map<string, string>()
2564
+ const resultByUseId = new Map<string, boolean>()
2565
+ for (let i = messages.length - 1; i >= 0; i--) {
2566
+ const m = messages[i]
2567
+ if (!m) continue
2568
+ if (isHumanTurn(m) && m !== lastUserMessage) break
2569
+ if (m.type === 'assistant' && typeof m.message.content !== 'string') {
2570
+ for (const block of m.message.content) {
2571
+ if (block.type === 'tool_use') useIdToName.set(block.id, block.name)
2572
+ }
2573
+ } else if (
2574
+ m.type === 'user' &&
2575
+ 'message' in m &&
2576
+ Array.isArray(m.message.content)
2577
+ ) {
2578
+ for (const block of m.message.content) {
2579
+ if (isToolResultBlock(block)) {
2580
+ resultByUseId.set(block.tool_use_id, block.is_error === true)
2581
+ }
2582
+ }
2583
+ }
2584
+ }
2585
+ const failed = new Set<string>()
2586
+ const succeeded = new Set<string>()
2587
+ for (const [id, name] of useIdToName) {
2588
+ const errored = resultByUseId.get(id)
2589
+ if (errored === undefined) continue
2590
+ if (errored) {
2591
+ failed.add(name)
2592
+ } else {
2593
+ succeeded.add(name)
2594
+ }
2595
+ }
2596
+ return [...succeeded].filter(t => !failed.has(t))
2597
+ }
2598
+
2599
+
2600
+ /**
2601
+ * Filters prefetched memory attachments to exclude memories the model already
2602
+ * has in context via FileRead/Write/Edit tool calls (any iteration this turn)
2603
+ * or a previous turn's memory surfacing — both tracked in the cumulative
2604
+ * readFileState. Survivors are then marked in readFileState so subsequent
2605
+ * turns won't re-surface them.
2606
+ *
2607
+ * The mark-after-filter ordering is load-bearing: readMemoriesForSurfacing
2608
+ * used to write to readFileState during the prefetch, which meant the filter
2609
+ * saw every prefetch-selected path as "already in context" and dropped them
2610
+ * all (self-referential filter). Deferring the write to here, after the
2611
+ * filter runs, breaks that cycle while still deduping against tool calls
2612
+ * from any iteration.
2613
+ */
2614
+ export function filterDuplicateMemoryAttachments(
2615
+ attachments: Attachment[],
2616
+ readFileState: FileStateCache,
2617
+ ): Attachment[] {
2618
+ return attachments
2619
+ .map(attachment => {
2620
+ if (attachment.type !== 'relevant_memories') return attachment
2621
+ const filtered = attachment.memories.filter(
2622
+ m => !readFileState.has(m.path),
2623
+ )
2624
+ for (const m of filtered) {
2625
+ readFileState.set(m.path, {
2626
+ content: m.content,
2627
+ timestamp: m.mtimeMs,
2628
+ offset: undefined,
2629
+ limit: m.limit,
2630
+ })
2631
+ }
2632
+ return filtered.length > 0 ? { ...attachment, memories: filtered } : null
2633
+ })
2634
+ .filter((a): a is Attachment => a !== null)
2635
+ }
2636
+
2637
+ /**
2638
+ * Processes skill directories that were discovered during file operations.
2639
+ * Uses dynamicSkillDirTriggers field from ToolUseContext
2640
+ */
2641
+ async function getDynamicSkillAttachments(
2642
+ toolUseContext: ToolUseContext,
2643
+ ): Promise<Attachment[]> {
2644
+ const attachments: Attachment[] = []
2645
+
2646
+ if (
2647
+ toolUseContext.dynamicSkillDirTriggers &&
2648
+ toolUseContext.dynamicSkillDirTriggers.size > 0
2649
+ ) {
2650
+ // Parallelize: readdir all skill dirs concurrently
2651
+ const perDirResults = await Promise.all(
2652
+ Array.from(toolUseContext.dynamicSkillDirTriggers).map(async skillDir => {
2653
+ try {
2654
+ const entries = await readdir(skillDir, { withFileTypes: true })
2655
+ const candidates = entries
2656
+ .filter(e => e.isDirectory() || e.isSymbolicLink())
2657
+ .map(e => e.name)
2658
+ // Parallelize: stat all SKILL.md candidates concurrently
2659
+ const checked = await Promise.all(
2660
+ candidates.map(async name => {
2661
+ try {
2662
+ await stat(resolve(skillDir, name, 'SKILL.md'))
2663
+ return name
2664
+ } catch {
2665
+ return null // SKILL.md doesn't exist, skip this entry
2666
+ }
2667
+ }),
2668
+ )
2669
+ return {
2670
+ skillDir,
2671
+ skillNames: checked.filter((n): n is string => n !== null),
2672
+ }
2673
+ } catch {
2674
+ // Ignore errors reading skill directories (e.g., directory doesn't exist)
2675
+ return { skillDir, skillNames: [] }
2676
+ }
2677
+ }),
2678
+ )
2679
+
2680
+ for (const { skillDir, skillNames } of perDirResults) {
2681
+ if (skillNames.length > 0) {
2682
+ attachments.push({
2683
+ type: 'dynamic_skill',
2684
+ skillDir,
2685
+ skillNames,
2686
+ displayPath: relative(getCwd(), skillDir),
2687
+ })
2688
+ }
2689
+ }
2690
+
2691
+ toolUseContext.dynamicSkillDirTriggers.clear()
2692
+ }
2693
+
2694
+ return attachments
2695
+ }
2696
+
2697
+ // Track which skills have been sent to avoid re-sending. Keyed by agentId
2698
+ // (empty string = main thread) so subagents get their own turn-0 listing —
2699
+ // without per-agent scoping, the main thread populating this Set would cause
2700
+ // every subagent's filterToBundledAndMcp result to dedup to empty.
2701
+ const sentSkillNames = new Map<string, Set<string>>()
2702
+
2703
+ // Called when the skill set genuinely changes (plugin reload, skill file
2704
+ // change on disk) so new skills get announced. NOT called on compact —
2705
+ // post-compact re-injection costs ~4K tokens/event for marginal benefit.
2706
+ export function resetSentSkillNames(): void {
2707
+ sentSkillNames.clear()
2708
+ suppressNext = false
2709
+ }
2710
+
2711
+ /**
2712
+ * Suppress the next skill-listing injection. Called by conversationRecovery
2713
+ * on --resume when a skill_listing attachment already exists in the
2714
+ * transcript.
2715
+ *
2716
+ * `sentSkillNames` is module-scope — process-local. Each `claude -p` spawn
2717
+ * starts with an empty Map, so without this every resume re-injects the
2718
+ * full ~600-token listing even though it's already in the conversation from
2719
+ * the prior process. Shows up on every --resume; particularly loud for
2720
+ * daemons that respawn frequently.
2721
+ *
2722
+ * Trade-off: skills added between sessions won't be announced until the
2723
+ * next non-resume session. Acceptable — skill_listing was never meant to
2724
+ * cover cross-process deltas, and the agent can still call them (they're
2725
+ * in the Skill tool's runtime registry regardless).
2726
+ */
2727
+ export function suppressNextSkillListing(): void {
2728
+ suppressNext = true
2729
+ }
2730
+ let suppressNext = false
2731
+
2732
+ // When skill-search is enabled and the filtered (bundled + MCP) listing exceeds
2733
+ // this count, fall back to bundled-only. Protects MCP-heavy users (100+ servers)
2734
+ // from truncation while keeping the turn-0 guarantee for typical setups.
2735
+ const FILTERED_LISTING_MAX = 30
2736
+
2737
+ /**
2738
+ * Filter skills to bundled (Anthropic-curated) + MCP (user-connected) only.
2739
+ * Used when skill-search is enabled to resolve the turn-0 gap for subagents:
2740
+ * these sources are small, intent-signaled, and won't hit the truncation budget.
2741
+ * User/project/plugin skills (the long tail — 200+) go through discovery instead.
2742
+ *
2743
+ * Falls back to bundled-only if bundled+mcp exceeds FILTERED_LISTING_MAX.
2744
+ */
2745
+ export function filterToBundledAndMcp(commands: Command[]): Command[] {
2746
+ const filtered = commands.filter(
2747
+ cmd => cmd.loadedFrom === 'bundled' || cmd.loadedFrom === 'mcp',
2748
+ )
2749
+ if (filtered.length > FILTERED_LISTING_MAX) {
2750
+ return filtered.filter(cmd => cmd.loadedFrom === 'bundled')
2751
+ }
2752
+ return filtered
2753
+ }
2754
+
2755
+ async function getSkillListingAttachments(
2756
+ toolUseContext: ToolUseContext,
2757
+ ): Promise<Attachment[]> {
2758
+ if (process.env.NODE_ENV === 'test') {
2759
+ return []
2760
+ }
2761
+
2762
+ // Skip skill listing for agents that don't have the Skill tool — they can't use skills directly.
2763
+ if (
2764
+ !toolUseContext.options.tools.some(t => toolMatchesName(t, SKILL_TOOL_NAME))
2765
+ ) {
2766
+ return []
2767
+ }
2768
+
2769
+ const cwd = getProjectRoot()
2770
+ const localCommands = await getSkillToolCommands(cwd)
2771
+ const mcpSkills = getMcpSkillCommands(
2772
+ toolUseContext.getAppState().mcp.commands,
2773
+ )
2774
+ let allCommands =
2775
+ mcpSkills.length > 0
2776
+ ? uniqBy([...localCommands, ...mcpSkills], 'name')
2777
+ : localCommands
2778
+
2779
+ // When skill search is active, filter to bundled + MCP instead of full
2780
+ // suppression. Resolves the turn-0 gap: main thread gets turn-0 discovery
2781
+ // via getTurnZeroSkillDiscovery (blocking), but subagents use the async
2782
+ // subagent_spawn signal (collected post-tools, visible turn 1). Bundled +
2783
+ // MCP are small and intent-signaled; user/project/plugin skills go through
2784
+ // discovery. feature() first for DCE — the property-access string leaks
2785
+ // otherwise even with ?. on null.
2786
+ if (
2787
+ feature('EXPERIMENTAL_SKILL_SEARCH') &&
2788
+ skillSearchModules?.featureCheck.isSkillSearchEnabled()
2789
+ ) {
2790
+ allCommands = filterToBundledAndMcp(allCommands)
2791
+ }
2792
+
2793
+ const agentKey = toolUseContext.agentId ?? ''
2794
+ let sent = sentSkillNames.get(agentKey)
2795
+ if (!sent) {
2796
+ sent = new Set()
2797
+ sentSkillNames.set(agentKey, sent)
2798
+ }
2799
+
2800
+ // Resume path: prior process already injected a listing; it's in the
2801
+ // transcript. Mark everything current as sent so only post-resume deltas
2802
+ // (skills loaded later via /reload-plugins etc) get announced.
2803
+ if (suppressNext) {
2804
+ suppressNext = false
2805
+ for (const cmd of allCommands) {
2806
+ sent.add(cmd.name)
2807
+ }
2808
+ return []
2809
+ }
2810
+
2811
+ // Find skills we haven't sent yet
2812
+ const newSkills = allCommands.filter(cmd => !sent.has(cmd.name))
2813
+
2814
+ if (newSkills.length === 0) {
2815
+ return []
2816
+ }
2817
+
2818
+ // If no skills have been sent yet, this is the initial batch
2819
+ const isInitial = sent.size === 0
2820
+
2821
+ // Mark as sent
2822
+ for (const cmd of newSkills) {
2823
+ sent.add(cmd.name)
2824
+ }
2825
+
2826
+ logForDebugging(
2827
+ `Sending ${newSkills.length} skills via attachment (${isInitial ? 'initial' : 'dynamic'}, ${sent.size} total sent)`,
2828
+ )
2829
+
2830
+ // Format within budget using existing logic
2831
+ const contextWindowTokens = getContextWindowForModel(
2832
+ toolUseContext.options.mainLoopModel,
2833
+ getSdkBetas(),
2834
+ )
2835
+ const content = formatCommandsWithinBudget(newSkills, contextWindowTokens)
2836
+
2837
+ return [
2838
+ {
2839
+ type: 'skill_listing',
2840
+ content,
2841
+ skillCount: newSkills.length,
2842
+ isInitial,
2843
+ },
2844
+ ]
2845
+ }
2846
+
2847
+ // getSkillDiscoveryAttachment moved to skillSearch/prefetch.ts as
2848
+ // getTurnZeroSkillDiscovery — keeps the 'skill_discovery' string literal inside
2849
+ // a feature-gated module so it doesn't leak into external builds.
2850
+
2851
+ export function extractAtMentionedFiles(content: string): string[] {
2852
+ // Extract filenames mentioned with @ symbol, including line range syntax: @file.txt#L10-20
2853
+ // Also supports quoted paths for files with spaces: @"my/file with spaces.txt"
2854
+ // Example: "foo bar @baz moo" would extract "baz"
2855
+ // Example: 'check @"my file.txt" please' would extract "my file.txt"
2856
+
2857
+ // Two patterns: quoted paths and regular paths
2858
+ const quotedAtMentionRegex = /(^|\s)@"([^"]+)"/g
2859
+ const regularAtMentionRegex = /(^|\s)@([^\s]+)\b/g
2860
+
2861
+ const quotedMatches: string[] = []
2862
+ const regularMatches: string[] = []
2863
+
2864
+ // Extract quoted mentions first (skip agent mentions like @"code-reviewer (agent)")
2865
+ let match
2866
+ while ((match = quotedAtMentionRegex.exec(content)) !== null) {
2867
+ if (match[2] && !match[2].endsWith(' (agent)')) {
2868
+ quotedMatches.push(match[2]) // The content inside quotes
2869
+ }
2870
+ }
2871
+
2872
+ // Extract regular mentions
2873
+ const regularMatchArray = content.match(regularAtMentionRegex) || []
2874
+ regularMatchArray.forEach(match => {
2875
+ const filename = match.slice(match.indexOf('@') + 1)
2876
+ // Don't include if it starts with a quote (already handled as quoted)
2877
+ if (!filename.startsWith('"')) {
2878
+ regularMatches.push(filename)
2879
+ }
2880
+ })
2881
+
2882
+ // Combine and deduplicate
2883
+ return uniq([...quotedMatches, ...regularMatches])
2884
+ }
2885
+
2886
+ export function extractMcpResourceMentions(content: string): string[] {
2887
+ // Extract MCP resources mentioned with @ symbol in format @server:uri
2888
+ // Example: "@server1:resource/path" would extract "server1:resource/path"
2889
+ const atMentionRegex = /(^|\s)@([^\s]+:[^\s]+)\b/g
2890
+ const matches = content.match(atMentionRegex) || []
2891
+
2892
+ // Remove the prefix (everything before @) from each match
2893
+ return uniq(matches.map(match => match.slice(match.indexOf('@') + 1)))
2894
+ }
2895
+
2896
+ export function extractAgentMentions(content: string): string[] {
2897
+ // Extract agent mentions in two formats:
2898
+ // 1. @agent-<agent-type> (legacy/manual typing)
2899
+ // Example: "@agent-code-elegance-refiner" → "agent-code-elegance-refiner"
2900
+ // 2. @"<agent-type> (agent)" (from autocomplete selection)
2901
+ // Example: '@"code-reviewer (agent)"' → "code-reviewer"
2902
+ // Supports colons, dots, and at-signs for plugin-scoped agents like "@agent-asana:project-status-updater"
2903
+ const results: string[] = []
2904
+
2905
+ // Match quoted format: @"<type> (agent)"
2906
+ const quotedAgentRegex = /(^|\s)@"([\w:.@-]+) \(agent\)"/g
2907
+ let match
2908
+ while ((match = quotedAgentRegex.exec(content)) !== null) {
2909
+ if (match[2]) {
2910
+ results.push(match[2])
2911
+ }
2912
+ }
2913
+
2914
+ // Match unquoted format: @agent-<type>
2915
+ const unquotedAgentRegex = /(^|\s)@(agent-[\w:.@-]+)/g
2916
+ const unquotedMatches = content.match(unquotedAgentRegex) || []
2917
+ for (const m of unquotedMatches) {
2918
+ results.push(m.slice(m.indexOf('@') + 1))
2919
+ }
2920
+
2921
+ return uniq(results)
2922
+ }
2923
+
2924
+ interface AtMentionedFileLines {
2925
+ filename: string
2926
+ lineStart?: number
2927
+ lineEnd?: number
2928
+ }
2929
+
2930
+ export function parseAtMentionedFileLines(
2931
+ mention: string,
2932
+ ): AtMentionedFileLines {
2933
+ // Parse mentions like "file.txt#L10-20", "file.txt#heading", or just "file.txt"
2934
+ // Supports line ranges (#L10, #L10-20) and strips non-line-range fragments (#heading)
2935
+ const match = mention.match(/^([^#]+)(?:#L(\d+)(?:-(\d+))?)?(?:#[^#]*)?$/)
2936
+
2937
+ if (!match) {
2938
+ return { filename: mention }
2939
+ }
2940
+
2941
+ const [, filename, lineStartStr, lineEndStr] = match
2942
+ const lineStart = lineStartStr ? parseInt(lineStartStr, 10) : undefined
2943
+ const lineEnd = lineEndStr ? parseInt(lineEndStr, 10) : lineStart
2944
+
2945
+ return { filename: filename ?? mention, lineStart, lineEnd }
2946
+ }
2947
+
2948
+ async function getDiagnosticAttachments(
2949
+ toolUseContext: ToolUseContext,
2950
+ ): Promise<Attachment[]> {
2951
+ // Diagnostics are only useful if the agent has the Bash tool to act on them
2952
+ if (
2953
+ !toolUseContext.options.tools.some(t => toolMatchesName(t, BASH_TOOL_NAME))
2954
+ ) {
2955
+ return []
2956
+ }
2957
+
2958
+ // Get new diagnostics from the tracker (IDE diagnostics via MCP)
2959
+ const newDiagnostics = await diagnosticTracker.getNewDiagnostics()
2960
+ if (newDiagnostics.length === 0) {
2961
+ return []
2962
+ }
2963
+
2964
+ return [
2965
+ {
2966
+ type: 'diagnostics',
2967
+ files: newDiagnostics,
2968
+ isNew: true,
2969
+ },
2970
+ ]
2971
+ }
2972
+
2973
+ /**
2974
+ * Get LSP diagnostic attachments from passive LSP servers.
2975
+ * Follows the AsyncHookRegistry pattern for consistent async attachment delivery.
2976
+ */
2977
+ async function getLSPDiagnosticAttachments(
2978
+ toolUseContext: ToolUseContext,
2979
+ ): Promise<Attachment[]> {
2980
+ // LSP diagnostics are only useful if the agent has the Bash tool to act on them
2981
+ if (
2982
+ !toolUseContext.options.tools.some(t => toolMatchesName(t, BASH_TOOL_NAME))
2983
+ ) {
2984
+ return []
2985
+ }
2986
+
2987
+ logForDebugging('LSP Diagnostics: getLSPDiagnosticAttachments called')
2988
+
2989
+ try {
2990
+ const diagnosticSets = checkForLSPDiagnostics()
2991
+
2992
+ if (diagnosticSets.length === 0) {
2993
+ return []
2994
+ }
2995
+
2996
+ logForDebugging(
2997
+ `LSP Diagnostics: Found ${diagnosticSets.length} pending diagnostic set(s)`,
2998
+ )
2999
+
3000
+ // Convert each diagnostic set to an attachment
3001
+ const attachments: Attachment[] = diagnosticSets.map(({ files }) => ({
3002
+ type: 'diagnostics' as const,
3003
+ files,
3004
+ isNew: true,
3005
+ }))
3006
+
3007
+ // Clear delivered diagnostics from registry to prevent memory leak
3008
+ // Follows same pattern as removeDeliveredAsyncHooks
3009
+ if (diagnosticSets.length > 0) {
3010
+ clearAllLSPDiagnostics()
3011
+ logForDebugging(
3012
+ `LSP Diagnostics: Cleared ${diagnosticSets.length} delivered diagnostic(s) from registry`,
3013
+ )
3014
+ }
3015
+
3016
+ logForDebugging(
3017
+ `LSP Diagnostics: Returning ${attachments.length} diagnostic attachment(s)`,
3018
+ )
3019
+
3020
+ return attachments
3021
+ } catch (error) {
3022
+ const err = toError(error)
3023
+ logError(
3024
+ new Error(`Failed to get LSP diagnostic attachments: ${err.message}`),
3025
+ )
3026
+ // Return empty array to allow other attachments to proceed
3027
+ return []
3028
+ }
3029
+ }
3030
+
3031
+ export async function* getAttachmentMessages(
3032
+ input: string | null,
3033
+ toolUseContext: ToolUseContext,
3034
+ ideSelection: IDESelection | null,
3035
+ queuedCommands: QueuedCommand[],
3036
+ messages?: Message[],
3037
+ querySource?: QuerySource,
3038
+ options?: { skipSkillDiscovery?: boolean },
3039
+ ): AsyncGenerator<AttachmentMessage, void> {
3040
+ // TODO: Compute this upstream
3041
+ const attachments = await getAttachments(
3042
+ input,
3043
+ toolUseContext,
3044
+ ideSelection,
3045
+ queuedCommands,
3046
+ messages,
3047
+ querySource,
3048
+ options,
3049
+ )
3050
+
3051
+ if (attachments.length === 0) {
3052
+ return
3053
+ }
3054
+
3055
+ logEvent('tengu_attachments', {
3056
+ attachment_types: attachments.map(
3057
+ _ => _.type,
3058
+ ) as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
3059
+ })
3060
+
3061
+ for (const attachment of attachments) {
3062
+ yield createAttachmentMessage(attachment)
3063
+ }
3064
+ }
3065
+
3066
+ /**
3067
+ * Generates a file attachment by reading a file with proper validation and truncation.
3068
+ * This is the core file reading logic shared between @-mentioned files and post-compact restoration.
3069
+ *
3070
+ * @param filename The absolute path to the file to read
3071
+ * @param toolUseContext The tool use context for calling FileReadTool
3072
+ * @param options Optional configuration for file reading
3073
+ * @returns A new_file attachment or null if the file couldn't be read
3074
+ */
3075
+ /**
3076
+ * Check if a PDF file should be represented as a lightweight reference
3077
+ * instead of being inlined. Returns a PDFReferenceAttachment for large PDFs
3078
+ * (more than PDF_AT_MENTION_INLINE_THRESHOLD pages), or null otherwise.
3079
+ */
3080
+ export async function tryGetPDFReference(
3081
+ filename: string,
3082
+ ): Promise<PDFReferenceAttachment | null> {
3083
+ const ext = parse(filename).ext.toLowerCase()
3084
+ if (!isPDFExtension(ext)) {
3085
+ return null
3086
+ }
3087
+ try {
3088
+ const [stats, pageCount] = await Promise.all([
3089
+ getFsImplementation().stat(filename),
3090
+ getPDFPageCount(filename),
3091
+ ])
3092
+ // Use page count if available, otherwise fall back to size heuristic (~100KB per page)
3093
+ const effectivePageCount = pageCount ?? Math.ceil(stats.size / (100 * 1024))
3094
+ if (effectivePageCount > PDF_AT_MENTION_INLINE_THRESHOLD) {
3095
+ logEvent('tengu_pdf_reference_attachment', {
3096
+ pageCount: effectivePageCount,
3097
+ fileSize: stats.size,
3098
+ hadPdfinfo: pageCount !== null,
3099
+ } as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS)
3100
+ return {
3101
+ type: 'pdf_reference',
3102
+ filename,
3103
+ pageCount: effectivePageCount,
3104
+ fileSize: stats.size,
3105
+ displayPath: relative(getCwd(), filename),
3106
+ }
3107
+ }
3108
+ } catch {
3109
+ // If we can't stat the file, return null to proceed with normal reading
3110
+ }
3111
+ return null
3112
+ }
3113
+
3114
+ export async function generateFileAttachment(
3115
+ filename: string,
3116
+ toolUseContext: ToolUseContext,
3117
+ successEventName: string,
3118
+ errorEventName: string,
3119
+ mode: 'compact' | 'at-mention',
3120
+ options?: {
3121
+ offset?: number
3122
+ limit?: number
3123
+ },
3124
+ ): Promise<
3125
+ | FileAttachment
3126
+ | CompactFileReferenceAttachment
3127
+ | PDFReferenceAttachment
3128
+ | AlreadyReadFileAttachment
3129
+ | null
3130
+ > {
3131
+ const { offset, limit } = options ?? {}
3132
+
3133
+ // Check if file has a deny rule configured
3134
+ const appState = toolUseContext.getAppState()
3135
+ if (isFileReadDenied(filename, appState.toolPermissionContext)) {
3136
+ return null
3137
+ }
3138
+
3139
+ // Check file size before attempting to read (skip for PDFs — they have their own size/page handling below)
3140
+ if (
3141
+ mode === 'at-mention' &&
3142
+ !isFileWithinReadSizeLimit(
3143
+ filename,
3144
+ getDefaultFileReadingLimits().maxSizeBytes,
3145
+ )
3146
+ ) {
3147
+ const ext = parse(filename).ext.toLowerCase()
3148
+ if (!isPDFExtension(ext)) {
3149
+ try {
3150
+ const stats = await getFsImplementation().stat(filename)
3151
+ logEvent('tengu_attachment_file_too_large', {
3152
+ size_bytes: stats.size,
3153
+ mode,
3154
+ } as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS)
3155
+ return null
3156
+ } catch {
3157
+ // If we can't stat the file, proceed with normal reading (will fail later if file doesn't exist)
3158
+ }
3159
+ }
3160
+ }
3161
+
3162
+ // For large PDFs on @ mention, return a lightweight reference instead of inlining
3163
+ if (mode === 'at-mention') {
3164
+ const pdfRef = await tryGetPDFReference(filename)
3165
+ if (pdfRef) {
3166
+ return pdfRef
3167
+ }
3168
+ }
3169
+
3170
+ // Check if file is already in context with latest version
3171
+ const existingFileState = toolUseContext.readFileState.get(filename)
3172
+ if (existingFileState && mode === 'at-mention') {
3173
+ try {
3174
+ // Check if the file has been modified since we last read it
3175
+ const mtimeMs = await getFileModificationTimeAsync(filename)
3176
+
3177
+ // Handle timestamp format inconsistency:
3178
+ // - FileReadTool stores Date.now() (current time when read)
3179
+ // - FileEdit/WriteTools store mtimeMs (file modification time)
3180
+ //
3181
+ // If timestamp > mtimeMs, it was stored by FileReadTool using Date.now()
3182
+ // In this case, we should not use the optimization since we can't reliably
3183
+ // compare modification times. Only use optimization when timestamp <= mtimeMs,
3184
+ // indicating it was stored by FileEdit/WriteTool with actual mtimeMs.
3185
+
3186
+ if (
3187
+ existingFileState.timestamp <= mtimeMs &&
3188
+ mtimeMs === existingFileState.timestamp
3189
+ ) {
3190
+ // File hasn't been modified, return already_read_file attachment
3191
+ // This tells the system the file is already in context and doesn't need to be sent to API
3192
+ logEvent(successEventName, {})
3193
+ return {
3194
+ type: 'already_read_file',
3195
+ filename,
3196
+ displayPath: relative(getCwd(), filename),
3197
+ content: {
3198
+ type: 'text',
3199
+ file: {
3200
+ filePath: filename,
3201
+ content: existingFileState.content,
3202
+ numLines: countCharInString(existingFileState.content, '\n') + 1,
3203
+ startLine: offset ?? 1,
3204
+ totalLines:
3205
+ countCharInString(existingFileState.content, '\n') + 1,
3206
+ },
3207
+ },
3208
+ }
3209
+ }
3210
+ } catch {
3211
+ // If we can't stat the file, proceed with normal reading
3212
+ }
3213
+ }
3214
+
3215
+ try {
3216
+ const fileInput = {
3217
+ file_path: filename,
3218
+ offset,
3219
+ limit,
3220
+ }
3221
+
3222
+ async function readTruncatedFile(): Promise<
3223
+ | FileAttachment
3224
+ | CompactFileReferenceAttachment
3225
+ | AlreadyReadFileAttachment
3226
+ | null
3227
+ > {
3228
+ if (mode === 'compact') {
3229
+ return {
3230
+ type: 'compact_file_reference',
3231
+ filename,
3232
+ displayPath: relative(getCwd(), filename),
3233
+ }
3234
+ }
3235
+
3236
+ // Check deny rules before reading truncated file
3237
+ const appState = toolUseContext.getAppState()
3238
+ if (isFileReadDenied(filename, appState.toolPermissionContext)) {
3239
+ return null
3240
+ }
3241
+
3242
+ try {
3243
+ // Read only the first MAX_LINES_TO_READ lines for files that are too large
3244
+ const truncatedInput = {
3245
+ file_path: filename,
3246
+ offset: offset ?? 1,
3247
+ limit: MAX_LINES_TO_READ,
3248
+ }
3249
+ const result = await FileReadTool.call(truncatedInput, toolUseContext)
3250
+ logEvent(successEventName, {})
3251
+
3252
+ return {
3253
+ type: 'file' as const,
3254
+ filename,
3255
+ content: result.data,
3256
+ truncated: true,
3257
+ displayPath: relative(getCwd(), filename),
3258
+ }
3259
+ } catch {
3260
+ logEvent(errorEventName, {})
3261
+ return null
3262
+ }
3263
+ }
3264
+
3265
+ // Validate file path is valid
3266
+ const isValid = await FileReadTool.validateInput(fileInput, toolUseContext)
3267
+ if (!isValid.result) {
3268
+ return null
3269
+ }
3270
+
3271
+ try {
3272
+ const result = await FileReadTool.call(fileInput, toolUseContext)
3273
+ logEvent(successEventName, {})
3274
+ return {
3275
+ type: 'file',
3276
+ filename,
3277
+ content: result.data,
3278
+ displayPath: relative(getCwd(), filename),
3279
+ }
3280
+ } catch (error) {
3281
+ if (
3282
+ error instanceof MaxFileReadTokenExceededError ||
3283
+ error instanceof FileTooLargeError
3284
+ ) {
3285
+ return await readTruncatedFile()
3286
+ }
3287
+ throw error
3288
+ }
3289
+ } catch {
3290
+ logEvent(errorEventName, {})
3291
+ return null
3292
+ }
3293
+ }
3294
+
3295
+ export function createAttachmentMessage(
3296
+ attachment: Attachment,
3297
+ ): AttachmentMessage {
3298
+ return {
3299
+ attachment,
3300
+ type: 'attachment',
3301
+ uuid: randomUUID(),
3302
+ timestamp: new Date().toISOString(),
3303
+ }
3304
+ }
3305
+
3306
+ function getTodoReminderTurnCounts(messages: Message[]): {
3307
+ turnsSinceLastTodoWrite: number
3308
+ turnsSinceLastReminder: number
3309
+ } {
3310
+ let lastTodoWriteIndex = -1
3311
+ let lastReminderIndex = -1
3312
+ let assistantTurnsSinceWrite = 0
3313
+ let assistantTurnsSinceReminder = 0
3314
+
3315
+ // Iterate backwards to find most recent events
3316
+ for (let i = messages.length - 1; i >= 0; i--) {
3317
+ const message = messages[i]
3318
+
3319
+ if (message?.type === 'assistant') {
3320
+ if (isThinkingMessage(message)) {
3321
+ // Skip thinking messages
3322
+ continue
3323
+ }
3324
+
3325
+ // Check for TodoWrite usage BEFORE incrementing counter
3326
+ // (we don't want to count the TodoWrite message itself as "1 turn since write")
3327
+ if (
3328
+ lastTodoWriteIndex === -1 &&
3329
+ 'message' in message &&
3330
+ Array.isArray(message.message?.content) &&
3331
+ message.message.content.some(
3332
+ block => block.type === 'tool_use' && block.name === 'TodoWrite',
3333
+ )
3334
+ ) {
3335
+ lastTodoWriteIndex = i
3336
+ }
3337
+
3338
+ // Count assistant turns before finding events
3339
+ if (lastTodoWriteIndex === -1) assistantTurnsSinceWrite++
3340
+ if (lastReminderIndex === -1) assistantTurnsSinceReminder++
3341
+ } else if (
3342
+ lastReminderIndex === -1 &&
3343
+ message?.type === 'attachment' &&
3344
+ message.attachment.type === 'todo_reminder'
3345
+ ) {
3346
+ lastReminderIndex = i
3347
+ }
3348
+
3349
+ if (lastTodoWriteIndex !== -1 && lastReminderIndex !== -1) {
3350
+ break
3351
+ }
3352
+ }
3353
+
3354
+ return {
3355
+ turnsSinceLastTodoWrite: assistantTurnsSinceWrite,
3356
+ turnsSinceLastReminder: assistantTurnsSinceReminder,
3357
+ }
3358
+ }
3359
+
3360
+ async function getTodoReminderAttachments(
3361
+ messages: Message[] | undefined,
3362
+ toolUseContext: ToolUseContext,
3363
+ ): Promise<Attachment[]> {
3364
+ // Skip if TodoWrite tool is not available
3365
+ if (
3366
+ !toolUseContext.options.tools.some(t =>
3367
+ toolMatchesName(t, TODO_WRITE_TOOL_NAME),
3368
+ )
3369
+ ) {
3370
+ return []
3371
+ }
3372
+
3373
+ // When SendUserMessage is in the toolkit, it's the primary communication
3374
+ // channel and the model is always told to use it (#20467). TodoWrite
3375
+ // becomes a side channel — nudging the model about it conflicts with the
3376
+ // brief workflow. The tool itself stays available; this only gates the
3377
+ // "you haven't used it in a while" nag.
3378
+ if (
3379
+ BRIEF_TOOL_NAME &&
3380
+ toolUseContext.options.tools.some(t => toolMatchesName(t, BRIEF_TOOL_NAME))
3381
+ ) {
3382
+ return []
3383
+ }
3384
+
3385
+ // Skip if no messages provided
3386
+ if (!messages || messages.length === 0) {
3387
+ return []
3388
+ }
3389
+
3390
+ const { turnsSinceLastTodoWrite, turnsSinceLastReminder } =
3391
+ getTodoReminderTurnCounts(messages)
3392
+
3393
+ // Check if we should show a reminder
3394
+ if (
3395
+ turnsSinceLastTodoWrite >= TODO_REMINDER_CONFIG.TURNS_SINCE_WRITE &&
3396
+ turnsSinceLastReminder >= TODO_REMINDER_CONFIG.TURNS_BETWEEN_REMINDERS
3397
+ ) {
3398
+ const todoKey = toolUseContext.agentId ?? getSessionId()
3399
+ const appState = toolUseContext.getAppState()
3400
+ const todos = appState.todos[todoKey] ?? []
3401
+ return [
3402
+ {
3403
+ type: 'todo_reminder',
3404
+ content: todos,
3405
+ itemCount: todos.length,
3406
+ },
3407
+ ]
3408
+ }
3409
+
3410
+ return []
3411
+ }
3412
+
3413
+ function getTaskReminderTurnCounts(messages: Message[]): {
3414
+ turnsSinceLastTaskManagement: number
3415
+ turnsSinceLastReminder: number
3416
+ } {
3417
+ let lastTaskManagementIndex = -1
3418
+ let lastReminderIndex = -1
3419
+ let assistantTurnsSinceTaskManagement = 0
3420
+ let assistantTurnsSinceReminder = 0
3421
+
3422
+ // Iterate backwards to find most recent events
3423
+ for (let i = messages.length - 1; i >= 0; i--) {
3424
+ const message = messages[i]
3425
+
3426
+ if (message?.type === 'assistant') {
3427
+ if (isThinkingMessage(message)) {
3428
+ // Skip thinking messages
3429
+ continue
3430
+ }
3431
+
3432
+ // Check for TaskCreate or TaskUpdate usage BEFORE incrementing counter
3433
+ if (
3434
+ lastTaskManagementIndex === -1 &&
3435
+ 'message' in message &&
3436
+ Array.isArray(message.message?.content) &&
3437
+ message.message.content.some(
3438
+ block =>
3439
+ block.type === 'tool_use' &&
3440
+ (block.name === TASK_CREATE_TOOL_NAME ||
3441
+ block.name === TASK_UPDATE_TOOL_NAME),
3442
+ )
3443
+ ) {
3444
+ lastTaskManagementIndex = i
3445
+ }
3446
+
3447
+ // Count assistant turns before finding events
3448
+ if (lastTaskManagementIndex === -1) assistantTurnsSinceTaskManagement++
3449
+ if (lastReminderIndex === -1) assistantTurnsSinceReminder++
3450
+ } else if (
3451
+ lastReminderIndex === -1 &&
3452
+ message?.type === 'attachment' &&
3453
+ message.attachment.type === 'task_reminder'
3454
+ ) {
3455
+ lastReminderIndex = i
3456
+ }
3457
+
3458
+ if (lastTaskManagementIndex !== -1 && lastReminderIndex !== -1) {
3459
+ break
3460
+ }
3461
+ }
3462
+
3463
+ return {
3464
+ turnsSinceLastTaskManagement: assistantTurnsSinceTaskManagement,
3465
+ turnsSinceLastReminder: assistantTurnsSinceReminder,
3466
+ }
3467
+ }
3468
+
3469
+ async function getTaskReminderAttachments(
3470
+ messages: Message[] | undefined,
3471
+ toolUseContext: ToolUseContext,
3472
+ ): Promise<Attachment[]> {
3473
+ if (!isTodoV2Enabled()) {
3474
+ return []
3475
+ }
3476
+
3477
+ // Skip for ant users
3478
+ if (process.env.USER_TYPE === 'ant') {
3479
+ return []
3480
+ }
3481
+
3482
+ // When SendUserMessage is in the toolkit, it's the primary communication
3483
+ // channel and the model is always told to use it (#20467). TaskUpdate
3484
+ // becomes a side channel — nudging the model about it conflicts with the
3485
+ // brief workflow. The tool itself stays available; this only gates the nag.
3486
+ if (
3487
+ BRIEF_TOOL_NAME &&
3488
+ toolUseContext.options.tools.some(t => toolMatchesName(t, BRIEF_TOOL_NAME))
3489
+ ) {
3490
+ return []
3491
+ }
3492
+
3493
+ // Skip if TaskUpdate tool is not available
3494
+ if (
3495
+ !toolUseContext.options.tools.some(t =>
3496
+ toolMatchesName(t, TASK_UPDATE_TOOL_NAME),
3497
+ )
3498
+ ) {
3499
+ return []
3500
+ }
3501
+
3502
+ // Skip if no messages provided
3503
+ if (!messages || messages.length === 0) {
3504
+ return []
3505
+ }
3506
+
3507
+ const { turnsSinceLastTaskManagement, turnsSinceLastReminder } =
3508
+ getTaskReminderTurnCounts(messages)
3509
+
3510
+ // Check if we should show a reminder
3511
+ if (
3512
+ turnsSinceLastTaskManagement >= TODO_REMINDER_CONFIG.TURNS_SINCE_WRITE &&
3513
+ turnsSinceLastReminder >= TODO_REMINDER_CONFIG.TURNS_BETWEEN_REMINDERS
3514
+ ) {
3515
+ const tasks = await listTasks(getTaskListId())
3516
+ return [
3517
+ {
3518
+ type: 'task_reminder',
3519
+ content: tasks,
3520
+ itemCount: tasks.length,
3521
+ },
3522
+ ]
3523
+ }
3524
+
3525
+ return []
3526
+ }
3527
+
3528
+ /**
3529
+ * Get attachments for all unified tasks using the Task framework.
3530
+ * Replaces the old getBackgroundShellAttachments, getBackgroundRemoteSessionAttachments,
3531
+ * and getAsyncAgentAttachments functions.
3532
+ */
3533
+ async function getUnifiedTaskAttachments(
3534
+ toolUseContext: ToolUseContext,
3535
+ ): Promise<Attachment[]> {
3536
+ const appState = toolUseContext.getAppState()
3537
+ const { attachments, updatedTaskOffsets, evictedTaskIds } =
3538
+ await generateTaskAttachments(appState)
3539
+
3540
+ applyTaskOffsetsAndEvictions(
3541
+ toolUseContext.setAppState,
3542
+ updatedTaskOffsets,
3543
+ evictedTaskIds,
3544
+ )
3545
+
3546
+ // Convert TaskAttachment to Attachment format
3547
+ return attachments.map(taskAttachment => ({
3548
+ type: 'task_status' as const,
3549
+ taskId: taskAttachment.taskId,
3550
+ taskType: taskAttachment.taskType,
3551
+ status: taskAttachment.status,
3552
+ description: taskAttachment.description,
3553
+ deltaSummary: taskAttachment.deltaSummary,
3554
+ outputFilePath: getTaskOutputPath(taskAttachment.taskId),
3555
+ }))
3556
+ }
3557
+
3558
+ async function getAsyncHookResponseAttachments(): Promise<Attachment[]> {
3559
+ const responses = await checkForAsyncHookResponses()
3560
+
3561
+ if (responses.length === 0) {
3562
+ return []
3563
+ }
3564
+
3565
+ logForDebugging(
3566
+ `Hooks: getAsyncHookResponseAttachments found ${responses.length} responses`,
3567
+ )
3568
+
3569
+ const attachments = responses.map(
3570
+ ({
3571
+ processId,
3572
+ response,
3573
+ hookName,
3574
+ hookEvent,
3575
+ toolName,
3576
+ pluginId,
3577
+ stdout,
3578
+ stderr,
3579
+ exitCode,
3580
+ }) => {
3581
+ logForDebugging(
3582
+ `Hooks: Creating attachment for ${processId} (${hookName}): ${jsonStringify(response)}`,
3583
+ )
3584
+ return {
3585
+ type: 'async_hook_response' as const,
3586
+ processId,
3587
+ hookName,
3588
+ hookEvent,
3589
+ toolName,
3590
+ response,
3591
+ stdout,
3592
+ stderr,
3593
+ exitCode,
3594
+ }
3595
+ },
3596
+ )
3597
+
3598
+ // Remove delivered hooks from registry to prevent re-processing
3599
+ if (responses.length > 0) {
3600
+ const processIds = responses.map(r => r.processId)
3601
+ removeDeliveredAsyncHooks(processIds)
3602
+ logForDebugging(
3603
+ `Hooks: Removed ${processIds.length} delivered hooks from registry`,
3604
+ )
3605
+ }
3606
+
3607
+ logForDebugging(
3608
+ `Hooks: getAsyncHookResponseAttachments found ${attachments.length} attachments`,
3609
+ )
3610
+
3611
+ return attachments
3612
+ }
3613
+
3614
+ /**
3615
+ * Get teammate mailbox attachments for agent swarm communication
3616
+ * Teammates are independent Claude Code sessions running in parallel (swarms),
3617
+ * not parent-child subagent relationships.
3618
+ *
3619
+ * This function checks two sources for messages:
3620
+ * 1. File-based mailbox (for messages that arrived between polls)
3621
+ * 2. AppState.inbox (for messages queued mid-turn by useInboxPoller)
3622
+ *
3623
+ * Messages from AppState.inbox are delivered mid-turn as attachments,
3624
+ * allowing teammates to receive messages without waiting for the turn to end.
3625
+ */
3626
+ async function getTeammateMailboxAttachments(
3627
+ toolUseContext: ToolUseContext,
3628
+ ): Promise<Attachment[]> {
3629
+ if (!isAgentSwarmsEnabled()) {
3630
+ return []
3631
+ }
3632
+ if (process.env.USER_TYPE !== 'ant') {
3633
+ return []
3634
+ }
3635
+
3636
+ // Get AppState early to check for team lead status
3637
+ const appState = toolUseContext.getAppState()
3638
+
3639
+ // Use agent name from helper (checks AsyncLocalStorage, then dynamicTeamContext)
3640
+ const envAgentName = getAgentName()
3641
+
3642
+ // Get team name (checks AsyncLocalStorage, dynamicTeamContext, then AppState)
3643
+ const teamName = getTeamName(appState.teamContext)
3644
+
3645
+ // Check if we're the team lead (uses shared logic from swarm utils)
3646
+ const teamLeadStatus = isTeamLead(appState.teamContext)
3647
+
3648
+ // Check if viewing a teammate's transcript (for in-process teammates)
3649
+ const viewedTeammate = getViewedTeammateTask(appState)
3650
+
3651
+ // Resolve agent name based on who we're VIEWING:
3652
+ // - If viewing a teammate, use THEIR name (to read from their mailbox)
3653
+ // - Otherwise use env var if set, or leader's name if we're the team lead
3654
+ let agentName = viewedTeammate?.identity.agentName ?? envAgentName
3655
+ if (!agentName && teamLeadStatus && appState.teamContext) {
3656
+ const leadAgentId = appState.teamContext.leadAgentId
3657
+ // Look up the lead's name from agents map (not the UUID)
3658
+ agentName = appState.teamContext.teammates[leadAgentId]?.name || 'team-lead'
3659
+ }
3660
+
3661
+ logForDebugging(
3662
+ `[SwarmMailbox] getTeammateMailboxAttachments called: envAgentName=${envAgentName}, isTeamLead=${teamLeadStatus}, resolved agentName=${agentName}, teamName=${teamName}`,
3663
+ )
3664
+
3665
+ // Only check inbox if running as an agent in a swarm or team lead
3666
+ if (!agentName) {
3667
+ logForDebugging(
3668
+ `[SwarmMailbox] Not checking inbox - not in a swarm or team lead`,
3669
+ )
3670
+ return []
3671
+ }
3672
+
3673
+ logForDebugging(
3674
+ `[SwarmMailbox] Checking inbox for agent="${agentName}" team="${teamName || 'default'}"`,
3675
+ )
3676
+
3677
+ // Check mailbox for unread messages (routes to in-process or file-based)
3678
+ // Filter out structured protocol messages (permission requests/responses, shutdown
3679
+ // messages, etc.) — these must be left unread for useInboxPoller to route to their
3680
+ // proper handlers (workerPermissions queue, sandbox queue, etc.). Without filtering,
3681
+ // attachment generation races with InboxPoller: whichever reads first marks all
3682
+ // messages as read, and if attachments wins, protocol messages get bundled as raw
3683
+ // LLM context text instead of being routed to their UI handlers.
3684
+ const allUnreadMessages = await readUnreadMessages(agentName, teamName)
3685
+ const unreadMessages = allUnreadMessages.filter(
3686
+ m => !isStructuredProtocolMessage(m.text),
3687
+ )
3688
+ logForDebugging(
3689
+ `[MailboxBridge] Found ${allUnreadMessages.length} unread message(s) for "${agentName}" (${allUnreadMessages.length - unreadMessages.length} structured protocol messages filtered out)`,
3690
+ )
3691
+
3692
+ // Also check AppState.inbox for pending messages (queued mid-turn by useInboxPoller)
3693
+ // IMPORTANT: appState.inbox contains messages FROM teammates TO the leader.
3694
+ // Only show these when viewing the leader's transcript (not a teammate's).
3695
+ // When viewing a teammate, their messages come from the file-based mailbox above.
3696
+ // In-process teammates share AppState with the leader — appState.inbox contains
3697
+ // the LEADER's queued messages, not the teammate's. Skip it to prevent leakage
3698
+ // (including self-echo from broadcasts). Teammates receive messages exclusively
3699
+ // through their file-based mailbox + waitForNextPromptOrShutdown.
3700
+ // Note: viewedTeammate was already computed above for agentName resolution
3701
+ const pendingInboxMessages =
3702
+ viewedTeammate || isInProcessTeammate()
3703
+ ? [] // Viewing teammate or running as in-process teammate - don't show leader's inbox
3704
+ : appState.inbox.messages.filter(m => m.status === 'pending')
3705
+ logForDebugging(
3706
+ `[SwarmMailbox] Found ${pendingInboxMessages.length} pending message(s) in AppState.inbox`,
3707
+ )
3708
+
3709
+ // Combine both sources of messages WITH DEDUPLICATION
3710
+ // The same message could exist in both file mailbox and AppState.inbox due to race conditions:
3711
+ // 1. getTeammateMailboxAttachments reads file -> finds message M
3712
+ // 2. InboxPoller reads same file -> queues M in AppState.inbox
3713
+ // 3. getTeammateMailboxAttachments reads AppState -> finds M again
3714
+ // We deduplicate using from+timestamp+text prefix as the key
3715
+ const seen = new Set<string>()
3716
+ let allMessages: Array<{
3717
+ from: string
3718
+ text: string
3719
+ timestamp: string
3720
+ color?: string
3721
+ summary?: string
3722
+ }> = []
3723
+
3724
+ for (const m of [...unreadMessages, ...pendingInboxMessages]) {
3725
+ const key = `${m.from}|${m.timestamp}|${m.text.slice(0, 100)}`
3726
+ if (!seen.has(key)) {
3727
+ seen.add(key)
3728
+ allMessages.push({
3729
+ from: m.from,
3730
+ text: m.text,
3731
+ timestamp: m.timestamp,
3732
+ color: m.color,
3733
+ summary: m.summary,
3734
+ })
3735
+ }
3736
+ }
3737
+
3738
+ // Collapse multiple idle notifications per agent — keep only the latest.
3739
+ // Single pass to parse, then filter without re-parsing.
3740
+ const idleAgentByIndex = new Map<number, string>()
3741
+ const latestIdleByAgent = new Map<string, number>()
3742
+ for (let i = 0; i < allMessages.length; i++) {
3743
+ const idle = isIdleNotification(allMessages[i]!.text)
3744
+ if (idle) {
3745
+ idleAgentByIndex.set(i, idle.from)
3746
+ latestIdleByAgent.set(idle.from, i)
3747
+ }
3748
+ }
3749
+ if (idleAgentByIndex.size > latestIdleByAgent.size) {
3750
+ const beforeCount = allMessages.length
3751
+ allMessages = allMessages.filter((_m, i) => {
3752
+ const agent = idleAgentByIndex.get(i)
3753
+ if (agent === undefined) return true
3754
+ return latestIdleByAgent.get(agent) === i
3755
+ })
3756
+ logForDebugging(
3757
+ `[SwarmMailbox] Collapsed ${beforeCount - allMessages.length} duplicate idle notification(s)`,
3758
+ )
3759
+ }
3760
+
3761
+ if (allMessages.length === 0) {
3762
+ logForDebugging(`[SwarmMailbox] No messages to deliver, returning empty`)
3763
+ return []
3764
+ }
3765
+
3766
+ logForDebugging(
3767
+ `[SwarmMailbox] Returning ${allMessages.length} message(s) as attachment for "${agentName}" (${unreadMessages.length} from file, ${pendingInboxMessages.length} from AppState, after dedup)`,
3768
+ )
3769
+
3770
+ // Build the attachment BEFORE marking messages as processed
3771
+ // This prevents message loss if any operation below fails
3772
+ const attachment: Attachment[] = [
3773
+ {
3774
+ type: 'teammate_mailbox',
3775
+ messages: allMessages,
3776
+ },
3777
+ ]
3778
+
3779
+ // Mark only non-structured mailbox messages as read after attachment is built.
3780
+ // Structured protocol messages stay unread for useInboxPoller to handle.
3781
+ if (unreadMessages.length > 0) {
3782
+ await markMessagesAsReadByPredicate(
3783
+ agentName,
3784
+ m => !isStructuredProtocolMessage(m.text),
3785
+ teamName,
3786
+ )
3787
+ logForDebugging(
3788
+ `[MailboxBridge] marked ${unreadMessages.length} non-structured message(s) as read for agent="${agentName}" team="${teamName || 'default'}"`,
3789
+ )
3790
+ }
3791
+
3792
+ // Process shutdown_approved messages - remove teammates from team file
3793
+ // This mirrors what useInboxPoller does in interactive mode (lines 546-606)
3794
+ // In -p mode, useInboxPoller doesn't run, so we must handle this here
3795
+ if (teamLeadStatus && teamName) {
3796
+ for (const m of allMessages) {
3797
+ const shutdownApproval = isShutdownApproved(m.text)
3798
+ if (shutdownApproval) {
3799
+ const teammateToRemove = shutdownApproval.from
3800
+ logForDebugging(
3801
+ `[SwarmMailbox] Processing shutdown_approved from ${teammateToRemove}`,
3802
+ )
3803
+
3804
+ // Find the teammate ID by name
3805
+ const teammateId = appState.teamContext?.teammates
3806
+ ? Object.entries(appState.teamContext.teammates).find(
3807
+ ([, t]) => t.name === teammateToRemove,
3808
+ )?.[0]
3809
+ : undefined
3810
+
3811
+ if (teammateId) {
3812
+ // Remove from team file
3813
+ removeTeammateFromTeamFile(teamName, {
3814
+ agentId: teammateId,
3815
+ name: teammateToRemove,
3816
+ })
3817
+ logForDebugging(
3818
+ `[SwarmMailbox] Removed ${teammateToRemove} from team file`,
3819
+ )
3820
+
3821
+ // Unassign tasks owned by this teammate
3822
+ await unassignTeammateTasks(
3823
+ teamName,
3824
+ teammateId,
3825
+ teammateToRemove,
3826
+ 'shutdown',
3827
+ )
3828
+
3829
+ // Remove from teamContext in AppState
3830
+ toolUseContext.setAppState(prev => {
3831
+ if (!prev.teamContext?.teammates) return prev
3832
+ if (!(teammateId in prev.teamContext.teammates)) return prev
3833
+ const { [teammateId]: _, ...remainingTeammates } =
3834
+ prev.teamContext.teammates
3835
+ return {
3836
+ ...prev,
3837
+ teamContext: {
3838
+ ...prev.teamContext,
3839
+ teammates: remainingTeammates,
3840
+ },
3841
+ }
3842
+ })
3843
+ }
3844
+ }
3845
+ }
3846
+ }
3847
+
3848
+ // Mark AppState inbox messages as processed LAST, after attachment is built
3849
+ // This ensures messages aren't lost if earlier operations fail
3850
+ if (pendingInboxMessages.length > 0) {
3851
+ const pendingIds = new Set(pendingInboxMessages.map(m => m.id))
3852
+ toolUseContext.setAppState(prev => ({
3853
+ ...prev,
3854
+ inbox: {
3855
+ messages: prev.inbox.messages.map(m =>
3856
+ pendingIds.has(m.id) ? { ...m, status: 'processed' as const } : m,
3857
+ ),
3858
+ },
3859
+ }))
3860
+ }
3861
+
3862
+ return attachment
3863
+ }
3864
+
3865
+ /**
3866
+ * Get team context attachment for teammates in a swarm.
3867
+ * Only injected on the first turn to provide team coordination instructions.
3868
+ */
3869
+ function getTeamContextAttachment(messages: Message[]): Attachment[] {
3870
+ const teamName = getTeamName()
3871
+ const agentId = getAgentId()
3872
+ const agentName = getAgentName()
3873
+
3874
+ // Only inject for teammates (not team lead or non-team sessions)
3875
+ if (!teamName || !agentId) {
3876
+ return []
3877
+ }
3878
+
3879
+ // Only inject on first turn - check if there are no assistant messages yet
3880
+ const hasAssistantMessage = messages.some(m => m.type === 'assistant')
3881
+ if (hasAssistantMessage) {
3882
+ return []
3883
+ }
3884
+
3885
+ const configDir = getClaudeConfigHomeDir()
3886
+ const teamConfigPath = `${configDir}/teams/${teamName}/config.json`
3887
+ const taskListPath = `${configDir}/tasks/${teamName}/`
3888
+
3889
+ return [
3890
+ {
3891
+ type: 'team_context',
3892
+ agentId,
3893
+ agentName: agentName || agentId,
3894
+ teamName,
3895
+ teamConfigPath,
3896
+ taskListPath,
3897
+ },
3898
+ ]
3899
+ }
3900
+
3901
+ function getTokenUsageAttachment(
3902
+ messages: Message[],
3903
+ model: string,
3904
+ ): Attachment[] {
3905
+ if (!isEnvTruthy(process.env.CLAUDE_CODE_ENABLE_TOKEN_USAGE_ATTACHMENT)) {
3906
+ return []
3907
+ }
3908
+
3909
+ const contextWindow = getEffectiveContextWindowSize(model)
3910
+ const usedTokens = tokenCountFromLastAPIResponse(messages)
3911
+
3912
+ return [
3913
+ {
3914
+ type: 'token_usage',
3915
+ used: usedTokens,
3916
+ total: contextWindow,
3917
+ remaining: contextWindow - usedTokens,
3918
+ },
3919
+ ]
3920
+ }
3921
+
3922
+ function getOutputTokenUsageAttachment(): Attachment[] {
3923
+ if (feature('TOKEN_BUDGET')) {
3924
+ const budget = getCurrentTurnTokenBudget()
3925
+ if (budget === null || budget <= 0) {
3926
+ return []
3927
+ }
3928
+ return [
3929
+ {
3930
+ type: 'output_token_usage',
3931
+ turn: getTurnOutputTokens(),
3932
+ session: getTotalOutputTokens(),
3933
+ budget,
3934
+ },
3935
+ ]
3936
+ }
3937
+ return []
3938
+ }
3939
+
3940
+ function getMaxBudgetUsdAttachment(maxBudgetUsd?: number): Attachment[] {
3941
+ if (maxBudgetUsd === undefined) {
3942
+ return []
3943
+ }
3944
+
3945
+ const usedCost = getTotalCostUSD()
3946
+ const remainingBudget = maxBudgetUsd - usedCost
3947
+
3948
+ return [
3949
+ {
3950
+ type: 'budget_usd',
3951
+ used: usedCost,
3952
+ total: maxBudgetUsd,
3953
+ remaining: remainingBudget,
3954
+ },
3955
+ ]
3956
+ }
3957
+
3958
+ /**
3959
+ * Count human turns since plan mode exit (plan_mode_exit attachment).
3960
+ * Returns 0 if no plan_mode_exit attachment found.
3961
+ *
3962
+ * tool_result messages are type:'user' without isMeta, so filter by
3963
+ * toolUseResult to avoid counting them — otherwise the 10-turn reminder
3964
+ * interval fires every ~10 tool calls instead of ~10 human turns.
3965
+ */
3966
+ export function getVerifyPlanReminderTurnCount(messages: Message[]): number {
3967
+ let turnCount = 0
3968
+ for (let i = messages.length - 1; i >= 0; i--) {
3969
+ const message = messages[i]
3970
+ if (message && isHumanTurn(message)) {
3971
+ turnCount++
3972
+ }
3973
+ // Stop counting at plan_mode_exit attachment (marks when implementation started)
3974
+ if (
3975
+ message?.type === 'attachment' &&
3976
+ message.attachment.type === 'plan_mode_exit'
3977
+ ) {
3978
+ return turnCount
3979
+ }
3980
+ }
3981
+ // No plan_mode_exit found
3982
+ return 0
3983
+ }
3984
+
3985
+ /**
3986
+ * Get verify plan reminder attachment if the model hasn't called VerifyPlanExecution yet.
3987
+ */
3988
+ async function getVerifyPlanReminderAttachment(
3989
+ messages: Message[] | undefined,
3990
+ toolUseContext: ToolUseContext,
3991
+ ): Promise<Attachment[]> {
3992
+ if (
3993
+ process.env.USER_TYPE !== 'ant' ||
3994
+ !isEnvTruthy(process.env.CLAUDE_CODE_VERIFY_PLAN)
3995
+ ) {
3996
+ return []
3997
+ }
3998
+
3999
+ const appState = toolUseContext.getAppState()
4000
+ const pending = appState.pendingPlanVerification
4001
+
4002
+ // Only remind if plan exists and verification not started or completed
4003
+ if (
4004
+ !pending ||
4005
+ pending.verificationStarted ||
4006
+ pending.verificationCompleted
4007
+ ) {
4008
+ return []
4009
+ }
4010
+
4011
+ // Only remind every N turns
4012
+ if (messages && messages.length > 0) {
4013
+ const turnCount = getVerifyPlanReminderTurnCount(messages)
4014
+ if (
4015
+ turnCount === 0 ||
4016
+ turnCount % VERIFY_PLAN_REMINDER_CONFIG.TURNS_BETWEEN_REMINDERS !== 0
4017
+ ) {
4018
+ return []
4019
+ }
4020
+ }
4021
+
4022
+ return [{ type: 'verify_plan_reminder' }]
4023
+ }
4024
+
4025
+ export function getCompactionReminderAttachment(
4026
+ messages: Message[],
4027
+ model: string,
4028
+ ): Attachment[] {
4029
+ if (!getFeatureValue_CACHED_MAY_BE_STALE('tengu_marble_fox', false)) {
4030
+ return []
4031
+ }
4032
+
4033
+ if (!isAutoCompactEnabled()) {
4034
+ return []
4035
+ }
4036
+
4037
+ const contextWindow = getContextWindowForModel(model, getSdkBetas())
4038
+ if (contextWindow < 1_000_000) {
4039
+ return []
4040
+ }
4041
+
4042
+ const effectiveWindow = getEffectiveContextWindowSize(model)
4043
+ const usedTokens = tokenCountWithEstimation(messages)
4044
+ if (usedTokens < effectiveWindow * 0.25) {
4045
+ return []
4046
+ }
4047
+
4048
+ return [{ type: 'compaction_reminder' }]
4049
+ }
4050
+
4051
+ /**
4052
+ * Context-efficiency nudge. Injected after every N tokens of growth without
4053
+ * a snip. Pacing is handled entirely by shouldNudgeForSnips — the 10k
4054
+ * interval resets on prior nudges, snip markers, snip boundaries, and
4055
+ * compact boundaries.
4056
+ */
4057
+ export function getContextEfficiencyAttachment(
4058
+ messages: Message[],
4059
+ ): Attachment[] {
4060
+ if (!feature('HISTORY_SNIP')) {
4061
+ return []
4062
+ }
4063
+ // Gate must match SnipTool.isEnabled() — don't nudge toward a tool that
4064
+ // isn't in the tool list. Lazy require keeps this file snip-string-free.
4065
+ const { isSnipRuntimeEnabled, shouldNudgeForSnips } =
4066
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
4067
+ require('../services/compact/snipCompact.js') as typeof import('../services/compact/snipCompact.js')
4068
+ if (!isSnipRuntimeEnabled()) {
4069
+ return []
4070
+ }
4071
+
4072
+ if (!shouldNudgeForSnips(messages)) {
4073
+ return []
4074
+ }
4075
+
4076
+ return [{ type: 'context_efficiency' }]
4077
+ }
4078
+
4079
+
4080
+ function isFileReadDenied(
4081
+ filePath: string,
4082
+ toolPermissionContext: ToolPermissionContext,
4083
+ ): boolean {
4084
+ const denyRule = matchingRuleForInput(
4085
+ filePath,
4086
+ toolPermissionContext,
4087
+ 'read',
4088
+ 'deny',
4089
+ )
4090
+ return denyRule !== null
4091
+ }