@smthrs/plan 0.0.0-stage → 1.0.0-rc.3
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/CHANGELOG.md +183 -0
- package/LICENSE +21 -0
- package/README.md +228 -2
- package/dist/cjs/CachePolicy.d.ts +131 -0
- package/dist/cjs/CachePolicy.d.ts.map +1 -0
- package/dist/cjs/CachePolicy.js +50 -0
- package/dist/cjs/CachePolicy.js.map +7 -0
- package/dist/cjs/Effects.d.ts +240 -0
- package/dist/cjs/Effects.d.ts.map +1 -0
- package/dist/cjs/Effects.js +76 -0
- package/dist/cjs/Effects.js.map +7 -0
- package/dist/cjs/FileSet.d.ts +288 -0
- package/dist/cjs/FileSet.d.ts.map +1 -0
- package/dist/cjs/FileSet.js +191 -0
- package/dist/cjs/FileSet.js.map +7 -0
- package/dist/cjs/GraphBuildError.d.ts +122 -0
- package/dist/cjs/GraphBuildError.d.ts.map +1 -0
- package/dist/cjs/GraphBuildError.js +71 -0
- package/dist/cjs/GraphBuildError.js.map +7 -0
- package/dist/cjs/KeyMaterial.d.ts +102 -0
- package/dist/cjs/KeyMaterial.d.ts.map +1 -0
- package/dist/cjs/KeyMaterial.js +64 -0
- package/dist/cjs/KeyMaterial.js.map +7 -0
- package/dist/cjs/Node.d.ts +606 -0
- package/dist/cjs/Node.d.ts.map +1 -0
- package/dist/cjs/Node.js +207 -0
- package/dist/cjs/Node.js.map +7 -0
- package/dist/cjs/Placement.d.ts +117 -0
- package/dist/cjs/Placement.d.ts.map +1 -0
- package/dist/cjs/Placement.js +46 -0
- package/dist/cjs/Placement.js.map +7 -0
- package/dist/cjs/Plan.d.ts +440 -0
- package/dist/cjs/Plan.d.ts.map +1 -0
- package/dist/cjs/Plan.js +508 -0
- package/dist/cjs/Plan.js.map +7 -0
- package/dist/cjs/PlanDiff.d.ts +58 -0
- package/dist/cjs/PlanDiff.d.ts.map +1 -0
- package/dist/cjs/PlanDiff.js +124 -0
- package/dist/cjs/PlanDiff.js.map +7 -0
- package/dist/cjs/Planned.d.ts +121 -0
- package/dist/cjs/Planned.d.ts.map +1 -0
- package/dist/cjs/Planned.js +72 -0
- package/dist/cjs/Planned.js.map +7 -0
- package/dist/cjs/Repetition.d.ts +31 -0
- package/dist/cjs/Repetition.d.ts.map +1 -0
- package/dist/cjs/Repetition.js +36 -0
- package/dist/cjs/Repetition.js.map +7 -0
- package/dist/cjs/Scheduling.d.ts +89 -0
- package/dist/cjs/Scheduling.d.ts.map +1 -0
- package/dist/cjs/Scheduling.js +76 -0
- package/dist/cjs/Scheduling.js.map +7 -0
- package/dist/cjs/StepKey.d.ts +333 -0
- package/dist/cjs/StepKey.d.ts.map +1 -0
- package/dist/cjs/StepKey.js +311 -0
- package/dist/cjs/StepKey.js.map +7 -0
- package/dist/cjs/index.d.ts +91 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +59 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/cjs/internal/ConflictAnnotation.d.ts +59 -0
- package/dist/cjs/internal/ConflictAnnotation.d.ts.map +1 -0
- package/dist/cjs/internal/ConflictAnnotation.js +247 -0
- package/dist/cjs/internal/ConflictAnnotation.js.map +7 -0
- package/dist/cjs/internal/EffectCandidates.d.ts +19 -0
- package/dist/cjs/internal/EffectCandidates.d.ts.map +1 -0
- package/dist/cjs/internal/EffectCandidates.js +92 -0
- package/dist/cjs/internal/EffectCandidates.js.map +7 -0
- package/dist/cjs/internal/JsonMirror.d.ts +34 -0
- package/dist/cjs/internal/JsonMirror.d.ts.map +1 -0
- package/dist/cjs/internal/JsonMirror.js +195 -0
- package/dist/cjs/internal/JsonMirror.js.map +7 -0
- package/dist/cjs/internal/effects.d.ts +245 -0
- package/dist/cjs/internal/effects.d.ts.map +1 -0
- package/dist/cjs/internal/effects.js +290 -0
- package/dist/cjs/internal/effects.js.map +7 -0
- package/dist/cjs/internal/node.d.ts +526 -0
- package/dist/cjs/internal/node.d.ts.map +1 -0
- package/dist/cjs/internal/node.js +312 -0
- package/dist/cjs/internal/node.js.map +7 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/test/PlanFixtures.d.ts +127 -0
- package/dist/cjs/test/PlanFixtures.d.ts.map +1 -0
- package/dist/cjs/test/PlanFixtures.js +59 -0
- package/dist/cjs/test/PlanFixtures.js.map +7 -0
- package/dist/esm/CachePolicy.d.ts +131 -0
- package/dist/esm/CachePolicy.d.ts.map +1 -0
- package/dist/esm/CachePolicy.js +117 -0
- package/dist/esm/CachePolicy.js.map +1 -0
- package/dist/esm/Effects.d.ts +240 -0
- package/dist/esm/Effects.d.ts.map +1 -0
- package/dist/esm/Effects.js +167 -0
- package/dist/esm/Effects.js.map +1 -0
- package/dist/esm/FileSet.d.ts +288 -0
- package/dist/esm/FileSet.d.ts.map +1 -0
- package/dist/esm/FileSet.js +324 -0
- package/dist/esm/FileSet.js.map +1 -0
- package/dist/esm/GraphBuildError.d.ts +122 -0
- package/dist/esm/GraphBuildError.d.ts.map +1 -0
- package/dist/esm/GraphBuildError.js +146 -0
- package/dist/esm/GraphBuildError.js.map +1 -0
- package/dist/esm/KeyMaterial.d.ts +102 -0
- package/dist/esm/KeyMaterial.d.ts.map +1 -0
- package/dist/esm/KeyMaterial.js +85 -0
- package/dist/esm/KeyMaterial.js.map +1 -0
- package/dist/esm/Node.d.ts +606 -0
- package/dist/esm/Node.d.ts.map +1 -0
- package/dist/esm/Node.js +521 -0
- package/dist/esm/Node.js.map +1 -0
- package/dist/esm/Placement.d.ts +117 -0
- package/dist/esm/Placement.d.ts.map +1 -0
- package/dist/esm/Placement.js +65 -0
- package/dist/esm/Placement.js.map +1 -0
- package/dist/esm/Plan.d.ts +440 -0
- package/dist/esm/Plan.d.ts.map +1 -0
- package/dist/esm/Plan.js +673 -0
- package/dist/esm/Plan.js.map +1 -0
- package/dist/esm/PlanDiff.d.ts +58 -0
- package/dist/esm/PlanDiff.d.ts.map +1 -0
- package/dist/esm/PlanDiff.js +159 -0
- package/dist/esm/PlanDiff.js.map +1 -0
- package/dist/esm/Planned.d.ts +121 -0
- package/dist/esm/Planned.d.ts.map +1 -0
- package/dist/esm/Planned.js +147 -0
- package/dist/esm/Planned.js.map +1 -0
- package/dist/esm/Repetition.d.ts +31 -0
- package/dist/esm/Repetition.d.ts.map +1 -0
- package/dist/esm/Repetition.js +24 -0
- package/dist/esm/Repetition.js.map +1 -0
- package/dist/esm/Scheduling.d.ts +89 -0
- package/dist/esm/Scheduling.d.ts.map +1 -0
- package/dist/esm/Scheduling.js +82 -0
- package/dist/esm/Scheduling.js.map +1 -0
- package/dist/esm/StepKey.d.ts +333 -0
- package/dist/esm/StepKey.d.ts.map +1 -0
- package/dist/esm/StepKey.js +478 -0
- package/dist/esm/StepKey.js.map +1 -0
- package/dist/esm/index.d.ts +91 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +91 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/internal/ConflictAnnotation.d.ts +59 -0
- package/dist/esm/internal/ConflictAnnotation.d.ts.map +1 -0
- package/dist/esm/internal/ConflictAnnotation.js +332 -0
- package/dist/esm/internal/ConflictAnnotation.js.map +1 -0
- package/dist/esm/internal/EffectCandidates.d.ts +19 -0
- package/dist/esm/internal/EffectCandidates.d.ts.map +1 -0
- package/dist/esm/internal/EffectCandidates.js +86 -0
- package/dist/esm/internal/EffectCandidates.js.map +1 -0
- package/dist/esm/internal/JsonMirror.d.ts +34 -0
- package/dist/esm/internal/JsonMirror.d.ts.map +1 -0
- package/dist/esm/internal/JsonMirror.js +204 -0
- package/dist/esm/internal/JsonMirror.js.map +1 -0
- package/dist/esm/internal/effects.d.ts +245 -0
- package/dist/esm/internal/effects.d.ts.map +1 -0
- package/dist/esm/internal/effects.js +459 -0
- package/dist/esm/internal/effects.js.map +1 -0
- package/dist/esm/internal/node.d.ts +526 -0
- package/dist/esm/internal/node.d.ts.map +1 -0
- package/dist/esm/internal/node.js +564 -0
- package/dist/esm/internal/node.js.map +1 -0
- package/dist/esm/test/PlanFixtures.d.ts +127 -0
- package/dist/esm/test/PlanFixtures.d.ts.map +1 -0
- package/dist/esm/test/PlanFixtures.js +60 -0
- package/dist/esm/test/PlanFixtures.js.map +1 -0
- package/docs/README.md +165 -0
- package/docs/api.md +1475 -0
- package/docs/concepts/authoring-ast.md +203 -0
- package/docs/concepts/effects-and-conflicts.md +135 -0
- package/docs/concepts/plan-value.md +168 -0
- package/docs/concepts/step-keys.md +179 -0
- package/docs/guides/author-a-node-graph.md +182 -0
- package/docs/guides/compile-a-plan.md +146 -0
- package/docs/guides/declare-file-effects.md +143 -0
- package/docs/guides/diff-two-plans.md +101 -0
- package/docs/installation.md +71 -0
- package/docs/quickstart.md +128 -0
- package/docs/testing.md +73 -0
- package/docs/troubleshooting.md +314 -0
- package/package.json +254 -3
- package/src/CachePolicy.ts +143 -0
- package/src/Effects.ts +284 -0
- package/src/FileSet.ts +411 -0
- package/src/GraphBuildError.ts +158 -0
- package/src/KeyMaterial.ts +106 -0
- package/src/Node.ts +871 -0
- package/src/Placement.ts +99 -0
- package/src/Plan.ts +868 -0
- package/src/PlanDiff.ts +199 -0
- package/src/Planned.ts +208 -0
- package/src/Repetition.ts +33 -0
- package/src/Scheduling.ts +154 -0
- package/src/StepKey.ts +710 -0
- package/src/index.ts +103 -0
- package/src/internal/ConflictAnnotation.ts +374 -0
- package/src/internal/EffectCandidates.ts +89 -0
- package/src/internal/JsonMirror.ts +255 -0
- package/src/internal/effects.ts +543 -0
- package/src/internal/node.ts +858 -0
- package/src/test/PlanFixtures.ts +76 -0
package/src/Plan.ts
ADDED
|
@@ -0,0 +1,868 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The plan: a node graph with every step key computed, inert until run.
|
|
3
|
+
*
|
|
4
|
+
* The way a plan is produced is a law: **planning performs no I/O**.
|
|
5
|
+
* Everything in this module is therefore a pure function of declarations plus
|
|
6
|
+
* the injected `Crypto` service: no filesystem, no clock, no network. A node's
|
|
7
|
+
* key is a function of what it consumes, so an edited declaration re-keys that
|
|
8
|
+
* node and everything downstream of it, and nothing else. That is the entire
|
|
9
|
+
* invalidation mechanism. Invalidation is re-keying, which **rejects**
|
|
10
|
+
* Skyframe's reverse-dependency index and invalidating node visitor outright
|
|
11
|
+
* because content addressing subsumes them. Key invalidation never retains a
|
|
12
|
+
* reverse-dependency index; annotation uses only a temporary reachability cache.
|
|
13
|
+
*
|
|
14
|
+
* Growth is append-only: {@link append} adds a pre-keyed subgraph to the same
|
|
15
|
+
* plan and never rewrites a node already in it.
|
|
16
|
+
*
|
|
17
|
+
* @since 0.1.0
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { DerivedKey, StoredKey } from "@smthrs/keys"
|
|
21
|
+
import type * as Crypto from "effect/Crypto"
|
|
22
|
+
import * as Effect from "effect/Effect"
|
|
23
|
+
import * as Inspectable from "effect/Inspectable"
|
|
24
|
+
import * as Schema from "effect/Schema"
|
|
25
|
+
import * as Effects from "./Effects.ts"
|
|
26
|
+
import * as FileSet from "./FileSet.ts"
|
|
27
|
+
import { GraphBuildError } from "./GraphBuildError.ts"
|
|
28
|
+
import * as Conflicts from "./internal/ConflictAnnotation.ts"
|
|
29
|
+
import { jsonMirror } from "./internal/JsonMirror.ts"
|
|
30
|
+
import * as KeyMaterial from "./KeyMaterial.ts"
|
|
31
|
+
import * as StepKey from "./StepKey.ts"
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* What a node does to the world, declared. Paths only — measuring them is
|
|
35
|
+
* run-time work, so a digest here would break the no-I/O law.
|
|
36
|
+
*
|
|
37
|
+
* @since 0.1.0
|
|
38
|
+
* @category schemas
|
|
39
|
+
* @slop
|
|
40
|
+
*/
|
|
41
|
+
export const NodeEffects = Schema.Struct({
|
|
42
|
+
reads: Schema.Array(FileSet.ReadDeclaration),
|
|
43
|
+
writes: Schema.Array(FileSet.Declaration),
|
|
44
|
+
/**
|
|
45
|
+
* Paths the node declares it will DELETE. A removal mutates the world exactly
|
|
46
|
+
* as a write does, so {@link produces} folds the two together and both the
|
|
47
|
+
* conflict pass and the reader-after-writer pass see them as one set.
|
|
48
|
+
*/
|
|
49
|
+
removes: Schema.optional(Schema.Array(FileSet.Pattern)),
|
|
50
|
+
boundaryMode: FileSet.BoundaryMode
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The value form of {@link NodeEffects}.
|
|
55
|
+
*
|
|
56
|
+
* @since 0.1.0
|
|
57
|
+
* @category models
|
|
58
|
+
* @slop
|
|
59
|
+
*/
|
|
60
|
+
export type NodeEffects = typeof NodeEffects.Type
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The plan-time write-conflict strategies of the effect taxonomy.
|
|
64
|
+
*
|
|
65
|
+
* @since 0.1.0
|
|
66
|
+
* @category schemas
|
|
67
|
+
* @slop
|
|
68
|
+
*/
|
|
69
|
+
export const PairStrategy = Effects.ConflictStrategy
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The value form of {@link PairStrategy}.
|
|
73
|
+
*
|
|
74
|
+
* @since 0.1.0
|
|
75
|
+
* @category models
|
|
76
|
+
* @slop
|
|
77
|
+
*/
|
|
78
|
+
export type PairStrategy = typeof PairStrategy.Type
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The runtime half of a conflict annotation: what the scheduler
|
|
82
|
+
* does when the overlap the plan predicted actually bites, or when a
|
|
83
|
+
* scheduled node's inputs are invalidated under it.
|
|
84
|
+
*
|
|
85
|
+
* @since 0.1.0
|
|
86
|
+
* @category schemas
|
|
87
|
+
* @slop
|
|
88
|
+
*/
|
|
89
|
+
export const RuntimeStrategy = Schema.Literals(["delay-rebase", "stop-merge"])
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The value form of {@link RuntimeStrategy}.
|
|
93
|
+
*
|
|
94
|
+
* @since 0.1.0
|
|
95
|
+
* @category models
|
|
96
|
+
* @slop
|
|
97
|
+
*/
|
|
98
|
+
export type RuntimeStrategy = typeof RuntimeStrategy.Type
|
|
99
|
+
|
|
100
|
+
/** @private */
|
|
101
|
+
const NodeKind = Schema.Literals(["step", "agent", "merge"])
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* One resolved overlap between two writers that no dependency path already
|
|
105
|
+
* orders. Conflict is a property of the PAIR, not of one declaration.
|
|
106
|
+
*
|
|
107
|
+
* @since 0.1.0
|
|
108
|
+
* @category schemas
|
|
109
|
+
* @slop
|
|
110
|
+
*/
|
|
111
|
+
export const ConflictAnnotation = Schema.Struct({
|
|
112
|
+
with: Schema.NonEmptyString,
|
|
113
|
+
paths: Schema.Array(Schema.String),
|
|
114
|
+
strategy: PairStrategy,
|
|
115
|
+
runtime: RuntimeStrategy
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* The value form of {@link ConflictAnnotation}.
|
|
120
|
+
*
|
|
121
|
+
* @since 0.1.0
|
|
122
|
+
* @category models
|
|
123
|
+
* @slop
|
|
124
|
+
*/
|
|
125
|
+
export type ConflictAnnotation = typeof ConflictAnnotation.Type
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* A keyed node of the plan.
|
|
129
|
+
*
|
|
130
|
+
* `dependsOn` is the *edge* set: material references, any ordering edge a
|
|
131
|
+
* `serialize` verdict added, and the reader-after-writer edges that put a
|
|
132
|
+
* node behind whoever produces the paths it reads. Ordering edges are
|
|
133
|
+
* deliberately NOT part of the key — a node ordered behind another still
|
|
134
|
+
* computes the same result, so re-keying it would throw away a legitimate
|
|
135
|
+
* cache hit.
|
|
136
|
+
*
|
|
137
|
+
* `strategy` and `runtime` are this declaration's own preferences, recorded so
|
|
138
|
+
* a later elaboration can resolve a pair against them without re-reading the
|
|
139
|
+
* flow source.
|
|
140
|
+
*
|
|
141
|
+
* @since 0.1.0
|
|
142
|
+
* @category schemas
|
|
143
|
+
* @slop
|
|
144
|
+
*/
|
|
145
|
+
export const PlanNode = Schema.Struct({
|
|
146
|
+
id: Schema.NonEmptyString,
|
|
147
|
+
kind: NodeKind,
|
|
148
|
+
key: StoredKey,
|
|
149
|
+
material: KeyMaterial.KeyMaterial,
|
|
150
|
+
effects: NodeEffects,
|
|
151
|
+
dependsOn: Schema.Array(Schema.NonEmptyString),
|
|
152
|
+
conflicts: Schema.Array(ConflictAnnotation),
|
|
153
|
+
strategy: PairStrategy,
|
|
154
|
+
runtime: RuntimeStrategy,
|
|
155
|
+
priority: Schema.Int,
|
|
156
|
+
generation: Schema.Int
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* The value form of {@link PlanNode}.
|
|
161
|
+
*
|
|
162
|
+
* @since 0.1.0
|
|
163
|
+
* @category models
|
|
164
|
+
* @slop
|
|
165
|
+
*/
|
|
166
|
+
export type PlanNode = typeof PlanNode.Type
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* A plan: the whole keyed graph plus the digest an approval binds to.
|
|
170
|
+
*
|
|
171
|
+
* `nodes` follows material-dependency order within each generation. Inferred
|
|
172
|
+
* edges can point to later entries; schedule from the complete `dependsOn`
|
|
173
|
+
* graph, never by iterating the array alone.
|
|
174
|
+
*
|
|
175
|
+
* `baseDigest` is the digest at generation 0: what a human approved and what
|
|
176
|
+
* a `RUNNING` run pins. `digest` advances with every appended elaboration.
|
|
177
|
+
*
|
|
178
|
+
* @since 0.1.0
|
|
179
|
+
* @category schemas
|
|
180
|
+
* @slop
|
|
181
|
+
*/
|
|
182
|
+
export const Plan = Schema.Struct({
|
|
183
|
+
planId: Schema.NonEmptyString,
|
|
184
|
+
flow: Schema.NonEmptyString,
|
|
185
|
+
generation: Schema.Int,
|
|
186
|
+
baseDigest: StoredKey,
|
|
187
|
+
digest: StoredKey,
|
|
188
|
+
nodes: Schema.Array(PlanNode)
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* The value form of {@link Plan}.
|
|
193
|
+
*
|
|
194
|
+
* @since 0.1.0
|
|
195
|
+
* @category models
|
|
196
|
+
* @slop
|
|
197
|
+
*/
|
|
198
|
+
export type Plan = typeof Plan.Type
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* What a planner hands {@link compile}: a node without its key.
|
|
202
|
+
*
|
|
203
|
+
* @since 0.1.0
|
|
204
|
+
* @category models
|
|
205
|
+
* @slop
|
|
206
|
+
*/
|
|
207
|
+
export interface NodeDraft {
|
|
208
|
+
readonly id: string
|
|
209
|
+
readonly material: KeyMaterial.KeyMaterial
|
|
210
|
+
readonly effects: NodeEffects
|
|
211
|
+
readonly kind?: PlanNode["kind"] | undefined
|
|
212
|
+
readonly priority?: number | undefined
|
|
213
|
+
/** This declaration's preferred plan-time verdict for a detected overlap. */
|
|
214
|
+
readonly conflictStrategy?: PairStrategy | undefined
|
|
215
|
+
/** This declaration's preferred runtime response. */
|
|
216
|
+
readonly runtimeStrategy?: RuntimeStrategy | undefined
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* A graph the compiler refuses.
|
|
221
|
+
*
|
|
222
|
+
* @since 0.1.0
|
|
223
|
+
* @category errors
|
|
224
|
+
* @slop
|
|
225
|
+
*/
|
|
226
|
+
export class PlanError extends Schema.TaggedError<PlanError>()("@smthrs/plan/PlanError", {
|
|
227
|
+
code: Schema.Literals([
|
|
228
|
+
"cycle",
|
|
229
|
+
"unknown_dependency",
|
|
230
|
+
"duplicate_node",
|
|
231
|
+
"overlap_forbidden",
|
|
232
|
+
"invalid_effects",
|
|
233
|
+
"invalid_node",
|
|
234
|
+
"invalid_plan",
|
|
235
|
+
"graph_too_large"
|
|
236
|
+
]),
|
|
237
|
+
message: Schema.String
|
|
238
|
+
}) {}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Maximum number of nodes retained by one compiled plan.
|
|
242
|
+
*
|
|
243
|
+
* Effect analysis also has a yielding work budget, since a node ceiling alone
|
|
244
|
+
* does not bound dense conflicts to practical work.
|
|
245
|
+
*
|
|
246
|
+
* @since 1.0.0
|
|
247
|
+
* @category limits
|
|
248
|
+
* @slop
|
|
249
|
+
*/
|
|
250
|
+
export const maximumPlanNodes = 10_000
|
|
251
|
+
|
|
252
|
+
/** @private */
|
|
253
|
+
const graphSizeError = (size: number): PlanError =>
|
|
254
|
+
new PlanError({
|
|
255
|
+
code: "graph_too_large",
|
|
256
|
+
message: `A plan may contain at most ${maximumPlanNodes} nodes, received ${size}`
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
/** @private */
|
|
260
|
+
type Ordered =
|
|
261
|
+
| { readonly ok: true; readonly drafts: ReadonlyArray<NodeDraft> }
|
|
262
|
+
| { readonly ok: false; readonly error: PlanError }
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Topologically orders drafts by their MATERIAL dependencies — the only edges
|
|
266
|
+
* that exist before keys do.
|
|
267
|
+
*
|
|
268
|
+
* @private
|
|
269
|
+
*/
|
|
270
|
+
const topological = (drafts: ReadonlyArray<NodeDraft>, known: ReadonlySet<string>): Ordered => {
|
|
271
|
+
const byId = new Map(drafts.map((draft) => [draft.id, draft]))
|
|
272
|
+
const ordered: Array<NodeDraft> = []
|
|
273
|
+
const state = new Map<string, "visiting" | "done">()
|
|
274
|
+
interface Frame {
|
|
275
|
+
readonly draft: NodeDraft
|
|
276
|
+
readonly dependencies: ReadonlyArray<string>
|
|
277
|
+
next: number
|
|
278
|
+
}
|
|
279
|
+
for (const draft of drafts) {
|
|
280
|
+
if (state.get(draft.id) === "done") continue
|
|
281
|
+
state.set(draft.id, "visiting")
|
|
282
|
+
const stack: Array<Frame> = [{ draft, dependencies: KeyMaterial.dependencies(draft.material), next: 0 }]
|
|
283
|
+
while (stack.length > 0) {
|
|
284
|
+
const frame = stack[stack.length - 1]!
|
|
285
|
+
if (frame.next === frame.dependencies.length) {
|
|
286
|
+
state.set(frame.draft.id, "done")
|
|
287
|
+
ordered.push(frame.draft)
|
|
288
|
+
stack.pop()
|
|
289
|
+
continue
|
|
290
|
+
}
|
|
291
|
+
const dependency = frame.dependencies[frame.next++]!
|
|
292
|
+
if (known.has(dependency)) continue
|
|
293
|
+
const next = byId.get(dependency)
|
|
294
|
+
if (next === undefined) {
|
|
295
|
+
return {
|
|
296
|
+
ok: false,
|
|
297
|
+
error: new PlanError({
|
|
298
|
+
code: "unknown_dependency",
|
|
299
|
+
message: `Node ${frame.draft.id} depends on unknown node ${dependency}`
|
|
300
|
+
})
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
const mark = state.get(next.id)
|
|
304
|
+
if (mark === "done") continue
|
|
305
|
+
if (mark === "visiting") {
|
|
306
|
+
return {
|
|
307
|
+
ok: false,
|
|
308
|
+
error: new PlanError({ code: "cycle", message: `Plan cycle through node ${next.id}` })
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
state.set(next.id, "visiting")
|
|
312
|
+
stack.push({ draft: next, dependencies: KeyMaterial.dependencies(next.material), next: 0 })
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return { ok: true, drafts: ordered }
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Runs the conflict and reader-after-writer passes and records what they found
|
|
320
|
+
* on every node outside `frozen`, whose rows an append never rewrites. A
|
|
321
|
+
* refused graph fails as a `PlanError` carrying the pass's code and message.
|
|
322
|
+
*
|
|
323
|
+
* @private
|
|
324
|
+
*/
|
|
325
|
+
const annotate = (
|
|
326
|
+
nodes: ReadonlyArray<PlanNode>,
|
|
327
|
+
frozen: ReadonlySet<string>,
|
|
328
|
+
replayGenerations = false
|
|
329
|
+
): Effect.Effect<ReadonlyArray<PlanNode>, PlanError> =>
|
|
330
|
+
Conflicts.annotate(nodes, frozen.size, replayGenerations).pipe(
|
|
331
|
+
Effect.mapError((refusal) => new PlanError(refusal)),
|
|
332
|
+
Effect.map(({ conflicts, ordering }) =>
|
|
333
|
+
nodes.map((node) => {
|
|
334
|
+
if (frozen.has(node.id)) return node
|
|
335
|
+
const added = ordering.get(node.id) ?? []
|
|
336
|
+
return freezeNode({
|
|
337
|
+
...node,
|
|
338
|
+
conflicts: conflicts.get(node.id) ?? [],
|
|
339
|
+
dependsOn: added.length === 0 ? node.dependsOn : [...node.dependsOn, ...added]
|
|
340
|
+
})
|
|
341
|
+
})
|
|
342
|
+
)
|
|
343
|
+
)
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* The single approval projection. Its node list is the complete reviewable and
|
|
347
|
+
* behavior-bearing contract: id, kind, key, edges, conflict annotations,
|
|
348
|
+
* declared effects, conflict strategy, runtime strategy, and priority.
|
|
349
|
+
*
|
|
350
|
+
* @private
|
|
351
|
+
*/
|
|
352
|
+
const approvalPayload = (
|
|
353
|
+
planId: string,
|
|
354
|
+
flow: string,
|
|
355
|
+
nodes: ReadonlyArray<PlanNode>
|
|
356
|
+
) => ({
|
|
357
|
+
body: { kind: "plan", planId, flow },
|
|
358
|
+
inputs: {
|
|
359
|
+
nodes: nodes.map((node) => ({
|
|
360
|
+
id: node.id,
|
|
361
|
+
kind: node.kind,
|
|
362
|
+
key: node.key,
|
|
363
|
+
dependsOn: node.dependsOn,
|
|
364
|
+
conflicts: node.conflicts,
|
|
365
|
+
effects: node.effects,
|
|
366
|
+
strategy: node.strategy,
|
|
367
|
+
runtime: node.runtime,
|
|
368
|
+
priority: node.priority
|
|
369
|
+
}))
|
|
370
|
+
},
|
|
371
|
+
layers: [],
|
|
372
|
+
capabilities: {}
|
|
373
|
+
})
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* The plan's digest: what an approval binds to. Deriving it only from
|
|
377
|
+
* {@link approvalPayload} keeps the approved field list in one place.
|
|
378
|
+
*
|
|
379
|
+
* @private
|
|
380
|
+
*/
|
|
381
|
+
const digestOf = (
|
|
382
|
+
planId: string,
|
|
383
|
+
flow: string,
|
|
384
|
+
nodes: ReadonlyArray<PlanNode>
|
|
385
|
+
): Effect.Effect<StepKey.StepKey, StepKey.KeyMaterialError | Schema.SchemaError, Crypto.Crypto> =>
|
|
386
|
+
StepKey.content(approvalPayload(planId, flow, nodes))
|
|
387
|
+
|
|
388
|
+
/** @private */
|
|
389
|
+
const maximumDescriptionLength = 256
|
|
390
|
+
|
|
391
|
+
/** @private */
|
|
392
|
+
const describeValue = (value: unknown): string => {
|
|
393
|
+
const rendered = typeof value === "string" ? JSON.stringify(value) : Inspectable.toStringUnknown(value, 0)
|
|
394
|
+
return rendered.length > maximumDescriptionLength
|
|
395
|
+
? `${rendered.slice(0, maximumDescriptionLength - 3)}...`
|
|
396
|
+
: rendered
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/** @private */
|
|
400
|
+
const describeNodeId = (id: string): string => id.length > maximumDescriptionLength ? describeValue(id) : id
|
|
401
|
+
|
|
402
|
+
/** @private */
|
|
403
|
+
const validateIdentity = (planId: unknown, flow: unknown): PlanError | undefined => {
|
|
404
|
+
if (typeof planId !== "string" || planId.length === 0) {
|
|
405
|
+
return new PlanError({
|
|
406
|
+
code: "invalid_node",
|
|
407
|
+
message: `Plan option planId must be a non-empty string, received ${describeValue(planId)}`
|
|
408
|
+
})
|
|
409
|
+
}
|
|
410
|
+
if (typeof flow !== "string" || flow.length === 0) {
|
|
411
|
+
return new PlanError({
|
|
412
|
+
code: "invalid_node",
|
|
413
|
+
message: `Plan option flow must be a non-empty string, received ${describeValue(flow)}`
|
|
414
|
+
})
|
|
415
|
+
}
|
|
416
|
+
return undefined
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/** Whether `value` is a container this module may safely copy. */
|
|
420
|
+
const isSnapshotContainer = (value: unknown): value is object => {
|
|
421
|
+
if (typeof value !== "object" || value === null) return false
|
|
422
|
+
if (Array.isArray(value)) return true
|
|
423
|
+
const prototype = Object.getPrototypeOf(value)
|
|
424
|
+
return prototype === Object.prototype || prototype === null
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/** Creates an empty container with the source container's supported prototype. */
|
|
428
|
+
const emptySnapshot = (source: object): object =>
|
|
429
|
+
Array.isArray(source) ? [] : Object.create(Object.getPrototypeOf(source)) as object
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Copies and freezes plain structural containers without interpreting them as
|
|
433
|
+
* JSON. Shared references and cycles point at one cloned container through
|
|
434
|
+
* `memo`; every other value passes through by reference and remains unfrozen.
|
|
435
|
+
* In particular, this does not probe a `Planned` proxy. `validateDrafts`
|
|
436
|
+
* creates the inert material mirror inside the Effect while retaining that
|
|
437
|
+
* proxy, so the canonical serializer still reaches and refuses it.
|
|
438
|
+
*/
|
|
439
|
+
const snapshot = <A extends object>(value: A): A => {
|
|
440
|
+
const root = emptySnapshot(value)
|
|
441
|
+
const memo = new WeakMap<object, object>([[value, root]])
|
|
442
|
+
const pending: Array<{ readonly source: object; readonly target: object }> = [{ source: value, target: root }]
|
|
443
|
+
const created = [root]
|
|
444
|
+
|
|
445
|
+
while (pending.length > 0) {
|
|
446
|
+
const { source, target } = pending.pop()!
|
|
447
|
+
for (const key of Reflect.ownKeys(source)) {
|
|
448
|
+
const descriptor = Object.getOwnPropertyDescriptor(source, key)!
|
|
449
|
+
if ("value" in descriptor && isSnapshotContainer(descriptor.value)) {
|
|
450
|
+
let child = memo.get(descriptor.value)
|
|
451
|
+
if (child === undefined) {
|
|
452
|
+
child = emptySnapshot(descriptor.value)
|
|
453
|
+
memo.set(descriptor.value, child)
|
|
454
|
+
pending.push({ source: descriptor.value, target: child })
|
|
455
|
+
created.push(child)
|
|
456
|
+
}
|
|
457
|
+
descriptor.value = child
|
|
458
|
+
}
|
|
459
|
+
Object.defineProperty(target, key, descriptor)
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
for (const container of created) Object.freeze(container)
|
|
464
|
+
return root as A
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/** Freezes one node built by this module while retaining its material snapshot. */
|
|
468
|
+
const freezeNode = (node: PlanNode): PlanNode =>
|
|
469
|
+
Object.freeze({
|
|
470
|
+
...node,
|
|
471
|
+
dependsOn: Object.freeze([...node.dependsOn]),
|
|
472
|
+
conflicts: Object.freeze(
|
|
473
|
+
node.conflicts.map((conflict) => Object.freeze({ ...conflict, paths: Object.freeze([...conflict.paths]) }))
|
|
474
|
+
)
|
|
475
|
+
})
|
|
476
|
+
|
|
477
|
+
/** Plans whose complete immutable snapshot was created by this module. */
|
|
478
|
+
const frozenPlans = new WeakSet<Plan>()
|
|
479
|
+
|
|
480
|
+
/** Freezes the plan envelope and its newly built node list. */
|
|
481
|
+
const freezePlan = (plan: Plan): Plan => {
|
|
482
|
+
const frozen = Object.freeze({ ...plan, nodes: Object.freeze([...plan.nodes]) })
|
|
483
|
+
frozenPlans.add(frozen)
|
|
484
|
+
return frozen
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Validates the runtime-only parts of `NodeDraft` without replacing effect
|
|
489
|
+
* admission. Effect 4 Struct decoding strips excess properties and copies its
|
|
490
|
+
* schema arrays. The decoded effect declaration replaces any caller-supplied
|
|
491
|
+
* `material.effects`, and every accepted draft receives one deeply frozen JSON
|
|
492
|
+
* mirror for both storage and hashing.
|
|
493
|
+
*
|
|
494
|
+
* @private
|
|
495
|
+
*/
|
|
496
|
+
const validateDrafts = (
|
|
497
|
+
drafts: ReadonlyArray<NodeDraft>
|
|
498
|
+
): Effect.Effect<ReadonlyArray<NodeDraft>, PlanError> =>
|
|
499
|
+
Effect.forEach(drafts, (draft) =>
|
|
500
|
+
Effect.gen(function*() {
|
|
501
|
+
if (typeof draft.id !== "string" || draft.id.length === 0) {
|
|
502
|
+
return yield* Effect.fail(
|
|
503
|
+
new PlanError({
|
|
504
|
+
code: "invalid_node",
|
|
505
|
+
message: `Node id must be a non-empty string, received ${describeValue(draft.id)}`
|
|
506
|
+
})
|
|
507
|
+
)
|
|
508
|
+
}
|
|
509
|
+
const nodeId = describeNodeId(draft.id)
|
|
510
|
+
if (draft.priority !== undefined && !Number.isSafeInteger(draft.priority)) {
|
|
511
|
+
return yield* Effect.fail(
|
|
512
|
+
new PlanError({
|
|
513
|
+
code: "invalid_node",
|
|
514
|
+
message: `Node ${nodeId} priority must be a safe integer, received ${describeValue(draft.priority)}`
|
|
515
|
+
})
|
|
516
|
+
)
|
|
517
|
+
}
|
|
518
|
+
if (draft.kind !== undefined && !Schema.is(NodeKind)(draft.kind)) {
|
|
519
|
+
return yield* Effect.fail(
|
|
520
|
+
new PlanError({
|
|
521
|
+
code: "invalid_node",
|
|
522
|
+
message: `Node ${nodeId} kind must be step, agent, or merge, received ${describeValue(draft.kind)}`
|
|
523
|
+
})
|
|
524
|
+
)
|
|
525
|
+
}
|
|
526
|
+
if (draft.conflictStrategy !== undefined && !Schema.is(PairStrategy)(draft.conflictStrategy)) {
|
|
527
|
+
return yield* Effect.fail(
|
|
528
|
+
new PlanError({
|
|
529
|
+
code: "invalid_node",
|
|
530
|
+
message: `Node ${nodeId} conflictStrategy must be serialize, lane, or fail, received ${
|
|
531
|
+
describeValue(draft.conflictStrategy)
|
|
532
|
+
}`
|
|
533
|
+
})
|
|
534
|
+
)
|
|
535
|
+
}
|
|
536
|
+
if (draft.runtimeStrategy !== undefined && !Schema.is(RuntimeStrategy)(draft.runtimeStrategy)) {
|
|
537
|
+
return yield* Effect.fail(
|
|
538
|
+
new PlanError({
|
|
539
|
+
code: "invalid_node",
|
|
540
|
+
message: `Node ${nodeId} runtimeStrategy must be delay-rebase or stop-merge, received ${
|
|
541
|
+
describeValue(draft.runtimeStrategy)
|
|
542
|
+
}`
|
|
543
|
+
})
|
|
544
|
+
)
|
|
545
|
+
}
|
|
546
|
+
const decodedMaterial = yield* Schema.decodeUnknownEffect(KeyMaterial.KeyMaterial)(draft.material).pipe(
|
|
547
|
+
Effect.mapError((cause) =>
|
|
548
|
+
new PlanError({
|
|
549
|
+
code: "invalid_node",
|
|
550
|
+
message: `Node ${nodeId} has invalid material: ${cause.message}`
|
|
551
|
+
})
|
|
552
|
+
)
|
|
553
|
+
)
|
|
554
|
+
const decodedEffects = yield* Schema.decodeUnknownEffect(NodeEffects)(draft.effects).pipe(
|
|
555
|
+
Effect.mapError((cause) =>
|
|
556
|
+
new PlanError({
|
|
557
|
+
code: "invalid_node",
|
|
558
|
+
message: `Node ${nodeId} has invalid effects: ${cause.message}`
|
|
559
|
+
})
|
|
560
|
+
)
|
|
561
|
+
)
|
|
562
|
+
let material: KeyMaterial.KeyMaterial
|
|
563
|
+
try {
|
|
564
|
+
material = snapshot(
|
|
565
|
+
jsonMirror({ ...decodedMaterial, effects: decodedEffects }, (planned) => planned) as KeyMaterial.KeyMaterial
|
|
566
|
+
)
|
|
567
|
+
} catch (cause) {
|
|
568
|
+
const path = cause instanceof GraphBuildError ? cause.path : []
|
|
569
|
+
return yield* Effect.fail(
|
|
570
|
+
new PlanError({
|
|
571
|
+
code: "invalid_node",
|
|
572
|
+
message: `Node ${nodeId} has invalid material payload at ${["$", ...path].join(".")}`
|
|
573
|
+
})
|
|
574
|
+
)
|
|
575
|
+
}
|
|
576
|
+
return Object.freeze({
|
|
577
|
+
...draft,
|
|
578
|
+
material,
|
|
579
|
+
effects: material.effects as NodeEffects
|
|
580
|
+
})
|
|
581
|
+
}))
|
|
582
|
+
|
|
583
|
+
/**
|
|
584
|
+
* The effects schema admits each individual declaration in `validateDrafts`.
|
|
585
|
+
* This pass enforces the cross-field invariant that one path cannot be both a
|
|
586
|
+
* write and a removal.
|
|
587
|
+
*
|
|
588
|
+
* @private
|
|
589
|
+
*/
|
|
590
|
+
const validateEffects = (id: string, effects: NodeEffects): PlanError | undefined => {
|
|
591
|
+
const removes = effects.removes ?? []
|
|
592
|
+
const writes = FileSet.expand(effects.writes)
|
|
593
|
+
for (const removal of removes) {
|
|
594
|
+
if (writes.some((entry) => FileSet.overlaps(entry, removal))) {
|
|
595
|
+
return new PlanError({
|
|
596
|
+
code: "invalid_effects",
|
|
597
|
+
message: `Node ${id} declares ${removal} as both a write and a removal`
|
|
598
|
+
})
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
return undefined
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
/** @private */
|
|
605
|
+
const keyNodes = (
|
|
606
|
+
drafts: ReadonlyArray<NodeDraft>,
|
|
607
|
+
existing: ReadonlyArray<PlanNode>,
|
|
608
|
+
generation: number
|
|
609
|
+
): Effect.Effect<
|
|
610
|
+
ReadonlyArray<PlanNode>,
|
|
611
|
+
PlanError | StepKey.KeyMaterialError | Schema.SchemaError,
|
|
612
|
+
Crypto.Crypto
|
|
613
|
+
> =>
|
|
614
|
+
Effect.gen(function*() {
|
|
615
|
+
const digests: Record<string, string> = Object.create(null) as Record<string, string>
|
|
616
|
+
const known = new Set<string>()
|
|
617
|
+
for (const node of existing) {
|
|
618
|
+
Object.defineProperty(digests, node.id, {
|
|
619
|
+
configurable: true,
|
|
620
|
+
enumerable: true,
|
|
621
|
+
value: node.key,
|
|
622
|
+
writable: true
|
|
623
|
+
})
|
|
624
|
+
known.add(node.id)
|
|
625
|
+
}
|
|
626
|
+
for (const draft of drafts) {
|
|
627
|
+
if (known.has(draft.id)) {
|
|
628
|
+
return yield* Effect.fail(
|
|
629
|
+
new PlanError({ code: "duplicate_node", message: `Node ${draft.id} is already in the plan` })
|
|
630
|
+
)
|
|
631
|
+
}
|
|
632
|
+
known.add(draft.id)
|
|
633
|
+
}
|
|
634
|
+
const sorted = topological(drafts, new Set(existing.map((node) => node.id)))
|
|
635
|
+
if (!sorted.ok) return yield* Effect.fail(sorted.error)
|
|
636
|
+
const keyed: Array<PlanNode> = []
|
|
637
|
+
for (const draft of sorted.drafts) {
|
|
638
|
+
const invalid = validateEffects(draft.id, draft.effects)
|
|
639
|
+
if (invalid !== undefined) return yield* Effect.fail(invalid)
|
|
640
|
+
const key = yield* StepKey.planIdentity(draft.material, digests)
|
|
641
|
+
Object.defineProperty(digests, draft.id, {
|
|
642
|
+
configurable: true,
|
|
643
|
+
enumerable: true,
|
|
644
|
+
value: key,
|
|
645
|
+
writable: true
|
|
646
|
+
})
|
|
647
|
+
keyed.push(freezeNode({
|
|
648
|
+
id: draft.id,
|
|
649
|
+
kind: draft.kind ?? "step",
|
|
650
|
+
key,
|
|
651
|
+
material: draft.material,
|
|
652
|
+
effects: draft.effects,
|
|
653
|
+
dependsOn: KeyMaterial.dependencies(draft.material),
|
|
654
|
+
conflicts: [],
|
|
655
|
+
strategy: draft.conflictStrategy ?? "serialize",
|
|
656
|
+
runtime: draft.runtimeStrategy ?? "delay-rebase",
|
|
657
|
+
priority: draft.priority ?? 0,
|
|
658
|
+
generation
|
|
659
|
+
}))
|
|
660
|
+
}
|
|
661
|
+
return keyed
|
|
662
|
+
})
|
|
663
|
+
|
|
664
|
+
/**
|
|
665
|
+
* Compiles drafts into a plan: material-dependency order, dependency-digest
|
|
666
|
+
* substitution, overlap annotation, and the plan digest. No I/O.
|
|
667
|
+
* Inferred edges extend `dependsOn` without reordering the node array.
|
|
668
|
+
*
|
|
669
|
+
* Traversal uses explicit stacks. Because the conflict and reader/writer
|
|
670
|
+
* passes consider a quadratic number of node pairs, plans are bounded by
|
|
671
|
+
* {@link maximumPlanNodes} and fail with `graph_too_large` above that limit
|
|
672
|
+
* or the effect-analysis work budget. Analysis yields periodically.
|
|
673
|
+
*
|
|
674
|
+
* @since 0.1.0
|
|
675
|
+
* @category constructors
|
|
676
|
+
* @slop
|
|
677
|
+
*/
|
|
678
|
+
export const compile = (options: {
|
|
679
|
+
readonly planId: string
|
|
680
|
+
readonly flow: string
|
|
681
|
+
readonly nodes: ReadonlyArray<NodeDraft>
|
|
682
|
+
}): Effect.Effect<Plan, PlanError | StepKey.KeyMaterialError | Schema.SchemaError, Crypto.Crypto> => {
|
|
683
|
+
const captured = snapshot(options)
|
|
684
|
+
return Effect.gen(function*() {
|
|
685
|
+
const invalidIdentity = validateIdentity(captured.planId, captured.flow)
|
|
686
|
+
if (invalidIdentity !== undefined) return yield* Effect.fail(invalidIdentity)
|
|
687
|
+
if (captured.nodes.length > maximumPlanNodes) return yield* Effect.fail(graphSizeError(captured.nodes.length))
|
|
688
|
+
const drafts = yield* validateDrafts(captured.nodes)
|
|
689
|
+
const keyed = yield* keyNodes(drafts, [], 0)
|
|
690
|
+
const nodes = yield* annotate(keyed, new Set())
|
|
691
|
+
const digest = yield* digestOf(captured.planId, captured.flow, nodes)
|
|
692
|
+
return freezePlan({
|
|
693
|
+
planId: captured.planId,
|
|
694
|
+
flow: captured.flow,
|
|
695
|
+
generation: 0,
|
|
696
|
+
baseDigest: digest,
|
|
697
|
+
digest,
|
|
698
|
+
nodes
|
|
699
|
+
})
|
|
700
|
+
})
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
/**
|
|
704
|
+
* Appends an elaborated subgraph to an existing plan.
|
|
705
|
+
*
|
|
706
|
+
* The plan GROWS; it is never invalidated. Nodes already in it keep their id,
|
|
707
|
+
* key, edges, and generation byte for byte, and the new nodes arrive pre-keyed
|
|
708
|
+
* against them, so a `hit` shows instantly. New nodes retain their material
|
|
709
|
+
* dependency order; scheduling must honor their complete `dependsOn` graph.
|
|
710
|
+
* Re-ordering after a reconciliation happens by re-keying *future* steps,
|
|
711
|
+
* never by rewriting history.
|
|
712
|
+
*
|
|
713
|
+
* @since 0.1.0
|
|
714
|
+
* @category constructors
|
|
715
|
+
* @slop
|
|
716
|
+
*/
|
|
717
|
+
export const append = (
|
|
718
|
+
plan: Plan,
|
|
719
|
+
drafts: ReadonlyArray<NodeDraft>
|
|
720
|
+
): Effect.Effect<Plan, PlanError | StepKey.KeyMaterialError | Schema.SchemaError, Crypto.Crypto> => {
|
|
721
|
+
const candidate = frozenPlans.has(plan) ? plan : snapshot(plan)
|
|
722
|
+
const capturedDrafts = snapshot(drafts)
|
|
723
|
+
return Effect.gen(function*() {
|
|
724
|
+
const invalidIdentity = validateIdentity(candidate.planId, candidate.flow)
|
|
725
|
+
if (invalidIdentity !== undefined) return yield* Effect.fail(invalidIdentity)
|
|
726
|
+
const capturedPlan = yield* verify(candidate)
|
|
727
|
+
const nextSize = capturedPlan.nodes.length + capturedDrafts.length
|
|
728
|
+
if (nextSize > maximumPlanNodes) return yield* Effect.fail(graphSizeError(nextSize))
|
|
729
|
+
const validated = yield* validateDrafts(capturedDrafts)
|
|
730
|
+
if (validated.length === 0) {
|
|
731
|
+
return yield* Effect.fail(
|
|
732
|
+
new PlanError({
|
|
733
|
+
code: "invalid_node",
|
|
734
|
+
message: `Plan ${capturedPlan.planId} append requires at least one draft`
|
|
735
|
+
})
|
|
736
|
+
)
|
|
737
|
+
}
|
|
738
|
+
const generation = capturedPlan.generation + 1
|
|
739
|
+
const keyed = yield* keyNodes(validated, capturedPlan.nodes, generation)
|
|
740
|
+
const nodes = yield* annotate(
|
|
741
|
+
[...capturedPlan.nodes, ...keyed],
|
|
742
|
+
new Set(capturedPlan.nodes.map((node) => node.id))
|
|
743
|
+
)
|
|
744
|
+
const digest = yield* digestOf(capturedPlan.planId, capturedPlan.flow, nodes)
|
|
745
|
+
return freezePlan({ ...capturedPlan, generation, digest, nodes })
|
|
746
|
+
})
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
/**
|
|
750
|
+
* Verifies an imported plan against the same key and topology rules as the
|
|
751
|
+
* compiler. Valid compiler-owned immutable values need no second compilation.
|
|
752
|
+
*
|
|
753
|
+
* Generation boundaries are replayed when reconstructing conflict annotations:
|
|
754
|
+
* an append may annotate new nodes, but cannot rewrite an earlier generation.
|
|
755
|
+
* Recorded material-dependency order is verified without sorting inferred edges.
|
|
756
|
+
* Keys and approval digests retain their existing format. The returned snapshot
|
|
757
|
+
* is immutable; later caller mutation cannot change the verified value.
|
|
758
|
+
*
|
|
759
|
+
* @since 1.0.0
|
|
760
|
+
* @category validation
|
|
761
|
+
*/
|
|
762
|
+
export const verify = (
|
|
763
|
+
input: unknown
|
|
764
|
+
): Effect.Effect<Plan, PlanError | StepKey.KeyMaterialError | Schema.SchemaError, Crypto.Crypto> =>
|
|
765
|
+
Effect.gen(function*() {
|
|
766
|
+
if (typeof input === "object" && input !== null && frozenPlans.has(input as Plan)) return input as Plan
|
|
767
|
+
// Check the array ceiling before schema decoding copies its members.
|
|
768
|
+
if (typeof input === "object" && input !== null && "nodes" in input && Array.isArray(input.nodes)) {
|
|
769
|
+
if (input.nodes.length > maximumPlanNodes) return yield* Effect.fail(graphSizeError(input.nodes.length))
|
|
770
|
+
}
|
|
771
|
+
const decoded = yield* Schema.decodeUnknownEffect(Plan)(input)
|
|
772
|
+
const invalid = (message: string) => new PlanError({ code: "invalid_plan", message })
|
|
773
|
+
if (
|
|
774
|
+
!Number.isSafeInteger(decoded.generation) || decoded.generation < 0 || decoded.generation > decoded.nodes.length
|
|
775
|
+
) {
|
|
776
|
+
return yield* Effect.fail(
|
|
777
|
+
invalid("Plan generation must be a non-negative safe integer with a nonempty appended generation")
|
|
778
|
+
)
|
|
779
|
+
}
|
|
780
|
+
const generations = new Map<string, number>()
|
|
781
|
+
let previous = 0
|
|
782
|
+
for (const node of decoded.nodes) {
|
|
783
|
+
if (!Number.isSafeInteger(node.generation) || node.generation < previous || node.generation > previous + 1) {
|
|
784
|
+
return yield* Effect.fail(invalid(`Node ${node.id} has an invalid or out-of-order generation`))
|
|
785
|
+
}
|
|
786
|
+
generations.set(node.id, node.generation)
|
|
787
|
+
previous = node.generation
|
|
788
|
+
}
|
|
789
|
+
if (previous !== decoded.generation) {
|
|
790
|
+
return yield* Effect.fail(invalid("Plan generation does not match its newest nodes"))
|
|
791
|
+
}
|
|
792
|
+
for (const node of decoded.nodes) {
|
|
793
|
+
for (const dependency of KeyMaterial.dependencies(node.material)) {
|
|
794
|
+
const generation = generations.get(dependency)
|
|
795
|
+
if (generation !== undefined && generation > node.generation) {
|
|
796
|
+
return yield* Effect.fail(invalid(`Node ${node.id} refers to a future generation`))
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
const drafts = yield* validateDrafts(decoded.nodes.map((node) => ({
|
|
801
|
+
id: node.id,
|
|
802
|
+
kind: node.kind,
|
|
803
|
+
material: node.material,
|
|
804
|
+
effects: node.effects,
|
|
805
|
+
conflictStrategy: node.strategy,
|
|
806
|
+
runtimeStrategy: node.runtime,
|
|
807
|
+
priority: node.priority
|
|
808
|
+
})))
|
|
809
|
+
const keyed = (yield* keyNodes(drafts, [], 0)).map((node) =>
|
|
810
|
+
freezeNode({ ...node, generation: generations.get(node.id)! })
|
|
811
|
+
)
|
|
812
|
+
// Replay boundaries within one annotation pass, retaining its expansion
|
|
813
|
+
// map, candidate trie, inferred graph and reachability cache throughout.
|
|
814
|
+
const nodes = yield* annotate(keyed, new Set(), true)
|
|
815
|
+
const baseDigest = yield* digestOf(decoded.planId, decoded.flow, nodes.filter((node) => node.generation === 0))
|
|
816
|
+
const digest = decoded.generation === 0 ? baseDigest : yield* digestOf(decoded.planId, decoded.flow, nodes)
|
|
817
|
+
if (decoded.baseDigest !== baseDigest || decoded.digest !== digest) {
|
|
818
|
+
return yield* Effect.fail(invalid("Plan approval digest does not match its compiled content"))
|
|
819
|
+
}
|
|
820
|
+
// Compare the complete node contract, including material and generation,
|
|
821
|
+
// rather than trusting the claimed keys or the approval projection alone.
|
|
822
|
+
const hash = Schema.decodeUnknownEffect(DerivedKey)
|
|
823
|
+
if ((yield* hash(decoded.nodes)) !== (yield* hash(nodes))) {
|
|
824
|
+
return yield* Effect.fail(
|
|
825
|
+
invalid("Plan nodes do not match their compiled keys, effects, ordering, or generations")
|
|
826
|
+
)
|
|
827
|
+
}
|
|
828
|
+
return freezePlan({ ...decoded, nodes })
|
|
829
|
+
})
|
|
830
|
+
|
|
831
|
+
/**
|
|
832
|
+
* The nodes added by the newest generation: what {@link module:PlanStore}
|
|
833
|
+
* appends and what the `subgraph-appended` journal record names.
|
|
834
|
+
*
|
|
835
|
+
* @since 0.1.0
|
|
836
|
+
* @category accessors
|
|
837
|
+
* @slop
|
|
838
|
+
*/
|
|
839
|
+
export const generationNodes = (plan: Plan): ReadonlyArray<PlanNode> =>
|
|
840
|
+
plan.nodes.filter((node) => node.generation === plan.generation)
|
|
841
|
+
|
|
842
|
+
/**
|
|
843
|
+
* Whether `input` is a compiler-owned immutable snapshot that already passed
|
|
844
|
+
* {@link verify}: such values take verify's trusted fast path and need no
|
|
845
|
+
* second schema validation.
|
|
846
|
+
*
|
|
847
|
+
* @since 1.0.0
|
|
848
|
+
* @category guards
|
|
849
|
+
* @slop
|
|
850
|
+
*/
|
|
851
|
+
export const isVerified = (input: unknown): input is Plan =>
|
|
852
|
+
typeof input === "object" && input !== null && frozenPlans.has(input as Plan)
|
|
853
|
+
|
|
854
|
+
/**
|
|
855
|
+
* The approval digest of every generation before the newest: the digest this
|
|
856
|
+
* plan carried before its last append. {@link module:PlanStore}'s append
|
|
857
|
+
* matches it against the stored envelope, so an append proves the recorded
|
|
858
|
+
* plan's integrity from the already-verified prefix instead of decoding and
|
|
859
|
+
* re-verifying every stored row.
|
|
860
|
+
*
|
|
861
|
+
* @since 1.0.0
|
|
862
|
+
* @category accessors
|
|
863
|
+
* @slop
|
|
864
|
+
*/
|
|
865
|
+
export const prefixDigest = (
|
|
866
|
+
plan: Plan
|
|
867
|
+
): Effect.Effect<StepKey.StepKey, StepKey.KeyMaterialError | Schema.SchemaError, Crypto.Crypto> =>
|
|
868
|
+
digestOf(plan.planId, plan.flow, plan.nodes.filter((node) => node.generation < plan.generation))
|