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
//! Supervised `serve --foreground` entry point (issue #787).
//!
//! Why: extracted from `lib.rs` to keep that file under the 500-line-cap
//! ratchet budget. All three issue-#787 fixes (lock file ownership, http_addr
//! guarantee, bind-abort-on-collision) live here so they are easy to find and
//! test together.
//! What: exports `bind_foreground_port` (Fix C) and `run_http_foreground`
//! (Fix A + Fix B + Fix C combined entry point) for use by `main.rs`.
//! Test: `bind_foreground_port_refuses_collision` (unit, real TCP bind);
//! `daemon_lock` module tests cover the lock-file logic.
use Result;
use SocketAddr;
use crateDEFAULT_HTTP_PORT;
use crate::;
/// Bind a `TcpListener` to `127.0.0.1:DEFAULT_HTTP_PORT` for the
/// launchd/supervisor `serve --foreground` path — abort on collision.
///
/// Why (issue #787, Fix C): the generic `bind_dynamic_port` silently walks
/// 7070→7071→…→7079 when port 7070 is taken, then falls back to an
/// OS-assigned port. Under `serve --foreground` (the launchd path) this
/// produces a hidden second daemon on a different port rather than failing
/// loudly. The correct behavior for a supervised `serve --foreground` is to
/// abort with a clear error when port 7070 is already bound — the existing
/// daemon is already serving and the new instance should not start at all.
/// The `single_instance_check` in `main.rs` catches the live-daemon case
/// before reaching this function; the only cases that slip through are
/// non-trusty-memory processes bound to 7070 (a genuine conflict) and race
/// windows (two simultaneous launchd respawns) — both are better served by
/// a loud error than a silent port-walk.
/// What: attempts to bind exactly `127.0.0.1:DEFAULT_HTTP_PORT`. Returns
/// `Err` with a clear human-readable message when the port is already in
/// use (EADDRINUSE), so callers can surface it to launchd's log and the
/// user's terminal instead of silently spawning on a different port.
/// Test: `bind_foreground_port_refuses_collision` occupies port 7070 then
/// asserts `bind_foreground_port` returns `Err` containing "already in use".
pub async
/// The canonical `serve --foreground` entry point used by launchd and systemd
/// supervisors (issue #787).
///
/// Why (issue #787): previously `serve --foreground` shared the same
/// `run_http_dynamic` path used by ad-hoc CLI invocations. That path
/// silently port-walked (7070→7071→…→7079→OS-assigned) on bind collision,
/// producing hidden second instances that never appeared in the `http_addr`
/// discovery file at the expected port. This function replaces that path
/// for the supervised case with three explicit guarantees:
///
/// 1. **Lock file ownership** (Fix A): writes `daemon.lock` containing the
/// current PID before binding. The RAII guard removes the file on any
/// exit (graceful shutdown, panic, launchd SIGTERM). `start` and the
/// single-instance guard read this file as a second detection layer when
/// `http_addr` is absent or stale.
///
/// 2. **http_addr written on bind** (Fix B): `run_http_on` writes both the
/// OS-standard `http_addr` file and the legacy dotfile path
/// (`~/.trusty-memory/http_addr`) immediately after binding, before
/// accepting the first request. Both files are removed on clean shutdown.
/// This ensures `trusty-memory port` and the MCP bridge always find the
/// running daemon.
///
/// 3. **Abort on port collision** (Fix C): uses `bind_foreground_port`
/// (binds exactly port 7070, returns `Err` on `EADDRINUSE`) instead of
/// the port-walking `bind_dynamic_port`. If 7070 is already taken the
/// function returns `Err` with a clear message; the caller (`main.rs`)
/// exits non-zero, launchd logs the error, applies `ThrottleInterval`,
/// and the single-instance guard prevents a respawn storm.
///
/// What: acquires the daemon lock, binds `127.0.0.1:7070` (aborts on
/// collision), then runs `run_http_on` which writes the addr file and
/// serves until graceful shutdown. The lock guard is dropped after
/// `run_http_on` returns, removing `daemon.lock` best-effort.
///
/// Test: `bind_foreground_port_refuses_collision` (unit), plus the
/// integration path `trusty-memory service start` followed by a second
/// `trusty-memory serve --foreground` which should exit immediately with
/// the "already in use" error.
pub async