-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathplanner.ts
More file actions
158 lines (146 loc) · 5.65 KB
/
planner.ts
File metadata and controls
158 lines (146 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { notOk, ok } from '@prisma-next/utils/result';
import { requireHeadRef } from './aggregate';
import type { PerSpacePlan, PlannerError, PlannerInput, PlannerOutput } from './planner-types';
import { graphWalkStrategy } from './strategies/graph-walk';
import { synthStrategy } from './strategies/synth';
import type { ContractSpaceMember } from './types';
export type {
AggregateCurrentDBState,
AggregateMigrationEdgeRef,
CallerPolicy,
PerSpacePlan,
PlannerError,
PlannerInput,
PlannerOutput,
PlannerSuccess,
} from './planner-types';
/**
* Plan a migration across every member of a {@link ContractSpaceAggregate}.
*
* Strategy selection per member, in order; first match wins:
*
* 1. If `callerPolicy.ignoreGraphFor.has(member.spaceId)`:
* - If `member.headRef.invariants` is empty → synth.
* - Else → `policyConflict` (synth cannot satisfy authored invariants).
* 2. Else if `member.graph()` is non-empty AND graph-walk
* succeeds → graph-walk.
* 3. Else if `member.headRef.invariants` is empty → synth.
* 4. Else → graph-walk failure → `extensionPathUnreachable` /
* `extensionPathUnsatisfiable`.
*
* Output `applyOrder` is `[...aggregate.extensions.map(spaceId), aggregate.app.spaceId]`
* — extensions alphabetical, then app — matching today's
* `concatenateSpaceApplyInputs` ordering. This preserves
* `MigrationRunnerFailure.failingSpace` attribution byte-for-byte.
*
* Every emitted `MigrationPlan` has `targetId = aggregate.targetId`.
* No placeholder cast; no patch step.
*/
export async function planMigration<TFamilyId extends string, TTargetId extends string>(
input: PlannerInput<TFamilyId, TTargetId>,
): Promise<PlannerOutput> {
const { aggregate, currentDBState, callerPolicy } = input;
const allMembers: ReadonlyArray<ContractSpaceMember> = [aggregate.app, ...aggregate.extensions];
const perSpace = new Map<string, PerSpacePlan>();
// Iterate in apply order so a per-member error short-circuits the
// walk in the same order the runner would walk inputs.
const orderedMembers: ReadonlyArray<ContractSpaceMember> = [
...aggregate.extensions,
aggregate.app,
];
for (const member of orderedMembers) {
const otherMembers = allMembers.filter((m) => m.spaceId !== member.spaceId);
const currentMarker = currentDBState.markersBySpaceId.get(member.spaceId) ?? null;
const headRef = requireHeadRef(member);
const ignoreGraph = callerPolicy.ignoreGraphFor.has(member.spaceId);
const invariantsRequired = headRef.invariants.length > 0;
if (ignoreGraph && invariantsRequired) {
const conflict: PlannerError = {
kind: 'policyConflict',
spaceId: member.spaceId,
detail: `\`callerPolicy.ignoreGraphFor\` requested for space "${member.spaceId}", but the member declares non-empty head-ref invariants (${headRef.invariants.join(', ')}). Synthesising a plan from the contract IR cannot satisfy authored invariants — the graph must be walked. Either remove "${member.spaceId}" from \`ignoreGraphFor\` or amend the on-disk head ref to declare zero invariants.`,
};
return notOk(conflict);
}
if (ignoreGraph) {
const synthOutcome = await synthStrategy({
aggregateTargetId: aggregate.targetId,
currentMarker,
member,
otherMembers,
schemaIntrospection: currentDBState.schemaIntrospection,
familyInstance: input.familyInstance,
migrations: input.migrations,
frameworkComponents: input.frameworkComponents,
operationPolicy: input.operationPolicy,
});
if (synthOutcome.kind === 'failure') {
return notOk({
kind: 'appSynthFailure',
spaceId: member.spaceId,
conflicts: synthOutcome.conflicts,
});
}
perSpace.set(member.spaceId, synthOutcome.result);
continue;
}
// Try graph-walk first when the graph has nodes; fall back to synth
// when the graph is empty AND no invariants are required.
if (member.graph().nodes.size > 0) {
const walked = graphWalkStrategy({
aggregateTargetId: aggregate.targetId,
member,
currentMarker,
});
if (walked.kind === 'ok') {
perSpace.set(member.spaceId, walked.result);
continue;
}
if (walked.kind === 'unreachable') {
return notOk({
kind: 'extensionPathUnreachable',
spaceId: member.spaceId,
target: headRef.hash,
});
}
// unsatisfiable — surface
return notOk({
kind: 'extensionPathUnsatisfiable',
spaceId: member.spaceId,
missingInvariants: walked.missing,
});
}
// Empty graph: synth is the only option, and it can only satisfy
// empty-invariant members.
if (invariantsRequired) {
return notOk({
kind: 'extensionPathUnsatisfiable',
spaceId: member.spaceId,
missingInvariants: [...headRef.invariants].sort(),
});
}
const synthOutcome = await synthStrategy({
aggregateTargetId: aggregate.targetId,
currentMarker,
member,
otherMembers,
schemaIntrospection: currentDBState.schemaIntrospection,
familyInstance: input.familyInstance,
migrations: input.migrations,
frameworkComponents: input.frameworkComponents,
operationPolicy: input.operationPolicy,
});
if (synthOutcome.kind === 'failure') {
return notOk({
kind: 'appSynthFailure',
spaceId: member.spaceId,
conflicts: synthOutcome.conflicts,
});
}
perSpace.set(member.spaceId, synthOutcome.result);
}
return ok({
perSpace,
applyOrder: [...aggregate.extensions.map((m) => m.spaceId), aggregate.app.spaceId],
});
}