@adhdev/daemon-standalone 1.0.58-rc.46 → 1.0.58-rc.47

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
@@ -36911,10 +36911,10 @@ var require_dist3 = __commonJS({
36911
36911
  }
36912
36912
  function getDaemonBuildInfo() {
36913
36913
  if (cached2) return cached2;
36914
- const commit = readInjected(true ? "a03baab8130d59d9dfaca9173cddefd7f052d6a4" : void 0) ?? "unknown";
36915
- const commitShort = readInjected(true ? "a03baab8" : void 0) ?? (commit !== "unknown" ? commit.slice(0, 7) : "unknown");
36916
- const version2 = readInjected(true ? "1.0.58-rc.46" : void 0) ?? readInjected(typeof process !== "undefined" ? process.env?.ADHDEV_PKG_VERSION : void 0) ?? "unknown";
36917
- const builtAt = readInjected(true ? "2026-08-30T06:41:16.415Z" : void 0);
36914
+ const commit = readInjected(true ? "f13d08263df6a24da9d6766eebfbfc7cac237235" : void 0) ?? "unknown";
36915
+ const commitShort = readInjected(true ? "f13d0826" : void 0) ?? (commit !== "unknown" ? commit.slice(0, 7) : "unknown");
36916
+ const version2 = readInjected(true ? "1.0.58-rc.47" : void 0) ?? readInjected(typeof process !== "undefined" ? process.env?.ADHDEV_PKG_VERSION : void 0) ?? "unknown";
36917
+ const builtAt = readInjected(true ? "2026-08-30T09:04:38.562Z" : void 0);
36918
36918
  cached2 = builtAt ? { commit, commitShort, version: version2, builtAt } : { commit, commitShort, version: version2 };
36919
36919
  return cached2;
36920
36920
  }
@@ -74031,6 +74031,96 @@ ${rendered}`, "utf-8");
74031
74031
  return { status: "failed", attemptId: presentation.attemptId, stage };
74032
74032
  }
74033
74033
  }
74034
+ function pushTerminalBucket(buckets, key2, terminal) {
74035
+ if (!key2) return;
74036
+ let bucket2 = buckets.get(key2);
74037
+ if (!bucket2) {
74038
+ bucket2 = { real: [], approval: [] };
74039
+ buckets.set(key2, bucket2);
74040
+ }
74041
+ (terminal.entry.kind === "task_approval_needed" ? bucket2.approval : bucket2.real).push(terminal);
74042
+ }
74043
+ function pushTerminalQuery(queries, key2, query) {
74044
+ if (!key2) return;
74045
+ const existing = queries.get(key2);
74046
+ if (existing) existing.push(query);
74047
+ else queries.set(key2, [query]);
74048
+ }
74049
+ function earlierTerminal(a, b) {
74050
+ if (!a) return b;
74051
+ if (!b) return a;
74052
+ return a.order <= b.order ? a : b;
74053
+ }
74054
+ function buildMeshActiveWorkLedgerSnapshot(ledgerEntries, options = {}) {
74055
+ const timestampByEntry = /* @__PURE__ */ new Map();
74056
+ for (const entry of ledgerEntries) timestampByEntry.set(entry, new Date(entry.timestamp).getTime());
74057
+ const entries = ledgerEntries.slice().sort((a, b) => timestampByEntry.get(a) - timestampByEntry.get(b));
74058
+ const taskBuckets = /* @__PURE__ */ new Map();
74059
+ const sessionBuckets = /* @__PURE__ */ new Map();
74060
+ const nodeBuckets = /* @__PURE__ */ new Map();
74061
+ let order = 0;
74062
+ for (const entry of entries) {
74063
+ if (!TERMINAL_LEDGER_KINDS.has(entry.kind) && entry.kind !== "task_approval_needed") continue;
74064
+ const timestampMs = timestampByEntry.get(entry);
74065
+ if (!Number.isFinite(timestampMs)) continue;
74066
+ const indexed = { entry, timestampMs, order: order++ };
74067
+ const taskId = readString7(entry.payload?.taskId);
74068
+ if (taskId) {
74069
+ pushTerminalBucket(taskBuckets, taskId, indexed);
74070
+ } else {
74071
+ pushTerminalBucket(sessionBuckets, readString7(entry.sessionId), indexed);
74072
+ pushTerminalBucket(nodeBuckets, machineCoreFromDaemonId(entry.nodeId), indexed);
74073
+ }
74074
+ }
74075
+ return {
74076
+ entries,
74077
+ matchDirectDispatchTerminals(dispatches) {
74078
+ const sortedDispatches = dispatches.slice().sort((a, b) => timestampByEntry.get(a) - timestampByEntry.get(b));
74079
+ const taskQueries = /* @__PURE__ */ new Map();
74080
+ const sessionQueries = /* @__PURE__ */ new Map();
74081
+ const nodeQueries = /* @__PURE__ */ new Map();
74082
+ for (const dispatch of sortedDispatches) {
74083
+ const timestampMs = timestampByEntry.get(dispatch) ?? new Date(dispatch.timestamp).getTime();
74084
+ if (!Number.isFinite(timestampMs)) continue;
74085
+ const query = { dispatch, timestampMs };
74086
+ pushTerminalQuery(taskQueries, directDispatchTaskId(dispatch), query);
74087
+ const sessionId = readString7(dispatch.sessionId);
74088
+ if (dispatch.sessionId) pushTerminalQuery(sessionQueries, sessionId, query);
74089
+ else pushTerminalQuery(nodeQueries, machineCoreFromDaemonId(dispatch.nodeId), query);
74090
+ }
74091
+ const realByDispatch = /* @__PURE__ */ new Map();
74092
+ const approvalByDispatch = /* @__PURE__ */ new Map();
74093
+ const resolve32 = (queries, buckets, bucketKind, output) => {
74094
+ for (const [key2, bucketQueries] of queries) {
74095
+ const terminals = buckets.get(key2)?.[bucketKind];
74096
+ if (!terminals?.length) continue;
74097
+ let cursor = 0;
74098
+ for (const query of bucketQueries) {
74099
+ while (cursor < terminals.length) {
74100
+ options.onTerminalProbe?.();
74101
+ if (terminals[cursor].timestampMs >= query.timestampMs) break;
74102
+ cursor += 1;
74103
+ }
74104
+ const candidate = terminals[cursor];
74105
+ if (candidate) output.set(query.dispatch, earlierTerminal(output.get(query.dispatch), candidate));
74106
+ }
74107
+ }
74108
+ };
74109
+ resolve32(taskQueries, taskBuckets, "real", realByDispatch);
74110
+ resolve32(sessionQueries, sessionBuckets, "real", realByDispatch);
74111
+ resolve32(nodeQueries, nodeBuckets, "real", realByDispatch);
74112
+ resolve32(taskQueries, taskBuckets, "approval", approvalByDispatch);
74113
+ resolve32(sessionQueries, sessionBuckets, "approval", approvalByDispatch);
74114
+ resolve32(nodeQueries, nodeBuckets, "approval", approvalByDispatch);
74115
+ const result = /* @__PURE__ */ new Map();
74116
+ for (const dispatch of sortedDispatches) {
74117
+ const selected = realByDispatch.get(dispatch) || approvalByDispatch.get(dispatch);
74118
+ if (selected) result.set(dispatch, selected.entry);
74119
+ }
74120
+ return result;
74121
+ }
74122
+ };
74123
+ }
74034
74124
  function readString7(value) {
74035
74125
  return typeof value === "string" && value.trim() ? value.trim() : void 0;
74036
74126
  }
@@ -74102,13 +74192,6 @@ ${rendered}`, "utf-8");
74102
74192
  function directDispatchTaskId(entry) {
74103
74193
  return readString7(entry.payload?.taskId) || entry.id;
74104
74194
  }
74105
- function terminalMatchesDispatch(terminal, dispatch, taskId) {
74106
- const terminalTaskId = readString7(terminal.payload?.taskId);
74107
- if (terminalTaskId && terminalTaskId === taskId) return true;
74108
- if (terminalTaskId && terminalTaskId !== taskId) return false;
74109
- if (dispatch.sessionId && sessionIdsEquivalent(terminal.sessionId, dispatch.sessionId)) return true;
74110
- return Boolean(dispatch.nodeId && daemonIdsEquivalent(terminal.nodeId, dispatch.nodeId) && !dispatch.sessionId);
74111
- }
74112
74195
  function statusFromTerminal(entry) {
74113
74196
  if (entry.kind === "task_approval_needed") return "awaiting_approval";
74114
74197
  if (entry.kind === "task_question_pending") return "awaiting_choice";
@@ -74125,8 +74208,7 @@ ${rendered}`, "utf-8");
74125
74208
  }
74126
74209
  function buildLedgerDirectDispatchRecord(dispatch, ctx) {
74127
74210
  const taskId = directDispatchTaskId(dispatch);
74128
- const matching = ctx.terminals.filter((entry) => new Date(entry.timestamp).getTime() >= new Date(dispatch.timestamp).getTime()).filter((entry) => terminalMatchesDispatch(entry, dispatch, taskId));
74129
- const terminal = matching.find((entry) => entry.kind !== "task_approval_needed") || matching[0];
74211
+ const terminal = ctx.terminal;
74130
74212
  const terminalStatus = terminal ? statusFromTerminal(terminal) : void 0;
74131
74213
  const live = sessionStatusFromNodes(ctx.nodes, dispatch.nodeId, dispatch.sessionId);
74132
74214
  const status = terminalStatus || live.status || "assigned";
@@ -74227,6 +74309,8 @@ ${rendered}`, "utf-8");
74227
74309
  }
74228
74310
  function buildMeshActiveWork(opts) {
74229
74311
  const now = opts.now ?? Date.now();
74312
+ const ledgerSnapshot = opts.ledgerSnapshot ?? buildMeshActiveWorkLedgerSnapshot(opts.ledgerEntries || []);
74313
+ const ledgerEntries = ledgerSnapshot.entries;
74230
74314
  const records = [];
74231
74315
  const staleDirectWork = [];
74232
74316
  const terminalDirectWork = [];
@@ -74237,7 +74321,7 @@ ${rendered}`, "utf-8");
74237
74321
  const queueNodeId = task.assignedNodeId || task.targetNodeId;
74238
74322
  const queueSessionId = task.assignedSessionId || task.targetSessionId;
74239
74323
  const queueLive = task.status === "assigned" ? sessionStatusFromNodes(opts.nodes, queueNodeId ?? void 0, queueSessionId ?? void 0) : {};
74240
- const queueTerminalAuthority = task.status === "assigned" && hasTerminalLedgerAuthorityForTask(opts.ledgerEntries, task.id);
74324
+ const queueTerminalAuthority = task.status === "assigned" && hasTerminalLedgerAuthorityForTask(ledgerEntries, task.id);
74241
74325
  let queueStatus = !queueTerminalAuthority && (queueLive.status === "awaiting_approval" || queueLive.status === "awaiting_choice" || queueLive.status === "generating") ? queueLive.status : task.status;
74242
74326
  const turnOverlay = task.status === "assigned" ? turnProjectionActiveWorkStatus(opts.meshId, task.id) : null;
74243
74327
  if (turnOverlay) queueStatus = turnOverlay.status;
@@ -74267,7 +74351,7 @@ ${rendered}`, "utf-8");
74267
74351
  const dbStatus = dispatch.status;
74268
74352
  const isTerminal = dbStatus === "completed" || dbStatus === "failed" || dbStatus === "stale";
74269
74353
  const turnOverlay = isTerminal ? null : turnProjectionActiveWorkStatus(opts.meshId, dispatch.taskId);
74270
- const directTerminalAuthority = !isTerminal && hasTerminalLedgerAuthorityForTask(opts.ledgerEntries, dispatch.taskId);
74354
+ const directTerminalAuthority = !isTerminal && hasTerminalLedgerAuthorityForTask(ledgerEntries, dispatch.taskId);
74271
74355
  const liveStatus = directTerminalAuthority ? void 0 : live.status;
74272
74356
  const status = isTerminal ? dbStatus === "completed" ? "idle" : "failed" : turnOverlay?.status || liveStatus || (dbStatus === "acked" ? "generating" : "assigned");
74273
74357
  const { ledgerOnlyStaleReason, isFreshUnacknowledged } = classifyDirectDispatch({
@@ -74311,12 +74395,12 @@ ${rendered}`, "utf-8");
74311
74395
  }
74312
74396
  records.push(record2);
74313
74397
  }
74314
- const ledgerEntries = (opts.ledgerEntries || []).slice().sort((a, b) => new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime());
74315
- const terminals = ledgerEntries.filter((entry) => TERMINAL_LEDGER_KINDS.has(entry.kind) || entry.kind === "task_approval_needed");
74316
- for (const dispatch of ledgerEntries.filter(isDirectDispatch)) {
74398
+ const ledgerDispatches = ledgerEntries.filter(isDirectDispatch);
74399
+ const terminalByDispatch = ledgerSnapshot.matchDirectDispatchTerminals(ledgerDispatches);
74400
+ for (const dispatch of ledgerDispatches) {
74317
74401
  if (dbTaskIds.has(directDispatchTaskId(dispatch))) continue;
74318
74402
  if (queueTaskIds.has(directDispatchTaskId(dispatch))) continue;
74319
- const { record: record2, terminalRow } = buildLedgerDirectDispatchRecord(dispatch, { terminals, nodes: opts.nodes, now });
74403
+ const { record: record2, terminalRow } = buildLedgerDirectDispatchRecord(dispatch, { terminal: terminalByDispatch.get(dispatch), nodes: opts.nodes, now });
74320
74404
  if (terminalRow) {
74321
74405
  terminalDirectWork.push(record2);
74322
74406
  if (opts.includeTerminalDirect !== true) continue;
@@ -74328,11 +74412,11 @@ ${rendered}`, "utf-8");
74328
74412
  records.push(record2);
74329
74413
  }
74330
74414
  } else {
74331
- const ledgerEntries = (opts.ledgerEntries || []).slice().sort((a, b) => new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime());
74332
- const terminals = ledgerEntries.filter((entry) => TERMINAL_LEDGER_KINDS.has(entry.kind) || entry.kind === "task_approval_needed");
74333
- for (const dispatch of ledgerEntries.filter(isDirectDispatch)) {
74415
+ const ledgerDispatches = ledgerEntries.filter(isDirectDispatch);
74416
+ const terminalByDispatch = ledgerSnapshot.matchDirectDispatchTerminals(ledgerDispatches);
74417
+ for (const dispatch of ledgerDispatches) {
74334
74418
  if (queueTaskIds.has(directDispatchTaskId(dispatch))) continue;
74335
- const { record: record2, terminalRow } = buildLedgerDirectDispatchRecord(dispatch, { terminals, nodes: opts.nodes, now });
74419
+ const { record: record2, terminalRow } = buildLedgerDirectDispatchRecord(dispatch, { terminal: terminalByDispatch.get(dispatch), nodes: opts.nodes, now });
74336
74420
  if (terminalRow) {
74337
74421
  terminalDirectWork.push(record2);
74338
74422
  if (opts.includeTerminalDirect !== true) continue;
@@ -74374,6 +74458,7 @@ ${rendered}`, "utf-8");
74374
74458
  meshId: opts.meshId,
74375
74459
  queue: opts.queue,
74376
74460
  ledgerEntries: opts.ledgerEntries,
74461
+ ledgerSnapshot: opts.ledgerSnapshot,
74377
74462
  directDispatches: opts.directDispatches,
74378
74463
  nodes: opts.nodes,
74379
74464
  now,
@@ -74526,13 +74611,13 @@ ${rendered}`, "utf-8");
74526
74611
  ${lines.join("\n")}
74527
74612
  The mesh has no work in flight. For each mission, decide its outcome: continue it (enqueue/dispatch the remaining work) or close it with mesh_mission_upsert(mission_id, status: "completed" | "abandoned"). Do not leave a finished mission in 'active'. This is a one-time reminder.`;
74528
74613
  }
74529
- function maybeInjectIdleActiveMissionReminder(meshId, coordinator, policy, now = Date.now(), instanceManager) {
74614
+ function maybeInjectIdleActiveMissionReminder(meshId, coordinator, policy, now = Date.now(), instanceManager, sharedLedgerSnapshot) {
74530
74615
  try {
74531
74616
  if (!coordinator) return false;
74532
74617
  if (policy?.idleActiveMissionReminder === false) return false;
74533
74618
  const activeMissions = getMeshMissions(meshId, ["active"]);
74534
74619
  if (activeMissions.length === 0) return false;
74535
- const ledgerEntries = readLedgerEntriesByKind(meshId, [
74620
+ const ledgerEntries = sharedLedgerSnapshot?.entries ?? readLedgerEntriesByKind(meshId, [
74536
74621
  "task_dispatched",
74537
74622
  "task_completed",
74538
74623
  "task_failed",
@@ -74545,6 +74630,7 @@ The mesh has no work in flight. For each mission, decide its outcome: continue i
74545
74630
  queue: getQueue(meshId),
74546
74631
  directDispatches: getActiveDirectDispatches(meshId),
74547
74632
  ledgerEntries,
74633
+ ledgerSnapshot: sharedLedgerSnapshot,
74548
74634
  now
74549
74635
  }).summary;
74550
74636
  if (summary.totalActiveCount !== 0 || summary.generatingCount !== 0) return false;
@@ -87051,6 +87137,7 @@ ${cleanBody}`;
87051
87137
  }
87052
87138
  var DEFAULT_RECONCILE_INTERVAL_MS;
87053
87139
  var DEFAULT_AUTO_PRUNE_MIN_AGE_MS;
87140
+ var DEFAULT_AUTO_PRUNE_INTERVAL_MS;
87054
87141
  var DEFAULT_PENDING_HELD_DRAIN_ESCALATE_MS;
87055
87142
  var init_mesh_reconcile_config = __esm2({
87056
87143
  "src/mesh/mesh-reconcile-config.ts"() {
@@ -87059,6 +87146,7 @@ ${cleanBody}`;
87059
87146
  init_mesh_reconcile_acked_hold();
87060
87147
  DEFAULT_RECONCILE_INTERVAL_MS = 4e3;
87061
87148
  DEFAULT_AUTO_PRUNE_MIN_AGE_MS = 24 * 60 * 6e4;
87149
+ DEFAULT_AUTO_PRUNE_INTERVAL_MS = 6e4;
87062
87150
  DEFAULT_PENDING_HELD_DRAIN_ESCALATE_MS = 12e3;
87063
87151
  }
87064
87152
  });
@@ -89784,10 +89872,18 @@ ${cleanBody}`;
89784
89872
  }
89785
89873
  }
89786
89874
  }
89787
- async function autoPruneStaleDirectDispatches(components, mesh, selfIds, localDaemonId, minAgeMs) {
89875
+ async function autoPruneStaleDirectDispatches(components, mesh, selfIds, localDaemonId, minAgeMs, sharedLedgerSnapshot) {
89788
89876
  const directDispatches = getActiveDirectDispatches(mesh.id);
89789
- if (directDispatches.length === 0) return;
89877
+ if (directDispatches.length === 0) return void 0;
89790
89878
  const liveNodes = await collectLiveNodesWithSessions(components, mesh, selfIds, localDaemonId);
89879
+ const ledgerSnapshot = sharedLedgerSnapshot ?? buildMeshActiveWorkLedgerSnapshot(readLedgerEntriesByKind(mesh.id, [
89880
+ "task_dispatched",
89881
+ "task_completed",
89882
+ "task_failed",
89883
+ "task_stalled",
89884
+ "task_approval_needed",
89885
+ "task_question_pending"
89886
+ ]));
89791
89887
  const result = pruneStaleDirectDispatches({
89792
89888
  meshId: mesh.id,
89793
89889
  queue: getQueue(mesh.id),
@@ -89796,14 +89892,8 @@ ${cleanBody}`;
89796
89892
  // tail:500 window can be crowded out by unrelated mesh traffic while a still-active
89797
89893
  // dispatch's ledger evidence falls out of the window, risking a live dispatch being
89798
89894
  // misclassified as an orphan and pruned.
89799
- ledgerEntries: readLedgerEntriesByKind(mesh.id, [
89800
- "task_dispatched",
89801
- "task_completed",
89802
- "task_failed",
89803
- "task_stalled",
89804
- "task_approval_needed",
89805
- "task_question_pending"
89806
- ]),
89895
+ ledgerEntries: ledgerSnapshot.entries,
89896
+ ledgerSnapshot,
89807
89897
  directDispatches,
89808
89898
  nodes: liveNodes,
89809
89899
  execute: true,
@@ -89813,6 +89903,7 @@ ${cleanBody}`;
89813
89903
  if (result.prunedCount > 0) {
89814
89904
  LOG2.info("MeshReconcile", `Auto-pruned ${result.prunedCount} orphaned direct dispatch record(s) for mesh ${mesh.id}`);
89815
89905
  }
89906
+ return ledgerSnapshot;
89816
89907
  }
89817
89908
  async function fetchAssignedTaskChatTail(components, mesh, row, tailLimit = 10) {
89818
89909
  const sessionId = readNonEmptyString(row.assignedSessionId);
@@ -91671,6 +91762,7 @@ ${cleanBody}`;
91671
91762
  return void 0;
91672
91763
  }
91673
91764
  })();
91765
+ const activeWorkLedgerSnapshots = /* @__PURE__ */ new Map();
91674
91766
  if (dispatchMeshCommand) {
91675
91767
  try {
91676
91768
  await retryUnresolvedDelegateForwards(components);
@@ -91828,11 +91920,16 @@ ${cleanBody}`;
91828
91920
  }
91829
91921
  {
91830
91922
  const minAgeMs = resolveAutoPruneMinAgeMs();
91923
+ const nowMs = Date.now();
91831
91924
  for (const mesh of listMeshes()) {
91832
91925
  const selfIds = resolveCoordinatorSelfIds(mesh, drainDaemonIds);
91833
91926
  if (!daemonHostsMesh(mesh, selfIds)) continue;
91927
+ const lastRunAt = lastAutoPruneRunAtByMesh.get(mesh.id);
91928
+ if (lastRunAt !== void 0 && nowMs - lastRunAt < DEFAULT_AUTO_PRUNE_INTERVAL_MS) continue;
91929
+ lastAutoPruneRunAtByMesh.set(mesh.id, nowMs);
91834
91930
  try {
91835
- await autoPruneStaleDirectDispatches(components, mesh, selfIds, localDaemonId, minAgeMs);
91931
+ const snapshot = await autoPruneStaleDirectDispatches(components, mesh, selfIds, localDaemonId, minAgeMs);
91932
+ if (snapshot) activeWorkLedgerSnapshots.set(mesh.id, snapshot);
91836
91933
  } catch (e) {
91837
91934
  LOG2.warn("MeshReconcile", `Auto-prune stale direct failed for mesh ${mesh.id}: ${e?.message || e}`);
91838
91935
  }
@@ -92059,7 +92156,8 @@ ${cleanBody}`;
92059
92156
  targetCoordinators[0].instance,
92060
92157
  getMesh(meshId)?.policy,
92061
92158
  void 0,
92062
- components.instanceManager
92159
+ components.instanceManager,
92160
+ activeWorkLedgerSnapshots.get(meshId)
92063
92161
  );
92064
92162
  continue;
92065
92163
  }
@@ -92126,6 +92224,7 @@ ${cleanBody}`;
92126
92224
  var lastDiskRetentionRunAt;
92127
92225
  var IDLE_SESSION_REAP_INTERVAL_MS;
92128
92226
  var lastIdleSessionReapRunAt;
92227
+ var lastAutoPruneRunAtByMesh;
92129
92228
  var init_mesh_reconcile_loop = __esm2({
92130
92229
  "src/mesh/mesh-reconcile-loop.ts"() {
92131
92230
  "use strict";
@@ -92164,6 +92263,7 @@ ${cleanBody}`;
92164
92263
  init_mesh_reconcile_unresolved_forward();
92165
92264
  DISK_RETENTION_INTERVAL_MS2 = 60 * 60 * 1e3;
92166
92265
  IDLE_SESSION_REAP_INTERVAL_MS = 5 * 60 * 1e3;
92266
+ lastAutoPruneRunAtByMesh = /* @__PURE__ */ new Map();
92167
92267
  }
92168
92268
  });
92169
92269
  var mesh_events_exports = {};