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
@@ -4,6 +4,7 @@
4
4
  * reconstructed response, tool-call parsing and dispatch, and the result.
5
5
  */
6
6
  import { dedupeToolCallList } from '../lib/leaked-toolcall.mjs';
7
+ import { generationWindow } from '../lib/generation-window.mjs';
7
8
  import { truncatedCompatStreamError } from '../lib/openai-tool-args.mjs';
8
9
  import {
9
10
  emitCompatToolCallOnce,
@@ -104,5 +105,6 @@ export function settleCompatStream(state, { label, parseToolCalls }) {
104
105
  reasoningSource: state.sawReasoningContent ? state.reasoningSource : null,
105
106
  reasoningDetails: state.reasoningDetails.length ? state.reasoningDetails : null,
106
107
  rawUsage: state.rawUsage,
108
+ ...generationWindow(state.firstOutputAt),
107
109
  };
108
110
  }
@@ -14,6 +14,7 @@ import {
14
14
  import { chatCompletionUsage } from '../openai-compat-response-normalization.mjs';
15
15
  import { noteAbandonedUsage } from '../../../../shared/llm/usage-context.mjs';
16
16
  import { createToolCallAccumulator } from './tool-call-acc.mjs';
17
+ import { isVisibleStreamProgress } from '../../../../shared/stream-progress.mjs';
17
18
 
18
19
  export function createCompatStreamState({
19
20
  providerName,
@@ -65,6 +66,10 @@ export function reportTransport(state) {
65
66
 
66
67
  export function reportProgress(state, kind) {
67
68
  if (kind !== 'transport') state.semanticIdleDeadlineAt = Date.now() + state.idleMs;
69
+ // A chat stream shows output only as deltas: the first visible one starts
70
+ // the generation (generation-window.mjs). Usage accounting trusts it only
71
+ // from a provider that streams every token, reasoning included.
72
+ if (isVisibleStreamProgress(kind)) state.firstOutputAt ||= Date.now();
68
73
  try {
69
74
  state.onStreamDelta?.(kind);
70
75
  } catch {}
@@ -4,6 +4,7 @@ import {
4
4
  normalizeOpencodeGoReasoningEffort,
5
5
  } from './openai-compat-xai.mjs';
6
6
  import { isCustomProviderName, resolveCustomReasoning } from './custom-reasoning.mjs';
7
+ import { openRouterProviderRouting } from './openrouter-provider-routing.mjs';
7
8
 
8
9
  function normalizeReasoningEffort(value, allowed) {
9
10
  const effort = String(value ?? '')
@@ -66,6 +67,8 @@ export function applyCompatProviderChatOptions(params, providerName, opts = {},
66
67
  if (providerName === 'openrouter') {
67
68
  const reasoning = openRouterReasoning(opts, config);
68
69
  if (reasoning) params.reasoning = reasoning;
70
+ const routing = openRouterProviderRouting(opts);
71
+ if (routing) params.provider = routing;
69
72
  return params;
70
73
  }
71
74
  if (providerName === 'xai') {
@@ -242,6 +242,8 @@ export function normalizeCompatChatResponse({
242
242
  ...(reasoningContent !== null ? { reasoningContent } : {}),
243
243
  ...(Object.keys(providerMetadata).length ? { providerMetadata } : {}),
244
244
  usage: response.usage ? chatCompletionUsage(providerName, response.usage) : undefined,
245
+ // The answering stream's own generation window (generation-window.mjs).
246
+ ...(assembled.generationMs != null ? { generationMs: assembled.generationMs } : {}),
245
247
  };
246
248
  }
247
249
 
@@ -26,6 +26,7 @@ import { nativeDeferredFamilyForRoute } from './wire-route.mjs';
26
26
  import { preparePdfTextForProvider } from './media-normalization.mjs';
27
27
  import { normalizeOpencodeGoReasoningEffort } from './openai-compat-xai.mjs';
28
28
  import { isCustomProviderName, resolveCustomReasoning } from './custom-reasoning.mjs';
29
+ import { applyCustomRequestBody, customRequestHeaders } from './custom-provider-config.mjs';
29
30
  import { projectEffortConfiguration, withEffortConfigurationFallback } from './effort-configuration.mjs';
30
31
  import { normalizeCacheKey } from '../agent-runtime/cache-strategy.mjs';
31
32
  import { createProviderReplay } from './lib/provider-replay.mjs';
@@ -205,6 +206,10 @@ function buildCompatResponsesParams({
205
206
  (typeof provider.getCachedModelInfo === 'function' && provider.getCachedModelInfo(useModel)) || null;
206
207
  hook(params, { useModel, opts, modelInfo });
207
208
  }
209
+ // A custom provider's own extra JSON; the app-owned keys are never overridden.
210
+ if (isCustomProviderName(provider.name) && provider.customConfig) {
211
+ applyCustomRequestBody(params, provider.customConfig, useModel);
212
+ }
208
213
  return params;
209
214
  }
210
215
 
@@ -344,7 +349,11 @@ async function sendCompatResponsesOnce(provider, requestMessages, useModel, tool
344
349
  // Custom routes only: a gateway route that opts into prompt_cache_key keeps
345
350
  // its own headers.
346
351
  const headers = isCustomProviderName(provider.name)
347
- ? customSessionHeaders(provider, sessionCacheKey, opts.requestHeaders)
352
+ ? customSessionHeaders(
353
+ provider,
354
+ sessionCacheKey,
355
+ customRequestHeaders(provider.customConfig, useModel, opts.requestHeaders)
356
+ )
348
357
  : opts.requestHeaders;
349
358
 
350
359
  try {
@@ -41,25 +41,11 @@ function promptCacheForm(model) {
41
41
  return openAiSupportsExtendedRetention(model) ? 'retention' : null;
42
42
  }
43
43
 
44
- // The environment developer item buildRequestBody places first in `input`:
45
- // the end of the session's stable prefix (tools + instructions + environment).
46
- // Top-level `instructions` cannot carry a breakpoint, so this is the earliest
47
- // place one fits.
48
- function stablePrefixBlock(body) {
49
- const first = Array.isArray(body.input) ? body.input[0] : null;
50
- const content = first?.type === 'message' && first.role === 'developer' ? first.content : null;
51
- const block = Array.isArray(content) && content.length === 1 ? content[0] : null;
52
- return block?.type === 'input_text' && String(block.text || '').startsWith('<environment_context>') ? block : null;
53
- }
54
-
55
44
  /**
56
- * Public-API cache lifetime fields. GPT-5.6+: request ttl "30m" for the
57
- * moving tail; `prefixTtl` '12h' (Lead class, cache-strategy.mjs
58
- * resolveOpenAiPrefixCacheTtl) adds one explicit 12h breakpoint at the end of
59
- * the stable prefix in the default implicit mode, so the implicit tail
60
- * breakpoint stays 30m. Earlier models are unchanged.
45
+ * Public-API cache lifetime fields. GPT-5.6+: request ttl "30m" for every
46
+ * session. Earlier models are unchanged.
61
47
  */
62
- export function applyOpenAIDirectCachePolicy(body, model, storeResponses, { prefixTtl = null } = {}) {
48
+ export function applyOpenAIDirectCachePolicy(body, model, storeResponses) {
63
49
  body.store = storeResponses;
64
50
  delete body.prompt_cache_retention;
65
51
  delete body.prompt_cache_options;
@@ -67,16 +53,7 @@ export function applyOpenAIDirectCachePolicy(body, model, storeResponses, { pref
67
53
  // response storage is disabled. The provider's default cache still applies.
68
54
  if (!storeResponses) return body;
69
55
  const form = promptCacheForm(model);
70
- if (form === 'options') {
71
- body.prompt_cache_options = { ttl: '30m' };
72
- const block = prefixTtl === '12h' ? stablePrefixBlock(body) : null;
73
- if (block) {
74
- const [first, ...rest] = body.input;
75
- body.input = [
76
- { ...first, content: [{ ...block, prompt_cache_breakpoint: { mode: 'explicit', ttl: '12h' } }] },
77
- ...rest,
78
- ];
79
- }
80
- } else if (form === 'retention') body.prompt_cache_retention = '24h';
56
+ if (form === 'options') body.prompt_cache_options = { ttl: '30m' };
57
+ else if (form === 'retention') body.prompt_cache_retention = '24h';
81
58
  return body;
82
59
  }
@@ -44,6 +44,9 @@ export function createResponseEvents({
44
44
  }
45
45
  break;
46
46
  case 'response.output_item.added':
47
+ // An encrypted reasoning item opens as the model starts thinking:
48
+ // the first item starts the generation (generation-window.mjs).
49
+ state.firstOutputAt ||= Date.now();
47
50
  output.onOutputItemAdded(event.item);
48
51
  reportStreamItemStart(onStreamItemStart, responsesItemStart(event.item));
49
52
  break;
@@ -10,6 +10,7 @@ import { _displayCodexModel } from '../openai-codex-model.mjs';
10
10
  import { createProviderReplay } from '../lib/provider-replay.mjs';
11
11
  import { createResponseStamps } from './response-stamps.mjs';
12
12
  import { refusalResultFields } from '../openai-ws-response-state/refusals.mjs';
13
+ import { generationWindow } from '../lib/generation-window.mjs';
13
14
 
14
15
  export function createResponseOutcome({ state }) {
15
16
  const { stampToolSafety, stampOutcome, stallPartial, stampStreamError, attachStreamPartial, endedEarlyError } =
@@ -87,6 +88,7 @@ export function createResponseOutcome({ state }) {
87
88
  serviceTier: state.serviceTier || undefined,
88
89
  // A refused turn: typed stop reason and the refusal text.
89
90
  ...refusalResultFields(state.refusals, state.content),
91
+ ...generationWindow(state.firstOutputAt),
90
92
  };
91
93
  };
92
94
 
@@ -29,6 +29,7 @@ import {
29
29
  _cfCookieHeader,
30
30
  } from './openai-ws-headers.mjs';
31
31
  import { clearAllCodexTurnStates, clearCodexTurnStateScope, retireCodexTurnStateOwner } from './openai-turn-state.mjs';
32
+ import { runInRootAsyncContext } from '../../../shared/root-async-context.mjs';
32
33
 
33
34
  // Human-readable transport label for handshake/acquire error messages. Shared
34
35
  // with openai-oauth-ws.mjs (stream-side errors use the same labels).
@@ -273,16 +274,20 @@ function _removeFromPool(poolKey, entry) {
273
274
  if (arr.length === 0) _wsPool.delete(poolKey);
274
275
  }
275
276
 
277
+ // Pooled sockets and their idle/liveness timers outlive the request that made
278
+ // them; created in the root context they never retain that request's session.
276
279
  function _scheduleIdleClose(poolKey, entry) {
277
280
  if (!entry) return;
278
281
  if (entry.idleTimer) clearTimeout(entry.idleTimer);
279
- entry.idleTimer = setTimeout(() => {
280
- if (entry.busy) return;
281
- try {
282
- entry.socket.close(1000, 'idle_timeout');
283
- } catch {}
284
- _removeFromPool(poolKey, entry);
285
- }, WS_IDLE_MS);
282
+ entry.idleTimer = runInRootAsyncContext(() =>
283
+ setTimeout(() => {
284
+ if (entry.busy) return;
285
+ try {
286
+ entry.socket.close(1000, 'idle_timeout');
287
+ } catch {}
288
+ _removeFromPool(poolKey, entry);
289
+ }, WS_IDLE_MS)
290
+ );
286
291
  try {
287
292
  entry.idleTimer.unref?.();
288
293
  } catch {}
@@ -369,22 +374,24 @@ function _pingProbe(entry, timeoutMs) {
369
374
  // its own inter-chunk/semantic-idle watchdogs.
370
375
  function _armLiveness(poolKey, entry) {
371
376
  _clearLiveness(entry);
372
- entry.pingTimer = setInterval(async () => {
373
- if (entry.busy || entry.closing || entry.probing) return;
374
- if (!_isOpen(entry)) {
375
- _evictDead(poolKey, entry);
376
- return;
377
- }
378
- // Recent activity ⇒ assume live, skip the probe this tick.
379
- if (Date.now() - (entry.lastAliveAt || 0) < WS_LIVENESS_STALE_MS) return;
380
- entry.probing = true;
381
- try {
382
- const alive = await _pingProbe(entry, WS_PONG_TIMEOUT_MS);
383
- if (!alive && !entry.busy) _evictDead(poolKey, entry);
384
- } finally {
385
- entry.probing = false;
386
- }
387
- }, WS_PING_INTERVAL_MS);
377
+ entry.pingTimer = runInRootAsyncContext(() =>
378
+ setInterval(async () => {
379
+ if (entry.busy || entry.closing || entry.probing) return;
380
+ if (!_isOpen(entry)) {
381
+ _evictDead(poolKey, entry);
382
+ return;
383
+ }
384
+ // Recent activity ⇒ assume live, skip the probe this tick.
385
+ if (Date.now() - (entry.lastAliveAt || 0) < WS_LIVENESS_STALE_MS) return;
386
+ entry.probing = true;
387
+ try {
388
+ const alive = await _pingProbe(entry, WS_PONG_TIMEOUT_MS);
389
+ if (!alive && !entry.busy) _evictDead(poolKey, entry);
390
+ } finally {
391
+ entry.probing = false;
392
+ }
393
+ }, WS_PING_INTERVAL_MS)
394
+ );
388
395
  try {
389
396
  entry.pingTimer.unref?.();
390
397
  } catch {}
@@ -734,7 +741,11 @@ async function _openPoolEntry({ auth, cacheKey, codexHeaders, externalSignal, co
734
741
  const tokenHash = createHash('sha256').update(String(sessionToken)).digest('hex').slice(0, 8);
735
742
  _acquireTrace(`acquire-new tokenHash=${tokenHash} elapsed=${Date.now() - acqStart}ms`);
736
743
  }
737
- const { socket } = await _openSocketImpl({ auth, sessionToken, externalSignal, cacheKey, codexHeaders });
744
+ // The socket (its TCP/TLS handle and handshake timers) is reused by later
745
+ // requests: open it in the root context, never this one's.
746
+ const { socket } = await runInRootAsyncContext(() =>
747
+ _openSocketImpl({ auth, sessionToken, externalSignal, cacheKey, codexHeaders })
748
+ );
738
749
  if (_drainComplete) {
739
750
  try {
740
751
  socket.close(1000, 'drain-complete');
@@ -922,8 +933,18 @@ export function releaseWebSocket({ entry, poolKey: callerPoolKey, keep }) {
922
933
  _releasePoolOwner(poolKey, entry);
923
934
  }
924
935
 
925
- function closeOpenaiWsPoolForSession(poolKey, reason = 'session_closed') {
936
+ function closeOpenaiWsPoolForSession(poolKey, reason = 'session_closed', { inFlightOnly = false } = {}) {
926
937
  if (!poolKey) return;
938
+ if (inFlightOnly) {
939
+ // A turn abort: only a socket mid-response is unusable. An idle one keeps
940
+ // its response chain for the next turn; the closed one's own 'close'
941
+ // handler releases its owner claim and pool slot.
942
+ const owned = [...(_wsPool.get(poolKey) || []), ...[..._unpooledSockets].filter((e) => e.poolKey === poolKey)];
943
+ for (const entry of owned) {
944
+ if (entry.busy) _shutdownEntry(entry, String(reason || 'turn_aborted'));
945
+ }
946
+ return;
947
+ }
927
948
  _clearPoolOwnerScope(poolKey);
928
949
  clearCodexTurnStateScope(poolKey);
929
950
  const closeReason = String(reason || 'session_closed');
@@ -1052,11 +1073,12 @@ export function drainOpenaiWsPool(reason = 'shutdown', { immediate = false } = {
1052
1073
  // registers the same globals), so chain whatever is already installed instead of
1053
1074
  // replacing it — import order must not decide which transport gets torn down.
1054
1075
  const _priorPoolSessionCloseHook = globalThis.__mixdogCloseProviderConnectionsForSession;
1055
- globalThis.__mixdogCloseProviderConnectionsForSession = (poolKey, reason) => {
1076
+ // `options.inFlightOnly`: a turn abort, which leaves idle connections alone.
1077
+ globalThis.__mixdogCloseProviderConnectionsForSession = (poolKey, reason, options) => {
1056
1078
  try {
1057
- _priorPoolSessionCloseHook?.(poolKey, reason);
1079
+ _priorPoolSessionCloseHook?.(poolKey, reason, options);
1058
1080
  } finally {
1059
- closeOpenaiWsPoolForSession(poolKey, reason);
1081
+ closeOpenaiWsPoolForSession(poolKey, reason, options);
1060
1082
  }
1061
1083
  };
1062
1084
  const _priorPoolDrainHook = globalThis.__mixdogDrainProviderConnections;
@@ -15,6 +15,7 @@ import { createWsToolCalls } from './openai-ws-response-state/tool-calls.mjs';
15
15
  import { normalizeWsUsage } from './openai-ws-response-state/usage.mjs';
16
16
  import { createRefusals, refusalResultFields } from './openai-ws-response-state/refusals.mjs';
17
17
  import { isIncompleteToolItem } from './lib/responses-terminal-fields.mjs';
18
+ import { generationWindow } from './lib/generation-window.mjs';
18
19
 
19
20
  export { parseToolSearchArgs } from './openai-ws-response-state/tool-calls.mjs';
20
21
 
@@ -27,6 +28,7 @@ export { parseToolSearchArgs } from './openai-ws-response-state/tool-calls.mjs';
27
28
  */
28
29
  export function createWsResponseState({ onToolCall, onStreamDelta, midState, traceProvider }) {
29
30
  let content = '';
31
+ let firstOutputAt = 0;
30
32
  let model = '';
31
33
  let responseId = '';
32
34
  let responseServiceTier = '';
@@ -152,8 +154,12 @@ export function createWsResponseState({ onToolCall, onStreamDelta, midState, tra
152
154
  if (response?.model) model = response.model;
153
155
  if (response?.id) responseId = response.id;
154
156
  },
155
- /** response.output_item.added */
156
- noteItemAdded: tools.noteItemAdded,
157
+ /** response.output_item.added: an encrypted reasoning item opens as the
158
+ * model starts thinking, so the first one starts the generation. */
159
+ noteItemAdded(item) {
160
+ firstOutputAt ||= Date.now();
161
+ return tools.noteItemAdded(item);
162
+ },
157
163
  /** response.function_call_arguments.delta / custom_tool_call_input.delta */
158
164
  noteToolInputDelta: tools.noteToolInputDelta,
159
165
  /** response.function_call_arguments.done */
@@ -257,6 +263,7 @@ export function createWsResponseState({ onToolCall, onStreamDelta, midState, tra
257
263
  serviceTier: responseServiceTier || undefined,
258
264
  // A refused turn: typed stop reason and the refusal text.
259
265
  ...refusalResultFields(refusals, content),
266
+ ...generationWindow(firstOutputAt),
260
267
  };
261
268
  },
262
269
  };
@@ -1,3 +1,4 @@
1
+ import { AsyncResource } from 'node:async_hooks';
1
2
  import { performance } from 'node:perf_hooks';
2
3
  import { _parseEvent } from '../openai-ws-events.mjs';
3
4
  import {
@@ -158,13 +159,20 @@ export function createSocketHandlers({
158
159
  };
159
160
  const onOpen = () => _writeWsLifecycleTrace('open');
160
161
 
162
+ // Pooled sockets are opened in the root context (openai-ws-pool.mjs), and a
163
+ // socket's events run in the context it was opened in: bind this stream's
164
+ // handlers to the context of the request that sends on it.
165
+ const socketListeners = [
166
+ ['message', AsyncResource.bind(onMessage)],
167
+ ['close', AsyncResource.bind(onClose)],
168
+ ['error', AsyncResource.bind(onError)],
169
+ ['ping', AsyncResource.bind(onPing)],
170
+ ['pong', AsyncResource.bind(onPong)],
171
+ ];
172
+
161
173
  function detach() {
162
- socket.off('message', onMessage);
163
- socket.off('close', onClose);
164
- socket.off('error', onError);
174
+ for (const [event, listener] of socketListeners) socket.off(event, listener);
165
175
  socket.off('open', onOpen);
166
- socket.off('ping', onPing);
167
- socket.off('pong', onPong);
168
176
  externalSignal?.removeEventListener('abort', onAbort);
169
177
  }
170
178
 
@@ -176,11 +184,7 @@ export function createSocketHandlers({
176
184
  }
177
185
  externalSignal.addEventListener('abort', onAbort, { once: true });
178
186
  }
179
- socket.on('message', onMessage);
180
- socket.on('close', onClose);
181
- socket.on('error', onError);
182
- socket.on('ping', onPing);
183
- socket.on('pong', onPong);
187
+ for (const [event, listener] of socketListeners) socket.on(event, listener);
184
188
  if (WS_TRACE_ENABLED) {
185
189
  socket.on('open', onOpen);
186
190
  if (socket.readyState === WebSocket.OPEN) _writeWsLifecycleTrace('open');
@@ -142,9 +142,7 @@ export class OpenAIDirectProvider {
142
142
  // Keep public response storage and model-specific cache options out of
143
143
  // the shared OAuth payload. Storage opt-out still forces full frames in
144
144
  // _computeDelta; retained output items make those frames self-contained.
145
- applyOpenAIDirectCachePolicy(body, useModel, _envFlag('MIXDOG_OAI_STORE', true), {
146
- prefixTtl: opts.promptCachePrefixTtl || null,
147
- });
145
+ applyOpenAIDirectCachePolicy(body, useModel, _envFlag('MIXDOG_OAI_STORE', true));
148
146
  // poolKey MUST be sessionId-only. Falling back to promptCacheKey would
149
147
  // let unrelated raw sessions sharing the same provider-scoped cache
150
148
  // bucket reuse each other's pooled socket and inherit lastResponseId
@@ -0,0 +1,28 @@
1
+ /**
2
+ * OpenRouter provider routing: the per-model `providerSort` picker parameter
3
+ * and the request's `provider: { sort }` field. Auto (the default) sends
4
+ * nothing; a stored value outside the known sorts is dropped.
5
+ */
6
+ export const OPENROUTER_SORT_PARAMETER = 'providerSort';
7
+
8
+ const SORTS = new Set(['price', 'throughput', 'latency']);
9
+
10
+ export const OPENROUTER_SORT_OPTION = Object.freeze({
11
+ id: OPENROUTER_SORT_PARAMETER,
12
+ label: 'Routing',
13
+ kind: 'enum',
14
+ options: [
15
+ { value: 'auto', label: 'Auto' },
16
+ { value: 'price', label: 'Price first' },
17
+ { value: 'throughput', label: 'Speed first' },
18
+ { value: 'latency', label: 'Latency first' },
19
+ ],
20
+ });
21
+
22
+ /** `{ sort }` for the request body's `provider` field, or null for Auto or an unknown value. */
23
+ export function openRouterProviderRouting(opts = {}) {
24
+ const sort = String(opts?.modelParameters?.[OPENROUTER_SORT_PARAMETER] ?? '')
25
+ .trim()
26
+ .toLowerCase();
27
+ return SORTS.has(sort) ? { sort } : null;
28
+ }
@@ -10,6 +10,7 @@
10
10
  import { WireGatewayProvider, messagesBaseFromOpenAIBase } from './wire-gateway.mjs';
11
11
  import { OPENAI_COMPAT_PRESETS } from './openai-compat-presets.mjs';
12
12
  import { openRouterReasoning } from './openai-compat-options.mjs';
13
+ import { openRouterProviderRouting } from './openrouter-provider-routing.mjs';
13
14
 
14
15
  /**
15
16
  * The Claude id a routing slug stands for: an alias (`~anthropic/claude-opus-latest`)
@@ -25,12 +26,14 @@ export function openRouterCapabilityModel(model, aliasTarget = null) {
25
26
 
26
27
  // Responses requests carry the same reasoning control as Chat requests
27
28
  // (`openRouterReasoningEffort`, else the turn effort, else the configured
28
- // effort; `none` disables reasoning).
29
+ // effort; `none` disables reasoning) and the same provider routing.
29
30
  function openRouterResponsesParams(config) {
30
31
  return (params, { opts }) => {
31
32
  const reasoning = openRouterReasoning(opts, config);
32
33
  if (reasoning) params.reasoning = reasoning;
33
34
  else delete params.reasoning;
35
+ const routing = openRouterProviderRouting(opts);
36
+ if (routing) params.provider = routing;
34
37
  };
35
38
  }
36
39
 
@@ -385,29 +385,6 @@ async function _initProvidersUnsynchronized(config, signal = null, proxyChannel
385
385
  if (result.sig !== null) nextSignatures.set(result.name, result.sig);
386
386
  }
387
387
  _providerInitErrors = nextInitErrors;
388
- // Second guard: every entry was disabled. Same reasoning — keep the
389
- // existing registry rather than going dark. Isolation still holds: a live
390
- // instance of a provider that is now an invalid custom config is removed
391
- // (the rest of the registry stays), and the recorded reasons are reported.
392
- if (next.size === 0) {
393
- let removed = false;
394
- for (const name of nextInitErrors.keys()) {
395
- removed = providers.delete(name) || removed;
396
- signatures.delete(name);
397
- }
398
- if (removed) {
399
- _providerCatalogRevision += 1;
400
- _startupCatalogRefreshPromise = null;
401
- // The live registry no longer reflects the last applied config.
402
- _lastAppliedSig = null;
403
- }
404
- const reasons = [...nextInitErrors.values()];
405
- throw noEnabledProvidersError(
406
- `[provider] all providers disabled in config — refusing to clear registry${
407
- reasons.length ? `; invalid custom providers skipped: ${reasons.join('; ')}` : ''
408
- }`
409
- );
410
- }
411
388
  // OAuth preservation guard. anthropic-oauth / openai-oauth are NOT stored
412
389
  // in mixdog-config.json — buildDefaultConfig injects them at runtime by
413
390
  // calling hasAnthropic/OpenAIOAuthCredentials(), which reads the on-disk
@@ -430,20 +407,48 @@ async function _initProvidersUnsynchronized(config, signal = null, proxyChannel
430
407
  // `enabled` itself, so an explicit UI/hand-edited disable still removes the
431
408
  // provider even while its credential file happens to be unreadable, and a
432
409
  // logout (probe 'absent') never carries the marker at all.
410
+ //
411
+ // It runs before the all-disabled guard below: a secrets-less load
412
+ // (explore) marks every OAuth entry probe-unavailable, so an OAuth-only
413
+ // setup has no enabled entry and only its preserved instances.
433
414
  const nextDisabled = new Set();
434
415
  for (const [name, cfg] of entries) {
435
416
  if (cfg?.enabled) continue;
436
417
  if (OAUTH_PROVIDER_NAMES.has(name) && cfg?.credentialProbeUnavailable === true) continue;
437
418
  nextDisabled.add(name);
438
419
  }
439
- _explicitlyDisabled = nextDisabled;
440
420
  for (const name of OAUTH_PROVIDER_NAMES) {
441
421
  if (next.has(name) || !providers.has(name)) continue;
442
- if (_explicitlyDisabled.has(name)) continue;
422
+ if (nextDisabled.has(name)) continue;
443
423
  if (oauthCredentialProbeState(name) === 'absent') continue;
444
424
  next.set(name, providers.get(name));
445
425
  if (signatures.has(name)) nextSignatures.set(name, signatures.get(name));
446
426
  }
427
+ // Second guard: every entry was disabled and nothing was preserved. Same
428
+ // reasoning — keep the existing registry rather than going dark. Isolation
429
+ // still holds: a live instance of a provider that is now an invalid custom
430
+ // config is removed (the rest of the registry stays), and the recorded
431
+ // reasons are reported.
432
+ if (next.size === 0) {
433
+ let removed = false;
434
+ for (const name of nextInitErrors.keys()) {
435
+ removed = providers.delete(name) || removed;
436
+ signatures.delete(name);
437
+ }
438
+ if (removed) {
439
+ _providerCatalogRevision += 1;
440
+ _startupCatalogRefreshPromise = null;
441
+ // The live registry no longer reflects the last applied config.
442
+ _lastAppliedSig = null;
443
+ }
444
+ const reasons = [...nextInitErrors.values()];
445
+ throw noEnabledProvidersError(
446
+ `[provider] all providers disabled in config — refusing to clear registry${
447
+ reasons.length ? `; invalid custom providers skipped: ${reasons.join('; ')}` : ''
448
+ }`
449
+ );
450
+ }
451
+ _explicitlyDisabled = nextDisabled;
447
452
  throwIfAborted(signal);
448
453
  // Reconfiguration opens a new catalog epoch. A provider added (or rebuilt
449
454
  // with an empty model cache) after the startup refresh settled would
@@ -4,6 +4,7 @@
4
4
  * idle worker from holding the process open.
5
5
  */
6
6
  import { Worker } from 'node:worker_threads';
7
+ import { runInRootAsyncContext } from '../../../../../shared/root-async-context.mjs';
7
8
 
8
9
  // After this many consecutive worker-construction failures the pool stops
9
10
  // trying to spawn and stays inline. This is a failure latch, not a throttle:
@@ -65,9 +66,13 @@ export function createSlotSpawner({ state, WorkerImpl = Worker, isClosed, affini
65
66
  function createSlot() {
66
67
  let worker;
67
68
  try {
68
- worker = new WorkerImpl(new URL('../../stream-json-worker.mjs', import.meta.url), {
69
- execArgv: [],
70
- });
69
+ // A slot serves later streams too: spawn it outside this stream's context.
70
+ worker = runInRootAsyncContext(
71
+ () =>
72
+ new WorkerImpl(new URL('../../stream-json-worker.mjs', import.meta.url), {
73
+ execArgv: [],
74
+ })
75
+ );
71
76
  } catch {
72
77
  noteSpawnFailure();
73
78
  return null;
@@ -1,4 +1,4 @@
1
- import { AsyncLocalStorage } from 'node:async_hooks';
1
+ import { runtimeAsyncLocalStorage } from '../../../shared/root-async-context.mjs';
2
2
  import { currentUsageContext } from '../../../shared/llm/usage-context.mjs';
3
3
  import { noteErrorUsage } from './lib/note-error-usage.mjs';
4
4
  import { OpenAICompatProvider } from './openai-compat.mjs';
@@ -73,7 +73,7 @@ export function vertexErrorBody(text) {
73
73
 
74
74
  // The account one send targets ({ identity, token }), read by its engine's
75
75
  // fetch on every HTTP attempt, the engine's own retries included.
76
- const sendAccount = new AsyncLocalStorage();
76
+ const sendAccount = runtimeAsyncLocalStorage();
77
77
 
78
78
  // Each HTTP attempt carries the send's account's current token, and a
79
79
  // credential switch since the send began aborts it before anything is sent.
@@ -0,0 +1,66 @@
1
+ // The text an ask_user call returns to the model for the user's decision:
2
+ // shared by the tool (ask-user-tool.mjs) and the hand-off copies of a
3
+ // question (tui/session/tool-approval.mjs), whose settled card shows the same
4
+ // record the asking session's transcript keeps.
5
+ import { ASK_USER_MAX_TEXT } from './ask-user-tool-defs.mjs';
6
+
7
+ const ASK_USER_DISMISSED =
8
+ 'User closed the questions without answering. Proceed with your best judgment; ask again only if something new depends on it.';
9
+
10
+ /** Wire answers -> array aligned with questions; invalid entries become null. */
11
+ function normalizeAskUserAnswers(answers, questions) {
12
+ const list = Array.isArray(answers) ? answers.slice(0, 4) : [];
13
+ return questions.map((q, i) => {
14
+ const entry = list[i];
15
+ if (!entry || typeof entry !== 'object') return null;
16
+ const labels = new Set(q.options.map((o) => o.label));
17
+ const rawSelected = entry.selected == null ? [] : entry.selected;
18
+ if (!Array.isArray(rawSelected) || !rawSelected.every((l) => typeof l === 'string' && labels.has(l))) return null;
19
+ if (entry.text != null && (typeof entry.text !== 'string' || entry.text.length > ASK_USER_MAX_TEXT)) return null;
20
+ const selected = q.multi_select === true ? [...new Set(rawSelected)] : rawSelected.slice(0, 1);
21
+ const text = typeof entry.text === 'string' ? entry.text.trim() : '';
22
+ if (!selected.length && !text) return null;
23
+ return text ? { selected, text } : { selected };
24
+ });
25
+ }
26
+
27
+ // Strings are JSON-quoted so a quote inside one cannot end it early; typed
28
+ // text stays apart from the picked labels (`"picked" + "typed" (typed)`).
29
+ function formatAskUserAnswers(questions, answers) {
30
+ const quote = (text) => JSON.stringify(text);
31
+ const parts = questions.map((q, i) => {
32
+ const a = answers[i];
33
+ if (!a) return `${quote(q.question)}=(no answer)`;
34
+ const picked = a.selected.length ? quote(a.selected.join(', ')) : '';
35
+ const typed = a.text ? `${quote(a.text)} (typed)` : '';
36
+ return `${quote(q.question)}=${[picked, typed].filter(Boolean).join(' + ')}`;
37
+ });
38
+ return `User has answered your questions: ${parts.join('; ')}. You can now continue with these answers in mind.`;
39
+ }
40
+
41
+ /** A composer message sent instead of (or with) picking: kept when it is a
42
+ * non-empty string within the typed-answer limit, else dropped. */
43
+ function normalizeAskUserReply(reply) {
44
+ if (typeof reply !== 'string' || reply.length > ASK_USER_MAX_TEXT) return '';
45
+ return reply.trim();
46
+ }
47
+
48
+ // `User replied in chat: "<reply>". ` leads, then the usual picks sentence
49
+ // when any question was answered, else just the closing sentence.
50
+ function formatAskUserResult(questions, answers, reply) {
51
+ if (!reply) return formatAskUserAnswers(questions, answers);
52
+ const lead = `User replied in chat: ${JSON.stringify(reply)}. `;
53
+ if (answers.some(Boolean)) return `${lead}${formatAskUserAnswers(questions, answers)}`;
54
+ return `${lead}You can now continue with these answers in mind.`;
55
+ }
56
+
57
+ /** The ask_user result for `questions` (validated) and the hook's `result`
58
+ * (`{ approved, answers, reply }`): the answers, or the dismissal. */
59
+ export function askUserResultText(questions, result) {
60
+ if (result?.approved !== true) return ASK_USER_DISMISSED;
61
+ return formatAskUserResult(
62
+ questions,
63
+ normalizeAskUserAnswers(result.answers, questions),
64
+ normalizeAskUserReply(result.reply)
65
+ );
66
+ }