Skip to content

wiring: read past #[cfg(test)] items and treat split-out test files as tests - #64

Merged
blockmandev merged 2 commits into
mainfrom
fix/wiring-scanner
Oct 23, 2026
Merged

blockmandev merged 2 commits into
mainfrom
fix/wiring-scanner

Conversation

@blockmandev

Copy link
Copy Markdown
Member

Fixes two measured blind spots in the dead-capability scanner (src/wiring.rs), found in the independent review of #63.

Root causes and fixes

  1. The scanner stopped reading a file at its first column-zero #[cfg(test)], even when that attribute guarded a single use. src/agent/claude_code.rs:8 (now src/agent/claude_code/turn.rs:4) opens with #[cfg(test)]\nuse std::io::BufRead;, so about 1,600 production lines were never read, in either direction: their callers didn't count and their functions were never declared.
    Fix: scan_source treats a #[cfg(test)] as a guard on the one item that follows it. The item is brace-matched, skipping strings, raw strings, chars and comments. An inline mod tests { … } hides only its own body, and the code after its closing brace is production again. cfg(all(test, …)) counts as test-only. cfg(any(test, …)) and cfg(not(test)) do not, because both ship.
  2. Split-out test files were read as production. A file declared #[cfg(test)] mod x; has no marker of its own, so its calls counted as production callers and its helpers as capabilities.
    Fix: test_module_files resolves each such declaration the way rustc does: x.rs or x/mod.rs next to the declaring file, or the #[path = "…"] target relative to the declaring file's directory. That file and every module file declared beneath it are test code. On this tree it resolves 36 files (all *_tests.rs, parser_pins.rs, permissions/rules/tests/*, tools/documents/fixtures.rs, …).

Scanner tests, each with a control

  • a_test_only_use_does_not_hide_the_rest_of_the_file: a #[cfg(test)] use mid-file. A caller below it must be seen, and a dead fn below it must be declared.
  • an_inline_test_module_hides_only_its_own_body: an inline test mod whose body contains '{', "}", r#"}}"# and // }. Production code after it must be read, and calls inside it must not count.
  • a_split_out_test_file_is_neither_a_caller_nor_a_capability: covers a sibling #[cfg(test)] mod x;, a #[path] one, cfg(all(test, unix)), a module tree below a test file, and a cfg(any(test, …)) module that must stay production.

Controls were run locally, then restored:

  • A: restoring the column-zero cut makes the first two tests fail, on "a caller below a test-only use was not seen" and "production code after an inline test module was not read".
  • B: an empty test-file set makes the third test fail, on "a call in a split-out test file counted as production". The same run also reports the test-file helper as a capability.

Unwired count: 38 → 41

MAX_UNWIRED is set to the honest number, 41. All 38 previous items are still reported. The 3 newly visible items are listed below and were not deleted or wired up in this PR; that is the owner's decision.

Item Defined Only callers (all tests)
Toolbox::set_permission_rules src/toolbox/mod.rs:947 src/toolbox/rule_tests.rs:41 (split-out test file); src/agent/codex.rs:2109 (inside codex's inline mod tests, from line 1282)
ClaudeCodeBackend::with_session src/agent/claude_code/mod.rs:297 src/agent/claude_code/command_tests.rs:334; src/agent/claude_code/tests.rs:116, :126
ClaudeCodeBackend::with_models src/agent/claude_code/mod.rs:310 src/agent/claude_code/command_tests.rs:333; src/agent/claude_code/tests.rs:127, :150, :159 (plus a comment at mod.rs:509, which doesn't count)

Local gate (Windows)

cargo fmt --all -- --check passed, cargo clippy --all-targets -q -- -D warnings passed, and cargo test -q passed (2308 + 59, 0 failed).

…s tests

Two blind spots in the dead-capability scanner, both measured:

1. It stopped reading a file at the first column-zero #[cfg(test)], even
   when that attribute guarded a single `use`. agent/claude_code.rs (now
   claude_code/turn.rs:4) opens with `#[cfg(test)] use std::io::BufRead;`,
   so ~1,600 production lines were never read. A #[cfg(test)] now hides
   exactly the item it guards: the item is brace-matched with strings,
   chars and comments skipped, and an inline `mod tests { ... }` hides only
   its own body. cfg(all(test, ..)) is test-only; cfg(any(test, ..)) and
   cfg(not(test)) are not.

2. Files declared by a parent as `#[cfg(test)] mod x;` carry no marker of
   their own, so their calls counted as production callers and their items
   as capabilities. Each such declaration is now resolved to its file
   (x.rs, x/mod.rs, or #[path]) and that file and its module tree are
   test code.

Seeing again raises the honest unwired count from 38 to 41:
Toolbox::set_permission_rules and ClaudeCodeBackend::{with_session,
with_models} are reached only by tests. MAX_UNWIRED is set to 41; the
items are reported, not removed or wired.
Pure move, so wiring.rs is under the 1,000-line threshold (1240 -> 886):
the inline `#[cfg(test)] mod tests { ... }` becomes
`#[cfg(test)] #[path = "wiring_tests.rs"] mod tests;`, the pattern
mcp_config.rs, mcp_oauth.rs and ansi_c.rs already use. No test is
changed, renamed or removed; the body is only dedented one level.

The scanner classifies the new file as test code: it calls `unwired_in`
and `Unwired::line_report`, both on the unwired list, so reading it as
production would drop the count to 39 and fail the ratchet. It stays 41.
@blockmandev
blockmandev merged commit 3c13422 into main Oct 23, 2026
12 checks passed
@blockmandev
blockmandev deleted the fix/wiring-scanner branch November 15, 2026 20:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant