@zergai/ztc 0.26.2-dev.202611210149 → 0.26.4-dev.202611231953

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (275) hide show
  1. package/README.md +590 -28
  2. package/bin/zmux-darwin-arm64 +0 -0
  3. package/bin/zmux-linux-x64 +0 -0
  4. package/bin/ztc-audio-darwin-arm64 +0 -0
  5. package/bin/ztc-capture-darwin-arm64 +0 -0
  6. package/bin/ztc-tui-darwin-arm64 +0 -0
  7. package/bin/ztc-tui-linux-x64 +0 -0
  8. package/bin/zterm-darwin-arm64 +2 -2
  9. package/dist/App.js +18 -1
  10. package/dist/agent/agent.d.ts +25 -1
  11. package/dist/agent/agent.js +26 -1
  12. package/dist/agent/attached_images.d.ts +58 -0
  13. package/dist/agent/attached_images.js +145 -0
  14. package/dist/agent/backends/anthropic.d.ts +14 -0
  15. package/dist/agent/backends/anthropic.js +49 -2
  16. package/dist/agent/backends/codex_app_server.d.ts +3 -1
  17. package/dist/agent/backends/codex_app_server.js +9 -6
  18. package/dist/agent/backends/codex_auth.js +3 -0
  19. package/dist/agent/backends/gemini.js +3 -0
  20. package/dist/agent/backends/host_effort_policy.js +14 -2
  21. package/dist/agent/backends/http_error.d.ts +17 -0
  22. package/dist/agent/backends/http_error.js +50 -1
  23. package/dist/agent/backends/index.js +2 -1
  24. package/dist/agent/backends/model_redirect.d.ts +47 -0
  25. package/dist/agent/backends/model_redirect.js +97 -0
  26. package/dist/agent/backends/openai_compatible.js +5 -0
  27. package/dist/agent/backends/openai_responses.d.ts +9 -1
  28. package/dist/agent/backends/openai_responses.js +21 -12
  29. package/dist/agent/backends/prompt_cache.d.ts +8 -0
  30. package/dist/agent/backends/prompt_cache.js +10 -0
  31. package/dist/agent/backends/provider_ids.d.ts +1 -1
  32. package/dist/agent/backends/provider_ids.js +1 -1
  33. package/dist/agent/backends/types.d.ts +8 -0
  34. package/dist/agent/backends/zerg-api.js +5 -1
  35. package/dist/agent/backends/zrouter_stream.d.ts +75 -9
  36. package/dist/agent/backends/zrouter_stream.js +250 -49
  37. package/dist/agent/commands/config.js +51 -4
  38. package/dist/agent/commands/connect.js +20 -0
  39. package/dist/agent/commands/credential_status.js +5 -3
  40. package/dist/agent/commands/debug.js +3 -0
  41. package/dist/agent/commands/diff.js +9 -5
  42. package/dist/agent/commands/emulation.js +7 -1
  43. package/dist/agent/commands/inbox.js +4 -3
  44. package/dist/agent/commands/mcp.js +3 -0
  45. package/dist/agent/commands/models.js +20 -3
  46. package/dist/agent/commands/permissions.js +16 -2
  47. package/dist/agent/commands/provider_selection.js +13 -6
  48. package/dist/agent/commands/settings.js +15 -4
  49. package/dist/agent/commands/status.js +23 -1
  50. package/dist/agent/commands/types.d.ts +34 -3
  51. package/dist/agent/commands/update.d.ts +2 -1
  52. package/dist/agent/commands/update.js +8 -3
  53. package/dist/agent/commands/zerg.d.ts +6 -0
  54. package/dist/agent/commands/zerg.js +152 -28
  55. package/dist/agent/commands/ztc_pane.js +8 -5
  56. package/dist/agent/factory.js +22 -4
  57. package/dist/agent/request_encoder.js +11 -2
  58. package/dist/agent/runtime/command_jobs.js +16 -1
  59. package/dist/agent/runtime/command_panes.js +4 -1
  60. package/dist/agent/runtime/execution_context.d.ts +8 -1
  61. package/dist/agent/runtime/execution_context.js +8 -2
  62. package/dist/agent/runtime/policy.d.ts +66 -5
  63. package/dist/agent/runtime/policy.js +161 -33
  64. package/dist/agent/runtime/remote_broker_transport.d.ts +7 -0
  65. package/dist/agent/runtime/remote_broker_transport.js +33 -3
  66. package/dist/agent/runtime/remote_resource_errors.d.ts +7 -0
  67. package/dist/agent/runtime/remote_resource_errors.js +10 -0
  68. package/dist/agent/runtime/remote_resources.d.ts +1 -1
  69. package/dist/agent/runtime/remote_resources.js +7 -3
  70. package/dist/agent/runtime/source_verification.js +15 -12
  71. package/dist/agent/runtime/tool_runtime.js +165 -58
  72. package/dist/agent/runtime/verifier_capture.d.ts +1 -1
  73. package/dist/agent/runtime/verifier_capture.js +6 -0
  74. package/dist/agent/runtime/work_base.js +46 -14
  75. package/dist/agent/tools/adaptation.js +29 -12
  76. package/dist/agent/tools/apply_patch.js +62 -5
  77. package/dist/agent/tools/background.js +2 -0
  78. package/dist/agent/tools/benchmark_cpu.js +2 -0
  79. package/dist/agent/tools/bus_message.d.ts +4 -2
  80. package/dist/agent/tools/bus_message.js +21 -3
  81. package/dist/agent/tools/delegate.js +2 -0
  82. package/dist/agent/tools/file.d.ts +12 -1
  83. package/dist/agent/tools/file.js +92 -11
  84. package/dist/agent/tools/lsp.d.ts +12 -0
  85. package/dist/agent/tools/lsp.js +61 -5
  86. package/dist/agent/tools/pane.js +20 -45
  87. package/dist/agent/tools/project_context.js +5 -2
  88. package/dist/agent/tools/routine.js +20 -4
  89. package/dist/agent/tools/screenshot.d.ts +8 -0
  90. package/dist/agent/tools/screenshot.js +39 -25
  91. package/dist/agent/tools/search.js +31 -3
  92. package/dist/agent/tools/shell.js +7 -1
  93. package/dist/agent/tools/supervision.js +6 -0
  94. package/dist/agent/tools/types.d.ts +18 -0
  95. package/dist/agent/tools/zerg.js +15 -19
  96. package/dist/agent/tools/ztc_command.d.ts +15 -4
  97. package/dist/agent/tools/ztc_command.js +48 -34
  98. package/dist/app/agent_runner.js +8 -6
  99. package/dist/app/conversation_compaction.d.ts +6 -3
  100. package/dist/app/conversation_compaction.js +6 -3
  101. package/dist/app/tool_callbacks.js +1 -0
  102. package/dist/audio/sfx.js +6 -0
  103. package/dist/batch/worktree.js +22 -10
  104. package/dist/board/client.js +2 -1
  105. package/dist/cli_main.d.ts +1 -0
  106. package/dist/cli_main.js +141 -45
  107. package/dist/cli_ratatui.d.ts +49 -4
  108. package/dist/cli_ratatui.js +355 -79
  109. package/dist/conductor_branch.js +4 -1
  110. package/dist/config/host_credentials.d.ts +157 -0
  111. package/dist/config/host_credentials.js +589 -0
  112. package/dist/config/model_pin.d.ts +111 -0
  113. package/dist/config/model_pin.js +182 -0
  114. package/dist/config/own_state_files.d.ts +35 -0
  115. package/dist/config/own_state_files.js +161 -0
  116. package/dist/config/permission_pin.d.ts +148 -0
  117. package/dist/config/permission_pin.js +203 -0
  118. package/dist/config/permissions.d.ts +9 -2
  119. package/dist/config/permissions.js +14 -3
  120. package/dist/config/platform_login.d.ts +72 -0
  121. package/dist/config/platform_login.js +111 -0
  122. package/dist/config/profile_limits.d.ts +34 -0
  123. package/dist/config/profile_limits.js +66 -0
  124. package/dist/config/provider_api_key_env.js +3 -2
  125. package/dist/config/provider_models.d.ts +6 -1
  126. package/dist/config/provider_models.js +38 -11
  127. package/dist/config/repo_config.d.ts +22 -0
  128. package/dist/config/repo_config.js +77 -3
  129. package/dist/config/router_runtime_grant.d.ts +53 -0
  130. package/dist/config/router_runtime_grant.js +131 -0
  131. package/dist/config/settings_merge.d.ts +22 -0
  132. package/dist/config/settings_merge.js +75 -0
  133. package/dist/config/types.d.ts +8 -0
  134. package/dist/config.d.ts +334 -4
  135. package/dist/config.js +1024 -41
  136. package/dist/context/project_intelligence.d.ts +11 -2
  137. package/dist/context/project_intelligence.js +38 -15
  138. package/dist/desktop/daily/desktop_wiring.js +8 -6
  139. package/dist/desktop/daily/native_pane_launcher.js +2 -1
  140. package/dist/desktop/daily/native_tool.d.ts +1 -1
  141. package/dist/desktop/daily/native_tool.js +4 -3
  142. package/dist/desktop/daily/process_identity.js +10 -2
  143. package/dist/desktop/daily/source_baseline.js +4 -3
  144. package/dist/diagnostics/native_terminal_benchmark.js +3 -2
  145. package/dist/diagnostics/session_export.js +27 -14
  146. package/dist/embedded/anempa_runtime.js +3 -3
  147. package/dist/embedded/gameboy_runtime.js +5 -4
  148. package/dist/embedded/zergpod_runtime.js +5 -4
  149. package/dist/emulation/catalog.js +6 -1
  150. package/dist/emulation/tool_adapters.js +13 -3
  151. package/dist/environment/store.js +5 -4
  152. package/dist/extension_discovery.d.ts +12 -2
  153. package/dist/extension_discovery.js +52 -7
  154. package/dist/headless.d.ts +8 -0
  155. package/dist/headless.js +52 -13
  156. package/dist/headless_cli.js +16 -1
  157. package/dist/headless_workflow_trigger.js +1 -1
  158. package/dist/hooks/dispatcher.d.ts +8 -0
  159. package/dist/hooks/dispatcher.js +22 -2
  160. package/dist/hooks/lifecycle.d.ts +6 -0
  161. package/dist/hooks/lifecycle.js +1 -0
  162. package/dist/hooks/program_files.d.ts +10 -0
  163. package/dist/hooks/program_files.js +175 -0
  164. package/dist/hooks/runner.d.ts +11 -1
  165. package/dist/hooks/runner.js +112 -11
  166. package/dist/hooks/types.d.ts +2 -0
  167. package/dist/host_credentials_startup.d.ts +6 -0
  168. package/dist/host_credentials_startup.js +17 -0
  169. package/dist/instance_registry.d.ts +6 -2
  170. package/dist/instance_registry.js +9 -15
  171. package/dist/intake/format.d.ts +6 -0
  172. package/dist/intake/format.js +9 -0
  173. package/dist/intake/notifications.js +2 -1
  174. package/dist/lsp/manager.d.ts +1 -1
  175. package/dist/lsp/manager.js +4 -5
  176. package/dist/mcp/client.d.ts +2 -0
  177. package/dist/mcp/client.js +116 -18
  178. package/dist/mcp/registry.d.ts +12 -0
  179. package/dist/mcp/registry.js +15 -1
  180. package/dist/modes/handlers.js +14 -5
  181. package/dist/panes/agent_cli.d.ts +23 -1
  182. package/dist/panes/agent_cli.js +96 -1
  183. package/dist/panes/agent_panes.d.ts +27 -4
  184. package/dist/panes/agent_panes.js +73 -11
  185. package/dist/panes/editor_program.d.ts +8 -0
  186. package/dist/panes/editor_program.js +25 -0
  187. package/dist/panes/motsi.d.ts +11 -1
  188. package/dist/panes/motsi.js +14 -1
  189. package/dist/panes/pane_registry.d.ts +28 -1
  190. package/dist/panes/pane_registry.js +25 -0
  191. package/dist/panes/worker_route.js +5 -3
  192. package/dist/panes/ztc_launch.d.ts +8 -0
  193. package/dist/panes/ztc_launch.js +26 -1
  194. package/dist/profile_baseline.js +2 -0
  195. package/dist/router_auth.d.ts +8 -3
  196. package/dist/router_auth.js +34 -2
  197. package/dist/runtime_info.js +56 -38
  198. package/dist/session.js +12 -2
  199. package/dist/shannon/runtime.js +13 -1
  200. package/dist/simulation/domains/loader.d.ts +2 -1
  201. package/dist/simulation/domains/loader.js +19 -2
  202. package/dist/simulation/living/context.d.ts +8 -8
  203. package/dist/simulation/living/types.d.ts +2 -2
  204. package/dist/simulation/memory/embedding.d.ts +2 -1
  205. package/dist/simulation/memory/embedding.js +25 -6
  206. package/dist/terminal_attach.js +2 -1
  207. package/dist/types.d.ts +21 -0
  208. package/dist/ui/ratatui/headless_pane_manager.d.ts +12 -6
  209. package/dist/ui/ratatui/headless_pane_manager.js +65 -21
  210. package/dist/ui/ratatui/ipc_path.d.ts +41 -2
  211. package/dist/ui/ratatui/ipc_path.js +164 -5
  212. package/dist/ui/ratatui/pane_credential_broker.d.ts +17 -4
  213. package/dist/ui/ratatui/pane_credential_broker.js +23 -5
  214. package/dist/ui/ratatui/pane_relay.d.ts +74 -0
  215. package/dist/ui/ratatui/pane_relay.js +188 -13
  216. package/dist/ui/ratatui/process.d.ts +15 -1
  217. package/dist/ui/ratatui/process.js +129 -163
  218. package/dist/ui/ratatui/protocol.d.ts +11 -1
  219. package/dist/ui/ratatui/protocol.js +6 -0
  220. package/dist/ui/ratatui/ztc_launch_trust.d.ts +87 -0
  221. package/dist/ui/ratatui/ztc_launch_trust.js +249 -0
  222. package/dist/utils/admitted_open.d.ts +40 -0
  223. package/dist/utils/admitted_open.js +111 -0
  224. package/dist/utils/clipboard.js +2 -1
  225. package/dist/utils/computer_native.js +12 -56
  226. package/dist/utils/dictation.js +15 -12
  227. package/dist/utils/dictation_native.js +17 -60
  228. package/dist/utils/git_probe_options.d.ts +28 -0
  229. package/dist/utils/git_probe_options.js +30 -0
  230. package/dist/utils/git_program_files.d.ts +145 -0
  231. package/dist/utils/git_program_files.js +1282 -0
  232. package/dist/utils/git_repository_programs.d.ts +13 -0
  233. package/dist/utils/git_repository_programs.js +38 -0
  234. package/dist/utils/host_shell.js +2 -1
  235. package/dist/utils/idle_compaction.js +8 -1
  236. package/dist/utils/image_fit.d.ts +4 -1
  237. package/dist/utils/image_fit.js +6 -2
  238. package/dist/utils/image_preview.js +3 -2
  239. package/dist/utils/mirror_media.d.ts +22 -4
  240. package/dist/utils/mirror_media.js +87 -86
  241. package/dist/utils/ml_native.js +11 -56
  242. package/dist/utils/native_install.d.ts +55 -0
  243. package/dist/utils/native_install.js +107 -0
  244. package/dist/utils/open_url.js +2 -0
  245. package/dist/utils/process_end.d.ts +16 -0
  246. package/dist/utils/process_end.js +73 -0
  247. package/dist/utils/router_catalog.js +9 -2
  248. package/dist/utils/sandbox.d.ts +64 -1
  249. package/dist/utils/sandbox.js +397 -89
  250. package/dist/utils/shell.js +31 -12
  251. package/dist/utils/subprocess_env.d.ts +218 -2
  252. package/dist/utils/subprocess_env.js +429 -11
  253. package/dist/utils/system_ps.d.ts +5 -0
  254. package/dist/utils/system_ps.js +30 -0
  255. package/dist/utils/tts.js +5 -0
  256. package/dist/utils/tts_native.js +13 -60
  257. package/dist/utils/zlicky_overlay.js +17 -50
  258. package/dist/utils/ztc_own_state.d.ts +58 -0
  259. package/dist/utils/ztc_own_state.js +236 -0
  260. package/dist/web/mirror_authenticator.d.ts +67 -6
  261. package/dist/web/mirror_authenticator.js +131 -22
  262. package/dist/web/mirror_stream.d.ts +54 -10
  263. package/dist/web/mirror_stream.js +379 -92
  264. package/dist/web/observer_query_redaction.js +53 -13
  265. package/dist/web/observer_secret_redaction.d.ts +59 -0
  266. package/dist/web/observer_secret_redaction.js +714 -0
  267. package/dist/web/pane_credentials.d.ts +20 -2
  268. package/dist/web/pane_credentials.js +58 -5
  269. package/dist/worker/executor.js +3 -1
  270. package/dist/workspace_collision.js +19 -15
  271. package/dist/zerg/local_zerg_launches.d.ts +27 -0
  272. package/dist/zerg/local_zerg_launches.js +198 -0
  273. package/dist/zlm-embed.mjs +6257 -2791
  274. package/npm-shrinkwrap.json +1 -1
  275. package/package.json +42 -15
@@ -15,15 +15,30 @@
15
15
  * OpenMirrorAuthenticator (dev/local — softens the final deny to allow, while the
16
16
  * token path still issues a scoped session so a dev superuser keeps prompt access).
17
17
  *
18
- * Self-contained: depends only on node crypto, the URL/header shapes and the
19
- * self-contained pane credential store. No imports from the transport, so the
20
- * dependency stays one-way (mirror_stream -> here).
18
+ * Self-contained: depends only on node crypto, the URL/header shapes, the
19
+ * self-contained pane credential store and the host-credential store. No imports
20
+ * from the transport, so the dependency stays one-way (mirror_stream -> here).
21
21
  */
22
- import { createHmac, randomUUID, timingSafeEqual } from 'crypto';
22
+ import { createHash, createHmac, randomUUID, timingSafeEqual } from 'crypto';
23
+ import { hostCredential, holdMintedCredential } from '../config/host_credentials.js';
23
24
  import { PANE_CREDENTIAL_HEADER, PaneCredentialStore } from './pane_credentials.js';
24
25
  const DEFAULT_COOKIE_NAME = 'ztc_mirror_session';
25
26
  const DEFAULT_QUERY_TOKEN_NAME = 'ztc_mirror_token';
26
27
  const DEFAULT_TTL_MS = 12 * 60 * 60 * 1000;
28
+ /**
29
+ * Sessions one token keeps at most: its requests keep one as a header and one in its link, and each sign-in with it
30
+ * starts another, as a browser that signs in with the token from Settings does.
31
+ */
32
+ export const DEFAULT_MAX_SESSIONS_PER_TOKEN = 16;
33
+ /**
34
+ * Sessions the store keeps at most, whatever tokens started them: a backstop that binds only when thousands of tokens
35
+ * hold sessions at once, which only the owner's own minting (or someone holding the mirror's secret) can bring about.
36
+ */
37
+ export const DEFAULT_MAX_SESSIONS = 4096;
38
+ /** The name a token goes by in the store: its digest, the same for every session it starts. */
39
+ function tokenKeyOf(token) {
40
+ return createHash('sha256').update(token).digest('hex');
41
+ }
27
42
  /** Production HMAC + session authenticator. Denies requests without valid credentials. */
28
43
  export class HmacMirrorAuthenticator {
29
44
  requiresCredentials = true;
@@ -34,15 +49,28 @@ export class HmacMirrorAuthenticator {
34
49
  sharedSecret;
35
50
  internalToken;
36
51
  paneCredentials = new PaneCredentialStore();
52
+ /** The sessions, the one used longest ago first (getSession moves a session to the end). */
37
53
  sessions = new Map();
54
+ sessionIds = new WeakMap();
55
+ origins = new WeakMap();
56
+ /**
57
+ * The session each token's requests started last, as a header and in its link (keyed `<how>:<token key>`): a later
58
+ * request presenting the token the same way, a poller's or a browser's with no cookie of it yet, keeps that one.
59
+ */
60
+ requestSessions = new Map();
61
+ maxSessions;
62
+ maxSessionsPerToken;
38
63
  shares = new Map();
39
64
  revocationListeners = new Set();
65
+ dropListeners = new Set();
40
66
  constructor(config) {
41
67
  this.sharedSecret = config.sharedSecret;
42
68
  this.zergId = config.zergId;
43
69
  this.internalToken = config.internalToken;
44
70
  this.cookieName = config.cookieName ?? DEFAULT_COOKIE_NAME;
45
71
  this.queryTokenName = config.queryTokenName ?? DEFAULT_QUERY_TOKEN_NAME;
72
+ this.maxSessions = Math.max(1, Math.floor(config.maxSessions ?? DEFAULT_MAX_SESSIONS));
73
+ this.maxSessionsPerToken = Math.max(1, Math.floor(config.maxSessionsPerToken ?? DEFAULT_MAX_SESSIONS_PER_TOKEN));
46
74
  }
47
75
  issueToken(opts = {}) {
48
76
  const ttlMs = opts.ttlMs ?? DEFAULT_TTL_MS;
@@ -149,19 +177,29 @@ export class HmacMirrorAuthenticator {
149
177
  return { ok: false, session: null, issueCookie: null, source: null };
150
178
  }
151
179
  // 2. Bearer (Authorization header) or query token. A valid one mints a scoped
152
- // session the transport should persist as a cookie.
180
+ // session the transport should persist as a cookie, unless this token already
181
+ // started the session the request can keep.
153
182
  const bearer = this.extractBearerToken(ctx.headers);
154
183
  const queryToken = ctx.url.searchParams.get(this.queryTokenName) || '';
155
184
  const tokenCandidate = bearer || queryToken;
156
185
  if (tokenCandidate) {
157
186
  const result = this.isValidToken(tokenCandidate);
158
187
  if (result.ok) {
159
- const session = {
160
- expiresAtMs: result.expiresAtMs,
161
- canInspectPrompts: result.canInspectPrompts,
162
- canControl: result.canControl !== false,
163
- ...(result.shareId ? { shareId: result.shareId } : {}),
164
- };
188
+ const tokenKey = tokenKeyOf(tokenCandidate);
189
+ // A browser this token signed in sends its link's token with the cookie it was given: it keeps that session.
190
+ const presented = this.sessionFromCookies(ctx.cookies);
191
+ if (presented && this.origins.get(presented)?.tokenKey === tokenKey) {
192
+ return { ok: true, session: presented, issueCookie: null, source: 'browser' };
193
+ }
194
+ // Any other request presenting the token keeps the session the token's requests presenting it the same way
195
+ // started last: a poller's, as a header on each request, or a browser's, in its link with no cookie of it.
196
+ // A browser signing out of its session so leaves a poller's alone.
197
+ const how = bearer ? 'header' : 'link';
198
+ const started = this.requestSessions.get(`${how}:${tokenKey}`);
199
+ const kept = started ? this.getSession(started) : null;
200
+ if (kept)
201
+ return { ok: true, session: kept, issueCookie: kept, source: 'browser' };
202
+ const session = this.newSession(result, tokenKey, how);
165
203
  return { ok: true, session, issueCookie: session, source: 'browser' };
166
204
  }
167
205
  if (tokenCandidate.split('.')[1] === 'observe')
@@ -180,25 +218,95 @@ export class HmacMirrorAuthenticator {
180
218
  // A pane credential is for a pane's own bus and publications, never the voice stream.
181
219
  return decision.ok && decision.source !== 'pane' && decision.session?.canControl !== false;
182
220
  }
221
+ /**
222
+ * Keep SESSION and return the id its cookie carries: a session the store already keeps (one authenticate() handed
223
+ * back) keeps its id. Each insert first drops every session that is no longer valid; then, while the token SESSION
224
+ * came from keeps more than its cap, that token's own session used longest ago, so no token pushes out another's;
225
+ * then, while the store holds more than its cap, the one used longest ago.
226
+ */
183
227
  persistSession(session) {
228
+ const known = this.sessionIds.get(session);
229
+ if (known !== undefined && this.sessions.get(known) === session && this.getSession(known))
230
+ return known;
231
+ for (const [id, kept] of this.sessions)
232
+ if (!this.isLive(kept))
233
+ this.dropSession(id);
184
234
  const sessionId = randomUUID();
185
235
  const observer = session.canControl === false || session.shareId !== undefined;
186
- this.sessions.set(sessionId, { ...session, canControl: !observer, canInspectPrompts: observer ? false : session.canInspectPrompts });
236
+ const stored = { ...session, canControl: !observer, canInspectPrompts: observer ? false : session.canInspectPrompts };
237
+ this.sessions.set(sessionId, stored);
238
+ this.sessionIds.set(stored, sessionId);
239
+ const origin = this.origins.get(session);
240
+ if (origin) {
241
+ this.origins.set(stored, origin);
242
+ if (origin.started !== 'sign-in')
243
+ this.requestSessions.set(`${origin.started}:${origin.tokenKey}`, sessionId);
244
+ const own = [...this.sessions].filter(([, kept]) => this.origins.get(kept)?.tokenKey === origin.tokenKey);
245
+ for (const [id] of own.slice(0, Math.max(0, own.length - this.maxSessionsPerToken)))
246
+ this.dropSession(id);
247
+ }
248
+ for (const [id] of this.sessions) {
249
+ if (this.sessions.size <= this.maxSessions)
250
+ break;
251
+ this.dropSession(id);
252
+ }
187
253
  return sessionId;
188
254
  }
255
+ startSession(token) {
256
+ const result = this.isValidToken(token);
257
+ return result.ok ? this.newSession(result, tokenKeyOf(token), 'sign-in') : null;
258
+ }
189
259
  getSession(sessionId) {
190
260
  const session = this.sessions.get(sessionId);
191
261
  if (!session)
192
262
  return null;
193
- const share = session.shareId ? this.shares.get(session.shareId) : undefined;
194
- if (Date.now() >= session.expiresAtMs || (session.shareId && (!share || share.revoked || Date.now() >= share.expiresAtMs))) {
195
- this.sessions.delete(sessionId);
263
+ if (!this.isLive(session)) {
264
+ this.dropSession(sessionId);
196
265
  return null;
197
266
  }
267
+ // Used now: the last the store would drop.
268
+ this.sessions.delete(sessionId);
269
+ this.sessions.set(sessionId, session);
198
270
  return session;
199
271
  }
200
272
  clearSession(sessionId) {
273
+ this.dropSession(sessionId);
274
+ }
275
+ onSessionDropped(callback) {
276
+ this.dropListeners.add(callback);
277
+ return () => { this.dropListeners.delete(callback); };
278
+ }
279
+ credentialKeyOf(session) {
280
+ return (session && this.origins.get(session)?.tokenKey) ?? null;
281
+ }
282
+ /** A session for the token RESULT validated, which goes by TOKEN_KEY, started as STARTED says. */
283
+ newSession(result, tokenKey, started) {
284
+ const session = {
285
+ expiresAtMs: result.expiresAtMs,
286
+ canInspectPrompts: result.canInspectPrompts,
287
+ canControl: result.canControl !== false,
288
+ ...(result.shareId ? { shareId: result.shareId } : {}),
289
+ };
290
+ this.origins.set(session, { tokenKey, started });
291
+ return session;
292
+ }
293
+ /** Whether SESSION is still valid: not expired, and its share, if it has one, neither revoked nor expired. */
294
+ isLive(session) {
295
+ const share = session.shareId ? this.shares.get(session.shareId) : undefined;
296
+ return Date.now() < session.expiresAtMs
297
+ && !(session.shareId && (!share || share.revoked || Date.now() >= share.expiresAtMs));
298
+ }
299
+ dropSession(sessionId) {
300
+ const session = this.sessions.get(sessionId);
301
+ if (!session)
302
+ return;
201
303
  this.sessions.delete(sessionId);
304
+ const origin = this.origins.get(session);
305
+ const key = origin ? `${origin.started}:${origin.tokenKey}` : '';
306
+ if (origin && this.requestSessions.get(key) === sessionId)
307
+ this.requestSessions.delete(key);
308
+ for (const listener of this.dropListeners)
309
+ listener(session);
202
310
  }
203
311
  matchesInternalToken(headers) {
204
312
  if (!this.internalToken)
@@ -265,26 +373,27 @@ export function mirrorSignInRequired(env = process.env) {
265
373
  /**
266
374
  * Build the authenticator from environment policy, mirroring the legacy
267
375
  * MirrorStreamServer constructor: derive the enabled flag and self-mint the shared
268
- * secret and the internal token, writing them back to process.env so the rest of
269
- * this process (another mirror it runs, its own helpers) shares them. No pane or
270
- * renderer inherits either: the renderer is spawned without them, every pane
376
+ * secret and the internal token when the host gave none, keeping them in the
377
+ * host-credential store (config/host_credentials.ts), never in process.env, so the
378
+ * rest of this process (another mirror it runs, its own relaunch) shares them and no
379
+ * program it starts inherits them. No pane or renderer is given either: every pane
271
380
  * transport clears them, and each pane gets a credential of its own instead (see
272
381
  * pane_credentials.ts).
273
382
  */
274
383
  export function createMirrorAuthenticatorFromEnv() {
275
384
  const zergId = (process.env.ZERG_ID || '').trim();
276
385
  const enabled = mirrorSignInRequired(process.env);
277
- let sharedSecret = (process.env.ZTC_MIRROR_SHARED_SECRET || '').trim();
386
+ let sharedSecret = (hostCredential('ZTC_MIRROR_SHARED_SECRET') || '').trim();
278
387
  if (!sharedSecret) {
279
388
  sharedSecret = randomUUID();
280
- process.env.ZTC_MIRROR_SHARED_SECRET = sharedSecret;
389
+ holdMintedCredential('ZTC_MIRROR_SHARED_SECRET', sharedSecret);
281
390
  }
282
391
  // Always mint an internal token (even in dev/open mode): it is ZTC's own credential
283
392
  // for the internal-only endpoints, kept in this process. Panes use their own.
284
- let internalToken = (process.env.ZTC_MIRROR_INTERNAL_TOKEN || '').trim();
393
+ let internalToken = (hostCredential('ZTC_MIRROR_INTERNAL_TOKEN') || '').trim();
285
394
  if (!internalToken) {
286
395
  internalToken = randomUUID();
287
- process.env.ZTC_MIRROR_INTERNAL_TOKEN = internalToken;
396
+ holdMintedCredential('ZTC_MIRROR_INTERNAL_TOKEN', internalToken);
288
397
  }
289
398
  const config = { sharedSecret, zergId, internalToken };
290
399
  return enabled ? new HmacMirrorAuthenticator(config) : new OpenMirrorAuthenticator(config);
@@ -2,6 +2,7 @@ import type { DailyDesktopSnapshot } from '../desktop/daily/desktop_wiring.js';
2
2
  import { type GoalView } from '../goal_contract.js';
3
3
  import { type EvidenceSnapshot } from '../agent/runtime/evidence.js';
4
4
  import type { TaskHandoffProjection } from '../session_handoff_contract.js';
5
+ import type { NativeNonAgentPane } from '../panes/pane_registry.js';
5
6
  import type { PaneRunStatus } from '../panes/types.js';
6
7
  import { type AttentionSnapshot } from '../attention/queue.js';
7
8
  import type { ViewPlugin } from './view_plugin.js';
@@ -116,6 +117,15 @@ export type BusSendResult = {
116
117
  code?: string;
117
118
  error: string;
118
119
  };
120
+ /**
121
+ * The refusal of a message for pane TO, which runs no agent (a shell, a command, another program), or, when MISSING, is
122
+ * no pane at all (not_an_agent): the mirror's answer, and ZTC's own when it runs no mirror (typeOverseerMessage). Said
123
+ * to an agent, workspace-mcp's in a Claude Code or Codex pane or a ZTC's own: each types into such a pane with its tool.
124
+ * When the pane is one of the desktop app's NATIVE panes that takes no agent's message (nativeNonAgentPane), the refusal
125
+ * says what the pane is and what works instead (nativePaneRefusal): a native Claude Code or Codex pane is no pane a
126
+ * message reaches, though Claude Code and Codex panes are.
127
+ */
128
+ export declare function notAnAgentResult(to: number | 'all', missing: boolean, native?: NativeNonAgentPane): BusSendResult;
119
129
  export type MirrorMuxCommand = {
120
130
  type: 'focus_pane';
121
131
  pane_id: number;
@@ -514,6 +524,7 @@ export declare class MirrorStreamServer implements MirrorPublisher {
514
524
  private paneInputCallback;
515
525
  private paneControlCallback;
516
526
  private agentPaneMessageCallback;
527
+ private nativeNonAgentPaneLookup;
517
528
  private busMessageCheck;
518
529
  private muxCommandCallback;
519
530
  private supervisionCommandCallback;
@@ -781,6 +792,12 @@ export declare class MirrorStreamServer implements MirrorPublisher {
781
792
  * it into the pane with its sender); false leaves the usual fallbacks.
782
793
  */
783
794
  onAgentPaneMessage(cb: (paneId: number, msg: BusMessage) => boolean): void;
795
+ /**
796
+ * Name the desktop app's native panes that take no agent's message (nativeNonAgentPane), from the owner's record of
797
+ * each, so the refusal of a message for one (not_an_agent), Main's or a pane's own, says what the pane is and what
798
+ * works instead (notAnAgentResult). Nothing is delivered either way.
799
+ */
800
+ onNativeNonAgentPane(cb: (paneId: number) => NativeNonAgentPane | undefined): void;
784
801
  /**
785
802
  * What Main says of a message a pane posts to /bus/send before it is delivered (BusMessageCheck): it refuses a reply
786
803
  * mode that names an arbitrator the person didn't set up, and tells the sender what became of its mode.
@@ -792,7 +809,16 @@ export declare class MirrorStreamServer implements MirrorPublisher {
792
809
  onAllEvents(callback: (event: MirrorEvent) => void): () => void;
793
810
  getAttentionSnapshot(limit?: number, offset?: number): AttentionSnapshot;
794
811
  private resolveAttention;
795
- respondAttention(id: string, answer: unknown): void;
812
+ /**
813
+ * Answer attention item ID with ANSWER: a question's text, an approval's decision and reason, a prompt widget's fields.
814
+ * FROM is who sent it: the browser mirror ('mirror': this server's routes, and a command the browser sent or that is
815
+ * relayed here), or the person at this ZTC's own terminal ('terminal': the /attention command they type). Every string
816
+ * the mirror sends is mirror text (stripForeignMediaTokensIn), wherever it goes next: a question's answer becomes the
817
+ * agent's next message, a plan's feedback becomes one, and a ZTC pane that is relayed an answer runs it as its prompt,
818
+ * so a forged `[image PATH]` in it would read PATH. What the person types at the terminal keeps their own paths, as
819
+ * their messages do.
820
+ */
821
+ respondAttention(id: string, answer: unknown, from?: 'mirror' | 'terminal'): void;
796
822
  dismissAttention(id: string): void;
797
823
  /**
798
824
  * Register an in-process bus member (parent ZTC). No HTTP round-trip needed.
@@ -807,14 +833,16 @@ export declare class MirrorStreamServer implements MirrorPublisher {
807
833
  */
808
834
  onAllBusMessages(callback: (msg: BusMessage) => void): () => void;
809
835
  /**
810
- * Deliver a bus message to the appropriate target(s). With `agentsOnly` (what a pane's own credential sends), a message
811
- * goes only to an agent: Main, every bus member ('all'), a pane on the bus or running ZTC, or a Claude Code or Codex
812
- * pane, which the owner types it into with its sender. For any other pane (a shell, a command) nothing is delivered,
813
- * queued or typed, and the answer is 'not_an_agent'. Whoever sends it, an agent's message for a ZTC agent's pane that
814
- * hasn't joined the bus is held until it joins, never typed into the pane, and the answer is 'held' (waitsForJoin);
815
- * once the renderer has reported that pane's program exited, nothing is held or typed and the answer is 'pane_exited'
816
- * (forExitedZtc), until a ZTC that joins stands for the pane again. What a pane that can no longer join held is dropped
817
- * first (dropUnjoinable), so no message for it goes ahead of one that waits.
836
+ * Deliver a bus message to the appropriate target(s). With `agentsOnly` (what a pane's own credential sends, and what
837
+ * Main sends: overseerBusTransport), a message goes only to an agent: Main, every bus member ('all'), a pane on the bus
838
+ * or running ZTC, or a Claude Code or Codex pane, which the owner types it into with its sender. For any other pane (a
839
+ * shell, a command) nothing is delivered, queued or typed, and the answer is 'not_an_agent'. Whoever sends it, an
840
+ * agent's message for a ZTC agent's pane that hasn't joined the bus is held until it joins, never typed into the pane,
841
+ * and the answer is 'held' (waitsForJoin); once the renderer has reported that pane's program exited, nothing is held
842
+ * or typed and the answer is 'pane_exited' (forExitedZtc), until a ZTC that joins stands for the pane again. What a
843
+ * pane that can no longer join held is dropped first (dropUnjoinable), so no message for it goes ahead of one that
844
+ * waits. With `agentsOnly`, a message for a ZTC pane that reads stdin and joins no bus is typed in as one line that
845
+ * says who sent it, never on the pane's control channel and never as its raw text.
818
846
  */
819
847
  deliverBusMessage(msg: BusMessage, options?: {
820
848
  agentsOnly?: boolean;
@@ -930,7 +958,17 @@ export declare class MirrorStreamServer implements MirrorPublisher {
930
958
  private observerProjections;
931
959
  private observerQueryRedaction;
932
960
  private observerTextRedaction;
961
+ /** Each live stream's credential context, keyed as observerQueryRedaction is (see observer_secret_redaction.ts). */
962
+ private observerSecretRedaction;
963
+ /**
964
+ * EVENT as a read-only observer gets it, or null for a type it never gets. Every string it carries has the URL
965
+ * tokens and the credentials an observer is never shown withheld; streamed text keeps each stream's context, so a
966
+ * credential split across its pieces is withheld too. A STANDALONE event is a connect snapshot of a stream's
967
+ * retained text, which neither reads nor moves its live stream's context.
968
+ */
933
969
  private observerEvent;
970
+ /** A copy of VALUE with each string in it as an observer reads it: no /job environment options, URL tokens or SECRETS. */
971
+ private observerStrings;
934
972
  private observerConnectionData;
935
973
  /**
936
974
  * The /debug/config payload. The web client fetches this right after
@@ -1065,6 +1103,10 @@ export declare class MirrorStreamServer implements MirrorPublisher {
1065
1103
  height: number;
1066
1104
  byte_length: number;
1067
1105
  }>;
1106
+ /**
1107
+ * The bucket a capture POST counts against: the token that sent it (one name for every session it starts), the pane
1108
+ * or ZTC process, else the socket's address. Never a header the client writes, such as X-Forwarded-For.
1109
+ */
1068
1110
  private rateLimitKey;
1069
1111
  private isLocalRequest;
1070
1112
  private allowZlickyCapture;
@@ -1118,7 +1160,7 @@ export declare class MirrorStreamServer implements MirrorPublisher {
1118
1160
  private isWavBuffer;
1119
1161
  private transcribeVoiceWav;
1120
1162
  private parseCookies;
1121
- /** Persist the authenticator-minted session and write the Set-Cookie header. */
1163
+ /** Persist the authenticator-minted session and write the Set-Cookie header. Returns the session as the store keeps it. */
1122
1164
  private issueMirrorSession;
1123
1165
  private persistLayoutToDisk;
1124
1166
  private clearMirrorSession;
@@ -1203,6 +1245,8 @@ export declare class MirrorStreamServer implements MirrorPublisher {
1203
1245
  private parseWebSocketFrames;
1204
1246
  private handleUpgrade;
1205
1247
  private handleVoiceStreamSocket;
1248
+ /** Refuse with 403, and say so, an event a pane's own credential may not publish (paneEventRefusal). */
1249
+ private refusePaneEvent;
1206
1250
  private admitChildPublication;
1207
1251
  private handleRequest;
1208
1252
  }