mixdog 0.9.95 → 0.9.96

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 (178) hide show
  1. package/LICENSES/Apache-2.0.txt +201 -0
  2. package/LICENSES/MIT.txt +56 -0
  3. package/LICENSES/codex-NOTICE.txt +6 -0
  4. package/NOTICE.md +46 -9
  5. package/package.json +12 -9
  6. package/scripts/.tmp-coverage-report.mjs +96 -0
  7. package/scripts/build-tui.mjs +17 -1
  8. package/scripts/fixtures/patch-replay-corpus.json +98 -0
  9. package/src/defaults/mixdog-config.template.json +5 -8
  10. package/src/rules/agent/30-explorer.md +7 -4
  11. package/src/rules/shared/01-tool.md +20 -29
  12. package/src/runtime/agent/orchestrator/agent-runtime/agent-dispatch.mjs +18 -2
  13. package/src/runtime/agent/orchestrator/agent-runtime/agent-progress-watchdog.mjs +31 -1
  14. package/src/runtime/agent/orchestrator/agent-runtime/maintenance-route.mjs +6 -17
  15. package/src/runtime/agent/orchestrator/agent-trace-format.mjs +12 -1
  16. package/src/runtime/agent/orchestrator/config.mjs +246 -67
  17. package/src/runtime/agent/orchestrator/mcp/client.mjs +26 -11
  18. package/src/runtime/agent/orchestrator/mcp/reconnect-singleflight.mjs +23 -0
  19. package/src/runtime/agent/orchestrator/providers/admission-scheduler.mjs +113 -14
  20. package/src/runtime/agent/orchestrator/providers/anthropic-sse.mjs +5 -1
  21. package/src/runtime/agent/orchestrator/providers/gemini-stream.mjs +32 -11
  22. package/src/runtime/agent/orchestrator/providers/gemini.mjs +89 -2
  23. package/src/runtime/agent/orchestrator/providers/grok-oauth.mjs +12 -1
  24. package/src/runtime/agent/orchestrator/providers/openai-compat-xai.mjs +19 -1
  25. package/src/runtime/agent/orchestrator/providers/openai-compat.mjs +144 -39
  26. package/src/runtime/agent/orchestrator/providers/openai-oauth-http-sse.mjs +22 -8
  27. package/src/runtime/agent/orchestrator/providers/openai-oauth.mjs +15 -1
  28. package/src/runtime/agent/orchestrator/providers/openai-transport-policy.mjs +1 -1
  29. package/src/runtime/agent/orchestrator/providers/openai-ws-pool.mjs +3 -4
  30. package/src/runtime/agent/orchestrator/providers/retry-classifier.mjs +67 -7
  31. package/src/runtime/agent/orchestrator/providers/stream-json-pool.mjs +291 -0
  32. package/src/runtime/agent/orchestrator/providers/stream-json-worker.mjs +21 -0
  33. package/src/runtime/agent/orchestrator/session/agent-loop.mjs +28 -4
  34. package/src/runtime/agent/orchestrator/session/eager-dispatch.mjs +4 -33
  35. package/src/runtime/agent/orchestrator/session/loop/pre-dispatch-deny.mjs +13 -0
  36. package/src/runtime/agent/orchestrator/session/loop/termination.mjs +2 -2
  37. package/src/runtime/agent/orchestrator/session/loop/tool-classify.mjs +0 -24
  38. package/src/runtime/agent/orchestrator/session/loop/tool-exec.mjs +1 -27
  39. package/src/runtime/agent/orchestrator/session/loop/tool-helpers.mjs +6 -10
  40. package/src/runtime/agent/orchestrator/session/manager/ask-session.mjs +5 -0
  41. package/src/runtime/agent/orchestrator/session/manager/pending-messages.mjs +142 -84
  42. package/src/runtime/agent/orchestrator/session/manager/runtime-loaders.mjs +4 -0
  43. package/src/runtime/agent/orchestrator/session/manager/session-crud.mjs +31 -1
  44. package/src/runtime/agent/orchestrator/session/manager/turn-interruption.mjs +29 -9
  45. package/src/runtime/agent/orchestrator/session/manager.mjs +2 -0
  46. package/src/runtime/agent/orchestrator/session/result-classification.mjs +65 -0
  47. package/src/runtime/agent/orchestrator/session/send-with-recovery.mjs +86 -8
  48. package/src/runtime/agent/orchestrator/session/store-summary-reader.mjs +12 -0
  49. package/src/runtime/agent/orchestrator/session/tool-batch.mjs +3 -33
  50. package/src/runtime/agent/orchestrator/session/tool-result-offload.mjs +4 -5
  51. package/src/runtime/agent/orchestrator/stall-policy.mjs +1 -1
  52. package/src/runtime/agent/orchestrator/tools/bash-session.mjs +2 -2
  53. package/src/runtime/agent/orchestrator/tools/builtin/arg-guard.mjs +35 -1
  54. package/src/runtime/agent/orchestrator/tools/builtin/bash-tool.mjs +118 -18
  55. package/src/runtime/agent/orchestrator/tools/builtin/builtin-tools.mjs +8 -9
  56. package/src/runtime/agent/orchestrator/tools/builtin/cache-layers.mjs +2 -2
  57. package/src/runtime/agent/orchestrator/tools/builtin/device-paths.mjs +2 -2
  58. package/src/runtime/agent/orchestrator/tools/builtin/lib/grep-context-expander.mjs +6 -15
  59. package/src/runtime/agent/orchestrator/tools/builtin/list-tool.mjs +10 -26
  60. package/src/runtime/agent/orchestrator/tools/builtin/read-image.mjs +1 -1
  61. package/src/runtime/agent/orchestrator/tools/builtin/read-single-tool.mjs +3 -3
  62. package/src/runtime/agent/orchestrator/tools/builtin/read-tool.mjs +1 -23
  63. package/src/runtime/agent/orchestrator/tools/builtin/rg-runner.mjs +23 -9
  64. package/src/runtime/agent/orchestrator/tools/builtin/search-path-diagnostics.mjs +7 -0
  65. package/src/runtime/agent/orchestrator/tools/builtin/search-tool.mjs +17 -39
  66. package/src/runtime/agent/orchestrator/tools/builtin/shell-analysis.mjs +73 -0
  67. package/src/runtime/agent/orchestrator/tools/builtin/shell-job-paths.mjs +1 -1
  68. package/src/runtime/agent/orchestrator/tools/builtin/shell-job-spawn.mjs +0 -2
  69. package/src/runtime/agent/orchestrator/tools/builtin/shell-jobs.mjs +3 -8
  70. package/src/runtime/agent/orchestrator/tools/builtin/shell-runtime.mjs +30 -0
  71. package/src/runtime/agent/orchestrator/tools/code-graph/build.mjs +5 -5
  72. package/src/runtime/agent/orchestrator/tools/code-graph/dispatch.mjs +87 -15
  73. package/src/runtime/agent/orchestrator/tools/code-graph/search.mjs +6 -15
  74. package/src/runtime/agent/orchestrator/tools/code-graph-tool-defs.mjs +2 -2
  75. package/src/runtime/agent/orchestrator/tools/lib/pwsh-standby-pool.mjs +83 -13
  76. package/src/runtime/agent/orchestrator/tools/patch/matcher.mjs +39 -1
  77. package/src/runtime/agent/orchestrator/tools/patch/orchestrator.mjs +29 -1
  78. package/src/runtime/agent/orchestrator/tools/patch/parsing.mjs +16 -0
  79. package/src/runtime/agent/orchestrator/tools/patch/v4a-convert.mjs +108 -12
  80. package/src/runtime/agent/orchestrator/tools/patch-tool-defs.mjs +10 -10
  81. package/src/runtime/agent/orchestrator/tools/shell-command.mjs +56 -8
  82. package/src/runtime/agent/orchestrator/tools/shell-exec-output.mjs +2 -2
  83. package/src/runtime/channels/lib/config.mjs +2 -2
  84. package/src/runtime/channels/lib/inbound-handler.mjs +1 -1
  85. package/src/runtime/channels/lib/scheduler.mjs +2 -2
  86. package/src/runtime/memory/index.mjs +5 -38
  87. package/src/runtime/memory/lib/agent-ipc.mjs +145 -81
  88. package/src/runtime/memory/lib/compact-vector-cache.mjs +88 -0
  89. package/src/runtime/memory/lib/core-memory-store.mjs +18 -1
  90. package/src/runtime/memory/lib/cycle-llm-adapters.mjs +12 -33
  91. package/src/runtime/memory/lib/embedding-provider.mjs +47 -26
  92. package/src/runtime/memory/lib/embedding-worker.mjs +30 -34
  93. package/src/runtime/memory/lib/http-router.mjs +8 -0
  94. package/src/runtime/memory/lib/ko-morph.mjs +3 -20
  95. package/src/runtime/memory/lib/memory-action-handlers.mjs +2 -1
  96. package/src/runtime/memory/lib/memory-cycle1.mjs +1 -0
  97. package/src/runtime/memory/lib/memory-cycle2-gate.mjs +4 -2
  98. package/src/runtime/memory/lib/memory-cycle2-mutations.mjs +1 -0
  99. package/src/runtime/memory/lib/memory-cycle3.mjs +3 -1
  100. package/src/runtime/memory/lib/pg/process.mjs +32 -9
  101. package/src/runtime/memory/tool-defs.mjs +1 -5
  102. package/src/runtime/search/tool-defs.mjs +2 -2
  103. package/src/runtime/shared/agent-route-config.mjs +119 -0
  104. package/src/runtime/shared/atomic-file.mjs +80 -4
  105. package/src/runtime/shared/channel-notification-routing.mjs +2 -2
  106. package/src/runtime/shared/child-guardian.mjs +204 -108
  107. package/src/runtime/shared/child-spawn-gate.mjs +60 -31
  108. package/src/runtime/shared/config.mjs +130 -44
  109. package/src/runtime/shared/resource-admission.mjs +146 -97
  110. package/src/runtime/shared/stream-progress.mjs +6 -0
  111. package/src/runtime/shared/tool-surface.mjs +0 -5
  112. package/src/runtime/shared/turn-snapshot.mjs +31 -5
  113. package/src/runtime/shared/turn-worktree-snapshot.mjs +14 -2
  114. package/src/session-runtime/config-helpers.mjs +39 -37
  115. package/src/session-runtime/model-route-api.mjs +7 -5
  116. package/src/session-runtime/provider-auth-api.mjs +0 -7
  117. package/src/session-runtime/runtime-core.mjs +11 -11
  118. package/src/session-runtime/runtime-tunables.mjs +4 -4
  119. package/src/session-runtime/session-lifecycle.mjs +9 -2
  120. package/src/session-runtime/session-turn-api.mjs +41 -9
  121. package/src/session-runtime/settings-api.mjs +5 -17
  122. package/src/session-runtime/tool-defs.mjs +3 -3
  123. package/src/session-runtime/workflow-agents-api.mjs +55 -81
  124. package/src/session-runtime/workflow.mjs +14 -24
  125. package/src/standalone/agent-dispatch-broker.mjs +198 -0
  126. package/src/standalone/agent-tool/helpers.mjs +2 -2
  127. package/src/standalone/agent-tool/spawn-flow.mjs +2 -0
  128. package/src/standalone/agent-tool/spawn-preset.mjs +11 -21
  129. package/src/standalone/agent-tool/tool-def.mjs +0 -14
  130. package/src/standalone/backend-daemon.mjs +97 -36
  131. package/src/standalone/channel-admin.mjs +12 -12
  132. package/src/standalone/channel-daemon-client.mjs +17 -1
  133. package/src/standalone/channel-daemon-transport.mjs +90 -8
  134. package/src/standalone/engine-daemon-client.mjs +380 -205
  135. package/src/standalone/engine-daemon-protocol.mjs +3 -4
  136. package/src/standalone/engine-daemon-service.mjs +378 -134
  137. package/src/standalone/engine-daemon-transport.mjs +268 -77
  138. package/src/standalone/explore-tool.mjs +187 -70
  139. package/src/standalone/fair-call-scheduler.mjs +264 -0
  140. package/src/standalone/session-protocol.mjs +188 -0
  141. package/src/tui/App.jsx +30 -17
  142. package/src/tui/app/app-view.jsx +5 -2
  143. package/src/tui/app/core-memory-picker.mjs +19 -19
  144. package/src/tui/app/doctor.mjs +5 -11
  145. package/src/tui/app/message-selector.mjs +103 -0
  146. package/src/tui/app/onboarding-steps.mjs +1 -2
  147. package/src/tui/app/project-picker.mjs +80 -56
  148. package/src/tui/app/prompt-submit.mjs +46 -41
  149. package/src/tui/app/resume-picker.mjs +2 -2
  150. package/src/tui/app/route-pickers.mjs +3 -1
  151. package/src/tui/app/settings-picker.mjs +6 -8
  152. package/src/tui/app/transcript-window.mjs +38 -0
  153. package/src/tui/app/use-prompt-draft-flow.mjs +5 -7
  154. package/src/tui/app/use-prompt-handlers.mjs +86 -21
  155. package/src/tui/app/use-prompt-hint.mjs +3 -2
  156. package/src/tui/app/use-prompt-queue-history.mjs +10 -3
  157. package/src/tui/app/use-transcript-scroll.mjs +9 -2
  158. package/src/tui/app/use-transcript-window.mjs +50 -17
  159. package/src/tui/components/PromptInput.jsx +71 -38
  160. package/src/tui/components/Spinner.jsx +1 -1
  161. package/src/tui/components/prompt-input/edit-helpers.mjs +9 -11
  162. package/src/tui/components/prompt-input/escape-policy.mjs +42 -0
  163. package/src/tui/components/prompt-input/immediate-render.mjs +0 -10
  164. package/src/tui/components/prompt-input/interrupt-policy.mjs +10 -0
  165. package/src/tui/components/prompt-input/restore-policy.mjs +10 -0
  166. package/src/tui/dist/index.mjs +920 -1592
  167. package/src/tui/engine/live-share.mjs +34 -0
  168. package/src/tui/engine/session-api-ext.mjs +1 -4
  169. package/src/tui/engine/session-api.mjs +103 -33
  170. package/src/tui/engine/session-flow.mjs +7 -1
  171. package/src/tui/engine/tool-card-results.mjs +4 -2
  172. package/src/tui/engine/tool-result-text.mjs +10 -0
  173. package/src/tui/engine/turn.mjs +9 -69
  174. package/src/tui/engine-local-session.mjs +47 -31
  175. package/src/tui/index.jsx +1 -2
  176. package/src/tui/spinner-meta.mjs +4 -4
  177. package/src/tui/spinner-verbs.mjs +51 -3
  178. package/src/standalone/engine-daemon-local-bridge.mjs +0 -20
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  import { spawn } from 'node:child_process';
4
+ import { randomUUID } from 'node:crypto';
4
5
  import { detachedSpawnOpts } from './spawn-flags.mjs';
5
6
 
6
7
  function positiveInt(value) {
@@ -8,13 +9,6 @@ function positiveInt(value) {
8
9
  return Number.isInteger(n) && n > 0 ? n : null;
9
10
  }
10
11
 
11
- const DEFAULT_MIN_FREE_MEMORY_MB = 1024;
12
-
13
- export function childGuardianMemoryFloorMb(env = process.env) {
14
- const n = Math.floor(Number(env.MIXDOG_MIN_FREE_MEMORY_MB));
15
- return Number.isFinite(n) && n >= 0 ? n : DEFAULT_MIN_FREE_MEMORY_MB;
16
- }
17
-
18
12
  export function childGuardianSpawnEnv(env = process.env) {
19
13
  return {
20
14
  PATH: env.PATH || '',
@@ -29,40 +23,25 @@ export function childGuardianSpawnEnv(env = process.env) {
29
23
  };
30
24
  }
31
25
 
32
- function guardianScript({
33
- parentPid,
34
- childPid,
35
- childGroupPid,
36
- platform,
37
- pollMs,
38
- orphanGraceMs,
39
- forceGraceMs,
40
- minFreeMemoryMb,
41
- receiptPath,
42
- }) {
26
+ function guardianBrokerScript() {
43
27
  return `
44
28
  const { spawnSync } = require('node:child_process');
45
- const { freemem } = require('node:os');
46
- const parentPid = ${JSON.stringify(parentPid)};
47
- const childPid = ${JSON.stringify(childPid)};
48
- const childGroupPid = ${JSON.stringify(childGroupPid || childPid)};
49
- const platform = ${JSON.stringify(platform)};
50
- const pollMs = ${JSON.stringify(pollMs)};
51
- const orphanGraceMs = ${JSON.stringify(orphanGraceMs)};
52
- const forceGraceMs = ${JSON.stringify(forceGraceMs)};
53
- const minFreeMemoryBytes = ${JSON.stringify(minFreeMemoryMb * 1024 * 1024)};
54
- const receiptPath = ${JSON.stringify(receiptPath || null)};
29
+ const platform = ${JSON.stringify(process.platform)};
30
+ const targets = new Map();
31
+ let input = '';
32
+ let everHadTarget = false;
33
+ let emptySince = 0;
55
34
  // Kill receipt: guardian kills are otherwise indistinguishable from a crashed
56
35
  // wrapper ("process exited without reporting an exit code"). Written BEFORE
57
36
  // the kill so the owner's next status refresh can attribute the death.
58
- function writeReceipt(reason, extra) {
59
- if (!receiptPath) return;
37
+ function writeReceipt(target, reason, extra) {
38
+ if (!target.receiptPath) return;
60
39
  try {
61
- require('node:fs').writeFileSync(receiptPath, JSON.stringify({
40
+ require('node:fs').writeFileSync(target.receiptPath, JSON.stringify({
62
41
  reason,
63
42
  at: new Date().toISOString(),
64
43
  guardianPid: process.pid,
65
- childPid,
44
+ childPid: target.childPid,
66
45
  ...extra,
67
46
  }));
68
47
  } catch {}
@@ -72,57 +51,186 @@ function alive(pid) {
72
51
  try { process.kill(pid, 0); return true; }
73
52
  catch (error) { return error && error.code === 'EPERM'; }
74
53
  }
75
- function killTarget(force) {
54
+ function killTarget(target, force) {
76
55
  if (platform === 'win32') {
77
56
  const systemRoot = process.env.SystemRoot || process.env.WINDIR || 'C:\\\\Windows';
78
57
  const taskkill = systemRoot + '\\\\System32\\\\taskkill.exe';
79
58
  if (force) {
80
- try { spawnSync(taskkill, ['/PID', String(childPid), '/T', '/F'], { stdio: 'ignore', windowsHide: true }); } catch {}
59
+ try { spawnSync(taskkill, ['/PID', String(target.childPid), '/T', '/F'], { stdio: 'ignore', windowsHide: true }); } catch {}
81
60
  } else {
82
- try { process.kill(childPid, 'SIGTERM'); } catch {}
83
- try { spawnSync(taskkill, ['/PID', String(childPid), '/T'], { stdio: 'ignore', windowsHide: true }); } catch {}
61
+ try { process.kill(target.childPid, 'SIGTERM'); } catch {}
62
+ try { spawnSync(taskkill, ['/PID', String(target.childPid), '/T'], { stdio: 'ignore', windowsHide: true }); } catch {}
84
63
  }
85
64
  return;
86
65
  }
87
66
  const signal = force ? 'SIGKILL' : 'SIGTERM';
88
- try { process.kill(-childGroupPid, signal); return; } catch {}
89
- try { process.kill(childPid, signal); } catch {}
67
+ try { process.kill(-target.childGroupPid, signal); return; } catch {}
68
+ try { process.kill(target.childPid, signal); } catch {}
69
+ }
70
+ function accept(message) {
71
+ if (!message || typeof message !== 'object') return;
72
+ const id = String(message.id || '');
73
+ if (!id) return;
74
+ if (message.type === 'remove') {
75
+ targets.delete(id);
76
+ return;
77
+ }
78
+ if (message.type !== 'add') return;
79
+ const childPid = Number(message.childPid);
80
+ const parentPid = Number(message.parentPid);
81
+ if (!Number.isInteger(childPid) || childPid <= 0
82
+ || !Number.isInteger(parentPid) || parentPid <= 0) return;
83
+ targets.set(id, {
84
+ id,
85
+ parentPid,
86
+ childPid,
87
+ childGroupPid: Number(message.childGroupPid) || childPid,
88
+ pollMs: Math.max(100, Number(message.pollMs) || 750),
89
+ orphanGraceMs: Math.max(100, Number(message.orphanGraceMs) || 3000),
90
+ forceGraceMs: Math.max(100, Number(message.forceGraceMs) || 3000),
91
+ receiptPath: typeof message.receiptPath === 'string' ? message.receiptPath : null,
92
+ orphanedAt: 0,
93
+ lastPolledAt: 0,
94
+ killing: false,
95
+ });
96
+ everHadTarget = true;
97
+ emptySince = 0;
90
98
  }
91
- let killing = false;
92
- let orphanedAt = 0;
99
+ process.stdin.setEncoding('utf8');
100
+ process.stdin.on('data', (chunk) => {
101
+ input += chunk;
102
+ let newline;
103
+ while ((newline = input.indexOf('\\n')) >= 0) {
104
+ const line = input.slice(0, newline);
105
+ input = input.slice(newline + 1);
106
+ try { accept(JSON.parse(line)); } catch {}
107
+ }
108
+ });
93
109
  const timer = setInterval(() => {
94
- if (!alive(childPid)) process.exit(0);
95
- // Admission protects only NEW work. A running shell can still consume the
96
- // host after it starts, so reclaim its entire tree before it starves the
97
- // owning Mixdog process. Force is intentional: at the memory floor there is
98
- // no safe grace window for a runaway allocator.
99
- if (!killing && minFreeMemoryBytes > 0) {
100
- let freeBytes = Number.POSITIVE_INFINITY;
101
- try { freeBytes = Number(freemem()); } catch {}
102
- if (Number.isFinite(freeBytes) && freeBytes < minFreeMemoryBytes) {
103
- killing = true;
104
- writeReceipt('host-memory-floor', { freeBytes, minFreeMemoryBytes });
105
- killTarget(true);
106
- process.exit(0);
110
+ const now = Date.now();
111
+ for (const [id, target] of targets) {
112
+ if (now - target.lastPolledAt < target.pollMs) continue;
113
+ target.lastPolledAt = now;
114
+ if (!alive(target.childPid)) {
115
+ targets.delete(id);
116
+ continue;
117
+ }
118
+ if (alive(target.parentPid)) {
119
+ target.orphanedAt = 0;
120
+ continue;
107
121
  }
122
+ if (!target.orphanedAt) target.orphanedAt = now;
123
+ if (target.killing || now - target.orphanedAt < target.orphanGraceMs) continue;
124
+ target.killing = true;
125
+ writeReceipt(target, 'parent-exit', {});
126
+ killTarget(target, false);
127
+ setTimeout(() => {
128
+ if (alive(target.childPid)) killTarget(target, true);
129
+ targets.delete(id);
130
+ }, target.forceGraceMs).unref?.();
108
131
  }
109
- // Orphaned when the parent PID dies. Matches mainstream CLIs: no console
110
- // probe — a hidden shell spawn (windowsHide) plus tree-kill on cleanup, so
111
- // orphan detection is pure parent-liveness.
112
- if (alive(parentPid)) {
113
- orphanedAt = 0;
114
- return;
132
+ // Pay for one Node supervisor only while it owns at least one live child.
133
+ // A short empty grace absorbs command-to-command reuse without keeping the
134
+ // broker resident for the whole daemon lifetime.
135
+ if (targets.size > 0) emptySince = 0;
136
+ else if (everHadTarget) {
137
+ if (!emptySince) emptySince = now;
138
+ else if (now - emptySince >= 1000) process.exit(0);
115
139
  }
116
- if (!orphanedAt) orphanedAt = Date.now();
117
- if (killing || Date.now() - orphanedAt < orphanGraceMs) return;
118
- killing = true;
119
- writeReceipt('parent-exit', {});
120
- killTarget(false);
121
- setTimeout(() => { if (alive(childPid)) killTarget(true); process.exit(0); }, forceGraceMs).unref?.();
122
- }, pollMs);
140
+ }, 100);
123
141
  `;
124
142
  }
125
143
 
144
+ let sharedBroker = null;
145
+ let brokerRestartTimer = null;
146
+ let brokerTargetSweepTimer = null;
147
+ const brokerTargets = new Map();
148
+
149
+ function sendBrokerMessage(message) {
150
+ try {
151
+ if (!sharedBroker?.stdin?.writable) return false;
152
+ sharedBroker.stdin.write(`${JSON.stringify(message)}\n`);
153
+ return true;
154
+ } catch {
155
+ return false;
156
+ }
157
+ }
158
+
159
+ function targetPidAlive(pid) {
160
+ try {
161
+ process.kill(pid, 0);
162
+ return true;
163
+ } catch (error) {
164
+ return error?.code === 'EPERM';
165
+ }
166
+ }
167
+
168
+ function stopBrokerTargetSweepIfIdle() {
169
+ if (brokerTargets.size > 0 || !brokerTargetSweepTimer) return;
170
+ clearInterval(brokerTargetSweepTimer);
171
+ brokerTargetSweepTimer = null;
172
+ }
173
+
174
+ function removeBrokerTarget(id, { notify = true } = {}) {
175
+ const target = brokerTargets.get(id);
176
+ if (!target) return false;
177
+ brokerTargets.delete(id);
178
+ try { target.onRemoved?.(); } catch {}
179
+ if (notify) sendBrokerMessage({ type: 'remove', id });
180
+ stopBrokerTargetSweepIfIdle();
181
+ return true;
182
+ }
183
+
184
+ function ensureBrokerTargetSweep() {
185
+ if (brokerTargetSweepTimer) return;
186
+ brokerTargetSweepTimer = setInterval(() => {
187
+ for (const [id, target] of brokerTargets) {
188
+ if (!targetPidAlive(target.childPid)) removeBrokerTarget(id);
189
+ }
190
+ stopBrokerTargetSweepIfIdle();
191
+ }, 250);
192
+ brokerTargetSweepTimer.unref?.();
193
+ }
194
+
195
+ function ensureSharedBroker() {
196
+ if (sharedBroker && sharedBroker.exitCode == null && sharedBroker.signalCode == null) {
197
+ return sharedBroker;
198
+ }
199
+ try {
200
+ const broker = spawn(process.execPath, [
201
+ '--no-warnings',
202
+ '--eval',
203
+ guardianBrokerScript(),
204
+ ], {
205
+ stdio: ['pipe', 'ignore', 'ignore'],
206
+ env: childGuardianSpawnEnv(),
207
+ ...detachedSpawnOpts,
208
+ });
209
+ sharedBroker = broker;
210
+ broker.unref?.();
211
+ broker.stdin?.unref?.();
212
+ broker.stdin?.on?.('error', () => {});
213
+ broker.once('exit', () => {
214
+ if (sharedBroker !== broker) return;
215
+ sharedBroker = null;
216
+ if (brokerTargets.size > 0 && !brokerRestartTimer) {
217
+ brokerRestartTimer = setTimeout(() => {
218
+ brokerRestartTimer = null;
219
+ const replacement = ensureSharedBroker();
220
+ if (!replacement) return;
221
+ for (const target of brokerTargets.values()) sendBrokerMessage(target);
222
+ }, 100);
223
+ brokerRestartTimer.unref?.();
224
+ }
225
+ });
226
+ for (const target of brokerTargets.values()) sendBrokerMessage(target);
227
+ return broker;
228
+ } catch {
229
+ sharedBroker = null;
230
+ return null;
231
+ }
232
+ }
233
+
126
234
  export function startChildGuardian({
127
235
  parentPid = process.pid,
128
236
  childPid,
@@ -132,53 +240,41 @@ export function startChildGuardian({
132
240
  graceMs = 3000,
133
241
  orphanGraceMs = graceMs,
134
242
  forceGraceMs = graceMs,
135
- protectHostMemory = false,
136
- minFreeMemoryMb = protectHostMemory ? childGuardianMemoryFloorMb() : 0,
137
243
  receiptPath = null,
138
244
  } = {}) {
139
245
  const parent = positiveInt(parentPid);
140
246
  const child = positiveInt(childPid);
141
247
  if (!parent || !child || parent === child) return null;
142
- const memoryFloorMb = Math.max(0, Math.floor(Number(minFreeMemoryMb) || 0));
143
-
144
- try {
145
- const guardian = spawn(process.execPath, [
146
- '--no-warnings',
147
- '--eval',
148
- guardianScript({
149
- parentPid: parent,
150
- childPid: child,
151
- childGroupPid: positiveInt(childGroupPid) || child,
152
- platform: process.platform,
153
- pollMs: Math.max(100, Math.floor(Number(pollMs) || 750)),
154
- orphanGraceMs: Math.max(100, Math.floor(Number(orphanGraceMs) || Number(graceMs) || 3000)),
155
- forceGraceMs: Math.max(100, Math.floor(Number(forceGraceMs) || Number(graceMs) || 3000)),
156
- minFreeMemoryMb: memoryFloorMb,
157
- receiptPath: typeof receiptPath === 'string' && receiptPath ? receiptPath : null,
158
- }),
159
- ], {
160
- stdio: 'ignore',
161
- env: childGuardianSpawnEnv(),
162
- ...detachedSpawnOpts,
163
- });
164
- guardian.unref?.();
165
- let stopped = false;
166
- return {
167
- pid: guardian.pid || null,
168
- label,
169
- childPid: child,
170
- stop() {
171
- if (stopped) return false;
172
- stopped = true;
173
- try {
174
- guardian.kill();
175
- return true;
176
- } catch {
177
- return false;
178
- }
179
- },
180
- };
181
- } catch {
248
+ const id = randomUUID();
249
+ let stopped = false;
250
+ const target = {
251
+ type: 'add',
252
+ id,
253
+ parentPid: parent,
254
+ childPid: child,
255
+ childGroupPid: positiveInt(childGroupPid) || child,
256
+ pollMs: Math.max(100, Math.floor(Number(pollMs) || 750)),
257
+ orphanGraceMs: Math.max(100, Math.floor(Number(orphanGraceMs) || Number(graceMs) || 3000)),
258
+ forceGraceMs: Math.max(100, Math.floor(Number(forceGraceMs) || Number(graceMs) || 3000)),
259
+ receiptPath: typeof receiptPath === 'string' && receiptPath ? receiptPath : null,
260
+ onRemoved: () => { stopped = true; },
261
+ };
262
+ brokerTargets.set(id, target);
263
+ ensureBrokerTargetSweep();
264
+ const broker = ensureSharedBroker();
265
+ if (!broker) {
266
+ removeBrokerTarget(id, { notify: false });
182
267
  return null;
183
268
  }
269
+ sendBrokerMessage(target);
270
+ return {
271
+ pid: broker.pid || null,
272
+ label,
273
+ childPid: child,
274
+ stop() {
275
+ if (stopped) return false;
276
+ stopped = true;
277
+ return removeBrokerTarget(id);
278
+ },
279
+ };
184
280
  }
@@ -6,40 +6,56 @@
6
6
  // singleton semaphore CAN bound the number of concurrent child processes (rg,
7
7
  // mixdog-graph, …) across ALL agents/workers in this daemon.
8
8
  //
9
- // Default: 1 on win32, unbounded elsewhere. Measured on Windows (explore-bench
10
- // 13-way fan-out, 2026-08-05): concurrent rg spawns thrash the AV child-scan
11
- // path — wall 31-61s unbounded vs 20-23s at cap 1, monotonic 1<2<6≈∞, tool
12
- // p50 14.6s→11.0s, quality unchanged. Spawns are short (rg/graph only; shell
13
- // is deliberately NOT gated), so serializing them overlaps LLM waits instead
14
- // of starving anything. MIXDOG_CHILD_SPAWN_MAX_INFLIGHT still overrides.
9
+ // Default: unbounded on every platform. A machine whose AV/process policy
10
+ // benefits from serialization may opt into a finite limit with
11
+ // MIXDOG_CHILD_SPAWN_MAX_INFLIGHT, but the product does not silently turn
12
+ // independent session tools into one global FIFO.
15
13
  //
16
- // IMPORTANT: this is a resource-control knob and is deliberately NOT exposed
17
- // on any tool JSON schema / tool parameter surface. The only tuning surface
18
- // is the MIXDOG_CHILD_SPAWN_MAX_INFLIGHT env override.
14
+ // IMPORTANT: these are resource-control knobs and are deliberately NOT exposed
15
+ // on any tool JSON schema / tool parameter surface. Operators may set the
16
+ // shared MIXDOG_CHILD_SPAWN_MAX_INFLIGHT fallback or a lane-specific override.
19
17
 
20
18
  function _defaultMaxInflight() {
21
19
  const override = Number(process.env.MIXDOG_CHILD_SPAWN_MAX_INFLIGHT);
22
20
  if (Number.isFinite(override) && override >= 1) return Math.floor(override);
23
- return process.platform === 'win32' ? 1 : Infinity;
21
+ return Infinity;
24
22
  }
25
23
 
26
- const MAX_INFLIGHT = _defaultMaxInflight();
24
+ const DEFAULT_MAX_INFLIGHT = _defaultMaxInflight();
25
+ const LANE_ALIASES = new Map([
26
+ ['default', 'search'],
27
+ ['build', 'code-graph'],
28
+ ]);
29
+
30
+ function _laneName(name) {
31
+ const clean = String(name || 'search').trim().toLowerCase() || 'search';
32
+ return LANE_ALIASES.get(clean) || clean;
33
+ }
34
+
35
+ function _laneLimit(name) {
36
+ const key = _laneName(name).toUpperCase().replace(/[^A-Z0-9]+/g, '_');
37
+ const override = Number(process.env[`MIXDOG_CHILD_SPAWN_${key}_MAX_INFLIGHT`]);
38
+ return Number.isFinite(override) && override >= 1
39
+ ? Math.floor(override)
40
+ : DEFAULT_MAX_INFLIGHT;
41
+ }
27
42
 
28
43
  // ── Lanes ────────────────────────────────────────────────────────────────
29
- // One shared cap number, applied PER LANE with independent counters/queues:
30
- // 'default' — short-lived spawns (rg). Holding time is one search run.
31
- // 'build' — long-lived builds (code-graph worker / graph binary). A cold
32
- // graph build can hold its slot for seconds-to-minutes; on the
33
- // win32 cap=1 default a single shared lane would let one build
44
+ // Independent optional limits and queues:
45
+ // 'search' — short-lived rg processes.
46
+ // 'code-graph' — long-lived graph workers / native graph binaries. A cold
47
+ // graph build can hold its slot for seconds-to-minutes; a
48
+ // single shared lane would let one build
34
49
  // stall every rg spawn behind it. Separate lanes remove that
35
50
  // starvation class while keeping each spawn family bounded.
36
- function _makeLane() {
37
- return { inflight: 0, queue: [] };
51
+ function _makeLane(name) {
52
+ return { inflight: 0, queue: [], limit: _laneLimit(name) };
38
53
  }
39
54
  const _lanes = new Map();
40
55
  function _lane(name) {
41
- let lane = _lanes.get(name);
42
- if (!lane) { lane = _makeLane(); _lanes.set(name, lane); }
56
+ const normalized = _laneName(name);
57
+ let lane = _lanes.get(normalized);
58
+ if (!lane) { lane = _makeLane(normalized); _lanes.set(normalized, lane); }
43
59
  return lane;
44
60
  }
45
61
 
@@ -61,14 +77,14 @@ function _maybeWarnSlow(waitedMs, laneName, lane) {
61
77
  _lastSlowWarnAt = now;
62
78
  try {
63
79
  process.stderr.write(
64
- `[child-spawn-gate] lane=${laneName} queue wait ${waitedMs}ms (inflight cap=${MAX_INFLIGHT}, queued=${lane.queue.length}); `
65
- + 'raise MIXDOG_CHILD_SPAWN_MAX_INFLIGHT if this persists\n',
80
+ `[child-spawn-gate] lane=${laneName} queue wait ${waitedMs}ms (inflight cap=${lane.limit}, queued=${lane.queue.length}); `
81
+ + 'raise the lane-specific MIXDOG_CHILD_SPAWN_*_MAX_INFLIGHT if this persists\n',
66
82
  );
67
83
  } catch { /* ignore */ }
68
84
  }
69
85
 
70
86
  function _drain(laneName, lane) {
71
- while (lane.inflight < MAX_INFLIGHT && lane.queue.length > 0) {
87
+ while (lane.inflight < lane.limit && lane.queue.length > 0) {
72
88
  const waiter = lane.queue.shift();
73
89
  if (waiter.onAbort && waiter.signal) {
74
90
  try { waiter.signal.removeEventListener('abort', waiter.onAbort); } catch { /* ignore */ }
@@ -89,23 +105,24 @@ function _drain(laneName, lane) {
89
105
  * call release().
90
106
  *
91
107
  * @param {AbortSignal | null} [signal]
92
- * @param {string} [laneName] — 'default' (short spawns) or 'build'.
108
+ * @param {string} [laneName] — 'search' (rg) or 'code-graph'.
93
109
  * @returns {Promise<() => void>}
94
110
  */
95
- export function acquire(signal = null, laneName = 'default') {
111
+ export function acquire(signal = null, laneName = 'search') {
96
112
  if (signal && signal.aborted) {
97
113
  return Promise.reject(signal.reason ?? _abortError());
98
114
  }
99
- const lane = _lane(laneName);
115
+ const normalizedLaneName = _laneName(laneName);
116
+ const lane = _lane(normalizedLaneName);
100
117
  return new Promise((resolve, reject) => {
101
118
  let released = false;
102
119
  const release = () => {
103
120
  if (released) return;
104
121
  released = true;
105
122
  lane.inflight = Math.max(0, lane.inflight - 1);
106
- _drain(laneName, lane);
123
+ _drain(normalizedLaneName, lane);
107
124
  };
108
- if (lane.inflight < MAX_INFLIGHT && lane.queue.length === 0) {
125
+ if (lane.inflight < lane.limit && lane.queue.length === 0) {
109
126
  lane.inflight++;
110
127
  resolve(release);
111
128
  return;
@@ -147,9 +164,21 @@ function _abortError() {
147
164
  * best-effort work. Real queries must use acquire()/withGate() and never gate
148
165
  * themselves on this.
149
166
  */
150
- export function hasSpareCapacity(laneName = 'default') {
167
+ export function hasSpareCapacity(laneName = 'search') {
151
168
  const lane = _lane(laneName);
152
- return lane.inflight < MAX_INFLIGHT && lane.queue.length === 0;
169
+ return lane.inflight < lane.limit && lane.queue.length === 0;
170
+ }
171
+
172
+ export function snapshot() {
173
+ return {
174
+ maxInflight: Number.isFinite(DEFAULT_MAX_INFLIGHT) ? DEFAULT_MAX_INFLIGHT : null,
175
+ lanes: [..._lanes.entries()].map(([name, lane]) => ({
176
+ name,
177
+ inflight: lane.inflight,
178
+ queued: lane.queue.length,
179
+ limit: Number.isFinite(lane.limit) ? lane.limit : null,
180
+ })),
181
+ };
153
182
  }
154
183
 
155
184
  /**
@@ -162,7 +191,7 @@ export function hasSpareCapacity(laneName = 'default') {
162
191
  * @param {string} [laneName]
163
192
  * @returns {Promise<T>}
164
193
  */
165
- async function withGate(fn, signal = null, laneName = 'default') {
194
+ async function withGate(fn, signal = null, laneName = 'search') {
166
195
  const release = await acquire(signal, laneName);
167
196
  try {
168
197
  return await fn({ signal: signal || null });