@zergai/ztc 0.23.1-dev.202611152321 → 0.24.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 (149) hide show
  1. package/README.md +299 -13
  2. package/bin/zmux-darwin-arm64 +0 -0
  3. package/bin/ztc-audio-darwin-arm64 +0 -0
  4. package/bin/ztc-capture-darwin-arm64 +0 -0
  5. package/bin/ztc-computer-darwin-arm64 +0 -0
  6. package/bin/ztc-llm-darwin-arm64 +0 -0
  7. package/bin/ztc-tts-darwin-arm64 +0 -0
  8. package/bin/ztc-tui-darwin-arm64 +0 -0
  9. package/bin/zterm-darwin-arm64 +2 -2
  10. package/dist/agent/agent.js +57 -14
  11. package/dist/agent/backends/http_error.d.ts +2 -0
  12. package/dist/agent/backends/http_error.js +2 -0
  13. package/dist/agent/backends/router_gate.d.ts +3 -2
  14. package/dist/agent/backends/router_gate.js +5 -4
  15. package/dist/agent/backends/router_keys.d.ts +14 -0
  16. package/dist/agent/backends/router_keys.js +19 -0
  17. package/dist/agent/backends/router_refusal.d.ts +5 -3
  18. package/dist/agent/backends/router_refusal.js +28 -6
  19. package/dist/agent/backends/types.d.ts +1 -1
  20. package/dist/agent/backends/zrouter_stream.js +10 -6
  21. package/dist/agent/commands/agent_cli.d.ts +11 -1
  22. package/dist/agent/commands/agent_cli.js +295 -38
  23. package/dist/agent/commands/diff.js +4 -1
  24. package/dist/agent/commands/help.js +4 -1
  25. package/dist/agent/commands/plan.js +3 -1
  26. package/dist/agent/commands/session_redaction.d.ts +16 -0
  27. package/dist/agent/commands/session_redaction.js +77 -0
  28. package/dist/agent/commands/settings.js +144 -0
  29. package/dist/agent/commands/types.d.ts +5 -0
  30. package/dist/agent/commands/usage_spec.d.ts +4 -0
  31. package/dist/agent/commands/usage_spec.js +10 -4
  32. package/dist/agent/runtime/policy.d.ts +25 -0
  33. package/dist/agent/runtime/policy.js +154 -26
  34. package/dist/agent/runtime/source_verification.js +6 -2
  35. package/dist/agent/runtime/tool_runtime.js +81 -3
  36. package/dist/agent/tools/apply_patch.js +678 -131
  37. package/dist/agent/tools/bus_message.d.ts +26 -1
  38. package/dist/agent/tools/bus_message.js +126 -10
  39. package/dist/agent/tools/file.d.ts +16 -1
  40. package/dist/agent/tools/file.js +43 -5
  41. package/dist/agent/tools/index.d.ts +1 -1
  42. package/dist/agent/tools/index.js +1 -1
  43. package/dist/agent/tools/pane.js +5 -0
  44. package/dist/agent/tools/search.js +5 -1
  45. package/dist/agent/tools/supervision.js +16 -2
  46. package/dist/agent/tools/types.d.ts +22 -0
  47. package/dist/app/mirror_startup.d.ts +7 -5
  48. package/dist/app/mirror_startup.js +7 -5
  49. package/dist/app/navigator_mirrors.d.ts +20 -0
  50. package/dist/app/navigator_mirrors.js +36 -0
  51. package/dist/app/workspace_layout_controller.d.ts +11 -2
  52. package/dist/app/workspace_layout_controller.js +13 -2
  53. package/dist/batch/worktree.js +9 -1
  54. package/dist/cli_ratatui.d.ts +264 -12
  55. package/dist/cli_ratatui.js +1198 -103
  56. package/dist/config/types.d.ts +29 -0
  57. package/dist/desktop/daily/source_baseline.js +3 -1
  58. package/dist/diagnostics/session_export.d.ts +1 -0
  59. package/dist/diagnostics/session_export.js +229 -26
  60. package/dist/emulation/tool_adapters.js +42 -5
  61. package/dist/headless.js +24 -5
  62. package/dist/instance_registry.d.ts +7 -0
  63. package/dist/mcp/workspace_server.d.ts +12 -1
  64. package/dist/mcp/workspace_server.js +75 -8
  65. package/dist/panes/agent_cli.d.ts +60 -3
  66. package/dist/panes/agent_cli.js +169 -16
  67. package/dist/panes/agent_pane_sessions.d.ts +22 -0
  68. package/dist/panes/agent_pane_sessions.js +111 -0
  69. package/dist/panes/agent_panes.d.ts +77 -3
  70. package/dist/panes/agent_panes.js +199 -10
  71. package/dist/panes/agent_profiles.d.ts +5 -0
  72. package/dist/panes/agent_profiles.js +7 -0
  73. package/dist/panes/agent_resume.d.ts +134 -0
  74. package/dist/panes/agent_resume.js +155 -0
  75. package/dist/panes/agent_sessions/claude_head.d.ts +32 -0
  76. package/dist/panes/agent_sessions/claude_head.js +122 -0
  77. package/dist/panes/agent_sessions/claude_store.d.ts +59 -0
  78. package/dist/panes/agent_sessions/claude_store.js +223 -0
  79. package/dist/panes/agent_sessions/codex_head.d.ts +55 -0
  80. package/dist/panes/agent_sessions/codex_head.js +161 -0
  81. package/dist/panes/agent_sessions/codex_store.d.ts +43 -0
  82. package/dist/panes/agent_sessions/codex_store.js +187 -0
  83. package/dist/panes/agent_sessions/controller.d.ts +74 -0
  84. package/dist/panes/agent_sessions/controller.js +110 -0
  85. package/dist/panes/agent_sessions/cut_json.d.ts +14 -0
  86. package/dist/panes/agent_sessions/cut_json.js +109 -0
  87. package/dist/panes/agent_sessions/limits.d.ts +48 -0
  88. package/dist/panes/agent_sessions/limits.js +48 -0
  89. package/dist/panes/agent_sessions/scan.d.ts +49 -0
  90. package/dist/panes/agent_sessions/scan.js +86 -0
  91. package/dist/panes/agent_sessions/store_fs.d.ts +51 -0
  92. package/dist/panes/agent_sessions/store_fs.js +128 -0
  93. package/dist/panes/agent_sessions/stores.d.ts +36 -0
  94. package/dist/panes/agent_sessions/stores.js +89 -0
  95. package/dist/panes/agent_sessions/title.d.ts +9 -0
  96. package/dist/panes/agent_sessions/title.js +51 -0
  97. package/dist/panes/agent_sessions/types.d.ts +48 -0
  98. package/dist/panes/agent_sessions/types.js +2 -0
  99. package/dist/panes/reply_arbitrator.d.ts +39 -0
  100. package/dist/panes/reply_arbitrator.js +185 -0
  101. package/dist/panes/reply_modes.d.ts +291 -0
  102. package/dist/panes/reply_modes.js +598 -0
  103. package/dist/runtime_info.js +11 -2
  104. package/dist/session.js +4 -2
  105. package/dist/simulation/living/context.d.ts +8 -8
  106. package/dist/simulation/living/types.d.ts +5 -5
  107. package/dist/simulation/toolkit/dynamic_state.d.ts +1 -1
  108. package/dist/ui/ratatui/ipc_path.d.ts +5 -0
  109. package/dist/ui/ratatui/ipc_path.js +23 -3
  110. package/dist/ui/ratatui/pane_credential_broker.d.ts +23 -8
  111. package/dist/ui/ratatui/pane_credential_broker.js +49 -14
  112. package/dist/ui/ratatui/pane_relay.d.ts +5 -2
  113. package/dist/ui/ratatui/pane_relay.js +86 -19
  114. package/dist/ui/ratatui/process.js +3 -0
  115. package/dist/ui/ratatui/protocol.d.ts +50 -1
  116. package/dist/ui/ratatui/protocol.js +56 -0
  117. package/dist/ui/ratatui/relay_policy.d.ts +39 -0
  118. package/dist/ui/ratatui/relay_policy.js +94 -0
  119. package/dist/ui/ratatui/screen_privacy.d.ts +15 -0
  120. package/dist/ui/ratatui/screen_privacy.js +24 -0
  121. package/dist/ui/scrollback.js +8 -0
  122. package/dist/ui/widgets/agent_sessions_panel.d.ts +139 -0
  123. package/dist/ui/widgets/agent_sessions_panel.js +542 -0
  124. package/dist/ui/widgets/log_view.d.ts +5 -1
  125. package/dist/ui/widgets/log_view.js +40 -4
  126. package/dist/utils/atomic_write.js +9 -11
  127. package/dist/utils/canonical_path.d.ts +19 -0
  128. package/dist/utils/canonical_path.js +44 -0
  129. package/dist/utils/compaction.d.ts +2 -1
  130. package/dist/utils/compaction.js +3 -1
  131. package/dist/utils/git_directory_names.d.ts +51 -0
  132. package/dist/utils/git_directory_names.js +429 -0
  133. package/dist/utils/temporary_write_root.d.ts +6 -1
  134. package/dist/utils/temporary_write_root.js +32 -2
  135. package/dist/utils/tool_result_message.d.ts +4 -1
  136. package/dist/utils/tool_result_message.js +6 -2
  137. package/dist/web/bus_hold.d.ts +70 -0
  138. package/dist/web/bus_hold.js +118 -0
  139. package/dist/web/mirror_client.d.ts +7 -3
  140. package/dist/web/mirror_client.js +61 -23
  141. package/dist/web/mirror_route_gate.d.ts +40 -0
  142. package/dist/web/mirror_route_gate.js +71 -0
  143. package/dist/web/mirror_stream.d.ts +147 -22
  144. package/dist/web/mirror_stream.js +350 -83
  145. package/dist/web/pane_credentials.d.ts +32 -1
  146. package/dist/web/pane_credentials.js +62 -2
  147. package/dist/workspace_collision.js +11 -2
  148. package/dist/zlm-embed.mjs +940 -273
  149. package/package.json +39 -7
@@ -157,7 +157,7 @@ export declare function gameMasterAttemptInput(state: LivingState, actorId: stri
157
157
  id: string;
158
158
  toId: string;
159
159
  topic: string;
160
- status: "open" | "done" | "refused" | "answered";
160
+ status: "refused" | "open" | "done" | "answered";
161
161
  }[];
162
162
  recentHere: {
163
163
  at: string;
@@ -394,7 +394,7 @@ export declare function gameMasterAttemptInput(state: LivingState, actorId: stri
394
394
  id: string;
395
395
  toId: string;
396
396
  topic: string;
397
- status: "open" | "done" | "refused" | "answered";
397
+ status: "refused" | "open" | "done" | "answered";
398
398
  }[];
399
399
  recentHere: {
400
400
  at: string;
@@ -636,7 +636,7 @@ export declare function gameMasterAttemptInput(state: LivingState, actorId: stri
636
636
  id: string;
637
637
  toId: string;
638
638
  topic: string;
639
- status: "open" | "done" | "refused" | "answered";
639
+ status: "refused" | "open" | "done" | "answered";
640
640
  }[];
641
641
  recentHere: {
642
642
  at: string;
@@ -880,7 +880,7 @@ export declare function gameMasterAttemptInput(state: LivingState, actorId: stri
880
880
  id: string;
881
881
  toId: string;
882
882
  topic: string;
883
- status: "open" | "done" | "refused" | "answered";
883
+ status: "refused" | "open" | "done" | "answered";
884
884
  }[];
885
885
  recentHere: {
886
886
  at: string;
@@ -2062,7 +2062,7 @@ export declare function characterMind(state: LivingState, person: LivingPerson,
2062
2062
  fromId: string;
2063
2063
  toId: string;
2064
2064
  topic: string;
2065
- status: "open" | "done" | "refused" | "answered";
2065
+ status: "refused" | "open" | "done" | "answered";
2066
2066
  }[];
2067
2067
  stances: {
2068
2068
  actorId: string;
@@ -2319,7 +2319,7 @@ export declare function characterReactionInput(state: LivingState, person: Livin
2319
2319
  fromId: string;
2320
2320
  toId: string;
2321
2321
  topic: string;
2322
- status: "open" | "done" | "refused" | "answered";
2322
+ status: "refused" | "open" | "done" | "answered";
2323
2323
  }[];
2324
2324
  stances: {
2325
2325
  actorId: string;
@@ -2583,7 +2583,7 @@ export declare function characterPlanInput(state: LivingState, person: LivingPer
2583
2583
  fromId: string;
2584
2584
  toId: string;
2585
2585
  topic: string;
2586
- status: "open" | "done" | "refused" | "answered";
2586
+ status: "refused" | "open" | "done" | "answered";
2587
2587
  }[];
2588
2588
  stances: {
2589
2589
  actorId: string;
@@ -2804,7 +2804,7 @@ export declare function characterMessageReplyInput(state: LivingState, person: L
2804
2804
  fromId: string;
2805
2805
  toId: string;
2806
2806
  topic: string;
2807
- status: "open" | "done" | "refused" | "answered";
2807
+ status: "refused" | "open" | "done" | "answered";
2808
2808
  }[];
2809
2809
  stances: {
2810
2810
  actorId: string;
@@ -375,9 +375,9 @@ export declare const requestSchema: z.ZodObject<{
375
375
  receiverId: z.ZodString;
376
376
  topic: z.ZodString;
377
377
  status: z.ZodEnum<{
378
+ refused: "refused";
378
379
  open: "open";
379
380
  done: "done";
380
- refused: "refused";
381
381
  answered: "answered";
382
382
  }>;
383
383
  openedSeq: z.ZodNumber;
@@ -614,8 +614,8 @@ export declare const effectSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
614
614
  kind: z.ZodLiteral<"request.update">;
615
615
  requestId: z.ZodString;
616
616
  status: z.ZodEnum<{
617
- done: "done";
618
617
  refused: "refused";
618
+ done: "done";
619
619
  answered: "answered";
620
620
  }>;
621
621
  answer: z.ZodOptional<z.ZodString>;
@@ -1147,8 +1147,8 @@ export declare const eventSchema: z.ZodObject<{
1147
1147
  kind: z.ZodLiteral<"request.update">;
1148
1148
  requestId: z.ZodString;
1149
1149
  status: z.ZodEnum<{
1150
- done: "done";
1151
1150
  refused: "refused";
1151
+ done: "done";
1152
1152
  answered: "answered";
1153
1153
  }>;
1154
1154
  answer: z.ZodOptional<z.ZodString>;
@@ -1816,9 +1816,9 @@ export declare const stateSchema: z.ZodObject<{
1816
1816
  receiverId: z.ZodString;
1817
1817
  topic: z.ZodString;
1818
1818
  status: z.ZodEnum<{
1819
+ refused: "refused";
1819
1820
  open: "open";
1820
1821
  done: "done";
1821
- refused: "refused";
1822
1822
  answered: "answered";
1823
1823
  }>;
1824
1824
  openedSeq: z.ZodNumber;
@@ -2118,8 +2118,8 @@ export declare const stateSchema: z.ZodObject<{
2118
2118
  kind: z.ZodLiteral<"request.update">;
2119
2119
  requestId: z.ZodString;
2120
2120
  status: z.ZodEnum<{
2121
- done: "done";
2122
2121
  refused: "refused";
2122
+ done: "done";
2123
2123
  answered: "answered";
2124
2124
  }>;
2125
2125
  answer: z.ZodOptional<z.ZodString>;
@@ -63,8 +63,8 @@ export declare const agentSchema: z.ZodObject<{
63
63
  id: z.ZodString;
64
64
  eventId: z.ZodString;
65
65
  kind: z.ZodEnum<{
66
- promise: "promise";
67
66
  outcome: "outcome";
67
+ promise: "promise";
68
68
  observation: "observation";
69
69
  conflict: "conflict";
70
70
  cooperation: "cooperation";
@@ -10,6 +10,11 @@
10
10
  /**
11
11
  * Build a unique IPC endpoint name for the current process.
12
12
  *
13
+ * A Unix socket goes in the temporary directory only while that directory is this user's alone and the address fits.
14
+ * Anywhere else (Linux's shared /tmp, a directory others can open, an address past the limit) it gets a fresh
15
+ * directory of its own in /tmp that only this user can open, which removeOwnedIpcPath retires: in a shared directory
16
+ * every user sees the socket, can try it, and can take its predictable name first.
17
+ *
13
18
  * `platformName` is injectable so both branches are directly testable rather
14
19
  * than only whichever one the host happens to run.
15
20
  */
@@ -7,13 +7,32 @@
7
7
  * `\\.\pipe\<name>` form: libuv maps a bare path into the pipe namespace itself,
8
8
  * so a filesystem-style path can never reach an AF_UNIX socket there.
9
9
  */
10
- import { chmodSync, mkdtempSync, rmdirSync } from 'fs';
10
+ import { chmodSync, mkdtempSync, rmdirSync, statSync } from 'fs';
11
11
  import { tmpdir } from 'os';
12
12
  import { join } from 'path';
13
13
  const ownedEndpoints = new Map();
14
+ /**
15
+ * Whether DIRECTORY is this user's alone: a directory this user owns that no one else may open (mode 0700), as macOS's
16
+ * per-user $TMPDIR is. Linux's shared /tmp isn't, nor is a directory that is gone.
17
+ */
18
+ function isPrivateDirectory(directory) {
19
+ const uid = process.getuid?.();
20
+ try {
21
+ const info = statSync(directory);
22
+ return info.isDirectory() && info.uid === uid && (info.mode & 0o077) === 0;
23
+ }
24
+ catch {
25
+ return false;
26
+ }
27
+ }
14
28
  /**
15
29
  * Build a unique IPC endpoint name for the current process.
16
30
  *
31
+ * A Unix socket goes in the temporary directory only while that directory is this user's alone and the address fits.
32
+ * Anywhere else (Linux's shared /tmp, a directory others can open, an address past the limit) it gets a fresh
33
+ * directory of its own in /tmp that only this user can open, which removeOwnedIpcPath retires: in a shared directory
34
+ * every user sees the socket, can try it, and can take its predictable name first.
35
+ *
17
36
  * `platformName` is injectable so both branches are directly testable rather
18
37
  * than only whichever one the host happens to run.
19
38
  */
@@ -22,10 +41,11 @@ export function generateIpcPath(prefix, platformName = process.platform) {
22
41
  if (platformName === 'win32') {
23
42
  return `\\\\.\\pipe\\${prefix}-${unique}`;
24
43
  }
25
- const requested = join(tmpdir(), `${prefix}-${unique}.sock`);
44
+ const temporary = tmpdir();
45
+ const requested = join(temporary, `${prefix}-${unique}.sock`);
26
46
  // Darwin reserves one of its 104 address bytes for NUL. A long TMPDIR
27
47
  // must not truncate distinct endpoints onto the same kernel address.
28
- if (Buffer.byteLength(requested, 'utf8') <= 103)
48
+ if (Buffer.byteLength(requested, 'utf8') <= 103 && isPrivateDirectory(temporary))
29
49
  return requested;
30
50
  const channel = prefix.replace(/[^a-zA-Z0-9_-]/g, '').slice(0, 16) || 'ztc-ipc';
31
51
  const directory = mkdtempSync(join('/tmp', `${channel}-`));
@@ -20,9 +20,13 @@
20
20
  * place (replace_pane, through prepareReplace) gets a fresh credential the same way, in a one-time file, since
21
21
  * replace_pane carries no environment either. A closed pane's credential is revoked, and so is the credential of a pane
22
22
  * the renderer relaunches in place without one: zmux's restart_pane, which starts the program with the renderer's
23
- * environment alone, or a replace a pane asked for through the relay. (The desktop app's restart keeps the pane's own
24
- * environment and command line and reports no new pane, so a pane's credential stays bound; a program that took its
25
- * credential from a file, which it deleted, starts again without it.)
23
+ * environment alone, or a replace a pane asked for through the relay. The desktop app's restart keeps the pane's own
24
+ * environment and command line and reports no new pane: a credential the program took from a file, which it deleted,
25
+ * is revoked as the restart is sent (restarted), since the program that starts again can't take it, and one in the
26
+ * pane's environment stays bound, as the program starts again with it. The credential of a pane a pane opens through the
27
+ * relay records the pane whose relay connection asked for it (markRelayed), and only that credential does: one its owner
28
+ * gives the pane when it starts it again in place records none. The mirror reads it when a pane's credential asks to
29
+ * supervise a pane (PaneCredentialStore.openerOf).
26
30
  */
27
31
  import type { RustCommand } from './protocol.js';
28
32
  type CreatePane = Extract<RustCommand, {
@@ -32,12 +36,14 @@ type ReplacePane = Extract<RustCommand, {
32
36
  type: 'replace_pane';
33
37
  }>;
34
38
  /**
35
- * Mark a command that a pane sent through the pane relay. The mark is a hidden property, so it never reaches the
36
- * renderer or changes the command's shape; it tells the transport the command is a pane's, which may ask for a plain
37
- * credential for a pane it opens, never a workspace monitor's, and only from the mirror its transport's owner runs, and
38
- * gets none for a pane it relaunches in place.
39
+ * Mark a command that a pane sent through the pane relay, from the connection that announced ANNOUNCED_PANE_ID (null:
40
+ * one that named no pane). The mark is a hidden property, so it never reaches the renderer or changes the command's
41
+ * shape; it tells the transport the command is a pane's, which may ask for a plain credential for a pane it opens,
42
+ * never a workspace monitor's, and only from the mirror its transport's owner runs, and gets none for a pane it
43
+ * relaunches in place. The credential of a pane it opens records the announced pane as the pane that opened it
44
+ * (PaneCredentialStore.openerOf).
39
45
  */
40
- export declare function markRelayed<T extends object>(command: T): T;
46
+ export declare function markRelayed<T extends object>(command: T, announcedPaneId?: number | null): T;
41
47
  export declare class PaneCredentialBroker {
42
48
  private readonly inheritedMirrorPort;
43
49
  /** Credentials waiting for pane_created, by the request id the broker sent their create_pane or replace_pane under. */
@@ -97,6 +103,15 @@ export declare class PaneCredentialBroker {
97
103
  */
98
104
  created(paneId: number, requestId?: string): string | undefined;
99
105
  failed(requestId?: string): string | undefined;
106
+ /**
107
+ * The renderer starts pane PANE_ID's program again in place with its own command line and environment, and reports no
108
+ * new pane (the desktop app's restart_pane; zmux's reports one, and created() revokes the credential then). A
109
+ * credential the program took from a one-time file can't be taken again, so it stops working now: the program that
110
+ * starts again holds none, and the mirror counts the pane as no ZTC that joins its bus. One in the pane's environment
111
+ * stays, since the program starts again with it, and so does a ZTC pane's of the renderer's own (pane type ztc), which
112
+ * the desktop app restarts only through its owner (replace_pane).
113
+ */
114
+ restarted(paneId: number): void;
100
115
  /** The pane closed: its credential stops working, and a handoff its program never took is removed. */
101
116
  closed(paneId: number): void;
102
117
  /** The renderer is gone: every credential it was given stops working. */
@@ -20,9 +20,13 @@
20
20
  * place (replace_pane, through prepareReplace) gets a fresh credential the same way, in a one-time file, since
21
21
  * replace_pane carries no environment either. A closed pane's credential is revoked, and so is the credential of a pane
22
22
  * the renderer relaunches in place without one: zmux's restart_pane, which starts the program with the renderer's
23
- * environment alone, or a replace a pane asked for through the relay. (The desktop app's restart keeps the pane's own
24
- * environment and command line and reports no new pane, so a pane's credential stays bound; a program that took its
25
- * credential from a file, which it deleted, starts again without it.)
23
+ * environment alone, or a replace a pane asked for through the relay. The desktop app's restart keeps the pane's own
24
+ * environment and command line and reports no new pane: a credential the program took from a file, which it deleted,
25
+ * is revoked as the restart is sent (restarted), since the program that starts again can't take it, and one in the
26
+ * pane's environment stays bound, as the program starts again with it. The credential of a pane a pane opens through the
27
+ * relay records the pane whose relay connection asked for it (markRelayed), and only that credential does: one its owner
28
+ * gives the pane when it starts it again in place records none. The mirror reads it when a pane's credential asks to
29
+ * supervise a pane (PaneCredentialStore.openerOf).
26
30
  */
27
31
  import { randomUUID } from 'node:crypto';
28
32
  import { OWNER_MIRROR_CREDENTIAL_ENV, PANE_CREDENTIAL_ENV, PANE_CREDENTIAL_FILE_FLAG, WORKSPACE_MONITOR_CREDENTIAL_REQUEST, ZTC_PANE_CREDENTIAL_REQUEST, paneCredentialStoreForPort, removePaneCredentialHandoff, writePaneCredentialHandoff, } from '../../web/pane_credentials.js';
@@ -30,21 +34,36 @@ import { OWNER_MIRROR_CREDENTIAL_ENV, PANE_CREDENTIAL_ENV, PANE_CREDENTIAL_FILE_
30
34
  const PENDING_BIND_MS = 30_000;
31
35
  const RELAYED = Symbol('ztc.paneRelayed');
32
36
  /**
33
- * Mark a command that a pane sent through the pane relay. The mark is a hidden property, so it never reaches the
34
- * renderer or changes the command's shape; it tells the transport the command is a pane's, which may ask for a plain
35
- * credential for a pane it opens, never a workspace monitor's, and only from the mirror its transport's owner runs, and
36
- * gets none for a pane it relaunches in place.
37
+ * Mark a command that a pane sent through the pane relay, from the connection that announced ANNOUNCED_PANE_ID (null:
38
+ * one that named no pane). The mark is a hidden property, so it never reaches the renderer or changes the command's
39
+ * shape; it tells the transport the command is a pane's, which may ask for a plain credential for a pane it opens,
40
+ * never a workspace monitor's, and only from the mirror its transport's owner runs, and gets none for a pane it
41
+ * relaunches in place. The credential of a pane it opens records the announced pane as the pane that opened it
42
+ * (PaneCredentialStore.openerOf).
37
43
  */
38
- export function markRelayed(command) {
39
- Object.defineProperty(command, RELAYED, { value: true });
44
+ export function markRelayed(command, announcedPaneId = null) {
45
+ const mark = { announcedPaneId };
46
+ Object.defineProperty(command, RELAYED, { value: mark });
40
47
  return command;
41
48
  }
49
+ function relayedMark(command) {
50
+ return command[RELAYED];
51
+ }
42
52
  function isRelayed(command) {
43
- return command[RELAYED] === true;
53
+ return relayedMark(command) !== undefined;
44
54
  }
45
55
  function isZtcPane(paneType) {
46
56
  return paneType === 'ztc' || paneType === 'colony_ztc';
47
57
  }
58
+ /**
59
+ * Whether the ZTC a pane of PANE_TYPE runs with ARGS joins its mirror's bus as it starts. A colony cell does, and so does
60
+ * a ZTC in pane mode (--pane), which every renderer with a terminal starts a ZTC pane in, behind MotSi's or the desktop
61
+ * launcher's own arguments too. The browser mirror's own ZTC chat panes run without it, as headless pane children that
62
+ * read their input from stdin and join no bus: the mirror delivers to them there, and holds nothing for them.
63
+ */
64
+ function joinsBus(paneType, args) {
65
+ return paneType === 'colony_ztc' || (args ?? []).includes('--pane');
66
+ }
48
67
  /**
49
68
  * ARGS with the flag naming HANDOFF among the pane program's options. A ZTC pane opened with a task (New pane or New
50
69
  * session, an isolated ZTC, the workspace tools' ZTC pane) ends its command line with its end of options ('--') and the
@@ -118,7 +137,9 @@ export class PaneCredentialBroker {
118
137
  const store = paneCredentialStoreForPort(port);
119
138
  if (!store)
120
139
  return { command: { ...cmd, env } };
121
- const credential = store.issue(scope, paneId);
140
+ // A pane a pane opens through the relay is that pane's: the pane its relay connection announced opened it. The mirror
141
+ // knows a ZTC that joins its bus by its credential, whatever its pane type, and holds a message for it until it joins.
142
+ const credential = store.issue(scope, paneId, relayedMark(cmd)?.announcedPaneId ?? null, { joinsBus: ztc && joinsBus(paneType, cmd.args) });
122
143
  let handoff;
123
144
  if (scope === 'workspace-monitor' || ztc) {
124
145
  // Never in its environment, which any program running as this user can read: in a file only it is told of.
@@ -135,7 +156,7 @@ export class PaneCredentialBroker {
135
156
  }
136
157
  // The pane talks to the mirror that issued its credential.
137
158
  env.ZTC_MIRROR_PORT = String(port);
138
- const held = { credential, handoff };
159
+ const held = { credential, handoff, paneType };
139
160
  const args = handoff ? withHandoff(cmd.args ?? [], handoff) : cmd.args;
140
161
  const command = { ...cmd, env, ...(args ? { args } : {}) };
141
162
  if (paneId !== undefined) {
@@ -162,7 +183,7 @@ export class PaneCredentialBroker {
162
183
  const store = paneCredentialStoreForPort(port);
163
184
  if (!store)
164
185
  return cmd;
165
- const credential = store.issue(status ? 'workspace-monitor' : 'pane');
186
+ const credential = store.issue(status ? 'workspace-monitor' : 'pane', undefined, null, { joinsBus: isZtcPane(cmd.pane_type) && joinsBus(cmd.pane_type, cmd.args) });
166
187
  let handoff;
167
188
  try {
168
189
  handoff = writePaneCredentialHandoff(credential.token);
@@ -171,7 +192,7 @@ export class PaneCredentialBroker {
171
192
  credential.revoke();
172
193
  return cmd;
173
194
  }
174
- const requestId = this.await({ held: { credential, handoff }, requestId: cmd.request_id });
195
+ const requestId = this.await({ held: { credential, handoff, paneType: cmd.pane_type }, requestId: cmd.request_id });
175
196
  return { ...cmd, request_id: requestId, args: withHandoff(cmd.args ?? [], handoff) };
176
197
  }
177
198
  /**
@@ -210,6 +231,20 @@ export class PaneCredentialBroker {
210
231
  release(waiting.held);
211
232
  return waiting.requestId;
212
233
  }
234
+ /**
235
+ * The renderer starts pane PANE_ID's program again in place with its own command line and environment, and reports no
236
+ * new pane (the desktop app's restart_pane; zmux's reports one, and created() revokes the credential then). A
237
+ * credential the program took from a one-time file can't be taken again, so it stops working now: the program that
238
+ * starts again holds none, and the mirror counts the pane as no ZTC that joins its bus. One in the pane's environment
239
+ * stays, since the program starts again with it, and so does a ZTC pane's of the renderer's own (pane type ztc), which
240
+ * the desktop app restarts only through its owner (replace_pane).
241
+ */
242
+ restarted(paneId) {
243
+ const held = this.bound.get(paneId);
244
+ if (!held?.handoff || held.paneType === 'ztc')
245
+ return;
246
+ this.closed(paneId);
247
+ }
213
248
  /** The pane closed: its credential stops working, and a handoff its program never took is removed. */
214
249
  closed(paneId) {
215
250
  const held = this.bound.get(paneId);
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * Parent ZTC (--mux) runs a PaneRelayServer on a Unix socket.
5
5
  * Child ZTC (--pane) connects via PaneRelayClient.
6
- * Commands flow: child → relay → parent TuiProcess → zmux
6
+ * Commands flow: child → relay → parent TuiProcess → zmux, only those relay_policy.ts lets a child send
7
7
  * Events flow: zmux → parent TuiProcess → relay → all children
8
8
  *
9
9
  * Both TuiProcess and PaneRelayClient implement PaneCommandTransport,
@@ -45,7 +45,10 @@ export declare class PaneRelayServer {
45
45
  private readonly childCommandListeners;
46
46
  private readonly agentCliOf?;
47
47
  constructor(transport: PaneCommandTransport, socketPath: string, options?: PaneRelayServerOptions);
48
- /** EVENT as children get it: a pane list says which panes run an agent CLI (see agentCliOf). */
48
+ /**
49
+ * EVENT as children get it: a pane list says which panes run an agent CLI (see agentCliOf), and a pane's text the
50
+ * main ZTC refuses to have read now (screen_privacy.ts), a read already on its way, comes without its content.
51
+ */
49
52
  private forChildren;
50
53
  /** Start listening for child connections */
51
54
  start(): Promise<void>;
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * Parent ZTC (--mux) runs a PaneRelayServer on a Unix socket.
5
5
  * Child ZTC (--pane) connects via PaneRelayClient.
6
- * Commands flow: child → relay → parent TuiProcess → zmux
6
+ * Commands flow: child → relay → parent TuiProcess → zmux, only those relay_policy.ts lets a child send
7
7
  * Events flow: zmux → parent TuiProcess → relay → all children
8
8
  *
9
9
  * Both TuiProcess and PaneRelayClient implement PaneCommandTransport,
@@ -15,6 +15,8 @@ import { serializeCommand, parseEvent, CAPABILITY_PANE_DELIVERY_ACK, CAPABILITY_
15
15
  import { runtimeDiag } from '../../utils/debug_log.js';
16
16
  import { generateIpcPath, removeOwnedIpcPath } from './ipc_path.js';
17
17
  import { markRelayed } from './pane_credential_broker.js';
18
+ import { relayVerdict } from './relay_policy.js';
19
+ import { paneReadRefusal } from './screen_privacy.js';
18
20
  // ============================================================================
19
21
  // PaneRelayServer — runs in parent ZTC (--mux mode)
20
22
  // ============================================================================
@@ -35,6 +37,18 @@ const PANE_EVENT_TYPES = new Set([
35
37
  function isPaneId(value) {
36
38
  return Number.isInteger(value) && value >= 0 && value <= 0xffff_ffff;
37
39
  }
40
+ /** The relays listening in this process, which stop as it exits (stopListeningRelaysAtExit). */
41
+ const listeningRelays = new Set();
42
+ let stopsRelaysAtExit = false;
43
+ /**
44
+ * Stop each relay still listening as the process exits, so its socket, and the private directory that holds it where it
45
+ * has one (generateIpcPath), go with it: an exit runs no close callback, and a ZTC that exits without its exit path
46
+ * stopping the relay left both in /tmp.
47
+ */
48
+ function stopListeningRelaysAtExit() {
49
+ for (const relay of [...listeningRelays])
50
+ relay.stop();
51
+ }
38
52
  export class PaneRelayServer {
39
53
  transport;
40
54
  server = null;
@@ -48,8 +62,19 @@ export class PaneRelayServer {
48
62
  this.socketPath = socketPath;
49
63
  this.agentCliOf = options.agentCliOf;
50
64
  }
51
- /** EVENT as children get it: a pane list says which panes run an agent CLI (see agentCliOf). */
65
+ /**
66
+ * EVENT as children get it: a pane list says which panes run an agent CLI (see agentCliOf), and a pane's text the
67
+ * main ZTC refuses to have read now (screen_privacy.ts), a read already on its way, comes without its content.
68
+ */
52
69
  forChildren(event) {
70
+ if (event.type === 'pane_read' || event.type === 'pane_output') {
71
+ const refused = paneReadRefusal(event.pane_id);
72
+ if (refused === null)
73
+ return event;
74
+ if (event.type === 'pane_output')
75
+ return null;
76
+ return { ...event, content: '', error: refused };
77
+ }
53
78
  const agentCliOf = this.agentCliOf;
54
79
  if (event.type !== 'pane_list' || !agentCliOf)
55
80
  return event;
@@ -65,7 +90,9 @@ export class PaneRelayServer {
65
90
  // Subscribe to pane events from the parent's TuiProcess
66
91
  this.tuiUnsubscribe = this.transport.on((event) => {
67
92
  if (PANE_EVENT_TYPES.has(event.type)) {
68
- this.broadcast(this.forChildren(event));
93
+ const passed = this.forChildren(event);
94
+ if (passed)
95
+ this.broadcast(passed);
69
96
  }
70
97
  });
71
98
  this.server = createServer((client) => {
@@ -88,11 +115,17 @@ export class PaneRelayServer {
88
115
  client.destroy();
89
116
  });
90
117
  rl.on('line', (line) => {
91
- const parsed = parseEvent(line);
92
- if (!parsed)
118
+ if (line.trim() === '')
93
119
  return;
120
+ let parsed;
121
+ try {
122
+ parsed = JSON.parse(line);
123
+ }
124
+ catch {
125
+ parsed = undefined;
126
+ }
94
127
  const hello = parsed;
95
- if (hello.type === 'relay_hello') {
128
+ if (hello && typeof hello === 'object' && hello.type === 'relay_hello') {
96
129
  if (!named) {
97
130
  named = true;
98
131
  if (isPaneId(hello.pane_id))
@@ -100,10 +133,35 @@ export class PaneRelayServer {
100
133
  }
101
134
  return;
102
135
  }
103
- // Treat incoming lines as commands (same JSON shape as RustCommand)
104
- // Forward to parent's TuiProcess, marked as a pane's: a pane it opens gets a plain credential of its own,
105
- // never a workspace monitor's, and a pane it relaunches in place gets none (PaneCredentialBroker).
106
- const command = markRelayed(parsed);
136
+ // Only what a child may send (relay_policy.ts) is forwarded; anything else is answered with an error, to this
137
+ // child alone.
138
+ const verdict = relayVerdict(parsed);
139
+ if (!verdict.forward) {
140
+ try {
141
+ client.write(JSON.stringify(verdict.error) + '\n');
142
+ }
143
+ catch { /* the close handler retires this client */ }
144
+ return;
145
+ }
146
+ // A pane the main ZTC won't have read now (its screen shows the person's sessions picker) is answered here, to
147
+ // this child alone, as the read it asked for, so read_terminal_pane says why; the renderer is never asked.
148
+ if (verdict.command.type === 'read_pane') {
149
+ const read = verdict.command;
150
+ const refused = paneReadRefusal(read.pane_id);
151
+ if (refused !== null) {
152
+ const answer = { type: 'pane_read', pane_id: read.pane_id, content: '', error: refused,
153
+ ...(read.request_id ? { request_id: read.request_id } : {}), ...(read.viewport ? { viewport: true } : {}) };
154
+ try {
155
+ client.write(JSON.stringify(answer) + '\n');
156
+ }
157
+ catch { /* the close handler retires this client */ }
158
+ return;
159
+ }
160
+ }
161
+ // Forward to parent's TuiProcess, marked as a pane's, the one this connection announced: a pane it opens gets a
162
+ // plain credential of its own, never a workspace monitor's, which records that pane as the one that opened it,
163
+ // and a pane it relaunches in place gets none (PaneCredentialBroker).
164
+ const command = markRelayed(verdict.command, announced);
107
165
  this.transport.send(command);
108
166
  for (const listener of [...this.childCommandListeners]) {
109
167
  try {
@@ -133,6 +191,11 @@ export class PaneRelayServer {
133
191
  runtimeDiag('error', `Pane relay stopped: ${error.message}`);
134
192
  this.stop();
135
193
  });
194
+ listeningRelays.add(this);
195
+ if (!stopsRelaysAtExit) {
196
+ stopsRelaysAtExit = true;
197
+ process.on('exit', stopListeningRelaysAtExit);
198
+ }
136
199
  resolve();
137
200
  });
138
201
  server.listen(this.socketPath);
@@ -169,6 +232,7 @@ export class PaneRelayServer {
169
232
  }
170
233
  /** Stop the relay server and clean up */
171
234
  stop() {
235
+ listeningRelays.delete(this);
172
236
  if (this.tuiUnsubscribe) {
173
237
  this.tuiUnsubscribe();
174
238
  this.tuiUnsubscribe = null;
@@ -181,18 +245,21 @@ export class PaneRelayServer {
181
245
  }
182
246
  this.clients.clear();
183
247
  if (this.server) {
184
- this.server.close(() => {
185
- try {
186
- removeOwnedIpcPath(this.socketPath);
187
- }
188
- catch (error) {
189
- runtimeDiag('error', `Pane relay IPC cleanup failed: ${String(error)}`);
190
- }
191
- });
248
+ const server = this.server;
192
249
  this.server = null;
250
+ // Closing a listening server removes its socket at once, so its private directory goes too, before this returns:
251
+ // not once every connection has closed, by which time a ZTC that stops its relay as it exits has exited.
252
+ server.close();
253
+ try {
254
+ removeOwnedIpcPath(this.socketPath);
255
+ }
256
+ catch (error) {
257
+ runtimeDiag('error', `Pane relay IPC cleanup failed: ${String(error)}`);
258
+ }
193
259
  }
194
260
  // Node closes its own bound endpoint. A failed bind never owns a path
195
- // and must not unlink a file belonging to an existing listener.
261
+ // and must not unlink a file belonging to an existing listener: the
262
+ // directory that holds it isn't empty, and stays.
196
263
  }
197
264
  }
198
265
  // ============================================================================
@@ -948,6 +948,9 @@ export class TuiProcess {
948
948
  }
949
949
  else {
950
950
  this.socket.write(json + '\n');
951
+ // The pane's program starts again with its command line, and can't take a credential it took from a file.
952
+ if (outgoing.type === 'restart_pane')
953
+ this.paneCredentials.restarted(outgoing.pane_id);
951
954
  }
952
955
  return true;
953
956
  }