Skip to main content

Multi-language code complexity analyzer with AI cost metrics (AIRD, AICP), SARIF/JSON/CSV output, and CI threshold enforcement

Project description

Knots

A fast multi-language code complexity analyzer built on tree-sitter. Knots measures traditional complexity metrics alongside two AI-specific cost scores — AIRD (AI Reasoning Difficulty) and AICP (AI Context Pressure) — to identify which functions are genuinely expensive to modify with AI assistance.

Features

  • Multiple Complexity Metrics: McCabe, Cognitive, Nesting Depth, SLOC, ABC, Test Scoring
  • AI Cost Metrics: AIRD (reasoning difficulty) and AICP (context pressure) — corpus-validated against 32,205 functions across 6 open-source C codebases
  • Multi-Language: C, C++, Rust, Python, JavaScript, TypeScript, Ada, Go, Java, C#, Kotlin, Swift, PHP, Fortran, Scala, and Lua — same metrics and thresholds across all supported languages
  • Testability Matrix: Categorize functions by complexity and testability
  • Multiple Output Formats: text, SARIF, JSON, NDJSON (find/xargs-composable), CSV
  • CI Threshold Enforcement: exit 1 on any threshold violation; recommended --aird-threshold 85
  • Pre-commit Hook: native integration, no shim scripts required
  • Validated: McCabe matches pmccabe exactly; Cognitive matches Mozilla rust-code-analysis at 1.004× mean ratio (11,365 Rust functions)

Installation

Prebuilt binary from PyPI (no Rust toolchain, installs in seconds):

pipx install knots          # or: uv tool install knots

From crates.io:

cargo install knots

Or from source:

git clone https://github.com/brandon-arrendondo/knots.git
cd knots
cargo build --release

No C compiler or build system required.

Quick Start

# Single file
knots src/main.c

# Recursive directory
knots -r src/

# CI gate — fail if any function has AIRD > 85
knots -r src/ --aird-threshold 85

# Adopt the gate on a legacy codebase: snapshot today, then fail only on regressions
knots -r src/ --aird-threshold 85 --baseline .knots-baseline.json --write-baseline
knots -r src/ --aird-threshold 85 --baseline .knots-baseline.json

# Gate only the functions you actually touched (no new debt in this change)
knots -r src/ --aird-threshold 85 --changed

# SARIF for GitHub Code Scanning
knots -r --format sarif src/ > knots.sarif

# Corpus analysis — one JSON record per function
find . -name "*.c" -o -name "*.rs" | xargs knots --format ndjson > metrics.ndjson

# Testability matrix
knots -m src/main.c

Complexity Indicators

Based on max(McCabe, cognitive):

Range Indicator Meaning
1–10 😊 Good Low complexity, easy to maintain
11–20 😐 Okay Moderate complexity, monitor
21–49 😠 Bad High complexity, consider refactoring
50+ 😢 Critical Urgent refactoring needed

Command-Line Options

knots [OPTIONS] [FILE]...
knots [OPTIONS] --compile-commands <FILE>

Options:
  -r, --recursive                   Recursively process all supported source files in directories
  -v, --verbose                     Show detailed per-function analysis
  -m, --matrix                      Show testability matrix categorization
  --compile-commands <FILE>         Use compile_commands.json to get file list
  --include <FILE>                  Include filter rules from JSON file (whitelist)
  --exclude <FILE>                  Exclude filter rules from JSON file (blacklist)
  --exclude-path <PATTERN>          Exclude files whose path matches this regex (repeatable)
  --format <FORMAT>                 text (default) | sarif | json | ndjson | csv
  --mccabe-threshold <N>            Exit 1 if any function exceeds this McCabe complexity
  --cognitive-threshold <N>         Exit 1 if any function exceeds this cognitive complexity
  --nesting-threshold <N>           Exit 1 if any function exceeds this nesting depth
  --sloc-threshold <N>              Exit 1 if any function exceeds this SLOC count
  --abc-threshold <F>               Exit 1 if any function exceeds this ABC magnitude
  --return-threshold <N>            Exit 1 if any function exceeds this return count
  --aird-threshold <N>              Exit 1 if any function exceeds this AIRD (AI Reasoning Difficulty) score (recommended: 85)
  --aicp-threshold <N>              Exit 1 if any function exceeds this AICP (AI Context Pressure) score
  --external-calls-threshold <N>    Exit 1 if any function exceeds this external call count
  --report <FILE>                   Write a detailed per-function report to this file (opt-in)
  --baseline <FILE>                 Ratchet mode: gate only on regressions vs. this snapshot (see docs/baseline.rst)
  --write-baseline                  Snapshot current scores to --baseline and exit without gating
  --since <REF>                     Gate only functions overlapping lines changed since this git ref
  --changed                         Gate only functions changed in the working tree (sugar for --since HEAD)
  --explain <METRIC>                Explain a metric (e.g. aird, aicp) and how to lower it, then exit
  -j, --jobs <N>                    Parallel analysis threads (0 = auto-detect, 1 = sequential, default: 0)
  -h, --help                        Print help
  -V, --version                     Print version

Documentation

Full documentation is in the docs/ directory (Sphinx/RST):

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

knots-1.13.0.tar.gz (164.7 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

knots-1.13.0-py3-none-win_amd64.whl (4.5 MB view details)

Uploaded Python 3Windows x86-64

knots-1.13.0-py3-none-musllinux_1_2_x86_64.whl (5.0 MB view details)

Uploaded Python 3musllinux: musl 1.2+ x86-64

knots-1.13.0-py3-none-musllinux_1_2_aarch64.whl (4.9 MB view details)

Uploaded Python 3musllinux: musl 1.2+ ARM64

knots-1.13.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.9 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ x86-64

knots-1.13.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (5.0 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ ARM64

knots-1.13.0-py3-none-macosx_11_0_arm64.whl (5.0 MB view details)

Uploaded Python 3macOS 11.0+ ARM64

knots-1.13.0-py3-none-macosx_10_12_x86_64.whl (4.8 MB view details)

Uploaded Python 3macOS 10.12+ x86-64

File details

Details for the file knots-1.13.0.tar.gz.

File metadata

  • Download URL: knots-1.13.0.tar.gz
  • Upload date:
  • Size: 164.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for knots-1.13.0.tar.gz
Algorithm Hash digest
SHA256 edd7943bfcb1ca20ebe15b900afe4a323aa558c61da1e0fec3acc563076913a9
MD5 48ca8984ce8717a0486fc16168173aee
BLAKE2b-256 aafce3bdaf2eae8b71ac06343d35d7615b19d5c22b60abc2d75deb438b584ba5

See more details on using hashes here.

Provenance

The following attestation bundles were made for knots-1.13.0.tar.gz:

Publisher: wheels.yml on brandon-arrendondo/knots

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file knots-1.13.0-py3-none-win_amd64.whl.

File metadata

  • Download URL: knots-1.13.0-py3-none-win_amd64.whl
  • Upload date:
  • Size: 4.5 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for knots-1.13.0-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 634ada084c200919586f89638179182d01049e5dfaaec3328848b1ebea6bdf8e
MD5 742c6c33c1ce35eb9ef176588a7495b8
BLAKE2b-256 c973f423475d9efa3f3acc5c43236ed8c965bad94b013fa2be5dddab68c632f9

See more details on using hashes here.

Provenance

The following attestation bundles were made for knots-1.13.0-py3-none-win_amd64.whl:

Publisher: wheels.yml on brandon-arrendondo/knots

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file knots-1.13.0-py3-none-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for knots-1.13.0-py3-none-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 a0245700d20ff9752df9b161eceedfeafc7cd405debf1422eb456eed7ebcb915
MD5 658ae45ef23702810e5f0ddb33cec27d
BLAKE2b-256 6c7c01568e661ed73a1fa6e13d7d0d16e9ce58e72e2b9c84de299e9bb03620e9

See more details on using hashes here.

Provenance

The following attestation bundles were made for knots-1.13.0-py3-none-musllinux_1_2_x86_64.whl:

Publisher: wheels.yml on brandon-arrendondo/knots

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file knots-1.13.0-py3-none-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for knots-1.13.0-py3-none-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 a7dfc23974156107bd19bbaceaa372c965416ce2b9fc52bf43a135d79183af5a
MD5 540b5f3b3a7f00ba981c65a1a8e1247e
BLAKE2b-256 f01df615335717d7018447239e05ea20d6a32d3c74cd43aa7ac2b4f0f6545512

See more details on using hashes here.

Provenance

The following attestation bundles were made for knots-1.13.0-py3-none-musllinux_1_2_aarch64.whl:

Publisher: wheels.yml on brandon-arrendondo/knots

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file knots-1.13.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for knots-1.13.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8141e112da7bbdbd74121c926e93a2c69a779eda1d19e9b585dc98878a4aca4c
MD5 297851a6d77e257a15151e35b16adb27
BLAKE2b-256 f8378f4c9201244176cbe5b7fc39bde34f69067e0aba5d2bf565d285de4c30c2

See more details on using hashes here.

Provenance

The following attestation bundles were made for knots-1.13.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: wheels.yml on brandon-arrendondo/knots

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file knots-1.13.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for knots-1.13.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 c1af85dfece3257eb0014b8896e25f3aef5e5eb0a745bf65521175fb8bd9371f
MD5 3ff0b805cf5131274ed75fe2935c2a6a
BLAKE2b-256 e28ac9663a3c54cd565d4dec8c00bceebb3df6d8070c44a98f9c2782af538c95

See more details on using hashes here.

Provenance

The following attestation bundles were made for knots-1.13.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: wheels.yml on brandon-arrendondo/knots

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file knots-1.13.0-py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for knots-1.13.0-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c5f4290ea45c140d05b1ab707078dcf09542d12fd312997acbaf85d7e2b83215
MD5 96f630f79a307707d611db1e1c75a9f0
BLAKE2b-256 3a1028c39a62b32ac013d4f0e7f210b4c43b1d2946131a92d241a501b5c5e5b5

See more details on using hashes here.

Provenance

The following attestation bundles were made for knots-1.13.0-py3-none-macosx_11_0_arm64.whl:

Publisher: wheels.yml on brandon-arrendondo/knots

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file knots-1.13.0-py3-none-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for knots-1.13.0-py3-none-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 b9cc6fe00ab29040c2980bbe2632fba787006b0a5960a419081af0bf1f21c54a
MD5 de6f668af4fdd77e0c3bf5900de00252
BLAKE2b-256 8c57b106e0044e89e3e0231a5b0e39c48ba559924b941b7acd1a8ed1bbf59d9c

See more details on using hashes here.

Provenance

The following attestation bundles were made for knots-1.13.0-py3-none-macosx_10_12_x86_64.whl:

Publisher: wheels.yml on brandon-arrendondo/knots

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page