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
//! `tcode serve --http` daemon discovery file (issue #3415, DOC-50 §3.4).
//!
//! Why: `tcode tui` (`crate::tui_client::CodeEngine`) needs to find a running
//! `tcode serve --http` daemon without the operator copying a port number by
//! hand. DOC-50 §3.4's prose sketched a NEW, tcode-specific JSON file
//! (`~/.trusty-code/daemon.json`, `{daemon_url, pid, started_at}`) for this —
//! but this crate's siblings already solve the identical problem, and their
//! convention is different: `trusty-memory` (which wrote one until #6286 retired both the file and its listener)
//! and `trusty-search` (`crates/trusty-search/src/service/daemon.rs`) both
//! write the bound `host:port` as PLAIN TEXT to a file named `http_addr`
//! under `trusty_common::resolve_data_dir(<app_name>)`, atomically (tmp +
//! rename) so a reader never observes a half-written value. This module
//! follows THAT established convention for tcode instead of inventing a
//! second, JSON-shaped one: `pid`/`started_at` have no consumer anywhere in
//! this codebase yet, and a plain-text `host:port` is exactly what
//! `discover_daemon_url` (`crate::tui_client::discovery`) needs to build
//! `http://{addr}`.
//! What: [`http_addr_path`] resolves `{resolve_data_dir("trusty-code")}/http_addr`
//! (server AND client side share this one path-resolution function so they
//! can never drift onto two different locations); [`write_http_addr_file`]
//! (called from `crate::serve::http::run_http` after binding) atomically
//! writes the bound address; [`remove_http_addr_file`] (called on graceful
//! shutdown) clears it so a stopped daemon doesn't leave a stale pointer for
//! the next reader; [`read_http_addr_file`] (called from
//! `crate::tui_client::discovery`) reads and trims it back. Every operation
//! is best-effort: a write/remove failure only degrades discovery to the
//! `TCODE_DAEMON_URL` env var; a read failure (file absent, stale content,
//! unreadable) means "no candidate from this source," not a hard error — the
//! caller's overall discovery still fails with a clear, actionable message
//! when no source yields a live daemon.
//! Test: `discovery_tests::*`.
use io;
use SocketAddr;
use ;
/// Filename of the discovery file, written under
/// `resolve_data_dir("trusty-code")` — mirrors what `trusty-memory`'s
/// `http_addr` convention exactly (same filename, same directory-resolution
/// helper, same plain-text `host:port` content).
pub const HTTP_ADDR_FILENAME: &str = "http_addr";
/// Resolve `{resolve_data_dir("trusty-code")}/http_addr`, or `None` if the
/// data directory cannot be resolved (matches
/// retired `http_addr_path` did: the degrade-gracefully
/// contract — a resolution failure here is never fatal, only a missing
/// discovery source).
/// Atomically write `addr` to `path` (tmp + rename).
///
/// Why: a client reading this file mid-write must never observe a partial
/// value; writing to a `.addr.tmp` sibling and renaming over the target
/// gives POSIX atomicity. Mirrors
/// trusty-memory's retired `write_http_addr_file` byte-for-byte in
/// behaviour — duplicated here (rather than adding a cross-product
/// dependency on trusty-memory for one ~10-line helper) since both crates
/// are independent binaries with no other shared coupling.
/// What: creates the parent directory if missing; writes `addr` followed by
/// a trailing newline; renames the tmp file over `path`.
/// Test: `discovery_tests::write_then_read_round_trips_bound_addr`.
pub
/// Best-effort remove of the discovery file.
///
/// Why: called on graceful `tcode serve --http` shutdown so a stopped
/// daemon doesn't leave a stale `http_addr` pointing at a dead port for the
/// next `CodeEngine::discover` call — a reader would otherwise pass the
/// "candidate found" step and only fail at the liveness ping, which is a
/// worse error message than "no daemon found" for the common
/// stop-then-later-reconnect case.
/// What: `std::fs::remove_file`, ignoring any error (already-absent is the
/// common case, e.g. `write_http_addr_file` itself failed earlier).
/// Test: `discovery_tests::remove_is_best_effort_on_missing_file`.
pub
/// Read and trim the discovery file's contents (just `host:port`), or
/// `None` if it's absent/unreadable/empty.
///
/// Why: the client side of discovery — deliberately infallible (`Option`,
/// not `Result`) since "no file" and "unreadable file" are both just "no
/// candidate from this source" to the caller, not distinct error states
/// worth surfacing.
/// Test: `discovery_tests::write_then_read_round_trips_bound_addr`,
/// `discovery_tests::read_missing_file_returns_none`.
pub