Ratchet Baseline
codeknife.baseline records the findings a repository already has, so codeknife check only fails on findings that are new or worse. Use it to adopt CodeKnife on an existing codebase without fixing everything first, then let the file shrink as you fix things.
The full cycle
Section titled “The full cycle”The demo is a git repository with four findings: a hardcoded key, an orphan file, a nesting depth of 5 and a function with 6 parameters. Paths printed by the binary are shortened to demo/.
-
Record the current findings.
Terminal window codeknife --no-cache baseline updateupdated demo/codeknife.baseline (4 entries)# codeknife baseline v1: shrink-only violation ratchet. Prune with `codeknife baseline prune`.CK_ARCH_DEEP_NESTING src/app.py src.app.build_report 5CK_ARCH_TOO_MANY_PARAMS src/app.py src.app.build_report 6CK_DEAD_FILE src/token.py src/token.py 1CK_SEC_HARDCODED_SECRET src/token.py h:317d00e91a9e 1 -
Check now passes. The baselined findings are hidden and counted in a note.
Terminal window codeknife --no-cache --format human checknote: no coverage file found (running complexity-only mode); generate one with: pytest --cov --cov-report=xml:coverage.xmlnote: analyzed 2 commit(s) over the last 90 day(s)note: 4 finding(s) suppressed by codeknife.baselinecodeknife check: PASS (2 files, 0 errors, 0 warnings, 0 info, 7ms)Commit
codeknife.baseline. -
Make things worse: add a seventh parameter to
build_reportand a second hardcoded key tosrc/token.py.Terminal window codeknife --no-cache --format agent checkCODEKNIFE check status=fail files=2 errors=1 warnings=1 infos=0 elapsed_ms=4[1] ERROR CK_SEC_HARDCODED_SECRET src/token.py:2-2 sym=- impact=95 | potential hardcoded secret (Stripe live secret key) in source code | fix: Move credentials to environment variables or an Age/secret manager vault.[2] WARNING CK_ARCH_TOO_MANY_PARAMS src/app.py:11-18 sym=src.app.build_report impact=45 | `build_report` takes 7 parameters (limit 5) | fix: Group cohesive parameters into an options or domain context struct.NOTE no coverage file found (running complexity-only mode); generate one with: pytest --cov --cov-report=xml:coverage.xmlNOTE analyzed 3 commit(s) over the last 90 day(s)NOTE 3 finding(s) suppressed by codeknife.baselineThe parameter count went from 6 to 7, above the recorded
6, so that finding is back. The second key is a second occurrence of an entry with count1, so one of the two keys is reported. -
Re-recording the baseline to hide the regressions is caught by the Loosening Guard:
Terminal window codeknife --no-cache baseline updatecodeknife --no-cache --format human checkupdated demo/codeknife.baseline (4 entries)loosening warnings:! codeknife.baseline: grew `CK_ARCH_TOO_MANY_PARAMS src/app.py src.app.build_report` from 6 to 7! codeknife.baseline: grew `CK_SEC_HARDCODED_SECRET src/token.py h:317d00e91a9e` from 1 to 2note: no coverage file found (running complexity-only mode); generate one with: pytest --cov --cov-report=xml:coverage.xmlnote: analyzed 3 commit(s) over the last 90 day(s)note: 5 finding(s) suppressed by codeknife.baselinecodeknife check: FAIL (2 files, 0 errors, 0 warnings, 0 info, 7ms) -
Fix the code instead: remove the extra parameter and delete
src/token.py. Then prune.Terminal window codeknife --no-cache baseline prunepruned demo/codeknife.baseline (4 -> 2 entries)# codeknife baseline v1: shrink-only violation ratchet. Prune with `codeknife baseline prune`.CK_ARCH_DEEP_NESTING src/app.py src.app.build_report 5CK_ARCH_TOO_MANY_PARAMS src/app.py src.app.build_report 7CK_DEAD_FILE src/token.py src/token.py 1CK_SEC_HARDCODED_SECRET src/token.py h:317d00e91a9e 2CK_ARCH_TOO_MANY_PARAMS src/app.py src.app.build_report 6Entries for findings that no longer exist are removed. The parameter limit drops to the current value,
6.
File format
Section titled “File format”codeknife.baseline lives at the repository root (the --path directory). It is plain text:
-
The first line written is this header:
# codeknife baseline v1: shrink-only violation ratchet. Prune with `codeknife baseline prune`. -
Blank lines and lines starting with
#are ignored when reading. -
Every other line has exactly four tab-separated columns:
rule,file,symbol,value. -
valueis a non-negative integer. -
Entries are written sorted by rule, then file, then symbol.
A line with the wrong column count, or a value that is not an integer, stops every command that reads the baseline with exit code 2:
codeknife error: codeknife.baseline:4: expected 4 tab-separated columns, found 3codeknife error: codeknife.baseline:4: invalid integer 'lots': invalid digit found in stringcodeknife init writes a header-only baseline if the file does not exist (or always, with --force). See Init, Skill & Usage Spec.
How entries are keyed
Section titled “How entries are keyed”Each entry is keyed by (rule, file, symbol):
| Column | Source |
|---|---|
rule |
the finding’s rule ID |
file |
the finding’s file path, relative to the root |
symbol |
the finding’s symbol, if it has one. Otherwise h: plus the first 12 hex digits of an xxh3 hash of the rule ID, a tab and the finding message |
Line numbers are not part of the key. A finding that moves to another line in the same file still matches. A finding without a symbol whose message changes gets a new key.
When reading, an entry whose symbol is line:N is also accepted for a symbol-less finding starting on line N. CodeKnife never writes that form.
What the value means
Section titled “What the value means”The value depends on whether the finding carries a metric (metric_value in JSON):
| Finding type | Value written by update |
Finding passes check when |
|---|---|---|
| With a metric (for example parameter count, nesting depth) | the highest metric seen for that key, rounded up, at least 1 |
its metric, rounded up (at least 1), is less than or equal to the value |
| Without a metric | the number of findings with that key | it is one of the first value findings with that key in the run |
info findings are never written to the baseline and are never filtered by it.
How check treats baselined findings
Section titled “How check treats baselined findings”When codeknife.baseline exists, every report that goes through the gate filters warning and error findings through it:
- Inside the limit: the finding is removed from the report. The note
N finding(s) suppressed by codeknife.baselinecounts them. - New (no entry for its key): reported as normal.
- Worse (metric above the value, or more occurrences than the value): reported as normal. For count-based entries the extra occurrences are reported, in the order the findings were sorted.
- Gone (an entry with no matching finding): nothing happens. The entry stays in the file and keeps allowing that finding until you prune it.
Guard findings (CK_GUARD_*) are ordinary findings, so baseline update records them too. Loosenings are not findings and cannot be baselined.
The same filter is applied by the single-blade commands (for example codeknife arch) and by the codeknife_check MCP tool. codeknife plan also filters through the baseline. codeknife map does not.
Commands
Section titled “Commands”All three are subcommands of codeknife baseline. update and prune print their result line to stderr and exit 0.
baseline update
Section titled “baseline update”Runs the full static suite (the same blades, guard and toggles as a standard check) and replaces codeknife.baseline with the current findings.
--changeddoes not scope it. It always records the whole repository.--deepand--tier fullfindings are never recorded, becauseupdatedoes not run them.- It can raise values and add entries. That is how you accept new debt on purpose. The loosening guard reports the growth (step 4 above).
baseline prune
Section titled “baseline prune”Runs the same static suite and writes a new baseline that can only be smaller:
- An entry with no current finding is removed.
- An entry whose current value is lower is lowered to the current value.
- An entry whose current value is higher keeps its old value.
- New findings are not added.
If codeknife.baseline does not exist, prune creates a header-only file:
pruned demo/codeknife.baseline (0 -> 0 entries)This is the shrink-only rule in code: prune keeps min(old, current) for keys present in both, and drops everything else.
baseline merge-driver BASE OURS THEIRS
Section titled “baseline merge-driver BASE OURS THEIRS”A three-way merge for codeknife.baseline, using git’s %O %A %B arguments. It reads the three files, merges them, writes the result into OURS, and exits 0.
Merge rules for each key in OURS or THEIRS:
| In base | In ours | In theirs | Result |
|---|---|---|---|
| any | yes | yes | the lower of the two values |
| yes | yes | no | dropped (theirs pruned it) |
| yes | no | yes | dropped (ours pruned it) |
| no | yes | no | kept with ours’ value |
| no | no | yes | kept with theirs’ value |
Run directly on three hand-written files (header lines omitted here):
tail -n +2 base.baseline ours.baseline theirs.baseline==> base.baseline <==CK_ARCH_LONG_FN src/a.rs run 80CK_SLOP_EMPTY_CATCH src/b.ts h:aaaaaaaaaaaa 2
==> ours.baseline <==CK_ARCH_LONG_FN src/a.rs run 70
==> theirs.baseline <==CK_ARCH_LONG_FN src/a.rs run 75CK_SLOP_EMPTY_CATCH src/b.ts h:aaaaaaaaaaaa 2CK_DEAD_SYMBOL src/c.py src.c.old 1codeknife baseline merge-driver base.baseline ours.baseline theirs.baselineecho $?cat ours.baseline0# codeknife baseline v1: shrink-only violation ratchet. Prune with `codeknife baseline prune`.CK_ARCH_LONG_FN src/a.rs run 70CK_DEAD_SYMBOL src/c.py src.c.old 1Exit codes:
| Code | When |
|---|---|
0 |
merged; OURS rewritten |
1 |
one of the three files does not parse as a baseline. OURS is left unchanged, so git reports a conflict |
2 |
a file cannot be read, for example codeknife error: reading nope: No such file or directory (os error 2) |
Registering it with git
Section titled “Registering it with git”Neither codeknife init nor any other command registers the driver. Do it yourself with standard git configuration. The driver name (codeknife-baseline below) is your choice.
echo 'codeknife.baseline merge=codeknife-baseline' > .gitattributesgit config merge.codeknife-baseline.name "CodeKnife shrink-only baseline"git config merge.codeknife-baseline.driver "codeknife baseline merge-driver %O %A %B"In the demo, main lowered one value and feature pruned an entry and added one. Without the driver:
Auto-merging codeknife.baselineCONFLICT (content): Merge conflict in codeknife.baselineAutomatic merge failed; fix conflicts and then commit the result.With the driver:
Auto-merging codeknife.baselineMerge made by the 'ort' strategy. codeknife.baseline | 1 + 1 file changed, 1 insertion(+)The merged file has the same content as the direct run above. git config settings are local to each clone, so every developer and CI job that merges needs them.
Baseline and the loosening guard
Section titled “Baseline and the loosening guard”The guard compares codeknife.baseline at the base git ref with the file on disk. It reports a key that is not in the old file, and a value that went up, in these forms:
codeknife.baseline: added new entry `RULE FILE SYMBOL` (VALUE)codeknife.baseline: grew `RULE FILE SYMBOL` from OLD to NEWLowered and removed entries are not reported. If the old file is missing at the base ref, nothing is compared. If the old file does not parse, it is treated as empty.
By default the base ref is HEAD, so a baseline growth stops being reported once you commit it. Pass --since with the branch you merge into to keep it visible for the whole branch:
codeknife --no-cache --format agent checkcodeknife --no-cache --format agent --since HEAD~1 checkCODEKNIFE check status=pass files=2 errors=0 warnings=0 infos=0 elapsed_ms=10...CODEKNIFE check status=fail files=2 errors=0 warnings=0 infos=0 elapsed_ms=8LOOSENINGS count=2 - codeknife.baseline: grew `CK_ARCH_TOO_MANY_PARAMS src/app.py src.app.build_report` from 6 to 7 - codeknife.baseline: grew `CK_SEC_HARDCODED_SECRET src/token.py h:317d00e91a9e` from 1 to 2...Details are on Loosening Guard.
Also available via
Section titled “Also available via”codeknife_checkMCP tool: applies the baseline filter. There is no MCP tool forupdate,pruneormerge-driver. See MCP Tools.- The Check Gate covers how baseline filtering feeds pass or fail.