@prisma-next/migration-tools 0.3.0-dev.84 → 0.3.0-dev.86

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.
@@ -88,28 +88,66 @@ function errorSelfLoop(dirName, hash) {
88
88
  }
89
89
  });
90
90
  }
91
- function errorAmbiguousLeaf(leaves) {
91
+ function errorAmbiguousLeaf(leaves, context) {
92
+ const divergenceInfo = context ? `\nDivergence point: ${context.divergencePoint}\nBranches:\n${context.branches.map((b) => ` → ${b.leaf} (${b.edges.length} edge(s): ${b.edges.map((e) => e.dirName).join(" → ") || "direct"})`).join("\n")}` : "";
92
93
  return new MigrationToolsError("MIGRATION.AMBIGUOUS_LEAF", "Ambiguous migration graph", {
93
- why: `Multiple leaf nodes found: ${leaves.join(", ")}. The migration graph has diverged — this typically happens when two developers plan migrations from the same starting point.`,
94
- fix: "Delete one of the conflicting migration directories, then re-run `migration plan` to re-plan it from the remaining branch. Or use --from <hash> to explicitly select a starting point.",
95
- details: { leaves }
94
+ why: `Multiple leaf nodes found: ${leaves.join(", ")}. The migration graph has diverged — this typically happens when two developers plan migrations from the same starting point.${divergenceInfo}`,
95
+ fix: "Use `migration ref set <name> <hash>` to target a specific branch, delete one of the conflicting migration directories and re-run `migration plan`, or use --from <hash> to explicitly select a starting point.",
96
+ details: {
97
+ leaves,
98
+ ...context ? {
99
+ divergencePoint: context.divergencePoint,
100
+ branches: context.branches
101
+ } : {}
102
+ }
96
103
  });
97
104
  }
98
105
  function errorNoRoot(nodes) {
99
106
  return new MigrationToolsError("MIGRATION.NO_ROOT", "Migration graph has no root", {
100
- why: `No root migration found in the migration graph (nodes: ${nodes.join(", ")}). Every migration references a parentMigrationId that does not exist, or the graph contains a cycle in parent pointers.`,
101
- fix: "Inspect the migrations directory for corrupted migration.json files. Exactly one migration must have parentMigrationId set to null (the first migration).",
107
+ why: `No root migration found in the migration graph (nodes: ${nodes.join(", ")}). No migration starts from the empty contract hash, or all edges form a disconnected subgraph.`,
108
+ fix: "Inspect the migrations directory for corrupted migration.json files. At least one migration must start from the empty contract hash.",
102
109
  details: { nodes }
103
110
  });
104
111
  }
112
+ function errorInvalidRefs(refsPath, reason) {
113
+ return new MigrationToolsError("MIGRATION.INVALID_REFS", "Invalid refs.json", {
114
+ why: `refs.json at "${refsPath}" is invalid: ${reason}`,
115
+ fix: "Ensure refs.json is a flat object mapping valid ref names to contract hash strings.",
116
+ details: {
117
+ path: refsPath,
118
+ reason
119
+ }
120
+ });
121
+ }
122
+ function errorInvalidRefName(refName) {
123
+ return new MigrationToolsError("MIGRATION.INVALID_REF_NAME", "Invalid ref name", {
124
+ why: `Ref name "${refName}" is invalid. Names must be lowercase alphanumeric with hyphens or forward slashes (no "." or ".." segments).`,
125
+ fix: `Use a valid ref name (e.g., "staging", "envs/production").`,
126
+ details: { refName }
127
+ });
128
+ }
129
+ function errorNoResolvableLeaf(reachableNodes) {
130
+ return new MigrationToolsError("MIGRATION.NO_RESOLVABLE_LEAF", "Migration graph has no resolvable leaf", {
131
+ why: `The migration graph contains cycles and no node has zero outgoing edges (reachable nodes: ${reachableNodes.join(", ")}). This typically happens after rollback migrations (e.g., C1→C2→C1).`,
132
+ fix: "Use --from <hash> to specify the planning origin explicitly.",
133
+ details: { reachableNodes }
134
+ });
135
+ }
136
+ function errorInvalidRefValue(value) {
137
+ return new MigrationToolsError("MIGRATION.INVALID_REF_VALUE", "Invalid ref value", {
138
+ why: `Ref value "${value}" is not a valid contract hash. Values must be in the format "sha256:<64 hex chars>" or "sha256:empty".`,
139
+ fix: "Use a valid storage hash from `prisma-next contract emit` output or an existing migration.",
140
+ details: { value }
141
+ });
142
+ }
105
143
  function errorDuplicateMigrationId(migrationId) {
106
144
  return new MigrationToolsError("MIGRATION.DUPLICATE_MIGRATION_ID", "Duplicate migrationId in migration graph", {
107
- why: `Multiple migrations share migrationId "${migrationId}". This makes parent-chain reconstruction ambiguous and unsafe.`,
145
+ why: `Multiple migrations share migrationId "${migrationId}". Each migration must have a unique content-addressed identity.`,
108
146
  fix: "Regenerate one of the conflicting migrations so each migrationId is unique, then re-run migration commands.",
109
147
  details: { migrationId }
110
148
  });
111
149
  }
112
150
 
113
151
  //#endregion
114
- export { errorInvalidJson as a, errorMissingFile as c, errorDuplicateMigrationId as i, errorNoRoot as l, errorAmbiguousLeaf as n, errorInvalidManifest as o, errorDirectoryExists as r, errorInvalidSlug as s, MigrationToolsError as t, errorSelfLoop as u };
115
- //# sourceMappingURL=errors-DdSjGRqx.mjs.map
152
+ export { errorInvalidJson as a, errorInvalidRefValue as c, errorMissingFile as d, errorNoResolvableLeaf as f, errorDuplicateMigrationId as i, errorInvalidRefs as l, errorSelfLoop as m, errorAmbiguousLeaf as n, errorInvalidManifest as o, errorNoRoot as p, errorDirectoryExists as r, errorInvalidRefName as s, MigrationToolsError as t, errorInvalidSlug as u };
153
+ //# sourceMappingURL=errors-CqLiJwqA.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors-CqLiJwqA.mjs","names":[],"sources":["../src/errors.ts"],"sourcesContent":["/**\n * Structured error for migration tooling operations.\n *\n * Follows the NAMESPACE.SUBCODE convention from ADR 027. All codes live under\n * the MIGRATION namespace. These are tooling-time errors (file I/O, attestation,\n * migration-chain reconstruction), distinct from the runtime MIGRATION.* codes for apply-time\n * failures (PRECHECK_FAILED, POSTCHECK_FAILED, etc.).\n *\n * Fields:\n * - code: Stable machine-readable code (MIGRATION.SUBCODE)\n * - category: Always 'MIGRATION'\n * - why: Explains the cause in plain language\n * - fix: Actionable remediation step\n * - details: Machine-readable structured data for agents\n */\nexport class MigrationToolsError extends Error {\n readonly code: string;\n readonly category = 'MIGRATION' as const;\n readonly why: string;\n readonly fix: string;\n readonly details: Record<string, unknown> | undefined;\n\n constructor(\n code: string,\n summary: string,\n options: {\n readonly why: string;\n readonly fix: string;\n readonly details?: Record<string, unknown>;\n },\n ) {\n super(summary);\n this.name = 'MigrationToolsError';\n this.code = code;\n this.why = options.why;\n this.fix = options.fix;\n this.details = options.details;\n }\n\n static is(error: unknown): error is MigrationToolsError {\n if (!(error instanceof Error)) return false;\n const candidate = error as MigrationToolsError;\n return candidate.name === 'MigrationToolsError' && typeof candidate.code === 'string';\n }\n}\n\nexport function errorDirectoryExists(dir: string): MigrationToolsError {\n return new MigrationToolsError('MIGRATION.DIR_EXISTS', 'Migration directory already exists', {\n why: `The directory \"${dir}\" already exists. Each migration must have a unique directory.`,\n fix: 'Use --name to pick a different name, or delete the existing directory and re-run.',\n details: { dir },\n });\n}\n\nexport function errorMissingFile(file: string, dir: string): MigrationToolsError {\n return new MigrationToolsError('MIGRATION.FILE_MISSING', `Missing ${file}`, {\n why: `Expected \"${file}\" in \"${dir}\" but the file does not exist.`,\n fix: 'Ensure the migration directory contains both migration.json and ops.json. If the directory is corrupt, delete it and re-run migration plan.',\n details: { file, dir },\n });\n}\n\nexport function errorInvalidJson(filePath: string, parseError: string): MigrationToolsError {\n return new MigrationToolsError('MIGRATION.INVALID_JSON', 'Invalid JSON in migration file', {\n why: `Failed to parse \"${filePath}\": ${parseError}`,\n fix: 'Fix the JSON syntax error, or delete the migration directory and re-run migration plan.',\n details: { filePath, parseError },\n });\n}\n\nexport function errorInvalidManifest(filePath: string, reason: string): MigrationToolsError {\n return new MigrationToolsError('MIGRATION.INVALID_MANIFEST', 'Invalid migration manifest', {\n why: `Manifest at \"${filePath}\" is invalid: ${reason}`,\n fix: 'Ensure the manifest has all required fields (from, to, kind, toContract). If corrupt, delete and re-plan.',\n details: { filePath, reason },\n });\n}\n\nexport function errorInvalidSlug(slug: string): MigrationToolsError {\n return new MigrationToolsError('MIGRATION.INVALID_NAME', 'Invalid migration name', {\n why: `The slug \"${slug}\" contains no valid characters after sanitization (only a-z, 0-9 are kept).`,\n fix: 'Provide a name with at least one alphanumeric character, e.g. --name add_users.',\n details: { slug },\n });\n}\n\nexport function errorSelfLoop(dirName: string, hash: string): MigrationToolsError {\n return new MigrationToolsError('MIGRATION.SELF_LOOP', 'Self-loop in migration graph', {\n why: `Migration \"${dirName}\" has from === to === \"${hash}\". A migration must transition between two different contract states.`,\n fix: 'Delete the invalid migration directory and re-run migration plan.',\n details: { dirName, hash },\n });\n}\n\nexport function errorAmbiguousLeaf(\n leaves: readonly string[],\n context?: {\n divergencePoint: string;\n branches: readonly {\n leaf: string;\n edges: readonly { dirName: string; from: string; to: string }[];\n }[];\n },\n): MigrationToolsError {\n const divergenceInfo = context\n ? `\\nDivergence point: ${context.divergencePoint}\\nBranches:\\n${context.branches.map((b) => ` → ${b.leaf} (${b.edges.length} edge(s): ${b.edges.map((e) => e.dirName).join(' → ') || 'direct'})`).join('\\n')}`\n : '';\n return new MigrationToolsError('MIGRATION.AMBIGUOUS_LEAF', 'Ambiguous migration graph', {\n why: `Multiple leaf nodes found: ${leaves.join(', ')}. The migration graph has diverged — this typically happens when two developers plan migrations from the same starting point.${divergenceInfo}`,\n fix: 'Use `migration ref set <name> <hash>` to target a specific branch, delete one of the conflicting migration directories and re-run `migration plan`, or use --from <hash> to explicitly select a starting point.',\n details: {\n leaves,\n ...(context ? { divergencePoint: context.divergencePoint, branches: context.branches } : {}),\n },\n });\n}\n\nexport function errorNoRoot(nodes: readonly string[]): MigrationToolsError {\n return new MigrationToolsError('MIGRATION.NO_ROOT', 'Migration graph has no root', {\n why: `No root migration found in the migration graph (nodes: ${nodes.join(', ')}). No migration starts from the empty contract hash, or all edges form a disconnected subgraph.`,\n fix: 'Inspect the migrations directory for corrupted migration.json files. At least one migration must start from the empty contract hash.',\n details: { nodes },\n });\n}\n\nexport function errorInvalidRefs(refsPath: string, reason: string): MigrationToolsError {\n return new MigrationToolsError('MIGRATION.INVALID_REFS', 'Invalid refs.json', {\n why: `refs.json at \"${refsPath}\" is invalid: ${reason}`,\n fix: 'Ensure refs.json is a flat object mapping valid ref names to contract hash strings.',\n details: { path: refsPath, reason },\n });\n}\n\nexport function errorInvalidRefName(refName: string): MigrationToolsError {\n return new MigrationToolsError('MIGRATION.INVALID_REF_NAME', 'Invalid ref name', {\n why: `Ref name \"${refName}\" is invalid. Names must be lowercase alphanumeric with hyphens or forward slashes (no \".\" or \"..\" segments).`,\n fix: `Use a valid ref name (e.g., \"staging\", \"envs/production\").`,\n details: { refName },\n });\n}\n\nexport function errorNoResolvableLeaf(reachableNodes: readonly string[]): MigrationToolsError {\n return new MigrationToolsError(\n 'MIGRATION.NO_RESOLVABLE_LEAF',\n 'Migration graph has no resolvable leaf',\n {\n why: `The migration graph contains cycles and no node has zero outgoing edges (reachable nodes: ${reachableNodes.join(', ')}). This typically happens after rollback migrations (e.g., C1→C2→C1).`,\n fix: 'Use --from <hash> to specify the planning origin explicitly.',\n details: { reachableNodes },\n },\n );\n}\n\nexport function errorInvalidRefValue(value: string): MigrationToolsError {\n return new MigrationToolsError('MIGRATION.INVALID_REF_VALUE', 'Invalid ref value', {\n why: `Ref value \"${value}\" is not a valid contract hash. Values must be in the format \"sha256:<64 hex chars>\" or \"sha256:empty\".`,\n fix: 'Use a valid storage hash from `prisma-next contract emit` output or an existing migration.',\n details: { value },\n });\n}\n\nexport function errorDuplicateMigrationId(migrationId: string): MigrationToolsError {\n return new MigrationToolsError(\n 'MIGRATION.DUPLICATE_MIGRATION_ID',\n 'Duplicate migrationId in migration graph',\n {\n why: `Multiple migrations share migrationId \"${migrationId}\". Each migration must have a unique content-addressed identity.`,\n fix: 'Regenerate one of the conflicting migrations so each migrationId is unique, then re-run migration commands.',\n details: { migrationId },\n },\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAeA,IAAa,sBAAb,cAAyC,MAAM;CAC7C,AAAS;CACT,AAAS,WAAW;CACpB,AAAS;CACT,AAAS;CACT,AAAS;CAET,YACE,MACA,SACA,SAKA;AACA,QAAM,QAAQ;AACd,OAAK,OAAO;AACZ,OAAK,OAAO;AACZ,OAAK,MAAM,QAAQ;AACnB,OAAK,MAAM,QAAQ;AACnB,OAAK,UAAU,QAAQ;;CAGzB,OAAO,GAAG,OAA8C;AACtD,MAAI,E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3D,KAAK;EACL,SAAS,EAAE,aAAa;EACzB,CACF"}
@@ -1,4 +1,4 @@
1
- import { a as MigrationOps, i as MigrationManifest } from "../types-CUnzoaLY.mjs";
1
+ import { c as MigrationManifest, l as MigrationOps } from "../types-9YQfIg6N.mjs";
2
2
 
3
3
  //#region src/attestation.d.ts
4
4
  interface VerifyResult {
@@ -1,4 +1,4 @@
1
- import { n as readMigrationPackage } from "../io-Dx98-h0p.mjs";
1
+ import { n as readMigrationPackage } from "../io-afog-e8J.mjs";
2
2
  import { writeFile } from "node:fs/promises";
3
3
  import { join } from "pathe";
4
4
  import { createHash } from "node:crypto";
@@ -1,30 +1,49 @@
1
- import { n as MigrationGraph, o as MigrationPackage, t as MigrationChainEntry } from "../types-CUnzoaLY.mjs";
1
+ import { a as MigrationChainEntry, o as MigrationGraph, t as AttestedMigrationBundle } from "../types-9YQfIg6N.mjs";
2
2
 
3
3
  //#region src/dag.d.ts
4
- declare function reconstructGraph(packages: readonly MigrationPackage[]): MigrationGraph;
4
+ declare function reconstructGraph(packages: readonly AttestedMigrationBundle[]): MigrationGraph;
5
5
  /**
6
- * Walk the parent-migration chain to find the latest migration.
7
- * Returns the migration with no children, or null for an empty graph.
8
- * Throws AMBIGUOUS_LEAF if the chain branches.
6
+ * Find the shortest path from `fromHash` to `toHash` using BFS over the
7
+ * contract-hash graph. Returns the ordered list of edges, or null if no path
8
+ * exists. Returns an empty array when `fromHash === toHash` (no-op).
9
+ *
10
+ * Neighbor ordering is deterministic via the tie-break sort key:
11
+ * label priority → createdAt → to → migrationId.
9
12
  */
10
- declare function findLatestMigration(graph: MigrationGraph): MigrationChainEntry | null;
13
+ declare function findPath(graph: MigrationGraph, fromHash: string, toHash: string): readonly MigrationChainEntry[] | null;
14
+ interface PathDecision {
15
+ readonly selectedPath: readonly MigrationChainEntry[];
16
+ readonly fromHash: string;
17
+ readonly toHash: string;
18
+ readonly alternativeCount: number;
19
+ readonly tieBreakReasons: readonly string[];
20
+ readonly refName?: string;
21
+ }
22
+ /**
23
+ * Find the shortest path from `fromHash` to `toHash` and return structured
24
+ * path-decision metadata for machine-readable output.
25
+ */
26
+ declare function findPathWithDecision(graph: MigrationGraph, fromHash: string, toHash: string, refName?: string): PathDecision | null;
11
27
  /**
12
- * Find the leaf contract hash of the migration chain.
13
- * Convenience wrapper around findLatestMigration.
28
+ * Find all leaf nodes reachable from `fromHash` via forward edges.
29
+ * A leaf is a node with no outgoing edges in the graph.
30
+ */
31
+ declare function findReachableLeaves(graph: MigrationGraph, fromHash: string): readonly string[];
32
+ /**
33
+ * Find the leaf contract hash of the migration graph reachable from
34
+ * EMPTY_CONTRACT_HASH. Throws NO_ROOT if the graph has nodes but none
35
+ * originate from the empty hash (e.g. root migration was deleted).
36
+ * Throws AMBIGUOUS_LEAF if multiple leaves exist.
14
37
  */
15
38
  declare function findLeaf(graph: MigrationGraph): string;
16
39
  /**
17
- * Find the ordered chain of migrations from `fromHash` to `toHash` by walking the
18
- * parent-migration chain. Returns the sub-sequence of migrations whose cumulative path
19
- * goes from `fromHash` to `toHash`.
20
- *
21
- * This reconstructs the full chain from root to leaf via parent pointers, then
22
- * extracts the segment between the two hashes. This correctly handles revisited
23
- * contract hashes (e.g. A→B→A) because it operates on migrations, not nodes.
40
+ * Find the latest migration entry by traversing from EMPTY_CONTRACT_HASH
41
+ * to the single leaf. Returns null for an empty graph.
42
+ * Throws AMBIGUOUS_LEAF if the graph has multiple leaves.
24
43
  */
25
- declare function findPath(graph: MigrationGraph, fromHash: string, toHash: string): readonly MigrationChainEntry[] | null;
44
+ declare function findLatestMigration(graph: MigrationGraph): MigrationChainEntry | null;
26
45
  declare function detectCycles(graph: MigrationGraph): readonly string[][];
27
46
  declare function detectOrphans(graph: MigrationGraph): readonly MigrationChainEntry[];
28
47
  //#endregion
29
- export { detectCycles, detectOrphans, findLatestMigration, findLeaf, findPath, reconstructGraph };
48
+ export { type PathDecision, detectCycles, detectOrphans, findLatestMigration, findLeaf, findPath, findPathWithDecision, findReachableLeaves, reconstructGraph };
30
49
  //# sourceMappingURL=dag.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"dag.d.mts","names":[],"sources":["../../src/dag.ts"],"sourcesContent":[],"mappings":";;;iBASgB,gBAAA,oBAAoC,qBAAqB;;AAAzE;AAiEA;AAyCA;AAcA;AA6DgB,iBApHA,mBAAA,CAoHoB,KAAc,EApHP,cAoHO,CAAA,EApHU,mBAoHV,GAAA,IAAA;AAkDlD;;;;iBA7HgB,QAAA,QAAgB;;;;;;;;;;iBAchB,QAAA,QACP,4DAGG;iBAyDI,YAAA,QAAoB;iBAkDpB,aAAA,QAAqB,0BAA0B"}
1
+ {"version":3,"file":"dag.d.mts","names":[],"sources":["../../src/dag.ts"],"sourcesContent":[],"mappings":";;;iBAWgB,gBAAA,oBAAoC,4BAA4B;;AAAhF;AAiFA;AA6CA;AAaA;AAqGA;AAgCA;AAwCA;AAkBgB,iBAzPA,QAAA,CAyPoB,KAAA,EAxP3B,cAwPyC,EAAA,QAAA,EAAA,MAAA,EAAA,MAAA,EAAA,MAAA,CAAA,EAAA,SArPtC,mBAqPsC,EAAA,GAAA,IAAA;AAiDlC,UA7PC,YAAA,CA6PY;kCA5PK;;;;;;;;;;;iBAYlB,oBAAA,QACP,qEAIN;;;;;iBAgGa,mBAAA,QAA2B;;;;;;;iBAgC3B,QAAA,QAAgB;;;;;;iBAwChB,mBAAA,QAA2B,iBAAiB;iBAkB5C,YAAA,QAAoB;iBAiDpB,aAAA,QAAqB,0BAA0B"}
@@ -1,5 +1,6 @@
1
- import { i as errorDuplicateMigrationId, l as errorNoRoot, n as errorAmbiguousLeaf, u as errorSelfLoop } from "../errors-DdSjGRqx.mjs";
1
+ import { f as errorNoResolvableLeaf, i as errorDuplicateMigrationId, m as errorSelfLoop, n as errorAmbiguousLeaf, p as errorNoRoot } from "../errors-CqLiJwqA.mjs";
2
2
  import { EMPTY_CONTRACT_HASH } from "@prisma-next/core-control-plane/constants";
3
+ import { ifDefined } from "@prisma-next/utils/defined";
3
4
 
4
5
  //#region src/dag.ts
5
6
  function reconstructGraph(packages) {
@@ -7,7 +8,6 @@ function reconstructGraph(packages) {
7
8
  const forwardChain = /* @__PURE__ */ new Map();
8
9
  const reverseChain = /* @__PURE__ */ new Map();
9
10
  const migrationById = /* @__PURE__ */ new Map();
10
- const childrenByParentId = /* @__PURE__ */ new Map();
11
11
  for (const pkg of packages) {
12
12
  const { from, to } = pkg.manifest;
13
13
  if (from === to) throw errorSelfLoop(pkg.dirName, from);
@@ -17,7 +17,6 @@ function reconstructGraph(packages) {
17
17
  from,
18
18
  to,
19
19
  migrationId: pkg.manifest.migrationId,
20
- parentMigrationId: pkg.manifest.parentMigrationId,
21
20
  dirName: pkg.dirName,
22
21
  createdAt: pkg.manifest.createdAt,
23
22
  labels: pkg.manifest.labels
@@ -26,10 +25,6 @@ function reconstructGraph(packages) {
26
25
  if (migrationById.has(migration.migrationId)) throw errorDuplicateMigrationId(migration.migrationId);
27
26
  migrationById.set(migration.migrationId, migration);
28
27
  }
29
- const parentId = migration.parentMigrationId;
30
- const siblings = childrenByParentId.get(parentId);
31
- if (siblings) siblings.push(migration);
32
- else childrenByParentId.set(parentId, [migration]);
33
28
  const fwd = forwardChain.get(from);
34
29
  if (fwd) fwd.push(migration);
35
30
  else forwardChain.set(from, [migration]);
@@ -41,91 +36,221 @@ function reconstructGraph(packages) {
41
36
  nodes,
42
37
  forwardChain,
43
38
  reverseChain,
44
- migrationById,
45
- childrenByParentId
39
+ migrationById
46
40
  };
47
41
  }
42
+ const LABEL_PRIORITY = {
43
+ main: 0,
44
+ default: 1,
45
+ feature: 2
46
+ };
47
+ function labelPriority(labels) {
48
+ let best = 3;
49
+ for (const l of labels) {
50
+ const p = LABEL_PRIORITY[l];
51
+ if (p !== void 0 && p < best) best = p;
52
+ }
53
+ return best;
54
+ }
55
+ function sortedNeighbors(edges) {
56
+ return [...edges].sort((a, b) => {
57
+ const lp = labelPriority(a.labels) - labelPriority(b.labels);
58
+ if (lp !== 0) return lp;
59
+ const ca = a.createdAt.localeCompare(b.createdAt);
60
+ if (ca !== 0) return ca;
61
+ const tc = a.to.localeCompare(b.to);
62
+ if (tc !== 0) return tc;
63
+ return (a.migrationId ?? "").localeCompare(b.migrationId ?? "");
64
+ });
65
+ }
48
66
  /**
49
- * Walk the parent-migration chain to find the latest migration.
50
- * Returns the migration with no children, or null for an empty graph.
51
- * Throws AMBIGUOUS_LEAF if the chain branches.
67
+ * Find the shortest path from `fromHash` to `toHash` using BFS over the
68
+ * contract-hash graph. Returns the ordered list of edges, or null if no path
69
+ * exists. Returns an empty array when `fromHash === toHash` (no-op).
70
+ *
71
+ * Neighbor ordering is deterministic via the tie-break sort key:
72
+ * label priority → createdAt → to → migrationId.
52
73
  */
53
- function findLatestMigration(graph) {
54
- if (graph.nodes.size === 0) return null;
55
- const roots = graph.childrenByParentId.get(null);
56
- if (!roots || roots.length === 0) throw errorNoRoot([...graph.nodes].sort());
57
- if (roots.length > 1) throw errorAmbiguousLeaf(roots.map((e) => e.to));
58
- let current = roots[0];
59
- if (!current) throw errorNoRoot([...graph.nodes].sort());
60
- for (let depth = 0; depth < graph.migrationById.size + 1 && current; depth++) {
61
- const children = current.migrationId !== null ? graph.childrenByParentId.get(current.migrationId) : void 0;
62
- if (!children || children.length === 0) return current;
63
- if (children.length > 1) throw errorAmbiguousLeaf(children.map((e) => e.to));
64
- current = children[0];
74
+ function findPath(graph, fromHash, toHash) {
75
+ if (fromHash === toHash) return [];
76
+ const visited = /* @__PURE__ */ new Set();
77
+ const parent = /* @__PURE__ */ new Map();
78
+ const queue = [fromHash];
79
+ visited.add(fromHash);
80
+ while (queue.length > 0) {
81
+ const current = queue.shift();
82
+ if (current === void 0) break;
83
+ if (current === toHash) {
84
+ const path = [];
85
+ let node = toHash;
86
+ let entry = parent.get(node);
87
+ while (entry) {
88
+ const { node: prev, edge } = entry;
89
+ path.push(edge);
90
+ node = prev;
91
+ entry = parent.get(node);
92
+ }
93
+ path.reverse();
94
+ return path;
95
+ }
96
+ const outgoing = graph.forwardChain.get(current);
97
+ if (!outgoing) continue;
98
+ for (const edge of sortedNeighbors(outgoing)) if (!visited.has(edge.to)) {
99
+ visited.add(edge.to);
100
+ parent.set(edge.to, {
101
+ node: current,
102
+ edge
103
+ });
104
+ queue.push(edge.to);
105
+ }
65
106
  }
66
- throw errorNoRoot([...graph.nodes].sort());
107
+ return null;
67
108
  }
68
109
  /**
69
- * Find the leaf contract hash of the migration chain.
70
- * Convenience wrapper around findLatestMigration.
110
+ * Find the shortest path from `fromHash` to `toHash` and return structured
111
+ * path-decision metadata for machine-readable output.
71
112
  */
72
- function findLeaf(graph) {
73
- const migration = findLatestMigration(graph);
74
- return migration ? migration.to : EMPTY_CONTRACT_HASH;
113
+ function findPathWithDecision(graph, fromHash, toHash, refName) {
114
+ if (fromHash === toHash) return {
115
+ selectedPath: [],
116
+ fromHash,
117
+ toHash,
118
+ alternativeCount: 0,
119
+ tieBreakReasons: [],
120
+ ...ifDefined("refName", refName)
121
+ };
122
+ const path = findPath(graph, fromHash, toHash);
123
+ if (!path) return null;
124
+ const tieBreakReasons = [];
125
+ let alternativeCount = 0;
126
+ for (const edge of path) {
127
+ const outgoing = graph.forwardChain.get(edge.from);
128
+ if (outgoing && outgoing.length > 1) {
129
+ const reachable = outgoing.filter((e) => {
130
+ return findPath(graph, e.to, toHash) !== null || e.to === toHash;
131
+ });
132
+ if (reachable.length > 1) {
133
+ alternativeCount += reachable.length - 1;
134
+ const sorted = sortedNeighbors(reachable);
135
+ if (sorted[0] && sorted[0].migrationId === edge.migrationId) {
136
+ if (reachable.some((e) => e.migrationId !== edge.migrationId)) tieBreakReasons.push(`at ${edge.from}: ${reachable.length} candidates, selected by tie-break`);
137
+ }
138
+ }
139
+ }
140
+ }
141
+ return {
142
+ selectedPath: path,
143
+ fromHash,
144
+ toHash,
145
+ alternativeCount,
146
+ tieBreakReasons,
147
+ ...ifDefined("refName", refName)
148
+ };
75
149
  }
76
150
  /**
77
- * Find the ordered chain of migrations from `fromHash` to `toHash` by walking the
78
- * parent-migration chain. Returns the sub-sequence of migrations whose cumulative path
79
- * goes from `fromHash` to `toHash`.
80
- *
81
- * This reconstructs the full chain from root to leaf via parent pointers, then
82
- * extracts the segment between the two hashes. This correctly handles revisited
83
- * contract hashes (e.g. A→B→A) because it operates on migrations, not nodes.
151
+ * Walk ancestors of each leaf back from the leaves to find the last node
152
+ * that appears on all paths. Returns `fromHash` if no shared ancestor is found.
84
153
  */
85
- function findPath(graph, fromHash, toHash) {
86
- if (fromHash === toHash) return [];
87
- const chain = buildChain(graph);
88
- if (!chain) return null;
89
- let startIdx = -1;
90
- if (chain.length > 0 && chain[0]?.from === fromHash) startIdx = 0;
91
- else for (let i = chain.length - 1; i >= 0; i--) if (chain[i]?.to === fromHash) {
92
- startIdx = i + 1;
93
- break;
154
+ function findDivergencePoint(graph, fromHash, leaves) {
155
+ const ancestorSets = leaves.map((leaf) => {
156
+ const ancestors = /* @__PURE__ */ new Set();
157
+ const queue = [leaf];
158
+ while (queue.length > 0) {
159
+ const current = queue.shift();
160
+ if (ancestors.has(current)) continue;
161
+ ancestors.add(current);
162
+ const incoming = graph.reverseChain.get(current);
163
+ if (incoming) for (const edge of incoming) queue.push(edge.from);
164
+ }
165
+ return ancestors;
166
+ });
167
+ const commonAncestors = [...ancestorSets[0] ?? []].filter((node) => ancestorSets.every((s) => s.has(node)));
168
+ let deepest = fromHash;
169
+ let deepestDepth = -1;
170
+ for (const ancestor of commonAncestors) {
171
+ const path = findPath(graph, fromHash, ancestor);
172
+ const depth = path ? path.length : 0;
173
+ if (depth > deepestDepth) {
174
+ deepestDepth = depth;
175
+ deepest = ancestor;
176
+ }
94
177
  }
95
- if (startIdx === -1) return null;
96
- let endIdx = -1;
97
- for (let i = chain.length - 1; i >= startIdx; i--) if (chain[i]?.to === toHash) {
98
- endIdx = i + 1;
99
- break;
178
+ return deepest;
179
+ }
180
+ /**
181
+ * Find all leaf nodes reachable from `fromHash` via forward edges.
182
+ * A leaf is a node with no outgoing edges in the graph.
183
+ */
184
+ function findReachableLeaves(graph, fromHash) {
185
+ const visited = /* @__PURE__ */ new Set();
186
+ const queue = [fromHash];
187
+ visited.add(fromHash);
188
+ const leaves = [];
189
+ while (queue.length > 0) {
190
+ const current = queue.shift();
191
+ if (current === void 0) break;
192
+ const outgoing = graph.forwardChain.get(current);
193
+ if (!outgoing || outgoing.length === 0) leaves.push(current);
194
+ else for (const edge of outgoing) if (!visited.has(edge.to)) {
195
+ visited.add(edge.to);
196
+ queue.push(edge.to);
197
+ }
100
198
  }
101
- if (endIdx === -1) return null;
102
- return chain.slice(startIdx, endIdx);
199
+ return leaves;
103
200
  }
104
201
  /**
105
- * Build the full ordered chain of migrations from root to leaf by following
106
- * parent pointers. Returns null if the chain cannot be reconstructed
107
- * (e.g. missing root, branches).
202
+ * Find the leaf contract hash of the migration graph reachable from
203
+ * EMPTY_CONTRACT_HASH. Throws NO_ROOT if the graph has nodes but none
204
+ * originate from the empty hash (e.g. root migration was deleted).
205
+ * Throws AMBIGUOUS_LEAF if multiple leaves exist.
108
206
  */
109
- function buildChain(graph) {
110
- const roots = graph.childrenByParentId.get(null);
111
- if (!roots || roots.length !== 1) return null;
112
- const chain = [];
113
- let current = roots[0];
114
- for (let depth = 0; depth < graph.migrationById.size + 1 && current; depth++) {
115
- chain.push(current);
116
- const children = current.migrationId !== null ? graph.childrenByParentId.get(current.migrationId) : void 0;
117
- if (!children || children.length === 0) break;
118
- if (children.length > 1) return null;
119
- current = children[0];
207
+ function findLeaf(graph) {
208
+ if (graph.nodes.size === 0) return EMPTY_CONTRACT_HASH;
209
+ if (!graph.nodes.has(EMPTY_CONTRACT_HASH)) throw errorNoRoot([...graph.nodes]);
210
+ const leaves = findReachableLeaves(graph, EMPTY_CONTRACT_HASH);
211
+ if (leaves.length === 0) {
212
+ const reachable = [...graph.nodes].filter((n) => n !== EMPTY_CONTRACT_HASH);
213
+ if (reachable.length > 0) throw errorNoResolvableLeaf(reachable);
214
+ return EMPTY_CONTRACT_HASH;
215
+ }
216
+ if (leaves.length > 1) {
217
+ const divergencePoint = findDivergencePoint(graph, EMPTY_CONTRACT_HASH, leaves);
218
+ throw errorAmbiguousLeaf(leaves, {
219
+ divergencePoint,
220
+ branches: leaves.map((leaf$1) => {
221
+ return {
222
+ leaf: leaf$1,
223
+ edges: (findPath(graph, divergencePoint, leaf$1) ?? []).map((e) => ({
224
+ dirName: e.dirName,
225
+ from: e.from,
226
+ to: e.to
227
+ }))
228
+ };
229
+ })
230
+ });
120
231
  }
121
- return chain;
232
+ const leaf = leaves[0];
233
+ return leaf !== void 0 ? leaf : EMPTY_CONTRACT_HASH;
234
+ }
235
+ /**
236
+ * Find the latest migration entry by traversing from EMPTY_CONTRACT_HASH
237
+ * to the single leaf. Returns null for an empty graph.
238
+ * Throws AMBIGUOUS_LEAF if the graph has multiple leaves.
239
+ */
240
+ function findLatestMigration(graph) {
241
+ if (graph.nodes.size === 0) return null;
242
+ const leafHash = findLeaf(graph);
243
+ if (leafHash === EMPTY_CONTRACT_HASH) return null;
244
+ const path = findPath(graph, EMPTY_CONTRACT_HASH, leafHash);
245
+ if (!path || path.length === 0) return null;
246
+ return path[path.length - 1] ?? null;
122
247
  }
123
248
  function detectCycles(graph) {
124
249
  const WHITE = 0;
125
250
  const GRAY = 1;
126
251
  const BLACK = 2;
127
252
  const color = /* @__PURE__ */ new Map();
128
- const parent = /* @__PURE__ */ new Map();
253
+ const parentMap = /* @__PURE__ */ new Map();
129
254
  const cycles = [];
130
255
  for (const node of graph.nodes) color.set(node, WHITE);
131
256
  function dfs(u) {
@@ -138,19 +263,19 @@ function detectCycles(graph) {
138
263
  let cur = u;
139
264
  while (cur !== v) {
140
265
  cycle.push(cur);
141
- cur = parent.get(cur) ?? v;
266
+ cur = parentMap.get(cur) ?? v;
142
267
  }
143
268
  cycle.reverse();
144
269
  cycles.push(cycle);
145
270
  } else if (color.get(v) === WHITE) {
146
- parent.set(v, u);
271
+ parentMap.set(v, u);
147
272
  dfs(v);
148
273
  }
149
274
  }
150
275
  color.set(u, BLACK);
151
276
  }
152
277
  for (const node of graph.nodes) if (color.get(node) === WHITE) {
153
- parent.set(node, null);
278
+ parentMap.set(node, null);
154
279
  dfs(node);
155
280
  }
156
281
  return cycles;
@@ -158,9 +283,14 @@ function detectCycles(graph) {
158
283
  function detectOrphans(graph) {
159
284
  if (graph.nodes.size === 0) return [];
160
285
  const reachable = /* @__PURE__ */ new Set();
161
- const rootMigrations = graph.childrenByParentId.get(null) ?? [];
162
- const rootHashes = rootMigrations.some((migration) => migration.from === EMPTY_CONTRACT_HASH) ? [EMPTY_CONTRACT_HASH] : [...new Set(rootMigrations.map((migration) => migration.from))];
163
- const queue = rootHashes.length > 0 ? rootHashes : [EMPTY_CONTRACT_HASH];
286
+ const startNodes = [];
287
+ if (graph.forwardChain.has(EMPTY_CONTRACT_HASH)) startNodes.push(EMPTY_CONTRACT_HASH);
288
+ else {
289
+ const allTargets = /* @__PURE__ */ new Set();
290
+ for (const edges of graph.forwardChain.values()) for (const edge of edges) allTargets.add(edge.to);
291
+ for (const node of graph.nodes) if (!allTargets.has(node)) startNodes.push(node);
292
+ }
293
+ const queue = [...startNodes];
164
294
  for (const hash of queue) reachable.add(hash);
165
295
  while (queue.length > 0) {
166
296
  const node = queue.shift();
@@ -178,5 +308,5 @@ function detectOrphans(graph) {
178
308
  }
179
309
 
180
310
  //#endregion
181
- export { detectCycles, detectOrphans, findLatestMigration, findLeaf, findPath, reconstructGraph };
311
+ export { detectCycles, detectOrphans, findLatestMigration, findLeaf, findPath, findPathWithDecision, findReachableLeaves, reconstructGraph };
182
312
  //# sourceMappingURL=dag.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"dag.mjs","names":["migration: MigrationChainEntry","children: readonly MigrationChainEntry[] | undefined","chain: MigrationChainEntry[]","current: MigrationChainEntry | undefined","cycles: string[][]","cycle: string[]","queue: string[]","orphans: MigrationChainEntry[]"],"sources":["../../src/dag.ts"],"sourcesContent":["import { EMPTY_CONTRACT_HASH } from '@prisma-next/core-control-plane/constants';\nimport {\n errorAmbiguousLeaf,\n errorDuplicateMigrationId,\n errorNoRoot,\n errorSelfLoop,\n} from './errors';\nimport type { MigrationChainEntry, MigrationGraph, MigrationPackage } from './types';\n\nexport function reconstructGraph(packages: readonly MigrationPackage[]): MigrationGraph {\n const nodes = new Set<string>();\n const forwardChain = new Map<string, MigrationChainEntry[]>();\n const reverseChain = new Map<string, MigrationChainEntry[]>();\n const migrationById = new Map<string, MigrationChainEntry>();\n const childrenByParentId = new Map<string | null, MigrationChainEntry[]>();\n\n for (const pkg of packages) {\n const { from, to } = pkg.manifest;\n\n if (from === to) {\n throw errorSelfLoop(pkg.dirName, from);\n }\n\n nodes.add(from);\n nodes.add(to);\n\n const migration: MigrationChainEntry = {\n from,\n to,\n migrationId: pkg.manifest.migrationId,\n parentMigrationId: pkg.manifest.parentMigrationId,\n dirName: pkg.dirName,\n createdAt: pkg.manifest.createdAt,\n labels: pkg.manifest.labels,\n };\n\n if (migration.migrationId !== null) {\n if (migrationById.has(migration.migrationId)) {\n throw errorDuplicateMigrationId(migration.migrationId);\n }\n migrationById.set(migration.migrationId, migration);\n }\n\n const parentId = migration.parentMigrationId;\n const siblings = childrenByParentId.get(parentId);\n if (siblings) {\n siblings.push(migration);\n } else {\n childrenByParentId.set(parentId, [migration]);\n }\n\n const fwd = forwardChain.get(from);\n if (fwd) {\n fwd.push(migration);\n } else {\n forwardChain.set(from, [migration]);\n }\n\n const rev = reverseChain.get(to);\n if (rev) {\n rev.push(migration);\n } else {\n reverseChain.set(to, [migration]);\n }\n }\n\n return { nodes, forwardChain, reverseChain, migrationById, childrenByParentId };\n}\n\n/**\n * Walk the parent-migration chain to find the latest migration.\n * Returns the migration with no children, or null for an empty graph.\n * Throws AMBIGUOUS_LEAF if the chain branches.\n */\nexport function findLatestMigration(graph: MigrationGraph): MigrationChainEntry | null {\n if (graph.nodes.size === 0) {\n return null;\n }\n\n const roots = graph.childrenByParentId.get(null);\n if (!roots || roots.length === 0) {\n throw errorNoRoot([...graph.nodes].sort());\n }\n\n if (roots.length > 1) {\n throw errorAmbiguousLeaf(roots.map((e) => e.to));\n }\n\n let current = roots[0];\n if (!current) {\n throw errorNoRoot([...graph.nodes].sort());\n }\n\n for (let depth = 0; depth < graph.migrationById.size + 1 && current; depth++) {\n const children: readonly MigrationChainEntry[] | undefined =\n current.migrationId !== null ? graph.childrenByParentId.get(current.migrationId) : undefined;\n\n if (!children || children.length === 0) {\n return current;\n }\n\n if (children.length > 1) {\n throw errorAmbiguousLeaf(children.map((e) => e.to));\n }\n\n current = children[0];\n }\n\n throw errorNoRoot([...graph.nodes].sort());\n}\n\n/**\n * Find the leaf contract hash of the migration chain.\n * Convenience wrapper around findLatestMigration.\n */\nexport function findLeaf(graph: MigrationGraph): string {\n const migration = findLatestMigration(graph);\n return migration ? migration.to : EMPTY_CONTRACT_HASH;\n}\n\n/**\n * Find the ordered chain of migrations from `fromHash` to `toHash` by walking the\n * parent-migration chain. Returns the sub-sequence of migrations whose cumulative path\n * goes from `fromHash` to `toHash`.\n *\n * This reconstructs the full chain from root to leaf via parent pointers, then\n * extracts the segment between the two hashes. This correctly handles revisited\n * contract hashes (e.g. A→B→A) because it operates on migrations, not nodes.\n */\nexport function findPath(\n graph: MigrationGraph,\n fromHash: string,\n toHash: string,\n): readonly MigrationChainEntry[] | null {\n if (fromHash === toHash) return [];\n\n const chain = buildChain(graph);\n if (!chain) return null;\n\n let startIdx = -1;\n if (chain.length > 0 && chain[0]?.from === fromHash) {\n startIdx = 0;\n } else {\n for (let i = chain.length - 1; i >= 0; i--) {\n if (chain[i]?.to === fromHash) {\n startIdx = i + 1;\n break;\n }\n }\n }\n\n if (startIdx === -1) return null;\n\n let endIdx = -1;\n for (let i = chain.length - 1; i >= startIdx; i--) {\n if (chain[i]?.to === toHash) {\n endIdx = i + 1;\n break;\n }\n }\n\n if (endIdx === -1) return null;\n\n return chain.slice(startIdx, endIdx);\n}\n\n/**\n * Build the full ordered chain of migrations from root to leaf by following\n * parent pointers. Returns null if the chain cannot be reconstructed\n * (e.g. missing root, branches).\n */\nfunction buildChain(graph: MigrationGraph): readonly MigrationChainEntry[] | null {\n const roots = graph.childrenByParentId.get(null);\n if (!roots || roots.length !== 1) return null;\n\n const chain: MigrationChainEntry[] = [];\n let current: MigrationChainEntry | undefined = roots[0];\n\n for (let depth = 0; depth < graph.migrationById.size + 1 && current; depth++) {\n chain.push(current);\n const children =\n current.migrationId !== null ? graph.childrenByParentId.get(current.migrationId) : undefined;\n if (!children || children.length === 0) break;\n if (children.length > 1) return null;\n current = children[0];\n }\n\n return chain;\n}\n\nexport function detectCycles(graph: MigrationGraph): readonly string[][] {\n const WHITE = 0;\n const GRAY = 1;\n const BLACK = 2;\n\n const color = new Map<string, number>();\n const parent = new Map<string, string | null>();\n const cycles: string[][] = [];\n\n for (const node of graph.nodes) {\n color.set(node, WHITE);\n }\n\n function dfs(u: string): void {\n color.set(u, GRAY);\n\n const outgoing = graph.forwardChain.get(u);\n if (outgoing) {\n for (const edge of outgoing) {\n const v = edge.to;\n if (color.get(v) === GRAY) {\n // Back edge found — reconstruct cycle\n const cycle: string[] = [v];\n let cur = u;\n while (cur !== v) {\n cycle.push(cur);\n cur = parent.get(cur) ?? v;\n }\n cycle.reverse();\n cycles.push(cycle);\n } else if (color.get(v) === WHITE) {\n parent.set(v, u);\n dfs(v);\n }\n }\n }\n\n color.set(u, BLACK);\n }\n\n for (const node of graph.nodes) {\n if (color.get(node) === WHITE) {\n parent.set(node, null);\n dfs(node);\n }\n }\n\n return cycles;\n}\n\nexport function detectOrphans(graph: MigrationGraph): readonly MigrationChainEntry[] {\n if (graph.nodes.size === 0) return [];\n\n const reachable = new Set<string>();\n const rootMigrations = graph.childrenByParentId.get(null) ?? [];\n const emptyRootExists = rootMigrations.some(\n (migration) => migration.from === EMPTY_CONTRACT_HASH,\n );\n const rootHashes = emptyRootExists\n ? [EMPTY_CONTRACT_HASH]\n : [...new Set(rootMigrations.map((migration) => migration.from))];\n const queue: string[] = rootHashes.length > 0 ? rootHashes : [EMPTY_CONTRACT_HASH];\n\n for (const hash of queue) {\n reachable.add(hash);\n }\n\n while (queue.length > 0) {\n const node = queue.shift();\n if (node === undefined) break;\n const outgoing = graph.forwardChain.get(node);\n if (!outgoing) continue;\n\n for (const migration of outgoing) {\n if (!reachable.has(migration.to)) {\n reachable.add(migration.to);\n queue.push(migration.to);\n }\n }\n }\n\n const orphans: MigrationChainEntry[] = [];\n for (const [from, migrations] of graph.forwardChain) {\n if (!reachable.has(from)) {\n orphans.push(...migrations);\n }\n }\n\n return orphans;\n}\n"],"mappings":";;;;AASA,SAAgB,iBAAiB,UAAuD;CACtF,MAAM,wBAAQ,IAAI,KAAa;CAC/B,MAAM,+BAAe,IAAI,KAAoC;CAC7D,MAAM,+BAAe,IAAI,KAAoC;CAC7D,MAAM,gCAAgB,IAAI,KAAkC;CAC5D,MAAM,qCAAqB,IAAI,KAA2C;AAE1E,MAAK,MAAM,OAAO,UAAU;EAC1B,MAAM,EAAE,MAAM,OAAO,IAAI;AAEzB,MAAI,SAAS,GACX,OAAM,cAAc,IAAI,SAAS,KAAK;AAGxC,QAAM,IAAI,KAAK;AACf,QAAM,IAAI,GAAG;EAEb,MAAMA,YAAiC;GACrC;GACA;GACA,aAAa,IAAI,SAAS;GAC1B,mBAAmB,IAAI,SAAS;GAChC,SAAS,IAAI;GACb,WAAW,IAAI,SAAS;GACxB,QAAQ,IAAI,SAAS;GACtB;AAED,MAAI,UAAU,gBAAgB,MAAM;AAClC,OAAI,cAAc,IAAI,UAAU,YAAY,CAC1C,OAAM,0BAA0B,UAAU,YAAY;AAExD,iBAAc,IAAI,UAAU,aAAa,UAAU;;EAGrD,MAAM,WAAW,UAAU;EAC3B,MAAM,WAAW,mBAAmB,IAAI,SAAS;AACjD,MAAI,SACF,UAAS,KAAK,UAAU;MAExB,oBAAmB,IAAI,UAAU,CAAC,UAAU,CAAC;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AI,CAAC,UAAU,IAAI,UAAU,GAAG,EAAE;AAChC,aAAU,IAAI,UAAU,GAAG;AAC3B,SAAM,KAAK,UAAU,GAAG;;;CAK9B,MAAMC,UAAiC,EAAE;AACzC,MAAK,MAAM,CAAC,MAAM,eAAe,MAAM,aACrC,KAAI,CAAC,UAAU,IAAI,KAAK,CACtB,SAAQ,KAAK,GAAG,WAAW;AAI/B,QAAO"}
1
+ {"version":3,"file":"dag.mjs","names":["migration: MigrationChainEntry","LABEL_PRIORITY: Record<string, number>","queue: string[]","path: MigrationChainEntry[]","tieBreakReasons: string[]","leaves: string[]","leaf","cycles: string[][]","cycle: string[]","startNodes: string[]","orphans: MigrationChainEntry[]"],"sources":["../../src/dag.ts"],"sourcesContent":["import { EMPTY_CONTRACT_HASH } from '@prisma-next/core-control-plane/constants';\nimport { ifDefined } from '@prisma-next/utils/defined';\nimport {\n errorAmbiguousLeaf,\n errorDuplicateMigrationId,\n errorNoResolvableLeaf,\n errorNoRoot,\n errorSelfLoop,\n} from './errors';\nimport type { AttestedMigrationBundle, MigrationChainEntry, MigrationGraph } from './types';\n\nexport function reconstructGraph(packages: readonly AttestedMigrationBundle[]): MigrationGraph {\n const nodes = new Set<string>();\n const forwardChain = new Map<string, MigrationChainEntry[]>();\n const reverseChain = new Map<string, MigrationChainEntry[]>();\n const migrationById = new Map<string, MigrationChainEntry>();\n\n for (const pkg of packages) {\n const { from, to } = pkg.manifest;\n\n if (from === to) {\n throw errorSelfLoop(pkg.dirName, from);\n }\n\n nodes.add(from);\n nodes.add(to);\n\n const migration: MigrationChainEntry = {\n from,\n to,\n migrationId: pkg.manifest.migrationId,\n dirName: pkg.dirName,\n createdAt: pkg.manifest.createdAt,\n labels: pkg.manifest.labels,\n };\n\n if (migration.migrationId !== null) {\n if (migrationById.has(migration.migrationId)) {\n throw errorDuplicateMigrationId(migration.migrationId);\n }\n migrationById.set(migration.migrationId, migration);\n }\n\n const fwd = forwardChain.get(from);\n if (fwd) {\n fwd.push(migration);\n } else {\n forwardChain.set(from, [migration]);\n }\n\n const rev = reverseChain.get(to);\n if (rev) {\n rev.push(migration);\n } else {\n reverseChain.set(to, [migration]);\n }\n }\n\n return { nodes, forwardChain, reverseChain, migrationById };\n}\n\nconst LABEL_PRIORITY: Record<string, number> = { main: 0, default: 1, feature: 2 };\n\nfunction labelPriority(labels: readonly string[]): number {\n let best = 3;\n for (const l of labels) {\n const p = LABEL_PRIORITY[l];\n if (p !== undefined && p < best) best = p;\n }\n return best;\n}\n\nfunction sortedNeighbors(edges: readonly MigrationChainEntry[]): readonly MigrationChainEntry[] {\n return [...edges].sort((a, b) => {\n const lp = labelPriority(a.labels) - labelPriority(b.labels);\n if (lp !== 0) return lp;\n const ca = a.createdAt.localeCompare(b.createdAt);\n if (ca !== 0) return ca;\n const tc = a.to.localeCompare(b.to);\n if (tc !== 0) return tc;\n return (a.migrationId ?? '').localeCompare(b.migrationId ?? '');\n });\n}\n\n/**\n * Find the shortest path from `fromHash` to `toHash` using BFS over the\n * contract-hash graph. Returns the ordered list of edges, or null if no path\n * exists. Returns an empty array when `fromHash === toHash` (no-op).\n *\n * Neighbor ordering is deterministic via the tie-break sort key:\n * label priority → createdAt → to → migrationId.\n */\nexport function findPath(\n graph: MigrationGraph,\n fromHash: string,\n toHash: string,\n): readonly MigrationChainEntry[] | null {\n if (fromHash === toHash) return [];\n\n const visited = new Set<string>();\n const parent = new Map<string, { node: string; edge: MigrationChainEntry }>();\n const queue: string[] = [fromHash];\n visited.add(fromHash);\n\n while (queue.length > 0) {\n const current = queue.shift();\n if (current === undefined) break;\n\n if (current === toHash) {\n const path: MigrationChainEntry[] = [];\n let node = toHash;\n let entry = parent.get(node);\n while (entry) {\n const { node: prev, edge } = entry;\n path.push(edge);\n node = prev;\n entry = parent.get(node);\n }\n path.reverse();\n return path;\n }\n\n const outgoing = graph.forwardChain.get(current);\n if (!outgoing) continue;\n\n for (const edge of sortedNeighbors(outgoing)) {\n if (!visited.has(edge.to)) {\n visited.add(edge.to);\n parent.set(edge.to, { node: current, edge });\n queue.push(edge.to);\n }\n }\n }\n\n return null;\n}\n\nexport interface PathDecision {\n readonly selectedPath: readonly MigrationChainEntry[];\n readonly fromHash: string;\n readonly toHash: string;\n readonly alternativeCount: number;\n readonly tieBreakReasons: readonly string[];\n readonly refName?: string;\n}\n\n/**\n * Find the shortest path from `fromHash` to `toHash` and return structured\n * path-decision metadata for machine-readable output.\n */\nexport function findPathWithDecision(\n graph: MigrationGraph,\n fromHash: string,\n toHash: string,\n refName?: string,\n): PathDecision | null {\n if (fromHash === toHash) {\n return {\n selectedPath: [],\n fromHash,\n toHash,\n alternativeCount: 0,\n tieBreakReasons: [],\n ...ifDefined('refName', refName),\n };\n }\n\n const path = findPath(graph, fromHash, toHash);\n if (!path) return null;\n\n const tieBreakReasons: string[] = [];\n let alternativeCount = 0;\n\n for (const edge of path) {\n const outgoing = graph.forwardChain.get(edge.from);\n if (outgoing && outgoing.length > 1) {\n const reachable = outgoing.filter((e) => {\n const pathFromE = findPath(graph, e.to, toHash);\n return pathFromE !== null || e.to === toHash;\n });\n if (reachable.length > 1) {\n alternativeCount += reachable.length - 1;\n const sorted = sortedNeighbors(reachable);\n if (sorted[0] && sorted[0].migrationId === edge.migrationId) {\n if (reachable.some((e) => e.migrationId !== edge.migrationId)) {\n tieBreakReasons.push(\n `at ${edge.from}: ${reachable.length} candidates, selected by tie-break`,\n );\n }\n }\n }\n }\n }\n\n return {\n selectedPath: path,\n fromHash,\n toHash,\n alternativeCount,\n tieBreakReasons,\n ...ifDefined('refName', refName),\n };\n}\n\n/**\n * Walk ancestors of each leaf back from the leaves to find the last node\n * that appears on all paths. Returns `fromHash` if no shared ancestor is found.\n */\nfunction findDivergencePoint(\n graph: MigrationGraph,\n fromHash: string,\n leaves: readonly string[],\n): string {\n const ancestorSets = leaves.map((leaf) => {\n const ancestors = new Set<string>();\n const queue = [leaf];\n while (queue.length > 0) {\n const current = queue.shift() as string;\n if (ancestors.has(current)) continue;\n ancestors.add(current);\n const incoming = graph.reverseChain.get(current);\n if (incoming) {\n for (const edge of incoming) {\n queue.push(edge.from);\n }\n }\n }\n return ancestors;\n });\n\n const commonAncestors = [...(ancestorSets[0] ?? [])].filter((node) =>\n ancestorSets.every((s) => s.has(node)),\n );\n\n let deepest = fromHash;\n let deepestDepth = -1;\n for (const ancestor of commonAncestors) {\n const path = findPath(graph, fromHash, ancestor);\n const depth = path ? path.length : 0;\n if (depth > deepestDepth) {\n deepestDepth = depth;\n deepest = ancestor;\n }\n }\n return deepest;\n}\n\n/**\n * Find all leaf nodes reachable from `fromHash` via forward edges.\n * A leaf is a node with no outgoing edges in the graph.\n */\nexport function findReachableLeaves(graph: MigrationGraph, fromHash: string): readonly string[] {\n const visited = new Set<string>();\n const queue: string[] = [fromHash];\n visited.add(fromHash);\n const leaves: string[] = [];\n\n while (queue.length > 0) {\n const current = queue.shift();\n if (current === undefined) break;\n const outgoing = graph.forwardChain.get(current);\n\n if (!outgoing || outgoing.length === 0) {\n leaves.push(current);\n } else {\n for (const edge of outgoing) {\n if (!visited.has(edge.to)) {\n visited.add(edge.to);\n queue.push(edge.to);\n }\n }\n }\n }\n\n return leaves;\n}\n\n/**\n * Find the leaf contract hash of the migration graph reachable from\n * EMPTY_CONTRACT_HASH. Throws NO_ROOT if the graph has nodes but none\n * originate from the empty hash (e.g. root migration was deleted).\n * Throws AMBIGUOUS_LEAF if multiple leaves exist.\n */\nexport function findLeaf(graph: MigrationGraph): string {\n if (graph.nodes.size === 0) {\n return EMPTY_CONTRACT_HASH;\n }\n\n if (!graph.nodes.has(EMPTY_CONTRACT_HASH)) {\n throw errorNoRoot([...graph.nodes]);\n }\n\n const leaves = findReachableLeaves(graph, EMPTY_CONTRACT_HASH);\n\n if (leaves.length === 0) {\n const reachable = [...graph.nodes].filter((n) => n !== EMPTY_CONTRACT_HASH);\n if (reachable.length > 0) {\n throw errorNoResolvableLeaf(reachable);\n }\n return EMPTY_CONTRACT_HASH;\n }\n\n if (leaves.length > 1) {\n const divergencePoint = findDivergencePoint(graph, EMPTY_CONTRACT_HASH, leaves);\n const branches = leaves.map((leaf) => {\n const path = findPath(graph, divergencePoint, leaf);\n return {\n leaf,\n edges: (path ?? []).map((e) => ({ dirName: e.dirName, from: e.from, to: e.to })),\n };\n });\n throw errorAmbiguousLeaf(leaves, { divergencePoint, branches });\n }\n\n const leaf = leaves[0];\n return leaf !== undefined ? leaf : EMPTY_CONTRACT_HASH;\n}\n\n/**\n * Find the latest migration entry by traversing from EMPTY_CONTRACT_HASH\n * to the single leaf. Returns null for an empty graph.\n * Throws AMBIGUOUS_LEAF if the graph has multiple leaves.\n */\nexport function findLatestMigration(graph: MigrationGraph): MigrationChainEntry | null {\n if (graph.nodes.size === 0) {\n return null;\n }\n\n const leafHash = findLeaf(graph);\n if (leafHash === EMPTY_CONTRACT_HASH) {\n return null;\n }\n\n const path = findPath(graph, EMPTY_CONTRACT_HASH, leafHash);\n if (!path || path.length === 0) {\n return null;\n }\n\n return path[path.length - 1] ?? null;\n}\n\nexport function detectCycles(graph: MigrationGraph): readonly string[][] {\n const WHITE = 0;\n const GRAY = 1;\n const BLACK = 2;\n\n const color = new Map<string, number>();\n const parentMap = new Map<string, string | null>();\n const cycles: string[][] = [];\n\n for (const node of graph.nodes) {\n color.set(node, WHITE);\n }\n\n function dfs(u: string): void {\n color.set(u, GRAY);\n\n const outgoing = graph.forwardChain.get(u);\n if (outgoing) {\n for (const edge of outgoing) {\n const v = edge.to;\n if (color.get(v) === GRAY) {\n const cycle: string[] = [v];\n let cur = u;\n while (cur !== v) {\n cycle.push(cur);\n cur = parentMap.get(cur) ?? v;\n }\n cycle.reverse();\n cycles.push(cycle);\n } else if (color.get(v) === WHITE) {\n parentMap.set(v, u);\n dfs(v);\n }\n }\n }\n\n color.set(u, BLACK);\n }\n\n for (const node of graph.nodes) {\n if (color.get(node) === WHITE) {\n parentMap.set(node, null);\n dfs(node);\n }\n }\n\n return cycles;\n}\n\nexport function detectOrphans(graph: MigrationGraph): readonly MigrationChainEntry[] {\n if (graph.nodes.size === 0) return [];\n\n const reachable = new Set<string>();\n const startNodes: string[] = [];\n\n if (graph.forwardChain.has(EMPTY_CONTRACT_HASH)) {\n startNodes.push(EMPTY_CONTRACT_HASH);\n } else {\n const allTargets = new Set<string>();\n for (const edges of graph.forwardChain.values()) {\n for (const edge of edges) {\n allTargets.add(edge.to);\n }\n }\n for (const node of graph.nodes) {\n if (!allTargets.has(node)) {\n startNodes.push(node);\n }\n }\n }\n\n const queue = [...startNodes];\n for (const hash of queue) {\n reachable.add(hash);\n }\n\n while (queue.length > 0) {\n const node = queue.shift();\n if (node === undefined) break;\n const outgoing = graph.forwardChain.get(node);\n if (!outgoing) continue;\n\n for (const migration of outgoing) {\n if (!reachable.has(migration.to)) {\n reachable.add(migration.to);\n queue.push(migration.to);\n }\n }\n }\n\n const orphans: MigrationChainEntry[] = [];\n for (const [from, migrations] of graph.forwardChain) {\n if (!reachable.has(from)) {\n orphans.push(...migrations);\n }\n }\n\n return orphans;\n}\n"],"mappings":";;;;;AAWA,SAAgB,iBAAiB,UAA8D;CAC7F,MAAM,wBAAQ,IAAI,KAAa;CAC/B,MAAM,+BAAe,IAAI,KAAoC;CAC7D,MAAM,+BAAe,IAAI,KAAoC;CAC7D,MAAM,gCAAgB,IAAI,KAAkC;AAE5D,MAAK,MAAM,OAAO,UAAU;EAC1B,MAAM,EAAE,MAAM,OAAO,IAAI;AAEzB,MAAI,SAAS,GACX,OAAM,cAAc,IAAI,SAAS,KAAK;AAGxC,QAAM,IAAI,KAAK;AACf,QAAM,IAAI,GAAG;EAEb,MAAMA,YAAiC;GACrC;GACA;GACA,aAAa,IAAI,SAAS;GAC1B,SAAS,IAAI;GACb,WAAW,IAAI,SAAS;GACxB,QAAQ,IAAI,SAAS;GACtB;AAED,MAAI,UAAU,gBAAgB,MAAM;AAClC,OAAI,cAAc,IAAI,UAAU,YAAY,CAC1C,OAAM,0BAA0B,UAAU,YAAY;AAExD,iBAAc,IA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