@adhdev/daemon-standalone 0.9.67 → 0.9.68

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -27884,6 +27884,20 @@ var require_dist2 = __commonJS({
27884
27884
  mod
27885
27885
  ));
27886
27886
  var __toCommonJS2 = (mod) => __copyProps2(__defProp2({}, "__esModule", { value: true }), mod);
27887
+ var DEFAULT_MESH_POLICY;
27888
+ var init_repo_mesh_types = __esm2({
27889
+ "src/repo-mesh-types.ts"() {
27890
+ "use strict";
27891
+ DEFAULT_MESH_POLICY = {
27892
+ requirePreTaskCheckpoint: false,
27893
+ requirePostTaskCheckpoint: true,
27894
+ requireApprovalForPush: true,
27895
+ requireApprovalForDestructiveGit: true,
27896
+ dirtyWorkspaceBehavior: "warn",
27897
+ maxParallelTasks: 2
27898
+ };
27899
+ }
27900
+ });
27887
27901
  var config_exports = {};
27888
27902
  __export2(config_exports, {
27889
27903
  generateMachineId: () => generateMachineId,
@@ -28125,6 +28139,276 @@ var require_dist2 = __commonJS({
28125
28139
  MACHINE_ID_PREFIX = "mach_";
28126
28140
  }
28127
28141
  });
28142
+ var mesh_config_exports = {};
28143
+ __export2(mesh_config_exports, {
28144
+ addNode: () => addNode,
28145
+ createMesh: () => createMesh,
28146
+ deleteMesh: () => deleteMesh,
28147
+ getMesh: () => getMesh,
28148
+ getMeshByRepo: () => getMeshByRepo,
28149
+ listMeshes: () => listMeshes,
28150
+ normalizeRepoIdentity: () => normalizeRepoIdentity,
28151
+ removeNode: () => removeNode,
28152
+ updateMesh: () => updateMesh,
28153
+ updateNode: () => updateNode
28154
+ });
28155
+ function getMeshConfigPath() {
28156
+ return (0, import_path22.join)(getConfigDir(), "meshes.json");
28157
+ }
28158
+ function loadMeshConfig() {
28159
+ const path26 = getMeshConfigPath();
28160
+ if (!(0, import_fs2.existsSync)(path26)) return { meshes: [] };
28161
+ try {
28162
+ const raw = JSON.parse((0, import_fs2.readFileSync)(path26, "utf-8"));
28163
+ if (!raw || !Array.isArray(raw.meshes)) return { meshes: [] };
28164
+ return raw;
28165
+ } catch {
28166
+ return { meshes: [] };
28167
+ }
28168
+ }
28169
+ function saveMeshConfig(config2) {
28170
+ const path26 = getMeshConfigPath();
28171
+ (0, import_fs2.writeFileSync)(path26, JSON.stringify(config2, null, 2), { encoding: "utf-8", mode: 384 });
28172
+ }
28173
+ function normalizeRepoIdentity(remoteUrl) {
28174
+ let identity = remoteUrl.trim();
28175
+ if (identity.startsWith("http://") || identity.startsWith("https://")) {
28176
+ try {
28177
+ const url2 = new URL(identity);
28178
+ const path26 = url2.pathname.replace(/^\//, "").replace(/\.git$/, "");
28179
+ return `${url2.hostname}/${path26}`;
28180
+ } catch {
28181
+ }
28182
+ }
28183
+ const sshMatch = identity.match(/^(?:ssh:\/\/)?[\w.-]+@([\w.-]+)[:/]([\w.\-/]+?)(?:\.git)?$/);
28184
+ if (sshMatch) return `${sshMatch[1]}/${sshMatch[2]}`;
28185
+ return identity;
28186
+ }
28187
+ function listMeshes() {
28188
+ return loadMeshConfig().meshes;
28189
+ }
28190
+ function getMesh(meshId) {
28191
+ return loadMeshConfig().meshes.find((m) => m.id === meshId);
28192
+ }
28193
+ function getMeshByRepo(repoIdentity) {
28194
+ return loadMeshConfig().meshes.find((m) => m.repoIdentity === repoIdentity);
28195
+ }
28196
+ function createMesh(opts) {
28197
+ const config2 = loadMeshConfig();
28198
+ if (config2.meshes.length >= 20) {
28199
+ throw new Error("Maximum 20 meshes allowed");
28200
+ }
28201
+ const repoIdentity = opts.repoIdentity || (opts.repoRemoteUrl ? normalizeRepoIdentity(opts.repoRemoteUrl) : "");
28202
+ if (!repoIdentity) throw new Error("Either repoRemoteUrl or repoIdentity is required");
28203
+ const now = (/* @__PURE__ */ new Date()).toISOString();
28204
+ const mesh = {
28205
+ id: `mesh_${(0, import_crypto32.randomUUID)().replace(/-/g, "")}`,
28206
+ name: opts.name.trim().slice(0, 100),
28207
+ repoIdentity,
28208
+ repoRemoteUrl: opts.repoRemoteUrl,
28209
+ defaultBranch: opts.defaultBranch,
28210
+ policy: { ...DEFAULT_MESH_POLICY, ...opts.policy },
28211
+ coordinator: opts.coordinator || {},
28212
+ nodes: [],
28213
+ createdAt: now,
28214
+ updatedAt: now
28215
+ };
28216
+ config2.meshes.push(mesh);
28217
+ saveMeshConfig(config2);
28218
+ return mesh;
28219
+ }
28220
+ function updateMesh(meshId, opts) {
28221
+ const config2 = loadMeshConfig();
28222
+ const mesh = config2.meshes.find((m) => m.id === meshId);
28223
+ if (!mesh) return void 0;
28224
+ if (opts.name !== void 0) mesh.name = opts.name.trim().slice(0, 100);
28225
+ if (opts.defaultBranch !== void 0) mesh.defaultBranch = opts.defaultBranch;
28226
+ if (opts.policy) mesh.policy = { ...mesh.policy, ...opts.policy };
28227
+ if (opts.coordinator) mesh.coordinator = opts.coordinator;
28228
+ mesh.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
28229
+ saveMeshConfig(config2);
28230
+ return mesh;
28231
+ }
28232
+ function deleteMesh(meshId) {
28233
+ const config2 = loadMeshConfig();
28234
+ const idx = config2.meshes.findIndex((m) => m.id === meshId);
28235
+ if (idx === -1) return false;
28236
+ config2.meshes.splice(idx, 1);
28237
+ saveMeshConfig(config2);
28238
+ return true;
28239
+ }
28240
+ function addNode(meshId, opts) {
28241
+ const config2 = loadMeshConfig();
28242
+ const mesh = config2.meshes.find((m) => m.id === meshId);
28243
+ if (!mesh) return void 0;
28244
+ if (mesh.nodes.length >= 10) {
28245
+ throw new Error("Maximum 10 nodes per mesh");
28246
+ }
28247
+ if (mesh.nodes.some((n) => n.workspace === opts.workspace)) {
28248
+ throw new Error("This workspace is already in the mesh");
28249
+ }
28250
+ const node = {
28251
+ id: `node_${(0, import_crypto32.randomUUID)().replace(/-/g, "")}`,
28252
+ workspace: opts.workspace.trim(),
28253
+ repoRoot: opts.repoRoot,
28254
+ userOverrides: opts.userOverrides || {},
28255
+ policy: opts.policy || {},
28256
+ isLocalWorktree: opts.isLocalWorktree
28257
+ };
28258
+ mesh.nodes.push(node);
28259
+ mesh.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
28260
+ saveMeshConfig(config2);
28261
+ return node;
28262
+ }
28263
+ function removeNode(meshId, nodeId) {
28264
+ const config2 = loadMeshConfig();
28265
+ const mesh = config2.meshes.find((m) => m.id === meshId);
28266
+ if (!mesh) return false;
28267
+ const idx = mesh.nodes.findIndex((n) => n.id === nodeId);
28268
+ if (idx === -1) return false;
28269
+ mesh.nodes.splice(idx, 1);
28270
+ mesh.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
28271
+ saveMeshConfig(config2);
28272
+ return true;
28273
+ }
28274
+ function updateNode(meshId, nodeId, opts) {
28275
+ const config2 = loadMeshConfig();
28276
+ const mesh = config2.meshes.find((m) => m.id === meshId);
28277
+ if (!mesh) return void 0;
28278
+ const node = mesh.nodes.find((n) => n.id === nodeId);
28279
+ if (!node) return void 0;
28280
+ if (opts.userOverrides) node.userOverrides = { ...node.userOverrides, ...opts.userOverrides };
28281
+ if (opts.policy) node.policy = { ...node.policy, ...opts.policy };
28282
+ mesh.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
28283
+ saveMeshConfig(config2);
28284
+ return node;
28285
+ }
28286
+ var import_fs2;
28287
+ var import_path22;
28288
+ var import_crypto32;
28289
+ var init_mesh_config = __esm2({
28290
+ "src/config/mesh-config.ts"() {
28291
+ "use strict";
28292
+ import_fs2 = require("fs");
28293
+ import_path22 = require("path");
28294
+ import_crypto32 = require("crypto");
28295
+ init_config();
28296
+ init_repo_mesh_types();
28297
+ }
28298
+ });
28299
+ var coordinator_prompt_exports = {};
28300
+ __export2(coordinator_prompt_exports, {
28301
+ buildCoordinatorSystemPrompt: () => buildCoordinatorSystemPrompt
28302
+ });
28303
+ function buildCoordinatorSystemPrompt(ctx) {
28304
+ const { mesh, status, userInstruction } = ctx;
28305
+ const sections = [];
28306
+ sections.push(`You are a **Repo Mesh Coordinator** \u2014 a technical team lead who orchestrates work across multiple agent sessions on a shared Git repository.
28307
+
28308
+ Your mesh: **${mesh.name}**
28309
+ Repository: \`${mesh.repoIdentity}\`${mesh.defaultBranch ? `
28310
+ Default branch: \`${mesh.defaultBranch}\`` : ""}`);
28311
+ if (status?.nodes?.length) {
28312
+ sections.push(buildNodeStatusSection(status.nodes));
28313
+ } else if (mesh.nodes.length) {
28314
+ sections.push(buildNodeConfigSection(mesh));
28315
+ } else {
28316
+ sections.push("## Nodes\nNo nodes configured yet. Ask the user to add nodes with `adhdev mesh add-node`.");
28317
+ }
28318
+ sections.push(buildPolicySection(mesh.policy));
28319
+ sections.push(TOOLS_SECTION);
28320
+ sections.push(WORKFLOW_SECTION);
28321
+ sections.push(RULES_SECTION);
28322
+ if (userInstruction) {
28323
+ sections.push(`## Additional Context
28324
+ ${userInstruction}`);
28325
+ }
28326
+ if (mesh.coordinator.systemPromptSuffix) {
28327
+ sections.push(mesh.coordinator.systemPromptSuffix);
28328
+ }
28329
+ return sections.join("\n\n");
28330
+ }
28331
+ function buildNodeStatusSection(nodes) {
28332
+ const lines = ["## Current Node Status", ""];
28333
+ for (const n of nodes) {
28334
+ const healthIcon = n.health === "online" ? "\u{1F7E2}" : n.health === "dirty" ? "\u{1F7E1}" : n.health === "offline" ? "\u26AB" : "\u{1F534}";
28335
+ const sessions = n.activeSessions.length > 0 ? `sessions: ${n.activeSessions.join(", ")}` : "no active sessions";
28336
+ const branch = n.git?.branch ? `branch: \`${n.git.branch}\`` : "";
28337
+ lines.push(`- ${healthIcon} **${n.machineLabel}** (${n.nodeId})`);
28338
+ lines.push(` workspace: \`${n.workspace}\` | ${branch} | ${sessions}`);
28339
+ if (n.error) lines.push(` \u26A0\uFE0F ${n.error}`);
28340
+ }
28341
+ return lines.join("\n");
28342
+ }
28343
+ function buildNodeConfigSection(mesh) {
28344
+ const lines = ["## Configured Nodes", ""];
28345
+ for (const n of mesh.nodes) {
28346
+ const labels = [];
28347
+ if (n.isLocalWorktree) labels.push("worktree");
28348
+ if (n.policy.readOnly) labels.push("read-only");
28349
+ const suffix = labels.length ? ` [${labels.join(", ")}]` : "";
28350
+ lines.push(`- **${n.workspace}** (${n.id})${suffix}`);
28351
+ }
28352
+ lines.push("", "_Use `mesh_status` to probe live health before delegating work._");
28353
+ return lines.join("\n");
28354
+ }
28355
+ function buildPolicySection(policy) {
28356
+ const rules = [];
28357
+ if (policy.requirePreTaskCheckpoint) rules.push("- Create a git checkpoint **before** starting each task");
28358
+ if (policy.requirePostTaskCheckpoint) rules.push("- Create a git checkpoint **after** each task completes");
28359
+ if (policy.requireApprovalForPush) rules.push("- **Ask for user approval** before pushing to remote");
28360
+ if (policy.requireApprovalForDestructiveGit) rules.push("- **Ask for user approval** before destructive git operations (force push, reset, etc.)");
28361
+ const dirtyBehavior = {
28362
+ block: "- **Do not** send tasks to nodes with dirty workspaces",
28363
+ warn: "- Warn the user if a node has uncommitted changes before sending a task",
28364
+ checkpoint_then_continue: "- Auto-checkpoint dirty nodes before sending tasks"
28365
+ }[policy.dirtyWorkspaceBehavior] || "";
28366
+ if (dirtyBehavior) rules.push(dirtyBehavior);
28367
+ rules.push(`- Maximum **${policy.maxParallelTasks}** tasks running in parallel`);
28368
+ return `## Policy
28369
+ ${rules.join("\n")}`;
28370
+ }
28371
+ var TOOLS_SECTION;
28372
+ var WORKFLOW_SECTION;
28373
+ var RULES_SECTION;
28374
+ var init_coordinator_prompt = __esm2({
28375
+ "src/mesh/coordinator-prompt.ts"() {
28376
+ "use strict";
28377
+ TOOLS_SECTION = `## Available Tools
28378
+
28379
+ | Tool | Purpose |
28380
+ |------|---------|
28381
+ | \`mesh_status\` | Check all nodes' health, git state, and active sessions |
28382
+ | \`mesh_list_nodes\` | List nodes with workspace paths |
28383
+ | \`mesh_launch_session\` | Start a new agent session on a node |
28384
+ | \`mesh_send_task\` | Send a task (natural language) to a running agent |
28385
+ | \`mesh_read_chat\` | Read an agent's recent messages to check progress |
28386
+ | \`mesh_git_status\` | Check git status on a specific node |
28387
+ | \`mesh_checkpoint\` | Create a git checkpoint on a node |
28388
+ | \`mesh_approve\` | Approve/reject a pending agent action |`;
28389
+ WORKFLOW_SECTION = `## Orchestration Workflow
28390
+
28391
+ 1. **Assess** \u2014 Call \`mesh_status\` to see which nodes are healthy and available.
28392
+ 2. **Plan** \u2014 Decompose the user's request into independent tasks for parallel execution, or sequential tasks when dependencies exist.
28393
+ 3. **Delegate** \u2014 For each task:
28394
+ a. Pick the best node (consider: health, dirty state, current workload).
28395
+ b. If no session exists, call \`mesh_launch_session\` to start one.
28396
+ c. Call \`mesh_send_task\` with a clear, self-contained natural-language instruction.
28397
+ 4. **Monitor** \u2014 Periodically call \`mesh_read_chat\` to check progress. Handle approvals via \`mesh_approve\`.
28398
+ 5. **Verify** \u2014 When a task reports completion, call \`mesh_git_status\` to verify changes were made.
28399
+ 6. **Checkpoint** \u2014 Call \`mesh_checkpoint\` to save the work.
28400
+ 7. **Report** \u2014 Summarize what was done, what changed, and any issues.`;
28401
+ RULES_SECTION = `## Rules
28402
+
28403
+ - **Be conversational.** Delegate work the way a tech lead would \u2014 clear, specific instructions in natural language.
28404
+ - **Don't inspect code.** Trust the agent's output. Verify via git diff/status, not by reading source files.
28405
+ - **Don't over-parallelize.** Start with 1-2 concurrent tasks. Scale up if they succeed.
28406
+ - **Handle failures gracefully.** If a task fails, read the chat to understand why, then retry or reassign.
28407
+ - **Keep the user informed.** Report progress after each delegation round.
28408
+ - **Respect node capabilities.** Don't send build tasks to read-only nodes. Don't push from nodes that aren't allowed to.
28409
+ - **Never fabricate tool results.** Always call the actual tool; never pretend you did.`;
28410
+ }
28411
+ });
28128
28412
  function setLogLevel(level) {
28129
28413
  currentLevel = level;
28130
28414
  daemonLog("Logger", `Log level set to: ${level}`, "info");
@@ -31381,14 +31665,7 @@ var require_dist2 = __commonJS({
31381
31665
  upsertSavedProviderSession: () => upsertSavedProviderSession
31382
31666
  });
31383
31667
  module2.exports = __toCommonJS2(index_exports);
31384
- var DEFAULT_MESH_POLICY = {
31385
- requirePreTaskCheckpoint: false,
31386
- requirePostTaskCheckpoint: true,
31387
- requireApprovalForPush: true,
31388
- requireApprovalForDestructiveGit: true,
31389
- dirtyWorkspaceBehavior: "warn",
31390
- maxParallelTasks: 2
31391
- };
31668
+ init_repo_mesh_types();
31392
31669
  var import_node_child_process = require("child_process");
31393
31670
  var import_node_fs3 = require("fs");
31394
31671
  var import_promises4 = require("fs/promises");
@@ -33071,329 +33348,96 @@ var require_dist2 = __commonJS({
33071
33348
  inboxBucket: forcedInboxBucket
33072
33349
  };
33073
33350
  }
33074
- if (!currentNotificationId || !dismissedNotificationId || currentNotificationId !== dismissedNotificationId) {
33075
- return {
33076
- unread: !!session.unread,
33077
- inboxBucket: session.inboxBucket || "idle"
33078
- };
33079
- }
33080
- return {
33081
- unread: false,
33082
- inboxBucket: "idle"
33083
- };
33084
- }
33085
- function markSessionSeen(state, sessionId, seenAt = Date.now(), completionMarker, providerSessionId) {
33086
- const prev = state.sessionReads || {};
33087
- const prevMarkers = state.sessionReadMarkers || {};
33088
- const nextMarker = typeof completionMarker === "string" ? completionMarker : "";
33089
- const readKeys = Array.from(new Set([
33090
- sessionId,
33091
- buildSessionReadStateKey(sessionId, providerSessionId)
33092
- ].filter(Boolean)));
33093
- const nextSessionReads = { ...prev };
33094
- const nextSessionReadMarkers = { ...prevMarkers };
33095
- const nextSessionNotificationDismissals = { ...state.sessionNotificationDismissals || {} };
33096
- const nextSessionNotificationUnreadOverrides = { ...state.sessionNotificationUnreadOverrides || {} };
33097
- for (const key of readKeys) {
33098
- nextSessionReads[key] = Math.max(prev[key] || 0, seenAt);
33099
- if (nextMarker) nextSessionReadMarkers[key] = nextMarker;
33100
- delete nextSessionNotificationDismissals[key];
33101
- delete nextSessionNotificationUnreadOverrides[key];
33102
- }
33103
- return {
33104
- ...state,
33105
- sessionReads: nextSessionReads,
33106
- sessionReadMarkers: nextMarker ? nextSessionReadMarkers : prevMarkers,
33107
- sessionNotificationDismissals: nextSessionNotificationDismissals,
33108
- sessionNotificationUnreadOverrides: nextSessionNotificationUnreadOverrides
33109
- };
33110
- }
33111
- var path6 = __toESM2(require("path"));
33112
- var MAX_SAVED_SESSIONS = 500;
33113
- function normalizeWorkspace2(workspace) {
33114
- if (!workspace) return "";
33115
- try {
33116
- return path6.resolve(expandPath(workspace));
33117
- } catch {
33118
- return path6.resolve(workspace);
33119
- }
33120
- }
33121
- function buildSavedProviderSessionKey(providerSessionId) {
33122
- return `saved:${providerSessionId.trim()}`;
33123
- }
33124
- function upsertSavedProviderSession(state, entry) {
33125
- const providerSessionId = typeof entry.providerSessionId === "string" ? entry.providerSessionId.trim() : "";
33126
- if (!providerSessionId) return state;
33127
- const id = buildSavedProviderSessionKey(providerSessionId);
33128
- const existing = (state.savedProviderSessions || []).find((item) => item.id === id);
33129
- const nextEntry = {
33130
- id,
33131
- kind: entry.kind,
33132
- providerType: entry.providerType,
33133
- providerName: entry.providerName,
33134
- providerSessionId,
33135
- workspace: entry.workspace ? normalizeWorkspace2(entry.workspace) : void 0,
33136
- summaryMetadata: normalizePersistedSummaryMetadata({
33137
- summaryMetadata: entry.summaryMetadata
33138
- }),
33139
- title: entry.title,
33140
- createdAt: existing?.createdAt || entry.createdAt || Date.now(),
33141
- lastUsedAt: entry.lastUsedAt || Date.now()
33142
- };
33143
- const filtered = (state.savedProviderSessions || []).filter((item) => item.id !== id);
33144
- return {
33145
- ...state,
33146
- savedProviderSessions: [nextEntry, ...filtered].slice(0, MAX_SAVED_SESSIONS)
33147
- };
33148
- }
33149
- function getSavedProviderSessions(state, filters) {
33150
- return [...state.savedProviderSessions || []].filter((entry) => {
33151
- if (filters?.providerType && entry.providerType !== filters.providerType) return false;
33152
- if (filters?.kind && entry.kind !== filters.kind) return false;
33153
- return true;
33154
- }).map((entry) => ({
33155
- ...entry,
33156
- summaryMetadata: normalizePersistedSummaryMetadata({
33157
- summaryMetadata: entry.summaryMetadata
33158
- })
33159
- })).sort((a, b2) => b2.lastUsedAt - a.lastUsedAt);
33160
- }
33161
- var import_fs2 = require("fs");
33162
- var import_path22 = require("path");
33163
- var import_crypto32 = require("crypto");
33164
- init_config();
33165
- function getMeshConfigPath() {
33166
- return (0, import_path22.join)(getConfigDir(), "meshes.json");
33167
- }
33168
- function loadMeshConfig() {
33169
- const path26 = getMeshConfigPath();
33170
- if (!(0, import_fs2.existsSync)(path26)) return { meshes: [] };
33171
- try {
33172
- const raw = JSON.parse((0, import_fs2.readFileSync)(path26, "utf-8"));
33173
- if (!raw || !Array.isArray(raw.meshes)) return { meshes: [] };
33174
- return raw;
33175
- } catch {
33176
- return { meshes: [] };
33177
- }
33178
- }
33179
- function saveMeshConfig(config2) {
33180
- const path26 = getMeshConfigPath();
33181
- (0, import_fs2.writeFileSync)(path26, JSON.stringify(config2, null, 2), { encoding: "utf-8", mode: 384 });
33182
- }
33183
- function normalizeRepoIdentity(remoteUrl) {
33184
- let identity = remoteUrl.trim();
33185
- if (identity.startsWith("http://") || identity.startsWith("https://")) {
33186
- try {
33187
- const url2 = new URL(identity);
33188
- const path26 = url2.pathname.replace(/^\//, "").replace(/\.git$/, "");
33189
- return `${url2.hostname}/${path26}`;
33190
- } catch {
33191
- }
33192
- }
33193
- const sshMatch = identity.match(/^(?:ssh:\/\/)?[\w.-]+@([\w.-]+)[:/]([\w.\-/]+?)(?:\.git)?$/);
33194
- if (sshMatch) return `${sshMatch[1]}/${sshMatch[2]}`;
33195
- return identity;
33196
- }
33197
- function listMeshes() {
33198
- return loadMeshConfig().meshes;
33199
- }
33200
- function getMesh(meshId) {
33201
- return loadMeshConfig().meshes.find((m) => m.id === meshId);
33202
- }
33203
- function getMeshByRepo(repoIdentity) {
33204
- return loadMeshConfig().meshes.find((m) => m.repoIdentity === repoIdentity);
33205
- }
33206
- function createMesh(opts) {
33207
- const config2 = loadMeshConfig();
33208
- if (config2.meshes.length >= 20) {
33209
- throw new Error("Maximum 20 meshes allowed");
33210
- }
33211
- const repoIdentity = opts.repoIdentity || (opts.repoRemoteUrl ? normalizeRepoIdentity(opts.repoRemoteUrl) : "");
33212
- if (!repoIdentity) throw new Error("Either repoRemoteUrl or repoIdentity is required");
33213
- const now = (/* @__PURE__ */ new Date()).toISOString();
33214
- const mesh = {
33215
- id: `mesh_${(0, import_crypto32.randomUUID)().replace(/-/g, "")}`,
33216
- name: opts.name.trim().slice(0, 100),
33217
- repoIdentity,
33218
- repoRemoteUrl: opts.repoRemoteUrl,
33219
- defaultBranch: opts.defaultBranch,
33220
- policy: { ...DEFAULT_MESH_POLICY, ...opts.policy },
33221
- coordinator: opts.coordinator || {},
33222
- nodes: [],
33223
- createdAt: now,
33224
- updatedAt: now
33225
- };
33226
- config2.meshes.push(mesh);
33227
- saveMeshConfig(config2);
33228
- return mesh;
33229
- }
33230
- function updateMesh(meshId, opts) {
33231
- const config2 = loadMeshConfig();
33232
- const mesh = config2.meshes.find((m) => m.id === meshId);
33233
- if (!mesh) return void 0;
33234
- if (opts.name !== void 0) mesh.name = opts.name.trim().slice(0, 100);
33235
- if (opts.defaultBranch !== void 0) mesh.defaultBranch = opts.defaultBranch;
33236
- if (opts.policy) mesh.policy = { ...mesh.policy, ...opts.policy };
33237
- if (opts.coordinator) mesh.coordinator = opts.coordinator;
33238
- mesh.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
33239
- saveMeshConfig(config2);
33240
- return mesh;
33241
- }
33242
- function deleteMesh(meshId) {
33243
- const config2 = loadMeshConfig();
33244
- const idx = config2.meshes.findIndex((m) => m.id === meshId);
33245
- if (idx === -1) return false;
33246
- config2.meshes.splice(idx, 1);
33247
- saveMeshConfig(config2);
33248
- return true;
33249
- }
33250
- function addNode(meshId, opts) {
33251
- const config2 = loadMeshConfig();
33252
- const mesh = config2.meshes.find((m) => m.id === meshId);
33253
- if (!mesh) return void 0;
33254
- if (mesh.nodes.length >= 10) {
33255
- throw new Error("Maximum 10 nodes per mesh");
33256
- }
33257
- if (mesh.nodes.some((n) => n.workspace === opts.workspace)) {
33258
- throw new Error("This workspace is already in the mesh");
33351
+ if (!currentNotificationId || !dismissedNotificationId || currentNotificationId !== dismissedNotificationId) {
33352
+ return {
33353
+ unread: !!session.unread,
33354
+ inboxBucket: session.inboxBucket || "idle"
33355
+ };
33259
33356
  }
33260
- const node = {
33261
- id: `node_${(0, import_crypto32.randomUUID)().replace(/-/g, "")}`,
33262
- workspace: opts.workspace.trim(),
33263
- repoRoot: opts.repoRoot,
33264
- userOverrides: opts.userOverrides || {},
33265
- policy: opts.policy || {},
33266
- isLocalWorktree: opts.isLocalWorktree
33357
+ return {
33358
+ unread: false,
33359
+ inboxBucket: "idle"
33267
33360
  };
33268
- mesh.nodes.push(node);
33269
- mesh.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
33270
- saveMeshConfig(config2);
33271
- return node;
33272
- }
33273
- function removeNode(meshId, nodeId) {
33274
- const config2 = loadMeshConfig();
33275
- const mesh = config2.meshes.find((m) => m.id === meshId);
33276
- if (!mesh) return false;
33277
- const idx = mesh.nodes.findIndex((n) => n.id === nodeId);
33278
- if (idx === -1) return false;
33279
- mesh.nodes.splice(idx, 1);
33280
- mesh.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
33281
- saveMeshConfig(config2);
33282
- return true;
33283
33361
  }
33284
- function updateNode(meshId, nodeId, opts) {
33285
- const config2 = loadMeshConfig();
33286
- const mesh = config2.meshes.find((m) => m.id === meshId);
33287
- if (!mesh) return void 0;
33288
- const node = mesh.nodes.find((n) => n.id === nodeId);
33289
- if (!node) return void 0;
33290
- if (opts.userOverrides) node.userOverrides = { ...node.userOverrides, ...opts.userOverrides };
33291
- if (opts.policy) node.policy = { ...node.policy, ...opts.policy };
33292
- mesh.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
33293
- saveMeshConfig(config2);
33294
- return node;
33295
- }
33296
- function buildCoordinatorSystemPrompt(ctx) {
33297
- const { mesh, status, userInstruction } = ctx;
33298
- const sections = [];
33299
- sections.push(`You are a **Repo Mesh Coordinator** \u2014 a technical team lead who orchestrates work across multiple agent sessions on a shared Git repository.
33300
-
33301
- Your mesh: **${mesh.name}**
33302
- Repository: \`${mesh.repoIdentity}\`${mesh.defaultBranch ? `
33303
- Default branch: \`${mesh.defaultBranch}\`` : ""}`);
33304
- if (status?.nodes?.length) {
33305
- sections.push(buildNodeStatusSection(status.nodes));
33306
- } else if (mesh.nodes.length) {
33307
- sections.push(buildNodeConfigSection(mesh));
33308
- } else {
33309
- sections.push("## Nodes\nNo nodes configured yet. Ask the user to add nodes with `adhdev mesh add-node`.");
33310
- }
33311
- sections.push(buildPolicySection(mesh.policy));
33312
- sections.push(TOOLS_SECTION);
33313
- sections.push(WORKFLOW_SECTION);
33314
- sections.push(RULES_SECTION);
33315
- if (userInstruction) {
33316
- sections.push(`## Additional Context
33317
- ${userInstruction}`);
33318
- }
33319
- if (mesh.coordinator.systemPromptSuffix) {
33320
- sections.push(mesh.coordinator.systemPromptSuffix);
33362
+ function markSessionSeen(state, sessionId, seenAt = Date.now(), completionMarker, providerSessionId) {
33363
+ const prev = state.sessionReads || {};
33364
+ const prevMarkers = state.sessionReadMarkers || {};
33365
+ const nextMarker = typeof completionMarker === "string" ? completionMarker : "";
33366
+ const readKeys = Array.from(new Set([
33367
+ sessionId,
33368
+ buildSessionReadStateKey(sessionId, providerSessionId)
33369
+ ].filter(Boolean)));
33370
+ const nextSessionReads = { ...prev };
33371
+ const nextSessionReadMarkers = { ...prevMarkers };
33372
+ const nextSessionNotificationDismissals = { ...state.sessionNotificationDismissals || {} };
33373
+ const nextSessionNotificationUnreadOverrides = { ...state.sessionNotificationUnreadOverrides || {} };
33374
+ for (const key of readKeys) {
33375
+ nextSessionReads[key] = Math.max(prev[key] || 0, seenAt);
33376
+ if (nextMarker) nextSessionReadMarkers[key] = nextMarker;
33377
+ delete nextSessionNotificationDismissals[key];
33378
+ delete nextSessionNotificationUnreadOverrides[key];
33321
33379
  }
33322
- return sections.join("\n\n");
33380
+ return {
33381
+ ...state,
33382
+ sessionReads: nextSessionReads,
33383
+ sessionReadMarkers: nextMarker ? nextSessionReadMarkers : prevMarkers,
33384
+ sessionNotificationDismissals: nextSessionNotificationDismissals,
33385
+ sessionNotificationUnreadOverrides: nextSessionNotificationUnreadOverrides
33386
+ };
33323
33387
  }
33324
- function buildNodeStatusSection(nodes) {
33325
- const lines = ["## Current Node Status", ""];
33326
- for (const n of nodes) {
33327
- const healthIcon = n.health === "online" ? "\u{1F7E2}" : n.health === "dirty" ? "\u{1F7E1}" : n.health === "offline" ? "\u26AB" : "\u{1F534}";
33328
- const sessions = n.activeSessions.length > 0 ? `sessions: ${n.activeSessions.join(", ")}` : "no active sessions";
33329
- const branch = n.git?.branch ? `branch: \`${n.git.branch}\`` : "";
33330
- lines.push(`- ${healthIcon} **${n.machineLabel}** (${n.nodeId})`);
33331
- lines.push(` workspace: \`${n.workspace}\` | ${branch} | ${sessions}`);
33332
- if (n.error) lines.push(` \u26A0\uFE0F ${n.error}`);
33388
+ var path6 = __toESM2(require("path"));
33389
+ var MAX_SAVED_SESSIONS = 500;
33390
+ function normalizeWorkspace2(workspace) {
33391
+ if (!workspace) return "";
33392
+ try {
33393
+ return path6.resolve(expandPath(workspace));
33394
+ } catch {
33395
+ return path6.resolve(workspace);
33333
33396
  }
33334
- return lines.join("\n");
33335
33397
  }
33336
- function buildNodeConfigSection(mesh) {
33337
- const lines = ["## Configured Nodes", ""];
33338
- for (const n of mesh.nodes) {
33339
- const labels = [];
33340
- if (n.isLocalWorktree) labels.push("worktree");
33341
- if (n.policy.readOnly) labels.push("read-only");
33342
- const suffix = labels.length ? ` [${labels.join(", ")}]` : "";
33343
- lines.push(`- **${n.workspace}** (${n.id})${suffix}`);
33344
- }
33345
- lines.push("", "_Use `mesh_status` to probe live health before delegating work._");
33346
- return lines.join("\n");
33398
+ function buildSavedProviderSessionKey(providerSessionId) {
33399
+ return `saved:${providerSessionId.trim()}`;
33347
33400
  }
33348
- function buildPolicySection(policy) {
33349
- const rules = [];
33350
- if (policy.requirePreTaskCheckpoint) rules.push("- Create a git checkpoint **before** starting each task");
33351
- if (policy.requirePostTaskCheckpoint) rules.push("- Create a git checkpoint **after** each task completes");
33352
- if (policy.requireApprovalForPush) rules.push("- **Ask for user approval** before pushing to remote");
33353
- if (policy.requireApprovalForDestructiveGit) rules.push("- **Ask for user approval** before destructive git operations (force push, reset, etc.)");
33354
- const dirtyBehavior = {
33355
- block: "- **Do not** send tasks to nodes with dirty workspaces",
33356
- warn: "- Warn the user if a node has uncommitted changes before sending a task",
33357
- checkpoint_then_continue: "- Auto-checkpoint dirty nodes before sending tasks"
33358
- }[policy.dirtyWorkspaceBehavior] || "";
33359
- if (dirtyBehavior) rules.push(dirtyBehavior);
33360
- rules.push(`- Maximum **${policy.maxParallelTasks}** tasks running in parallel`);
33361
- return `## Policy
33362
- ${rules.join("\n")}`;
33401
+ function upsertSavedProviderSession(state, entry) {
33402
+ const providerSessionId = typeof entry.providerSessionId === "string" ? entry.providerSessionId.trim() : "";
33403
+ if (!providerSessionId) return state;
33404
+ const id = buildSavedProviderSessionKey(providerSessionId);
33405
+ const existing = (state.savedProviderSessions || []).find((item) => item.id === id);
33406
+ const nextEntry = {
33407
+ id,
33408
+ kind: entry.kind,
33409
+ providerType: entry.providerType,
33410
+ providerName: entry.providerName,
33411
+ providerSessionId,
33412
+ workspace: entry.workspace ? normalizeWorkspace2(entry.workspace) : void 0,
33413
+ summaryMetadata: normalizePersistedSummaryMetadata({
33414
+ summaryMetadata: entry.summaryMetadata
33415
+ }),
33416
+ title: entry.title,
33417
+ createdAt: existing?.createdAt || entry.createdAt || Date.now(),
33418
+ lastUsedAt: entry.lastUsedAt || Date.now()
33419
+ };
33420
+ const filtered = (state.savedProviderSessions || []).filter((item) => item.id !== id);
33421
+ return {
33422
+ ...state,
33423
+ savedProviderSessions: [nextEntry, ...filtered].slice(0, MAX_SAVED_SESSIONS)
33424
+ };
33363
33425
  }
33364
- var TOOLS_SECTION = `## Available Tools
33365
-
33366
- | Tool | Purpose |
33367
- |------|---------|
33368
- | \`mesh_status\` | Check all nodes' health, git state, and active sessions |
33369
- | \`mesh_list_nodes\` | List nodes with workspace paths |
33370
- | \`mesh_launch_session\` | Start a new agent session on a node |
33371
- | \`mesh_send_task\` | Send a task (natural language) to a running agent |
33372
- | \`mesh_read_chat\` | Read an agent's recent messages to check progress |
33373
- | \`mesh_git_status\` | Check git status on a specific node |
33374
- | \`mesh_checkpoint\` | Create a git checkpoint on a node |
33375
- | \`mesh_approve\` | Approve/reject a pending agent action |`;
33376
- var WORKFLOW_SECTION = `## Orchestration Workflow
33377
-
33378
- 1. **Assess** \u2014 Call \`mesh_status\` to see which nodes are healthy and available.
33379
- 2. **Plan** \u2014 Decompose the user's request into independent tasks for parallel execution, or sequential tasks when dependencies exist.
33380
- 3. **Delegate** \u2014 For each task:
33381
- a. Pick the best node (consider: health, dirty state, current workload).
33382
- b. If no session exists, call \`mesh_launch_session\` to start one.
33383
- c. Call \`mesh_send_task\` with a clear, self-contained natural-language instruction.
33384
- 4. **Monitor** \u2014 Periodically call \`mesh_read_chat\` to check progress. Handle approvals via \`mesh_approve\`.
33385
- 5. **Verify** \u2014 When a task reports completion, call \`mesh_git_status\` to verify changes were made.
33386
- 6. **Checkpoint** \u2014 Call \`mesh_checkpoint\` to save the work.
33387
- 7. **Report** \u2014 Summarize what was done, what changed, and any issues.`;
33388
- var RULES_SECTION = `## Rules
33389
-
33390
- - **Be conversational.** Delegate work the way a tech lead would \u2014 clear, specific instructions in natural language.
33391
- - **Don't inspect code.** Trust the agent's output. Verify via git diff/status, not by reading source files.
33392
- - **Don't over-parallelize.** Start with 1-2 concurrent tasks. Scale up if they succeed.
33393
- - **Handle failures gracefully.** If a task fails, read the chat to understand why, then retry or reassign.
33394
- - **Keep the user informed.** Report progress after each delegation round.
33395
- - **Respect node capabilities.** Don't send build tasks to read-only nodes. Don't push from nodes that aren't allowed to.
33396
- - **Never fabricate tool results.** Always call the actual tool; never pretend you did.`;
33426
+ function getSavedProviderSessions(state, filters) {
33427
+ return [...state.savedProviderSessions || []].filter((entry) => {
33428
+ if (filters?.providerType && entry.providerType !== filters.providerType) return false;
33429
+ if (filters?.kind && entry.kind !== filters.kind) return false;
33430
+ return true;
33431
+ }).map((entry) => ({
33432
+ ...entry,
33433
+ summaryMetadata: normalizePersistedSummaryMetadata({
33434
+ summaryMetadata: entry.summaryMetadata
33435
+ })
33436
+ })).sort((a, b2) => b2.lastUsedAt - a.lastUsedAt);
33437
+ }
33438
+ init_mesh_config();
33439
+ init_coordinator_prompt();
33440
+ init_mesh_config();
33397
33441
  async function syncMeshes(transport) {
33398
33442
  const result = { pushed: 0, pulled: 0, deleted: 0, errors: [] };
33399
33443
  let remoteMeshes;
@@ -48914,6 +48958,142 @@ Run 'adhdev doctor' for detailed diagnostics.`
48914
48958
  updateConfig({ machineNickname: nickname || null });
48915
48959
  return { success: true };
48916
48960
  }
48961
+ // ─── Mesh CRUD (local meshes.json) ───
48962
+ case "list_meshes": {
48963
+ try {
48964
+ const { listMeshes: listMeshes2 } = await Promise.resolve().then(() => (init_mesh_config(), mesh_config_exports));
48965
+ return { success: true, meshes: listMeshes2() };
48966
+ } catch (e) {
48967
+ return { success: false, error: e.message };
48968
+ }
48969
+ }
48970
+ case "get_mesh": {
48971
+ const meshId = typeof args?.meshId === "string" ? args.meshId.trim() : "";
48972
+ if (!meshId) return { success: false, error: "meshId required" };
48973
+ try {
48974
+ const { getMesh: getMesh3 } = await Promise.resolve().then(() => (init_mesh_config(), mesh_config_exports));
48975
+ const mesh = getMesh3(meshId);
48976
+ if (!mesh) return { success: false, error: "Mesh not found" };
48977
+ return { success: true, mesh };
48978
+ } catch (e) {
48979
+ return { success: false, error: e.message };
48980
+ }
48981
+ }
48982
+ case "create_mesh": {
48983
+ const name = typeof args?.name === "string" ? args.name.trim() : "";
48984
+ const repoIdentity = typeof args?.repoIdentity === "string" ? args.repoIdentity.trim() : "";
48985
+ const repoRemoteUrl = typeof args?.repoRemoteUrl === "string" ? args.repoRemoteUrl.trim() : void 0;
48986
+ const defaultBranch = typeof args?.defaultBranch === "string" ? args.defaultBranch.trim() : void 0;
48987
+ if (!name) return { success: false, error: "name required" };
48988
+ try {
48989
+ const { createMesh: createMesh2 } = await Promise.resolve().then(() => (init_mesh_config(), mesh_config_exports));
48990
+ const mesh = createMesh2({ name, repoIdentity, repoRemoteUrl, defaultBranch });
48991
+ return { success: true, mesh };
48992
+ } catch (e) {
48993
+ return { success: false, error: e.message };
48994
+ }
48995
+ }
48996
+ case "delete_mesh": {
48997
+ const meshId = typeof args?.meshId === "string" ? args.meshId.trim() : "";
48998
+ if (!meshId) return { success: false, error: "meshId required" };
48999
+ try {
49000
+ const { deleteMesh: deleteMesh3 } = await Promise.resolve().then(() => (init_mesh_config(), mesh_config_exports));
49001
+ const deleted = deleteMesh3(meshId);
49002
+ return { success: true, deleted };
49003
+ } catch (e) {
49004
+ return { success: false, error: e.message };
49005
+ }
49006
+ }
49007
+ case "add_mesh_node": {
49008
+ const meshId = typeof args?.meshId === "string" ? args.meshId.trim() : "";
49009
+ const workspace = typeof args?.workspace === "string" ? args.workspace.trim() : "";
49010
+ if (!meshId) return { success: false, error: "meshId required" };
49011
+ if (!workspace) return { success: false, error: "workspace required" };
49012
+ try {
49013
+ const { addNode: addNode3 } = await Promise.resolve().then(() => (init_mesh_config(), mesh_config_exports));
49014
+ const node = addNode3(meshId, { workspace });
49015
+ if (!node) return { success: false, error: "Mesh not found" };
49016
+ return { success: true, node };
49017
+ } catch (e) {
49018
+ return { success: false, error: e.message };
49019
+ }
49020
+ }
49021
+ case "remove_mesh_node": {
49022
+ const meshId = typeof args?.meshId === "string" ? args.meshId.trim() : "";
49023
+ const nodeId = typeof args?.nodeId === "string" ? args.nodeId.trim() : "";
49024
+ if (!meshId || !nodeId) return { success: false, error: "meshId and nodeId required" };
49025
+ try {
49026
+ const { removeNode: removeNode3 } = await Promise.resolve().then(() => (init_mesh_config(), mesh_config_exports));
49027
+ const removed = removeNode3(meshId, nodeId);
49028
+ return { success: true, removed };
49029
+ } catch (e) {
49030
+ return { success: false, error: e.message };
49031
+ }
49032
+ }
49033
+ // ─── Mesh Coordinator Launch ───
49034
+ case "launch_mesh_coordinator": {
49035
+ const meshId = typeof args?.meshId === "string" ? args.meshId.trim() : "";
49036
+ const cliType = typeof args?.cliType === "string" ? args.cliType.trim() : "claude-cli";
49037
+ if (!meshId) return { success: false, error: "meshId required" };
49038
+ try {
49039
+ const { getMesh: getMesh3 } = await Promise.resolve().then(() => (init_mesh_config(), mesh_config_exports));
49040
+ const { buildCoordinatorSystemPrompt: buildCoordinatorSystemPrompt2 } = await Promise.resolve().then(() => (init_coordinator_prompt(), coordinator_prompt_exports));
49041
+ const mesh = getMesh3(meshId);
49042
+ if (!mesh) return { success: false, error: "Mesh not found" };
49043
+ if (mesh.nodes.length === 0) return { success: false, error: "No nodes in mesh" };
49044
+ const workspace = mesh.nodes[0].workspace;
49045
+ const { existsSync: existsSync21, readFileSync: readFileSync15, writeFileSync: writeFileSync12, copyFileSync: copyFileSync3 } = await import("fs");
49046
+ const { join: join23 } = await import("path");
49047
+ const mcpConfigPath = join23(workspace, ".mcp.json");
49048
+ const hadExistingMcpConfig = existsSync21(mcpConfigPath);
49049
+ let existingMcpConfig = {};
49050
+ if (hadExistingMcpConfig) {
49051
+ try {
49052
+ existingMcpConfig = JSON.parse(readFileSync15(mcpConfigPath, "utf-8"));
49053
+ copyFileSync3(mcpConfigPath, mcpConfigPath + ".backup");
49054
+ } catch {
49055
+ }
49056
+ }
49057
+ const mcpConfig = {
49058
+ ...existingMcpConfig,
49059
+ mcpServers: {
49060
+ ...existingMcpConfig.mcpServers || {},
49061
+ "adhdev-mesh": {
49062
+ command: "adhdev-mcp",
49063
+ args: ["--repo-mesh", meshId]
49064
+ }
49065
+ }
49066
+ };
49067
+ writeFileSync12(mcpConfigPath, JSON.stringify(mcpConfig, null, 2), "utf-8");
49068
+ LOG2.info("MeshCoordinator", `Wrote .mcp.json to ${workspace} with adhdev-mesh server`);
49069
+ let systemPrompt = "";
49070
+ try {
49071
+ systemPrompt = buildCoordinatorSystemPrompt2({ mesh });
49072
+ } catch {
49073
+ systemPrompt = `You are a Repo Mesh Coordinator for "${mesh.name}". Use the adhdev-mesh MCP tools (mesh_status, mesh_list_nodes, mesh_send_task, mesh_read_chat, mesh_launch_session, etc.) to orchestrate work across ${mesh.nodes.length} node(s).`;
49074
+ }
49075
+ const launchResult = await this.deps.cliManager.handleCliCommand("launch_cli", {
49076
+ cliType,
49077
+ dir: workspace,
49078
+ initialPrompt: systemPrompt
49079
+ });
49080
+ if (!launchResult?.success) {
49081
+ return { success: false, error: launchResult?.error || "Failed to launch CLI session" };
49082
+ }
49083
+ LOG2.info("MeshCoordinator", `Launched ${cliType} coordinator for mesh ${meshId} in ${workspace}`);
49084
+ return {
49085
+ success: true,
49086
+ meshId,
49087
+ cliType,
49088
+ workspace,
49089
+ sessionId: launchResult.sessionId || launchResult.id,
49090
+ mcpConfigWritten: true
49091
+ };
49092
+ } catch (e) {
49093
+ LOG2.error("MeshCoordinator", `Failed: ${e.message}`);
49094
+ return { success: false, error: e.message };
49095
+ }
49096
+ }
48917
49097
  default:
48918
49098
  break;
48919
49099
  }