mixdog 1.4.4 → 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 (218) hide show
  1. package/package.json +1 -2
  2. package/src/cli.mjs +5 -1
  3. package/src/defaults/shipped-history.json +26 -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 -240
  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 +16 -20
  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 +72 -13
  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/lead/LEAD.md +3 -1
  64. package/src/runtime/agent/orchestrator/agent-runtime/cache-strategy.mjs +8 -30
  65. package/src/runtime/agent/orchestrator/agent-runtime/title-completion.mjs +5 -0
  66. package/src/runtime/agent/orchestrator/mcp/oauth-request.mjs +21 -2
  67. package/src/runtime/agent/orchestrator/providers/account-pool.mjs +3 -0
  68. package/src/runtime/agent/orchestrator/providers/admission-scheduler.mjs +2 -2
  69. package/src/runtime/agent/orchestrator/providers/anthropic-api-transport.mjs +2 -0
  70. package/src/runtime/agent/orchestrator/providers/anthropic-message-threads.mjs +583 -0
  71. package/src/runtime/agent/orchestrator/providers/anthropic-oauth-client-version.mjs +1 -1
  72. package/src/runtime/agent/orchestrator/providers/anthropic-oauth.mjs +81 -19
  73. package/src/runtime/agent/orchestrator/providers/anthropic-sse.mjs +180 -13
  74. package/src/runtime/agent/orchestrator/providers/anthropic.mjs +56 -6
  75. package/src/runtime/agent/orchestrator/providers/cursor-wire.mjs +4 -2
  76. package/src/runtime/agent/orchestrator/providers/gemini-error-contract.mjs +6 -6
  77. package/src/runtime/agent/orchestrator/providers/gemini-model-redirect.mjs +44 -0
  78. package/src/runtime/agent/orchestrator/providers/gemini-reasoning-relay.mjs +5 -0
  79. package/src/runtime/agent/orchestrator/providers/gemini-request-body.mjs +34 -5
  80. package/src/runtime/agent/orchestrator/providers/gemini-response.mjs +10 -0
  81. package/src/runtime/agent/orchestrator/providers/gemini-schema.mjs +11 -6
  82. package/src/runtime/agent/orchestrator/providers/gemini-sdk-request.mjs +4 -100
  83. package/src/runtime/agent/orchestrator/providers/gemini-stream.mjs +3 -227
  84. package/src/runtime/agent/orchestrator/providers/gemini.mjs +226 -118
  85. package/src/runtime/agent/orchestrator/providers/lib/anthropic-request-utils.mjs +11 -1
  86. package/src/runtime/agent/orchestrator/providers/lib/claude-bridge-schema.mjs +15 -3
  87. package/src/runtime/agent/orchestrator/providers/lib/gemini-model-catalog.mjs +2 -1
  88. package/src/runtime/agent/orchestrator/providers/openai-direct-request.mjs +5 -28
  89. package/src/runtime/agent/orchestrator/providers/openai-ws-pool.mjs +50 -28
  90. package/src/runtime/agent/orchestrator/providers/openai-ws-stream/socket-handlers.mjs +14 -10
  91. package/src/runtime/agent/orchestrator/providers/openai-ws.mjs +1 -3
  92. package/src/runtime/agent/orchestrator/providers/stream-json-pool/worker-slots/slot-spawn.mjs +8 -3
  93. package/src/runtime/agent/orchestrator/providers/vertex-ai-maas.mjs +2 -2
  94. package/src/runtime/agent/orchestrator/runtime-core/config-helpers.mjs +4 -5
  95. package/src/runtime/agent/orchestrator/runtime-core/goal-tool-defs.mjs +2 -2
  96. package/src/runtime/agent/orchestrator/runtime-core/provider-request-tools.mjs +15 -8
  97. package/src/runtime/agent/orchestrator/runtime-core/session-title.mjs +1 -0
  98. package/src/runtime/agent/orchestrator/runtime-core/todo-list.mjs +9 -32
  99. package/src/runtime/agent/orchestrator/runtime-core/todo-tool-defs.mjs +4 -5
  100. package/src/runtime/agent/orchestrator/session/compact/summary-route.mjs +1 -3
  101. package/src/runtime/agent/orchestrator/session/loop/compact-policy.mjs +5 -2
  102. package/src/runtime/agent/orchestrator/session/loop/no-tool-turn/todo-check.mjs +2 -17
  103. package/src/runtime/agent/orchestrator/session/loop/usage.mjs +7 -3
  104. package/src/runtime/agent/orchestrator/session/manager/ask-turn-settle.mjs +9 -2
  105. package/src/runtime/agent/orchestrator/session/manager/session-close.mjs +26 -13
  106. package/src/runtime/agent/orchestrator/session/manager/session-lifecycle.mjs +4 -5
  107. package/src/runtime/agent/orchestrator/session/manager/usage-metrics.mjs +16 -5
  108. package/src/runtime/agent/orchestrator/session/store/lifecycle-barriers.mjs +22 -1
  109. package/src/runtime/agent/orchestrator/session/store/live-state.mjs +4 -0
  110. package/src/runtime/agent/orchestrator/tools/builtin/cache-layers.mjs +11 -9
  111. package/src/runtime/agent/orchestrator/tools/builtin/grep-formatting.mjs +2 -8
  112. package/src/runtime/agent/orchestrator/tools/builtin/lib/search-input-helpers.mjs +1 -9
  113. package/src/runtime/agent/orchestrator/tools/builtin/local-search-telemetry.mjs +2 -2
  114. package/src/runtime/agent/orchestrator/tools/builtin/native-search-client.mjs +1 -1
  115. package/src/runtime/agent/orchestrator/tools/builtin/runtime-capabilities.mjs +12 -3
  116. package/src/runtime/agent/orchestrator/tools/code-graph/ast-calls.mjs +9 -2
  117. package/src/runtime/agent/orchestrator/tools/code-graph/build.mjs +137 -7
  118. package/src/runtime/agent/orchestrator/tools/code-graph/calls-cache.mjs +43 -14
  119. package/src/runtime/agent/orchestrator/tools/code-graph/calls-sidecar-worker.mjs +33 -0
  120. package/src/runtime/agent/orchestrator/tools/code-graph/disk-cache.mjs +146 -11
  121. package/src/runtime/agent/orchestrator/tools/code-graph/dispatch.mjs +31 -24
  122. package/src/runtime/agent/orchestrator/tools/code-graph/graph-binary/spawn-jsonl.mjs +172 -25
  123. package/src/runtime/agent/orchestrator/tools/code-graph/graph-model.mjs +100 -2
  124. package/src/runtime/agent/orchestrator/tools/code-graph/main-thread-slice.mjs +18 -0
  125. package/src/runtime/agent/orchestrator/tools/code-graph/mask-pool.mjs +219 -0
  126. package/src/runtime/agent/orchestrator/tools/code-graph/mask-worker.mjs +12 -0
  127. package/src/runtime/agent/orchestrator/tools/code-graph/memory-cache.mjs +6 -1
  128. package/src/runtime/agent/orchestrator/tools/code-graph/modes/calls.mjs +9 -2
  129. package/src/runtime/agent/orchestrator/tools/code-graph/modes/dependents.mjs +5 -1
  130. package/src/runtime/agent/orchestrator/tools/code-graph/modes/structure.mjs +8 -2
  131. package/src/runtime/agent/orchestrator/tools/code-graph/modes/symbols.mjs +14 -11
  132. package/src/runtime/agent/orchestrator/tools/code-graph/search-references.mjs +41 -18
  133. package/src/runtime/agent/orchestrator/tools/code-graph/search.mjs +391 -83
  134. package/src/runtime/agent/orchestrator/tools/code-graph/symbol-index.mjs +109 -9
  135. package/src/runtime/agent/orchestrator/tools/code-graph/text-mask.mjs +73 -29
  136. package/src/runtime/agent/orchestrator/tools/code-graph-prewarm-worker.mjs +6 -5
  137. package/src/runtime/agent/orchestrator/tools/lib/native-spawn-client.mjs +7 -3
  138. package/src/runtime/channels/lib/crash-log.mjs +21 -1
  139. package/src/runtime/channels/lib/worker-main.mjs +8 -25
  140. package/src/runtime/media/jobs.mjs +5 -0
  141. package/src/runtime/media/store.mjs +146 -3
  142. package/src/runtime/office/core/office-write-ledger.mjs +2 -2
  143. package/src/runtime/shared/agent-reap-state.mjs +19 -0
  144. package/src/runtime/shared/desktop-notice-host.d.mts +13 -0
  145. package/src/runtime/shared/desktop-notice-host.mjs +87 -0
  146. package/src/runtime/shared/file-lock.mjs +2 -2
  147. package/src/runtime/shared/heap-cap.mjs +9 -11
  148. package/src/runtime/shared/llm/http-agent.mjs +11 -1
  149. package/src/runtime/shared/llm/system-proxy.mjs +100 -25
  150. package/src/runtime/shared/llm/usage-accounting.mjs +18 -7
  151. package/src/runtime/shared/llm/usage-context.mjs +2 -2
  152. package/src/runtime/shared/llm/usage-ledger-requests.mjs +10 -3
  153. package/src/runtime/shared/llm/usage-ledger.mjs +13 -1
  154. package/src/runtime/shared/provider-accounts.mjs +22 -0
  155. package/src/runtime/shared/provider-auth-binding.mjs +2 -2
  156. package/src/runtime/shared/resource-admission.mjs +2 -2
  157. package/src/runtime/shared/root-async-context.mjs +51 -0
  158. package/src/runtime/shared/run-child.mjs +18 -1
  159. package/src/runtime/shared/slow-span.mjs +2 -2
  160. package/src/runtime/shared/tool-execution-owner.mjs +2 -2
  161. package/src/runtime/shared/tool-work-units.mjs +2 -0
  162. package/src/runtime/shared/turn-snapshot-store.mjs +75 -18
  163. package/src/runtime/shared/turn-worktree-snapshot.mjs +38 -8
  164. package/src/runtime/shared/user-cwd.mjs +2 -2
  165. package/src/runtime/shared/worker-requests.mjs +3 -1
  166. package/src/runtime/shared/worktrees/lock.mjs +2 -2
  167. package/src/runtime/visualize-bridge/client.mjs +26 -4
  168. package/src/runtime/visualize-bridge/tool-defs.mjs +46 -5
  169. package/src/session-runtime/boot/apis.mjs +1 -0
  170. package/src/session-runtime/boot/begin.mjs +2 -0
  171. package/src/session-runtime/media-api.mjs +15 -0
  172. package/src/session-runtime/provider-readiness.mjs +10 -2
  173. package/src/session-runtime/runtime-core.mjs +35 -8
  174. package/src/session-runtime/runtime-review-api.mjs +3 -1
  175. package/src/session-runtime/services/agent-tool/close-flow.mjs +6 -1
  176. package/src/session-runtime/services/agent-tool/execute-resume.mjs +94 -0
  177. package/src/session-runtime/services/agent-tool/execute-send.mjs +77 -27
  178. package/src/session-runtime/services/agent-tool/execute-spawn.mjs +28 -7
  179. package/src/session-runtime/services/agent-tool/execute.mjs +6 -1
  180. package/src/session-runtime/services/agent-tool/helpers.mjs +11 -0
  181. package/src/session-runtime/services/agent-tool/job-views/job-render.mjs +13 -10
  182. package/src/session-runtime/services/agent-tool/render.mjs +2 -0
  183. package/src/session-runtime/services/agent-tool/send-flow.mjs +1 -0
  184. package/src/session-runtime/services/agent-tool/spawn-flow/spawn-prep.mjs +6 -0
  185. package/src/session-runtime/services/agent-tool/spawn-flow/spawn-run.mjs +2 -1
  186. package/src/session-runtime/services/agent-tool/tag-registry/reaped-sessions.mjs +82 -0
  187. package/src/session-runtime/services/agent-tool/tag-registry/record-facts-worker.mjs +36 -0
  188. package/src/session-runtime/services/agent-tool/tag-registry.mjs +4 -0
  189. package/src/session-runtime/services/agent-tool/terminal-reaper/row-helpers.mjs +29 -0
  190. package/src/session-runtime/services/agent-tool/terminal-reaper/scan-admission.mjs +11 -3
  191. package/src/session-runtime/services/agent-tool/terminal-reaper/stale-rows.mjs +19 -8
  192. package/src/session-runtime/services/agent-tool/terminal-reaper.mjs +5 -2
  193. package/src/session-runtime/services/agent-tool/worker-index.mjs +1 -0
  194. package/src/session-runtime/services/daemon-crash-capture/daemon-exit-record.mjs +2 -2
  195. package/src/session-runtime/session-create.mjs +5 -0
  196. package/src/session-runtime/setup-tool/settings-contract.mjs +2 -0
  197. package/src/session-runtime/usage-stats-api.mjs +3 -0
  198. package/src/session-runtime/workspace/admission-gate.mjs +25 -7
  199. package/src/session-runtime/workspace/transitions.mjs +4 -0
  200. package/src/standalone/daemon-log.mjs +28 -4
  201. package/src/standalone/daemon-owner.mjs +41 -7
  202. package/src/standalone/daemon-work-gate.mjs +139 -0
  203. package/src/standalone/daemon.mjs +80 -11
  204. package/src/standalone/desktop-service-registry.mjs +11 -4
  205. package/src/standalone/session-client.mjs +57 -5
  206. package/src/standalone/session-protocol.mjs +5 -0
  207. package/src/standalone/session-service/agent-tree/agent-cancel.mjs +17 -1
  208. package/src/standalone/session-service/agent-tree.mjs +7 -0
  209. package/src/standalone/session-service/session-calls/session-turns.mjs +12 -5
  210. package/src/standalone/session-transport/call-route.mjs +10 -4
  211. package/src/standalone/session-transport/routes.mjs +21 -0
  212. package/src/standalone/session-transport/stack.mjs +3 -1
  213. package/src/standalone/session-transport.mjs +5 -0
  214. package/src/tui/dist/index.mjs +495 -477
  215. package/src/tui/session/session-api/media.mjs +5 -0
  216. package/src/tui/session/turn.mjs +13 -0
  217. package/src/runtime/agent/orchestrator/providers/gemini-sdk-stream/cancellation.mjs +0 -71
  218. package/src/runtime/agent/orchestrator/providers/gemini-sdk-stream/reader.mjs +0 -108
@@ -26,6 +26,25 @@ export function stampMs(value) {
26
26
  return Number.isFinite(parsed) && parsed > 0 ? parsed : 0;
27
27
  }
28
28
 
29
+ /** Stamped as `closeProvenance` on a record by every completed turn, beside
30
+ * that turn's completion time (`lastTurnCompletedAt`): from that turn on, an
31
+ * owner close of the session is recorded on the record (`ownerClosedAt`). A
32
+ * record without it last ran under a build that did not record owner closes,
33
+ * so its absence proves nothing. */
34
+ export const CLOSE_PROVENANCE_VERSION = 1;
35
+
36
+ /** Whether a reaped tag may continue this stored session: its record carries
37
+ * close provenance (absent → never; fail closed) and its owner did not close
38
+ * it (the agent tool's `close`, which keeps the conversation open) after its
39
+ * last completed turn. Only a turn completed after that close — the owner
40
+ * reusing it — lifts it; `lastUsedAt` also moves without a turn (a rewind),
41
+ * so it never counts. */
42
+ export function resumableAfterReap(record) {
43
+ if (record?.closeProvenance !== CLOSE_PROVENANCE_VERSION) return false;
44
+ const closedAt = stampMs(record.ownerClosedAt);
45
+ return !(closedAt > 0 && closedAt >= stampMs(record.lastTurnCompletedAt));
46
+ }
47
+
29
48
  export function tagTombstoneKey(row = {}) {
30
49
  const parent = clean(row.parentSessionId || row.ownerSessionId);
31
50
  if (parent) return `session:${parent}\0${clean(row.tag)}`;
@@ -0,0 +1,13 @@
1
+ export interface RaisedDesktopNotice {
2
+ key: string;
3
+ code: string;
4
+ params: Record<string, string>;
5
+ }
6
+ export function raiseDesktopNotice(key: string, code: string, params?: Record<string, unknown>): boolean;
7
+ export function clearDesktopNotice(key: string): void;
8
+ export function deliverPendingDesktopNotices(): void;
9
+ /** `sink` returns false when it had nobody to deliver to; the notice then
10
+ * waits and is offered again with `replay` true. */
11
+ export function registerDesktopNoticeSink(
12
+ sink: (notice: RaisedDesktopNotice, replay: boolean) => boolean | undefined
13
+ ): () => void;
@@ -0,0 +1,87 @@
1
+ // The few problems the desktop app tells the user as a toast (sign-in expired,
2
+ // secure storage failing, a conversation not saved), raised wherever they are
3
+ // detected — the runtime, MCP, the keychain, the session store — and handed to
4
+ // the desktop service (or the Electron main process) that reaches the window.
5
+ // A notice is raised once per key until clearDesktopNotice(key) re-arms it, so
6
+ // a retry or a poll never repeats it. A notice no sink delivered (no window or
7
+ // client attached) waits, one per key, for the next delivery chance.
8
+ //
9
+ // Deliberately dependency-free: the desktop service bundle imports this file
10
+ // too, and every copy (and keychain-cjs.cjs, through `raise`) shares one
11
+ // registry through a global symbol.
12
+
13
+ const KEY = Symbol.for('mixdog.desktopNoticeHost.v1');
14
+ /** Undelivered notices kept; the oldest go first. */
15
+ const PENDING_LIMIT = 20;
16
+
17
+ function registry() {
18
+ globalThis[KEY] ??= { raised: new Set(), sinks: [], pending: [], raise: null };
19
+ return globalThis[KEY];
20
+ }
21
+
22
+ /** Whether some sink took `notice`: a sink returns false when nobody was
23
+ * there to receive it. `replay`: it was offered before and waited. */
24
+ function offer(host, notice, replay = false) {
25
+ let delivered = false;
26
+ for (const { sink } of host.sinks) {
27
+ try {
28
+ if (sink(notice, replay) !== false) delivered = true;
29
+ } catch {}
30
+ }
31
+ return delivered;
32
+ }
33
+
34
+ function hold(host, notice) {
35
+ host.pending = host.pending.filter((held) => held.key !== notice.key);
36
+ host.pending.push(notice);
37
+ if (host.pending.length > PENDING_LIMIT) host.pending.shift();
38
+ }
39
+
40
+ /** Raise notice `code` (with string `params`) under `key`. False when that
41
+ * key is already raised. */
42
+ export function raiseDesktopNotice(key, code, params = {}) {
43
+ const host = registry();
44
+ const id = String(key || '');
45
+ if (!id || !code || host.raised.has(id)) return false;
46
+ host.raised.add(id);
47
+ const notice = {
48
+ key: id,
49
+ code: String(code),
50
+ params: Object.fromEntries(Object.entries(params || {}).map(([name, value]) => [name, String(value ?? '')])),
51
+ };
52
+ if (!offer(host, notice)) hold(host, notice);
53
+ return true;
54
+ }
55
+
56
+ /** The condition behind `key` is over: the next raise shows again. */
57
+ export function clearDesktopNotice(key) {
58
+ const host = registry();
59
+ const id = String(key || '');
60
+ if (!host.raised.delete(id)) return;
61
+ host.pending = host.pending.filter((notice) => notice.key !== id);
62
+ }
63
+
64
+ /** Offer every waiting notice again (a client just attached); the ones still
65
+ * undelivered keep waiting. */
66
+ export function deliverPendingDesktopNotices() {
67
+ const host = registry();
68
+ const pending = host.pending;
69
+ host.pending = [];
70
+ for (const notice of pending) if (!offer(host, notice, true)) hold(host, notice);
71
+ }
72
+
73
+ /** `sink(notice, replay)` receives every notice from now on, the waiting ones
74
+ * first (`replay` true); it returns false when it had nobody to deliver to.
75
+ * Returns the unregister. */
76
+ export function registerDesktopNoticeSink(sink) {
77
+ const host = registry();
78
+ const entry = { sink };
79
+ host.sinks.push(entry);
80
+ deliverPendingDesktopNotices();
81
+ return () => {
82
+ const index = host.sinks.indexOf(entry);
83
+ if (index >= 0) host.sinks.splice(index, 1);
84
+ };
85
+ }
86
+
87
+ registry().raise = raiseDesktopNotice;
@@ -10,7 +10,7 @@ import {
10
10
  } from 'node:fs/promises';
11
11
  import { basename, dirname, join } from 'node:path';
12
12
  import { randomBytes } from 'node:crypto';
13
- import { AsyncLocalStorage } from 'node:async_hooks';
13
+ import { runtimeAsyncLocalStorage } from './root-async-context.mjs';
14
14
  import { enforceOwnerOnlyAclWin32, enforceOwnerOnlyAclWin32Async } from './file-permissions.mjs';
15
15
  import { createKeyedSerialQueue } from './keyed-serial-queue.mjs';
16
16
  import { sleep, sleepSync } from './sleep.mjs';
@@ -34,7 +34,7 @@ const OWNER_TOKEN = randomBytes(12).toString('hex');
34
34
  const osHeldPaths = new Map();
35
35
  // In-process waiters queue per lock path before contending for the OS lock.
36
36
  const lockQueue = createKeyedSerialQueue();
37
- const heldLockPaths = new AsyncLocalStorage();
37
+ const heldLockPaths = runtimeAsyncLocalStorage();
38
38
  // A reclaim guard is held for a few metadata calls; one older than this whose
39
39
  // owner is dead was stranded by a process that died inside the reclaim.
40
40
  const RECLAIM_GUARD_STALE_MS = 30_000;
@@ -9,16 +9,14 @@
9
9
  // Capping old space makes V8 pace its major GCs against a realistic size. On
10
10
  // this exact runtime, one identical workload settled at RSS 401MB uncapped
11
11
  // versus 245MB under a 768MB cap (39% lower) while the live set was unchanged;
12
- // the gap between heapTotal and RSS fell from 274MB to 100MB. The desktop
13
- // renderer already banks the same win from its own cap — see the note above
14
- // `rendererHeapMb` in apps/desktop/src/main/index.ts, which reached this
15
- // conclusion first and named forced collection as the path that does NOT work.
12
+ // the gap between heapTotal and RSS fell from 274MB to 100MB.
16
13
  //
17
- // Memory and session-runtime keep caps well above their measured peaks so the
18
- // ceiling shapes GC pacing. The daemon is left to V8's own sizing by default:
19
- // a fixed 768MB ceiling was tight enough to risk avoidable exits. An explicit
20
- // `<ROLE>_HEAP_MB` still applies; `0` restores V8's own sizing, mirroring the
21
- // MIXDOG_RENDERER_HEAP_MB escape hatch.
14
+ // Every role is nevertheless left to V8's own sizing by default, like the
15
+ // desktop renderer (`rendererHeapMb` in apps/desktop/src/main/index.ts): a
16
+ // fixed ceiling turns a spike past it into an exit while the machine still
17
+ // has memory to spare, and an exit costs the work running in that process.
18
+ // An explicit `<ROLE>_HEAP_MB` still applies; `0` leaves V8's own sizing,
19
+ // mirroring the MIXDOG_RENDERER_HEAP_MB escape hatch.
22
20
 
23
21
  import { nonNegativeInt } from './numbers.mjs';
24
22
 
@@ -29,9 +27,9 @@ const ROLES = {
29
27
  // Default 0 leaves V8's own sizing; MIXDOG_DAEMON_HEAP_MB still applies.
30
28
  daemon: { env: 'MIXDOG_DAEMON_HEAP_MB', defaultMb: 0 },
31
29
  // Memory runtime observed resident at 259MB with the embedding model cold.
32
- memory: { env: 'MIXDOG_MEMORY_HEAP_MB', defaultMb: 512 },
30
+ memory: { env: 'MIXDOG_MEMORY_HEAP_MB', defaultMb: 0 },
33
31
  // Shard-mode session runtime holds the same transcripts the daemon would.
34
- 'session-runtime': { env: 'MIXDOG_SESSION_RUNTIME_HEAP_MB', defaultMb: 768 },
32
+ 'session-runtime': { env: 'MIXDOG_SESSION_RUNTIME_HEAP_MB', defaultMb: 0 },
35
33
  };
36
34
 
37
35
  /** Cap in MB for a role; 0 means "leave V8's own sizing alone". */
@@ -21,6 +21,7 @@
21
21
 
22
22
  import { createRequire } from 'node:module';
23
23
  import { BlockList, isIP } from 'node:net';
24
+ import { runInRootAsyncContext } from '../root-async-context.mjs';
24
25
 
25
26
  const require = createRequire(import.meta.url);
26
27
  let _undici = null;
@@ -371,10 +372,19 @@ function llmAgent() {
371
372
  _agent = null;
372
373
  _globalInstalled = false;
373
374
  }
374
- _agent ??= new (undici().Agent)(llmAgentOptions());
375
+ _agent ??= new (undici().Agent)({ ...llmAgentOptions(), connect: rootContextConnector() });
375
376
  return _agent;
376
377
  }
377
378
 
379
+ // A kept-alive socket serves later requests of any session: connect it in the
380
+ // root context so it never retains the request that opened it. buildConnector
381
+ // with no options matches the connector undici builds for an Agent without
382
+ // TLS/connect options.
383
+ function rootContextConnector() {
384
+ const connect = undici().buildConnector({});
385
+ return (options, callback) => runInRootAsyncContext(() => connect(options, callback));
386
+ }
387
+
378
388
  /**
379
389
  * The shared singleton dispatcher for LLM API requests. Created on first use.
380
390
  *
@@ -25,9 +25,10 @@
25
25
  * Any read failure means no system proxy.
26
26
  */
27
27
 
28
- import { execFileSync } from 'node:child_process';
28
+ import { execFile, execFileSync } from 'node:child_process';
29
29
  import { isIP } from 'node:net';
30
30
  import { join } from 'node:path';
31
+ import { promisify } from 'node:util';
31
32
  import { adoptPrivateProxy, isLocalHost, normalizeHost } from './http-agent.mjs';
32
33
 
33
34
  const INTERNET_SETTINGS_KEY = 'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings';
@@ -69,36 +70,51 @@ const lookups = new Map();
69
70
  let socksReported = false;
70
71
  let failureReported = false;
71
72
 
72
- /** The rows of `reg query <key>` output as a name → data map; null when the query failed. */
73
+ const execFileAsync = promisify(execFile);
74
+ const REG_QUERY_OPTIONS = {
75
+ encoding: 'utf8',
76
+ timeout: 2000,
77
+ maxBuffer: 256 * 1024,
78
+ windowsHide: true,
79
+ };
80
+
81
+ function regExe() {
82
+ return join(process.env.SystemRoot || 'C:\\Windows', 'System32', 'reg.exe');
83
+ }
84
+
85
+ /** The rows of `reg query` output as a name → data map. */
86
+ function parseRegistryOutput(out) {
87
+ const values = new Map();
88
+ for (const row of out.split(/\r?\n/)) {
89
+ // " Name REG_TYPE data" (data is absent for an empty string)
90
+ const match = row.match(/^ {4}(\S.*?) {4}(REG_[A-Z_]+)(?: {4}(.*))?$/);
91
+ if (match) values.set(match[1].toLowerCase(), (match[3] ?? '').trim());
92
+ }
93
+ return values;
94
+ }
95
+
96
+ /** `reg query <key>` as a name → data map; null when the query failed. */
73
97
  function queryRegistryKey(key) {
74
98
  try {
75
- const exe = join(process.env.SystemRoot || 'C:\\Windows', 'System32', 'reg.exe');
76
- const out = execFileSync(exe, ['query', key], {
77
- encoding: 'utf8',
78
- stdio: ['ignore', 'pipe', 'ignore'],
79
- timeout: 2000,
80
- maxBuffer: 256 * 1024,
81
- windowsHide: true,
82
- });
83
- const values = new Map();
84
- for (const row of out.split(/\r?\n/)) {
85
- // " Name REG_TYPE data" (data is absent for an empty string)
86
- const match = row.match(/^ {4}(\S.*?) {4}(REG_[A-Z_]+)(?: {4}(.*))?$/);
87
- if (match) values.set(match[1].toLowerCase(), (match[3] ?? '').trim());
88
- }
89
- return values;
99
+ return parseRegistryOutput(
100
+ execFileSync(regExe(), ['query', key], { ...REG_QUERY_OPTIONS, stdio: ['ignore', 'pipe', 'ignore'] })
101
+ );
90
102
  } catch {
91
103
  return null;
92
104
  }
93
105
  }
94
106
 
95
- /**
96
- * Raw registry values: `{ proxyEnable, proxyServer, proxyOverride, autoConfigUrl }`
97
- * (strings as `reg query` prints them, null when absent), or null when the
98
- * Internet Settings key cannot be read.
99
- */
100
- function readWindowsProxyRegistry() {
101
- const settings = queryRegistryKey(INTERNET_SETTINGS_KEY);
107
+ /** queryRegistryKey without holding the event loop for the reg.exe spawn. */
108
+ async function queryRegistryKeyAsync(key) {
109
+ try {
110
+ const { stdout } = await execFileAsync(regExe(), ['query', key], REG_QUERY_OPTIONS);
111
+ return parseRegistryOutput(stdout);
112
+ } catch {
113
+ return null;
114
+ }
115
+ }
116
+
117
+ function proxySettings(settings) {
102
118
  if (!settings) return null;
103
119
  return {
104
120
  proxyEnable: settings.get('proxyenable') ?? null,
@@ -108,6 +124,19 @@ function readWindowsProxyRegistry() {
108
124
  };
109
125
  }
110
126
 
127
+ /**
128
+ * Raw registry values: `{ proxyEnable, proxyServer, proxyOverride, autoConfigUrl }`
129
+ * (strings as `reg query` prints them, null when absent), or null when the
130
+ * Internet Settings key cannot be read.
131
+ */
132
+ function readWindowsProxyRegistry() {
133
+ return proxySettings(queryRegistryKey(INTERNET_SETTINGS_KEY));
134
+ }
135
+
136
+ async function readWindowsProxyRegistryAsync() {
137
+ return proxySettings(await queryRegistryKeyAsync(INTERNET_SETTINGS_KEY));
138
+ }
139
+
111
140
  const PROXY_ADDRESS = /^(https?:\/\/)?((?:[a-z0-9_-]+\.)*[a-z0-9_-]+|\[[0-9a-f:]+\]):(\d{1,5})$/i;
112
141
 
113
142
  function proxyUrl(address) {
@@ -256,6 +285,20 @@ function discover(reader, log) {
256
285
  } catch {
257
286
  return { proxy: null };
258
287
  }
288
+ return discovered(found, log);
289
+ }
290
+
291
+ async function discoverAsync(reader, log) {
292
+ let found;
293
+ try {
294
+ found = systemProxyFromRegistry(await reader());
295
+ } catch {
296
+ return { proxy: null };
297
+ }
298
+ return discovered(found, log);
299
+ }
300
+
301
+ function discovered(found, log) {
259
302
  if (!found.proxy) {
260
303
  if (!found.silent) log(`[mixdog] system proxy not used: ${found.reason}`);
261
304
  return { proxy: null };
@@ -387,6 +430,33 @@ export function adoptSystemProxy(
387
430
  apply = adoptPrivateProxy,
388
431
  } = {}
389
432
  ) {
433
+ const begun = beginAdoption(env, platform, logLine, apply);
434
+ return begun === DISCOVER ? finishAdoption(discover(reader, log)) : begun;
435
+ }
436
+
437
+ /**
438
+ * adoptSystemProxy for a process that may have to read the Windows registry
439
+ * (the daemon, the CLI): the reg.exe spawn is awaited instead of held on the
440
+ * event loop. Everything but that read is shared with adoptSystemProxy; the
441
+ * decision is settled when the returned promise resolves.
442
+ */
443
+ export async function adoptSystemProxyAsync(
444
+ env = process.env,
445
+ {
446
+ platform = process.platform,
447
+ reader = readWindowsProxyRegistryAsync,
448
+ log: logLine = (line) => process.stderr.write(`${line}\n`),
449
+ apply = adoptPrivateProxy,
450
+ } = {}
451
+ ) {
452
+ const begun = beginAdoption(env, platform, logLine, apply);
453
+ return begun === DISCOVER ? finishAdoption(await discoverAsync(reader, log)) : begun;
454
+ }
455
+
456
+ const DISCOVER = Symbol('discover');
457
+
458
+ /** Everything before the registry read: the final result, or DISCOVER when the registry decides. */
459
+ function beginAdoption(env, platform, logLine, apply) {
390
460
  const handoff = parseHandoff(env[HANDOFF_VAR]);
391
461
  delete env[HANDOFF_VAR];
392
462
  active = null;
@@ -402,7 +472,12 @@ export function adoptSystemProxy(
402
472
  active = { proxy: null };
403
473
  return false;
404
474
  }
405
- active = handoff ?? (platform === 'win32' ? discover(reader, log) : null);
475
+ if (!handoff && platform === 'win32') return DISCOVER;
476
+ return finishAdoption(handoff);
477
+ }
478
+
479
+ function finishAdoption(decision) {
480
+ active = decision;
406
481
  if (!active?.proxy) return false;
407
482
  return ensureRouting();
408
483
  }
@@ -72,9 +72,17 @@ export async function accountProviderSend(provider, instance, send, model, opts
72
72
  // that merely runs inside that async context (a tool or agent started
73
73
  // while the outer stream was live) asks for its own model.
74
74
  const outer = currentUsageContext();
75
+ // A session on a configured route-fallback hop (route-fallback.mjs) asks
76
+ // for the route the user configured, the primary one kept at the first hop,
77
+ // however many hops the chain has taken: that is what its answer replaces.
78
+ const primary = opts.session?.provider === provider ? opts.session.routeFallbackPrimary : null;
79
+ const routeHop = Boolean(primary?.provider && (primary.provider !== provider || primary.model !== model));
75
80
  const identity = {
76
81
  provider,
77
- requestedModel: (outer && opts[SEND_IDENTITY] === outer && outer.requestedModel) || model,
82
+ requestedProvider: routeHop ? primary.provider : provider,
83
+ requestedModel: routeHop
84
+ ? primary.model
85
+ : (outer && opts[SEND_IDENTITY] === outer && outer.requestedModel) || model,
78
86
  sessionId,
79
87
  sourceType,
80
88
  inputTokensInclusive,
@@ -104,12 +112,14 @@ export async function accountProviderSend(provider, instance, send, model, opts
104
112
  status = usageRequestStatus({
105
113
  failed: outcome.failed,
106
114
  attempts,
107
- substituted: servedModelSubstituted({
108
- provider,
109
- requestedModel: identity.requestedModel,
110
- servedModel,
111
- fallback: Boolean(result.providerMetadata?.anthropicFallbacks?.length),
112
- }),
115
+ substituted:
116
+ routeHop ||
117
+ servedModelSubstituted({
118
+ provider,
119
+ requestedModel: identity.requestedModel,
120
+ servedModel,
121
+ fallback: Boolean(result.providerMetadata?.anthropicFallbacks?.length),
122
+ }),
113
123
  });
114
124
  // The one measured request time: the loop's speed figures read it, so
115
125
  // the composer and the ledger show the same span.
@@ -122,6 +132,7 @@ export async function accountProviderSend(provider, instance, send, model, opts
122
132
  provider,
123
133
  model: servedModel,
124
134
  requestedModel: outcome ? identity.requestedModel : model,
135
+ requestedProvider: outcome ? identity.requestedProvider : provider,
125
136
  status,
126
137
  attempts,
127
138
  // Measured to the moment the stream returned, not to this write: the
@@ -1,7 +1,7 @@
1
- import { AsyncLocalStorage } from 'node:async_hooks';
1
+ import { runtimeAsyncLocalStorage } from '../root-async-context.mjs';
2
2
 
3
3
  // The selected route owns accounting identity, not an inner transport adapter.
4
- const context = new AsyncLocalStorage();
4
+ const context = runtimeAsyncLocalStorage();
5
5
  export const withUsageContext = (identity, send) => context.run(identity, send);
6
6
  export const currentUsageContext = () => context.getStore();
7
7
  // Each wire attempt stamps the tier it actually sent on its own send's
@@ -5,8 +5,10 @@
5
5
  * the page: a page of "all history" costs what a page of today does.
6
6
  *
7
7
  * The request facts (requested model, status, attempts) sit in
8
- * `usage_requests` and the first-output time in `usage_request_first_output`,
9
- * beside `usage_events` and keyed by the record id, so the record table keeps
8
+ * `usage_requests`, the first-output time in `usage_request_first_output` and
9
+ * the provider asked for, when a configured fallback hop made another provider
10
+ * answer, in `usage_request_requested_provider`, beside `usage_events` and
11
+ * keyed by the record id, so the record table keeps
10
12
  * the exact shape every earlier build writes: an older app still opens and
11
13
  * writes a ledger this one touched. A record without a request row is a
12
14
  * completed single attempt (every record from before).
@@ -28,6 +30,9 @@ export const REQUEST_LOG_SCHEMA = `
28
30
  CREATE TABLE IF NOT EXISTS usage_request_first_output (
29
31
  id BLOB PRIMARY KEY, ttft_ms REAL NOT NULL
30
32
  ) WITHOUT ROWID;
33
+ CREATE TABLE IF NOT EXISTS usage_request_requested_provider (
34
+ id BLOB PRIMARY KEY, provider TEXT NOT NULL
35
+ ) WITHOUT ROWID;
31
36
  CREATE INDEX IF NOT EXISTS usage_requests_status_time ON usage_requests(status, ts) WHERE status<>'completed';
32
37
  CREATE INDEX IF NOT EXISTS usage_events_route_time ON usage_events(route, ts);
33
38
  CREATE INDEX IF NOT EXISTS usage_routes_model ON usage_routes(json_extract(signature,'$[1]'));
@@ -50,7 +55,8 @@ const NOT_ABANDONED = `NOT (substr(e.id,1,1)=X'01' AND instr(e.id, CAST(':abando
50
55
  // generated in a few milliseconds, and an older process may still write it.
51
56
  const COLUMNS = `e.id,e.ts,e.route,e.session,e.input,e.output,e.cache_read,e.cache_write,e.cost_usd,e.duration_ms,
52
57
  r.requested_model,r.status,r.attempts,r.elapsed_ms,
53
- (SELECT f.ttft_ms FROM usage_request_first_output f WHERE f.id=e.id) AS first_output_ms`;
58
+ (SELECT f.ttft_ms FROM usage_request_first_output f WHERE f.id=e.id) AS first_output_ms,
59
+ (SELECT p.provider FROM usage_request_requested_provider p WHERE p.id=e.id) AS requested_provider`;
54
60
  const COMPLETED = `(r.status IS NULL OR r.status='completed')`;
55
61
  // Checked on the row the time-ordered read already joined: no other index.
56
62
  const LISTED = `(r.status IS NULL OR r.status<>'cancelled')`;
@@ -148,6 +154,7 @@ export function readUsageRequests(db, options = {}) {
148
154
  provider: route.provider || '',
149
155
  model: route.model || '',
150
156
  requestedModel: row.requested_model || route.model || '',
157
+ requestedProvider: row.requested_provider || '',
151
158
  kind: route.kind || '',
152
159
  sourceType: route.source_type || '',
153
160
  costSource: route.cost_source || '',
@@ -20,7 +20,12 @@ import {
20
20
  rollupUsage,
21
21
  } from './usage-ledger-rollup.mjs';
22
22
  import { QUOTA_SCHEMA, readQuotaHistory, readQuotaWindows, recordQuotaSamples } from './usage-ledger-quota.mjs';
23
- import { NO_USAGE_COST_SOURCE, REQUEST_LOG_SCHEMA, STORED_REQUEST_STATUSES, readUsageRequests } from './usage-ledger-requests.mjs';
23
+ import {
24
+ NO_USAGE_COST_SOURCE,
25
+ REQUEST_LOG_SCHEMA,
26
+ STORED_REQUEST_STATUSES,
27
+ readUsageRequests,
28
+ } from './usage-ledger-requests.mjs';
24
29
  import { SHARE_ENV } from 'node:worker_threads';
25
30
  import { createWorkerRequestClient } from '../worker-requests.mjs';
26
31
 
@@ -119,6 +124,7 @@ function compactEventWriter(db) {
119
124
  const insert = db.prepare('INSERT INTO usage_events VALUES (?,?,?,?,?,?,?,?,?,?,?)');
120
125
  const insertRequest = db.prepare('INSERT INTO usage_requests VALUES (?,?,?,?,?,?,?)');
121
126
  const insertFirstOutput = db.prepare('INSERT INTO usage_request_first_output VALUES (?,?)');
127
+ const insertRequestedProvider = db.prepare('INSERT INTO usage_request_requested_provider VALUES (?,?)');
122
128
  const insertOwner = db.prepare('INSERT OR IGNORE INTO usage_session_owners VALUES (?,?)');
123
129
  return (row) => {
124
130
  for (const key of ['id', 'provider', 'model', 'kind', 'source_type', 'cost_source', 'origin', 'session_id']) {
@@ -172,6 +178,8 @@ function compactEventWriter(db) {
172
178
  // The first-output time has its own table: only this build writes it, so
173
179
  // a row there is this build's measurement (usage-ledger-requests.mjs).
174
180
  if (row.ttft_ms != null) insertFirstOutput.run(id, row.ttft_ms);
181
+ // Likewise the provider a fallback hop was asked on: older builds never see it.
182
+ if (row.requested_provider) insertRequestedProvider.run(id, row.requested_provider);
175
183
  return true;
176
184
  };
177
185
  }
@@ -266,6 +274,7 @@ export function makeUsageRecord(args) {
266
274
  const provider = text(args.provider);
267
275
  const model = text(args.model);
268
276
  const requestedModel = text(args.requestedModel);
277
+ const requestedProvider = text(args.requestedProvider);
269
278
  if (!provider || !model) throw new Error('Usage requires a provider and model');
270
279
  const ts = Number(args.ts ?? Date.now());
271
280
  if (!Number.isSafeInteger(ts) || ts <= 0) throw new Error('Usage requires a valid timestamp');
@@ -339,6 +348,8 @@ export function makeUsageRecord(args) {
339
348
  // The request log (usage-ledger-requests.mjs): kept only when the
340
349
  // answering model differs from the one asked for.
341
350
  requestedModel: requestedModel !== model ? requestedModel : '',
351
+ // Only a route-fallback hop asks another provider.
352
+ requestedProvider: requestedProvider !== provider ? requestedProvider : '',
342
353
  status: STORED_REQUEST_STATUSES.includes(args.status) ? args.status : 'completed',
343
354
  attempts: Math.max(1, Math.floor(num(args.attempts)) || 1),
344
355
  ttftMs: args.ttftMs == null || !Number.isFinite(Number(args.ttftMs)) ? null : Math.max(0, Number(args.ttftMs)),
@@ -525,6 +536,7 @@ export class UsageLedger {
525
536
  rates: row.rates ? JSON.stringify(row.rates) : null,
526
537
  duration_ms: row.durationMs,
527
538
  requested_model: row.requestedModel,
539
+ requested_provider: row.requestedProvider,
528
540
  ttft_ms: row.ttftMs,
529
541
  elapsed_ms: row.elapsedMs,
530
542
  rank,
@@ -163,6 +163,28 @@ export function accountScopedSessionId(provider, accountId, sessionId) {
163
163
  return `account-${createHash('sha256').update(`${provider}:${accountId}:${sessionId}`).digest('hex')}`;
164
164
  }
165
165
 
166
+ // Visible session id -> the account-scoped ids it has sent under. Transports
167
+ // key pooled connections by the scoped id, which cannot be derived back, so
168
+ // closing the visible session looks them up here.
169
+ const accountScopedIdsBySession = new Map();
170
+
171
+ /** Record that `sessionId` sent under `scopedId` (account-pool.mjs). */
172
+ export function noteAccountScopedSessionId(sessionId, scopedId) {
173
+ let ids = accountScopedIdsBySession.get(sessionId);
174
+ if (!ids) {
175
+ ids = new Set();
176
+ accountScopedIdsBySession.set(sessionId, ids);
177
+ }
178
+ ids.add(scopedId);
179
+ }
180
+
181
+ /** The account-scoped ids `sessionId` has sent under; `forget` drops the record. */
182
+ export function accountScopedSessionIdsOf(sessionId, { forget = false } = {}) {
183
+ const ids = accountScopedIdsBySession.get(sessionId);
184
+ if (forget) accountScopedIdsBySession.delete(sessionId);
185
+ return ids ? [...ids] : [];
186
+ }
187
+
166
188
  export function providerAccountPath(provider, id, dataDir = resolvePluginData()) {
167
189
  requireAccountProvider(provider);
168
190
  requireAccountId(id);
@@ -1,8 +1,8 @@
1
- import { AsyncLocalStorage } from 'node:async_hooks';
2
1
  import { ACCOUNT_PROVIDERS, providerAccountPath, readProviderAccountPool } from './provider-accounts.mjs';
2
+ import { runtimeAsyncLocalStorage } from './root-async-context.mjs';
3
3
 
4
4
  const AUTH_BINDINGS = Symbol.for('mixdog.providerAuthBindings');
5
- const scopedBindings = new AsyncLocalStorage();
5
+ const scopedBindings = runtimeAsyncLocalStorage();
6
6
 
7
7
  export function hasExplicitProviderAuthBinding(provider) {
8
8
  return Boolean(globalThis[AUTH_BINDINGS]?.[provider]);
@@ -1,6 +1,6 @@
1
1
  import { availableParallelism, freemem, totalmem } from 'node:os';
2
- import { AsyncLocalStorage } from 'node:async_hooks';
3
2
  import { requestMemoryPressureSnapshot } from './memory-snapshot.mjs';
3
+ import { runtimeAsyncLocalStorage } from './root-async-context.mjs';
4
4
  import { nonNegativeInt, positiveInt } from './numbers.mjs';
5
5
 
6
6
  const MB = 1024 * 1024;
@@ -108,7 +108,7 @@ export class ResourceAdmissionController {
108
108
  this.queue = [];
109
109
  // Live leases for saturation diagnostics only (labels/ages in snapshot()).
110
110
  this.activeLeases = new Set();
111
- this.context = new AsyncLocalStorage();
111
+ this.context = runtimeAsyncLocalStorage();
112
112
  // Admission waits by pool: first acquisition (agent/shell) and the
113
113
  // reacquisition after a yielded provider call (restore). Shows whether
114
114
  // the agent cap actually delays work.
@@ -0,0 +1,51 @@
1
+ // The process's empty async context, for long-lived handles.
2
+ //
3
+ // A handle (socket, timer, worker port) keeps the async context it was created
4
+ // in for its whole life: every AsyncLocalStorage store active at creation stays
5
+ // reachable from it. A pooled connection or shared worker first opened inside
6
+ // one request would otherwise pin that request's session, transcript and tools
7
+ // until the handle dies, long after the session is deleted.
8
+ //
9
+ // Process entries (src/standalone/daemon.mjs, src/cli.mjs) capture the root
10
+ // with initRootAsyncContext() before any request exists. Where nothing did
11
+ // (another host, a test, a module first loaded inside a request), every
12
+ // runtime store is left instead, so no load order can capture a request.
13
+ import { AsyncLocalStorage, AsyncResource } from 'node:async_hooks';
14
+
15
+ // Shared by every copy of this module in the process, so a second copy (a late
16
+ // or duplicated import) sees the same root and the same stores.
17
+ const STATE_KEY = Symbol.for('mixdog.rootAsyncContext');
18
+ globalThis[STATE_KEY] ??= { root: null, stores: new Set() };
19
+ const state = globalThis[STATE_KEY];
20
+
21
+ /** An AsyncLocalStorage the root context leaves: every runtime store is made here. */
22
+ export function runtimeAsyncLocalStorage() {
23
+ const store = new AsyncLocalStorage();
24
+ // Weak: per-instance stores (resource-admission controllers) come and go.
25
+ state.stores.add(new WeakRef(store));
26
+ return store;
27
+ }
28
+
29
+ function activeStores() {
30
+ const active = [];
31
+ for (const ref of state.stores) {
32
+ const store = ref.deref();
33
+ if (!store) state.stores.delete(ref);
34
+ else if (store.getStore() !== undefined) active.push(store);
35
+ }
36
+ return active;
37
+ }
38
+
39
+ /** Capture the current context as the root; refused (false) inside a request. */
40
+ export function initRootAsyncContext() {
41
+ if (state.root) return true;
42
+ if (activeStores().length > 0) return false;
43
+ state.root = new AsyncResource('MixdogRootContext');
44
+ return true;
45
+ }
46
+
47
+ /** Run `fn` (synchronously) in the root context; returns its result. */
48
+ export function runInRootAsyncContext(fn) {
49
+ if (state.root) return state.root.runInAsyncScope(fn);
50
+ return activeStores().reduce((inner, store) => () => store.exit(inner), fn)();
51
+ }