@adhdev/daemon-standalone 0.9.82-rc.373 → 0.9.82-rc.375

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.
@@ -382,213 +382,57 @@ function annotateRapidReadChatAdvisory(payload, options) {
382
382
  }
383
383
 
384
384
  // src/tools/mesh-tools.ts
385
- var import_daemon_core = require("@adhdev/daemon-core");
386
- var SESSION_PROVIDER_METADATA_TTL_MS = 30 * 6e4;
387
- var meshSessionProviderMetadata = /* @__PURE__ */ new Map();
388
- function getSessionMetadata(key) {
389
- const entry = meshSessionProviderMetadata.get(key);
390
- if (!entry) return void 0;
391
- if (entry.expiresAt <= Date.now()) {
392
- meshSessionProviderMetadata.delete(key);
393
- return void 0;
394
- }
395
- return entry;
396
- }
397
- var ACTIVE_WORK_POLLING_BACKOFF_MS = 6e4;
398
- function buildActiveWorkPollingGuidance(summary, now = Date.now()) {
399
- if (!summary || summary.generatingCount <= 0) return void 0;
400
- return {
401
- activeGeneratingWork: true,
402
- generatingCount: summary.generatingCount,
403
- doNotPollBefore: new Date(now + ACTIVE_WORK_POLLING_BACKOFF_MS).toISOString(),
404
- eventSurface: "pendingCoordinatorEvents",
405
- nextRecommendedAction: "Wait for pendingCoordinatorEvents/completion events or an explicit user status request. If no terminal evidence appears and the user asks for status, make one bounded status check, then wait again.",
406
- message: "Do not repeatedly poll mesh_status/mesh_view_queue/mesh_read_chat while delegated work is generating; terminal ledger or completion evidence will be surfaced through pendingCoordinatorEvents when available."
407
- };
408
- }
385
+ var import_daemon_core2 = require("@adhdev/daemon-core");
386
+
387
+ // src/tools/mesh-tool-shared.ts
409
388
  function readString(value) {
410
389
  return typeof value === "string" && value.trim() ? value.trim() : void 0;
411
390
  }
412
- function summarizeTaskMessage(message) {
413
- const taskSummary = message.replace(/\s+/g, " ").trim();
414
- const taskTitle = taskSummary.length > 96 ? `${taskSummary.slice(0, 93)}...` : taskSummary;
415
- return { taskTitle: taskTitle || "(untitled task)", taskSummary };
416
- }
417
- function buildDirectTaskPayload(message, via, opts) {
418
- const descriptor = summarizeTaskMessage(message);
419
- return {
420
- source: "direct",
421
- via,
422
- taskId: opts.taskId,
423
- message,
424
- taskTitle: descriptor.taskTitle,
425
- taskSummary: descriptor.taskSummary,
426
- ...opts.taskMode ? { taskMode: opts.taskMode } : {},
427
- ...opts.providerType ? { providerType: opts.providerType } : {},
428
- ...opts.targetSessionId ? { targetSessionId: opts.targetSessionId } : {},
429
- ...opts.dispatchedToIdleSession !== void 0 ? { dispatchedToIdleSession: opts.dispatchedToIdleSession } : {}
430
- };
431
- }
432
- function findNode(mesh, nodeId) {
433
- const node = mesh.nodes.find((n) => (0, import_daemon_core.meshNodeIdMatches)(n, nodeId));
434
- if (!node) throw new Error(`Node '${nodeId}' is not a member of mesh '${mesh.name}'`);
435
- return node;
436
- }
437
- var DUPLICATE_DISPATCH_WINDOW_MS = 6e4;
438
- var STALE_ASSIGNED_QUEUE_MS = 30 * 6e4;
439
- var OLD_HISTORICAL_QUEUE_RECORD_MS = 7 * 24 * 60 * 6e4;
440
- var ACTIVE_QUEUE_STATUSES = /* @__PURE__ */ new Set(["pending", "assigned"]);
441
- var HISTORICAL_QUEUE_STATUSES = /* @__PURE__ */ new Set(["completed", "failed", "cancelled"]);
442
- async function refreshMeshFromDaemon(ctx) {
443
- try {
444
- const result = await ctx.transport.command("get_mesh", { meshId: ctx.mesh.id });
445
- if (!result?.success || !Array.isArray(result.mesh?.nodes)) return;
446
- const refreshedNodes = result.mesh.nodes.filter((n) => n?.id).map((n) => n);
447
- ctx.mesh.nodes.splice(0, ctx.mesh.nodes.length, ...refreshedNodes);
448
- ctx.mesh.updatedAt = result.mesh.updatedAt ?? ctx.mesh.updatedAt;
449
- } catch {
450
- }
391
+ function readNumeric(value, fallback = 0) {
392
+ const parsed = Number(value);
393
+ return Number.isFinite(parsed) ? parsed : fallback;
451
394
  }
452
- async function syncCoordinatorDaemonMeshCache(ctx) {
453
- if (!(ctx.transport instanceof IpcTransport)) return;
454
- try {
455
- await ctx.transport.command("get_mesh", {
456
- meshId: ctx.mesh.id,
457
- inlineMesh: ctx.mesh
458
- });
459
- } catch {
395
+ var LARGE_LEDGER_FIELD_KEYS = /* @__PURE__ */ new Set(["plan", "validationPlan", "suggestedConfig", "payload"]);
396
+ var LARGE_LEDGER_OBJECT_THRESHOLD = 800;
397
+ var LARGE_LEDGER_NESTED_BYTES_THRESHOLD = 2e3;
398
+ function summarizeLargeLedgerField(key, value) {
399
+ if (typeof value === "string") {
400
+ return value.length > 500 ? value.slice(0, 500) + "\u2026" : value;
460
401
  }
461
- }
462
- async function findNodeWithRefresh(ctx, nodeId) {
463
- const hit = ctx.mesh.nodes.find((n) => (0, import_daemon_core.meshNodeIdMatches)(n, nodeId));
464
- if (hit && !hit.isLocalWorktree) return hit;
465
- await refreshMeshFromDaemon(ctx);
466
- const refreshed = ctx.mesh.nodes.find((n) => (0, import_daemon_core.meshNodeIdMatches)(n, nodeId));
467
- if (!refreshed) throw new Error(`Node '${nodeId}' is not a member of mesh '${ctx.mesh.name}'`);
468
- return refreshed;
469
- }
470
- async function findOptionalNodeWithRefresh(ctx, nodeId) {
471
- const hit = ctx.mesh.nodes.find((n) => (0, import_daemon_core.meshNodeIdMatches)(n, nodeId));
472
- if (hit && !hit.isLocalWorktree) return hit;
473
- await refreshMeshFromDaemon(ctx);
474
- return ctx.mesh.nodes.find((n) => (0, import_daemon_core.meshNodeIdMatches)(n, nodeId)) ?? null;
475
- }
476
- function hasRecentDuplicateDispatch(ctx, args) {
477
- const now = Date.now();
478
- const normalizedMessage = args.message.trim();
479
- for (const task of (0, import_daemon_core.getQueue)(ctx.mesh.id)) {
480
- const timestamp = new Date(task.updatedAt || task.createdAt).getTime();
481
- if (!Number.isFinite(timestamp) || now - timestamp > DUPLICATE_DISPATCH_WINDOW_MS) continue;
482
- if (task.targetNodeId && task.targetNodeId !== args.node_id) continue;
483
- if (task.assignedNodeId && task.assignedNodeId !== args.node_id) continue;
484
- if (args.session_id && task.targetSessionId !== args.session_id && task.assignedSessionId !== args.session_id) continue;
485
- if (task.message?.trim() === normalizedMessage) {
486
- return { duplicate: true, entry: task, source: "queue" };
402
+ if (Array.isArray(value)) {
403
+ const serialized = JSON.stringify(value);
404
+ if (serialized && serialized.length > LARGE_LEDGER_OBJECT_THRESHOLD) {
405
+ return `[${key} summarized: ${value.length} items \u2014 use verbose=true or mesh_reconcile_ledger]`;
487
406
  }
407
+ return value;
488
408
  }
489
- const entries = (0, import_daemon_core.readLedgerEntries)(ctx.mesh.id, { tail: 200 });
490
- for (let i = entries.length - 1; i >= 0; i -= 1) {
491
- const entry = entries[i];
492
- const timestamp = new Date(entry.timestamp).getTime();
493
- if (Number.isFinite(timestamp) && now - timestamp > DUPLICATE_DISPATCH_WINDOW_MS) break;
494
- if (entry.kind !== "task_dispatched") continue;
495
- if (entry.nodeId !== args.node_id) continue;
496
- if (args.session_id && entry.sessionId !== args.session_id) continue;
497
- if (typeof entry.payload?.message !== "string") continue;
498
- if (entry.payload.message.trim() === normalizedMessage) {
499
- return { duplicate: true, entry, source: "ledger" };
409
+ if (value && typeof value === "object") {
410
+ const serialized = JSON.stringify(value);
411
+ if (serialized && serialized.length > LARGE_LEDGER_OBJECT_THRESHOLD) {
412
+ return `[${key} summarized: ${Object.keys(value).length} keys \u2014 use verbose=true or mesh_reconcile_ledger]`;
500
413
  }
414
+ return value;
501
415
  }
502
- return { duplicate: false };
416
+ return value;
503
417
  }
504
- function buildMissingNodeReadChatRecovery(ctx, args) {
505
- const entries = (0, import_daemon_core.readLedgerEntries)(ctx.mesh.id, { tail: 300 });
506
- const relatedEntries = entries.filter((entry) => entry.nodeId === args.node_id || entry.sessionId === args.session_id);
507
- const completedEntries = relatedEntries.filter((entry) => entry.kind === "task_completed");
508
- const lastDispatch = [...relatedEntries].reverse().find((entry) => entry.kind === "task_dispatched");
509
- const lastTerminal = [...relatedEntries].reverse().find((entry) => entry.kind === "task_completed" || entry.kind === "task_failed" || entry.kind === "task_stalled");
510
- const lastRemoved = [...relatedEntries].reverse().find((entry) => entry.kind === "node_removed");
511
- const lastLaunch = [...relatedEntries].reverse().find((entry) => entry.kind === "session_launched");
512
- const providerSessionId = args.provider_session_id || readString(lastTerminal?.payload?.providerSessionId) || readString(lastLaunch?.payload?.providerSessionId) || readString(lastDispatch?.payload?.providerSessionId);
513
- const finalSummary = readString(lastTerminal?.payload?.finalSummary) || readString(lastTerminal?.payload?.compactSummary) || readString(lastTerminal?.payload?.summary);
514
- const ledger = {
515
- taskCompletedFound: completedEntries.length > 0,
516
- nodeRemovedFound: !!lastRemoved,
517
- providerType: lastTerminal?.providerType || lastLaunch?.providerType || lastDispatch?.providerType,
518
- providerSessionId,
519
- nodeRemovedAt: lastRemoved?.timestamp,
520
- sessionCleanupMode: readString(lastRemoved?.payload?.sessionCleanupMode),
521
- readDebugLocator: readString(lastTerminal?.payload?.readDebugLocator) || readString(lastTerminal?.payload?.debugBundlePath)
522
- };
523
- if (finalSummary) {
524
- if (args.compact === true) {
525
- return {
526
- ...compactChatPayload({
527
- success: true,
528
- status: "idle",
529
- providerSessionId,
530
- summary: finalSummary,
531
- messages: [{ role: "assistant", content: finalSummary, isHistorical: true }]
532
- }, {
533
- nodeId: args.node_id,
534
- sessionId: args.session_id,
535
- limit: args.tail ?? 10
536
- }),
537
- recoveredFromLedger: true,
538
- ledger
539
- };
540
- }
541
- return {
542
- success: true,
543
- compact: false,
544
- recoveredFromLedger: true,
545
- nodeId: args.node_id,
546
- sessionId: args.session_id,
547
- summary: finalSummary,
548
- ledger,
549
- messages: [{ role: "assistant", content: finalSummary, isHistorical: true }]
550
- };
418
+ function elideLargeNestedValue(key, value) {
419
+ if (value === null || value === void 0) return value;
420
+ if (typeof value === "string") {
421
+ return value.length > 1e3 ? value.slice(0, 1e3) + "\u2026" : value;
551
422
  }
423
+ if (typeof value !== "object") return value;
424
+ const serialized = JSON.stringify(value);
425
+ const bytes = serialized ? serialized.length : 0;
426
+ if (bytes <= LARGE_LEDGER_NESTED_BYTES_THRESHOLD) return value;
552
427
  return {
553
- success: false,
554
- recoverable: true,
555
- code: "mesh_removed_node_transcript_unavailable",
556
- error: `Node '${args.node_id}' is not a current member of mesh '${ctx.mesh.name}'.`,
557
- nodeId: args.node_id,
558
- sessionId: args.session_id,
559
- providerSessionId,
560
- reason: "node_not_in_current_mesh_snapshot",
561
- ledger,
562
- completedSessionSeenInLedger: ledger.taskCompletedFound,
563
- lastDispatch: lastDispatch ? {
564
- timestamp: lastDispatch.timestamp,
565
- sessionId: lastDispatch.sessionId,
566
- providerType: lastDispatch.providerType,
567
- taskId: typeof lastDispatch.payload?.taskId === "string" ? lastDispatch.payload.taskId : void 0,
568
- messagePreview: typeof lastDispatch.payload?.message === "string" ? lastDispatch.payload.message.slice(0, 500) : void 0
569
- } : null,
570
- lastTerminalEvent: lastTerminal ? {
571
- kind: lastTerminal.kind,
572
- timestamp: lastTerminal.timestamp,
573
- sessionId: lastTerminal.sessionId,
574
- providerType: lastTerminal.providerType,
575
- taskId: typeof lastTerminal.payload?.taskId === "string" ? lastTerminal.payload.taskId : void 0,
576
- payload: lastTerminal.payload
577
- } : null,
578
- nextSteps: [
579
- providerSessionId ? `Retry mesh_read_chat with provider_session_id='${providerSessionId}' on a current live node for the same daemon if one exists.` : "If the node UI shows a provider transcript id, retry mesh_read_chat/mesh_read_debug with provider_session_id.",
580
- "Use mesh_read_debug with the provider_session_id or daemon-side debug bundle locator if available.",
581
- "Check mesh_task_history for task_completed and node_removed entries before redispatching; do not resend solely because transcript recovery failed.",
582
- "If this node was removed with stop_and_delete, the runtime transcript may be gone; rely on the ledger summary/locator or ask the operator for the saved UI output."
583
- ],
584
- recoveryHints: [
585
- "The worktree/node may have been removed or the mesh snapshot may be stale after task completion.",
586
- "If you have a provider_session_id, retry mesh_read_chat with that value while targeting a live node for the same daemon if available.",
587
- "Use mesh_read_debug with provider_session_id, or inspect the daemon/session-host history locator if the transcript has already been archived.",
588
- "Avoid redispatching the same task solely because read_chat could not recover the transcript; check task_history and git status first."
589
- ]
428
+ _elided: true,
429
+ _kind: key,
430
+ _bytes: bytes,
431
+ _hint: "full evidence via mesh_reconcile_ledger"
590
432
  };
591
433
  }
434
+
435
+ // src/tools/mesh-session-helpers.ts
592
436
  function readSessionRecordId(session) {
593
437
  return readString(session?.id) || readString(session?.sessionId) || readString(session?.session_id) || readString(session?.runtimeSessionId) || readString(session?.runtime_session_id) || readString(session?.instanceId) || readString(session?.instance_id);
594
438
  }
@@ -656,6 +500,36 @@ function collectNodeSessionIds(node) {
656
500
  sessionRecords.forEach((session) => addSessionRecord(sessions, session));
657
501
  return sessions;
658
502
  }
503
+ function unwrapCommandPayload(value) {
504
+ let current = value;
505
+ const seen = /* @__PURE__ */ new Set();
506
+ for (let depth = 0; depth < 8; depth += 1) {
507
+ if (!current || typeof current !== "object" || seen.has(current)) break;
508
+ seen.add(current);
509
+ const nested = current.result ?? current.payload;
510
+ if (!nested || typeof nested !== "object") break;
511
+ current = nested;
512
+ }
513
+ return current;
514
+ }
515
+ function isTerminalSessionRecord(session) {
516
+ const status = typeof session?.status === "string" ? session.status.toLowerCase() : "";
517
+ const lifecycle = typeof session?.lifecycle === "string" ? session.lifecycle.toLowerCase() : "";
518
+ const state = typeof session?.state === "string" ? session.state.toLowerCase() : "";
519
+ return [status, lifecycle, state].some((value) => ["stopped", "failed", "terminated", "exited", "closed"].includes(value));
520
+ }
521
+ function isIdleSessionRecord(session) {
522
+ if (isTerminalSessionRecord(session)) return false;
523
+ const status = typeof session?.status === "string" ? session.status.toLowerCase() : "";
524
+ const chatStatus = typeof session?.activeChat?.status === "string" ? session.activeChat.status.toLowerCase() : "";
525
+ return status === "idle" || chatStatus === "waiting_input";
526
+ }
527
+
528
+ // src/tools/mesh-queue-helpers.ts
529
+ var STALE_ASSIGNED_QUEUE_MS = 30 * 6e4;
530
+ var OLD_HISTORICAL_QUEUE_RECORD_MS = 7 * 24 * 60 * 6e4;
531
+ var ACTIVE_QUEUE_STATUSES = /* @__PURE__ */ new Set(["pending", "assigned"]);
532
+ var HISTORICAL_QUEUE_STATUSES = /* @__PURE__ */ new Set(["completed", "failed", "cancelled"]);
659
533
  function buildQueueLivenessIndex(mesh) {
660
534
  const nodeIds = /* @__PURE__ */ new Set();
661
535
  const nodeSessionIds = /* @__PURE__ */ new Map();
@@ -885,403 +759,105 @@ function annotateQueueStaleness(queue, mesh) {
885
759
  };
886
760
  });
887
761
  }
888
- function unwrapCommandPayload(value) {
889
- let current = value;
890
- const seen = /* @__PURE__ */ new Set();
891
- for (let depth = 0; depth < 8; depth += 1) {
892
- if (!current || typeof current !== "object" || seen.has(current)) break;
893
- seen.add(current);
894
- const nested = current.result ?? current.payload;
895
- if (!nested || typeof nested !== "object") break;
896
- current = nested;
897
- }
898
- return current;
899
- }
900
- function isDirectDispatchLedgerEntry(entry) {
901
- if (entry?.kind !== "task_dispatched") return false;
902
- const payload = entry.payload || {};
903
- const via = readString(payload.via);
904
- return payload.source === "direct" || via === "p2p_direct" || via === "local_direct" || via === "mesh_send_task";
905
- }
906
- function readMessageTimestampIso(message) {
907
- for (const value of [message?.timestamp, message?.createdAt, message?.created_at, message?.updatedAt, message?.time]) {
908
- if (typeof value === "number" && Number.isFinite(value)) {
909
- const ms = value > 1e10 ? value : value * 1e3;
910
- return new Date(ms).toISOString();
911
- }
912
- if (typeof value === "string" && value.trim()) {
913
- const ms = new Date(value.trim()).getTime();
914
- if (Number.isFinite(ms)) return new Date(ms).toISOString();
915
- }
762
+
763
+ // src/tools/mesh-compact.ts
764
+ function buildCompactGitSnapshot(status) {
765
+ if (!status || typeof status !== "object" || Array.isArray(status)) return void 0;
766
+ const slim = {};
767
+ const carry = [
768
+ "isGitRepo",
769
+ "branch",
770
+ "headCommit",
771
+ "upstream",
772
+ "upstreamStatus",
773
+ "ahead",
774
+ "behind",
775
+ "dirty",
776
+ "detached",
777
+ "submodules"
778
+ ];
779
+ for (const key of carry) {
780
+ if (status[key] !== void 0) slim[key] = status[key];
916
781
  }
917
- return void 0;
782
+ return slim;
918
783
  }
919
- function readFinalAssistantTranscriptEvidence(payload) {
920
- const rawMessages = Array.isArray(payload?.messages) ? payload.messages : [];
921
- const finalAssistant = [...rawMessages].reverse().filter(isCoordinatorVisibleMessage).find((message) => {
922
- const role = String(message?.role ?? "").toLowerCase();
923
- return (role === "assistant" || role === "agent") && messageContent(message).trim();
924
- });
925
- const finalSummary = messageContent(finalAssistant).trim() || (typeof payload?.summary === "string" && payload.summary.trim() ? payload.summary.trim() : void 0);
784
+ function summarizeCompactSubmodules(submodules) {
785
+ if (!Array.isArray(submodules) || submodules.length === 0) return void 0;
786
+ const outOfSync = submodules.filter((s) => s?.outOfSync).map((s) => s?.path).filter(Boolean);
926
787
  return {
927
- finalSummary,
928
- transcriptMessageAt: finalAssistant ? readMessageTimestampIso(finalAssistant) : void 0
788
+ count: submodules.length,
789
+ ...outOfSync.length > 0 ? { outOfSyncPaths: outOfSync } : {}
929
790
  };
930
791
  }
931
- function findNodeSession(nodes, nodeId, sessionId) {
932
- if (!nodeId || !sessionId) return {};
933
- const node = nodes.find((candidate) => (0, import_daemon_core.meshNodeIdMatches)(candidate, nodeId));
934
- if (!node) return {};
935
- const sessions = Array.isArray(node.sessions) ? node.sessions : [];
936
- const session = sessions.find((candidate) => readSessionRecordId(candidate) === sessionId);
937
- return { node, session };
938
- }
939
- function buildDirectDispatchReconciliationCandidates(directDispatches, ledgerEntries) {
940
- const candidates = [];
941
- const seenTaskIds = /* @__PURE__ */ new Set();
942
- for (const dispatch of directDispatches || []) {
943
- const taskId = readString(dispatch?.taskId);
944
- if (!taskId || seenTaskIds.has(taskId)) continue;
945
- seenTaskIds.add(taskId);
946
- candidates.push(dispatch);
947
- }
948
- for (const entry of ledgerEntries || []) {
949
- if (!isDirectDispatchLedgerEntry(entry)) continue;
950
- const taskId = readString(entry.payload?.taskId);
951
- if (!taskId || seenTaskIds.has(taskId)) continue;
952
- seenTaskIds.add(taskId);
953
- candidates.push({
954
- taskId,
955
- nodeId: entry.nodeId,
956
- sessionId: entry.sessionId,
957
- providerType: entry.providerType || readString(entry.payload?.providerType),
958
- message: readString(entry.payload?.message),
959
- dispatchedAt: entry.timestamp,
960
- via: readString(entry.payload?.via)
961
- });
962
- }
963
- return candidates;
964
- }
965
- async function reconcileDirectDispatchesFromTranscriptEvidence(ctx, liveNodes, directDispatches, ledgerEntries) {
966
- let attempted = 0;
967
- let reconciled = 0;
968
- let skipped = 0;
969
- const candidates = buildDirectDispatchReconciliationCandidates(directDispatches, ledgerEntries);
970
- for (const dispatch of candidates) {
971
- const taskId = readString(dispatch?.taskId);
972
- const nodeId = readString(dispatch?.nodeId);
973
- const sessionId = readString(dispatch?.sessionId);
974
- if (!taskId || !nodeId || !sessionId) {
975
- skipped += 1;
976
- continue;
977
- }
978
- const { session } = findNodeSession(liveNodes, nodeId, sessionId);
979
- if (!session || !isIdleSessionRecord(session)) {
980
- skipped += 1;
981
- continue;
982
- }
983
- const node = await findOptionalNodeWithRefresh(ctx, nodeId).catch(() => null);
984
- if (!node) {
985
- skipped += 1;
986
- continue;
987
- }
988
- const providerType = readString(dispatch?.providerType) || resolveSessionProviderType(session);
989
- const providerSessionId = readString(session?.providerSessionId) || readString(session?.activeChat?.providerSessionId) || readString(session?.settings?.providerSessionId) || resolveMeshSessionProviderMetadata(ctx, nodeId, sessionId)?.providerSessionId;
990
- attempted += 1;
991
- try {
992
- const readResult = await commandForNode(ctx, node, "read_chat", {
993
- sessionId,
994
- targetSessionId: sessionId,
995
- workspace: node.workspace,
996
- ...providerType ? { agentType: providerType, providerType } : {},
997
- ...providerSessionId ? { providerSessionId } : {},
998
- tailLimit: 10
999
- });
1000
- const payload = unwrapCommandPayload(readResult);
1001
- if (payload?.success === false) continue;
1002
- const evidence = readFinalAssistantTranscriptEvidence(payload);
1003
- if (!evidence.finalSummary) continue;
1004
- const result = (0, import_daemon_core.reconcileDirectDispatchCompletionFromTranscript)({
1005
- meshId: ctx.mesh.id,
1006
- nodeId,
1007
- sessionId,
1008
- providerType,
1009
- providerSessionId: readString(payload?.providerSessionId) || providerSessionId,
1010
- taskId,
1011
- finalSummary: evidence.finalSummary,
1012
- transcriptMessageAt: evidence.transcriptMessageAt,
1013
- targetCoordinatorDaemonId: ctx.localDaemonId,
1014
- source: "mcp_mesh_status_transcript_reconciliation"
1015
- });
1016
- if (result.reconciled) reconciled += 1;
1017
- } catch {
1018
- skipped += 1;
792
+ var MESH_COMPACT_PRESERVED_MARKER_FIELDS = ["dataFreshness"];
793
+ function compactMeshStatusNode(entry) {
794
+ if (!entry || typeof entry !== "object") return entry;
795
+ const next = { ...entry };
796
+ if (next.git !== void 0) {
797
+ const slimGit = buildCompactGitSnapshot(next.git);
798
+ if (slimGit) {
799
+ if (slimGit.submodules !== void 0) {
800
+ const subSummary = summarizeCompactSubmodules(slimGit.submodules);
801
+ if (subSummary) slimGit.submodules = subSummary;
802
+ else delete slimGit.submodules;
803
+ }
804
+ next.git = slimGit;
1019
805
  }
1020
806
  }
1021
- return { attempted, reconciled, skipped };
1022
- }
1023
- async function triggerMeshQueueAndReport(ctx) {
1024
- try {
1025
- const raw = await ctx.transport.command("trigger_mesh_queue", { meshId: ctx.mesh.id });
1026
- const payload = unwrapCommandPayload(raw);
1027
- const trigger = payload?.trigger && typeof payload.trigger === "object" ? payload.trigger : payload;
1028
- return trigger && typeof trigger === "object" ? trigger : { success: true };
1029
- } catch (e) {
1030
- return {
1031
- success: false,
1032
- error: e?.message || String(e)
807
+ if (next.machine && typeof next.machine === "object") {
808
+ const m = next.machine;
809
+ next.machine = {
810
+ daemonId: m.daemonId,
811
+ machineId: m.machineId,
812
+ hostname: m.hostname,
813
+ displayName: m.displayName,
814
+ sameMachine: m.sameMachine,
815
+ locality: m.locality
1033
816
  };
1034
817
  }
1035
- }
1036
- function buildQueueTriggerGuidance(queueTrigger) {
1037
- if (!queueTrigger || queueTrigger.claimed === true) return void 0;
1038
- if (queueTrigger.success === false) {
1039
- return {
1040
- queueClaimed: false,
1041
- queueDispatchState: "trigger_failed",
1042
- nextAction: "Do not assume the queued task is running. Check mesh_view_queue and daemon connectivity before redispatching."
1043
- };
818
+ if (typeof next.submoduleWarning === "string") {
819
+ next.submodulesOutOfSync = true;
820
+ delete next.submoduleWarning;
1044
821
  }
1045
- if (queueTrigger.autoLaunchPending === true) {
1046
- return {
1047
- queueClaimed: false,
1048
- queueDispatchState: "pending_waiting_for_autolaunch",
1049
- nextAction: "A worker session was just auto-launched for this task and is booting; it will claim the task shortly. Wait for it to claim \u2014 do NOT launch another session. Use mesh_view_queue to confirm the assignment lands."
822
+ if (next.staleDaemonBuild && typeof next.staleDaemonBuild === "object") {
823
+ const b = next.staleDaemonBuild;
824
+ next.staleDaemonBuild = {
825
+ scope: b.scope,
826
+ isDaemonAffecting: b.isDaemonAffecting !== false,
827
+ seeStaleDaemonBuilds: true
1050
828
  };
1051
829
  }
1052
- if (queueTrigger.noIdleMeshSessionAvailable === true) {
1053
- return {
1054
- queueClaimed: false,
1055
- queueDispatchState: "pending_no_idle_mesh_session",
1056
- nextAction: "The task is queued but not running. Launch a managed worker with mesh_launch_session, or wait for a delegated session to become ready and trigger the queue again."
1057
- };
830
+ delete next.capabilityTagsByProvider;
831
+ const elideSkip = /* @__PURE__ */ new Set(["git", "machine", "branchConvergence", "staleDaemonBuild", "sessions", ...MESH_COMPACT_PRESERVED_MARKER_FIELDS]);
832
+ for (const k of Object.keys(next)) {
833
+ if (elideSkip.has(k)) continue;
834
+ next[k] = elideLargeNestedValue(k, next[k]);
1058
835
  }
1059
- return {
1060
- queueClaimed: false,
1061
- queueDispatchState: "pending_or_waiting_for_ready",
1062
- nextAction: "The task is queued but this trigger did not claim it. Use mesh_view_queue for the current active-work source of truth before retrying."
1063
- };
836
+ return next;
1064
837
  }
1065
- function isTerminalSessionRecord(session) {
1066
- const status = typeof session?.status === "string" ? session.status.toLowerCase() : "";
1067
- const lifecycle = typeof session?.lifecycle === "string" ? session.lifecycle.toLowerCase() : "";
1068
- const state = typeof session?.state === "string" ? session.state.toLowerCase() : "";
1069
- return [status, lifecycle, state].some((value) => ["stopped", "failed", "terminated", "exited", "closed"].includes(value));
838
+ function compactNodeSeverity(entry) {
839
+ if (!entry || typeof entry !== "object") return 0;
840
+ if (entry.error || entry.health && entry.health !== "online" && entry.health !== "dirty") return 5;
841
+ if (entry.launchReady === false) return 4;
842
+ if (entry.isDirty === true || entry.health === "dirty") return 3;
843
+ if (entry.branchConvergence?.needsConvergence === true) return 2;
844
+ if (entry.staleDaemonBuild || entry.submodulesOutOfSync || entry.recoveryHints) return 1;
845
+ return 0;
1070
846
  }
1071
- function isIdleSessionRecord(session) {
1072
- if (isTerminalSessionRecord(session)) return false;
1073
- const status = typeof session?.status === "string" ? session.status.toLowerCase() : "";
1074
- const chatStatus = typeof session?.activeChat?.status === "string" ? session.activeChat.status.toLowerCase() : "";
1075
- return status === "idle" || chatStatus === "waiting_input";
1076
- }
1077
- function isMeshOwnedDelegateSession(session, meshId, nodeId) {
1078
- const settings = session?.settings;
1079
- const sessionMeshId = typeof settings?.meshNodeFor === "string" ? settings.meshNodeFor.trim() : "";
1080
- const sessionNodeId = typeof settings?.meshNodeId === "string" ? settings.meshNodeId.trim() : "";
1081
- if (sessionMeshId) {
1082
- if (sessionMeshId !== meshId) return false;
1083
- return !sessionNodeId || sessionNodeId === nodeId;
1084
- }
1085
- const coordinatorOwned = settings?.launchedByCoordinator === true || Boolean(readString(settings?.meshCoordinatorDaemonId));
1086
- if (!coordinatorOwned) return false;
1087
- const lastNodeId = readString(settings?.meshLastNodeId);
1088
- if (lastNodeId) return lastNodeId === nodeId;
1089
- return true;
1090
- }
1091
- function hasRemoteRelayMetadata(session) {
1092
- return Boolean(
1093
- readString(session?.settings?.meshCoordinatorDaemonId) || readString(session?.meta?.meshCoordinatorDaemonId) || readString(session?.metadata?.meshCoordinatorDaemonId) || readString(session?.meshCoordinatorDaemonId)
1094
- );
1095
- }
1096
- function classifyRemoteDelegateRelaySafety(session, meshId, nodeId, coordinatorDaemonId) {
1097
- if (!isMeshOwnedDelegateSession(session, meshId, nodeId)) return "unsafe_alias";
1098
- if (hasRemoteRelayMetadata(session)) return "safe";
1099
- return coordinatorDaemonId ? "self_heal" : "missing_anchor";
1100
- }
1101
- function chooseDispatchableSession(sessions, providerType, meshId, nodeId, coordinatorDaemonId) {
1102
- const live = sessions.filter((session) => !isTerminalSessionRecord(session));
1103
- const matchingProvider = (session) => !providerType || session?.providerType === providerType || session?.cliType === providerType;
1104
- const meshSessions = live.filter((session) => {
1105
- const safety = classifyRemoteDelegateRelaySafety(session, meshId, nodeId, coordinatorDaemonId);
1106
- return safety === "safe" || safety === "self_heal";
1107
- });
1108
- return meshSessions.find((session) => isIdleSessionRecord(session) && matchingProvider(session)) || void 0;
1109
- }
1110
- function buildRelayUnsafeRemoteSessionFailure(ctx, node, sessionId, providerType) {
1111
- return {
1112
- success: false,
1113
- recoverable: true,
1114
- code: "mesh_delegate_session_missing_relay_metadata",
1115
- reason: "mesh_delegate_session_missing_relay_metadata",
1116
- transport: "mesh_transport",
1117
- retryRecommended: true,
1118
- meshId: ctx.mesh.id,
1119
- nodeId: node.id,
1120
- daemonId: node.daemonId,
1121
- workspace: node.workspace,
1122
- sessionId,
1123
- unsafeTranscriptAlias: true,
1124
- ...providerType ? { resolvedProviderType: providerType } : {},
1125
- error: `Remote session '${sessionId}' is not relay-safe for mesh '${ctx.mesh.id}': missing meshNodeFor/meshCoordinatorDaemonId metadata, so completion events would not reach the coordinator ledger. This session may be the coordinator itself or an unrelated session (unsafe_transcript_alias risk).`,
1126
- nextAction: `Launch a fresh relay-safe session with mesh_launch_session(node_id: '${node.id}'${providerType ? `, type: '${providerType}'` : ""}) or dispatch without session_id so Repo Mesh can choose a valid delegate session.`,
1127
- noFallbackReason: "Blindly reusing a remote session without mesh relay metadata would silently drop task_completed / generating_completed events."
1128
- };
1129
- }
1130
- function buildMissingCoordinatorDaemonIdFailure(ctx, node, providerType) {
1131
- return {
1132
- success: false,
1133
- recoverable: true,
1134
- code: "mesh_coordinator_daemon_unknown",
1135
- reason: "mesh_coordinator_daemon_unknown",
1136
- transport: "mesh_transport",
1137
- retryRecommended: true,
1138
- meshId: ctx.mesh.id,
1139
- nodeId: node.id,
1140
- daemonId: node.daemonId,
1141
- workspace: node.workspace,
1142
- ...providerType ? { resolvedProviderType: providerType } : {},
1143
- error: `Cannot launch a remote mesh delegate for node '${node.id}': coordinator daemon identity is unavailable, so the worker would be unable to relay completion events back to the coordinator.`,
1144
- nextAction: "Retry after the coordinator daemon identity is available (for example from an attached daemon-backed MCP session) so meshCoordinatorDaemonId can be stamped on the worker session.",
1145
- noFallbackReason: "Launching without meshCoordinatorDaemonId would create a worker session that can finish work but cannot emit task_completed / generating_completed back to the coordinator."
1146
- };
1147
- }
1148
- function findNestedPayload(value, predicate) {
1149
- const seen = /* @__PURE__ */ new Set();
1150
- const stack = [{ payload: value, depth: 0 }];
1151
- while (stack.length) {
1152
- const { payload, depth } = stack.pop();
1153
- if (predicate(payload)) return payload;
1154
- if (!payload || typeof payload !== "object" || seen.has(payload) || depth >= 8) continue;
1155
- seen.add(payload);
1156
- for (const key of ["payload", "result"]) {
1157
- if (key in payload) stack.push({ payload: payload[key], depth: depth + 1 });
1158
- }
1159
- }
1160
- return value;
1161
- }
1162
- function extractCloneNodePayload(value) {
1163
- return findNestedPayload(value, (payload) => Boolean(payload?.node?.id));
1164
- }
1165
- function extractGitStatus(value) {
1166
- const payload = unwrapCommandPayload(value);
1167
- return payload?.status ?? value?.status ?? payload;
1168
- }
1169
- function extractGitDiff(value) {
1170
- const payload = unwrapCommandPayload(value);
1171
- return payload?.diffSummary ?? payload?.diff ?? value?.diffSummary ?? value?.diff ?? payload;
1172
- }
1173
- function extractSubmodules(value, ignorePaths) {
1174
- const payload = unwrapCommandPayload(value);
1175
- const subs = payload?.status?.submodules ?? payload?.submodules ?? value?.status?.submodules ?? value?.submodules;
1176
- if (!Array.isArray(subs)) return void 0;
1177
- if (ignorePaths.length === 0) return subs;
1178
- const ignoreSet = new Set(ignorePaths);
1179
- return subs.filter((s) => s?.path && !ignoreSet.has(s.path));
1180
- }
1181
- function assignFullGitSnapshot(entry, status) {
1182
- if (!status || typeof status !== "object" || Array.isArray(status)) return;
1183
- entry.git = status;
1184
- }
1185
- function buildCompactGitSnapshot(status) {
1186
- if (!status || typeof status !== "object" || Array.isArray(status)) return void 0;
1187
- const slim = {};
1188
- const carry = [
1189
- "isGitRepo",
1190
- "branch",
1191
- "headCommit",
1192
- "upstream",
1193
- "upstreamStatus",
1194
- "ahead",
1195
- "behind",
1196
- "dirty",
1197
- "detached",
1198
- "submodules"
1199
- ];
1200
- for (const key of carry) {
1201
- if (status[key] !== void 0) slim[key] = status[key];
1202
- }
1203
- return slim;
1204
- }
1205
- function summarizeCompactSubmodules(submodules) {
1206
- if (!Array.isArray(submodules) || submodules.length === 0) return void 0;
1207
- const outOfSync = submodules.filter((s) => s?.outOfSync).map((s) => s?.path).filter(Boolean);
1208
- return {
1209
- count: submodules.length,
1210
- ...outOfSync.length > 0 ? { outOfSyncPaths: outOfSync } : {}
1211
- };
1212
- }
1213
- var MESH_COMPACT_PRESERVED_MARKER_FIELDS = ["dataFreshness"];
1214
- function compactMeshStatusNode(entry) {
1215
- if (!entry || typeof entry !== "object") return entry;
1216
- const next = { ...entry };
1217
- if (next.git !== void 0) {
1218
- const slimGit = buildCompactGitSnapshot(next.git);
1219
- if (slimGit) {
1220
- if (slimGit.submodules !== void 0) {
1221
- const subSummary = summarizeCompactSubmodules(slimGit.submodules);
1222
- if (subSummary) slimGit.submodules = subSummary;
1223
- else delete slimGit.submodules;
1224
- }
1225
- next.git = slimGit;
1226
- }
1227
- }
1228
- if (next.machine && typeof next.machine === "object") {
1229
- const m = next.machine;
1230
- next.machine = {
1231
- daemonId: m.daemonId,
1232
- machineId: m.machineId,
1233
- hostname: m.hostname,
1234
- displayName: m.displayName,
1235
- sameMachine: m.sameMachine,
1236
- locality: m.locality
1237
- };
1238
- }
1239
- if (typeof next.submoduleWarning === "string") {
1240
- next.submodulesOutOfSync = true;
1241
- delete next.submoduleWarning;
1242
- }
1243
- if (next.staleDaemonBuild && typeof next.staleDaemonBuild === "object") {
1244
- const b = next.staleDaemonBuild;
1245
- next.staleDaemonBuild = {
1246
- scope: b.scope,
1247
- isDaemonAffecting: b.isDaemonAffecting !== false,
1248
- seeStaleDaemonBuilds: true
1249
- };
1250
- }
1251
- delete next.capabilityTagsByProvider;
1252
- const elideSkip = /* @__PURE__ */ new Set(["git", "machine", "branchConvergence", "staleDaemonBuild", "sessions", ...MESH_COMPACT_PRESERVED_MARKER_FIELDS]);
1253
- for (const k of Object.keys(next)) {
1254
- if (elideSkip.has(k)) continue;
1255
- next[k] = elideLargeNestedValue(k, next[k]);
1256
- }
1257
- return next;
1258
- }
1259
- var COMPACT_DETAILED_NODES_BYTE_BUDGET = 9e3;
1260
- var COMPACT_NODES_TOTAL_BYTE_BUDGET = 13e3;
1261
- var COMPACT_MISSIONS_BYTE_BUDGET = 6e3;
1262
- function compactNodeSeverity(entry) {
1263
- if (!entry || typeof entry !== "object") return 0;
1264
- if (entry.error || entry.health && entry.health !== "online" && entry.health !== "dirty") return 5;
1265
- if (entry.launchReady === false) return 4;
1266
- if (entry.isDirty === true || entry.health === "dirty") return 3;
1267
- if (entry.branchConvergence?.needsConvergence === true) return 2;
1268
- if (entry.staleDaemonBuild || entry.submodulesOutOfSync || entry.recoveryHints) return 1;
1269
- return 0;
1270
- }
1271
- function isNoteworthyCompactNode(entry) {
1272
- if (!entry || typeof entry !== "object") return true;
1273
- if (entry.health && entry.health !== "online") return true;
1274
- if (entry.isDirty === true) return true;
1275
- if (entry.error) return true;
1276
- if (entry.launchReady === false) return true;
1277
- if (entry.staleDaemonBuild) return true;
1278
- if (entry.submoduleWarning || entry.submodulesOutOfSync) return true;
1279
- if (entry.recoveryHints) return true;
1280
- if (Array.isArray(entry.nextStepHints) && entry.nextStepHints.length > 0) return true;
1281
- if (entry.branchConvergence?.needsConvergence === true) return true;
1282
- const sessionCount = Array.isArray(entry.sessions) ? entry.sessions.length : entry.sessionSummary?.total ?? 0;
1283
- if (sessionCount > 0) return true;
1284
- return false;
847
+ function isNoteworthyCompactNode(entry) {
848
+ if (!entry || typeof entry !== "object") return true;
849
+ if (entry.health && entry.health !== "online") return true;
850
+ if (entry.isDirty === true) return true;
851
+ if (entry.error) return true;
852
+ if (entry.launchReady === false) return true;
853
+ if (entry.staleDaemonBuild) return true;
854
+ if (entry.submoduleWarning || entry.submodulesOutOfSync) return true;
855
+ if (entry.recoveryHints) return true;
856
+ if (Array.isArray(entry.nextStepHints) && entry.nextStepHints.length > 0) return true;
857
+ if (entry.branchConvergence?.needsConvergence === true) return true;
858
+ const sessionCount = Array.isArray(entry.sessions) ? entry.sessions.length : entry.sessionSummary?.total ?? 0;
859
+ if (sessionCount > 0) return true;
860
+ return false;
1285
861
  }
1286
862
  function minimalCompactNode(entry) {
1287
863
  if (!entry || typeof entry !== "object") return entry;
@@ -1335,284 +911,26 @@ function summarizeNodeSessions(sessions) {
1335
911
  }
1336
912
  return summary;
1337
913
  }
1338
- function extractLaunchPayload(value) {
1339
- return findNestedPayload(value, (payload) => Boolean(payload?.sessionId || payload?.id || payload?.runtimeSessionId));
1340
- }
1341
- function classifyMeshLaunchFailure(error) {
1342
- const message = error instanceof Error ? error.message : String(error || "launch failed");
1343
- const lower = message.toLowerCase();
1344
- const p2pClassification = (0, import_daemon_core.classifyP2pRelayFailure)(error, { command: "launch_cli" });
1345
- if (p2pClassification.recoverable) {
1346
- return p2pClassification;
914
+
915
+ // src/tools/mesh-node-identity.ts
916
+ var import_daemon_core = require("@adhdev/daemon-core");
917
+ function resolveCoordinatorNode(ctx) {
918
+ const preferredNodeId = typeof ctx.mesh.coordinator?.preferredNodeId === "string" ? ctx.mesh.coordinator.preferredNodeId.trim() : "";
919
+ if (preferredNodeId) {
920
+ const preferred = ctx.mesh.nodes.find((n) => n.id === preferredNodeId && typeof n.daemonId === "string" && n.daemonId.trim());
921
+ if (preferred) return preferred;
1347
922
  }
1348
- if (lower.includes("cannot connect to daemon ipc") || lower.includes("daemon ipc command")) {
1349
- return {
1350
- code: "local_ipc_unavailable",
1351
- reason: "local_daemon_ipc_unavailable",
1352
- transport: "local_ipc",
1353
- recoverable: true,
1354
- retryRecommended: true,
1355
- nextAction: "Check the local daemon IPC connection, then retry mesh_launch_session once after the daemon is reachable."
1356
- };
923
+ if (ctx.localMachineId) {
924
+ const byMachine = ctx.mesh.nodes.find((n) => readNodeMachineId(n) === ctx.localMachineId);
925
+ if (byMachine) return byMachine;
1357
926
  }
1358
- if (lower.includes("timed out") || lower.includes("timeout")) {
1359
- return {
1360
- code: "mesh_transport_timeout",
1361
- reason: "mesh_transport_timeout",
1362
- transport: "mesh_transport",
1363
- recoverable: true,
1364
- retryRecommended: true,
1365
- nextAction: "Check mesh transport health, then do one bounded retry before requeueing or relaunching the task."
1366
- };
927
+ if (ctx.localDaemonId) {
928
+ return ctx.mesh.nodes.find((n) => readNodeDaemonId(n) === ctx.localDaemonId);
1367
929
  }
1368
- return {
1369
- code: "mesh_launch_failed",
1370
- reason: "provider_launch_failed",
1371
- transport: "mesh_transport",
1372
- recoverable: false,
1373
- retryRecommended: false,
1374
- nextAction: "Inspect the provider launch error and fix the underlying provider/configuration issue before retrying."
1375
- };
930
+ return void 0;
1376
931
  }
1377
- function buildWorktreeCleanupHint(node) {
1378
- if (!node.isLocalWorktree) return void 0;
1379
- return {
1380
- tool: "mesh_remove_node",
1381
- args: { node_id: node.id, session_cleanup_mode: "preserve" },
1382
- hint: `If the worktree is no longer needed, remove the orphan worktree node with mesh_remove_node(node_id: "${node.id}").`
1383
- };
1384
- }
1385
- function buildRecoverableLaunchFailure(ctx, node, providerType, error) {
1386
- const message = error instanceof Error ? error.message : String(error || "launch failed");
1387
- const classified = classifyMeshLaunchFailure(error);
1388
- const cleanup = buildWorktreeCleanupHint(node);
1389
- return {
1390
- success: false,
1391
- recoverable: classified.recoverable,
1392
- code: classified.code,
1393
- reason: classified.reason,
1394
- transport: classified.transport,
1395
- retryRecommended: classified.retryRecommended,
1396
- nextAction: classified.nextAction,
1397
- ...classified.noFallbackReason ? { noFallbackReason: classified.noFallbackReason } : {},
1398
- error: message,
1399
- meshId: ctx.mesh.id,
1400
- nodeId: node.id,
1401
- daemonId: node.daemonId,
1402
- workspace: node.workspace,
1403
- isLocalWorktree: node.isLocalWorktree === true,
1404
- worktreeBranch: node.worktreeBranch,
1405
- clonedFromNodeId: node.clonedFromNodeId,
1406
- ...providerType ? { resolvedProviderType: providerType } : {},
1407
- retryHint: `Retry mesh_launch_session(node_id: "${node.id}"${providerType ? `, type: "${providerType}"` : ""}) after daemon mesh transport/P2P is healthy.`,
1408
- ...cleanup ? { cleanup } : {},
1409
- nextStepHints: [
1410
- `Retry mesh_launch_session(node_id: "${node.id}"${providerType ? `, type: "${providerType}"` : ""}) after checking daemon/P2P health.`,
1411
- ...cleanup ? [`Cleanup orphan worktree node with mesh_remove_node(node_id: "${node.id}") if retry is not desired.`] : [],
1412
- "Run mesh_status to see the degraded reason and recovery hints before redispatching work."
1413
- ]
1414
- };
1415
- }
1416
- function recordRecoverableLaunchFailure(ctx, node, providerType, error) {
1417
- const failure = buildRecoverableLaunchFailure(ctx, node, providerType, error);
1418
- try {
1419
- (0, import_daemon_core.appendLedgerEntry)(ctx.mesh.id, {
1420
- kind: "recovery_attempted",
1421
- nodeId: node.id,
1422
- providerType,
1423
- payload: {
1424
- event: "session_launch_failed",
1425
- ...failure
1426
- }
1427
- });
1428
- } catch {
1429
- }
1430
- return failure;
1431
- }
1432
- function getLatestActiveLaunchFailure(meshId, nodeId) {
1433
- const entries = (0, import_daemon_core.readLedgerEntries)(meshId, { tail: 200 });
1434
- for (let i = entries.length - 1; i >= 0; i -= 1) {
1435
- const entry = entries[i];
1436
- if (entry.nodeId !== nodeId) continue;
1437
- if (entry.kind === "session_launched" || entry.kind === "node_removed") return null;
1438
- if (entry.kind === "recovery_attempted" && entry.payload?.event === "session_launch_failed") {
1439
- return { timestamp: entry.timestamp, ...entry.payload };
1440
- }
1441
- }
1442
- return null;
1443
- }
1444
- function buildCoordinatorP2pRelayFailure(error, context) {
1445
- const payload = (0, import_daemon_core.buildP2pRelayFailurePayload)(error, {
1446
- command: context.command,
1447
- targetDaemonId: context.targetDaemonId
1448
- });
1449
- return {
1450
- ...payload,
1451
- ...context.nodeId ? { nodeId: context.nodeId } : {},
1452
- ...context.sessionId ? { sessionId: context.sessionId } : {},
1453
- retryHint: payload.retryRecommended ? payload.nextAction : "Do not retry as a P2P transport recovery; inspect the command/provider error first."
1454
- };
1455
- }
1456
- async function ipcDispatchToRemoteAgent(ctx, node, args) {
1457
- const transport = ctx.transport;
1458
- const daemonId = node.daemonId;
1459
- const dispatchCoordinatorDaemonId = readString(args.meshContext?.coordinatorDaemonId) || "";
1460
- let sessionId = args.session_id?.trim() || "";
1461
- const providerPriorityList = Array.isArray(node.policy?.providerPriority) ? node.policy.providerPriority : [];
1462
- let resolvedProviderType = args.providerType?.trim() || providerPriorityList[0] || "";
1463
- if (sessionId && args.verifiedSession) {
1464
- const explicitSession = args.verifiedSession;
1465
- const relaySafety = classifyRemoteDelegateRelaySafety(explicitSession, ctx.mesh.id, node.id, dispatchCoordinatorDaemonId);
1466
- if (relaySafety === "unsafe_alias") {
1467
- return buildRelayUnsafeRemoteSessionFailure(
1468
- ctx,
1469
- node,
1470
- sessionId,
1471
- resolvedProviderType || resolveSessionProviderType(explicitSession) || void 0
1472
- );
1473
- }
1474
- if (relaySafety === "missing_anchor") {
1475
- return buildMissingCoordinatorDaemonIdFailure(
1476
- ctx,
1477
- node,
1478
- resolvedProviderType || resolveSessionProviderType(explicitSession) || void 0
1479
- );
1480
- }
1481
- if (!resolvedProviderType) {
1482
- resolvedProviderType = resolveSessionProviderType(explicitSession);
1483
- }
1484
- } else if (!sessionId || args.session_id) {
1485
- try {
1486
- const relayResult = await transport.meshCommand(daemonId, "get_status_metadata", {});
1487
- const sessions = extractStatusMetadataSessions(relayResult);
1488
- if (sessionId) {
1489
- const explicitSession = sessions.find((session) => readSessionRecordId(session) === sessionId);
1490
- if (!explicitSession) {
1491
- return {
1492
- success: false,
1493
- recoverable: true,
1494
- code: "mesh_target_session_not_found",
1495
- reason: "mesh_target_session_not_found",
1496
- transport: "mesh_transport",
1497
- retryRecommended: true,
1498
- meshId: ctx.mesh.id,
1499
- nodeId: node.id,
1500
- daemonId,
1501
- workspace: node.workspace,
1502
- sessionId,
1503
- ...resolvedProviderType ? { resolvedProviderType } : {},
1504
- error: `Remote session '${sessionId}' is not present in the live status for node '${node.id}'.`,
1505
- nextAction: `Launch a fresh session with mesh_launch_session(node_id: '${node.id}'${resolvedProviderType ? `, type: '${resolvedProviderType}'` : ""}) or retry without session_id so Repo Mesh can target a live delegate session.`
1506
- };
1507
- }
1508
- const relaySafety = classifyRemoteDelegateRelaySafety(explicitSession, ctx.mesh.id, node.id, dispatchCoordinatorDaemonId);
1509
- if (relaySafety === "unsafe_alias") {
1510
- return buildRelayUnsafeRemoteSessionFailure(
1511
- ctx,
1512
- node,
1513
- sessionId,
1514
- resolvedProviderType || resolveSessionProviderType(explicitSession) || void 0
1515
- );
1516
- }
1517
- if (relaySafety === "missing_anchor") {
1518
- return buildMissingCoordinatorDaemonIdFailure(
1519
- ctx,
1520
- node,
1521
- resolvedProviderType || resolveSessionProviderType(explicitSession) || void 0
1522
- );
1523
- }
1524
- if (!resolvedProviderType) {
1525
- resolvedProviderType = resolveSessionProviderType(explicitSession);
1526
- }
1527
- } else {
1528
- const targetSession = chooseDispatchableSession(sessions, resolvedProviderType, ctx.mesh.id, node.id, dispatchCoordinatorDaemonId);
1529
- if (targetSession?.id || targetSession?.sessionId) {
1530
- sessionId = targetSession.id || targetSession.sessionId;
1531
- if (!resolvedProviderType) {
1532
- resolvedProviderType = resolveSessionProviderType(targetSession);
1533
- }
1534
- }
1535
- }
1536
- } catch (e) {
1537
- if (sessionId) {
1538
- return {
1539
- ...buildCoordinatorP2pRelayFailure(e, {
1540
- command: "get_status_metadata",
1541
- targetDaemonId: daemonId,
1542
- nodeId: node.id,
1543
- sessionId
1544
- }),
1545
- success: false,
1546
- error: `Cannot verify remote session '${sessionId}' before dispatch: ${e?.message || String(e)}`
1547
- };
1548
- }
1549
- }
1550
- }
1551
- if (!resolvedProviderType) {
1552
- return { success: false, error: `Cannot dispatch to remote node '${node.id}': providerType unknown. Set providerPriority on the node policy or call mesh_launch_session first.` };
1553
- }
1554
- try {
1555
- const dispatchResult = await transport.meshCommand(daemonId, "agent_command", {
1556
- ...sessionId ? { targetSessionId: sessionId } : {},
1557
- agentType: resolvedProviderType,
1558
- cliType: resolvedProviderType,
1559
- action: "send_chat",
1560
- message: args.message,
1561
- // WTCLAIM (B): carry the node workspace so a sessionless dispatch can be
1562
- // scoped to THIS node's session on the worker (findAdapter dir match /
1563
- // findMeshNodeAdapter). Without it, a worker hosting both a base node and a
1564
- // cloned worktree node (same daemonId) would fall through to a provider-only
1565
- // fuzzy match and could land worktree work on the base session.
1566
- ...node.workspace ? { dir: node.workspace } : {},
1567
- ...args.meshContext ? { meshContext: args.meshContext } : {}
1568
- });
1569
- const dispatchPayload = unwrapCommandPayload(dispatchResult);
1570
- if (dispatchPayload?.success === false || dispatchResult?.success === false) {
1571
- const source = dispatchPayload?.success === false ? dispatchPayload : dispatchResult;
1572
- const errorMessage = dispatchPayload?.error || dispatchResult?.error || "agent_command rejected the task";
1573
- return {
1574
- ...buildCoordinatorP2pRelayFailure(source?.error || errorMessage, {
1575
- command: "agent_command",
1576
- targetDaemonId: daemonId,
1577
- nodeId: node.id,
1578
- sessionId
1579
- }),
1580
- ...source && typeof source === "object" ? source : {},
1581
- success: false,
1582
- error: `P2P dispatch failed: ${errorMessage}`
1583
- };
1584
- }
1585
- return { success: true, dispatched: true, sessionId: sessionId || "", providerType: resolvedProviderType };
1586
- } catch (e) {
1587
- const errorMessage = e?.message || String(e);
1588
- return {
1589
- ...buildCoordinatorP2pRelayFailure(e, {
1590
- command: "agent_command",
1591
- targetDaemonId: daemonId,
1592
- nodeId: node.id,
1593
- sessionId
1594
- }),
1595
- error: `P2P dispatch failed: ${errorMessage}`
1596
- };
1597
- }
1598
- }
1599
- function resolveCoordinatorNode(ctx) {
1600
- const preferredNodeId = typeof ctx.mesh.coordinator?.preferredNodeId === "string" ? ctx.mesh.coordinator.preferredNodeId.trim() : "";
1601
- if (preferredNodeId) {
1602
- const preferred = ctx.mesh.nodes.find((n) => n.id === preferredNodeId && typeof n.daemonId === "string" && n.daemonId.trim());
1603
- if (preferred) return preferred;
1604
- }
1605
- if (ctx.localMachineId) {
1606
- const byMachine = ctx.mesh.nodes.find((n) => readNodeMachineId(n) === ctx.localMachineId);
1607
- if (byMachine) return byMachine;
1608
- }
1609
- if (ctx.localDaemonId) {
1610
- return ctx.mesh.nodes.find((n) => readNodeDaemonId(n) === ctx.localDaemonId);
1611
- }
1612
- return void 0;
1613
- }
1614
- function resolveCoordinatorDaemonId(ctx) {
1615
- return readString(resolveCoordinatorNode(ctx)?.daemonId) || readString(ctx.localDaemonId) || readString(ctx.localMachineId);
932
+ function resolveCoordinatorDaemonId(ctx) {
933
+ return readString(resolveCoordinatorNode(ctx)?.daemonId) || readString(ctx.localDaemonId) || readString(ctx.localMachineId);
1616
934
  }
1617
935
  function readNodeMachineId(node) {
1618
936
  return readString(node.machineId) || readString(node.machine_id) || readString(node.machine?.id) || readString(node.machine?.machineId) || readString(node.lastProbe?.machineId) || readString(node.last_probe?.machine_id) || readString(node.lastProbe?.machine?.id) || readString(node.lastProbe?.machine?.machineId) || readString(node.last_probe?.machine?.id) || readString(node.last_probe?.machine?.machine_id);
@@ -1760,1061 +1078,1775 @@ function resolvePreferredWorktreeNodeId(ctx) {
1760
1078
  function isLocalControlPlaneNode(ctx, node) {
1761
1079
  return !!getLocalControlPlaneMatchReason(ctx, node);
1762
1080
  }
1763
- function meshSessionCacheKey(nodeId, runtimeSessionId) {
1764
- return `${nodeId}:${runtimeSessionId}`;
1765
- }
1766
- function rememberMeshSessionProviderMetadata(nodeId, runtimeSessionId, metadata) {
1767
- const keyNodeId = readString(nodeId);
1768
- const keySessionId = readString(runtimeSessionId);
1769
- if (!keyNodeId || !keySessionId) return;
1770
- const providerType = readString(metadata.providerType);
1771
- const providerSessionId = readString(metadata.providerSessionId);
1772
- if (!providerType && !providerSessionId) return;
1773
- const existing = getSessionMetadata(meshSessionCacheKey(keyNodeId, keySessionId)) || { providerType: "" };
1774
- meshSessionProviderMetadata.set(meshSessionCacheKey(keyNodeId, keySessionId), {
1775
- providerType: providerType || existing.providerType,
1776
- providerSessionId: providerSessionId || existing.providerSessionId,
1777
- expiresAt: Date.now() + SESSION_PROVIDER_METADATA_TTL_MS
1778
- });
1779
- }
1780
- function rememberMeshSessionProviderMetadataFromEvent(event) {
1781
- const metadataEvent = event?.metadataEvent && typeof event.metadataEvent === "object" ? event.metadataEvent : event && typeof event === "object" ? event : {};
1782
- const nodeId = readString(event?.nodeId) || readString(metadataEvent.nodeId) || readString(metadataEvent.meshNodeId);
1783
- const sessionId = readString(metadataEvent.targetSessionId) || readString(metadataEvent.sessionId) || readString(metadataEvent.instanceId) || readString(event?.sessionId);
1784
- rememberMeshSessionProviderMetadata(nodeId, sessionId, {
1785
- providerType: readString(metadataEvent.providerType) || readString(event?.providerType) || "",
1786
- providerSessionId: readString(metadataEvent.providerSessionId) || readString(event?.providerSessionId)
1787
- });
1788
- }
1789
- function resolveMeshSessionProviderMetadataFromLedger(ctx, nodeId, runtimeSessionId) {
1790
- const entries = (0, import_daemon_core.readLedgerEntries)(ctx.mesh.id, { tail: 50 });
1791
- for (let i = entries.length - 1; i >= 0; i -= 1) {
1792
- const entry = entries[i];
1793
- const payload = entry.payload && typeof entry.payload === "object" && !Array.isArray(entry.payload) ? entry.payload : {};
1794
- const entryNodeId = readString(entry.nodeId) || readString(payload.nodeId) || readString(payload.meshNodeId);
1795
- if (entryNodeId && entryNodeId !== nodeId) continue;
1796
- const entrySessionId = readString(entry.sessionId) || readString(payload.targetSessionId) || readString(payload.sessionId) || readString(payload.instanceId);
1797
- if (entrySessionId !== runtimeSessionId) continue;
1798
- const providerType = readString(entry.providerType) || readString(payload.providerType);
1799
- const completionDiagnostic = payload.completionDiagnostic && typeof payload.completionDiagnostic === "object" && !Array.isArray(payload.completionDiagnostic) ? payload.completionDiagnostic : {};
1800
- const metadataEvent = payload.metadataEvent && typeof payload.metadataEvent === "object" && !Array.isArray(payload.metadataEvent) ? payload.metadataEvent : {};
1801
- const providerSessionId = readString(payload.providerSessionId) || readString(completionDiagnostic.providerSessionId) || readString(metadataEvent.providerSessionId);
1802
- if (providerType || providerSessionId) {
1803
- return { providerType: providerType || "", providerSessionId };
1804
- }
1805
- }
1806
- return void 0;
1807
- }
1808
- function resolveMeshSessionProviderMetadata(ctx, nodeId, runtimeSessionId) {
1809
- const cached = getSessionMetadata(meshSessionCacheKey(nodeId, runtimeSessionId));
1810
- if (cached?.providerType || cached?.providerSessionId) return cached;
1811
- const fromLedger = resolveMeshSessionProviderMetadataFromLedger(ctx, nodeId, runtimeSessionId);
1812
- if (fromLedger) rememberMeshSessionProviderMetadata(nodeId, runtimeSessionId, fromLedger);
1813
- return fromLedger;
1814
- }
1815
- function countUncommittedChanges(status) {
1816
- if (typeof status?.uncommittedChanges === "number") return status.uncommittedChanges;
1817
- const keys = ["staged", "modified", "untracked", "deleted", "renamed"];
1818
- const counted = keys.reduce((sum, key) => sum + (Number.isFinite(Number(status?.[key])) ? Number(status[key]) : 0), 0);
1819
- const conflicts = Array.isArray(status?.conflictFiles) ? status.conflictFiles.length : status?.hasConflicts ? 1 : 0;
1820
- return counted + conflicts;
1821
- }
1822
- function isGitStatusDirty(status) {
1823
- if (typeof status?.isDirty === "boolean") return status.isDirty;
1824
- if (typeof status?.dirty === "boolean") return status.dirty;
1825
- if (Array.isArray(status?.submodules) && status.submodules.some((submodule) => submodule?.dirty || submodule?.outOfSync || submodule?.error)) return true;
1826
- return countUncommittedChanges(status) > 0;
1827
- }
1828
- var LARGE_LEDGER_FIELD_KEYS = /* @__PURE__ */ new Set(["plan", "validationPlan", "suggestedConfig", "payload"]);
1829
- var LARGE_LEDGER_OBJECT_THRESHOLD = 800;
1830
- var LARGE_LEDGER_NESTED_BYTES_THRESHOLD = 2e3;
1831
- function summarizeLargeLedgerField(key, value) {
1832
- if (typeof value === "string") {
1833
- return value.length > 500 ? value.slice(0, 500) + "\u2026" : value;
1834
- }
1835
- if (Array.isArray(value)) {
1836
- const serialized = JSON.stringify(value);
1837
- if (serialized && serialized.length > LARGE_LEDGER_OBJECT_THRESHOLD) {
1838
- return `[${key} summarized: ${value.length} items \u2014 use verbose=true or mesh_reconcile_ledger]`;
1081
+
1082
+ // src/tools/mesh-tool-schemas.ts
1083
+ var MESH_STATUS_TOOL = {
1084
+ name: "mesh_status",
1085
+ description: "Get the current status of all nodes in the repo mesh \u2014 health, git state, active sessions, recovery hints, and recommended next steps. Use this to decide which node to send work to or how to recover from failures. Also reports the running daemon build per daemonId under top-level daemonBuilds ({commit, commitShort, version}); when a live daemon was built from a commit BEHIND its workspace HEAD it adds staleDaemonBuilds[] + staleDaemonBuildWarning \u2014 meaning a just-merged refinery/mesh-tool fix is NOT yet live on that daemon (awaiting deploy/restart; a local dist rebuild does not update a cloud daemon). Do not repeatedly call this to wait for generating delegated work; wait for pendingCoordinatorEvents/completion events or an explicit user status request.",
1086
+ inputSchema: {
1087
+ type: "object",
1088
+ properties: {
1089
+ _gemini_compat: { type: "string", description: "Dummy property for Gemini compatibility. Ignore this." },
1090
+ includeStaleDirectWorkDetails: { type: "boolean", description: "Opt in to the full staleDirectWork array. Defaults false; normal status returns compact staleDirectWorkSummary only." },
1091
+ includeSessions: { type: "boolean", description: "Opt in to per-node live session arrays. Default false: compact mode returns a per-node sessionSummary (counts) and de-duplicated full session lists under top-level daemonSessions keyed by daemonId (sessions are not repeated for every node that shares a daemon). Set true to also include the full session array on each node." },
1092
+ compact: { type: "boolean", description: "Slim payload for LLM callers. Default true. Folds per-node session arrays to sessionSummary and de-duplicates daemon-shared sessions into daemonSessions. Set false (or verbose=true) for the full dashboard-grade payload." },
1093
+ verbose: { type: "boolean", description: "Force the full payload; overrides compact." }
1839
1094
  }
1840
- return value;
1841
1095
  }
1842
- if (value && typeof value === "object") {
1843
- const serialized = JSON.stringify(value);
1844
- if (serialized && serialized.length > LARGE_LEDGER_OBJECT_THRESHOLD) {
1845
- return `[${key} summarized: ${Object.keys(value).length} keys \u2014 use verbose=true or mesh_reconcile_ledger]`;
1096
+ };
1097
+ var MESH_LIST_NODES_TOOL = {
1098
+ name: "mesh_list_nodes",
1099
+ description: "List all nodes in the mesh with their capabilities, platform, and workspace paths.",
1100
+ inputSchema: {
1101
+ type: "object",
1102
+ properties: {
1103
+ _gemini_compat: { type: "string", description: "Dummy property for Gemini compatibility. Ignore this." }
1846
1104
  }
1847
- return value;
1848
- }
1849
- return value;
1850
- }
1851
- function elideLargeNestedValue(key, value) {
1852
- if (value === null || value === void 0) return value;
1853
- if (typeof value === "string") {
1854
- return value.length > 1e3 ? value.slice(0, 1e3) + "\u2026" : value;
1855
1105
  }
1856
- if (typeof value !== "object") return value;
1857
- const serialized = JSON.stringify(value);
1858
- const bytes = serialized ? serialized.length : 0;
1859
- if (bytes <= LARGE_LEDGER_NESTED_BYTES_THRESHOLD) return value;
1860
- return {
1861
- _elided: true,
1862
- _kind: key,
1863
- _bytes: bytes,
1864
- _hint: "full evidence via mesh_reconcile_ledger"
1865
- };
1866
- }
1867
- function slimLedgerPayload(payload) {
1868
- const slim = {};
1869
- for (const [k, v] of Object.entries(payload)) {
1870
- if (k === "message" || k === "taskSummary") {
1871
- slim[k] = typeof v === "string" && v.length > 200 ? v.slice(0, 200) + "\u2026" : v;
1872
- } else if (k === "evidence" || k === "workerResult" || k === "gitStatus" || k === "validationResults") {
1873
- } else if (k === "finalSummary") {
1874
- slim[k] = typeof v === "string" && v.length > 300 ? v.slice(0, 300) + "\u2026" : v;
1875
- } else if (LARGE_LEDGER_FIELD_KEYS.has(k)) {
1876
- slim[k] = summarizeLargeLedgerField(k, v);
1877
- } else {
1878
- slim[k] = elideLargeNestedValue(k, v);
1879
- }
1106
+ };
1107
+ var MESH_ENQUEUE_TASK_TOOL = {
1108
+ name: "mesh_enqueue_task",
1109
+ description: "Add a new task to the mesh work queue. Idle nodes will automatically pull and execute tasks from this queue. Use this instead of mesh_send_task when you do not need to target a specific node.",
1110
+ inputSchema: {
1111
+ type: "object",
1112
+ properties: {
1113
+ message: { type: "string", description: "The task instruction for the agent." },
1114
+ task_mode: { type: "string", enum: ["code_change", "validation", "live_debug_readonly", "launch_app", "convergence"], description: "Optional task-mode contract. live_debug_readonly rejects obvious write/commit/push/deploy/destructive instructions before dispatch." },
1115
+ taskMode: { type: "string", enum: ["code_change", "validation", "live_debug_readonly", "launch_app", "convergence"], description: "CamelCase alias for task_mode." },
1116
+ requiredTags: { type: "array", items: { type: "string" }, description: "Optional capability tags that every eligible node must have, e.g. os=darwin, provider=codex-cli, gpu." },
1117
+ required_tags: { type: "array", items: { type: "string" }, description: "Snake_case alias for requiredTags." },
1118
+ target_node_id: { type: "string", description: "Optional: only this node may claim the task. Use to route a queued task to a specific (e.g. freshly cloned) worktree node instead of letting the first idle base node claim it. Takes priority over prefer_worktree." },
1119
+ targetNodeId: { type: "string", description: "CamelCase alias for target_node_id." },
1120
+ prefer_worktree: { type: "boolean", description: "Optional: when true, route this task to the most recently cloned idle worktree node (avoids the main/base workspace preemptively claiming an isolated task). No-op if no worktree node exists; resolves to a target_node_id when one does." },
1121
+ preferWorktree: { type: "boolean", description: "CamelCase alias for prefer_worktree." },
1122
+ depends_on: { type: "array", items: { type: "string" }, description: "Task ids that must complete before this task becomes claimable. Cycles are rejected at enqueue." },
1123
+ dependsOn: { type: "array", items: { type: "string" }, description: "CamelCase alias for depends_on." },
1124
+ mission_id: { type: "string", description: "Mission this task belongs to (mesh_mission record id)." },
1125
+ missionId: { type: "string", description: "CamelCase alias for mission_id." }
1126
+ },
1127
+ required: ["message"]
1880
1128
  }
1881
- return slim;
1882
- }
1883
- function readRelatedRepos(node) {
1884
- const raw = Array.isArray(node.relatedRepos) ? node.relatedRepos : Array.isArray(node.policy?.relatedRepos) ? node.policy.relatedRepos : [];
1885
- return raw.map((entry) => ({
1886
- label: typeof entry?.label === "string" ? entry.label.trim() : "",
1887
- workspace: typeof entry?.workspace === "string" ? entry.workspace.trim() : ""
1888
- })).filter((entry) => Boolean(entry.label && entry.workspace));
1889
- }
1890
- function summarizeRelatedRepoStatus(repo, status) {
1891
- const dirty = isGitStatusDirty(status);
1892
- return {
1893
- label: repo.label,
1894
- workspace: repo.workspace,
1895
- isGitRepo: status?.isGitRepo === true,
1896
- branch: status?.branch ?? null,
1897
- upstream: status?.upstream ?? null,
1898
- upstreamStatus: typeof status?.upstreamStatus === "string" ? status.upstreamStatus : status?.upstream ? "unchecked" : "no_upstream",
1899
- upstreamFetchedAt: Number.isFinite(Number(status?.upstreamFetchedAt)) ? Number(status.upstreamFetchedAt) : null,
1900
- upstreamFetchError: typeof status?.upstreamFetchError === "string" ? status.upstreamFetchError : null,
1901
- ahead: Number.isFinite(Number(status?.ahead)) ? Number(status.ahead) : 0,
1902
- behind: Number.isFinite(Number(status?.behind)) ? Number(status.behind) : 0,
1903
- dirty,
1904
- uncommittedChanges: countUncommittedChanges(status),
1905
- head: status?.headCommit ?? null,
1906
- lastCommitSummary: status?.headMessage ?? null,
1907
- ...status?.reason ? { reason: status.reason } : {},
1908
- ...status?.error ? { error: status.error } : {}
1909
- };
1910
- }
1911
- async function collectRelatedRepoStatuses(ctx, node) {
1912
- const relatedRepos = readRelatedRepos(node);
1913
- if (!relatedRepos.length) return [];
1914
- const results = [];
1915
- for (const repo of relatedRepos) {
1916
- try {
1917
- const statusResult = await commandForNode(ctx, node, "git_status", { workspace: repo.workspace, refreshUpstream: true });
1918
- const status = extractGitStatus(statusResult);
1919
- results.push(summarizeRelatedRepoStatus(repo, status));
1920
- } catch (e) {
1921
- results.push({
1922
- label: repo.label,
1923
- workspace: repo.workspace,
1924
- error: e?.message || "related repo status failed"
1925
- });
1129
+ };
1130
+ var MESH_VIEW_QUEUE_TOOL = {
1131
+ name: "mesh_view_queue",
1132
+ description: "View the mesh work queue with source-of-truth active counts separated from historical completed/failed/cancelled records. Do not repeatedly call this to wait for generating assigned work; wait for pendingCoordinatorEvents/completion events or an explicit user status request.",
1133
+ inputSchema: {
1134
+ type: "object",
1135
+ properties: {
1136
+ status: {
1137
+ type: "array",
1138
+ items: { type: "string" },
1139
+ description: "Explicit row filter by task status: pending, assigned, completed, failed, cancelled. Source-of-truth counts remain unfiltered; visible* counts describe returned rows."
1140
+ },
1141
+ view: {
1142
+ type: "string",
1143
+ enum: ["all", "active", "historical"],
1144
+ description: "Optional row view. active returns pending/assigned rows, historical returns completed/failed/cancelled rows, all returns every persisted queue row. Defaults to all for compatibility."
1145
+ },
1146
+ compact: { type: "boolean", description: "Slim payload for LLM callers. Default true. Drops large historical (completed/failed/cancelled) queue row arrays, the full staleDirectWork orphan array (kept as staleDirectWorkSummary counts), and per-row maintenance cleanupCandidates in favor of counts; pending/assigned active rows are retained. Set false (or verbose=true) for the full dashboard-grade payload." },
1147
+ verbose: { type: "boolean", description: "Force the full payload; overrides compact." }
1926
1148
  }
1927
1149
  }
1928
- return results;
1929
- }
1930
- function readProviderPriority(policy) {
1931
- const raw = policy?.providerPriority;
1932
- return Array.isArray(raw) ? raw.map((type) => typeof type === "string" ? type.trim() : "").filter(Boolean) : [];
1933
- }
1934
- function buildNodeCapabilityExposure(node) {
1935
- const providers = readProviderPriority(node.policy);
1936
- const capabilityTags = (0, import_daemon_core.buildMeshNodeCapabilityTags)(node);
1937
- const exposure = { capabilityTags };
1938
- if (providers.length) {
1939
- const byProvider = {};
1940
- for (const provider of providers) {
1941
- byProvider[provider] = (0, import_daemon_core.buildMeshNodeCapabilityTags)(node, provider);
1942
- }
1943
- exposure.capabilityTagsByProvider = byProvider;
1150
+ };
1151
+ var MESH_QUEUE_CANCEL_TOOL = {
1152
+ name: "mesh_queue_cancel",
1153
+ description: "Cancel a pending/assigned/completed/failed mesh queue task without deleting audit history. Use this to retire stale queue items that target dead sessions.",
1154
+ inputSchema: {
1155
+ type: "object",
1156
+ properties: {
1157
+ task_id: { type: "string", description: "Queue task ID to cancel." },
1158
+ reason: { type: "string", description: "Optional operator-visible reason for cancellation." }
1159
+ },
1160
+ required: ["task_id"]
1944
1161
  }
1945
- const capabilities = Array.isArray(node.capabilities) ? node.capabilities.filter((tag) => typeof tag === "string" && !!tag.trim()) : [];
1946
- if (capabilities.length) exposure.capabilities = capabilities;
1947
- return exposure;
1948
- }
1949
- function readSpawnedSessionVisibility(policy) {
1950
- return policy?.spawnedSessionVisibility === "hidden" ? "hidden" : "visible";
1951
- }
1952
- function missingProviderPriorityMessage(nodeId) {
1953
- return `Node '${nodeId}' has no providerPriority policy; pass type explicitly or configure node.policy.providerPriority`;
1954
- }
1955
- function getNodeLaunchReadiness(node) {
1956
- const bootstrap = node.worktreeBootstrap;
1957
- if (node.isLocalWorktree && bootstrap?.status === "failed" && bootstrap?.required !== false) {
1958
- return {
1959
- providerPriority: readProviderPriority(node.policy),
1960
- launchReady: false,
1961
- launchBlockedReason: "worktree_bootstrap_failed",
1962
- launchBlockedMessage: typeof bootstrap.error === "string" && bootstrap.error.trim() ? bootstrap.error.trim() : "Required worktree bootstrap failed; resolve it before launching an agent into this node.",
1963
- worktreeBootstrap: bootstrap
1964
- };
1162
+ };
1163
+ var MESH_QUEUE_REQUEUE_TOOL = {
1164
+ name: "mesh_queue_requeue",
1165
+ description: "Return a mesh queue task to pending for retry. By default clears stale assigned owner and target session so another live session can claim it. When the task has exceeded its retry cap it is auto-failed instead; use force=true to override.",
1166
+ inputSchema: {
1167
+ type: "object",
1168
+ properties: {
1169
+ task_id: { type: "string", description: "Queue task ID to requeue." },
1170
+ reason: { type: "string", description: "Optional operator-visible reason for requeueing." },
1171
+ target_node_id: { type: "string", description: "Optional replacement target node ID." },
1172
+ target_session_id: { type: "string", description: "Optional replacement target runtime session ID." },
1173
+ clear_target_node: { type: "boolean", description: "When true, remove any existing target node constraint." },
1174
+ keep_target_session: { type: "boolean", description: "When true, preserve an existing target session if target_session_id is not provided. Defaults false to avoid stale session targets." },
1175
+ force: { type: "boolean", description: "When true, bypass the retry cap and requeue even if maxRetries has been exceeded. Use only for explicit operator recovery." }
1176
+ },
1177
+ required: ["task_id"]
1965
1178
  }
1966
- const providerPriority = readProviderPriority(node.policy);
1967
- if (providerPriority.length) {
1968
- return {
1969
- providerPriority,
1970
- launchReady: true
1971
- };
1972
- }
1973
- return {
1974
- providerPriority,
1975
- launchReady: false,
1976
- launchBlockedReason: "missing_provider_priority",
1977
- launchBlockedMessage: missingProviderPriorityMessage(node.id)
1978
- };
1979
- }
1980
- function getWorktreeBootstrapLaunchBlock(node, meshPolicy) {
1981
- if (!node.isLocalWorktree) return void 0;
1982
- const bootstrap = node.worktreeBootstrap;
1983
- const requireReady = !!(meshPolicy && typeof meshPolicy === "object" && meshPolicy.requireBootstrapBeforeLaunch === true);
1984
- if (requireReady && bootstrap?.status !== "ready") {
1985
- return {
1986
- success: false,
1987
- code: "bootstrap_not_ready",
1988
- error: `Node '${node.id}' bootstrap state is '${bootstrap?.status ?? "unknown"}' and mesh policy requireBootstrapBeforeLaunch is enabled.`,
1989
- nodeId: node.id,
1990
- worktreeBootstrap: bootstrap ?? null,
1991
- recoveryHint: "Run the worktree bootstrap (clone runOnClone or a refine with bootstrap inherit) until the node reports ready, or disable requireBootstrapBeforeLaunch."
1992
- };
1179
+ };
1180
+ var MESH_SEND_TASK_TOOL = {
1181
+ name: "mesh_send_task",
1182
+ description: "Legacy push-based task assignment. Enqueues a task specifically targeted at a given node. The node will pull it immediately if idle.",
1183
+ inputSchema: {
1184
+ type: "object",
1185
+ properties: {
1186
+ node_id: { type: "string", description: "Target node ID (from mesh_list_nodes)." },
1187
+ session_id: { type: "string", description: "Agent session ID on the target node." },
1188
+ message: { type: "string", description: "Natural-language task to send to the agent." },
1189
+ task_mode: { type: "string", enum: ["code_change", "validation", "live_debug_readonly", "launch_app", "convergence"], description: "Optional task-mode contract. live_debug_readonly rejects obvious write/commit/push/deploy/destructive instructions before local or remote direct dispatch." },
1190
+ taskMode: { type: "string", enum: ["code_change", "validation", "live_debug_readonly", "launch_app", "convergence"], description: "CamelCase alias for task_mode." },
1191
+ mission_id: { type: "string", description: "Mission this task belongs to (mesh_mission record id). When set, the directly dispatched task is attributed to the mission task aggregates exactly like mesh_enqueue_task, including terminal completion. Omit for an unattributed direct dispatch." },
1192
+ missionId: { type: "string", description: "CamelCase alias for mission_id." }
1193
+ },
1194
+ required: ["node_id", "session_id", "message"]
1993
1195
  }
1994
- if (bootstrap?.status !== "failed" || bootstrap?.required === false) return void 0;
1995
- return {
1996
- success: false,
1997
- code: "worktree_bootstrap_failed",
1998
- error: typeof bootstrap.error === "string" && bootstrap.error.trim() ? bootstrap.error.trim() : `Node '${node.id}' has a failed required worktree bootstrap.`,
1999
- nodeId: node.id,
2000
- worktreeBootstrap: bootstrap,
2001
- recoveryHint: "Fix the configured worktree bootstrap command or remove/recreate the worktree node before launching an agent."
2002
- };
2003
- }
2004
- async function collectLiveStatusSessions(ctx, node) {
2005
- try {
2006
- const statusResult = await commandForNode(ctx, node, "get_status_metadata", {});
2007
- return extractStatusMetadataSessions(statusResult);
2008
- } catch {
2009
- return [];
1196
+ };
1197
+ var MESH_READ_CHAT_TOOL = {
1198
+ name: "mesh_read_chat",
1199
+ description: "Read recent chat messages from a delegated agent session on a mesh node. Use compact=true for coordinator context-efficient review: it filters tool/internal/debug chatter and returns the final user-visible summary plus recent key messages. If the runtime session has completed, provider_session_id can explicitly target provider transcript history.",
1200
+ inputSchema: {
1201
+ type: "object",
1202
+ properties: {
1203
+ node_id: { type: "string", description: "Target node ID." },
1204
+ session_id: { type: "string", description: "Agent session ID to read from." },
1205
+ provider_session_id: { type: "string", description: "Optional provider transcript/session ID for completed sessions." },
1206
+ tail: { type: "number", description: "Number of recent messages to return (default: 10)." },
1207
+ compact: { type: "boolean", description: "When true, return a compact coordinator summary instead of the full transcript: tool/internal/control/debug messages are excluded and only recent user-visible key messages plus the final assistant summary are included." }
1208
+ },
1209
+ required: ["node_id", "session_id"]
2010
1210
  }
2011
- }
2012
- async function collectLiveStatusProbe(ctx, node) {
2013
- try {
2014
- const statusResult = await commandForNode(ctx, node, "get_status_metadata", {});
2015
- return {
2016
- sessions: extractStatusMetadataSessions(statusResult),
2017
- daemonBuild: extractDaemonBuildInfo(statusResult)
2018
- };
2019
- } catch {
2020
- return { sessions: [] };
1211
+ };
1212
+ var MESH_READ_DEBUG_TOOL = {
1213
+ name: "mesh_read_debug",
1214
+ description: "Collect a daemon-side chat/parser debug bundle for a delegated agent session on a mesh node without opening the browser UI. Defaults to daemon_file delivery and returns a saved bundle locator.",
1215
+ inputSchema: {
1216
+ type: "object",
1217
+ properties: {
1218
+ node_id: { type: "string", description: "Target node ID." },
1219
+ session_id: { type: "string", description: "Agent session ID to debug." },
1220
+ provider_session_id: { type: "string", description: "Optional provider transcript/session ID for completed session history." },
1221
+ tail: { type: "number", description: "Number of recent read_chat messages to embed (default: 40)." },
1222
+ delivery: { type: "string", enum: ["daemon_file", "inline"], description: "daemon_file saves the full sanitized bundle on the daemon; inline returns it directly. Default: daemon_file." }
1223
+ },
1224
+ required: ["node_id", "session_id"]
2021
1225
  }
2022
- }
2023
- function extractDaemonBuildInfo(value) {
2024
- const payload = unwrapCommandPayload(value);
2025
- const build = payload?.daemonBuild && typeof payload.daemonBuild === "object" ? payload.daemonBuild : value?.daemonBuild && typeof value.daemonBuild === "object" ? value.daemonBuild : void 0;
2026
- if (!build) return void 0;
2027
- const commit = readString(build.commit);
2028
- if (!commit) return void 0;
2029
- return {
2030
- commit,
2031
- commitShort: readString(build.commitShort) || commit.slice(0, 7),
2032
- version: readString(build.version) || "unknown",
2033
- ...readString(build.builtAt) ? { builtAt: readString(build.builtAt) } : {}
2034
- };
2035
- }
2036
- async function collectMeshViewQueueNodesWithLiveSessions(ctx) {
2037
- const nodes = await Promise.all(ctx.mesh.nodes.map(async (node) => {
2038
- const liveSessions = await collectLiveStatusSessions(ctx, node);
2039
- return liveSessions.length > 0 ? { ...node, sessions: liveSessions } : node;
2040
- }));
2041
- return nodes;
2042
- }
2043
- function readNumeric(value, fallback = 0) {
2044
- const parsed = Number(value);
2045
- return Number.isFinite(parsed) ? parsed : fallback;
2046
- }
2047
- function buildBranchConvergence(mesh, node, status, dirty, uncommittedChanges) {
2048
- const defaultBranch = readString(mesh.defaultBranch) ?? "main";
2049
- const branch = readString(status?.branch) ?? readString(node.worktreeBranch) ?? null;
2050
- const ahead = readNumeric(status?.ahead);
2051
- const behind = readNumeric(status?.behind);
2052
- const upstream = readString(status?.upstream) ?? null;
2053
- const upstreamStatus = readString(status?.upstreamStatus) ?? (upstream ? "unchecked" : "no_upstream");
2054
- const hasConflicts = status?.hasConflicts === true || Array.isArray(status?.conflictFiles) && status.conflictFiles.length > 0;
2055
- const base = {
2056
- defaultBranch,
2057
- branch,
2058
- upstream,
2059
- upstreamStatus,
2060
- ahead,
2061
- behind,
2062
- isWorktree: node.isLocalWorktree === true,
2063
- isDefaultBranch: branch === defaultBranch
2064
- };
2065
- if (status?.isGitRepo !== true) {
2066
- return {
2067
- ...base,
2068
- status: "blocked_review",
2069
- needsConvergence: true,
2070
- reason: "git_status_unavailable",
2071
- nextStep: `Resolve git status for node '${node.id}' before marking the task complete.`
2072
- };
1226
+ };
1227
+ var MESH_LAUNCH_SESSION_TOOL = {
1228
+ name: "mesh_launch_session",
1229
+ description: "Launch a new agent session on a mesh node. Returns the session ID for subsequent send_task/read_chat calls. If the user names a provider, preserve it exactly: Hermes = hermes-cli, Claude Code/Claude = claude-cli, Codex = codex-cli, Gemini = gemini-cli. If type is omitted, resolve strictly from the node policy providerPriority and provider detection; fail closed when no configured provider is usable. Do not default to claude-cli.",
1230
+ inputSchema: {
1231
+ type: "object",
1232
+ properties: {
1233
+ node_id: { type: "string", description: "Target node ID." },
1234
+ type: { type: "string", description: "Optional provider type to launch. Use hermes-cli for Hermes, claude-cli for Claude Code, codex-cli for Codex, gemini-cli for Gemini. When omitted, node.policy.providerPriority is probed in order." }
1235
+ },
1236
+ required: ["node_id"]
2073
1237
  }
2074
- if (!branch) {
2075
- return {
2076
- ...base,
2077
- status: "blocked_review",
2078
- needsConvergence: true,
2079
- reason: "branch_unknown",
2080
- nextStep: `Inspect node '${node.id}' git branch before deciding whether it is merged to ${defaultBranch}.`
2081
- };
1238
+ };
1239
+ var MESH_GIT_STATUS_TOOL = {
1240
+ name: "mesh_git_status",
1241
+ description: "Get git status for a mesh node workspace \u2014 branch, dirty state, changed files.",
1242
+ inputSchema: {
1243
+ type: "object",
1244
+ properties: {
1245
+ node_id: { type: "string", description: "Target node ID." }
1246
+ },
1247
+ required: ["node_id"]
2082
1248
  }
2083
- if (hasConflicts || dirty || uncommittedChanges > 0) {
2084
- return {
2085
- ...base,
2086
- status: "not_mergeable",
2087
- needsConvergence: true,
2088
- reason: hasConflicts ? "conflicts_present" : "dirty_workspace",
2089
- nextStep: `Commit, checkpoint, or resolve node '${node.id}' before any main convergence step.`
2090
- };
1249
+ };
1250
+ var MESH_READ_NODE_LOGS_TOOL = {
1251
+ name: "mesh_read_node_logs",
1252
+ description: "Fetch a recent daemon LOG tail directly from a (possibly remote) mesh node over P2P \u2014 no session launch, no PowerShell/shell grep on the remote machine. Use this to debug a node's daemon: read its error/warn lines, grep for a pattern, or read since a timestamp. The reply is byte-bounded (\u2264128KB, default 64KB; truncated:true when the file was larger, newest lines kept) and secrets (API keys, machine secrets, bearer tokens, JWTs, TURN credentials) are redacted before transmission. This reads the DAEMON log, not an agent session transcript \u2014 for a session transcript use mesh_read_chat / mesh_read_debug.",
1253
+ inputSchema: {
1254
+ type: "object",
1255
+ properties: {
1256
+ node_id: { type: "string", description: "Target node ID (the daemon owning it serves its own log)." },
1257
+ grep: { type: "string", description: "Optional regex (case-insensitive) \u2014 only matching log lines are returned. Invalid regex falls back to a literal substring match." },
1258
+ since_ms: { type: "number", description: "Optional epoch-ms floor \u2014 only log lines at/after this time are returned (lines without a parseable timestamp are kept)." },
1259
+ tail_bytes: { type: "number", description: "Max bytes of log tail to read (default 65536, capped at 131072). Larger files are truncated to the newest tail_bytes." },
1260
+ date: { type: "string", description: "Optional YYYY-MM-DD log date (defaults to today). Falls back to the size-rotation backup when the active file is absent." }
1261
+ },
1262
+ required: ["node_id"]
2091
1263
  }
2092
- if (branch === defaultBranch) {
2093
- if (upstream && upstreamStatus !== "fresh") {
2094
- return {
2095
- ...base,
2096
- status: "blocked_review",
2097
- needsConvergence: true,
2098
- reason: "default_branch_upstream_unverified",
2099
- nextStep: `Refresh ${defaultBranch}'s upstream refs or resolve the fetch failure before declaring convergence complete for node '${node.id}'.`
2100
- };
2101
- }
2102
- if (ahead > 0 || behind > 0) {
2103
- return {
2104
- ...base,
2105
- status: "blocked_review",
2106
- needsConvergence: true,
2107
- reason: "default_branch_not_even_with_upstream",
2108
- nextStep: `Bring ${defaultBranch} even with its upstream before declaring convergence complete.`
2109
- };
2110
- }
2111
- return {
2112
- ...base,
2113
- status: "merged_to_main",
2114
- needsConvergence: false,
2115
- reason: "clean_default_branch",
2116
- nextStep: null
2117
- };
1264
+ };
1265
+ var MESH_FAST_FORWARD_NODE_TOOL = {
1266
+ name: "mesh_fast_forward_node",
1267
+ description: 'Safely dry-run or execute an obvious direct fast-forward for a mesh node without launching an agent session. mode="merge" (default) absorbs upstream commits into the local branch via git merge --ff-only (ahead=0, behind>0). mode="push" publishes local commits to origin via a strict ff-only push (HEAD must be a descendant of origin/<branch>). Defaults to dry-run; execution requires execute=true. Never force-pushes, rebases, resets, cleans, or checks out arbitrary revisions. When the merge path finds the branch ahead with nothing to merge, it returns code "ahead_needs_push" pointing at mode="push".',
1268
+ inputSchema: {
1269
+ type: "object",
1270
+ properties: {
1271
+ node_id: { type: "string", description: "Target node ID." },
1272
+ mode: { type: "string", enum: ["merge", "push"], description: "merge (default): git merge --ff-only to absorb upstream. push: strict ff-only push of local commits to origin/<branch>; refuses any non-fast-forward." },
1273
+ branch: { type: "string", description: "Optional guard: require the node's current branch to match this branch before planning/executing." },
1274
+ execute: { type: "boolean", description: "When true, apply the fast-forward/push if all safety gates pass. Defaults false/dry-run." },
1275
+ dry_run: { type: "boolean", description: "Preview only. Defaults true unless execute=true; dry_run=true overrides execute." },
1276
+ update_submodules: { type: "boolean", description: 'mode="merge" only: when true, if the root fast-forward changes gitlinks, run only git submodule update --init --recursive and verify submodules clean.' },
1277
+ push_submodules: { type: "boolean", description: 'mode="push" only: also ff-only push submodule HEADs to their origin main. Gated by mesh policy allowAutoPublishSubmoduleMainCommits \u2014 skipped unless that policy is enabled. Defaults false (root push only).' }
1278
+ },
1279
+ required: ["node_id"]
2118
1280
  }
2119
- if (node.isLocalWorktree) {
2120
- return {
2121
- ...base,
2122
- status: "cleanup_candidate",
2123
- needsConvergence: true,
2124
- reason: "clean_non_default_worktree_branch",
2125
- nextStep: `Run mesh_refine_node(node_id: "${node.id}") or explicitly classify this worktree as blocked_review/not_mergeable before ending the task.`
2126
- };
2127
- }
2128
- if (upstream && upstreamStatus !== "fresh") {
2129
- return {
2130
- ...base,
2131
- status: "blocked_review",
2132
- needsConvergence: true,
2133
- reason: "feature_branch_upstream_unverified",
2134
- nextStep: `Refresh branch '${branch}' upstream refs or resolve the fetch failure before deciding whether it is ready to merge into ${defaultBranch}.`
2135
- };
2136
- }
2137
- if (!upstream || ahead > 0 || behind > 0) {
2138
- return {
2139
- ...base,
2140
- status: "blocked_review",
2141
- needsConvergence: true,
2142
- reason: !upstream ? "feature_branch_missing_upstream" : "feature_branch_not_even_with_upstream",
2143
- nextStep: `Push or reconcile branch '${branch}', then merge it into ${defaultBranch} or mark it not_mergeable with a reason.`
2144
- };
2145
- }
2146
- return {
2147
- ...base,
2148
- status: "pushed_feature_branch_needs_merge",
2149
- needsConvergence: true,
2150
- reason: "clean_non_default_branch",
2151
- nextStep: `Review and merge branch '${branch}' into ${defaultBranch}; do not report the task as fully complete while it remains off main.`
2152
- };
2153
- }
2154
- var COMPACT_MAX_CONVERGENCE_FOLLOWUPS = 12;
2155
- function summarizeBranchConvergence(nodes, compact = false) {
2156
- const allFollowUps = nodes.filter((node) => node?.branchConvergence?.needsConvergence === true).map((node) => ({
2157
- nodeId: node.nodeId,
2158
- // workspace is a long absolute path redundant with nodeId — drop it in
2159
- // compact mode to keep this summary bounded.
2160
- ...compact ? {} : { workspace: node.workspace },
2161
- branch: node.branchConvergence.branch,
2162
- status: node.branchConvergence.status,
2163
- reason: node.branchConvergence.reason,
2164
- // The per-node nextStep is long prose that repeats node ids/branch names.
2165
- // In compact mode drop it (the status+reason carry the actionable signal;
2166
- // verbose still surfaces the full nextStep) so this summary stays bounded
2167
- // as node count grows.
2168
- ...compact ? {} : { nextStep: node.branchConvergence.nextStep }
2169
- }));
2170
- const byStatus = {};
2171
- for (const f of allFollowUps) {
2172
- const s = typeof f.status === "string" ? f.status : "unknown";
2173
- byStatus[s] = (byStatus[s] ?? 0) + 1;
2174
- }
2175
- const followUps = compact ? allFollowUps.slice(0, COMPACT_MAX_CONVERGENCE_FOLLOWUPS) : allFollowUps;
2176
- const omitted = allFollowUps.length - followUps.length;
2177
- return {
2178
- needsFollowUp: allFollowUps.length > 0,
2179
- unresolvedCount: allFollowUps.length,
2180
- byStatus,
2181
- requiredFinalStates: ["merged_to_main", "pushed_feature_branch_needs_merge", "blocked_review", "cleanup_candidate", "not_mergeable"],
2182
- followUps,
2183
- ...omitted > 0 ? { followUpsOmitted: omitted, followUpsHint: "Per-node followUp rows are capped in compact mode; counts above are complete. Use verbose=true for the full list." } : {}
2184
- };
2185
- }
2186
- async function commandForNode(ctx, node, command, args = {}) {
2187
- const isLocalNode = isLocalControlPlaneNode(ctx, node);
2188
- if (ctx.transport instanceof IpcTransport && node.daemonId && !isLocalNode) {
2189
- return ctx.transport.meshCommand(node.daemonId, command, args);
2190
- }
2191
- return ctx.transport.command(command, args);
2192
- }
2193
- function normalizePendingMeshCoordinatorEvents(value) {
2194
- const payload = unwrapCommandPayload(value);
2195
- const events = Array.isArray(payload?.events) ? payload.events : Array.isArray(value?.events) ? value.events : [];
2196
- return events.filter((event) => event && typeof event === "object");
2197
- }
2198
- function buildMeshForwardPayloadFromPendingEvent(event) {
2199
- const metadataEvent = event?.metadataEvent && typeof event.metadataEvent === "object" ? event.metadataEvent : {};
2200
- return {
2201
- event: readString(event?.event),
2202
- meshId: readString(event?.meshId),
2203
- nodeId: readString(event?.nodeId) || readString(metadataEvent.meshNodeId),
2204
- workspace: readString(event?.workspace) || readString(metadataEvent.workspace),
2205
- targetSessionId: readString(metadataEvent.targetSessionId) || readString(metadataEvent.sessionId) || readString(metadataEvent.instanceId),
2206
- providerType: readString(metadataEvent.providerType),
2207
- providerSessionId: readString(metadataEvent.providerSessionId),
2208
- finalSummary: readString(metadataEvent.finalSummary) || readString(metadataEvent.summary),
2209
- jobId: readString(metadataEvent.jobId),
2210
- interactionId: readString(metadataEvent.interactionId),
2211
- status: readString(metadataEvent.status),
2212
- targetDaemonId: readString(metadataEvent.targetDaemonId),
2213
- startedAt: readString(metadataEvent.startedAt),
2214
- completedAt: readString(metadataEvent.completedAt),
2215
- retryOfJobId: readString(metadataEvent.retryOfJobId),
2216
- ...metadataEvent.result && typeof metadataEvent.result === "object" && !Array.isArray(metadataEvent.result) ? { result: metadataEvent.result } : {},
2217
- ...metadataEvent.intentional === true ? { intentional: true } : {},
2218
- ...metadataEvent.intentionalStop === true ? { intentionalStop: true } : {},
2219
- ...metadataEvent.operatorCleanup === true ? { operatorCleanup: true } : {},
2220
- ...readString(metadataEvent.reason) ? { reason: readString(metadataEvent.reason) } : {},
2221
- ...readString(metadataEvent.stopReason) ? { stopReason: readString(metadataEvent.stopReason) } : {},
2222
- ...readString(metadataEvent.cleanupReason) ? { cleanupReason: readString(metadataEvent.cleanupReason) } : {},
2223
- ...readString(metadataEvent.source) ? { source: readString(metadataEvent.source) } : {}
2224
- };
2225
- }
2226
- async function drainCoordinatorPendingEvents(ctx, opts) {
2227
- const requestedNodeIds = opts?.nodeIds?.length ? new Set(opts.nodeIds) : null;
2228
- const matchesCurrentMesh = (event) => readString(event?.meshId) === ctx.mesh.id;
2229
- if (ctx.transport instanceof IpcTransport) {
2230
- const surfacedEvents = [];
2231
- const coordinatorDaemonId = readString(ctx.localDaemonId);
2232
- const pendingEventArgs = {
2233
- meshId: ctx.mesh.id,
2234
- ...coordinatorDaemonId ? { coordinatorDaemonId } : {}
2235
- };
2236
- try {
2237
- const localEvents = normalizePendingMeshCoordinatorEvents(await ctx.transport.command("get_pending_mesh_events", pendingEventArgs)).filter(matchesCurrentMesh);
2238
- for (const event of localEvents) {
2239
- const payload = buildMeshForwardPayloadFromPendingEvent(event);
2240
- if (!payload.event || !payload.meshId) continue;
2241
- let injected = false;
2242
- try {
2243
- await ctx.transport.command("mesh_forward_event", payload);
2244
- injected = true;
2245
- } catch {
2246
- }
2247
- rememberMeshSessionProviderMetadataFromEvent({ ...event, metadataEvent: payload });
2248
- if (!injected) surfacedEvents.push(event);
2249
- }
2250
- } catch {
2251
- }
2252
- for (const node of ctx.mesh.nodes) {
2253
- if (!node.daemonId || isLocalControlPlaneNode(ctx, node)) continue;
2254
- if (requestedNodeIds && !requestedNodeIds.has(node.id)) continue;
2255
- try {
2256
- const remoteEvents = normalizePendingMeshCoordinatorEvents(
2257
- await ctx.transport.meshCommand(node.daemonId, "get_pending_mesh_events", pendingEventArgs)
2258
- ).filter(matchesCurrentMesh);
2259
- if (remoteEvents.length === 0) continue;
2260
- for (const event of remoteEvents) {
2261
- const payload = buildMeshForwardPayloadFromPendingEvent(event);
2262
- if (!payload.event || !payload.meshId) continue;
2263
- await ctx.transport.command("mesh_forward_event", payload);
2264
- rememberMeshSessionProviderMetadataFromEvent({ ...event, metadataEvent: payload });
2265
- }
2266
- } catch {
2267
- }
2268
- }
2269
- try {
2270
- const localEvents = normalizePendingMeshCoordinatorEvents(await ctx.transport.command("get_pending_mesh_events", pendingEventArgs)).filter(matchesCurrentMesh);
2271
- for (const event of localEvents) {
2272
- const payload = buildMeshForwardPayloadFromPendingEvent(event);
2273
- if (!payload.event || !payload.meshId) continue;
2274
- let injected = false;
2275
- try {
2276
- await ctx.transport.command("mesh_forward_event", payload);
2277
- injected = true;
2278
- } catch {
2279
- }
2280
- rememberMeshSessionProviderMetadataFromEvent({ ...event, metadataEvent: payload });
2281
- if (!injected) surfacedEvents.push(event);
2282
- }
2283
- } catch {
2284
- }
2285
- return surfacedEvents;
2286
- }
2287
- const events = (0, import_daemon_core.drainPendingMeshCoordinatorEvents)(ctx.mesh.id, ctx.localDaemonId).filter(matchesCurrentMesh);
2288
- events.forEach(rememberMeshSessionProviderMetadataFromEvent);
2289
- return events;
2290
- }
2291
- function isP2pTransportUnavailableError(error) {
2292
- return (0, import_daemon_core.isP2pRelayTransportFailure)(error);
2293
- }
2294
- function buildRemoveNodeArgs(ctx, nodeId, sessionCleanupMode, force) {
2295
- return {
2296
- meshId: ctx.mesh.id,
2297
- nodeId,
2298
- ...sessionCleanupMode ? { sessionCleanupMode } : {},
2299
- ...force === true ? { force: true } : {},
2300
- inlineMesh: ctx.mesh
2301
- };
2302
- }
2303
- var MESH_STATUS_TOOL = {
2304
- name: "mesh_status",
2305
- description: "Get the current status of all nodes in the repo mesh \u2014 health, git state, active sessions, recovery hints, and recommended next steps. Use this to decide which node to send work to or how to recover from failures. Also reports the running daemon build per daemonId under top-level daemonBuilds ({commit, commitShort, version}); when a live daemon was built from a commit BEHIND its workspace HEAD it adds staleDaemonBuilds[] + staleDaemonBuildWarning \u2014 meaning a just-merged refinery/mesh-tool fix is NOT yet live on that daemon (awaiting deploy/restart; a local dist rebuild does not update a cloud daemon). Do not repeatedly call this to wait for generating delegated work; wait for pendingCoordinatorEvents/completion events or an explicit user status request.",
2306
- inputSchema: {
2307
- type: "object",
2308
- properties: {
2309
- _gemini_compat: { type: "string", description: "Dummy property for Gemini compatibility. Ignore this." },
2310
- includeStaleDirectWorkDetails: { type: "boolean", description: "Opt in to the full staleDirectWork array. Defaults false; normal status returns compact staleDirectWorkSummary only." },
2311
- includeSessions: { type: "boolean", description: "Opt in to per-node live session arrays. Default false: compact mode returns a per-node sessionSummary (counts) and de-duplicated full session lists under top-level daemonSessions keyed by daemonId (sessions are not repeated for every node that shares a daemon). Set true to also include the full session array on each node." },
2312
- compact: { type: "boolean", description: "Slim payload for LLM callers. Default true. Folds per-node session arrays to sessionSummary and de-duplicates daemon-shared sessions into daemonSessions. Set false (or verbose=true) for the full dashboard-grade payload." },
2313
- verbose: { type: "boolean", description: "Force the full payload; overrides compact." }
2314
- }
1281
+ };
1282
+ var MESH_RESTART_DAEMON_TOOL = {
1283
+ name: "mesh_restart_daemon",
1284
+ description: `Update a mesh node's daemon to the latest published version on its release channel and restart it \u2014 the same path as the dashboard "preview update" button, exposed as a mesh command so a coordinator can roll a worker daemon onto a freshly deployed version without a manual restart round-trip. No agent session is launched. Idle-gated: a node whose daemon has an active session (generating / waiting_approval / starting) is refused with code "blocking_sessions" so an in-flight turn is never interrupted. If the node is already on the latest version it is a no-op (no restart), matching the dashboard button (returns alreadyLatest:true). Targets a single node \u2014 call other (idle) nodes first; restarting the coordinator's OWN daemon is naturally refused while its calling turn is active. Passing channel switches the daemon's release channel (and server URL) before restarting; omit it to keep the daemon on its configured channel.`,
1285
+ inputSchema: {
1286
+ type: "object",
1287
+ properties: {
1288
+ node_id: { type: "string", description: "Target node ID \u2014 the daemon that owns this node is updated and restarted." },
1289
+ channel: { type: "string", enum: ["stable", "preview"], description: "Optional release channel to update from. Defaults to the daemon's configured updateChannel. Setting it also repoints the daemon's server URL to that channel." }
1290
+ },
1291
+ required: ["node_id"]
2315
1292
  }
2316
1293
  };
2317
- var MESH_LIST_NODES_TOOL = {
2318
- name: "mesh_list_nodes",
2319
- description: "List all nodes in the mesh with their capabilities, platform, and workspace paths.",
1294
+ var MESH_CHECKPOINT_TOOL = {
1295
+ name: "mesh_checkpoint",
1296
+ description: "Create a git checkpoint (commit) on a mesh node workspace.",
2320
1297
  inputSchema: {
2321
1298
  type: "object",
2322
1299
  properties: {
2323
- _gemini_compat: { type: "string", description: "Dummy property for Gemini compatibility. Ignore this." }
2324
- }
1300
+ node_id: { type: "string", description: "Target node ID." },
1301
+ message: { type: "string", description: "Checkpoint commit message." }
1302
+ },
1303
+ required: ["node_id", "message"]
2325
1304
  }
2326
1305
  };
2327
- var MESH_ENQUEUE_TASK_TOOL = {
2328
- name: "mesh_enqueue_task",
2329
- description: "Add a new task to the mesh work queue. Idle nodes will automatically pull and execute tasks from this queue. Use this instead of mesh_send_task when you do not need to target a specific node.",
1306
+ var MESH_MISSION_UPSERT_TOOL = {
1307
+ name: "mesh_mission_upsert",
1308
+ description: "Create or update a persistent mission record so the plan survives coordinator restarts. Create a mission before enqueueing a multi-task batch, attach tasks via mesh_enqueue_task mission_id, and update status to completed/abandoned when the outcome is decided. Progress is derived from task statuses \u2014 there is no separate progress field.",
2330
1309
  inputSchema: {
2331
1310
  type: "object",
2332
1311
  properties: {
2333
- message: { type: "string", description: "The task instruction for the agent." },
2334
- task_mode: { type: "string", enum: ["code_change", "validation", "live_debug_readonly", "launch_app", "convergence"], description: "Optional task-mode contract. live_debug_readonly rejects obvious write/commit/push/deploy/destructive instructions before dispatch." },
2335
- taskMode: { type: "string", enum: ["code_change", "validation", "live_debug_readonly", "launch_app", "convergence"], description: "CamelCase alias for task_mode." },
2336
- requiredTags: { type: "array", items: { type: "string" }, description: "Optional capability tags that every eligible node must have, e.g. os=darwin, provider=codex-cli, gpu." },
2337
- required_tags: { type: "array", items: { type: "string" }, description: "Snake_case alias for requiredTags." },
2338
- target_node_id: { type: "string", description: "Optional: only this node may claim the task. Use to route a queued task to a specific (e.g. freshly cloned) worktree node instead of letting the first idle base node claim it. Takes priority over prefer_worktree." },
2339
- targetNodeId: { type: "string", description: "CamelCase alias for target_node_id." },
2340
- prefer_worktree: { type: "boolean", description: "Optional: when true, route this task to the most recently cloned idle worktree node (avoids the main/base workspace preemptively claiming an isolated task). No-op if no worktree node exists; resolves to a target_node_id when one does." },
2341
- preferWorktree: { type: "boolean", description: "CamelCase alias for prefer_worktree." },
2342
- depends_on: { type: "array", items: { type: "string" }, description: "Task ids that must complete before this task becomes claimable. Cycles are rejected at enqueue." },
2343
- dependsOn: { type: "array", items: { type: "string" }, description: "CamelCase alias for depends_on." },
2344
- mission_id: { type: "string", description: "Mission this task belongs to (mesh_mission record id)." },
2345
- missionId: { type: "string", description: "CamelCase alias for mission_id." }
1312
+ mission_id: { type: "string", description: "Mission id to update. Omit to create a new mission." },
1313
+ title: { type: "string", description: "Short mission title." },
1314
+ goal: { type: "string", description: "Free-text mission goal/definition of done." },
1315
+ status: { type: "string", enum: ["active", "paused", "completed", "abandoned"], description: "Mission lifecycle status. Defaults to active on create." }
2346
1316
  },
2347
- required: ["message"]
1317
+ required: ["title"]
2348
1318
  }
2349
1319
  };
2350
- var MESH_VIEW_QUEUE_TOOL = {
2351
- name: "mesh_view_queue",
2352
- description: "View the mesh work queue with source-of-truth active counts separated from historical completed/failed/cancelled records. Do not repeatedly call this to wait for generating assigned work; wait for pendingCoordinatorEvents/completion events or an explicit user status request.",
1320
+ var MESH_MISSION_LIST_TOOL = {
1321
+ name: "mesh_mission_list",
1322
+ description: 'List missions with their goal, status, and live task progress (total/pending/assigned/completed/failed). Unlike mesh_status (which surfaces live + recent missions), this returns every mission regardless of status by default, so paused/abandoned/completed missions are never hidden. Filter with `status` to scope (e.g. ["paused"] to find paused missions). Compact (default) elides the full goal to a capped preview; pass verbose=true for full goal text. Read-only.',
2353
1323
  inputSchema: {
2354
1324
  type: "object",
2355
1325
  properties: {
2356
1326
  status: {
2357
1327
  type: "array",
2358
- items: { type: "string" },
2359
- description: "Explicit row filter by task status: pending, assigned, completed, failed, cancelled. Source-of-truth counts remain unfiltered; visible* counts describe returned rows."
2360
- },
2361
- view: {
2362
- type: "string",
2363
- enum: ["all", "active", "historical"],
2364
- description: "Optional row view. active returns pending/assigned rows, historical returns completed/failed/cancelled rows, all returns every persisted queue row. Defaults to all for compatibility."
1328
+ items: { type: "string", enum: ["active", "paused", "completed", "abandoned"] },
1329
+ description: "Optional status filter. Omit to return missions of every status."
2365
1330
  },
2366
- compact: { type: "boolean", description: "Slim payload for LLM callers. Default true. Drops large historical (completed/failed/cancelled) queue row arrays, the full staleDirectWork orphan array (kept as staleDirectWorkSummary counts), and per-row maintenance cleanupCandidates in favor of counts; pending/assigned active rows are retained. Set false (or verbose=true) for the full dashboard-grade payload." },
2367
- verbose: { type: "boolean", description: "Force the full payload; overrides compact." }
1331
+ verbose: { type: "boolean", description: "Return full goal text instead of a capped preview. Defaults to false (compact)." }
2368
1332
  }
2369
1333
  }
2370
1334
  };
2371
- var MESH_QUEUE_CANCEL_TOOL = {
2372
- name: "mesh_queue_cancel",
2373
- description: "Cancel a pending/assigned/completed/failed mesh queue task without deleting audit history. Use this to retire stale queue items that target dead sessions.",
1335
+ var MESH_APPROVE_TOOL = {
1336
+ name: "mesh_approve",
1337
+ description: "Approve or reject a pending action on a delegated agent session.",
2374
1338
  inputSchema: {
2375
1339
  type: "object",
2376
1340
  properties: {
2377
- task_id: { type: "string", description: "Queue task ID to cancel." },
2378
- reason: { type: "string", description: "Optional operator-visible reason for cancellation." }
1341
+ node_id: { type: "string", description: "Target node ID." },
1342
+ session_id: { type: "string", description: "Agent session ID with pending approval." },
1343
+ action: { type: "string", enum: ["approve", "reject"], description: "Action to take." }
2379
1344
  },
2380
- required: ["task_id"]
1345
+ required: ["node_id", "session_id", "action"]
2381
1346
  }
2382
1347
  };
2383
- var MESH_QUEUE_REQUEUE_TOOL = {
2384
- name: "mesh_queue_requeue",
2385
- description: "Return a mesh queue task to pending for retry. By default clears stale assigned owner and target session so another live session can claim it. When the task has exceeded its retry cap it is auto-failed instead; use force=true to override.",
1348
+ var MESH_CLONE_NODE_TOOL = {
1349
+ name: "mesh_clone_node",
1350
+ description: "Create a new worktree-based node from an existing node for isolated parallel work. Creates a git worktree on a new branch so multiple tasks can run on separate branches simultaneously.",
2386
1351
  inputSchema: {
2387
1352
  type: "object",
2388
1353
  properties: {
2389
- task_id: { type: "string", description: "Queue task ID to requeue." },
2390
- reason: { type: "string", description: "Optional operator-visible reason for requeueing." },
2391
- target_node_id: { type: "string", description: "Optional replacement target node ID." },
2392
- target_session_id: { type: "string", description: "Optional replacement target runtime session ID." },
2393
- clear_target_node: { type: "boolean", description: "When true, remove any existing target node constraint." },
2394
- keep_target_session: { type: "boolean", description: "When true, preserve an existing target session if target_session_id is not provided. Defaults false to avoid stale session targets." },
2395
- force: { type: "boolean", description: "When true, bypass the retry cap and requeue even if maxRetries has been exceeded. Use only for explicit operator recovery." }
1354
+ source_node_id: { type: "string", description: "Node ID to clone from (from mesh_list_nodes)." },
1355
+ branch: { type: "string", description: 'Branch name for the new worktree (e.g. "feat/auth-refactor").' },
1356
+ base_branch: { type: "string", description: "Starting point for the branch (default: current HEAD)." }
2396
1357
  },
2397
- required: ["task_id"]
1358
+ required: ["source_node_id", "branch"]
2398
1359
  }
2399
1360
  };
2400
- var MESH_SEND_TASK_TOOL = {
2401
- name: "mesh_send_task",
2402
- description: "Legacy push-based task assignment. Enqueues a task specifically targeted at a given node. The node will pull it immediately if idle.",
1361
+ var MESH_REMOVE_NODE_TOOL = {
1362
+ name: "mesh_remove_node",
1363
+ description: "Remove a node from the mesh. If the node is a worktree, also cleans up the git worktree and directory. Session cleanup is controlled by mesh policy sessionCleanupOnNodeRemove unless session_cleanup_mode overrides it for this call. The coordinator's own local base node (same machine, NOT a worktree) is protected \u2014 removing it breaks live mesh membership and is rejected unless force:true is passed.",
2403
1364
  inputSchema: {
2404
1365
  type: "object",
2405
1366
  properties: {
2406
- node_id: { type: "string", description: "Target node ID (from mesh_list_nodes)." },
2407
- session_id: { type: "string", description: "Agent session ID on the target node." },
2408
- message: { type: "string", description: "Natural-language task to send to the agent." },
2409
- task_mode: { type: "string", enum: ["code_change", "validation", "live_debug_readonly", "launch_app", "convergence"], description: "Optional task-mode contract. live_debug_readonly rejects obvious write/commit/push/deploy/destructive instructions before local or remote direct dispatch." },
2410
- taskMode: { type: "string", enum: ["code_change", "validation", "live_debug_readonly", "launch_app", "convergence"], description: "CamelCase alias for task_mode." },
2411
- mission_id: { type: "string", description: "Mission this task belongs to (mesh_mission record id). When set, the directly dispatched task is attributed to the mission task aggregates exactly like mesh_enqueue_task, including terminal completion. Omit for an unattributed direct dispatch." },
2412
- missionId: { type: "string", description: "CamelCase alias for mission_id." }
1367
+ node_id: { type: "string", description: "Node ID to remove." },
1368
+ session_cleanup_mode: {
1369
+ type: "string",
1370
+ enum: ["preserve", "stop", "delete_stopped", "stop_and_delete"],
1371
+ description: "Optional override for cleanup of delegated sessions attached to this node. preserve keeps history/processes; stop stops live runtimes only; delete_stopped removes completed transcripts only; stop_and_delete stops live runtimes and deletes records."
1372
+ },
1373
+ force: { type: "boolean", description: "Override the coordinator-base-node guard. Only set true to intentionally tear down this mesh; the coordinator must then be re-registered/restarted. Worktree nodes never need force." }
2413
1374
  },
2414
- required: ["node_id", "session_id", "message"]
1375
+ required: ["node_id"]
2415
1376
  }
2416
1377
  };
2417
- var MESH_READ_CHAT_TOOL = {
2418
- name: "mesh_read_chat",
2419
- description: "Read recent chat messages from a delegated agent session on a mesh node. Use compact=true for coordinator context-efficient review: it filters tool/internal/debug chatter and returns the final user-visible summary plus recent key messages. If the runtime session has completed, provider_session_id can explicitly target provider transcript history.",
1378
+ var MESH_CLEANUP_SESSIONS_TOOL = {
1379
+ name: "mesh_cleanup_sessions",
1380
+ description: "Manually clean up delegated session records for a mesh node without removing the node. Defaults should preserve reviewable history unless the caller chooses a mode explicitly.",
2420
1381
  inputSchema: {
2421
1382
  type: "object",
2422
1383
  properties: {
2423
- node_id: { type: "string", description: "Target node ID." },
2424
- session_id: { type: "string", description: "Agent session ID to read from." },
2425
- provider_session_id: { type: "string", description: "Optional provider transcript/session ID for completed sessions." },
2426
- tail: { type: "number", description: "Number of recent messages to return (default: 10)." },
2427
- compact: { type: "boolean", description: "When true, return a compact coordinator summary instead of the full transcript: tool/internal/control/debug messages are excluded and only recent user-visible key messages plus the final assistant summary are included." }
1384
+ node_id: { type: "string", description: "Node ID whose delegated sessions should be considered for cleanup." },
1385
+ mode: {
1386
+ type: "string",
1387
+ enum: ["preserve", "stop", "delete_stopped", "stop_and_delete"],
1388
+ description: "preserve = no-op; stop = release process occupancy by stopping live runtimes; delete_stopped = remove completed/stopped records while leaving live runtimes alone; stop_and_delete = stop live runtimes and delete records."
1389
+ },
1390
+ session_ids: {
1391
+ type: "array",
1392
+ items: { type: "string" },
1393
+ description: "Optional explicit session IDs to limit cleanup to. When omitted, sessions are matched by node/workspace metadata."
1394
+ },
1395
+ dry_run: { type: "boolean", description: "Preview matched/stopped/deleted/skipped session IDs without mutating session-host state." }
2428
1396
  },
2429
- required: ["node_id", "session_id"]
1397
+ required: ["node_id", "mode"]
2430
1398
  }
2431
1399
  };
2432
- var MESH_READ_DEBUG_TOOL = {
2433
- name: "mesh_read_debug",
2434
- description: "Collect a daemon-side chat/parser debug bundle for a delegated agent session on a mesh node without opening the browser UI. Defaults to daemon_file delivery and returns a saved bundle locator.",
1400
+ var MESH_TASK_HISTORY_TOOL = {
1401
+ name: "mesh_task_history",
1402
+ description: "Read the task ledger for this mesh \u2014 dispatched tasks, completions, failures, checkpoints, and node lifecycle events. Use to understand what has been done before deciding next steps, to detect repeated failures, and to inform recovery decisions.",
2435
1403
  inputSchema: {
2436
1404
  type: "object",
2437
1405
  properties: {
2438
- node_id: { type: "string", description: "Target node ID." },
2439
- session_id: { type: "string", description: "Agent session ID to debug." },
2440
- provider_session_id: { type: "string", description: "Optional provider transcript/session ID for completed session history." },
2441
- tail: { type: "number", description: "Number of recent read_chat messages to embed (default: 40)." },
2442
- delivery: { type: "string", enum: ["daemon_file", "inline"], description: "daemon_file saves the full sanitized bundle on the daemon; inline returns it directly. Default: daemon_file." }
2443
- },
2444
- required: ["node_id", "session_id"]
1406
+ tail: { type: "number", description: "Number of recent entries to return (default: 20; clamped to 40 in compact mode, 200 in verbose)." },
1407
+ kind: { type: "string", description: "Filter by entry kind: task_dispatched, task_completed, task_failed, task_stalled, session_launched, checkpoint_created, node_cloned, node_removed, direct_fast_forward." },
1408
+ compact: { type: "boolean", description: "Slim payload for LLM callers. Default true. Truncates long payload strings (message/taskSummary \u2264200, finalSummary \u2264300) and elides any large nested evidence blob (>2KB serialized \u2014 e.g. validationSummary/result/patchEquivalence/submoduleReachability) to a {_elided,_kind,_bytes,_hint} placeholder; full evidence stays accessible via mesh_reconcile_ledger. Set false (or verbose=true) for full untruncated payloads." },
1409
+ verbose: { type: "boolean", description: "Force the full untruncated payload; overrides compact." }
1410
+ }
2445
1411
  }
2446
1412
  };
2447
- var MESH_LAUNCH_SESSION_TOOL = {
2448
- name: "mesh_launch_session",
2449
- description: "Launch a new agent session on a mesh node. Returns the session ID for subsequent send_task/read_chat calls. If the user names a provider, preserve it exactly: Hermes = hermes-cli, Claude Code/Claude = claude-cli, Codex = codex-cli, Gemini = gemini-cli. If type is omitted, resolve strictly from the node policy providerPriority and provider detection; fail closed when no configured provider is usable. Do not default to claude-cli.",
1413
+ var MESH_RECORD_NOTE_TOOL = {
1414
+ name: "mesh_record_note",
1415
+ description: "Record a durable operating note for this mesh \u2014 a runtime-accumulated lesson that future coordinators inherit. Unlike Claude-only memory/CLAUDE.md, this is provider-neutral: it persists in the mesh ledger and is injected into every coordinator's system prompt at launch (codex, hermes, antigravity, claude alike). Use it when you learn something durable: a provider quirk, a pattern to avoid, or a recovery lesson. Keep each note to one concrete, reusable fact. Not for transient task status \u2014 use missions/checkpoints for that.",
2450
1416
  inputSchema: {
2451
1417
  type: "object",
2452
1418
  properties: {
2453
- node_id: { type: "string", description: "Target node ID." },
2454
- type: { type: "string", description: "Optional provider type to launch. Use hermes-cli for Hermes, claude-cli for Claude Code, codex-cli for Codex, gemini-cli for Gemini. When omitted, node.policy.providerPriority is probed in order." }
1419
+ text: { type: "string", description: "The note \u2014 one concrete, reusable operating fact/lesson. Phrase it so a future coordinator can act on it without this conversation's context." },
1420
+ category: {
1421
+ type: "string",
1422
+ enum: ["provider_quirk", "pattern_to_avoid", "recovery_lesson"],
1423
+ description: "Optional classification: provider_quirk (a provider/runtime behaves unexpectedly), pattern_to_avoid (an approach that caused problems), recovery_lesson (how a failure was recovered)."
1424
+ }
2455
1425
  },
2456
- required: ["node_id"]
1426
+ required: ["text"]
2457
1427
  }
2458
1428
  };
2459
- var MESH_GIT_STATUS_TOOL = {
2460
- name: "mesh_git_status",
2461
- description: "Get git status for a mesh node workspace \u2014 branch, dirty state, changed files.",
1429
+ var MESH_RECONCILE_LEDGER_TOOL = {
1430
+ name: "mesh_reconcile_ledger",
1431
+ description: "Reconcile daemon-local mesh ledgers by querying bounded ledger slices over P2P/DataChannel and importing missing entries into the coordinator local JSONL ledger. Cloud/D1 is not used as a ledger source of truth.",
2462
1432
  inputSchema: {
2463
1433
  type: "object",
2464
1434
  properties: {
2465
- node_id: { type: "string", description: "Target node ID." }
2466
- },
2467
- required: ["node_id"]
1435
+ node_ids: { type: "array", items: { type: "string" }, description: "Optional node IDs to query. Defaults to all mesh nodes." },
1436
+ limit: { type: "number", description: "Bounded slice size per node. Defaults to 100 and is clamped by daemon-core." },
1437
+ after_id: { type: "string", description: "Optional cursor entry ID; remote slices return entries strictly after this ID when present." },
1438
+ since: { type: "string", description: "Optional ISO timestamp lower bound for queried entries." },
1439
+ import_entries: { type: "boolean", description: "When false, query and report evidence without importing remote entries. Defaults true." }
1440
+ }
2468
1441
  }
2469
1442
  };
2470
- var MESH_READ_NODE_LOGS_TOOL = {
2471
- name: "mesh_read_node_logs",
2472
- description: "Fetch a recent daemon LOG tail directly from a (possibly remote) mesh node over P2P \u2014 no session launch, no PowerShell/shell grep on the remote machine. Use this to debug a node's daemon: read its error/warn lines, grep for a pattern, or read since a timestamp. The reply is byte-bounded (\u2264128KB, default 64KB; truncated:true when the file was larger, newest lines kept) and secrets (API keys, machine secrets, bearer tokens, JWTs, TURN credentials) are redacted before transmission. This reads the DAEMON log, not an agent session transcript \u2014 for a session transcript use mesh_read_chat / mesh_read_debug.",
1443
+ var MESH_PRUNE_STALE_DIRECT_TOOL = {
1444
+ name: "mesh_prune_stale_direct",
1445
+ description: "Prune orphaned staleDirect dispatch records \u2014 direct task dispatches whose original node/session is no longer present in the live mesh. dry_run (default) reports exactly which records would be pruned without mutating anything; pass execute=true to delete them. Active/pending/assigned/generating work and fresh unacknowledged dispatch failures (node/session still live) are always preserved. The append-only mesh ledger audit history is left intact.",
2473
1446
  inputSchema: {
2474
1447
  type: "object",
2475
1448
  properties: {
2476
- node_id: { type: "string", description: "Target node ID (the daemon owning it serves its own log)." },
2477
- grep: { type: "string", description: "Optional regex (case-insensitive) \u2014 only matching log lines are returned. Invalid regex falls back to a literal substring match." },
2478
- since_ms: { type: "number", description: "Optional epoch-ms floor \u2014 only log lines at/after this time are returned (lines without a parseable timestamp are kept)." },
2479
- tail_bytes: { type: "number", description: "Max bytes of log tail to read (default 65536, capped at 131072). Larger files are truncated to the newest tail_bytes." },
2480
- date: { type: "string", description: "Optional YYYY-MM-DD log date (defaults to today). Falls back to the size-rotation backup when the active file is absent." }
2481
- },
2482
- required: ["node_id"]
1449
+ execute: { type: "boolean", description: "When true, actually delete the orphaned records. Defaults false (dry run). Ignored when dry_run=true." },
1450
+ dry_run: { type: "boolean", description: "Force a preview without mutation even if execute=true. Defaults to dry-run behavior when execute is not set." },
1451
+ include_terminal: { type: "boolean", description: "Also prune terminal (completed/failed) direct dispatch store rows in addition to orphans. Defaults false." }
1452
+ }
2483
1453
  }
2484
1454
  };
2485
- var MESH_FAST_FORWARD_NODE_TOOL = {
2486
- name: "mesh_fast_forward_node",
2487
- description: 'Safely dry-run or execute an obvious direct fast-forward for a mesh node without launching an agent session. mode="merge" (default) absorbs upstream commits into the local branch via git merge --ff-only (ahead=0, behind>0). mode="push" publishes local commits to origin via a strict ff-only push (HEAD must be a descendant of origin/<branch>). Defaults to dry-run; execution requires execute=true. Never force-pushes, rebases, resets, cleans, or checks out arbitrary revisions. When the merge path finds the branch ahead with nothing to merge, it returns code "ahead_needs_push" pointing at mode="push".',
1455
+ var MESH_REFINE_NODE_TOOL = {
1456
+ name: "mesh_refine_node",
1457
+ description: "The Refinery: validate \u2192 merge \u2192 push \u2192 clean up a completed worktree node onto the base branch. Defaults to dry-run (plan only): returns the validation plan with mergeWillRun:false/cleanupWillRun:false and performs NO merge/push/cleanup. Pass execute=true to actually converge the node. execute=true is async: the immediate response includes async:true, status:'accepted', jobId, interactionId, target node, and startedAt; completion/failure evidence is delivered through pending mesh events and the mesh task ledger. dry_run=true overrides execute. Matches the mesh_refine_batch / mesh_fast_forward_node dry_run/execute contract.",
2488
1458
  inputSchema: {
2489
1459
  type: "object",
2490
1460
  properties: {
2491
- node_id: { type: "string", description: "Target node ID." },
2492
- mode: { type: "string", enum: ["merge", "push"], description: "merge (default): git merge --ff-only to absorb upstream. push: strict ff-only push of local commits to origin/<branch>; refuses any non-fast-forward." },
2493
- branch: { type: "string", description: "Optional guard: require the node's current branch to match this branch before planning/executing." },
2494
- execute: { type: "boolean", description: "When true, apply the fast-forward/push if all safety gates pass. Defaults false/dry-run." },
2495
- dry_run: { type: "boolean", description: "Preview only. Defaults true unless execute=true; dry_run=true overrides execute." },
2496
- update_submodules: { type: "boolean", description: 'mode="merge" only: when true, if the root fast-forward changes gitlinks, run only git submodule update --init --recursive and verify submodules clean.' },
2497
- push_submodules: { type: "boolean", description: 'mode="push" only: also ff-only push submodule HEADs to their origin main. Gated by mesh policy allowAutoPublishSubmoduleMainCommits \u2014 skipped unless that policy is enabled. Defaults false (root push only).' }
1461
+ node_id: { type: "string", description: "Node ID of the completed worktree node to refine and merge." },
1462
+ execute: { type: "boolean", description: "When true, run validation/merge/push/cleanup for this node. Defaults false/dry-run." },
1463
+ dry_run: { type: "boolean", description: "Preview the validation plan without merging. Defaults true unless execute=true; dry_run=true overrides execute." }
2498
1464
  },
2499
1465
  required: ["node_id"]
2500
1466
  }
2501
1467
  };
2502
- var MESH_RESTART_DAEMON_TOOL = {
2503
- name: "mesh_restart_daemon",
2504
- description: `Update a mesh node's daemon to the latest published version on its release channel and restart it \u2014 the same path as the dashboard "preview update" button, exposed as a mesh command so a coordinator can roll a worker daemon onto a freshly deployed version without a manual restart round-trip. No agent session is launched. Idle-gated: a node whose daemon has an active session (generating / waiting_approval / starting) is refused with code "blocking_sessions" so an in-flight turn is never interrupted. If the node is already on the latest version it is a no-op (no restart), matching the dashboard button (returns alreadyLatest:true). Targets a single node \u2014 call other (idle) nodes first; restarting the coordinator's OWN daemon is naturally refused while its calling turn is active. Passing channel switches the daemon's release channel (and server URL) before restarting; omit it to keep the daemon on its configured channel.`,
1468
+ var MESH_REFINE_BATCH_TOOL = {
1469
+ name: "mesh_refine_batch",
1470
+ description: "Batch Refinery: converge multiple sibling worktree nodes onto the base branch in one conflict-aware sequential pipeline. Orders nodes by change-area (non-submodule nodes first, submodule-touching nodes serialized last) so each merged sibling advances the base and the next node auto-rebases + re-checks patch-equivalence before its own merge. Each node runs the same validation/patch-equivalence/submodule-reachability/merge/cleanup gates as mesh_refine_node. Conflicting or blocked nodes are isolated as blocked_review while the rest of the batch proceeds. Defaults to dry-run (plan only); set execute=true to converge. Never force-pushes or resets. execute=true is async: the immediate response is async:true / status:'accepted' with the batch jobId and ordered target node list; per-node convergence runs in the background and the aggregate completion/failure (with per-node merged / blocked_review / not_mergeable results) is delivered as a terminal refine event via pending mesh events and the ledger \u2014 do not re-invoke while a batch is in flight. dry_run returns the plan synchronously.",
2505
1471
  inputSchema: {
2506
1472
  type: "object",
2507
1473
  properties: {
2508
- node_id: { type: "string", description: "Target node ID \u2014 the daemon that owns this node is updated and restarted." },
2509
- channel: { type: "string", enum: ["stable", "preview"], description: "Optional release channel to update from. Defaults to the daemon's configured updateChannel. Setting it also repoints the daemon's server URL to that channel." }
1474
+ node_ids: {
1475
+ type: "array",
1476
+ items: { type: "string" },
1477
+ description: "Optional explicit node IDs to converge, in any order (the tool computes the safe merge order). When omitted, all local worktree nodes that need convergence are auto-collected."
1478
+ },
1479
+ execute: { type: "boolean", description: "When true, run validation/rebase/merge for each node in order. Defaults false/dry-run." },
1480
+ dry_run: { type: "boolean", description: "Preview the ordering + per-node validation plan without executing. Defaults true unless execute=true; dry_run=true overrides execute." }
2510
1481
  },
2511
- required: ["node_id"]
1482
+ required: []
2512
1483
  }
2513
1484
  };
2514
- var MESH_CHECKPOINT_TOOL = {
2515
- name: "mesh_checkpoint",
2516
- description: "Create a git checkpoint (commit) on a mesh node workspace.",
1485
+ var MESH_REFINE_CONFIG_SCHEMA_TOOL = {
1486
+ name: "mesh_refine_config_schema",
1487
+ description: "Return the Repo Mesh Refinery config JSON schema and supported repo-local config locations. This is the validation source of truth; heuristic command detection is suggestions-only.",
1488
+ inputSchema: { type: "object", properties: {} }
1489
+ };
1490
+ var MESH_VALIDATE_REFINE_CONFIG_TOOL = {
1491
+ name: "mesh_validate_refine_config",
1492
+ description: "Validate the repo mesh/refine config for a node/workspace without running validation commands or merging.",
2517
1493
  inputSchema: {
2518
1494
  type: "object",
2519
1495
  properties: {
2520
- node_id: { type: "string", description: "Target node ID." },
2521
- message: { type: "string", description: "Checkpoint commit message." }
2522
- },
2523
- required: ["node_id", "message"]
1496
+ node_id: { type: "string", description: "Optional node/workspace whose refine config should be loaded. Defaults to the first mesh node." },
1497
+ config: { type: "object", description: "Optional inline config object to validate instead of loading from the repo." }
1498
+ }
2524
1499
  }
2525
1500
  };
2526
- var MESH_MISSION_UPSERT_TOOL = {
2527
- name: "mesh_mission_upsert",
2528
- description: "Create or update a persistent mission record so the plan survives coordinator restarts. Create a mission before enqueueing a multi-task batch, attach tasks via mesh_enqueue_task mission_id, and update status to completed/abandoned when the outcome is decided. Progress is derived from task statuses \u2014 there is no separate progress field.",
1501
+ var MESH_SUGGEST_REFINE_CONFIG_TOOL = {
1502
+ name: "mesh_suggest_refine_config",
1503
+ description: "Suggest a repo mesh/refine config scaffold from project context/package scripts. Suggestions are never executed until saved as explicit refine config.",
2529
1504
  inputSchema: {
2530
1505
  type: "object",
2531
1506
  properties: {
2532
- mission_id: { type: "string", description: "Mission id to update. Omit to create a new mission." },
2533
- title: { type: "string", description: "Short mission title." },
2534
- goal: { type: "string", description: "Free-text mission goal/definition of done." },
2535
- status: { type: "string", enum: ["active", "paused", "completed", "abandoned"], description: "Mission lifecycle status. Defaults to active on create." }
2536
- },
2537
- required: ["title"]
1507
+ node_id: { type: "string", description: "Optional node/workspace used for suggestions. Defaults to the first mesh node." }
1508
+ }
2538
1509
  }
2539
1510
  };
2540
- var MESH_MISSION_LIST_TOOL = {
2541
- name: "mesh_mission_list",
2542
- description: 'List missions with their goal, status, and live task progress (total/pending/assigned/completed/failed). Unlike mesh_status (which surfaces live + recent missions), this returns every mission regardless of status by default, so paused/abandoned/completed missions are never hidden. Filter with `status` to scope (e.g. ["paused"] to find paused missions). Compact (default) elides the full goal to a capped preview; pass verbose=true for full goal text. Read-only.',
1511
+ var MESH_CHANGE_IMPACT_CONFIG_SCHEMA_TOOL = {
1512
+ name: "mesh_change_impact_config_schema",
1513
+ description: "Return the Change Impact config JSON schema and supported repo-local config locations. Change Impact config declaratively classifies which package/file changes between the live daemon build and workspace HEAD require a daemon rebuild/restart vs. a web-only redeploy vs. nothing. Declarative only \u2014 config is parsed, never executed.",
1514
+ inputSchema: { type: "object", properties: {} }
1515
+ };
1516
+ var MESH_VALIDATE_CHANGE_IMPACT_CONFIG_TOOL = {
1517
+ name: "mesh_validate_change_impact_config",
1518
+ description: "Validate a Change Impact config for a node/workspace and report valid/errors. Loads .adhdev/change-impact.{json,yaml,yml} (or repo-mesh-change-impact.* alias) from the repo unless an inline config is provided.",
2543
1519
  inputSchema: {
2544
1520
  type: "object",
2545
1521
  properties: {
2546
- status: {
2547
- type: "array",
2548
- items: { type: "string", enum: ["active", "paused", "completed", "abandoned"] },
2549
- description: "Optional status filter. Omit to return missions of every status."
2550
- },
2551
- verbose: { type: "boolean", description: "Return full goal text instead of a capped preview. Defaults to false (compact)." }
1522
+ node_id: { type: "string", description: "Optional node/workspace whose change-impact config should be loaded. Defaults to the first mesh node." },
1523
+ config: { type: "object", description: "Optional inline config object to validate instead of loading from the repo." }
2552
1524
  }
2553
1525
  }
2554
1526
  };
2555
- var MESH_APPROVE_TOOL = {
2556
- name: "mesh_approve",
2557
- description: "Approve or reject a pending action on a delegated agent session.",
1527
+ var MESH_SUGGEST_CHANGE_IMPACT_CONFIG_TOOL = {
1528
+ name: "mesh_suggest_change_impact_config",
1529
+ description: "Suggest a Change Impact config scaffold from the repo package layout (web-* \u2192 web-only, others \u2192 daemon-runtime, plus docs/license markers as non-runtime). Heuristic scaffold only \u2014 the draft must be reviewed and saved before it takes effect; nothing is executed.",
2558
1530
  inputSchema: {
2559
1531
  type: "object",
2560
1532
  properties: {
2561
- node_id: { type: "string", description: "Target node ID." },
2562
- session_id: { type: "string", description: "Agent session ID with pending approval." },
2563
- action: { type: "string", enum: ["approve", "reject"], description: "Action to take." }
1533
+ node_id: { type: "string", description: "Optional node/workspace used for suggestions. Defaults to the first mesh node." }
1534
+ }
1535
+ }
1536
+ };
1537
+ var MESH_INIT_TOOL = {
1538
+ name: "mesh_init",
1539
+ description: "One-click mesh onboarding for an existing git project. Detects installed CLI providers, suggests Refinery (.adhdev/refine.json) and worktree bootstrap (.adhdev/worktree_bootstrap.json) configs, optionally writes them to disk, and recommends a node providerPriority from the detected providers. Suggestions are scaffold only and never execute until saved; providerPriority is a recommendation to apply to node policy, not auto-applied. Defaults to dry-run (no files written) and never overwrites an existing config unless overwrite=true.",
1540
+ inputSchema: {
1541
+ type: "object",
1542
+ properties: {
1543
+ node_id: { type: "string", description: "Optional node/workspace to onboard. Defaults to the first mesh node with a workspace." },
1544
+ write: { type: "boolean", description: "When true, persist the suggested configs to disk. Defaults false (dry-run preview only)." },
1545
+ overwrite: { type: "boolean", description: "When true, overwrite an existing config file. Defaults false (never clobber an existing refine/bootstrap config)." }
1546
+ }
1547
+ }
1548
+ };
1549
+ var MESH_REFINE_PLAN_TOOL = {
1550
+ name: "mesh_refine_plan",
1551
+ description: "Dry-run Refinery plan for a worktree node: reports config source, validation commands, suggestions/unavailable reason, and merge/cleanup intent without executing validation or git merge.",
1552
+ inputSchema: {
1553
+ type: "object",
1554
+ properties: {
1555
+ node_id: { type: "string", description: "Node ID of the worktree node to plan." }
2564
1556
  },
2565
- required: ["node_id", "session_id", "action"]
1557
+ required: ["node_id"]
1558
+ }
1559
+ };
1560
+ var MESH_REVIEW_INBOX_TOOL = {
1561
+ name: "mesh_review_inbox",
1562
+ description: "List local worktree nodes that need human review: merge candidates (pushed feature branches ready to merge) and Refinery-blocked review results. Returns evidence summaries, diff stats vs. the default branch, and suggested actions (Refine / Requeue / Dismiss). Remote nodes are excluded in M4.0.",
1563
+ inputSchema: {
1564
+ type: "object",
1565
+ properties: {
1566
+ mesh_id: { type: "string", description: "Mesh ID (optional \u2014 inferred from active mesh if omitted)." }
1567
+ },
1568
+ required: []
1569
+ }
1570
+ };
1571
+ var ALL_MESH_TOOLS = [
1572
+ MESH_STATUS_TOOL,
1573
+ MESH_LIST_NODES_TOOL,
1574
+ MESH_ENQUEUE_TASK_TOOL,
1575
+ MESH_VIEW_QUEUE_TOOL,
1576
+ MESH_QUEUE_CANCEL_TOOL,
1577
+ MESH_QUEUE_REQUEUE_TOOL,
1578
+ MESH_SEND_TASK_TOOL,
1579
+ MESH_READ_CHAT_TOOL,
1580
+ MESH_READ_DEBUG_TOOL,
1581
+ MESH_LAUNCH_SESSION_TOOL,
1582
+ MESH_GIT_STATUS_TOOL,
1583
+ MESH_READ_NODE_LOGS_TOOL,
1584
+ MESH_FAST_FORWARD_NODE_TOOL,
1585
+ MESH_RESTART_DAEMON_TOOL,
1586
+ MESH_CHECKPOINT_TOOL,
1587
+ MESH_APPROVE_TOOL,
1588
+ MESH_CLONE_NODE_TOOL,
1589
+ MESH_REMOVE_NODE_TOOL,
1590
+ MESH_REFINE_NODE_TOOL,
1591
+ MESH_REFINE_BATCH_TOOL,
1592
+ MESH_REFINE_CONFIG_SCHEMA_TOOL,
1593
+ MESH_VALIDATE_REFINE_CONFIG_TOOL,
1594
+ MESH_SUGGEST_REFINE_CONFIG_TOOL,
1595
+ MESH_CHANGE_IMPACT_CONFIG_SCHEMA_TOOL,
1596
+ MESH_VALIDATE_CHANGE_IMPACT_CONFIG_TOOL,
1597
+ MESH_SUGGEST_CHANGE_IMPACT_CONFIG_TOOL,
1598
+ MESH_INIT_TOOL,
1599
+ MESH_REFINE_PLAN_TOOL,
1600
+ MESH_CLEANUP_SESSIONS_TOOL,
1601
+ MESH_PRUNE_STALE_DIRECT_TOOL,
1602
+ MESH_TASK_HISTORY_TOOL,
1603
+ MESH_RECORD_NOTE_TOOL,
1604
+ MESH_RECONCILE_LEDGER_TOOL,
1605
+ MESH_MISSION_UPSERT_TOOL,
1606
+ MESH_MISSION_LIST_TOOL,
1607
+ MESH_REVIEW_INBOX_TOOL
1608
+ ];
1609
+
1610
+ // src/tools/mesh-tools.ts
1611
+ var SESSION_PROVIDER_METADATA_TTL_MS = 30 * 6e4;
1612
+ var meshSessionProviderMetadata = /* @__PURE__ */ new Map();
1613
+ function getSessionMetadata(key) {
1614
+ const entry = meshSessionProviderMetadata.get(key);
1615
+ if (!entry) return void 0;
1616
+ if (entry.expiresAt <= Date.now()) {
1617
+ meshSessionProviderMetadata.delete(key);
1618
+ return void 0;
1619
+ }
1620
+ return entry;
1621
+ }
1622
+ var ACTIVE_WORK_POLLING_BACKOFF_MS = 6e4;
1623
+ function buildActiveWorkPollingGuidance(summary, now = Date.now()) {
1624
+ if (!summary || summary.generatingCount <= 0) return void 0;
1625
+ return {
1626
+ activeGeneratingWork: true,
1627
+ generatingCount: summary.generatingCount,
1628
+ doNotPollBefore: new Date(now + ACTIVE_WORK_POLLING_BACKOFF_MS).toISOString(),
1629
+ eventSurface: "pendingCoordinatorEvents",
1630
+ nextRecommendedAction: "Wait for pendingCoordinatorEvents/completion events or an explicit user status request. If no terminal evidence appears and the user asks for status, make one bounded status check, then wait again.",
1631
+ message: "Do not repeatedly poll mesh_status/mesh_view_queue/mesh_read_chat while delegated work is generating; terminal ledger or completion evidence will be surfaced through pendingCoordinatorEvents when available."
1632
+ };
1633
+ }
1634
+ function summarizeTaskMessage(message) {
1635
+ const taskSummary = message.replace(/\s+/g, " ").trim();
1636
+ const taskTitle = taskSummary.length > 96 ? `${taskSummary.slice(0, 93)}...` : taskSummary;
1637
+ return { taskTitle: taskTitle || "(untitled task)", taskSummary };
1638
+ }
1639
+ function buildDirectTaskPayload(message, via, opts) {
1640
+ const descriptor = summarizeTaskMessage(message);
1641
+ return {
1642
+ source: "direct",
1643
+ via,
1644
+ taskId: opts.taskId,
1645
+ message,
1646
+ taskTitle: descriptor.taskTitle,
1647
+ taskSummary: descriptor.taskSummary,
1648
+ ...opts.taskMode ? { taskMode: opts.taskMode } : {},
1649
+ ...opts.providerType ? { providerType: opts.providerType } : {},
1650
+ ...opts.targetSessionId ? { targetSessionId: opts.targetSessionId } : {},
1651
+ ...opts.dispatchedToIdleSession !== void 0 ? { dispatchedToIdleSession: opts.dispatchedToIdleSession } : {}
1652
+ };
1653
+ }
1654
+ function findNode(mesh, nodeId) {
1655
+ const node = mesh.nodes.find((n) => (0, import_daemon_core2.meshNodeIdMatches)(n, nodeId));
1656
+ if (!node) throw new Error(`Node '${nodeId}' is not a member of mesh '${mesh.name}'`);
1657
+ return node;
1658
+ }
1659
+ var DUPLICATE_DISPATCH_WINDOW_MS = 6e4;
1660
+ async function refreshMeshFromDaemon(ctx) {
1661
+ try {
1662
+ const result = await ctx.transport.command("get_mesh", { meshId: ctx.mesh.id });
1663
+ if (!result?.success || !Array.isArray(result.mesh?.nodes)) return;
1664
+ const refreshedNodes = result.mesh.nodes.filter((n) => n?.id).map((n) => n);
1665
+ ctx.mesh.nodes.splice(0, ctx.mesh.nodes.length, ...refreshedNodes);
1666
+ ctx.mesh.updatedAt = result.mesh.updatedAt ?? ctx.mesh.updatedAt;
1667
+ } catch {
1668
+ }
1669
+ }
1670
+ async function syncCoordinatorDaemonMeshCache(ctx) {
1671
+ if (!(ctx.transport instanceof IpcTransport)) return;
1672
+ try {
1673
+ await ctx.transport.command("get_mesh", {
1674
+ meshId: ctx.mesh.id,
1675
+ inlineMesh: ctx.mesh
1676
+ });
1677
+ } catch {
1678
+ }
1679
+ }
1680
+ async function findNodeWithRefresh(ctx, nodeId) {
1681
+ const hit = ctx.mesh.nodes.find((n) => (0, import_daemon_core2.meshNodeIdMatches)(n, nodeId));
1682
+ if (hit && !hit.isLocalWorktree) return hit;
1683
+ await refreshMeshFromDaemon(ctx);
1684
+ const refreshed = ctx.mesh.nodes.find((n) => (0, import_daemon_core2.meshNodeIdMatches)(n, nodeId));
1685
+ if (!refreshed) throw new Error(`Node '${nodeId}' is not a member of mesh '${ctx.mesh.name}'`);
1686
+ return refreshed;
1687
+ }
1688
+ async function findOptionalNodeWithRefresh(ctx, nodeId) {
1689
+ const hit = ctx.mesh.nodes.find((n) => (0, import_daemon_core2.meshNodeIdMatches)(n, nodeId));
1690
+ if (hit && !hit.isLocalWorktree) return hit;
1691
+ await refreshMeshFromDaemon(ctx);
1692
+ return ctx.mesh.nodes.find((n) => (0, import_daemon_core2.meshNodeIdMatches)(n, nodeId)) ?? null;
1693
+ }
1694
+ function hasRecentDuplicateDispatch(ctx, args) {
1695
+ const now = Date.now();
1696
+ const normalizedMessage = args.message.trim();
1697
+ for (const task of (0, import_daemon_core2.getQueue)(ctx.mesh.id)) {
1698
+ const timestamp = new Date(task.updatedAt || task.createdAt).getTime();
1699
+ if (!Number.isFinite(timestamp) || now - timestamp > DUPLICATE_DISPATCH_WINDOW_MS) continue;
1700
+ if (task.targetNodeId && task.targetNodeId !== args.node_id) continue;
1701
+ if (task.assignedNodeId && task.assignedNodeId !== args.node_id) continue;
1702
+ if (args.session_id && task.targetSessionId !== args.session_id && task.assignedSessionId !== args.session_id) continue;
1703
+ if (task.message?.trim() === normalizedMessage) {
1704
+ return { duplicate: true, entry: task, source: "queue" };
1705
+ }
1706
+ }
1707
+ const entries = (0, import_daemon_core2.readLedgerEntries)(ctx.mesh.id, { tail: 200 });
1708
+ for (let i = entries.length - 1; i >= 0; i -= 1) {
1709
+ const entry = entries[i];
1710
+ const timestamp = new Date(entry.timestamp).getTime();
1711
+ if (Number.isFinite(timestamp) && now - timestamp > DUPLICATE_DISPATCH_WINDOW_MS) break;
1712
+ if (entry.kind !== "task_dispatched") continue;
1713
+ if (entry.nodeId !== args.node_id) continue;
1714
+ if (args.session_id && entry.sessionId !== args.session_id) continue;
1715
+ if (typeof entry.payload?.message !== "string") continue;
1716
+ if (entry.payload.message.trim() === normalizedMessage) {
1717
+ return { duplicate: true, entry, source: "ledger" };
1718
+ }
1719
+ }
1720
+ return { duplicate: false };
1721
+ }
1722
+ function buildMissingNodeReadChatRecovery(ctx, args) {
1723
+ const entries = (0, import_daemon_core2.readLedgerEntries)(ctx.mesh.id, { tail: 300 });
1724
+ const relatedEntries = entries.filter((entry) => entry.nodeId === args.node_id || entry.sessionId === args.session_id);
1725
+ const completedEntries = relatedEntries.filter((entry) => entry.kind === "task_completed");
1726
+ const lastDispatch = [...relatedEntries].reverse().find((entry) => entry.kind === "task_dispatched");
1727
+ const lastTerminal = [...relatedEntries].reverse().find((entry) => entry.kind === "task_completed" || entry.kind === "task_failed" || entry.kind === "task_stalled");
1728
+ const lastRemoved = [...relatedEntries].reverse().find((entry) => entry.kind === "node_removed");
1729
+ const lastLaunch = [...relatedEntries].reverse().find((entry) => entry.kind === "session_launched");
1730
+ const providerSessionId = args.provider_session_id || readString(lastTerminal?.payload?.providerSessionId) || readString(lastLaunch?.payload?.providerSessionId) || readString(lastDispatch?.payload?.providerSessionId);
1731
+ const finalSummary = readString(lastTerminal?.payload?.finalSummary) || readString(lastTerminal?.payload?.compactSummary) || readString(lastTerminal?.payload?.summary);
1732
+ const ledger = {
1733
+ taskCompletedFound: completedEntries.length > 0,
1734
+ nodeRemovedFound: !!lastRemoved,
1735
+ providerType: lastTerminal?.providerType || lastLaunch?.providerType || lastDispatch?.providerType,
1736
+ providerSessionId,
1737
+ nodeRemovedAt: lastRemoved?.timestamp,
1738
+ sessionCleanupMode: readString(lastRemoved?.payload?.sessionCleanupMode),
1739
+ readDebugLocator: readString(lastTerminal?.payload?.readDebugLocator) || readString(lastTerminal?.payload?.debugBundlePath)
1740
+ };
1741
+ if (finalSummary) {
1742
+ if (args.compact === true) {
1743
+ return {
1744
+ ...compactChatPayload({
1745
+ success: true,
1746
+ status: "idle",
1747
+ providerSessionId,
1748
+ summary: finalSummary,
1749
+ messages: [{ role: "assistant", content: finalSummary, isHistorical: true }]
1750
+ }, {
1751
+ nodeId: args.node_id,
1752
+ sessionId: args.session_id,
1753
+ limit: args.tail ?? 10
1754
+ }),
1755
+ recoveredFromLedger: true,
1756
+ ledger
1757
+ };
1758
+ }
1759
+ return {
1760
+ success: true,
1761
+ compact: false,
1762
+ recoveredFromLedger: true,
1763
+ nodeId: args.node_id,
1764
+ sessionId: args.session_id,
1765
+ summary: finalSummary,
1766
+ ledger,
1767
+ messages: [{ role: "assistant", content: finalSummary, isHistorical: true }]
1768
+ };
1769
+ }
1770
+ return {
1771
+ success: false,
1772
+ recoverable: true,
1773
+ code: "mesh_removed_node_transcript_unavailable",
1774
+ error: `Node '${args.node_id}' is not a current member of mesh '${ctx.mesh.name}'.`,
1775
+ nodeId: args.node_id,
1776
+ sessionId: args.session_id,
1777
+ providerSessionId,
1778
+ reason: "node_not_in_current_mesh_snapshot",
1779
+ ledger,
1780
+ completedSessionSeenInLedger: ledger.taskCompletedFound,
1781
+ lastDispatch: lastDispatch ? {
1782
+ timestamp: lastDispatch.timestamp,
1783
+ sessionId: lastDispatch.sessionId,
1784
+ providerType: lastDispatch.providerType,
1785
+ taskId: typeof lastDispatch.payload?.taskId === "string" ? lastDispatch.payload.taskId : void 0,
1786
+ messagePreview: typeof lastDispatch.payload?.message === "string" ? lastDispatch.payload.message.slice(0, 500) : void 0
1787
+ } : null,
1788
+ lastTerminalEvent: lastTerminal ? {
1789
+ kind: lastTerminal.kind,
1790
+ timestamp: lastTerminal.timestamp,
1791
+ sessionId: lastTerminal.sessionId,
1792
+ providerType: lastTerminal.providerType,
1793
+ taskId: typeof lastTerminal.payload?.taskId === "string" ? lastTerminal.payload.taskId : void 0,
1794
+ payload: lastTerminal.payload
1795
+ } : null,
1796
+ nextSteps: [
1797
+ providerSessionId ? `Retry mesh_read_chat with provider_session_id='${providerSessionId}' on a current live node for the same daemon if one exists.` : "If the node UI shows a provider transcript id, retry mesh_read_chat/mesh_read_debug with provider_session_id.",
1798
+ "Use mesh_read_debug with the provider_session_id or daemon-side debug bundle locator if available.",
1799
+ "Check mesh_task_history for task_completed and node_removed entries before redispatching; do not resend solely because transcript recovery failed.",
1800
+ "If this node was removed with stop_and_delete, the runtime transcript may be gone; rely on the ledger summary/locator or ask the operator for the saved UI output."
1801
+ ],
1802
+ recoveryHints: [
1803
+ "The worktree/node may have been removed or the mesh snapshot may be stale after task completion.",
1804
+ "If you have a provider_session_id, retry mesh_read_chat with that value while targeting a live node for the same daemon if available.",
1805
+ "Use mesh_read_debug with provider_session_id, or inspect the daemon/session-host history locator if the transcript has already been archived.",
1806
+ "Avoid redispatching the same task solely because read_chat could not recover the transcript; check task_history and git status first."
1807
+ ]
1808
+ };
1809
+ }
1810
+ function isDirectDispatchLedgerEntry(entry) {
1811
+ if (entry?.kind !== "task_dispatched") return false;
1812
+ const payload = entry.payload || {};
1813
+ const via = readString(payload.via);
1814
+ return payload.source === "direct" || via === "p2p_direct" || via === "local_direct" || via === "mesh_send_task";
1815
+ }
1816
+ function readMessageTimestampIso(message) {
1817
+ for (const value of [message?.timestamp, message?.createdAt, message?.created_at, message?.updatedAt, message?.time]) {
1818
+ if (typeof value === "number" && Number.isFinite(value)) {
1819
+ const ms = value > 1e10 ? value : value * 1e3;
1820
+ return new Date(ms).toISOString();
1821
+ }
1822
+ if (typeof value === "string" && value.trim()) {
1823
+ const ms = new Date(value.trim()).getTime();
1824
+ if (Number.isFinite(ms)) return new Date(ms).toISOString();
1825
+ }
1826
+ }
1827
+ return void 0;
1828
+ }
1829
+ function readFinalAssistantTranscriptEvidence(payload) {
1830
+ const rawMessages = Array.isArray(payload?.messages) ? payload.messages : [];
1831
+ const finalAssistant = [...rawMessages].reverse().filter(isCoordinatorVisibleMessage).find((message) => {
1832
+ const role = String(message?.role ?? "").toLowerCase();
1833
+ return (role === "assistant" || role === "agent") && messageContent(message).trim();
1834
+ });
1835
+ const finalSummary = messageContent(finalAssistant).trim() || (typeof payload?.summary === "string" && payload.summary.trim() ? payload.summary.trim() : void 0);
1836
+ return {
1837
+ finalSummary,
1838
+ transcriptMessageAt: finalAssistant ? readMessageTimestampIso(finalAssistant) : void 0
1839
+ };
1840
+ }
1841
+ function findNodeSession(nodes, nodeId, sessionId) {
1842
+ if (!nodeId || !sessionId) return {};
1843
+ const node = nodes.find((candidate) => (0, import_daemon_core2.meshNodeIdMatches)(candidate, nodeId));
1844
+ if (!node) return {};
1845
+ const sessions = Array.isArray(node.sessions) ? node.sessions : [];
1846
+ const session = sessions.find((candidate) => readSessionRecordId(candidate) === sessionId);
1847
+ return { node, session };
1848
+ }
1849
+ function buildDirectDispatchReconciliationCandidates(directDispatches, ledgerEntries) {
1850
+ const candidates = [];
1851
+ const seenTaskIds = /* @__PURE__ */ new Set();
1852
+ for (const dispatch of directDispatches || []) {
1853
+ const taskId = readString(dispatch?.taskId);
1854
+ if (!taskId || seenTaskIds.has(taskId)) continue;
1855
+ seenTaskIds.add(taskId);
1856
+ candidates.push(dispatch);
1857
+ }
1858
+ for (const entry of ledgerEntries || []) {
1859
+ if (!isDirectDispatchLedgerEntry(entry)) continue;
1860
+ const taskId = readString(entry.payload?.taskId);
1861
+ if (!taskId || seenTaskIds.has(taskId)) continue;
1862
+ seenTaskIds.add(taskId);
1863
+ candidates.push({
1864
+ taskId,
1865
+ nodeId: entry.nodeId,
1866
+ sessionId: entry.sessionId,
1867
+ providerType: entry.providerType || readString(entry.payload?.providerType),
1868
+ message: readString(entry.payload?.message),
1869
+ dispatchedAt: entry.timestamp,
1870
+ via: readString(entry.payload?.via)
1871
+ });
1872
+ }
1873
+ return candidates;
1874
+ }
1875
+ async function reconcileDirectDispatchesFromTranscriptEvidence(ctx, liveNodes, directDispatches, ledgerEntries) {
1876
+ let attempted = 0;
1877
+ let reconciled = 0;
1878
+ let skipped = 0;
1879
+ const candidates = buildDirectDispatchReconciliationCandidates(directDispatches, ledgerEntries);
1880
+ for (const dispatch of candidates) {
1881
+ const taskId = readString(dispatch?.taskId);
1882
+ const nodeId = readString(dispatch?.nodeId);
1883
+ const sessionId = readString(dispatch?.sessionId);
1884
+ if (!taskId || !nodeId || !sessionId) {
1885
+ skipped += 1;
1886
+ continue;
1887
+ }
1888
+ const { session } = findNodeSession(liveNodes, nodeId, sessionId);
1889
+ if (!session || !isIdleSessionRecord(session)) {
1890
+ skipped += 1;
1891
+ continue;
1892
+ }
1893
+ const node = await findOptionalNodeWithRefresh(ctx, nodeId).catch(() => null);
1894
+ if (!node) {
1895
+ skipped += 1;
1896
+ continue;
1897
+ }
1898
+ const providerType = readString(dispatch?.providerType) || resolveSessionProviderType(session);
1899
+ const providerSessionId = readString(session?.providerSessionId) || readString(session?.activeChat?.providerSessionId) || readString(session?.settings?.providerSessionId) || resolveMeshSessionProviderMetadata(ctx, nodeId, sessionId)?.providerSessionId;
1900
+ attempted += 1;
1901
+ try {
1902
+ const readResult = await commandForNode(ctx, node, "read_chat", {
1903
+ sessionId,
1904
+ targetSessionId: sessionId,
1905
+ workspace: node.workspace,
1906
+ ...providerType ? { agentType: providerType, providerType } : {},
1907
+ ...providerSessionId ? { providerSessionId } : {},
1908
+ tailLimit: 10
1909
+ });
1910
+ const payload = unwrapCommandPayload(readResult);
1911
+ if (payload?.success === false) continue;
1912
+ const evidence = readFinalAssistantTranscriptEvidence(payload);
1913
+ if (!evidence.finalSummary) continue;
1914
+ const result = (0, import_daemon_core2.reconcileDirectDispatchCompletionFromTranscript)({
1915
+ meshId: ctx.mesh.id,
1916
+ nodeId,
1917
+ sessionId,
1918
+ providerType,
1919
+ providerSessionId: readString(payload?.providerSessionId) || providerSessionId,
1920
+ taskId,
1921
+ finalSummary: evidence.finalSummary,
1922
+ transcriptMessageAt: evidence.transcriptMessageAt,
1923
+ targetCoordinatorDaemonId: ctx.localDaemonId,
1924
+ source: "mcp_mesh_status_transcript_reconciliation"
1925
+ });
1926
+ if (result.reconciled) reconciled += 1;
1927
+ } catch {
1928
+ skipped += 1;
1929
+ }
1930
+ }
1931
+ return { attempted, reconciled, skipped };
1932
+ }
1933
+ async function triggerMeshQueueAndReport(ctx) {
1934
+ try {
1935
+ const raw = await ctx.transport.command("trigger_mesh_queue", { meshId: ctx.mesh.id });
1936
+ const payload = unwrapCommandPayload(raw);
1937
+ const trigger = payload?.trigger && typeof payload.trigger === "object" ? payload.trigger : payload;
1938
+ return trigger && typeof trigger === "object" ? trigger : { success: true };
1939
+ } catch (e) {
1940
+ return {
1941
+ success: false,
1942
+ error: e?.message || String(e)
1943
+ };
1944
+ }
1945
+ }
1946
+ function buildQueueTriggerGuidance(queueTrigger) {
1947
+ if (!queueTrigger || queueTrigger.claimed === true) return void 0;
1948
+ if (queueTrigger.success === false) {
1949
+ return {
1950
+ queueClaimed: false,
1951
+ queueDispatchState: "trigger_failed",
1952
+ nextAction: "Do not assume the queued task is running. Check mesh_view_queue and daemon connectivity before redispatching."
1953
+ };
1954
+ }
1955
+ if (queueTrigger.autoLaunchPending === true) {
1956
+ return {
1957
+ queueClaimed: false,
1958
+ queueDispatchState: "pending_waiting_for_autolaunch",
1959
+ nextAction: "A worker session was just auto-launched for this task and is booting; it will claim the task shortly. Wait for it to claim \u2014 do NOT launch another session. Use mesh_view_queue to confirm the assignment lands."
1960
+ };
1961
+ }
1962
+ if (queueTrigger.noIdleMeshSessionAvailable === true) {
1963
+ return {
1964
+ queueClaimed: false,
1965
+ queueDispatchState: "pending_no_idle_mesh_session",
1966
+ nextAction: "The task is queued but not running. Launch a managed worker with mesh_launch_session, or wait for a delegated session to become ready and trigger the queue again."
1967
+ };
1968
+ }
1969
+ return {
1970
+ queueClaimed: false,
1971
+ queueDispatchState: "pending_or_waiting_for_ready",
1972
+ nextAction: "The task is queued but this trigger did not claim it. Use mesh_view_queue for the current active-work source of truth before retrying."
1973
+ };
1974
+ }
1975
+ function isMeshOwnedDelegateSession(session, meshId, nodeId) {
1976
+ const settings = session?.settings;
1977
+ const sessionMeshId = typeof settings?.meshNodeFor === "string" ? settings.meshNodeFor.trim() : "";
1978
+ const sessionNodeId = typeof settings?.meshNodeId === "string" ? settings.meshNodeId.trim() : "";
1979
+ if (sessionMeshId) {
1980
+ if (sessionMeshId !== meshId) return false;
1981
+ return !sessionNodeId || sessionNodeId === nodeId;
1982
+ }
1983
+ const coordinatorOwned = settings?.launchedByCoordinator === true || Boolean(readString(settings?.meshCoordinatorDaemonId));
1984
+ if (!coordinatorOwned) return false;
1985
+ const lastNodeId = readString(settings?.meshLastNodeId);
1986
+ if (lastNodeId) return lastNodeId === nodeId;
1987
+ return true;
1988
+ }
1989
+ function hasRemoteRelayMetadata(session) {
1990
+ return Boolean(
1991
+ readString(session?.settings?.meshCoordinatorDaemonId) || readString(session?.meta?.meshCoordinatorDaemonId) || readString(session?.metadata?.meshCoordinatorDaemonId) || readString(session?.meshCoordinatorDaemonId)
1992
+ );
1993
+ }
1994
+ function classifyRemoteDelegateRelaySafety(session, meshId, nodeId, coordinatorDaemonId) {
1995
+ if (!isMeshOwnedDelegateSession(session, meshId, nodeId)) return "unsafe_alias";
1996
+ if (hasRemoteRelayMetadata(session)) return "safe";
1997
+ return coordinatorDaemonId ? "self_heal" : "missing_anchor";
1998
+ }
1999
+ function chooseDispatchableSession(sessions, providerType, meshId, nodeId, coordinatorDaemonId) {
2000
+ const live = sessions.filter((session) => !isTerminalSessionRecord(session));
2001
+ const matchingProvider = (session) => !providerType || session?.providerType === providerType || session?.cliType === providerType;
2002
+ const meshSessions = live.filter((session) => {
2003
+ const safety = classifyRemoteDelegateRelaySafety(session, meshId, nodeId, coordinatorDaemonId);
2004
+ return safety === "safe" || safety === "self_heal";
2005
+ });
2006
+ return meshSessions.find((session) => isIdleSessionRecord(session) && matchingProvider(session)) || void 0;
2007
+ }
2008
+ function buildRelayUnsafeRemoteSessionFailure(ctx, node, sessionId, providerType) {
2009
+ return {
2010
+ success: false,
2011
+ recoverable: true,
2012
+ code: "mesh_delegate_session_missing_relay_metadata",
2013
+ reason: "mesh_delegate_session_missing_relay_metadata",
2014
+ transport: "mesh_transport",
2015
+ retryRecommended: true,
2016
+ meshId: ctx.mesh.id,
2017
+ nodeId: node.id,
2018
+ daemonId: node.daemonId,
2019
+ workspace: node.workspace,
2020
+ sessionId,
2021
+ unsafeTranscriptAlias: true,
2022
+ ...providerType ? { resolvedProviderType: providerType } : {},
2023
+ error: `Remote session '${sessionId}' is not relay-safe for mesh '${ctx.mesh.id}': missing meshNodeFor/meshCoordinatorDaemonId metadata, so completion events would not reach the coordinator ledger. This session may be the coordinator itself or an unrelated session (unsafe_transcript_alias risk).`,
2024
+ nextAction: `Launch a fresh relay-safe session with mesh_launch_session(node_id: '${node.id}'${providerType ? `, type: '${providerType}'` : ""}) or dispatch without session_id so Repo Mesh can choose a valid delegate session.`,
2025
+ noFallbackReason: "Blindly reusing a remote session without mesh relay metadata would silently drop task_completed / generating_completed events."
2026
+ };
2027
+ }
2028
+ function buildMissingCoordinatorDaemonIdFailure(ctx, node, providerType) {
2029
+ return {
2030
+ success: false,
2031
+ recoverable: true,
2032
+ code: "mesh_coordinator_daemon_unknown",
2033
+ reason: "mesh_coordinator_daemon_unknown",
2034
+ transport: "mesh_transport",
2035
+ retryRecommended: true,
2036
+ meshId: ctx.mesh.id,
2037
+ nodeId: node.id,
2038
+ daemonId: node.daemonId,
2039
+ workspace: node.workspace,
2040
+ ...providerType ? { resolvedProviderType: providerType } : {},
2041
+ error: `Cannot launch a remote mesh delegate for node '${node.id}': coordinator daemon identity is unavailable, so the worker would be unable to relay completion events back to the coordinator.`,
2042
+ nextAction: "Retry after the coordinator daemon identity is available (for example from an attached daemon-backed MCP session) so meshCoordinatorDaemonId can be stamped on the worker session.",
2043
+ noFallbackReason: "Launching without meshCoordinatorDaemonId would create a worker session that can finish work but cannot emit task_completed / generating_completed back to the coordinator."
2044
+ };
2045
+ }
2046
+ function findNestedPayload(value, predicate) {
2047
+ const seen = /* @__PURE__ */ new Set();
2048
+ const stack = [{ payload: value, depth: 0 }];
2049
+ while (stack.length) {
2050
+ const { payload, depth } = stack.pop();
2051
+ if (predicate(payload)) return payload;
2052
+ if (!payload || typeof payload !== "object" || seen.has(payload) || depth >= 8) continue;
2053
+ seen.add(payload);
2054
+ for (const key of ["payload", "result"]) {
2055
+ if (key in payload) stack.push({ payload: payload[key], depth: depth + 1 });
2056
+ }
2057
+ }
2058
+ return value;
2059
+ }
2060
+ function extractCloneNodePayload(value) {
2061
+ return findNestedPayload(value, (payload) => Boolean(payload?.node?.id));
2062
+ }
2063
+ function extractGitStatus(value) {
2064
+ const payload = unwrapCommandPayload(value);
2065
+ return payload?.status ?? value?.status ?? payload;
2066
+ }
2067
+ function extractGitDiff(value) {
2068
+ const payload = unwrapCommandPayload(value);
2069
+ return payload?.diffSummary ?? payload?.diff ?? value?.diffSummary ?? value?.diff ?? payload;
2070
+ }
2071
+ function extractSubmodules(value, ignorePaths) {
2072
+ const payload = unwrapCommandPayload(value);
2073
+ const subs = payload?.status?.submodules ?? payload?.submodules ?? value?.status?.submodules ?? value?.submodules;
2074
+ if (!Array.isArray(subs)) return void 0;
2075
+ if (ignorePaths.length === 0) return subs;
2076
+ const ignoreSet = new Set(ignorePaths);
2077
+ return subs.filter((s) => s?.path && !ignoreSet.has(s.path));
2078
+ }
2079
+ function assignFullGitSnapshot(entry, status) {
2080
+ if (!status || typeof status !== "object" || Array.isArray(status)) return;
2081
+ entry.git = status;
2082
+ }
2083
+ var COMPACT_DETAILED_NODES_BYTE_BUDGET = 9e3;
2084
+ var COMPACT_NODES_TOTAL_BYTE_BUDGET = 13e3;
2085
+ var COMPACT_MISSIONS_BYTE_BUDGET = 6e3;
2086
+ function extractLaunchPayload(value) {
2087
+ return findNestedPayload(value, (payload) => Boolean(payload?.sessionId || payload?.id || payload?.runtimeSessionId));
2088
+ }
2089
+ function classifyMeshLaunchFailure(error) {
2090
+ const message = error instanceof Error ? error.message : String(error || "launch failed");
2091
+ const lower = message.toLowerCase();
2092
+ const p2pClassification = (0, import_daemon_core2.classifyP2pRelayFailure)(error, { command: "launch_cli" });
2093
+ if (p2pClassification.recoverable) {
2094
+ return p2pClassification;
2095
+ }
2096
+ if (lower.includes("cannot connect to daemon ipc") || lower.includes("daemon ipc command")) {
2097
+ return {
2098
+ code: "local_ipc_unavailable",
2099
+ reason: "local_daemon_ipc_unavailable",
2100
+ transport: "local_ipc",
2101
+ recoverable: true,
2102
+ retryRecommended: true,
2103
+ nextAction: "Check the local daemon IPC connection, then retry mesh_launch_session once after the daemon is reachable."
2104
+ };
2105
+ }
2106
+ if (lower.includes("timed out") || lower.includes("timeout")) {
2107
+ return {
2108
+ code: "mesh_transport_timeout",
2109
+ reason: "mesh_transport_timeout",
2110
+ transport: "mesh_transport",
2111
+ recoverable: true,
2112
+ retryRecommended: true,
2113
+ nextAction: "Check mesh transport health, then do one bounded retry before requeueing or relaunching the task."
2114
+ };
2115
+ }
2116
+ return {
2117
+ code: "mesh_launch_failed",
2118
+ reason: "provider_launch_failed",
2119
+ transport: "mesh_transport",
2120
+ recoverable: false,
2121
+ retryRecommended: false,
2122
+ nextAction: "Inspect the provider launch error and fix the underlying provider/configuration issue before retrying."
2123
+ };
2124
+ }
2125
+ function buildWorktreeCleanupHint(node) {
2126
+ if (!node.isLocalWorktree) return void 0;
2127
+ return {
2128
+ tool: "mesh_remove_node",
2129
+ args: { node_id: node.id, session_cleanup_mode: "preserve" },
2130
+ hint: `If the worktree is no longer needed, remove the orphan worktree node with mesh_remove_node(node_id: "${node.id}").`
2131
+ };
2132
+ }
2133
+ function buildRecoverableLaunchFailure(ctx, node, providerType, error) {
2134
+ const message = error instanceof Error ? error.message : String(error || "launch failed");
2135
+ const classified = classifyMeshLaunchFailure(error);
2136
+ const cleanup = buildWorktreeCleanupHint(node);
2137
+ return {
2138
+ success: false,
2139
+ recoverable: classified.recoverable,
2140
+ code: classified.code,
2141
+ reason: classified.reason,
2142
+ transport: classified.transport,
2143
+ retryRecommended: classified.retryRecommended,
2144
+ nextAction: classified.nextAction,
2145
+ ...classified.noFallbackReason ? { noFallbackReason: classified.noFallbackReason } : {},
2146
+ error: message,
2147
+ meshId: ctx.mesh.id,
2148
+ nodeId: node.id,
2149
+ daemonId: node.daemonId,
2150
+ workspace: node.workspace,
2151
+ isLocalWorktree: node.isLocalWorktree === true,
2152
+ worktreeBranch: node.worktreeBranch,
2153
+ clonedFromNodeId: node.clonedFromNodeId,
2154
+ ...providerType ? { resolvedProviderType: providerType } : {},
2155
+ retryHint: `Retry mesh_launch_session(node_id: "${node.id}"${providerType ? `, type: "${providerType}"` : ""}) after daemon mesh transport/P2P is healthy.`,
2156
+ ...cleanup ? { cleanup } : {},
2157
+ nextStepHints: [
2158
+ `Retry mesh_launch_session(node_id: "${node.id}"${providerType ? `, type: "${providerType}"` : ""}) after checking daemon/P2P health.`,
2159
+ ...cleanup ? [`Cleanup orphan worktree node with mesh_remove_node(node_id: "${node.id}") if retry is not desired.`] : [],
2160
+ "Run mesh_status to see the degraded reason and recovery hints before redispatching work."
2161
+ ]
2162
+ };
2163
+ }
2164
+ function recordRecoverableLaunchFailure(ctx, node, providerType, error) {
2165
+ const failure = buildRecoverableLaunchFailure(ctx, node, providerType, error);
2166
+ try {
2167
+ (0, import_daemon_core2.appendLedgerEntry)(ctx.mesh.id, {
2168
+ kind: "recovery_attempted",
2169
+ nodeId: node.id,
2170
+ providerType,
2171
+ payload: {
2172
+ event: "session_launch_failed",
2173
+ ...failure
2174
+ }
2175
+ });
2176
+ } catch {
2177
+ }
2178
+ return failure;
2179
+ }
2180
+ function getLatestActiveLaunchFailure(meshId, nodeId) {
2181
+ const entries = (0, import_daemon_core2.readLedgerEntries)(meshId, { tail: 200 });
2182
+ for (let i = entries.length - 1; i >= 0; i -= 1) {
2183
+ const entry = entries[i];
2184
+ if (entry.nodeId !== nodeId) continue;
2185
+ if (entry.kind === "session_launched" || entry.kind === "node_removed") return null;
2186
+ if (entry.kind === "recovery_attempted" && entry.payload?.event === "session_launch_failed") {
2187
+ return { timestamp: entry.timestamp, ...entry.payload };
2188
+ }
2189
+ }
2190
+ return null;
2191
+ }
2192
+ function buildCoordinatorP2pRelayFailure(error, context) {
2193
+ const payload = (0, import_daemon_core2.buildP2pRelayFailurePayload)(error, {
2194
+ command: context.command,
2195
+ targetDaemonId: context.targetDaemonId
2196
+ });
2197
+ return {
2198
+ ...payload,
2199
+ ...context.nodeId ? { nodeId: context.nodeId } : {},
2200
+ ...context.sessionId ? { sessionId: context.sessionId } : {},
2201
+ retryHint: payload.retryRecommended ? payload.nextAction : "Do not retry as a P2P transport recovery; inspect the command/provider error first."
2202
+ };
2203
+ }
2204
+ async function ipcDispatchToRemoteAgent(ctx, node, args) {
2205
+ const transport = ctx.transport;
2206
+ const daemonId = node.daemonId;
2207
+ const dispatchCoordinatorDaemonId = readString(args.meshContext?.coordinatorDaemonId) || "";
2208
+ let sessionId = args.session_id?.trim() || "";
2209
+ const providerPriorityList = Array.isArray(node.policy?.providerPriority) ? node.policy.providerPriority : [];
2210
+ let resolvedProviderType = args.providerType?.trim() || providerPriorityList[0] || "";
2211
+ if (sessionId && args.verifiedSession) {
2212
+ const explicitSession = args.verifiedSession;
2213
+ const relaySafety = classifyRemoteDelegateRelaySafety(explicitSession, ctx.mesh.id, node.id, dispatchCoordinatorDaemonId);
2214
+ if (relaySafety === "unsafe_alias") {
2215
+ return buildRelayUnsafeRemoteSessionFailure(
2216
+ ctx,
2217
+ node,
2218
+ sessionId,
2219
+ resolvedProviderType || resolveSessionProviderType(explicitSession) || void 0
2220
+ );
2221
+ }
2222
+ if (relaySafety === "missing_anchor") {
2223
+ return buildMissingCoordinatorDaemonIdFailure(
2224
+ ctx,
2225
+ node,
2226
+ resolvedProviderType || resolveSessionProviderType(explicitSession) || void 0
2227
+ );
2228
+ }
2229
+ if (!resolvedProviderType) {
2230
+ resolvedProviderType = resolveSessionProviderType(explicitSession);
2231
+ }
2232
+ } else if (!sessionId || args.session_id) {
2233
+ try {
2234
+ const relayResult = await transport.meshCommand(daemonId, "get_status_metadata", {});
2235
+ const sessions = extractStatusMetadataSessions(relayResult);
2236
+ if (sessionId) {
2237
+ const explicitSession = sessions.find((session) => readSessionRecordId(session) === sessionId);
2238
+ if (!explicitSession) {
2239
+ return {
2240
+ success: false,
2241
+ recoverable: true,
2242
+ code: "mesh_target_session_not_found",
2243
+ reason: "mesh_target_session_not_found",
2244
+ transport: "mesh_transport",
2245
+ retryRecommended: true,
2246
+ meshId: ctx.mesh.id,
2247
+ nodeId: node.id,
2248
+ daemonId,
2249
+ workspace: node.workspace,
2250
+ sessionId,
2251
+ ...resolvedProviderType ? { resolvedProviderType } : {},
2252
+ error: `Remote session '${sessionId}' is not present in the live status for node '${node.id}'.`,
2253
+ nextAction: `Launch a fresh session with mesh_launch_session(node_id: '${node.id}'${resolvedProviderType ? `, type: '${resolvedProviderType}'` : ""}) or retry without session_id so Repo Mesh can target a live delegate session.`
2254
+ };
2255
+ }
2256
+ const relaySafety = classifyRemoteDelegateRelaySafety(explicitSession, ctx.mesh.id, node.id, dispatchCoordinatorDaemonId);
2257
+ if (relaySafety === "unsafe_alias") {
2258
+ return buildRelayUnsafeRemoteSessionFailure(
2259
+ ctx,
2260
+ node,
2261
+ sessionId,
2262
+ resolvedProviderType || resolveSessionProviderType(explicitSession) || void 0
2263
+ );
2264
+ }
2265
+ if (relaySafety === "missing_anchor") {
2266
+ return buildMissingCoordinatorDaemonIdFailure(
2267
+ ctx,
2268
+ node,
2269
+ resolvedProviderType || resolveSessionProviderType(explicitSession) || void 0
2270
+ );
2271
+ }
2272
+ if (!resolvedProviderType) {
2273
+ resolvedProviderType = resolveSessionProviderType(explicitSession);
2274
+ }
2275
+ } else {
2276
+ const targetSession = chooseDispatchableSession(sessions, resolvedProviderType, ctx.mesh.id, node.id, dispatchCoordinatorDaemonId);
2277
+ if (targetSession?.id || targetSession?.sessionId) {
2278
+ sessionId = targetSession.id || targetSession.sessionId;
2279
+ if (!resolvedProviderType) {
2280
+ resolvedProviderType = resolveSessionProviderType(targetSession);
2281
+ }
2282
+ }
2283
+ }
2284
+ } catch (e) {
2285
+ if (sessionId) {
2286
+ return {
2287
+ ...buildCoordinatorP2pRelayFailure(e, {
2288
+ command: "get_status_metadata",
2289
+ targetDaemonId: daemonId,
2290
+ nodeId: node.id,
2291
+ sessionId
2292
+ }),
2293
+ success: false,
2294
+ error: `Cannot verify remote session '${sessionId}' before dispatch: ${e?.message || String(e)}`
2295
+ };
2296
+ }
2297
+ }
2298
+ }
2299
+ if (!resolvedProviderType) {
2300
+ return { success: false, error: `Cannot dispatch to remote node '${node.id}': providerType unknown. Set providerPriority on the node policy or call mesh_launch_session first.` };
2301
+ }
2302
+ try {
2303
+ const dispatchResult = await transport.meshCommand(daemonId, "agent_command", {
2304
+ ...sessionId ? { targetSessionId: sessionId } : {},
2305
+ agentType: resolvedProviderType,
2306
+ cliType: resolvedProviderType,
2307
+ action: "send_chat",
2308
+ message: args.message,
2309
+ // WTCLAIM (B): carry the node workspace so a sessionless dispatch can be
2310
+ // scoped to THIS node's session on the worker (findAdapter dir match /
2311
+ // findMeshNodeAdapter). Without it, a worker hosting both a base node and a
2312
+ // cloned worktree node (same daemonId) would fall through to a provider-only
2313
+ // fuzzy match and could land worktree work on the base session.
2314
+ ...node.workspace ? { dir: node.workspace } : {},
2315
+ ...args.meshContext ? { meshContext: args.meshContext } : {}
2316
+ });
2317
+ const dispatchPayload = unwrapCommandPayload(dispatchResult);
2318
+ if (dispatchPayload?.success === false || dispatchResult?.success === false) {
2319
+ const source = dispatchPayload?.success === false ? dispatchPayload : dispatchResult;
2320
+ const errorMessage = dispatchPayload?.error || dispatchResult?.error || "agent_command rejected the task";
2321
+ return {
2322
+ ...buildCoordinatorP2pRelayFailure(source?.error || errorMessage, {
2323
+ command: "agent_command",
2324
+ targetDaemonId: daemonId,
2325
+ nodeId: node.id,
2326
+ sessionId
2327
+ }),
2328
+ ...source && typeof source === "object" ? source : {},
2329
+ success: false,
2330
+ error: `P2P dispatch failed: ${errorMessage}`
2331
+ };
2332
+ }
2333
+ return { success: true, dispatched: true, sessionId: sessionId || "", providerType: resolvedProviderType };
2334
+ } catch (e) {
2335
+ const errorMessage = e?.message || String(e);
2336
+ return {
2337
+ ...buildCoordinatorP2pRelayFailure(e, {
2338
+ command: "agent_command",
2339
+ targetDaemonId: daemonId,
2340
+ nodeId: node.id,
2341
+ sessionId
2342
+ }),
2343
+ error: `P2P dispatch failed: ${errorMessage}`
2344
+ };
2345
+ }
2346
+ }
2347
+ function meshSessionCacheKey(nodeId, runtimeSessionId) {
2348
+ return `${nodeId}:${runtimeSessionId}`;
2349
+ }
2350
+ function rememberMeshSessionProviderMetadata(nodeId, runtimeSessionId, metadata) {
2351
+ const keyNodeId = readString(nodeId);
2352
+ const keySessionId = readString(runtimeSessionId);
2353
+ if (!keyNodeId || !keySessionId) return;
2354
+ const providerType = readString(metadata.providerType);
2355
+ const providerSessionId = readString(metadata.providerSessionId);
2356
+ if (!providerType && !providerSessionId) return;
2357
+ const existing = getSessionMetadata(meshSessionCacheKey(keyNodeId, keySessionId)) || { providerType: "" };
2358
+ meshSessionProviderMetadata.set(meshSessionCacheKey(keyNodeId, keySessionId), {
2359
+ providerType: providerType || existing.providerType,
2360
+ providerSessionId: providerSessionId || existing.providerSessionId,
2361
+ expiresAt: Date.now() + SESSION_PROVIDER_METADATA_TTL_MS
2362
+ });
2363
+ }
2364
+ function rememberMeshSessionProviderMetadataFromEvent(event) {
2365
+ const metadataEvent = event?.metadataEvent && typeof event.metadataEvent === "object" ? event.metadataEvent : event && typeof event === "object" ? event : {};
2366
+ const nodeId = readString(event?.nodeId) || readString(metadataEvent.nodeId) || readString(metadataEvent.meshNodeId);
2367
+ const sessionId = readString(metadataEvent.targetSessionId) || readString(metadataEvent.sessionId) || readString(metadataEvent.instanceId) || readString(event?.sessionId);
2368
+ rememberMeshSessionProviderMetadata(nodeId, sessionId, {
2369
+ providerType: readString(metadataEvent.providerType) || readString(event?.providerType) || "",
2370
+ providerSessionId: readString(metadataEvent.providerSessionId) || readString(event?.providerSessionId)
2371
+ });
2372
+ }
2373
+ function resolveMeshSessionProviderMetadataFromLedger(ctx, nodeId, runtimeSessionId) {
2374
+ const entries = (0, import_daemon_core2.readLedgerEntries)(ctx.mesh.id, { tail: 50 });
2375
+ for (let i = entries.length - 1; i >= 0; i -= 1) {
2376
+ const entry = entries[i];
2377
+ const payload = entry.payload && typeof entry.payload === "object" && !Array.isArray(entry.payload) ? entry.payload : {};
2378
+ const entryNodeId = readString(entry.nodeId) || readString(payload.nodeId) || readString(payload.meshNodeId);
2379
+ if (entryNodeId && entryNodeId !== nodeId) continue;
2380
+ const entrySessionId = readString(entry.sessionId) || readString(payload.targetSessionId) || readString(payload.sessionId) || readString(payload.instanceId);
2381
+ if (entrySessionId !== runtimeSessionId) continue;
2382
+ const providerType = readString(entry.providerType) || readString(payload.providerType);
2383
+ const completionDiagnostic = payload.completionDiagnostic && typeof payload.completionDiagnostic === "object" && !Array.isArray(payload.completionDiagnostic) ? payload.completionDiagnostic : {};
2384
+ const metadataEvent = payload.metadataEvent && typeof payload.metadataEvent === "object" && !Array.isArray(payload.metadataEvent) ? payload.metadataEvent : {};
2385
+ const providerSessionId = readString(payload.providerSessionId) || readString(completionDiagnostic.providerSessionId) || readString(metadataEvent.providerSessionId);
2386
+ if (providerType || providerSessionId) {
2387
+ return { providerType: providerType || "", providerSessionId };
2388
+ }
2389
+ }
2390
+ return void 0;
2391
+ }
2392
+ function resolveMeshSessionProviderMetadata(ctx, nodeId, runtimeSessionId) {
2393
+ const cached = getSessionMetadata(meshSessionCacheKey(nodeId, runtimeSessionId));
2394
+ if (cached?.providerType || cached?.providerSessionId) return cached;
2395
+ const fromLedger = resolveMeshSessionProviderMetadataFromLedger(ctx, nodeId, runtimeSessionId);
2396
+ if (fromLedger) rememberMeshSessionProviderMetadata(nodeId, runtimeSessionId, fromLedger);
2397
+ return fromLedger;
2398
+ }
2399
+ function countUncommittedChanges(status) {
2400
+ if (typeof status?.uncommittedChanges === "number") return status.uncommittedChanges;
2401
+ const keys = ["staged", "modified", "untracked", "deleted", "renamed"];
2402
+ const counted = keys.reduce((sum, key) => sum + (Number.isFinite(Number(status?.[key])) ? Number(status[key]) : 0), 0);
2403
+ const conflicts = Array.isArray(status?.conflictFiles) ? status.conflictFiles.length : status?.hasConflicts ? 1 : 0;
2404
+ return counted + conflicts;
2405
+ }
2406
+ function isGitStatusDirty(status) {
2407
+ if (typeof status?.isDirty === "boolean") return status.isDirty;
2408
+ if (typeof status?.dirty === "boolean") return status.dirty;
2409
+ if (Array.isArray(status?.submodules) && status.submodules.some((submodule) => submodule?.dirty || submodule?.outOfSync || submodule?.error)) return true;
2410
+ return countUncommittedChanges(status) > 0;
2411
+ }
2412
+ function slimLedgerPayload(payload) {
2413
+ const slim = {};
2414
+ for (const [k, v] of Object.entries(payload)) {
2415
+ if (k === "message" || k === "taskSummary") {
2416
+ slim[k] = typeof v === "string" && v.length > 200 ? v.slice(0, 200) + "\u2026" : v;
2417
+ } else if (k === "evidence" || k === "workerResult" || k === "gitStatus" || k === "validationResults") {
2418
+ } else if (k === "finalSummary") {
2419
+ slim[k] = typeof v === "string" && v.length > 300 ? v.slice(0, 300) + "\u2026" : v;
2420
+ } else if (LARGE_LEDGER_FIELD_KEYS.has(k)) {
2421
+ slim[k] = summarizeLargeLedgerField(k, v);
2422
+ } else {
2423
+ slim[k] = elideLargeNestedValue(k, v);
2424
+ }
2425
+ }
2426
+ return slim;
2427
+ }
2428
+ function readRelatedRepos(node) {
2429
+ const raw = Array.isArray(node.relatedRepos) ? node.relatedRepos : Array.isArray(node.policy?.relatedRepos) ? node.policy.relatedRepos : [];
2430
+ return raw.map((entry) => ({
2431
+ label: typeof entry?.label === "string" ? entry.label.trim() : "",
2432
+ workspace: typeof entry?.workspace === "string" ? entry.workspace.trim() : ""
2433
+ })).filter((entry) => Boolean(entry.label && entry.workspace));
2434
+ }
2435
+ function summarizeRelatedRepoStatus(repo, status) {
2436
+ const dirty = isGitStatusDirty(status);
2437
+ return {
2438
+ label: repo.label,
2439
+ workspace: repo.workspace,
2440
+ isGitRepo: status?.isGitRepo === true,
2441
+ branch: status?.branch ?? null,
2442
+ upstream: status?.upstream ?? null,
2443
+ upstreamStatus: typeof status?.upstreamStatus === "string" ? status.upstreamStatus : status?.upstream ? "unchecked" : "no_upstream",
2444
+ upstreamFetchedAt: Number.isFinite(Number(status?.upstreamFetchedAt)) ? Number(status.upstreamFetchedAt) : null,
2445
+ upstreamFetchError: typeof status?.upstreamFetchError === "string" ? status.upstreamFetchError : null,
2446
+ ahead: Number.isFinite(Number(status?.ahead)) ? Number(status.ahead) : 0,
2447
+ behind: Number.isFinite(Number(status?.behind)) ? Number(status.behind) : 0,
2448
+ dirty,
2449
+ uncommittedChanges: countUncommittedChanges(status),
2450
+ head: status?.headCommit ?? null,
2451
+ lastCommitSummary: status?.headMessage ?? null,
2452
+ ...status?.reason ? { reason: status.reason } : {},
2453
+ ...status?.error ? { error: status.error } : {}
2454
+ };
2455
+ }
2456
+ async function collectRelatedRepoStatuses(ctx, node) {
2457
+ const relatedRepos = readRelatedRepos(node);
2458
+ if (!relatedRepos.length) return [];
2459
+ const results = [];
2460
+ for (const repo of relatedRepos) {
2461
+ try {
2462
+ const statusResult = await commandForNode(ctx, node, "git_status", { workspace: repo.workspace, refreshUpstream: true });
2463
+ const status = extractGitStatus(statusResult);
2464
+ results.push(summarizeRelatedRepoStatus(repo, status));
2465
+ } catch (e) {
2466
+ results.push({
2467
+ label: repo.label,
2468
+ workspace: repo.workspace,
2469
+ error: e?.message || "related repo status failed"
2470
+ });
2471
+ }
2472
+ }
2473
+ return results;
2474
+ }
2475
+ function readProviderPriority(policy) {
2476
+ const raw = policy?.providerPriority;
2477
+ return Array.isArray(raw) ? raw.map((type) => typeof type === "string" ? type.trim() : "").filter(Boolean) : [];
2478
+ }
2479
+ function buildNodeCapabilityExposure(node) {
2480
+ const providers = readProviderPriority(node.policy);
2481
+ const capabilityTags = (0, import_daemon_core2.buildMeshNodeCapabilityTags)(node);
2482
+ const exposure = { capabilityTags };
2483
+ if (providers.length) {
2484
+ const byProvider = {};
2485
+ for (const provider of providers) {
2486
+ byProvider[provider] = (0, import_daemon_core2.buildMeshNodeCapabilityTags)(node, provider);
2487
+ }
2488
+ exposure.capabilityTagsByProvider = byProvider;
2489
+ }
2490
+ const capabilities = Array.isArray(node.capabilities) ? node.capabilities.filter((tag) => typeof tag === "string" && !!tag.trim()) : [];
2491
+ if (capabilities.length) exposure.capabilities = capabilities;
2492
+ return exposure;
2493
+ }
2494
+ function readSpawnedSessionVisibility(policy) {
2495
+ return policy?.spawnedSessionVisibility === "hidden" ? "hidden" : "visible";
2496
+ }
2497
+ function missingProviderPriorityMessage(nodeId) {
2498
+ return `Node '${nodeId}' has no providerPriority policy; pass type explicitly or configure node.policy.providerPriority`;
2499
+ }
2500
+ function getNodeLaunchReadiness(node) {
2501
+ const bootstrap = node.worktreeBootstrap;
2502
+ if (node.isLocalWorktree && bootstrap?.status === "failed" && bootstrap?.required !== false) {
2503
+ return {
2504
+ providerPriority: readProviderPriority(node.policy),
2505
+ launchReady: false,
2506
+ launchBlockedReason: "worktree_bootstrap_failed",
2507
+ launchBlockedMessage: typeof bootstrap.error === "string" && bootstrap.error.trim() ? bootstrap.error.trim() : "Required worktree bootstrap failed; resolve it before launching an agent into this node.",
2508
+ worktreeBootstrap: bootstrap
2509
+ };
2510
+ }
2511
+ const providerPriority = readProviderPriority(node.policy);
2512
+ if (providerPriority.length) {
2513
+ return {
2514
+ providerPriority,
2515
+ launchReady: true
2516
+ };
2517
+ }
2518
+ return {
2519
+ providerPriority,
2520
+ launchReady: false,
2521
+ launchBlockedReason: "missing_provider_priority",
2522
+ launchBlockedMessage: missingProviderPriorityMessage(node.id)
2523
+ };
2524
+ }
2525
+ function getWorktreeBootstrapLaunchBlock(node, meshPolicy) {
2526
+ if (!node.isLocalWorktree) return void 0;
2527
+ const bootstrap = node.worktreeBootstrap;
2528
+ const requireReady = !!(meshPolicy && typeof meshPolicy === "object" && meshPolicy.requireBootstrapBeforeLaunch === true);
2529
+ if (requireReady && bootstrap?.status !== "ready") {
2530
+ return {
2531
+ success: false,
2532
+ code: "bootstrap_not_ready",
2533
+ error: `Node '${node.id}' bootstrap state is '${bootstrap?.status ?? "unknown"}' and mesh policy requireBootstrapBeforeLaunch is enabled.`,
2534
+ nodeId: node.id,
2535
+ worktreeBootstrap: bootstrap ?? null,
2536
+ recoveryHint: "Run the worktree bootstrap (clone runOnClone or a refine with bootstrap inherit) until the node reports ready, or disable requireBootstrapBeforeLaunch."
2537
+ };
2538
+ }
2539
+ if (bootstrap?.status !== "failed" || bootstrap?.required === false) return void 0;
2540
+ return {
2541
+ success: false,
2542
+ code: "worktree_bootstrap_failed",
2543
+ error: typeof bootstrap.error === "string" && bootstrap.error.trim() ? bootstrap.error.trim() : `Node '${node.id}' has a failed required worktree bootstrap.`,
2544
+ nodeId: node.id,
2545
+ worktreeBootstrap: bootstrap,
2546
+ recoveryHint: "Fix the configured worktree bootstrap command or remove/recreate the worktree node before launching an agent."
2547
+ };
2548
+ }
2549
+ async function collectLiveStatusSessions(ctx, node) {
2550
+ try {
2551
+ const statusResult = await commandForNode(ctx, node, "get_status_metadata", {});
2552
+ return extractStatusMetadataSessions(statusResult);
2553
+ } catch {
2554
+ return [];
2566
2555
  }
2567
- };
2568
- var MESH_CLONE_NODE_TOOL = {
2569
- name: "mesh_clone_node",
2570
- description: "Create a new worktree-based node from an existing node for isolated parallel work. Creates a git worktree on a new branch so multiple tasks can run on separate branches simultaneously.",
2571
- inputSchema: {
2572
- type: "object",
2573
- properties: {
2574
- source_node_id: { type: "string", description: "Node ID to clone from (from mesh_list_nodes)." },
2575
- branch: { type: "string", description: 'Branch name for the new worktree (e.g. "feat/auth-refactor").' },
2576
- base_branch: { type: "string", description: "Starting point for the branch (default: current HEAD)." }
2577
- },
2578
- required: ["source_node_id", "branch"]
2556
+ }
2557
+ async function collectLiveStatusProbe(ctx, node) {
2558
+ try {
2559
+ const statusResult = await commandForNode(ctx, node, "get_status_metadata", {});
2560
+ return {
2561
+ sessions: extractStatusMetadataSessions(statusResult),
2562
+ daemonBuild: extractDaemonBuildInfo(statusResult)
2563
+ };
2564
+ } catch {
2565
+ return { sessions: [] };
2579
2566
  }
2580
- };
2581
- var MESH_REMOVE_NODE_TOOL = {
2582
- name: "mesh_remove_node",
2583
- description: "Remove a node from the mesh. If the node is a worktree, also cleans up the git worktree and directory. Session cleanup is controlled by mesh policy sessionCleanupOnNodeRemove unless session_cleanup_mode overrides it for this call. The coordinator's own local base node (same machine, NOT a worktree) is protected \u2014 removing it breaks live mesh membership and is rejected unless force:true is passed.",
2584
- inputSchema: {
2585
- type: "object",
2586
- properties: {
2587
- node_id: { type: "string", description: "Node ID to remove." },
2588
- session_cleanup_mode: {
2589
- type: "string",
2590
- enum: ["preserve", "stop", "delete_stopped", "stop_and_delete"],
2591
- description: "Optional override for cleanup of delegated sessions attached to this node. preserve keeps history/processes; stop stops live runtimes only; delete_stopped removes completed transcripts only; stop_and_delete stops live runtimes and deletes records."
2592
- },
2593
- force: { type: "boolean", description: "Override the coordinator-base-node guard. Only set true to intentionally tear down this mesh; the coordinator must then be re-registered/restarted. Worktree nodes never need force." }
2594
- },
2595
- required: ["node_id"]
2567
+ }
2568
+ function extractDaemonBuildInfo(value) {
2569
+ const payload = unwrapCommandPayload(value);
2570
+ const build = payload?.daemonBuild && typeof payload.daemonBuild === "object" ? payload.daemonBuild : value?.daemonBuild && typeof value.daemonBuild === "object" ? value.daemonBuild : void 0;
2571
+ if (!build) return void 0;
2572
+ const commit = readString(build.commit);
2573
+ if (!commit) return void 0;
2574
+ return {
2575
+ commit,
2576
+ commitShort: readString(build.commitShort) || commit.slice(0, 7),
2577
+ version: readString(build.version) || "unknown",
2578
+ ...readString(build.builtAt) ? { builtAt: readString(build.builtAt) } : {}
2579
+ };
2580
+ }
2581
+ async function collectMeshViewQueueNodesWithLiveSessions(ctx) {
2582
+ const nodes = await Promise.all(ctx.mesh.nodes.map(async (node) => {
2583
+ const liveSessions = await collectLiveStatusSessions(ctx, node);
2584
+ return liveSessions.length > 0 ? { ...node, sessions: liveSessions } : node;
2585
+ }));
2586
+ return nodes;
2587
+ }
2588
+ function buildBranchConvergence(mesh, node, status, dirty, uncommittedChanges) {
2589
+ const defaultBranch = readString(mesh.defaultBranch) ?? "main";
2590
+ const branch = readString(status?.branch) ?? readString(node.worktreeBranch) ?? null;
2591
+ const ahead = readNumeric(status?.ahead);
2592
+ const behind = readNumeric(status?.behind);
2593
+ const upstream = readString(status?.upstream) ?? null;
2594
+ const upstreamStatus = readString(status?.upstreamStatus) ?? (upstream ? "unchecked" : "no_upstream");
2595
+ const hasConflicts = status?.hasConflicts === true || Array.isArray(status?.conflictFiles) && status.conflictFiles.length > 0;
2596
+ const base = {
2597
+ defaultBranch,
2598
+ branch,
2599
+ upstream,
2600
+ upstreamStatus,
2601
+ ahead,
2602
+ behind,
2603
+ isWorktree: node.isLocalWorktree === true,
2604
+ isDefaultBranch: branch === defaultBranch
2605
+ };
2606
+ if (status?.isGitRepo !== true) {
2607
+ return {
2608
+ ...base,
2609
+ status: "blocked_review",
2610
+ needsConvergence: true,
2611
+ reason: "git_status_unavailable",
2612
+ nextStep: `Resolve git status for node '${node.id}' before marking the task complete.`
2613
+ };
2596
2614
  }
2597
- };
2598
- var MESH_CLEANUP_SESSIONS_TOOL = {
2599
- name: "mesh_cleanup_sessions",
2600
- description: "Manually clean up delegated session records for a mesh node without removing the node. Defaults should preserve reviewable history unless the caller chooses a mode explicitly.",
2601
- inputSchema: {
2602
- type: "object",
2603
- properties: {
2604
- node_id: { type: "string", description: "Node ID whose delegated sessions should be considered for cleanup." },
2605
- mode: {
2606
- type: "string",
2607
- enum: ["preserve", "stop", "delete_stopped", "stop_and_delete"],
2608
- description: "preserve = no-op; stop = release process occupancy by stopping live runtimes; delete_stopped = remove completed/stopped records while leaving live runtimes alone; stop_and_delete = stop live runtimes and delete records."
2609
- },
2610
- session_ids: {
2611
- type: "array",
2612
- items: { type: "string" },
2613
- description: "Optional explicit session IDs to limit cleanup to. When omitted, sessions are matched by node/workspace metadata."
2614
- },
2615
- dry_run: { type: "boolean", description: "Preview matched/stopped/deleted/skipped session IDs without mutating session-host state." }
2616
- },
2617
- required: ["node_id", "mode"]
2615
+ if (!branch) {
2616
+ return {
2617
+ ...base,
2618
+ status: "blocked_review",
2619
+ needsConvergence: true,
2620
+ reason: "branch_unknown",
2621
+ nextStep: `Inspect node '${node.id}' git branch before deciding whether it is merged to ${defaultBranch}.`
2622
+ };
2618
2623
  }
2619
- };
2620
- var MESH_TASK_HISTORY_TOOL = {
2621
- name: "mesh_task_history",
2622
- description: "Read the task ledger for this mesh \u2014 dispatched tasks, completions, failures, checkpoints, and node lifecycle events. Use to understand what has been done before deciding next steps, to detect repeated failures, and to inform recovery decisions.",
2623
- inputSchema: {
2624
- type: "object",
2625
- properties: {
2626
- tail: { type: "number", description: "Number of recent entries to return (default: 20; clamped to 40 in compact mode, 200 in verbose)." },
2627
- kind: { type: "string", description: "Filter by entry kind: task_dispatched, task_completed, task_failed, task_stalled, session_launched, checkpoint_created, node_cloned, node_removed, direct_fast_forward." },
2628
- compact: { type: "boolean", description: "Slim payload for LLM callers. Default true. Truncates long payload strings (message/taskSummary \u2264200, finalSummary \u2264300) and elides any large nested evidence blob (>2KB serialized \u2014 e.g. validationSummary/result/patchEquivalence/submoduleReachability) to a {_elided,_kind,_bytes,_hint} placeholder; full evidence stays accessible via mesh_reconcile_ledger. Set false (or verbose=true) for full untruncated payloads." },
2629
- verbose: { type: "boolean", description: "Force the full untruncated payload; overrides compact." }
2630
- }
2624
+ if (hasConflicts || dirty || uncommittedChanges > 0) {
2625
+ return {
2626
+ ...base,
2627
+ status: "not_mergeable",
2628
+ needsConvergence: true,
2629
+ reason: hasConflicts ? "conflicts_present" : "dirty_workspace",
2630
+ nextStep: `Commit, checkpoint, or resolve node '${node.id}' before any main convergence step.`
2631
+ };
2631
2632
  }
2632
- };
2633
- var MESH_RECONCILE_LEDGER_TOOL = {
2634
- name: "mesh_reconcile_ledger",
2635
- description: "Reconcile daemon-local mesh ledgers by querying bounded ledger slices over P2P/DataChannel and importing missing entries into the coordinator local JSONL ledger. Cloud/D1 is not used as a ledger source of truth.",
2636
- inputSchema: {
2637
- type: "object",
2638
- properties: {
2639
- node_ids: { type: "array", items: { type: "string" }, description: "Optional node IDs to query. Defaults to all mesh nodes." },
2640
- limit: { type: "number", description: "Bounded slice size per node. Defaults to 100 and is clamped by daemon-core." },
2641
- after_id: { type: "string", description: "Optional cursor entry ID; remote slices return entries strictly after this ID when present." },
2642
- since: { type: "string", description: "Optional ISO timestamp lower bound for queried entries." },
2643
- import_entries: { type: "boolean", description: "When false, query and report evidence without importing remote entries. Defaults true." }
2633
+ if (branch === defaultBranch) {
2634
+ if (upstream && upstreamStatus !== "fresh") {
2635
+ return {
2636
+ ...base,
2637
+ status: "blocked_review",
2638
+ needsConvergence: true,
2639
+ reason: "default_branch_upstream_unverified",
2640
+ nextStep: `Refresh ${defaultBranch}'s upstream refs or resolve the fetch failure before declaring convergence complete for node '${node.id}'.`
2641
+ };
2644
2642
  }
2645
- }
2646
- };
2647
- var MESH_PRUNE_STALE_DIRECT_TOOL = {
2648
- name: "mesh_prune_stale_direct",
2649
- description: "Prune orphaned staleDirect dispatch records \u2014 direct task dispatches whose original node/session is no longer present in the live mesh. dry_run (default) reports exactly which records would be pruned without mutating anything; pass execute=true to delete them. Active/pending/assigned/generating work and fresh unacknowledged dispatch failures (node/session still live) are always preserved. The append-only mesh ledger audit history is left intact.",
2650
- inputSchema: {
2651
- type: "object",
2652
- properties: {
2653
- execute: { type: "boolean", description: "When true, actually delete the orphaned records. Defaults false (dry run). Ignored when dry_run=true." },
2654
- dry_run: { type: "boolean", description: "Force a preview without mutation even if execute=true. Defaults to dry-run behavior when execute is not set." },
2655
- include_terminal: { type: "boolean", description: "Also prune terminal (completed/failed) direct dispatch store rows in addition to orphans. Defaults false." }
2643
+ if (ahead > 0 || behind > 0) {
2644
+ return {
2645
+ ...base,
2646
+ status: "blocked_review",
2647
+ needsConvergence: true,
2648
+ reason: "default_branch_not_even_with_upstream",
2649
+ nextStep: `Bring ${defaultBranch} even with its upstream before declaring convergence complete.`
2650
+ };
2656
2651
  }
2652
+ return {
2653
+ ...base,
2654
+ status: "merged_to_main",
2655
+ needsConvergence: false,
2656
+ reason: "clean_default_branch",
2657
+ nextStep: null
2658
+ };
2657
2659
  }
2658
- };
2659
- var MESH_REFINE_NODE_TOOL = {
2660
- name: "mesh_refine_node",
2661
- description: "The Refinery: validate \u2192 merge \u2192 push \u2192 clean up a completed worktree node onto the base branch. Defaults to dry-run (plan only): returns the validation plan with mergeWillRun:false/cleanupWillRun:false and performs NO merge/push/cleanup. Pass execute=true to actually converge the node. execute=true is async: the immediate response includes async:true, status:'accepted', jobId, interactionId, target node, and startedAt; completion/failure evidence is delivered through pending mesh events and the mesh task ledger. dry_run=true overrides execute. Matches the mesh_refine_batch / mesh_fast_forward_node dry_run/execute contract.",
2662
- inputSchema: {
2663
- type: "object",
2664
- properties: {
2665
- node_id: { type: "string", description: "Node ID of the completed worktree node to refine and merge." },
2666
- execute: { type: "boolean", description: "When true, run validation/merge/push/cleanup for this node. Defaults false/dry-run." },
2667
- dry_run: { type: "boolean", description: "Preview the validation plan without merging. Defaults true unless execute=true; dry_run=true overrides execute." }
2668
- },
2669
- required: ["node_id"]
2660
+ if (node.isLocalWorktree) {
2661
+ return {
2662
+ ...base,
2663
+ status: "cleanup_candidate",
2664
+ needsConvergence: true,
2665
+ reason: "clean_non_default_worktree_branch",
2666
+ nextStep: `Run mesh_refine_node(node_id: "${node.id}") or explicitly classify this worktree as blocked_review/not_mergeable before ending the task.`
2667
+ };
2670
2668
  }
2671
- };
2672
- var MESH_REFINE_BATCH_TOOL = {
2673
- name: "mesh_refine_batch",
2674
- description: "Batch Refinery: converge multiple sibling worktree nodes onto the base branch in one conflict-aware sequential pipeline. Orders nodes by change-area (non-submodule nodes first, submodule-touching nodes serialized last) so each merged sibling advances the base and the next node auto-rebases + re-checks patch-equivalence before its own merge. Each node runs the same validation/patch-equivalence/submodule-reachability/merge/cleanup gates as mesh_refine_node. Conflicting or blocked nodes are isolated as blocked_review while the rest of the batch proceeds. Defaults to dry-run (plan only); set execute=true to converge. Never force-pushes or resets. execute=true is async: the immediate response is async:true / status:'accepted' with the batch jobId and ordered target node list; per-node convergence runs in the background and the aggregate completion/failure (with per-node merged / blocked_review / not_mergeable results) is delivered as a terminal refine event via pending mesh events and the ledger \u2014 do not re-invoke while a batch is in flight. dry_run returns the plan synchronously.",
2675
- inputSchema: {
2676
- type: "object",
2677
- properties: {
2678
- node_ids: {
2679
- type: "array",
2680
- items: { type: "string" },
2681
- description: "Optional explicit node IDs to converge, in any order (the tool computes the safe merge order). When omitted, all local worktree nodes that need convergence are auto-collected."
2682
- },
2683
- execute: { type: "boolean", description: "When true, run validation/rebase/merge for each node in order. Defaults false/dry-run." },
2684
- dry_run: { type: "boolean", description: "Preview the ordering + per-node validation plan without executing. Defaults true unless execute=true; dry_run=true overrides execute." }
2685
- },
2686
- required: []
2669
+ if (upstream && upstreamStatus !== "fresh") {
2670
+ return {
2671
+ ...base,
2672
+ status: "blocked_review",
2673
+ needsConvergence: true,
2674
+ reason: "feature_branch_upstream_unverified",
2675
+ nextStep: `Refresh branch '${branch}' upstream refs or resolve the fetch failure before deciding whether it is ready to merge into ${defaultBranch}.`
2676
+ };
2687
2677
  }
2688
- };
2689
- var MESH_REFINE_CONFIG_SCHEMA_TOOL = {
2690
- name: "mesh_refine_config_schema",
2691
- description: "Return the Repo Mesh Refinery config JSON schema and supported repo-local config locations. This is the validation source of truth; heuristic command detection is suggestions-only.",
2692
- inputSchema: { type: "object", properties: {} }
2693
- };
2694
- var MESH_VALIDATE_REFINE_CONFIG_TOOL = {
2695
- name: "mesh_validate_refine_config",
2696
- description: "Validate the repo mesh/refine config for a node/workspace without running validation commands or merging.",
2697
- inputSchema: {
2698
- type: "object",
2699
- properties: {
2700
- node_id: { type: "string", description: "Optional node/workspace whose refine config should be loaded. Defaults to the first mesh node." },
2701
- config: { type: "object", description: "Optional inline config object to validate instead of loading from the repo." }
2702
- }
2678
+ if (!upstream || ahead > 0 || behind > 0) {
2679
+ return {
2680
+ ...base,
2681
+ status: "blocked_review",
2682
+ needsConvergence: true,
2683
+ reason: !upstream ? "feature_branch_missing_upstream" : "feature_branch_not_even_with_upstream",
2684
+ nextStep: `Push or reconcile branch '${branch}', then merge it into ${defaultBranch} or mark it not_mergeable with a reason.`
2685
+ };
2686
+ }
2687
+ return {
2688
+ ...base,
2689
+ status: "pushed_feature_branch_needs_merge",
2690
+ needsConvergence: true,
2691
+ reason: "clean_non_default_branch",
2692
+ nextStep: `Review and merge branch '${branch}' into ${defaultBranch}; do not report the task as fully complete while it remains off main.`
2693
+ };
2694
+ }
2695
+ var COMPACT_MAX_CONVERGENCE_FOLLOWUPS = 12;
2696
+ function summarizeBranchConvergence(nodes, compact = false) {
2697
+ const allFollowUps = nodes.filter((node) => node?.branchConvergence?.needsConvergence === true).map((node) => ({
2698
+ nodeId: node.nodeId,
2699
+ // workspace is a long absolute path redundant with nodeId — drop it in
2700
+ // compact mode to keep this summary bounded.
2701
+ ...compact ? {} : { workspace: node.workspace },
2702
+ branch: node.branchConvergence.branch,
2703
+ status: node.branchConvergence.status,
2704
+ reason: node.branchConvergence.reason,
2705
+ // The per-node nextStep is long prose that repeats node ids/branch names.
2706
+ // In compact mode drop it (the status+reason carry the actionable signal;
2707
+ // verbose still surfaces the full nextStep) so this summary stays bounded
2708
+ // as node count grows.
2709
+ ...compact ? {} : { nextStep: node.branchConvergence.nextStep }
2710
+ }));
2711
+ const byStatus = {};
2712
+ for (const f of allFollowUps) {
2713
+ const s = typeof f.status === "string" ? f.status : "unknown";
2714
+ byStatus[s] = (byStatus[s] ?? 0) + 1;
2703
2715
  }
2704
- };
2705
- var MESH_SUGGEST_REFINE_CONFIG_TOOL = {
2706
- name: "mesh_suggest_refine_config",
2707
- description: "Suggest a repo mesh/refine config scaffold from project context/package scripts. Suggestions are never executed until saved as explicit refine config.",
2708
- inputSchema: {
2709
- type: "object",
2710
- properties: {
2711
- node_id: { type: "string", description: "Optional node/workspace used for suggestions. Defaults to the first mesh node." }
2712
- }
2716
+ const followUps = compact ? allFollowUps.slice(0, COMPACT_MAX_CONVERGENCE_FOLLOWUPS) : allFollowUps;
2717
+ const omitted = allFollowUps.length - followUps.length;
2718
+ return {
2719
+ needsFollowUp: allFollowUps.length > 0,
2720
+ unresolvedCount: allFollowUps.length,
2721
+ byStatus,
2722
+ requiredFinalStates: ["merged_to_main", "pushed_feature_branch_needs_merge", "blocked_review", "cleanup_candidate", "not_mergeable"],
2723
+ followUps,
2724
+ ...omitted > 0 ? { followUpsOmitted: omitted, followUpsHint: "Per-node followUp rows are capped in compact mode; counts above are complete. Use verbose=true for the full list." } : {}
2725
+ };
2726
+ }
2727
+ async function commandForNode(ctx, node, command, args = {}) {
2728
+ const isLocalNode = isLocalControlPlaneNode(ctx, node);
2729
+ if (ctx.transport instanceof IpcTransport && node.daemonId && !isLocalNode) {
2730
+ return ctx.transport.meshCommand(node.daemonId, command, args);
2713
2731
  }
2714
- };
2715
- var MESH_CHANGE_IMPACT_CONFIG_SCHEMA_TOOL = {
2716
- name: "mesh_change_impact_config_schema",
2717
- description: "Return the Change Impact config JSON schema and supported repo-local config locations. Change Impact config declaratively classifies which package/file changes between the live daemon build and workspace HEAD require a daemon rebuild/restart vs. a web-only redeploy vs. nothing. Declarative only \u2014 config is parsed, never executed.",
2718
- inputSchema: { type: "object", properties: {} }
2719
- };
2720
- var MESH_VALIDATE_CHANGE_IMPACT_CONFIG_TOOL = {
2721
- name: "mesh_validate_change_impact_config",
2722
- description: "Validate a Change Impact config for a node/workspace and report valid/errors. Loads .adhdev/change-impact.{json,yaml,yml} (or repo-mesh-change-impact.* alias) from the repo unless an inline config is provided.",
2723
- inputSchema: {
2724
- type: "object",
2725
- properties: {
2726
- node_id: { type: "string", description: "Optional node/workspace whose change-impact config should be loaded. Defaults to the first mesh node." },
2727
- config: { type: "object", description: "Optional inline config object to validate instead of loading from the repo." }
2732
+ return ctx.transport.command(command, args);
2733
+ }
2734
+ function normalizePendingMeshCoordinatorEvents(value) {
2735
+ const payload = unwrapCommandPayload(value);
2736
+ const events = Array.isArray(payload?.events) ? payload.events : Array.isArray(value?.events) ? value.events : [];
2737
+ return events.filter((event) => event && typeof event === "object");
2738
+ }
2739
+ function buildMeshForwardPayloadFromPendingEvent(event) {
2740
+ const metadataEvent = event?.metadataEvent && typeof event.metadataEvent === "object" ? event.metadataEvent : {};
2741
+ return {
2742
+ event: readString(event?.event),
2743
+ meshId: readString(event?.meshId),
2744
+ nodeId: readString(event?.nodeId) || readString(metadataEvent.meshNodeId),
2745
+ workspace: readString(event?.workspace) || readString(metadataEvent.workspace),
2746
+ targetSessionId: readString(metadataEvent.targetSessionId) || readString(metadataEvent.sessionId) || readString(metadataEvent.instanceId),
2747
+ providerType: readString(metadataEvent.providerType),
2748
+ providerSessionId: readString(metadataEvent.providerSessionId),
2749
+ finalSummary: readString(metadataEvent.finalSummary) || readString(metadataEvent.summary),
2750
+ jobId: readString(metadataEvent.jobId),
2751
+ interactionId: readString(metadataEvent.interactionId),
2752
+ status: readString(metadataEvent.status),
2753
+ targetDaemonId: readString(metadataEvent.targetDaemonId),
2754
+ startedAt: readString(metadataEvent.startedAt),
2755
+ completedAt: readString(metadataEvent.completedAt),
2756
+ retryOfJobId: readString(metadataEvent.retryOfJobId),
2757
+ ...metadataEvent.result && typeof metadataEvent.result === "object" && !Array.isArray(metadataEvent.result) ? { result: metadataEvent.result } : {},
2758
+ ...metadataEvent.intentional === true ? { intentional: true } : {},
2759
+ ...metadataEvent.intentionalStop === true ? { intentionalStop: true } : {},
2760
+ ...metadataEvent.operatorCleanup === true ? { operatorCleanup: true } : {},
2761
+ ...readString(metadataEvent.reason) ? { reason: readString(metadataEvent.reason) } : {},
2762
+ ...readString(metadataEvent.stopReason) ? { stopReason: readString(metadataEvent.stopReason) } : {},
2763
+ ...readString(metadataEvent.cleanupReason) ? { cleanupReason: readString(metadataEvent.cleanupReason) } : {},
2764
+ ...readString(metadataEvent.source) ? { source: readString(metadataEvent.source) } : {}
2765
+ };
2766
+ }
2767
+ async function drainCoordinatorPendingEvents(ctx, opts) {
2768
+ const requestedNodeIds = opts?.nodeIds?.length ? new Set(opts.nodeIds) : null;
2769
+ const matchesCurrentMesh = (event) => readString(event?.meshId) === ctx.mesh.id;
2770
+ if (ctx.transport instanceof IpcTransport) {
2771
+ const surfacedEvents = [];
2772
+ const coordinatorDaemonId = readString(ctx.localDaemonId);
2773
+ const pendingEventArgs = {
2774
+ meshId: ctx.mesh.id,
2775
+ ...coordinatorDaemonId ? { coordinatorDaemonId } : {}
2776
+ };
2777
+ try {
2778
+ const localEvents = normalizePendingMeshCoordinatorEvents(await ctx.transport.command("get_pending_mesh_events", pendingEventArgs)).filter(matchesCurrentMesh);
2779
+ for (const event of localEvents) {
2780
+ const payload = buildMeshForwardPayloadFromPendingEvent(event);
2781
+ if (!payload.event || !payload.meshId) continue;
2782
+ let injected = false;
2783
+ try {
2784
+ await ctx.transport.command("mesh_forward_event", payload);
2785
+ injected = true;
2786
+ } catch {
2787
+ }
2788
+ rememberMeshSessionProviderMetadataFromEvent({ ...event, metadataEvent: payload });
2789
+ if (!injected) surfacedEvents.push(event);
2790
+ }
2791
+ } catch {
2728
2792
  }
2729
- }
2730
- };
2731
- var MESH_SUGGEST_CHANGE_IMPACT_CONFIG_TOOL = {
2732
- name: "mesh_suggest_change_impact_config",
2733
- description: "Suggest a Change Impact config scaffold from the repo package layout (web-* \u2192 web-only, others \u2192 daemon-runtime, plus docs/license markers as non-runtime). Heuristic scaffold only \u2014 the draft must be reviewed and saved before it takes effect; nothing is executed.",
2734
- inputSchema: {
2735
- type: "object",
2736
- properties: {
2737
- node_id: { type: "string", description: "Optional node/workspace used for suggestions. Defaults to the first mesh node." }
2793
+ for (const node of ctx.mesh.nodes) {
2794
+ if (!node.daemonId || isLocalControlPlaneNode(ctx, node)) continue;
2795
+ if (requestedNodeIds && !requestedNodeIds.has(node.id)) continue;
2796
+ try {
2797
+ const remoteEvents = normalizePendingMeshCoordinatorEvents(
2798
+ await ctx.transport.meshCommand(node.daemonId, "get_pending_mesh_events", pendingEventArgs)
2799
+ ).filter(matchesCurrentMesh);
2800
+ if (remoteEvents.length === 0) continue;
2801
+ for (const event of remoteEvents) {
2802
+ const payload = buildMeshForwardPayloadFromPendingEvent(event);
2803
+ if (!payload.event || !payload.meshId) continue;
2804
+ await ctx.transport.command("mesh_forward_event", payload);
2805
+ rememberMeshSessionProviderMetadataFromEvent({ ...event, metadataEvent: payload });
2806
+ }
2807
+ } catch {
2808
+ }
2738
2809
  }
2739
- }
2740
- };
2741
- var MESH_INIT_TOOL = {
2742
- name: "mesh_init",
2743
- description: "One-click mesh onboarding for an existing git project. Detects installed CLI providers, suggests Refinery (.adhdev/refine.json) and worktree bootstrap (.adhdev/worktree_bootstrap.json) configs, optionally writes them to disk, and recommends a node providerPriority from the detected providers. Suggestions are scaffold only and never execute until saved; providerPriority is a recommendation to apply to node policy, not auto-applied. Defaults to dry-run (no files written) and never overwrites an existing config unless overwrite=true.",
2744
- inputSchema: {
2745
- type: "object",
2746
- properties: {
2747
- node_id: { type: "string", description: "Optional node/workspace to onboard. Defaults to the first mesh node with a workspace." },
2748
- write: { type: "boolean", description: "When true, persist the suggested configs to disk. Defaults false (dry-run preview only)." },
2749
- overwrite: { type: "boolean", description: "When true, overwrite an existing config file. Defaults false (never clobber an existing refine/bootstrap config)." }
2810
+ try {
2811
+ const localEvents = normalizePendingMeshCoordinatorEvents(await ctx.transport.command("get_pending_mesh_events", pendingEventArgs)).filter(matchesCurrentMesh);
2812
+ for (const event of localEvents) {
2813
+ const payload = buildMeshForwardPayloadFromPendingEvent(event);
2814
+ if (!payload.event || !payload.meshId) continue;
2815
+ let injected = false;
2816
+ try {
2817
+ await ctx.transport.command("mesh_forward_event", payload);
2818
+ injected = true;
2819
+ } catch {
2820
+ }
2821
+ rememberMeshSessionProviderMetadataFromEvent({ ...event, metadataEvent: payload });
2822
+ if (!injected) surfacedEvents.push(event);
2823
+ }
2824
+ } catch {
2750
2825
  }
2826
+ return surfacedEvents;
2751
2827
  }
2752
- };
2753
- var MESH_REFINE_PLAN_TOOL = {
2754
- name: "mesh_refine_plan",
2755
- description: "Dry-run Refinery plan for a worktree node: reports config source, validation commands, suggestions/unavailable reason, and merge/cleanup intent without executing validation or git merge.",
2756
- inputSchema: {
2757
- type: "object",
2758
- properties: {
2759
- node_id: { type: "string", description: "Node ID of the worktree node to plan." }
2760
- },
2761
- required: ["node_id"]
2762
- }
2763
- };
2764
- var MESH_REVIEW_INBOX_TOOL = {
2765
- name: "mesh_review_inbox",
2766
- description: "List local worktree nodes that need human review: merge candidates (pushed feature branches ready to merge) and Refinery-blocked review results. Returns evidence summaries, diff stats vs. the default branch, and suggested actions (Refine / Requeue / Dismiss). Remote nodes are excluded in M4.0.",
2767
- inputSchema: {
2768
- type: "object",
2769
- properties: {
2770
- mesh_id: { type: "string", description: "Mesh ID (optional \u2014 inferred from active mesh if omitted)." }
2771
- },
2772
- required: []
2773
- }
2774
- };
2775
- var ALL_MESH_TOOLS = [
2776
- MESH_STATUS_TOOL,
2777
- MESH_LIST_NODES_TOOL,
2778
- MESH_ENQUEUE_TASK_TOOL,
2779
- MESH_VIEW_QUEUE_TOOL,
2780
- MESH_QUEUE_CANCEL_TOOL,
2781
- MESH_QUEUE_REQUEUE_TOOL,
2782
- MESH_SEND_TASK_TOOL,
2783
- MESH_READ_CHAT_TOOL,
2784
- MESH_READ_DEBUG_TOOL,
2785
- MESH_LAUNCH_SESSION_TOOL,
2786
- MESH_GIT_STATUS_TOOL,
2787
- MESH_READ_NODE_LOGS_TOOL,
2788
- MESH_FAST_FORWARD_NODE_TOOL,
2789
- MESH_RESTART_DAEMON_TOOL,
2790
- MESH_CHECKPOINT_TOOL,
2791
- MESH_APPROVE_TOOL,
2792
- MESH_CLONE_NODE_TOOL,
2793
- MESH_REMOVE_NODE_TOOL,
2794
- MESH_REFINE_NODE_TOOL,
2795
- MESH_REFINE_BATCH_TOOL,
2796
- MESH_REFINE_CONFIG_SCHEMA_TOOL,
2797
- MESH_VALIDATE_REFINE_CONFIG_TOOL,
2798
- MESH_SUGGEST_REFINE_CONFIG_TOOL,
2799
- MESH_CHANGE_IMPACT_CONFIG_SCHEMA_TOOL,
2800
- MESH_VALIDATE_CHANGE_IMPACT_CONFIG_TOOL,
2801
- MESH_SUGGEST_CHANGE_IMPACT_CONFIG_TOOL,
2802
- MESH_INIT_TOOL,
2803
- MESH_REFINE_PLAN_TOOL,
2804
- MESH_CLEANUP_SESSIONS_TOOL,
2805
- MESH_PRUNE_STALE_DIRECT_TOOL,
2806
- MESH_TASK_HISTORY_TOOL,
2807
- MESH_RECONCILE_LEDGER_TOOL,
2808
- MESH_MISSION_UPSERT_TOOL,
2809
- MESH_MISSION_LIST_TOOL,
2810
- MESH_REVIEW_INBOX_TOOL
2811
- ];
2828
+ const events = (0, import_daemon_core2.drainPendingMeshCoordinatorEvents)(ctx.mesh.id, ctx.localDaemonId).filter(matchesCurrentMesh);
2829
+ events.forEach(rememberMeshSessionProviderMetadataFromEvent);
2830
+ return events;
2831
+ }
2832
+ function isP2pTransportUnavailableError(error) {
2833
+ return (0, import_daemon_core2.isP2pRelayTransportFailure)(error);
2834
+ }
2835
+ function buildRemoveNodeArgs(ctx, nodeId, sessionCleanupMode, force) {
2836
+ return {
2837
+ meshId: ctx.mesh.id,
2838
+ nodeId,
2839
+ ...sessionCleanupMode ? { sessionCleanupMode } : {},
2840
+ ...force === true ? { force: true } : {},
2841
+ inlineMesh: ctx.mesh
2842
+ };
2843
+ }
2812
2844
  async function meshStatus(ctx, args = {}) {
2813
- const rateResult = (0, import_daemon_core.recordMeshToolCall)({ meshId: ctx.mesh.id, tool: "mesh_status" });
2845
+ const rateResult = (0, import_daemon_core2.recordMeshToolCall)({ meshId: ctx.mesh.id, tool: "mesh_status" });
2814
2846
  const compact = args.verbose === true ? false : args.compact ?? true;
2815
2847
  await refreshMeshFromDaemon(ctx);
2816
2848
  const { mesh, transport } = ctx;
2817
- let ledgerSummary = (0, import_daemon_core.getLedgerSummary)(mesh.id);
2849
+ let ledgerSummary = (0, import_daemon_core2.getLedgerSummary)(mesh.id);
2818
2850
  const results = await Promise.all(mesh.nodes.map(async (node) => {
2819
2851
  const entry = {
2820
2852
  nodeId: node.id,
@@ -2870,14 +2902,14 @@ async function meshStatus(ctx, args = {}) {
2870
2902
  noFallbackReason: failure.noFallbackReason
2871
2903
  });
2872
2904
  }
2873
- entry.dataFreshness = (0, import_daemon_core.buildMeshNodeProbeFreshness)({
2905
+ entry.dataFreshness = (0, import_daemon_core2.buildMeshNodeProbeFreshness)({
2874
2906
  git: entry.git,
2875
2907
  liveTruthProbed,
2876
2908
  isSelfNode: entry.machine?.sameMachine === true,
2877
2909
  daemonId: readNodeDaemonId(node),
2878
2910
  node
2879
2911
  });
2880
- const recoveryContext = (0, import_daemon_core.getSessionRecoveryContext)(mesh.id, { nodeId: node.id });
2912
+ const recoveryContext = (0, import_daemon_core2.getSessionRecoveryContext)(mesh.id, { nodeId: node.id });
2881
2913
  if (recoveryContext.consecutiveNodeFailures > 0) {
2882
2914
  entry.recoveryHints = {
2883
2915
  consecutiveFailures: recoveryContext.consecutiveNodeFailures,
@@ -2949,23 +2981,23 @@ async function meshStatus(ctx, args = {}) {
2949
2981
  }
2950
2982
  return entry;
2951
2983
  }));
2952
- let ledgerEntries = (0, import_daemon_core.readLedgerEntries)(mesh.id, { tail: 200 });
2953
- let directDispatches = (0, import_daemon_core.getActiveDirectDispatches)(mesh.id);
2984
+ let ledgerEntries = (0, import_daemon_core2.readLedgerEntries)(mesh.id, { tail: 200 });
2985
+ let directDispatches = (0, import_daemon_core2.getActiveDirectDispatches)(mesh.id);
2954
2986
  const directReconciliation = await reconcileDirectDispatchesFromTranscriptEvidence(ctx, results, directDispatches, ledgerEntries);
2955
2987
  if (directReconciliation.reconciled > 0) {
2956
- ledgerEntries = (0, import_daemon_core.readLedgerEntries)(mesh.id, { tail: 200 });
2957
- directDispatches = (0, import_daemon_core.getActiveDirectDispatches)(mesh.id);
2958
- ledgerSummary = (0, import_daemon_core.getLedgerSummary)(mesh.id);
2988
+ ledgerEntries = (0, import_daemon_core2.readLedgerEntries)(mesh.id, { tail: 200 });
2989
+ directDispatches = (0, import_daemon_core2.getActiveDirectDispatches)(mesh.id);
2990
+ ledgerSummary = (0, import_daemon_core2.getLedgerSummary)(mesh.id);
2959
2991
  }
2960
- const activeWorkEvidence = (0, import_daemon_core.buildMeshActiveWork)({
2992
+ const activeWorkEvidence = (0, import_daemon_core2.buildMeshActiveWork)({
2961
2993
  meshId: mesh.id,
2962
- queue: (0, import_daemon_core.getQueue)(mesh.id),
2994
+ queue: (0, import_daemon_core2.getQueue)(mesh.id),
2963
2995
  ledgerEntries,
2964
2996
  directDispatches,
2965
2997
  nodes: results
2966
2998
  });
2967
2999
  const pollingGuidance = buildActiveWorkPollingGuidance(activeWorkEvidence.summary);
2968
- const staleDirectWorkSummary = (0, import_daemon_core.buildCompactStaleDirectWorkSummary)(activeWorkEvidence.staleDirectWork, {
3000
+ const staleDirectWorkSummary = (0, import_daemon_core2.buildCompactStaleDirectWorkSummary)(activeWorkEvidence.staleDirectWork, {
2969
3001
  note: activeWorkEvidence.staleDirectWorkNote,
2970
3002
  detailHint: "Full stale direct entries are omitted from mesh_status by default. Call mesh_status with includeStaleDirectWorkDetails=true or inspect mesh_task_history for ledger detail."
2971
3003
  });
@@ -3153,7 +3185,7 @@ async function meshStatus(ctx, args = {}) {
3153
3185
  }
3154
3186
  try {
3155
3187
  if (compact) {
3156
- const { live, historyFold } = (0, import_daemon_core.getMeshStatusMissionsCompact)(mesh.id);
3188
+ const { live, historyFold } = (0, import_daemon_core2.getMeshStatusMissionsCompact)(mesh.id);
3157
3189
  const ranked = [...live].sort((a, b) => String(b.tasks?.lastActivityAt ?? "").localeCompare(String(a.tasks?.lastActivityAt ?? "")));
3158
3190
  const kept = [];
3159
3191
  const overflow = [];
@@ -3180,11 +3212,11 @@ async function meshStatus(ctx, args = {}) {
3180
3212
  }
3181
3213
  if (historyFold) response.missionsHistory = historyFold;
3182
3214
  } else {
3183
- const missions = (0, import_daemon_core.getMeshStatusMissionSummaries)(mesh.id, { verbose: true });
3215
+ const missions = (0, import_daemon_core2.getMeshStatusMissionSummaries)(mesh.id, { verbose: true });
3184
3216
  if (missions.length > 0) {
3185
3217
  response.missions = missions.map((mission) => {
3186
3218
  try {
3187
- return { ...mission, stats: (0, import_daemon_core.computeMeshMissionStats)(mesh.id, mission.id) };
3219
+ return { ...mission, stats: (0, import_daemon_core2.computeMeshMissionStats)(mesh.id, mission.id) };
3188
3220
  } catch {
3189
3221
  return mission;
3190
3222
  }
@@ -3195,14 +3227,14 @@ async function meshStatus(ctx, args = {}) {
3195
3227
  }
3196
3228
  try {
3197
3229
  const pendingEvents = await drainCoordinatorPendingEvents(ctx);
3198
- const asyncRefineJobs = (0, import_daemon_core.buildMeshAsyncRefineJobs)({
3230
+ const asyncRefineJobs = (0, import_daemon_core2.buildMeshAsyncRefineJobs)({
3199
3231
  meshId: mesh.id,
3200
3232
  ledgerEntries,
3201
3233
  pendingEvents
3202
3234
  });
3203
3235
  if (asyncRefineJobs.length > 0) {
3204
3236
  if (compact) {
3205
- const summary = (0, import_daemon_core.summarizeMeshAsyncRefineJobs)(asyncRefineJobs);
3237
+ const summary = (0, import_daemon_core2.summarizeMeshAsyncRefineJobs)(asyncRefineJobs);
3206
3238
  if (summary.activeJobs.length > 0) response.asyncRefineJobs = summary.activeJobs;
3207
3239
  response.asyncRefineJobsSummary = {
3208
3240
  total: summary.total,
@@ -3228,17 +3260,17 @@ async function meshTaskHistory(ctx, args) {
3228
3260
  const compactCap = requestedTail > 50 ? 20 : 30;
3229
3261
  const tail = compact ? Math.min(requestedTail, compactCap) : Math.min(requestedTail, 200);
3230
3262
  const kind = typeof args.kind === "string" && args.kind.trim() ? [args.kind.trim()] : void 0;
3231
- const rawEntries = (0, import_daemon_core.readLedgerEntries)(mesh.id, { tail, kind });
3263
+ const rawEntries = (0, import_daemon_core2.readLedgerEntries)(mesh.id, { tail, kind });
3232
3264
  const entries = compact ? rawEntries.map((e) => ({
3233
3265
  ...e,
3234
3266
  payload: e.payload ? slimLedgerPayload(e.payload) : e.payload
3235
3267
  })) : rawEntries;
3236
- const summary = (0, import_daemon_core.getLedgerSummary)(mesh.id);
3268
+ const summary = (0, import_daemon_core2.getLedgerSummary)(mesh.id);
3237
3269
  let taskStats;
3238
3270
  try {
3239
3271
  const taskIds = [...new Set(rawEntries.map((e) => typeof e.payload?.taskId === "string" ? e.payload.taskId : "").filter(Boolean))];
3240
3272
  if (taskIds.length > 0) {
3241
- const stats = (0, import_daemon_core.computeMeshTaskStats)(mesh.id, { taskIds });
3273
+ const stats = (0, import_daemon_core2.computeMeshTaskStats)(mesh.id, { taskIds });
3242
3274
  if (stats.length > 0) taskStats = stats;
3243
3275
  }
3244
3276
  } catch {
@@ -3252,6 +3284,33 @@ async function meshTaskHistory(ctx, args) {
3252
3284
  ...pendingEvents.length > 0 ? { pendingCoordinatorEvents: pendingEvents } : {}
3253
3285
  }, null, 2);
3254
3286
  }
3287
+ async function meshRecordNote(ctx, args) {
3288
+ const { mesh } = ctx;
3289
+ const text = typeof args.text === "string" ? args.text.trim() : "";
3290
+ if (!text) {
3291
+ return JSON.stringify({ success: false, error: "text required" }, null, 2);
3292
+ }
3293
+ const category = args.category === "provider_quirk" || args.category === "pattern_to_avoid" || args.category === "recovery_lesson" ? args.category : void 0;
3294
+ const createdAt = (/* @__PURE__ */ new Date()).toISOString();
3295
+ const sourceCoordinator = ctx.coordinatorSessionId || ctx.localDaemonId || ctx.coordinatorHostname || void 0;
3296
+ const entry = (0, import_daemon_core2.appendLedgerEntry)(mesh.id, {
3297
+ kind: "coordinator_operating_note",
3298
+ ...sourceCoordinator ? { sessionId: sourceCoordinator } : {},
3299
+ payload: {
3300
+ text,
3301
+ ...category ? { category } : {},
3302
+ createdAt,
3303
+ ...sourceCoordinator ? { sourceCoordinator } : {}
3304
+ }
3305
+ });
3306
+ return JSON.stringify({
3307
+ success: true,
3308
+ meshId: mesh.id,
3309
+ noteId: entry.id,
3310
+ recorded: { text, category: category ?? null, createdAt },
3311
+ note: 'Recorded to the mesh ledger. Future coordinators on this mesh will see it under "## Operating Notes" at launch.'
3312
+ }, null, 2);
3313
+ }
3255
3314
  async function meshReconcileLedger(ctx, args) {
3256
3315
  await refreshMeshFromDaemon(ctx);
3257
3316
  const requestedNodeIds = Array.isArray(args.node_ids) ? new Set(args.node_ids.map((id) => typeof id === "string" ? id.trim() : "").filter(Boolean)) : null;
@@ -3267,8 +3326,8 @@ async function meshReconcileLedger(ctx, args) {
3267
3326
  for (const node of nodes) {
3268
3327
  try {
3269
3328
  if (isLocalControlPlaneNode(ctx, node) || !node.daemonId) {
3270
- const slice2 = (0, import_daemon_core.readLedgerSliceFromStore)(ctx.mesh.id, queryArgs);
3271
- replicas.push((0, import_daemon_core.buildMeshLedgerReplicaEvidence)({
3329
+ const slice2 = (0, import_daemon_core2.readLedgerSliceFromStore)(ctx.mesh.id, queryArgs);
3330
+ replicas.push((0, import_daemon_core2.buildMeshLedgerReplicaEvidence)({
3272
3331
  nodeId: node.id,
3273
3332
  daemonId: node.daemonId,
3274
3333
  transport: "local",
@@ -3286,8 +3345,8 @@ async function meshReconcileLedger(ctx, args) {
3286
3345
  if (slice?.protocol !== "adhdev.mesh.ledger.slice.v1" || !Array.isArray(slice.entries)) {
3287
3346
  throw new Error("remote daemon returned an invalid ledger slice payload");
3288
3347
  }
3289
- const importResult = shouldImport ? (0, import_daemon_core.appendRemoteLedgerEntries)(ctx.mesh.id, slice.entries) : { accepted: 0, skippedDuplicate: 0, rejectedInvalid: 0, entries: [] };
3290
- replicas.push((0, import_daemon_core.buildMeshLedgerReplicaEvidence)({
3348
+ const importResult = shouldImport ? (0, import_daemon_core2.appendRemoteLedgerEntries)(ctx.mesh.id, slice.entries) : { accepted: 0, skippedDuplicate: 0, rejectedInvalid: 0, entries: [] };
3349
+ replicas.push((0, import_daemon_core2.buildMeshLedgerReplicaEvidence)({
3291
3350
  nodeId: node.id,
3292
3351
  daemonId: node.daemonId,
3293
3352
  transport: "p2p_datachannel",
@@ -3295,7 +3354,7 @@ async function meshReconcileLedger(ctx, args) {
3295
3354
  importResult
3296
3355
  }));
3297
3356
  if (shouldImport && importResult.accepted > 0) {
3298
- (0, import_daemon_core.appendLedgerEntry)(ctx.mesh.id, {
3357
+ (0, import_daemon_core2.appendLedgerEntry)(ctx.mesh.id, {
3299
3358
  kind: "ledger_replicated",
3300
3359
  nodeId: node.id,
3301
3360
  payload: {
@@ -3309,7 +3368,7 @@ async function meshReconcileLedger(ctx, args) {
3309
3368
  });
3310
3369
  }
3311
3370
  } catch (e) {
3312
- replicas.push((0, import_daemon_core.buildMeshLedgerReplicaEvidence)({
3371
+ replicas.push((0, import_daemon_core2.buildMeshLedgerReplicaEvidence)({
3313
3372
  nodeId: node.id,
3314
3373
  daemonId: node.daemonId,
3315
3374
  transport: node.daemonId ? "p2p_datachannel" : "local",
@@ -3318,8 +3377,8 @@ async function meshReconcileLedger(ctx, args) {
3318
3377
  }));
3319
3378
  }
3320
3379
  }
3321
- const evidence = (0, import_daemon_core.buildMeshLedgerReconciliationEvidence)(ctx.mesh.id, replicas);
3322
- (0, import_daemon_core.appendLedgerEntry)(ctx.mesh.id, {
3380
+ const evidence = (0, import_daemon_core2.buildMeshLedgerReconciliationEvidence)(ctx.mesh.id, replicas);
3381
+ (0, import_daemon_core2.appendLedgerEntry)(ctx.mesh.id, {
3323
3382
  kind: "ledger_reconciled",
3324
3383
  payload: {
3325
3384
  protocol: evidence.protocol,
@@ -3335,11 +3394,11 @@ async function meshPruneStaleDirect(ctx, args = {}) {
3335
3394
  const execute = args.execute === true && args.dry_run !== true;
3336
3395
  const includeTerminal = args.include_terminal === true;
3337
3396
  const liveNodes = await collectMeshViewQueueNodesWithLiveSessions(ctx);
3338
- const ledgerEntries = (0, import_daemon_core.readLedgerEntries)(ctx.mesh.id, { tail: 500 });
3339
- const directDispatches = (0, import_daemon_core.getActiveDirectDispatches)(ctx.mesh.id);
3340
- const result = (0, import_daemon_core.pruneStaleDirectDispatches)({
3397
+ const ledgerEntries = (0, import_daemon_core2.readLedgerEntries)(ctx.mesh.id, { tail: 500 });
3398
+ const directDispatches = (0, import_daemon_core2.getActiveDirectDispatches)(ctx.mesh.id);
3399
+ const result = (0, import_daemon_core2.pruneStaleDirectDispatches)({
3341
3400
  meshId: ctx.mesh.id,
3342
- queue: (0, import_daemon_core.getQueue)(ctx.mesh.id),
3401
+ queue: (0, import_daemon_core2.getQueue)(ctx.mesh.id),
3343
3402
  ledgerEntries,
3344
3403
  directDispatches,
3345
3404
  nodes: liveNodes,
@@ -3402,7 +3461,7 @@ async function meshListNodes(ctx) {
3402
3461
  }
3403
3462
  async function meshMissionUpsert(ctx, args) {
3404
3463
  try {
3405
- const mission = (0, import_daemon_core.upsertMeshMission)(ctx.mesh.id, {
3464
+ const mission = (0, import_daemon_core2.upsertMeshMission)(ctx.mesh.id, {
3406
3465
  id: readString(args.mission_id) || readString(args.missionId) || void 0,
3407
3466
  title: args.title,
3408
3467
  goal: typeof args.goal === "string" ? args.goal : void 0,
@@ -3422,21 +3481,21 @@ async function meshMissionUpsert(ctx, args) {
3422
3481
  async function meshMissionList(ctx, args = {}) {
3423
3482
  try {
3424
3483
  const rawStatuses = Array.isArray(args.status) ? args.status : typeof args.status === "string" && args.status.trim() ? [args.status] : [];
3425
- const invalid = rawStatuses.filter((s) => !import_daemon_core.MESH_MISSION_STATUSES.includes(s));
3484
+ const invalid = rawStatuses.filter((s) => !import_daemon_core2.MESH_MISSION_STATUSES.includes(s));
3426
3485
  if (invalid.length > 0) {
3427
3486
  return JSON.stringify({
3428
3487
  success: false,
3429
3488
  code: "invalid_mission_status",
3430
- error: `invalid status filter: ${invalid.join(", ")} (valid: ${import_daemon_core.MESH_MISSION_STATUSES.join(", ")})`
3489
+ error: `invalid status filter: ${invalid.join(", ")} (valid: ${import_daemon_core2.MESH_MISSION_STATUSES.join(", ")})`
3431
3490
  });
3432
3491
  }
3433
3492
  const statuses = rawStatuses.length > 0 ? rawStatuses : void 0;
3434
- const missions = (0, import_daemon_core.listMeshMissionSummaries)(ctx.mesh.id, {
3493
+ const missions = (0, import_daemon_core2.listMeshMissionSummaries)(ctx.mesh.id, {
3435
3494
  statuses,
3436
3495
  verbose: args.verbose === true
3437
3496
  }).map((mission) => {
3438
3497
  try {
3439
- return { ...mission, stats: (0, import_daemon_core.computeMeshMissionStats)(ctx.mesh.id, mission.id) };
3498
+ return { ...mission, stats: (0, import_daemon_core2.computeMeshMissionStats)(ctx.mesh.id, mission.id) };
3440
3499
  } catch {
3441
3500
  return mission;
3442
3501
  }
@@ -3453,14 +3512,14 @@ async function meshMissionList(ctx, args = {}) {
3453
3512
  }
3454
3513
  async function meshEnqueueTask(ctx, args) {
3455
3514
  const taskMode = readString(args.task_mode) || readString(args.taskMode);
3456
- const requiredTags = (0, import_daemon_core.normalizeMeshCapabilityTags)(Array.isArray(args.requiredTags) ? args.requiredTags : args.required_tags);
3515
+ const requiredTags = (0, import_daemon_core2.normalizeMeshCapabilityTags)(Array.isArray(args.requiredTags) ? args.requiredTags : args.required_tags);
3457
3516
  const dependsOn = Array.isArray(args.dependsOn) ? args.dependsOn : Array.isArray(args.depends_on) ? args.depends_on : void 0;
3458
3517
  const missionId = readString(args.missionId) || readString(args.mission_id) || void 0;
3459
3518
  const explicitTarget = readString(args.targetNodeId) || readString(args.target_node_id) || void 0;
3460
3519
  const preferWorktree = args.preferWorktree === true || args.prefer_worktree === true;
3461
3520
  const targetNodeId = explicitTarget || (preferWorktree ? resolvePreferredWorktreeNodeId(ctx) : void 0);
3462
3521
  try {
3463
- const task = (0, import_daemon_core.enqueueTask)(ctx.mesh.id, args.message, { taskMode, requiredTags, dependsOn, missionId, targetNodeId, ...ctx.coordinatorSessionId ? { sourceCoordinatorSessionId: ctx.coordinatorSessionId } : {} });
3522
+ const task = (0, import_daemon_core2.enqueueTask)(ctx.mesh.id, args.message, { taskMode, requiredTags, dependsOn, missionId, targetNodeId, ...ctx.coordinatorSessionId ? { sourceCoordinatorSessionId: ctx.coordinatorSessionId } : {} });
3464
3523
  if (!(ctx.transport instanceof IpcTransport)) {
3465
3524
  const queueTrigger = await triggerMeshQueueAndReport(ctx);
3466
3525
  return JSON.stringify({
@@ -3483,14 +3542,14 @@ async function meshEnqueueTask(ctx, args) {
3483
3542
  const isLocalNode = isLocalControlPlaneNode(ctx, node);
3484
3543
  if (isLocalNode || !node.daemonId) continue;
3485
3544
  if (targetNodeId && node.id !== targetNodeId) continue;
3486
- if (!(0, import_daemon_core.nodeSatisfiesRequiredTags)(requiredTags, (0, import_daemon_core.buildMeshNodeCapabilityTags)(node))) continue;
3545
+ if (!(0, import_daemon_core2.nodeSatisfiesRequiredTags)(requiredTags, (0, import_daemon_core2.buildMeshNodeCapabilityTags)(node))) continue;
3487
3546
  dispatchPromises.push(
3488
3547
  ipcDispatchToRemoteAgent(ctx, node, { message: args.message }).then((result) => {
3489
3548
  if (result.success) {
3490
3549
  try {
3491
3550
  const providerType = result.providerType;
3492
3551
  const descriptor = summarizeTaskMessage(args.message);
3493
- (0, import_daemon_core.appendLedgerEntry)(ctx.mesh.id, {
3552
+ (0, import_daemon_core2.appendLedgerEntry)(ctx.mesh.id, {
3494
3553
  kind: "task_dispatched",
3495
3554
  nodeId: node.id,
3496
3555
  sessionId: result.sessionId,
@@ -3512,7 +3571,7 @@ async function meshEnqueueTask(ctx, args) {
3512
3571
  }
3513
3572
  }).catch((err) => {
3514
3573
  try {
3515
- (0, import_daemon_core.appendLedgerEntry)(ctx.mesh.id, {
3574
+ (0, import_daemon_core2.appendLedgerEntry)(ctx.mesh.id, {
3516
3575
  kind: "p2p_dispatch_failed",
3517
3576
  nodeId: node.id,
3518
3577
  payload: {
@@ -3555,17 +3614,17 @@ async function meshEnqueueTask(ctx, args) {
3555
3614
  }
3556
3615
  }
3557
3616
  async function meshViewQueue(ctx, args) {
3558
- const rateResult = (0, import_daemon_core.recordMeshToolCall)({ meshId: ctx.mesh.id, tool: "mesh_view_queue" });
3617
+ const rateResult = (0, import_daemon_core2.recordMeshToolCall)({ meshId: ctx.mesh.id, tool: "mesh_view_queue" });
3559
3618
  const compact = args.verbose === true ? false : args.compact ?? true;
3560
3619
  try {
3561
3620
  await refreshMeshFromDaemon(ctx);
3562
3621
  const statusFilter = sanitizeQueueStatusFilter(args.status);
3563
3622
  const view = normalizeQueueViewMode(args.view);
3564
- const rawQueue = (0, import_daemon_core.getQueue)(ctx.mesh.id);
3623
+ const rawQueue = (0, import_daemon_core2.getQueue)(ctx.mesh.id);
3565
3624
  const statusById = new Map(rawQueue.map((task) => [task.id, task.status]));
3566
3625
  const withDependencies = rawQueue.map((task) => {
3567
3626
  if (!Array.isArray(task.dependsOn) || task.dependsOn.length === 0) return task;
3568
- const depState = (0, import_daemon_core.describeTaskDependencyState)(task, statusById);
3627
+ const depState = (0, import_daemon_core2.describeTaskDependencyState)(task, statusById);
3569
3628
  return { ...task, ...depState };
3570
3629
  });
3571
3630
  const fullQueue = prioritizeActiveQueueRows(annotateQueueStaleness(withDependencies, ctx.mesh));
@@ -3574,16 +3633,16 @@ async function meshViewQueue(ctx, args) {
3574
3633
  const visibleSummary = buildQueueStatusSummary(queue);
3575
3634
  const maintenance = buildQueueMaintenanceReport(fullQueue);
3576
3635
  const liveNodes = await collectMeshViewQueueNodesWithLiveSessions(ctx);
3577
- let ledgerEntries = (0, import_daemon_core.readLedgerEntries)(ctx.mesh.id, { tail: 200 });
3578
- let directDispatches = (0, import_daemon_core.getActiveDirectDispatches)(ctx.mesh.id);
3636
+ let ledgerEntries = (0, import_daemon_core2.readLedgerEntries)(ctx.mesh.id, { tail: 200 });
3637
+ let directDispatches = (0, import_daemon_core2.getActiveDirectDispatches)(ctx.mesh.id);
3579
3638
  const directReconciliation = await reconcileDirectDispatchesFromTranscriptEvidence(ctx, liveNodes, directDispatches, ledgerEntries);
3580
3639
  if (directReconciliation.reconciled > 0) {
3581
- ledgerEntries = (0, import_daemon_core.readLedgerEntries)(ctx.mesh.id, { tail: 200 });
3582
- directDispatches = (0, import_daemon_core.getActiveDirectDispatches)(ctx.mesh.id);
3640
+ ledgerEntries = (0, import_daemon_core2.readLedgerEntries)(ctx.mesh.id, { tail: 200 });
3641
+ directDispatches = (0, import_daemon_core2.getActiveDirectDispatches)(ctx.mesh.id);
3583
3642
  }
3584
- (0, import_daemon_core.markStaleDirectDispatches)(ctx.mesh.id);
3585
- directDispatches = (0, import_daemon_core.getActiveDirectDispatches)(ctx.mesh.id);
3586
- const activeWorkEvidence = (0, import_daemon_core.buildMeshActiveWork)({
3643
+ (0, import_daemon_core2.markStaleDirectDispatches)(ctx.mesh.id);
3644
+ directDispatches = (0, import_daemon_core2.getActiveDirectDispatches)(ctx.mesh.id);
3645
+ const activeWorkEvidence = (0, import_daemon_core2.buildMeshActiveWork)({
3587
3646
  meshId: ctx.mesh.id,
3588
3647
  queue: fullQueue,
3589
3648
  ledgerEntries,
@@ -3608,7 +3667,7 @@ async function meshViewQueue(ctx, args) {
3608
3667
  const wantActiveQueueArray = view === "active" || statusFilter?.some((status) => ACTIVE_QUEUE_STATUSES.has(status));
3609
3668
  const wantHistoricalQueueArray = !compact && (view === "historical" || requestedHistoricalRows);
3610
3669
  const activeWorkResult = compact ? compactActiveWorkRecords(activeWorkEvidence.activeWork) : { records: activeWorkEvidence.activeWork, omitted: 0 };
3611
- const staleDirectWorkSummary = (0, import_daemon_core.buildCompactStaleDirectWorkSummary)(activeWorkEvidence.staleDirectWork, {
3670
+ const staleDirectWorkSummary = (0, import_daemon_core2.buildCompactStaleDirectWorkSummary)(activeWorkEvidence.staleDirectWork, {
3612
3671
  note: activeWorkEvidence.staleDirectWorkNote,
3613
3672
  detailHint: "Full stale direct entries are omitted from mesh_view_queue in compact mode. Call mesh_view_queue with verbose=true, or inspect mesh_task_history for ledger detail."
3614
3673
  });
@@ -3683,7 +3742,7 @@ async function meshQueueCancel(ctx, args) {
3683
3742
  try {
3684
3743
  const taskId = (args.task_id || args.taskId || "").trim();
3685
3744
  if (!taskId) return JSON.stringify({ success: false, error: "task_id required" });
3686
- const task = (0, import_daemon_core.cancelTask)(ctx.mesh.id, taskId, { reason: args.reason });
3745
+ const task = (0, import_daemon_core2.cancelTask)(ctx.mesh.id, taskId, { reason: args.reason });
3687
3746
  if (!task) return JSON.stringify({ success: false, error: `Queue task '${taskId}' not found` });
3688
3747
  ctx.transport.command("trigger_mesh_queue", { meshId: ctx.mesh.id }).catch(() => {
3689
3748
  });
@@ -3699,7 +3758,7 @@ async function meshQueueRequeue(ctx, args) {
3699
3758
  const targetNodeId = (args.target_node_id || args.targetNodeId || "").trim() || void 0;
3700
3759
  const targetSessionId = (args.target_session_id || args.targetSessionId || "").trim() || void 0;
3701
3760
  const keepTargetSession = args.keep_target_session === true || args.keepTargetSession === true;
3702
- const task = (0, import_daemon_core.requeueTask)(ctx.mesh.id, taskId, {
3761
+ const task = (0, import_daemon_core2.requeueTask)(ctx.mesh.id, taskId, {
3703
3762
  reason: args.reason,
3704
3763
  targetNodeId,
3705
3764
  targetSessionId,
@@ -3731,7 +3790,7 @@ async function meshQueueRequeue(ctx, args) {
3731
3790
  async function meshSendTask(ctx, args) {
3732
3791
  const requestedTaskMode = readString(args.task_mode) || readString(args.taskMode);
3733
3792
  const missionId = readString(args.missionId) || readString(args.mission_id) || void 0;
3734
- const modeValidation = (0, import_daemon_core.validateMeshTaskModeRequest)(requestedTaskMode, args.message);
3793
+ const modeValidation = (0, import_daemon_core2.validateMeshTaskModeRequest)(requestedTaskMode, args.message);
3735
3794
  if (!modeValidation.valid) {
3736
3795
  return JSON.stringify({
3737
3796
  success: false,
@@ -3843,7 +3902,7 @@ async function meshSendTask(ctx, args) {
3843
3902
  const dispatchedAt = (/* @__PURE__ */ new Date()).toISOString();
3844
3903
  try {
3845
3904
  const providerType = result2.providerType || cached?.providerType;
3846
- (0, import_daemon_core.appendLedgerEntry)(ctx.mesh.id, {
3905
+ (0, import_daemon_core2.appendLedgerEntry)(ctx.mesh.id, {
3847
3906
  kind: "task_dispatched",
3848
3907
  nodeId: args.node_id,
3849
3908
  sessionId: dispatchedSessionId,
@@ -3855,7 +3914,7 @@ async function meshSendTask(ctx, args) {
3855
3914
  targetSessionId: dispatchedSessionId
3856
3915
  })
3857
3916
  });
3858
- (0, import_daemon_core.insertDirectDispatch)(ctx.mesh.id, {
3917
+ (0, import_daemon_core2.insertDirectDispatch)(ctx.mesh.id, {
3859
3918
  taskId,
3860
3919
  nodeId: args.node_id,
3861
3920
  sessionId: dispatchedSessionId,
@@ -3866,7 +3925,7 @@ async function meshSendTask(ctx, args) {
3866
3925
  dispatchedAt
3867
3926
  });
3868
3927
  if (missionId) {
3869
- (0, import_daemon_core.recordDirectDispatchTask)(ctx.mesh.id, args.message, {
3928
+ (0, import_daemon_core2.recordDirectDispatchTask)(ctx.mesh.id, args.message, {
3870
3929
  id: taskId,
3871
3930
  missionId,
3872
3931
  assignedNodeId: args.node_id,
@@ -4025,7 +4084,7 @@ async function meshSendTask(ctx, args) {
4025
4084
  });
4026
4085
  }
4027
4086
  try {
4028
- (0, import_daemon_core.appendLedgerEntry)(ctx.mesh.id, {
4087
+ (0, import_daemon_core2.appendLedgerEntry)(ctx.mesh.id, {
4029
4088
  kind: "task_dispatched",
4030
4089
  nodeId: args.node_id,
4031
4090
  sessionId: args.session_id,
@@ -4040,7 +4099,7 @@ async function meshSendTask(ctx, args) {
4040
4099
  });
4041
4100
  } catch {
4042
4101
  }
4043
- (0, import_daemon_core.insertDirectDispatch)(ctx.mesh.id, {
4102
+ (0, import_daemon_core2.insertDirectDispatch)(ctx.mesh.id, {
4044
4103
  taskId,
4045
4104
  nodeId: args.node_id,
4046
4105
  sessionId: args.session_id,
@@ -4053,7 +4112,7 @@ async function meshSendTask(ctx, args) {
4053
4112
  });
4054
4113
  if (missionId) {
4055
4114
  try {
4056
- (0, import_daemon_core.recordDirectDispatchTask)(ctx.mesh.id, args.message, {
4115
+ (0, import_daemon_core2.recordDirectDispatchTask)(ctx.mesh.id, args.message, {
4057
4116
  id: taskId,
4058
4117
  missionId,
4059
4118
  assignedNodeId: args.node_id,
@@ -4098,14 +4157,14 @@ async function meshSendTask(ctx, args) {
4098
4157
  } : {}
4099
4158
  });
4100
4159
  }
4101
- const task = (0, import_daemon_core.enqueueTask)(ctx.mesh.id, args.message, {
4160
+ const task = (0, import_daemon_core2.enqueueTask)(ctx.mesh.id, args.message, {
4102
4161
  targetNodeId: args.node_id,
4103
4162
  targetSessionId: args.session_id,
4104
4163
  taskMode,
4105
4164
  ...missionId ? { missionId } : {}
4106
4165
  });
4107
4166
  const queueTrigger = await triggerMeshQueueAndReport(ctx);
4108
- const pendingEvents = (0, import_daemon_core.drainPendingMeshCoordinatorEvents)(ctx.mesh.id, ctx.localDaemonId);
4167
+ const pendingEvents = (0, import_daemon_core2.drainPendingMeshCoordinatorEvents)(ctx.mesh.id, ctx.localDaemonId);
4109
4168
  const result = {
4110
4169
  success: true,
4111
4170
  source: "queue",
@@ -4138,7 +4197,7 @@ function classifyReadChatTransportCause(error) {
4138
4197
  return "saturated";
4139
4198
  }
4140
4199
  function resolveCachedMeshSessionPreviewFromLedger(ctx, nodeId, sessionId) {
4141
- const entries = (0, import_daemon_core.readLedgerEntries)(ctx.mesh.id, { tail: 200 });
4200
+ const entries = (0, import_daemon_core2.readLedgerEntries)(ctx.mesh.id, { tail: 200 });
4142
4201
  for (let i = entries.length - 1; i >= 0; i -= 1) {
4143
4202
  const entry = entries[i];
4144
4203
  const payload = entry.payload && typeof entry.payload === "object" && !Array.isArray(entry.payload) ? entry.payload : {};
@@ -4147,7 +4206,7 @@ function resolveCachedMeshSessionPreviewFromLedger(ctx, nodeId, sessionId) {
4147
4206
  const entrySessionId = readString(entry.sessionId) || readString(payload.targetSessionId) || readString(payload.sessionId) || readString(payload.instanceId);
4148
4207
  if (entrySessionId !== sessionId) continue;
4149
4208
  const metadataEvent = payload.metadataEvent && typeof payload.metadataEvent === "object" && !Array.isArray(payload.metadataEvent) ? payload.metadataEvent : payload;
4150
- const preview = (0, import_daemon_core.resolveMeshSurfacedSessionPreview)(metadataEvent);
4209
+ const preview = (0, import_daemon_core2.resolveMeshSurfacedSessionPreview)(metadataEvent);
4151
4210
  if (preview) {
4152
4211
  return { ...preview, ledgerKind: entry.kind, timestamp: entry.timestamp };
4153
4212
  }
@@ -4155,7 +4214,7 @@ function resolveCachedMeshSessionPreviewFromLedger(ctx, nodeId, sessionId) {
4155
4214
  return void 0;
4156
4215
  }
4157
4216
  function buildMeshReadChatCacheFallback(ctx, args, node, error) {
4158
- const classification = (0, import_daemon_core.classifyP2pRelayFailure)(error, { command: "read_chat", targetDaemonId: node.daemonId });
4217
+ const classification = (0, import_daemon_core2.classifyP2pRelayFailure)(error, { command: "read_chat", targetDaemonId: node.daemonId });
4159
4218
  const cause = classifyReadChatTransportCause(error);
4160
4219
  const errorMessage = error instanceof Error ? error.message : String(error ?? "");
4161
4220
  const causeNote = cause === "not_connected" ? "the worker daemon is not currently connected over P2P (no live channel)" : "the worker daemon is connected but saturated \u2014 it acknowledged the request but did not return the transcript within the deadline";
@@ -4225,7 +4284,7 @@ async function meshReadChat(ctx, args) {
4225
4284
  tailLimit: args.tail ?? 10
4226
4285
  });
4227
4286
  } catch (e) {
4228
- if (isLocalNode || !(0, import_daemon_core.isP2pRelayTransportFailure)(e)) throw e;
4287
+ if (isLocalNode || !(0, import_daemon_core2.isP2pRelayTransportFailure)(e)) throw e;
4229
4288
  return buildMeshReadChatCacheFallback(ctx, args, node, e);
4230
4289
  }
4231
4290
  const payload = annotateRapidReadChatAdvisory(unwrapCommandPayload(result), {
@@ -4293,7 +4352,7 @@ async function meshLaunchSession(ctx, args) {
4293
4352
  const coordinatorNode = resolveCoordinatorNode(ctx);
4294
4353
  const coordinatorDaemonId = resolveCoordinatorDaemonId(ctx);
4295
4354
  const spawnedSessionVisibility = readSpawnedSessionVisibility(ctx.mesh.policy);
4296
- const delegatedWorkerAutoApprove = (0, import_daemon_core.resolveDelegatedWorkerAutoApprove)(ctx.mesh.policy, node.policy);
4355
+ const delegatedWorkerAutoApprove = (0, import_daemon_core2.resolveDelegatedWorkerAutoApprove)(ctx.mesh.policy, node.policy);
4297
4356
  const isLocalNode = isLocalControlPlaneNode(ctx, node);
4298
4357
  if (node.daemonId && !isLocalNode && !coordinatorDaemonId) {
4299
4358
  return JSON.stringify(buildMissingCoordinatorDaemonIdFailure(ctx, node, resolvedProviderType), null, 2);
@@ -4340,7 +4399,7 @@ async function meshLaunchSession(ctx, args) {
4340
4399
  });
4341
4400
  }
4342
4401
  try {
4343
- (0, import_daemon_core.appendLedgerEntry)(ctx.mesh.id, {
4402
+ (0, import_daemon_core2.appendLedgerEntry)(ctx.mesh.id, {
4344
4403
  kind: "session_launched",
4345
4404
  nodeId: args.node_id,
4346
4405
  sessionId: runtimeSessionId || void 0,
@@ -4493,7 +4552,7 @@ async function meshCheckpoint(ctx, args) {
4493
4552
  includeUntracked: true
4494
4553
  });
4495
4554
  try {
4496
- (0, import_daemon_core.appendLedgerEntry)(ctx.mesh.id, {
4555
+ (0, import_daemon_core2.appendLedgerEntry)(ctx.mesh.id, {
4497
4556
  kind: "checkpoint_created",
4498
4557
  nodeId: args.node_id,
4499
4558
  payload: {
@@ -5958,6 +6017,9 @@ async function startMcpServer(opts) {
5958
6017
  case "mesh_task_history":
5959
6018
  text = await meshTaskHistory(meshCtx, a);
5960
6019
  break;
6020
+ case "mesh_record_note":
6021
+ text = await meshRecordNote(meshCtx, a);
6022
+ break;
5961
6023
  case "mesh_reconcile_ledger":
5962
6024
  text = await meshReconcileLedger(meshCtx, a);
5963
6025
  break;