@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.
Files changed (50) hide show
  1. package/.claude-plugin/marketplace.json +14 -0
  2. package/.claude-plugin/plugin.json +11 -0
  3. package/CHANGELOG.md +15 -0
  4. package/LICENSE +21 -0
  5. package/README.md +240 -2
  6. package/agents/entity-editor.md +16 -0
  7. package/bin/entity-sync.mjs +36 -0
  8. package/dist/cli.d.mts +20 -0
  9. package/dist/commands/asserts.d.mts +4 -0
  10. package/dist/commands/check.d.mts +6 -0
  11. package/dist/commands/inspect.d.mts +30 -0
  12. package/dist/commands/plan.d.mts +28 -0
  13. package/dist/commands/schema.d.mts +39 -0
  14. package/dist/config.d.mts +175 -0
  15. package/dist/index.d.mts +5 -0
  16. package/dist/lib/cache.d.mts +120 -0
  17. package/dist/lib/diff.d.mts +228 -0
  18. package/dist/lib/field-map.d.mts +93 -0
  19. package/dist/lib/gen-asserts.d.mts +70 -0
  20. package/dist/lib/graph.d.mts +90 -0
  21. package/dist/lib/parse-entities.d.mts +257 -0
  22. package/dist/lib/parse-schema.d.mts +98 -0
  23. package/dist/lib/resolve.d.mts +135 -0
  24. package/dist/project.d.mts +60 -0
  25. package/hooks/editor-guard.mjs +86 -0
  26. package/hooks/hooks.json +15 -0
  27. package/package.json +54 -4
  28. package/skills/entity/SKILL.md +105 -0
  29. package/skills/entity/reference/create.md +74 -0
  30. package/skills/entity/reference/editor.md +80 -0
  31. package/skills/entity/reference/enums.md +89 -0
  32. package/skills/entity/reference/fields.md +101 -0
  33. package/skills/entity/reference/sync.md +191 -0
  34. package/src/cli.mjs +110 -0
  35. package/src/commands/asserts.mjs +52 -0
  36. package/src/commands/check.mjs +88 -0
  37. package/src/commands/inspect.mjs +290 -0
  38. package/src/commands/plan.mjs +542 -0
  39. package/src/commands/schema.mjs +206 -0
  40. package/src/config.mjs +205 -0
  41. package/src/index.mjs +19 -0
  42. package/src/lib/cache.mjs +323 -0
  43. package/src/lib/diff.mjs +706 -0
  44. package/src/lib/field-map.mjs +438 -0
  45. package/src/lib/gen-asserts.mjs +478 -0
  46. package/src/lib/graph.mjs +284 -0
  47. package/src/lib/parse-entities.mjs +935 -0
  48. package/src/lib/parse-schema.mjs +667 -0
  49. package/src/lib/resolve.mjs +455 -0
  50. package/src/project.mjs +115 -0
@@ -0,0 +1,284 @@
1
+ // @ts-check
2
+ /**
3
+ * Dependency-graph algorithms for ordering entity edits.
4
+ *
5
+ * The `$ref` graph between schema components mirrors the `EntityField` graph
6
+ * between entities, and a parent depends on its children twice over: its
7
+ * `EntityField({ entity: Child })` names the child class, and its `Fields` type
8
+ * names `ChildEntityFields`. So children must be settled before parents, or
9
+ * editing a parent has to be redone once the child changes.
10
+ *
11
+ * Three standard build-system algorithms cover it. They are kept here as pure
12
+ * functions over plain `Map`s so they can be tested without any schema at all.
13
+ *
14
+ * - `toWaves` - Kahn's algorithm, grouped into levels
15
+ * - `findCycles` - Tarjan's strongly-connected components
16
+ * - `propagateDirty` - reverse-edge worklist to a fixpoint
17
+ *
18
+ * ## Edge direction
19
+ *
20
+ * Throughout, an edge points from a node to what it *depends on*:
21
+ * `Parent -> Child`. `toWaves` therefore emits leaves first.
22
+ */
23
+
24
+ /**
25
+ * @typedef {Map<string, Set<string>>} Graph
26
+ * node -> the set of nodes it depends on
27
+ */
28
+
29
+ /**
30
+ * Build a graph from an adjacency description, adding any node that appears only
31
+ * as a dependency.
32
+ *
33
+ * Normalising here means the algorithms below never have to guard against a
34
+ * dangling edge, which is the usual source of a silently-dropped node.
35
+ *
36
+ * @param {Record<string, string[]> | Iterable<[string, Iterable<string>]>} edges
37
+ * @returns {Graph}
38
+ */
39
+ export function buildGraph(edges) {
40
+ /** @type {Graph} */
41
+ const graph = new Map();
42
+
43
+ /** @param {string} node */
44
+ const ensure = (node) => {
45
+ if (!graph.has(node)) graph.set(node, new Set());
46
+ return /** @type {Set<string>} */ (graph.get(node));
47
+ };
48
+
49
+ const entries = Symbol.iterator in edges ? edges : Object.entries(edges);
50
+
51
+ for (const [node, dependencies] of entries) {
52
+ const set = ensure(node);
53
+ for (const dependency of dependencies) {
54
+ // Self-edges carry no ordering information and would deadlock the sort.
55
+ if (dependency === node) continue;
56
+ set.add(dependency);
57
+ ensure(dependency);
58
+ }
59
+ }
60
+
61
+ return graph;
62
+ }
63
+
64
+ /**
65
+ * Reverse a graph: `node -> dependents`.
66
+ *
67
+ * @param {Graph} graph
68
+ * @returns {Graph}
69
+ */
70
+ export function reverseGraph(graph) {
71
+ /** @type {Graph} */
72
+ const reversed = new Map();
73
+
74
+ for (const node of graph.keys()) reversed.set(node, new Set());
75
+
76
+ for (const [node, dependencies] of graph) {
77
+ for (const dependency of dependencies) {
78
+ /** @type {Set<string>} */ (reversed.get(dependency)).add(node);
79
+ }
80
+ }
81
+
82
+ return reversed;
83
+ }
84
+
85
+ /**
86
+ * Group nodes into dependency waves: everything in wave N depends only on nodes
87
+ * in earlier waves.
88
+ *
89
+ * Kahn's algorithm, but emitting whole levels rather than a flat order, because
90
+ * the useful property for editing is that nodes *within* a wave are mutually
91
+ * independent - so editing one cannot invalidate another.
92
+ *
93
+ * Nodes caught in a cycle can never reach in-degree zero, so they are returned
94
+ * separately rather than silently dropped. Use `findCycles` to see what they are.
95
+ *
96
+ * @param {Graph} graph
97
+ * @returns {{ waves: string[][], unordered: string[] }}
98
+ */
99
+ export function toWaves(graph) {
100
+ /** @type {Map<string, number>} */
101
+ const remaining = new Map();
102
+ for (const [node, dependencies] of graph) {
103
+ remaining.set(node, dependencies.size);
104
+ }
105
+
106
+ const dependents = reverseGraph(graph);
107
+
108
+ /** @type {string[][]} */
109
+ const waves = [];
110
+ const placed = new Set();
111
+
112
+ while (placed.size < graph.size) {
113
+ // Sorted for determinism: the same graph must always produce the same
114
+ // report, or a diff of two runs becomes unreadable.
115
+ const wave = [...remaining.keys()]
116
+ .filter((node) => !placed.has(node) && remaining.get(node) === 0)
117
+ .sort();
118
+
119
+ if (!wave.length) break;
120
+
121
+ for (const node of wave) placed.add(node);
122
+
123
+ for (const node of wave) {
124
+ for (const dependent of dependents.get(node) ?? []) {
125
+ remaining.set(dependent, (remaining.get(dependent) ?? 1) - 1);
126
+ }
127
+ }
128
+
129
+ waves.push(wave);
130
+ }
131
+
132
+ const unordered = [...graph.keys()]
133
+ .filter((node) => !placed.has(node))
134
+ .sort();
135
+
136
+ return { waves, unordered };
137
+ }
138
+
139
+ /**
140
+ * Find strongly-connected components of size > 1, plus self-loops.
141
+ *
142
+ * Tarjan's algorithm, iterative rather than recursive so a deep chain cannot
143
+ * blow the stack. Mutually recursive DRF serializers are entirely normal, and a
144
+ * cycle must be reported as one unit to edit atomically rather than deadlocking
145
+ * the topological sort.
146
+ *
147
+ * @param {Graph} graph
148
+ * @returns {string[][]} each cycle, members sorted; cycles sorted by first member
149
+ */
150
+ export function findCycles(graph) {
151
+ let nextIndex = 0;
152
+ /** @type {Map<string, number>} */
153
+ const index = new Map();
154
+ /** @type {Map<string, number>} */
155
+ const lowLink = new Map();
156
+ /** @type {string[]} */
157
+ const stack = [];
158
+ const onStack = new Set();
159
+ /** @type {string[][]} */
160
+ const components = [];
161
+
162
+ for (const root of [...graph.keys()].sort()) {
163
+ if (index.has(root)) continue;
164
+
165
+ /**
166
+ * Explicit work stack: each frame is a node plus how many of its
167
+ * dependencies have been visited so far.
168
+ *
169
+ * @type {{ node: string, dependencies: string[], next: number }[]}
170
+ */
171
+ const work = [
172
+ {
173
+ node: root,
174
+ dependencies: [...(graph.get(root) ?? [])].sort(),
175
+ next: 0,
176
+ },
177
+ ];
178
+
179
+ index.set(root, nextIndex);
180
+ lowLink.set(root, nextIndex);
181
+ nextIndex += 1;
182
+ stack.push(root);
183
+ onStack.add(root);
184
+
185
+ while (work.length) {
186
+ const frame = work[work.length - 1];
187
+
188
+ if (frame.next < frame.dependencies.length) {
189
+ const dependency = frame.dependencies[frame.next];
190
+ frame.next += 1;
191
+
192
+ if (!index.has(dependency)) {
193
+ index.set(dependency, nextIndex);
194
+ lowLink.set(dependency, nextIndex);
195
+ nextIndex += 1;
196
+ stack.push(dependency);
197
+ onStack.add(dependency);
198
+ work.push({
199
+ node: dependency,
200
+ dependencies: [...(graph.get(dependency) ?? [])].sort(),
201
+ next: 0,
202
+ });
203
+ } else if (onStack.has(dependency)) {
204
+ // A back-edge to a node still on the stack: this frame can reach at
205
+ // least as far up as that node.
206
+ lowLink.set(
207
+ frame.node,
208
+ Math.min(
209
+ /** @type {number} */ (lowLink.get(frame.node)),
210
+ /** @type {number} */ (index.get(dependency)),
211
+ ),
212
+ );
213
+ }
214
+ continue;
215
+ }
216
+
217
+ // Frame exhausted: fold its low-link into its parent, then close it.
218
+ work.pop();
219
+
220
+ const parent = work[work.length - 1];
221
+ if (parent) {
222
+ lowLink.set(
223
+ parent.node,
224
+ Math.min(
225
+ /** @type {number} */ (lowLink.get(parent.node)),
226
+ /** @type {number} */ (lowLink.get(frame.node)),
227
+ ),
228
+ );
229
+ }
230
+
231
+ if (lowLink.get(frame.node) === index.get(frame.node)) {
232
+ /** @type {string[]} */
233
+ const component = [];
234
+ let member;
235
+ do {
236
+ member = /** @type {string} */ (stack.pop());
237
+ onStack.delete(member);
238
+ component.push(member);
239
+ } while (member !== frame.node);
240
+
241
+ const isSelfLoop =
242
+ component.length === 1 && graph.get(component[0])?.has(component[0]);
243
+
244
+ if (component.length > 1 || isSelfLoop) {
245
+ components.push(component.sort());
246
+ }
247
+ }
248
+ }
249
+ }
250
+
251
+ return components.sort((a, b) => a[0].localeCompare(b[0]));
252
+ }
253
+
254
+ /**
255
+ * Expand a set of directly-changed nodes to everything transitively affected.
256
+ *
257
+ * When a child's shape changes its parents are affected too, because a parent's
258
+ * nested field type names the child. A worklist walked over reverse edges to a
259
+ * fixpoint is the cheapest correct way to get that, and terminates because each
260
+ * node enters the result at most once.
261
+ *
262
+ * @param {Graph} graph node -> dependencies
263
+ * @param {Iterable<string>} seeds directly-changed nodes
264
+ * @returns {Set<string>} seeds plus every transitive dependent
265
+ */
266
+ export function propagateDirty(graph, seeds) {
267
+ const dependents = reverseGraph(graph);
268
+
269
+ /** @type {Set<string>} */
270
+ const dirty = new Set();
271
+ const queue = [...seeds];
272
+
273
+ while (queue.length) {
274
+ const node = /** @type {string} */ (queue.pop());
275
+ if (dirty.has(node)) continue;
276
+ dirty.add(node);
277
+
278
+ for (const dependent of dependents.get(node) ?? []) {
279
+ if (!dirty.has(dependent)) queue.push(dependent);
280
+ }
281
+ }
282
+
283
+ return dirty;
284
+ }