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

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
  }
@@ -32581,10 +32595,12 @@ Follow these recovery rules:
32581
32595
  const state = inst.getState();
32582
32596
  const settings = state.settings || {};
32583
32597
  const instMeshId = readNonEmptyString(settings.meshNodeFor);
32584
- if (instMeshId !== meshId && !settings.launchedByCoordinator) continue;
32598
+ if (instMeshId !== meshId) continue;
32585
32599
  const nodeId = readNonEmptyString(settings.meshNodeId) || readNonEmptyString(settings.nodeId);
32586
32600
  if (!nodeId) continue;
32587
- if (state.status !== "idle" && state.status !== "stopped" && state.activeChat?.status !== "waiting_input") continue;
32601
+ const status = readNonEmptyString(state.status).toLowerCase();
32602
+ if (["stopped", "failed", "terminated", "exited", "closed"].includes(status)) continue;
32603
+ if (status !== "idle" && state.activeChat?.status !== "waiting_input") continue;
32588
32604
  const sessionId = state.instanceId;
32589
32605
  const providerType = state.type || readNonEmptyString(settings.providerType);
32590
32606
  if (providerType) {
@@ -32647,7 +32663,7 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
32647
32663
  }
32648
32664
  function injectMeshSystemMessage(components, args) {
32649
32665
  if (args.event === "agent:generating_completed") {
32650
- const sessionId = readNonEmptyString(args.metadataEvent.targetSessionId);
32666
+ const sessionId = resolveEventSessionId(args.metadataEvent, args.sourceInstanceId);
32651
32667
  const nodeId = readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId);
32652
32668
  const providerType = readNonEmptyString(args.metadataEvent.providerType);
32653
32669
  if (sessionId) {
@@ -32659,9 +32675,29 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
32659
32675
  }
32660
32676
  }
32661
32677
  } else if (args.event === "agent:ready") {
32662
- const sessionId = readNonEmptyString(args.metadataEvent.targetSessionId);
32678
+ const sessionId = resolveEventSessionId(args.metadataEvent, args.sourceInstanceId);
32663
32679
  const nodeId = readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId);
32664
32680
  const providerType = readNonEmptyString(args.metadataEvent.providerType);
32681
+ const completedTask = sessionId ? updateSessionTaskStatus(args.meshId, sessionId, "completed") : null;
32682
+ if (completedTask) {
32683
+ try {
32684
+ appendLedgerEntry(args.meshId, {
32685
+ kind: "task_completed",
32686
+ nodeId: nodeId || void 0,
32687
+ sessionId,
32688
+ providerType: providerType || void 0,
32689
+ payload: {
32690
+ event: args.event,
32691
+ nodeLabel: args.nodeLabel,
32692
+ taskId: completedTask.id,
32693
+ completedViaReady: true,
32694
+ providerSessionId: readNonEmptyString(args.metadataEvent.providerSessionId) || void 0
32695
+ }
32696
+ });
32697
+ } catch (e) {
32698
+ LOG2.warn("MeshLedger", `Failed to record task_completed from ready: ${e?.message || e}`);
32699
+ }
32700
+ }
32665
32701
  if (sessionId && nodeId && providerType) {
32666
32702
  remoteIdleSessions.set(`${nodeId}:${sessionId}`, { nodeId, sessionId, providerType });
32667
32703
  setTimeout(() => {
@@ -32672,13 +32708,13 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
32672
32708
  }, 500);
32673
32709
  }
32674
32710
  } else if (args.event === "agent:generating_started") {
32675
- const sessionId = readNonEmptyString(args.metadataEvent.targetSessionId);
32711
+ const sessionId = resolveEventSessionId(args.metadataEvent, args.sourceInstanceId);
32676
32712
  const nodeId = readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId);
32677
32713
  if (sessionId && nodeId) {
32678
32714
  remoteIdleSessions.delete(`${nodeId}:${sessionId}`);
32679
32715
  }
32680
32716
  } else if (args.event === "agent:stopped") {
32681
- const sessionId = readNonEmptyString(args.metadataEvent.targetSessionId);
32717
+ const sessionId = resolveEventSessionId(args.metadataEvent, args.sourceInstanceId);
32682
32718
  const nodeId = readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId);
32683
32719
  if (sessionId && nodeId) {
32684
32720
  remoteIdleSessions.delete(`${nodeId}:${sessionId}`);
@@ -32693,7 +32729,7 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
32693
32729
  appendLedgerEntry(args.meshId, {
32694
32730
  kind: ledgerKind,
32695
32731
  nodeId: readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId) || void 0,
32696
- sessionId: readNonEmptyString(args.metadataEvent.targetSessionId) || void 0,
32732
+ sessionId: resolveEventSessionId(args.metadataEvent, args.sourceInstanceId) || void 0,
32697
32733
  providerType: readNonEmptyString(args.metadataEvent.providerType) || void 0,
32698
32734
  payload: {
32699
32735
  event: args.event,
@@ -32711,7 +32747,7 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
32711
32747
  const mesh = getMesh(args.meshId);
32712
32748
  const maxRetries = mesh?.policy?.maxTaskRetries ?? 1;
32713
32749
  recoveryContext = getSessionRecoveryContext(args.meshId, {
32714
- sessionId: readNonEmptyString(args.metadataEvent.targetSessionId) || void 0,
32750
+ sessionId: resolveEventSessionId(args.metadataEvent, args.sourceInstanceId) || void 0,
32715
32751
  nodeId: readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId) || void 0,
32716
32752
  maxRetries
32717
32753
  });
@@ -32811,7 +32847,7 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
32811
32847
  nodeLabel,
32812
32848
  event: eventName,
32813
32849
  metadataEvent: {
32814
- targetSessionId: readNonEmptyString(payload.targetSessionId) || readNonEmptyString(payload.sessionId),
32850
+ targetSessionId: readNonEmptyString(payload.targetSessionId) || readNonEmptyString(payload.sessionId) || readNonEmptyString(payload.instanceId),
32815
32851
  providerType: readNonEmptyString(payload.providerType),
32816
32852
  providerSessionId: readNonEmptyString(payload.providerSessionId)
32817
32853
  }
@@ -32865,6 +32901,7 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
32865
32901
  MAX_PENDING_EVENTS = 50;
32866
32902
  pendingMeshCoordinatorEvents = [];
32867
32903
  MESH_COORDINATOR_EVENTS = /* @__PURE__ */ new Set([
32904
+ "agent:generating_started",
32868
32905
  "agent:generating_completed",
32869
32906
  "agent:waiting_approval",
32870
32907
  "agent:stopped",
@@ -54911,6 +54948,18 @@ ${(0, import_node_path.resolve)(workspace || os17.tmpdir())}`;
54911
54948
  return { success: false, error: e.message };
54912
54949
  }
54913
54950
  }
54951
+ case "get_mesh_queue": {
54952
+ const meshId = typeof args?.meshId === "string" ? args.meshId.trim() : "";
54953
+ if (!meshId) return { success: false, error: "meshId required" };
54954
+ try {
54955
+ const { getQueue: getQueue2 } = await Promise.resolve().then(() => (init_mesh_work_queue(), mesh_work_queue_exports));
54956
+ const status = Array.isArray(args?.status) ? args.status.map((s15) => typeof s15 === "string" ? s15.trim() : "").filter(Boolean) : void 0;
54957
+ const queue = getQueue2(meshId, { status });
54958
+ return { success: true, queue };
54959
+ } catch (e) {
54960
+ return { success: false, error: e.message };
54961
+ }
54962
+ }
54914
54963
  case "add_mesh_node": {
54915
54964
  const meshId = typeof args?.meshId === "string" ? args.meshId.trim() : "";
54916
54965
  const workspace = typeof args?.workspace === "string" ? args.workspace.trim() : "";