@prisma-next/migration-tools 0.5.0-dev.66 → 0.5.0-dev.68

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 (98) hide show
  1. package/dist/{constants-B87kJAGj.mjs → constants-DWV9_o2Z.mjs} +2 -2
  2. package/dist/{constants-B87kJAGj.mjs.map → constants-DWV9_o2Z.mjs.map} +1 -1
  3. package/dist/{errors-DQsXvidG.mjs → errors-EPL_9p9f.mjs} +13 -8
  4. package/dist/errors-EPL_9p9f.mjs.map +1 -0
  5. package/dist/exports/aggregate.d.mts +534 -0
  6. package/dist/exports/aggregate.d.mts.map +1 -0
  7. package/dist/exports/aggregate.mjs +598 -0
  8. package/dist/exports/aggregate.mjs.map +1 -0
  9. package/dist/exports/constants.d.mts.map +1 -1
  10. package/dist/exports/constants.mjs +2 -3
  11. package/dist/exports/errors.d.mts +6 -1
  12. package/dist/exports/errors.d.mts.map +1 -1
  13. package/dist/exports/errors.mjs +2 -3
  14. package/dist/exports/graph.d.mts +1 -1
  15. package/dist/exports/graph.mjs +1 -1
  16. package/dist/exports/hash.d.mts +2 -2
  17. package/dist/exports/hash.d.mts.map +1 -1
  18. package/dist/exports/hash.mjs +2 -3
  19. package/dist/exports/invariants.d.mts +14 -4
  20. package/dist/exports/invariants.d.mts.map +1 -1
  21. package/dist/exports/invariants.mjs +2 -4
  22. package/dist/exports/io.d.mts +26 -2
  23. package/dist/exports/io.d.mts.map +1 -1
  24. package/dist/exports/io.mjs +2 -5
  25. package/dist/exports/metadata.d.mts +1 -1
  26. package/dist/exports/metadata.mjs +1 -1
  27. package/dist/exports/migration-graph.d.mts +2 -2
  28. package/dist/exports/migration-graph.d.mts.map +1 -1
  29. package/dist/exports/migration-graph.mjs +1 -525
  30. package/dist/exports/migration-ts.d.mts.map +1 -1
  31. package/dist/exports/migration-ts.mjs +1 -3
  32. package/dist/exports/migration-ts.mjs.map +1 -1
  33. package/dist/exports/migration.d.mts +1 -1
  34. package/dist/exports/migration.d.mts.map +1 -1
  35. package/dist/exports/migration.mjs +5 -6
  36. package/dist/exports/migration.mjs.map +1 -1
  37. package/dist/exports/package.d.mts +1 -1
  38. package/dist/exports/package.mjs +1 -1
  39. package/dist/exports/refs.d.mts.map +1 -1
  40. package/dist/exports/refs.mjs +2 -3
  41. package/dist/exports/refs.mjs.map +1 -1
  42. package/dist/exports/spaces.d.mts +341 -238
  43. package/dist/exports/spaces.d.mts.map +1 -1
  44. package/dist/exports/spaces.mjs +138 -348
  45. package/dist/exports/spaces.mjs.map +1 -1
  46. package/dist/{graph-Czaj8O2q.d.mts → graph-HMWAldoR.d.mts} +1 -1
  47. package/dist/graph-HMWAldoR.d.mts.map +1 -0
  48. package/dist/{hash-G0bAfIGh.mjs → hash-By50zM_E.mjs} +2 -4
  49. package/dist/hash-By50zM_E.mjs.map +1 -0
  50. package/dist/{invariants-4Avb_Yhy.mjs → invariants-Duc8f9NM.mjs} +17 -7
  51. package/dist/invariants-Duc8f9NM.mjs.map +1 -0
  52. package/dist/{io-CDJaWGbt.mjs → io-D13dLvUh.mjs} +46 -14
  53. package/dist/io-D13dLvUh.mjs.map +1 -0
  54. package/dist/migration-graph-DGNnKDY5.mjs +523 -0
  55. package/dist/migration-graph-DGNnKDY5.mjs.map +1 -0
  56. package/dist/{op-schema-BiF1ZYqH.mjs → op-schema-D5qkXfEf.mjs} +2 -3
  57. package/dist/{op-schema-BiF1ZYqH.mjs.map → op-schema-D5qkXfEf.mjs.map} +1 -1
  58. package/dist/{package-B3Yl6DTr.d.mts → package-BjiZ7KDy.d.mts} +1 -1
  59. package/dist/package-BjiZ7KDy.d.mts.map +1 -0
  60. package/dist/read-contract-space-contract-C3-1eyaI.mjs +298 -0
  61. package/dist/read-contract-space-contract-C3-1eyaI.mjs.map +1 -0
  62. package/package.json +13 -9
  63. package/src/aggregate/loader.ts +409 -0
  64. package/src/aggregate/marker-types.ts +16 -0
  65. package/src/aggregate/planner-types.ts +137 -0
  66. package/src/aggregate/planner.ts +158 -0
  67. package/src/aggregate/project-schema-to-space.ts +64 -0
  68. package/src/aggregate/strategies/graph-walk.ts +92 -0
  69. package/src/aggregate/strategies/synth.ts +122 -0
  70. package/src/aggregate/types.ts +89 -0
  71. package/src/aggregate/verifier.ts +230 -0
  72. package/src/assert-descriptor-self-consistency.ts +70 -0
  73. package/src/compute-extension-space-apply-path.ts +152 -0
  74. package/src/concatenate-space-apply-inputs.ts +2 -2
  75. package/src/detect-space-contract-drift.ts +22 -26
  76. package/src/{emit-pinned-space-artefacts.ts → emit-contract-space-artefacts.ts} +14 -33
  77. package/src/errors.ts +11 -5
  78. package/src/exports/aggregate.ts +37 -0
  79. package/src/exports/errors.ts +1 -0
  80. package/src/exports/io.ts +1 -0
  81. package/src/exports/spaces.ts +23 -10
  82. package/src/gather-disk-contract-space-state.ts +62 -0
  83. package/src/invariants.ts +14 -3
  84. package/src/io.ts +42 -0
  85. package/src/plan-all-spaces.ts +3 -7
  86. package/src/read-contract-space-contract.ts +44 -0
  87. package/src/read-contract-space-head-ref.ts +63 -0
  88. package/src/space-layout.ts +4 -11
  89. package/src/verify-contract-spaces.ts +45 -49
  90. package/dist/errors-DQsXvidG.mjs.map +0 -1
  91. package/dist/exports/migration-graph.mjs.map +0 -1
  92. package/dist/graph-Czaj8O2q.d.mts.map +0 -1
  93. package/dist/hash-G0bAfIGh.mjs.map +0 -1
  94. package/dist/invariants-4Avb_Yhy.mjs.map +0 -1
  95. package/dist/io-CDJaWGbt.mjs.map +0 -1
  96. package/dist/package-B3Yl6DTr.d.mts.map +0 -1
  97. package/src/read-pinned-contract-hash.ts +0 -77
  98. /package/dist/{metadata-CSjwljJx.d.mts → metadata-BnLFiI6B.d.mts} +0 -0
@@ -1,7 +1,148 @@
1
- import { APP_SPACE_ID } from "@prisma-next/framework-components/control";
1
+ import { t as MigrationOps } from "../package-BjiZ7KDy.mjs";
2
+ import { APP_SPACE_ID, ContractSpaceHeadRef } from "@prisma-next/framework-components/control";
2
3
 
4
+ //#region src/assert-descriptor-self-consistency.d.ts
5
+ /**
6
+ * Inputs the helper needs to recompute the descriptor's storage hash and
7
+ * compare it to the published `headRef.hash`. Kept structural so the SQL
8
+ * family (and any future target family) can compose the check without
9
+ * coupling to its own descriptor types.
10
+ */
11
+ interface DescriptorSelfConsistencyInputs {
12
+ readonly extensionId: string;
13
+ readonly target: string;
14
+ readonly targetFamily: string;
15
+ /**
16
+ * Family-specific storage object. Typed as `unknown` so callers can
17
+ * pass their own narrow storage shape (e.g. `SqlStorage`) without an
18
+ * inline cast — the helper canonicalises through `JSON.stringify`
19
+ * inside {@link computeStorageHash} and only requires a plain
20
+ * record-shaped value at runtime.
21
+ */
22
+ readonly storage: unknown;
23
+ readonly headRefHash: string;
24
+ }
25
+ /**
26
+ * Assert that an extension descriptor is self-consistent: the
27
+ * `headRef.hash` it publishes must match the canonical hash recomputed
28
+ * from its `contractSpace.contractJson`.
29
+ *
30
+ * Recomputes via {@link computeStorageHash} — the same canonical-JSON
31
+ * pipeline the descriptor's own emit pipeline produced the hash with —
32
+ * over `(target, targetFamily, storage)`. Mismatch indicates the
33
+ * extension author bumped `contractJson` without rerunning emit, leaving
34
+ * the descriptor's `headRef.hash` stale; the consumer-side helpers
35
+ * (drift detection, on-disk artefact emission, runner marker writes) all
36
+ * trust `headRef.hash` as the canonical identity, so a stale value would
37
+ * silently corrupt every downstream boundary.
38
+ *
39
+ * Synchronous, pure, no I/O. Throws
40
+ * `MIGRATION.DESCRIPTOR_HEAD_HASH_MISMATCH` on failure with both the
41
+ * recomputed and published hashes in `details` so callers can surface a
42
+ * clear remediation hint without re-deriving them.
43
+ */
44
+ declare function assertDescriptorSelfConsistency(inputs: DescriptorSelfConsistencyInputs): void;
45
+ //#endregion
46
+ //#region src/read-contract-space-head-ref.d.ts
47
+ /**
48
+ * Read the head ref (`hash` + `invariants`) for a contract space from
49
+ * `<projectMigrationsDir>/<spaceId>/refs/head.json`.
50
+ *
51
+ * Returns `null` when the file does not exist (first emit). Surfaces
52
+ * `MIGRATION.INVALID_JSON` / `MIGRATION.INVALID_REF_FILE` on a corrupt
53
+ * `refs/head.json` so callers can distinguish "no head ref on disk"
54
+ * (returns `null`) from "head ref present but unreadable" (throws).
55
+ *
56
+ * Validates the space id against `[a-z][a-z0-9_-]{0,63}` for the same
57
+ * filesystem-safety reasons as the rest of the per-space helpers. The
58
+ * helper is uniform across the app and extension spaces.
59
+ */
60
+ declare function readContractSpaceHeadRef(projectMigrationsDir: string, spaceId: string): Promise<ContractSpaceHeadRef | null>;
61
+ //#endregion
62
+ //#region src/compute-extension-space-apply-path.d.ts
63
+ /**
64
+ * Outcome of {@link computeExtensionSpaceApplyPath} — a discriminated union
65
+ * mirroring {@link import('./migration-graph').FindPathOutcome} so callers
66
+ * can map structural / invariant failures to their preferred CLI envelope
67
+ * without re-running pathfinding.
68
+ */
69
+ type ExtensionSpaceApplyPathOutcome = {
70
+ readonly kind: 'ok';
71
+ readonly contractSpaceHeadRef: ContractSpaceHeadRef;
72
+ /**
73
+ * Sorted, deduplicated invariant ids covered by the walked path.
74
+ * Mirrors the on-disk `providedInvariants` summed across edges and
75
+ * canonicalised — what the runner stamps on the marker after apply.
76
+ */
77
+ readonly providedInvariants: readonly string[];
78
+ /**
79
+ * Path operations in apply order. Empty when the marker is already
80
+ * at the recorded head (no-op).
81
+ */
82
+ readonly pathOps: MigrationOps;
83
+ /**
84
+ * Migration directory names walked, in order. Mirrors `pathOps`'s
85
+ * structure but at the package granularity — useful for surfacing
86
+ * "applied N migration(s)" messages.
87
+ */
88
+ readonly walkedMigrationDirs: readonly string[];
89
+ } | {
90
+ readonly kind: 'unreachable';
91
+ readonly contractSpaceHeadRef: ContractSpaceHeadRef;
92
+ } | {
93
+ readonly kind: 'unsatisfiable';
94
+ readonly contractSpaceHeadRef: ContractSpaceHeadRef;
95
+ readonly missing: readonly string[];
96
+ readonly structuralPath: readonly {
97
+ readonly dirName: string;
98
+ readonly to: string;
99
+ }[];
100
+ } | {
101
+ readonly kind: 'contractSpaceHeadRefMissing';
102
+ };
103
+ /**
104
+ * Inputs to {@link computeExtensionSpaceApplyPath}. The helper is
105
+ * deliberately framework-neutral and consumes only on-disk state:
106
+ *
107
+ * - `projectMigrationsDir` is the project's top-level `migrations/` dir.
108
+ * - `spaceId` selects the per-space subdirectory under it.
109
+ * - `currentMarkerHash` / `currentMarkerInvariants` come from the live
110
+ * marker row keyed by `space = <spaceId>`. `null` hash = no marker yet
111
+ * (the pathfinder treats this as the empty-contract sentinel per ADR
112
+ * 208).
113
+ */
114
+ interface ComputeExtensionSpaceApplyPathInputs {
115
+ readonly projectMigrationsDir: string;
116
+ readonly spaceId: string;
117
+ readonly currentMarkerHash: string | null;
118
+ readonly currentMarkerInvariants: readonly string[];
119
+ }
120
+ /**
121
+ * Compute the apply path for an extension contract space — the shortest
122
+ * sequence of on-disk migration packages that walks the live marker
123
+ * forward to the on-disk head ref hash, covering every required
124
+ * invariant.
125
+ *
126
+ * Reads only on-disk artefacts (`migrations/<spaceId>/refs/head.json`
127
+ * and the per-space migration packages). **Does not import any
128
+ * extension descriptor module** — `db init` / `db update` must remain
129
+ * runnable without the descriptor source on disk.
130
+ *
131
+ * Behaviour:
132
+ * - Returns `{ kind: 'ok', pathOps: [], … }` when the marker is already
133
+ * at the recorded head and no required invariants are missing.
134
+ * - Returns `{ kind: 'unreachable' }` when the marker hash is not
135
+ * structurally connected to the recorded head in the graph.
136
+ * - Returns `{ kind: 'unsatisfiable', missing, … }` when the marker is
137
+ * reachable but no path covers the required invariants.
138
+ * - Returns `{ kind: 'contractSpaceHeadRefMissing' }` when the per-space
139
+ * `refs/head.json` is absent — the precheck verifier should already
140
+ * have rejected this case, but the helper is defensive so callers can
141
+ * surface a coherent error rather than throw.
142
+ */
143
+ declare function computeExtensionSpaceApplyPath(inputs: ComputeExtensionSpaceApplyPathInputs): Promise<ExtensionSpaceApplyPathOutcome>;
144
+ //#endregion
3
145
  //#region src/concatenate-space-apply-inputs.d.ts
4
-
5
146
  /**
6
147
  * Per-space input the runner consumes when applying a migration.
7
148
  *
@@ -11,8 +152,8 @@ import { APP_SPACE_ID } from "@prisma-next/framework-components/control";
11
152
  * and the helper preserves it through the concatenation.
12
153
  *
13
154
  * - `migrationDirectory` is the on-disk migration directory for the
14
- * space — `<projectRoot>/migrations` for `'app'` and
15
- * `<projectRoot>/migrations/<space-id>` for an extension space.
155
+ * space — `<projectRoot>/migrations/<space-id>` (uniform; the app
156
+ * subspaces under its own `<APP_SPACE_ID>/` directory).
16
157
  * - `currentMarkerHash` and `currentMarkerInvariants` are the values
17
158
  * read from the `prisma_contract.marker` row keyed by `space = <space-id>`
18
159
  * (T1.1). `null` hash = no marker row yet.
@@ -29,37 +170,6 @@ interface SpaceApplyInput<TOp> {
29
170
  readonly currentMarkerInvariants: readonly string[];
30
171
  readonly path: readonly TOp[];
31
172
  }
32
- /**
33
- * Order a set of per-space apply inputs into the canonical cross-space
34
- * sequence the runner applies under a single transaction.
35
- *
36
- * Cross-space ordering convention (sub-spec § 4):
37
- *
38
- * 1. **Extension spaces first**, alphabetically by `spaceId`.
39
- * 2. **App space last** — only one `'app'` entry expected, at most.
40
- *
41
- * Rationale: extensions install their own structural objects (types,
42
- * functions, helper tables) before the app's structural ops reference
43
- * them. Putting app-space last lets app-space ops freely depend on any
44
- * extension-space declaration in the same transaction.
45
- *
46
- * Determinism (NFR6): the output order is independent of the input
47
- * order, so two callers with the same set of `extensionPacks` produce
48
- * identical apply sequences.
49
- *
50
- * Atomicity: rejects duplicate `spaceId`s with
51
- * `MIGRATION.DUPLICATE_SPACE_ID` before producing any output. This
52
- * mirrors {@link import('./plan-all-spaces').planAllSpaces} so the
53
- * planner-side and runner-side helpers reject malformed inputs the same
54
- * way (callers don't need a separate dedup pass).
55
- *
56
- * Synchronous, pure, no I/O: callers resolve marker rows and `path`
57
- * before invoking this helper. The actual DB application — driving the
58
- * transaction, committing marker writes, recording the per-space marker
59
- * rows — happens at the SQL-family consumption site (per the
60
- * helper-location convention from R3).
61
- */
62
- declare function concatenateSpaceApplyInputs<TOp>(inputs: readonly SpaceApplyInput<TOp>[]): readonly SpaceApplyInput<TOp>[];
63
173
  //#endregion
64
174
  //#region src/detect-space-contract-drift.d.ts
65
175
  /**
@@ -72,42 +182,40 @@ declare function concatenateSpaceApplyInputs<TOp>(inputs: readonly SpaceApplyInp
72
182
  * family speaks its own contract type, and both reduce to a hash string
73
183
  * before drift detection runs.
74
184
  *
75
- * `pinnedHash` is `null` when no pinned `contract.json` exists yet for
185
+ * `priorHeadHash` is `null` when no `contract.json` exists yet on disk for
76
186
  * the space (the descriptor declares an extension that has never been
77
187
  * emitted into the user's repo). That's the "first emit" case — no
78
- * drift to surface; the migrate emit will create the pinned files.
79
- *
80
- * @see specs/framework-mechanism.spec.md § 3 — Drift detection (T1.9).
188
+ * drift to surface; the migrate emit will create the on-disk artefacts.
81
189
  */
82
190
  interface DetectSpaceContractDriftInputs {
83
191
  readonly descriptorHash: string;
84
- readonly pinnedHash: string | null;
192
+ readonly priorHeadHash: string | null;
85
193
  }
86
194
  /**
87
195
  * Result discriminant for {@link detectSpaceContractDrift}.
88
196
  *
89
- * - `noDrift`: descriptor hash and pinned hash agree byte-for-byte.
197
+ * - `noDrift`: descriptor hash and on-disk head hash agree byte-for-byte.
90
198
  * The migrate emit can proceed with no warning.
91
- * - `firstEmit`: no pinned `contract.json` on disk yet. The extension
199
+ * - `firstEmit`: no on-disk `contract.json` on disk yet. The extension
92
200
  * was just added to `extensionPacks`; this run will create the
93
- * pinned files. No warning either — the user's intent is to install
94
- * the extension, not to "drift" from a state they haven't pinned.
95
- * - `drift`: descriptor hash differs from pinned hash. The caller
201
+ * on-disk artefacts. No warning either — the user's intent is to install
202
+ * the extension, not to "drift" from a state they haven't recorded.
203
+ * - `drift`: descriptor hash differs from on-disk head hash. The caller
96
204
  * surfaces a non-fatal warning naming the extension and the
97
- * diff direction (descriptor → pinned). The migrate emit proceeds
205
+ * diff direction (descriptor → on-disk head). The migrate emit proceeds
98
206
  * normally so the bump is materialised this run; the warning just
99
207
  * confirms the bump is being captured.
100
208
  *
101
- * `spaceId`, `descriptorHash`, and `pinnedHash` are threaded through
209
+ * `spaceId`, `descriptorHash`, and `priorHeadHash` are threaded through
102
210
  * verbatim so the caller (logger / TerminalUI / strict-mode envelope)
103
211
  * has everything it needs to format the warning message without
104
- * re-reading the descriptor or the pinned file.
212
+ * re-reading the descriptor or the on-disk artefact.
105
213
  */
106
214
  type SpaceContractDriftResult = {
107
215
  readonly kind: 'noDrift' | 'firstEmit' | 'drift';
108
216
  readonly spaceId: string;
109
217
  readonly descriptorHash: string;
110
- readonly pinnedHash: string | null;
218
+ readonly priorHeadHash: string | null;
111
219
  };
112
220
  /**
113
221
  * Pure drift-detection primitive for a single contract space.
@@ -118,30 +226,19 @@ type SpaceContractDriftResult = {
118
226
  * already canonical hashes produced by the same pipeline, so any
119
227
  * difference is meaningful drift.
120
228
  *
121
- * Synchronous, pure, no I/O. The caller (SQL family in M2 R1) reads
122
- * the pinned `contract.json` and computes its hash, then invokes this
123
- * helper alongside the descriptor's `headRef.hash`. Composes naturally
124
- * with {@link import('./read-pinned-contract-hash').readPinnedContractHash}
229
+ * Synchronous, pure, no I/O. The caller (SQL family) reads the on-disk
230
+ * `contract.json` and computes its hash, then invokes this helper
231
+ * alongside the descriptor's `headRef.hash`. Composes naturally with
232
+ * {@link import('./read-contract-space-head-ref').readContractSpaceHeadRef}
125
233
  * which provides the read-side primitive.
126
234
  *
127
- * @see specs/framework-mechanism.spec.md § 3 Drift detection (T1.9).
128
- * @see specs/framework-mechanism.spec.md AM7 — drift warning surfaces
129
- * the extension name and the diff direction.
235
+ * The drift warning surfaces the extension name and the diff direction.
130
236
  */
131
237
  declare function detectSpaceContractDrift(spaceId: string, inputs: DetectSpaceContractDriftInputs): SpaceContractDriftResult;
132
238
  //#endregion
133
- //#region src/emit-pinned-space-artefacts.d.ts
239
+ //#region src/emit-contract-space-artefacts.d.ts
134
240
  /**
135
- * Pinned head reference for a contract space — `(hash, invariants)`.
136
- * Mirrors {@link import('./refs').RefEntry} but is redeclared locally so
137
- * callers can construct the input without depending on the refs module.
138
- */
139
- interface PinnedSpaceHeadRef {
140
- readonly hash: string;
141
- readonly invariants: readonly string[];
142
- }
143
- /**
144
- * Inputs for {@link emitPinnedSpaceArtefacts}.
241
+ * Inputs for {@link emitContractSpaceArtefacts}.
145
242
  *
146
243
  * - `contract` is the canonical contract value the framework just emitted
147
244
  * for the space; it is serialised through {@link canonicalizeJson}, so
@@ -155,18 +252,18 @@ interface PinnedSpaceHeadRef {
155
252
  * needs), so this helper accepts the text verbatim and writes it out
156
253
  * without further transformation.
157
254
  *
158
- * - `headRef` is the pinned head reference for the space.
255
+ * - `headRef` is the head reference for the space.
159
256
  * `invariants` are sorted alphabetically before serialisation so two
160
257
  * callers passing the same set in different orders produce
161
258
  * byte-identical `refs/head.json`.
162
259
  */
163
- interface PinnedSpaceArtefactInputs {
260
+ interface ContractSpaceArtefactInputs {
164
261
  readonly contract: unknown;
165
262
  readonly contractDts: string;
166
- readonly headRef: PinnedSpaceHeadRef;
263
+ readonly headRef: ContractSpaceHeadRef;
167
264
  }
168
265
  /**
169
- * Emit the pinned per-space artefacts (`contract.json`, `contract.d.ts`,
266
+ * Emit the per-space artefacts (`contract.json`, `contract.d.ts`,
170
267
  * `refs/head.json`) under `<projectMigrationsDir>/<spaceId>/`.
171
268
  *
172
269
  * Always-overwrite: the framework owns these files; running `migrate`
@@ -174,144 +271,20 @@ interface PinnedSpaceArtefactInputs {
174
271
  * helper does not check pre-existing contents — re-emit always wins.
175
272
  *
176
273
  * Path layout matches the convention in
177
- * [`spaceMigrationDirectory`](./space-layout.ts), with two restrictions
178
- * specific to pinned artefacts:
179
- *
180
- * - Rejects the app space (`spaceId === APP_SPACE_ID`): the app space's
181
- * canonical `contract.json` lives at the project root, not under
182
- * `migrations/`. Callers that want to emit it use the app-space
183
- * contract emit pipeline.
184
- * - Validates `spaceId` against `[a-z][a-z0-9_-]{0,63}` via
185
- * {@link assertValidSpaceId} for the same filesystem-safety reasons.
274
+ * [`spaceMigrationDirectory`](./space-layout.ts). The space id is
275
+ * validated against `[a-z][a-z0-9_-]{0,63}` via
276
+ * {@link assertValidSpaceId} for filesystem-safety reasons; the helper
277
+ * accepts every space uniformly (including the app space, default
278
+ * `'app'`).
186
279
  *
187
280
  * The migrations directory and space subdirectory are created if they
188
281
  * do not yet exist (`mkdir { recursive: true }`).
189
- *
190
- * @see specs/framework-mechanism.spec.md § 3 — Pinned artefact emission (T1.8).
191
- */
192
- declare function emitPinnedSpaceArtefacts(projectMigrationsDir: string, spaceId: string, inputs: PinnedSpaceArtefactInputs): Promise<void>;
193
- //#endregion
194
- //#region src/plan-all-spaces.d.ts
195
- /**
196
- * Per-space input for {@link planAllSpaces}. One entry per loaded
197
- * contract space (the application's `'app'` plus each extension that
198
- * exposes a `contractSpace`).
199
- *
200
- * - `priorContract` is `null` for a space that has never been emitted
201
- * (no `migrations/<space-id>/contract.json` on disk yet); otherwise it
202
- * is the canonical contract value pinned for that space.
203
- * - `newContract` is the canonical contract value the planner is about
204
- * to emit for that space — for app-space, the just-emitted root
205
- * `contract.json`; for an extension space, the descriptor's
206
- * `contractSpace.contractJson`.
207
- *
208
- * @see specs/framework-mechanism.spec.md § 3.
209
- */
210
- interface SpacePlanInput<TContract> {
211
- readonly spaceId: string;
212
- readonly priorContract: TContract | null;
213
- readonly newContract: TContract;
214
- }
215
- interface SpacePlanOutput<TPackage> {
216
- readonly spaceId: string;
217
- readonly migrationPackages: readonly TPackage[];
218
- }
219
- /**
220
- * Iterate the per-space planner across a set of loaded contract spaces
221
- * and return a deterministic shape regardless of declaration order.
222
- *
223
- * Behaviour:
224
- *
225
- * - The output is sorted alphabetically by `spaceId` (AM3). Two callers
226
- * passing the same set of inputs in different orders observe
227
- * byte-identical outputs.
228
- * - The per-space planner (`planSpace`) is called exactly once per
229
- * input, in alphabetical-by-spaceId order. Its return value is
230
- * attached to the corresponding output entry verbatim.
231
- * - Duplicate `spaceId`s in the input array throw
232
- * `MIGRATION.DUPLICATE_SPACE_ID` before any `planSpace` call runs,
233
- * keeping the planner pure when the input is malformed.
234
- *
235
- * The signature is generic over `TContract` and `TPackage` because the
236
- * shape is framework-neutral (SQL family today, Mongo family
237
- * eventually). Callers wire in whatever contract value and migration
238
- * package shape their family already speaks.
239
- *
240
- * Synchronous: the underlying per-space planner (target's
241
- * `MigrationPlanner.plan(...)`) is synchronous; callers that need to
242
- * resolve async I/O (e.g. reading pinned `contract.json` from disk)
243
- * resolve it before calling `planAllSpaces` and pass the materialised
244
- * inputs through.
245
- *
246
- * @see specs/framework-mechanism.spec.md § 3 — Per-space planner (T1.3).
247
- */
248
- declare function planAllSpaces<TContract, TPackage>(inputs: readonly SpacePlanInput<TContract>[], planSpace: (input: SpacePlanInput<TContract>) => readonly TPackage[]): readonly SpacePlanOutput<TPackage>[];
249
- //#endregion
250
- //#region src/read-pinned-contract-hash.d.ts
251
- /**
252
- * Read the pinned head hash for an extension space.
253
- *
254
- * Returns the `hash` field of `<projectMigrationsDir>/<spaceId>/refs/head.json`
255
- * — i.e. the canonical contract hash the framework wrote on the last
256
- * `migrate` for this space. Returns `null` when the file does not exist
257
- * (or the migrations directory is missing entirely), which is the
258
- * "first emit" signal {@link import('./detect-space-contract-drift').detectSpaceContractDrift}
259
- * uses to distinguish a brand-new extension from drift.
260
- *
261
- * Pure I/O (read + parse). The "comparison hash" is stored on disk by
262
- * {@link import('./emit-pinned-space-artefacts').emitPinnedSpaceArtefacts}
263
- * via the descriptor's `headRef.hash`, so reading it back here matches
264
- * the descriptor's hashing pipeline by construction — neither side
265
- * recomputes anything.
266
- *
267
- * Validation:
268
- *
269
- * - Rejects the app space — pinned head refs are an extension-space
270
- * concept; the app space's contract-of-record lives at the project
271
- * root, not under `migrations/`.
272
- * - Validates the space id against the same `[a-z][a-z0-9_-]{0,63}`
273
- * pattern as the rest of the per-space helpers.
274
- * - Surfaces `MIGRATION.INVALID_JSON` / `MIGRATION.INVALID_REF_FILE`
275
- * on a corrupt `refs/head.json` so callers can distinguish "no
276
- * pinned file" (returns `null`) from "pinned file but unreadable"
277
- * (throws).
278
- *
279
- * @see specs/framework-mechanism.spec.md § 3 — Drift detection (T1.9).
280
- */
281
- declare function readPinnedContractHash(projectMigrationsDir: string, spaceId: string): Promise<string | null>;
282
- //#endregion
283
- //#region src/space-layout.d.ts
284
- /**
285
- * Branded string carrying a compile-time guarantee that the value has
286
- * been validated by {@link assertValidSpaceId}. Downstream filesystem
287
- * helpers (e.g. {@link spaceMigrationDirectory}) accept this type to
288
- * make "validated" tracking visible at the type level rather than
289
- * relying purely on a runtime check.
290
282
  */
291
- type ValidSpaceId = string & {
292
- readonly __brand: 'ValidSpaceId';
293
- };
294
- declare function isValidSpaceId(spaceId: string): spaceId is ValidSpaceId;
295
- declare function assertValidSpaceId(spaceId: string): asserts spaceId is ValidSpaceId;
296
- /**
297
- * Resolve the migrations subdirectory for a given contract space.
298
- *
299
- * - **App space** (`spaceId === APP_SPACE_ID`) keeps today's layout: the
300
- * project's `migrations/` directory is the migrations directory, no
301
- * subdirectory.
302
- * - **Extension space** lands under `<projectMigrationsDir>/<spaceId>/`.
303
- * The space id is validated against {@link SPACE_ID_PATTERN} because
304
- * it becomes a filesystem directory name verbatim.
305
- *
306
- * `projectMigrationsDir` is the project's top-level `migrations/`
307
- * directory; the helper does not assume anything about its absolute /
308
- * relative shape and is symmetric with `pathe.join`.
309
- */
310
- declare function spaceMigrationDirectory(projectMigrationsDir: string, spaceId: string): string;
283
+ declare function emitContractSpaceArtefacts(projectMigrationsDir: string, spaceId: string, inputs: ContractSpaceArtefactInputs): Promise<void>;
311
284
  //#endregion
312
285
  //#region src/verify-contract-spaces.d.ts
313
286
  /**
314
- * List the per-space pinned subdirectories under
287
+ * List the per-space subdirectories under
315
288
  * `<projectRoot>/migrations/`. Returns space-id directory names (sorted
316
289
  * alphabetically) — i.e. any non-dot-prefixed subdirectory whose root
317
290
  * does **not** contain a `migration.json` manifest. The manifest is the
@@ -325,24 +298,22 @@ declare function spaceMigrationDirectory(projectMigrationsDir: string, spaceId:
325
298
  * Reads only the user's repo. **No descriptor import.** The caller
326
299
  * (verifier) feeds the result into {@link verifyContractSpaces} alongside
327
300
  * the loaded-space set and the marker rows.
328
- *
329
- * @see specs/framework-mechanism.spec.md § 4 — Verifier (steps 5–6).
330
301
  */
331
- declare function listPinnedSpaceDirectories(projectMigrationsDir: string): Promise<readonly string[]>;
302
+ declare function listContractSpaceDirectories(projectMigrationsDir: string): Promise<readonly string[]>;
332
303
  /**
333
- * Pinned head value (`(hash, invariants)`) for one contract space.
304
+ * On-disk head value (`(hash, invariants)`) for one contract space.
334
305
  * The verifier compares this against the marker row for the same space
335
306
  * to detect drift between the user-emitted artefacts and the live DB
336
307
  * marker.
337
308
  */
338
- interface SpacePinnedHashRecord {
309
+ interface ContractSpaceHeadRecord {
339
310
  readonly hash: string;
340
311
  readonly invariants: readonly string[];
341
312
  }
342
313
  /**
343
314
  * Marker row read from `prisma_contract.marker` (one per `space`).
344
- * Caller resolves these via the family runtime's marker reader (T1.1)
345
- * before invoking {@link verifyContractSpaces}.
315
+ * Caller resolves these via the family runtime's marker reader before
316
+ * invoking {@link verifyContractSpaces}.
346
317
  */
347
318
  interface SpaceMarkerRecord {
348
319
  readonly hash: string;
@@ -358,19 +329,19 @@ interface VerifyContractSpacesInputs {
358
329
  */
359
330
  readonly loadedSpaces: ReadonlySet<string>;
360
331
  /**
361
- * Pinned per-space subdirectories observed under
332
+ * Per-space subdirectories observed under
362
333
  * `<projectRoot>/migrations/`. Resolved via
363
- * {@link listPinnedSpaceDirectories}.
334
+ * {@link listContractSpaceDirectories}.
364
335
  */
365
- readonly pinnedDirsOnDisk: readonly string[];
336
+ readonly spaceDirsOnDisk: readonly string[];
366
337
  /**
367
- * Pinned head ref per space, keyed by space id. Caller reads
338
+ * Head ref per space, keyed by space id. Caller reads
368
339
  * `<projectRoot>/migrations/<space-id>/contract.json` and
369
- * `refs/head.json` (or, for app-space if its pinned shape ever moves
370
- * under `migrations/`, the equivalent files) to construct this map.
371
- * Spaces with no pinned dir on disk simply omit a map entry.
340
+ * `<projectRoot>/migrations/<space-id>/refs/head.json` to construct
341
+ * this map. Spaces with no contract-space dir on disk simply omit a
342
+ * map entry.
372
343
  */
373
- readonly pinnedHashesBySpace: ReadonlyMap<string, SpacePinnedHashRecord>;
344
+ readonly headRefsBySpace: ReadonlyMap<string, ContractSpaceHeadRecord>;
374
345
  /**
375
346
  * Marker rows keyed by `space`. Caller reads them from the
376
347
  * `prisma_contract.marker` table.
@@ -386,19 +357,19 @@ type SpaceVerifierViolation = {
386
357
  readonly spaceId: string;
387
358
  readonly remediation: string;
388
359
  } | {
389
- readonly kind: 'orphanPinnedDir';
360
+ readonly kind: 'orphanSpaceDir';
390
361
  readonly spaceId: string;
391
362
  readonly remediation: string;
392
363
  } | {
393
364
  readonly kind: 'hashMismatch';
394
365
  readonly spaceId: string;
395
- readonly pinnedHash: string;
366
+ readonly priorHeadHash: string;
396
367
  readonly markerHash: string;
397
368
  readonly remediation: string;
398
369
  } | {
399
370
  readonly kind: 'invariantsMismatch';
400
371
  readonly spaceId: string;
401
- readonly pinnedInvariants: readonly string[];
372
+ readonly onDiskInvariants: readonly string[];
402
373
  readonly markerInvariants: readonly string[];
403
374
  readonly remediation: string;
404
375
  };
@@ -410,38 +381,170 @@ type VerifyContractSpacesResult = {
410
381
  };
411
382
  /**
412
383
  * Pure structural verifier for the per-space mechanism. Aggregates the
413
- * three orphan / missing checks (FR6 cases a–c) plus per-space hash and
414
- * invariant comparison.
384
+ * three orphan / missing checks plus per-space hash and invariant
385
+ * comparison.
415
386
  *
416
- * Algorithm (sub-spec § 4):
387
+ * Algorithm:
417
388
  *
418
389
  * - For every extension space declared in `loadedSpaces` (`'app'`
419
- * excluded — its pinned `contract.json` lives at the project root):
420
- * - If no pinned dir on disk → `declaredButUnmigrated`.
390
+ * excluded — the per-space verifier is scoped to extension members;
391
+ * the app is verified through the aggregate path):
392
+ * - If no contract-space dir on disk → `declaredButUnmigrated`.
421
393
  * - Else if `markerRowsBySpace` lacks an entry → no violation here;
422
- * the live-DB compare in step 8 (out of scope of this helper) is
423
- * where the absence shows up.
424
- * - Else compare marker hash / invariants vs. pinned hash /
394
+ * the live-DB compare done outside this helper is where the
395
+ * absence shows up.
396
+ * - Else compare marker hash / invariants vs. on-disk head hash /
425
397
  * invariants → `hashMismatch` / `invariantsMismatch` on drift.
426
- * - For every pinned dir on disk that is not in `loadedSpaces` →
427
- * `orphanPinnedDir`.
398
+ * - For every contract-space dir on disk that is not in `loadedSpaces` →
399
+ * `orphanSpaceDir`.
428
400
  * - For every marker row whose `space` is not in `loadedSpaces` →
429
401
  * `orphanMarker`. The app-space marker is always loaded (`'app'` is
430
402
  * in `loadedSpaces` by definition).
431
403
  *
432
- * Output is deterministic (NFR6): violations are sorted first by `kind`
433
- * (`declaredButUnmigrated` → `orphanMarker` → `orphanPinnedDir` →
404
+ * Output is deterministic: violations are sorted first by `kind`
405
+ * (`declaredButUnmigrated` → `orphanMarker` → `orphanSpaceDir` →
434
406
  * `hashMismatch` → `invariantsMismatch`) then by `spaceId`. Two callers
435
407
  * passing equivalent inputs see byte-identical violation lists.
436
408
  *
437
409
  * Synchronous, pure, no I/O. **Does not import the extension descriptor**
438
- * (the inputs are pre-resolved by the caller). This is the property
439
- * AC-15 / AC-26 ("verifier reads only the user repo, not
440
- * `node_modules`") locks in.
441
- *
442
- * @see specs/framework-mechanism.spec.md § 4 — Verifier (T1.5).
410
+ * (the inputs are pre-resolved by the caller); the verifier reads only
411
+ * the user repo, not `node_modules`.
443
412
  */
444
413
  declare function verifyContractSpaces(inputs: VerifyContractSpacesInputs): VerifyContractSpacesResult;
445
414
  //#endregion
446
- export { APP_SPACE_ID, type DetectSpaceContractDriftInputs, type PinnedSpaceArtefactInputs, type PinnedSpaceHeadRef, type SpaceApplyInput, type SpaceContractDriftResult, type SpaceMarkerRecord, type SpacePinnedHashRecord, type SpacePlanInput, type SpacePlanOutput, type SpaceVerifierViolation, type ValidSpaceId, type VerifyContractSpacesInputs, type VerifyContractSpacesResult, assertValidSpaceId, concatenateSpaceApplyInputs, detectSpaceContractDrift, emitPinnedSpaceArtefacts, isValidSpaceId, listPinnedSpaceDirectories, planAllSpaces, readPinnedContractHash, spaceMigrationDirectory, verifyContractSpaces };
415
+ //#region src/gather-disk-contract-space-state.d.ts
416
+ /**
417
+ * Disk-side inputs to {@link import('./verify-contract-spaces').verifyContractSpaces}
418
+ * — gathered without touching the live database. The caller composes
419
+ * this with the marker rows it reads from the runtime to invoke the
420
+ * verifier.
421
+ */
422
+ interface DiskContractSpaceState {
423
+ /** Contract-space directory names observed under `<projectMigrationsDir>/`. */
424
+ readonly spaceDirsOnDisk: readonly string[];
425
+ /** Head-ref `(hash, invariants)` per extension space. */
426
+ readonly headRefsBySpace: ReadonlyMap<string, ContractSpaceHeadRecord>;
427
+ }
428
+ /**
429
+ * Read the on-disk state the per-space verifier needs:
430
+ *
431
+ * - The list of contract-space directories under
432
+ * `<projectMigrationsDir>/` (via
433
+ * {@link import('./verify-contract-spaces').listContractSpaceDirectories}).
434
+ * - The on-disk head ref `(hash, invariants)` for each declared extension space
435
+ * (via {@link readContractSpaceHeadRef}; missing on-disk artefacts are simply
436
+ * omitted — the verifier reports them as `declaredButUnmigrated`).
437
+ *
438
+ * Synchronous in spirit but async due to filesystem reads. Reads only
439
+ * the user's repo. **Does not import any extension descriptor module.**
440
+ *
441
+ * Composition convention: pure target-agnostic primitive in
442
+ * `1-framework`; the SQL family (and any future target family) wires
443
+ * it into its `dbInit` / `verify` flows alongside its own marker-row
444
+ * read before invoking `verifyContractSpaces`.
445
+ */
446
+ declare function gatherDiskContractSpaceState(args: {
447
+ readonly projectMigrationsDir: string;
448
+ /**
449
+ * Set of space ids the project declares: `'app'` plus each entry in
450
+ * `extensionPacks` whose descriptor exposes a `contractSpace`. The
451
+ * helper reads on-disk head data only for the extension members.
452
+ */
453
+ readonly loadedSpaceIds: ReadonlySet<string>;
454
+ }): Promise<DiskContractSpaceState>;
455
+ //#endregion
456
+ //#region src/plan-all-spaces.d.ts
457
+ /**
458
+ * Per-space input for {@link planAllSpaces}. One entry per loaded
459
+ * contract space (the application's `'app'` plus each extension that
460
+ * exposes a `contractSpace`).
461
+ *
462
+ * - `priorContract` is `null` for a space that has never been emitted
463
+ * (no `migrations/<space-id>/contract.json` on disk yet); otherwise it
464
+ * is the canonical contract value emitted for that space.
465
+ * - `newContract` is the canonical contract value the planner is about
466
+ * to emit for that space — for app-space, the just-emitted root
467
+ * `contract.json`; for an extension space, the descriptor's
468
+ * `contractSpace.contractJson`.
469
+ */
470
+ interface SpacePlanInput<TContract> {
471
+ readonly spaceId: string;
472
+ readonly priorContract: TContract | null;
473
+ readonly newContract: TContract;
474
+ }
475
+ interface SpacePlanOutput<TPackage> {
476
+ readonly spaceId: string;
477
+ readonly migrationPackages: readonly TPackage[];
478
+ }
479
+ /**
480
+ * Iterate the per-space planner across a set of loaded contract spaces
481
+ * and return a deterministic shape regardless of declaration order.
482
+ *
483
+ * Behaviour:
484
+ *
485
+ * - The output is sorted alphabetically by `spaceId`. Two callers
486
+ * passing the same set of inputs in different orders observe
487
+ * byte-identical outputs.
488
+ * - The per-space planner (`planSpace`) is called exactly once per
489
+ * input, in alphabetical-by-spaceId order. Its return value is
490
+ * attached to the corresponding output entry verbatim.
491
+ * - Duplicate `spaceId`s in the input array throw
492
+ * `MIGRATION.DUPLICATE_SPACE_ID` before any `planSpace` call runs,
493
+ * keeping the planner pure when the input is malformed.
494
+ *
495
+ * The signature is generic over `TContract` and `TPackage` because the
496
+ * shape is framework-neutral (SQL family today, Mongo family
497
+ * eventually). Callers wire in whatever contract value and migration
498
+ * package shape their family already speaks.
499
+ *
500
+ * Synchronous: the underlying per-space planner (target's
501
+ * `MigrationPlanner.plan(...)`) is synchronous; callers that need to
502
+ * resolve async I/O (e.g. reading on-disk `contract.json` from disk)
503
+ * resolve it before calling `planAllSpaces` and pass the materialised
504
+ * inputs through.
505
+ */
506
+ declare function planAllSpaces<TContract, TPackage>(inputs: readonly SpacePlanInput<TContract>[], planSpace: (input: SpacePlanInput<TContract>) => readonly TPackage[]): readonly SpacePlanOutput<TPackage>[];
507
+ //#endregion
508
+ //#region src/read-contract-space-contract.d.ts
509
+ /**
510
+ * Read the on-disk contract value for a contract space
511
+ * (`<projectMigrationsDir>/<spaceId>/contract.json`). Returns the parsed
512
+ * JSON value as `unknown` — callers that need a typed contract validate
513
+ * via their family's `validateContract` to surface schema issues.
514
+ *
515
+ * Companion to {@link import('./read-contract-space-head-ref').readContractSpaceHeadRef}
516
+ * — same ENOENT-throws / corrupt-file-error semantics. Returns the
517
+ * canonical-JSON value the framework wrote during emit, so re-running
518
+ * this helper across machines / runs yields a byte-identical value.
519
+ */
520
+ declare function readContractSpaceContract(projectMigrationsDir: string, spaceId: string): Promise<unknown>;
521
+ //#endregion
522
+ //#region src/space-layout.d.ts
523
+ /**
524
+ * Branded string carrying a compile-time guarantee that the value has
525
+ * been validated by {@link assertValidSpaceId}. Downstream filesystem
526
+ * helpers (e.g. {@link spaceMigrationDirectory}) accept this type to
527
+ * make "validated" tracking visible at the type level rather than
528
+ * relying purely on a runtime check.
529
+ */
530
+ type ValidSpaceId = string & {
531
+ readonly __brand: 'ValidSpaceId';
532
+ };
533
+ declare function isValidSpaceId(spaceId: string): spaceId is ValidSpaceId;
534
+ declare function assertValidSpaceId(spaceId: string): asserts spaceId is ValidSpaceId;
535
+ /**
536
+ * Resolve the migrations subdirectory for a given contract space.
537
+ *
538
+ * Every contract space — including the app space (default `'app'`) —
539
+ * lands under `<projectMigrationsDir>/<spaceId>/`. The space id is
540
+ * validated against {@link SPACE_ID_PATTERN} because it becomes a
541
+ * filesystem directory name verbatim.
542
+ *
543
+ * `projectMigrationsDir` is the project's top-level `migrations/`
544
+ * directory; the helper does not assume anything about its absolute /
545
+ * relative shape and is symmetric with `pathe.join`.
546
+ */
547
+ declare function spaceMigrationDirectory(projectMigrationsDir: string, spaceId: string): string;
548
+ //#endregion
549
+ export { APP_SPACE_ID, type ComputeExtensionSpaceApplyPathInputs, type ContractSpaceArtefactInputs, type ContractSpaceHeadRecord, type ContractSpaceHeadRef, type DescriptorSelfConsistencyInputs, type DetectSpaceContractDriftInputs, type DiskContractSpaceState, type ExtensionSpaceApplyPathOutcome, type SpaceApplyInput, type SpaceContractDriftResult, type SpaceMarkerRecord, type SpacePlanInput, type SpacePlanOutput, type SpaceVerifierViolation, type ValidSpaceId, type VerifyContractSpacesInputs, type VerifyContractSpacesResult, assertDescriptorSelfConsistency, assertValidSpaceId, computeExtensionSpaceApplyPath, detectSpaceContractDrift, emitContractSpaceArtefacts, gatherDiskContractSpaceState, isValidSpaceId, listContractSpaceDirectories, planAllSpaces, readContractSpaceContract, readContractSpaceHeadRef, spaceMigrationDirectory, verifyContractSpaces };
447
550
  //# sourceMappingURL=spaces.d.mts.map