1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! Compose a managed Claude Code launch as parameters, not as a shell script
//! (#8233).
//!
//! Why: `spawn_command`/`resume_command`/`attach_command` used to return one
//! shell STRING that `tmux send-keys` typed into the pane. Every parameter — the
//! cwd, the `env -u` scrub flags, each `NAME=VALUE`, the prompt-file path, the
//! `--mcp-config` path — added bytes to that line, and past the tty's
//! canonical-mode limit (`MAX_CANON`, 1024 on macOS) the line discipline dropped
//! the tail and the session died mid-word. This module composes the SAME
//! parameters into a [`LaunchSpec`] instead, so the typed line carries only a
//! fixed-shape reference to it.
//!
//! What: [`managed_env_unset`]/[`managed_env_set`] are the structured form of
//! the old `env_bin_prefix` — same variables, same order, same reasons (see each
//! function). [`spawn_spec`], [`resume_spec`] and [`attach_spec`] add the argv
//! each path needs. [`pane_line`] renders the one thing the pane is typed.
//!
//! The #2997 disclaim contract is unchanged and slightly stronger: the pane
//! still runs `tm internal-spawn-disclaimed`, and that shim now `posix_spawn`s
//! `claude` ITSELF rather than spawning an `env` that execs it, so `claude` is
//! its own TCC responsible process with one less hop.
//!
//! Test: `crates/trusty-mpm/src/runtime/managed_launch_tests.rs`.
use Path;
use LaunchSpec;
/// The environment variable a managed spawn always strips, ahead of the
/// inherited-marker scrub.
///
/// Why: with `ANTHROPIC_API_KEY` removed, Claude Code falls back to OAuth, so
/// the operator's API key never reaches a managed session (DOC-34). This was the
/// `-u ANTHROPIC_API_KEY` that led every `env` prefix.
/// Test: `env_unset_leads_with_the_api_key`.
const API_KEY_ENV: &str = "ANTHROPIC_API_KEY";
/// Everything a managed launch removes from the inherited pane environment.
///
/// Why: three independent removals, in one list because `LaunchSpec` applies
/// them before any assignment — the same ordering POSIX `env` forced when this
/// was a `-u` flag run. (1) [`API_KEY_ENV`], so the session authenticates via
/// the tm-owned config dir rather than the operator's key. (2)
/// [`crate::core::claude_env_scrub::INHERITED_SESSION_MARKERS`] — an inherited
/// `CLAUDE_CODE_CHILD_SESSION` makes the spawned `claude` turn transcript saving
/// OFF, costing the session its native `--resume`/`--continue`/`/rewind`
/// recovery (#4467). (3) #6668: the `gh` identity variables the pane already
/// exports, when this launch pins a `gh` account — `gh` reads an env token
/// before a config dir, so leaving an inherited `GH_TOKEN` in place would make
/// every `gh` call in the session use the wrong account. Which ones to clear is
/// decided by [`crate::core::gh_identity::inherited_identity_to_clear`], the
/// same function `resolve_gh_env` uses, so the precedence rule has one
/// definition. (Before #8233 those were `unset NAME` lines in a sourced temp
/// file; they are the same names, applied to the child instead of to the pane
/// shell.)
/// What: the names, in that order.
/// Test: `env_unset_leads_with_the_api_key`,
/// `env_unset_carries_every_inherited_session_marker`,
/// `env_unset_clears_an_inherited_gh_token`,
/// `env_unset_clears_nothing_without_a_gh_identity_binding`.
pub
/// Every variable a managed launch ASSIGNS, in the order the `env` prefix
/// emitted them.
///
/// Why, per entry — each rationale is the one the shell prefix carried:
/// * #8066 `CLAUDE_CODE_ENABLE_TODO_TOOLS=1`, unconditional: Claude Code
/// 2.1.260 gates `TodoWrite` and the `TaskCreate` family to a fixed model
/// list, so a managed session on any other model has no progress tracking.
/// * #7685 `CLAUDE_CODE_DISABLE_AUTO_MEMORY=1`, only when `memory_reachable`:
/// auto memory is the FALLBACK, so with trusty-memory down a session that
/// also lost auto memory would have no memory at all (owner ruling
/// 2026-09-12).
/// * DOC-34 / #2246 `CLAUDE_CONFIG_DIR`: points the session at the tm-owned
/// config home, which relocates the `user` settings tier the bundled agent
/// roster lives in (#4451/#4455) and keys its auth away from `~/.claude`.
/// * #4181 `mcp_env`: the per-project MCP pins (`TRUSTY_MEMORY_PALACE`,
/// `TRUSTY_INDEX`) Claude Code hands on to every stdio MCP server it spawns.
/// * #2246 `CLAUDE_CODE_OAUTH_TOKEN`: bypasses the `CLAUDE_CONFIG_DIR`-keyed
/// Keychain divergence that otherwise produces the managed-session login
/// loop.
/// * #3025/#6668 `gh_env` (`GH_TOKEN`/`GH_USER`/`GH_CONFIG_DIR`): the pinned
/// `gh` identity. Before #8233 these were `export` lines in a mode-0600 temp
/// file the pane sourced and deleted, expressly so the token never appeared
/// in the typed line. `LaunchSpec`'s own file is mode 0600 inside a mode
/// 0700 directory and is consumed-and-deleted, so that property is kept by
/// the same means for one carrier instead of two.
///
/// The #6495/#7160 alternate-screen and mouse defaults are deliberately NOT
/// here: they must yield per-variable to a value the pane already exports, which
/// [`LaunchSpec::to_command`] expresses with
/// [`crate::core::alt_screen::apply_default_to_command`] — the exec-path twin of
/// the `${NAME-1}` shell operand. The config-decided renderer (#8405) is not a
/// default and is added by [`ManagedLaunch::base`].
/// What: `(name, value)` pairs, in the order above.
/// Test: `env_set_enables_todo_tools_unconditionally`,
/// `env_set_disables_auto_memory_when_trusty_memory_answers`,
/// `env_set_keeps_auto_memory_when_trusty_memory_is_unreachable`,
/// `env_set_relocates_the_config_dir`, `env_set_carries_a_non_empty_mcp_env`,
/// `env_set_carries_the_oauth_token_when_available`,
/// `env_set_omits_the_oauth_token_when_absent`,
/// `env_set_carries_the_gh_identity`.
pub
/// The launch inputs shared by the spawn, resume and attach paths.
///
/// Why: the three builders need the same nine values, and passing them as one
/// borrowed struct keeps each builder a single testable call rather than a
/// nine-argument function (the shape `claude_code_agents::RelaunchInputs`
/// already established for the two relaunch builders).
/// What: plain borrows, no ownership and no I/O. `claude_bin` is the RESOLVED
/// absolute binary (#1298) and is NOT disclaim-wrapped — the shim is now the
/// process that reads this spec, so it spawns `claude` directly (#2997).
/// Test: used by every test in `managed_launch_tests.rs`.
pub
/// Build the spec for a fresh managed spawn (replaces `spawn_command`).
///
/// Why: the daemon's default on-ramp. See [`ManagedLaunch::isolation_args`] for
/// why each flag is there.
/// What: [`ManagedLaunch::base`] plus the isolation argv.
/// Test: `spawn_argv_matches_the_shell_line_it_replaces`,
/// `spawn_spec_roots_the_child_at_the_workspace`.
pub
/// Build the spec for a resume (replaces `resume_command`).
///
/// Why: a resume restores the prior conversation when an id survived the
/// staleness check; there is no `--continue` fallback (#6765), because a bare
/// `--continue` resolves "most recent" against a managed store this process did
/// not choose.
/// What: [`spawn_spec`]'s argv, then `--resume <id>` when `claude_session_id` is
/// `Some`; otherwise the fresh-launch argv unchanged.
/// Test: `resume_spec_appends_the_resume_flag`,
/// `resume_spec_without_an_id_is_a_plain_spawn`.
pub
/// Build the spec for `claude attach <short-id>` (replaces `attach_command`).
///
/// Why: a session Claude Code is still running as a background job refuses
/// `--resume` and exits 0; `attach` re-enters it with the conversation intact
/// (#6863). It must otherwise look exactly like a resume — same cwd, same
/// environment, same disclaim shim — or the attached session loses whatever the
/// differing piece carried.
/// What: [`ManagedLaunch::base`] with `attach <attach_id>` as the whole argv.
/// The isolation flags are deliberately absent: `claude attach <id>` accepts no
/// options, so passing them would make `claude` reject the invocation, and the
/// system prompt they would carry is already part of the conversation being
/// re-entered.
/// Test: `attach_spec_omits_flags_attach_cannot_take`,
/// `attach_and_resume_share_an_identical_environment`.
pub
/// The whole line typed into the pane for a managed launch (#8233).
///
/// Why: this is the string whose length caused the bug, so its shape is the
/// fix's central claim. It has exactly two variable parts — a 36-character UUID
/// and two tm-owned paths — and grows with NONE of the launch's parameters.
/// What: `export TM_MANAGED_SESSION_ID='<uuid>'; '<wrapper>'
/// internal-spawn-disclaimed --launch-spec '<spec>'`.
///
/// The `export` is kept on the pane line deliberately (#2023 component B): it
/// lands in the pane's own shell, so it survives `claude` exiting and lets a
/// bare `tm` run in that pane afterwards identify the session. It is
/// fixed-length — a UUID — so it costs the line nothing that grows. The spawned
/// `claude` gets the same variable from [`LaunchSpec::to_command`], not from
/// this export.
///
/// Both paths are single-quoted for the same reason every path in this crate's
/// pane commands is: a home directory containing a space would otherwise
/// word-split and silently break the launch.
/// Test: `pane_line_is_short_for_the_worst_case_launch`,
/// `pane_line_exports_the_managed_session_id`,
/// `pane_line_routes_through_the_disclaim_shim`.
pub
/// The short line typed into the pane when a launch is ABANDONED (#8233).
///
/// Why: a launch that cannot run must not leave the operator staring at a pane
/// that simply never started anything. Every abort path — the spec could not be
/// written, it could not be read back, the tm binary could not be resolved, the
/// command line was refused as over-length — types this before the error
/// propagates, and the caller marks the session record errored.
/// What: a single `echo` whose length is fixed by construction: a static
/// sentence plus a 36-character UUID. It must stay far under
/// [`crate::core::tmux::MAX_PANE_COMMAND_BYTES`], because the case it exists for
/// includes "the previous line was too long to type".
/// Test: `abort_notice_is_short_enough_to_always_type`,
/// `deliver_announces_a_refused_line_in_the_pane`.
/// Write the spec, prove it readable, and type the pane's launch line.
///
/// Why: this is the fail-closed choke point. Each step can fail, and every
/// failure has to end the same way — nothing half-launched, a loud line in the
/// pane, and a `RuntimeError` the caller turns into `mark_errored`. Keeping the
/// four steps in one function is what stops a future caller performing three of
/// them.
/// What: resolves the tm binary ([`crate::core::spawn_disclaim::launch_wrapper_bin`]);
/// CONFIRMS the pane's shell is executing what it is typed
/// ([`super::pane_handshake::confirm_prompt`]) before anything else happens;
/// writes the spec to its mode-0600 file; reads it back ([`LaunchSpec::verify`]);
/// publishes this launch's id ([`LaunchSpec::write_launch_pointer_in`]); types
/// [`pane_line`] through
/// [`crate::session_manager::ManagedTmuxDriver::send_command_line`], which
/// refuses an over-length line rather than letting the tty truncate it. The
/// pane's shim then writes the sentinel, which
/// `daemon::managed_routes::launch_verify::launch_was_delivered` reads — that is
/// the half of the handshake that proves the SHELL, not just tmux, took the
/// line. On any failure it types [`abort_notice`] (best-effort — a pane that
/// cannot be typed into cannot be told either), removes the spec it wrote so no
/// credential is left on disk, and returns the error.
/// Test: `deliver_types_a_short_line_and_leaves_a_readable_spec`,
/// `deliver_announces_a_refused_line_in_the_pane`,
/// `deliver_errors_and_cleans_up_when_the_spec_dir_is_unwritable`,
/// `deliver_resets_the_pane_before_typing` — the wedged-pane handshake, and
/// that it precedes the keystrokes.
///
/// // #8233: the `spec_dir` is an ARGUMENT. This used to be a `deliver()`
/// wrapper resolving `LaunchSpec::root()` from the process home, which put a
/// file carrying `CLAUDE_CODE_OAUTH_TOKEN` and `GH_TOKEN` into the operator's
/// own config home on every test-driven launch. The caller
/// (`ClaudeCodeAdapter::spec_dir`) derives it from the framework root the
/// daemon holds, so the home-unresolvable arm the wrapper carried is gone.
pub
/// The steps [`deliver`] wraps; see its doc.