@adhdev/daemon-core 0.9.82-rc.487 → 0.9.82-rc.488

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.
@@ -13,6 +13,20 @@ export declare function buildRefineJobHandle(self: DaemonCommandRouter, args: {
13
13
  retryOfJobId?: string;
14
14
  coordinatorDaemonId?: string;
15
15
  }): MeshRefineJobHandle;
16
+ /**
17
+ * Slim the terminal-stage refine result down to the fields a coordinator needs to
18
+ * decide next-step, dropping the heavy per-command / per-entry detail.
19
+ *
20
+ * The full `CommandRouterResult` (with `validationSummary.commandsRun[]` carrying
21
+ * per-command stdout/stderr, `rejectedCommands`, `suggestions`, `suggestedConfig`,
22
+ * the full `patchEquivalence`, and `submoduleReachability.entries[]`/`.unreachable[]`)
23
+ * routinely exceeds 70KB and overflows the coordinator token limit when it rides on a
24
+ * `mesh_wait_events` payload. The full detail is still persisted verbatim to the ledger
25
+ * (`appendRefineJobLedger`) and `terminalRefineJobs`, so slimming only the EVENT loses
26
+ * nothing — the coordinator can pull the full record on demand via
27
+ * `evidence.ledgerCommand` / `taskHistoryKind`.
28
+ */
29
+ export declare function slimRefineEventResult(result: Record<string, unknown>): Record<string, unknown>;
16
30
  export declare function queueRefineJobEvent(self: DaemonCommandRouter, event: 'refine:accepted' | 'refine:completed' | 'refine:failed', handle: MeshRefineJobHandle, result?: Record<string, unknown>): void;
17
31
  export declare function appendRefineJobLedger(self: DaemonCommandRouter, kind: 'task_dispatched' | 'task_completed' | 'task_failed', handle: MeshRefineJobHandle, result?: Record<string, unknown>): Promise<void>;
18
32
  /**
package/dist/index.js CHANGED
@@ -414,10 +414,10 @@ function readInjected(value) {
414
414
  }
415
415
  function getDaemonBuildInfo() {
416
416
  if (cached) return cached;
417
- const commit = readInjected(true ? "ef3ded0f5df148982ed222411ea08c6ab0fdb39b" : void 0) ?? "unknown";
418
- const commitShort = readInjected(true ? "ef3ded0f" : void 0) ?? (commit !== "unknown" ? commit.slice(0, 7) : "unknown");
419
- const version = readInjected(true ? "0.9.82-rc.487" : void 0) ?? readInjected(typeof process !== "undefined" ? process.env?.ADHDEV_PKG_VERSION : void 0) ?? "unknown";
420
- const builtAt = readInjected(true ? "2026-07-10T01:53:17.254Z" : void 0);
417
+ const commit = readInjected(true ? "a659fcbbe9641240de42b95804a16b8852c5b4eb" : void 0) ?? "unknown";
418
+ const commitShort = readInjected(true ? "a659fcbb" : void 0) ?? (commit !== "unknown" ? commit.slice(0, 7) : "unknown");
419
+ const version = readInjected(true ? "0.9.82-rc.488" : void 0) ?? readInjected(typeof process !== "undefined" ? process.env?.ADHDEV_PKG_VERSION : void 0) ?? "unknown";
420
+ const builtAt = readInjected(true ? "2026-07-10T03:46:52.780Z" : void 0);
421
421
  cached = builtAt ? { commit, commitShort, version, builtAt } : { commit, commitShort, version };
422
422
  return cached;
423
423
  }
@@ -6175,6 +6175,7 @@ function reclaimStrandedAssignedTask(meshId, taskId, opts) {
6175
6175
  delete entry.assignedSessionId;
6176
6176
  delete entry.assignedProviderType;
6177
6177
  delete entry.dispatchTimestamp;
6178
+ entry.dispatchNonce = (entry.dispatchNonce || 0) + 1;
6178
6179
  entry.strandedReclaimCount = reclaims;
6179
6180
  entry.updatedAt = now;
6180
6181
  endTaskDispatchInFlight(meshId, taskId);
@@ -7199,6 +7200,7 @@ var init_mesh_runtime_store = __esm({
7199
7200
  entry.assignedSessionId = sessionId;
7200
7201
  if (providerType) entry.assignedProviderType = providerType;
7201
7202
  entry.dispatchTimestamp = now;
7203
+ entry.dispatchNonce = (entry.dispatchNonce || 0) + 1;
7202
7204
  entry.updatedAt = now;
7203
7205
  this.db.prepare(`
7204
7206
  UPDATE mesh_queue SET
@@ -15657,6 +15659,10 @@ function tryAssignQueueTask(components, meshId, nodeId, sessionId, providerType)
15657
15659
  meshId,
15658
15660
  nodeId,
15659
15661
  taskId: task.id,
15662
+ // REDRIVE-DUP: carry the current dispatch nonce so the worker can echo it
15663
+ // back on generating_started; a reclaim bumps this row's nonce, making an
15664
+ // already-in-flight stale inject rejectable on arrival.
15665
+ ...typeof task.dispatchNonce === "number" ? { dispatchNonce: task.dispatchNonce } : {},
15660
15666
  ...localDaemonIdForDispatch ? { coordinatorDaemonId: localDaemonIdForDispatch } : {},
15661
15667
  ...sourceCoordinatorSessionId ? { coordinatorSessionId: sourceCoordinatorSessionId } : {}
15662
15668
  }
@@ -15715,6 +15721,8 @@ function tryAssignQueueTask(components, meshId, nodeId, sessionId, providerType)
15715
15721
  meshId,
15716
15722
  nodeId,
15717
15723
  taskId: task.id,
15724
+ // REDRIVE-DUP: carry the current dispatch nonce (see remote branch above).
15725
+ ...typeof task.dispatchNonce === "number" ? { dispatchNonce: task.dispatchNonce } : {},
15718
15726
  ...localCoordinatorDaemonId() ? { coordinatorDaemonId: localCoordinatorDaemonId() } : {},
15719
15727
  ...readNonEmptyString2(task.sourceCoordinatorSessionId) ? { coordinatorSessionId: readNonEmptyString2(task.sourceCoordinatorSessionId) } : {}
15720
15728
  }
@@ -19698,6 +19706,42 @@ function sourceWorkerAutoApproves(components, sessionId) {
19698
19706
  return false;
19699
19707
  }
19700
19708
  }
19709
+ function stopStaleMeshWorker(components, args) {
19710
+ const { meshId, sessionId, providerType } = args;
19711
+ const stopArgs = {
19712
+ targetSessionId: sessionId,
19713
+ ...providerType ? { cliType: providerType } : {},
19714
+ mode: "hard",
19715
+ reason: "stale_mesh_dispatch_reclaimed"
19716
+ };
19717
+ try {
19718
+ const isLocal = components.cliManager?.adapters?.has?.(sessionId) === true;
19719
+ if (isLocal) {
19720
+ if (!stopArgs.cliType) {
19721
+ const localType = components.cliManager?.adapters?.get?.(sessionId)?.cliType;
19722
+ if (localType) stopArgs.cliType = localType;
19723
+ }
19724
+ Promise.resolve(components.cliManager?.handleCliCommand?.("stop_cli", stopArgs)).catch((e) => LOG.warn("MeshQueue", `Local stop of stale worker ${sessionId} failed: ${e?.message || e}`));
19725
+ return;
19726
+ }
19727
+ let daemonId = args.daemonId;
19728
+ if (!daemonId && args.nodeId) {
19729
+ try {
19730
+ const mesh = getMeshWithCache(components, meshId);
19731
+ const node = mesh?.nodes?.find((n) => meshNodeIdMatches(n, args.nodeId));
19732
+ daemonId = node ? readMeshNodeDaemonId(node) || void 0 : void 0;
19733
+ } catch {
19734
+ }
19735
+ }
19736
+ if (daemonId && components.dispatchMeshCommand) {
19737
+ Promise.resolve(components.dispatchMeshCommand(daemonId, "stop_cli", stopArgs)).catch((e) => LOG.warn("MeshQueue", `Remote stop of stale worker ${sessionId} on daemon ${daemonId} failed: ${e?.message || e}`));
19738
+ } else {
19739
+ LOG.warn("MeshQueue", `Cannot stop stale worker ${sessionId}: no local adapter and no resolvable remote daemon id (node ${args.nodeId ?? "?"}). Ack already rejected \u2014 task will re-strand-and-fail if the worker completes.`);
19740
+ }
19741
+ } catch (e) {
19742
+ LOG.warn("MeshQueue", `stopStaleMeshWorker error for ${sessionId}: ${e?.message || e}`);
19743
+ }
19744
+ }
19701
19745
  function injectMeshSystemMessage(components, args) {
19702
19746
  const eventSessionId = resolveEventSessionId(args.metadataEvent, args.sourceInstanceId);
19703
19747
  const eventNodeId = readNonEmptyString2(args.nodeId) || readNonEmptyString2(args.metadataEvent.meshNodeId);
@@ -19937,6 +19981,35 @@ function injectMeshSystemMessage(components, args) {
19937
19981
  }
19938
19982
  if (sessionId) {
19939
19983
  const startedTaskId = readNonEmptyString2(args.metadataEvent.taskId) || void 0;
19984
+ const startedNonce = typeof args.metadataEvent.dispatchNonce === "number" ? args.metadataEvent.dispatchNonce : void 0;
19985
+ if (startedTaskId && startedNonce !== void 0) {
19986
+ const currentRow = (() => {
19987
+ try {
19988
+ return MeshRuntimeStore.getInstance().findQueueEntryById(args.meshId, startedTaskId);
19989
+ } catch {
19990
+ return null;
19991
+ }
19992
+ })();
19993
+ const currentNonce = typeof currentRow?.dispatchNonce === "number" ? currentRow.dispatchNonce : void 0;
19994
+ if (currentNonce !== void 0 && startedNonce < currentNonce) {
19995
+ LOG.warn("MeshQueue", `Rejecting stale mesh dispatch: task ${startedTaskId} generating_started from session ${sessionId} (node ${nodeId ?? "?"}) carries dispatchNonce ${startedNonce} < current ${currentNonce} \u2014 the task was reclaimed and re-dispatched; stopping this worker to prevent duplicate execution.`);
19996
+ traceMeshEventDrop("stale_dispatch_nonce_rejected", {
19997
+ taskId: startedTaskId,
19998
+ sessionId,
19999
+ nodeId,
20000
+ meshId: args.meshId,
20001
+ event: "agent:generating_started"
20002
+ }, `nonce ${startedNonce} < ${currentNonce}`);
20003
+ stopStaleMeshWorker(components, {
20004
+ meshId: args.meshId,
20005
+ sessionId,
20006
+ nodeId,
20007
+ providerType: readNonEmptyString2(args.metadataEvent.providerType) || readNonEmptyString2(args.metadataEvent.cliType),
20008
+ daemonId: readNonEmptyString2(args.metadataEvent.sourceDaemonId) || readNonEmptyString2(args.metadataEvent.daemonId)
20009
+ });
20010
+ return { success: true, forwarded: 0, suppressed: true, staleDispatchRejected: true };
20011
+ }
20012
+ }
19940
20013
  if (startedTaskId) {
19941
20014
  updateDirectDispatchStatus(args.meshId, sessionId, "acked", startedTaskId);
19942
20015
  } else if (sessionHasActiveAssignment(args.meshId, sessionId)) {
@@ -20473,6 +20546,7 @@ var init_mesh_event_forwarding = __esm({
20473
20546
  init_dist();
20474
20547
  init_mesh_events_stale();
20475
20548
  init_mesh_task_inflight();
20549
+ init_mesh_node_identity();
20476
20550
  init_mesh_events_utils();
20477
20551
  init_mesh_event_classify();
20478
20552
  init_mesh_queue_assignment();
@@ -45977,6 +46051,10 @@ var CliProviderInstance = class _CliProviderInstance {
45977
46051
  // shares this daemon. See isMeshOwnedDelegateSession's post-detach gate.
45978
46052
  ...assignment.nodeId ? { meshNodeId: assignment.nodeId, meshLastNodeId: assignment.nodeId } : {},
45979
46053
  ...assignment.taskId ? { meshActiveTaskId: assignment.taskId } : {},
46054
+ // REDRIVE-DUP: task-level dispatch nonce, echoed on generating_started so the
46055
+ // coordinator can reject a stale (reclaimed) dispatch. Cleared with meshActiveTaskId
46056
+ // on detach so a subsequent unrelated turn never re-echoes a prior task's nonce.
46057
+ ...typeof assignment.dispatchNonce === "number" ? { meshActiveDispatchNonce: assignment.dispatchNonce } : {},
45980
46058
  ...assignment.coordinatorDaemonId ? { meshCoordinatorDaemonId: assignment.coordinatorDaemonId } : {},
45981
46059
  // Session-level routing anchor: the originating coordinator session, so this
45982
46060
  // worker's completion events route back to the exact session that dispatched it.
@@ -46012,15 +46090,17 @@ var CliProviderInstance = class _CliProviderInstance {
46012
46090
  if (!this.settings.meshNodeFor && !this.settings.meshActiveTaskId && !this.settings.meshNodeId) return;
46013
46091
  if (this.settings.launchedByCoordinator === true) {
46014
46092
  if (!this.settings.meshActiveTaskId) return;
46015
- const { meshActiveTaskId: meshActiveTaskId2, ...rest2 } = this.settings;
46093
+ const { meshActiveTaskId: meshActiveTaskId2, meshActiveDispatchNonce: meshActiveDispatchNonce2, ...rest2 } = this.settings;
46016
46094
  void meshActiveTaskId2;
46095
+ void meshActiveDispatchNonce2;
46017
46096
  this.settings = rest2;
46018
46097
  this.adapter.updateRuntimeSettings?.(this.settings);
46019
46098
  return;
46020
46099
  }
46021
- const { meshNodeFor, meshNodeId, meshActiveTaskId, ...rest } = this.settings;
46100
+ const { meshNodeFor, meshNodeId, meshActiveTaskId, meshActiveDispatchNonce, ...rest } = this.settings;
46022
46101
  void meshNodeFor;
46023
46102
  void meshActiveTaskId;
46103
+ void meshActiveDispatchNonce;
46024
46104
  const lastNodeId = typeof meshNodeId === "string" && meshNodeId.trim() ? meshNodeId.trim() : typeof rest.meshLastNodeId === "string" && rest.meshLastNodeId.trim() ? rest.meshLastNodeId.trim() : void 0;
46025
46105
  this.settings = lastNodeId ? { ...rest, meshLastNodeId: lastNodeId } : rest;
46026
46106
  this.adapter.updateRuntimeSettings?.(this.settings);
@@ -47509,6 +47589,9 @@ var CliProviderInstance = class _CliProviderInstance {
47509
47589
  const resolved = this.completingTurnTaskId();
47510
47590
  if (resolved) enrichedEvent.taskId = resolved;
47511
47591
  }
47592
+ if (enrichedEvent.dispatchNonce === void 0 && typeof this.settings.meshActiveDispatchNonce === "number") {
47593
+ enrichedEvent.dispatchNonce = this.settings.meshActiveDispatchNonce;
47594
+ }
47512
47595
  }
47513
47596
  if (this.context?.emitProviderEvent) {
47514
47597
  this.context.emitProviderEvent(enrichedEvent);
@@ -50395,6 +50478,10 @@ Run 'adhdev doctor' for detailed diagnostics.`
50395
50478
  meshId: meshContext.meshId,
50396
50479
  ...typeof meshContext.nodeId === "string" && meshContext.nodeId ? { nodeId: meshContext.nodeId } : {},
50397
50480
  ...typeof meshContext.taskId === "string" && meshContext.taskId ? { taskId: meshContext.taskId } : {},
50481
+ // REDRIVE-DUP: carry the dispatch nonce onto the worker session so
50482
+ // its generating_started event echoes it back for the coordinator's
50483
+ // stale-nonce guard.
50484
+ ...typeof meshContext.dispatchNonce === "number" ? { dispatchNonce: meshContext.dispatchNonce } : {},
50398
50485
  ...typeof meshContext.coordinatorDaemonId === "string" && meshContext.coordinatorDaemonId ? { coordinatorDaemonId: meshContext.coordinatorDaemonId } : {}
50399
50486
  });
50400
50487
  } catch {
@@ -59329,7 +59416,50 @@ function buildRefineJobHandle(self, args) {
59329
59416
  }
59330
59417
  };
59331
59418
  }
59419
+ function slimRefineEventResult(result) {
59420
+ const slim = {};
59421
+ for (const key2 of [
59422
+ "success",
59423
+ "code",
59424
+ "error",
59425
+ "convergenceStatus",
59426
+ "blockedReason",
59427
+ "branch",
59428
+ "into",
59429
+ "terminalKind",
59430
+ "nextStep",
59431
+ "finalBranchConvergenceState"
59432
+ ]) {
59433
+ if (result[key2] !== void 0) slim[key2] = result[key2];
59434
+ }
59435
+ if (Array.isArray(result.unreachableSubmoduleCommits)) {
59436
+ slim.unreachableSubmoduleCommits = result.unreachableSubmoduleCommits.map((e) => ({ path: e?.path, autoPublishAllowed: e?.autoPublishAllowed }));
59437
+ }
59438
+ if (result.validationSummary && typeof result.validationSummary === "object") {
59439
+ const vs = result.validationSummary;
59440
+ slim.validationSummary = {
59441
+ status: vs.status,
59442
+ failureCode: vs.failureCode,
59443
+ configSource: vs.configSource,
59444
+ configSourceType: vs.configSourceType,
59445
+ commandsRunCount: Array.isArray(vs.commandsRun) ? vs.commandsRun.length : void 0
59446
+ };
59447
+ }
59448
+ if (result.patchEquivalence && typeof result.patchEquivalence === "object") {
59449
+ const pe = result.patchEquivalence;
59450
+ slim.patchEquivalence = { status: pe.status, equivalent: pe.equivalent };
59451
+ }
59452
+ if (result.submoduleReachability && typeof result.submoduleReachability === "object") {
59453
+ const sr = result.submoduleReachability;
59454
+ slim.submoduleReachability = {
59455
+ checked: Array.isArray(sr.entries) ? sr.entries.length : void 0,
59456
+ unreachable: Array.isArray(sr.unreachable) ? sr.unreachable.length : void 0
59457
+ };
59458
+ }
59459
+ return slim;
59460
+ }
59332
59461
  function queueRefineJobEvent(self, event, handle, result) {
59462
+ const slimResult = result ? slimRefineEventResult(result) : void 0;
59333
59463
  const metadataEvent = {
59334
59464
  source: "refine_mesh_node_async_job",
59335
59465
  jobId: handle.jobId,
@@ -59342,7 +59472,7 @@ function queueRefineJobEvent(self, event, handle, result) {
59342
59472
  startedAt: handle.startedAt,
59343
59473
  completedAt: handle.completedAt,
59344
59474
  retryOfJobId: handle.retryOfJobId,
59345
- ...result ? { result } : {}
59475
+ ...slimResult ? { result: slimResult } : {}
59346
59476
  };
59347
59477
  const eventPayload = {
59348
59478
  event,
@@ -59369,7 +59499,7 @@ function queueRefineJobEvent(self, event, handle, result) {
59369
59499
  startedAt: handle.startedAt,
59370
59500
  completedAt: handle.completedAt,
59371
59501
  retryOfJobId: handle.retryOfJobId,
59372
- ...result ? { result } : {}
59502
+ ...slimResult ? { result: slimResult } : {}
59373
59503
  }
59374
59504
  );
59375
59505
  if (forwarded?.success === true) return;