External review of the ruleset and reap. All five reproduced before changing anything; two were worse than reported. A capture narrower than the credential it matches stops early and the preview then prints the rest of the key as surrounding context. The OpenAI rule excluded `-` and `_`, so a modern sk-proj key ending on `-` had its tail printed raw, and one ending on `_` matched nothing at all — a missed key, not just a leaky preview. The rule is fixed, but the guarantee now holds structurally: masked spans are widened over the whole credential-shaped run, so any rule with that flaw is survivable, including ones added later. The two NAME=value heuristics were blind inside JSONL transcripts, which is the surface nanny exists for. A transcript's newlines are the two characters \n and its quotes are \", so (?m)^ never matched; and \b does not match after an underscore, so SECRET inside SUPABASE_SECRET_KEY was invisible. Measured before: 3/3 in a raw .env dump, 0/3 inside a transcript line. After: 3/3 in both. It also made the ruleset six times faster — consuming a real character instead of a zero-width assertion lets the engine build a literal prefilter, and 37.6 MB went from 1.7s to 0.29s. Reap supersession paired each stale row against any survivor with a matching fingerprint. Where one fingerprint covers several distinct secrets — which is what a marker rule does — deleting one of two private keys from a file paired both stale rows against the single survivor and recorded a deletion as a move. Pairing is now one-to-one, which is also the right answer for ordinary rules where the pool holds one entry. The harness was inferred from the path although the collector knew it, so a repository with its own .claude/ directory was labelled as the claude-code harness and never got the working-tree demotion. Excerpts carry the collector's answer now. And .txt no longer counts as documentation: a scratch notes.txt is the likeliest place for a pasted key, and demoting it quietened exactly the finding worth shouting about. The sixth was found by running the fixes: editing two heuristics made 167 findings report as "no longer on disk" when nothing had moved. Reaping compares against what a previous sweep recorded, so it is only meaningful when both used the same ruleset. Sweeps now record a ruleset digest and stand the reap down when it changes. The 172 rows falsely marked gone on this machine have been cleared. While confirming the transcript fix on real data, MAX_PROMPT_TOKENS turned out to be a whole false-positive class the relaxation exposed. A suffix must now start with an underscore, so plural names — counts and collections — no longer match while SECRET_KEY_ID still does. Measured on roosta: 743 new findings, 192 of them in claude transcripts that were previously invisible, including 13 OpenAI keys. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XAqHWfdMAsYu1o36tgeima
7.4 KiB
nanny — notes for agents
Read ~/git/architecture/generic.md first; it is the baseline. readme.md here
lists the deliberate deviations. What follows is what is easy to get wrong in
this codebase.
The one rule that matters
A matched secret never leaves the scanner. nanny_entities::redact is where
that is enforced, and it is enforced by types rather than by care:
Secret<'a> borrows, does not clone into an owned form, does not serialise, and
Debugs as Secret(<redacted, N bytes>).
If you find yourself wanting to widen that — to store a prefix "just for identification", to log a value while debugging, to put a real value in a notification so it is more useful — stop. The fingerprint identifies, the length disambiguates, the preview locates. That is sufficient, and it has to be, because a leaked value in nanny's own database costs the operator a rotation they cannot delegate.
Secret::expose_for_validation exists for rule validators that must inspect the
value inside the scanner. It is not an escape hatch for anything else.
Things measured on real data, not to be re-litigated by intuition
Both of these looked correct and were not. They are documented at the code, and
the numbers are in readme.md:
- Do not reintroduce
RegexSet. One combined pass over twenty-two patterns was 6× slower than running them individually, because the combined automaton loses the per-pattern literal prefilters. - Do not cut chunks at the last newline in the window. It stalls to one byte
per iteration on a short-line-then-huge-line file and eats gigabytes. A
newline is only a valid cut point in the second half of a window, and
scan.overlap_bytesmust stay under half ofscan.chunk_bytesfor the progress invariant to hold.
Before adding a rule whose pattern has no literal prefix, measure it:
cargo run --release -p nanny-core --example profile -- ~/.claude/history.jsonl
Adding a harness
It should be a table, not a new implementation. crates/nanny-data/src/collect/mod.rs
holds the per-harness globs; FileCollector covers anything file-shaped and
SqliteCollector anything that keeps its transcripts in a database. Choose
Pattern::tail for append-only sources and Pattern::whole for rewritten ones
— getting that wrong is how a collector silently stops seeing new data.
Adding a rule
Two failure modes worth checking before you add one, both found by review rather than by tests:
- A capture that stops short leaks its own tail. If the character class is
narrower than the credential —
[A-Za-z0-9]against a key containing-— the match ends early and everything after it is printed as preview context. The mask is widened over the credential-shaped run to make that survivable, but the rule should still be right. Check with a value that contains-,_and a trailing=. - A rule that only works on disk is a rule that does not work. Most of what
nanny reads is JSONL: newlines are the two characters
\n, quotes are\", and\bdoes not match after an underscore. Anchoring on(?m)^or leading with\bwill pass every test you write against a file and find nothing in a transcript. Test both forms of the same content.
Rules are data: crates/nanny-core/rules/builtin.toml. Put context in the
pattern and the secret in capture group 1 — the context must never be
fingerprinted as though it were the value. Give it a RotationHint if there is
anything useful to say, and set rotation_suffices = false for anything whose
leak is incident response rather than a rotation.
Rule ids are persisted with every finding. Renaming one orphans the status history and ignore entries that reference it.
Severity has two inputs
severity on a finding is the effective one; rule_severity is what the rule
alone says; context is why they may differ. Filters, sorts and alerts all use
the effective one — using the rule's would mean judging a finding by a number
that is never displayed.
A demotion must always be able to explain itself (Finding::demotion()). Never
add a context that suppresses rather than demotes, and never demote outside a
working tree: in a harness store there is no way to distinguish a quoted fixture
from a printed credential, and guessing there is how a real leak gets buried.
Suppressions vs. ignoring
Status::Ignored is a judgement about a row. A Suppression is a judgement
about a value, keyed by fingerprint and optionally scoped to a path prefix.
The keying is not incidental — it is what lets nanny remember a judgement
without storing anything it does not already store. A suppression that held the
value, or a plaintext hint of it, would defeat the whole design.
Suppressed findings are still recorded, as ignored. Skipping the write would
make the counts lie about what is on disk.
Three levers, and picking the wrong one is the failure mode
- Suppression — a judgement about a value, keyed by fingerprint. Only safe when the rule captures the secret itself.
- Downgrade — a judgement about a place: this rule, under this path, is not that serious. Path scope is mandatory; a placeless downgrade is a worse way of editing the rule, and neutering a rule fleet-wide from the command line is the mistake worth making impossible.
- Exclusion (
excludein config) — stop reading the path at all. Blunt: a credential landing there later goes unseen.
Rule::matches_marker is what keeps the first two apart. A rule whose capture
is a fixed marker — a PEM header, a magic string — gives every instance the same
fingerprint, so suppressing one suppresses all of them everywhere. Set it on any
rule where that is true, and the CLI will refuse unscoped suppression and point
at a downgrade instead.
Validate before mutating. The guard originally fired after the status changes had been written, which left a half-applied command — worse for the operator than either doing it or refusing.
Reaping
ExcerptSink::note_complete_scan is the only thing that licenses concluding a
secret is gone, and it defaults to a no-op so a collector cannot make the
guarantee by accident. Silence there costs a stale row; a wrong claim costs an
exposure reported as handled.
Never reap on "not re-confirmed". Tail sources resume from a cursor and are almost never re-confirmed; whole-mode sources are stat-skipped when unchanged and are not read at all. Both look identical to "the secret is gone" from the findings table alone, and both are covered by tests that will fail loudly if someone tries.
Vanishing is a timestamp, not a status. A finding can be acknowledged and gone, only the operator sets the first, and vanishing must never resolve anything — the leak outlives the copy on disk.
Exclusions
crates/nanny-core/src/exclude.rs holds two different things that share one
matcher, and the distinction is load-bearing. Sanctioned stores are places a
credential is meant to be, which nanny refuses to read. Noise is generated
or vendored content that costs time and produces fixtures. Adding to the first
list narrows what nanny can see, so it needs to be a genuine store.
Testing
Tests state a behaviour, not a mechanism — a_truncated_file_is_read_from_the_ beginning_again, not test_tail_2. Several exist specifically to prove a
negative (that a preview does not contain the value, that a masked preview
cannot be rediscovered as a fresh finding); those are the ones worth keeping
green above all others.