@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
@@ -23,7 +23,8 @@
23
23
  * working when its pane closes or its renderer exits. The pane transport that creates a pane mints from the store of
24
24
  * the port the pane will talk to (paneCredentialStoreForPort), and binds the credential when the pane appears.
25
25
  *
26
- * Self-contained (node builtins only), so the mirror, the pane transports and the CLI entry can all depend on it.
26
+ * Self-contained (node builtins and the host-credential store only), so the mirror, the pane transports and the CLI
27
+ * entry can all depend on it.
27
28
  */
28
29
  /** The pane's own mirror credential, in the environment of a pane whose launcher asked for it there (see above). */
29
30
  export declare const PANE_CREDENTIAL_ENV = "ZTC_MIRROR_PANE_TOKEN";
@@ -35,7 +36,11 @@ export declare const PANE_CREDENTIAL_HEADER = "x-ztc-pane-token";
35
36
  * transport clears them in every pane it creates before giving the pane a credential of its own.
36
37
  */
37
38
  export declare const OWNER_MIRROR_CREDENTIAL_ENV: readonly string[];
38
- /** ENV without the owner's mirror credentials (see OWNER_MIRROR_CREDENTIAL_ENV). */
39
+ /**
40
+ * ENV without the owner's mirror credentials (see OWNER_MIRROR_CREDENTIAL_ENV), under their names or any other: a
41
+ * variable that holds one of their values (blanks around it aside) goes too, since a host can hand one credential
42
+ * under two names, and no pane needs any of them (each is given its own).
43
+ */
39
44
  export declare function withoutMirrorCredentials(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv;
40
45
  /**
41
46
  * What a credential lets its pane do besides act as itself: 'workspace-monitor' also reads the workspace runtime and
@@ -104,6 +109,10 @@ export declare class PaneCredentialStore {
104
109
  * no credential of a ZTC that still joins it stands for the pane.
105
110
  */
106
111
  ztcExited(paneId: number): boolean;
112
+ /** Whether TOKEN is a credential this store holds now: bound to its pane, or still waiting for it. */
113
+ holds(token: string): boolean;
114
+ /** Whether this store holds any credential, bound or waiting. */
115
+ holdsAny(): boolean;
107
116
  /** The pane a presented credential stands for; 'pending' while it waits for its pane; null for anything else. */
108
117
  resolve(token: string): PanePrincipal | 'pending' | null;
109
118
  /**
@@ -116,6 +125,15 @@ export declare class PaneCredentialStore {
116
125
  private wake;
117
126
  private sweep;
118
127
  }
128
+ /** How long a pane credential is: issue mints 32 random bytes, which unpadded base64url writes in 43 characters. */
129
+ export declare const PANE_CREDENTIAL_LENGTH = 43;
130
+ /**
131
+ * Whether TOKEN is a pane credential a mirror in this process holds now (PaneCredentialStore.holds), as its digest
132
+ * says: a read-only observer is withheld every one of them wherever it appears (web/observer_secret_redaction.ts).
133
+ */
134
+ export declare function isLivePaneCredential(token: string): boolean;
135
+ /** Whether a mirror in this process holds any pane credential, bound or waiting for its pane. */
136
+ export declare function holdsPaneCredentials(): boolean;
119
137
  /**
120
138
  * The store a mirror that starts listening on PORT honours: the one an earlier mirror on the port left, so the panes
121
139
  * that talked to it rejoin this one, or a new one.
@@ -23,12 +23,14 @@
23
23
  * working when its pane closes or its renderer exits. The pane transport that creates a pane mints from the store of
24
24
  * the port the pane will talk to (paneCredentialStoreForPort), and binds the credential when the pane appears.
25
25
  *
26
- * Self-contained (node builtins only), so the mirror, the pane transports and the CLI entry can all depend on it.
26
+ * Self-contained (node builtins and the host-credential store only), so the mirror, the pane transports and the CLI
27
+ * entry can all depend on it.
27
28
  */
28
29
  import { createHash, randomBytes } from 'node:crypto';
29
30
  import { lstatSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
30
31
  import { tmpdir } from 'node:os';
31
32
  import { basename, dirname, join } from 'node:path';
33
+ import { hostEnvironmentValue } from '../config/host_credentials.js';
32
34
  /** The pane's own mirror credential, in the environment of a pane whose launcher asked for it there (see above). */
33
35
  export const PANE_CREDENTIAL_ENV = 'ZTC_MIRROR_PANE_TOKEN';
34
36
  /** The header a pane's programs present it in. */
@@ -43,11 +45,31 @@ export const OWNER_MIRROR_CREDENTIAL_ENV = Object.freeze([
43
45
  'ZTC_MIRROR_SHARED_SECRET',
44
46
  PANE_CREDENTIAL_ENV,
45
47
  ]);
46
- /** ENV without the owner's mirror credentials (see OWNER_MIRROR_CREDENTIAL_ENV). */
48
+ /**
49
+ * The shortest value of one of the owner's mirror credentials taken for it when another variable holds it as well, as
50
+ * subprocess_env.ts takes a stripped credential's copies: a shorter one (a port, a flag, a word) could be anyone's.
51
+ */
52
+ const MIRROR_CREDENTIAL_COPY_MIN_LENGTH = 12;
53
+ /**
54
+ * ENV without the owner's mirror credentials (see OWNER_MIRROR_CREDENTIAL_ENV), under their names or any other: a
55
+ * variable that holds one of their values (blanks around it aside) goes too, since a host can hand one credential
56
+ * under two names, and no pane needs any of them (each is given its own).
57
+ */
47
58
  export function withoutMirrorCredentials(env) {
48
59
  const copy = { ...env };
49
- for (const name of OWNER_MIRROR_CREDENTIAL_ENV)
60
+ const values = new Set();
61
+ for (const name of OWNER_MIRROR_CREDENTIAL_ENV) {
62
+ const value = env[name]?.trim();
63
+ if (value && value.length >= MIRROR_CREDENTIAL_COPY_MIN_LENGTH)
64
+ values.add(value);
50
65
  delete copy[name];
66
+ }
67
+ if (values.size > 0) {
68
+ for (const [name, value] of Object.entries(copy)) {
69
+ if (typeof value === 'string' && values.has(value.trim()))
70
+ delete copy[name];
71
+ }
72
+ }
51
73
  return copy;
52
74
  }
53
75
  /**
@@ -158,6 +180,15 @@ export class PaneCredentialStore {
158
180
  }
159
181
  return exited;
160
182
  }
183
+ /** Whether TOKEN is a credential this store holds now: bound to its pane, or still waiting for it. */
184
+ holds(token) {
185
+ const entry = this.entries.get(digest(token));
186
+ return entry !== undefined && (entry.paneId !== null || Date.now() < entry.pendingUntilMs);
187
+ }
188
+ /** Whether this store holds any credential, bound or waiting. */
189
+ holdsAny() {
190
+ return this.entries.size > 0;
191
+ }
161
192
  /** The pane a presented credential stands for; 'pending' while it waits for its pane; null for anything else. */
162
193
  resolve(token) {
163
194
  if (!token)
@@ -210,6 +241,27 @@ export class PaneCredentialStore {
210
241
  }
211
242
  /** Each port's store, from the first mirror in this process that listens on the port, for the life of the process. */
212
243
  const storesByPort = new Map();
244
+ /** How long a pane credential is: issue mints 32 random bytes, which unpadded base64url writes in 43 characters. */
245
+ export const PANE_CREDENTIAL_LENGTH = 43;
246
+ /**
247
+ * Whether TOKEN is a pane credential a mirror in this process holds now (PaneCredentialStore.holds), as its digest
248
+ * says: a read-only observer is withheld every one of them wherever it appears (web/observer_secret_redaction.ts).
249
+ */
250
+ export function isLivePaneCredential(token) {
251
+ if (token.length !== PANE_CREDENTIAL_LENGTH)
252
+ return false;
253
+ for (const store of storesByPort.values())
254
+ if (store.holds(token))
255
+ return true;
256
+ return false;
257
+ }
258
+ /** Whether a mirror in this process holds any pane credential, bound or waiting for its pane. */
259
+ export function holdsPaneCredentials() {
260
+ for (const store of storesByPort.values())
261
+ if (store.holdsAny())
262
+ return true;
263
+ return false;
264
+ }
213
265
  /**
214
266
  * The store a mirror that starts listening on PORT honours: the one an earlier mirror on the port left, so the panes
215
267
  * that talked to it rejoin this one, or a new one.
@@ -346,10 +398,11 @@ export function takePaneCredentialHandoff(path) {
346
398
  * per-pane credentials, the process-wide token that ZTC handed its panes. Empty when there is none of them.
347
399
  */
348
400
  export function paneMirrorCredentialHeaders(env = process.env) {
349
- const own = heldCredential ?? env[PANE_CREDENTIAL_ENV]?.trim();
401
+ // This process's own environment is read as it was started: the host-credential store holds what it was given.
402
+ const own = heldCredential ?? hostEnvironmentValue(PANE_CREDENTIAL_ENV, env)?.trim();
350
403
  if (own)
351
404
  return { [PANE_CREDENTIAL_HEADER]: own };
352
- const older = env.ZTC_MIRROR_INTERNAL_TOKEN?.trim();
405
+ const older = hostEnvironmentValue('ZTC_MIRROR_INTERNAL_TOKEN', env)?.trim();
353
406
  return older ? { 'x-ztc-internal-token': older } : {};
354
407
  }
355
408
  /**
@@ -46,6 +46,8 @@ export class InProcessWorkerExecutor {
46
46
  isSubagent: true,
47
47
  }, {
48
48
  approvalRequester: this.config.approvalRequester,
49
+ // A worker's hooks run under its command policy, as its commands do.
50
+ commandPolicy: () => this.config.commandPolicy,
49
51
  });
50
52
  const subAgent = new SubAgent({
51
53
  resumeSessionId: task.resumeSessionId,
@@ -59,7 +61,7 @@ export class InProcessWorkerExecutor {
59
61
  hooks,
60
62
  approvalRequester: this.config.approvalRequester,
61
63
  backend: this.config.backend,
62
- policy: new CapabilityPolicy(this.config.toolPermissions, this.config.permissionProfile, workerCwd),
64
+ policy: new CapabilityPolicy(this.config.toolPermissions, this.config.permissionProfile, workerCwd, { sandbox: this.config.commandPolicy?.sandbox }),
63
65
  commandPolicy: this.config.commandPolicy,
64
66
  temporaryWriteRoot,
65
67
  });
@@ -6,6 +6,8 @@ import { chmod, copyFile, link, lstat, mkdir, mkdtemp, open, readFile, readdir,
6
6
  import { homedir, tmpdir } from 'node:os';
7
7
  import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
8
8
  import { promisify } from 'node:util';
9
+ import { GIT_DIFF_OPTIONS, GIT_PROBE_OPTIONS } from './utils/git_probe_options.js';
10
+ import { buildGitSubprocessEnv, buildUserSubprocessEnv } from './utils/subprocess_env.js';
9
11
  import { deriveWorkspaceSessionWork, parseWorkspaceSessionWork, formatWorkspaceSessionWork } from './workspace_session_context.js';
10
12
  export class WorkspaceIsolationError extends Error {
11
13
  retainedTarget;
@@ -31,21 +33,23 @@ function ownersDirectory() {
31
33
  // All profiles share local coordination; selecting a profile cannot hide a sibling.
32
34
  return join(process.env.ZTC_CONFIG_DIR || join(homedir(), '.ztc'), 'workspace-owners');
33
35
  }
36
+ /**
37
+ * A user subprocess's environment: git runs the repository's hooks and helpers with it, so none of ZTC's credentials,
38
+ * and no repository, index or object store of ZTC's own environment (buildGitSubprocessEnv).
39
+ */
34
40
  function gitEnvironment() {
35
- const env = { ...process.env };
36
- for (const key of ['GIT_DIR', 'GIT_WORK_TREE', 'GIT_COMMON_DIR', 'GIT_INDEX_FILE'])
37
- delete env[key];
38
- return { ...env, GIT_OPTIONAL_LOCKS: '0', GIT_TERMINAL_PROMPT: '0' };
41
+ return { ...buildGitSubprocessEnv(), GIT_OPTIONAL_LOCKS: '0', GIT_TERMINAL_PROMPT: '0' };
39
42
  }
40
43
  /**
41
- * Options every git run here takes first, as runtime_info's probes do. Inspection and isolation run git in the work tree
42
- * the agent writes to, in ZTC's own process and outside any sandbox the agent works under: core.fsmonitor=false keeps a
43
- * core.fsmonitor command in the repository's config from running on a diff or an apply, and safe.bareRepository=explicit
44
- * keeps git from taking a bare repository planted in the work tree for the session's repository and running its config.
45
- * Both are honoured from the command line, which the agent cannot write, and leave normal work trees and linked
44
+ * Options every git run here takes first, as runtime_info's probes do, and each diff GIT_DIFF_OPTIONS
45
+ * (utils/git_probe_options.ts). Inspection and isolation run git in the work tree the agent writes to, in ZTC's own
46
+ * process and outside any sandbox the agent works under: no core.fsmonitor command in the repository's config runs on a
47
+ * diff or an apply, no external diff or textconv driver on the diffs isolation copies (a textconv's text would not apply
48
+ * either), no signature program, and no bare repository planted in the work tree is taken for the session's repository.
49
+ * Each is honoured from the command line, which the agent cannot write, and leaves normal work trees and linked
46
50
  * worktrees working.
47
51
  */
48
- const GIT_OPTIONS = ['-c', 'core.fsmonitor=false', '-c', 'safe.bareRepository=explicit'];
52
+ const GIT_OPTIONS = GIT_PROBE_OPTIONS;
49
53
  async function git(cwd, args, limit = 1024 * 1024, timeout = GIT_TIMEOUT_MS) {
50
54
  const result = await execFileAsync('git', [...GIT_OPTIONS, '-C', cwd, ...args], {
51
55
  env: gitEnvironment(), encoding: 'buffer', timeout, maxBuffer: limit,
@@ -138,7 +142,7 @@ async function readBounded(path) {
138
142
  export async function observedProcessStart(pid, timeout) {
139
143
  try {
140
144
  const result = await execFileAsync('ps', ['-p', String(pid), '-o', 'lstart='], {
141
- timeout, maxBuffer: 1024, env: { ...process.env, LC_ALL: 'C', TZ: 'UTC' },
145
+ timeout, maxBuffer: 1024, env: { ...buildUserSubprocessEnv(), LC_ALL: 'C', TZ: 'UTC' },
142
146
  });
143
147
  const value = Date.parse(`${result.stdout.trim()} UTC`);
144
148
  return Number.isFinite(value) ? value : null;
@@ -547,10 +551,10 @@ export class LocalWorkspaceCoordinator {
547
551
  function hash(value) { return createHash('sha256').update(value).digest('hex'); }
548
552
  async function captureSource(root) {
549
553
  const [staged, unstaged, stagedPaths, unstagedPaths, untrackedRaw, indexRaw] = await Promise.all([
550
- git(root, ['diff', '--cached', '--binary', '--no-ext-diff', 'HEAD', '--'], PATCH_LIMIT),
551
- git(root, ['diff', '--binary', '--no-ext-diff', '--'], PATCH_LIMIT),
552
- git(root, ['diff', '--cached', '--no-renames', '--name-only', '-z', 'HEAD', '--']),
553
- git(root, ['diff', '--no-renames', '--name-only', '-z', '--']),
554
+ git(root, ['diff', ...GIT_DIFF_OPTIONS, '--cached', '--binary', 'HEAD', '--'], PATCH_LIMIT),
555
+ git(root, ['diff', ...GIT_DIFF_OPTIONS, '--binary', '--'], PATCH_LIMIT),
556
+ git(root, ['diff', ...GIT_DIFF_OPTIONS, '--cached', '--no-renames', '--name-only', '-z', 'HEAD', '--']),
557
+ git(root, ['diff', ...GIT_DIFF_OPTIONS, '--no-renames', '--name-only', '-z', '--']),
554
558
  git(root, ['ls-files', '--others', '--exclude-standard', '-z']),
555
559
  git(root, ['rev-parse', '--path-format=absolute', '--git-path', 'index']),
556
560
  ]);
@@ -0,0 +1,27 @@
1
+ /** The variable a launch carries its launch id in. */
2
+ export declare const LOCAL_ZERG_LAUNCH_ID_ENV = "ZTC_LOCAL_ZERG_LAUNCH_ID";
3
+ export interface LocalZergLaunchRecord {
4
+ /** The launch's process (Poetry), which leads its process group. */
5
+ pid: number;
6
+ launchId: string;
7
+ /** The endpoint the launched Zerg answers on. */
8
+ endpoint: string;
9
+ }
10
+ /** A fresh launch id: 128 random bits, as hex. */
11
+ export declare function newLocalZergLaunchId(): string;
12
+ /** Record the launch started in RUN_DIR, its runtime folder, for a later ZTC's /zerg stop. */
13
+ export declare function recordLocalZergLaunch(runDir: string, record: LocalZergLaunchRecord): void;
14
+ /** Drop the record of the launch started in RUN_DIR, once it is stopped or gone. */
15
+ export declare function forgetLocalZergLaunch(runDir: string): void;
16
+ /**
17
+ * What a later ZTC's stop of the Zerg launched at an endpoint did: 'stopped' it; found its launch 'exited'; left a
18
+ * process running that it could not confirm is the launch its record names ('unconfirmed'); or found no record of a
19
+ * launch there ('not-recorded').
20
+ */
21
+ export type RecordedLaunchStop = 'stopped' | 'exited' | 'unconfirmed' | 'not-recorded';
22
+ /**
23
+ * Stop the Zerg an earlier ZTC launched at ENDPOINT, by its record in RUNS_DIR: SIGTERM its launch's process group
24
+ * (Poetry and everything it started), but only when the process the record names still leads its own group and was
25
+ * started with the record's launch id (see above). The newest record for the endpoint's origin is tried first.
26
+ */
27
+ export declare function stopRecordedLocalZerg(endpoint: string, runsDir: string): Promise<RecordedLaunchStop>;
@@ -0,0 +1,198 @@
1
+ /**
2
+ * The local Zergs a ZTC launched with /zerg launch, recorded where a later ZTC finds them. /restart, the hot restart
3
+ * after an update, and quitting and starting again each start a new ZTC, and a launch, detached, outlives the ZTC that
4
+ * started it; the ZTC that launched it stops it from memory (commands/zerg.ts), a later one from its record here.
5
+ *
6
+ * Each launch carries a random launch id in its environment (LOCAL_ZERG_LAUNCH_ID_ENV), and its record (launch.json in
7
+ * the launch's runtime folder under ~/.ztc/zerg-runs) names its pid, its launch id and the endpoint it answers on.
8
+ * Anything that can write ~/.ztc can change a record to name another process, so a record is never taken on its word:
9
+ * a later ZTC ends only a process that leads its own process group, as a launch does (it is started detached), and
10
+ * that was started with the record's launch id in its environment, which no other program's environment holds. Any
11
+ * program running as this user can read the environment another of this user's processes started with (`ps e` on
12
+ * macOS and the BSDs, /proc/PID/environ on Linux). Where that cannot be read, nothing is ended.
13
+ */
14
+ import { execFile } from 'node:child_process';
15
+ import { randomBytes } from 'node:crypto';
16
+ import { readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
17
+ import { join } from 'node:path';
18
+ import { promisify } from 'node:util';
19
+ import { buildUserSubprocessEnv } from '../utils/subprocess_env.js';
20
+ const execFileAsync = promisify(execFile);
21
+ /** The variable a launch carries its launch id in. */
22
+ export const LOCAL_ZERG_LAUNCH_ID_ENV = 'ZTC_LOCAL_ZERG_LAUNCH_ID';
23
+ const RECORD_FILE = 'launch.json';
24
+ const LAUNCH_ID = /^[0-9a-f]{32}$/;
25
+ const PROBE_TIMEOUT_MS = 2_000;
26
+ /** A fresh launch id: 128 random bits, as hex. */
27
+ export function newLocalZergLaunchId() {
28
+ return randomBytes(16).toString('hex');
29
+ }
30
+ /** Record the launch started in RUN_DIR, its runtime folder, for a later ZTC's /zerg stop. */
31
+ export function recordLocalZergLaunch(runDir, record) {
32
+ writeFileSync(join(runDir, RECORD_FILE), `${JSON.stringify(record)}\n`, { mode: 0o600 });
33
+ }
34
+ /** Drop the record of the launch started in RUN_DIR, once it is stopped or gone. */
35
+ export function forgetLocalZergLaunch(runDir) {
36
+ rmSync(join(runDir, RECORD_FILE), { force: true });
37
+ }
38
+ function originOf(endpoint) {
39
+ try {
40
+ return new URL(endpoint).origin;
41
+ }
42
+ catch {
43
+ return null;
44
+ }
45
+ }
46
+ function portOf(endpoint) {
47
+ try {
48
+ return new URL(endpoint).port || null;
49
+ }
50
+ catch {
51
+ return null;
52
+ }
53
+ }
54
+ function parseRecord(text) {
55
+ try {
56
+ const value = JSON.parse(text);
57
+ if (!Number.isSafeInteger(value.pid) || value.pid <= 1 || typeof value.launchId !== 'string' || !LAUNCH_ID.test(value.launchId)
58
+ || typeof value.endpoint !== 'string')
59
+ return null;
60
+ return { pid: value.pid, launchId: value.launchId, endpoint: value.endpoint };
61
+ }
62
+ catch {
63
+ return null;
64
+ }
65
+ }
66
+ /**
67
+ * The runtime folders in RUNS_DIR that hold a record of a launch at ENDPOINT, newest first (a folder is named
68
+ * local-zerg-<port>-<started ms>-<nonce>), each with its record.
69
+ */
70
+ function recordsFor(endpoint, runsDir) {
71
+ const origin = originOf(endpoint);
72
+ const port = portOf(endpoint);
73
+ if (origin === null || port === null)
74
+ return [];
75
+ let names;
76
+ try {
77
+ names = readdirSync(runsDir);
78
+ }
79
+ catch {
80
+ return [];
81
+ }
82
+ const prefix = `local-zerg-${port}-`;
83
+ const started = (name) => Number(name.slice(prefix.length).split('-')[0]) || 0;
84
+ const found = [];
85
+ for (const name of names.filter(entry => entry.startsWith(prefix)).sort((a, b) => started(b) - started(a))) {
86
+ const runDir = join(runsDir, name);
87
+ let text;
88
+ try {
89
+ text = readFileSync(join(runDir, RECORD_FILE), 'utf8');
90
+ }
91
+ catch {
92
+ continue;
93
+ }
94
+ const record = parseRecord(text);
95
+ if (record && originOf(record.endpoint) === origin)
96
+ found.push({ runDir, record });
97
+ }
98
+ return found;
99
+ }
100
+ /** Whether PID names a process: true, false, or null when that cannot be told. */
101
+ function exists(pid) {
102
+ try {
103
+ process.kill(pid, 0);
104
+ return true;
105
+ }
106
+ catch (error) {
107
+ const code = error.code;
108
+ return code === 'ESRCH' ? false : code === 'EPERM' ? true : null;
109
+ }
110
+ }
111
+ function probeEnv() {
112
+ return { ...buildUserSubprocessEnv(), LC_ALL: 'C' };
113
+ }
114
+ /** The process group PID is in, or null when that cannot be read. */
115
+ async function processGroupOf(pid) {
116
+ if (process.platform === 'win32')
117
+ return null;
118
+ if (process.platform === 'linux') {
119
+ try {
120
+ const stat = readFileSync(`/proc/${pid}/stat`, 'utf8');
121
+ // pid (comm) state ppid pgrp ...: comm may hold spaces and parentheses, so read after its last ')'.
122
+ const group = Number(stat.slice(stat.lastIndexOf(')') + 2).split(' ')[2]);
123
+ return Number.isSafeInteger(group) && group > 0 ? group : null;
124
+ }
125
+ catch {
126
+ return null;
127
+ }
128
+ }
129
+ try {
130
+ const { stdout } = await execFileAsync('ps', ['-o', 'pgid=', '-p', String(pid)], { timeout: PROBE_TIMEOUT_MS, maxBuffer: 1024, env: probeEnv() });
131
+ const group = Number(stdout.trim());
132
+ return Number.isSafeInteger(group) && group > 0 ? group : null;
133
+ }
134
+ catch {
135
+ return null;
136
+ }
137
+ }
138
+ /**
139
+ * Whether PID was started with NAME=VALUE in its environment: true, false, or null when its environment cannot be
140
+ * read. On macOS and the BSDs `ps e` prints the command line and then the environment, so the entry is looked for as
141
+ * a whole word; a launch id is 128 random bits, which nothing else's command line or environment holds by chance.
142
+ */
143
+ async function startedWith(pid, name, value) {
144
+ const entry = `${name}=${value}`;
145
+ if (process.platform === 'win32')
146
+ return null;
147
+ if (process.platform === 'linux') {
148
+ try {
149
+ return readFileSync(`/proc/${pid}/environ`, 'utf8').split('\0').includes(entry);
150
+ }
151
+ catch {
152
+ return null;
153
+ }
154
+ }
155
+ try {
156
+ const { stdout } = await execFileAsync('ps', ['eww', '-o', 'command=', '-p', String(pid)], { timeout: PROBE_TIMEOUT_MS, maxBuffer: 4 * 1024 * 1024, env: probeEnv() });
157
+ return stdout.split(/\s+/).includes(entry);
158
+ }
159
+ catch {
160
+ return null;
161
+ }
162
+ }
163
+ /**
164
+ * Stop the Zerg an earlier ZTC launched at ENDPOINT, by its record in RUNS_DIR: SIGTERM its launch's process group
165
+ * (Poetry and everything it started), but only when the process the record names still leads its own group and was
166
+ * started with the record's launch id (see above). The newest record for the endpoint's origin is tried first.
167
+ */
168
+ export async function stopRecordedLocalZerg(endpoint, runsDir) {
169
+ let outcome = 'not-recorded';
170
+ for (const { runDir, record } of recordsFor(endpoint, runsDir)) {
171
+ const present = exists(record.pid);
172
+ if (present === false) {
173
+ forgetLocalZergLaunch(runDir);
174
+ if (outcome === 'not-recorded')
175
+ outcome = 'exited';
176
+ continue;
177
+ }
178
+ const [group, carriesLaunchId] = present === null
179
+ ? [null, null]
180
+ : await Promise.all([processGroupOf(record.pid), startedWith(record.pid, LOCAL_ZERG_LAUNCH_ID_ENV, record.launchId)]);
181
+ if (group !== record.pid || carriesLaunchId !== true) {
182
+ outcome = 'unconfirmed';
183
+ continue;
184
+ }
185
+ try {
186
+ process.kill(-record.pid, 'SIGTERM');
187
+ }
188
+ catch {
189
+ forgetLocalZergLaunch(runDir);
190
+ if (outcome === 'not-recorded')
191
+ outcome = 'exited';
192
+ continue;
193
+ }
194
+ forgetLocalZergLaunch(runDir);
195
+ return 'stopped';
196
+ }
197
+ return outcome;
198
+ }