@smthrs/plan 0.0.0-stage → 1.0.0-rc.4

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 (199) hide show
  1. package/CHANGELOG.md +183 -0
  2. package/LICENSE +21 -0
  3. package/README.md +228 -2
  4. package/dist/cjs/CachePolicy.d.ts +131 -0
  5. package/dist/cjs/CachePolicy.d.ts.map +1 -0
  6. package/dist/cjs/CachePolicy.js +50 -0
  7. package/dist/cjs/CachePolicy.js.map +7 -0
  8. package/dist/cjs/Effects.d.ts +240 -0
  9. package/dist/cjs/Effects.d.ts.map +1 -0
  10. package/dist/cjs/Effects.js +76 -0
  11. package/dist/cjs/Effects.js.map +7 -0
  12. package/dist/cjs/FileSet.d.ts +288 -0
  13. package/dist/cjs/FileSet.d.ts.map +1 -0
  14. package/dist/cjs/FileSet.js +191 -0
  15. package/dist/cjs/FileSet.js.map +7 -0
  16. package/dist/cjs/GraphBuildError.d.ts +122 -0
  17. package/dist/cjs/GraphBuildError.d.ts.map +1 -0
  18. package/dist/cjs/GraphBuildError.js +71 -0
  19. package/dist/cjs/GraphBuildError.js.map +7 -0
  20. package/dist/cjs/KeyMaterial.d.ts +102 -0
  21. package/dist/cjs/KeyMaterial.d.ts.map +1 -0
  22. package/dist/cjs/KeyMaterial.js +64 -0
  23. package/dist/cjs/KeyMaterial.js.map +7 -0
  24. package/dist/cjs/Node.d.ts +606 -0
  25. package/dist/cjs/Node.d.ts.map +1 -0
  26. package/dist/cjs/Node.js +207 -0
  27. package/dist/cjs/Node.js.map +7 -0
  28. package/dist/cjs/Placement.d.ts +117 -0
  29. package/dist/cjs/Placement.d.ts.map +1 -0
  30. package/dist/cjs/Placement.js +46 -0
  31. package/dist/cjs/Placement.js.map +7 -0
  32. package/dist/cjs/Plan.d.ts +440 -0
  33. package/dist/cjs/Plan.d.ts.map +1 -0
  34. package/dist/cjs/Plan.js +508 -0
  35. package/dist/cjs/Plan.js.map +7 -0
  36. package/dist/cjs/PlanDiff.d.ts +58 -0
  37. package/dist/cjs/PlanDiff.d.ts.map +1 -0
  38. package/dist/cjs/PlanDiff.js +124 -0
  39. package/dist/cjs/PlanDiff.js.map +7 -0
  40. package/dist/cjs/Planned.d.ts +121 -0
  41. package/dist/cjs/Planned.d.ts.map +1 -0
  42. package/dist/cjs/Planned.js +72 -0
  43. package/dist/cjs/Planned.js.map +7 -0
  44. package/dist/cjs/Repetition.d.ts +31 -0
  45. package/dist/cjs/Repetition.d.ts.map +1 -0
  46. package/dist/cjs/Repetition.js +36 -0
  47. package/dist/cjs/Repetition.js.map +7 -0
  48. package/dist/cjs/Scheduling.d.ts +89 -0
  49. package/dist/cjs/Scheduling.d.ts.map +1 -0
  50. package/dist/cjs/Scheduling.js +76 -0
  51. package/dist/cjs/Scheduling.js.map +7 -0
  52. package/dist/cjs/StepKey.d.ts +333 -0
  53. package/dist/cjs/StepKey.d.ts.map +1 -0
  54. package/dist/cjs/StepKey.js +311 -0
  55. package/dist/cjs/StepKey.js.map +7 -0
  56. package/dist/cjs/index.d.ts +91 -0
  57. package/dist/cjs/index.d.ts.map +1 -0
  58. package/dist/cjs/index.js +59 -0
  59. package/dist/cjs/index.js.map +7 -0
  60. package/dist/cjs/internal/ConflictAnnotation.d.ts +59 -0
  61. package/dist/cjs/internal/ConflictAnnotation.d.ts.map +1 -0
  62. package/dist/cjs/internal/ConflictAnnotation.js +247 -0
  63. package/dist/cjs/internal/ConflictAnnotation.js.map +7 -0
  64. package/dist/cjs/internal/EffectCandidates.d.ts +19 -0
  65. package/dist/cjs/internal/EffectCandidates.d.ts.map +1 -0
  66. package/dist/cjs/internal/EffectCandidates.js +92 -0
  67. package/dist/cjs/internal/EffectCandidates.js.map +7 -0
  68. package/dist/cjs/internal/JsonMirror.d.ts +34 -0
  69. package/dist/cjs/internal/JsonMirror.d.ts.map +1 -0
  70. package/dist/cjs/internal/JsonMirror.js +195 -0
  71. package/dist/cjs/internal/JsonMirror.js.map +7 -0
  72. package/dist/cjs/internal/effects.d.ts +245 -0
  73. package/dist/cjs/internal/effects.d.ts.map +1 -0
  74. package/dist/cjs/internal/effects.js +290 -0
  75. package/dist/cjs/internal/effects.js.map +7 -0
  76. package/dist/cjs/internal/node.d.ts +526 -0
  77. package/dist/cjs/internal/node.d.ts.map +1 -0
  78. package/dist/cjs/internal/node.js +312 -0
  79. package/dist/cjs/internal/node.js.map +7 -0
  80. package/dist/cjs/package.json +1 -0
  81. package/dist/cjs/test/PlanFixtures.d.ts +127 -0
  82. package/dist/cjs/test/PlanFixtures.d.ts.map +1 -0
  83. package/dist/cjs/test/PlanFixtures.js +59 -0
  84. package/dist/cjs/test/PlanFixtures.js.map +7 -0
  85. package/dist/esm/CachePolicy.d.ts +131 -0
  86. package/dist/esm/CachePolicy.d.ts.map +1 -0
  87. package/dist/esm/CachePolicy.js +117 -0
  88. package/dist/esm/CachePolicy.js.map +1 -0
  89. package/dist/esm/Effects.d.ts +240 -0
  90. package/dist/esm/Effects.d.ts.map +1 -0
  91. package/dist/esm/Effects.js +167 -0
  92. package/dist/esm/Effects.js.map +1 -0
  93. package/dist/esm/FileSet.d.ts +288 -0
  94. package/dist/esm/FileSet.d.ts.map +1 -0
  95. package/dist/esm/FileSet.js +324 -0
  96. package/dist/esm/FileSet.js.map +1 -0
  97. package/dist/esm/GraphBuildError.d.ts +122 -0
  98. package/dist/esm/GraphBuildError.d.ts.map +1 -0
  99. package/dist/esm/GraphBuildError.js +146 -0
  100. package/dist/esm/GraphBuildError.js.map +1 -0
  101. package/dist/esm/KeyMaterial.d.ts +102 -0
  102. package/dist/esm/KeyMaterial.d.ts.map +1 -0
  103. package/dist/esm/KeyMaterial.js +85 -0
  104. package/dist/esm/KeyMaterial.js.map +1 -0
  105. package/dist/esm/Node.d.ts +606 -0
  106. package/dist/esm/Node.d.ts.map +1 -0
  107. package/dist/esm/Node.js +521 -0
  108. package/dist/esm/Node.js.map +1 -0
  109. package/dist/esm/Placement.d.ts +117 -0
  110. package/dist/esm/Placement.d.ts.map +1 -0
  111. package/dist/esm/Placement.js +65 -0
  112. package/dist/esm/Placement.js.map +1 -0
  113. package/dist/esm/Plan.d.ts +440 -0
  114. package/dist/esm/Plan.d.ts.map +1 -0
  115. package/dist/esm/Plan.js +673 -0
  116. package/dist/esm/Plan.js.map +1 -0
  117. package/dist/esm/PlanDiff.d.ts +58 -0
  118. package/dist/esm/PlanDiff.d.ts.map +1 -0
  119. package/dist/esm/PlanDiff.js +159 -0
  120. package/dist/esm/PlanDiff.js.map +1 -0
  121. package/dist/esm/Planned.d.ts +121 -0
  122. package/dist/esm/Planned.d.ts.map +1 -0
  123. package/dist/esm/Planned.js +147 -0
  124. package/dist/esm/Planned.js.map +1 -0
  125. package/dist/esm/Repetition.d.ts +31 -0
  126. package/dist/esm/Repetition.d.ts.map +1 -0
  127. package/dist/esm/Repetition.js +24 -0
  128. package/dist/esm/Repetition.js.map +1 -0
  129. package/dist/esm/Scheduling.d.ts +89 -0
  130. package/dist/esm/Scheduling.d.ts.map +1 -0
  131. package/dist/esm/Scheduling.js +82 -0
  132. package/dist/esm/Scheduling.js.map +1 -0
  133. package/dist/esm/StepKey.d.ts +333 -0
  134. package/dist/esm/StepKey.d.ts.map +1 -0
  135. package/dist/esm/StepKey.js +478 -0
  136. package/dist/esm/StepKey.js.map +1 -0
  137. package/dist/esm/index.d.ts +91 -0
  138. package/dist/esm/index.d.ts.map +1 -0
  139. package/dist/esm/index.js +91 -0
  140. package/dist/esm/index.js.map +1 -0
  141. package/dist/esm/internal/ConflictAnnotation.d.ts +59 -0
  142. package/dist/esm/internal/ConflictAnnotation.d.ts.map +1 -0
  143. package/dist/esm/internal/ConflictAnnotation.js +332 -0
  144. package/dist/esm/internal/ConflictAnnotation.js.map +1 -0
  145. package/dist/esm/internal/EffectCandidates.d.ts +19 -0
  146. package/dist/esm/internal/EffectCandidates.d.ts.map +1 -0
  147. package/dist/esm/internal/EffectCandidates.js +86 -0
  148. package/dist/esm/internal/EffectCandidates.js.map +1 -0
  149. package/dist/esm/internal/JsonMirror.d.ts +34 -0
  150. package/dist/esm/internal/JsonMirror.d.ts.map +1 -0
  151. package/dist/esm/internal/JsonMirror.js +204 -0
  152. package/dist/esm/internal/JsonMirror.js.map +1 -0
  153. package/dist/esm/internal/effects.d.ts +245 -0
  154. package/dist/esm/internal/effects.d.ts.map +1 -0
  155. package/dist/esm/internal/effects.js +459 -0
  156. package/dist/esm/internal/effects.js.map +1 -0
  157. package/dist/esm/internal/node.d.ts +526 -0
  158. package/dist/esm/internal/node.d.ts.map +1 -0
  159. package/dist/esm/internal/node.js +564 -0
  160. package/dist/esm/internal/node.js.map +1 -0
  161. package/dist/esm/test/PlanFixtures.d.ts +127 -0
  162. package/dist/esm/test/PlanFixtures.d.ts.map +1 -0
  163. package/dist/esm/test/PlanFixtures.js +60 -0
  164. package/dist/esm/test/PlanFixtures.js.map +1 -0
  165. package/docs/README.md +165 -0
  166. package/docs/api.md +1475 -0
  167. package/docs/concepts/authoring-ast.md +203 -0
  168. package/docs/concepts/effects-and-conflicts.md +135 -0
  169. package/docs/concepts/plan-value.md +168 -0
  170. package/docs/concepts/step-keys.md +179 -0
  171. package/docs/guides/author-a-node-graph.md +182 -0
  172. package/docs/guides/compile-a-plan.md +146 -0
  173. package/docs/guides/declare-file-effects.md +143 -0
  174. package/docs/guides/diff-two-plans.md +101 -0
  175. package/docs/installation.md +71 -0
  176. package/docs/quickstart.md +128 -0
  177. package/docs/testing.md +73 -0
  178. package/docs/troubleshooting.md +314 -0
  179. package/package.json +254 -3
  180. package/src/CachePolicy.ts +143 -0
  181. package/src/Effects.ts +284 -0
  182. package/src/FileSet.ts +411 -0
  183. package/src/GraphBuildError.ts +158 -0
  184. package/src/KeyMaterial.ts +106 -0
  185. package/src/Node.ts +871 -0
  186. package/src/Placement.ts +99 -0
  187. package/src/Plan.ts +868 -0
  188. package/src/PlanDiff.ts +199 -0
  189. package/src/Planned.ts +208 -0
  190. package/src/Repetition.ts +33 -0
  191. package/src/Scheduling.ts +154 -0
  192. package/src/StepKey.ts +710 -0
  193. package/src/index.ts +103 -0
  194. package/src/internal/ConflictAnnotation.ts +374 -0
  195. package/src/internal/EffectCandidates.ts +89 -0
  196. package/src/internal/JsonMirror.ts +255 -0
  197. package/src/internal/effects.ts +543 -0
  198. package/src/internal/node.ts +858 -0
  199. package/src/test/PlanFixtures.ts +76 -0
package/src/StepKey.ts ADDED
@@ -0,0 +1,710 @@
1
+ /**
2
+ * The KeyMaterial → step-key compiler.
3
+ *
4
+ * Revived from the deleted `packages/smithers/flows/keys/src/StepKey.ts`. Two
5
+ * deliberate deviations from the original:
6
+ *
7
+ * 1. **It lives here, not in `@smthrs/keys`.** That package owns generic
8
+ * derivation and stored-key validation; a compiler that understands plan
9
+ * material belongs above it.
10
+ * 2. **It produces `@smthrs/keys` `StoredKey` values, not a second `sk1_` digest
11
+ * format.** The original minted its own prefix over a private `Digest`
12
+ * module. The engine dispatches under `StoredKey` (`FlowEngine/ActionKey.ts`),
13
+ * so a plan whose node keys were a *different* string format could never be
14
+ * the thing the cache is consulted against, and that is the whole point of
15
+ * a `Plan`. One key format, one hashing
16
+ * chokepoint (canonical JSON + injected `Crypto`), one namespace field
17
+ * (`kind`) distinguishing content keys from ordinal ones.
18
+ *
19
+ * Everything the original earned is kept: nominally branded digest inputs (a
20
+ * literal that merely *looks* like `{digest}` hashes as a literal), set
21
+ * normalization, the separate environment namespace, per-variant tagging of
22
+ * resolved references, and the hashed `version`.
23
+ *
24
+ * Governing contract: the step-key rules at
25
+ * https://smithers.sh/docs/concepts/content-addressing.
26
+ *
27
+ * @since 0.1.0
28
+ */
29
+
30
+ import { DerivedKey, type StoredKey } from "@smthrs/keys"
31
+ import * as Cause from "effect/Cause"
32
+ import type * as Crypto from "effect/Crypto"
33
+ import * as Deferred from "effect/Deferred"
34
+ import * as Effect from "effect/Effect"
35
+ import * as Exit from "effect/Exit"
36
+ import * as Schema from "effect/Schema"
37
+ import type * as FileSet from "./FileSet.ts"
38
+ import type * as KeyMaterial from "./KeyMaterial.ts"
39
+
40
+ /**
41
+ * A computed step key. Identical in representation to any other flow key —
42
+ * `key1_` plus a SHA-256 digest of the canonical serialization of the material.
43
+ *
44
+ * @since 0.1.0
45
+ * @category models
46
+ * @slop
47
+ */
48
+ export type StepKey = StoredKey
49
+
50
+ /**
51
+ * The nominal brand carried by every {@link DigestInput}. Kept private: the
52
+ * only way to produce a value bearing it is {@link digestInput}, so a plain
53
+ * value that merely happens to look like `{ digest: "..." }` — actions pass
54
+ * content hashes around as ordinary data — can never be mistaken for a genuine
55
+ * upstream-result digest reference. This closes a step-key collision where
56
+ * shape-sniffing (`"digest" in value`) hashed the two identically.
57
+ *
58
+ * @since 0.1.0
59
+ * @category symbols
60
+ */
61
+ const DigestInputTypeId: unique symbol = Symbol.for("@smthrs/plan/StepKey/DigestInput")
62
+
63
+ /**
64
+ * Which kind of graph reference a {@link DigestInput} stands for. Declared
65
+ * once here so the field on `DigestInput` and the parameter of
66
+ * {@link digestInput} name one set and cannot drift apart.
67
+ */
68
+ type GraphReference = "ref" | "pending" | "ref-projected"
69
+
70
+ /**
71
+ * A precomputed digest supplied as a step input rather than a literal value.
72
+ *
73
+ * @since 0.1.0
74
+ * @category models
75
+ * @slop
76
+ */
77
+ export interface DigestInput {
78
+ readonly [DigestInputTypeId]: typeof DigestInputTypeId
79
+ readonly digest: string
80
+ /**
81
+ * Which kind of graph reference resolved to this digest.
82
+ *
83
+ * Carrying the discriminator on the digest input keeps `normalizeInputs` the
84
+ * single normalizer: it wraps every variant once, and the variants stay
85
+ * mutually distinct because they differ in this field rather than in a
86
+ * hand-built object a second wrap would hide.
87
+ */
88
+ readonly reference?: GraphReference
89
+ /** The projection applied to the referenced result, for `ref-projected`. */
90
+ readonly path?: ReadonlyArray<string>
91
+ }
92
+
93
+ /**
94
+ * Nominally tags a precomputed digest so it is hashed as a digest reference
95
+ * rather than a literal value.
96
+ *
97
+ * @since 0.1.0
98
+ * @category constructors
99
+ * @slop
100
+ */
101
+ export const digestInput = (
102
+ digest: string,
103
+ reference?: {
104
+ readonly reference: GraphReference
105
+ readonly path?: ReadonlyArray<string>
106
+ }
107
+ ): DigestInput => ({
108
+ [DigestInputTypeId]: DigestInputTypeId,
109
+ digest,
110
+ ...(reference === undefined ? {} : { reference: reference.reference }),
111
+ ...(reference?.path === undefined ? {} : { path: reference.path })
112
+ })
113
+
114
+ /**
115
+ * Type guard for values produced by {@link digestInput}.
116
+ *
117
+ * @since 0.1.0
118
+ * @category guards
119
+ * @slop
120
+ */
121
+ export const isDigestInput = (value: unknown): value is DigestInput =>
122
+ typeof value === "object" && value !== null && DigestInputTypeId in value &&
123
+ (value as Record<PropertyKey, unknown>)[DigestInputTypeId] === DigestInputTypeId
124
+
125
+ /**
126
+ * The engine-resolved execution environment a content key is computed under,
127
+ * hashed in its OWN namespace rather than merged into the caller's `layers`
128
+ * and `capabilities`.
129
+ *
130
+ * Three properties matter and none survives a merge:
131
+ *
132
+ * 1. **Non-aliasing.** Concatenating environment material onto the caller's
133
+ * made `caller{fs:["a"]} + env{fs:["b"]}` hash identically to
134
+ * `caller{fs:["a","b"]} + env{}` — a cross-run collision in the very
135
+ * material whose purpose is preventing one.
136
+ * 2. **Declared vs undeclared.** `declared: false` is not the same value as
137
+ * `declared: true` with empty sets.
138
+ * 3. **Composition order.** Environment layers retain declaration order,
139
+ * because plugin and service composition order can change behavior. The
140
+ * caller-owned `ContentIdentity.layers` stays set-normalized.
141
+ *
142
+ * `runScope` is set only when `declared` is `false`: it pins the key to one
143
+ * run, so a step whose environment identity is unknown can never serve a
144
+ * cross-run hit. The union enforces this for typed callers; both
145
+ * {@link content} and {@link dispatchIdentity} validate it at run time.
146
+ *
147
+ * @since 0.1.0
148
+ * @category models
149
+ * @slop
150
+ */
151
+ export type EnvironmentIdentity = typeof EnvironmentIdentity.Type
152
+
153
+ /**
154
+ * Validated, copyable runtime environment identity. Layer order matters;
155
+ * capability patterns are set-like. Unknown fields are not identity material.
156
+ * @since 1.0.0
157
+ * @category schemas
158
+ */
159
+ export const EnvironmentIdentity = Schema.Union([
160
+ Schema.Struct({
161
+ declared: Schema.Literal(true),
162
+ layers: Schema.Array(Schema.String),
163
+ capabilities: Schema.Record(Schema.String, Schema.Array(Schema.String)),
164
+ runScope: Schema.optionalKey(Schema.Undefined)
165
+ }),
166
+ Schema.Struct({
167
+ declared: Schema.Literal(false),
168
+ layers: Schema.Array(Schema.String),
169
+ capabilities: Schema.Record(Schema.String, Schema.Array(Schema.String)),
170
+ runScope: Schema.NonEmptyString
171
+ })
172
+ ])
173
+
174
+ /**
175
+ * Caller-owned memoization context for projected dependency-value digests.
176
+ *
177
+ * This is the flows counterpart of Bazel's `ActionKeyContext`: one context is
178
+ * passed through related key computations so shared input material is hashed
179
+ * once. Concurrent requests for the same projection share the in-flight
180
+ * digest. Entries are sound only while each settled `from` value is immutable;
181
+ * callers must create a fresh memo when those values can change.
182
+ *
183
+ * @since 0.1.0
184
+ * @category models
185
+ * @slop
186
+ */
187
+ export interface DigestMemo {
188
+ readonly digest: (
189
+ from: string,
190
+ path: ReadonlyArray<string>,
191
+ compute: Effect.Effect<StepKey, Schema.SchemaError, Crypto.Crypto>
192
+ ) => Effect.Effect<StepKey, Schema.SchemaError, Crypto.Crypto>
193
+ }
194
+
195
+ /**
196
+ * Creates an empty projected-value digest memo. Projection addresses are
197
+ * JSON-encoded `[from, path]` tuples so segment boundaries are unambiguous.
198
+ *
199
+ * @since 0.1.0
200
+ * @category constructors
201
+ * @slop
202
+ */
203
+ export const makeDigestMemo = (): DigestMemo => {
204
+ const entries = new Map<string, Deferred.Deferred<StepKey, Schema.SchemaError>>()
205
+ const digest: DigestMemo["digest"] = (from, path, compute) =>
206
+ Effect.suspend(() => {
207
+ const address = JSON.stringify([from, path])
208
+ const existing = entries.get(address)
209
+ if (existing !== undefined) {
210
+ return Effect.flatMap(Effect.exit(Deferred.await(existing)), (exit) => {
211
+ if (Exit.isSuccess(exit)) return Effect.succeed(exit.value)
212
+ // A waiter did not request the leader's cancellation. It competes to
213
+ // become the replacement leader instead of inheriting that interrupt.
214
+ return Cause.hasInterrupts(exit.cause) ? digest(from, path, compute) : Effect.failCause(exit.cause)
215
+ })
216
+ }
217
+ const pending = Deferred.makeUnsafe<StepKey, Schema.SchemaError>()
218
+ entries.set(address, pending)
219
+ return compute.pipe(
220
+ Effect.onExit((exit) =>
221
+ Effect.gen(function*() {
222
+ if (Exit.isFailure(exit) && entries.get(address) === pending) {
223
+ entries.delete(address)
224
+ }
225
+ yield* Deferred.done(pending, exit)
226
+ })
227
+ )
228
+ )
229
+ })
230
+ return { digest }
231
+ }
232
+
233
+ /**
234
+ * Material describing a sealed or hermetic content-addressed step.
235
+ *
236
+ * @since 0.1.0
237
+ * @category models
238
+ * @slop
239
+ */
240
+ export interface ContentIdentity {
241
+ readonly body: unknown
242
+ readonly inputs: Readonly<Record<string, unknown | DigestInput>>
243
+ readonly layers: ReadonlyArray<string>
244
+ readonly capabilities: Readonly<Record<string, ReadonlyArray<string>>>
245
+ readonly environment?: EnvironmentIdentity | undefined
246
+ readonly hermetic?: {
247
+ readonly readSet: ReadonlyArray<{ readonly path: string; readonly digest: string }>
248
+ readonly writeSet: ReadonlyArray<FileSet.Entry>
249
+ readonly removes?: ReadonlyArray<string> | undefined
250
+ readonly boundaryMode: FileSet.BoundaryMode
251
+ } | undefined
252
+ }
253
+
254
+ /**
255
+ * Material describing the run-local identity of a non-cacheable step.
256
+ *
257
+ * @since 0.1.0
258
+ * @category models
259
+ * @slop
260
+ */
261
+ export interface OrdinalIdentity {
262
+ readonly runId: string
263
+ readonly parentScope?: string | undefined
264
+ readonly ordinal: number
265
+ readonly tier: "compensable" | "irreversible" | "unsealed"
266
+ }
267
+
268
+ /**
269
+ * Stable failures while resolving graph-local dependency references.
270
+ *
271
+ * @since 0.1.0
272
+ * @category errors
273
+ * @slop
274
+ */
275
+ export class KeyMaterialError extends Schema.TaggedError<KeyMaterialError>()("@smthrs/plan/KeyMaterialError", {
276
+ code: Schema.Literals(["invalid_environment", "missing_dependency", "non_content_material"]),
277
+ message: Schema.String
278
+ }) {}
279
+
280
+ const validateEnvironment = (environment: EnvironmentIdentity): KeyMaterialError | undefined => {
281
+ const candidate = environment as { readonly declared?: unknown; readonly runScope?: unknown }
282
+ if (candidate.declared === false) {
283
+ if (typeof candidate.runScope !== "string" || candidate.runScope.length === 0) {
284
+ return new KeyMaterialError({
285
+ code: "invalid_environment",
286
+ message: "Undeclared environment identity requires a non-empty runScope"
287
+ })
288
+ }
289
+ return undefined
290
+ }
291
+ if (candidate.declared === true) {
292
+ return candidate.runScope === undefined
293
+ ? undefined
294
+ : new KeyMaterialError({
295
+ code: "invalid_environment",
296
+ message: "Declared environment identity must not include runScope"
297
+ })
298
+ }
299
+ return new KeyMaterialError({
300
+ code: "invalid_environment",
301
+ message: "Environment identity requires a boolean declared field"
302
+ })
303
+ }
304
+
305
+ const decodeEnvironment = (environment: EnvironmentIdentity) =>
306
+ Effect.try({
307
+ try: () => {
308
+ const invalid = validateEnvironment(environment)
309
+ if (invalid !== undefined) throw invalid
310
+ return Schema.decodeUnknownSync(EnvironmentIdentity)(environment, { onExcessProperty: "error" })
311
+ },
312
+ catch: (cause) =>
313
+ cause instanceof KeyMaterialError ? cause : new KeyMaterialError({
314
+ code: "invalid_environment",
315
+ message:
316
+ "Environment identity must contain only its declared fields, string layers, and string capability patterns"
317
+ })
318
+ })
319
+
320
+ const sortStrings = (values: ReadonlyArray<string>): Array<string> =>
321
+ [...new Set(values.map((value) => value.normalize("NFC")))].sort()
322
+
323
+ const normalizeInputs = (inputs: ContentIdentity["inputs"]): Record<string, unknown> =>
324
+ Object.fromEntries(
325
+ Object.entries(inputs).map(([name, value]) => [
326
+ name,
327
+ isDigestInput(value)
328
+ ? {
329
+ kind: "digest",
330
+ digest: value.digest,
331
+ ...(value.reference === undefined ? {} : { reference: value.reference }),
332
+ ...(value.path === undefined ? {} : { path: value.path })
333
+ }
334
+ : { kind: "literal", value }
335
+ ])
336
+ )
337
+
338
+ const normalizeCapabilities = (capabilities: ContentIdentity["capabilities"]): Record<string, Array<string>> =>
339
+ Object.fromEntries(Object.entries(capabilities).map(([group, patterns]) => [group, sortStrings(patterns)]))
340
+
341
+ const normalizeEnvironment = (environment: EnvironmentIdentity) => ({
342
+ declared: environment.declared,
343
+ layers: environment.layers.map((layer) => layer.normalize("NFC")),
344
+ capabilities: normalizeCapabilities(environment.capabilities),
345
+ ...(environment.runScope === undefined ? {} : { runScope: environment.runScope })
346
+ })
347
+
348
+ const compareCodeUnits = (left: string, right: string): number => left < right ? -1 : 1
349
+
350
+ type NormalizedWriteEntry =
351
+ | string
352
+ | { readonly _tag: "TreeArtifact"; readonly path: string }
353
+ | {
354
+ readonly _tag: "Glob"
355
+ readonly include: ReadonlyArray<string>
356
+ readonly exclude?: ReadonlyArray<string> | undefined
357
+ }
358
+
359
+ const writeEntryKey = (entry: NormalizedWriteEntry): string =>
360
+ typeof entry === "string"
361
+ ? JSON.stringify(["Path", entry])
362
+ : entry._tag === "TreeArtifact"
363
+ ? JSON.stringify([entry._tag, entry.path])
364
+ : JSON.stringify([entry._tag, entry.include, entry.exclude ?? null])
365
+
366
+ const normalizeHermetic = (hermetic: NonNullable<ContentIdentity["hermetic"]>) => {
367
+ const readSet = [...hermetic.readSet]
368
+ .map((entry) => ({ path: entry.path.normalize("NFC"), digest: entry.digest }))
369
+ .sort((left, right) => {
370
+ if (left.path !== right.path) return left.path < right.path ? -1 : 1
371
+ return left.digest < right.digest ? -1 : left.digest > right.digest ? 1 : 0
372
+ })
373
+ .filter((entry, index, entries) =>
374
+ index === 0 || entry.path !== entries[index - 1]!.path || entry.digest !== entries[index - 1]!.digest
375
+ )
376
+ const normalizedWrites = [...hermetic.writeSet].map((entry) => {
377
+ if (typeof entry === "string") return entry.normalize("NFC")
378
+ if (entry._tag === "TreeArtifact") return { ...entry, path: entry.path.normalize("NFC") }
379
+ return {
380
+ ...entry,
381
+ include: sortStrings(entry.include),
382
+ ...(entry.exclude === undefined ? {} : { exclude: sortStrings(entry.exclude) })
383
+ }
384
+ })
385
+ const writeSet = [...new Map(normalizedWrites.map((entry) => [writeEntryKey(entry), entry])).entries()]
386
+ .sort(([left], [right]) => compareCodeUnits(left, right))
387
+ .map(([, entry]) => entry)
388
+ return {
389
+ readSet,
390
+ writeSet,
391
+ ...(hermetic.removes === undefined ? {} : { removes: sortStrings(hermetic.removes) }),
392
+ boundaryMode: hermetic.boundaryMode
393
+ }
394
+ }
395
+
396
+ const decodeKey = Schema.decodeUnknownEffect(DerivedKey)
397
+
398
+ /**
399
+ * Fingerprint for binding one execution to its runtime environment, not an
400
+ * action dispatch key. Uses the same normalization as content/dispatch keys.
401
+ * An omitted environment is distinct from declared-empty and run-scoped ones.
402
+ * @since 1.0.0
403
+ * @category constructors
404
+ */
405
+ export const environmentIdentity = (
406
+ environment?: EnvironmentIdentity
407
+ ): Effect.Effect<StoredKey, KeyMaterialError | Schema.SchemaError, Crypto.Crypto> =>
408
+ Effect.gen(function*() {
409
+ const captured = environment === undefined ? undefined : yield* decodeEnvironment(environment)
410
+ return yield* decodeKey({
411
+ kind: "execution-environment/v1",
412
+ environment: captured === undefined ? null : normalizeEnvironment(captured)
413
+ })
414
+ })
415
+
416
+ /**
417
+ * Produces a cross-run reusable key for a sealed or hermetic step. Set-like
418
+ * declarations are normalized before serialization; write declarations remain
419
+ * part of the identity even when the step writes nothing.
420
+ *
421
+ * @since 0.1.0
422
+ * @category constructors
423
+ * @slop
424
+ */
425
+ export const content = (
426
+ identity: ContentIdentity
427
+ ): Effect.Effect<StepKey, KeyMaterialError | Schema.SchemaError, Crypto.Crypto> =>
428
+ Effect.gen(function*() {
429
+ const environment = identity.environment === undefined ? undefined : yield* decodeEnvironment(identity.environment)
430
+ return yield* decodeKey({
431
+ kind: "content",
432
+ body: identity.body,
433
+ inputs: normalizeInputs(identity.inputs),
434
+ layers: sortStrings(identity.layers),
435
+ capabilities: normalizeCapabilities(identity.capabilities),
436
+ ...(environment === undefined ? {} : { environment: normalizeEnvironment(environment) }),
437
+ ...(identity.hermetic === undefined ? {} : { hermetic: normalizeHermetic(identity.hermetic) })
438
+ })
439
+ })
440
+
441
+ /**
442
+ * Produces a run-local ordinal key for compensable, irreversible, or unsealed
443
+ * work. These keys intentionally cannot be reused across runs.
444
+ *
445
+ * @since 0.1.0
446
+ * @category constructors
447
+ * @slop
448
+ */
449
+ export const ordinal = (
450
+ identity: OrdinalIdentity
451
+ ): Effect.Effect<StepKey, Schema.SchemaError, Crypto.Crypto> => decodeKey({ kind: "ordinal", ...identity })
452
+
453
+ /**
454
+ * Resolves graph-local references to dependency digests and constructs a
455
+ * sealed content key. Use {@link planIdentity} to compile any effect tier.
456
+ *
457
+ * This is the handoff the Flow Builder Brief describes: the planner hands over
458
+ * a topologically ordered sequence of `{ nodeId, material }`, and for each one
459
+ * the compiler substitutes every `Ref`/`Pending` with the *already computed
460
+ * key of the referenced node*. Structural node ids are lookup addresses only
461
+ * and never enter the hashed value — rename a node and nothing re-keys; change
462
+ * what a node consumes and everything downstream of it does.
463
+ *
464
+ * @since 0.1.0
465
+ * @category constructors
466
+ * @slop
467
+ */
468
+ export const fromKeyMaterial = (
469
+ material: KeyMaterial.KeyMaterial,
470
+ dependencyDigests: Readonly<Record<string, string>>
471
+ ): Effect.Effect<StepKey, KeyMaterialError | Schema.SchemaError, Crypto.Crypto> => {
472
+ if (material.kind !== "sealed") {
473
+ return Effect.fail(
474
+ new KeyMaterialError({
475
+ code: "non_content_material",
476
+ message: `Cannot create a content key for ${material.kind} material`
477
+ })
478
+ )
479
+ }
480
+ return materialIdentity(material, dependencyDigests).pipe(Effect.flatMap(content))
481
+ }
482
+
483
+ /**
484
+ * Identifies a declaration in a plan, independently of whether its execution
485
+ * can be cached. Sealed material retains its existing content-key format;
486
+ * compensable and irreversible material use a distinct, tier-bearing plan
487
+ * namespace. These fingerprints bind approvals, not cross-run result reuse.
488
+ * Dispatch non-cacheable work under a run-local {@link ordinal} instead.
489
+ *
490
+ * @since 1.0.0
491
+ * @category constructors
492
+ */
493
+ export const planIdentity = (
494
+ material: KeyMaterial.KeyMaterial,
495
+ dependencyDigests: Readonly<Record<string, string>>
496
+ ): Effect.Effect<StepKey, KeyMaterialError | Schema.SchemaError, Crypto.Crypto> =>
497
+ material.kind === "sealed" ?
498
+ fromKeyMaterial(material, dependencyDigests) :
499
+ materialIdentity(material, dependencyDigests).pipe(Effect.flatMap((identity) =>
500
+ decodeKey({
501
+ kind: "plan-declaration",
502
+ tier: material.kind,
503
+ body: identity.body,
504
+ inputs: normalizeInputs(identity.inputs),
505
+ layers: sortStrings(identity.layers),
506
+ capabilities: normalizeCapabilities(identity.capabilities)
507
+ })
508
+ ))
509
+
510
+ /**
511
+ * Reads the entry a graph reference names. Only an own data property counts:
512
+ * an absent name or an accessor is a missing dependency, and no getter runs.
513
+ * `label` names what the record holds, as each refusal says it.
514
+ *
515
+ * @private
516
+ */
517
+ const ownDataProperty = (
518
+ record: Readonly<Record<string, unknown>>,
519
+ from: string,
520
+ label: "Digest" | "Settled result"
521
+ ): Effect.Effect<unknown, KeyMaterialError> => {
522
+ const descriptor = Object.getOwnPropertyDescriptor(record, from)
523
+ if (descriptor === undefined) {
524
+ return Effect.fail(
525
+ new KeyMaterialError({
526
+ code: "missing_dependency",
527
+ message: `Missing ${label.toLowerCase()} for graph dependency ${from}`
528
+ })
529
+ )
530
+ }
531
+ if (!("value" in descriptor)) {
532
+ return Effect.fail(
533
+ new KeyMaterialError({
534
+ code: "missing_dependency",
535
+ message: `${label} for graph dependency ${from} must be a data property`
536
+ })
537
+ )
538
+ }
539
+ return Effect.succeed(descriptor.value)
540
+ }
541
+
542
+ /**
543
+ * The positional inputs both identities hash. A `Literal` contributes its raw
544
+ * value, so `normalizeInputs` applies the one literal wrap; `resolve` supplies
545
+ * the digest for each `Ref` and `Pending`, tagged with the reference variant.
546
+ *
547
+ * @private
548
+ */
549
+ const resolveInputs = <E, R>(
550
+ material: KeyMaterial.KeyMaterial,
551
+ resolve: (reference: Exclude<KeyMaterial.InputRef, { readonly _tag: "Literal" }>) => Effect.Effect<string, E, R>
552
+ ): Effect.Effect<Record<string, unknown>, E, R> =>
553
+ Effect.gen(function*() {
554
+ const inputs: Record<string, unknown> = {}
555
+ for (let index = 0; index < material.inputs.length; index++) {
556
+ const input = material.inputs[index]!
557
+ if (input._tag === "Literal") {
558
+ inputs[String(index)] = input.value
559
+ continue
560
+ }
561
+ const digest = yield* resolve(input)
562
+ inputs[String(index)] = input._tag === "Pending"
563
+ ? digestInput(digest, { reference: "pending" })
564
+ : input.path.length > 0
565
+ ? digestInput(digest, { reference: "ref-projected", path: input.path })
566
+ : digestInput(digest, { reference: "ref" })
567
+ }
568
+ return inputs
569
+ })
570
+
571
+ /** Resolves plan references once for both content and non-cacheable declarations. */
572
+ const materialIdentity = (
573
+ material: KeyMaterial.KeyMaterial,
574
+ dependencyDigests: Readonly<Record<string, string>>
575
+ ): Effect.Effect<ContentIdentity, KeyMaterialError> =>
576
+ resolveInputs(
577
+ material,
578
+ (reference) =>
579
+ Effect.flatMap(ownDataProperty(dependencyDigests, reference.from, "Digest"), (digest) =>
580
+ typeof digest === "string" ? Effect.succeed(digest) : Effect.fail(
581
+ new KeyMaterialError({
582
+ code: "missing_dependency",
583
+ message: `Digest for graph dependency ${reference.from} must be a string`
584
+ })
585
+ ))
586
+ ).pipe(Effect.map((inputs) => ({
587
+ body: materialBody(material),
588
+ inputs,
589
+ layers: material.layers,
590
+ capabilities: { declared: material.capabilities }
591
+ })))
592
+
593
+ /**
594
+ * The node's own declaration, hashed. Shared by {@link planIdentity},
595
+ * {@link fromKeyMaterial}, and {@link dispatchIdentity} so derivations cannot drift about what
596
+ * "the node itself" means.
597
+ *
598
+ * @private
599
+ */
600
+ const materialBody = (material: KeyMaterial.KeyMaterial) => ({
601
+ version: material.version,
602
+ declaration: material.body,
603
+ ...(material.nondeterministic === undefined ? {} : { nondeterministic: material.nondeterministic }),
604
+ ...(material.effects === undefined ? {} : { effects: material.effects }),
605
+ ...(material.placement === undefined ? {} : { placement: material.placement })
606
+ })
607
+
608
+ /**
609
+ * The digest a `Pending` input contributes to a dispatch key. It is a
610
+ * constant, and that is the point: `Pending` is an ordering reference, and
611
+ * ordering does not change what a node consumes, so it must not change what
612
+ * the node caches under. The string is self-describing, so a reader of a
613
+ * hashed payload can see that no upstream value was folded.
614
+ *
615
+ * @private
616
+ */
617
+ const orderingOnly = "ordering-only"
618
+
619
+ /**
620
+ * Projects a settled result along a `Ref` path. Only own data properties
621
+ * resolve, so a missing, inherited, or accessor segment yields `undefined`
622
+ * without invoking a getter. This is a stable, distinct value: a projection that
623
+ * walks off the end of a result is a fact about the graph, not a failure.
624
+ * `undefined` drops out of the canonical form, so it hashes distinctly from
625
+ * every JSON value including `null`.
626
+ *
627
+ * Exported because it is the ONE projection semantics for the value channel:
628
+ * {@link dispatchIdentity} digests what this returns, so every consumer that
629
+ * resolves a `Ref` at execution time must resolve it the same way, or two
630
+ * inputs that key identically could be consumed differently — a stale-hit
631
+ * vector.
632
+ *
633
+ * @since 0.1.0
634
+ * @category utilities
635
+ * @slop
636
+ */
637
+ export const project = (value: unknown, path: ReadonlyArray<string>): unknown => {
638
+ let current = value
639
+ for (const segment of path) {
640
+ if (typeof current !== "object" || current === null) return undefined
641
+ const descriptor = Object.getOwnPropertyDescriptor(current, segment)
642
+ if (descriptor === undefined || !("value" in descriptor)) return undefined
643
+ current = descriptor.value
644
+ }
645
+ return current
646
+ }
647
+
648
+ /**
649
+ * The key a dispatch is *cached* under, as distinct from the plan key a node
650
+ * is *identified* by.
651
+ *
652
+ * A plan key folds the resolved keys of every upstream node, transitively, so
653
+ * an edit anywhere upstream re-keys everything below it — even when the edited
654
+ * node's output value is byte for byte what it was before. Bazel does not work
655
+ * that way: `ActionCacheChecker` keys an action by the *content* of its
656
+ * inputs, so an unchanged output stops invalidation dead. The file channel in
657
+ * flows already had that property through the measured boundary digests; the
658
+ * JSON value channel did not, and this is what gives it one.
659
+ *
660
+ * The derivation folds the node's OWN material — body, version, effects,
661
+ * placement, layers, capabilities — and never an upstream key. Each material
662
+ * input contributes content instead: a `Literal` its value, a `Ref` the digest
663
+ * of the settled result of `from` projected along `path`, and a `Pending`
664
+ * nothing beyond its tag. The measured hermetic boundary is folded unchanged.
665
+ *
666
+ * `results` must hold every dependency the material names. The scheduler's
667
+ * halt rule guarantees it: a dependent of failed or skipped work never
668
+ * dispatches, so a `Ref` always resolves against a success.
669
+ *
670
+ * @since 0.1.0
671
+ * @category constructors
672
+ * @slop
673
+ */
674
+ export const dispatchIdentity = (options: {
675
+ readonly material: KeyMaterial.KeyMaterial
676
+ /** The settled output value of each dependency, by node id. */
677
+ readonly results: Readonly<Record<string, unknown>>
678
+ readonly hermetic: NonNullable<ContentIdentity["hermetic"]>
679
+ /** The engine-resolved execution environment this dispatch runs under. */
680
+ readonly environment?: EnvironmentIdentity | undefined
681
+ /** Reuses projected-value digests while the corresponding settled values remain immutable. */
682
+ readonly digestMemo?: DigestMemo | undefined
683
+ }): Effect.Effect<StepKey, KeyMaterialError | Schema.SchemaError, Crypto.Crypto> =>
684
+ Effect.gen(function*() {
685
+ const material = options.material
686
+ if (material.kind !== "sealed") {
687
+ return yield* new KeyMaterialError({
688
+ code: "non_content_material",
689
+ message: `Cannot create a dispatch key for ${material.kind} material`
690
+ })
691
+ }
692
+ const environment = options.environment === undefined ? undefined : yield* decodeEnvironment(options.environment)
693
+ const inputs = yield* resolveInputs(material, (reference) =>
694
+ reference._tag === "Pending"
695
+ ? Effect.succeed(orderingOnly)
696
+ : Effect.flatMap(ownDataProperty(options.results, reference.from, "Settled result"), (settled) => {
697
+ const compute = decodeKey({ kind: "input-value", value: project(settled, reference.path) })
698
+ return options.digestMemo === undefined
699
+ ? compute
700
+ : options.digestMemo.digest(reference.from, reference.path, compute)
701
+ }))
702
+ return yield* content({
703
+ body: materialBody(material),
704
+ inputs,
705
+ layers: material.layers,
706
+ capabilities: { declared: material.capabilities },
707
+ environment,
708
+ hermetic: options.hermetic
709
+ })
710
+ })