@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
@@ -41,6 +41,8 @@ export interface OpenAgentPaneOptions {
41
41
  profile?: string | null;
42
42
  /** Home directory the profiles live under (default: the user's). */
43
43
  profilesHome?: string;
44
+ /** The id a fresh Claude Code session is given (default: a random UUID); see choosesClaudeSession. */
45
+ newSessionId?: () => string;
44
46
  /**
45
47
  * Who asked for the pane. The person: their own /claude or /codex, ZTC's menus and splits, Ctrl+B c. An agent: the
46
48
  * ztc_command tool running /claude or /codex, or workspace-mcp's open_pane. Only a pane the person opens starts
@@ -70,16 +72,88 @@ interface PaneMessage {
70
72
  };
71
73
  text: string;
72
74
  }
75
+ /** The longest answer of Main's typed into an agent pane whole, in characters; a longer one is cut, saying so. */
76
+ export declare const AGENT_PANE_ANSWER_MAX_CHARS = 8000;
77
+ /**
78
+ * Why a turn on an agent pane's message isn't a finished answer though it wrote one: it stopped at the configured
79
+ * iteration limit, or ZTC flagged its answer as a claim the turn's tools don't show (it ran none, or they showed
80
+ * errors).
81
+ */
82
+ export type AgentPaneUnfinishedReason = 'iteration_limit' | 'claimed_without_tools' | 'claimed_despite_errors';
83
+ /**
84
+ * Why the work on an agent pane's message ended with no answer to come: the person closed Main's question without
85
+ * answering it, cancelled its plan or took the plan to edit themselves, with nothing going on from that; cleared or
86
+ * rewound Main's conversation past the message; or switched Main to another session.
87
+ */
88
+ export type AgentPaneClosedReason = 'question_skipped' | 'plan_cancelled' | 'plan_edit' | 'cleared' | 'session_switched';
89
+ /**
90
+ * What became of the work on an agent pane's message: what Main wrote (TEXT, the answer), that it wrote it but isn't
91
+ * done (REASON), that it was interrupted or failed (ERROR, why), that it stopped to wait for the person (ON: their
92
+ * answer to a question, their approval of a plan, or their continuing a session paused for a workspace overlap), or
93
+ * that it ended with no answer to come (WHY). Main sends it to that pane when the turn ends (agentPaneAnswerText).
94
+ * EXCHANGE_NOTE ends an answer that is the last its exchange's reply mode allows, saying so (panes/reply_modes.ts); the
95
+ * STOPPED line, NOTE, says that an arbitrator stopped the exchange instead of the answer it would have been.
96
+ */
97
+ export type AgentPaneAnswer = {
98
+ outcome: 'answered';
99
+ text: string;
100
+ read?: AgentPaneAnswerRead;
101
+ exchangeNote?: string;
102
+ } | {
103
+ outcome: 'unfinished';
104
+ reason: AgentPaneUnfinishedReason;
105
+ text: string;
106
+ read?: AgentPaneAnswerRead;
107
+ exchangeNote?: string;
108
+ } | {
109
+ outcome: 'interrupted';
110
+ exchangeNote?: string;
111
+ } | {
112
+ outcome: 'failed';
113
+ error: string;
114
+ exchangeNote?: string;
115
+ } | {
116
+ outcome: 'waiting';
117
+ on: 'question' | 'plan' | 'workspace';
118
+ } | {
119
+ outcome: 'closed';
120
+ why: AgentPaneClosedReason;
121
+ } | {
122
+ outcome: 'stopped';
123
+ note: string;
124
+ };
125
+ /**
126
+ * How read_pane reads a long answer whole in the pane that wrote it: with LINES rows (from the message that asked to
127
+ * the end of the pane), once the person's reading cap, if it is still revealing the answer, has shown the rest, which
128
+ * takes about REVEAL_SECONDS.
129
+ */
130
+ export interface AgentPaneAnswerRead {
131
+ lines: number;
132
+ revealSeconds?: number;
133
+ }
134
+ /** The bus message text that carries ANSWER, which formatAgentPaneMessage types in as Main's answer. */
135
+ export declare function agentPaneAnswerText(answer: AgentPaneAnswer): string;
136
+ /**
137
+ * What Main's model reads right after a message an agent pane (Claude Code, Codex) sent it, which the transcript leaves
138
+ * out: who sent it, that Main's answer, the text the model writes in the turn, goes back to that pane when the turn
139
+ * ends, and, when the model's send_message reaches that pane at all (HELD_SENDS: not a colony cell's, which reaches the
140
+ * colony), that a message the model sends the pane during the turn follows the answer.
141
+ */
142
+ export declare function agentPaneNoteForModel(from: PaneMessage['from'], heldSends?: boolean): string;
73
143
  /**
74
144
  * A bus message as it is typed into an agent pane: one line, saying who sent
75
- * it and, for a question or request, how to answer.
145
+ * it and, for a question or request, how to answer; Main's answer at the end
146
+ * of a turn (agentPaneAnswerText) says it is one.
76
147
  */
77
148
  export declare function formatAgentPaneMessage(message: PaneMessage): string;
78
149
  /** Delay between typed text and its Enter for programs that read a burst of input as a paste. */
79
150
  export declare const TYPED_ENTER_DELAY_MS = 120;
151
+ /** Resolves once nothing is being typed into a pane through TRANSPORT (typeLineIntoPane), or after MAX_MS. */
152
+ export declare function typingSettled(transport: PaneCommandTransport, maxMs: number): Promise<void>;
80
153
  /**
81
- * Type a line into a program that is not ZTC (an agent CLI or a shell), then
82
- * press Enter as a separate keystroke.
154
+ * Type a line into a program that is not ZTC (an agent CLI or a shell), then press Enter as a separate keystroke. A
155
+ * line for a pane that is still being typed into waits its turn, so each arrives whole with its own Enter, in order,
156
+ * the next no sooner than TYPED_ENTER_DELAY_MS after the Enter before it; it counts as typed.
83
157
  */
84
158
  export declare function typeLineIntoPane(transport: PaneCommandTransport, paneId: number, line: string): boolean;
85
159
  export {};
@@ -9,10 +9,10 @@
9
9
  import { randomUUID } from 'node:crypto';
10
10
  import { listWorkspacePanes, waitForPaneCreatedOrError } from '../agent/tools/pane.js';
11
11
  import { CAPABILITY_ATTENTION_BADGES, CAPABILITY_SYNCHRONIZED_OUTPUT } from '../ui/ratatui/protocol.js';
12
- import { AGENT_CLIS, agentCliArgs, agentPaneEnv, agentPermissionsSetting, bypassRefusal, bypassWord, choosesPermissions, findAgentCli, isAgentCliId, loginShellExec, nextPaneTitle, permissionDefaultArgs, workspaceMcpProgram, } from './agent_cli.js';
12
+ import { AGENT_CLIS, agentCliArgs, agentPaneEnv, agentPermissionsSetting, bypassRefusal, bypassWord, choosesClaudeSession, choosesPermissions, findAgentCli, isAgentCliId, loginShellExec, nextPaneTitle, permissionDefaultArgs, workspaceMcpProgram, } from './agent_cli.js';
13
13
  import { sessionNamePaneTitle } from './labels.js';
14
14
  import { MOTSI_PANE_ENV, remoteViewMotsi, throughMotsi } from './motsi.js';
15
- import { agentProfileClearedEnv, agentProfileEnv, ensureAgentProfile } from './agent_profiles.js';
15
+ import { agentProfileArgs, agentProfileClearedEnv, agentProfileEnv, ensureAgentProfile } from './agent_profiles.js';
16
16
  import { badgedNotificationArgs } from './claude_notifications.js';
17
17
  import { configStore } from '../config.js';
18
18
  /** Request ids of agent pane launches start with this, so the overseer can tell them apart. */
@@ -29,6 +29,17 @@ async function existingTitles(transport) {
29
29
  const panes = await listWorkspacePanes(transport, 3000);
30
30
  return panes instanceof Error ? [] : panes.map(pane => pane.labels?.manual_title || pane.title);
31
31
  }
32
+ /**
33
+ * A fresh Claude Code session ZTC starts is given its id (--session-id, after the workspace server and ahead of any
34
+ * prompt), so which session the pane holds is read off its command line (agent_pane_sessions.ts) and the picker goes
35
+ * to it rather than opening it twice. Words that choose a session themselves get none, nor does Codex, which takes no
36
+ * id for a new session.
37
+ */
38
+ function newSessionArgs(options) {
39
+ if (options.cli !== 'claude' || choosesClaudeSession(options.request?.words ?? []))
40
+ return [];
41
+ return ['--session-id', (options.newSessionId ?? randomUUID)()];
42
+ }
32
43
  /** Open the user's own `claude` or `codex` in a new pane, attached to this workspace. */
33
44
  export async function openAgentPane(transport, options) {
34
45
  const spec = AGENT_CLIS[options.cli];
@@ -87,9 +98,9 @@ export async function openAgentPane(transport, options) {
87
98
  // prompts unless their own arguments choose otherwise, ZTC's setting says ask, or the CLI would refuse the flag.
88
99
  ...permissionArgs,
89
100
  // On a profile, Codex keeps the sign-in in the profile's files, never a keyring entry other terminals share.
90
- ...(profile && options.cli === 'codex' ? ['-c', 'cli_auth_credentials_store="file"'] : []),
101
+ ...(profile ? agentProfileArgs(options.cli) : []),
91
102
  ...notifyArgs,
92
- ...agentCliArgs(options.cli, options.request ?? { words: [] }, attached),
103
+ ...agentCliArgs(options.cli, options.request ?? { words: [] }, attached, newSessionArgs(options)),
93
104
  ];
94
105
  const motsi = options.motsi === undefined ? remoteViewMotsi(configStore.get().panes, { env }) : options.motsi;
95
106
  // On a profile, Claude Code's sign-in overrides are cleared in the pane's
@@ -148,11 +159,154 @@ export async function openAgentPane(transport, options) {
148
159
  result.notice = notices.join(' ');
149
160
  return result;
150
161
  }
162
+ /** The longest answer of Main's typed into an agent pane whole, in characters; a longer one is cut, saying so. */
163
+ export const AGENT_PANE_ANSWER_MAX_CHARS = 8000;
164
+ /** read_pane reads at most this many rows of a pane. */
165
+ const READ_PANE_MAX_LINES = 2000;
166
+ /** READ as it came in an answer, when it is one. */
167
+ function parseAnswerRead(read) {
168
+ if (!read || typeof read !== 'object')
169
+ return undefined;
170
+ const { lines, revealSeconds } = read;
171
+ if (typeof lines !== 'number' || !Number.isSafeInteger(lines) || lines < 1)
172
+ return undefined;
173
+ const reveal = typeof revealSeconds === 'number' && Number.isSafeInteger(revealSeconds) && revealSeconds >= 1 ? revealSeconds : undefined;
174
+ return reveal === undefined ? { lines } : { lines, revealSeconds: reveal };
175
+ }
176
+ const UNFINISHED_REASONS = ['iteration_limit', 'claimed_without_tools', 'claimed_despite_errors'];
177
+ const CLOSED_REASONS = ['question_skipped', 'plan_cancelled', 'plan_edit', 'cleared', 'session_switched'];
178
+ /** The bus message text that carries ANSWER, which formatAgentPaneMessage types in as Main's answer. */
179
+ export function agentPaneAnswerText(answer) {
180
+ return JSON.stringify({ __ztc_answer: true, ...answer });
181
+ }
182
+ /** The answer ANSWER_TEXT carries, when it is one; only Main (bus member 0) answers this way. */
183
+ function parseAgentPaneAnswer(from, text) {
184
+ if (from.id !== 0 || !text.startsWith('{"__ztc_answer":true'))
185
+ return null;
186
+ try {
187
+ const parsed = JSON.parse(text);
188
+ const read = parseAnswerRead(parsed.read);
189
+ const note = typeof parsed.exchangeNote === 'string' && parsed.exchangeNote.trim() ? { exchangeNote: parsed.exchangeNote } : {};
190
+ if (parsed.outcome === 'answered' && typeof parsed.text === 'string') {
191
+ return { outcome: 'answered', text: parsed.text, ...(read ? { read } : {}), ...note };
192
+ }
193
+ const reason = UNFINISHED_REASONS.find(candidate => candidate === parsed.reason);
194
+ if (parsed.outcome === 'unfinished' && reason && typeof parsed.text === 'string') {
195
+ return { outcome: 'unfinished', reason, text: parsed.text, ...(read ? { read } : {}), ...note };
196
+ }
197
+ if (parsed.outcome === 'interrupted')
198
+ return { outcome: 'interrupted', ...note };
199
+ if (parsed.outcome === 'failed' && typeof parsed.error === 'string')
200
+ return { outcome: 'failed', error: parsed.error, ...note };
201
+ if (parsed.outcome === 'stopped' && typeof parsed.note === 'string' && parsed.note.trim())
202
+ return { outcome: 'stopped', note: parsed.note };
203
+ if (parsed.outcome === 'waiting' && (parsed.on === 'question' || parsed.on === 'plan' || parsed.on === 'workspace')) {
204
+ return { outcome: 'waiting', on: parsed.on };
205
+ }
206
+ const why = CLOSED_REASONS.find(candidate => candidate === parsed.why);
207
+ if (parsed.outcome === 'closed' && why)
208
+ return { outcome: 'closed', why };
209
+ }
210
+ catch {
211
+ // Not an answer.
212
+ }
213
+ return null;
214
+ }
215
+ /**
216
+ * TEXT as one typed line: its line ends, and the spaces around them, one space, a tab a space, and no other control
217
+ * character (C0, DEL or C1), which the CLI typed into would read as a key: an ESC sequence or Ctrl+C an answer quoted.
218
+ */
219
+ function asOneLine(text) {
220
+ return text.replace(/\s*[\r\n]+\s*/g, ' ').replace(/\t/g, ' ').replace(/[\u0000-\u001f\u007f-\u009f]/g, '').trim();
221
+ }
222
+ /** What Main's answer says, cut to AGENT_PANE_ANSWER_MAX_CHARS characters, never inside one, saying what was left out. */
223
+ function answerLine(from, answer) {
224
+ const readPane = `read_pane ${from.id}`;
225
+ if (answer.outcome === 'interrupted') {
226
+ return `${from.name}'s turn on your message was interrupted before it finished; ${readPane} shows how far it got.`;
227
+ }
228
+ if (answer.outcome === 'failed')
229
+ return `${from.name}'s turn on your message failed (${asOneLine(answer.error)}); ${readPane} shows how far it got.`;
230
+ if (answer.outcome === 'waiting') {
231
+ const waitsFor = answer.on === 'plan' ? `approve its plan in pane ${from.id}`
232
+ : answer.on === 'workspace' ? `continue its session in pane ${from.id}, paused for a workspace overlap`
233
+ : `answer its question in pane ${from.id}`;
234
+ return `${from.name} stopped to wait for the person to ${waitsFor}; its answer to your message comes when it goes on.`;
235
+ }
236
+ if (answer.outcome === 'closed') {
237
+ if (answer.why === 'cleared') {
238
+ return `The person cleared ${from.name}'s conversation, or rewound it, past your message, so ${from.name} won't answer it; `
239
+ + 'send it again if you still need an answer.';
240
+ }
241
+ if (answer.why === 'session_switched') {
242
+ return `The person switched ${from.name} to another session, so ${from.name} won't answer your message; send it again if you `
243
+ + 'still need an answer.';
244
+ }
245
+ const what = answer.why === 'question_skipped' ? `The person closed ${from.name}'s question without answering it`
246
+ : answer.why === 'plan_cancelled' ? `The person cancelled ${from.name}'s plan` : `The person took ${from.name}'s plan to edit it themselves`;
247
+ return `${what}, so ${from.name} stopped without finishing your message and is idle; send it again to have ${from.name} go on.`;
248
+ }
249
+ const written = cutAnswer(asOneLine(answer.text), from.id, answer.read);
250
+ if (answer.outcome === 'unfinished') {
251
+ const what = written === '' ? `It wrote nothing; ${readPane} shows what it did.` : `What it wrote: ${written}`;
252
+ if (answer.reason === 'iteration_limit') {
253
+ return `${from.name}'s turn on your message reached its iteration limit, so the task is unfinished; send ${from.name} a message `
254
+ + `to have it continue. ${what}`;
255
+ }
256
+ return answer.reason === 'claimed_without_tools'
257
+ ? `ZTC flagged ${from.name}'s answer: it says your task is done, but ${from.name} ran no tools, so it likely wasn't done; `
258
+ + `check before relying on it. ${what}`
259
+ : `ZTC flagged ${from.name}'s answer: it reports success, but its own tools showed errors; check the result before relying on `
260
+ + `it. ${what}`;
261
+ }
262
+ if (written === '')
263
+ return `${from.name} finished its turn on your message without writing an answer; ${readPane} shows what it did.`;
264
+ return written;
265
+ }
266
+ /**
267
+ * LINE, an answer from pane PANE_ID, cut to AGENT_PANE_ANSWER_MAX_CHARS characters, never inside one, saying what was
268
+ * left out and the read_pane that shows it (READ): with enough lines for all of it, once the person's reading cap has
269
+ * shown the rest.
270
+ */
271
+ function cutAnswer(line, paneId, read) {
272
+ const characters = Array.from(line);
273
+ if (characters.length <= AGENT_PANE_ANSWER_MAX_CHARS)
274
+ return line;
275
+ const left = characters.length - AGENT_PANE_ANSWER_MAX_CHARS;
276
+ const readPane = `read_pane ${paneId}`;
277
+ const shows = !read ? `${readPane} shows the whole answer`
278
+ : read.lines > READ_PANE_MAX_LINES ? `${readPane} with lines ${READ_PANE_MAX_LINES} shows the last ${READ_PANE_MAX_LINES} rows of it`
279
+ : `${readPane} with lines ${read.lines} shows the whole answer`;
280
+ const where = read?.revealSeconds === undefined ? shows
281
+ : `pane ${paneId} is still showing the rest at the person's reading pace, for about ${read.revealSeconds} s more; then ${shows}`;
282
+ return `${characters.slice(0, AGENT_PANE_ANSWER_MAX_CHARS).join('')} [… ${left} more character${left === 1 ? '' : 's'}: ${where}]`;
283
+ }
284
+ /**
285
+ * What Main's model reads right after a message an agent pane (Claude Code, Codex) sent it, which the transcript leaves
286
+ * out: who sent it, that Main's answer, the text the model writes in the turn, goes back to that pane when the turn
287
+ * ends, and, when the model's send_message reaches that pane at all (HELD_SENDS: not a colony cell's, which reaches the
288
+ * colony), that a message the model sends the pane during the turn follows the answer.
289
+ */
290
+ export function agentPaneNoteForModel(from, heldSends = true) {
291
+ const who = `${from.name} (pane ${from.id})`;
292
+ return `The message above is from ${who}, an agent in another pane. When this turn ends, ZTC sends ${who} your answer: `
293
+ + 'the text you write in this turn. You don\'t need send_message for it.'
294
+ + (heldSends ? ` A message you send ${who} during this turn reaches it right after your answer.` : '');
295
+ }
151
296
  /**
152
297
  * A bus message as it is typed into an agent pane: one line, saying who sent
153
- * it and, for a question or request, how to answer.
298
+ * it and, for a question or request, how to answer; Main's answer at the end
299
+ * of a turn (agentPaneAnswerText) says it is one.
154
300
  */
155
301
  export function formatAgentPaneMessage(message) {
302
+ const who = `${message.from.name} (pane ${message.from.id})`;
303
+ const answer = parseAgentPaneAnswer(message.from, message.text);
304
+ if (answer?.outcome === 'stopped')
305
+ return `[Answer from ${who}] ${asOneLine(answer.note)}`;
306
+ if (answer) {
307
+ const note = 'exchangeNote' in answer && answer.exchangeNote ? ` [${asOneLine(answer.exchangeNote)}]` : '';
308
+ return `[Answer from ${who}] ${answerLine(message.from, answer)}${note}`;
309
+ }
156
310
  let text = message.text;
157
311
  let kind = 'Message';
158
312
  try {
@@ -165,8 +319,7 @@ export function formatAgentPaneMessage(message) {
165
319
  catch {
166
320
  // Plain text.
167
321
  }
168
- const line = text.replace(/\s*[\r\n]+\s*/g, ' ').trim();
169
- const who = `${message.from.name} (pane ${message.from.id})`;
322
+ const line = asOneLine(text);
170
323
  return kind === 'Message'
171
324
  ? `[Message from ${who}] ${line}`
172
325
  : `[${kind} from ${who} — answer with the ztc send_message tool, to: ${message.from.id}] ${line}`;
@@ -174,14 +327,50 @@ export function formatAgentPaneMessage(message) {
174
327
  /** Delay between typed text and its Enter for programs that read a burst of input as a paste. */
175
328
  export const TYPED_ENTER_DELAY_MS = 120;
176
329
  /**
177
- * Type a line into a program that is not ZTC (an agent CLI or a shell), then
178
- * press Enter as a separate keystroke.
330
+ * For each renderer, the panes a line is being typed into, with the lines waiting their turn there. A pane is in it
331
+ * from its line's text until TYPED_ENTER_DELAY_MS after that line's Enter.
179
332
  */
180
- export function typeLineIntoPane(transport, paneId, line) {
333
+ const typingInto = new WeakMap();
334
+ /** Type LINE, then its Enter TYPED_ENTER_DELAY_MS later, then the pane's next waiting line TYPED_ENTER_DELAY_MS after it. */
335
+ function typeInTurn(transport, panes, paneId, line) {
181
336
  if (!transport.send({ type: 'write_pane', pane_id: paneId, input: line, submit: false }))
182
337
  return false;
183
338
  setTimeout(() => {
184
339
  transport.send({ type: 'write_pane', pane_id: paneId, input: '\r', submit: false });
340
+ setTimeout(() => {
341
+ const waiting = panes.get(paneId) ?? [];
342
+ // A line the renderer doesn't take gets no Enter; the next one still has its turn.
343
+ while (waiting.length > 0)
344
+ if (typeInTurn(transport, panes, paneId, waiting.shift()))
345
+ return;
346
+ panes.delete(paneId);
347
+ }, TYPED_ENTER_DELAY_MS);
185
348
  }, TYPED_ENTER_DELAY_MS);
186
349
  return true;
187
350
  }
351
+ /** Resolves once nothing is being typed into a pane through TRANSPORT (typeLineIntoPane), or after MAX_MS. */
352
+ export async function typingSettled(transport, maxMs) {
353
+ const until = Date.now() + maxMs;
354
+ while ((typingInto.get(transport)?.size ?? 0) > 0 && Date.now() < until)
355
+ await new Promise(resolve => setTimeout(resolve, 20));
356
+ }
357
+ /**
358
+ * Type a line into a program that is not ZTC (an agent CLI or a shell), then press Enter as a separate keystroke. A
359
+ * line for a pane that is still being typed into waits its turn, so each arrives whole with its own Enter, in order,
360
+ * the next no sooner than TYPED_ENTER_DELAY_MS after the Enter before it; it counts as typed.
361
+ */
362
+ export function typeLineIntoPane(transport, paneId, line) {
363
+ let panes = typingInto.get(transport);
364
+ if (!panes)
365
+ typingInto.set(transport, panes = new Map());
366
+ const waiting = panes.get(paneId);
367
+ if (waiting) {
368
+ waiting.push(line);
369
+ return true;
370
+ }
371
+ panes.set(paneId, []);
372
+ if (typeInTurn(transport, panes, paneId, line))
373
+ return true;
374
+ panes.delete(paneId);
375
+ return false;
376
+ }
@@ -40,3 +40,8 @@ export declare const CLAUDE_SIGN_IN_OVERRIDES: readonly ["CLAUDE_CODE_OAUTH_TOKE
40
40
  export declare function agentProfileClearedEnv(cli: AgentCliId): readonly string[];
41
41
  /** The variable that points the CLI at the profile. */
42
42
  export declare function agentProfileEnv(cli: AgentCliId, dir: string): Record<string, string>;
43
+ /**
44
+ * The CLI's own arguments a pane on a profile starts with, ahead of the person's: Codex keeps the sign-in in the
45
+ * profile's files, never a keyring entry other terminals share. Claude Code needs none.
46
+ */
47
+ export declare function agentProfileArgs(cli: AgentCliId): string[];
@@ -72,3 +72,10 @@ export function agentProfileClearedEnv(cli) {
72
72
  export function agentProfileEnv(cli, dir) {
73
73
  return cli === 'claude' ? { CLAUDE_CONFIG_DIR: dir } : { CODEX_HOME: dir };
74
74
  }
75
+ /**
76
+ * The CLI's own arguments a pane on a profile starts with, ahead of the person's: Codex keeps the sign-in in the
77
+ * profile's files, never a keyring entry other terminals share. Claude Code needs none.
78
+ */
79
+ export function agentProfileArgs(cli) {
80
+ return cli === 'codex' ? ['-c', 'cli_auth_credentials_store="file"'] : [];
81
+ }
@@ -0,0 +1,134 @@
1
+ /**
2
+ * Opening a Claude Code or Codex session again: the rule for what its pane starts with and where (resumeLaunchPlan),
3
+ * and the thin call that opens it like any pane the person opens (openResumedAgentPane).
4
+ */
5
+ import type { PaneCommandTransport } from '../ui/ratatui/pane_relay.js';
6
+ import { type AgentCliId, type AgentLaunchRequest, type AgentResumeMode, type PaneProgram } from './agent_cli.js';
7
+ import { type OpenAgentPaneResult } from './agent_panes.js';
8
+ import type { AgentSessionsController } from './agent_sessions/controller.js';
9
+ /** The session to open: where its store says it is and whose sign-in it belongs to. */
10
+ export interface ResumeTarget {
11
+ cli: AgentCliId;
12
+ id: string;
13
+ /** The session's folder, as its file says; null when its head names none. */
14
+ cwd: string | null;
15
+ /** Whether that folder is known to be the session's. */
16
+ folderVerified: boolean;
17
+ /** The ZTC profile whose store holds the session; null for this computer's own sign-in. */
18
+ profile: string | null;
19
+ }
20
+ export interface ResumePlanOptions {
21
+ mode: AgentResumeMode;
22
+ /** Open it in this folder instead of its own. */
23
+ here: boolean;
24
+ /** This pane's folder. */
25
+ cwdNow: string;
26
+ /** The session's next prompt, passed as the CLI's own argument. */
27
+ prompt?: string;
28
+ /** Whether a folder exists. */
29
+ folderExists: (path: string) => boolean;
30
+ /** The new id a Claude Code fork is given, so the pane holding it is known (default: a random UUID). */
31
+ newSessionId?: () => string;
32
+ }
33
+ /** What the pane starts with, where, and on which sign-in. */
34
+ export interface ResumeLaunchPlan {
35
+ cli: AgentCliId;
36
+ request: AgentLaunchRequest;
37
+ cwd: string;
38
+ /** null for this computer's own sign-in, a profile's name, or undefined for the one new panes start on. */
39
+ profile: string | null | undefined;
40
+ }
41
+ export type ResumePlanResult = {
42
+ plan: ResumeLaunchPlan;
43
+ } | {
44
+ error: 'folder-gone';
45
+ folder: string;
46
+ } | {
47
+ error: 'folder-unknown';
48
+ };
49
+ /**
50
+ * How a session is opened again: in its own folder, on the sign-in whose store holds it, with Claude Code's
51
+ * `--resume <id>` (and `--fork-session`) or Codex's `resume <id>` (or `fork <id>`), and its next prompt as the CLI's
52
+ * argument. With `here`, in this folder instead. A folder the session names that is gone, or one its file doesn't
53
+ * name or Claude Code's project folder doesn't confirm, is refused, never swapped for this one: the agent would work
54
+ * against paths that aren't there. `here` is how the person chooses this folder for it.
55
+ */
56
+ export declare function resumeLaunchPlan(target: ResumeTarget, options: ResumePlanOptions): ResumePlanResult;
57
+ /**
58
+ * The most recent session in this folder: Claude Code's `--continue`, Codex's `resume --last`, which each CLI finds
59
+ * itself. ZTC reads nothing for it; it starts on the profile asked for, else the one new panes start on.
60
+ */
61
+ export declare function continueLaunchPlan(cli: AgentCliId, options: {
62
+ cwdNow: string;
63
+ prompt?: string;
64
+ profile?: string;
65
+ }): ResumeLaunchPlan;
66
+ /** A session by its name, which the CLI looks for itself (`--resume <name>`, `resume <name>`), in this folder. */
67
+ export declare function nameLaunchPlan(cli: AgentCliId, name: string, options: {
68
+ cwdNow: string;
69
+ prompt?: string;
70
+ profile?: string;
71
+ }): ResumeLaunchPlan;
72
+ export interface ResumedPaneDeps {
73
+ findBinary?: (id: AgentCliId) => string | null;
74
+ server?: PaneProgram | Error | null;
75
+ env?: NodeJS.ProcessEnv;
76
+ /** `motsi` to start the pane through; undefined for MotSi's remote view, as for any pane the person opens. */
77
+ motsi?: string | null;
78
+ /** Home directory the profiles live under (default: the user's). */
79
+ profilesHome?: string;
80
+ /** The pane to open it next to (create_pane's target), if any. */
81
+ target?: number;
82
+ }
83
+ /**
84
+ * Open the plan's pane exactly as any /claude or /codex pane the person opens: openAgentPane, so it gets the same
85
+ * permission defaults, workspace server, kitty settings, MotSi wrapping and name ("Claude Code", "Codex @work", never
86
+ * the session's title). The profile is passed explicitly when the plan names one (null: this computer's own
87
+ * sign-in), so it wins over the profile new panes start on.
88
+ */
89
+ export declare function openResumedAgentPane(transport: PaneCommandTransport | null, plan: ResumeLaunchPlan, deps?: ResumedPaneDeps): Promise<OpenAgentPaneResult>;
90
+ /**
91
+ * A Claude Code or Codex pane the workspace saved, PANE_ID, whose program isn't running (restored "Not started", or
92
+ * exited), started again on the session its saved launch held (SESSION, from agentPaneSession): opened as the person's
93
+ * /claude resume <id> opens it, in its folder on the sign-in whose store holds it, `--resume <id>` in a pane Main knows
94
+ * and answers, next to PANE_ID, which then closes. Its saved command would start a fresh session's `--session-id <id>`
95
+ * over again, in a pane Main doesn't know. Says what happened; null when no store holds the session (one started and
96
+ * never written to), so the saved command runs as it was saved. A session a running pane holds is gone to there, and
97
+ * one PANE_ID itself runs is refused. Throws, saying why, when it can't be opened.
98
+ */
99
+ export declare function resumeSavedAgentPane(transport: PaneCommandTransport, paneId: number, session: {
100
+ cli: AgentCliId;
101
+ id: string;
102
+ }, sessions: AgentSessionsController, options: {
103
+ cwdNow: string;
104
+ deps?: ResumedPaneDeps;
105
+ }): Promise<string | null>;
106
+ /**
107
+ * What Main says once a session's pane opened: the pane, and nothing of the session, its folder or a prompt. Other
108
+ * panes and their agents may read Main's screen (workspace spec §7.4), so with panes no line names those.
109
+ */
110
+ export declare function openedSessionLine(opened: {
111
+ title?: string;
112
+ paneId?: number;
113
+ }, how: AgentResumeMode | 'continue'): string;
114
+ /** `~/…` for a folder under the home the stores are read from. */
115
+ export declare function homeRelative(path: string, home: string): string;
116
+ /** The command as the person reads it: the permission flag ZTC added, then the CLI's own words and the prompt. */
117
+ export declare function shownAgentCommand(binary: string, request: AgentLaunchRequest, permissionArgs?: readonly string[]): string;
118
+ /** The sign-in a session's CLI starts on: a profile's config folder, or this computer's own, named when ZTC's env set it. */
119
+ export interface ResumeSignIn {
120
+ /** The config folder to name; null for this computer's own sign-in in its default folder. */
121
+ dir: string | null;
122
+ /** Whether it is a ZTC profile's, which a pane signs in to as openAgentPane does. */
123
+ profile: boolean;
124
+ }
125
+ /**
126
+ * Where there are no panes: the command that resumes the plan's session in another terminal, in its folder and on its
127
+ * sign-in as its pane would start: `dir`, the config folder to name (a profile's, or this computer's own when ZTC's
128
+ * environment set it, else null), and on a profile (`options.profile`) Claude Code's sign-in overrides unset
129
+ * (agentProfileClearedEnv) or Codex's file store (agentProfileArgs). ZTC's bypass flag is a pane default, so it isn't
130
+ * in it, and nothing is copied to the clipboard.
131
+ */
132
+ export declare function elsewhereCommandLine(plan: ResumeLaunchPlan, dir: string | null, options?: {
133
+ profile?: boolean;
134
+ }): string;