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.1.tar.gz (175.6 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.1-py3-none-win_amd64.whl (4.7 MB view details)

Uploaded Python 3Windows x86-64

knots-1.13.1-py3-none-musllinux_1_2_x86_64.whl (5.2 MB view details)

Uploaded Python 3musllinux: musl 1.2+ x86-64

knots-1.13.1-py3-none-musllinux_1_2_aarch64.whl (5.1 MB view details)

Uploaded Python 3musllinux: musl 1.2+ ARM64

knots-1.13.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.1 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ x86-64

knots-1.13.1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (5.2 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ ARM64

knots-1.13.1-py3-none-macosx_11_0_arm64.whl (5.1 MB view details)

Uploaded Python 3macOS 11.0+ ARM64

knots-1.13.1-py3-none-macosx_10_12_x86_64.whl (5.0 MB view details)

Uploaded Python 3macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: knots-1.13.1.tar.gz
  • Upload date:
  • Size: 175.6 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.1.tar.gz
Algorithm Hash digest
SHA256 a8304a4a87ee1ccc7b05d24b519e4438c297c77d7f0c2e31237994f38bffcc59
MD5 527708776ec894ff427731a466394a54
BLAKE2b-256 20e1c52af855aee3a5926c150e9548c8e48fe80c08efce449ee664447fde7679

See more details on using hashes here.

Provenance

The following attestation bundles were made for knots-1.13.1.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.1-py3-none-win_amd64.whl.

File metadata

  • Download URL: knots-1.13.1-py3-none-win_amd64.whl
  • Upload date:
  • Size: 4.7 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.1-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 f873bb0c160acdbcf7cc3a26997ce05de6f92d31ac375885794e4ba0275344b2
MD5 501da8260ffeb10202751a57245dce65
BLAKE2b-256 6d677911bca1ef534ef678986dc5acefaa9eab01f7bd88e5e367a80f89ae78a6

See more details on using hashes here.

Provenance

The following attestation bundles were made for knots-1.13.1-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.1-py3-none-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for knots-1.13.1-py3-none-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 f0032550d36ce0ccc3e5659d19cacef91439bb1ade92f4071c1c81c79912e339
MD5 8cc809ba0e2101d54abe8dde05db0a3d
BLAKE2b-256 88d1798390cfeee63ac0f586078487e290a53e921af3411230300a37cd409ea5

See more details on using hashes here.

Provenance

The following attestation bundles were made for knots-1.13.1-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.1-py3-none-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for knots-1.13.1-py3-none-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 9ab565a0e39d913d7c345943429b065a64b7cf40342bd0c95a12649e88106b50
MD5 05c47626d05fb24e6cf30d3864758047
BLAKE2b-256 24ea5049d74af4052b75b4ef57febf945e142a62310469cfaa9b34cca75ba94f

See more details on using hashes here.

Provenance

The following attestation bundles were made for knots-1.13.1-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.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for knots-1.13.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0adcb8c53a9897c8ec95a6b1a660cb5965fc863a8aaf1b5c3c9b38372fece13c
MD5 19b09b610d6fe4aeac78ad6185a9e3c1
BLAKE2b-256 1d4b8e84b6edd2565cf929e7a054ba1a38face191ec7881635ca60f0d54b9120

See more details on using hashes here.

Provenance

The following attestation bundles were made for knots-1.13.1-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.1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for knots-1.13.1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 173c0eeed7680619a89b9f034650345ba3255cd32653f55fb4627dc9ff77a2f7
MD5 f26892b8a2b3afb16658f5495554e678
BLAKE2b-256 97bc0f341d969f7eaeb1aa98edcf32d3a68f3b70445936919678b30f99e723a6

See more details on using hashes here.

Provenance

The following attestation bundles were made for knots-1.13.1-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.1-py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for knots-1.13.1-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c1cc32593405fe1411e4fc0ab0f5e87146813875e0c6774854a1d11b61a8ee0f
MD5 4c8c8cca5809b761e492891a38817ffe
BLAKE2b-256 0f31ee1ff2ae9447a21a41648f8872e915acbf8a771aac6162993a9dc3051572

See more details on using hashes here.

Provenance

The following attestation bundles were made for knots-1.13.1-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.1-py3-none-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for knots-1.13.1-py3-none-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 5efbf6846d3a2e9a016153e0cca6a109a293320a39aa34b584d4b89e2fab1f06
MD5 461bab3c2194b82765b9e34c137f4d57
BLAKE2b-256 375de1816bcc5297c1bfeb79104b0be882886af80cb3954a05337da5762551c8

See more details on using hashes here.

Provenance

The following attestation bundles were made for knots-1.13.1-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