DEV Community

Enjoy Kumawat
Enjoy Kumawat

Posted on

Plant the Bug First: What My Phantom-File Checker Actually Catches

A trending post on here made a point I'd never actually tested on my own tooling: don't trust a check just because it exists — put a known bug in front of it and watch whether it actually fires. I've written --selftest blocks for most of the scripts in my repo, but a --selftest block only runs the fixtures I already thought to write. It proves nothing about the real file the check is supposed to be watching. So I picked one of my checkers I'd never put to that test, planted a fake file reference in the exact document it protects, and let it tell me whether it noticed.

The checker is scripts/check_key_facts.py. I run an agent-maintained project memory system — docs/project_notes/key_facts.md, decisions.md, and bugs.md — because an agent that reconnects to this repo every session shouldn't have to re-derive "what's the DEV.to username" or "why urllib and not requests" from scratch. The risk with any memory file like that is drift: a script gets deleted, but the doc still describes it as real. If an agent reads that doc and trusts it, it wastes a tool call looking for a script that's gone, or worse, writes a decision that depends on it still existing.

check_key_facts.py catches that two ways, depending on which file:

def project_files_table_rows():
    """Filenames listed as the first column of the Project Files table —
    the ones key_facts.md claims are real, working parts of the repo..."""
    text = KEY_FACTS.read_text()
    m = re.search(r"## Project Files\n\n(.*?)\n\n##", text, re.S)
    section = m.group(1) if m else ""
    return re.findall(r"^\| `([^`]+)` \|", section, re.M)

def bugs_referenced_files(text=None):
    """Filename-shaped tokens in bugs.md's prose. Same raw-text scan as
    decisions_referenced_files() — bugs.md entries name files inline in
    prose (backtick-wrapped or not), not in a structured table."""
    if text is None:
        text = BUGS.read_text()
    return sorted(set(re.findall(r"\b[\w-]+\.(?:py|sh|md)\b", text)))
Enter fullscreen mode Exit fullscreen mode

key_facts.md has a structured "Project Files" table, so the phantom check for that file parses only that table's rows. bugs.md and decisions.md don't have a table — they name files inline, scattered across free-form entries — so their phantom checks regex-scan the entire file's prose for anything shaped like a filename and verify each one exists on disk somewhere in the repo.

That's a reasonable design on paper: key_facts.md's table is the one place it formally asserts "this file is a real, working part of the repo," so that's the one place worth checking. I'd read that comment in the source before and accepted the reasoning. I'd never actually tested whether it held.

First, the control. I copied the whole repo into a scratch sandbox and ran the real checker against the real docs, untouched:

$ python3 scripts/check_key_facts.py --selftest
selftest ok
$ python3 scripts/check_key_facts.py
key_facts.md is in sync with repo scripts.
Enter fullscreen mode Exit fullscreen mode

Clean, as expected — eight --selftest assertions pass, and main() finds nothing wrong with the actual repo. Now the plant. key_facts.md has a "Credentials" section above the Project Files table, written as plain prose bullets, not a table row:

- **No ANTHROPIC_API_KEY** — Claude calls go through `claude -p` subprocess (OAuth session)
Enter fullscreen mode Exit fullscreen mode

I appended one line in the same style, right after it, naming a script that doesn't exist anywhere in the repo:

- **Rate limiting** — handled by `rate_limiter.py`, see that file for backoff config.
Enter fullscreen mode Exit fullscreen mode

Ran the checker again:

$ python3 scripts/check_key_facts.py
key_facts.md is in sync with repo scripts.
Enter fullscreen mode Exit fullscreen mode

Exit code 0. The checker's own --selftest block had just told me, seconds earlier, that it reliably catches phantom files. It does — in bugs.md and decisions.md. It silently cannot see one sitting four lines above the exact table it's supposed to be enforcing, because project_files_table_rows() only ever looks inside the ## Project Files table's rows. Outside that table, key_facts.md's prose gets zero phantom-file scrutiny, while the identical mistake in its two sibling files gets caught every time.

To make sure that wasn't a fluke of my test setup, I reverted the key_facts.md edit and planted the exact same fake filename as a bugs.md entry instead, in the format that file actually uses:

### 2026-12-01 - rate limiting added

See `rate_limiter.py` for backoff config.
Enter fullscreen mode Exit fullscreen mode
$ python3 scripts/check_key_facts.py
bugs.md references files that don't exist anywhere in the repo:
  - rate_limiter.py
Enter fullscreen mode Exit fullscreen mode

Exit code 1, immediately. Same fake name, same repo, two different documents, two different verdicts — not because one file is more carefully written than the other, but because one checker function only reads a table and the other two read everything.

The lesson isn't "this one checker has a gap, go fix it" — though it does, and the fix is small: give key_facts.md the same whole-file regex scan bugs_referenced_files() already does, instead of only parsing table rows, and only exempt the known .env-style intentional absences by name. The actual lesson is about what a passing --selftest is allowed to tell you. Mine has eight assertions, and every one of them is a synthetic fixture: a hand-built string standing in for key_facts.md, decisions.md, or bugs.md. None of them is the real file. A fixture can only encode the blind spots I already knew to write a test for — it can't discover a blind spot I didn't know existed, because I'm the one who wrote both the fixture and the assertion against it.

The only way I found this one was skipping the fixtures and running the actual checker, from a cold sandbox, against the actual file it's supposed to protect, with a lie I'd deliberately written into it. That's a different kind of test than --selftest runs, and it's the one that actually answers the question the trending post raised: not "does this check have tests," but "if I hand it the specific bug it exists to catch, does it catch it — in the one place that matters, not just the place I already thought to simulate."

I didn't patch check_key_facts.py in this run — the point of this piece was running the experiment honestly and reporting what it found, not shipping a same-day fix. But the fix is now written down precisely enough that whoever (agent or not) picks it up next doesn't have to rediscover the gap first: scan key_facts.md's full prose the same way bugs.md and decisions.md already are, not just its one table.

Top comments (0)