@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,323 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* Staleness tracking across runs, via a lockfile of content hashes.
|
|
4
|
+
*
|
|
5
|
+
* ## What this actually buys
|
|
6
|
+
*
|
|
7
|
+
* Not compute. Parsing `schema.d.ts` has to happen anyway in order to hash it,
|
|
8
|
+
* and the diff itself is cheap. The win is in the *report*: on a second run,
|
|
9
|
+
* drift you have already read - and consciously deferred - should not be
|
|
10
|
+
* indistinguishable from drift that just appeared upstream.
|
|
11
|
+
*
|
|
12
|
+
* Without this, a sync that surfaces 170 known ops teaches you to skim. With it,
|
|
13
|
+
* the second run says "19 unchanged since last sync" in one line and shows only
|
|
14
|
+
* what is new.
|
|
15
|
+
*
|
|
16
|
+
* ## Three hashes per entity
|
|
17
|
+
*
|
|
18
|
+
* - **component** - the normalised schema slice. Changes when the API changes.
|
|
19
|
+
* - **entity** - the parsed entity declaration. Changes when someone edits it.
|
|
20
|
+
* - **ops** - the drift itself. Changes when the disagreement changes, which
|
|
21
|
+
* catches the case where component and entity both changed in ways that cancel
|
|
22
|
+
* out or compound.
|
|
23
|
+
*
|
|
24
|
+
* All three are recorded because they answer different questions, and the report
|
|
25
|
+
* says *which* of them moved.
|
|
26
|
+
*
|
|
27
|
+
* ## Dirty propagation
|
|
28
|
+
*
|
|
29
|
+
* A changed child dirties its parents, because a parent's nested field type names
|
|
30
|
+
* the child's Fields type. That is `propagateDirty` in `graph.mjs`; this module
|
|
31
|
+
* supplies the seeds.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
import { createHash } from 'node:crypto';
|
|
35
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
36
|
+
import { dirname } from 'node:path';
|
|
37
|
+
|
|
38
|
+
import { propagateDirty } from './graph.mjs';
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Bumped when the hashing scheme changes, so an old lockfile is discarded rather
|
|
42
|
+
* than compared against hashes it cannot produce - which would report the entire
|
|
43
|
+
* codebase as changed.
|
|
44
|
+
*/
|
|
45
|
+
export const LOCK_VERSION = 1;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @typedef {object} Lock
|
|
49
|
+
* @property {number} version
|
|
50
|
+
* @property {Record<string, string>} components component name -> hash
|
|
51
|
+
* @property {Record<string, { entity: string, component: string, ops: string }>} entities
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @typedef {'new' | 'component-changed' | 'entity-changed' | 'ops-changed' | 'dependency-changed' | 'unchanged'} Staleness
|
|
56
|
+
*/
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Stable JSON: object keys sorted at every level, so key ordering cannot change
|
|
60
|
+
* a hash. Without this, a reordered field list would read as an API change.
|
|
61
|
+
*
|
|
62
|
+
* @param {unknown} value
|
|
63
|
+
* @returns {string}
|
|
64
|
+
*/
|
|
65
|
+
export function stableStringify(value) {
|
|
66
|
+
if (value === null || typeof value !== 'object') return JSON.stringify(value);
|
|
67
|
+
|
|
68
|
+
if (Array.isArray(value)) {
|
|
69
|
+
// Array order is meaningful (field order), so it is preserved.
|
|
70
|
+
return `[${value.map(stableStringify).join(',')}]`;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const entries = Object.entries(value)
|
|
74
|
+
.filter(([, entryValue]) => entryValue !== undefined)
|
|
75
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
76
|
+
|
|
77
|
+
return `{${entries
|
|
78
|
+
.map(
|
|
79
|
+
([key, entryValue]) =>
|
|
80
|
+
`${JSON.stringify(key)}:${stableStringify(entryValue)}`,
|
|
81
|
+
)
|
|
82
|
+
.join(',')}}`;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* @param {unknown} value
|
|
87
|
+
* @returns {string} first 16 hex chars of the sha256, enough to make a collision
|
|
88
|
+
* irrelevant here while keeping the lockfile readable
|
|
89
|
+
*/
|
|
90
|
+
export function hashValue(value) {
|
|
91
|
+
return createHash('sha256')
|
|
92
|
+
.update(stableStringify(value))
|
|
93
|
+
.digest('hex')
|
|
94
|
+
.slice(0, 16);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Hash a component's field slice.
|
|
99
|
+
*
|
|
100
|
+
* Only the signals the diff consumes are hashed. `description` is deliberately
|
|
101
|
+
* excluded: a reworded docstring upstream is not drift, and including it would
|
|
102
|
+
* dirty half the entities on an unrelated backend commit.
|
|
103
|
+
*
|
|
104
|
+
* @param {import('./parse-schema.mjs').SchemaComponent} component
|
|
105
|
+
* @returns {string}
|
|
106
|
+
*/
|
|
107
|
+
export function hashComponent(component) {
|
|
108
|
+
return hashValue({
|
|
109
|
+
kind: component.kind,
|
|
110
|
+
enumValues: component.enumValues,
|
|
111
|
+
fields: component.fields.map((field) => ({
|
|
112
|
+
name: field.name,
|
|
113
|
+
optional: field.optional,
|
|
114
|
+
readonly: field.readonly,
|
|
115
|
+
nullable: field.nullable,
|
|
116
|
+
isArray: field.isArray,
|
|
117
|
+
refs: field.refs,
|
|
118
|
+
tsType: field.tsType,
|
|
119
|
+
format: field.format,
|
|
120
|
+
})),
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Hash an entity's declarations.
|
|
126
|
+
*
|
|
127
|
+
* Comments, imports and method bodies are excluded, so reformatting a file or
|
|
128
|
+
* editing a docstring does not read as a change.
|
|
129
|
+
*
|
|
130
|
+
* @param {import('./parse-entities.mjs').ParsedEntity} entity
|
|
131
|
+
* @returns {string}
|
|
132
|
+
*/
|
|
133
|
+
export function hashEntity(entity) {
|
|
134
|
+
return hashValue({
|
|
135
|
+
schemaAnnotation: entity.schemaAnnotation,
|
|
136
|
+
schemaAlso: entity.schemaAlso ?? [],
|
|
137
|
+
noSchema: entity.noSchema,
|
|
138
|
+
// The suppression annotations are hashed by field name only, not by reason.
|
|
139
|
+
// Adding or removing one changes what is checked, so it has to dirty the
|
|
140
|
+
// entity - otherwise a removed `@schemaIgnore` would surface only if the
|
|
141
|
+
// resulting ops happened to differ, and a reworded reason would read as
|
|
142
|
+
// drift when nothing about the comparison changed.
|
|
143
|
+
schemaIgnores: Object.keys(entity.schemaIgnores ?? {}).sort(),
|
|
144
|
+
schemaSubset: entity.schemaSubset !== null,
|
|
145
|
+
apiBase: entity.apiBase,
|
|
146
|
+
paginated: entity.paginated,
|
|
147
|
+
duck: entity.duck,
|
|
148
|
+
fieldsTypeName: entity.fieldsTypeName,
|
|
149
|
+
recordTypeName: entity.recordTypeName,
|
|
150
|
+
recordTypeText: entity.recordTypeText,
|
|
151
|
+
fields: entity.fields.map((field) => ({
|
|
152
|
+
name: field.name,
|
|
153
|
+
fieldClass: field.fieldClass,
|
|
154
|
+
entityRef: field.entityRef,
|
|
155
|
+
many: field.many,
|
|
156
|
+
readOnly: field.readOnly,
|
|
157
|
+
blank: field.blank,
|
|
158
|
+
})),
|
|
159
|
+
fieldsTypeMembers: entity.fieldsTypeMembers,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Hash a diff's ops, ignoring their order.
|
|
165
|
+
*
|
|
166
|
+
* @param {import('./diff.mjs').FieldOp[]} ops
|
|
167
|
+
* @returns {string}
|
|
168
|
+
*/
|
|
169
|
+
export function hashOps(ops) {
|
|
170
|
+
return hashValue(
|
|
171
|
+
ops.map((op) => `${op.kind}:${op.field}:${op.detail}`).sort(),
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Read the lockfile, discarding it if absent, unreadable or of another version.
|
|
177
|
+
*
|
|
178
|
+
* A corrupt lockfile must not be fatal: the worst case is reporting everything
|
|
179
|
+
* as new, which is exactly the behaviour of a first run.
|
|
180
|
+
*
|
|
181
|
+
* @param {string} path
|
|
182
|
+
* @returns {Lock}
|
|
183
|
+
*/
|
|
184
|
+
export function readLock(path) {
|
|
185
|
+
/** @type {Lock} */
|
|
186
|
+
const empty = { version: LOCK_VERSION, components: {}, entities: {} };
|
|
187
|
+
|
|
188
|
+
if (!existsSync(path)) return empty;
|
|
189
|
+
|
|
190
|
+
try {
|
|
191
|
+
const parsed = JSON.parse(readFileSync(path, 'utf8'));
|
|
192
|
+
if (parsed?.version !== LOCK_VERSION) return empty;
|
|
193
|
+
return {
|
|
194
|
+
version: LOCK_VERSION,
|
|
195
|
+
components: parsed.components ?? {},
|
|
196
|
+
entities: parsed.entities ?? {},
|
|
197
|
+
};
|
|
198
|
+
} catch {
|
|
199
|
+
return empty;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* @param {string} path
|
|
205
|
+
* @param {Lock} lock
|
|
206
|
+
*/
|
|
207
|
+
export function writeLock(path, lock) {
|
|
208
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
209
|
+
writeFileSync(path, `${JSON.stringify(lock, null, 2)}\n`);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Build the lockfile for the current state.
|
|
214
|
+
*
|
|
215
|
+
* @param {object} input
|
|
216
|
+
* @param {import('./parse-schema.mjs').ParsedSchema} input.schema
|
|
217
|
+
* @param {import('./parse-entities.mjs').ParsedEntity[]} input.entities
|
|
218
|
+
* @param {import('./diff.mjs').EntityDiff[]} input.diffs
|
|
219
|
+
* @param {import('./parse-entities.mjs').ParsedEnum[]} [input.enums]
|
|
220
|
+
* @returns {Lock}
|
|
221
|
+
*/
|
|
222
|
+
export function buildLock({ schema, entities, diffs, enums = [] }) {
|
|
223
|
+
/** @type {Record<string, string>} */
|
|
224
|
+
const components = {};
|
|
225
|
+
for (const [name, component] of Object.entries(schema.components)) {
|
|
226
|
+
components[name] = hashComponent(component);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const entityByClassName = new Map(entities.map((e) => [e.className, e]));
|
|
230
|
+
|
|
231
|
+
// A choices dict belongs to no class, so it is attributed to its file: an
|
|
232
|
+
// edit to `ApplicationStatus` dirties the entities declared alongside it,
|
|
233
|
+
// which is where the field types that reference it live.
|
|
234
|
+
/** @type {Map<string, string[]>} */
|
|
235
|
+
const enumsByFile = new Map();
|
|
236
|
+
for (const parsed of enums) {
|
|
237
|
+
const group = enumsByFile.get(parsed.file) ?? [];
|
|
238
|
+
group.push(`${parsed.name}:${parsed.schemaEnum ?? ''}:${parsed.values}`);
|
|
239
|
+
enumsByFile.set(parsed.file, group);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** @type {Lock['entities']} */
|
|
243
|
+
const lockEntities = {};
|
|
244
|
+
for (const diff of diffs) {
|
|
245
|
+
const entity = entityByClassName.get(diff.className);
|
|
246
|
+
if (!entity) continue;
|
|
247
|
+
|
|
248
|
+
lockEntities[diff.className] = {
|
|
249
|
+
entity: hashValue({
|
|
250
|
+
entity: hashEntity(entity),
|
|
251
|
+
enums: (enumsByFile.get(entity.file) ?? []).sort(),
|
|
252
|
+
}),
|
|
253
|
+
component: components[diff.component] ?? '',
|
|
254
|
+
ops: hashOps(diff.ops),
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return { version: LOCK_VERSION, components, entities: lockEntities };
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Classify each diffed entity against the previous run.
|
|
263
|
+
*
|
|
264
|
+
* The order of checks is the order of usefulness to a reader: what changed
|
|
265
|
+
* upstream, then what changed locally, then what changed only as a consequence.
|
|
266
|
+
*
|
|
267
|
+
* @param {object} input
|
|
268
|
+
* @param {import('./diff.mjs').EntityDiff[]} input.diffs
|
|
269
|
+
* @param {Lock} input.previous
|
|
270
|
+
* @param {Lock} input.current
|
|
271
|
+
* @param {import('./graph.mjs').Graph} input.graph entity -> entities it embeds
|
|
272
|
+
* @returns {Map<string, Staleness>}
|
|
273
|
+
*/
|
|
274
|
+
export function classifyStaleness({ diffs, previous, current, graph }) {
|
|
275
|
+
/** @type {Map<string, Staleness>} */
|
|
276
|
+
const staleness = new Map();
|
|
277
|
+
|
|
278
|
+
// Seeds for propagation: entities whose own inputs moved.
|
|
279
|
+
/** @type {string[]} */
|
|
280
|
+
const seeds = [];
|
|
281
|
+
|
|
282
|
+
for (const diff of diffs) {
|
|
283
|
+
const before = previous.entities[diff.className];
|
|
284
|
+
const after = current.entities[diff.className];
|
|
285
|
+
|
|
286
|
+
if (!before) {
|
|
287
|
+
staleness.set(diff.className, 'new');
|
|
288
|
+
seeds.push(diff.className);
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (before.component !== after?.component) {
|
|
293
|
+
staleness.set(diff.className, 'component-changed');
|
|
294
|
+
seeds.push(diff.className);
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (before.entity !== after?.entity) {
|
|
299
|
+
staleness.set(diff.className, 'entity-changed');
|
|
300
|
+
seeds.push(diff.className);
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
if (before.ops !== after?.ops) {
|
|
305
|
+
staleness.set(diff.className, 'ops-changed');
|
|
306
|
+
seeds.push(diff.className);
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
staleness.set(diff.className, 'unchanged');
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// A changed child dirties its parents: the parent's nested field type names
|
|
314
|
+
// the child's Fields type, so the parent may need editing even though nothing
|
|
315
|
+
// about the parent itself moved.
|
|
316
|
+
for (const className of propagateDirty(graph, seeds)) {
|
|
317
|
+
if (staleness.get(className) === 'unchanged') {
|
|
318
|
+
staleness.set(className, 'dependency-changed');
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
return staleness;
|
|
323
|
+
}
|