@zergai/ztc 0.26.2-dev.202611210149 → 0.26.4-dev.202611231953

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 (275) hide show
  1. package/README.md +590 -28
  2. package/bin/zmux-darwin-arm64 +0 -0
  3. package/bin/zmux-linux-x64 +0 -0
  4. package/bin/ztc-audio-darwin-arm64 +0 -0
  5. package/bin/ztc-capture-darwin-arm64 +0 -0
  6. package/bin/ztc-tui-darwin-arm64 +0 -0
  7. package/bin/ztc-tui-linux-x64 +0 -0
  8. package/bin/zterm-darwin-arm64 +2 -2
  9. package/dist/App.js +18 -1
  10. package/dist/agent/agent.d.ts +25 -1
  11. package/dist/agent/agent.js +26 -1
  12. package/dist/agent/attached_images.d.ts +58 -0
  13. package/dist/agent/attached_images.js +145 -0
  14. package/dist/agent/backends/anthropic.d.ts +14 -0
  15. package/dist/agent/backends/anthropic.js +49 -2
  16. package/dist/agent/backends/codex_app_server.d.ts +3 -1
  17. package/dist/agent/backends/codex_app_server.js +9 -6
  18. package/dist/agent/backends/codex_auth.js +3 -0
  19. package/dist/agent/backends/gemini.js +3 -0
  20. package/dist/agent/backends/host_effort_policy.js +14 -2
  21. package/dist/agent/backends/http_error.d.ts +17 -0
  22. package/dist/agent/backends/http_error.js +50 -1
  23. package/dist/agent/backends/index.js +2 -1
  24. package/dist/agent/backends/model_redirect.d.ts +47 -0
  25. package/dist/agent/backends/model_redirect.js +97 -0
  26. package/dist/agent/backends/openai_compatible.js +5 -0
  27. package/dist/agent/backends/openai_responses.d.ts +9 -1
  28. package/dist/agent/backends/openai_responses.js +21 -12
  29. package/dist/agent/backends/prompt_cache.d.ts +8 -0
  30. package/dist/agent/backends/prompt_cache.js +10 -0
  31. package/dist/agent/backends/provider_ids.d.ts +1 -1
  32. package/dist/agent/backends/provider_ids.js +1 -1
  33. package/dist/agent/backends/types.d.ts +8 -0
  34. package/dist/agent/backends/zerg-api.js +5 -1
  35. package/dist/agent/backends/zrouter_stream.d.ts +75 -9
  36. package/dist/agent/backends/zrouter_stream.js +250 -49
  37. package/dist/agent/commands/config.js +51 -4
  38. package/dist/agent/commands/connect.js +20 -0
  39. package/dist/agent/commands/credential_status.js +5 -3
  40. package/dist/agent/commands/debug.js +3 -0
  41. package/dist/agent/commands/diff.js +9 -5
  42. package/dist/agent/commands/emulation.js +7 -1
  43. package/dist/agent/commands/inbox.js +4 -3
  44. package/dist/agent/commands/mcp.js +3 -0
  45. package/dist/agent/commands/models.js +20 -3
  46. package/dist/agent/commands/permissions.js +16 -2
  47. package/dist/agent/commands/provider_selection.js +13 -6
  48. package/dist/agent/commands/settings.js +15 -4
  49. package/dist/agent/commands/status.js +23 -1
  50. package/dist/agent/commands/types.d.ts +34 -3
  51. package/dist/agent/commands/update.d.ts +2 -1
  52. package/dist/agent/commands/update.js +8 -3
  53. package/dist/agent/commands/zerg.d.ts +6 -0
  54. package/dist/agent/commands/zerg.js +152 -28
  55. package/dist/agent/commands/ztc_pane.js +8 -5
  56. package/dist/agent/factory.js +22 -4
  57. package/dist/agent/request_encoder.js +11 -2
  58. package/dist/agent/runtime/command_jobs.js +16 -1
  59. package/dist/agent/runtime/command_panes.js +4 -1
  60. package/dist/agent/runtime/execution_context.d.ts +8 -1
  61. package/dist/agent/runtime/execution_context.js +8 -2
  62. package/dist/agent/runtime/policy.d.ts +66 -5
  63. package/dist/agent/runtime/policy.js +161 -33
  64. package/dist/agent/runtime/remote_broker_transport.d.ts +7 -0
  65. package/dist/agent/runtime/remote_broker_transport.js +33 -3
  66. package/dist/agent/runtime/remote_resource_errors.d.ts +7 -0
  67. package/dist/agent/runtime/remote_resource_errors.js +10 -0
  68. package/dist/agent/runtime/remote_resources.d.ts +1 -1
  69. package/dist/agent/runtime/remote_resources.js +7 -3
  70. package/dist/agent/runtime/source_verification.js +15 -12
  71. package/dist/agent/runtime/tool_runtime.js +165 -58
  72. package/dist/agent/runtime/verifier_capture.d.ts +1 -1
  73. package/dist/agent/runtime/verifier_capture.js +6 -0
  74. package/dist/agent/runtime/work_base.js +46 -14
  75. package/dist/agent/tools/adaptation.js +29 -12
  76. package/dist/agent/tools/apply_patch.js +62 -5
  77. package/dist/agent/tools/background.js +2 -0
  78. package/dist/agent/tools/benchmark_cpu.js +2 -0
  79. package/dist/agent/tools/bus_message.d.ts +4 -2
  80. package/dist/agent/tools/bus_message.js +21 -3
  81. package/dist/agent/tools/delegate.js +2 -0
  82. package/dist/agent/tools/file.d.ts +12 -1
  83. package/dist/agent/tools/file.js +92 -11
  84. package/dist/agent/tools/lsp.d.ts +12 -0
  85. package/dist/agent/tools/lsp.js +61 -5
  86. package/dist/agent/tools/pane.js +20 -45
  87. package/dist/agent/tools/project_context.js +5 -2
  88. package/dist/agent/tools/routine.js +20 -4
  89. package/dist/agent/tools/screenshot.d.ts +8 -0
  90. package/dist/agent/tools/screenshot.js +39 -25
  91. package/dist/agent/tools/search.js +31 -3
  92. package/dist/agent/tools/shell.js +7 -1
  93. package/dist/agent/tools/supervision.js +6 -0
  94. package/dist/agent/tools/types.d.ts +18 -0
  95. package/dist/agent/tools/zerg.js +15 -19
  96. package/dist/agent/tools/ztc_command.d.ts +15 -4
  97. package/dist/agent/tools/ztc_command.js +48 -34
  98. package/dist/app/agent_runner.js +8 -6
  99. package/dist/app/conversation_compaction.d.ts +6 -3
  100. package/dist/app/conversation_compaction.js +6 -3
  101. package/dist/app/tool_callbacks.js +1 -0
  102. package/dist/audio/sfx.js +6 -0
  103. package/dist/batch/worktree.js +22 -10
  104. package/dist/board/client.js +2 -1
  105. package/dist/cli_main.d.ts +1 -0
  106. package/dist/cli_main.js +141 -45
  107. package/dist/cli_ratatui.d.ts +49 -4
  108. package/dist/cli_ratatui.js +355 -79
  109. package/dist/conductor_branch.js +4 -1
  110. package/dist/config/host_credentials.d.ts +157 -0
  111. package/dist/config/host_credentials.js +589 -0
  112. package/dist/config/model_pin.d.ts +111 -0
  113. package/dist/config/model_pin.js +182 -0
  114. package/dist/config/own_state_files.d.ts +35 -0
  115. package/dist/config/own_state_files.js +161 -0
  116. package/dist/config/permission_pin.d.ts +148 -0
  117. package/dist/config/permission_pin.js +203 -0
  118. package/dist/config/permissions.d.ts +9 -2
  119. package/dist/config/permissions.js +14 -3
  120. package/dist/config/platform_login.d.ts +72 -0
  121. package/dist/config/platform_login.js +111 -0
  122. package/dist/config/profile_limits.d.ts +34 -0
  123. package/dist/config/profile_limits.js +66 -0
  124. package/dist/config/provider_api_key_env.js +3 -2
  125. package/dist/config/provider_models.d.ts +6 -1
  126. package/dist/config/provider_models.js +38 -11
  127. package/dist/config/repo_config.d.ts +22 -0
  128. package/dist/config/repo_config.js +77 -3
  129. package/dist/config/router_runtime_grant.d.ts +53 -0
  130. package/dist/config/router_runtime_grant.js +131 -0
  131. package/dist/config/settings_merge.d.ts +22 -0
  132. package/dist/config/settings_merge.js +75 -0
  133. package/dist/config/types.d.ts +8 -0
  134. package/dist/config.d.ts +334 -4
  135. package/dist/config.js +1024 -41
  136. package/dist/context/project_intelligence.d.ts +11 -2
  137. package/dist/context/project_intelligence.js +38 -15
  138. package/dist/desktop/daily/desktop_wiring.js +8 -6
  139. package/dist/desktop/daily/native_pane_launcher.js +2 -1
  140. package/dist/desktop/daily/native_tool.d.ts +1 -1
  141. package/dist/desktop/daily/native_tool.js +4 -3
  142. package/dist/desktop/daily/process_identity.js +10 -2
  143. package/dist/desktop/daily/source_baseline.js +4 -3
  144. package/dist/diagnostics/native_terminal_benchmark.js +3 -2
  145. package/dist/diagnostics/session_export.js +27 -14
  146. package/dist/embedded/anempa_runtime.js +3 -3
  147. package/dist/embedded/gameboy_runtime.js +5 -4
  148. package/dist/embedded/zergpod_runtime.js +5 -4
  149. package/dist/emulation/catalog.js +6 -1
  150. package/dist/emulation/tool_adapters.js +13 -3
  151. package/dist/environment/store.js +5 -4
  152. package/dist/extension_discovery.d.ts +12 -2
  153. package/dist/extension_discovery.js +52 -7
  154. package/dist/headless.d.ts +8 -0
  155. package/dist/headless.js +52 -13
  156. package/dist/headless_cli.js +16 -1
  157. package/dist/headless_workflow_trigger.js +1 -1
  158. package/dist/hooks/dispatcher.d.ts +8 -0
  159. package/dist/hooks/dispatcher.js +22 -2
  160. package/dist/hooks/lifecycle.d.ts +6 -0
  161. package/dist/hooks/lifecycle.js +1 -0
  162. package/dist/hooks/program_files.d.ts +10 -0
  163. package/dist/hooks/program_files.js +175 -0
  164. package/dist/hooks/runner.d.ts +11 -1
  165. package/dist/hooks/runner.js +112 -11
  166. package/dist/hooks/types.d.ts +2 -0
  167. package/dist/host_credentials_startup.d.ts +6 -0
  168. package/dist/host_credentials_startup.js +17 -0
  169. package/dist/instance_registry.d.ts +6 -2
  170. package/dist/instance_registry.js +9 -15
  171. package/dist/intake/format.d.ts +6 -0
  172. package/dist/intake/format.js +9 -0
  173. package/dist/intake/notifications.js +2 -1
  174. package/dist/lsp/manager.d.ts +1 -1
  175. package/dist/lsp/manager.js +4 -5
  176. package/dist/mcp/client.d.ts +2 -0
  177. package/dist/mcp/client.js +116 -18
  178. package/dist/mcp/registry.d.ts +12 -0
  179. package/dist/mcp/registry.js +15 -1
  180. package/dist/modes/handlers.js +14 -5
  181. package/dist/panes/agent_cli.d.ts +23 -1
  182. package/dist/panes/agent_cli.js +96 -1
  183. package/dist/panes/agent_panes.d.ts +27 -4
  184. package/dist/panes/agent_panes.js +73 -11
  185. package/dist/panes/editor_program.d.ts +8 -0
  186. package/dist/panes/editor_program.js +25 -0
  187. package/dist/panes/motsi.d.ts +11 -1
  188. package/dist/panes/motsi.js +14 -1
  189. package/dist/panes/pane_registry.d.ts +28 -1
  190. package/dist/panes/pane_registry.js +25 -0
  191. package/dist/panes/worker_route.js +5 -3
  192. package/dist/panes/ztc_launch.d.ts +8 -0
  193. package/dist/panes/ztc_launch.js +26 -1
  194. package/dist/profile_baseline.js +2 -0
  195. package/dist/router_auth.d.ts +8 -3
  196. package/dist/router_auth.js +34 -2
  197. package/dist/runtime_info.js +56 -38
  198. package/dist/session.js +12 -2
  199. package/dist/shannon/runtime.js +13 -1
  200. package/dist/simulation/domains/loader.d.ts +2 -1
  201. package/dist/simulation/domains/loader.js +19 -2
  202. package/dist/simulation/living/context.d.ts +8 -8
  203. package/dist/simulation/living/types.d.ts +2 -2
  204. package/dist/simulation/memory/embedding.d.ts +2 -1
  205. package/dist/simulation/memory/embedding.js +25 -6
  206. package/dist/terminal_attach.js +2 -1
  207. package/dist/types.d.ts +21 -0
  208. package/dist/ui/ratatui/headless_pane_manager.d.ts +12 -6
  209. package/dist/ui/ratatui/headless_pane_manager.js +65 -21
  210. package/dist/ui/ratatui/ipc_path.d.ts +41 -2
  211. package/dist/ui/ratatui/ipc_path.js +164 -5
  212. package/dist/ui/ratatui/pane_credential_broker.d.ts +17 -4
  213. package/dist/ui/ratatui/pane_credential_broker.js +23 -5
  214. package/dist/ui/ratatui/pane_relay.d.ts +74 -0
  215. package/dist/ui/ratatui/pane_relay.js +188 -13
  216. package/dist/ui/ratatui/process.d.ts +15 -1
  217. package/dist/ui/ratatui/process.js +129 -163
  218. package/dist/ui/ratatui/protocol.d.ts +11 -1
  219. package/dist/ui/ratatui/protocol.js +6 -0
  220. package/dist/ui/ratatui/ztc_launch_trust.d.ts +87 -0
  221. package/dist/ui/ratatui/ztc_launch_trust.js +249 -0
  222. package/dist/utils/admitted_open.d.ts +40 -0
  223. package/dist/utils/admitted_open.js +111 -0
  224. package/dist/utils/clipboard.js +2 -1
  225. package/dist/utils/computer_native.js +12 -56
  226. package/dist/utils/dictation.js +15 -12
  227. package/dist/utils/dictation_native.js +17 -60
  228. package/dist/utils/git_probe_options.d.ts +28 -0
  229. package/dist/utils/git_probe_options.js +30 -0
  230. package/dist/utils/git_program_files.d.ts +145 -0
  231. package/dist/utils/git_program_files.js +1282 -0
  232. package/dist/utils/git_repository_programs.d.ts +13 -0
  233. package/dist/utils/git_repository_programs.js +38 -0
  234. package/dist/utils/host_shell.js +2 -1
  235. package/dist/utils/idle_compaction.js +8 -1
  236. package/dist/utils/image_fit.d.ts +4 -1
  237. package/dist/utils/image_fit.js +6 -2
  238. package/dist/utils/image_preview.js +3 -2
  239. package/dist/utils/mirror_media.d.ts +22 -4
  240. package/dist/utils/mirror_media.js +87 -86
  241. package/dist/utils/ml_native.js +11 -56
  242. package/dist/utils/native_install.d.ts +55 -0
  243. package/dist/utils/native_install.js +107 -0
  244. package/dist/utils/open_url.js +2 -0
  245. package/dist/utils/process_end.d.ts +16 -0
  246. package/dist/utils/process_end.js +73 -0
  247. package/dist/utils/router_catalog.js +9 -2
  248. package/dist/utils/sandbox.d.ts +64 -1
  249. package/dist/utils/sandbox.js +397 -89
  250. package/dist/utils/shell.js +31 -12
  251. package/dist/utils/subprocess_env.d.ts +218 -2
  252. package/dist/utils/subprocess_env.js +429 -11
  253. package/dist/utils/system_ps.d.ts +5 -0
  254. package/dist/utils/system_ps.js +30 -0
  255. package/dist/utils/tts.js +5 -0
  256. package/dist/utils/tts_native.js +13 -60
  257. package/dist/utils/zlicky_overlay.js +17 -50
  258. package/dist/utils/ztc_own_state.d.ts +58 -0
  259. package/dist/utils/ztc_own_state.js +236 -0
  260. package/dist/web/mirror_authenticator.d.ts +67 -6
  261. package/dist/web/mirror_authenticator.js +131 -22
  262. package/dist/web/mirror_stream.d.ts +54 -10
  263. package/dist/web/mirror_stream.js +379 -92
  264. package/dist/web/observer_query_redaction.js +53 -13
  265. package/dist/web/observer_secret_redaction.d.ts +59 -0
  266. package/dist/web/observer_secret_redaction.js +714 -0
  267. package/dist/web/pane_credentials.d.ts +20 -2
  268. package/dist/web/pane_credentials.js +58 -5
  269. package/dist/worker/executor.js +3 -1
  270. package/dist/workspace_collision.js +19 -15
  271. package/dist/zerg/local_zerg_launches.d.ts +27 -0
  272. package/dist/zerg/local_zerg_launches.js +198 -0
  273. package/dist/zlm-embed.mjs +6257 -2791
  274. package/npm-shrinkwrap.json +1 -1
  275. package/package.json +42 -15
@@ -9,14 +9,22 @@
9
9
  * Both TuiProcess and PaneRelayClient implement PaneCommandTransport,
10
10
  * so pane.ts tools work identically regardless of which is wired up.
11
11
  */
12
+ import { randomUUID } from 'node:crypto';
12
13
  import { createServer, createConnection } from 'net';
13
14
  import { createInterface } from 'readline';
14
- import { serializeCommand, parseEvent, CAPABILITY_PANE_DELIVERY_ACK, CAPABILITY_DESKTOP_TASK_CONTROLS, CAPABILITY_DESKTOP_RUNTIME_CONTROLS, CAPABILITY_DESKTOP_RUNTIME_CHOICES, CAPABILITY_DESKTOP_SESSION_SHARE, CAPABILITY_SYNCHRONIZED_OUTPUT, CAPABILITY_ATTENTION_BADGES, CAPABILITY_PANE_RELAY_IDENTITY, } from './protocol.js';
15
- import { runtimeDiag } from '../../utils/debug_log.js';
16
- import { generateIpcPath, removeOwnedIpcPath } from './ipc_path.js';
15
+ import { serializeCommand, parseEvent, CAPABILITY_PANE_DELIVERY_ACK, CAPABILITY_DESKTOP_TASK_CONTROLS, CAPABILITY_DESKTOP_RUNTIME_CONTROLS, CAPABILITY_DESKTOP_RUNTIME_CHOICES, CAPABILITY_DESKTOP_SESSION_SHARE, CAPABILITY_SYNCHRONIZED_OUTPUT, CAPABILITY_ATTENTION_BADGES, CAPABILITY_PANE_RELAY_IDENTITY, CAPABILITY_ZTC_LAUNCH_NONCE, } from './protocol.js';
16
+ import { hostCredential, ROUTER_RUNTIME_GRANT_ENV, withoutHostHandoff } from '../../config/host_credentials.js';
17
+ import { withoutExtensionRootFlags } from '../../extension_discovery.js';
18
+ import { throughMotsi } from '../../panes/motsi.js';
19
+ import { debugLog, runtimeDiag } from '../../utils/debug_log.js';
20
+ import { onProcessEnd } from '../../utils/process_end.js';
21
+ import { ztcChildDiscoversExplicitly } from '../../utils/subprocess_env.js';
22
+ import { generateIpcPath, hasOwnIpcDirectory, removeOwnedIpcPath, restoreOwnedIpcDirectory, sweepStaleIpcDirectories } from './ipc_path.js';
17
23
  import { markRelayed } from './pane_credential_broker.js';
24
+ import { isRatatuiBinaryAvailable, ztcPaneRendererMissing } from './process.js';
18
25
  import { relayVerdict } from './relay_policy.js';
19
26
  import { paneReadRefusal } from './screen_privacy.js';
27
+ import { ownZtcLaunch, relayedZtcPaneEnv, ZtcLaunchNonces } from './ztc_launch_trust.js';
20
28
  // ============================================================================
21
29
  // PaneRelayServer — runs in parent ZTC (--mux mode)
22
30
  // ============================================================================
@@ -37,18 +45,38 @@ const PANE_EVENT_TYPES = new Set([
37
45
  function isPaneId(value) {
38
46
  return Number.isInteger(value) && value >= 0 && value <= 0xffff_ffff;
39
47
  }
40
- /** The relays listening in this process, which stop as it exits (stopListeningRelaysAtExit). */
48
+ function isLaunchNonceRequest(value) {
49
+ return !!value && typeof value === 'object' && value.type === 'ztc_launch_nonce';
50
+ }
51
+ function isZtcPaneType(paneType) {
52
+ return paneType === 'ztc' || paneType === 'colony_ztc';
53
+ }
54
+ /** The relays bound or listening in this process, which stop as it ends (stopListeningRelaysAtExit). */
41
55
  const listeningRelays = new Set();
42
56
  let stopsRelaysAtExit = false;
43
57
  /**
44
- * Stop each relay still listening as the process exits, so its socket, and the private directory that holds it where it
58
+ * Stop each relay still listening as the process ends, so its socket, and the private directory that holds it where it
45
59
  * has one (generateIpcPath), go with it: an exit runs no close callback, and a ZTC that exits without its exit path
46
- * stopping the relay left both in /tmp.
60
+ * stopping the relay left both in /tmp, as did a process a signal ended that nothing in it handled (onProcessEnd).
47
61
  */
48
62
  function stopListeningRelaysAtExit() {
49
63
  for (const relay of [...listeningRelays])
50
64
  relay.stop();
51
65
  }
66
+ let staleRelaySweep = false;
67
+ /**
68
+ * Once per process, as its relay starts in a private directory of its own in /tmp (generateIpcPath: Linux's shared
69
+ * /tmp, or a temporary directory others can open), remove in the background the directories there whose relays are
70
+ * gone, which a SIGKILL or a crash leaves and nothing else removes (sweepStaleIpcDirectories): a live relay's, one still
71
+ * starting and anything else are kept. A relay whose socket is in a temporary directory of this user's alone leaves
72
+ * nothing in /tmp, and sweeps nothing.
73
+ */
74
+ export function startStaleRelaySweep(endpoint) {
75
+ if (staleRelaySweep || !hasOwnIpcDirectory(endpoint))
76
+ return;
77
+ staleRelaySweep = true;
78
+ void sweepStaleIpcDirectories('ztc-relay').then(({ examined, removed }) => debugLog(`[relay] Swept ${removed.length} of ${examined} stale relay directories in /tmp`), error => debugLog(`[relay] Stale relay directory sweep failed: ${String(error)}`));
79
+ }
52
80
  export class PaneRelayServer {
53
81
  transport;
54
82
  server = null;
@@ -57,10 +85,92 @@ export class PaneRelayServer {
57
85
  tuiUnsubscribe = null;
58
86
  childCommandListeners = new Set();
59
87
  agentCliOf;
88
+ ownZtcLaunch;
89
+ ztcPanesThroughMotsi;
90
+ /** The launch nonces this relay issued its children, each for one connection and one launch (ztc_launch_trust.ts). */
91
+ launchNonces = new ZtcLaunchNonces();
60
92
  constructor(transport, socketPath, options = {}) {
61
93
  this.transport = transport;
62
94
  this.socketPath = socketPath;
63
95
  this.agentCliOf = options.agentCliOf;
96
+ this.ownZtcLaunch = options.ownZtcLaunch ?? ((command, args) => ownZtcLaunch(command, args));
97
+ this.ztcPanesThroughMotsi = options.ztcPanesThroughMotsi ?? (() => null);
98
+ }
99
+ /**
100
+ * What a launch nonce for OWN starts: OWN's program, ZTC's own as this process found it, inside the motsi its owner
101
+ * starts its own ZTC panes through when the child asked for MotSi, else directly. Never inside MotSi in a process
102
+ * that holds a host's Router runtime grant, which goes to the ZTC panes it opens and no program around one: held, that
103
+ * is, in the host-credential store, which took it out of process.env as ZTC started (hostCredential). A pane that
104
+ * discovers extensions explicitly (ztcChildDiscoversExplicitly) starts without the launch flags that add an extension
105
+ * root (withoutExtensionRootFlags), as ZTC's own launches of one do: its roots are the ones its host set. And none
106
+ * starts with a handoff of variables the client's arguments name (withoutHostHandoff), which could set the Router its
107
+ * grant goes to: only ZTC writes one, for a launch of its own.
108
+ */
109
+ relayedLaunch(own) {
110
+ const motsi = own.motsiTitle !== undefined && !hostCredential(ROUTER_RUNTIME_GRANT_ENV)?.trim() ? this.ztcPanesThroughMotsi() : null;
111
+ const program = { command: own.command, args: withoutHostHandoff(ztcChildDiscoversExplicitly() ? withoutExtensionRootFlags(own.args) : own.args) };
112
+ return motsi ? { ...throughMotsi(motsi, own.motsiTitle, program), throughMotsi: true } : { ...program, throughMotsi: false };
113
+ }
114
+ /**
115
+ * Answer CLIENT's request for a launch nonce: one for its exact command and arguments when they launch ZTC from this
116
+ * process's install (ownZtcLaunch) and this install opens ZTC panes at all, as create_ztc_pane does only with a
117
+ * ztc-tui renderer in it (isRatatuiBinaryAvailable); else an error. Either way, to this child alone.
118
+ */
119
+ answerLaunchNonce(client, request) {
120
+ const requestId = typeof request.request_id === 'string' ? request.request_id : undefined;
121
+ const args = request.args === undefined ? [] : request.args;
122
+ let answer;
123
+ if (typeof request.command !== 'string' || !Array.isArray(args) || !args.every(arg => typeof arg === 'string')) {
124
+ answer = { type: 'error', message: 'A launch nonce is asked for a command and its arguments, as text.', ...(requestId ? { request_id: requestId } : {}) };
125
+ }
126
+ else if (!isRatatuiBinaryAvailable()) {
127
+ answer = { type: 'error', message: ztcPaneRendererMissing(), ...(requestId ? { request_id: requestId } : {}) };
128
+ }
129
+ else {
130
+ const own = this.ownZtcLaunch(request.command, args);
131
+ answer = own
132
+ ? { type: 'ztc_launch_nonce', ...(requestId ? { request_id: requestId } : {}),
133
+ nonce: this.launchNonces.issue(client, request.command, args, this.relayedLaunch(own)) }
134
+ : { type: 'error', message: 'The workspace starts a ZTC pane only for a launch of its own ZTC install; this one is not, so it '
135
+ + 'opens only as a process pane.', ...(requestId ? { request_id: requestId } : {}) };
136
+ }
137
+ try {
138
+ client.write(JSON.stringify(answer) + '\n');
139
+ }
140
+ catch { /* the close handler retires this client */ }
141
+ }
142
+ /**
143
+ * What COMMAND, a create_pane or replace_pane CLIENT sent, starts: on a launch nonce this relay issued CLIENT for its
144
+ * exact command and arguments, a ZTC pane running the launch the nonce was issued for (relayedLaunch), not the
145
+ * command the client sent, with only the environment ZTC's pane tools send one (relayedZtcPaneEnv); on any other
146
+ * nonce, nothing (null: CLIENT is told why); without one, a process pane, whatever type it names (a replace that names
147
+ * none would keep the type of the pane it replaces). Any other command goes on as it is.
148
+ */
149
+ launchTrust(client, command) {
150
+ if (command.type !== 'create_pane' && command.type !== 'replace_pane')
151
+ return { command, ztcLaunch: false };
152
+ const { launch_nonce: nonce, ...launch } = command;
153
+ if (nonce !== undefined) {
154
+ const relayed = typeof nonce === 'string' && launch.pane_type === 'ztc'
155
+ ? this.launchNonces.redeem(nonce, client, launch.command, launch.args) : null;
156
+ if (relayed) {
157
+ const started = { ...launch, command: relayed.command, args: [...relayed.args] };
158
+ const trusted = started.type === 'create_pane'
159
+ ? { ...started, env: relayedZtcPaneEnv(started.env, this.socketPath, { throughMotsi: relayed.throughMotsi }) } : started;
160
+ return { command: trusted, ztcLaunch: true };
161
+ }
162
+ const refusal = { type: 'error', message: 'The workspace refused this launch: its launch nonce was not issued to this connection for '
163
+ + 'this exact command and arguments, was used already, or has expired.', ...(launch.request_id ? { request_id: launch.request_id } : {}) };
164
+ try {
165
+ client.write(JSON.stringify(refusal) + '\n');
166
+ }
167
+ catch { /* the close handler retires this client */ }
168
+ return null;
169
+ }
170
+ if (isZtcPaneType(launch.pane_type) || (launch.type === 'replace_pane' && launch.command && launch.pane_type === undefined)) {
171
+ return { command: { ...launch, pane_type: 'process' }, ztcLaunch: false };
172
+ }
173
+ return { command: launch, ztcLaunch: false };
64
174
  }
65
175
  /**
66
176
  * EVENT as children get it: a pane list says which panes run an agent CLI (see agentCliOf), and a pane's text the
@@ -101,7 +211,7 @@ export class PaneRelayServer {
101
211
  client.write(JSON.stringify({ type: 'ready', version: 'pane-relay', cols: 0, rows: 0,
102
212
  capabilities: [...[CAPABILITY_PANE_DELIVERY_ACK, 'pane_labels_v1', CAPABILITY_DESKTOP_TASK_CONTROLS, CAPABILITY_DESKTOP_RUNTIME_CONTROLS,
103
213
  CAPABILITY_DESKTOP_RUNTIME_CHOICES, CAPABILITY_DESKTOP_SESSION_SHARE, CAPABILITY_SYNCHRONIZED_OUTPUT, CAPABILITY_ATTENTION_BADGES]
104
- .filter(capability => this.transport.hasCapability?.(capability)), CAPABILITY_PANE_RELAY_IDENTITY],
214
+ .filter(capability => this.transport.hasCapability?.(capability)), CAPABILITY_PANE_RELAY_IDENTITY, CAPABILITY_ZTC_LAUNCH_NONCE],
105
215
  }) + '\n');
106
216
  // A connection names its pane once, with its first relay_hello, whenever it comes: a child whose main ZTC was busy
107
217
  // as it connected may have sent lines before its hello reached here (its connect() resolves after a short grace
@@ -133,6 +243,11 @@ export class PaneRelayServer {
133
243
  }
134
244
  return;
135
245
  }
246
+ // A launch nonce a child asks for is answered here, to it alone; nothing reaches the renderer.
247
+ if (isLaunchNonceRequest(parsed)) {
248
+ this.answerLaunchNonce(client, parsed);
249
+ return;
250
+ }
136
251
  // Only what a child may send (relay_policy.ts) is forwarded; anything else is answered with an error, to this
137
252
  // child alone.
138
253
  const verdict = relayVerdict(parsed);
@@ -158,10 +273,15 @@ export class PaneRelayServer {
158
273
  return;
159
274
  }
160
275
  }
276
+ // A pane a child opens or relaunches is a ZTC pane only on a launch nonce this relay issued it; else a process
277
+ // pane (launchTrust). A nonce it was not issued for opens nothing.
278
+ const launch = this.launchTrust(client, verdict.command);
279
+ if (launch === null)
280
+ return;
161
281
  // Forward to parent's TuiProcess, marked as a pane's, the one this connection announced: a pane it opens gets a
162
282
  // plain credential of its own, never a workspace monitor's, which records that pane as the one that opened it,
163
283
  // and a pane it relaunches in place gets none (PaneCredentialBroker).
164
- const command = markRelayed(verdict.command, announced);
284
+ const command = markRelayed(launch.command, announced, launch.ztcLaunch);
165
285
  this.transport.send(command);
166
286
  for (const listener of [...this.childCommandListeners]) {
167
287
  try {
@@ -177,10 +297,18 @@ export class PaneRelayServer {
177
297
  });
178
298
  client.on('close', () => {
179
299
  this.clients.delete(client);
300
+ this.launchNonces.forget(client);
180
301
  rl.close();
181
302
  });
182
303
  });
183
304
  const server = this.server;
305
+ // From before it binds, which listen() does at once, a tick ahead of 'listening': a process ending in between would
306
+ // leave the bound socket behind. A listen that fails stops the relay, which takes it out again.
307
+ listeningRelays.add(this);
308
+ if (!stopsRelaysAtExit) {
309
+ stopsRelaysAtExit = true;
310
+ onProcessEnd(stopListeningRelaysAtExit);
311
+ }
184
312
  try {
185
313
  await new Promise((resolve, reject) => {
186
314
  const failed = (error) => reject(error);
@@ -191,13 +319,11 @@ export class PaneRelayServer {
191
319
  runtimeDiag('error', `Pane relay stopped: ${error.message}`);
192
320
  this.stop();
193
321
  });
194
- listeningRelays.add(this);
195
- if (!stopsRelaysAtExit) {
196
- stopsRelaysAtExit = true;
197
- process.on('exit', stopListeningRelaysAtExit);
198
- }
199
322
  resolve();
200
323
  });
324
+ // Its private directory, made again if a sweep took it before it got here (one that isn't this user's alone
325
+ // any more refuses the relay, as an occupied address does).
326
+ restoreOwnedIpcDirectory(this.socketPath);
201
327
  server.listen(this.socketPath);
202
328
  });
203
329
  }
@@ -273,6 +399,8 @@ export class PaneRelayClient {
273
399
  capabilities = new Set();
274
400
  socketPath;
275
401
  paneId;
402
+ /** ZTC pane launches waiting for the launch nonce asked for them, by the request id it was asked under. */
403
+ pendingLaunches = new Map();
276
404
  /** `paneId` is this child's pane (ZTC_PANE_ID): it names it to a relay that asks, before anything else it sends. */
277
405
  constructor(socketPath, options = {}) {
278
406
  this.socketPath = socketPath;
@@ -322,6 +450,8 @@ export class PaneRelayClient {
322
450
  return;
323
451
  this.readline = createInterface({ input: this.socket, crlfDelay: Infinity });
324
452
  this.readline.on('line', (line) => {
453
+ if (this.answeredLaunch(line))
454
+ return;
325
455
  const event = parseEvent(line);
326
456
  if (!event)
327
457
  return;
@@ -359,10 +489,54 @@ export class PaneRelayClient {
359
489
  this.cleanup();
360
490
  });
361
491
  }
492
+ /**
493
+ * LINE from the relay, when it answers a launch nonce asked for: the launch waiting for it goes, with the nonce, and the
494
+ * line is this transport's own (true). A refusal goes on to the handlers as the launch's own refusal, and the launch
495
+ * is dropped.
496
+ */
497
+ answeredLaunch(line) {
498
+ let answer;
499
+ try {
500
+ answer = JSON.parse(line);
501
+ }
502
+ catch {
503
+ return false;
504
+ }
505
+ if (!answer || typeof answer !== 'object' || typeof answer.request_id !== 'string')
506
+ return false;
507
+ const launch = this.pendingLaunches.get(answer.request_id);
508
+ if (!launch || (answer.type !== 'ztc_launch_nonce' && answer.type !== 'error'))
509
+ return false;
510
+ this.pendingLaunches.delete(answer.request_id);
511
+ if (answer.type === 'error')
512
+ return false;
513
+ if (typeof answer.nonce !== 'string')
514
+ return true;
515
+ try {
516
+ this.socket?.write(serializeCommand({ ...launch, launch_nonce: answer.nonce }) + '\n');
517
+ }
518
+ catch { /* the close handler retires this transport */ }
519
+ return true;
520
+ }
362
521
  /** Send a pane command to the parent via relay */
363
522
  send(cmd) {
364
523
  if (!this.socket || !this.ready)
365
524
  return false;
525
+ // A ZTC pane opens through a relay that asks for one only on a launch nonce it issued for the exact launch: asked
526
+ // for first, and the launch sent with it (answeredLaunch).
527
+ if ((cmd.type === 'create_pane' || cmd.type === 'replace_pane') && cmd.pane_type === 'ztc' && cmd.command
528
+ && cmd.launch_nonce === undefined && this.capabilities.has(CAPABILITY_ZTC_LAUNCH_NONCE)) {
529
+ const requestId = cmd.request_id ?? `ztc-launch-${randomUUID()}`;
530
+ const request = { type: 'ztc_launch_nonce', request_id: requestId, command: cmd.command, args: cmd.args ?? [] };
531
+ try {
532
+ this.socket.write(JSON.stringify(request) + '\n');
533
+ }
534
+ catch {
535
+ return false;
536
+ }
537
+ this.pendingLaunches.set(requestId, cmd);
538
+ return true;
539
+ }
366
540
  try {
367
541
  const json = serializeCommand(cmd);
368
542
  this.socket.write(json + '\n');
@@ -389,6 +563,7 @@ export class PaneRelayClient {
389
563
  }
390
564
  cleanup() {
391
565
  this.capabilities.clear();
566
+ this.pendingLaunches.clear();
392
567
  if (this.readline) {
393
568
  this.readline.close();
394
569
  this.readline = null;
@@ -12,7 +12,7 @@ import type { OwnerPingCommand, RustCommand, RustEvent } from './protocol.js';
12
12
  import { type DesktopRuntime } from '../../desktop/runtime.js';
13
13
  export type TuiEventHandler = (event: RustEvent) => void;
14
14
  /**
15
- * Get the path to the zmux binary
15
+ * Get the path to the zmux binary, from ZTC's own install only (findNativeProgram), as ztc-tui's.
16
16
  */
17
17
  export declare function getZmuxBinaryPath(): string | null;
18
18
  /** Resolve the executable inside a standard installed ZTerm.app bundle. */
@@ -59,6 +59,12 @@ export declare function isTTY(): boolean;
59
59
  * Used for pane-mode child launches that inherit the parent's TTY.
60
60
  */
61
61
  export declare function isRatatuiBinaryAvailable(): boolean;
62
+ /**
63
+ * Why no ZTC pane opens where this install has no ztc-tui (isRatatuiBinaryAvailable): create_ztc_pane's refusal, and the
64
+ * pane relay's to a launch nonce asked for one. A source checkout is told to build it; a packed install, which would never
65
+ * run that build (utils/native_install.ts), that it ships none.
66
+ */
67
+ export declare function ztcPaneRendererMissing(): string;
62
68
  /**
63
69
  * Check if the Ratatui TUI renderer is available
64
70
  * Requires both the binary to exist and a TTY to be present
@@ -144,6 +150,12 @@ export declare class TuiProcess {
144
150
  * (see PaneCredentialBroker).
145
151
  */
146
152
  private readonly paneCredentials;
153
+ /**
154
+ * The one-time handoff of ZTC's credentials each ZTC pane the renderer started again in place was given, by pane id
155
+ * (withZtcChildHandoff): its program takes it as it starts, and one it never took goes when the pane closes, or
156
+ * when the renderer is gone.
157
+ */
158
+ private readonly hostHandoffs;
147
159
  /**
148
160
  * In-flight `read_clipboard_image` requests, keyed by request id. The
149
161
  * renderer answers asynchronously (it reads and PNG-encodes the clipboard
@@ -178,6 +190,8 @@ export declare class TuiProcess {
178
190
  */
179
191
  private emit;
180
192
  private dispatchPaneMutation;
193
+ /** Remove the handoff pane PANE_ID was given when it was started again in place, if its program never took it. */
194
+ private releaseHostHandoff;
181
195
  private timeoutPaneMutation;
182
196
  /** Whether zmux (multiplexer mode) is active */
183
197
  get isMuxMode(): boolean;