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
//! Does the project's VCS CLAIM this file? (issue #4448)
//!
//! Why: #4526 was blocked because `ShadowsBundled` + `Untracked` cannot tell a
//! stale leftover from a project's deliberate override — the two are the same
//! classification by construction. `Origin::Project` was going to supply the
//! missing declaration and was closed as superseded (#4443), so the
//! declaration has to come from somewhere the deployer can actually reach.
//!
//! It already does: **a repository that commits a project-tier agent is
//! declaring it.** `git ls-files` is that declaration, readable without any
//! new schema, ledger, or config surface. A tracked file is the project's; a
//! sweep must never move it, no matter what its name or schema says.
//!
//! What: [`VcsIndex::probe`] runs at most two `git` invocations for a WHOLE
//! sweep (not one per file) and answers [`VcsIndex::claim`] from the result.
//! Three states, never a bool — "no repository here" and "git could not be
//! asked" are different facts with opposite safe answers, and collapsing them
//! either freezes the sweep in every non-git project or lets it run blind when
//! git is broken.
//!
//! This module NEVER writes. Both invocations are read-only queries, and
//! neither is passed any caller-controlled argument beyond the directory.
//!
//! Test: `vcs_claim_tests.rs`.
use BTreeSet;
use Path;
use Command;
/// The only git stderr CONSISTENT with "there is genuinely no repository here".
///
/// Why: the parenthesised clause is load-bearing. Git emits
/// `fatal: not a git repository: (null)` for a STALE WORKTREE POINTER, so the
/// shorter phrase `not a git repository` matches a broken repo and a
/// genuinely-absent one alike — matching it would have swept the ~70 worktrees
/// orphaned on 2026-07-21.
///
/// Consistent with, NOT proof of. Git emits this exact text for an unreadable
/// `.git` too, where the repository is real. [`classify_failure`] therefore
/// corroborates it with a filesystem witness before concluding anything; this
/// constant is a necessary condition, never a sufficient one.
///
/// What: verified byte-identical against git 2.54.0's output for an empty temp
/// directory. Any wording drift falls through to [`IndexState::Unavailable`],
/// which refuses — the fail-closed direction.
/// Test: `claim_outside_a_repo_is_unclaimed` (the match),
/// `a_stale_worktree_pointer_is_unknown_not_no_repo` (the near-miss),
/// `an_unreadable_git_dir_is_unknown_not_no_repo` (the same text, real repo).
const NO_REPO_STDERR: &str = "not a git repository (or any of the parent directories)";
/// What the project's VCS says about one file.
///
/// Why: [`Unknown`](Self::Unknown) is not a synonym for
/// [`Unclaimed`](Self::Unclaimed). If git could not be consulted, the honest
/// answer is that a claim cannot be ruled out — and the caller must refuse to
/// move rather than guess. Folding the two together is the fail-open shape
/// this repo has been bitten by before.
/// What: [`Claimed`](Self::Claimed) — the file is tracked;
/// [`Unclaimed`](Self::Unclaimed) — a repository was found and does not track
/// it, or there is no repository at all (nothing can be claiming it);
/// [`Unknown`](Self::Unknown) — git is absent or failed, so no conclusion.
/// Test: `probe_finds_tracked_files`, `claim_of_an_untracked_file`,
/// `claim_outside_a_repo_is_unclaimed`,
/// `claim_is_unknown_when_git_is_unavailable`.
/// Why a failed `rev-parse` failed — the classifier gate 3 turns on.
///
/// Why: git's "no repository" message is NOT proof there is no repository. It
/// emits the exact [`NO_REPO_STDERR`] text whenever discovery never got far
/// enough to conclude otherwise — an unreadable `.git` (mode 000), an
/// unreadable `.git/HEAD`, or `GIT_CEILING_DIRECTORIES` stopping the upward
/// walk. In every one of those the repository is real, its agent files are
/// committed, and trusting the message moves them. #4448 review round 2 executed
/// exactly that and moved a tracked file.
///
/// So the message alone does not decide. This code has a witness git does not
/// use: `symlink_metadata` on a `.git` entry needs only the PARENT's search
/// bit, which is necessarily present — the sweep already listed the tier below
/// it. If any ancestor carries a `.git` and git still says "no repository",
/// the two disagree, and a disagreement is exactly the "cannot be asked" state.
///
/// What: [`IndexState::NoRepo`] only when the message matches AND no ancestor
/// carries a `.git` entry; [`IndexState::Unavailable`] otherwise.
///
/// The path is canonicalised first, and a canonicalisation failure is itself
/// `Unavailable`. A RELATIVE `dir` would otherwise walk a truncated ancestor
/// chain — `.claude/agents` → `.claude` → `""` — miss the project's `.git`
/// entirely, and land back on the permissive answer. That hole is not
/// reachable from today's call sites, which pass absolute paths, but it is one
/// caller away and fails in the dangerous direction.
///
/// Two deliberate over-refusals, both fail-closed and both visible in the
/// report as `VcsUnknown` rather than silent: a stray empty `.git` DIRECTORY
/// that is not a repository, and a project under `GIT_CEILING_DIRECTORIES`.
/// Both refuse to sweep. Refusing to move a file that could have moved is the
/// cheap error here; the expensive one is the reverse.
///
/// Test: `an_unreadable_git_dir_is_unknown_not_no_repo`,
/// `an_unreadable_git_head_is_unknown_not_no_repo`,
/// `a_ceiling_directory_is_unknown_not_no_repo`,
/// `a_stray_empty_git_dir_is_unknown`,
/// `claim_outside_a_repo_is_unclaimed`,
/// `an_unresolvable_relative_path_is_unknown`.
/// The tracked-file set for one directory, resolved once per sweep.
///
/// Why: a per-file `git ls-files --error-unmatch` would spawn a process for
/// every candidate in a hot launch path. One listing answers the whole tier.
/// What: built by [`VcsIndex::probe`]; queried by [`VcsIndex::claim`]. There is
/// deliberately NO constructor that fabricates an empty tracked set — such a
/// constructor is a permissive-by-accident injection point for exactly the gate
/// this type exists to enforce, so tests exercise real repositories instead.
/// Test: `vcs_claim_tests.rs`.