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
@@ -41,10 +41,21 @@ import { PLUGIN_LOG_MAX_BYTES, PLUGIN_LOG_KEEP_BYTES } from '../lib/mixdog-debug
41
41
  import { setChannelNotifySink } from '../runtime/channels/lib/parent-bridge.mjs';
42
42
  import { setOwnerContext } from '../runtime/channels/lib/runtime-paths.mjs';
43
43
  import { safeIpcSend } from '../runtime/shared/safe-ipc-send.mjs';
44
+ import { resourceAdmission } from '../runtime/shared/resource-admission.mjs';
45
+ import { snapshot as childSpawnSnapshot } from '../runtime/shared/child-spawn-gate.mjs';
46
+ import { providerAdmissionScheduler } from '../runtime/agent/orchestrator/providers/admission-scheduler.mjs';
47
+ import { loadConfig as loadAgentConfig } from '../runtime/agent/orchestrator/config.mjs';
48
+ import { initProviders } from '../runtime/agent/orchestrator/providers/registry.mjs';
49
+ import { makeAgentDispatch } from '../runtime/agent/orchestrator/agent-runtime/agent-dispatch.mjs';
50
+ import {
51
+ closeProviderStreamJsonPool,
52
+ providerStreamJsonSnapshot,
53
+ } from '../runtime/agent/orchestrator/providers/stream-json-pool.mjs';
54
+ import { createAgentDispatchBroker } from './agent-dispatch-broker.mjs';
44
55
  import { createChannelDaemonTransport } from './channel-daemon-transport.mjs';
45
- import { installEngineDaemonLocalBridge } from './engine-daemon-local-bridge.mjs';
46
56
  import { createEngineDaemonTransport } from './engine-daemon-transport.mjs';
47
57
  import { createEngineDaemonService } from './engine-daemon-service.mjs';
58
+ import { createSessionProtocolClient } from './session-protocol.mjs';
48
59
  import { createStandaloneMemoryRuntime } from './memory-runtime-proxy.mjs';
49
60
 
50
61
  function runtimeRoot() {
@@ -87,6 +98,7 @@ let logQueueBytes = 0;
87
98
  let logDropped = 0;
88
99
  let logFlushTimer = null;
89
100
  let logWriter = Promise.resolve();
101
+ let logFileBytes = null;
90
102
 
91
103
  function boundedLogText(value) {
92
104
  const text = String(value ?? '');
@@ -95,15 +107,18 @@ function boundedLogText(value) {
95
107
  }
96
108
 
97
109
  async function rotateDaemonLogIfNeeded(incomingBytes) {
98
- let info;
99
- try { info = await stat(LOG_PATH); } catch { return; }
100
- if (info.size + incomingBytes <= PLUGIN_LOG_MAX_BYTES) return;
101
- const keep = Math.min(info.size, PLUGIN_LOG_KEEP_BYTES);
110
+ if (logFileBytes === null) {
111
+ try { logFileBytes = (await stat(LOG_PATH)).size; }
112
+ catch { logFileBytes = 0; }
113
+ }
114
+ if (logFileBytes + incomingBytes <= PLUGIN_LOG_MAX_BYTES) return;
115
+ const keep = Math.min(logFileBytes, PLUGIN_LOG_KEEP_BYTES);
102
116
  const tail = Buffer.allocUnsafe(keep);
103
117
  const handle = await open(LOG_PATH, 'r');
104
118
  try {
105
- const { bytesRead } = await handle.read(tail, 0, keep, Math.max(0, info.size - keep));
119
+ const { bytesRead } = await handle.read(tail, 0, keep, Math.max(0, logFileBytes - keep));
106
120
  await writeFile(LOG_PATH, tail.subarray(0, bytesRead));
121
+ logFileBytes = bytesRead;
107
122
  } finally {
108
123
  await handle.close().catch(() => {});
109
124
  }
@@ -132,6 +147,7 @@ function queueLogFlush(delayMs = 10) {
132
147
  await mkdir(path.dirname(LOG_PATH), { recursive: true });
133
148
  await rotateDaemonLogIfNeeded(Buffer.byteLength(batch));
134
149
  await appendFile(LOG_PATH, batch, 'utf8');
150
+ logFileBytes = (logFileBytes || 0) + Buffer.byteLength(batch);
135
151
  }).catch(() => {});
136
152
  if (logQueue.length > 0 || logDropped > 0) queueLogFlush();
137
153
  }, delayMs);
@@ -162,6 +178,7 @@ async function flushDaemonLogs() {
162
178
  await mkdir(path.dirname(LOG_PATH), { recursive: true });
163
179
  await rotateDaemonLogIfNeeded(Buffer.byteLength(batch));
164
180
  await appendFile(LOG_PATH, batch, 'utf8');
181
+ logFileBytes = (logFileBytes || 0) + Buffer.byteLength(batch);
165
182
  }).catch(() => {});
166
183
  }
167
184
  await logWriter;
@@ -215,9 +232,9 @@ let channels = null;
215
232
  let transport = null;
216
233
  let engineTransport = null;
217
234
  let engineService = null;
218
- let localEngineBridge = null;
219
- let uninstallLocalEngineBridge = null;
235
+ let localSessionBridge = null;
220
236
  let memoryRuntime = null;
237
+ let agentDispatchBroker = null;
221
238
  let shuttingDown = false;
222
239
  let shutdownRecheckTimer = null;
223
240
  const eventLoopDelay = monitorEventLoopDelay({ resolution: 20 });
@@ -243,13 +260,13 @@ async function shutdown(reason, code = 0) {
243
260
  log(`shutting down (${reason})`);
244
261
  try { setChannelNotifySink(null); } catch {}
245
262
  try { await engineService?.stop?.(reason); } catch (e) { log(`engine.stop failed: ${e?.message || e}`); }
246
- try { await localEngineBridge?.close?.(reason); } catch (e) { log(`local engine bridge.close failed: ${e?.message || e}`); }
247
- try { uninstallLocalEngineBridge?.(); } catch {}
248
- uninstallLocalEngineBridge = null;
263
+ try { await localSessionBridge?.close?.(reason); } catch (e) { log(`local session bridge.close failed: ${e?.message || e}`); }
249
264
  try { await engineTransport?.stop?.(); } catch (e) { log(`engine transport.stop failed: ${e?.message || e}`); }
250
265
  try { await channels?.stop?.(); } catch (e) { log(`channels.stop failed: ${e?.message || e}`); }
251
266
  // Detach the memory client (never hard-kills the shared memory daemon).
252
267
  try { await memoryRuntime?.stop?.(); } catch (e) { log(`memory.stop failed: ${e?.message || e}`); }
268
+ try { agentDispatchBroker?.close?.(reason); } catch (e) { log(`agent broker stop failed: ${e?.message || e}`); }
269
+ try { await closeProviderStreamJsonPool(reason); } catch (e) { log(`stream parser stop failed: ${e?.message || e}`); }
253
270
  try { await transport?.stop?.(); } catch (e) { log(`transport.stop failed: ${e?.message || e}`); }
254
271
  if (eventLoopLagTimer) { clearInterval(eventLoopLagTimer); eventLoopLagTimer = null; }
255
272
  eventLoopDelay.disable();
@@ -275,12 +292,16 @@ function maybeSelfShutdown(reason) {
275
292
  // A turn in flight outlives every view — closing the app or the terminal is
276
293
  // not a reason to abandon work the daemon is still running.
277
294
  const busyEngines = engineService?.busyCount ?? 0;
278
- if (busyEngines > 0) {
279
- log(`shutdown deferred (${reason}): ${busyEngines} engine(s) still working`);
295
+ const busyMemoryAgents = agentDispatchBroker?.snapshot?.().inFlight ?? 0;
296
+ if (busyEngines > 0 || busyMemoryAgents > 0) {
297
+ log(
298
+ `shutdown deferred (${reason}): engines=${busyEngines}`
299
+ + ` memoryAgents=${busyMemoryAgents}`,
300
+ );
280
301
  if (!shutdownRecheckTimer) {
281
302
  shutdownRecheckTimer = setTimeout(() => {
282
303
  shutdownRecheckTimer = null;
283
- maybeSelfShutdown('busy engines settled');
304
+ maybeSelfShutdown('busy backend work settled');
284
305
  }, 1_000);
285
306
  shutdownRecheckTimer.unref?.();
286
307
  }
@@ -305,6 +326,13 @@ async function main() {
305
326
  process.exit(0);
306
327
  }
307
328
  process.on('exit', () => { try { releaseSingletonOwner(OWNER_PATH, process.pid); } catch {} });
329
+ agentDispatchBroker = createAgentDispatchBroker({
330
+ loadConfig: loadAgentConfig,
331
+ initProviders,
332
+ makeAgentDispatch,
333
+ log,
334
+ onActivityChanged: () => { maybeSelfShutdown('memory agent activity changed'); },
335
+ });
308
336
  process.on('mixdog:turn-timing', (row = {}) => {
309
337
  const ms = (value) => Number.isFinite(value) ? Math.round(value) : -1;
310
338
  log(
@@ -357,6 +385,7 @@ async function main() {
357
385
  };
358
386
  transport = createChannelDaemonTransport({
359
387
  handleCall,
388
+ agentBroker: agentDispatchBroker,
360
389
  // If a manual owner disappears without sending OFF, deactivate the backend.
361
390
  // No survivor is selected or rebound.
362
391
  dispatchControl: (name, args, ctx) => handleCall(name, args, ctx),
@@ -370,6 +399,13 @@ async function main() {
370
399
  });
371
400
  setChannelNotifySink((method, params) => transport.notify(method, params));
372
401
  const { port, token } = await transport.start();
402
+ // Match the old memory-child behavior: provider preparation starts eagerly
403
+ // after the backend front door is ready, but now it initializes the ONE
404
+ // process-wide registry shared by channels, sessions, and all memory cycles.
405
+ // Calls arriving during warmup await registry singleflight; they never build
406
+ // another provider graph.
407
+ void agentDispatchBroker.warmup()
408
+ .catch((error) => log(`agent broker warmup failed (non-fatal): ${error?.message || error}`));
373
409
 
374
410
  // ── Engine front door ───────────────────────────────────────────────────────
375
411
  // Session engines live in THIS process too, so the terminal TUI and the
@@ -377,16 +413,16 @@ async function main() {
377
413
  // engine module is the whole runtime graph, so it is imported when the first
378
414
  // engine client registers — early enough to overlap user think time, while a
379
415
  // channels-only spawn still never pays for it.
380
- const localEngineClients = new Map();
381
- let nextLocalEngineClient = 0;
382
- localEngineBridge = {
416
+ const localSessionClients = new Map();
417
+ let nextLocalSessionClient = 0;
418
+ localSessionBridge = {
383
419
  attach({ onFrame = () => {}, onFatal = () => {} } = {}) {
384
- const clientToken = `daemon_local_${process.pid}_${++nextLocalEngineClient}`;
420
+ const clientToken = `daemon_local_${process.pid}_${++nextLocalSessionClient}`;
385
421
  let closed = false;
386
- localEngineClients.set(clientToken, { onFrame, onFatal });
387
- return {
422
+ localSessionClients.set(clientToken, { onFrame, onFatal });
423
+ return createSessionProtocolClient({
388
424
  call(name, args = {}, options = {}) {
389
- if (closed) throw new Error('engine daemon local bridge is closed');
425
+ if (closed) throw new Error('daemon-local session client is closed');
390
426
  return engineService.handleCall(name, args, {
391
427
  clientToken,
392
428
  ...(options?.callId ? { callId: String(options.callId) } : {}),
@@ -395,39 +431,34 @@ async function main() {
395
431
  async close(reason = 'local engine view closed') {
396
432
  if (closed) return;
397
433
  closed = true;
398
- localEngineClients.delete(clientToken);
434
+ localSessionClients.delete(clientToken);
399
435
  try { engineService.releaseClient(clientToken); } catch {}
400
436
  log(`${reason} (${clientToken})`);
401
437
  },
402
- };
438
+ });
403
439
  },
404
440
  publish(frame, targetTokens = null) {
405
441
  const targets = targetTokens ? new Set(targetTokens) : null;
406
- for (const [clientToken, client] of localEngineClients) {
442
+ for (const [clientToken, client] of localSessionClients) {
407
443
  if (targets && !targets.has(clientToken)) continue;
408
444
  try { client.onFrame(frame); } catch {}
409
445
  }
410
446
  },
411
447
  async close(reason = 'daemon shutdown') {
412
- for (const [clientToken, client] of localEngineClients) {
448
+ for (const [clientToken, client] of localSessionClients) {
413
449
  try { client.onFatal(reason); } catch {}
414
450
  try { engineService.releaseClient(clientToken); } catch {}
415
451
  }
416
- localEngineClients.clear();
452
+ localSessionClients.clear();
417
453
  },
418
454
  };
419
- uninstallLocalEngineBridge = installEngineDaemonLocalBridge(localEngineBridge);
420
- const engineDaemonClientModule = () => import('./engine-daemon-client.mjs');
421
455
  const desktopRuntime = {
422
- async createRemoteEngineSession(options) {
423
- return (await engineDaemonClientModule()).createRemoteEngineSession(options);
424
- },
425
- async callDaemonSession(options) {
426
- return (await engineDaemonClientModule()).callDaemonSession(options);
456
+ async attachSessionClient(options = {}) {
457
+ if (!localSessionBridge) throw new Error('daemon-local session client is unavailable');
458
+ return localSessionBridge.attach(options);
427
459
  },
428
460
  loadProjects: () => import('./projects.mjs'),
429
461
  loadSessionStore: () => import('../runtime/agent/orchestrator/session/store-summary-reader.mjs'),
430
- loadStatuslineSegments: () => import('../ui/statusline-segments.mjs'),
431
462
  loadConfig: () => import('../runtime/shared/config.mjs'),
432
463
  loadCommitCompletion: () => import(
433
464
  '../runtime/agent/orchestrator/agent-runtime/commit-message-completion.mjs'
@@ -454,8 +485,9 @@ async function main() {
454
485
  void loadEngineModule()
455
486
  .then((engineModule) => {
456
487
  engineModule.preloadSessionRuntimeModule?.();
488
+ engineModule.preloadAgentLoopRuntime?.();
457
489
  engineModule.preloadKeychainSecrets?.();
458
- log('engine runtime/keychain prewarm started');
490
+ log('engine runtime/agent-loop/keychain prewarm started');
459
491
  })
460
492
  .catch((error) => {
461
493
  engineRuntimePrewarmStarted = false;
@@ -467,9 +499,19 @@ async function main() {
467
499
  const engineModule = await loadEngineModule();
468
500
  return engineModule.createLocalEngineSession(options);
469
501
  },
502
+ sessionExists: async (sessionId) => {
503
+ const store = await desktopRuntime.loadSessionStore();
504
+ return store.storedSessionExists?.(sessionId) === true;
505
+ },
506
+ listSessions: async (options = {}) => {
507
+ const store = await desktopRuntime.loadSessionStore();
508
+ return store.listStoredSessionSummaries({
509
+ refreshFromStorage: options.refreshFromStorage === true,
510
+ });
511
+ },
470
512
  desktopRuntime,
471
513
  onFrame: (frame, targetTokens) => {
472
- localEngineBridge?.publish(frame, targetTokens);
514
+ localSessionBridge?.publish(frame, targetTokens);
473
515
  engineTransport?.broadcast(frame, targetTokens);
474
516
  },
475
517
  onExternalClientsChanged: () => { maybeSelfShutdown('remote clients changed'); },
@@ -487,6 +529,25 @@ async function main() {
487
529
  getStatus: () => ({
488
530
  engines: engineService.size,
489
531
  busy: engineService.busyCount,
532
+ sessionPool: engineService.status,
533
+ workload: {
534
+ resources: resourceAdmission.snapshot(),
535
+ childSpawns: childSpawnSnapshot(),
536
+ providers: providerAdmissionScheduler.snapshot(),
537
+ streamParsing: providerStreamJsonSnapshot(),
538
+ },
539
+ memory: {
540
+ ...(() => {
541
+ const usage = process.memoryUsage();
542
+ return {
543
+ rssBytes: usage.rss,
544
+ heapTotalBytes: usage.heapTotal,
545
+ heapUsedBytes: usage.heapUsed,
546
+ externalBytes: usage.external,
547
+ arrayBufferBytes: usage.arrayBuffers,
548
+ };
549
+ })(),
550
+ },
490
551
  ...eventLoopStatus(),
491
552
  }),
492
553
  onClientsEmpty: () => { maybeSelfShutdown('no live engine views'); },
@@ -11,6 +11,7 @@ import {
11
11
  import { basename, dirname, join } from 'node:path';
12
12
  import {
13
13
  SECRET_ACCOUNTS,
14
+ canonicalizeStoredChannelsConfig,
14
15
  deleteSecret,
15
16
  diagnoseDiscordTokenValue,
16
17
  getDiscordToken,
@@ -46,7 +47,7 @@ const DEFAULT_CHANNELS = Object.freeze({
46
47
  backend: 'discord',
47
48
  discord: {},
48
49
  access: { dmPolicy: 'allowlist', allowFrom: [], channels: {} },
49
- channel: { channelId: '', discordChannelId: '', telegramChatId: '' },
50
+ channel: { discordChannelId: '', telegramChatId: '' },
50
51
  webhook: { enabled: true, port: 3333 },
51
52
  });
52
53
 
@@ -91,13 +92,14 @@ function assertScheduleName(name) {
91
92
  }
92
93
 
93
94
  function normalizeChannelsConfig(raw = {}) {
95
+ const stored = canonicalizeStoredChannelsConfig(raw);
94
96
  return {
95
97
  ...DEFAULT_CHANNELS,
96
- ...(raw && typeof raw === 'object' ? raw : {}),
97
- discord: { ...DEFAULT_CHANNELS.discord, ...(raw?.discord || {}) },
98
- access: { ...DEFAULT_CHANNELS.access, ...(raw?.access || {}) },
99
- channel: { ...DEFAULT_CHANNELS.channel, ...(raw?.channel || {}) },
100
- webhook: { ...DEFAULT_CHANNELS.webhook, ...(raw?.webhook || {}) },
98
+ ...stored,
99
+ discord: { ...DEFAULT_CHANNELS.discord, ...(stored.discord || {}) },
100
+ access: { ...DEFAULT_CHANNELS.access, ...(stored.access || {}) },
101
+ channel: { ...DEFAULT_CHANNELS.channel, ...(stored.channel || {}) },
102
+ webhook: { ...DEFAULT_CHANNELS.webhook, ...(stored.webhook || {}) },
101
103
  };
102
104
  }
103
105
 
@@ -214,8 +216,7 @@ export function setChannel({ channelId, backend = null } = {}) {
214
216
  const nextEntry = { ...current };
215
217
  if (writeBackend === 'telegram') nextEntry.telegramChatId = id;
216
218
  else nextEntry.discordChannelId = id;
217
- // `channelId` mirrors the active backend's id for legacy readers.
218
- nextEntry.channelId = channelIdForBackend(nextEntry, activeBackend);
219
+ delete nextEntry.channelId;
219
220
  return { ...cfg, channel: nextEntry };
220
221
  });
221
222
  }
@@ -231,7 +232,7 @@ export async function setChannelAsync({ channelId, backend = null } = {}) {
231
232
  const nextEntry = { ...current };
232
233
  if (writeBackend === 'telegram') nextEntry.telegramChatId = id;
233
234
  else nextEntry.discordChannelId = id;
234
- nextEntry.channelId = channelIdForBackend(nextEntry, activeBackend);
235
+ delete nextEntry.channelId;
235
236
  return { ...cfg, channel: nextEntry };
236
237
  });
237
238
  }
@@ -271,9 +272,8 @@ function backendBuilder(value) {
271
272
  return (cfg) => {
272
273
  const activeBackend = cfg.backend === 'telegram' ? 'telegram' : 'discord';
273
274
  const seeded = seedBackendChannelIds(resolveChannelEntry(cfg), activeBackend);
274
- // `channelId` now mirrors the newly-selected backend's id; both per-backend
275
- // id fields are retained so switching back keeps the other id.
276
- const channel = { ...seeded, channelId: channelIdForBackend(seeded, value) };
275
+ const channel = { ...seeded };
276
+ delete channel.channelId;
277
277
  return { ...cfg, channel, backend: value };
278
278
  };
279
279
  }
@@ -39,7 +39,10 @@ export function readDaemonDiscovery(discoveryPath) {
39
39
  return { port, pid, token: String(raw.token) };
40
40
  }
41
41
 
42
- function request({ port, method = 'GET', path = '/', token, body = null, timeoutMs = 10_000 }) {
42
+ function request({
43
+ port, method = 'GET', path = '/', token, body = null, timeoutMs = 10_000,
44
+ agent = undefined,
45
+ }) {
43
46
  return new Promise((resolve, reject) => {
44
47
  const payload = body == null ? null : JSON.stringify(body);
45
48
  let req = null;
@@ -69,6 +72,7 @@ function request({ port, method = 'GET', path = '/', token, body = null, timeout
69
72
  port,
70
73
  path,
71
74
  method,
75
+ agent,
72
76
  headers: {
73
77
  ...(token ? { 'X-Mixdog-Daemon-Token': token } : {}),
74
78
  ...(payload
@@ -130,6 +134,12 @@ export async function attachToDaemon({
130
134
  const expectedPid = parsePid(discovery?.pid);
131
135
  if (!discovery?.port || !discovery?.token || !expectedPid) throw new Error('daemon discovery {port, pid, token} required');
132
136
  const { port, token: serverToken } = discovery;
137
+ const callAgent = new http.Agent({
138
+ keepAlive: true, keepAliveMsecs: 5_000, maxSockets: 16, maxFreeSockets: 4,
139
+ });
140
+ const controlAgent = new http.Agent({
141
+ keepAlive: true, keepAliveMsecs: 5_000, maxSockets: 4, maxFreeSockets: 2,
142
+ });
133
143
  const staleDiscoveryError = (reason) => {
134
144
  const err = new Error(reason);
135
145
  err.daemonDiscoveryStale = true;
@@ -153,6 +163,7 @@ export async function attachToDaemon({
153
163
  // terminal still served by an older machine-global daemon.
154
164
  body: { leadPid, cwd, reattach: true },
155
165
  timeoutMs: 3000,
166
+ agent: controlAgent,
156
167
  });
157
168
  } catch (err) {
158
169
  if (err?.statusCode === 401 || err?.statusCode === 403) {
@@ -193,6 +204,7 @@ export async function attachToDaemon({
193
204
  ...(registrationId ? { registrationId, replaceToken, leadPid, cwd } : {}),
194
205
  },
195
206
  timeoutMs: 1500,
207
+ agent: controlAgent,
196
208
  });
197
209
  } catch { /* best-effort; daemon sweep reaps us */ }
198
210
  }
@@ -321,6 +333,7 @@ export async function attachToDaemon({
321
333
  leadPid, cwd, reattach: true, replaceToken,
322
334
  registrationId,
323
335
  }, timeoutMs: 3000,
336
+ agent: controlAgent,
324
337
  }).then(async (r) => {
325
338
  const freshToken = r?.token;
326
339
  if (!freshToken) return false;
@@ -356,6 +369,7 @@ export async function attachToDaemon({
356
369
  // a retried transport failure to a single side-effect.
357
370
  body: { token: clientToken, name, args: args || {}, ...(callId ? { callId } : {}) },
358
371
  timeoutMs,
372
+ agent: callAgent,
359
373
  });
360
374
  } catch (err) {
361
375
  // Transport failure (daemon dead/restarted/unreachable) — tag so the
@@ -383,6 +397,8 @@ export async function attachToDaemon({
383
397
  closePromise = (async () => {
384
398
  if (pendingRegistration) await pendingRegistration;
385
399
  await deregister(clientToken, { registrationId, replaceToken });
400
+ callAgent.destroy();
401
+ controlAgent.destroy();
386
402
  log(`detached (${reason})`);
387
403
  })();
388
404
  return closePromise;
@@ -18,6 +18,7 @@ import { readFileSync, rmSync } from 'node:fs';
18
18
  import { basename, dirname, join } from 'node:path';
19
19
  import { writeJsonAtomicSync } from '../runtime/shared/atomic-file.mjs';
20
20
  import { readBody, sendJson, sendError } from '../runtime/memory/lib/http-wire.mjs';
21
+ import { createFairCallScheduler } from './fair-call-scheduler.mjs';
21
22
 
22
23
  function parsePid(value) {
23
24
  const n = Number(value);
@@ -49,6 +50,7 @@ export function createChannelDaemonTransport({
49
50
  remoteStatePath = null,
50
51
  remoteIntentPath = null,
51
52
  onClientRegistered = null,
53
+ agentBroker = null,
52
54
  } = {}) {
53
55
  if (typeof handleCall !== 'function') throw new Error('handleCall is required');
54
56
 
@@ -81,6 +83,23 @@ export function createChannelDaemonTransport({
81
83
  // retry never double-runs a non-idempotent tool (e.g. reply). Short TTL.
82
84
  const callCache = new Map();
83
85
  const CALL_CACHE_TTL_MS = 60_000;
86
+ const configuredLaneLimit = (name) => {
87
+ const parsed = Math.floor(Number(process.env[name]));
88
+ return Number.isFinite(parsed) && parsed >= 1 ? parsed : Infinity;
89
+ };
90
+ const CALL_QUEUE_MAX = Math.max(256, Number(process.env.MIXDOG_CHANNEL_DAEMON_CALL_QUEUE) || 256);
91
+ const channelCalls = createFairCallScheduler({
92
+ name: 'channel daemon call',
93
+ activeMax: configuredLaneLimit('MIXDOG_CHANNEL_DAEMON_ACTIVE_CALLS'),
94
+ queueMax: CALL_QUEUE_MAX,
95
+ minOwnerQueue: Math.max(8, Math.floor(CALL_QUEUE_MAX / 16)),
96
+ });
97
+ const channelControlCalls = createFairCallScheduler({
98
+ name: 'channel daemon control call',
99
+ activeMax: configuredLaneLimit('MIXDOG_CHANNEL_DAEMON_CONTROL_RESERVE'),
100
+ queueMax: Math.max(16, Math.min(64, CALL_QUEUE_MAX)),
101
+ minOwnerQueue: 4,
102
+ });
84
103
  // Reconnect register replay: a server may commit replacement just before its
85
104
  // HTTP response is lost. The retry supplies this stable id and receives the
86
105
  // already-created fresh token instead of creating an orphan replacement.
@@ -615,7 +634,16 @@ export function createChannelDaemonTransport({
615
634
  const pathName = url.pathname;
616
635
  try {
617
636
  if (req.method === 'GET' && pathName === '/health') {
618
- sendJson(res, { status: 'ok', pid: process.pid, clients: clients.size, ...getStatus() });
637
+ sendJson(res, {
638
+ status: 'ok',
639
+ pid: process.pid,
640
+ clients: clients.size,
641
+ activeCalls: channelCalls.active + channelControlCalls.active,
642
+ queuedCalls: channelCalls.queued + channelControlCalls.queued,
643
+ callOwners: channelCalls.snapshot().owners,
644
+ ...(agentBroker?.snapshot ? { agentBroker: agentBroker.snapshot() } : {}),
645
+ ...getStatus(),
646
+ });
619
647
  return;
620
648
  }
621
649
  const token = req.headers['x-mixdog-daemon-token'] || url.searchParams.get('server_token');
@@ -649,6 +677,45 @@ export function createChannelDaemonTransport({
649
677
  if (!attachSse(clientToken, res)) { sendError(res, 'unknown client token', 404); return; }
650
678
  return; // stream stays open
651
679
  }
680
+ // Internal memory -> backend LLM bridge. It is authenticated with the
681
+ // daemon discovery token but deliberately does NOT register as a channel
682
+ // client: background memory work must not acquire remote ownership or
683
+ // keep the channels client registry alive. The broker itself owns a
684
+ // parallel fair scheduler and per-call cancellation.
685
+ if (req.method === 'POST' && pathName === '/agent/dispatch') {
686
+ if (!agentBroker?.dispatch) { sendError(res, 'agent broker unavailable', 503); return; }
687
+ const body = await readBody(req);
688
+ const callId = String(body.callId || '').trim();
689
+ if (!callId) { sendError(res, 'callId required', 400); return; }
690
+ res.on('close', () => {
691
+ if (res.writableFinished) return;
692
+ try { agentBroker.cancel(callId, 'agent broker client disconnected'); } catch {}
693
+ });
694
+ try {
695
+ const result = await agentBroker.dispatch(body.params || {}, { callId });
696
+ sendJson(res, { ok: true, result });
697
+ } catch (error) {
698
+ sendJson(res, { ok: false, error: error?.message || String(error) }, 200);
699
+ }
700
+ return;
701
+ }
702
+ if (req.method === 'POST' && pathName === '/agent/cancel') {
703
+ if (!agentBroker?.cancel) { sendError(res, 'agent broker unavailable', 503); return; }
704
+ const body = await readBody(req);
705
+ const callId = String(body.callId || '').trim();
706
+ if (!callId) { sendError(res, 'callId required', 400); return; }
707
+ const cancelled = agentBroker.cancelAndWait
708
+ ? await agentBroker.cancelAndWait(
709
+ callId,
710
+ String(body.reason || 'memory agent dispatch canceled'),
711
+ )
712
+ : agentBroker.cancel(callId, String(body.reason || 'memory agent dispatch canceled'));
713
+ sendJson(res, {
714
+ ok: true,
715
+ cancelled,
716
+ });
717
+ return;
718
+ }
652
719
  if (req.method === 'POST' && pathName === '/call') {
653
720
  const body = await readBody(req);
654
721
  const clientToken = body.token || null;
@@ -660,13 +727,21 @@ export function createChannelDaemonTransport({
660
727
  c.lastSeen = nowMs();
661
728
  const name = String(body.name || '');
662
729
  const callId = body.callId ? String(body.callId) : null;
730
+ const addressedSessionId = String(body.args?.sessionId || '').trim()
731
+ || remoteSessionIdFromBinding(name, body.args || {});
732
+ const ownerKey = addressedSessionId
733
+ ? `session:${addressedSessionId}`
734
+ : c.leadPid
735
+ ? `pid:${c.leadPid}`
736
+ : `client:${clientToken}`;
737
+ const cacheKey = callId ? `${ownerKey}\u0000${callId}` : null;
663
738
  let dispatch;
664
- if (callId && callCache.has(callId)) {
739
+ if (cacheKey && callCache.has(cacheKey)) {
665
740
  // Replay of a retried call — dedup to the original run (exactly one
666
741
  // side-effect) instead of dispatching handleCall a second time.
667
- dispatch = callCache.get(callId).promise;
742
+ dispatch = callCache.get(cacheKey).promise;
668
743
  } else {
669
- dispatch = Promise.resolve().then(async () => {
744
+ const run = async () => {
670
745
  const args = body.args || {};
671
746
  const activationCall = name === ACTIVATE_TOOL;
672
747
  const activating = activationCall && args.active === true;
@@ -748,15 +823,20 @@ export function createChannelDaemonTransport({
748
823
  }
749
824
  throw err;
750
825
  }
751
- });
752
- if (callId) {
753
- callCache.set(callId, { promise: dispatch, at: nowMs() });
826
+ };
827
+ const scheduler = BINDING_TOOLS.has(name) ? channelControlCalls : channelCalls;
828
+ dispatch = scheduler.enqueue(ownerKey, run);
829
+ if (cacheKey) {
830
+ const record = { promise: dispatch, at: nowMs() };
831
+ callCache.set(cacheKey, record);
754
832
  // Start the TTL only once the call SETTLES: an in-flight call can
755
833
  // outlive a fixed-from-dispatch TTL (e.g. a slow reply upload past
756
834
  // 60s), and expiring its entry mid-flight would let a transport
757
835
  // retry replay-miss and dispatch a second real side-effect.
758
836
  dispatch.then(() => {}, () => {}).then(() => {
759
- const t = setTimeout(() => callCache.delete(callId), CALL_CACHE_TTL_MS);
837
+ const t = setTimeout(() => {
838
+ if (callCache.get(cacheKey) === record) callCache.delete(cacheKey);
839
+ }, CALL_CACHE_TTL_MS);
760
840
  t.unref?.();
761
841
  });
762
842
  }
@@ -813,6 +893,8 @@ export function createChannelDaemonTransport({
813
893
 
814
894
  async function stop() {
815
895
  closed = true;
896
+ channelCalls.close('channel daemon transport is closed');
897
+ channelControlCalls.close('channel daemon transport is closed');
816
898
  cancelGrace();
817
899
  if (sweepTimer) { try { clearInterval(sweepTimer); } catch {} sweepTimer = null; }
818
900
  for (const [token] of clients) dropClient(token, 'transport stop');