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
//! Bounded NESTED-MANIFEST discovery for marker detection (#7781).
//!
//! Why: probing the project root plus the members a root manifest DECLARES
//! (`super::workspace`) still misses every stack a repository keeps one
//! directory down without declaring it. trusty-tools itself is the worked
//! example: a Cargo workspace whose Svelte/TypeScript UIs live in
//! `crates/*/ui`, declared by no root `workspaces` key, so root-only detection
//! answered `rust-engineer` and nothing else and the PM was primed to route
//! front-end work to a Rust specialist. Owner ruling 2026-09-13: root-only
//! stack detection is not enough.
//! What: [`nested_probe_roots`] walks the project tree breadth-first to
//! [`MAX_NESTED_DEPTH`] and returns every directory carrying a file that any
//! declared marker names — the ANCHOR set, derived from the manifest itself
//! ([`MarkerAnchors::from_categories`]) so a new marker needs no edit here.
//!
//! **Bounds, and why these numbers.** Session launch waits on this walk, so
//! every dimension is capped and every cap fails closed — an unscanned
//! directory is simply a directory with no markers, never an error. Failing
//! closed SILENTLY was the round-1 review finding, so every bound now reports
//! itself. #7781 round-3 splits that report in two, because the two kinds of
//! bound mean different things: a RESOURCE cap stopping the walk is an
//! exceptional, act-on-it event ([`NestedProbe::truncated`], WARN), while
//! stopping at the declared depth is this design's ordinary scope and trips on
//! most real repositories ([`NestedProbe::depth_limited`], DEBUG). One flag
//! covering both was true nearly always, so a consumer could not fail closed
//! on it.
//!
//! | Bound | Value | Rationale |
//! |---|---|---|
//! | Depth | [`MAX_NESTED_DEPTH`] = 4 | `crates/<crate>/ui`, `apps/<app>/web`, `packages/<pkg>/src-tauri` all sit at depth ≤ 3. Depth 4 covers them with one level of headroom. |
//! | Directories scanned | [`MAX_SCANNED_DIRS`] = 4096 | One `read_dir` each. trusty-tools, the largest repo on hand, has 617 directories within depth 4. |
//! | Probe roots | [`super::workspace::MAX_WORKSPACE_MEMBERS`] | Shared with declared members, so the two discovery paths cannot together exceed the cap either one respects alone. |
//!
//! Skipped outright: dependency, build output, and fixture trees
//! ([`SKIP_DIR_NAMES`]), every dot-directory (`.git`, `.claude/worktrees`,
//! `.venv`, a nested `.trusty-mpm`), and every plain directory name the root
//! `.gitignore` lists.
//!
//! #7809: [`NestedProbe::truncated`] and [`NestedProbe::depth_limited`] are
//! correct as an in-process boolean OR for one detection call, but a caller
//! that PERSISTS this walk's answer — e.g. a negative `skillOverrides` entry
//! for a stack this walk did not find — must carry the truncation state
//! alongside the boolean rather than dropping it, or budget exhaustion is
//! written to disk as an indistinguishable "genuinely absent". Applying that
//! at the `skillOverrides` write site is tracked separately (#7781).
//! Test: `crates/trusty-mpm/src/core/manifest/nested_tests.rs`.
use BTreeSet;
use ;
use ;
use ;
/// Deepest directory level the nested walk descends to, project root = 0.
pub const MAX_NESTED_DEPTH: usize = 4;
/// Maximum directories the nested walk reads for one detection call.
pub const MAX_SCANNED_DIRS: usize = 4096;
/// Directory names never descended into, whatever the project declares.
///
/// Why: two kinds of tree carry marker files that are not this project's stack.
/// Dependency and build output holds a vendored third party's manifests —
/// `node_modules` alone can hold tens of thousands of `package.json` files. A
/// FIXTURE tree holds manifests written to be parsed by a test: #7781 measured
/// this repo and `crates/trusty-analyze/testdata/csharp` alone put
/// `dotnet-engineer` in the roster of a Rust workspace, which is precisely the
/// delegation-surface noise the gates exist to remove (#1941).
/// What: matched by exact directory name at every depth. Dot-directories are
/// excluded separately by [`SkipDirs::skips`], which covers `.git`,
/// `.claude/worktrees`, and every other hidden tree in one rule.
const SKIP_DIR_NAMES: & = &;
/// The file names a project's declared markers anchor on.
///
/// Why: the walk must decide "is this directory a project?" without a second,
/// hand-maintained copy of the marker table — the exact drift #4765 removed
/// from `project_lang`. Deriving the anchors from the SAME [`AgentCategories`]
/// the detection then evaluates means a marker added to
/// `framework-manifest.toml` is discovered nested with no code change here.
/// What: the first path segment of every declared marker, with any `::needle`
/// content-probe tail stripped — `package.json::"svelte"` anchors on
/// `package.json`, `src-tauri/tauri.conf.json` on `src-tauri`. Names carrying a
/// `*` are kept as globs and matched with the manifest's own glob vocabulary.
/// Test: `anchors_cover_every_bundled_marker`, `anchor_globs_match_dotnet`.
pub
/// Directory names the walk refuses to descend into for this project.
///
/// Why: the fixed list covers what every ecosystem generates; the root
/// `.gitignore` covers what THIS project generates, which no fixed list can
/// know. Reading it is one bounded file read and removes a whole class of
/// false positives (a build tree whose copied `package.json` is not a project).
/// What: [`SKIP_DIR_NAMES`] plus every plain directory name the root
/// `.gitignore` lists. Only bare names are taken — an entry with a path
/// separator, a glob, or a `!` negation is left to git, because honouring it
/// would mean implementing gitignore semantics rather than reading a name.
/// Test: `gitignored_directory_is_skipped`, `gitignore_path_rules_are_ignored`.
;
/// The bare directory names a `.gitignore` body lists.
///
/// Why: factored out so the deliberately narrow parse is testable on its own.
/// What: one name per line, after dropping blanks, comments, and negations, and
/// after stripping a leading and trailing `/`. A leading `**/` is stripped too:
/// `**/target/` is gitignore's spelling of "this bare name, at any depth",
/// which is exactly what this skip set means, and it is the spelling most
/// `.gitignore` files actually use (#7781). A remaining `/`, or any glob
/// metacharacter, still disqualifies the line — those are real path and glob
/// semantics, left to git.
/// Test: `gitignore_path_rules_are_ignored`.
/// One directory's scan: does it anchor a project, and where does the walk go next.
/// Read `dir` once, answering both questions the walk asks of it.
///
/// Why: one `read_dir` per directory is the whole cost model of the walk;
/// asking "is it anchored?" and "what is below it?" separately would double it.
/// What: `None` when the directory cannot be read — fail-closed, identical to an
/// empty directory. Children are sorted so a capped walk is reproducible. A
/// SYMLINK to a directory is not a child: `file_type` does not follow links, so
/// the walk cannot leave the project tree, matching the no-escape rule
/// `super::workspace::expand_member_glob` applies to a declared member pattern.
/// Test: `walk_is_deterministic`, `missing_root_is_not_an_error`,
/// `unreadable_subdirectory_is_skipped_not_an_error`,
/// `directory_symlink_is_not_traversed`.
/// The outcome of one nested walk: what it found, and why it stopped.
///
/// Why: every bound in this module fails closed by returning fewer roots, and a
/// short result is byte-identical to a genuinely shallow tree. Round-1 review of
/// #7781 called that out: a consumer cannot tell "this repo has no nested stack"
/// from "the walk gave up before reaching it". Round-2 answered it with ONE
/// flag, which the depth bound then set on most real repositories — a signal
/// that is nearly always true cannot be failed closed on (#7751), so round-3
/// splits the exceptional case from the designed one.
/// What: the probe roots, plus [`Self::truncated`] for the RESOURCE caps
/// ([`MAX_SCANNED_DIRS`], the shared [`MAX_WORKSPACE_MEMBERS`] ceiling), each
/// beside a `tracing::warn!` naming the bound, and [`Self::depth_limited`] for
/// [`MAX_NESTED_DEPTH`], logged at DEBUG because a deep repository trips it as
/// designed. The two are independent: either, both, or neither can be set.
/// Test: `scanned_dirs_bound_reports_truncation`,
/// `a_small_tree_is_not_truncated`, `depth_bound_stops_the_walk`,
/// `depth_limited_tree_is_not_truncated`, `member_cap_is_shared`,
/// `both_bounds_can_trip_together`, `depth_limit_is_logged_at_debug`.
pub
/// Log that the walk stopped at its declared depth (#7781).
///
/// Why: round-3 — [`MAX_NESTED_DEPTH`] is tripped by ordinary repositories, so
/// warning on it trained operators to ignore the warning that means a resource
/// cap really was exhausted. DEBUG keeps the fact available without claiming
/// anything went wrong.
/// What: one DEBUG naming the depth and the project. The flag it accompanies is
/// [`NestedProbe::depth_limited`], which is informational, never fail-closed.
/// Test: `depth_limit_is_logged_at_debug` captures it through a `LogBuffer`.
/// The nested directories under `project_dir` that carry a declared marker.
///
/// Why: the one entry point [`super::project_lang::MarkerProbe`] calls for
/// undeclared nested projects, so "how deep, how wide, what is skipped" is
/// stated in exactly one place (#7781).
/// What: a breadth-first walk to [`MAX_NESTED_DEPTH`], returning every visited
/// directory below the root that [`MarkerAnchors`] matches, in deterministic
/// (breadth-first, sorted-per-directory) order, plus the two independent
/// [`NestedProbe`] flags described there. `project_dir` itself is never returned
/// — the caller already probes it. `already` carries the roots resolved so far
/// so the shared [`MAX_WORKSPACE_MEMBERS`] ceiling counts both discovery paths
/// together and declared members are not returned twice.
/// Test: `nested_ui_package_is_found`, `depth_bound_stops_the_walk`,
/// `skipped_directories_are_never_probed`, `member_cap_is_shared`,
/// `scanned_dirs_bound_reports_truncation`, `a_small_tree_is_not_truncated`,
/// `depth_limited_tree_is_not_truncated`, `both_bounds_can_trip_together`.
pub
/// The one exit of [`nested_probe_roots`]: log what stopped it, then report it.
///
/// Why: #7781 round-3 review — the walk had three exits, and the two early ones
/// logged their own resource cap but skipped the depth DEBUG, so a walk that hit
/// BOTH bounds reported `depth_limited` in the returned flag and nowhere in the
/// log. One epilogue makes "flag set" and "line logged" impossible to drift
/// apart, whichever bound ended the walk.
/// What: one [`warn_truncated`] when a resource cap ended the walk, naming that
/// cap, and one [`debug_depth_limited`] when children were left unread at the
/// declared depth. Both, either, or neither fire; the returned flags mirror them
/// exactly.
/// Test: `both_bounds_can_trip_together`, `depth_limit_is_logged_at_debug`,
/// `scanned_dirs_bound_reports_truncation`.