u-foo 3.1.7 → 4.0.0

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 (193) hide show
  1. package/README.md +172 -13
  2. package/README.zh-CN.md +108 -13
  3. package/SKILLS/ufoo/SKILL.md +15 -3
  4. package/SKILLS/ufoo-bus/SKILL.md +21 -1
  5. package/bin/uclaude.js +4 -0
  6. package/bin/ucodex.js +4 -0
  7. package/bin/ufoo.js +3 -1
  8. package/dist/tui/darwin-arm64/ufoo-tui +0 -0
  9. package/dist/tui/darwin-x64/ufoo-tui +0 -0
  10. package/dist/tui/linux-arm64/ufoo-tui +0 -0
  11. package/dist/tui/linux-x64/ufoo-tui +0 -0
  12. package/package.json +10 -3
  13. package/scripts/postinstall-skills.js +53 -0
  14. package/scripts/postinstall.js +11 -16
  15. package/scripts/smoke-native-messages.js +214 -0
  16. package/src/agents/activity/activityStateWriter.js +37 -38
  17. package/src/agents/capabilities/agentManagement/index.js +4 -0
  18. package/src/agents/capabilities/agentManagement/service.js +113 -0
  19. package/src/agents/capabilities/agentRouting/index.js +4 -0
  20. package/src/agents/capabilities/coding/index.js +32 -0
  21. package/src/agents/capabilities/coordination/index.js +37 -0
  22. package/src/agents/capabilities/globalRouting/index.js +5 -0
  23. package/src/agents/capabilities/groups/index.js +3 -0
  24. package/src/agents/capabilities/index.js +25 -0
  25. package/src/agents/capabilities/planning/index.js +123 -0
  26. package/src/agents/capabilities/planning/main.js +28 -0
  27. package/src/agents/capabilities/projectDiscovery/index.js +3 -0
  28. package/src/agents/capabilities/scheduling/index.js +3 -0
  29. package/src/agents/capabilities/sharedMemory/index.js +4 -0
  30. package/src/agents/capabilities/skills/index.js +12 -0
  31. package/src/agents/capabilities/taskExecution/index.js +3 -0
  32. package/src/agents/capabilities/taskReports/index.js +6 -0
  33. package/src/agents/controller/controllerToolExecutor.js +6 -4
  34. package/src/agents/controller/loopRuntime.js +85 -221
  35. package/src/agents/controller/ufooAgent.js +1 -0
  36. package/src/agents/internal/internalRunner.js +103 -32
  37. package/src/agents/launch/launcher.js +88 -6
  38. package/src/agents/launch/nativeCapabilities.js +40 -0
  39. package/src/agents/launch/nativeMessages.js +362 -0
  40. package/src/agents/launch/notifier.js +13 -18
  41. package/src/agents/launch/readyDetector.js +26 -12
  42. package/src/agents/profiles/coding.js +6 -0
  43. package/src/agents/profiles/globalRouter.js +2 -0
  44. package/src/agents/profiles/main.js +5 -0
  45. package/src/agents/prompts/groupBootstrap.js +10 -0
  46. package/src/agents/prompts/mainAgent.js +39 -0
  47. package/src/agents/prompts/native/tasks.js +2 -2
  48. package/src/agents/prompts/native/ufoo.js +3 -0
  49. package/src/agents/providers/claudeEventTranslator.js +6 -1
  50. package/src/agents/providers/claudeThreadProvider.js +44 -2
  51. package/src/agents/providers/codexEventTranslator.js +47 -7
  52. package/src/agents/providers/codexThreadProvider.js +3 -2
  53. package/src/agents/providers/controllerJsonTransport.js +28 -0
  54. package/src/agents/providers/credentials/claude.js +10 -3
  55. package/src/agents/providers/nativeTransport.js +1135 -0
  56. package/src/agents/providers/runtimeConfig.js +186 -0
  57. package/src/agents/providers/thinkingLevels.js +132 -0
  58. package/src/agents/providers/transports/anthropicMessagesTransport.js +121 -0
  59. package/src/agents/providers/transports/index.js +10 -0
  60. package/src/agents/providers/transports/openaiChatTransport.js +117 -0
  61. package/src/agents/providers/transports/openaiResponsesTransport.js +120 -0
  62. package/src/agents/providers/transports/responsesProtocol.js +499 -0
  63. package/src/agents/providers/transports/systemBlocks.js +16 -0
  64. package/src/agents/providers/transports/transportContract.js +50 -0
  65. package/src/agents/providers/transports/visionBlocks.js +151 -0
  66. package/src/agents/providers/upstreamTransport.js +13 -4
  67. package/src/agents/runtime/composeCapabilities.js +89 -0
  68. package/src/agents/runtime/context/commandStore.js +43 -0
  69. package/src/agents/runtime/context/contextWindow.js +119 -0
  70. package/src/agents/runtime/context/fileLock.js +36 -0
  71. package/src/agents/runtime/context/runtimeStore.js +126 -0
  72. package/src/agents/runtime/context/sessionJournal.js +360 -0
  73. package/src/agents/runtime/context/sessionStore.js +57 -0
  74. package/src/agents/runtime/core/agentLoop.js +270 -0
  75. package/src/agents/runtime/core/stableJson.js +29 -0
  76. package/src/agents/runtime/core/toolLoopGuard.js +55 -0
  77. package/src/agents/runtime/createAgentRuntime.js +247 -0
  78. package/src/agents/runtime/index.js +14 -0
  79. package/src/agents/runtime/protocol/faultHarness.js +90 -0
  80. package/src/agents/runtime/protocol/materialize.js +107 -0
  81. package/src/agents/runtime/protocol/toolCallLedger.js +221 -0
  82. package/src/agents/runtime/tasks/resourceLease.js +66 -0
  83. package/src/agents/runtime/tasks/scheduler.js +73 -0
  84. package/src/agents/runtime/tools/registry.js +57 -0
  85. package/src/app/chat/ChatController.js +14 -3
  86. package/src/app/chat/agentDirectory.js +8 -2
  87. package/src/app/chat/commandExecutor.js +36 -46
  88. package/src/app/chat/commands.js +14 -7
  89. package/src/app/chat/daemonConnection.js +58 -5
  90. package/src/app/chat/daemonMessageRouter.js +61 -3
  91. package/src/app/chat/inputSubmitHandler.js +14 -2
  92. package/src/app/chat/launchRequestContext.js +42 -0
  93. package/src/app/chat/multiWindow/paneManager.js +85 -165
  94. package/src/app/chat/streamState.js +3 -3
  95. package/src/app/chat/transport.js +1 -1
  96. package/src/app/cli/busCoreCommands.js +9 -0
  97. package/src/app/cli/features/doctor.js +33 -0
  98. package/src/app/cli/features/skills.js +16 -4
  99. package/src/app/cli/reportCoreCommands.js +37 -0
  100. package/src/app/cli/run.js +40 -39
  101. package/src/code/agent.js +16 -4
  102. package/src/code/context/artifacts.js +12 -11
  103. package/src/code/context/assembler.js +2 -1
  104. package/src/code/context/promptLayers.js +1 -13
  105. package/src/code/context/stableJson.js +2 -27
  106. package/src/code/contextWindow.js +2 -117
  107. package/src/code/conversation/sessionJournal.js +3 -349
  108. package/src/code/internalThread.js +100 -0
  109. package/src/code/nativeRunner.js +36 -2667
  110. package/src/code/protocol/faultHarness.js +2 -88
  111. package/src/code/protocol/materialize.js +2 -105
  112. package/src/code/protocol/toolCallLedger.js +5 -215
  113. package/src/code/providers/anthropicMessagesTransport.js +2 -118
  114. package/src/code/providers/openaiChatTransport.js +2 -114
  115. package/src/code/providers/openaiResponsesTransport.js +2 -117
  116. package/src/code/providers/responsesProtocol.js +2 -497
  117. package/src/code/providers/transportContract.js +2 -48
  118. package/src/code/providers/visionBlocks.js +2 -149
  119. package/src/code/sessionStore.js +24 -96
  120. package/src/code/thinkingLevels.js +1 -130
  121. package/src/code/toolLoopGuard.js +3 -49
  122. package/src/code/tools/artifactRead.js +1 -1
  123. package/src/code/tools/executor.js +373 -0
  124. package/src/code/tools/specs.js +312 -0
  125. package/src/config.js +81 -67
  126. package/src/coordination/bus/deliveryQueue.js +16 -2
  127. package/src/coordination/bus/index.js +2 -6
  128. package/src/coordination/bus/inject.js +13 -5
  129. package/src/coordination/bus/nativeReceipts.js +100 -0
  130. package/src/coordination/bus/queue.js +1 -3
  131. package/src/coordination/bus/utils.js +7 -3
  132. package/src/coordination/history/agentSurface.js +142 -0
  133. package/src/coordination/history/controllerSummary.js +23 -0
  134. package/src/coordination/memory/index.js +5 -21
  135. package/src/coordination/report/store.js +103 -83
  136. package/src/coordination/state/agentsStore.js +57 -3
  137. package/src/coordination/state/fileLock.js +2 -0
  138. package/src/coordination/state/workspaceAccess.js +36 -0
  139. package/src/runtime/contracts/agentMode.js +17 -0
  140. package/src/runtime/contracts/eventContract.js +3 -0
  141. package/src/runtime/contracts/uiProtocol.js +4 -0
  142. package/src/runtime/daemon/agentHost.js +375 -0
  143. package/src/runtime/daemon/busBridge.js +396 -0
  144. package/src/runtime/daemon/claudeChannel.js +159 -0
  145. package/src/runtime/daemon/controlPlaneService.js +27 -17
  146. package/src/runtime/daemon/cronOps.js +25 -17
  147. package/src/runtime/daemon/deliveryScheduler.js +32 -5
  148. package/src/runtime/daemon/executionPaths.js +30 -0
  149. package/src/runtime/daemon/globalDaemon.js +28 -1
  150. package/src/runtime/daemon/groupOrchestrator.js +1 -0
  151. package/src/runtime/daemon/index.js +114 -344
  152. package/src/runtime/daemon/ipcServer.js +32 -10
  153. package/src/runtime/daemon/mcpConfigure.js +113 -20
  154. package/src/runtime/daemon/mcpHttpServer.js +27 -5
  155. package/src/runtime/daemon/mcpServer.js +7 -4
  156. package/src/runtime/daemon/mcpStdioProxy.js +127 -10
  157. package/src/runtime/daemon/ops.js +22 -17
  158. package/src/runtime/daemon/promptRequest.js +23 -4
  159. package/src/runtime/daemon/providerSessions.js +67 -33
  160. package/src/runtime/daemon/reportControlBus.js +59 -25
  161. package/src/runtime/daemon/status.js +4 -0
  162. package/src/runtime/privacy/redactor.js +2 -2
  163. package/src/runtime/projects/identity.js +2 -1
  164. package/src/runtime/projects/registry.js +98 -38
  165. package/src/tools/controllerSchemas.js +24 -0
  166. package/src/tools/handlers/dispatchMessage.js +5 -2
  167. package/src/tools/handlers/listAgents.js +3 -1
  168. package/src/tools/handlers/readBusSummary.js +3 -1
  169. package/src/tools/handlers/readProjectRegistry.js +29 -2
  170. package/src/tools/handlers/routeAgent.js +35 -0
  171. package/src/tools/index.js +1 -0
  172. package/src/tools/registry.js +2 -0
  173. package/src/tools/schemaFixtures.js +16 -3
  174. package/src/tools/tier1/routeAgent.js +2 -2
  175. package/src/tools/tier2/runtimeCoordination.js +38 -0
  176. package/src/tools/types.js +2 -0
  177. package/src/ui/agentPresentation.js +109 -0
  178. package/src/ui/agentSurface.js +270 -0
  179. package/src/ui/format/banner.js +8 -11
  180. package/src/ui/format/startupBanner.js +50 -0
  181. package/src/ui/multiPaneBusMirror.js +29 -3
  182. package/src/ui/rustChatHost.js +170 -438
  183. package/src/ui/rustMultiSession.js +225 -51
  184. package/src/ui/rustUcodeHost.js +20 -87
  185. package/src/ui/settingsBridge.js +7 -4
  186. package/src/app/chat/agentEnter.js +0 -70
  187. package/src/app/chat/multiWindow/index.js +0 -310
  188. package/src/app/chat/multiWindow/paneLayout.js +0 -84
  189. package/src/app/chat/multiWindow/renderer.js +0 -420
  190. package/src/app/chat/multiWindow/virtualTerminal.js +0 -327
  191. package/src/app/chat/multiWindow/vtFrame.js +0 -93
  192. package/src/ui/multiWindowHandoff.js +0 -232
  193. package/src/ui/ptyHandoff.js +0 -23
@@ -17,15 +17,13 @@ const { createRustMultiSession } = require("./rustMultiSession");
17
17
  const { writeMultiPaneBusEvent } = require("./multiPaneBusMirror");
18
18
  const { buildSettingsSnapshot, applySettingsPatch } = require("./settingsBridge");
19
19
  const fmt = require("./format");
20
+ const { buildUfooStartupEntries } = require("./format/startupBanner");
20
21
  const {
21
22
  loadGlobalProjectRows,
22
23
  buildDashboardPublishPayload,
23
24
  } = require("./dashboardBridge");
24
25
  const { createChatController } = require("../app/chat/ChatController");
25
- const {
26
- resolveAgentEnterRequest,
27
- resolveDashboardAgentEnterAction,
28
- } = require("../app/chat/agentEnter");
26
+ const { isInternalAgentMeta, isInternalDashboardEvent } = require("../runtime/contracts/agentMode");
29
27
  const { loadConfig } = require("../config");
30
28
  const { bootstrapEnvironment, ensureSubscriberId } = require("../app/chat/bootstrap");
31
29
  const {
@@ -174,16 +172,18 @@ async function runChatRust(projectRoot, options = {}) {
174
172
 
175
173
  let entrySeq = 0;
176
174
  const streamIds = new Map();
175
+ let mainSubmissionRev = 0;
177
176
  let hostRef = null;
178
177
  let daemonSend = () => {};
179
178
  let daemonCoordinator = null;
180
179
  let routedMessageHandler = () => {};
181
180
  let activeProjectRoot = projectRoot;
182
181
  let globalScope = "controller";
183
- let agentViewId = "";
184
182
  let multiSession = null;
183
+ const internalSubmissions = new Map();
185
184
  /** Internal pane agents currently BUS_WATCH'd for multi/side inbound. */
186
185
  const watchedInternalAgents = new Set();
186
+ const welcomedProjects = new Set();
187
187
  const settings = (() => {
188
188
  try {
189
189
  return loadConfig(projectRoot) || {};
@@ -216,7 +216,7 @@ async function runChatRust(projectRoot, options = {}) {
216
216
 
217
217
  function reconcileMultiInternalWatches() {
218
218
  const next = new Set();
219
- if (multiSession && multiSession.isActive()
219
+ if (multiSession
220
220
  && typeof multiSession.listInternalAgentIds === "function") {
221
221
  for (const id of multiSession.listInternalAgentIds()) {
222
222
  next.add(id);
@@ -233,7 +233,7 @@ async function runChatRust(projectRoot, options = {}) {
233
233
  }
234
234
 
235
235
  function mirrorBusToMultiPanes(data = {}) {
236
- if (!multiSession || !multiSession.isActive()) return false;
236
+ if (!multiSession) return false;
237
237
  if (typeof multiSession.writeToPane !== "function") return false;
238
238
  const ids = typeof multiSession.listInternalAgentIds === "function"
239
239
  ? multiSession.listInternalAgentIds()
@@ -245,6 +245,8 @@ async function runChatRust(projectRoot, options = {}) {
245
245
  try { return controller.session.metaMap.get(agentId) || {}; } catch { return {}; }
246
246
  },
247
247
  writeToPane: (agentId, text) => multiSession.writeToPane(agentId, text),
248
+ acceptEvent: (agentId, event, seq) => multiSession.acceptEvent(agentId, event, seq),
249
+ hasStructuredEvents: (agentId) => multiSession.hasStructuredEvents(agentId),
248
250
  });
249
251
  }
250
252
 
@@ -254,7 +256,7 @@ async function runChatRust(projectRoot, options = {}) {
254
256
  agents: payload.agents,
255
257
  footer: payload.footer,
256
258
  });
257
- if (multiSession && multiSession.isActive()) {
259
+ if (multiSession) {
258
260
  try { multiSession.syncAgents(); } catch {}
259
261
  try { reconcileMultiInternalWatches(); } catch {}
260
262
  }
@@ -299,6 +301,11 @@ async function runChatRust(projectRoot, options = {}) {
299
301
  }));
300
302
  }
301
303
 
304
+ function isInternalChild(agentId) {
305
+ const id = String(agentId || "");
306
+ return id !== "ufoo-agent" && !id.endsWith(":ufoo-agent") && hostApi.isInternalAgent(id);
307
+ }
308
+
302
309
  function publishLossy(name, payload) {
303
310
  if (!hostRef) return;
304
311
  hostRef.broadcast(hostRef.createLossyEvent(name, payload, {
@@ -341,9 +348,7 @@ async function runChatRust(projectRoot, options = {}) {
341
348
  const pathMod = require("path");
342
349
  const label = pathMod.basename(root) || root;
343
350
 
344
- if (multiSession && multiSession.isActive()) {
345
- multiSession.stop();
346
- }
351
+ if (multiSession) multiSession.stop({ clearDrafts: true });
347
352
 
348
353
  const targetEndpoint = resolveDaemonEndpoint(root);
349
354
  const targetDaemonRoot = targetEndpoint.scope === "global"
@@ -366,19 +371,6 @@ async function runChatRust(projectRoot, options = {}) {
366
371
  return { ok: false, error: `project is not running: ${label}`, stopped: true };
367
372
  }
368
373
 
369
- if (agentViewId) {
370
- try {
371
- daemonSend({
372
- type: IPC_REQUEST_TYPES.BUS_WATCH,
373
- agent_id: agentViewId,
374
- enabled: false,
375
- });
376
- } catch {
377
- // ignore
378
- }
379
- agentViewId = "";
380
- publish("agent.view.close", {});
381
- }
382
374
 
383
375
  if (daemonCoordinator && typeof daemonCoordinator.switchProject === "function") {
384
376
  const res = await daemonCoordinator.switchProject({
@@ -412,9 +404,7 @@ async function runChatRust(projectRoot, options = {}) {
412
404
  return { ok: true, project_root: root, root };
413
405
  },
414
406
  async switchToControllerRoot() {
415
- if (multiSession && multiSession.isActive()) {
416
- multiSession.stop();
417
- }
407
+ if (multiSession) multiSession.stop({ clearDrafts: true });
418
408
  if (!env.globalMode) {
419
409
  return { ok: true, project_root: projectRoot, root: projectRoot };
420
410
  }
@@ -433,19 +423,6 @@ async function runChatRust(projectRoot, options = {}) {
433
423
  return res || { ok: false, error: "switch to global failed" };
434
424
  }
435
425
  }
436
- if (agentViewId) {
437
- try {
438
- daemonSend({
439
- type: IPC_REQUEST_TYPES.BUS_WATCH,
440
- agent_id: agentViewId,
441
- enabled: false,
442
- });
443
- } catch {
444
- // ignore
445
- }
446
- agentViewId = "";
447
- publish("agent.view.close", {});
448
- }
449
426
  activeProjectRoot = projectRoot;
450
427
  globalScope = "controller";
451
428
  controller.session.targetAgent = null;
@@ -463,78 +440,13 @@ async function runChatRust(projectRoot, options = {}) {
463
440
  }
464
441
  return { ok: true, project_root: projectRoot, root: projectRoot };
465
442
  },
466
- openBusAgentView(agentId, label = "") {
467
- const id = String(agentId || "").trim();
468
- if (!id) return { ok: false, error: "missing agent_id" };
469
- const viewLabel = String(label || id);
470
- if (agentViewId && agentViewId !== id) {
471
- try {
472
- daemonSend({
473
- type: IPC_REQUEST_TYPES.BUS_WATCH,
474
- agent_id: agentViewId,
475
- enabled: false,
476
- });
477
- } catch {
478
- // ignore
479
- }
480
- }
481
- agentViewId = id;
482
- controller.session.targetAgent = null;
483
- publish("prompt.set_prefix", { prefix: "› " });
484
- try {
485
- const { loadInternalAgentLogHistory } = require("../app/chat/internalAgentLogHistory");
486
- const history = loadInternalAgentLogHistory(activeProjectRoot, id, {
487
- width: 80,
488
- }) || [];
489
- const entries = historyToEntries(
490
- Array.isArray(history) ? history.map((line) => String(line || "")) : []
491
- );
492
- publish("agent.view.open", {
493
- agent_id: id,
494
- label: viewLabel,
495
- status: "ready",
496
- entries,
497
- });
498
- } catch {
499
- publish("agent.view.open", {
500
- agent_id: id,
501
- label: viewLabel,
502
- status: "ready",
503
- entries: [],
504
- });
505
- }
506
- try {
507
- daemonSend({
508
- type: IPC_REQUEST_TYPES.BUS_WATCH,
509
- agent_id: id,
510
- enabled: true,
511
- });
512
- } catch {
513
- // ignore
514
- }
515
- return { ok: true, mode: "agent_view", agent_id: id };
516
- },
517
443
  isInternalAgent(agentId) {
518
- const id = String(agentId || "").trim();
519
- if (!id) return false;
520
- const enter = resolveAgentEnterRequest({
521
- agentId: id,
522
- projectRoot: activeProjectRoot,
523
- activeAgentMeta: controller.session.metaMap,
524
- settings,
525
- });
526
- if (enter && enter.useBus) return true;
527
- // UI launch mode internal + agent without activate/socket → treat as bus.
528
- const mode = String(settings.launchMode || settings.launch_mode || "").trim();
529
- if (mode === "internal" && enter && !enter.supportsActivate && !enter.supportsSocket) {
530
- return true;
531
- }
532
- return false;
444
+ return isInternalAgentMeta(controller.session.metaMap.get(String(agentId || "").trim()));
533
445
  },
534
446
  /** Internal activate: split like multi-with-1-agent (kind=side). Not /multi. */
535
447
  startSide(agentId) {
536
448
  const id = String(agentId || "").trim();
537
- if (!id) return { ok: false, error: "missing agent_id" };
449
+ if (!hostApi.isInternalAgent(id)) return { ok: false, error: "Dashboard supports internal agents only" };
538
450
  if (!multiSession) return { ok: false, error: "split session not initialised" };
539
451
  // Prefer capability check, but do not hard-fail — connected Rust child
540
452
  // may be mid-handshake; multi.set is still the right wire.
@@ -547,24 +459,6 @@ async function runChatRust(projectRoot, options = {}) {
547
459
  error: `Rust TUI lacks ${MULTI_FRAMES_CAPABILITY}; rebuild ufoo-tui`,
548
460
  };
549
461
  }
550
- // Close fullscreen AgentView if it was open.
551
- if (agentViewId) {
552
- try {
553
- daemonSend({
554
- type: IPC_REQUEST_TYPES.BUS_WATCH,
555
- agent_id: agentViewId,
556
- enabled: false,
557
- });
558
- } catch {
559
- // ignore
560
- }
561
- agentViewId = "";
562
- publish("agent.view.close", {});
563
- }
564
- // /multi stays /multi — stop it before entering side.
565
- if (multiSession.isMultiKind && multiSession.isMultiKind()) {
566
- multiSession.stop();
567
- }
568
462
  const result = multiSession.start({
569
463
  kind: "side",
570
464
  agentIds: [id],
@@ -573,99 +467,28 @@ async function runChatRust(projectRoot, options = {}) {
573
467
  if (result && result.ok) {
574
468
  controller.session.targetAgent = null;
575
469
  publish("prompt.set_prefix", { prefix: "› " });
576
- publish("status.set", { text: "ready" });
577
470
  try { reconcileMultiInternalWatches(); } catch { /* ignore */ }
578
471
  return result;
579
472
  }
580
473
  return result || { ok: false, error: "side start failed" };
581
474
  },
582
- enterAgentView(agentId, options = {}) {
475
+ enterAgentView(agentId) {
583
476
  const id = String(agentId || "").trim();
584
- if (!id) return;
585
- const enter = resolveAgentEnterRequest({
586
- agentId: id,
587
- projectRoot: activeProjectRoot,
588
- activeAgentMeta: controller.session.metaMap,
589
- settings,
590
- });
591
- const label = (() => {
592
- const meta = controller.session.metaMap.get(id) || {};
593
- return meta.display_nickname || meta.nickname || id;
594
- })();
595
- const action = options.useBus
596
- ? "internal"
597
- : resolveDashboardAgentEnterAction(enter);
598
- const isInternal = action === "internal"
599
- || (enter && enter.useBus)
600
- || options.useBus;
601
-
602
- // /multi (including internal panes): focus the in-window pane.
603
- if (multiSession && multiSession.isMultiKind && multiSession.isMultiKind()) {
604
- try { multiSession.syncAgents(); } catch {}
605
- const focused = multiSession.focusAgent(id);
606
- if (focused && focused.ok) {
607
- appendLocal("system", `Multi focus → ${id}`);
608
- return { ok: true, mode: "multi_focus", agent_id: id };
609
- }
477
+ if (!hostApi.isInternalAgent(id)) return { ok: false, error: "Dashboard supports internal agents only" };
478
+ if (multiSession && multiSession.isMultiKind()) {
479
+ multiSession.syncAgents();
480
+ const result = multiSession.focusAgent(id);
481
+ if (result && result.ok) return { ...result, mode: "multi_focus", agent_id: id };
610
482
  }
611
-
612
- // Non-multi internal activate → side (same chrome as multi×1).
613
- if (isInternal) {
614
- const side = hostApi.startSide(id);
615
- if (side && side.ok) {
616
- return { ok: true, mode: "side", agent_id: id };
617
- }
618
- appendLocal("error", (side && side.error) || "side start failed");
619
- return side || { ok: false, error: "side start failed" };
620
- }
621
-
622
- // Already in side for this agent: re-focus.
623
- if (multiSession && multiSession.isSideKind && multiSession.isSideKind()) {
624
- const focused = multiSession.focusAgent(id);
625
- if (focused && focused.ok) {
626
- return { ok: true, mode: "side", agent_id: id };
627
- }
628
- // Different agent — switch side target.
629
- const side = hostApi.startSide(id);
630
- if (side && side.ok) return { ok: true, mode: "side", agent_id: id };
631
- }
632
-
633
- // Ink parity: activate = focus the agent's external terminal window/tab.
634
- if (action === "activate") {
635
- try {
636
- const AgentActivator = require("../coordination/bus/activate");
637
- const activator = new AgentActivator(activeProjectRoot || projectRoot);
638
- void activator.activate(id).catch((err) => {
639
- appendLocal(
640
- "error",
641
- `Failed to activate ${id}: ${err && err.message ? err.message : err}`
642
- );
643
- });
644
- appendLocal("system", `Activated ${label}`);
645
- return { ok: true, mode: "activate", agent_id: id };
646
- } catch (err) {
647
- appendLocal(
648
- "error",
649
- `Failed to activate ${id}: ${err && err.message ? err.message : err}`
650
- );
651
- return { ok: false, mode: "activate", agent_id: id, error: String(err && err.message || err) };
652
- }
653
- }
654
-
655
- // No PTY fullscreen handoff — host/socket agents need activate capability.
656
- appendLocal(
657
- "error",
658
- `Cannot enter ${label}: no activate support (host should expose activate; use /mode terminal|tmux|internal otherwise)`
659
- );
660
- return { ok: false, mode: "none", agent_id: id, error: "no activate support" };
483
+ const result = hostApi.startSide(id);
484
+ return result && result.ok ? { ...result, mode: "side", agent_id: id } : result;
661
485
  },
662
486
  getAgentAdapter(agentId) {
663
487
  try {
664
488
  const { createTerminalAdapterRouter } = require("../runtime/terminal/adapterRouter");
665
489
  const meta = controller.session.metaMap.get(agentId) || {};
666
- const launchMode = String(
667
- meta.launch_mode || meta.launchMode || settings.launchMode || ""
668
- ).trim();
490
+ if (!isInternalAgentMeta(meta)) return null;
491
+ const launchMode = "internal";
669
492
  return createTerminalAdapterRouter().getAdapter({ launchMode, agentId, meta });
670
493
  } catch {
671
494
  return null;
@@ -713,9 +536,55 @@ async function runChatRust(projectRoot, options = {}) {
713
536
  return last;
714
537
  }
715
538
 
539
+ function changeMultiLayout(args = []) {
540
+ if (!multiSession) return { ok: false, error: "Multi-window session not initialised" };
541
+ const values = Array.isArray(args) ? args : [];
542
+ if (values.length > 1) {
543
+ appendLocal("error", "Usage: /multi [on|off|@agent]");
544
+ return { ok: false, error: "invalid layout arguments" };
545
+ }
546
+ const value = String(values[0] || "").trim();
547
+ const layout = !value ? "toggle" : value === "on" ? "all" : value === "off" ? "main" : "single";
548
+ if (layout === "single") {
549
+ const label = value.replace(/^@/, "");
550
+ const matches = getActiveAgentIds().filter((id) => {
551
+ const meta = getAgentMetaForMulti(id);
552
+ return [id, meta.nickname, meta.scoped_nickname, meta.display_nickname].includes(label);
553
+ });
554
+ if (matches.length !== 1 || !hostApi.isInternalAgent(matches[0])) {
555
+ const error = matches.length > 1 ? `Ambiguous agent: ${label}` : `Internal agent not found: ${label}`;
556
+ appendLocal("error", error);
557
+ return { ok: false, error };
558
+ }
559
+ return hostApi.startSide(matches[0]);
560
+ }
561
+ if (layout !== "main" && !(layout === "toggle" && multiSession.isMultiKind())) {
562
+ const caps = hostRef && typeof hostRef.getClientCapabilities === "function"
563
+ ? hostRef.getClientCapabilities() : [];
564
+ if (!caps.includes(MULTI_FRAMES_CAPABILITY)) {
565
+ const error = `Rust TUI lacks ${MULTI_FRAMES_CAPABILITY}; upgrade ufoo-tui`;
566
+ appendLocal("error", error);
567
+ return { ok: false, error };
568
+ }
569
+ }
570
+ const result = multiSession.setLayout(layout);
571
+ if (!result.ok) {
572
+ appendLocal("error", result.error || "Layout switch failed");
573
+ return result;
574
+ }
575
+ reconcileMultiInternalWatches();
576
+ controller.session.targetAgent = null;
577
+ publish("prompt.set_prefix", { prefix: "› " });
578
+ appendLocal("system", multiSession.isActive()
579
+ ? "Showing all agents."
580
+ : "Showing main agent.");
581
+ return result;
582
+ }
583
+
716
584
  const controller = createChatController({
717
585
  projectRoot,
718
586
  globalMode: env.globalMode,
587
+ internalOnly: true,
719
588
  ports: {
720
589
  publish,
721
590
  logMessage: (kind, text) => {
@@ -762,7 +631,7 @@ async function runChatRust(projectRoot, options = {}) {
762
631
  enterAgentView: (...args) => hostApi.enterAgentView(...args),
763
632
  getAgentAdapter: (...args) => hostApi.getAgentAdapter(...args),
764
633
  focusMultiPane: async (agentId) => {
765
- // /multi: focus pane (terminal or internal — multi unchanged).
634
+ // Focus a visible internal pane.
766
635
  if (multiSession && multiSession.isMultiKind && multiSession.isMultiKind()) {
767
636
  try { multiSession.syncAgents(); } catch { /* ignore */ }
768
637
  const focused = multiSession.focusAgent(agentId);
@@ -779,26 +648,7 @@ async function runChatRust(projectRoot, options = {}) {
779
648
  }
780
649
  return false;
781
650
  },
782
- activateAgent: async (agentId) => {
783
- if (multiSession && multiSession.isMultiKind && multiSession.isMultiKind()) {
784
- try { multiSession.syncAgents(); } catch { /* ignore */ }
785
- const focused = multiSession.focusAgent(agentId);
786
- if (focused && focused.ok) {
787
- appendLocal("system", `Multi focus → ${agentId}`);
788
- return;
789
- }
790
- }
791
- if (hostApi.isInternalAgent(agentId)) {
792
- const side = hostApi.startSide(agentId);
793
- if (!side || !side.ok) {
794
- appendLocal("error", (side && side.error) || "side start failed");
795
- }
796
- return;
797
- }
798
- const AgentActivator = require("../coordination/bus/activate");
799
- const activator = new AgentActivator(activeProjectRoot || projectRoot);
800
- await activator.activate(agentId);
801
- },
651
+ activateAgent: async (agentId) => hostApi.enterAgentView(agentId),
802
652
  listProjects: () => listProjectsForCommands(),
803
653
  getCurrentProject: () => ({ project_root: activeProjectRoot }),
804
654
  getActiveProjectRoot: () => activeProjectRoot,
@@ -809,12 +659,15 @@ async function runChatRust(projectRoot, options = {}) {
809
659
  },
810
660
  dispatch: (action) => {
811
661
  if (!action || typeof action !== "object") return;
662
+ // Child streams are presented in their own pane, including their busy
663
+ // state. They must not start or complete the main agent's status.
664
+ if (action.publisher && isInternalChild(action.publisher)) return;
812
665
  if (action.type === "stream/begin") {
813
666
  tools.beginScope();
814
667
  const speaker = String(action.publisher || "");
815
668
  const id = `stream-${speaker || "agent"}-${Date.now()}`;
816
669
  streamIds.set(speaker, id);
817
- publish("stream.start", { id, speaker });
670
+ publish("stream.start", { id, speaker: action.displayName || speaker });
818
671
  return;
819
672
  }
820
673
  if (action.type === "stream/delta") {
@@ -831,6 +684,7 @@ async function runChatRust(projectRoot, options = {}) {
831
684
  if (action.type === "stream/end") {
832
685
  tools.flush();
833
686
  for (const [key, id] of [...streamIds.entries()]) {
687
+ if (action.publisher && key !== action.publisher) continue;
834
688
  publish("stream.done", { id });
835
689
  streamIds.delete(key);
836
690
  }
@@ -841,42 +695,7 @@ async function runChatRust(projectRoot, options = {}) {
841
695
  publish("transcript.reset", {});
842
696
  }
843
697
  },
844
- toggleMultiWindow: () => {
845
- if (!multiSession) {
846
- appendLocal("error", "Multi-window session not initialised");
847
- return false;
848
- }
849
- // Exit /multi when already in multi. If currently in side (internal
850
- // activate split), fall through and open real /multi instead.
851
- if (multiSession.isActive() && !(multiSession.isSideKind && multiSession.isSideKind())) {
852
- multiSession.stop();
853
- try { reconcileMultiInternalWatches(); } catch { /* ignore */ }
854
- appendLocal("system", "Exited multi-window.");
855
- return true;
856
- }
857
- if (multiSession.isActive()) {
858
- multiSession.stop();
859
- try { reconcileMultiInternalWatches(); } catch { /* ignore */ }
860
- }
861
- const caps = hostRef && typeof hostRef.getClientCapabilities === "function"
862
- ? hostRef.getClientCapabilities()
863
- : [];
864
- if (!caps.includes(MULTI_FRAMES_CAPABILITY)) {
865
- appendLocal(
866
- "error",
867
- `Rust TUI lacks ${MULTI_FRAMES_CAPABILITY}; upgrade ufoo-tui`
868
- );
869
- return false;
870
- }
871
- const result = multiSession.start({ kind: "multi" });
872
- if (!result.ok) {
873
- appendLocal("error", result.error || "multi-window start failed");
874
- return false;
875
- }
876
- try { reconcileMultiInternalWatches(); } catch { /* ignore */ }
877
- appendLocal("system", "Entered multi-window (Ctrl+W focus · Ctrl+Q exit).");
878
- return true;
879
- },
698
+ toggleMultiWindow: (args) => changeMultiLayout(args).ok,
880
699
  applyChatSettings: (patch = {}) => {
881
700
  if (patch.launchMode != null) settings.launchMode = patch.launchMode;
882
701
  if (patch.agentProvider != null) settings.agentProvider = patch.agentProvider;
@@ -916,13 +735,6 @@ async function runChatRust(projectRoot, options = {}) {
916
735
  }
917
736
 
918
737
  function resolveMultiPaneOptions(agentId) {
919
- const enter = resolveAgentEnterRequest({
920
- agentId,
921
- projectRoot: activeProjectRoot,
922
- activeAgentMeta: controller.session.metaMap,
923
- settings,
924
- });
925
- if (!enter || !enter.useBus) return { mode: "socket" };
926
738
  let initialLines = [];
927
739
  try {
928
740
  const { loadInternalAgentLogHistory } = require("../app/chat/internalAgentLogHistory");
@@ -935,33 +747,36 @@ async function runChatRust(projectRoot, options = {}) {
935
747
  }
936
748
  return {
937
749
  mode: "internal",
938
- initialLines: [
939
- `ufoo internal agent · ${getAgentLabel(agentId)}`,
940
- `agent: ${agentId}`,
941
- "",
942
- ...initialLines,
943
- ],
750
+ initialLines,
944
751
  };
945
752
  }
946
753
 
947
754
  multiSession = createRustMultiSession({
755
+ getProjectRoot: () => activeProjectRoot,
948
756
  projectRoot,
949
757
  getActiveAgents: getActiveAgentIds,
950
758
  getAgentMeta: getAgentMetaForMulti,
951
- getInjectSockPath: (id) =>
952
- require("../app/chat/agentEnter").resolveInjectSockPathForAgent(activeProjectRoot, id),
953
759
  resolvePaneOptions: resolveMultiPaneOptions,
954
760
  onInternalSubmit: (agentId, message) => {
761
+ // Layout commands belong to the workspace even when a child has focus.
762
+ if (/^\/multi(?:\s|$)/.test(String(message || "").trim())) {
763
+ changeMultiLayout(String(message).trim().split(/\s+/).slice(1));
764
+ return;
765
+ }
766
+ const requestId = `pane-send-${require("crypto").randomUUID()}`;
767
+ internalSubmissions.set(requestId, { agentId, message, projectRoot: activeProjectRoot });
955
768
  try {
956
- daemonSend({
769
+ return daemonSend({
957
770
  type: IPC_REQUEST_TYPES.BUS_SEND,
771
+ request_id: requestId,
958
772
  target: agentId,
959
773
  message: String(message || ""),
960
774
  injection_mode: "immediate",
961
775
  source: "rust-multi-window",
962
776
  });
963
- } catch {
964
- // ignore
777
+ } catch (error) {
778
+ internalSubmissions.delete(requestId);
779
+ throw error;
965
780
  }
966
781
  },
967
782
  publish,
@@ -969,12 +784,15 @@ async function runChatRust(projectRoot, options = {}) {
969
784
  getLabel: getAgentLabel,
970
785
  });
971
786
 
972
- function buildSnapshot() {
787
+ function buildSnapshot({ startup = true } = {}) {
973
788
  const { loadChatHistory, loadInputHistory } = require("../app/chat/historyStore");
974
789
  const historyRoot = activeProjectRoot || projectRoot;
975
790
  const historyOpts = historyOptions();
976
791
  const history = loadChatHistory(historyRoot, 200, historyOpts);
977
792
  const inputHistory = loadInputHistory(historyRoot, 200, historyOpts);
793
+ const showWelcome = startup && history.length === 0 && inputHistory.length === 0
794
+ && !welcomedProjects.has(historyRoot);
795
+ if (showWelcome) welcomedProjects.add(historyRoot);
978
796
  const agents = controller.getAgentsSnapshot();
979
797
  const settingsSnap = buildSettingsSnapshot(settings);
980
798
  const projects = env.globalMode ? loadGlobalProjectRows(activeProjectRoot) : [];
@@ -982,7 +800,15 @@ async function runChatRust(projectRoot, options = {}) {
982
800
  status: "ready",
983
801
  package_version: PACKAGE_VERSION,
984
802
  footer: agents.footer || "",
985
- entries: historyToEntries(history),
803
+ entries: [
804
+ ...historyToEntries(history),
805
+ ...(showWelcome ? buildUfooStartupEntries({
806
+ version: PACKAGE_VERSION,
807
+ workspaceRoot: historyRoot,
808
+ globalMode: Boolean(env.globalMode),
809
+ width: Math.max(1, (process.stdout.columns || 80) - 6),
810
+ }) : []),
811
+ ],
986
812
  input_history: Array.isArray(inputHistory) ? inputHistory.filter(Boolean) : [],
987
813
  agents: agents.agents,
988
814
  settings: settingsSnap,
@@ -1018,7 +844,7 @@ async function runChatRust(projectRoot, options = {}) {
1018
844
  name: "app.snapshot",
1019
845
  seq: host.nextSeq(),
1020
846
  scope: { surface: "chat", project_id: projectRoot },
1021
- payload: buildSnapshot(),
847
+ payload: buildSnapshot({ startup: true }),
1022
848
  });
1023
849
  socket.write(encodeMessage(snap));
1024
850
  if (env.globalMode) publishProjects();
@@ -1052,7 +878,7 @@ async function runChatRust(projectRoot, options = {}) {
1052
878
  soloProfiles: dynamic.soloProfiles,
1053
879
  limit: 20,
1054
880
  });
1055
- publish("completions.set", { items });
881
+ publish("completions.set", { items, text: String(payload.text || "") });
1056
882
  return { ok: true, count: items.length };
1057
883
  }
1058
884
  if (name === "task.cancel") {
@@ -1062,12 +888,10 @@ async function runChatRust(projectRoot, options = {}) {
1062
888
  }
1063
889
  if (name === "agent.select") {
1064
890
  const agentId = String(payload.agent_id || payload.agentId || "").trim();
891
+ if (agentId && !hostApi.isInternalAgent(agentId)) return { ok: false, error: "Unknown internal agent" };
1065
892
  if (agentId) {
1066
893
  controller.session.targetAgent = agentId;
1067
894
  const label = payload.label || agentId;
1068
- publish("status.set", {
1069
- text: `target @${label}`,
1070
- });
1071
895
  publish("prompt.set_prefix", { prefix: `›@${label} ` });
1072
896
  } else {
1073
897
  controller.session.targetAgent = null;
@@ -1084,12 +908,12 @@ async function runChatRust(projectRoot, options = {}) {
1084
908
  appendLocal("system", "ui.suspend ignored (PTY handoff removed)");
1085
909
  return { ok: true, suspend: false, agent_id: agentId };
1086
910
  }
1087
- if (name === "multi.exit") {
1088
- if (multiSession && multiSession.isActive()) {
1089
- multiSession.stop();
1090
- try { reconcileMultiInternalWatches(); } catch { /* ignore */ }
911
+ if (name === "multi.exit" || name === "multi.open" || name === "multi.toggle") {
912
+ if (payload.session_id && (!multiSession || payload.session_id !== multiSession.getSessionId())) {
913
+ return { ok: false, error: "stale session_id" };
1091
914
  }
1092
- return { ok: true };
915
+ const args = name === "multi.exit" ? ["off"] : name === "multi.open" ? ["on"] : [];
916
+ return changeMultiLayout(args);
1093
917
  }
1094
918
  if (name === "multi.focus") {
1095
919
  return multiSession
@@ -1106,52 +930,15 @@ async function runChatRust(projectRoot, options = {}) {
1106
930
  ? multiSession.handleRaw(payload)
1107
931
  : { ok: false, error: "multi not active" };
1108
932
  }
1109
- if (name === "agent.view.exit") {
1110
- if (agentViewId) {
1111
- try {
1112
- daemonSend({
1113
- type: IPC_REQUEST_TYPES.BUS_WATCH,
1114
- agent_id: agentViewId,
1115
- enabled: false,
1116
- });
1117
- } catch {
1118
- // ignore
1119
- }
1120
- }
1121
- for (const [key, id] of [...streamIds.entries()]) {
1122
- if (String(id).startsWith("av-stream-") || key === agentViewId) {
1123
- publish("stream.done", { id });
1124
- streamIds.delete(key);
1125
- }
1126
- }
1127
- agentViewId = "";
1128
- publish("agent.view.close", {});
1129
- publish("app.snapshot", buildSnapshot());
1130
- return { ok: true };
1131
- }
1132
- if (name === "agent.view.submit") {
1133
- const agentId = String(payload.agent_id || agentViewId || "").trim();
1134
- const text = String(payload.text || "").trim();
1135
- if (!agentId || !text) return { ok: false, error: "missing agent or text" };
1136
- publish("agent.view.append", {
1137
- id: `av-user-${Date.now()}`,
1138
- kind: "user",
1139
- text: `> ${text}`,
1140
- speaker: "",
1141
- });
1142
- daemonSend({
1143
- type: IPC_REQUEST_TYPES.BUS_SEND,
1144
- target: agentId,
1145
- message: text,
1146
- injection_mode: "immediate",
1147
- source: "chat-internal-agent-view",
1148
- });
1149
- publish("agent.view.status", { text: "working" });
1150
- return { ok: true };
933
+ if (name === "multi.scroll") {
934
+ return multiSession
935
+ ? multiSession.handleScroll(payload)
936
+ : { ok: false, error: "multi not active" };
1151
937
  }
938
+ if (name === "multi.expand") return multiSession?.handleExpand(payload) || { ok: false };
1152
939
  if (name === "agent.close") {
1153
940
  const agentId = String(payload.agent_id || payload.agentId || "").trim();
1154
- if (!agentId) return { ok: false, error: "missing agent_id" };
941
+ if (!hostApi.isInternalAgent(agentId)) return { ok: false, error: "Unknown internal agent" };
1155
942
  daemonSend({ type: IPC_REQUEST_TYPES.CLOSE_AGENT, agent_id: agentId });
1156
943
  publish("status.set", { text: `closing ${agentId}` });
1157
944
  return { ok: true };
@@ -1286,6 +1073,8 @@ async function runChatRust(projectRoot, options = {}) {
1286
1073
  if (name === "input.submit") {
1287
1074
  const text = String(payload.text || "");
1288
1075
  const payloadTarget = String(payload.target_agent || payload.targetAgent || "").trim();
1076
+ const childOrLayout = Boolean(controller.session.targetAgent || payloadTarget)
1077
+ || text.trimStart().startsWith("@") || /^\/multi(?:\s|$)/.test(text.trim());
1289
1078
  if (payloadTarget && !controller.session.targetAgent) {
1290
1079
  controller.session.targetAgent = payloadTarget;
1291
1080
  }
@@ -1300,13 +1089,13 @@ async function runChatRust(projectRoot, options = {}) {
1300
1089
  appendLocal("error", `side failed: ${(side && side.error) || "unknown"}`);
1301
1090
  // Fall through to submitInput for bus AgentView fallback.
1302
1091
  } else {
1303
- publish("status.set", { text: "ready", busy: false });
1304
1092
  return { ok: true, routed: "side", agent_id: target };
1305
1093
  }
1306
1094
  }
1307
1095
  }
1096
+ const beforeSubmit = mainSubmissionRev;
1308
1097
  await controller.submitInput(text);
1309
- publish("status.set", { text: "ready", busy: false });
1098
+ if (!childOrLayout && beforeSubmit === mainSubmissionRev) publish("status.set", { text: "ready", busy: false });
1310
1099
  return { ok: true, routed: text.trim() ? "submit" : "empty" };
1311
1100
  }
1312
1101
  return { ok: false, error: `unsupported command ${name}` };
@@ -1317,21 +1106,13 @@ async function runChatRust(projectRoot, options = {}) {
1317
1106
 
1318
1107
  const router = createDaemonMessageRouter({
1319
1108
  escapeBlessed: (value) => String(value || ""),
1109
+ resolveAgentDisplayName: getAgentLabel,
1320
1110
  stripBlessedTags: stripTags,
1321
1111
  logMessage: (kind, text) => {
1322
1112
  const normalized = kind === "error" ? "error"
1323
1113
  : kind === "user" ? "user"
1324
1114
  : kind === "assistant" ? "assistant"
1325
1115
  : "system";
1326
- if (agentViewId) {
1327
- publish("agent.view.append", {
1328
- id: `av-log-${Date.now()}`,
1329
- kind: normalized,
1330
- text: stripTags(text),
1331
- speaker: "",
1332
- });
1333
- return;
1334
- }
1335
1116
  appendLocal(normalized, text);
1336
1117
  },
1337
1118
  renderScreen: () => {},
@@ -1345,79 +1126,17 @@ async function runChatRust(projectRoot, options = {}) {
1345
1126
  setPending: (value) => {
1346
1127
  controller.session.pending = value || null;
1347
1128
  },
1348
- resolveStatusLine: (text) => {
1349
- if (agentViewId) {
1350
- publish("agent.view.status", { text: stripTags(text || "ready") });
1351
- return;
1352
- }
1353
- publish("status.set", { text: stripTags(text || "ready") });
1354
- },
1355
- enqueueBusStatus: (text) => {
1356
- if (agentViewId) {
1357
- publish("agent.view.status", { text: stripTags(text) });
1358
- return;
1359
- }
1360
- publish("status.set", { text: stripTags(text) });
1129
+ resolveStatusLine: (text, data) => {
1130
+ if (data?.key && isInternalChild(data.key)) return;
1131
+ publish("status.set", { text: stripTags(text || "ready"), busy: false });
1361
1132
  },
1362
- resolveBusStatus: () => {
1363
- if (agentViewId) {
1364
- publish("agent.view.status", { text: "ready" });
1365
- return;
1366
- }
1367
- publish("status.set", { text: "ready" });
1133
+ enqueueBusStatus: (item) => {
1134
+ if (item?.key && isInternalChild(item.key)) return;
1135
+ publish("status.set", { text: stripTags(typeof item === "object" ? item.text : item), busy: true });
1368
1136
  },
1369
- getCurrentView: () => (agentViewId ? "agent" : "main"),
1370
- isAgentViewUsesBus: () => Boolean(agentViewId),
1371
- getViewingAgent: () => agentViewId || "",
1372
- isAgentEventForViewingAgent: (data, viewingAgent, publisher) => {
1373
- if (!viewingAgent) return false;
1374
- const candidates = [
1375
- viewingAgent,
1376
- publisher,
1377
- data && data.publisher,
1378
- data && data.target,
1379
- data && data.subscriber,
1380
- ].filter(Boolean).map(String);
1381
- return candidates.some((id) => (
1382
- id === viewingAgent
1383
- || id.endsWith(`:${viewingAgent}`)
1384
- || viewingAgent.endsWith(`:${id}`)
1385
- || viewingAgent === id
1386
- ));
1387
- },
1388
- writeToAgentTerm: (text, meta = {}) => {
1389
- if (!agentViewId) return;
1390
- const streamPayload = meta && meta.streamPayload && typeof meta.streamPayload === "object"
1391
- ? meta.streamPayload
1392
- : null;
1393
- const publisher = String((meta && meta.publisher) || agentViewId);
1394
- const raw = stripTags(text);
1395
- if (streamPayload) {
1396
- let id = streamIds.get(publisher);
1397
- if (!id) {
1398
- id = `av-stream-${publisher || "agent"}-${Date.now()}`;
1399
- streamIds.set(publisher, id);
1400
- publish("stream.start", { id, speaker: publisher });
1401
- }
1402
- if (raw) {
1403
- publish("stream.delta", { id, text: raw.replace(/\r\n/g, "\n").replace(/\r/g, "\n"), speaker: publisher });
1404
- }
1405
- if (streamPayload.done || meta.done) {
1406
- publish("stream.done", { id });
1407
- streamIds.delete(publisher);
1408
- publish("agent.view.status", { text: "ready" });
1409
- } else {
1410
- publish("agent.view.status", { text: "working" });
1411
- }
1412
- return;
1413
- }
1414
- if (!raw) return;
1415
- publish("agent.view.append", {
1416
- id: `av-${Date.now()}-${Math.random().toString(36).slice(2, 6)}`,
1417
- kind: meta && meta.kind ? meta.kind : "assistant",
1418
- text: raw.replace(/\r\n/g, "\n").replace(/\r/g, "\n"),
1419
- speaker: publisher,
1420
- });
1137
+ resolveBusStatus: (item) => {
1138
+ if (item?.key && isInternalChild(item.key)) return;
1139
+ publish("status.set", { text: "ready", busy: false });
1421
1140
  },
1422
1141
  beginStream: (...args) => controller.getStreamState().beginStream(...args),
1423
1142
  appendStreamDelta: (...args) => controller.getStreamState().appendStreamDelta(...args),
@@ -1430,18 +1149,12 @@ async function runChatRust(projectRoot, options = {}) {
1430
1149
  activity_state: value,
1431
1150
  activity_detail: options.detail || "",
1432
1151
  });
1433
- if (agentViewId && agentId === agentViewId) {
1434
- publish("agent.view.status", { text: value || "ready" });
1435
- }
1436
1152
  },
1437
1153
  clearTransientAgentState: (agentId) => {
1438
1154
  controller.patchAgentActivity(agentId, {
1439
1155
  activity_state: "",
1440
1156
  activity_detail: "",
1441
1157
  });
1442
- if (agentViewId && agentId === agentViewId) {
1443
- publish("agent.view.status", { text: "ready" });
1444
- }
1445
1158
  },
1446
1159
  });
1447
1160
 
@@ -1453,22 +1166,37 @@ async function runChatRust(projectRoot, options = {}) {
1453
1166
  return;
1454
1167
  }
1455
1168
  if (!msg || typeof msg !== "object") return;
1169
+ if ([IPC_RESPONSE_TYPES.BUS_SEND_OK, IPC_RESPONSE_TYPES.ERROR].includes(msg.type)) {
1170
+ // Older daemons return uncorrelated bus_send_ok; retain compatibility.
1171
+ const id = msg.request_id || (msg.type === IPC_RESPONSE_TYPES.BUS_SEND_OK
1172
+ ? internalSubmissions.keys().next().value : "");
1173
+ const submission = internalSubmissions.get(id);
1174
+ if (submission) {
1175
+ internalSubmissions.delete(id);
1176
+ if (submission.projectRoot === activeProjectRoot && msg.type === IPC_RESPONSE_TYPES.ERROR) {
1177
+ multiSession.acceptEvent(submission.agentId, { type: "submission_failed", message: submission.message,
1178
+ error: `Message delivery failed: ${msg.error || "unknown error"}` });
1179
+ }
1180
+ controller.requestDaemonStatus();
1181
+ return;
1182
+ }
1183
+ }
1184
+ if (msg.type === IPC_RESPONSE_TYPES.STATUS) {
1185
+ const root = msg.data && (msg.data.projectRoot || msg.data.project_root);
1186
+ if (root) {
1187
+ const { canonicalProjectRoot } = require("../runtime/projects");
1188
+ if (canonicalProjectRoot(root) !== canonicalProjectRoot(activeProjectRoot)) return;
1189
+ }
1190
+ const key = msg.data && msg.data.key;
1191
+ if (key && String(key).includes(":") && !hostApi.isInternalAgent(key)) return;
1192
+ }
1456
1193
  if (msg.type === IPC_RESPONSE_TYPES.BUS) {
1194
+ if (!isInternalDashboardEvent(msg.data || {}, (id) => controller.session.metaMap.get(id))) return;
1457
1195
  try { mirrorBusToMultiPanes(msg.data || {}); } catch { /* ignore */ }
1196
+ if (msg.data?.event === "agent_surface") return;
1458
1197
  }
1459
1198
  if (msg.type === IPC_RESPONSE_TYPES.BUS_SEND_OK) {
1460
- if (agentViewId) {
1461
- publish("agent.view.append", {
1462
- id: `av-ok-${Date.now()}`,
1463
- kind: "system",
1464
- text: "✓ Message delivered",
1465
- speaker: "",
1466
- });
1467
- publish("agent.view.status", { text: "ready" });
1468
- } else {
1469
- appendLocal("system", "✓ Message delivered");
1470
- publish("status.set", { text: "ready" });
1471
- }
1199
+ appendLocal("system", "✓ Message delivered");
1472
1200
  controller.requestDaemonStatus();
1473
1201
  return;
1474
1202
  }
@@ -1501,7 +1229,10 @@ async function runChatRust(projectRoot, options = {}) {
1501
1229
  });
1502
1230
 
1503
1231
  await daemonConnection.connect();
1504
- daemonSend = (req) => daemonConnection.send(req);
1232
+ daemonSend = (req) => {
1233
+ if (req.type === IPC_REQUEST_TYPES.PROMPT) mainSubmissionRev += 1;
1234
+ return daemonConnection.send(req);
1235
+ };
1505
1236
  controller.setSend(daemonSend);
1506
1237
 
1507
1238
  controller.start({
@@ -1547,6 +1278,7 @@ async function runChatRust(projectRoot, options = {}) {
1547
1278
  try { multiSession.stop(); } catch {}
1548
1279
  }
1549
1280
  controller.stop();
1281
+ internalSubmissions.clear();
1550
1282
  daemonConnection.markExit();
1551
1283
  daemonConnection.close();
1552
1284
  await host.close();