schema-gate
Fail-Closed Schema-Boundary Contract Gate for AI Coding Agents
(Zero-dependency Python 3 library, CLI, Claude Code Hook, MCP stdio server, SARIF CI gate & Runtime DB-API SQL Proxy)
Why AGENTS.md and Import Linters Are Not Enough
Your CI blocks risk from importing auth. Your AGENTS.md politely tells AI coding agents not to couple domains. Then an agent gets a sprint ticket "freeze high-risk users" and writes:
UPDATE users SET status = 'risk_locked' WHERE id = :entity_id
Zero forbidden imports. Unit tests pass. Yet this single line breaks three architectural invariants at once:
- Concept Compression (
G2) — stuffs fraud-risk state intoauth's account-lifecycle column; - Foreign-Writer Coupling (
G3) — mutates a table column owned by another module behind its back; - Domain Pollution (
G5) — injects an undeclared magic string'risk_locked'that silently breaks downstreambilling/fulfillmentswitch statements.
Or an agent inside billing runs an in-place UPDATE payment_intents SET intent_state = 'reconciled' on an AppendOnlyFact audit table (G4), permanently erasing intent history so cross-system reconciliation becomes mathematically impossible.
In controlled live coding-agent trials (120 symmetric trials on the original 40-task suite), both AGENTS.md soft rules and executable import linters allowed 73.33% (22/30) of unsafe schema-boundary mutations. SchemaGate cut unsafe deliveries to 0.00% (0/30) ($p = 4.77 \times 10^{-7}$), drove 100% (20/20) single-turn agent self-repair via structured diagnostics, had 0/10 false blocks, and ran in ~1.08 ms at 4,096 contracted columns.
In a strictly disjoint cross-vendor, multi-language external holdout (5 unseen domains × Python & TypeScript/Prisma × Claude Sonnet 5.5, Kimi K3, DeepSeek Chat = 180 arm evaluations), AGENTS.md and import linters still let 62.22% (28/45) of unsafe patches slip through (Claude 86.7%, Kimi 80.0%, DeepSeek 20.0%; Python 58.3%, TypeScript/Prisma 66.7%). SchemaGate cut unsafe deliveries to 0.00% (0/45) ($p = 7.45 \times 10^{-9}$), achieved 100% (28/28) 1-turn auto-repair across all three vendors and both languages, with 0/15 false blocks.
Five Enforced Invariants (G1–G5)
Formally verified in Isabelle/HOL 2026 (session RC_SchemaBoundary, zero sorry):
| Gate | Invariant | What It Blocks |
|---|---|---|
| G1 | Syntactic Import Compliance | Forbidden cross-module import / require edges |
| G2 | Concept Decompression | Overloading one column with multiple domain concepts |
| G3 | Single-Writer Ownership | Foreign modules writing to a column owned by another module |
| G4 | Append-Only History Integrity | Destructive UPDATE, DELETE, REPLACE, or DROP COLUMN on AppendOnlyFact ledgers |
| G5 | Domain Closure | Writing undeclared sentinel/magic tags outside a column's allowed_tags |
Supported Languages & Surfaces
- Python (
.py): AST imports, raw & parameterized SQL (:name,%(name)s,%s,?,$1), f-strings / string concat, SQLAlchemy (update(Table).values(...),delete(Table),query(Model).update(...)), Django ORM (Model.objects...update(...),.delete()), attribute/subscript writes (user.status = ...). - TypeScript / JavaScript (
.ts,.tsx,.js,.mjs,.cjs): ES/CJS imports, raw SQL strings & template literals, Prisma (prisma.<model>.update/updateMany/upsert/delete/deleteMany), Drizzle (db.update(tbl).set(...),db.delete(tbl)). - SQL Migrations (
.sql):UPDATE,DELETE,REPLACE,INSERT,ALTER TABLE ... DROP COLUMN. - Runtime DB-API 2.0 SQL Proxy:
wrap_dbapi_connection(conn, contract, module_name)intercepts dynamically constructed SQL at test/staging runtime using the exact sameschema-contract.json.
Installation
Zero third-party dependencies (Python 3.10+ stdlib only):
pip install schema-boundary-gate
Or from source:
pip install -e .
Quickstart
1. CLI & CI Gate (Text / JSON / SARIF)
# Generate starter schema-contract.json
schema-gate init
# Check repository (exit 0 on PASS, exit 2 on BLOCK with repair hints)
schema-gate check --contract schema-contract.json src/
# Check only staged git files or PR diff against main
schema-gate check --contract schema-contract.json --staged
schema-gate check --contract schema-contract.json --diff origin/main --format sarif
Run the interactive multi-language + runtime-proxy demo:
bash examples/demo.sh
2. Claude Code PreToolUse Hook (1-Turn Auto-Repair)
Add to .claude/settings.json (see examples/claude-code-settings.json):
{
"hooks": {
"PreToolUse": [
{
"matcher": "Write|Edit|MultiEdit",
"hooks": [
{
"type": "command",
"command": "schema-gate hook claude-code --contract schema-contract.json"
}
]
}
]
}
}
When Claude Code attempts a shortcut mutation, schema-gate exits 2 before disk write with exact rule codes and repair hints so the agent automatically decompresses the write into an owned column on the next turn.
3. MCP Stdio Server (Claude Code / Cursor / Any MCP Agent)
{
"mcpServers": {
"schema-gate": {
"command": "schema-gate",
"args": ["mcp", "--contract", "schema-contract.json"]
}
}
}
Exposed MCP tools:
schema_gate_inspect_contract— inspect table owners, concepts, append-only tables, and allowed enum tags before codingschema_gate_check_patch— dry-run check a Python / TypeScript / SQL patch against G1–G5schema_gate_check_files— scan files or directoriesschema_gate_validate_sql— check a raw SQL statement + parameters
4. Runtime SQL Proxy (Dynamic SQL Protection in Tests)
import sqlite3
from schema_gate import SchemaContract, wrap_dbapi_connection
contract = SchemaContract.from_json_file("schema-contract.json")
conn = wrap_dbapi_connection(sqlite3.connect(":memory:"), contract, module_name="risk")
# Any dynamic SQL violating G2-G5 raises SchemaBoundaryViolationError before execution!
5. Pre-commit Hook
repos:
- repo: https://github.com/iannil/schema-gate
rev: v0.1.0
hooks:
- id: schema-gate
Running Tests
python3 -m unittest discover -s tests -v
Paper & Formal Proofs
- Paper: Tao Shi (Zhurong), Data as the Boundary: Schema-Boundary Contract Gates Against Silent Semantic Coupling in AI Coding Agents, https://zhurongshuo.com/en/papers/schema-boundary-gate/
- Isabelle/HOL 2026 Proofs (
RC_SchemaBoundary, zerosorry) & Benchmark Archive (Zenodo): 10.5281/zenodo.24459632
Metadata
Release files for schema-boundary-gate 0.1.1
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 |
|---|---|---|---|---|
| schema_boundary_gate-0.1.1.tar.gz | 26.1 kB | With provenance | Details for schema_boundary_gate-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| schema_boundary_gate-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for schema_boundary_gate-0.1.1-py3-none-any.whl |
Total release size: 52.5 kB
Release files / schema_boundary_gate-0.1.1.tar.gz
| Download URL | schema_boundary_gate-0.1.1.tar.gz |
|---|---|
| Size | 26.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2ba1dc31c1671dde5a6723c9dbcad7dfc4e3d7ffb9f780e58f628c6b51b41016
|
|
BLAKE2b-256 checksum How to use checksums |
f1d7e414ae829803e268032729e66315dfbaab70f3c33896b004777b3d1d6dd7
|
| 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 23, 2026.
Transparency logRelease files / schema_boundary_gate-0.1.1-py3-none-any.whl
| Download URL | schema_boundary_gate-0.1.1-py3-none-any.whl |
|---|---|
| Size | 26.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8e3e9c7bbaacd08418fd5cd3779c4c3897320f08280deaae8f4da91721e511da
|
|
BLAKE2b-256 checksum How to use checksums |
c9a56c23afdcd962039ed5825a60c992f66aec4af19776f75f3604e9f87e4fc5
|
| 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 23, 2026.
Transparency logSecurity for schema-boundary-gate 0.1.1
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 schema_boundary_gate-0.1.1.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 23, 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.