@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,5 @@
1
+ export { loadProject } from "./project.mjs";
2
+ export { main } from "./cli.mjs";
3
+ export type Config = import("./config.mjs").Config;
4
+ export type ResolvedConfig = import("./config.mjs").ResolvedConfig;
5
+ export { defineConfig, loadConfig, resolveConfig, CONFIG_FILENAMES } from "./config.mjs";
@@ -0,0 +1,120 @@
1
+ /**
2
+ * @typedef {object} Lock
3
+ * @property {number} version
4
+ * @property {Record<string, string>} components component name -> hash
5
+ * @property {Record<string, { entity: string, component: string, ops: string }>} entities
6
+ */
7
+ /**
8
+ * @typedef {'new' | 'component-changed' | 'entity-changed' | 'ops-changed' | 'dependency-changed' | 'unchanged'} Staleness
9
+ */
10
+ /**
11
+ * Stable JSON: object keys sorted at every level, so key ordering cannot change
12
+ * a hash. Without this, a reordered field list would read as an API change.
13
+ *
14
+ * @param {unknown} value
15
+ * @returns {string}
16
+ */
17
+ export function stableStringify(value: unknown): string;
18
+ /**
19
+ * @param {unknown} value
20
+ * @returns {string} first 16 hex chars of the sha256, enough to make a collision
21
+ * irrelevant here while keeping the lockfile readable
22
+ */
23
+ export function hashValue(value: unknown): string;
24
+ /**
25
+ * Hash a component's field slice.
26
+ *
27
+ * Only the signals the diff consumes are hashed. `description` is deliberately
28
+ * excluded: a reworded docstring upstream is not drift, and including it would
29
+ * dirty half the entities on an unrelated backend commit.
30
+ *
31
+ * @param {import('./parse-schema.mjs').SchemaComponent} component
32
+ * @returns {string}
33
+ */
34
+ export function hashComponent(component: import("./parse-schema.mjs").SchemaComponent): string;
35
+ /**
36
+ * Hash an entity's declarations.
37
+ *
38
+ * Comments, imports and method bodies are excluded, so reformatting a file or
39
+ * editing a docstring does not read as a change.
40
+ *
41
+ * @param {import('./parse-entities.mjs').ParsedEntity} entity
42
+ * @returns {string}
43
+ */
44
+ export function hashEntity(entity: import("./parse-entities.mjs").ParsedEntity): string;
45
+ /**
46
+ * Hash a diff's ops, ignoring their order.
47
+ *
48
+ * @param {import('./diff.mjs').FieldOp[]} ops
49
+ * @returns {string}
50
+ */
51
+ export function hashOps(ops: import("./diff.mjs").FieldOp[]): string;
52
+ /**
53
+ * Read the lockfile, discarding it if absent, unreadable or of another version.
54
+ *
55
+ * A corrupt lockfile must not be fatal: the worst case is reporting everything
56
+ * as new, which is exactly the behaviour of a first run.
57
+ *
58
+ * @param {string} path
59
+ * @returns {Lock}
60
+ */
61
+ export function readLock(path: string): Lock;
62
+ /**
63
+ * @param {string} path
64
+ * @param {Lock} lock
65
+ */
66
+ export function writeLock(path: string, lock: Lock): void;
67
+ /**
68
+ * Build the lockfile for the current state.
69
+ *
70
+ * @param {object} input
71
+ * @param {import('./parse-schema.mjs').ParsedSchema} input.schema
72
+ * @param {import('./parse-entities.mjs').ParsedEntity[]} input.entities
73
+ * @param {import('./diff.mjs').EntityDiff[]} input.diffs
74
+ * @param {import('./parse-entities.mjs').ParsedEnum[]} [input.enums]
75
+ * @returns {Lock}
76
+ */
77
+ export function buildLock({ schema, entities, diffs, enums }: {
78
+ schema: import("./parse-schema.mjs").ParsedSchema;
79
+ entities: import("./parse-entities.mjs").ParsedEntity[];
80
+ diffs: import("./diff.mjs").EntityDiff[];
81
+ enums?: import("./parse-entities.mjs").ParsedEnum[] | undefined;
82
+ }): Lock;
83
+ /**
84
+ * Classify each diffed entity against the previous run.
85
+ *
86
+ * The order of checks is the order of usefulness to a reader: what changed
87
+ * upstream, then what changed locally, then what changed only as a consequence.
88
+ *
89
+ * @param {object} input
90
+ * @param {import('./diff.mjs').EntityDiff[]} input.diffs
91
+ * @param {Lock} input.previous
92
+ * @param {Lock} input.current
93
+ * @param {import('./graph.mjs').Graph} input.graph entity -> entities it embeds
94
+ * @returns {Map<string, Staleness>}
95
+ */
96
+ export function classifyStaleness({ diffs, previous, current, graph }: {
97
+ diffs: import("./diff.mjs").EntityDiff[];
98
+ previous: Lock;
99
+ current: Lock;
100
+ graph: import("./graph.mjs").Graph;
101
+ }): Map<string, Staleness>;
102
+ /**
103
+ * Bumped when the hashing scheme changes, so an old lockfile is discarded rather
104
+ * than compared against hashes it cannot produce - which would report the entire
105
+ * codebase as changed.
106
+ */
107
+ export const LOCK_VERSION: 1;
108
+ export type Lock = {
109
+ version: number;
110
+ /**
111
+ * component name -> hash
112
+ */
113
+ components: Record<string, string>;
114
+ entities: Record<string, {
115
+ entity: string;
116
+ component: string;
117
+ ops: string;
118
+ }>;
119
+ };
120
+ export type Staleness = "new" | "component-changed" | "entity-changed" | "ops-changed" | "dependency-changed" | "unchanged";
@@ -0,0 +1,228 @@
1
+ /**
2
+ * @typedef {'needed'|'redundant'|'orphaned'} SuppressionStatus
3
+ * `needed` - the field still disagrees, so the annotation is still doing work.
4
+ * `redundant` - the schema and the entity now agree; the broker has caught up
5
+ * and the annotation can be deleted.
6
+ * `orphaned` - the named field is in neither the component nor the Fields
7
+ * type, so the annotation refers to something that no longer exists.
8
+ */
9
+ /**
10
+ * One `@schemaIgnore`, with the ops it is currently hiding.
11
+ *
12
+ * @typedef {object} Suppression
13
+ * @property {string} field
14
+ * @property {string} reason the annotation's own words; '' if none was given
15
+ * @property {SuppressionStatus} status
16
+ * @property {FieldOp[]} ops the ops this annotation dropped, for review
17
+ */
18
+ /**
19
+ * @typedef {object} EntityDiff
20
+ * @property {string} className
21
+ * @property {string} file
22
+ * @property {string} component
23
+ * @property {FieldOp[]} ops
24
+ * @property {string[]} nestedEntities entity classes this one embeds
25
+ * @property {Suppression[]} suppressions every `@schemaIgnore`, and its status
26
+ * @property {string | null} schemaSubset the `@schemaSubset` reason, if any
27
+ * @property {string[]} subsetDropped fields the projection declined to compare
28
+ */
29
+ /**
30
+ * Normalise a TypeScript type for comparison: collapse whitespace and sort
31
+ * top-level union members.
32
+ *
33
+ * Splitting only at depth zero keeps `List<A | B>` and `(A | B)[]` intact, so a
34
+ * nested union is not mistaken for a top-level one.
35
+ *
36
+ * @param {string} typeText
37
+ * @returns {string}
38
+ */
39
+ export function normaliseType(typeText: string): string;
40
+ /**
41
+ * Diff one entity against its component.
42
+ *
43
+ * @param {object} input
44
+ * @param {ParsedEntity} input.entity
45
+ * @param {SchemaComponent} input.component
46
+ * @param {Record<string, SchemaComponent>} input.components
47
+ * @param {Record<string, { className: string, fieldsTypeName: string, recordTypeName: string }>} [input.entityByComponent]
48
+ * @param {Record<string, { name: string, typeName: string, file: string }>} [input.enumByComponent]
49
+ * @param {Readonly<Record<string, readonly string[]>>} [input.equivalentFieldClasses]
50
+ * @returns {EntityDiff}
51
+ */
52
+ export function diffEntity({ entity, component, components, entityByComponent, enumByComponent, equivalentFieldClasses, }: {
53
+ entity: ParsedEntity;
54
+ component: SchemaComponent;
55
+ components: Record<string, SchemaComponent>;
56
+ entityByComponent?: Record<string, {
57
+ className: string;
58
+ fieldsTypeName: string;
59
+ recordTypeName: string;
60
+ }> | undefined;
61
+ enumByComponent?: Record<string, {
62
+ name: string;
63
+ typeName: string;
64
+ file: string;
65
+ }> | undefined;
66
+ equivalentFieldClasses?: Readonly<Record<string, readonly string[]>> | undefined;
67
+ }): EntityDiff;
68
+ /**
69
+ * Diff every resolved entity.
70
+ *
71
+ * Entities that are excluded or unresolved are skipped: there is nothing to
72
+ * compare them against, and `resolve.mjs` already reports why.
73
+ *
74
+ * @param {object} input
75
+ * @param {ParsedEntity[]} input.entities
76
+ * @param {import('./resolve.mjs').Resolution[]} input.resolutions
77
+ * @param {ParsedSchema} input.schema
78
+ * @param {Record<string, { className: string, fieldsTypeName: string, recordTypeName: string }>} [input.entityByComponent]
79
+ * @param {Record<string, { name: string, typeName: string, file: string }>} [input.enumByComponent]
80
+ * @param {Readonly<Record<string, readonly string[]>>} [input.equivalentFieldClasses]
81
+ * @returns {EntityDiff[]}
82
+ */
83
+ export function diffAll({ entities, resolutions, schema, entityByComponent, enumByComponent, equivalentFieldClasses, }: {
84
+ entities: ParsedEntity[];
85
+ resolutions: import("./resolve.mjs").Resolution[];
86
+ schema: ParsedSchema;
87
+ entityByComponent?: Record<string, {
88
+ className: string;
89
+ fieldsTypeName: string;
90
+ recordTypeName: string;
91
+ }> | undefined;
92
+ enumByComponent?: Record<string, {
93
+ name: string;
94
+ typeName: string;
95
+ file: string;
96
+ }> | undefined;
97
+ equivalentFieldClasses?: Readonly<Record<string, readonly string[]>> | undefined;
98
+ }): EntityDiff[];
99
+ /**
100
+ * Compare an entity against the schema component it models, and emit the edits
101
+ * needed to bring them into agreement.
102
+ *
103
+ * The output is the whole point of the tool: a short list of ops per entity,
104
+ * which is what the `/entity sync` skill reads instead of the 296KB schema.
105
+ *
106
+ * ## Op kinds
107
+ *
108
+ * | kind | meaning |
109
+ * | ------------- | ---------------------------------------------------------- |
110
+ * | `add` | the API sends a field the entity does not declare |
111
+ * | `remove` | the entity declares a field the API does not send |
112
+ * | `retype` | the field class disagrees with the schema signal |
113
+ * | `opts` | `readOnly` / `blank` / `many` / `entity` disagree |
114
+ * | `type-text` | the `Fields` member type disagrees |
115
+ * | `record-type` | the `Record` type is missing its Immutable substitution |
116
+ * | `internal` | `static fields` and the `Fields` type disagree with each |
117
+ * | | other, independent of the schema |
118
+ * | `review` | something the mapper declined to decide - needs a human |
119
+ *
120
+ * ## Deliberate tolerances
121
+ *
122
+ * Three places accept more than one answer, because insisting on an exact string
123
+ * would produce noise that trains a reader to ignore the report:
124
+ *
125
+ * - **enum types**: the ideal is a literal union, but a plain `string` or a named
126
+ * alias (as in `ApplicationStatusValue`) is equally valid and cannot be
127
+ * verified without the type checker.
128
+ * - **union member order**: `string | null` and `null | string` are the same
129
+ * type, so comparison normalises order.
130
+ * - **TextField for CharField**: the schema types a Django `TextField` and
131
+ * `CharField` alike as a plain `string`, so it cannot say which one the
132
+ * entity should use. `TextField` is a `CharField` that entity-form draws as a
133
+ * textarea - a presentation choice, not drift.
134
+ */
135
+ /**
136
+ * Field classes accepted in place of the one the schema maps to.
137
+ *
138
+ * Projects extend this via `equivalentFieldClasses` in their config.
139
+ *
140
+ * @type {Readonly<Record<string, readonly string[]>>}
141
+ */
142
+ export const DEFAULT_EQUIVALENT_FIELD_CLASSES: Readonly<Record<string, readonly string[]>>;
143
+ /**
144
+ * @typedef {import('./parse-entities.mjs').ParsedEntity} ParsedEntity
145
+ * @typedef {import('./parse-schema.mjs').SchemaComponent} SchemaComponent
146
+ * @typedef {import('./parse-schema.mjs').ParsedSchema} ParsedSchema
147
+ */
148
+ /**
149
+ * @typedef {'add'|'remove'|'retype'|'opts'|'type-text'|'record-type'|'internal'|'enum-missing'} ShapeOpKind
150
+ * a genuine disagreement about the data's shape
151
+ * @typedef {'readonly-hint'|'review'|'enum-untyped'} AdvisoryOpKind
152
+ * worth knowing, but not a shape error - reported separately so it cannot
153
+ * crowd out the ops above
154
+ */
155
+ /**
156
+ * @typedef {object} FieldOp
157
+ * @property {ShapeOpKind | AdvisoryOpKind} kind
158
+ * @property {string} field
159
+ * @property {string} detail
160
+ * @property {string | null} expected
161
+ * @property {string | null} actual
162
+ */
163
+ /** Op kinds that are advisory rather than shape drift. */
164
+ export const ADVISORY_OP_KINDS: Set<string>;
165
+ /**
166
+ * `needed` - the field still disagrees, so the annotation is still doing work.
167
+ * `redundant` - the schema and the entity now agree; the broker has caught up
168
+ * and the annotation can be deleted.
169
+ * `orphaned` - the named field is in neither the component nor the Fields
170
+ * type, so the annotation refers to something that no longer exists.
171
+ */
172
+ export type SuppressionStatus = "needed" | "redundant" | "orphaned";
173
+ /**
174
+ * One `@schemaIgnore`, with the ops it is currently hiding.
175
+ */
176
+ export type Suppression = {
177
+ field: string;
178
+ /**
179
+ * the annotation's own words; '' if none was given
180
+ */
181
+ reason: string;
182
+ status: SuppressionStatus;
183
+ /**
184
+ * the ops this annotation dropped, for review
185
+ */
186
+ ops: FieldOp[];
187
+ };
188
+ export type EntityDiff = {
189
+ className: string;
190
+ file: string;
191
+ component: string;
192
+ ops: FieldOp[];
193
+ /**
194
+ * entity classes this one embeds
195
+ */
196
+ nestedEntities: string[];
197
+ /**
198
+ * every `@schemaIgnore`, and its status
199
+ */
200
+ suppressions: Suppression[];
201
+ /**
202
+ * the `@schemaSubset` reason, if any
203
+ */
204
+ schemaSubset: string | null;
205
+ /**
206
+ * fields the projection declined to compare
207
+ */
208
+ subsetDropped: string[];
209
+ };
210
+ export type ParsedEntity = import("./parse-entities.mjs").ParsedEntity;
211
+ export type SchemaComponent = import("./parse-schema.mjs").SchemaComponent;
212
+ export type ParsedSchema = import("./parse-schema.mjs").ParsedSchema;
213
+ /**
214
+ * a genuine disagreement about the data's shape
215
+ */
216
+ export type ShapeOpKind = "add" | "remove" | "retype" | "opts" | "type-text" | "record-type" | "internal" | "enum-missing";
217
+ /**
218
+ * worth knowing, but not a shape error - reported separately so it cannot
219
+ * crowd out the ops above
220
+ */
221
+ export type AdvisoryOpKind = "readonly-hint" | "review" | "enum-untyped";
222
+ export type FieldOp = {
223
+ kind: ShapeOpKind | AdvisoryOpKind;
224
+ field: string;
225
+ detail: string;
226
+ expected: string | null;
227
+ actual: string | null;
228
+ };
@@ -0,0 +1,93 @@
1
+ /**
2
+ * Decide the field class and options a schema field should map to.
3
+ *
4
+ * @param {SchemaField} field
5
+ * @param {object} context
6
+ * @param {Record<string, SchemaComponent>} context.components
7
+ * used to tell an enum ref from an object ref
8
+ * @param {Record<string, { className: string, fieldsTypeName: string, recordTypeName: string }>} [context.entityByComponent]
9
+ * component name -> the entity that models it, when one does
10
+ * @param {Record<string, { name: string, typeName: string, file: string }>} [context.enumByComponent]
11
+ * component name -> the choices dict that models it, when one does
12
+ * @returns {ExpectedField}
13
+ */
14
+ export function expectedField(field: SchemaField, { components, entityByComponent, enumByComponent }: {
15
+ components: Record<string, SchemaComponent>;
16
+ entityByComponent?: Record<string, {
17
+ className: string;
18
+ fieldsTypeName: string;
19
+ recordTypeName: string;
20
+ }> | undefined;
21
+ enumByComponent?: Record<string, {
22
+ name: string;
23
+ typeName: string;
24
+ file: string;
25
+ }> | undefined;
26
+ }): ExpectedField;
27
+ /**
28
+ * Build `component -> entity` from resolutions, for `entityByComponent`.
29
+ *
30
+ * When two entities model the same component - as `Application` is modelled by
31
+ * both `ApplicationRowEntity` and (via the detail serializer) others - the first
32
+ * wins, and the ambiguity is already reported by `resolve.mjs`.
33
+ *
34
+ * @param {import('./resolve.mjs').Resolution[]} resolutions
35
+ * @param {import('./parse-entities.mjs').ParsedEntity[]} entities
36
+ */
37
+ export function buildEntityByComponent(resolutions: import("./resolve.mjs").Resolution[], entities: import("./parse-entities.mjs").ParsedEntity[]): Record<string, {
38
+ className: string;
39
+ fieldsTypeName: string;
40
+ recordTypeName: string;
41
+ }>;
42
+ /**
43
+ * Components drf-spectacular emits to widen a field, not to enumerate it.
44
+ *
45
+ * A blank-able choices field is rendered as `StateEnum | BlankEnum`, where
46
+ * `BlankEnum` is the single value `''`. They are not members of the set the
47
+ * broker defines - `''` means "unset" - so a choices dict must not absorb them,
48
+ * or every dict would carry an empty-string member and `Status.` would offer it
49
+ * for selection. They stay as literals in the field's type instead.
50
+ */
51
+ export const WIDENING_ENUM_COMPONENTS: Set<string>;
52
+ export type SchemaField = import("./parse-schema.mjs").SchemaField;
53
+ export type SchemaComponent = import("./parse-schema.mjs").SchemaComponent;
54
+ export type ExpectedField = {
55
+ /**
56
+ * e.g. 'CharField'
57
+ */
58
+ fieldClass: string;
59
+ /**
60
+ * expected constructor options
61
+ */
62
+ opts: Record<string, true | string>;
63
+ /**
64
+ * expected `Fields` member type
65
+ */
66
+ typeText: string;
67
+ /**
68
+ * expected `Record` member type, when
69
+ * it differs from `typeText`: any `many: true` field (a `List`), a nested
70
+ * object, or a date (a `Moment`)
71
+ */
72
+ recordTypeText: string | null;
73
+ /**
74
+ * expected `entity:` option value
75
+ */
76
+ entityRef: string | null;
77
+ /**
78
+ * when the field is an enum union
79
+ */
80
+ enumValues: string[];
81
+ /**
82
+ * the `*Enum` components the field refs
83
+ */
84
+ enumRefs?: string[] | undefined;
85
+ /**
86
+ * type aliases of the dicts modelling them
87
+ */
88
+ enumAliases?: string[] | undefined;
89
+ /**
90
+ * anything the diff should pass to a human
91
+ */
92
+ note: string | null;
93
+ };
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Derive the module specifier to import an entity's types from, relative to the
3
+ * directory the generated file is written to.
4
+ *
5
+ * @param {string} file repo-relative, e.g. 'src/entities/filter/Foo.ts'
6
+ * @param {string} [assertsDir] repo-relative directory of the generated file
7
+ * @returns {string} e.g. '../filter/Foo'
8
+ */
9
+ export function moduleSpecifier(file: string, assertsDir?: string): string;
10
+ /**
11
+ * Generate the assertions file.
12
+ *
13
+ * @param {object} input
14
+ * @param {ParsedEntity[]} input.entities
15
+ * @param {import('./resolve.mjs').Resolution[]} input.resolutions
16
+ * @param {ParsedSchema} input.schema
17
+ * @param {Record<string, { className: string, fieldsTypeName: string, recordTypeName: string }>} input.entityByComponent
18
+ * @param {import('./resolve.mjs').EnumResolution[]} [input.enumResolutions]
19
+ * @param {Record<string, { name: string, typeName: string, file: string }>} [input.enumByComponent]
20
+ * @param {string} [input.assertsDir] repo-relative directory the file is written to
21
+ * @param {string} [input.schemaSpecifier] module the schema `components` type is imported from
22
+ * @returns {GeneratedAsserts}
23
+ */
24
+ export function generateAsserts({ entities, resolutions, schema, entityByComponent, enumResolutions, enumByComponent, assertsDir, schemaSpecifier, }: {
25
+ entities: ParsedEntity[];
26
+ resolutions: import("./resolve.mjs").Resolution[];
27
+ schema: ParsedSchema;
28
+ entityByComponent: Record<string, {
29
+ className: string;
30
+ fieldsTypeName: string;
31
+ recordTypeName: string;
32
+ }>;
33
+ enumResolutions?: import("./resolve.mjs").EnumResolution[] | undefined;
34
+ enumByComponent?: Record<string, {
35
+ name: string;
36
+ typeName: string;
37
+ file: string;
38
+ }> | undefined;
39
+ assertsDir?: string | undefined;
40
+ schemaSpecifier?: string | undefined;
41
+ }): GeneratedAsserts;
42
+ /**
43
+ * @typedef {import('./parse-entities.mjs').ParsedEntity} ParsedEntity
44
+ * @typedef {import('./parse-schema.mjs').ParsedSchema} ParsedSchema
45
+ */
46
+ /**
47
+ * @typedef {object} GeneratedAsserts
48
+ * @property {string} source the file contents
49
+ * @property {{ className: string, reason: string }[]} skipped
50
+ * @property {number} entityCount
51
+ * @property {number} assertionCount
52
+ */
53
+ /** Where the generated file lives by convention, repo-relative. */
54
+ export const DEFAULT_ASSERTS_DIR: "src/entities/__generated__";
55
+ /** How the generated file imports the schema types by convention. */
56
+ export const DEFAULT_SCHEMA_SPECIFIER: "@/api/schema";
57
+ export type ParsedEntity = import("./parse-entities.mjs").ParsedEntity;
58
+ export type ParsedSchema = import("./parse-schema.mjs").ParsedSchema;
59
+ export type GeneratedAsserts = {
60
+ /**
61
+ * the file contents
62
+ */
63
+ source: string;
64
+ skipped: {
65
+ className: string;
66
+ reason: string;
67
+ }[];
68
+ entityCount: number;
69
+ assertionCount: number;
70
+ };
@@ -0,0 +1,90 @@
1
+ /**
2
+ * Dependency-graph algorithms for ordering entity edits.
3
+ *
4
+ * The `$ref` graph between schema components mirrors the `EntityField` graph
5
+ * between entities, and a parent depends on its children twice over: its
6
+ * `EntityField({ entity: Child })` names the child class, and its `Fields` type
7
+ * names `ChildEntityFields`. So children must be settled before parents, or
8
+ * editing a parent has to be redone once the child changes.
9
+ *
10
+ * Three standard build-system algorithms cover it. They are kept here as pure
11
+ * functions over plain `Map`s so they can be tested without any schema at all.
12
+ *
13
+ * - `toWaves` - Kahn's algorithm, grouped into levels
14
+ * - `findCycles` - Tarjan's strongly-connected components
15
+ * - `propagateDirty` - reverse-edge worklist to a fixpoint
16
+ *
17
+ * ## Edge direction
18
+ *
19
+ * Throughout, an edge points from a node to what it *depends on*:
20
+ * `Parent -> Child`. `toWaves` therefore emits leaves first.
21
+ */
22
+ /**
23
+ * @typedef {Map<string, Set<string>>} Graph
24
+ * node -> the set of nodes it depends on
25
+ */
26
+ /**
27
+ * Build a graph from an adjacency description, adding any node that appears only
28
+ * as a dependency.
29
+ *
30
+ * Normalising here means the algorithms below never have to guard against a
31
+ * dangling edge, which is the usual source of a silently-dropped node.
32
+ *
33
+ * @param {Record<string, string[]> | Iterable<[string, Iterable<string>]>} edges
34
+ * @returns {Graph}
35
+ */
36
+ export function buildGraph(edges: Record<string, string[]> | Iterable<[string, Iterable<string>]>): Graph;
37
+ /**
38
+ * Reverse a graph: `node -> dependents`.
39
+ *
40
+ * @param {Graph} graph
41
+ * @returns {Graph}
42
+ */
43
+ export function reverseGraph(graph: Graph): Graph;
44
+ /**
45
+ * Group nodes into dependency waves: everything in wave N depends only on nodes
46
+ * in earlier waves.
47
+ *
48
+ * Kahn's algorithm, but emitting whole levels rather than a flat order, because
49
+ * the useful property for editing is that nodes *within* a wave are mutually
50
+ * independent - so editing one cannot invalidate another.
51
+ *
52
+ * Nodes caught in a cycle can never reach in-degree zero, so they are returned
53
+ * separately rather than silently dropped. Use `findCycles` to see what they are.
54
+ *
55
+ * @param {Graph} graph
56
+ * @returns {{ waves: string[][], unordered: string[] }}
57
+ */
58
+ export function toWaves(graph: Graph): {
59
+ waves: string[][];
60
+ unordered: string[];
61
+ };
62
+ /**
63
+ * Find strongly-connected components of size > 1, plus self-loops.
64
+ *
65
+ * Tarjan's algorithm, iterative rather than recursive so a deep chain cannot
66
+ * blow the stack. Mutually recursive DRF serializers are entirely normal, and a
67
+ * cycle must be reported as one unit to edit atomically rather than deadlocking
68
+ * the topological sort.
69
+ *
70
+ * @param {Graph} graph
71
+ * @returns {string[][]} each cycle, members sorted; cycles sorted by first member
72
+ */
73
+ export function findCycles(graph: Graph): string[][];
74
+ /**
75
+ * Expand a set of directly-changed nodes to everything transitively affected.
76
+ *
77
+ * When a child's shape changes its parents are affected too, because a parent's
78
+ * nested field type names the child. A worklist walked over reverse edges to a
79
+ * fixpoint is the cheapest correct way to get that, and terminates because each
80
+ * node enters the result at most once.
81
+ *
82
+ * @param {Graph} graph node -> dependencies
83
+ * @param {Iterable<string>} seeds directly-changed nodes
84
+ * @returns {Set<string>} seeds plus every transitive dependent
85
+ */
86
+ export function propagateDirty(graph: Graph, seeds: Iterable<string>): Set<string>;
87
+ /**
88
+ * node -> the set of nodes it depends on
89
+ */
90
+ export type Graph = Map<string, Set<string>>;