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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (130) hide show
  1. package/README.md +34 -22
  2. package/dist/{constants-BRi0X7B_.mjs → constants-DWV9_o2Z.mjs} +2 -2
  3. package/dist/{constants-BRi0X7B_.mjs.map → constants-DWV9_o2Z.mjs.map} +1 -1
  4. package/dist/errors-EPL_9p9f.mjs +297 -0
  5. package/dist/errors-EPL_9p9f.mjs.map +1 -0
  6. package/dist/exports/aggregate.d.mts +614 -0
  7. package/dist/exports/aggregate.d.mts.map +1 -0
  8. package/dist/exports/aggregate.mjs +611 -0
  9. package/dist/exports/aggregate.mjs.map +1 -0
  10. package/dist/exports/constants.d.mts.map +1 -1
  11. package/dist/exports/constants.mjs +2 -3
  12. package/dist/exports/errors.d.mts +68 -0
  13. package/dist/exports/errors.d.mts.map +1 -0
  14. package/dist/exports/errors.mjs +2 -0
  15. package/dist/exports/graph.d.mts +2 -0
  16. package/dist/exports/graph.mjs +1 -0
  17. package/dist/exports/hash.d.mts +52 -0
  18. package/dist/exports/hash.d.mts.map +1 -0
  19. package/dist/exports/hash.mjs +2 -0
  20. package/dist/exports/invariants.d.mts +34 -0
  21. package/dist/exports/invariants.d.mts.map +1 -0
  22. package/dist/exports/invariants.mjs +2 -0
  23. package/dist/exports/io.d.mts +66 -6
  24. package/dist/exports/io.d.mts.map +1 -1
  25. package/dist/exports/io.mjs +2 -3
  26. package/dist/exports/metadata.d.mts +2 -0
  27. package/dist/exports/metadata.mjs +1 -0
  28. package/dist/exports/migration-graph.d.mts +2 -0
  29. package/dist/exports/migration-graph.mjs +2 -0
  30. package/dist/exports/migration-ts.d.mts.map +1 -1
  31. package/dist/exports/migration-ts.mjs +2 -4
  32. package/dist/exports/migration-ts.mjs.map +1 -1
  33. package/dist/exports/migration.d.mts +15 -14
  34. package/dist/exports/migration.d.mts.map +1 -1
  35. package/dist/exports/migration.mjs +70 -43
  36. package/dist/exports/migration.mjs.map +1 -1
  37. package/dist/exports/package.d.mts +3 -0
  38. package/dist/exports/package.mjs +1 -0
  39. package/dist/exports/refs.d.mts.map +1 -1
  40. package/dist/exports/refs.mjs +3 -4
  41. package/dist/exports/refs.mjs.map +1 -1
  42. package/dist/exports/spaces.d.mts +550 -0
  43. package/dist/exports/spaces.d.mts.map +1 -0
  44. package/dist/exports/spaces.mjs +223 -0
  45. package/dist/exports/spaces.mjs.map +1 -0
  46. package/dist/graph-HMWAldoR.d.mts +28 -0
  47. package/dist/graph-HMWAldoR.d.mts.map +1 -0
  48. package/dist/hash-By50zM_E.mjs +74 -0
  49. package/dist/hash-By50zM_E.mjs.map +1 -0
  50. package/dist/invariants-Duc8f9NM.mjs +52 -0
  51. package/dist/invariants-Duc8f9NM.mjs.map +1 -0
  52. package/dist/io-D13dLvUh.mjs +239 -0
  53. package/dist/io-D13dLvUh.mjs.map +1 -0
  54. package/dist/metadata-CFvm3ayn.d.mts +2 -0
  55. package/dist/migration-graph-DGNnKDY5.mjs +523 -0
  56. package/dist/migration-graph-DGNnKDY5.mjs.map +1 -0
  57. package/dist/migration-graph-DulOITvG.d.mts +124 -0
  58. package/dist/migration-graph-DulOITvG.d.mts.map +1 -0
  59. package/dist/op-schema-D5qkXfEf.mjs +13 -0
  60. package/dist/op-schema-D5qkXfEf.mjs.map +1 -0
  61. package/dist/package-BjiZ7KDy.d.mts +21 -0
  62. package/dist/package-BjiZ7KDy.d.mts.map +1 -0
  63. package/dist/read-contract-space-contract-C3-1eyaI.mjs +298 -0
  64. package/dist/read-contract-space-contract-C3-1eyaI.mjs.map +1 -0
  65. package/package.json +44 -19
  66. package/src/aggregate/loader.ts +409 -0
  67. package/src/aggregate/marker-types.ts +16 -0
  68. package/src/aggregate/planner-types.ts +171 -0
  69. package/src/aggregate/planner.ts +158 -0
  70. package/src/aggregate/project-schema-to-space.ts +64 -0
  71. package/src/aggregate/strategies/graph-walk.ts +118 -0
  72. package/src/aggregate/strategies/synth.ts +122 -0
  73. package/src/aggregate/types.ts +89 -0
  74. package/src/aggregate/verifier.ts +230 -0
  75. package/src/assert-descriptor-self-consistency.ts +70 -0
  76. package/src/compute-extension-space-apply-path.ts +152 -0
  77. package/src/concatenate-space-apply-inputs.ts +90 -0
  78. package/src/detect-space-contract-drift.ts +91 -0
  79. package/src/emit-contract-space-artefacts.ts +70 -0
  80. package/src/errors.ts +251 -17
  81. package/src/exports/aggregate.ts +42 -0
  82. package/src/exports/errors.ts +8 -0
  83. package/src/exports/graph.ts +1 -0
  84. package/src/exports/hash.ts +2 -0
  85. package/src/exports/invariants.ts +1 -0
  86. package/src/exports/io.ts +3 -1
  87. package/src/exports/metadata.ts +1 -0
  88. package/src/exports/{dag.ts → migration-graph.ts} +3 -2
  89. package/src/exports/migration.ts +0 -1
  90. package/src/exports/package.ts +2 -0
  91. package/src/exports/spaces.ts +49 -0
  92. package/src/gather-disk-contract-space-state.ts +62 -0
  93. package/src/graph-ops.ts +57 -30
  94. package/src/graph.ts +25 -0
  95. package/src/hash.ts +91 -0
  96. package/src/invariants.ts +56 -0
  97. package/src/io.ts +163 -40
  98. package/src/metadata.ts +1 -0
  99. package/src/migration-base.ts +97 -56
  100. package/src/migration-graph.ts +676 -0
  101. package/src/op-schema.ts +11 -0
  102. package/src/package.ts +21 -0
  103. package/src/plan-all-spaces.ts +76 -0
  104. package/src/read-contract-space-contract.ts +44 -0
  105. package/src/read-contract-space-head-ref.ts +63 -0
  106. package/src/space-layout.ts +48 -0
  107. package/src/verify-contract-spaces.ts +272 -0
  108. package/dist/attestation-BnzTb0Qp.mjs +0 -65
  109. package/dist/attestation-BnzTb0Qp.mjs.map +0 -1
  110. package/dist/errors-BmiSgz1j.mjs +0 -160
  111. package/dist/errors-BmiSgz1j.mjs.map +0 -1
  112. package/dist/exports/attestation.d.mts +0 -37
  113. package/dist/exports/attestation.d.mts.map +0 -1
  114. package/dist/exports/attestation.mjs +0 -4
  115. package/dist/exports/dag.d.mts +0 -51
  116. package/dist/exports/dag.d.mts.map +0 -1
  117. package/dist/exports/dag.mjs +0 -386
  118. package/dist/exports/dag.mjs.map +0 -1
  119. package/dist/exports/types.d.mts +0 -35
  120. package/dist/exports/types.d.mts.map +0 -1
  121. package/dist/exports/types.mjs +0 -3
  122. package/dist/io-Cd6GLyjK.mjs +0 -153
  123. package/dist/io-Cd6GLyjK.mjs.map +0 -1
  124. package/dist/types-DyGXcWWp.d.mts +0 -71
  125. package/dist/types-DyGXcWWp.d.mts.map +0 -1
  126. package/src/attestation.ts +0 -81
  127. package/src/dag.ts +0 -426
  128. package/src/exports/attestation.ts +0 -2
  129. package/src/exports/types.ts +0 -10
  130. package/src/types.ts +0 -66
@@ -0,0 +1,124 @@
1
+ import { n as OnDiskMigrationPackage } from "./package-BjiZ7KDy.mjs";
2
+ import { n as MigrationGraph, t as MigrationEdge } from "./graph-HMWAldoR.mjs";
3
+
4
+ //#region src/migration-graph.d.ts
5
+ declare function reconstructGraph(packages: readonly OnDiskMigrationPackage[]): MigrationGraph;
6
+ /**
7
+ * Find the shortest path from `fromHash` to `toHash` using BFS over the
8
+ * contract-hash graph. Returns the ordered list of edges, or null if no path
9
+ * exists. Returns an empty array when `fromHash === toHash` (no-op).
10
+ *
11
+ * Neighbor ordering is deterministic via the tie-break sort key:
12
+ * label priority → createdAt → to → migrationHash.
13
+ */
14
+ declare function findPath(graph: MigrationGraph, fromHash: string, toHash: string): readonly MigrationEdge[] | null;
15
+ /**
16
+ * Find the shortest path from `fromHash` to `toHash` whose edges collectively
17
+ * cover every invariant in `required`. Returns `null` when no such path exists
18
+ * (either `fromHash`→`toHash` is structurally unreachable, or every reachable
19
+ * path leaves at least one required invariant uncovered). When `required` is
20
+ * empty, delegates to `findPath` so the result is byte-identical for that case.
21
+ *
22
+ * Algorithm: BFS over `(node, coveredSubset)` states with state-level dedup.
23
+ * The covered subset is a `Set<string>` of invariant ids; the state's dedup
24
+ * key is `${node}\0${[...covered].sort().join('\0')}`. State keys distinguish
25
+ * distinct `(node, covered)` tuples regardless of node-name length because
26
+ * `\0` cannot appear in any invariant id (validation rejects whitespace and
27
+ * control chars at authoring time).
28
+ *
29
+ * Neighbour ordering when `required ≠ ∅`: edges covering ≥1 still-needed
30
+ * invariant come first, with `labelPriority → createdAt → to → migrationHash`
31
+ * as the secondary key. The heuristic steers BFS toward the satisfying path;
32
+ * correctness (shortest, deterministic) does not depend on it.
33
+ */
34
+ declare function findPathWithInvariants(graph: MigrationGraph, fromHash: string, toHash: string, required: ReadonlySet<string>): readonly MigrationEdge[] | null;
35
+ interface PathDecision {
36
+ readonly selectedPath: readonly MigrationEdge[];
37
+ readonly fromHash: string;
38
+ readonly toHash: string;
39
+ readonly alternativeCount: number;
40
+ readonly tieBreakReasons: readonly string[];
41
+ readonly refName?: string;
42
+ /** The caller-supplied required invariant set, sorted ascending. */
43
+ readonly requiredInvariants: readonly string[];
44
+ /**
45
+ * The subset of `requiredInvariants` actually covered by edges on
46
+ * `selectedPath`. Always a subset of `requiredInvariants` (when the path
47
+ * is satisfying, equal to it); always derived from `selectedPath`.
48
+ */
49
+ readonly satisfiedInvariants: readonly string[];
50
+ }
51
+ /**
52
+ * Outcome of {@link findPathWithDecision}. The pathfinder distinguishes
53
+ * three cases up front so callers don't re-derive structural reachability:
54
+ *
55
+ * - `ok` — a path covering `required` exists; `decision` carries the
56
+ * selection metadata and per-edge invariants.
57
+ * - `unreachable` — `from`→`to` has no structural path. Mapped by callers
58
+ * to the existing no-path / `NO_TARGET` diagnostic.
59
+ * - `unsatisfiable` — `from`→`to` is structurally reachable but no path
60
+ * covers every required invariant. `structuralPath` is the
61
+ * `findPath(graph, from, to)` result, included so callers don't have to
62
+ * recompute it when raising `MIGRATION.NO_INVARIANT_PATH`. `missing` is
63
+ * the subset of `required` that the structural path does *not* cover —
64
+ * correctly accounts for partial coverage when some required invariants
65
+ * are met by the fallback path. Only emitted when `required` is
66
+ * non-empty.
67
+ */
68
+ type FindPathOutcome = {
69
+ readonly kind: 'ok';
70
+ readonly decision: PathDecision;
71
+ } | {
72
+ readonly kind: 'unreachable';
73
+ } | {
74
+ readonly kind: 'unsatisfiable';
75
+ readonly structuralPath: readonly MigrationEdge[];
76
+ readonly missing: readonly string[];
77
+ };
78
+ /**
79
+ * Routing context for {@link findPathWithDecision}. Both fields are optional;
80
+ * `refName` is only used to decorate the resulting `PathDecision` for the
81
+ * JSON envelope, and `required` defaults to an empty set (purely structural
82
+ * routing). They are passed via a single options object so the call sites
83
+ * cannot silently swap two adjacent string parameters.
84
+ */
85
+ interface FindPathWithDecisionOptions {
86
+ readonly refName?: string;
87
+ readonly required?: ReadonlySet<string>;
88
+ }
89
+ /**
90
+ * Find the shortest path from `fromHash` to `toHash` and return structured
91
+ * path-decision metadata for machine-readable output. When `required` is
92
+ * non-empty, the returned path is the shortest one whose edges collectively
93
+ * cover every required invariant.
94
+ *
95
+ * The discriminated return type tells the caller *why* a path could not be
96
+ * found, so the CLI can pick the right structured error without re-running
97
+ * a structural BFS.
98
+ */
99
+ declare function findPathWithDecision(graph: MigrationGraph, fromHash: string, toHash: string, options?: FindPathWithDecisionOptions): FindPathOutcome;
100
+ /**
101
+ * Find all branch tips (nodes with no outgoing edges) reachable from
102
+ * `fromHash` via forward edges.
103
+ */
104
+ declare function findReachableLeaves(graph: MigrationGraph, fromHash: string): readonly string[];
105
+ /**
106
+ * Find the target contract hash of the migration graph reachable from
107
+ * EMPTY_CONTRACT_HASH. Returns `null` for a graph that has no target
108
+ * state (either empty, or containing only the root with no outgoing
109
+ * edges). Throws NO_INITIAL_MIGRATION if the graph has nodes but none
110
+ * originate from the empty hash, and AMBIGUOUS_TARGET if multiple
111
+ * branch tips exist.
112
+ */
113
+ declare function findLeaf(graph: MigrationGraph): string | null;
114
+ /**
115
+ * Find the latest migration entry by traversing from EMPTY_CONTRACT_HASH
116
+ * to the single target. Returns null for an empty graph.
117
+ * Throws AMBIGUOUS_TARGET if the graph has multiple branch tips.
118
+ */
119
+ declare function findLatestMigration(graph: MigrationGraph): MigrationEdge | null;
120
+ declare function detectCycles(graph: MigrationGraph): readonly string[][];
121
+ declare function detectOrphans(graph: MigrationGraph): readonly MigrationEdge[];
122
+ //#endregion
123
+ export { findLeaf as a, findPathWithInvariants as c, findLatestMigration as i, findReachableLeaves as l, detectCycles as n, findPath as o, detectOrphans as r, findPathWithDecision as s, PathDecision as t, reconstructGraph as u };
124
+ //# sourceMappingURL=migration-graph-DulOITvG.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migration-graph-DulOITvG.d.mts","names":[],"sources":["../src/migration-graph.ts"],"mappings":";;;;iBAsCgB,gBAAA,CAAiB,QAAA,WAAmB,sBAAA,KAA2B,cAAA;;AAA/E;;;;;;;iBAqFgB,QAAA,CACd,KAAA,EAAO,cAAA,EACP,QAAA,UACA,MAAA,oBACU,aAAA;;AAJZ;;;;;;;;;;;AAgDA;;;;;;;iBAAgB,sBAAA,CACd,KAAA,EAAO,cAAA,EACP,QAAA,UACA,MAAA,UACA,QAAA,EAAU,WAAA,oBACA,aAAA;AAAA,UAsFK,YAAA;EAAA,SACN,YAAA,WAAuB,aAAA;EAAA,SACvB,QAAA;EAAA,SACA,MAAA;EAAA,SACA,gBAAA;EAAA,SACA,eAAA;EAAA,SACA,OAAA;EA5Fc;EAAA,SA8Fd,kBAAA;EARM;;;;;EAAA,SAcN,mBAAA;AAAA;;;;;;;;;AAoBX;;;;;;;;;KAAY,eAAA;EAAA,SACG,IAAA;EAAA,SAAqB,QAAA,EAAU,YAAA;AAAA;EAAA,SAC/B,IAAA;AAAA;EAAA,SAEA,IAAA;EAAA,SACA,cAAA,WAAyB,aAAA;EAAA,SACzB,OAAA;AAAA;;;;;AAyBf;;;UAfiB,2BAAA;EAAA,SACN,OAAA;EAAA,SACA,QAAA,GAAW,WAAA;AAAA;;;;;;;;;;;iBAaN,oBAAA,CACd,KAAA,EAAO,cAAA,EACP,QAAA,UACA,MAAA,UACA,OAAA,GAAS,2BAAA,GACR,eAAA;;;;;iBAqLa,mBAAA,CAAoB,KAAA,EAAO,cAAA,EAAgB,QAAA;;;;AAkB3D;;;;;iBAAgB,QAAA,CAAS,KAAA,EAAO,cAAA;;;;;;iBAwChB,mBAAA,CAAoB,KAAA,EAAO,cAAA,GAAiB,aAAA;AAAA,iBAQ5C,YAAA,CAAa,KAAA,EAAO,cAAA;AAAA,iBA8DpB,aAAA,CAAc,KAAA,EAAO,cAAA,YAA0B,aAAA"}
@@ -0,0 +1,13 @@
1
+ import { type } from "arktype";
2
+ //#region src/op-schema.ts
3
+ const MigrationOpSchema = type({
4
+ id: "string",
5
+ label: "string",
6
+ operationClass: "'additive' | 'widening' | 'destructive' | 'data'",
7
+ "invariantId?": "string"
8
+ });
9
+ const MigrationOpsSchema = MigrationOpSchema.array();
10
+ //#endregion
11
+ export { MigrationOpsSchema as n, MigrationOpSchema as t };
12
+
13
+ //# sourceMappingURL=op-schema-D5qkXfEf.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"op-schema-D5qkXfEf.mjs","names":[],"sources":["../src/op-schema.ts"],"sourcesContent":["import { type } from 'arktype';\n\nexport const MigrationOpSchema = type({\n id: 'string',\n label: 'string',\n operationClass: \"'additive' | 'widening' | 'destructive' | 'data'\",\n 'invariantId?': 'string',\n});\n\n// Intentionally shallow: operation-specific payload validation is owned by planner/runner layers.\nexport const MigrationOpsSchema = MigrationOpSchema.array();\n"],"mappings":";;AAEA,MAAa,oBAAoB,KAAK;CACpC,IAAI;CACJ,OAAO;CACP,gBAAgB;CAChB,gBAAgB;CACjB,CAAC;AAGF,MAAa,qBAAqB,kBAAkB,OAAO"}
@@ -0,0 +1,21 @@
1
+ import { MigrationPackage, MigrationPlanOperation } from "@prisma-next/framework-components/control";
2
+
3
+ //#region src/package.d.ts
4
+ type MigrationOps = readonly MigrationPlanOperation[];
5
+ /**
6
+ * Augmented form of the canonical {@link MigrationPackage} returned by
7
+ * the on-disk readers (`readMigrationPackage`, `readMigrationsDir`).
8
+ * Adds `dirPath` — the absolute path the package was loaded from — so
9
+ * downstream diagnostics can point operators at a concrete directory.
10
+ *
11
+ * Holding an `OnDiskMigrationPackage` value implies the loader verified
12
+ * the package's integrity (hash recomputation against the stored
13
+ * `migrationHash`); the canonical structural shape carries no such
14
+ * guarantee on its own.
15
+ */
16
+ interface OnDiskMigrationPackage extends MigrationPackage {
17
+ readonly dirPath: string;
18
+ }
19
+ //#endregion
20
+ export { OnDiskMigrationPackage as n, MigrationOps as t };
21
+ //# sourceMappingURL=package-BjiZ7KDy.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"package-BjiZ7KDy.d.mts","names":[],"sources":["../src/package.ts"],"mappings":";;;KAKY,YAAA,YAAwB,sBAAA;;AAApC;;;;;AAaA;;;;;UAAiB,sBAAA,SAA+B,gBAAA;EAAA,SACrC,OAAA;AAAA"}
@@ -0,0 +1,298 @@
1
+ import { _ as errorInvalidSpaceId, p as errorInvalidRefFile, u as errorInvalidJson, y as errorMissingFile } from "./errors-EPL_9p9f.mjs";
2
+ import { t as MANIFEST_FILE } from "./io-D13dLvUh.mjs";
3
+ import { join } from "pathe";
4
+ import { readFile, readdir, stat } from "node:fs/promises";
5
+ import { APP_SPACE_ID } from "@prisma-next/framework-components/control";
6
+ //#region src/space-layout.ts
7
+ /**
8
+ * Pattern a contract-space identifier must match. The constraint is
9
+ * filesystem-friendly: lowercase letters / digits / hyphen / underscore,
10
+ * starts with a letter, max 64 characters.
11
+ */
12
+ const SPACE_ID_PATTERN = /^[a-z][a-z0-9_-]{0,63}$/;
13
+ function isValidSpaceId(spaceId) {
14
+ return SPACE_ID_PATTERN.test(spaceId);
15
+ }
16
+ function assertValidSpaceId(spaceId) {
17
+ if (!isValidSpaceId(spaceId)) throw errorInvalidSpaceId(spaceId);
18
+ }
19
+ /**
20
+ * Resolve the migrations subdirectory for a given contract space.
21
+ *
22
+ * Every contract space — including the app space (default `'app'`) —
23
+ * lands under `<projectMigrationsDir>/<spaceId>/`. The space id is
24
+ * validated against {@link SPACE_ID_PATTERN} because it becomes a
25
+ * filesystem directory name verbatim.
26
+ *
27
+ * `projectMigrationsDir` is the project's top-level `migrations/`
28
+ * directory; the helper does not assume anything about its absolute /
29
+ * relative shape and is symmetric with `pathe.join`.
30
+ */
31
+ function spaceMigrationDirectory(projectMigrationsDir, spaceId) {
32
+ assertValidSpaceId(spaceId);
33
+ return join(projectMigrationsDir, spaceId);
34
+ }
35
+ //#endregion
36
+ //#region src/read-contract-space-head-ref.ts
37
+ function hasErrnoCode$2(error, code) {
38
+ return error instanceof Error && error.code === code;
39
+ }
40
+ /**
41
+ * Read the head ref (`hash` + `invariants`) for a contract space from
42
+ * `<projectMigrationsDir>/<spaceId>/refs/head.json`.
43
+ *
44
+ * Returns `null` when the file does not exist (first emit). Surfaces
45
+ * `MIGRATION.INVALID_JSON` / `MIGRATION.INVALID_REF_FILE` on a corrupt
46
+ * `refs/head.json` so callers can distinguish "no head ref on disk"
47
+ * (returns `null`) from "head ref present but unreadable" (throws).
48
+ *
49
+ * Validates the space id against `[a-z][a-z0-9_-]{0,63}` for the same
50
+ * filesystem-safety reasons as the rest of the per-space helpers. The
51
+ * helper is uniform across the app and extension spaces.
52
+ */
53
+ async function readContractSpaceHeadRef(projectMigrationsDir, spaceId) {
54
+ assertValidSpaceId(spaceId);
55
+ const filePath = join(projectMigrationsDir, spaceId, "refs", "head.json");
56
+ let raw;
57
+ try {
58
+ raw = await readFile(filePath, "utf-8");
59
+ } catch (error) {
60
+ if (hasErrnoCode$2(error, "ENOENT")) return null;
61
+ throw error;
62
+ }
63
+ let parsed;
64
+ try {
65
+ parsed = JSON.parse(raw);
66
+ } catch (e) {
67
+ throw errorInvalidJson(filePath, e instanceof Error ? e.message : String(e));
68
+ }
69
+ if (typeof parsed !== "object" || parsed === null) throw errorInvalidRefFile(filePath, "expected an object");
70
+ const obj = parsed;
71
+ if (typeof obj.hash !== "string") throw errorInvalidRefFile(filePath, "expected an object with a string `hash` field");
72
+ if (!Array.isArray(obj.invariants) || obj.invariants.some((value) => typeof value !== "string")) throw errorInvalidRefFile(filePath, "expected an object with an `invariants` array of strings");
73
+ return {
74
+ hash: obj.hash,
75
+ invariants: obj.invariants
76
+ };
77
+ }
78
+ //#endregion
79
+ //#region src/detect-space-contract-drift.ts
80
+ /**
81
+ * Pure drift-detection primitive for a single contract space.
82
+ *
83
+ * Runs once per loaded extension space, just before computing the
84
+ * `priorContract` that feeds {@link import('./plan-all-spaces').planAllSpaces}.
85
+ * Hash equality is byte-for-byte (no normalisation) — both sides are
86
+ * already canonical hashes produced by the same pipeline, so any
87
+ * difference is meaningful drift.
88
+ *
89
+ * Synchronous, pure, no I/O. The caller (SQL family) reads the on-disk
90
+ * `contract.json` and computes its hash, then invokes this helper
91
+ * alongside the descriptor's `headRef.hash`. Composes naturally with
92
+ * {@link import('./read-contract-space-head-ref').readContractSpaceHeadRef}
93
+ * which provides the read-side primitive.
94
+ *
95
+ * The drift warning surfaces the extension name and the diff direction.
96
+ */
97
+ function detectSpaceContractDrift(spaceId, inputs) {
98
+ if (inputs.priorHeadHash === null) return {
99
+ kind: "firstEmit",
100
+ spaceId,
101
+ descriptorHash: inputs.descriptorHash,
102
+ priorHeadHash: null
103
+ };
104
+ if (inputs.descriptorHash === inputs.priorHeadHash) return {
105
+ kind: "noDrift",
106
+ spaceId,
107
+ descriptorHash: inputs.descriptorHash,
108
+ priorHeadHash: inputs.priorHeadHash
109
+ };
110
+ return {
111
+ kind: "drift",
112
+ spaceId,
113
+ descriptorHash: inputs.descriptorHash,
114
+ priorHeadHash: inputs.priorHeadHash
115
+ };
116
+ }
117
+ //#endregion
118
+ //#region src/verify-contract-spaces.ts
119
+ function hasErrnoCode$1(error, code) {
120
+ return error instanceof Error && error.code === code;
121
+ }
122
+ /**
123
+ * List the per-space subdirectories under
124
+ * `<projectRoot>/migrations/`. Returns space-id directory names (sorted
125
+ * alphabetically) — i.e. any non-dot-prefixed subdirectory whose root
126
+ * does **not** contain a `migration.json` manifest. The manifest is the
127
+ * structural marker of a user-authored migration directory (see
128
+ * `readMigrationsDir` in `./io`); directory names themselves belong to
129
+ * the user and are not part of the contract.
130
+ *
131
+ * Returns `[]` if the migrations directory does not exist (greenfield
132
+ * project).
133
+ *
134
+ * Reads only the user's repo. **No descriptor import.** The caller
135
+ * (verifier) feeds the result into {@link verifyContractSpaces} alongside
136
+ * the loaded-space set and the marker rows.
137
+ */
138
+ async function listContractSpaceDirectories(projectMigrationsDir) {
139
+ let entries;
140
+ try {
141
+ entries = (await readdir(projectMigrationsDir, { withFileTypes: true })).map((d) => ({
142
+ name: d.name,
143
+ isDirectory: d.isDirectory()
144
+ }));
145
+ } catch (error) {
146
+ if (hasErrnoCode$1(error, "ENOENT")) return [];
147
+ throw error;
148
+ }
149
+ const namedCandidates = entries.filter((e) => e.isDirectory).map((e) => e.name).filter((name) => !name.startsWith(".")).sort();
150
+ return (await Promise.all(namedCandidates.map(async (name) => {
151
+ try {
152
+ await stat(join(projectMigrationsDir, name, MANIFEST_FILE));
153
+ return {
154
+ name,
155
+ isMigrationDir: true
156
+ };
157
+ } catch (error) {
158
+ if (hasErrnoCode$1(error, "ENOENT")) return {
159
+ name,
160
+ isMigrationDir: false
161
+ };
162
+ throw error;
163
+ }
164
+ }))).filter((c) => !c.isMigrationDir).map((c) => c.name);
165
+ }
166
+ /**
167
+ * Pure structural verifier for the per-space mechanism. Aggregates the
168
+ * three orphan / missing checks plus per-space hash and invariant
169
+ * comparison.
170
+ *
171
+ * Algorithm:
172
+ *
173
+ * - For every extension space declared in `loadedSpaces` (`'app'`
174
+ * excluded — the per-space verifier is scoped to extension members;
175
+ * the app is verified through the aggregate path):
176
+ * - If no contract-space dir on disk → `declaredButUnmigrated`.
177
+ * - Else if `markerRowsBySpace` lacks an entry → no violation here;
178
+ * the live-DB compare done outside this helper is where the
179
+ * absence shows up.
180
+ * - Else compare marker hash / invariants vs. on-disk head hash /
181
+ * invariants → `hashMismatch` / `invariantsMismatch` on drift.
182
+ * - For every contract-space dir on disk that is not in `loadedSpaces` →
183
+ * `orphanSpaceDir`.
184
+ * - For every marker row whose `space` is not in `loadedSpaces` →
185
+ * `orphanMarker`. The app-space marker is always loaded (`'app'` is
186
+ * in `loadedSpaces` by definition).
187
+ *
188
+ * Output is deterministic: violations are sorted first by `kind`
189
+ * (`declaredButUnmigrated` → `orphanMarker` → `orphanSpaceDir` →
190
+ * `hashMismatch` → `invariantsMismatch`) then by `spaceId`. Two callers
191
+ * passing equivalent inputs see byte-identical violation lists.
192
+ *
193
+ * Synchronous, pure, no I/O. **Does not import the extension descriptor**
194
+ * (the inputs are pre-resolved by the caller); the verifier reads only
195
+ * the user repo, not `node_modules`.
196
+ */
197
+ function verifyContractSpaces(inputs) {
198
+ const violations = [];
199
+ for (const spaceId of [...inputs.loadedSpaces].sort()) {
200
+ if (spaceId === APP_SPACE_ID) continue;
201
+ if (!inputs.spaceDirsOnDisk.includes(spaceId)) {
202
+ violations.push({
203
+ kind: "declaredButUnmigrated",
204
+ spaceId,
205
+ remediation: `Extension '${spaceId}' is declared in extensionPacks but has not been emitted; run \`prisma-next migrate\`.`
206
+ });
207
+ continue;
208
+ }
209
+ const head = inputs.headRefsBySpace.get(spaceId);
210
+ const marker = inputs.markerRowsBySpace.get(spaceId);
211
+ if (!head || !marker) continue;
212
+ if (head.hash !== marker.hash) {
213
+ violations.push({
214
+ kind: "hashMismatch",
215
+ spaceId,
216
+ priorHeadHash: head.hash,
217
+ markerHash: marker.hash,
218
+ remediation: `Marker row for space '${spaceId}' is keyed at ${marker.hash}, but the on-disk ${join("migrations", spaceId, "contract.json")} resolves to ${head.hash}. Run \`prisma-next db update\` to advance the database, or \`prisma-next migrate\` if the descriptor was bumped without re-emitting.`
219
+ });
220
+ continue;
221
+ }
222
+ const onDiskInvariants = [...head.invariants].sort();
223
+ const markerInvariants = new Set(marker.invariants);
224
+ const missing = onDiskInvariants.filter((id) => !markerInvariants.has(id));
225
+ if (missing.length > 0) violations.push({
226
+ kind: "invariantsMismatch",
227
+ spaceId,
228
+ onDiskInvariants,
229
+ markerInvariants: [...marker.invariants].sort(),
230
+ 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.`
231
+ });
232
+ }
233
+ for (const dir of [...inputs.spaceDirsOnDisk].sort()) if (!inputs.loadedSpaces.has(dir)) violations.push({
234
+ kind: "orphanSpaceDir",
235
+ spaceId: dir,
236
+ remediation: `Orphan contract-space directory \`${join("migrations", dir)}/\` for an extension not in extensionPacks; remove the directory or re-add the extension.`
237
+ });
238
+ for (const space of [...inputs.markerRowsBySpace.keys()].sort()) if (!inputs.loadedSpaces.has(space)) violations.push({
239
+ kind: "orphanMarker",
240
+ spaceId: space,
241
+ remediation: `Orphan marker row for space '${space}' (no longer in extensionPacks); remediation: manually delete the row from \`prisma_contract.marker\`.`
242
+ });
243
+ if (violations.length === 0) return { ok: true };
244
+ const kindOrder = {
245
+ declaredButUnmigrated: 0,
246
+ orphanMarker: 1,
247
+ orphanSpaceDir: 2,
248
+ hashMismatch: 3,
249
+ invariantsMismatch: 4
250
+ };
251
+ violations.sort((a, b) => {
252
+ const k = kindOrder[a.kind] - kindOrder[b.kind];
253
+ if (k !== 0) return k;
254
+ if (a.spaceId < b.spaceId) return -1;
255
+ if (a.spaceId > b.spaceId) return 1;
256
+ return 0;
257
+ });
258
+ return {
259
+ ok: false,
260
+ violations
261
+ };
262
+ }
263
+ //#endregion
264
+ //#region src/read-contract-space-contract.ts
265
+ function hasErrnoCode(error, code) {
266
+ return error instanceof Error && error.code === code;
267
+ }
268
+ /**
269
+ * Read the on-disk contract value for a contract space
270
+ * (`<projectMigrationsDir>/<spaceId>/contract.json`). Returns the parsed
271
+ * JSON value as `unknown` — callers that need a typed contract validate
272
+ * via their family's `validateContract` to surface schema issues.
273
+ *
274
+ * Companion to {@link import('./read-contract-space-head-ref').readContractSpaceHeadRef}
275
+ * — same ENOENT-throws / corrupt-file-error semantics. Returns the
276
+ * canonical-JSON value the framework wrote during emit, so re-running
277
+ * this helper across machines / runs yields a byte-identical value.
278
+ */
279
+ async function readContractSpaceContract(projectMigrationsDir, spaceId) {
280
+ assertValidSpaceId(spaceId);
281
+ const filePath = join(projectMigrationsDir, spaceId, "contract.json");
282
+ let raw;
283
+ try {
284
+ raw = await readFile(filePath, "utf-8");
285
+ } catch (error) {
286
+ if (hasErrnoCode(error, "ENOENT")) throw errorMissingFile("contract.json", join(projectMigrationsDir, spaceId));
287
+ throw error;
288
+ }
289
+ try {
290
+ return JSON.parse(raw);
291
+ } catch (e) {
292
+ throw errorInvalidJson(filePath, e instanceof Error ? e.message : String(e));
293
+ }
294
+ }
295
+ //#endregion
296
+ export { readContractSpaceHeadRef as a, isValidSpaceId as c, detectSpaceContractDrift as i, spaceMigrationDirectory as l, listContractSpaceDirectories as n, APP_SPACE_ID as o, verifyContractSpaces as r, assertValidSpaceId as s, readContractSpaceContract as t };
297
+
298
+ //# sourceMappingURL=read-contract-space-contract-C3-1eyaI.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"read-contract-space-contract-C3-1eyaI.mjs","names":["hasErrnoCode","hasErrnoCode"],"sources":["../src/space-layout.ts","../src/read-contract-space-head-ref.ts","../src/detect-space-contract-drift.ts","../src/verify-contract-spaces.ts","../src/read-contract-space-contract.ts"],"sourcesContent":["import { APP_SPACE_ID } from '@prisma-next/framework-components/control';\nimport { join } from 'pathe';\nimport { errorInvalidSpaceId } from './errors';\n\nexport { APP_SPACE_ID };\n\n/**\n * Branded string carrying a compile-time guarantee that the value has\n * been validated by {@link assertValidSpaceId}. Downstream filesystem\n * helpers (e.g. {@link spaceMigrationDirectory}) accept this type to\n * make \"validated\" tracking visible at the type level rather than\n * relying purely on a runtime check.\n */\nexport type ValidSpaceId = string & { readonly __brand: 'ValidSpaceId' };\n\n/**\n * Pattern a contract-space identifier must match. The constraint is\n * filesystem-friendly: lowercase letters / digits / hyphen / underscore,\n * starts with a letter, max 64 characters.\n */\nconst SPACE_ID_PATTERN = /^[a-z][a-z0-9_-]{0,63}$/;\n\nexport function isValidSpaceId(spaceId: string): spaceId is ValidSpaceId {\n return SPACE_ID_PATTERN.test(spaceId);\n}\n\nexport function assertValidSpaceId(spaceId: string): asserts spaceId is ValidSpaceId {\n if (!isValidSpaceId(spaceId)) {\n throw errorInvalidSpaceId(spaceId);\n }\n}\n\n/**\n * Resolve the migrations subdirectory for a given contract space.\n *\n * Every contract space — including the app space (default `'app'`) —\n * lands under `<projectMigrationsDir>/<spaceId>/`. The space id is\n * validated against {@link SPACE_ID_PATTERN} because it becomes a\n * filesystem directory name verbatim.\n *\n * `projectMigrationsDir` is the project's top-level `migrations/`\n * directory; the helper does not assume anything about its absolute /\n * relative shape and is symmetric with `pathe.join`.\n */\nexport function spaceMigrationDirectory(projectMigrationsDir: string, spaceId: string): string {\n assertValidSpaceId(spaceId);\n return join(projectMigrationsDir, spaceId);\n}\n","import { readFile } from 'node:fs/promises';\nimport type { ContractSpaceHeadRef } from '@prisma-next/framework-components/control';\nimport { join } from 'pathe';\nimport { errorInvalidJson, errorInvalidRefFile } from './errors';\nimport { assertValidSpaceId } from './space-layout';\n\nexport type { ContractSpaceHeadRef };\n\nfunction hasErrnoCode(error: unknown, code: string): boolean {\n return error instanceof Error && (error as { code?: string }).code === code;\n}\n\n/**\n * Read the head ref (`hash` + `invariants`) for a contract space from\n * `<projectMigrationsDir>/<spaceId>/refs/head.json`.\n *\n * Returns `null` when the file does not exist (first emit). Surfaces\n * `MIGRATION.INVALID_JSON` / `MIGRATION.INVALID_REF_FILE` on a corrupt\n * `refs/head.json` so callers can distinguish \"no head ref on disk\"\n * (returns `null`) from \"head ref present but unreadable\" (throws).\n *\n * Validates the space id against `[a-z][a-z0-9_-]{0,63}` for the same\n * filesystem-safety reasons as the rest of the per-space helpers. The\n * helper is uniform across the app and extension spaces.\n */\nexport async function readContractSpaceHeadRef(\n projectMigrationsDir: string,\n spaceId: string,\n): Promise<ContractSpaceHeadRef | null> {\n assertValidSpaceId(spaceId);\n\n const filePath = join(projectMigrationsDir, spaceId, 'refs', 'head.json');\n\n let raw: string;\n try {\n raw = await readFile(filePath, 'utf-8');\n } catch (error) {\n if (hasErrnoCode(error, 'ENOENT')) {\n return null;\n }\n throw error;\n }\n\n let parsed: unknown;\n try {\n parsed = JSON.parse(raw);\n } catch (e) {\n throw errorInvalidJson(filePath, e instanceof Error ? e.message : String(e));\n }\n\n if (typeof parsed !== 'object' || parsed === null) {\n throw errorInvalidRefFile(filePath, 'expected an object');\n }\n const obj = parsed as { hash?: unknown; invariants?: unknown };\n if (typeof obj.hash !== 'string') {\n throw errorInvalidRefFile(filePath, 'expected an object with a string `hash` field');\n }\n if (!Array.isArray(obj.invariants) || obj.invariants.some((value) => typeof value !== 'string')) {\n throw errorInvalidRefFile(filePath, 'expected an object with an `invariants` array of strings');\n }\n\n return { hash: obj.hash, invariants: obj.invariants as readonly string[] };\n}\n","/**\n * Inputs for {@link detectSpaceContractDrift}.\n *\n * Both hashes are produced by the caller (the SQL-family wiring at the\n * consumption site) using the canonical contract hashing pipeline.\n * Keeping the helper pure lets `migration-tools` stay framework-neutral\n * — the SQL family already speaks `Contract<SqlStorage>`, the Mongo\n * family speaks its own contract type, and both reduce to a hash string\n * before drift detection runs.\n *\n * `priorHeadHash` is `null` when no `contract.json` exists yet on disk for\n * the space (the descriptor declares an extension that has never been\n * emitted into the user's repo). That's the \"first emit\" case — no\n * drift to surface; the migrate emit will create the on-disk artefacts.\n */\nexport interface DetectSpaceContractDriftInputs {\n readonly descriptorHash: string;\n readonly priorHeadHash: string | null;\n}\n\n/**\n * Result discriminant for {@link detectSpaceContractDrift}.\n *\n * - `noDrift`: descriptor hash and on-disk head hash agree byte-for-byte.\n * The migrate emit can proceed with no warning.\n * - `firstEmit`: no on-disk `contract.json` on disk yet. The extension\n * was just added to `extensionPacks`; this run will create the\n * on-disk artefacts. No warning either — the user's intent is to install\n * the extension, not to \"drift\" from a state they haven't recorded.\n * - `drift`: descriptor hash differs from on-disk head hash. The caller\n * surfaces a non-fatal warning naming the extension and the\n * diff direction (descriptor → on-disk head). The migrate emit proceeds\n * normally so the bump is materialised this run; the warning just\n * confirms the bump is being captured.\n *\n * `spaceId`, `descriptorHash`, and `priorHeadHash` are threaded through\n * verbatim so the caller (logger / TerminalUI / strict-mode envelope)\n * has everything it needs to format the warning message without\n * re-reading the descriptor or the on-disk artefact.\n */\nexport type SpaceContractDriftResult = {\n readonly kind: 'noDrift' | 'firstEmit' | 'drift';\n readonly spaceId: string;\n readonly descriptorHash: string;\n readonly priorHeadHash: string | null;\n};\n\n/**\n * Pure drift-detection primitive for a single contract space.\n *\n * Runs once per loaded extension space, just before computing the\n * `priorContract` that feeds {@link import('./plan-all-spaces').planAllSpaces}.\n * Hash equality is byte-for-byte (no normalisation) — both sides are\n * already canonical hashes produced by the same pipeline, so any\n * difference is meaningful drift.\n *\n * Synchronous, pure, no I/O. The caller (SQL family) reads the on-disk\n * `contract.json` and computes its hash, then invokes this helper\n * alongside the descriptor's `headRef.hash`. Composes naturally with\n * {@link import('./read-contract-space-head-ref').readContractSpaceHeadRef}\n * which provides the read-side primitive.\n *\n * The drift warning surfaces the extension name and the diff direction.\n */\nexport function detectSpaceContractDrift(\n spaceId: string,\n inputs: DetectSpaceContractDriftInputs,\n): SpaceContractDriftResult {\n if (inputs.priorHeadHash === null) {\n return {\n kind: 'firstEmit',\n spaceId,\n descriptorHash: inputs.descriptorHash,\n priorHeadHash: null,\n };\n }\n if (inputs.descriptorHash === inputs.priorHeadHash) {\n return {\n kind: 'noDrift',\n spaceId,\n descriptorHash: inputs.descriptorHash,\n priorHeadHash: inputs.priorHeadHash,\n };\n }\n return {\n kind: 'drift',\n spaceId,\n descriptorHash: inputs.descriptorHash,\n priorHeadHash: inputs.priorHeadHash,\n };\n}\n","import { readdir, stat } from 'node:fs/promises';\nimport { join } from 'pathe';\nimport { MANIFEST_FILE } from './io';\nimport { APP_SPACE_ID } from './space-layout';\n\nfunction hasErrnoCode(error: unknown, code: string): boolean {\n return error instanceof Error && (error as { code?: string }).code === code;\n}\n\n/**\n * List the per-space subdirectories under\n * `<projectRoot>/migrations/`. Returns space-id directory names (sorted\n * alphabetically) — i.e. any non-dot-prefixed subdirectory whose root\n * does **not** contain a `migration.json` manifest. The manifest is the\n * structural marker of a user-authored migration directory (see\n * `readMigrationsDir` in `./io`); directory names themselves belong to\n * the user and are not part of the contract.\n *\n * Returns `[]` if the migrations directory does not exist (greenfield\n * project).\n *\n * Reads only the user's repo. **No descriptor import.** The caller\n * (verifier) feeds the result into {@link verifyContractSpaces} alongside\n * the loaded-space set and the marker rows.\n */\nexport async function listContractSpaceDirectories(\n projectMigrationsDir: string,\n): Promise<readonly string[]> {\n let entries: { readonly name: string; readonly isDirectory: boolean }[];\n try {\n const dirents = await readdir(projectMigrationsDir, { withFileTypes: true });\n entries = dirents.map((d) => ({ name: d.name, isDirectory: d.isDirectory() }));\n } catch (error) {\n if (hasErrnoCode(error, 'ENOENT')) {\n return [];\n }\n throw error;\n }\n\n const namedCandidates = entries\n .filter((e) => e.isDirectory)\n .map((e) => e.name)\n .filter((name) => !name.startsWith('.'))\n .sort();\n\n const manifestChecks = await Promise.all(\n namedCandidates.map(async (name) => {\n try {\n await stat(join(projectMigrationsDir, name, MANIFEST_FILE));\n return { name, isMigrationDir: true };\n } catch (error) {\n if (hasErrnoCode(error, 'ENOENT')) {\n return { name, isMigrationDir: false };\n }\n throw error;\n }\n }),\n );\n\n return manifestChecks.filter((c) => !c.isMigrationDir).map((c) => c.name);\n}\n\n/**\n * On-disk head value (`(hash, invariants)`) for one contract space.\n * The verifier compares this against the marker row for the same space\n * to detect drift between the user-emitted artefacts and the live DB\n * marker.\n */\nexport interface ContractSpaceHeadRecord {\n readonly hash: string;\n readonly invariants: readonly string[];\n}\n\n/**\n * Marker row read from `prisma_contract.marker` (one per `space`).\n * Caller resolves these via the family runtime's marker reader before\n * invoking {@link verifyContractSpaces}.\n */\nexport interface SpaceMarkerRecord {\n readonly hash: string;\n readonly invariants: readonly string[];\n}\n\nexport interface VerifyContractSpacesInputs {\n /**\n * Set of contract spaces the project declares: `'app'` plus each\n * extension space in `extensionPacks`. The caller's discovery path\n * never reads the extension descriptor module — it walks the\n * `extensionPacks` configuration in `prisma-next.config.ts` for the\n * space ids.\n */\n readonly loadedSpaces: ReadonlySet<string>;\n\n /**\n * Per-space subdirectories observed under\n * `<projectRoot>/migrations/`. Resolved via\n * {@link listContractSpaceDirectories}.\n */\n readonly spaceDirsOnDisk: readonly string[];\n\n /**\n * Head ref per space, keyed by space id. Caller reads\n * `<projectRoot>/migrations/<space-id>/contract.json` and\n * `<projectRoot>/migrations/<space-id>/refs/head.json` to construct\n * this map. Spaces with no contract-space dir on disk simply omit a\n * map entry.\n */\n readonly headRefsBySpace: ReadonlyMap<string, ContractSpaceHeadRecord>;\n\n /**\n * Marker rows keyed by `space`. Caller reads them from the\n * `prisma_contract.marker` table.\n */\n readonly markerRowsBySpace: ReadonlyMap<string, SpaceMarkerRecord>;\n}\n\nexport type SpaceVerifierViolation =\n | {\n readonly kind: 'declaredButUnmigrated';\n readonly spaceId: string;\n readonly remediation: string;\n }\n | {\n readonly kind: 'orphanMarker';\n readonly spaceId: string;\n readonly remediation: string;\n }\n | {\n readonly kind: 'orphanSpaceDir';\n readonly spaceId: string;\n readonly remediation: string;\n }\n | {\n readonly kind: 'hashMismatch';\n readonly spaceId: string;\n readonly priorHeadHash: string;\n readonly markerHash: string;\n readonly remediation: string;\n }\n | {\n readonly kind: 'invariantsMismatch';\n readonly spaceId: string;\n readonly onDiskInvariants: readonly string[];\n readonly markerInvariants: readonly string[];\n readonly remediation: string;\n };\n\nexport type VerifyContractSpacesResult =\n | { readonly ok: true }\n | { readonly ok: false; readonly violations: readonly SpaceVerifierViolation[] };\n\n/**\n * Pure structural verifier for the per-space mechanism. Aggregates the\n * three orphan / missing checks plus per-space hash and invariant\n * comparison.\n *\n * Algorithm:\n *\n * - For every extension space declared in `loadedSpaces` (`'app'`\n * excluded — the per-space verifier is scoped to extension members;\n * the app is verified through the aggregate path):\n * - If no contract-space dir on disk → `declaredButUnmigrated`.\n * - Else if `markerRowsBySpace` lacks an entry → no violation here;\n * the live-DB compare done outside this helper is where the\n * absence shows up.\n * - Else compare marker hash / invariants vs. on-disk head hash /\n * invariants → `hashMismatch` / `invariantsMismatch` on drift.\n * - For every contract-space dir on disk that is not in `loadedSpaces` →\n * `orphanSpaceDir`.\n * - For every marker row whose `space` is not in `loadedSpaces` →\n * `orphanMarker`. The app-space marker is always loaded (`'app'` is\n * in `loadedSpaces` by definition).\n *\n * Output is deterministic: violations are sorted first by `kind`\n * (`declaredButUnmigrated` → `orphanMarker` → `orphanSpaceDir` →\n * `hashMismatch` → `invariantsMismatch`) then by `spaceId`. Two callers\n * passing equivalent inputs see byte-identical violation lists.\n *\n * Synchronous, pure, no I/O. **Does not import the extension descriptor**\n * (the inputs are pre-resolved by the caller); the verifier reads only\n * the user repo, not `node_modules`.\n */\nexport function verifyContractSpaces(\n inputs: VerifyContractSpacesInputs,\n): VerifyContractSpacesResult {\n const violations: SpaceVerifierViolation[] = [];\n\n for (const spaceId of [...inputs.loadedSpaces].sort()) {\n if (spaceId === APP_SPACE_ID) continue;\n\n if (!inputs.spaceDirsOnDisk.includes(spaceId)) {\n violations.push({\n kind: 'declaredButUnmigrated',\n spaceId,\n remediation: `Extension '${spaceId}' is declared in extensionPacks but has not been emitted; run \\`prisma-next migrate\\`.`,\n });\n continue;\n }\n\n const head = inputs.headRefsBySpace.get(spaceId);\n const marker = inputs.markerRowsBySpace.get(spaceId);\n if (!head || !marker) {\n continue;\n }\n\n if (head.hash !== marker.hash) {\n violations.push({\n kind: 'hashMismatch',\n spaceId,\n priorHeadHash: head.hash,\n markerHash: marker.hash,\n remediation: `Marker row for space '${spaceId}' is keyed at ${marker.hash}, but the on-disk ${join('migrations', spaceId, 'contract.json')} resolves to ${head.hash}. Run \\`prisma-next db update\\` to advance the database, or \\`prisma-next migrate\\` if the descriptor was bumped without re-emitting.`,\n });\n continue;\n }\n\n const onDiskInvariants = [...head.invariants].sort();\n const markerInvariants = new Set(marker.invariants);\n const missing = onDiskInvariants.filter((id) => !markerInvariants.has(id));\n if (missing.length > 0) {\n violations.push({\n kind: 'invariantsMismatch',\n spaceId,\n onDiskInvariants,\n markerInvariants: [...marker.invariants].sort(),\n 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.`,\n });\n }\n }\n\n for (const dir of [...inputs.spaceDirsOnDisk].sort()) {\n if (!inputs.loadedSpaces.has(dir)) {\n violations.push({\n kind: 'orphanSpaceDir',\n spaceId: dir,\n remediation: `Orphan contract-space directory \\`${join('migrations', dir)}/\\` for an extension not in extensionPacks; remove the directory or re-add the extension.`,\n });\n }\n }\n\n for (const space of [...inputs.markerRowsBySpace.keys()].sort()) {\n if (!inputs.loadedSpaces.has(space)) {\n violations.push({\n kind: 'orphanMarker',\n spaceId: space,\n remediation: `Orphan marker row for space '${space}' (no longer in extensionPacks); remediation: manually delete the row from \\`prisma_contract.marker\\`.`,\n });\n }\n }\n\n if (violations.length === 0) {\n return { ok: true };\n }\n\n const kindOrder: Record<SpaceVerifierViolation['kind'], number> = {\n declaredButUnmigrated: 0,\n orphanMarker: 1,\n orphanSpaceDir: 2,\n hashMismatch: 3,\n invariantsMismatch: 4,\n };\n\n violations.sort((a, b) => {\n const k = kindOrder[a.kind] - kindOrder[b.kind];\n if (k !== 0) return k;\n if (a.spaceId < b.spaceId) return -1;\n if (a.spaceId > b.spaceId) return 1;\n return 0;\n });\n\n return { ok: false, violations };\n}\n","import { readFile } from 'node:fs/promises';\nimport { join } from 'pathe';\nimport { errorInvalidJson, errorMissingFile } from './errors';\nimport { assertValidSpaceId } from './space-layout';\n\nfunction hasErrnoCode(error: unknown, code: string): boolean {\n return error instanceof Error && (error as { code?: string }).code === code;\n}\n\n/**\n * Read the on-disk contract value for a contract space\n * (`<projectMigrationsDir>/<spaceId>/contract.json`). Returns the parsed\n * JSON value as `unknown` — callers that need a typed contract validate\n * via their family's `validateContract` to surface schema issues.\n *\n * Companion to {@link import('./read-contract-space-head-ref').readContractSpaceHeadRef}\n * — same ENOENT-throws / corrupt-file-error semantics. Returns the\n * canonical-JSON value the framework wrote during emit, so re-running\n * this helper across machines / runs yields a byte-identical value.\n */\nexport async function readContractSpaceContract(\n projectMigrationsDir: string,\n spaceId: string,\n): Promise<unknown> {\n assertValidSpaceId(spaceId);\n\n const filePath = join(projectMigrationsDir, spaceId, 'contract.json');\n\n let raw: string;\n try {\n raw = await readFile(filePath, 'utf-8');\n } catch (error) {\n if (hasErrnoCode(error, 'ENOENT')) {\n throw errorMissingFile('contract.json', join(projectMigrationsDir, spaceId));\n }\n throw error;\n }\n\n try {\n return JSON.parse(raw);\n } catch (e) {\n throw errorInvalidJson(filePath, e instanceof Error ? e.message : String(e));\n }\n}\n"],"mappings":";;;;;;;;;;;AAoBA,MAAM,mBAAmB;AAEzB,SAAgB,eAAe,SAA0C;CACvE,OAAO,iBAAiB,KAAK,QAAQ;;AAGvC,SAAgB,mBAAmB,SAAkD;CACnF,IAAI,CAAC,eAAe,QAAQ,EAC1B,MAAM,oBAAoB,QAAQ;;;;;;;;;;;;;;AAgBtC,SAAgB,wBAAwB,sBAA8B,SAAyB;CAC7F,mBAAmB,QAAQ;CAC3B,OAAO,KAAK,sBAAsB,QAAQ;;;;ACtC5C,SAASA,eAAa,OAAgB,MAAuB;CAC3D,OAAO,iBAAiB,SAAU,MAA4B,SAAS;;;;;;;;;;;;;;;AAgBzE,eAAsB,yBACpB,sBACA,SACsC;CACtC,mBAAmB,QAAQ;CAE3B,MAAM,WAAW,KAAK,sBAAsB,SAAS,QAAQ,YAAY;CAEzE,IAAI;CACJ,IAAI;EACF,MAAM,MAAM,SAAS,UAAU,QAAQ;UAChC,OAAO;EACd,IAAIA,e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QAAQ;CAE3B,MAAM,WAAW,KAAK,sBAAsB,SAAS,gBAAgB;CAErE,IAAI;CACJ,IAAI;EACF,MAAM,MAAM,SAAS,UAAU,QAAQ;UAChC,OAAO;EACd,IAAI,aAAa,OAAO,SAAS,EAC/B,MAAM,iBAAiB,iBAAiB,KAAK,sBAAsB,QAAQ,CAAC;EAE9E,MAAM;;CAGR,IAAI;EACF,OAAO,KAAK,MAAM,IAAI;UACf,GAAG;EACV,MAAM,iBAAiB,UAAU,aAAa,QAAQ,EAAE,UAAU,OAAO,EAAE,CAAC"}
package/package.json CHANGED
@@ -1,23 +1,24 @@
1
1
  {
2
2
  "name": "@prisma-next/migration-tools",
3
- "version": "0.5.0-dev.9",
3
+ "version": "0.5.0",
4
+ "license": "Apache-2.0",
4
5
  "type": "module",
5
6
  "sideEffects": false,
6
- "description": "On-disk migration persistence, attestation, and chain reconstruction for Prisma Next",
7
+ "description": "On-disk migration persistence, hash verification, and chain reconstruction for Prisma Next",
7
8
  "dependencies": {
8
9
  "arktype": "^2.1.29",
9
10
  "pathe": "^2.0.3",
10
- "prettier": "^3.6.2",
11
- "@prisma-next/framework-components": "0.5.0-dev.9",
12
- "@prisma-next/utils": "0.5.0-dev.9",
13
- "@prisma-next/contract": "0.5.0-dev.9"
11
+ "prettier": "^3.8.3",
12
+ "@prisma-next/contract": "0.5.0",
13
+ "@prisma-next/framework-components": "0.5.0",
14
+ "@prisma-next/utils": "0.5.0"
14
15
  },
15
16
  "devDependencies": {
16
- "tsdown": "0.18.4",
17
+ "tsdown": "0.22.0",
17
18
  "typescript": "5.9.3",
18
- "vitest": "4.0.17",
19
- "@prisma-next/tsconfig": "0.0.0",
20
- "@prisma-next/tsdown": "0.0.0"
19
+ "vitest": "4.1.5",
20
+ "@prisma-next/tsdown": "0.0.0",
21
+ "@prisma-next/tsconfig": "0.0.0"
21
22
  },
22
23
  "files": [
23
24
  "dist",
@@ -27,21 +28,37 @@
27
28
  "node": ">=20"
28
29
  },
29
30
  "exports": {
30
- "./types": {
31
- "types": "./dist/exports/types.d.mts",
32
- "import": "./dist/exports/types.mjs"
31
+ "./metadata": {
32
+ "types": "./dist/exports/metadata.d.mts",
33
+ "import": "./dist/exports/metadata.mjs"
34
+ },
35
+ "./package": {
36
+ "types": "./dist/exports/package.d.mts",
37
+ "import": "./dist/exports/package.mjs"
38
+ },
39
+ "./graph": {
40
+ "types": "./dist/exports/graph.d.mts",
41
+ "import": "./dist/exports/graph.mjs"
42
+ },
43
+ "./errors": {
44
+ "types": "./dist/exports/errors.d.mts",
45
+ "import": "./dist/exports/errors.mjs"
33
46
  },
34
47
  "./io": {
35
48
  "types": "./dist/exports/io.d.mts",
36
49
  "import": "./dist/exports/io.mjs"
37
50
  },
38
- "./attestation": {
39
- "types": "./dist/exports/attestation.d.mts",
40
- "import": "./dist/exports/attestation.mjs"
51
+ "./hash": {
52
+ "types": "./dist/exports/hash.d.mts",
53
+ "import": "./dist/exports/hash.mjs"
41
54
  },
42
- "./dag": {
43
- "types": "./dist/exports/dag.d.mts",
44
- "import": "./dist/exports/dag.mjs"
55
+ "./invariants": {
56
+ "types": "./dist/exports/invariants.d.mts",
57
+ "import": "./dist/exports/invariants.mjs"
58
+ },
59
+ "./migration-graph": {
60
+ "types": "./dist/exports/migration-graph.d.mts",
61
+ "import": "./dist/exports/migration-graph.mjs"
45
62
  },
46
63
  "./refs": {
47
64
  "types": "./dist/exports/refs.d.mts",
@@ -59,6 +76,14 @@
59
76
  "types": "./dist/exports/migration.d.mts",
60
77
  "import": "./dist/exports/migration.mjs"
61
78
  },
79
+ "./spaces": {
80
+ "types": "./dist/exports/spaces.d.mts",
81
+ "import": "./dist/exports/spaces.mjs"
82
+ },
83
+ "./aggregate": {
84
+ "types": "./dist/exports/aggregate.d.mts",
85
+ "import": "./dist/exports/aggregate.mjs"
86
+ },
62
87
  "./package.json": "./package.json"
63
88
  },
64
89
  "repository": {