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
//! The daemon's single background-maintenance lane, and what it did last
//! (#7965).
//!
//! Why: the daemon runs two unattended subprocess sweeps — the merged-PR
//! worktree reclaim ([`super::merged_pr_reclaim`]) and the in-project hygiene
//! pass ([`crate::daemon::managed_routes::inproject_hygiene`]). Before #7965 they
//! started within milliseconds of each other at boot and then ran concurrently,
//! unbounded, for as long as they liked: hygiene alone was measured still
//! running NINE MINUTES after start on a host with 72 base clones, ~10 `git`
//! subprocesses each including a network fetch. The resource the two of them
//! consume is the one every HTTP handler also needs — this process's CPU and its
//! share of the host's IO — and with both of them on it `GET /health` answered in
//! ~5 s, past the 500 ms probe in [`crate::core::discovery`], so every `tm`
//! command reported the daemon unreachable.
//!
//! What: [`lane`] is one permit. Both sweeps take it for the duration of a pass,
//! so at most ONE subprocess storm exists at a time and neither can start while
//! the other is mid-pass. [`RECLAIM`] and [`HYGIENE`] are the in-memory records
//! this process reads, and [`super::sweep_state_file`] is the durable marker the
//! same guard writes for every OTHER process.
//!
//! # Why the row needs both (#8059)
//!
//! `tm doctor` runs daemonless (#6336), so the row is evaluated in the CLI
//! process — where the atomics are untouched and every sweep therefore reads
//! "ON, idle, no pass yet", which is what it reported on 2026-09-15 while the
//! daemon's reclaim sweep had live `git`/`gh` children. The atomics remain the
//! daemon's own lock-free answer; the marker is what makes the same answer
//! readable from outside it. The row takes the two together, so neither source
//! can silently become the only one.
//!
//! # Why a lane and not a thread-count cap
//!
//! Neither sweep is parallel inside itself; each is a serial loop on ONE
//! blocking-pool thread. What made the machine unusable was two such loops plus
//! `git fetch`'s own internal `index-pack` threads, all at once. Serialising the
//! two loops is what bounds the concurrent demand; the per-subprocess ceilings in
//! [`crate::core::bounded_proc`] are what bound each loop's duration.
//!
//! Test: `sweep_status_records_the_last_pass_duration`,
//! `sweep_status_guard_records_even_on_panic`,
//! `the_lane_serialises_two_sweeps` in `sweep_status_tests.rs`.
use ;
use LazyLock;
use ;
use ;
use Semaphore;
/// Sentinel for "this sweep has not completed a pass in this process".
///
/// Why a sentinel rather than `Option` behind a lock: the whole point of the
/// atomics is that the doctor row costs no lock on a busy daemon.
const NEVER: u64 = u64MAX;
/// One sweep's observable state: is it on, is it running, how long did the last
/// pass take.
///
/// Why: an operator whose daemon has gone quiet needs to see a sweep OVERRUNNING
/// before they can connect it to the latency they are feeling. Before #7965 the
/// only evidence was `sample`.
/// What: two atomics — no lock, no persistence. `enabled` is NOT stored: it is
/// read from the env gate at display time so a doctor run always reports the live
/// policy rather than what boot happened to see.
/// Test: `sweep_status_records_the_last_pass_duration`.
pub
/// Records a pass's duration when it ends, however it ends.
///
/// Test: `sweep_status_guard_records_even_on_panic`.
pub
/// The merged-PR worktree reclaim sweep's name, in the row and on disk.
pub const RECLAIM_NAME: &str = "worktree-reclaim";
/// The in-project hygiene sweep's name, in the row and on disk.
pub const HYGIENE_NAME: &str = "inproject-hygiene";
/// The merged-PR worktree reclaim sweep's record.
pub static RECLAIM: SweepStatus = new;
/// The in-project hygiene sweep's record.
pub static HYGIENE: SweepStatus = new;
/// The one background-maintenance permit both sweeps take.
///
/// Why one and not two: see the module doc — the failure was two concurrent
/// subprocess storms, and one permit is the smallest thing that makes that
/// impossible without ordering the sweeps against each other.
/// What: a `Semaphore` with a single permit, acquired for a whole pass. A sweep
/// that finds it taken WAITS rather than skipping, because both sweeps are
/// idempotent and a skipped hygiene pass would leave a base clone stale for the
/// life of the daemon (hygiene runs once per boot).
/// Test: `the_lane_serialises_two_sweeps`.
pub
/// One row of the `background_sweeps` doctor check.
///
/// Test: `sweep_rows_report_each_sweeps_switch_and_last_pass`.
pub
/// Both sweeps' current state, for the doctor row (#7965, #8059).
///
/// Why here rather than in the doctor module: the two statics, the two markers
/// and the two env gates are this module's and the sweeps' own, and a doctor
/// check that re-derived any of them would drift from what the sweep actually
/// does.
/// What: each row folds this process's atomics together with the sweep's durable
/// marker under `framework_root` — see [`fold`].
/// Test: `sweep_rows_report_each_sweeps_switch_and_last_pass`,
/// `a_running_sweep_is_reported_by_a_process_that_did_not_run_it`.
pub
/// One sweep's row, from this process's atomics AND its on-disk marker (#8059).
///
/// Why both rather than the marker alone: inside the daemon the atomics are the
/// authoritative, lock-free answer and cost no syscall, and a marker that could
/// not be written must not silently downgrade the daemon's own row. So the two
/// are OR-ed for `running` and the atomics win for `last_pass` — a reader that
/// has neither (every process but the daemon, before #8059) now has one.
/// Test: `a_running_sweep_is_reported_by_a_process_that_did_not_run_it`,
/// `a_row_with_no_marker_still_reports_this_processs_own_pass`.
/// The `background_sweeps` doctor row (#7965, #8059).
///
/// Why: before this, an operator whose `tm` commands had started timing out had
/// no way to see that a background sweep was mid-pass — the only evidence on the
/// reporting host was `sample` against the daemon. The row makes "hygiene has
/// been running for nine minutes" a thing you can read.
/// What: one line per sweep — its kill switch, whether it is on, whether a pass
/// is in flight, and how long the last completed pass took, read from the state
/// the sweep itself updates at both edges of a pass ([`rows`]). `Warn` when a
/// pass is currently running or the last one exceeded [`SLOW_PASS`], because that
/// is the state that costs request latency; `Ok` otherwise. Never `Unknown`:
/// both sources are always readable, and an absent marker is an absent pass.
/// Test: `background_sweeps_row_warns_on_a_slow_pass`,
/// `background_sweeps_row_is_ok_when_both_sweeps_are_idle`,
/// `a_running_sweep_is_reported_by_a_process_that_did_not_run_it`.
pub
/// Pass duration past which a sweep is worth an operator's attention.
///
/// Why 60 seconds: both sweeps are bounded now — hygiene by its own pass budget,
/// the reclaim by its per-subprocess ceilings — so a pass over a minute means the
/// host has enough bases or worktrees that the sweep is a measurable share of the
/// daemon's time, which is the thing #7965 is about.
const SLOW_PASS: Duration = from_secs;