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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
//! macOS TCC "responsible process" disclaim for the children tm spawns.
//!
//! Why: trusty-mpm launches every managed agent as a `claude` process — via a
//! `tmux` session ([`crate::core::tmux`]), as an inherited-stdio attended
//! process (`tm run`/`tm login`), or, for the daemon's default actor-managed
//! session backend, as a direct long-lived piped child
//! ([`crate::control::backend::stream_json`]). On macOS the consent system
//! (TCC) attributes a child's access request to its *responsible process*,
//! which it resolves by walking the spawn chain up to the first ancestor that
//! took responsibility. Because the signed `trusty-mpm` daemon forked all
//! three of these WITHOUT disclaiming, that responsible process resolved back
//! to `trusty-mpm` itself. So when an agent (or a shell/build-script it runs,
//! e.g. a `cargo build` touching `~/Library/CloudStorage`) touches a
//! TCC-protected resource, macOS pops "trusty-mpm would like to access data
//! from other apps" / "…Apple Music…" — repeatedly, one storm per requesting
//! child, because each transient child has no stable code identity for TCC to
//! remember the decision against (issues #2819, #2721, #2997). tm's own
//! filesystem traversal is already fenced off from those paths
//! ([`crate::core::protected_dirs`], #2760); this remaining prompt class is
//! child-initiated access mis-attributed to the signed parent.
//! What: [`disclaimed_output`] spawns a command exactly like
//! `Command::new(program).args(args).output()` but, on macOS, sets the
//! `responsibility_spawnattrs_setdisclaim` posix_spawn attribute so the child
//! — and everything it forks, including the persistent tmux server — becomes
//! its OWN responsible process (used by the tmux-hosted managed-session path,
//! [`crate::core::tmux::run_tmux_with_bin`]). [`disclaimed_status`] does the
//! same for an inherited-stdio child (used by `tm run`/`tm login`).
//! [`disclaimed_piped_spawn`] does the same for a long-lived, piped-I/O child
//! (used by [`crate::control::backend::stream_json::StreamJsonBackend`]'s
//! default actor-managed session — issue #2997's fix). All three resolve the
//! private SPI with `dlsym` at call time: if it is ever absent the spawn
//! proceeds normally (no disclaim, no regression, no re-run). Setting
//! `TM_DISABLE_SPAWN_DISCLAIM=1` forces the plain path for any of them as an
//! operational safety valve. On non-macOS all three are a thin pass-through
//! to the standard library's own spawn (`Command::output` / `Command::status`
//! / `tokio::process::Command::spawn`).
//!
//! Known limitation: tmux uses one shared server. The disclaim takes effect
//! only when *tm* forks the server; if a server was already running (e.g. the
//! user's own `tmux`), `new-session -A` attaches to it and that server's
//! responsibility is unchanged until the next server fork (`tmux
//! kill-server` / reboot).
//!
//! Scope: as of #2997 this covers the tmux-hosted managed-session path
//! ([`crate::core::tmux::run_tmux_with_bin`]), the `tm run`/`tm login`
//! inherited-stdio path (`standalone::run::build_launch_command` /
//! `build_login_command`), AND the daemon's default actor-managed session
//! backend ([`crate::control::backend::stream_json::StreamJsonBackend`]) —
//! every known `claude`-spawning call site in the crate now disclaims. As of
//! #3126 [`disclaimed_spawn_detached`] adds the TUI health screen's `[S]` key
//! (`tui::event_loop::health_start`, a detached `cargo run` child never
//! waited on) — the last undisclaimed spawn site found by the #2997 part 5
//! sweep. As of #3267 (#2997 part 6) [`disclaimed_stderr_piped_spawn`] and
//! [`disclaimed_stdout_piped_spawn`] add the two non-`claude` spawn sites the
//! #3261 code-critic review flagged: `provisioner::clone_progress::clone_with_progress`
//! (a daemon-initiated `git clone`) and
//! `formatters::info_box::probes::run_git_log` (the `tm` binary's welcome-panel
//! `git log` probe). Split into their own `stderr_piped`/`stdout_piped`
//! submodules (mirroring the pre-existing `macos::stderr_piped`/
//! `macos::stdout_piped` split) to keep this file under the 500-SLOC
//! production cap — see those modules' own docs for their Why/What/Test.
//! Test: `disclaimed_output_captures_stdout`,
//! `disclaimed_output_captures_stderr_and_nonzero_exit`,
//! `disclaimed_output_saturates_stdout_and_stderr_without_deadlock`,
//! `disclaimed_output_reports_spawn_error_for_missing_binary`,
//! `disclaimed_status_inherits_and_reports_exit_code`,
//! `disclaimed_status_applies_cwd_and_env_override`,
//! `disclaimed_status_reports_spawn_error_for_missing_binary`,
//! `disclaimed_second_kill_after_wait_is_noop` (all macOS-only,
//! in this module's `tests`); `spawn_piped_disclaimed_writes_and_reads_via_cat`
//! and its siblings in `macos::piped` (piped path, macOS-only);
//! `disclaimed_piped_spawn_native_path_round_trips` (piped path, any OS —
//! exercises the exact code path Linux always takes);
//! `disclaimed_spawn_detached_returns_ok_for_true`,
//! `disclaimed_spawn_detached_reports_spawn_error_for_missing_binary`,
//! `disclaimed_spawn_detached_disable_env_forces_plain_path` (macOS-only,
//! this module's `tests`); `stderr_piped`'s and `stdout_piped`'s own `tests`
//! (macOS-only) and `native_tests` (any OS) submodules cover
//! [`disclaimed_stderr_piped_spawn`]/[`disclaimed_stdout_piped_spawn`].
/// Environment variable that, when set to any value, forces
/// [`disclaimed_output`]/[`disclaimed_status`]/[`disclaimed_piped_spawn`]
/// onto the plain (non-disclaimed) spawn path. Operational safety valve for
/// the private-SPI-based spawn.
pub const DISABLE_ENV: &str = "TM_DISABLE_SPAWN_DISCLAIM";
/// Spawn `program` with `args`, capture stdout/stderr, and — on macOS —
/// disclaim TCC responsibility so the child is its own responsible process.
///
/// Why: see the module docs — this is tm's fix for the media-library/App-Data
/// consent storm (issue #2819) on the tmux-hosted managed-session path, which
/// arises because a `claude`/`tmux` child's TCC access is otherwise attributed
/// to the signed `trusty-mpm` binary.
/// What: behaves like `Command::new(program).args(args).output()` (stdin from
/// `/dev/null`, stdout+stderr captured), but on macOS spawns via `posix_spawnp`
/// with the `responsibility_spawnattrs_setdisclaim` attribute set. There is
/// exactly ONE spawn regardless of whether the disclaim SPI is present, so a
/// missing SPI never causes a double-run. On non-macOS it delegates straight to
/// `Command::output`. A failure to spawn with `ETXTBSY` is retried by
/// [`trusty_common::spawn_retry::retry_on_etxtbsy`]; `ETXTBSY` cannot arrive
/// once a child exists, so no retry ever re-runs a command that already
/// started.
/// Test: `disclaimed_output_captures_stdout`,
/// `disclaimed_output_captures_stderr_and_nonzero_exit`,
/// `disclaimed_output_saturates_stdout_and_stderr_without_deadlock`,
/// `disclaimed_output_reports_spawn_error_for_missing_binary`; the retry
/// itself by `trusty_common::spawn_retry`'s own tests.
/// Spawn an already-built `Command` connected to the current process's
/// stdin/stdout/stderr (i.e. one built with `Stdio::inherit()` on all three,
/// the shape `tm run`/`tm login` use for attended sessions) and — on macOS —
/// disclaim TCC responsibility exactly like [`disclaimed_output`]. Returns
/// once the child exits, mirroring `Command::status()`.
///
/// Why: `standalone::run::build_launch_command`/`build_login_command` (the
/// `tm run <alias>`/`tm login` interactive drivers) spawn `claude` directly
/// via `Command::status()`, bypassing the tmux-hosted disclaim in
/// [`disclaimed_output`]/[`crate::core::tmux::run_tmux_with_bin`] entirely —
/// so this session-launch path still reproduced the #2819/#2721 TCC
/// mis-attribution storm (issue #2997). This routes it through the same
/// disclaim.
/// What: reconstructs the program, args, working directory, and environment
/// overrides from the already-built `cmd` via `Command`'s public accessors
/// (`get_program`/`get_args`/`get_current_dir`/`get_envs`) — so callers keep
/// building and unit-testing plain `Command`s exactly as before (see
/// `standalone::run::build_launch_command`'s own tests) — then, on macOS,
/// spawns via `posix_spawnp` with NO stdio file actions (fds 0/1/2 pass
/// through unmodified: `Stdio::inherit()` semantics), a `chdir` file action
/// when `cmd` has a working directory set, an environment block equal to the
/// current process environment with `cmd`'s env overlay applied (same
/// override/remove semantics `Command::get_envs` documents — this does NOT
/// support a prior `Command::env_clear()`, which none of tm's callers use),
/// and the disclaim posix_spawn attribute when the private SPI resolves. On
/// non-macOS, or when `TM_DISABLE_SPAWN_DISCLAIM` is set, delegates straight
/// to `cmd.status()`.
/// Test: `disclaimed_status_inherits_and_reports_exit_code`,
/// `disclaimed_status_applies_cwd_and_env_override`,
/// `disclaimed_status_reports_spawn_error_for_missing_binary`,
/// `disclaimed_status_disable_env_forces_plain_path`.
/// Spawn `program` with `args` detached from the current process — a
/// fire-and-forget spawn, matching `Command::spawn()`'s semantics of
/// returning immediately without waiting for the child — and, on macOS,
/// disclaim TCC responsibility exactly like [`disclaimed_output`]/
/// [`disclaimed_status`].
///
/// Why: the TUI health screen's `[S]` key (`tui::event_loop::health_start`)
/// launches a detached `cargo run -p trusty-search -- start` / `cargo run -p
/// trusty-memory` child and never waits on it, so it fit none of the other
/// three disclaim shapes (capture-to-completion, wait-for-status, or
/// long-lived piped) — the last undisclaimed spawn site in the TUI/
/// session-launch call graph (issue #2997 part 5, #3126). Two more
/// undisclaimed spawn sites (`provisioner::clone_progress`,
/// `formatters::info_box::probes::run_git_log`) were found by the #3261
/// code-critic review and are fixed by [`disclaimed_stderr_piped_spawn`]/
/// [`disclaimed_stdout_piped_spawn`] below (#2997 part 6, issue #3267).
/// What: on macOS, builds a `Command` for `program`/`args` with stdio left at
/// the default (inherited from the caller, matching the pre-existing
/// `.spawn()` call this replaces) and spawns it via `posix_spawnp` with the
/// disclaim attribute set, discarding the pid without waiting on it — the
/// child is reaped later either by an unrelated `waitpid` or, once `tm`
/// itself exits, by init/launchd, exactly as the un-awaited
/// `std::process::Child` this replaces already behaved (its caller dropped
/// the `Child` without calling `wait()`). On non-macOS, or with
/// [`DISABLE_ENV`] set, delegates straight to `Command::spawn()` and drops
/// the `Child` the same way.
/// Test: `disclaimed_spawn_detached_returns_ok_for_true`,
/// `disclaimed_spawn_detached_reports_spawn_error_for_missing_binary`,
/// `disclaimed_spawn_detached_disable_env_forces_plain_path`.
pub use ;
pub use ;
// #5969: every other spawn shape here waits unbounded, which hung `tm` on a
// wedged `claude --version`; this one carries a deadline.
// #6863: `runtime::claude_code_agents::query_registry` needs the same deadline
// for a `Command` it builds itself (it exports `CLAUDE_CONFIG_DIR`).
pub use ;
/// Trait-object alias for a spawned child's writable stdin, uniform across
/// the native (`tokio::process::ChildStdin`) and macOS-disclaimed
/// (`tokio::net::unix::pipe::Sender`) spawn paths so callers ([`PipedSpawn`])
/// don't need to match on which path produced the child.
pub type DynChildStdin = ;
/// Trait-object alias for a spawned child's readable stdout — see
/// [`DynChildStdin`].
pub type DynChildStdout = ;
/// A handle to a spawned child's lifecycle (kill/wait), uniform across the
/// native and macOS-disclaimed spawn paths.
///
/// Why: [`StreamJsonBackend`](crate::control::backend::stream_json::StreamJsonBackend)
/// needs `start_kill`/`wait` regardless of which path spawned the child; this
/// enum keeps that call surface identical to `tokio::process::Child`'s own,
/// so callers migrating from a bare `tokio::process::Child` change only the
/// type name.
/// What: `Native` wraps a real `tokio::process::Child` (used on non-macOS,
/// and on macOS when [`DISABLE_ENV`] forces the fallback). `Disclaimed`
/// (macOS only) holds the raw pid, a `spawn_blocking` reaper task (spawned
/// eagerly at spawn time so the child is reaped even if `wait` is never
/// called), and the still-open-but-undrained stderr read end (kept alive
/// only for RAII — matches `StreamJsonBackend`'s pre-existing "captured but
/// never forwarded" behavior).
/// Test: exercised by every `spawn_piped_disclaimed_*` test (macOS) and
/// `disclaimed_piped_spawn_native_path_round_trips` (any OS);
/// `disclaimed_second_kill_after_wait_is_noop` covers the
/// reap-then-re-kill sequence specifically.
/// The result of a piped, disclaimed spawn: a lifecycle [`ChildHandle`] plus
/// the child's stdin/stdout pipes as uniform async trait objects.
///
/// Why: [`disclaimed_piped_spawn`]'s return shape — see that function's docs.
/// What: three public fields; the caller destructures them (stderr is
/// intentionally not exposed — see [`ChildHandle`]'s docs on why it stays
/// open-but-undrained rather than surfaced).
/// Test: see [`disclaimed_piped_spawn`].
/// Spawn a long-lived child with piped stdin/stdout and — on macOS — disclaim
/// TCC responsibility, exactly like [`disclaimed_output`] but for a
/// long-lived, async, piped-I/O child instead of a capture-to-completion one.
///
/// Why: fixes issue #2997 — `StreamJsonBackend`'s `claude -p --output-format
/// stream-json` child (the daemon's default actor-managed session backend)
/// previously spawned via plain `tokio::process::Command`, so its TCC access
/// (and that of anything IT forks, e.g. an agent's `cargo build`) rolled up
/// to the signed `trusty-mpm` binary. This is the same disclaim fix as #2819
/// applied to the long-lived piped spawn shape.
/// What: takes an already-built `std::process::Command` (e.g.
/// `build_claude_command`'s output, `ANTHROPIC_API_KEY` already removed) and,
/// on macOS, re-derives argv/envp/cwd from it via the stable
/// `get_program`/`get_args`/`get_envs`/`get_current_dir` accessors and spawns
/// via `posix_spawnp` with three pipes and the disclaim attribute set,
/// wrapping the parent-held pipe ends in `tokio::net::unix::pipe`. On
/// non-macOS (or with [`DISABLE_ENV`] set) it spawns `cmd` through
/// `tokio::process::Command` exactly as `StreamJsonBackend` did before this
/// fix — a pure pass-through with no behavior change.
/// Test: `spawn_piped_disclaimed_writes_and_reads_via_cat`,
/// `spawn_piped_disclaimed_preserves_cwd`,
/// `spawn_piped_disclaimed_removes_env_and_keeps_rest`,
/// `spawn_piped_disclaimed_reports_spawn_error_for_missing_binary`,
/// `spawn_piped_disclaimed_kill_and_wait_reaps_child` (macOS-only, in
/// `macos::piped`); `disclaimed_piped_spawn_native_path_round_trips` (any OS).
/// The non-disclaimed fallback for [`disclaimed_piped_spawn`]: spawn `cmd`
/// through plain `tokio::process::Command`.
///
/// Why: used unconditionally on non-macOS (there is no TCC there) and as the
/// [`DISABLE_ENV`] escape hatch on macOS — identical to
/// `StreamJsonBackend::spawn`'s pre-#2997 behavior.
/// What: converts `cmd` to a `tokio::process::Command`, spawns it, and takes
/// its stdin/stdout pipes (the caller's `cmd` must already have configured
/// `Stdio::piped()` on both — `build_claude_command` does). stderr is left
/// untouched (never `.take()`n), matching `StreamJsonBackend`'s pre-existing
/// "captured but never forwarded" behavior.
/// Test: `disclaimed_piped_spawn_native_path_round_trips`.
pub
pub use ;
/// Whether the private `responsibility_spawnattrs_setdisclaim` SPI resolves on
/// this build.
///
/// Why: `tm doctor`'s TCC-taint check (issue #2997) needs to tell the operator
/// whether managed panes can actually disclaim — a future macOS that dropped
/// the SPI would silently degrade the whole fix to a no-op, and that should be
/// surfaced, not invisible.
/// What: `true` on macOS when `macos::resolve_disclaim_fn` finds the symbol;
/// `false` on every non-macOS build (there is no TCC there).
/// Test: `daemon::doctor`'s `tcc_taint_*` checks fold this into the verdict;
/// direct coverage lives in the capture/status/piped spawn tests that only
/// disclaim when it returns `Some`.
/// Whether the [`DISABLE_ENV`] escape hatch is currently set.
///
/// Why: the same env var that forces every spawn shape onto the plain path
/// also disables the pane disclaim ([`disclaim_pane_command`]); `tm doctor`
/// reports when it is set so an operator who flipped it is reminded the #2997
/// attribution fix is off.
/// What: `true` when the process sees `TM_DISABLE_SPAWN_DISCLAIM` set to any
/// value.
/// Test: exercised via `daemon::doctor`'s verdict builder.