@adhdev/daemon-core 0.9.77-rc.30 → 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
@@ -978,6 +978,15 @@ var init_mesh_ledger = __esm({
978
978
  });
979
979
 
980
980
  // src/mesh/mesh-work-queue.ts
981
+ var mesh_work_queue_exports = {};
982
+ __export(mesh_work_queue_exports, {
983
+ claimNextTask: () => claimNextTask,
984
+ enqueueTask: () => enqueueTask,
985
+ getMeshQueueStats: () => getMeshQueueStats,
986
+ getQueue: () => getQueue,
987
+ updateSessionTaskStatus: () => updateSessionTaskStatus,
988
+ updateTaskStatus: () => updateTaskStatus
989
+ });
981
990
  function getQueuePath(meshId) {
982
991
  const safe = meshId.replace(/[^a-zA-Z0-9_-]/g, "_");
983
992
  return (0, import_path4.join)(getLedgerDir(), `${safe}.queue.json`);
@@ -1004,6 +1013,7 @@ function enqueueTask(meshId, message, opts) {
1004
1013
  message,
1005
1014
  status: "pending",
1006
1015
  targetNodeId: opts?.targetNodeId,
1016
+ targetSessionId: opts?.targetSessionId,
1007
1017
  createdAt: (/* @__PURE__ */ new Date()).toISOString(),
1008
1018
  updatedAt: (/* @__PURE__ */ new Date()).toISOString()
1009
1019
  };
@@ -1021,9 +1031,14 @@ function getQueue(meshId, opts) {
1021
1031
  }
1022
1032
  function claimNextTask(meshId, nodeId, sessionId) {
1023
1033
  const queue = readQueue(meshId);
1024
- let targetIdx = queue.findIndex((q) => q.status === "pending" && q.targetNodeId === nodeId);
1034
+ const hasActiveAssignment = queue.some((q) => q.status === "assigned" && (q.assignedSessionId === sessionId || q.assignedNodeId === nodeId));
1035
+ if (hasActiveAssignment) return null;
1036
+ let targetIdx = queue.findIndex((q) => q.status === "pending" && q.targetSessionId === sessionId);
1037
+ if (targetIdx === -1) {
1038
+ targetIdx = queue.findIndex((q) => q.status === "pending" && q.targetNodeId === nodeId && !q.targetSessionId);
1039
+ }
1025
1040
  if (targetIdx === -1) {
1026
- targetIdx = queue.findIndex((q) => q.status === "pending" && !q.targetNodeId);
1041
+ targetIdx = queue.findIndex((q) => q.status === "pending" && !q.targetNodeId && !q.targetSessionId);
1027
1042
  }
1028
1043
  if (targetIdx === -1) return null;
1029
1044
  const entry = queue[targetIdx];
@@ -1304,6 +1319,9 @@ function drainPendingMeshCoordinatorEvents() {
1304
1319
  function readNonEmptyString(value) {
1305
1320
  return typeof value === "string" && value.trim() ? value.trim() : "";
1306
1321
  }
1322
+ function resolveEventSessionId(event, fallback) {
1323
+ return readNonEmptyString(event.targetSessionId) || readNonEmptyString(event.sessionId) || readNonEmptyString(event.instanceId) || readNonEmptyString(fallback);
1324
+ }
1307
1325
  function isMeshCoordinatorEvent(eventName) {
1308
1326
  return typeof eventName === "string" && MESH_COORDINATOR_EVENTS.has(eventName);
1309
1327
  }
@@ -1428,7 +1446,7 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
1428
1446
  }
1429
1447
  function injectMeshSystemMessage(components, args) {
1430
1448
  if (args.event === "agent:generating_completed") {
1431
- const sessionId = readNonEmptyString(args.metadataEvent.targetSessionId);
1449
+ const sessionId = resolveEventSessionId(args.metadataEvent, args.sourceInstanceId);
1432
1450
  const nodeId = readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId);
1433
1451
  const providerType = readNonEmptyString(args.metadataEvent.providerType);
1434
1452
  if (sessionId) {
@@ -1440,9 +1458,29 @@ function injectMeshSystemMessage(components, args) {
1440
1458
  }
1441
1459
  }
1442
1460
  } else if (args.event === "agent:ready") {
1443
- const sessionId = readNonEmptyString(args.metadataEvent.targetSessionId);
1461
+ const sessionId = resolveEventSessionId(args.metadataEvent, args.sourceInstanceId);
1444
1462
  const nodeId = readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId);
1445
1463
  const providerType = readNonEmptyString(args.metadataEvent.providerType);
1464
+ const completedTask = sessionId ? updateSessionTaskStatus(args.meshId, sessionId, "completed") : null;
1465
+ if (completedTask) {
1466
+ try {
1467
+ appendLedgerEntry(args.meshId, {
1468
+ kind: "task_completed",
1469
+ nodeId: nodeId || void 0,
1470
+ sessionId,
1471
+ providerType: providerType || void 0,
1472
+ payload: {
1473
+ event: args.event,
1474
+ nodeLabel: args.nodeLabel,
1475
+ taskId: completedTask.id,
1476
+ completedViaReady: true,
1477
+ providerSessionId: readNonEmptyString(args.metadataEvent.providerSessionId) || void 0
1478
+ }
1479
+ });
1480
+ } catch (e) {
1481
+ LOG.warn("MeshLedger", `Failed to record task_completed from ready: ${e?.message || e}`);
1482
+ }
1483
+ }
1446
1484
  if (sessionId && nodeId && providerType) {
1447
1485
  remoteIdleSessions.set(`${nodeId}:${sessionId}`, { nodeId, sessionId, providerType });
1448
1486
  setTimeout(() => {
@@ -1453,13 +1491,13 @@ function injectMeshSystemMessage(components, args) {
1453
1491
  }, 500);
1454
1492
  }
1455
1493
  } else if (args.event === "agent:generating_started") {
1456
- const sessionId = readNonEmptyString(args.metadataEvent.targetSessionId);
1494
+ const sessionId = resolveEventSessionId(args.metadataEvent, args.sourceInstanceId);
1457
1495
  const nodeId = readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId);
1458
1496
  if (sessionId && nodeId) {
1459
1497
  remoteIdleSessions.delete(`${nodeId}:${sessionId}`);
1460
1498
  }
1461
1499
  } else if (args.event === "agent:stopped") {
1462
- const sessionId = readNonEmptyString(args.metadataEvent.targetSessionId);
1500
+ const sessionId = resolveEventSessionId(args.metadataEvent, args.sourceInstanceId);
1463
1501
  const nodeId = readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId);
1464
1502
  if (sessionId && nodeId) {
1465
1503
  remoteIdleSessions.delete(`${nodeId}:${sessionId}`);
@@ -1474,7 +1512,7 @@ function injectMeshSystemMessage(components, args) {
1474
1512
  appendLedgerEntry(args.meshId, {
1475
1513
  kind: ledgerKind,
1476
1514
  nodeId: readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId) || void 0,
1477
- sessionId: readNonEmptyString(args.metadataEvent.targetSessionId) || void 0,
1515
+ sessionId: resolveEventSessionId(args.metadataEvent, args.sourceInstanceId) || void 0,
1478
1516
  providerType: readNonEmptyString(args.metadataEvent.providerType) || void 0,
1479
1517
  payload: {
1480
1518
  event: args.event,
@@ -1492,7 +1530,7 @@ function injectMeshSystemMessage(components, args) {
1492
1530
  const mesh = getMesh(args.meshId);
1493
1531
  const maxRetries = mesh?.policy?.maxTaskRetries ?? 1;
1494
1532
  recoveryContext = getSessionRecoveryContext(args.meshId, {
1495
- sessionId: readNonEmptyString(args.metadataEvent.targetSessionId) || void 0,
1533
+ sessionId: resolveEventSessionId(args.metadataEvent, args.sourceInstanceId) || void 0,
1496
1534
  nodeId: readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId) || void 0,
1497
1535
  maxRetries
1498
1536
  });
@@ -1592,7 +1630,7 @@ function handleMeshForwardEvent(components, payload) {
1592
1630
  nodeLabel,
1593
1631
  event: eventName,
1594
1632
  metadataEvent: {
1595
- targetSessionId: readNonEmptyString(payload.targetSessionId) || readNonEmptyString(payload.sessionId),
1633
+ targetSessionId: readNonEmptyString(payload.targetSessionId) || readNonEmptyString(payload.sessionId) || readNonEmptyString(payload.instanceId),
1596
1634
  providerType: readNonEmptyString(payload.providerType),
1597
1635
  providerSessionId: readNonEmptyString(payload.providerSessionId)
1598
1636
  }
@@ -1642,6 +1680,7 @@ var init_mesh_events = __esm({
1642
1680
  MAX_PENDING_EVENTS = 50;
1643
1681
  pendingMeshCoordinatorEvents = [];
1644
1682
  MESH_COORDINATOR_EVENTS = /* @__PURE__ */ new Set([
1683
+ "agent:generating_started",
1645
1684
  "agent:generating_completed",
1646
1685
  "agent:waiting_approval",
1647
1686
  "agent:stopped",
@@ -23863,6 +23902,18 @@ var DaemonCommandRouter = class {
23863
23902
  return { success: false, error: e.message };
23864
23903
  }
23865
23904
  }
23905
+ case "get_mesh_queue": {
23906
+ const meshId = typeof args?.meshId === "string" ? args.meshId.trim() : "";
23907
+ if (!meshId) return { success: false, error: "meshId required" };
23908
+ try {
23909
+ const { getQueue: getQueue2 } = await Promise.resolve().then(() => (init_mesh_work_queue(), mesh_work_queue_exports));
23910
+ const status = Array.isArray(args?.status) ? args.status.map((s) => typeof s === "string" ? s.trim() : "").filter(Boolean) : void 0;
23911
+ const queue = getQueue2(meshId, { status });
23912
+ return { success: true, queue };
23913
+ } catch (e) {
23914
+ return { success: false, error: e.message };
23915
+ }
23916
+ }
23866
23917
  case "add_mesh_node": {
23867
23918
  const meshId = typeof args?.meshId === "string" ? args.meshId.trim() : "";
23868
23919
  const workspace = typeof args?.workspace === "string" ? args.workspace.trim() : "";