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

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 (209) hide show
  1. package/CHANGELOG.md +185 -0
  2. package/LICENSE +21 -0
  3. package/README.md +230 -2
  4. package/THIRD_PARTY_NOTICES.md +48 -0
  5. package/dist/cjs/CachePolicy.d.ts +131 -0
  6. package/dist/cjs/CachePolicy.d.ts.map +1 -0
  7. package/dist/cjs/CachePolicy.js +50 -0
  8. package/dist/cjs/CachePolicy.js.map +7 -0
  9. package/dist/cjs/Effects.d.ts +258 -0
  10. package/dist/cjs/Effects.d.ts.map +1 -0
  11. package/dist/cjs/Effects.js +76 -0
  12. package/dist/cjs/Effects.js.map +7 -0
  13. package/dist/cjs/FileSet.d.ts +366 -0
  14. package/dist/cjs/FileSet.d.ts.map +1 -0
  15. package/dist/cjs/FileSet.js +196 -0
  16. package/dist/cjs/FileSet.js.map +7 -0
  17. package/dist/cjs/GraphBuildError.d.ts +120 -0
  18. package/dist/cjs/GraphBuildError.d.ts.map +1 -0
  19. package/dist/cjs/GraphBuildError.js +71 -0
  20. package/dist/cjs/GraphBuildError.js.map +7 -0
  21. package/dist/cjs/KeyMaterial.d.ts +107 -0
  22. package/dist/cjs/KeyMaterial.d.ts.map +1 -0
  23. package/dist/cjs/KeyMaterial.js +64 -0
  24. package/dist/cjs/KeyMaterial.js.map +7 -0
  25. package/dist/cjs/Node.d.ts +677 -0
  26. package/dist/cjs/Node.d.ts.map +1 -0
  27. package/dist/cjs/Node.js +229 -0
  28. package/dist/cjs/Node.js.map +7 -0
  29. package/dist/cjs/Placement.d.ts +117 -0
  30. package/dist/cjs/Placement.d.ts.map +1 -0
  31. package/dist/cjs/Placement.js +46 -0
  32. package/dist/cjs/Placement.js.map +7 -0
  33. package/dist/cjs/Plan.d.ts +489 -0
  34. package/dist/cjs/Plan.d.ts.map +1 -0
  35. package/dist/cjs/Plan.js +511 -0
  36. package/dist/cjs/Plan.js.map +7 -0
  37. package/dist/cjs/PlanDiff.d.ts +77 -0
  38. package/dist/cjs/PlanDiff.d.ts.map +1 -0
  39. package/dist/cjs/PlanDiff.js +149 -0
  40. package/dist/cjs/PlanDiff.js.map +7 -0
  41. package/dist/cjs/Planned.d.ts +121 -0
  42. package/dist/cjs/Planned.d.ts.map +1 -0
  43. package/dist/cjs/Planned.js +72 -0
  44. package/dist/cjs/Planned.js.map +7 -0
  45. package/dist/cjs/Repetition.d.ts +31 -0
  46. package/dist/cjs/Repetition.d.ts.map +1 -0
  47. package/dist/cjs/Repetition.js +36 -0
  48. package/dist/cjs/Repetition.js.map +7 -0
  49. package/dist/cjs/Scheduling.d.ts +89 -0
  50. package/dist/cjs/Scheduling.d.ts.map +1 -0
  51. package/dist/cjs/Scheduling.js +76 -0
  52. package/dist/cjs/Scheduling.js.map +7 -0
  53. package/dist/cjs/StepKey.d.ts +354 -0
  54. package/dist/cjs/StepKey.d.ts.map +1 -0
  55. package/dist/cjs/StepKey.js +313 -0
  56. package/dist/cjs/StepKey.js.map +7 -0
  57. package/dist/cjs/index.d.ts +91 -0
  58. package/dist/cjs/index.d.ts.map +1 -0
  59. package/dist/cjs/index.js +59 -0
  60. package/dist/cjs/index.js.map +7 -0
  61. package/dist/cjs/internal/ConflictAnnotation.d.ts +59 -0
  62. package/dist/cjs/internal/ConflictAnnotation.d.ts.map +1 -0
  63. package/dist/cjs/internal/ConflictAnnotation.js +247 -0
  64. package/dist/cjs/internal/ConflictAnnotation.js.map +7 -0
  65. package/dist/cjs/internal/EffectCandidates.d.ts +19 -0
  66. package/dist/cjs/internal/EffectCandidates.d.ts.map +1 -0
  67. package/dist/cjs/internal/EffectCandidates.js +92 -0
  68. package/dist/cjs/internal/EffectCandidates.js.map +7 -0
  69. package/dist/cjs/internal/JsonMirror.d.ts +34 -0
  70. package/dist/cjs/internal/JsonMirror.d.ts.map +1 -0
  71. package/dist/cjs/internal/JsonMirror.js +195 -0
  72. package/dist/cjs/internal/JsonMirror.js.map +7 -0
  73. package/dist/cjs/internal/SchemaIdentity.d.ts +140 -0
  74. package/dist/cjs/internal/SchemaIdentity.d.ts.map +1 -0
  75. package/dist/cjs/internal/SchemaIdentity.js +718 -0
  76. package/dist/cjs/internal/SchemaIdentity.js.map +7 -0
  77. package/dist/cjs/internal/effects.d.ts +245 -0
  78. package/dist/cjs/internal/effects.d.ts.map +1 -0
  79. package/dist/cjs/internal/effects.js +290 -0
  80. package/dist/cjs/internal/effects.js.map +7 -0
  81. package/dist/cjs/internal/node.d.ts +526 -0
  82. package/dist/cjs/internal/node.d.ts.map +1 -0
  83. package/dist/cjs/internal/node.js +313 -0
  84. package/dist/cjs/internal/node.js.map +7 -0
  85. package/dist/cjs/package.json +1 -0
  86. package/dist/cjs/test/PlanFixtures.d.ts +127 -0
  87. package/dist/cjs/test/PlanFixtures.d.ts.map +1 -0
  88. package/dist/cjs/test/PlanFixtures.js +59 -0
  89. package/dist/cjs/test/PlanFixtures.js.map +7 -0
  90. package/dist/esm/CachePolicy.d.ts +131 -0
  91. package/dist/esm/CachePolicy.d.ts.map +1 -0
  92. package/dist/esm/CachePolicy.js +117 -0
  93. package/dist/esm/CachePolicy.js.map +1 -0
  94. package/dist/esm/Effects.d.ts +258 -0
  95. package/dist/esm/Effects.d.ts.map +1 -0
  96. package/dist/esm/Effects.js +178 -0
  97. package/dist/esm/Effects.js.map +1 -0
  98. package/dist/esm/FileSet.d.ts +366 -0
  99. package/dist/esm/FileSet.d.ts.map +1 -0
  100. package/dist/esm/FileSet.js +398 -0
  101. package/dist/esm/FileSet.js.map +1 -0
  102. package/dist/esm/GraphBuildError.d.ts +120 -0
  103. package/dist/esm/GraphBuildError.d.ts.map +1 -0
  104. package/dist/esm/GraphBuildError.js +145 -0
  105. package/dist/esm/GraphBuildError.js.map +1 -0
  106. package/dist/esm/KeyMaterial.d.ts +107 -0
  107. package/dist/esm/KeyMaterial.d.ts.map +1 -0
  108. package/dist/esm/KeyMaterial.js +92 -0
  109. package/dist/esm/KeyMaterial.js.map +1 -0
  110. package/dist/esm/Node.d.ts +677 -0
  111. package/dist/esm/Node.d.ts.map +1 -0
  112. package/dist/esm/Node.js +586 -0
  113. package/dist/esm/Node.js.map +1 -0
  114. package/dist/esm/Placement.d.ts +117 -0
  115. package/dist/esm/Placement.d.ts.map +1 -0
  116. package/dist/esm/Placement.js +65 -0
  117. package/dist/esm/Placement.js.map +1 -0
  118. package/dist/esm/Plan.d.ts +489 -0
  119. package/dist/esm/Plan.d.ts.map +1 -0
  120. package/dist/esm/Plan.js +718 -0
  121. package/dist/esm/Plan.js.map +1 -0
  122. package/dist/esm/PlanDiff.d.ts +77 -0
  123. package/dist/esm/PlanDiff.d.ts.map +1 -0
  124. package/dist/esm/PlanDiff.js +205 -0
  125. package/dist/esm/PlanDiff.js.map +1 -0
  126. package/dist/esm/Planned.d.ts +121 -0
  127. package/dist/esm/Planned.d.ts.map +1 -0
  128. package/dist/esm/Planned.js +147 -0
  129. package/dist/esm/Planned.js.map +1 -0
  130. package/dist/esm/Repetition.d.ts +31 -0
  131. package/dist/esm/Repetition.d.ts.map +1 -0
  132. package/dist/esm/Repetition.js +24 -0
  133. package/dist/esm/Repetition.js.map +1 -0
  134. package/dist/esm/Scheduling.d.ts +89 -0
  135. package/dist/esm/Scheduling.d.ts.map +1 -0
  136. package/dist/esm/Scheduling.js +82 -0
  137. package/dist/esm/Scheduling.js.map +1 -0
  138. package/dist/esm/StepKey.d.ts +354 -0
  139. package/dist/esm/StepKey.d.ts.map +1 -0
  140. package/dist/esm/StepKey.js +503 -0
  141. package/dist/esm/StepKey.js.map +1 -0
  142. package/dist/esm/index.d.ts +91 -0
  143. package/dist/esm/index.d.ts.map +1 -0
  144. package/dist/esm/index.js +91 -0
  145. package/dist/esm/index.js.map +1 -0
  146. package/dist/esm/internal/ConflictAnnotation.d.ts +59 -0
  147. package/dist/esm/internal/ConflictAnnotation.d.ts.map +1 -0
  148. package/dist/esm/internal/ConflictAnnotation.js +332 -0
  149. package/dist/esm/internal/ConflictAnnotation.js.map +1 -0
  150. package/dist/esm/internal/EffectCandidates.d.ts +19 -0
  151. package/dist/esm/internal/EffectCandidates.d.ts.map +1 -0
  152. package/dist/esm/internal/EffectCandidates.js +86 -0
  153. package/dist/esm/internal/EffectCandidates.js.map +1 -0
  154. package/dist/esm/internal/JsonMirror.d.ts +34 -0
  155. package/dist/esm/internal/JsonMirror.d.ts.map +1 -0
  156. package/dist/esm/internal/JsonMirror.js +204 -0
  157. package/dist/esm/internal/JsonMirror.js.map +1 -0
  158. package/dist/esm/internal/SchemaIdentity.d.ts +140 -0
  159. package/dist/esm/internal/SchemaIdentity.d.ts.map +1 -0
  160. package/dist/esm/internal/SchemaIdentity.js +1002 -0
  161. package/dist/esm/internal/SchemaIdentity.js.map +1 -0
  162. package/dist/esm/internal/effects.d.ts +245 -0
  163. package/dist/esm/internal/effects.d.ts.map +1 -0
  164. package/dist/esm/internal/effects.js +459 -0
  165. package/dist/esm/internal/effects.js.map +1 -0
  166. package/dist/esm/internal/node.d.ts +526 -0
  167. package/dist/esm/internal/node.d.ts.map +1 -0
  168. package/dist/esm/internal/node.js +565 -0
  169. package/dist/esm/internal/node.js.map +1 -0
  170. package/dist/esm/test/PlanFixtures.d.ts +127 -0
  171. package/dist/esm/test/PlanFixtures.d.ts.map +1 -0
  172. package/dist/esm/test/PlanFixtures.js +60 -0
  173. package/dist/esm/test/PlanFixtures.js.map +1 -0
  174. package/docs/README.md +167 -0
  175. package/docs/api.md +1485 -0
  176. package/docs/concepts/authoring-ast.md +204 -0
  177. package/docs/concepts/effects-and-conflicts.md +135 -0
  178. package/docs/concepts/plan-value.md +168 -0
  179. package/docs/concepts/step-keys.md +179 -0
  180. package/docs/guides/author-a-node-graph.md +182 -0
  181. package/docs/guides/compile-a-plan.md +146 -0
  182. package/docs/guides/declare-file-effects.md +143 -0
  183. package/docs/guides/diff-two-plans.md +310 -0
  184. package/docs/installation.md +75 -0
  185. package/docs/quickstart.md +128 -0
  186. package/docs/testing.md +73 -0
  187. package/docs/troubleshooting.md +314 -0
  188. package/package.json +255 -3
  189. package/src/CachePolicy.ts +143 -0
  190. package/src/Effects.ts +302 -0
  191. package/src/FileSet.ts +493 -0
  192. package/src/GraphBuildError.ts +156 -0
  193. package/src/KeyMaterial.ts +111 -0
  194. package/src/Node.ts +969 -0
  195. package/src/Placement.ts +99 -0
  196. package/src/Plan.ts +911 -0
  197. package/src/PlanDiff.ts +244 -0
  198. package/src/Planned.ts +208 -0
  199. package/src/Repetition.ts +33 -0
  200. package/src/Scheduling.ts +154 -0
  201. package/src/StepKey.ts +736 -0
  202. package/src/index.ts +103 -0
  203. package/src/internal/ConflictAnnotation.ts +374 -0
  204. package/src/internal/EffectCandidates.ts +89 -0
  205. package/src/internal/JsonMirror.ts +255 -0
  206. package/src/internal/SchemaIdentity.ts +1096 -0
  207. package/src/internal/effects.ts +543 -0
  208. package/src/internal/node.ts +859 -0
  209. package/src/test/PlanFixtures.ts +76 -0
@@ -0,0 +1,143 @@
1
+ /**
2
+ * The caller-declared cache policy: how far a recorded sealed result may
3
+ * travel, and how long it stays servable.
4
+ *
5
+ * This is the ONE cache-policy model. `@smthrs/flow` publishes it as
6
+ * `CacheEnvironment.CachePolicy` and the engine reads it off a dispatched
7
+ * action; `@smthrs/patterns` declares it over a flow as `WithCache.Policy`. It
8
+ * used to be two declarations in those two packages, tied together by nothing
9
+ * but a re-typed identifier string, so a field added on one side was invisible
10
+ * to the other. It lives here because `@smthrs/plan` is the package both
11
+ * already depend on.
12
+ *
13
+ * The annotation key still reads `"@smthrs/flow/Action/CachePolicy"`.
14
+ * `@smthrs/engine-store`'s `ActionPersistence` reads the policy off stored and
15
+ * dispatched actions by that identifier, so retagging it would make every
16
+ * policy already written invisible at dispatch. The identifier is a wire
17
+ * value, not a location.
18
+ *
19
+ * @since 1.0.0-rc.0
20
+ */
21
+
22
+ import * as Context from "effect/Context"
23
+ import * as Option from "effect/Option"
24
+ import * as Schema from "effect/Schema"
25
+
26
+ /**
27
+ * How far a recorded sealed result may travel.
28
+ *
29
+ * `shared` is the content-addressed default: the key names the inputs and the
30
+ * environment, so any run on any host that would have produced the same bytes
31
+ * may reuse the row. `run` and `flow` narrow that on purpose — a step whose
32
+ * result is only meaningful inside one execution, or inside one flow, folds
33
+ * that identity into its key so a sibling never reads it.
34
+ *
35
+ * The old scope vocabulary was `run | workflow | global`; `flow` and `shared`
36
+ * are the same three levels named after the current concepts.
37
+ *
38
+ * @category models
39
+ * @since 1.0.0-rc.0
40
+ */
41
+ export const CacheScope = Schema.Literals(["run", "flow", "shared"])
42
+
43
+ /**
44
+ * The value form of {@link CacheScope}.
45
+ *
46
+ * @category models
47
+ * @since 1.0.0-rc.0
48
+ */
49
+ export type CacheScope = typeof CacheScope.Type
50
+
51
+ /**
52
+ * A positive whole number of milliseconds.
53
+ *
54
+ * @private
55
+ * @since 1.0.0-rc.0
56
+ */
57
+ const PositiveMillis = Schema.Int.check(Schema.isGreaterThan(0))
58
+
59
+ /**
60
+ * The caller's declaration about the decay and the reach of a sealed result.
61
+ *
62
+ * `ttlMs` bounds the age of a row the engine may serve: past it the dispatch
63
+ * executes again and journals `cache-expired`, so the refusal is durable
64
+ * evidence a replay reads rather than a fresh clock reading. `scope` decides
65
+ * what the key names besides the inputs.
66
+ *
67
+ * Both fields are optional and both defaults are the pre-policy behavior: no
68
+ * age bound, and the reach the composition's cache environment already
69
+ * granted.
70
+ *
71
+ * @category schemas
72
+ * @since 1.0.0-rc.0
73
+ */
74
+ export const CachePolicy = Schema.Struct({
75
+ ttlMs: Schema.optionalKey(PositiveMillis),
76
+ scope: Schema.optionalKey(CacheScope)
77
+ })
78
+
79
+ /**
80
+ * The value form of {@link CachePolicy}.
81
+ *
82
+ * @category models
83
+ * @since 1.0.0-rc.0
84
+ */
85
+ export type CachePolicy = typeof CachePolicy.Type
86
+
87
+ /**
88
+ * Annotation key carrying a declaration's {@link CachePolicy}.
89
+ *
90
+ * It is an annotation, not a field on the declaration, for the same reason
91
+ * placement and effects are: the policy is data that travels with the
92
+ * declaration and is read by whoever executes it, and adding it changes no
93
+ * existing call site.
94
+ *
95
+ * @category annotations
96
+ * @since 1.0.0-rc.0
97
+ */
98
+ export const CachePolicyAnnotation = Context.Service<CachePolicy>("@smthrs/flow/Action/CachePolicy")
99
+
100
+ /**
101
+ * Reads the cache policy an annotation bag carries, or `undefined` when it
102
+ * carries none.
103
+ *
104
+ * @category getters
105
+ * @since 1.0.0-rc.0
106
+ */
107
+ export const cachePolicyOf = (annotations: Context.Context<never>): CachePolicy | undefined =>
108
+ Option.getOrUndefined(Context.getOption(annotations, CachePolicyAnnotation))
109
+
110
+ /**
111
+ * Attaches a cache policy to a declaration that can carry annotations.
112
+ *
113
+ * The declaration is not mutated: `annotate` answers a new one carrying the
114
+ * policy, which is what a plan captures. It is written against the `annotate`
115
+ * method rather than against an action or a flow type so the one declaration
116
+ * serves both, which is what `@smthrs/flow` publishes as
117
+ * `CacheEnvironment.withCache`.
118
+ *
119
+ * @example
120
+ * ```ts
121
+ * import { CachePolicy } from "@smthrs/plan"
122
+ * import { Action } from "@smthrs/flow"
123
+ * import * as Effect from "effect/Effect"
124
+ * import * as Schema from "effect/Schema"
125
+ *
126
+ * const compile = CachePolicy.annotate(
127
+ * Action.make({
128
+ * name: "build/compile",
129
+ * success: Schema.String,
130
+ * tier: "sealed",
131
+ * execute: Effect.succeed("dist/server.js")
132
+ * }),
133
+ * { ttlMs: 60_000, scope: "shared" }
134
+ * )
135
+ * ```
136
+ *
137
+ * @category combinators
138
+ * @since 1.0.0-rc.0
139
+ */
140
+ export const annotate = <A extends { annotate: (key: typeof CachePolicyAnnotation, value: CachePolicy) => A }>(
141
+ declaration: A,
142
+ policy: CachePolicy
143
+ ): A => declaration.annotate(CachePolicyAnnotation, policy)
package/src/Effects.ts ADDED
@@ -0,0 +1,302 @@
1
+ /**
2
+ * Pure effect declarations used to describe flow read and write envelopes.
3
+ *
4
+ * This is the ONE effect-envelope model. `@smthrs/flow` enforces it while it
5
+ * builds a graph, `@smthrs/core` re-exports it, and `@smthrs/patterns` reads it
6
+ * to intersect a decorator's declaration with the flow it wraps. It lives here
7
+ * because `@smthrs/plan` is the lowest package all three depend on, beside the
8
+ * `tier` and `onConflict` vocabularies a plan already speaks.
9
+ *
10
+ * Two layers: the declaration API, which a flow author and a catalog use, and
11
+ * the prepared matching API below it, which a graph builder uses to check one
12
+ * envelope against many steps without re-reading the envelope each time.
13
+ *
14
+ * Governing contract: `packages/smithers/flows/plan/docs/api.md`, published as
15
+ * https://smithers.sh/docs/reference/api/plan.
16
+ *
17
+ * @since 0.0.0
18
+ */
19
+
20
+ import * as Context from "effect/Context"
21
+ import * as Schema from "effect/Schema"
22
+ import * as Index from "./internal/effects.ts"
23
+
24
+ /**
25
+ * How reversible the work beneath a declaration is.
26
+ *
27
+ * This is the ONE tier vocabulary. `KeyMaterial.kind` is this schema, and so
28
+ * is `@smthrs/flow`'s `Action.Tier`: an action's durability and a declared
29
+ * envelope's ceiling are the same three words, and a fourth spelling of them
30
+ * in one of the three places would fork identity silently.
31
+ *
32
+ * @category schemas
33
+ * @since 1.0.0-rc.0
34
+ */
35
+ export const Tier = Schema.Literals(["sealed", "compensable", "irreversible"])
36
+
37
+ /**
38
+ * The value form of {@link Tier}.
39
+ *
40
+ * @category models
41
+ * @since 1.0.0-rc.0
42
+ */
43
+ export type Tier = typeof Tier.Type
44
+
45
+ /**
46
+ * How hermetic the work beneath a declaration is.
47
+ *
48
+ * This is the ONE mode vocabulary: the two words were written out in
49
+ * `Declaration`, in `MakeOptions`, and twice more inside the matcher.
50
+ *
51
+ * @category schemas
52
+ * @since 1.0.0-rc.0
53
+ */
54
+ export const Mode = Schema.Literals(["hermetic", "expected"])
55
+
56
+ /**
57
+ * The value form of {@link Mode}.
58
+ *
59
+ * @category models
60
+ * @since 1.0.0-rc.0
61
+ */
62
+ export type Mode = typeof Mode.Type
63
+
64
+ /**
65
+ * What a plan does when two nodes write the same path.
66
+ *
67
+ * This is the ONE conflict vocabulary: `Declaration.onConflict` declares it and
68
+ * `Plan.PairStrategy` is this same schema, resolved per pair by
69
+ * `Plan.compile`.
70
+ *
71
+ * @category schemas
72
+ * @since 1.0.0-rc.0
73
+ */
74
+ export const ConflictStrategy = Schema.Literals(["serialize", "lane", "fail"])
75
+
76
+ /**
77
+ * The value form of {@link ConflictStrategy}.
78
+ *
79
+ * @category models
80
+ * @since 1.0.0-rc.0
81
+ */
82
+ export type ConflictStrategy = typeof ConflictStrategy.Type
83
+
84
+ /**
85
+ * The annotation key a flow's declared effect ENVELOPE is carried under.
86
+ *
87
+ * One key, here, because two graph builders read it: `@smthrs/flow`'s
88
+ * `Graph.build` enforces the envelope it names, and `@smthrs/core` re-exports
89
+ * this key as `Annotations.Effects`. While they were separate keys a flow
90
+ * annotated for one builder was invisible to the other.
91
+ *
92
+ * It is a `Context` key, not hashed material: annotating a flow with an
93
+ * envelope changes what a build ADMITS, and the declaration reaches identity
94
+ * only through `KeyMaterial.kind`.
95
+ *
96
+ * @category annotations
97
+ * @since 1.0.0-rc.0
98
+ */
99
+ export const Envelope = Context.Service<Declaration>("@smthrs/plan/Effects/Envelope")
100
+
101
+ /**
102
+ * The prepared matching API: what a graph builder needs to check one envelope
103
+ * against every step it encloses, and to find the write overlaps between two
104
+ * declarations, at the cost the public functions above pay once rather than
105
+ * once per step.
106
+ *
107
+ * {@link prepareEnvelope} reads an envelope once into the form
108
+ * {@link narrowPrepared} answers from, which is what makes a wide envelope cost
109
+ * its size per build rather than per node. {@link indexPaths},
110
+ * {@link rankPaths} and {@link overlapRanks} are the same decomposition for
111
+ * write overlap, which {@link overlaps} wraps for the pairwise case.
112
+ * {@link boundedEffects} snapshots a declaration into builder-owned data,
113
+ * refusing through the caller's own error before the copy grows past its limit,
114
+ * and {@link maximumPathLength} and {@link maximumGlobs} are the per-path bounds
115
+ * that refusal enforces.
116
+ *
117
+ * @category matching
118
+ * @since 1.0.0-rc.0
119
+ */
120
+ export type { PathIndex, PreparedEnvelope, Ranked } from "./internal/effects.ts"
121
+
122
+ /**
123
+ * The prepared matching API. See the type re-exports above for what each member
124
+ * is for.
125
+ *
126
+ * @category matching
127
+ * @since 1.0.0-rc.0
128
+ */
129
+ export {
130
+ boundedEffects,
131
+ indexPaths,
132
+ maximumGlobs,
133
+ maximumPathLength,
134
+ narrowPrepared,
135
+ overlapRanks,
136
+ prepareEnvelope,
137
+ rankPaths
138
+ } from "./internal/effects.ts"
139
+
140
+ /**
141
+ * A normalized description of the resources a flow or step may read and
142
+ * write. Build one with {@link make}.
143
+ *
144
+ * @category models
145
+ * @since 0.0.0
146
+ */
147
+ export interface Declaration {
148
+ /** Readable paths or envelope patterns, sorted and duplicate-free. */
149
+ readonly reads: ReadonlyArray<string>
150
+ /** Writable paths or envelope patterns, sorted and duplicate-free. */
151
+ readonly writes: ReadonlyArray<string>
152
+ /** `hermetic` or `expected` enforcement. */
153
+ readonly mode: Mode
154
+ /** What a plan does when another node writes an overlapping path. */
155
+ readonly onConflict: ConflictStrategy
156
+ /** The effect tier. Absent means `sealed`. */
157
+ readonly tier?: Tier | undefined
158
+ }
159
+
160
+ /**
161
+ * Input accepted by {@link make}. Iterables are normalized into sorted,
162
+ * duplicate-free arrays so declarations are deterministic key material.
163
+ * Envelope entries and declared paths must already be path-normalized: no
164
+ * separator or dot-segment rewriting is performed. A declared path containing
165
+ * a whole `.` or `..` segment is never covered and therefore surfaces from
166
+ * {@link narrow} as an `effect_outside_envelope` diagnostic.
167
+ *
168
+ * @category models
169
+ * @since 0.0.0
170
+ */
171
+ export interface MakeOptions {
172
+ readonly reads: Iterable<string>
173
+ readonly writes: Iterable<string>
174
+ readonly mode: Mode
175
+ readonly onConflict: ConflictStrategy
176
+ readonly tier?: Tier | undefined
177
+ }
178
+
179
+ /**
180
+ * A result of checking that a step declaration narrows a flow envelope.
181
+ *
182
+ * On failure, `code` names the first rule broken, checked in this order:
183
+ * `effect_outside_envelope` (with the uncovered read and write paths, sorted),
184
+ * `effect_mode_widening`, then `effect_tier_widening` (both with empty
185
+ * `paths`).
186
+ *
187
+ * @category models
188
+ * @since 0.0.0
189
+ */
190
+ export type NarrowResult =
191
+ | { readonly ok: true }
192
+ | {
193
+ readonly ok: false
194
+ readonly code: "effect_outside_envelope" | "effect_mode_widening" | "effect_tier_widening"
195
+ readonly paths: ReadonlyArray<string>
196
+ }
197
+
198
+ const normalize = (paths: Iterable<string>): ReadonlyArray<string> => [...new Set(paths)].sort()
199
+
200
+ /**
201
+ * Constructs a deterministic effect declaration: `reads` and `writes` are
202
+ * collected, deduplicated, and sorted; an absent `tier` is omitted. Paths are
203
+ * not validated or rewritten.
204
+ *
205
+ * @param input - The paths, mode, conflict strategy, and optional tier.
206
+ * @returns The normalized declaration.
207
+ * @category constructors
208
+ * @since 0.0.0
209
+ */
210
+ export const make = (input: MakeOptions): Declaration => ({
211
+ reads: normalize(input.reads),
212
+ writes: normalize(input.writes),
213
+ mode: input.mode,
214
+ onConflict: input.onConflict,
215
+ ...(input.tier === undefined ? {} : { tier: input.tier })
216
+ })
217
+
218
+ /**
219
+ * Checks whether `path` is covered by an envelope entry.
220
+ *
221
+ * The grammar is exhaustive: an exact path matches itself; `*` and `**` match
222
+ * everything; `prefix*` matches by string prefix; and `prefix/**` matches
223
+ * `prefix/` and everything below it, but not bare `prefix`. This is
224
+ * intentionally not full minimatch syntax. Envelope entries and declared paths
225
+ * must be normalized. A path containing a whole `.` or `..` segment is never
226
+ * covered, so {@link narrow} reports it as `effect_outside_envelope` rather
227
+ * than silently accepting an escape.
228
+ *
229
+ * @param envelope - One envelope entry.
230
+ * @param path - One declared path.
231
+ * @returns `true` when the entry covers the path.
232
+ * @category predicates
233
+ * @since 0.0.0
234
+ */
235
+ export const covers = (envelope: string, path: string): boolean => {
236
+ if (Index.hasDotSegment(path)) return false
237
+ return envelope === path || (Index.isGlob(envelope) && path.startsWith(Index.globPrefix(envelope)))
238
+ }
239
+
240
+ /**
241
+ * Verifies that a step declaration stays within an enclosing flow envelope.
242
+ *
243
+ * Read and write paths must be covered independently. A step may tighten
244
+ * `expected` to `hermetic`, but cannot widen `hermetic` to `expected`.
245
+ * Effect tiers narrow from irreversible to compensable to sealed.
246
+ *
247
+ * The envelope's lists are prepared once: exact entries go into a set and
248
+ * covering patterns collapse to their outermost prefixes, sorted, so each step
249
+ * path costs one dot-segment scan, one lookup, and one binary search ending in
250
+ * one prefix comparison, whatever the envelope's width or how many of its
251
+ * patterns nest. A graph build prepares each envelope once for every node it
252
+ * encloses.
253
+ *
254
+ * @param envelope - The flow's declaration.
255
+ * @param step - The step's declaration.
256
+ * @returns `{ ok: true }`, or the first rule the step breaks.
257
+ * @category validation
258
+ * @since 0.0.0
259
+ */
260
+ export const narrow = (envelope: Declaration, step: Declaration): NarrowResult =>
261
+ Index.narrowPrepared(Index.prepareEnvelope(envelope), step)
262
+
263
+ /**
264
+ * Returns the concrete or narrower write declarations shared by two effect
265
+ * declarations. The result is sorted and duplicate-free.
266
+ *
267
+ * Two declarations of the same literal path always overlap, including a path
268
+ * {@link covers} refuses to match because it carries a `.` or `..` segment.
269
+ * Glob coverage stays strict for those paths, so an unnormalized declaration
270
+ * still escapes no envelope, but two writers naming the same unnormalized path
271
+ * are still detected as writing the same resource.
272
+ *
273
+ * The union of both declarations is indexed once: the distinct paths are
274
+ * sorted, each is scanned once for a dot segment, and each pattern's prefix
275
+ * is located by binary search. Exact paths then match through a merge of two
276
+ * rank lists and each covering pattern enumerates the other declaration's
277
+ * ranks inside its interval, so the cost is linear in the two declarations
278
+ * plus the matches, whatever the paths' lengths or how many patterns nest.
279
+ *
280
+ * @param a - One declaration.
281
+ * @param b - The other declaration.
282
+ * @returns The shared write paths; empty when the writes are disjoint.
283
+ * @category analysis
284
+ * @since 0.0.0
285
+ */
286
+ export const overlaps = (a: Declaration, b: Declaration): ReadonlyArray<string> => {
287
+ const indexed = Index.indexPaths([a.writes, b.writes])
288
+ return Index.overlapRanks(indexed, Index.rankPaths(indexed, a.writes), Index.rankPaths(indexed, b.writes))
289
+ .map((rank) => indexed.paths[rank]!)
290
+ }
291
+
292
+ /**
293
+ * Returns a sealed, hermetic copy of an effect declaration, normalized by
294
+ * {@link make}. Paths and `onConflict` are kept.
295
+ *
296
+ * @param declaration - The declaration to copy.
297
+ * @returns The copy with `mode: "hermetic"` and `tier: "sealed"`.
298
+ * @category constructors
299
+ * @since 0.0.0
300
+ */
301
+ export const sealed = (declaration: Declaration): Declaration =>
302
+ make({ ...declaration, mode: "hermetic", tier: "sealed" })