@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
@@ -1,14 +1,22 @@
1
+ import { execFileSync } from 'node:child_process';
2
+ import { CREDENTIAL_COPY_MIN_LENGTH, heldCredentialValues, hostEnvironmentValue, hostLaunchEnvironment, ZTC_CREDENTIAL_ENV, } from '../config/host_credentials.js';
1
3
  import { PROVIDER_API_KEY_ENV } from '../config/provider_api_key_env.js';
4
+ import { hostRouterGrantFor, ROUTER_RUNTIME_GRANT_ENV } from '../config/router_runtime_grant.js';
5
+ import { ZTC_EXTENSION_DISCOVERY_ENV } from '../extension_discovery.js';
2
6
  /**
3
7
  * A host's effort and model policy (host_effort_policy.ts): where to read it and the token that reads it. ZTC reads
4
8
  * them as a pair. Both unset, or both empty, is no policy; one without the other fails every turn as an invalid
5
- * configuration. So no child gets one of them without the other.
9
+ * configuration. So no child gets one of them without the other: the host-credential store holds both outside
10
+ * process.env (config/host_credentials.ts), and a ZTC child is given both explicitly or neither.
6
11
  */
7
12
  const HOST_EFFORT_POLICY_ENV = ['ZTC_EFFORT_POLICY_URL', 'ZTC_EFFORT_POLICY_TOKEN'];
8
13
  const USER_SUBPROCESS_STRIPPED_ZTC_ENV = [
9
14
  ...HOST_EFFORT_POLICY_ENV,
10
15
  'ZTC_APPROVAL_TOKEN',
11
16
  'ZERG_ROUTER_BASE_URL',
17
+ // The host's Router runtime grant (config/router_runtime_grant.ts): no child regains it (restoreModelRouteCredentials
18
+ // never restores a name on this list); a ZTC pane or colony cell is given it explicitly (hostRouterGrantForZtcChild).
19
+ ROUTER_RUNTIME_GRANT_ENV,
12
20
  'ZTC_AUTH_TOKEN',
13
21
  'ZTC_BROKER_TOKEN',
14
22
  'ZTC_CHILD_ENTRYPOINT',
@@ -28,8 +36,55 @@ const USER_SUBPROCESS_STRIPPED_ZTC_ENV = [
28
36
  // A pane's launch task came from an agent (panes/initial_input.ts PANE_TASK_AS_MESSAGE_ENV): the pane's own, no one else's.
29
37
  'ZTC_PANE_TASK_AS_MESSAGE',
30
38
  'ZTC_PARENT_PID',
39
+ // The secret a ZergCloud zerg's service verifies operator sessions with (zerg/service/runtime_auth.py). The service
40
+ // reads it from the container, never from a child of ZTC; with it, a child could sign itself an operator session.
41
+ 'ZERG_RUNTIME_SESSION_SECRET',
31
42
  ];
32
43
  const STRIPPED_ZTC_ENV_NAMES = new Set(USER_SUBPROCESS_STRIPPED_ZTC_ENV);
44
+ /**
45
+ * The names above whose values are secrets (ZTC_CREDENTIAL_ENV, config/host_credentials.ts, which holds them outside
46
+ * process.env): what speaks for this ZTC (its mirror's shared secret and process-wide token, its own pane credential,
47
+ * the Router grant it infers with, the host's Router runtime grant, its host's approval, broker and effort-policy
48
+ * tokens) and the zerg service's session secret. The other names are coordinates (a port, a pane's id, the relay's
49
+ * path, the Router's URL), not credentials. A read-only observer of the mirror is never shown their values either
50
+ * (web/observer_secret_redaction.ts reads them through the store).
51
+ */
52
+ export { ZTC_CREDENTIAL_ENV };
53
+ /** The values SOURCE holds under NAMES that are long enough to be credentials, blanks around them aside. */
54
+ function credentialValues(source, names) {
55
+ const values = new Set();
56
+ for (const name of names) {
57
+ const value = source[name]?.trim();
58
+ if (value && value.length >= CREDENTIAL_COPY_MIN_LENGTH)
59
+ values.add(value);
60
+ }
61
+ return values;
62
+ }
63
+ /**
64
+ * The names in ENV that hold one of VALUES: a stripped credential's copies under other names. ZergCloud hands ZTC the
65
+ * same Router grant as ZTC_AUTH_TOKEN and as ZERG_API_TOKEN, so stripping a credential by its name alone would leave
66
+ * it in every child.
67
+ */
68
+ function namesHolding(env, values) {
69
+ if (values.size === 0)
70
+ return [];
71
+ return Object.entries(env).flatMap(([name, value]) => typeof value === 'string' && values.has(value.trim()) ? [name] : []);
72
+ }
73
+ /** The variable a remote resource's broker conventionally takes its credential from: the one of ZTC's that is a broker's. */
74
+ const BROKER_TOKEN_ENV = 'ZTC_BROKER_TOKEN';
75
+ /**
76
+ * What variable NAME holds, when it is reserved for a credential ZTC sends only where it is for, so a remote resource's
77
+ * broker (which is sent its tokenEnv's value) may not take its credential from it: the host's Router runtime grant,
78
+ * another of ZTC's own credentials (ZTC_CREDENTIAL_ENV; ZTC_BROKER_TOKEN is the broker's own), or a model route's
79
+ * credential (MODEL_ROUTE_CREDENTIAL_ENV). Undefined for any other name.
80
+ */
81
+ export function reservedCredentialEnv(name) {
82
+ if (name === ROUTER_RUNTIME_GRANT_ENV)
83
+ return 'host_router_grant';
84
+ if (name !== BROKER_TOKEN_ENV && ZTC_CREDENTIAL_ENV.includes(name))
85
+ return 'ztc_credential';
86
+ return MODEL_ROUTE_CREDENTIAL_ENV.includes(name) ? 'model_route' : undefined;
87
+ }
33
88
  /**
34
89
  * Whether another ZTC launched this one into one of its panes: a --pane child, a colony cell (--mirror --mux), or any
35
90
  * launch through buildChildZtcPaneLaunch or childZtcPaneEnv, which set ZTC_PARENT_PID or ZTC_COLONY_CHILD. A user
@@ -93,13 +148,32 @@ export const MODEL_ROUTE_CREDENTIAL_ENV = Object.freeze([...new Set([
93
148
  * a key from are skipped rather than failing the run.
94
149
  */
95
150
  export function reserveModelRouteCredentials(namedKeyEnv = {}) {
96
- const names = [...new Set([
97
- ...MODEL_ROUTE_CREDENTIAL_ENV,
98
- ...Object.values(namedKeyEnv).filter(name => typeof name === 'string' && ENV_NAME.test(name)),
99
- ])];
151
+ const names = [...new Set([...MODEL_ROUTE_CREDENTIAL_ENV, ...providerKeyEnvNames(namedKeyEnv)])];
100
152
  reserveCredentialNames(names, modelRouteCredentialEnvNames);
101
153
  return names;
102
154
  }
155
+ /** The variables PROVIDER_KEY_ENV (a profile's apiKeyEnv) names as provider keys, skipping what is no variable's name. */
156
+ function providerKeyEnvNames(providerKeyEnv) {
157
+ return Object.values(providerKeyEnv).filter(name => typeof name === 'string' && ENV_NAME.test(name));
158
+ }
159
+ /**
160
+ * Where the variables the person's profile names as provider keys (apiKeyEnv) are read from, as the profile names them
161
+ * now: ZTC's configuration follows its profile here (config.ts), so that a native helper starts without them in every
162
+ * mode (buildNativeHelperEnv), the terminal app's among them, which keeps none of them from its tools. None until then,
163
+ * and none for ZTC used as a library that never loads a profile.
164
+ */
165
+ let profileProviderKeyEnv = () => ({});
166
+ /** Read the variables the person's profile names as provider keys from READ (config.ts gives its ConfigStore's). */
167
+ export function followProfileProviderKeyEnv(read) {
168
+ profileProviderKeyEnv = () => read() ?? {};
169
+ }
170
+ /**
171
+ * Whether this process keeps its route credentials from its tools (reserveModelRouteCredentials): a --headless run or
172
+ * a headless mirror, which a model drives with no human reviewing each command. The interactive app reserves none.
173
+ */
174
+ export function modelRouteCredentialsReserved() {
175
+ return modelRouteCredentialEnvNames.size > 0;
176
+ }
103
177
  /**
104
178
  * Give a ZTC framework child that runs its own inference (a headless `ztc`
105
179
  * pane: sub-agent or sim agent) back the reserved route credentials present
@@ -126,11 +200,12 @@ export function restoreModelRouteCredentials(env, source) {
126
200
  */
127
201
  export function restoreHostEffortPolicy(env, source) {
128
202
  const restored = { ...env };
203
+ const from = hostLaunchEnvironment(source);
129
204
  for (const name of HOST_EFFORT_POLICY_ENV)
130
205
  delete restored[name];
131
- if (HOST_EFFORT_POLICY_ENV.every(name => source[name])) {
206
+ if (HOST_EFFORT_POLICY_ENV.every(name => from[name])) {
132
207
  for (const name of HOST_EFFORT_POLICY_ENV)
133
- restored[name] = source[name];
208
+ restored[name] = from[name];
134
209
  }
135
210
  return restored;
136
211
  }
@@ -147,13 +222,93 @@ export function withoutHostEffortPolicy(env) {
147
222
  /**
148
223
  * Overrides that blank the host policy in a pane a renderer starts, which inherits the renderer's copy of this
149
224
  * process's environment, when this process holds either of the pair; none when it holds neither, so the person's own
150
- * shells carry nothing about a policy they don't have.
225
+ * shells carry nothing about a policy they don't have. The CLI's store keeps both out of the renderer's environment;
226
+ * a process that never took its credentials out of its environment (ZTC used as a library) passes them on to it.
151
227
  */
152
228
  export function hostEffortPolicyBlanks(source = process.env) {
153
- return HOST_EFFORT_POLICY_ENV.some(name => source[name] !== undefined)
229
+ return HOST_EFFORT_POLICY_ENV.some(name => hostEnvironmentValue(name, source) !== undefined)
154
230
  ? Object.fromEntries(HOST_EFFORT_POLICY_ENV.map(name => [name, '']))
155
231
  : {};
156
232
  }
233
+ /**
234
+ * `env` without the host's Router runtime grant (config/router_runtime_grant.ts): for a ZTC child that otherwise inherits
235
+ * its parent's environment, such as a colony cell, which is given it only as hostRouterGrantForZtcChild says.
236
+ */
237
+ export function withoutHostRouterGrant(env) {
238
+ const kept = { ...env };
239
+ delete kept[ROUTER_RUNTIME_GRANT_ENV];
240
+ return kept;
241
+ }
242
+ /**
243
+ * What a ZTC child that runs its own turns (a ZTC pane, a colony cell) is given of the host's Router runtime grant: the
244
+ * grant and the Router it is for (ZERG_ROUTER_BASE_URL), when SOURCE holds one ZTC sends there (hostRouterGrantFor:
245
+ * https, or http on loopback, and a usable bearer); nothing otherwise. A resident whose only route is the grant has
246
+ * nothing else to start them on. Each binds it to that Router as its parent does and strips it from its own children;
247
+ * no child that is not a ZTC is given it. It starts with explicit extension discovery (hostRouterGrantDiscovery).
248
+ */
249
+ export function hostRouterGrantForZtcChild(source = process.env) {
250
+ const router = source.ZERG_ROUTER_BASE_URL;
251
+ const grant = router ? hostRouterGrantFor(router, source) : undefined;
252
+ return router && grant ? { [ROUTER_RUNTIME_GRANT_ENV]: grant, ZERG_ROUTER_BASE_URL: router, ...hostRouterGrantDiscovery(source) } : {};
253
+ }
254
+ /**
255
+ * How a ZTC child given the host's grant (hostRouterGrantForZtcChild) discovers extensions: explicitly
256
+ * (extension_discovery.ts), so it imports no plugin, mode or skill from its working directory, that directory's
257
+ * repository root or HOME, where an agent writes, only what its host names (ZTC_PLUGIN_PATHS and its kin) and ZTC's
258
+ * own. Nothing when SOURCE holds no grant that takes effect: a pane without one discovers them as it always has.
259
+ */
260
+ export function hostRouterGrantDiscovery(source = process.env) {
261
+ const router = source.ZERG_ROUTER_BASE_URL;
262
+ return router && hostRouterGrantFor(router, source) ? { [ZTC_EXTENSION_DISCOVERY_ENV]: 'explicit' } : {};
263
+ }
264
+ /**
265
+ * Whether a ZTC child this process starts (a ZTC pane, a colony cell) discovers extensions explicitly: given the host's
266
+ * grant, it is given explicit discovery (hostRouterGrantDiscovery); and it inherits this process's own.
267
+ */
268
+ export function ztcChildDiscoversExplicitly(source = process.env) {
269
+ return source[ZTC_EXTENSION_DISCOVERY_ENV]?.trim() === 'explicit' || Object.keys(hostRouterGrantDiscovery(source)).length > 0;
270
+ }
271
+ /**
272
+ * The override for a ZTC pane a renderer starts, which inherits the renderer's copy of this process's environment: it
273
+ * keeps a grant ZTC sends to the Router it names, with that Router (hostRouterGrantForZtcChild), and blanks one that
274
+ * takes no effect; nothing when this process holds none, so the person's own panes carry nothing about a grant they
275
+ * don't have.
276
+ */
277
+ export function hostRouterGrantBlank(source = process.env) {
278
+ if (hostEnvironmentValue(ROUTER_RUNTIME_GRANT_ENV, source) === undefined || Object.keys(hostRouterGrantForZtcChild(source)).length > 0)
279
+ return {};
280
+ return { [ROUTER_RUNTIME_GRANT_ENV]: '' };
281
+ }
282
+ /**
283
+ * The credentials no program a renderer starts needs: the zerg service's session secret (no ZTC reads it), and the
284
+ * host's approval token, which answers only a headless run, and no renderer starts one.
285
+ */
286
+ const RENDERER_UNNEEDED_CREDENTIAL_ENV = ['ZERG_RUNTIME_SESSION_SECRET', 'ZTC_APPROVAL_TOKEN'];
287
+ /**
288
+ * The environment of a renderer ZTC starts (zmux, ztc-tui, ZTerm), which starts each pane it opens with its own
289
+ * environment and the pane's: SOURCE without the credentials no pane needs (RENDERER_UNNEEDED_CREDENTIAL_ENV, the
290
+ * remote-resource broker tokens a host configured, which no child regains), or any variable holding one of their
291
+ * values. The person's own environment and the coordinates ZTC hands its panes (the mirror's port, the pane relay) stay.
292
+ *
293
+ * In the CLI, process.env holds none of ZTC's own credentials either: the host-credential store
294
+ * (config/host_credentials.ts) took them out at startup, so no renderer holds the host's Router runtime grant, the
295
+ * platform login in ZTC_AUTH_TOKEN or the host's effort policy for a pane to inherit. A ZTC pane or colony cell is given
296
+ * what it infers and routes with explicitly instead: in its create_pane environment (ui/ratatui/process.ts
297
+ * withZtcChildCredentials), and in a one-time handoff when the owner starts one again in place with replace_pane, which
298
+ * carries no environment (withZtcChildHandoff). zmux's restart_pane starts a process or command pane's program again
299
+ * with the environment its create_pane gave it over the renderer's own, its pane credential blank and no one-time
300
+ * credential file on its command line, so a pane narrowed as a tool child stays narrowed; it refuses a ZTC pane, which
301
+ * is reopened to be given what it routes with again. A process that never took its credentials out of its environment
302
+ * (ZTC used as a library) still passes what a ZTC pane infers and routes with on to its renderer. The mirror's
303
+ * credentials are the caller's to remove (withoutMirrorCredentials).
304
+ */
305
+ export function buildRendererEnv(source = process.env) {
306
+ const names = [...RENDERER_UNNEEDED_CREDENTIAL_ENV, ...brokerCredentialEnvNames];
307
+ const env = { ...source };
308
+ for (const name of [...names, ...namesHolding(source, credentialValues(source, names))])
309
+ delete env[name];
310
+ return env;
311
+ }
157
312
  /** Keep pane-local control fields while removing host RPC credentials. */
158
313
  export function removeUserSubprocessCredentials(source) {
159
314
  const env = { ...source };
@@ -197,12 +352,268 @@ const CREDENTIAL_SAFE_ENV_ALLOWLIST = new Set([
197
352
  'TZ',
198
353
  'USER',
199
354
  ]);
355
+ /**
356
+ * Every variable a user subprocess loses as a credential, by name and by value: ZTC's own (ZTC_CREDENTIAL_ENV) and
357
+ * those the host reserved so far (remote-resource broker tokens, a headless run's route credentials and the keys its
358
+ * profile names). What shows output to someone who is not this ZTC's user, such as a read-only observer of the mirror,
359
+ * withholds their values too, read through the host-credential store (config/host_credentials.ts
360
+ * liveCredentialValues), which holds ZTC's own outside process.env.
361
+ */
362
+ export function userSubprocessCredentialEnv() {
363
+ return [...new Set([...ZTC_CREDENTIAL_ENV, ...hostCredentialEnvNames])];
364
+ }
365
+ /**
366
+ * The values of every credential a user subprocess loses: what SOURCE holds under userSubprocessCredentialEnv's names,
367
+ * and every value the host-credential store holds, wherever SOURCE came from.
368
+ */
369
+ function strippedCredentialValues(source) {
370
+ return new Set([...credentialValues(source, userSubprocessCredentialEnv()), ...heldCredentialValues()]);
371
+ }
372
+ /**
373
+ * The environment of a user subprocess: SOURCE without ZTC's own variables (USER_SUBPROCESS_STRIPPED_ZTC_ENV), the
374
+ * credentials the host reserved, or any other variable that holds one of those credentials' values. Everything else
375
+ * is the person's own and is kept, their own ZERG_API_TOKEN and provider keys among it.
376
+ */
200
377
  export function buildUserSubprocessEnv(source = process.env) {
201
378
  const env = { ...source };
202
379
  for (const key of USER_SUBPROCESS_STRIPPED_ZTC_ENV) {
203
380
  delete env[key];
204
381
  }
205
- return removeUserSubprocessCredentials(env);
382
+ const stripped = removeUserSubprocessCredentials(env);
383
+ for (const name of namesHolding(stripped, strippedCredentialValues(source)))
384
+ delete stripped[name];
385
+ return stripped;
386
+ }
387
+ /**
388
+ * The names among NAMES that no hook is given, whatever its passEnv lists: those ZTC keeps from every user subprocess
389
+ * (USER_SUBPROCESS_STRIPPED_ZTC_ENV: its credentials, the host's Router runtime grant among them, and its own
390
+ * variables), the remote-resource broker tokens this process reserved, and any variable of SOURCE holding one of those
391
+ * credentials' values (ZergCloud passes the Router grant as ZERG_API_TOKEN too). SOURCE is read as ZTC was started with
392
+ * it (hostLaunchEnvironment): the host-credential store took such a copy out of process.env with the credential, and the
393
+ * hook's result still names it. The person's own names (a GITHUB_TOKEN, a provider key) are none of these.
394
+ */
395
+ export function refusedPassEnvNames(names, source = process.env) {
396
+ const launch = hostLaunchEnvironment(source);
397
+ const values = credentialValues(launch, [...ZTC_CREDENTIAL_ENV, ...brokerCredentialEnvNames]);
398
+ return [...new Set(names)].filter(name => STRIPPED_ZTC_ENV_NAMES.has(name) || brokerCredentialEnvNames.has(name)
399
+ || (typeof launch[name] === 'string' && values.has(launch[name].trim())));
400
+ }
401
+ /**
402
+ * The variables that tell git which repository, work tree, index or object store to use in place of the one it finds
403
+ * from its working directory. git sets GIT_INDEX_FILE for the hooks it runs (and GIT_DIR too in a linked worktree,
404
+ * naming the main repository's .git/worktrees/<name>), and GIT_PREFIX for an alias, so a ZTC started from either holds
405
+ * them. A git ZTC runs in a directory that has a repository of its own acts on that one, so it takes none of these from
406
+ * ZTC's environment (gitEnvFor): with GIT_DIR naming another repository, /batch registered its worktree and branch
407
+ * there.
408
+ */
409
+ export const REPOSITORY_GIT_ENV = Object.freeze(['GIT_DIR', 'GIT_WORK_TREE', 'GIT_INDEX_FILE',
410
+ 'GIT_OBJECT_DIRECTORY', 'GIT_ALTERNATE_OBJECT_DIRECTORIES', 'GIT_COMMON_DIR', 'GIT_PREFIX']);
411
+ /** ENV without REPOSITORY_GIT_ENV, for a git that ZTC runs in a repository it names. */
412
+ export function withoutRepositoryGitEnv(env) {
413
+ const copy = { ...env };
414
+ for (const name of REPOSITORY_GIT_ENV)
415
+ delete copy[name];
416
+ return copy;
417
+ }
418
+ /**
419
+ * The environment of a git that ZTC runs in a repository it names, which has one of its own: a user subprocess's
420
+ * (buildUserSubprocessEnv), since git runs the repository's hooks and the programs its config names with it, without
421
+ * REPOSITORY_GIT_ENV. A directory that may have none (the person's working directory) takes buildGitSubprocessEnvFor.
422
+ */
423
+ export function buildGitSubprocessEnv(source = process.env) {
424
+ return withoutRepositoryGitEnv(buildUserSubprocessEnv(source));
425
+ }
426
+ /**
427
+ * Whether git finds a repository from DIRECTORY with ENV, which holds none of REPOSITORY_GIT_ENV: a .git there or in a
428
+ * folder above it, as far as GIT_CEILING_DIRECTORIES lets git look. safe.bareRepository=explicit, as ZTC's probes and
429
+ * /batch run git, so a bare repository planted in the work tree is not taken for its repository.
430
+ */
431
+ function findsRepository(directory, env) {
432
+ try {
433
+ execFileSync('git', ['-C', directory, '-c', 'safe.bareRepository=explicit', 'rev-parse', '--git-dir'], { env, stdio: 'ignore', timeout: 5000, windowsHide: true });
434
+ return true;
435
+ }
436
+ catch {
437
+ return false;
438
+ }
439
+ }
440
+ /**
441
+ * ENV for a git that ZTC runs in DIRECTORY. When git finds a repository from DIRECTORY by itself (a project, a /batch
442
+ * worktree, a clone), that is the one it acts on, so ENV loses REPOSITORY_GIT_ENV: a hook's GIT_DIR or another
443
+ * repository's cannot redirect it. When it finds none, ENV keeps them, since they are then the only repository
444
+ * DIRECTORY has: `vcsh enter` and `yadm enter` export GIT_DIR naming a bare repository of the person's dotfiles and
445
+ * GIT_WORK_TREE naming its work tree, which holds no .git, and the person's own git works there. Telling which takes
446
+ * a git run, made only when ENV holds one of the variables.
447
+ */
448
+ export function gitEnvFor(directory, env) {
449
+ if (!REPOSITORY_GIT_ENV.some(name => env[name] !== undefined))
450
+ return { ...env };
451
+ const without = withoutRepositoryGitEnv(env);
452
+ return findsRepository(directory, without) ? without : { ...env };
453
+ }
454
+ /**
455
+ * The environment of a git that ZTC runs in DIRECTORY, which need not have a repository of its own (the person's
456
+ * working directory): a user subprocess's (buildUserSubprocessEnv), with REPOSITORY_GIT_ENV as gitEnvFor leaves it.
457
+ */
458
+ export function buildGitSubprocessEnvFor(directory, source = process.env) {
459
+ return gitEnvFor(directory, buildUserSubprocessEnv(source));
460
+ }
461
+ /**
462
+ * The environment of a child that is a model client by design and someone else's program, never a ZTC, that the person
463
+ * starts: the Shannon runner, the Zerg service /zerg launch starts. (The adaptation pipeline, a tool the agent starts,
464
+ * gets a user subprocess's environment and only the key it injects.) It is a user subprocess's environment
465
+ * (buildUserSubprocessEnv), with the provider keys a headless run reserved from its tools given back, since the child
466
+ * infers with them. ZTC's own credentials (ZTC_AUTH_TOKEN among them), broker tokens, and any variable holding one of
467
+ * their values, it never gets.
468
+ */
469
+ export function buildModelClientSubprocessEnv(source = process.env) {
470
+ const env = buildUserSubprocessEnv(source);
471
+ const launch = hostLaunchEnvironment(source);
472
+ const ztcCredentials = new Set([...credentialValues(launch, [...ZTC_CREDENTIAL_ENV, ...brokerCredentialEnvNames]), ...heldCredentialValues()]);
473
+ for (const name of modelRouteCredentialEnvNames) {
474
+ if (brokerCredentialEnvNames.has(name) || STRIPPED_ZTC_ENV_NAMES.has(name))
475
+ continue;
476
+ const value = launch[name];
477
+ if (value !== undefined && !ztcCredentials.has(value.trim()))
478
+ env[name] = value;
479
+ }
480
+ return env;
481
+ }
482
+ /**
483
+ * The environment of a native helper ZTC starts from its own install (utils/native_install.ts: ztc-audio, ztc-computer,
484
+ * ztc-ml, ztc-tts, the zlicky overlay): a user subprocess's (buildUserSubprocessEnv: none of ZTC's own variables or
485
+ * credentials, under any name), without the credentials ZTC routes models with either (MODEL_ROUTE_CREDENTIAL_ENV, those
486
+ * this process reserved, the variables PROVIDER_KEY_ENV names as provider keys, by default those the person's profile
487
+ * names now (followProfileProviderKeyEnv), and any other variable holding one of their values), whether or not this
488
+ * process reserved them from its tools: the terminal app never does. None of the helpers reads one: they capture the
489
+ * screen and audio, inject input, transcribe and speak locally (ztc-tts reads ZTC_TTS_CPU, and HF_HOME where it
490
+ * downloads a model to), and the programs they start get what they got.
491
+ */
492
+ export function buildNativeHelperEnv(providerKeyEnv = profileProviderKeyEnv(), source = process.env) {
493
+ const env = buildUserSubprocessEnv(source);
494
+ const route = [...new Set([...MODEL_ROUTE_CREDENTIAL_ENV, ...modelRouteCredentialEnvNames, ...providerKeyEnvNames(providerKeyEnv)])];
495
+ const launch = hostLaunchEnvironment(source);
496
+ for (const name of [...route, ...namesHolding(launch, credentialValues(launch, route))])
497
+ delete env[name];
498
+ return env;
499
+ }
500
+ /**
501
+ * The environment of a stdio MCP server, someone else's program, which runs as long as ZTC does. It gets what a tool
502
+ * child of a headless run gets: a user subprocess's environment (buildUserSubprocessEnv: none of ZTC's own variables or
503
+ * credentials, nor the broker tokens a host configured, under any name), without the credentials ZTC routes models with
504
+ * either (MODEL_ROUTE_CREDENTIAL_ENV, those this process reserved, the variables a profile names as provider keys,
505
+ * PROVIDER_KEY_ENV, and any other variable holding one of their values), whether or not this process has reserved them
506
+ * yet (reserveModelRouteCredentials): the terminal app never does, and a headless run does once its agent is built,
507
+ * after its MCP servers start. So a server ZTC starts is the same in the app, a headless run and a resident. What the
508
+ * entry's passEnv names it is given from ZTC's environment as well, as a hook's passEnv is, unless no MCP server may be
509
+ * given it (refusedPassEnvNames: ZTC's own credentials and variables, a broker token, a copy of one of ZTC's
510
+ * credentials); a provider key it may name. The entry's own env goes over all of it.
511
+ */
512
+ export function buildMcpServerEnv(entry, providerKeyEnv = {}, source = process.env) {
513
+ const asked = [...new Set(entry.passEnv ?? [])];
514
+ const refused = refusedPassEnvNames(asked, source);
515
+ const passed = new Set(asked.filter(name => !refused.includes(name)));
516
+ const route = [...new Set([...MODEL_ROUTE_CREDENTIAL_ENV, ...modelRouteCredentialEnvNames, ...providerKeyEnvNames(providerKeyEnv)])]
517
+ .filter(name => !passed.has(name));
518
+ const launch = hostLaunchEnvironment(source);
519
+ const kept = buildUserSubprocessEnv(source);
520
+ const copies = namesHolding(launch, credentialValues(launch, route)).filter(name => !passed.has(name) && !route.includes(name));
521
+ for (const name of [...route, ...copies])
522
+ delete kept[name];
523
+ for (const name of passed) {
524
+ const value = source[name];
525
+ if (value !== undefined)
526
+ kept[name] = value;
527
+ }
528
+ const env = {};
529
+ for (const [name, value] of Object.entries(kept))
530
+ if (typeof value === 'string')
531
+ env[name] = value;
532
+ const own = entry.env ?? {};
533
+ Object.assign(env, own);
534
+ // Only what a passEnv could give: ZTC's own credentials (ZTC_AUTH_TOKEN, or a copy of one) no entry may name.
535
+ const withheld = [...route.filter(name => launch[name]?.trim()), ...copies]
536
+ .filter(name => !Object.hasOwn(own, name) && refusedPassEnvNames([name], source).length === 0);
537
+ return { env, withheld, refused };
538
+ }
539
+ /**
540
+ * What a ZTC that ZTC starts as a framework child (a colony cell) needs of ZTC's own credentials, by design: the Router
541
+ * grant it infers with, and its host's RPC transport (ZTC_BROKER_TOKEN and the broker tokens a host configured), which
542
+ * a trusted framework host retains. Its mirror credential is its own (pane_credentials.ts), the host's approval token
543
+ * answers only a headless run, and no ZTC reads the zerg service's session secret.
544
+ */
545
+ const ZTC_CHILD_CREDENTIAL_ENV = ['ZTC_AUTH_TOKEN', 'ZTC_BROKER_TOKEN'];
546
+ /**
547
+ * ENV without ZTC's own credentials (ZTC_CREDENTIAL_ENV), the remote-resource broker tokens this process reserved, or
548
+ * any other variable holding one of their values, for a child given the rest of ZTC's environment. ZTC's coordinates
549
+ * (the mirror's port, a pane's id, the relay) are not credentials and stay. For a child that is ZTC itself (`ztcChild`:
550
+ * a colony cell), the credentials it needs by design stay too (ZTC_CHILD_CREDENTIAL_ENV, the configured broker tokens).
551
+ * A stdio MCP server, someone else's program, gets what a tool child gets instead (buildMcpServerEnv).
552
+ */
553
+ export function withoutZtcCredentials(env, options = {}) {
554
+ // This process's own environment is read as ZTC was started with it: the store holds its credentials, which a ZTC
555
+ // child is given from there.
556
+ const source = hostLaunchEnvironment(env);
557
+ const keep = new Set(options.ztcChild ? [...ZTC_CHILD_CREDENTIAL_ENV, ...brokerCredentialEnvNames] : []);
558
+ const names = [...ZTC_CREDENTIAL_ENV, ...brokerCredentialEnvNames].filter(name => !keep.has(name));
559
+ const kept = { ...source };
560
+ for (const name of [...names, ...namesHolding(source, credentialValues(source, names))])
561
+ delete kept[name];
562
+ for (const name of keep)
563
+ if (source[name] !== undefined)
564
+ kept[name] = source[name];
565
+ return kept;
566
+ }
567
+ /**
568
+ * What a ZTC child of PANE_TYPE that a renderer starts (zmux, ZTerm) is given of ZTC's credentials, in its create_pane
569
+ * environment. A renderer starts each pane with its own environment plus the pane's, and its own holds none of them:
570
+ * the host-credential store took them out of this process's environment before the renderer was spawned. So a ZTC
571
+ * pane is given the host's Router runtime grant with the Router it names (hostRouterGrantForZtcChild), the host's
572
+ * effort policy, as a pair, and the platform login in ZTC_AUTH_TOKEN, which it inherited from the renderer before and
573
+ * signs in on (the headless pane manager has never given a ZTC pane that one); a colony cell those and what a cell
574
+ * keeps by design (ZTC_CHILD_CREDENTIAL_ENV), as the headless pane manager gives them (computePaneSpawnEnv). Any other
575
+ * pane gets nothing.
576
+ */
577
+ export function ztcChildCredentialEnv(paneType, source = process.env) {
578
+ if (paneType !== 'ztc' && paneType !== 'colony_ztc')
579
+ return {};
580
+ const launch = hostLaunchEnvironment(source);
581
+ const env = {};
582
+ if (HOST_EFFORT_POLICY_ENV.every(name => launch[name])) {
583
+ for (const name of HOST_EFFORT_POLICY_ENV)
584
+ env[name] = launch[name];
585
+ }
586
+ for (const name of paneType === 'colony_ztc' ? ZTC_CHILD_CREDENTIAL_ENV : ['ZTC_AUTH_TOKEN']) {
587
+ const value = launch[name];
588
+ if (value !== undefined)
589
+ env[name] = value;
590
+ }
591
+ return { ...env, ...hostRouterGrantForZtcChild(source) };
592
+ }
593
+ /**
594
+ * The desktop app's runtime lease marker, which ZTerm's CLI bridge (--ztc-cli) exports to the bundled ZTC it starts
595
+ * (desktop/runtime.ts verifyRuntimeLease): it names a descriptor that one process inherited, and means nothing in any
596
+ * other. A ZTC the bridge starts again is given a marker of its own.
597
+ */
598
+ const DESKTOP_RUNTIME_LEASE_ENV = 'ZTC_DESKTOP_RUNTIME_LEASE';
599
+ /**
600
+ * What ZTC's own relaunch through someone else's program is to be started with beyond what that program passes on: a
601
+ * terminal's new window (--new-window) runs the relaunched ZTC with the terminal's environment, and ZTC gives the
602
+ * terminal a user subprocess's (buildUserSubprocessEnv). That leaves out ZTC's own variables (the mirror's sign-in and
603
+ * port, the Router it routes on, ...), the credentials the host-credential store holds with every copy of one, and the
604
+ * credentials a host reserved: this is each of those, as the environment ZTC was started with holds it
605
+ * (hostLaunchEnvironment). The relaunched ZTC is given them in a one-time handoff its command line names
606
+ * (config/host_credentials.ts HOST_HANDOFF_FLAG), so it starts as this one was started, and the terminal holds none.
607
+ * Taking a handoff sets its variables over the ones that ZTC was started with, so it holds none that the relaunch sets
608
+ * afresh: the desktop app's runtime lease marker (DESKTOP_RUNTIME_LEASE_ENV), and LAUNCH_SET, the variables the
609
+ * relaunch's command line sets (an owned relaunch's bundle state routing).
610
+ */
611
+ export function ztcRelaunchWithheldEnv(source = process.env, launchSet = []) {
612
+ const user = buildUserSubprocessEnv(source);
613
+ const fresh = new Set([DESKTOP_RUNTIME_LEASE_ENV, ...launchSet]);
614
+ return Object.fromEntries(Object.entries(hostLaunchEnvironment(source))
615
+ .filter((entry) => entry[1] !== undefined && user[entry[0]] !== entry[1]
616
+ && !fresh.has(entry[0])));
206
617
  }
207
618
  export function buildTemporaryWriteRootSubprocessEnv(temporaryWriteRoot, source = process.env) {
208
619
  const inherited = buildUserSubprocessEnv(source);
@@ -237,13 +648,20 @@ export function buildCredentialSafeSubprocessEnv(source = process.env) {
237
648
  }
238
649
  return env;
239
650
  }
240
- export function buildUserSubprocessEnvOverrides() {
651
+ /**
652
+ * Overrides that blank, in a pane the renderer starts with its copy of SOURCE (this process's environment), what a
653
+ * user subprocess never gets (buildUserSubprocessEnv): ZTC's own variables, the reserved credentials, and every other
654
+ * variable holding one of those credentials' values.
655
+ */
656
+ export function buildUserSubprocessEnvOverrides(source = process.env) {
241
657
  const env = {};
242
658
  for (const key of USER_SUBPROCESS_STRIPPED_ZTC_ENV) {
243
659
  env[key] = '';
244
660
  }
245
661
  for (const key of hostCredentialEnvNames)
246
662
  env[key] = '';
663
+ for (const name of namesHolding(source, strippedCredentialValues(source)))
664
+ env[name] = '';
247
665
  return env;
248
666
  }
249
667
  function isTemporaryWriteRootEnvAllowed(key) {
@@ -0,0 +1,5 @@
1
+ /**
2
+ * What the system's ps prints for ARGS, or null when it cannot be read: on a machine with none of SYSTEM_PS (which
3
+ * lists nothing, as one without ps did, so nothing is proven by a listing), or when ps fails or runs past TIMEOUT_MS.
4
+ */
5
+ export declare function systemPsOutput(args: readonly string[], timeoutMs: number): string | null;
@@ -0,0 +1,30 @@
1
+ import { execFileSync } from 'node:child_process';
2
+ import { existsSync } from 'node:fs';
3
+ /**
4
+ * The ps ZTC reads the process listings it acts on with: the groups a command left running (utils/shell.ts) and the
5
+ * state of a quiet peer in the instance registry (instance_registry.ts). It is the system's own, never one found on
6
+ * PATH, where anything that can write a folder on PATH (a command the agent runs among them) could put a program of its
7
+ * own under that name, and it is never given ZTC's environment, which holds the host's Router runtime grant and the
8
+ * mirror's credentials: a ps on PATH was started with all of them. NixOS and Guix keep theirs in the system profile,
9
+ * which only root writes too.
10
+ */
11
+ const SYSTEM_PS = ['/bin/ps', '/usr/bin/ps', '/run/current-system/sw/bin/ps', '/run/current-system/profile/bin/ps'];
12
+ /** All of an environment that ps is given: the system's folders and the locale its listing is read in, nothing of ZTC's. */
13
+ const SYSTEM_PS_ENV = { PATH: '/usr/bin:/bin', LANG: 'C' };
14
+ /**
15
+ * What the system's ps prints for ARGS, or null when it cannot be read: on a machine with none of SYSTEM_PS (which
16
+ * lists nothing, as one without ps did, so nothing is proven by a listing), or when ps fails or runs past TIMEOUT_MS.
17
+ */
18
+ export function systemPsOutput(args, timeoutMs) {
19
+ const ps = SYSTEM_PS.find(path => existsSync(path));
20
+ if (!ps)
21
+ return null;
22
+ try {
23
+ return execFileSync(ps, args, {
24
+ encoding: 'utf8', timeout: timeoutMs, stdio: ['ignore', 'pipe', 'ignore'], env: { ...SYSTEM_PS_ENV },
25
+ });
26
+ }
27
+ catch {
28
+ return null;
29
+ }
30
+ }
package/dist/utils/tts.js CHANGED
@@ -6,6 +6,7 @@
6
6
  * - elevenlabs: ElevenLabs TTS API (high quality voices)
7
7
  */
8
8
  import { spawn } from 'child_process';
9
+ import { buildUserSubprocessEnv } from './subprocess_env.js';
9
10
  import { writeFile, unlink } from 'fs/promises';
10
11
  import { tmpdir } from 'os';
11
12
  import { join } from 'path';
@@ -57,6 +58,7 @@ async function speakWithSay(text, voice, rate) {
57
58
  args.push('-f', '-');
58
59
  const proc = spawn('say', args, {
59
60
  stdio: ['pipe', 'pipe', 'pipe'],
61
+ env: buildUserSubprocessEnv(),
60
62
  });
61
63
  // Write text to stdin
62
64
  proc.stdin.write(text);
@@ -175,6 +177,7 @@ async function playAudioFile(filePath) {
175
177
  }
176
178
  const proc = spawn(command, args, {
177
179
  stdio: ['pipe', 'pipe', 'pipe'],
180
+ env: buildUserSubprocessEnv(),
178
181
  });
179
182
  proc.on('close', (code) => {
180
183
  if (code === 0) {
@@ -189,6 +192,7 @@ async function playAudioFile(filePath) {
189
192
  if (process.platform === 'linux' && command === 'aplay') {
190
193
  const paplay = spawn('paplay', [filePath], {
191
194
  stdio: ['pipe', 'pipe', 'pipe'],
195
+ env: buildUserSubprocessEnv(),
192
196
  });
193
197
  paplay.on('close', (code2) => {
194
198
  if (code2 === 0) {
@@ -288,6 +292,7 @@ export async function listMacOSVoices() {
288
292
  return new Promise((resolve) => {
289
293
  const proc = spawn('say', ['-v', '?'], {
290
294
  stdio: ['pipe', 'pipe', 'pipe'],
295
+ env: buildUserSubprocessEnv(),
291
296
  });
292
297
  let output = '';
293
298
  proc.stdout.on('data', (data) => {