@adhdev/daemon-standalone 0.9.77-rc.31 → 0.9.77-rc.32

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
@@ -32176,6 +32176,15 @@ Follow these recovery rules:
32176
32176
  meshLedgerEvents = new import_events.EventEmitter();
32177
32177
  }
32178
32178
  });
32179
+ var mesh_work_queue_exports = {};
32180
+ __export2(mesh_work_queue_exports, {
32181
+ claimNextTask: () => claimNextTask,
32182
+ enqueueTask: () => enqueueTask,
32183
+ getMeshQueueStats: () => getMeshQueueStats,
32184
+ getQueue: () => getQueue,
32185
+ updateSessionTaskStatus: () => updateSessionTaskStatus,
32186
+ updateTaskStatus: () => updateTaskStatus
32187
+ });
32179
32188
  function getQueuePath(meshId) {
32180
32189
  const safe = meshId.replace(/[^a-zA-Z0-9_-]/g, "_");
32181
32190
  return (0, import_path4.join)(getLedgerDir(), `${safe}.queue.json`);
@@ -32220,6 +32229,8 @@ Follow these recovery rules:
32220
32229
  }
32221
32230
  function claimNextTask(meshId, nodeId, sessionId) {
32222
32231
  const queue = readQueue(meshId);
32232
+ const hasActiveAssignment = queue.some((q2) => q2.status === "assigned" && (q2.assignedSessionId === sessionId || q2.assignedNodeId === nodeId));
32233
+ if (hasActiveAssignment) return null;
32223
32234
  let targetIdx = queue.findIndex((q2) => q2.status === "pending" && q2.targetSessionId === sessionId);
32224
32235
  if (targetIdx === -1) {
32225
32236
  targetIdx = queue.findIndex((q2) => q2.status === "pending" && q2.targetNodeId === nodeId && !q2.targetSessionId);
@@ -32523,6 +32534,9 @@ Follow these recovery rules:
32523
32534
  function readNonEmptyString(value) {
32524
32535
  return typeof value === "string" && value.trim() ? value.trim() : "";
32525
32536
  }
32537
+ function resolveEventSessionId(event, fallback) {
32538
+ return readNonEmptyString(event.targetSessionId) || readNonEmptyString(event.sessionId) || readNonEmptyString(event.instanceId) || readNonEmptyString(fallback);
32539
+ }
32526
32540
  function isMeshCoordinatorEvent(eventName) {
32527
32541
  return typeof eventName === "string" && MESH_COORDINATOR_EVENTS.has(eventName);
32528
32542
  }
@@ -32647,7 +32661,7 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
32647
32661
  }
32648
32662
  function injectMeshSystemMessage(components, args) {
32649
32663
  if (args.event === "agent:generating_completed") {
32650
- const sessionId = readNonEmptyString(args.metadataEvent.targetSessionId);
32664
+ const sessionId = resolveEventSessionId(args.metadataEvent, args.sourceInstanceId);
32651
32665
  const nodeId = readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId);
32652
32666
  const providerType = readNonEmptyString(args.metadataEvent.providerType);
32653
32667
  if (sessionId) {
@@ -32659,9 +32673,29 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
32659
32673
  }
32660
32674
  }
32661
32675
  } else if (args.event === "agent:ready") {
32662
- const sessionId = readNonEmptyString(args.metadataEvent.targetSessionId);
32676
+ const sessionId = resolveEventSessionId(args.metadataEvent, args.sourceInstanceId);
32663
32677
  const nodeId = readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId);
32664
32678
  const providerType = readNonEmptyString(args.metadataEvent.providerType);
32679
+ const completedTask = sessionId ? updateSessionTaskStatus(args.meshId, sessionId, "completed") : null;
32680
+ if (completedTask) {
32681
+ try {
32682
+ appendLedgerEntry(args.meshId, {
32683
+ kind: "task_completed",
32684
+ nodeId: nodeId || void 0,
32685
+ sessionId,
32686
+ providerType: providerType || void 0,
32687
+ payload: {
32688
+ event: args.event,
32689
+ nodeLabel: args.nodeLabel,
32690
+ taskId: completedTask.id,
32691
+ completedViaReady: true,
32692
+ providerSessionId: readNonEmptyString(args.metadataEvent.providerSessionId) || void 0
32693
+ }
32694
+ });
32695
+ } catch (e) {
32696
+ LOG2.warn("MeshLedger", `Failed to record task_completed from ready: ${e?.message || e}`);
32697
+ }
32698
+ }
32665
32699
  if (sessionId && nodeId && providerType) {
32666
32700
  remoteIdleSessions.set(`${nodeId}:${sessionId}`, { nodeId, sessionId, providerType });
32667
32701
  setTimeout(() => {
@@ -32672,13 +32706,13 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
32672
32706
  }, 500);
32673
32707
  }
32674
32708
  } else if (args.event === "agent:generating_started") {
32675
- const sessionId = readNonEmptyString(args.metadataEvent.targetSessionId);
32709
+ const sessionId = resolveEventSessionId(args.metadataEvent, args.sourceInstanceId);
32676
32710
  const nodeId = readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId);
32677
32711
  if (sessionId && nodeId) {
32678
32712
  remoteIdleSessions.delete(`${nodeId}:${sessionId}`);
32679
32713
  }
32680
32714
  } else if (args.event === "agent:stopped") {
32681
- const sessionId = readNonEmptyString(args.metadataEvent.targetSessionId);
32715
+ const sessionId = resolveEventSessionId(args.metadataEvent, args.sourceInstanceId);
32682
32716
  const nodeId = readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId);
32683
32717
  if (sessionId && nodeId) {
32684
32718
  remoteIdleSessions.delete(`${nodeId}:${sessionId}`);
@@ -32693,7 +32727,7 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
32693
32727
  appendLedgerEntry(args.meshId, {
32694
32728
  kind: ledgerKind,
32695
32729
  nodeId: readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId) || void 0,
32696
- sessionId: readNonEmptyString(args.metadataEvent.targetSessionId) || void 0,
32730
+ sessionId: resolveEventSessionId(args.metadataEvent, args.sourceInstanceId) || void 0,
32697
32731
  providerType: readNonEmptyString(args.metadataEvent.providerType) || void 0,
32698
32732
  payload: {
32699
32733
  event: args.event,
@@ -32711,7 +32745,7 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
32711
32745
  const mesh = getMesh(args.meshId);
32712
32746
  const maxRetries = mesh?.policy?.maxTaskRetries ?? 1;
32713
32747
  recoveryContext = getSessionRecoveryContext(args.meshId, {
32714
- sessionId: readNonEmptyString(args.metadataEvent.targetSessionId) || void 0,
32748
+ sessionId: resolveEventSessionId(args.metadataEvent, args.sourceInstanceId) || void 0,
32715
32749
  nodeId: readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId) || void 0,
32716
32750
  maxRetries
32717
32751
  });
@@ -32811,7 +32845,7 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
32811
32845
  nodeLabel,
32812
32846
  event: eventName,
32813
32847
  metadataEvent: {
32814
- targetSessionId: readNonEmptyString(payload.targetSessionId) || readNonEmptyString(payload.sessionId),
32848
+ targetSessionId: readNonEmptyString(payload.targetSessionId) || readNonEmptyString(payload.sessionId) || readNonEmptyString(payload.instanceId),
32815
32849
  providerType: readNonEmptyString(payload.providerType),
32816
32850
  providerSessionId: readNonEmptyString(payload.providerSessionId)
32817
32851
  }
@@ -32865,6 +32899,7 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
32865
32899
  MAX_PENDING_EVENTS = 50;
32866
32900
  pendingMeshCoordinatorEvents = [];
32867
32901
  MESH_COORDINATOR_EVENTS = /* @__PURE__ */ new Set([
32902
+ "agent:generating_started",
32868
32903
  "agent:generating_completed",
32869
32904
  "agent:waiting_approval",
32870
32905
  "agent:stopped",
@@ -54911,6 +54946,18 @@ ${(0, import_node_path.resolve)(workspace || os17.tmpdir())}`;
54911
54946
  return { success: false, error: e.message };
54912
54947
  }
54913
54948
  }
54949
+ case "get_mesh_queue": {
54950
+ const meshId = typeof args?.meshId === "string" ? args.meshId.trim() : "";
54951
+ if (!meshId) return { success: false, error: "meshId required" };
54952
+ try {
54953
+ const { getQueue: getQueue2 } = await Promise.resolve().then(() => (init_mesh_work_queue(), mesh_work_queue_exports));
54954
+ const status = Array.isArray(args?.status) ? args.status.map((s15) => typeof s15 === "string" ? s15.trim() : "").filter(Boolean) : void 0;
54955
+ const queue = getQueue2(meshId, { status });
54956
+ return { success: true, queue };
54957
+ } catch (e) {
54958
+ return { success: false, error: e.message };
54959
+ }
54960
+ }
54914
54961
  case "add_mesh_node": {
54915
54962
  const meshId = typeof args?.meshId === "string" ? args.meshId.trim() : "";
54916
54963
  const workspace = typeof args?.workspace === "string" ? args.workspace.trim() : "";