@adhdev/daemon-core 0.9.82-rc.533 → 0.9.82-rc.534

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.js CHANGED
@@ -419,10 +419,10 @@ function readInjected(value) {
419
419
  }
420
420
  function getDaemonBuildInfo() {
421
421
  if (cached) return cached;
422
- const commit = readInjected(true ? "4bcba3bd84cc9f4cd08a3d93965f10f43fcf5dad" : void 0) ?? "unknown";
423
- const commitShort = readInjected(true ? "4bcba3bd" : void 0) ?? (commit !== "unknown" ? commit.slice(0, 7) : "unknown");
424
- const version = readInjected(true ? "0.9.82-rc.533" : void 0) ?? readInjected(typeof process !== "undefined" ? process.env?.ADHDEV_PKG_VERSION : void 0) ?? "unknown";
425
- const builtAt = readInjected(true ? "2026-07-15T07:45:39.050Z" : void 0);
422
+ const commit = readInjected(true ? "7b6411ebc267c71484a3f7d16c392b5bd3e71bd8" : void 0) ?? "unknown";
423
+ const commitShort = readInjected(true ? "7b6411eb" : void 0) ?? (commit !== "unknown" ? commit.slice(0, 7) : "unknown");
424
+ const version = readInjected(true ? "0.9.82-rc.534" : void 0) ?? readInjected(typeof process !== "undefined" ? process.env?.ADHDEV_PKG_VERSION : void 0) ?? "unknown";
425
+ const builtAt = readInjected(true ? "2026-07-15T08:30:18.262Z" : void 0);
426
426
  cached = builtAt ? { commit, commitShort, version, builtAt } : { commit, commitShort, version };
427
427
  return cached;
428
428
  }
@@ -5632,12 +5632,16 @@ function getActiveSessionDeliveries(meshId, sessionId) {
5632
5632
  return [];
5633
5633
  }
5634
5634
  }
5635
+ function consumeSessionDelivery(meshId, sessionId, status, taskId) {
5636
+ try {
5637
+ return MeshRuntimeStore.getInstance().consumeSessionDelivery(meshId, sessionId, status, taskId);
5638
+ } catch {
5639
+ return 0;
5640
+ }
5641
+ }
5635
5642
  function markSessionDeliveriesTerminal(meshId, sessionId, terminalStatus) {
5636
5643
  try {
5637
- const active = MeshRuntimeStore.getInstance().getActiveSessionDeliveries(meshId, sessionId);
5638
- for (const delivery of active) {
5639
- MeshRuntimeStore.getInstance().updateSessionDeliveryStatus(delivery.id, terminalStatus);
5640
- }
5644
+ MeshRuntimeStore.getInstance().markOpenSessionDeliveriesTerminal(meshId, sessionId, terminalStatus);
5641
5645
  } catch {
5642
5646
  }
5643
5647
  }
@@ -7685,6 +7689,25 @@ var init_mesh_runtime_store = __esm({
7685
7689
  });
7686
7690
  this.maybeCheckpointWal();
7687
7691
  }
7692
+ // DELIVERED-NOT-CONSUMED-REDRIVE monotonic FSM: the forward-progress lifecycle of a
7693
+ // delivery is a strictly increasing rank — a status may only advance, never regress.
7694
+ // The redrive bug was a NON-monotonic FSM: the transport-confirm callback
7695
+ // (mesh-queue-assignment :384) writes 'delivered' unconditionally by PK, so when the
7696
+ // worker's agent:generating_started raced AHEAD of the confirm and already flipped the
7697
+ // row 'delivering'→'acked', the late confirm CLOBBERED 'acked' back to 'delivered'.
7698
+ // taskDeliveryConsumed() (which keys on 'acked'/'completed') then read false forever,
7699
+ // and the short-grace re-drive re-opened an already-consumed task. Enforcing the rank
7700
+ // ordering here makes the two event orders converge on the same monotone terminal state
7701
+ // regardless of arrival order, so a late confirm can never demote a consumed delivery.
7702
+ // 'failed'/'expired'/'cancelled' are absorbing OUTCOMES, not progress ranks — they are
7703
+ // always allowed (a genuine dispatch failure must be recordable even from 'acked').
7704
+ static DELIVERY_PROGRESS_RANK = {
7705
+ queued: 0,
7706
+ delivering: 1,
7707
+ delivered: 2,
7708
+ acked: 3,
7709
+ completed: 4
7710
+ };
7688
7711
  updateSessionDeliveryStatus(id, status, opts) {
7689
7712
  const now = (/* @__PURE__ */ new Date()).toISOString();
7690
7713
  if (opts?.incrementAttempt) {
@@ -7693,13 +7716,75 @@ var init_mesh_runtime_store = __esm({
7693
7716
  SET status = @status, last_error = @lastError, attempt_count = attempt_count + 1, updated_at = @updatedAt
7694
7717
  WHERE id = @id
7695
7718
  `).run({ id, status, lastError: opts?.lastError ?? null, updatedAt: now });
7696
- } else {
7719
+ return;
7720
+ }
7721
+ const targetRank = _MeshRuntimeStore.DELIVERY_PROGRESS_RANK[status];
7722
+ if (targetRank === void 0) {
7697
7723
  this.db.prepare(`
7698
7724
  UPDATE mesh_session_delivery
7699
7725
  SET status = @status, last_error = @lastError, updated_at = @updatedAt
7700
7726
  WHERE id = @id
7701
7727
  `).run({ id, status, lastError: opts?.lastError ?? null, updatedAt: now });
7728
+ return;
7729
+ }
7730
+ this.db.prepare(`
7731
+ UPDATE mesh_session_delivery
7732
+ SET status = @status, last_error = @lastError, updated_at = @updatedAt
7733
+ WHERE id = @id AND (@targetRank >= CASE status
7734
+ WHEN 'queued' THEN 0 WHEN 'delivering' THEN 1 WHEN 'delivered' THEN 2
7735
+ WHEN 'acked' THEN 3 WHEN 'completed' THEN 4 ELSE 99 END)
7736
+ `).run({ id, status, lastError: opts?.lastError ?? null, updatedAt: now, targetRank });
7737
+ }
7738
+ /**
7739
+ * DELIVERED-NOT-CONSUMED-REDRIVE consume path. Advance a task's delivery record(s) to a
7740
+ * CONSUMED status ('acked' or 'completed'), matching on mesh + session (+ taskId when the
7741
+ * event names one) and INCLUDING rows already in 'delivered'/'acked'/'delivering'.
7742
+ *
7743
+ * The ack/terminal callers previously routed through getActiveSessionDeliveries(), whose SQL
7744
+ * EXCLUDES 'delivered' — so in the normal event order (transport confirm flips 'delivered'
7745
+ * BEFORE the worker's generating_started fires) the ack matched zero rows and the delivery
7746
+ * was stranded 'delivered', never 'acked'. This finds the row by (mesh, session[, task])
7747
+ * directly and relies on updateSessionDeliveryStatus's monotonic guard to only advance it.
7748
+ * Returns the number of rows advanced.
7749
+ */
7750
+ consumeSessionDelivery(meshId, sessionId, status, taskId) {
7751
+ const rows = this.db.prepare(
7752
+ taskId ? `SELECT id, session_id FROM mesh_session_delivery
7753
+ WHERE mesh_id = ? AND task_id = ?
7754
+ AND status IN ('queued','delivering','delivered','acked')` : `SELECT id, session_id FROM mesh_session_delivery
7755
+ WHERE mesh_id = ? AND session_id = ?
7756
+ AND status IN ('queued','delivering','delivered','acked')`
7757
+ ).all(meshId, taskId ?? sessionId);
7758
+ let advanced = 0;
7759
+ for (const r of rows) {
7760
+ if (!sessionIdsEquivalent(r.session_id ?? void 0, sessionId)) continue;
7761
+ this.updateSessionDeliveryStatus(r.id, status);
7762
+ advanced++;
7763
+ }
7764
+ return advanced;
7765
+ }
7766
+ /**
7767
+ * DELIVERED-NOT-CONSUMED-REDRIVE terminal path. Mark every OPEN delivery for a session
7768
+ * (queued/delivering/delivered/acked) terminal on task completion/failure. The prior
7769
+ * markSessionDeliveriesTerminal() routed through getActiveSessionDeliveries(), whose SQL
7770
+ * EXCLUDES 'delivered'/'completed' — so a 'delivered' row (the common case, since the
7771
+ * transport confirm flips it before the completion event) was never marked terminal and
7772
+ * stayed 'delivered', keeping taskDeliveryConsumed() false and feeding the false re-drive.
7773
+ * We match rows in OPEN states directly here. 'completed' advances monotonically (it is the
7774
+ * top progress rank); 'failed' is an absorbing outcome written unconditionally.
7775
+ */
7776
+ markOpenSessionDeliveriesTerminal(meshId, sessionId, terminalStatus) {
7777
+ const rows = this.db.prepare(
7778
+ `SELECT id, session_id FROM mesh_session_delivery
7779
+ WHERE mesh_id = ? AND status IN ('queued','delivering','delivered','acked')`
7780
+ ).all(meshId);
7781
+ let marked = 0;
7782
+ for (const r of rows) {
7783
+ if (!sessionIdsEquivalent(r.session_id ?? void 0, sessionId)) continue;
7784
+ this.updateSessionDeliveryStatus(r.id, terminalStatus);
7785
+ marked++;
7702
7786
  }
7787
+ return marked;
7703
7788
  }
7704
7789
  getActiveSessionDeliveries(meshId, sessionId) {
7705
7790
  const now = (/* @__PURE__ */ new Date()).toISOString();
@@ -20627,17 +20712,7 @@ function injectMeshSystemMessage(components, args) {
20627
20712
  updateDirectDispatchStatus(args.meshId, sessionId, "acked", soleTaskId);
20628
20713
  }
20629
20714
  }
20630
- const activeDeliveries = (() => {
20631
- try {
20632
- return MeshRuntimeStore.getInstance().getActiveSessionDeliveries(args.meshId, sessionId);
20633
- } catch {
20634
- return [];
20635
- }
20636
- })();
20637
- const deliveriesToAck = startedTaskId ? activeDeliveries.filter((d) => d.taskId === startedTaskId) : activeDeliveries;
20638
- for (const d of deliveriesToAck) {
20639
- updateSessionDeliveryStatus(d.id, "acked");
20640
- }
20715
+ consumeSessionDelivery(args.meshId, sessionId, "acked", startedTaskId);
20641
20716
  }
20642
20717
  } else if (args.event === "agent:stopped") {
20643
20718
  const sessionId = resolveEventSessionId(args.metadataEvent, args.sourceInstanceId);
@@ -22077,6 +22152,9 @@ function recoverStrandedAssignedDispatches(components, meshId, store) {
22077
22152
  for (const key2 of [...deliveredNoTurnUnknownStreak.keys()]) {
22078
22153
  if (key2.startsWith(meshKeyPrefix) && !assignedKeys.has(key2)) deliveredNoTurnUnknownStreak.delete(key2);
22079
22154
  }
22155
+ for (const key2 of [...deliveredUnconsumedUnknownStreak.keys()]) {
22156
+ if (key2.startsWith(meshKeyPrefix) && !assignedKeys.has(key2)) deliveredUnconsumedUnknownStreak.delete(key2);
22157
+ }
22080
22158
  for (const row of assigned) {
22081
22159
  const dispatchedAtMs = Date.parse(row.dispatchTimestamp ?? "");
22082
22160
  if (!Number.isFinite(dispatchedAtMs)) continue;
@@ -22088,13 +22166,33 @@ function recoverStrandedAssignedDispatches(components, meshId, store) {
22088
22166
  updateTaskStatus(meshId, row.id, status);
22089
22167
  continue;
22090
22168
  }
22169
+ const shortStreakKey = `${meshId}::${row.id}`;
22091
22170
  const verdict = row.assignedSessionId ? resolveSessionBusyVerdict(components, row.assignedSessionId) : "IDLE_CONFIRMED";
22092
- if (verdict !== "GENERATING") {
22171
+ if (verdict === "GENERATING") {
22172
+ deliveredUnconsumedUnknownStreak.delete(shortStreakKey);
22173
+ } else {
22174
+ if (verdict === "IDLE_CONFIRMED") {
22175
+ deliveredUnconsumedUnknownStreak.delete(shortStreakKey);
22176
+ } else {
22177
+ const streak = (deliveredUnconsumedUnknownStreak.get(shortStreakKey) ?? 0) + 1;
22178
+ deliveredUnconsumedUnknownStreak.set(shortStreakKey, streak);
22179
+ if (streak < RECLAIM_UNKNOWN_GRACE_TICKS) {
22180
+ traceMeshEventDrop("short_redrive_deferred_unknown_verdict", {
22181
+ taskId: row.id,
22182
+ sessionId: row.assignedSessionId,
22183
+ nodeId: row.assignedNodeId,
22184
+ meshId,
22185
+ event: "agent:generating_started"
22186
+ }, `unknown ${streak}/${RECLAIM_UNKNOWN_GRACE_TICKS}`);
22187
+ continue;
22188
+ }
22189
+ }
22093
22190
  const redriven = reclaimStrandedAssignedTask(meshId, row.id, {
22094
22191
  reason: "delivered_not_consumed_redrive",
22095
22192
  ageMs
22096
22193
  });
22097
22194
  if (redriven) {
22195
+ deliveredUnconsumedUnknownStreak.delete(shortStreakKey);
22098
22196
  LOG.warn("MeshReconcile", `Re-drove delivered-but-unconsumed task ${row.id} on mesh ${meshId} (node=${row.assignedNodeId ?? "?"} session=${row.assignedSessionId ?? "?"}, delivered but no generating_started in ${Math.round(ageMs / 1e3)}s, verdict ${verdict} \u2192 ${redriven.status})`);
22099
22197
  traceMeshEventDrop("assigned_delivered_not_consumed_redrive", {
22100
22198
  taskId: row.id,
@@ -22649,7 +22747,7 @@ function setupMeshReconcileLoop(components) {
22649
22747
  }
22650
22748
  };
22651
22749
  }
22652
- var coordinatorModalParkState, heldEventLedgerRecorded, ASSIGNED_STRANDED_DEADLINE_MS, DELIVERED_NO_TURN_DEADLINE_MS, ASSIGNED_DELIVERED_UNCONSUMED_REDRIVE_MS, RECLAIM_UNKNOWN_GRACE_TICKS, deliveredNoTurnUnknownStreak, ZOMBIE_ASSIGNED_MIN_AGE_MS, STRICT_SESSION_MATCH_TTL_MS, unresolvedForwardRejectionCounts, MAX_FORWARD_REJECTIONS, UNRESOLVED_FORWARD_NUDGE_DELAY_MS, unresolvedForwardNudgeTimer, unresolvedForwardNudgeRunning;
22750
+ var coordinatorModalParkState, heldEventLedgerRecorded, ASSIGNED_STRANDED_DEADLINE_MS, DELIVERED_NO_TURN_DEADLINE_MS, ASSIGNED_DELIVERED_UNCONSUMED_REDRIVE_MS, RECLAIM_UNKNOWN_GRACE_TICKS, deliveredNoTurnUnknownStreak, deliveredUnconsumedUnknownStreak, ZOMBIE_ASSIGNED_MIN_AGE_MS, STRICT_SESSION_MATCH_TTL_MS, unresolvedForwardRejectionCounts, MAX_FORWARD_REJECTIONS, UNRESOLVED_FORWARD_NUDGE_DELAY_MS, unresolvedForwardNudgeTimer, unresolvedForwardNudgeRunning;
22653
22751
  var init_mesh_reconcile_loop = __esm({
22654
22752
  "src/mesh/mesh-reconcile-loop.ts"() {
22655
22753
  "use strict";
@@ -22683,6 +22781,7 @@ var init_mesh_reconcile_loop = __esm({
22683
22781
  ASSIGNED_DELIVERED_UNCONSUMED_REDRIVE_MS = 25e3;
22684
22782
  RECLAIM_UNKNOWN_GRACE_TICKS = 3;
22685
22783
  deliveredNoTurnUnknownStreak = /* @__PURE__ */ new Map();
22784
+ deliveredUnconsumedUnknownStreak = /* @__PURE__ */ new Map();
22686
22785
  ZOMBIE_ASSIGNED_MIN_AGE_MS = 30 * 60 * 1e3;
22687
22786
  STRICT_SESSION_MATCH_TTL_MS = 6e4;
22688
22787
  unresolvedForwardRejectionCounts = /* @__PURE__ */ new Map();