@adhdev/daemon-core 0.9.82-rc.9 → 0.9.82-rc.90

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.
Files changed (67) hide show
  1. package/dist/cli-adapters/provider-cli-adapter.d.ts +2 -0
  2. package/dist/cli-adapters/provider-cli-parse.d.ts +1 -0
  3. package/dist/cli-adapters/provider-cli-shared.d.ts +2 -0
  4. package/dist/commands/router.d.ts +22 -0
  5. package/dist/config/mesh-config.d.ts +66 -1
  6. package/dist/index.d.ts +13 -6
  7. package/dist/index.js +5395 -1197
  8. package/dist/index.js.map +1 -1
  9. package/dist/index.mjs +5359 -1183
  10. package/dist/index.mjs.map +1 -1
  11. package/dist/installer.d.ts +1 -4
  12. package/dist/launch.d.ts +1 -1
  13. package/dist/logging/async-batch-writer.d.ts +10 -0
  14. package/dist/mesh/beads-db.d.ts +18 -0
  15. package/dist/mesh/mesh-active-work.d.ts +60 -0
  16. package/dist/mesh/mesh-events.d.ts +29 -5
  17. package/dist/mesh/mesh-fast-forward.d.ts +39 -0
  18. package/dist/mesh/mesh-host-ownership.d.ts +9 -0
  19. package/dist/mesh/mesh-ledger.d.ts +38 -1
  20. package/dist/mesh/mesh-work-queue.d.ts +27 -5
  21. package/dist/mesh/refine-config.d.ts +176 -0
  22. package/dist/providers/chat-message-normalization.d.ts +1 -0
  23. package/dist/providers/cli-provider-instance.d.ts +2 -1
  24. package/dist/repo-mesh-types.d.ts +46 -0
  25. package/dist/status/reporter.d.ts +2 -0
  26. package/package.json +3 -1
  27. package/src/boot/daemon-lifecycle.ts +1 -0
  28. package/src/cli-adapters/provider-cli-adapter.ts +91 -3
  29. package/src/cli-adapters/provider-cli-parse.d.ts +1 -0
  30. package/src/cli-adapters/provider-cli-parse.ts +4 -0
  31. package/src/cli-adapters/provider-cli-runtime.ts +3 -1
  32. package/src/cli-adapters/provider-cli-shared.d.ts +2 -0
  33. package/src/cli-adapters/provider-cli-shared.ts +20 -10
  34. package/src/commands/chat-commands.ts +454 -15
  35. package/src/commands/cli-manager.ts +126 -0
  36. package/src/commands/handler.ts +8 -1
  37. package/src/commands/mesh-coordinator.ts +13 -143
  38. package/src/commands/router.ts +2687 -435
  39. package/src/config/chat-history.ts +9 -7
  40. package/src/config/mesh-config.ts +245 -1
  41. package/src/daemon/dev-cli-debug.ts +10 -1
  42. package/src/detection/ide-detector.ts +26 -16
  43. package/src/index.ts +31 -5
  44. package/src/installer.d.ts +1 -1
  45. package/src/installer.ts +8 -6
  46. package/src/launch.d.ts +1 -1
  47. package/src/launch.ts +37 -28
  48. package/src/logging/async-batch-writer.ts +55 -0
  49. package/src/logging/logger.ts +2 -1
  50. package/src/mesh/beads-db.ts +176 -0
  51. package/src/mesh/coordinator-prompt.ts +30 -7
  52. package/src/mesh/mesh-active-work.ts +243 -0
  53. package/src/mesh/mesh-events.ts +400 -47
  54. package/src/mesh/mesh-fast-forward.ts +430 -0
  55. package/src/mesh/mesh-host-ownership.ts +73 -0
  56. package/src/mesh/mesh-ledger.ts +138 -1
  57. package/src/mesh/mesh-work-queue.ts +199 -137
  58. package/src/mesh/refine-config.ts +356 -0
  59. package/src/providers/chat-message-normalization.ts +3 -1
  60. package/src/providers/cli-provider-instance.ts +91 -13
  61. package/src/providers/ide-provider-instance.ts +17 -3
  62. package/src/providers/provider-loader.ts +10 -4
  63. package/src/providers/read-chat-contract.ts +1 -1
  64. package/src/providers/version-archive.ts +38 -20
  65. package/src/repo-mesh-types.ts +51 -0
  66. package/src/status/reporter.ts +15 -0
  67. package/src/system/host-memory.ts +29 -12
@@ -38,13 +38,25 @@ import { createInteractionId, getRecentDebugTrace, recordDebugTrace } from '../l
38
38
  import { getSessionHostSurfaceKind, partitionSessionHostRecords } from '../session-host/runtime-surface.js';
39
39
  import { createHermesManualMeshCoordinatorSetup, resolveMeshCoordinatorSetup } from './mesh-coordinator.js';
40
40
  import { buildSessionEntries } from '../status/builders.js';
41
- import { handleMeshForwardEvent, drainPendingMeshCoordinatorEvents } from '../mesh/mesh-events.js';
41
+ import { handleMeshForwardEvent, drainPendingMeshCoordinatorEvents, getPendingMeshCoordinatorEvents, queuePendingMeshCoordinatorEvent } from '../mesh/mesh-events.js';
42
+ import { buildMeshHostRequiredFailure, normalizeMeshDaemonRole, resolveMeshHostStatus } from '../mesh/mesh-host-ownership.js';
43
+ import { fastForwardMeshNode } from '../mesh/mesh-fast-forward.js';
44
+ import {
45
+ MESH_REFINE_CONFIG_LOCATIONS,
46
+ MESH_REFINE_CONFIG_SCHEMA,
47
+ loadMeshRefineConfig,
48
+ resolveMeshRefineValidationPlan,
49
+ suggestMeshRefineConfig,
50
+ validateMeshRefineConfig,
51
+ type MeshRefineValidationCommandPlan,
52
+ } from '../mesh/refine-config.js';
42
53
  import { buildMachineInfo, buildStatusSnapshot } from '../status/snapshot.js';
43
54
  import { getSessionCompletionMarker } from '../status/snapshot.js';
44
55
  import { execNpmCommandSync, resolveCurrentGlobalInstallSurface, spawnDetachedDaemonUpgradeHelper } from './upgrade-helper.js';
56
+ import { getMeshQueueRevision } from '../mesh/mesh-work-queue.js';
45
57
  import type { RepoMeshSessionCleanupMode } from '../repo-mesh-types.js';
46
58
  import { homedir } from 'os';
47
- import { join as pathJoin, resolve as pathResolve } from 'path';
59
+ import { basename as pathBasename, join as pathJoin, resolve as pathResolve } from 'path';
48
60
  import * as fs from 'fs';
49
61
 
50
62
  type ReleaseChannel = 'stable' | 'preview';
@@ -114,14 +126,95 @@ function readBooleanValue(...values: unknown[]): boolean | undefined {
114
126
  return undefined;
115
127
  }
116
128
 
117
- function readGitSubmodules(value: unknown): GitSubmoduleStatus[] | undefined {
129
+ function summarizeRepoMeshDebugGit(git: unknown): Record<string, unknown> | null {
130
+ const record = readObjectRecord(git);
131
+ if (!Object.keys(record).length) return null;
132
+ const submodules = Array.isArray(record.submodules)
133
+ ? record.submodules.map((entry: any) => ({
134
+ path: readStringValue(entry?.path) ?? null,
135
+ commit: readStringValue(entry?.commit)?.slice(0, 12) ?? null,
136
+ dirty: readBooleanValue(entry?.dirty) ?? false,
137
+ outOfSync: readBooleanValue(entry?.outOfSync, entry?.out_of_sync) ?? false,
138
+ }))
139
+ : [];
140
+ return {
141
+ isGitRepo: readBooleanValue(record.isGitRepo),
142
+ workspace: readStringValue(record.workspace) ?? null,
143
+ repoRoot: readStringValue(record.repoRoot, record.repo_root) ?? null,
144
+ branch: readStringValue(record.branch) ?? null,
145
+ upstream: readStringValue(record.upstream) ?? null,
146
+ upstreamStatus: readStringValue(record.upstreamStatus, record.upstream_status) ?? null,
147
+ headCommit: readStringValue(record.headCommit, record.head_commit)?.slice(0, 12) ?? null,
148
+ ahead: readNumberValue(record.ahead) ?? null,
149
+ behind: readNumberValue(record.behind) ?? null,
150
+ dirtyCounts: {
151
+ staged: readNumberValue(record.staged) ?? 0,
152
+ modified: readNumberValue(record.modified) ?? 0,
153
+ untracked: readNumberValue(record.untracked) ?? 0,
154
+ deleted: readNumberValue(record.deleted) ?? 0,
155
+ renamed: readNumberValue(record.renamed) ?? 0,
156
+ },
157
+ lastCheckedAt: readNumberValue(record.lastCheckedAt, record.last_checked_at) ?? null,
158
+ submoduleCount: submodules.length,
159
+ submodules,
160
+ };
161
+ }
162
+
163
+ function summarizeRepoMeshStatusDebug(status: any): Record<string, unknown> {
164
+ const nodes = Array.isArray(status?.nodes) ? status.nodes : [];
165
+ return {
166
+ success: status?.success,
167
+ meshId: readStringValue(status?.meshId, status?.mesh_id) ?? null,
168
+ refreshedAt: readStringValue(status?.refreshedAt, status?.refreshed_at) ?? null,
169
+ sourceOfTruth: status?.sourceOfTruth ?? null,
170
+ branchConvergenceSummary: status?.branchConvergenceSummary ?? status?.branch_convergence_summary ?? null,
171
+ nodeCount: nodes.length,
172
+ nodes: nodes.map((node: any) => ({
173
+ nodeId: readStringValue(node?.nodeId, node?.id) ?? null,
174
+ daemonId: readStringValue(node?.daemonId, node?.daemon_id) ?? null,
175
+ workspace: readStringValue(node?.workspace, node?.git?.workspace) ?? null,
176
+ health: readStringValue(node?.health) ?? null,
177
+ machineStatus: readStringValue(node?.machineStatus, node?.machine_status) ?? null,
178
+ connection: node?.connection && typeof node.connection === 'object' ? {
179
+ state: readStringValue(node.connection.state) ?? null,
180
+ transport: readStringValue(node.connection.transport) ?? null,
181
+ source: readStringValue(node.connection.source) ?? null,
182
+ reported: readBooleanValue(node.connection.reported) ?? null,
183
+ } : null,
184
+ gitProbePending: node?.gitProbePending === true,
185
+ launchReady: node?.launchReady === true,
186
+ git: summarizeRepoMeshDebugGit(node?.git),
187
+ branchConvergence: node?.branchConvergence ?? node?.branch_convergence ?? null,
188
+ })),
189
+ };
190
+ }
191
+
192
+ function logRepoMeshStatusDebug(event: string, fields: Record<string, unknown>): void {
193
+ try {
194
+ LOG.info('MeshStatusDebug', `[RepoMeshStatusDebug] ${JSON.stringify({ event, ...fields })}`);
195
+ } catch {
196
+ LOG.info('MeshStatusDebug', `[RepoMeshStatusDebug] ${event}`);
197
+ }
198
+ }
199
+
200
+ function joinRepoPath(root: string | undefined, relativePath: string | undefined): string | undefined {
201
+ const normalizedRoot = typeof root === 'string' ? root.trim().replace(/[\\/]+$/, '') : '';
202
+ const normalizedPath = typeof relativePath === 'string' ? relativePath.trim() : '';
203
+ if (!normalizedPath) return undefined;
204
+ if (/^(?:[A-Za-z]:[\\/]|\/)/.test(normalizedPath)) return normalizedPath;
205
+ if (!normalizedRoot) return undefined;
206
+ return `${normalizedRoot}/${normalizedPath.replace(/^[\\/]+/, '')}`;
207
+ }
208
+
209
+ function readGitSubmodules(value: unknown, parentRepoRoot?: string): GitSubmoduleStatus[] | undefined {
118
210
  if (!Array.isArray(value)) return undefined;
119
211
  const submodules = value
120
212
  .map(entry => {
121
213
  const submodule = readObjectRecord(entry);
122
214
  const path = readStringValue(submodule.path);
123
215
  const commit = readStringValue(submodule.commit);
124
- const repoPath = readStringValue(submodule.repoPath, submodule.repo_root);
216
+ const repoPath = readStringValue(submodule.repoPath, submodule.repo_root)
217
+ ?? joinRepoPath(parentRepoRoot, path);
125
218
  if (!path || !commit || !repoPath) return null;
126
219
  return {
127
220
  path,
@@ -137,60 +230,23 @@ function readGitSubmodules(value: unknown): GitSubmoduleStatus[] | undefined {
137
230
  return submodules.length > 0 ? submodules : undefined;
138
231
  }
139
232
 
140
- function buildCachedInlineMeshGitStatus(node: any): Record<string, unknown> | undefined {
141
- const cachedStatus = readObjectRecord(node?.cachedStatus);
142
- const cachedGit = readObjectRecord(cachedStatus.git);
143
- if (Object.keys(cachedGit).length) {
144
- const conflictFiles = Array.isArray(cachedGit.conflictFiles)
145
- ? cachedGit.conflictFiles.filter((value: unknown): value is string => typeof value === 'string')
146
- : [];
147
- const conflictCount = readNumberValue(cachedGit.conflicts) ?? conflictFiles.length;
148
- const hasConflicts = readBooleanValue(cachedGit.hasConflicts) ?? conflictCount > 0;
149
- const isGitRepo = readBooleanValue(cachedGit.isGitRepo);
150
- if (isGitRepo !== undefined) {
151
- const submodules = readGitSubmodules(cachedGit.submodules);
152
- return {
153
- workspace: readStringValue(cachedGit.workspace, node?.workspace) || '',
154
- repoRoot: readStringValue(cachedGit.repoRoot, node?.repoRoot, node?.workspace) || null,
155
- isGitRepo,
156
- branch: readStringValue(cachedGit.branch) ?? null,
157
- headCommit: readStringValue(cachedGit.headCommit) ?? null,
158
- headMessage: readStringValue(cachedGit.headMessage) ?? null,
159
- upstream: readStringValue(cachedGit.upstream) ?? null,
160
- ahead: readNumberValue(cachedGit.ahead) ?? 0,
161
- behind: readNumberValue(cachedGit.behind) ?? 0,
162
- staged: readNumberValue(cachedGit.staged) ?? 0,
163
- modified: readNumberValue(cachedGit.modified) ?? 0,
164
- untracked: readNumberValue(cachedGit.untracked) ?? 0,
165
- deleted: readNumberValue(cachedGit.deleted) ?? 0,
166
- renamed: readNumberValue(cachedGit.renamed) ?? 0,
167
- hasConflicts,
168
- conflictFiles,
169
- stashCount: readNumberValue(cachedGit.stashCount) ?? 0,
170
- lastCheckedAt: readNumberValue(cachedGit.lastCheckedAt) ?? Date.now(),
171
- ...(submodules ? { submodules } : {}),
172
- };
173
- }
174
- }
233
+ function buildMeshNodeDisplayLabel(node: Record<string, unknown>, nodeId: string, providerPriority: string[]): string {
234
+ const explicit = readStringValue(node.machineLabel, node.machine_label, node.machineNickname, node.machine_nickname, node.alias);
235
+ if (explicit) return explicit;
236
+ const workspace = readStringValue(node.workspace, node.repoRoot, node.repo_root);
237
+ const workspaceName = workspace ? pathBasename(workspace) : undefined;
238
+ const host = readStringValue(node.hostname, node.host, node.daemonId, node.daemon_id, node.machineId, node.machine_id);
239
+ const provider = providerPriority[0] || (Array.isArray(node.providers) ? readStringValue(...node.providers) : undefined);
240
+ const parts = [workspaceName, host, provider].filter(Boolean);
241
+ if (parts.length > 0) return parts.join(' · ');
242
+ return nodeId || 'unidentified mesh node';
243
+ }
175
244
 
176
- const rawGit = readObjectRecord(node?.lastGit ?? node?.last_git);
177
- const gitResult = readObjectRecord(rawGit.result);
178
- const directStatus = readObjectRecord(rawGit.status);
179
- const nestedStatus = readObjectRecord(gitResult.status);
180
- const rawProbe = readObjectRecord(node?.lastProbe ?? node?.last_probe);
181
- const probeGit = readObjectRecord(rawProbe.git);
182
- const probeGitResult = readObjectRecord(probeGit.result);
183
- const probeDirectStatus = readObjectRecord(probeGit.status);
184
- const probeNestedStatus = readObjectRecord(probeGitResult.status);
185
- const status = Object.keys(directStatus).length
186
- ? directStatus
187
- : Object.keys(nestedStatus).length
188
- ? nestedStatus
189
- : Object.keys(probeDirectStatus).length
190
- ? probeDirectStatus
191
- : Object.keys(probeNestedStatus).length
192
- ? probeNestedStatus
193
- : {};
245
+ function normalizeInlineMeshGitStatus(
246
+ status: Record<string, unknown>,
247
+ node: any,
248
+ options?: { lastCheckedAt?: number },
249
+ ): Record<string, unknown> | undefined {
194
250
  const isGitRepo = readBooleanValue(status.isGitRepo);
195
251
  if (!Object.keys(status).length || isGitRepo === undefined) return undefined;
196
252
  const conflictFiles = Array.isArray(status.conflictFiles)
@@ -198,15 +254,20 @@ function buildCachedInlineMeshGitStatus(node: any): Record<string, unknown> | un
198
254
  : [];
199
255
  const conflictCount = readNumberValue(status.conflicts) ?? conflictFiles.length;
200
256
  const hasConflicts = readBooleanValue(status.hasConflicts) ?? conflictCount > 0;
201
- const submodules = readGitSubmodules(status.submodules);
257
+ const repoRoot = readStringValue(status.repoRoot, status.repo_root, node?.repoRoot, node?.repo_root, status.workspace, node?.workspace) || undefined;
258
+ const submodules = readGitSubmodules(status.submodules, repoRoot);
202
259
  return {
203
260
  workspace: readStringValue(status.workspace, node?.workspace) || '',
204
- repoRoot: readStringValue(status.repoRoot, node?.repoRoot, node?.workspace) || null,
261
+ repoRoot: repoRoot ?? null,
205
262
  isGitRepo,
206
263
  branch: readStringValue(status.branch) ?? null,
207
264
  headCommit: readStringValue(status.headCommit) ?? null,
208
265
  headMessage: readStringValue(status.headMessage) ?? null,
209
266
  upstream: readStringValue(status.upstream) ?? null,
267
+ upstreamStatus: readStringValue(status.upstreamStatus, status.upstream_status)
268
+ ?? (readStringValue(status.upstream) ? 'unchecked' : 'no_upstream'),
269
+ upstreamFetchedAt: readNumberValue(status.upstreamFetchedAt, status.upstream_fetched_at),
270
+ upstreamFetchError: readStringValue(status.upstreamFetchError, status.upstream_fetch_error),
210
271
  ahead: readNumberValue(status.ahead) ?? 0,
211
272
  behind: readNumberValue(status.behind) ?? 0,
212
273
  staged: readNumberValue(status.staged) ?? 0,
@@ -217,14 +278,247 @@ function buildCachedInlineMeshGitStatus(node: any): Record<string, unknown> | un
217
278
  hasConflicts,
218
279
  conflictFiles,
219
280
  stashCount: readNumberValue(status.stashCount) ?? 0,
220
- lastCheckedAt: Date.now(),
281
+ lastCheckedAt: options?.lastCheckedAt ?? readNumberValue(status.lastCheckedAt) ?? Date.now(),
221
282
  ...(submodules ? { submodules } : {}),
222
283
  };
223
284
  }
224
285
 
286
+ function scoreInlineMeshGitStatus(git: Record<string, unknown> | undefined): number {
287
+ if (!git) return Number.NEGATIVE_INFINITY;
288
+ let score = 0;
289
+ if (readBooleanValue(git.isGitRepo) === true) score += 50;
290
+ if (readBooleanValue(git.isGitRepo) === false) score -= 10;
291
+ if (readStringValue(git.branch)) score += 20;
292
+ if (readStringValue(git.headCommit)) score += 20;
293
+ if (readStringValue(git.upstream)) score += 10;
294
+ if (readStringValue(git.upstreamStatus)) score += 5;
295
+ if (readNumberValue(git.ahead) !== undefined) score += 2;
296
+ if (readNumberValue(git.behind) !== undefined) score += 2;
297
+ if (Array.isArray(git.submodules) && git.submodules.length > 0) score += 4 + git.submodules.length;
298
+ if (readStringValue(git.error)) score -= 20;
299
+ return score;
300
+ }
301
+
302
+ function buildInlineMeshTransitGitStatus(node: any): Record<string, unknown> | undefined {
303
+ const rawGit = readObjectRecord(node?.lastGit ?? node?.last_git);
304
+ const gitResult = readObjectRecord(rawGit.result);
305
+ const directStatus = readObjectRecord(rawGit.status);
306
+ const nestedStatus = readObjectRecord(gitResult.status);
307
+ const rawProbe = readObjectRecord(node?.lastProbe ?? node?.last_probe);
308
+ const probeGit = readObjectRecord(rawProbe.git);
309
+ const probeGitResult = readObjectRecord(probeGit.result);
310
+ const probeDirectStatus = readObjectRecord(probeGit.status);
311
+ const probeNestedStatus = readObjectRecord(probeGitResult.status);
312
+ const candidates = [directStatus, nestedStatus, probeDirectStatus, probeNestedStatus];
313
+ let best: { git: Record<string, unknown>; score: number } | null = null;
314
+ for (const status of candidates) {
315
+ const normalized = normalizeInlineMeshGitStatus(status, node, { lastCheckedAt: Date.now() });
316
+ if (!normalized) continue;
317
+ const score = scoreInlineMeshGitStatus(normalized);
318
+ if (!best || score > best.score) best = { git: normalized, score };
319
+ }
320
+ return best?.git;
321
+ }
322
+
323
+ function shouldRefreshStalePendingAggregate(snapshot: any, options?: { requireDirectPeerTruth?: boolean }): boolean {
324
+ if (options?.requireDirectPeerTruth !== true || !Array.isArray(snapshot?.nodes)) return false;
325
+ return snapshot.nodes.some((node: any) => {
326
+ if (node?.gitProbePending !== true) return false;
327
+ const git = readObjectRecord(node?.git);
328
+ return !readBooleanValue(git.isGitRepo) && !readStringValue(git.branch, git.headCommit, git.upstream);
329
+ });
330
+ }
331
+
332
+ function buildLivePeerGitConnection(connection: Record<string, unknown>, timestamp = new Date().toISOString()): Record<string, unknown> {
333
+ const source = readStringValue(connection.source);
334
+ const transport = readStringValue(connection.transport);
335
+ return {
336
+ ...connection,
337
+ perspective: readStringValue(connection.perspective) ?? 'selected_coordinator',
338
+ source: source && source !== 'not_reported' ? source : 'mesh_peer_status',
339
+ state: 'connected',
340
+ transport: transport && transport !== 'unknown' ? transport : 'direct',
341
+ reported: true,
342
+ reason: 'Live peer git snapshot reported by the selected coordinator.',
343
+ lastStateChangeAt: readStringValue(connection.lastStateChangeAt) ?? timestamp,
344
+ };
345
+ }
346
+
347
+ function recordInlineMeshDirectGitTruth(
348
+ node: any,
349
+ git: Record<string, unknown>,
350
+ source: 'selected_coordinator_local_git' | 'selected_coordinator_mesh_p2p_git',
351
+ ): void {
352
+ if (!node || typeof node !== 'object' || Array.isArray(node)) return;
353
+ const checkedAt = readNumberValue(git.lastCheckedAt) ?? Date.now();
354
+ const updatedAt = new Date(checkedAt).toISOString();
355
+ const nextGit: Record<string, unknown> = {
356
+ ...git,
357
+ lastCheckedAt: checkedAt,
358
+ };
359
+ node.lastGit = {
360
+ source,
361
+ checkedAt,
362
+ status: nextGit,
363
+ };
364
+ node.last_git = node.lastGit;
365
+ node.machineStatus = 'online';
366
+ node.updatedAt = updatedAt;
367
+ node.lastSeenAt = updatedAt;
368
+ const repoRoot = readStringValue(nextGit.repoRoot);
369
+ if (repoRoot && !readStringValue(node.repoRoot)) node.repoRoot = repoRoot;
370
+ }
371
+
372
+ function buildCachedInlineMeshGitStatus(node: any): Record<string, unknown> | undefined {
373
+ const liveGit = buildInlineMeshTransitGitStatus(node);
374
+ if (liveGit) return liveGit;
375
+
376
+ const cachedStatus = readObjectRecord(node?.cachedStatus);
377
+ const cachedGit = readObjectRecord(cachedStatus.git);
378
+ if (!Object.keys(cachedGit).length) return undefined;
379
+ return normalizeInlineMeshGitStatus(cachedGit, node);
380
+ }
381
+
382
+ function shouldDiscardCachedInlineMeshStatus(node: any): boolean {
383
+ const cachedStatus = readObjectRecord(node?.cachedStatus);
384
+ if (!Object.keys(cachedStatus).length) return false;
385
+ const cachedGit = readObjectRecord(cachedStatus.git);
386
+ const workspaceError = readStringValue(cachedStatus.error, node?.error);
387
+ if (workspaceError && /workspace must be an existing directory/i.test(workspaceError)) return true;
388
+ const isGitRepo = readBooleanValue(cachedGit.isGitRepo);
389
+ const branch = readStringValue(cachedGit.branch);
390
+ const headCommit = readStringValue(cachedGit.headCommit);
391
+ return isGitRepo === false && !branch && !headCommit;
392
+ }
393
+
394
+ function stripInlineMeshTransientNodeState(node: any): any {
395
+ if (!node || typeof node !== 'object' || Array.isArray(node)) return node;
396
+ const {
397
+ cachedStatus,
398
+ lastGit: _lastGit,
399
+ last_git: _lastGitLegacy,
400
+ lastProbe: _lastProbe,
401
+ last_probe: _lastProbeLegacy,
402
+ error: _error,
403
+ health: _health,
404
+ machineStatus: _machineStatus,
405
+ lastSeenAt: _lastSeenAt,
406
+ last_seen_at: _lastSeenAtLegacy,
407
+ updatedAt: _updatedAt,
408
+ updated_at: _updatedAtLegacy,
409
+ activeSession: _activeSession,
410
+ active_session: _activeSessionLegacy,
411
+ activeSessionId: _activeSessionId,
412
+ active_session_id: _activeSessionIdLegacy,
413
+ sessionId: _sessionId,
414
+ session_id: _sessionIdLegacy,
415
+ providerType: _providerType,
416
+ provider_type: _providerTypeLegacy,
417
+ ...rest
418
+ } = node as Record<string, unknown>;
419
+ if (cachedStatus && !shouldDiscardCachedInlineMeshStatus(node)) {
420
+ return { ...rest, cachedStatus };
421
+ }
422
+ return rest;
423
+ }
424
+
425
+ function hasInlineMeshTransientNodeState(node: any): boolean {
426
+ if (!node || typeof node !== 'object' || Array.isArray(node)) return false;
427
+ return 'cachedStatus' in node
428
+ || 'lastGit' in node
429
+ || 'last_git' in node
430
+ || 'lastProbe' in node
431
+ || 'last_probe' in node
432
+ || 'error' in node
433
+ || 'health' in node
434
+ || 'machineStatus' in node
435
+ || 'lastSeenAt' in node
436
+ || 'last_seen_at' in node
437
+ || 'updatedAt' in node
438
+ || 'updated_at' in node
439
+ || 'activeSession' in node
440
+ || 'active_session' in node
441
+ || 'activeSessionId' in node
442
+ || 'active_session_id' in node
443
+ || 'sessionId' in node
444
+ || 'session_id' in node
445
+ || 'providerType' in node
446
+ || 'provider_type' in node;
447
+ }
448
+
449
+ function inlineMeshCarriesTransientNodeTruth(inlineMesh: any): boolean {
450
+ if (!inlineMesh || typeof inlineMesh !== 'object' || Array.isArray(inlineMesh)) return false;
451
+ if (!Array.isArray(inlineMesh.nodes) || inlineMesh.nodes.length === 0) return false;
452
+ return inlineMesh.nodes.some((node: any) => hasInlineMeshTransientNodeState(node));
453
+ }
454
+
455
+ function readInlineMeshNodeId(node: any): string {
456
+ return readStringValue(node?.id, node?.nodeId) || '';
457
+ }
458
+
459
+ function sanitizeInlineMesh(inlineMesh: any): any {
460
+ if (!inlineMesh || typeof inlineMesh !== 'object' || Array.isArray(inlineMesh)) return inlineMesh;
461
+ if (!Array.isArray(inlineMesh.nodes)) return inlineMesh;
462
+ let changed = false;
463
+ const nodes = inlineMesh.nodes.map((node: any) => {
464
+ if (!hasInlineMeshTransientNodeState(node)) return node;
465
+ changed = true;
466
+ return stripInlineMeshTransientNodeState(node);
467
+ });
468
+ if (!changed) return inlineMesh;
469
+ return {
470
+ ...inlineMesh,
471
+ nodes,
472
+ };
473
+ }
474
+
475
+ function reconcileInlineMeshCache(cached: any, incoming: any): any {
476
+ if (!cached || typeof cached !== 'object' || Array.isArray(cached)) return incoming;
477
+ if (!incoming || typeof incoming !== 'object' || Array.isArray(incoming)) return cached;
478
+ const cachedNodes = Array.isArray(cached.nodes) ? cached.nodes : [];
479
+ const incomingNodes = Array.isArray(incoming.nodes) ? incoming.nodes : [];
480
+ if (!cachedNodes.length || !incomingNodes.length) return { ...cached, ...incoming };
481
+
482
+ const cachedUpdatedAt = Date.parse(readStringValue(cached.updatedAt, cached.updated_at) || '');
483
+ const incomingUpdatedAt = Date.parse(readStringValue(incoming.updatedAt, incoming.updated_at) || '');
484
+ const preserveCachedMembership = Number.isFinite(cachedUpdatedAt)
485
+ && (!Number.isFinite(incomingUpdatedAt) || cachedUpdatedAt > incomingUpdatedAt);
486
+
487
+ const cachedById = new Map<string, any>();
488
+ for (const node of cachedNodes) {
489
+ const nodeId = readInlineMeshNodeId(node);
490
+ if (nodeId) cachedById.set(nodeId, node);
491
+ }
492
+
493
+ const nodes = incomingNodes.map((incomingNode: any) => {
494
+ const nodeId = readInlineMeshNodeId(incomingNode);
495
+ const cachedNode = nodeId ? cachedById.get(nodeId) : undefined;
496
+ if (!cachedNode && preserveCachedMembership) return null;
497
+ if (!cachedNode) return incomingNode;
498
+ if (hasInlineMeshTransientNodeState(incomingNode)) {
499
+ return { ...cachedNode, ...incomingNode };
500
+ }
501
+ return { ...stripInlineMeshTransientNodeState(cachedNode), ...incomingNode };
502
+ }).filter(Boolean);
503
+
504
+ return {
505
+ ...cached,
506
+ ...incoming,
507
+ nodes,
508
+ };
509
+ }
510
+
225
511
  function hasGitWorktreeChanges(git: Record<string, unknown> | null | undefined): boolean {
226
- if (!git) return false;
227
- return Number(git.staged || 0) + Number(git.modified || 0) + Number(git.untracked || 0) + Number(git.deleted || 0) + Number(git.renamed || 0) > 0;
512
+ return countGitWorktreeChanges(git) > 0;
513
+ }
514
+
515
+ function countGitWorktreeChanges(git: Record<string, unknown> | null | undefined): number {
516
+ if (!git) return 0;
517
+ return Number(git.staged || 0)
518
+ + Number(git.modified || 0)
519
+ + Number(git.untracked || 0)
520
+ + Number(git.deleted || 0)
521
+ + Number(git.renamed || 0);
228
522
  }
229
523
 
230
524
  function getGitSubmoduleDriftState(git: Record<string, unknown> | null | undefined): { dirty: boolean; outOfSync: boolean } {
@@ -249,6 +543,167 @@ function deriveMeshNodeHealthFromGit(git: Record<string, unknown> | null | undef
249
543
  return 'online';
250
544
  }
251
545
 
546
+ function readMeshNodeLabel(status: Record<string, unknown>, node: any): string {
547
+ return readStringValue(status.nodeId, node?.id, node?.nodeId) ?? 'unknown';
548
+ }
549
+
550
+ function buildInlineMeshBranchConvergence(args: {
551
+ mesh: any;
552
+ node: any;
553
+ status: Record<string, unknown>;
554
+ }): Record<string, unknown> {
555
+ const git = readObjectRecord(args.status.git);
556
+ const nodeLabel = readMeshNodeLabel(args.status, args.node);
557
+ const defaultBranch = readStringValue(args.mesh?.defaultBranch) ?? 'main';
558
+ const branch = readStringValue(git.branch, args.node?.worktreeBranch) ?? null;
559
+ const upstream = readStringValue(git.upstream) ?? null;
560
+ const upstreamStatus = readStringValue(git.upstreamStatus, git.upstream_status)
561
+ ?? (upstream ? 'unchecked' : 'no_upstream');
562
+ const ahead = readNumberValue(git.ahead) ?? 0;
563
+ const behind = readNumberValue(git.behind) ?? 0;
564
+ const uncommittedChanges = countGitWorktreeChanges(git);
565
+ const hasConflicts = readBooleanValue(git.hasConflicts)
566
+ ?? (Array.isArray(git.conflictFiles) && git.conflictFiles.length > 0);
567
+ const base = {
568
+ defaultBranch,
569
+ branch,
570
+ upstream,
571
+ upstreamStatus,
572
+ ahead,
573
+ behind,
574
+ isWorktree: args.node?.isLocalWorktree === true || args.status.isLocalWorktree === true,
575
+ isDefaultBranch: branch === defaultBranch,
576
+ };
577
+
578
+ if (readBooleanValue(git.isGitRepo) !== true) {
579
+ return {
580
+ ...base,
581
+ status: 'blocked_review',
582
+ needsConvergence: true,
583
+ reason: 'git_status_unavailable',
584
+ nextStep: `Resolve git status for node '${nodeLabel}' before marking the task complete.`,
585
+ };
586
+ }
587
+
588
+ if (!branch) {
589
+ return {
590
+ ...base,
591
+ status: 'blocked_review',
592
+ needsConvergence: true,
593
+ reason: 'branch_unknown',
594
+ nextStep: `Inspect node '${nodeLabel}' git branch before deciding whether it is merged to ${defaultBranch}.`,
595
+ };
596
+ }
597
+
598
+ if (hasConflicts || uncommittedChanges > 0) {
599
+ return {
600
+ ...base,
601
+ status: 'not_mergeable',
602
+ needsConvergence: true,
603
+ reason: hasConflicts ? 'conflicts_present' : 'dirty_workspace',
604
+ nextStep: `Commit, checkpoint, or resolve node '${nodeLabel}' before any main convergence step.`,
605
+ };
606
+ }
607
+
608
+ if (branch === defaultBranch) {
609
+ if (upstream && upstreamStatus !== 'fresh') {
610
+ return {
611
+ ...base,
612
+ status: 'blocked_review',
613
+ needsConvergence: true,
614
+ reason: 'default_branch_upstream_unverified',
615
+ nextStep: `Refresh ${defaultBranch}'s upstream refs or resolve the fetch failure before declaring convergence complete for node '${nodeLabel}'.`,
616
+ };
617
+ }
618
+ if (ahead > 0 || behind > 0) {
619
+ return {
620
+ ...base,
621
+ status: 'blocked_review',
622
+ needsConvergence: true,
623
+ reason: 'default_branch_not_even_with_upstream',
624
+ nextStep: `Bring ${defaultBranch} even with its upstream before declaring convergence complete.`,
625
+ };
626
+ }
627
+ return {
628
+ ...base,
629
+ status: 'merged_to_main',
630
+ needsConvergence: false,
631
+ reason: 'clean_default_branch',
632
+ nextStep: null,
633
+ };
634
+ }
635
+
636
+ if (args.node?.isLocalWorktree === true || args.status.isLocalWorktree === true) {
637
+ return {
638
+ ...base,
639
+ status: 'cleanup_candidate',
640
+ needsConvergence: true,
641
+ reason: 'clean_non_default_worktree_branch',
642
+ nextStep: `Run mesh_refine_node(node_id: "${nodeLabel}") or explicitly classify this worktree as blocked_review/not_mergeable before ending the task.`,
643
+ };
644
+ }
645
+
646
+ if (upstream && upstreamStatus !== 'fresh') {
647
+ return {
648
+ ...base,
649
+ status: 'blocked_review',
650
+ needsConvergence: true,
651
+ reason: 'feature_branch_upstream_unverified',
652
+ nextStep: `Refresh branch '${branch}' upstream refs or resolve the fetch failure before deciding whether it is ready to merge into ${defaultBranch}.`,
653
+ };
654
+ }
655
+
656
+ if (!upstream || ahead > 0 || behind > 0) {
657
+ return {
658
+ ...base,
659
+ status: 'blocked_review',
660
+ needsConvergence: true,
661
+ reason: !upstream ? 'feature_branch_missing_upstream' : 'feature_branch_not_even_with_upstream',
662
+ nextStep: `Push or reconcile branch '${branch}', then merge it into ${defaultBranch} or mark it not_mergeable with a reason.`,
663
+ };
664
+ }
665
+
666
+ return {
667
+ ...base,
668
+ status: 'pushed_feature_branch_needs_merge',
669
+ needsConvergence: true,
670
+ reason: 'clean_non_default_branch',
671
+ nextStep: `Review and merge branch '${branch}' into ${defaultBranch}; do not report the task as fully complete while it remains off main.`,
672
+ };
673
+ }
674
+
675
+ function applyInlineMeshBranchConvergence(mesh: any, node: any, status: Record<string, unknown>): void {
676
+ const git = readObjectRecord(status.git);
677
+ if (Object.keys(git).length === 0 && !status.gitProbePending) return;
678
+ const uncommittedChanges = countGitWorktreeChanges(git);
679
+ status.isDirty = uncommittedChanges > 0;
680
+ status.uncommittedChanges = uncommittedChanges;
681
+ status.branchConvergence = buildInlineMeshBranchConvergence({ mesh, node, status });
682
+ }
683
+
684
+ function summarizeInlineMeshBranchConvergence(nodes: Array<Record<string, unknown>>): Record<string, unknown> {
685
+ const followUps = nodes
686
+ .filter(node => readObjectRecord(node.branchConvergence).needsConvergence === true)
687
+ .map(node => {
688
+ const convergence = readObjectRecord(node.branchConvergence);
689
+ return {
690
+ nodeId: node.nodeId,
691
+ workspace: node.workspace,
692
+ branch: convergence.branch,
693
+ status: convergence.status,
694
+ reason: convergence.reason,
695
+ nextStep: convergence.nextStep,
696
+ };
697
+ });
698
+
699
+ return {
700
+ needsFollowUp: followUps.length > 0,
701
+ unresolvedCount: followUps.length,
702
+ requiredFinalStates: ['merged_to_main', 'pushed_feature_branch_needs_merge', 'blocked_review', 'cleanup_candidate', 'not_mergeable'],
703
+ followUps,
704
+ };
705
+ }
706
+
252
707
  function readCachedInlineMeshActiveSessions(node: any): string[] {
253
708
  const cachedStatus = readObjectRecord(node?.cachedStatus);
254
709
  const activeSession = readObjectRecord(cachedStatus.activeSession);
@@ -313,6 +768,152 @@ function toIsoTimestamp(value: unknown): string | null {
313
768
  return stringValue || null;
314
769
  }
315
770
 
771
+ function synthesizeMeshNodeFreshnessFromConnection(status: Record<string, unknown>): void {
772
+ const connection = readObjectRecord(status.connection);
773
+ const connectionFreshAt = toIsoTimestamp(connection.lastCommandAt ?? connection.lastConnectedAt ?? connection.lastStateChangeAt);
774
+ const git = readObjectRecord(status.git);
775
+ const gitCheckedAt = toIsoTimestamp(git.lastCheckedAt);
776
+ if (!status.lastSeenAt && connectionFreshAt) status.lastSeenAt = connectionFreshAt;
777
+ if (!status.updatedAt && (gitCheckedAt || connectionFreshAt)) {
778
+ status.updatedAt = gitCheckedAt ?? connectionFreshAt;
779
+ }
780
+ }
781
+
782
+ function finalizeMeshNodeStatus(args: {
783
+ status: Record<string, unknown>;
784
+ node: any;
785
+ daemonId?: string;
786
+ isSelfNode: boolean;
787
+ }): void {
788
+ const { status, node, daemonId, isSelfNode } = args;
789
+ if (!readStringValue(status.machineStatus)) {
790
+ const cachedStatus = readObjectRecord(node?.cachedStatus);
791
+ const machineStatus = readStringValue(cachedStatus.machineStatus, cachedStatus.machine_status, node?.machineStatus);
792
+ if (machineStatus) status.machineStatus = machineStatus;
793
+ }
794
+ synthesizeMeshNodeFreshnessFromConnection(status);
795
+ const connectionState = readStringValue(readObjectRecord(status.connection).state);
796
+ status.launchReady = !!daemonId && (
797
+ readStringValue(status.machineStatus) === 'online'
798
+ || connectionState === 'connected'
799
+ || isSelfNode
800
+ );
801
+ }
802
+
803
+ async function probeRemoteMeshGitStatus(args: {
804
+ dispatchMeshCommand?: (daemonId: string, cmd: string, args: Record<string, unknown>) => Promise<unknown>;
805
+ daemonId: string;
806
+ workspace: string;
807
+ timeoutMs: number;
808
+ }): Promise<Record<string, unknown> | null> {
809
+ if (!args.dispatchMeshCommand) return null;
810
+ const remoteResult = await Promise.race([
811
+ args.dispatchMeshCommand(args.daemonId, 'git_status', { workspace: args.workspace, refreshUpstream: true }),
812
+ new Promise<never>((_, reject) => setTimeout(() => reject(new Error('timeout')), args.timeoutMs)),
813
+ ]) as any;
814
+ const remoteGit = remoteResult?.status ?? remoteResult?.git ?? remoteResult;
815
+ return remoteGit && typeof remoteGit === 'object' && typeof remoteGit.isGitRepo === 'boolean'
816
+ ? remoteGit as Record<string, unknown>
817
+ : null;
818
+ }
819
+
820
+ async function hydrateInlineMeshDirectTruth(args: {
821
+ mesh: any;
822
+ meshSource: 'inline_cache' | 'inline_bootstrap' | 'local_config';
823
+ dispatchMeshCommand?: (daemonId: string, cmd: string, args: Record<string, unknown>) => Promise<unknown>;
824
+ statusInstanceId?: string;
825
+ localMachineId?: string;
826
+ }): Promise<{
827
+ directEvidenceCount: number;
828
+ localConfirmedCount: number;
829
+ peerAttemptedCount: number;
830
+ peerConfirmedCount: number;
831
+ unavailableNodeIds: string[];
832
+ }> {
833
+ const nodes = Array.isArray(args.mesh?.nodes) ? args.mesh.nodes : [];
834
+ if (!nodes.length) {
835
+ return {
836
+ directEvidenceCount: 0,
837
+ localConfirmedCount: 0,
838
+ peerAttemptedCount: 0,
839
+ peerConfirmedCount: 0,
840
+ unavailableNodeIds: [],
841
+ };
842
+ }
843
+
844
+ const selectedCoordinatorNodeId = readStringValue(
845
+ args.mesh?.coordinator?.preferredNodeId,
846
+ nodes[0]?.id,
847
+ nodes[0]?.nodeId,
848
+ );
849
+
850
+ let localConfirmedCount = 0;
851
+ let peerAttemptedCount = 0;
852
+ let peerConfirmedCount = 0;
853
+ const unavailableNodeIds: string[] = [];
854
+
855
+ for (const [nodeIndex, node] of nodes.entries()) {
856
+ const nodeId = readStringValue(node?.id, node?.nodeId) || `node_${nodeIndex}`;
857
+ const workspace = readStringValue(node?.workspace);
858
+ const daemonId = readStringValue(node?.daemonId);
859
+ const isSelfNode = Boolean(
860
+ nodeId && selectedCoordinatorNodeId && nodeId === selectedCoordinatorNodeId,
861
+ ) || Boolean(
862
+ daemonId && (daemonId === args.localMachineId || daemonId === args.statusInstanceId),
863
+ ) || Boolean(args.meshSource !== 'local_config' && nodeIndex === 0);
864
+
865
+ if (!workspace) {
866
+ if (!isSelfNode && daemonId) unavailableNodeIds.push(nodeId);
867
+ continue;
868
+ }
869
+
870
+ if (fs.existsSync(workspace)) {
871
+ try {
872
+ const localGit = await getGitRepoStatus(workspace, { timeoutMs: 10_000, refreshUpstream: true });
873
+ if (localGit?.isGitRepo) {
874
+ recordInlineMeshDirectGitTruth(node, localGit as unknown as Record<string, unknown>, 'selected_coordinator_local_git');
875
+ localConfirmedCount += 1;
876
+ continue;
877
+ }
878
+ } catch {
879
+ // Fall through to remote classification.
880
+ }
881
+ }
882
+
883
+ if (!daemonId || !args.dispatchMeshCommand) {
884
+ if (!isSelfNode) unavailableNodeIds.push(nodeId);
885
+ continue;
886
+ }
887
+
888
+ peerAttemptedCount += 1;
889
+ try {
890
+ const remoteGit = await probeRemoteMeshGitStatus({
891
+ dispatchMeshCommand: args.dispatchMeshCommand,
892
+ daemonId,
893
+ workspace,
894
+ timeoutMs: 8_000,
895
+ });
896
+ if (remoteGit) {
897
+ recordInlineMeshDirectGitTruth(node, remoteGit, 'selected_coordinator_mesh_p2p_git');
898
+ peerConfirmedCount += 1;
899
+ continue;
900
+ }
901
+ } catch {
902
+ // Strict direct-only path: do not fall back to persisted cloud truth here.
903
+ }
904
+
905
+ unavailableNodeIds.push(nodeId);
906
+ }
907
+
908
+ return {
909
+ directEvidenceCount: localConfirmedCount + peerConfirmedCount,
910
+ localConfirmedCount,
911
+ peerAttemptedCount,
912
+ peerConfirmedCount,
913
+ unavailableNodeIds,
914
+ };
915
+ }
916
+
316
917
  function summarizeMeshSessionRecord(record: any): Record<string, unknown> {
317
918
  return {
318
919
  sessionId: readStringValue(record?.sessionId) || 'unknown',
@@ -328,11 +929,85 @@ function summarizeMeshSessionRecord(record: any): Record<string, unknown> {
328
929
  };
329
930
  }
330
931
 
331
- function applyCachedInlineMeshNodeStatus(status: Record<string, unknown>, node: any): boolean {
932
+ function liveSessionRecordMatchesMeshNode(record: any, meshId: string, nodeId: string): boolean {
933
+ const recordNodeId = readStringValue(record?.meta?.meshNodeId);
934
+ if (!recordNodeId || recordNodeId !== nodeId) return false;
935
+ const recordMeshId = readStringValue(record?.meta?.meshNodeFor);
936
+ return !recordMeshId || recordMeshId === meshId;
937
+ }
938
+
939
+ function liveSessionRecordMatchesMeshWorkspace(record: any, meshId: string, workspace: string): boolean {
940
+ const recordWorkspace = readStringValue(record?.workspace);
941
+ if (!recordWorkspace || !workspace || recordWorkspace !== workspace) return false;
942
+
943
+ const recordMeshId = readStringValue(record?.meta?.meshNodeFor);
944
+ if (recordMeshId) return recordMeshId === meshId;
945
+
946
+ return record?.meta?.launchedByCoordinator === true || !!readStringValue(record?.meta?.meshNodeId);
947
+ }
948
+
949
+ function readLiveMeshNodeWorkspace(args: {
950
+ meshId: string;
951
+ nodeId: string;
952
+ liveSessionRecords: any[];
953
+ allowCoordinatorSession?: boolean;
954
+ }): string {
955
+ const directNodeWorkspace = args.liveSessionRecords.find((record) => (
956
+ liveSessionRecordMatchesMeshNode(record, args.meshId, args.nodeId)
957
+ && readStringValue(record?.workspace)
958
+ ));
959
+ if (directNodeWorkspace) {
960
+ return readStringValue(directNodeWorkspace.workspace) || '';
961
+ }
962
+
963
+ if (args.allowCoordinatorSession) {
964
+ const coordinatorWorkspace = args.liveSessionRecords.find((record) => (
965
+ readStringValue(record?.meta?.meshCoordinatorFor) === args.meshId
966
+ && readStringValue(record?.workspace)
967
+ ));
968
+ if (coordinatorWorkspace) {
969
+ return readStringValue(coordinatorWorkspace.workspace) || '';
970
+ }
971
+ }
972
+
973
+ return '';
974
+ }
975
+
976
+ function collectLiveMeshSessionRecords(args: {
977
+ meshId: string;
978
+ node: any;
979
+ nodeId: string;
980
+ liveSessionRecords: any[];
981
+ allowCoordinatorSession?: boolean;
982
+ }): any[] {
983
+ const matches = args.liveSessionRecords.filter((record) => {
984
+ const nodeWorkspace = readStringValue(args.node?.workspace);
985
+ if (liveSessionRecordMatchesMeshNode(record, args.meshId, args.nodeId)) return true;
986
+ return !!nodeWorkspace && liveSessionRecordMatchesMeshWorkspace(record, args.meshId, nodeWorkspace);
987
+ });
988
+
989
+ if (args.allowCoordinatorSession) {
990
+ for (const record of args.liveSessionRecords) {
991
+ if (readStringValue(record?.meta?.meshCoordinatorFor) !== args.meshId) continue;
992
+ const sessionId = readStringValue(record?.sessionId);
993
+ if (sessionId && matches.some((entry) => readStringValue(entry?.sessionId) === sessionId)) continue;
994
+ matches.push(record);
995
+ }
996
+ }
997
+
998
+ return matches;
999
+ }
1000
+
1001
+ function applyCachedInlineMeshNodeStatus(
1002
+ status: Record<string, unknown>,
1003
+ node: any,
1004
+ options?: { skipGit?: boolean; skipError?: boolean; skipHealth?: boolean },
1005
+ ): boolean {
332
1006
  const cachedStatus = readObjectRecord(node?.cachedStatus);
333
- const git = buildCachedInlineMeshGitStatus(node);
334
- const error = readStringValue(cachedStatus.error, node?.error);
335
- const health = readStringValue(cachedStatus.health, node?.health);
1007
+ const liveGit = buildInlineMeshTransitGitStatus(node);
1008
+ const git = options?.skipGit ? undefined : (liveGit ?? buildCachedInlineMeshGitStatus(node));
1009
+ const error = options?.skipError ? undefined : (liveGit ? undefined : readStringValue(cachedStatus.error, node?.error));
1010
+ const health = options?.skipHealth ? undefined : (liveGit ? undefined : readStringValue(cachedStatus.health, node?.health));
336
1011
  const machineStatus = readStringValue(cachedStatus.machineStatus, node?.machineStatus);
337
1012
  const lastSeenAt = toIsoTimestamp(cachedStatus.lastSeenAt ?? cachedStatus.last_seen_at ?? node?.lastSeenAt ?? node?.last_seen_at);
338
1013
  const updatedAt = toIsoTimestamp(cachedStatus.updatedAt ?? cachedStatus.updated_at ?? node?.updatedAt ?? node?.updated_at);
@@ -389,29 +1064,114 @@ async function resolveProviderTypeFromPriority(args: {
389
1064
  }
390
1065
  type MeshCoordinatorConfigFormat = 'claude_mcp_json' | 'hermes_config_yaml';
391
1066
  type MeshRefineValidationStatus = 'passed' | 'failed' | 'skipped';
392
- type MeshRefineValidationCommand = {
393
- command: string;
394
- args: string[];
395
- displayCommand: string;
396
- category: string;
397
- source: string;
398
- };
1067
+ type MeshRefineValidationCommand = MeshRefineValidationCommandPlan;
399
1068
 
400
1069
  type MeshRefineValidationSummary = {
401
1070
  status: MeshRefineValidationStatus;
402
1071
  required: true;
403
1072
  commandsRun: Array<Record<string, unknown>>;
1073
+ bootstrapCommandsRun: Array<Record<string, unknown>>;
404
1074
  rejectedCommands: Array<Record<string, unknown>>;
405
1075
  skippedReason?: string;
1076
+ failureKind?: string;
1077
+ failureCode?: string;
406
1078
  timeoutMs: number;
407
1079
  outputLimitBytes: number;
1080
+ configSource?: string;
1081
+ configSourceType?: string;
1082
+ suggestions?: unknown[];
1083
+ suggestedConfig?: unknown;
408
1084
  };
409
1085
 
1086
+ type MeshRefineStageStatus = 'passed' | 'failed' | 'skipped';
1087
+
1088
+ type MeshRefinePatchEquivalenceSummary = {
1089
+ status: MeshRefineStageStatus;
1090
+ equivalent: boolean;
1091
+ baseHead: string;
1092
+ branchHead: string;
1093
+ mergeBase?: string;
1094
+ mergedTree?: string;
1095
+ expectedPatchId?: string;
1096
+ actualPatchId?: string;
1097
+ durationMs: number;
1098
+ error?: string;
1099
+ stdout?: string;
1100
+ stderr?: string;
1101
+ };
1102
+
1103
+ type MeshRefineSubmoduleReachabilityEntry = {
1104
+ path: string;
1105
+ commit: string;
1106
+ reachable: boolean;
1107
+ publishRequired?: boolean;
1108
+ autoPublishAllowed?: boolean;
1109
+ autoPublishAttempted?: boolean;
1110
+ autoPublishSucceeded?: boolean;
1111
+ autoPublishVerified?: boolean;
1112
+ autoPublishRefspec?: string;
1113
+ autoPublishSkippedReason?: string;
1114
+ importedFromWorktree?: boolean;
1115
+ checkedLocal?: boolean;
1116
+ localReachable?: boolean;
1117
+ remote?: string;
1118
+ remoteUrl?: string;
1119
+ remoteReachable?: boolean;
1120
+ remoteMainBranch?: string;
1121
+ remoteMainReachable?: boolean;
1122
+ fetchedFromOrigin?: boolean;
1123
+ error?: string;
1124
+ publishStdout?: string;
1125
+ publishStderr?: string;
1126
+ };
1127
+
1128
+ type MeshRefineSubmoduleReachabilitySummary = {
1129
+ status: MeshRefineStageStatus;
1130
+ checked: number;
1131
+ unreachable: MeshRefineSubmoduleReachabilityEntry[];
1132
+ entries: MeshRefineSubmoduleReachabilityEntry[];
1133
+ durationMs: number;
1134
+ autoPublishAllowed?: boolean;
1135
+ autoPublishPolicySource?: string;
1136
+ error?: string;
1137
+ };
1138
+
1139
+ type MeshRefineAsyncJobStatus = 'accepted' | 'completed' | 'failed';
1140
+
1141
+ type MeshRefineJobHandle = {
1142
+ success: true;
1143
+ async: true;
1144
+ status: MeshRefineAsyncJobStatus;
1145
+ jobId: string;
1146
+ interactionId: string;
1147
+ meshId: string;
1148
+ nodeId: string;
1149
+ targetNodeId: string;
1150
+ targetDaemonId?: string;
1151
+ workspace?: string;
1152
+ startedAt: string;
1153
+ completedAt?: string;
1154
+ duplicate?: boolean;
1155
+ retryOfJobId?: string;
1156
+ eventDelivery: {
1157
+ pendingEvents: true;
1158
+ ledger: true;
1159
+ };
1160
+ evidence: {
1161
+ pendingEventsCommand: 'get_pending_mesh_events';
1162
+ ledgerCommand: 'get_mesh_ledger_slice';
1163
+ taskHistoryKind: 'task_dispatched' | 'task_completed' | 'task_failed';
1164
+ };
1165
+ };
1166
+
1167
+ type MeshRefineTerminalJob = MeshRefineJobHandle & { result?: Record<string, unknown> };
1168
+
410
1169
  const REFINE_VALIDATION_CATEGORIES = ['typecheck', 'test', 'lint', 'build'] as const;
411
1170
  const REFINE_VALIDATION_TIMEOUT_MS = 120_000;
412
1171
  const REFINE_VALIDATION_OUTPUT_LIMIT_BYTES = 128 * 1024;
413
1172
  const REFINE_VALIDATION_SUMMARY_CHARS = 2_000;
414
1173
  const REFINE_VALIDATION_MAX_COMMANDS = 4;
1174
+ const REFINE_PATCH_EQUIVALENCE_OUTPUT_LIMIT_BYTES = 4 * 1024 * 1024;
415
1175
 
416
1176
  function truncateValidationOutput(value: unknown): string {
417
1177
  const text = typeof value === 'string' ? value : value == null ? '' : String(value);
@@ -419,171 +1179,336 @@ function truncateValidationOutput(value: unknown): string {
419
1179
  return `${text.slice(0, REFINE_VALIDATION_SUMMARY_CHARS)}\n[truncated ${text.length - REFINE_VALIDATION_SUMMARY_CHARS} chars]`;
420
1180
  }
421
1181
 
422
- function readPackageScripts(workspace: string): Record<string, string> {
423
- try {
424
- const packageJsonPath = pathJoin(workspace, 'package.json');
425
- const parsed = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
426
- return parsed?.scripts && typeof parsed.scripts === 'object' && !Array.isArray(parsed.scripts)
427
- ? parsed.scripts as Record<string, string>
428
- : {};
429
- } catch {
430
- return {};
431
- }
432
- }
433
-
434
- function tokenizeValidationCommand(command: string): string[] | null {
435
- const trimmed = command.trim();
436
- if (!trimmed) return null;
437
- // Fail closed: the gate never hands shell syntax to a shell. Package-manager
438
- // scripts are invoked via execFile(binary, args), and metacharacters/quotes are
439
- // rejected before tokenization so `npm run test && rm -rf` cannot be smuggled in.
440
- if (/[;&|<>`$\\\n\r'\"]/.test(trimmed)) return null;
441
- const tokens = trimmed.split(/\s+/).filter(Boolean);
442
- if (!tokens.length) return null;
443
- if (tokens.some(token => !/^[A-Za-z0-9_@./:=+-]+$/.test(token))) return null;
444
- return tokens;
1182
+ function recordMeshRefineStage(
1183
+ stages: Array<Record<string, unknown>>,
1184
+ stage: string,
1185
+ status: MeshRefineStageStatus,
1186
+ startedAt: number,
1187
+ details?: Record<string, unknown>,
1188
+ ): void {
1189
+ stages.push({
1190
+ stage,
1191
+ status,
1192
+ durationMs: Date.now() - startedAt,
1193
+ ...(details || {}),
1194
+ });
445
1195
  }
446
1196
 
447
- function scriptMatchesValidationCategory(scriptName: string, category: string): boolean {
448
- return scriptName === category || scriptName.startsWith(`${category}:`);
1197
+ function buildSubmodulePublishRequiredNextStep(entries: MeshRefineSubmoduleReachabilityEntry[]): string {
1198
+ const refs = entries
1199
+ .map(entry => `${entry.path}@${entry.commit}`)
1200
+ .join(', ');
1201
+ return `Ask the user for explicit approval to push/publish the unreachable submodule commit(s) (${refs}) to the configured submodule remote main branch, then rerun mesh_refine_node. Do not merge the root branch until every submodule gitlink commit is reachable from submodule origin/main.`;
449
1202
  }
450
1203
 
451
- function parsePackageManagerValidationCommand(
452
- rawCommand: string,
453
- category: string,
454
- scripts: Record<string, string>,
455
- source: string,
456
- ): { command?: MeshRefineValidationCommand; rejected?: Record<string, unknown> } {
457
- const tokens = tokenizeValidationCommand(rawCommand);
458
- if (!tokens) {
459
- return { rejected: { command: rawCommand, category, source, reason: 'unsafe command string is not allowlisted' } };
460
- }
461
-
462
- const [binary, second, third, ...rest] = tokens;
463
- let scriptName = '';
464
- let command = binary;
465
- let args: string[] = [];
466
-
467
- if ((binary === 'npm' || binary === 'pnpm' || binary === 'bun') && second === 'run' && third) {
468
- scriptName = third;
469
- args = ['run', scriptName, ...rest];
470
- } else if (binary === 'npm' && second === 'test' && !third) {
471
- scriptName = 'test';
472
- args = ['test'];
473
- } else if (binary === 'yarn' && second === 'run' && third) {
474
- scriptName = third;
475
- args = ['run', scriptName, ...rest];
476
- } else if (binary === 'yarn' && second && !third) {
477
- scriptName = second;
478
- args = [scriptName];
479
- } else {
480
- return { rejected: { command: rawCommand, category, source, reason: 'command is not a supported package-manager script invocation' } };
481
- }
482
-
483
- if (!scriptName || !Object.prototype.hasOwnProperty.call(scripts, scriptName)) {
484
- return { rejected: { command: rawCommand, category, source, script: scriptName, reason: 'script is not declared in package.json' } };
1204
+ function resolveRefineryAutoPublishSubmoduleMainCommits(mesh: any, workspace: string): { enabled: boolean; source?: string } {
1205
+ if (mesh?.policy?.allowAutoPublishSubmoduleMainCommits === true) {
1206
+ return { enabled: true, source: 'mesh.policy.allowAutoPublishSubmoduleMainCommits' };
485
1207
  }
486
- if (!scriptMatchesValidationCategory(scriptName, category)) {
487
- return { rejected: { command: rawCommand, category, source, script: scriptName, reason: 'script name is outside the validation category allowlist' } };
1208
+ const loaded = loadMeshRefineConfig(mesh, workspace);
1209
+ if (loaded.config?.allowAutoPublishSubmoduleMainCommits === true) {
1210
+ return { enabled: true, source: loaded.path || loaded.source };
488
1211
  }
1212
+ return { enabled: false };
1213
+ }
489
1214
 
490
- return {
491
- command: {
492
- command,
493
- args,
494
- displayCommand: [command, ...args].join(' '),
495
- category,
496
- source,
497
- },
498
- };
1215
+ async function computeGitPatchId(cwd: string, fromRef: string, toRef: string): Promise<string> {
1216
+ const { execFileSync } = await import('node:child_process');
1217
+ const diff = execFileSync('git', ['diff', '--patch', '--full-index', fromRef, toRef], {
1218
+ cwd,
1219
+ encoding: 'utf8',
1220
+ maxBuffer: REFINE_PATCH_EQUIVALENCE_OUTPUT_LIMIT_BYTES,
1221
+ });
1222
+ if (!diff.trim()) return '';
1223
+ const patchId = execFileSync('git', ['patch-id', '--stable'], {
1224
+ cwd,
1225
+ input: diff,
1226
+ encoding: 'utf8',
1227
+ maxBuffer: REFINE_PATCH_EQUIVALENCE_OUTPUT_LIMIT_BYTES,
1228
+ }).trim();
1229
+ return patchId.split(/\s+/)[0] || '';
499
1230
  }
500
1231
 
501
- function collectProjectContextValidationCandidates(mesh: any): Array<{ command: string; category: string; source: string; confidence?: string }> {
502
- const commands = mesh?.projectContext?.commands;
503
- if (!commands || typeof commands !== 'object' || Array.isArray(commands)) return [];
504
- const candidates: Array<{ command: string; category: string; source: string; confidence?: string }> = [];
505
- for (const category of REFINE_VALIDATION_CATEGORIES) {
506
- const entries = Array.isArray(commands[category]) ? commands[category] : [];
507
- for (const entry of entries) {
508
- if (typeof entry?.command !== 'string') continue;
509
- candidates.push({
510
- command: entry.command,
511
- category,
512
- source: typeof entry.sourcePath === 'string' ? entry.sourcePath : 'projectContext.commands',
513
- confidence: typeof entry.confidence === 'string' ? entry.confidence : undefined,
514
- });
1232
+ async function runMeshRefinePatchEquivalenceGate(
1233
+ repoRoot: string,
1234
+ baseHead: string,
1235
+ branchHead: string,
1236
+ ): Promise<MeshRefinePatchEquivalenceSummary> {
1237
+ const startedAt = Date.now();
1238
+ try {
1239
+ const { execFileSync } = await import('node:child_process');
1240
+ const git = (args: string[]) => execFileSync('git', args, {
1241
+ cwd: repoRoot,
1242
+ encoding: 'utf8',
1243
+ maxBuffer: REFINE_PATCH_EQUIVALENCE_OUTPUT_LIMIT_BYTES,
1244
+ });
1245
+ const mergeBase = git(['merge-base', baseHead, branchHead]).trim();
1246
+ const mergeTreeStdout = git(['merge-tree', '--write-tree', baseHead, branchHead]);
1247
+ const mergedTree = mergeTreeStdout.trim().split(/\s+/)[0] || '';
1248
+ if (!mergeBase || !mergedTree) {
1249
+ return {
1250
+ status: 'failed',
1251
+ equivalent: false,
1252
+ baseHead,
1253
+ branchHead,
1254
+ mergeBase: mergeBase || undefined,
1255
+ mergedTree: mergedTree || undefined,
1256
+ durationMs: Date.now() - startedAt,
1257
+ error: 'patch equivalence preflight could not resolve merge-base or synthetic merge tree',
1258
+ stdout: truncateValidationOutput(mergeTreeStdout),
1259
+ };
515
1260
  }
1261
+ const expectedPatchId = await computeGitPatchId(repoRoot, mergeBase, branchHead);
1262
+ const actualPatchId = await computeGitPatchId(repoRoot, baseHead, mergedTree);
1263
+ const equivalent = expectedPatchId === actualPatchId;
1264
+ return {
1265
+ status: equivalent ? 'passed' : 'failed',
1266
+ equivalent,
1267
+ baseHead,
1268
+ branchHead,
1269
+ mergeBase,
1270
+ mergedTree,
1271
+ expectedPatchId,
1272
+ actualPatchId,
1273
+ durationMs: Date.now() - startedAt,
1274
+ };
1275
+ } catch (e: any) {
1276
+ return {
1277
+ status: 'failed',
1278
+ equivalent: false,
1279
+ baseHead,
1280
+ branchHead,
1281
+ durationMs: Date.now() - startedAt,
1282
+ error: e?.message || String(e),
1283
+ stdout: truncateValidationOutput(e?.stdout),
1284
+ stderr: truncateValidationOutput(e?.stderr),
1285
+ };
516
1286
  }
517
- return candidates.sort((a, b) => {
518
- const rank = (value?: string) => value === 'high' ? 0 : value === 'medium' ? 1 : 2;
519
- return rank(a.confidence) - rank(b.confidence);
520
- });
521
1287
  }
522
1288
 
523
- function collectPolicyValidationCandidates(mesh: any): Array<{ command: string; category: string; source: string }> {
524
- const policy = mesh?.policy && typeof mesh.policy === 'object' && !Array.isArray(mesh.policy) ? mesh.policy : {};
525
- const configured = Array.isArray(policy.validationCommands)
526
- ? policy.validationCommands
527
- : Array.isArray(policy.validationGate?.commands)
528
- ? policy.validationGate.commands
529
- : [];
530
- return configured
531
- .map((entry: any) => typeof entry === 'string' ? { command: entry, category: '', source: 'mesh.policy.validationCommands' } : entry)
532
- .filter((entry: any) => entry && typeof entry.command === 'string')
533
- .map((entry: any) => {
534
- const commandText = entry.command.trim();
535
- const category = REFINE_VALIDATION_CATEGORIES.find(cat => commandText.includes(` ${cat}`)) ?? '';
536
- return { command: commandText, category, source: 'mesh.policy.validationCommands' };
537
- })
538
- .filter((entry: any) => !!entry.category);
539
- }
1289
+ async function runMeshRefineSubmoduleReachabilityGate(
1290
+ repoRoot: string,
1291
+ mergedTree: string,
1292
+ options: { allowAutoPublishSubmoduleMainCommits?: boolean; autoPublishPolicySource?: string; worktreeRoot?: string } = {},
1293
+ ): Promise<MeshRefineSubmoduleReachabilitySummary> {
1294
+ const startedAt = Date.now();
1295
+ const entries: MeshRefineSubmoduleReachabilityEntry[] = [];
1296
+ try {
1297
+ const { execFile } = await import('node:child_process');
1298
+ const { promisify } = await import('node:util');
1299
+ const execFileAsync = promisify(execFile);
1300
+ const runGit = async (cwd: string, args: string[]): Promise<string> => {
1301
+ const { stdout } = await execFileAsync('git', args, {
1302
+ cwd,
1303
+ encoding: 'utf8',
1304
+ timeout: 30_000,
1305
+ maxBuffer: REFINE_PATCH_EQUIVALENCE_OUTPUT_LIMIT_BYTES,
1306
+ windowsHide: true,
1307
+ });
1308
+ return String(stdout || '');
1309
+ };
1310
+ const verifyRemoteMainContainsCommit = async (submodulePath: string, commit: string, branch = 'main'): Promise<void> => {
1311
+ await runGit(submodulePath, ['-c', 'protocol.file.allow=always', 'fetch', 'origin', `refs/heads/${branch}:refs/remotes/origin/${branch}`]);
1312
+ await runGit(submodulePath, ['merge-base', '--is-ancestor', commit, `refs/remotes/origin/${branch}`]);
1313
+ };
1314
+ const publishCommitToRemoteMain = async (submodulePath: string, commit: string, branch = 'main'): Promise<{ stdout: string; stderr: string; refspec: string }> => {
1315
+ const refspec = `${commit}:refs/heads/${branch}`;
1316
+ const { stdout, stderr } = await execFileAsync('git', ['push', 'origin', refspec], {
1317
+ cwd: submodulePath,
1318
+ encoding: 'utf8',
1319
+ timeout: 30_000,
1320
+ maxBuffer: REFINE_PATCH_EQUIVALENCE_OUTPUT_LIMIT_BYTES,
1321
+ windowsHide: true,
1322
+ });
1323
+ return { stdout: String(stdout || ''), stderr: String(stderr || ''), refspec };
1324
+ };
1325
+ const importCommitFromWorktreeSubmodule = async (submodulePath: string, worktreeSubmodulePath: string, commit: string): Promise<boolean> => {
1326
+ if (!fs.existsSync(worktreeSubmodulePath)) return false;
1327
+ try {
1328
+ await runGit(worktreeSubmodulePath, ['cat-file', '-e', `${commit}^{commit}`]);
1329
+ } catch {
1330
+ return false;
1331
+ }
1332
+ await runGit(submodulePath, ['-c', 'protocol.file.allow=always', 'fetch', worktreeSubmodulePath, commit]);
1333
+ await runGit(submodulePath, ['cat-file', '-e', `${commit}^{commit}`]);
1334
+ return true;
1335
+ };
540
1336
 
541
- function selectMeshRefineValidationCommands(mesh: any, workspace: string): { commands: MeshRefineValidationCommand[]; rejectedCommands: Array<Record<string, unknown>>; source: string } {
542
- const scripts = readPackageScripts(workspace);
543
- const rejectedCommands: Array<Record<string, unknown>> = [];
544
- const selected: MeshRefineValidationCommand[] = [];
545
- const seen = new Set<string>();
546
- const candidates = [
547
- ...collectPolicyValidationCandidates(mesh),
548
- ...collectProjectContextValidationCandidates(mesh),
549
- ];
550
-
551
- for (const candidate of candidates) {
552
- const parsed = parsePackageManagerValidationCommand(candidate.command, candidate.category, scripts, candidate.source);
553
- if (parsed.rejected) {
554
- rejectedCommands.push(parsed.rejected);
555
- continue;
556
- }
557
- if (!parsed.command || seen.has(parsed.command.displayCommand)) continue;
558
- selected.push(parsed.command);
559
- seen.add(parsed.command.displayCommand);
560
- if (selected.length >= REFINE_VALIDATION_MAX_COMMANDS) break;
561
- }
562
-
563
- if (!selected.length && candidates.length === 0) {
564
- for (const category of REFINE_VALIDATION_CATEGORIES) {
565
- if (!Object.prototype.hasOwnProperty.call(scripts, category)) continue;
566
- const fallback = parsePackageManagerValidationCommand(`npm run ${category}`, category, scripts, 'package.json:scripts');
567
- if (fallback.command && !seen.has(fallback.command.displayCommand)) {
568
- selected.push(fallback.command);
569
- seen.add(fallback.command.displayCommand);
570
- } else if (fallback.rejected) {
571
- rejectedCommands.push(fallback.rejected);
572
- }
573
- if (selected.length >= 2) break;
1337
+ const treeOutput = await runGit(repoRoot, ['ls-tree', '-r', '-z', mergedTree]);
1338
+ const gitlinks = treeOutput
1339
+ .split('\0')
1340
+ .filter(Boolean)
1341
+ .map(record => {
1342
+ const match = /^160000\s+commit\s+([0-9a-f]{40})\t(.+)$/.exec(record);
1343
+ return match ? { commit: match[1], path: match[2] } : null;
1344
+ })
1345
+ .filter((entry): entry is { commit: string; path: string } => !!entry);
1346
+
1347
+ for (const gitlink of gitlinks) {
1348
+ const submodulePath = pathResolve(repoRoot, gitlink.path);
1349
+ const entry: MeshRefineSubmoduleReachabilityEntry = {
1350
+ path: gitlink.path,
1351
+ commit: gitlink.commit,
1352
+ reachable: false,
1353
+ };
1354
+ try {
1355
+ if (!fs.existsSync(submodulePath)) {
1356
+ entry.error = `Submodule checkout missing at ${gitlink.path}`;
1357
+ entry.publishRequired = true;
1358
+ if (options.allowAutoPublishSubmoduleMainCommits === true) {
1359
+ entry.autoPublishAllowed = true;
1360
+ entry.autoPublishAttempted = false;
1361
+ entry.autoPublishSkippedReason = `submodule checkout missing at ${gitlink.path}; cannot perform non-force push to origin/main`;
1362
+ }
1363
+ entries.push(entry);
1364
+ continue;
1365
+ }
1366
+
1367
+ entry.checkedLocal = true;
1368
+ try {
1369
+ await runGit(submodulePath, ['cat-file', '-e', `${gitlink.commit}^{commit}`]);
1370
+ entry.localReachable = true;
1371
+ } catch {
1372
+ entry.localReachable = false;
1373
+ if (options.allowAutoPublishSubmoduleMainCommits === true && options.worktreeRoot) {
1374
+ try {
1375
+ const imported = await importCommitFromWorktreeSubmodule(
1376
+ submodulePath,
1377
+ pathResolve(options.worktreeRoot, gitlink.path),
1378
+ gitlink.commit,
1379
+ );
1380
+ if (imported) {
1381
+ entry.localReachable = true;
1382
+ entry.importedFromWorktree = true;
1383
+ }
1384
+ } catch (importError: any) {
1385
+ entry.autoPublishSkippedReason = `candidate commit was not present in the source checkout and could not be imported from worktree submodule: ${truncateValidationOutput(importError?.stderr || importError?.message || String(importError))}`;
1386
+ }
1387
+ }
1388
+ // Probe the submodule remote before allowing cleanup/completion.
1389
+ }
1390
+
1391
+ try {
1392
+ entry.remote = 'origin';
1393
+ let remoteUrl = '';
1394
+ try {
1395
+ remoteUrl = (await runGit(submodulePath, ['remote', 'get-url', 'origin'])).trim();
1396
+ if (!remoteUrl) throw new Error('origin remote has no URL');
1397
+ entry.remoteUrl = remoteUrl;
1398
+ } catch {
1399
+ entry.error = 'Submodule remote reachability check failed: no configured origin remote';
1400
+ entry.publishRequired = true;
1401
+ if (options.allowAutoPublishSubmoduleMainCommits === true) {
1402
+ entry.autoPublishAllowed = true;
1403
+ entry.autoPublishAttempted = false;
1404
+ entry.autoPublishSkippedReason = 'submodule origin remote is not configured; cannot perform non-force push to origin/main';
1405
+ }
1406
+ entries.push(entry);
1407
+ continue;
1408
+ }
1409
+ entry.remoteMainBranch = 'main';
1410
+ try {
1411
+ await verifyRemoteMainContainsCommit(submodulePath, gitlink.commit, 'main');
1412
+ entry.fetchedFromOrigin = true;
1413
+ entry.remoteReachable = true;
1414
+ entry.remoteMainReachable = true;
1415
+ entry.reachable = true;
1416
+ } catch (e: any) {
1417
+ entry.remoteReachable = false;
1418
+ entry.remoteMainReachable = false;
1419
+ entry.publishRequired = true;
1420
+ const details = truncateValidationOutput(e?.stderr || e?.message || String(e));
1421
+ entry.error = `Submodule remote main reachability check failed for origin/main: ${details}`;
1422
+ if (options.allowAutoPublishSubmoduleMainCommits === true && entry.localReachable === true) {
1423
+ entry.autoPublishAllowed = true;
1424
+ entry.autoPublishAttempted = true;
1425
+ try {
1426
+ const publish = await publishCommitToRemoteMain(submodulePath, gitlink.commit, 'main');
1427
+ entry.autoPublishRefspec = publish.refspec;
1428
+ entry.publishStdout = truncateValidationOutput(publish.stdout);
1429
+ entry.publishStderr = truncateValidationOutput(publish.stderr);
1430
+ entry.autoPublishSucceeded = true;
1431
+ await verifyRemoteMainContainsCommit(submodulePath, gitlink.commit, 'main');
1432
+ entry.fetchedFromOrigin = true;
1433
+ entry.remoteReachable = true;
1434
+ entry.remoteMainReachable = true;
1435
+ entry.autoPublishVerified = true;
1436
+ entry.publishRequired = false;
1437
+ entry.reachable = true;
1438
+ entry.error = undefined;
1439
+ } catch (publishError: any) {
1440
+ entry.autoPublishSucceeded = false;
1441
+ entry.autoPublishVerified = false;
1442
+ const publishDetails = truncateValidationOutput(publishError?.stderr || publishError?.message || String(publishError));
1443
+ entry.error = `Submodule auto-publish to origin/main failed or could not be verified: ${publishDetails}`;
1444
+ }
1445
+ } else if (options.allowAutoPublishSubmoduleMainCommits === true) {
1446
+ entry.autoPublishAllowed = true;
1447
+ entry.autoPublishAttempted = false;
1448
+ entry.autoPublishSkippedReason = entry.autoPublishSkippedReason
1449
+ || 'candidate commit is not reachable in the source checkout or worktree submodule, so Refinery cannot push it to origin/main';
1450
+ }
1451
+ }
1452
+ } catch (e: any) {
1453
+ entry.remoteReachable = false;
1454
+ entry.remoteMainReachable = false;
1455
+ entry.publishRequired = true;
1456
+ const details = truncateValidationOutput(e?.stderr || e?.message || String(e));
1457
+ entry.error = `Submodule remote main reachability check failed for origin/main: ${details}`;
1458
+ }
1459
+ } catch (e: any) {
1460
+ entry.error = truncateValidationOutput(e?.message || String(e));
1461
+ entry.publishRequired = true;
1462
+ }
1463
+ entries.push(entry);
574
1464
  }
1465
+
1466
+ const unreachable = entries.filter(entry => !entry.reachable);
1467
+ return {
1468
+ status: unreachable.length ? 'failed' : 'passed',
1469
+ checked: entries.length,
1470
+ unreachable: unreachable.map(entry => ({ ...entry, publishRequired: entry.publishRequired !== false })),
1471
+ entries: entries.map(entry => entry.reachable ? entry : { ...entry, publishRequired: entry.publishRequired !== false }),
1472
+ durationMs: Date.now() - startedAt,
1473
+ autoPublishAllowed: options.allowAutoPublishSubmoduleMainCommits === true,
1474
+ autoPublishPolicySource: options.autoPublishPolicySource,
1475
+ };
1476
+ } catch (e: any) {
1477
+ const unreachable = entries.filter(entry => !entry.reachable).map(entry => ({ ...entry, publishRequired: true }));
1478
+ return {
1479
+ status: 'failed',
1480
+ checked: entries.length,
1481
+ unreachable,
1482
+ entries: entries.map(entry => entry.reachable ? entry : { ...entry, publishRequired: true }),
1483
+ durationMs: Date.now() - startedAt,
1484
+ autoPublishAllowed: options.allowAutoPublishSubmoduleMainCommits === true,
1485
+ autoPublishPolicySource: options.autoPublishPolicySource,
1486
+ error: truncateValidationOutput(e?.message || String(e)),
1487
+ };
575
1488
  }
1489
+ }
576
1490
 
1491
+ function buildMeshRefineValidationPlan(mesh: any, workspace: string): Record<string, unknown> {
1492
+ const plan = resolveMeshRefineValidationPlan(mesh, workspace);
1493
+ const mapCommand = (command: MeshRefineValidationCommandPlan) => ({
1494
+ displayCommand: command.displayCommand,
1495
+ category: command.category,
1496
+ source: command.source,
1497
+ cwd: command.cwd,
1498
+ timeoutMs: command.timeoutMs,
1499
+ });
577
1500
  return {
578
- commands: selected,
579
- rejectedCommands,
580
- source: selected.some(command => command.source === 'mesh.policy.validationCommands')
581
- ? 'mesh_policy'
582
- : selected.some(command => command.source !== 'package.json:scripts')
583
- ? 'project_context'
584
- : selected.length
585
- ? 'package_json_scripts'
586
- : 'unavailable',
1501
+ source: plan.source,
1502
+ sourceType: plan.sourceType,
1503
+ bootstrapCommands: plan.bootstrapCommands.map(mapCommand),
1504
+ commands: plan.commands.map(mapCommand),
1505
+ unavailableReason: plan.unavailableReason,
1506
+ rejectedCommands: plan.rejectedCommands,
1507
+ suggestions: plan.suggestions,
1508
+ suggestedConfig: plan.suggestedConfig,
1509
+ note: plan.sourceType === 'unavailable'
1510
+ ? 'No validation command will be executed until a repo mesh/refine config is provided. Heuristics are suggestions only.'
1511
+ : 'Validation commands are resolved from repo mesh/refine config; heuristics are suggestions only.',
587
1512
  };
588
1513
  }
589
1514
 
@@ -591,60 +1516,119 @@ async function runMeshRefineValidationGate(mesh: any, workspace: string): Promis
591
1516
  const { execFile } = await import('node:child_process');
592
1517
  const { promisify } = await import('node:util');
593
1518
  const execFileAsync = promisify(execFile);
594
- const selection = selectMeshRefineValidationCommands(mesh, workspace);
1519
+ const selection = resolveMeshRefineValidationPlan(mesh, workspace);
595
1520
  const summary: MeshRefineValidationSummary = {
596
1521
  status: 'skipped',
597
1522
  required: true,
598
1523
  commandsRun: [],
1524
+ bootstrapCommandsRun: [],
599
1525
  rejectedCommands: selection.rejectedCommands,
600
1526
  skippedReason: undefined,
601
1527
  timeoutMs: REFINE_VALIDATION_TIMEOUT_MS,
602
1528
  outputLimitBytes: REFINE_VALIDATION_OUTPUT_LIMIT_BYTES,
1529
+ configSource: selection.source,
1530
+ configSourceType: selection.sourceType,
1531
+ suggestions: selection.suggestions,
1532
+ suggestedConfig: selection.suggestedConfig,
603
1533
  };
604
1534
 
605
1535
  if (!selection.commands.length) {
606
- summary.skippedReason = 'validation_unavailable: no allowlisted projectContext, mesh policy, or package.json build/test/typecheck/lint command was available';
1536
+ summary.skippedReason = selection.unavailableReason || 'validation_unavailable: repo mesh/refine config did not provide executable validation.commands';
607
1537
  return summary;
608
1538
  }
609
1539
 
610
- for (const candidate of selection.commands) {
1540
+ const commandRecord = (candidate: MeshRefineValidationCommand, cwd: string, startedAt: number, result: any, passed: boolean, extras: Record<string, unknown> = {}) => ({
1541
+ command: candidate.command,
1542
+ args: candidate.args,
1543
+ displayCommand: candidate.displayCommand,
1544
+ category: candidate.category,
1545
+ source: candidate.source,
1546
+ cwd,
1547
+ passed,
1548
+ durationMs: Date.now() - startedAt,
1549
+ stdout: truncateValidationOutput(result?.stdout),
1550
+ stderr: truncateValidationOutput(result?.stderr || result?.message),
1551
+ ...extras,
1552
+ });
1553
+ const isPackageManagerValidation = (candidate: MeshRefineValidationCommand): boolean => {
1554
+ const command = pathBasename(candidate.command).replace(/\.(?:cmd|exe)$/i, '');
1555
+ return ['npm', 'pnpm', 'yarn', 'bun'].includes(command)
1556
+ && candidate.args.some(arg => arg === 'run' || arg === 'test' || arg === 'exec');
1557
+ };
1558
+ const dependenciesLikelyMissing = (cwd: string): boolean => {
1559
+ if (!fs.existsSync(pathJoin(cwd, 'package.json'))) return false;
1560
+ if (fs.existsSync(pathJoin(cwd, 'node_modules'))) return false;
1561
+ return ['package-lock.json', 'npm-shrinkwrap.json', 'pnpm-lock.yaml', 'yarn.lock', 'bun.lockb', 'bun.lock']
1562
+ .some(lock => fs.existsSync(pathJoin(cwd, lock)));
1563
+ };
1564
+
1565
+ for (const candidate of selection.bootstrapCommands) {
611
1566
  const startedAt = Date.now();
1567
+ const cwd = candidate.cwd ? pathResolve(workspace, candidate.cwd) : workspace;
1568
+ const timeout = candidate.timeoutMs || REFINE_VALIDATION_TIMEOUT_MS;
612
1569
  try {
613
1570
  const result = await execFileAsync(candidate.command, candidate.args, {
614
- cwd: workspace,
1571
+ cwd,
615
1572
  encoding: 'utf8',
616
- timeout: REFINE_VALIDATION_TIMEOUT_MS,
1573
+ timeout,
617
1574
  maxBuffer: REFINE_VALIDATION_OUTPUT_LIMIT_BYTES,
618
- env: { ...process.env, CI: process.env.CI || '1' },
619
- });
620
- summary.commandsRun.push({
621
- command: candidate.command,
622
- args: candidate.args,
623
- displayCommand: candidate.displayCommand,
624
- category: candidate.category,
625
- source: candidate.source,
626
- passed: true,
627
- exitCode: 0,
628
- durationMs: Date.now() - startedAt,
629
- stdout: truncateValidationOutput(result.stdout),
630
- stderr: truncateValidationOutput(result.stderr),
1575
+ env: { ...process.env, CI: process.env.CI || '1', ...(candidate.env || {}) },
631
1576
  });
1577
+ summary.bootstrapCommandsRun.push(commandRecord(candidate, cwd, startedAt, result, true, { exitCode: 0 }));
632
1578
  } catch (error: any) {
633
- summary.commandsRun.push({
634
- command: candidate.command,
635
- args: candidate.args,
636
- displayCommand: candidate.displayCommand,
637
- category: candidate.category,
638
- source: candidate.source,
639
- passed: false,
1579
+ summary.bootstrapCommandsRun.push(commandRecord(candidate, cwd, startedAt, error, false, {
640
1580
  exitCode: typeof error?.code === 'number' ? error.code : null,
641
1581
  signal: typeof error?.signal === 'string' ? error.signal : null,
642
1582
  timedOut: error?.killed === true || /timed out/i.test(String(error?.message || '')),
643
- durationMs: Date.now() - startedAt,
644
- stdout: truncateValidationOutput(error?.stdout),
645
- stderr: truncateValidationOutput(error?.stderr || error?.message),
1583
+ failureKind: 'dependency_bootstrap_failed',
1584
+ }));
1585
+ summary.status = 'failed';
1586
+ summary.failureKind = 'dependency_bootstrap_failed';
1587
+ summary.failureCode = 'dependency_bootstrap_failed';
1588
+ return summary;
1589
+ }
1590
+ }
1591
+
1592
+ for (const candidate of selection.commands) {
1593
+ const startedAt = Date.now();
1594
+ const cwd = candidate.cwd ? pathResolve(workspace, candidate.cwd) : workspace;
1595
+ const timeout = candidate.timeoutMs || REFINE_VALIDATION_TIMEOUT_MS;
1596
+ if (selection.bootstrapCommands.length === 0 && isPackageManagerValidation(candidate) && dependenciesLikelyMissing(cwd)) {
1597
+ summary.commandsRun.push(commandRecord(candidate, cwd, startedAt, {
1598
+ stderr: 'Dependencies appear to be missing: package.json and a lockfile are present, but node_modules is absent. Configure validation.bootstrapCommands in repo mesh/refine config if Refinery should install/bootstrap before validation.',
1599
+ }, false, {
1600
+ exitCode: null,
1601
+ skipped: true,
1602
+ failureKind: 'missing_dependencies',
1603
+ }));
1604
+ summary.status = 'failed';
1605
+ summary.failureKind = 'missing_dependencies';
1606
+ summary.failureCode = 'missing_dependencies';
1607
+ return summary;
1608
+ }
1609
+ try {
1610
+ const result = await execFileAsync(candidate.command, candidate.args, {
1611
+ cwd,
1612
+ encoding: 'utf8',
1613
+ timeout,
1614
+ maxBuffer: REFINE_VALIDATION_OUTPUT_LIMIT_BYTES,
1615
+ env: { ...process.env, CI: process.env.CI || '1', ...(candidate.env || {}) },
646
1616
  });
1617
+ summary.commandsRun.push(commandRecord(candidate, cwd, startedAt, result, true, { exitCode: 0 }));
1618
+ } catch (error: any) {
1619
+ const stderr = truncateValidationOutput(error?.stderr || error?.message);
1620
+ const missingDependencyFailure = /Cannot find module|MODULE_NOT_FOUND|node_modules|command not found|not found/i.test(stderr);
1621
+ summary.commandsRun.push(commandRecord(candidate, cwd, startedAt, error, false, {
1622
+ exitCode: typeof error?.code === 'number' ? error.code : null,
1623
+ signal: typeof error?.signal === 'string' ? error.signal : null,
1624
+ timedOut: error?.killed === true || /timed out/i.test(String(error?.message || '')),
1625
+ ...(missingDependencyFailure ? { failureKind: 'missing_dependencies' } : {}),
1626
+ }));
647
1627
  summary.status = 'failed';
1628
+ if (missingDependencyFailure) {
1629
+ summary.failureKind = 'missing_dependencies';
1630
+ summary.failureCode = 'missing_dependencies';
1631
+ }
648
1632
  return summary;
649
1633
  }
650
1634
  }
@@ -776,6 +1760,8 @@ export interface CommandRouterDeps {
776
1760
  sessionHostControl?: SessionHostControlPlane | null;
777
1761
  /** Selected-coordinator mesh peer telemetry surface for target daemons, when supported by the runtime. */
778
1762
  getMeshPeerConnectionStatus?: (daemonId: string) => Record<string, unknown> | null;
1763
+ /** Dispatch a command to a remote mesh node via P2P/relay. Injected by cloud runtime; absent in standalone. */
1764
+ dispatchMeshCommand?: (daemonId: string, cmd: string, args: Record<string, unknown>) => Promise<unknown>;
779
1765
  }
780
1766
 
781
1767
  export interface CommandRouterResult {
@@ -887,42 +1873,266 @@ function summarizeSessionHostPruneResult(result: unknown): Record<string, unknow
887
1873
  };
888
1874
  }
889
1875
 
1876
+ function normalizeStandaloneHostCommandUrl(hostAddress: string): string {
1877
+ const raw = hostAddress.trim();
1878
+ if (!raw) throw new Error('hostAddress required');
1879
+ const url = new URL(raw.replace(/^ws:/, 'http:').replace(/^wss:/, 'https:'));
1880
+ url.pathname = '/api/v1/command';
1881
+ url.search = '';
1882
+ url.hash = '';
1883
+ return url.toString();
1884
+ }
1885
+
1886
+ function buildMemberJoinNode(mesh: any, args: any, fallbackDaemonId?: string): Record<string, unknown> | null {
1887
+ const requestedNodeId = typeof args?.memberNodeId === 'string' ? args.memberNodeId.trim() : '';
1888
+ const explicit = args?.memberNode && typeof args.memberNode === 'object' && !Array.isArray(args.memberNode)
1889
+ ? args.memberNode as Record<string, any>
1890
+ : null;
1891
+ const configured = Array.isArray(mesh?.nodes)
1892
+ ? (requestedNodeId
1893
+ ? mesh.nodes.find((node: any) => node?.id === requestedNodeId || node?.nodeId === requestedNodeId)
1894
+ : mesh.nodes[0])
1895
+ : null;
1896
+ const source = explicit || configured;
1897
+ const workspace = typeof source?.workspace === 'string' && source.workspace.trim()
1898
+ ? source.workspace.trim()
1899
+ : typeof args?.workspace === 'string' && args.workspace.trim()
1900
+ ? args.workspace.trim()
1901
+ : process.cwd();
1902
+ if (!workspace) return null;
1903
+ const nodeId = typeof source?.id === 'string' && source.id.trim()
1904
+ ? source.id.trim()
1905
+ : typeof source?.nodeId === 'string' && source.nodeId.trim()
1906
+ ? source.nodeId.trim()
1907
+ : undefined;
1908
+ return {
1909
+ ...(nodeId ? { id: nodeId } : {}),
1910
+ workspace,
1911
+ ...(typeof source?.repoRoot === 'string' && source.repoRoot.trim() ? { repoRoot: source.repoRoot.trim() } : {}),
1912
+ ...(typeof source?.daemonId === 'string' && source.daemonId.trim() ? { daemonId: source.daemonId.trim() } : fallbackDaemonId ? { daemonId: fallbackDaemonId } : {}),
1913
+ ...(typeof source?.machineId === 'string' && source.machineId.trim() ? { machineId: source.machineId.trim() } : {}),
1914
+ userOverrides: source?.userOverrides && typeof source.userOverrides === 'object' && !Array.isArray(source.userOverrides) ? source.userOverrides : {},
1915
+ policy: source?.policy && typeof source.policy === 'object' && !Array.isArray(source.policy) ? source.policy : {},
1916
+ role: 'member',
1917
+ };
1918
+ }
1919
+
890
1920
  export class DaemonCommandRouter {
891
1921
  private deps: CommandRouterDeps;
892
1922
  /** In-memory cache for cloud-originating meshes passed via inlineMesh.
893
1923
  * Allows the MCP server to query mesh data via get_mesh even when
894
1924
  * the mesh doesn't exist in the local meshes.json file. */
895
1925
  private inlineMeshCache = new Map<string, any>();
1926
+ /** Coordinator-owned whole-mesh aggregate status snapshots. Browser callers read this by default. */
1927
+ private aggregateMeshStatusCache = new Map<string, { builtAt: number; snapshot: any; queueRevision: string }>();
1928
+ /** In-memory async Refinery jobs keyed by meshId:nodeId to reject/return duplicate in-flight requests. */
1929
+ private runningRefineJobs = new Map<string, MeshRefineJobHandle>();
1930
+ /** Terminal async Refinery jobs preserve a clear answer after the worktree node has been removed. */
1931
+ private terminalRefineJobs = new Map<string, MeshRefineTerminalJob>();
896
1932
 
897
1933
  constructor(deps: CommandRouterDeps) {
898
1934
  this.deps = deps;
899
1935
  }
900
1936
 
1937
+ private cloneJsonValue<T>(value: T): T {
1938
+ if (typeof structuredClone === 'function') return structuredClone(value);
1939
+ return JSON.parse(JSON.stringify(value)) as T;
1940
+ }
1941
+
1942
+ private hydrateCachedAggregateMeshStatusFromInline(snapshot: any, mesh: any, options?: { requireDirectPeerTruth?: boolean }): any {
1943
+ if (!mesh || typeof mesh !== 'object' || !Array.isArray(mesh.nodes) || !Array.isArray(snapshot?.nodes)) return snapshot;
1944
+ const inlineNodesById = new Map<string, any>();
1945
+ for (const node of mesh.nodes) {
1946
+ const nodeId = readInlineMeshNodeId(node);
1947
+ if (nodeId) inlineNodesById.set(nodeId, node);
1948
+ }
1949
+ if (!inlineNodesById.size) return snapshot;
1950
+
1951
+ let changed = false;
1952
+ const unavailableNodeIds = new Set<string>();
1953
+ const sourceOfTruth = readObjectRecord(snapshot.sourceOfTruth);
1954
+ const directPeerTruth = readObjectRecord(sourceOfTruth.directPeerTruth);
1955
+ for (const entry of Array.isArray(directPeerTruth.unavailableNodeIds) ? directPeerTruth.unavailableNodeIds : []) {
1956
+ const nodeId = readStringValue(entry);
1957
+ if (nodeId) unavailableNodeIds.add(nodeId);
1958
+ }
1959
+
1960
+ const nodes = snapshot.nodes.map((statusNode: any) => {
1961
+ const nodeId = readStringValue(statusNode?.nodeId, statusNode?.id);
1962
+ const inlineNode = nodeId ? inlineNodesById.get(nodeId) : undefined;
1963
+ if (!inlineNode) return statusNode;
1964
+ const liveGit = buildInlineMeshTransitGitStatus(inlineNode);
1965
+ if (!liveGit) return statusNode;
1966
+ const nextStatus = { ...statusNode };
1967
+ nextStatus.git = liveGit;
1968
+ nextStatus.health = deriveMeshNodeHealthFromGit(liveGit);
1969
+ applyInlineMeshBranchConvergence(mesh, inlineNode, nextStatus);
1970
+ nextStatus.launchReady = readBooleanValue(nextStatus.launchReady) ?? true;
1971
+ const connection = readObjectRecord(nextStatus.connection);
1972
+ const connectionState = readStringValue(connection.state);
1973
+ const connectionReported = readBooleanValue(connection.reported) ?? false;
1974
+ if (!connectionReported || connectionState === 'unknown') {
1975
+ nextStatus.connection = buildLivePeerGitConnection(connection);
1976
+ }
1977
+ delete nextStatus.gitProbePending;
1978
+ const error = readStringValue(nextStatus.error);
1979
+ if (error && /pending_git|git probe|live peer git snapshot|no peer git snapshot/i.test(error)) delete nextStatus.error;
1980
+ if (!readStringValue(nextStatus.machineStatus)) nextStatus.machineStatus = 'online';
1981
+ if (nodeId) unavailableNodeIds.delete(nodeId);
1982
+ changed = true;
1983
+ return nextStatus;
1984
+ });
1985
+
1986
+ const aggregateDirectTruthSatisfied = sourceOfTruth.coordinatorOwnsLiveTruth === true
1987
+ || directPeerTruth.satisfied === true;
1988
+ if (!changed && !(options?.requireDirectPeerTruth && unavailableNodeIds.size > 0 && !aggregateDirectTruthSatisfied)) return snapshot;
1989
+ const nextSourceOfTruth = {
1990
+ ...sourceOfTruth,
1991
+ ...(Object.keys(directPeerTruth).length ? {
1992
+ directPeerTruth: {
1993
+ ...directPeerTruth,
1994
+ satisfied: options?.requireDirectPeerTruth === true
1995
+ ? aggregateDirectTruthSatisfied || unavailableNodeIds.size === 0
1996
+ : directPeerTruth.satisfied,
1997
+ unavailableNodeIds: [...unavailableNodeIds],
1998
+ },
1999
+ ...(options?.requireDirectPeerTruth === true ? {
2000
+ coordinatorOwnsLiveTruth: aggregateDirectTruthSatisfied || unavailableNodeIds.size === 0,
2001
+ currentStatus: aggregateDirectTruthSatisfied || unavailableNodeIds.size === 0 ? 'live_git_and_session_probes' : 'direct_peer_truth_unavailable',
2002
+ } : {}),
2003
+ } : {}),
2004
+ };
2005
+ return {
2006
+ ...snapshot,
2007
+ ...(options?.requireDirectPeerTruth === true && unavailableNodeIds.size > 0 && !aggregateDirectTruthSatisfied ? {
2008
+ success: false,
2009
+ code: 'mesh_direct_peer_truth_unavailable',
2010
+ error: 'Selected coordinator could not confirm direct mesh truth for every remote node yet.',
2011
+ } : {}),
2012
+ sourceOfTruth: nextSourceOfTruth,
2013
+ branchConvergenceSummary: summarizeInlineMeshBranchConvergence(nodes),
2014
+ nodes,
2015
+ };
2016
+ }
2017
+
2018
+ private getCachedAggregateMeshStatus(meshId: string, mesh?: any, options?: { requireDirectPeerTruth?: boolean }): any | null {
2019
+ const cached = this.aggregateMeshStatusCache.get(meshId);
2020
+ if (!cached?.snapshot || cached.snapshot.success !== true || !Array.isArray(cached.snapshot.nodes)) return null;
2021
+ if (cached.queueRevision !== getMeshQueueRevision(meshId)) return null;
2022
+ let snapshot = this.cloneJsonValue(cached.snapshot);
2023
+ snapshot = this.hydrateCachedAggregateMeshStatusFromInline(snapshot, mesh, options);
2024
+ if (shouldRefreshStalePendingAggregate(snapshot, options)) return null;
2025
+ const ageMs = Math.max(0, Date.now() - cached.builtAt);
2026
+ const sourceOfTruth = snapshot.sourceOfTruth && typeof snapshot.sourceOfTruth === 'object'
2027
+ ? snapshot.sourceOfTruth
2028
+ : {};
2029
+ snapshot.sourceOfTruth = {
2030
+ ...sourceOfTruth,
2031
+ aggregateSnapshot: {
2032
+ ...(sourceOfTruth.aggregateSnapshot && typeof sourceOfTruth.aggregateSnapshot === 'object'
2033
+ ? sourceOfTruth.aggregateSnapshot
2034
+ : {}),
2035
+ owner: 'coordinator_daemon_memory',
2036
+ cached: true,
2037
+ source: 'memory',
2038
+ refreshReason: 'memory_cache_hit',
2039
+ ageMs,
2040
+ cachedAt: new Date(cached.builtAt).toISOString(),
2041
+ returnedAt: new Date().toISOString(),
2042
+ },
2043
+ };
2044
+ return snapshot;
2045
+ }
2046
+
2047
+ private rememberAggregateMeshStatus(meshId: string, snapshot: any, refreshReason: string): any {
2048
+ if (!snapshot || typeof snapshot !== 'object' || snapshot.success !== true || !Array.isArray(snapshot.nodes)) return snapshot;
2049
+ const builtAt = Date.now();
2050
+ const next = this.cloneJsonValue(snapshot);
2051
+ const sourceOfTruth = next.sourceOfTruth && typeof next.sourceOfTruth === 'object'
2052
+ ? next.sourceOfTruth
2053
+ : {};
2054
+ next.sourceOfTruth = {
2055
+ ...sourceOfTruth,
2056
+ aggregateSnapshot: {
2057
+ owner: 'coordinator_daemon_memory',
2058
+ cached: false,
2059
+ source: 'live_refresh',
2060
+ refreshReason,
2061
+ ageMs: 0,
2062
+ cachedAt: new Date(builtAt).toISOString(),
2063
+ returnedAt: new Date(builtAt).toISOString(),
2064
+ },
2065
+ };
2066
+ this.aggregateMeshStatusCache.set(meshId, { builtAt, snapshot: this.cloneJsonValue(next), queueRevision: getMeshQueueRevision(meshId) });
2067
+ return next;
2068
+ }
2069
+
901
2070
  public getCachedInlineMesh(meshId: string, inlineMesh?: unknown): any | undefined {
902
2071
  if (inlineMesh && typeof inlineMesh === 'object') {
903
- this.inlineMeshCache.set(meshId, inlineMesh as any);
904
- return inlineMesh as any;
2072
+ return this.warmInlineMeshCache(meshId, inlineMesh);
905
2073
  }
906
2074
  return this.inlineMeshCache.get(meshId);
907
2075
  }
908
2076
 
2077
+ private warmInlineMeshCache(meshId: string, inlineMesh?: unknown): any | undefined {
2078
+ if (!inlineMesh || typeof inlineMesh !== 'object') return undefined;
2079
+ const sanitizedInlineMesh = sanitizeInlineMesh(inlineMesh as any);
2080
+ const cached = this.inlineMeshCache.get(meshId);
2081
+ if (cached) {
2082
+ const merged = reconcileInlineMeshCache(cached, sanitizedInlineMesh);
2083
+ this.inlineMeshCache.set(meshId, merged);
2084
+ return merged;
2085
+ }
2086
+ this.inlineMeshCache.set(meshId, sanitizedInlineMesh as any);
2087
+ return sanitizedInlineMesh as any;
2088
+ }
2089
+
909
2090
  private async getMeshForCommand(
910
2091
  meshId: string,
911
2092
  inlineMesh?: unknown,
912
2093
  options?: { preferInline?: boolean },
913
- ): Promise<{ mesh: any; inline: boolean } | null> {
2094
+ ): Promise<{ mesh: any; inline: boolean; source: 'inline_cache' | 'inline_bootstrap' | 'local_config' } | null> {
914
2095
  const preferInline = options?.preferInline === true;
915
2096
  if (preferInline) {
916
- const cached = this.getCachedInlineMesh(meshId, inlineMesh);
917
- if (cached) return { mesh: cached, inline: true };
2097
+ const cached = this.getCachedInlineMesh(meshId);
2098
+ if (cached) {
2099
+ if (inlineMeshCarriesTransientNodeTruth(inlineMesh)) {
2100
+ const merged = reconcileInlineMeshCache(cached, inlineMesh as any);
2101
+ this.inlineMeshCache.set(meshId, sanitizeInlineMesh(merged));
2102
+ return { mesh: merged, inline: true, source: 'inline_cache' };
2103
+ }
2104
+ return { mesh: cached, inline: true, source: 'inline_cache' };
2105
+ }
2106
+ if (inlineMeshCarriesTransientNodeTruth(inlineMesh)) {
2107
+ this.warmInlineMeshCache(meshId, inlineMesh);
2108
+ return { mesh: inlineMesh, inline: true, source: 'inline_bootstrap' };
2109
+ }
918
2110
  }
919
2111
  try {
920
2112
  const { getMesh } = await import('../config/mesh-config.js');
921
2113
  const mesh = getMesh(meshId);
922
- if (mesh) return { mesh, inline: false };
2114
+ if (mesh) return { mesh, inline: false, source: 'local_config' };
923
2115
  } catch { /* fall through to inline cache */ }
924
- const cached = this.getCachedInlineMesh(meshId, inlineMesh);
925
- return cached ? { mesh: cached, inline: true } : null;
2116
+ const cached = this.getCachedInlineMesh(meshId);
2117
+ if (cached) return { mesh: cached, inline: true, source: 'inline_cache' };
2118
+ const warmedInline = this.warmInlineMeshCache(meshId, inlineMesh);
2119
+ return warmedInline ? { mesh: warmedInline, inline: true, source: 'inline_bootstrap' } : null;
2120
+ }
2121
+
2122
+ private invalidateAggregateMeshStatus(meshId: string): void {
2123
+ this.aggregateMeshStatusCache.delete(meshId);
2124
+ }
2125
+
2126
+
2127
+ private async requireMeshHostMutationOwner(meshId: string, inlineMesh: unknown, operation: string): Promise<CommandRouterResult | null> {
2128
+ const meshRecord = await this.getMeshForCommand(meshId, inlineMesh, { preferInline: true });
2129
+ const mesh = meshRecord?.mesh;
2130
+ if (!mesh) return { success: false, error: 'Mesh not found' };
2131
+ const meshHost = resolveMeshHostStatus(mesh);
2132
+ if (!meshHost.canOwnCoordinator || !meshHost.canOwnQueue) {
2133
+ return { ...buildMeshHostRequiredFailure(mesh, operation), success: false, meshId };
2134
+ }
2135
+ return null;
926
2136
  }
927
2137
 
928
2138
  private updateInlineMeshNode(meshId: string, mesh: any, node: any): void {
@@ -932,6 +2142,7 @@ export class DaemonCommandRouter {
932
2142
  else mesh.nodes.push(node);
933
2143
  mesh.updatedAt = new Date().toISOString();
934
2144
  this.inlineMeshCache.set(meshId, mesh);
2145
+ this.invalidateAggregateMeshStatus(meshId);
935
2146
  }
936
2147
 
937
2148
  private removeInlineMeshNode(meshId: string, mesh: any, nodeId: string): boolean {
@@ -941,6 +2152,7 @@ export class DaemonCommandRouter {
941
2152
  mesh.nodes.splice(idx, 1);
942
2153
  mesh.updatedAt = new Date().toISOString();
943
2154
  this.inlineMeshCache.set(meshId, mesh);
2155
+ this.invalidateAggregateMeshStatus(meshId);
944
2156
  return true;
945
2157
  }
946
2158
 
@@ -1219,6 +2431,7 @@ export class DaemonCommandRouter {
1219
2431
  const deletedSessionIds: string[] = [];
1220
2432
  const skippedSessionIds: string[] = [];
1221
2433
  const skippedLiveSessionIds: string[] = [];
2434
+ const skippedCoordinatorSessionIds: string[] = [];
1222
2435
  const deleteUnsupportedSessionIds: string[] = [];
1223
2436
  const recordsRemainSessionIds: string[] = [];
1224
2437
  const errors: Array<{ sessionId: string; error: string }> = [];
@@ -1253,6 +2466,12 @@ export class DaemonCommandRouter {
1253
2466
  const completed = this.isCompletedHostedSession(record);
1254
2467
  const surfaceKind = getSessionHostSurfaceKind(record);
1255
2468
  const liveRuntime = surfaceKind === 'live_runtime';
2469
+ const coordinatorSession = readStringValue(record?.meta?.meshCoordinatorFor) === args.meshId;
2470
+ if (!hasExplicitSessionIds && coordinatorSession) {
2471
+ skippedSessionIds.push(sessionId);
2472
+ skippedCoordinatorSessionIds.push(sessionId);
2473
+ continue;
2474
+ }
1256
2475
  if (!hasExplicitSessionIds && liveRuntime) {
1257
2476
  skippedSessionIds.push(sessionId);
1258
2477
  skippedLiveSessionIds.push(sessionId);
@@ -1322,6 +2541,7 @@ export class DaemonCommandRouter {
1322
2541
  deletedSessionIds,
1323
2542
  skippedSessionIds,
1324
2543
  skippedLiveSessionIds,
2544
+ skippedCoordinatorSessionIds,
1325
2545
  ...(deleteUnsupported ? {
1326
2546
  deleteUnsupported: true,
1327
2547
  effectiveCleanup: args.mode === 'stop_and_delete'
@@ -1429,39 +2649,550 @@ export class DaemonCommandRouter {
1429
2649
  level: daemonResult.success ? 'info' : 'warn',
1430
2650
  payload: { cmd, source: logSource, success: daemonResult.success, durationMs: Date.now() - cmdStart },
1431
2651
  });
1432
- return daemonResult;
2652
+ return daemonResult;
2653
+ }
2654
+
2655
+ // 2. Delegate to DaemonCommandHandler
2656
+ const handlerResult = await this.deps.commandHandler.handle(cmd, normalizedArgs);
2657
+ logCommand({ ts: new Date().toISOString(), cmd, source: logSource, interactionId, args: normalizedArgs, success: handlerResult.success, durationMs: Date.now() - cmdStart });
2658
+ recordDebugTrace({
2659
+ interactionId,
2660
+ category: 'command',
2661
+ stage: 'completed',
2662
+ level: handlerResult.success ? 'info' : 'warn',
2663
+ payload: { cmd, source: logSource, success: handlerResult.success, durationMs: Date.now() - cmdStart },
2664
+ });
2665
+
2666
+ // 3. Post-chat command callback
2667
+ if (CHAT_COMMANDS.includes(cmd) && this.deps.onPostChatCommand) {
2668
+ this.deps.onPostChatCommand();
2669
+ }
2670
+
2671
+ return handlerResult;
2672
+ } catch (e: any) {
2673
+ logCommand({ ts: new Date().toISOString(), cmd, source: logSource, interactionId, args: normalizedArgs, success: false, error: e.message, durationMs: Date.now() - cmdStart });
2674
+ recordDebugTrace({
2675
+ interactionId,
2676
+ category: 'command',
2677
+ stage: 'failed',
2678
+ level: 'error',
2679
+ payload: { cmd, source: logSource, error: e?.message || String(e), durationMs: Date.now() - cmdStart },
2680
+ });
2681
+ throw e;
2682
+ }
2683
+ }
2684
+
2685
+
2686
+ private buildRefineJobKey(meshId: string, nodeId: string): string {
2687
+ return `${meshId}:${nodeId}`;
2688
+ }
2689
+
2690
+ private buildRefineJobHandle(args: {
2691
+ meshId: string;
2692
+ nodeId: string;
2693
+ node?: any;
2694
+ status?: MeshRefineAsyncJobStatus;
2695
+ startedAt?: string;
2696
+ completedAt?: string;
2697
+ jobId?: string;
2698
+ interactionId?: string;
2699
+ retryOfJobId?: string;
2700
+ }): MeshRefineJobHandle {
2701
+ return {
2702
+ success: true,
2703
+ async: true,
2704
+ status: args.status || 'accepted',
2705
+ jobId: args.jobId || `refine_${createInteractionId()}`,
2706
+ interactionId: args.interactionId || createInteractionId(),
2707
+ meshId: args.meshId,
2708
+ nodeId: args.nodeId,
2709
+ targetNodeId: args.nodeId,
2710
+ targetDaemonId: readStringValue(args.node?.daemonId),
2711
+ workspace: readStringValue(args.node?.workspace),
2712
+ startedAt: args.startedAt || new Date().toISOString(),
2713
+ ...(args.completedAt ? { completedAt: args.completedAt } : {}),
2714
+ ...(args.retryOfJobId ? { retryOfJobId: args.retryOfJobId } : {}),
2715
+ eventDelivery: { pendingEvents: true, ledger: true },
2716
+ evidence: {
2717
+ pendingEventsCommand: 'get_pending_mesh_events',
2718
+ ledgerCommand: 'get_mesh_ledger_slice',
2719
+ taskHistoryKind: args.status === 'completed' ? 'task_completed' : args.status === 'failed' ? 'task_failed' : 'task_dispatched',
2720
+ },
2721
+ };
2722
+ }
2723
+
2724
+ private queueRefineJobEvent(event: 'refine:accepted' | 'refine:completed' | 'refine:failed', handle: MeshRefineJobHandle, result?: Record<string, unknown>): void {
2725
+ const metadataEvent = {
2726
+ source: 'refine_mesh_node_async_job',
2727
+ jobId: handle.jobId,
2728
+ interactionId: handle.interactionId,
2729
+ meshId: handle.meshId,
2730
+ nodeId: handle.targetNodeId,
2731
+ targetDaemonId: handle.targetDaemonId,
2732
+ workspace: handle.workspace,
2733
+ status: handle.status,
2734
+ startedAt: handle.startedAt,
2735
+ completedAt: handle.completedAt,
2736
+ retryOfJobId: handle.retryOfJobId,
2737
+ ...(result ? { result } : {}),
2738
+ };
2739
+ const eventPayload = {
2740
+ event,
2741
+ meshId: handle.meshId,
2742
+ nodeLabel: handle.targetNodeId,
2743
+ nodeId: handle.targetNodeId,
2744
+ workspace: handle.workspace,
2745
+ metadataEvent,
2746
+ queuedAt: Date.now(),
2747
+ };
2748
+ if (typeof this.deps.instanceManager?.getByCategory === 'function') {
2749
+ const forwarded = handleMeshForwardEvent(
2750
+ { instanceManager: this.deps.instanceManager } as any,
2751
+ {
2752
+ event,
2753
+ meshId: handle.meshId,
2754
+ nodeId: handle.targetNodeId,
2755
+ workspace: handle.workspace,
2756
+ jobId: handle.jobId,
2757
+ interactionId: handle.interactionId,
2758
+ status: handle.status,
2759
+ targetDaemonId: handle.targetDaemonId,
2760
+ startedAt: handle.startedAt,
2761
+ completedAt: handle.completedAt,
2762
+ retryOfJobId: handle.retryOfJobId,
2763
+ ...(result ? { result } : {}),
2764
+ },
2765
+ );
2766
+ if (forwarded?.success === true) return;
2767
+ LOG.warn('Mesh', `[Refinery] Failed to forward async refine event ${event}: ${forwarded?.error || 'unknown error'}`);
2768
+ }
2769
+ queuePendingMeshCoordinatorEvent(eventPayload);
2770
+ }
2771
+
2772
+ private async appendRefineJobLedger(kind: 'task_dispatched' | 'task_completed' | 'task_failed', handle: MeshRefineJobHandle, result?: Record<string, unknown>): Promise<void> {
2773
+ try {
2774
+ const { appendLedgerEntry } = await import('../mesh/mesh-ledger.js');
2775
+ appendLedgerEntry(handle.meshId, {
2776
+ kind,
2777
+ nodeId: handle.targetNodeId,
2778
+ payload: {
2779
+ source: 'refine_mesh_node_async_job',
2780
+ refineJob: {
2781
+ jobId: handle.jobId,
2782
+ interactionId: handle.interactionId,
2783
+ status: handle.status,
2784
+ meshId: handle.meshId,
2785
+ nodeId: handle.targetNodeId,
2786
+ targetDaemonId: handle.targetDaemonId,
2787
+ workspace: handle.workspace,
2788
+ startedAt: handle.startedAt,
2789
+ completedAt: handle.completedAt,
2790
+ retryOfJobId: handle.retryOfJobId,
2791
+ },
2792
+ async: true,
2793
+ retryOfJobId: handle.retryOfJobId,
2794
+ ...(result ? {
2795
+ success: result.success === true,
2796
+ result,
2797
+ finalBranchConvergenceState: result.finalBranchConvergenceState,
2798
+ } : {}),
2799
+ },
2800
+ });
2801
+ } catch (e: any) {
2802
+ LOG.warn('Mesh', `[Refinery] Failed to append async refine ledger entry: ${e?.message || e}`);
2803
+ }
2804
+ }
2805
+
2806
+ private async executeMeshRefineNodeSynchronously(meshId: string, nodeId: string, args: any): Promise<CommandRouterResult> {
2807
+ const refineStages: Array<Record<string, unknown>> = [];
2808
+ try {
2809
+ const meshRecord = await this.getMeshForCommand(meshId, args?.inlineMesh);
2810
+ const mesh = meshRecord?.mesh;
2811
+ const node = mesh?.nodes?.find((n: any) => n.id === nodeId || n.nodeId === nodeId);
2812
+ if (!node) return { success: false, error: `Node '${nodeId}' not found in mesh`, refineStages };
2813
+
2814
+ if (!node.isLocalWorktree || !node.workspace) {
2815
+ return { success: false, error: `Refinery requires a local worktree node`, refineStages };
2816
+ }
2817
+
2818
+ const sourceNode = node.clonedFromNodeId
2819
+ ? mesh?.nodes.find((n: any) => n.id === node.clonedFromNodeId || n.nodeId === node.clonedFromNodeId)
2820
+ : mesh?.nodes.find((n: any) => !n.isLocalWorktree);
2821
+ const repoRoot = sourceNode?.repoRoot || sourceNode?.workspace;
2822
+ if (!repoRoot) return { success: false, error: 'Source node repoRoot not found', refineStages };
2823
+
2824
+ const { execFile } = await import('node:child_process');
2825
+ const { promisify } = await import('node:util');
2826
+ const execFileAsync = promisify(execFile);
2827
+
2828
+ const resolveStarted = Date.now();
2829
+ const { stdout: branchStdout } = await execFileAsync('git', ['branch', '--show-current'], { cwd: node.workspace, encoding: 'utf8' });
2830
+ const branch = branchStdout.trim();
2831
+ if (!branch) return { success: false, error: 'Could not determine branch of the worktree node', refineStages };
2832
+
2833
+ const { stdout: baseBranchStdout } = await execFileAsync('git', ['branch', '--show-current'], { cwd: repoRoot, encoding: 'utf8' });
2834
+ const baseBranch = baseBranchStdout.trim();
2835
+ const { stdout: baseHeadStdout } = await execFileAsync('git', ['rev-parse', 'HEAD'], { cwd: repoRoot, encoding: 'utf8' });
2836
+ const { stdout: branchHeadStdout } = await execFileAsync('git', ['rev-parse', branch], { cwd: node.workspace, encoding: 'utf8' });
2837
+ const baseHead = baseHeadStdout.trim();
2838
+ const branchHead = branchHeadStdout.trim();
2839
+ recordMeshRefineStage(refineStages, 'resolve_refs', 'passed', resolveStarted, { branch, baseBranch, baseHead, branchHead });
2840
+
2841
+ const validationStarted = Date.now();
2842
+ const validationSummary = await runMeshRefineValidationGate(mesh, node.workspace);
2843
+ recordMeshRefineStage(
2844
+ refineStages,
2845
+ 'validation',
2846
+ validationSummary.status === 'passed' ? 'passed' : validationSummary.status === 'failed' ? 'failed' : 'skipped',
2847
+ validationStarted,
2848
+ { validationStatus: validationSummary.status, commandsRun: validationSummary.commandsRun.length },
2849
+ );
2850
+ if (validationSummary.status === 'failed') {
2851
+ return {
2852
+ success: false,
2853
+ code: validationSummary.failureCode || 'validation_failed',
2854
+ convergenceStatus: 'blocked_review',
2855
+ error: validationSummary.failureCode === 'missing_dependencies'
2856
+ ? 'Refinery validation dependencies are missing; merge/refine was not attempted. Configure validation.bootstrapCommands if Refinery should bootstrap dependencies before validation.'
2857
+ : validationSummary.failureCode === 'dependency_bootstrap_failed'
2858
+ ? 'Refinery dependency/bootstrap command failed; merge/refine was not attempted.'
2859
+ : 'Refinery validation gate failed; merge/refine was not attempted.',
2860
+ branch,
2861
+ into: baseBranch,
2862
+ validationSummary,
2863
+ refineStages,
2864
+ finalBranchConvergenceState: {
2865
+ branch,
2866
+ baseBranch,
2867
+ merged: false,
2868
+ removed: false,
2869
+ validation: 'failed',
2870
+ status: 'blocked_review',
2871
+ },
2872
+ };
2873
+ }
2874
+ if (validationSummary.status === 'skipped') {
2875
+ return {
2876
+ success: false,
2877
+ code: 'validation_unavailable',
2878
+ convergenceStatus: 'blocked_review',
2879
+ error: 'Refinery validation gate is required but no allowlisted validation command was available; merge/refine was not attempted.',
2880
+ branch,
2881
+ into: baseBranch,
2882
+ validationSummary,
2883
+ refineStages,
2884
+ finalBranchConvergenceState: {
2885
+ branch,
2886
+ baseBranch,
2887
+ merged: false,
2888
+ removed: false,
2889
+ validation: 'unavailable',
2890
+ status: 'blocked_review',
2891
+ },
2892
+ };
2893
+ }
2894
+
2895
+ const patchEquivalenceStarted = Date.now();
2896
+ const patchEquivalence = await runMeshRefinePatchEquivalenceGate(repoRoot, baseHead, branchHead);
2897
+ recordMeshRefineStage(refineStages, 'patch_equivalence', patchEquivalence.status, patchEquivalenceStarted, {
2898
+ equivalent: patchEquivalence.equivalent,
2899
+ expectedPatchId: patchEquivalence.expectedPatchId,
2900
+ actualPatchId: patchEquivalence.actualPatchId,
2901
+ error: patchEquivalence.error,
2902
+ });
2903
+ if (!patchEquivalence.equivalent) {
2904
+ return {
2905
+ success: false,
2906
+ code: 'patch_equivalence_failed',
2907
+ convergenceStatus: 'blocked_review',
2908
+ error: 'Refinery patch-equivalence preflight failed; merge/refine was not attempted.',
2909
+ branch,
2910
+ into: baseBranch,
2911
+ validationSummary,
2912
+ patchEquivalence,
2913
+ refineStages,
2914
+ finalBranchConvergenceState: {
2915
+ branch,
2916
+ baseBranch,
2917
+ merged: false,
2918
+ removed: false,
2919
+ validation: 'passed',
2920
+ patchEquivalence: 'failed',
2921
+ status: 'blocked_review',
2922
+ },
2923
+ };
2924
+ }
2925
+
2926
+ const submoduleReachabilityStarted = Date.now();
2927
+ const autoPublishSubmoduleMainCommits = resolveRefineryAutoPublishSubmoduleMainCommits(mesh, node.workspace);
2928
+ const submoduleReachability = await runMeshRefineSubmoduleReachabilityGate(repoRoot, patchEquivalence.mergedTree || branchHead, {
2929
+ allowAutoPublishSubmoduleMainCommits: autoPublishSubmoduleMainCommits.enabled,
2930
+ autoPublishPolicySource: autoPublishSubmoduleMainCommits.source,
2931
+ worktreeRoot: node.workspace,
2932
+ });
2933
+ recordMeshRefineStage(refineStages, 'submodule_reachability', submoduleReachability.status, submoduleReachabilityStarted, {
2934
+ checked: submoduleReachability.checked,
2935
+ autoPublishAllowed: submoduleReachability.autoPublishAllowed,
2936
+ autoPublishPolicySource: submoduleReachability.autoPublishPolicySource,
2937
+ autoPublished: submoduleReachability.entries
2938
+ .filter(entry => entry.autoPublishAttempted)
2939
+ .map(entry => ({
2940
+ path: entry.path,
2941
+ commit: entry.commit,
2942
+ remote: entry.remote,
2943
+ remoteUrl: entry.remoteUrl,
2944
+ remoteMainBranch: entry.remoteMainBranch,
2945
+ refspec: entry.autoPublishRefspec,
2946
+ succeeded: entry.autoPublishSucceeded,
2947
+ verified: entry.autoPublishVerified,
2948
+ remoteMainReachable: entry.remoteMainReachable,
2949
+ error: entry.error,
2950
+ })),
2951
+ autoPublishSkipped: submoduleReachability.entries
2952
+ .filter(entry => entry.autoPublishAllowed === true && entry.autoPublishAttempted !== true)
2953
+ .map(entry => ({
2954
+ path: entry.path,
2955
+ commit: entry.commit,
2956
+ remote: entry.remote,
2957
+ remoteUrl: entry.remoteUrl,
2958
+ remoteMainBranch: entry.remoteMainBranch,
2959
+ reason: entry.autoPublishSkippedReason || entry.error || 'auto-publish was allowed but no publish attempt was possible',
2960
+ })),
2961
+ unreachable: submoduleReachability.unreachable.map(entry => ({
2962
+ path: entry.path,
2963
+ commit: entry.commit,
2964
+ publishRequired: entry.publishRequired === true,
2965
+ autoPublishAllowed: entry.autoPublishAllowed,
2966
+ autoPublishAttempted: entry.autoPublishAttempted,
2967
+ autoPublishSucceeded: entry.autoPublishSucceeded,
2968
+ autoPublishVerified: entry.autoPublishVerified,
2969
+ autoPublishRefspec: entry.autoPublishRefspec,
2970
+ autoPublishSkippedReason: entry.autoPublishSkippedReason,
2971
+ remote: entry.remote,
2972
+ remoteUrl: entry.remoteUrl,
2973
+ remoteReachable: entry.remoteReachable,
2974
+ remoteMainBranch: entry.remoteMainBranch,
2975
+ remoteMainReachable: entry.remoteMainReachable,
2976
+ error: entry.error,
2977
+ })),
2978
+ error: submoduleReachability.error,
2979
+ });
2980
+ if (submoduleReachability.status === 'failed') {
2981
+ const nextStep = buildSubmodulePublishRequiredNextStep(submoduleReachability.unreachable);
2982
+ return {
2983
+ success: false,
2984
+ code: 'submodule_reachability_failed',
2985
+ convergenceStatus: 'blocked_review',
2986
+ publishRequired: true,
2987
+ blockedReason: 'submodule_publish_required',
2988
+ error: 'Refinery submodule reachability preflight failed because one or more submodule gitlink commits are not reachable from their configured remote main branch; merge/refine cleanup was not attempted.',
2989
+ nextStep,
2990
+ nextSteps: [
2991
+ 'Ask the user for explicit approval before pushing or publishing any submodule commit.',
2992
+ 'Push/publish each unreachable submodule commit to the configured submodule remote main branch shown in the evidence.',
2993
+ 'Rerun mesh_refine_node after remote reachability is confirmed.',
2994
+ 'Do not merge the root branch until every submodule gitlink commit is reachable from submodule origin/main.',
2995
+ ],
2996
+ unreachableSubmoduleCommits: submoduleReachability.unreachable.map(entry => ({
2997
+ path: entry.path,
2998
+ commit: entry.commit,
2999
+ remote: entry.remote,
3000
+ remoteUrl: entry.remoteUrl,
3001
+ remoteReachable: entry.remoteReachable,
3002
+ remoteMainBranch: entry.remoteMainBranch,
3003
+ remoteMainReachable: entry.remoteMainReachable,
3004
+ autoPublishAllowed: entry.autoPublishAllowed,
3005
+ autoPublishAttempted: entry.autoPublishAttempted,
3006
+ autoPublishSucceeded: entry.autoPublishSucceeded,
3007
+ autoPublishVerified: entry.autoPublishVerified,
3008
+ autoPublishRefspec: entry.autoPublishRefspec,
3009
+ autoPublishSkippedReason: entry.autoPublishSkippedReason,
3010
+ error: entry.error,
3011
+ })),
3012
+ branch,
3013
+ into: baseBranch,
3014
+ validationSummary,
3015
+ patchEquivalence,
3016
+ submoduleReachability,
3017
+ refineStages,
3018
+ finalBranchConvergenceState: {
3019
+ branch,
3020
+ baseBranch,
3021
+ merged: false,
3022
+ removed: false,
3023
+ validation: 'passed',
3024
+ patchEquivalence: 'passed',
3025
+ submoduleReachability: 'failed',
3026
+ status: 'blocked_review',
3027
+ reason: 'submodule_publish_required',
3028
+ nextStep,
3029
+ },
3030
+ };
3031
+ }
3032
+
3033
+ let mergeResult: Record<string, unknown> | undefined;
3034
+ const mergeStarted = Date.now();
3035
+ try {
3036
+ const result = await execFileAsync('git', ['merge', '--no-ff', branch, '-m', `Auto-merge branch '${branch}' via Refinery`], { cwd: repoRoot, encoding: 'utf8' });
3037
+ mergeResult = {
3038
+ stdout: truncateValidationOutput(result.stdout),
3039
+ stderr: truncateValidationOutput(result.stderr),
3040
+ durationMs: Date.now() - mergeStarted,
3041
+ };
3042
+ recordMeshRefineStage(refineStages, 'merge', 'passed', mergeStarted, mergeResult);
3043
+ } catch (e: any) {
3044
+ recordMeshRefineStage(refineStages, 'merge', 'failed', mergeStarted, {
3045
+ error: e?.message || String(e),
3046
+ stdout: truncateValidationOutput(e?.stdout),
3047
+ stderr: truncateValidationOutput(e?.stderr),
3048
+ });
3049
+ return {
3050
+ success: false,
3051
+ error: `Merge failed (conflicts?): ${e.message}`,
3052
+ validationSummary,
3053
+ patchEquivalence,
3054
+ refineStages,
3055
+ finalBranchConvergenceState: {
3056
+ branch,
3057
+ baseBranch,
3058
+ merged: false,
3059
+ removed: false,
3060
+ validation: 'passed',
3061
+ patchEquivalence: 'passed',
3062
+ status: 'not_mergeable',
3063
+ },
3064
+ };
3065
+ }
3066
+
3067
+ const cleanupStarted = Date.now();
3068
+ const removeResult = await this.execute('remove_mesh_node', {
3069
+ meshId,
3070
+ nodeId,
3071
+ sessionCleanupMode: 'preserve',
3072
+ inlineMesh: args?.inlineMesh,
3073
+ });
3074
+ recordMeshRefineStage(refineStages, 'cleanup', removeResult?.success === false ? 'failed' : 'passed', cleanupStarted, {
3075
+ removed: removeResult?.removed,
3076
+ code: removeResult?.code,
3077
+ error: removeResult?.error,
3078
+ });
3079
+
3080
+ let ledgerError: string | undefined;
3081
+ const ledgerStarted = Date.now();
3082
+ try {
3083
+ const { appendLedgerEntry } = await import('../mesh/mesh-ledger.js');
3084
+ appendLedgerEntry(meshId, {
3085
+ kind: 'node_removed',
3086
+ nodeId,
3087
+ payload: { refined: true, mergedBranch: branch, into: baseBranch, validationSummary, patchEquivalence, submoduleReachability },
3088
+ });
3089
+ recordMeshRefineStage(refineStages, 'ledger', 'passed', ledgerStarted);
3090
+ } catch (e: any) {
3091
+ ledgerError = e?.message || String(e);
3092
+ recordMeshRefineStage(refineStages, 'ledger', 'failed', ledgerStarted, { error: ledgerError });
1433
3093
  }
1434
3094
 
1435
- // 2. Delegate to DaemonCommandHandler
1436
- const handlerResult = await this.deps.commandHandler.handle(cmd, normalizedArgs);
1437
- logCommand({ ts: new Date().toISOString(), cmd, source: logSource, interactionId, args: normalizedArgs, success: handlerResult.success, durationMs: Date.now() - cmdStart });
1438
- recordDebugTrace({
1439
- interactionId,
1440
- category: 'command',
1441
- stage: 'completed',
1442
- level: handlerResult.success ? 'info' : 'warn',
1443
- payload: { cmd, source: logSource, success: handlerResult.success, durationMs: Date.now() - cmdStart },
1444
- });
3095
+ const finalBranchConvergenceState = {
3096
+ branch: baseBranch,
3097
+ mergedBranch: branch,
3098
+ baseBranch,
3099
+ merged: true,
3100
+ removed: removeResult?.success !== false,
3101
+ validation: 'passed',
3102
+ patchEquivalence: 'passed',
3103
+ status: removeResult?.success === false ? 'merged_cleanup_failed' : 'merged',
3104
+ };
1445
3105
 
1446
- // 3. Post-chat command callback
1447
- if (CHAT_COMMANDS.includes(cmd) && this.deps.onPostChatCommand) {
1448
- this.deps.onPostChatCommand();
3106
+ if (removeResult?.success === false) {
3107
+ return {
3108
+ success: false,
3109
+ code: 'cleanup_failed',
3110
+ error: 'Refinery merge completed but worktree cleanup failed; manual cleanup/retry is required.',
3111
+ merged: true,
3112
+ branch,
3113
+ into: baseBranch,
3114
+ removeResult,
3115
+ validationSummary,
3116
+ patchEquivalence,
3117
+ submoduleReachability,
3118
+ mergeResult,
3119
+ refineStages,
3120
+ ...(ledgerError ? { ledgerError } : {}),
3121
+ finalBranchConvergenceState,
3122
+ };
1449
3123
  }
1450
3124
 
1451
- return handlerResult;
3125
+ return {
3126
+ success: true,
3127
+ merged: true,
3128
+ branch,
3129
+ into: baseBranch,
3130
+ removeResult,
3131
+ validationSummary,
3132
+ patchEquivalence,
3133
+ submoduleReachability,
3134
+ mergeResult,
3135
+ refineStages,
3136
+ ...(ledgerError ? { ledgerError } : {}),
3137
+ finalBranchConvergenceState,
3138
+ };
1452
3139
  } catch (e: any) {
1453
- logCommand({ ts: new Date().toISOString(), cmd, source: logSource, interactionId, args: normalizedArgs, success: false, error: e.message, durationMs: Date.now() - cmdStart });
1454
- recordDebugTrace({
1455
- interactionId,
1456
- category: 'command',
1457
- stage: 'failed',
1458
- level: 'error',
1459
- payload: { cmd, source: logSource, error: e?.message || String(e), durationMs: Date.now() - cmdStart },
1460
- });
1461
- throw e;
3140
+ return { success: false, error: e.message, refineStages };
1462
3141
  }
1463
3142
  }
1464
3143
 
3144
+ private async finishMeshRefineJob(handle: MeshRefineJobHandle, args: any): Promise<void> {
3145
+ const key = this.buildRefineJobKey(handle.meshId, handle.targetNodeId);
3146
+ let result: Record<string, unknown>;
3147
+ try {
3148
+ result = await this.executeMeshRefineNodeSynchronously(handle.meshId, handle.targetNodeId, args) as Record<string, unknown>;
3149
+ } catch (e: any) {
3150
+ result = { success: false, error: e?.message || String(e) };
3151
+ }
3152
+ const completedAt = new Date().toISOString();
3153
+ const terminalHandle = this.buildRefineJobHandle({
3154
+ meshId: handle.meshId,
3155
+ nodeId: handle.targetNodeId,
3156
+ status: result.success === true ? 'completed' : 'failed',
3157
+ startedAt: handle.startedAt,
3158
+ completedAt,
3159
+ jobId: handle.jobId,
3160
+ interactionId: handle.interactionId,
3161
+ retryOfJobId: handle.retryOfJobId,
3162
+ node: { daemonId: handle.targetDaemonId, workspace: handle.workspace },
3163
+ });
3164
+ const terminal: MeshRefineTerminalJob = { ...terminalHandle, result };
3165
+ this.terminalRefineJobs.set(key, terminal);
3166
+ this.runningRefineJobs.delete(key);
3167
+ this.invalidateAggregateMeshStatus(handle.meshId);
3168
+ await this.appendRefineJobLedger(result.success === true ? 'task_completed' : 'task_failed', terminalHandle, result);
3169
+ this.queueRefineJobEvent(result.success === true ? 'refine:completed' : 'refine:failed', terminalHandle, result);
3170
+ }
3171
+
3172
+ private async startMeshRefineJob(meshId: string, nodeId: string, args: any): Promise<CommandRouterResult> {
3173
+ const key = this.buildRefineJobKey(meshId, nodeId);
3174
+ const running = this.runningRefineJobs.get(key);
3175
+ if (running) return { ...running, duplicate: true };
3176
+ const terminal = this.terminalRefineJobs.get(key);
3177
+
3178
+ const meshRecord = await this.getMeshForCommand(meshId, args?.inlineMesh);
3179
+ const mesh = meshRecord?.mesh;
3180
+ const node = mesh?.nodes?.find((n: any) => n.id === nodeId || n.nodeId === nodeId);
3181
+ if (!node) return { success: false, error: `Node '${nodeId}' not found in mesh` };
3182
+ if (!node.isLocalWorktree || !node.workspace) return { success: false, error: `Refinery requires a local worktree node` };
3183
+
3184
+ const handle = this.buildRefineJobHandle({ meshId, nodeId, node, retryOfJobId: terminal?.jobId });
3185
+ this.runningRefineJobs.set(key, handle);
3186
+ await this.appendRefineJobLedger('task_dispatched', handle);
3187
+ this.queueRefineJobEvent('refine:accepted', handle);
3188
+
3189
+ setImmediate(() => {
3190
+ void this.finishMeshRefineJob(handle, args);
3191
+ });
3192
+
3193
+ return handle;
3194
+ }
3195
+
1465
3196
  // ─── Daemon-level command core ───────────────────
1466
3197
 
1467
3198
  /**
@@ -1476,7 +3207,8 @@ export class DaemonCommandRouter {
1476
3207
  }
1477
3208
 
1478
3209
  case 'get_pending_mesh_events': {
1479
- const events = drainPendingMeshCoordinatorEvents();
3210
+ const meshId = typeof args?.meshId === 'string' ? args.meshId.trim() : '';
3211
+ const events = drainPendingMeshCoordinatorEvents(meshId || undefined);
1480
3212
  return { success: true, events };
1481
3213
  }
1482
3214
 
@@ -2081,15 +3813,44 @@ export class DaemonCommandRouter {
2081
3813
  case 'get_mesh': {
2082
3814
  const meshId = typeof args?.meshId === 'string' ? args.meshId.trim() : '';
2083
3815
  if (!meshId) return { success: false, error: 'meshId required' };
2084
- try {
2085
- const { getMesh } = await import('../config/mesh-config.js');
2086
- const mesh = getMesh(meshId);
2087
- if (mesh) return { success: true, mesh };
2088
- } catch { /* fall through to inline cache */ }
2089
- // Fallback: check in-memory cache for cloud-originating meshes
2090
- const cached = this.inlineMeshCache.get(meshId);
2091
- if (cached) return { success: true, mesh: cached };
2092
- return { success: false, error: 'Mesh not found' };
3816
+ const meshRecord = await this.getMeshForCommand(meshId, args?.inlineMesh, { preferInline: true });
3817
+ if (!meshRecord?.mesh) return { success: false, error: 'Mesh not found' };
3818
+
3819
+ const requireDirectPeerTruth = args?.requireDirectPeerTruth === true;
3820
+ const directTruth = await hydrateInlineMeshDirectTruth({
3821
+ mesh: meshRecord.mesh,
3822
+ meshSource: meshRecord.source,
3823
+ dispatchMeshCommand: this.deps.dispatchMeshCommand,
3824
+ statusInstanceId: this.deps.statusInstanceId,
3825
+ localMachineId: loadConfig().machineId || '',
3826
+ });
3827
+ const directTruthSatisfied = meshRecord.source !== 'inline_bootstrap' || directTruth.directEvidenceCount > 0;
3828
+ const sourceOfTruth = {
3829
+ membership: meshRecord.source === 'inline_cache'
3830
+ ? 'coordinator_inline_mesh_cache'
3831
+ : meshRecord.source === 'local_config'
3832
+ ? 'local_mesh_config'
3833
+ : 'inline_bootstrap_snapshot',
3834
+ coordinatorOwnsLiveTruth: directTruthSatisfied,
3835
+ directPeerTruth: {
3836
+ required: requireDirectPeerTruth,
3837
+ satisfied: directTruthSatisfied,
3838
+ directEvidenceCount: directTruth.directEvidenceCount,
3839
+ localConfirmedCount: directTruth.localConfirmedCount,
3840
+ peerAttemptedCount: directTruth.peerAttemptedCount,
3841
+ peerConfirmedCount: directTruth.peerConfirmedCount,
3842
+ unavailableNodeIds: directTruth.unavailableNodeIds,
3843
+ },
3844
+ };
3845
+ if (requireDirectPeerTruth && !directTruthSatisfied) {
3846
+ return {
3847
+ success: false,
3848
+ code: 'mesh_direct_peer_truth_unavailable',
3849
+ error: 'Selected coordinator could not confirm direct mesh truth yet. Bootstrap inventory stays unavailable until direct get_mesh probes succeed.',
3850
+ sourceOfTruth,
3851
+ };
3852
+ }
3853
+ return { success: true, mesh: meshRecord.mesh, sourceOfTruth };
2093
3854
  }
2094
3855
 
2095
3856
  case 'create_mesh': {
@@ -2100,7 +3861,10 @@ export class DaemonCommandRouter {
2100
3861
  if (!name) return { success: false, error: 'name required' };
2101
3862
  try {
2102
3863
  const { createMesh } = await import('../config/mesh-config.js');
2103
- const mesh = createMesh({ name, repoIdentity, repoRemoteUrl, defaultBranch, policy: args?.policy });
3864
+ const meshHost = args?.meshHost && typeof args.meshHost === 'object' && !Array.isArray(args.meshHost)
3865
+ ? args.meshHost
3866
+ : undefined;
3867
+ const mesh = createMesh({ name, repoIdentity, repoRemoteUrl, defaultBranch, policy: args?.policy, meshHost });
2104
3868
  return { success: true, mesh };
2105
3869
  } catch (e: any) {
2106
3870
  return { success: false, error: e.message };
@@ -2117,16 +3881,237 @@ export class DaemonCommandRouter {
2117
3881
  if (typeof args?.defaultBranch === 'string') patch.defaultBranch = args.defaultBranch;
2118
3882
  if (args?.policy && typeof args.policy === 'object' && !Array.isArray(args.policy)) patch.policy = args.policy;
2119
3883
  if (args?.coordinator && typeof args.coordinator === 'object' && !Array.isArray(args.coordinator)) patch.coordinator = args.coordinator;
3884
+ if (args?.meshHost && typeof args.meshHost === 'object' && !Array.isArray(args.meshHost)) patch.meshHost = args.meshHost;
2120
3885
  if (!Object.keys(patch).length) return { success: false, error: 'No updates provided' };
2121
3886
  const mesh = updateMesh(meshId, patch as any);
2122
3887
  if (!mesh) return { success: false, error: 'Mesh not found' };
2123
3888
  this.inlineMeshCache.set(meshId, mesh);
3889
+ this.invalidateAggregateMeshStatus(meshId);
2124
3890
  return { success: true, mesh };
2125
3891
  } catch (e: any) {
2126
3892
  return { success: false, error: e.message };
2127
3893
  }
2128
3894
  }
2129
3895
 
3896
+ case 'get_mesh_host_pairing': {
3897
+ const meshId = typeof args?.meshId === 'string' ? args.meshId.trim() : '';
3898
+ if (!meshId) return { success: false, error: 'meshId required' };
3899
+ const meshRecord = await this.getMeshForCommand(meshId, args?.inlineMesh, { preferInline: true });
3900
+ const mesh = meshRecord?.mesh;
3901
+ if (!mesh) return { success: false, error: 'Mesh not found' };
3902
+ const meshHost = resolveMeshHostStatus(mesh);
3903
+ const pairingStatus = meshHost.pairing?.status || 'not_configured';
3904
+ return {
3905
+ success: true,
3906
+ code: pairingStatus === 'not_configured' ? 'mesh_host_pairing_not_configured' : 'mesh_host_pairing_pending',
3907
+ meshId,
3908
+ hostAddress: meshHost.hostAddress,
3909
+ meshHost,
3910
+ manualPairing: {
3911
+ status: pairingStatus,
3912
+ joinImplemented: true,
3913
+ protocol: 'standalone_command_direct_v1',
3914
+ description: 'Standalone manual pairing can save address/token metadata, apply a host join over direct standalone command HTTP or injected mesh command dispatch, and check persisted status. P2P signaling remains outside this slice.',
3915
+ },
3916
+ };
3917
+ }
3918
+
3919
+ case 'configure_mesh_host_pairing': {
3920
+ const meshId = typeof args?.meshId === 'string' ? args.meshId.trim() : '';
3921
+ const hostAddress = typeof args?.hostAddress === 'string' ? args.hostAddress.trim() : '';
3922
+ const token = typeof args?.token === 'string' ? args.token.trim() : '';
3923
+ if (!meshId) return { success: false, error: 'meshId required' };
3924
+ if (!hostAddress || !token) return { success: false, error: 'hostAddress and token required' };
3925
+ try {
3926
+ const { configureMeshHostPairing } = await import('../config/mesh-config.js');
3927
+ const configured = configureMeshHostPairing(meshId, { hostAddress, token });
3928
+ if (!configured) return { success: false, error: 'Mesh not found' };
3929
+ this.inlineMeshCache.set(meshId, configured.mesh);
3930
+ const meshHost = resolveMeshHostStatus(configured.mesh);
3931
+ return {
3932
+ success: true,
3933
+ code: 'mesh_host_pairing_pending',
3934
+ meshId,
3935
+ hostAddress: configured.hostAddress,
3936
+ meshHost,
3937
+ manualPairing: {
3938
+ status: meshHost.pairing?.status || 'pairing',
3939
+ joinImplemented: true,
3940
+ protocol: 'standalone_command_direct_v1',
3941
+ description: 'Manual Mesh Host pairing config was saved locally. Use join_mesh_host_pairing to apply it to the host. Raw token was not persisted.',
3942
+ },
3943
+ };
3944
+ } catch (e: any) {
3945
+ return { success: false, code: 'mesh_host_pairing_invalid', meshId, hostAddress, error: e.message };
3946
+ }
3947
+ }
3948
+
3949
+ case 'create_mesh_host_pairing_token': {
3950
+ const meshId = typeof args?.meshId === 'string' ? args.meshId.trim() : '';
3951
+ if (!meshId) return { success: false, error: 'meshId required' };
3952
+ try {
3953
+ const { createMeshHostPairingToken } = await import('../config/mesh-config.js');
3954
+ const created = createMeshHostPairingToken(meshId, {
3955
+ token: typeof args?.token === 'string' ? args.token : undefined,
3956
+ expiresAt: typeof args?.expiresAt === 'string' ? args.expiresAt : undefined,
3957
+ });
3958
+ if (!created) return { success: false, error: 'Mesh not found' };
3959
+ this.inlineMeshCache.set(meshId, created.mesh);
3960
+ this.invalidateAggregateMeshStatus(meshId);
3961
+ return {
3962
+ success: true,
3963
+ code: 'mesh_host_pairing_token_created',
3964
+ meshId,
3965
+ token: created.token,
3966
+ tokenId: created.tokenId,
3967
+ expiresAt: created.expiresAt,
3968
+ meshHost: resolveMeshHostStatus(created.mesh),
3969
+ warning: 'Raw token is returned once and is not persisted; share it with member daemons over a trusted channel.',
3970
+ };
3971
+ } catch (e: any) {
3972
+ return { success: false, code: 'mesh_host_pairing_token_invalid', meshId, error: e.message };
3973
+ }
3974
+ }
3975
+
3976
+ case 'apply_mesh_host_join': {
3977
+ const meshId = typeof args?.meshId === 'string' ? args.meshId.trim() : '';
3978
+ const token = typeof args?.token === 'string' ? args.token.trim() : '';
3979
+ const memberNode = args?.memberNode && typeof args.memberNode === 'object' && !Array.isArray(args.memberNode)
3980
+ ? args.memberNode
3981
+ : null;
3982
+ if (!meshId) return { success: false, error: 'meshId required' };
3983
+ if (!token || !memberNode) return { success: false, error: 'token and memberNode required' };
3984
+ try {
3985
+ const { applyMeshHostJoinRequest } = await import('../config/mesh-config.js');
3986
+ const applied = applyMeshHostJoinRequest(meshId, {
3987
+ token,
3988
+ memberNode: memberNode as any,
3989
+ memberMeshId: typeof args?.memberMeshId === 'string' ? args.memberMeshId : undefined,
3990
+ });
3991
+ if (!applied) return { success: false, error: 'Mesh not found' };
3992
+ if (!applied.accepted) {
3993
+ return {
3994
+ success: false,
3995
+ code: 'mesh_host_join_rejected',
3996
+ meshId,
3997
+ tokenId: applied.tokenId,
3998
+ meshHost: applied.meshHost ? resolveMeshHostStatus({ meshHost: applied.meshHost }) : undefined,
3999
+ error: applied.reason,
4000
+ };
4001
+ }
4002
+ this.inlineMeshCache.set(meshId, applied.mesh);
4003
+ this.invalidateAggregateMeshStatus(meshId);
4004
+ try {
4005
+ const { appendLedgerEntry } = await import('../mesh/mesh-ledger.js');
4006
+ appendLedgerEntry(meshId, {
4007
+ kind: 'node_joined',
4008
+ nodeId: applied.node.id,
4009
+ payload: { role: 'member', tokenId: applied.tokenId, workspace: applied.node.workspace },
4010
+ });
4011
+ } catch { /* ledger append is best-effort */ }
4012
+ return {
4013
+ success: true,
4014
+ code: 'mesh_host_join_accepted',
4015
+ meshId,
4016
+ node: applied.node,
4017
+ tokenId: applied.tokenId,
4018
+ meshHost: resolveMeshHostStatus(applied.mesh),
4019
+ };
4020
+ } catch (e: any) {
4021
+ return { success: false, code: 'mesh_host_join_failed', meshId, error: e.message };
4022
+ }
4023
+ }
4024
+
4025
+ case 'join_mesh_host_pairing': {
4026
+ const meshId = typeof args?.meshId === 'string' ? args.meshId.trim() : '';
4027
+ const token = typeof args?.token === 'string' ? args.token.trim() : '';
4028
+ if (!meshId) return { success: false, error: 'meshId required' };
4029
+ if (!token) return { success: false, error: 'token required because raw pairing tokens are not persisted' };
4030
+ const meshRecord = await this.getMeshForCommand(meshId, args?.inlineMesh, { preferInline: true });
4031
+ const mesh = meshRecord?.mesh;
4032
+ if (!mesh) return { success: false, error: 'Mesh not found' };
4033
+ const meshHost = resolveMeshHostStatus(mesh);
4034
+ if (meshHost.role !== 'member') {
4035
+ return { success: false, code: 'mesh_host_join_not_member', meshId, meshHost, error: 'join_mesh_host_pairing must run from a member daemon configured with a Mesh Host address/token.' };
4036
+ }
4037
+ try {
4038
+ const { tokenIdForManualPairing, markMeshHostPairingJoined } = await import('../config/mesh-config.js');
4039
+ const tokenId = tokenIdForManualPairing(token);
4040
+ if (meshHost.pairing?.tokenId && meshHost.pairing.tokenId !== tokenId) {
4041
+ return { success: false, code: 'mesh_host_join_rejected', meshId, tokenId, meshHost, error: 'invalid pairing token' };
4042
+ }
4043
+ const memberNode = buildMemberJoinNode(mesh, args, this.deps.statusInstanceId);
4044
+ if (!memberNode) return { success: false, error: 'member node metadata unavailable' };
4045
+ const hostMeshId = typeof args?.hostMeshId === 'string' && args.hostMeshId.trim() ? args.hostMeshId.trim() : meshId;
4046
+ const hostDaemonId = typeof args?.hostDaemonId === 'string' && args.hostDaemonId.trim()
4047
+ ? args.hostDaemonId.trim()
4048
+ : meshHost.hostDaemonId;
4049
+ let hostResult: any;
4050
+ let transport: string;
4051
+ if (hostDaemonId && this.deps.dispatchMeshCommand) {
4052
+ transport = 'mesh_command_dispatch';
4053
+ hostResult = await this.deps.dispatchMeshCommand(hostDaemonId, 'apply_mesh_host_join', {
4054
+ meshId: hostMeshId,
4055
+ token,
4056
+ memberMeshId: meshId,
4057
+ memberNode,
4058
+ });
4059
+ } else if (meshHost.hostAddress) {
4060
+ transport = 'standalone_http_command';
4061
+ const commandUrl = normalizeStandaloneHostCommandUrl(meshHost.hostAddress);
4062
+ const response = await fetch(commandUrl, {
4063
+ method: 'POST',
4064
+ headers: { 'Content-Type': 'application/json' },
4065
+ body: JSON.stringify({ type: 'apply_mesh_host_join', payload: { meshId: hostMeshId, token, memberMeshId: meshId, memberNode } }),
4066
+ });
4067
+ hostResult = await response.json().catch(() => ({ success: false, error: `Host returned HTTP ${response.status}` }));
4068
+ if (!response.ok && hostResult?.success !== false) hostResult = { success: false, error: `Host returned HTTP ${response.status}` };
4069
+ } else {
4070
+ return {
4071
+ success: false,
4072
+ code: 'mesh_host_join_transport_unavailable',
4073
+ meshId,
4074
+ meshHost,
4075
+ error: 'No hostDaemonId dispatch path or hostAddress HTTP command path is available. P2P signaling join is not implemented in this slice.',
4076
+ };
4077
+ }
4078
+ if (!hostResult?.success) {
4079
+ return { success: false, code: hostResult?.code || 'mesh_host_join_rejected', meshId, meshHost, transport, error: hostResult?.error || 'Mesh Host rejected join request', hostResult };
4080
+ }
4081
+ const joined = meshRecord.inline
4082
+ ? null
4083
+ : markMeshHostPairingJoined(meshId, {
4084
+ tokenId: hostResult.tokenId || tokenId,
4085
+ hostDaemonId: hostResult.meshHost?.hostDaemonId || hostDaemonId,
4086
+ hostNodeId: hostResult.meshHost?.hostNodeId,
4087
+ joinedAt: hostResult.meshHost?.pairing?.joinedAt,
4088
+ });
4089
+ if (joined) {
4090
+ this.inlineMeshCache.set(meshId, joined.mesh);
4091
+ this.invalidateAggregateMeshStatus(meshId);
4092
+ }
4093
+ return {
4094
+ success: true,
4095
+ code: 'mesh_host_join_applied',
4096
+ meshId,
4097
+ hostMeshId,
4098
+ transport,
4099
+ node: hostResult.node,
4100
+ tokenId: hostResult.tokenId || tokenId,
4101
+ meshHost: joined ? resolveMeshHostStatus(joined.mesh) : { ...meshHost, pairing: { ...(meshHost.pairing || {}), status: 'paired', tokenId: hostResult.tokenId || tokenId } },
4102
+ hostResult,
4103
+ manualPairing: {
4104
+ status: 'paired',
4105
+ joinImplemented: true,
4106
+ protocol: 'standalone_command_direct_v1',
4107
+ description: 'Mesh Host accepted the join and local member pairing status was marked paired. P2P runtime signaling remains outside this slice.',
4108
+ },
4109
+ };
4110
+ } catch (e: any) {
4111
+ return { success: false, code: 'mesh_host_join_failed', meshId, meshHost, error: e.message };
4112
+ }
4113
+ }
4114
+
2130
4115
  case 'delete_mesh': {
2131
4116
  const meshId = typeof args?.meshId === 'string' ? args.meshId.trim() : '';
2132
4117
  if (!meshId) return { success: false, error: 'meshId required' };
@@ -2220,6 +4205,8 @@ export class DaemonCommandRouter {
2220
4205
  const meshId = typeof args?.meshId === 'string' ? args.meshId.trim() : '';
2221
4206
  const taskId = typeof args?.taskId === 'string' ? args.taskId.trim() : '';
2222
4207
  if (!meshId || !taskId) return { success: false, error: 'meshId and taskId required' };
4208
+ const ownerFailure = await this.requireMeshHostMutationOwner(meshId, args?.inlineMesh, 'queue cancellation');
4209
+ if (ownerFailure) return ownerFailure;
2223
4210
  try {
2224
4211
  const { cancelTask } = await import('../mesh/mesh-work-queue.js');
2225
4212
  const reason = typeof args?.reason === 'string' ? args.reason : undefined;
@@ -2235,6 +4222,8 @@ export class DaemonCommandRouter {
2235
4222
  const meshId = typeof args?.meshId === 'string' ? args.meshId.trim() : '';
2236
4223
  const taskId = typeof args?.taskId === 'string' ? args.taskId.trim() : '';
2237
4224
  if (!meshId || !taskId) return { success: false, error: 'meshId and taskId required' };
4225
+ const ownerFailure = await this.requireMeshHostMutationOwner(meshId, args?.inlineMesh, 'queue requeue');
4226
+ if (ownerFailure) return ownerFailure;
2238
4227
  try {
2239
4228
  const { requeueTask } = await import('../mesh/mesh-work-queue.js');
2240
4229
  const task = requeueTask(meshId, taskId, {
@@ -2256,6 +4245,8 @@ export class DaemonCommandRouter {
2256
4245
  const workspace = typeof args?.workspace === 'string' ? args.workspace.trim() : '';
2257
4246
  if (!meshId) return { success: false, error: 'meshId required' };
2258
4247
  if (!workspace) return { success: false, error: 'workspace required' };
4248
+ const ownerFailure = await this.requireMeshHostMutationOwner(meshId, args?.inlineMesh, 'node addition');
4249
+ if (ownerFailure) return ownerFailure;
2259
4250
  try {
2260
4251
  const { addNode } = await import('../config/mesh-config.js');
2261
4252
  const providerPriority = Array.isArray(args?.providerPriority)
@@ -2266,7 +4257,18 @@ export class DaemonCommandRouter {
2266
4257
  ...(readOnly ? { readOnly: true } : {}),
2267
4258
  ...(providerPriority.length ? { providerPriority } : {}),
2268
4259
  };
2269
- const node = addNode(meshId, { workspace, ...(policy ? { policy } : {}) });
4260
+ const role = normalizeMeshDaemonRole(args?.role);
4261
+ const daemonId = typeof args?.daemonId === 'string' && args.daemonId.trim() ? args.daemonId.trim() : undefined;
4262
+ const machineId = typeof args?.machineId === 'string' && args.machineId.trim() ? args.machineId.trim() : undefined;
4263
+ const repoRoot = typeof args?.repoRoot === 'string' && args.repoRoot.trim() ? args.repoRoot.trim() : undefined;
4264
+ const node = addNode(meshId, {
4265
+ workspace,
4266
+ ...(repoRoot ? { repoRoot } : {}),
4267
+ ...(daemonId ? { daemonId } : {}),
4268
+ ...(machineId ? { machineId } : {}),
4269
+ ...(policy ? { policy } : {}),
4270
+ ...(role ? { role } : {}),
4271
+ });
2270
4272
  if (!node) return { success: false, error: 'Mesh not found' };
2271
4273
  return { success: true, node };
2272
4274
  } catch (e: any) {
@@ -2278,6 +4280,8 @@ export class DaemonCommandRouter {
2278
4280
  const meshId = typeof args?.meshId === 'string' ? args.meshId.trim() : '';
2279
4281
  const nodeId = typeof args?.nodeId === 'string' ? args.nodeId.trim() : '';
2280
4282
  if (!meshId || !nodeId) return { success: false, error: 'meshId and nodeId required' };
4283
+ const ownerFailure = await this.requireMeshHostMutationOwner(meshId, args?.inlineMesh, 'node update');
4284
+ if (ownerFailure) return ownerFailure;
2281
4285
  try {
2282
4286
  const { updateNode } = await import('../config/mesh-config.js');
2283
4287
  const policy = args?.policy && typeof args.policy === 'object' && !Array.isArray(args.policy)
@@ -2306,6 +4310,8 @@ export class DaemonCommandRouter {
2306
4310
  const meshId = typeof args?.meshId === 'string' ? args.meshId.trim() : '';
2307
4311
  const nodeId = typeof args?.nodeId === 'string' ? args.nodeId.trim() : '';
2308
4312
  if (!meshId || !nodeId) return { success: false, error: 'meshId and nodeId required' };
4313
+ const ownerFailure = await this.requireMeshHostMutationOwner(meshId, args?.inlineMesh, 'node removal');
4314
+ if (ownerFailure) return ownerFailure;
2309
4315
  try {
2310
4316
  const meshRecord = await this.getMeshForCommand(meshId, args?.inlineMesh);
2311
4317
  const mesh = meshRecord?.mesh;
@@ -2331,131 +4337,91 @@ export class DaemonCommandRouter {
2331
4337
  }
2332
4338
  }
2333
4339
 
2334
- case 'refine_mesh_node': {
4340
+ case 'get_mesh_refine_config_schema': {
4341
+ return {
4342
+ success: true,
4343
+ schema: MESH_REFINE_CONFIG_SCHEMA,
4344
+ locations: MESH_REFINE_CONFIG_LOCATIONS,
4345
+ sourceOfTruth: 'repo mesh/refine config',
4346
+ heuristicRole: 'suggestions_only_not_execution_path',
4347
+ };
4348
+ }
4349
+
4350
+ case 'validate_mesh_refine_config': {
4351
+ const workspace = typeof args?.workspace === 'string' ? args.workspace : process.cwd();
4352
+ const mesh = args?.inlineMesh || {};
4353
+ const loaded = args?.config !== undefined
4354
+ ? { config: args.config, source: 'inline', sourceType: 'mesh_policy' as const }
4355
+ : loadMeshRefineConfig(mesh, workspace);
4356
+ const validation = loaded.config
4357
+ ? validateMeshRefineConfig(loaded.config, loaded.source)
4358
+ : { valid: false, errors: [((loaded as { error?: string }).error) || 'repo mesh/refine config unavailable'], commands: [], rejectedCommands: [] };
4359
+ return { success: validation.valid, ...loaded, ...validation };
4360
+ }
4361
+
4362
+ case 'suggest_mesh_refine_config': {
4363
+ const workspace = typeof args?.workspace === 'string' ? args.workspace : process.cwd();
4364
+ const mesh = args?.inlineMesh || {};
4365
+ return {
4366
+ success: true,
4367
+ ...suggestMeshRefineConfig(mesh, workspace),
4368
+ note: 'Suggestions are heuristic scaffold only; Refinery will not execute them until saved into repo mesh/refine config.',
4369
+ };
4370
+ }
4371
+
4372
+ case 'plan_mesh_refine_node': {
2335
4373
  const meshId = typeof args?.meshId === 'string' ? args.meshId.trim() : '';
2336
4374
  const nodeId = typeof args?.nodeId === 'string' ? args.nodeId.trim() : '';
2337
4375
  if (!meshId || !nodeId) return { success: false, error: 'meshId and nodeId required' };
2338
- try {
4376
+ const meshRecord = await this.getMeshForCommand(meshId, args?.inlineMesh);
4377
+ const mesh = meshRecord?.mesh;
4378
+ const node = mesh?.nodes?.find((n: any) => n.id === nodeId || n.nodeId === nodeId);
4379
+ if (!node?.workspace) return { success: false, error: `Node '${nodeId}' workspace not found` };
4380
+ return {
4381
+ success: true,
4382
+ dryRun: true,
4383
+ nodeId,
4384
+ workspace: node.workspace,
4385
+ validationPlan: buildMeshRefineValidationPlan(mesh, node.workspace),
4386
+ mergeWillRun: false,
4387
+ cleanupWillRun: false,
4388
+ };
4389
+ }
4390
+
4391
+ case 'fast_forward_mesh_node': {
4392
+ const meshId = typeof args?.meshId === 'string' ? args.meshId.trim() : '';
4393
+ const nodeId = typeof args?.nodeId === 'string' ? args.nodeId.trim() : '';
4394
+ let workspace = typeof args?.workspace === 'string' ? args.workspace.trim() : '';
4395
+ let submoduleIgnorePaths = Array.isArray(args?.submoduleIgnorePaths)
4396
+ ? args.submoduleIgnorePaths.filter((value: unknown): value is string => typeof value === 'string')
4397
+ : undefined;
4398
+ if (!workspace && meshId && nodeId) {
2339
4399
  const meshRecord = await this.getMeshForCommand(meshId, args?.inlineMesh);
2340
4400
  const mesh = meshRecord?.mesh;
2341
4401
  const node = mesh?.nodes?.find((n: any) => n.id === nodeId || n.nodeId === nodeId);
2342
- if (!node) return { success: false, error: `Node '${nodeId}' not found in mesh` };
2343
-
2344
- if (!node.isLocalWorktree || !node.workspace) {
2345
- return { success: false, error: `Refinery requires a local worktree node` };
2346
- }
2347
-
2348
- const sourceNode = node.clonedFromNodeId
2349
- ? mesh?.nodes.find((n: any) => n.id === node.clonedFromNodeId || n.nodeId === node.clonedFromNodeId)
2350
- : mesh?.nodes.find((n: any) => !n.isLocalWorktree);
2351
- const repoRoot = sourceNode?.repoRoot || sourceNode?.workspace;
2352
- if (!repoRoot) return { success: false, error: 'Source node repoRoot not found' };
2353
-
2354
- const { execFile } = await import('node:child_process');
2355
- const { promisify } = await import('node:util');
2356
- const execFileAsync = promisify(execFile);
2357
-
2358
- const { stdout: branchStdout } = await execFileAsync('git', ['branch', '--show-current'], { cwd: node.workspace, encoding: 'utf8' });
2359
- const branch = branchStdout.trim();
2360
- if (!branch) return { success: false, error: 'Could not determine branch of the worktree node' };
2361
-
2362
- const { stdout: baseBranchStdout } = await execFileAsync('git', ['branch', '--show-current'], { cwd: repoRoot, encoding: 'utf8' });
2363
- const baseBranch = baseBranchStdout.trim();
2364
-
2365
- const validationSummary = await runMeshRefineValidationGate(mesh, node.workspace);
2366
- if (validationSummary.status === 'failed') {
2367
- return {
2368
- success: false,
2369
- code: 'validation_failed',
2370
- convergenceStatus: 'blocked_review',
2371
- error: 'Refinery validation gate failed; merge/refine was not attempted.',
2372
- branch,
2373
- into: baseBranch,
2374
- validationSummary,
2375
- finalBranchConvergenceState: {
2376
- branch,
2377
- baseBranch,
2378
- merged: false,
2379
- removed: false,
2380
- validation: 'failed',
2381
- status: 'blocked_review',
2382
- },
2383
- };
2384
- }
2385
- if (validationSummary.status === 'skipped') {
2386
- return {
2387
- success: false,
2388
- code: 'validation_unavailable',
2389
- convergenceStatus: 'blocked_review',
2390
- error: 'Refinery validation gate is required but no allowlisted validation command was available; merge/refine was not attempted.',
2391
- branch,
2392
- into: baseBranch,
2393
- validationSummary,
2394
- finalBranchConvergenceState: {
2395
- branch,
2396
- baseBranch,
2397
- merged: false,
2398
- removed: false,
2399
- validation: 'unavailable',
2400
- status: 'blocked_review',
2401
- },
2402
- };
2403
- }
2404
-
2405
- try {
2406
- await execFileAsync('git', ['merge', '--no-ff', branch, '-m', `Auto-merge branch '${branch}' via Refinery`], { cwd: repoRoot, encoding: 'utf8' });
2407
- } catch (e: any) {
2408
- return {
2409
- success: false,
2410
- error: `Merge failed (conflicts?): ${e.message}`,
2411
- validationSummary,
2412
- finalBranchConvergenceState: {
2413
- branch,
2414
- baseBranch,
2415
- merged: false,
2416
- removed: false,
2417
- validation: 'passed',
2418
- status: 'not_mergeable',
2419
- },
2420
- };
4402
+ workspace = typeof node?.workspace === 'string' ? node.workspace.trim() : '';
4403
+ if (!submoduleIgnorePaths && Array.isArray(node?.policy?.submoduleIgnorePaths)) {
4404
+ submoduleIgnorePaths = node.policy.submoduleIgnorePaths.filter((value: unknown): value is string => typeof value === 'string');
2421
4405
  }
2422
-
2423
- const removeResult = await this.execute('remove_mesh_node', {
2424
- meshId,
2425
- nodeId,
2426
- sessionCleanupMode: 'kill',
2427
- inlineMesh: args?.inlineMesh,
2428
- });
2429
-
2430
- try {
2431
- const { appendLedgerEntry } = await import('../mesh/mesh-ledger.js');
2432
- appendLedgerEntry(meshId, {
2433
- kind: 'node_removed',
2434
- nodeId,
2435
- payload: { refined: true, mergedBranch: branch, into: baseBranch, validationSummary },
2436
- });
2437
- } catch {}
2438
-
2439
- return {
2440
- success: true,
2441
- merged: true,
2442
- branch,
2443
- into: baseBranch,
2444
- removeResult,
2445
- validationSummary,
2446
- finalBranchConvergenceState: {
2447
- branch: baseBranch,
2448
- mergedBranch: branch,
2449
- baseBranch,
2450
- merged: true,
2451
- removed: removeResult?.success !== false,
2452
- validation: 'passed',
2453
- status: removeResult?.success === false ? 'merged_cleanup_failed' : 'merged',
2454
- },
2455
- };
2456
- } catch (e: any) {
2457
- return { success: false, error: e.message };
2458
4406
  }
4407
+ const result = await (fastForwardMeshNode({
4408
+ meshId: meshId || undefined,
4409
+ nodeId: nodeId || undefined,
4410
+ workspace,
4411
+ branch: typeof args?.branch === 'string' ? args.branch : undefined,
4412
+ execute: args?.execute === true,
4413
+ dryRun: args?.dryRun === true,
4414
+ updateSubmodules: args?.updateSubmodules === true,
4415
+ submoduleIgnorePaths,
4416
+ }) as Promise<unknown>);
4417
+ return result as CommandRouterResult;
4418
+ }
4419
+
4420
+ case 'refine_mesh_node': {
4421
+ const meshId = typeof args?.meshId === 'string' ? args.meshId.trim() : '';
4422
+ const nodeId = typeof args?.nodeId === 'string' ? args.nodeId.trim() : '';
4423
+ if (!meshId || !nodeId) return { success: false, error: 'meshId and nodeId required' };
4424
+ return this.startMeshRefineJob(meshId, nodeId, args);
2459
4425
  }
2460
4426
 
2461
4427
  case 'remove_mesh_node': {
@@ -2499,6 +4465,7 @@ export class DaemonCommandRouter {
2499
4465
  } else {
2500
4466
  const { removeNode } = await import('../config/mesh-config.js');
2501
4467
  removed = removeNode(meshId, nodeId);
4468
+ if (removed) this.invalidateAggregateMeshStatus(meshId);
2502
4469
  }
2503
4470
 
2504
4471
  // Record in task ledger
@@ -2536,6 +4503,8 @@ export class DaemonCommandRouter {
2536
4503
  if (!meshId) return { success: false, error: 'meshId required' };
2537
4504
  if (!sourceNodeId) return { success: false, error: 'sourceNodeId required' };
2538
4505
  if (!branch) return { success: false, error: 'branch required' };
4506
+ const ownerFailure = await this.requireMeshHostMutationOwner(meshId, args?.inlineMesh, 'worktree clone');
4507
+ if (ownerFailure) return ownerFailure;
2539
4508
 
2540
4509
  try {
2541
4510
  const meshRecord = await this.getMeshForCommand(meshId, args?.inlineMesh);
@@ -2584,6 +4553,7 @@ export class DaemonCommandRouter {
2584
4553
  policy: { ...(sourceNode.policy || {}) },
2585
4554
  });
2586
4555
  if (!node) return { success: false, error: 'Failed to register worktree node' };
4556
+ this.invalidateAggregateMeshStatus(meshId);
2587
4557
  }
2588
4558
 
2589
4559
  // Initialize submodules if policy allows (default: true)
@@ -2625,6 +4595,8 @@ export class DaemonCommandRouter {
2625
4595
  case 'trigger_mesh_queue': {
2626
4596
  const meshId = typeof args?.meshId === 'string' ? args.meshId.trim() : '';
2627
4597
  if (!meshId) return { success: false, error: 'meshId required' };
4598
+ const ownerFailure = await this.requireMeshHostMutationOwner(meshId, args?.inlineMesh, 'queue trigger');
4599
+ if (ownerFailure) return ownerFailure;
2628
4600
  try {
2629
4601
  const { triggerMeshQueue } = await import('../mesh/mesh-events.js');
2630
4602
  if (meshId) {
@@ -2656,6 +4628,15 @@ export class DaemonCommandRouter {
2656
4628
  mesh = getMesh(meshId);
2657
4629
  }
2658
4630
  if (!mesh) return { success: false, error: 'Mesh not found' };
4631
+ const meshHost = resolveMeshHostStatus(mesh);
4632
+ if (!meshHost.canOwnCoordinator) {
4633
+ return {
4634
+ success: false,
4635
+ ...buildMeshHostRequiredFailure(mesh, 'coordinator launch'),
4636
+ meshId,
4637
+ cliType,
4638
+ };
4639
+ }
2659
4640
  if (!Array.isArray(mesh.nodes) || mesh.nodes.length === 0) return { success: false, error: 'No nodes in mesh' };
2660
4641
 
2661
4642
  const requestedCoordinatorNodeId = typeof args?.coordinatorNodeId === 'string'
@@ -2675,7 +4656,16 @@ export class DaemonCommandRouter {
2675
4656
  cliType,
2676
4657
  };
2677
4658
  }
2678
- const workspace = typeof coordinatorNode.workspace === 'string' ? coordinatorNode.workspace.trim() : '';
4659
+ const sessionHostRecords = this.deps.sessionHostControl?.listSessions
4660
+ ? await this.deps.sessionHostControl.listSessions().catch(() => [])
4661
+ : [];
4662
+ const liveMeshSessions = partitionSessionHostRecords(Array.isArray(sessionHostRecords) ? sessionHostRecords : []).liveRuntimes;
4663
+ const workspace = readLiveMeshNodeWorkspace({
4664
+ meshId,
4665
+ nodeId: String(coordinatorNode.id || coordinatorNode.nodeId || preferredCoordinatorNodeId || ''),
4666
+ liveSessionRecords: liveMeshSessions,
4667
+ allowCoordinatorSession: true,
4668
+ }) || (typeof coordinatorNode.workspace === 'string' ? coordinatorNode.workspace.trim() : '');
2679
4669
  if (!workspace) return { success: false, error: 'Coordinator node workspace required', meshId, cliType };
2680
4670
  if (!cliType) {
2681
4671
  const resolved = await resolveProviderTypeFromPriority({
@@ -3020,6 +5010,30 @@ export class DaemonCommandRouter {
3020
5010
  const meshRecord = await this.getMeshForCommand(meshId, args?.inlineMesh, { preferInline: true });
3021
5011
  const mesh = meshRecord?.mesh;
3022
5012
  if (!mesh) return { success: false, error: 'Mesh not found' };
5013
+ const meshHost = resolveMeshHostStatus(mesh);
5014
+
5015
+ const refreshRequested = args?.refresh === true || args?.forceRefresh === true;
5016
+ const pendingCoordinatorEventCount = getPendingMeshCoordinatorEvents(meshId).length;
5017
+ const hadAggregateCache = this.aggregateMeshStatusCache.has(meshId);
5018
+ if (!refreshRequested && pendingCoordinatorEventCount === 0) {
5019
+ const cachedStatus = this.getCachedAggregateMeshStatus(meshId, mesh, { requireDirectPeerTruth: args?.requireDirectPeerTruth === true });
5020
+ if (cachedStatus) {
5021
+ logRepoMeshStatusDebug('return_cached', {
5022
+ meshId,
5023
+ command: 'mesh_status',
5024
+ refreshRequested,
5025
+ summary: summarizeRepoMeshStatusDebug(cachedStatus),
5026
+ });
5027
+ return cachedStatus;
5028
+ }
5029
+ }
5030
+ const refreshReason = refreshRequested
5031
+ ? 'explicit_refresh'
5032
+ : pendingCoordinatorEventCount > 0
5033
+ ? 'pending_coordinator_events'
5034
+ : hadAggregateCache
5035
+ ? 'stale_pending_cache_refresh'
5036
+ : 'cold_cache_miss';
3023
5037
 
3024
5038
  const { getMeshQueueStats, getQueue } = await import('../mesh/mesh-work-queue.js');
3025
5039
  const queue = getQueue(meshId);
@@ -3034,8 +5048,80 @@ export class DaemonCommandRouter {
3034
5048
  const liveMeshSessions = partitionSessionHostRecords(Array.isArray(sessionHostRecords) ? sessionHostRecords : []).liveRuntimes;
3035
5049
 
3036
5050
  const localMachineId = loadConfig().machineId || '';
5051
+ const requireDirectPeerTruth = args?.requireDirectPeerTruth === true;
5052
+ const directTruth = requireDirectPeerTruth
5053
+ ? await hydrateInlineMeshDirectTruth({
5054
+ mesh,
5055
+ meshSource: meshRecord.source,
5056
+ dispatchMeshCommand: this.deps.dispatchMeshCommand,
5057
+ statusInstanceId: this.deps.statusInstanceId,
5058
+ localMachineId,
5059
+ })
5060
+ : {
5061
+ directEvidenceCount: 0,
5062
+ localConfirmedCount: 0,
5063
+ peerAttemptedCount: 0,
5064
+ peerConfirmedCount: 0,
5065
+ unavailableNodeIds: [] as string[],
5066
+ };
5067
+ // Default/cached loads may not attempt a remote peer probe yet; do not surface that as
5068
+ // a direct mesh truth failure until an explicit probe attempt actually fails.
5069
+ const passivePeerTruthNotAttempted = requireDirectPeerTruth
5070
+ && !refreshRequested
5071
+ && directTruth.directEvidenceCount > 0
5072
+ && directTruth.peerAttemptedCount === 0;
5073
+ const effectiveDirectTruth = passivePeerTruthNotAttempted
5074
+ ? { ...directTruth, unavailableNodeIds: [] as string[] }
5075
+ : directTruth;
5076
+ const unavailableDirectTruthNodeIds = new Set(effectiveDirectTruth.unavailableNodeIds);
5077
+ const unavailableNodesAreOnlyRemovedWorktrees = unavailableDirectTruthNodeIds.size > 0
5078
+ && Array.isArray(mesh.nodes)
5079
+ && mesh.nodes
5080
+ .filter((node: any) => unavailableDirectTruthNodeIds.has(String(node.id || node.nodeId || '')))
5081
+ .every((node: any) => node?.isLocalWorktree === true);
5082
+ const directTruthSatisfied = !requireDirectPeerTruth
5083
+ || (effectiveDirectTruth.directEvidenceCount > 0 && (effectiveDirectTruth.unavailableNodeIds.length === 0 || unavailableNodesAreOnlyRemovedWorktrees));
5084
+ if (requireDirectPeerTruth && !directTruthSatisfied) {
5085
+ const failureResult = {
5086
+ success: false,
5087
+ code: 'mesh_direct_peer_truth_unavailable',
5088
+ error: 'Selected coordinator could not confirm direct mesh truth yet. Bootstrap inventory stays unavailable until direct mesh_status probes succeed.',
5089
+ sourceOfTruth: {
5090
+ membership: meshRecord.source === 'inline_cache'
5091
+ ? 'coordinator_inline_mesh_cache'
5092
+ : meshRecord.source === 'local_config'
5093
+ ? 'local_mesh_config'
5094
+ : 'inline_bootstrap_snapshot',
5095
+ coordinatorOwnsLiveTruth: false,
5096
+ currentStatus: 'direct_peer_truth_unavailable',
5097
+ directPeerTruth: {
5098
+ required: true,
5099
+ satisfied: false,
5100
+ directEvidenceCount: directTruth.directEvidenceCount,
5101
+ localConfirmedCount: directTruth.localConfirmedCount,
5102
+ peerAttemptedCount: directTruth.peerAttemptedCount,
5103
+ peerConfirmedCount: directTruth.peerConfirmedCount,
5104
+ unavailableNodeIds: directTruth.unavailableNodeIds,
5105
+ },
5106
+ },
5107
+ };
5108
+ logRepoMeshStatusDebug('direct_truth_unavailable', {
5109
+ meshId,
5110
+ command: 'mesh_status',
5111
+ refreshRequested,
5112
+ meshSource: meshRecord.source,
5113
+ directTruth,
5114
+ });
5115
+ return failureResult;
5116
+ }
5117
+ const directTruthUnavailableNodeIds = new Set(effectiveDirectTruth.unavailableNodeIds);
5118
+ const selectedCoordinatorNodeId = readStringValue(
5119
+ mesh.coordinator?.preferredNodeId,
5120
+ (mesh.nodes?.[0] as any)?.id,
5121
+ (mesh.nodes?.[0] as any)?.nodeId,
5122
+ );
3037
5123
  const inlineCoordinatorNodeId = meshRecord?.inline && Array.isArray(mesh.nodes)
3038
- ? readStringValue((mesh.nodes[0] as any)?.id, (mesh.nodes[0] as any)?.nodeId)
5124
+ ? selectedCoordinatorNodeId
3039
5125
  : undefined;
3040
5126
  const refreshedAt = new Date().toISOString();
3041
5127
  const nodeStatuses = [];
@@ -3050,11 +5136,15 @@ export class DaemonCommandRouter {
3050
5136
  ) || Boolean(meshRecord?.inline && nodeIndex === 0);
3051
5137
  const status: Record<string, unknown> = {
3052
5138
  nodeId,
3053
- machineLabel: node.machineLabel || node.id || node.nodeId,
5139
+ machineLabel: buildMeshNodeDisplayLabel(node as Record<string, unknown>, nodeId, providerPriority),
5140
+ labelSource: readStringValue(node.machineLabel, node.machine_label, node.machineNickname, node.machine_nickname, node.alias)
5141
+ ? 'explicit_metadata'
5142
+ : 'workspace_host_provider_context',
3054
5143
  workspace: node.workspace,
3055
5144
  repoRoot: node.repoRoot,
3056
5145
  isLocalWorktree: node.isLocalWorktree,
3057
5146
  worktreeBranch: node.worktreeBranch,
5147
+ role: normalizeMeshDaemonRole(node.role) || (meshHost.hostNodeId && nodeId === meshHost.hostNodeId ? 'host' : undefined),
3058
5148
  daemonId,
3059
5149
  machineId: node.machineId,
3060
5150
  machineStatus: node.machineStatus,
@@ -3095,8 +5185,20 @@ export class DaemonCommandRouter {
3095
5185
  reason: 'Node has no daemon id, so mesh transport cannot be reported from the selected coordinator.',
3096
5186
  };
3097
5187
  }
3098
- const matchedLiveSessionRecords = liveMeshSessions
3099
- .filter((record) => this.sessionMatchesMeshNode(record, node, nodeId));
5188
+ const matchedLiveSessionRecords = collectLiveMeshSessionRecords({
5189
+ meshId,
5190
+ node,
5191
+ nodeId,
5192
+ liveSessionRecords: liveMeshSessions,
5193
+ allowCoordinatorSession: nodeId === selectedCoordinatorNodeId,
5194
+ });
5195
+ const workspace = readLiveMeshNodeWorkspace({
5196
+ meshId,
5197
+ nodeId,
5198
+ liveSessionRecords: matchedLiveSessionRecords,
5199
+ allowCoordinatorSession: nodeId === selectedCoordinatorNodeId,
5200
+ }) || (typeof node.workspace === 'string' ? node.workspace : '');
5201
+ status.workspace = workspace || node.workspace;
3100
5202
  if (matchedLiveSessionRecords.length > 0) {
3101
5203
  const sessionIds = matchedLiveSessionRecords
3102
5204
  .map((record: any) => typeof record?.sessionId === 'string' ? record.sessionId : '')
@@ -3110,44 +5212,194 @@ export class DaemonCommandRouter {
3110
5212
  status.providers = Array.from(new Set([...(Array.isArray(status.providers) ? status.providers as string[] : []), ...providerTypes]));
3111
5213
  }
3112
5214
  }
3113
- if (node.workspace && typeof node.workspace === 'string') {
3114
- if (!fs.existsSync(node.workspace as string) && applyCachedInlineMeshNodeStatus(status, node)) {
3115
- status.launchReady = !!daemonId && (readStringValue(status.machineStatus) === 'online' || isSelfNode);
3116
- nodeStatuses.push(status);
3117
- continue;
3118
- }
3119
- try {
3120
- const gitStatus = await getGitRepoStatus(node.workspace as string, { timeoutMs: 10_000, refreshUpstream: true });
3121
- status.git = gitStatus;
3122
- if (gitStatus.isGitRepo) {
3123
- status.health = deriveMeshNodeHealthFromGit(gitStatus as unknown as Record<string, unknown>);
3124
- } else {
3125
- status.health = 'degraded';
3126
- if (gitStatus.error && !status.error) status.error = gitStatus.error;
5215
+ if (workspace) {
5216
+ if (!fs.existsSync(workspace)) {
5217
+ // Workspace not local prefer direct live inline truth, then attempt a P2P git probe.
5218
+ const inlineTransitGit = buildInlineMeshTransitGitStatus(node);
5219
+ let remoteProbeApplied = false;
5220
+ if (inlineTransitGit) {
5221
+ status.git = inlineTransitGit;
5222
+ status.health = inlineTransitGit.isGitRepo
5223
+ ? deriveMeshNodeHealthFromGit(inlineTransitGit as unknown as Record<string, unknown>)
5224
+ : 'degraded';
5225
+ const connection = readObjectRecord(status.connection);
5226
+ const connectionState = readStringValue(connection.state);
5227
+ const connectionReported = readBooleanValue(connection.reported) ?? false;
5228
+ if (!connectionReported || connectionState === 'unknown') {
5229
+ status.connection = buildLivePeerGitConnection(connection, refreshedAt);
5230
+ }
5231
+ remoteProbeApplied = true;
5232
+ } else if (!isSelfNode && daemonId && this.deps.dispatchMeshCommand && !directTruthUnavailableNodeIds.has(nodeId)) {
5233
+ try {
5234
+ const remoteGit = await probeRemoteMeshGitStatus({
5235
+ dispatchMeshCommand: this.deps.dispatchMeshCommand,
5236
+ daemonId,
5237
+ workspace,
5238
+ timeoutMs: 8000,
5239
+ });
5240
+ if (remoteGit) {
5241
+ status.git = remoteGit;
5242
+ status.health = remoteGit.isGitRepo
5243
+ ? deriveMeshNodeHealthFromGit(remoteGit as unknown as Record<string, unknown>)
5244
+ : 'degraded';
5245
+ const connection = readObjectRecord(status.connection);
5246
+ const connectionState = readStringValue(connection.state);
5247
+ const connectionReported = readBooleanValue(connection.reported) ?? false;
5248
+ if (!connectionReported || connectionState === 'unknown') {
5249
+ status.connection = buildLivePeerGitConnection(connection, refreshedAt);
5250
+ }
5251
+ recordInlineMeshDirectGitTruth(node, remoteGit, 'selected_coordinator_mesh_p2p_git');
5252
+ remoteProbeApplied = true;
5253
+ }
5254
+ } catch {
5255
+ const refreshedConnection = this.deps.getMeshPeerConnectionStatus?.(daemonId);
5256
+ const refreshedConnectionState = readStringValue(refreshedConnection?.state);
5257
+ if (refreshedConnection && refreshedConnectionState === 'connected') {
5258
+ status.connection = refreshedConnection;
5259
+ try {
5260
+ const remoteGit = await probeRemoteMeshGitStatus({
5261
+ dispatchMeshCommand: this.deps.dispatchMeshCommand,
5262
+ daemonId,
5263
+ workspace,
5264
+ timeoutMs: 12000,
5265
+ });
5266
+ if (remoteGit) {
5267
+ status.git = remoteGit;
5268
+ status.health = remoteGit.isGitRepo
5269
+ ? deriveMeshNodeHealthFromGit(remoteGit as unknown as Record<string, unknown>)
5270
+ : 'degraded';
5271
+ const connection = readObjectRecord(status.connection);
5272
+ const connectionState = readStringValue(connection.state);
5273
+ const connectionReported = readBooleanValue(connection.reported) ?? false;
5274
+ if (!connectionReported || connectionState === 'unknown') {
5275
+ status.connection = buildLivePeerGitConnection(connection, refreshedAt);
5276
+ }
5277
+ recordInlineMeshDirectGitTruth(node, remoteGit, 'selected_coordinator_mesh_p2p_git');
5278
+ remoteProbeApplied = true;
5279
+ }
5280
+ } catch {
5281
+ // Probe timed out again or P2P unavailable — fall back to cached status
5282
+ }
5283
+ }
5284
+ }
3127
5285
  }
3128
- } catch {
3129
- if (!applyCachedInlineMeshNodeStatus(status, node)) {
3130
- status.health = 'degraded';
5286
+ if (!remoteProbeApplied) {
5287
+ const connectionState = readStringValue((status.connection as any)?.state);
5288
+ const pendingPeerGitProbe = !inlineTransitGit
5289
+ && !isSelfNode
5290
+ && !!daemonId
5291
+ && (
5292
+ readStringValue(status.machineStatus) === 'online'
5293
+ || readStringValue(status.health) === 'online'
5294
+ || connectionState === 'connecting'
5295
+ || connectionState === 'connected'
5296
+ || connectionState === 'unknown'
5297
+ );
5298
+ if (pendingPeerGitProbe) {
5299
+ status.gitProbePending = true;
5300
+ status.health = 'unknown';
5301
+ }
5302
+ if (applyCachedInlineMeshNodeStatus(
5303
+ status,
5304
+ node,
5305
+ pendingPeerGitProbe ? { skipGit: true, skipError: true, skipHealth: true } : undefined,
5306
+ )) {
5307
+ applyInlineMeshBranchConvergence(mesh, node, status);
5308
+ finalizeMeshNodeStatus({ status, node, daemonId, isSelfNode });
5309
+ nodeStatuses.push(status);
5310
+ continue;
5311
+ }
5312
+ if (meshRecord?.source === 'inline_cache' && !isSelfNode) {
5313
+ applyInlineMeshBranchConvergence(mesh, node, status);
5314
+ finalizeMeshNodeStatus({ status, node, daemonId, isSelfNode });
5315
+ nodeStatuses.push(status);
5316
+ continue;
5317
+ }
5318
+ }
5319
+ } else {
5320
+ try {
5321
+ const gitStatus = await getGitRepoStatus(workspace, { timeoutMs: 10_000, refreshUpstream: true });
5322
+ status.git = gitStatus;
5323
+ recordInlineMeshDirectGitTruth(node, gitStatus as unknown as Record<string, unknown>, 'selected_coordinator_local_git');
5324
+ if (gitStatus.isGitRepo) {
5325
+ status.health = deriveMeshNodeHealthFromGit(gitStatus as unknown as Record<string, unknown>);
5326
+ } else {
5327
+ status.health = 'degraded';
5328
+ if (gitStatus.error && !status.error) status.error = gitStatus.error;
5329
+ }
5330
+ } catch {
5331
+ if (!applyCachedInlineMeshNodeStatus(status, node)) {
5332
+ status.health = 'degraded';
5333
+ }
3131
5334
  }
3132
5335
  }
3133
5336
  } else {
3134
5337
  applyCachedInlineMeshNodeStatus(status, node);
3135
5338
  }
3136
- status.launchReady = !!daemonId && (readStringValue(status.machineStatus) === 'online' || isSelfNode);
5339
+ applyInlineMeshBranchConvergence(mesh, node, status);
5340
+ finalizeMeshNodeStatus({ status, node, daemonId, isSelfNode });
3137
5341
  nodeStatuses.push(status);
3138
5342
  }
3139
5343
 
3140
- return {
5344
+ const pendingCoordinatorEvents = drainPendingMeshCoordinatorEvents(meshId);
5345
+ const statusResult = {
3141
5346
  success: true,
3142
5347
  meshId: mesh.id,
3143
5348
  meshName: mesh.name,
3144
5349
  repoIdentity: mesh.repoIdentity,
3145
5350
  defaultBranch: mesh.defaultBranch,
3146
- refreshedAt: new Date().toISOString(),
5351
+ refreshedAt,
5352
+ meshHost,
5353
+ sourceOfTruth: {
5354
+ membership: meshRecord?.source === 'inline_cache'
5355
+ ? 'coordinator_inline_mesh_cache'
5356
+ : meshRecord?.source === 'local_config'
5357
+ ? 'local_mesh_config'
5358
+ : 'inline_bootstrap_snapshot',
5359
+ coordinatorOwnsLiveTruth: directTruthSatisfied,
5360
+ meshHost: {
5361
+ owner: 'mesh_host_daemon',
5362
+ localRole: meshHost.role,
5363
+ hostDaemonId: meshHost.hostDaemonId,
5364
+ hostNodeId: meshHost.hostNodeId,
5365
+ hostAddress: meshHost.hostAddress,
5366
+ },
5367
+ ...(requireDirectPeerTruth ? {
5368
+ currentStatus: directTruthSatisfied ? 'live_git_and_session_probes' : 'direct_peer_truth_unavailable',
5369
+ directPeerTruth: {
5370
+ required: true,
5371
+ satisfied: directTruthSatisfied,
5372
+ directEvidenceCount: effectiveDirectTruth.directEvidenceCount,
5373
+ localConfirmedCount: effectiveDirectTruth.localConfirmedCount,
5374
+ peerAttemptedCount: effectiveDirectTruth.peerAttemptedCount,
5375
+ peerConfirmedCount: effectiveDirectTruth.peerConfirmedCount,
5376
+ unavailableNodeIds: effectiveDirectTruth.unavailableNodeIds,
5377
+ partialNodeFailures: effectiveDirectTruth.unavailableNodeIds,
5378
+ },
5379
+ } : {}),
5380
+ historicalEvidenceOnly: ['recoveryHints', 'ledger.summary', 'queue.summary'],
5381
+ },
5382
+ branchConvergenceSummary: summarizeInlineMeshBranchConvergence(nodeStatuses),
3147
5383
  nodes: nodeStatuses,
3148
5384
  queue: { tasks: queue, summary: queueSummary },
3149
5385
  ledger: { entries: ledgerEntries, summary: ledgerSummary },
5386
+ ...(pendingCoordinatorEvents.length > 0 ? { pendingCoordinatorEvents } : {}),
3150
5387
  };
5388
+ const { pendingCoordinatorEvents: _pendingCoordinatorEvents, ...cacheableStatusResult } = statusResult as any;
5389
+ const rememberedStatus = this.rememberAggregateMeshStatus(meshId, cacheableStatusResult, refreshReason);
5390
+ const returnedStatus = pendingCoordinatorEvents.length > 0
5391
+ ? { ...rememberedStatus, pendingCoordinatorEvents }
5392
+ : rememberedStatus;
5393
+ logRepoMeshStatusDebug('return_live', {
5394
+ meshId,
5395
+ command: 'mesh_status',
5396
+ refreshRequested,
5397
+ refreshReason,
5398
+ meshSource: meshRecord.source,
5399
+ directTruth,
5400
+ summary: summarizeRepoMeshStatusDebug(returnedStatus),
5401
+ });
5402
+ return returnedStatus;
3151
5403
  } catch (e: any) {
3152
5404
  return { success: false, error: e.message };
3153
5405
  }
@@ -3205,7 +5457,7 @@ export class DaemonCommandRouter {
3205
5457
 
3206
5458
  // 3. Kill OS process if requested
3207
5459
  if (killProcess) {
3208
- const running = isIdeRunning(ideType);
5460
+ const running = await isIdeRunning(ideType);
3209
5461
  if (running) {
3210
5462
  LOG.info('StopIDE', `Killing IDE process: ${ideType}`);
3211
5463
  const killed = await killIdeProcess(ideType);