@adhdev/daemon-core 0.9.82-rc.273 → 0.9.82-rc.274

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.
package/dist/index.mjs CHANGED
@@ -270,10 +270,10 @@ function readInjected(value) {
270
270
  }
271
271
  function getDaemonBuildInfo() {
272
272
  if (cached) return cached;
273
- const commit = readInjected(true ? "e4a64afbd050fa5167c383b1fc6f9df5f8e48ba2" : void 0) ?? "unknown";
274
- const commitShort = readInjected(true ? "e4a64afb" : void 0) ?? (commit !== "unknown" ? commit.slice(0, 7) : "unknown");
275
- const version = readInjected(true ? "0.9.82-rc.273" : void 0) ?? readInjected(typeof process !== "undefined" ? process.env?.ADHDEV_PKG_VERSION : void 0) ?? "unknown";
276
- const builtAt = readInjected(true ? "2026-06-15T05:37:07.054Z" : void 0);
273
+ const commit = readInjected(true ? "89d1ac61612c1dfe7fac53be61ec5dbfa67d00b2" : void 0) ?? "unknown";
274
+ const commitShort = readInjected(true ? "89d1ac61" : void 0) ?? (commit !== "unknown" ? commit.slice(0, 7) : "unknown");
275
+ const version = readInjected(true ? "0.9.82-rc.274" : void 0) ?? readInjected(typeof process !== "undefined" ? process.env?.ADHDEV_PKG_VERSION : void 0) ?? "unknown";
276
+ const builtAt = readInjected(true ? "2026-06-15T07:04:52.710Z" : void 0);
277
277
  cached = builtAt ? { commit, commitShort, version, builtAt } : { commit, commitShort, version };
278
278
  return cached;
279
279
  }
@@ -6162,16 +6162,6 @@ function readNonEmptyString2(value) {
6162
6162
  function readRecord3(value) {
6163
6163
  return value && typeof value === "object" && !Array.isArray(value) ? value : void 0;
6164
6164
  }
6165
- function canonicalDaemonId(value) {
6166
- const id = readNonEmptyString2(value);
6167
- if (!id) return "";
6168
- return id.replace(/^(?:daemon|standalone)_/, "");
6169
- }
6170
- function sameDaemonId(a, b) {
6171
- const ca = canonicalDaemonId(a);
6172
- const cb = canonicalDaemonId(b);
6173
- return ca !== "" && ca === cb;
6174
- }
6175
6165
  function buildMeshWorkerRelayStamp(currentSettings, meshContext) {
6176
6166
  if (!meshContext) return void 0;
6177
6167
  const settings = currentSettings && typeof currentSettings === "object" ? currentSettings : {};
@@ -6375,29 +6365,6 @@ function buildPendingEventFingerprint(event) {
6375
6365
  timestamp || ""
6376
6366
  ].join("::");
6377
6367
  }
6378
- function markMeshCoordinatorEventDirectDelivered(coordinatorDaemonId, event) {
6379
- const canonical = canonicalDaemonId(coordinatorDaemonId);
6380
- if (!canonical) return;
6381
- const fingerprint = buildPendingEventFingerprint(event);
6382
- if (!fingerprint.trim()) return;
6383
- try {
6384
- const store = MeshRuntimeStore.getInstance();
6385
- store.recordDirectDelivered(canonical, fingerprint, DIRECT_DELIVERED_TTL_MS);
6386
- store.sweepExpiredDirectDelivered();
6387
- } catch {
6388
- }
6389
- }
6390
- function wasDirectDeliveredToCoordinator(coordinatorDaemonId, event) {
6391
- const canonical = canonicalDaemonId(coordinatorDaemonId);
6392
- if (!canonical) return false;
6393
- const fingerprint = buildPendingEventFingerprint(event);
6394
- if (!fingerprint.trim()) return false;
6395
- try {
6396
- return MeshRuntimeStore.getInstance().wasDirectDelivered(canonical, fingerprint);
6397
- } catch {
6398
- return false;
6399
- }
6400
- }
6401
6368
  function hasPendingCoordinatorEventDuplicate(event) {
6402
6369
  const fingerprint = buildPendingEventFingerprint(event);
6403
6370
  if (!fingerprint.trim()) return false;
@@ -6596,9 +6563,7 @@ function drainPendingMeshCoordinatorEvents(meshId, coordinatorDaemonId) {
6596
6563
  for (const event of filtered) pushUnique(event);
6597
6564
  }
6598
6565
  if (merged.length === 0) return [];
6599
- const deliverable = coordinatorDaemonId ? merged.filter((event) => !wasDirectDeliveredToCoordinator(coordinatorDaemonId, event)) : merged;
6600
- if (deliverable.length === 0) return [];
6601
- return reconcilePendingMeshCoordinatorEvents(meshId, deliverable);
6566
+ return reconcilePendingMeshCoordinatorEvents(meshId, merged);
6602
6567
  }
6603
6568
  function getPendingMeshCoordinatorEvents(meshId, coordinatorDaemonId) {
6604
6569
  if (!meshId) return [];
@@ -6625,8 +6590,7 @@ function getPendingMeshCoordinatorEvents(meshId, coordinatorDaemonId) {
6625
6590
  for (const event of readPendingMeshCoordinatorEventsFromDisk(meshId, coordinatorDaemonId)) {
6626
6591
  pushUnique(event);
6627
6592
  }
6628
- const deliverable = coordinatorDaemonId ? merged.filter((event) => !wasDirectDeliveredToCoordinator(coordinatorDaemonId, event)) : merged;
6629
- return reconcilePendingMeshCoordinatorEvents(meshId, deliverable);
6593
+ return reconcilePendingMeshCoordinatorEvents(meshId, merged);
6630
6594
  }
6631
6595
  function clearPendingMeshCoordinatorEvents(meshId, coordinatorDaemonId) {
6632
6596
  if (!meshId) return;
@@ -6642,7 +6606,7 @@ function clearPendingMeshCoordinatorEvents(meshId, coordinatorDaemonId) {
6642
6606
  }
6643
6607
  }
6644
6608
  }
6645
- var REFINE_TERMINAL_EVENTS, DIRECT_DELIVERED_TTL_MS, MAX_PENDING_EVENTS_BYTES, MAX_PENDING_EVENTS_KEEP;
6609
+ var REFINE_TERMINAL_EVENTS, MAX_PENDING_EVENTS_BYTES, MAX_PENDING_EVENTS_KEEP;
6646
6610
  var init_mesh_events_pending = __esm({
6647
6611
  "src/mesh/mesh-events-pending.ts"() {
6648
6612
  "use strict";
@@ -6651,7 +6615,6 @@ var init_mesh_events_pending = __esm({
6651
6615
  init_mesh_runtime_store();
6652
6616
  init_mesh_events_utils();
6653
6617
  REFINE_TERMINAL_EVENTS = /* @__PURE__ */ new Set(["refine:completed", "refine:failed"]);
6654
- DIRECT_DELIVERED_TTL_MS = 10 * 60 * 1e3;
6655
6618
  MAX_PENDING_EVENTS_BYTES = 100 * 1024;
6656
6619
  MAX_PENDING_EVENTS_KEEP = 50;
6657
6620
  }
@@ -8013,7 +7976,6 @@ function injectMeshSystemMessage(components, args) {
8013
7976
  const workerCoordinatorDaemonId = readNonEmptyString2(
8014
7977
  sourceSession?.getState()?.settings?.meshCoordinatorDaemonId
8015
7978
  );
8016
- const localDaemonId = readNonEmptyString2(loadConfig().machineId);
8017
7979
  if (components.onMeshCoordinatorEventForwarded) {
8018
7980
  try {
8019
7981
  components.onMeshCoordinatorEventForwarded({
@@ -8368,60 +8330,6 @@ function injectMeshSystemMessage(components, args) {
8368
8330
  recoveryContext
8369
8331
  });
8370
8332
  if (!messageText) return { success: false, error: "unsupported mesh event" };
8371
- const coordinatorInstances = components.instanceManager.getByCategory("cli").filter((inst) => {
8372
- const instState = inst.getState();
8373
- if (instState.settings?.meshCoordinatorFor !== args.meshId) return false;
8374
- if (args.sourceInstanceId && instState.instanceId === args.sourceInstanceId) return false;
8375
- if (workerCoordinatorDaemonId && localDaemonId && !sameDaemonId(workerCoordinatorDaemonId, localDaemonId)) return false;
8376
- return true;
8377
- });
8378
- if (coordinatorInstances.length === 0) {
8379
- const remoteCoordinatorDaemonId = workerCoordinatorDaemonId && localDaemonId && !sameDaemonId(workerCoordinatorDaemonId, localDaemonId) ? workerCoordinatorDaemonId : "";
8380
- if (remoteCoordinatorDaemonId && components.dispatchMeshCommand) {
8381
- const forwardPayload = {
8382
- event: args.event,
8383
- meshId: args.meshId,
8384
- nodeId: args.nodeId || void 0,
8385
- workspace: readNonEmptyString2(args.metadataEvent.workspace),
8386
- ...args.metadataEvent,
8387
- ...recoveryContext ? { recoveryContext } : {}
8388
- };
8389
- components.dispatchMeshCommand(remoteCoordinatorDaemonId, "mesh_forward_event", forwardPayload).then(() => {
8390
- LOG.info("MeshEvents", `Forwarded ${args.event} for mesh ${args.meshId} to remote coordinator daemon ${remoteCoordinatorDaemonId.slice(0, 12)}\u2026`);
8391
- }).catch((error) => {
8392
- LOG.warn("MeshEvents", `Remote forward of ${args.event} failed (${error?.message || error}); queuing for backfill`);
8393
- queuePendingMeshCoordinatorEvent({
8394
- event: args.event,
8395
- meshId: args.meshId,
8396
- nodeLabel: args.nodeLabel,
8397
- nodeId: args.nodeId || void 0,
8398
- workspace: readNonEmptyString2(args.metadataEvent.workspace),
8399
- metadataEvent: { ...args.metadataEvent, ...recoveryContext ? { recoveryContext } : {} },
8400
- coordinatorMessage: messageText,
8401
- queuedAt: Date.now(),
8402
- targetCoordinatorDaemonId: remoteCoordinatorDaemonId
8403
- });
8404
- });
8405
- return { success: true, forwarded: 0, remoteForwarded: true };
8406
- }
8407
- if (queuePendingMeshCoordinatorEvent({
8408
- event: args.event,
8409
- meshId: args.meshId,
8410
- nodeLabel: args.nodeLabel,
8411
- nodeId: args.nodeId || void 0,
8412
- workspace: readNonEmptyString2(args.metadataEvent.workspace),
8413
- metadataEvent: {
8414
- ...args.metadataEvent,
8415
- ...recoveryContext ? { recoveryContext } : {}
8416
- },
8417
- coordinatorMessage: messageText,
8418
- queuedAt: Date.now(),
8419
- ...workerCoordinatorDaemonId ? { targetCoordinatorDaemonId: workerCoordinatorDaemonId } : {}
8420
- })) {
8421
- LOG.info("MeshEvents", `Queued ${args.event} for MCP coordinator (mesh ${args.meshId}${workerCoordinatorDaemonId ? `, coordinator daemon ${workerCoordinatorDaemonId}` : ""})`);
8422
- }
8423
- return { success: true, forwarded: 0 };
8424
- }
8425
8333
  const pendingEvent = {
8426
8334
  event: args.event,
8427
8335
  meshId: args.meshId,
@@ -8437,21 +8345,9 @@ function injectMeshSystemMessage(components, args) {
8437
8345
  ...workerCoordinatorDaemonId ? { targetCoordinatorDaemonId: workerCoordinatorDaemonId } : {}
8438
8346
  };
8439
8347
  if (queuePendingMeshCoordinatorEvent(pendingEvent)) {
8440
- LOG.info("MeshEvents", `Queued ${args.event} for MCP coordinator (mesh ${args.meshId})`);
8441
- }
8442
- if (localDaemonId) {
8443
- markMeshCoordinatorEventDirectDelivered(localDaemonId, pendingEvent);
8444
- }
8445
- const forceInject = shouldForceInjectMeshEvent(args.event);
8446
- for (const coord of coordinatorInstances) {
8447
- const coordState = coord.getState();
8448
- LOG.info("MeshEvents", `Forwarding mesh event to coordinator ${coordState.instanceId}${forceInject ? " (force)" : ""}`);
8449
- coord.onEvent("send_message", {
8450
- input: { text: messageText, textFallback: messageText },
8451
- ...forceInject ? { force: true } : {}
8452
- });
8348
+ LOG.info("MeshEvents", `Queued ${args.event} for coordinator (mesh ${args.meshId}${workerCoordinatorDaemonId ? `, coordinator daemon ${workerCoordinatorDaemonId}` : ""})`);
8453
8349
  }
8454
- return { success: true, forwarded: coordinatorInstances.length };
8350
+ return { success: true, forwarded: 0 };
8455
8351
  }
8456
8352
  function handleMeshForwardEvent(components, payload) {
8457
8353
  const eventName = readNonEmptyString2(payload.event);
@@ -8621,6 +8517,169 @@ var init_mesh_events_coordinator = __esm({
8621
8517
  }
8622
8518
  });
8623
8519
 
8520
+ // src/mesh/mesh-reconcile-loop.ts
8521
+ function resolveReconcileIntervalMs() {
8522
+ const raw = readNonEmptyString2(process.env.MESH_RECONCILE_INTERVAL_MS);
8523
+ if (raw) {
8524
+ const parsed = Number.parseInt(raw, 10);
8525
+ if (Number.isFinite(parsed) && parsed >= 1e3 && parsed <= 6e4) return parsed;
8526
+ }
8527
+ return DEFAULT_RECONCILE_INTERVAL_MS;
8528
+ }
8529
+ function findLiveCoordinators(components) {
8530
+ const out = [];
8531
+ for (const inst of components.instanceManager.getByCategory("cli")) {
8532
+ const state = inst.getState();
8533
+ const settings = state.settings && typeof state.settings === "object" ? state.settings : {};
8534
+ const meshId = readNonEmptyString2(settings.meshCoordinatorFor);
8535
+ if (!meshId) continue;
8536
+ const status = readNonEmptyString2(state.status).toLowerCase();
8537
+ out.push({ meshId, instance: inst, idle: status === "idle" });
8538
+ }
8539
+ return out;
8540
+ }
8541
+ function injectPendingIntoCoordinator(coordinator, pending) {
8542
+ if (!coordinator || !pending.coordinatorMessage) return;
8543
+ const force = shouldForceInjectMeshEvent(pending.event);
8544
+ coordinator.onEvent("send_message", {
8545
+ input: { text: pending.coordinatorMessage, textFallback: pending.coordinatorMessage },
8546
+ ...force ? { force: true } : {}
8547
+ });
8548
+ }
8549
+ async function runMeshReconcileTick(components) {
8550
+ const coordinators = findLiveCoordinators(components);
8551
+ if (coordinators.length === 0) {
8552
+ return;
8553
+ }
8554
+ const byMesh = /* @__PURE__ */ new Map();
8555
+ for (const c of coordinators) {
8556
+ const list = byMesh.get(c.meshId);
8557
+ if (list) list.push(c);
8558
+ else byMesh.set(c.meshId, [c]);
8559
+ }
8560
+ const localDaemonId = readNonEmptyString2(loadConfig().machineId) || void 0;
8561
+ const dispatchMeshCommand = components.dispatchMeshCommand;
8562
+ const store = (() => {
8563
+ try {
8564
+ return MeshRuntimeStore.getInstance();
8565
+ } catch {
8566
+ return void 0;
8567
+ }
8568
+ })();
8569
+ for (const [meshId, meshCoordinators] of byMesh) {
8570
+ if (dispatchMeshCommand) {
8571
+ try {
8572
+ await pullRemoteNodeQueues(components, meshId, localDaemonId);
8573
+ } catch (e) {
8574
+ LOG.warn("MeshReconcile", `Remote node pull failed for mesh ${meshId}: ${e?.message || e}`);
8575
+ }
8576
+ }
8577
+ const idleCoordinators = meshCoordinators.filter((c) => c.idle);
8578
+ if (idleCoordinators.length === 0) continue;
8579
+ if (store) {
8580
+ try {
8581
+ if (store.pendingEventCount(meshId) === 0) continue;
8582
+ } catch {
8583
+ }
8584
+ }
8585
+ let pendingEvents = [];
8586
+ try {
8587
+ pendingEvents = drainPendingMeshCoordinatorEvents(meshId, localDaemonId);
8588
+ } catch (e) {
8589
+ LOG.warn("MeshReconcile", `Drain failed for mesh ${meshId}: ${e?.message || e}`);
8590
+ continue;
8591
+ }
8592
+ if (pendingEvents.length === 0) continue;
8593
+ LOG.info("MeshReconcile", `Reconcile inject: ${pendingEvents.length} pending event(s) \u2192 ${idleCoordinators.length} idle coordinator(s) for mesh ${meshId}`);
8594
+ for (const pending of pendingEvents) {
8595
+ for (const c of idleCoordinators) {
8596
+ injectPendingIntoCoordinator(c.instance, pending);
8597
+ }
8598
+ }
8599
+ }
8600
+ }
8601
+ async function pullRemoteNodeQueues(components, meshId, localDaemonId) {
8602
+ const dispatchMeshCommand = components.dispatchMeshCommand;
8603
+ if (!dispatchMeshCommand) return;
8604
+ const mesh = listMeshes().find((m) => m.id === meshId);
8605
+ if (!mesh) return;
8606
+ const pendingEventArgs = {
8607
+ meshId,
8608
+ ...localDaemonId ? { coordinatorDaemonId: localDaemonId } : {}
8609
+ };
8610
+ for (const node of mesh.nodes) {
8611
+ const nodeDaemonId = readNonEmptyString2(node.daemonId);
8612
+ if (!nodeDaemonId) continue;
8613
+ if (localDaemonId && nodeDaemonId === localDaemonId) continue;
8614
+ let events;
8615
+ try {
8616
+ events = await dispatchMeshCommand(nodeDaemonId, "get_pending_mesh_events", pendingEventArgs);
8617
+ } catch {
8618
+ continue;
8619
+ }
8620
+ const list = extractPendingEvents(events).filter((e) => readNonEmptyString2(e?.meshId) === meshId);
8621
+ for (const event of list) {
8622
+ const payload = buildForwardPayloadFromPending(event);
8623
+ if (!payload.event || !payload.meshId) continue;
8624
+ try {
8625
+ handleMeshForwardEvent(components, payload);
8626
+ } catch {
8627
+ }
8628
+ }
8629
+ }
8630
+ }
8631
+ function extractPendingEvents(raw) {
8632
+ if (Array.isArray(raw)) return raw;
8633
+ if (raw && typeof raw === "object") {
8634
+ const events = raw.events;
8635
+ if (Array.isArray(events)) return events;
8636
+ }
8637
+ return [];
8638
+ }
8639
+ function buildForwardPayloadFromPending(event) {
8640
+ const metadata = event?.metadataEvent && typeof event.metadataEvent === "object" ? event.metadataEvent : {};
8641
+ return {
8642
+ event: readNonEmptyString2(event?.event),
8643
+ meshId: readNonEmptyString2(event?.meshId),
8644
+ nodeId: readNonEmptyString2(event?.nodeId) || readNonEmptyString2(metadata.meshNodeId),
8645
+ workspace: readNonEmptyString2(event?.workspace) || readNonEmptyString2(metadata.workspace),
8646
+ ...metadata
8647
+ };
8648
+ }
8649
+ function setupMeshReconcileLoop(components) {
8650
+ const intervalMs = resolveReconcileIntervalMs();
8651
+ let running = false;
8652
+ const timer = setInterval(() => {
8653
+ if (running) return;
8654
+ running = true;
8655
+ void runMeshReconcileTick(components).catch((e) => LOG.warn("MeshReconcile", `Reconcile tick error: ${e?.message || e}`)).finally(() => {
8656
+ running = false;
8657
+ });
8658
+ }, intervalMs);
8659
+ if (typeof timer.unref === "function") timer.unref();
8660
+ LOG.info("MeshReconcile", `Mesh reconcile loop started (interval ${intervalMs}ms)`);
8661
+ return {
8662
+ stop() {
8663
+ clearInterval(timer);
8664
+ LOG.info("MeshReconcile", "Mesh reconcile loop stopped");
8665
+ }
8666
+ };
8667
+ }
8668
+ var DEFAULT_RECONCILE_INTERVAL_MS;
8669
+ var init_mesh_reconcile_loop = __esm({
8670
+ "src/mesh/mesh-reconcile-loop.ts"() {
8671
+ "use strict";
8672
+ init_config();
8673
+ init_mesh_config();
8674
+ init_logger();
8675
+ init_mesh_events_pending();
8676
+ init_mesh_runtime_store();
8677
+ init_mesh_events_coordinator();
8678
+ init_mesh_events_utils();
8679
+ DEFAULT_RECONCILE_INTERVAL_MS = 4e3;
8680
+ }
8681
+ });
8682
+
8624
8683
  // src/mesh/mesh-events.ts
8625
8684
  var mesh_events_exports = {};
8626
8685
  __export(mesh_events_exports, {
@@ -8631,10 +8690,11 @@ __export(mesh_events_exports, {
8631
8690
  getPendingMeshCoordinatorEvents: () => getPendingMeshCoordinatorEvents,
8632
8691
  handleMeshForwardEvent: () => handleMeshForwardEvent,
8633
8692
  isMeshCoordinatorEvent: () => isMeshCoordinatorEvent,
8634
- markMeshCoordinatorEventDirectDelivered: () => markMeshCoordinatorEventDirectDelivered,
8635
8693
  queuePendingMeshCoordinatorEvent: () => queuePendingMeshCoordinatorEvent,
8636
8694
  reconcileDirectDispatchCompletionFromTranscript: () => reconcileDirectDispatchCompletionFromTranscript,
8695
+ runMeshReconcileTick: () => runMeshReconcileTick,
8637
8696
  setupMeshEventForwarding: () => setupMeshEventForwarding,
8697
+ setupMeshReconcileLoop: () => setupMeshReconcileLoop,
8638
8698
  triggerMeshQueue: () => triggerMeshQueue,
8639
8699
  tryAssignQueueTask: () => tryAssignQueueTask
8640
8700
  });
@@ -8643,6 +8703,7 @@ var init_mesh_events = __esm({
8643
8703
  "use strict";
8644
8704
  init_mesh_events_pending();
8645
8705
  init_mesh_events_stale();
8706
+ init_mesh_reconcile_loop();
8646
8707
  init_mesh_events_coordinator();
8647
8708
  }
8648
8709
  });
@@ -55108,6 +55169,7 @@ var SessionRegistry = class {
55108
55169
  init_logger();
55109
55170
  init_config();
55110
55171
  init_mesh_events();
55172
+ init_mesh_reconcile_loop();
55111
55173
 
55112
55174
  // src/boot/process-hardening.ts
55113
55175
  var _hardened = false;
@@ -55309,6 +55371,7 @@ async function initDaemonComponents(config) {
55309
55371
  onMeshCoordinatorEventForwarded: config.onMeshCoordinatorEventForwarded
55310
55372
  };
55311
55373
  setupMeshEventForwarding(components);
55374
+ components.meshReconcileLoop = setupMeshReconcileLoop(components);
55312
55375
  setImmediate(() => void router.resumePendingRefineJobsOnStartup());
55313
55376
  return components;
55314
55377
  }
@@ -55334,10 +55397,15 @@ async function shutdownDaemonComponents(components) {
55334
55397
  agentStreamManager,
55335
55398
  cliManager,
55336
55399
  instanceManager,
55337
- cdpManagers
55400
+ cdpManagers,
55401
+ meshReconcileLoop
55338
55402
  } = components;
55339
55403
  poller.stop();
55340
55404
  cdpInitializer.stop();
55405
+ try {
55406
+ meshReconcileLoop?.stop();
55407
+ } catch {
55408
+ }
55341
55409
  try {
55342
55410
  if (agentStreamManager) {
55343
55411
  await agentStreamManager.dispose(cdpManagers);