deltafold 0.0.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +278 -4
- package/dist/changelog.d.ts +116 -0
- package/dist/changelog.d.ts.map +1 -0
- package/dist/changelog.js +304 -0
- package/dist/changelog.js.map +1 -0
- package/dist/document-BZcZo9nr.js +171 -0
- package/dist/document-BZcZo9nr.js.map +1 -0
- package/dist/document-DWHsgywm.d.ts +257 -0
- package/dist/document-DWHsgywm.d.ts.map +1 -0
- package/dist/html.d.ts +8 -0
- package/dist/html.d.ts.map +1 -0
- package/dist/html.js +13 -0
- package/dist/html.js.map +1 -0
- package/dist/index.d.ts +517 -1
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2126 -0
- package/dist/index.js.map +1 -0
- package/dist/markdown.d.ts +8 -0
- package/dist/markdown.d.ts.map +1 -0
- package/dist/markdown.js +16 -0
- package/dist/markdown.js.map +1 -0
- package/dist/mdast-DiRsYwPU.js +108 -0
- package/dist/mdast-DiRsYwPU.js.map +1 -0
- package/dist/package-BfBYnaCz.d.ts +1125 -0
- package/dist/package-BfBYnaCz.d.ts.map +1 -0
- package/dist/rolldown-runtime-D8jQpDG7.js +12 -0
- package/package.json +13 -1
- package/src/features/documents/changelog.ts +450 -0
- package/src/features/documents/document.ts +357 -0
- package/src/features/documents/index.ts +4 -0
- package/src/features/documents/public-document.ts +31 -0
- package/src/features/documents/sections.ts +44 -0
- package/src/features/execution/executor.ts +968 -0
- package/src/features/execution/handler.ts +485 -0
- package/src/features/execution/settlement.ts +52 -0
- package/src/features/package.ts +32 -0
- package/src/features/planning/guide.ts +56 -0
- package/src/features/planning/history.ts +174 -0
- package/src/features/planning/identifiers.ts +43 -0
- package/src/features/planning/resolve-error.ts +201 -0
- package/src/features/planning/resolve.ts +724 -0
- package/src/features/planning/rewrite.ts +331 -0
- package/src/features/planning/validate.ts +388 -0
- package/src/foundation/action.ts +188 -0
- package/src/foundation/change.ts +147 -0
- package/src/foundation/instruction.ts +110 -0
- package/src/foundation/migration.ts +236 -0
- package/src/foundation/plan.ts +245 -0
- package/src/foundation/rule.ts +74 -0
- package/src/foundation/shortcut.ts +79 -0
- package/src/foundation/template.ts +28 -0
- package/src/foundation/version.ts +78 -0
- package/src/index.ts +22 -11
- package/src/renderers/html.ts +12 -0
- package/src/renderers/markdown.ts +12 -0
- package/src/renderers/mdast.ts +120 -0
- package/src/types.ts +2 -0
package/dist/index.js
CHANGED
|
@@ -0,0 +1,2126 @@
|
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime-D8jQpDG7.js";
|
|
2
|
+
import { Array as Array$1, Cause, Context, Data, Deferred, Duration, Effect, Equal, Exit, Fiber, Graph, Layer, Option, Order, Predicate, Result, Schema, Scope, SemVer } from "effect";
|
|
3
|
+
import dedentText from "dedent";
|
|
4
|
+
// src/foundation/action.ts
|
|
5
|
+
var action_exports = /* @__PURE__ */ __exportAll({
|
|
6
|
+
define: () => define$1,
|
|
7
|
+
isAction: () => isAction,
|
|
8
|
+
isParallel: () => isParallel,
|
|
9
|
+
isRef: () => isRef,
|
|
10
|
+
isSequence: () => isSequence,
|
|
11
|
+
parallel: () => parallel,
|
|
12
|
+
ref: () => ref,
|
|
13
|
+
sequence: () => sequence
|
|
14
|
+
});
|
|
15
|
+
/**
|
|
16
|
+
* Defines an action kind.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const RenameKey = Action.define<{ target: string; from: string; to: string }>(
|
|
21
|
+
* "config.rename",
|
|
22
|
+
* { describe: ({ target, from, to }) => `In ${target}, rename "${from}" to "${to}"` },
|
|
23
|
+
* );
|
|
24
|
+
* const renameExportA = RenameKey.make("rename-export-a-timeout", {
|
|
25
|
+
* target: "export-a",
|
|
26
|
+
* from: "timeout",
|
|
27
|
+
* to: "requestTimeout",
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @since @next
|
|
32
|
+
*/
|
|
33
|
+
const define$1 = (kind, options = {}) => {
|
|
34
|
+
const describe = options.describe ?? (() => kind);
|
|
35
|
+
return {
|
|
36
|
+
_tag: "ActionKind",
|
|
37
|
+
kind,
|
|
38
|
+
describe,
|
|
39
|
+
make: (id, payload) => ({
|
|
40
|
+
_tag: "Action",
|
|
41
|
+
id,
|
|
42
|
+
kind,
|
|
43
|
+
payload,
|
|
44
|
+
description: describe(payload)
|
|
45
|
+
})
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Mentions an action in prose without declaring another occurrence.
|
|
50
|
+
*
|
|
51
|
+
* @since @next
|
|
52
|
+
*/
|
|
53
|
+
const ref = (action) => ({
|
|
54
|
+
_tag: "ActionRef",
|
|
55
|
+
action
|
|
56
|
+
});
|
|
57
|
+
/**
|
|
58
|
+
* Declares steps that run one after another.
|
|
59
|
+
*
|
|
60
|
+
* @since @next
|
|
61
|
+
*/
|
|
62
|
+
const sequence = (children) => ({
|
|
63
|
+
_tag: "Sequence",
|
|
64
|
+
children
|
|
65
|
+
});
|
|
66
|
+
/**
|
|
67
|
+
* Declares independent branches that may run concurrently. Each branch can be a single
|
|
68
|
+
* step or a sequence.
|
|
69
|
+
*
|
|
70
|
+
* @since @next
|
|
71
|
+
*/
|
|
72
|
+
const parallel = (branches, options = {}) => ({
|
|
73
|
+
_tag: "Parallel",
|
|
74
|
+
branches,
|
|
75
|
+
concurrency: options.concurrency ?? "unbounded"
|
|
76
|
+
});
|
|
77
|
+
/** @since @next */
|
|
78
|
+
const isAction = (value) => Predicate.isTagged(value, "Action") && Predicate.hasProperty(value, "id");
|
|
79
|
+
/** @since @next */
|
|
80
|
+
const isRef = (value) => Predicate.isTagged(value, "ActionRef");
|
|
81
|
+
/** @since @next */
|
|
82
|
+
const isSequence = (value) => Predicate.isTagged(value, "Sequence");
|
|
83
|
+
/** @since @next */
|
|
84
|
+
const isParallel = (value) => Predicate.isTagged(value, "Parallel");
|
|
85
|
+
// src/foundation/template.ts
|
|
86
|
+
const placeholder = "\0";
|
|
87
|
+
/**
|
|
88
|
+
* Removes the common indentation of a tagged template and its surrounding whitespace.
|
|
89
|
+
*
|
|
90
|
+
* Returns one literal string per original literal, so interpolated values stay at their
|
|
91
|
+
* positions and are never stringified or re-indented.
|
|
92
|
+
*/
|
|
93
|
+
const dedent = (strings) => dedentText(strings.join(placeholder)).split(placeholder);
|
|
94
|
+
const isScalar = (value) => typeof value === "string" || typeof value === "number" || typeof value === "boolean" || typeof value === "bigint";
|
|
95
|
+
const describeValue = (value) => {
|
|
96
|
+
if (value === null) return "null";
|
|
97
|
+
if (Array.isArray(value)) return "an array";
|
|
98
|
+
return typeof value === "object" ? "an object" : typeof value;
|
|
99
|
+
};
|
|
100
|
+
// src/foundation/change.ts
|
|
101
|
+
var change_exports = /* @__PURE__ */ __exportAll({
|
|
102
|
+
define: () => define,
|
|
103
|
+
isChange: () => isChange,
|
|
104
|
+
make: () => make$6
|
|
105
|
+
});
|
|
106
|
+
const describe = (id, metadata, relations) => (strings, ...values) => {
|
|
107
|
+
const literals = dedent(strings);
|
|
108
|
+
let description = "";
|
|
109
|
+
for (const [index, literal] of literals.entries()) {
|
|
110
|
+
description += literal;
|
|
111
|
+
if (index >= values.length) continue;
|
|
112
|
+
const value = values[index];
|
|
113
|
+
if (!isScalar(value)) throw new TypeError(`Change "${id}": cannot interpolate ${describeValue(value)}.`);
|
|
114
|
+
description += String(value);
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
_tag: "Change",
|
|
118
|
+
id,
|
|
119
|
+
metadata,
|
|
120
|
+
description,
|
|
121
|
+
reverts: relations.reverts ?? [],
|
|
122
|
+
backportOf: relations.backportOf
|
|
123
|
+
};
|
|
124
|
+
};
|
|
125
|
+
function make$6(id, metadata = {}, relations = {}) {
|
|
126
|
+
return describe(id, metadata, relations);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Defines a reusable change factory whose metadata must have the type `M`.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```ts
|
|
133
|
+
* const ConfigChange = Change.define<{
|
|
134
|
+
* readonly kind: "added" | "changed" | "fixed";
|
|
135
|
+
* readonly breaking: boolean;
|
|
136
|
+
* }>();
|
|
137
|
+
* const timeoutRenamed = ConfigChange.make("2026-11-06.rename-timeout", {
|
|
138
|
+
* kind: "changed",
|
|
139
|
+
* breaking: true,
|
|
140
|
+
* })`
|
|
141
|
+
* Rename the timeout configuration option to requestTimeout.
|
|
142
|
+
* `;
|
|
143
|
+
* ```
|
|
144
|
+
*
|
|
145
|
+
* @since @next
|
|
146
|
+
*/
|
|
147
|
+
const define = () => ({ make: make$6 });
|
|
148
|
+
/** @since @next */
|
|
149
|
+
const isChange = (value) => Predicate.isTagged(value, "Change");
|
|
150
|
+
// src/features/execution/settlement.ts
|
|
151
|
+
const SettlementReference = Context.Reference("deltafold/Settlement", { defaultValue: () => ({
|
|
152
|
+
grace: Duration.zero,
|
|
153
|
+
unsettled: () => {}
|
|
154
|
+
}) });
|
|
155
|
+
/**
|
|
156
|
+
* Runs a Promise-producing effect. If it is interrupted after the Promise started,
|
|
157
|
+
* waits up to the settlement grace period and hands a still-pending Promise to the
|
|
158
|
+
* settlement, which can hold resources until it settles.
|
|
159
|
+
*/
|
|
160
|
+
const trackPromise = (start) => Effect.suspend(() => {
|
|
161
|
+
let pending;
|
|
162
|
+
const capture = (promise) => {
|
|
163
|
+
pending = promise;
|
|
164
|
+
return promise;
|
|
165
|
+
};
|
|
166
|
+
return start(capture).pipe(Effect.onInterrupt(() => Effect.gen(function* () {
|
|
167
|
+
const started = pending;
|
|
168
|
+
if (started === void 0) return;
|
|
169
|
+
const settles = started.then(() => void 0, () => void 0);
|
|
170
|
+
const settlement = yield* SettlementReference;
|
|
171
|
+
const settled = yield* Effect.promise(() => settles).pipe(Effect.timeoutOption(settlement.grace));
|
|
172
|
+
if (Option.isNone(settled)) settlement.unsettled(settles);
|
|
173
|
+
})));
|
|
174
|
+
});
|
|
175
|
+
// src/features/execution/handler.ts
|
|
176
|
+
var handler_exports = /* @__PURE__ */ __exportAll({
|
|
177
|
+
UnexpectedHandlerError: () => UnexpectedHandlerError,
|
|
178
|
+
effect: () => effect$1,
|
|
179
|
+
isHandler: () => isHandler,
|
|
180
|
+
prepared: () => prepared,
|
|
181
|
+
promise: () => promise,
|
|
182
|
+
skip: () => skip
|
|
183
|
+
});
|
|
184
|
+
/**
|
|
185
|
+
* A Promise handler rejected or threw a value that none of its declared error guards
|
|
186
|
+
* recognized, or its prepare phase returned something other than `Handler.prepared` or
|
|
187
|
+
* `Handler.skip`.
|
|
188
|
+
*
|
|
189
|
+
* @since @next
|
|
190
|
+
*/
|
|
191
|
+
var UnexpectedHandlerError = class extends Data.TaggedError("UnexpectedHandlerError") {
|
|
192
|
+
get message() {
|
|
193
|
+
return `Handler for action "${this.actionId}" failed unexpectedly in its ${this.phase} phase`;
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
const PreparationTypeId = "~deltafold/Preparation";
|
|
197
|
+
function prepared(value) {
|
|
198
|
+
return {
|
|
199
|
+
[PreparationTypeId]: PreparationTypeId,
|
|
200
|
+
_tag: "Prepared",
|
|
201
|
+
value
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Ends a prepare phase by skipping the occurrence. The apply phase does not run.
|
|
206
|
+
*
|
|
207
|
+
* @since @next
|
|
208
|
+
*/
|
|
209
|
+
const skip = (reason) => ({
|
|
210
|
+
[PreparationTypeId]: PreparationTypeId,
|
|
211
|
+
_tag: "Skip",
|
|
212
|
+
reason
|
|
213
|
+
});
|
|
214
|
+
const isPreparation = (value) => Predicate.hasProperty(value, PreparationTypeId);
|
|
215
|
+
const notAPreparation = () => /* @__PURE__ */ new TypeError("A prepare phase must return Handler.prepared(value) or Handler.skip(reason)");
|
|
216
|
+
const applyStep = (apply) => ({
|
|
217
|
+
_tag: "Apply",
|
|
218
|
+
apply
|
|
219
|
+
});
|
|
220
|
+
const TypeId = "~deltafold/Handler";
|
|
221
|
+
/** @since @next */
|
|
222
|
+
const isHandler = (value) => Predicate.hasProperty(value, TypeId);
|
|
223
|
+
const make$5 = (action, prepares, prepare) => ({
|
|
224
|
+
[TypeId]: TypeId,
|
|
225
|
+
action,
|
|
226
|
+
prepares,
|
|
227
|
+
prepare
|
|
228
|
+
});
|
|
229
|
+
const declaredError = (guards) => (cause) => guards.some((guard) => Schema.isSchema(guard) ? Schema.is(guard)(cause) : guard(cause));
|
|
230
|
+
function promise(action, run, options) {
|
|
231
|
+
const isDeclared = declaredError(options?.errors ?? []);
|
|
232
|
+
const services = options?.services ?? {};
|
|
233
|
+
const resolve = Effect.map(Effect.context(), (context) => Object.fromEntries(Reflect.ownKeys(services).map((name) => [name, Context.get(context, services[name])])));
|
|
234
|
+
const unexpected = (phase, cause) => new UnexpectedHandlerError({
|
|
235
|
+
actionId: action.id,
|
|
236
|
+
kind: action.kind,
|
|
237
|
+
phase,
|
|
238
|
+
cause
|
|
239
|
+
});
|
|
240
|
+
const adapt = (phase, callback) => Effect.flatMap(resolve, (resolved) => trackPromise((capture) => Effect.tryPromise({
|
|
241
|
+
try: (signal) => capture(Promise.resolve(callback({
|
|
242
|
+
signal,
|
|
243
|
+
services: resolved
|
|
244
|
+
}))),
|
|
245
|
+
catch: (cause) => isDeclared(cause) ? cause : unexpected(phase, cause)
|
|
246
|
+
})));
|
|
247
|
+
if (Predicate.isFunction(run)) {
|
|
248
|
+
const apply = Effect.asVoid(adapt("apply", (context) => run(action.payload, context)));
|
|
249
|
+
return make$5(action, false, () => Effect.succeed(applyStep(apply)));
|
|
250
|
+
}
|
|
251
|
+
const prepare = adapt("prepare", (context) => run.prepare(action.payload, context)).pipe(Effect.filterOrFail(isPreparation, () => unexpected("prepare", notAPreparation())), Effect.map((preparation) => preparation._tag === "Skip" ? preparation : applyStep(Effect.asVoid(adapt("apply", (context) => run.apply(preparation.value, {
|
|
252
|
+
...context,
|
|
253
|
+
payload: action.payload
|
|
254
|
+
}))))));
|
|
255
|
+
return make$5(action, true, () => prepare);
|
|
256
|
+
}
|
|
257
|
+
function effect$1(action, run) {
|
|
258
|
+
if (Predicate.isFunction(run)) {
|
|
259
|
+
const apply = Effect.asVoid(Effect.suspend(() => run(action.payload)));
|
|
260
|
+
return make$5(action, false, (scope) => Effect.succeed(applyStep(Scope.provide(apply, scope))));
|
|
261
|
+
}
|
|
262
|
+
return make$5(action, true, (scope) => Effect.suspend(() => run.prepare(action.payload)).pipe(Scope.provide(scope), Effect.filterOrElse(isPreparation, () => Effect.die(notAPreparation())), Effect.map((preparation) => preparation._tag === "Skip" ? preparation : applyStep(Scope.provide(Effect.asVoid(Effect.suspend(() => run.apply(preparation.value, { payload: action.payload }))), scope)))));
|
|
263
|
+
}
|
|
264
|
+
// src/features/execution/executor.ts
|
|
265
|
+
var executor_exports = /* @__PURE__ */ __exportAll({
|
|
266
|
+
ExecutionFailed: () => ExecutionFailed,
|
|
267
|
+
ExecutionInterrupted: () => ExecutionInterrupted,
|
|
268
|
+
HandlerCoverageError: () => HandlerCoverageError,
|
|
269
|
+
effect: () => effect,
|
|
270
|
+
runPromise: () => runPromise
|
|
271
|
+
});
|
|
272
|
+
const HandlerCoverageBase = Data.TaggedError("HandlerCoverageError");
|
|
273
|
+
/**
|
|
274
|
+
* Handler coverage failed before any handler phase ran. Every occurrence needs exactly
|
|
275
|
+
* one handler, for the same action definition: the same identifier, kind, and payload.
|
|
276
|
+
* For an occurrence that a rule produced, the issue names the rule and the occurrences
|
|
277
|
+
* it replaced.
|
|
278
|
+
*
|
|
279
|
+
* @since @next
|
|
280
|
+
*/
|
|
281
|
+
var HandlerCoverageError = class extends HandlerCoverageBase {
|
|
282
|
+
get message() {
|
|
283
|
+
return `Handler coverage failed for ${this.issues.length} occurrence(s): ${this.issues.map((issue) => `${issue._tag.toLowerCase()} handler for "${issue.actionId}" (${issue.description})`).join("; ")}.`;
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
const ExecutionFailedBase = Data.TaggedError("ExecutionFailed");
|
|
287
|
+
/**
|
|
288
|
+
* An occurrence failed. Execution stopped admitting new actions; started ones settled.
|
|
289
|
+
*
|
|
290
|
+
* @since @next
|
|
291
|
+
*/
|
|
292
|
+
var ExecutionFailed = class extends ExecutionFailedBase {
|
|
293
|
+
get message() {
|
|
294
|
+
return `Execution failed at ${this.failures.map((failure) => `"${failure.occurrence.id}"`).join(", ")}. Completed actions were not rolled back.`;
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
const ExecutionInterruptedBase = Data.TaggedError("ExecutionInterrupted");
|
|
298
|
+
const interruptedBy = {
|
|
299
|
+
cancelled: "cancelled",
|
|
300
|
+
timeout: "stopped by a timeout",
|
|
301
|
+
handler: "interrupted by a handler"
|
|
302
|
+
};
|
|
303
|
+
/**
|
|
304
|
+
* Explicit cancellation, a configured timeout, or a handler's own interruption stopped
|
|
305
|
+
* execution.
|
|
306
|
+
*
|
|
307
|
+
* @since @next
|
|
308
|
+
*/
|
|
309
|
+
var ExecutionInterrupted = class extends ExecutionInterruptedBase {
|
|
310
|
+
get message() {
|
|
311
|
+
const pending = this.report.occurrences.filter((entry) => entry.outcome._tag === "Interrupted" && !entry.outcome.settled);
|
|
312
|
+
const suffix = pending.length === 0 ? "" : ` ${pending.length} interrupted action(s) had not settled and may still be running.`;
|
|
313
|
+
return `Execution was ${interruptedBy[this.reason]}.${suffix}`;
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
const describeReplaces = (occurrence) => occurrence.origin._tag === "Replacement" ? occurrence.origin.replaces.map((replaced) => ({
|
|
317
|
+
occurrenceId: replaced.id,
|
|
318
|
+
description: replaced.action.description
|
|
319
|
+
})) : [];
|
|
320
|
+
const coverage = (plan, handlers) => {
|
|
321
|
+
const byAction = /* @__PURE__ */ new Map();
|
|
322
|
+
for (const handler of handlers) {
|
|
323
|
+
const list = byAction.get(handler.action.id) ?? [];
|
|
324
|
+
list.push(handler);
|
|
325
|
+
byAction.set(handler.action.id, list);
|
|
326
|
+
}
|
|
327
|
+
const resolved = /* @__PURE__ */ new Map();
|
|
328
|
+
const issues = [];
|
|
329
|
+
for (const occurrence of plan.occurrences) {
|
|
330
|
+
const issue = (tag) => issues.push({
|
|
331
|
+
_tag: tag,
|
|
332
|
+
occurrenceId: occurrence.id,
|
|
333
|
+
actionId: occurrence.action.id,
|
|
334
|
+
description: occurrence.action.description,
|
|
335
|
+
migrationId: occurrence.migration.id,
|
|
336
|
+
rule: occurrence.origin._tag === "Replacement" ? occurrence.origin.rule.id : void 0,
|
|
337
|
+
replaces: describeReplaces(occurrence)
|
|
338
|
+
});
|
|
339
|
+
const candidates = byAction.get(occurrence.action.id) ?? [];
|
|
340
|
+
const [selected] = candidates;
|
|
341
|
+
if (candidates.length > 1) issue("Duplicate");
|
|
342
|
+
else if (selected === void 0) issue("Missing");
|
|
343
|
+
else if (selected.action.kind !== occurrence.action.kind || !Equal.equals(selected.action.payload, occurrence.action.payload)) issue("Mismatched");
|
|
344
|
+
else resolved.set(occurrence.id, selected);
|
|
345
|
+
}
|
|
346
|
+
return {
|
|
347
|
+
resolved,
|
|
348
|
+
issues
|
|
349
|
+
};
|
|
350
|
+
};
|
|
351
|
+
const report = (plan, mode, status, outcomes, bodiesStarted) => ({
|
|
352
|
+
_tag: "Report",
|
|
353
|
+
mode,
|
|
354
|
+
status,
|
|
355
|
+
from: plan.from.id,
|
|
356
|
+
to: plan.to.id,
|
|
357
|
+
route: plan.route.id,
|
|
358
|
+
occurrences: plan.occurrences.map((occurrence) => ({
|
|
359
|
+
occurrence,
|
|
360
|
+
outcome: outcomes.get(occurrence.id) ?? { _tag: "Unattempted" }
|
|
361
|
+
})),
|
|
362
|
+
bodiesStarted,
|
|
363
|
+
persistence: "none"
|
|
364
|
+
});
|
|
365
|
+
const failureOf = (cause) => ({
|
|
366
|
+
error: Option.getOrUndefined(Cause.findErrorOption(cause)),
|
|
367
|
+
cause
|
|
368
|
+
});
|
|
369
|
+
/** The exit an occurrence's scope closes with, so finalizers see how it ended. */
|
|
370
|
+
const exitOf = (outcome) => {
|
|
371
|
+
switch (outcome?._tag) {
|
|
372
|
+
case "Failed":
|
|
373
|
+
case "PreparationFailed": return Exit.failCause(outcome.cause);
|
|
374
|
+
case "Interrupted": return Exit.interrupt();
|
|
375
|
+
default: return Exit.void;
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
const forEachBranch = (branches, concurrency, run) => Effect.forEach(branches, run, {
|
|
379
|
+
concurrency,
|
|
380
|
+
discard: true
|
|
381
|
+
});
|
|
382
|
+
/**
|
|
383
|
+
* Logs a cleanup failure that the report cannot carry, with the occurrence or the
|
|
384
|
+
* services layer that owned the resources.
|
|
385
|
+
*/
|
|
386
|
+
const logCleanupFailure = (owner) => (cause) => Effect.logError("deltafold: cleanup failed", cause).pipe(Effect.annotateLogs(owner));
|
|
387
|
+
/** Admission state, cancellation, and timeouts shared by real and dry runs. */
|
|
388
|
+
const makeControl = (options, subscribe) => Effect.gen(function* () {
|
|
389
|
+
const halt = { reason: void 0 };
|
|
390
|
+
const cancelled = yield* Deferred.make();
|
|
391
|
+
const settleTimeout = options.settleTimeout ?? Duration.seconds(5);
|
|
392
|
+
const stop = (reason) => {
|
|
393
|
+
halt.reason ??= reason;
|
|
394
|
+
};
|
|
395
|
+
subscribe?.(() => {
|
|
396
|
+
stop("cancelled");
|
|
397
|
+
Deferred.doneUnsafe(cancelled, Exit.succeed("cancelled"));
|
|
398
|
+
});
|
|
399
|
+
/**
|
|
400
|
+
* Runs one phase, racing it against cancellation and its own timeout. An
|
|
401
|
+
* interrupted Promise that outlives the settlement grace period goes to `unsettled`.
|
|
402
|
+
*/
|
|
403
|
+
const runPhase = (work, unsettled) => Effect.gen(function* () {
|
|
404
|
+
let settled = true;
|
|
405
|
+
const tracked = work.pipe(Effect.provideService(SettlementReference, {
|
|
406
|
+
grace: settleTimeout,
|
|
407
|
+
unsettled: (promise) => {
|
|
408
|
+
settled = false;
|
|
409
|
+
unsettled.push(promise);
|
|
410
|
+
}
|
|
411
|
+
}));
|
|
412
|
+
const stopSignals = [Deferred.await(cancelled)];
|
|
413
|
+
if (options.timeout !== void 0) stopSignals.push(Effect.as(Effect.sleep(options.timeout), "timeout"));
|
|
414
|
+
const result = yield* Effect.raceFirst(Effect.map(Effect.exit(tracked), (exit) => ({
|
|
415
|
+
_tag: "Done",
|
|
416
|
+
exit
|
|
417
|
+
})), Effect.raceAll(stopSignals).pipe(Effect.tap((reason) => Effect.sync(() => stop(reason))), Effect.map((reason) => ({
|
|
418
|
+
_tag: "Stop",
|
|
419
|
+
reason
|
|
420
|
+
}))));
|
|
421
|
+
if (result._tag === "Done") return result;
|
|
422
|
+
return {
|
|
423
|
+
_tag: "Interrupted",
|
|
424
|
+
reason: result.reason,
|
|
425
|
+
settled
|
|
426
|
+
};
|
|
427
|
+
});
|
|
428
|
+
return {
|
|
429
|
+
halt,
|
|
430
|
+
stop,
|
|
431
|
+
runPhase
|
|
432
|
+
};
|
|
433
|
+
});
|
|
434
|
+
/**
|
|
435
|
+
* Runs one occurrence in its own scope, which both phases share. `use` returns the
|
|
436
|
+
* exit to close the scope with. The scope closes as soon as the occurrence ends, and
|
|
437
|
+
* a failing finalizer goes to `onCleanupFailure`. If an interrupted Promise has not
|
|
438
|
+
* settled, the scope stays open until it does, possibly forever; a failing
|
|
439
|
+
* finalizer is then logged, because the report may already have been returned.
|
|
440
|
+
*/
|
|
441
|
+
const inScope = (retain, occurrenceId, use, onCleanupFailure) => Effect.uninterruptibleMask((restore) => Effect.gen(function* () {
|
|
442
|
+
const scope = yield* Scope.make();
|
|
443
|
+
const unsettled = [];
|
|
444
|
+
const used = yield* Effect.exit(restore(use(scope, unsettled)));
|
|
445
|
+
const end = Exit.isSuccess(used) ? used.value : used;
|
|
446
|
+
if (unsettled.length > 0) retain(yield* Effect.forkDetach(Effect.promise(() => Promise.all(unsettled)).pipe(Effect.andThen(Scope.close(scope, end)), Effect.catchCause(logCleanupFailure({ occurrence: occurrenceId })))));
|
|
447
|
+
else {
|
|
448
|
+
const closed = yield* Effect.exit(Scope.close(scope, end));
|
|
449
|
+
if (Exit.isFailure(closed)) yield* onCleanupFailure(closed.cause);
|
|
450
|
+
}
|
|
451
|
+
return yield* used;
|
|
452
|
+
})).pipe(Effect.asVoid);
|
|
453
|
+
const interrupted = (plan, mode, reason, outcomes, bodiesStarted) => new ExecutionInterrupted({
|
|
454
|
+
reason,
|
|
455
|
+
report: report(plan, mode, "interrupted", outcomes, bodiesStarted)
|
|
456
|
+
});
|
|
457
|
+
/**
|
|
458
|
+
* The outcome of a phase that did not succeed. `undefined` when it succeeded. An Effect
|
|
459
|
+
* handler that interrupted itself stops admission.
|
|
460
|
+
*/
|
|
461
|
+
const unsuccessful = (phase, result, stop) => {
|
|
462
|
+
if (result._tag === "Interrupted") return {
|
|
463
|
+
_tag: "Interrupted",
|
|
464
|
+
phase,
|
|
465
|
+
reason: result.reason,
|
|
466
|
+
settled: result.settled
|
|
467
|
+
};
|
|
468
|
+
if (Exit.isFailure(result.exit) && Cause.hasInterruptsOnly(result.exit.cause)) {
|
|
469
|
+
stop("handler");
|
|
470
|
+
return {
|
|
471
|
+
_tag: "Interrupted",
|
|
472
|
+
phase,
|
|
473
|
+
reason: "handler",
|
|
474
|
+
settled: true
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
const execute = (plan, resolved, options, subscribe, retain) => Effect.gen(function* () {
|
|
479
|
+
const outcomes = /* @__PURE__ */ new Map();
|
|
480
|
+
const { halt, stop, runPhase } = yield* makeControl(options, subscribe);
|
|
481
|
+
let bodiesStarted = false;
|
|
482
|
+
const runOccurrence = (occurrence) => {
|
|
483
|
+
if (halt.reason !== void 0) return Effect.void;
|
|
484
|
+
const handler = resolved.get(occurrence.id);
|
|
485
|
+
if (handler === void 0) return Effect.void;
|
|
486
|
+
let phase = "prepare";
|
|
487
|
+
const end = (outcome) => {
|
|
488
|
+
outcomes.set(occurrence.id, outcome);
|
|
489
|
+
return exitOf(outcome);
|
|
490
|
+
};
|
|
491
|
+
const fail = (cause) => {
|
|
492
|
+
stop("failure");
|
|
493
|
+
return end({
|
|
494
|
+
_tag: "Failed",
|
|
495
|
+
phase,
|
|
496
|
+
...failureOf(cause)
|
|
497
|
+
});
|
|
498
|
+
};
|
|
499
|
+
const use = (scope, unsettled) => Effect.gen(function* () {
|
|
500
|
+
let step;
|
|
501
|
+
if (handler.prepares) {
|
|
502
|
+
const preparation = yield* runPhase(handler.prepare(scope), unsettled);
|
|
503
|
+
const stopped = unsuccessful("prepare", preparation, stop);
|
|
504
|
+
if (stopped !== void 0) return end(stopped);
|
|
505
|
+
if (preparation._tag !== "Done") return Exit.void;
|
|
506
|
+
if (Exit.isFailure(preparation.exit)) return fail(preparation.exit.cause);
|
|
507
|
+
step = preparation.exit.value;
|
|
508
|
+
if (step._tag === "Apply" && halt.reason === "cancelled") return Exit.void;
|
|
509
|
+
} else {
|
|
510
|
+
const started = yield* Effect.exit(handler.prepare(scope));
|
|
511
|
+
if (Exit.isFailure(started)) return fail(started.cause);
|
|
512
|
+
step = started.value;
|
|
513
|
+
}
|
|
514
|
+
if (step._tag === "Skip") return end({
|
|
515
|
+
_tag: "Skipped",
|
|
516
|
+
reason: step.reason
|
|
517
|
+
});
|
|
518
|
+
phase = "apply";
|
|
519
|
+
bodiesStarted = true;
|
|
520
|
+
const applied = yield* runPhase(step.apply, unsettled);
|
|
521
|
+
const stopped = unsuccessful("apply", applied, stop);
|
|
522
|
+
if (stopped !== void 0) return end(stopped);
|
|
523
|
+
if (applied._tag !== "Done") return Exit.void;
|
|
524
|
+
if (Exit.isFailure(applied.exit)) return fail(applied.exit.cause);
|
|
525
|
+
return end({ _tag: "Completed" });
|
|
526
|
+
});
|
|
527
|
+
const onCleanupFailure = (cause) => Effect.suspend(() => {
|
|
528
|
+
const outcome = outcomes.get(occurrence.id);
|
|
529
|
+
if (outcome?._tag === "Interrupted") return logCleanupFailure({ occurrence: occurrence.id })(cause);
|
|
530
|
+
stop("failure");
|
|
531
|
+
outcomes.set(occurrence.id, {
|
|
532
|
+
_tag: "Failed",
|
|
533
|
+
phase,
|
|
534
|
+
...failureOf(outcome?._tag === "Failed" ? Cause.combine(outcome.cause, cause) : cause)
|
|
535
|
+
});
|
|
536
|
+
return Effect.void;
|
|
537
|
+
});
|
|
538
|
+
return inScope(retain, occurrence.id, use, onCleanupFailure);
|
|
539
|
+
};
|
|
540
|
+
const runNode = (node) => {
|
|
541
|
+
switch (node._tag) {
|
|
542
|
+
case "Occurrence": return runOccurrence(node);
|
|
543
|
+
case "Sequence": return Effect.forEach(node.children, runNode, { discard: true });
|
|
544
|
+
}
|
|
545
|
+
return forEachBranch(node.branches, node.concurrency, runNode);
|
|
546
|
+
};
|
|
547
|
+
for (const step of plan.steps) {
|
|
548
|
+
if (halt.reason !== void 0) break;
|
|
549
|
+
yield* runNode(step.node);
|
|
550
|
+
}
|
|
551
|
+
if (halt.reason === void 0) return report(plan, "execute", "completed", outcomes, bodiesStarted);
|
|
552
|
+
if (halt.reason === "failure") {
|
|
553
|
+
const result = report(plan, "execute", "failed", outcomes, bodiesStarted);
|
|
554
|
+
return yield* new ExecutionFailed({
|
|
555
|
+
failures: result.occurrences.filter((entry) => entry.outcome._tag === "Failed"),
|
|
556
|
+
report: result
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
return yield* interrupted(plan, "execute", halt.reason, outcomes, bodiesStarted);
|
|
560
|
+
});
|
|
561
|
+
const dryRun = (plan, resolved, options, subscribe, retain) => Effect.gen(function* () {
|
|
562
|
+
const outcomes = /* @__PURE__ */ new Map();
|
|
563
|
+
const { halt, stop, runPhase } = yield* makeControl(options, subscribe);
|
|
564
|
+
let failedAt;
|
|
565
|
+
/** Runs a prepare phase in the occurrence's scope and records what it predicts. */
|
|
566
|
+
const prepare = (id, handler) => {
|
|
567
|
+
const end = (outcome) => {
|
|
568
|
+
outcomes.set(id, outcome);
|
|
569
|
+
return exitOf(outcome);
|
|
570
|
+
};
|
|
571
|
+
const use = (scope, unsettled) => Effect.gen(function* () {
|
|
572
|
+
const preparation = yield* runPhase(handler.prepare(scope), unsettled);
|
|
573
|
+
const stopped = unsuccessful("prepare", preparation, stop);
|
|
574
|
+
if (stopped !== void 0) return end(stopped);
|
|
575
|
+
if (preparation._tag !== "Done") return Exit.void;
|
|
576
|
+
if (Exit.isFailure(preparation.exit)) {
|
|
577
|
+
failedAt = id;
|
|
578
|
+
return end({
|
|
579
|
+
_tag: "PreparationFailed",
|
|
580
|
+
...failureOf(preparation.exit.cause)
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
const step = preparation.exit.value;
|
|
584
|
+
return end(step._tag === "Skip" ? {
|
|
585
|
+
_tag: "WouldSkip",
|
|
586
|
+
reason: step.reason
|
|
587
|
+
} : {
|
|
588
|
+
_tag: "WouldExecute",
|
|
589
|
+
evaluated: true
|
|
590
|
+
});
|
|
591
|
+
});
|
|
592
|
+
const onCleanupFailure = (cause) => Effect.suspend(() => {
|
|
593
|
+
const outcome = outcomes.get(id);
|
|
594
|
+
if (outcome?._tag === "Interrupted") return logCleanupFailure({ occurrence: id })(cause);
|
|
595
|
+
failedAt ??= id;
|
|
596
|
+
outcomes.set(id, {
|
|
597
|
+
_tag: "PreparationFailed",
|
|
598
|
+
...failureOf(outcome?._tag === "PreparationFailed" ? Cause.combine(outcome.cause, cause) : cause)
|
|
599
|
+
});
|
|
600
|
+
return Effect.void;
|
|
601
|
+
});
|
|
602
|
+
return inScope(retain, id, use, onCleanupFailure);
|
|
603
|
+
};
|
|
604
|
+
/** Returns whether the node would or might change state. */
|
|
605
|
+
const visit = (node, dirty) => Effect.gen(function* () {
|
|
606
|
+
if (node._tag === "Sequence") {
|
|
607
|
+
let current = dirty;
|
|
608
|
+
for (const child of node.children) current = yield* visit(child, current);
|
|
609
|
+
return current;
|
|
610
|
+
}
|
|
611
|
+
if (node._tag === "Parallel") {
|
|
612
|
+
let changed = dirty;
|
|
613
|
+
for (const branch of node.branches) changed = (yield* visit(branch, dirty)) || changed;
|
|
614
|
+
return changed;
|
|
615
|
+
}
|
|
616
|
+
const handler = resolved.get(node.id);
|
|
617
|
+
if (handler === void 0 || halt.reason !== void 0) return dirty;
|
|
618
|
+
if (failedAt !== void 0) {
|
|
619
|
+
outcomes.set(node.id, {
|
|
620
|
+
_tag: "Undetermined",
|
|
621
|
+
reason: `Execution would stop at the failed preparation of "${failedAt}".`
|
|
622
|
+
});
|
|
623
|
+
return true;
|
|
624
|
+
}
|
|
625
|
+
if (!handler.prepares) {
|
|
626
|
+
outcomes.set(node.id, {
|
|
627
|
+
_tag: "WouldExecute",
|
|
628
|
+
evaluated: false
|
|
629
|
+
});
|
|
630
|
+
return true;
|
|
631
|
+
}
|
|
632
|
+
if (dirty) {
|
|
633
|
+
outcomes.set(node.id, {
|
|
634
|
+
_tag: "Undetermined",
|
|
635
|
+
reason: "The prepare phase depends on earlier actions that a dry run does not apply."
|
|
636
|
+
});
|
|
637
|
+
return true;
|
|
638
|
+
}
|
|
639
|
+
yield* prepare(node.id, handler);
|
|
640
|
+
return outcomes.get(node.id)?._tag !== "WouldSkip";
|
|
641
|
+
});
|
|
642
|
+
let dirty = false;
|
|
643
|
+
for (const step of plan.steps) dirty = yield* visit(step.node, dirty);
|
|
644
|
+
if (halt.reason !== void 0 && halt.reason !== "failure") return yield* interrupted(plan, "dry-run", halt.reason, outcomes, false);
|
|
645
|
+
return report(plan, "dry-run", failedAt === void 0 ? "would-complete" : "would-fail", outcomes, false);
|
|
646
|
+
});
|
|
647
|
+
const run = (plan, options, subscribe, retain) => {
|
|
648
|
+
const handlers = options.handlers ?? [];
|
|
649
|
+
return Effect.gen(function* () {
|
|
650
|
+
const checked = coverage(plan, handlers);
|
|
651
|
+
if (checked.issues.length > 0) return yield* new HandlerCoverageError({
|
|
652
|
+
issues: checked.issues,
|
|
653
|
+
report: report(plan, options.dryRun === true ? "dry-run" : "execute", "not-started", /* @__PURE__ */ new Map(), false)
|
|
654
|
+
});
|
|
655
|
+
return options.dryRun === true ? yield* dryRun(plan, checked.resolved, options, subscribe, retain) : yield* execute(plan, checked.resolved, options, subscribe, retain);
|
|
656
|
+
});
|
|
657
|
+
};
|
|
658
|
+
/**
|
|
659
|
+
* Runs a plan as an Effect. Coverage is checked before any handler phase runs. Steps
|
|
660
|
+
* are sequential barriers; parallel groups run their branches concurrently. On failure,
|
|
661
|
+
* no new action starts, and actions already running settle before the effect fails
|
|
662
|
+
* with the partial report.
|
|
663
|
+
*
|
|
664
|
+
* Each occurrence has its own scope, shared by its phases and closed when it ends. An
|
|
665
|
+
* interrupted Promise that has not settled keeps its occurrence's scope open until it
|
|
666
|
+
* settles, after the effect has ended. Services that the caller provides stay the
|
|
667
|
+
* caller's to release.
|
|
668
|
+
*
|
|
669
|
+
* To cancel, interrupt the fiber. Interruption reaches running actions and aborts the
|
|
670
|
+
* signal passed to Promise callbacks. The effect then ends as interrupted, without a
|
|
671
|
+
* report.
|
|
672
|
+
*
|
|
673
|
+
* @since @next
|
|
674
|
+
*/
|
|
675
|
+
const effect = (plan, options) => run(plan, options, void 0, () => {});
|
|
676
|
+
/**
|
|
677
|
+
* Runs a plan and returns a Promise. It rejects with `HandlerCoverageError`,
|
|
678
|
+
* `ExecutionFailed`, or `ExecutionInterrupted`, each carrying the partial report.
|
|
679
|
+
* Building a supplied services layer can also reject with the layer's own error,
|
|
680
|
+
* without a report.
|
|
681
|
+
*
|
|
682
|
+
* A services layer is built for this run. A services context stays owned by the
|
|
683
|
+
* caller. A Promise callback that ignores its signal can keep running after
|
|
684
|
+
* cancellation or a timeout, beyond `settleTimeout`; the report marks it as possibly
|
|
685
|
+
* still running. Its occurrence's scope and the run's services layer then stay open
|
|
686
|
+
* until it settles, possibly forever, and close in that order. A cleanup failure after
|
|
687
|
+
* the Promise resolved is logged through Effect.
|
|
688
|
+
*
|
|
689
|
+
* @since @next
|
|
690
|
+
*/
|
|
691
|
+
const runPromise = (plan, options) => {
|
|
692
|
+
const { signal } = options;
|
|
693
|
+
const notStarted = () => new ExecutionInterrupted({
|
|
694
|
+
reason: "cancelled",
|
|
695
|
+
report: report(plan, options.dryRun === true ? "dry-run" : "execute", "interrupted", /* @__PURE__ */ new Map(), false)
|
|
696
|
+
});
|
|
697
|
+
if (signal?.aborted === true) return Promise.reject(notStarted());
|
|
698
|
+
const abortedBeforeRun = Deferred.makeUnsafe();
|
|
699
|
+
let cancelRun;
|
|
700
|
+
const onAbort = () => {
|
|
701
|
+
if (cancelRun === void 0) Deferred.doneUnsafe(abortedBeforeRun, Effect.void);
|
|
702
|
+
else cancelRun();
|
|
703
|
+
};
|
|
704
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
705
|
+
const subscribe = signal === void 0 ? void 0 : (cancel) => {
|
|
706
|
+
cancelRun = cancel;
|
|
707
|
+
if (signal.aborted) cancel();
|
|
708
|
+
};
|
|
709
|
+
const retained = [];
|
|
710
|
+
const program = run(plan, options, subscribe, (fiber) => retained.push(fiber));
|
|
711
|
+
const services = options.services;
|
|
712
|
+
const provided = services === void 0 ? program : Context.isContext(services) ? Effect.provideContext(program, services) : Effect.uninterruptibleMask((restore) => Effect.gen(function* () {
|
|
713
|
+
const scope = yield* Scope.make();
|
|
714
|
+
const built = yield* Effect.exit(restore(Layer.buildWithScope(services, scope)));
|
|
715
|
+
if (Exit.isFailure(built)) {
|
|
716
|
+
yield* Scope.close(scope, built);
|
|
717
|
+
return yield* built;
|
|
718
|
+
}
|
|
719
|
+
const exit = yield* Effect.exit(restore(Effect.provideContext(program, built.value)));
|
|
720
|
+
if (retained.length === 0) yield* Scope.close(scope, exit);
|
|
721
|
+
else yield* Effect.forkDetach(Fiber.awaitAll(retained).pipe(Effect.andThen(Scope.close(scope, exit)), Effect.catchCause(logCleanupFailure({ owner: "services layer" }))));
|
|
722
|
+
return yield* exit;
|
|
723
|
+
}));
|
|
724
|
+
const cancellable = signal === void 0 ? provided : Effect.raceFirst(provided, Effect.andThen(Deferred.await(abortedBeforeRun), Effect.fail(notStarted())));
|
|
725
|
+
return Effect.runPromiseExit(cancellable).finally(() => signal?.removeEventListener("abort", onAbort)).then((exit) => {
|
|
726
|
+
if (Exit.isSuccess(exit)) return exit.value;
|
|
727
|
+
throw Cause.squash(exit.cause);
|
|
728
|
+
});
|
|
729
|
+
};
|
|
730
|
+
// src/features/planning/resolve-error.ts
|
|
731
|
+
/**
|
|
732
|
+
* The history's declarations are invalid. Resolution needs a valid history.
|
|
733
|
+
*
|
|
734
|
+
* @since @next
|
|
735
|
+
*/
|
|
736
|
+
var InvalidHistory = class extends Data.TaggedError("InvalidHistory") {
|
|
737
|
+
get message() {
|
|
738
|
+
return `The history has ${this.issues.length} declaration issue(s): ${this.issues.map((issue) => issue.message).join(" ")}`;
|
|
739
|
+
}
|
|
740
|
+
};
|
|
741
|
+
/** @since @next */
|
|
742
|
+
var UnknownVersion = class extends Data.TaggedError("UnknownVersion") {
|
|
743
|
+
get message() {
|
|
744
|
+
return `Version "${this.version}" is not registered in the history.`;
|
|
745
|
+
}
|
|
746
|
+
};
|
|
747
|
+
/**
|
|
748
|
+
* No declared route connects the versions. This never means that no work is needed:
|
|
749
|
+
* declare `Migration.noop` for a supported zero-work transition.
|
|
750
|
+
*
|
|
751
|
+
* @since @next
|
|
752
|
+
*/
|
|
753
|
+
var UnsupportedRoute = class extends Data.TaggedError("UnsupportedRoute") {
|
|
754
|
+
get message() {
|
|
755
|
+
const closest = this.closest.length === 0 ? "" : ` Closest reachable: ${this.closest.join(", ")}.`;
|
|
756
|
+
return `No declared ${this.direction} route from "${this.from}" to "${this.to}".${closest}`;
|
|
757
|
+
}
|
|
758
|
+
};
|
|
759
|
+
/**
|
|
760
|
+
* Several routes connect the versions and no selection or policy chose one.
|
|
761
|
+
*
|
|
762
|
+
* @since @next
|
|
763
|
+
*/
|
|
764
|
+
var AmbiguousRoute = class extends Data.TaggedError("AmbiguousRoute") {
|
|
765
|
+
get message() {
|
|
766
|
+
const ids = this.candidates.map((candidate) => `"${candidate.id}"`).join(", ");
|
|
767
|
+
const reason = this.policy === void 0 ? "" : ` after applying the "${this.policy}" policy`;
|
|
768
|
+
return `${this.candidates.length} routes connect "${this.from}" and "${this.to}"${reason}: ${ids}. Select one with the \`route\` option, or choose a route policy.`;
|
|
769
|
+
}
|
|
770
|
+
};
|
|
771
|
+
/**
|
|
772
|
+
* The `route` option names no candidate route.
|
|
773
|
+
*
|
|
774
|
+
* @since @next
|
|
775
|
+
*/
|
|
776
|
+
var UnknownRoute = class extends Data.TaggedError("UnknownRoute") {
|
|
777
|
+
get message() {
|
|
778
|
+
return `No candidate route has the identifier "${this.route}". Candidates: ${this.candidates.map((candidate) => `"${candidate.id}"`).join(", ")}.`;
|
|
779
|
+
}
|
|
780
|
+
};
|
|
781
|
+
/**
|
|
782
|
+
* Rules rewrite a sequence back into itself.
|
|
783
|
+
*
|
|
784
|
+
* @since @next
|
|
785
|
+
*/
|
|
786
|
+
var RewriteCycle = class extends Data.TaggedError("RewriteCycle") {
|
|
787
|
+
get message() {
|
|
788
|
+
return `Rules ${this.rules.map((rule) => `"${rule}"`).join(", ")} rewrite a sequence back into itself on route "${this.route}".`;
|
|
789
|
+
}
|
|
790
|
+
};
|
|
791
|
+
/**
|
|
792
|
+
* Rules can produce different results depending on which applies first.
|
|
793
|
+
*
|
|
794
|
+
* @since @next
|
|
795
|
+
*/
|
|
796
|
+
var ConflictingRewrites = class extends Data.TaggedError("ConflictingRewrites") {
|
|
797
|
+
get message() {
|
|
798
|
+
return `Rules produce ${this.outcomes.length} different results on route "${this.route}", depending on the order they apply in.`;
|
|
799
|
+
}
|
|
800
|
+
};
|
|
801
|
+
/**
|
|
802
|
+
* Optimization explored too many states or chained too many rule applications, for
|
|
803
|
+
* example because a rule keeps growing the sequence.
|
|
804
|
+
*
|
|
805
|
+
* @since @next
|
|
806
|
+
*/
|
|
807
|
+
var RewriteLimitExceeded = class extends Data.TaggedError("RewriteLimitExceeded") {
|
|
808
|
+
get message() {
|
|
809
|
+
return this.limit === "states" ? `Optimizing route "${this.route}" explored more than ${this.value} states.` : `Optimizing route "${this.route}" chained more than ${this.value} rule applications.`;
|
|
810
|
+
}
|
|
811
|
+
};
|
|
812
|
+
/**
|
|
813
|
+
* More candidate routes exist than the resolver enumerates.
|
|
814
|
+
*
|
|
815
|
+
* @since @next
|
|
816
|
+
*/
|
|
817
|
+
var TooManyRoutes = class extends Data.TaggedError("TooManyRoutes") {
|
|
818
|
+
get message() {
|
|
819
|
+
return `More than ${this.limit} routes connect "${this.from}" and "${this.to}".`;
|
|
820
|
+
}
|
|
821
|
+
};
|
|
822
|
+
// src/features/planning/guide.ts
|
|
823
|
+
/** The declared occurrence a retained occurrence is rendered at. */
|
|
824
|
+
const anchorOf = (occurrence) => {
|
|
825
|
+
if (occurrence.origin._tag === "Declared") return occurrence;
|
|
826
|
+
const first = occurrence.origin.replaces[0];
|
|
827
|
+
return first === void 0 ? occurrence : anchorOf(first);
|
|
828
|
+
};
|
|
829
|
+
/**
|
|
830
|
+
* Resolves a migration's content against the occurrences that survived optimization.
|
|
831
|
+
* Removed occurrences disappear with their instruction blocks. Replacements render at
|
|
832
|
+
* the position of the first occurrence they replaced, using their own instruction or
|
|
833
|
+
* description. Unassociated prose stays.
|
|
834
|
+
*/
|
|
835
|
+
const resolveGuide = (content, retained, declaredId) => {
|
|
836
|
+
const byAnchor = /* @__PURE__ */ new Map();
|
|
837
|
+
for (const occurrence of retained) {
|
|
838
|
+
const anchor = anchorOf(occurrence).id;
|
|
839
|
+
const list = byAnchor.get(anchor) ?? [];
|
|
840
|
+
list.push(occurrence);
|
|
841
|
+
byAnchor.set(anchor, list);
|
|
842
|
+
}
|
|
843
|
+
const renderStep = (step) => {
|
|
844
|
+
if (step._tag === "Action" || step._tag === "Instruction") {
|
|
845
|
+
const action = step._tag === "Action" ? step : step.action;
|
|
846
|
+
return (byAnchor.get(declaredId(action)) ?? []).map((occurrence) => ({
|
|
847
|
+
_tag: "Occurrence",
|
|
848
|
+
occurrence
|
|
849
|
+
}));
|
|
850
|
+
}
|
|
851
|
+
const children = (step._tag === "Sequence" ? step.children : step.branches).map((child) => renderStep(child)).filter((items) => items.length > 0);
|
|
852
|
+
if (children.length === 0) return [];
|
|
853
|
+
return [{
|
|
854
|
+
_tag: "Group",
|
|
855
|
+
kind: step._tag,
|
|
856
|
+
children
|
|
857
|
+
}];
|
|
858
|
+
};
|
|
859
|
+
return content.flatMap((fragment) => {
|
|
860
|
+
if (fragment._tag === "Text" || fragment._tag === "ActionRef") return [fragment];
|
|
861
|
+
return renderStep(fragment);
|
|
862
|
+
});
|
|
863
|
+
};
|
|
864
|
+
// src/features/planning/identifiers.ts
|
|
865
|
+
const escape = (value, separators) => value.replace(separators, "\\$&");
|
|
866
|
+
const occurrencePart = (id) => escape(id, /[\\/()+]/g);
|
|
867
|
+
const routePart = (id) => escape(id, /[\\>[\]]/g);
|
|
868
|
+
/** `<migration>/<action>` forward, `<migration>/revert/<action>` in reverse. */
|
|
869
|
+
const declaredOccurrenceId = (migration, direction, action) => direction === "forward" ? `${occurrencePart(migration.id)}/${occurrencePart(action.id)}` : `${occurrencePart(migration.id)}/revert/${occurrencePart(action.id)}`;
|
|
870
|
+
/**
|
|
871
|
+
* `<rule>(<replaced>+<replaced>)/<action>`. Replaced identifiers are complete occurrence
|
|
872
|
+
* identifiers, so they nest without further escaping.
|
|
873
|
+
*/
|
|
874
|
+
const replacementOccurrenceId = (rule, replaced, action) => `${occurrencePart(rule.id)}(${replaced.map((occurrence) => occurrence.id).join("+")})/${occurrencePart(action.id)}`;
|
|
875
|
+
/** The first version of a route identifier. */
|
|
876
|
+
const routeStart = (version) => routePart(version);
|
|
877
|
+
/** A migration hop: `>` and the target version. */
|
|
878
|
+
const routeHop = (to) => `>${routePart(to)}`;
|
|
879
|
+
/** A shortcut hop: `>[shortcut]>` and the target version. */
|
|
880
|
+
const routeShortcut = (shortcut, to) => `>[${routePart(shortcut)}]>${routePart(to)}`;
|
|
881
|
+
// src/features/planning/rewrite.ts
|
|
882
|
+
var RewriteFailure = class {
|
|
883
|
+
failure;
|
|
884
|
+
constructor(failure) {
|
|
885
|
+
this.failure = failure;
|
|
886
|
+
}
|
|
887
|
+
};
|
|
888
|
+
/** The largest number of distinct states explored while normalizing one sequence. */
|
|
889
|
+
const stateLimit = 1e4;
|
|
890
|
+
const occurrencesOf = (items) => items.flatMap((item) => {
|
|
891
|
+
if (item._tag === "Occ") return [item.occurrence];
|
|
892
|
+
if (item._tag === "Par") return item.branches.flatMap(occurrencesOf);
|
|
893
|
+
return [];
|
|
894
|
+
});
|
|
895
|
+
const groupKey = (branches) => `{${Array$1.sort(branches.map((branch) => `[${branch}]`), Order.String).join("|")}}`;
|
|
896
|
+
/** A key that identifies a sequence by its action structure. Branch order is ignored. */
|
|
897
|
+
const keyOf = (items) => items.map((item) => {
|
|
898
|
+
if (item._tag === "Occ") return JSON.stringify(item.occurrence.action.id);
|
|
899
|
+
if (item._tag === "Barrier") return "#";
|
|
900
|
+
return groupKey(item.branches.map(keyOf));
|
|
901
|
+
}).join(",");
|
|
902
|
+
/** A key that identifies declared steps by their action structure. Branch order is ignored. */
|
|
903
|
+
const stepKey = (steps) => steps.map((step) => {
|
|
904
|
+
switch (step._tag) {
|
|
905
|
+
case "Action": return JSON.stringify(step.id);
|
|
906
|
+
case "Instruction": return JSON.stringify(step.action.id);
|
|
907
|
+
case "Sequence": return stepKey(step.children);
|
|
908
|
+
}
|
|
909
|
+
const branches = step.branches.map((branch) => stepKey([branch]));
|
|
910
|
+
if (branches.length === 1) return branches[0];
|
|
911
|
+
return groupKey(branches);
|
|
912
|
+
}).filter((key) => key !== "").join(",");
|
|
913
|
+
/** Converts declared steps to items. Sequences flatten; single-branch groups flatten. */
|
|
914
|
+
const itemsOf = (steps, make, step) => steps.flatMap((declared) => {
|
|
915
|
+
switch (declared._tag) {
|
|
916
|
+
case "Action": return [{
|
|
917
|
+
_tag: "Occ",
|
|
918
|
+
occurrence: make(declared, void 0),
|
|
919
|
+
step
|
|
920
|
+
}];
|
|
921
|
+
case "Instruction": return [{
|
|
922
|
+
_tag: "Occ",
|
|
923
|
+
occurrence: make(declared.action, declared),
|
|
924
|
+
step
|
|
925
|
+
}];
|
|
926
|
+
case "Sequence": return itemsOf(declared.children, make, step);
|
|
927
|
+
}
|
|
928
|
+
return parallelOf(declared.branches.map((branch) => itemsOf([branch], make, step)), declared.concurrency, step);
|
|
929
|
+
});
|
|
930
|
+
/** Drops empty branches and flattens a group that has a single branch left. */
|
|
931
|
+
const parallelOf = (branches, concurrency, step) => {
|
|
932
|
+
const nonEmpty = branches.filter((branch) => branch.length > 0);
|
|
933
|
+
if (nonEmpty.length === 0) return [];
|
|
934
|
+
if (nonEmpty.length === 1) return nonEmpty[0] ?? [];
|
|
935
|
+
return [{
|
|
936
|
+
_tag: "Par",
|
|
937
|
+
branches: nonEmpty,
|
|
938
|
+
concurrency,
|
|
939
|
+
step
|
|
940
|
+
}];
|
|
941
|
+
};
|
|
942
|
+
const matchesAt = (pattern, items, position) => pattern.every((expected, offset) => {
|
|
943
|
+
const item = items[position + offset];
|
|
944
|
+
if (item === void 0) return false;
|
|
945
|
+
if (expected._tag === "Action") return item._tag === "Occ" && item.occurrence.action.id === expected.id;
|
|
946
|
+
return item._tag === "Par" && keyOf([item]) === stepKey([expected]);
|
|
947
|
+
});
|
|
948
|
+
/** Replaces the match of a rule at a position with occurrences that record their provenance. */
|
|
949
|
+
const replace$1 = (rule, items, position) => {
|
|
950
|
+
const matched = items.slice(position, position + rule.match.length);
|
|
951
|
+
const removed = occurrencesOf(matched);
|
|
952
|
+
const first = removed[0];
|
|
953
|
+
if (first === void 0) throw new Error(`Rule "${rule.id}" matched no occurrences`);
|
|
954
|
+
const step = matched[0]?.step ?? 0;
|
|
955
|
+
const produced = [];
|
|
956
|
+
const make = (action, instruction) => {
|
|
957
|
+
const occurrence = {
|
|
958
|
+
_tag: "Occurrence",
|
|
959
|
+
id: replacementOccurrenceId(rule, removed, action),
|
|
960
|
+
action,
|
|
961
|
+
migration: first.migration,
|
|
962
|
+
direction: first.direction,
|
|
963
|
+
instruction,
|
|
964
|
+
origin: {
|
|
965
|
+
_tag: "Replacement",
|
|
966
|
+
rule,
|
|
967
|
+
replaces: removed
|
|
968
|
+
}
|
|
969
|
+
};
|
|
970
|
+
produced.push(occurrence);
|
|
971
|
+
return occurrence;
|
|
972
|
+
};
|
|
973
|
+
return {
|
|
974
|
+
next: [
|
|
975
|
+
...items.slice(0, position),
|
|
976
|
+
...itemsOf(rule.replacement, make, step),
|
|
977
|
+
...items.slice(position + rule.match.length)
|
|
978
|
+
],
|
|
979
|
+
application: {
|
|
980
|
+
rule,
|
|
981
|
+
removed,
|
|
982
|
+
produced
|
|
983
|
+
}
|
|
984
|
+
};
|
|
985
|
+
};
|
|
986
|
+
const makeNormalizer = (rules) => {
|
|
987
|
+
const ordered = Array$1.sort(rules, Order.mapInput(Order.String, (rule) => rule.id));
|
|
988
|
+
/**
|
|
989
|
+
* Every single rule application available in a sequence: at any position of the
|
|
990
|
+
* sequence itself, or inside any branch of its groups. Branch-local and whole-group
|
|
991
|
+
* rewrites compete as transitions from the same state.
|
|
992
|
+
*/
|
|
993
|
+
const transitions = (items) => {
|
|
994
|
+
const found = [];
|
|
995
|
+
for (let position = 0; position < items.length; position++) {
|
|
996
|
+
for (const rule of ordered) if (rule.match.length > 0 && matchesAt(rule.match, items, position)) found.push(replace$1(rule, items, position));
|
|
997
|
+
const item = items[position];
|
|
998
|
+
if (item?._tag !== "Par") continue;
|
|
999
|
+
for (const [index, branch] of item.branches.entries()) for (const inner of transitions(branch)) {
|
|
1000
|
+
const branches = item.branches.map((other, otherIndex) => otherIndex === index ? inner.next : other);
|
|
1001
|
+
found.push({
|
|
1002
|
+
next: [
|
|
1003
|
+
...items.slice(0, position),
|
|
1004
|
+
...parallelOf(branches, item.concurrency, item.step),
|
|
1005
|
+
...items.slice(position + 1)
|
|
1006
|
+
],
|
|
1007
|
+
application: inner.application
|
|
1008
|
+
});
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
return found;
|
|
1012
|
+
};
|
|
1013
|
+
/**
|
|
1014
|
+
* Explores every rewrite order of the whole sequence, nested groups included, under
|
|
1015
|
+
* one shared state and depth budget.
|
|
1016
|
+
*/
|
|
1017
|
+
const normalize = (input) => {
|
|
1018
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1019
|
+
const onPath = /* @__PURE__ */ new Map();
|
|
1020
|
+
const normals = /* @__PURE__ */ new Map();
|
|
1021
|
+
const visit = (items, trace) => {
|
|
1022
|
+
const key = keyOf(items);
|
|
1023
|
+
const entered = onPath.get(key);
|
|
1024
|
+
if (entered !== void 0) throw new RewriteFailure({
|
|
1025
|
+
_tag: "RewriteCycle",
|
|
1026
|
+
rules: trace.slice(entered).map((application) => application.rule.id),
|
|
1027
|
+
actions: key
|
|
1028
|
+
});
|
|
1029
|
+
if (seen.has(key)) return;
|
|
1030
|
+
seen.add(key);
|
|
1031
|
+
if (seen.size > 1e4) throw new RewriteFailure({
|
|
1032
|
+
_tag: "RewriteLimitExceeded",
|
|
1033
|
+
limit: "states",
|
|
1034
|
+
value: stateLimit
|
|
1035
|
+
});
|
|
1036
|
+
if (trace.length > 256) throw new RewriteFailure({
|
|
1037
|
+
_tag: "RewriteLimitExceeded",
|
|
1038
|
+
limit: "depth",
|
|
1039
|
+
value: 256
|
|
1040
|
+
});
|
|
1041
|
+
onPath.set(key, trace.length);
|
|
1042
|
+
const available = transitions(items);
|
|
1043
|
+
for (const { next, application } of available) visit(next, [...trace, application]);
|
|
1044
|
+
if (available.length === 0) normals.set(key, {
|
|
1045
|
+
items,
|
|
1046
|
+
trace
|
|
1047
|
+
});
|
|
1048
|
+
onPath.delete(key);
|
|
1049
|
+
};
|
|
1050
|
+
visit(input, []);
|
|
1051
|
+
if (normals.size > 1) throw new RewriteFailure({
|
|
1052
|
+
_tag: "ConflictingRewrites",
|
|
1053
|
+
outcomes: [...normals.entries()].map(([actions, outcome]) => ({
|
|
1054
|
+
actions,
|
|
1055
|
+
rules: outcome.trace.map((application) => application.rule.id)
|
|
1056
|
+
}))
|
|
1057
|
+
});
|
|
1058
|
+
const [outcome] = normals.values();
|
|
1059
|
+
return outcome ?? {
|
|
1060
|
+
items: input,
|
|
1061
|
+
trace: []
|
|
1062
|
+
};
|
|
1063
|
+
};
|
|
1064
|
+
return { normalize };
|
|
1065
|
+
};
|
|
1066
|
+
/** Applies rules to a sequence until no rule matches. */
|
|
1067
|
+
const rewrite = (items, rules) => {
|
|
1068
|
+
try {
|
|
1069
|
+
return {
|
|
1070
|
+
_tag: "Success",
|
|
1071
|
+
outcome: makeNormalizer(rules).normalize(items)
|
|
1072
|
+
};
|
|
1073
|
+
} catch (error) {
|
|
1074
|
+
if (error instanceof RewriteFailure) return {
|
|
1075
|
+
_tag: "Failure",
|
|
1076
|
+
failure: error.failure
|
|
1077
|
+
};
|
|
1078
|
+
throw error;
|
|
1079
|
+
}
|
|
1080
|
+
};
|
|
1081
|
+
// src/features/planning/validate.ts
|
|
1082
|
+
const issueOrder = Order.combine(Order.combine(Order.mapInput(Order.String, (issue) => issue._tag), Order.mapInput(Order.String, (issue) => issue.subject)), Order.mapInput(Order.String, (issue) => issue.message));
|
|
1083
|
+
/** Action instances declared by steps, in declaration order. */
|
|
1084
|
+
const actionsOfSteps = (steps) => steps.flatMap((step) => {
|
|
1085
|
+
switch (step._tag) {
|
|
1086
|
+
case "Action": return [step];
|
|
1087
|
+
case "Instruction": return [step.action];
|
|
1088
|
+
case "Sequence": return actionsOfSteps(step.children);
|
|
1089
|
+
}
|
|
1090
|
+
return actionsOfSteps(step.branches);
|
|
1091
|
+
});
|
|
1092
|
+
const stepsOf = (content) => content.flatMap((fragment) => fragment._tag === "Text" || fragment._tag === "ActionRef" ? [] : [fragment]);
|
|
1093
|
+
/** Whether two definitions with the same identifier describe the same change. */
|
|
1094
|
+
const sameDefinition = (self, that) => self === that || self.id === that.id && Equal.equals(self.metadata, that.metadata) && self.description === that.description && self.backportOf?.id === that.backportOf?.id && self.reverts.length === that.reverts.length && self.reverts.every((change, index) => change.id === that.reverts[index]?.id);
|
|
1095
|
+
/** Whether content declares at least one occurrence. Empty groups declare none. */
|
|
1096
|
+
const hasOccurrences = (content) => actionsOfSteps(stepsOf(content)).length > 0;
|
|
1097
|
+
/** Whether content has prose or mentions. */
|
|
1098
|
+
const hasProse = (content) => content.some((fragment) => fragment._tag === "ActionRef" || fragment._tag === "Text" && fragment.text.trim() !== "");
|
|
1099
|
+
const sameAction = (self, that) => self === that || self.kind === that.kind && Equal.equals(self.payload, that.payload);
|
|
1100
|
+
/** Collects every distinct migration: registered ones and those inside shortcuts. */
|
|
1101
|
+
const allMigrations = (declarations) => {
|
|
1102
|
+
const result = [...declarations.migrations];
|
|
1103
|
+
for (const shortcut of declarations.shortcuts) for (const migration of shortcut.migrations) if (!result.includes(migration)) result.push(migration);
|
|
1104
|
+
return result;
|
|
1105
|
+
};
|
|
1106
|
+
const isChain = (migrations, from, to) => {
|
|
1107
|
+
let current = from.id;
|
|
1108
|
+
for (const migration of migrations) {
|
|
1109
|
+
if (migration.from.id !== current) return false;
|
|
1110
|
+
current = migration.to.id;
|
|
1111
|
+
}
|
|
1112
|
+
return migrations.length > 0 && current === to.id;
|
|
1113
|
+
};
|
|
1114
|
+
const validate$1 = (declarations) => {
|
|
1115
|
+
const issues = [];
|
|
1116
|
+
const issue = (tag, subject, message) => issues.push({
|
|
1117
|
+
_tag: tag,
|
|
1118
|
+
subject,
|
|
1119
|
+
message
|
|
1120
|
+
});
|
|
1121
|
+
const versions = /* @__PURE__ */ new Map();
|
|
1122
|
+
for (const version of declarations.versions) {
|
|
1123
|
+
if (versions.has(version.id)) {
|
|
1124
|
+
issue("DuplicateVersion", version.id, `Version "${version.id}" is registered more than once.`);
|
|
1125
|
+
continue;
|
|
1126
|
+
}
|
|
1127
|
+
versions.set(version.id, version);
|
|
1128
|
+
}
|
|
1129
|
+
const unregistered = /* @__PURE__ */ new Map();
|
|
1130
|
+
const requireVersion = (version, referencedBy) => {
|
|
1131
|
+
if (versions.get(version.id) !== void 0) return;
|
|
1132
|
+
const referrers = unregistered.get(version.id) ?? [];
|
|
1133
|
+
if (!referrers.includes(referencedBy)) referrers.push(referencedBy);
|
|
1134
|
+
unregistered.set(version.id, referrers);
|
|
1135
|
+
};
|
|
1136
|
+
for (const version of versions.values()) if (version.previous !== void 0) requireVersion(version.previous, `version "${version.id}"`);
|
|
1137
|
+
const ancestry = Graph.directed((graph) => {
|
|
1138
|
+
const nodes = /* @__PURE__ */ new Map();
|
|
1139
|
+
for (const id of versions.keys()) nodes.set(id, Graph.addNode(graph, id));
|
|
1140
|
+
for (const version of versions.values()) {
|
|
1141
|
+
const source = nodes.get(version.id);
|
|
1142
|
+
const target = version.previous === void 0 ? void 0 : nodes.get(version.previous.id);
|
|
1143
|
+
if (source !== void 0 && target !== void 0) Graph.addEdge(graph, source, target, void 0);
|
|
1144
|
+
}
|
|
1145
|
+
});
|
|
1146
|
+
const cycle = Graph.findCycle(ancestry);
|
|
1147
|
+
if (Option.isSome(cycle)) {
|
|
1148
|
+
const ids = cycle.value.path.flatMap((index) => Option.toArray(Graph.getNode(ancestry, index)));
|
|
1149
|
+
issue("AncestryCycle", ids[0] ?? "", `Release ancestry contains a cycle: ${ids.join(" -> ")}.`);
|
|
1150
|
+
}
|
|
1151
|
+
const changes = /* @__PURE__ */ new Map();
|
|
1152
|
+
const visited = /* @__PURE__ */ new Set();
|
|
1153
|
+
const conflictingChanges = /* @__PURE__ */ new Set();
|
|
1154
|
+
const visitChange = (change) => {
|
|
1155
|
+
if (visited.has(change)) return;
|
|
1156
|
+
visited.add(change);
|
|
1157
|
+
const known = changes.get(change.id);
|
|
1158
|
+
if (known === void 0) changes.set(change.id, change);
|
|
1159
|
+
else if (!sameDefinition(known, change) && !conflictingChanges.has(change.id)) {
|
|
1160
|
+
conflictingChanges.add(change.id);
|
|
1161
|
+
issue("ConflictingChange", change.id, `Change "${change.id}" has conflicting definitions.`);
|
|
1162
|
+
}
|
|
1163
|
+
for (const reverted of change.reverts) visitChange(reverted);
|
|
1164
|
+
if (change.backportOf !== void 0) visitChange(change.backportOf);
|
|
1165
|
+
};
|
|
1166
|
+
for (const version of declarations.versions) for (const change of version.changes) visitChange(change);
|
|
1167
|
+
const actions = /* @__PURE__ */ new Map();
|
|
1168
|
+
const conflicting = /* @__PURE__ */ new Set();
|
|
1169
|
+
const visitAction = (action) => {
|
|
1170
|
+
const known = actions.get(action.id);
|
|
1171
|
+
if (known === void 0) {
|
|
1172
|
+
actions.set(action.id, action);
|
|
1173
|
+
return;
|
|
1174
|
+
}
|
|
1175
|
+
if (!sameAction(known, action) && !conflicting.has(action.id)) {
|
|
1176
|
+
conflicting.add(action.id);
|
|
1177
|
+
issue("ConflictingAction", action.id, `Action "${action.id}" has conflicting definitions; action identifiers must be unique.`);
|
|
1178
|
+
}
|
|
1179
|
+
};
|
|
1180
|
+
const migrations = allMigrations(declarations);
|
|
1181
|
+
const migrationIds = /* @__PURE__ */ new Map();
|
|
1182
|
+
const registered = /* @__PURE__ */ new Set();
|
|
1183
|
+
for (const migration of declarations.migrations) {
|
|
1184
|
+
if (registered.has(migration) || migrationIds.has(migration.id)) issue("DuplicateMigration", migration.id, `Migration "${migration.id}" is registered more than once. Register a migration once; register the result of Migration.withRevert in place of the original.`);
|
|
1185
|
+
registered.add(migration);
|
|
1186
|
+
migrationIds.set(migration.id, migration);
|
|
1187
|
+
}
|
|
1188
|
+
for (const migration of migrations) {
|
|
1189
|
+
const known = migrationIds.get(migration.id);
|
|
1190
|
+
if (known !== void 0 && known !== migration && !registered.has(migration)) issue("DuplicateMigration", migration.id, `Migration "${migration.id}" has another definition inside a shortcut.`);
|
|
1191
|
+
if (known === void 0) migrationIds.set(migration.id, migration);
|
|
1192
|
+
requireVersion(migration.from, `migration "${migration.id}"`);
|
|
1193
|
+
requireVersion(migration.to, `migration "${migration.id}"`);
|
|
1194
|
+
if (migration.from.id === migration.to.id) issue("SelfMigration", migration.id, `Migration "${migration.id}" starts and ends at the same version.`);
|
|
1195
|
+
for (const change of migration.addresses) visitChange(change);
|
|
1196
|
+
const sides = [["forward", migration.content], ["revert", migration.revert]];
|
|
1197
|
+
for (const [direction, content] of sides) {
|
|
1198
|
+
if (content === void 0) continue;
|
|
1199
|
+
if (!migration.noop && !hasOccurrences(content) && !hasProse(content)) issue("EmptyMigration", migration.id, `The ${direction} side of migration "${migration.id}" is empty. Use Migration.noop to declare a zero-work transition.`);
|
|
1200
|
+
const counts = /* @__PURE__ */ new Map();
|
|
1201
|
+
for (const action of actionsOfSteps(stepsOf(content))) {
|
|
1202
|
+
visitAction(action);
|
|
1203
|
+
counts.set(action.id, (counts.get(action.id) ?? 0) + 1);
|
|
1204
|
+
}
|
|
1205
|
+
for (const [actionId, count] of counts) {
|
|
1206
|
+
if (count < 2) continue;
|
|
1207
|
+
issue("DuplicateOccurrence", migration.id, `Action "${actionId}" occurs ${count} times in the ${direction} side of migration "${migration.id}". Use Action.ref to mention an action, or a separate action instance to repeat the work.`);
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
const edgeSides = /* @__PURE__ */ new Map();
|
|
1212
|
+
const addSide = (from, to, label) => {
|
|
1213
|
+
const key = JSON.stringify([from.id, to.id]);
|
|
1214
|
+
const side = edgeSides.get(key) ?? {
|
|
1215
|
+
from: from.id,
|
|
1216
|
+
to: to.id,
|
|
1217
|
+
labels: []
|
|
1218
|
+
};
|
|
1219
|
+
side.labels.push(label);
|
|
1220
|
+
edgeSides.set(key, side);
|
|
1221
|
+
};
|
|
1222
|
+
for (const migration of new Set(migrationIds.values())) {
|
|
1223
|
+
if (!registered.has(migration)) continue;
|
|
1224
|
+
addSide(migration.from, migration.to, `"${migration.id}"`);
|
|
1225
|
+
if (migration.revert !== void 0) addSide(migration.to, migration.from, `the revert of "${migration.id}"`);
|
|
1226
|
+
}
|
|
1227
|
+
for (const { from, to, labels } of edgeSides.values()) {
|
|
1228
|
+
if (labels.length < 2) continue;
|
|
1229
|
+
issue("DuplicateEdge", `${from} -> ${to}`, `${labels.join(" and ")} both go from "${from}" to "${to}". Merge them into one migration.`);
|
|
1230
|
+
}
|
|
1231
|
+
const rules = /* @__PURE__ */ new Set();
|
|
1232
|
+
for (const rule of declarations.rules) {
|
|
1233
|
+
if (rules.has(rule.id)) issue("DuplicateRule", rule.id, `Rule "${rule.id}" is registered more than once.`);
|
|
1234
|
+
rules.add(rule.id);
|
|
1235
|
+
if (rule.match.length === 0) issue("InvalidRule", rule.id, `Rule "${rule.id}" matches nothing.`);
|
|
1236
|
+
const patternActions = actionsOfSteps(rule.match);
|
|
1237
|
+
const replacementActions = actionsOfSteps(rule.replacement);
|
|
1238
|
+
for (const action of [...patternActions, ...replacementActions]) visitAction(action);
|
|
1239
|
+
if (new Set(replacementActions.map((action) => action.id)).size !== replacementActions.length) issue("InvalidRule", rule.id, `Rule "${rule.id}" repeats an action in its replacement.`);
|
|
1240
|
+
if (rule.match.length > 0 && stepKey(rule.match) === stepKey(rule.replacement)) issue("InvalidRule", rule.id, `Rule "${rule.id}" replaces its match with itself.`);
|
|
1241
|
+
}
|
|
1242
|
+
const shortcuts = /* @__PURE__ */ new Set();
|
|
1243
|
+
for (const shortcut of declarations.shortcuts) {
|
|
1244
|
+
if (shortcuts.has(shortcut.id)) issue("DuplicateShortcut", shortcut.id, `Shortcut "${shortcut.id}" is registered more than once.`);
|
|
1245
|
+
shortcuts.add(shortcut.id);
|
|
1246
|
+
requireVersion(shortcut.from, `shortcut "${shortcut.id}"`);
|
|
1247
|
+
requireVersion(shortcut.to, `shortcut "${shortcut.id}"`);
|
|
1248
|
+
const unknown = shortcut.replaces.filter((migration) => !registered.has(migration));
|
|
1249
|
+
if (unknown.length > 0) issue("InvalidShortcut", shortcut.id, `Shortcut "${shortcut.id}" replaces unregistered migrations: ${unknown.map((migration) => migration.id).join(", ")}.`);
|
|
1250
|
+
if (!isChain(shortcut.replaces, shortcut.from, shortcut.to)) issue("InvalidShortcut", shortcut.id, `Shortcut "${shortcut.id}" must replace a consecutive route from "${shortcut.from.id}" to "${shortcut.to.id}".`);
|
|
1251
|
+
if (!isChain(shortcut.migrations, shortcut.from, shortcut.to)) issue("InvalidShortcut", shortcut.id, `The migrations of shortcut "${shortcut.id}" must form a route from "${shortcut.from.id}" to "${shortcut.to.id}".`);
|
|
1252
|
+
}
|
|
1253
|
+
for (const [id, referrers] of unregistered) issue("UnregisteredVersion", id, `Version "${id}" is referenced by ${referrers.join(", ")} but is not registered.`);
|
|
1254
|
+
return Array$1.sort(issues, issueOrder);
|
|
1255
|
+
};
|
|
1256
|
+
const memberCost = (migration) => migration.cost ?? 1;
|
|
1257
|
+
const makeContext = (declarations) => {
|
|
1258
|
+
const versions = new Map(declarations.versions.map((version) => [version.id, version]));
|
|
1259
|
+
const ancestors = /* @__PURE__ */ new Map();
|
|
1260
|
+
const ancestorsOf = (id) => {
|
|
1261
|
+
const known = ancestors.get(id);
|
|
1262
|
+
if (known !== void 0) return known;
|
|
1263
|
+
const result = /* @__PURE__ */ new Set();
|
|
1264
|
+
let current = versions.get(id)?.previous;
|
|
1265
|
+
while (current !== void 0 && !result.has(current.id)) {
|
|
1266
|
+
result.add(current.id);
|
|
1267
|
+
current = versions.get(current.id)?.previous;
|
|
1268
|
+
}
|
|
1269
|
+
ancestors.set(id, result);
|
|
1270
|
+
return result;
|
|
1271
|
+
};
|
|
1272
|
+
for (const id of versions.keys()) ancestorsOf(id);
|
|
1273
|
+
const classOf = (from, to) => {
|
|
1274
|
+
if (ancestorsOf(to).has(from)) return "up";
|
|
1275
|
+
if (ancestorsOf(from).has(to)) return "down";
|
|
1276
|
+
return "cross";
|
|
1277
|
+
};
|
|
1278
|
+
const edges = declarations.migrations.flatMap((migration) => {
|
|
1279
|
+
const forward = {
|
|
1280
|
+
from: migration.from,
|
|
1281
|
+
to: migration.to,
|
|
1282
|
+
members: [{
|
|
1283
|
+
migration,
|
|
1284
|
+
direction: "forward"
|
|
1285
|
+
}],
|
|
1286
|
+
shortcut: void 0,
|
|
1287
|
+
cost: memberCost(migration),
|
|
1288
|
+
class: classOf(migration.from.id, migration.to.id)
|
|
1289
|
+
};
|
|
1290
|
+
if (migration.revert === void 0) return [forward];
|
|
1291
|
+
return [forward, {
|
|
1292
|
+
from: migration.to,
|
|
1293
|
+
to: migration.from,
|
|
1294
|
+
members: [{
|
|
1295
|
+
migration,
|
|
1296
|
+
direction: "revert"
|
|
1297
|
+
}],
|
|
1298
|
+
shortcut: void 0,
|
|
1299
|
+
cost: memberCost(migration),
|
|
1300
|
+
class: classOf(migration.to.id, migration.from.id)
|
|
1301
|
+
}];
|
|
1302
|
+
});
|
|
1303
|
+
for (const shortcut of declarations.shortcuts) edges.push({
|
|
1304
|
+
from: shortcut.from,
|
|
1305
|
+
to: shortcut.to,
|
|
1306
|
+
members: shortcut.migrations.map((migration) => ({
|
|
1307
|
+
migration,
|
|
1308
|
+
direction: "forward"
|
|
1309
|
+
})),
|
|
1310
|
+
shortcut,
|
|
1311
|
+
cost: shortcut.cost ?? shortcut.migrations.reduce((total, migration) => total + memberCost(migration), 0),
|
|
1312
|
+
class: classOf(shortcut.from.id, shortcut.to.id)
|
|
1313
|
+
});
|
|
1314
|
+
return {
|
|
1315
|
+
declarations,
|
|
1316
|
+
versions,
|
|
1317
|
+
ancestors,
|
|
1318
|
+
edges
|
|
1319
|
+
};
|
|
1320
|
+
};
|
|
1321
|
+
const requestDirection = (context, from, to) => {
|
|
1322
|
+
if (from === to) return "none";
|
|
1323
|
+
if (context.ancestors.get(to)?.has(from)) return "upgrade";
|
|
1324
|
+
if (context.ancestors.get(from)?.has(to)) return "downgrade";
|
|
1325
|
+
return "cross-branch";
|
|
1326
|
+
};
|
|
1327
|
+
/**
|
|
1328
|
+
* Edge classes allowed together in one route. A route never mixes moves toward
|
|
1329
|
+
* ancestors with moves toward descendants, so sharing an ancestor does not authorize a
|
|
1330
|
+
* downgrade-and-upgrade detour. Explicit cross-branch migrations fit either family.
|
|
1331
|
+
*/
|
|
1332
|
+
const families = [/* @__PURE__ */ new Set(["up", "cross"]), /* @__PURE__ */ new Set(["down", "cross"])];
|
|
1333
|
+
const routeCost = (edges) => edges.reduce((total, edge) => total + edge.cost, 0);
|
|
1334
|
+
const routeOf = (from, edges) => {
|
|
1335
|
+
let id = routeStart(from.id);
|
|
1336
|
+
const hops = [];
|
|
1337
|
+
for (const edge of edges) {
|
|
1338
|
+
if (edge.shortcut === void 0) {
|
|
1339
|
+
id += routeHop(edge.to.id);
|
|
1340
|
+
hops.push({
|
|
1341
|
+
_tag: "Migrations",
|
|
1342
|
+
from: edge.from,
|
|
1343
|
+
to: edge.to,
|
|
1344
|
+
migrations: edge.members
|
|
1345
|
+
});
|
|
1346
|
+
continue;
|
|
1347
|
+
}
|
|
1348
|
+
id += routeShortcut(edge.shortcut.id, edge.to.id);
|
|
1349
|
+
hops.push({
|
|
1350
|
+
_tag: "Shortcut",
|
|
1351
|
+
from: edge.from,
|
|
1352
|
+
to: edge.to,
|
|
1353
|
+
shortcut: edge.shortcut
|
|
1354
|
+
});
|
|
1355
|
+
}
|
|
1356
|
+
return {
|
|
1357
|
+
id,
|
|
1358
|
+
hops
|
|
1359
|
+
};
|
|
1360
|
+
};
|
|
1361
|
+
const graphOf = (context, family) => {
|
|
1362
|
+
const nodes = /* @__PURE__ */ new Map();
|
|
1363
|
+
return {
|
|
1364
|
+
graph: Graph.directed((mutable) => {
|
|
1365
|
+
for (const id of context.versions.keys()) nodes.set(id, Graph.addNode(mutable, id));
|
|
1366
|
+
for (const edge of context.edges) {
|
|
1367
|
+
if (!family.has(edge.class)) continue;
|
|
1368
|
+
const source = nodes.get(edge.from.id);
|
|
1369
|
+
const target = nodes.get(edge.to.id);
|
|
1370
|
+
if (source !== void 0 && target !== void 0) Graph.addEdge(mutable, source, target, edge);
|
|
1371
|
+
}
|
|
1372
|
+
}),
|
|
1373
|
+
nodes
|
|
1374
|
+
};
|
|
1375
|
+
};
|
|
1376
|
+
const enumerateRoutes = (context, from, to) => {
|
|
1377
|
+
const routes = /* @__PURE__ */ new Map();
|
|
1378
|
+
for (const family of families) {
|
|
1379
|
+
const { graph, nodes } = graphOf(context, family);
|
|
1380
|
+
const source = nodes.get(from.id);
|
|
1381
|
+
const target = nodes.get(to.id);
|
|
1382
|
+
if (source === void 0 || target === void 0) continue;
|
|
1383
|
+
for (const path of Graph.simplePaths(graph, {
|
|
1384
|
+
source,
|
|
1385
|
+
target,
|
|
1386
|
+
limit: 101
|
|
1387
|
+
})) routes.set(routeOf(from, path.costs).id, path.costs);
|
|
1388
|
+
}
|
|
1389
|
+
const found = [...routes.values()];
|
|
1390
|
+
return {
|
|
1391
|
+
routes: found.slice(0, 100),
|
|
1392
|
+
overLimit: found.length > 100
|
|
1393
|
+
};
|
|
1394
|
+
};
|
|
1395
|
+
const toNode = (items) => items.flatMap((item) => {
|
|
1396
|
+
if (item._tag === "Occ") return [item.occurrence];
|
|
1397
|
+
if (item._tag === "Barrier") return [];
|
|
1398
|
+
return [{
|
|
1399
|
+
_tag: "Parallel",
|
|
1400
|
+
branches: item.branches.map((branch) => {
|
|
1401
|
+
const children = toNode(branch);
|
|
1402
|
+
const [only] = children;
|
|
1403
|
+
return children.length === 1 && only !== void 0 ? only : {
|
|
1404
|
+
_tag: "Sequence",
|
|
1405
|
+
children
|
|
1406
|
+
};
|
|
1407
|
+
}),
|
|
1408
|
+
concurrency: item.concurrency
|
|
1409
|
+
}];
|
|
1410
|
+
});
|
|
1411
|
+
const contentOf = (member) => (member.direction === "forward" ? member.migration.content : member.migration.revert) ?? [];
|
|
1412
|
+
const buildPlan = (context, from, to, direction, edges) => {
|
|
1413
|
+
const route = routeOf(from, edges);
|
|
1414
|
+
const members = edges.flatMap((edge) => edge.members);
|
|
1415
|
+
const items = members.flatMap((member, index) => {
|
|
1416
|
+
const content = contentOf(member);
|
|
1417
|
+
const make = (action, instruction) => ({
|
|
1418
|
+
_tag: "Occurrence",
|
|
1419
|
+
id: declaredOccurrenceId(member.migration, member.direction, action),
|
|
1420
|
+
action,
|
|
1421
|
+
migration: member.migration,
|
|
1422
|
+
direction: member.direction,
|
|
1423
|
+
instruction,
|
|
1424
|
+
origin: { _tag: "Declared" }
|
|
1425
|
+
});
|
|
1426
|
+
const declared = itemsOf(stepsOf(content), make, index);
|
|
1427
|
+
if (declared.length === 0 && hasProse(content)) return [{
|
|
1428
|
+
_tag: "Barrier",
|
|
1429
|
+
step: index
|
|
1430
|
+
}];
|
|
1431
|
+
return declared;
|
|
1432
|
+
});
|
|
1433
|
+
const rewritten = rewrite(items, context.declarations.rules);
|
|
1434
|
+
if (rewritten._tag === "Failure") {
|
|
1435
|
+
const failure = rewritten.failure;
|
|
1436
|
+
switch (failure._tag) {
|
|
1437
|
+
case "RewriteCycle": return Result.fail(new RewriteCycle({
|
|
1438
|
+
route: route.id,
|
|
1439
|
+
rules: failure.rules,
|
|
1440
|
+
actions: failure.actions
|
|
1441
|
+
}));
|
|
1442
|
+
case "ConflictingRewrites": return Result.fail(new ConflictingRewrites({
|
|
1443
|
+
route: route.id,
|
|
1444
|
+
outcomes: failure.outcomes
|
|
1445
|
+
}));
|
|
1446
|
+
}
|
|
1447
|
+
return Result.fail(new RewriteLimitExceeded({
|
|
1448
|
+
route: route.id,
|
|
1449
|
+
limit: failure.limit,
|
|
1450
|
+
value: failure.value
|
|
1451
|
+
}));
|
|
1452
|
+
}
|
|
1453
|
+
const { items: optimized, trace } = rewritten.outcome;
|
|
1454
|
+
const explanations = trace.map((application) => ({
|
|
1455
|
+
_tag: "Rewrite",
|
|
1456
|
+
rule: application.rule,
|
|
1457
|
+
removed: application.removed,
|
|
1458
|
+
produced: application.produced
|
|
1459
|
+
}));
|
|
1460
|
+
const steps = members.map((member, index) => {
|
|
1461
|
+
const stepItems = optimized.filter((item) => item.step === index);
|
|
1462
|
+
const content = contentOf(member);
|
|
1463
|
+
const declaredCount = actionsOfSteps(stepsOf(content)).length;
|
|
1464
|
+
const retained = occurrencesOf(stepItems);
|
|
1465
|
+
const guide = resolveGuide(content, retained, (action) => declaredOccurrenceId(member.migration, member.direction, action));
|
|
1466
|
+
const allRemoved = declaredCount > 0 && retained.length === 0;
|
|
1467
|
+
const noop = member.migration.noop && content.length === 0;
|
|
1468
|
+
const omitted = noop || allRemoved;
|
|
1469
|
+
if (omitted) explanations.push({
|
|
1470
|
+
_tag: "OmittedStep",
|
|
1471
|
+
migration: member.migration,
|
|
1472
|
+
direction: member.direction,
|
|
1473
|
+
reason: noop ? "noop" : "all-actions-removed"
|
|
1474
|
+
});
|
|
1475
|
+
const [stepFrom, stepTo] = member.direction === "forward" ? [member.migration.from, member.migration.to] : [member.migration.to, member.migration.from];
|
|
1476
|
+
return {
|
|
1477
|
+
_tag: "Step",
|
|
1478
|
+
migration: member.migration,
|
|
1479
|
+
direction: member.direction,
|
|
1480
|
+
from: stepFrom,
|
|
1481
|
+
to: stepTo,
|
|
1482
|
+
node: {
|
|
1483
|
+
_tag: "Sequence",
|
|
1484
|
+
children: toNode(stepItems)
|
|
1485
|
+
},
|
|
1486
|
+
guide: omitted ? [] : guide,
|
|
1487
|
+
omitted
|
|
1488
|
+
};
|
|
1489
|
+
});
|
|
1490
|
+
const changes = [];
|
|
1491
|
+
for (const member of members) for (const change of member.migration.addresses) if (!changes.some((known) => known.id === change.id)) changes.push(change);
|
|
1492
|
+
return Result.succeed({
|
|
1493
|
+
_tag: "Plan",
|
|
1494
|
+
from,
|
|
1495
|
+
to,
|
|
1496
|
+
direction,
|
|
1497
|
+
route,
|
|
1498
|
+
steps,
|
|
1499
|
+
occurrences: occurrencesOf(optimized),
|
|
1500
|
+
changes,
|
|
1501
|
+
explanations,
|
|
1502
|
+
coverage: "static"
|
|
1503
|
+
});
|
|
1504
|
+
};
|
|
1505
|
+
const summaryOf = (candidate) => ({
|
|
1506
|
+
id: candidate.route.id,
|
|
1507
|
+
migrations: candidate.edges.flatMap((edge) => edge.members.map((member) => member.migration.id)),
|
|
1508
|
+
shortcuts: candidate.edges.flatMap((edge) => edge.shortcut === void 0 ? [] : [edge.shortcut.id]),
|
|
1509
|
+
actions: Result.isSuccess(candidate.plan) ? candidate.plan.success.occurrences.length : void 0,
|
|
1510
|
+
cost: candidate.cost,
|
|
1511
|
+
failure: Result.isFailure(candidate.plan) ? candidate.plan.failure.message : void 0
|
|
1512
|
+
});
|
|
1513
|
+
/** Migration tokens of a route, with shortcut hops expanded to the route they replace. */
|
|
1514
|
+
const canonicalTokens = (candidate) => JSON.stringify(candidate.edges.flatMap((edge) => edge.shortcut === void 0 ? edge.members.map((member) => [member.direction, member.migration.id]) : edge.shortcut.replaces.map((migration) => ["forward", migration.id])));
|
|
1515
|
+
const shortcutCount = (candidate) => candidate.edges.filter((edge) => edge.shortcut !== void 0).length;
|
|
1516
|
+
const minimal = (candidates, measure) => {
|
|
1517
|
+
const measured = candidates.flatMap((candidate) => {
|
|
1518
|
+
const value = measure(candidate);
|
|
1519
|
+
return value === void 0 ? [] : [{
|
|
1520
|
+
candidate,
|
|
1521
|
+
value
|
|
1522
|
+
}];
|
|
1523
|
+
});
|
|
1524
|
+
const best = Math.min(...measured.map((entry) => entry.value));
|
|
1525
|
+
return measured.filter((entry) => entry.value === best).map((entry) => entry.candidate);
|
|
1526
|
+
};
|
|
1527
|
+
const applyPolicy = (policy, candidates) => {
|
|
1528
|
+
switch (policy) {
|
|
1529
|
+
case "fewest-actions": return minimal(candidates, (candidate) => Result.isSuccess(candidate.plan) ? candidate.plan.success.occurrences.length : void 0);
|
|
1530
|
+
case "fewest-migrations": return minimal(candidates, (candidate) => candidate.edges.reduce((total, edge) => total + edge.members.length, 0));
|
|
1531
|
+
case "lowest-cost": return minimal(candidates, (candidate) => candidate.cost);
|
|
1532
|
+
}
|
|
1533
|
+
return candidates.filter((candidate) => !candidates.some((other) => other !== candidate && canonicalTokens(other) === canonicalTokens(candidate) && shortcutCount(other) > shortcutCount(candidate)));
|
|
1534
|
+
};
|
|
1535
|
+
const depthOf = (context, id) => context.ancestors.get(id)?.size ?? 0;
|
|
1536
|
+
/** Distance between two versions in the release ancestry tree. */
|
|
1537
|
+
const ancestryDistance = (context, self, that) => {
|
|
1538
|
+
const selfLine = [self, ...ancestorsInOrder(context, self)];
|
|
1539
|
+
const thatLine = /* @__PURE__ */ new Set([that, ...ancestorsInOrder(context, that)]);
|
|
1540
|
+
const common = selfLine.find((id) => thatLine.has(id));
|
|
1541
|
+
if (common === void 0) return Number.POSITIVE_INFINITY;
|
|
1542
|
+
return depthOf(context, self) + depthOf(context, that) - 2 * depthOf(context, common);
|
|
1543
|
+
};
|
|
1544
|
+
const ancestorsInOrder = (context, id) => {
|
|
1545
|
+
const result = [];
|
|
1546
|
+
let current = context.versions.get(id)?.previous;
|
|
1547
|
+
while (current !== void 0 && !result.includes(current.id)) {
|
|
1548
|
+
result.push(current.id);
|
|
1549
|
+
current = context.versions.get(current.id)?.previous;
|
|
1550
|
+
}
|
|
1551
|
+
return result;
|
|
1552
|
+
};
|
|
1553
|
+
/** The ancestry path between two versions, through their closest common ancestor. */
|
|
1554
|
+
const ancestryPath = (context, from, to) => {
|
|
1555
|
+
const fromLine = [from, ...ancestorsInOrder(context, from)];
|
|
1556
|
+
const toLine = [to, ...ancestorsInOrder(context, to)];
|
|
1557
|
+
const common = fromLine.find((id) => toLine.includes(id));
|
|
1558
|
+
if (common === void 0) return [];
|
|
1559
|
+
return [...fromLine.slice(0, fromLine.indexOf(common) + 1), ...Array$1.reverse(toLine.slice(0, toLine.indexOf(common)))];
|
|
1560
|
+
};
|
|
1561
|
+
const reachable = (context, from, family) => {
|
|
1562
|
+
const seen = /* @__PURE__ */ new Set([from]);
|
|
1563
|
+
const queue = [from];
|
|
1564
|
+
for (let current = queue.shift(); current !== void 0; current = queue.shift()) for (const edge of context.edges) {
|
|
1565
|
+
if (edge.from.id !== current || !family.has(edge.class) || seen.has(edge.to.id)) continue;
|
|
1566
|
+
seen.add(edge.to.id);
|
|
1567
|
+
queue.push(edge.to.id);
|
|
1568
|
+
}
|
|
1569
|
+
seen.delete(from);
|
|
1570
|
+
return [...seen];
|
|
1571
|
+
};
|
|
1572
|
+
const familiesFor = (direction) => {
|
|
1573
|
+
if (direction === "upgrade") return [families[0]];
|
|
1574
|
+
if (direction === "downgrade") return [families[1]];
|
|
1575
|
+
return families;
|
|
1576
|
+
};
|
|
1577
|
+
const unsupported = (context, from, to, direction) => {
|
|
1578
|
+
const allowed = familiesFor(direction).flatMap((family) => family === void 0 ? [] : [family]);
|
|
1579
|
+
const reachableIds = Array$1.sort(new Set(allowed.flatMap((family) => reachable(context, from, family))), Order.String);
|
|
1580
|
+
const distances = reachableIds.map((id) => ({
|
|
1581
|
+
id,
|
|
1582
|
+
distance: ancestryDistance(context, id, to)
|
|
1583
|
+
}));
|
|
1584
|
+
const best = Math.min(...distances.map((entry) => entry.distance));
|
|
1585
|
+
const closest = Number.isFinite(best) ? distances.filter((entry) => entry.distance === best).map((entry) => entry.id) : [];
|
|
1586
|
+
const path = ancestryPath(context, from, to);
|
|
1587
|
+
const blockers = [];
|
|
1588
|
+
for (let index = 0; index + 1 < path.length; index++) {
|
|
1589
|
+
const hopFrom = path[index];
|
|
1590
|
+
const hopTo = path[index + 1];
|
|
1591
|
+
if (hopFrom === void 0 || hopTo === void 0) continue;
|
|
1592
|
+
if (context.edges.some((edge) => edge.from.id === hopFrom && edge.to.id === hopTo)) continue;
|
|
1593
|
+
blockers.push({
|
|
1594
|
+
from: hopFrom,
|
|
1595
|
+
to: hopTo,
|
|
1596
|
+
reason: `No migration is declared from "${hopFrom}" to "${hopTo}".`
|
|
1597
|
+
});
|
|
1598
|
+
}
|
|
1599
|
+
const turn = path.find((id) => context.ancestors.get(from)?.has(id) === true && context.ancestors.get(to)?.has(id) === true);
|
|
1600
|
+
if (blockers.length === 0 && turn !== void 0) blockers.push({
|
|
1601
|
+
from,
|
|
1602
|
+
to,
|
|
1603
|
+
reason: `A route through "${turn}" would combine a downgrade with an upgrade. Declare a migration from "${from}" to "${to}".`
|
|
1604
|
+
});
|
|
1605
|
+
return new UnsupportedRoute({
|
|
1606
|
+
from,
|
|
1607
|
+
to,
|
|
1608
|
+
direction,
|
|
1609
|
+
reachable: reachableIds,
|
|
1610
|
+
closest,
|
|
1611
|
+
blockers
|
|
1612
|
+
});
|
|
1613
|
+
};
|
|
1614
|
+
const lookup = (context, reference) => {
|
|
1615
|
+
const id = typeof reference === "string" ? reference : reference.id;
|
|
1616
|
+
const version = context.versions.get(id);
|
|
1617
|
+
return version === void 0 ? Result.fail(new UnknownVersion({
|
|
1618
|
+
version: id,
|
|
1619
|
+
known: [...context.versions.keys()]
|
|
1620
|
+
})) : Result.succeed(version);
|
|
1621
|
+
};
|
|
1622
|
+
const candidatesBetween = (context, from, to) => {
|
|
1623
|
+
const direction = requestDirection(context, from.id, to.id);
|
|
1624
|
+
const { routes, overLimit } = enumerateRoutes(context, from, to);
|
|
1625
|
+
if (overLimit) return Result.fail(new TooManyRoutes({
|
|
1626
|
+
from: from.id,
|
|
1627
|
+
to: to.id,
|
|
1628
|
+
limit: 100
|
|
1629
|
+
}));
|
|
1630
|
+
return Result.succeed(routes.map((edges) => ({
|
|
1631
|
+
route: routeOf(from, edges),
|
|
1632
|
+
edges,
|
|
1633
|
+
plan: buildPlan(context, from, to, direction, edges),
|
|
1634
|
+
cost: routeCost(edges)
|
|
1635
|
+
})));
|
|
1636
|
+
};
|
|
1637
|
+
const resolve$1 = (context, options) => Result.gen(function* () {
|
|
1638
|
+
const from = yield* lookup(context, options.from);
|
|
1639
|
+
const to = yield* lookup(context, options.to);
|
|
1640
|
+
const direction = requestDirection(context, from.id, to.id);
|
|
1641
|
+
if (direction === "none") return yield* buildPlan(context, from, to, direction, []);
|
|
1642
|
+
const candidates = yield* candidatesBetween(context, from, to);
|
|
1643
|
+
if (candidates.length === 0) return yield* Result.fail(unsupported(context, from.id, to.id, direction));
|
|
1644
|
+
if (options.route !== void 0) {
|
|
1645
|
+
const selected = candidates.find((candidate) => candidate.route.id === options.route);
|
|
1646
|
+
if (selected === void 0) return yield* Result.fail(new UnknownRoute({
|
|
1647
|
+
route: options.route,
|
|
1648
|
+
candidates: candidates.map(summaryOf)
|
|
1649
|
+
}));
|
|
1650
|
+
return yield* selected.plan;
|
|
1651
|
+
}
|
|
1652
|
+
const chosen = options.policy === void 0 ? candidates : applyPolicy(options.policy, candidates);
|
|
1653
|
+
const [only] = chosen;
|
|
1654
|
+
if (chosen.length === 1 && only !== void 0) return yield* only.plan;
|
|
1655
|
+
return yield* Result.fail(new AmbiguousRoute({
|
|
1656
|
+
from: from.id,
|
|
1657
|
+
to: to.id,
|
|
1658
|
+
candidates: (chosen.length === 0 ? candidates : chosen).map(summaryOf),
|
|
1659
|
+
policy: options.policy
|
|
1660
|
+
}));
|
|
1661
|
+
});
|
|
1662
|
+
const semverOrder = (self, that) => {
|
|
1663
|
+
const parsedSelf = SemVer.parse(self);
|
|
1664
|
+
const parsedThat = SemVer.parse(that);
|
|
1665
|
+
if (Result.isFailure(parsedSelf) || Result.isFailure(parsedThat)) return void 0;
|
|
1666
|
+
return SemVer.Order(parsedSelf.success, parsedThat.success);
|
|
1667
|
+
};
|
|
1668
|
+
/**
|
|
1669
|
+
* Destinations reachable from a version. Cross-branch destinations are classified by
|
|
1670
|
+
* semver order when both identifiers parse, and listed in both directions otherwise.
|
|
1671
|
+
* Classification is presentation only; it does not change routing.
|
|
1672
|
+
*/
|
|
1673
|
+
const destinations = (context, from, kind) => {
|
|
1674
|
+
const family = kind === "upgrade" ? families[0] : families[1];
|
|
1675
|
+
if (family === void 0) return [];
|
|
1676
|
+
return reachable(context, from.id, family).flatMap((id) => {
|
|
1677
|
+
const version = context.versions.get(id);
|
|
1678
|
+
if (version === void 0) return [];
|
|
1679
|
+
const direction = requestDirection(context, from.id, id);
|
|
1680
|
+
if (direction === "cross-branch") {
|
|
1681
|
+
const order = semverOrder(id, from.id);
|
|
1682
|
+
if (order !== void 0 && (kind === "upgrade" ? order < 0 : order > 0)) return [];
|
|
1683
|
+
} else if (direction !== kind) return [];
|
|
1684
|
+
const { routes, overLimit } = enumerateRoutes(context, from, version);
|
|
1685
|
+
if (!overLimit) return [{
|
|
1686
|
+
version,
|
|
1687
|
+
routes: routes.map((edges) => routeOf(from, edges).id)
|
|
1688
|
+
}];
|
|
1689
|
+
return [{
|
|
1690
|
+
version,
|
|
1691
|
+
routes: Array$1.sort(routes, Order.mapInput(Order.Number, (edges) => routeCost(edges))).map((edges) => routeOf(from, edges).id),
|
|
1692
|
+
overLimit: true
|
|
1693
|
+
}];
|
|
1694
|
+
});
|
|
1695
|
+
};
|
|
1696
|
+
// src/features/planning/history.ts
|
|
1697
|
+
var history_exports = /* @__PURE__ */ __exportAll({
|
|
1698
|
+
AmbiguousRoute: () => AmbiguousRoute,
|
|
1699
|
+
ConflictingRewrites: () => ConflictingRewrites,
|
|
1700
|
+
InvalidHistory: () => InvalidHistory,
|
|
1701
|
+
RewriteCycle: () => RewriteCycle,
|
|
1702
|
+
RewriteLimitExceeded: () => RewriteLimitExceeded,
|
|
1703
|
+
TooManyRoutes: () => TooManyRoutes,
|
|
1704
|
+
UnknownRoute: () => UnknownRoute,
|
|
1705
|
+
UnknownVersion: () => UnknownVersion,
|
|
1706
|
+
UnsupportedRoute: () => UnsupportedRoute,
|
|
1707
|
+
downgrades: () => downgrades,
|
|
1708
|
+
isHistory: () => isHistory,
|
|
1709
|
+
make: () => make$4,
|
|
1710
|
+
resolve: () => resolve,
|
|
1711
|
+
upgrades: () => upgrades,
|
|
1712
|
+
validate: () => validate
|
|
1713
|
+
});
|
|
1714
|
+
/**
|
|
1715
|
+
* Registers declarations. Migrations are registered independently of versions, so
|
|
1716
|
+
* routes can be added later without changing published release definitions.
|
|
1717
|
+
*
|
|
1718
|
+
* `make` does not throw. Use `History.validate` to list declaration issues;
|
|
1719
|
+
* resolution fails with `InvalidHistory` when there are any.
|
|
1720
|
+
*
|
|
1721
|
+
* @since @next
|
|
1722
|
+
*/
|
|
1723
|
+
const make$4 = (declarations) => ({
|
|
1724
|
+
_tag: "History",
|
|
1725
|
+
versions: declarations.versions,
|
|
1726
|
+
migrations: declarations.migrations,
|
|
1727
|
+
rules: declarations.rules ?? [],
|
|
1728
|
+
shortcuts: declarations.shortcuts ?? []
|
|
1729
|
+
});
|
|
1730
|
+
/** @since @next */
|
|
1731
|
+
const isHistory = (value) => Predicate.isTagged(value, "History");
|
|
1732
|
+
const contexts = /* @__PURE__ */ new WeakMap();
|
|
1733
|
+
const prepare = (history) => {
|
|
1734
|
+
const cached = contexts.get(history);
|
|
1735
|
+
if (cached !== void 0) return cached;
|
|
1736
|
+
const prepared = {
|
|
1737
|
+
issues: validate$1(history),
|
|
1738
|
+
context: makeContext(history)
|
|
1739
|
+
};
|
|
1740
|
+
contexts.set(history, prepared);
|
|
1741
|
+
return prepared;
|
|
1742
|
+
};
|
|
1743
|
+
/**
|
|
1744
|
+
* Lists declaration issues in a deterministic order. An empty list means valid.
|
|
1745
|
+
*
|
|
1746
|
+
* @since @next
|
|
1747
|
+
*/
|
|
1748
|
+
const validate = (history) => prepare(history).issues;
|
|
1749
|
+
const valid = (history) => {
|
|
1750
|
+
const { issues, context } = prepare(history);
|
|
1751
|
+
return issues.length === 0 ? Result.succeed(context) : Result.fail(new InvalidHistory({ issues }));
|
|
1752
|
+
};
|
|
1753
|
+
/**
|
|
1754
|
+
* Resolves an optimized plan between two versions. Planning needs no handlers or
|
|
1755
|
+
* execution services, and it never reads or modifies consumer files.
|
|
1756
|
+
*
|
|
1757
|
+
* When several routes connect the versions, resolution fails with `AmbiguousRoute`
|
|
1758
|
+
* unless the `route` option selects one or a route `policy` leaves exactly one.
|
|
1759
|
+
*
|
|
1760
|
+
* @example
|
|
1761
|
+
* ```ts
|
|
1762
|
+
* const result = History.resolve(history, { from: "1.5.3", to: "2.0.0" });
|
|
1763
|
+
* if (Result.isSuccess(result)) console.log(result.success.route.id);
|
|
1764
|
+
* ```
|
|
1765
|
+
*
|
|
1766
|
+
* @since @next
|
|
1767
|
+
*/
|
|
1768
|
+
const resolve = (history, options) => Result.flatMap(valid(history), (context) => resolve$1(context, options));
|
|
1769
|
+
/**
|
|
1770
|
+
* Destinations reachable by upgrade from a version, with their candidate routes.
|
|
1771
|
+
*
|
|
1772
|
+
* @since @next
|
|
1773
|
+
*/
|
|
1774
|
+
const upgrades = (history, options) => reachableFrom(history, options.from, "upgrade");
|
|
1775
|
+
/**
|
|
1776
|
+
* Destinations reachable by downgrade from a version, with their candidate routes.
|
|
1777
|
+
*
|
|
1778
|
+
* @since @next
|
|
1779
|
+
*/
|
|
1780
|
+
const downgrades = (history, options) => reachableFrom(history, options.from, "downgrade");
|
|
1781
|
+
const reachableFrom = (history, from, kind) => Result.flatMap(valid(history), (context) => Result.map(lookup(context, from), (version) => destinations(context, version, kind)));
|
|
1782
|
+
// src/foundation/instruction.ts
|
|
1783
|
+
const isInstruction = (value) => Predicate.isTagged(value, "Instruction");
|
|
1784
|
+
/** Builds prose from a tagged template. Throws on values that are not text or mentions. */
|
|
1785
|
+
const prose = (strings, values, context) => {
|
|
1786
|
+
const literals = dedent(strings);
|
|
1787
|
+
const content = [];
|
|
1788
|
+
const pushText = (text) => {
|
|
1789
|
+
if (text === "") return;
|
|
1790
|
+
const last = content.at(-1);
|
|
1791
|
+
if (last?._tag === "Text") {
|
|
1792
|
+
content[content.length - 1] = {
|
|
1793
|
+
_tag: "Text",
|
|
1794
|
+
text: last.text + text
|
|
1795
|
+
};
|
|
1796
|
+
return;
|
|
1797
|
+
}
|
|
1798
|
+
content.push({
|
|
1799
|
+
_tag: "Text",
|
|
1800
|
+
text
|
|
1801
|
+
});
|
|
1802
|
+
};
|
|
1803
|
+
for (const [index, literal] of literals.entries()) {
|
|
1804
|
+
pushText(literal);
|
|
1805
|
+
if (index >= values.length) continue;
|
|
1806
|
+
const value = values[index];
|
|
1807
|
+
if (isScalar(value)) {
|
|
1808
|
+
pushText(String(value));
|
|
1809
|
+
continue;
|
|
1810
|
+
}
|
|
1811
|
+
if (isRef(value)) {
|
|
1812
|
+
content.push(value);
|
|
1813
|
+
continue;
|
|
1814
|
+
}
|
|
1815
|
+
if (isAction(value)) throw new TypeError(`${context}: action "${value.id}" cannot be interpolated here because it would declare another occurrence. Use Action.ref(action) to mention it.`);
|
|
1816
|
+
throw new TypeError(`${context}: cannot interpolate ${describeValue(value)}.`);
|
|
1817
|
+
}
|
|
1818
|
+
return content;
|
|
1819
|
+
};
|
|
1820
|
+
/**
|
|
1821
|
+
* Associates prose with one action occurrence.
|
|
1822
|
+
*
|
|
1823
|
+
* @example
|
|
1824
|
+
* ```ts
|
|
1825
|
+
* Migration.instruction(renameTimeout)`
|
|
1826
|
+
* Rename the timeout option, keeping its existing value.
|
|
1827
|
+
* `;
|
|
1828
|
+
* ```
|
|
1829
|
+
*
|
|
1830
|
+
* @since @next
|
|
1831
|
+
*/
|
|
1832
|
+
const instruction = (action) => (strings, ...values) => ({
|
|
1833
|
+
_tag: "Instruction",
|
|
1834
|
+
action,
|
|
1835
|
+
content: prose(strings, values, `Instruction for "${action.id}"`)
|
|
1836
|
+
});
|
|
1837
|
+
// src/foundation/migration.ts
|
|
1838
|
+
var migration_exports = /* @__PURE__ */ __exportAll({
|
|
1839
|
+
instruction: () => instruction,
|
|
1840
|
+
isMigration: () => isMigration,
|
|
1841
|
+
make: () => make$3,
|
|
1842
|
+
noop: () => noop,
|
|
1843
|
+
withRevert: () => withRevert
|
|
1844
|
+
});
|
|
1845
|
+
/** @since @next */
|
|
1846
|
+
const isMigration = (value) => Predicate.isTagged(value, "Migration");
|
|
1847
|
+
const isStep = (value) => isAction(value) || isInstruction(value) || isSequence(value) || isParallel(value);
|
|
1848
|
+
const validateStep = (step, context) => {
|
|
1849
|
+
if (isSequence(step)) {
|
|
1850
|
+
for (const child of step.children) checkStep(child, context);
|
|
1851
|
+
return;
|
|
1852
|
+
}
|
|
1853
|
+
if (isParallel(step)) for (const branch of step.branches) checkStep(branch, context);
|
|
1854
|
+
};
|
|
1855
|
+
const checkStep = (value, context) => {
|
|
1856
|
+
if (!isStep(value)) throw new TypeError(`${context}: a group can only contain actions, instructions, and groups.`);
|
|
1857
|
+
validateStep(value, context);
|
|
1858
|
+
};
|
|
1859
|
+
/** Builds content from a tagged template. */
|
|
1860
|
+
const content = (strings, values, context) => {
|
|
1861
|
+
const literals = dedent(strings);
|
|
1862
|
+
const result = [];
|
|
1863
|
+
const pushText = (text) => {
|
|
1864
|
+
if (text === "") return;
|
|
1865
|
+
const last = result.at(-1);
|
|
1866
|
+
if (last?._tag === "Text") {
|
|
1867
|
+
result[result.length - 1] = {
|
|
1868
|
+
_tag: "Text",
|
|
1869
|
+
text: last.text + text
|
|
1870
|
+
};
|
|
1871
|
+
return;
|
|
1872
|
+
}
|
|
1873
|
+
result.push({
|
|
1874
|
+
_tag: "Text",
|
|
1875
|
+
text
|
|
1876
|
+
});
|
|
1877
|
+
};
|
|
1878
|
+
for (const [index, literal] of literals.entries()) {
|
|
1879
|
+
pushText(literal);
|
|
1880
|
+
if (index >= values.length) continue;
|
|
1881
|
+
const value = values[index];
|
|
1882
|
+
if (isScalar(value)) {
|
|
1883
|
+
pushText(String(value));
|
|
1884
|
+
continue;
|
|
1885
|
+
}
|
|
1886
|
+
if (isRef(value)) {
|
|
1887
|
+
result.push(value);
|
|
1888
|
+
continue;
|
|
1889
|
+
}
|
|
1890
|
+
if (isStep(value)) {
|
|
1891
|
+
validateStep(value, context);
|
|
1892
|
+
result.push(value);
|
|
1893
|
+
continue;
|
|
1894
|
+
}
|
|
1895
|
+
throw new TypeError(`${context}: cannot interpolate ${describeValue(value)}.`);
|
|
1896
|
+
}
|
|
1897
|
+
return result;
|
|
1898
|
+
};
|
|
1899
|
+
/**
|
|
1900
|
+
* Declares a migration between exact versions. Interpolated actions, instruction blocks,
|
|
1901
|
+
* and groups declare occurrences; `Action.ref` only mentions an action.
|
|
1902
|
+
*
|
|
1903
|
+
* @example
|
|
1904
|
+
* ```ts
|
|
1905
|
+
* const migrate152To200 = Migration.make("1.5.2-to-2.0.0", {
|
|
1906
|
+
* from: v152,
|
|
1907
|
+
* to: v200,
|
|
1908
|
+
* addresses: [timeoutRenamed],
|
|
1909
|
+
* })`
|
|
1910
|
+
* Update the configuration for export A:
|
|
1911
|
+
*
|
|
1912
|
+
* ${renameExportA}
|
|
1913
|
+
* `;
|
|
1914
|
+
* ```
|
|
1915
|
+
*
|
|
1916
|
+
* @since @next
|
|
1917
|
+
*/
|
|
1918
|
+
const make$3 = (id, options) => (strings, ...values) => ({
|
|
1919
|
+
_tag: "Migration",
|
|
1920
|
+
id,
|
|
1921
|
+
from: options.from,
|
|
1922
|
+
to: options.to,
|
|
1923
|
+
addresses: options.addresses ?? [],
|
|
1924
|
+
noop: false,
|
|
1925
|
+
cost: options.cost,
|
|
1926
|
+
content: content(strings, values, `Migration "${id}"`),
|
|
1927
|
+
revert: void 0
|
|
1928
|
+
});
|
|
1929
|
+
/**
|
|
1930
|
+
* Declares a supported transition that requires no consumer work. It is a directed edge
|
|
1931
|
+
* like any migration. Do not use it to hide missing support for changes that need
|
|
1932
|
+
* instructions.
|
|
1933
|
+
*
|
|
1934
|
+
* @since @next
|
|
1935
|
+
*/
|
|
1936
|
+
const noop = (id, options) => ({
|
|
1937
|
+
_tag: "Migration",
|
|
1938
|
+
id,
|
|
1939
|
+
from: options.from,
|
|
1940
|
+
to: options.to,
|
|
1941
|
+
addresses: options.addresses ?? [],
|
|
1942
|
+
noop: true,
|
|
1943
|
+
cost: options.cost,
|
|
1944
|
+
content: [],
|
|
1945
|
+
revert: options.reversible === true ? [] : void 0
|
|
1946
|
+
});
|
|
1947
|
+
/**
|
|
1948
|
+
* Adds a reverse transition. Register the returned migration in place of the original:
|
|
1949
|
+
* both share the same identifier. On a `Migration.noop`, the reverse is real work: only
|
|
1950
|
+
* the forward side stays zero-work.
|
|
1951
|
+
*
|
|
1952
|
+
* @example
|
|
1953
|
+
* ```ts
|
|
1954
|
+
* const reversible = Migration.withRevert(migrate152To200)`
|
|
1955
|
+
* Restore the previous configuration:
|
|
1956
|
+
*
|
|
1957
|
+
* ${restoreTimeout}
|
|
1958
|
+
* `;
|
|
1959
|
+
* ```
|
|
1960
|
+
*
|
|
1961
|
+
* @since @next
|
|
1962
|
+
*/
|
|
1963
|
+
const withRevert = (migration) => (strings, ...values) => ({
|
|
1964
|
+
...migration,
|
|
1965
|
+
revert: content(strings, values, `Revert of migration "${migration.id}"`)
|
|
1966
|
+
});
|
|
1967
|
+
// src/features/package.ts
|
|
1968
|
+
var package_exports = /* @__PURE__ */ __exportAll({
|
|
1969
|
+
isPackage: () => isPackage,
|
|
1970
|
+
make: () => make$2
|
|
1971
|
+
});
|
|
1972
|
+
/**
|
|
1973
|
+
* Defines the modeled package. It holds no renderer settings, release metadata, or
|
|
1974
|
+
* handlers.
|
|
1975
|
+
*
|
|
1976
|
+
* @example
|
|
1977
|
+
* ```ts
|
|
1978
|
+
* export const widgets = Package.make("@acme/widgets", { history });
|
|
1979
|
+
* ```
|
|
1980
|
+
*
|
|
1981
|
+
* @since @next
|
|
1982
|
+
*/
|
|
1983
|
+
const make$2 = (name, options) => ({
|
|
1984
|
+
_tag: "Package",
|
|
1985
|
+
name,
|
|
1986
|
+
history: options.history
|
|
1987
|
+
});
|
|
1988
|
+
/** @since @next */
|
|
1989
|
+
const isPackage = (value) => Predicate.isTagged(value, "Package");
|
|
1990
|
+
// src/foundation/plan.ts
|
|
1991
|
+
var plan_exports = /* @__PURE__ */ __exportAll({
|
|
1992
|
+
isEmpty: () => isEmpty,
|
|
1993
|
+
withRuntimeCoverage: () => withRuntimeCoverage
|
|
1994
|
+
});
|
|
1995
|
+
/**
|
|
1996
|
+
* Whether the plan has no action occurrences. An empty plan is valid.
|
|
1997
|
+
*
|
|
1998
|
+
* @since @next
|
|
1999
|
+
*/
|
|
2000
|
+
const isEmpty = (plan) => plan.occurrences.length === 0;
|
|
2001
|
+
/**
|
|
2002
|
+
* Marks a plan for run-time coverage checks only. Use it for dynamically selected or
|
|
2003
|
+
* loaded plans whose action union is not known statically. Executors still reject
|
|
2004
|
+
* missing handlers before any action body runs.
|
|
2005
|
+
*
|
|
2006
|
+
* @since @next
|
|
2007
|
+
*/
|
|
2008
|
+
const withRuntimeCoverage = (plan) => ({
|
|
2009
|
+
...plan,
|
|
2010
|
+
coverage: "runtime"
|
|
2011
|
+
});
|
|
2012
|
+
// src/foundation/rule.ts
|
|
2013
|
+
var rule_exports = /* @__PURE__ */ __exportAll({
|
|
2014
|
+
isRule: () => isRule,
|
|
2015
|
+
replace: () => replace
|
|
2016
|
+
});
|
|
2017
|
+
/**
|
|
2018
|
+
* Declares a replacement. Cancellation, composition, and supersession all use it.
|
|
2019
|
+
*
|
|
2020
|
+
* The author asserts that the replacement is equivalent to the matched occurrences.
|
|
2021
|
+
* deltafold checks the rule's structure and where it applies, but it cannot prove the
|
|
2022
|
+
* equivalence.
|
|
2023
|
+
*
|
|
2024
|
+
* Ordinary rules match consecutive occurrences inside one sequential branch, including
|
|
2025
|
+
* across migration boundaries of a route. They never match across parallel branches.
|
|
2026
|
+
*
|
|
2027
|
+
* @example
|
|
2028
|
+
* ```ts
|
|
2029
|
+
* const cancelRoundTrip = Rule.replace("cancel-timeout-round-trip", {
|
|
2030
|
+
* match: [renameTimeout, restoreTimeout],
|
|
2031
|
+
* replacement: [],
|
|
2032
|
+
* });
|
|
2033
|
+
* const combineRenames = Rule.replace("combine-renames", {
|
|
2034
|
+
* match: [renameAtoB, renameBtoC],
|
|
2035
|
+
* replacement: [renameAtoC],
|
|
2036
|
+
* });
|
|
2037
|
+
* ```
|
|
2038
|
+
*
|
|
2039
|
+
* @since @next
|
|
2040
|
+
*/
|
|
2041
|
+
const replace = (id, options) => ({
|
|
2042
|
+
_tag: "Rule",
|
|
2043
|
+
id,
|
|
2044
|
+
match: options.match,
|
|
2045
|
+
replacement: options.replacement
|
|
2046
|
+
});
|
|
2047
|
+
/** @since @next */
|
|
2048
|
+
const isRule = (value) => Predicate.isTagged(value, "Rule");
|
|
2049
|
+
// src/foundation/shortcut.ts
|
|
2050
|
+
var shortcut_exports = /* @__PURE__ */ __exportAll({
|
|
2051
|
+
isShortcut: () => isShortcut,
|
|
2052
|
+
make: () => make$1
|
|
2053
|
+
});
|
|
2054
|
+
/**
|
|
2055
|
+
* Declares a shortcut. The author asserts that its migrations are equivalent to the
|
|
2056
|
+
* canonical route it replaces; deltafold does not prove it.
|
|
2057
|
+
*
|
|
2058
|
+
* @example
|
|
2059
|
+
* ```ts
|
|
2060
|
+
* const fastUpgrade = Shortcut.make("fast-upgrade", {
|
|
2061
|
+
* from: v201,
|
|
2062
|
+
* to: v208,
|
|
2063
|
+
* replaces: [migrate202, migrate203, migrate204],
|
|
2064
|
+
* migrations: [migrateDirectlyTo208],
|
|
2065
|
+
* });
|
|
2066
|
+
* ```
|
|
2067
|
+
*
|
|
2068
|
+
* @since @next
|
|
2069
|
+
*/
|
|
2070
|
+
const make$1 = (id, options) => ({
|
|
2071
|
+
_tag: "Shortcut",
|
|
2072
|
+
id,
|
|
2073
|
+
from: options.from,
|
|
2074
|
+
to: options.to,
|
|
2075
|
+
replaces: options.replaces,
|
|
2076
|
+
migrations: options.migrations,
|
|
2077
|
+
cost: options.cost
|
|
2078
|
+
});
|
|
2079
|
+
/** @since @next */
|
|
2080
|
+
const isShortcut = (value) => Predicate.isTagged(value, "Shortcut");
|
|
2081
|
+
// src/foundation/version.ts
|
|
2082
|
+
var version_exports = /* @__PURE__ */ __exportAll({
|
|
2083
|
+
isVersion: () => isVersion,
|
|
2084
|
+
make: () => make,
|
|
2085
|
+
withMetadata: () => withMetadata
|
|
2086
|
+
});
|
|
2087
|
+
/**
|
|
2088
|
+
* Declares a version.
|
|
2089
|
+
*
|
|
2090
|
+
* @example
|
|
2091
|
+
* ```ts
|
|
2092
|
+
* const v0 = Version.make("0.0.0", { changes: [] });
|
|
2093
|
+
* const v100 = Version.make("1.0.0", { previous: v0, changes: [timeoutRenamed] });
|
|
2094
|
+
* ```
|
|
2095
|
+
*
|
|
2096
|
+
* @since @next
|
|
2097
|
+
*/
|
|
2098
|
+
const make = (id, options) => ({
|
|
2099
|
+
_tag: "Version",
|
|
2100
|
+
id,
|
|
2101
|
+
previous: options.previous,
|
|
2102
|
+
changes: options.changes
|
|
2103
|
+
});
|
|
2104
|
+
/** @since @next */
|
|
2105
|
+
const isVersion = (value) => Predicate.isTagged(value, "Version");
|
|
2106
|
+
/**
|
|
2107
|
+
* Annotates a version with release metadata.
|
|
2108
|
+
*
|
|
2109
|
+
* @example
|
|
2110
|
+
* ```ts
|
|
2111
|
+
* export const release200 = Version.withMetadata(v200, {
|
|
2112
|
+
* releasedAt: "2026-11-06T12:00:00Z",
|
|
2113
|
+
* commit: "abc123",
|
|
2114
|
+
* });
|
|
2115
|
+
* ```
|
|
2116
|
+
*
|
|
2117
|
+
* @since @next
|
|
2118
|
+
*/
|
|
2119
|
+
const withMetadata = (version, metadata) => ({
|
|
2120
|
+
_tag: "Release",
|
|
2121
|
+
version,
|
|
2122
|
+
metadata
|
|
2123
|
+
});
|
|
2124
|
+
export { action_exports as Action, change_exports as Change, executor_exports as Executor, handler_exports as Handler, history_exports as History, migration_exports as Migration, package_exports as Package, plan_exports as Plan, rule_exports as Rule, shortcut_exports as Shortcut, version_exports as Version };
|
|
2125
|
+
|
|
2126
|
+
//# sourceMappingURL=index.js.map
|