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
//! `tcode tui` — launch the interactive TUI REPL against a running
//! `tcode serve --http` daemon (issue #4424; DOC-50 §4.1, AC-2.4).
//!
//! Why: every DOC-50 MVP slice landed — the shared `trusty-code-tui` framework
//! (event loop, generalized `ReplApp`, widgets) and
//! `trusty_code::tui_client::CodeEngine` (the `TuiEngine` impl) — but
//! nothing ever CONSTRUCTED a `CodeEngine` outside tests, so the REPL was
//! unreachable from a user's shell. This module is that missing integration
//! point and nothing more: it owns no REPL behaviour, no rendering, and no
//! daemon logic, matching `crate::cli`'s "translate CLI args into a call,
//! decisions belong elsewhere" contract.
//! What: [`run`] resolves the optional project path, obtains a daemon URL to
//! drive from `super::daemon_autospawn`, and hands a `CodeEngine` pointed at
//! it to `trusty_code_tui::run::run` together with the shared `ReplApp` model,
//! its reducer (`trusty_code_tui::app::apply`), and its renderer
//! (`trusty_code_tui::layout::draw`).
//!
//! `tcode tui` AUTO-SPAWNS its daemon (#4512, reversing DOC-50 §4.1's
//! deferral): discovery is unchanged (`TCODE_DAEMON_URL` -> the `http_addr`
//! discovery file -> a `GET /health` liveness ping), but a missing or stale
//! discovery file now starts `tcode serve --http` instead of exiting with an
//! actionable message.
//!
//! **Quitting the TUI never stops the daemon** — not even one this command
//! started. The daemon owns PM lifecycle, agent dispatch, and agent
//! communication, and a TUI is one attached client among possibly several,
//! so a client exit must not end live work (owner directive, 2026-08-01).
//! There is correspondingly NO teardown step here. An
//! explicitly-set-but-unreachable `TCODE_DAEMON_URL` still errors rather
//! than spawning, since starting a daemon at a different address would
//! ignore that instruction, and a daemon bound to a DIFFERENT project than
//! this TUI is refused rather than attached to. See
//! `super::daemon_autospawn` for the whole policy — none of it lives here.
//!
//! Daemon resolution deliberately runs BEFORE `trusty_code_tui::run::run` enters
//! the alternate screen, so the startup spinner and any failure land on a
//! normal terminal instead of flashing behind a TUI that is about to tear
//! itself down.
//! Test: `tui_tests::*` covers the pure project-resolution helper;
//! `super::daemon_autospawn`'s tests cover every attach/spawn/binding-check
//! branch against real child processes;
//! `tests/cli_e2e.rs::{tui_subcommand_is_listed_in_help,
//! tui_auto_spawns_a_daemon_that_outlives_it,
//! tui_refuses_to_spawn_for_an_unreachable_explicit_daemon_url,
//! tui_refuses_a_daemon_bound_to_a_different_project}` cover the CLI surface
//! against the REAL binary. The launch path past daemon resolution needs a
//! real TTY (`trusty_code_tui::TerminalGuard::enter`), so it is verified by
//! running `tcode tui` by hand; the engine half is already covered
//! end-to-end by `tests/tui_client_engine.rs`.
use PathBuf;
use Arc;
use ;
use CodeEngine;
use ReplApp;
use daemon_autospawn;
/// Product label for the banner identity line and the `[tcode] ` status
/// prefix `ReplApp::new` derives from it.
const PRODUCT_LABEL: &str = "tcode";
/// Launch the TUI REPL, returning once the user quits — starting a daemon
/// first if none is running, and leaving it running on the way out.
///
/// Why/What/Test: see the module docs — this function IS the module.
/// `project` is `Option` because a projectless session is a first-class
/// state (`session.create` without a `project`), not a degraded one; it is
/// forwarded to a spawned daemon so the daemon's binding matches the TUI's,
/// and checked against a pre-existing daemon's reported binding.
pub async
/// Canonicalize `--project` when given, so the daemon receives an absolute
/// path and an unusable one fails HERE (with the flag's name in the
/// message) rather than as a confusing `-32003 invalid_argument` from
/// `session.create` after the TUI has already started.
/// Name shown on the banner's `{user} · tcode` identity line — `$USER`, or a
/// generic fallback. Mirrors tagent's `repl::run` derivation so both REPLs
/// label the human the same way.
/// The `$USER`-to-label rule, split out from [`user_label`] so it is
/// testable without mutating process-global environment state.