Agent-first guardrails framework for AI-assisted Python projects
Project description
kragg
kragg is an opinionated guardrails framework for AI-assisted Python projects.
It is agent-first: coding agents (Claude Code, Codex, Cursor, Gemini CLI, …)
are the primary users, running check → fix loops. kragg gives them one command,
structured results, meaningful exit codes, and copy-pasteable fixes.
Install modes
Dev dependency (recommended). kragg runs inside the project environment, so every tool sees the project's packages:
uv add --dev kragg
uv run kragg check
Global tool (pipx / uv tool). kragg runs from its own environment, but environment-dependent gates (pytest, mypy, pip-audit, deptry) are always executed on the project's interpreter, resolved in this order:
KRAGG_PROJECT_PYTHONenvironment variable (explicit override)<project>/.venv- an active
VIRTUAL_ENV(ignored when it is kragg's own environment) uv run --project <project>as a fallback
If no project environment exists, those gates fail loudly with the exact fix
(uv sync, uv add --dev mypy, …) instead of silently running in the wrong
environment. kragg doctor shows which interpreter was resolved and which
tools are missing.
Commands
kragg new my-app --kind cli # layered skeleton (cli | api | worker) + uv sync
kragg gen module payments # service/domain/test slots in the layout
kragg init . # add guardrails to an existing project
kragg check # all gates, consolidated report
kragg check --changed # only files changed in git (cheap inner loop)
kragg check --since main # changed vs merge-base with a ref
kragg check --file src/foo.py --file src/bar.py
kragg check --format json # stable machine-readable schema
kragg fix # auto-fix formatting and safe lint
kragg map # public symbol inventory (what already exists)
kragg spec # test suite rendered as a readable spec tree
kragg coverage # uncovered lines in critical functions (ranked)
kragg mutation # mutation-test changed critical files (cosmic-ray)
kragg mutation --all # mutation-test every critical file
kragg mutation --update-baseline # accept current survivors (equivalent mutants)
kragg brief # reviewable digest of the change set
kragg security
kragg audit
kragg criticality --write # call-graph risk -> CRITICALITY.md + .kragg/
kragg status # what failed last run, without re-running
kragg flaky # gates that flipped pass/fail on an unchanged commit
kragg flaky --rerun 20 # re-run the suite N times, rank tests by failure ratio
kragg hook claude # harness hook adapter (reads hook JSON on stdin)
kragg doctor # environment diagnostics with exact fixes
kragg policy show
Gates
Fast (always all run): ruff, mypy, typing-strictness (the project's mypy
config actually meets the strict floor — no missing/loosened flags, no
ignore_errors, no bare # type: ignore; makes strict-ai-python a verified
contract instead of a label), radon-cc, radon-mi, halstead, type-complexity,
boundaries (layered import contract from [tool.kragg] layers),
structure (file/symbol budgets), nullable-default (arithmetic on
.get(key, default) results, which crash when the key is present-but-null),
critical-tests (critical functions cannot change without test changes),
test-quality (no assertion-free tests; critical functions must be
referenced by tests), bandit, detect-secrets.
Slow (skipped while fast gates fail): pytest+coverage, critical-coverage
(public critical functions must have no uncovered lines), pip-audit.
Test depth
Green checkmarks are easy to fake, so kragg looks past them with layered, mostly-deterministic signals — each a harder-to-game answer to "do the tests actually defend behavior":
- what's run —
kragg coveragesurfaces uncovered lines in critical functions, ranked by fan-in, instead of a gameable global percentage; thecritical-coveragegate fails on any uncovered line in a critical function. - what's defended —
kragg mutationruns cosmic-ray over the changed critical files (the criticality graph and git diff keep it cheap) and reports surviving mutants asfile:linefixes. Accept equivalent mutants withkragg mutation --update-baseline. - what's claimed —
kragg specrenders test names and docstrings as a documentation tree and flags critical functions that have only example-based tests (property-based tests, via Hypothesis, kill more mutants). - what's trustworthy —
kragg flakymines the run journal for gates that flipped on an unchanged commit;--rerun Nre-runs the suite and ranks tests by failure ratio (for cron/CI, never the inner loop).
Mutation and active flaky runs are deliberately outside kragg check: they are
on-demand or CI surfaces, not inner-loop gates.
kragg mutation runs cosmic-ray on the project interpreter, so add it to the
project being checked (uv add --dev cosmic-ray); kragg prints the exact
command if it is missing. Property-based tests use Hypothesis
(uv add --dev hypothesis). Mutation needs an editable/source install so it
mutates the code the tests import.
Agent-native design
Agents drift where they have freedom, so the scaffold removes the freedom:
every kind ships one layered layout (entrypoints/ → services/ →
domain/) with the boundaries gate enforcing dependency direction from
the first commit, and kragg gen module creates new code in the one place
it belongs. The tool holds the memory the agent lacks: kragg map is the
inventory of what exists (injected at session start so nothing gets
reinvented), .kragg/history.jsonl remembers runs, CRITICALITY.md
remembers risk, and kragg brief renders the change set legible to the
human reviewer.
Agent loop design
kragg checkruns all static gates and reports every failure in one run (one loop iteration instead of one per gate). Slow gates (pytest, pip-audit) are skipped while static gates fail;--fail-fastand--alloverride.- Output is token-efficient: violations are deduplicated, capped per gate
(
--max-violations, policymax_violations_per_gate), and reported asfile:linepointers with fix hints instead of raw tool dumps. - Exit codes are branchable without parsing:
0pass,1gate failures,2usage error,3environment broken. - Every run appends a slim line to
.kragg/history.jsonl;kragg statusanswers "what failed last run" for a few tokens. - Scaffolding emits
AGENTS.mdas the canonical agent contract (read by Codex, Cursor, Gemini CLI, and Claude Code via aCLAUDE.mdpointer), plus hooks that runkragg check --changedafter edits.
Configuration
Configure kragg in pyproject.toml under [tool.kragg], or in a standalone
kragg.toml (top-level keys, takes precedence when present):
[tool.kragg]
profile = "strict-ai-python"
source_paths = ["src"]
test_paths = ["tests"]
coverage_fail_under = 80
type_max_nesting_depth = 2
type_max_length = 40
max_violations_per_gate = 25
max_file_lines = 500
max_public_symbols = 20
structure_exclude = [
# flat or generated files that legitimately exceed the budgets;
# document why each entry earns its exemption
"src/app/icons.py", # 4k lines of generated base64 icon data
"*_pb2.py", # protobuf-generated modules, any depth
]
structure_exclude exempts matching files from the structure gate's
file- and symbol-budgets only — they stay subject to every other gate, so
the cap stays meaningful repo-wide. Reach for it after splitting fails:
genuinely flat data, vendored code, or generated modules you can't annotate.
Patterns are repo-root-relative POSIX paths matched with fnmatch
(case-sensitive); * matches any characters including /, so
src/generated/* exempts that whole subtree — scope patterns narrowly.
V1 scope
kragg models agentic workflow support as policy packs, not as an LLM runtime.
A policy pack defines the gates, thresholds, generated agent instructions, and
project wrappers that keep AI-authored Python code inside known constraints.
The default policy pack is strict-ai-python.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kragg-0.5.0.tar.gz.
File metadata
- Download URL: kragg-0.5.0.tar.gz
- Upload date:
- Size: 182.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
88dfe3821ae1f4a333c1b460c3eb417cdcccd41a5a77174dc6713a33e9d82bf3
|
|
| MD5 |
8172e90804fdce3229002eb3405a2ba9
|
|
| BLAKE2b-256 |
b0ea25a4d4836dcd97cf5034537307ca40a8489f4819d4d0c187a12b33c3e9e2
|
Provenance
The following attestation bundles were made for kragg-0.5.0.tar.gz:
Publisher:
release.yml on tortastudios/kragg
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kragg-0.5.0.tar.gz -
Subject digest:
88dfe3821ae1f4a333c1b460c3eb417cdcccd41a5a77174dc6713a33e9d82bf3 - Sigstore transparency entry: 1955784340
- Sigstore integration time:
-
Permalink:
tortastudios/kragg@01228ff524925070283ceeca5b913e0d7d3796ef -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/tortastudios
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@01228ff524925070283ceeca5b913e0d7d3796ef -
Trigger Event:
release
-
Statement type:
File details
Details for the file kragg-0.5.0-py3-none-any.whl.
File metadata
- Download URL: kragg-0.5.0-py3-none-any.whl
- Upload date:
- Size: 70.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
417db74f9c95ce2192092c5e1888515681d741c90288d293ba78b49280b5493f
|
|
| MD5 |
4dcf93e4e217a7eaf0b989b2547187d6
|
|
| BLAKE2b-256 |
92b42384102b4f92a9196eec246a8f988e970ffe3ab2a34caf9459c6e64a00b3
|
Provenance
The following attestation bundles were made for kragg-0.5.0-py3-none-any.whl:
Publisher:
release.yml on tortastudios/kragg
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kragg-0.5.0-py3-none-any.whl -
Subject digest:
417db74f9c95ce2192092c5e1888515681d741c90288d293ba78b49280b5493f - Sigstore transparency entry: 1955784428
- Sigstore integration time:
-
Permalink:
tortastudios/kragg@01228ff524925070283ceeca5b913e0d7d3796ef -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/tortastudios
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@01228ff524925070283ceeca5b913e0d7d3796ef -
Trigger Event:
release
-
Statement type: