vellum 0.2.1 → 0.2.7

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 (361) hide show
  1. package/README.md +15 -2
  2. package/bun.lock +71 -100
  3. package/package.json +5 -3
  4. package/scripts/capture-x-graphql.ts +562 -0
  5. package/scripts/ipc/check-swift-decoder-drift.ts +2 -1
  6. package/scripts/test.sh +5 -0
  7. package/src/__tests__/__snapshots__/ipc-snapshot.test.ts.snap +133 -34
  8. package/src/__tests__/account-registry.test.ts +2 -1
  9. package/src/__tests__/agent-heartbeat-service.test.ts +250 -0
  10. package/src/__tests__/asset-materialize-tool.test.ts +16 -15
  11. package/src/__tests__/asset-search-tool.test.ts +23 -22
  12. package/src/__tests__/attachments-store.test.ts +56 -127
  13. package/src/__tests__/browser-skill-baseline-tool-payload.test.ts +5 -4
  14. package/src/__tests__/browser-skill-endstate.test.ts +4 -3
  15. package/src/__tests__/call-bridge.test.ts +385 -0
  16. package/src/__tests__/call-constants.test.ts +40 -0
  17. package/src/__tests__/call-orchestrator.test.ts +130 -4
  18. package/src/__tests__/call-recovery.test.ts +518 -0
  19. package/src/__tests__/call-routes-http.test.ts +459 -0
  20. package/src/__tests__/call-state-machine.test.ts +143 -0
  21. package/src/__tests__/call-store.test.ts +216 -1
  22. package/src/__tests__/cli-discover.test.ts +1 -1
  23. package/src/__tests__/commit-message-enrichment-service.test.ts +148 -7
  24. package/src/__tests__/compaction.benchmark.test.ts +176 -0
  25. package/src/__tests__/computer-use-tools.test.ts +250 -0
  26. package/src/__tests__/config-schema.test.ts +305 -3
  27. package/src/__tests__/conflict-store.test.ts +2 -1
  28. package/src/__tests__/contacts-tools.test.ts +331 -0
  29. package/src/__tests__/conversation-store.test.ts +30 -32
  30. package/src/__tests__/credential-security-invariants.test.ts +4 -0
  31. package/src/__tests__/date-context.test.ts +373 -0
  32. package/src/__tests__/db-schedule-syntax-migration.test.ts +129 -0
  33. package/src/__tests__/fixtures/media-reuse-fixtures.ts +3 -3
  34. package/src/__tests__/followup-tools.test.ts +303 -0
  35. package/src/__tests__/handlers-twilio-config.test.ts +221 -0
  36. package/src/__tests__/handlers-twitter-config.test.ts +718 -0
  37. package/src/__tests__/intent-routing.test.ts +64 -57
  38. package/src/__tests__/ipc-roundtrip.benchmark.test.ts +237 -0
  39. package/src/__tests__/ipc-snapshot.test.ts +71 -28
  40. package/src/__tests__/llm-usage-store.test.ts +3 -8
  41. package/src/__tests__/media-generate-image.test.ts +1 -1
  42. package/src/__tests__/media-reuse-story.e2e.test.ts +7 -7
  43. package/src/__tests__/memory-regressions.test.ts +100 -2
  44. package/src/__tests__/memory-retrieval.benchmark.test.ts +430 -0
  45. package/src/__tests__/parallel-tool.benchmark.test.ts +294 -0
  46. package/src/__tests__/playbook-tools.test.ts +342 -0
  47. package/src/__tests__/profile-compiler.test.ts +2 -1
  48. package/src/__tests__/provider-commit-message-generator.test.ts +303 -0
  49. package/src/__tests__/provider-streaming.benchmark.test.ts +773 -0
  50. package/src/__tests__/recurrence-engine-rruleset.test.ts +78 -0
  51. package/src/__tests__/recurrence-engine.test.ts +69 -0
  52. package/src/__tests__/recurrence-types.test.ts +71 -0
  53. package/src/__tests__/registry.test.ts +5 -3
  54. package/src/__tests__/relay-server.test.ts +633 -0
  55. package/src/__tests__/reminder-store.test.ts +6 -3
  56. package/src/__tests__/reminder.test.ts +43 -77
  57. package/src/__tests__/run-orchestrator-assistant-events.test.ts +8 -4
  58. package/src/__tests__/run-orchestrator.test.ts +4 -4
  59. package/src/__tests__/runtime-attachment-metadata.test.ts +7 -6
  60. package/src/__tests__/runtime-runs-http.test.ts +4 -4
  61. package/src/__tests__/runtime-runs.test.ts +4 -4
  62. package/src/__tests__/schedule-store.test.ts +482 -0
  63. package/src/__tests__/schedule-tools.test.ts +700 -0
  64. package/src/__tests__/scheduler-recurrence.test.ts +329 -0
  65. package/src/__tests__/server-history-render.test.ts +14 -13
  66. package/src/__tests__/session-conflict-gate.test.ts +28 -25
  67. package/src/__tests__/session-error.test.ts +28 -0
  68. package/src/__tests__/session-init.benchmark.test.ts +462 -0
  69. package/src/__tests__/session-queue.test.ts +71 -48
  70. package/src/__tests__/session-runtime-assembly.test.ts +161 -0
  71. package/src/__tests__/session-surfaces-task-progress.test.ts +104 -0
  72. package/src/__tests__/signup-e2e.test.ts +2 -1
  73. package/src/__tests__/skill-projection.benchmark.test.ts +328 -0
  74. package/src/__tests__/skill-script-runner.test.ts +159 -0
  75. package/src/__tests__/speaker-identification.test.ts +52 -0
  76. package/src/__tests__/subagent-manager-notify.test.ts +42 -10
  77. package/src/__tests__/subagent-tools.test.ts +141 -41
  78. package/src/__tests__/task-compiler.test.ts +2 -1
  79. package/src/__tests__/task-runner.test.ts +2 -1
  80. package/src/__tests__/task-scheduler.test.ts +2 -1
  81. package/src/__tests__/task-tools.test.ts +49 -56
  82. package/src/__tests__/tool-audit-listener.test.ts +1 -0
  83. package/src/__tests__/tool-domain-event-publisher.test.ts +2 -0
  84. package/src/__tests__/tool-execution-pipeline.benchmark.test.ts +500 -0
  85. package/src/__tests__/tool-executor.test.ts +13 -17
  86. package/src/__tests__/turn-commit.test.ts +218 -3
  87. package/src/__tests__/twilio-provider.test.ts +143 -0
  88. package/src/__tests__/twilio-routes.test.ts +789 -0
  89. package/src/__tests__/twitter-auth-handler.test.ts +581 -0
  90. package/src/__tests__/view-image-tool.test.ts +217 -0
  91. package/src/__tests__/workspace-git-service.test.ts +186 -0
  92. package/src/__tests__/workspace-heartbeat-service.test.ts +13 -3
  93. package/src/agent-heartbeat/agent-heartbeat-service.ts +155 -0
  94. package/src/bundler/app-bundler.ts +12 -8
  95. package/src/calls/__tests__/twilio-webhook-urls.test.ts +162 -0
  96. package/src/calls/call-bridge.ts +95 -0
  97. package/src/calls/call-constants.ts +43 -5
  98. package/src/calls/call-domain.ts +276 -0
  99. package/src/calls/call-orchestrator.ts +43 -17
  100. package/src/calls/call-recovery.ts +207 -0
  101. package/src/calls/call-state-machine.ts +68 -0
  102. package/src/calls/call-store.ts +192 -5
  103. package/src/calls/relay-server.ts +41 -4
  104. package/src/calls/speaker-identification.ts +213 -0
  105. package/src/calls/twilio-config.ts +8 -8
  106. package/src/calls/twilio-provider.ts +13 -9
  107. package/src/calls/twilio-routes.ts +90 -76
  108. package/src/calls/twilio-webhook-urls.ts +50 -0
  109. package/src/calls/types.ts +1 -1
  110. package/src/cli/config-commands.ts +334 -0
  111. package/src/cli/core-commands.ts +776 -0
  112. package/src/cli/doordash.ts +251 -1
  113. package/src/cli/ipc-client.ts +82 -0
  114. package/src/cli/map.ts +270 -0
  115. package/src/cli/twitter.ts +575 -0
  116. package/src/cli.ts +7 -5
  117. package/src/commands/__tests__/cc-command-registry.test.ts +319 -0
  118. package/src/commands/cc-command-registry.ts +209 -0
  119. package/src/config/bundled-skills/contacts/SKILL.md +39 -0
  120. package/src/config/bundled-skills/contacts/TOOLS.json +122 -0
  121. package/src/config/bundled-skills/contacts/tools/contact-merge.ts +9 -0
  122. package/src/config/bundled-skills/contacts/tools/contact-search.ts +9 -0
  123. package/src/config/bundled-skills/contacts/tools/contact-upsert.ts +9 -0
  124. package/src/config/bundled-skills/document/SKILL.md +18 -0
  125. package/src/config/bundled-skills/document/TOOLS.json +53 -0
  126. package/src/config/bundled-skills/document/tools/document-create.ts +9 -0
  127. package/src/config/bundled-skills/document/tools/document-update.ts +9 -0
  128. package/src/config/bundled-skills/doordash/SKILL.md +82 -23
  129. package/src/config/bundled-skills/followups/SKILL.md +32 -0
  130. package/src/config/bundled-skills/followups/TOOLS.json +100 -0
  131. package/src/config/bundled-skills/followups/tools/followup-create.ts +9 -0
  132. package/src/config/bundled-skills/followups/tools/followup-list.ts +9 -0
  133. package/src/config/bundled-skills/followups/tools/followup-resolve.ts +9 -0
  134. package/src/config/bundled-skills/image-studio/tools/media-generate-image.ts +1 -23
  135. package/src/config/bundled-skills/messaging/tools/messaging-analyze-style.ts +2 -1
  136. package/src/config/bundled-skills/playbooks/SKILL.md +31 -0
  137. package/src/config/bundled-skills/playbooks/TOOLS.json +126 -0
  138. package/src/config/bundled-skills/playbooks/tools/playbook-create.ts +9 -0
  139. package/src/config/bundled-skills/playbooks/tools/playbook-delete.ts +9 -0
  140. package/src/config/bundled-skills/playbooks/tools/playbook-list.ts +9 -0
  141. package/src/config/bundled-skills/playbooks/tools/playbook-update.ts +9 -0
  142. package/src/config/bundled-skills/reminder/SKILL.md +20 -0
  143. package/src/config/bundled-skills/reminder/TOOLS.json +67 -0
  144. package/src/config/bundled-skills/reminder/tools/reminder-cancel.ts +9 -0
  145. package/src/config/bundled-skills/reminder/tools/reminder-create.ts +9 -0
  146. package/src/config/bundled-skills/reminder/tools/reminder-list.ts +9 -0
  147. package/src/config/bundled-skills/schedule/SKILL.md +74 -0
  148. package/src/config/bundled-skills/schedule/TOOLS.json +135 -0
  149. package/src/config/bundled-skills/schedule/tools/schedule-create.ts +9 -0
  150. package/src/config/bundled-skills/schedule/tools/schedule-delete.ts +9 -0
  151. package/src/config/bundled-skills/schedule/tools/schedule-list.ts +9 -0
  152. package/src/config/bundled-skills/schedule/tools/schedule-update.ts +9 -0
  153. package/src/config/bundled-skills/subagent/SKILL.md +25 -0
  154. package/src/config/bundled-skills/subagent/TOOLS.json +107 -0
  155. package/src/config/bundled-skills/subagent/tools/subagent-abort.ts +9 -0
  156. package/src/config/bundled-skills/subagent/tools/subagent-message.ts +9 -0
  157. package/src/config/bundled-skills/subagent/tools/subagent-read.ts +9 -0
  158. package/src/config/bundled-skills/subagent/tools/subagent-spawn.ts +9 -0
  159. package/src/config/bundled-skills/subagent/tools/subagent-status.ts +9 -0
  160. package/src/config/bundled-skills/tasks/SKILL.md +28 -0
  161. package/src/config/bundled-skills/tasks/TOOLS.json +256 -0
  162. package/src/config/bundled-skills/tasks/tools/task-delete.ts +9 -0
  163. package/src/config/bundled-skills/tasks/tools/task-list-add.ts +9 -0
  164. package/src/config/bundled-skills/tasks/tools/task-list-remove.ts +9 -0
  165. package/src/config/bundled-skills/tasks/tools/task-list-show.ts +9 -0
  166. package/src/config/bundled-skills/tasks/tools/task-list-update.ts +9 -0
  167. package/src/config/bundled-skills/tasks/tools/task-list.ts +9 -0
  168. package/src/config/bundled-skills/tasks/tools/task-run.ts +9 -0
  169. package/src/config/bundled-skills/tasks/tools/task-save.ts +9 -0
  170. package/src/config/bundled-skills/twitter/SKILL.md +134 -0
  171. package/src/config/bundled-skills/watcher/SKILL.md +27 -0
  172. package/src/config/bundled-skills/watcher/TOOLS.json +147 -0
  173. package/src/config/bundled-skills/watcher/tools/watcher-create.ts +9 -0
  174. package/src/config/bundled-skills/watcher/tools/watcher-delete.ts +9 -0
  175. package/src/config/bundled-skills/watcher/tools/watcher-digest.ts +9 -0
  176. package/src/config/bundled-skills/watcher/tools/watcher-list.ts +9 -0
  177. package/src/config/bundled-skills/watcher/tools/watcher-update.ts +9 -0
  178. package/src/config/defaults.ts +34 -0
  179. package/src/config/loader.ts +4 -1
  180. package/src/config/schema.ts +165 -1
  181. package/src/config/system-prompt.ts +61 -16
  182. package/src/config/templates/IDENTITY.md +7 -0
  183. package/src/config/types.ts +4 -0
  184. package/src/config/vellum-skills/telegram-setup/SKILL.md +1 -5
  185. package/src/contacts/contact-store.ts +4 -4
  186. package/src/daemon/assistant-attachments.ts +10 -0
  187. package/src/daemon/classifier.ts +3 -1
  188. package/src/daemon/computer-use-session.ts +3 -1
  189. package/src/daemon/date-context.ts +136 -0
  190. package/src/daemon/handlers/apps.ts +16 -1
  191. package/src/daemon/handlers/browser.ts +54 -0
  192. package/src/daemon/handlers/computer-use.ts +7 -1
  193. package/src/daemon/handlers/config.ts +205 -5
  194. package/src/daemon/handlers/diagnostics.ts +5 -1
  195. package/src/daemon/handlers/documents.ts +18 -29
  196. package/src/daemon/handlers/home-base.ts +5 -1
  197. package/src/daemon/handlers/index.ts +40 -277
  198. package/src/daemon/handlers/misc.ts +9 -1
  199. package/src/daemon/handlers/publish.ts +6 -1
  200. package/src/daemon/handlers/sessions.ts +65 -12
  201. package/src/daemon/handlers/shared.ts +36 -1
  202. package/src/daemon/handlers/signing.ts +37 -0
  203. package/src/daemon/handlers/skills.ts +20 -6
  204. package/src/daemon/handlers/subagents.ts +8 -3
  205. package/src/daemon/handlers/twitter-auth.ts +169 -0
  206. package/src/daemon/handlers/work-items.ts +384 -68
  207. package/src/daemon/ipc-contract-inventory.json +32 -4
  208. package/src/daemon/ipc-contract.ts +156 -37
  209. package/src/daemon/ipc-protocol.ts +7 -2
  210. package/src/daemon/lifecycle.ts +21 -0
  211. package/src/daemon/main.ts +10 -4
  212. package/src/daemon/ride-shotgun-handler.ts +75 -10
  213. package/src/daemon/server.ts +143 -26
  214. package/src/daemon/session-agent-loop.ts +922 -0
  215. package/src/daemon/session-attachments.ts +28 -5
  216. package/src/daemon/session-conflict-gate.ts +18 -109
  217. package/src/daemon/session-error.ts +24 -3
  218. package/src/daemon/session-lifecycle.ts +147 -0
  219. package/src/daemon/session-media-retry.ts +147 -0
  220. package/src/daemon/session-messaging.ts +145 -0
  221. package/src/daemon/session-notifiers.ts +164 -0
  222. package/src/daemon/session-process.ts +2 -2
  223. package/src/daemon/session-queue-manager.ts +1 -0
  224. package/src/daemon/session-runtime-assembly.ts +52 -0
  225. package/src/daemon/session-skill-tools.ts +124 -5
  226. package/src/daemon/session-slash.ts +3 -0
  227. package/src/daemon/session-surfaces.ts +77 -2
  228. package/src/daemon/session-tool-setup.ts +216 -2
  229. package/src/daemon/session-usage.ts +0 -2
  230. package/src/daemon/session.ts +114 -1404
  231. package/src/daemon/video-thumbnail.ts +60 -0
  232. package/src/doordash/client.ts +121 -27
  233. package/src/doordash/queries.ts +1 -2
  234. package/src/export/formatter.ts +3 -1
  235. package/src/followups/followup-store.ts +4 -2
  236. package/src/followups/types.ts +6 -0
  237. package/src/hooks/templates.ts +1 -1
  238. package/src/index.ts +32 -1153
  239. package/src/memory/attachments-store.ts +28 -83
  240. package/src/memory/channel-delivery-store.ts +7 -21
  241. package/src/memory/clarification-resolver.ts +6 -5
  242. package/src/memory/conflict-intent.ts +114 -0
  243. package/src/memory/contradiction-checker.ts +3 -2
  244. package/src/memory/conversation-key-store.ts +10 -29
  245. package/src/memory/conversation-store.ts +2 -1
  246. package/src/memory/db.ts +96 -2
  247. package/src/memory/entity-extractor.ts +6 -3
  248. package/src/memory/items-extractor.ts +5 -4
  249. package/src/memory/job-handlers/conflict.ts +23 -1
  250. package/src/memory/jobs-store.ts +3 -2
  251. package/src/memory/llm-usage-store.ts +1 -2
  252. package/src/memory/runs-store.ts +1 -2
  253. package/src/memory/schema.ts +23 -2
  254. package/src/messaging/style-analyzer.ts +3 -2
  255. package/src/messaging/thread-summarizer.ts +8 -12
  256. package/src/messaging/triage-engine.ts +4 -2
  257. package/src/providers/openrouter/client.ts +20 -0
  258. package/src/providers/registry.ts +8 -0
  259. package/src/runtime/gateway-client.ts +36 -0
  260. package/src/runtime/http-server.ts +166 -22
  261. package/src/runtime/routes/attachment-routes.ts +2 -3
  262. package/src/runtime/routes/call-routes.ts +140 -0
  263. package/src/runtime/routes/channel-routes.ts +125 -88
  264. package/src/runtime/routes/conversation-routes.ts +5 -5
  265. package/src/runtime/routes/run-routes.ts +2 -2
  266. package/src/runtime/run-orchestrator.ts +9 -3
  267. package/src/schedule/recurrence-engine.ts +138 -0
  268. package/src/schedule/recurrence-types.ts +67 -0
  269. package/src/schedule/schedule-store.ts +102 -57
  270. package/src/schedule/scheduler.ts +9 -6
  271. package/src/security/oauth2.ts +29 -4
  272. package/src/security/secret-allowlist.ts +46 -0
  273. package/src/skills/clawhub.ts +1 -1
  274. package/src/subagent/manager.ts +40 -8
  275. package/src/swarm/backend-claude-code.ts +64 -9
  276. package/src/swarm/worker-prompts.ts +2 -1
  277. package/src/tasks/SPEC.md +34 -28
  278. package/src/tasks/ephemeral-permissions.ts +16 -7
  279. package/src/tasks/task-compiler.ts +5 -4
  280. package/src/tasks/task-runner.ts +10 -5
  281. package/src/tasks/task-scheduler.ts +1 -1
  282. package/src/tasks/tool-sanitizer.ts +36 -0
  283. package/src/tools/assets/search.ts +4 -4
  284. package/src/tools/browser/api-map.ts +293 -0
  285. package/src/tools/browser/auto-navigate.ts +270 -0
  286. package/src/tools/browser/browser-execution.ts +2 -1
  287. package/src/tools/browser/browser-manager.ts +2 -2
  288. package/src/tools/browser/network-recorder.ts +5 -4
  289. package/src/tools/browser/x-auto-navigate.ts +207 -0
  290. package/src/tools/calls/call-end.ts +17 -67
  291. package/src/tools/calls/call-start.ts +24 -85
  292. package/src/tools/calls/call-status.ts +35 -51
  293. package/src/tools/claude-code/claude-code.ts +207 -11
  294. package/src/tools/contacts/contact-merge.ts +46 -78
  295. package/src/tools/contacts/contact-search.ts +35 -79
  296. package/src/tools/contacts/contact-upsert.ts +35 -108
  297. package/src/tools/credentials/vault.ts +20 -4
  298. package/src/tools/document/document-tool.ts +71 -144
  299. package/src/tools/executor.ts +129 -10
  300. package/src/tools/followups/followup_create.ts +46 -88
  301. package/src/tools/followups/followup_list.ts +34 -74
  302. package/src/tools/followups/followup_resolve.ts +31 -66
  303. package/src/tools/host-terminal/cli-discover.ts +2 -1
  304. package/src/tools/host-terminal/host-shell.ts +10 -0
  305. package/src/tools/memory/handlers.ts +5 -4
  306. package/src/tools/network/__tests__/web-search.test.ts +427 -0
  307. package/src/tools/network/script-proxy/__tests__/logging.test.ts +248 -0
  308. package/src/tools/network/script-proxy/__tests__/policy.test.ts +234 -0
  309. package/src/tools/network/script-proxy/__tests__/router.test.ts +76 -0
  310. package/src/tools/network/web-fetch.ts +18 -6
  311. package/src/tools/playbooks/index.ts +4 -5
  312. package/src/tools/playbooks/playbook-create.ts +3 -47
  313. package/src/tools/playbooks/playbook-delete.ts +1 -25
  314. package/src/tools/playbooks/playbook-list.ts +1 -28
  315. package/src/tools/playbooks/playbook-update.ts +3 -51
  316. package/src/tools/reminder/reminder.ts +5 -78
  317. package/src/tools/schedule/create.ts +69 -74
  318. package/src/tools/schedule/delete.ts +21 -47
  319. package/src/tools/schedule/list.ts +55 -74
  320. package/src/tools/schedule/update.ts +77 -84
  321. package/src/tools/subagent/abort.ts +29 -58
  322. package/src/tools/subagent/message.ts +30 -63
  323. package/src/tools/subagent/read.ts +53 -84
  324. package/src/tools/subagent/spawn.ts +43 -82
  325. package/src/tools/subagent/status.ts +42 -71
  326. package/src/tools/swarm/delegate.ts +2 -1
  327. package/src/tools/tasks/index.ts +8 -8
  328. package/src/tools/tasks/task-delete.ts +60 -88
  329. package/src/tools/tasks/task-list.ts +31 -52
  330. package/src/tools/tasks/task-run.ts +72 -108
  331. package/src/tools/tasks/task-save.ts +33 -65
  332. package/src/tools/tasks/work-item-enqueue.ts +183 -215
  333. package/src/tools/tasks/work-item-list.ts +33 -63
  334. package/src/tools/tasks/work-item-remove.ts +45 -97
  335. package/src/tools/tasks/work-item-update.ts +91 -163
  336. package/src/tools/terminal/backends/native.ts +3 -1
  337. package/src/tools/tool-manifest.ts +0 -62
  338. package/src/tools/types.ts +6 -0
  339. package/src/tools/ui-surface/definitions.ts +3 -1
  340. package/src/tools/watch/screen-watch.ts +3 -1
  341. package/src/tools/watcher/create.ts +52 -98
  342. package/src/tools/watcher/delete.ts +20 -46
  343. package/src/tools/watcher/digest.ts +36 -70
  344. package/src/tools/watcher/list.ts +49 -79
  345. package/src/tools/watcher/update.ts +45 -91
  346. package/src/twitter/client.ts +690 -0
  347. package/src/twitter/session.ts +91 -0
  348. package/src/usage/types.ts +0 -1
  349. package/src/util/truncate.ts +6 -0
  350. package/src/watcher/providers/slack.ts +2 -1
  351. package/src/watcher/watcher-store.ts +3 -2
  352. package/src/work-items/work-item-store.ts +27 -2
  353. package/src/workspace/commit-message-enrichment-service.ts +31 -7
  354. package/src/workspace/git-service.ts +87 -22
  355. package/src/workspace/provider-commit-message-generator.ts +269 -0
  356. package/src/workspace/turn-commit.ts +62 -3
  357. package/src/tools/contacts/index.ts +0 -4
  358. package/src/tools/document/index.ts +0 -5
  359. package/src/tools/followups/index.ts +0 -3
  360. package/src/tools/subagent/index.ts +0 -5
  361. /package/src/__tests__/{memory-context-benchmark.test.ts → memory-context-benchmark.benchmark.test.ts} +0 -0
@@ -0,0 +1,922 @@
1
+ /**
2
+ * Agent loop execution extracted from Session.runAgentLoop.
3
+ *
4
+ * This module contains the core agent loop orchestration: pre-flight
5
+ * setup, event handling, retry logic, history reconstruction, and
6
+ * completion event emission. The Session class delegates its
7
+ * runAgentLoop method here via the AgentLoopSessionContext interface.
8
+ */
9
+
10
+ import { v4 as uuid } from 'uuid';
11
+ import type { Message, ContentBlock, ImageContent } from '../providers/types.js';
12
+ import type { ServerMessage, UsageStats, SurfaceType, SurfaceData, DynamicPageSurfaceData } from './ipc-protocol.js';
13
+ import type { AgentLoop, CheckpointDecision, AgentEvent } from '../agent/loop.js';
14
+ import type { Provider } from '../providers/types.js';
15
+ import { createAssistantMessage } from '../agent/message-types.js';
16
+ import * as conversationStore from '../memory/conversation-store.js';
17
+ import type { PermissionPrompter } from '../permissions/prompter.js';
18
+ import { getConfig } from '../config/loader.js';
19
+ import { getLogger } from '../util/logger.js';
20
+ import type { TraceEmitter } from './trace-emitter.js';
21
+ import { classifySessionError, isUserCancellation, isContextTooLarge, buildSessionErrorMessage } from './session-error.js';
22
+ import type { ToolProfiler } from '../events/tool-profiling-listener.js';
23
+ import type { ContextWindowManager } from '../context/window-manager.js';
24
+ import { getHookManager } from '../hooks/manager.js';
25
+ import { truncate } from '../util/truncate.js';
26
+ import { stripMemoryRecallMessages } from '../memory/retriever.js';
27
+ import { getApp, listAppFiles } from '../memory/app-store.js';
28
+ import type { ConflictGate } from './session-conflict-gate.js';
29
+ import { stripDynamicProfileMessages } from './session-dynamic-profile.js';
30
+ import type { MessageQueue } from './session-queue-manager.js';
31
+ import type { QueueDrainReason } from './session-queue-manager.js';
32
+ import {
33
+ applyRuntimeInjections,
34
+ stripActiveSurfaceContext,
35
+ stripWorkspaceTopLevelContext,
36
+ stripChannelCapabilityContext,
37
+ stripTemporalContext,
38
+ } from './session-runtime-assembly.js';
39
+ import { buildTemporalContext } from './date-context.js';
40
+ import type { ActiveSurfaceContext, ChannelCapabilities } from './session-runtime-assembly.js';
41
+ import {
42
+ cleanAssistantContent,
43
+ drainDirectiveDisplayBuffer,
44
+ type DirectiveRequest,
45
+ type AssistantAttachmentDraft,
46
+ } from './assistant-attachments.js';
47
+ import { prepareMemoryContext } from './session-memory.js';
48
+ import {
49
+ approveHostAttachmentRead,
50
+ formatAttachmentWarnings,
51
+ resolveAssistantAttachments,
52
+ } from './session-attachments.js';
53
+ import { consolidateAssistantMessages } from './session-history.js';
54
+ import { recordUsage } from './session-usage.js';
55
+ import { recordRequestLog } from '../memory/llm-request-log-store.js';
56
+ import { isProviderOrderingError } from './session-slash.js';
57
+ import { repairHistory, deepRepairHistory } from './history-repair.js';
58
+ import { stripMediaPayloadsForRetry, raceWithTimeout } from './session-media-retry.js';
59
+ import { commitTurnChanges } from '../workspace/turn-commit.js';
60
+ import { getWorkspaceGitService } from '../workspace/git-service.js';
61
+ import type { UsageActor } from '../usage/actors.js';
62
+ import type { SkillProjectionCache } from './session-skill-tools.js';
63
+
64
+ const log = getLogger('session-agent-loop');
65
+
66
+ type GitServiceInitializer = {
67
+ ensureInitialized(): Promise<void>;
68
+ };
69
+
70
+ // ── Context Interface ────────────────────────────────────────────────
71
+
72
+ export interface AgentLoopSessionContext {
73
+ readonly conversationId: string;
74
+ messages: Message[];
75
+ processing: boolean;
76
+ abortController: AbortController | null;
77
+ currentRequestId?: string;
78
+
79
+ readonly agentLoop: AgentLoop;
80
+ readonly provider: Provider;
81
+ readonly systemPrompt: string;
82
+
83
+ readonly contextWindowManager: ContextWindowManager;
84
+ contextCompactedMessageCount: number;
85
+ contextCompactedAt: number | null;
86
+
87
+ readonly conflictGate: ConflictGate;
88
+ readonly memoryPolicy: { scopeId: string; includeDefaultFallback: boolean };
89
+
90
+ currentActiveSurfaceId?: string;
91
+ currentPage?: string;
92
+ readonly surfaceState: Map<string, { surfaceType: SurfaceType; data: SurfaceData }>;
93
+ pendingSurfaceActions: Map<string, { surfaceType: SurfaceType }>;
94
+ currentTurnSurfaces: Array<{ surfaceId: string; surfaceType: SurfaceType; title?: string; data: SurfaceData; actions?: Array<{ id: string; label: string; style?: string }>; display?: string }>;
95
+
96
+ workingDir: string;
97
+ workspaceTopLevelContext: string | null;
98
+ workspaceTopLevelDirty: boolean;
99
+ channelCapabilities?: ChannelCapabilities;
100
+
101
+ readonly coreToolNames: Set<string>;
102
+ allowedToolNames?: Set<string>;
103
+ preactivatedSkillIds?: string[];
104
+ readonly skillProjectionState: Map<string, string>;
105
+ readonly skillProjectionCache: SkillProjectionCache;
106
+
107
+ readonly traceEmitter: TraceEmitter;
108
+ readonly profiler: ToolProfiler;
109
+ usageStats: UsageStats;
110
+ turnCount: number;
111
+
112
+ lastAssistantAttachments: AssistantAttachmentDraft[];
113
+ lastAttachmentWarnings: string[];
114
+
115
+ hasNoClient: boolean;
116
+ readonly prompter: PermissionPrompter;
117
+ readonly queue: MessageQueue;
118
+
119
+ getWorkspaceGitService?: (workspaceDir: string) => GitServiceInitializer;
120
+ commitTurnChanges?: typeof commitTurnChanges;
121
+
122
+ refreshWorkspaceTopLevelContextIfNeeded(): void;
123
+ markWorkspaceTopLevelDirty(): void;
124
+ getQueueDepth(): number;
125
+ hasQueuedMessages(): boolean;
126
+ canHandoffAtCheckpoint(): boolean;
127
+ drainQueue(reason: QueueDrainReason): void;
128
+ }
129
+
130
+ // ── runAgentLoop ─────────────────────────────────────────────────────
131
+
132
+ export async function runAgentLoopImpl(
133
+ ctx: AgentLoopSessionContext,
134
+ content: string,
135
+ userMessageId: string,
136
+ onEvent: (msg: ServerMessage) => void,
137
+ options?: { skipPreMessageRollback?: boolean },
138
+ ): Promise<void> {
139
+ if (!ctx.abortController) {
140
+ throw new Error('runAgentLoop called without prior persistUserMessage');
141
+ }
142
+ const abortController = ctx.abortController;
143
+ const reqId = ctx.currentRequestId ?? uuid();
144
+ const rlog = log.child({ conversationId: ctx.conversationId, requestId: reqId });
145
+ let yieldedForHandoff = false;
146
+
147
+ ctx.lastAssistantAttachments = [];
148
+ ctx.lastAttachmentWarnings = [];
149
+
150
+ // Ensure workspace git repo is initialized before any tools run.
151
+ try {
152
+ const getWorkspaceGitServiceFn = ctx.getWorkspaceGitService ?? getWorkspaceGitService;
153
+ const gitService = getWorkspaceGitServiceFn(ctx.workingDir);
154
+ await gitService.ensureInitialized();
155
+ } catch (err) {
156
+ rlog.warn({ err }, 'Failed to initialize workspace git repo (non-fatal)');
157
+ }
158
+
159
+ ctx.profiler.startRequest();
160
+ let turnStarted = false;
161
+
162
+ try {
163
+ const preMessageResult = await getHookManager().trigger('pre-message', {
164
+ sessionId: ctx.conversationId,
165
+ messagePreview: truncate(content, 200, ''),
166
+ });
167
+
168
+ if (preMessageResult.blocked) {
169
+ if (!options?.skipPreMessageRollback) {
170
+ ctx.messages.pop();
171
+ conversationStore.deleteMessageById(userMessageId);
172
+ }
173
+ onEvent({ type: 'error', message: `Message blocked by hook "${preMessageResult.blockedBy}"` });
174
+ return;
175
+ }
176
+
177
+ const isFirstMessage = ctx.messages.length === 1;
178
+
179
+ const compacted = await ctx.contextWindowManager.maybeCompact(
180
+ ctx.messages,
181
+ abortController.signal,
182
+ { lastCompactedAt: ctx.contextCompactedAt ?? undefined },
183
+ );
184
+ if (compacted.compacted) {
185
+ ctx.messages = compacted.messages;
186
+ ctx.contextCompactedMessageCount += compacted.compactedPersistedMessages;
187
+ ctx.contextCompactedAt = Date.now();
188
+ conversationStore.updateConversationContextWindow(
189
+ ctx.conversationId,
190
+ compacted.summaryText,
191
+ ctx.contextCompactedMessageCount,
192
+ );
193
+ onEvent({
194
+ type: 'context_compacted',
195
+ previousEstimatedInputTokens: compacted.previousEstimatedInputTokens,
196
+ estimatedInputTokens: compacted.estimatedInputTokens,
197
+ maxInputTokens: compacted.maxInputTokens,
198
+ thresholdTokens: compacted.thresholdTokens,
199
+ compactedMessages: compacted.compactedMessages,
200
+ summaryCalls: compacted.summaryCalls,
201
+ summaryInputTokens: compacted.summaryInputTokens,
202
+ summaryOutputTokens: compacted.summaryOutputTokens,
203
+ summaryModel: compacted.summaryModel,
204
+ });
205
+ emitUsage(ctx, compacted.summaryInputTokens, compacted.summaryOutputTokens, compacted.summaryModel, onEvent, 'context_compactor', reqId);
206
+ }
207
+
208
+ let firstAssistantText = '';
209
+ let exchangeInputTokens = 0;
210
+ let exchangeOutputTokens = 0;
211
+ let model = '';
212
+ let runMessages = ctx.messages;
213
+ const pendingToolResults = new Map<string, { content: string; isError: boolean; contentBlocks?: ContentBlock[] }>();
214
+ const persistedToolUseIds = new Set<string>();
215
+ const accumulatedDirectives: DirectiveRequest[] = [];
216
+ const accumulatedToolContentBlocks: ContentBlock[] = [];
217
+ const directiveWarnings: string[] = [];
218
+ let pendingDirectiveDisplayBuffer = '';
219
+ let lastAssistantMessageId: string | undefined;
220
+ let providerErrorUserMessage: string | null = null;
221
+
222
+ const memoryResult = await prepareMemoryContext(
223
+ {
224
+ conversationId: ctx.conversationId,
225
+ messages: ctx.messages,
226
+ systemPrompt: ctx.systemPrompt,
227
+ provider: ctx.provider,
228
+ conflictGate: ctx.conflictGate,
229
+ scopeId: ctx.memoryPolicy.scopeId,
230
+ includeDefaultFallback: ctx.memoryPolicy.includeDefaultFallback,
231
+ },
232
+ content,
233
+ userMessageId,
234
+ abortController.signal,
235
+ onEvent,
236
+ );
237
+
238
+ if (memoryResult.conflictClarification) {
239
+ const assistantMessage = createAssistantMessage(memoryResult.conflictClarification);
240
+ conversationStore.addMessage(
241
+ ctx.conversationId,
242
+ 'assistant',
243
+ JSON.stringify(assistantMessage.content),
244
+ );
245
+ ctx.messages.push(assistantMessage);
246
+ onEvent({
247
+ type: 'assistant_text_delta',
248
+ text: memoryResult.conflictClarification,
249
+ sessionId: ctx.conversationId,
250
+ });
251
+ ctx.traceEmitter.emit('message_complete', 'Conflict clarification requested (relevant)', {
252
+ requestId: reqId,
253
+ status: 'info',
254
+ attributes: { conflictGate: 'relevant' },
255
+ });
256
+ onEvent({ type: 'message_complete', sessionId: ctx.conversationId });
257
+ return;
258
+ }
259
+
260
+ const { recall, dynamicProfile, softConflictInstruction, recallInjectionStrategy } = memoryResult;
261
+ runMessages = memoryResult.runMessages;
262
+
263
+ // Build active surface context
264
+ let activeSurface: ActiveSurfaceContext | null = null;
265
+ if (ctx.currentActiveSurfaceId) {
266
+ const stored = ctx.surfaceState.get(ctx.currentActiveSurfaceId);
267
+ if (stored && stored.surfaceType === 'dynamic_page') {
268
+ const data = stored.data as DynamicPageSurfaceData;
269
+ activeSurface = {
270
+ surfaceId: ctx.currentActiveSurfaceId,
271
+ html: data.html,
272
+ currentPage: ctx.currentPage,
273
+ };
274
+ if (data.appId) {
275
+ const app = getApp(data.appId);
276
+ if (app) {
277
+ activeSurface.appId = app.id;
278
+ activeSurface.appName = app.name;
279
+ activeSurface.appSchemaJson = app.schemaJson;
280
+ activeSurface.appFiles = listAppFiles(app.id);
281
+ if (app.pages && Object.keys(app.pages).length > 0) {
282
+ activeSurface.appPages = app.pages;
283
+ }
284
+ }
285
+ }
286
+ }
287
+ }
288
+
289
+ ctx.refreshWorkspaceTopLevelContextIfNeeded();
290
+
291
+ // Compute fresh temporal context each turn for date grounding.
292
+ const temporalContext = buildTemporalContext({
293
+ timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
294
+ });
295
+
296
+ runMessages = applyRuntimeInjections(runMessages, {
297
+ softConflictInstruction,
298
+ activeSurface,
299
+ workspaceTopLevelContext: ctx.workspaceTopLevelContext,
300
+ channelCapabilities: ctx.channelCapabilities ?? null,
301
+ temporalContext,
302
+ });
303
+
304
+ // Pre-run repair
305
+ let preRepairMessages = runMessages;
306
+ const preRunRepair = repairHistory(runMessages);
307
+ if (preRunRepair.stats.assistantToolResultsMigrated > 0 || preRunRepair.stats.missingToolResultsInserted > 0 || preRunRepair.stats.orphanToolResultsDowngraded > 0 || preRunRepair.stats.consecutiveSameRoleMerged > 0) {
308
+ rlog.warn({ phase: 'pre_run', ...preRunRepair.stats }, 'Repaired runtime history before provider call');
309
+ runMessages = preRunRepair.messages;
310
+ }
311
+
312
+ let orderingErrorDetected = false;
313
+ let deferredOrderingError: string | null = null;
314
+ let contextTooLargeDetected = false;
315
+ let preRunHistoryLength = runMessages.length;
316
+
317
+ let llmCallStartedEmitted = false;
318
+ const toolUseIdToName = new Map<string, string>();
319
+ let currentTurnToolNames: string[] = [];
320
+
321
+ const buildEventHandler = () => (event: AgentEvent) => {
322
+ const emitLlmCallStartedIfNeeded = () => {
323
+ if (llmCallStartedEmitted) return;
324
+ llmCallStartedEmitted = true;
325
+ ctx.traceEmitter.emit('llm_call_started', `LLM call to ${ctx.provider.name}`, {
326
+ requestId: reqId,
327
+ status: 'info',
328
+ attributes: { provider: ctx.provider.name, model: model || 'unknown' },
329
+ });
330
+ };
331
+
332
+ switch (event.type) {
333
+ case 'text_delta': {
334
+ emitLlmCallStartedIfNeeded();
335
+ pendingDirectiveDisplayBuffer += event.text;
336
+ const drained = drainDirectiveDisplayBuffer(pendingDirectiveDisplayBuffer);
337
+ pendingDirectiveDisplayBuffer = drained.bufferedRemainder;
338
+ if (drained.emitText.length > 0) {
339
+ onEvent({ type: 'assistant_text_delta', text: drained.emitText, sessionId: ctx.conversationId });
340
+ if (isFirstMessage) firstAssistantText += drained.emitText;
341
+ }
342
+ break;
343
+ }
344
+ case 'thinking_delta':
345
+ emitLlmCallStartedIfNeeded();
346
+ onEvent({ type: 'assistant_thinking_delta', thinking: event.thinking });
347
+ break;
348
+ case 'tool_use':
349
+ toolUseIdToName.set(event.id, event.name);
350
+ currentTurnToolNames.push(event.name);
351
+ onEvent({ type: 'tool_use_start', toolName: event.name, input: event.input, sessionId: ctx.conversationId });
352
+ break;
353
+ case 'tool_output_chunk': {
354
+ // Try to parse structured progress fields from the chunk.
355
+ // Cheap pre-check: only attempt JSON.parse when the chunk looks like an object.
356
+ let structured: { subType?: 'tool_start' | 'tool_complete' | 'status'; subToolName?: string; subToolInput?: string; subToolIsError?: boolean; subToolId?: string } | undefined;
357
+ const trimmed = event.chunk.trimStart();
358
+ if (trimmed.length > 0 && trimmed.length < 4096 && trimmed[0] === '{') {
359
+ try {
360
+ const parsed = JSON.parse(event.chunk);
361
+ const VALID_SUB_TYPES = new Set(['tool_start', 'tool_complete', 'status']);
362
+ if (parsed && typeof parsed === 'object' && typeof parsed.subType === 'string' && VALID_SUB_TYPES.has(parsed.subType)) {
363
+ structured = {
364
+ subType: parsed.subType as 'tool_start' | 'tool_complete' | 'status',
365
+ subToolName: typeof parsed.subToolName === 'string' ? parsed.subToolName : undefined,
366
+ subToolInput: typeof parsed.subToolInput === 'string' ? parsed.subToolInput : undefined,
367
+ subToolIsError: typeof parsed.subToolIsError === 'boolean' ? parsed.subToolIsError : undefined,
368
+ subToolId: typeof parsed.subToolId === 'string' ? parsed.subToolId : undefined,
369
+ };
370
+ }
371
+ } catch {
372
+ // Not valid JSON — pass through as plain chunk
373
+ }
374
+ }
375
+ if (structured) {
376
+ onEvent({
377
+ type: 'tool_output_chunk',
378
+ chunk: event.chunk,
379
+ sessionId: ctx.conversationId,
380
+ subType: structured.subType,
381
+ subToolName: structured.subToolName,
382
+ subToolInput: structured.subToolInput,
383
+ subToolIsError: structured.subToolIsError,
384
+ subToolId: structured.subToolId,
385
+ });
386
+ } else {
387
+ onEvent({ type: 'tool_output_chunk', chunk: event.chunk, sessionId: ctx.conversationId });
388
+ }
389
+ break;
390
+ }
391
+ case 'input_json_delta':
392
+ onEvent({ type: 'tool_input_delta', toolName: event.toolName, content: event.accumulatedJson, sessionId: ctx.conversationId });
393
+ break;
394
+ case 'tool_result': {
395
+ const imageBlock = event.contentBlocks?.find((b): b is ImageContent => b.type === 'image');
396
+ onEvent({ type: 'tool_result', toolName: '', result: event.content, isError: event.isError, diff: event.diff, status: event.status, sessionId: ctx.conversationId, imageData: imageBlock?.source.data });
397
+ pendingToolResults.set(event.toolUseId, { content: event.content, isError: event.isError, contentBlocks: event.contentBlocks });
398
+ {
399
+ const toolName = toolUseIdToName.get(event.toolUseId);
400
+ if (toolName === 'file_write' || toolName === 'bash') {
401
+ ctx.markWorkspaceTopLevelDirty();
402
+ } else if (toolName === 'file_edit' && !event.isError) {
403
+ ctx.markWorkspaceTopLevelDirty();
404
+ }
405
+ }
406
+ if (event.contentBlocks) {
407
+ for (const cb of event.contentBlocks) {
408
+ if (cb.type === 'image' || cb.type === 'file') {
409
+ accumulatedToolContentBlocks.push(cb);
410
+ }
411
+ }
412
+ }
413
+ break;
414
+ }
415
+ case 'error':
416
+ if (isProviderOrderingError(event.error.message)) {
417
+ orderingErrorDetected = true;
418
+ deferredOrderingError = event.error.message;
419
+ } else if (isContextTooLarge(event.error.message)) {
420
+ contextTooLargeDetected = true;
421
+ } else {
422
+ const classified = classifySessionError(event.error, { phase: 'agent_loop' });
423
+ onEvent(buildSessionErrorMessage(ctx.conversationId, classified));
424
+ providerErrorUserMessage = classified.userMessage;
425
+ }
426
+ break;
427
+ case 'message_complete': {
428
+ if (pendingDirectiveDisplayBuffer.length > 0) {
429
+ onEvent({
430
+ type: 'assistant_text_delta',
431
+ text: pendingDirectiveDisplayBuffer,
432
+ sessionId: ctx.conversationId,
433
+ });
434
+ if (isFirstMessage) firstAssistantText += pendingDirectiveDisplayBuffer;
435
+ pendingDirectiveDisplayBuffer = '';
436
+ }
437
+ if (pendingToolResults.size > 0) {
438
+ const toolResultBlocks = Array.from(pendingToolResults.entries()).map(
439
+ ([toolUseId, result]) => ({
440
+ type: 'tool_result',
441
+ tool_use_id: toolUseId,
442
+ content: result.content,
443
+ is_error: result.isError,
444
+ ...(result.contentBlocks ? { contentBlocks: result.contentBlocks } : {}),
445
+ }),
446
+ );
447
+ conversationStore.addMessage(
448
+ ctx.conversationId,
449
+ 'user',
450
+ JSON.stringify(toolResultBlocks),
451
+ );
452
+ for (const id of pendingToolResults.keys()) {
453
+ persistedToolUseIds.add(id);
454
+ }
455
+ pendingToolResults.clear();
456
+ }
457
+ const { cleanedContent, directives: msgDirectives, warnings: msgWarnings } =
458
+ cleanAssistantContent(event.message.content);
459
+ accumulatedDirectives.push(...msgDirectives);
460
+ directiveWarnings.push(...msgWarnings);
461
+ if (msgDirectives.length > 0) {
462
+ rlog.info(
463
+ { parsedDirectives: msgDirectives.map(d => ({ source: d.source, path: d.path, mimeType: d.mimeType })), totalAccumulated: accumulatedDirectives.length },
464
+ 'Parsed attachment directives from assistant message',
465
+ );
466
+ }
467
+
468
+ const contentWithSurfaces: ContentBlock[] = [...cleanedContent as ContentBlock[]];
469
+ for (const surface of ctx.currentTurnSurfaces) {
470
+ contentWithSurfaces.push({
471
+ type: 'ui_surface',
472
+ surfaceId: surface.surfaceId,
473
+ surfaceType: surface.surfaceType,
474
+ title: surface.title,
475
+ data: surface.data,
476
+ actions: surface.actions,
477
+ display: surface.display,
478
+ } as unknown as ContentBlock);
479
+ }
480
+
481
+ const assistantMsg = conversationStore.addMessage(
482
+ ctx.conversationId,
483
+ 'assistant',
484
+ JSON.stringify(contentWithSurfaces),
485
+ );
486
+ lastAssistantMessageId = assistantMsg.id;
487
+
488
+ ctx.currentTurnSurfaces = [];
489
+
490
+ const charCount = cleanedContent
491
+ .filter((b) => (b as Record<string, unknown>).type === 'text')
492
+ .reduce((sum: number, b) => sum + ((b as { text?: string }).text?.length ?? 0), 0);
493
+ const toolUseCount = event.message.content
494
+ .filter((b) => b.type === 'tool_use')
495
+ .length;
496
+ ctx.traceEmitter.emit('assistant_message', 'Assistant message complete', {
497
+ requestId: reqId,
498
+ status: 'success',
499
+ attributes: { charCount, toolUseCount },
500
+ });
501
+ break;
502
+ }
503
+ case 'usage':
504
+ exchangeInputTokens += event.inputTokens;
505
+ exchangeOutputTokens += event.outputTokens;
506
+ model = event.model;
507
+
508
+ if (event.rawRequest && event.rawResponse) {
509
+ try {
510
+ recordRequestLog(
511
+ ctx.conversationId,
512
+ JSON.stringify(event.rawRequest),
513
+ JSON.stringify(event.rawResponse),
514
+ );
515
+ } catch (err) {
516
+ rlog.warn({ err }, 'Failed to persist LLM request log (non-fatal)');
517
+ }
518
+ }
519
+
520
+ emitLlmCallStartedIfNeeded();
521
+
522
+ ctx.traceEmitter.emit('llm_call_finished', `LLM call to ${ctx.provider.name} finished`, {
523
+ requestId: reqId,
524
+ status: 'success',
525
+ attributes: {
526
+ provider: ctx.provider.name,
527
+ model: event.model,
528
+ inputTokens: event.inputTokens,
529
+ outputTokens: event.outputTokens,
530
+ latencyMs: event.providerDurationMs,
531
+ },
532
+ });
533
+ llmCallStartedEmitted = false;
534
+ break;
535
+ }
536
+ };
537
+
538
+ const onCheckpoint = (): CheckpointDecision => {
539
+ const turnTools = currentTurnToolNames;
540
+ currentTurnToolNames = [];
541
+
542
+ if (ctx.canHandoffAtCheckpoint()) {
543
+ const inBrowserFlow = turnTools.length > 0
544
+ && turnTools.every(n => n.startsWith('browser_'));
545
+ if (!inBrowserFlow) {
546
+ yieldedForHandoff = true;
547
+ return 'yield';
548
+ }
549
+ }
550
+ return 'continue';
551
+ };
552
+
553
+ turnStarted = true;
554
+
555
+ let updatedHistory = await ctx.agentLoop.run(
556
+ runMessages,
557
+ buildEventHandler(),
558
+ abortController.signal,
559
+ reqId,
560
+ onCheckpoint,
561
+ );
562
+
563
+ // One-shot ordering error retry
564
+ if (orderingErrorDetected && updatedHistory.length === preRunHistoryLength) {
565
+ rlog.warn({ phase: 'retry' }, 'Provider ordering error detected, attempting one-shot deep-repair retry');
566
+ const retryRepair = deepRepairHistory(runMessages);
567
+ runMessages = retryRepair.messages;
568
+ preRepairMessages = retryRepair.messages;
569
+ preRunHistoryLength = runMessages.length;
570
+ orderingErrorDetected = false;
571
+ deferredOrderingError = null;
572
+
573
+ updatedHistory = await ctx.agentLoop.run(
574
+ runMessages,
575
+ buildEventHandler(),
576
+ abortController.signal,
577
+ reqId,
578
+ onCheckpoint,
579
+ );
580
+
581
+ if (orderingErrorDetected) {
582
+ rlog.error({ phase: 'retry' }, 'Deep-repair retry also failed with ordering error. Consider starting a new conversation if this persists.');
583
+ }
584
+ }
585
+
586
+ // One-shot context-too-large recovery
587
+ if (contextTooLargeDetected && updatedHistory.length === preRunHistoryLength) {
588
+ rlog.warn({ phase: 'retry' }, 'Context too large — attempting forced compaction and retry');
589
+ const emergencyCompact = await ctx.contextWindowManager.maybeCompact(
590
+ ctx.messages,
591
+ abortController.signal,
592
+ { lastCompactedAt: ctx.contextCompactedAt ?? undefined, force: true },
593
+ );
594
+ if (emergencyCompact.compacted) {
595
+ ctx.messages = emergencyCompact.messages;
596
+ ctx.contextCompactedMessageCount += emergencyCompact.compactedPersistedMessages;
597
+ ctx.contextCompactedAt = Date.now();
598
+ conversationStore.updateConversationContextWindow(
599
+ ctx.conversationId,
600
+ emergencyCompact.summaryText,
601
+ ctx.contextCompactedMessageCount,
602
+ );
603
+ onEvent({
604
+ type: 'context_compacted',
605
+ previousEstimatedInputTokens: emergencyCompact.previousEstimatedInputTokens,
606
+ estimatedInputTokens: emergencyCompact.estimatedInputTokens,
607
+ maxInputTokens: emergencyCompact.maxInputTokens,
608
+ thresholdTokens: emergencyCompact.thresholdTokens,
609
+ compactedMessages: emergencyCompact.compactedMessages,
610
+ summaryCalls: emergencyCompact.summaryCalls,
611
+ summaryInputTokens: emergencyCompact.summaryInputTokens,
612
+ summaryOutputTokens: emergencyCompact.summaryOutputTokens,
613
+ summaryModel: emergencyCompact.summaryModel,
614
+ });
615
+ emitUsage(ctx, emergencyCompact.summaryInputTokens, emergencyCompact.summaryOutputTokens, emergencyCompact.summaryModel, onEvent, 'context_compactor', reqId);
616
+
617
+ runMessages = applyRuntimeInjections(ctx.messages, {
618
+ softConflictInstruction,
619
+ activeSurface,
620
+ workspaceTopLevelContext: ctx.workspaceTopLevelContext,
621
+ channelCapabilities: ctx.channelCapabilities ?? null,
622
+ temporalContext,
623
+ });
624
+ preRepairMessages = runMessages;
625
+ preRunHistoryLength = runMessages.length;
626
+ contextTooLargeDetected = false;
627
+
628
+ updatedHistory = await ctx.agentLoop.run(
629
+ runMessages,
630
+ buildEventHandler(),
631
+ abortController.signal,
632
+ reqId,
633
+ onCheckpoint,
634
+ );
635
+ }
636
+
637
+ if (contextTooLargeDetected) {
638
+ const mediaTrimmed = stripMediaPayloadsForRetry(ctx.messages);
639
+ if (mediaTrimmed.modified) {
640
+ rlog.warn(
641
+ {
642
+ phase: 'retry',
643
+ replacedBlocks: mediaTrimmed.replacedBlocks,
644
+ latestUserIndex: mediaTrimmed.latestUserIndex,
645
+ },
646
+ 'Context still too large — retrying with older media payloads trimmed',
647
+ );
648
+ ctx.messages = mediaTrimmed.messages;
649
+ runMessages = applyRuntimeInjections(ctx.messages, {
650
+ softConflictInstruction,
651
+ activeSurface,
652
+ workspaceTopLevelContext: ctx.workspaceTopLevelContext,
653
+ channelCapabilities: ctx.channelCapabilities ?? null,
654
+ temporalContext,
655
+ });
656
+ preRepairMessages = runMessages;
657
+ preRunHistoryLength = runMessages.length;
658
+ contextTooLargeDetected = false;
659
+
660
+ updatedHistory = await ctx.agentLoop.run(
661
+ runMessages,
662
+ buildEventHandler(),
663
+ abortController.signal,
664
+ reqId,
665
+ onCheckpoint,
666
+ );
667
+ }
668
+ }
669
+
670
+ if (contextTooLargeDetected) {
671
+ const classified = classifySessionError(
672
+ new Error('context_length_exceeded'),
673
+ { phase: 'agent_loop' },
674
+ );
675
+ onEvent(buildSessionErrorMessage(ctx.conversationId, classified));
676
+ }
677
+ }
678
+
679
+ if (deferredOrderingError) {
680
+ const classified = classifySessionError(new Error(deferredOrderingError), { phase: 'agent_loop' });
681
+ onEvent(buildSessionErrorMessage(ctx.conversationId, classified));
682
+ }
683
+
684
+ // Reconcile synthesized cancellation tool_results
685
+ for (let i = preRunHistoryLength; i < updatedHistory.length; i++) {
686
+ const msg = updatedHistory[i];
687
+ if (msg.role === 'user') {
688
+ for (const block of msg.content) {
689
+ if (block.type === 'tool_result' && !pendingToolResults.has(block.tool_use_id) && !persistedToolUseIds.has(block.tool_use_id)) {
690
+ pendingToolResults.set(block.tool_use_id, {
691
+ content: block.content,
692
+ isError: block.is_error ?? false,
693
+ });
694
+ }
695
+ }
696
+ }
697
+ }
698
+
699
+ // Flush remaining tool results
700
+ if (pendingToolResults.size > 0) {
701
+ const toolResultBlocks = Array.from(pendingToolResults.entries()).map(
702
+ ([toolUseId, result]) => ({
703
+ type: 'tool_result',
704
+ tool_use_id: toolUseId,
705
+ content: result.content,
706
+ is_error: result.isError,
707
+ ...(result.contentBlocks ? { contentBlocks: result.contentBlocks } : {}),
708
+ }),
709
+ );
710
+ conversationStore.addMessage(
711
+ ctx.conversationId,
712
+ 'user',
713
+ JSON.stringify(toolResultBlocks),
714
+ );
715
+ pendingToolResults.clear();
716
+ }
717
+
718
+ // Reconstruct history
719
+ const newMessages = updatedHistory.slice(preRunHistoryLength).map((msg) => {
720
+ if (msg.role !== 'assistant') return msg;
721
+ const { cleanedContent } = cleanAssistantContent(msg.content);
722
+ return { ...msg, content: cleanedContent as ContentBlock[] };
723
+ });
724
+
725
+ const hasAssistantResponse = newMessages.some((msg) => msg.role === 'assistant');
726
+ if (!hasAssistantResponse && providerErrorUserMessage && !abortController.signal.aborted && !yieldedForHandoff) {
727
+ const errorAssistantMessage = createAssistantMessage(providerErrorUserMessage);
728
+ conversationStore.addMessage(
729
+ ctx.conversationId,
730
+ 'assistant',
731
+ JSON.stringify(errorAssistantMessage.content),
732
+ );
733
+ newMessages.push(errorAssistantMessage);
734
+ onEvent({
735
+ type: 'assistant_text_delta',
736
+ text: providerErrorUserMessage,
737
+ sessionId: ctx.conversationId,
738
+ });
739
+ }
740
+
741
+ const restoredHistory = [...preRepairMessages, ...newMessages];
742
+ const recallStripped = stripMemoryRecallMessages(restoredHistory, recall.injectedText, recallInjectionStrategy);
743
+ ctx.messages = stripTemporalContext(
744
+ stripChannelCapabilityContext(
745
+ stripWorkspaceTopLevelContext(
746
+ stripActiveSurfaceContext(
747
+ stripDynamicProfileMessages(recallStripped, dynamicProfile.text),
748
+ ),
749
+ ),
750
+ ),
751
+ );
752
+
753
+ emitUsage(ctx, exchangeInputTokens, exchangeOutputTokens, model, onEvent, 'main_agent', reqId);
754
+
755
+ void getHookManager().trigger('post-message', {
756
+ sessionId: ctx.conversationId,
757
+ });
758
+
759
+ // Resolve attachments
760
+ const attachmentResult = await resolveAssistantAttachments(
761
+ accumulatedDirectives,
762
+ accumulatedToolContentBlocks,
763
+ directiveWarnings,
764
+ ctx.workingDir,
765
+ async (filePath) => approveHostAttachmentRead(filePath, ctx.workingDir, ctx.prompter, ctx.conversationId, ctx.hasNoClient),
766
+ lastAssistantMessageId,
767
+ );
768
+ const { assistantAttachments, emittedAttachments } = attachmentResult;
769
+
770
+ ctx.lastAssistantAttachments = assistantAttachments;
771
+ ctx.lastAttachmentWarnings = attachmentResult.directiveWarnings;
772
+
773
+ const warningText = formatAttachmentWarnings(attachmentResult.directiveWarnings);
774
+ if (warningText) {
775
+ onEvent({ type: 'assistant_text_delta', text: warningText, sessionId: ctx.conversationId });
776
+ }
777
+
778
+ // Emit completion event
779
+ if (yieldedForHandoff) {
780
+ ctx.traceEmitter.emit('generation_handoff', 'Handing off to next queued message', {
781
+ requestId: reqId,
782
+ status: 'info',
783
+ attributes: { queuedCount: ctx.getQueueDepth() },
784
+ });
785
+ onEvent({
786
+ type: 'generation_handoff',
787
+ sessionId: ctx.conversationId,
788
+ requestId: reqId,
789
+ queuedCount: ctx.getQueueDepth(),
790
+ ...(emittedAttachments.length > 0 ? { attachments: emittedAttachments } : {}),
791
+ });
792
+ } else if (abortController.signal.aborted) {
793
+ ctx.traceEmitter.emit('generation_cancelled', 'Generation cancelled by user', {
794
+ requestId: reqId,
795
+ status: 'warning',
796
+ });
797
+ onEvent({ type: 'generation_cancelled', sessionId: ctx.conversationId });
798
+ } else {
799
+ ctx.traceEmitter.emit('message_complete', 'Message processing complete', {
800
+ requestId: reqId,
801
+ status: 'success',
802
+ });
803
+ onEvent({
804
+ type: 'message_complete',
805
+ sessionId: ctx.conversationId,
806
+ ...(emittedAttachments.length > 0 ? { attachments: emittedAttachments } : {}),
807
+ });
808
+ }
809
+
810
+ if (isFirstMessage) {
811
+ generateTitle(ctx, content, firstAssistantText).catch((err) => {
812
+ log.warn({ err, conversationId: ctx.conversationId }, 'Failed to generate conversation title (non-fatal, using default title)');
813
+ });
814
+ }
815
+ } catch (err) {
816
+ const errorCtx = { phase: 'agent_loop' as const, aborted: abortController.signal.aborted };
817
+ if (isUserCancellation(err, errorCtx)) {
818
+ rlog.info('Generation cancelled by user');
819
+ ctx.traceEmitter.emit('generation_cancelled', 'Generation cancelled by user', {
820
+ requestId: reqId,
821
+ status: 'warning',
822
+ });
823
+ onEvent({ type: 'generation_cancelled', sessionId: ctx.conversationId });
824
+ } else {
825
+ const message = err instanceof Error ? err.message : String(err);
826
+ const errorClass = err instanceof Error ? err.constructor.name : 'Error';
827
+ rlog.error({ err }, 'Session processing error');
828
+ ctx.traceEmitter.emit('request_error', truncate(message, 200, ''), {
829
+ requestId: reqId,
830
+ status: 'error',
831
+ attributes: { errorClass, message: truncate(message, 500, '') },
832
+ });
833
+ onEvent({ type: 'error', message: `Failed to process message: ${message}` });
834
+ const classified = classifySessionError(err, errorCtx);
835
+ onEvent(buildSessionErrorMessage(ctx.conversationId, classified));
836
+ void getHookManager().trigger('on-error', {
837
+ error: err instanceof Error ? err.name : 'Error',
838
+ message,
839
+ stack: err instanceof Error ? err.stack : undefined,
840
+ sessionId: ctx.conversationId,
841
+ });
842
+ }
843
+ } finally {
844
+ if (turnStarted) {
845
+ ctx.turnCount++;
846
+ const config = getConfig();
847
+ const maxWait = config.workspaceGit?.turnCommitMaxWaitMs ?? 4000;
848
+ const deadlineMs = Date.now() + maxWait;
849
+ const commitTurnChangesFn = ctx.commitTurnChanges ?? commitTurnChanges;
850
+ const commitPromise = commitTurnChangesFn(
851
+ ctx.workingDir, ctx.conversationId, ctx.turnCount,
852
+ undefined,
853
+ deadlineMs,
854
+ );
855
+ const outcome = await raceWithTimeout(commitPromise, maxWait);
856
+ if (outcome === 'timed_out') {
857
+ rlog.warn(
858
+ { turnNumber: ctx.turnCount, maxWaitMs: maxWait, conversationId: ctx.conversationId },
859
+ 'Turn-boundary commit timed out — continuing without waiting (commit still runs in background)',
860
+ );
861
+ }
862
+ }
863
+
864
+ ctx.profiler.emitSummary(ctx.traceEmitter, reqId);
865
+
866
+ ctx.abortController = null;
867
+ ctx.processing = false;
868
+ ctx.currentRequestId = undefined;
869
+ ctx.currentActiveSurfaceId = undefined;
870
+ ctx.allowedToolNames = undefined;
871
+ ctx.preactivatedSkillIds = undefined;
872
+
873
+ if (userMessageId) {
874
+ consolidateAssistantMessages(ctx.conversationId, userMessageId);
875
+ }
876
+
877
+ ctx.drainQueue(yieldedForHandoff ? 'checkpoint_handoff' : 'loop_complete');
878
+ }
879
+ }
880
+
881
+ // ── generateTitle ────────────────────────────────────────────────────
882
+
883
+ async function generateTitle(
884
+ ctx: Pick<AgentLoopSessionContext, 'conversationId' | 'provider'>,
885
+ userMessage: string,
886
+ assistantResponse: string,
887
+ ): Promise<void> {
888
+ const prompt = `Generate a very short title for this conversation. Rules: at most 5 words, at most 40 characters, no quotes.\n\nUser: ${truncate(userMessage, 200, '')}\nAssistant: ${truncate(assistantResponse, 200, '')}`;
889
+ const response = await ctx.provider.sendMessage(
890
+ [{ role: 'user', content: [{ type: 'text', text: prompt }] }],
891
+ [],
892
+ undefined,
893
+ { config: { max_tokens: 30 } },
894
+ );
895
+
896
+ const textBlock = response.content.find((b) => b.type === 'text');
897
+ if (textBlock && textBlock.type === 'text') {
898
+ let title = textBlock.text.trim().replace(/^["']|["']$/g, '');
899
+ const words = title.split(/\s+/);
900
+ if (words.length > 5) title = words.slice(0, 5).join(' ');
901
+ if (title.length > 40) title = title.slice(0, 40).trimEnd();
902
+ conversationStore.updateConversationTitle(ctx.conversationId, title);
903
+ log.info({ conversationId: ctx.conversationId, title }, 'Auto-generated conversation title');
904
+ }
905
+ }
906
+
907
+ // ── Helper ───────────────────────────────────────────────────────────
908
+
909
+ function emitUsage(
910
+ ctx: Pick<AgentLoopSessionContext, 'conversationId' | 'provider' | 'usageStats'>,
911
+ inputTokens: number,
912
+ outputTokens: number,
913
+ model: string,
914
+ onEvent: (msg: ServerMessage) => void,
915
+ actor: UsageActor,
916
+ requestId: string | null = null,
917
+ ): void {
918
+ recordUsage(
919
+ { conversationId: ctx.conversationId, providerName: ctx.provider.name, usageStats: ctx.usageStats },
920
+ inputTokens, outputTokens, model, onEvent, actor, requestId,
921
+ );
922
+ }