@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.
@@ -82,6 +82,9 @@ export interface DaemonComponents {
82
82
  refreshProviderAvailability: (providerType?: string) => Promise<void>;
83
83
  dispatchMeshCommand?: (daemonId: string, command: string, args: Record<string, unknown>) => Promise<any>;
84
84
  onMeshCoordinatorEventForwarded?: (payload: Record<string, unknown>) => void;
85
+ meshReconcileLoop?: {
86
+ stop(): void;
87
+ };
85
88
  }
86
89
  export interface DaemonDevSupportOptions {
87
90
  components: DaemonComponents;
package/dist/index.js CHANGED
@@ -275,10 +275,10 @@ function readInjected(value) {
275
275
  }
276
276
  function getDaemonBuildInfo() {
277
277
  if (cached) return cached;
278
- const commit = readInjected(true ? "e4a64afbd050fa5167c383b1fc6f9df5f8e48ba2" : void 0) ?? "unknown";
279
- const commitShort = readInjected(true ? "e4a64afb" : void 0) ?? (commit !== "unknown" ? commit.slice(0, 7) : "unknown");
280
- const version = readInjected(true ? "0.9.82-rc.273" : void 0) ?? readInjected(typeof process !== "undefined" ? process.env?.ADHDEV_PKG_VERSION : void 0) ?? "unknown";
281
- const builtAt = readInjected(true ? "2026-06-15T05:37:07.054Z" : void 0);
278
+ const commit = readInjected(true ? "89d1ac61612c1dfe7fac53be61ec5dbfa67d00b2" : void 0) ?? "unknown";
279
+ const commitShort = readInjected(true ? "89d1ac61" : void 0) ?? (commit !== "unknown" ? commit.slice(0, 7) : "unknown");
280
+ const version = readInjected(true ? "0.9.82-rc.274" : void 0) ?? readInjected(typeof process !== "undefined" ? process.env?.ADHDEV_PKG_VERSION : void 0) ?? "unknown";
281
+ const builtAt = readInjected(true ? "2026-06-15T07:04:52.710Z" : void 0);
282
282
  cached = builtAt ? { commit, commitShort, version, builtAt } : { commit, commitShort, version };
283
283
  return cached;
284
284
  }
@@ -6168,16 +6168,6 @@ function readNonEmptyString2(value) {
6168
6168
  function readRecord3(value) {
6169
6169
  return value && typeof value === "object" && !Array.isArray(value) ? value : void 0;
6170
6170
  }
6171
- function canonicalDaemonId(value) {
6172
- const id = readNonEmptyString2(value);
6173
- if (!id) return "";
6174
- return id.replace(/^(?:daemon|standalone)_/, "");
6175
- }
6176
- function sameDaemonId(a, b) {
6177
- const ca = canonicalDaemonId(a);
6178
- const cb = canonicalDaemonId(b);
6179
- return ca !== "" && ca === cb;
6180
- }
6181
6171
  function buildMeshWorkerRelayStamp(currentSettings, meshContext) {
6182
6172
  if (!meshContext) return void 0;
6183
6173
  const settings = currentSettings && typeof currentSettings === "object" ? currentSettings : {};
@@ -6378,29 +6368,6 @@ function buildPendingEventFingerprint(event) {
6378
6368
  timestamp || ""
6379
6369
  ].join("::");
6380
6370
  }
6381
- function markMeshCoordinatorEventDirectDelivered(coordinatorDaemonId, event) {
6382
- const canonical = canonicalDaemonId(coordinatorDaemonId);
6383
- if (!canonical) return;
6384
- const fingerprint = buildPendingEventFingerprint(event);
6385
- if (!fingerprint.trim()) return;
6386
- try {
6387
- const store = MeshRuntimeStore.getInstance();
6388
- store.recordDirectDelivered(canonical, fingerprint, DIRECT_DELIVERED_TTL_MS);
6389
- store.sweepExpiredDirectDelivered();
6390
- } catch {
6391
- }
6392
- }
6393
- function wasDirectDeliveredToCoordinator(coordinatorDaemonId, event) {
6394
- const canonical = canonicalDaemonId(coordinatorDaemonId);
6395
- if (!canonical) return false;
6396
- const fingerprint = buildPendingEventFingerprint(event);
6397
- if (!fingerprint.trim()) return false;
6398
- try {
6399
- return MeshRuntimeStore.getInstance().wasDirectDelivered(canonical, fingerprint);
6400
- } catch {
6401
- return false;
6402
- }
6403
- }
6404
6371
  function hasPendingCoordinatorEventDuplicate(event) {
6405
6372
  const fingerprint = buildPendingEventFingerprint(event);
6406
6373
  if (!fingerprint.trim()) return false;
@@ -6599,9 +6566,7 @@ function drainPendingMeshCoordinatorEvents(meshId, coordinatorDaemonId) {
6599
6566
  for (const event of filtered) pushUnique(event);
6600
6567
  }
6601
6568
  if (merged.length === 0) return [];
6602
- const deliverable = coordinatorDaemonId ? merged.filter((event) => !wasDirectDeliveredToCoordinator(coordinatorDaemonId, event)) : merged;
6603
- if (deliverable.length === 0) return [];
6604
- return reconcilePendingMeshCoordinatorEvents(meshId, deliverable);
6569
+ return reconcilePendingMeshCoordinatorEvents(meshId, merged);
6605
6570
  }
6606
6571
  function getPendingMeshCoordinatorEvents(meshId, coordinatorDaemonId) {
6607
6572
  if (!meshId) return [];
@@ -6628,8 +6593,7 @@ function getPendingMeshCoordinatorEvents(meshId, coordinatorDaemonId) {
6628
6593
  for (const event of readPendingMeshCoordinatorEventsFromDisk(meshId, coordinatorDaemonId)) {
6629
6594
  pushUnique(event);
6630
6595
  }
6631
- const deliverable = coordinatorDaemonId ? merged.filter((event) => !wasDirectDeliveredToCoordinator(coordinatorDaemonId, event)) : merged;
6632
- return reconcilePendingMeshCoordinatorEvents(meshId, deliverable);
6596
+ return reconcilePendingMeshCoordinatorEvents(meshId, merged);
6633
6597
  }
6634
6598
  function clearPendingMeshCoordinatorEvents(meshId, coordinatorDaemonId) {
6635
6599
  if (!meshId) return;
@@ -6645,7 +6609,7 @@ function clearPendingMeshCoordinatorEvents(meshId, coordinatorDaemonId) {
6645
6609
  }
6646
6610
  }
6647
6611
  }
6648
- var import_fs8, import_path8, import_crypto7, REFINE_TERMINAL_EVENTS, DIRECT_DELIVERED_TTL_MS, MAX_PENDING_EVENTS_BYTES, MAX_PENDING_EVENTS_KEEP;
6612
+ var import_fs8, import_path8, import_crypto7, REFINE_TERMINAL_EVENTS, MAX_PENDING_EVENTS_BYTES, MAX_PENDING_EVENTS_KEEP;
6649
6613
  var init_mesh_events_pending = __esm({
6650
6614
  "src/mesh/mesh-events-pending.ts"() {
6651
6615
  "use strict";
@@ -6657,7 +6621,6 @@ var init_mesh_events_pending = __esm({
6657
6621
  init_mesh_runtime_store();
6658
6622
  init_mesh_events_utils();
6659
6623
  REFINE_TERMINAL_EVENTS = /* @__PURE__ */ new Set(["refine:completed", "refine:failed"]);
6660
- DIRECT_DELIVERED_TTL_MS = 10 * 60 * 1e3;
6661
6624
  MAX_PENDING_EVENTS_BYTES = 100 * 1024;
6662
6625
  MAX_PENDING_EVENTS_KEEP = 50;
6663
6626
  }
@@ -8019,7 +7982,6 @@ function injectMeshSystemMessage(components, args) {
8019
7982
  const workerCoordinatorDaemonId = readNonEmptyString2(
8020
7983
  sourceSession?.getState()?.settings?.meshCoordinatorDaemonId
8021
7984
  );
8022
- const localDaemonId = readNonEmptyString2(loadConfig().machineId);
8023
7985
  if (components.onMeshCoordinatorEventForwarded) {
8024
7986
  try {
8025
7987
  components.onMeshCoordinatorEventForwarded({
@@ -8374,60 +8336,6 @@ function injectMeshSystemMessage(components, args) {
8374
8336
  recoveryContext
8375
8337
  });
8376
8338
  if (!messageText) return { success: false, error: "unsupported mesh event" };
8377
- const coordinatorInstances = components.instanceManager.getByCategory("cli").filter((inst) => {
8378
- const instState = inst.getState();
8379
- if (instState.settings?.meshCoordinatorFor !== args.meshId) return false;
8380
- if (args.sourceInstanceId && instState.instanceId === args.sourceInstanceId) return false;
8381
- if (workerCoordinatorDaemonId && localDaemonId && !sameDaemonId(workerCoordinatorDaemonId, localDaemonId)) return false;
8382
- return true;
8383
- });
8384
- if (coordinatorInstances.length === 0) {
8385
- const remoteCoordinatorDaemonId = workerCoordinatorDaemonId && localDaemonId && !sameDaemonId(workerCoordinatorDaemonId, localDaemonId) ? workerCoordinatorDaemonId : "";
8386
- if (remoteCoordinatorDaemonId && components.dispatchMeshCommand) {
8387
- const forwardPayload = {
8388
- event: args.event,
8389
- meshId: args.meshId,
8390
- nodeId: args.nodeId || void 0,
8391
- workspace: readNonEmptyString2(args.metadataEvent.workspace),
8392
- ...args.metadataEvent,
8393
- ...recoveryContext ? { recoveryContext } : {}
8394
- };
8395
- components.dispatchMeshCommand(remoteCoordinatorDaemonId, "mesh_forward_event", forwardPayload).then(() => {
8396
- LOG.info("MeshEvents", `Forwarded ${args.event} for mesh ${args.meshId} to remote coordinator daemon ${remoteCoordinatorDaemonId.slice(0, 12)}\u2026`);
8397
- }).catch((error) => {
8398
- LOG.warn("MeshEvents", `Remote forward of ${args.event} failed (${error?.message || error}); queuing for backfill`);
8399
- queuePendingMeshCoordinatorEvent({
8400
- event: args.event,
8401
- meshId: args.meshId,
8402
- nodeLabel: args.nodeLabel,
8403
- nodeId: args.nodeId || void 0,
8404
- workspace: readNonEmptyString2(args.metadataEvent.workspace),
8405
- metadataEvent: { ...args.metadataEvent, ...recoveryContext ? { recoveryContext } : {} },
8406
- coordinatorMessage: messageText,
8407
- queuedAt: Date.now(),
8408
- targetCoordinatorDaemonId: remoteCoordinatorDaemonId
8409
- });
8410
- });
8411
- return { success: true, forwarded: 0, remoteForwarded: true };
8412
- }
8413
- if (queuePendingMeshCoordinatorEvent({
8414
- event: args.event,
8415
- meshId: args.meshId,
8416
- nodeLabel: args.nodeLabel,
8417
- nodeId: args.nodeId || void 0,
8418
- workspace: readNonEmptyString2(args.metadataEvent.workspace),
8419
- metadataEvent: {
8420
- ...args.metadataEvent,
8421
- ...recoveryContext ? { recoveryContext } : {}
8422
- },
8423
- coordinatorMessage: messageText,
8424
- queuedAt: Date.now(),
8425
- ...workerCoordinatorDaemonId ? { targetCoordinatorDaemonId: workerCoordinatorDaemonId } : {}
8426
- })) {
8427
- LOG.info("MeshEvents", `Queued ${args.event} for MCP coordinator (mesh ${args.meshId}${workerCoordinatorDaemonId ? `, coordinator daemon ${workerCoordinatorDaemonId}` : ""})`);
8428
- }
8429
- return { success: true, forwarded: 0 };
8430
- }
8431
8339
  const pendingEvent = {
8432
8340
  event: args.event,
8433
8341
  meshId: args.meshId,
@@ -8443,21 +8351,9 @@ function injectMeshSystemMessage(components, args) {
8443
8351
  ...workerCoordinatorDaemonId ? { targetCoordinatorDaemonId: workerCoordinatorDaemonId } : {}
8444
8352
  };
8445
8353
  if (queuePendingMeshCoordinatorEvent(pendingEvent)) {
8446
- LOG.info("MeshEvents", `Queued ${args.event} for MCP coordinator (mesh ${args.meshId})`);
8447
- }
8448
- if (localDaemonId) {
8449
- markMeshCoordinatorEventDirectDelivered(localDaemonId, pendingEvent);
8450
- }
8451
- const forceInject = shouldForceInjectMeshEvent(args.event);
8452
- for (const coord of coordinatorInstances) {
8453
- const coordState = coord.getState();
8454
- LOG.info("MeshEvents", `Forwarding mesh event to coordinator ${coordState.instanceId}${forceInject ? " (force)" : ""}`);
8455
- coord.onEvent("send_message", {
8456
- input: { text: messageText, textFallback: messageText },
8457
- ...forceInject ? { force: true } : {}
8458
- });
8354
+ LOG.info("MeshEvents", `Queued ${args.event} for coordinator (mesh ${args.meshId}${workerCoordinatorDaemonId ? `, coordinator daemon ${workerCoordinatorDaemonId}` : ""})`);
8459
8355
  }
8460
- return { success: true, forwarded: coordinatorInstances.length };
8356
+ return { success: true, forwarded: 0 };
8461
8357
  }
8462
8358
  function handleMeshForwardEvent(components, payload) {
8463
8359
  const eventName = readNonEmptyString2(payload.event);
@@ -8628,6 +8524,169 @@ var init_mesh_events_coordinator = __esm({
8628
8524
  }
8629
8525
  });
8630
8526
 
8527
+ // src/mesh/mesh-reconcile-loop.ts
8528
+ function resolveReconcileIntervalMs() {
8529
+ const raw = readNonEmptyString2(process.env.MESH_RECONCILE_INTERVAL_MS);
8530
+ if (raw) {
8531
+ const parsed = Number.parseInt(raw, 10);
8532
+ if (Number.isFinite(parsed) && parsed >= 1e3 && parsed <= 6e4) return parsed;
8533
+ }
8534
+ return DEFAULT_RECONCILE_INTERVAL_MS;
8535
+ }
8536
+ function findLiveCoordinators(components) {
8537
+ const out = [];
8538
+ for (const inst of components.instanceManager.getByCategory("cli")) {
8539
+ const state = inst.getState();
8540
+ const settings = state.settings && typeof state.settings === "object" ? state.settings : {};
8541
+ const meshId = readNonEmptyString2(settings.meshCoordinatorFor);
8542
+ if (!meshId) continue;
8543
+ const status = readNonEmptyString2(state.status).toLowerCase();
8544
+ out.push({ meshId, instance: inst, idle: status === "idle" });
8545
+ }
8546
+ return out;
8547
+ }
8548
+ function injectPendingIntoCoordinator(coordinator, pending) {
8549
+ if (!coordinator || !pending.coordinatorMessage) return;
8550
+ const force = shouldForceInjectMeshEvent(pending.event);
8551
+ coordinator.onEvent("send_message", {
8552
+ input: { text: pending.coordinatorMessage, textFallback: pending.coordinatorMessage },
8553
+ ...force ? { force: true } : {}
8554
+ });
8555
+ }
8556
+ async function runMeshReconcileTick(components) {
8557
+ const coordinators = findLiveCoordinators(components);
8558
+ if (coordinators.length === 0) {
8559
+ return;
8560
+ }
8561
+ const byMesh = /* @__PURE__ */ new Map();
8562
+ for (const c of coordinators) {
8563
+ const list = byMesh.get(c.meshId);
8564
+ if (list) list.push(c);
8565
+ else byMesh.set(c.meshId, [c]);
8566
+ }
8567
+ const localDaemonId = readNonEmptyString2(loadConfig().machineId) || void 0;
8568
+ const dispatchMeshCommand = components.dispatchMeshCommand;
8569
+ const store = (() => {
8570
+ try {
8571
+ return MeshRuntimeStore.getInstance();
8572
+ } catch {
8573
+ return void 0;
8574
+ }
8575
+ })();
8576
+ for (const [meshId, meshCoordinators] of byMesh) {
8577
+ if (dispatchMeshCommand) {
8578
+ try {
8579
+ await pullRemoteNodeQueues(components, meshId, localDaemonId);
8580
+ } catch (e) {
8581
+ LOG.warn("MeshReconcile", `Remote node pull failed for mesh ${meshId}: ${e?.message || e}`);
8582
+ }
8583
+ }
8584
+ const idleCoordinators = meshCoordinators.filter((c) => c.idle);
8585
+ if (idleCoordinators.length === 0) continue;
8586
+ if (store) {
8587
+ try {
8588
+ if (store.pendingEventCount(meshId) === 0) continue;
8589
+ } catch {
8590
+ }
8591
+ }
8592
+ let pendingEvents = [];
8593
+ try {
8594
+ pendingEvents = drainPendingMeshCoordinatorEvents(meshId, localDaemonId);
8595
+ } catch (e) {
8596
+ LOG.warn("MeshReconcile", `Drain failed for mesh ${meshId}: ${e?.message || e}`);
8597
+ continue;
8598
+ }
8599
+ if (pendingEvents.length === 0) continue;
8600
+ LOG.info("MeshReconcile", `Reconcile inject: ${pendingEvents.length} pending event(s) \u2192 ${idleCoordinators.length} idle coordinator(s) for mesh ${meshId}`);
8601
+ for (const pending of pendingEvents) {
8602
+ for (const c of idleCoordinators) {
8603
+ injectPendingIntoCoordinator(c.instance, pending);
8604
+ }
8605
+ }
8606
+ }
8607
+ }
8608
+ async function pullRemoteNodeQueues(components, meshId, localDaemonId) {
8609
+ const dispatchMeshCommand = components.dispatchMeshCommand;
8610
+ if (!dispatchMeshCommand) return;
8611
+ const mesh = listMeshes().find((m) => m.id === meshId);
8612
+ if (!mesh) return;
8613
+ const pendingEventArgs = {
8614
+ meshId,
8615
+ ...localDaemonId ? { coordinatorDaemonId: localDaemonId } : {}
8616
+ };
8617
+ for (const node of mesh.nodes) {
8618
+ const nodeDaemonId = readNonEmptyString2(node.daemonId);
8619
+ if (!nodeDaemonId) continue;
8620
+ if (localDaemonId && nodeDaemonId === localDaemonId) continue;
8621
+ let events;
8622
+ try {
8623
+ events = await dispatchMeshCommand(nodeDaemonId, "get_pending_mesh_events", pendingEventArgs);
8624
+ } catch {
8625
+ continue;
8626
+ }
8627
+ const list = extractPendingEvents(events).filter((e) => readNonEmptyString2(e?.meshId) === meshId);
8628
+ for (const event of list) {
8629
+ const payload = buildForwardPayloadFromPending(event);
8630
+ if (!payload.event || !payload.meshId) continue;
8631
+ try {
8632
+ handleMeshForwardEvent(components, payload);
8633
+ } catch {
8634
+ }
8635
+ }
8636
+ }
8637
+ }
8638
+ function extractPendingEvents(raw) {
8639
+ if (Array.isArray(raw)) return raw;
8640
+ if (raw && typeof raw === "object") {
8641
+ const events = raw.events;
8642
+ if (Array.isArray(events)) return events;
8643
+ }
8644
+ return [];
8645
+ }
8646
+ function buildForwardPayloadFromPending(event) {
8647
+ const metadata = event?.metadataEvent && typeof event.metadataEvent === "object" ? event.metadataEvent : {};
8648
+ return {
8649
+ event: readNonEmptyString2(event?.event),
8650
+ meshId: readNonEmptyString2(event?.meshId),
8651
+ nodeId: readNonEmptyString2(event?.nodeId) || readNonEmptyString2(metadata.meshNodeId),
8652
+ workspace: readNonEmptyString2(event?.workspace) || readNonEmptyString2(metadata.workspace),
8653
+ ...metadata
8654
+ };
8655
+ }
8656
+ function setupMeshReconcileLoop(components) {
8657
+ const intervalMs = resolveReconcileIntervalMs();
8658
+ let running = false;
8659
+ const timer = setInterval(() => {
8660
+ if (running) return;
8661
+ running = true;
8662
+ void runMeshReconcileTick(components).catch((e) => LOG.warn("MeshReconcile", `Reconcile tick error: ${e?.message || e}`)).finally(() => {
8663
+ running = false;
8664
+ });
8665
+ }, intervalMs);
8666
+ if (typeof timer.unref === "function") timer.unref();
8667
+ LOG.info("MeshReconcile", `Mesh reconcile loop started (interval ${intervalMs}ms)`);
8668
+ return {
8669
+ stop() {
8670
+ clearInterval(timer);
8671
+ LOG.info("MeshReconcile", "Mesh reconcile loop stopped");
8672
+ }
8673
+ };
8674
+ }
8675
+ var DEFAULT_RECONCILE_INTERVAL_MS;
8676
+ var init_mesh_reconcile_loop = __esm({
8677
+ "src/mesh/mesh-reconcile-loop.ts"() {
8678
+ "use strict";
8679
+ init_config();
8680
+ init_mesh_config();
8681
+ init_logger();
8682
+ init_mesh_events_pending();
8683
+ init_mesh_runtime_store();
8684
+ init_mesh_events_coordinator();
8685
+ init_mesh_events_utils();
8686
+ DEFAULT_RECONCILE_INTERVAL_MS = 4e3;
8687
+ }
8688
+ });
8689
+
8631
8690
  // src/mesh/mesh-events.ts
8632
8691
  var mesh_events_exports = {};
8633
8692
  __export(mesh_events_exports, {
@@ -8638,10 +8697,11 @@ __export(mesh_events_exports, {
8638
8697
  getPendingMeshCoordinatorEvents: () => getPendingMeshCoordinatorEvents,
8639
8698
  handleMeshForwardEvent: () => handleMeshForwardEvent,
8640
8699
  isMeshCoordinatorEvent: () => isMeshCoordinatorEvent,
8641
- markMeshCoordinatorEventDirectDelivered: () => markMeshCoordinatorEventDirectDelivered,
8642
8700
  queuePendingMeshCoordinatorEvent: () => queuePendingMeshCoordinatorEvent,
8643
8701
  reconcileDirectDispatchCompletionFromTranscript: () => reconcileDirectDispatchCompletionFromTranscript,
8702
+ runMeshReconcileTick: () => runMeshReconcileTick,
8644
8703
  setupMeshEventForwarding: () => setupMeshEventForwarding,
8704
+ setupMeshReconcileLoop: () => setupMeshReconcileLoop,
8645
8705
  triggerMeshQueue: () => triggerMeshQueue,
8646
8706
  tryAssignQueueTask: () => tryAssignQueueTask
8647
8707
  });
@@ -8650,6 +8710,7 @@ var init_mesh_events = __esm({
8650
8710
  "use strict";
8651
8711
  init_mesh_events_pending();
8652
8712
  init_mesh_events_stale();
8713
+ init_mesh_reconcile_loop();
8653
8714
  init_mesh_events_coordinator();
8654
8715
  }
8655
8716
  });
@@ -55437,6 +55498,7 @@ var SessionRegistry = class {
55437
55498
  init_logger();
55438
55499
  init_config();
55439
55500
  init_mesh_events();
55501
+ init_mesh_reconcile_loop();
55440
55502
 
55441
55503
  // src/boot/process-hardening.ts
55442
55504
  var _hardened = false;
@@ -55638,6 +55700,7 @@ async function initDaemonComponents(config) {
55638
55700
  onMeshCoordinatorEventForwarded: config.onMeshCoordinatorEventForwarded
55639
55701
  };
55640
55702
  setupMeshEventForwarding(components);
55703
+ components.meshReconcileLoop = setupMeshReconcileLoop(components);
55641
55704
  setImmediate(() => void router.resumePendingRefineJobsOnStartup());
55642
55705
  return components;
55643
55706
  }
@@ -55663,10 +55726,15 @@ async function shutdownDaemonComponents(components) {
55663
55726
  agentStreamManager,
55664
55727
  cliManager,
55665
55728
  instanceManager,
55666
- cdpManagers
55729
+ cdpManagers,
55730
+ meshReconcileLoop
55667
55731
  } = components;
55668
55732
  poller.stop();
55669
55733
  cdpInitializer.stop();
55734
+ try {
55735
+ meshReconcileLoop?.stop();
55736
+ } catch {
55737
+ }
55670
55738
  try {
55671
55739
  if (agentStreamManager) {
55672
55740
  await agentStreamManager.dispose(cdpManagers);