@prisma-next/migration-tools 0.5.0-dev.9 → 0.6.0-dev.1

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