@prisma-next/migration-tools 0.5.0-dev.67 → 0.5.0-dev.69
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.
- package/dist/{errors-5KVuWV_5.mjs → errors-EPL_9p9f.mjs} +12 -6
- package/dist/errors-EPL_9p9f.mjs.map +1 -0
- package/dist/exports/aggregate.d.mts +534 -0
- package/dist/exports/aggregate.d.mts.map +1 -0
- package/dist/exports/aggregate.mjs +598 -0
- package/dist/exports/aggregate.mjs.map +1 -0
- package/dist/exports/errors.d.mts +6 -1
- package/dist/exports/errors.d.mts.map +1 -1
- package/dist/exports/errors.mjs +2 -2
- package/dist/exports/graph.d.mts +1 -1
- package/dist/exports/hash.d.mts +1 -1
- package/dist/exports/invariants.d.mts +13 -2
- package/dist/exports/invariants.d.mts.map +1 -1
- package/dist/exports/invariants.mjs +1 -1
- package/dist/exports/io.d.mts +25 -1
- package/dist/exports/io.d.mts.map +1 -1
- package/dist/exports/io.mjs +2 -2
- package/dist/exports/metadata.d.mts +1 -1
- package/dist/exports/migration-graph.d.mts +1 -1
- package/dist/exports/migration-graph.mjs +1 -522
- package/dist/exports/migration.d.mts +1 -1
- package/dist/exports/migration.mjs +2 -2
- package/dist/exports/refs.mjs +1 -1
- package/dist/exports/spaces.d.mts +341 -237
- package/dist/exports/spaces.d.mts.map +1 -1
- package/dist/exports/spaces.mjs +137 -339
- package/dist/exports/spaces.mjs.map +1 -1
- package/dist/{graph-4dIUm90i.d.mts → graph-HMWAldoR.d.mts} +1 -1
- package/dist/{graph-4dIUm90i.d.mts.map → graph-HMWAldoR.d.mts.map} +1 -1
- package/dist/{invariants-CkLSBcMu.mjs → invariants-Duc8f9NM.mjs} +16 -5
- package/dist/invariants-Duc8f9NM.mjs.map +1 -0
- package/dist/{io-TX8RPDeh.mjs → io-D13dLvUh.mjs} +38 -4
- package/dist/io-D13dLvUh.mjs.map +1 -0
- package/dist/migration-graph-DGNnKDY5.mjs +523 -0
- package/dist/{exports/migration-graph.mjs.map → migration-graph-DGNnKDY5.mjs.map} +1 -1
- package/dist/read-contract-space-contract-C3-1eyaI.mjs +298 -0
- package/dist/read-contract-space-contract-C3-1eyaI.mjs.map +1 -0
- package/package.json +10 -6
- package/src/aggregate/loader.ts +409 -0
- package/src/aggregate/marker-types.ts +16 -0
- package/src/aggregate/planner-types.ts +137 -0
- package/src/aggregate/planner.ts +158 -0
- package/src/aggregate/project-schema-to-space.ts +64 -0
- package/src/aggregate/strategies/graph-walk.ts +92 -0
- package/src/aggregate/strategies/synth.ts +122 -0
- package/src/aggregate/types.ts +89 -0
- package/src/aggregate/verifier.ts +230 -0
- package/src/assert-descriptor-self-consistency.ts +70 -0
- package/src/compute-extension-space-apply-path.ts +152 -0
- package/src/concatenate-space-apply-inputs.ts +2 -2
- package/src/detect-space-contract-drift.ts +22 -26
- package/src/{emit-pinned-space-artefacts.ts → emit-contract-space-artefacts.ts} +14 -33
- package/src/errors.ts +11 -5
- package/src/exports/aggregate.ts +37 -0
- package/src/exports/errors.ts +1 -0
- package/src/exports/io.ts +1 -0
- package/src/exports/spaces.ts +23 -10
- package/src/gather-disk-contract-space-state.ts +62 -0
- package/src/invariants.ts +14 -3
- package/src/io.ts +42 -0
- package/src/plan-all-spaces.ts +3 -7
- package/src/read-contract-space-contract.ts +44 -0
- package/src/read-contract-space-head-ref.ts +63 -0
- package/src/space-layout.ts +4 -11
- package/src/verify-contract-spaces.ts +45 -49
- package/dist/errors-5KVuWV_5.mjs.map +0 -1
- package/dist/invariants-CkLSBcMu.mjs.map +0 -1
- package/dist/io-TX8RPDeh.mjs.map +0 -1
- package/src/read-pinned-contract-hash.ts +0 -77
- /package/dist/{metadata-th_MvOTT.d.mts → metadata-BnLFiI6B.d.mts} +0 -0
|
@@ -0,0 +1,523 @@
|
|
|
1
|
+
import { S as errorNoTarget, b as errorNoInitialMigration, n as errorAmbiguousTarget, o as errorDuplicateMigrationHash, w as errorSameSourceAndTarget } from "./errors-EPL_9p9f.mjs";
|
|
2
|
+
import { t as EMPTY_CONTRACT_HASH } from "./constants-DWV9_o2Z.mjs";
|
|
3
|
+
import { ifDefined } from "@prisma-next/utils/defined";
|
|
4
|
+
//#region src/queue.ts
|
|
5
|
+
/**
|
|
6
|
+
* FIFO queue with amortised O(1) push and shift.
|
|
7
|
+
*
|
|
8
|
+
* Uses a head-index cursor over a backing array rather than
|
|
9
|
+
* `Array.prototype.shift()`, which is O(n) on V8. Intended for BFS-shaped
|
|
10
|
+
* traversals where the queue is drained in a single pass — it does not
|
|
11
|
+
* reclaim memory for already-shifted items, so it is not suitable for
|
|
12
|
+
* long-lived queues with many push/shift cycles.
|
|
13
|
+
*/
|
|
14
|
+
var Queue = class {
|
|
15
|
+
items;
|
|
16
|
+
head = 0;
|
|
17
|
+
constructor(initial = []) {
|
|
18
|
+
this.items = [...initial];
|
|
19
|
+
}
|
|
20
|
+
push(item) {
|
|
21
|
+
this.items.push(item);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Remove and return the next item. Caller must check `isEmpty` first —
|
|
25
|
+
* shifting an empty queue throws.
|
|
26
|
+
*/
|
|
27
|
+
shift() {
|
|
28
|
+
if (this.head >= this.items.length) throw new Error("Queue.shift called on empty queue");
|
|
29
|
+
return this.items[this.head++];
|
|
30
|
+
}
|
|
31
|
+
get isEmpty() {
|
|
32
|
+
return this.head >= this.items.length;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/graph-ops.ts
|
|
37
|
+
function* bfs(starts, neighbours, key = (state) => state) {
|
|
38
|
+
const visited = /* @__PURE__ */ new Set();
|
|
39
|
+
const parentMap = /* @__PURE__ */ new Map();
|
|
40
|
+
const queue = new Queue();
|
|
41
|
+
for (const start of starts) {
|
|
42
|
+
const k = key(start);
|
|
43
|
+
if (!visited.has(k)) {
|
|
44
|
+
visited.add(k);
|
|
45
|
+
queue.push({
|
|
46
|
+
state: start,
|
|
47
|
+
key: k
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
while (!queue.isEmpty) {
|
|
52
|
+
const { state: current, key: curKey } = queue.shift();
|
|
53
|
+
const parentInfo = parentMap.get(curKey);
|
|
54
|
+
yield {
|
|
55
|
+
state: current,
|
|
56
|
+
parent: parentInfo?.parent ?? null,
|
|
57
|
+
incomingEdge: parentInfo?.edge ?? null
|
|
58
|
+
};
|
|
59
|
+
for (const { next, edge } of neighbours(current)) {
|
|
60
|
+
const k = key(next);
|
|
61
|
+
if (!visited.has(k)) {
|
|
62
|
+
visited.add(k);
|
|
63
|
+
parentMap.set(k, {
|
|
64
|
+
parent: current,
|
|
65
|
+
edge
|
|
66
|
+
});
|
|
67
|
+
queue.push({
|
|
68
|
+
state: next,
|
|
69
|
+
key: k
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
//#endregion
|
|
76
|
+
//#region src/migration-graph.ts
|
|
77
|
+
/** Forward-edge neighbours: edge `e` from `n` visits `e.to` next. */
|
|
78
|
+
function forwardNeighbours(graph, node) {
|
|
79
|
+
return (graph.forwardChain.get(node) ?? []).map((edge) => ({
|
|
80
|
+
next: edge.to,
|
|
81
|
+
edge
|
|
82
|
+
}));
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Forward-edge neighbours, sorted by the deterministic tie-break.
|
|
86
|
+
* Used by path-finding so the resulting shortest path is stable across runs.
|
|
87
|
+
*/
|
|
88
|
+
function sortedForwardNeighbours(graph, node) {
|
|
89
|
+
return [...graph.forwardChain.get(node) ?? []].sort(compareTieBreak).map((edge) => ({
|
|
90
|
+
next: edge.to,
|
|
91
|
+
edge
|
|
92
|
+
}));
|
|
93
|
+
}
|
|
94
|
+
/** Reverse-edge neighbours: edge `e` from `n` visits `e.from` next. */
|
|
95
|
+
function reverseNeighbours(graph, node) {
|
|
96
|
+
return (graph.reverseChain.get(node) ?? []).map((edge) => ({
|
|
97
|
+
next: edge.from,
|
|
98
|
+
edge
|
|
99
|
+
}));
|
|
100
|
+
}
|
|
101
|
+
function appendEdge(map, key, entry) {
|
|
102
|
+
const bucket = map.get(key);
|
|
103
|
+
if (bucket) bucket.push(entry);
|
|
104
|
+
else map.set(key, [entry]);
|
|
105
|
+
}
|
|
106
|
+
function reconstructGraph(packages) {
|
|
107
|
+
const nodes = /* @__PURE__ */ new Set();
|
|
108
|
+
const forwardChain = /* @__PURE__ */ new Map();
|
|
109
|
+
const reverseChain = /* @__PURE__ */ new Map();
|
|
110
|
+
const migrationByHash = /* @__PURE__ */ new Map();
|
|
111
|
+
for (const pkg of packages) {
|
|
112
|
+
const from = pkg.metadata.from ?? "sha256:empty";
|
|
113
|
+
const { to } = pkg.metadata;
|
|
114
|
+
if (from === to) {
|
|
115
|
+
if (!pkg.ops.some((op) => op.operationClass === "data")) throw errorSameSourceAndTarget(pkg.dirPath, from);
|
|
116
|
+
}
|
|
117
|
+
nodes.add(from);
|
|
118
|
+
nodes.add(to);
|
|
119
|
+
const migration = {
|
|
120
|
+
from,
|
|
121
|
+
to,
|
|
122
|
+
migrationHash: pkg.metadata.migrationHash,
|
|
123
|
+
dirName: pkg.dirName,
|
|
124
|
+
createdAt: pkg.metadata.createdAt,
|
|
125
|
+
labels: pkg.metadata.labels,
|
|
126
|
+
invariants: pkg.metadata.providedInvariants
|
|
127
|
+
};
|
|
128
|
+
if (migrationByHash.has(migration.migrationHash)) throw errorDuplicateMigrationHash(migration.migrationHash);
|
|
129
|
+
migrationByHash.set(migration.migrationHash, migration);
|
|
130
|
+
appendEdge(forwardChain, from, migration);
|
|
131
|
+
appendEdge(reverseChain, to, migration);
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
nodes,
|
|
135
|
+
forwardChain,
|
|
136
|
+
reverseChain,
|
|
137
|
+
migrationByHash
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
const LABEL_PRIORITY = {
|
|
141
|
+
main: 0,
|
|
142
|
+
default: 1,
|
|
143
|
+
feature: 2
|
|
144
|
+
};
|
|
145
|
+
function labelPriority(labels) {
|
|
146
|
+
let best = 3;
|
|
147
|
+
for (const l of labels) {
|
|
148
|
+
const p = LABEL_PRIORITY[l];
|
|
149
|
+
if (p !== void 0 && p < best) best = p;
|
|
150
|
+
}
|
|
151
|
+
return best;
|
|
152
|
+
}
|
|
153
|
+
function compareTieBreak(a, b) {
|
|
154
|
+
const lp = labelPriority(a.labels) - labelPriority(b.labels);
|
|
155
|
+
if (lp !== 0) return lp;
|
|
156
|
+
const ca = a.createdAt.localeCompare(b.createdAt);
|
|
157
|
+
if (ca !== 0) return ca;
|
|
158
|
+
const tc = a.to.localeCompare(b.to);
|
|
159
|
+
if (tc !== 0) return tc;
|
|
160
|
+
return a.migrationHash.localeCompare(b.migrationHash);
|
|
161
|
+
}
|
|
162
|
+
function sortedNeighbors(edges) {
|
|
163
|
+
return [...edges].sort(compareTieBreak);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Find the shortest path from `fromHash` to `toHash` using BFS over the
|
|
167
|
+
* contract-hash graph. Returns the ordered list of edges, or null if no path
|
|
168
|
+
* exists. Returns an empty array when `fromHash === toHash` (no-op).
|
|
169
|
+
*
|
|
170
|
+
* Neighbor ordering is deterministic via the tie-break sort key:
|
|
171
|
+
* label priority → createdAt → to → migrationHash.
|
|
172
|
+
*/
|
|
173
|
+
function findPath(graph, fromHash, toHash) {
|
|
174
|
+
if (fromHash === toHash) return [];
|
|
175
|
+
const parents = /* @__PURE__ */ new Map();
|
|
176
|
+
for (const step of bfs([fromHash], (n) => sortedForwardNeighbours(graph, n))) {
|
|
177
|
+
if (step.parent !== null && step.incomingEdge !== null) parents.set(step.state, {
|
|
178
|
+
parent: step.parent,
|
|
179
|
+
edge: step.incomingEdge
|
|
180
|
+
});
|
|
181
|
+
if (step.state === toHash) {
|
|
182
|
+
const path = [];
|
|
183
|
+
let cur = toHash;
|
|
184
|
+
let p = parents.get(cur);
|
|
185
|
+
while (p) {
|
|
186
|
+
path.push(p.edge);
|
|
187
|
+
cur = p.parent;
|
|
188
|
+
p = parents.get(cur);
|
|
189
|
+
}
|
|
190
|
+
path.reverse();
|
|
191
|
+
return path;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return null;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Find the shortest path from `fromHash` to `toHash` whose edges collectively
|
|
198
|
+
* cover every invariant in `required`. Returns `null` when no such path exists
|
|
199
|
+
* (either `fromHash`→`toHash` is structurally unreachable, or every reachable
|
|
200
|
+
* path leaves at least one required invariant uncovered). When `required` is
|
|
201
|
+
* empty, delegates to `findPath` so the result is byte-identical for that case.
|
|
202
|
+
*
|
|
203
|
+
* Algorithm: BFS over `(node, coveredSubset)` states with state-level dedup.
|
|
204
|
+
* The covered subset is a `Set<string>` of invariant ids; the state's dedup
|
|
205
|
+
* key is `${node}\0${[...covered].sort().join('\0')}`. State keys distinguish
|
|
206
|
+
* distinct `(node, covered)` tuples regardless of node-name length because
|
|
207
|
+
* `\0` cannot appear in any invariant id (validation rejects whitespace and
|
|
208
|
+
* control chars at authoring time).
|
|
209
|
+
*
|
|
210
|
+
* Neighbour ordering when `required ≠ ∅`: edges covering ≥1 still-needed
|
|
211
|
+
* invariant come first, with `labelPriority → createdAt → to → migrationHash`
|
|
212
|
+
* as the secondary key. The heuristic steers BFS toward the satisfying path;
|
|
213
|
+
* correctness (shortest, deterministic) does not depend on it.
|
|
214
|
+
*/
|
|
215
|
+
function findPathWithInvariants(graph, fromHash, toHash, required) {
|
|
216
|
+
if (required.size === 0) return findPath(graph, fromHash, toHash);
|
|
217
|
+
const stateKey = (s) => {
|
|
218
|
+
if (s.covered.size === 0) return `${s.node}\0`;
|
|
219
|
+
return `${s.node}\0${[...s.covered].sort().join("\0")}`;
|
|
220
|
+
};
|
|
221
|
+
const neighbours = (s) => {
|
|
222
|
+
const outgoing = graph.forwardChain.get(s.node) ?? [];
|
|
223
|
+
if (outgoing.length === 0) return [];
|
|
224
|
+
return [...outgoing].map((edge) => {
|
|
225
|
+
let useful = false;
|
|
226
|
+
let next = null;
|
|
227
|
+
for (const inv of edge.invariants) if (required.has(inv) && !s.covered.has(inv)) {
|
|
228
|
+
if (next === null) next = new Set(s.covered);
|
|
229
|
+
next.add(inv);
|
|
230
|
+
useful = true;
|
|
231
|
+
}
|
|
232
|
+
return {
|
|
233
|
+
edge,
|
|
234
|
+
useful,
|
|
235
|
+
nextCovered: next ?? s.covered
|
|
236
|
+
};
|
|
237
|
+
}).sort((a, b) => {
|
|
238
|
+
if (a.useful !== b.useful) return a.useful ? -1 : 1;
|
|
239
|
+
return compareTieBreak(a.edge, b.edge);
|
|
240
|
+
}).map(({ edge, nextCovered }) => ({
|
|
241
|
+
next: {
|
|
242
|
+
node: edge.to,
|
|
243
|
+
covered: nextCovered
|
|
244
|
+
},
|
|
245
|
+
edge
|
|
246
|
+
}));
|
|
247
|
+
};
|
|
248
|
+
const parents = /* @__PURE__ */ new Map();
|
|
249
|
+
for (const step of bfs([{
|
|
250
|
+
node: fromHash,
|
|
251
|
+
covered: /* @__PURE__ */ new Set()
|
|
252
|
+
}], neighbours, stateKey)) {
|
|
253
|
+
const curKey = stateKey(step.state);
|
|
254
|
+
if (step.parent !== null && step.incomingEdge !== null) parents.set(curKey, {
|
|
255
|
+
parentKey: stateKey(step.parent),
|
|
256
|
+
edge: step.incomingEdge
|
|
257
|
+
});
|
|
258
|
+
if (step.state.node === toHash && step.state.covered.size === required.size) {
|
|
259
|
+
const path = [];
|
|
260
|
+
let cur = curKey;
|
|
261
|
+
while (cur !== void 0) {
|
|
262
|
+
const p = parents.get(cur);
|
|
263
|
+
if (!p) break;
|
|
264
|
+
path.push(p.edge);
|
|
265
|
+
cur = p.parentKey;
|
|
266
|
+
}
|
|
267
|
+
path.reverse();
|
|
268
|
+
return path;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return null;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Reverse-BFS from `toHash` over `reverseChain` to collect every node from
|
|
275
|
+
* which `toHash` is reachable (inclusive of `toHash` itself).
|
|
276
|
+
*/
|
|
277
|
+
function collectNodesReachingTarget(graph, toHash) {
|
|
278
|
+
const reached = /* @__PURE__ */ new Set();
|
|
279
|
+
for (const step of bfs([toHash], (n) => reverseNeighbours(graph, n))) reached.add(step.state);
|
|
280
|
+
return reached;
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Find the shortest path from `fromHash` to `toHash` and return structured
|
|
284
|
+
* path-decision metadata for machine-readable output. When `required` is
|
|
285
|
+
* non-empty, the returned path is the shortest one whose edges collectively
|
|
286
|
+
* cover every required invariant.
|
|
287
|
+
*
|
|
288
|
+
* The discriminated return type tells the caller *why* a path could not be
|
|
289
|
+
* found, so the CLI can pick the right structured error without re-running
|
|
290
|
+
* a structural BFS.
|
|
291
|
+
*/
|
|
292
|
+
function findPathWithDecision(graph, fromHash, toHash, options = {}) {
|
|
293
|
+
const { refName, required = /* @__PURE__ */ new Set() } = options;
|
|
294
|
+
const requiredInvariants = [...required].sort();
|
|
295
|
+
if (fromHash === toHash && required.size === 0) return {
|
|
296
|
+
kind: "ok",
|
|
297
|
+
decision: {
|
|
298
|
+
selectedPath: [],
|
|
299
|
+
fromHash,
|
|
300
|
+
toHash,
|
|
301
|
+
alternativeCount: 0,
|
|
302
|
+
tieBreakReasons: [],
|
|
303
|
+
requiredInvariants,
|
|
304
|
+
satisfiedInvariants: [],
|
|
305
|
+
...ifDefined("refName", refName)
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
const path = findPathWithInvariants(graph, fromHash, toHash, required);
|
|
309
|
+
if (!path) {
|
|
310
|
+
if (required.size === 0) return { kind: "unreachable" };
|
|
311
|
+
const structural = findPath(graph, fromHash, toHash);
|
|
312
|
+
if (structural === null) return { kind: "unreachable" };
|
|
313
|
+
const coveredByStructural = /* @__PURE__ */ new Set();
|
|
314
|
+
for (const edge of structural) for (const inv of edge.invariants) if (required.has(inv)) coveredByStructural.add(inv);
|
|
315
|
+
return {
|
|
316
|
+
kind: "unsatisfiable",
|
|
317
|
+
structuralPath: structural,
|
|
318
|
+
missing: requiredInvariants.filter((id) => !coveredByStructural.has(id))
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
const satisfiedInvariants = computeSatisfiedInvariants(required, path);
|
|
322
|
+
const reachesTarget = collectNodesReachingTarget(graph, toHash);
|
|
323
|
+
const coveragePrefixes = requiredCoveragePrefixes(required, path);
|
|
324
|
+
const tieBreakReasons = [];
|
|
325
|
+
let alternativeCount = 0;
|
|
326
|
+
for (const [i, edge] of path.entries()) {
|
|
327
|
+
const outgoing = graph.forwardChain.get(edge.from);
|
|
328
|
+
if (!outgoing || outgoing.length <= 1) continue;
|
|
329
|
+
const reachable = outgoing.filter((e) => reachesTarget.has(e.to));
|
|
330
|
+
if (reachable.length <= 1) continue;
|
|
331
|
+
let comparisonPool = reachable;
|
|
332
|
+
if (required.size > 0) {
|
|
333
|
+
const prefixSet = coveragePrefixes[i];
|
|
334
|
+
if (prefixSet === void 0) continue;
|
|
335
|
+
comparisonPool = invariantViableAlternativesAtStep(required, prefixSet, reachable);
|
|
336
|
+
}
|
|
337
|
+
alternativeCount += reachable.length - 1;
|
|
338
|
+
if (sortedNeighbors(reachable)[0]?.migrationHash !== edge.migrationHash) continue;
|
|
339
|
+
if (!reachable.some((e) => e.migrationHash !== edge.migrationHash)) continue;
|
|
340
|
+
const sortedViable = sortedNeighbors(comparisonPool);
|
|
341
|
+
if (sortedViable.length > 1 && sortedViable[0]?.migrationHash === edge.migrationHash && sortedViable.some((e) => e.migrationHash !== edge.migrationHash)) tieBreakReasons.push(`at ${edge.from}: ${comparisonPool.length} candidates, selected by tie-break`);
|
|
342
|
+
}
|
|
343
|
+
return {
|
|
344
|
+
kind: "ok",
|
|
345
|
+
decision: {
|
|
346
|
+
selectedPath: path,
|
|
347
|
+
fromHash,
|
|
348
|
+
toHash,
|
|
349
|
+
alternativeCount,
|
|
350
|
+
tieBreakReasons,
|
|
351
|
+
requiredInvariants,
|
|
352
|
+
satisfiedInvariants,
|
|
353
|
+
...ifDefined("refName", refName)
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
function computeSatisfiedInvariants(required, path) {
|
|
358
|
+
if (required.size === 0) return [];
|
|
359
|
+
const covered = /* @__PURE__ */ new Set();
|
|
360
|
+
for (const edge of path) for (const inv of edge.invariants) if (required.has(inv)) covered.add(inv);
|
|
361
|
+
return [...covered].sort();
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* For each edge on path, invariant coverage accumulated from earlier edges only —
|
|
365
|
+
* `(required ∩ ∪_{j<i} path[j].invariants)` represented as cumulative set along `required`,
|
|
366
|
+
* keyed as "full set of required ids satisfied before taking path[i]".
|
|
367
|
+
*/
|
|
368
|
+
function requiredCoveragePrefixes(required, path) {
|
|
369
|
+
const prefixes = [];
|
|
370
|
+
const acc = /* @__PURE__ */ new Set();
|
|
371
|
+
for (const edge of path) {
|
|
372
|
+
prefixes.push(new Set(acc));
|
|
373
|
+
for (const inv of edge.invariants) if (required.has(inv)) acc.add(inv);
|
|
374
|
+
}
|
|
375
|
+
return prefixes;
|
|
376
|
+
}
|
|
377
|
+
function invariantViableAlternativesAtStep(required, coverageBeforeTakingEdge, outgoing) {
|
|
378
|
+
if (required.size === 0) return [...outgoing];
|
|
379
|
+
return outgoing.filter((e) => [...required].every((id) => coverageBeforeTakingEdge.has(id) || e.invariants.includes(id)));
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Walk ancestors of each branch tip back to find the last node
|
|
383
|
+
* that appears on all paths. Returns `fromHash` if no shared ancestor is found.
|
|
384
|
+
*/
|
|
385
|
+
function findDivergencePoint(graph, fromHash, leaves) {
|
|
386
|
+
const ancestorSets = leaves.map((leaf) => {
|
|
387
|
+
const ancestors = /* @__PURE__ */ new Set();
|
|
388
|
+
for (const step of bfs([leaf], (n) => reverseNeighbours(graph, n))) ancestors.add(step.state);
|
|
389
|
+
return ancestors;
|
|
390
|
+
});
|
|
391
|
+
const commonAncestors = [...ancestorSets[0] ?? []].filter((node) => ancestorSets.every((s) => s.has(node)));
|
|
392
|
+
let deepest = fromHash;
|
|
393
|
+
let deepestDepth = -1;
|
|
394
|
+
for (const ancestor of commonAncestors) {
|
|
395
|
+
const path = findPath(graph, fromHash, ancestor);
|
|
396
|
+
const depth = path ? path.length : 0;
|
|
397
|
+
if (depth > deepestDepth) {
|
|
398
|
+
deepestDepth = depth;
|
|
399
|
+
deepest = ancestor;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
return deepest;
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Find all branch tips (nodes with no outgoing edges) reachable from
|
|
406
|
+
* `fromHash` via forward edges.
|
|
407
|
+
*/
|
|
408
|
+
function findReachableLeaves(graph, fromHash) {
|
|
409
|
+
const leaves = [];
|
|
410
|
+
for (const step of bfs([fromHash], (n) => forwardNeighbours(graph, n))) if (!graph.forwardChain.get(step.state)?.length) leaves.push(step.state);
|
|
411
|
+
return leaves;
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Find the target contract hash of the migration graph reachable from
|
|
415
|
+
* EMPTY_CONTRACT_HASH. Returns `null` for a graph that has no target
|
|
416
|
+
* state (either empty, or containing only the root with no outgoing
|
|
417
|
+
* edges). Throws NO_INITIAL_MIGRATION if the graph has nodes but none
|
|
418
|
+
* originate from the empty hash, and AMBIGUOUS_TARGET if multiple
|
|
419
|
+
* branch tips exist.
|
|
420
|
+
*/
|
|
421
|
+
function findLeaf(graph) {
|
|
422
|
+
if (graph.nodes.size === 0) return null;
|
|
423
|
+
if (!graph.nodes.has("sha256:empty")) throw errorNoInitialMigration([...graph.nodes]);
|
|
424
|
+
const leaves = findReachableLeaves(graph, EMPTY_CONTRACT_HASH);
|
|
425
|
+
if (leaves.length === 0) {
|
|
426
|
+
const reachable = [...graph.nodes].filter((n) => n !== EMPTY_CONTRACT_HASH);
|
|
427
|
+
if (reachable.length > 0) throw errorNoTarget(reachable);
|
|
428
|
+
return null;
|
|
429
|
+
}
|
|
430
|
+
if (leaves.length > 1) {
|
|
431
|
+
const divergencePoint = findDivergencePoint(graph, EMPTY_CONTRACT_HASH, leaves);
|
|
432
|
+
throw errorAmbiguousTarget(leaves, {
|
|
433
|
+
divergencePoint,
|
|
434
|
+
branches: leaves.map((tip) => {
|
|
435
|
+
return {
|
|
436
|
+
tip,
|
|
437
|
+
edges: (findPath(graph, divergencePoint, tip) ?? []).map((e) => ({
|
|
438
|
+
dirName: e.dirName,
|
|
439
|
+
from: e.from,
|
|
440
|
+
to: e.to
|
|
441
|
+
}))
|
|
442
|
+
};
|
|
443
|
+
})
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
return leaves[0];
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* Find the latest migration entry by traversing from EMPTY_CONTRACT_HASH
|
|
450
|
+
* to the single target. Returns null for an empty graph.
|
|
451
|
+
* Throws AMBIGUOUS_TARGET if the graph has multiple branch tips.
|
|
452
|
+
*/
|
|
453
|
+
function findLatestMigration(graph) {
|
|
454
|
+
const leafHash = findLeaf(graph);
|
|
455
|
+
if (leafHash === null) return null;
|
|
456
|
+
return findPath(graph, "sha256:empty", leafHash)?.at(-1) ?? null;
|
|
457
|
+
}
|
|
458
|
+
function detectCycles(graph) {
|
|
459
|
+
const WHITE = 0;
|
|
460
|
+
const GRAY = 1;
|
|
461
|
+
const BLACK = 2;
|
|
462
|
+
const color = /* @__PURE__ */ new Map();
|
|
463
|
+
const parentMap = /* @__PURE__ */ new Map();
|
|
464
|
+
const cycles = [];
|
|
465
|
+
for (const node of graph.nodes) color.set(node, WHITE);
|
|
466
|
+
const stack = [];
|
|
467
|
+
function pushFrame(u) {
|
|
468
|
+
color.set(u, GRAY);
|
|
469
|
+
stack.push({
|
|
470
|
+
node: u,
|
|
471
|
+
outgoing: graph.forwardChain.get(u) ?? [],
|
|
472
|
+
index: 0
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
for (const root of graph.nodes) {
|
|
476
|
+
if (color.get(root) !== WHITE) continue;
|
|
477
|
+
parentMap.set(root, null);
|
|
478
|
+
pushFrame(root);
|
|
479
|
+
while (stack.length > 0) {
|
|
480
|
+
const frame = stack[stack.length - 1];
|
|
481
|
+
if (frame.index >= frame.outgoing.length) {
|
|
482
|
+
color.set(frame.node, BLACK);
|
|
483
|
+
stack.pop();
|
|
484
|
+
continue;
|
|
485
|
+
}
|
|
486
|
+
const v = frame.outgoing[frame.index++].to;
|
|
487
|
+
const vColor = color.get(v);
|
|
488
|
+
if (vColor === GRAY) {
|
|
489
|
+
const cycle = [v];
|
|
490
|
+
let cur = frame.node;
|
|
491
|
+
while (cur !== v) {
|
|
492
|
+
cycle.push(cur);
|
|
493
|
+
cur = parentMap.get(cur) ?? v;
|
|
494
|
+
}
|
|
495
|
+
cycle.reverse();
|
|
496
|
+
cycles.push(cycle);
|
|
497
|
+
} else if (vColor === WHITE) {
|
|
498
|
+
parentMap.set(v, frame.node);
|
|
499
|
+
pushFrame(v);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
return cycles;
|
|
504
|
+
}
|
|
505
|
+
function detectOrphans(graph) {
|
|
506
|
+
if (graph.nodes.size === 0) return [];
|
|
507
|
+
const reachable = /* @__PURE__ */ new Set();
|
|
508
|
+
const startNodes = [];
|
|
509
|
+
if (graph.forwardChain.has("sha256:empty")) startNodes.push(EMPTY_CONTRACT_HASH);
|
|
510
|
+
else {
|
|
511
|
+
const allTargets = /* @__PURE__ */ new Set();
|
|
512
|
+
for (const edges of graph.forwardChain.values()) for (const edge of edges) allTargets.add(edge.to);
|
|
513
|
+
for (const node of graph.nodes) if (!allTargets.has(node)) startNodes.push(node);
|
|
514
|
+
}
|
|
515
|
+
for (const step of bfs(startNodes, (n) => forwardNeighbours(graph, n))) reachable.add(step.state);
|
|
516
|
+
const orphans = [];
|
|
517
|
+
for (const [from, migrations] of graph.forwardChain) if (!reachable.has(from)) orphans.push(...migrations);
|
|
518
|
+
return orphans;
|
|
519
|
+
}
|
|
520
|
+
//#endregion
|
|
521
|
+
export { findPath as a, findReachableLeaves as c, findLeaf as i, reconstructGraph as l, detectOrphans as n, findPathWithDecision as o, findLatestMigration as r, findPathWithInvariants as s, detectCycles as t };
|
|
522
|
+
|
|
523
|
+
//# sourceMappingURL=migration-graph-DGNnKDY5.mjs.map
|