From harness-claude
Orchestrates dead code removal and architecture violation fixes via hotspot mapping and shared convergence loop. Catches cascades after refactoring or for periodic codebase hygiene.
npx claudepluginhub intense-visions/harness-engineering --plugin harness-claudeThis skill uses the workspace's default tool permissions.
> Orchestrate dead code removal and architecture violation fixes with a shared convergence loop. Catches cross-concern cascades that individual skills miss.
Analyzes entropy to find and safely remove unused exports, dead files, pattern violations, orphaned dependencies, and stale constraints after refactoring or for periodic hygiene.
Refactors project-wide code for guideline compliance and testability using 4 parallel agents (compliance, testability barriers, duplication, simplification). Aligns with project docs before unit tests.
Detects dead code, unused exports, orphaned files, circular dependencies, unused packages, stale TODOs, and hygiene issues across 11 categories. Use scan, safe, aggressive modes on full codebase or paths.
Share bugs, ideas, or general feedback.
Orchestrate dead code removal and architecture violation fixes with a shared convergence loop. Catches cross-concern cascades that individual skills miss.
cleanup-dead-code or enforce-architecture individually are not catching cascading issuescleanup-dead-code or enforce-architecture directly| Flag | Effect |
|---|---|
--fix | Enable convergence-based auto-fix (default: detect + report only) |
--dead-code-only | Skip architecture checks |
--architecture-only | Skip dead code checks |
--dry-run | Show what would be fixed without applying |
--ci | Non-interactive: apply safe fixes only, report everything else |
git log --format=format: --name-only --since="6 months ago" | sort | uniq -c | sort -rn | head -50
file -> commit count mapping.Run detect_stale_constraints to find architectural constraints referencing removed or restructured code. Run detect_anomalies to identify structural irregularities that should be addressed during cleanup.
Dead code detection (skip if --architecture-only):
harness cleanup --type dead-code --jsonArchitecture detection (skip if --dead-code-only):
harness check-deps --jsonMerge findings. Convert all raw findings into CleanupFinding objects using classifyFinding(). This normalizes both concerns into a shared schema.
Apply safety classification. Each CleanupFinding already has a safety level from classifyFinding(). Review the classification rules:
Dead code safety:
| Finding | Safety | Condition |
|---|---|---|
| Dead files | Safe | Not entry point, no side effects |
| Unused imports | Safe | Zero references |
| Dead exports (non-public) | Safe | Zero importers, not in package entry point |
| Dead exports (public API) | Unsafe | In package entry point or published package |
| Commented-out code | Safe | Always (code is in git history) |
| Orphaned npm deps | Probably safe | Needs install + test verification |
| Dead internals | Unsafe | Cannot reliably determine all callers |
Architecture safety:
| Violation | Safety | Condition |
|---|---|---|
| Import ordering | Safe | Mechanical reorder |
| Forbidden import (with alternative) | Probably safe | 1:1 replacement configured |
| Forbidden import (no alternative) | Unsafe | Requires restructuring |
| Design token (unambiguous) | Probably safe | Single token match |
| Design token (ambiguous) | Unsafe | Multiple candidates |
| Upward dependency | Unsafe | Always |
| Skip-layer dependency | Unsafe | Always |
| Circular dependency | Unsafe | Always |
Apply hotspot downgrade. For each finding, check if the file is in the top 10% by churn (from Phase 1 HotspotContext). If so, downgrade safe to probably-safe. Do not downgrade unsafe findings.
Cross-concern dedup. Call deduplicateFindings() to merge overlapping findings:
Only runs when --fix flag is set. Without --fix, skip to Phase 5 (REPORT).
findings = classified findings from Phase 3
previousCount = findings.length
iteration = 0
while iteration < 5:
iteration++
# Batch 1: Apply safe fixes silently
safeFixes = findings.filter(f => f.safety === 'safe')
apply(safeFixes)
# Batch 2: Present probably-safe fixes
if --ci mode:
skip probably-safe fixes (report only)
else:
probablySafeFixes = findings.filter(f => f.safety === 'probably-safe')
presentAsDiffs(probablySafeFixes)
apply(approved fixes)
# Verify: lint + typecheck + test
verifyResult = run("pnpm lint && pnpm tsc --noEmit && pnpm test")
if verifyResult.failed:
revertBatch()
reclassify failed fixes as unsafe
continue
# Re-detect both concerns
newFindings = runDetection() # Phase 2 again
newFindings = classify(newFindings) # Phase 3 again
if newFindings.length >= previousCount:
break # No progress, stop
previousCount = newFindings.length
findings = newFindings
Verification gate: Every fix batch must pass lint + typecheck + test. If verification fails:
git checkout -- .)unsafeCross-concern cascade examples:
Generate a structured report with two sections:
1. Fixes Applied: For each fix that was applied:
2. Remaining Findings (requires human action): For each unsafe finding that was not auto-fixed:
Example report output:
=== HARNESS CODEBASE CLEANUP REPORT ===
Fixes applied: 12
- 5 unused imports removed (safe)
- 3 dead exports de-exported (safe)
- 2 commented-out code blocks deleted (safe)
- 1 forbidden import replaced (probably-safe, approved)
- 1 orphaned dependency removed (probably-safe, approved)
Convergence: 3 iterations, 12 → 8 → 3 → 3 (stopped)
Remaining findings: 3 (require human action)
1. UNSAFE: Circular dependency
File: src/services/order-service.ts <-> src/services/inventory-service.ts
Why: Circular dependencies require structural refactoring
Suggested: Extract shared logic into src/services/stock-calculator.ts
2. UNSAFE: Dead internal function
File: src/utils/legacy.ts:45 — processLegacyFormat()
Why: Cannot reliably determine all callers (possible dynamic usage)
Suggested: Search for string references, check config files, then delete if confirmed unused
3. UNSAFE: Public API dead export
File: packages/core/src/index.ts — legacyHelper
Why: Export is in package entry point; external consumers may depend on it
Suggested: Deprecate with @deprecated JSDoc tag, remove in next major version
These are common rationalizations that sound reasonable but lead to incorrect results. When you catch yourself thinking any of these, stop and follow the documented process instead.
| Rationalization | Why It Is Wrong |
|---|---|
| "This dead export is in a high-churn file but the removal is clearly safe" | High-churn files have more hidden consumers. Safe findings in the top 10% by churn are downgraded to probably-safe, requiring explicit approval. |
| "The convergence loop is not reducing findings quickly enough, so I will apply unsafe fixes to make progress" | Unsafe findings are never auto-fixed, regardless of convergence pressure. Each requires human judgment. |
| "The verification gate failed on a probably-safe fix, but I am confident the fix is correct" | When verification fails after a fix batch, the entire batch must be reverted and all findings reclassified as unsafe. |
| "I will skip the hotspot context phase since it adds time and the churn data is just supplementary" | The hotspot map drives safety classification accuracy. Without it, safe fixes in high-churn areas are not downgraded. |
After removing the legacy-auth module:
src/services/auth.ts has 42 commits (top 5%).src/utils/token.ts (were only used by legacy-auth). Architecture detects 1 forbidden import in src/services/session.ts (still importing from removed module's location).safe but downgraded to probably-safe because token.ts is in a high-churn file. Forbidden import classified as unsafe (no alternative configured).token.ts now has zero exports and becomes a dead file. Second iteration deletes the dead file. Convergence stops -- the forbidden import requires manual restructuring.harness cleanup --type dead-code --json -- Dead code detection inputharness check-deps --json -- Architecture violation detection inputgit log analysis -- Hotspot context for safety classification (inline command, no skill invocation needed)harness validate -- Final validation after all fixesharness check-deps -- Final architecture check after all fixesharness validate passes after cleanup