@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
package/src/lib/diff.mjs
ADDED
|
@@ -0,0 +1,706 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* Compare an entity against the schema component it models, and emit the edits
|
|
4
|
+
* needed to bring them into agreement.
|
|
5
|
+
*
|
|
6
|
+
* The output is the whole point of the tool: a short list of ops per entity,
|
|
7
|
+
* which is what the `/entity sync` skill reads instead of the 296KB schema.
|
|
8
|
+
*
|
|
9
|
+
* ## Op kinds
|
|
10
|
+
*
|
|
11
|
+
* | kind | meaning |
|
|
12
|
+
* | ------------- | ---------------------------------------------------------- |
|
|
13
|
+
* | `add` | the API sends a field the entity does not declare |
|
|
14
|
+
* | `remove` | the entity declares a field the API does not send |
|
|
15
|
+
* | `retype` | the field class disagrees with the schema signal |
|
|
16
|
+
* | `opts` | `readOnly` / `blank` / `many` / `entity` disagree |
|
|
17
|
+
* | `type-text` | the `Fields` member type disagrees |
|
|
18
|
+
* | `record-type` | the `Record` type is missing its Immutable substitution |
|
|
19
|
+
* | `internal` | `static fields` and the `Fields` type disagree with each |
|
|
20
|
+
* | | other, independent of the schema |
|
|
21
|
+
* | `review` | something the mapper declined to decide - needs a human |
|
|
22
|
+
*
|
|
23
|
+
* ## Deliberate tolerances
|
|
24
|
+
*
|
|
25
|
+
* Three places accept more than one answer, because insisting on an exact string
|
|
26
|
+
* would produce noise that trains a reader to ignore the report:
|
|
27
|
+
*
|
|
28
|
+
* - **enum types**: the ideal is a literal union, but a plain `string` or a named
|
|
29
|
+
* alias (as in `ApplicationStatusValue`) is equally valid and cannot be
|
|
30
|
+
* verified without the type checker.
|
|
31
|
+
* - **union member order**: `string | null` and `null | string` are the same
|
|
32
|
+
* type, so comparison normalises order.
|
|
33
|
+
* - **TextField for CharField**: the schema types a Django `TextField` and
|
|
34
|
+
* `CharField` alike as a plain `string`, so it cannot say which one the
|
|
35
|
+
* entity should use. `TextField` is a `CharField` that entity-form draws as a
|
|
36
|
+
* textarea - a presentation choice, not drift.
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Field classes accepted in place of the one the schema maps to.
|
|
41
|
+
*
|
|
42
|
+
* Projects extend this via `equivalentFieldClasses` in their config.
|
|
43
|
+
*
|
|
44
|
+
* @type {Readonly<Record<string, readonly string[]>>}
|
|
45
|
+
*/
|
|
46
|
+
export const DEFAULT_EQUIVALENT_FIELD_CLASSES = Object.freeze({
|
|
47
|
+
CharField: Object.freeze(['TextField']),
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
import { expectedField } from './field-map.mjs';
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @typedef {import('./parse-entities.mjs').ParsedEntity} ParsedEntity
|
|
54
|
+
* @typedef {import('./parse-schema.mjs').SchemaComponent} SchemaComponent
|
|
55
|
+
* @typedef {import('./parse-schema.mjs').ParsedSchema} ParsedSchema
|
|
56
|
+
*/
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @typedef {'add'|'remove'|'retype'|'opts'|'type-text'|'record-type'|'internal'|'enum-missing'} ShapeOpKind
|
|
60
|
+
* a genuine disagreement about the data's shape
|
|
61
|
+
* @typedef {'readonly-hint'|'review'|'enum-untyped'} AdvisoryOpKind
|
|
62
|
+
* worth knowing, but not a shape error - reported separately so it cannot
|
|
63
|
+
* crowd out the ops above
|
|
64
|
+
*/
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @typedef {object} FieldOp
|
|
68
|
+
* @property {ShapeOpKind | AdvisoryOpKind} kind
|
|
69
|
+
* @property {string} field
|
|
70
|
+
* @property {string} detail
|
|
71
|
+
* @property {string | null} expected
|
|
72
|
+
* @property {string | null} actual
|
|
73
|
+
*/
|
|
74
|
+
|
|
75
|
+
/** Op kinds that are advisory rather than shape drift. */
|
|
76
|
+
export const ADVISORY_OP_KINDS = new Set([
|
|
77
|
+
'readonly-hint',
|
|
78
|
+
'review',
|
|
79
|
+
'enum-untyped',
|
|
80
|
+
]);
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @typedef {'needed'|'redundant'|'orphaned'} SuppressionStatus
|
|
84
|
+
* `needed` - the field still disagrees, so the annotation is still doing work.
|
|
85
|
+
* `redundant` - the schema and the entity now agree; the broker has caught up
|
|
86
|
+
* and the annotation can be deleted.
|
|
87
|
+
* `orphaned` - the named field is in neither the component nor the Fields
|
|
88
|
+
* type, so the annotation refers to something that no longer exists.
|
|
89
|
+
*/
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* One `@schemaIgnore`, with the ops it is currently hiding.
|
|
93
|
+
*
|
|
94
|
+
* @typedef {object} Suppression
|
|
95
|
+
* @property {string} field
|
|
96
|
+
* @property {string} reason the annotation's own words; '' if none was given
|
|
97
|
+
* @property {SuppressionStatus} status
|
|
98
|
+
* @property {FieldOp[]} ops the ops this annotation dropped, for review
|
|
99
|
+
*/
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* @typedef {object} EntityDiff
|
|
103
|
+
* @property {string} className
|
|
104
|
+
* @property {string} file
|
|
105
|
+
* @property {string} component
|
|
106
|
+
* @property {FieldOp[]} ops
|
|
107
|
+
* @property {string[]} nestedEntities entity classes this one embeds
|
|
108
|
+
* @property {Suppression[]} suppressions every `@schemaIgnore`, and its status
|
|
109
|
+
* @property {string | null} schemaSubset the `@schemaSubset` reason, if any
|
|
110
|
+
* @property {string[]} subsetDropped fields the projection declined to compare
|
|
111
|
+
*/
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Normalise a TypeScript type for comparison: collapse whitespace and sort
|
|
115
|
+
* top-level union members.
|
|
116
|
+
*
|
|
117
|
+
* Splitting only at depth zero keeps `List<A | B>` and `(A | B)[]` intact, so a
|
|
118
|
+
* nested union is not mistaken for a top-level one.
|
|
119
|
+
*
|
|
120
|
+
* @param {string} typeText
|
|
121
|
+
* @returns {string}
|
|
122
|
+
*/
|
|
123
|
+
export function normaliseType(typeText) {
|
|
124
|
+
const collapsed = typeText.replace(/\s+/g, ' ').trim();
|
|
125
|
+
|
|
126
|
+
/** @type {string[]} */
|
|
127
|
+
const members = [];
|
|
128
|
+
let depth = 0;
|
|
129
|
+
let current = '';
|
|
130
|
+
|
|
131
|
+
for (const char of collapsed) {
|
|
132
|
+
if (char === '<' || char === '(' || char === '[') depth += 1;
|
|
133
|
+
if (char === '>' || char === ')' || char === ']') depth -= 1;
|
|
134
|
+
|
|
135
|
+
if (char === '|' && depth === 0) {
|
|
136
|
+
members.push(current.trim());
|
|
137
|
+
current = '';
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
current += char;
|
|
141
|
+
}
|
|
142
|
+
members.push(current.trim());
|
|
143
|
+
|
|
144
|
+
return members.filter(Boolean).sort().join(' | ');
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Judge an entity's declared type for a field the schema types as an enum.
|
|
149
|
+
*
|
|
150
|
+
* This used to answer only yes/no, and said yes to any capitalised identifier
|
|
151
|
+
* on the grounds that an alias cannot be resolved without the type checker.
|
|
152
|
+
* That was true before choices dicts were parsed, and it made enum drift
|
|
153
|
+
* invisible: `status: string` and `status: WhateverValue` both passed.
|
|
154
|
+
*
|
|
155
|
+
* Now the dict's own alias name is known, so the cases can be told apart. They
|
|
156
|
+
* are graded rather than merged, because they are not equally wrong:
|
|
157
|
+
*
|
|
158
|
+
* - `match` - the entity references the dict's type. Nothing to do.
|
|
159
|
+
* - `literals` - a hand-written union with exactly the right values. Correct
|
|
160
|
+
* today, but duplicated from the broker and free to drift tomorrow.
|
|
161
|
+
* - `untyped` - `string`. Assignable from the wire, so `Extends` passes, but it
|
|
162
|
+
* throws away every value the broker promised.
|
|
163
|
+
* - `mismatch` - something else entirely. Real drift.
|
|
164
|
+
*
|
|
165
|
+
* The middle two are advisory: the data flowing through them is correct, so
|
|
166
|
+
* listing them as shape ops would put "works, but could be tidier" beside
|
|
167
|
+
* "wrong type", which is how a report teaches its reader to skim.
|
|
168
|
+
*
|
|
169
|
+
* @param {string} actual
|
|
170
|
+
* @param {string} expected
|
|
171
|
+
* @param {string[]} expectedValues
|
|
172
|
+
* @returns {'match'|'literals'|'untyped'|'mismatch'}
|
|
173
|
+
*/
|
|
174
|
+
function classifyEnumType(actual, expected, expectedValues) {
|
|
175
|
+
const normalisedActual = normaliseType(actual);
|
|
176
|
+
if (normalisedActual === normaliseType(expected)) return 'match';
|
|
177
|
+
|
|
178
|
+
// Strip the widening the broker expresses with BlankEnum/NullEnum before
|
|
179
|
+
// judging the core type; `expected` carries those separately.
|
|
180
|
+
const core = normalisedActual
|
|
181
|
+
.split('|')
|
|
182
|
+
.map((part) => part.trim())
|
|
183
|
+
.filter((part) => part !== 'null' && part !== "''");
|
|
184
|
+
|
|
185
|
+
if (core.every((part) => /^'[^']*'$/.test(part))) {
|
|
186
|
+
const declared = new Set(core.map((part) => part.slice(1, -1)));
|
|
187
|
+
const wire = new Set(expectedValues.filter(Boolean));
|
|
188
|
+
const sameValues =
|
|
189
|
+
declared.size === wire.size && [...wire].every((v) => declared.has(v));
|
|
190
|
+
|
|
191
|
+
return sameValues ? 'literals' : 'mismatch';
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (core.length !== 1) return 'mismatch';
|
|
195
|
+
const [only] = core;
|
|
196
|
+
|
|
197
|
+
if (only === 'string' || only === 'string[]') return 'untyped';
|
|
198
|
+
|
|
199
|
+
return 'mismatch';
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Compare the options actually passed against those expected.
|
|
204
|
+
*
|
|
205
|
+
* Only the options with schema meaning are compared; anything else the entity
|
|
206
|
+
* passes (`default`, `maxLength`) is a local decision and left alone.
|
|
207
|
+
*
|
|
208
|
+
* ## Which options are compared, and why `blank` is not
|
|
209
|
+
*
|
|
210
|
+
* `blank` is **not** compared. In the entity library it does exactly one thing -
|
|
211
|
+
* `validators: options.blank ? [] : [isRequired]` - so it gates form validation
|
|
212
|
+
* and says nothing about the wire. Whether a form requires input is a form's
|
|
213
|
+
* decision, and the read-side signal that does matter, nullability, is enforced
|
|
214
|
+
* precisely by the `type-text` comparison instead.
|
|
215
|
+
*
|
|
216
|
+
* `readOnly` is compared in **one direction only**, and reported as advisory:
|
|
217
|
+
*
|
|
218
|
+
* - Schema read-only, entity not: worth knowing. The server silently ignores
|
|
219
|
+
* writes to the field and the entity does not say so.
|
|
220
|
+
* - Schema writable, entity read-only: not drift. The frontend has chosen to be
|
|
221
|
+
* stricter than the server allows, which is legitimate -
|
|
222
|
+
* `TrustPolicy.description` is writable in the API but the app only ever
|
|
223
|
+
* displays trust policies.
|
|
224
|
+
*
|
|
225
|
+
* It is advisory rather than a shape op because `Field.readOnly` is not yet
|
|
226
|
+
* implemented in the library; it is a statement of intent, so it must not sit in
|
|
227
|
+
* the same list as a wrong field type.
|
|
228
|
+
*
|
|
229
|
+
* `many` and `entity` stay symmetric and are real shape ops: a list/scalar or
|
|
230
|
+
* wrong-entity mismatch is a genuine disagreement whichever way it points.
|
|
231
|
+
*
|
|
232
|
+
* Comparing both directions of both flags produced 206 of 374 ops on the first
|
|
233
|
+
* real run, nearly all harmless - enough noise to train a reader to skim the
|
|
234
|
+
* report, which is worse than not having one.
|
|
235
|
+
*
|
|
236
|
+
* @param {import('./parse-entities.mjs').EntityFieldDecl} declared
|
|
237
|
+
* @param {import('./field-map.mjs').ExpectedField} expected
|
|
238
|
+
* @returns {{ shape: string[], advisory: string[] }}
|
|
239
|
+
*/
|
|
240
|
+
function compareOpts(declared, expected) {
|
|
241
|
+
/** @type {string[]} */
|
|
242
|
+
const shape = [];
|
|
243
|
+
/** @type {string[]} */
|
|
244
|
+
const advisory = [];
|
|
245
|
+
|
|
246
|
+
if (expected.opts.readOnly === true && !declared.readOnly) {
|
|
247
|
+
advisory.push('add readOnly (schema says read-only)');
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if ((expected.opts.many === true) !== declared.many) {
|
|
251
|
+
shape.push(expected.opts.many === true ? 'add many' : 'drop many');
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (expected.entityRef && declared.entityRef !== expected.entityRef) {
|
|
255
|
+
shape.push(
|
|
256
|
+
`entity: ${declared.entityRef ?? '(none)'} -> ${expected.entityRef}`,
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return { shape, advisory };
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Render an expected field declaration, for an `add` op's detail.
|
|
265
|
+
*
|
|
266
|
+
* @param {string} name
|
|
267
|
+
* @param {import('./field-map.mjs').ExpectedField} expected
|
|
268
|
+
*/
|
|
269
|
+
function renderDeclaration(name, expected) {
|
|
270
|
+
const opts = Object.entries(expected.opts)
|
|
271
|
+
.map(([key, value]) =>
|
|
272
|
+
value === true ? `${key}: true` : `${key}: ${value}`,
|
|
273
|
+
)
|
|
274
|
+
.join(', ');
|
|
275
|
+
|
|
276
|
+
return `${name}: new Fields.${expected.fieldClass}(${opts ? `{ ${opts} }` : ''})`;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Diff one entity against its component.
|
|
281
|
+
*
|
|
282
|
+
* @param {object} input
|
|
283
|
+
* @param {ParsedEntity} input.entity
|
|
284
|
+
* @param {SchemaComponent} input.component
|
|
285
|
+
* @param {Record<string, SchemaComponent>} input.components
|
|
286
|
+
* @param {Record<string, { className: string, fieldsTypeName: string, recordTypeName: string }>} [input.entityByComponent]
|
|
287
|
+
* @param {Record<string, { name: string, typeName: string, file: string }>} [input.enumByComponent]
|
|
288
|
+
* @param {Readonly<Record<string, readonly string[]>>} [input.equivalentFieldClasses]
|
|
289
|
+
* @returns {EntityDiff}
|
|
290
|
+
*/
|
|
291
|
+
export function diffEntity({
|
|
292
|
+
entity,
|
|
293
|
+
component,
|
|
294
|
+
components,
|
|
295
|
+
entityByComponent = {},
|
|
296
|
+
enumByComponent = {},
|
|
297
|
+
equivalentFieldClasses = DEFAULT_EQUIVALENT_FIELD_CLASSES,
|
|
298
|
+
}) {
|
|
299
|
+
/** @type {FieldOp[]} */
|
|
300
|
+
const ops = [];
|
|
301
|
+
/** @type {string[]} */
|
|
302
|
+
const nestedEntities = [];
|
|
303
|
+
|
|
304
|
+
const declaredByName = new Map(entity.fields.map((f) => [f.name, f]));
|
|
305
|
+
const typeMemberByName = new Map(
|
|
306
|
+
entity.fieldsTypeMembers.map((m) => [m.name, m]),
|
|
307
|
+
);
|
|
308
|
+
const schemaNames = new Set(component.fields.map((f) => f.name));
|
|
309
|
+
|
|
310
|
+
/** Nested fields, for the Record-type substitution check. */
|
|
311
|
+
/** @type {{ name: string, recordTypeText: string }[]} */
|
|
312
|
+
const nestedFields = [];
|
|
313
|
+
|
|
314
|
+
for (const schemaField of component.fields) {
|
|
315
|
+
const expected = expectedField(schemaField, {
|
|
316
|
+
components,
|
|
317
|
+
entityByComponent,
|
|
318
|
+
enumByComponent,
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
if (expected.entityRef) {
|
|
322
|
+
nestedEntities.push(expected.entityRef);
|
|
323
|
+
if (expected.recordTypeText) {
|
|
324
|
+
nestedFields.push({
|
|
325
|
+
name: schemaField.name,
|
|
326
|
+
recordTypeText: expected.recordTypeText,
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
if (expected.note) {
|
|
332
|
+
ops.push({
|
|
333
|
+
kind: 'review',
|
|
334
|
+
field: schemaField.name,
|
|
335
|
+
detail: expected.note,
|
|
336
|
+
expected: null,
|
|
337
|
+
actual: null,
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
const declared = declaredByName.get(schemaField.name);
|
|
342
|
+
const typeMember = typeMemberByName.get(schemaField.name);
|
|
343
|
+
|
|
344
|
+
if (!declared && !typeMember) {
|
|
345
|
+
ops.push({
|
|
346
|
+
kind: 'add',
|
|
347
|
+
field: schemaField.name,
|
|
348
|
+
detail: renderDeclaration(schemaField.name, expected),
|
|
349
|
+
expected: expected.typeText,
|
|
350
|
+
actual: null,
|
|
351
|
+
});
|
|
352
|
+
continue;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// Declared in one place but not the other: an internal inconsistency that
|
|
356
|
+
// has nothing to do with the schema, so it gets its own kind.
|
|
357
|
+
if (!declared) {
|
|
358
|
+
ops.push({
|
|
359
|
+
kind: 'internal',
|
|
360
|
+
field: schemaField.name,
|
|
361
|
+
detail: 'present in the Fields type but not in `static fields`',
|
|
362
|
+
expected: null,
|
|
363
|
+
actual: null,
|
|
364
|
+
});
|
|
365
|
+
} else if (!typeMember) {
|
|
366
|
+
ops.push({
|
|
367
|
+
kind: 'internal',
|
|
368
|
+
field: schemaField.name,
|
|
369
|
+
detail: 'present in `static fields` but not in the Fields type',
|
|
370
|
+
expected: expected.typeText,
|
|
371
|
+
actual: null,
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
if (
|
|
376
|
+
declared?.fieldClass &&
|
|
377
|
+
declared.fieldClass !== expected.fieldClass &&
|
|
378
|
+
!equivalentFieldClasses[expected.fieldClass]?.includes(
|
|
379
|
+
declared.fieldClass,
|
|
380
|
+
)
|
|
381
|
+
) {
|
|
382
|
+
ops.push({
|
|
383
|
+
kind: 'retype',
|
|
384
|
+
field: schemaField.name,
|
|
385
|
+
detail: `${declared.fieldClass} -> ${expected.fieldClass}`,
|
|
386
|
+
expected: expected.fieldClass,
|
|
387
|
+
actual: declared.fieldClass,
|
|
388
|
+
});
|
|
389
|
+
} else if (declared && !declared.fieldClass) {
|
|
390
|
+
ops.push({
|
|
391
|
+
kind: 'review',
|
|
392
|
+
field: schemaField.name,
|
|
393
|
+
detail: 'not declared as a field constructor; cannot be checked',
|
|
394
|
+
expected: expected.fieldClass,
|
|
395
|
+
actual: null,
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
if (declared) {
|
|
400
|
+
const { shape, advisory } = compareOpts(declared, expected);
|
|
401
|
+
|
|
402
|
+
if (shape.length) {
|
|
403
|
+
ops.push({
|
|
404
|
+
kind: 'opts',
|
|
405
|
+
field: schemaField.name,
|
|
406
|
+
detail: shape.join(', '),
|
|
407
|
+
expected: JSON.stringify(expected.opts),
|
|
408
|
+
actual: JSON.stringify({
|
|
409
|
+
many: declared.many,
|
|
410
|
+
...(declared.entityRef ? { entity: declared.entityRef } : {}),
|
|
411
|
+
}),
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
for (const hint of advisory) {
|
|
416
|
+
ops.push({
|
|
417
|
+
kind: 'readonly-hint',
|
|
418
|
+
field: schemaField.name,
|
|
419
|
+
detail: hint,
|
|
420
|
+
expected: 'readOnly: true',
|
|
421
|
+
actual: null,
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// A `*Enum` component the broker sends but no dict models yet. Reported
|
|
427
|
+
// once per field rather than once per component, because the fix is per
|
|
428
|
+
// field: the dict is written where the entity that uses it lives.
|
|
429
|
+
const uncovered = (expected.enumRefs ?? []).filter(
|
|
430
|
+
(ref) => !enumByComponent[ref],
|
|
431
|
+
);
|
|
432
|
+
for (const ref of uncovered) {
|
|
433
|
+
ops.push({
|
|
434
|
+
kind: 'enum-missing',
|
|
435
|
+
field: schemaField.name,
|
|
436
|
+
detail:
|
|
437
|
+
`no choices dict models ${ref}; ` +
|
|
438
|
+
`add \`export const ${ref.replace(/Enum$/, '')}\` with @schemaEnum ${ref}`,
|
|
439
|
+
expected: ref,
|
|
440
|
+
actual: null,
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
if (typeMember) {
|
|
445
|
+
if (expected.enumValues.length) {
|
|
446
|
+
// While a dict is missing, `expected.typeText` is the raw literal
|
|
447
|
+
// union - what the type would be if nobody ever wrote a dict. Comparing
|
|
448
|
+
// against it would report the entity's existing alias as drift and
|
|
449
|
+
// propose replacing it with an inline union, which is the opposite of
|
|
450
|
+
// the intended fix. The `enum-missing` op above is the actionable one.
|
|
451
|
+
const grade = uncovered.length
|
|
452
|
+
? 'match'
|
|
453
|
+
: classifyEnumType(
|
|
454
|
+
typeMember.typeText,
|
|
455
|
+
expected.typeText,
|
|
456
|
+
expected.enumValues,
|
|
457
|
+
);
|
|
458
|
+
|
|
459
|
+
if (grade === 'mismatch') {
|
|
460
|
+
ops.push({
|
|
461
|
+
kind: 'type-text',
|
|
462
|
+
field: schemaField.name,
|
|
463
|
+
detail: `${typeMember.typeText} -> ${expected.typeText}`,
|
|
464
|
+
expected: expected.typeText,
|
|
465
|
+
actual: typeMember.typeText,
|
|
466
|
+
});
|
|
467
|
+
} else if (grade !== 'match') {
|
|
468
|
+
ops.push({
|
|
469
|
+
kind: 'enum-untyped',
|
|
470
|
+
field: schemaField.name,
|
|
471
|
+
detail:
|
|
472
|
+
grade === 'untyped'
|
|
473
|
+
? `typed \`${typeMember.typeText}\`, discarding the values in ${expected.typeText}`
|
|
474
|
+
: `inlines the values of ${expected.typeText}; reference the dict instead`,
|
|
475
|
+
expected: expected.typeText,
|
|
476
|
+
actual: typeMember.typeText,
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
} else if (
|
|
480
|
+
normaliseType(typeMember.typeText) !== normaliseType(expected.typeText)
|
|
481
|
+
) {
|
|
482
|
+
ops.push({
|
|
483
|
+
kind: 'type-text',
|
|
484
|
+
field: schemaField.name,
|
|
485
|
+
detail: `${typeMember.typeText} -> ${expected.typeText}`,
|
|
486
|
+
expected: expected.typeText,
|
|
487
|
+
actual: typeMember.typeText,
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// ── Fields the entity declares but the API does not send ────────────────────
|
|
494
|
+
for (const declared of entity.fields) {
|
|
495
|
+
if (schemaNames.has(declared.name)) continue;
|
|
496
|
+
ops.push({
|
|
497
|
+
kind: 'remove',
|
|
498
|
+
field: declared.name,
|
|
499
|
+
detail: `not present in ${component.name}`,
|
|
500
|
+
expected: null,
|
|
501
|
+
actual: declared.fieldClass,
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
for (const member of entity.fieldsTypeMembers) {
|
|
506
|
+
if (schemaNames.has(member.name)) continue;
|
|
507
|
+
if (declaredByName.has(member.name)) continue;
|
|
508
|
+
ops.push({
|
|
509
|
+
kind: 'remove',
|
|
510
|
+
field: member.name,
|
|
511
|
+
detail: `in the Fields type but not present in ${component.name}`,
|
|
512
|
+
expected: null,
|
|
513
|
+
actual: member.typeText,
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
ops.push(...recordTypeOps(entity, nestedFields));
|
|
518
|
+
|
|
519
|
+
const suppressed = suppressAnnotated(
|
|
520
|
+
entity,
|
|
521
|
+
ops,
|
|
522
|
+
[...declaredByName.keys(), ...typeMemberByName.keys()],
|
|
523
|
+
schemaNames,
|
|
524
|
+
);
|
|
525
|
+
|
|
526
|
+
return {
|
|
527
|
+
className: entity.className,
|
|
528
|
+
file: entity.file,
|
|
529
|
+
component: component.name,
|
|
530
|
+
ops: suppressed.ops,
|
|
531
|
+
nestedEntities: [...new Set(nestedEntities)],
|
|
532
|
+
suppressions: suppressed.suppressions,
|
|
533
|
+
schemaSubset: entity.schemaSubset,
|
|
534
|
+
subsetDropped: suppressed.subsetDropped,
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* Split the ops an entity's annotations have already answered away from the
|
|
540
|
+
* ops that still need attention.
|
|
541
|
+
*
|
|
542
|
+
* The report and the generated assertions have to agree: an entity whose
|
|
543
|
+
* `@schemaIgnore` reasons make `npm run entity:check` pass but whose ops still
|
|
544
|
+
* fill the plan would never converge, and the next run would propose undoing
|
|
545
|
+
* the decision. So the two honour the same tags, with the same meanings:
|
|
546
|
+
*
|
|
547
|
+
* - `@schemaIgnore <field>` - the schema is wrong here; hold back every op
|
|
548
|
+
* about that field, in both directions.
|
|
549
|
+
* - `@schemaSubset` - the entity is a deliberate projection, so only the fields
|
|
550
|
+
* it declares are compared. Extra and misnamed fields still surface, which
|
|
551
|
+
* mirrors the `SubsetKeys` assertion.
|
|
552
|
+
*
|
|
553
|
+
* Suppressed ops are *partitioned*, not discarded. An annotation that silences
|
|
554
|
+
* a disagreement the broker has since fixed is indistinguishable from one still
|
|
555
|
+
* doing work unless the ops it would have raised are still countable, so they
|
|
556
|
+
* ride along on the diff and `plan.mjs` reports them.
|
|
557
|
+
*
|
|
558
|
+
* @param {ParsedEntity} entity
|
|
559
|
+
* @param {FieldOp[]} ops
|
|
560
|
+
* @param {string[]} declaredNames every field the entity declares
|
|
561
|
+
* @param {Set<string>} schemaNames every field the component sends
|
|
562
|
+
* @returns {{ ops: FieldOp[], suppressions: Suppression[], subsetDropped: string[] }}
|
|
563
|
+
*/
|
|
564
|
+
function suppressAnnotated(entity, ops, declaredNames, schemaNames) {
|
|
565
|
+
const ignores = entity.schemaIgnores ?? {};
|
|
566
|
+
const ignored = new Set(Object.keys(ignores));
|
|
567
|
+
const declared = new Set(declaredNames);
|
|
568
|
+
// `recordTypeOps` names this one after the Record type rather than a field,
|
|
569
|
+
// so neither filter applies: a missing Record type is a fact about the whole
|
|
570
|
+
// entity, not about a field a projection chose to leave out.
|
|
571
|
+
const wholeEntity = entity.recordTypeName ?? '(record type)';
|
|
572
|
+
|
|
573
|
+
/** @type {FieldOp[]} */
|
|
574
|
+
const kept = [];
|
|
575
|
+
/** @type {Map<string, FieldOp[]>} */
|
|
576
|
+
const byIgnoredField = new Map([...ignored].map((field) => [field, []]));
|
|
577
|
+
/** @type {Set<string>} */
|
|
578
|
+
const subsetDropped = new Set();
|
|
579
|
+
|
|
580
|
+
for (const op of ops) {
|
|
581
|
+
if (op.field !== wholeEntity) {
|
|
582
|
+
if (ignored.has(op.field)) {
|
|
583
|
+
byIgnoredField.get(op.field)?.push(op);
|
|
584
|
+
continue;
|
|
585
|
+
}
|
|
586
|
+
if (entity.schemaSubset !== null && !declared.has(op.field)) {
|
|
587
|
+
subsetDropped.add(op.field);
|
|
588
|
+
continue;
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
kept.push(op);
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/** @type {Suppression[]} */
|
|
595
|
+
const suppressions = [...ignored].sort().map((field) => {
|
|
596
|
+
const dropped = byIgnoredField.get(field) ?? [];
|
|
597
|
+
const exists = schemaNames.has(field) || declared.has(field);
|
|
598
|
+
|
|
599
|
+
return {
|
|
600
|
+
field,
|
|
601
|
+
reason: ignores[field] ?? '',
|
|
602
|
+
status: !exists ? 'orphaned' : dropped.length ? 'needed' : 'redundant',
|
|
603
|
+
ops: dropped,
|
|
604
|
+
};
|
|
605
|
+
});
|
|
606
|
+
|
|
607
|
+
return { ops: kept, suppressions, subsetDropped: [...subsetDropped].sort() };
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/**
|
|
611
|
+
* Check that the Record type performs the Immutable substitution the convention
|
|
612
|
+
* requires.
|
|
613
|
+
*
|
|
614
|
+
* `EntityNameFields` carries the raw wire shape, so `EntityNameRecord` has to
|
|
615
|
+
* substitute nested members via `Omit<…> & { … }`. An entity with nested fields
|
|
616
|
+
* whose Record type is a bare `EntityRecord<XEntityFields>` has not done that.
|
|
617
|
+
*
|
|
618
|
+
* Deliberately a containment check rather than a parse: the aim is to catch the
|
|
619
|
+
* substitution being absent, not to police how it is written.
|
|
620
|
+
*
|
|
621
|
+
* @param {ParsedEntity} entity
|
|
622
|
+
* @param {{ name: string, recordTypeText: string }[]} nestedFields
|
|
623
|
+
* @returns {FieldOp[]}
|
|
624
|
+
*/
|
|
625
|
+
function recordTypeOps(entity, nestedFields) {
|
|
626
|
+
if (!nestedFields.length) return [];
|
|
627
|
+
|
|
628
|
+
const recordText = entity.recordTypeText;
|
|
629
|
+
if (!recordText) {
|
|
630
|
+
return [
|
|
631
|
+
{
|
|
632
|
+
kind: 'record-type',
|
|
633
|
+
field: entity.recordTypeName ?? '(record type)',
|
|
634
|
+
detail: `no Record type found, but ${nestedFields.length} nested field(s) need the Immutable substitution`,
|
|
635
|
+
expected: null,
|
|
636
|
+
actual: null,
|
|
637
|
+
},
|
|
638
|
+
];
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/** @type {FieldOp[]} */
|
|
642
|
+
const ops = [];
|
|
643
|
+
|
|
644
|
+
for (const nested of nestedFields) {
|
|
645
|
+
if (recordText.includes(nested.recordTypeText)) continue;
|
|
646
|
+
ops.push({
|
|
647
|
+
kind: 'record-type',
|
|
648
|
+
field: nested.name,
|
|
649
|
+
detail: `Record type should substitute ${nested.recordTypeText}`,
|
|
650
|
+
expected: nested.recordTypeText,
|
|
651
|
+
actual: null,
|
|
652
|
+
});
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
return ops;
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
/**
|
|
659
|
+
* Diff every resolved entity.
|
|
660
|
+
*
|
|
661
|
+
* Entities that are excluded or unresolved are skipped: there is nothing to
|
|
662
|
+
* compare them against, and `resolve.mjs` already reports why.
|
|
663
|
+
*
|
|
664
|
+
* @param {object} input
|
|
665
|
+
* @param {ParsedEntity[]} input.entities
|
|
666
|
+
* @param {import('./resolve.mjs').Resolution[]} input.resolutions
|
|
667
|
+
* @param {ParsedSchema} input.schema
|
|
668
|
+
* @param {Record<string, { className: string, fieldsTypeName: string, recordTypeName: string }>} [input.entityByComponent]
|
|
669
|
+
* @param {Record<string, { name: string, typeName: string, file: string }>} [input.enumByComponent]
|
|
670
|
+
* @param {Readonly<Record<string, readonly string[]>>} [input.equivalentFieldClasses]
|
|
671
|
+
* @returns {EntityDiff[]}
|
|
672
|
+
*/
|
|
673
|
+
export function diffAll({
|
|
674
|
+
entities,
|
|
675
|
+
resolutions,
|
|
676
|
+
schema,
|
|
677
|
+
entityByComponent = {},
|
|
678
|
+
enumByComponent = {},
|
|
679
|
+
equivalentFieldClasses = DEFAULT_EQUIVALENT_FIELD_CLASSES,
|
|
680
|
+
}) {
|
|
681
|
+
const byClassName = new Map(entities.map((e) => [e.className, e]));
|
|
682
|
+
|
|
683
|
+
/** @type {EntityDiff[]} */
|
|
684
|
+
const diffs = [];
|
|
685
|
+
|
|
686
|
+
for (const resolution of resolutions) {
|
|
687
|
+
if (!resolution.component) continue;
|
|
688
|
+
|
|
689
|
+
const entity = byClassName.get(resolution.className);
|
|
690
|
+
const component = schema.components[resolution.component];
|
|
691
|
+
if (!entity || component?.kind !== 'object') continue;
|
|
692
|
+
|
|
693
|
+
diffs.push(
|
|
694
|
+
diffEntity({
|
|
695
|
+
entity,
|
|
696
|
+
component,
|
|
697
|
+
components: schema.components,
|
|
698
|
+
entityByComponent,
|
|
699
|
+
enumByComponent,
|
|
700
|
+
equivalentFieldClasses,
|
|
701
|
+
}),
|
|
702
|
+
);
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
return diffs;
|
|
706
|
+
}
|