@adhdev/daemon-core 0.9.82-rc.30 → 0.9.82-rc.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -24072,13 +24072,21 @@ function readBooleanValue(...values) {
24072
24072
  }
24073
24073
  return void 0;
24074
24074
  }
24075
- function readGitSubmodules(value) {
24075
+ function joinRepoPath(root, relativePath) {
24076
+ const normalizedRoot = typeof root === "string" ? root.trim().replace(/[\\/]+$/, "") : "";
24077
+ const normalizedPath = typeof relativePath === "string" ? relativePath.trim() : "";
24078
+ if (!normalizedPath) return void 0;
24079
+ if (/^(?:[A-Za-z]:[\\/]|\/)/.test(normalizedPath)) return normalizedPath;
24080
+ if (!normalizedRoot) return void 0;
24081
+ return `${normalizedRoot}/${normalizedPath.replace(/^[\\/]+/, "")}`;
24082
+ }
24083
+ function readGitSubmodules(value, parentRepoRoot) {
24076
24084
  if (!Array.isArray(value)) return void 0;
24077
24085
  const submodules = value.map((entry) => {
24078
24086
  const submodule = readObjectRecord(entry);
24079
24087
  const path28 = readStringValue(submodule.path);
24080
24088
  const commit = readStringValue(submodule.commit);
24081
- const repoPath = readStringValue(submodule.repoPath, submodule.repo_root);
24089
+ const repoPath = readStringValue(submodule.repoPath, submodule.repo_root) ?? joinRepoPath(parentRepoRoot, path28);
24082
24090
  if (!path28 || !commit || !repoPath) return null;
24083
24091
  return {
24084
24092
  path: path28,
@@ -24098,10 +24106,11 @@ function normalizeInlineMeshGitStatus(status, node, options) {
24098
24106
  const conflictFiles = Array.isArray(status.conflictFiles) ? status.conflictFiles.filter((value) => typeof value === "string") : [];
24099
24107
  const conflictCount = readNumberValue(status.conflicts) ?? conflictFiles.length;
24100
24108
  const hasConflicts = readBooleanValue(status.hasConflicts) ?? conflictCount > 0;
24101
- const submodules = readGitSubmodules(status.submodules);
24109
+ const repoRoot = readStringValue(status.repoRoot, status.repo_root, node?.repoRoot, node?.repo_root, status.workspace, node?.workspace) || void 0;
24110
+ const submodules = readGitSubmodules(status.submodules, repoRoot);
24102
24111
  return {
24103
24112
  workspace: readStringValue(status.workspace, node?.workspace) || "",
24104
- repoRoot: readStringValue(status.repoRoot, node?.repoRoot, node?.workspace) || null,
24113
+ repoRoot: repoRoot ?? null,
24105
24114
  isGitRepo,
24106
24115
  branch: readStringValue(status.branch) ?? null,
24107
24116
  headCommit: readStringValue(status.headCommit) ?? null,
@@ -24134,6 +24143,26 @@ function buildInlineMeshTransitGitStatus(node) {
24134
24143
  const status = Object.keys(directStatus).length ? directStatus : Object.keys(nestedStatus).length ? nestedStatus : Object.keys(probeDirectStatus).length ? probeDirectStatus : Object.keys(probeNestedStatus).length ? probeNestedStatus : {};
24135
24144
  return normalizeInlineMeshGitStatus(status, node, { lastCheckedAt: Date.now() });
24136
24145
  }
24146
+ function recordInlineMeshDirectGitTruth(node, git, source) {
24147
+ if (!node || typeof node !== "object" || Array.isArray(node)) return;
24148
+ const checkedAt = readNumberValue(git.lastCheckedAt) ?? Date.now();
24149
+ const updatedAt = new Date(checkedAt).toISOString();
24150
+ const nextGit = {
24151
+ ...git,
24152
+ lastCheckedAt: checkedAt
24153
+ };
24154
+ node.lastGit = {
24155
+ source,
24156
+ checkedAt,
24157
+ status: nextGit
24158
+ };
24159
+ node.last_git = node.lastGit;
24160
+ node.machineStatus = "online";
24161
+ node.updatedAt = updatedAt;
24162
+ node.lastSeenAt = updatedAt;
24163
+ const repoRoot = readStringValue(nextGit.repoRoot);
24164
+ if (repoRoot && !readStringValue(node.repoRoot)) node.repoRoot = repoRoot;
24165
+ }
24137
24166
  function buildCachedInlineMeshGitStatus(node) {
24138
24167
  const liveGit = buildInlineMeshTransitGitStatus(node);
24139
24168
  if (liveGit) return liveGit;
@@ -24176,7 +24205,6 @@ function stripInlineMeshTransientNodeState(node) {
24176
24205
  session_id: _sessionIdLegacy,
24177
24206
  providerType: _providerType,
24178
24207
  provider_type: _providerTypeLegacy,
24179
- providers: _providers,
24180
24208
  ...rest
24181
24209
  } = node;
24182
24210
  if (cachedStatus && !shouldDiscardCachedInlineMeshStatus(node)) {
@@ -24186,7 +24214,7 @@ function stripInlineMeshTransientNodeState(node) {
24186
24214
  }
24187
24215
  function hasInlineMeshTransientNodeState(node) {
24188
24216
  if (!node || typeof node !== "object" || Array.isArray(node)) return false;
24189
- return "cachedStatus" in node || "lastGit" in node || "last_git" in node || "lastProbe" in node || "last_probe" in node || "error" in node || "health" in node || "machineStatus" in node || "lastSeenAt" in node || "last_seen_at" in node || "updatedAt" in node || "updated_at" in node || "activeSession" in node || "active_session" in node || "activeSessionId" in node || "active_session_id" in node || "sessionId" in node || "session_id" in node || "providerType" in node || "provider_type" in node || "providers" in node;
24217
+ return "cachedStatus" in node || "lastGit" in node || "last_git" in node || "lastProbe" in node || "last_probe" in node || "error" in node || "health" in node || "machineStatus" in node || "lastSeenAt" in node || "last_seen_at" in node || "updatedAt" in node || "updated_at" in node || "activeSession" in node || "active_session" in node || "activeSessionId" in node || "active_session_id" in node || "sessionId" in node || "session_id" in node || "providerType" in node || "provider_type" in node;
24190
24218
  }
24191
24219
  function readInlineMeshNodeId(node) {
24192
24220
  return readStringValue(node?.id, node?.nodeId) || "";
@@ -24312,6 +24340,109 @@ function toIsoTimestamp(value) {
24312
24340
  const stringValue = readStringValue(value);
24313
24341
  return stringValue || null;
24314
24342
  }
24343
+ function synthesizeMeshNodeFreshnessFromConnection(status) {
24344
+ const connection = readObjectRecord(status.connection);
24345
+ const connectionFreshAt = toIsoTimestamp(connection.lastCommandAt ?? connection.lastConnectedAt ?? connection.lastStateChangeAt);
24346
+ const git = readObjectRecord(status.git);
24347
+ const gitCheckedAt = toIsoTimestamp(git.lastCheckedAt);
24348
+ if (!status.lastSeenAt && connectionFreshAt) status.lastSeenAt = connectionFreshAt;
24349
+ if (!status.updatedAt && (gitCheckedAt || connectionFreshAt)) {
24350
+ status.updatedAt = gitCheckedAt ?? connectionFreshAt;
24351
+ }
24352
+ }
24353
+ function finalizeMeshNodeStatus(args) {
24354
+ const { status, node, daemonId, isSelfNode } = args;
24355
+ if (!readStringValue(status.machineStatus)) {
24356
+ const cachedStatus = readObjectRecord(node?.cachedStatus);
24357
+ const machineStatus = readStringValue(cachedStatus.machineStatus, cachedStatus.machine_status, node?.machineStatus);
24358
+ if (machineStatus) status.machineStatus = machineStatus;
24359
+ }
24360
+ synthesizeMeshNodeFreshnessFromConnection(status);
24361
+ const connectionState = readStringValue(readObjectRecord(status.connection).state);
24362
+ status.launchReady = !!daemonId && (readStringValue(status.machineStatus) === "online" || connectionState === "connected" || isSelfNode);
24363
+ }
24364
+ async function probeRemoteMeshGitStatus(args) {
24365
+ if (!args.dispatchMeshCommand) return null;
24366
+ const remoteResult = await Promise.race([
24367
+ args.dispatchMeshCommand(args.daemonId, "git_status", { workspace: args.workspace }),
24368
+ new Promise((_, reject) => setTimeout(() => reject(new Error("timeout")), args.timeoutMs))
24369
+ ]);
24370
+ const remoteGit = remoteResult?.status ?? remoteResult?.git ?? remoteResult;
24371
+ return remoteGit && typeof remoteGit === "object" && typeof remoteGit.isGitRepo === "boolean" ? remoteGit : null;
24372
+ }
24373
+ async function hydrateInlineMeshDirectTruth(args) {
24374
+ const nodes = Array.isArray(args.mesh?.nodes) ? args.mesh.nodes : [];
24375
+ if (!nodes.length) {
24376
+ return {
24377
+ directEvidenceCount: 0,
24378
+ localConfirmedCount: 0,
24379
+ peerAttemptedCount: 0,
24380
+ peerConfirmedCount: 0,
24381
+ unavailableNodeIds: []
24382
+ };
24383
+ }
24384
+ const selectedCoordinatorNodeId = readStringValue(
24385
+ args.mesh?.coordinator?.preferredNodeId,
24386
+ nodes[0]?.id,
24387
+ nodes[0]?.nodeId
24388
+ );
24389
+ let localConfirmedCount = 0;
24390
+ let peerAttemptedCount = 0;
24391
+ let peerConfirmedCount = 0;
24392
+ const unavailableNodeIds = [];
24393
+ for (const [nodeIndex, node] of nodes.entries()) {
24394
+ const nodeId = readStringValue(node?.id, node?.nodeId) || `node_${nodeIndex}`;
24395
+ const workspace = readStringValue(node?.workspace);
24396
+ const daemonId = readStringValue(node?.daemonId);
24397
+ const isSelfNode = Boolean(
24398
+ nodeId && selectedCoordinatorNodeId && nodeId === selectedCoordinatorNodeId
24399
+ ) || Boolean(
24400
+ daemonId && (daemonId === args.localMachineId || daemonId === args.statusInstanceId)
24401
+ ) || Boolean(args.meshSource !== "local_config" && nodeIndex === 0);
24402
+ if (!workspace) {
24403
+ if (!isSelfNode && daemonId) unavailableNodeIds.push(nodeId);
24404
+ continue;
24405
+ }
24406
+ if (isSelfNode && fs10.existsSync(workspace)) {
24407
+ try {
24408
+ const localGit = await getGitRepoStatus(workspace, { timeoutMs: 1e4, refreshUpstream: true });
24409
+ if (localGit?.isGitRepo) {
24410
+ recordInlineMeshDirectGitTruth(node, localGit, "selected_coordinator_local_git");
24411
+ localConfirmedCount += 1;
24412
+ continue;
24413
+ }
24414
+ } catch {
24415
+ }
24416
+ }
24417
+ if (!daemonId || !args.dispatchMeshCommand) {
24418
+ if (!isSelfNode) unavailableNodeIds.push(nodeId);
24419
+ continue;
24420
+ }
24421
+ peerAttemptedCount += 1;
24422
+ try {
24423
+ const remoteGit = await probeRemoteMeshGitStatus({
24424
+ dispatchMeshCommand: args.dispatchMeshCommand,
24425
+ daemonId,
24426
+ workspace,
24427
+ timeoutMs: 8e3
24428
+ });
24429
+ if (remoteGit) {
24430
+ recordInlineMeshDirectGitTruth(node, remoteGit, "selected_coordinator_mesh_p2p_git");
24431
+ peerConfirmedCount += 1;
24432
+ continue;
24433
+ }
24434
+ } catch {
24435
+ }
24436
+ unavailableNodeIds.push(nodeId);
24437
+ }
24438
+ return {
24439
+ directEvidenceCount: localConfirmedCount + peerConfirmedCount,
24440
+ localConfirmedCount,
24441
+ peerAttemptedCount,
24442
+ peerConfirmedCount,
24443
+ unavailableNodeIds
24444
+ };
24445
+ }
24315
24446
  function summarizeMeshSessionRecord(record) {
24316
24447
  return {
24317
24448
  sessionId: readStringValue(record?.sessionId) || "unknown",
@@ -25823,8 +25954,38 @@ var DaemonCommandRouter = class {
25823
25954
  const meshId = typeof args?.meshId === "string" ? args.meshId.trim() : "";
25824
25955
  if (!meshId) return { success: false, error: "meshId required" };
25825
25956
  const meshRecord = await this.getMeshForCommand(meshId, args?.inlineMesh, { preferInline: true });
25826
- if (meshRecord?.mesh) return { success: true, mesh: meshRecord.mesh };
25827
- return { success: false, error: "Mesh not found" };
25957
+ if (!meshRecord?.mesh) return { success: false, error: "Mesh not found" };
25958
+ const requireDirectPeerTruth = args?.requireDirectPeerTruth === true;
25959
+ const directTruth = await hydrateInlineMeshDirectTruth({
25960
+ mesh: meshRecord.mesh,
25961
+ meshSource: meshRecord.source,
25962
+ dispatchMeshCommand: this.deps.dispatchMeshCommand,
25963
+ statusInstanceId: this.deps.statusInstanceId,
25964
+ localMachineId: loadConfig().machineId || ""
25965
+ });
25966
+ const directTruthSatisfied = meshRecord.source !== "inline_bootstrap" || directTruth.directEvidenceCount > 0;
25967
+ const sourceOfTruth = {
25968
+ membership: meshRecord.source === "inline_cache" ? "coordinator_inline_mesh_cache" : meshRecord.source === "local_config" ? "local_mesh_config" : "inline_bootstrap_snapshot",
25969
+ coordinatorOwnsLiveTruth: directTruthSatisfied,
25970
+ directPeerTruth: {
25971
+ required: requireDirectPeerTruth,
25972
+ satisfied: directTruthSatisfied,
25973
+ directEvidenceCount: directTruth.directEvidenceCount,
25974
+ localConfirmedCount: directTruth.localConfirmedCount,
25975
+ peerAttemptedCount: directTruth.peerAttemptedCount,
25976
+ peerConfirmedCount: directTruth.peerConfirmedCount,
25977
+ unavailableNodeIds: directTruth.unavailableNodeIds
25978
+ }
25979
+ };
25980
+ if (requireDirectPeerTruth && !directTruthSatisfied) {
25981
+ return {
25982
+ success: false,
25983
+ code: "mesh_direct_peer_truth_unavailable",
25984
+ error: "Selected coordinator could not confirm direct mesh truth yet. Bootstrap inventory stays unavailable until direct get_mesh probes succeed.",
25985
+ sourceOfTruth
25986
+ };
25987
+ }
25988
+ return { success: true, mesh: meshRecord.mesh, sourceOfTruth };
25828
25989
  }
25829
25990
  case "create_mesh": {
25830
25991
  const name = typeof args?.name === "string" ? args.name.trim() : "";
@@ -26752,25 +26913,51 @@ ${block}`);
26752
26913
  }
26753
26914
  if (workspace) {
26754
26915
  if (!fs10.existsSync(workspace)) {
26916
+ const inlineTransitGit = buildInlineMeshTransitGitStatus(node);
26755
26917
  let remoteProbeApplied = false;
26756
- if (!isSelfNode && daemonId && this.deps.dispatchMeshCommand) {
26918
+ if (inlineTransitGit) {
26919
+ status.git = inlineTransitGit;
26920
+ status.health = inlineTransitGit.isGitRepo ? deriveMeshNodeHealthFromGit(inlineTransitGit) : "degraded";
26921
+ remoteProbeApplied = true;
26922
+ } else if (!isSelfNode && daemonId && this.deps.dispatchMeshCommand) {
26757
26923
  try {
26758
- const remoteResult = await Promise.race([
26759
- this.deps.dispatchMeshCommand(daemonId, "git_status", { workspace }),
26760
- new Promise((_, reject) => setTimeout(() => reject(new Error("timeout")), 8e3))
26761
- ]);
26762
- const remoteGit = remoteResult?.status ?? remoteResult?.git ?? remoteResult;
26763
- if (remoteGit && typeof remoteGit === "object" && typeof remoteGit.isGitRepo === "boolean") {
26924
+ const remoteGit = await probeRemoteMeshGitStatus({
26925
+ dispatchMeshCommand: this.deps.dispatchMeshCommand,
26926
+ daemonId,
26927
+ workspace,
26928
+ timeoutMs: 8e3
26929
+ });
26930
+ if (remoteGit) {
26764
26931
  status.git = remoteGit;
26765
26932
  status.health = remoteGit.isGitRepo ? deriveMeshNodeHealthFromGit(remoteGit) : "degraded";
26933
+ recordInlineMeshDirectGitTruth(node, remoteGit, "selected_coordinator_mesh_p2p_git");
26766
26934
  remoteProbeApplied = true;
26767
26935
  }
26768
26936
  } catch {
26937
+ const refreshedConnection = this.deps.getMeshPeerConnectionStatus?.(daemonId);
26938
+ const refreshedConnectionState = readStringValue(refreshedConnection?.state);
26939
+ if (refreshedConnection && refreshedConnectionState === "connected") {
26940
+ status.connection = refreshedConnection;
26941
+ try {
26942
+ const remoteGit = await probeRemoteMeshGitStatus({
26943
+ dispatchMeshCommand: this.deps.dispatchMeshCommand,
26944
+ daemonId,
26945
+ workspace,
26946
+ timeoutMs: 12e3
26947
+ });
26948
+ if (remoteGit) {
26949
+ status.git = remoteGit;
26950
+ status.health = remoteGit.isGitRepo ? deriveMeshNodeHealthFromGit(remoteGit) : "degraded";
26951
+ recordInlineMeshDirectGitTruth(node, remoteGit, "selected_coordinator_mesh_p2p_git");
26952
+ remoteProbeApplied = true;
26953
+ }
26954
+ } catch {
26955
+ }
26956
+ }
26769
26957
  }
26770
26958
  }
26771
26959
  if (!remoteProbeApplied) {
26772
26960
  const connectionState = readStringValue(status.connection?.state);
26773
- const inlineTransitGit = buildInlineMeshTransitGitStatus(node);
26774
26961
  const pendingPeerGitProbe = !inlineTransitGit && !isSelfNode && !!daemonId && (readStringValue(status.machineStatus) === "online" || readStringValue(status.health) === "online" || connectionState === "connecting" || connectionState === "connected" || connectionState === "unknown");
26775
26962
  if (pendingPeerGitProbe) {
26776
26963
  status.gitProbePending = true;
@@ -26781,12 +26968,12 @@ ${block}`);
26781
26968
  node,
26782
26969
  pendingPeerGitProbe ? { skipGit: true, skipError: true, skipHealth: true } : void 0
26783
26970
  )) {
26784
- status.launchReady = !!daemonId && (readStringValue(status.machineStatus) === "online" || isSelfNode);
26971
+ finalizeMeshNodeStatus({ status, node, daemonId, isSelfNode });
26785
26972
  nodeStatuses.push(status);
26786
26973
  continue;
26787
26974
  }
26788
26975
  if (meshRecord?.source === "inline_cache" && !isSelfNode) {
26789
- status.launchReady = !!daemonId && (readStringValue(status.machineStatus) === "online" || isSelfNode);
26976
+ finalizeMeshNodeStatus({ status, node, daemonId, isSelfNode });
26790
26977
  nodeStatuses.push(status);
26791
26978
  continue;
26792
26979
  }
@@ -26795,6 +26982,7 @@ ${block}`);
26795
26982
  try {
26796
26983
  const gitStatus = await getGitRepoStatus(workspace, { timeoutMs: 1e4, refreshUpstream: true });
26797
26984
  status.git = gitStatus;
26985
+ recordInlineMeshDirectGitTruth(node, gitStatus, "selected_coordinator_local_git");
26798
26986
  if (gitStatus.isGitRepo) {
26799
26987
  status.health = deriveMeshNodeHealthFromGit(gitStatus);
26800
26988
  } else {
@@ -26810,7 +26998,7 @@ ${block}`);
26810
26998
  } else {
26811
26999
  applyCachedInlineMeshNodeStatus(status, node);
26812
27000
  }
26813
- status.launchReady = !!daemonId && (readStringValue(status.machineStatus) === "online" || isSelfNode);
27001
+ finalizeMeshNodeStatus({ status, node, daemonId, isSelfNode });
26814
27002
  nodeStatuses.push(status);
26815
27003
  }
26816
27004
  return {