Repository laws that coding agents and pull requests cannot weaken.
AGENTS.md is a suggestion. Linters can be silenced with eslint-disable or @ts-ignore. CI workflows live in the same repository the agent can edit. When AI writes most of your diffs, every rule enforced only by the branch itself is one edit away from disappearing.
square² makes repository architecture rules enforceable — without adding a SaaS platform, a new linter, or a single runtime dependency.
- Rules as data, backed by proof. Declare guardrails once in
.square2/as canonical JSON. Every running guardrail needs a defect replay or a tracked decision anchor, plusbad/andgood/fixtures tested on every run. - Check runs your tools, writes nothing, names the fix. Wraps
eslint,ruff,depcruise,pattern,suppressions, customcommandchecks, and monorepoARCH-*checks in one deterministic Node.js 22+ process. - Judge runs from the base, on git objects only. Compares the base commit against the merge result using the base commit's vendored copy. A PR cannot lower a guardrail, widen an exception, grow the baseline, edit protected configs, or replace the tool without an approval already landed on the integration branch.
- Review is cross-family and strictly advisory. Once a day, a model from a different family than the commits' authors audits new commits in a read-only copy with no GitHub token. Findings become issues; it never blocks a merge.
Vendor it into your repo. Keep your existing tools. Read the whole trusted base yourself.
Node.js 22+ · git 2.40+ (for judge) · 0 runtime dependencies · Linux & macOS · MIT
Without square²: an agent hits a failing rule. It adds eslint-disable, drops a check from the config, adds an exception, or edits the CI workflow. The build turns green; the rule is gone.
With square²: suppression comments fail square2 check, weakening .square2/ or protected configs fails square2 judge --base <rev>, and only fixing the code passes:
[APP-NO-CONSOLE] src/app.js:1 matches console.log( — Instead: use the logger of the application
[S2-POLICY] guardrails.APP-NO-CONSOLE.state — Instead: keep the constraint, or get an approval at the base
policy_digest: <digest>
A pull request cannot approve itself. A pull request cannot judge itself.
The model lives in .square2/ as canonical JSON with fixed filenames: policy.json, guardrails.json, and when needed architecture.json and review.json. Evidence lives beside it in baseline.json, exceptions.json, and approvals.json.
A rule is a guardrail with a state (proposed, observe, enforce, demoted, retired), a one-line law, a proof, and a fix (instead):
{
"id": "APP-NO-CONSOLE",
"kind": "engine",
"state": "enforce",
"layer": "gate",
"class": "invariant",
"level": 2,
"owner": "your-team",
"law": "Application code does not write to the console.",
"proof": {
"kind": "decision",
"anchor": ".square2/decisions.md#no-console"
},
"check": {
"engine": "pattern",
"options": {
"pattern": "console\\.log\\("
}
},
"scope": ["src/**"],
"instead": "use the logger of the application"
}No dead rules pile up:
- Every running guardrail needs a proof. Either a
failureproof (a commit and asquare2 replayreport showing it fires on the defect commit and not its first parent) or adecisionproof (an anchor line in a tracked document). No setting turns this off. - Every engine guardrail self-tests first.
.square2/fixtures/<id>/bad/must trigger it;.square2/fixtures/<id>/good/must stay silent.square2 checkrunssquare2 selftestbefore scanning the tree. - One source of truth for
AGENTS.md.square2 generatewrites every active guardrail's law and check command into the enforcement block ofAGENTS.md(or any trackedenforcement_file), andARCH-SYNCfailscheckif that table drifts.
square² is a minimal harness, not a linter or build system. Keep the tools your repository already uses; plug them in as engines, or declare monorepo structure checks.
| Engine | Runs |
|---|---|
pattern |
Built-in regex check (no external tool, 2s per-file timeout) |
suppressions |
Catches eslint-disable, @ts-ignore, @ts-expect-error, @ts-nocheck, noqa, ruff: noqa, flake8: noqa, type: ignore |
eslint |
Your repository's ESLint rules |
depcruise |
Your repository's dependency-cruiser boundary rules |
ruff |
Ruff for Python repositories |
command |
Any executable printing JSON lines {"path", "line", "subject"} |
Built-in architecture checks (active only when declared in .square2/guardrails.json):
ARCH-UNIT,ARCH-EXPORTS,ARCH-TSCONFIG: keep workspace members (pnpm, uv, TypeScript solutions, directory globs), packageexports, and TypeScript project references aligned with.square2/architecture.json.ARCH-GENERATED,ARCH-REGISTRY,ARCH-RESERVED: verify generated files match fresh generator output byte for byte, and enforcestrict,unique_prefix, andappend_onlyrules on reserved directories such as migrations.ARCH-SYNC,ARCH-LEDGER,ARCH-POLICY: keep theAGENTS.mdenforcement block fresh, validate the defect recall ledger (.square2/ledger/defects.jsonl), and register the judge.
- Three exit codes:
0pass,1new finding or stale baseline entry,2no verdict (invalid model, missing tool, failed self-test, tampered vendor copy, or a write to the tree). Exit2is never a pass. - Check never writes:
square2 checksnapshots the work tree before and after checks run. Any changed tracked or unignored path aborts with[S2-WROTE](exit2).--reportwrites only after that, to the path you pass. Same tree, same lines, byte for byte. - Baseline only shrinks: old findings live in
.square2/baseline.json. Once fixed, a stale entry failscheckwith[S2-BASELINE-STALE]untilsquare2 baseline shrinkdrops it. Growing the baseline requiressquare2 baseline grow --approval <id>plus a base approval.
square2 judge --base <rev> compares the base commit against the merge result of the pull request:
- Model and evidence ratchet: flags lowered guardrail states, narrowed scopes, removed guardrails, grown baselines, new or widened exceptions, and added
policy.envvariables. Adding a guardrail or raising its state is free. - Protected files and watched JSON keys: compares
.githooks/pre-push,.github/workflows/square2-*.yml, engine configs, command scripts, the vendored tool,.square2/lock.json, and any JSON pointers inwatchedby git object. - Zero execution of PR code: reads git objects only — no checkout, no hooks, no scripts from the PR. In CI,
square2-judge.ymlextracts the vendored copy from the base commit withgit archiveand verifies its hash against the base'slock.json. A PR that replaces the tool is judged by the tool it replaces.
Deterministic gates catch structural regressions; they do not catch subtle logic bugs. Once a day on the integration branch, square2-review.yml runs a three-stage review (prepare → analyze → publish) with a model family different from the commits' authors:
- Credential-isolated:
analyzerunssquare2 review guard-env(failing ifGITHUB_TOKEN,GH_TOKEN, orACTIONS_RUNTIME_TOKENis set) and runssquare2 review runin a read-only tree copy with no.gitand no GitHub token. - Untrusted output: model output is validated against
schemas/review.v1.json, pinned to shown changed lines, scanned for key leaks, and escaped to plain Markdown before opening issues. - Strictly advisory: findings become GitHub issues. It never fails a job and never gates a merge.
What a tool refuses to build defines its trust boundary:
- No hosted service, no dashboard, no telemetry. Everything lives in your repository and runs in your CI.
- No runtime dependencies. Plain Node.js 22+ ES modules (
node:built-ins only), vendored and hash-locked to.square2/lock.json([S2-VENDOR]on tampering). Runs before any install step. - No YAML and no comments in model files. Canonical two-space sorted JSON only; rationale lives in
law,why, and tracked decision anchors. - No blocking AI review. Models hallucinate and untrusted code carries prompt injection; the worst case of review is an issue a person closes.
- No hidden writes.
checkfails if anything mutates the work tree.
- Linux and macOS only. Windows is untested; byte comparisons and shell hooks assume POSIX.
- Git 2.40+ required for
judge. Older git versions exit2. - Public repos and
pull_request_target:square2-judge.ymlshipsjudge(pull_request_target, strong — workflow comes from the default branch) andjudge-pr(pull_request, weaker — a PR can edit its own workflow). Keep one and make both the judge job andcheckrequired status checks. - Guard
.square2/with host review rules. Adding an entry toapprovals.jsonis free for the judge; aCODEOWNERSrule requiring approver review on.square2/decides who can land an approval.
See Trust model for all nine trust rules and the full list of limits.
-
Copy
templates/consumer/into your repository (.square2/,.githooks/pre-push, and.github/workflows/square2-*.yml) and replace everyCONSUMERplaceholder. -
Clone square² and vendor it into your repository (writes
.square2/lock.json):git clone https://github.com/Overbaker/square2 /tmp/square2 node /tmp/square2/bin/square2.mjs vendor --from https://github.com/Overbaker/square2 --ref <tag-or-sha>
-
Run the starter check (passes with zero guardrails):
node tools/square2/bin/square2.mjs check
-
Enable the pre-push hook (
git config core.hooksPath .githooks), keep one judge job insquare2-judge.yml, and make both the judge andcheckrequired status checks.
Tell your coding agent in AGENTS.md:
Before pushing:
- run `node tools/square2/bin/square2.mjs check` and fix every finding as its `Instead:` line directs
- run `node tools/square2/bin/square2.mjs explain <path>` to inspect the guardrails on a file
- do not weaken `.square2/` guardrails, add suppression comments, or widen exceptions without an approval at the baseThe Guide covers adding a first guardrail, replaying a historical defect, syncing AGENTS.md, and approving a weaker model.
- Guide — adopt square², add guardrails, replay defects, accept findings, approve weaker models, upgrade, roll back, run advisory review.
- Reference — every model file and JSON key, engines, built-in checks, commands, exit codes, diagnostic codes, environment allow-list, report schema.
- Trust model — the nine trust rules, judge mechanics, workflow threat boundaries, and what square² does not promise.
- Decisions — architectural decisions (with anchors) explaining what was chosen and what was rejected.
- Auth — Azure OpenAI and OpenAI-compatible gateway settings for the advisory review.
- Templates — starter
.square2/model, pre-push hook, and GitHub Actions workflows. - llms.txt — concise machine-readable index for coding agents and LLMs.
- Contributing — layer rules, local verification, and the public repository rule.
npm install
npm run typecheck
npm test
node bin/square2.mjs checksquare² checks its own architectural guardrails in .square2/guardrails.json (one-way layer imports, single process runner, single glob dialect, zero runtime dependencies). Tests require Node.js 22+, git 2.40+, pnpm 12, and uv 0.13 on PATH. Licensed under MIT (LICENSE).




