Skip to content

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

  1. Record the current findings.

    Terminal window
    codeknife --no-cache baseline update
    updated 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 5
    CK_ARCH_TOO_MANY_PARAMS src/app.py src.app.build_report 6
    CK_DEAD_FILE src/token.py src/token.py 1
    CK_SEC_HARDCODED_SECRET src/token.py h:317d00e91a9e 1
  2. Check now passes. The baselined findings are hidden and counted in a note.

    Terminal window
    codeknife --no-cache --format human check
    note: no coverage file found (running complexity-only mode); generate one with: pytest --cov --cov-report=xml:coverage.xml
    note: analyzed 2 commit(s) over the last 90 day(s)
    note: 4 finding(s) suppressed by codeknife.baseline
    codeknife check: PASS (2 files, 0 errors, 0 warnings, 0 info, 7ms)

    Commit codeknife.baseline.

  3. Make things worse: add a seventh parameter to build_report and a second hardcoded key to src/token.py.

    Terminal window
    codeknife --no-cache --format agent check
    CODEKNIFE 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.xml
    NOTE analyzed 3 commit(s) over the last 90 day(s)
    NOTE 3 finding(s) suppressed by codeknife.baseline

    The 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 count 1, so one of the two keys is reported.

  4. Re-recording the baseline to hide the regressions is caught by the Loosening Guard:

    Terminal window
    codeknife --no-cache baseline update
    codeknife --no-cache --format human check
    updated 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 2
    note: no coverage file found (running complexity-only mode); generate one with: pytest --cov --cov-report=xml:coverage.xml
    note: analyzed 3 commit(s) over the last 90 day(s)
    note: 5 finding(s) suppressed by codeknife.baseline
    codeknife check: FAIL (2 files, 0 errors, 0 warnings, 0 info, 7ms)
  5. Fix the code instead: remove the extra parameter and delete src/token.py. Then prune.

    Terminal window
    codeknife --no-cache baseline prune
    pruned 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 5
    CK_ARCH_TOO_MANY_PARAMS src/app.py src.app.build_report 7
    CK_DEAD_FILE src/token.py src/token.py 1
    CK_SEC_HARDCODED_SECRET src/token.py h:317d00e91a9e 2
    CK_ARCH_TOO_MANY_PARAMS src/app.py src.app.build_report 6

    Entries for findings that no longer exist are removed. The parameter limit drops to the current value, 6.

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.

  • value is 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 3
codeknife error: codeknife.baseline:4: invalid integer 'lots': invalid digit found in string

codeknife init writes a header-only baseline if the file does not exist (or always, with --force). See Init, Skill & Usage Spec.

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.

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.

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

All three are subcommands of codeknife baseline. update and prune print their result line to stderr and exit 0.

Runs the full static suite (the same blades, guard and toggles as a standard check) and replaces codeknife.baseline with the current findings.

  • --changed does not scope it. It always records the whole repository.
  • --deep and --tier full findings are never recorded, because update does 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).

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.

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

Terminal window
tail -n +2 base.baseline ours.baseline theirs.baseline
==> base.baseline <==
CK_ARCH_LONG_FN src/a.rs run 80
CK_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 75
CK_SLOP_EMPTY_CATCH src/b.ts h:aaaaaaaaaaaa 2
CK_DEAD_SYMBOL src/c.py src.c.old 1
Terminal window
codeknife baseline merge-driver base.baseline ours.baseline theirs.baseline
echo $?
cat ours.baseline
0
# codeknife baseline v1: shrink-only violation ratchet. Prune with `codeknife baseline prune`.
CK_ARCH_LONG_FN src/a.rs run 70
CK_DEAD_SYMBOL src/c.py src.c.old 1

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

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.

Terminal window
echo 'codeknife.baseline merge=codeknife-baseline' > .gitattributes
git 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.baseline
CONFLICT (content): Merge conflict in codeknife.baseline
Automatic merge failed; fix conflicts and then commit the result.

With the driver:

Auto-merging codeknife.baseline
Merge 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.

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 NEW

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

Terminal window
codeknife --no-cache --format agent check
codeknife --no-cache --format agent --since HEAD~1 check
CODEKNIFE 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=8
LOOSENINGS 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.

  • codeknife_check MCP tool: applies the baseline filter. There is no MCP tool for update, prune or merge-driver. See MCP Tools.
  • The Check Gate covers how baseline filtering feeds pass or fail.