@zergai/ztc 0.26.6-dev.202611240947 → 0.27.0-beta.1

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 (268) hide show
  1. package/README.md +629 -167
  2. package/RUST_THIRD_PARTY_LICENSES.txt +5 -5
  3. package/bin/zmux-darwin-arm64 +0 -0
  4. package/bin/zmux-linux-x64 +0 -0
  5. package/bin/ztc-audio-darwin-arm64 +0 -0
  6. package/bin/ztc-capture-darwin-arm64 +0 -0
  7. package/bin/ztc-computer-darwin-arm64 +0 -0
  8. package/bin/ztc-llm-darwin-arm64 +0 -0
  9. package/bin/ztc-tts-darwin-arm64 +0 -0
  10. package/bin/ztc-tui-darwin-arm64 +0 -0
  11. package/bin/ztc-tui-linux-x64 +0 -0
  12. package/bin/zterm-darwin-arm64 +2 -2
  13. package/dist/App.js +6 -1
  14. package/dist/agent/agent.d.ts +21 -15
  15. package/dist/agent/agent.js +389 -163
  16. package/dist/agent/backends/codex_app_server.d.ts +7 -1
  17. package/dist/agent/backends/codex_app_server.js +11 -1
  18. package/dist/agent/backends/gemini.js +22 -8
  19. package/dist/agent/commands/batch.js +8 -0
  20. package/dist/agent/commands/config.js +10 -0
  21. package/dist/agent/commands/environment.js +5 -5
  22. package/dist/agent/commands/goal.d.ts +1 -0
  23. package/dist/agent/commands/goal.js +2 -1
  24. package/dist/agent/commands/index.js +2 -1
  25. package/dist/agent/commands/manifest.js +4 -0
  26. package/dist/agent/commands/optimize.js +5 -2
  27. package/dist/agent/commands/pane_cli.d.ts +29 -0
  28. package/dist/agent/commands/pane_cli.js +303 -0
  29. package/dist/agent/commands/settings.js +13 -3
  30. package/dist/agent/commands/shannon.js +8 -0
  31. package/dist/agent/commands/update.js +15 -3
  32. package/dist/agent/commands/zerg.js +31 -1
  33. package/dist/agent/commands/ztc_pane.js +3 -1
  34. package/dist/agent/factory.d.ts +4 -0
  35. package/dist/agent/factory.js +11 -1
  36. package/dist/agent/loop_policy.d.ts +42 -5
  37. package/dist/agent/loop_policy.js +206 -3
  38. package/dist/agent/runtime/command_jobs.d.ts +5 -0
  39. package/dist/agent/runtime/command_jobs.js +241 -31
  40. package/dist/agent/runtime/command_wait.d.ts +2 -0
  41. package/dist/agent/runtime/command_wait.js +1 -0
  42. package/dist/agent/runtime/done_report.d.ts +18 -0
  43. package/dist/agent/runtime/done_report.js +130 -0
  44. package/dist/agent/runtime/execution_context.js +4 -1
  45. package/dist/agent/runtime/failure_cause.d.ts +2 -0
  46. package/dist/agent/runtime/failure_cause.js +35 -0
  47. package/dist/agent/runtime/policy.d.ts +14 -2
  48. package/dist/agent/runtime/policy.js +104 -12
  49. package/dist/agent/runtime/quiet_job.d.ts +18 -0
  50. package/dist/agent/runtime/quiet_job.js +48 -0
  51. package/dist/agent/runtime/recovery.d.ts +1 -5
  52. package/dist/agent/runtime/recovery.js +2 -44
  53. package/dist/agent/runtime/tool_runtime.d.ts +1 -0
  54. package/dist/agent/runtime/tool_runtime.js +2 -0
  55. package/dist/agent/subagent.js +6 -1
  56. package/dist/agent/tools/deferred.js +1 -0
  57. package/dist/agent/tools/index.d.ts +2 -0
  58. package/dist/agent/tools/index.js +1 -0
  59. package/dist/agent/tools/outcome.d.ts +15 -0
  60. package/dist/agent/tools/outcome.js +107 -0
  61. package/dist/agent/tools/pane.d.ts +2 -1
  62. package/dist/agent/tools/pane.js +21 -3
  63. package/dist/agent/tools/registry.js +2 -0
  64. package/dist/agent/tools/shell.d.ts +1 -0
  65. package/dist/agent/tools/shell.js +128 -17
  66. package/dist/agent/tools/sim_domain_tools.js +4 -2
  67. package/dist/agent/tools/supervision.js +4 -2
  68. package/dist/agent/tools/types.d.ts +3 -0
  69. package/dist/app/command_context.js +5 -9
  70. package/dist/app/conversation_compaction.d.ts +4 -2
  71. package/dist/app/conversation_compaction.js +16 -5
  72. package/dist/app/headless_shell_mode.d.ts +14 -0
  73. package/dist/app/headless_shell_mode.js +7 -0
  74. package/dist/app/hosted_resident.d.ts +356 -0
  75. package/dist/app/hosted_resident.js +677 -0
  76. package/dist/app/restart.js +11 -2
  77. package/dist/app/session_manager.d.ts +5 -0
  78. package/dist/app/session_manager.js +21 -6
  79. package/dist/app/session_messages.js +6 -6
  80. package/dist/app/workspace_layout_controller.js +7 -0
  81. package/dist/batch/worktree.d.ts +10 -0
  82. package/dist/batch/worktree.js +15 -0
  83. package/dist/bench/registry.js +4 -2
  84. package/dist/cli_args.js +1 -1
  85. package/dist/cli_main.js +80 -10
  86. package/dist/cli_ratatui.d.ts +75 -0
  87. package/dist/cli_ratatui.js +485 -84
  88. package/dist/components/InputArea.js +4 -4
  89. package/dist/config/execution.d.ts +10 -0
  90. package/dist/config/execution.js +10 -0
  91. package/dist/config/hosted_runtime.d.ts +4 -1
  92. package/dist/config/hosted_runtime.js +4 -1
  93. package/dist/config/permission_pin.d.ts +34 -2
  94. package/dist/config/permission_pin.js +52 -2
  95. package/dist/config/person_command_policy.d.ts +38 -0
  96. package/dist/config/person_command_policy.js +11 -0
  97. package/dist/config/profiles.d.ts +21 -0
  98. package/dist/config/profiles.js +56 -2
  99. package/dist/config/types.d.ts +4 -1
  100. package/dist/config.js +34 -19
  101. package/dist/desktop/daily/codex_exec_adapter.js +7 -2
  102. package/dist/desktop/daily/desktop_wiring.d.ts +7 -1
  103. package/dist/desktop/daily/desktop_wiring.js +11 -3
  104. package/dist/desktop/daily/recipes.js +20 -4
  105. package/dist/desktop/health.d.ts +24 -0
  106. package/dist/desktop/health.js +151 -0
  107. package/dist/desktop/native_dialogs.d.ts +37 -0
  108. package/dist/desktop/native_dialogs.js +105 -0
  109. package/dist/diagnostics/session_export.d.ts +1 -0
  110. package/dist/diagnostics/session_export.js +28 -0
  111. package/dist/embedded/registry.js +4 -2
  112. package/dist/emulation/tool_adapters.js +9 -4
  113. package/dist/emulation/ztc_addendum.js +3 -0
  114. package/dist/headless.d.ts +11 -4
  115. package/dist/headless.js +99 -34
  116. package/dist/headless_approval.d.ts +6 -0
  117. package/dist/headless_cli.js +1 -1
  118. package/dist/hooks/runner.js +7 -6
  119. package/dist/library.d.ts +4 -0
  120. package/dist/library.js +3 -0
  121. package/dist/mcp/client.js +3 -3
  122. package/dist/modes/registry.js +13 -4
  123. package/dist/onboarding/setup_environment.js +13 -1
  124. package/dist/optimize/adapters/kernelbench.js +88 -10
  125. package/dist/optimize/adapters/types.d.ts +5 -0
  126. package/dist/optimize/executor.js +21 -0
  127. package/dist/panes/cli_install.d.ts +54 -0
  128. package/dist/panes/cli_install.js +157 -0
  129. package/dist/panes/model_catalog.js +3 -0
  130. package/dist/panes/new_pane_choices.d.ts +6 -2
  131. package/dist/panes/new_pane_choices.js +24 -12
  132. package/dist/panes/pane_cli_launch.d.ts +50 -0
  133. package/dist/panes/pane_cli_launch.js +124 -0
  134. package/dist/panes/pane_clis.d.ts +171 -0
  135. package/dist/panes/pane_clis.js +325 -0
  136. package/dist/panes/person_only_panes.d.ts +45 -0
  137. package/dist/panes/person_only_panes.js +117 -0
  138. package/dist/plugin/simulation_living.js +0 -2
  139. package/dist/plugin/simulation_living_engine.d.ts +1 -0
  140. package/dist/plugin/simulation_living_engine.js +1 -0
  141. package/dist/plugin/simulation_living_labels.d.ts +3 -0
  142. package/dist/plugin/simulation_living_labels.js +2 -0
  143. package/dist/router_auth.js +12 -1
  144. package/dist/session.js +26 -1
  145. package/dist/simulation/art/gemini_image.js +11 -5
  146. package/dist/simulation/domains/loader.js +25 -3
  147. package/dist/simulation/living/cold.d.ts +23 -0
  148. package/dist/simulation/living/cold.js +41 -0
  149. package/dist/simulation/living/context.d.ts +591 -1283
  150. package/dist/simulation/living/context.js +378 -244
  151. package/dist/simulation/living/contradictions.d.ts +14 -0
  152. package/dist/simulation/living/contradictions.js +282 -0
  153. package/dist/simulation/living/core/ids.d.ts +53 -0
  154. package/dist/simulation/living/core/ids.js +39 -0
  155. package/dist/simulation/living/core/kind.d.ts +20 -0
  156. package/dist/simulation/living/core/kind.js +15 -0
  157. package/dist/simulation/living/core/laws.d.ts +179 -0
  158. package/dist/simulation/living/core/laws.js +184 -0
  159. package/dist/simulation/living/core/ops.d.ts +257 -0
  160. package/dist/simulation/living/core/ops.js +34 -0
  161. package/dist/simulation/living/definer.d.ts +17 -1
  162. package/dist/simulation/living/definer.js +130 -51
  163. package/dist/simulation/living/definition.d.ts +174 -65
  164. package/dist/simulation/living/definition.js +287 -180
  165. package/dist/simulation/living/director.d.ts +80 -0
  166. package/dist/simulation/living/director.js +363 -0
  167. package/dist/simulation/living/frame.d.ts +108 -21
  168. package/dist/simulation/living/frame.js +111 -66
  169. package/dist/simulation/living/identifiers.d.ts +0 -1
  170. package/dist/simulation/living/identifiers.js +0 -3
  171. package/dist/simulation/living/index.d.ts +18 -9
  172. package/dist/simulation/living/index.js +18 -9
  173. package/dist/simulation/living/inference.d.ts +92 -50
  174. package/dist/simulation/living/inference.js +319 -175
  175. package/dist/simulation/living/knowledge.d.ts +28 -0
  176. package/dist/simulation/living/knowledge.js +73 -0
  177. package/dist/simulation/living/library.json +1177 -0
  178. package/dist/simulation/living/mechanics.d.ts +31 -34
  179. package/dist/simulation/living/mechanics.js +222 -138
  180. package/dist/simulation/living/model-input.d.ts +6 -3
  181. package/dist/simulation/living/model-input.js +18 -26
  182. package/dist/simulation/living/open-world.d.ts +74 -94
  183. package/dist/simulation/living/open-world.js +151 -30
  184. package/dist/simulation/living/phrases.d.ts +3 -0
  185. package/dist/simulation/living/phrases.js +37 -0
  186. package/dist/simulation/living/phrases.json +560 -0
  187. package/dist/simulation/living/projection.d.ts +316 -2
  188. package/dist/simulation/living/projection.js +50 -2
  189. package/dist/simulation/living/prompts/example-spec.json +11 -0
  190. package/dist/simulation/living/prompts/role-text.json +14 -0
  191. package/dist/simulation/living/prompts.d.ts +5 -0
  192. package/dist/simulation/living/prompts.js +275 -0
  193. package/dist/simulation/living/recall.d.ts +2 -2
  194. package/dist/simulation/living/reduce.d.ts +51 -14
  195. package/dist/simulation/living/reduce.js +941 -579
  196. package/dist/simulation/living/replay.d.ts +5 -4
  197. package/dist/simulation/living/replay.js +10 -9
  198. package/dist/simulation/living/runtime.d.ts +135 -25
  199. package/dist/simulation/living/runtime.js +1641 -583
  200. package/dist/simulation/living/scale.d.ts +96 -0
  201. package/dist/simulation/living/scale.js +124 -0
  202. package/dist/simulation/living/script.d.ts +5 -0
  203. package/dist/simulation/living/script.js +115 -0
  204. package/dist/simulation/living/shared-kinds.d.ts +4 -11
  205. package/dist/simulation/living/shared-kinds.js +11 -81
  206. package/dist/simulation/living/space.d.ts +14 -0
  207. package/dist/simulation/living/space.js +132 -0
  208. package/dist/simulation/living/state.d.ts +100 -0
  209. package/dist/simulation/living/state.js +393 -0
  210. package/dist/simulation/living/types.d.ts +829 -512
  211. package/dist/simulation/living/types.js +162 -187
  212. package/dist/simulation/living/world-rules.d.ts +100 -0
  213. package/dist/simulation/living/world-rules.js +267 -0
  214. package/dist/simulation/plugins/loader.js +11 -0
  215. package/dist/skills/loader.js +4 -0
  216. package/dist/tools.d.ts +1 -0
  217. package/dist/tools.js +1 -0
  218. package/dist/ui/ratatui/headless_pane_manager.d.ts +4 -1
  219. package/dist/ui/ratatui/headless_pane_manager.js +13 -9
  220. package/dist/ui/ratatui/pane_relay.js +29 -0
  221. package/dist/ui/ratatui/pane_type.d.ts +10 -0
  222. package/dist/ui/ratatui/pane_type.js +11 -0
  223. package/dist/ui/ratatui/process.d.ts +30 -0
  224. package/dist/ui/ratatui/process.js +133 -19
  225. package/dist/ui/ratatui/protocol.d.ts +124 -2
  226. package/dist/ui/ratatui/protocol.js +35 -0
  227. package/dist/ui/ratatui/relay_policy.js +3 -0
  228. package/dist/ui/ratatui/ztc_launch_trust.d.ts +2 -1
  229. package/dist/ui/ratatui/ztc_launch_trust.js +8 -1
  230. package/dist/ui/terminal_image.d.ts +12 -0
  231. package/dist/ui/terminal_image.js +21 -1
  232. package/dist/utils/codex_project_trust.d.ts +19 -0
  233. package/dist/utils/codex_project_trust.js +37 -0
  234. package/dist/utils/compaction.js +103 -20
  235. package/dist/utils/git_program_files.d.ts +19 -12
  236. package/dist/utils/git_program_files.js +411 -83
  237. package/dist/utils/host_shell.d.ts +10 -1
  238. package/dist/utils/host_shell.js +14 -1
  239. package/dist/utils/mid_turn_compaction.d.ts +2 -0
  240. package/dist/utils/mid_turn_compaction.js +2 -0
  241. package/dist/utils/models.js +6 -1
  242. package/dist/utils/sandbox.d.ts +109 -8
  243. package/dist/utils/sandbox.js +981 -187
  244. package/dist/utils/shell.d.ts +2 -0
  245. package/dist/utils/shell.js +10 -4
  246. package/dist/utils/subprocess_env.d.ts +47 -11
  247. package/dist/utils/subprocess_env.js +107 -15
  248. package/dist/utils/system_ps.d.ts +8 -0
  249. package/dist/utils/system_ps.js +39 -7
  250. package/dist/utils/unattended_compaction.d.ts +9 -0
  251. package/dist/utils/unattended_compaction.js +13 -0
  252. package/dist/utils/ztc_own_state.d.ts +8 -5
  253. package/dist/utils/ztc_own_state.js +17 -11
  254. package/dist/web/mirror_client.js +1 -0
  255. package/dist/web/mirror_stream.d.ts +50 -6
  256. package/dist/web/mirror_stream.js +160 -15
  257. package/dist/workflows/registry.js +3 -1
  258. package/dist/workspace/controller.js +12 -0
  259. package/dist/workspace_collision.d.ts +3 -0
  260. package/dist/workspace_collision.js +14 -1
  261. package/dist/workspace_session_context.js +2 -1
  262. package/dist/zlm-embed.mjs +3823 -1970
  263. package/node_modules/@zerg/model-contracts/README.md +16 -4
  264. package/node_modules/@zerg/model-contracts/index.js +6 -1
  265. package/npm-shrinkwrap.json +1 -1
  266. package/package.json +42 -24
  267. package/packages/model-contracts/README.md +16 -4
  268. package/packages/model-contracts/index.js +6 -1
@@ -10,12 +10,12 @@
10
10
  */
11
11
  import { spawn } from 'child_process';
12
12
  import { randomUUID } from 'node:crypto';
13
- import { existsSync, unlinkSync } from 'fs';
13
+ import { existsSync, readFileSync, unlinkSync } from 'fs';
14
14
  import { createInterface } from 'readline';
15
15
  import { join } from 'path';
16
16
  import { homedir, platform } from 'os';
17
17
  import { createConnection } from 'net';
18
- import { serializeCommand, parseEvent, CAPABILITY_CLIPBOARD_IMAGE, CAPABILITY_CLIPBOARD_TEXT } from './protocol.js';
18
+ import { serializeCommand, parseEvent, CAPABILITY_CLIPBOARD_IMAGE, CAPABILITY_CLIPBOARD_TEXT, CAPABILITY_OWNER_REATTACH } from './protocol.js';
19
19
  import { generateIpcPath, removeOwnedIpcPath } from './ipc_path.js';
20
20
  import { loadDesktopRuntime, validateDesktopRendererReady } from '../../desktop/runtime.js';
21
21
  import { debugFileLog as debugLog, describeInputForLog } from '../../utils/debug_log.js';
@@ -293,6 +293,12 @@ export class TuiProcess {
293
293
  _muxMode = false;
294
294
  /** Whether zterm is being used as the native desktop renderer */
295
295
  _desktopMode = false;
296
+ /** Whether this owner attached to an already-running ZTerm via `owner_reattach_v1`. */
297
+ _reattached = false;
298
+ /** True while detaching cleanly for an in-situ owner handoff (never send shutdown / unlink socket / SIGTERM zterm). */
299
+ detachingForReattach = false;
300
+ /** First-line reattach token waiting to be written immediately upon socket connect. */
301
+ pendingReattachToken = null;
296
302
  desktopCompatibility = null;
297
303
  /** Retained across socket cleanup so an admitted renderer can request owner exit. */
298
304
  desktopStartupAdmitted = false;
@@ -542,6 +548,18 @@ export class TuiProcess {
542
548
  get zmuxMirrorSocketPath() {
543
549
  return this._zmuxMirrorSocketPath;
544
550
  }
551
+ /** Whether this owner reattached to an existing ZTerm instance. */
552
+ get isReattached() {
553
+ return this._reattached;
554
+ }
555
+ /** Active Unix socket path for the current renderer connection. */
556
+ get activeSocketPath() {
557
+ return this.socketPath;
558
+ }
559
+ /** Sibling 0600 reattach token file path for the current renderer socket. */
560
+ get reattachTokenPath() {
561
+ return this.socketPath ? reattachTokenPathForSocket(this.socketPath) : null;
562
+ }
545
563
  /**
546
564
  * Start the Rust TUI process
547
565
  * @param options.mux - If true, spawn zmux instead of ztc-tui
@@ -550,16 +568,57 @@ export class TuiProcess {
550
568
  * @returns Promise that resolves with terminal size when ready
551
569
  */
552
570
  async start(options) {
553
- if (this.process) {
571
+ if (this.process || this.socket) {
554
572
  throw new Error('TUI process already running');
555
573
  }
556
574
  this._muxMode = options?.mux ?? false;
557
- this._desktopMode = options?.desktop ?? false;
575
+ this._desktopMode = options?.desktop ?? Boolean(options?.reattachSocket);
576
+ this._reattached = false;
577
+ this.detachingForReattach = false;
578
+ this.pendingReattachToken = null;
558
579
  this.desktopCompatibility = null;
559
580
  this.desktopStartupAdmitted = false;
560
581
  this._capabilities.clear();
561
582
  this.stopping = false;
562
583
  this.timedOutPaneRequestIds.clear();
584
+ // Live owner reattach (`owner_reattach_v1`): connect directly to an
585
+ // existing ZTerm socket without spawning a second window.
586
+ if (options?.reattachSocket) {
587
+ const tokenPath = options.reattachTokenFile ?? reattachTokenPathForSocket(options.reattachSocket);
588
+ const token = (options.reattachToken ?? readFileSync(tokenPath, 'utf8')).trim();
589
+ if (!token) {
590
+ throw new Error(`Reattach token file is empty: ${tokenPath}`);
591
+ }
592
+ this._muxMode = options.mux ?? true;
593
+ this._desktopMode = options.desktop ?? true;
594
+ this._reattached = true;
595
+ this.socketPath = options.reattachSocket;
596
+ this.pendingReattachToken = token;
597
+ this.readyPromise = new Promise((resolve, reject) => {
598
+ this.readyResolve = resolve;
599
+ this.readyReject = reject;
600
+ });
601
+ const myReject = this.readyReject;
602
+ const timeout = setTimeout(() => {
603
+ if (this.readyReject && this.readyReject === myReject) {
604
+ this.readyReject(new Error('ZTerm reattach did not become ready within timeout'));
605
+ this.readyResolve = null;
606
+ this.readyReject = null;
607
+ this.cleanup();
608
+ }
609
+ }, 20000);
610
+ try {
611
+ this.connectToSocket();
612
+ return await this.readyPromise;
613
+ }
614
+ catch (err) {
615
+ this.cleanup();
616
+ throw err;
617
+ }
618
+ finally {
619
+ clearTimeout(timeout);
620
+ }
621
+ }
563
622
  let binaryPath;
564
623
  if (this._desktopMode) {
565
624
  const runtime = options?.desktopRuntime === undefined ? loadDesktopRuntime() : options.desktopRuntime;
@@ -784,7 +843,7 @@ export class TuiProcess {
784
843
  this.connectRetryTimer = null;
785
844
  // Abort if start() already settled or the process was torn down between
786
845
  // scheduling this retry and now — otherwise we'd connect a dead path.
787
- if (!this.process || this.socketPath !== targetPath || !this.readyReject) {
846
+ if ((!this.process && !this._reattached) || this.socketPath !== targetPath || !this.readyReject) {
788
847
  return;
789
848
  }
790
849
  attempts++;
@@ -816,6 +875,11 @@ export class TuiProcess {
816
875
  this.cleanup();
817
876
  });
818
877
  this.setupSocketHandlers();
878
+ if (this.pendingReattachToken !== null) {
879
+ const token = this.pendingReattachToken;
880
+ this.pendingReattachToken = null;
881
+ socket.write(serializeCommand({ type: 'reattach', token }) + '\n');
882
+ }
819
883
  });
820
884
  this.socket = socket;
821
885
  socket.on('error', onConnectError);
@@ -858,6 +922,13 @@ export class TuiProcess {
858
922
  this.settleClipboardTextRequest(event.request_id, !event.error && typeof event.text === 'string' ? event.text : null);
859
923
  return;
860
924
  }
925
+ if (event.type === 'error' && this.readyReject && !this.ready) {
926
+ this.readyReject(new Error(event.message || event.code || 'Renderer refused connection'));
927
+ this.readyResolve = null;
928
+ this.readyReject = null;
929
+ this.cleanup();
930
+ return;
931
+ }
861
932
  if (event.type === 'ready' && this.readyResolve) {
862
933
  if (this.desktopCompatibility) {
863
934
  try {
@@ -873,6 +944,8 @@ export class TuiProcess {
873
944
  }
874
945
  this.desktopStartupAdmitted = this._desktopMode;
875
946
  this.ready = true;
947
+ if (event.reattached)
948
+ this._reattached = true;
876
949
  this._isZmux = event.version.startsWith('zmux-') || event.version.startsWith('zterm-');
877
950
  if (event.capabilities) {
878
951
  this._capabilities = new Set(event.capabilities);
@@ -895,7 +968,12 @@ export class TuiProcess {
895
968
  this.emit(event);
896
969
  });
897
970
  this.readline.on('close', () => {
971
+ const shouldExitOwner = this._reattached && this._desktopMode && this.desktopStartupAdmitted
972
+ && !this.stopping && !this.detachingForReattach;
898
973
  this.cleanup();
974
+ if (shouldExitOwner) {
975
+ this.emit({ type: 'quit_requested' });
976
+ }
899
977
  });
900
978
  }
901
979
  /**
@@ -943,7 +1021,30 @@ export class TuiProcess {
943
1021
  * Check if the process is running and ready
944
1022
  */
945
1023
  isReady() {
946
- return this.ready && this.process !== null && this.socket !== null;
1024
+ return this.ready && (this.process !== null || this._reattached) && this.socket !== null;
1025
+ }
1026
+ /**
1027
+ * Disconnect cleanly from a live ZTerm window WITHOUT sending `shutdown`,
1028
+ * WITHOUT unlinking the socket, and WITHOUT signalling the ZTerm process,
1029
+ * so a replacement ZTC harness can reattach via `owner_reattach_v1` while
1030
+ * every shell/agent pane and detached OS window keeps running.
1031
+ */
1032
+ disconnectForReattach() {
1033
+ if (!this._desktopMode || !this.socketPath || !this.hasCapability(CAPABILITY_OWNER_REATTACH)) {
1034
+ return null;
1035
+ }
1036
+ const socketPath = this.socketPath;
1037
+ const tokenFilePath = reattachTokenPathForSocket(socketPath);
1038
+ this.detachingForReattach = true;
1039
+ this.stopping = true;
1040
+ if (this.process) {
1041
+ this.process.removeAllListeners();
1042
+ this.process.stderr?.removeAllListeners();
1043
+ this.process.unref();
1044
+ this.process = null;
1045
+ }
1046
+ this.cleanup();
1047
+ return { socketPath, tokenFilePath };
947
1048
  }
948
1049
  /**
949
1050
  * Clean up resources
@@ -985,18 +1086,21 @@ export class TuiProcess {
985
1086
  this.socket.destroy();
986
1087
  this.socket = null;
987
1088
  }
1089
+ const preserveLiveRenderer = this.detachingForReattach || this._reattached;
988
1090
  if (this.socketPath) {
989
- try {
990
- unlinkSync(this.socketPath);
991
- }
992
- catch {
993
- // Ignore
994
- }
995
- try {
996
- removeOwnedIpcPath(this.socketPath);
997
- }
998
- catch (error) {
999
- debugLog(`[TuiProcess] IPC directory cleanup failed: ${String(error)}`);
1091
+ if (!preserveLiveRenderer) {
1092
+ try {
1093
+ unlinkSync(this.socketPath);
1094
+ }
1095
+ catch {
1096
+ // Ignore
1097
+ }
1098
+ try {
1099
+ removeOwnedIpcPath(this.socketPath);
1100
+ }
1101
+ catch (error) {
1102
+ debugLog(`[TuiProcess] IPC directory cleanup failed: ${String(error)}`);
1103
+ }
1000
1104
  }
1001
1105
  this.socketPath = null;
1002
1106
  }
@@ -1007,7 +1111,7 @@ export class TuiProcess {
1007
1111
  // handle escalation.
1008
1112
  const child = this.process;
1009
1113
  this.process = null;
1010
- if (child && child.exitCode === null && child.signalCode === null) {
1114
+ if (!this.detachingForReattach && child && child.exitCode === null && child.signalCode === null) {
1011
1115
  try {
1012
1116
  child.kill('SIGTERM');
1013
1117
  }
@@ -1020,7 +1124,7 @@ export class TuiProcess {
1020
1124
  * Stop the Rust TUI process gracefully
1021
1125
  */
1022
1126
  async stop() {
1023
- if (!this.process)
1127
+ if (!this.process && !this._reattached)
1024
1128
  return;
1025
1129
  this.stopping = true;
1026
1130
  // Send shutdown command
@@ -1039,6 +1143,12 @@ export class TuiProcess {
1039
1143
  resolve();
1040
1144
  });
1041
1145
  }
1146
+ else if (this.socket) {
1147
+ this.socket.once('close', () => {
1148
+ clearTimeout(timeout);
1149
+ resolve();
1150
+ });
1151
+ }
1042
1152
  else {
1043
1153
  clearTimeout(timeout);
1044
1154
  resolve();
@@ -1115,6 +1225,10 @@ export class NullTuiProcess {
1115
1225
  export function getNullTuiProcess(options) {
1116
1226
  return new NullTuiProcess(options);
1117
1227
  }
1228
+ /** Sibling 0600 rotating reattach token path created by ZTerm (`owner_reattach_v1`). */
1229
+ export function reattachTokenPathForSocket(socketPath) {
1230
+ return `${socketPath}.reattach-token`;
1231
+ }
1118
1232
  // Singleton instance
1119
1233
  let tuiProcess = null;
1120
1234
  /**
@@ -85,6 +85,27 @@ export declare const CAPABILITY_ATTENTION_BADGES = "attention_badges_v1";
85
85
  * close_window only to a renderer that advertises it.
86
86
  */
87
87
  export declare const CAPABILITY_DESKTOP_CLOSE_WINDOW = "desktop_close_window_v1";
88
+ /**
89
+ * The renderer presents native non-blocking modal dialogs (select pickers, text inputs,
90
+ * delegated worker approvals) via `show_desktop_dialog` / `dismiss_desktop_dialog` and
91
+ * answers with `desktop_dialog_result`, without stealing focus to Main or blocking on Main prompts.
92
+ */
93
+ export declare const CAPABILITY_DESKTOP_NATIVE_DIALOGS = "desktop_native_dialogs_v1";
94
+ /**
95
+ * The renderer supports detaching non-Main tabs into separate OS windows (multi-viewport)
96
+ * and reattaching them back into the primary OS window.
97
+ */
98
+ export declare const CAPABILITY_DESKTOP_MULTI_WINDOW = "desktop_multi_window_v1";
99
+ /**
100
+ * The renderer supports live owner reattach via `Command::Reattach { token }`
101
+ * with a rotating 0600 token file beside its Unix socket (`owner_reattach_v1`).
102
+ */
103
+ export declare const CAPABILITY_OWNER_REATTACH = "owner_reattach_v1";
104
+ /**
105
+ * The renderer periodically emits `Event::Health` telemetry and renders a live
106
+ * title-bar health chip via `Command::SetHealthChip` / `Event::HealthActionChosen`.
107
+ */
108
+ export declare const CAPABILITY_DESKTOP_HEALTH = "desktop_health_v1";
88
109
  /** Render work the native renderer counted, rather than timed, over `frames` frames
89
110
  * (packages/zterm/src/terminal/stats.rs). Lock and repaint times are nanoseconds. */
90
111
  export interface TerminalRenderCounters {
@@ -209,6 +230,8 @@ export interface MuxWindowState {
209
230
  layout: MuxLayoutNode;
210
231
  color?: string;
211
232
  pattern?: string;
233
+ /** Optional detached OS window group id (multi-window ZTerm). Absent/0 for primary OS window. */
234
+ os_window_id?: number;
212
235
  }
213
236
  export interface MuxSessionState {
214
237
  schema_version?: 1;
@@ -747,7 +770,85 @@ export type RustCommand = {
747
770
  } | {
748
771
  type: 'set_wheel_capture';
749
772
  active: boolean;
773
+ }
774
+ /**
775
+ * Present a native non-blocking modal dialog in ZTerm (select picker, text input,
776
+ * or delegated worker approval). Answered with `desktop_dialog_result`.
777
+ * Capability gate: `desktop_native_dialogs_v1`.
778
+ */
779
+ | {
780
+ type: 'show_desktop_dialog';
781
+ request_id: string;
782
+ dialog: DesktopDialogSpec;
783
+ }
784
+ /** Dismiss a pending native desktop modal dialog by request_id. */
785
+ | {
786
+ type: 'dismiss_desktop_dialog';
787
+ request_id: string;
788
+ }
789
+ /** Update the title-bar desktop health chip (`desktop_health_v1`). */
790
+ | {
791
+ type: 'set_health_chip';
792
+ level: HealthLevel;
793
+ text: string;
794
+ detail?: string[];
795
+ actions?: HealthAction[];
750
796
  };
797
+ export type HealthLevel = 'ok' | 'warn' | 'critical';
798
+ export type HealthAction = 'restart_owner' | 'end_owner' | 'copy_diagnostics' | 'open_doctor';
799
+ export interface RendererHealth {
800
+ ui_frame_age_ms?: number;
801
+ ui_queue?: number;
802
+ ui_oldest_ms?: number;
803
+ out_bytes?: number;
804
+ out_stalled_ms?: number;
805
+ threads?: number;
806
+ fds?: number;
807
+ fd_limit?: number;
808
+ rss_bytes?: number;
809
+ owner_threads?: number;
810
+ owner_fds?: number;
811
+ panes?: number;
812
+ detached_readers?: number;
813
+ max_event_backlog?: number;
814
+ longest_lock_hold_ms?: number;
815
+ coalesced_events?: number;
816
+ tty_blocked_ms?: number;
817
+ }
818
+ export interface DesktopDialogOption {
819
+ value: string;
820
+ label: string;
821
+ description?: string;
822
+ badge?: string;
823
+ }
824
+ export type DesktopDialogSpec = {
825
+ kind: 'select';
826
+ title: string;
827
+ question?: string;
828
+ options: DesktopDialogOption[];
829
+ filterable?: boolean;
830
+ hint?: string;
831
+ } | {
832
+ kind: 'input';
833
+ title: string;
834
+ question?: string;
835
+ default_value?: string;
836
+ placeholder?: string;
837
+ hint?: string;
838
+ } | {
839
+ kind: 'approval';
840
+ title: string;
841
+ detail?: string;
842
+ pane_id?: number;
843
+ capabilities: string[];
844
+ approvable: boolean;
845
+ };
846
+ export interface DesktopDialogResultEvent {
847
+ type: 'desktop_dialog_result';
848
+ request_id: string;
849
+ submitted: boolean;
850
+ value?: string | null;
851
+ }
751
852
  /**
752
853
  * The owner's liveness check, every 2 s, to a renderer that advertises owner_ping_v1 (owner_ping.ts): its socket reader
753
854
  * answers at once with `pong` and the same `seq`. Not a RustCommand: only the owner's own connection carries one, so the
@@ -757,7 +858,16 @@ export type OwnerPingCommand = {
757
858
  type: 'ping';
758
859
  seq: number;
759
860
  };
760
- export type RustEvent = DesktopTaskAction | DesktopRuntimeAction | DesktopSessionShareAction | {
861
+ /**
862
+ * Replacement owner takeover handshake (`owner_reattach_v1`): sent as the very
863
+ * first line on a new connection to an existing ZTerm socket after its prior
864
+ * owner left without `shutdown`. Never a RustCommand (refused on the pane relay).
865
+ */
866
+ export type ReattachCommand = {
867
+ type: 'reattach';
868
+ token: string;
869
+ };
870
+ export type RustEvent = DesktopTaskAction | DesktopRuntimeAction | DesktopSessionShareAction | DesktopDialogResultEvent | {
761
871
  type: 'ready';
762
872
  version: string;
763
873
  cols: number;
@@ -767,6 +877,7 @@ export type RustEvent = DesktopTaskAction | DesktopRuntimeAction | DesktopSessio
767
877
  width: number;
768
878
  height: number;
769
879
  };
880
+ reattached?: boolean;
770
881
  } | {
771
882
  type: 'input';
772
883
  input: string;
@@ -939,11 +1050,21 @@ export type RustEvent = DesktopTaskAction | DesktopRuntimeAction | DesktopSessio
939
1050
  seq: number;
940
1051
  ui_oldest_ms: number;
941
1052
  out_bytes: number;
1053
+ }
1054
+ /** Periodic renderer health telemetry (`desktop_health_v1`). */
1055
+ | {
1056
+ type: 'health';
1057
+ health: RendererHealth;
1058
+ }
1059
+ /** User chose an action from the title-bar health popover (`desktop_health_v1`). */
1060
+ | {
1061
+ type: 'health_action_chosen';
1062
+ action: HealthAction;
942
1063
  };
943
1064
  /**
944
1065
  * Serialize a command to JSON for sending to Rust
945
1066
  */
946
- export declare function serializeCommand(cmd: RustCommand | OwnerPingCommand): string;
1067
+ export declare function serializeCommand(cmd: RustCommand | OwnerPingCommand | ReattachCommand): string;
947
1068
  /**
948
1069
  * Parse an event from Rust
949
1070
  */
@@ -983,5 +1104,6 @@ export declare function isDesktopCredentialsUrl(value: unknown): value is string
983
1104
  * it conservatively.
984
1105
  */
985
1106
  export declare function isDesktopRuntimeChoices(value: unknown): value is DesktopRuntimeChoices;
1107
+ export declare function isDesktopDialogResult(value: unknown): value is DesktopDialogResultEvent;
986
1108
  /** Reject malformed native actions before any host queue operation sees them. */
987
1109
  export declare function isDesktopTaskAction(value: unknown): value is DesktopTaskAction;
@@ -56,6 +56,27 @@ export const CAPABILITY_ATTENTION_BADGES = 'attention_badges_v1';
56
56
  * close_window only to a renderer that advertises it.
57
57
  */
58
58
  export const CAPABILITY_DESKTOP_CLOSE_WINDOW = 'desktop_close_window_v1';
59
+ /**
60
+ * The renderer presents native non-blocking modal dialogs (select pickers, text inputs,
61
+ * delegated worker approvals) via `show_desktop_dialog` / `dismiss_desktop_dialog` and
62
+ * answers with `desktop_dialog_result`, without stealing focus to Main or blocking on Main prompts.
63
+ */
64
+ export const CAPABILITY_DESKTOP_NATIVE_DIALOGS = 'desktop_native_dialogs_v1';
65
+ /**
66
+ * The renderer supports detaching non-Main tabs into separate OS windows (multi-viewport)
67
+ * and reattaching them back into the primary OS window.
68
+ */
69
+ export const CAPABILITY_DESKTOP_MULTI_WINDOW = 'desktop_multi_window_v1';
70
+ /**
71
+ * The renderer supports live owner reattach via `Command::Reattach { token }`
72
+ * with a rotating 0600 token file beside its Unix socket (`owner_reattach_v1`).
73
+ */
74
+ export const CAPABILITY_OWNER_REATTACH = 'owner_reattach_v1';
75
+ /**
76
+ * The renderer periodically emits `Event::Health` telemetry and renders a live
77
+ * title-bar health chip via `Command::SetHealthChip` / `Event::HealthActionChosen`.
78
+ */
79
+ export const CAPABILITY_DESKTOP_HEALTH = 'desktop_health_v1';
59
80
  /** Limits on `runtime_choices`, the same as ztc-core's (protocol.rs). Byte limits are UTF-8 bytes. */
60
81
  export const DESKTOP_RUNTIME_MAX_PROVIDER_CHOICES = 48;
61
82
  export const DESKTOP_RUNTIME_MAX_MODEL_CHOICES = 128;
@@ -99,6 +120,8 @@ export function parseEvent(json) {
99
120
  return null;
100
121
  if (parsed.type === 'desktop_session_share_action' && !isDesktopSessionShareAction(parsed))
101
122
  return null;
123
+ if (parsed.type === 'desktop_dialog_result' && !isDesktopDialogResult(parsed))
124
+ return null;
102
125
  if (parsed.type === 'window_closed')
103
126
  return isWindowClosed(parsed) ? windowClosedFields(parsed) : null;
104
127
  if (parsed.type === 'layout_changed')
@@ -360,6 +383,18 @@ export function isDesktopRuntimeChoices(value) {
360
383
  return new Set(providerIds).size === providerIds.length && new Set(modelIds).size === modelIds.length
361
384
  && providerIds.includes(provider) && modelIds.includes(model);
362
385
  }
386
+ export function isDesktopDialogResult(value) {
387
+ if (value === null || typeof value !== 'object' || Array.isArray(value))
388
+ return false;
389
+ const event = value;
390
+ return event.type === 'desktop_dialog_result'
391
+ && typeof event.request_id === 'string' && event.request_id.length > 0
392
+ && Buffer.byteLength(event.request_id, 'utf8') <= 256
393
+ && !/[\u0000-\u001f\u007f-\u009f]/.test(event.request_id)
394
+ && typeof event.submitted === 'boolean'
395
+ && (event.value === undefined || event.value === null
396
+ || (typeof event.value === 'string' && Buffer.byteLength(event.value, 'utf8') <= 65_536));
397
+ }
363
398
  /** Reject malformed native actions before any host queue operation sees them. */
364
399
  export function isDesktopTaskAction(value) {
365
400
  if (value === null || typeof value !== 'object' || Array.isArray(value))
@@ -71,6 +71,9 @@ const CLASSES = {
71
71
  set_keybindings: 'owner_only',
72
72
  set_input_capture: 'owner_only',
73
73
  set_wheel_capture: 'owner_only',
74
+ show_desktop_dialog: 'owner_only',
75
+ dismiss_desktop_dialog: 'owner_only',
76
+ set_health_chip: 'owner_only',
74
77
  };
75
78
  /** Each command type's class. */
76
79
  export const RELAY_COMMAND_CLASSES = CLASSES;
@@ -75,7 +75,8 @@ export declare class ZtcLaunchNonces<Launch> {
75
75
  * tools send a ZTC pane (agent/tools/pane.ts childZtcPaneEnv and agentZtcPaneEnv), and only the values they send, and a
76
76
  * request for the pane's own mirror credential, which the pane transport answers (pane_credential_broker.ts). Nothing
77
77
  * else of it reaches the pane: not a program to preload (NODE_OPTIONS), not a PATH or another grant or Router of the
78
- * child's choosing; the host's pin of the permission settings only as 1, which pins. The pane joins this relay
78
+ * child's choosing; the host's pin of the permission settings only as 1, which pins; and a nested run's marker
79
+ * (config/profiles.ts nestedRunEnv) only as this process's own, whose settings the pane starts on. The pane joins this relay
79
80
  * (RELAY_PATH) when its command asks to join one. One the relay starts
80
81
  * inside MotSi (THROUGH_MOTSI) has MotSi's terminal id blank (panes/motsi.ts MOTSI_PANE_ENV), as ZTC's own do. What this
81
82
  * process gives one of its own ZTC panes of its grant goes in whatever the client sent: a blank for a grant that takes
@@ -17,6 +17,7 @@ import { randomBytes } from 'node:crypto';
17
17
  import { existsSync, realpathSync } from 'node:fs';
18
18
  import { dirname, isAbsolute, join, resolve } from 'node:path';
19
19
  import { PERMISSION_PIN_ENV } from '../../config/permission_pin.js';
20
+ import { nestedRunEnv } from '../../config/profiles.js';
20
21
  import { ROUTER_RUNTIME_GRANT_ENV } from '../../config/router_runtime_grant.js';
21
22
  import { loadDesktopRuntime } from '../../desktop/runtime.js';
22
23
  import { ZTC_EXTENSION_DISCOVERY_ENV } from '../../extension_discovery.js';
@@ -193,7 +194,8 @@ export class ZtcLaunchNonces {
193
194
  * tools send a ZTC pane (agent/tools/pane.ts childZtcPaneEnv and agentZtcPaneEnv), and only the values they send, and a
194
195
  * request for the pane's own mirror credential, which the pane transport answers (pane_credential_broker.ts). Nothing
195
196
  * else of it reaches the pane: not a program to preload (NODE_OPTIONS), not a PATH or another grant or Router of the
196
- * child's choosing; the host's pin of the permission settings only as 1, which pins. The pane joins this relay
197
+ * child's choosing; the host's pin of the permission settings only as 1, which pins; and a nested run's marker
198
+ * (config/profiles.ts nestedRunEnv) only as this process's own, whose settings the pane starts on. The pane joins this relay
197
199
  * (RELAY_PATH) when its command asks to join one. One the relay starts
198
200
  * inside MotSi (THROUGH_MOTSI) has MotSi's terminal id blank (panes/motsi.ts MOTSI_PANE_ENV), as ZTC's own do. What this
199
201
  * process gives one of its own ZTC panes of its grant goes in whatever the client sent: a blank for a grant that takes
@@ -234,6 +236,11 @@ export function relayedZtcPaneEnv(env, relayPath, { throughMotsi = false } = {})
234
236
  if (value === '1')
235
237
  kept[name] = value;
236
238
  break;
239
+ // A pane its agent opened is a nested run: this ZTC's, whose renderer starts it, whatever ZTC the client names.
240
+ case 'ZTC_NESTED_RUN':
241
+ if (value !== '')
242
+ Object.assign(kept, nestedRunEnv());
243
+ break;
237
244
  case PANE_CREDENTIAL_ENV:
238
245
  if (value === ZTC_PANE_CREDENTIAL_REQUEST || value === WORKSPACE_MONITOR_CREDENTIAL_REQUEST)
239
246
  kept[name] = value;
@@ -62,6 +62,18 @@ export interface CellPixels {
62
62
  * `ZTC_IMAGE_PROTOCOL=kitty` or `=blocks` overrides the detection.
63
63
  */
64
64
  export declare function kittyGraphicsTerminal(env: NodeJS.ProcessEnv): boolean;
65
+ /**
66
+ * Decide whether transcript images use uploaded Kitty/GPU graphics or half blocks.
67
+ * ZTC Desktop renders uploaded RGBA images natively on the GPU once it advertises
68
+ * `kitty_graphics`, and ZTerm terminal panes (including child ZTC panes `ztc --pane`
69
+ * running with `TERM_PROGRAM=zterm`) natively ingest Kitty APC uploads into GPU textures.
70
+ */
71
+ export declare function resolveTerminalImageMode(env: NodeJS.ProcessEnv, options: {
72
+ desktopMode: boolean;
73
+ paneMode: boolean;
74
+ headlessMirror: boolean;
75
+ rendererSupportsImages: boolean;
76
+ }): TerminalImageMode;
65
77
  /**
66
78
  * Draw transcript images as `mode` from now on. `cell` is the cell size the
67
79
  * renderer measured, when the terminal reports one. Previews of files are read
@@ -199,9 +199,29 @@ export function kittyGraphicsTerminal(env) {
199
199
  if (env.TMUX || term.startsWith('tmux') || term.startsWith('screen'))
200
200
  return false;
201
201
  if (program)
202
- return program === 'ghostty' || program === 'kitty';
202
+ return program === 'ghostty' || program === 'kitty' || program === 'zterm';
203
203
  return term === 'xterm-ghostty' || term === 'xterm-kitty' || Boolean(env.KITTY_WINDOW_ID) || Boolean(env.GHOSTTY_RESOURCES_DIR);
204
204
  }
205
+ /**
206
+ * Decide whether transcript images use uploaded Kitty/GPU graphics or half blocks.
207
+ * ZTC Desktop renders uploaded RGBA images natively on the GPU once it advertises
208
+ * `kitty_graphics`, and ZTerm terminal panes (including child ZTC panes `ztc --pane`
209
+ * running with `TERM_PROGRAM=zterm`) natively ingest Kitty APC uploads into GPU textures.
210
+ */
211
+ export function resolveTerminalImageMode(env, options) {
212
+ if (options.headlessMirror || !options.rendererSupportsImages)
213
+ return 'blocks';
214
+ const override = env.ZTC_IMAGE_PROTOCOL?.toLowerCase();
215
+ if (override === 'blocks' || override === 'none' || override === '0')
216
+ return 'blocks';
217
+ if (override === 'kitty')
218
+ return 'kitty';
219
+ if (options.paneMode)
220
+ return kittyGraphicsTerminal(env) ? 'kitty' : 'blocks';
221
+ if (options.desktopMode)
222
+ return 'kitty';
223
+ return kittyGraphicsTerminal(env) ? 'kitty' : 'blocks';
224
+ }
205
225
  /**
206
226
  * Draw transcript images as `mode` from now on. `cell` is the cell size the
207
227
  * renderer measured, when the terminal reports one. Previews of files are read
@@ -0,0 +1,19 @@
1
+ /** A folder's decision in a Codex config (`projects.<folder>.trust_level`): untrusted. */
2
+ export interface UntrustedProject {
3
+ trust_level: 'untrusted';
4
+ }
5
+ /**
6
+ * The Codex config that marks CWD and every folder above it untrusted, each under the path given and the one it
7
+ * resolves to. Codex's app server trusts the folder a thread names when no decision names it and the thread's sandbox
8
+ * can write it, and saves that in CODEX_HOME for the repository's root, or the main checkout's root for a linked
9
+ * worktree (codex 0.147.0, 0.167.2 and 0.170.0, and 0.171.0 by E1; 0.147.0 also saves a folder outside any repository
10
+ * itself). A trusted folder's .codex/config.toml then starts its MCP servers, with the environment names they ask for,
11
+ * its hooks and its exec rules, outside ZTC's OS sandbox, and so does a folder CODEX_HOME saved as trusted before. The
12
+ * folder alone is not enough: Codex loads the .codex of each folder it trusts from the repository's root down to the
13
+ * folder, so a repository root CODEX_HOME trusts, or a folder between them, loads its own though the folder is marked.
14
+ * Nor is the path given alone: Codex looks a folder named through a link up by the path it resolves to. A decision the
15
+ * config names is one Codex never overrides, whatever CODEX_HOME saved. A CWD that doesn't resolve is marked as given.
16
+ */
17
+ export declare function untrustedProjectsConfig(cwd: string): {
18
+ projects: Record<string, UntrustedProject>;
19
+ };
@@ -0,0 +1,37 @@
1
+ /**
2
+ * The project trust a Codex ZTC starts is given: none. Codex's App Server threads (agent/backends/codex_app_server.ts,
3
+ * in thread/start's config) and the desktop daily driver's delegated `codex exec` workers (desktop/daily/recipes.ts, as
4
+ * a `-c projects=` override) mark their folders untrusted with it, so neither loads a project-local Codex config.
5
+ */
6
+ import { realpathSync } from 'node:fs';
7
+ import { dirname, resolve } from 'node:path';
8
+ /**
9
+ * The Codex config that marks CWD and every folder above it untrusted, each under the path given and the one it
10
+ * resolves to. Codex's app server trusts the folder a thread names when no decision names it and the thread's sandbox
11
+ * can write it, and saves that in CODEX_HOME for the repository's root, or the main checkout's root for a linked
12
+ * worktree (codex 0.147.0, 0.167.2 and 0.170.0, and 0.171.0 by E1; 0.147.0 also saves a folder outside any repository
13
+ * itself). A trusted folder's .codex/config.toml then starts its MCP servers, with the environment names they ask for,
14
+ * its hooks and its exec rules, outside ZTC's OS sandbox, and so does a folder CODEX_HOME saved as trusted before. The
15
+ * folder alone is not enough: Codex loads the .codex of each folder it trusts from the repository's root down to the
16
+ * folder, so a repository root CODEX_HOME trusts, or a folder between them, loads its own though the folder is marked.
17
+ * Nor is the path given alone: Codex looks a folder named through a link up by the path it resolves to. A decision the
18
+ * config names is one Codex never overrides, whatever CODEX_HOME saved. A CWD that doesn't resolve is marked as given.
19
+ */
20
+ export function untrustedProjectsConfig(cwd) {
21
+ const projects = {};
22
+ const starts = [resolve(cwd)];
23
+ try {
24
+ starts.push(realpathSync(cwd));
25
+ }
26
+ catch {
27
+ // A folder that isn't there (any longer) is marked as given.
28
+ }
29
+ for (const start of starts) {
30
+ for (let folder = start;; folder = dirname(folder)) {
31
+ projects[folder] = { trust_level: 'untrusted' };
32
+ if (dirname(folder) === folder)
33
+ break;
34
+ }
35
+ }
36
+ return { projects };
37
+ }