@burnsred/entity-sync 0.0.0-stage → 3.0.0-rc.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/.claude-plugin/marketplace.json +14 -0
- package/.claude-plugin/plugin.json +11 -0
- package/CHANGELOG.md +15 -0
- package/LICENSE +21 -0
- package/README.md +240 -2
- package/agents/entity-editor.md +16 -0
- package/bin/entity-sync.mjs +36 -0
- package/dist/cli.d.mts +20 -0
- package/dist/commands/asserts.d.mts +4 -0
- package/dist/commands/check.d.mts +6 -0
- package/dist/commands/inspect.d.mts +30 -0
- package/dist/commands/plan.d.mts +28 -0
- package/dist/commands/schema.d.mts +39 -0
- package/dist/config.d.mts +175 -0
- package/dist/index.d.mts +5 -0
- package/dist/lib/cache.d.mts +120 -0
- package/dist/lib/diff.d.mts +228 -0
- package/dist/lib/field-map.d.mts +93 -0
- package/dist/lib/gen-asserts.d.mts +70 -0
- package/dist/lib/graph.d.mts +90 -0
- package/dist/lib/parse-entities.d.mts +257 -0
- package/dist/lib/parse-schema.d.mts +98 -0
- package/dist/lib/resolve.d.mts +135 -0
- package/dist/project.d.mts +60 -0
- package/hooks/editor-guard.mjs +86 -0
- package/hooks/hooks.json +15 -0
- package/package.json +54 -4
- package/skills/entity/SKILL.md +105 -0
- package/skills/entity/reference/create.md +74 -0
- package/skills/entity/reference/editor.md +80 -0
- package/skills/entity/reference/enums.md +89 -0
- package/skills/entity/reference/fields.md +101 -0
- package/skills/entity/reference/sync.md +191 -0
- package/src/cli.mjs +110 -0
- package/src/commands/asserts.mjs +52 -0
- package/src/commands/check.mjs +88 -0
- package/src/commands/inspect.mjs +290 -0
- package/src/commands/plan.mjs +542 -0
- package/src/commands/schema.mjs +206 -0
- package/src/config.mjs +205 -0
- package/src/index.mjs +19 -0
- package/src/lib/cache.mjs +323 -0
- package/src/lib/diff.mjs +706 -0
- package/src/lib/field-map.mjs +438 -0
- package/src/lib/gen-asserts.mjs +478 -0
- package/src/lib/graph.mjs +284 -0
- package/src/lib/parse-entities.mjs +935 -0
- package/src/lib/parse-schema.mjs +667 -0
- package/src/lib/resolve.mjs +455 -0
- package/src/project.mjs +115 -0
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse a single entity source file.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} source
|
|
5
|
+
* @param {string} file repo-relative path, recorded on each entity
|
|
6
|
+
* @param {{ prefixes?: Record<string, string>, basePrefix?: string }} [options]
|
|
7
|
+
* @returns {ParsedEntity[]}
|
|
8
|
+
*/
|
|
9
|
+
export function parseEntitySource(source: string, file: string, options?: {
|
|
10
|
+
prefixes?: Record<string, string>;
|
|
11
|
+
basePrefix?: string;
|
|
12
|
+
}): ParsedEntity[];
|
|
13
|
+
/**
|
|
14
|
+
* Parse the choices dicts out of a single entity source file.
|
|
15
|
+
*
|
|
16
|
+
* Separate from `parseEntitySource` because enums are not owned by a class -
|
|
17
|
+
* they sit at file scope, and `ApplicationPolicySnapshot.ts` declares six of
|
|
18
|
+
* them and no entity at all.
|
|
19
|
+
*
|
|
20
|
+
* @param {string} source
|
|
21
|
+
* @param {string} file repo-relative path, recorded on each enum
|
|
22
|
+
* @returns {ParsedEnum[]}
|
|
23
|
+
*/
|
|
24
|
+
export function parseEnumSource(source: string, file: string): ParsedEnum[];
|
|
25
|
+
/**
|
|
26
|
+
* Parse every `.ts` entity file under a directory.
|
|
27
|
+
*
|
|
28
|
+
* @param {string} entitiesDir absolute path to `src/entities`
|
|
29
|
+
* @param {string} repoRoot absolute path, used to make `file` relative
|
|
30
|
+
* @param {{ prefixes?: Record<string, string>, basePrefix?: string }} [options]
|
|
31
|
+
* @returns {ParsedEntity[]}
|
|
32
|
+
*/
|
|
33
|
+
export function parseEntitiesDir(entitiesDir: string, repoRoot: string, options?: {
|
|
34
|
+
prefixes?: Record<string, string>;
|
|
35
|
+
basePrefix?: string;
|
|
36
|
+
}): ParsedEntity[];
|
|
37
|
+
/**
|
|
38
|
+
* Parse every choices dict under a directory.
|
|
39
|
+
*
|
|
40
|
+
* @param {string} entitiesDir absolute path to `src/entities`
|
|
41
|
+
* @param {string} repoRoot absolute path, used to make `file` relative
|
|
42
|
+
* @returns {ParsedEnum[]}
|
|
43
|
+
*/
|
|
44
|
+
export function parseEnumsDir(entitiesDir: string, repoRoot: string): ParsedEnum[];
|
|
45
|
+
/**
|
|
46
|
+
* @typedef {object} EntityFieldDecl
|
|
47
|
+
* @property {string} name
|
|
48
|
+
* @property {string | null} fieldClass e.g. 'CharField'; null if not a `new …Field()`
|
|
49
|
+
* @property {Record<string, string>} opts option name -> source text
|
|
50
|
+
* @property {string | null} entityRef identifier passed as the `entity:` option
|
|
51
|
+
* @property {boolean} many `many: true` was passed
|
|
52
|
+
* @property {boolean} readOnly `readOnly: true` was passed
|
|
53
|
+
* @property {boolean} blank `blank: true` was passed
|
|
54
|
+
*/
|
|
55
|
+
/**
|
|
56
|
+
* @typedef {object} TypeMember
|
|
57
|
+
* @property {string} name
|
|
58
|
+
* @property {string} typeText the declared type, verbatim
|
|
59
|
+
* @property {boolean} optional
|
|
60
|
+
*/
|
|
61
|
+
/**
|
|
62
|
+
* @typedef {object} ParsedEntity
|
|
63
|
+
* @property {string} file repo-relative path
|
|
64
|
+
* @property {string} className
|
|
65
|
+
* @property {boolean} isDefaultExport
|
|
66
|
+
* @property {string | null} schemaAnnotation from a `@schema <Component>` JSDoc tag
|
|
67
|
+
* @property {string[]} [schemaAlso] from `@schemaAlso <Component>` tags: other components this entity also models
|
|
68
|
+
* @property {string | null} noSchema reason from `@noSchema`; '' if tagged without one
|
|
69
|
+
* @property {Record<string, string>} schemaIgnores field -> reason, from `@schemaIgnore`
|
|
70
|
+
* @property {string | null} schemaSubset reason from `@schemaSubset`; a deliberate projection
|
|
71
|
+
* @property {string | null} apiBaseRaw source text of the `apiBase` expression
|
|
72
|
+
* @property {string | null} apiBase resolved URL path, matching schema `paths` keys
|
|
73
|
+
* @property {string | null} apiBaseNote why resolution failed, when it did
|
|
74
|
+
* @property {boolean} paginated
|
|
75
|
+
* @property {string | null} duck duck class name, e.g. 'DjangoRestFramework'
|
|
76
|
+
* @property {string | null} fieldsTypeName from `EntityFields<T>`, else conventional
|
|
77
|
+
* @property {EntityFieldDecl[]} fields
|
|
78
|
+
* @property {TypeMember[]} fieldsTypeMembers
|
|
79
|
+
* @property {string | null} recordTypeName
|
|
80
|
+
* @property {string | null} recordTypeText
|
|
81
|
+
* @property {boolean} fieldsTypeExported can `gen-asserts.mjs` import it?
|
|
82
|
+
* @property {boolean} recordTypeExported
|
|
83
|
+
*/
|
|
84
|
+
/**
|
|
85
|
+
* A `export const X = { … } as const` choices dict.
|
|
86
|
+
*
|
|
87
|
+
* The entity library has no first-class enum - `EnumField` is backed by
|
|
88
|
+
* `EntityEnum` and changes the runtime value to an Immutable Map - so a broker
|
|
89
|
+
* enum is modelled as a plain frozen object plus a type alias of the same name.
|
|
90
|
+
* That gives `Status.draft` at call sites and `Status` as a type, from one
|
|
91
|
+
* import.
|
|
92
|
+
*
|
|
93
|
+
* A dict is only schema-backed if it carries `@schemaEnum <Component>`. Several
|
|
94
|
+
* enums here have no broker counterpart at all (the policy-snapshot widget and
|
|
95
|
+
* rule enums are a client-side convention), so absence of the tag means "local",
|
|
96
|
+
* not "unresolved".
|
|
97
|
+
*
|
|
98
|
+
* @typedef {object} ParsedEnum
|
|
99
|
+
* @property {string} file repo-relative path
|
|
100
|
+
* @property {string} name
|
|
101
|
+
* @property {string | null} schemaEnum component name from `@schemaEnum`
|
|
102
|
+
* @property {Record<string, string>} members key -> string value, in source order
|
|
103
|
+
* @property {string[]} values
|
|
104
|
+
* @property {boolean} exported
|
|
105
|
+
* @property {string | null} typeName an exported type alias the dict derives
|
|
106
|
+
*/
|
|
107
|
+
/**
|
|
108
|
+
* Maps `settings.*` API URL constants to the path prefix that appears in the
|
|
109
|
+
* schema's `paths` keys, e.g. `{ BASE_API_URL: '/api/v1' }`.
|
|
110
|
+
*
|
|
111
|
+
* Projects supply this as `settingsPrefixes` in `entity-sync.config.mjs`,
|
|
112
|
+
* mirroring their `src/settings/settings.ts`, where each constant is composed as
|
|
113
|
+
* `${BASE_API_ORIGIN}<prefix>`. Only the path part matters here, because schema
|
|
114
|
+
* `paths` keys are origin-relative.
|
|
115
|
+
*
|
|
116
|
+
* Kept as data so a drift between the map and settings.ts shows up as an
|
|
117
|
+
* unresolved `apiBase` in the report rather than a silent mismatch. Empty by
|
|
118
|
+
* default: a project-specific prefix is never guessed.
|
|
119
|
+
*
|
|
120
|
+
* @type {Readonly<Record<string, string>>}
|
|
121
|
+
*/
|
|
122
|
+
export const DEFAULT_SETTINGS_PREFIXES: Readonly<Record<string, string>>;
|
|
123
|
+
/**
|
|
124
|
+
* Prefix applied to a bare relative `apiBase` such as `'/application/'`.
|
|
125
|
+
*
|
|
126
|
+
* Typically the path of whatever `axiosInstance` uses as `baseURL`; projects set
|
|
127
|
+
* it with `apiBasePrefix` in their config. Empty by default.
|
|
128
|
+
*/
|
|
129
|
+
export const DEFAULT_API_BASE_PREFIX: "";
|
|
130
|
+
export type EntityFieldDecl = {
|
|
131
|
+
name: string;
|
|
132
|
+
/**
|
|
133
|
+
* e.g. 'CharField'; null if not a `new …Field()`
|
|
134
|
+
*/
|
|
135
|
+
fieldClass: string | null;
|
|
136
|
+
/**
|
|
137
|
+
* option name -> source text
|
|
138
|
+
*/
|
|
139
|
+
opts: Record<string, string>;
|
|
140
|
+
/**
|
|
141
|
+
* identifier passed as the `entity:` option
|
|
142
|
+
*/
|
|
143
|
+
entityRef: string | null;
|
|
144
|
+
/**
|
|
145
|
+
* `many: true` was passed
|
|
146
|
+
*/
|
|
147
|
+
many: boolean;
|
|
148
|
+
/**
|
|
149
|
+
* `readOnly: true` was passed
|
|
150
|
+
*/
|
|
151
|
+
readOnly: boolean;
|
|
152
|
+
/**
|
|
153
|
+
* `blank: true` was passed
|
|
154
|
+
*/
|
|
155
|
+
blank: boolean;
|
|
156
|
+
};
|
|
157
|
+
export type TypeMember = {
|
|
158
|
+
name: string;
|
|
159
|
+
/**
|
|
160
|
+
* the declared type, verbatim
|
|
161
|
+
*/
|
|
162
|
+
typeText: string;
|
|
163
|
+
optional: boolean;
|
|
164
|
+
};
|
|
165
|
+
export type ParsedEntity = {
|
|
166
|
+
/**
|
|
167
|
+
* repo-relative path
|
|
168
|
+
*/
|
|
169
|
+
file: string;
|
|
170
|
+
className: string;
|
|
171
|
+
isDefaultExport: boolean;
|
|
172
|
+
/**
|
|
173
|
+
* from a `@schema <Component>` JSDoc tag
|
|
174
|
+
*/
|
|
175
|
+
schemaAnnotation: string | null;
|
|
176
|
+
/**
|
|
177
|
+
* from `@schemaAlso <Component>` tags: other components this entity also models
|
|
178
|
+
*/
|
|
179
|
+
schemaAlso?: string[] | undefined;
|
|
180
|
+
/**
|
|
181
|
+
* reason from `@noSchema`; '' if tagged without one
|
|
182
|
+
*/
|
|
183
|
+
noSchema: string | null;
|
|
184
|
+
/**
|
|
185
|
+
* field -> reason, from `@schemaIgnore`
|
|
186
|
+
*/
|
|
187
|
+
schemaIgnores: Record<string, string>;
|
|
188
|
+
/**
|
|
189
|
+
* reason from `@schemaSubset`; a deliberate projection
|
|
190
|
+
*/
|
|
191
|
+
schemaSubset: string | null;
|
|
192
|
+
/**
|
|
193
|
+
* source text of the `apiBase` expression
|
|
194
|
+
*/
|
|
195
|
+
apiBaseRaw: string | null;
|
|
196
|
+
/**
|
|
197
|
+
* resolved URL path, matching schema `paths` keys
|
|
198
|
+
*/
|
|
199
|
+
apiBase: string | null;
|
|
200
|
+
/**
|
|
201
|
+
* why resolution failed, when it did
|
|
202
|
+
*/
|
|
203
|
+
apiBaseNote: string | null;
|
|
204
|
+
paginated: boolean;
|
|
205
|
+
/**
|
|
206
|
+
* duck class name, e.g. 'DjangoRestFramework'
|
|
207
|
+
*/
|
|
208
|
+
duck: string | null;
|
|
209
|
+
/**
|
|
210
|
+
* from `EntityFields<T>`, else conventional
|
|
211
|
+
*/
|
|
212
|
+
fieldsTypeName: string | null;
|
|
213
|
+
fields: EntityFieldDecl[];
|
|
214
|
+
fieldsTypeMembers: TypeMember[];
|
|
215
|
+
recordTypeName: string | null;
|
|
216
|
+
recordTypeText: string | null;
|
|
217
|
+
/**
|
|
218
|
+
* can `gen-asserts.mjs` import it?
|
|
219
|
+
*/
|
|
220
|
+
fieldsTypeExported: boolean;
|
|
221
|
+
recordTypeExported: boolean;
|
|
222
|
+
};
|
|
223
|
+
/**
|
|
224
|
+
* A `export const X = { … } as const` choices dict.
|
|
225
|
+
*
|
|
226
|
+
* The entity library has no first-class enum - `EnumField` is backed by
|
|
227
|
+
* `EntityEnum` and changes the runtime value to an Immutable Map - so a broker
|
|
228
|
+
* enum is modelled as a plain frozen object plus a type alias of the same name.
|
|
229
|
+
* That gives `Status.draft` at call sites and `Status` as a type, from one
|
|
230
|
+
* import.
|
|
231
|
+
*
|
|
232
|
+
* A dict is only schema-backed if it carries `@schemaEnum <Component>`. Several
|
|
233
|
+
* enums here have no broker counterpart at all (the policy-snapshot widget and
|
|
234
|
+
* rule enums are a client-side convention), so absence of the tag means "local",
|
|
235
|
+
* not "unresolved".
|
|
236
|
+
*/
|
|
237
|
+
export type ParsedEnum = {
|
|
238
|
+
/**
|
|
239
|
+
* repo-relative path
|
|
240
|
+
*/
|
|
241
|
+
file: string;
|
|
242
|
+
name: string;
|
|
243
|
+
/**
|
|
244
|
+
* component name from `@schemaEnum`
|
|
245
|
+
*/
|
|
246
|
+
schemaEnum: string | null;
|
|
247
|
+
/**
|
|
248
|
+
* key -> string value, in source order
|
|
249
|
+
*/
|
|
250
|
+
members: Record<string, string>;
|
|
251
|
+
values: string[];
|
|
252
|
+
exported: boolean;
|
|
253
|
+
/**
|
|
254
|
+
* an exported type alias the dict derives
|
|
255
|
+
*/
|
|
256
|
+
typeName: string | null;
|
|
257
|
+
};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse a `schema.d.ts` source string.
|
|
3
|
+
*
|
|
4
|
+
* Exposed separately from `parseSchemaFile` so tests can run against fixture
|
|
5
|
+
* strings without touching the filesystem.
|
|
6
|
+
*
|
|
7
|
+
* @param {string} source
|
|
8
|
+
* @param {string} [fileName] for diagnostics only
|
|
9
|
+
* @returns {ParsedSchema}
|
|
10
|
+
*/
|
|
11
|
+
export function parseSchema(source: string, fileName?: string): ParsedSchema;
|
|
12
|
+
/**
|
|
13
|
+
* Parse a `schema.d.ts` file from disk.
|
|
14
|
+
*
|
|
15
|
+
* @param {string} filePath
|
|
16
|
+
* @returns {ParsedSchema}
|
|
17
|
+
*/
|
|
18
|
+
export function parseSchemaFile(filePath: string): ParsedSchema;
|
|
19
|
+
/**
|
|
20
|
+
* The value side of an `additionalProperties` map, described with the same
|
|
21
|
+
* vocabulary as a field so `field-map.mjs` can render `Record<string, V>`.
|
|
22
|
+
*/
|
|
23
|
+
export type MapValue = {
|
|
24
|
+
tsType: string;
|
|
25
|
+
isArray: boolean;
|
|
26
|
+
nullable: boolean;
|
|
27
|
+
refs: string[];
|
|
28
|
+
};
|
|
29
|
+
export type SchemaField = {
|
|
30
|
+
name: string;
|
|
31
|
+
/**
|
|
32
|
+
* had a `?` modifier (DRF `required: false`)
|
|
33
|
+
*/
|
|
34
|
+
optional: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* had a `readonly` modifier (DRF `readOnly: true`)
|
|
37
|
+
*/
|
|
38
|
+
readonly: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* the type union included `null`
|
|
41
|
+
*/
|
|
42
|
+
nullable: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* the type was `T[]`
|
|
45
|
+
*/
|
|
46
|
+
isArray: boolean;
|
|
47
|
+
/**
|
|
48
|
+
* component names this field references, in order
|
|
49
|
+
*/
|
|
50
|
+
refs: string[];
|
|
51
|
+
/**
|
|
52
|
+
* 'string' | 'number' | 'boolean' | 'ref' | 'record' | 'unknown'
|
|
53
|
+
*/
|
|
54
|
+
tsType: string;
|
|
55
|
+
/**
|
|
56
|
+
* populated when tsType === 'record'
|
|
57
|
+
*/
|
|
58
|
+
mapValue: MapValue | null;
|
|
59
|
+
/**
|
|
60
|
+
* e.g. 'uuid', 'date-time', 'decimal'
|
|
61
|
+
*/
|
|
62
|
+
format: string | null;
|
|
63
|
+
description: string | null;
|
|
64
|
+
};
|
|
65
|
+
export type SchemaComponent = {
|
|
66
|
+
name: string;
|
|
67
|
+
kind: "object" | "enum" | "other";
|
|
68
|
+
description: string | null;
|
|
69
|
+
/**
|
|
70
|
+
* populated when kind === 'object'
|
|
71
|
+
*/
|
|
72
|
+
fields: SchemaField[];
|
|
73
|
+
/**
|
|
74
|
+
* populated when kind === 'enum'
|
|
75
|
+
*/
|
|
76
|
+
enumValues: string[];
|
|
77
|
+
};
|
|
78
|
+
export type EndpointResponse = {
|
|
79
|
+
/**
|
|
80
|
+
* the `operations` key, e.g. 'x_application_retrieve'
|
|
81
|
+
*/
|
|
82
|
+
operation: string;
|
|
83
|
+
/**
|
|
84
|
+
* the component the 2xx JSON response resolves to
|
|
85
|
+
*/
|
|
86
|
+
component: string;
|
|
87
|
+
/**
|
|
88
|
+
* the response was a `Paginated…List` wrapper
|
|
89
|
+
*/
|
|
90
|
+
paginated: boolean;
|
|
91
|
+
};
|
|
92
|
+
export type ParsedSchema = {
|
|
93
|
+
components: Record<string, SchemaComponent>;
|
|
94
|
+
/**
|
|
95
|
+
* urlPath -> httpMethod -> response info
|
|
96
|
+
*/
|
|
97
|
+
pathsIndex: Record<string, Record<string, EndpointResponse>>;
|
|
98
|
+
};
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Jaccard overlap between two sets of field names.
|
|
3
|
+
*
|
|
4
|
+
* Used as the tiebreak between a list and detail serializer. It is a measurable
|
|
5
|
+
* criterion rather than a naming heuristic, which is what makes it trustworthy
|
|
6
|
+
* for the one case naming gets wrong: `ApplicationRow` (8 fields) scores higher
|
|
7
|
+
* against `Application` while `Application` (25 fields) scores higher against
|
|
8
|
+
* `ApplicationDetail`.
|
|
9
|
+
*
|
|
10
|
+
* @param {string[]} a
|
|
11
|
+
* @param {string[]} b
|
|
12
|
+
* @returns {number} 0..1, rounded to 3 places
|
|
13
|
+
*/
|
|
14
|
+
export function fieldOverlap(a: string[], b: string[]): number;
|
|
15
|
+
/**
|
|
16
|
+
* Resolve every entity to a component.
|
|
17
|
+
*
|
|
18
|
+
* @param {object} input
|
|
19
|
+
* @param {ParsedEntity[]} input.entities
|
|
20
|
+
* @param {ParsedSchema} input.schema
|
|
21
|
+
* @returns {Resolution[]} in the order entities were given
|
|
22
|
+
*/
|
|
23
|
+
export function resolveEntities({ entities, schema }: {
|
|
24
|
+
entities: ParsedEntity[];
|
|
25
|
+
schema: ParsedSchema;
|
|
26
|
+
}): Resolution[];
|
|
27
|
+
/**
|
|
28
|
+
* @typedef {object} EnumResolution
|
|
29
|
+
* @property {string} name the dict's identifier
|
|
30
|
+
* @property {string} file
|
|
31
|
+
* @property {string | null} typeName the type alias to assert against
|
|
32
|
+
* @property {string | null} component the `*Enum` component it models, if any
|
|
33
|
+
* @property {string[]} values the dict's own values, for the report
|
|
34
|
+
* @property {boolean} local no `@schemaEnum` tag: a client-side enum
|
|
35
|
+
* @property {string | null} note why it is unresolved, when it is
|
|
36
|
+
*/
|
|
37
|
+
/**
|
|
38
|
+
* Resolve each choices dict to the schema component it models.
|
|
39
|
+
*
|
|
40
|
+
* Unlike entity resolution, this is a lookup rather than a derivation: only the
|
|
41
|
+
* `@schemaEnum` tag binds a dict to a component. Deriving `X` -> `XEnum` by name
|
|
42
|
+
* was rejected because more than half the dicts in `src/entities` have no broker
|
|
43
|
+
* counterpart at all - the policy-snapshot widget and rule enums are a
|
|
44
|
+
* client-side convention - and a name-based rule would report every one of them
|
|
45
|
+
* as unresolved on every run.
|
|
46
|
+
*
|
|
47
|
+
* So an untagged dict is `local`, not a failure. The gap in the other direction,
|
|
48
|
+
* a component no dict models, is found by walking entity fields in `diff.mjs`,
|
|
49
|
+
* which knows which components are actually referenced.
|
|
50
|
+
*
|
|
51
|
+
* @param {object} input
|
|
52
|
+
* @param {import('./parse-entities.mjs').ParsedEnum[]} input.enums
|
|
53
|
+
* @param {ParsedSchema} input.schema
|
|
54
|
+
* @returns {EnumResolution[]}
|
|
55
|
+
*/
|
|
56
|
+
export function resolveEnums({ enums, schema }: {
|
|
57
|
+
enums: import("./parse-entities.mjs").ParsedEnum[];
|
|
58
|
+
schema: ParsedSchema;
|
|
59
|
+
}): EnumResolution[];
|
|
60
|
+
/**
|
|
61
|
+
* Index resolved enums by the component they model.
|
|
62
|
+
*
|
|
63
|
+
* @param {EnumResolution[]} resolutions
|
|
64
|
+
* @returns {Record<string, { name: string, typeName: string, file: string }>}
|
|
65
|
+
*/
|
|
66
|
+
export function buildEnumByComponent(resolutions: EnumResolution[]): Record<string, {
|
|
67
|
+
name: string;
|
|
68
|
+
typeName: string;
|
|
69
|
+
file: string;
|
|
70
|
+
}>;
|
|
71
|
+
export type EnumResolution = {
|
|
72
|
+
/**
|
|
73
|
+
* the dict's identifier
|
|
74
|
+
*/
|
|
75
|
+
name: string;
|
|
76
|
+
file: string;
|
|
77
|
+
/**
|
|
78
|
+
* the type alias to assert against
|
|
79
|
+
*/
|
|
80
|
+
typeName: string | null;
|
|
81
|
+
/**
|
|
82
|
+
* the `*Enum` component it models, if any
|
|
83
|
+
*/
|
|
84
|
+
component: string | null;
|
|
85
|
+
/**
|
|
86
|
+
* the dict's own values, for the report
|
|
87
|
+
*/
|
|
88
|
+
values: string[];
|
|
89
|
+
/**
|
|
90
|
+
* no `@schemaEnum` tag: a client-side enum
|
|
91
|
+
*/
|
|
92
|
+
local: boolean;
|
|
93
|
+
/**
|
|
94
|
+
* why it is unresolved, when it is
|
|
95
|
+
*/
|
|
96
|
+
note: string | null;
|
|
97
|
+
};
|
|
98
|
+
export type ParsedEntity = import("./parse-entities.mjs").ParsedEntity;
|
|
99
|
+
export type ParsedSchema = import("./parse-schema.mjs").ParsedSchema;
|
|
100
|
+
export type Candidate = {
|
|
101
|
+
component: string;
|
|
102
|
+
/**
|
|
103
|
+
* field-name overlap, 0..1
|
|
104
|
+
*/
|
|
105
|
+
score: number;
|
|
106
|
+
/**
|
|
107
|
+
* how it was found, e.g. 'detail' or 'list'
|
|
108
|
+
*/
|
|
109
|
+
via: string;
|
|
110
|
+
};
|
|
111
|
+
export type Resolution = {
|
|
112
|
+
className: string;
|
|
113
|
+
file: string;
|
|
114
|
+
/**
|
|
115
|
+
* the chosen component, if any
|
|
116
|
+
*/
|
|
117
|
+
component: string | null;
|
|
118
|
+
source: "annotation" | "path" | "nested" | null;
|
|
119
|
+
/**
|
|
120
|
+
* all considered, best first
|
|
121
|
+
*/
|
|
122
|
+
candidates: Candidate[];
|
|
123
|
+
/**
|
|
124
|
+
* more than one plausible candidate
|
|
125
|
+
*/
|
|
126
|
+
ambiguous: boolean;
|
|
127
|
+
/**
|
|
128
|
+
* no schema counterpart by design
|
|
129
|
+
*/
|
|
130
|
+
excluded: boolean;
|
|
131
|
+
/**
|
|
132
|
+
* why it is unresolved or excluded
|
|
133
|
+
*/
|
|
134
|
+
note: string | null;
|
|
135
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {import('./config.mjs').ResolvedConfig} ResolvedConfig
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* @param {ResolvedConfig} config
|
|
6
|
+
*/
|
|
7
|
+
export function readSchema(config: ResolvedConfig): import("./lib/parse-schema.mjs").ParsedSchema;
|
|
8
|
+
/**
|
|
9
|
+
* @param {ResolvedConfig} config
|
|
10
|
+
*/
|
|
11
|
+
export function readEntities(config: ResolvedConfig): import("./lib/parse-entities.mjs").ParsedEntity[];
|
|
12
|
+
/**
|
|
13
|
+
* @param {ResolvedConfig} config
|
|
14
|
+
*/
|
|
15
|
+
export function readEnums(config: ResolvedConfig): import("./lib/parse-entities.mjs").ParsedEnum[];
|
|
16
|
+
/**
|
|
17
|
+
* Everything `plan` and `asserts` both need.
|
|
18
|
+
*
|
|
19
|
+
* @param {ResolvedConfig} config
|
|
20
|
+
*/
|
|
21
|
+
export function loadProject(config: ResolvedConfig): {
|
|
22
|
+
schema: import("./lib/parse-schema.mjs").ParsedSchema;
|
|
23
|
+
entities: import("./lib/parse-entities.mjs").ParsedEntity[];
|
|
24
|
+
resolutions: import("./lib/resolve.mjs").Resolution[];
|
|
25
|
+
entityByComponent: Record<string, {
|
|
26
|
+
className: string;
|
|
27
|
+
fieldsTypeName: string;
|
|
28
|
+
recordTypeName: string;
|
|
29
|
+
}>;
|
|
30
|
+
enums: import("./lib/parse-entities.mjs").ParsedEnum[];
|
|
31
|
+
enumResolutions: import("./lib/resolve.mjs").EnumResolution[];
|
|
32
|
+
enumByComponent: Record<string, {
|
|
33
|
+
name: string;
|
|
34
|
+
typeName: string;
|
|
35
|
+
file: string;
|
|
36
|
+
}>;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Import a peer dependency as the project resolves it, so the project's own
|
|
40
|
+
* version (and config) is used rather than whichever copy sits beside this
|
|
41
|
+
* package.
|
|
42
|
+
*
|
|
43
|
+
* @param {string} root
|
|
44
|
+
* @param {string} name
|
|
45
|
+
* @returns {Promise<any>}
|
|
46
|
+
*/
|
|
47
|
+
export function importFromProject(root: string, name: string): Promise<any>;
|
|
48
|
+
/**
|
|
49
|
+
* Format generated source with the project's prettier and prettier config.
|
|
50
|
+
*
|
|
51
|
+
* Load-bearing, not cosmetic: unformatted output would produce a whole-file
|
|
52
|
+
* diff on every sync, burying the lines that actually changed.
|
|
53
|
+
*
|
|
54
|
+
* @param {ResolvedConfig} config
|
|
55
|
+
* @param {string} path absolute path the source will be written to
|
|
56
|
+
* @param {string} source
|
|
57
|
+
* @returns {Promise<string>}
|
|
58
|
+
*/
|
|
59
|
+
export function formatForProject(config: ResolvedConfig, path: string, source: string): Promise<string>;
|
|
60
|
+
export type ResolvedConfig = import("./config.mjs").ResolvedConfig;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// @ts-check
|
|
3
|
+
/**
|
|
4
|
+
* PreToolUse hook: limit the `entity-editor` subagent's Bash to a short
|
|
5
|
+
* allowlist.
|
|
6
|
+
*
|
|
7
|
+
* Plugin agents cannot scope Bash in their `tools:` frontmatter, and their own
|
|
8
|
+
* `hooks:` frontmatter is ignored, so the limit lives here, in the plugin's
|
|
9
|
+
* hooks.json, keyed on the `agent_type` Claude Code puts in the hook input.
|
|
10
|
+
* Everything that is not the editor - the main session included - passes
|
|
11
|
+
* through untouched, which is what lets the orchestrator run `entity:check`
|
|
12
|
+
* while the editor cannot.
|
|
13
|
+
*
|
|
14
|
+
* This is a guardrail against a well-meaning agent, not a security boundary:
|
|
15
|
+
* it matches command text.
|
|
16
|
+
*
|
|
17
|
+
* Set ENTITY_SYNC_HOOK_LOG=<file> to append every hook input to that file
|
|
18
|
+
* (used to confirm the exact `agent_type` value a plugin agent reports).
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { appendFileSync, readFileSync } from 'node:fs';
|
|
22
|
+
import { pathToFileURL } from 'node:url';
|
|
23
|
+
|
|
24
|
+
/** Matches the plugin-scoped name and the bare name. */
|
|
25
|
+
const EDITOR_AGENT = /(^|:)entity-editor$/;
|
|
26
|
+
|
|
27
|
+
/** Commands the editor may run, matched against the whole command. */
|
|
28
|
+
const ALLOWED = [
|
|
29
|
+
/^npm run (--silent )?entity:inspect( |$)/,
|
|
30
|
+
/^npx entity-sync inspect( |$)/,
|
|
31
|
+
/^npx eslint( |$)/,
|
|
32
|
+
/^npx prettier( |$)/,
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
/** Chaining, substitution and redirection would let an allowed prefix smuggle anything. */
|
|
36
|
+
const SHELL_OPERATORS = /[;&|`<>]|\$\(/;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @typedef {{ agent_type?: string, tool_name?: string, tool_input?: { command?: string } }} HookInput
|
|
40
|
+
* @typedef {{ hookSpecificOutput: { hookEventName: 'PreToolUse', permissionDecision: 'deny', permissionDecisionReason: string } }} HookDeny
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @param {HookInput} input
|
|
45
|
+
* @returns {HookDeny | null} null to let the call proceed
|
|
46
|
+
*/
|
|
47
|
+
export function decide(input) {
|
|
48
|
+
if (!input.agent_type || !EDITOR_AGENT.test(input.agent_type)) return null;
|
|
49
|
+
if (input.tool_name !== 'Bash') return null;
|
|
50
|
+
|
|
51
|
+
const command = (input.tool_input?.command ?? '').trim();
|
|
52
|
+
const allowed =
|
|
53
|
+
!SHELL_OPERATORS.test(command) && ALLOWED.some((re) => re.test(command));
|
|
54
|
+
if (allowed) return null;
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
hookSpecificOutput: {
|
|
58
|
+
hookEventName: 'PreToolUse',
|
|
59
|
+
permissionDecision: 'deny',
|
|
60
|
+
permissionDecisionReason:
|
|
61
|
+
'entity-editor may only run single commands: `npm run entity:inspect -- ...`, ' +
|
|
62
|
+
'`npx entity-sync inspect ...`, `npx eslint ...`, `npx prettier ...`. ' +
|
|
63
|
+
'`entity:check` belongs to the orchestrator. Report what you needed instead.',
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function main() {
|
|
69
|
+
const raw = readFileSync(0, 'utf8');
|
|
70
|
+
if (process.env.ENTITY_SYNC_HOOK_LOG) {
|
|
71
|
+
appendFileSync(process.env.ENTITY_SYNC_HOOK_LOG, `${raw.trim()}\n`);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** @type {HookInput} */
|
|
75
|
+
let input;
|
|
76
|
+
try {
|
|
77
|
+
input = JSON.parse(raw);
|
|
78
|
+
} catch {
|
|
79
|
+
return; // never block on input we cannot read
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const decision = decide(input);
|
|
83
|
+
if (decision) process.stdout.write(JSON.stringify(decision));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (import.meta.url === pathToFileURL(process.argv[1] ?? '').href) main();
|