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
//! Adopting a project's PRE-EXISTING worktrees into the daemon's registry
//! (#7357).
//!
//! Why: until #7357 the daemon's whole worktree view came from
//! [`crate::session_manager::worktree_registry::scan_registered_worktrees`],
//! which walks exactly `<repos_root>/<owner>/<repo>`. A project registered from
//! a checkout that does not sit at that depth under the repos root is never
//! interrogated, so every worktree beneath it is invisible to
//! `tm session reconcile-worktrees`, `prune-worktrees --merged-prs`, `tm doctor`
//! and the Disk survey. Registering the project changed nothing, because
//! registration wrote a [`crate::project::Project`] record and nothing else —
//! the guarded reclaim path stayed permanently unavailable for any repo that had
//! agent activity before it was registered, and the operator's only remaining
//! option was `git worktree remove --force` by hand.
//!
//! What: an ADOPTION record per pre-existing worktree, written by
//! [`AdoptionStore::record`] — the one record-writing function — and a set of
//! project ANCHORS derived from those records ([`adopted_anchors`]) that the
//! scan interrogates alongside its repos-root walk. The records carry
//! ATTRIBUTION only (which project a path belongs to, when it was adopted);
//! every FACT about a worktree still comes from `git worktree list --porcelain`
//! at scan time. That split is deliberate: two registries disagreeing about one
//! worktree is a failure this repository has already had twice (see
//! `worktree_registry`'s module docs), so nothing here caches a branch name, a
//! lock, or a liveness answer as truth.
//!
//! FAIL-OPEN, in the one direction that is safe here: an unreadable store, an
//! unreadable worktree, or a checkout git cannot answer for yields FEWER
//! records and FEWER anchors, never more. A missing anchor means the sweep sees
//! a worktree it saw before; a fabricated one would point a reclaim gate at a
//! directory nothing vouched for.
//!
//! FAIL-OPEN IS NOT SILENT (#7357). Every candidate this pass declines to
//! record lands in [`BackfillReport::unrecorded`] with the reason, and both
//! registration transports return that report to their caller, so a skipped or
//! foreign-claimed worktree is visible rather than a `tracing` line nobody
//! reads.
//! Test: `worktree_adoption_tests`.
//!
//! # Concurrency
//!
//! `worktrees.json` is written by the daemon and by any `tm` process that
//! registers a project, so the load → mutate → save cycle needs the same
//! cross-process critical section `projects.json` got in
//! [`crate::project::store`]: two registrations interleaving read/read/write/
//! write drop one project's records while both callers see success.
//! [`AdoptionStore::update`] is the only write path and runs the whole cycle
//! inside [`trusty_common::json_rmw::update`], which takes a file lock,
//! re-reads under it, and publishes by atomic rename. There is deliberately no
//! unlocked `save`.
use BTreeSet;
use ;
use ;
use ;
use warn;
use ;
use crate;
/// File name of the adoption store, inside the project-registry data directory.
///
/// Why: naming it once keeps the daemon writer and the out-of-process `tm`
/// reader from pointing at different files — the same discipline #4300
/// established for `projects.json`.
/// What: `"worktrees.json"`, beside `projects.json`.
/// Test: `adoption_store_path_is_beside_the_project_registry`.
pub const ADOPTED_WORKTREES_FILE: &str = "worktrees.json";
/// Directory names a project parks its worktrees under, checked in addition to
/// git's own registry (#7357).
///
/// Why: git answers for every worktree IT registered, which is the enumeration
/// that matters. These two names catch the remainder — a directory that looks
/// like a worktree but whose gitdir pointer is missing or unreadable, which is
/// exactly the case the backfill must skip loudly rather than silently.
/// What: `.claude/worktrees` (the Claude Code harness's own store) and
/// `.worktrees` (trusty-mpm's).
/// Test: `backfill_skips_a_worktree_with_no_gitdir_pointer_and_still_succeeds`.
const WORKTREE_PARENTS: = ;
/// One adopted worktree: which project owns a path, and when that was decided.
///
/// Why: attribution is the only thing the daemon could not derive. Everything
/// else about a worktree — its branch, whether it is locked, whether its
/// directory still exists — is git's answer at scan time, and storing a second
/// copy is how two registries start to disagree.
/// What: `path` is the canonical worktree directory; `project` is the
/// registered project name; `checkout` is the project checkout whose registry
/// named it (this is what becomes a scan anchor); `branch` is recorded for
/// operator-facing reporting only and is never read back as truth.
/// Test: `record_writes_one_entry_per_worktree`.
/// What [`AdoptionStore::record`] did with one candidate.
///
/// Why: the caller has to be able to tell "already ours" from "someone else's"
/// — the second is the case #7357 requires the backfill to leave strictly
/// alone, and collapsing both into a silent no-op would hide a real conflict.
/// What: three outcomes, exactly one of which writes.
/// Test: `record_is_idempotent_for_the_same_project`,
/// `record_never_reattributes_another_projects_worktree`.
/// The adoption records, loaded from and published to one JSON document.
///
/// Why: a flat list keyed by path is enough — the store answers exactly two
/// questions ("who owns this path?" and "which checkouts should the scan
/// interrogate?") and carries no state a reader could act on destructively.
/// What: just the entries — the FILE is named by the directory each entry
/// point passes, never cached on the value, so no caller can hold a store that
/// remembers a path it is no longer allowed to write. Reads go through
/// [`load`](Self::load) and take no lock; every WRITE goes through
/// [`update`](Self::update), which holds the file lock across the whole
/// read-modify-write cycle (see this module's Concurrency section).
/// Test: every test in `worktree_adoption_tests`.
/// Why one candidate was NOT recorded (#7357).
///
/// Why: "skipped" alone tells an operator a worktree is missing from the
/// registry but not what to do about it, and the three causes need three
/// different actions — fix the checkout path, repair the worktree, or accept
/// that another project already owns it.
/// What: one variant per refusal in [`backfill_checkout`], serialized
/// internally-tagged so the reason and its detail are one flat JSON object
/// beside the path.
/// Test: `backfill_names_the_skipped_worktree_and_why`,
/// `backfill_leaves_another_projects_worktree_alone`.
/// One candidate a backfill pass declined to record, and why (#7357).
///
/// Test: `backfill_names_the_skipped_worktree_and_why`.
/// What one backfill pass did (#7357).
///
/// Why: before this was returned to callers it reached only `tracing`, so a
/// registration that adopted nothing looked exactly like one that adopted
/// everything. Both registration transports now serialize this into their
/// response, which is the whole signal: registration still SUCCEEDS whatever
/// is in here.
/// What: the four counts, plus one [`UnrecordedWorktree`] per candidate that
/// did not become a record — so `unrecorded.len() == skipped +
/// claimed_by_another` — plus the persistence failure, if any.
/// Test: `backfill_records_every_pre_existing_worktree`,
/// `backfill_is_idempotent`, `backfill_names_the_skipped_worktree_and_why`.
/// Adopt every worktree that already exists under `checkout` into `project`
/// (#7357).
///
/// Why: this is the whole fix. A project registered after its worktrees exist
/// must end up with the same registry entries it would have had if the
/// worktrees had been provisioned afterwards, and the only missing ingredient
/// is attribution — so the backfill writes attribution and nothing else.
/// What: enumerates `git worktree list --porcelain` at `checkout` (dropping the
/// main checkout and bare records — neither is a worktree to reclaim), then
/// sweeps `.claude/worktrees/*` and `.worktrees/*` for directories git did not
/// name. Each candidate goes through [`AdoptionStore::record`]. The store is
/// saved once, at the end, only when something changed.
///
/// FAIL-OPEN, and the caller must not treat this as a gate: a `checkout` that
/// is not a git repository root, a git invocation that will not answer, and a
/// candidate directory with no readable gitdir pointer are each SKIPPED with a
/// warning. Registration is a bookkeeping call and must succeed whatever the
/// filesystem says; the cost of a skip is that one worktree stays as invisible
/// as it was before this function existed.
/// Test: `backfill_records_every_pre_existing_worktree`,
/// `backfill_is_idempotent`,
/// `backfill_leaves_another_projects_worktree_alone`,
/// `backfill_skips_a_worktree_with_no_gitdir_pointer_and_still_succeeds`,
/// `backfill_names_the_skipped_worktree_and_why`,
/// `concurrent_backfills_never_lose_a_projects_records`.
/// The project already attributed to `checkout`, if any (#7588).
///
/// Why: registration exists on two independently-named surfaces —
/// `tm project init` (path-keyed, name derived from the directory) and
/// `tm projects register` (operator-named) — and both now adopt. Without this
/// lookup the second one to run would record under a DIFFERENT name for the same
/// directory, and [`AdoptionStore::record`] would answer `ClaimedByAnother` for
/// every worktree, turning a correct second registration into a wall of
/// skips. Asking the store which name already owns the checkout makes the two
/// surfaces converge on one attribution instead of competing.
/// What: the `project` of the first record whose `checkout` equals the canonical
/// form of `checkout`, or `None` — including when git will not answer for the
/// path, which is the same "nothing is attributed" outcome.
/// Test: `owning_project_reports_the_name_that_already_claimed_the_checkout_7588`,
/// `owning_project_is_none_for_an_unrecorded_checkout_7588`.
/// The distinct project checkouts the adoption records name (#7357).
///
/// Why: the scan needs ANCHORS, not worktrees — it re-derives every worktree
/// fact from git at each pass, so what it is missing is only "which checkouts
/// should I ask?". Returning checkouts rather than worktree paths is what keeps
/// this store from becoming a second, drifting copy of git's registry.
/// What: the `checkout` field of every record, de-duplicated and sorted.
/// Test: `anchors_are_the_distinct_checkouts`.
/// The adopted anchors under a framework root the caller already knows (#7357).
///
/// Why: the daemon WRITES its records under `state.framework_root()`, so a
/// reader that resolves `$HOME` instead reads a different file whenever the two
/// disagree — which is exactly what an isolated-managed daemon and every
/// daemon test do. Every entry point holding a `DaemonState` resolves anchors
/// through here rather than [`default_adopted_anchors`], which keeps the reader
/// and the writer on one file and keeps those tests hermetic.
/// What: [`adopted_anchors`] over
/// [`crate::project::registry_data_dir_under`].
/// Test: `parity_register_adopts_the_same_worktrees_across_transports`.
/// The adopted anchors for a process with no `DaemonState` (#7357).
///
/// Why: a few entry points — `tm doctor`'s worktree-disk probe above all — run
/// with no daemon handle, and resolving the framework root the same way
/// [`crate::project::registry_data_dir`] does is what keeps them on the file
/// the daemon writes.
///
/// RESOLVE THIS AT AN ENTRY POINT ONLY. A scan function that calls it
/// internally reads the developer's real
/// `~/.trusty-mpm/project-registry/worktrees.json` from its own unit tests,
/// which is how #7357's first round lost test hermeticity; every scan below an
/// entry point takes `adopted: &[PathBuf]` instead.
/// What: [`adopted_anchors`] over [`crate::project::registry_data_dir`].
/// Test: covered through `adopted_anchors`; the `$HOME`-derived root itself is
/// covered by `core::paths`' own tests.
/// Resolve `checkout` to the canonical root of the repository it belongs to.
///
/// Why: an anchor that is merely a directory INSIDE a repository makes git walk
/// up and answer for an unrelated enclosing repository — the same trap
/// `worktree_registry::scan_from_anchor` guards. Confirming the path IS the
/// root is what makes the recorded anchor safe to hand the scan later.
/// What: `None` unless the path is a directory that git reports as its own
/// repository root.
/// Test: `backfill_ignores_a_path_that_is_not_a_repository_root`.
/// Every worktree candidate under `checkout`, plus the ones left out.
///
/// Why: git's registry is the enumeration that matters, and reusing
/// [`list_registered_worktrees`] rather than re-parsing porcelain here keeps
/// one parser in the crate. The directory sweep afterwards exists for the
/// remainder only — a directory shaped like a worktree that git does not name,
/// which is either registered to some other checkout or broken.
/// What: `(path, branch)` pairs, and one [`UnrecordedWorktree`] per candidate
/// directory with no readable gitdir pointer. The main checkout and bare
/// records are dropped: neither is a worktree an operator could reclaim.
/// Test: `backfill_records_every_pre_existing_worktree`,
/// `backfill_skips_a_worktree_with_no_gitdir_pointer_and_still_succeeds`,
/// `backfill_names_the_skipped_worktree_and_why`.
/// Does `path` carry the gitdir pointer `git worktree add` writes (#7357)?
///
/// Why: a linked worktree's `.git` is a FILE holding `gitdir: <admin dir>`,
/// and that pointer is the only local evidence the directory is a worktree at
/// all. Asking git instead would answer about the ENCLOSING repository — the
/// project checkout — and so would say yes for any directory sitting inside it,
/// including an empty one the harness left behind.
/// What: `true` only when `<path>/.git` reads as a `gitdir:` pointer.
/// Test: `backfill_skips_a_worktree_with_no_gitdir_pointer_and_still_succeeds`.