@prisma-next/migration-tools 0.5.0-dev.9 → 0.5.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 (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 +599 -0
  7. package/dist/exports/aggregate.d.mts.map +1 -0
  8. package/dist/exports/aggregate.mjs +599 -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 +526 -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-Cme8KZk_.mjs +259 -0
  64. package/dist/read-contract-space-contract-Cme8KZk_.mjs.map +1 -0
  65. package/package.json +42 -17
  66. package/src/aggregate/loader.ts +379 -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 +159 -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/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 +45 -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 +61 -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,526 @@
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/emit-contract-space-artefacts.d.ts
216
+ /**
217
+ * Inputs for {@link emitContractSpaceArtefacts}.
218
+ *
219
+ * - `contract` is the canonical contract value the framework just emitted
220
+ * for the space; it is serialised through {@link canonicalizeJson}, so
221
+ * it must be a JSON-compatible value (objects / arrays / primitives).
222
+ * Typed as `unknown` rather than the SQL-family `Contract<SqlStorage>`
223
+ * to keep `migration-tools` framework-neutral; SQL-family callers pass
224
+ * their typed value through unchanged.
225
+ *
226
+ * - `contractDts` is the pre-rendered `.d.ts` text. Rendering happens in
227
+ * the SQL family (which owns the codec / typemap input the renderer
228
+ * needs), so this helper accepts the text verbatim and writes it out
229
+ * without further transformation.
230
+ *
231
+ * - `headRef` is the head reference for the space.
232
+ * `invariants` are sorted alphabetically before serialisation so two
233
+ * callers passing the same set in different orders produce
234
+ * byte-identical `refs/head.json`.
235
+ */
236
+ interface ContractSpaceArtefactInputs {
237
+ readonly contract: unknown;
238
+ readonly contractDts: string;
239
+ readonly headRef: ContractSpaceHeadRef;
240
+ }
241
+ /**
242
+ * Emit the per-space artefacts (`contract.json`, `contract.d.ts`,
243
+ * `refs/head.json`) under `<projectMigrationsDir>/<spaceId>/`.
244
+ *
245
+ * Always-overwrite: the framework owns these files; running `migrate`
246
+ * twice with the same inputs is a no-op observably (idempotent), but the
247
+ * helper does not check pre-existing contents — re-emit always wins.
248
+ *
249
+ * Path layout matches the convention in
250
+ * [`spaceMigrationDirectory`](./space-layout.ts). The space id is
251
+ * validated against `[a-z][a-z0-9_-]{0,63}` via
252
+ * {@link assertValidSpaceId} for filesystem-safety reasons; the helper
253
+ * accepts every space uniformly (including the app space, default
254
+ * `'app'`).
255
+ *
256
+ * The migrations directory and space subdirectory are created if they
257
+ * do not yet exist (`mkdir { recursive: true }`).
258
+ */
259
+ declare function emitContractSpaceArtefacts(projectMigrationsDir: string, spaceId: string, inputs: ContractSpaceArtefactInputs): Promise<void>;
260
+ //#endregion
261
+ //#region src/verify-contract-spaces.d.ts
262
+ /**
263
+ * List the per-space subdirectories under
264
+ * `<projectRoot>/migrations/`. Returns space-id directory names (sorted
265
+ * alphabetically) — i.e. any non-dot-prefixed subdirectory whose root
266
+ * does **not** contain a `migration.json` manifest. The manifest is the
267
+ * structural marker of a user-authored migration directory (see
268
+ * `readMigrationsDir` in `./io`); directory names themselves belong to
269
+ * the user and are not part of the contract.
270
+ *
271
+ * Returns `[]` if the migrations directory does not exist (greenfield
272
+ * project).
273
+ *
274
+ * Reads only the user's repo. **No descriptor import.** The caller
275
+ * (verifier) feeds the result into {@link verifyContractSpaces} alongside
276
+ * the loaded-space set and the marker rows.
277
+ */
278
+ declare function listContractSpaceDirectories(projectMigrationsDir: string): Promise<readonly string[]>;
279
+ /**
280
+ * On-disk head value (`(hash, invariants)`) for one contract space.
281
+ * The verifier compares this against the marker row for the same space
282
+ * to detect drift between the user-emitted artefacts and the live DB
283
+ * marker.
284
+ */
285
+ interface ContractSpaceHeadRecord {
286
+ readonly hash: string;
287
+ readonly invariants: readonly string[];
288
+ }
289
+ /**
290
+ * Marker row read from `prisma_contract.marker` (one per `space`).
291
+ * Caller resolves these via the family runtime's marker reader before
292
+ * invoking {@link verifyContractSpaces}.
293
+ */
294
+ interface SpaceMarkerRecord {
295
+ readonly hash: string;
296
+ readonly invariants: readonly string[];
297
+ }
298
+ interface VerifyContractSpacesInputs {
299
+ /**
300
+ * Set of contract spaces the project declares: `'app'` plus each
301
+ * extension space in `extensionPacks`. The caller's discovery path
302
+ * never reads the extension descriptor module — it walks the
303
+ * `extensionPacks` configuration in `prisma-next.config.ts` for the
304
+ * space ids.
305
+ */
306
+ readonly loadedSpaces: ReadonlySet<string>;
307
+ /**
308
+ * Per-space subdirectories observed under
309
+ * `<projectRoot>/migrations/`. Resolved via
310
+ * {@link listContractSpaceDirectories}.
311
+ */
312
+ readonly spaceDirsOnDisk: readonly string[];
313
+ /**
314
+ * Head ref per space, keyed by space id. Caller reads
315
+ * `<projectRoot>/migrations/<space-id>/contract.json` and
316
+ * `<projectRoot>/migrations/<space-id>/refs/head.json` to construct
317
+ * this map. Spaces with no contract-space dir on disk simply omit a
318
+ * map entry.
319
+ */
320
+ readonly headRefsBySpace: ReadonlyMap<string, ContractSpaceHeadRecord>;
321
+ /**
322
+ * Marker rows keyed by `space`. Caller reads them from the
323
+ * `prisma_contract.marker` table.
324
+ */
325
+ readonly markerRowsBySpace: ReadonlyMap<string, SpaceMarkerRecord>;
326
+ }
327
+ type SpaceVerifierViolation = {
328
+ readonly kind: 'declaredButUnmigrated';
329
+ readonly spaceId: string;
330
+ readonly remediation: string;
331
+ } | {
332
+ readonly kind: 'orphanMarker';
333
+ readonly spaceId: string;
334
+ readonly remediation: string;
335
+ } | {
336
+ readonly kind: 'orphanSpaceDir';
337
+ readonly spaceId: string;
338
+ readonly remediation: string;
339
+ } | {
340
+ readonly kind: 'hashMismatch';
341
+ readonly spaceId: string;
342
+ readonly priorHeadHash: string;
343
+ readonly markerHash: string;
344
+ readonly remediation: string;
345
+ } | {
346
+ readonly kind: 'invariantsMismatch';
347
+ readonly spaceId: string;
348
+ readonly onDiskInvariants: readonly string[];
349
+ readonly markerInvariants: readonly string[];
350
+ readonly remediation: string;
351
+ };
352
+ type VerifyContractSpacesResult = {
353
+ readonly ok: true;
354
+ } | {
355
+ readonly ok: false;
356
+ readonly violations: readonly SpaceVerifierViolation[];
357
+ };
358
+ /**
359
+ * Pure structural verifier for the per-space mechanism. Aggregates the
360
+ * three orphan / missing checks plus per-space hash and invariant
361
+ * comparison.
362
+ *
363
+ * Algorithm:
364
+ *
365
+ * - For every extension space declared in `loadedSpaces` (`'app'`
366
+ * excluded — the per-space verifier is scoped to extension members;
367
+ * the app is verified through the aggregate path):
368
+ * - If no contract-space dir on disk → `declaredButUnmigrated`.
369
+ * - Else if `markerRowsBySpace` lacks an entry → no violation here;
370
+ * the live-DB compare done outside this helper is where the
371
+ * absence shows up.
372
+ * - Else compare marker hash / invariants vs. on-disk head hash /
373
+ * invariants → `hashMismatch` / `invariantsMismatch` on drift.
374
+ * - For every contract-space dir on disk that is not in `loadedSpaces` →
375
+ * `orphanSpaceDir`.
376
+ * - For every marker row whose `space` is not in `loadedSpaces` →
377
+ * `orphanMarker`. The app-space marker is always loaded (`'app'` is
378
+ * in `loadedSpaces` by definition).
379
+ *
380
+ * Output is deterministic: violations are sorted first by `kind`
381
+ * (`declaredButUnmigrated` → `orphanMarker` → `orphanSpaceDir` →
382
+ * `hashMismatch` → `invariantsMismatch`) then by `spaceId`. Two callers
383
+ * passing equivalent inputs see byte-identical violation lists.
384
+ *
385
+ * Synchronous, pure, no I/O. **Does not import the extension descriptor**
386
+ * (the inputs are pre-resolved by the caller); the verifier reads only
387
+ * the user repo, not `node_modules`.
388
+ */
389
+ declare function verifyContractSpaces(inputs: VerifyContractSpacesInputs): VerifyContractSpacesResult;
390
+ //#endregion
391
+ //#region src/gather-disk-contract-space-state.d.ts
392
+ /**
393
+ * Disk-side inputs to {@link import('./verify-contract-spaces').verifyContractSpaces}
394
+ * — gathered without touching the live database. The caller composes
395
+ * this with the marker rows it reads from the runtime to invoke the
396
+ * verifier.
397
+ */
398
+ interface DiskContractSpaceState {
399
+ /** Contract-space directory names observed under `<projectMigrationsDir>/`. */
400
+ readonly spaceDirsOnDisk: readonly string[];
401
+ /** Head-ref `(hash, invariants)` per extension space. */
402
+ readonly headRefsBySpace: ReadonlyMap<string, ContractSpaceHeadRecord>;
403
+ }
404
+ /**
405
+ * Read the on-disk state the per-space verifier needs:
406
+ *
407
+ * - The list of contract-space directories under
408
+ * `<projectMigrationsDir>/` (via
409
+ * {@link import('./verify-contract-spaces').listContractSpaceDirectories}).
410
+ * - The on-disk head ref `(hash, invariants)` for each declared extension space
411
+ * (via {@link readContractSpaceHeadRef}; missing on-disk artefacts are simply
412
+ * omitted — the verifier reports them as `declaredButUnmigrated`).
413
+ *
414
+ * Synchronous in spirit but async due to filesystem reads. Reads only
415
+ * the user's repo. **Does not import any extension descriptor module.**
416
+ *
417
+ * Composition convention: pure target-agnostic primitive in
418
+ * `1-framework`; the SQL family (and any future target family) wires
419
+ * it into its `dbInit` / `verify` flows alongside its own marker-row
420
+ * read before invoking `verifyContractSpaces`.
421
+ */
422
+ declare function gatherDiskContractSpaceState(args: {
423
+ readonly projectMigrationsDir: string;
424
+ /**
425
+ * Set of space ids the project declares: `'app'` plus each entry in
426
+ * `extensionPacks` whose descriptor exposes a `contractSpace`. The
427
+ * helper reads on-disk head data only for the extension members.
428
+ */
429
+ readonly loadedSpaceIds: ReadonlySet<string>;
430
+ }): Promise<DiskContractSpaceState>;
431
+ //#endregion
432
+ //#region src/plan-all-spaces.d.ts
433
+ /**
434
+ * Per-space input for {@link planAllSpaces}. One entry per loaded
435
+ * contract space (the application's `'app'` plus each extension that
436
+ * exposes a `contractSpace`).
437
+ *
438
+ * - `priorContract` is `null` for a space that has never been emitted
439
+ * (no `migrations/<space-id>/contract.json` on disk yet); otherwise it
440
+ * is the canonical contract value emitted for that space.
441
+ * - `newContract` is the canonical contract value the planner is about
442
+ * to emit for that space — for app-space, the just-emitted root
443
+ * `contract.json`; for an extension space, the descriptor's
444
+ * `contractSpace.contractJson`.
445
+ */
446
+ interface SpacePlanInput<TContract> {
447
+ readonly spaceId: string;
448
+ readonly priorContract: TContract | null;
449
+ readonly newContract: TContract;
450
+ }
451
+ interface SpacePlanOutput<TPackage> {
452
+ readonly spaceId: string;
453
+ readonly migrationPackages: readonly TPackage[];
454
+ }
455
+ /**
456
+ * Iterate the per-space planner across a set of loaded contract spaces
457
+ * and return a deterministic shape regardless of declaration order.
458
+ *
459
+ * Behaviour:
460
+ *
461
+ * - The output is sorted alphabetically by `spaceId`. Two callers
462
+ * passing the same set of inputs in different orders observe
463
+ * byte-identical outputs.
464
+ * - The per-space planner (`planSpace`) is called exactly once per
465
+ * input, in alphabetical-by-spaceId order. Its return value is
466
+ * attached to the corresponding output entry verbatim.
467
+ * - Duplicate `spaceId`s in the input array throw
468
+ * `MIGRATION.DUPLICATE_SPACE_ID` before any `planSpace` call runs,
469
+ * keeping the planner pure when the input is malformed.
470
+ *
471
+ * The signature is generic over `TContract` and `TPackage` because the
472
+ * shape is framework-neutral (SQL family today, Mongo family
473
+ * eventually). Callers wire in whatever contract value and migration
474
+ * package shape their family already speaks.
475
+ *
476
+ * Synchronous: the underlying per-space planner (target's
477
+ * `MigrationPlanner.plan(...)`) is synchronous; callers that need to
478
+ * resolve async I/O (e.g. reading on-disk `contract.json` from disk)
479
+ * resolve it before calling `planAllSpaces` and pass the materialised
480
+ * inputs through.
481
+ */
482
+ declare function planAllSpaces<TContract, TPackage>(inputs: readonly SpacePlanInput<TContract>[], planSpace: (input: SpacePlanInput<TContract>) => readonly TPackage[]): readonly SpacePlanOutput<TPackage>[];
483
+ //#endregion
484
+ //#region src/read-contract-space-contract.d.ts
485
+ /**
486
+ * Read the on-disk contract value for a contract space
487
+ * (`<projectMigrationsDir>/<spaceId>/contract.json`). Returns the parsed
488
+ * JSON value as `unknown` — callers that need a typed contract validate
489
+ * via their family's `validateContract` to surface schema issues.
490
+ *
491
+ * Companion to {@link import('./read-contract-space-head-ref').readContractSpaceHeadRef}
492
+ * — same ENOENT-throws / corrupt-file-error semantics. Returns the
493
+ * canonical-JSON value the framework wrote during emit, so re-running
494
+ * this helper across machines / runs yields a byte-identical value.
495
+ */
496
+ declare function readContractSpaceContract(projectMigrationsDir: string, spaceId: string): Promise<unknown>;
497
+ //#endregion
498
+ //#region src/space-layout.d.ts
499
+ /**
500
+ * Branded string carrying a compile-time guarantee that the value has
501
+ * been validated by {@link assertValidSpaceId}. Downstream filesystem
502
+ * helpers (e.g. {@link spaceMigrationDirectory}) accept this type to
503
+ * make "validated" tracking visible at the type level rather than
504
+ * relying purely on a runtime check.
505
+ */
506
+ type ValidSpaceId = string & {
507
+ readonly __brand: 'ValidSpaceId';
508
+ };
509
+ declare function isValidSpaceId(spaceId: string): spaceId is ValidSpaceId;
510
+ declare function assertValidSpaceId(spaceId: string): asserts spaceId is ValidSpaceId;
511
+ /**
512
+ * Resolve the migrations subdirectory for a given contract space.
513
+ *
514
+ * Every contract space — including the app space (default `'app'`) —
515
+ * lands under `<projectMigrationsDir>/<spaceId>/`. The space id is
516
+ * validated against {@link SPACE_ID_PATTERN} because it becomes a
517
+ * filesystem directory name verbatim.
518
+ *
519
+ * `projectMigrationsDir` is the project's top-level `migrations/`
520
+ * directory; the helper does not assume anything about its absolute /
521
+ * relative shape and is symmetric with `pathe.join`.
522
+ */
523
+ declare function spaceMigrationDirectory(projectMigrationsDir: string, spaceId: string): string;
524
+ //#endregion
525
+ export { APP_SPACE_ID, type ComputeExtensionSpaceApplyPathInputs, type ContractSpaceArtefactInputs, type ContractSpaceHeadRecord, type ContractSpaceHeadRef, type DescriptorSelfConsistencyInputs, type DiskContractSpaceState, type ExtensionSpaceApplyPathOutcome, type SpaceApplyInput, type SpaceMarkerRecord, type SpacePlanInput, type SpacePlanOutput, type SpaceVerifierViolation, type ValidSpaceId, type VerifyContractSpacesInputs, type VerifyContractSpacesResult, assertDescriptorSelfConsistency, assertValidSpaceId, computeExtensionSpaceApplyPath, contractSpaceFromJson, emitContractSpaceArtefacts, gatherDiskContractSpaceState, isValidSpaceId, listContractSpaceDirectories, planAllSpaces, readContractSpaceContract, readContractSpaceHeadRef, spaceMigrationDirectory, verifyContractSpaces };
526
+ //# 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/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,SAS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