Skip to content
OverbakerPublic

About

square² (方方): enforceable repository laws that coding agents and PRs cannot weaken. Zero-dep CI gates, base-copy judge, advisory cross-family review.

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

square² (方方): enforceable repository laws that coding agents and pull requests cannot weaken

square² (方方)

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, plus bad/ and good/ fixtures tested on every run.
  • Check runs your tools, writes nothing, names the fix. Wraps eslint, ruff, depcruise, pattern, suppressions, custom command checks, and monorepo ARCH-* 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

Problem: agents optimize for green CI

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.

Without square²: coding agent adds eslint-disable and turns off rule in eslint.config.mjs so CI passes and merges a broken law

With square²: suppression comments fail square2 check, weakening .square2/ or protected configs fails square2 judge --base <rev>, and only fixing the code passes:

With square²: check blocks suppression comments, judge blocks weakening guardrails from the base copy, and only the real fix 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.

Architecture

square² architecture: one canonical JSON model feeding square2 check, square2 judge, and advisory review

Guardrails as data, backed by proof

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 failure proof (a commit and a square2 replay report showing it fires on the defect commit and not its first parent) or a decision proof (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 check runs square2 selftest before scanning the tree.
  • One source of truth for AGENTS.md. square2 generate writes every active guardrail's law and check command into the enforcement block of AGENTS.md (or any tracked enforcement_file), and ARCH-SYNC fails check if that table drifts.

Brings your tools, adds zero dependencies

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), package exports, 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 enforce strict, unique_prefix, and append_only rules on reserved directories such as migrations.
  • ARCH-SYNC, ARCH-LEDGER, ARCH-POLICY: keep the AGENTS.md enforcement block fresh, validate the defect recall ledger (.square2/ledger/defects.jsonl), and register the judge.

Deterministic, read-only, shrink-only

square² check deterministic 6-stage read-only pipeline and shrink-only baseline ratchet

  • Three exit codes: 0 pass, 1 new finding or stale baseline entry, 2 no verdict (invalid model, missing tool, failed self-test, tampered vendor copy, or a write to the tree). Exit 2 is never a pass.
  • Check never writes: square2 check snapshots the work tree before and after checks run. Any changed tracked or unignored path aborts with [S2-WROTE] (exit 2). --report writes 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 fails check with [S2-BASELINE-STALE] until square2 baseline shrink drops it. Growing the baseline requires square2 baseline grow --approval <id> plus a base approval.

The judge runs from the base, on git objects only

square² judge extracts the base copy of the tool with git archive, compares git objects without executing PR code, and requires a base approval digest for weakening changes

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.env variables. 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 in watched by git object.
  • Zero execution of PR code: reads git objects only — no checkout, no hooks, no scripts from the PR. In CI, square2-judge.yml extracts the vendored copy from the base commit with git archive and verifies its hash against the base's lock.json. A PR that replaces the tool is judged by the tool it replaces.

Advisory cross-family AI review

square² review 3-stage credential isolation: prepare with read-only GH token, analyze with different model family and zero GH token, publish validated advisory issues

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: analyze runs square2 review guard-env (failing if GITHUB_TOKEN, GH_TOKEN, or ACTIONS_RUNTIME_TOKEN is set) and runs square2 review run in a read-only tree copy with no .git and 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.

Guardrail lifecycle & trust boundaries

Guardrail state rank ratchet from retired to enforce and CI job permission isolation matrix

What square² leaves out

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. check fails if anything mutates the work tree.

Known limits

  • Linux and macOS only. Windows is untested; byte comparisons and shell hooks assume POSIX.
  • Git 2.40+ required for judge. Older git versions exit 2.
  • Public repos and pull_request_target: square2-judge.yml ships judge (pull_request_target, strong — workflow comes from the default branch) and judge-pr (pull_request, weaker — a PR can edit its own workflow). Keep one and make both the judge job and check required status checks.
  • Guard .square2/ with host review rules. Adding an entry to approvals.json is free for the judge; a CODEOWNERS rule 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.

Quick start

  1. Copy templates/consumer/ into your repository (.square2/, .githooks/pre-push, and .github/workflows/square2-*.yml) and replace every CONSUMER placeholder.

  2. 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>
  3. Run the starter check (passes with zero guardrails):

    node tools/square2/bin/square2.mjs check
  4. Enable the pre-push hook (git config core.hooksPath .githooks), keep one judge job in square2-judge.yml, and make both the judge and check required 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 base

The Guide covers adding a first guardrail, replaying a historical defect, syncing AGENTS.md, and approving a weaker model.

Documentation

  • 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.

Development

npm install
npm run typecheck
npm test
node bin/square2.mjs check

square² 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).

About

square² (方方): enforceable repository laws that coding agents and PRs cannot weaken. Zero-dep CI gates, base-copy judge, advisory cross-family review.

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages