mixdog 1.4.3 → 1.4.5

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 (281) hide show
  1. package/package.json +1 -2
  2. package/src/cli.mjs +5 -1
  3. package/src/defaults/shipped-history.json +30 -2
  4. package/src/defaults/skills/browser-use/SKILL.md +148 -273
  5. package/src/defaults/skills/browser-use/references/devtools.md +14 -0
  6. package/src/defaults/skills/browser-use/references/page-flows.md +66 -0
  7. package/src/defaults/skills/browser-use/references/sign-in.md +70 -0
  8. package/src/defaults/skills/code-tidy/SKILL.md +60 -84
  9. package/src/defaults/skills/code-tidy/references/agent-cleanup.md +27 -2
  10. package/src/defaults/skills/computer-use/SKILL.md +145 -229
  11. package/src/defaults/skills/computer-use/references/flows.md +35 -0
  12. package/src/defaults/skills/computer-use/references/operations.md +22 -0
  13. package/src/defaults/skills/computer-use/references/platforms.md +12 -0
  14. package/src/defaults/skills/docx/SKILL.md +23 -31
  15. package/src/defaults/skills/docx/references/native-authoring.md +26 -3
  16. package/src/defaults/skills/docx/references/redlining.md +15 -0
  17. package/src/defaults/skills/image/SKILL.md +37 -77
  18. package/src/defaults/skills/image/references/prompt-forms.md +51 -0
  19. package/src/defaults/skills/image/references/text.md +6 -0
  20. package/src/defaults/skills/local-provider/SKILL.md +75 -105
  21. package/src/defaults/skills/local-provider/references/lifecycle.md +14 -0
  22. package/src/defaults/skills/local-provider/references/maintenance.md +19 -0
  23. package/src/defaults/skills/local-provider/references/recovery.md +30 -0
  24. package/src/defaults/skills/memory-management/SKILL.md +22 -21
  25. package/src/defaults/skills/pdf/SKILL.md +21 -23
  26. package/src/defaults/skills/pdf/references/blocks.md +20 -0
  27. package/src/defaults/skills/pdf/references/forms.md +12 -0
  28. package/src/defaults/skills/pdf/references/html.md +2 -2
  29. package/src/defaults/skills/pdf/references/ocr.md +7 -0
  30. package/src/defaults/skills/pdf/references/security.md +8 -0
  31. package/src/defaults/skills/pptx/SKILL.md +30 -109
  32. package/src/defaults/skills/pptx/references/audit.md +45 -0
  33. package/src/defaults/skills/pptx/references/brief-critique.md +47 -0
  34. package/src/defaults/skills/pptx/references/editing.md +24 -0
  35. package/src/defaults/skills/pptx/references/html.md +1 -1
  36. package/src/defaults/skills/pptx/references/script.md +33 -0
  37. package/src/defaults/skills/setup/SKILL.md +34 -102
  38. package/src/defaults/skills/setup/references/actions.md +73 -14
  39. package/src/defaults/skills/setup/references/surfaces.md +23 -8
  40. package/src/defaults/skills/setup-guide/SKILL.md +28 -115
  41. package/src/defaults/skills/setup-guide/references/features.md +36 -0
  42. package/src/defaults/skills/setup-guide/references/glossary.md +38 -0
  43. package/src/defaults/skills/setup-guide/references/workflow-models.md +62 -0
  44. package/src/defaults/skills/test-suite/SKILL.md +15 -127
  45. package/src/defaults/skills/test-suite/references/case-management.md +17 -9
  46. package/src/defaults/skills/test-suite/references/diagnose.md +23 -0
  47. package/src/defaults/skills/test-suite/references/prune.md +27 -0
  48. package/src/defaults/skills/test-suite/references/write.md +44 -0
  49. package/src/defaults/skills/video/SKILL.md +28 -42
  50. package/src/defaults/skills/video/references/prompting.md +31 -0
  51. package/src/defaults/skills/visualize/SKILL.md +73 -107
  52. package/src/defaults/skills/visualize/references/craft.md +1 -2
  53. package/src/defaults/skills/visualize/references/feedback.md +36 -0
  54. package/src/defaults/skills/visualize/references/showing.md +13 -0
  55. package/src/defaults/skills/xlsx/SKILL.md +23 -50
  56. package/src/defaults/skills/xlsx/references/editing.md +15 -0
  57. package/src/defaults/skills/xlsx/references/model-conventions.md +1 -1
  58. package/src/defaults/skills/xlsx/references/recalculation.md +37 -0
  59. package/src/defaults/skills/xlsx/references/report-design.md +18 -2
  60. package/src/lib/keychain-cjs.cjs +22 -1
  61. package/src/lib/rules-builder.cjs +1 -1
  62. package/src/rules/agent/AGENT.md +3 -0
  63. package/src/rules/agent-mode/MIXY.md +5 -3
  64. package/src/rules/lead/LEAD.md +3 -1
  65. package/src/runtime/agent/orchestrator/agent-runtime/cache-strategy.mjs +8 -30
  66. package/src/runtime/agent/orchestrator/agent-runtime/title-completion.mjs +5 -0
  67. package/src/runtime/agent/orchestrator/mcp/oauth-request.mjs +21 -2
  68. package/src/runtime/agent/orchestrator/providers/account-pool.mjs +3 -0
  69. package/src/runtime/agent/orchestrator/providers/admission-scheduler.mjs +4 -3
  70. package/src/runtime/agent/orchestrator/providers/anthropic-api-request.mjs +11 -1
  71. package/src/runtime/agent/orchestrator/providers/anthropic-api-transport.mjs +2 -0
  72. package/src/runtime/agent/orchestrator/providers/anthropic-message-threads.mjs +583 -0
  73. package/src/runtime/agent/orchestrator/providers/anthropic-oauth-client-version.mjs +1 -1
  74. package/src/runtime/agent/orchestrator/providers/anthropic-oauth.mjs +81 -19
  75. package/src/runtime/agent/orchestrator/providers/anthropic-sse-turn/turn-events/content-block-start.mjs +4 -0
  76. package/src/runtime/agent/orchestrator/providers/anthropic-sse-turn/turn-outcome.mjs +2 -0
  77. package/src/runtime/agent/orchestrator/providers/anthropic-sse.mjs +180 -13
  78. package/src/runtime/agent/orchestrator/providers/anthropic-turn-result.mjs +2 -0
  79. package/src/runtime/agent/orchestrator/providers/anthropic.mjs +58 -6
  80. package/src/runtime/agent/orchestrator/providers/cursor-wire.mjs +4 -2
  81. package/src/runtime/agent/orchestrator/providers/custom-provider-config.mjs +121 -9
  82. package/src/runtime/agent/orchestrator/providers/custom-provider.mjs +4 -1
  83. package/src/runtime/agent/orchestrator/providers/gemini-error-contract.mjs +6 -6
  84. package/src/runtime/agent/orchestrator/providers/gemini-model-redirect.mjs +44 -0
  85. package/src/runtime/agent/orchestrator/providers/gemini-reasoning-relay.mjs +5 -0
  86. package/src/runtime/agent/orchestrator/providers/gemini-request-body.mjs +34 -5
  87. package/src/runtime/agent/orchestrator/providers/gemini-response.mjs +10 -0
  88. package/src/runtime/agent/orchestrator/providers/gemini-schema.mjs +11 -6
  89. package/src/runtime/agent/orchestrator/providers/gemini-sdk-request.mjs +4 -100
  90. package/src/runtime/agent/orchestrator/providers/gemini-stream.mjs +3 -227
  91. package/src/runtime/agent/orchestrator/providers/gemini.mjs +226 -118
  92. package/src/runtime/agent/orchestrator/providers/lib/anthropic-request-utils.mjs +11 -1
  93. package/src/runtime/agent/orchestrator/providers/lib/claude-bridge-schema.mjs +15 -3
  94. package/src/runtime/agent/orchestrator/providers/lib/gemini-model-catalog.mjs +2 -1
  95. package/src/runtime/agent/orchestrator/providers/lib/generation-window.mjs +9 -0
  96. package/src/runtime/agent/orchestrator/providers/openai-compat-chat-send.mjs +9 -1
  97. package/src/runtime/agent/orchestrator/providers/openai-compat-chat-stream/stream-result.mjs +2 -0
  98. package/src/runtime/agent/orchestrator/providers/openai-compat-chat-stream/stream-state.mjs +5 -0
  99. package/src/runtime/agent/orchestrator/providers/openai-compat-options.mjs +3 -0
  100. package/src/runtime/agent/orchestrator/providers/openai-compat-response-normalization.mjs +2 -0
  101. package/src/runtime/agent/orchestrator/providers/openai-compat-responses.mjs +10 -1
  102. package/src/runtime/agent/orchestrator/providers/openai-direct-request.mjs +5 -28
  103. package/src/runtime/agent/orchestrator/providers/openai-http-sse-response-state/response-events.mjs +3 -0
  104. package/src/runtime/agent/orchestrator/providers/openai-http-sse-response-state/response-outcome.mjs +2 -0
  105. package/src/runtime/agent/orchestrator/providers/openai-ws-pool.mjs +50 -28
  106. package/src/runtime/agent/orchestrator/providers/openai-ws-response-state.mjs +9 -2
  107. package/src/runtime/agent/orchestrator/providers/openai-ws-stream/socket-handlers.mjs +14 -10
  108. package/src/runtime/agent/orchestrator/providers/openai-ws.mjs +1 -3
  109. package/src/runtime/agent/orchestrator/providers/openrouter-provider-routing.mjs +28 -0
  110. package/src/runtime/agent/orchestrator/providers/openrouter.mjs +4 -1
  111. package/src/runtime/agent/orchestrator/providers/registry.mjs +30 -25
  112. package/src/runtime/agent/orchestrator/providers/stream-json-pool/worker-slots/slot-spawn.mjs +8 -3
  113. package/src/runtime/agent/orchestrator/providers/vertex-ai-maas.mjs +2 -2
  114. package/src/runtime/agent/orchestrator/runtime-core/ask-user-result.mjs +66 -0
  115. package/src/runtime/agent/orchestrator/runtime-core/config-helpers.mjs +4 -5
  116. package/src/runtime/agent/orchestrator/runtime-core/goal-tool-defs.mjs +2 -2
  117. package/src/runtime/agent/orchestrator/runtime-core/provider-request-tools.mjs +15 -8
  118. package/src/runtime/agent/orchestrator/runtime-core/session-title.mjs +1 -0
  119. package/src/runtime/agent/orchestrator/runtime-core/todo-list.mjs +9 -32
  120. package/src/runtime/agent/orchestrator/runtime-core/todo-tool-defs.mjs +4 -5
  121. package/src/runtime/agent/orchestrator/session/compact/summary-route.mjs +1 -3
  122. package/src/runtime/agent/orchestrator/session/loop/compact-policy.mjs +5 -2
  123. package/src/runtime/agent/orchestrator/session/loop/no-tool-turn/todo-check.mjs +2 -17
  124. package/src/runtime/agent/orchestrator/session/loop/usage.mjs +7 -3
  125. package/src/runtime/agent/orchestrator/session/manager/ask-turn-settle.mjs +9 -2
  126. package/src/runtime/agent/orchestrator/session/manager/runtime-liveness.mjs +8 -1
  127. package/src/runtime/agent/orchestrator/session/manager/session-close.mjs +26 -13
  128. package/src/runtime/agent/orchestrator/session/manager/session-lifecycle.mjs +4 -5
  129. package/src/runtime/agent/orchestrator/session/manager/usage-metrics.mjs +16 -5
  130. package/src/runtime/agent/orchestrator/session/store/lifecycle-barriers.mjs +22 -1
  131. package/src/runtime/agent/orchestrator/session/store/live-state.mjs +4 -0
  132. package/src/runtime/agent/orchestrator/session/store.mjs +12 -6
  133. package/src/runtime/agent/orchestrator/tools/builtin/cache-layers.mjs +11 -9
  134. package/src/runtime/agent/orchestrator/tools/builtin/grep-formatting.mjs +2 -8
  135. package/src/runtime/agent/orchestrator/tools/builtin/lib/search-input-helpers.mjs +1 -9
  136. package/src/runtime/agent/orchestrator/tools/builtin/lib/shell-output-silence.mjs +64 -0
  137. package/src/runtime/agent/orchestrator/tools/builtin/lib/shell-task-output.mjs +20 -6
  138. package/src/runtime/agent/orchestrator/tools/builtin/local-search-telemetry.mjs +2 -2
  139. package/src/runtime/agent/orchestrator/tools/builtin/native-search-client.mjs +1 -1
  140. package/src/runtime/agent/orchestrator/tools/builtin/runtime-capabilities.mjs +12 -3
  141. package/src/runtime/agent/orchestrator/tools/builtin/task-tool.mjs +12 -1
  142. package/src/runtime/agent/orchestrator/tools/code-graph/ast-calls.mjs +9 -2
  143. package/src/runtime/agent/orchestrator/tools/code-graph/build.mjs +137 -7
  144. package/src/runtime/agent/orchestrator/tools/code-graph/calls-cache.mjs +43 -14
  145. package/src/runtime/agent/orchestrator/tools/code-graph/calls-sidecar-worker.mjs +33 -0
  146. package/src/runtime/agent/orchestrator/tools/code-graph/disk-cache.mjs +146 -11
  147. package/src/runtime/agent/orchestrator/tools/code-graph/dispatch.mjs +31 -24
  148. package/src/runtime/agent/orchestrator/tools/code-graph/graph-binary/spawn-jsonl.mjs +172 -25
  149. package/src/runtime/agent/orchestrator/tools/code-graph/graph-model.mjs +100 -2
  150. package/src/runtime/agent/orchestrator/tools/code-graph/main-thread-slice.mjs +18 -0
  151. package/src/runtime/agent/orchestrator/tools/code-graph/mask-pool.mjs +219 -0
  152. package/src/runtime/agent/orchestrator/tools/code-graph/mask-worker.mjs +12 -0
  153. package/src/runtime/agent/orchestrator/tools/code-graph/memory-cache.mjs +6 -1
  154. package/src/runtime/agent/orchestrator/tools/code-graph/modes/calls.mjs +9 -2
  155. package/src/runtime/agent/orchestrator/tools/code-graph/modes/dependents.mjs +5 -1
  156. package/src/runtime/agent/orchestrator/tools/code-graph/modes/structure.mjs +8 -2
  157. package/src/runtime/agent/orchestrator/tools/code-graph/modes/symbols.mjs +14 -11
  158. package/src/runtime/agent/orchestrator/tools/code-graph/search-references.mjs +41 -18
  159. package/src/runtime/agent/orchestrator/tools/code-graph/search.mjs +391 -83
  160. package/src/runtime/agent/orchestrator/tools/code-graph/symbol-index.mjs +109 -9
  161. package/src/runtime/agent/orchestrator/tools/code-graph/text-mask.mjs +73 -29
  162. package/src/runtime/agent/orchestrator/tools/code-graph-prewarm-worker.mjs +6 -5
  163. package/src/runtime/agent/orchestrator/tools/lib/native-spawn-client.mjs +7 -3
  164. package/src/runtime/channels/lib/crash-log.mjs +21 -1
  165. package/src/runtime/channels/lib/routines-db.mjs +35 -0
  166. package/src/runtime/channels/lib/schedules-db.mjs +4 -0
  167. package/src/runtime/channels/lib/webhooks-db.mjs +4 -0
  168. package/src/runtime/channels/lib/worker-main.mjs +8 -25
  169. package/src/runtime/computer-bridge/action-schema.mjs +2 -2
  170. package/src/runtime/computer-bridge/core-actions.mjs +16 -2
  171. package/src/runtime/computer-bridge/result-canonical.mjs +6 -0
  172. package/src/runtime/media/jobs.mjs +5 -0
  173. package/src/runtime/media/store.mjs +146 -3
  174. package/src/runtime/memory/tool-defs.mjs +1 -1
  175. package/src/runtime/office/core/office-write-ledger.mjs +2 -2
  176. package/src/runtime/shared/agent-reap-state.mjs +19 -0
  177. package/src/runtime/shared/desktop-notice-host.d.mts +13 -0
  178. package/src/runtime/shared/desktop-notice-host.mjs +87 -0
  179. package/src/runtime/shared/file-lock.mjs +2 -2
  180. package/src/runtime/shared/heap-cap.mjs +9 -11
  181. package/src/runtime/shared/llm/http-agent.mjs +11 -1
  182. package/src/runtime/shared/llm/system-proxy.mjs +100 -25
  183. package/src/runtime/shared/llm/usage-accounting.mjs +121 -19
  184. package/src/runtime/shared/llm/usage-context.mjs +3 -3
  185. package/src/runtime/shared/llm/usage-ledger-quota.mjs +2 -0
  186. package/src/runtime/shared/llm/usage-ledger-requests.mjs +190 -0
  187. package/src/runtime/shared/llm/usage-ledger-rollup.mjs +7 -3
  188. package/src/runtime/shared/llm/usage-ledger-worker.mjs +2 -0
  189. package/src/runtime/shared/llm/usage-ledger.mjs +69 -1
  190. package/src/runtime/shared/llm/usage-request-status.mjs +48 -0
  191. package/src/runtime/shared/provider-accounts.mjs +22 -0
  192. package/src/runtime/shared/provider-auth-binding.mjs +2 -2
  193. package/src/runtime/shared/resource-admission.mjs +2 -2
  194. package/src/runtime/shared/root-async-context.mjs +51 -0
  195. package/src/runtime/shared/run-child.mjs +18 -1
  196. package/src/runtime/shared/slow-span.mjs +2 -2
  197. package/src/runtime/shared/tool-execution-owner.mjs +2 -2
  198. package/src/runtime/shared/tool-result-summary.mjs +1 -1
  199. package/src/runtime/shared/tool-work-units.mjs +2 -0
  200. package/src/runtime/shared/turn-snapshot-store.mjs +75 -18
  201. package/src/runtime/shared/turn-worktree-snapshot.mjs +38 -8
  202. package/src/runtime/shared/user-cwd.mjs +2 -2
  203. package/src/runtime/shared/worker-requests.mjs +3 -1
  204. package/src/runtime/shared/worktrees/lock.mjs +2 -2
  205. package/src/runtime/visualize-bridge/client.mjs +26 -4
  206. package/src/runtime/visualize-bridge/tool-defs.mjs +46 -5
  207. package/src/session-runtime/agent-home-delivery.mjs +76 -8
  208. package/src/session-runtime/agent-task-sessions.mjs +63 -22
  209. package/src/session-runtime/boot/apis.mjs +16 -0
  210. package/src/session-runtime/boot/begin.mjs +2 -0
  211. package/src/session-runtime/boot/tools.mjs +8 -2
  212. package/src/session-runtime/internal-tool-executor/ask-user-tool.mjs +3 -58
  213. package/src/session-runtime/media-api.mjs +15 -0
  214. package/src/session-runtime/model-route/route-persist.mjs +12 -4
  215. package/src/session-runtime/provider-models/row-hydration.mjs +2 -0
  216. package/src/session-runtime/provider-readiness.mjs +10 -2
  217. package/src/session-runtime/routine-owner.mjs +9 -3
  218. package/src/session-runtime/runtime-core.mjs +35 -8
  219. package/src/session-runtime/runtime-review-api.mjs +3 -1
  220. package/src/session-runtime/services/agent-task-status.mjs +29 -1
  221. package/src/session-runtime/services/agent-tool/close-flow.mjs +6 -1
  222. package/src/session-runtime/services/agent-tool/execute-resume.mjs +94 -0
  223. package/src/session-runtime/services/agent-tool/execute-send.mjs +77 -27
  224. package/src/session-runtime/services/agent-tool/execute-spawn.mjs +28 -7
  225. package/src/session-runtime/services/agent-tool/execute.mjs +6 -1
  226. package/src/session-runtime/services/agent-tool/helpers.mjs +11 -0
  227. package/src/session-runtime/services/agent-tool/job-views/job-render.mjs +13 -10
  228. package/src/session-runtime/services/agent-tool/job-views/session-progress.mjs +3 -0
  229. package/src/session-runtime/services/agent-tool/render.mjs +8 -1
  230. package/src/session-runtime/services/agent-tool/send-flow.mjs +1 -0
  231. package/src/session-runtime/services/agent-tool/spawn-flow/spawn-prep.mjs +6 -0
  232. package/src/session-runtime/services/agent-tool/spawn-flow/spawn-run.mjs +2 -1
  233. package/src/session-runtime/services/agent-tool/tag-registry/reaped-sessions.mjs +82 -0
  234. package/src/session-runtime/services/agent-tool/tag-registry/record-facts-worker.mjs +36 -0
  235. package/src/session-runtime/services/agent-tool/tag-registry.mjs +4 -0
  236. package/src/session-runtime/services/agent-tool/terminal-reaper/row-helpers.mjs +29 -0
  237. package/src/session-runtime/services/agent-tool/terminal-reaper/scan-admission.mjs +11 -3
  238. package/src/session-runtime/services/agent-tool/terminal-reaper/stale-rows.mjs +19 -8
  239. package/src/session-runtime/services/agent-tool/terminal-reaper.mjs +5 -2
  240. package/src/session-runtime/services/agent-tool/worker-index.mjs +1 -0
  241. package/src/session-runtime/services/channel-admin.mjs +44 -3
  242. package/src/session-runtime/services/daemon-crash-capture/daemon-exit-record.mjs +2 -2
  243. package/src/session-runtime/services/plugin-admin.mjs +46 -7
  244. package/src/session-runtime/services/provider-admin.mjs +5 -0
  245. package/src/session-runtime/session-create.mjs +5 -0
  246. package/src/session-runtime/setup-tool/agent-settings-actions.mjs +1 -1
  247. package/src/session-runtime/setup-tool/executor.mjs +1 -1
  248. package/src/session-runtime/setup-tool/extended-actions.mjs +1 -1
  249. package/src/session-runtime/setup-tool/settings-contract.mjs +4 -2
  250. package/src/session-runtime/usage-stats-api.mjs +120 -1
  251. package/src/session-runtime/workflow-agents-api/agent-connectors.mjs +36 -29
  252. package/src/session-runtime/workflow-agents-api/agent-editor.mjs +183 -23
  253. package/src/session-runtime/workspace/admission-gate.mjs +25 -7
  254. package/src/session-runtime/workspace/transitions.mjs +4 -0
  255. package/src/standalone/daemon-log.mjs +28 -4
  256. package/src/standalone/daemon-owner.mjs +41 -7
  257. package/src/standalone/daemon-work-gate.mjs +139 -0
  258. package/src/standalone/daemon.mjs +80 -11
  259. package/src/standalone/desktop-service-registry.mjs +11 -4
  260. package/src/standalone/session-client.mjs +57 -5
  261. package/src/standalone/session-protocol.mjs +6 -0
  262. package/src/standalone/session-service/agent-tree/agent-cancel.mjs +17 -1
  263. package/src/standalone/session-service/agent-tree.mjs +7 -0
  264. package/src/standalone/session-service/retention/busy.mjs +3 -2
  265. package/src/standalone/session-service/session-calls/session-turns.mjs +12 -5
  266. package/src/standalone/session-transport/call-route.mjs +10 -4
  267. package/src/standalone/session-transport/routes.mjs +21 -0
  268. package/src/standalone/session-transport/stack.mjs +3 -1
  269. package/src/standalone/session-transport.mjs +5 -0
  270. package/src/tui/app/model-picker/model-footer.mjs +1 -1
  271. package/src/tui/app/model-picker/provider-models-list.mjs +7 -3
  272. package/src/tui/app/model-picker/route-selection/fast-parameters.mjs +23 -3
  273. package/src/tui/app/model-picker/route-selection.mjs +1 -0
  274. package/src/tui/dist/index.mjs +536 -484
  275. package/src/tui/session/session-api/integrations.mjs +3 -0
  276. package/src/tui/session/session-api/media.mjs +5 -0
  277. package/src/tui/session/tool-approval-settle.mjs +24 -4
  278. package/src/tui/session/tool-approval.mjs +35 -10
  279. package/src/tui/session/turn.mjs +13 -0
  280. package/src/runtime/agent/orchestrator/providers/gemini-sdk-stream/cancellation.mjs +0 -71
  281. package/src/runtime/agent/orchestrator/providers/gemini-sdk-stream/reader.mjs +0 -108
@@ -56,6 +56,7 @@ import { applyAnthropicErrorContract } from './anthropic-error-contract.mjs';
56
56
  import { adjustMaxTokensForContextOverflow } from './anthropic-max-tokens-recovery.mjs';
57
57
  import { createAnthropicOverloadTracker, takeAnthropicModelFallback } from './anthropic-overload-fallback.mjs';
58
58
  import { createAnthropicOAuthRecovery } from './anthropic-oauth-recovery.mjs';
59
+ import { THREAD_MESSAGE_STOP_WAIT_MS, planAnthropicThread, threadAccountKey } from './anthropic-message-threads.mjs';
59
60
  import { createMidState, createMidstreamRecovery } from './anthropic-oauth-midstream.mjs';
60
61
  import { assertAnthropicStreamNotEmpty } from './anthropic-midstream-recovery.mjs';
61
62
  import { applyAnthropicEffortToBody, shouldIncludeEffortBeta } from './anthropic-effort.mjs';
@@ -553,18 +554,39 @@ export class AnthropicOAuthProvider {
553
554
  // Default when the caller doesn't pin a model: newest high-tier chat
554
555
  // model from the live catalog (one warmup round-trip if cache is cold).
555
556
  const useModel = model || (await ensureLatestAnthropicModel(this));
556
- const body = buildRequestBody(messages, useModel, tools, sendOpts);
557
- body.messages = await prepareAnthropicImages(body.messages, { signal: externalSignal });
557
+ const fullBody = buildRequestBody(messages, useModel, tools, sendOpts);
558
+ fullBody.messages = await prepareAnthropicImages(fullBody.messages, { signal: externalSignal });
558
559
  // Diagnostic: per-block prefix hashes of exactly what is sent.
559
- recordAnthropicPrefix(anthropicPrefixSessionId(opts), anthropicPrefixFingerprint(body));
560
- if (body.speed === 'fast') {
560
+ recordAnthropicPrefix(anthropicPrefixSessionId(opts), anthropicPrefixFingerprint(fullBody));
561
+ if (fullBody.speed === 'fast') {
561
562
  this.fastModeBetaHeaderLatched = true;
562
563
  }
563
564
  // advanced-tool-use-2025-11-20 beta is only needed when this request
564
565
  // actually carries deferred (defer_loading) tools — gate the header on
565
566
  // that instead of sending it unconditionally on every request.
566
- const hasDeferredTools = Array.isArray(body.tools) && body.tools.some((t) => t && t.defer_loading === true);
567
- const knownToolNames = requestToolNames(body);
567
+ const hasDeferredTools = Array.isArray(fullBody.tools) && fullBody.tools.some((t) => t && t.defer_loading === true);
568
+ const knownToolNames = requestToolNames(fullBody);
569
+ // Message thread (anthropic-message-threads.mjs): the same request as a
570
+ // create, a delta continue, or unchanged. Its beta set is the full body's,
571
+ // so a continue keeps the create's fixed header.
572
+ const thread = planAnthropicThread({
573
+ provider: 'anthropic-oauth',
574
+ config: this.config,
575
+ opts,
576
+ body: fullBody,
577
+ betas: buildOAuthBetaHeaders(fullBody, {
578
+ fastMode: this.fastModeBetaHeaderLatched,
579
+ toolSearch: hasDeferredTools,
580
+ model: useModel,
581
+ opts,
582
+ }).split(','),
583
+ account: threadAccountKey(
584
+ 'anthropic-oauth',
585
+ auth.creds?.identity?.id || auth.creds?.path || boundProviderAuthPath('anthropic-oauth') || ''
586
+ ),
587
+ requestMessages: messages,
588
+ });
589
+ const body = thread.body;
568
590
  const sessionId = opts.sessionId || null;
569
591
  const iteration = Number.isFinite(Number(opts.iteration)) ? Number(opts.iteration) : null;
570
592
  // Option A: no absolute wall-clock cap on streaming generation. A stream
@@ -589,6 +611,7 @@ export class AnthropicOAuthProvider {
589
611
  auth,
590
612
  totalSignal,
591
613
  hasDeferredTools,
614
+ thread,
592
615
  onStageChange,
593
616
  onTextReset,
594
617
  });
@@ -596,6 +619,8 @@ export class AnthropicOAuthProvider {
596
619
  let maxTokensAdjusted = false;
597
620
  try {
598
621
  for (let attemptIndex = 0; attemptIndex <= ANTHROPIC_MAX_MIDSTREAM_RETRIES; attemptIndex++) {
622
+ // A dropped continue is resent once as-is; after that it replays.
623
+ if (attemptIndex >= 2) thread.toCreate();
599
624
  const opened = await this._openResponse({
600
625
  requestWithRetry,
601
626
  cleanupCancelHandler,
@@ -607,6 +632,8 @@ export class AnthropicOAuthProvider {
607
632
  maxTokensAdjusted = opened.maxTokensAdjusted;
608
633
  if (!response.ok) {
609
634
  cleanupCancelHandler(cancelHandler);
635
+ // This request is over; any resend plans the thread afresh.
636
+ thread.settle();
610
637
  return await this._resendAfterRejection({
611
638
  response,
612
639
  rejectedAuthBody,
@@ -616,6 +643,7 @@ export class AnthropicOAuthProvider {
616
643
  tools,
617
644
  opts,
618
645
  onStageChange,
646
+ thread,
619
647
  });
620
648
  }
621
649
 
@@ -638,13 +666,26 @@ export class AnthropicOAuthProvider {
638
666
  midState,
639
667
  onTextDelta,
640
668
  knownToolNames,
641
- { relayProgressUpdates: body.thinking?.display === 'updates', onStreamItemStart }
669
+ {
670
+ relayProgressUpdates: body.thinking?.display === 'updates',
671
+ onStreamItemStart,
672
+ awaitMessageStopMs: thread.mode === 'none' ? 0 : THREAD_MESSAGE_STOP_WAIT_MS,
673
+ releaseSignal: externalSignal,
674
+ }
642
675
  );
643
- try {
644
- controller?.abort?.('Anthropic SSE complete');
645
- } catch {
646
- // the stream is complete; the controller may already be aborted
647
- }
676
+ thread.commit(result);
677
+ const finishStream = () => {
678
+ try {
679
+ controller?.abort?.('Anthropic SSE complete');
680
+ } catch {
681
+ // the stream is complete; the controller may already be aborted
682
+ }
683
+ };
684
+ // A thread's message_stop may still be read in the background: the
685
+ // body stays open until that read settles.
686
+ const receiptTail = result?.threadReceipt?.pending;
687
+ if (receiptTail) void receiptTail.then(finishStream);
688
+ else finishStream();
648
689
  this._settleStreamedTurn({
649
690
  result,
650
691
  midState,
@@ -667,6 +708,7 @@ export class AnthropicOAuthProvider {
667
708
  }
668
709
  throw midstream.exhaustedError();
669
710
  } catch (err) {
711
+ thread.settle();
670
712
  const fallbackOpts = takeAnthropicModelFallback(err, {
671
713
  tag: 'anthropic-oauth',
672
714
  model: useModel,
@@ -676,6 +718,7 @@ export class AnthropicOAuthProvider {
676
718
  });
677
719
  return await this.send(messages, err.fallbackModel, tools, fallbackOpts);
678
720
  } finally {
721
+ thread.settle();
679
722
  totalTimeout.cleanup();
680
723
  }
681
724
  }
@@ -691,6 +734,7 @@ export class AnthropicOAuthProvider {
691
734
  auth,
692
735
  totalSignal,
693
736
  hasDeferredTools,
737
+ thread = null,
694
738
  onStageChange,
695
739
  onTextReset,
696
740
  }) {
@@ -709,12 +753,14 @@ export class AnthropicOAuthProvider {
709
753
  totalSignal,
710
754
  overload,
711
755
  betaHeadersFor: (requestBody) =>
712
- buildOAuthBetaHeaders(requestBody, {
713
- fastMode: this.fastModeBetaHeaderLatched,
714
- toolSearch: hasDeferredTools,
715
- model: useModel,
716
- opts,
717
- }),
756
+ thread && thread.mode !== 'none'
757
+ ? thread.betas.join(',')
758
+ : buildOAuthBetaHeaders(requestBody, {
759
+ fastMode: this.fastModeBetaHeaderLatched,
760
+ toolSearch: hasDeferredTools,
761
+ model: useModel,
762
+ opts,
763
+ }),
718
764
  onStageChange,
719
765
  });
720
766
  const recovery = createAnthropicOAuthRecovery({
@@ -793,8 +839,24 @@ export class AnthropicOAuthProvider {
793
839
 
794
840
  // A non-OK initial response: quota errors surface as-is; a CLI-version
795
841
  // floor or an unknown/retired model replays the turn ONCE through send().
796
- async _resendAfterRejection({ response, rejectedAuthBody, messages, model, useModel, tools, opts, onStageChange }) {
842
+ async _resendAfterRejection({
843
+ response,
844
+ rejectedAuthBody,
845
+ messages,
846
+ model,
847
+ useModel,
848
+ tools,
849
+ opts,
850
+ onStageChange,
851
+ thread = null,
852
+ }) {
797
853
  const text = rejectedAuthBody ?? (await response.text().catch(() => ''));
854
+ // A refused thread request (nothing ran): replay as a create or send
855
+ // unthreaded, per the thread error.
856
+ const threadDirective = thread?.classify(response.status, text);
857
+ if (threadDirective) {
858
+ return this._sendOnce(messages, useModel, tools, { ...opts, _anthropicThread: threadDirective });
859
+ }
798
860
  const scrubbedText = this.scrubTokens(text);
799
861
  const safeText = scrubbedText.slice(0, 200);
800
862
  process.stderr.write(`[anthropic-oauth] API error ${response.status}: ${safeText}\n`);
@@ -63,6 +63,10 @@ export function createContentBlockStart({ turn, blocks, state, progress, seedTex
63
63
  // The call opened; its input still streams before onToolCall.
64
64
  reportStreamItemStart(onStreamItemStart, { kind: 'tool' });
65
65
  }
66
+ // Any output block opening starts the generation (generation-window.mjs),
67
+ // a thinking block above all: under omitted or "updates" thinking display
68
+ // it streams nothing until its signature at the end.
69
+ if (block?.type && !fallback) turn.firstOutputAt ||= Date.now();
66
70
  if (block?.type === 'text') {
67
71
  // Seed the ordered text slot. An empty block still needs its slot (it
68
72
  // separates thinking runs); a non-empty opening `text` is model output
@@ -7,6 +7,7 @@
7
7
  */
8
8
  import { stampStreamOutcome, STREAM_TRANSPORTS } from '../lib/stream-outcome.mjs';
9
9
  import { createProviderReplay } from '../lib/provider-replay.mjs';
10
+ import { generationWindow } from '../lib/generation-window.mjs';
10
11
  import { OAUTH_STREAM_LABELS } from '../lib/anthropic-stream-labels.mjs';
11
12
  import { anthropicFallbackProviderMetadata } from '../anthropic-server-fallback.mjs';
12
13
 
@@ -155,6 +156,7 @@ export function createTurnOutcome({ turn, blocks, state, labels = OAUTH_STREAM_L
155
156
  assistantBlocks,
156
157
  providerReplay: createProviderReplay('anthropic', assistantBlocks),
157
158
  providerMetadata: anthropicFallbackProviderMetadata(turn.fallbackEvents),
159
+ ...generationWindow(turn.firstOutputAt),
158
160
  };
159
161
  };
160
162
 
@@ -131,8 +131,9 @@ export function _anthropicSseError(event, labels) {
131
131
  // must stop reading (a terminal frame). A malformed record is skipped exactly
132
132
  // like the former per-event JSON.parse throw — and, unlike a whole-batch
133
133
  // rejection, the well-formed records beside it still run.
134
- function applyAnthropicSseEvent(framedEvent, { turn, watchdogs, state, progress, labels }) {
134
+ function applyAnthropicSseEvent(framedEvent, { turn, watchdogs, state, progress, labels, receipt }) {
135
135
  const event = framedEvent.value;
136
+ noteThreadReceipt(receipt, event);
136
137
  try {
137
138
  if (framedEvent.name === 'error' || event?.type === 'error' || event?.error) {
138
139
  throw _anthropicSseError(event, labels);
@@ -192,7 +193,21 @@ export async function parseSSEStream(
192
193
  state,
193
194
  onTextDelta,
194
195
  knownToolNames,
195
- { relayProgressUpdates = false, labels = OAUTH_STREAM_LABELS, onStreamItemStart = null } = {}
196
+ {
197
+ relayProgressUpdates = false,
198
+ labels = OAUTH_STREAM_LABELS,
199
+ onStreamItemStart = null,
200
+ // A message-thread request: after an early terminal frame (tool_use
201
+ // message_delta) the stream is read on in the background, up to this
202
+ // long, for the message_stop that confirms the API stored the response
203
+ // (`threadReceipt.pending`). The caller must not abort the response body
204
+ // before that settles.
205
+ awaitMessageStopMs = 0,
206
+ // The send's own request/session signal: aborting it (turn abort, session
207
+ // close) ends that background read at once, as does
208
+ // `threadReceipt.cancel()`.
209
+ releaseSignal = null,
210
+ } = {}
196
211
  ) {
197
212
  // Anthropic/Claude parity: every received SSE byte proves transport
198
213
  // activity, including comment and named ping keepalives. Content kinds
@@ -233,6 +248,11 @@ export async function parseSSEStream(
233
248
  const onAbort = () => cancelReaderOnAbort(reader);
234
249
  let buffer = '';
235
250
  let currentEvent = '';
251
+ // What a message thread needs from the stream: the response id, whether
252
+ // message_stop arrived (and when), and every content block exactly as the
253
+ // API stored it, before any leak-guard rewriting.
254
+ const receipt = createThreadReceipt();
255
+ let handedOff = false;
236
256
  retainProviderSseStream(streamKey);
237
257
  try {
238
258
  // Reader ownership begins at getReader() above, so even a signal that
@@ -268,11 +288,29 @@ export async function parseSSEStream(
268
288
  const framed = frameProviderSseChunk(region, { currentEvent, streamKey });
269
289
  const framedChunk = typeof framed?.then === 'function' ? await framed : framed;
270
290
  currentEvent = framedChunk.currentEvent;
271
- if (applyAnthropicSseChunk(framedChunk.events, { turn, watchdogs, state, progress, labels })) break;
291
+ if (applyAnthropicSseChunk(framedChunk.events, { turn, watchdogs, state, progress, labels, receipt })) break;
272
292
  }
273
293
  turn.flushLeak();
274
294
  if (state?.sawMessageStart && !state?.sawCompleted) throw turn.truncatedError();
275
- return turn.result();
295
+ const result = turn.result();
296
+ // An early terminal frame (tool_use message_delta) ends the turn now; the
297
+ // message_stop that confirms storage is awaited on the side, never by
298
+ // the turn.
299
+ if (awaitMessageStopMs > 0 && !receipt.stoppedAt) {
300
+ receipt.terminalAt = Date.now();
301
+ receipt.pending = drainForMessageStop({
302
+ reader,
303
+ decoder,
304
+ rest: buffer,
305
+ receipt,
306
+ waitMs: awaitMessageStopMs,
307
+ labels,
308
+ signals: [signal, releaseSignal],
309
+ });
310
+ handedOff = true;
311
+ }
312
+ Object.defineProperty(result, 'threadReceipt', { value: receipt, enumerable: false });
313
+ return result;
276
314
  } finally {
277
315
  watchdogs.dispose();
278
316
  try {
@@ -283,13 +321,16 @@ export async function parseSSEStream(
283
321
  if (signal) signal.removeEventListener('abort', onAbort);
284
322
  // message_stop deliberately exits before EOF because Anthropic may keep
285
323
  // sending pings. Cancel the reader so the successful response body and
286
- // underlying keep-alive connection are not stranded.
287
- try {
288
- await reader.cancel('Anthropic SSE complete');
289
- } catch {
290
- // the reader may already be closed after the stream ended
324
+ // underlying keep-alive connection are not stranded. A reader handed to
325
+ // the message_stop drain is cancelled there.
326
+ if (!handedOff) {
327
+ try {
328
+ await reader.cancel('Anthropic SSE complete');
329
+ } catch {
330
+ // the reader may already be closed after the stream ended
331
+ }
332
+ releaseReaderLock(reader, labels);
291
333
  }
292
- releaseReaderLock(reader, labels);
293
334
  }
294
335
  }
295
336
 
@@ -324,14 +365,140 @@ function readFailureError(err, { watchdogs, signal, state, turn, abortedError })
324
365
 
325
366
  // Fold one network chunk's framed records into the turn, in order. Returns true
326
367
  // once a terminal record ends the stream; a framing error skips its record.
368
+ // Records after the terminal one are only scanned for message_stop.
327
369
  function applyAnthropicSseChunk(events, context) {
328
- for (const framedEvent of events) {
329
- if (framedEvent.error) continue;
330
- if (applyAnthropicSseEvent(framedEvent, context)) return true;
370
+ for (let i = 0; i < events.length; i++) {
371
+ if (events[i].error) continue;
372
+ if (applyAnthropicSseEvent(events[i], context)) {
373
+ for (const rest of events.slice(i + 1)) if (!rest.error) noteThreadReceipt(context.receipt, rest.value);
374
+ return true;
375
+ }
331
376
  }
332
377
  return false;
333
378
  }
334
379
 
380
+ // The response as the API stores it, block by block. `opaque` marks anything
381
+ // the recording cannot reproduce exactly (an unknown delta, an unparsable
382
+ // tool input, a block never closed); such a response is never continued.
383
+ function createThreadReceipt() {
384
+ const open = new Map();
385
+ const receipt = { id: '', stoppedAt: 0, terminalAt: 0, blocks: [], opaque: false, pending: null };
386
+ Object.defineProperty(receipt, 'open', { value: open, enumerable: false });
387
+ return receipt;
388
+ }
389
+
390
+ const APPEND_DELTA_FIELD = {
391
+ text_delta: ['text', 'text'],
392
+ thinking_delta: ['thinking', 'thinking'],
393
+ signature_delta: ['signature', 'signature'],
394
+ };
395
+
396
+ function noteThreadReceipt(receipt, event) {
397
+ if (!receipt || !event || typeof event !== 'object') return;
398
+ switch (event.type) {
399
+ case 'message_start':
400
+ if (typeof event.message?.id === 'string') receipt.id = event.message.id;
401
+ return;
402
+ case 'message_stop':
403
+ receipt.stoppedAt ||= Date.now();
404
+ if (receipt.open.size) receipt.opaque = true;
405
+ return;
406
+ case 'content_block_start': {
407
+ const block = event.content_block;
408
+ if (!block || typeof block !== 'object') receipt.opaque = true;
409
+ else receipt.open.set(event.index, { block: structuredClone(block), json: '' });
410
+ return;
411
+ }
412
+ case 'content_block_delta': {
413
+ const entry = receipt.open.get(event.index);
414
+ const delta = event.delta || {};
415
+ const append = APPEND_DELTA_FIELD[delta.type];
416
+ if (!entry) receipt.opaque = true;
417
+ else if (append) entry.block[append[0]] = `${entry.block[append[0]] ?? ''}${delta[append[1]] ?? ''}`;
418
+ else if (delta.type === 'input_json_delta') entry.json += String(delta.partial_json ?? '');
419
+ else if (delta.type === 'citations_delta') (entry.block.citations ||= []).push(delta.citation);
420
+ else receipt.opaque = true;
421
+ return;
422
+ }
423
+ case 'content_block_stop': {
424
+ const entry = receipt.open.get(event.index);
425
+ if (!entry) {
426
+ receipt.opaque = true;
427
+ return;
428
+ }
429
+ receipt.open.delete(event.index);
430
+ if (entry.json) {
431
+ try {
432
+ entry.block.input = JSON.parse(entry.json);
433
+ } catch {
434
+ receipt.opaque = true;
435
+ }
436
+ }
437
+ receipt.blocks[event.index] = entry.block;
438
+ return;
439
+ }
440
+ default:
441
+ }
442
+ }
443
+
444
+ const MESSAGE_STOP_FRAME = /(?:^|\n)event:\s*message_stop\b|"type"\s*:\s*"message_stop"/;
445
+
446
+ // Read a handed-over stream for at most `waitMs` until its message_stop frame,
447
+ // then cancel it. Only the storage confirmation depends on it: a late or
448
+ // missing frame leaves the response unconfirmed. An abort of any of
449
+ // `signals`, or `receipt.cancel()`, ends it at once. Never rejects.
450
+ async function drainForMessageStop({ reader, decoder, rest, receipt, waitMs, labels, signals = [] }) {
451
+ const deadline = Date.now() + waitMs;
452
+ let tail = rest || '';
453
+ let timer = null;
454
+ let cancelled = false;
455
+ let wake = null;
456
+ const cancel = () => {
457
+ cancelled = true;
458
+ wake?.(null);
459
+ };
460
+ Object.defineProperty(receipt, 'cancel', { value: cancel, enumerable: false, configurable: true });
461
+ const watched = signals.filter(Boolean);
462
+ for (const s of watched) {
463
+ if (s.aborted) cancelled = true;
464
+ else s.addEventListener('abort', cancel, { once: true });
465
+ }
466
+ try {
467
+ while (!receipt.stoppedAt && !cancelled) {
468
+ if (MESSAGE_STOP_FRAME.test(tail)) {
469
+ receipt.stoppedAt = Date.now();
470
+ break;
471
+ }
472
+ const remaining = deadline - Date.now();
473
+ if (remaining <= 0) break;
474
+ const read = reader.read();
475
+ read.catch(() => {});
476
+ const chunk = await Promise.race([
477
+ read,
478
+ new Promise((resolve) => {
479
+ wake = resolve;
480
+ timer = setTimeout(() => resolve(null), remaining);
481
+ }),
482
+ ]);
483
+ clearTimeout(timer);
484
+ wake = null;
485
+ if (!chunk || chunk.done || cancelled) break;
486
+ tail = `${tail}${decoder.decode(chunk.value, { stream: true })}`.slice(-4096);
487
+ }
488
+ } catch {
489
+ // the turn already completed; only the storage confirmation is lost
490
+ } finally {
491
+ clearTimeout(timer);
492
+ for (const s of watched) s.removeEventListener('abort', cancel);
493
+ try {
494
+ await reader.cancel('Anthropic SSE complete');
495
+ } catch {
496
+ // the reader may already be closed
497
+ }
498
+ releaseReaderLock(reader, labels);
499
+ }
500
+ }
501
+
335
502
  function releaseReaderLock(reader, labels) {
336
503
  try {
337
504
  reader.releaseLock();
@@ -83,6 +83,8 @@ export function buildAnthropicTurnResult(parseResult, { provider, useModel, opts
83
83
  ...(parseResult.providerMetadata && typeof parseResult.providerMetadata === 'object'
84
84
  ? { providerMetadata: parseResult.providerMetadata }
85
85
  : {}),
86
+ // The answering stream's own generation window (generation-window.mjs).
87
+ ...(parseResult.generationMs != null ? { generationMs: parseResult.generationMs } : {}),
86
88
  usage: {
87
89
  inputTokens: input,
88
90
  outputTokens: output,
@@ -28,6 +28,7 @@ import {
28
28
  import { enrichModels } from './model-catalog.mjs';
29
29
  import { sanitizeModelList } from './model-list-sanitize.mjs';
30
30
  import { getLlmDispatcher } from '../../../shared/llm/http-agent.mjs';
31
+ import { THREAD_MESSAGE_STOP_WAIT_MS, planAnthropicThread, threadAccountKey } from './anthropic-message-threads.mjs';
31
32
 
32
33
  import {
33
34
  loadAnthropic,
@@ -40,6 +41,13 @@ import { streamCallbacks } from './lib/send-callbacks.mjs';
40
41
  import { preparePdfTextForProvider } from './media-normalization.mjs';
41
42
  export { _test, _toAnthropicMessagesForTest } from './anthropic-messages.mjs';
42
43
 
44
+ // The beta names a request actually sends: its anthropic-beta header (which a
45
+ // caller's requestHeaders may have replaced), else the selected names.
46
+ function sentBetas(requestHeaders, betas) {
47
+ const header = requestHeaders?.['anthropic-beta'];
48
+ return typeof header === 'string' ? header.split(',').filter(Boolean) : betas || [];
49
+ }
50
+
43
51
  // One stream attempt's controller, aborted with the caller's reason when the
44
52
  // send's signal aborts. An already-aborted send fails before the attempt.
45
53
  function linkAttemptAbort(totalSignal) {
@@ -186,6 +194,7 @@ export class AnthropicProvider {
186
194
  fastModeLatched: this.fastModeBetaHeaderLatched,
187
195
  modelInfo: isCustomProviderName(this.name) ? this.getCachedModelInfo?.(model) || null : null,
188
196
  capabilityModel: this._capabilityModel(model),
197
+ customConfig: this.customConfig ?? null,
189
198
  });
190
199
  return sendAnthropicCacheWarm({
191
200
  providerName: this.name,
@@ -218,7 +227,7 @@ export class AnthropicProvider {
218
227
  // Wire request (system/cache layout, tools, effort, fast mode, images)
219
228
  // and the per-request beta headers derived from that final body —
220
229
  // anthropic-api-request.mjs. Fast mode latches on the provider instance.
221
- const { params, requestHeaders, knownToolNames, fastModeLatched } = await buildAnthropicApiRequest({
230
+ const built = await buildAnthropicApiRequest({
222
231
  name: this.name,
223
232
  config: this.config,
224
233
  messages,
@@ -229,8 +238,27 @@ export class AnthropicProvider {
229
238
  // Custom providers carry per-model reasoning settings on their own rows.
230
239
  modelInfo: isCustomProviderName(this.name) ? this.getCachedModelInfo?.(useModel) || null : null,
231
240
  capabilityModel: this._capabilityModel(useModel),
241
+ customConfig: this.customConfig ?? null,
232
242
  });
243
+ const { knownToolNames, fastModeLatched } = built;
233
244
  this.fastModeBetaHeaderLatched = fastModeLatched;
245
+ // Message thread (anthropic-message-threads.mjs): the same request as a
246
+ // create, a delta continue, or unchanged. The thread belongs to this key.
247
+ this._currentClient();
248
+ const thread = planAnthropicThread({
249
+ provider: this.name,
250
+ config: this.config,
251
+ opts,
252
+ body: built.params,
253
+ betas: sentBetas(built.requestHeaders, built.betas),
254
+ account: threadAccountKey(this.name, this.config?.baseURL || '', this.apiKey),
255
+ requestMessages: messages,
256
+ });
257
+ const params = thread.body;
258
+ const requestHeaders =
259
+ thread.mode === 'none'
260
+ ? built.requestHeaders
261
+ : { ...(built.requestHeaders || {}), 'anthropic-beta': thread.betas.join(',') };
234
262
 
235
263
  const { onStageChange, onStreamDelta, onToolCall, onTextDelta, onTextReset, onStreamItemStart } =
236
264
  streamCallbacks(opts);
@@ -258,6 +286,8 @@ export class AnthropicProvider {
258
286
  let streamingFallbackTried = false;
259
287
  try {
260
288
  for (let attemptIndex = 0; attemptIndex <= ANTHROPIC_MAX_MIDSTREAM_RETRIES; attemptIndex++) {
289
+ // A dropped continue is resent once as-is; after that it replays.
290
+ if (attemptIndex >= 2) thread.toCreate();
261
291
  const { streamController, cancelHandler } = linkAttemptAbort(totalSignal);
262
292
 
263
293
  const midState = createAnthropicMidState(attemptIndex);
@@ -295,13 +325,23 @@ export class AnthropicProvider {
295
325
  relayProgressUpdates: params.thinking?.display === 'updates',
296
326
  labels: { display: 'Anthropic', tag: this.name },
297
327
  onStreamItemStart,
328
+ awaitMessageStopMs: thread.mode === 'none' ? 0 : THREAD_MESSAGE_STOP_WAIT_MS,
329
+ releaseSignal: externalSignal,
298
330
  }
299
331
  );
300
- try {
301
- streamController.abort?.('Anthropic SSE complete');
302
- } catch {
303
- // the stream is complete; the controller may already be aborted
304
- }
332
+ thread.commit(parseResult);
333
+ const finishStream = () => {
334
+ try {
335
+ streamController.abort?.('Anthropic SSE complete');
336
+ } catch {
337
+ // the stream is complete; the controller may already be aborted
338
+ }
339
+ };
340
+ // A thread's message_stop may still be read in the background: the
341
+ // body stays open until that read settles.
342
+ const receiptTail = parseResult.threadReceipt?.pending;
343
+ if (receiptTail) void receiptTail.then(finishStream);
344
+ else finishStream();
305
345
 
306
346
  firstByte.cleanup();
307
347
  assertAnthropicStreamNotEmpty(midState, parseResult, 'Anthropic');
@@ -310,6 +350,16 @@ export class AnthropicProvider {
310
350
  } catch (err) {
311
351
  // The fallback trigger is handled around the whole send below.
312
352
  if (err instanceof AnthropicFallbackTriggeredError) throw err;
353
+ // A refused thread request (nothing ran): replay as a create or
354
+ // send unthreaded, per the thread error.
355
+ const threadDirective =
356
+ err?.initialResponseError === true
357
+ ? thread.classify(err.status, err.responseBody ?? err.error ?? err.message)
358
+ : null;
359
+ if (threadDirective) {
360
+ thread.settle();
361
+ return await this._sendOnce(messages, model, tools, { ...opts, _anthropicThread: threadDirective });
362
+ }
313
363
  // Gateways that 404 the streaming endpoint but serve non-streaming:
314
364
  // one non-streaming replay (nothing was streamed yet).
315
365
  if (err?.initialResponseError === true && Number(err.status) === 404 && !streamingFallbackTried) {
@@ -335,9 +385,11 @@ export class AnthropicProvider {
335
385
  }
336
386
  throw midstream.exhaustedError();
337
387
  } catch (err) {
388
+ thread.settle();
338
389
  const fallbackOpts = takeAnthropicModelFallback(err, { tag: this.name, model: useModel, opts, onStageChange });
339
390
  return await this._doSend(messages, err.fallbackModel, tools, fallbackOpts);
340
391
  } finally {
392
+ thread.settle();
341
393
  totalTimeout.cleanup();
342
394
  }
343
395
  }
@@ -121,9 +121,11 @@ function drainCursorRuns(reason = 'shutdown') {
121
121
  // so chain the previously registered handler instead of overwriting it: whoever
122
122
  // loads first stays reachable, in either import order.
123
123
  const _priorCursorSessionCloseHook = globalThis.__mixdogCloseProviderConnectionsForSession;
124
- globalThis.__mixdogCloseProviderConnectionsForSession = (sessionId, reason) => {
124
+ // An active Cursor run is the turn in flight, so a turn abort
125
+ // (`options.inFlightOnly`) closes it as well; the options pass down the chain.
126
+ globalThis.__mixdogCloseProviderConnectionsForSession = (sessionId, reason, options) => {
125
127
  try {
126
- _priorCursorSessionCloseHook?.(sessionId, reason);
128
+ _priorCursorSessionCloseHook?.(sessionId, reason, options);
127
129
  } finally {
128
130
  closeCursorRunsForSession(sessionId, reason);
129
131
  // A disposed transport scope never sends again: drop its conversation.