@adhdev/daemon-standalone 0.9.77-rc.2 → 0.9.77-rc.21

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
@@ -32532,13 +32532,15 @@ Follow these recovery rules:
32532
32532
  }
32533
32533
  function tryAssignQueueTask(components, meshId, nodeId, sessionId, providerType) {
32534
32534
  const task = claimNextTask(meshId, nodeId, sessionId);
32535
- if (!task) return false;
32535
+ if (!task) {
32536
+ return false;
32537
+ }
32536
32538
  LOG2.info("MeshQueue", `Node ${nodeId} (${sessionId}) pulled task ${task.id}`);
32537
32539
  components.cliManager.handleCliCommand("agent_command", {
32538
32540
  targetSessionId: sessionId,
32539
32541
  cliType: providerType,
32540
32542
  action: "send_chat",
32541
- input: task.message
32543
+ message: task.message
32542
32544
  }).catch((e) => {
32543
32545
  LOG2.error("MeshQueue", `Failed to dispatch task to node ${nodeId}: ${e?.message}`);
32544
32546
  });
@@ -32610,7 +32612,7 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
32610
32612
  function injectMeshSystemMessage(components, args) {
32611
32613
  if (args.event === "agent:generating_completed") {
32612
32614
  const sessionId = readNonEmptyString(args.metadataEvent.targetSessionId);
32613
- const nodeId = readNonEmptyString(args.metadataEvent.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId);
32615
+ const nodeId = readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId);
32614
32616
  const providerType = readNonEmptyString(args.metadataEvent.providerType);
32615
32617
  if (sessionId) {
32616
32618
  updateSessionTaskStatus(args.meshId, sessionId, "completed");
@@ -32620,6 +32622,15 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
32620
32622
  }, 500);
32621
32623
  }
32622
32624
  }
32625
+ } else if (args.event === "agent:ready") {
32626
+ const sessionId = readNonEmptyString(args.metadataEvent.targetSessionId);
32627
+ const nodeId = readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId);
32628
+ const providerType = readNonEmptyString(args.metadataEvent.providerType);
32629
+ if (sessionId && nodeId && providerType) {
32630
+ setTimeout(() => {
32631
+ tryAssignQueueTask(components, args.meshId, nodeId, sessionId, providerType);
32632
+ }, 500);
32633
+ }
32623
32634
  } else if (args.event === "agent:stopped") {
32624
32635
  const sessionId = readNonEmptyString(args.metadataEvent.targetSessionId);
32625
32636
  if (sessionId) {
@@ -32631,7 +32642,7 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
32631
32642
  try {
32632
32643
  appendLedgerEntry(args.meshId, {
32633
32644
  kind: ledgerKind,
32634
- nodeId: readNonEmptyString(args.metadataEvent.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId) || void 0,
32645
+ nodeId: readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId) || void 0,
32635
32646
  sessionId: readNonEmptyString(args.metadataEvent.targetSessionId) || void 0,
32636
32647
  providerType: readNonEmptyString(args.metadataEvent.providerType) || void 0,
32637
32648
  payload: {
@@ -32651,7 +32662,7 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
32651
32662
  const maxRetries = mesh?.policy?.maxTaskRetries ?? 1;
32652
32663
  recoveryContext = getSessionRecoveryContext(args.meshId, {
32653
32664
  sessionId: readNonEmptyString(args.metadataEvent.targetSessionId) || void 0,
32654
- nodeId: readNonEmptyString(args.metadataEvent.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId) || void 0,
32665
+ nodeId: readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId) || void 0,
32655
32666
  maxRetries
32656
32667
  });
32657
32668
  recoveryContext.failedProviderType = readNonEmptyString(args.metadataEvent.providerType) || null;
@@ -32746,6 +32757,7 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
32746
32757
  const nodeLabel = nodeId ? `Node '${nodeId}'` : workspace ? `Agent at ${workspace}` : "Remote agent";
32747
32758
  return injectMeshSystemMessage(components, {
32748
32759
  meshId,
32760
+ nodeId,
32749
32761
  nodeLabel,
32750
32762
  event: eventName,
32751
32763
  metadataEvent: {
@@ -32775,10 +32787,12 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
32775
32787
  if (!meshId) return;
32776
32788
  const targetNode = mesh?.nodes?.find((n) => n.workspace === workspace);
32777
32789
  const runtimeNodeId = readNonEmptyString(settings.meshNodeId);
32790
+ const resolvedNodeId = targetNode?.id || runtimeNodeId;
32778
32791
  const nodeLabel = targetNode ? `Node '${targetNode.id}'` : runtimeNodeId ? `Node '${runtimeNodeId}'` : `Agent at ${workspace}`;
32779
32792
  injectMeshSystemMessage(components, {
32780
32793
  meshId,
32781
32794
  sourceInstanceId: instanceId,
32795
+ nodeId: resolvedNodeId,
32782
32796
  nodeLabel,
32783
32797
  event: event.event,
32784
32798
  metadataEvent: event
@@ -32802,6 +32816,7 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
32802
32816
  "agent:generating_completed",
32803
32817
  "agent:waiting_approval",
32804
32818
  "agent:stopped",
32819
+ "agent:ready",
32805
32820
  "monitor:long_generating"
32806
32821
  ]);
32807
32822
  EVENT_TO_LEDGER_KIND = {
@@ -33914,6 +33929,8 @@ Do NOT retry on this node. Consider reassigning to a different node or asking th
33914
33929
  statusHistory = [];
33915
33930
  // ─── CLI Scripts (script-based parsing) ───
33916
33931
  cliScripts;
33932
+ /** Per-session opaque state object created by cliScripts.createState(), reset on stop. */
33933
+ scriptState = null;
33917
33934
  runtimeSettings = {};
33918
33935
  /** Full accumulated rendered PTY transcript for parser/readback use */
33919
33936
  accumulatedBuffer = "";
@@ -34095,6 +34112,7 @@ ${lastSnapshot}`;
34095
34112
  this.cliScripts = scripts;
34096
34113
  this.parsedStatusCache = null;
34097
34114
  this.parseErrorMessage = null;
34115
+ this.scriptState = typeof scripts.createState === "function" ? scripts.createState() : null;
34098
34116
  const scriptNames = listCliScriptNames(scripts);
34099
34117
  LOG2.info("CLI", `[${this.cliType}] CLI scripts injected: [${scriptNames.join(", ")}]`);
34100
34118
  }
@@ -34212,6 +34230,7 @@ ${lastSnapshot}`;
34212
34230
  this.ready = false;
34213
34231
  this.startupParseGate = false;
34214
34232
  this.spawnAt = 0;
34233
+ this.scriptState = null;
34215
34234
  this.onStatusChange?.();
34216
34235
  });
34217
34236
  this.spawnAt = Date.now();
@@ -34985,7 +35004,7 @@ ${lastSnapshot}`;
34985
35004
  scope: this.currentTurnScope,
34986
35005
  runtimeSettings: this.runtimeSettings
34987
35006
  });
34988
- const session = this.cliScripts.parseSession({ ...input, tail, tailScreen: buildCliScreenSnapshot(tail) });
35007
+ const session = this.cliScripts.parseSession(this.scriptState, { ...input, tail, tailScreen: buildCliScreenSnapshot(tail) });
34989
35008
  this.parseErrorMessage = null;
34990
35009
  return session && typeof session === "object" ? session : null;
34991
35010
  } catch (e) {
@@ -34999,7 +35018,7 @@ ${lastSnapshot}`;
34999
35018
  if (!this.cliScripts?.detectStatus) return null;
35000
35019
  try {
35001
35020
  const screenText = this.terminalScreen.getText();
35002
- const status = this.cliScripts.detectStatus({
35021
+ const status = this.cliScripts.detectStatus(this.scriptState, {
35003
35022
  tail: text.slice(-500),
35004
35023
  screenText,
35005
35024
  rawBuffer: this.accumulatedRawBuffer,
@@ -35018,7 +35037,7 @@ ${lastSnapshot}`;
35018
35037
  try {
35019
35038
  const screenText = this.terminalScreen.getText();
35020
35039
  const buffer = screenText || this.accumulatedBuffer;
35021
- return this.cliScripts.parseApproval({
35040
+ return this.cliScripts.parseApproval(this.scriptState, {
35022
35041
  buffer,
35023
35042
  screenText,
35024
35043
  rawBuffer: this.accumulatedRawBuffer,
@@ -35126,7 +35145,7 @@ ${lastSnapshot}`;
35126
35145
  scope: this.currentTurnScope,
35127
35146
  runtimeSettings: this.runtimeSettings
35128
35147
  });
35129
- return await Promise.resolve(fn2({
35148
+ return await Promise.resolve(fn2(this.scriptState, {
35130
35149
  ...input,
35131
35150
  args: args && typeof args === "object" ? { ...args } : {}
35132
35151
  }));
@@ -46296,11 +46315,13 @@ ${effect.notification.body || ""}`.trim();
46296
46315
  async function handlePtyInput(h, args) {
46297
46316
  const { cliType, data, targetSessionId } = args || {};
46298
46317
  if (!data) return { success: false, error: "data required" };
46318
+ const cleanData = typeof data === "string" ? data.replace(/\x1b\[[?>][0-9;]*c/g, "") : data;
46319
+ if (!cleanData) return { success: true };
46299
46320
  const adapter = h.getCliAdapter(targetSessionId || cliType);
46300
46321
  if (!adapter || typeof adapter.writeRaw !== "function") {
46301
46322
  return { success: false, error: `CLI adapter not found: ${targetSessionId || cliType || "unknown"}` };
46302
46323
  }
46303
- await adapter.writeRaw(data);
46324
+ await adapter.writeRaw(cleanData);
46304
46325
  return { success: true };
46305
46326
  }
46306
46327
  function handlePtyResize(_h, args) {
@@ -47918,6 +47939,8 @@ ${effect.notification.body || ""}`.trim();
47918
47939
  this.completedDebounceTimer = null;
47919
47940
  }, 3e3);
47920
47941
  }
47942
+ } else if (newStatus === "idle" && this.lastStatus === "starting") {
47943
+ this.pushEvent({ event: "agent:ready", chatTitle, timestamp: now });
47921
47944
  } else if (newStatus === "stopped") {
47922
47945
  if (this.generatingDebounceTimer) {
47923
47946
  clearTimeout(this.generatingDebounceTimer);
@@ -49653,9 +49676,6 @@ ${rawInput}` : rawInput;
49653
49676
  const cliType = String(input.cliType || "").trim();
49654
49677
  const cliArgs = Array.isArray(input.cliArgs) ? [...input.cliArgs] : [];
49655
49678
  const env2 = { ...input.env || {}, ...COORDINATOR_DELEGATED_ENV_UNSETS };
49656
- if (cliType === "hermes-cli" && !hasCliArg(cliArgs, "--ignore-user-config")) {
49657
- cliArgs.unshift("--ignore-user-config");
49658
- }
49659
49679
  if (cliType === "claude-cli" && !hasCliArg(cliArgs, "--mcp-config")) {
49660
49680
  cliArgs.unshift("--mcp-config", ensureEmptyDelegatedMcpConfig(input.workspace));
49661
49681
  }
@@ -52970,6 +52990,22 @@ Run 'adhdev doctor' for detailed diagnostics.`
52970
52990
  if (!instructions || !template?.trim()) {
52971
52991
  return { kind: "unsupported", reason: "Provider manual MCP setup is missing instructions or template" };
52972
52992
  }
52993
+ const renderedTemplate = renderMeshCoordinatorTemplate(template, {
52994
+ meshId,
52995
+ workspace,
52996
+ serverName,
52997
+ adhdevMcpCommand: options.adhdevMcpCommand || DEFAULT_ADHDEV_MCP_COMMAND
52998
+ });
52999
+ const isCliCommand = !renderedTemplate.trim().includes("\n") && !renderedTemplate.trim().startsWith("{");
53000
+ if (isCliCommand) {
53001
+ return {
53002
+ kind: "cli_command",
53003
+ serverName,
53004
+ command: renderedTemplate.trim(),
53005
+ requiresRestart: mcpConfig.requiresRestart === true,
53006
+ instructions
53007
+ };
53008
+ }
52973
53009
  return {
52974
53010
  kind: "manual",
52975
53011
  serverName,
@@ -52977,12 +53013,7 @@ Run 'adhdev doctor' for detailed diagnostics.`
52977
53013
  configPathCommand: mcpConfig.configPathCommand,
52978
53014
  requiresRestart: mcpConfig.requiresRestart === true,
52979
53015
  instructions,
52980
- template: renderMeshCoordinatorTemplate(template, {
52981
- meshId,
52982
- workspace,
52983
- serverName,
52984
- adhdevMcpCommand: options.adhdevMcpCommand || DEFAULT_ADHDEV_MCP_COMMAND
52985
- })
53016
+ template: renderedTemplate
52986
53017
  };
52987
53018
  }
52988
53019
  return {
@@ -55156,6 +55187,93 @@ ${(0, import_node_path.resolve)(workspace || os17.tmpdir())}`;
55156
55187
  meshCoordinatorSetup: coordinatorSetup
55157
55188
  };
55158
55189
  }
55190
+ if (coordinatorSetup.kind === "cli_command") {
55191
+ let cliCmdSystemPrompt = "";
55192
+ try {
55193
+ cliCmdSystemPrompt = buildCoordinatorSystemPrompt2({ mesh, coordinatorCliType: cliType });
55194
+ } catch (error48) {
55195
+ const message = error48?.message || String(error48);
55196
+ LOG2.error("MeshCoordinator", `Failed to build coordinator prompt: ${message}`);
55197
+ return {
55198
+ success: false,
55199
+ code: "mesh_coordinator_prompt_failed",
55200
+ error: `Failed to build Repo Mesh coordinator prompt: ${message}`,
55201
+ meshId,
55202
+ cliType,
55203
+ workspace
55204
+ };
55205
+ }
55206
+ try {
55207
+ const { execFileSync: execCmdSync } = await import("child_process");
55208
+ const cmdParts = coordinatorSetup.command.trim().split(/\s+/);
55209
+ const [regCmd, ...regArgs] = cmdParts;
55210
+ LOG2.info("MeshCoordinator", `Running MCP registration: ${coordinatorSetup.command}`);
55211
+ execCmdSync(regCmd, regArgs, { stdio: "pipe", timeout: 15e3 });
55212
+ } catch (error48) {
55213
+ LOG2.warn("MeshCoordinator", `MCP registration command failed (may be pre-registered): ${error48?.message || error48}`);
55214
+ }
55215
+ const cliCmdArgs = [];
55216
+ const cliCmdEnv = {};
55217
+ if (cliCmdSystemPrompt) {
55218
+ if (cliType === "codex-cli") {
55219
+ cliCmdArgs.push("-c", `developer_instructions=${JSON.stringify(cliCmdSystemPrompt)}`);
55220
+ } else if (cliType === "gemini-cli") {
55221
+ try {
55222
+ const { writeFileSync: wfs, existsSync: efs, readFileSync: rfs } = await import("fs");
55223
+ const geminiMdPath = `${workspace}/GEMINI.md`;
55224
+ const marker = "<!-- adhdev-mesh-coordinator-prompt -->";
55225
+ const markerEnd = "<!-- /adhdev-mesh-coordinator-prompt -->";
55226
+ const block = `${marker}
55227
+ ${cliCmdSystemPrompt}
55228
+ ${markerEnd}`;
55229
+ if (efs(geminiMdPath)) {
55230
+ const existing = rfs(geminiMdPath, "utf-8");
55231
+ const replaced = existing.replace(
55232
+ new RegExp(`${marker}[\\s\\S]*?${markerEnd}`, "g"),
55233
+ block
55234
+ );
55235
+ wfs(geminiMdPath, replaced.includes(marker) ? replaced : `${existing}
55236
+
55237
+ ${block}`);
55238
+ } else {
55239
+ wfs(geminiMdPath, block);
55240
+ }
55241
+ LOG2.info("MeshCoordinator", `Wrote coordinator prompt to ${workspace}/GEMINI.md`);
55242
+ } catch (e) {
55243
+ LOG2.warn("MeshCoordinator", `Could not write GEMINI.md: ${e?.message || e}`);
55244
+ }
55245
+ }
55246
+ }
55247
+ const cliCmdLaunch = await this.deps.cliManager.handleCliCommand("launch_cli", {
55248
+ cliType,
55249
+ dir: workspace,
55250
+ cliArgs: cliCmdArgs.length > 0 ? cliCmdArgs : void 0,
55251
+ env: Object.keys(cliCmdEnv).length > 0 ? cliCmdEnv : void 0,
55252
+ settings: { meshCoordinatorFor: meshId }
55253
+ });
55254
+ if (!cliCmdLaunch?.success) {
55255
+ return { success: false, error: cliCmdLaunch?.error || "Failed to launch CLI session" };
55256
+ }
55257
+ LOG2.info("MeshCoordinator", `Launched ${cliType} coordinator (cli_command) for mesh ${meshId}`);
55258
+ try {
55259
+ const { appendLedgerEntry: appendLedgerEntry2 } = await Promise.resolve().then(() => (init_mesh_ledger(), mesh_ledger_exports));
55260
+ appendLedgerEntry2(meshId, {
55261
+ kind: "coordinator_started",
55262
+ sessionId: cliCmdLaunch.sessionId || cliCmdLaunch.id,
55263
+ providerType: cliType,
55264
+ payload: { workspace }
55265
+ });
55266
+ } catch {
55267
+ }
55268
+ return {
55269
+ success: true,
55270
+ meshId,
55271
+ cliType,
55272
+ workspace,
55273
+ sessionId: cliCmdLaunch.sessionId || cliCmdLaunch.id,
55274
+ mcpRegistered: true
55275
+ };
55276
+ }
55159
55277
  const configFormat = coordinatorSetup.configFormat;
55160
55278
  if (configFormat !== "claude_mcp_json" && configFormat !== "hermes_config_yaml") {
55161
55279
  return {