The challenge
Code generation is fast, but each new session still reconstructs ownership, boundaries, and dependencies from scattered implementation details.
Quick introduction video
A repository-native contract graph so the next session can find where a change belongs before reading code.
Quick start
Install the CLI, initialise a repository, then reload your coding environment.
npm install --global contract-graph
cg --version
cd your-repository
cg init
Requires Node.js 18.17 or newer. cg init confirms this directory before writing. For existing code, continue with /cg-warmup (adoption if roots are unmapped, reseed if the graph is already connected). For a new repository, use /cg-plan. Upgrading 0.3.0 or 0.4.0 is documented separately.
Clone the project to inspect, test, or contribute to the source.
git clone https://github.com/sarada-io/contract-graph.git
cd contract-graph
npm ci
npm test
The repository includes the CLI, schemas, structural bindings, and all seven lifecycle skills.
Open Contract Graph on GitHub ↗ (opens in a new tab)Executive summary
Contract Graph is an open-source, repository-native system for helping coding agents locate, plan, implement, and verify changes without rediscovering the entire architecture in every session.
Code generation is fast, but each new session still reconstructs ownership, boundaries, and dependencies from scattered implementation details.
Schema-backed contracts form a traversable graph from repository to module, submodule, component, and implementation.
An agent can route a task to the smallest responsible boundary, use its declared surface, and carry contracts and verification forward with the code.
The mental model
A request enters at the repository contract and follows authored routes through progressively smaller responsibilities. Only then does the agent open implementation files.
Parent and child contracts form the top-down spine. Every reachable unit has one canonical contract and reciprocal edges.
Dependency, surface, route, invariant, and verification edges explain how a unit participates without flattening the repository into a file graph.
The graph narrows the search to the smallest responsible boundary and its declared public surface before implementation reading begins.
Workflow
Each stage produces an artifact the next stage can verify. Stage boundaries preserve review points; execution inside a stage stays continuous and sequential.
Turn the intended outcome into ordered phases with measurable acceptance gates.
/cg-planConvert one phase into dependency-aware Step briefs with exact editable paths and completion gates.
/cg-prepareExecute the earliest ready Step, delivering implementation, tests, contracts, and detectors together.
/cg-produceVerify the accumulated result, repair composition findings, preserve durable knowledge, and close only when green.
/cg-sign-offAdoption for existing code, or additive reseed after a package upgrade: discover or extend contracts without rewriting existing purpose or product rules.
/cg-warmupUse contract-backed decisions and reversible defaults; consolidate only material owner decisions.
/cg-unblockFollow measured handoffs within granted authority, stopping at decisions, failures, limits, or budget.
/cg-auto-runThe contract
A contract lives at <unit>/.agents/cg/contract.yaml. It describes current truth in the context of its parent—not an aspirational architecture and not a generated inventory of files.
schemaVersion: 1
kind: component
name: payments
purpose: Own payment authorization.
responsibilities:
owns:
- Authorize a payment request.
forbids:
- Persist customer profiles.
surface:
- kind: service
path: src/PaymentService.ts
relations:
parent: ../.agents/cg/contract.yaml
verification:
- npm test -- payments
Seeing where you are
These commands inspect the same disk state the stages use. They do not require the last chat. Contract Graph protects structural truth it can measure while keeping advisory guidance separate from blocking rules.
cg contract route --task matches task language against repository-owned routes, then returns the contracts to load.
cg verify checks schema shape, reference resolution, reciprocal edges, acyclicity, root reachability, surfaces, and verification links.
cg modules reads build manifests to expose module roots that remain unmapped or still need recursive descent.
A principles are globally enforced, P rules bind one repository, and E guidance remains non-binding engineering advice.
cg nextcg residuecg verifycg graph showcg contract route --task "…"cg sync
What verification means: the authored graph is internally consistent and its declared checks resolve. It is not a claim that every implementation dependency or exported symbol has been inferred from source.
Adopt it
Initialisation installs the schemas, bindings, workflow, and seven skills. The next step differs for brownfield, an existing graph, and greenfield code.
Existing repository
cg init./cg-warmup in a new AI session. If cg modules still has gaps, that is adoption; if every adapter-detected root is governed, that is reseed. cg modules lists those roots, not every directory that already has a contract.cg sync && cg verify.New repository
cg init./cg-plan.Already on 0.3.0 or 0.4.0: install contract-graph@0.5.0, run cg init --yes --docs docs, reload the IDE, then /cg-warmup. Skills and schemas replace; contracts and catalogs stay. A stored all profile expands. A connected graph takes the reseed path. See the upgrade guide.
Public beta: review generated governance and proposed structural changes before merging. Warmup describes existing behavior; any restructuring it identifies should become planned delivery work.
Capabilities and limits
Schema-backed contracts, task routing, brownfield discovery, seven lifecycle skills, graph reciprocity, acyclicity, reachability, surface checks, and verification links.
Complete inference of every implementation dependency or exported symbol, proof that architecture matches runtime behavior, or safe arbitrary parallel write sets.
The graph remains useful only when contract, detector, implementation, and tests change together. Governance is how that durable context stays trustworthy.
The reasoning behind bounded context and contract-led scaling.
Read the vision →The full canonical contract model and graph invariants.
Understand contracts →How an outcome becomes phases, steps, and a lasting graph on disk.
Follow the workflow →The stages you run, and the structural walk they share.
Follow the lifecycle →Move a 0.3.0 or 0.4.0 install to 0.5.0 without rewriting contracts.
Read the upgrade path →The JSON Schemas referenced by Contract Graph files.
Browse schemas →