trace2rules
Mine and audit coding-agent sessions into evidence-backed AGENTS.md rules.
trace2rules is a local-first CLI. It ingests agent trajectories, mines recurring friction, audits existing AGENTS.md/CLAUDE.md rules against real sessions, and identifies whether each rule is observed, compliant, violated, unobserved, or already enforced by CI/pre-commit.
Status: beta. The output is a proposal, not an automatically applied AGENTS.md.
Quick start
Paste this into your coding agent to audit the current checkout:
Install trace2rules if missing (`uv tool install trace2rules`). Audit this repo's AGENTS.md and CLAUDE.md against my local sessions with `trace2rules audit`. Report unobserved, violated, and unenforced rules, and propose existing CI/pre-commit checks without duplicating configured enforcement.
Or run the audit manually:
trace2rules audit
trace2rules audit --prompt > audit.txt
Paste this into your coding agent to mine your local sessions:
Install trace2rules if missing (`uv tool install trace2rules`). Run `trace2rules run --out-dir reports/` for my local agent sessions. Read the report files and synthesize repo-specific AGENTS.md rules yourself. Keep only rules supported by the evidence.
Or run it manually:
uv tool install trace2rules
trace2rules run --out-dir reports/
An existing AGENTS.md or CLAUDE.md in a session’s cwd is discovered automatically and included in that repo’s prompt. Use --agents PATH only to override discovery from a different checkout.
Then give reports/*.md to your own coding agent to synthesize the rules. run ingests local sessions automatically when the database is empty. trace2rules synthesize --report reports/acme__repo.md is an optional thin wrapper for the same prompt.
Examples
Auditing a real AGENTS.md
On the SWE-rebench swc-project/swc split (188 sessions), the current AGENTS.md audit finds 66 rules: 9 measured, 1 partial, and 56 unmeasured.
| Rule | Coverage | Followed | Violations | Confidence | Enforcement |
|---|---|---|---|---|---|
| Read the codebase before editing | measured | 121 | 64/185 (35%) | high | guidance only |
| Verify assumptions against source and tests | measured | 121 | 64/185 (35%) | high | guidance only |
git commit only after git add |
measured | 8 | 6/14 (43%) | low | guidance only |
| Submodule update before tests | measured | 0 | 32/32 (100%) | medium | guidance only |
Snapshot updates with UPDATE=1 cargo test |
measured | 11 | 147/171 (86%) | high | missing |
cargo fmt --all |
measured | 0 | 185/185 (100%) | high | CI configured |
cargo clippy --all --all-targets -- -D warnings |
measured | 0 | 185/185 (100%) | high | CI configured |
cargo test -p <crate> |
measured | 5 | 158/171 (92%) | high | missing |
--no-verify guard |
partial | 13 | 1/14 (7%) | low | missing |
configured means the command appears in .github/workflows/CI.yml; it does not prove the check is required by branch protection. available means a Makefile/package target exists but is not wired into CI. missing means no mechanism was found.
Rules synthesized from report prompts
swc-project/swc
- [guidance] Reproduce before editing crate sources: 118 sessions edited source with no preceding failing exec. Start with a targeted test such as
RUSTUP_TOOLCHAIN=1.84.1 cargo test -p swc_ecma_transforms_compat --lib regex_dot_all --offline. - [guidance] Read the exact target range before editing: 72 sessions made 132 edits with no prior read, e.g. inspect the
ExprExtsites incrates/swc_ecma_utils/src/lib.rs. - [enforceable] Require validation after edits: 26 sessions had no test/lint exec after the first edit and 74 of 188 sessions were unvalidated. Add a CI job running
RUSTUP_TOOLCHAIN=1.84.1 cargo test -p <changed-crate> --offline; the pinned toolchain was the successful delta in 21 sessions.
rkkautsar/trace2rules
- [enforceable] Make
uv run pytest -qa required check in.github/workflows/ci.yml. - [enforceable] Keep dependencies declared in
pyproject.tomland install withuv sync --frozen;environment_thrashfound 25 package-manager calls across 3 sessions, 4 failed. - [guidance] Read the owning package module by symbol or range, not whole-file: the synthesis and ingest modules were read-only hotspots in 22/62 and 20/62 sessions.
rkkautsar/louibot
- [enforceable] Gate changes with
npm run format:check && npm run build && npm test, matching.github/workflows/ci.yml; 3 sessions used each validation command. - [guidance] Read
src/agent.tsand its matching test before editing: 133 edits had no prior read across 6 sessions, andsrc/agent.tshad 227 reads across 9 sessions. - [enforceable] Block
git push --force-with-leaseoncodex/*branches andrm -rf .louibot/benchmarks/runs/<run>; 3 destructive/gate-bypassing invocations occurred across 3 sessions.
rkkautsar/personal-website
- [enforceable] Run
npm run lint && npm run format:check, plusnpm run check:workerforfunctions/**andnpm run test:discussionforfunctions/api/discussion/[slug].ts; 3 sessions made edits with no test/lint exec after the first edit. - [enforceable] Reproduce API/worker failures before editing source: 3 sessions edited source with no preceding failing exec; extend
npm run test:discussionfirst. - [guidance] Read
src/components/sections/WritingSection.tsx,src/index.css, andsrc/blog/client.tsbefore editing; 117 edits across 3 sessions had no prior read of the target.
Scope
trace2rules proposes durable, normative AGENTS.md rules, not memories.
It targets two destinations:
- Enforceable rules: a CI check, pre-commit hook, lint rule, or test can observe and enforce them.
- Stable AGENTS rules: durable repo or global policy that future agents should follow even though it is not mechanically checkable.
It deliberately does not mine memories, historical decisions, ownership, dates, task state, or "what happened" summaries. Those are contextual and belong in a memory system, not in AGENTS.md. If evidence is descriptive rather than prescriptive, the tool should omit it.
How it works
- Ingest normalizes tool calls into DuckDB (
sessions,tool_events,feedback_events,test_results). - Mining finds hotspots, recovery loops, validation gaps, edit rejections, blind edits, environment thrash, destructive commands, generated-path edits, dependency drift, and correction loops.
- Sequence mining normalizes commands and paths, anchors patterns at errors, allows small gaps, and reports support/failure context. Exact sequences use lift and paired-rework evidence; windows never cross a compaction boundary.
- Report selects bounded findings and writes one readable Markdown evidence prompt per repo. The optional
synthesize --reportwrapper adds a JSON output contract and sends the prompt to an LLM; it does not re-mine. - Audit checks existing
AGENTS.mdrules against sessions.holdoutsplits sessions by time and audits the same rules on an earlier train slice and later holdout.
Generated verifier scripts and low-support task-specific sequences are filtered before synthesis.
Commands
| Command | Purpose |
|---|---|
ingest |
Load sessions and tool events into DuckDB |
mine |
Show friction findings and failure signals |
report |
Emit findings and evidence as JSON or a Markdown report prompt |
run |
Ingest if needed, then write one evidence prompt per detected repo |
doctor |
Check install, Node, credentials, source stores, and database schema |
patterns |
Mine error-anchored, normalized attribute patterns |
exact |
Show recurring normalized sequences with evidence |
synthesize |
Add a JSON output contract to a report prompt and send it to an LLM |
audit |
Audit existing AGENTS.md rules against sessions and suggest enforcement |
holdout |
Audit rules on an earlier train split and a later time holdout |
Useful options:
trace2rules ingest # ~, all detected local stores
trace2rules ingest --format swe-rebench # opt into the public dataset
trace2rules ingest --source PATH --limit 200
trace2rules synthesize --report reports/acme__repo.md
trace2rules synthesize --report reports/acme__repo.md --json
trace2rules exact --length 4 --per-repo 2
trace2rules patterns --top 10
trace2rules audit --agents AGENTS.md
trace2rules audit --agents AGENTS.md --commit HEAD --json
trace2rules audit --agents AGENTS.md --prompt
trace2rules holdout --agents AGENTS.md --fraction 0.3
trace2rules report --prompt --min-support 5
trace2rules report --prompt --max-rules 5
trace2rules run --out-dir reports/ --max-rules 10
Local sessions
--source is repeatable and defaults to your home directory. It scans:
~/.claude/projects~/.codex/sessions~/.pi/agent/sessions~/.omp/agent/sessions~/.local/share/opencode
The default cap is 200 sessions per source and selects the most recently modified files; use --limit 0 for all. OpenCode is read directly from its SQLite database; JSON exports are still accepted:
uv run trace2rules ingest --format agent-trajectory --source ~/.local/share/opencode
The repository label is inferred from the git remote (owner/name) when available, falling back to the working-directory basename. report --prompt emits one prompt per detected repo:
trace2rules report --prompt --out-dir reports/
run and mine auto-ingest local sessions when the database is empty. ingest writes to $TMPDIR/trace2rules/trace2rules.duckdb by default. Set TRACE2RULES_DB to use a persistent path.
Install
uv tool install trace2rules
# or
pip install trace2rules
For development from a clone:
uv sync
agent-trajectory calls its bundled JS bridge through the system Node runtime for Claude Code, Codex, Pi, and OMP. Install Node.js 20 or newer; OpenCode SQLite ingestion is pure Python.
Supported ingestion formats:
| Format | What it reads |
|---|---|
swe-rebench |
nebius/SWE-rebench-openhands-trajectories parquet, opt-in and cached locally from Hugging Face |
agent-trajectory |
Local Claude Code, Codex, Pi, OMP, OpenCode SQLite/JSON |
Datasets are not included in this repository.
Synthesis wrapper
run/report --prompt produce the evidence prompt. The normal path is to hand that prompt to your own coding agent.
trace2rules synthesize --report reports/acme__repo.md reads the Markdown prompt, appends a JSON output contract for structured parsing, and sends it to an OpenAI-compatible chat model. It does not query DuckDB, re-mine sessions, or reconstruct the prompt.
Provider selection:
TRACE2RULES_LLM_API_KEY— custom OpenAI-compatible endpoint.GEMINI_API_KEYorGOOGLE_API_KEY— Gemini OpenAI-compatible endpoint.OPENAI_API_KEY— OpenAI.
TRACE2RULES_BASE_URL makes any OpenAI-compatible endpoint usable, including a LiteLLM proxy.
Configuration
| Variable | Purpose |
|---|---|
GEMINI_API_KEY |
Gemini synthesis provider |
GOOGLE_API_KEY |
Alternate Gemini key |
OPENAI_API_KEY |
OpenAI synthesis provider |
TRACE2RULES_LLM_API_KEY |
API key for a custom OpenAI-compatible endpoint |
TRACE2RULES_BASE_URL |
OpenAI-compatible base URL (including LiteLLM) |
TRACE2RULES_MODEL |
Synthesis model, e.g. gemini-4-hydrogen |
TRACE2RULES_MAX_RULES |
Maximum rules per prompt; defaults to 10 |
TRACE2RULES_CACHE_DIR |
Local parquet cache; defaults to ~/.cache/trace2rules |
HF_TOKEN |
Used by hf download for gated datasets; not read by trace2rules |
Data and privacy
- Session data stays in your local DuckDB database.
synthesize --reportsends only the report prompt and its JSON output contract, not the full raw session database.report/runperform no LLM calls. Findings can still contain commands, paths, and repository names; review before using an external provider.data/is gitignored.
Known limits
- Detectors are heuristics. Review every proposed rule.
- Local-session formats change frequently.
agent-trajectoryis the compatibility layer; pin its version.
Development
uv sync
uv run pytest
uv build
License
MIT. See LICENSE.
Metadata
Release files for trace2rules 0.1.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| trace2rules-0.1.6.tar.gz | 88.5 kB | With provenance | Details for trace2rules-0.1.6.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| trace2rules-0.1.6-py3-none-any.whl | Python 3 | none | any | Details for trace2rules-0.1.6-py3-none-any.whl |
Total release size: 207.6 kB
Release files / trace2rules-0.1.6.tar.gz
| Download URL | trace2rules-0.1.6.tar.gz |
|---|---|
| Size | 88.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
08851cb07eb8233f70458e08b9a6c47cd2fb1a841ba27f395306a6ff7f58a8e4
|
|
BLAKE2b-256 checksum How to use checksums |
5a55d46c4b1d3be52effd3b53d57364c908f080cfc35ec73b399c0bc6b3ae349
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logRelease files / trace2rules-0.1.6-py3-none-any.whl
| Download URL | trace2rules-0.1.6-py3-none-any.whl |
|---|---|
| Size | 119.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f698f6bcd493baab250cf9c09cbd6abd2d39f99bd25794d4cbc8f6d89fea115b
|
|
BLAKE2b-256 checksum How to use checksums |
dbf93049ca3b756c1682551b4ca5c82a1d0947f6ef564d5e861cecfef6888905
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logSecurity for trace2rules 0.1.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for trace2rules-0.1.6.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.