Skip to main content

xtask/
greenfield_status.rs

1//! `cargo xtask greenfield-status` — the probe harness (track doc §8, proposal #999).
2//!
3//! Twenty-one probes measuring the tree against `design/bynk-greenfield-compiler.md`:
4//! the twelve in track doc §8, `emit_abi_shapes` (ADR 0310's probe, #999 Decision E —
5//! this slice measures the emit-ABI enumeration guard but does not wire it; wiring is
6//! packaging-track work), phase 7's own four — `ts_writes`, `ts_any`,
7//! `verbatim_origins`, `verbatim_sites` (P7.0/#1296, P7.5/#1307 — see phase 7's own
8//! closing summary in `design/archive/retired-tracks.md`) — and phase 8's own two,
9//! `incremental_query_types` and `keystroke_latency` (P8.0/#1510, settled by #1509's
10//! Q5/ADR 0414 — see `design/tracks/incrementality.md` §5), plus the IR cutover
11//! track's own adoption probe, `unconsumed_ir_items` (Slice D3 of #1542 — the gate the
12//! 30 August 2026 post-restructuring review's Part 5 §8 asked for), and the
13//! runtime-semantics track's `diagnostic_coverage` (#1662, G2 of #1648).
14//!
15//! **Sixteen are gated**, committed and diffed: `workspace_lints`, `fs_below_driver`,
16//! `options_sources`, `hoist_sinks`, `span_keyed_maps`, `emit_diagnostics`,
17//! `ide_emit_edge`, `ast_importers`, `emit_abi_shapes`, `ts_writes`, `ts_any`,
18//! `verbatim_origins`, `verbatim_sites`, `incremental_query_types`,
19//! `unconsumed_ir_items`, `diagnostic_coverage`. Ten of these are
20//! zero/closure-shaped — a boolean, or a count pinned at a small, argued floor
21//! (`ast_importers` = 5, `emit_abi_shapes` = 1). Phase 7's own four are the same shape:
22//! each converged toward an argued floor over dozens of slices, the same trajectory
23//! `ast_importers` had throughout phase 6's 59 — retired at `ts_writes` = 809,
24//! `ts_any` = 26, `verbatim_origins` = 1, `verbatim_sites` = 2 (phase 7's own closing
25//! summary, `design/archive/retired-tracks.md`), none the literal 0 first proposed —
26//! gated throughout despite the churn that implies, a deliberate call argued in ADR
27//! 0389/ADR 0390 (review of #1297), not an oversight of #999 Decision D's
28//! churn-avoidance principle. `incremental_query_types` is a different shape again —
29//! phase 8's own probe reads a one-time existence proof, not a count trending toward a
30//! floor (settled §5/Q5, ADR 0414), re-settled by #1537 to certify that the two
31//! levels that never found a consumer stay deleted; see its own doc comment. A
32//! disagreement between a
33//! fresh run and the committed table fails `greenfield_status_table_is_current`
34//! (`xtask/tests/greenfield_status.rs`), which rides both the `test` job (`cargo test
35//! --workspace`, any Rust-touching PR) and the `drift` job's existing `cargo test -p
36//! xtask` (pending/decisions-only PRs) — no new CI wiring (#999 Decision D, which also
37//! explains why a `drift`-job *step* would have been silently skipped on the PRs that
38//! move these probes most).
39//!
40//! A disagreement a pending file acknowledges (its `probes` key) does not fail the
41//! gate: the increment leaves the shared table alone and `cargo xtask stamp --apply`
42//! rewrites the gated rows on `main` in merge order ([`refresh_gated_rows`]). The
43//! acknowledgement still has to be written, and reviewed, in the PR — a gated probe
44//! still cannot move silently — but parallel PRs no longer conflict on these rows.
45//!
46//! **Five are count/ratio trend probes**, recomputed and printed but never diffed:
47//! `wildcard_arms`, `keep_in_sync`, `test_density`, `fixture_kinds`,
48//! `keystroke_latency`. The first four move on nearly any ordinary Rust PR with no
49//! slice actively driving them toward a floor (§8 calls two of them "trends, not
50//! gates"); hard-gating them would make the committed table churn, and conflict, on
51//! routine work for no corresponding benefit. `keystroke_latency` moves on nothing
52//! yet — settled (Q3/ADR 0414) as staying "not measured" for phase 8's own whole
53//! lifetime, since no scheduler ships this phase to produce a real number; reported
54//! anyway so the trajectory's own §3.0 baseline has a live, CI-computed row.
55//!
56//! `Closes-Rule:` rule-id provenance (#999 Decision B) is deferred to a follow-on
57//! slice — the committed table below carries no rule-citation column yet.
58
59use std::collections::BTreeSet;
60use std::fmt::Write as _;
61use std::path::{Path, PathBuf};
62use std::process::Command;
63
64/// One probe's result. `gated` probes are diffed against the committed table by
65/// [`crate::greenfield_status::gated_disagreements`]; the rest are reported only.
66pub struct Probe {
67    pub name: &'static str,
68    pub gated: bool,
69    pub reads: String,
70}
71
72pub struct Report {
73    pub probes: Vec<Probe>,
74}
75
76impl Report {
77    pub fn get(&self, name: &str) -> &str {
78        self.probes
79            .iter()
80            .find(|p| p.name == name)
81            .map(|p| p.reads.as_str())
82            .unwrap_or_else(|| panic!("no probe named {name:?}"))
83    }
84}
85
86/// Run every probe against the tree rooted at `root` (the repo root). Used by the CLI's
87/// full report; the gating test uses the sixteen gated probes alone
88/// ([`gated_disagreements`]) so it never pays for a workspace-wide clippy pass
89/// (`wildcard_arms`) just to check the probes that are actually diffed.
90pub fn run(root: &Path) -> Report {
91    let mut probes = run_gated(root);
92    probes.extend(run_trend(root));
93    Report { probes }
94}
95
96/// The gated probes' names, in [`run_gated`]'s order — what a pending file's
97/// `probes` key may cite. `greenfield_status_table_is_current` asserts this
98/// matches the probes [`run_gated`] actually returns, so the two cannot drift.
99pub const GATED_PROBES: [&str; 16] = [
100    "workspace_lints",
101    "fs_below_driver",
102    "options_sources",
103    "hoist_sinks",
104    "span_keyed_maps",
105    "emit_diagnostics",
106    "ide_emit_edge",
107    "ast_importers",
108    "emit_abi_shapes",
109    "ts_writes",
110    "ts_any",
111    "verbatim_origins",
112    "verbatim_sites",
113    "incremental_query_types",
114    "unconsumed_ir_items",
115    "diagnostic_coverage",
116];
117
118/// The sixteen gated (zero/closure) probes only — what [`gated_disagreements`] diffs.
119pub fn run_gated(root: &Path) -> Vec<Probe> {
120    vec![
121        workspace_lints(root),
122        fs_below_driver(root),
123        options_sources(root),
124        hoist_sinks(root),
125        span_keyed_maps(root),
126        emit_diagnostics(root),
127        ide_emit_edge(root),
128        ast_importers(root),
129        emit_abi_shapes(root),
130        ts_writes(root),
131        ts_any(root),
132        verbatim_origins(root),
133        verbatim_sites(root),
134        incremental_query_types(root),
135        unconsumed_ir_items(root),
136        diagnostic_coverage(root),
137    ]
138}
139
140/// The five reported-only trend probes — never diffed, and notably including the one
141/// (`wildcard_arms`) that shells out to a full `cargo clippy --workspace` pass, which
142/// the gating test must not pay for on every run.
143fn run_trend(root: &Path) -> Vec<Probe> {
144    vec![
145        wildcard_arms(root),
146        keep_in_sync(root),
147        test_density(root),
148        fixture_kinds(root),
149        keystroke_latency(root),
150    ]
151}
152
153/// `design/greenfield-status.md` — the committed table this probe set regenerates.
154pub fn table_path(root: &Path) -> PathBuf {
155    root.join("design/greenfield-status.md")
156}
157
158// --- Filesystem helpers --------------------------------------------------
159
160/// Every `.rs` file under `dir`, recursively, as `(path, contents)`. Unreadable files
161/// (permissions, non-UTF-8) are skipped rather than failing the whole walk — this is a
162/// measurement tool, not a build step.
163fn rust_files(dir: &Path) -> Vec<(PathBuf, String)> {
164    let mut out = Vec::new();
165    walk(dir, &mut out);
166    out
167}
168
169fn walk(dir: &Path, out: &mut Vec<(PathBuf, String)>) {
170    let Ok(entries) = std::fs::read_dir(dir) else {
171        return;
172    };
173    let mut entries: Vec<_> = entries.flatten().collect();
174    entries.sort_by_key(|e| e.file_name());
175    for entry in entries {
176        let path = entry.path();
177        if path.is_dir() {
178            walk(&path, out);
179        } else if path.extension().is_some_and(|e| e == "rs")
180            && let Ok(contents) = std::fs::read_to_string(&path)
181        {
182            out.push((path, contents));
183        }
184    }
185}
186
187/// The inner text of every **standalone** `"bynk.<ident>"` string literal (the
188/// `bynk.*` convention used for diagnostic codes and commons/namespace paths alike).
189///
190/// Standalone, not merely prefix-matching: the identifier run must be immediately
191/// followed by the closing quote, matching the naive `rg -o '"bynk\.[a-zA-Z0-9_.]*"'`
192/// this probe is deliberately more careful than (#999 Decision A). Without that
193/// requirement this would also match the *start* of an unrelated, longer message that
194/// merely happens to begin with "bynk." — e.g. a panic string
195/// `"bynk.map itself uses bynk.list, so list must be injected too: {paths:?}"` is prose
196/// beginning with a namespace-shaped word, not a `"bynk.map"` code literal, and a
197/// dev-only compile-time error message split across lines with a `\`-continuation
198/// (`"bynk.emit.unresolved_cross_context_signature: no signature for \` ...) is one
199/// string, not a diagnostic-code literal, even though its first segment matches the
200/// identifier charset. Both were found — and wrongly counted — by an earlier,
201/// less careful version of this scan; the fix is requiring the closing quote.
202fn bynk_dotted_literals(src: &str) -> Vec<&str> {
203    let mut out = Vec::new();
204    let bytes = src.as_bytes();
205    let mut i = 0;
206    while let Some(rel) = src[i..].find("\"bynk.") {
207        let start = i + rel + 1; // skip the opening quote
208        let mut end = start;
209        while end < bytes.len()
210            && (bytes[end].is_ascii_alphanumeric() || bytes[end] == b'_' || bytes[end] == b'.')
211        {
212            end += 1;
213        }
214        if end < bytes.len() && bytes[end] == b'"' {
215            out.push(&src[start..end]);
216        }
217        i = end.max(start + 1);
218    }
219    out
220}
221
222/// True if `line`, trimmed, is a `//` or `///` or `//!` line comment. Doesn't attempt
223/// block comments (`/* */`) — none of this codebase's `bynk.*`/dead-identifier
224/// mentions live in one.
225fn is_line_comment(line: &str) -> bool {
226    line.trim_start().starts_with("//")
227}
228
229// --- Gated probe 1: workspace_lints --------------------------------------
230
231/// R2.12. `[workspace.lints]` presence and `clippy::wildcard_enum_match_arm`'s level in
232/// the root `Cargo.toml`. A boolean-shaped probe (not a count) — gated because it only
233/// ever changes once, when T0.3 lands it.
234fn workspace_lints(root: &Path) -> Probe {
235    let cargo_toml = std::fs::read_to_string(root.join("Cargo.toml")).unwrap_or_default();
236    let has_section = cargo_toml
237        .lines()
238        .any(|l| l.trim() == "[workspace.lints.clippy]" || l.trim() == "[workspace.lints]");
239    let level = cargo_toml
240        .lines()
241        .find(|l| l.contains("wildcard_enum_match_arm"))
242        .map(|l| l.trim().to_string());
243    let reads = match (has_section, level) {
244        (true, Some(l)) => format!("present — {l}"),
245        (true, None) => "present, wildcard_enum_match_arm not set".to_string(),
246        (false, _) => "absent".to_string(),
247    };
248    Probe {
249        name: "workspace_lints",
250        gated: true,
251        reads,
252    }
253}
254
255// --- Gated probe 2: fs_below_driver --------------------------------------
256
257/// R2.3. Files under `bynk-emit/src`, `bynk-ide/src`, `bynk-fmt/src` (the crates below
258/// the `bynk` driver, which owns disk I/O) that touch `std::fs` in **production** code.
259///
260/// Excludes usage inside a trailing `#[cfg(test)] mod tests { ... }` block — the
261/// convention every file in this codebase uses, always the last item in the file. A
262/// line is production-scope unless it falls at or after the line following a
263/// `#[cfg(test)]` attribute whose very next non-empty line opens a `mod ... {` block
264/// (as opposed to a `mod name;` external-file declaration, which is not a scope at
265/// all). This mirrors the comment-exclusion discipline elsewhere in this probe set:
266/// tests writing fixtures to a tempdir are not "the driver's job" bypassed, and
267/// counting them would report a rule open that the production code has already closed.
268///
269/// A file counts if its own text names `std::fs` ([`has_production_std_fs`]), **or** if
270/// a bare `fs::`-style call site in it resolves to `std::fs` through its imports
271/// ([`production_std_fs_files`]) — a module-level `use std::fs;` in a parent module is
272/// visible to a child through `use super::*;` (module privacy is ancestor-scoped), so
273/// `bynk-emit/src/project/discovery.rs` reads and walks the filesystem while never
274/// spelling `std::fs` itself. The literal text scan alone missed exactly that file,
275/// so a probe reading `bynk-emit=0` would have asserted R2.3 closed on a false
276/// premise (#1013).
277///
278/// #1104 (a content-ownership (#1086) probe-precision follow-on): a flagged *count*
279/// alone can't tell a residual R2.3 violation from a documented, permanent exception —
280/// `bynk-emit` read 3 that way from the track's retirement (`design/archive/
281/// retired-tracks.md`'s closing summary) until P4.0 moved all three named files out of
282/// `bynk-emit` entirely, leaving [`NAMED_FS_EXCEPTIONS`] empty (#1561) — the mechanism
283/// stays live for whatever named exception is decided next. So each flagged file is
284/// additionally classified as a **named floor** file — every
285/// production-scope touch it has is either inside one of those named functions, or is
286/// a bare import declaration (no fn encloses it — [`enclosing_fn`] returns `None`) that
287/// performs no I/O of its own, existing only so a *descendant* module's bare `fs::`
288/// call can resolve (the motivating case, #1013: `project.rs`'s own `use std::fs;`,
289/// which `discovery.rs` and `paths.rs` glob-imported via `use super::*;` before P4.0
290/// moved both files out) — or a **residual** file: any other file
291/// touching `std::fs` in production scope, which still reads as a real R2.3 violation
292/// ([`file_is_named_fs_floor`]).
293fn fs_below_driver(root: &Path) -> Probe {
294    let crates = ["bynk-emit", "bynk-ide", "bynk-fmt"];
295    let mut per_crate = Vec::new();
296    let mut total = 0usize;
297    let mut total_floor = 0usize;
298    for krate in crates {
299        let dir = root.join(krate).join("src");
300        let files: Vec<(PathBuf, String)> = rust_files(&dir)
301            .into_iter()
302            .map(|(path, contents)| {
303                let rel = path.strip_prefix(&dir).unwrap_or(&path).to_path_buf();
304                (rel, contents)
305            })
306            .collect();
307        let flagged = production_std_fs_files(&files);
308        let count = flagged.len();
309        total += count;
310        let facts: Vec<FsImportFacts> = files.iter().map(|(_, s)| fs_import_facts(s)).collect();
311        let parents: Vec<Option<usize>> = files
312            .iter()
313            .map(|(p, _)| module_parent(p, &files))
314            .collect();
315        let floor = flagged
316            .iter()
317            .filter(|&&i| {
318                file_is_named_fs_floor(krate, &files, &facts, &parents, i, NAMED_FS_EXCEPTIONS)
319            })
320            .count();
321        total_floor += floor;
322        let residual = count - floor;
323        per_crate.push(if floor > 0 {
324            format!("{krate}={count} ({floor} named floor, {residual} residual)")
325        } else {
326            format!("{krate}={count}")
327        });
328    }
329    Probe {
330        name: "fs_below_driver",
331        gated: true,
332        reads: format!(
333            "{total} files ({}) — {total_floor} named floor, {} residual total",
334            per_crate.join(", "),
335            total - total_floor
336        ),
337    }
338}
339
340/// #1104: the specific, permanently-carved-out production functions whose
341/// `std::fs` touch is a *named* exception, not evidence of unfinished R2.3
342/// migration — settled in `design/tracks/content-ownership.md` §3.2 (retired) and
343/// its closing summary in `design/archive/retired-tracks.md`. `(crate, file path
344/// relative to that crate's `src/`, enclosing production fn name)`. A future
345/// carve-out decided the same deliberate way joins this list; anything touching
346/// `std::fs` in production scope that isn't listed here reads as a residual R2.3
347/// violation, per [`file_is_named_fs_floor`].
348///
349/// Empty since #1561: these three entries were real when #1104 (`769a60a3`,
350/// 6 Aug 2026) added them, but P4.0 (`69af8f2d`, the very next day) moved
351/// `discover_bynk_files`/`read_adapter_binding` (`project/discovery.rs`) and
352/// `try_read_project_paths` (`project/paths.rs`) out of `bynk-emit` into
353/// `bynk-project` entirely — dead from that point on, unnoticed for weeks.
354/// `fs_below_driver`'s `bynk-emit` reading was already 0 with them present
355/// (nothing in the tree matched the dead tuples), confirmed unchanged with
356/// them gone.
357const NAMED_FS_EXCEPTIONS: &[(&str, &str, &str)] = &[];
358
359/// Is flagged file `files[i]` (already known, by [`production_std_fs_files`], to touch
360/// `std::fs` in production scope) a **named floor** file — every production-scope touch
361/// it has is either inside a [`NAMED_FS_EXCEPTIONS`] function for this exact
362/// `(krate, file)`, or a bare `use` import declaration (which reads but performs no
363/// filesystem operation by itself, unlike a module-scope `static`/`const` initialiser or
364/// macro invocation that might)? `facts`/`parents` are the caller's already-computed
365/// [`fs_import_facts`]/[`module_parent`] vectors for `files`, threaded through rather
366/// than recomputed per flagged file.
367///
368/// A single disallowed touch — inside an unlisted fn, inside a listed fn's *file* but
369/// wrong *name*, or outside every fn and not a plain import — makes the whole file
370/// residual: partial credit isn't meaningful here, since the point is "can a reader stop
371/// cross-referencing track docs for this file," not a ratio. Likewise, a file this
372/// function attributes *no* touch line to at all (despite the caller already knowing it's
373/// flagged — [`line_touches_std_fs`]'s re-implementation of the file-level detection
374/// disagreeing with it) reads as residual, not floor: an unattributable touch means this
375/// classifier doesn't understand the file, which must fail loud, not quiet.
376///
377/// `exceptions` is [`NAMED_FS_EXCEPTIONS`] at the one production call site
378/// ([`fs_below_driver`]) — parameterised (#1561) so a test can exercise the
379/// matching logic against a synthetic tuple instead of real, currently-empty
380/// production data, the same way `facts`/`parents` are caller-supplied rather
381/// than recomputed.
382fn file_is_named_fs_floor(
383    krate: &str,
384    files: &[(PathBuf, String)],
385    facts: &[FsImportFacts],
386    parents: &[Option<usize>],
387    i: usize,
388    exceptions: &[(&str, &str, &str)],
389) -> bool {
390    let (path, _) = &files[i];
391    let rel = path.to_string_lossy().replace('\\', "/");
392    let lines: Vec<&str> = files[i].1.lines().collect();
393    let ranges = test_mod_ranges(&lines);
394    let fn_ranges = production_fn_ranges(&lines, &ranges);
395
396    let mut saw_touch = false;
397    for (li, line) in lines.iter().enumerate() {
398        if in_test_range(li, &ranges) {
399            continue;
400        }
401        if !line_touches_std_fs(i, line, facts, parents, files) {
402            continue;
403        }
404        saw_touch = true;
405        let Some(fn_name) = enclosing_fn(li, &fn_ranges) else {
406            // No enclosing fn is harmless only when the line is literally an import
407            // declaration. A module-scope `static`/`const` initialiser, a macro
408            // invocation, or a fn shape `fn_name_on_line` can't parse (`extern "C" fn`)
409            // does real I/O outside every known range and must read as residual.
410            if use_declaration(line).is_some() {
411                continue;
412            }
413            return false;
414        };
415        let named = exceptions
416            .iter()
417            .any(|&(c, f, func)| c == krate && f == rel && func == fn_name);
418        if !named {
419            return false;
420        }
421    }
422    saw_touch
423}
424
425/// Does `line` (already known to be production-scope) itself touch `std::fs` — by the
426/// same two means [`production_std_fs_files`] checks at file granularity, applied here
427/// to one line: a literal `std::fs` substring, or a bare/qualified path this line spells
428/// that resolves to `std::fs` through file `i`'s visible import bindings.
429fn line_touches_std_fs(
430    i: usize,
431    line: &str,
432    facts: &[FsImportFacts],
433    parents: &[Option<usize>],
434    files: &[(PathBuf, String)],
435) -> bool {
436    if line.contains("std::fs") {
437        return true;
438    }
439    let mut roots = BTreeSet::new();
440    collect_bare_path_roots(line, &mut roots);
441    if roots.iter().any(|name| {
442        matches!(
443            resolve_name_in_module(i, name, facts, parents),
444            NameResolution::StdFs
445        )
446    }) {
447        return true;
448    }
449    let mut chains = BTreeSet::new();
450    collect_qualified_paths(line, &mut chains);
451    chains
452        .iter()
453        .any(|chain| qualified_chain_reaches_std_fs(chain, i, facts, parents, files))
454}
455
456/// The name and inclusive body line-range of every production-scope `fn` in `lines`
457/// (`test_ranges` excluded, same as everywhere else in this probe) — used by
458/// [`file_is_named_fs_floor`] to attribute a flagged touch line to its enclosing
459/// function. A wrapped signature (the `{` arriving lines after the `fn` line, past a
460/// multi-line parameter list) is handled the same way [`test_mod_ranges`] handles a
461/// `mod` line: brace depth is tracked starting at the `fn` line itself, but a parameter
462/// list has no `{`/`}` in it, so `started` only flips true once the real body-opening
463/// brace arrives, however many lines later.
464fn production_fn_ranges(
465    lines: &[&str],
466    test_ranges: &[(usize, usize)],
467) -> Vec<(String, usize, usize)> {
468    let mut out = Vec::new();
469    for (i, line) in lines.iter().enumerate() {
470        if in_test_range(i, test_ranges) {
471            continue;
472        }
473        let Some(name) = fn_name_on_line(line) else {
474            continue;
475        };
476        let mut state = BraceScanState::Normal;
477        let mut depth = 0i32;
478        let mut started = false;
479        let mut end = lines.len() - 1;
480        for (j, l) in lines[i..].iter().enumerate() {
481            let (delta, new_state) = brace_delta(l, state);
482            state = new_state;
483            depth += delta;
484            if depth != 0 {
485                started = true;
486            }
487            if started && depth == 0 {
488                end = i + j;
489                break;
490            }
491        }
492        out.push((name, i, end));
493    }
494    out
495}
496
497/// The leading `fn NAME` on `line`, past an optional `pub`/`pub(...)`, `async`,
498/// `unsafe`, `const` modifier run (in any order/repetition, mirroring
499/// [`collect_declared_type_name`]'s `pub`-stripping) — `None` if `line` doesn't open a
500/// function at all (a call site, a doc comment mentioning "fn", a closure). Doesn't
501/// require a trailing `{` or even `(` on this same line — a wrapped signature's `fn`
502/// line can end right at the name.
503fn fn_name_on_line(line: &str) -> Option<String> {
504    let mut t = line.trim();
505    loop {
506        if let Some(rest) = t.strip_prefix("pub") {
507            let rest = rest.trim_start();
508            t = if let Some(after_paren) = rest.strip_prefix('(') {
509                after_paren.split_once(')')?.1.trim_start()
510            } else {
511                rest
512            };
513            continue;
514        }
515        let mut advanced = false;
516        for kw in ["async ", "unsafe ", "const "] {
517            if let Some(rest) = t.strip_prefix(kw) {
518                t = rest.trim_start();
519                advanced = true;
520                break;
521            }
522        }
523        if !advanced {
524            break;
525        }
526    }
527    let rest = t.strip_prefix("fn ")?;
528    let end = rest
529        .find(|c: char| !c.is_ascii_alphanumeric() && c != '_')
530        .unwrap_or(rest.len());
531    if end == 0 {
532        return None;
533    }
534    Some(rest[..end].to_string())
535}
536
537/// The innermost [`production_fn_ranges`] entry containing `line_idx`, by name — `None`
538/// if `line_idx` sits outside every production fn (module scope: a `use` declaration,
539/// a `const`/`static`, or a `struct`/`enum` body).
540fn enclosing_fn(line_idx: usize, fn_ranges: &[(String, usize, usize)]) -> Option<String> {
541    fn_ranges
542        .iter()
543        .filter(|(_, start, end)| line_idx >= *start && line_idx <= *end)
544        .min_by_key(|(_, start, end)| end - start)
545        .map(|(name, _, _)| name.clone())
546}
547
548/// The literal text component of [`fs_below_driver`]: some production-scope line names
549/// `std::fs`. Necessary but not sufficient (#1013) — a file can touch `std::fs`
550/// through a glob-imported parent binding without ever spelling it; that resolution
551/// lives in [`production_std_fs_files`], which layers on top of this scan.
552fn has_production_std_fs(src: &str) -> bool {
553    let lines: Vec<&str> = src.lines().collect();
554    let ranges = test_mod_ranges(&lines);
555    for (i, line) in lines.iter().enumerate() {
556        if in_test_range(i, &ranges) {
557            continue;
558        }
559        if line.contains("std::fs") {
560            return true;
561        }
562    }
563    false
564}
565
566/// Indices (into `files`, whose paths are relative to the crate's `src/` root) of the
567/// files that touch `std::fs` in production code — the union of the literal text scan
568/// ([`has_production_std_fs`]) and import resolution: a path whose leading module
569/// segment a production `use` declaration binds to `std::fs` (or an item under it),
570/// either a bare `NAME::` root resolved in the file itself (`use std::{fs, io};` — a
571/// form the substring scan can't see) or in an ancestor module reached through
572/// `use super::*;`, transitively (#1013), or a `super::`/`self::`/`crate::`-qualified
573/// path walked through the module tree to the same bindings (#1016 review — a
574/// descendant may spell `super::fs::read_to_string(p)` with no glob import at all,
575/// one disambiguating edit away from a currently-flagged bare call).
576///
577/// Resolution is Rust-shaped, not hand-tracked (#1013 rejects a special-case list):
578/// a private `use std::fs;` in a parent is visible to descendants because module
579/// privacy is ancestor-scoped, a chain of `use super::*;` globs re-reaches it from
580/// any depth, and a nearer binding of the same name shadows a farther one — whether
581/// that binding is another `use` or a locally-declared type-namespace item (`mod fs;`,
582/// `struct File`, …; value-namespace items like `fn` can't head a `NAME::` path, so
583/// they don't shadow one) — so a child that binds `fs` to something else keeps its
584/// bare `fs::` calls unflagged. Visibility is *not* modelled: a path that names a
585/// too-private binding wouldn't compile anyway, so over-approximating is safe.
586///
587/// Known remaining gaps, accepted as out of reach for a text-level scanner: an
588/// ancestor's `use std::fs::read_to_string;` item import called bare (`read_to_string(p)`)
589/// presents no `::` path segment to resolve — the same import used as a path root
590/// (`File::open`) **is** caught, since item bindings under `std::fs` participate in
591/// the same resolution — and a `use` declaration rustfmt has split across lines is
592/// not parsed. #1013 grepped the three scanned crates for the item-import form, and
593/// the #1016 review for the qualified-path and split-declaration forms — zero hits.
594fn production_std_fs_files(files: &[(PathBuf, String)]) -> Vec<usize> {
595    let facts: Vec<FsImportFacts> = files.iter().map(|(_, src)| fs_import_facts(src)).collect();
596    let parents: Vec<Option<usize>> = files
597        .iter()
598        .map(|(path, _)| module_parent(path, files))
599        .collect();
600    (0..files.len())
601        .filter(|&i| {
602            has_production_std_fs(&files[i].1)
603                || resolves_bare_std_fs(i, &facts, &parents)
604                || resolves_qualified_std_fs(i, &facts, &parents, files)
605        })
606        .collect()
607}
608
609/// Per-file production-scope import facts for [`production_std_fs_files`]'s
610/// resolution. All fields exclude `#[cfg(test)] mod` ranges — a test module's
611/// `use super::*;` or tempdir `fs::write` must not make the file, or its children,
612/// read as production `std::fs` (the `bynk-ide` files' shape).
613#[derive(Default)]
614struct FsImportFacts {
615    /// A production `use super::*;` (optionally `pub`-qualified) — the edge that lets
616    /// this file see its parent module's `use` bindings, and (chained) its ancestors'.
617    glob_imports_super: bool,
618    /// Names production `use` declarations bind to `std::fs` or an item under it:
619    /// `use std::fs;` → `fs`, `use std::fs as x;` → `x`, `use std::{fs, io};` → `fs`,
620    /// `use std::fs::File;` → `File`.
621    std_fs_bindings: BTreeSet<String>,
622    /// Every name a production `use` declaration binds, whatever the target — the
623    /// shadow set: a nearer non-`std::fs` binding of a candidate name stops resolution.
624    use_bound_names: BTreeSet<String>,
625    /// Type-namespace items the file declares (`mod fs;`, `struct File`, `enum`,
626    /// `trait`, `type`, `union`) — these beat a glob-imported name in real Rust, so
627    /// they join [`Self::use_bound_names`] on the shadow side of resolution (#1016
628    /// review). Value-namespace items (`fn`, `const`, `static`) can't head a `NAME::`
629    /// module path and are deliberately not collected.
630    declared_type_names: BTreeSet<String>,
631    /// Identifiers appearing as a bare path root `NAME::` (not preceded by another
632    /// path segment) on a production line — the call-site side of the resolution.
633    bare_path_roots: BTreeSet<String>,
634    /// Segment chains of `super::`/`self::`/`crate::`-qualified paths on production
635    /// lines — `super::fs::read_to_string` records `["super", "fs", "read_to_string"]`.
636    /// These need no glob import to reach an ancestor's binding (#1016 review).
637    qualified_paths: BTreeSet<Vec<String>>,
638}
639
640fn fs_import_facts(src: &str) -> FsImportFacts {
641    let lines: Vec<&str> = src.lines().collect();
642    let ranges = test_mod_ranges(&lines);
643    let mut facts = FsImportFacts::default();
644    for (i, line) in lines.iter().enumerate() {
645        if in_test_range(i, &ranges) {
646            continue;
647        }
648        if let Some(decl) = use_declaration(line) {
649            if decl == "super::*" {
650                facts.glob_imports_super = true;
651            }
652            collect_use_bindings("", decl, &mut facts);
653        }
654        collect_declared_type_name(line, &mut facts.declared_type_names);
655        collect_bare_path_roots(line, &mut facts.bare_path_roots);
656        collect_qualified_paths(line, &mut facts.qualified_paths);
657    }
658    facts
659}
660
661/// The path text of a single-line `use` declaration — `use std::fs;` → `std::fs`,
662/// with an optional `pub`/`pub(crate)`/`pub(in …)` prefix stripped and a trailing
663/// `//` comment tolerated (`use super::*; // parent's fs` must not silently sever
664/// the glob edge for a whole subtree — #1016 review; safe to split on `//` because
665/// a `use` path can contain neither a comment marker nor a string). A declaration
666/// rustfmt has split across lines has no trailing `;` here and is not recognised —
667/// none of the `std::fs` forms in the scanned crates are long enough to split.
668fn use_declaration(line: &str) -> Option<&str> {
669    let mut t = line.trim();
670    if let Some(rest) = t.strip_prefix("pub") {
671        let rest = rest.trim_start();
672        t = if let Some(after_paren) = rest.strip_prefix('(') {
673            after_paren.split_once(')')?.1.trim_start()
674        } else {
675            rest
676        };
677    }
678    let body = t.strip_prefix("use ")?;
679    let body = body.split("//").next().unwrap_or(body);
680    body.trim().strip_suffix(';').map(str::trim)
681}
682
683/// If `line` declares a type-namespace item — `mod`/`struct`/`enum`/`trait`/`type`/
684/// `union`, optionally `pub`-qualified, optionally `unsafe` (traits) — record its
685/// name. Field/variable positions can't start a trimmed line with these keywords, so
686/// a leading-keyword scan is enough for rustfmt-shaped code.
687fn collect_declared_type_name(line: &str, out: &mut BTreeSet<String>) {
688    let mut t = line.trim();
689    if let Some(rest) = t.strip_prefix("pub") {
690        let rest = rest.trim_start();
691        t = if let Some(after_paren) = rest.strip_prefix('(') {
692            match after_paren.split_once(')') {
693                Some((_, after)) => after.trim_start(),
694                None => return,
695            }
696        } else {
697            rest
698        };
699    }
700    if let Some(rest) = t.strip_prefix("unsafe ") {
701        t = rest.trim_start();
702    }
703    for kw in ["mod ", "struct ", "enum ", "trait ", "type ", "union "] {
704        if let Some(rest) = t.strip_prefix(kw) {
705            let rest = rest.trim_start();
706            let end = rest
707                .find(|c: char| !c.is_ascii_alphanumeric() && c != '_')
708                .unwrap_or(rest.len());
709            if end > 0 {
710                out.insert(rest[..end].to_string());
711            }
712            return;
713        }
714    }
715}
716
717/// Record the name(s) a `use` path binds into `facts` — `path::to::name`,
718/// `path as alias`, and brace groups (`std::{fs, path::PathBuf}`, nested one level
719/// per recursion). `prefix` is the already-consumed leading path (empty at the top).
720fn collect_use_bindings(prefix: &str, entry: &str, facts: &mut FsImportFacts) {
721    let entry = entry.trim();
722    if entry.is_empty() {
723        return;
724    }
725    if let Some((path_part, group)) = entry.split_once('{') {
726        let inner_prefix = join_use_path(prefix, path_part.trim().trim_end_matches("::"));
727        let group = group.strip_suffix('}').unwrap_or(group);
728        for part in split_group_entries(group) {
729            collect_use_bindings(&inner_prefix, part, facts);
730        }
731        return;
732    }
733    let (path_part, alias) = match entry.split_once(" as ") {
734        Some((p, a)) => (p.trim(), Some(a.trim())),
735        None => (entry, None),
736    };
737    let full = join_use_path(prefix, path_part);
738    // `use std::fs::{self};` binds `fs` — normalise the `self` leaf away.
739    let full = full.strip_suffix("::self").unwrap_or(&full);
740    let last = full.rsplit("::").next().unwrap_or(full);
741    let name = alias.unwrap_or(last);
742    if name.is_empty() || name == "*" {
743        return; // globs bind no single name; `super::*` is tracked separately
744    }
745    facts.use_bound_names.insert(name.to_string());
746    if full == "std::fs" || full.starts_with("std::fs::") {
747        facts.std_fs_bindings.insert(name.to_string());
748    }
749}
750
751fn join_use_path(prefix: &str, part: &str) -> String {
752    if prefix.is_empty() {
753        part.to_string()
754    } else {
755        format!("{prefix}::{part}")
756    }
757}
758
759/// Split a brace group's contents on top-level commas only — `fs::{self, File}, io`
760/// is two entries, not three.
761fn split_group_entries(s: &str) -> Vec<&str> {
762    let mut out = Vec::new();
763    let mut depth = 0i32;
764    let mut start = 0;
765    for (i, c) in s.char_indices() {
766        match c {
767            '{' => depth += 1,
768            '}' => depth -= 1,
769            ',' if depth == 0 => {
770                out.push(&s[start..i]);
771                start = i + 1;
772            }
773            _ => {}
774        }
775    }
776    out.push(&s[start..]);
777    out
778}
779
780/// Every identifier `NAME` occurring as `NAME::` where the character before `NAME` is
781/// not `:` — i.e. a path *root*, so `std::fs::read` contributes `std`, never `fs`.
782/// Same line discipline as the text scan: comments included, production scope only
783/// (the caller has already excluded test ranges).
784fn collect_bare_path_roots(line: &str, out: &mut BTreeSet<String>) {
785    let bytes = line.as_bytes();
786    let mut search_from = 0;
787    while let Some(rel) = line[search_from..].find("::") {
788        let pos = search_from + rel;
789        let mut start = pos;
790        while start > 0 && (bytes[start - 1].is_ascii_alphanumeric() || bytes[start - 1] == b'_') {
791            start -= 1;
792        }
793        if start < pos && (start == 0 || bytes[start - 1] != b':') {
794            out.insert(line[start..pos].to_string());
795        }
796        search_from = pos + 2;
797    }
798}
799
800/// Every `super::`/`self::`/`crate::`-rooted path on `line`, as its segment chain —
801/// `super::fs::read_to_string(p)` yields `["super", "fs", "read_to_string"]`. The
802/// root must sit at a bare word boundary (not `a_super::` or `a::super::`), so only
803/// genuine path roots are collected; `Self::` (capital) never matches, and `self.x`
804/// has no `::` to match.
805fn collect_qualified_paths(line: &str, out: &mut BTreeSet<Vec<String>>) {
806    let bytes = line.as_bytes();
807    for root in ["super", "self", "crate"] {
808        let mut from = 0;
809        while let Some(rel) = line[from..].find(root) {
810            let start = from + rel;
811            let root_end = start + root.len();
812            from = root_end;
813            let boundary_ok = start == 0 || {
814                let c = bytes[start - 1];
815                !(c.is_ascii_alphanumeric() || c == b'_' || c == b':')
816            };
817            if !boundary_ok || !line[root_end..].starts_with("::") {
818                continue;
819            }
820            let mut segments = vec![root.to_string()];
821            let mut pos = root_end;
822            while line[pos..].starts_with("::") {
823                let seg_start = pos + 2;
824                let mut seg_end = seg_start;
825                while seg_end < bytes.len()
826                    && (bytes[seg_end].is_ascii_alphanumeric() || bytes[seg_end] == b'_')
827                {
828                    seg_end += 1;
829                }
830                if seg_end == seg_start {
831                    break; // `super::*` and friends — no further identifier
832                }
833                segments.push(line[seg_start..seg_end].to_string());
834                pos = seg_end;
835            }
836            if segments.len() >= 2 {
837                out.insert(segments);
838            }
839        }
840    }
841}
842
843/// The file defining `path`'s parent module, by the standard layout: `a/b.rs`'s parent
844/// is `a.rs` (or `a/mod.rs`), `a/mod.rs`'s parent is the crate root, and the roots
845/// (`lib.rs`/`main.rs`) have none. `#[path]`-remapped modules are not handled — none
846/// exist below the driver, and a text-level probe can't chase them anyway.
847fn module_parent(path: &Path, files: &[(PathBuf, String)]) -> Option<usize> {
848    let stem = path.file_stem()?.to_str()?;
849    let dir = path.parent().filter(|d| !d.as_os_str().is_empty());
850    let parent_module: PathBuf = if stem == "mod" {
851        dir?.parent().map(Path::to_path_buf).unwrap_or_default()
852    } else if let Some(dir) = dir {
853        dir.to_path_buf()
854    } else {
855        if stem == "lib" || stem == "main" {
856            return None;
857        }
858        PathBuf::new()
859    };
860    let candidates = if parent_module.as_os_str().is_empty() {
861        vec![PathBuf::from("lib.rs"), PathBuf::from("main.rs")]
862    } else {
863        vec![
864            parent_module.with_extension("rs"),
865            parent_module.join("mod.rs"),
866        ]
867    };
868    candidates
869        .iter()
870        .find_map(|c| files.iter().position(|(p, _)| p == c))
871}
872
873/// The scopes whose bindings a name used in module `i` can see: the module itself,
874/// then each ancestor reachable while every module below it glob-imports `super::*`.
875fn visible_scopes(i: usize, facts: &[FsImportFacts], parents: &[Option<usize>]) -> Vec<usize> {
876    let mut scopes = vec![i];
877    let mut cur = i;
878    loop {
879        if !facts[cur].glob_imports_super {
880            break;
881        }
882        let Some(parent) = parents[cur] else { break };
883        scopes.push(parent);
884        cur = parent;
885    }
886    scopes
887}
888
889/// How `name` resolves in module `m`'s namespace, walking [`visible_scopes`] with
890/// nearest binding winning — a closer non-`std::fs` `use` binding *or* locally
891/// declared type-namespace item shadows a farther `std::fs` binding, as in Rust.
892enum NameResolution {
893    StdFs,
894    Other,
895    Unbound,
896}
897
898fn resolve_name_in_module(
899    m: usize,
900    name: &str,
901    facts: &[FsImportFacts],
902    parents: &[Option<usize>],
903) -> NameResolution {
904    for s in visible_scopes(m, facts, parents) {
905        if facts[s].std_fs_bindings.contains(name) {
906            return NameResolution::StdFs;
907        }
908        if facts[s].use_bound_names.contains(name) || facts[s].declared_type_names.contains(name) {
909            return NameResolution::Other;
910        }
911    }
912    NameResolution::Unbound
913}
914
915/// Does a bare path root in file `i` resolve to `std::fs` through the bindings it
916/// can see? Candidates are the names any visible scope binds to `std::fs`; each is
917/// then resolved from `i` with nearest-binding-wins shadowing.
918fn resolves_bare_std_fs(i: usize, facts: &[FsImportFacts], parents: &[Option<usize>]) -> bool {
919    let scopes = visible_scopes(i, facts, parents);
920    let mut candidates: BTreeSet<&str> = BTreeSet::new();
921    for &s in &scopes {
922        candidates.extend(facts[s].std_fs_bindings.iter().map(String::as_str));
923    }
924    candidates.into_iter().any(|name| {
925        facts[i].bare_path_roots.contains(name)
926            && matches!(
927                resolve_name_in_module(i, name, facts, parents),
928                NameResolution::StdFs
929            )
930    })
931}
932
933/// Does a `super::`/`self::`/`crate::`-qualified path in file `i` reach a `std::fs`
934/// binding (#1016 review)? Unlike the bare-root case these need no glob import: the
935/// root picks the starting module directly (`super`-hops up the parent chain, `self`
936/// the file itself, `crate` the crate root), then each further segment either
937/// resolves in that module's namespace — `std::fs` flags, anything else stops — or
938/// descends into a child module file and continues. Inline `mod name { … }` blocks
939/// are not modelled (their `use` bindings live in the same file, which the text scan
940/// and bare-root resolution already cover).
941fn resolves_qualified_std_fs(
942    i: usize,
943    facts: &[FsImportFacts],
944    parents: &[Option<usize>],
945    files: &[(PathBuf, String)],
946) -> bool {
947    facts[i]
948        .qualified_paths
949        .iter()
950        .any(|chain| qualified_chain_reaches_std_fs(chain, i, facts, parents, files))
951}
952
953fn qualified_chain_reaches_std_fs(
954    chain: &[String],
955    i: usize,
956    facts: &[FsImportFacts],
957    parents: &[Option<usize>],
958    files: &[(PathBuf, String)],
959) -> bool {
960    let mut idx = 1;
961    let mut m = match chain[0].as_str() {
962        "self" => i,
963        "crate" => {
964            let root = files
965                .iter()
966                .position(|(p, _)| p == Path::new("lib.rs") || p == Path::new("main.rs"));
967            match root {
968                Some(root) => root,
969                None => return false,
970            }
971        }
972        "super" => {
973            let mut m = i;
974            idx = 0;
975            while idx < chain.len() && chain[idx] == "super" {
976                let Some(parent) = parents[m] else {
977                    return false;
978                };
979                m = parent;
980                idx += 1;
981            }
982            m
983        }
984        _ => return false,
985    };
986    while idx < chain.len() {
987        let seg = chain[idx].as_str();
988        // Resolve `seg` in `m`, nearest scope first. Within a scope, a child module
989        // file for `seg` is checked *before* the shadow set: a declared `mod seg;`
990        // lands `seg` in `declared_type_names`, but that declaration IS the child
991        // module — it's the path's next hop, not a shadow over it. (In valid Rust a
992        // module and another same-name type-namespace item can't coexist in one
993        // scope, so the ordering costs nothing.)
994        let mut next = None;
995        for s in visible_scopes(m, facts, parents) {
996            if facts[s].std_fs_bindings.contains(seg) {
997                return true;
998            }
999            if let Some(child) = child_module_file(s, seg, files) {
1000                next = Some(child);
1001                break;
1002            }
1003            if facts[s].use_bound_names.contains(seg) || facts[s].declared_type_names.contains(seg)
1004            {
1005                return false; // bound to something that is neither std::fs nor a module
1006            }
1007        }
1008        let Some(child) = next else {
1009            return false;
1010        };
1011        m = child;
1012        idx += 1;
1013    }
1014    false
1015}
1016
1017/// The file defining module `m`'s child module `seg`, if it exists as a file:
1018/// `lib.rs` + `a` → `a.rs`/`a/mod.rs`, `a.rs` + `b` → `a/b.rs`/`a/b/mod.rs`,
1019/// `a/mod.rs` + `b` → `a/b.rs`/`a/b/mod.rs`.
1020fn child_module_file(m: usize, seg: &str, files: &[(PathBuf, String)]) -> Option<usize> {
1021    let m_path = &files[m].0;
1022    let module_dir: PathBuf = match m_path.file_stem().and_then(|s| s.to_str()) {
1023        Some("mod") => m_path.parent().unwrap_or(Path::new("")).to_path_buf(),
1024        Some("lib") | Some("main") if m_path.parent().is_none_or(|p| p.as_os_str().is_empty()) => {
1025            PathBuf::new()
1026        }
1027        _ => m_path.with_extension(""),
1028    };
1029    let candidates = [
1030        module_dir.join(format!("{seg}.rs")),
1031        module_dir.join(seg).join("mod.rs"),
1032    ];
1033    candidates
1034        .iter()
1035        .find_map(|c| files.iter().position(|(p, _)| p == c))
1036}
1037
1038/// Every `#[cfg(test)] mod <ident> { ... }` block in `lines`, as inclusive
1039/// `(start_line, end_line)` line-index ranges — every occurrence, not just a single
1040/// trailing block. A file in this codebase can carry several test modules scattered
1041/// through it with production code between them — `bynk-emit/src/emitter/lower.rs` has
1042/// two, 1031 lines apart, and treating "everything after the first (or last)
1043/// `#[cfg(test)]`" as one cutoff silently misclassifies that intervening production
1044/// code as test-scope (caught in review: it made `fs_below_driver`, a *gated* probe,
1045/// blind over that span, and inflated `test_density`'s ratio by up to 39%).
1046///
1047/// A block's end is found by real brace-depth counting via [`brace_delta`], not a
1048/// "first column-0 `}`" shortcut: an earlier version of this fix tried exactly that
1049/// shortcut (reasoning that rustfmt always dedents a closing brace back to column 0)
1050/// and it broke on files like `bynk-ide/src/sequence.rs`, whose test module embeds
1051/// multi-line `.bynk`/TypeScript fixture source as string literals — source that
1052/// itself contains a column-0 `}` closing a top-level construct *inside the string*,
1053/// which the shortcut mistook for the end of the Rust `mod` block, truncating it by
1054/// hundreds of lines. `brace_delta` skips characters inside Rust string/char literals
1055/// and comments, so embedded fixture text can't be mistaken for real Rust braces.
1056///
1057/// Only matches a brace-opening `mod` line — `#[cfg(test)] mod foo;` (an external-file
1058/// declaration, not an inline scope) does not open a range.
1059fn test_mod_ranges(lines: &[&str]) -> Vec<(usize, usize)> {
1060    let mut ranges = Vec::new();
1061    let mut i = 0;
1062    while i < lines.len() {
1063        if lines[i].trim() == "#[cfg(test)]"
1064            && let Some(off) = lines[i + 1..].iter().position(|l| !l.trim().is_empty())
1065        {
1066            let mod_line = i + 1 + off;
1067            let t = lines[mod_line].trim();
1068            if t.starts_with("mod ") && t.ends_with('{') {
1069                let mut depth = 0i32;
1070                let mut state = BraceScanState::Normal;
1071                let mut started = false;
1072                let mut end = lines.len() - 1;
1073                for (j, line) in lines[mod_line..].iter().enumerate() {
1074                    let (delta, new_state) = brace_delta(line, state);
1075                    state = new_state;
1076                    depth += delta;
1077                    if depth != 0 {
1078                        started = true;
1079                    }
1080                    if started && depth == 0 {
1081                        end = mod_line + j;
1082                        break;
1083                    }
1084                }
1085                ranges.push((mod_line, end));
1086                i = end + 1;
1087                continue;
1088            }
1089        }
1090        i += 1;
1091    }
1092    ranges
1093}
1094
1095fn in_test_range(line_idx: usize, ranges: &[(usize, usize)]) -> bool {
1096    ranges
1097        .iter()
1098        .any(|(start, end)| line_idx >= *start && line_idx <= *end)
1099}
1100
1101/// Scanner state carried across lines for [`brace_delta`]: whether the cursor is
1102/// inside a string literal, a raw string (with its `#`-count), or a block comment
1103/// (with nesting depth — Rust block comments nest).
1104#[derive(Clone, Copy, PartialEq)]
1105enum BraceScanState {
1106    Normal,
1107    InString,
1108    InRawString(u8),
1109    InBlockComment(u32),
1110}
1111
1112/// The net `{`/`}` depth change in `line`, skipping characters inside Rust string/char
1113/// literals, raw strings, and line/block comments — a naive per-character brace count
1114/// breaks the moment a line contains a fixture string like `"fn f() { \"{\" }"` or a
1115/// doc comment mentioning a brace. Returns the depth delta and the state to carry into
1116/// the next line (a string or block comment can span line boundaries).
1117fn brace_delta(line: &str, mut state: BraceScanState) -> (i32, BraceScanState) {
1118    let mut delta = 0i32;
1119    let chars: Vec<char> = line.chars().collect();
1120    let mut i = 0;
1121    while i < chars.len() {
1122        match state {
1123            BraceScanState::Normal => {
1124                if chars[i] == '/' && chars.get(i + 1) == Some(&'/') {
1125                    break; // rest of the line is a line comment
1126                }
1127                if chars[i] == '/' && chars.get(i + 1) == Some(&'*') {
1128                    state = BraceScanState::InBlockComment(1);
1129                    i += 2;
1130                    continue;
1131                }
1132                if chars[i] == '"' {
1133                    state = BraceScanState::InString;
1134                    i += 1;
1135                    continue;
1136                }
1137                if chars[i] == 'r' && matches!(chars.get(i + 1), Some('"') | Some('#')) {
1138                    let mut j = i + 1;
1139                    let mut hashes = 0u8;
1140                    while chars.get(j) == Some(&'#') {
1141                        hashes += 1;
1142                        j += 1;
1143                    }
1144                    if chars.get(j) == Some(&'"') {
1145                        state = BraceScanState::InRawString(hashes);
1146                        i = j + 1;
1147                        continue;
1148                    }
1149                }
1150                if chars[i] == '\'' {
1151                    // A `'\x'`/`'\\'`-style escaped char literal, or a plain `'x'` —
1152                    // skip past it so its contents can't be mistaken for braces.
1153                    // Anything else (no closing `'` within a couple of chars) is a
1154                    // lifetime, which owns no closing quote to skip.
1155                    if chars.get(i + 1) == Some(&'\\') {
1156                        let mut j = i + 2;
1157                        while j < chars.len() && chars[j] != '\'' {
1158                            j += 1;
1159                        }
1160                        i = (j + 1).min(chars.len());
1161                        continue;
1162                    } else if chars.get(i + 2) == Some(&'\'') {
1163                        i += 3;
1164                        continue;
1165                    }
1166                }
1167                match chars[i] {
1168                    '{' => delta += 1,
1169                    '}' => delta -= 1,
1170                    _ => {}
1171                }
1172                i += 1;
1173            }
1174            BraceScanState::InString => {
1175                if chars[i] == '\\' {
1176                    i += 2;
1177                    continue;
1178                }
1179                if chars[i] == '"' {
1180                    state = BraceScanState::Normal;
1181                }
1182                i += 1;
1183            }
1184            BraceScanState::InRawString(hashes) => {
1185                if chars[i] == '"' {
1186                    let mut j = i + 1;
1187                    let mut h = 0u8;
1188                    while chars.get(j) == Some(&'#') && h < hashes {
1189                        h += 1;
1190                        j += 1;
1191                    }
1192                    if h == hashes {
1193                        state = BraceScanState::Normal;
1194                        i = j;
1195                        continue;
1196                    }
1197                }
1198                i += 1;
1199            }
1200            BraceScanState::InBlockComment(depth) => {
1201                if chars[i] == '/' && chars.get(i + 1) == Some(&'*') {
1202                    state = BraceScanState::InBlockComment(depth + 1);
1203                    i += 2;
1204                    continue;
1205                }
1206                if chars[i] == '*' && chars.get(i + 1) == Some(&'/') {
1207                    state = if depth <= 1 {
1208                        BraceScanState::Normal
1209                    } else {
1210                        BraceScanState::InBlockComment(depth - 1)
1211                    };
1212                    i += 2;
1213                    continue;
1214                }
1215                i += 1;
1216            }
1217        }
1218    }
1219    (delta, state)
1220}
1221
1222// --- Gated probe 3: options_sources --------------------------------------
1223
1224/// R2.3. `CompileOptions` (in `bynk-emit/src/project.rs`) has a `sources` field.
1225fn options_sources(root: &Path) -> Probe {
1226    let src = std::fs::read_to_string(root.join("bynk-emit/src/project.rs")).unwrap_or_default();
1227    let present = struct_body(&src, "CompileOptions").is_some_and(|body| body.contains("sources"));
1228    Probe {
1229        name: "options_sources",
1230        gated: true,
1231        reads: if present {
1232            "present".to_string()
1233        } else {
1234            "absent".to_string()
1235        },
1236    }
1237}
1238
1239/// The `{ ... }` body text of `struct <name>` in `src`, brace-matched from the struct's
1240/// own opening brace to its close.
1241fn struct_body<'a>(src: &'a str, name: &str) -> Option<&'a str> {
1242    let needle = format!("struct {name}");
1243    let start = src.find(&needle)?;
1244    let open = start + src[start..].find('{')?;
1245    let mut depth = 0i32;
1246    for (offset, ch) in src[open..].char_indices() {
1247        match ch {
1248            '{' => depth += 1,
1249            '}' => {
1250                depth -= 1;
1251                if depth == 0 {
1252                    return Some(&src[open..open + offset + 1]);
1253                }
1254            }
1255            _ => {}
1256        }
1257    }
1258    None
1259}
1260
1261// --- Gated probe 4: hoist_sinks -------------------------------------------
1262
1263/// R6.2. Live (non-comment) occurrences of the sink-passing signature
1264/// `stmts: &mut Vec<String>` in `bynk-emit`. Tier B (T2.1) deletes it entirely.
1265fn hoist_sinks(root: &Path) -> Probe {
1266    let dir = root.join("bynk-emit/src");
1267    let needle = "stmts: &mut Vec<String>";
1268    let mut count = 0usize;
1269    for (_, contents) in rust_files(&dir) {
1270        for line in contents.lines() {
1271            if !is_line_comment(line) && line.contains(needle) {
1272                count += 1;
1273            }
1274        }
1275    }
1276    Probe {
1277        name: "hoist_sinks",
1278        gated: true,
1279        reads: count.to_string(),
1280    }
1281}
1282
1283// --- Gated probe 5: span_keyed_maps ---------------------------------------
1284
1285/// R2.4. Whole-repo occurrences of `HashMap<Span` (comments included — the phase-3
1286/// migration target is every mention, not just live call sites), **excluding
1287/// `xtask` itself**: this probe's own doc comment and source both name the search
1288/// string, which would otherwise self-count every time this file is touched — the
1289/// same self-reference hazard flagged for the dead-identifier probes below, caught
1290/// here by running the probe against itself before committing the first table.
1291fn span_keyed_maps(root: &Path) -> Probe {
1292    let count = count_repo_wide(root, "HashMap<Span", &["xtask"]);
1293    Probe {
1294        name: "span_keyed_maps",
1295        gated: true,
1296        reads: count.to_string(),
1297    }
1298}
1299
1300fn count_repo_wide(root: &Path, needle: &str, exclude_crates: &[&str]) -> usize {
1301    let mut total = 0usize;
1302    for entry in top_level_crate_dirs(root) {
1303        if exclude_crates
1304            .iter()
1305            .any(|c| entry.file_name().is_some_and(|n| n == *c))
1306        {
1307            continue;
1308        }
1309        for (_, contents) in rust_files(&entry.join("src")) {
1310            total += contents.matches(needle).count();
1311        }
1312    }
1313    total
1314}
1315
1316/// Every workspace member crate directory (anything at the repo root with a
1317/// `Cargo.toml` and a `src/` dir), excluding `target` and non-crate directories.
1318fn top_level_crate_dirs(root: &Path) -> Vec<PathBuf> {
1319    let mut out = Vec::new();
1320    let Ok(entries) = std::fs::read_dir(root) else {
1321        return out;
1322    };
1323    for entry in entries.flatten() {
1324        let path = entry.path();
1325        if path.is_dir() && path.join("Cargo.toml").is_file() && path.join("src").is_dir() {
1326            out.push(path);
1327        }
1328    }
1329    out.sort();
1330    out
1331}
1332
1333// --- Gated probe 6: emit_diagnostics --------------------------------------
1334
1335/// R3.5. `bynk.*` string literals in `bynk-emit`/`bynk-check` source, cross-referenced
1336/// against `bynk_syntax::diagnostics::REGISTRY` — not pattern-matched. A literal not in
1337/// `REGISTRY` is a commons/namespace path (e.g. `bynk.locale`, the compiled first-party
1338/// source module name), not a diagnostic code, and must not inflate the count (#999
1339/// Decision A: this cross-reference is what makes the exclusion correct by
1340/// construction rather than a second hand-maintained list).
1341fn emit_diagnostics(root: &Path) -> Probe {
1342    let registry: BTreeSet<&str> = bynk_syntax::diagnostics::REGISTRY
1343        .iter()
1344        .map(|d| d.code)
1345        .collect();
1346    let mut parts = Vec::new();
1347    for (label, dir) in [
1348        ("bynk-emit", "bynk-emit/src"),
1349        ("bynk-check", "bynk-check/src"),
1350    ] {
1351        let mut naive: BTreeSet<String> = BTreeSet::new();
1352        for (_, contents) in rust_files(&root.join(dir)) {
1353            for lit in bynk_dotted_literals(&contents) {
1354                naive.insert(lit.to_string());
1355            }
1356        }
1357        let true_count = naive
1358            .iter()
1359            .filter(|l| registry.contains(l.as_str()))
1360            .count();
1361        parts.push(format!("{label}={true_count}/{}", naive.len()));
1362    }
1363    Probe {
1364        name: "emit_diagnostics",
1365        gated: true,
1366        reads: format!("{} (true/naive)", parts.join(", ")),
1367    }
1368}
1369
1370// --- Gated probe 7: ide_emit_edge -----------------------------------------
1371
1372/// R10.2. `bynk-ide` → `bynk-emit` in the manifest (`bynk-emit.workspace = true` or an
1373/// equivalent path/version dependency line).
1374fn ide_emit_edge(root: &Path) -> Probe {
1375    let manifest = std::fs::read_to_string(root.join("bynk-ide/Cargo.toml")).unwrap_or_default();
1376    let present = manifest
1377        .lines()
1378        .any(|l| l.trim_start().starts_with("bynk-emit"));
1379    Probe {
1380        name: "ide_emit_edge",
1381        gated: true,
1382        reads: if present {
1383            "present".to_string()
1384        } else {
1385            "absent".to_string()
1386        },
1387    }
1388}
1389
1390// --- Gated probe 8: ast_importers -----------------------------------------
1391
1392/// #1176: `bynk-emit::ir`'s own two files — named exactly, not by path prefix, the same
1393/// permanent-carve-out discipline [`NAMED_FS_EXCEPTIONS`] and [`emit_diagnostics`]'s
1394/// registry cross-reference already use. An `Ast → Ir` lowering pass importing
1395/// `bynk_syntax::ast` is that pass's entire job, not the AST-walking this track is
1396/// closing (phase 6's own P6.9 correction, #1167 — see the retired `the-ir.md`'s
1397/// closing summary, `design/archive/retired-tracks.md`) — but `project.rs` also
1398/// imports `bynk_syntax::ast` today (`EmitProjectCtx` holding `ActorDecl`/`AgentDecl`
1399/// fields directly), and that *is* exactly the still-open R6.13 defect this probe
1400/// tracks (P6.6: "closes the emitter reading AST declarations directly"). A
1401/// path-prefix rule scoped to `emitter/**` would exclude that file right along with
1402/// `ir/`'s legitimate ones, silently undercounting real remaining work — see
1403/// [`is_named_ast_importer`].
1404///
1405/// #1184 review: this exclusion is necessary but not sufficient for R6.13. `bynk-ir`
1406/// still embeds AST types directly in IR struct fields (`TypeShape::Refined`'s
1407/// `BaseType`/`Refinement`, ADR 0366) rather than IR-native equivalents — an emitter
1408/// reading such a field touches the AST without ever spelling `bynk_syntax::ast`
1409/// itself, so it is invisible to this probe by construction. `ast_importers` reading
1410/// its retired floor (5 — `design/archive/retired-tracks.md`'s own closing summary
1411/// has the per-file argument) proves no *remaining* file outside these two and the
1412/// five-file rendering subtree imports the AST module directly; it does not by
1413/// itself prove every `bynk-ir` field is AST-free.
1414///
1415/// #1187's own closing scoping pass adds one more, on different grounds than the
1416/// `ir.rs`/`ir/lower.rs` pair above: `project/tests_emit.rs` was deliberately *not*
1417/// added alongside `project.rs` when this list was first cut (the
1418/// `ast_importer_exclusion_is_named_not_prefixed` test below used to assert exactly
1419/// that) — #1187's own scoping pass found new evidence changing that: its test/suite
1420/// case bodies call `emitter::lower_block_to_async_body`/`lower_test_case_body`/
1421/// `lower_integration_case_body` directly (the Q7-settled body-rendering pass —
1422/// `emitter/lower.rs` keeps hand-writing TypeScript source text after phase 6's
1423/// cutover, the printer that would change that is phase 7's), and
1424/// its own `driver_param_ty`/`strip_effect_httpresult` read a handler's *declared*
1425/// param/return `TypeRef` with no corresponding `TyId` available at that call site
1426/// (the same caller-reads-callee's-raw-declared-shape pattern #661 established for
1427/// cross-context codec generation). Both are the Q7/printer kind of unreachable, not
1428/// the "still open, real work" kind the original exclusion list deliberately left this
1429/// file out of — the correction is new evidence, not a reversal of that reasoning.
1430///
1431/// Review of #1210: `emitter.rs`/`emitter/lower.rs` themselves were considered for
1432/// this same exclusion and **rejected** — Q7 settles that these files' *body-rendering*
1433/// surface stays AST-parameter-driven, but both files also hold live, currently
1434/// untouched AST-*declaration* reads with no such gate: `emitter.rs`'s own
1435/// `CommonsItem::Service`/`svc.protocol` walk (consumed-event-root collection) and
1436/// `emitter/lower.rs`'s own `cap_op_param_names` (`CommonsItem::Capability`/`c.ops`)
1437/// were exactly the P6.2/P6.6-class conversions phase 6's own slice decomposition
1438/// still listed as in scope at the time, not body-rendering. Excluding either file
1439/// would have hidden that real, fixable surface from this probe the same way a
1440/// path-prefix rule would — the harm the named-not-prefixed discipline above exists
1441/// to prevent, just at file granularity instead of directory granularity. (Both
1442/// converted their own reachable surface later, without joining this list — phase 6's
1443/// closing summary, `design/archive/retired-tracks.md`, has the account.)
1444///
1445/// P6.33 (phase 6's own §6a.D re-settling, 19 August 2026): `emitter/serialisation.rs`
1446/// joins the list, on grounds distinct from every entry above — not Q7 body-rendering,
1447/// not test-only reach, but a phase boundary. Unlike `emitter.rs`/`emitter/lower.rs`,
1448/// this file holds no `CommonsItem`-declaration-read surface at all (confirmed:
1449/// `grep -c bynk_syntax::ast` finds only its one `use` line and its `#[cfg(test)]`
1450/// module) — its entire AST surface, ~120 sites, *is* the `TypeRef`-driven JSON/wire
1451/// codec renderer (`emit_record_codec`/`emit_sum_codec`/`serialise_expr`/
1452/// `deserialise_expr`/`ts_inner_type` and siblings). Rendering a checker type as TS
1453/// codec source text is the same class of question Q7 already settled belongs to the
1454/// eventual printer (phase 7, `bynk-ts`) — this file has no `use crate::ir` at all, so
1455/// nothing here has been resisting an available IR-native alternative; none exists. The
1456/// re-settling found no clean way to shrink this file's AST surface further without
1457/// building printer infrastructure phase 6's own scope already excluded.
1458///
1459/// P6.49 (phase 6's own §6b, 19 August 2026): `project.rs` — R6.13's own still-open
1460/// declaration-read surface named at the top of this doc block, above — cleared
1461/// **without joining this list**. Nine slices (P6.42–P6.49) relocated its remaining
1462/// declaration reads to the `bynk-check`/`bynk-project` crates that already own the
1463/// data (`SourceUnit::name()`, `own_contract_hashes`, `discover_event_subscribers`,
1464/// `combined_types_for_unit_info`, two owner-side accessors,
1465/// `lower_event_subscriber_shapes_ir`, `walk_unit_table_bodies`) or re-exported a type
1466/// from a `bynk-check` module whose own public API was already parameterised by it
1467/// (`TypeDecl`/`FnDecl`/`Visibility` from `project_model`, `ActorDecl` from `actors` —
1468/// the P6.27 `ExprId` precedent, applied four more times). This is the evidence this
1469/// exclusion list's own entries above are real, earned exclusions and not a standing
1470/// habit: the harder file cleared its own way, on its own schedule, with zero new
1471/// entries here.
1472///
1473/// **P6.58/P6.59, 19 August 2026: phase 6 (`the-ir.md`, spine #1137) retired at this
1474/// probe reading 5, not 0.** The floor is exactly `bynk-emit/src/emitter{,/**}` —
1475/// `emitter.rs`, `emitter/emit.rs`, `emitter/lower.rs`, `emitter/workers.rs`,
1476/// `emitter/workers_entry.rs` — the TypeScript-rendering subtree phase 7's own printer
1477/// inherits; each file's own structural reason, and the full slice history behind
1478/// every correction this doc block narrates, live in `design/archive/retired-tracks.md`
1479/// now that `the-ir.md` itself is gone. This exclusion list does **not** grow to
1480/// reach that floor — the floor is a fact about `AST_IMPORTER_EXCEPTIONS`'s own
1481/// four entries staying exactly these four, not a fifth argument for adding to them.
1482/// The probe itself stays gated, unchanged, reading 5: a regression ratchet phase 7
1483/// inherits and drives down as it builds the printer this floor's own residue names.
1484///
1485/// **Arc D, P7.12 (crate carve): `ir.rs`/`ir/lower.rs` drop out of this list
1486/// entirely — not because they stopped importing the AST (unchanged, still
1487/// do), but because they left `bynk-emit/src` altogether, carved into the new
1488/// `bynk-ir`/`bynk-lower` crates ADR 0332 deferred and ADR 0385 triggered.**
1489/// This probe was never scoped to those crates (`ast_importer_files` walks
1490/// `bynk-emit/src` only), so the pair is simply outside its universe now,
1491/// the same way a file moving to `bynk-check`/`bynk-project` already leaves
1492/// silently rather than needing its own exclusion-list removal step. Two
1493/// named exclusions remain.
1494const AST_IMPORTER_EXCEPTIONS: &[&str] = &["project/tests_emit.rs", "emitter/serialisation.rs"];
1495
1496/// Is `rel_path` (relative to `bynk-emit/src`) one of [`AST_IMPORTER_EXCEPTIONS`]?
1497fn is_named_ast_importer(rel_path: &Path) -> bool {
1498    let rel = rel_path.to_string_lossy().replace('\\', "/");
1499    AST_IMPORTER_EXCEPTIONS.contains(&rel.as_str())
1500}
1501
1502/// Is `contents` a module (not a nested block) that glob-imports its parent —
1503/// i.e. does it carry a top-level (column-0) `use super::*;`? Rust's own privacy
1504/// rule makes a parent module's private `use` visible to descendant modules, so a
1505/// file matching this can expose `bynk_syntax::ast` names it never spells itself
1506/// (P6.26 review, #1259) — deliberately column-0 only, so a `use super::*;`
1507/// *inside* a nested `#[cfg(test)] mod tests { .. }` block (glob-importing its own
1508/// immediately-enclosing module, not the grandparent file on disk) doesn't
1509/// false-positive this check.
1510fn has_module_level_super_glob(contents: &str) -> bool {
1511    contents.lines().any(|line| line == "use super::*;")
1512}
1513
1514/// For `rel_path` = `<dir>/<file>.rs`, does the sibling module file `<dir>.rs`
1515/// (the parent module a top-level `use super::*;` in `rel_path` would inherit
1516/// from) itself contain `bynk_syntax::ast`? `None` if `rel_path` has no such
1517/// parent (a file directly under `bynk-emit/src`, e.g. `emitter.rs` itself).
1518fn super_glob_parent_imports_ast(dir: &Path, rel_path: &Path) -> Option<bool> {
1519    let parent_dir = rel_path.parent()?;
1520    if parent_dir.as_os_str().is_empty() {
1521        return None;
1522    }
1523    let parent_file = dir.join(parent_dir).with_extension("rs");
1524    Some(
1525        std::fs::read_to_string(&parent_file)
1526            .is_ok_and(|contents| contents.contains("bynk_syntax::ast")),
1527    )
1528}
1529
1530/// The files [`ast_importers`] counts: `bynk-emit/src` files whose contents match
1531/// `bynk_syntax::ast` **or** that inherit it from an AST-importing parent through a
1532/// top-level `use super::*;` (P6.26 review, #1259 — a file that stops spelling the
1533/// AST module directly by deleting its own explicit import, while a live `use
1534/// super::*;` still channels a still-AST-importing parent's names in, must stay
1535/// counted; otherwise a future partial conversion could silently drop this probe
1536/// without the underlying AST dependency actually being gone), excluding
1537/// [`AST_IMPORTER_EXCEPTIONS`]. Split out from [`ast_importers`] so a test can
1538/// assert on the actual survivor set, not just its length (#1184 review).
1539fn ast_importer_files(root: &Path) -> Vec<PathBuf> {
1540    let dir = root.join("bynk-emit/src");
1541    rust_files(&dir)
1542        .into_iter()
1543        .filter(|(path, contents)| {
1544            contents.contains("bynk_syntax::ast")
1545                || (has_module_level_super_glob(contents)
1546                    && super_glob_parent_imports_ast(&dir, path.strip_prefix(&dir).unwrap_or(path))
1547                        .unwrap_or(false))
1548        })
1549        .filter(|(path, _)| !is_named_ast_importer(path.strip_prefix(&dir).unwrap_or(path)))
1550        .map(|(path, _)| path)
1551        .collect()
1552}
1553
1554/// R6.13. Files in `bynk-emit/src` that import `bynk_syntax::ast`, excluding
1555/// [`AST_IMPORTER_EXCEPTIONS`] — phase 6's own remaining AST import surface (retired,
1556/// spine #1137; `design/archive/retired-tracks.md` has the closing summary). #1176:
1557/// the unexcluded, crate-wide count could never reach 0 while `bynk-emit::ir`'s
1558/// lowering pass exists at all; this exclusion is what let the probe track phase 6's
1559/// real completion criterion instead of a floor its own IR module structurally could
1560/// not clear. Gated at 5, phase 6's own retired floor, for phase 7 to drive down.
1561fn ast_importers(root: &Path) -> Probe {
1562    Probe {
1563        name: "ast_importers",
1564        gated: true,
1565        reads: ast_importer_files(root).len().to_string(),
1566    }
1567}
1568
1569// --- Gated probe 9: emit_abi_shapes ---------------------------------------
1570
1571/// ADR 0310 D1's four emit-ABI shapes, as they surface as import names in the vendored
1572/// bindings — the `Result`/`Option` tag layout plus `JsonError`, `Uuid`, `FetchError`.
1573const EMIT_ABI: &[&str] = &[
1574    "Result",
1575    "Option",
1576    "Ok",
1577    "Err",
1578    "Some",
1579    "None",
1580    "JsonError",
1581    "Uuid",
1582    "FetchError",
1583];
1584
1585/// The capability interfaces a vendored binding legitimately imports to implement what
1586/// it declares — governed by language-stability rules, not ADR 0310's codegen-freeze
1587/// concern. See [`emit_abi_shapes`] and #999 Decision E for the two-list rationale.
1588const CAPABILITY_SURFACE: &[&str] = &[
1589    "Clock",
1590    "Fetch",
1591    "Idempotency",
1592    "Locale",
1593    "Logger",
1594    "Random",
1595    "Secrets",
1596    "Request",
1597    "Response",
1598    "LocaleTag",
1599    "Kv",
1600    "KVNamespace",
1601    // Interop I5 (#1972): `bynk.cloudflare`'s `Service`, and the Cloudflare
1602    // `Fetch` provider its binding sends through, so the two share one set of
1603    // header and response rules. Binding-to-binding, not a codegen shape.
1604    "Service",
1605    "FetchProvider",
1606];
1607
1608/// Is `ident` one of ADR 0310's enumerated emit-ABI shapes, or part of the capability
1609/// surface a binding is required to import? If neither, it's a leak `emit_abi_shapes`
1610/// flags — this is the single predicate both the probe and its tests use, so a test
1611/// asserting "no leak" can't silently pass against a list the test itself redefined.
1612fn is_enumerated_emit_abi_or_capability_surface(ident: &str) -> bool {
1613    EMIT_ABI.contains(&ident) || CAPABILITY_SURFACE.contains(&ident)
1614}
1615
1616/// The directories holding the vendored first-party TypeScript bindings, relative to
1617/// the repo root (standard-library track, #1940): the `bynk` surface's per-platform
1618/// bindings in `bynk-std`'s portable layer, and the Cloudflare platform adapter's
1619/// binding in its own subtree. [`emit_abi_shapes`] reads every `.ts` file in each,
1620/// non-recursively, so a binding added to a new subtree must be listed here.
1621const EMIT_ABI_BINDING_DIRS: &[&str] = &["bynk-std/lib/core/bindings", "bynk-std/lib/cloudflare"];
1622
1623/// ADR 0310's probe (#999 Decision E). The vendored first-party bindings under
1624/// [`EMIT_ABI_BINDING_DIRS`] must reference only [`EMIT_ABI`]'s nine names.
1625///
1626/// This does NOT count every non-enumerated import: a binding legitimately imports the
1627/// [`CAPABILITY_SURFACE`] interfaces it implements — that surface is governed by
1628/// language-stability rules, not ADR 0310's codegen-freeze concern, and a probe that
1629/// flagged it would read non-zero on every binding by construction. See #999 Decision
1630/// E for the two-list rationale and its falsifier.
1631///
1632/// A listed directory that is missing, or holds no `.ts` file, reads as a failure
1633/// rather than as zero leaks: a binding moved out of the probe's sight must not pass
1634/// it vacuously (#1940 Decision D).
1635fn emit_abi_shapes(root: &Path) -> Probe {
1636    let mut files: Vec<std::path::PathBuf> = Vec::new();
1637    for rel in EMIT_ABI_BINDING_DIRS {
1638        let Ok(entries) = std::fs::read_dir(root.join(rel)) else {
1639            return Probe {
1640                name: "emit_abi_shapes",
1641                gated: true,
1642                reads: format!("bindings directory not found: {rel}"),
1643            };
1644        };
1645        let before = files.len();
1646        files.extend(
1647            entries
1648                .flatten()
1649                .map(|e| e.path())
1650                .filter(|path| path.extension().is_some_and(|e| e == "ts")),
1651        );
1652        if files.len() == before {
1653            return Probe {
1654                name: "emit_abi_shapes",
1655                gated: true,
1656                reads: format!("no bindings found in {rel}"),
1657            };
1658        }
1659    }
1660    // By file name, so the reading does not depend on which directory a binding sits in.
1661    files.sort_by_key(|path| path.file_name().map(|n| n.to_os_string()));
1662    let mut leaks: Vec<String> = Vec::new();
1663    for path in files {
1664        let Ok(contents) = std::fs::read_to_string(&path) else {
1665            continue;
1666        };
1667        let name = path.file_name().unwrap().to_string_lossy().to_string();
1668        for ident in ts_named_imports_from_runtime_modules(&contents) {
1669            if !is_enumerated_emit_abi_or_capability_surface(&ident) {
1670                leaks.push(format!("{name}:{ident}"));
1671            }
1672        }
1673    }
1674    Probe {
1675        name: "emit_abi_shapes",
1676        gated: true,
1677        reads: format!("{} ({})", leaks.len(), leaks.join(", ")),
1678    }
1679}
1680
1681// --- Gated probe 10: ts_writes ---------------------------------------------
1682
1683/// Files under `bynk-emit/src` that contain `write!`/`writeln!`/`format!` calls but
1684/// produce no TypeScript at all — excluded from both [`ts_writes`] and [`ts_any`], each
1685/// argued individually the same way [`AST_IMPORTER_EXCEPTIONS`] is, not assumed from a
1686/// path prefix: `emitter/wrangler.rs` writes `wrangler.toml`; `emitter/secrets.rs`
1687/// writes `bynk-secrets.json`; `emitter/contracts.rs` writes `bynk-contracts.json`;
1688/// `testkit.rs` builds a `.bynk` source fixture — a compiler *input* for tests, not
1689/// output. P7.3 (#1303): `emitter/toml_doc.rs` writes `wrangler.toml` text too —
1690/// `emitter/wrangler.rs`'s own writes moved here when it stopped building the TOML text
1691/// directly and started building a typed `TomlDocument` for this module to print — same
1692/// rationale, same exclusion.
1693///
1694/// (`ir/lower.rs` — Rust-internal `String` values stored on `Ir*` struct fields during
1695/// the checker→IR lowering pass, never emitted syntax — was excluded here for the same
1696/// reason until Arc D's P7.12 crate carve moved it to `bynk-lower` entirely, outside
1697/// this probe's own `bynk-emit/src` universe; no exclusion needed for a file this probe
1698/// no longer walks. `emitter/source_map.rs`, which wrote source-map JSON, is the same
1699/// shape one carve earlier — P7.5 (#1308) relocated it to `bynk-ts/src/source_map.rs`
1700/// in full; its own #1561 removal from this list.)
1701const TS_WRITES_EXCLUDED_FILES: &[&str] = &[
1702    "emitter/wrangler.rs",
1703    "emitter/toml_doc.rs",
1704    "emitter/secrets.rs",
1705    "emitter/contracts.rs",
1706    "testkit.rs",
1707];
1708
1709/// Is `rel_path` (relative to `bynk-emit/src`) one of [`TS_WRITES_EXCLUDED_FILES`]?
1710fn is_ts_writes_excluded_file(rel_path: &Path) -> bool {
1711    let rel = rel_path.to_string_lossy().replace('\\', "/");
1712    TS_WRITES_EXCLUDED_FILES.contains(&rel.as_str())
1713}
1714
1715/// True if `line` builds a filesystem path via `format!` rather than TypeScript text —
1716/// the `PathBuf::from(format!(...))`/`.join(format!(...))`/`.with_file_name(format!(...))`
1717/// idiom [`ts_writes`] excludes at line granularity, not by file, because the files it
1718/// appears in (`project.rs`, `project/tests_emit.rs`) are otherwise genuinely
1719/// TypeScript-producing.
1720///
1721/// **`.with_file_name(format!` found and added by Arc F's own item-4 investigation
1722/// (#1457):** `project.rs`'s `sibling_path` (`output_path.with_file_name(format!(
1723/// "{name}.{suffix}"))`) builds a sibling filesystem path the same way the two idioms
1724/// above do, but spelled with `.with_file_name(` — the prior substring match didn't
1725/// catch it, over-counting `ts_writes` by this one site.
1726fn is_path_construction_line(line: &str) -> bool {
1727    line.contains("PathBuf::from(format!")
1728        || line.contains(".join(format!")
1729        || line.contains(".with_file_name(format!")
1730}
1731
1732/// Relativises every path in [`rust_files`]'s output against `dir`, so [`ts_writes`]
1733/// and [`ts_any`]'s counting logic ([`ts_writes_violations`], [`ts_any_violations`])
1734/// takes the same `&[(PathBuf, String)]` shape [`production_std_fs_files`] does — an
1735/// in-memory file list a test can construct directly, per review of #1297 (a first cut
1736/// of these two probes took `root: &Path` and did its own walk, so nothing but the
1737/// drift gate actually exercised the exclusion logic; deleting a `continue` left every
1738/// test green).
1739fn rust_files_relative(dir: &Path) -> Vec<(PathBuf, String)> {
1740    rust_files(dir)
1741        .into_iter()
1742        .map(|(path, contents)| {
1743            let rel = path.strip_prefix(dir).unwrap_or(&path).to_path_buf();
1744            (rel, contents)
1745        })
1746        .collect()
1747}
1748
1749/// [`ts_writes`]'s counting logic, over an explicit `(relative path, contents)` list —
1750/// see [`rust_files_relative`] for why this isn't `root: &Path`.
1751///
1752/// **A real mistake this slice's own grounding found and fixed, not carried forward:**
1753/// an earlier survey (during phase 7's own track-opening research) characterised
1754/// `project/tests_emit.rs`'s 128 such sites as excludable "test-assertion strings" — the
1755/// same mischaracterisation `semantics-in-the-checker.md`'s own settling review caught
1756/// and corrected for a *different* probe (`emit_diagnostics`) on this same file: it is
1757/// `process_tests`/`process_integration_tests`, real production TypeScript-emission
1758/// code, not fixture noise, and none of its 128 sites fall inside its own single
1759/// `#[cfg(test)] mod tests { .. }` block. All 128 count here, less the one line that
1760/// genuinely builds a file path ([`is_path_construction_line`]).
1761///
1762/// **Known, accepted gap:** `project/tests_emit.rs`'s
1763/// `target_name: format!("system · {suite}")` builds a human-readable struct-field
1764/// label, not TypeScript text, and matches neither exclusion rule. A text-level scanner
1765/// has no cheap way to catch one field-name-specific site without a bespoke rule for it
1766/// alone — accepted as a one-site over-count, the same "known remaining gaps, out of
1767/// reach for a text-level scanner" discipline [`production_std_fs_files`] already
1768/// documents for a different probe.
1769fn ts_writes_violations(files: &[(PathBuf, String)]) -> usize {
1770    let mut count = 0usize;
1771    for (rel, contents) in files {
1772        if is_ts_writes_excluded_file(rel) {
1773            continue;
1774        }
1775        let lines: Vec<&str> = contents.lines().collect();
1776        let ranges = test_mod_ranges(&lines);
1777        for (i, line) in lines.iter().enumerate() {
1778            if in_test_range(i, &ranges) || is_line_comment(line) || is_path_construction_line(line)
1779            {
1780                continue;
1781            }
1782            if line.contains("write!") || line.contains("writeln!") || line.contains("format!") {
1783                count += 1;
1784            }
1785        }
1786    }
1787    count
1788}
1789
1790/// The trajectory's own phase-7 probe (`design/bynk-compiler-trajectory.md` §3):
1791/// "TypeScript-producing `write!` outside a printer". Never measured before this slice
1792/// (P7.0, #1296; see phase 7's own closing summary, `design/archive/retired-tracks.md`,
1793/// for the full retirement argument) — `bynk-ts` did not exist yet at measurement time,
1794/// so "outside a printer" reduced then to "in `bynk-emit`, outside a `Verbatim`
1795/// construction"; the `Verbatim` half of that exclusion was vacuous until P7.5 built the
1796/// type.
1797///
1798/// **Not "zero/closure"-shaped like this module's other twelve gated probes, and gated
1799/// anyway — a deliberate choice, not an inherited one.** The reading started at 1641 and
1800/// converged, over dozens of slices, to phase 7's own argued retirement floor, **809**
1801/// (ADR 0409; the full bucket-by-bucket accounting is in phase 7's own closing summary,
1802/// `design/archive/retired-tracks.md`) — never the literal 0 first proposed, nor a small
1803/// fixed number the way `ast_importers`/`emit_abi_shapes` are. Stays gated after
1804/// retirement, not deleted — a regression ratchet like `ast_importers` (floor 5) already
1805/// is: it moves on any `bynk-emit` PR that adds or removes a single `write!`/`writeln!`/
1806/// `format!` line anywhere in the crate — the same volatility #999 Decision D cites for
1807/// *not* gating `wildcard_arms` (311, ungated for exactly this reason) — but a floor this
1808/// track spent dozens of slices earning is worth catching a silent regression against,
1809/// the same trade `ast_importers` already made successfully across phase 6's 59 slices.
1810/// The churn cost is real and accepted, not overlooked: see ADR 0389/ADR 0390 for the
1811/// argument in full (review of #1297).
1812///
1813/// Counts `bynk-emit/src/**/*.rs` lines — excluding comments, `#[cfg(test)]` test-module
1814/// ranges, [`TS_WRITES_EXCLUDED_FILES`], and [`is_path_construction_line`] matches —
1815/// containing `write!`, `writeln!` or `format!`. See [`ts_writes_violations`] for the
1816/// counting logic itself.
1817fn ts_writes(root: &Path) -> Probe {
1818    let dir = root.join("bynk-emit/src");
1819    Probe {
1820        name: "ts_writes",
1821        gated: true,
1822        reads: ts_writes_violations(&rust_files_relative(&dir)).to_string(),
1823    }
1824}
1825
1826// --- Gated probe 11: ts_any -------------------------------------------------
1827
1828/// True if `line` (not a comment) violates R7.1's `TsType::Any` prohibition: an
1829/// `as any` cast, a bare `: any` type annotation, or `any` in generic type-argument
1830/// position (`Array<any>`, `Record<string, any[]>`, `Promise<any>`).
1831///
1832/// Six patterns, not `as any` alone, following three rounds of the same finding.
1833/// Round one (Q3, phase 7's own settling pass) found `as any` alone
1834/// under-counts R7.1 and added bare `: any`. Round two (review of #1297) found *that*
1835/// still under-counts: `bynk-emit/src/emitter/lower.rs`'s `joinOn`/`leftJoin`/`groupBy`
1836/// emit `const __h: Record<string, any[]> = {}` — `, any[]` contains neither `as any`
1837/// nor `: any`, so three live, production, TypeScript-emitting sites read as clean
1838/// under the round-one predicate. Widened to also match `<any`, `any>` and `any[]` —
1839/// each checked against the live tree for false positives (no non-`any`-typed English
1840/// word starts with `any` immediately after `<` or ends in `any` immediately before
1841/// `>`/`[]` anywhere in `bynk-emit/src` today) rather than assumed safe. Round three
1842/// (review of #1322) found a fourth spelling: once a site builds a real `bynk_ts::
1843/// TsType` node instead of writing TypeScript text directly, an emitted `any` no
1844/// longer appears as Rust-source `as any`/`: any` at all — `workers.rs`'s own
1845/// `TsType::named("any")` calls (#1321) emit the identical `payload as any`/
1846/// `let __who: any` text as before, byte-for-byte, but the *Rust spelling* that
1847/// produces it no longer matches any of the five text patterns above, so the probe
1848/// silently uncounted three real, still-live R7.1 residuals. Every later Arc C slice
1849/// converting an `any`-emitting `writeln!`/`format!` site the same way would keep
1850/// deflating this count the same way, so the fix generalises rather than special-
1851/// cases these three lines: match the construction spelling itself
1852/// (`named("any"`), not just raw emitted text.
1853///
1854/// Split out from [`ts_any_violations`] so a test can exercise the predicate directly,
1855/// without file I/O.
1856fn line_violates_ts_any(line: &str) -> bool {
1857    !is_line_comment(line)
1858        && (line.contains("as any")
1859            || line.contains(": any")
1860            || line.contains("<any")
1861            || line.contains("any>")
1862            || line.contains("any[]")
1863            || line.contains("named(\"any\""))
1864}
1865
1866/// [`ts_any`]'s counting logic, over an explicit `(relative path, contents)` list — see
1867/// [`rust_files_relative`] for why this isn't `root: &Path`.
1868fn ts_any_violations(files: &[(PathBuf, String)]) -> usize {
1869    let mut count = 0usize;
1870    for (rel, contents) in files {
1871        if is_ts_writes_excluded_file(rel) {
1872            continue;
1873        }
1874        let lines: Vec<&str> = contents.lines().collect();
1875        let ranges = test_mod_ranges(&lines);
1876        for (i, line) in lines.iter().enumerate() {
1877            if in_test_range(i, &ranges) {
1878                continue;
1879            }
1880            if line_violates_ts_any(line) {
1881                count += 1;
1882            }
1883        }
1884    }
1885    count
1886}
1887
1888/// Reference rule R7.1 (`design/bynk-greenfield-compiler.md` Part 7) — "the tree
1889/// contains no ... `TsType::Any`". Gated for the same reason [`ts_writes`] is (see its
1890/// own doc comment): started at 55 (not the settling review's estimated ~24 — ADR
1891/// 0390), converged over several slices to phase 7's own argued retirement floor, **26**
1892/// (ADR 0404; six already-argued families, none newly tractable — full accounting in
1893/// phase 7's own closing summary, `design/archive/retired-tracks.md`), and stays gated
1894/// after retirement as a regression ratchet, the same "I removed an `Any`" CI-checkable
1895/// claim every slice needed.
1896///
1897/// Counts `bynk-emit/src/**/*.rs` lines — excluding `#[cfg(test)]` test-module ranges
1898/// and [`TS_WRITES_EXCLUDED_FILES`] (the same files [`ts_writes`] excludes for producing
1899/// no TypeScript at all; an `any`-typed value there isn't R7.1's business either) —
1900/// matching [`line_violates_ts_any`]. Hand-written runtime `.ts` files under
1901/// `bynk-emit/runtime/` are out of scope by construction: [`rust_files`] only walks
1902/// `.rs` files, and R7.1 governs the emitted *tree*, not the hand-written runtime R7.7
1903/// separately covers.
1904fn ts_any(root: &Path) -> Probe {
1905    let dir = root.join("bynk-emit/src");
1906    Probe {
1907        name: "ts_any",
1908        gated: true,
1909        reads: ts_any_violations(&rust_files_relative(&dir)).to_string(),
1910    }
1911}
1912
1913// --- Gated probe 12: verbatim_origins ---------------------------------------
1914
1915/// P7.5 (#1307): distinct `bynk_ts::VerbatimOrigin` variants named in
1916/// `bynk-emit/src` — how many *families* of residual, not-yet-converted
1917/// emission remain, not their size (`verbatim_sites`, below, is the size).
1918/// Retired at an **argued floor**, named file-by-file the way `ast_importers`'s
1919/// floor of 5 was: **1** (ADR 0410) — only `NotYetConverted` has a live
1920/// production reference, at the same two sites `verbatim_sites`'s own floor
1921/// names permanent; full accounting in phase 7's own closing summary,
1922/// `design/archive/retired-tracks.md`. Read **0** at this slice's own
1923/// landing (`bynk-emit` built no `Verbatim` content yet, #1307's Decision C
1924/// — Arc C's own first slice is what gave this probe something to count);
1925/// stays gated after retirement as a regression ratchet.
1926///
1927/// Line-scans for `VerbatimOrigin::<Variant>` and counts distinct variant
1928/// names referenced, the same needle-scan shape [`hoist_sinks`] uses. A
1929/// known, accepted gap (review of #1308, finding 6): a bare `use
1930/// bynk_ts::VerbatimOrigin::Contracts;` followed by unqualified `Contracts`
1931/// elsewhere would undercount, since the needle is the qualified path. Not
1932/// worth a real-parser fix for an *argued-floor* probe (unlike
1933/// `verbatim_sites`'s own floor of exactly 0) — `bynk-emit`'s own existing
1934/// call-site style always qualifies (`TsStmt::verbatim(VerbatimOrigin::X,
1935/// …)`), so this is a theoretical undercount, not an observed one.
1936fn verbatim_origins(root: &Path) -> Probe {
1937    let dir = root.join("bynk-emit/src");
1938    Probe {
1939        name: "verbatim_origins",
1940        gated: true,
1941        reads: verbatim_origins_violations(&rust_files_relative(&dir)).to_string(),
1942    }
1943}
1944
1945/// [`verbatim_origins`]'s counting logic, over an explicit `(relative path,
1946/// contents)` list — see [`rust_files_relative`] for why this isn't `root:
1947/// &Path`. Excludes `#[cfg(test)]` ranges the same way [`ts_any_violations`]
1948/// does (review of #1308, finding 6): without this, one `bynk-emit` unit
1949/// test constructing a `VerbatimOrigin` for its own fixture pins this probe
1950/// above its argued floor permanently, for a reason that has nothing to do
1951/// with residual production emission.
1952fn verbatim_origins_violations(files: &[(PathBuf, String)]) -> usize {
1953    let needle = "VerbatimOrigin::";
1954    let mut variants: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
1955    for (_, contents) in files {
1956        let lines: Vec<&str> = contents.lines().collect();
1957        let ranges = test_mod_ranges(&lines);
1958        for (i, line) in lines.iter().enumerate() {
1959            if in_test_range(i, &ranges) || is_line_comment(line) {
1960                continue;
1961            }
1962            let mut rest = *line;
1963            while let Some(idx) = rest.find(needle) {
1964                let after = &rest[idx + needle.len()..];
1965                let name: String = after
1966                    .chars()
1967                    .take_while(|c| c.is_ascii_alphanumeric() || *c == '_')
1968                    .collect();
1969                rest = &after[name.len()..];
1970                if !name.is_empty() {
1971                    variants.insert(name);
1972                }
1973            }
1974        }
1975    }
1976    variants.len()
1977}
1978
1979// --- Gated probe 13: verbatim_sites -----------------------------------------
1980
1981/// P7.5 (#1307): distinct `TsStmt::verbatim(...)`/`TsExpr::VerbatimExpr(...)`
1982/// construction call sites in `bynk-emit/src`, line-scanned the same way
1983/// [`hoist_sinks`] counts `stmts: &mut Vec<String>` occurrences. Every call
1984/// site converting to a real tree node is what Arc C's own per-file slices
1985/// were actually for — `verbatim_origins` alone can't distinguish "3
1986/// variants, 12 residual call sites" from "3 variants, 900 residual call
1987/// sites, two files never decomposed"; this is what closes that gap. Retired
1988/// at an **argued floor** for the `TsStmt` half, not the flat 0 first
1989/// proposed: **2** (ADR 0399/ADR 0407, confirmed unchanged by the #1486
1990/// capstone) — `project.rs`'s adapter-binding copy loop (a foreign,
1991/// user-authored TypeScript payload) and its `runtime.ts` staging (a
1992/// committed npm build artifact), neither ever generated by `bynk-emit`;
1993/// full accounting in phase 7's own closing summary, `design/archive/
1994/// retired-tracks.md`.
1995///
1996/// #1539 widens the scan to the `TsExpr` half of the same escape hatch
1997/// (`TsExpr::VerbatimExpr`, closing the untagged-`Ident` gap the review
1998/// found) and moves the floor to **11** (the 2 permanent `TsStmt` sites plus
1999/// 9 residual `TsExpr` construction sites in `emit.rs` — a generic-typed
2000/// callee `Call`/`New` has no `type_args` field, a nested `As`-under-`As`
2001/// chain the printer's own operand-parenthesisation rule would mis-wrap, a
2002/// block-bodied ICU IIFE, and a `pred_condition_and_message`-style message
2003/// that a second `TsLit::Str` escaping pass would corrupt — see
2004/// `TsExpr::VerbatimExpr`'s own doc for the full list). Unlike the `TsStmt`
2005/// pair, these 9 are not argued-permanent the same way: each converts to a
2006/// real node the day `bynk-ts` gains the matching type-algebra piece
2007/// (`type_args`, a parenthesisation fix, …), so this half of the floor is
2008/// expected to keep shrinking, tracked here rather than assumed fixed. Read
2009/// **0** at this slice's own landing, same reason `verbatim_origins` did;
2010/// stays gated after retirement as a regression ratchet.
2011fn verbatim_sites(root: &Path) -> Probe {
2012    let dir = root.join("bynk-emit/src");
2013    Probe {
2014        name: "verbatim_sites",
2015        gated: true,
2016        reads: verbatim_sites_violations(&rust_files_relative(&dir)).to_string(),
2017    }
2018}
2019
2020/// [`verbatim_sites`]'s counting logic, over an explicit `(relative path,
2021/// contents)` list — see [`rust_files_relative`] for why this isn't `root:
2022/// &Path`. Excludes `#[cfg(test)]` ranges the same way [`ts_any_violations`]
2023/// does (review of #1308, finding 6): `verbatim_sites` is documented as
2024/// retiring at 0, so a residual construction site inside a test fixture
2025/// would pin it above zero permanently for a reason that has nothing to do
2026/// with production emission conversion.
2027fn verbatim_sites_violations(files: &[(PathBuf, String)]) -> usize {
2028    let needles = ["TsStmt::verbatim(", "TsExpr::VerbatimExpr("];
2029    let mut count = 0usize;
2030    for (_, contents) in files {
2031        let lines: Vec<&str> = contents.lines().collect();
2032        let ranges = test_mod_ranges(&lines);
2033        for (i, line) in lines.iter().enumerate() {
2034            if in_test_range(i, &ranges) || is_line_comment(line) {
2035                continue;
2036            }
2037            if needles.iter().any(|needle| line.contains(needle)) {
2038                count += 1;
2039            }
2040        }
2041    }
2042    count
2043}
2044
2045/// Named identifiers imported from the compiler-generated firstparty/runtime relative
2046/// modules (`./bynk.js`, `./runtime.js`, `./bynk/locale/types.js`, `./cloudflare.js`,
2047/// or their `../` forms) — `import type { A, B }`/`import { A, B }` braces, stripping
2048/// `type ` markers and `X as Y` aliases (keeping the imported name, not the local one,
2049/// since the allowlists are about what's referenced, not what it's called locally).
2050fn ts_named_imports_from_runtime_modules(src: &str) -> Vec<String> {
2051    let mut out = Vec::new();
2052    for line in src.lines() {
2053        let line = line.trim();
2054        if !line.starts_with("import") {
2055            continue;
2056        }
2057        let is_runtime_module = ["\"./bynk.js\"", "\"./runtime.js\"", "\"../runtime.js\""]
2058            .iter()
2059            .any(|m| line.ends_with(&format!("from {m};")))
2060            || line.contains("bynk/locale/types.js")
2061            || line.contains("cloudflare.js");
2062        if !is_runtime_module {
2063            continue;
2064        }
2065        let Some(open) = line.find('{') else { continue };
2066        let Some(close) = line.find('}') else {
2067            continue;
2068        };
2069        for part in line[open + 1..close].split(',') {
2070            let part = part.trim().trim_start_matches("type ").trim();
2071            if part.is_empty() {
2072                continue;
2073            }
2074            let imported = part.split(" as ").next().unwrap_or(part).trim();
2075            out.push(imported.to_string());
2076        }
2077    }
2078    out
2079}
2080
2081// --- Gated probe 14: incremental_query_types --------------------------------
2082
2083/// Phase 8's own completion criterion (`design/bynk-compiler-trajectory.md` §3,
2084/// "keystroke-to-diagnostic latency by query level"), settled by #1509 (Q5, ADR 0414;
2085/// `design/tracks/incrementality.md` §5) as a one-time **existence** proof, not a count
2086/// trending toward a floor the way every other gated probe in this module is shaped —
2087/// R3.13/R3.14 describe a property to construct, not a defect to exhaust, so a
2088/// shrinking count would be the wrong shape regardless of how it was tuned.
2089///
2090/// **Re-settled by #1537 (2 September 2026), after the 30 August post-restructuring
2091/// review found the probe could not tell adoption from existence.** Phase 8 built all
2092/// four R3.13 levels; only the file level (P8.4's shared parse cache) and the unit
2093/// level's *proof* (P8.2's stability test over `UnitSignature`) had a consumer. The
2094/// definition level (`Body(DefId)`/`TypeOf(DefId)`, 816 lines) and the project level
2095/// (`ProjectGraph`, 174 lines) were reachable only from their own tests, with no
2096/// scheduler to call them and — per R3.15 and #1523 — no trigger yet for one. Both
2097/// were deleted rather than left "available but unwired" (P5), the same decision the
2098/// IR cutover (#1542) reached for phase 6's expression IR. This probe now certifies
2099/// the decision, in both directions:
2100///
2101/// 1. **Unit level** — `struct UnitSignature` exists as real code in `bynk-check`
2102///    (P8.1, ADR 0412). It is the R3.14 firewall's own specification, and the one
2103///    phase 8 artefact #1523's trigger presupposes; it stays as a *proof*, not a
2104///    production path (its only reader is clause 3's test), argued in #1537's ADR.
2105/// 2. **Shared cache** — the file-level parse cache has migrated off
2106///    `PROJECT_UNIT_CACHE` (`bynk-ide/src/completion.rs`) onto one shared,
2107///    `bynk-project`-owned cache (P8.4, ADR 0413). Checked two ways: the old static
2108///    gone from `bynk-ide/src`, *and* some cache-shaped `static`/`struct` present in
2109///    `bynk-project/src` — absence alone would read "migrated" for a bare deletion.
2110/// 3. **Stability test** — some `#[test]` under `bynk-check/tests/` proves
2111///    `UnitSignature`'s stability under a body edit (P8.2): any test name containing
2112///    both `unit_signature` and `stab`.
2113/// 4. **Definition and project levels absent** — no `struct ProjectGraph`, and no
2114///    `DefId`-keyed `fn body(`/`fn type_of(`, in **any** workspace crate's `src/`
2115///    ([`workspace_crate_src_files`], the same walk `unconsumed_ir_items` uses, minus
2116///    `xtask` itself, whose source spells the needles) — not just the two crates
2117///    phase 8 landed them in, since R3.13's own table
2118///    assigns `DefId` bindings to a `bynk-resolve` crate that does not exist yet and
2119///    a rebuild might put them there (review of #1582). `checker.rs`'s own
2120///    pre-existing per-expression `type_of`, which has no `DefId` parameter, does
2121///    not count — the false positive #1510's first run caught, now with the opposite
2122///    consequence. This clause is what makes the probe a gate on #1537 rather than a
2123///    memorial: re-adding either level changes the committed reading and fails the
2124///    currency test, so it needs a consumer and a re-settling — the trigger
2125///    R3.15/#1523 names. **What the gate does not see, stated rather than asserted
2126///    away:** a `DefId` parameter wrapped onto the line after `fn body(` (the
2127///    same-line rule [`defid_query_fn_present`] documents), or a query function under
2128///    any other name. Text-level, like every probe in this file.
2129///
2130/// Every clause is a static read of the tree (never a nested build or test run — see
2131/// [`unit_signature_present`]'s own doc comment for why a "does the stability test
2132/// *pass*" clause was deliberately rejected, #1510's own review-shaped framing).
2133fn incremental_query_types(root: &Path) -> Probe {
2134    let check_src = rust_files(&root.join("bynk-check/src"));
2135    let project_src = rust_files(&root.join("bynk-project/src"));
2136    let ide_src = rust_files(&root.join("bynk-ide/src"));
2137    let check_tests = rust_files(&root.join("bynk-check/tests"));
2138
2139    let unit_present = unit_signature_present(&check_src);
2140    let cache_migrated = shared_cache_migrated(&ide_src, &project_src);
2141    let test_present = stability_test_present(&check_tests);
2142    // Every workspace crate but this one: the harness's own source spells the
2143    // needles it scans for (in these very functions and their tests), so it
2144    // would read as a re-add of both levels on every run.
2145    let workspace_src: Vec<(PathBuf, String)> = workspace_crate_src_files(root)
2146        .into_iter()
2147        .filter(|(krate, _, _)| krate != "xtask")
2148        .map(|(_, path, contents)| (path, contents))
2149        .collect();
2150    let readded = deleted_levels_present(&workspace_src);
2151
2152    let reads = format!(
2153        "unit_signature {}; shared_cache {}; stability_test {}; definition/project levels {}",
2154        if unit_present { "present" } else { "absent" },
2155        if cache_migrated {
2156            "migrated"
2157        } else {
2158            "not migrated (PROJECT_UNIT_CACHE still bynk-ide-local)"
2159        },
2160        if test_present { "present" } else { "absent" },
2161        if readded.is_empty() {
2162            "absent (deleted by #1537)".to_string()
2163        } else {
2164            format!(
2165                "re-added ({}) — need a consumer and a re-settling of #1537",
2166                readded.join(", ")
2167            )
2168        },
2169    );
2170    Probe {
2171        name: "incremental_query_types",
2172        gated: true,
2173        reads,
2174    }
2175}
2176
2177/// Clause 1 of [`incremental_query_types`]: does `struct UnitSignature` exist as real
2178/// code (not a comment or doc prose) in `bynk-check`? The same "grep for the real
2179/// identifier, not the doc claim" discipline `design/tracks/incrementality.md` §1 used
2180/// to measure this reading as zero at settling. Deliberately *not* a "does P8.2's
2181/// fixture pass" check: every gated probe here is a static read of the tree, computed
2182/// from inside `xtask/tests/greenfield_status.rs`'s own `#[test]`; shelling out to
2183/// `cargo test` from inside a running `cargo test` is the identical nested-invocation
2184/// cost [`wildcard_arms`] (the one probe that shells out, and stays trend-only for
2185/// exactly this reason) avoids.
2186fn unit_signature_present(check_src: &[(PathBuf, String)]) -> bool {
2187    any_real_code_line(check_src, "struct UnitSignature")
2188}
2189
2190/// Clause 4 of [`incremental_query_types`]: which of the two levels #1537 deleted are
2191/// back anywhere in `src` — `ProjectGraph` as a real struct, or a `DefId`-keyed
2192/// `body`/`type_of` query function ([`defid_query_fn_present`]). Empty is the
2193/// committed reading; any entry changes the table and fails the currency gate, which
2194/// is the point.
2195fn deleted_levels_present(src: &[(PathBuf, String)]) -> Vec<&'static str> {
2196    let mut found = Vec::new();
2197    if any_real_code_line(src, "struct ProjectGraph") {
2198        found.push("ProjectGraph");
2199    }
2200    if defid_query_fn_present(src, "fn body(") {
2201        found.push("Body");
2202    }
2203    if defid_query_fn_present(src, "fn type_of(") {
2204        found.push("TypeOf");
2205    }
2206    found
2207}
2208
2209/// A `fn_needle`-matching signature line that *also* names `DefId` on the same line —
2210/// not just `fn_needle` alone. **A real, empirically-confirmed false positive this
2211/// slice's own first run caught, not a hypothetical:** `bynk-check/src/checker.rs`
2212/// already has a `pub(crate) fn type_of(expr: &Expr, expected: Option<TyId>, ctx: &mut
2213/// Ctx) -> Option<TyId>` — real, pre-existing, ordinary per-expression type-inference
2214/// plumbing that predates this whole track and has nothing to do with R3.13's
2215/// `DefId`-keyed query — a naive `fn type_of(` scan reads this as `TypeOf` already
2216/// existing on the very first run, before P8.5 does any work at all. Requiring
2217/// `DefId` on the same signature line is a real, if narrow, precision fix: it correctly
2218/// reads false against `checker.rs`'s own `type_of` today, and correctly flips true
2219/// once P8.5 lands a real `DefId`-keyed function, whatever it ends up calling it, as
2220/// long as the parameter appears on the `fn` line itself (a wrapped multi-line
2221/// signature would need widening this scan window — not needed for any function in
2222/// the tree today).
2223fn defid_query_fn_present(files: &[(PathBuf, String)], fn_needle: &str) -> bool {
2224    files.iter().any(|(_, contents)| {
2225        contents.lines().any(|line| {
2226            !is_line_comment(line) && line.contains(fn_needle) && line.contains("DefId")
2227        })
2228    })
2229}
2230
2231/// Whether the file-level parse cache has migrated off `bynk-ide`'s own
2232/// `PROJECT_UNIT_CACHE` onto some shared cache in `bynk-project` — see
2233/// [`incremental_query_types`]'s own doc comment (clause 2) for why *both* halves are
2234/// checked: absence from `bynk-ide` alone cannot distinguish a real migration from a
2235/// bare rename or deletion with nothing shared put in its place. The needle is
2236/// anchored on the `static` declaration line (`static PROJECT_UNIT_CACHE`), not a bare
2237/// substring, so `PROJECT_UNIT_CACHE_CAP` (a real, unrelated `const` in
2238/// `bynk-ide/src/completion.rs`) can't hold this false on its own.
2239fn shared_cache_migrated(ide_src: &[(PathBuf, String)], project_src: &[(PathBuf, String)]) -> bool {
2240    !any_real_code_line(ide_src, "static PROJECT_UNIT_CACHE")
2241        && cache_shaped_item_present(project_src)
2242}
2243
2244/// Whether `bynk-project/src` has a `static`/`struct` item whose name mentions "cache"
2245/// (case-insensitive) — the only crate-boundary-checkable proxy for "some shared cache
2246/// now lives where P8.4 is meant to put it," until that slice pins the real identifier
2247/// down. Deliberately loose, the same "exact name not yet proposed" reasoning
2248/// [`stability_test_present`] already uses.
2249fn cache_shaped_item_present(project_src: &[(PathBuf, String)]) -> bool {
2250    project_src.iter().any(|(_, contents)| {
2251        contents.lines().any(|line| {
2252            if is_line_comment(line) {
2253                return false;
2254            }
2255            let trimmed = line.trim_start();
2256            let is_item = trimmed.starts_with("static ")
2257                || trimmed.starts_with("pub static ")
2258                || trimmed.starts_with("struct ")
2259                || trimmed.starts_with("pub struct ");
2260            is_item && line.to_lowercase().contains("cache")
2261        })
2262    })
2263}
2264
2265/// Whether any `#[test]` fn under `bynk-check/tests/` looks like P8.2's own
2266/// body-edit-stability property test — see [`incremental_query_types`]'s own doc
2267/// comment (clause 3) for why the name match (`unit_signature` + `stab`) is
2268/// deliberately loose. The `#[test]` attribute itself is *not* loose: it must be on the
2269/// matching `fn` line or on a contiguous run of attribute lines directly above it, so a
2270/// same-named non-test helper (a fixture builder, say) can't satisfy this clause.
2271fn stability_test_present(check_tests: &[(PathBuf, String)]) -> bool {
2272    check_tests.iter().any(|(_, contents)| {
2273        let lines: Vec<&str> = contents.lines().collect();
2274        lines.iter().enumerate().any(|(i, line)| {
2275            if is_line_comment(line) {
2276                return false;
2277            }
2278            let lower = line.to_lowercase();
2279            let name_matches =
2280                lower.contains("fn ") && lower.contains("unit_signature") && lower.contains("stab");
2281            if !name_matches {
2282                return false;
2283            }
2284            line.contains("#[test]")
2285                || lines[..i]
2286                    .iter()
2287                    .rev()
2288                    .take_while(|l| l.trim_start().starts_with('#'))
2289                    .any(|l| l.trim() == "#[test]")
2290        })
2291    })
2292}
2293
2294/// Whether any line in `files` (excluding comments) contains `needle` — the shared
2295/// existence-check primitive [`unit_signature_present`]/[`deleted_levels_present`]/
2296/// [`shared_cache_migrated`] all use.
2297fn any_real_code_line(files: &[(PathBuf, String)], needle: &str) -> bool {
2298    files.iter().any(|(_, contents)| {
2299        contents
2300            .lines()
2301            .any(|line| !is_line_comment(line) && line.contains(needle))
2302    })
2303}
2304
2305// --- Gated probe 15: unconsumed_ir_items ------------------------------------
2306
2307/// Slice D3 of #1542 (`design/archive/retired-tracks.md`, the IR cutover's own
2308/// closing summary): the adoption probe the 30 August 2026 post-restructuring
2309/// review (`design/reviews/2026-08-30-post-restructuring-review.md`, Part 5 §8)
2310/// asked for — for each `pub` item in `bynk-ir/src` and `bynk-lower/src`, does
2311/// a production call site exist outside the owning crate and outside a test
2312/// module? That review found phase 6 had shipped an expression IR nothing
2313/// consumed (fifteen `bynk-lower` entry points, twenty-one `bynk-ir` types),
2314/// invisible to every existing gate because each of them certifies that a name
2315/// *exists* in a directory, not that anything *reads* it. Scoped to the two IR
2316/// crates because their entire purpose is to be consumed elsewhere: a `pub`
2317/// item in either with no reader in another crate is, by construction, either
2318/// dead or a second path waiting to be wired in — the P5 failure
2319/// (`bynk-greenfield-compiler.md`) both phases 6 and 8 reproduced.
2320///
2321/// Reads **0** at its own landing, by construction: Slices D0–D2 deleted every
2322/// unconsumed item (D1 also demoted the two crate-internal helpers,
2323/// `lower_fn_sig_ir_from_types`/`lower_op_sig_ir_from_commons`, that would
2324/// otherwise have read as 2). Gated as a ratchet: a new `pub` item in either
2325/// crate with no consumer moves it off zero and fails
2326/// `greenfield_status_table_is_current`, so the "available but unwired" state
2327/// cannot land silently again. The reading names the offending items so the
2328/// failure is actionable, not just a count.
2329///
2330/// **What counts as a consumer.** A non-comment line, outside any `#[cfg(test)]
2331/// mod` range ([`test_mod_ranges`]), in a workspace crate's `src/` that is
2332/// **neither owner crate**, containing the item's name as a whole word. The
2333/// two owners do not vouch for each other, on purpose (review of this slice's
2334/// own PR, #1581): run against pre-D0 `main` with only the owning crate
2335/// excluded, every one of phase 6's twenty-one unconsumed `bynk-ir` types
2336/// would have read as consumed, because `bynk-lower`'s own unconsumed
2337/// constructors named them — the probe would have missed half the surface it
2338/// was built to see. With both excluded, its first honest run read 5
2339/// (`IndexIr` and the four `MUTATING_*_OPS` tables, read only from
2340/// `bynk-lower`), resolved by inlining the alias and moving the tables beside
2341/// their one reader rather than arguing a floor. Text-level, like every probe
2342/// in this file — a name that happens to be shared with an unrelated item in
2343/// another crate would read as consumed (a false negative for the ratchet,
2344/// never a false positive that blocks a PR), accepted the same way
2345/// [`ts_writes`]'s own known over-count is. Items are `pub` at column zero
2346/// only — `fn`, `struct`, `enum`, `type`, `const`, `static`, `trait`, `union`,
2347/// with any `async`/`unsafe`/`const`/`extern` qualifier on a `fn` — see
2348/// [`column_zero_pub_item_name`]: `pub(crate)` is by definition not offered
2349/// to another crate, a `pub` item nested inside an `impl` block is reachable
2350/// only through its owner (which is what gets counted), and `pub use`/`pub
2351/// mod` re-export rather than declare.
2352fn unconsumed_ir_items(root: &Path) -> Probe {
2353    let owners = ["bynk-ir", "bynk-lower"];
2354    let mut owner_files = Vec::new();
2355    for owner in owners {
2356        for (path, contents) in rust_files_relative(&root.join(owner).join("src")) {
2357            owner_files.push((owner.to_string(), path, contents));
2358        }
2359    }
2360    let consumer_files = workspace_crate_src_files(root);
2361    let unconsumed = unconsumed_pub_items(&owners, &owner_files, &consumer_files);
2362    let reads = if unconsumed.is_empty() {
2363        "0".to_string()
2364    } else {
2365        format!("{} ({})", unconsumed.len(), unconsumed.join(", "))
2366    };
2367    Probe {
2368        name: "unconsumed_ir_items",
2369        gated: true,
2370        reads,
2371    }
2372}
2373
2374/// Every `.rs` file under `<crate>/src` for every workspace crate — each entry
2375/// tagged with its crate directory name so [`unconsumed_pub_items`] can exclude
2376/// an item's own crate. A workspace crate is any immediate child of `root` with
2377/// both a `Cargo.toml` and a `src/`, the same shape every `members` entry in the
2378/// root manifest has; reading the manifest itself would add a TOML parse for no
2379/// gain in precision.
2380fn workspace_crate_src_files(root: &Path) -> Vec<(String, PathBuf, String)> {
2381    let mut out = Vec::new();
2382    let Ok(entries) = std::fs::read_dir(root) else {
2383        return out;
2384    };
2385    let mut crates: Vec<PathBuf> = entries
2386        .flatten()
2387        .map(|e| e.path())
2388        .filter(|p| p.join("Cargo.toml").is_file() && p.join("src").is_dir())
2389        .collect();
2390    crates.sort();
2391    for krate in crates {
2392        let name = krate
2393            .file_name()
2394            .map(|n| n.to_string_lossy().into_owned())
2395            .unwrap_or_default();
2396        for (path, contents) in rust_files_relative(&krate.join("src")) {
2397            out.push((name.clone(), path, contents));
2398        }
2399    }
2400    out
2401}
2402
2403/// [`unconsumed_ir_items`]'s counting logic over explicit `(crate, relative
2404/// path, contents)` lists — see [`rust_files_relative`] for why this isn't
2405/// `root: &Path`. Returns `crate::item` for every column-zero `pub` item in
2406/// `owner_files` that no non-comment, non-test line in a file belonging to a
2407/// crate outside `owners` names as a whole word; sorted and deduplicated (a
2408/// name declared in two files of one crate is one item, not two), so the
2409/// reading is stable across runs and across a crate being split into modules.
2410///
2411/// Each consumer file is split and its `#[cfg(test)]` ranges computed once,
2412/// up front, not once per candidate item (review of #1581): this runs under
2413/// `cargo test --workspace` on every Rust-touching PR, and the failure path —
2414/// an item with no consumer — is exactly the one that scans every file.
2415fn unconsumed_pub_items(
2416    owners: &[&str],
2417    owner_files: &[(String, PathBuf, String)],
2418    consumer_files: &[(String, PathBuf, String)],
2419) -> Vec<String> {
2420    let mut items: Vec<(String, String)> = Vec::new();
2421    for (krate, _, contents) in owner_files {
2422        let lines: Vec<&str> = contents.lines().collect();
2423        let ranges = test_mod_ranges(&lines);
2424        for (i, line) in lines.iter().enumerate() {
2425            if in_test_range(i, &ranges) {
2426                continue;
2427            }
2428            if let Some(name) = column_zero_pub_item_name(line) {
2429                items.push((krate.clone(), name.to_string()));
2430            }
2431        }
2432    }
2433    items.sort();
2434    items.dedup();
2435
2436    // Production, non-owner lines only — preprocessed once.
2437    let consumer_lines: Vec<&str> = consumer_files
2438        .iter()
2439        .filter(|(krate, _, _)| !owners.contains(&krate.as_str()))
2440        .flat_map(|(_, _, contents)| {
2441            let lines: Vec<&str> = contents.lines().collect();
2442            let ranges = test_mod_ranges(&lines);
2443            lines
2444                .iter()
2445                .enumerate()
2446                .filter(|(i, line)| !in_test_range(*i, &ranges) && !is_line_comment(line))
2447                .map(|(_, line)| *line)
2448                .collect::<Vec<_>>()
2449        })
2450        .collect();
2451
2452    items
2453        .iter()
2454        .filter(|(_, name)| !consumer_lines.iter().any(|line| contains_word(line, name)))
2455        .map(|(owner, name)| format!("{owner}::{name}"))
2456        .collect()
2457}
2458
2459/// The name of a column-zero `pub` item declaration, if `line` is one: `pub`
2460/// followed by `fn`, `struct`, `enum`, `type`, `const`, `static`, `trait` or
2461/// `union`, where a `fn` may carry `async`/`unsafe`/`const`/`extern "…"`
2462/// qualifiers in any order. `pub(crate)`/`pub(super)` do not qualify (they
2463/// are not offered to other crates), and neither does anything indented (a
2464/// method or associated item, reachable only through its owner), nor `pub
2465/// use`/`pub mod` (re-exports and module declarations, not items).
2466fn column_zero_pub_item_name(line: &str) -> Option<&str> {
2467    let mut rest = line.strip_prefix("pub ")?;
2468    // `pub const fn` / `pub async unsafe fn` / `pub unsafe extern "C" fn` …:
2469    // peel qualifiers until the item keyword is exposed. A bare `pub const X`
2470    // is a constant, not a qualifier, so `const` only peels when a `fn`
2471    // (possibly behind further qualifiers) follows it.
2472    loop {
2473        if let Some(r) = rest
2474            .strip_prefix("async ")
2475            .or_else(|| rest.strip_prefix("unsafe "))
2476        {
2477            rest = r;
2478            continue;
2479        }
2480        if let Some(r) = rest.strip_prefix("extern ") {
2481            // `extern "C" fn` — skip the ABI string if present.
2482            let r = r.trim_start();
2483            let r = if let Some(after_quote) = r.strip_prefix('"') {
2484                after_quote
2485                    .find('"')
2486                    .map(|q| after_quote[q + 1..].trim_start())
2487                    .unwrap_or(r)
2488            } else {
2489                r
2490            };
2491            rest = r;
2492            continue;
2493        }
2494        if let Some(r) = rest.strip_prefix("const ")
2495            && (r.starts_with("fn ")
2496                || r.starts_with("async ")
2497                || r.starts_with("unsafe ")
2498                || r.starts_with("extern "))
2499        {
2500            rest = r;
2501            continue;
2502        }
2503        break;
2504    }
2505    let rest = [
2506        "fn ", "struct ", "enum ", "type ", "const ", "static ", "trait ", "union ",
2507    ]
2508    .iter()
2509    .find_map(|kw| rest.strip_prefix(kw))?;
2510    let end = rest
2511        .find(|c: char| !(c.is_ascii_alphanumeric() || c == '_'))
2512        .unwrap_or(rest.len());
2513    (end > 0).then(|| &rest[..end])
2514}
2515
2516/// Does `line` contain `word` as a whole identifier — not as a prefix or
2517/// suffix of a longer one (`IrExpr` inside `IrExprKind` must not count)?
2518fn contains_word(line: &str, word: &str) -> bool {
2519    let bytes = line.as_bytes();
2520    let mut from = 0;
2521    while let Some(pos) = line[from..].find(word) {
2522        let start = from + pos;
2523        let end = start + word.len();
2524        let before_ok = start == 0 || !is_ident_byte(bytes[start - 1]);
2525        let after_ok = end == bytes.len() || !is_ident_byte(bytes[end]);
2526        if before_ok && after_ok {
2527            return true;
2528        }
2529        from = start + 1;
2530    }
2531    false
2532}
2533
2534fn is_ident_byte(b: u8) -> bool {
2535    b.is_ascii_alphanumeric() || b == b'_'
2536}
2537
2538// --- Reported probe 1: wildcard_arms --------------------------------------
2539
2540/// R2.12. `clippy::wildcard_enum_match_arm` diagnostics, forced on via `-W` so the
2541/// count is real from day one and doesn't wait on `workspace_lints`/T0.3 (#999 Decision
2542/// C — delegating to clippy's own type-aware pass, rather than a hand-rolled scan for
2543/// "compiler-owned enum", so the probe and the enforcement mechanism can never
2544/// disagree). A count, not a boolean — moves on nearly every match statement anyone
2545/// writes, so it is reported, not gated (#999 Decision D).
2546fn wildcard_arms(root: &Path) -> Probe {
2547    let reads = match run_clippy_wildcard_scan(root) {
2548        Ok(n) => n.to_string(),
2549        Err(e) => format!("error running clippy: {e}"),
2550    };
2551    Probe {
2552        name: "wildcard_arms",
2553        gated: false,
2554        reads,
2555    }
2556}
2557
2558/// Runs clippy with the lint forced on and parses the NDJSON output properly —
2559/// **not** a substring count. A single `wildcard_enum_match_arm` diagnostic's JSON
2560/// repeats the lint name several times (the `code` field, the human-readable message,
2561/// the `#[warn(...)]` note, and the `rendered` field duplicating the whole thing as
2562/// text), so `stdout.matches("wildcard_enum_match_arm").count()` overcounts by roughly
2563/// 3x — caught by cross-checking this probe's own first run against a real JSON parse
2564/// (296 real diagnostics, not the naive scan's 888).
2565///
2566/// Checks the process exit status: a forced `-W` (not `-D`) never fails the build on
2567/// account of the lint itself, so a non-zero exit means clippy genuinely could not run
2568/// (a compile error elsewhere, a missing toolchain component, offline with no cached
2569/// index) — in which case stdout carries no `compiler-message` lines and a silent
2570/// success would report a false, and indistinguishable, `0`. This probe is reported,
2571/// not gated, precisely so an honest "couldn't measure" surfaces loudly here rather
2572/// than being read as "closed."
2573fn run_clippy_wildcard_scan(root: &Path) -> std::io::Result<usize> {
2574    let output = Command::new("cargo")
2575        .args([
2576            "clippy",
2577            "--workspace",
2578            "--message-format=json",
2579            "--",
2580            "-W",
2581            "clippy::wildcard_enum_match_arm",
2582        ])
2583        .current_dir(root)
2584        .output()?;
2585    if !output.status.success() {
2586        return Err(std::io::Error::other(format!(
2587            "cargo clippy exited with {}: {}",
2588            output.status,
2589            String::from_utf8_lossy(&output.stderr).trim()
2590        )));
2591    }
2592    let stdout = String::from_utf8_lossy(&output.stdout);
2593    let mut count = 0usize;
2594    for line in stdout.lines() {
2595        let Ok(value) = serde_json::from_str::<serde_json::Value>(line) else {
2596            continue;
2597        };
2598        if value.get("reason").and_then(|r| r.as_str()) != Some("compiler-message") {
2599            continue;
2600        }
2601        let code = value.pointer("/message/code/code").and_then(|c| c.as_str());
2602        if code == Some("clippy::wildcard_enum_match_arm") {
2603            count += 1;
2604        }
2605    }
2606    Ok(count)
2607}
2608
2609// --- Reported probe 2: keep_in_sync ---------------------------------------
2610
2611/// P2 (trend only). Comments across the workspace containing "in sync", "mirrors",
2612/// "parity", or "must match" — each one names a rule the compiler cannot teach itself
2613/// and must be taught in review, every time.
2614fn keep_in_sync(root: &Path) -> Probe {
2615    let phrases = ["in sync", "mirrors", "parity", "must match"];
2616    let mut count = 0usize;
2617    for dir in top_level_crate_dirs(root) {
2618        for (_, contents) in rust_files(&dir.join("src")) {
2619            for line in contents.lines() {
2620                if is_line_comment(line) {
2621                    let lower = line.to_lowercase();
2622                    if phrases.iter().any(|p| lower.contains(p)) {
2623                        count += 1;
2624                    }
2625                }
2626            }
2627        }
2628    }
2629    Probe {
2630        name: "keep_in_sync",
2631        gated: false,
2632        reads: count.to_string(),
2633    }
2634}
2635
2636// --- Reported probe 3: test_density ---------------------------------------
2637
2638/// R11.1, and §3.4's phase-3 trigger. Per crate: (lines inside `#[test]` fn bodies,
2639/// plus lines inside `#[cfg(test)] mod` blocks outside those fns) ÷ (non-blank,
2640/// non-comment lines under that crate's `src/`) — #999 Decision F's definition,
2641/// written down precisely because an undefined "ratio" is exactly the ambiguity that
2642/// produced the track doc §9's four-row ambiguity.
2643fn test_density(root: &Path) -> Probe {
2644    let mut parts = Vec::new();
2645    for dir in top_level_crate_dirs(root) {
2646        let name = dir.file_name().unwrap().to_string_lossy().to_string();
2647        let src_dir = dir.join("src");
2648        let mut test_lines = 0usize;
2649        let mut code_lines = 0usize;
2650        for (_, contents) in rust_files(&src_dir) {
2651            let lines: Vec<&str> = contents.lines().collect();
2652            let ranges = test_mod_ranges(&lines);
2653            for (i, line) in lines.iter().enumerate() {
2654                let is_blank_or_comment = line.trim().is_empty() || is_line_comment(line);
2655                if !is_blank_or_comment {
2656                    code_lines += 1;
2657                }
2658                if in_test_range(i, &ranges) && !is_blank_or_comment {
2659                    test_lines += 1;
2660                }
2661            }
2662        }
2663        if code_lines > 0 {
2664            let ratio = 100.0 * test_lines as f64 / code_lines as f64;
2665            parts.push(format!("{name}={ratio:.1}%"));
2666        }
2667    }
2668    Probe {
2669        name: "test_density",
2670        gated: false,
2671        reads: parts.join(", "),
2672    }
2673}
2674
2675// --- diagnostic_coverage ---------------------------------------------------
2676
2677/// #1662 (track #1648, G2). Registry codes that some test **asserts**, out of all
2678/// `bynk_syntax::diagnostics::REGISTRY` codes, and the count no test asserts.
2679///
2680/// Static, so it runs inside this harness: the review that set the baseline
2681/// (2026-10-01, #1647 Part 4) instrumented `CompileError::new` across a full
2682/// `cargo test --workspace`, which this harness cannot afford. Asserted is a
2683/// subset of produced (a passing test that names a code saw it), so driving the
2684/// unasserted count to the argued floor meets "every reachable code is produced
2685/// by a test" a fortiori.
2686///
2687/// A code counts as asserted when it appears in:
2688/// - line 1 of a negative fixture's `expected_error.txt` (line 2 is a message
2689///   substring and may quote other codes);
2690/// - any other non-`.bynk` file under a crate's `tests/` directory (test
2691///   sources, expected-diagnostics files, JSON goldens). `.bynk` sources are
2692///   skipped, because their comments often name the code they provoke, and Rust
2693///   sources are read with their `//` comments removed, for the same reason;
2694/// - a `#[cfg(test)] mod` block in a crate's `src/`, comments removed;
2695/// - a blessed diagnostic transcript, `site/src/diagnostics/*.txt`.
2696///
2697/// Gated at the argued floor of **4** (#1662 Decision B). Each of the four is
2698/// emitted where no compiler test can reach it:
2699/// - `bynk.deploy.contract_skew`: `bynk deploy`, against a live deployment's
2700///   lock;
2701/// - `bynk.project.read_failed`: only when a host's file overlay omits a
2702///   discovered file, which neither the CLI nor the LSP does;
2703/// - `bynk.target.vendor_conflict`: needs platform-native capabilities from two
2704///   platforms, and only Cloudflare ships any today (the decision function is
2705///   unit-tested);
2706/// - `bynk.wasm.strip_failed`: stripping the compiler's own emitted TypeScript,
2707///   which fails only on an emitter bug.
2708///
2709/// `cargo xtask greenfield-status --list-unasserted` prints the codes.
2710pub fn unasserted_codes(root: &Path) -> Vec<&'static str> {
2711    let registry: BTreeSet<&'static str> = bynk_syntax::diagnostics::REGISTRY
2712        .iter()
2713        .map(|d| d.code)
2714        .collect();
2715    let mut asserted: BTreeSet<String> = BTreeSet::new();
2716    let mut note = |text: &str| {
2717        for code in registry_tokens(text) {
2718            asserted.insert(code);
2719        }
2720    };
2721    for krate in top_level_crate_dirs(root) {
2722        for (path, contents) in text_files(&krate.join("tests")) {
2723            if path.extension().is_some_and(|e| e == "bynk") {
2724                continue;
2725            }
2726            if path.file_name().is_some_and(|n| n == "expected_error.txt") {
2727                note(
2728                    contents
2729                        .lines()
2730                        .find(|l| !l.trim().is_empty())
2731                        .unwrap_or(""),
2732                );
2733            } else if path.extension().is_some_and(|e| e == "rs") {
2734                note(&strip_line_comments(&contents));
2735            } else {
2736                note(&contents);
2737            }
2738        }
2739        for (_, contents) in rust_files(&krate.join("src")) {
2740            let lines: Vec<&str> = contents.lines().collect();
2741            for (start, end) in test_mod_ranges(&lines) {
2742                note(&strip_line_comments(&lines[start..=end].join("\n")));
2743            }
2744        }
2745    }
2746    for (path, contents) in text_files(&root.join("site/src/diagnostics")) {
2747        if path.extension().is_some_and(|e| e == "txt") {
2748            note(&contents);
2749        }
2750    }
2751    registry
2752        .into_iter()
2753        .filter(|c| !asserted.contains(*c))
2754        .collect()
2755}
2756
2757/// `src` with every `//` comment (including `///` and `//!` doc comments)
2758/// removed, so a code named only in prose does not count as asserted. A `//`
2759/// inside a string literal is kept; block comments are rare enough in this
2760/// workspace to leave.
2761fn strip_line_comments(src: &str) -> String {
2762    src.lines()
2763        .map(|line| {
2764            let bytes = line.as_bytes();
2765            let (mut in_str, mut escaped) = (false, false);
2766            for (i, &b) in bytes.iter().enumerate() {
2767                if escaped {
2768                    escaped = false;
2769                } else if b == b'\\' && in_str {
2770                    escaped = true;
2771                } else if b == b'"' {
2772                    in_str = !in_str;
2773                } else if b == b'/' && !in_str && bytes.get(i + 1) == Some(&b'/') {
2774                    return &line[..i];
2775                }
2776            }
2777            line
2778        })
2779        .collect::<Vec<_>>()
2780        .join("\n")
2781}
2782
2783/// Every `bynk.<family>.<name>` token in `text` — the shape of a registry code,
2784/// whether quoted (Rust source) or bare (fixtures, transcripts).
2785fn registry_tokens(text: &str) -> Vec<String> {
2786    let bytes = text.as_bytes();
2787    let ident = |b: u8| b.is_ascii_alphanumeric() || b == b'_' || b == b'.';
2788    let mut out = Vec::new();
2789    let mut i = 0;
2790    while let Some(rel) = text[i..].find("bynk.") {
2791        let start = i + rel;
2792        let mut end = start;
2793        while end < bytes.len() && ident(bytes[end]) {
2794            end += 1;
2795        }
2796        if start == 0 || !ident(bytes[start - 1]) {
2797            out.push(text[start..end].trim_end_matches('.').to_string());
2798        }
2799        i = end.max(start + 1);
2800    }
2801    out
2802}
2803
2804/// Every readable UTF-8 file under `dir`, recursively, as `(path, contents)`.
2805fn text_files(dir: &Path) -> Vec<(PathBuf, String)> {
2806    let mut out = Vec::new();
2807    let mut stack = vec![dir.to_path_buf()];
2808    while let Some(d) = stack.pop() {
2809        let Ok(entries) = std::fs::read_dir(&d) else {
2810            continue;
2811        };
2812        for entry in entries.flatten() {
2813            let path = entry.path();
2814            if path.is_dir() {
2815                stack.push(path);
2816            } else if let Ok(contents) = std::fs::read_to_string(&path) {
2817                out.push((path, contents));
2818            }
2819        }
2820    }
2821    out
2822}
2823
2824fn diagnostic_coverage(root: &Path) -> Probe {
2825    let total = bynk_syntax::diagnostics::REGISTRY.len();
2826    let unasserted = unasserted_codes(root).len();
2827    Probe {
2828        name: "diagnostic_coverage",
2829        gated: true,
2830        reads: format!(
2831            "unasserted={unasserted} (asserted {}/{total})",
2832            total - unasserted
2833        ),
2834    }
2835}
2836
2837// --- Reported probe 4: fixture_kinds --------------------------------------
2838
2839/// R11.2. Fixture directories under `bynkc/tests` using each assertion granularity —
2840/// `expected_contains.txt` / `expected_absent.txt` / `expected_diagnostics.txt` — set
2841/// against the older, coarser `expected_error.txt` (category-string) convention.
2842///
2843/// #1660 (runtime-semantics track §3.5) added two counts:
2844/// - `warnings` (`expected_warnings.txt`, a positive fixture's pinned warnings), which
2845///   this probe had never counted;
2846/// - `run` (`expected_run.txt`), the positive fixtures whose `suite`s
2847///   `bynkc/tests/behaviour_fixtures.rs` actually *runs*, the one granularity that
2848///   asserts runtime behaviour rather than emitted text or diagnostics.
2849fn fixture_kinds(root: &Path) -> Probe {
2850    let tests_dir = root.join("bynkc/tests");
2851    let contains = count_files_named(&tests_dir, "expected_contains.txt");
2852    let absent = count_files_named(&tests_dir, "expected_absent.txt");
2853    let diagnostics = count_files_named(&tests_dir, "expected_diagnostics.txt");
2854    let error = count_files_named(&tests_dir, "expected_error.txt");
2855    let warnings = count_files_named(&tests_dir, "expected_warnings.txt");
2856    let run = count_files_named(&tests_dir, "expected_run.txt");
2857    Probe {
2858        name: "fixture_kinds",
2859        gated: false,
2860        reads: format!(
2861            "contains={contains}, absent={absent}, diagnostics={diagnostics}, error={error}, warnings={warnings}, run={run}"
2862        ),
2863    }
2864}
2865
2866fn count_files_named(dir: &Path, filename: &str) -> usize {
2867    let mut count = 0usize;
2868    count_files_named_walk(dir, filename, &mut count);
2869    count
2870}
2871
2872fn count_files_named_walk(dir: &Path, filename: &str, count: &mut usize) {
2873    let Ok(entries) = std::fs::read_dir(dir) else {
2874        return;
2875    };
2876    for entry in entries.flatten() {
2877        let path = entry.path();
2878        if path.is_dir() {
2879            count_files_named_walk(&path, filename, count);
2880        } else if path.file_name().is_some_and(|n| n == filename) {
2881            *count += 1;
2882        }
2883    }
2884}
2885
2886// --- Reported probe 5: keystroke_latency ------------------------------------
2887
2888/// Phase 8's own trend-only probe (`design/bynk-compiler-trajectory.md` §3,
2889/// "keystroke-to-diagnostic latency by query level") — settled (Q3/Q5, ADR 0414;
2890/// `design/tracks/incrementality.md` §5) as staying **"not measured" for this whole
2891/// phase's lifetime**: R3.15's scheduler decision defers whole (no memo table, salsa or
2892/// otherwise, ships in phase 8), and the literal latency number presupposes query
2893/// levels attributing latency to — levels [`incremental_query_types`] itself proves
2894/// exist, but attributing real latency to them needs a scheduler this phase
2895/// deliberately does not build. Added now, not deferred to whenever a scheduler
2896/// exists, so the trajectory's own §3.0 baseline table carries a live, CI-computed row
2897/// instead of a static doc claim — the same "instrument even a number that won't move
2898/// yet" precedent `test_density`/`fixture_kinds` already set for this module.
2899fn keystroke_latency(_root: &Path) -> Probe {
2900    Probe {
2901        name: "keystroke_latency",
2902        gated: false,
2903        reads: "not measured — no scheduler exists yet (R3.15, deferred whole this phase)"
2904            .to_string(),
2905    }
2906}
2907
2908// --- Rendering + diffing ---------------------------------------------------
2909
2910/// The committed table: a plain Markdown table, probe name → gated?/reads, plus a
2911/// pointer to the rule ledger `stamp::apply` writes (#1001).
2912pub fn render_table(report: &Report) -> String {
2913    let mut out = String::new();
2914    out.push_str("<!-- GENERATED FILE — do not edit by hand.\n");
2915    out.push_str("     Source: cargo xtask greenfield-status (xtask/src/greenfield_status.rs).\n");
2916    out.push_str("     Regenerate with: cargo xtask greenfield-status --apply -->\n\n");
2917    out.push_str("# Greenfield status\n\n");
2918    out.push_str(
2919        "Track slice T0.0 (#999); `ts_writes`/`ts_any` added by P7.0 (#1296); \
2920         `verbatim_origins`/`verbatim_sites` added by P7.5 (#1307); \
2921         `incremental_query_types`/`keystroke_latency` added by P8.0 (#1510); \
2922         `unconsumed_ir_items` added by Slice D3 of the IR cutover (#1542); \
2923         `diagnostic_coverage` added by #1662. Sixteen \
2924         probes are gated — a disagreement between this file and a fresh run fails \
2925         `greenfield_status_table_is_current` (`xtask/tests/greenfield_status.rs`). \
2926         Five are trend probes, reported only.\n\n",
2927    );
2928    out.push_str("| Probe | Gated | Reads |\n|---|---|---|\n");
2929    for probe in &report.probes {
2930        let _ = writeln!(
2931            out,
2932            "| `{}` | {} | {} |",
2933            probe.name,
2934            if probe.gated { "yes" } else { "no (trend)" },
2935            probe.reads
2936        );
2937    }
2938
2939    out.push_str("\n## Rules closed\n\n");
2940    // A static, unconditional link — not a count, and not even an existence
2941    // check. A first draft read `design/greenfield-status-rules.md` here to
2942    // report a row count, but nothing regenerates *this* file when `stamp`
2943    // writes the ledger (`stamp.yml` never runs `greenfield-status --apply`,
2944    // and the gating test only diffs the nine probes) — so a count or an
2945    // exists/doesn't-exist message would silently go stale the moment the
2946    // first `closes_rule` landed, which is exactly the drift this section
2947    // exists to avoid, not invite (#1001 review). Static text can't go stale;
2948    // the ledger is one click away either way.
2949    out.push_str(
2950        "See [`design/greenfield-status-rules.md`](greenfield-status-rules.md) for rule ids \
2951         closed so far (written by `cargo xtask stamp --apply` at merge; may not exist yet if \
2952         no increment has cited `closes_rule`).\n",
2953    );
2954    out
2955}
2956
2957/// Every gated probe whose live reading disagrees with the committed table's, as
2958/// `(probe name, committed, live)`. Trend probes are never compared, and never
2959/// computed here — this only runs the sixteen gated probes, so checking currency never
2960/// pays for `wildcard_arms`'s workspace-wide clippy pass. For a caller that has already
2961/// run the full report (e.g. to print it), use [`gated_disagreements_in`] instead so the
2962/// sixteen gated probes aren't computed a second time.
2963pub fn gated_disagreements(root: &Path) -> Vec<(String, String, String)> {
2964    gated_disagreements_in(&run_gated(root), root)
2965}
2966
2967/// Like [`gated_disagreements`], but diffs `probes` (typically a [`Report`]'s
2968/// `.probes`, already computed) instead of re-running the gated probes.
2969pub fn gated_disagreements_in(probes: &[Probe], root: &Path) -> Vec<(String, String, String)> {
2970    let committed = std::fs::read_to_string(table_path(root)).unwrap_or_default();
2971    let mut out = Vec::new();
2972    for probe in probes.iter().filter(|p| p.gated) {
2973        let row_prefix = format!("| `{}` | yes | ", probe.name);
2974        let committed_reads = committed
2975            .lines()
2976            .find(|l| l.starts_with(&row_prefix))
2977            .and_then(|l| l.strip_prefix(&row_prefix))
2978            .and_then(|l| l.strip_suffix(" |"))
2979            .unwrap_or("<row missing>");
2980        if committed_reads != probe.reads {
2981            out.push((
2982                probe.name.to_string(),
2983                committed_reads.to_string(),
2984                probe.reads.clone(),
2985            ));
2986        }
2987    }
2988    out
2989}
2990
2991/// The disagreements no pending file acknowledges — what actually fails the gate.
2992///
2993/// A gated probe may move in a PR in one of two ways: the PR regenerates
2994/// `design/greenfield-status.md` itself (the original mechanism, still valid for
2995/// a change that carries no increment), or the PR's `design/pending/<slug>.md`
2996/// names the probe in its `probes` key and leaves the table alone. The second
2997/// keeps the acknowledgement in review — a probe still cannot move silently —
2998/// without every probe-moving PR editing the same shared rows, which is what
2999/// made this table the repo's most-conflicted file. The stamp then rewrites the
3000/// gated rows on `main` in merge order ([`refresh_gated_rows`]).
3001pub fn unacknowledged(
3002    disagreements: Vec<(String, String, String)>,
3003    acknowledged: &BTreeSet<String>,
3004) -> Vec<(String, String, String)> {
3005    disagreements
3006        .into_iter()
3007        .filter(|(name, _, _)| !acknowledged.contains(name))
3008        .collect()
3009}
3010
3011/// `committed` (the table's current text) with every gated row's reading
3012/// replaced by `probes`' live one; trend rows, prose and the rule-ledger pointer
3013/// are left exactly as committed, so the stamp never pays for the trend probes'
3014/// workspace-wide clippy pass. Errors naming any gated probe with no row to
3015/// rewrite — a table that has lost a row needs a full
3016/// `cargo xtask greenfield-status --apply`, not a silent append.
3017pub fn refresh_gated_rows(committed: &str, probes: &[Probe]) -> Result<String, String> {
3018    let mut out = String::with_capacity(committed.len());
3019    let mut seen = BTreeSet::new();
3020    for line in committed.split_inclusive('\n') {
3021        let replacement = probes.iter().filter(|p| p.gated).find_map(|p| {
3022            let prefix = format!("| `{}` | yes | ", p.name);
3023            line.starts_with(&prefix).then(|| {
3024                seen.insert(p.name);
3025                let eol = if line.ends_with('\n') { "\n" } else { "" };
3026                format!("{prefix}{} |{eol}", p.reads)
3027            })
3028        });
3029        out.push_str(replacement.as_deref().unwrap_or(line));
3030    }
3031    let missing: Vec<_> = probes
3032        .iter()
3033        .filter(|p| p.gated && !seen.contains(p.name))
3034        .map(|p| p.name)
3035        .collect();
3036    if missing.is_empty() {
3037        Ok(out)
3038    } else {
3039        Err(format!(
3040            "design/greenfield-status.md has no row for gated probe(s) {} — run \
3041             `cargo xtask greenfield-status --apply`",
3042            missing.join(", ")
3043        ))
3044    }
3045}
3046
3047/// The stamp's refreshed table: [`refresh_gated_rows`], but only if every gated
3048/// probe whose live reading differs from `committed` is named by one of the
3049/// increments being stamped (`acknowledged` — the union of their `probes`).
3050///
3051/// Rewriting every gated row unconditionally would silently absorb a move that
3052/// reached `main` with no acknowledgement (a bypass push, say): after the stamp
3053/// the table would match by construction, and nothing downstream could notice.
3054/// Refusing instead keeps the invariant the gate enforces on PRs — a gated probe
3055/// never moves unacknowledged — true on `main` too. The stamp computes this
3056/// before applying, so a refusal leaves nothing written and the pending files
3057/// in place.
3058pub fn refresh_for_stamp(
3059    committed: &str,
3060    probes: &[Probe],
3061    acknowledged: &BTreeSet<String>,
3062) -> Result<String, String> {
3063    let unnamed: Vec<String> = probes
3064        .iter()
3065        .filter(|p| p.gated && !acknowledged.contains(p.name))
3066        .filter_map(|p| {
3067            let prefix = format!("| `{}` | yes | ", p.name);
3068            let committed_reads = committed
3069                .lines()
3070                .find(|l| l.starts_with(&prefix))?
3071                .strip_prefix(&prefix)?
3072                .strip_suffix(" |")?;
3073            (committed_reads != p.reads).then(|| {
3074                format!(
3075                    "  {}: committed {committed_reads:?}, live {:?}",
3076                    p.name, p.reads
3077                )
3078            })
3079        })
3080        .collect();
3081    if !unnamed.is_empty() {
3082        return Err(format!(
3083            "gated greenfield-status probe(s) moved without being named by any increment being \
3084             stamped — refusing to absorb them into the table. Acknowledge them in a pending \
3085             file or regenerate the table in a PR:\n{}",
3086            unnamed.join("\n")
3087        ));
3088    }
3089    refresh_gated_rows(committed, probes)
3090}
3091
3092#[cfg(test)]
3093mod tests {
3094    use super::*;
3095
3096    // --- probe acknowledgement + refresh ------------------------------------
3097
3098    fn gated(name: &'static str, reads: &str) -> Probe {
3099        Probe {
3100            name,
3101            gated: true,
3102            reads: reads.to_string(),
3103        }
3104    }
3105
3106    #[test]
3107    fn unacknowledged_drops_only_acknowledged_probes() {
3108        let d = vec![
3109            (
3110                "ts_writes".to_string(),
3111                "850".to_string(),
3112                "851".to_string(),
3113            ),
3114            ("ts_any".to_string(), "26".to_string(), "27".to_string()),
3115        ];
3116        let ack: BTreeSet<String> = ["ts_writes".to_string()].into();
3117        let left = unacknowledged(d, &ack);
3118        assert_eq!(left.len(), 1);
3119        assert_eq!(left[0].0, "ts_any");
3120    }
3121
3122    #[test]
3123    fn refresh_rewrites_gated_rows_and_leaves_the_rest() {
3124        let committed = "# Greenfield status\n\n| Probe | Gated | Reads |\n|---|---|---|\n\
3125                         | `ts_writes` | yes | 850 |\n\
3126                         | `wildcard_arms` | no (trend) | 355 |\n\nfooter\n";
3127        let out = refresh_gated_rows(committed, &[gated("ts_writes", "851")]).unwrap();
3128        assert!(out.contains("| `ts_writes` | yes | 851 |\n"));
3129        assert!(out.contains("| `wildcard_arms` | no (trend) | 355 |\n"));
3130        assert!(out.ends_with("\nfooter\n"));
3131        assert_eq!(out.lines().count(), committed.lines().count());
3132    }
3133
3134    const STAMP_TABLE: &str = "| Probe | Gated | Reads |\n|---|---|---|\n\
3135                               | `ts_writes` | yes | 850 |\n\
3136                               | `ts_any` | yes | 26 |\n";
3137
3138    #[test]
3139    fn stamp_refresh_rewrites_named_moves() {
3140        let ack: BTreeSet<String> = ["ts_writes".to_string()].into();
3141        let out = refresh_for_stamp(
3142            STAMP_TABLE,
3143            &[gated("ts_writes", "851"), gated("ts_any", "26")],
3144            &ack,
3145        )
3146        .unwrap();
3147        assert!(out.contains("| `ts_writes` | yes | 851 |"));
3148        assert!(out.contains("| `ts_any` | yes | 26 |"));
3149    }
3150
3151    #[test]
3152    fn stamp_refresh_refuses_an_unnamed_move() {
3153        let ack: BTreeSet<String> = ["ts_writes".to_string()].into();
3154        let err = refresh_for_stamp(
3155            STAMP_TABLE,
3156            &[gated("ts_writes", "851"), gated("ts_any", "27")],
3157            &ack,
3158        )
3159        .unwrap_err();
3160        assert!(err.contains("ts_any"), "{err}");
3161        assert!(!err.contains("ts_writes"), "{err}");
3162    }
3163
3164    #[test]
3165    fn refresh_errors_on_a_missing_gated_row() {
3166        let err = refresh_gated_rows("| Probe | Gated | Reads |\n", &[gated("ts_any", "26")])
3167            .unwrap_err();
3168        assert!(err.contains("ts_any"));
3169    }
3170
3171    // --- emit_diagnostics (#999 Decision A) ---------------------------------
3172
3173    /// A standalone `"bynk.foo"` literal is found — the ordinary case.
3174    #[test]
3175    fn bynk_dotted_literals_finds_standalone_literal() {
3176        let src = r#"code("bynk.check.something", "a message")"#;
3177        assert_eq!(bynk_dotted_literals(src), vec!["bynk.check.something"]);
3178    }
3179
3180    /// The bug this slice found in its own first draft: a longer message that merely
3181    /// *starts* with "bynk." must not be truncated into a fake code literal. Regression
3182    /// test for `bynk.map itself uses bynk.list, so list must be injected too: {paths:?}`
3183    /// (`bynk-emit/src/project.rs`), which an earlier, less careful version of this scan
3184    /// wrongly counted as the literal `"bynk.map"`.
3185    #[test]
3186    fn bynk_dotted_literals_ignores_prefix_of_a_longer_message() {
3187        let src = r#"assert!(cond, "bynk.map itself uses bynk.list, so list must be injected too: {paths:?}");"#;
3188        assert!(bynk_dotted_literals(src).is_empty());
3189    }
3190
3191    /// Regression test for the other half of the same bug: a `\`-continued string
3192    /// literal (`"bynk.emit.unresolved_cross_context_signature: no signature for \`,
3193    /// continued on the next source line) is one string, not a diagnostic-code literal,
3194    /// even though its first segment matches the identifier charset — because the
3195    /// character after the run is `:`, never a closing quote, on either line.
3196    #[test]
3197    fn bynk_dotted_literals_ignores_a_line_continued_message() {
3198        let src =
3199            "\"bynk.emit.unresolved_cross_context_signature: no signature for \\\n     the rest\"";
3200        assert!(bynk_dotted_literals(src).is_empty());
3201    }
3202
3203    /// The whole point of Decision A: cross-referencing the real registry, not a
3204    /// hand-maintained exclusion list, correctly separates a real diagnostic code from
3205    /// a commons/namespace path that merely looks like one.
3206    #[test]
3207    fn emit_diagnostics_cross_references_the_real_registry() {
3208        let registry: BTreeSet<&str> = bynk_syntax::diagnostics::REGISTRY
3209            .iter()
3210            .map(|d| d.code)
3211            .collect();
3212        // A code this registry is known to carry (bynk-syntax/src/diagnostics.rs).
3213        assert!(registry.contains("bynk.parse.expected_expression"));
3214        // A commons/namespace path, not a diagnostic code — #999's own verified survey.
3215        assert!(!registry.contains("bynk.locale"));
3216    }
3217
3218    // --- ast_importers (#1176) ------------------------------------------------
3219
3220    /// The exclusion is named, not prefixed: `project/tests_emit.rs` is the
3221    /// Q7-settled `Ir → String` half that keeps hand-writing TypeScript by calling
3222    /// straight into `emitter.rs`'s own body-rendering, and
3223    /// keeps reading a handler's declared param/return `TypeRef` with no `TyId`
3224    /// available at that call site — but `project.rs` (which also imports
3225    /// `bynk_syntax::ast`, via `EmitProjectCtx`) must stay counted, and so, per
3226    /// review of #1210, must `emitter.rs`/`emitter/lower.rs` themselves: both still
3227    /// hold live AST-*declaration* reads (`emitter.rs`'s `CommonsItem::Service`/
3228    /// `svc.protocol` walk, `emitter/lower.rs`'s `cap_op_param_names`) that are the
3229    /// still-open R6.13 defect this probe tracks, not the Q7 kind — excluding either
3230    /// file would hide that real work the same way a path-prefix rule would. A
3231    /// path-prefix rule (e.g. "only `emitter/**` counts") would have excluded
3232    /// `project.rs` right along with the legitimate ones, silently undercounting
3233    /// real work. (`ir.rs`/`ir/lower.rs`, the lowering pass's own former `Ast → Ir`
3234    /// exclusion, left this list at Arc D's P7.12 crate carve — they left
3235    /// `bynk-emit/src` entirely, not merely this list.)
3236    #[test]
3237    fn ast_importer_exclusion_is_named_not_prefixed() {
3238        assert!(is_named_ast_importer(Path::new("project/tests_emit.rs")));
3239        assert!(is_named_ast_importer(Path::new("emitter/serialisation.rs")));
3240        assert!(!is_named_ast_importer(Path::new("project.rs")));
3241        assert!(!is_named_ast_importer(Path::new("emitter.rs")));
3242        assert!(!is_named_ast_importer(Path::new("emitter/lower.rs")));
3243        assert!(!is_named_ast_importer(Path::new("emitter/workers.rs")));
3244        assert!(!is_named_ast_importer(Path::new("ir.rs")));
3245        assert!(!is_named_ast_importer(Path::new("ir/lower.rs")));
3246    }
3247
3248    /// #1184 review: an `AST_IMPORTER_EXCEPTIONS` entry going stale (renamed or split,
3249    /// e.g. `ir/lower.rs` becoming `ir/lower/mod.rs`) must fail loud here, not surface
3250    /// as a silent `ast_importers` regression in `greenfield_status_table_is_current` —
3251    /// mirrors [`file_is_named_fs_floor`]'s own "fail loud, not quiet" discipline.
3252    #[test]
3253    fn ast_importer_exceptions_still_exist_and_still_import_the_ast() {
3254        let dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
3255            .join("..")
3256            .join("bynk-emit/src");
3257        for rel in AST_IMPORTER_EXCEPTIONS {
3258            let contents = std::fs::read_to_string(dir.join(rel)).unwrap_or_else(|e| {
3259                panic!("AST_IMPORTER_EXCEPTIONS entry {rel:?} does not exist: {e}")
3260            });
3261            assert!(
3262                contents.contains("bynk_syntax::ast"),
3263                "AST_IMPORTER_EXCEPTIONS entry {rel:?} no longer imports bynk_syntax::ast \
3264                 — it excludes nothing and should be removed"
3265            );
3266        }
3267    }
3268
3269    /// #1184 review, extended by #1187's own closing scoping pass (and narrowed by
3270    /// review of #1210, which found `emitter.rs`/`emitter/lower.rs` still hold live,
3271    /// in-scope AST-declaration reads and must stay counted) and by P6.33's own
3272    /// re-settling (`emitter/serialisation.rs`, a phase boundary rather than a
3273    /// declaration-read exemption): exercises the real filter over the live tree, not
3274    /// just the pure predicate — the survivor set the PR's own named-vs-prefix
3275    /// argument depends on: the named exclusions drop out
3276    /// (`project/tests_emit.rs`'s Q7-settled `Ir → String` case and
3277    /// `emitter/serialisation.rs`'s phase-7 codec renderer), while `emitter.rs`/
3278    /// `emitter/lower.rs`/`emitter/workers.rs` do not. `ir.rs`/`ir/lower.rs` (the
3279    /// lowering pass's own former `Ast → Ir` pair, excluded here until Arc D's
3280    /// P7.12 crate carve) are asserted absent below for a different reason now:
3281    /// they left `bynk-emit/src` entirely, so `ast_importer_files` never walks
3282    /// them at all, named exclusion or not.
3283    ///
3284    /// P6.49 (phase 6's own §6b): `project.rs` and `project/diagnostics.rs`
3285    /// join the *excluded* side of this assertion — the opposite of what this test
3286    /// checked before. `project.rs` cleared without joining
3287    /// [`AST_IMPORTER_EXCEPTIONS`]: nine slices (P6.42–P6.49) either relocated its
3288    /// remaining declaration reads to the `bynk-check`/`bynk-project` crates that
3289    /// already own the data, or re-exported a type from a `bynk-check` module whose
3290    /// own public API was already parameterised by it (the P6.27 `ExprId` precedent,
3291    /// applied to `TypeDecl`/`FnDecl`/`Visibility`/`ActorDecl`) — real, verified
3292    /// movement, not a probe exemption. `project/diagnostics.rs` rides on it, per the
3293    /// same super-glob rule this file's own regression guard below pins.
3294    #[test]
3295    fn ast_importers_excludes_the_named_pairs_and_project_rs() {
3296        let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("..");
3297        let dir = root.join("bynk-emit/src");
3298        let counted: BTreeSet<String> = ast_importer_files(&root)
3299            .into_iter()
3300            .map(|path| {
3301                path.strip_prefix(&dir)
3302                    .unwrap_or(&path)
3303                    .to_string_lossy()
3304                    .replace('\\', "/")
3305            })
3306            .collect();
3307        assert!(!counted.contains("ir.rs"), "moved to bynk-ir at P7.12");
3308        assert!(
3309            !counted.contains("ir/lower.rs"),
3310            "moved to bynk-lower at P7.12"
3311        );
3312        assert!(!counted.contains("project/tests_emit.rs"));
3313        assert!(!counted.contains("emitter/serialisation.rs"));
3314        assert!(!counted.contains("project.rs"));
3315        assert!(!counted.contains("project/diagnostics.rs"));
3316        assert!(counted.contains("emitter.rs"));
3317        assert!(counted.contains("emitter/lower.rs"));
3318        assert!(counted.contains("emitter/workers.rs"));
3319    }
3320
3321    /// P6.26 review (#1259): a module-level `use super::*;` is a real inheritance
3322    /// channel (Rust's own privacy rule makes a parent's private `use` visible to
3323    /// descendants) — must be detected — but a `use super::*;` nested inside a
3324    /// `#[cfg(test)] mod tests { .. }` block glob-imports its own *immediately
3325    /// enclosing* module, not the grandparent file on disk, and must not
3326    /// false-positive.
3327    #[test]
3328    fn module_level_super_glob_detection_ignores_nested_test_mod() {
3329        assert!(has_module_level_super_glob(
3330            "use std::fmt;\nuse super::*;\n"
3331        ));
3332        assert!(!has_module_level_super_glob(
3333            "fn f() {}\n\n#[cfg(test)]\nmod tests {\n    use super::*;\n}\n"
3334        ));
3335    }
3336
3337    /// P6.26 review (#1259): pins the real scenario the review found —
3338    /// `emitter/emit.rs` and `emitter/lower.rs` both carry a live, module-level
3339    /// `use super::*;` inheriting from `emitter.rs`, which itself still imports
3340    /// `bynk_syntax::ast` directly. Regression guard: if a future slice deletes
3341    /// either child's own explicit AST import while this inheritance channel and
3342    /// the parent's own AST dependency both remain, [`ast_importer_files`] must
3343    /// keep counting it rather than silently dropping the probe.
3344    #[test]
3345    fn super_glob_children_of_an_ast_importing_parent_are_detected() {
3346        let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("..");
3347        let dir = root.join("bynk-emit/src");
3348        for rel in ["emitter/emit.rs", "emitter/lower.rs"] {
3349            let contents = std::fs::read_to_string(dir.join(rel))
3350                .unwrap_or_else(|e| panic!("{rel:?} does not exist: {e}"));
3351            assert!(
3352                has_module_level_super_glob(&contents),
3353                "{rel:?} no longer carries a module-level `use super::*;` — this \
3354                 regression guard (and the false-zero hazard it pins) no longer applies \
3355                 and may be deleted"
3356            );
3357            assert_eq!(
3358                super_glob_parent_imports_ast(&dir, Path::new(rel)),
3359                Some(true),
3360                "{rel:?}'s parent (`emitter.rs`) no longer imports bynk_syntax::ast — \
3361                 update this guard's expectation"
3362            );
3363        }
3364        // A file directly under `bynk-emit/src` (no directory component) has no
3365        // `use super::*;` parent to inherit from.
3366        assert_eq!(
3367            super_glob_parent_imports_ast(&dir, Path::new("emitter.rs")),
3368            None
3369        );
3370    }
3371
3372    // --- fs_below_driver / test_density (trailing `#[cfg(test)] mod tests {}`) ---
3373
3374    #[test]
3375    fn production_std_fs_usage_is_detected() {
3376        let src = "fn load(p: &Path) -> String {\n    std::fs::read_to_string(p).unwrap()\n}\n";
3377        assert!(has_production_std_fs(src));
3378    }
3379
3380    #[test]
3381    fn std_fs_inside_a_trailing_test_mod_is_not_production() {
3382        let src = "fn load(p: &Path) -> String {\n    String::new()\n}\n\n#[cfg(test)]\nmod tests {\n    #[test]\n    fn t() {\n        std::fs::write(\"x\", \"y\").unwrap();\n    }\n}\n";
3383        assert!(!has_production_std_fs(src));
3384    }
3385
3386    /// Regression test for the other real bug this slice found: `bynk-emit/src/lib.rs`
3387    /// has `#[cfg(test)] pub(crate) mod testkit;` — an external-file module
3388    /// *declaration* (ends in `;`), not an inline block. It must not be mistaken for a
3389    /// scope-opening `mod tests { ... }`, or the (genuinely production) code after it in
3390    /// the same file would be wrongly excluded.
3391    #[test]
3392    fn cfg_test_external_mod_declaration_does_not_open_a_test_region() {
3393        let src = "#[cfg(test)]\npub(crate) mod testkit;\n\nfn load(p: &Path) -> String {\n    std::fs::read_to_string(p).unwrap()\n}\n";
3394        assert!(has_production_std_fs(src));
3395    }
3396
3397    // --- unconsumed_ir_items (Slice D3 of #1542) ----------------------------
3398
3399    fn files(entries: &[(&str, &str, &str)]) -> Vec<(String, PathBuf, String)> {
3400        entries
3401            .iter()
3402            .map(|(k, p, c)| (k.to_string(), PathBuf::from(p), c.to_string()))
3403            .collect()
3404    }
3405
3406    /// The ordinary case: a `pub` item read from another crate's production
3407    /// code is consumed; one read from nowhere is not.
3408    #[test]
3409    fn unconsumed_pub_items_reports_only_items_with_no_other_crate_reader() {
3410        let owners = files(&[(
3411            "bynk-ir",
3412            "lib.rs",
3413            "pub struct Used;\npub struct Unused;\npub fn helper() {}\n",
3414        )]);
3415        let consumers = files(&[
3416            (
3417                "bynk-ir",
3418                "lib.rs",
3419                "pub struct Used;\npub struct Unused;\npub fn helper() {}\n",
3420            ),
3421            (
3422                "bynk-emit",
3423                "a.rs",
3424                "fn f(x: bynk_ir::Used) { helper(); }\n",
3425            ),
3426        ]);
3427        assert_eq!(
3428            unconsumed_pub_items(&["bynk-ir"], &owners, &consumers),
3429            vec!["bynk-ir::Unused"]
3430        );
3431    }
3432
3433    /// A mention in a comment, inside a `#[cfg(test)]` module, or in the
3434    /// owning crate itself is not a consumer.
3435    #[test]
3436    fn unconsumed_pub_items_ignores_comments_tests_and_the_owning_crate() {
3437        let owners = files(&[(
3438            "bynk-lower",
3439            "lib.rs",
3440            "pub fn lower_x() {}\npub fn lower_y() {}\npub fn lower_z() {}\n",
3441        )]);
3442        let consumers = files(&[
3443            (
3444                "bynk-lower",
3445                "lib.rs",
3446                "pub fn lower_x() {}\nfn own() { lower_x(); }\n",
3447            ),
3448            (
3449                "bynk-emit",
3450                "a.rs",
3451                "// lower_y() used to be called here\n/// and [`lower_y`] linked here\n",
3452            ),
3453            (
3454                "bynk-emit",
3455                "b.rs",
3456                "fn prod() {}\n\n#[cfg(test)]\nmod tests {\n    fn t() { lower_z(); }\n}\n",
3457            ),
3458        ]);
3459        assert_eq!(
3460            unconsumed_pub_items(&["bynk-lower"], &owners, &consumers),
3461            vec![
3462                "bynk-lower::lower_x",
3463                "bynk-lower::lower_y",
3464                "bynk-lower::lower_z"
3465            ]
3466        );
3467    }
3468
3469    /// Whole-word matching: `IrExpr` inside `IrExprKind` is not a read of
3470    /// `IrExpr`, and `pub(crate)`/indented items are not offered to other
3471    /// crates so are never counted either way.
3472    #[test]
3473    fn unconsumed_pub_items_matches_whole_words_and_skips_non_public_items() {
3474        let owners = files(&[(
3475            "bynk-ir",
3476            "lib.rs",
3477            "pub struct IrExpr;\npub enum IrExprKind {}\npub(crate) fn internal() {}\nimpl IrExpr {\n    pub fn method() {}\n}\n",
3478        )]);
3479        let consumers = files(&[("bynk-emit", "a.rs", "fn f(k: IrExprKind) {}\n")]);
3480        assert_eq!(
3481            unconsumed_pub_items(&["bynk-ir"], &owners, &consumers),
3482            vec!["bynk-ir::IrExpr"]
3483        );
3484    }
3485
3486    /// The two IR crates do not vouch for each other (review of #1581): a
3487    /// `bynk-ir` type read only from `bynk-lower` is unconsumed, and so is a
3488    /// `bynk-lower` helper read only from `bynk-ir` — pre-D0 `main`'s exact
3489    /// shape, where `bynk-lower`'s own unconsumed constructors named every
3490    /// unconsumed `bynk-ir` type.
3491    #[test]
3492    fn unconsumed_pub_items_does_not_let_owner_crates_vouch_for_each_other() {
3493        let owners = files(&[
3494            (
3495                "bynk-ir",
3496                "lib.rs",
3497                "pub struct IrExpr;\npub struct Shape;\n",
3498            ),
3499            (
3500                "bynk-lower",
3501                "lib.rs",
3502                "pub fn lower_expr_ir() -> IrExpr { IrExpr }\npub fn shape() -> Shape { Shape }\n",
3503            ),
3504        ]);
3505        let consumers = files(&[
3506            (
3507                "bynk-ir",
3508                "lib.rs",
3509                "pub struct IrExpr;\npub struct Shape;\n",
3510            ),
3511            (
3512                "bynk-lower",
3513                "lib.rs",
3514                "pub fn lower_expr_ir() -> IrExpr { IrExpr }\npub fn shape() -> Shape { Shape }\n",
3515            ),
3516            (
3517                "bynk-emit",
3518                "a.rs",
3519                "fn f() -> Shape { bynk_lower::shape() }\n",
3520            ),
3521        ]);
3522        assert_eq!(
3523            unconsumed_pub_items(&["bynk-ir", "bynk-lower"], &owners, &consumers),
3524            vec!["bynk-ir::IrExpr", "bynk-lower::lower_expr_ir"]
3525        );
3526    }
3527
3528    /// One item declared in two files of the same crate (a split module) is
3529    /// reported once, not twice.
3530    #[test]
3531    fn unconsumed_pub_items_deduplicates_a_name_declared_in_two_files() {
3532        let owners = files(&[
3533            ("bynk-lower", "a.rs", "pub fn twice() {}\n"),
3534            ("bynk-lower", "b.rs", "pub fn twice() {}\n"),
3535        ]);
3536        assert_eq!(
3537            unconsumed_pub_items(&["bynk-lower"], &owners, &[]),
3538            vec!["bynk-lower::twice"]
3539        );
3540    }
3541
3542    #[test]
3543    fn column_zero_pub_item_name_accepts_every_item_kind_and_fn_qualifier() {
3544        assert_eq!(column_zero_pub_item_name("pub fn f(x: i32) {}"), Some("f"));
3545        assert_eq!(column_zero_pub_item_name("pub struct S<'a> {"), Some("S"));
3546        assert_eq!(column_zero_pub_item_name("pub enum E {"), Some("E"));
3547        assert_eq!(
3548            column_zero_pub_item_name("pub type T = (u8, u8);"),
3549            Some("T")
3550        );
3551        assert_eq!(
3552            column_zero_pub_item_name("pub const C: &[&str] = &[];"),
3553            Some("C")
3554        );
3555        assert_eq!(
3556            column_zero_pub_item_name("pub static S: &[&str] = &[];"),
3557            Some("S")
3558        );
3559        assert_eq!(column_zero_pub_item_name("pub trait Tr {}"), Some("Tr"));
3560        assert_eq!(column_zero_pub_item_name("pub union U {"), Some("U"));
3561        // `fn` qualifiers, alone and stacked (review of #1581).
3562        assert_eq!(column_zero_pub_item_name("pub async fn a() {}"), Some("a"));
3563        assert_eq!(column_zero_pub_item_name("pub unsafe fn u() {}"), Some("u"));
3564        assert_eq!(column_zero_pub_item_name("pub const fn c() {}"), Some("c"));
3565        assert_eq!(
3566            column_zero_pub_item_name("pub extern \"C\" fn x() {}"),
3567            Some("x")
3568        );
3569        assert_eq!(
3570            column_zero_pub_item_name("pub const unsafe fn cu() {}"),
3571            Some("cu")
3572        );
3573        assert_eq!(
3574            column_zero_pub_item_name("pub unsafe extern \"C\" fn ue() {}"),
3575            Some("ue")
3576        );
3577        assert_eq!(
3578            column_zero_pub_item_name("pub async unsafe fn au() {}"),
3579            Some("au")
3580        );
3581        // Not items offered to another crate.
3582        assert_eq!(column_zero_pub_item_name("pub(crate) fn g() {}"), None);
3583        assert_eq!(column_zero_pub_item_name("pub(super) struct P;"), None);
3584        assert_eq!(column_zero_pub_item_name("    pub fn method() {}"), None);
3585        assert_eq!(column_zero_pub_item_name("pub use foo::Bar;"), None);
3586        assert_eq!(column_zero_pub_item_name("pub mod m;"), None);
3587        assert_eq!(column_zero_pub_item_name("pub impl Foo {}"), None);
3588    }
3589
3590    /// [`workspace_crate_src_files`] tags each file with its crate directory
3591    /// and only walks directories that are actually crates (a `Cargo.toml`
3592    /// *and* a `src/`), so a stray directory with one but not the other is
3593    /// neither an owner nor a consumer.
3594    #[test]
3595    fn workspace_crate_src_files_tags_files_by_crate_and_skips_non_crates() {
3596        let root = std::env::temp_dir().join(format!(
3597            "bynk-xtask-unconsumed-{}-{}",
3598            std::process::id(),
3599            std::time::SystemTime::now()
3600                .duration_since(std::time::UNIX_EPOCH)
3601                .map(|d| d.as_nanos())
3602                .unwrap_or(0)
3603        ));
3604        let mk = |rel: &str, contents: &str| {
3605            let p = root.join(rel);
3606            std::fs::create_dir_all(p.parent().unwrap()).unwrap();
3607            std::fs::write(p, contents).unwrap();
3608        };
3609        mk("alpha/Cargo.toml", "[package]\nname = \"alpha\"\n");
3610        mk("alpha/src/lib.rs", "pub fn a() {}\n");
3611        mk("alpha/src/inner/mod.rs", "pub fn b() {}\n");
3612        mk("beta/Cargo.toml", "[package]\nname = \"beta\"\n");
3613        mk("beta/src/lib.rs", "fn c() { alpha::a() }\n");
3614        mk("no-src/Cargo.toml", "[package]\nname = \"no-src\"\n");
3615        mk("no-manifest/src/lib.rs", "pub fn d() {}\n");
3616        let files = workspace_crate_src_files(&root);
3617        let _ = std::fs::remove_dir_all(&root);
3618        let mut tagged: Vec<(String, String)> = files
3619            .iter()
3620            .map(|(k, p, _)| (k.clone(), p.to_string_lossy().replace('\\', "/")))
3621            .collect();
3622        tagged.sort();
3623        assert_eq!(
3624            tagged,
3625            vec![
3626                ("alpha".to_string(), "inner/mod.rs".to_string()),
3627                ("alpha".to_string(), "lib.rs".to_string()),
3628                ("beta".to_string(), "lib.rs".to_string()),
3629            ]
3630        );
3631    }
3632
3633    /// Regression test for the bug caught in review: a file with **two** scattered
3634    /// `#[cfg(test)] mod ... { ... }` blocks, with real production code between them —
3635    /// exactly `bynk-emit/src/emitter/lower.rs`'s shape (two test modules, 1031
3636    /// production lines apart). A single "everything from the first/last `#[cfg(test)]`
3637    /// onward" cutoff would misclassify `lower_lambda` here as test-scope; the fix must
3638    /// close each block at its own boundary and resume production scanning after it.
3639    #[test]
3640    fn production_code_between_two_scattered_test_mods_is_detected() {
3641        let src = "\
3642#[cfg(test)]
3643mod decode_map_key_tests {
3644    #[test]
3645    fn t() {
3646        assert_eq!(1, 1);
3647    }
3648}
3649
3650fn lower_lambda(p: &Path) -> String {
3651    std::fs::read_to_string(p).unwrap()
3652}
3653
3654#[cfg(test)]
3655mod idempotency_scoping_tests {
3656    #[test]
3657    fn t2() {
3658        assert_eq!(2, 2);
3659    }
3660}
3661";
3662        assert!(has_production_std_fs(src));
3663    }
3664
3665    /// The same fixture's `test_mod_ranges` shape, checked directly: two disjoint
3666    /// ranges, not one span from the first block to the last.
3667    #[test]
3668    fn test_mod_ranges_finds_each_block_separately() {
3669        let src = "\
3670#[cfg(test)]
3671mod a {
3672    fn x() {}
3673}
3674
3675fn production() {}
3676
3677#[cfg(test)]
3678mod b {
3679    fn y() {}
3680}
3681";
3682        let lines: Vec<&str> = src.lines().collect();
3683        let ranges = test_mod_ranges(&lines);
3684        assert_eq!(
3685            ranges.len(),
3686            2,
3687            "expected two disjoint test-mod ranges: {ranges:?}"
3688        );
3689        // Line 5 (0-indexed) is `fn production() {}`, between the two blocks.
3690        assert!(
3691            !in_test_range(5, &ranges),
3692            "production() must not read as test-scope"
3693        );
3694    }
3695
3696    /// Regression test for the bug in the *fix* for the above: a column-0-`}`
3697    /// shortcut (tried and reverted during review) truncates a test module the moment
3698    /// its body embeds a multi-line fixture string containing a `}` flush against the
3699    /// left margin — exactly `bynk-ide/src/sequence.rs`'s shape, whose test mod embeds
3700    /// `.bynk` source fixtures. The real brace-depth scanner must see through the
3701    /// string and find the module's *actual* closing brace, hundreds of lines later.
3702    /// Uses a raw string for the outer fixture so the embedded `"..."` doesn't need
3703    /// escaping, and locates the real end by content rather than a hand-counted index
3704    /// — a hand-counted line number is exactly the kind of easy-to-miscount detail
3705    /// this codebase's own convention (verify, don't assume) warns against.
3706    #[test]
3707    fn test_mod_ranges_is_not_fooled_by_a_column_zero_brace_inside_a_string() {
3708        let src = r#"#[cfg(test)]
3709mod tests {
3710    const FIXTURE: &str = "
3711commons app.demo {
3712}
3713";
3714
3715    fn real_end_of_module() {}
3716}
3717"#;
3718        let lines: Vec<&str> = src.lines().collect();
3719        let ranges = test_mod_ranges(&lines);
3720        assert_eq!(ranges.len(), 1, "expected exactly one range: {ranges:?}");
3721        let (_, end) = ranges[0];
3722        // `str::lines()` drops the trailing newline, so the module's real closing
3723        // brace — the fixture's last line — is at `lines.len() - 1`. The string's
3724        // embedded `}` (an earlier line) must not be mistaken for it.
3725        assert_eq!(
3726            end,
3727            lines.len() - 1,
3728            "closed too early — mistook the string's `}}` for the module's: {ranges:?}"
3729        );
3730    }
3731
3732    // --- fs_below_driver: import resolution through `use super::*;` (#1013) ---
3733
3734    /// Run [`production_std_fs_files`] over an in-memory crate layout and name the
3735    /// flagged files, so each case reads as "these files, and only these".
3736    fn flagged(files: &[(&str, &str)]) -> Vec<String> {
3737        let owned: Vec<(PathBuf, String)> = files
3738            .iter()
3739            .map(|(p, s)| (PathBuf::from(p), (*s).to_string()))
3740            .collect();
3741        production_std_fs_files(&owned)
3742            .into_iter()
3743            .map(|i| files[i].0.to_string())
3744            .collect()
3745    }
3746
3747    /// The concrete #1013 instance, in miniature: `project.rs` has a module-level
3748    /// `use std::fs;` (ancestor-scoped, so visible to descendants), `discovery.rs`
3749    /// glob-imports it via `use super::*;` and calls bare `fs::read_to_string` —
3750    /// touching `std::fs` in production while never spelling it. The text scan alone
3751    /// reads only `project.rs`; the resolved probe must read both.
3752    #[test]
3753    fn bare_fs_reached_through_a_glob_imported_parent_is_flagged() {
3754        let files = [
3755            ("lib.rs", "mod project;\n"),
3756            ("project.rs", "use std::fs;\n\nmod discovery;\n"),
3757            (
3758                "project/discovery.rs",
3759                "use super::*;\n\nfn read_source(path: &std::path::Path) -> String {\n    fs::read_to_string(path).unwrap()\n}\n",
3760            ),
3761        ];
3762        assert!(
3763            !has_production_std_fs(files[2].1),
3764            "the text scan alone must miss it"
3765        );
3766        assert_eq!(flagged(&files), vec!["project.rs", "project/discovery.rs"]);
3767    }
3768
3769    /// Without `use super::*;` there is no path from the bare `fs::` to the parent's
3770    /// binding — the probe must not guess one into existence.
3771    #[test]
3772    fn bare_fs_without_a_glob_super_import_is_not_flagged() {
3773        let files = [
3774            ("lib.rs", "mod project;\n"),
3775            ("project.rs", "use std::fs;\n\nmod discovery;\n"),
3776            (
3777                "project/discovery.rs",
3778                "fn read_source(path: &std::path::Path) -> String {\n    fs::read_to_string(path).unwrap()\n}\n",
3779            ),
3780        ];
3781        assert_eq!(flagged(&files), vec!["project.rs"]);
3782    }
3783
3784    /// Glob chains re-reach ancestors transitively — grandparent binds `fs`, both
3785    /// hops glob-import `super::*` — and the `mod.rs` layout maps to the same module
3786    /// tree as the `name.rs` one. The middle file sees `fs` but never uses it, so
3787    /// only the leaf joins the (text-flagged) root.
3788    #[test]
3789    fn glob_super_resolution_is_transitive_across_mod_rs_parents() {
3790        let files = [
3791            ("lib.rs", "use std::fs;\n\nmod a;\n"),
3792            ("a/mod.rs", "use super::*;\n\nmod b;\n"),
3793            (
3794                "a/b.rs",
3795                "use super::*;\n\nfn walk() {\n    let _ = fs::read_dir(\".\");\n}\n",
3796            ),
3797        ];
3798        assert_eq!(flagged(&files), vec!["lib.rs", "a/b.rs"]);
3799    }
3800
3801    /// A break anywhere in the chain stops resolution: the middle module does not
3802    /// glob-import `super::*`, so the leaf's `use super::*;` reaches a module with no
3803    /// `fs` binding to offer.
3804    #[test]
3805    fn a_break_in_the_glob_chain_stops_resolution() {
3806        let files = [
3807            ("lib.rs", "use std::fs;\n\nmod a;\n"),
3808            ("a/mod.rs", "mod b;\n"),
3809            (
3810                "a/b.rs",
3811                "use super::*;\n\nfn walk() {\n    let _ = fs::read_dir(\".\");\n}\n",
3812            ),
3813        ];
3814        assert_eq!(flagged(&files), vec!["lib.rs"]);
3815    }
3816
3817    /// Nearest binding wins, as in Rust: the child re-binds `fs` to something that is
3818    /// not `std::fs`, so its bare `fs::` calls are that something's, not std's.
3819    #[test]
3820    fn a_local_non_std_binding_shadows_the_ancestors_std_fs() {
3821        let files = [
3822            ("lib.rs", "mod project;\n"),
3823            ("project.rs", "use std::fs;\n\nmod overlay;\nmod d;\n"),
3824            ("project/overlay.rs", "pub fn read(_p: &str) {}\n"),
3825            (
3826                "project/d.rs",
3827                "use super::*;\nuse crate::project::overlay as fs;\n\nfn f() {\n    let _ = fs::read(\"x\");\n}\n",
3828            ),
3829        ];
3830        assert_eq!(flagged(&files), vec!["project.rs"]);
3831    }
3832
3833    /// An aliased module binding resolves under its alias — the call site never
3834    /// contains the substring `fs::` at all.
3835    #[test]
3836    fn an_aliased_std_fs_binding_resolves_through_the_glob() {
3837        let files = [
3838            ("lib.rs", "mod p;\n"),
3839            ("p.rs", "use std::fs as stdfs;\n\nmod c;\n"),
3840            (
3841                "p/c.rs",
3842                "use super::*;\n\nfn f() {\n    stdfs::write(\"a\", \"b\").unwrap();\n}\n",
3843            ),
3844        ];
3845        assert_eq!(flagged(&files), vec!["p.rs", "p/c.rs"]);
3846    }
3847
3848    /// `use std::{fs, io};` binds `fs` without ever containing the substring
3849    /// `std::fs` — the same blind spot as #1013's, one file deep. Resolution applies
3850    /// in the file's own scope, no glob import required.
3851    #[test]
3852    fn a_group_imported_fs_binding_is_resolved_in_its_own_file() {
3853        let src = "use std::{fs, io};\n\nfn f() -> io::Result<()> {\n    fs::metadata(\"x\").map(|_| ())\n}\n";
3854        assert!(
3855            !has_production_std_fs(src),
3856            "the text scan alone must miss it"
3857        );
3858        let files = [("thing.rs", src)];
3859        assert_eq!(flagged(&files), vec!["thing.rs"]);
3860    }
3861
3862    /// The item-import shape #1013 scope-checked (zero current instances), at the
3863    /// granularity this probe can reach: an ancestor's `use std::fs::File;` used as a
3864    /// bare path root `File::open` in a glob-importing child resolves and flags. (A
3865    /// bare *call* of an imported fn — `read_to_string(p)`, no `::` — presents no
3866    /// path root and remains out of a text-level scanner's reach, per the doc.)
3867    #[test]
3868    fn an_item_import_under_std_fs_resolves_as_a_path_root() {
3869        let files = [
3870            ("lib.rs", "mod p;\n"),
3871            ("p.rs", "use std::fs::File;\n\nmod c;\n"),
3872            (
3873                "p/c.rs",
3874                "use super::*;\n\nfn f() {\n    let _ = File::open(\"x\");\n}\n",
3875            ),
3876        ];
3877        assert_eq!(flagged(&files), vec!["p.rs", "p/c.rs"]);
3878    }
3879
3880    /// A test module's `use super::*;` and tempdir `fs::` calls are test-scope — the
3881    /// `bynk-ide` files' shape (`architecture.rs`, `sequence.rs`), which must stay
3882    /// unflagged exactly as they were under the text-only scan.
3883    #[test]
3884    fn glob_and_bare_fs_inside_a_test_mod_stay_test_scope() {
3885        let files = [
3886            ("lib.rs", "use std::fs;\n\nmod w;\n"),
3887            (
3888                "w.rs",
3889                "fn production() {}\n\n#[cfg(test)]\nmod tests {\n    use super::*;\n    use std::fs;\n\n    #[test]\n    fn t() {\n        let _ = fs::read_dir(\".\");\n    }\n}\n",
3890            ),
3891        ];
3892        assert_eq!(flagged(&files), vec!["lib.rs"]);
3893    }
3894
3895    /// The module-tree mapping behind the resolution, checked directly: `name.rs` and
3896    /// `mod.rs` layouts, a preferred `a.rs` over `a/mod.rs`, and rootless roots.
3897    #[test]
3898    fn module_parent_maps_both_file_layouts() {
3899        let files: Vec<(PathBuf, String)> = ["lib.rs", "a.rs", "a/b.rs", "c/mod.rs", "c/d.rs"]
3900            .iter()
3901            .map(|p| (PathBuf::from(p), String::new()))
3902            .collect();
3903        let idx = |name: &str| {
3904            files
3905                .iter()
3906                .position(|(p, _)| p == Path::new(name))
3907                .unwrap()
3908        };
3909        assert_eq!(module_parent(Path::new("lib.rs"), &files), None);
3910        assert_eq!(
3911            module_parent(Path::new("a.rs"), &files),
3912            Some(idx("lib.rs"))
3913        );
3914        assert_eq!(
3915            module_parent(Path::new("a/b.rs"), &files),
3916            Some(idx("a.rs"))
3917        );
3918        assert_eq!(
3919            module_parent(Path::new("c/mod.rs"), &files),
3920            Some(idx("lib.rs"))
3921        );
3922        assert_eq!(
3923            module_parent(Path::new("c/d.rs"), &files),
3924            Some(idx("c/mod.rs"))
3925        );
3926    }
3927
3928    // --- fs_below_driver: #1016 review findings ------------------------------
3929
3930    /// Finding 1: a `super::`-qualified path needs no glob import — module privacy is
3931    /// ancestor-scoped, so `super::fs` names the parent's private `use std::fs;` from
3932    /// any child. One disambiguating edit away from `discovery.rs:39`'s bare call,
3933    /// and it must not drop the file out of the count.
3934    #[test]
3935    fn a_super_qualified_path_resolves_without_a_glob_import() {
3936        let files = [
3937            ("lib.rs", "mod project;\n"),
3938            ("project.rs", "use std::fs;\n\nmod discovery;\n"),
3939            (
3940                "project/discovery.rs",
3941                "fn read_source(path: &std::path::Path) -> String {\n    super::fs::read_to_string(path).unwrap()\n}\n",
3942            ),
3943        ];
3944        assert_eq!(flagged(&files), vec!["project.rs", "project/discovery.rs"]);
3945    }
3946
3947    /// Finding 1, the `crate::`-rooted form: the walk descends the module tree from
3948    /// the crate root file by file, then resolves the leaf against that module's
3949    /// bindings — from anywhere in the crate, glob import or not.
3950    #[test]
3951    fn a_crate_qualified_path_resolves_through_the_module_tree() {
3952        let files = [
3953            ("lib.rs", "mod other;\nmod project;\n"),
3954            (
3955                "other.rs",
3956                "fn f() {\n    let _ = crate::project::fs::read_dir(\".\");\n}\n",
3957            ),
3958            ("project.rs", "use std::fs;\n"),
3959        ];
3960        assert_eq!(flagged(&files), vec!["other.rs", "project.rs"]);
3961    }
3962
3963    /// Finding 1, stacked hops: `super::super::` climbs two parents (through a
3964    /// glob-free middle module — qualified paths don't need the glob chain).
3965    #[test]
3966    fn stacked_super_hops_climb_the_parent_chain() {
3967        let files = [
3968            ("lib.rs", "use std::fs;\n\nmod a;\n"),
3969            ("a/mod.rs", "mod b;\n"),
3970            (
3971                "a/b.rs",
3972                "fn f() {\n    let _ = super::super::fs::read_dir(\".\");\n}\n",
3973            ),
3974        ];
3975        assert_eq!(flagged(&files), vec!["lib.rs", "a/b.rs"]);
3976    }
3977
3978    /// Finding 1, `self::` composed with the glob chain: `self::fs` resolves in the
3979    /// file's own namespace, which includes what its `use super::*;` pulled in.
3980    #[test]
3981    fn a_self_qualified_path_resolves_through_the_files_own_glob_chain() {
3982        let files = [
3983            ("lib.rs", "mod p;\n"),
3984            ("p.rs", "use std::fs;\n\nmod c;\n"),
3985            (
3986                "p/c.rs",
3987                "use super::*;\n\nfn f() {\n    let _ = self::fs::read_dir(\".\");\n}\n",
3988            ),
3989        ];
3990        assert_eq!(flagged(&files), vec!["p.rs", "p/c.rs"]);
3991    }
3992
3993    /// Finding 1's negatives: a qualified path to a name the parent binds to
3994    /// something other than `std::fs` stops at that binding, and a path through a
3995    /// module that doesn't exist resolves nowhere.
3996    #[test]
3997    fn a_qualified_path_to_a_non_std_binding_or_missing_module_is_not_flagged() {
3998        let files = [
3999            ("lib.rs", "mod overlay;\nmod p;\n"),
4000            ("overlay.rs", "pub fn read_dir(_p: &str) {}\n"),
4001            ("p.rs", "use crate::overlay as fs;\n\nmod d;\n"),
4002            (
4003                "p/d.rs",
4004                "fn f() {\n    let _ = super::fs::read_dir(\".\");\n    let _ = crate::missing::fs::read_dir(\".\");\n}\n",
4005            ),
4006        ];
4007        assert_eq!(flagged(&files), Vec::<String>::new());
4008    }
4009
4010    /// Finding 2: a locally-declared type-namespace item beats a glob-imported name
4011    /// in real Rust — a child with its own `mod fs;` calling `fs::…` is calling its
4012    /// own submodule, not the ancestor's `std::fs`.
4013    #[test]
4014    fn a_locally_declared_module_shadows_the_ancestors_std_fs() {
4015        let files = [
4016            ("lib.rs", "mod p;\n"),
4017            ("p.rs", "use std::fs;\n\nmod c;\n"),
4018            (
4019                "p/c.rs",
4020                "use super::*;\n\nmod fs;\n\nfn f() {\n    let _ = fs::read_dir(\".\");\n}\n",
4021            ),
4022            ("p/c/fs.rs", "pub fn read_dir(_p: &str) {}\n"),
4023        ];
4024        assert_eq!(flagged(&files), vec!["p.rs"]);
4025    }
4026
4027    /// Finding 3: a trailing `//` comment on a `use` line must not sever the edge —
4028    /// neither the glob (`use super::*; // …`) nor the binding (`use std::fs; // …`).
4029    #[test]
4030    fn a_trailing_comment_on_a_use_line_does_not_sever_resolution() {
4031        let files = [
4032            ("lib.rs", "mod p;\n"),
4033            (
4034                "p.rs",
4035                "use std::fs; // read_source's disk fallback\n\nmod c;\n",
4036            ),
4037            (
4038                "p/c.rs",
4039                "use super::*; // parent's fs, PathBuf\n\nfn f() {\n    let _ = fs::read_dir(\".\");\n}\n",
4040            ),
4041        ];
4042        assert_eq!(flagged(&files), vec!["p.rs", "p/c.rs"]);
4043    }
4044
4045    /// Finding 4: the nested-group + `::self` normalisation branches, pinned
4046    /// directly — `use std::{fs::{self, File}, io};` binds `fs` *and* `File` to
4047    /// `std::fs`, and `io` only to the shadow set. Getting `::self` wrong would
4048    /// silently under-count, which is exactly this probe's failure mode.
4049    #[test]
4050    fn a_nested_group_with_self_binds_the_module_and_its_items() {
4051        let facts = fs_import_facts("use std::{fs::{self, File}, io};\n");
4052        let bound: Vec<&str> = facts.std_fs_bindings.iter().map(String::as_str).collect();
4053        assert_eq!(bound, vec!["File", "fs"]);
4054        assert!(facts.use_bound_names.contains("io"));
4055        assert!(!facts.std_fs_bindings.contains("io"));
4056    }
4057
4058    /// Finding 4, the children half of [`FsImportFacts`]' contract: a parent whose
4059    /// *only* `use std::fs;` lives in its `#[cfg(test)] mod` hands no binding to a
4060    /// glob-importing child — `bynk-ide/src/symbols.rs`' shape, latent until it
4061    /// grows a submodule.
4062    #[test]
4063    fn a_parents_test_mod_use_std_fs_does_not_reach_its_children() {
4064        let files = [
4065            ("lib.rs", "mod p;\n"),
4066            (
4067                "p.rs",
4068                "mod c;\n\nfn production() {}\n\n#[cfg(test)]\nmod tests {\n    use std::fs;\n\n    #[test]\n    fn t() {\n        let _ = fs::read_dir(\".\");\n    }\n}\n",
4069            ),
4070            (
4071                "p/c.rs",
4072                "use super::*;\n\nfn f() {\n    let _ = fs::read_dir(\".\");\n}\n",
4073            ),
4074        ];
4075        assert_eq!(flagged(&files), Vec::<String>::new());
4076    }
4077
4078    // --- fs_below_driver: named-floor classification (#1104) -----------------
4079
4080    #[test]
4081    fn fn_name_on_line_strips_modifiers() {
4082        assert_eq!(fn_name_on_line("fn foo() {"), Some("foo".to_string()));
4083        assert_eq!(
4084            fn_name_on_line("pub(crate) fn read_adapter_binding("),
4085            Some("read_adapter_binding".to_string())
4086        );
4087        assert_eq!(
4088            fn_name_on_line("pub async unsafe fn go() {"),
4089            Some("go".to_string())
4090        );
4091    }
4092
4093    #[test]
4094    fn fn_name_on_line_ignores_non_fn_lines() {
4095        assert_eq!(fn_name_on_line("    let f = foo();"), None);
4096        assert_eq!(fn_name_on_line("/// calls fn bar somewhere"), None);
4097    }
4098
4099    /// A signature whose `{` arrives lines after the `fn` line — `read_adapter_binding`'s
4100    /// own real shape — must still resolve to the correct body range: `started` can't
4101    /// flip true on the parameter list, which has no braces of its own.
4102    #[test]
4103    fn production_fn_ranges_handles_a_wrapped_signature() {
4104        let src = "pub(crate) fn read_adapter_binding(\n    path: &Path,\n) -> std::io::Result<String> {\n    fs::read_to_string(path)\n}\n";
4105        let lines: Vec<&str> = src.lines().collect();
4106        let ranges = production_fn_ranges(&lines, &[]);
4107        assert_eq!(ranges.len(), 1);
4108        let (name, start, end) = &ranges[0];
4109        assert_eq!(name, "read_adapter_binding");
4110        assert_eq!(*start, 0);
4111        assert_eq!(*end, lines.len() - 1);
4112        assert_eq!(
4113            enclosing_fn(3, &ranges),
4114            Some("read_adapter_binding".to_string())
4115        );
4116    }
4117
4118    /// #1561: [`NAMED_FS_EXCEPTIONS`] itself is empty (P4.0 moved every file it used to
4119    /// name out of `bynk-emit` entirely — see the const's own doc comment), so the tests
4120    /// below that exercise a *match* use these synthetic tuples instead of real,
4121    /// currently-empty production data — same crate as the real #1104 shape, but a
4122    /// file/fn pair invented for this test and never a real production exception.
4123    const SYNTHETIC_EXCEPTIONS: &[(&str, &str, &str)] = &[
4124        (
4125            "bynk-emit",
4126            "project/synthetic_example.rs",
4127            "synthetic_named_fn_a",
4128        ),
4129        (
4130            "bynk-emit",
4131            "project/synthetic_example.rs",
4132            "synthetic_named_fn_b",
4133        ),
4134    ];
4135
4136    /// Build the `facts`/`parents` vectors [`file_is_named_fs_floor`] now takes as
4137    /// caller-supplied arguments, the same way [`fs_below_driver`] does, so each test
4138    /// below reads as "classify this file" rather than repeating the setup.
4139    fn classify(
4140        krate: &str,
4141        files: &[(PathBuf, String)],
4142        i: usize,
4143        exceptions: &[(&str, &str, &str)],
4144    ) -> bool {
4145        let facts: Vec<FsImportFacts> = files.iter().map(|(_, s)| fs_import_facts(s)).collect();
4146        let parents: Vec<Option<usize>> =
4147            files.iter().map(|(p, _)| module_parent(p, files)).collect();
4148        file_is_named_fs_floor(krate, files, &facts, &parents, i, exceptions)
4149    }
4150
4151    /// The concrete #1104 shape, in miniature (#1561: against [`SYNTHETIC_EXCEPTIONS`],
4152    /// `NAMED_FS_EXCEPTIONS` itself being empty): `project.rs`'s bare `use std::fs;` (no
4153    /// enclosing fn — never itself a violation) plus `synthetic_example.rs`'s two
4154    /// named-exception functions. The whole file must read as a named floor, not
4155    /// residual.
4156    #[test]
4157    fn file_is_named_fs_floor_true_for_a_file_of_only_named_exceptions() {
4158        let files = [
4159            (
4160                PathBuf::from("project.rs"),
4161                "use std::fs;\n\nmod synthetic_example;\n".to_string(),
4162            ),
4163            (
4164                PathBuf::from("project/synthetic_example.rs"),
4165                "use super::*;\n\npub(crate) fn synthetic_named_fn_a() {\n    let _ = fs::read_dir(\".\");\n}\n\npub(crate) fn synthetic_named_fn_b(path: &Path) -> std::io::Result<String> {\n    fs::read_to_string(path)\n}\n".to_string(),
4166            ),
4167        ];
4168        assert!(classify("bynk-emit", &files, 1, SYNTHETIC_EXCEPTIONS));
4169    }
4170
4171    /// A new, unlisted fn touching `std::fs` in the *same file* as two named exceptions
4172    /// must flip the whole file to residual — no partial credit, since "named floor"
4173    /// must mean every touch is accounted for, not most of them.
4174    #[test]
4175    fn file_is_named_fs_floor_false_when_an_unnamed_fn_also_touches_fs() {
4176        let files = [
4177            (
4178                PathBuf::from("project.rs"),
4179                "use std::fs;\n\nmod synthetic_example;\n".to_string(),
4180            ),
4181            (
4182                PathBuf::from("project/synthetic_example.rs"),
4183                "use super::*;\n\npub(crate) fn synthetic_named_fn_a() {\n    let _ = fs::read_dir(\".\");\n}\n\nfn some_new_helper() {\n    let _ = fs::write(\"x\", \"y\");\n}\n".to_string(),
4184            ),
4185        ];
4186        assert!(!classify("bynk-emit", &files, 1, SYNTHETIC_EXCEPTIONS));
4187    }
4188
4189    /// A file whose only production-scope touch is a bare `use std::fs;` import — no
4190    /// enclosing fn at all — is trivially a named floor: the import performs no I/O by
4191    /// itself, and the descendant it enables is checked (and named) separately. No
4192    /// named exceptions needed at all here — nothing to match against.
4193    #[test]
4194    fn file_is_named_fs_floor_true_for_an_import_only_file() {
4195        let files = [(
4196            PathBuf::from("project.rs"),
4197            "use std::fs;\n\nmod discovery;\n".to_string(),
4198        )];
4199        assert!(classify("bynk-emit", &files, 0, &[]));
4200    }
4201
4202    /// The same synthetic-example shape under the wrong crate label must not read as a
4203    /// floor — [`NAMED_FS_EXCEPTIONS`] is keyed on `(crate, file, fn)`, not `(file, fn)`
4204    /// alone, so a same-named file/fn pair in a different crate isn't accidentally
4205    /// covered.
4206    #[test]
4207    fn file_is_named_fs_floor_false_under_the_wrong_crate() {
4208        let files = [
4209            (
4210                PathBuf::from("project.rs"),
4211                "use std::fs;\n\nmod synthetic_example;\n".to_string(),
4212            ),
4213            (
4214                PathBuf::from("project/synthetic_example.rs"),
4215                "use super::*;\n\npub(crate) fn synthetic_named_fn_a() {\n    let _ = fs::read_dir(\".\");\n}\n".to_string(),
4216            ),
4217        ];
4218        assert!(!classify("bynk-ide", &files, 1, SYNTHETIC_EXCEPTIONS));
4219    }
4220
4221    /// Review finding (#1106): a module-scope `std::fs` touch that isn't an import
4222    /// declaration — a `static` initialiser doing real I/O — has no enclosing fn either,
4223    /// but is a genuine R2.3 violation and must not be waved through as a floor just
4224    /// because it sits outside every known fn range.
4225    #[test]
4226    fn file_is_named_fs_floor_false_for_a_module_scope_static_that_reads() {
4227        let files = [(
4228            PathBuf::from("project.rs"),
4229            "use std::fs;\n\nstatic ROOT: once_cell::sync::Lazy<String> = once_cell::sync::Lazy::new(|| fs::read_to_string(\"x\").unwrap());\n"
4230                .to_string(),
4231        )];
4232        assert!(!classify("bynk-emit", &files, 0, &[]));
4233    }
4234
4235    /// Same review finding, the [`fn_name_on_line`] half: an `extern "C" fn` (a modifier
4236    /// combination the parser doesn't strip) produces no [`production_fn_ranges`] entry
4237    /// at all, so its whole body would fall into the "no enclosing fn" branch. It must
4238    /// still read as residual, not floor, once it touches `std::fs`.
4239    #[test]
4240    fn file_is_named_fs_floor_false_for_an_unparsed_extern_fn_body() {
4241        let files = [(
4242            PathBuf::from("project.rs"),
4243            "use std::fs;\n\nextern \"C\" fn callback() {\n    let _ = fs::read_dir(\".\");\n}\n"
4244                .to_string(),
4245        )];
4246        assert!(!classify("bynk-emit", &files, 0, &[]));
4247    }
4248
4249    /// #1587: [`NAMED_FS_EXCEPTIONS`] is empty today (#1561 cleared the three entries
4250    /// that outlived their files' P4.0 move out of `bynk-emit` for weeks, unnoticed,
4251    /// while `fs_below_driver`'s `0 named floor` reading stayed vacuously "healthy"),
4252    /// so this loop is vacuous now — but guards whatever named exception is decided
4253    /// next the same way [`ts_writes_excluded_files_still_exist`] and
4254    /// [`ast_importer_exceptions_still_exist_and_still_import_the_ast`] guard their own
4255    /// lists: a future entry going stale must fail loud here, not silently surface as
4256    /// a falsely-healthy `fs_below_driver` count. The tuple's third field is checked
4257    /// too (review of #1591), not just discarded — [`file_is_named_fs_floor`] matches
4258    /// on the full `(crate, file, fn)` key, so a refactor that keeps the file but
4259    /// renames or deletes the named function would otherwise leave a silently dead
4260    /// entry, the same recurrence shape #1561's three entries took.
4261    #[test]
4262    fn named_fs_exceptions_still_exist() {
4263        let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("..");
4264        for (krate, rel, fn_name) in NAMED_FS_EXCEPTIONS {
4265            let path = root.join(krate).join("src").join(rel);
4266            let contents = std::fs::read_to_string(&path).unwrap_or_else(|e| {
4267                panic!("NAMED_FS_EXCEPTIONS entry {krate:?}/{rel:?} does not exist: {e}")
4268            });
4269            assert!(
4270                contents.contains(fn_name),
4271                "NAMED_FS_EXCEPTIONS entry {krate:?}/{rel:?}/{fn_name:?} no longer names \
4272                 a function in that file — it excludes nothing and should be removed"
4273            );
4274        }
4275    }
4276
4277    // --- emit_abi_shapes (#999 Decision E) ----------------------------------
4278
4279    /// A binding's ordinary capability-interface imports, and the emit-ABI tag-layout
4280    /// names, must not be flagged — the exact failure mode Decision E rebuilt the probe
4281    /// to avoid (the original single-allowlist definition read 29-33 here, not 1).
4282    ///
4283    /// Exercises the real production allowlists via [`is_enumerated_emit_abi_or_capability_surface`]
4284    /// — not a local re-declaration. A test with its own copy of `EMIT_ABI` would still
4285    /// pass if the real one lost an entry (e.g. deleting `Uuid` from the production
4286    /// list), proving nothing about the probe it claims to cover.
4287    #[test]
4288    fn emit_abi_shapes_does_not_flag_capability_or_tag_layout_imports() {
4289        let src = "import type { Clock, Fetch, Locale } from \"./bynk.js\";\n\
4290                    import { FetchError, Uuid } from \"./bynk.js\";\n\
4291                    import { Err, None, Ok, Some, type Option, type Result } from \"./runtime.js\";\n";
4292        let imports = ts_named_imports_from_runtime_modules(src);
4293        let leaks: Vec<&String> = imports
4294            .iter()
4295            .filter(|i| !is_enumerated_emit_abi_or_capability_surface(i))
4296            .collect();
4297        assert!(leaks.is_empty(), "unexpected leaks: {leaks:?}");
4298    }
4299
4300    /// The falsifier from #999 Decision E, checked directly: deleting an entry from the
4301    /// real production allowlist must be detectable by *some* test — this one flags
4302    /// `Uuid` as a leak the moment it's removed from [`EMIT_ABI`], which the test above
4303    /// (using the real const) would also start failing on.
4304    #[test]
4305    fn is_enumerated_checks_the_real_production_allowlist() {
4306        assert!(is_enumerated_emit_abi_or_capability_surface("Uuid"));
4307        assert!(is_enumerated_emit_abi_or_capability_surface("LocaleTag"));
4308        assert!(!is_enumerated_emit_abi_or_capability_surface(
4309            "negotiateLocale"
4310        ));
4311    }
4312
4313    /// The real, current-tree finding this probe exists to surface: `negotiateLocale`,
4314    /// a plain value helper from `./runtime.js` alongside the tag-layout constructors,
4315    /// is neither an enumerated emit-ABI shape nor a capability-interface import.
4316    #[test]
4317    fn emit_abi_shapes_flags_a_non_enumerated_runtime_helper() {
4318        let src = "import { Err, None, Ok, Some, negotiateLocale, type Option, type Result } from \"./runtime.js\";\n";
4319        let imports = ts_named_imports_from_runtime_modules(src);
4320        assert!(imports.contains(&"negotiateLocale".to_string()));
4321    }
4322
4323    /// `FetchError` is `import type` in one binding and a plain value import in
4324    /// another (`FetchError.Timeout`) — Decision E's rejected type-vs-value
4325    /// discriminator. Confirms the extractor treats both forms as the same identifier,
4326    /// so the allowlist check doesn't depend on which form a given file happens to use.
4327    #[test]
4328    fn ts_import_extraction_ignores_type_only_vs_value_distinction() {
4329        let type_only = "import type { FetchError } from \"./bynk.js\";\n";
4330        let value = "import { FetchError, Uuid } from \"./bynk.js\";\n";
4331        assert_eq!(
4332            ts_named_imports_from_runtime_modules(type_only),
4333            vec!["FetchError".to_string()]
4334        );
4335        assert!(ts_named_imports_from_runtime_modules(value).contains(&"FetchError".to_string()));
4336    }
4337
4338    // --- options_sources -----------------------------------------------------
4339
4340    #[test]
4341    fn struct_body_finds_a_field_by_name() {
4342        let src = "struct Foo {\n    pub sources: Option<HashMap<PathBuf, String>>,\n    pub other: bool,\n}\n";
4343        let body = struct_body(src, "Foo").expect("struct body found");
4344        assert!(body.contains("sources"));
4345    }
4346
4347    #[test]
4348    fn struct_body_does_not_match_an_unrelated_struct() {
4349        let src =
4350            "struct Bar {\n    pub sources: bool,\n}\n\nstruct Foo {\n    pub other: bool,\n}\n";
4351        let body = struct_body(src, "Foo").expect("struct body found");
4352        assert!(!body.contains("sources"));
4353    }
4354
4355    // --- render_table's "Rules closed" section (#1001) ------------------------
4356
4357    fn empty_report() -> Report {
4358        Report { probes: Vec::new() }
4359    }
4360
4361    /// The section is static text — no count, no existence check — precisely
4362    /// because nothing regenerates `design/greenfield-status.md` when `stamp`
4363    /// writes the ledger, so a computed count would silently go stale the
4364    /// moment the first `closes_rule` landed (the drift a first draft of this
4365    /// section introduced, caught in #1001's review). This test pins "static"
4366    /// as the actual behaviour, not just the intent in a comment.
4367    #[test]
4368    fn render_table_rules_closed_section_is_static_regardless_of_the_tree() {
4369        let out = render_table(&empty_report());
4370        assert!(out.contains("greenfield-status-rules.md"), "{out}");
4371        assert!(
4372            out.contains("may not exist yet"),
4373            "the wording must not claim to know whether the ledger exists: {out}"
4374        );
4375    }
4376
4377    // --- ts_writes / ts_any (P7.0, #1296; testability + widening, review of #1297) --
4378
4379    /// Run [`ts_writes_violations`] over an in-memory file list — mirrors
4380    /// [`flagged`]'s own role for `production_std_fs_files`.
4381    fn ts_writes_over(files: &[(&str, &str)]) -> usize {
4382        let owned: Vec<(PathBuf, String)> = files
4383            .iter()
4384            .map(|(p, s)| (PathBuf::from(p), (*s).to_string()))
4385            .collect();
4386        ts_writes_violations(&owned)
4387    }
4388
4389    /// Run [`ts_any_violations`] over an in-memory file list.
4390    fn ts_any_over(files: &[(&str, &str)]) -> usize {
4391        let owned: Vec<(PathBuf, String)> = files
4392            .iter()
4393            .map(|(p, s)| (PathBuf::from(p), (*s).to_string()))
4394            .collect();
4395        ts_any_violations(&owned)
4396    }
4397
4398    /// Run [`verbatim_origins_violations`] over an in-memory file list.
4399    fn verbatim_origins_over(files: &[(&str, &str)]) -> usize {
4400        let owned: Vec<(PathBuf, String)> = files
4401            .iter()
4402            .map(|(p, s)| (PathBuf::from(p), (*s).to_string()))
4403            .collect();
4404        verbatim_origins_violations(&owned)
4405    }
4406
4407    /// Run [`verbatim_sites_violations`] over an in-memory file list.
4408    fn verbatim_sites_over(files: &[(&str, &str)]) -> usize {
4409        let owned: Vec<(PathBuf, String)> = files
4410            .iter()
4411            .map(|(p, s)| (PathBuf::from(p), (*s).to_string()))
4412            .collect();
4413        verbatim_sites_violations(&owned)
4414    }
4415
4416    #[test]
4417    fn ts_writes_excluded_files_are_recognised() {
4418        assert!(is_ts_writes_excluded_file(Path::new("emitter/wrangler.rs")));
4419        assert!(is_ts_writes_excluded_file(Path::new("emitter/secrets.rs")));
4420        assert!(is_ts_writes_excluded_file(Path::new(
4421            "emitter/contracts.rs"
4422        )));
4423        assert!(is_ts_writes_excluded_file(Path::new("testkit.rs")));
4424        // Name proximity to a file that used to be excluded must not false-positive:
4425        // `emitter/lower.rs` (the emitter's own lowering pass) is genuinely
4426        // TS-producing and must stay counted — unlike `ir/lower.rs` (the checker→IR
4427        // pass), which isn't a name-proximity risk at all any more: it left
4428        // `bynk-emit/src` entirely at Arc D's P7.12 crate carve. `emitter/source_map.rs`
4429        // is the same shape (#1561): it left for `bynk-ts/src/source_map.rs` at P7.5
4430        // (#1308), so it's no longer excluded either — there's nothing left to exclude.
4431        assert!(!is_ts_writes_excluded_file(Path::new("emitter/lower.rs")));
4432        assert!(!is_ts_writes_excluded_file(Path::new("ir/lower.rs")));
4433        assert!(!is_ts_writes_excluded_file(Path::new(
4434            "emitter/source_map.rs"
4435        )));
4436        assert!(!is_ts_writes_excluded_file(Path::new("emitter.rs")));
4437        assert!(!is_ts_writes_excluded_file(Path::new("project.rs")));
4438        assert!(!is_ts_writes_excluded_file(Path::new(
4439            "project/tests_emit.rs"
4440        )));
4441    }
4442
4443    /// #1587: the exact failure #1561 fixed for [`NAMED_FS_EXCEPTIONS`] (an entry
4444    /// outliving its file by weeks while the gated probe kept reading a vacuous,
4445    /// falsely-healthy number) applies just as well to [`TS_WRITES_EXCLUDED_FILES`] —
4446    /// nothing previously caught a stale entry here either. Must fail loud, not surface
4447    /// as a silent `ts_writes`/`ts_any` regression in `greenfield_status_table_is_current`
4448    /// — mirrors [`ast_importer_exceptions_still_exist_and_still_import_the_ast`]'s own
4449    /// discipline for the sibling list: existence alone isn't enough (review of #1591)
4450    /// — a file that survives but stops containing any `write!`/`writeln!`/`format!`
4451    /// site would excludes nothing, and removing it would change no probe count, so
4452    /// [`ts_writes_violations`] must actually see a nonzero count over each entry's real
4453    /// content. Scored under a dummy, non-excluded path so the exclusion filter itself
4454    /// doesn't short-circuit the check.
4455    #[test]
4456    fn ts_writes_excluded_files_still_exist() {
4457        let dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
4458            .join("..")
4459            .join("bynk-emit/src");
4460        for rel in TS_WRITES_EXCLUDED_FILES {
4461            let contents = std::fs::read_to_string(dir.join(rel)).unwrap_or_else(|e| {
4462                panic!("TS_WRITES_EXCLUDED_FILES entry {rel:?} does not exist: {e}")
4463            });
4464            let dummy = [(PathBuf::from("__not_excluded__.rs"), contents)];
4465            assert!(
4466                ts_writes_violations(&dummy) > 0,
4467                "TS_WRITES_EXCLUDED_FILES entry {rel:?} no longer contains a \
4468                 write!/writeln!/format! call outside a test module — it excludes \
4469                 nothing and should be removed"
4470            );
4471        }
4472    }
4473
4474    /// Regression for a real mistake this slice's own grounding found: an earlier
4475    /// survey during phase 7's own track-opening research treated
4476    /// `project/tests_emit.rs` as excludable "test-assertion" noise. It is real
4477    /// production code (`process_tests`/`process_integration_tests`) per
4478    /// `semantics-in-the-checker.md`'s own settling finding for a different probe on
4479    /// the same file. Exercises the real probe, not just the predicate (review of
4480    /// #1297 — a first cut of this test called `is_ts_writes_excluded_file` directly,
4481    /// which can't catch a bug in [`ts_writes_violations`]'s own use of it).
4482    #[test]
4483    fn ts_writes_does_not_exclude_tests_emit_rs_wholesale() {
4484        let count = ts_writes_over(&[(
4485            "project/tests_emit.rs",
4486            "fn process_tests() {\n    let _ = format!(\"const x = 1;\");\n    let _ = writeln!(out, \"const y = 2;\");\n}\n",
4487        )]);
4488        assert_eq!(
4489            count, 2,
4490            "tests_emit.rs's own production emission code must be counted, not excluded wholesale"
4491        );
4492    }
4493
4494    #[test]
4495    fn is_path_construction_line_catches_the_idiom_not_ordinary_format_calls() {
4496        assert!(is_path_construction_line(
4497            "let p = PathBuf::from(format!(\"workers/{dashes}/index.ts\"));"
4498        ));
4499        assert!(is_path_construction_line(
4500            "root.join(format!(\"tests/integration_{sanitized}.test.ts\"))"
4501        ));
4502        assert!(is_path_construction_line(
4503            "output_path.with_file_name(format!(\"{name}.{suffix}\"))"
4504        ));
4505        // An ordinary TS-producing `format!` call, no path construction, must not be
4506        // excluded by this idiom.
4507        assert!(!is_path_construction_line(
4508            "writeln!(out, \"{}\", format!(\"const {name} = 1;\"))"
4509        ));
4510    }
4511
4512    #[test]
4513    fn line_violates_ts_any_catches_the_cast_and_the_bare_annotation() {
4514        assert!(line_violates_ts_any("let x = (value as any).field;"));
4515        assert!(line_violates_ts_any("format!(\"{}: any\", name)"));
4516        assert!(line_violates_ts_any(
4517            "\"(seq: any[]) => ({ns} as any).drive(seq)\""
4518        ));
4519        assert!(!line_violates_ts_any("let x: unknown = value;"));
4520    }
4521
4522    /// Regression for review of #1297, finding 1: `any` in generic type-argument
4523    /// position (`Record<string, any[]>`, the live `emitter/lower.rs`
4524    /// `joinOn`/`leftJoin`/`groupBy` shape) contains neither `as any` nor `: any` and
4525    /// was silently uncounted by the round-one predicate.
4526    #[test]
4527    fn line_violates_ts_any_catches_generic_position_any() {
4528        assert!(line_violates_ts_any(
4529            "\"{{ const __h: Record<string, any[]> = {{}}; ...}}\""
4530        ));
4531        assert!(line_violates_ts_any("\"Array<any>\""));
4532        assert!(line_violates_ts_any("\"Promise<any>\""));
4533        // Must not regress the round-one patterns while widening.
4534        assert!(line_violates_ts_any("(value as any).field"));
4535        assert!(line_violates_ts_any("(e: any) => {}"));
4536    }
4537
4538    /// Regression for review of #1322, finding 2: once a site builds a real
4539    /// `bynk_ts::TsType` node instead of writing TypeScript text directly, the
4540    /// emitted `any` no longer appears as Rust-source `as any`/`: any` — the round-
4541    /// one/round-two patterns above all match *emitted-text* spellings, none of
4542    /// which appear in `TsType::named("any")`. `workers.rs`'s own three real sites
4543    /// (#1321) were silently uncounted until this pattern was added.
4544    #[test]
4545    fn line_violates_ts_any_catches_the_named_any_construction_spelling() {
4546        assert!(line_violates_ts_any(
4547            "    let mut args = vec![as_expr(ident(\"payload\"), TsType::named(\"any\"))];"
4548        ));
4549        assert!(line_violates_ts_any(
4550            "        Some(TsType::named(\"any\")),"
4551        ));
4552        // Must not regress the round-one/round-two patterns while widening.
4553        assert!(line_violates_ts_any("(value as any).field"));
4554        assert!(line_violates_ts_any("\"Array<any>\""));
4555    }
4556
4557    /// A comment mentioning either pattern in prose — the same self-reference-shaped
4558    /// hazard [`bynk_dotted_literals`]'s own regression tests guard against for a
4559    /// different probe — must not count.
4560    #[test]
4561    fn line_violates_ts_any_ignores_comments() {
4562        assert!(!line_violates_ts_any(
4563            "/// lowering machinery, same as any other subexpression."
4564        ));
4565        assert!(!line_violates_ts_any(
4566            "// TODO: stop emitting `: any` here once bynk-ts exists"
4567        ));
4568    }
4569
4570    /// `#[cfg(test)]`-gated write!-family calls (a file's own unit tests constructing a
4571    /// fixture string) must not count toward either probe — mirrors
4572    /// [`has_production_std_fs`]'s own test-range exclusion for a different probe.
4573    /// Exercises the real probes end to end, not a re-implementation of their loop
4574    /// (review of #1297, finding 2): deleting either probe's `in_test_range` guard, its
4575    /// `is_ts_writes_excluded_file` `continue`, or (for `ts_writes`) its
4576    /// `is_path_construction_line` `continue` now fails one of these tests.
4577    #[test]
4578    fn ts_writes_and_ts_any_exclude_cfg_test_ranges() {
4579        let src = "fn production() {\n    let _ = format!(\"const x = 1;\");\n}\n\n\
4580                    #[cfg(test)]\nmod tests {\n    #[test]\n    fn t() {\n        \
4581                    let _ = format!(\"(v as any)\");\n    }\n}\n";
4582        assert_eq!(
4583            ts_writes_over(&[("emitter.rs", src)]),
4584            1,
4585            "only the production format! call counts"
4586        );
4587        assert_eq!(
4588            ts_any_over(&[("emitter.rs", src)]),
4589            0,
4590            "the test-only `as any` site must be excluded"
4591        );
4592    }
4593
4594    /// Exercises the real probes' file-exclusion `continue`, not just the predicate:
4595    /// a whole file on [`TS_WRITES_EXCLUDED_FILES`] must contribute 0 to either count
4596    /// even when its content would otherwise match both.
4597    #[test]
4598    fn ts_writes_and_ts_any_exclude_named_non_ts_files_end_to_end() {
4599        let files = [(
4600            "emitter/wrangler.rs",
4601            "fn write_toml(out: &mut String) {\n    let _ = writeln!(out, \"name = {v}\");\n    let __x: any = 1;\n}\n",
4602        )];
4603        assert_eq!(ts_writes_over(&files), 0);
4604        assert_eq!(ts_any_over(&files), 0);
4605    }
4606
4607    /// Exercises the real probes' [`is_path_construction_line`] `continue` end to end,
4608    /// not just the predicate in isolation.
4609    #[test]
4610    fn ts_writes_excludes_path_construction_end_to_end() {
4611        let files = [(
4612            "project.rs",
4613            "fn out_path(dashes: &str) -> PathBuf {\n    PathBuf::from(format!(\"workers/{dashes}/index.ts\"))\n}\n\nfn emit(out: &mut String) {\n    let _ = writeln!(out, \"export const x = 1;\");\n}\n",
4614        )];
4615        assert_eq!(
4616            ts_writes_over(&files),
4617            1,
4618            "the path-construction line must not count; the genuine emission line must"
4619        );
4620    }
4621
4622    #[test]
4623    fn verbatim_origins_counts_distinct_variants_not_construction_sites() {
4624        let files = [(
4625            "emitter/contracts.rs",
4626            "fn a() { TsStmt::verbatim(VerbatimOrigin::Contracts, \"x\", None) }\nfn b() { TsStmt::verbatim(VerbatimOrigin::Contracts, \"y\", None) }\nfn c() { TsStmt::verbatim(VerbatimOrigin::Secrets, \"z\", None) }\n",
4627        )];
4628        // Three construction sites, but only two distinct origins.
4629        assert_eq!(verbatim_origins_over(&files), 2);
4630        assert_eq!(verbatim_sites_over(&files), 3);
4631    }
4632
4633    #[test]
4634    fn verbatim_origins_and_sites_ignore_comments() {
4635        let files = [(
4636            "emitter/contracts.rs",
4637            "// TsStmt::verbatim(VerbatimOrigin::Contracts, \"x\", None)\n/// Mentions VerbatimOrigin::Secrets in prose.\n",
4638        )];
4639        assert_eq!(verbatim_origins_over(&files), 0);
4640        assert_eq!(verbatim_sites_over(&files), 0);
4641    }
4642
4643    #[test]
4644    fn verbatim_origins_and_sites_read_zero_over_an_empty_tree() {
4645        let files: [(&str, &str); 0] = [];
4646        assert_eq!(verbatim_origins_over(&files), 0);
4647        assert_eq!(verbatim_sites_over(&files), 0);
4648    }
4649
4650    /// Review of #1308, finding 6: without stripping `#[cfg(test)]` ranges,
4651    /// a single `bynk-emit` unit test fixture constructing a `TsStmt::
4652    /// verbatim(...)` for its own coverage would pin `verbatim_sites` above
4653    /// its documented 0 floor permanently, for a reason unrelated to
4654    /// residual production emission.
4655    #[test]
4656    fn verbatim_origins_and_sites_exclude_cfg_test_ranges() {
4657        let src = "fn production() {\n    TsStmt::verbatim(VerbatimOrigin::Contracts, \"x\", None);\n}\n\n\
4658                    #[cfg(test)]\nmod tests {\n    #[test]\n    fn t() {\n        \
4659                    TsStmt::verbatim(VerbatimOrigin::Secrets, \"y\", None);\n    }\n}\n";
4660        assert_eq!(
4661            verbatim_origins_over(&[("emitter/contracts.rs", src)]),
4662            1,
4663            "only the production-code origin counts"
4664        );
4665        assert_eq!(
4666            verbatim_sites_over(&[("emitter/contracts.rs", src)]),
4667            1,
4668            "the test-only construction site must be excluded"
4669        );
4670    }
4671
4672    /// #1539: `verbatim_sites` widened from a `TsStmt::verbatim(`-only scan
4673    /// to also count `TsExpr::VerbatimExpr(` construction sites — the same
4674    /// escape hatch, now closed at the expression level too. Pins that both
4675    /// needles are counted (and both still respect the comment/`#[cfg(test)]`
4676    /// exclusions the sibling tests above already establish for the
4677    /// `TsStmt` half).
4678    #[test]
4679    fn verbatim_sites_counts_the_expr_level_needle_too() {
4680        let src = "fn f() {\n    let x = TsExpr::VerbatimExpr(\"a\".to_string(), VerbatimOrigin::Emit);\n    let y = TsStmt::verbatim(VerbatimOrigin::Emit, \"b\", None);\n}\n";
4681        assert_eq!(
4682            verbatim_sites_over(&[("emitter/emit.rs", src)]),
4683            2,
4684            "one TsExpr::VerbatimExpr( site plus one TsStmt::verbatim( site"
4685        );
4686        assert_eq!(
4687            verbatim_origins_over(&[("emitter/emit.rs", src)]),
4688            1,
4689            "both reference the same VerbatimOrigin::Emit variant"
4690        );
4691    }
4692
4693    // --- incremental_query_types (P8.0, #1510; re-settled by #1537) ----------
4694
4695    fn owned(files: &[(&str, &str)]) -> Vec<(PathBuf, String)> {
4696        files
4697            .iter()
4698            .map(|(p, s)| (PathBuf::from(p), (*s).to_string()))
4699            .collect()
4700    }
4701
4702    #[test]
4703    fn unit_signature_present_recognises_the_real_struct_and_ignores_a_comment() {
4704        assert!(unit_signature_present(&owned(&[(
4705            "unit_signature.rs",
4706            "pub struct UnitSignature {\n    types: HashMap<String, Arc<TypeDecl>>,\n}\n",
4707        )])));
4708        assert!(!unit_signature_present(&owned(&[(
4709            "lib.rs",
4710            "// TODO: build a struct UnitSignature here eventually\n",
4711        )])));
4712        assert!(!unit_signature_present(&[]));
4713    }
4714
4715    /// The committed reading after #1537: neither deleted level is back.
4716    #[test]
4717    fn deleted_levels_present_is_empty_when_neither_level_exists() {
4718        assert!(deleted_levels_present(&[]).is_empty());
4719    }
4720
4721    /// Re-adding `ProjectGraph` anywhere flips the reading — the whole workspace is
4722    /// scanned (review of #1582) because P8.3 (ADR 0415) already landed it in a
4723    /// different crate from the one first assumed, and R3.13's table names a third
4724    /// (`bynk-resolve`) that does not exist yet.
4725    #[test]
4726    fn deleted_levels_present_sees_project_graph_wherever_it_lands() {
4727        let graph = "pub struct ProjectGraph {\n    units: HashMap<UnitId, Unit>,\n}\n";
4728        assert_eq!(
4729            deleted_levels_present(&owned(&[("project_graph.rs", graph)])),
4730            vec!["ProjectGraph"]
4731        );
4732        assert_eq!(
4733            deleted_levels_present(&owned(&[("resolve/graph.rs", graph)])),
4734            vec!["ProjectGraph"]
4735        );
4736        assert!(
4737            deleted_levels_present(&owned(&[(
4738                "lib.rs",
4739                "// a struct ProjectGraph used to live here\n"
4740            )]))
4741            .is_empty()
4742        );
4743    }
4744
4745    /// **The empirically-confirmed false positive #1510's own first run caught** (see
4746    /// [`defid_query_fn_present`]): `bynk-check/src/checker.rs` has an ordinary,
4747    /// pre-existing `fn type_of(expr: &Expr, ..)` with no `DefId` anywhere in its
4748    /// signature. After #1537 the direction of the mistake reverses — it would now
4749    /// read as the deleted level having been *re-added* — so it is pinned against the
4750    /// real function's own signature text here too.
4751    #[test]
4752    fn deleted_levels_present_does_not_count_checkers_pre_existing_type_of() {
4753        let found = deleted_levels_present(&owned(&[(
4754            "checker.rs",
4755            "pub(crate) fn type_of(expr: &Expr, expected: Option<TyId>, ctx: &mut Ctx) -> Option<TyId> {\n",
4756        )]));
4757        assert!(
4758            found.is_empty(),
4759            "checker.rs's own type_of has no DefId parameter and must not count: {found:?}"
4760        );
4761    }
4762
4763    #[test]
4764    fn deleted_levels_present_sees_a_real_defid_keyed_body_and_type_of() {
4765        let queries = "pub fn body(id: DefId) -> Body {\n    todo!()\n}\n\npub fn type_of(id: DefId) -> TypeOf {\n    todo!()\n}\n";
4766        assert_eq!(
4767            deleted_levels_present(&owned(&[("queries.rs", queries)])),
4768            vec!["Body", "TypeOf"]
4769        );
4770    }
4771
4772    #[test]
4773    fn shared_cache_migrated_is_false_while_project_unit_cache_still_exists() {
4774        let ide_src: Vec<(PathBuf, String)> = vec![(
4775            PathBuf::from("completion.rs"),
4776            "static PROJECT_UNIT_CACHE: LazyLock<Mutex<HashMap<PathBuf, CachedUnit>>> = ..;"
4777                .to_string(),
4778        )];
4779        let project_src: Vec<(PathBuf, String)> = vec![(
4780            PathBuf::from("cache.rs"),
4781            "pub struct SharedUnitCache { .. }".to_string(),
4782        )];
4783        assert!(!shared_cache_migrated(&ide_src, &project_src));
4784    }
4785
4786    #[test]
4787    fn shared_cache_migrated_is_true_once_project_unit_cache_is_gone_and_a_shared_cache_lands() {
4788        let ide_src: Vec<(PathBuf, String)> = vec![(
4789            PathBuf::from("completion.rs"),
4790            "fn cached_project_unit(path: &Path, content: &str) -> Option<Arc<SourceUnit>> { .. }"
4791                .to_string(),
4792        )];
4793        let project_src: Vec<(PathBuf, String)> = vec![(
4794            PathBuf::from("cache.rs"),
4795            "pub struct SharedUnitCache { units: HashMap<FileId, Arc<SourceUnit>> }".to_string(),
4796        )];
4797        assert!(shared_cache_migrated(&ide_src, &project_src));
4798    }
4799
4800    /// **The real hole finding 1 caught**: absence of `PROJECT_UNIT_CACHE` from
4801    /// `bynk-ide` alone used to read "migrated" even when nothing shared replaced it —
4802    /// a rename or deletion with no cache anywhere in `bynk-project` satisfied the old
4803    /// clause. Now requires a cache-shaped item to actually land in
4804    /// `bynk-project/src` too.
4805    #[test]
4806    fn shared_cache_migrated_is_false_when_project_unit_cache_is_gone_but_nothing_shared_replaces_it()
4807     {
4808        let ide_src: Vec<(PathBuf, String)> = vec![(
4809            PathBuf::from("completion.rs"),
4810            "fn cached_project_unit(path: &Path, content: &str) -> Option<Arc<SourceUnit>> { .. }"
4811                .to_string(),
4812        )];
4813        assert!(!shared_cache_migrated(&ide_src, &[]));
4814    }
4815
4816    /// The needle is anchored on `static PROJECT_UNIT_CACHE`, not a bare substring —
4817    /// `PROJECT_UNIT_CACHE_CAP` (a real, unrelated `const` in
4818    /// `bynk-ide/src/completion.rs`) must not hold this false on its own.
4819    #[test]
4820    fn shared_cache_migrated_is_not_confused_by_project_unit_cache_cap() {
4821        let ide_src: Vec<(PathBuf, String)> = vec![(
4822            PathBuf::from("completion.rs"),
4823            "const PROJECT_UNIT_CACHE_CAP: usize = 4096;".to_string(),
4824        )];
4825        let project_src: Vec<(PathBuf, String)> = vec![(
4826            PathBuf::from("cache.rs"),
4827            "pub struct SharedUnitCache { .. }".to_string(),
4828        )];
4829        assert!(shared_cache_migrated(&ide_src, &project_src));
4830    }
4831
4832    #[test]
4833    fn stability_test_present_recognises_a_matching_test_name() {
4834        let check_tests: Vec<(PathBuf, String)> = vec![(
4835            PathBuf::from("unit_signature.rs"),
4836            "#[test]\nfn unit_signature_is_stable_under_a_body_edit() { .. }\n".to_string(),
4837        )];
4838        assert!(stability_test_present(&check_tests));
4839    }
4840
4841    #[test]
4842    fn stability_test_present_recognises_test_attribute_separated_by_other_attributes() {
4843        let check_tests: Vec<(PathBuf, String)> = vec![(
4844            PathBuf::from("unit_signature.rs"),
4845            "#[test]\n#[should_panic]\nfn unit_signature_panics_when_stability_is_violated() { .. }\n"
4846                .to_string(),
4847        )];
4848        assert!(stability_test_present(&check_tests));
4849    }
4850
4851    #[test]
4852    fn stability_test_present_is_false_for_an_unrelated_test() {
4853        let check_tests: Vec<(PathBuf, String)> = vec![(
4854            PathBuf::from("differential_analysis.rs"),
4855            "#[test]\nfn new_entry_point_matches_analyse_project_with() { .. }\n".to_string(),
4856        )];
4857        assert!(!stability_test_present(&check_tests));
4858    }
4859
4860    #[test]
4861    fn stability_test_present_ignores_a_comment_mentioning_it() {
4862        let check_tests: Vec<(PathBuf, String)> = vec![(
4863            PathBuf::from("lib.rs"),
4864            "// TODO: add a unit_signature stability test (P8.2)\n".to_string(),
4865        )];
4866        assert!(!stability_test_present(&check_tests));
4867    }
4868
4869    /// **The real hole finding 3 caught**: the old match required only
4870    /// `fn `+`unit_signature`+`stab` on one line, with no check for an actual
4871    /// `#[test]` attribute — a plain, non-test helper used to satisfy the clause with
4872    /// no passing test in existence.
4873    #[test]
4874    fn stability_test_present_is_false_for_a_non_test_helper_with_a_matching_name() {
4875        let check_tests: Vec<(PathBuf, String)> = vec![(
4876            PathBuf::from("unit_signature.rs"),
4877            "fn unit_signature_stability_fixture(edit: &Edit) -> UnitSignature { .. }\n"
4878                .to_string(),
4879        )];
4880        assert!(!stability_test_present(&check_tests));
4881    }
4882}