@adhdev/daemon-core 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
@@ -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`);
@@ -1022,6 +1031,8 @@ function getQueue(meshId, opts) {
1022
1031
  }
1023
1032
  function claimNextTask(meshId, nodeId, sessionId) {
1024
1033
  const queue = readQueue(meshId);
1034
+ const hasActiveAssignment = queue.some((q) => q.status === "assigned" && (q.assignedSessionId === sessionId || q.assignedNodeId === nodeId));
1035
+ if (hasActiveAssignment) return null;
1025
1036
  let targetIdx = queue.findIndex((q) => q.status === "pending" && q.targetSessionId === sessionId);
1026
1037
  if (targetIdx === -1) {
1027
1038
  targetIdx = queue.findIndex((q) => q.status === "pending" && q.targetNodeId === nodeId && !q.targetSessionId);
@@ -1308,6 +1319,9 @@ function drainPendingMeshCoordinatorEvents() {
1308
1319
  function readNonEmptyString(value) {
1309
1320
  return typeof value === "string" && value.trim() ? value.trim() : "";
1310
1321
  }
1322
+ function resolveEventSessionId(event, fallback) {
1323
+ return readNonEmptyString(event.targetSessionId) || readNonEmptyString(event.sessionId) || readNonEmptyString(event.instanceId) || readNonEmptyString(fallback);
1324
+ }
1311
1325
  function isMeshCoordinatorEvent(eventName) {
1312
1326
  return typeof eventName === "string" && MESH_COORDINATOR_EVENTS.has(eventName);
1313
1327
  }
@@ -1432,7 +1446,7 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
1432
1446
  }
1433
1447
  function injectMeshSystemMessage(components, args) {
1434
1448
  if (args.event === "agent:generating_completed") {
1435
- const sessionId = readNonEmptyString(args.metadataEvent.targetSessionId);
1449
+ const sessionId = resolveEventSessionId(args.metadataEvent, args.sourceInstanceId);
1436
1450
  const nodeId = readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId);
1437
1451
  const providerType = readNonEmptyString(args.metadataEvent.providerType);
1438
1452
  if (sessionId) {
@@ -1444,9 +1458,29 @@ function injectMeshSystemMessage(components, args) {
1444
1458
  }
1445
1459
  }
1446
1460
  } else if (args.event === "agent:ready") {
1447
- const sessionId = readNonEmptyString(args.metadataEvent.targetSessionId);
1461
+ const sessionId = resolveEventSessionId(args.metadataEvent, args.sourceInstanceId);
1448
1462
  const nodeId = readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId);
1449
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
+ }
1450
1484
  if (sessionId && nodeId && providerType) {
1451
1485
  remoteIdleSessions.set(`${nodeId}:${sessionId}`, { nodeId, sessionId, providerType });
1452
1486
  setTimeout(() => {
@@ -1457,13 +1491,13 @@ function injectMeshSystemMessage(components, args) {
1457
1491
  }, 500);
1458
1492
  }
1459
1493
  } else if (args.event === "agent:generating_started") {
1460
- const sessionId = readNonEmptyString(args.metadataEvent.targetSessionId);
1494
+ const sessionId = resolveEventSessionId(args.metadataEvent, args.sourceInstanceId);
1461
1495
  const nodeId = readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId);
1462
1496
  if (sessionId && nodeId) {
1463
1497
  remoteIdleSessions.delete(`${nodeId}:${sessionId}`);
1464
1498
  }
1465
1499
  } else if (args.event === "agent:stopped") {
1466
- const sessionId = readNonEmptyString(args.metadataEvent.targetSessionId);
1500
+ const sessionId = resolveEventSessionId(args.metadataEvent, args.sourceInstanceId);
1467
1501
  const nodeId = readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId);
1468
1502
  if (sessionId && nodeId) {
1469
1503
  remoteIdleSessions.delete(`${nodeId}:${sessionId}`);
@@ -1478,7 +1512,7 @@ function injectMeshSystemMessage(components, args) {
1478
1512
  appendLedgerEntry(args.meshId, {
1479
1513
  kind: ledgerKind,
1480
1514
  nodeId: readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId) || void 0,
1481
- sessionId: readNonEmptyString(args.metadataEvent.targetSessionId) || void 0,
1515
+ sessionId: resolveEventSessionId(args.metadataEvent, args.sourceInstanceId) || void 0,
1482
1516
  providerType: readNonEmptyString(args.metadataEvent.providerType) || void 0,
1483
1517
  payload: {
1484
1518
  event: args.event,
@@ -1496,7 +1530,7 @@ function injectMeshSystemMessage(components, args) {
1496
1530
  const mesh = getMesh(args.meshId);
1497
1531
  const maxRetries = mesh?.policy?.maxTaskRetries ?? 1;
1498
1532
  recoveryContext = getSessionRecoveryContext(args.meshId, {
1499
- sessionId: readNonEmptyString(args.metadataEvent.targetSessionId) || void 0,
1533
+ sessionId: resolveEventSessionId(args.metadataEvent, args.sourceInstanceId) || void 0,
1500
1534
  nodeId: readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId) || void 0,
1501
1535
  maxRetries
1502
1536
  });
@@ -1596,7 +1630,7 @@ function handleMeshForwardEvent(components, payload) {
1596
1630
  nodeLabel,
1597
1631
  event: eventName,
1598
1632
  metadataEvent: {
1599
- targetSessionId: readNonEmptyString(payload.targetSessionId) || readNonEmptyString(payload.sessionId),
1633
+ targetSessionId: readNonEmptyString(payload.targetSessionId) || readNonEmptyString(payload.sessionId) || readNonEmptyString(payload.instanceId),
1600
1634
  providerType: readNonEmptyString(payload.providerType),
1601
1635
  providerSessionId: readNonEmptyString(payload.providerSessionId)
1602
1636
  }
@@ -1646,6 +1680,7 @@ var init_mesh_events = __esm({
1646
1680
  MAX_PENDING_EVENTS = 50;
1647
1681
  pendingMeshCoordinatorEvents = [];
1648
1682
  MESH_COORDINATOR_EVENTS = /* @__PURE__ */ new Set([
1683
+ "agent:generating_started",
1649
1684
  "agent:generating_completed",
1650
1685
  "agent:waiting_approval",
1651
1686
  "agent:stopped",
@@ -23867,6 +23902,18 @@ var DaemonCommandRouter = class {
23867
23902
  return { success: false, error: e.message };
23868
23903
  }
23869
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
+ }
23870
23917
  case "add_mesh_node": {
23871
23918
  const meshId = typeof args?.meshId === "string" ? args.meshId.trim() : "";
23872
23919
  const workspace = typeof args?.workspace === "string" ? args.workspace.trim() : "";