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.
Files changed (57) hide show
  1. package/README.md +278 -4
  2. package/dist/changelog.d.ts +116 -0
  3. package/dist/changelog.d.ts.map +1 -0
  4. package/dist/changelog.js +304 -0
  5. package/dist/changelog.js.map +1 -0
  6. package/dist/document-BZcZo9nr.js +171 -0
  7. package/dist/document-BZcZo9nr.js.map +1 -0
  8. package/dist/document-DWHsgywm.d.ts +257 -0
  9. package/dist/document-DWHsgywm.d.ts.map +1 -0
  10. package/dist/html.d.ts +8 -0
  11. package/dist/html.d.ts.map +1 -0
  12. package/dist/html.js +13 -0
  13. package/dist/html.js.map +1 -0
  14. package/dist/index.d.ts +517 -1
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +2126 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/markdown.d.ts +8 -0
  19. package/dist/markdown.d.ts.map +1 -0
  20. package/dist/markdown.js +16 -0
  21. package/dist/markdown.js.map +1 -0
  22. package/dist/mdast-DiRsYwPU.js +108 -0
  23. package/dist/mdast-DiRsYwPU.js.map +1 -0
  24. package/dist/package-BfBYnaCz.d.ts +1125 -0
  25. package/dist/package-BfBYnaCz.d.ts.map +1 -0
  26. package/dist/rolldown-runtime-D8jQpDG7.js +12 -0
  27. package/package.json +13 -1
  28. package/src/features/documents/changelog.ts +450 -0
  29. package/src/features/documents/document.ts +357 -0
  30. package/src/features/documents/index.ts +4 -0
  31. package/src/features/documents/public-document.ts +31 -0
  32. package/src/features/documents/sections.ts +44 -0
  33. package/src/features/execution/executor.ts +968 -0
  34. package/src/features/execution/handler.ts +485 -0
  35. package/src/features/execution/settlement.ts +52 -0
  36. package/src/features/package.ts +32 -0
  37. package/src/features/planning/guide.ts +56 -0
  38. package/src/features/planning/history.ts +174 -0
  39. package/src/features/planning/identifiers.ts +43 -0
  40. package/src/features/planning/resolve-error.ts +201 -0
  41. package/src/features/planning/resolve.ts +724 -0
  42. package/src/features/planning/rewrite.ts +331 -0
  43. package/src/features/planning/validate.ts +388 -0
  44. package/src/foundation/action.ts +188 -0
  45. package/src/foundation/change.ts +147 -0
  46. package/src/foundation/instruction.ts +110 -0
  47. package/src/foundation/migration.ts +236 -0
  48. package/src/foundation/plan.ts +245 -0
  49. package/src/foundation/rule.ts +74 -0
  50. package/src/foundation/shortcut.ts +79 -0
  51. package/src/foundation/template.ts +28 -0
  52. package/src/foundation/version.ts +78 -0
  53. package/src/index.ts +22 -11
  54. package/src/renderers/html.ts +12 -0
  55. package/src/renderers/markdown.ts +12 -0
  56. package/src/renderers/mdast.ts +120 -0
  57. package/src/types.ts +2 -0
@@ -0,0 +1,331 @@
1
+ import { Array as Arr, Order } from "effect";
2
+
3
+ import type * as Action from "../../foundation/action.ts";
4
+ import type * as Migration from "../../foundation/migration.ts";
5
+ import type { Occurrence } from "../../foundation/plan.ts";
6
+ import type * as Rule from "../../foundation/rule.ts";
7
+ import { replacementOccurrenceId } from "./identifiers.ts";
8
+
9
+ /**
10
+ * A top-level element of a route's sequential spine, or of one parallel branch. `step`
11
+ * is the index of the plan step that owns the element.
12
+ */
13
+ export type Item =
14
+ | { readonly _tag: "Occ"; readonly occurrence: Occurrence; readonly step: number }
15
+ | {
16
+ readonly _tag: "Par";
17
+ readonly branches: ReadonlyArray<ReadonlyArray<Item>>;
18
+ readonly concurrency: Action.Concurrency;
19
+ readonly step: number;
20
+ }
21
+ /** A text-only migration: manual work that rules must not match around. */
22
+ | { readonly _tag: "Barrier"; readonly step: number };
23
+
24
+ export type Application = {
25
+ readonly rule: Rule.Any;
26
+ readonly removed: ReadonlyArray<Occurrence>;
27
+ readonly produced: ReadonlyArray<Occurrence>;
28
+ };
29
+
30
+ export type Outcome = {
31
+ readonly items: ReadonlyArray<Item>;
32
+ readonly trace: ReadonlyArray<Application>;
33
+ };
34
+
35
+ export type Failure =
36
+ | {
37
+ readonly _tag: "RewriteCycle";
38
+ /** Rules applied around the cycle, in order. */
39
+ readonly rules: ReadonlyArray<string>;
40
+ /** The action sequence that repeats. */
41
+ readonly actions: string;
42
+ }
43
+ | {
44
+ readonly _tag: "ConflictingRewrites";
45
+ readonly outcomes: ReadonlyArray<{
46
+ readonly actions: string;
47
+ readonly rules: ReadonlyArray<string>;
48
+ }>;
49
+ }
50
+ | {
51
+ readonly _tag: "RewriteLimitExceeded";
52
+ readonly limit: "states" | "depth";
53
+ readonly value: number;
54
+ };
55
+
56
+ class RewriteFailure {
57
+ readonly failure: Failure;
58
+
59
+ constructor(failure: Failure) {
60
+ this.failure = failure;
61
+ }
62
+ }
63
+
64
+ /** The largest number of distinct states explored while normalizing one sequence. */
65
+ export const stateLimit = 10_000;
66
+
67
+ /** The largest number of rule applications in one chain of rewrites. */
68
+ export const depthLimit = 256;
69
+
70
+ const occurrencesOf = (items: ReadonlyArray<Item>): ReadonlyArray<Occurrence> =>
71
+ items.flatMap((item) => {
72
+ if (item._tag === "Occ") return [item.occurrence];
73
+ if (item._tag === "Par") return item.branches.flatMap(occurrencesOf);
74
+
75
+ return [];
76
+ });
77
+
78
+ export { occurrencesOf };
79
+
80
+ const groupKey = (branches: ReadonlyArray<string>) =>
81
+ `{${Arr.sort(
82
+ branches.map((branch) => `[${branch}]`),
83
+ Order.String,
84
+ ).join("|")}}`;
85
+
86
+ /** A key that identifies a sequence by its action structure. Branch order is ignored. */
87
+ export const keyOf = (items: ReadonlyArray<Item>): string =>
88
+ items
89
+ .map((item) => {
90
+ if (item._tag === "Occ") return JSON.stringify(item.occurrence.action.id);
91
+ if (item._tag === "Barrier") return "#";
92
+
93
+ return groupKey(item.branches.map(keyOf));
94
+ })
95
+ .join(",");
96
+
97
+ /** A key that identifies declared steps by their action structure. Branch order is ignored. */
98
+ export const stepKey = (steps: ReadonlyArray<Action.Step>): string =>
99
+ steps
100
+ .map((step) => {
101
+ switch (step._tag) {
102
+ case "Action":
103
+ return JSON.stringify(step.id);
104
+ case "Instruction":
105
+ return JSON.stringify(step.action.id);
106
+ case "Sequence":
107
+ return stepKey(step.children);
108
+ }
109
+ const branches = step.branches.map((branch) => stepKey([branch]));
110
+ if (branches.length === 1) return branches[0];
111
+
112
+ return groupKey(branches);
113
+ })
114
+ .filter((key) => key !== "")
115
+ .join(",");
116
+
117
+ export type MakeOccurrence = (
118
+ action: Action.Any,
119
+ instruction: Migration.Instruction | undefined,
120
+ ) => Occurrence;
121
+
122
+ /** Converts declared steps to items. Sequences flatten; single-branch groups flatten. */
123
+ export const itemsOf = (
124
+ steps: ReadonlyArray<Action.Step>,
125
+ make: MakeOccurrence,
126
+ step: number,
127
+ ): ReadonlyArray<Item> =>
128
+ steps.flatMap((declared): ReadonlyArray<Item> => {
129
+ switch (declared._tag) {
130
+ case "Action":
131
+ return [{ _tag: "Occ", occurrence: make(declared, undefined), step }];
132
+ case "Instruction":
133
+ return [{ _tag: "Occ", occurrence: make(declared.action, declared), step }];
134
+ case "Sequence":
135
+ return itemsOf(declared.children, make, step);
136
+ }
137
+
138
+ return parallelOf(
139
+ declared.branches.map((branch) => itemsOf([branch], make, step)),
140
+ declared.concurrency,
141
+ step,
142
+ );
143
+ });
144
+
145
+ /** Drops empty branches and flattens a group that has a single branch left. */
146
+ const parallelOf = (
147
+ branches: ReadonlyArray<ReadonlyArray<Item>>,
148
+ concurrency: Action.Concurrency,
149
+ step: number,
150
+ ): ReadonlyArray<Item> => {
151
+ const nonEmpty = branches.filter((branch) => branch.length > 0);
152
+ if (nonEmpty.length === 0) return [];
153
+ if (nonEmpty.length === 1) return nonEmpty[0] ?? [];
154
+
155
+ return [{ _tag: "Par", branches: nonEmpty, concurrency, step }];
156
+ };
157
+
158
+ const matchesAt = (
159
+ pattern: ReadonlyArray<Rule.Pattern>,
160
+ items: ReadonlyArray<Item>,
161
+ position: number,
162
+ ): boolean =>
163
+ pattern.every((expected, offset) => {
164
+ const item = items[position + offset];
165
+ if (item === undefined) return false;
166
+ if (expected._tag === "Action") {
167
+ return item._tag === "Occ" && item.occurrence.action.id === expected.id;
168
+ }
169
+
170
+ return item._tag === "Par" && keyOf([item]) === stepKey([expected]);
171
+ });
172
+
173
+ type Transition = {
174
+ readonly next: ReadonlyArray<Item>;
175
+ readonly application: Application;
176
+ };
177
+
178
+ /** Replaces the match of a rule at a position with occurrences that record their provenance. */
179
+ const replace = (rule: Rule.Any, items: ReadonlyArray<Item>, position: number): Transition => {
180
+ const matched = items.slice(position, position + rule.match.length);
181
+ const removed = occurrencesOf(matched);
182
+ const first = removed[0];
183
+ // A non-empty match always covers at least one occurrence.
184
+ if (first === undefined) throw new Error(`Rule "${rule.id}" matched no occurrences`);
185
+ const step = matched[0]?.step ?? 0;
186
+ const produced: Array<Occurrence> = [];
187
+ const make: MakeOccurrence = (action, instruction) => {
188
+ const occurrence: Occurrence = {
189
+ _tag: "Occurrence",
190
+ id: replacementOccurrenceId(rule, removed, action),
191
+ action,
192
+ migration: first.migration,
193
+ direction: first.direction,
194
+ instruction,
195
+ origin: { _tag: "Replacement", rule, replaces: removed },
196
+ };
197
+ produced.push(occurrence);
198
+
199
+ return occurrence;
200
+ };
201
+ const next = [
202
+ ...items.slice(0, position),
203
+ ...itemsOf(rule.replacement, make, step),
204
+ ...items.slice(position + rule.match.length),
205
+ ];
206
+
207
+ return { next, application: { rule, removed, produced } };
208
+ };
209
+
210
+ const makeNormalizer = (rules: ReadonlyArray<Rule.Any>) => {
211
+ const ordered = Arr.sort(
212
+ rules,
213
+ Order.mapInput(Order.String, (rule: Rule.Any) => rule.id),
214
+ );
215
+
216
+ /**
217
+ * Every single rule application available in a sequence: at any position of the
218
+ * sequence itself, or inside any branch of its groups. Branch-local and whole-group
219
+ * rewrites compete as transitions from the same state.
220
+ */
221
+ const transitions = (items: ReadonlyArray<Item>): ReadonlyArray<Transition> => {
222
+ const found: Array<Transition> = [];
223
+ for (let position = 0; position < items.length; position++) {
224
+ for (const rule of ordered) {
225
+ if (rule.match.length > 0 && matchesAt(rule.match, items, position)) {
226
+ found.push(replace(rule, items, position));
227
+ }
228
+ }
229
+
230
+ const item = items[position];
231
+ if (item?._tag !== "Par") continue;
232
+ for (const [index, branch] of item.branches.entries()) {
233
+ for (const inner of transitions(branch)) {
234
+ const branches = item.branches.map((other, otherIndex) =>
235
+ otherIndex === index ? inner.next : other,
236
+ );
237
+ found.push({
238
+ next: [
239
+ ...items.slice(0, position),
240
+ ...parallelOf(branches, item.concurrency, item.step),
241
+ ...items.slice(position + 1),
242
+ ],
243
+ application: inner.application,
244
+ });
245
+ }
246
+ }
247
+ }
248
+
249
+ return found;
250
+ };
251
+
252
+ /**
253
+ * Explores every rewrite order of the whole sequence, nested groups included, under
254
+ * one shared state and depth budget.
255
+ */
256
+ const normalize = (input: ReadonlyArray<Item>): Outcome => {
257
+ const seen = new Set<string>();
258
+ const onPath = new Map<string, number>();
259
+ const normals = new Map<string, Outcome>();
260
+
261
+ const visit = (items: ReadonlyArray<Item>, trace: ReadonlyArray<Application>): void => {
262
+ const key = keyOf(items);
263
+ const entered = onPath.get(key);
264
+ if (entered !== undefined) {
265
+ throw new RewriteFailure({
266
+ _tag: "RewriteCycle",
267
+ rules: trace.slice(entered).map((application) => application.rule.id),
268
+ actions: key,
269
+ });
270
+ }
271
+ if (seen.has(key)) return;
272
+
273
+ seen.add(key);
274
+ if (seen.size > stateLimit) {
275
+ throw new RewriteFailure({
276
+ _tag: "RewriteLimitExceeded",
277
+ limit: "states",
278
+ value: stateLimit,
279
+ });
280
+ }
281
+ if (trace.length > depthLimit) {
282
+ throw new RewriteFailure({
283
+ _tag: "RewriteLimitExceeded",
284
+ limit: "depth",
285
+ value: depthLimit,
286
+ });
287
+ }
288
+ onPath.set(key, trace.length);
289
+
290
+ const available = transitions(items);
291
+ for (const { next, application } of available) visit(next, [...trace, application]);
292
+ if (available.length === 0) normals.set(key, { items, trace });
293
+
294
+ onPath.delete(key);
295
+ };
296
+
297
+ visit(input, []);
298
+
299
+ if (normals.size > 1) {
300
+ throw new RewriteFailure({
301
+ _tag: "ConflictingRewrites",
302
+ outcomes: [...normals.entries()].map(([actions, outcome]) => ({
303
+ actions,
304
+ rules: outcome.trace.map((application) => application.rule.id),
305
+ })),
306
+ });
307
+ }
308
+
309
+ const [outcome] = normals.values();
310
+
311
+ return outcome ?? { items: input, trace: [] };
312
+ };
313
+
314
+ return { normalize };
315
+ };
316
+
317
+ /** Applies rules to a sequence until no rule matches. */
318
+ export const rewrite = (
319
+ items: ReadonlyArray<Item>,
320
+ rules: ReadonlyArray<Rule.Any>,
321
+ ):
322
+ | { readonly _tag: "Success"; readonly outcome: Outcome }
323
+ | { readonly _tag: "Failure"; readonly failure: Failure } => {
324
+ try {
325
+ return { _tag: "Success", outcome: makeNormalizer(rules).normalize(items) };
326
+ } catch (error) {
327
+ if (error instanceof RewriteFailure) return { _tag: "Failure", failure: error.failure };
328
+
329
+ throw error;
330
+ }
331
+ };