Harness codebase cleanup

From Intense-Visions/harness-engineering · View interactive version

Harness codebase cleanup is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > Orchestrate dead code removal and architecture violation fixes with a shared convergence loop. Catches cross-concern cascades that individual skills miss.

How to use this skill

Skill file

Harness Codebase Cleanup

Orchestrate dead code removal and architecture violation fixes with a shared convergence loop. Catches cross-concern cascades that individual skills miss.

When to Use

Flags

| 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 |

Process

Phase 1: CONTEXT -- Build Hotspot Map

  1. Run hotspot detection via git log analysis:

```bash

git log --format=format: --name-only --since="6 months ago" | sort | uniq -c | sort -rn | head -50

```

  1. Build churn map. Parse output into a file -> commit count mapping.
  2. Compute top 10% threshold. Sort all files by commit count. The file at the 90th percentile defines the threshold. Files above this threshold are "high churn."
  3. Store as HotspotContext for use in Phase 3 (CLASSIFY).

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.

Phase 2: DETECT -- Run Both Concerns in Parallel

  1. Dead code detection (skip if --architecture-only):
  1. Architecture detection (skip if --dead-code-only):
  1. Merge findings. Convert all raw findings into CleanupFinding objects using classifyFinding(). This normalizes both concerns into a shared schema.

Phase 3: CLASSIFY -- Safety Classification and Dedup

  1. 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 |

  1. 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.
  1. Cross-concern dedup. Call deduplicateFindings() to merge overlapping findings:

Phase 4: FIX -- Convergence Loop

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:

  1. Revert the entire batch (use git: git checkout -- .)
  2. Reclassify all findings in the batch as unsafe
  3. Continue the loop with remaining findings

Cross-concern cascade examples:

Phase 5: REPORT -- Actionable Output

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

Rationalizations to Reject

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. |

Examples

Example: Post-Refactoring Cleanup

After removing the legacy-auth module:

  1. Phase 1 (CONTEXT): Hotspot analysis shows src/services/auth.ts has 42 commits (top 5%).
  2. Phase 2 (DETECT): Dead code detects 3 dead exports in 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).
  3. Phase 3 (CLASSIFY): Dead exports classified as safe but downgraded to probably-safe because token.ts is in a high-churn file. Forbidden import classified as unsafe (no alternative configured).
  4. Phase 4 (FIX): First iteration removes 3 dead exports (approved as probably-safe). Re-detect finds 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.
  5. Phase 5 (REPORT): 4 fixes applied (3 dead exports + 1 dead file), 1 remaining finding (forbidden import requiring restructuring).

Harness Integration

Success Criteria

Escalation

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.