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

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 ? "b268e41ee8422d976c6774101d93809b131860e9" : void 0) ?? "unknown";
418
+ const commitShort = readInjected(true ? "b268e41e" : void 0) ?? (commit !== "unknown" ? commit.slice(0, 7) : "unknown");
419
+ const version = readInjected(true ? "0.9.82-rc.489" : void 0) ?? readInjected(typeof process !== "undefined" ? process.env?.ADHDEV_PKG_VERSION : void 0) ?? "unknown";
420
+ const builtAt = readInjected(true ? "2026-07-10T04:37:55.985Z" : 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();
@@ -45509,13 +45583,18 @@ var CliProviderInstance = class _CliProviderInstance {
45509
45583
  * INVARIANT (do not regress): must be STRICTLY GREATER than
45510
45584
  * AUTO_APPROVE_FLAP_CONTINUITY_MS + max_busy_phase + AUTO_APPROVE_SETTLE_MS so
45511
45585
  * that during a flap the settle clock (which FLAP_CONTINUITY keeps alive across
45512
- * each ~4.3–4.5s busy phase) gets to accrue its 600ms on the RETURNING approval
45513
- * frame before this stall bound can trip. Observed geometry: approval ~1.5s,
45514
- * busy ~4.3–4.5s. 9000ms CONTINUITY(6000) + busy(~4.5s) + SETTLE(600) headroom;
45586
+ * each busy phase) gets to accrue its 600ms on the RETURNING approval frame
45587
+ * before this stall bound can trip. Observed geometry — worker: approval ~1.5s,
45588
+ * busy ~4.3–4.5s; coordinator self-session: approval ~1.5s, busy ~2.85s. Both
45589
+ * now use the extended window (isAutonomousMeshSession covers worker +
45590
+ * meshCoordinatorFor). Worst case: CONTINUITY(6000) + busy(~4.5s) + SETTLE(600)
45591
+ * = ~11100ms, so the stall bound must exceed that. 10500ms satisfies the invariant
45592
+ * for coordinator (6000 + 2850 + 600 = 9450 < 10500) and was previously 9000ms
45593
+ * (which failed for a worker busy phase of 4.5s: 6000+4500+600=11100 > 9000).
45515
45594
  * the old 4500ms tripped inside the very first busy phase (while modal=none, so
45516
45595
  * the nudge was NOT deferred) and leaked to the coordinator.
45517
45596
  */
45518
- static AUTO_APPROVE_MASK_STALL_MS = 9e3;
45597
+ static AUTO_APPROVE_MASK_STALL_MS = 10500;
45519
45598
  adapter;
45520
45599
  context = null;
45521
45600
  events = [];
@@ -45977,6 +46056,10 @@ var CliProviderInstance = class _CliProviderInstance {
45977
46056
  // shares this daemon. See isMeshOwnedDelegateSession's post-detach gate.
45978
46057
  ...assignment.nodeId ? { meshNodeId: assignment.nodeId, meshLastNodeId: assignment.nodeId } : {},
45979
46058
  ...assignment.taskId ? { meshActiveTaskId: assignment.taskId } : {},
46059
+ // REDRIVE-DUP: task-level dispatch nonce, echoed on generating_started so the
46060
+ // coordinator can reject a stale (reclaimed) dispatch. Cleared with meshActiveTaskId
46061
+ // on detach so a subsequent unrelated turn never re-echoes a prior task's nonce.
46062
+ ...typeof assignment.dispatchNonce === "number" ? { meshActiveDispatchNonce: assignment.dispatchNonce } : {},
45980
46063
  ...assignment.coordinatorDaemonId ? { meshCoordinatorDaemonId: assignment.coordinatorDaemonId } : {},
45981
46064
  // Session-level routing anchor: the originating coordinator session, so this
45982
46065
  // worker's completion events route back to the exact session that dispatched it.
@@ -46012,15 +46095,17 @@ var CliProviderInstance = class _CliProviderInstance {
46012
46095
  if (!this.settings.meshNodeFor && !this.settings.meshActiveTaskId && !this.settings.meshNodeId) return;
46013
46096
  if (this.settings.launchedByCoordinator === true) {
46014
46097
  if (!this.settings.meshActiveTaskId) return;
46015
- const { meshActiveTaskId: meshActiveTaskId2, ...rest2 } = this.settings;
46098
+ const { meshActiveTaskId: meshActiveTaskId2, meshActiveDispatchNonce: meshActiveDispatchNonce2, ...rest2 } = this.settings;
46016
46099
  void meshActiveTaskId2;
46100
+ void meshActiveDispatchNonce2;
46017
46101
  this.settings = rest2;
46018
46102
  this.adapter.updateRuntimeSettings?.(this.settings);
46019
46103
  return;
46020
46104
  }
46021
- const { meshNodeFor, meshNodeId, meshActiveTaskId, ...rest } = this.settings;
46105
+ const { meshNodeFor, meshNodeId, meshActiveTaskId, meshActiveDispatchNonce, ...rest } = this.settings;
46022
46106
  void meshNodeFor;
46023
46107
  void meshActiveTaskId;
46108
+ void meshActiveDispatchNonce;
46024
46109
  const lastNodeId = typeof meshNodeId === "string" && meshNodeId.trim() ? meshNodeId.trim() : typeof rest.meshLastNodeId === "string" && rest.meshLastNodeId.trim() ? rest.meshLastNodeId.trim() : void 0;
46025
46110
  this.settings = lastNodeId ? { ...rest, meshLastNodeId: lastNodeId } : rest;
46026
46111
  this.adapter.updateRuntimeSettings?.(this.settings);
@@ -46670,7 +46755,7 @@ var CliProviderInstance = class _CliProviderInstance {
46670
46755
  * genuine resolution frees the gate promptly.
46671
46756
  */
46672
46757
  autoApproveContinuityWindowMs() {
46673
- return this.autoApproveMaskSince > 0 && this.isMeshWorkerSession() ? _CliProviderInstance.AUTO_APPROVE_FLAP_CONTINUITY_MS : _CliProviderInstance.AUTO_APPROVE_GATE_HYSTERESIS_MS;
46758
+ return this.autoApproveMaskSince > 0 && this.isAutonomousMeshSession() ? _CliProviderInstance.AUTO_APPROVE_FLAP_CONTINUITY_MS : _CliProviderInstance.AUTO_APPROVE_GATE_HYSTERESIS_MS;
46674
46759
  }
46675
46760
  /**
46676
46761
  * The settle-gate identity signature for a raw activeModal, or null when the
@@ -47509,6 +47594,9 @@ var CliProviderInstance = class _CliProviderInstance {
47509
47594
  const resolved = this.completingTurnTaskId();
47510
47595
  if (resolved) enrichedEvent.taskId = resolved;
47511
47596
  }
47597
+ if (enrichedEvent.dispatchNonce === void 0 && typeof this.settings.meshActiveDispatchNonce === "number") {
47598
+ enrichedEvent.dispatchNonce = this.settings.meshActiveDispatchNonce;
47599
+ }
47512
47600
  }
47513
47601
  if (this.context?.emitProviderEvent) {
47514
47602
  this.context.emitProviderEvent(enrichedEvent);
@@ -50395,6 +50483,10 @@ Run 'adhdev doctor' for detailed diagnostics.`
50395
50483
  meshId: meshContext.meshId,
50396
50484
  ...typeof meshContext.nodeId === "string" && meshContext.nodeId ? { nodeId: meshContext.nodeId } : {},
50397
50485
  ...typeof meshContext.taskId === "string" && meshContext.taskId ? { taskId: meshContext.taskId } : {},
50486
+ // REDRIVE-DUP: carry the dispatch nonce onto the worker session so
50487
+ // its generating_started event echoes it back for the coordinator's
50488
+ // stale-nonce guard.
50489
+ ...typeof meshContext.dispatchNonce === "number" ? { dispatchNonce: meshContext.dispatchNonce } : {},
50398
50490
  ...typeof meshContext.coordinatorDaemonId === "string" && meshContext.coordinatorDaemonId ? { coordinatorDaemonId: meshContext.coordinatorDaemonId } : {}
50399
50491
  });
50400
50492
  } catch {
@@ -59329,7 +59421,50 @@ function buildRefineJobHandle(self, args) {
59329
59421
  }
59330
59422
  };
59331
59423
  }
59424
+ function slimRefineEventResult(result) {
59425
+ const slim = {};
59426
+ for (const key2 of [
59427
+ "success",
59428
+ "code",
59429
+ "error",
59430
+ "convergenceStatus",
59431
+ "blockedReason",
59432
+ "branch",
59433
+ "into",
59434
+ "terminalKind",
59435
+ "nextStep",
59436
+ "finalBranchConvergenceState"
59437
+ ]) {
59438
+ if (result[key2] !== void 0) slim[key2] = result[key2];
59439
+ }
59440
+ if (Array.isArray(result.unreachableSubmoduleCommits)) {
59441
+ slim.unreachableSubmoduleCommits = result.unreachableSubmoduleCommits.map((e) => ({ path: e?.path, autoPublishAllowed: e?.autoPublishAllowed }));
59442
+ }
59443
+ if (result.validationSummary && typeof result.validationSummary === "object") {
59444
+ const vs = result.validationSummary;
59445
+ slim.validationSummary = {
59446
+ status: vs.status,
59447
+ failureCode: vs.failureCode,
59448
+ configSource: vs.configSource,
59449
+ configSourceType: vs.configSourceType,
59450
+ commandsRunCount: Array.isArray(vs.commandsRun) ? vs.commandsRun.length : void 0
59451
+ };
59452
+ }
59453
+ if (result.patchEquivalence && typeof result.patchEquivalence === "object") {
59454
+ const pe = result.patchEquivalence;
59455
+ slim.patchEquivalence = { status: pe.status, equivalent: pe.equivalent };
59456
+ }
59457
+ if (result.submoduleReachability && typeof result.submoduleReachability === "object") {
59458
+ const sr = result.submoduleReachability;
59459
+ slim.submoduleReachability = {
59460
+ checked: Array.isArray(sr.entries) ? sr.entries.length : void 0,
59461
+ unreachable: Array.isArray(sr.unreachable) ? sr.unreachable.length : void 0
59462
+ };
59463
+ }
59464
+ return slim;
59465
+ }
59332
59466
  function queueRefineJobEvent(self, event, handle, result) {
59467
+ const slimResult = result ? slimRefineEventResult(result) : void 0;
59333
59468
  const metadataEvent = {
59334
59469
  source: "refine_mesh_node_async_job",
59335
59470
  jobId: handle.jobId,
@@ -59342,7 +59477,7 @@ function queueRefineJobEvent(self, event, handle, result) {
59342
59477
  startedAt: handle.startedAt,
59343
59478
  completedAt: handle.completedAt,
59344
59479
  retryOfJobId: handle.retryOfJobId,
59345
- ...result ? { result } : {}
59480
+ ...slimResult ? { result: slimResult } : {}
59346
59481
  };
59347
59482
  const eventPayload = {
59348
59483
  event,
@@ -59369,7 +59504,7 @@ function queueRefineJobEvent(self, event, handle, result) {
59369
59504
  startedAt: handle.startedAt,
59370
59505
  completedAt: handle.completedAt,
59371
59506
  retryOfJobId: handle.retryOfJobId,
59372
- ...result ? { result } : {}
59507
+ ...slimResult ? { result: slimResult } : {}
59373
59508
  }
59374
59509
  );
59375
59510
  if (forwarded?.success === true) return;