KeiSeiKit-1.0/_primitives/_rust/kei-conflict-scan/src/tree.rs
Parfii-bot 280bb8132d fix(kei-conflict-scan): close 3 backlog bugs + Phase C draft emission
Closes engine bugs #1, #2, #3 from the user's backlog.md entry dated
2026-05-11 "kei-refactor-engine — 4 false-positive bugs". Bug #4 was
fixed in d2c966d8 (wikilink path-norm + handoff scanner removal).

## Bug #1 — vendored marketplaces skip

Engine was scanning `plugins/marketplaces/claude-plugins-official/` —
vendored upstream code where Constructor Pattern thresholds don't
apply. ~246 cp-violations were from this tree.

Fix: `tree::should_skip_path()` central filter. Skips any path
component named `marketplaces`, `target`, `node_modules`, or `.git`.
Applied via `WalkDir::filter_entry()` in `collect_markdown`,
`collect_with_ext`, `scanners::cp::scan`, `scanners::orphans::scan`,
`scanners::orphans::all_basenames`. `scanners::cp::skip_dir` now
delegates to `should_skip_path` (removed the older inline
`/target/`-substring check).

## Bug #2 — hooks-share-matcher false-positive class

Claude Code hook chains are designed to support N hooks per event by
design. `scanners::hooks` was flagging every pair sharing a matcher
as a "redundancy conflict" — 9 hooks/medium findings in the last
deep-sleep run, every one false-positive.

Fix: `scanners::hooks::scan` reduced to a no-op stub returning
`Vec::new()`. Module docstring documents the retraction + future
direction (a real `hooks-validity` scanner for broken shebangs,
missing chmod, syntax errors would replace it).

## Bug #3 — `.patch` file not unified diff

Already resolved in prior commit (v0.14.1 retraction in patch.rs):
CLI default is `plan-autoresolve.md`, Phase C template references
`-autoresolve.md` suffix, `write_patch` is deprecated shim. Only
legacy `.patch` artefacts in sync-repo/reports/ remain — those are
audit trail, not active.

## Phase C draft file emission (deep-sleep-trigger-prompt.md §6.d)

The earlier Phase C template emitted `proposed_rule` markdown blocks
only — no actionable artefacts. Extended §6 with step 6.d: when
WITH_FORK=1 AND fork branch was created, ALSO write skeleton draft
files into the branch:

  sync-repo/sleep-deep/YYYY-MM-DD/drafts/rules/<slug>.md
  sync-repo/sleep-deep/YYYY-MM-DD/drafts/hooks/<slug>.sh

Drafts follow pattern-codifier-agent Phase 3 templates. Phase C does
NOT register hooks — that's pattern-codifier's job via /sleep-review
morning click-flow (skill Phase 3a added in ~/.claude commit 49a320d).
This closes the loop: Phase C surfaces draft → morning review clicks
approve → pattern-codifier installs → settings.json registered.

Smoke-test required in §6.d: every emitted `.sh` MUST `bash -n` clean
or be excluded from commit + listed in plan markdown.

## Results on ~/.claude/memory/sync-repo (live data)

| Scanner   | Before | After | Delta |
|-----------|-------:|------:|------:|
| orphans   |    108 |     1 |  -107 |
| hooks     |      2 |     0 |    -2 |
| cp        |    174 |     0 |  -174 |
| **TOTAL** |    284 |     1 |  -283 |

On full ~/.claude scan: total drops from ~1614 (per 2026-05-11
backlog) to 983 (cp=186 + orphans=797 — orphan count high because
~/.claude tree has many memory/chatlogs/ refs out-of-tree).

## Tests

12/12 pass on kei-conflict-scan workspace (4 unit + 8 integration).
Pre-existing `oversize_file_flagged` + `orphan_wikilinks_flagged`
still green; new `cross_repo_wikilink_not_flagged` +
`path_prefixed_wikilink_matches_basename` from d2c966d8 still green.

Private mirror at ~/Projects/KeiSeiKit/_primitives/_rust/ synced
(4 files: tree.rs, scanners/cp.rs, scanners/orphans.rs,
scanners/hooks.rs).

Closes backlog "engine-noise-2026-05-11" tag bugs #1, #2, #3.
2026-05-12 18:30:01 +08:00

65 lines
2 KiB
Rust

//! Filesystem walker helpers — shared across scanners.
use std::fs;
use std::path::{Path, PathBuf};
use walkdir::WalkDir;
/// True if a path should be excluded from every scanner.
///
/// Skip rules:
/// - `plugins/marketplaces/...` — vendored upstream plugin code; Constructor
/// Pattern thresholds don't apply, refs are external. (backlog #1, 2026-05-12)
/// - `target/`, `node_modules/`, `.git/` — build/vendor noise that should never
/// contribute to architectural conflict counts.
///
/// Public because the standalone `WalkDir::new(root)` callers in
/// `scanners::cp` and `scanners::orphans` also need to apply it.
pub fn should_skip_path(path: &Path) -> bool {
path.components().any(|c| {
let s = c.as_os_str().to_string_lossy();
s == "target" || s == "node_modules" || s == ".git" || s == "marketplaces"
})
}
pub fn collect_markdown(root: &Path, sub: &str) -> Vec<PathBuf> {
let base = root.join(sub);
if !base.exists() {
return Vec::new();
}
WalkDir::new(&base)
.into_iter()
.filter_entry(|e| !should_skip_path(e.path()))
.filter_map(|e| e.ok())
.filter(|e| e.file_type().is_file())
.filter(|e| e.path().extension().is_some_and(|ext| ext == "md"))
.map(|e| e.into_path())
.collect()
}
pub fn collect_with_ext(root: &Path, sub: &str, ext: &str) -> Vec<PathBuf> {
let base = root.join(sub);
if !base.exists() {
return Vec::new();
}
WalkDir::new(&base)
.into_iter()
.filter_entry(|e| !should_skip_path(e.path()))
.filter_map(|e| e.ok())
.filter(|e| e.file_type().is_file())
.filter(|e| e.path().extension().is_some_and(|e2| e2 == ext))
.map(|e| e.into_path())
.collect()
}
pub fn read_lossy(path: &Path) -> String {
fs::read(path)
.map(|b| String::from_utf8_lossy(&b).into_owned())
.unwrap_or_default()
}
pub fn rel(root: &Path, path: &Path) -> String {
path.strip_prefix(root)
.unwrap_or(path)
.to_string_lossy()
.into_owned()
}