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.
The output is a proposal, not an automatically applied AGENTS.md.
Small local corpus? Results are most useful with about 10+ local sessions. With fewer, ingest a public dataset first; see Public datasets.
Quick start
Paste this into your coding agent to audit the current checkout:
Install trace2rules if missing (`uv tool install trace2rules`). Run `trace2rules audit --prompt > audit.txt`, read audit.txt, and list unobserved, violated, and unenforced rules. Propose concrete enforcement without duplicating existing CI/pre-commit checks.
Or run the deterministic audit manually:
trace2rules audit
Paste this into your coding agent to generate rules:
Install trace2rules if missing (`uv tool install trace2rules`). Run `trace2rules mine --prompt --out-dir prompts/` for my local agent sessions. Read the prompt files and synthesize repo-specific AGENTS.md rules yourself. Keep only rules supported by the evidence.
Or generate the evidence prompts manually:
trace2rules mine --prompt --out-dir prompts/
See Auditing AGENTS.md and Generating rules for the full flows.
Auditing AGENTS.md
trace2rules audit is deterministic: it prints counts, coverage, verdicts, enforcement status, and deterministic suggestions.
For a fresh-agent review, emit the evidence prompt:
trace2rules audit --prompt > audit.txt
Give audit.txt to your agent. The LLM receives the audit table but not the deterministic suggestions.
After the agent writes proposed rules, validate them:
trace2rules audit --proposal AGENTS.proposed.md
--proposal parses the proposed Markdown, audits it against sessions, and prints verdicts before you apply it.
On the Open-SWE swc-project/swc split (188 sessions), the current AGENTS.md audit finds 65 rules: 9 measured, 1 partial, and 55 unmeasured.
| Rule | Coverage | Followed | Violations | Confidence | Enforcement |
|---|---|---|---|---|---|
| Commit frequently | measured | 136 | 52/188 (28%) | high | missing |
| No nightly-only rustc features | measured | 172 | 16/188 (9%) | low | missing |
Use gh for GitHub data |
measured | 18 | 6/24 (25%) | medium | .github/workflows/CI.yml |
Snapshot update with UPDATE=1 cargo test |
measured | 23 | 2/25 (8%) | medium | missing |
--no-verify guard |
partial | 13 | 1/14 (7%) | low | missing |
| Read the codebase before editing | measured | 123 | 62/185 (34%) | high | guidance only |
| Verify assumptions against source/tests | measured | 123 | 62/185 (34%) | high | guidance only |
| Submodule update before tests | measured | 0 | 32/32 (100%) | medium | guidance only |
git commit only after git add |
measured | 8 | 6/14 (43%) | low | guidance only |
cargo test -p <crate> |
measured | 14 | 0/14 (0%) | 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.
The two read rules are separate clauses but share the read_before_edit detector. Their identical followed/violation counts are expected, not evidence that they are the same rule.
The MSRV rule is unmeasured: the trace does not declare a rust-version, and a RUSTC_BOOTSTRAP marker is not proof of an MSRV violation. Nightly counts all sessions with Rust toolchain activity; 16 of 188 used RUSTC_BOOTSTRAP or +nightly.
LLM review of the swc audit
An audit --prompt pass over the same table suggests:
- Delete the four stale binding
test.shcommands; they are unmeasured and reference paths that no longer exist. - Enforce commit cadence with a pre-push clean-worktree check and reject
WIP,fixup!, andsquash!subjects. - Enforce the nightly rule with a stable-toolchain check and reject
#![feature(...)]; MSRV remains unmeasured until a declaredrust-versioncheck exists. - Reject
curl/wgetto GitHub in CI soghremains the access path. - Run snapshot tests with
UPDATEunset and fail on uncommitted fixture changes. - Run the same checks in CI as in hooks so
--no-verifycannot bypass them. - Consolidate the two read rules into a PR verification requirement: read the implementation, fixtures, and tests, then list verified paths.
- Move submodule initialization into CI and test wrappers; add explicit staging and
git commit -achecks.
The review also proposed seven new rules: clean handoff, commit-subject hygiene, no git commit -a, no GitHub scraping, no UPDATE in CI, submodule wrappers, and an MSRV workflow.
audit prints the audited/total session count and defaults to the latest AGENTS.md commit cutoff. Pass --all-sessions for full history.
Generating rules
Mining produces evidence, not rules. trace2rules mine --prompt --out-dir prompts/ writes one prompt per detected repository; give those prompts to your agent. The agent proposes rules, then validate them with audit --proposal. Use --root when the checkout is not the directory containing AGENTS.md, so path and enforcement checks inspect the real repository.
swc-project/swc
- [guidance] Before changing implementation code, run an existing fixture/test or add a focused failing test and execute it on the unmodified tree; do not use a standalone
/tmpmock as the only reproduction for changes undercrates/. - [guidance] Create one-off reproductions under
/tmpor as tests/fixtures in the affected crate; do not add a temporary workspace crate or edit workspaceCargo.toml/Cargo.lockfor a reproduction. - [guidance] When the default toolchain rejects repository rustflags, run cargo with
RUSTUP_TOOLCHAIN=<stable>and setRUSTFLAGSat invocation; do not edit.cargo/config.tomlto work around the toolchain. - [guidance] For large source files, read the relevant ranges with
sed -n '<start>,<end>p' <file>or locate symbols withrg -n; do not page whole files until output is clipped.
rkkautsar/trace2rules
- [guidance] Keep
src/trace2rules/cli.pylimited to Typer wiring and option parsing; put non-CLI logic in a dedicated module undersrc/trace2rules/. - [guidance] When adding, renaming, or removing a CLI command or option in
src/trace2rules/cli.py, update the matching row in theREADME.mdcommand table in the same change.
The latest synthesis produced these four rules after the stale-path filter was corrected. Rules are marked guidance when no concrete CI, pre-commit, or lint check exists; enforceable rules require a specific check. Validate proposals with audit --proposal.
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, then mines repo-local sequential rules (
antecedent -> specific error) with support, confidence, lift, and a representative evidence window. Exact sequences use lift and paired-rework evidence; windows never cross a compaction boundary. - Prompt selects bounded findings and writes one readable Markdown evidence prompt per repo when
mine --promptis used. - 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 prompting.
Commands
| Command | Purpose |
|---|---|
ingest |
Load sessions and tool events into DuckDB |
mine |
Mine friction findings, JSON, or evidence prompts; auto-ingests local sessions |
doctor |
Check install, Node, source stores, and database schema |
patterns |
Mine repo-local sequential rules (antecedent → specific error) |
exact |
Show recurring normalized sequences with evidence |
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 --format open-swe --repos owner/name
trace2rules ingest --source PATH --limit 200
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 --root . --prompt
trace2rules holdout --agents AGENTS.md --fraction 0.3
trace2rules mine --prompt --min-support 5
trace2rules mine --prompt --max-rules 5
trace2rules mine --prompt --out-dir prompts/ --max-rules 10
trace2rules mine --json
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. --limit N selects the newest N files per invocation; re-runs can select a different set when files are added or touched. Use --limit 0 for the full deterministic set. 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. mine --prompt emits one prompt per detected repo:
trace2rules mine --prompt --out-dir prompts/
Local ingest treats each compaction segment as its own session by default; use --no-split-compactions to keep the original boundaries. This applies to OMP, Pi, Codex, Claude Code, and OpenCode.
mine and audit auto-ingest local sessions. They refresh the default database on each run; explicit --source or non-default --format triggers ingestion into a custom --db. ingest writes to $XDG_DATA_HOME/trace2rules/trace2rules.duckdb by default (~/.local/share/...). Set TRACE2RULES_DB to override it.
Public datasets
Use a public dataset when the local corpus is small. Both datasets are public and require no Hugging Face token. The first run downloads the parquet into ~/.cache/trace2rules; set TRACE2RULES_CACHE_DIR to move it, or pass --refresh-cache to re-download.
swe-rebench
nebius/SWE-rebench-openhands-trajectories
trace2rules ingest --format swe-rebench
trace2rules ingest --format swe-rebench --repos django__django --limit 50
The default repositories are django__django and pytest-dev__pytest. For swe-rebench, --repos takes instance prefixes such as django__django.
open-swe
trace2rules ingest --format open-swe --repos owner/name
--repos is required for open-swe and takes repository labels in owner/name form. This downloads six public train-*.parquet files.
agent-trajectory
Local Claude Code, Codex, Pi, OMP, and OpenCode SQLite/JSON stores. See Local sessions.
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.
Configuration
Variables are read from the process environment. trace2rules does not load .env files.
| Variable | Purpose |
|---|---|
TRACE2RULES_DB |
Override the default DuckDB path |
TRACE2RULES_CACHE_DIR |
Local parquet cache; defaults to ~/.cache/trace2rules |
TRACE2RULES_WINDOWS_STORES |
Auto-detects WSL and scans /mnt/c/Users; set to 0 to disable or 1 to force |
TRACE2RULES_WORKERS |
Mining worker count; defaults to the memory budget |
TRACE2RULES_MEMORY_LIMIT |
DuckDB memory cap; defaults to 25% of available memory |
Data and privacy
- Session data stays in your local DuckDB database.
mineperforms no LLM calls. Findings can still contain commands, paths, and repository names; review before sharing a prompt externally.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.2.2
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.2.2.tar.gz | 115.4 kB | With provenance | Details for trace2rules-0.2.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| trace2rules-0.2.2-py3-none-any.whl | Python 3 | none | any | Details for trace2rules-0.2.2-py3-none-any.whl |
Total release size: 272.2 kB
Release files / trace2rules-0.2.2.tar.gz
| Download URL | trace2rules-0.2.2.tar.gz |
|---|---|
| Size | 115.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
330b015d2808e5bea6161e5c81c27f612c3945d8f4d24c443df40377604d3dd2
|
|
BLAKE2b-256 checksum How to use checksums |
ff3741a31bf48b06598ce5571c874efa71c33679928bea45aeac6a56f6e49071
|
| 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 21, 2026.
Transparency logRelease files / trace2rules-0.2.2-py3-none-any.whl
| Download URL | trace2rules-0.2.2-py3-none-any.whl |
|---|---|
| Size | 156.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b3728fa5b5ec8c535fe1e01ec70f19635cedd754d3210ddc57358ef67829ecde
|
|
BLAKE2b-256 checksum How to use checksums |
4376a17b2ad065e130f6f1e3696e5917ddf51f738cb888af00e24588dc09394f
|
| 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 21, 2026.
Transparency logSecurity for trace2rules 0.2.2
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.2.2.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 21, 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.