@prisma-next/migration-tools 0.11.0-dev.5 → 0.11.0-dev.51

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 (133) hide show
  1. package/README.md +4 -4
  2. package/dist/{errors-DGYwcwXs.mjs → errors-4YabujxZ.mjs} +15 -21
  3. package/dist/errors-4YabujxZ.mjs.map +1 -0
  4. package/dist/exports/aggregate.d.mts +275 -179
  5. package/dist/exports/aggregate.d.mts.map +1 -1
  6. package/dist/exports/aggregate.mjs +363 -184
  7. package/dist/exports/aggregate.mjs.map +1 -1
  8. package/dist/exports/enumerate-migration-spaces.d.mts +53 -0
  9. package/dist/exports/enumerate-migration-spaces.d.mts.map +1 -0
  10. package/dist/exports/enumerate-migration-spaces.mjs +107 -0
  11. package/dist/exports/enumerate-migration-spaces.mjs.map +1 -0
  12. package/dist/exports/errors.d.mts +2 -2
  13. package/dist/exports/errors.d.mts.map +1 -1
  14. package/dist/exports/errors.mjs +1 -1
  15. package/dist/exports/graph.d.mts +1 -1
  16. package/dist/exports/hash.d.mts +8 -9
  17. package/dist/exports/hash.d.mts.map +1 -1
  18. package/dist/exports/hash.mjs +1 -1
  19. package/dist/exports/invariants.d.mts +1 -1
  20. package/dist/exports/invariants.d.mts.map +1 -1
  21. package/dist/exports/invariants.mjs +1 -1
  22. package/dist/exports/io.d.mts +2 -83
  23. package/dist/exports/io.mjs +1 -1
  24. package/dist/exports/metadata.d.mts +2 -2
  25. package/dist/exports/migration-graph.d.mts +9 -2
  26. package/dist/exports/migration-graph.d.mts.map +1 -0
  27. package/dist/exports/migration-graph.mjs +16 -2
  28. package/dist/exports/migration-graph.mjs.map +1 -0
  29. package/dist/exports/migration-list-graph-topology.d.mts +13 -0
  30. package/dist/exports/migration-list-graph-topology.d.mts.map +1 -0
  31. package/dist/exports/migration-list-graph-topology.mjs +105 -0
  32. package/dist/exports/migration-list-graph-topology.mjs.map +1 -0
  33. package/dist/exports/migration-list-types.d.mts +2 -0
  34. package/dist/exports/migration-list-types.mjs +1 -0
  35. package/dist/exports/migration-ts.d.mts.map +1 -1
  36. package/dist/exports/migration-ts.mjs.map +1 -1
  37. package/dist/exports/migration.d.mts +5 -6
  38. package/dist/exports/migration.d.mts.map +1 -1
  39. package/dist/exports/migration.mjs +14 -32
  40. package/dist/exports/migration.mjs.map +1 -1
  41. package/dist/exports/package.d.mts +1 -1
  42. package/dist/exports/ref-resolution.d.mts +2 -2
  43. package/dist/exports/ref-resolution.d.mts.map +1 -1
  44. package/dist/exports/ref-resolution.mjs +1 -1
  45. package/dist/exports/ref-resolution.mjs.map +1 -1
  46. package/dist/exports/refs.d.mts +15 -2
  47. package/dist/exports/refs.d.mts.map +1 -0
  48. package/dist/exports/refs.mjs +137 -2
  49. package/dist/exports/refs.mjs.map +1 -0
  50. package/dist/exports/spaces.d.mts +31 -132
  51. package/dist/exports/spaces.d.mts.map +1 -1
  52. package/dist/exports/spaces.mjs +14 -9
  53. package/dist/exports/spaces.mjs.map +1 -1
  54. package/dist/{graph-BrLXqoUc.d.mts → graph-BUZuUeBC.d.mts} +1 -2
  55. package/dist/graph-BUZuUeBC.d.mts.map +1 -0
  56. package/dist/{hash-Cr4WIr4Z.mjs → hash--Y7vCpN3.mjs} +8 -9
  57. package/dist/hash--Y7vCpN3.mjs.map +1 -0
  58. package/dist/{invariants-0daYEzyo.mjs → invariants-CCOAyg6c.mjs} +2 -2
  59. package/dist/{invariants-0daYEzyo.mjs.map → invariants-CCOAyg6c.mjs.map} +1 -1
  60. package/dist/{io-BPLfzvZe.mjs → io-BHl0amF0.mjs} +100 -13
  61. package/dist/io-BHl0amF0.mjs.map +1 -0
  62. package/dist/io-nqFXSSTN.d.mts +124 -0
  63. package/dist/io-nqFXSSTN.d.mts.map +1 -0
  64. package/dist/metadata-Bp9X04gM.d.mts +2 -0
  65. package/dist/{migration-graph-De0dUZoC.d.mts → migration-graph-DtNT-cqc.d.mts} +6 -6
  66. package/dist/migration-graph-DtNT-cqc.d.mts.map +1 -0
  67. package/dist/{migration-graph-nlS4TRpn.mjs → migration-graph-kGBkIZDa.mjs} +6 -26
  68. package/dist/migration-graph-kGBkIZDa.mjs.map +1 -0
  69. package/dist/migration-list-types-BRTuXR8i.d.mts +23 -0
  70. package/dist/migration-list-types-BRTuXR8i.d.mts.map +1 -0
  71. package/dist/op-schema-D5qkXfEf.mjs.map +1 -1
  72. package/dist/{package-DZj8YvD0.d.mts → package-DIttKL7X.d.mts} +1 -1
  73. package/dist/package-DIttKL7X.d.mts.map +1 -0
  74. package/dist/read-contract-space-contract-BS5Oxbgw.mjs +82 -0
  75. package/dist/read-contract-space-contract-BS5Oxbgw.mjs.map +1 -0
  76. package/dist/{refs-BDHo5l_g.mjs → refs-BBKNL45K.mjs} +76 -4
  77. package/dist/refs-BBKNL45K.mjs.map +1 -0
  78. package/dist/{refs-CDaNerhT.d.mts → refs-C8f2IGM8.d.mts} +12 -2
  79. package/dist/refs-C8f2IGM8.d.mts.map +1 -0
  80. package/dist/{read-contract-space-contract-DRueB4Aa.mjs → verify-contract-spaces-BJX5gqtD.mjs} +32 -80
  81. package/dist/verify-contract-spaces-BJX5gqtD.mjs.map +1 -0
  82. package/dist/verify-contract-spaces-T0aiJlBS.d.mts +132 -0
  83. package/dist/verify-contract-spaces-T0aiJlBS.d.mts.map +1 -0
  84. package/package.json +18 -6
  85. package/src/aggregate/aggregate.ts +90 -0
  86. package/src/aggregate/check-integrity.ts +243 -0
  87. package/src/aggregate/loader.ts +156 -334
  88. package/src/aggregate/planner.ts +8 -6
  89. package/src/aggregate/project-schema-to-space.ts +1 -1
  90. package/src/aggregate/strategies/graph-walk.ts +12 -7
  91. package/src/aggregate/strategies/synth.ts +2 -2
  92. package/src/aggregate/types.ts +56 -64
  93. package/src/aggregate/verifier.ts +6 -4
  94. package/src/assert-descriptor-self-consistency.ts +6 -0
  95. package/src/compute-extension-space-apply-path.ts +1 -1
  96. package/src/emit-contract-space-artefacts.ts +4 -3
  97. package/src/enumerate-migration-spaces.ts +127 -0
  98. package/src/errors.ts +17 -2
  99. package/src/exports/aggregate.ts +17 -12
  100. package/src/exports/enumerate-migration-spaces.ts +4 -0
  101. package/src/exports/io.ts +2 -0
  102. package/src/exports/metadata.ts +1 -1
  103. package/src/exports/migration-graph.ts +1 -0
  104. package/src/exports/migration-list-graph-topology.ts +5 -0
  105. package/src/exports/migration-list-types.ts +5 -0
  106. package/src/exports/refs.ts +8 -0
  107. package/src/exports/spaces.ts +3 -0
  108. package/src/graph-membership.ts +17 -0
  109. package/src/graph.ts +0 -1
  110. package/src/hash.ts +7 -8
  111. package/src/integrity-violation.ts +114 -0
  112. package/src/io.ts +139 -14
  113. package/src/metadata.ts +1 -1
  114. package/src/migration-base.ts +10 -30
  115. package/src/migration-graph.ts +7 -35
  116. package/src/migration-list-graph-topology.ts +158 -0
  117. package/src/migration-list-types.ts +21 -0
  118. package/src/read-contract-space-head-ref.ts +5 -2
  119. package/src/refs/snapshot.ts +197 -0
  120. package/src/refs.ts +97 -1
  121. package/src/space-layout.ts +30 -0
  122. package/dist/errors-DGYwcwXs.mjs.map +0 -1
  123. package/dist/exports/io.d.mts.map +0 -1
  124. package/dist/graph-BrLXqoUc.d.mts.map +0 -1
  125. package/dist/hash-Cr4WIr4Z.mjs.map +0 -1
  126. package/dist/io-BPLfzvZe.mjs.map +0 -1
  127. package/dist/metadata-BFX0xdz8.d.mts +0 -2
  128. package/dist/migration-graph-De0dUZoC.d.mts.map +0 -1
  129. package/dist/migration-graph-nlS4TRpn.mjs.map +0 -1
  130. package/dist/package-DZj8YvD0.d.mts.map +0 -1
  131. package/dist/read-contract-space-contract-DRueB4Aa.mjs.map +0 -1
  132. package/dist/refs-BDHo5l_g.mjs.map +0 -1
  133. package/dist/refs-CDaNerhT.d.mts.map +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"refs.d.mts","names":[],"sources":["../../src/refs/snapshot.ts"],"mappings":";;;UAQiB,UAAA;EAAA,SACN,QAAA;EAAA,SACA,WAAW;AAAA;AAAA,iBAgEA,gBAAA,CACpB,OAAA,UACA,IAAA,UACA,QAAA,EAAU,UAAA,GACT,OAAO;AAAA,iBA4BY,eAAA,CAAgB,OAAA,UAAiB,IAAA,WAAe,OAAO,CAAC,UAAA;AAAA,iBAiCxD,iBAAA,CAAkB,OAAA,UAAiB,IAAA,WAAe,OAAO;AAAA,iBASzD,cAAA,CACpB,OAAA,UACA,IAAA,UACA,KAAA,EAAO,QAAA,EACP,QAAA,EAAU,UAAA,GACT,OAAA;AAAA,iBA4BmB,eAAA,CAAgB,OAAA,UAAiB,IAAA,WAAe,OAAO"}
@@ -1,2 +1,137 @@
1
- import { a as validateRefName, i as resolveRef, n as readRef, o as validateRefValue, r as readRefs, s as writeRef, t as deleteRef } from "../refs-BDHo5l_g.mjs";
2
- export { deleteRef, readRef, readRefs, resolveRef, validateRefName, validateRefValue, writeRef };
1
+ import { m as errorInvalidRefName, p as errorInvalidRefFile, t as MigrationToolsError } from "../errors-4YabujxZ.mjs";
2
+ import { c as validateRefValue, i as readRefs, l as writeRef, n as deleteRef, o as resolveRef, r as readRef, s as validateRefName } from "../refs-BBKNL45K.mjs";
3
+ import { basename, dirname, join } from "pathe";
4
+ import { access, mkdir, readFile, rename, unlink, writeFile } from "node:fs/promises";
5
+ import { type } from "arktype";
6
+ import { randomBytes } from "node:crypto";
7
+ import { canonicalizeJson } from "@prisma-next/framework-components/utils";
8
+ //#region src/refs/snapshot.ts
9
+ const ContractIrSchema = type({
10
+ targetFamily: "string",
11
+ target: "string",
12
+ profileHash: "string",
13
+ storage: type({ storageHash: "string" }),
14
+ models: "object"
15
+ });
16
+ function hasErrnoCode(error, code) {
17
+ return error instanceof Error && error.code === code;
18
+ }
19
+ function snapshotJsonPath(refsDir, name) {
20
+ return join(refsDir, `${name}.contract.json`);
21
+ }
22
+ function snapshotDtsPath(refsDir, name) {
23
+ return join(refsDir, `${name}.contract.d.ts`);
24
+ }
25
+ function tmpPathFor(finalPath) {
26
+ return join(dirname(finalPath), `.${basename(finalPath)}.${Date.now()}-${randomBytes(4).toString("hex")}.tmp`);
27
+ }
28
+ async function atomicWriteFile(finalPath, content) {
29
+ await mkdir(dirname(finalPath), { recursive: true });
30
+ const tmpPath = tmpPathFor(finalPath);
31
+ await writeFile(tmpPath, content);
32
+ await rename(tmpPath, finalPath);
33
+ }
34
+ async function unlinkIfExists(filePath) {
35
+ try {
36
+ await unlink(filePath);
37
+ } catch (error) {
38
+ if (hasErrnoCode(error, "ENOENT")) return;
39
+ throw error;
40
+ }
41
+ }
42
+ function parseContractSnapshotJson(filePath, raw) {
43
+ let parsed;
44
+ try {
45
+ parsed = JSON.parse(raw);
46
+ } catch {
47
+ throw errorInvalidRefFile(filePath, "Failed to parse as JSON");
48
+ }
49
+ const result = ContractIrSchema(parsed);
50
+ if (result instanceof type.errors) throw errorInvalidRefFile(filePath, result.summary);
51
+ return result;
52
+ }
53
+ async function writeRefSnapshot(refsDir, name, snapshot) {
54
+ if (!validateRefName(name)) throw errorInvalidRefName(name);
55
+ const jsonPath = snapshotJsonPath(refsDir, name);
56
+ const dtsPath = snapshotDtsPath(refsDir, name);
57
+ const jsonContent = `${canonicalizeJson(snapshot.contract)}\n`;
58
+ const dtsContent = snapshot.contractDts.endsWith("\n") ? snapshot.contractDts : `${snapshot.contractDts}\n`;
59
+ try {
60
+ await atomicWriteFile(jsonPath, jsonContent);
61
+ } catch (error) {
62
+ await unlinkIfExists(jsonPath);
63
+ throw error;
64
+ }
65
+ try {
66
+ await atomicWriteFile(dtsPath, dtsContent);
67
+ } catch (error) {
68
+ await unlinkIfExists(jsonPath);
69
+ await unlinkIfExists(dtsPath);
70
+ throw error;
71
+ }
72
+ }
73
+ async function readRefSnapshot(refsDir, name) {
74
+ if (!validateRefName(name)) throw errorInvalidRefName(name);
75
+ const jsonPath = snapshotJsonPath(refsDir, name);
76
+ const dtsPath = snapshotDtsPath(refsDir, name);
77
+ let raw;
78
+ try {
79
+ raw = await readFile(jsonPath, "utf-8");
80
+ } catch (error) {
81
+ if (hasErrnoCode(error, "ENOENT")) return null;
82
+ throw error;
83
+ }
84
+ const contract = parseContractSnapshotJson(jsonPath, raw);
85
+ let contractDts;
86
+ try {
87
+ contractDts = await readFile(dtsPath, "utf-8");
88
+ } catch (error) {
89
+ if (hasErrnoCode(error, "ENOENT")) throw errorInvalidRefFile(dtsPath, "Missing paired contract.d.ts snapshot file");
90
+ throw error;
91
+ }
92
+ return {
93
+ contract,
94
+ contractDts
95
+ };
96
+ }
97
+ async function deleteRefSnapshot(refsDir, name) {
98
+ if (!validateRefName(name)) throw errorInvalidRefName(name);
99
+ await unlinkIfExists(snapshotJsonPath(refsDir, name));
100
+ await unlinkIfExists(snapshotDtsPath(refsDir, name));
101
+ }
102
+ async function writeRefPaired(refsDir, name, entry, snapshot) {
103
+ await writeRefSnapshot(refsDir, name, snapshot);
104
+ try {
105
+ await writeRef(refsDir, name, entry);
106
+ } catch (writeError) {
107
+ try {
108
+ await deleteRefSnapshot(refsDir, name);
109
+ } catch {}
110
+ throw writeError;
111
+ }
112
+ }
113
+ function isUnknownRefError(error) {
114
+ return MigrationToolsError.is(error) && error.code === "MIGRATION.UNKNOWN_REF";
115
+ }
116
+ async function snapshotFilesExist(refsDir, name) {
117
+ if (!validateRefName(name)) throw errorInvalidRefName(name);
118
+ const paths = [snapshotJsonPath(refsDir, name), snapshotDtsPath(refsDir, name)];
119
+ return (await Promise.allSettled(paths.map((filePath) => access(filePath)))).some((result) => result.status === "fulfilled");
120
+ }
121
+ async function deleteRefPaired(refsDir, name) {
122
+ if (await snapshotFilesExist(refsDir, name)) {
123
+ try {
124
+ await deleteRef(refsDir, name);
125
+ } catch (error) {
126
+ if (!isUnknownRefError(error)) throw error;
127
+ }
128
+ await deleteRefSnapshot(refsDir, name);
129
+ return;
130
+ }
131
+ await deleteRef(refsDir, name);
132
+ await deleteRefSnapshot(refsDir, name);
133
+ }
134
+ //#endregion
135
+ export { deleteRef, deleteRefPaired, deleteRefSnapshot, readRef, readRefSnapshot, readRefs, resolveRef, validateRefName, validateRefValue, writeRef, writeRefPaired, writeRefSnapshot };
136
+
137
+ //# sourceMappingURL=refs.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"refs.mjs","names":[],"sources":["../../src/refs/snapshot.ts"],"sourcesContent":["import { randomBytes } from 'node:crypto';\nimport { access, mkdir, readFile, rename, unlink, writeFile } from 'node:fs/promises';\nimport { canonicalizeJson } from '@prisma-next/framework-components/utils';\nimport { type } from 'arktype';\nimport { basename, dirname, join } from 'pathe';\nimport { errorInvalidRefFile, errorInvalidRefName, MigrationToolsError } from '../errors';\nimport { deleteRef, type RefEntry, validateRefName, writeRef } from '../refs';\n\nexport interface ContractIR {\n readonly contract: unknown;\n readonly contractDts: string;\n}\n\nconst ContractIrSchema = type({\n targetFamily: 'string',\n target: 'string',\n profileHash: 'string',\n storage: type({\n storageHash: 'string',\n }),\n models: 'object',\n});\n\nfunction hasErrnoCode(error: unknown, code: string): boolean {\n return error instanceof Error && (error as { code?: string }).code === code;\n}\n\nfunction snapshotJsonPath(refsDir: string, name: string): string {\n return join(refsDir, `${name}.contract.json`);\n}\n\nfunction snapshotDtsPath(refsDir: string, name: string): string {\n return join(refsDir, `${name}.contract.d.ts`);\n}\n\nfunction tmpPathFor(finalPath: string): string {\n const dir = dirname(finalPath);\n const fileName = basename(finalPath);\n return join(dir, `.${fileName}.${Date.now()}-${randomBytes(4).toString('hex')}.tmp`);\n}\n\nasync function atomicWriteFile(finalPath: string, content: string): Promise<void> {\n const dir = dirname(finalPath);\n await mkdir(dir, { recursive: true });\n const tmpPath = tmpPathFor(finalPath);\n await writeFile(tmpPath, content);\n await rename(tmpPath, finalPath);\n}\n\nasync function unlinkIfExists(filePath: string): Promise<void> {\n try {\n await unlink(filePath);\n } catch (error) {\n if (hasErrnoCode(error, 'ENOENT')) return;\n throw error;\n }\n}\n\nfunction parseContractSnapshotJson(filePath: string, raw: string): unknown {\n let parsed: unknown;\n try {\n parsed = JSON.parse(raw);\n } catch {\n throw errorInvalidRefFile(filePath, 'Failed to parse as JSON');\n }\n\n const result = ContractIrSchema(parsed);\n if (result instanceof type.errors) {\n throw errorInvalidRefFile(filePath, result.summary);\n }\n\n return result;\n}\n\nexport async function writeRefSnapshot(\n refsDir: string,\n name: string,\n snapshot: ContractIR,\n): Promise<void> {\n if (!validateRefName(name)) {\n throw errorInvalidRefName(name);\n }\n\n const jsonPath = snapshotJsonPath(refsDir, name);\n const dtsPath = snapshotDtsPath(refsDir, name);\n const jsonContent = `${canonicalizeJson(snapshot.contract)}\\n`;\n const dtsContent = snapshot.contractDts.endsWith('\\n')\n ? snapshot.contractDts\n : `${snapshot.contractDts}\\n`;\n\n try {\n await atomicWriteFile(jsonPath, jsonContent);\n } catch (error) {\n await unlinkIfExists(jsonPath);\n throw error;\n }\n\n try {\n await atomicWriteFile(dtsPath, dtsContent);\n } catch (error) {\n await unlinkIfExists(jsonPath);\n await unlinkIfExists(dtsPath);\n throw error;\n }\n}\n\nexport async function readRefSnapshot(refsDir: string, name: string): Promise<ContractIR | null> {\n if (!validateRefName(name)) {\n throw errorInvalidRefName(name);\n }\n\n const jsonPath = snapshotJsonPath(refsDir, name);\n const dtsPath = snapshotDtsPath(refsDir, name);\n\n let raw: string;\n try {\n raw = await readFile(jsonPath, 'utf-8');\n } catch (error) {\n if (hasErrnoCode(error, 'ENOENT')) {\n return null;\n }\n throw error;\n }\n\n const contract = parseContractSnapshotJson(jsonPath, raw);\n\n let contractDts: string;\n try {\n contractDts = await readFile(dtsPath, 'utf-8');\n } catch (error) {\n if (hasErrnoCode(error, 'ENOENT')) {\n throw errorInvalidRefFile(dtsPath, 'Missing paired contract.d.ts snapshot file');\n }\n throw error;\n }\n\n return { contract, contractDts };\n}\n\nexport async function deleteRefSnapshot(refsDir: string, name: string): Promise<void> {\n if (!validateRefName(name)) {\n throw errorInvalidRefName(name);\n }\n\n await unlinkIfExists(snapshotJsonPath(refsDir, name));\n await unlinkIfExists(snapshotDtsPath(refsDir, name));\n}\n\nexport async function writeRefPaired(\n refsDir: string,\n name: string,\n entry: RefEntry,\n snapshot: ContractIR,\n): Promise<void> {\n await writeRefSnapshot(refsDir, name, snapshot);\n try {\n await writeRef(refsDir, name, entry);\n } catch (writeError) {\n try {\n await deleteRefSnapshot(refsDir, name);\n } catch {\n // Rollback failure is secondary; preserve the original writeRef error.\n }\n throw writeError;\n }\n}\n\nfunction isUnknownRefError(error: unknown): boolean {\n return MigrationToolsError.is(error) && error.code === 'MIGRATION.UNKNOWN_REF';\n}\n\nasync function snapshotFilesExist(refsDir: string, name: string): Promise<boolean> {\n if (!validateRefName(name)) {\n throw errorInvalidRefName(name);\n }\n\n const paths = [snapshotJsonPath(refsDir, name), snapshotDtsPath(refsDir, name)];\n const checks = await Promise.allSettled(paths.map((filePath) => access(filePath)));\n return checks.some((result) => result.status === 'fulfilled');\n}\n\nexport async function deleteRefPaired(refsDir: string, name: string): Promise<void> {\n if (await snapshotFilesExist(refsDir, name)) {\n try {\n await deleteRef(refsDir, name);\n } catch (error) {\n if (!isUnknownRefError(error)) {\n throw error;\n }\n }\n await deleteRefSnapshot(refsDir, name);\n return;\n }\n\n await deleteRef(refsDir, name);\n await deleteRefSnapshot(refsDir, name);\n}\n"],"mappings":";;;;;;;;AAaA,MAAM,mBAAmB,KAAK;CAC5B,cAAc;CACd,QAAQ;CACR,aAAa;CACb,SAAS,KAAK,EACZ,aAAa,SACf,CAAC;CACD,QAAQ;AACV,CAAC;AAED,SAAS,aAAa,OAAgB,MAAuB;CAC3D,OAAO,iBAAiB,SAAU,MAA4B,SAAS;AACzE;AAEA,SAAS,iBAAiB,SAAiB,MAAsB;CAC/D,OAAO,KAAK,SAAS,GAAG,KAAK,eAAe;AAC9C;AAEA,SAAS,gBAAgB,SAAiB,MAAsB;CAC9D,OAAO,KAAK,SAAS,GAAG,KAAK,eAAe;AAC9C;AAEA,SAAS,WAAW,WAA2B;CAG7C,OAAO,KAFK,QAAQ,SAEN,GA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,QAAO,MADc,QAAQ,WAAW,MAAM,KAAK,aAAa,OAAO,QAAQ,CAAC,CAAC,GACnE,MAAM,WAAW,OAAO,WAAW,WAAW;AAC9D;AAEA,eAAsB,gBAAgB,SAAiB,MAA6B;CAClF,IAAI,MAAM,mBAAmB,SAAS,IAAI,GAAG;EAC3C,IAAI;GACF,MAAM,UAAU,SAAS,IAAI;EAC/B,SAAS,OAAO;GACd,IAAI,CAAC,kBAAkB,KAAK,GAC1B,MAAM;EAEV;EACA,MAAM,kBAAkB,SAAS,IAAI;EACrC;CACF;CAEA,MAAM,UAAU,SAAS,IAAI;CAC7B,MAAM,kBAAkB,SAAS,IAAI;AACvC"}
@@ -1,5 +1,7 @@
1
- import { t as MigrationOps } from "../package-DZj8YvD0.mjs";
1
+ import { t as MigrationOps } from "../package-DIttKL7X.mjs";
2
+ import { a as VerifyContractSpacesResult, i as VerifyContractSpacesInputs, n as SpaceMarkerRecord, o as listContractSpaceDirectories, r as SpaceVerifierViolation, s as verifyContractSpaces, t as ContractSpaceHeadRecord } from "../verify-contract-spaces-T0aiJlBS.mjs";
2
3
  import { APP_SPACE_ID, ContractSpace, ContractSpaceHeadRef, ContractSpaceHeadRef as ContractSpaceHeadRef$1 } from "@prisma-next/framework-components/control";
4
+ import { PreserveEmptyPredicate, StorageSort } from "@prisma-next/contract/hashing";
3
5
  import { Contract } from "@prisma-next/contract/types";
4
6
 
5
7
  //#region src/assert-descriptor-self-consistency.d.ts
@@ -22,6 +24,8 @@ interface DescriptorSelfConsistencyInputs {
22
24
  */
23
25
  readonly storage: unknown;
24
26
  readonly headRefHash: string;
27
+ readonly shouldPreserveEmpty?: PreserveEmptyPredicate;
28
+ readonly sortStorage?: StorageSort;
25
29
  }
26
30
  /**
27
31
  * Assert that an extension descriptor is self-consistent: the
@@ -258,136 +262,6 @@ interface ContractSpaceArtefactInputs {
258
262
  */
259
263
  declare function emitContractSpaceArtefacts(projectMigrationsDir: string, spaceId: string, inputs: ContractSpaceArtefactInputs): Promise<void>;
260
264
  //#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
265
  //#region src/gather-disk-contract-space-state.d.ts
392
266
  /**
393
267
  * Disk-side inputs to {@link import('./verify-contract-spaces').verifyContractSpaces}
@@ -521,6 +395,31 @@ declare function assertValidSpaceId(spaceId: string): asserts spaceId is ValidSp
521
395
  * relative shape and is symmetric with `pathe.join`.
522
396
  */
523
397
  declare function spaceMigrationDirectory(projectMigrationsDir: string, spaceId: string): string;
398
+ /**
399
+ * Per-space subdirectory name reserved for the ref store
400
+ * (`migrations/<space>/<SPACE_REFS_DIRNAME>/*.json`). Single source of
401
+ * truth: every helper that composes a per-space refs path imports this
402
+ * constant, and the enumerator uses it (via
403
+ * {@link RESERVED_SPACE_SUBDIR_NAMES}) to exclude reserved names from
404
+ * the contract-space candidate list.
405
+ */
406
+ declare const SPACE_REFS_DIRNAME = "refs";
407
+ /**
408
+ * Names reserved as per-space subdirectories of `migrations/<space>/`.
409
+ * Used by the enumerator to filter contract-space candidates so a
410
+ * reserved name (e.g. a top-level `migrations/refs/` left in the wrong
411
+ * place) is never enumerated as a phantom contract space. Currently
412
+ * holds `SPACE_REFS_DIRNAME`; extend if future per-space layouts add
413
+ * more reserved subdirectories.
414
+ */
415
+ declare const RESERVED_SPACE_SUBDIR_NAMES: ReadonlySet<string>;
416
+ /**
417
+ * Resolve the per-space refs directory for `spaceMigrationsDir`
418
+ * (typically the value returned by {@link spaceMigrationDirectory}).
419
+ * Composes the canonical {@link SPACE_REFS_DIRNAME} so callers do not
420
+ * hard-code the literal.
421
+ */
422
+ declare function spaceRefsDirectory(spaceMigrationsDir: string): string;
524
423
  //#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 };
424
+ export { APP_SPACE_ID, type ComputeExtensionSpaceApplyPathInputs, type ContractSpaceArtefactInputs, type ContractSpaceHeadRecord, type ContractSpaceHeadRef, type DescriptorSelfConsistencyInputs, type DiskContractSpaceState, type ExtensionSpaceApplyPathOutcome, RESERVED_SPACE_SUBDIR_NAMES, SPACE_REFS_DIRNAME, 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, spaceRefsDirectory, verifyContractSpaces };
526
425
  //# sourceMappingURL=spaces.d.mts.map
@@ -1 +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":";;;;;;;;;;AAwBA;UAAiB,+BAAA;EAAA,SACN,WAAA;EAAA,SACA,MAAA;EAAA,SACA,YAAA;EADA;;;;;;AAgCX;EAhCW,SASA,OAAA;EAAA,SACA,WAAA;AAAA;;;;;ACXX;;;;;;;;;;;;;ACTA;;iBF0CgB,+BAAA,CAAgC,MAAA,EAAQ,+BAAA;;;;;;AAlCxD;;;;;;;;;;iBCCsB,wBAAA,CACpB,oBAAA,UACA,OAAA,WACC,OAAA,CAAQ,oBAAA;;;;;;ADJX;;;KERY,8BAAA;EAAA,SAEG,IAAA;EAAA,SACA,oBAAA,EAAsB,oBAAA;EFQ1B;;;;;EAAA,SEFI,kBAAA;EFiCgC;;;;EAAA,SE5BhC,OAAA,EAAS,YAAA;;;ADLxB;;;WCWe,mBAAA;AAAA;EAAA,SAEA,IAAA;EAAA,SAA8B,oBAAA,EAAsB,oBAAA;AAAA;EAAA,SAEpD,IAAA;EAAA,SACA,oBAAA,EAAsB,oBAAA;EAAA,SACtB,OAAA;EAAA,SACA,cAAA;IAAA,SAAoC,OAAA;IAAA,SAA0B,EAAA;EAAA;AAAA;EAAA,SAE9D,IAAA;AAAA;;;;;;;;;;;;UAaE,oCAAA;EAAA,SACN,oBAAA;EAAA,SACA,OAAA;EAAA,SACA,iBAAA;EAAA,SACA,uBAAA;AAAA;;;;;;;;AAJX;;;;;;;;;;AA8BA;;;;;;iBAAsB,8BAAA,CACpB,MAAA,EAAQ,oCAAA,GACP,OAAA,CAAQ,8BAAA;;;;;;;;AFlEX;;;;;;;;;;;AAkCA;;;;UGnCiB,eAAA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C,kBAAA,CAAmB,OAAA,mBAA0B,OAAA,IAAW,YAAA;;;AVgCxE;;;;;;;;ACjCA;;iBSmBgB,uBAAA,CAAwB,oBAAA,UAA8B,OAAA"}
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/gather-disk-contract-space-state.ts","../../src/plan-all-spaces.ts","../../src/read-contract-space-contract.ts","../../src/space-layout.ts"],"mappings":";;;;;;;;;;;;;UA0BiB,+BAAA;EAAA,SACN,WAAA;EAAA,SACA,MAAA;EAAA,SACA,YAAA;EAFA;;;;;;;EAAA,SAUA,OAAA;EAAA,SACA,WAAA;EAAA,SACA,mBAAA,GAAsB,sBAAA;EAAA,SACtB,WAAA,GAAc,WAAW;AAAA;;;;AAsBmD;;;;ACrCvF;;;;;;;;;AAG+B;;;iBDkCf,+BAAA,CAAgC,MAAuC,EAA/B,+BAA+B;;;;;;;;AApCvF;;;;;;;;iBCDsB,wBAAA,CACpB,oBAAA,UACA,OAAA,WACC,OAAO,CAAC,oBAAA;;;;;;;;ADFX;KEVY,8BAAA;EAAA,SAEG,IAAA;EAAA,SACA,oBAAA,EAAsB,oBAAA;EFQ1B;;;;;EAAA,SEFI,kBAAA;EFckB;;;;EAAA,SETlB,OAAA,EAAS,YAAA;EFgCR;;;;AAAuE;EAAvE,SE1BD,mBAAA;AAAA;EAAA,SAEA,IAAA;EAAA,SAA8B,oBAAA,EAAsB,oBAAA;AAAA;EAAA,SAEpD,IAAA;EAAA,SACA,oBAAA,EAAsB,oBAAA;EAAA,SACtB,OAAA;EAAA,SACA,cAAA;IAAA,SAAoC,OAAA;IAAA,SAA0B,EAAA;EAAA;AAAA;EAAA,SAE9D,IAAA;AAAA;;AA7Bf;;;;;;;;;;UA0CiB,oCAAA;EAAA,SACN,oBA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Ae,SAAA;EAAA,SACf,WAAA,EAAa,SAAS;AAAA;AAAA,UAGhB,eAAA;EAAA,SACN,OAAA;EAAA,SACA,iBAAA,WAA4B,QAAQ;AAAA;APuC/C;;;;AAAuF;;;;ACrCvF;;;;;;;;;AAG+B;;;;ACZ/B;;;;;;AF8CA,iBOTgB,aAAA,qBAAA,CACd,MAAA,WAAiB,cAAA,CAAe,SAAA,KAChC,SAAA,GAAY,KAAA,EAAO,cAAA,CAAe,SAAA,eAAwB,QAAA,cAChD,eAAA,CAAgB,QAAA;;;;;;;;;;AP9B5B;;;;iBQNsB,yBAAA,CACpB,oBAAA,UACA,OAAA,WACC,OAAO;;;;;;;;ARGV;;KSbY,YAAA;EAAA,SAAmC,OAAO;AAAA;AAAA,iBAStC,cAAA,CAAe,OAAA,WAAkB,OAAA,IAAW,YAAY;AAAA,iBAIxD,kBAAA,CAAmB,OAAA,mBAA0B,OAAA,IAAW,YAAY;;;;;;;;ATchD;AAsBpC;;;;iBSlBgB,uBAAA,CAAwB,oBAAA,UAA8B,OAAe;;;;ARnBrF;;;;;cQgCa,kBAAA;;;;AR7BkB;;;;ACZ/B;cOmDa,2BAAA,EAA6B,WAAW;;;;;;;iBAQrC,kBAAA,CAAmB,kBAA0B"}
@@ -1,8 +1,10 @@
1
- import { r as errorDescriptorHeadHashMismatch, s as errorDuplicateSpaceId } from "../errors-DGYwcwXs.mjs";
2
- import { s as readMigrationsDir } from "../io-BPLfzvZe.mjs";
1
+ import { o as errorDuplicateSpaceId, r as errorDescriptorHeadHashMismatch } from "../errors-4YabujxZ.mjs";
2
+ import { s as readMigrationsDir } from "../io-BHl0amF0.mjs";
3
3
  import "../constants-DWV9_o2Z.mjs";
4
- import { l as reconstructGraph, o as findPathWithDecision } from "../migration-graph-nlS4TRpn.mjs";
5
- import { a as APP_SPACE_ID, c as spaceMigrationDirectory, i as readContractSpaceHeadRef, n as listContractSpaceDirectories, o as assertValidSpaceId, r as verifyContractSpaces, s as isValidSpaceId, t as readContractSpaceContract } from "../read-contract-space-contract-DRueB4Aa.mjs";
4
+ import { l as reconstructGraph, o as findPathWithDecision } from "../migration-graph-kGBkIZDa.mjs";
5
+ import { a as SPACE_REFS_DIRNAME, c as spaceMigrationDirectory, i as RESERVED_SPACE_SUBDIR_NAMES, l as spaceRefsDirectory, n as verifyContractSpaces, o as assertValidSpaceId, r as APP_SPACE_ID, s as isValidSpaceId, t as listContractSpaceDirectories } from "../verify-contract-spaces-BJX5gqtD.mjs";
6
+ import { n as readContractSpaceHeadRef, t as readContractSpaceContract } from "../read-contract-space-contract-BS5Oxbgw.mjs";
7
+ import { ifDefined } from "@prisma-next/utils/defined";
6
8
  import { join } from "pathe";
7
9
  import { mkdir, writeFile } from "node:fs/promises";
8
10
  import { canonicalizeJson } from "@prisma-next/framework-components/utils";
@@ -46,7 +48,9 @@ function assertDescriptorSelfConsistency(inputs) {
46
48
  const recomputed = computeStorageHash({
47
49
  target: inputs.target,
48
50
  targetFamily: inputs.targetFamily,
49
- storage: normalizedStorage
51
+ storage: normalizedStorage,
52
+ ...ifDefined("shouldPreserveEmpty", inputs.shouldPreserveEmpty),
53
+ ...ifDefined("sortStorage", inputs.sortStorage)
50
54
  });
51
55
  if (recomputed !== inputs.headRefHash) throw errorDescriptorHeadHashMismatch({
52
56
  extensionId: inputs.extensionId,
@@ -83,7 +87,7 @@ async function computeExtensionSpaceApplyPath(inputs) {
83
87
  const { projectMigrationsDir, spaceId, currentMarkerHash, currentMarkerInvariants } = inputs;
84
88
  const contractSpaceHeadRef = await readContractSpaceHeadRef(projectMigrationsDir, spaceId);
85
89
  if (contractSpaceHeadRef === null) return { kind: "contractSpaceHeadRefMissing" };
86
- const packages = await readMigrationsDir(spaceMigrationDirectory(projectMigrationsDir, spaceId));
90
+ const { packages } = await readMigrationsDir(spaceMigrationDirectory(projectMigrationsDir, spaceId));
87
91
  const graph = reconstructGraph(packages);
88
92
  const fromHash = currentMarkerHash ?? "sha256:empty";
89
93
  const required = new Set(contractSpaceHeadRef.invariants.filter((id) => !currentMarkerInvariants.includes(id)));
@@ -186,7 +190,8 @@ function contractSpaceFromJson(inputs) {
186
190
  async function emitContractSpaceArtefacts(projectMigrationsDir, spaceId, inputs) {
187
191
  assertValidSpaceId(spaceId);
188
192
  const dir = join(projectMigrationsDir, spaceId);
189
- await mkdir(join(dir, "refs"), { recursive: true });
193
+ const refsDir = spaceRefsDirectory(dir);
194
+ await mkdir(refsDir, { recursive: true });
190
195
  await writeFile(join(dir, "contract.json"), `${canonicalizeJson(inputs.contract)}\n`);
191
196
  await writeFile(join(dir, "contract.d.ts"), inputs.contractDts);
192
197
  const sortedInvariants = [...inputs.headRef.invariants].sort();
@@ -194,7 +199,7 @@ async function emitContractSpaceArtefacts(projectMigrationsDir, spaceId, inputs)
194
199
  hash: inputs.headRef.hash,
195
200
  invariants: sortedInvariants
196
201
  });
197
- await writeFile(join(dir, "refs", "head.json"), `${headJson}\n`);
202
+ await writeFile(join(refsDir, "head.json"), `${headJson}\n`);
198
203
  }
199
204
  //#endregion
200
205
  //#region src/gather-disk-contract-space-state.ts
@@ -275,6 +280,6 @@ function planAllSpaces(inputs, planSpace) {
275
280
  }));
276
281
  }
277
282
  //#endregion
278
- export { APP_SPACE_ID, assertDescriptorSelfConsistency, assertValidSpaceId, computeExtensionSpaceApplyPath, contractSpaceFromJson, emitContractSpaceArtefacts, gatherDiskContractSpaceState, isValidSpaceId, listContractSpaceDirectories, planAllSpaces, readContractSpaceContract, readContractSpaceHeadRef, spaceMigrationDirectory, verifyContractSpaces };
283
+ export { APP_SPACE_ID, RESERVED_SPACE_SUBDIR_NAMES, SPACE_REFS_DIRNAME, assertDescriptorSelfConsistency, assertValidSpaceId, computeExtensionSpaceApplyPath, contractSpaceFromJson, emitContractSpaceArtefacts, gatherDiskContractSpaceState, isValidSpaceId, listContractSpaceDirectories, planAllSpaces, readContractSpaceContract, readContractSpaceHeadRef, spaceMigrationDirectory, spaceRefsDirectory, verifyContractSpaces };
279
284
 
280
285
  //# sourceMappingURL=spaces.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"spaces.mjs","names":[],"sources":["../../src/assert-descriptor-self-consistency.ts","../../src/compute-extension-space-apply-path.ts","../../src/contract-space-from-json.ts","../../src/emit-contract-space-artefacts.ts","../../src/gather-disk-contract-space-state.ts","../../src/plan-all-spaces.ts"],"sourcesContent":["import { computeStorageHash } from '@prisma-next/contract/hashing';\nimport { errorDescriptorHeadHashMismatch } from './errors';\n\nfunction stripNamespaceKinds(storage: Record<string, unknown>): Record<string, unknown> {\n const namespaces = storage['namespaces'] as Record<string, Record<string, unknown>> | undefined;\n if (!namespaces) return storage;\n const stripped: Record<string, Record<string, unknown>> = {};\n for (const [nsId, ns] of Object.entries(namespaces)) {\n if (ns && typeof ns === 'object') {\n const { kind: _kind, ...rest } = ns as Record<string, unknown>;\n stripped[nsId] = rest as Record<string, unknown>;\n } else {\n stripped[nsId] = ns;\n }\n }\n return { ...storage, namespaces: stripped };\n}\n\n/**\n * Inputs the helper needs to recompute the descriptor's storage hash and\n * compare it to the published `headRef.hash`. Kept structural so the SQL\n * family (and any future target family) can compose the check without\n * coupling to its own descriptor types.\n */\nexport interface DescriptorSelfConsistencyInputs {\n readonly extensionId: string;\n readonly target: string;\n readonly targetFamily: string;\n /**\n * Family-specific storage object. Typed as `unknown` so callers can\n * pass their own narrow storage shape (e.g. `SqlStorage`) without an\n * inline cast — the helper canonicalises through `JSON.stringify`\n * inside {@link computeStorageHash} and only requires a plain\n * record-shaped value at runtime.\n */\n readonly storage: unknown;\n readonly headRefHash: string;\n}\n\n/**\n * Assert that an extension descriptor is self-consistent: the\n * `headRef.hash` it publishes must match the canonical hash recomputed\n * from its `contractSpace.contractJson`.\n *\n * Recomputes via {@link computeStorageHash} — the same canonical-JSON\n * pipeline the descriptor's own emit pipeline produced the hash with —\n * over `(target, targetFamily, storage)`. Mismatch indicates the\n * extension author bumped `contractJson` without rerunning emit, leaving\n * the descriptor's `headRef.hash` stale; the consumer-side helpers\n * (drift detection, on-disk artefact emission, runner marker writes) all\n * trust `headRef.hash` as the canonical identity, so a stale value would\n * silently corrupt every downstream boundary.\n *\n * Synchronous, pure, no I/O. Throws\n * `MIGRATION.DESCRIPTOR_HEAD_HASH_MISMATCH` on failure with both the\n * recomputed and published hashes in `details` so callers can surface a\n * clear remediation hint without re-deriving them.\n */\nexport function assertDescriptorSelfConsistency(inputs: DescriptorSelfConsistencyInputs): void {\n // The published `storage.storageHash` is the *output* of the production\n // emit pipeline's `computeStorageHash` call, computed over a storage\n // object that did not yet carry `storageHash`. Recomputing against the\n // published storage as-is would feed the result back into its own input\n // and produce a different digest. Strip `storageHash` before\n // recomputing so the helper sees the same canonical shape the\n // descriptor's authoring pipeline saw.\n // The helper requires only a plain record-shaped storage value at\n // runtime; a single cast here keeps the public input type\n // family-agnostic (`unknown`) while still letting us strip the\n // descriptor-published `storageHash` before re-canonicalising.\n const storageRecord = inputs.storage as Record<string, unknown>;\n const { storageHash: _stripped, ...storageWithoutHash } = storageRecord;\n // Target serializers (e.g. PostgresContractSerializer) inject a `kind`\n // discriminator into each namespace entry when writing contract.json (e.g.\n // `\"postgres-unbound-schema\"`). The authoring pipeline computes\n // `storageHash` from IR class instances whose `kind` property is\n // non-enumerable, so `kind` is absent from the hash input. Strip `kind`\n // from namespace entries before recomputing so this check always operates\n // on the same canonical shape the authoring pipeline saw.\n const normalizedStorage = stripNamespaceKinds(storageWithoutHash);\n const recomputed = computeStorageHash({\n target: inputs.target,\n targetFamily: inputs.targetFamily,\n storage: normalizedStorage,\n });\n if (recomputed !== inputs.headRefHash) {\n throw errorDescriptorHeadHashMismatch({\n extensionId: inputs.extensionId,\n recomputedHash: recomputed,\n headRefHash: inputs.headRefHash,\n });\n }\n}\n","import { EMPTY_CONTRACT_HASH } from './constants';\nimport { readMigrationsDir } from './io';\nimport { findPathWithDecision, reconstructGraph } from './migration-graph';\nimport type { MigrationOps } from './package';\nimport {\n type ContractSpaceHeadRef,\n readContractSpaceHeadRef,\n} from './read-contract-space-head-ref';\nimport { spaceMigrationDirectory } from './space-layout';\n\n/**\n * Outcome of {@link computeExtensionSpaceApplyPath} — a discriminated union\n * mirroring {@link import('./migration-graph').FindPathOutcome} so callers\n * can map structural / invariant failures to their preferred CLI envelope\n * without re-running pathfinding.\n */\nexport type ExtensionSpaceApplyPathOutcome =\n | {\n readonly kind: 'ok';\n readonly contractSpaceHeadRef: ContractSpaceHeadRef;\n /**\n * Sorted, deduplicated invariant ids covered by the walked path.\n * Mirrors the on-disk `providedInvariants` summed across edges and\n * canonicalised — what the runner stamps on the marker after apply.\n */\n readonly providedInvariants: readonly string[];\n /**\n * Path operations in apply order. Empty when the marker is already\n * at the recorded head (no-op).\n */\n readonly pathOps: MigrationOps;\n /**\n * Migration directory names walked, in order. Mirrors `pathOps`'s\n * structure but at the package granularity — useful for surfacing\n * \"applied N migration(s)\" messages.\n */\n readonly walkedMigrationDirs: readonly string[];\n }\n | { readonly kind: 'unreachable'; readonly contractSpaceHeadRef: ContractSpaceHeadRef }\n | {\n readonly kind: 'unsatisfiable';\n readonly contractSpaceHeadRef: ContractSpaceHeadRef;\n readonly missing: readonly string[];\n readonly structuralPath: readonly { readonly dirName: string; readonly to: string }[];\n }\n | { readonly kind: 'contractSpaceHeadRefMissing' };\n\n/**\n * Inputs to {@link computeExtensionSpaceApplyPath}. The helper is\n * deliberately framework-neutral and consumes only on-disk state:\n *\n * - `projectMigrationsDir` is the project's top-level `migrations/` dir.\n * - `spaceId` selects the per-space subdirectory under it.\n * - `currentMarkerHash` / `currentMarkerInvariants` come from the live\n * marker row keyed by `space = <spaceId>`. `null` hash = no marker yet\n * (the pathfinder treats this as the empty-contract sentinel per ADR\n * 208).\n */\nexport interface ComputeExtensionSpaceApplyPathInputs {\n readonly projectMigrationsDir: string;\n readonly spaceId: string;\n readonly currentMarkerHash: string | null;\n readonly currentMarkerInvariants: readonly string[];\n}\n\n/**\n * Compute the apply path for an extension contract space — the shortest\n * sequence of on-disk migration packages that walks the live marker\n * forward to the on-disk head ref hash, covering every required\n * invariant.\n *\n * Reads only on-disk artefacts (`migrations/<spaceId>/refs/head.json`\n * and the per-space migration packages). **Does not import any\n * extension descriptor module** — `db init` / `db update` must remain\n * runnable without the descriptor source on disk.\n *\n * Behaviour:\n * - Returns `{ kind: 'ok', pathOps: [], … }` when the marker is already\n * at the recorded head and no required invariants are missing.\n * - Returns `{ kind: 'unreachable' }` when the marker hash is not\n * structurally connected to the recorded head in the graph.\n * - Returns `{ kind: 'unsatisfiable', missing, … }` when the marker is\n * reachable but no path covers the required invariants.\n * - Returns `{ kind: 'contractSpaceHeadRefMissing' }` when the per-space\n * `refs/head.json` is absent — the precheck verifier should already\n * have rejected this case, but the helper is defensive so callers can\n * surface a coherent error rather than throw.\n */\nexport async function computeExtensionSpaceApplyPath(\n inputs: ComputeExtensionSpaceApplyPathInputs,\n): Promise<ExtensionSpaceApplyPathOutcome> {\n const { projectMigrationsDir, spaceId, currentMarkerHash, currentMarkerInvariants } = inputs;\n\n const contractSpaceHeadRef = await readContractSpaceHeadRef(projectMigrationsDir, spaceId);\n if (contractSpaceHeadRef === null) {\n return { kind: 'contractSpaceHeadRefMissing' };\n }\n\n const spaceDir = spaceMigrationDirectory(projectMigrationsDir, spaceId);\n const packages = await readMigrationsDir(spaceDir);\n const graph = reconstructGraph(packages);\n\n // Live-marker layer encodes \"no prior state\" as EMPTY_CONTRACT_HASH;\n // mirror the `migrate` flow so a fresh-marker initial walk\n // hits the baseline migration whose `from` is EMPTY_CONTRACT_HASH.\n const fromHash = currentMarkerHash ?? EMPTY_CONTRACT_HASH;\n const required = new Set(\n contractSpaceHeadRef.invariants.filter((id) => !currentMarkerInvariants.includes(id)),\n );\n\n const outcome = findPathWithDecision(graph, fromHash, contractSpaceHeadRef.hash, { required });\n\n if (outcome.kind === 'unreachable') {\n return { kind: 'unreachable', contractSpaceHeadRef };\n }\n if (outcome.kind === 'unsatisfiable') {\n return {\n kind: 'unsatisfiable',\n contractSpaceHeadRef,\n missing: outcome.missing,\n structuralPath: outcome.structuralPath.map(({ dirName, to }) => ({ dirName, to })),\n };\n }\n\n const packagesByHash = new Map(packages.map((pkg) => [pkg.metadata.migrationHash, pkg]));\n\n const pathOps: MigrationOps[number][] = [];\n const walkedMigrationDirs: string[] = [];\n const providedInvariantsSet = new Set<string>();\n for (const edge of outcome.decision.selectedPath) {\n const pkg = packagesByHash.get(edge.migrationHash);\n if (!pkg) {\n // Path edges always come from the same `packages` array, so this\n // is only reachable when the graph is internally inconsistent —\n // surface it loudly rather than silently truncating the path.\n throw new Error(\n `Migration package missing for edge ${edge.migrationHash} in space \"${spaceId}\"`,\n );\n }\n walkedMigrationDirs.push(pkg.dirName);\n for (const op of pkg.ops) pathOps.push(op);\n for (const invariant of pkg.metadata.providedInvariants) providedInvariantsSet.add(invariant);\n }\n\n return {\n kind: 'ok',\n contractSpaceHeadRef,\n providedInvariants: [...providedInvariantsSet].sort(),\n pathOps,\n walkedMigrationDirs,\n };\n}\n","import type { Contract } from '@prisma-next/contract/types';\nimport type {\n ContractSpace,\n ContractSpaceHeadRef,\n MigrationPackage,\n MigrationPlanOperation,\n} from '@prisma-next/framework-components/control';\nimport type { MigrationMetadata } from './metadata';\n\n/**\n * Materialise a typed {@link ContractSpace} from the JSON artefacts a\n * contract-space extension package emits to disk.\n *\n * Extension descriptors wire `contract.json`, per-migration\n * `migration.json` / `ops.json`, and `refs/head.json` to the framework's\n * typed surfaces. TypeScript widens JSON imports to a structural record\n * that does not preserve readonly modifiers or branded scalars (e.g.\n * `StorageHashBase<'sha256:...'>`), so authoring the descriptor inline\n * forces every wiring site to cast through `unknown`. This helper\n * encapsulates the single narrowing point: descriptor sources stay\n * cast-free, and the (necessary) coercion is colocated with the\n * documentation explaining why it is safe.\n *\n * Safety: the JSON files passed here are produced by the framework's own\n * emit pipeline (`prisma-next contract emit` and `MigrationCLI.run`)\n * and re-validated downstream by the runner / verifier. The descriptor\n * is a pass-through wiring layer — no descriptor consumer treats the\n * narrowed types as a stronger guarantee than \"these came from the\n * canonical emit pipeline\".\n *\n * The helper does not introspect or schema-validate the inputs; runtime\n * validation is the responsibility of `family.deserializeContract`\n * (codec-aware, invoked at control-stack construction) and the\n * per-migration `readMigrationPackage` reader used when loading\n * from disk. JSON-imported packages flow through the descriptor without\n * a disk read, so the equivalent runtime guarantee comes from the emit\n * pipeline that produced the JSON in the first place.\n */\nexport function contractSpaceFromJson<TContract extends Contract = Contract>(inputs: {\n readonly contractJson: unknown;\n readonly migrations: ReadonlyArray<{\n readonly dirName: string;\n readonly metadata: unknown;\n readonly ops: unknown;\n }>;\n readonly headRef: ContractSpaceHeadRef;\n}): ContractSpace<TContract> {\n // The narrowing happens once, here. Casting via `unknown` rather than a\n // direct cast preserves TS's structural soundness checks for the\n // inputs (they must be assignable to `unknown`, which is trivial); the\n // resulting type is the family-specific Contract / MigrationPackage\n // surface descriptors publish.\n const migrations: readonly MigrationPackage[] = inputs.migrations.map((m) => ({\n dirName: m.dirName,\n metadata: m.metadata as MigrationMetadata,\n ops: m.ops as readonly MigrationPlanOperation[],\n }));\n return {\n contractJson: inputs.contractJson as TContract,\n migrations,\n headRef: inputs.headRef,\n };\n}\n","import { mkdir, writeFile } from 'node:fs/promises';\nimport { canonicalizeJson } from '@prisma-next/framework-components/utils';\nimport { join } from 'pathe';\nimport type { ContractSpaceHeadRef } from './read-contract-space-head-ref';\nimport { assertValidSpaceId } from './space-layout';\n\n/**\n * Inputs for {@link emitContractSpaceArtefacts}.\n *\n * - `contract` is the canonical contract value the framework just emitted\n * for the space; it is serialised through {@link canonicalizeJson}, so\n * it must be a JSON-compatible value (objects / arrays / primitives).\n * Typed as `unknown` rather than the SQL-family `Contract<SqlStorage>`\n * to keep `migration-tools` framework-neutral; SQL-family callers pass\n * their typed value through unchanged.\n *\n * - `contractDts` is the pre-rendered `.d.ts` text. Rendering happens in\n * the SQL family (which owns the codec / typemap input the renderer\n * needs), so this helper accepts the text verbatim and writes it out\n * without further transformation.\n *\n * - `headRef` is the head reference for the space.\n * `invariants` are sorted alphabetically before serialisation so two\n * callers passing the same set in different orders produce\n * byte-identical `refs/head.json`.\n */\nexport interface ContractSpaceArtefactInputs {\n readonly contract: unknown;\n readonly contractDts: string;\n readonly headRef: ContractSpaceHeadRef;\n}\n\n/**\n * Emit the per-space artefacts (`contract.json`, `contract.d.ts`,\n * `refs/head.json`) under `<projectMigrationsDir>/<spaceId>/`.\n *\n * Always-overwrite: the framework owns these files; running `migrate`\n * twice with the same inputs is a no-op observably (idempotent), but the\n * helper does not check pre-existing contents — re-emit always wins.\n *\n * Path layout matches the convention in\n * [`spaceMigrationDirectory`](./space-layout.ts). The space id is\n * validated against `[a-z][a-z0-9_-]{0,63}` via\n * {@link assertValidSpaceId} for filesystem-safety reasons; the helper\n * accepts every space uniformly (including the app space, default\n * `'app'`).\n *\n * The migrations directory and space subdirectory are created if they\n * do not yet exist (`mkdir { recursive: true }`).\n */\nexport async function emitContractSpaceArtefacts(\n projectMigrationsDir: string,\n spaceId: string,\n inputs: ContractSpaceArtefactInputs,\n): Promise<void> {\n assertValidSpaceId(spaceId);\n\n const dir = join(projectMigrationsDir, spaceId);\n await mkdir(join(dir, 'refs'), { recursive: true });\n\n await writeFile(join(dir, 'contract.json'), `${canonicalizeJson(inputs.contract)}\\n`);\n await writeFile(join(dir, 'contract.d.ts'), inputs.contractDts);\n\n const sortedInvariants = [...inputs.headRef.invariants].sort();\n const headJson = canonicalizeJson({\n hash: inputs.headRef.hash,\n invariants: sortedInvariants,\n });\n await writeFile(join(dir, 'refs', 'head.json'), `${headJson}\\n`);\n}\n","import { readContractSpaceHeadRef } from './read-contract-space-head-ref';\nimport { APP_SPACE_ID } from './space-layout';\nimport {\n type ContractSpaceHeadRecord,\n listContractSpaceDirectories,\n} from './verify-contract-spaces';\n\n/**\n * Disk-side inputs to {@link import('./verify-contract-spaces').verifyContractSpaces}\n * — gathered without touching the live database. The caller composes\n * this with the marker rows it reads from the runtime to invoke the\n * verifier.\n */\nexport interface DiskContractSpaceState {\n /** Contract-space directory names observed under `<projectMigrationsDir>/`. */\n readonly spaceDirsOnDisk: readonly string[];\n /** Head-ref `(hash, invariants)` per extension space. */\n readonly headRefsBySpace: ReadonlyMap<string, ContractSpaceHeadRecord>;\n}\n\n/**\n * Read the on-disk state the per-space verifier needs:\n *\n * - The list of contract-space directories under\n * `<projectMigrationsDir>/` (via\n * {@link import('./verify-contract-spaces').listContractSpaceDirectories}).\n * - The on-disk head ref `(hash, invariants)` for each declared extension space\n * (via {@link readContractSpaceHeadRef}; missing on-disk artefacts are simply\n * omitted — the verifier reports them as `declaredButUnmigrated`).\n *\n * Synchronous in spirit but async due to filesystem reads. Reads only\n * the user's repo. **Does not import any extension descriptor module.**\n *\n * Composition convention: pure target-agnostic primitive in\n * `1-framework`; the SQL family (and any future target family) wires\n * it into its `dbInit` / `verify` flows alongside its own marker-row\n * read before invoking `verifyContractSpaces`.\n */\nexport async function gatherDiskContractSpaceState(args: {\n readonly projectMigrationsDir: string;\n /**\n * Set of space ids the project declares: `'app'` plus each entry in\n * `extensionPacks` whose descriptor exposes a `contractSpace`. The\n * helper reads on-disk head data only for the extension members.\n */\n readonly loadedSpaceIds: ReadonlySet<string>;\n}): Promise<DiskContractSpaceState> {\n const { projectMigrationsDir, loadedSpaceIds } = args;\n\n const spaceDirsOnDisk = await listContractSpaceDirectories(projectMigrationsDir);\n\n const headRefsBySpace = new Map<string, ContractSpaceHeadRecord>();\n for (const spaceId of loadedSpaceIds) {\n if (spaceId === APP_SPACE_ID) continue;\n const head = await readContractSpaceHeadRef(projectMigrationsDir, spaceId);\n if (head !== null) {\n headRefsBySpace.set(spaceId, head);\n }\n }\n\n return { spaceDirsOnDisk, headRefsBySpace };\n}\n","import { errorDuplicateSpaceId } from './errors';\n\n/**\n * Per-space input for {@link planAllSpaces}. One entry per loaded\n * contract space (the application's `'app'` plus each extension that\n * exposes a `contractSpace`).\n *\n * - `priorContract` is `null` for a space that has never been emitted\n * (no `migrations/<space-id>/contract.json` on disk yet); otherwise it\n * is the canonical contract value emitted for that space.\n * - `newContract` is the canonical contract value the planner is about\n * to emit for that space — for app-space, the just-emitted root\n * `contract.json`; for an extension space, the descriptor's\n * `contractSpace.contractJson`.\n */\nexport interface SpacePlanInput<TContract> {\n readonly spaceId: string;\n readonly priorContract: TContract | null;\n readonly newContract: TContract;\n}\n\nexport interface SpacePlanOutput<TPackage> {\n readonly spaceId: string;\n readonly migrationPackages: readonly TPackage[];\n}\n\n/**\n * Iterate the per-space planner across a set of loaded contract spaces\n * and return a deterministic shape regardless of declaration order.\n *\n * Behaviour:\n *\n * - The output is sorted alphabetically by `spaceId`. Two callers\n * passing the same set of inputs in different orders observe\n * byte-identical outputs.\n * - The per-space planner (`planSpace`) is called exactly once per\n * input, in alphabetical-by-spaceId order. Its return value is\n * attached to the corresponding output entry verbatim.\n * - Duplicate `spaceId`s in the input array throw\n * `MIGRATION.DUPLICATE_SPACE_ID` before any `planSpace` call runs,\n * keeping the planner pure when the input is malformed.\n *\n * The signature is generic over `TContract` and `TPackage` because the\n * shape is framework-neutral (SQL family today, Mongo family\n * eventually). Callers wire in whatever contract value and migration\n * package shape their family already speaks.\n *\n * Synchronous: the underlying per-space planner (target's\n * `MigrationPlanner.plan(...)`) is synchronous; callers that need to\n * resolve async I/O (e.g. reading on-disk `contract.json` from disk)\n * resolve it before calling `planAllSpaces` and pass the materialised\n * inputs through.\n */\nexport function planAllSpaces<TContract, TPackage>(\n inputs: readonly SpacePlanInput<TContract>[],\n planSpace: (input: SpacePlanInput<TContract>) => readonly TPackage[],\n): readonly SpacePlanOutput<TPackage>[] {\n const seen = new Set<string>();\n for (const input of inputs) {\n if (seen.has(input.spaceId)) {\n throw errorDuplicateSpaceId(input.spaceId);\n }\n seen.add(input.spaceId);\n }\n\n const sorted = [...inputs].sort((a, b) => {\n if (a.spaceId < b.spaceId) return -1;\n if (a.spaceId > b.spaceId) return 1;\n return 0;\n });\n\n return sorted.map((input) => ({\n spaceId: input.spaceId,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extension-space-apply-path.ts","../../src/contract-space-from-json.ts","../../src/emit-contract-space-artefacts.ts","../../src/gather-disk-contract-space-state.ts","../../src/plan-all-spaces.ts"],"sourcesContent":["import type { PreserveEmptyPredicate, StorageSort } from '@prisma-next/contract/hashing';\nimport { computeStorageHash } from '@prisma-next/contract/hashing';\nimport { ifDefined } from '@prisma-next/utils/defined';\nimport { errorDescriptorHeadHashMismatch } from './errors';\n\nfunction stripNamespaceKinds(storage: Record<string, unknown>): Record<string, unknown> {\n const namespaces = storage['namespaces'] as Record<string, Record<string, unknown>> | undefined;\n if (!namespaces) return storage;\n const stripped: Record<string, Record<string, unknown>> = {};\n for (const [nsId, ns] of Object.entries(namespaces)) {\n if (ns && typeof ns === 'object') {\n const { kind: _kind, ...rest } = ns as Record<string, unknown>;\n stripped[nsId] = rest as Record<string, unknown>;\n } else {\n stripped[nsId] = ns;\n }\n }\n return { ...storage, namespaces: stripped };\n}\n\n/**\n * Inputs the helper needs to recompute the descriptor's storage hash and\n * compare it to the published `headRef.hash`. Kept structural so the SQL\n * family (and any future target family) can compose the check without\n * coupling to its own descriptor types.\n */\nexport interface DescriptorSelfConsistencyInputs {\n readonly extensionId: string;\n readonly target: string;\n readonly targetFamily: string;\n /**\n * Family-specific storage object. Typed as `unknown` so callers can\n * pass their own narrow storage shape (e.g. `SqlStorage`) without an\n * inline cast — the helper canonicalises through `JSON.stringify`\n * inside {@link computeStorageHash} and only requires a plain\n * record-shaped value at runtime.\n */\n readonly storage: unknown;\n readonly headRefHash: string;\n readonly shouldPreserveEmpty?: PreserveEmptyPredicate;\n readonly sortStorage?: StorageSort;\n}\n\n/**\n * Assert that an extension descriptor is self-consistent: the\n * `headRef.hash` it publishes must match the canonical hash recomputed\n * from its `contractSpace.contractJson`.\n *\n * Recomputes via {@link computeStorageHash} — the same canonical-JSON\n * pipeline the descriptor's own emit pipeline produced the hash with —\n * over `(target, targetFamily, storage)`. Mismatch indicates the\n * extension author bumped `contractJson` without rerunning emit, leaving\n * the descriptor's `headRef.hash` stale; the consumer-side helpers\n * (drift detection, on-disk artefact emission, runner marker writes) all\n * trust `headRef.hash` as the canonical identity, so a stale value would\n * silently corrupt every downstream boundary.\n *\n * Synchronous, pure, no I/O. Throws\n * `MIGRATION.DESCRIPTOR_HEAD_HASH_MISMATCH` on failure with both the\n * recomputed and published hashes in `details` so callers can surface a\n * clear remediation hint without re-deriving them.\n */\nexport function assertDescriptorSelfConsistency(inputs: DescriptorSelfConsistencyInputs): void {\n // The published `storage.storageHash` is the *output* of the production\n // emit pipeline's `computeStorageHash` call, computed over a storage\n // object that did not yet carry `storageHash`. Recomputing against the\n // published storage as-is would feed the result back into its own input\n // and produce a different digest. Strip `storageHash` before\n // recomputing so the helper sees the same canonical shape the\n // descriptor's authoring pipeline saw.\n // The helper requires only a plain record-shaped storage value at\n // runtime; a single cast here keeps the public input type\n // family-agnostic (`unknown`) while still letting us strip the\n // descriptor-published `storageHash` before re-canonicalising.\n const storageRecord = inputs.storage as Record<string, unknown>;\n const { storageHash: _stripped, ...storageWithoutHash } = storageRecord;\n // Target serializers (e.g. PostgresContractSerializer) inject a `kind`\n // discriminator into each namespace entry when writing contract.json (e.g.\n // `\"postgres-unbound-schema\"`). The authoring pipeline computes\n // `storageHash` from IR class instances whose `kind` property is\n // non-enumerable, so `kind` is absent from the hash input. Strip `kind`\n // from namespace entries before recomputing so this check always operates\n // on the same canonical shape the authoring pipeline saw.\n const normalizedStorage = stripNamespaceKinds(storageWithoutHash);\n const recomputed = computeStorageHash({\n target: inputs.target,\n targetFamily: inputs.targetFamily,\n storage: normalizedStorage,\n ...ifDefined('shouldPreserveEmpty', inputs.shouldPreserveEmpty),\n ...ifDefined('sortStorage', inputs.sortStorage),\n });\n if (recomputed !== inputs.headRefHash) {\n throw errorDescriptorHeadHashMismatch({\n extensionId: inputs.extensionId,\n recomputedHash: recomputed,\n headRefHash: inputs.headRefHash,\n });\n }\n}\n","import { EMPTY_CONTRACT_HASH } from './constants';\nimport { readMigrationsDir } from './io';\nimport { findPathWithDecision, reconstructGraph } from './migration-graph';\nimport type { MigrationOps } from './package';\nimport {\n type ContractSpaceHeadRef,\n readContractSpaceHeadRef,\n} from './read-contract-space-head-ref';\nimport { spaceMigrationDirectory } from './space-layout';\n\n/**\n * Outcome of {@link computeExtensionSpaceApplyPath} — a discriminated union\n * mirroring {@link import('./migration-graph').FindPathOutcome} so callers\n * can map structural / invariant failures to their preferred CLI envelope\n * without re-running pathfinding.\n */\nexport type ExtensionSpaceApplyPathOutcome =\n | {\n readonly kind: 'ok';\n readonly contractSpaceHeadRef: ContractSpaceHeadRef;\n /**\n * Sorted, deduplicated invariant ids covered by the walked path.\n * Mirrors the on-disk `providedInvariants` summed across edges and\n * canonicalised — what the runner stamps on the marker after apply.\n */\n readonly providedInvariants: readonly string[];\n /**\n * Path operations in apply order. Empty when the marker is already\n * at the recorded head (no-op).\n */\n readonly pathOps: MigrationOps;\n /**\n * Migration directory names walked, in order. Mirrors `pathOps`'s\n * structure but at the package granularity — useful for surfacing\n * \"applied N migration(s)\" messages.\n */\n readonly walkedMigrationDirs: readonly string[];\n }\n | { readonly kind: 'unreachable'; readonly contractSpaceHeadRef: ContractSpaceHeadRef }\n | {\n readonly kind: 'unsatisfiable';\n readonly contractSpaceHeadRef: ContractSpaceHeadRef;\n readonly missing: readonly string[];\n readonly structuralPath: readonly { readonly dirName: string; readonly to: string }[];\n }\n | { readonly kind: 'contractSpaceHeadRefMissing' };\n\n/**\n * Inputs to {@link computeExtensionSpaceApplyPath}. The helper is\n * deliberately framework-neutral and consumes only on-disk state:\n *\n * - `projectMigrationsDir` is the project's top-level `migrations/` dir.\n * - `spaceId` selects the per-space subdirectory under it.\n * - `currentMarkerHash` / `currentMarkerInvariants` come from the live\n * marker row keyed by `space = <spaceId>`. `null` hash = no marker yet\n * (the pathfinder treats this as the empty-contract sentinel per ADR\n * 208).\n */\nexport interface ComputeExtensionSpaceApplyPathInputs {\n readonly projectMigrationsDir: string;\n readonly spaceId: string;\n readonly currentMarkerHash: string | null;\n readonly currentMarkerInvariants: readonly string[];\n}\n\n/**\n * Compute the apply path for an extension contract space — the shortest\n * sequence of on-disk migration packages that walks the live marker\n * forward to the on-disk head ref hash, covering every required\n * invariant.\n *\n * Reads only on-disk artefacts (`migrations/<spaceId>/refs/head.json`\n * and the per-space migration packages). **Does not import any\n * extension descriptor module** — `db init` / `db update` must remain\n * runnable without the descriptor source on disk.\n *\n * Behaviour:\n * - Returns `{ kind: 'ok', pathOps: [], … }` when the marker is already\n * at the recorded head and no required invariants are missing.\n * - Returns `{ kind: 'unreachable' }` when the marker hash is not\n * structurally connected to the recorded head in the graph.\n * - Returns `{ kind: 'unsatisfiable', missing, … }` when the marker is\n * reachable but no path covers the required invariants.\n * - Returns `{ kind: 'contractSpaceHeadRefMissing' }` when the per-space\n * `refs/head.json` is absent — the precheck verifier should already\n * have rejected this case, but the helper is defensive so callers can\n * surface a coherent error rather than throw.\n */\nexport async function computeExtensionSpaceApplyPath(\n inputs: ComputeExtensionSpaceApplyPathInputs,\n): Promise<ExtensionSpaceApplyPathOutcome> {\n const { projectMigrationsDir, spaceId, currentMarkerHash, currentMarkerInvariants } = inputs;\n\n const contractSpaceHeadRef = await readContractSpaceHeadRef(projectMigrationsDir, spaceId);\n if (contractSpaceHeadRef === null) {\n return { kind: 'contractSpaceHeadRefMissing' };\n }\n\n const spaceDir = spaceMigrationDirectory(projectMigrationsDir, spaceId);\n const { packages } = await readMigrationsDir(spaceDir);\n const graph = reconstructGraph(packages);\n\n // Live-marker layer encodes \"no prior state\" as EMPTY_CONTRACT_HASH;\n // mirror the `migrate` flow so a fresh-marker initial walk\n // hits the baseline migration whose `from` is EMPTY_CONTRACT_HASH.\n const fromHash = currentMarkerHash ?? EMPTY_CONTRACT_HASH;\n const required = new Set(\n contractSpaceHeadRef.invariants.filter((id) => !currentMarkerInvariants.includes(id)),\n );\n\n const outcome = findPathWithDecision(graph, fromHash, contractSpaceHeadRef.hash, { required });\n\n if (outcome.kind === 'unreachable') {\n return { kind: 'unreachable', contractSpaceHeadRef };\n }\n if (outcome.kind === 'unsatisfiable') {\n return {\n kind: 'unsatisfiable',\n contractSpaceHeadRef,\n missing: outcome.missing,\n structuralPath: outcome.structuralPath.map(({ dirName, to }) => ({ dirName, to })),\n };\n }\n\n const packagesByHash = new Map(packages.map((pkg) => [pkg.metadata.migrationHash, pkg]));\n\n const pathOps: MigrationOps[number][] = [];\n const walkedMigrationDirs: string[] = [];\n const providedInvariantsSet = new Set<string>();\n for (const edge of outcome.decision.selectedPath) {\n const pkg = packagesByHash.get(edge.migrationHash);\n if (!pkg) {\n // Path edges always come from the same `packages` array, so this\n // is only reachable when the graph is internally inconsistent —\n // surface it loudly rather than silently truncating the path.\n throw new Error(\n `Migration package missing for edge ${edge.migrationHash} in space \"${spaceId}\"`,\n );\n }\n walkedMigrationDirs.push(pkg.dirName);\n for (const op of pkg.ops) pathOps.push(op);\n for (const invariant of pkg.metadata.providedInvariants) providedInvariantsSet.add(invariant);\n }\n\n return {\n kind: 'ok',\n contractSpaceHeadRef,\n providedInvariants: [...providedInvariantsSet].sort(),\n pathOps,\n walkedMigrationDirs,\n };\n}\n","import type { Contract } from '@prisma-next/contract/types';\nimport type {\n ContractSpace,\n ContractSpaceHeadRef,\n MigrationPackage,\n MigrationPlanOperation,\n} from '@prisma-next/framework-components/control';\nimport type { MigrationMetadata } from './metadata';\n\n/**\n * Materialise a typed {@link ContractSpace} from the JSON artefacts a\n * contract-space extension package emits to disk.\n *\n * Extension descriptors wire `contract.json`, per-migration\n * `migration.json` / `ops.json`, and `refs/head.json` to the framework's\n * typed surfaces. TypeScript widens JSON imports to a structural record\n * that does not preserve readonly modifiers or branded scalars (e.g.\n * `StorageHashBase<'sha256:...'>`), so authoring the descriptor inline\n * forces every wiring site to cast through `unknown`. This helper\n * encapsulates the single narrowing point: descriptor sources stay\n * cast-free, and the (necessary) coercion is colocated with the\n * documentation explaining why it is safe.\n *\n * Safety: the JSON files passed here are produced by the framework's own\n * emit pipeline (`prisma-next contract emit` and `MigrationCLI.run`)\n * and re-validated downstream by the runner / verifier. The descriptor\n * is a pass-through wiring layer — no descriptor consumer treats the\n * narrowed types as a stronger guarantee than \"these came from the\n * canonical emit pipeline\".\n *\n * The helper does not introspect or schema-validate the inputs; runtime\n * validation is the responsibility of `family.deserializeContract`\n * (codec-aware, invoked at control-stack construction) and the\n * per-migration `readMigrationPackage` reader used when loading\n * from disk. JSON-imported packages flow through the descriptor without\n * a disk read, so the equivalent runtime guarantee comes from the emit\n * pipeline that produced the JSON in the first place.\n */\nexport function contractSpaceFromJson<TContract extends Contract = Contract>(inputs: {\n readonly contractJson: unknown;\n readonly migrations: ReadonlyArray<{\n readonly dirName: string;\n readonly metadata: unknown;\n readonly ops: unknown;\n }>;\n readonly headRef: ContractSpaceHeadRef;\n}): ContractSpace<TContract> {\n // The narrowing happens once, here. Casting via `unknown` rather than a\n // direct cast preserves TS's structural soundness checks for the\n // inputs (they must be assignable to `unknown`, which is trivial); the\n // resulting type is the family-specific Contract / MigrationPackage\n // surface descriptors publish.\n const migrations: readonly MigrationPackage[] = inputs.migrations.map((m) => ({\n dirName: m.dirName,\n metadata: m.metadata as MigrationMetadata,\n ops: m.ops as readonly MigrationPlanOperation[],\n }));\n return {\n contractJson: inputs.contractJson as TContract,\n migrations,\n headRef: inputs.headRef,\n };\n}\n","import { mkdir, writeFile } from 'node:fs/promises';\nimport { canonicalizeJson } from '@prisma-next/framework-components/utils';\nimport { join } from 'pathe';\nimport type { ContractSpaceHeadRef } from './read-contract-space-head-ref';\nimport { assertValidSpaceId, spaceRefsDirectory } from './space-layout';\n\n/**\n * Inputs for {@link emitContractSpaceArtefacts}.\n *\n * - `contract` is the canonical contract value the framework just emitted\n * for the space; it is serialised through {@link canonicalizeJson}, so\n * it must be a JSON-compatible value (objects / arrays / primitives).\n * Typed as `unknown` rather than the SQL-family `Contract<SqlStorage>`\n * to keep `migration-tools` framework-neutral; SQL-family callers pass\n * their typed value through unchanged.\n *\n * - `contractDts` is the pre-rendered `.d.ts` text. Rendering happens in\n * the SQL family (which owns the codec / typemap input the renderer\n * needs), so this helper accepts the text verbatim and writes it out\n * without further transformation.\n *\n * - `headRef` is the head reference for the space.\n * `invariants` are sorted alphabetically before serialisation so two\n * callers passing the same set in different orders produce\n * byte-identical `refs/head.json`.\n */\nexport interface ContractSpaceArtefactInputs {\n readonly contract: unknown;\n readonly contractDts: string;\n readonly headRef: ContractSpaceHeadRef;\n}\n\n/**\n * Emit the per-space artefacts (`contract.json`, `contract.d.ts`,\n * `refs/head.json`) under `<projectMigrationsDir>/<spaceId>/`.\n *\n * Always-overwrite: the framework owns these files; running `migrate`\n * twice with the same inputs is a no-op observably (idempotent), but the\n * helper does not check pre-existing contents — re-emit always wins.\n *\n * Path layout matches the convention in\n * [`spaceMigrationDirectory`](./space-layout.ts). The space id is\n * validated against `[a-z][a-z0-9_-]{0,63}` via\n * {@link assertValidSpaceId} for filesystem-safety reasons; the helper\n * accepts every space uniformly (including the app space, default\n * `'app'`).\n *\n * The migrations directory and space subdirectory are created if they\n * do not yet exist (`mkdir { recursive: true }`).\n */\nexport async function emitContractSpaceArtefacts(\n projectMigrationsDir: string,\n spaceId: string,\n inputs: ContractSpaceArtefactInputs,\n): Promise<void> {\n assertValidSpaceId(spaceId);\n\n const dir = join(projectMigrationsDir, spaceId);\n const refsDir = spaceRefsDirectory(dir);\n await mkdir(refsDir, { recursive: true });\n\n await writeFile(join(dir, 'contract.json'), `${canonicalizeJson(inputs.contract)}\\n`);\n await writeFile(join(dir, 'contract.d.ts'), inputs.contractDts);\n\n const sortedInvariants = [...inputs.headRef.invariants].sort();\n const headJson = canonicalizeJson({\n hash: inputs.headRef.hash,\n invariants: sortedInvariants,\n });\n await writeFile(join(refsDir, 'head.json'), `${headJson}\\n`);\n}\n","import { readContractSpaceHeadRef } from './read-contract-space-head-ref';\nimport { APP_SPACE_ID } from './space-layout';\nimport {\n type ContractSpaceHeadRecord,\n listContractSpaceDirectories,\n} from './verify-contract-spaces';\n\n/**\n * Disk-side inputs to {@link import('./verify-contract-spaces').verifyContractSpaces}\n * — gathered without touching the live database. The caller composes\n * this with the marker rows it reads from the runtime to invoke the\n * verifier.\n */\nexport interface DiskContractSpaceState {\n /** Contract-space directory names observed under `<projectMigrationsDir>/`. */\n readonly spaceDirsOnDisk: readonly string[];\n /** Head-ref `(hash, invariants)` per extension space. */\n readonly headRefsBySpace: ReadonlyMap<string, ContractSpaceHeadRecord>;\n}\n\n/**\n * Read the on-disk state the per-space verifier needs:\n *\n * - The list of contract-space directories under\n * `<projectMigrationsDir>/` (via\n * {@link import('./verify-contract-spaces').listContractSpaceDirectories}).\n * - The on-disk head ref `(hash, invariants)` for each declared extension space\n * (via {@link readContractSpaceHeadRef}; missing on-disk artefacts are simply\n * omitted — the verifier reports them as `declaredButUnmigrated`).\n *\n * Synchronous in spirit but async due to filesystem reads. Reads only\n * the user's repo. **Does not import any extension descriptor module.**\n *\n * Composition convention: pure target-agnostic primitive in\n * `1-framework`; the SQL family (and any future target family) wires\n * it into its `dbInit` / `verify` flows alongside its own marker-row\n * read before invoking `verifyContractSpaces`.\n */\nexport async function gatherDiskContractSpaceState(args: {\n readonly projectMigrationsDir: string;\n /**\n * Set of space ids the project declares: `'app'` plus each entry in\n * `extensionPacks` whose descriptor exposes a `contractSpace`. The\n * helper reads on-disk head data only for the extension members.\n */\n readonly loadedSpaceIds: ReadonlySet<string>;\n}): Promise<DiskContractSpaceState> {\n const { projectMigrationsDir, loadedSpaceIds } = args;\n\n const spaceDirsOnDisk = await listContractSpaceDirectories(projectMigrationsDir);\n\n const headRefsBySpace = new Map<string, ContractSpaceHeadRecord>();\n for (const spaceId of loadedSpaceIds) {\n if (spaceId === APP_SPACE_ID) continue;\n const head = await readContractSpaceHeadRef(projectMigrationsDir, spaceId);\n if (head !== null) {\n headRefsBySpace.set(spaceId, head);\n }\n }\n\n return { spaceDirsOnDisk, headRefsBySpace };\n}\n","import { errorDuplicateSpaceId } from './errors';\n\n/**\n * Per-space input for {@link planAllSpaces}. One entry per loaded\n * contract space (the application's `'app'` plus each extension that\n * exposes a `contractSpace`).\n *\n * - `priorContract` is `null` for a space that has never been emitted\n * (no `migrations/<space-id>/contract.json` on disk yet); otherwise it\n * is the canonical contract value emitted for that space.\n * - `newContract` is the canonical contract value the planner is about\n * to emit for that space — for app-space, the just-emitted root\n * `contract.json`; for an extension space, the descriptor's\n * `contractSpace.contractJson`.\n */\nexport interface SpacePlanInput<TContract> {\n readonly spaceId: string;\n readonly priorContract: TContract | null;\n readonly newContract: TContract;\n}\n\nexport interface SpacePlanOutput<TPackage> {\n readonly spaceId: string;\n readonly migrationPackages: readonly TPackage[];\n}\n\n/**\n * Iterate the per-space planner across a set of loaded contract spaces\n * and return a deterministic shape regardless of declaration order.\n *\n * Behaviour:\n *\n * - The output is sorted alphabetically by `spaceId`. Two callers\n * passing the same set of inputs in different orders observe\n * byte-identical outputs.\n * - The per-space planner (`planSpace`) is called exactly once per\n * input, in alphabetical-by-spaceId order. Its return value is\n * attached to the corresponding output entry verbatim.\n * - Duplicate `spaceId`s in the input array throw\n * `MIGRATION.DUPLICATE_SPACE_ID` before any `planSpace` call runs,\n * keeping the planner pure when the input is malformed.\n *\n * The signature is generic over `TContract` and `TPackage` because the\n * shape is framework-neutral (SQL family today, Mongo family\n * eventually). Callers wire in whatever contract value and migration\n * package shape their family already speaks.\n *\n * Synchronous: the underlying per-space planner (target's\n * `MigrationPlanner.plan(...)`) is synchronous; callers that need to\n * resolve async I/O (e.g. reading on-disk `contract.json` from disk)\n * resolve it before calling `planAllSpaces` and pass the materialised\n * inputs through.\n */\nexport function planAllSpaces<TContract, TPackage>(\n inputs: readonly SpacePlanInput<TContract>[],\n planSpace: (input: SpacePlanInput<TContract>) => readonly TPackage[],\n): readonly SpacePlanOutput<TPackage>[] {\n const seen = new Set<string>();\n for (const input of inputs) {\n if (seen.has(input.spaceId)) {\n throw errorDuplicateSpaceId(input.spaceId);\n }\n seen.add(input.spaceId);\n }\n\n const sorted = [...inputs].sort((a, b) => {\n if (a.spaceId < b.spaceId) return -1;\n if (a.spaceId > b.spaceId) return 1;\n return 0;\n });\n\n return sorted.map((input) => ({\n spaceId: input.spaceId,\n migrationPackages: planSpace(input),\n }));\n}\n"],"mappings":";;;;;;;;;;;;AAKA,SAAS,oBAAoB,SAA2D;CACtF,MAAM,aAAa,QAAQ;CAC3B,IAAI,CAAC,YAAY,OAAO;CACxB,MAAM,WAAoD,CAAC;CAC3D,KAAK,MAAM,CAAC,MAAM,OAAO,OAAO,QAAQ,UAAU,GAChD,IAAI,MAAM,OAAO,OAAO,UAAU;EAChC,MAAM,EAAE,MAAM,OAAO,GAAG,SAAS;EACjC,SAAS,QAAQ;CACnB,OACE,SAAS,QAAQ;CAGrB,OAAO;EAAE,GAAG;EAAS,YAAY;CAAS;AAC5C;;;;;;;;;;;;;;;;;;;;AA4CA,SAAgB,gCAAgC,QAA+C;CAa7F,MAAM,EAAE,aAAa,WAAW,GAAG,uBADb,OAAO;CAS7B,MAAM,oBAAoB,oBAAoB,kBAAkB;CAChE,MAAM,aAAa,mBAAmB;EACpC,QAAQ,OAAO;EACf,cAAc,OAAO;EACrB,SAAS;EACT,GAAG,UAAU,uBAAuB,OAAO,mBAAmB;EAC9D,GAAG,UAAU,eAAe,OAAO,WAAW;CAChD,CAAC;CACD,IAAI,eAAe,OAAO,aACxB,MAAM,gCAAgC;EACpC,aAAa,OAAO;EACpB,gBAAgB;EAChB,aAAa,OAAO;CACtB,CAAC;AAEL;;;;;;;;;;;;;;;;;;;;;;;;;;ACVA,eAAsB,+BACpB,QACyC;CACzC,MAAM,EAAE,sBAAsB,SAAS,mBAAmB,4BAA4B;CAEtF,MAAM,uBAAuB,MAAM,yBAAyB,sBAAsB,OAAO;CACzF,IAAI,yBAAyB,MAC3B,OAAO,EAAE,MAAM,8BAA8B;CAI/C,MAAM,EAAE,aAAa,MAAM,kBADV,wBAAwB,sBAAsB,OACX,CAAC;CACrD,MAA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AG,MAAM;EACxC,IAAI,EAAE,UAAU,EAAE,SAAS,OAAO;EAClC,IAAI,EAAE,UAAU,EAAE,SAAS,OAAO;EAClC,OAAO;CACT,CAEY,EAAE,KAAK,WAAW;EAC5B,SAAS,MAAM;EACf,mBAAmB,UAAU,KAAK;CACpC,EAAE;AACJ"}
@@ -9,7 +9,6 @@ interface MigrationEdge {
9
9
  readonly migrationHash: string;
10
10
  readonly dirName: string;
11
11
  readonly createdAt: string;
12
- readonly labels: readonly string[];
13
12
  /**
14
13
  * Sorted, deduplicated list of `invariantId`s this edge provides.
15
14
  * An empty array means the migration declares no routing-visible
@@ -25,4 +24,4 @@ interface MigrationGraph {
25
24
  }
26
25
  //#endregion
27
26
  export { MigrationGraph as n, MigrationEdge as t };
28
- //# sourceMappingURL=graph-BrLXqoUc.d.mts.map
27
+ //# sourceMappingURL=graph-BUZuUeBC.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graph-BUZuUeBC.d.mts","names":[],"sources":["../src/graph.ts"],"mappings":";;AAIA;;;UAAiB,aAAA;EAAA,SACN,IAAA;EAAA,SACA,EAAA;EAAA,SACA,aAAA;EAAA,SACA,OAAA;EAAA,SACA,SAAA;EAMA;;AAAU;AAGrB;;EAHW,SAAA,UAAA;AAAA;AAAA,UAGM,cAAA;EAAA,SACN,KAAA,EAAO,WAAA;EAAA,SACP,YAAA,EAAc,WAAA,kBAA6B,aAAA;EAAA,SAC3C,YAAA,EAAc,WAAA,kBAA6B,aAAA;EAAA,SAC3C,eAAA,EAAiB,WAAA,SAAoB,aAAA;AAAA"}
@@ -5,13 +5,12 @@ function sha256Hex(input) {
5
5
  return createHash("sha256").update(input).digest("hex");
6
6
  }
7
7
  /**
8
- * Content-addressed migration hash over (metadata envelope sans hints,
9
- * ops). See ADR 199 — Storage-only migration identity for the
10
- * rationale: the storage-hash bookends (`from`, `to`) inside the
11
- * envelope anchor the contract identity by hash, and planner hints are
12
- * advisory and must not affect identity. The full contract IRs are not
13
- * part of the manifest they live in sibling `*-contract.json` files
14
- * authored alongside the migration, never inlined here.
8
+ * Content-addressed migration hash over (metadata envelope, ops). See
9
+ * ADR 199 — Storage-only migration identity for the rationale: the
10
+ * storage-hash bookends (`from`, `to`) inside the envelope anchor the
11
+ * contract identity by hash. The full contract IRs are not part of the
12
+ * manifest they live in sibling `*-contract.json` files authored
13
+ * alongside the migration, never inlined here.
15
14
  *
16
15
  * The integrity check is purely structural, not semantic. The function
17
16
  * canonicalizes its inputs via `sortKeys` (recursive) + `JSON.stringify`
@@ -33,7 +32,7 @@ function sha256Hex(input) {
33
32
  * yet) and at verify time (rehashing an already-attested record).
34
33
  */
35
34
  function computeMigrationHash(metadata, ops) {
36
- const { migrationHash: _migrationHash, hints: _hints, ...strippedMeta } = metadata;
35
+ const { migrationHash: _migrationHash, ...strippedMeta } = metadata;
37
36
  return `sha256:${sha256Hex(canonicalizeJson([canonicalizeJson(strippedMeta), canonicalizeJson(ops)].map(sha256Hex)))}`;
38
37
  }
39
38
  /**
@@ -62,4 +61,4 @@ function verifyMigrationHash(pkg) {
62
61
  //#endregion
63
62
  export { verifyMigrationHash as n, computeMigrationHash as t };
64
63
 
65
- //# sourceMappingURL=hash-Cr4WIr4Z.mjs.map
64
+ //# sourceMappingURL=hash--Y7vCpN3.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hash--Y7vCpN3.mjs","names":[],"sources":["../src/hash.ts"],"sourcesContent":["import { createHash } from 'node:crypto';\nimport { canonicalizeJson } from '@prisma-next/framework-components/utils';\nimport type { MigrationMetadata } from './metadata';\nimport type { MigrationOps, OnDiskMigrationPackage } from './package';\n\nexport interface VerifyResult {\n readonly ok: boolean;\n readonly reason?: 'mismatch';\n readonly storedHash: string;\n readonly computedHash: string;\n}\n\nfunction sha256Hex(input: string): string {\n return createHash('sha256').update(input).digest('hex');\n}\n\n/**\n * Content-addressed migration hash over (metadata envelope, ops). See\n * ADR 199 — Storage-only migration identity for the rationale: the\n * storage-hash bookends (`from`, `to`) inside the envelope anchor the\n * contract identity by hash. The full contract IRs are not part of the\n * manifest — they live in sibling `*-contract.json` files authored\n * alongside the migration, never inlined here.\n *\n * The integrity check is purely structural, not semantic. The function\n * canonicalizes its inputs via `sortKeys` (recursive) + `JSON.stringify`\n * and hashes the result. Target-specific operation payloads (`step.sql`,\n * Mongo's pipeline AST, …) are hashed verbatim — no per-target\n * normalization is required, because what's being verified is \"do the\n * on-disk bytes still produce their recorded hash\", not \"do two\n * semantically-equivalent migrations hash the same\". The latter is an\n * emit-drift concern (ADR 192 step 2).\n *\n * The symmetry across write and read holds because `JSON.parse(\n * JSON.stringify(x))` round-trips JSON-safe values losslessly and\n * `sortKeys` is idempotent and deterministic — write-time and read-time\n * canonicalization produce the same canonical bytes regardless of\n * source-side key ordering or whitespace.\n *\n * The `migrationHash` field on the metadata is stripped before hashing\n * so the function can be used both at write time (when no hash exists\n * yet) and at verify time (rehashing an already-attested record).\n */\nexport function computeMigrationHash(\n metadata: Omit<MigrationMetadata, 'migrationHash'> & { readonly migrationHash?: string },\n ops: MigrationOps,\n): string {\n const { migrationHash: _migrationHash, ...strippedMeta } = metadata;\n\n const canonicalMetadata = canonicalizeJson(strippedMeta);\n const canonicalOps = canonicalizeJson(ops);\n\n const partHashes = [canonicalMetadata, canonicalOps].map(sha256Hex);\n const hash = sha256Hex(canonicalizeJson(partHashes));\n\n return `sha256:${hash}`;\n}\n\n/**\n * Re-hash an in-memory migration package and compare against the stored\n * `migrationHash`. See `computeMigrationHash` for the canonicalization rules.\n *\n * Returns `{ ok: true }` when the package is internally consistent, or\n * `{ ok: false, reason: 'mismatch', storedHash, computedHash }` when it is\n * not — typically a sign of FS corruption, partial writes, or a post-emit\n * hand edit.\n */\nexport function verifyMigrationHash(pkg: OnDiskMigrationPackage): VerifyResult {\n const computed = computeMigrationHash(pkg.metadata, pkg.ops);\n\n if (pkg.metadata.migrationHash === computed) {\n return {\n ok: true,\n storedHash: pkg.metadata.migrationHash,\n computedHash: computed,\n };\n }\n\n return {\n ok: false,\n reason: 'mismatch',\n storedHash: pkg.metadata.migrationHash,\n computedHash: computed,\n };\n}\n"],"mappings":";;;AAYA,SAAS,UAAU,OAAuB;CACxC,OAAO,WAAW,QAAQ,EAAE,OAAO,KAAK,EAAE,OAAO,KAAK;AACxD;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA,SAAgB,qBACd,UACA,KACQ;CACR,MAAM,EAAE,eAAe,gBAAgB,GAAG,iBAAiB;CAQ3D,OAAO,UAFM,UAAU,iBADJ,CAHO,iBAAiB,YAGP,GAFf,iBAAiB,GAEY,CAAC,EAAE,IAAI,SACR,CAAC,CAE9B;AACtB;;;;;;;;;;AAWA,SAAgB,oBAAoB,KAA2C;CAC7E,MAAM,WAAW,qBAAqB,IAAI,UAAU,IAAI,GAAG;CAE3D,IAAI,IAAI,SAAS,kBAAkB,UACjC,OAAO;EACL,IAAI;EACJ,YAAY,IAAI,SAAS;EACzB,cAAc;CAChB;CAGF,OAAO;EACL,IAAI;EACJ,QAAQ;EACR,YAAY,IAAI,SAAS;EACzB,cAAc;CAChB;AACF"}