@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.mjs CHANGED
@@ -973,6 +973,15 @@ var init_mesh_ledger = __esm({
973
973
  });
974
974
 
975
975
  // src/mesh/mesh-work-queue.ts
976
+ var mesh_work_queue_exports = {};
977
+ __export(mesh_work_queue_exports, {
978
+ claimNextTask: () => claimNextTask,
979
+ enqueueTask: () => enqueueTask,
980
+ getMeshQueueStats: () => getMeshQueueStats,
981
+ getQueue: () => getQueue,
982
+ updateSessionTaskStatus: () => updateSessionTaskStatus,
983
+ updateTaskStatus: () => updateTaskStatus
984
+ });
976
985
  import { existsSync as existsSync6, writeFileSync as writeFileSync3, readFileSync as readFileSync4 } from "fs";
977
986
  import { join as join6 } from "path";
978
987
  import { randomUUID as randomUUID5 } from "crypto";
@@ -1020,6 +1029,8 @@ function getQueue(meshId, opts) {
1020
1029
  }
1021
1030
  function claimNextTask(meshId, nodeId, sessionId) {
1022
1031
  const queue = readQueue(meshId);
1032
+ const hasActiveAssignment = queue.some((q) => q.status === "assigned" && (q.assignedSessionId === sessionId || q.assignedNodeId === nodeId));
1033
+ if (hasActiveAssignment) return null;
1023
1034
  let targetIdx = queue.findIndex((q) => q.status === "pending" && q.targetSessionId === sessionId);
1024
1035
  if (targetIdx === -1) {
1025
1036
  targetIdx = queue.findIndex((q) => q.status === "pending" && q.targetNodeId === nodeId && !q.targetSessionId);
@@ -1302,6 +1313,9 @@ function drainPendingMeshCoordinatorEvents() {
1302
1313
  function readNonEmptyString(value) {
1303
1314
  return typeof value === "string" && value.trim() ? value.trim() : "";
1304
1315
  }
1316
+ function resolveEventSessionId(event, fallback) {
1317
+ return readNonEmptyString(event.targetSessionId) || readNonEmptyString(event.sessionId) || readNonEmptyString(event.instanceId) || readNonEmptyString(fallback);
1318
+ }
1305
1319
  function isMeshCoordinatorEvent(eventName) {
1306
1320
  return typeof eventName === "string" && MESH_COORDINATOR_EVENTS.has(eventName);
1307
1321
  }
@@ -1426,7 +1440,7 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
1426
1440
  }
1427
1441
  function injectMeshSystemMessage(components, args) {
1428
1442
  if (args.event === "agent:generating_completed") {
1429
- const sessionId = readNonEmptyString(args.metadataEvent.targetSessionId);
1443
+ const sessionId = resolveEventSessionId(args.metadataEvent, args.sourceInstanceId);
1430
1444
  const nodeId = readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId);
1431
1445
  const providerType = readNonEmptyString(args.metadataEvent.providerType);
1432
1446
  if (sessionId) {
@@ -1438,9 +1452,29 @@ function injectMeshSystemMessage(components, args) {
1438
1452
  }
1439
1453
  }
1440
1454
  } else if (args.event === "agent:ready") {
1441
- const sessionId = readNonEmptyString(args.metadataEvent.targetSessionId);
1455
+ const sessionId = resolveEventSessionId(args.metadataEvent, args.sourceInstanceId);
1442
1456
  const nodeId = readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId);
1443
1457
  const providerType = readNonEmptyString(args.metadataEvent.providerType);
1458
+ const completedTask = sessionId ? updateSessionTaskStatus(args.meshId, sessionId, "completed") : null;
1459
+ if (completedTask) {
1460
+ try {
1461
+ appendLedgerEntry(args.meshId, {
1462
+ kind: "task_completed",
1463
+ nodeId: nodeId || void 0,
1464
+ sessionId,
1465
+ providerType: providerType || void 0,
1466
+ payload: {
1467
+ event: args.event,
1468
+ nodeLabel: args.nodeLabel,
1469
+ taskId: completedTask.id,
1470
+ completedViaReady: true,
1471
+ providerSessionId: readNonEmptyString(args.metadataEvent.providerSessionId) || void 0
1472
+ }
1473
+ });
1474
+ } catch (e) {
1475
+ LOG.warn("MeshLedger", `Failed to record task_completed from ready: ${e?.message || e}`);
1476
+ }
1477
+ }
1444
1478
  if (sessionId && nodeId && providerType) {
1445
1479
  remoteIdleSessions.set(`${nodeId}:${sessionId}`, { nodeId, sessionId, providerType });
1446
1480
  setTimeout(() => {
@@ -1451,13 +1485,13 @@ function injectMeshSystemMessage(components, args) {
1451
1485
  }, 500);
1452
1486
  }
1453
1487
  } else if (args.event === "agent:generating_started") {
1454
- const sessionId = readNonEmptyString(args.metadataEvent.targetSessionId);
1488
+ const sessionId = resolveEventSessionId(args.metadataEvent, args.sourceInstanceId);
1455
1489
  const nodeId = readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId);
1456
1490
  if (sessionId && nodeId) {
1457
1491
  remoteIdleSessions.delete(`${nodeId}:${sessionId}`);
1458
1492
  }
1459
1493
  } else if (args.event === "agent:stopped") {
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}`);
@@ -1472,7 +1506,7 @@ function injectMeshSystemMessage(components, args) {
1472
1506
  appendLedgerEntry(args.meshId, {
1473
1507
  kind: ledgerKind,
1474
1508
  nodeId: readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId) || void 0,
1475
- sessionId: readNonEmptyString(args.metadataEvent.targetSessionId) || void 0,
1509
+ sessionId: resolveEventSessionId(args.metadataEvent, args.sourceInstanceId) || void 0,
1476
1510
  providerType: readNonEmptyString(args.metadataEvent.providerType) || void 0,
1477
1511
  payload: {
1478
1512
  event: args.event,
@@ -1490,7 +1524,7 @@ function injectMeshSystemMessage(components, args) {
1490
1524
  const mesh = getMesh(args.meshId);
1491
1525
  const maxRetries = mesh?.policy?.maxTaskRetries ?? 1;
1492
1526
  recoveryContext = getSessionRecoveryContext(args.meshId, {
1493
- sessionId: readNonEmptyString(args.metadataEvent.targetSessionId) || void 0,
1527
+ sessionId: resolveEventSessionId(args.metadataEvent, args.sourceInstanceId) || void 0,
1494
1528
  nodeId: readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId) || void 0,
1495
1529
  maxRetries
1496
1530
  });
@@ -1590,7 +1624,7 @@ function handleMeshForwardEvent(components, payload) {
1590
1624
  nodeLabel,
1591
1625
  event: eventName,
1592
1626
  metadataEvent: {
1593
- targetSessionId: readNonEmptyString(payload.targetSessionId) || readNonEmptyString(payload.sessionId),
1627
+ targetSessionId: readNonEmptyString(payload.targetSessionId) || readNonEmptyString(payload.sessionId) || readNonEmptyString(payload.instanceId),
1594
1628
  providerType: readNonEmptyString(payload.providerType),
1595
1629
  providerSessionId: readNonEmptyString(payload.providerSessionId)
1596
1630
  }
@@ -1640,6 +1674,7 @@ var init_mesh_events = __esm({
1640
1674
  MAX_PENDING_EVENTS = 50;
1641
1675
  pendingMeshCoordinatorEvents = [];
1642
1676
  MESH_COORDINATOR_EVENTS = /* @__PURE__ */ new Set([
1677
+ "agent:generating_started",
1643
1678
  "agent:generating_completed",
1644
1679
  "agent:waiting_approval",
1645
1680
  "agent:stopped",
@@ -23650,6 +23685,18 @@ var DaemonCommandRouter = class {
23650
23685
  return { success: false, error: e.message };
23651
23686
  }
23652
23687
  }
23688
+ case "get_mesh_queue": {
23689
+ const meshId = typeof args?.meshId === "string" ? args.meshId.trim() : "";
23690
+ if (!meshId) return { success: false, error: "meshId required" };
23691
+ try {
23692
+ const { getQueue: getQueue2 } = await Promise.resolve().then(() => (init_mesh_work_queue(), mesh_work_queue_exports));
23693
+ const status = Array.isArray(args?.status) ? args.status.map((s) => typeof s === "string" ? s.trim() : "").filter(Boolean) : void 0;
23694
+ const queue = getQueue2(meshId, { status });
23695
+ return { success: true, queue };
23696
+ } catch (e) {
23697
+ return { success: false, error: e.message };
23698
+ }
23699
+ }
23653
23700
  case "add_mesh_node": {
23654
23701
  const meshId = typeof args?.meshId === "string" ? args.meshId.trim() : "";
23655
23702
  const workspace = typeof args?.workspace === "string" ? args.workspace.trim() : "";