Archy
Archy is an architectural sensor for Python codebases, exposing tools to help AI agents monitor, analyze, and enforce structural health.
Compute quality scores (
archy_score): Calculate a composite score (modularity, acyclicity, depth, equality) with optional regression gating.Find import cycles (
archy_cycles): Detect circular dependencies using Tarjan's SCC algorithm, sorted by size.Enforce layer rules (
archy_check): Validate direct imports against YAML-defined layer constraints, including Stable Dependencies Principle violations.Run transitive contracts (
archy_contracts): Stricter multi-hop enforcement via import-linter (Layers, Forbidden, Independence, AcyclicSiblings, etc.).Track score history (
archy_trend): Read historical score records to monitor architectural drift over time.Assess blast radius (
archy_impact): Identify all modules transitively affected by changes to given files — useful before refactoring.Snapshot & diff (
archy_snapshot,archy_diff): Capture a baseline of score/cycles/violations, then compare current state to detect regressions.Record baselines (
archy_record_baseline): Compute and persist a score to history for future regression comparisons.Explore dependency graphs (
archy_graph_focus,archy_graph_summary,archy_graph): Get a bounded subgraph around specific modules, a whole-project overview (top-N by fan-in/fan-out/PageRank, external deps), or a full graph dump with size limits.Agent loop prompt: Exposes a
loopprompt with a feedback-loop playbook for snapshot-diff workflows.
archy
Architectural sensor for Python codebases - keeps structure honest under AI-assisted development.
Status: v0.15.0. Usable today via:
Mode | Command |
Inspection |
|
CI governance |
|
Transitive contracts |
|
One-shot score |
|
Trended score |
|
MCP server |
|
How the score is computed and how to read it: docs/SCORING.md. Benchmarks against pydantic, fastapi, flask, pytest, and archy-on-archy: docs/CASE_STUDIES.md. Design rationale and comparison with sentrux: docs/LEARNINGS.md.
Why
AI agents generate code at machine speed. Without a feedback loop on structural health (module coupling, import cycles, layer violations), codebases drift architecturally even when every individual change looks fine in review.
archy watches a Python codebase, builds a live module-dependency graph, and surfaces drift through a single trended score plus a handful of actionable sub-metrics. It's designed to run in CI, in pre-commit, and as an MCP server (archy mcp) so coding agents can read their own architectural impact before committing.
The agent-feedback framing is empirically supported by 2025-2026 research: the Navigation Paradox paper shows large LLM context windows do not eliminate the need for structural graph navigation, LocAgent's ablation finds graph edges materially improve code-localization accuracy, and the coding-agent failure-mode literature names the specific patterns (scope drift, cross-file reasoning failure) that an architectural feedback loop is built to catch. Citations, a failure-mode-to-archy-capability mapping, and the resulting roadmap priorities are in docs/RESEARCH_METRICS.md §14c.
Scope
Python only. The cross-language story belongs to sentrux; that division is settled. archy goes deep on Python (transitive contracts, SDP, NCCD,
if TYPE_CHECKING:semantics) rather than broad across languages; seedocs/LEARNINGS.md§"Competitive landscape".Tree-sitter powered. Robust to in-flight edits and partial files; survives syntax errors that would crash
ast.Score that trends over time. A single number per commit, persisted, plotted. Trend matters more than the absolute value.
Rules as YAML. "Layer X cannot import Y." No DSL, no plugins (yet).
Non-goals
Multi-language analysis
Replacing linters, type checkers, or test runners
Generating code or auto-fixing violations
Quick start
pip install archy
# or: uv tool install archy
# or: pipx install archyAll examples below use the installed archy command. If you're working from a checkout, prefix them with uv run (e.g. uv run archy graph .).
Inspect the graph
archy graph path/to/project --internal-only
archy graph path/to/project --format json > graph.json
archy graph path/to/project --format dot | dot -Tsvg > graph.svgFind import cycles
Tarjan SCCs of size >= 2, plus self-loops (a module importing itself). Use --strict in CI to fail on any cycle.
archy cycles path/to/project
archy cycles path/to/project --format json
archy cycles path/to/project --strictEnforce layer rules
Reads archy.yaml from the repo root. Exits 1 on any violation. See Layer rules below.
archy check path/to/project
archy check path/to/project --format json
archy check path/to/project --config custom.yamlTransitive contracts (archy contracts)
archy check only sees direct edges. archy contracts wraps import-linter so the same layer story is enforced transitively (A → B → C still counts as A reaching C). It is the strictness upgrade for projects whose layers leak through indirect paths.
pip install 'archy[contracts]'
archy contracts path/to/project
archy contracts path/to/project --format jsonConfig resolution. archy contracts reads, in order:
The
--configargument if passed..importlinterin the project root: the canonical contracts config.archy.yaml: best-effort fallback. Eachforbid:rule becomes one Forbidden contract checked transitively. Emits aUserWarningbecause this path cannot expressignore_imports, so any legitimate transitive edge (e.g., a service layer reachingpsycopgthrough a sanctionedapp.libs.db.*module) will be reported as a violation with no way to whitelist it.
Two configs, one concern each:
archy.yamlowns layer definitions, direct-edge gating (archy check),sdp:,exclude:, androots:..importlinterowns transitive contracts: all five contract types (Forbidden, Layers, Independence, Protected, AcyclicSiblings) andignore_importswhitelists.
Reach for .importlinter as soon as you need transitive enforcement at all; the archy.yaml fallback is a zero-config onramp, not a feature target. See .importlinter in this repo for a real-world example, and the import-linter contract types reference for the full grammar.
Common case: forbid services from reaching psycopg but allow the sanctioned db library to do so:
[importlinter]
root_package = app
[importlinter:contract:services-must-not-reach-psycopg]
name = services must not reach psycopg
type = forbidden
source_modules =
app.services
forbidden_modules =
psycopg
ignore_imports =
app.libs.db.engine -> psycopgCompute a quality score
Composite of modularity, acyclicity, depth, and equality (geometric mean). See docs/SCORING.md for formulas and how to interpret the breakdown. These four axes were chosen after surveying ~15 alternatives from the package-metrics literature (Martin's I/A/D, Lakos's NCCD, MacCormack propagation cost, Structure101 fat/tangle, reflexion models, cognitive complexity, hotspots, logical coupling, dead/duplicate-code detection); Martin's I and the Stable Dependencies Principle check are also shipped as a per-module diagnostic and an archy check rule. See docs/RESEARCH_METRICS.md for the full validation, what was shipped, and what was deferred and why.
archy score path/to/project
archy score path/to/project --format jsonTrack score over time
Persist per-commit scores to .archy/history.jsonl and chart the trend.
archy score path/to/project --record
archy trend path/to/project
archy trend path/to/project --last 30 --format jsonRegression gate
Fail if the current score drops more than --strict-tolerance (default 0.02) below the most recent recorded run.
archy score path/to/project --strict
archy score path/to/project --strict --record # check then record
archy score path/to/project --strict --strict-tolerance 0.0Blast radius
List internal modules that transitively depend on a given file. Useful before refactoring or removing a module.
archy impact path/to/project --file app/libs/db.py
archy impact path/to/project --file app/libs/db.py --file app/services/auth.py --format jsonSnapshot and diff (agent feedback loop)
Capture a baseline at the start of an editing session, then diff after edits to see exactly which cycles or layer rules changed. See docs/AGENT_LOOP.md for the full playbook (also available via the MCP server's loop prompt).
archy snapshot path/to/project # writes .archy/baseline.json
# ... edit code ...
archy diff path/to/project # score deltas + added/resolved cycles & violationsRun as an MCP server
Stdio transport, so AI agents can call archy directly. See MCP server below.
archy mcpMCP server (archy mcp)
archy mcp exposes thirteen tools and one prompt to MCP-aware AI agents (Claude Code, the Anthropic API, etc.):
Tool | Purpose |
| Compute the four-metric score; optional |
| Find import cycles. |
| Run layer rules from |
| Run import-linter contracts (transitive Layers, Forbidden, Independence, Protected, AcyclicSiblings). Stricter than |
| Read recent score history. |
| Given changed file paths, return the modules that transitively import them (blast radius). |
| Capture score, cycles, and violations to |
| Compare current state against the snapshot; returns added/resolved cycles & violations and per-component score deltas. |
| Convenience wrapper for |
| Bounded subgraph around one or more modules (qualnames or file paths). |
| Top-N modules by fan-in, fan-out, and PageRank, plus top external dependencies. Whole-project overview sized for LLM context. |
| Full dependency-graph dump matching |
| Top-N internal modules by |
The server also exposes a loop prompt with the agent feedback-loop playbook (snapshot at start, impact before edit, diff after edit). Discoverable via the standard MCP prompts/list call. See docs/AGENT_LOOP.md for the human-readable version.
Wire it into Claude Code (or Cursor, Windsurf, OpenCode, any MCP client) with this stanza in your config:
{
"mcpServers": {
"archy": { "command": "archy", "args": ["mcp"] }
}
}If you're running from a checkout instead of an install, use:
{
"mcpServers": {
"archy": { "command": "uv", "args": ["run", "archy", "mcp"] }
}
}Regression-gate semantics
--strict reads the last row from .archy/history.jsonl and compares the current score against it. Drops beyond the tolerance fail with exit code 1. The default tolerance (0.02) matches the threshold sentrux's gate uses. This gives archy parity with sentrux's regression-gate use case while keeping the long-term JSONL history for archy trend.
CI integration
GitHub Action
archy ships a composite action you can drop into any workflow:
- uses: hslee16/archy@v0.15.0
with:
command: score # score | check | cycles
path: .
strict: "true" # fail on regression (score) or any cycle (cycles)Inputs (all optional unless noted):
Input | Default | Notes |
|
|
|
|
| Project root to analyze |
|
|
|
|
|
|
|
|
|
| (auto) |
|
|
| Python to install |
Pre-commit hook
Add to .pre-commit-config.yaml:
repos:
- repo: https://github.com/hslee16/archy
rev: v0.15.0
hooks:
- id: archy-check # layer rules from archy.yaml
- id: archy-score-strict # regression gate against last recorded score
- id: archy-cycles # fail on any import cyclearchy-score-strict reads .archy/history.jsonl; commit a baseline first with archy score . --record.
Layer rules (archy check)
Drop an archy.yaml at the repo root declaring layers and forbidden directions:
layers:
domain:
modules:
- "myapp.domain.**"
application:
modules:
- "myapp.application.**"
infra:
modules:
- "myapp.infra.**"
- "myapp.adapters.**"
forbid:
- {from: domain, to: application}
- {from: domain, to: infra}
- {from: application, to: infra}Pattern syntax. Dotted-name globs: * matches one segment, ** matches zero or more. myapp.domain.** covers the package itself and every descendant. Modules must belong to at most one layer.
Excluding directories. Add an optional exclude: list of directory basenames to skip codegen output, vendored code, etc. Each name is matched anywhere in the project tree (same mechanism as the built-in skips for .venv, node_modules, __pycache__):
exclude:
- baml_client
- generatedexclude: applies to every analysis (graph, cycles, score, check) and the equivalent MCP tools.
Namespace packages (roots:). archy discovers packages by walking __init__.py files. PEP 420 namespace packages (no __init__.py) are invisible by default. Declare them as roots so descendants get qualified names:
roots:
- app # `app/main.py` becomes `app.main`
- src/service # `src/service/db.py` becomes `service.db`Without roots:, a project like app/libs/db.py (no app/__init__.py) is either skipped entirely or shows up as a top-level libs.db, which makes layer rules like app.libs.** match nothing.
Discovery. archy check walks PATH upward to find archy.yaml unless --config is given. Exits 1 on violation.
archy enforces its own architecture this way; see archy.yaml at the repo root and the archy check . step in .github/workflows/ci.yml.
Stability check (sdp:). Optionally enable Robert Martin's Stable Dependencies Principle: a module should not import one that is less stable than itself. Stability is I = Ce / (Ce + Ca) where Ce is outgoing internal imports and Ca is incoming, so I = 0 means "depended on, depends on nothing" (most stable) and I = 1 means "depends on lots, nothing depends on this" (least stable).
sdp:
enabled: true
tolerance: 0.0 # ignore violations within this I gap; default 0
mode: error # 'error' fails the gate (default); 'warn' reports but exits 0When enabled, archy check flags every internal import edge whose target's I strictly exceeds the source's (plus tolerance). Per-module I is also surfaced in archy graph --format json whether or not sdp: is enabled, so you can audit before turning enforcement on.
Gradual adoption. Existing codebases will often have SDP violations on day one. Set mode: warn to report violations in the output (and archy_check's sdp_violations payload) without failing the gate, then flip to mode: error once the count is at zero. Layer-rule violations always fail the gate regardless of sdp.mode.
Development
uv sync # install runtime + dev deps from uv.lock
uv run ruff check # lint
uv run ruff format # format
uv run ty check # type check
uv run pytest # testsOne pytest case (test_pagerank_matches_networkx_when_available) compares archy's hand-rolled _pagerank against nx.pagerank, which needs numpy/scipy. The dependency is intentionally not in the default install (archy stays scientific-stack free); to run that test locally, sync the optional parity group:
uv sync --group parity # pulls in numpy + scipy for the parity test
uv run pytest # the test now runs instead of being skippedRoadmap
Next up:
Call graph: second edge type alongside imports
Design Structure Matrix (
archy dsm)
Shipped: tree-sitter import graph, __init__.py re-export resolution, Tarjan cycle detection, YAML layer rules (archy check), composite score (archy score), JSONL history + archy trend, MCP server (archy mcp), GitHub Action + pre-commit hooks, blast-radius (archy impact), snapshot/diff agent loop (archy snapshot / archy diff + MCP loop prompt), import-linter contract wrap (archy contracts, archy[contracts]), graph-navigation MCP tools (archy_graph_focus, archy_graph_summary, archy_graph; design in docs/SPEC_GRAPH_MCP.md), per-module edit_risk composite + archy_high_risk_modules MCP tool (geometric mean of propagation cost, normalized fan-in, and instability; surfaced on every graph payload).
See docs/FUTURE.md for the longer list and docs/LEARNINGS.md for design notes.
Contributing
See CONTRIBUTING.md for style rules. Notably: no em-dash characters (U+2014) anywhere in the repo.
License
MIT, see LICENSE.
Maintenance
Latest Blog Posts
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/hslee16/archy'
If you have feedback or need assistance with the MCP directory API, please join our Discord server