@gate-forge/pack-sqlalchemy 0.1.0
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.
- package/LICENSE +202 -0
- package/README.md +353 -0
- package/dist/adapter-schema.d.ts +84 -0
- package/dist/adapter-schema.d.ts.map +1 -0
- package/dist/adapter-schema.js +67 -0
- package/dist/adapter-schema.js.map +1 -0
- package/dist/detector.d.ts +116 -0
- package/dist/detector.d.ts.map +1 -0
- package/dist/detector.js +265 -0
- package/dist/detector.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -0
- package/dist/planes.d.ts +177 -0
- package/dist/planes.d.ts.map +1 -0
- package/dist/planes.js +280 -0
- package/dist/planes.js.map +1 -0
- package/dist/version.d.ts +12 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +12 -0
- package/dist/version.js.map +1 -0
- package/examples/example.accounts.adapter.mjs +97 -0
- package/package.json +45 -0
- package/python/gateforge_sqlalchemy_detector/__init__.py +68 -0
- package/python/gateforge_sqlalchemy_detector/__main__.py +50 -0
- package/python/gateforge_sqlalchemy_detector/__pycache__/__init__.cpython-312.pyc +0 -0
- package/python/gateforge_sqlalchemy_detector/__pycache__/__init__.cpython-314.pyc +0 -0
- package/python/gateforge_sqlalchemy_detector/__pycache__/__main__.cpython-312.pyc +0 -0
- package/python/gateforge_sqlalchemy_detector/__pycache__/__main__.cpython-314.pyc +0 -0
- package/python/gateforge_sqlalchemy_detector/__pycache__/scan.cpython-312.pyc +0 -0
- package/python/gateforge_sqlalchemy_detector/__pycache__/scan.cpython-314.pyc +0 -0
- package/python/gateforge_sqlalchemy_detector/scan.py +1446 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 UKD
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
# @gate-forge/pack-sqlalchemy
|
|
2
|
+
|
|
3
|
+
SQLAlchemy CRUD discovery pack: a Python detector that finds SQLAlchemy
|
|
4
|
+
tables from Python source **using only the stdlib `ast` module** — no
|
|
5
|
+
imports, no execution of application code, no SQLAlchemy dependency —
|
|
6
|
+
plus a TypeScript discover entry, the declarative `.gateforge/planes.json`
|
|
7
|
+
plane-evidence config (tenant/master/global), and the entity-adapter
|
|
8
|
+
schema with a sample adapter for the `example/` accounts app.
|
|
9
|
+
|
|
10
|
+
The detector speaks the resource graph's frozen vocabulary: business
|
|
11
|
+
`sqlalchemy.table` resources, `gateforge.class` symbol resources for
|
|
12
|
+
cross-module inheritance resolution, typed `unresolved` entries for
|
|
13
|
+
computed names (never a guess, never a silent drop — ADR 0001 D1), and
|
|
14
|
+
`DUPLICATE_TABLE_NAME` / `CLASS_NAME_REPEATED_IN_FILE` / `PARSE_ERROR`
|
|
15
|
+
findings (GF-20/GF-01/GF-19).
|
|
16
|
+
|
|
17
|
+
## How discovery works
|
|
18
|
+
|
|
19
|
+
`python/gateforge_sqlalchemy_detector/` parses each repo-relative path
|
|
20
|
+
with `ast.parse` and reports:
|
|
21
|
+
|
|
22
|
+
| Source construct | Emitted as |
|
|
23
|
+
| --- | --- |
|
|
24
|
+
| Declarative class with literal `__tablename__` (legacy `declarative_base()` and 2.0 `DeclarativeBase` styles) | `sqlalchemy.table` resource + class symbol |
|
|
25
|
+
| Direct `Table("name", ...)` call | `sqlalchemy.table` resource (provenance `table-call-first-arg`) |
|
|
26
|
+
| Abstract base (`__abstract__ = True`) or `DeclarativeBase` subclass with no table facts | class symbol only (`abstract: true` / base); never a business resource |
|
|
27
|
+
| Class whose tablename is a decorated function, f-string, call, name ref, or SQLModel `table=True` | class symbol with `tablenameUnresolved: true` + typed `unresolved` entry |
|
|
28
|
+
| Class with bases but no literal tablename anywhere | class symbol + `no_tablename_source` unresolved entry (the graph resolves through its repo-wide symbol table when a base carries a literal — same-file AND cross-file) |
|
|
29
|
+
| Plain classes (mixins, no bases, no table facts) | nothing |
|
|
30
|
+
| Malformed Python file | `PARSE_ERROR` finding with a line number; the file contributes no resources (GF-19) |
|
|
31
|
+
|
|
32
|
+
### Detector vocabulary
|
|
33
|
+
|
|
34
|
+
- **Business table resource** — `kind: "sqlalchemy.table"`, identity in
|
|
35
|
+
`attributes.resourceName`, plus `classQname`, `scope`, `tableName`,
|
|
36
|
+
`tablenameProvenance` (`literal` | `table-call-first-arg`),
|
|
37
|
+
`hasTableArgs`/`tableArgsSchema` (literal `schema` from
|
|
38
|
+
`__table_args__`), `tableKeywordTrue`, `abstract`, `baseNames`.
|
|
39
|
+
- **Class-symbol resource** — `kind: "gateforge.class"` with the graph's
|
|
40
|
+
canonical `qname` (dotted scope-qualified — function-local shadowing
|
|
41
|
+
never collapses), `resourceKind`, `baseNames`, `tableName`, `abstract`,
|
|
42
|
+
`tablenameUnresolved`.
|
|
43
|
+
- **Unresolved entries** — `{code, detail, location}` at the class
|
|
44
|
+
statement; codes: `computed_tablename` (decorator count + return
|
|
45
|
+
expression kind retained in full, GF-02), `table_name_derived_runtime`
|
|
46
|
+
(`table=True`), `no_tablename_source`. The graph retires an entry it
|
|
47
|
+
resolves and synthesizes `inherited_tablename_unresolved` when it
|
|
48
|
+
cannot.
|
|
49
|
+
- **Findings** — `DUPLICATE_TABLE_NAME` (2-file and 1-file variants,
|
|
50
|
+
GF-20), `CLASS_NAME_REPEATED_IN_FILE` (GF-01 non-collapse proof),
|
|
51
|
+
`PARSE_ERROR` (GF-19).
|
|
52
|
+
|
|
53
|
+
## Setup
|
|
54
|
+
|
|
55
|
+
Requires Python ≥ 3.11 (the detector and the GPP/3 client are
|
|
56
|
+
stdlib-only; no pip install is required in the monorepo — the client
|
|
57
|
+
bootstraps itself from `packages/plugin-protocol/python`).
|
|
58
|
+
|
|
59
|
+
Both transports run the **same** detector; pick one per project:
|
|
60
|
+
|
|
61
|
+
### Subprocess transport (GPP/3)
|
|
62
|
+
|
|
63
|
+
```yaml
|
|
64
|
+
# .gateforge.yml
|
|
65
|
+
plugins:
|
|
66
|
+
- id: gateforge.pack-sqlalchemy
|
|
67
|
+
version: 0.1.0
|
|
68
|
+
transport: subprocess
|
|
69
|
+
command: ['python3', '-m', 'gateforge_sqlalchemy_detector']
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
The command must resolve as a Python module, so the pack's `python/`
|
|
73
|
+
directory (and the plugin-protocol client) need to be on `PYTHONPATH`,
|
|
74
|
+
e.g. in your shell/environment:
|
|
75
|
+
|
|
76
|
+
```sh
|
|
77
|
+
export PYTHONPATH="$PWD/packages/pack-sqlalchemy/python:$PWD/packages/plugin-protocol/python"
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
(When only the pack's `python/` dir is on the path, `__main__.py`
|
|
81
|
+
bootstraps the sibling client from the monorepo layout automatically.)
|
|
82
|
+
The CLI spawns the plugin with the repo root as its working directory
|
|
83
|
+
and passes repo-root-relative paths; the `version` must match the pack
|
|
84
|
+
(`0.1.0`) — it is pinned at the GPP/3 handshake.
|
|
85
|
+
|
|
86
|
+
### In-process transport
|
|
87
|
+
|
|
88
|
+
```yaml
|
|
89
|
+
# .gateforge.yml
|
|
90
|
+
plugins:
|
|
91
|
+
- id: gateforge.pack-sqlalchemy
|
|
92
|
+
version: 0.1.0
|
|
93
|
+
transport: in-process
|
|
94
|
+
module: '@gate-forge/pack-sqlalchemy'
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
The pack's default export is the pinned `{ discover(paths) }` contract.
|
|
98
|
+
Internally it spawns the exact same python detector through the hardened
|
|
99
|
+
`PluginSession` host (GPP/3), with the `PYTHONPATH` computed from the
|
|
100
|
+
package's own location — zero environment setup. Requires a build of the
|
|
101
|
+
workspace first (`npm run build`) so the package exports resolve to
|
|
102
|
+
`dist/`.
|
|
103
|
+
|
|
104
|
+
Both entries emit byte-identical discovery output for the same repo
|
|
105
|
+
state — `test/in-process.test.ts` asserts this.
|
|
106
|
+
|
|
107
|
+
## Plane evidence config (`.gateforge/planes.json`)
|
|
108
|
+
|
|
109
|
+
Every business resource must carry plane evidence — `tenant`, `master`,
|
|
110
|
+
or `global` — before the classifier will decide anything about it; a
|
|
111
|
+
table with no plane is a typed `PLANE_UNRESOLVED` blocker. This pack is
|
|
112
|
+
where that evidence comes from: a declarative, **human-reviewed** JSON
|
|
113
|
+
config at the repo root. It feeds the same `attributes.plane` mechanism
|
|
114
|
+
as the programmatic factory option, so graph ids qualify as
|
|
115
|
+
`plane.name` (e.g. `master.accounts`) exactly as before.
|
|
116
|
+
|
|
117
|
+
```json
|
|
118
|
+
{
|
|
119
|
+
"rules": [
|
|
120
|
+
{
|
|
121
|
+
"match": "backend/admin_platform/models/**",
|
|
122
|
+
"plane": "master",
|
|
123
|
+
"reason": "AdminBase control-plane models (master database)"
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
"tables": ["master_users", "erp_client_configs"],
|
|
127
|
+
"plane": "master",
|
|
128
|
+
"reason": "platform control-plane tables"
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
"match": "backend/models/**",
|
|
132
|
+
"plane": "tenant",
|
|
133
|
+
"reason": "per-tenant database models"
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
"match": "backend/api/v1/**",
|
|
137
|
+
"exclude": ["backend/api/v1/public_payment.py", "backend/api/v1/master_admin.py"],
|
|
138
|
+
"plane": "tenant",
|
|
139
|
+
"reason": "contractor-scoped routers except the global-ingress files"
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
"match": "backend/api/v1/public_payment.py",
|
|
143
|
+
"plane": "global",
|
|
144
|
+
"reason": "public payment ingress is plane-global"
|
|
145
|
+
}
|
|
146
|
+
]
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
This example is deliberate: `master_users` and `erp_client_configs`
|
|
151
|
+
also live under `backend/models/`, so the catch-all glob matches them
|
|
152
|
+
too — the explicit `tables` rule wins for them (see evaluation
|
|
153
|
+
semantics below), and the glob covers only the tables it alone claims.
|
|
154
|
+
Likewise `backend/api/v1/` mixes contractor-scoped routers with
|
|
155
|
+
global-ingress files; without `exclude` the per-file `global` rule
|
|
156
|
+
would collide with the directory `tenant` glob inside the glob tier
|
|
157
|
+
and block as `PLANE_RULE_CONTRADICTION` — the exclusion documents the
|
|
158
|
+
exception on the directory rule instead.
|
|
159
|
+
|
|
160
|
+
### Schema
|
|
161
|
+
|
|
162
|
+
One document: `{ "rules": [rule, ...] }`. Every rule carries:
|
|
163
|
+
|
|
164
|
+
| Field | Meaning |
|
|
165
|
+
| --- | --- |
|
|
166
|
+
| `match` | Repo-root-relative glob over the resource's **source file path** — the same glob semantics as core's classifier (`*` within one segment, `**` across segments, `?` one character; whole-path, case-sensitive). |
|
|
167
|
+
| `exclude` | Optional, **only together with `match`**: a non-empty list of repo-root-relative globs (same semantics) pruning whole **source files** from the rule's surface — if the resource's source path matches ANY of them, the rule does not apply at all. Each entry is validated like `match` (absolute / backslash / `..` patterns throw). |
|
|
168
|
+
| `tables` | Explicit list matched against the table `resourceName` **or** the class simple name (`ErpClient` matches table `erp_clients`). Matching either is deliberate: table names and class names are both identity evidence for the same resource. |
|
|
169
|
+
| `plane` | Strictly `tenant`, `master`, or `global` (validated). |
|
|
170
|
+
| `reason` | Required non-empty string — the human review artifact. It rides conflict diagnostics verbatim. |
|
|
171
|
+
|
|
172
|
+
A rule carries **exactly one** of `match`/`tables` (both or neither is a
|
|
173
|
+
read-time error); `exclude` is rejected on a `tables` rule — it prunes
|
|
174
|
+
the surface of a source-path glob, so on an enumeration it would be
|
|
175
|
+
dead config, and dead config in a review artifact reads as a review
|
|
176
|
+
that never happened. Rules apply to business `sqlalchemy.table`
|
|
177
|
+
resources only — `gateforge.class` symbols never carry a plane.
|
|
178
|
+
|
|
179
|
+
### Evaluation semantics (deterministic, fail closed)
|
|
180
|
+
|
|
181
|
+
Rules resolve in **two tiers with explicit-beats-general precedence**:
|
|
182
|
+
|
|
183
|
+
- **Explicit tier first** — a table claimed by ANY `tables` rule
|
|
184
|
+
(explicit enumeration) resolves ONLY against `tables` rules;
|
|
185
|
+
`match` (glob) rules are ignored for it. The WHY: an enumerated name
|
|
186
|
+
list is a more specific, human-reviewed claim than a directory glob,
|
|
187
|
+
and a glob's `exclude` prunes source **files**, not table names —
|
|
188
|
+
a glob rule cannot enumerate name-level claims. Cross-tier overlap
|
|
189
|
+
therefore never reads as a contradiction: the explicit names win,
|
|
190
|
+
the glob keeps covering only the tables it alone claims.
|
|
191
|
+
- **Glob tier second** — `match` rules apply only to tables no explicit
|
|
192
|
+
rule claims. A `match` rule may carry `exclude`: source files matching
|
|
193
|
+
any exclusion glob are removed from the rule's surface BEFORE tier
|
|
194
|
+
collection, so an excluded file cannot collide with a narrower
|
|
195
|
+
per-file rule. WHY: real directory surfaces mix planes —
|
|
196
|
+
`backend/api/v1/**` holds contractor-scoped routers beside
|
|
197
|
+
global-ingress files (`public_payment.py`, `master_admin.py`) — and
|
|
198
|
+
without documented exceptions the narrow per-file rules collide with
|
|
199
|
+
the directory glob inside this tier and block as
|
|
200
|
+
`PLANE_RULE_CONTRADICTION`. The exclusions live in the reviewed
|
|
201
|
+
config (explicit, `reason`-backed, diff-visible) rather than being
|
|
202
|
+
resolved by silent rule precedence.
|
|
203
|
+
|
|
204
|
+
Within the deciding tier:
|
|
205
|
+
|
|
206
|
+
- **ALL matching rules of that tier are collected**, in config order.
|
|
207
|
+
- **Agreement** — every matching rule of the tier asserts the same
|
|
208
|
+
plane → it is applied; the graph then qualifies the id as
|
|
209
|
+
`plane.name`.
|
|
210
|
+
- **Conflict within the tier** — its rules disagree → a blocking
|
|
211
|
+
`PLANE_RULE_CONTRADICTION` finding names the table, every conflicting
|
|
212
|
+
plane, every reason, and every rule index, and NO plane is applied:
|
|
213
|
+
the table stays plane-unresolved and blocks. Fix the config; never
|
|
214
|
+
guess between planes.
|
|
215
|
+
- **No match** — nothing is applied and the resource stays
|
|
216
|
+
plane-unresolved → the classifier blocks it. This is deliberate
|
|
217
|
+
closed-world completeness: the config must cover **every** business
|
|
218
|
+
table, or the gate stays red.
|
|
219
|
+
|
|
220
|
+
In the example above, `master_users` and `erp_client_configs` are
|
|
221
|
+
claimed by the `tables` rule and get `master` even though the
|
|
222
|
+
`backend/models/**` glob also matches their files; tables under
|
|
223
|
+
`backend/models/` that are not enumerated get `tenant` from the glob;
|
|
224
|
+
`public_payment.py` is excluded from the `backend/api/v1/**` tenant
|
|
225
|
+
glob and gets `global` from its per-file rule, while the other routers
|
|
226
|
+
under `backend/api/v1/` keep `tenant`; a contradiction is reported only
|
|
227
|
+
when two rules of the SAME tier disagree for one resource (e.g. two
|
|
228
|
+
`tables` rules asserting different planes for one table, or two
|
|
229
|
+
`match` rules whose surfaces — after exclusions — still overlap).
|
|
230
|
+
|
|
231
|
+
### Reading behavior
|
|
232
|
+
|
|
233
|
+
The document is read once per `discover()` from the working directory
|
|
234
|
+
(absence is **normal** → byte-identical `NO_PLANE_MAPPING` behavior; a
|
|
235
|
+
malformed document, unknown key, bad plane, missing reason,
|
|
236
|
+
non-repo-relative glob (in `match` or `exclude`), or `exclude` on a
|
|
237
|
+
`tables` rule **throws**, and the CLI surfaces the error
|
|
238
|
+
instead of scanning with partial trust). Precedence: the programmatic
|
|
239
|
+
`plane` factory option wins and the config file is not read at all; an
|
|
240
|
+
explicit `planesConfig` option overrides the document; the default
|
|
241
|
+
document path can be moved with `planesConfigPath`.
|
|
242
|
+
|
|
243
|
+
## Plane and resource classification
|
|
244
|
+
|
|
245
|
+
The detector emits normalized facts, plane evidence (from
|
|
246
|
+
`.gateforge/planes.json`, above), and classification signals. It does not
|
|
247
|
+
read a per-resource classification file or attach project-configured business
|
|
248
|
+
meaning. The core classifier combines plane, identity, lifecycle, delete
|
|
249
|
+
semantics, exposure, and adapter signals with `.gateforge/classification-policy.yml`.
|
|
250
|
+
|
|
251
|
+
```yaml
|
|
252
|
+
# .gateforge/classification-policy.yml
|
|
253
|
+
schemaVersion: 1
|
|
254
|
+
scanRoots: ['backend/**/*.py']
|
|
255
|
+
trustedInternalEntryPoints: []
|
|
256
|
+
internalRules: []
|
|
257
|
+
declarations:
|
|
258
|
+
internality: gateforge:internal
|
|
259
|
+
volatileFields: []
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
Use `gateforge discover`, then `gateforge classify` and
|
|
263
|
+
`gateforge explain tenant.accounts` to inspect the effective decision and its
|
|
264
|
+
fingerprint. Unknown plane or identity is a typed blocking result; uncertainty
|
|
265
|
+
never silently suppresses obligations. `NO_PLANE_MAPPING` remains available for
|
|
266
|
+
tests that intentionally disable plane attribution (programmatic or
|
|
267
|
+
config-driven).
|
|
268
|
+
|
|
269
|
+
## Classification workflow (example app)
|
|
270
|
+
|
|
271
|
+
1. **Discover** — the detector finds `accounts` and emits normalized
|
|
272
|
+
resource facts plus classification signals.
|
|
273
|
+
2. **Classify** — the core classifier applies the repository-wide policy,
|
|
274
|
+
conservative defaults, and closed-world proofs. No resource entry is
|
|
275
|
+
hand-authored.
|
|
276
|
+
3. **Gate** — matching `user-facing` resources receive lifecycle-gated
|
|
277
|
+
`persistence:*` obligations. Missing identity, plane, delete semantics, or
|
|
278
|
+
adapter evidence produces a typed blocking result.
|
|
279
|
+
4. **Observe** — `gateforge discover --json`, `gateforge classify`,
|
|
280
|
+
`gateforge obligations --json`, and `gateforge check` trace detector →
|
|
281
|
+
classification → obligation → report. UI-semantic `crud:*` contracts
|
|
282
|
+
remain fail-closed until trusted UI observation exists.
|
|
283
|
+
|
|
284
|
+
## Entity adapter schema + sample
|
|
285
|
+
|
|
286
|
+
The evidence-witness adapter contract (interface pin #8): an adapter is
|
|
287
|
+
a reviewed, engine-side module at `.gateforge/adapters/<resourceId>.mjs`
|
|
288
|
+
whose default export is:
|
|
289
|
+
|
|
290
|
+
```ts
|
|
291
|
+
{
|
|
292
|
+
resourceId: string, // registry identity (matches the file name)
|
|
293
|
+
read(ctx: { baseUrl, headers? }, id): Promise<unknown>, // GET-only
|
|
294
|
+
normalize(body): { entityId, fields },
|
|
295
|
+
deletion: 'hard' | 'archive',
|
|
296
|
+
environmentFingerprint: string, // target marker the witness probes (GF-13)
|
|
297
|
+
}
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
The pack exports `EntityAdapterSchema` + `validateEntityAdapter`
|
|
301
|
+
(`src/adapter-schema.ts`) for fail-closed validation. Adapters execute
|
|
302
|
+
**engine-side** (witness service), their responses never reach the test
|
|
303
|
+
process, and a fingerprint mismatch rejects the record — never
|
|
304
|
+
`satisfied`.
|
|
305
|
+
|
|
306
|
+
**Sample** — `examples/example.accounts.adapter.mjs` implements the
|
|
307
|
+
`example.accounts` resource against the example app's read API:
|
|
308
|
+
`read` does `GET {baseUrl}/api/accounts/:id` (404 → `null`),
|
|
309
|
+
`normalize` projects `{entityId, fields:{id, status}}`, `deletion` is
|
|
310
|
+
`'archive'` (the app archives, never hard-deletes), and
|
|
311
|
+
`environmentFingerprint` is the `x-gateforge-env` marker the witness
|
|
312
|
+
expects on probe/read responses. Copy it to
|
|
313
|
+
`.gateforge/adapters/example.accounts.mjs` in a project that runs the
|
|
314
|
+
example app, and set the classifier's `evidenceAdapter: example.accounts`.
|
|
315
|
+
(Note: the graph's final resource id is the plane-qualified
|
|
316
|
+
`master.accounts`; the adapter's registry identity is the
|
|
317
|
+
company-resource name `example.accounts` per the witness registry
|
|
318
|
+
convention.)
|
|
319
|
+
|
|
320
|
+
## Adversarial coverage (GF fixtures)
|
|
321
|
+
|
|
322
|
+
| Case | Behaviour asserted in `test/` |
|
|
323
|
+
| --- | --- |
|
|
324
|
+
| GF-01 | Function-local `Row` × 4 → 4 distinct stable IDs, `CLASS_NAME_REPEATED_IN_FILE` finding, no collapse |
|
|
325
|
+
| GF-02 | 3 stacked decorators + default-valued calls → full reason (`3 decorator(s)`, return-expression kind), no truncation |
|
|
326
|
+
| GF-19 | Malformed file → `PARSE_ERROR` finding with line 6, no resources, no crash |
|
|
327
|
+
| GF-20 | `dupes` × 2 in one file + `shared_items` across two files → both `DUPLICATE_TABLE_NAME` variants, 3 distinct resources |
|
|
328
|
+
| GF-21 | declared_attr / f-string / call / name / `table=True` → typed unresolved entries, never absent |
|
|
329
|
+
| — | Declarative `.gateforge/planes.json` (phase 5): path/tables rules apply with explicit-beats-general precedence (`tables` enumeration beats an overlapping glob; conflicts fail closed WITHIN a tier), `exclude` carves mixed-plane directory surfaces (excluded files never collide with per-file rules), no-match stays plane-unresolved, config absence is byte-identical (`test/planes.test.ts`) |
|
|
330
|
+
| — | Cross-module inheritance resolved through the graph symbol table; genuinely computed chains stay typed-unresolved |
|
|
331
|
+
|
|
332
|
+
## Determinism
|
|
333
|
+
|
|
334
|
+
Same input paths + same file bytes → byte-identical discovery output
|
|
335
|
+
(plan invariant 7): stdlib-only, no clock/network/randomness, every
|
|
336
|
+
array sorted, every name a literal from the AST, fixed key order on the
|
|
337
|
+
wire. `test/subprocess.test.ts` asserts byte-identity across fresh
|
|
338
|
+
GPP/3 sessions. The default in-process entry reads the project config at
|
|
339
|
+
discover time, so identical repo state yields identical output.
|
|
340
|
+
|
|
341
|
+
## Development
|
|
342
|
+
|
|
343
|
+
```sh
|
|
344
|
+
npm test # from this dir (vitest; spawns python3)
|
|
345
|
+
npm run typecheck
|
|
346
|
+
npm run build # tsc → dist/
|
|
347
|
+
python3 -m gateforge_sqlalchemy_detector <root> # ad-hoc serve (needs PYTHONPATH)
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
Layout: `python/gateforge_sqlalchemy_detector/` (detector + serve entry),
|
|
351
|
+
`src/` (TS discover entry, plane mapping, adapter schema), `test/`
|
|
352
|
+
(fixtures + vitest suites), `examples/` (sample adapter),
|
|
353
|
+
`pyproject.toml` (pip-installable detector package).
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Entity-adapter schema (pack deliverable: "entity-adapter schema doc").
|
|
3
|
+
*
|
|
4
|
+
* The evidence-witness contract (interface pin #8, ADR 0002): an
|
|
5
|
+
* adapter is a reviewed, engine-side module whose default export
|
|
6
|
+
* executes GET-only reads against the target resource. The witness
|
|
7
|
+
* service loads `.gateforge/adapters/<resourceId>.mjs` and calls
|
|
8
|
+
* `read(ctx, id)` when a test asks for persistence evidence; the raw
|
|
9
|
+
* adapter response is never returned to the test process — only the
|
|
10
|
+
* `verdictRelevant` projection is.
|
|
11
|
+
*
|
|
12
|
+
* Adapter responsibilities (mirrored by {@link EntityAdapterSchema} and
|
|
13
|
+
* enforced by {@link validateEntityAdapter}):
|
|
14
|
+
* - `resourceId` — the resource this adapter proves; must match the
|
|
15
|
+
* file name (`<resourceId>.mjs`) so the registry binds by filename.
|
|
16
|
+
* - `read(ctx, id)` — GET-only fetch of one entity; returns the raw
|
|
17
|
+
* response body. Implementations MUST NOT mutate anything.
|
|
18
|
+
* - `normalize(body)` — project the raw body onto the evidence shape
|
|
19
|
+
* `{ entityId, fields }` where `fields` carry the classified
|
|
20
|
+
* `primaryKey` columns.
|
|
21
|
+
* - `deletion: 'hard' | 'archive'` — how removal manifests (archive =
|
|
22
|
+
* soft delete: the row keeps existing retrievable).
|
|
23
|
+
* - `environmentFingerprint` — the target-environment marker (e.g. the
|
|
24
|
+
* value of a response header the witness compares on a probe GET);
|
|
25
|
+
* a fingerprint mismatch rejects the record (GF-13).
|
|
26
|
+
*/
|
|
27
|
+
import { z } from 'zod';
|
|
28
|
+
/** The evidence shape `normalize` must return. */
|
|
29
|
+
export interface NormalizedEntity {
|
|
30
|
+
/** The entity id, extracted from the raw body (column-keyed). */
|
|
31
|
+
entityId: string;
|
|
32
|
+
/** Projected fields the obligation's `expectFields` can match. */
|
|
33
|
+
fields: Record<string, unknown>;
|
|
34
|
+
}
|
|
35
|
+
/** Context handed to `read` by the witness service. */
|
|
36
|
+
export interface EntityAdapterContext {
|
|
37
|
+
/** Base URL of the target environment (loopback, trusted). */
|
|
38
|
+
baseUrl: string;
|
|
39
|
+
/** Per-request headers (e.g. the run token for authenticity). */
|
|
40
|
+
headers?: Record<string, string>;
|
|
41
|
+
}
|
|
42
|
+
/** The frozen adapter module contract (pin #8). */
|
|
43
|
+
export interface EntityAdapter {
|
|
44
|
+
resourceId: string;
|
|
45
|
+
read(ctx: EntityAdapterContext, id: string): Promise<unknown>;
|
|
46
|
+
normalize(body: unknown): NormalizedEntity;
|
|
47
|
+
deletion: 'hard' | 'archive';
|
|
48
|
+
environmentFingerprint: string;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Zod schema for the data fields of an adapter module (functions are
|
|
52
|
+
* checked by {@link validateEntityAdapter} for a single-cause diagnostic
|
|
53
|
+
* list; zod function schemas would blur the message).
|
|
54
|
+
*/
|
|
55
|
+
export declare const EntityAdapterSchema: z.ZodObject<{
|
|
56
|
+
resourceId: z.ZodString;
|
|
57
|
+
deletion: z.ZodEnum<{
|
|
58
|
+
hard: "hard";
|
|
59
|
+
archive: "archive";
|
|
60
|
+
}>;
|
|
61
|
+
environmentFingerprint: z.ZodString;
|
|
62
|
+
read: z.ZodUnknown;
|
|
63
|
+
normalize: z.ZodUnknown;
|
|
64
|
+
}, z.core.$strict>;
|
|
65
|
+
/** Outcome of {@link validateEntityAdapter}. */
|
|
66
|
+
export type EntityAdapterValidation = {
|
|
67
|
+
ok: true;
|
|
68
|
+
adapter: EntityAdapter;
|
|
69
|
+
} | {
|
|
70
|
+
ok: false;
|
|
71
|
+
issues: string[];
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Validates an adapter module against the frozen contract, failing
|
|
75
|
+
* closed with one issue per violated field.
|
|
76
|
+
*
|
|
77
|
+
* Args:
|
|
78
|
+
* value: The default export of an `.mjs` adapter module.
|
|
79
|
+
*
|
|
80
|
+
* Returns:
|
|
81
|
+
* EntityAdapterValidation: The typed adapter, or every issue found.
|
|
82
|
+
*/
|
|
83
|
+
export declare function validateEntityAdapter(value: unknown): EntityAdapterValidation;
|
|
84
|
+
//# sourceMappingURL=adapter-schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter-schema.d.ts","sourceRoot":"","sources":["../src/adapter-schema.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,kDAAkD;AAClD,MAAM,WAAW,gBAAgB;IAC/B,iEAAiE;IACjE,QAAQ,EAAE,MAAM,CAAC;IACjB,kEAAkE;IAClE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACjC;AAED,uDAAuD;AACvD,MAAM,WAAW,oBAAoB;IACnC,8DAA8D;IAC9D,OAAO,EAAE,MAAM,CAAC;IAChB,iEAAiE;IACjE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClC;AAED,mDAAmD;AACnD,MAAM,WAAW,aAAa;IAC5B,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,CAAC,GAAG,EAAE,oBAAoB,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAC9D,SAAS,CAAC,IAAI,EAAE,OAAO,GAAG,gBAAgB,CAAC;IAC3C,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B,sBAAsB,EAAE,MAAM,CAAC;CAChC;AAED;;;;GAIG;AACH,eAAO,MAAM,mBAAmB;;;;;;;;;kBAQrB,CAAC;AAEZ,gDAAgD;AAChD,MAAM,MAAM,uBAAuB,GAC/B;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,OAAO,EAAE,aAAa,CAAA;CAAE,GACpC;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC;AAEpC;;;;;;;;;GASG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,OAAO,GAAG,uBAAuB,CAW7E"}
|