--- name: chestertons-fence version: 1.2.0 description: "Find why code or a constraint exists before you change it, from git, PR, and ADR history. Use for \"why does this code/constraint exist\". Not for forward risk (use pre-mortem)." license: Apache-2.0 user-invocable: true metadata: capability: kind: reusable-primitive owns: - code-archaeology status: active routing: role: conditional-adjunct invoker: memory-gate trigger: memory-gate points here for the full four-phase investigation framework user-facing: true --- # Chesterton's Fence Investigation Enforce epistemic humility before changing existing systems. Understand original purpose before proposing changes. ## Quick Start ```text # Investigate why code exists before changing it /chestertons-fence "path/to/file.py" "remove unused validation" # Investigate an ADR before deprecating it /chestertons-fence "docs/architecture/ADR-005.md" "allow bash scripts" ``` ## Triggers | Phrase | Context | |--------|---------| | `why does this exist` | Investigating existing code or patterns | | `chestertons fence` | Explicit investigation request | | `before removing` | Planning deletion or replacement | | `investigate history` | Researching original rationale | | `prior art investigation` | ADR-required investigation | ## Quick Reference | Input | Output | Destination | |-------|--------|-------------| | File path or ADR number | Investigation report | `.project-toolkit/analysis/NNN-chestertons-fence-TOPIC.md` | | Component description | Historical context summary | stdout (JSON) | ## Process ```text 1. Identify Structure What exists? Where is it defined? | v 2. Git Archaeology git log, git blame to find origin commit | v 3. PR/ADR Search Find the PR or ADR with original rationale | v 4. Dependency Analysis What references or depends on this? | v 5. Generate Report Fill the investigation template | v 6. Decision REMOVE | MODIFY | PRESERVE | REPLACE ``` ### Step Details **Step 1: Identify Structure.** Locate the exact file, function, pattern, or constraint under investigation. Record its current form. **Step 2: Git Archaeology.** Pick the probe by target kind. For a whole file, run `git log --follow --diff-filter=A -- `; the bundled investigate script does this, for files only. For a text span (a section, a rule line, a constant), run `git log -S '' --reverse` and read the oldest hits. Use `git blame` for the last touch, not the origin. Identify the commit that introduced the target, the author, and the date. Probe the repo's merge convention before you cite a PR: `git log --oneline -5` shows whether subjects carry `(#NNNN)`. **Checker.** Confirm the origin with a second probe that lacks the first one's blind spot, then run `git show ` on the candidate. It passes when both probes name the same commit and its diff adds the target as new text, not as a rename, mirror copy, or template move. When they disagree, probe the older candidate. Stop after three rounds and report every candidate with its evidence. **Step 3: PR/ADR Search.** Search for the originating PR using `gh pr list --search`. Check `${AWESOME_AI_ARCHITECTURE_DIR:-.project-toolkit/architecture}` for related ADRs (default `.project-toolkit/architecture`). Look for comments explaining intent. **Step 4: Dependency Analysis.** Use `grep` or `Grep` tool to find all references. Map upstream and downstream dependencies. Identify what breaks if the structure is removed. **Step 5: Generate Report.** Use the template at the bundled `templates/chestertons-fence-investigation.md` in this skill directory. Fill all sections with evidence from steps 1 through 4. **Step 6: Decision.** Based on evidence, recommend one action: | Decision | When to Use | |----------|-------------| | PRESERVE | Original rationale still applies | | MODIFY | Purpose valid but implementation needs updating | | REPLACE | Better approach exists, original concern addressed | | REMOVE | Original rationale no longer applies, with evidence | ## Post-2023 evidence rules This skill answers one question: what evidence exists that this code or constraint has a real purpose? It owns capability `code-archaeology`, the evidence-gathering step the `review` skill's technical-review contract consumes for code-intent analysis. For code committed during or after calendar year 2023, apply these rules: - Git history proves provenance (who committed it, and when), not intent. A commit message or a clean `git blame` does not establish why the code exists. - Author identity does not raise the preservation prior. A senior contributor or a well-reviewed PR is not evidence the code is still needed. - Complexity, polish, and apparent effort do not raise the preservation prior. Sophisticated code is not more likely to be load-bearing than simple code; it only takes longer to read. - Absence of rationale after this investigation runs (steps 1 through 4) is itself meaningful evidence, not an inconclusive result to set aside. - Inconclusive evidence, meaning no found rationale and no evidence the code is unused, may lead to a yell-test proposal instead of a REMOVE decision. The proposal's shape belongs to the `review` skill's technical-review contract; this skill supplies the evidence the proposal cites. ## Usage ```bash # Investigate a file or pattern python3 scripts/investigate.py --target path/to/file.py --change "remove unused validation" # Investigate an ADR python3 scripts/investigate.py --target docs/architecture/ADR-005.md --change "allow bash scripts" # Output as JSON (for automation) python3 scripts/investigate.py --target path/to/file.py --change "description" --format json ``` ## Scripts | Script | Purpose | Exit codes | |--------|---------|------------| | [`scripts/investigate.py`](scripts/investigate.py) | Git archaeology (origin commit, recent changes), related-ADR search, and dependent search for a target; emits a Markdown or JSON investigation report from the bundled template. | 0=success, 1=error | ## Gotchas - A file a build step mirrored or moved has a newer add commit at its current path. Without `--follow`, `--diff-filter=A` returns that copy. - `git log -S` searches every path, so the oldest hit can be a doc that quoted the text. Confirm with `git show` that it adds the target to this file or its earlier path. - Fetched PR refs hold pre-squash commits. Cite commits on the default branch unless asked about the PR branch. ## Verification - [ ] The Step 2 checker passed, or the report lists every unresolved candidate. - [ ] Investigation ran for the target before any change proposal was made. - [ ] Origin commit, recent changes, related ADRs, and dependents were gathered (or explicitly recorded as none found). - [ ] The resulting report (template-based or inline fallback) is attached to the change proposal or the ADR Prior Art Investigation section. ## References | File | Content | |------|---------| | `references/chestertons-fence-mental-model.md` | Core mental model, principle, investigation checklist, related models | | `references/boy-scout-rule.md` | Scoped improvement boundaries, connection to investigation workflow | | `references/legacy-code-techniques.md` | Bottom-up refactoring approach, inheritance vs composition, reading list |