Skip to content

Quickstart

This page walks you through a full CodeKnife session on a tiny repository with Rust, Python and TypeScript files and a few deliberate problems. It takes about ten minutes. Every command below was run in the order shown, and every output block is the real output.

You need codeknife on your PATH (see Installation) and git. CodeKnife does not need Cargo, Python or Node to analyze these files.

  1. Create the demo repository.

    Terminal window
    mkdir ledger && cd ledger
    mkdir src app web
    cat > Cargo.toml <<'EOF'
    [package]
    name = "ledger"
    version = "0.1.0"
    edition = "2021"
    EOF
    cat > src/main.rs <<'EOF'
    fn parse_amount(raw: &str) -> i64 {
    let value: i64 = raw.trim().parse().unwrap();
    dbg!(value);
    value
    }
    fn old_total(values: &[i64]) -> i64 {
    values.iter().sum()
    }
    fn main() {
    let mut total = 0;
    for raw in ["10", "32"] {
    // add the amount to the total
    total += parse_amount(raw);
    }
    println!("{total}");
    }
    EOF
    cat > app/report.py <<'EOF'
    import requests
    DATABASE_URL = "postgres://admin:hunter2@db.internal:5432/ledger"
    def fetch_report(url):
    response = requests.get(url)
    return response.json()
    EOF
    echo 'requests==2.32.3' > requirements.txt
    cat > web/client.ts <<'EOF'
    export async function loadUser(id: string) {
    const res = await fetch(`/api/users/${id}`);
    const body = (await res.json()) as any;
    return body.name as string;
    }
    EOF
    printf '# Ledger\n\nSee [setup notes](docs/setup.md) before running.\n' > README.md
    git init -b main && git add -A && git commit -m "initial ledger demo"

    The problems planted here: a hard-coded database password, an unwrap(), a leftover dbg!, an unused private function, a comment that narrates the next line, HTTP calls without a timeout, an as any cast, and a README link to a file that does not exist.

  2. Create the starter files with codeknife init.

    Terminal window
    codeknife init
    wrote /path/to/ledger/codeknife.toml
    wrote /path/to/ledger/codeknife.baseline
    wrote /path/to/ledger/arch.toml

    init writes three files and exits with code 0:

    • codeknife.toml holds every config key with its default value. See codeknife.toml.
    • codeknife.baseline starts with only a header comment. See Ratchet Baseline.
    • arch.toml contains a commented-out [[deny]] example for layer rules. See 2. Architecture.

    Commit them. The Loosening Guard compares these files against Git, so they need to be in a commit before you start changing them.

    Terminal window
    git add -A && git commit -m "add codeknife starter files"
  3. Run the gate with codeknife check.

    Terminal window
    codeknife check --format human
    error[CK_SEC_HARDCODED_SECRET]: potential hardcoded secret (database connection URL with embedded credentials) in source code
    --> app/report.py:3
    = fix: Move credentials to environment variables or an Age/secret manager vault.
    warning[CK_DEAD_FILE]: orphan file `app/report.py` has no inbound imports or reachable symbols
    --> app/report.py:1-8 (app/report.py)
    = fix: Delete this unreferenced file or wire its module into an entrypoint.
    warning[CK_SLOP_NO_TIMEOUT]: outbound HTTP request made without an explicit timeout
    --> app/report.py:7
    = fix: Use a client configured with explicit connect/request timeouts or `AbortSignal.timeout(...)`.
    warning[CK_DEAD_FILE]: orphan file `web/client.ts` has no inbound imports or reachable symbols
    --> web/client.ts:1-5 (web/client.ts)
    = fix: Delete this unreferenced file or wire its module into an entrypoint.
    warning[CK_SLOP_NO_TIMEOUT]: outbound HTTP request made without an explicit timeout
    --> web/client.ts:2
    = fix: Use a client configured with explicit connect/request timeouts or `AbortSignal.timeout(...)`.
    warning[CK_SLOP_TYPE_ESCAPE]: panic-prone `unwrap` in production code
    --> src/main.rs:2
    = fix: Propagate `Result`/`Option` cleanly with `?` or `.ok_or(...)` instead of panicking.
    warning[CK_DEAD_SYMBOL]: unreachable internal symbol `old_total`
    --> src/main.rs:7-9 (ledger::old_total)
    = fix: Delete this unused function (or run `codeknife dead --fix --write`).
    warning[CK_SLOP_TYPE_ESCAPE]: TypeScript type-safety escape (`(await res.json()) as any`)
    --> web/client.ts:3
    = fix: Parse/validate external data at the boundary instead of casting through `any` or `unknown`.
    warning[CK_DOC_DEAD_PATH]: broken markdown link target `docs/setup.md`
    --> README.md:3 (docs/setup.md)
    = fix: Update or remove the broken path reference in documentation.
    warning[CK_SLOP_DEBUG_LEFTOVER]: leftover debug macro/breakpoint in production code
    --> src/main.rs:3
    = fix: Remove debugging calls before committing.
    info[CK_CMT_NARRATION]: strippable comment: narration restating adjacent code
    --> src/main.rs:14 (main)
    = fix: Run `codeknife comments --strip --write` to remove safely.
    note: no coverage file found (running complexity-only mode); generate one with: cargo llvm-cov --lcov --output-path lcov.info | npx vitest run --coverage | pytest --cov --cov-report=xml:coverage.xml
    note: analyzed 2 commit(s) over the last 90 day(s)
    codeknife check: FAIL (3 files, 1 errors, 9 warnings, 1 info, 13ms)

    The exit code is 1.

  4. Read the output.

    Each finding has the same shape:

    • The first line is severity[RULE_ID]: message. Severity is error, warning or info. The rule ID tells you which check fired. Every ID is listed in the Rule Catalogue.
    • The --> line is the file and line range. The name in parentheses, when present, is the symbol the finding is attached to.
    • The = fix: line is a hint for the change that resolves the finding.

    Findings are sorted by severity, then by impact score, then by file and line. note: lines report what the run did or skipped. The last line is the verdict, the number of files scanned, the counts per severity and the elapsed time.

    check fails (exit code 1) if any error or warning is left after the baseline is applied, or if the loosening guard reports anything. Info findings alone do not fail it. See The Check Gate.

    check also writes a cache to .codeknife/ (here metrics.json and parse-cache.json). Pass the global --no-cache flag to keep the run read-only. See The .codeknife Directory.

  5. Run single blades.

    Each blade is also its own subcommand. It runs only that blade and uses the same output and exit rules as check, without the loosening guard.

    Terminal window
    codeknife sec --format human
    error[CK_SEC_HARDCODED_SECRET]: potential hardcoded secret (database connection URL with embedded credentials) in source code
    --> app/report.py:3
    = fix: Move credentials to environment variables or an Age/secret manager vault.
    codeknife sec: FAIL (3 files, 1 errors, 0 warnings, 0 info, 3ms)

    The agent format puts one finding on each line, which is easier to grep and cheaper for an AI agent to read:

    Terminal window
    codeknife slop --format agent
    CODEKNIFE slop status=fail files=3 errors=0 warnings=5 infos=0 elapsed_ms=2
    [1] WARNING CK_SLOP_NO_TIMEOUT app/report.py:7-7 sym=- impact=60 | outbound HTTP request made without an explicit timeout | fix: Use a client configured with explicit connect/request timeouts or `AbortSignal.timeout(...)`.
    [2] WARNING CK_SLOP_NO_TIMEOUT web/client.ts:2-2 sym=- impact=60 | outbound HTTP request made without an explicit timeout | fix: Use a client configured with explicit connect/request timeouts or `AbortSignal.timeout(...)`.
    [3] WARNING CK_SLOP_TYPE_ESCAPE src/main.rs:2-2 sym=- impact=55 | panic-prone `unwrap` in production code | fix: Propagate `Result`/`Option` cleanly with `?` or `.ok_or(...)` instead of panicking.
    [4] WARNING CK_SLOP_TYPE_ESCAPE web/client.ts:3-3 sym=- impact=55 | TypeScript type-safety escape (`(await res.json()) as any`) | fix: Parse/validate external data at the boundary instead of casting through `any` or `unknown`.
    [5] WARNING CK_SLOP_DEBUG_LEFTOVER src/main.rs:3-3 sym=- impact=45 | leftover debug macro/breakpoint in production code | fix: Remove debugging calls before committing.

    Some blades can repair what they find. dead --fix previews the removal of strictly private dead functions:

    Terminal window
    codeknife dead --fix --format human
    warning[CK_DEAD_SYMBOL]: unreachable internal symbol `old_total`
    --> src/main.rs:7-9 (ledger::old_total)
    = fix: Delete this unused function (or run `codeknife dead --fix --write`).
    warning[CK_DEAD_FILE]: orphan file `app/report.py` has no inbound imports or reachable symbols
    --> app/report.py:1-8 (app/report.py)
    = fix: Delete this unreferenced file or wire its module into an entrypoint.
    warning[CK_DEAD_FILE]: orphan file `web/client.ts` has no inbound imports or reachable symbols
    --> web/client.ts:1-5 (web/client.ts)
    = fix: Delete this unreferenced file or wire its module into an entrypoint.
    note: dry-run: 1 strictly private dead symbol(s) can be removed with `--fix --write`
    codeknife dead: FAIL (3 files, 0 errors, 3 warnings, 0 info, 2ms)

    All 13 blades are described under Blades, starting with 1. Comments.

  6. See the health score and the work queue.

    Terminal window
    codeknife score --format human
    CodeKnife Health Scorecard: 88.3/100 (Grade B+) | trend ▇ (n/a)
    Files: 3 | Code lines: 25 | Errors: 1 | Warnings: 9 | Info: 1
    Architecture (25%) 100.0/100 [0 finding(s)]
    Security (25%) 78.2/100 [1 finding(s)]
    Reliability (15%) 73.0/100 [5 finding(s)]
    Dead & Deps (10%) 83.8/100 [3 finding(s)]
    Testing (15%) 100.0/100 [0 finding(s)]
    Docs & Churn (10%) 94.1/100 [2 finding(s)]

    score always exits with code 0. See Health Scorecard.

    plan ranks the error and warning findings into an ordered to-do list. Info findings are left out, and the default is the top 10 (--top).

    Terminal window
    codeknife plan --format agent
    CODEKNIFE plan status=pass files=3 errors=1 warnings=9 infos=0 elapsed_ms=8
    [1] ERROR CK_SEC_HARDCODED_SECRET app/report.py:3-3 sym=- impact=100 | potential hardcoded secret (database connection URL with embedded credentials) in source code | fix: Move credentials to environment variables or an Age/secret manager vault.
    [2] WARNING CK_DEAD_FILE app/report.py:1-8 sym=app/report.py impact=66 | orphan file `app/report.py` has no inbound imports or reachable symbols | fix: Delete this unreferenced file or wire its module into an entrypoint.
    ...
    [10] WARNING CK_DOC_DEAD_PATH README.md:3-3 sym=docs/setup.md impact=45 | broken markdown link target `docs/setup.md` | fix: Update or remove the broken path reference in documentation.
    NOTE no coverage file found (running complexity-only mode); generate one with: cargo llvm-cov --lcov --output-path lcov.info | npx vitest run --coverage | pytest --cov --cov-report=xml:coverage.xml
    NOTE analyzed 2 commit(s) over the last 90 day(s)
    NOTE showing top 10 prioritized work order(s) ranked by impact x churn + CRAP

    The impact values differ from check because plan re-ranks findings. plan always exits with code 0, and its status is always pass. See Work Orders (plan).

  7. Preview the automatic fixes.

    codeknife fix is a dry run unless you pass --write:

    Terminal window
    codeknife fix --format human
    codeknife fix (dry-run): planned 3 patch(es) across 1 file(s), 0 reverted (1 pass(es))
    [FIX] CK_CMT_STRIP | Remove 1 strippable comment(s) in src/main.rs
    [FIX] CK_DEAD_SYMBOL | Remove unreachable private function in src/main.rs
    [FIX] CK_SLOP_DEBUG_LEFTOVER | Remove debug leftover at src/main.rs:3

    The timeout findings are not in this list because that fixer only runs with --aggressive. With --aggressive, the same dry run plans 5 patches:

    Terminal window
    codeknife fix --aggressive --format human
    codeknife fix (dry-run): planned 5 patch(es) across 3 file(s), 0 reverted (1 pass(es))
    [FIX] CK_CMT_STRIP | Remove 1 strippable comment(s) in src/main.rs
    [FIX] CK_DEAD_SYMBOL | Remove unreachable private function in src/main.rs
    [FIX] CK_SLOP_NO_TIMEOUT | Inject explicit HTTP timeout at app/report.py:7
    [FIX] CK_SLOP_DEBUG_LEFTOVER | Remove debug leftover at src/main.rs:3
    [FIX] CK_SLOP_NO_TIMEOUT | Inject explicit HTTP timeout at web/client.ts:2

    This walkthrough keeps to the default set.

  8. Apply the fixes.

    Terminal window
    codeknife fix --write --format human
    codeknife fix (write): applied 3 patch(es) across 1 file(s), 0 reverted (1 pass(es))
    [FIX] CK_CMT_STRIP | Remove 1 strippable comment(s) in src/main.rs
    [FIX] CK_DEAD_SYMBOL | Remove unreachable private function in src/main.rs
    [FIX] CK_SLOP_DEBUG_LEFTOVER | Remove debug leftover at src/main.rs:3

    git diff shows the edits:

    diff --git a/src/main.rs b/src/main.rs
    index 509a98c..2d8affd 100644
    --- a/src/main.rs
    +++ b/src/main.rs
    @@ -1,17 +1,11 @@
    fn parse_amount(raw: &str) -> i64 {
    let value: i64 = raw.trim().parse().unwrap();
    - dbg!(value);
    value
    }
    -fn old_total(values: &[i64]) -> i64 {
    - values.iter().sum()
    -}
    -
    fn main() {
    let mut total = 0;
    for raw in ["10", "32"] {
    - // add the amount to the total
    total += parse_amount(raw);
    }
    println!("{total}");

    The patched program still builds and prints 42. See Autofix Engine for --verify, --aggressive and --toolchains.

  9. Fix the secret by hand and check again.

    No fixer handles secrets. Change app/report.py to read the URL from the environment:

    import os
    import requests
    DATABASE_URL = os.environ["DATABASE_URL"]
    def fetch_report(url):
    response = requests.get(url)
    return response.json()
    Terminal window
    codeknife check --format human
    warning[CK_DEAD_FILE]: orphan file `app/report.py` has no inbound imports or reachable symbols
    --> app/report.py:1-10 (app/report.py)
    = fix: Delete this unreferenced file or wire its module into an entrypoint.
    ...
    warning[CK_DOC_DEAD_PATH]: broken markdown link target `docs/setup.md`
    --> README.md:3 (docs/setup.md)
    = fix: Update or remove the broken path reference in documentation.
    note: no coverage file found (running complexity-only mode); generate one with: cargo llvm-cov --lcov --output-path lcov.info | npx vitest run --coverage | pytest --cov --cov-report=xml:coverage.xml
    note: analyzed 2 commit(s) over the last 90 day(s)
    codeknife check: FAIL (3 files, 0 errors, 7 warnings, 0 info, 6ms)

    The error is gone. Seven warnings remain, and the exit code is still 1. Commit the work so far:

    Terminal window
    git add app src && git commit -m "apply fixes, read DB URL from env"
  10. Accept the remaining findings into the baseline.

    When you adopt CodeKnife on an existing codebase, you usually cannot fix everything at once. baseline update records the current findings so that check only fails on new ones:

    Terminal window
    codeknife baseline update
    updated /path/to/ledger/codeknife.baseline (7 entries)

    The baseline is a plain text file, one entry per line:

    # codeknife baseline v1: shrink-only violation ratchet. Prune with `codeknife baseline prune`.
    CK_DEAD_FILE app/report.py app/report.py 1
    CK_DEAD_FILE web/client.ts web/client.ts 1
    CK_DOC_DEAD_PATH README.md docs/setup.md 1
    CK_SLOP_NO_TIMEOUT app/report.py h:42bc78d5e4d7 1
    CK_SLOP_NO_TIMEOUT web/client.ts h:42bc78d5e4d7 1
    CK_SLOP_TYPE_ESCAPE src/main.rs h:66fa81ab1ff6 1
    CK_SLOP_TYPE_ESCAPE web/client.ts h:9f744db6ae73 1
  11. Run check again, and see the guard at work.

    Terminal window
    codeknife check --format human
    loosening warnings:
    ! codeknife.baseline: added new entry `CK_DEAD_FILE app/report.py app/report.py` (1)
    ! codeknife.baseline: added new entry `CK_DEAD_FILE web/client.ts web/client.ts` (1)
    ! codeknife.baseline: added new entry `CK_DOC_DEAD_PATH README.md docs/setup.md` (1)
    ! codeknife.baseline: added new entry `CK_SLOP_NO_TIMEOUT app/report.py h:42bc78d5e4d7` (1)
    ! codeknife.baseline: added new entry `CK_SLOP_NO_TIMEOUT web/client.ts h:42bc78d5e4d7` (1)
    ! codeknife.baseline: added new entry `CK_SLOP_TYPE_ESCAPE src/main.rs h:66fa81ab1ff6` (1)
    ! codeknife.baseline: added new entry `CK_SLOP_TYPE_ESCAPE web/client.ts h:9f744db6ae73` (1)
    note: no coverage file found (running complexity-only mode); generate one with: cargo llvm-cov --lcov --output-path lcov.info | npx vitest run --coverage | pytest --cov --cov-report=xml:coverage.xml
    note: analyzed 3 commit(s) over the last 90 day(s)
    note: 7 finding(s) suppressed by codeknife.baseline
    codeknife check: FAIL (3 files, 0 errors, 0 warnings, 0 info, 7ms)

    All seven findings are now suppressed, but check still fails. The loosening guard compares the baseline in your working tree with the one in HEAD (or in the ref you pass with --since) and reports every new entry. Growing the baseline is a deliberate act, so it has to go into a commit:

    Terminal window
    git add codeknife.baseline && git commit -m "accept remaining findings into baseline"
    codeknife check --format human
    note: no coverage file found (running complexity-only mode); generate one with: cargo llvm-cov --lcov --output-path lcov.info | npx vitest run --coverage | pytest --cov --cov-report=xml:coverage.xml
    note: analyzed 4 commit(s) over the last 90 day(s)
    note: 7 finding(s) suppressed by codeknife.baseline
    codeknife check: PASS (3 files, 0 errors, 0 warnings, 0 info, 5ms)

    The exit code is 0. From now on, any new finding fails the gate, while the seven recorded ones do not.

  12. Score the repository again.

    Terminal window
    codeknife score --format human
    CodeKnife Health Scorecard: 100.0/100 (Grade A+) | trend █ (n/a)
    Files: 3 | Code lines: 22 | Errors: 0 | Warnings: 0 | Info: 0
    Architecture (25%) 100.0/100 [0 finding(s)]
    Security (25%) 100.0/100 [0 finding(s)]
    Reliability (15%) 100.0/100 [0 finding(s)]
    Dead & Deps (10%) 100.0/100 [0 finding(s)]
    Testing (15%) 100.0/100 [0 finding(s)]
    Docs & Churn (10%) 100.0/100 [0 finding(s)]

    score and plan both skip findings recorded in the baseline, so the score is 100 and plan would now list no work orders. The baselined findings still exist in the code and are listed in codeknife.baseline.