schema-boundary-gate 0.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- schema_boundary_gate-0.1.1/LICENSE +21 -0
- schema_boundary_gate-0.1.1/PKG-INFO +190 -0
- schema_boundary_gate-0.1.1/README.md +168 -0
- schema_boundary_gate-0.1.1/pyproject.toml +37 -0
- schema_boundary_gate-0.1.1/setup.cfg +4 -0
- schema_boundary_gate-0.1.1/src/schema_boundary_gate.egg-info/PKG-INFO +190 -0
- schema_boundary_gate-0.1.1/src/schema_boundary_gate.egg-info/SOURCES.txt +16 -0
- schema_boundary_gate-0.1.1/src/schema_boundary_gate.egg-info/dependency_links.txt +1 -0
- schema_boundary_gate-0.1.1/src/schema_boundary_gate.egg-info/entry_points.txt +3 -0
- schema_boundary_gate-0.1.1/src/schema_boundary_gate.egg-info/top_level.txt +1 -0
- schema_boundary_gate-0.1.1/src/schema_gate/__init__.py +40 -0
- schema_boundary_gate-0.1.1/src/schema_gate/cli.py +129 -0
- schema_boundary_gate-0.1.1/src/schema_gate/contract.py +299 -0
- schema_boundary_gate-0.1.1/src/schema_gate/engine.py +864 -0
- schema_boundary_gate-0.1.1/src/schema_gate/hooks.py +89 -0
- schema_boundary_gate-0.1.1/src/schema_gate/mcp_server.py +186 -0
- schema_boundary_gate-0.1.1/src/schema_gate/runtime_proxy.py +160 -0
- schema_boundary_gate-0.1.1/tests/test_schema_gate.py +286 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tao Shi (Zhurong)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: schema-boundary-gate
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Fail-closed Schema-Boundary Contract Gate for AI coding agents: blocks silent shared-column concept overload, foreign-writer mutations, and append-only ledger overwrites across Python, TypeScript/Prisma/Drizzle, SQL migrations, and runtime DB-API.
|
|
5
|
+
Author: Tao Shi (Zhurong)
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/iannil/schema-gate
|
|
8
|
+
Project-URL: Repository, https://github.com/iannil/schema-gate
|
|
9
|
+
Project-URL: Paper, https://zhurongshuo.com/en/papers/schema-boundary-gate/
|
|
10
|
+
Project-URL: Zenodo Formal Proofs & Dataset, https://doi.org/10.5281/zenodo.24459632
|
|
11
|
+
Keywords: ai-coding-agents,schema-boundary,static-analysis,mcp,claude-code,sql,prisma,drizzle,architecture-governance
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
17
|
+
Classifier: Topic :: Database
|
|
18
|
+
Requires-Python: >=3.10
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
License-File: LICENSE
|
|
21
|
+
Dynamic: license-file
|
|
22
|
+
|
|
23
|
+
# schema-gate
|
|
24
|
+
|
|
25
|
+
[](https://github.com/iannil/schema-gate/actions/workflows/ci.yml)
|
|
26
|
+
[](https://pypi.org/project/schema-boundary-gate/)
|
|
27
|
+
[](LICENSE)
|
|
28
|
+
[](https://www.python.org/downloads/)
|
|
29
|
+
[-brightgreen.svg)](pyproject.toml)
|
|
30
|
+
|
|
31
|
+
**Fail-Closed Schema-Boundary Contract Gate for AI Coding Agents**
|
|
32
|
+
*(Zero-dependency Python 3 library, CLI, Claude Code Hook, MCP stdio server, SARIF CI gate & Runtime DB-API SQL Proxy)*
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## Why `AGENTS.md` and Import Linters Are Not Enough
|
|
37
|
+
|
|
38
|
+
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:
|
|
39
|
+
|
|
40
|
+
```sql
|
|
41
|
+
UPDATE users SET status = 'risk_locked' WHERE id = :entity_id
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Zero forbidden imports. Unit tests pass. Yet this single line breaks three architectural invariants at once:
|
|
45
|
+
1. **Concept Compression (`G2`)** — stuffs fraud-risk state into `auth`'s account-lifecycle column;
|
|
46
|
+
2. **Foreign-Writer Coupling (`G3`)** — mutates a table column owned by another module behind its back;
|
|
47
|
+
3. **Domain Pollution (`G5`)** — injects an undeclared magic string `'risk_locked'` that silently breaks downstream `billing` / `fulfillment` switch statements.
|
|
48
|
+
|
|
49
|
+
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.
|
|
50
|
+
|
|
51
|
+
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.
|
|
52
|
+
|
|
53
|
+
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**.
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
## Five Enforced Invariants (`G1`–`G5`)
|
|
58
|
+
|
|
59
|
+
Formally verified in **Isabelle/HOL 2026** (session `RC_SchemaBoundary`, zero `sorry`):
|
|
60
|
+
|
|
61
|
+
| Gate | Invariant | What It Blocks |
|
|
62
|
+
|---|---|---|
|
|
63
|
+
| **G1** | Syntactic Import Compliance | Forbidden cross-module `import` / `require` edges |
|
|
64
|
+
| **G2** | Concept Decompression | Overloading one column with multiple domain concepts |
|
|
65
|
+
| **G3** | Single-Writer Ownership | Foreign modules writing to a column owned by another module |
|
|
66
|
+
| **G4** | Append-Only History Integrity | Destructive `UPDATE`, `DELETE`, `REPLACE`, or `DROP COLUMN` on `AppendOnlyFact` ledgers |
|
|
67
|
+
| **G5** | Domain Closure | Writing undeclared sentinel/magic tags outside a column's `allowed_tags` |
|
|
68
|
+
|
|
69
|
+
## Supported Languages & Surfaces
|
|
70
|
+
|
|
71
|
+
* **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 = ...`).
|
|
72
|
+
* **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)`).
|
|
73
|
+
* **SQL Migrations (`.sql`)**: `UPDATE`, `DELETE`, `REPLACE`, `INSERT`, `ALTER TABLE ... DROP COLUMN`.
|
|
74
|
+
* **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 same `schema-contract.json`.
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
## Installation
|
|
79
|
+
|
|
80
|
+
Zero third-party dependencies (Python 3.10+ stdlib only):
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
pip install schema-boundary-gate
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Or from source:
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
pip install -e .
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
---
|
|
93
|
+
|
|
94
|
+
## Quickstart
|
|
95
|
+
|
|
96
|
+
### 1. CLI & CI Gate (Text / JSON / SARIF)
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
# Generate starter schema-contract.json
|
|
100
|
+
schema-gate init
|
|
101
|
+
|
|
102
|
+
# Check repository (exit 0 on PASS, exit 2 on BLOCK with repair hints)
|
|
103
|
+
schema-gate check --contract schema-contract.json src/
|
|
104
|
+
|
|
105
|
+
# Check only staged git files or PR diff against main
|
|
106
|
+
schema-gate check --contract schema-contract.json --staged
|
|
107
|
+
schema-gate check --contract schema-contract.json --diff origin/main --format sarif
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Run the interactive multi-language + runtime-proxy demo:
|
|
111
|
+
```bash
|
|
112
|
+
bash examples/demo.sh
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### 2. Claude Code `PreToolUse` Hook (1-Turn Auto-Repair)
|
|
116
|
+
|
|
117
|
+
Add to `.claude/settings.json` (see `examples/claude-code-settings.json`):
|
|
118
|
+
|
|
119
|
+
```json
|
|
120
|
+
{
|
|
121
|
+
"hooks": {
|
|
122
|
+
"PreToolUse": [
|
|
123
|
+
{
|
|
124
|
+
"matcher": "Write|Edit|MultiEdit",
|
|
125
|
+
"hooks": [
|
|
126
|
+
{
|
|
127
|
+
"type": "command",
|
|
128
|
+
"command": "schema-gate hook claude-code --contract schema-contract.json"
|
|
129
|
+
}
|
|
130
|
+
]
|
|
131
|
+
}
|
|
132
|
+
]
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
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.
|
|
138
|
+
|
|
139
|
+
### 3. MCP Stdio Server (Claude Code / Cursor / Any MCP Agent)
|
|
140
|
+
|
|
141
|
+
```json
|
|
142
|
+
{
|
|
143
|
+
"mcpServers": {
|
|
144
|
+
"schema-gate": {
|
|
145
|
+
"command": "schema-gate",
|
|
146
|
+
"args": ["mcp", "--contract", "schema-contract.json"]
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Exposed MCP tools:
|
|
153
|
+
* `schema_gate_inspect_contract` — inspect table owners, concepts, append-only tables, and allowed enum tags before coding
|
|
154
|
+
* `schema_gate_check_patch` — dry-run check a Python / TypeScript / SQL patch against G1–G5
|
|
155
|
+
* `schema_gate_check_files` — scan files or directories
|
|
156
|
+
* `schema_gate_validate_sql` — check a raw SQL statement + parameters
|
|
157
|
+
|
|
158
|
+
### 4. Runtime SQL Proxy (Dynamic SQL Protection in Tests)
|
|
159
|
+
|
|
160
|
+
```python
|
|
161
|
+
import sqlite3
|
|
162
|
+
from schema_gate import SchemaContract, wrap_dbapi_connection
|
|
163
|
+
|
|
164
|
+
contract = SchemaContract.from_json_file("schema-contract.json")
|
|
165
|
+
conn = wrap_dbapi_connection(sqlite3.connect(":memory:"), contract, module_name="risk")
|
|
166
|
+
# Any dynamic SQL violating G2-G5 raises SchemaBoundaryViolationError before execution!
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### 5. Pre-commit Hook
|
|
170
|
+
|
|
171
|
+
```yaml
|
|
172
|
+
repos:
|
|
173
|
+
- repo: https://github.com/iannil/schema-gate
|
|
174
|
+
rev: v0.1.0
|
|
175
|
+
hooks:
|
|
176
|
+
- id: schema-gate
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
---
|
|
180
|
+
|
|
181
|
+
## Running Tests
|
|
182
|
+
|
|
183
|
+
```bash
|
|
184
|
+
python3 -m unittest discover -s tests -v
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## Paper & Formal Proofs
|
|
188
|
+
|
|
189
|
+
* **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/
|
|
190
|
+
* **Isabelle/HOL 2026 Proofs (`RC_SchemaBoundary`, zero `sorry`) & Benchmark Archive (Zenodo):** [10.5281/zenodo.24459632](https://doi.org/10.5281/zenodo.24459632)
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# schema-gate
|
|
2
|
+
|
|
3
|
+
[](https://github.com/iannil/schema-gate/actions/workflows/ci.yml)
|
|
4
|
+
[](https://pypi.org/project/schema-boundary-gate/)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
[](https://www.python.org/downloads/)
|
|
7
|
+
[-brightgreen.svg)](pyproject.toml)
|
|
8
|
+
|
|
9
|
+
**Fail-Closed Schema-Boundary Contract Gate for AI Coding Agents**
|
|
10
|
+
*(Zero-dependency Python 3 library, CLI, Claude Code Hook, MCP stdio server, SARIF CI gate & Runtime DB-API SQL Proxy)*
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## Why `AGENTS.md` and Import Linters Are Not Enough
|
|
15
|
+
|
|
16
|
+
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:
|
|
17
|
+
|
|
18
|
+
```sql
|
|
19
|
+
UPDATE users SET status = 'risk_locked' WHERE id = :entity_id
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Zero forbidden imports. Unit tests pass. Yet this single line breaks three architectural invariants at once:
|
|
23
|
+
1. **Concept Compression (`G2`)** — stuffs fraud-risk state into `auth`'s account-lifecycle column;
|
|
24
|
+
2. **Foreign-Writer Coupling (`G3`)** — mutates a table column owned by another module behind its back;
|
|
25
|
+
3. **Domain Pollution (`G5`)** — injects an undeclared magic string `'risk_locked'` that silently breaks downstream `billing` / `fulfillment` switch statements.
|
|
26
|
+
|
|
27
|
+
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.
|
|
28
|
+
|
|
29
|
+
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.
|
|
30
|
+
|
|
31
|
+
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**.
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Five Enforced Invariants (`G1`–`G5`)
|
|
36
|
+
|
|
37
|
+
Formally verified in **Isabelle/HOL 2026** (session `RC_SchemaBoundary`, zero `sorry`):
|
|
38
|
+
|
|
39
|
+
| Gate | Invariant | What It Blocks |
|
|
40
|
+
|---|---|---|
|
|
41
|
+
| **G1** | Syntactic Import Compliance | Forbidden cross-module `import` / `require` edges |
|
|
42
|
+
| **G2** | Concept Decompression | Overloading one column with multiple domain concepts |
|
|
43
|
+
| **G3** | Single-Writer Ownership | Foreign modules writing to a column owned by another module |
|
|
44
|
+
| **G4** | Append-Only History Integrity | Destructive `UPDATE`, `DELETE`, `REPLACE`, or `DROP COLUMN` on `AppendOnlyFact` ledgers |
|
|
45
|
+
| **G5** | Domain Closure | Writing undeclared sentinel/magic tags outside a column's `allowed_tags` |
|
|
46
|
+
|
|
47
|
+
## Supported Languages & Surfaces
|
|
48
|
+
|
|
49
|
+
* **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 = ...`).
|
|
50
|
+
* **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)`).
|
|
51
|
+
* **SQL Migrations (`.sql`)**: `UPDATE`, `DELETE`, `REPLACE`, `INSERT`, `ALTER TABLE ... DROP COLUMN`.
|
|
52
|
+
* **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 same `schema-contract.json`.
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## Installation
|
|
57
|
+
|
|
58
|
+
Zero third-party dependencies (Python 3.10+ stdlib only):
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
pip install schema-boundary-gate
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Or from source:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
pip install -e .
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## Quickstart
|
|
73
|
+
|
|
74
|
+
### 1. CLI & CI Gate (Text / JSON / SARIF)
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
# Generate starter schema-contract.json
|
|
78
|
+
schema-gate init
|
|
79
|
+
|
|
80
|
+
# Check repository (exit 0 on PASS, exit 2 on BLOCK with repair hints)
|
|
81
|
+
schema-gate check --contract schema-contract.json src/
|
|
82
|
+
|
|
83
|
+
# Check only staged git files or PR diff against main
|
|
84
|
+
schema-gate check --contract schema-contract.json --staged
|
|
85
|
+
schema-gate check --contract schema-contract.json --diff origin/main --format sarif
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Run the interactive multi-language + runtime-proxy demo:
|
|
89
|
+
```bash
|
|
90
|
+
bash examples/demo.sh
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### 2. Claude Code `PreToolUse` Hook (1-Turn Auto-Repair)
|
|
94
|
+
|
|
95
|
+
Add to `.claude/settings.json` (see `examples/claude-code-settings.json`):
|
|
96
|
+
|
|
97
|
+
```json
|
|
98
|
+
{
|
|
99
|
+
"hooks": {
|
|
100
|
+
"PreToolUse": [
|
|
101
|
+
{
|
|
102
|
+
"matcher": "Write|Edit|MultiEdit",
|
|
103
|
+
"hooks": [
|
|
104
|
+
{
|
|
105
|
+
"type": "command",
|
|
106
|
+
"command": "schema-gate hook claude-code --contract schema-contract.json"
|
|
107
|
+
}
|
|
108
|
+
]
|
|
109
|
+
}
|
|
110
|
+
]
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
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.
|
|
116
|
+
|
|
117
|
+
### 3. MCP Stdio Server (Claude Code / Cursor / Any MCP Agent)
|
|
118
|
+
|
|
119
|
+
```json
|
|
120
|
+
{
|
|
121
|
+
"mcpServers": {
|
|
122
|
+
"schema-gate": {
|
|
123
|
+
"command": "schema-gate",
|
|
124
|
+
"args": ["mcp", "--contract", "schema-contract.json"]
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Exposed MCP tools:
|
|
131
|
+
* `schema_gate_inspect_contract` — inspect table owners, concepts, append-only tables, and allowed enum tags before coding
|
|
132
|
+
* `schema_gate_check_patch` — dry-run check a Python / TypeScript / SQL patch against G1–G5
|
|
133
|
+
* `schema_gate_check_files` — scan files or directories
|
|
134
|
+
* `schema_gate_validate_sql` — check a raw SQL statement + parameters
|
|
135
|
+
|
|
136
|
+
### 4. Runtime SQL Proxy (Dynamic SQL Protection in Tests)
|
|
137
|
+
|
|
138
|
+
```python
|
|
139
|
+
import sqlite3
|
|
140
|
+
from schema_gate import SchemaContract, wrap_dbapi_connection
|
|
141
|
+
|
|
142
|
+
contract = SchemaContract.from_json_file("schema-contract.json")
|
|
143
|
+
conn = wrap_dbapi_connection(sqlite3.connect(":memory:"), contract, module_name="risk")
|
|
144
|
+
# Any dynamic SQL violating G2-G5 raises SchemaBoundaryViolationError before execution!
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### 5. Pre-commit Hook
|
|
148
|
+
|
|
149
|
+
```yaml
|
|
150
|
+
repos:
|
|
151
|
+
- repo: https://github.com/iannil/schema-gate
|
|
152
|
+
rev: v0.1.0
|
|
153
|
+
hooks:
|
|
154
|
+
- id: schema-gate
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
## Running Tests
|
|
160
|
+
|
|
161
|
+
```bash
|
|
162
|
+
python3 -m unittest discover -s tests -v
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## Paper & Formal Proofs
|
|
166
|
+
|
|
167
|
+
* **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/
|
|
168
|
+
* **Isabelle/HOL 2026 Proofs (`RC_SchemaBoundary`, zero `sorry`) & Benchmark Archive (Zenodo):** [10.5281/zenodo.24459632](https://doi.org/10.5281/zenodo.24459632)
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "schema-boundary-gate"
|
|
7
|
+
version = "0.1.1"
|
|
8
|
+
description = "Fail-closed Schema-Boundary Contract Gate for AI coding agents: blocks silent shared-column concept overload, foreign-writer mutations, and append-only ledger overwrites across Python, TypeScript/Prisma/Drizzle, SQL migrations, and runtime DB-API."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [
|
|
14
|
+
{name = "Tao Shi (Zhurong)"}
|
|
15
|
+
]
|
|
16
|
+
keywords = ["ai-coding-agents", "schema-boundary", "static-analysis", "mcp", "claude-code", "sql", "prisma", "drizzle", "architecture-governance"]
|
|
17
|
+
classifiers = [
|
|
18
|
+
"Development Status :: 4 - Beta",
|
|
19
|
+
"Intended Audience :: Developers",
|
|
20
|
+
"Intended Audience :: Science/Research",
|
|
21
|
+
"Programming Language :: Python :: 3",
|
|
22
|
+
"Topic :: Software Development :: Quality Assurance",
|
|
23
|
+
"Topic :: Database",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[project.urls]
|
|
27
|
+
Homepage = "https://github.com/iannil/schema-gate"
|
|
28
|
+
Repository = "https://github.com/iannil/schema-gate"
|
|
29
|
+
Paper = "https://zhurongshuo.com/en/papers/schema-boundary-gate/"
|
|
30
|
+
"Zenodo Formal Proofs & Dataset" = "https://doi.org/10.5281/zenodo.24459632"
|
|
31
|
+
|
|
32
|
+
[project.scripts]
|
|
33
|
+
schema-gate = "schema_gate.cli:main"
|
|
34
|
+
schema-boundary-gate = "schema_gate.cli:main"
|
|
35
|
+
|
|
36
|
+
[tool.setuptools.packages.find]
|
|
37
|
+
where = ["src"]
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: schema-boundary-gate
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Fail-closed Schema-Boundary Contract Gate for AI coding agents: blocks silent shared-column concept overload, foreign-writer mutations, and append-only ledger overwrites across Python, TypeScript/Prisma/Drizzle, SQL migrations, and runtime DB-API.
|
|
5
|
+
Author: Tao Shi (Zhurong)
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/iannil/schema-gate
|
|
8
|
+
Project-URL: Repository, https://github.com/iannil/schema-gate
|
|
9
|
+
Project-URL: Paper, https://zhurongshuo.com/en/papers/schema-boundary-gate/
|
|
10
|
+
Project-URL: Zenodo Formal Proofs & Dataset, https://doi.org/10.5281/zenodo.24459632
|
|
11
|
+
Keywords: ai-coding-agents,schema-boundary,static-analysis,mcp,claude-code,sql,prisma,drizzle,architecture-governance
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
17
|
+
Classifier: Topic :: Database
|
|
18
|
+
Requires-Python: >=3.10
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
License-File: LICENSE
|
|
21
|
+
Dynamic: license-file
|
|
22
|
+
|
|
23
|
+
# schema-gate
|
|
24
|
+
|
|
25
|
+
[](https://github.com/iannil/schema-gate/actions/workflows/ci.yml)
|
|
26
|
+
[](https://pypi.org/project/schema-boundary-gate/)
|
|
27
|
+
[](LICENSE)
|
|
28
|
+
[](https://www.python.org/downloads/)
|
|
29
|
+
[-brightgreen.svg)](pyproject.toml)
|
|
30
|
+
|
|
31
|
+
**Fail-Closed Schema-Boundary Contract Gate for AI Coding Agents**
|
|
32
|
+
*(Zero-dependency Python 3 library, CLI, Claude Code Hook, MCP stdio server, SARIF CI gate & Runtime DB-API SQL Proxy)*
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## Why `AGENTS.md` and Import Linters Are Not Enough
|
|
37
|
+
|
|
38
|
+
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:
|
|
39
|
+
|
|
40
|
+
```sql
|
|
41
|
+
UPDATE users SET status = 'risk_locked' WHERE id = :entity_id
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Zero forbidden imports. Unit tests pass. Yet this single line breaks three architectural invariants at once:
|
|
45
|
+
1. **Concept Compression (`G2`)** — stuffs fraud-risk state into `auth`'s account-lifecycle column;
|
|
46
|
+
2. **Foreign-Writer Coupling (`G3`)** — mutates a table column owned by another module behind its back;
|
|
47
|
+
3. **Domain Pollution (`G5`)** — injects an undeclared magic string `'risk_locked'` that silently breaks downstream `billing` / `fulfillment` switch statements.
|
|
48
|
+
|
|
49
|
+
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.
|
|
50
|
+
|
|
51
|
+
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.
|
|
52
|
+
|
|
53
|
+
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**.
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
## Five Enforced Invariants (`G1`–`G5`)
|
|
58
|
+
|
|
59
|
+
Formally verified in **Isabelle/HOL 2026** (session `RC_SchemaBoundary`, zero `sorry`):
|
|
60
|
+
|
|
61
|
+
| Gate | Invariant | What It Blocks |
|
|
62
|
+
|---|---|---|
|
|
63
|
+
| **G1** | Syntactic Import Compliance | Forbidden cross-module `import` / `require` edges |
|
|
64
|
+
| **G2** | Concept Decompression | Overloading one column with multiple domain concepts |
|
|
65
|
+
| **G3** | Single-Writer Ownership | Foreign modules writing to a column owned by another module |
|
|
66
|
+
| **G4** | Append-Only History Integrity | Destructive `UPDATE`, `DELETE`, `REPLACE`, or `DROP COLUMN` on `AppendOnlyFact` ledgers |
|
|
67
|
+
| **G5** | Domain Closure | Writing undeclared sentinel/magic tags outside a column's `allowed_tags` |
|
|
68
|
+
|
|
69
|
+
## Supported Languages & Surfaces
|
|
70
|
+
|
|
71
|
+
* **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 = ...`).
|
|
72
|
+
* **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)`).
|
|
73
|
+
* **SQL Migrations (`.sql`)**: `UPDATE`, `DELETE`, `REPLACE`, `INSERT`, `ALTER TABLE ... DROP COLUMN`.
|
|
74
|
+
* **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 same `schema-contract.json`.
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
## Installation
|
|
79
|
+
|
|
80
|
+
Zero third-party dependencies (Python 3.10+ stdlib only):
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
pip install schema-boundary-gate
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Or from source:
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
pip install -e .
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
---
|
|
93
|
+
|
|
94
|
+
## Quickstart
|
|
95
|
+
|
|
96
|
+
### 1. CLI & CI Gate (Text / JSON / SARIF)
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
# Generate starter schema-contract.json
|
|
100
|
+
schema-gate init
|
|
101
|
+
|
|
102
|
+
# Check repository (exit 0 on PASS, exit 2 on BLOCK with repair hints)
|
|
103
|
+
schema-gate check --contract schema-contract.json src/
|
|
104
|
+
|
|
105
|
+
# Check only staged git files or PR diff against main
|
|
106
|
+
schema-gate check --contract schema-contract.json --staged
|
|
107
|
+
schema-gate check --contract schema-contract.json --diff origin/main --format sarif
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Run the interactive multi-language + runtime-proxy demo:
|
|
111
|
+
```bash
|
|
112
|
+
bash examples/demo.sh
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### 2. Claude Code `PreToolUse` Hook (1-Turn Auto-Repair)
|
|
116
|
+
|
|
117
|
+
Add to `.claude/settings.json` (see `examples/claude-code-settings.json`):
|
|
118
|
+
|
|
119
|
+
```json
|
|
120
|
+
{
|
|
121
|
+
"hooks": {
|
|
122
|
+
"PreToolUse": [
|
|
123
|
+
{
|
|
124
|
+
"matcher": "Write|Edit|MultiEdit",
|
|
125
|
+
"hooks": [
|
|
126
|
+
{
|
|
127
|
+
"type": "command",
|
|
128
|
+
"command": "schema-gate hook claude-code --contract schema-contract.json"
|
|
129
|
+
}
|
|
130
|
+
]
|
|
131
|
+
}
|
|
132
|
+
]
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
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.
|
|
138
|
+
|
|
139
|
+
### 3. MCP Stdio Server (Claude Code / Cursor / Any MCP Agent)
|
|
140
|
+
|
|
141
|
+
```json
|
|
142
|
+
{
|
|
143
|
+
"mcpServers": {
|
|
144
|
+
"schema-gate": {
|
|
145
|
+
"command": "schema-gate",
|
|
146
|
+
"args": ["mcp", "--contract", "schema-contract.json"]
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Exposed MCP tools:
|
|
153
|
+
* `schema_gate_inspect_contract` — inspect table owners, concepts, append-only tables, and allowed enum tags before coding
|
|
154
|
+
* `schema_gate_check_patch` — dry-run check a Python / TypeScript / SQL patch against G1–G5
|
|
155
|
+
* `schema_gate_check_files` — scan files or directories
|
|
156
|
+
* `schema_gate_validate_sql` — check a raw SQL statement + parameters
|
|
157
|
+
|
|
158
|
+
### 4. Runtime SQL Proxy (Dynamic SQL Protection in Tests)
|
|
159
|
+
|
|
160
|
+
```python
|
|
161
|
+
import sqlite3
|
|
162
|
+
from schema_gate import SchemaContract, wrap_dbapi_connection
|
|
163
|
+
|
|
164
|
+
contract = SchemaContract.from_json_file("schema-contract.json")
|
|
165
|
+
conn = wrap_dbapi_connection(sqlite3.connect(":memory:"), contract, module_name="risk")
|
|
166
|
+
# Any dynamic SQL violating G2-G5 raises SchemaBoundaryViolationError before execution!
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### 5. Pre-commit Hook
|
|
170
|
+
|
|
171
|
+
```yaml
|
|
172
|
+
repos:
|
|
173
|
+
- repo: https://github.com/iannil/schema-gate
|
|
174
|
+
rev: v0.1.0
|
|
175
|
+
hooks:
|
|
176
|
+
- id: schema-gate
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
---
|
|
180
|
+
|
|
181
|
+
## Running Tests
|
|
182
|
+
|
|
183
|
+
```bash
|
|
184
|
+
python3 -m unittest discover -s tests -v
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## Paper & Formal Proofs
|
|
188
|
+
|
|
189
|
+
* **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/
|
|
190
|
+
* **Isabelle/HOL 2026 Proofs (`RC_SchemaBoundary`, zero `sorry`) & Benchmark Archive (Zenodo):** [10.5281/zenodo.24459632](https://doi.org/10.5281/zenodo.24459632)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/schema_boundary_gate.egg-info/PKG-INFO
|
|
5
|
+
src/schema_boundary_gate.egg-info/SOURCES.txt
|
|
6
|
+
src/schema_boundary_gate.egg-info/dependency_links.txt
|
|
7
|
+
src/schema_boundary_gate.egg-info/entry_points.txt
|
|
8
|
+
src/schema_boundary_gate.egg-info/top_level.txt
|
|
9
|
+
src/schema_gate/__init__.py
|
|
10
|
+
src/schema_gate/cli.py
|
|
11
|
+
src/schema_gate/contract.py
|
|
12
|
+
src/schema_gate/engine.py
|
|
13
|
+
src/schema_gate/hooks.py
|
|
14
|
+
src/schema_gate/mcp_server.py
|
|
15
|
+
src/schema_gate/runtime_proxy.py
|
|
16
|
+
tests/test_schema_gate.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
schema_gate
|