@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/package.json CHANGED
@@ -1,6 +1,257 @@
1
1
  {
2
2
  "name": "@smthrs/plan",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "type": "module",
4
+ "version": "1.0.0-rc.4",
5
+ "license": "MIT",
6
+ "smthrs": {
7
+ "group": "engine"
8
+ },
9
+ "description": "The persisted flows plan: a keyed action graph, its append-only store, and its diff",
10
+ "homepage": "https://github.com/smithersai/smithers",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/smithersai/smithers.git",
14
+ "directory": "packages/smithers/flows/plan"
15
+ },
16
+ "bugs": {
17
+ "url": "https://github.com/smithersai/smithers/issues"
18
+ },
19
+ "tags": [
20
+ "typescript",
21
+ "effect",
22
+ "plan",
23
+ "graph"
24
+ ],
25
+ "keywords": [
26
+ "typescript",
27
+ "effect",
28
+ "plan",
29
+ "graph"
30
+ ],
31
+ "engines": {
32
+ "node": ">=26.4.0"
33
+ },
34
+ "sideEffects": [],
35
+ "exports": {
36
+ "./package.json": "./package.json",
37
+ ".": {
38
+ "import": {
39
+ "types": "./dist/esm/index.d.ts",
40
+ "default": "./dist/esm/index.js"
41
+ },
42
+ "require": {
43
+ "types": "./dist/cjs/index.d.ts",
44
+ "default": "./dist/cjs/index.js"
45
+ }
46
+ },
47
+ "./GraphBuildError": {
48
+ "import": {
49
+ "types": "./dist/esm/GraphBuildError.d.ts",
50
+ "default": "./dist/esm/GraphBuildError.js"
51
+ },
52
+ "require": {
53
+ "types": "./dist/cjs/GraphBuildError.d.ts",
54
+ "default": "./dist/cjs/GraphBuildError.js"
55
+ }
56
+ },
57
+ "./Node": {
58
+ "import": {
59
+ "types": "./dist/esm/Node.d.ts",
60
+ "default": "./dist/esm/Node.js"
61
+ },
62
+ "require": {
63
+ "types": "./dist/cjs/Node.d.ts",
64
+ "default": "./dist/cjs/Node.js"
65
+ }
66
+ },
67
+ "./Planned": {
68
+ "import": {
69
+ "types": "./dist/esm/Planned.d.ts",
70
+ "default": "./dist/esm/Planned.js"
71
+ },
72
+ "require": {
73
+ "types": "./dist/cjs/Planned.d.ts",
74
+ "default": "./dist/cjs/Planned.js"
75
+ }
76
+ },
77
+ "./internal/*": null,
78
+ "./*/index": null,
79
+ "./Effects": {
80
+ "import": {
81
+ "types": "./dist/esm/Effects.d.ts",
82
+ "default": "./dist/esm/Effects.js"
83
+ },
84
+ "require": {
85
+ "types": "./dist/cjs/Effects.d.ts",
86
+ "default": "./dist/cjs/Effects.js"
87
+ }
88
+ },
89
+ "./CachePolicy": {
90
+ "import": {
91
+ "types": "./dist/esm/CachePolicy.d.ts",
92
+ "default": "./dist/esm/CachePolicy.js"
93
+ },
94
+ "require": {
95
+ "types": "./dist/cjs/CachePolicy.d.ts",
96
+ "default": "./dist/cjs/CachePolicy.js"
97
+ }
98
+ },
99
+ "./Repetition": {
100
+ "import": {
101
+ "types": "./dist/esm/Repetition.d.ts",
102
+ "default": "./dist/esm/Repetition.js"
103
+ },
104
+ "require": {
105
+ "types": "./dist/cjs/Repetition.d.ts",
106
+ "default": "./dist/cjs/Repetition.js"
107
+ }
108
+ },
109
+ "./FileSet": {
110
+ "import": {
111
+ "types": "./dist/esm/FileSet.d.ts",
112
+ "default": "./dist/esm/FileSet.js"
113
+ },
114
+ "require": {
115
+ "types": "./dist/cjs/FileSet.d.ts",
116
+ "default": "./dist/cjs/FileSet.js"
117
+ }
118
+ },
119
+ "./KeyMaterial": {
120
+ "import": {
121
+ "types": "./dist/esm/KeyMaterial.d.ts",
122
+ "default": "./dist/esm/KeyMaterial.js"
123
+ },
124
+ "require": {
125
+ "types": "./dist/cjs/KeyMaterial.d.ts",
126
+ "default": "./dist/cjs/KeyMaterial.js"
127
+ }
128
+ },
129
+ "./Migrations": null,
130
+ "./Plan": {
131
+ "import": {
132
+ "types": "./dist/esm/Plan.d.ts",
133
+ "default": "./dist/esm/Plan.js"
134
+ },
135
+ "require": {
136
+ "types": "./dist/cjs/Plan.d.ts",
137
+ "default": "./dist/cjs/Plan.js"
138
+ }
139
+ },
140
+ "./Placement": {
141
+ "import": {
142
+ "types": "./dist/esm/Placement.d.ts",
143
+ "default": "./dist/esm/Placement.js"
144
+ },
145
+ "require": {
146
+ "types": "./dist/cjs/Placement.d.ts",
147
+ "default": "./dist/cjs/Placement.js"
148
+ }
149
+ },
150
+ "./PlanDiff": {
151
+ "import": {
152
+ "types": "./dist/esm/PlanDiff.d.ts",
153
+ "default": "./dist/esm/PlanDiff.js"
154
+ },
155
+ "require": {
156
+ "types": "./dist/cjs/PlanDiff.d.ts",
157
+ "default": "./dist/cjs/PlanDiff.js"
158
+ }
159
+ },
160
+ "./PlanStore": null,
161
+ "./StepKey": {
162
+ "import": {
163
+ "types": "./dist/esm/StepKey.d.ts",
164
+ "default": "./dist/esm/StepKey.js"
165
+ },
166
+ "require": {
167
+ "types": "./dist/cjs/StepKey.d.ts",
168
+ "default": "./dist/cjs/StepKey.js"
169
+ }
170
+ },
171
+ "./Scheduling": {
172
+ "import": {
173
+ "types": "./dist/esm/Scheduling.d.ts",
174
+ "default": "./dist/esm/Scheduling.js"
175
+ },
176
+ "require": {
177
+ "types": "./dist/cjs/Scheduling.d.ts",
178
+ "default": "./dist/cjs/Scheduling.js"
179
+ }
180
+ },
181
+ "./index": {
182
+ "import": {
183
+ "types": "./dist/esm/index.d.ts",
184
+ "default": "./dist/esm/index.js"
185
+ },
186
+ "require": {
187
+ "types": "./dist/cjs/index.d.ts",
188
+ "default": "./dist/cjs/index.js"
189
+ }
190
+ },
191
+ "./test/PlanFixtures": {
192
+ "import": {
193
+ "types": "./dist/esm/test/PlanFixtures.d.ts",
194
+ "default": "./dist/esm/test/PlanFixtures.js"
195
+ },
196
+ "require": {
197
+ "types": "./dist/cjs/test/PlanFixtures.d.ts",
198
+ "default": "./dist/cjs/test/PlanFixtures.js"
199
+ }
200
+ }
201
+ },
202
+ "files": [
203
+ "src/**/*.ts",
204
+ "dist/**/*.js",
205
+ "dist/**/*.js.map",
206
+ "dist/**/*.d.ts",
207
+ "dist/**/*.d.ts.map",
208
+ "dist/**/package.json",
209
+ "LICENSE",
210
+ "README.md",
211
+ "CHANGELOG.md",
212
+ "docs/"
213
+ ],
214
+ "publishConfig": {
215
+ "access": "public",
216
+ "provenance": true,
217
+ "tag": "next"
218
+ },
219
+ "dependencies": {
220
+ "@smthrs/crypto": "1.0.0-rc.4",
221
+ "@smthrs/keys": "1.0.0-rc.4",
222
+ "@smthrs/canonical": "1.0.0-rc.4"
223
+ },
224
+ "devDependencies": {
225
+ "@effect/language-service": "0.87.2",
226
+ "@effect/platform-node": "4.0.0-rc.115",
227
+ "@effect/vitest": "4.0.0-rc.115",
228
+ "@eslint/js": "9.39.5",
229
+ "@types/node": "26.4.1",
230
+ "@vitest/coverage-v8": "5.0.0",
231
+ "dprint": "0.57.1",
232
+ "effect": "4.0.0-rc.115",
233
+ "esbuild": "0.28.2",
234
+ "eslint": "9.39.5",
235
+ "eslint-import-resolver-typescript": "4.4.5",
236
+ "eslint-plugin-import": "2.32.0",
237
+ "eslint-plugin-jsdoc": "64.3.4",
238
+ "eslint-plugin-unicorn": "65.0.0",
239
+ "madge": "8.0.0",
240
+ "typescript": "5.9.3",
241
+ "typescript-eslint": "8.69.0",
242
+ "vitest": "5.0.0",
243
+ "fast-check": "4.9.0"
244
+ },
245
+ "peerDependencies": {
246
+ "effect": "4.0.0-rc.115"
247
+ },
248
+ "scripts": {
249
+ "lint": "eslint src --max-warnings=0 && dprint check",
250
+ "format": "dprint fmt",
251
+ "build": "node scripts/build.mjs",
252
+ "check": "tsc -b tsconfig.json && tsc -p tsconfig.test.json --noEmit",
253
+ "circular": "node scripts/circular.mjs",
254
+ "test": "vitest",
255
+ "coverage": "vitest --coverage"
256
+ }
6
257
  }
@@ -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,284 @@
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.
143
+ *
144
+ * @category models
145
+ * @since 0.0.0
146
+ * @slop
147
+ */
148
+ export interface Declaration {
149
+ readonly reads: ReadonlyArray<string>
150
+ readonly writes: ReadonlyArray<string>
151
+ readonly mode: Mode
152
+ readonly onConflict: ConflictStrategy
153
+ readonly tier?: Tier | undefined
154
+ }
155
+
156
+ /**
157
+ * Input accepted by {@link make}. Iterables are normalized into sorted,
158
+ * duplicate-free arrays so declarations are deterministic key material.
159
+ * Envelope entries and declared paths must already be path-normalized: no
160
+ * separator or dot-segment rewriting is performed. A declared path containing
161
+ * a whole `.` or `..` segment is never covered and therefore surfaces from
162
+ * {@link narrow} as an `effect_outside_envelope` diagnostic.
163
+ *
164
+ * @category models
165
+ * @since 0.0.0
166
+ * @slop
167
+ */
168
+ export interface MakeOptions {
169
+ readonly reads: Iterable<string>
170
+ readonly writes: Iterable<string>
171
+ readonly mode: Mode
172
+ readonly onConflict: ConflictStrategy
173
+ readonly tier?: Tier | undefined
174
+ }
175
+
176
+ /**
177
+ * A result of checking that a step declaration narrows a flow envelope.
178
+ *
179
+ * @category models
180
+ * @since 0.0.0
181
+ * @slop
182
+ */
183
+ export type NarrowResult =
184
+ | { readonly ok: true }
185
+ | {
186
+ readonly ok: false
187
+ readonly code: "effect_outside_envelope" | "effect_mode_widening" | "effect_tier_widening"
188
+ readonly paths: ReadonlyArray<string>
189
+ }
190
+
191
+ const normalize = (paths: Iterable<string>): ReadonlyArray<string> => [...new Set(paths)].sort()
192
+
193
+ /**
194
+ * Constructs a deterministic effect declaration.
195
+ *
196
+ * @category constructors
197
+ * @since 0.0.0
198
+ * @slop
199
+ */
200
+ export const make = (input: MakeOptions): Declaration => ({
201
+ reads: normalize(input.reads),
202
+ writes: normalize(input.writes),
203
+ mode: input.mode,
204
+ onConflict: input.onConflict,
205
+ ...(input.tier === undefined ? {} : { tier: input.tier })
206
+ })
207
+
208
+ /**
209
+ * Checks whether `path` is covered by an envelope entry.
210
+ *
211
+ * The grammar is exhaustive: an exact path matches itself; `*` and `**` match
212
+ * everything; `prefix*` matches by string prefix; and `prefix/**` matches
213
+ * `prefix/` and everything below it, but not bare `prefix`. This is
214
+ * intentionally not full minimatch syntax. Envelope entries and declared paths
215
+ * must be normalized. A path containing a whole `.` or `..` segment is never
216
+ * covered, so {@link narrow} reports it as `effect_outside_envelope` rather
217
+ * than silently accepting an escape.
218
+ *
219
+ * @category predicates
220
+ * @since 0.0.0
221
+ * @slop
222
+ */
223
+ export const covers = (envelope: string, path: string): boolean => {
224
+ if (Index.hasDotSegment(path)) return false
225
+ return envelope === path || (Index.isGlob(envelope) && path.startsWith(Index.globPrefix(envelope)))
226
+ }
227
+
228
+ /**
229
+ * Verifies that a step declaration stays within an enclosing flow envelope.
230
+ *
231
+ * Read and write paths must be covered independently. A step may tighten
232
+ * `expected` to `hermetic`, but cannot widen `hermetic` to `expected`.
233
+ * Effect tiers narrow from irreversible to compensable to sealed.
234
+ *
235
+ * The envelope's lists are prepared once: exact entries go into a set and
236
+ * covering patterns collapse to their outermost prefixes, sorted, so each step
237
+ * path costs one dot-segment scan, one lookup, and one binary search ending in
238
+ * one prefix comparison, whatever the envelope's width or how many of its
239
+ * patterns nest. A graph build prepares each envelope once for every node it
240
+ * encloses.
241
+ *
242
+ * @category validation
243
+ * @since 0.0.0
244
+ * @slop
245
+ */
246
+ export const narrow = (envelope: Declaration, step: Declaration): NarrowResult =>
247
+ Index.narrowPrepared(Index.prepareEnvelope(envelope), step)
248
+
249
+ /**
250
+ * Returns the concrete or narrower write declarations shared by two effect
251
+ * declarations. The result is sorted and duplicate-free.
252
+ *
253
+ * Two declarations of the same literal path always overlap, including a path
254
+ * {@link covers} refuses to match because it carries a `.` or `..` segment.
255
+ * Glob coverage stays strict for those paths, so an unnormalized declaration
256
+ * still escapes no envelope, but two writers naming the same unnormalized path
257
+ * are still detected as writing the same resource.
258
+ *
259
+ * The union of both declarations is indexed once: the distinct paths are
260
+ * sorted, each is scanned once for a dot segment, and each pattern's prefix
261
+ * is located by binary search. Exact paths then match through a merge of two
262
+ * rank lists and each covering pattern enumerates the other declaration's
263
+ * ranks inside its interval, so the cost is linear in the two declarations
264
+ * plus the matches, whatever the paths' lengths or how many patterns nest.
265
+ *
266
+ * @category analysis
267
+ * @since 0.0.0
268
+ * @slop
269
+ */
270
+ export const overlaps = (a: Declaration, b: Declaration): ReadonlyArray<string> => {
271
+ const indexed = Index.indexPaths([a.writes, b.writes])
272
+ return Index.overlapRanks(indexed, Index.rankPaths(indexed, a.writes), Index.rankPaths(indexed, b.writes))
273
+ .map((rank) => indexed.paths[rank]!)
274
+ }
275
+
276
+ /**
277
+ * Returns a sealed, hermetic copy of an effect declaration.
278
+ *
279
+ * @category constructors
280
+ * @since 0.0.0
281
+ * @slop
282
+ */
283
+ export const sealed = (declaration: Declaration): Declaration =>
284
+ make({ ...declaration, mode: "hermetic", tier: "sealed" })