@prisma-next/migration-tools 0.5.0-dev.8 → 0.5.0-dev.81

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 (130) hide show
  1. package/README.md +34 -22
  2. package/dist/{constants-BRi0X7B_.mjs → constants-DWV9_o2Z.mjs} +2 -2
  3. package/dist/{constants-BRi0X7B_.mjs.map → constants-DWV9_o2Z.mjs.map} +1 -1
  4. package/dist/errors-EPL_9p9f.mjs +297 -0
  5. package/dist/errors-EPL_9p9f.mjs.map +1 -0
  6. package/dist/exports/aggregate.d.mts +614 -0
  7. package/dist/exports/aggregate.d.mts.map +1 -0
  8. package/dist/exports/aggregate.mjs +611 -0
  9. package/dist/exports/aggregate.mjs.map +1 -0
  10. package/dist/exports/constants.d.mts.map +1 -1
  11. package/dist/exports/constants.mjs +2 -3
  12. package/dist/exports/errors.d.mts +68 -0
  13. package/dist/exports/errors.d.mts.map +1 -0
  14. package/dist/exports/errors.mjs +2 -0
  15. package/dist/exports/graph.d.mts +2 -0
  16. package/dist/exports/graph.mjs +1 -0
  17. package/dist/exports/hash.d.mts +52 -0
  18. package/dist/exports/hash.d.mts.map +1 -0
  19. package/dist/exports/hash.mjs +2 -0
  20. package/dist/exports/invariants.d.mts +34 -0
  21. package/dist/exports/invariants.d.mts.map +1 -0
  22. package/dist/exports/invariants.mjs +2 -0
  23. package/dist/exports/io.d.mts +66 -6
  24. package/dist/exports/io.d.mts.map +1 -1
  25. package/dist/exports/io.mjs +2 -3
  26. package/dist/exports/metadata.d.mts +2 -0
  27. package/dist/exports/metadata.mjs +1 -0
  28. package/dist/exports/migration-graph.d.mts +2 -0
  29. package/dist/exports/migration-graph.mjs +2 -0
  30. package/dist/exports/migration-ts.d.mts.map +1 -1
  31. package/dist/exports/migration-ts.mjs +2 -4
  32. package/dist/exports/migration-ts.mjs.map +1 -1
  33. package/dist/exports/migration.d.mts +15 -14
  34. package/dist/exports/migration.d.mts.map +1 -1
  35. package/dist/exports/migration.mjs +70 -43
  36. package/dist/exports/migration.mjs.map +1 -1
  37. package/dist/exports/package.d.mts +3 -0
  38. package/dist/exports/package.mjs +1 -0
  39. package/dist/exports/refs.d.mts.map +1 -1
  40. package/dist/exports/refs.mjs +3 -4
  41. package/dist/exports/refs.mjs.map +1 -1
  42. package/dist/exports/spaces.d.mts +550 -0
  43. package/dist/exports/spaces.d.mts.map +1 -0
  44. package/dist/exports/spaces.mjs +223 -0
  45. package/dist/exports/spaces.mjs.map +1 -0
  46. package/dist/graph-HMWAldoR.d.mts +28 -0
  47. package/dist/graph-HMWAldoR.d.mts.map +1 -0
  48. package/dist/hash-By50zM_E.mjs +74 -0
  49. package/dist/hash-By50zM_E.mjs.map +1 -0
  50. package/dist/invariants-Duc8f9NM.mjs +52 -0
  51. package/dist/invariants-Duc8f9NM.mjs.map +1 -0
  52. package/dist/io-D13dLvUh.mjs +239 -0
  53. package/dist/io-D13dLvUh.mjs.map +1 -0
  54. package/dist/metadata-CFvm3ayn.d.mts +2 -0
  55. package/dist/migration-graph-DGNnKDY5.mjs +523 -0
  56. package/dist/migration-graph-DGNnKDY5.mjs.map +1 -0
  57. package/dist/migration-graph-DulOITvG.d.mts +124 -0
  58. package/dist/migration-graph-DulOITvG.d.mts.map +1 -0
  59. package/dist/op-schema-D5qkXfEf.mjs +13 -0
  60. package/dist/op-schema-D5qkXfEf.mjs.map +1 -0
  61. package/dist/package-BjiZ7KDy.d.mts +21 -0
  62. package/dist/package-BjiZ7KDy.d.mts.map +1 -0
  63. package/dist/read-contract-space-contract-C3-1eyaI.mjs +298 -0
  64. package/dist/read-contract-space-contract-C3-1eyaI.mjs.map +1 -0
  65. package/package.json +42 -17
  66. package/src/aggregate/loader.ts +409 -0
  67. package/src/aggregate/marker-types.ts +16 -0
  68. package/src/aggregate/planner-types.ts +171 -0
  69. package/src/aggregate/planner.ts +158 -0
  70. package/src/aggregate/project-schema-to-space.ts +64 -0
  71. package/src/aggregate/strategies/graph-walk.ts +118 -0
  72. package/src/aggregate/strategies/synth.ts +122 -0
  73. package/src/aggregate/types.ts +89 -0
  74. package/src/aggregate/verifier.ts +230 -0
  75. package/src/assert-descriptor-self-consistency.ts +70 -0
  76. package/src/compute-extension-space-apply-path.ts +152 -0
  77. package/src/concatenate-space-apply-inputs.ts +90 -0
  78. package/src/detect-space-contract-drift.ts +91 -0
  79. package/src/emit-contract-space-artefacts.ts +70 -0
  80. package/src/errors.ts +251 -17
  81. package/src/exports/aggregate.ts +42 -0
  82. package/src/exports/errors.ts +8 -0
  83. package/src/exports/graph.ts +1 -0
  84. package/src/exports/hash.ts +2 -0
  85. package/src/exports/invariants.ts +1 -0
  86. package/src/exports/io.ts +3 -1
  87. package/src/exports/metadata.ts +1 -0
  88. package/src/exports/{dag.ts → migration-graph.ts} +3 -2
  89. package/src/exports/migration.ts +0 -1
  90. package/src/exports/package.ts +2 -0
  91. package/src/exports/spaces.ts +49 -0
  92. package/src/gather-disk-contract-space-state.ts +62 -0
  93. package/src/graph-ops.ts +57 -30
  94. package/src/graph.ts +25 -0
  95. package/src/hash.ts +91 -0
  96. package/src/invariants.ts +56 -0
  97. package/src/io.ts +163 -40
  98. package/src/metadata.ts +1 -0
  99. package/src/migration-base.ts +97 -56
  100. package/src/migration-graph.ts +676 -0
  101. package/src/op-schema.ts +11 -0
  102. package/src/package.ts +21 -0
  103. package/src/plan-all-spaces.ts +76 -0
  104. package/src/read-contract-space-contract.ts +44 -0
  105. package/src/read-contract-space-head-ref.ts +63 -0
  106. package/src/space-layout.ts +48 -0
  107. package/src/verify-contract-spaces.ts +272 -0
  108. package/dist/attestation-BnzTb0Qp.mjs +0 -65
  109. package/dist/attestation-BnzTb0Qp.mjs.map +0 -1
  110. package/dist/errors-BmiSgz1j.mjs +0 -160
  111. package/dist/errors-BmiSgz1j.mjs.map +0 -1
  112. package/dist/exports/attestation.d.mts +0 -37
  113. package/dist/exports/attestation.d.mts.map +0 -1
  114. package/dist/exports/attestation.mjs +0 -4
  115. package/dist/exports/dag.d.mts +0 -51
  116. package/dist/exports/dag.d.mts.map +0 -1
  117. package/dist/exports/dag.mjs +0 -386
  118. package/dist/exports/dag.mjs.map +0 -1
  119. package/dist/exports/types.d.mts +0 -35
  120. package/dist/exports/types.d.mts.map +0 -1
  121. package/dist/exports/types.mjs +0 -3
  122. package/dist/io-Cd6GLyjK.mjs +0 -153
  123. package/dist/io-Cd6GLyjK.mjs.map +0 -1
  124. package/dist/types-DyGXcWWp.d.mts +0 -71
  125. package/dist/types-DyGXcWWp.d.mts.map +0 -1
  126. package/src/attestation.ts +0 -81
  127. package/src/dag.ts +0 -426
  128. package/src/exports/attestation.ts +0 -2
  129. package/src/exports/types.ts +0 -10
  130. package/src/types.ts +0 -66
@@ -0,0 +1,550 @@
1
+ import { t as MigrationOps } from "../package-BjiZ7KDy.mjs";
2
+ import { APP_SPACE_ID, ContractSpaceHeadRef } from "@prisma-next/framework-components/control";
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
145
+ //#region src/concatenate-space-apply-inputs.d.ts
146
+ /**
147
+ * Per-space input the runner consumes when applying a migration.
148
+ *
149
+ * The shape is target-agnostic: callers (today the SQL family; later
150
+ * any other family) bind `TOp` to their own per-target operation type
151
+ * (e.g. `SqlMigrationPlanOperation<TTargetDetails>` for the SQL family)
152
+ * and the helper preserves it through the concatenation.
153
+ *
154
+ * - `migrationDirectory` is the on-disk migration directory for the
155
+ * space — `<projectRoot>/migrations/<space-id>` (uniform; the app
156
+ * subspaces under its own `<APP_SPACE_ID>/` directory).
157
+ * - `currentMarkerHash` and `currentMarkerInvariants` are the values
158
+ * read from the `prisma_contract.marker` row keyed by `space = <space-id>`
159
+ * (T1.1). `null` hash = no marker row yet.
160
+ * - `path` is the per-space operation list resolved from
161
+ * `findPathWithDecision(currentMarker, ref.hash, effectiveRequired)`
162
+ * per ADR 208, materialised against the on-disk migration packages.
163
+ *
164
+ * @see specs/framework-mechanism.spec.md § 4 — Runner.
165
+ */
166
+ interface SpaceApplyInput<TOp> {
167
+ readonly spaceId: string;
168
+ readonly migrationDirectory: string;
169
+ readonly currentMarkerHash: string | null;
170
+ readonly currentMarkerInvariants: readonly string[];
171
+ readonly path: readonly TOp[];
172
+ }
173
+ //#endregion
174
+ //#region src/detect-space-contract-drift.d.ts
175
+ /**
176
+ * Inputs for {@link detectSpaceContractDrift}.
177
+ *
178
+ * Both hashes are produced by the caller (the SQL-family wiring at the
179
+ * consumption site) using the canonical contract hashing pipeline.
180
+ * Keeping the helper pure lets `migration-tools` stay framework-neutral
181
+ * — the SQL family already speaks `Contract<SqlStorage>`, the Mongo
182
+ * family speaks its own contract type, and both reduce to a hash string
183
+ * before drift detection runs.
184
+ *
185
+ * `priorHeadHash` is `null` when no `contract.json` exists yet on disk for
186
+ * the space (the descriptor declares an extension that has never been
187
+ * emitted into the user's repo). That's the "first emit" case — no
188
+ * drift to surface; the migrate emit will create the on-disk artefacts.
189
+ */
190
+ interface DetectSpaceContractDriftInputs {
191
+ readonly descriptorHash: string;
192
+ readonly priorHeadHash: string | null;
193
+ }
194
+ /**
195
+ * Result discriminant for {@link detectSpaceContractDrift}.
196
+ *
197
+ * - `noDrift`: descriptor hash and on-disk head hash agree byte-for-byte.
198
+ * The migrate emit can proceed with no warning.
199
+ * - `firstEmit`: no on-disk `contract.json` on disk yet. The extension
200
+ * was just added to `extensionPacks`; this run will create the
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
204
+ * surfaces a non-fatal warning naming the extension and the
205
+ * diff direction (descriptor → on-disk head). The migrate emit proceeds
206
+ * normally so the bump is materialised this run; the warning just
207
+ * confirms the bump is being captured.
208
+ *
209
+ * `spaceId`, `descriptorHash`, and `priorHeadHash` are threaded through
210
+ * verbatim so the caller (logger / TerminalUI / strict-mode envelope)
211
+ * has everything it needs to format the warning message without
212
+ * re-reading the descriptor or the on-disk artefact.
213
+ */
214
+ type SpaceContractDriftResult = {
215
+ readonly kind: 'noDrift' | 'firstEmit' | 'drift';
216
+ readonly spaceId: string;
217
+ readonly descriptorHash: string;
218
+ readonly priorHeadHash: string | null;
219
+ };
220
+ /**
221
+ * Pure drift-detection primitive for a single contract space.
222
+ *
223
+ * Runs once per loaded extension space, just before computing the
224
+ * `priorContract` that feeds {@link import('./plan-all-spaces').planAllSpaces}.
225
+ * Hash equality is byte-for-byte (no normalisation) — both sides are
226
+ * already canonical hashes produced by the same pipeline, so any
227
+ * difference is meaningful drift.
228
+ *
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}
233
+ * which provides the read-side primitive.
234
+ *
235
+ * The drift warning surfaces the extension name and the diff direction.
236
+ */
237
+ declare function detectSpaceContractDrift(spaceId: string, inputs: DetectSpaceContractDriftInputs): SpaceContractDriftResult;
238
+ //#endregion
239
+ //#region src/emit-contract-space-artefacts.d.ts
240
+ /**
241
+ * Inputs for {@link emitContractSpaceArtefacts}.
242
+ *
243
+ * - `contract` is the canonical contract value the framework just emitted
244
+ * for the space; it is serialised through {@link canonicalizeJson}, so
245
+ * it must be a JSON-compatible value (objects / arrays / primitives).
246
+ * Typed as `unknown` rather than the SQL-family `Contract<SqlStorage>`
247
+ * to keep `migration-tools` framework-neutral; SQL-family callers pass
248
+ * their typed value through unchanged.
249
+ *
250
+ * - `contractDts` is the pre-rendered `.d.ts` text. Rendering happens in
251
+ * the SQL family (which owns the codec / typemap input the renderer
252
+ * needs), so this helper accepts the text verbatim and writes it out
253
+ * without further transformation.
254
+ *
255
+ * - `headRef` is the head reference for the space.
256
+ * `invariants` are sorted alphabetically before serialisation so two
257
+ * callers passing the same set in different orders produce
258
+ * byte-identical `refs/head.json`.
259
+ */
260
+ interface ContractSpaceArtefactInputs {
261
+ readonly contract: unknown;
262
+ readonly contractDts: string;
263
+ readonly headRef: ContractSpaceHeadRef;
264
+ }
265
+ /**
266
+ * Emit the per-space artefacts (`contract.json`, `contract.d.ts`,
267
+ * `refs/head.json`) under `<projectMigrationsDir>/<spaceId>/`.
268
+ *
269
+ * Always-overwrite: the framework owns these files; running `migrate`
270
+ * twice with the same inputs is a no-op observably (idempotent), but the
271
+ * helper does not check pre-existing contents — re-emit always wins.
272
+ *
273
+ * Path layout matches the convention in
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'`).
279
+ *
280
+ * The migrations directory and space subdirectory are created if they
281
+ * do not yet exist (`mkdir { recursive: true }`).
282
+ */
283
+ declare function emitContractSpaceArtefacts(projectMigrationsDir: string, spaceId: string, inputs: ContractSpaceArtefactInputs): Promise<void>;
284
+ //#endregion
285
+ //#region src/verify-contract-spaces.d.ts
286
+ /**
287
+ * List the per-space subdirectories under
288
+ * `<projectRoot>/migrations/`. Returns space-id directory names (sorted
289
+ * alphabetically) — i.e. any non-dot-prefixed subdirectory whose root
290
+ * does **not** contain a `migration.json` manifest. The manifest is the
291
+ * structural marker of a user-authored migration directory (see
292
+ * `readMigrationsDir` in `./io`); directory names themselves belong to
293
+ * the user and are not part of the contract.
294
+ *
295
+ * Returns `[]` if the migrations directory does not exist (greenfield
296
+ * project).
297
+ *
298
+ * Reads only the user's repo. **No descriptor import.** The caller
299
+ * (verifier) feeds the result into {@link verifyContractSpaces} alongside
300
+ * the loaded-space set and the marker rows.
301
+ */
302
+ declare function listContractSpaceDirectories(projectMigrationsDir: string): Promise<readonly string[]>;
303
+ /**
304
+ * On-disk head value (`(hash, invariants)`) for one contract space.
305
+ * The verifier compares this against the marker row for the same space
306
+ * to detect drift between the user-emitted artefacts and the live DB
307
+ * marker.
308
+ */
309
+ interface ContractSpaceHeadRecord {
310
+ readonly hash: string;
311
+ readonly invariants: readonly string[];
312
+ }
313
+ /**
314
+ * Marker row read from `prisma_contract.marker` (one per `space`).
315
+ * Caller resolves these via the family runtime's marker reader before
316
+ * invoking {@link verifyContractSpaces}.
317
+ */
318
+ interface SpaceMarkerRecord {
319
+ readonly hash: string;
320
+ readonly invariants: readonly string[];
321
+ }
322
+ interface VerifyContractSpacesInputs {
323
+ /**
324
+ * Set of contract spaces the project declares: `'app'` plus each
325
+ * extension space in `extensionPacks`. The caller's discovery path
326
+ * never reads the extension descriptor module — it walks the
327
+ * `extensionPacks` configuration in `prisma-next.config.ts` for the
328
+ * space ids.
329
+ */
330
+ readonly loadedSpaces: ReadonlySet<string>;
331
+ /**
332
+ * Per-space subdirectories observed under
333
+ * `<projectRoot>/migrations/`. Resolved via
334
+ * {@link listContractSpaceDirectories}.
335
+ */
336
+ readonly spaceDirsOnDisk: readonly string[];
337
+ /**
338
+ * Head ref per space, keyed by space id. Caller reads
339
+ * `<projectRoot>/migrations/<space-id>/contract.json` and
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.
343
+ */
344
+ readonly headRefsBySpace: ReadonlyMap<string, ContractSpaceHeadRecord>;
345
+ /**
346
+ * Marker rows keyed by `space`. Caller reads them from the
347
+ * `prisma_contract.marker` table.
348
+ */
349
+ readonly markerRowsBySpace: ReadonlyMap<string, SpaceMarkerRecord>;
350
+ }
351
+ type SpaceVerifierViolation = {
352
+ readonly kind: 'declaredButUnmigrated';
353
+ readonly spaceId: string;
354
+ readonly remediation: string;
355
+ } | {
356
+ readonly kind: 'orphanMarker';
357
+ readonly spaceId: string;
358
+ readonly remediation: string;
359
+ } | {
360
+ readonly kind: 'orphanSpaceDir';
361
+ readonly spaceId: string;
362
+ readonly remediation: string;
363
+ } | {
364
+ readonly kind: 'hashMismatch';
365
+ readonly spaceId: string;
366
+ readonly priorHeadHash: string;
367
+ readonly markerHash: string;
368
+ readonly remediation: string;
369
+ } | {
370
+ readonly kind: 'invariantsMismatch';
371
+ readonly spaceId: string;
372
+ readonly onDiskInvariants: readonly string[];
373
+ readonly markerInvariants: readonly string[];
374
+ readonly remediation: string;
375
+ };
376
+ type VerifyContractSpacesResult = {
377
+ readonly ok: true;
378
+ } | {
379
+ readonly ok: false;
380
+ readonly violations: readonly SpaceVerifierViolation[];
381
+ };
382
+ /**
383
+ * Pure structural verifier for the per-space mechanism. Aggregates the
384
+ * three orphan / missing checks plus per-space hash and invariant
385
+ * comparison.
386
+ *
387
+ * Algorithm:
388
+ *
389
+ * - For every extension space declared in `loadedSpaces` (`'app'`
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`.
393
+ * - Else if `markerRowsBySpace` lacks an entry → no violation here;
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 /
397
+ * invariants → `hashMismatch` / `invariantsMismatch` on drift.
398
+ * - For every contract-space dir on disk that is not in `loadedSpaces` →
399
+ * `orphanSpaceDir`.
400
+ * - For every marker row whose `space` is not in `loadedSpaces` →
401
+ * `orphanMarker`. The app-space marker is always loaded (`'app'` is
402
+ * in `loadedSpaces` by definition).
403
+ *
404
+ * Output is deterministic: violations are sorted first by `kind`
405
+ * (`declaredButUnmigrated` → `orphanMarker` → `orphanSpaceDir` →
406
+ * `hashMismatch` → `invariantsMismatch`) then by `spaceId`. Two callers
407
+ * passing equivalent inputs see byte-identical violation lists.
408
+ *
409
+ * Synchronous, pure, no I/O. **Does not import the extension descriptor**
410
+ * (the inputs are pre-resolved by the caller); the verifier reads only
411
+ * the user repo, not `node_modules`.
412
+ */
413
+ declare function verifyContractSpaces(inputs: VerifyContractSpacesInputs): VerifyContractSpacesResult;
414
+ //#endregion
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 };
550
+ //# sourceMappingURL=spaces.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"spaces.d.mts","names":[],"sources":["../../src/assert-descriptor-self-consistency.ts","../../src/read-contract-space-head-ref.ts","../../src/compute-extension-space-apply-path.ts","../../src/concatenate-space-apply-inputs.ts","../../src/detect-space-contract-drift.ts","../../src/emit-contract-space-artefacts.ts","../../src/verify-contract-spaces.ts","../../src/gather-disk-contract-space-state.ts","../../src/plan-all-spaces.ts","../../src/read-contract-space-contract.ts","../../src/space-layout.ts"],"mappings":";;;;;;;;AASA;;UAAiB,+BAAA;EAAA,SACN,WAAA;EAAA,SACA,MAAA;EAAA,SACA,YAAA;EAAA;;;;;AA+BX;;EA/BW,SAQA,OAAA;EAAA,SACA,WAAA;AAAA;;;;ACIX;;;;;;;;;;;;;ACTA;;;iBF2BgB,+BAAA,CAAgC,MAAA,EAAQ,+BAAA;;;;;AAlCxD;;;;;;;;;;;iBCgBsB,wBAAA,CACpB,oBAAA,UACA,OAAA,WACC,OAAA,CAAQ,oBAAA;;;;;ADnBX;;;;KEOY,8BAAA;EAAA,SAEG,IAAA;EAAA,SACA,oBAAA,EAAsB,oBAAA;EFC1B;;;;AAuBX;EAvBW,SEKI,kBAAA;;;;;WAKA,OAAA,EAAS,YAAA;;ADLxB;;;;WCWe,mBAAA;AAAA;EAAA,SAEA,IAAA;EAAA,SAA8B,oBAAA,EAAsB,oBAAA;AAAA;EAAA,SAEpD,IAAA;EAAA,SACA,oBAAA,EAAsB,oBAAA;EAAA,SACtB,OAAA;EAAA,SACA,cAAA;IAAA,SAAoC,OAAA;IAAA,SAA0B,EAAA;EAAA;AAAA;EAAA,SAE9D,IAAA;AAAA;;;;;;;;;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