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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/dist/{errors-5KVuWV_5.mjs → errors-EPL_9p9f.mjs} +12 -6
  2. package/dist/errors-EPL_9p9f.mjs.map +1 -0
  3. package/dist/exports/aggregate.d.mts +534 -0
  4. package/dist/exports/aggregate.d.mts.map +1 -0
  5. package/dist/exports/aggregate.mjs +598 -0
  6. package/dist/exports/aggregate.mjs.map +1 -0
  7. package/dist/exports/errors.d.mts +6 -1
  8. package/dist/exports/errors.d.mts.map +1 -1
  9. package/dist/exports/errors.mjs +2 -2
  10. package/dist/exports/graph.d.mts +1 -1
  11. package/dist/exports/hash.d.mts +1 -1
  12. package/dist/exports/invariants.d.mts +13 -2
  13. package/dist/exports/invariants.d.mts.map +1 -1
  14. package/dist/exports/invariants.mjs +1 -1
  15. package/dist/exports/io.d.mts +25 -1
  16. package/dist/exports/io.d.mts.map +1 -1
  17. package/dist/exports/io.mjs +2 -2
  18. package/dist/exports/metadata.d.mts +1 -1
  19. package/dist/exports/migration-graph.d.mts +1 -1
  20. package/dist/exports/migration-graph.mjs +1 -522
  21. package/dist/exports/migration.d.mts +1 -1
  22. package/dist/exports/migration.mjs +2 -2
  23. package/dist/exports/refs.mjs +1 -1
  24. package/dist/exports/spaces.d.mts +341 -237
  25. package/dist/exports/spaces.d.mts.map +1 -1
  26. package/dist/exports/spaces.mjs +137 -339
  27. package/dist/exports/spaces.mjs.map +1 -1
  28. package/dist/{graph-4dIUm90i.d.mts → graph-HMWAldoR.d.mts} +1 -1
  29. package/dist/{graph-4dIUm90i.d.mts.map → graph-HMWAldoR.d.mts.map} +1 -1
  30. package/dist/{invariants-CkLSBcMu.mjs → invariants-Duc8f9NM.mjs} +16 -5
  31. package/dist/invariants-Duc8f9NM.mjs.map +1 -0
  32. package/dist/{io-TX8RPDeh.mjs → io-D13dLvUh.mjs} +38 -4
  33. package/dist/io-D13dLvUh.mjs.map +1 -0
  34. package/dist/migration-graph-DGNnKDY5.mjs +523 -0
  35. package/dist/{exports/migration-graph.mjs.map → migration-graph-DGNnKDY5.mjs.map} +1 -1
  36. package/dist/read-contract-space-contract-C3-1eyaI.mjs +298 -0
  37. package/dist/read-contract-space-contract-C3-1eyaI.mjs.map +1 -0
  38. package/package.json +10 -6
  39. package/src/aggregate/loader.ts +409 -0
  40. package/src/aggregate/marker-types.ts +16 -0
  41. package/src/aggregate/planner-types.ts +137 -0
  42. package/src/aggregate/planner.ts +158 -0
  43. package/src/aggregate/project-schema-to-space.ts +64 -0
  44. package/src/aggregate/strategies/graph-walk.ts +92 -0
  45. package/src/aggregate/strategies/synth.ts +122 -0
  46. package/src/aggregate/types.ts +89 -0
  47. package/src/aggregate/verifier.ts +230 -0
  48. package/src/assert-descriptor-self-consistency.ts +70 -0
  49. package/src/compute-extension-space-apply-path.ts +152 -0
  50. package/src/concatenate-space-apply-inputs.ts +2 -2
  51. package/src/detect-space-contract-drift.ts +22 -26
  52. package/src/{emit-pinned-space-artefacts.ts → emit-contract-space-artefacts.ts} +14 -33
  53. package/src/errors.ts +11 -5
  54. package/src/exports/aggregate.ts +37 -0
  55. package/src/exports/errors.ts +1 -0
  56. package/src/exports/io.ts +1 -0
  57. package/src/exports/spaces.ts +23 -10
  58. package/src/gather-disk-contract-space-state.ts +62 -0
  59. package/src/invariants.ts +14 -3
  60. package/src/io.ts +42 -0
  61. package/src/plan-all-spaces.ts +3 -7
  62. package/src/read-contract-space-contract.ts +44 -0
  63. package/src/read-contract-space-head-ref.ts +63 -0
  64. package/src/space-layout.ts +4 -11
  65. package/src/verify-contract-spaces.ts +45 -49
  66. package/dist/errors-5KVuWV_5.mjs.map +0 -1
  67. package/dist/invariants-CkLSBcMu.mjs.map +0 -1
  68. package/dist/io-TX8RPDeh.mjs.map +0 -1
  69. package/src/read-pinned-contract-hash.ts +0 -77
  70. /package/dist/{metadata-th_MvOTT.d.mts → metadata-BnLFiI6B.d.mts} +0 -0
@@ -1 +1 @@
1
- {"version":3,"file":"spaces.d.mts","names":[],"sources":["../../src/concatenate-space-apply-inputs.ts","../../src/detect-space-contract-drift.ts","../../src/emit-pinned-space-artefacts.ts","../../src/plan-all-spaces.ts","../../src/read-pinned-contract-hash.ts","../../src/space-layout.ts","../../src/verify-contract-spaces.ts"],"mappings":";;;;;;AAuBA;;;;;;;;;;;;;AAsCA;;;;UAtCiB,eAAA;EAAA,SACN,OAAA;EAAA,SACA,kBAAA;EAAA,SACA,iBAAA;EAAA,SACA,uBAAA;EAAA,SACA,IAAA,WAAe,GAAA;AAAA;;;;;;;;;;ACX1B;;;;;AAyBA;;;;;;;;;;AA0BA;;;;;;iBDPgB,2BAAA,KAAA,CACd,MAAA,WAAiB,eAAA,CAAgB,GAAA,eACvB,eAAA,CAAgB,GAAA;;;;;;AAxC5B;;;;;;;;;;;;;AAsCA;UC5CiB,8BAAA;EAAA,SACN,cAAA;EAAA,SACA,UAAA;AAAA;;;;;;;;;;;;;;;;AAFX;;;;;KAyBY,wBAAA;EAAA,SACD,IAAA;EAAA,SACA,OAAA;EAAA,SACA,cAAA;EAAA,SACA,UAAA;AAAA;;;;;AAsBX;;;;;;;;;;;;;ACzDA;;iBDyDgB,wBAAA,CACd,OAAA,UACA,MAAA,EAAQ,8BAAA,GACP,wBAAA;;;;;;ADhDH;;UEZiB,kBAAA;EAAA,SACN,IAAA;EAAA,SACA,UAAA;AAAA;;;;;;;;AFgDX;;;;;;;;;;;;;UEzBiB,yBAAA;EAAA,SACN,QAAA;EAAA,SACA,WAAA;EAAA,SACA,OAAA,EAAS,kBAAA;AAAA;;;ADtBpB;;;;;AAyBA;;;;;;;;;;AA0BA;;;;;;;iBCFsB,wBAAA,CACpB,oBAAA,UACA,OAAA,UACA,MAAA,EAAQ,yBAAA,GACP,OAAA;;;;;;AF/CH;;;;;;;;;;;;UGNiB,cAAA;EAAA,SACN,OAAA;EAAA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SAGA,IAAA;EAAA,SACA,OAAA;EAAA,SACA,WAAA;AAAA;EAAA,SAGA,IAAA;EAAA,SACA,OAAA;EAAA,SACA,UAAA;EAAA,SACA,UAAA;EAAA,SACA,WAAA;AAAA;EAAA,SAGA,IAAA;EAAA,SACA,OAAA;EAAA,SACA,gBAAA;EAAA,SACA,gBAAA;EAAA,SACA,WAAA;AAAA;AAAA,KAGH,0BAAA;EAAA,SACG,EAAA;AAAA;EAAA,SACA,EAAA;EAAA,SAAoB,UAAA,WAAqB,sBAAA;AAAA;;;;;;;;AHhIxD;;;;;;;;;;AAkCA;;;;;;;;;;;;;;;;iBGiIgB,oBAAA,CACd,MAAA,EAAQ,0BAAA,GACP,0BAAA"}
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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ACf,WAAA,EAAa,SAAA;AAAA;AAAA,UAGP,eAAA;EAAA,SACN,OAAA;EAAA,SACA,iBAAA,WAA4B,QAAA;AAAA;;APEvC;;;;;;;;;;;;;ACTA;;;;;;;;;;;;;iBMqCgB,aAAA,qBAAA,CACd,MAAA,WAAiB,cAAA,CAAe,SAAA,KAChC,SAAA,GAAY,KAAA,EAAO,cAAA,CAAe,SAAA,eAAwB,QAAA,cAChD,eAAA,CAAgB,QAAA;;;;;;;AR/C5B;;;;;;;iBSWsB,yBAAA,CACpB,oBAAA,UACA,OAAA,WACC,OAAA;;;;;ATdH;;;;;KUIY,YAAA;EAAA,SAAmC,OAAA;AAAA;AAAA,iBAS/B,cAAA,CAAe,OAAA,WAAkB,OAAA,IAAW,YAAA;AAAA,iBAI5C,kBAAA,CAAmB,OAAA,mBAA0B,OAAA,IAAW,YAAA;;AViBxE;;;;;;;;AClBA;;;iBSmBgB,uBAAA,CAAwB,oBAAA,UAA8B,OAAA"}
@@ -1,137 +1,115 @@
1
- import { S as errorPinnedArtefactsAppSpace, f as errorInvalidRefFile, g as errorInvalidSpaceId, l as errorInvalidJson, o as errorDuplicateSpaceId } from "../errors-5KVuWV_5.mjs";
1
+ import { r as errorDescriptorHeadHashMismatch, s as errorDuplicateSpaceId } from "../errors-EPL_9p9f.mjs";
2
2
  import { r as canonicalizeJson } from "../hash-By50zM_E.mjs";
3
- import { t as MANIFEST_FILE } from "../io-TX8RPDeh.mjs";
3
+ import { s as readMigrationsDir } from "../io-D13dLvUh.mjs";
4
+ import "../constants-DWV9_o2Z.mjs";
5
+ import { l as reconstructGraph, o as findPathWithDecision } from "../migration-graph-DGNnKDY5.mjs";
6
+ import { a as readContractSpaceHeadRef, c as isValidSpaceId, i as detectSpaceContractDrift, l as spaceMigrationDirectory, n as listContractSpaceDirectories, o as APP_SPACE_ID, r as verifyContractSpaces, s as assertValidSpaceId, t as readContractSpaceContract } from "../read-contract-space-contract-C3-1eyaI.mjs";
4
7
  import { join } from "pathe";
5
- import { mkdir, readFile, readdir, stat, writeFile } from "node:fs/promises";
6
- import { APP_SPACE_ID } from "@prisma-next/framework-components/control";
7
- //#region src/space-layout.ts
8
+ import { mkdir, writeFile } from "node:fs/promises";
9
+ import { computeStorageHash } from "@prisma-next/contract/hashing";
10
+ //#region src/assert-descriptor-self-consistency.ts
8
11
  /**
9
- * Pattern a contract-space identifier must match. The constraint is
10
- * filesystem-friendly: lowercase letters / digits / hyphen / underscore,
11
- * starts with a letter, max 64 characters.
12
- *
13
- * @see specs/framework-mechanism.spec.md § 3.
12
+ * Assert that an extension descriptor is self-consistent: the
13
+ * `headRef.hash` it publishes must match the canonical hash recomputed
14
+ * from its `contractSpace.contractJson`.
15
+ *
16
+ * Recomputes via {@link computeStorageHash} the same canonical-JSON
17
+ * pipeline the descriptor's own emit pipeline produced the hash with —
18
+ * over `(target, targetFamily, storage)`. Mismatch indicates the
19
+ * extension author bumped `contractJson` without rerunning emit, leaving
20
+ * the descriptor's `headRef.hash` stale; the consumer-side helpers
21
+ * (drift detection, on-disk artefact emission, runner marker writes) all
22
+ * trust `headRef.hash` as the canonical identity, so a stale value would
23
+ * silently corrupt every downstream boundary.
24
+ *
25
+ * Synchronous, pure, no I/O. Throws
26
+ * `MIGRATION.DESCRIPTOR_HEAD_HASH_MISMATCH` on failure with both the
27
+ * recomputed and published hashes in `details` so callers can surface a
28
+ * clear remediation hint without re-deriving them.
14
29
  */
15
- const SPACE_ID_PATTERN = /^[a-z][a-z0-9_-]{0,63}$/;
16
- function isValidSpaceId(spaceId) {
17
- return SPACE_ID_PATTERN.test(spaceId);
18
- }
19
- function assertValidSpaceId(spaceId) {
20
- if (!isValidSpaceId(spaceId)) throw errorInvalidSpaceId(spaceId);
21
- }
22
- /**
23
- * Resolve the migrations subdirectory for a given contract space.
24
- *
25
- * - **App space** (`spaceId === APP_SPACE_ID`) keeps today's layout: the
26
- * project's `migrations/` directory is the migrations directory, no
27
- * subdirectory.
28
- * - **Extension space** lands under `<projectMigrationsDir>/<spaceId>/`.
29
- * The space id is validated against {@link SPACE_ID_PATTERN} because
30
- * it becomes a filesystem directory name verbatim.
31
- *
32
- * `projectMigrationsDir` is the project's top-level `migrations/`
33
- * directory; the helper does not assume anything about its absolute /
34
- * relative shape and is symmetric with `pathe.join`.
35
- */
36
- function spaceMigrationDirectory(projectMigrationsDir, spaceId) {
37
- if (spaceId === APP_SPACE_ID) return projectMigrationsDir;
38
- assertValidSpaceId(spaceId);
39
- return join(projectMigrationsDir, spaceId);
40
- }
41
- //#endregion
42
- //#region src/concatenate-space-apply-inputs.ts
43
- /**
44
- * Order a set of per-space apply inputs into the canonical cross-space
45
- * sequence the runner applies under a single transaction.
46
- *
47
- * Cross-space ordering convention (sub-spec § 4):
48
- *
49
- * 1. **Extension spaces first**, alphabetically by `spaceId`.
50
- * 2. **App space last** — only one `'app'` entry expected, at most.
51
- *
52
- * Rationale: extensions install their own structural objects (types,
53
- * functions, helper tables) before the app's structural ops reference
54
- * them. Putting app-space last lets app-space ops freely depend on any
55
- * extension-space declaration in the same transaction.
56
- *
57
- * Determinism (NFR6): the output order is independent of the input
58
- * order, so two callers with the same set of `extensionPacks` produce
59
- * identical apply sequences.
60
- *
61
- * Atomicity: rejects duplicate `spaceId`s with
62
- * `MIGRATION.DUPLICATE_SPACE_ID` before producing any output. This
63
- * mirrors {@link import('./plan-all-spaces').planAllSpaces} so the
64
- * planner-side and runner-side helpers reject malformed inputs the same
65
- * way (callers don't need a separate dedup pass).
66
- *
67
- * Synchronous, pure, no I/O: callers resolve marker rows and `path`
68
- * before invoking this helper. The actual DB application — driving the
69
- * transaction, committing marker writes, recording the per-space marker
70
- * rows — happens at the SQL-family consumption site (per the
71
- * helper-location convention from R3).
72
- */
73
- function concatenateSpaceApplyInputs(inputs) {
74
- const seen = /* @__PURE__ */ new Set();
75
- for (const input of inputs) {
76
- if (seen.has(input.spaceId)) throw errorDuplicateSpaceId(input.spaceId);
77
- seen.add(input.spaceId);
78
- }
79
- const extensions = [];
80
- let appSpace;
81
- for (const input of inputs) if (input.spaceId === APP_SPACE_ID) appSpace = input;
82
- else extensions.push(input);
83
- extensions.sort((a, b) => {
84
- if (a.spaceId < b.spaceId) return -1;
85
- if (a.spaceId > b.spaceId) return 1;
86
- return 0;
30
+ function assertDescriptorSelfConsistency(inputs) {
31
+ const { storageHash: _stripped, ...storageWithoutHash } = inputs.storage;
32
+ const recomputed = computeStorageHash({
33
+ target: inputs.target,
34
+ targetFamily: inputs.targetFamily,
35
+ storage: storageWithoutHash
36
+ });
37
+ if (recomputed !== inputs.headRefHash) throw errorDescriptorHeadHashMismatch({
38
+ extensionId: inputs.extensionId,
39
+ recomputedHash: recomputed,
40
+ headRefHash: inputs.headRefHash
87
41
  });
88
- return appSpace ? [...extensions, appSpace] : extensions;
89
42
  }
90
43
  //#endregion
91
- //#region src/detect-space-contract-drift.ts
44
+ //#region src/compute-extension-space-apply-path.ts
92
45
  /**
93
- * Pure drift-detection primitive for a single contract space.
94
- *
95
- * Runs once per loaded extension space, just before computing the
96
- * `priorContract` that feeds {@link import('./plan-all-spaces').planAllSpaces}.
97
- * Hash equality is byte-for-byte (no normalisation) — both sides are
98
- * already canonical hashes produced by the same pipeline, so any
99
- * difference is meaningful drift.
46
+ * Compute the apply path for an extension contract space — the shortest
47
+ * sequence of on-disk migration packages that walks the live marker
48
+ * forward to the on-disk head ref hash, covering every required
49
+ * invariant.
100
50
  *
101
- * Synchronous, pure, no I/O. The caller (SQL family in M2 R1) reads
102
- * the pinned `contract.json` and computes its hash, then invokes this
103
- * helper alongside the descriptor's `headRef.hash`. Composes naturally
104
- * with {@link import('./read-pinned-contract-hash').readPinnedContractHash}
105
- * which provides the read-side primitive.
51
+ * Reads only on-disk artefacts (`migrations/<spaceId>/refs/head.json`
52
+ * and the per-space migration packages). **Does not import any
53
+ * extension descriptor module** `db init` / `db update` must remain
54
+ * runnable without the descriptor source on disk.
106
55
  *
107
- * @see specs/framework-mechanism.spec.md § 3 — Drift detection (T1.9).
108
- * @see specs/framework-mechanism.spec.md AM7 drift warning surfaces
109
- * the extension name and the diff direction.
56
+ * Behaviour:
57
+ * - Returns `{ kind: 'ok', pathOps: [], … }` when the marker is already
58
+ * at the recorded head and no required invariants are missing.
59
+ * - Returns `{ kind: 'unreachable' }` when the marker hash is not
60
+ * structurally connected to the recorded head in the graph.
61
+ * - Returns `{ kind: 'unsatisfiable', missing, … }` when the marker is
62
+ * reachable but no path covers the required invariants.
63
+ * - Returns `{ kind: 'contractSpaceHeadRefMissing' }` when the per-space
64
+ * `refs/head.json` is absent — the precheck verifier should already
65
+ * have rejected this case, but the helper is defensive so callers can
66
+ * surface a coherent error rather than throw.
110
67
  */
111
- function detectSpaceContractDrift(spaceId, inputs) {
112
- if (inputs.pinnedHash === null) return {
113
- kind: "firstEmit",
114
- spaceId,
115
- descriptorHash: inputs.descriptorHash,
116
- pinnedHash: null
68
+ async function computeExtensionSpaceApplyPath(inputs) {
69
+ const { projectMigrationsDir, spaceId, currentMarkerHash, currentMarkerInvariants } = inputs;
70
+ const contractSpaceHeadRef = await readContractSpaceHeadRef(projectMigrationsDir, spaceId);
71
+ if (contractSpaceHeadRef === null) return { kind: "contractSpaceHeadRefMissing" };
72
+ const packages = await readMigrationsDir(spaceMigrationDirectory(projectMigrationsDir, spaceId));
73
+ const graph = reconstructGraph(packages);
74
+ const fromHash = currentMarkerHash ?? "sha256:empty";
75
+ const required = new Set(contractSpaceHeadRef.invariants.filter((id) => !currentMarkerInvariants.includes(id)));
76
+ const outcome = findPathWithDecision(graph, fromHash, contractSpaceHeadRef.hash, { required });
77
+ if (outcome.kind === "unreachable") return {
78
+ kind: "unreachable",
79
+ contractSpaceHeadRef
117
80
  };
118
- if (inputs.descriptorHash === inputs.pinnedHash) return {
119
- kind: "noDrift",
120
- spaceId,
121
- descriptorHash: inputs.descriptorHash,
122
- pinnedHash: inputs.pinnedHash
81
+ if (outcome.kind === "unsatisfiable") return {
82
+ kind: "unsatisfiable",
83
+ contractSpaceHeadRef,
84
+ missing: outcome.missing,
85
+ structuralPath: outcome.structuralPath.map(({ dirName, to }) => ({
86
+ dirName,
87
+ to
88
+ }))
123
89
  };
90
+ const packagesByHash = new Map(packages.map((pkg) => [pkg.metadata.migrationHash, pkg]));
91
+ const pathOps = [];
92
+ const walkedMigrationDirs = [];
93
+ const providedInvariantsSet = /* @__PURE__ */ new Set();
94
+ for (const edge of outcome.decision.selectedPath) {
95
+ const pkg = packagesByHash.get(edge.migrationHash);
96
+ if (!pkg) throw new Error(`Migration package missing for edge ${edge.migrationHash} in space "${spaceId}"`);
97
+ walkedMigrationDirs.push(pkg.dirName);
98
+ for (const op of pkg.ops) pathOps.push(op);
99
+ for (const invariant of pkg.metadata.providedInvariants) providedInvariantsSet.add(invariant);
100
+ }
124
101
  return {
125
- kind: "drift",
126
- spaceId,
127
- descriptorHash: inputs.descriptorHash,
128
- pinnedHash: inputs.pinnedHash
102
+ kind: "ok",
103
+ contractSpaceHeadRef,
104
+ providedInvariants: [...providedInvariantsSet].sort(),
105
+ pathOps,
106
+ walkedMigrationDirs
129
107
  };
130
108
  }
131
109
  //#endregion
132
- //#region src/emit-pinned-space-artefacts.ts
110
+ //#region src/emit-contract-space-artefacts.ts
133
111
  /**
134
- * Emit the pinned per-space artefacts (`contract.json`, `contract.d.ts`,
112
+ * Emit the per-space artefacts (`contract.json`, `contract.d.ts`,
135
113
  * `refs/head.json`) under `<projectMigrationsDir>/<spaceId>/`.
136
114
  *
137
115
  * Always-overwrite: the framework owns these files; running `migrate`
@@ -139,23 +117,16 @@ function detectSpaceContractDrift(spaceId, inputs) {
139
117
  * helper does not check pre-existing contents — re-emit always wins.
140
118
  *
141
119
  * Path layout matches the convention in
142
- * [`spaceMigrationDirectory`](./space-layout.ts), with two restrictions
143
- * specific to pinned artefacts:
144
- *
145
- * - Rejects the app space (`spaceId === APP_SPACE_ID`): the app space's
146
- * canonical `contract.json` lives at the project root, not under
147
- * `migrations/`. Callers that want to emit it use the app-space
148
- * contract emit pipeline.
149
- * - Validates `spaceId` against `[a-z][a-z0-9_-]{0,63}` via
150
- * {@link assertValidSpaceId} for the same filesystem-safety reasons.
120
+ * [`spaceMigrationDirectory`](./space-layout.ts). The space id is
121
+ * validated against `[a-z][a-z0-9_-]{0,63}` via
122
+ * {@link assertValidSpaceId} for filesystem-safety reasons; the helper
123
+ * accepts every space uniformly (including the app space, default
124
+ * `'app'`).
151
125
  *
152
126
  * The migrations directory and space subdirectory are created if they
153
127
  * do not yet exist (`mkdir { recursive: true }`).
154
- *
155
- * @see specs/framework-mechanism.spec.md § 3 — Pinned artefact emission (T1.8).
156
128
  */
157
- async function emitPinnedSpaceArtefacts(projectMigrationsDir, spaceId, inputs) {
158
- if (spaceId === APP_SPACE_ID) throw errorPinnedArtefactsAppSpace();
129
+ async function emitContractSpaceArtefacts(projectMigrationsDir, spaceId, inputs) {
159
130
  assertValidSpaceId(spaceId);
160
131
  const dir = join(projectMigrationsDir, spaceId);
161
132
  await mkdir(join(dir, "refs"), { recursive: true });
@@ -169,6 +140,40 @@ async function emitPinnedSpaceArtefacts(projectMigrationsDir, spaceId, inputs) {
169
140
  await writeFile(join(dir, "refs", "head.json"), `${headJson}\n`);
170
141
  }
171
142
  //#endregion
143
+ //#region src/gather-disk-contract-space-state.ts
144
+ /**
145
+ * Read the on-disk state the per-space verifier needs:
146
+ *
147
+ * - The list of contract-space directories under
148
+ * `<projectMigrationsDir>/` (via
149
+ * {@link import('./verify-contract-spaces').listContractSpaceDirectories}).
150
+ * - The on-disk head ref `(hash, invariants)` for each declared extension space
151
+ * (via {@link readContractSpaceHeadRef}; missing on-disk artefacts are simply
152
+ * omitted — the verifier reports them as `declaredButUnmigrated`).
153
+ *
154
+ * Synchronous in spirit but async due to filesystem reads. Reads only
155
+ * the user's repo. **Does not import any extension descriptor module.**
156
+ *
157
+ * Composition convention: pure target-agnostic primitive in
158
+ * `1-framework`; the SQL family (and any future target family) wires
159
+ * it into its `dbInit` / `verify` flows alongside its own marker-row
160
+ * read before invoking `verifyContractSpaces`.
161
+ */
162
+ async function gatherDiskContractSpaceState(args) {
163
+ const { projectMigrationsDir, loadedSpaceIds } = args;
164
+ const spaceDirsOnDisk = await listContractSpaceDirectories(projectMigrationsDir);
165
+ const headRefsBySpace = /* @__PURE__ */ new Map();
166
+ for (const spaceId of loadedSpaceIds) {
167
+ if (spaceId === APP_SPACE_ID) continue;
168
+ const head = await readContractSpaceHeadRef(projectMigrationsDir, spaceId);
169
+ if (head !== null) headRefsBySpace.set(spaceId, head);
170
+ }
171
+ return {
172
+ spaceDirsOnDisk,
173
+ headRefsBySpace
174
+ };
175
+ }
176
+ //#endregion
172
177
  //#region src/plan-all-spaces.ts
173
178
  /**
174
179
  * Iterate the per-space planner across a set of loaded contract spaces
@@ -176,7 +181,7 @@ async function emitPinnedSpaceArtefacts(projectMigrationsDir, spaceId, inputs) {
176
181
  *
177
182
  * Behaviour:
178
183
  *
179
- * - The output is sorted alphabetically by `spaceId` (AM3). Two callers
184
+ * - The output is sorted alphabetically by `spaceId`. Two callers
180
185
  * passing the same set of inputs in different orders observe
181
186
  * byte-identical outputs.
182
187
  * - The per-space planner (`planSpace`) is called exactly once per
@@ -193,11 +198,9 @@ async function emitPinnedSpaceArtefacts(projectMigrationsDir, spaceId, inputs) {
193
198
  *
194
199
  * Synchronous: the underlying per-space planner (target's
195
200
  * `MigrationPlanner.plan(...)`) is synchronous; callers that need to
196
- * resolve async I/O (e.g. reading pinned `contract.json` from disk)
201
+ * resolve async I/O (e.g. reading on-disk `contract.json` from disk)
197
202
  * resolve it before calling `planAllSpaces` and pass the materialised
198
203
  * inputs through.
199
- *
200
- * @see specs/framework-mechanism.spec.md § 3 — Per-space planner (T1.3).
201
204
  */
202
205
  function planAllSpaces(inputs, planSpace) {
203
206
  const seen = /* @__PURE__ */ new Set();
@@ -215,211 +218,6 @@ function planAllSpaces(inputs, planSpace) {
215
218
  }));
216
219
  }
217
220
  //#endregion
218
- //#region src/read-pinned-contract-hash.ts
219
- function hasErrnoCode$1(error, code) {
220
- return error instanceof Error && error.code === code;
221
- }
222
- /**
223
- * Read the pinned head hash for an extension space.
224
- *
225
- * Returns the `hash` field of `<projectMigrationsDir>/<spaceId>/refs/head.json`
226
- * — i.e. the canonical contract hash the framework wrote on the last
227
- * `migrate` for this space. Returns `null` when the file does not exist
228
- * (or the migrations directory is missing entirely), which is the
229
- * "first emit" signal {@link import('./detect-space-contract-drift').detectSpaceContractDrift}
230
- * uses to distinguish a brand-new extension from drift.
231
- *
232
- * Pure I/O (read + parse). The "comparison hash" is stored on disk by
233
- * {@link import('./emit-pinned-space-artefacts').emitPinnedSpaceArtefacts}
234
- * via the descriptor's `headRef.hash`, so reading it back here matches
235
- * the descriptor's hashing pipeline by construction — neither side
236
- * recomputes anything.
237
- *
238
- * Validation:
239
- *
240
- * - Rejects the app space — pinned head refs are an extension-space
241
- * concept; the app space's contract-of-record lives at the project
242
- * root, not under `migrations/`.
243
- * - Validates the space id against the same `[a-z][a-z0-9_-]{0,63}`
244
- * pattern as the rest of the per-space helpers.
245
- * - Surfaces `MIGRATION.INVALID_JSON` / `MIGRATION.INVALID_REF_FILE`
246
- * on a corrupt `refs/head.json` so callers can distinguish "no
247
- * pinned file" (returns `null`) from "pinned file but unreadable"
248
- * (throws).
249
- *
250
- * @see specs/framework-mechanism.spec.md § 3 — Drift detection (T1.9).
251
- */
252
- async function readPinnedContractHash(projectMigrationsDir, spaceId) {
253
- if (spaceId === APP_SPACE_ID) throw errorPinnedArtefactsAppSpace();
254
- assertValidSpaceId(spaceId);
255
- const filePath = join(projectMigrationsDir, spaceId, "refs", "head.json");
256
- let raw;
257
- try {
258
- raw = await readFile(filePath, "utf-8");
259
- } catch (error) {
260
- if (hasErrnoCode$1(error, "ENOENT")) return null;
261
- throw error;
262
- }
263
- let parsed;
264
- try {
265
- parsed = JSON.parse(raw);
266
- } catch (e) {
267
- throw errorInvalidJson(filePath, e instanceof Error ? e.message : String(e));
268
- }
269
- if (typeof parsed !== "object" || parsed === null || typeof parsed.hash !== "string") throw errorInvalidRefFile(filePath, "expected an object with a string `hash` field");
270
- return parsed.hash;
271
- }
272
- //#endregion
273
- //#region src/verify-contract-spaces.ts
274
- function hasErrnoCode(error, code) {
275
- return error instanceof Error && error.code === code;
276
- }
277
- /**
278
- * List the per-space pinned subdirectories under
279
- * `<projectRoot>/migrations/`. Returns space-id directory names (sorted
280
- * alphabetically) — i.e. any non-dot-prefixed subdirectory whose root
281
- * does **not** contain a `migration.json` manifest. The manifest is the
282
- * structural marker of a user-authored migration directory (see
283
- * `readMigrationsDir` in `./io`); directory names themselves belong to
284
- * the user and are not part of the contract.
285
- *
286
- * Returns `[]` if the migrations directory does not exist (greenfield
287
- * project).
288
- *
289
- * Reads only the user's repo. **No descriptor import.** The caller
290
- * (verifier) feeds the result into {@link verifyContractSpaces} alongside
291
- * the loaded-space set and the marker rows.
292
- *
293
- * @see specs/framework-mechanism.spec.md § 4 — Verifier (steps 5–6).
294
- */
295
- async function listPinnedSpaceDirectories(projectMigrationsDir) {
296
- let entries;
297
- try {
298
- entries = (await readdir(projectMigrationsDir, { withFileTypes: true })).map((d) => ({
299
- name: d.name,
300
- isDirectory: d.isDirectory()
301
- }));
302
- } catch (error) {
303
- if (hasErrnoCode(error, "ENOENT")) return [];
304
- throw error;
305
- }
306
- const namedCandidates = entries.filter((e) => e.isDirectory).map((e) => e.name).filter((name) => !name.startsWith(".")).sort();
307
- return (await Promise.all(namedCandidates.map(async (name) => {
308
- try {
309
- await stat(join(projectMigrationsDir, name, MANIFEST_FILE));
310
- return {
311
- name,
312
- isMigrationDir: true
313
- };
314
- } catch (error) {
315
- if (hasErrnoCode(error, "ENOENT")) return {
316
- name,
317
- isMigrationDir: false
318
- };
319
- throw error;
320
- }
321
- }))).filter((c) => !c.isMigrationDir).map((c) => c.name);
322
- }
323
- /**
324
- * Pure structural verifier for the per-space mechanism. Aggregates the
325
- * three orphan / missing checks (FR6 cases a–c) plus per-space hash and
326
- * invariant comparison.
327
- *
328
- * Algorithm (sub-spec § 4):
329
- *
330
- * - For every extension space declared in `loadedSpaces` (`'app'`
331
- * excluded — its pinned `contract.json` lives at the project root):
332
- * - If no pinned dir on disk → `declaredButUnmigrated`.
333
- * - Else if `markerRowsBySpace` lacks an entry → no violation here;
334
- * the live-DB compare in step 8 (out of scope of this helper) is
335
- * where the absence shows up.
336
- * - Else compare marker hash / invariants vs. pinned hash /
337
- * invariants → `hashMismatch` / `invariantsMismatch` on drift.
338
- * - For every pinned dir on disk that is not in `loadedSpaces` →
339
- * `orphanPinnedDir`.
340
- * - For every marker row whose `space` is not in `loadedSpaces` →
341
- * `orphanMarker`. The app-space marker is always loaded (`'app'` is
342
- * in `loadedSpaces` by definition).
343
- *
344
- * Output is deterministic (NFR6): violations are sorted first by `kind`
345
- * (`declaredButUnmigrated` → `orphanMarker` → `orphanPinnedDir` →
346
- * `hashMismatch` → `invariantsMismatch`) then by `spaceId`. Two callers
347
- * passing equivalent inputs see byte-identical violation lists.
348
- *
349
- * Synchronous, pure, no I/O. **Does not import the extension descriptor**
350
- * (the inputs are pre-resolved by the caller). This is the property
351
- * AC-15 / AC-26 ("verifier reads only the user repo, not
352
- * `node_modules`") locks in.
353
- *
354
- * @see specs/framework-mechanism.spec.md § 4 — Verifier (T1.5).
355
- */
356
- function verifyContractSpaces(inputs) {
357
- const violations = [];
358
- for (const spaceId of [...inputs.loadedSpaces].sort()) {
359
- if (spaceId === APP_SPACE_ID) continue;
360
- if (!inputs.pinnedDirsOnDisk.includes(spaceId)) {
361
- violations.push({
362
- kind: "declaredButUnmigrated",
363
- spaceId,
364
- remediation: `Extension '${spaceId}' is declared in extensionPacks but has not been emitted; run \`prisma-next migrate\`.`
365
- });
366
- continue;
367
- }
368
- const pinned = inputs.pinnedHashesBySpace.get(spaceId);
369
- const marker = inputs.markerRowsBySpace.get(spaceId);
370
- if (!pinned || !marker) continue;
371
- if (pinned.hash !== marker.hash) {
372
- violations.push({
373
- kind: "hashMismatch",
374
- spaceId,
375
- pinnedHash: pinned.hash,
376
- markerHash: marker.hash,
377
- remediation: `Marker row for space '${spaceId}' is keyed at ${marker.hash}, but the pinned ${join("migrations", spaceId, "contract.json")} resolves to ${pinned.hash}. Run \`prisma-next db update\` to advance the database, or \`prisma-next migrate\` if the descriptor was bumped without re-emitting.`
378
- });
379
- continue;
380
- }
381
- const pinnedInvariants = [...pinned.invariants].sort();
382
- const markerInvariants = new Set(marker.invariants);
383
- const missing = pinnedInvariants.filter((id) => !markerInvariants.has(id));
384
- if (missing.length > 0) violations.push({
385
- kind: "invariantsMismatch",
386
- spaceId,
387
- pinnedInvariants,
388
- markerInvariants: [...marker.invariants].sort(),
389
- remediation: `Marker row for space '${spaceId}' is missing invariants [${missing.map((s) => JSON.stringify(s)).join(", ")}]. Run \`prisma-next db update\` to apply the corresponding data-transform migrations.`
390
- });
391
- }
392
- for (const dir of [...inputs.pinnedDirsOnDisk].sort()) if (!inputs.loadedSpaces.has(dir)) violations.push({
393
- kind: "orphanPinnedDir",
394
- spaceId: dir,
395
- remediation: `Orphan pinned directory \`${join("migrations", dir)}/\` for an extension not in extensionPacks; remove the directory or re-add the extension.`
396
- });
397
- for (const space of [...inputs.markerRowsBySpace.keys()].sort()) if (!inputs.loadedSpaces.has(space)) violations.push({
398
- kind: "orphanMarker",
399
- spaceId: space,
400
- remediation: `Orphan marker row for space '${space}' (no longer in extensionPacks); remediation: manually delete the row from \`prisma_contract.marker\`.`
401
- });
402
- if (violations.length === 0) return { ok: true };
403
- const kindOrder = {
404
- declaredButUnmigrated: 0,
405
- orphanMarker: 1,
406
- orphanPinnedDir: 2,
407
- hashMismatch: 3,
408
- invariantsMismatch: 4
409
- };
410
- violations.sort((a, b) => {
411
- const k = kindOrder[a.kind] - kindOrder[b.kind];
412
- if (k !== 0) return k;
413
- if (a.spaceId < b.spaceId) return -1;
414
- if (a.spaceId > b.spaceId) return 1;
415
- return 0;
416
- });
417
- return {
418
- ok: false,
419
- violations
420
- };
421
- }
422
- //#endregion
423
- export { APP_SPACE_ID, assertValidSpaceId, concatenateSpaceApplyInputs, detectSpaceContractDrift, emitPinnedSpaceArtefacts, isValidSpaceId, listPinnedSpaceDirectories, planAllSpaces, readPinnedContractHash, spaceMigrationDirectory, verifyContractSpaces };
221
+ export { APP_SPACE_ID, assertDescriptorSelfConsistency, assertValidSpaceId, computeExtensionSpaceApplyPath, detectSpaceContractDrift, emitContractSpaceArtefacts, gatherDiskContractSpaceState, isValidSpaceId, listContractSpaceDirectories, planAllSpaces, readContractSpaceContract, readContractSpaceHeadRef, spaceMigrationDirectory, verifyContractSpaces };
424
222
 
425
223
  //# sourceMappingURL=spaces.mjs.map