nomoss 0.0.3 → 0.0.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 (150) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +5 -15
  3. package/ROADMAP.md +2 -0
  4. package/docs/README.md +1 -1
  5. package/docs/application/aws-resource-coverage.md +26 -0
  6. package/docs/application/aws-resource-lifecycle-management.md +13 -23
  7. package/docs/application/cli-workflow.md +1 -1
  8. package/docs/application/engine-design.md +2 -2
  9. package/docs/application/generation-architecture.md +10 -45
  10. package/docs/application/resource-command-policy-stage.md +2 -2
  11. package/docs/application/resource-graph.md +1 -1
  12. package/docs/application/resource-lifecycle-tracker.md +1 -9
  13. package/docs/application/resource-mapping.md +4 -9
  14. package/docs/application/resource-operations.md +4 -2
  15. package/docs/application/tracing.md +2 -6
  16. package/docs/development/code-documentation.md +2 -0
  17. package/docs/development/effect-native-development.md +44 -0
  18. package/docs/development/linting.md +1 -1
  19. package/docs/development/review-code.md +22 -6
  20. package/docs/development/review-remediation-spec.md +28 -37
  21. package/docs/development/spec-writing.md +1 -1
  22. package/docs/development/testing.md +23 -9
  23. package/docs/operations/observability-sop.md +11 -17
  24. package/docs/operations/releasing.md +3 -1
  25. package/examples/README.md +3 -26
  26. package/examples/commands.ts +2 -36
  27. package/examples/queue/application.ts +8 -2
  28. package/examples/state-machine/application.ts +3 -1
  29. package/package.json +40 -46
  30. package/providers/aws/README.md +21 -0
  31. package/providers/aws/awsEnvironment.ts +114 -0
  32. package/providers/aws/generated/managementRegistry.ts +136 -0
  33. package/{src/providers/aws/resources/generated/native/apigatewayv2/api/index.ts → providers/aws/generated/resources/apigatewayv2/api.ts} +13 -50
  34. package/{src/providers/aws/resources/generated/native/apigatewayv2/integration/index.ts → providers/aws/generated/resources/apigatewayv2/integration.ts} +13 -37
  35. package/{src/providers/aws/resources/generated/native/apigatewayv2/route/index.ts → providers/aws/generated/resources/apigatewayv2/route.ts} +23 -40
  36. package/{src/providers/aws/resources/generated/native/apigatewayv2/stage/index.ts → providers/aws/generated/resources/apigatewayv2/stage.ts} +13 -38
  37. package/providers/aws/generated/resources/iam/accesskey.ts +184 -0
  38. package/{src/providers/aws/resources/generated/native/iam/group/index.ts → providers/aws/generated/resources/iam/group.ts} +13 -38
  39. package/{src/providers/aws/resources/generated/native/iam/grouppolicy/index.ts → providers/aws/generated/resources/iam/grouppolicy.ts} +29 -59
  40. package/{src/providers/aws/resources/generated/native/iam/instanceprofile/index.ts → providers/aws/generated/resources/iam/instanceprofile.ts} +13 -41
  41. package/{src/providers/aws/resources/generated/native/iam/managedpolicy/index.ts → providers/aws/generated/resources/iam/managedpolicy.ts} +13 -95
  42. package/providers/aws/generated/resources/iam/oidcprovider.ts +314 -0
  43. package/{src/providers/aws/resources/generated/native/iam/policy/index.ts → providers/aws/generated/resources/iam/policy.ts} +13 -28
  44. package/{src/providers/aws/resources/generated/native/iam/role/index.ts → providers/aws/generated/resources/iam/role.ts} +13 -47
  45. package/{src/providers/aws/resources/generated/native/iam/rolepolicy/index.ts → providers/aws/generated/resources/iam/rolepolicy.ts} +29 -59
  46. package/providers/aws/generated/resources/iam/samlprovider.ts +463 -0
  47. package/providers/aws/generated/resources/iam/servercertificate.ts +465 -0
  48. package/providers/aws/generated/resources/iam/servicelinkedrole.ts +187 -0
  49. package/{src/providers/aws/resources/generated/native/iam/user/index.ts → providers/aws/generated/resources/iam/user.ts} +13 -38
  50. package/{src/providers/aws/resources/generated/native/iam/userpolicy/index.ts → providers/aws/generated/resources/iam/userpolicy.ts} +29 -59
  51. package/{src/providers/aws/resources/generated/native/iam/usertogroupaddition/index.ts → providers/aws/generated/resources/iam/usertogroupaddition.ts} +13 -27
  52. package/providers/aws/generated/resources/iam/virtualmfadevice.ts +298 -0
  53. package/{src/providers/aws/resources/generated/native/kms/key/index.ts → providers/aws/generated/resources/kms/key.ts} +13 -41
  54. package/{src/providers/aws/resources/generated/native/lambda/function/index.ts → providers/aws/generated/resources/lambda/function.ts} +13 -63
  55. package/{src/providers/aws/resources/generated/native/lambda/permission/index.ts → providers/aws/generated/resources/lambda/permission.ts} +13 -55
  56. package/{src/providers/aws/resources/generated/native/s3/bucket/index.ts → providers/aws/generated/resources/s3/bucket.ts} +14 -104
  57. package/{src/providers/aws/resources/generated/native/sns/topic/index.ts → providers/aws/generated/resources/sns/topic.ts} +14 -29
  58. package/{src/providers/aws/resources/generated/native/sqs/queue/index.ts → providers/aws/generated/resources/sqs/queue.ts} +14 -38
  59. package/{src/providers/aws/resources/generated/native/stepfunctions/statemachine/index.ts → providers/aws/generated/resources/stepfunctions/statemachine.ts} +15 -58
  60. package/providers/aws/management/mappings/iam/group.ts +63 -0
  61. package/providers/aws/management/mappings/iam/iamIdentity.ts +41 -0
  62. package/providers/aws/management/mappings/iam/iamLifecycleSettings.ts +8 -0
  63. package/providers/aws/management/mappings/iam/inlinePolicies.ts +159 -0
  64. package/providers/aws/management/mappings/iam/instanceProfile.ts +77 -0
  65. package/providers/aws/management/mappings/iam/oidcProvider.ts +94 -0
  66. package/providers/aws/management/mappings/iam/role.ts +74 -0
  67. package/providers/aws/management/mappings/iam/samlProvider.ts +98 -0
  68. package/providers/aws/management/mappings/iam/serverCertificate.ts +72 -0
  69. package/providers/aws/management/mappings/iam/user.ts +61 -0
  70. package/providers/aws/management/mappings/iam/userToGroupAddition.ts +75 -0
  71. package/providers/aws/management/operations/iam/accessKey.ts +206 -0
  72. package/providers/aws/management/operations/iam/managedPolicy.ts +491 -0
  73. package/providers/aws/management/operations/iam/policy.ts +476 -0
  74. package/providers/aws/management/operations/iam/serviceLinkedRole.ts +172 -0
  75. package/providers/aws/management/operations/iam/virtualMfaDevice.ts +253 -0
  76. package/providers/aws/management/resourceAssociationConfiguration.ts +112 -0
  77. package/providers/aws/management/resourceAssociationRegistration.ts +349 -0
  78. package/providers/aws/management/resourceAssociations.ts +168 -0
  79. package/{src/providers → providers}/aws/management/resourceConfiguration.ts +24 -26
  80. package/providers/aws/management/resourceLifecycleRegistration.ts +540 -0
  81. package/providers/aws/management/resourceManagementModule.ts +31 -0
  82. package/providers/aws/management/resourceManagementRegistry.ts +62 -0
  83. package/providers/aws/management/resourceMapping.ts +84 -0
  84. package/providers/aws/management/resourceOperation.ts +134 -0
  85. package/providers/aws/management/resourceTagOperations.ts +108 -0
  86. package/providers/aws/management/resourceTiming.ts +4 -0
  87. package/providers/aws/runtime.ts +52 -0
  88. package/src/core/resource/resourceCommandPolicy.ts +20 -0
  89. package/src/core/resource/resourceLifecycle.ts +644 -0
  90. package/src/core/resource/resourceStabilization.ts +120 -0
  91. package/src/core/resource/resourceVersions.ts +102 -0
  92. package/src/core/stack/planner.ts +9 -0
  93. package/src/core/stack/resourceStackLifecycle.ts +36 -3
  94. package/src/core/state/resourceState.ts +6 -4
  95. package/src/core/state/stateStore.ts +57 -10
  96. package/src/providers/file/resourcePolicy.ts +13 -12
  97. package/src/providers/providerRuntimeLayer.ts +5 -2
  98. package/src/providers/resourceCommandPolicyLayer.ts +117 -37
  99. package/src/providers/stripe/management/resourcePolicy.ts +22 -11
  100. package/examples/http-echo/application.ts +0 -87
  101. package/src/providers/aws/client/awsClient.ts +0 -418
  102. package/src/providers/aws/client/awsClientConfig.ts +0 -133
  103. package/src/providers/aws/client/awsClientModel.ts +0 -119
  104. package/src/providers/aws/client/awsHttpOperation.ts +0 -153
  105. package/src/providers/aws/client/awsQueryOperation.ts +0 -149
  106. package/src/providers/aws/client/awsRequestSigner.ts +0 -115
  107. package/src/providers/aws/client/awsResponseDecoder.ts +0 -135
  108. package/src/providers/aws/client/awsRetryPolicy.ts +0 -62
  109. package/src/providers/aws/client/generated/apiGatewayV2.ts +0 -3025
  110. package/src/providers/aws/client/generated/cloudControl.ts +0 -5746
  111. package/src/providers/aws/client/generated/iam.ts +0 -10892
  112. package/src/providers/aws/client/generated/lambda.ts +0 -5403
  113. package/src/providers/aws/client/generated/sqs.ts +0 -3769
  114. package/src/providers/aws/client/generated/stepFunctions.ts +0 -1726
  115. package/src/providers/aws/client/openapi/cloudControl/CancelResourceRequest.json +0 -579
  116. package/src/providers/aws/client/openapi/cloudControl/CreateResource.json +0 -1019
  117. package/src/providers/aws/client/openapi/cloudControl/DeleteResource.json +0 -1018
  118. package/src/providers/aws/client/openapi/cloudControl/GetResource.json +0 -883
  119. package/src/providers/aws/client/openapi/cloudControl/GetResourceRequestStatus.json +0 -655
  120. package/src/providers/aws/client/openapi/cloudControl/ListResourceRequests.json +0 -566
  121. package/src/providers/aws/client/openapi/cloudControl/ListResources.json +0 -913
  122. package/src/providers/aws/client/openapi/cloudControl/UpdateResource.json +0 -1027
  123. package/src/providers/aws/cloudControl/client.ts +0 -69
  124. package/src/providers/aws/cloudControl/requestTracker.ts +0 -138
  125. package/src/providers/aws/cloudControl/resourceCommandPolicyLayer.ts +0 -63
  126. package/src/providers/aws/cloudControl/resourceLifecycle.ts +0 -479
  127. package/src/providers/aws/cloudControl/resources/generated/certificatemanager/certificate/index.ts +0 -179
  128. package/src/providers/aws/cloudControl/resources/generated/kms/key/index.ts +0 -362
  129. package/src/providers/aws/cloudControl/resources/generated/s3/bucket/index.ts +0 -2364
  130. package/src/providers/aws/cloudControl/resources/generated/sns/topic/index.ts +0 -326
  131. package/src/providers/aws/cloudControl/resources/generated/sqs/queue/index.ts +0 -307
  132. package/src/providers/aws/cloudControl/resources/generated/sqs/queueinlinepolicy/index.ts +0 -108
  133. package/src/providers/aws/cloudControl/runtime.ts +0 -127
  134. package/src/providers/aws/management/resourceMapping.ts +0 -343
  135. package/src/providers/aws/management/resourceOperation.ts +0 -57
  136. package/src/providers/aws/management/resourcePolicy.ts +0 -549
  137. package/src/providers/aws/management/resourceRegistration.ts +0 -294
  138. package/src/providers/aws/resources/apiGatewayV2/httpApi.ts +0 -327
  139. package/src/providers/aws/resources/generated/native/resourceCommands.ts +0 -152
  140. package/src/providers/aws/resources/iam/grant.ts +0 -150
  141. package/src/providers/aws/resources/iam/grantCommands.ts +0 -283
  142. package/src/providers/aws/resources/iam/policyDocument.ts +0 -30
  143. package/src/providers/aws/resources/iam/policyStatement.ts +0 -59
  144. package/src/providers/aws/resources/iam/role.ts +0 -426
  145. package/src/providers/aws/resources/iam/roleDeclaration.ts +0 -105
  146. package/src/providers/aws/resources/lambda/function.ts +0 -403
  147. package/src/providers/aws/resources/lambda/functionDeclaration.ts +0 -234
  148. package/src/providers/aws/resources/lambda/permission.ts +0 -363
  149. package/src/providers/aws/resources/stepFunctions/stateMachine.ts +0 -95
  150. package/src/providers/aws/runtime.ts +0 -70
@@ -0,0 +1,644 @@
1
+ import {
2
+ Context,
3
+ type Duration,
4
+ Effect,
5
+ Exit,
6
+ Function as Fn,
7
+ Match,
8
+ Option,
9
+ type Schema,
10
+ } from "effect";
11
+ import {
12
+ ResourceCommand,
13
+ ResourceCommandResult,
14
+ ResourceCommandUnsupported,
15
+ ResourceObservation,
16
+ } from "./lifecycle.js";
17
+ import { ResourceModel, type ResourceNode } from "./model.js";
18
+ import { ResourceCommandCreation } from "./resourceCommandPolicy.js";
19
+ import { ResourceStabilization } from "./resourceStabilization.js";
20
+
21
+ type Confirmation<Outputs, Failure, Services> = (
22
+ outputs: Outputs,
23
+ ) => boolean | Effect.Effect<boolean, Failure, Services>;
24
+
25
+ /**
26
+ * The PreparedResourceChange interface lets the lifecycle verify a change
27
+ * without saving write-only values from the provider request.
28
+ */
29
+ interface PreparedResourceChange<Outputs, Failure, Services> {
30
+ readonly outputs: Outputs;
31
+ readonly request: Effect.Effect<unknown, Failure, Services>;
32
+ }
33
+
34
+ /** The RemovalPrerequisite interface specifies when an observed resource is safe to delete and which failure to report if it stays in use. */
35
+ interface RemovalPrerequisite<Outputs, Failure> {
36
+ readonly accepts: (outputs: Outputs) => boolean;
37
+ readonly repeats: number;
38
+ readonly onBlocked: (outputs: Outputs) => Failure;
39
+ }
40
+
41
+ /**
42
+ * The ResourceLifecycleLookup interface lets each mutation use the same
43
+ * validated provider observation when it checks the outcome of a change.
44
+ *
45
+ * @see ../../../docs/application/resource-operations.md (Create, read, update, and remove a resource)
46
+ */
47
+ interface ResourceLifecycleLookup<
48
+ Outputs extends Schema.JsonObject,
49
+ Identity,
50
+ Failure,
51
+ Services,
52
+ OwnershipFailure,
53
+ > {
54
+ /** The identity schema selects the saved outputs required by lookup and deletion. */
55
+ readonly identity: Schema.Schema<Identity>;
56
+ /**
57
+ * The lookup operation can use the command's declaration when one stack
58
+ * resource has several provider addresses. An inline IAM policy, for
59
+ * example, has a separate copy on each principal that the node names.
60
+ */
61
+ readonly lookup: (
62
+ identity: Identity,
63
+ node: ResourceNode,
64
+ ) => Effect.Effect<Option.Option<Outputs>, Failure, Services>;
65
+ /** IAM can reuse a name or ARN. A registration can require an immutable ID as well. */
66
+ readonly ownership?: {
67
+ readonly matches: (saved: Identity, observed: Outputs) => boolean;
68
+ readonly onMismatch: (saved: Identity) => OwnershipFailure;
69
+ };
70
+ /**
71
+ * The projectOutputs callback lets a provider use additional attributes
72
+ * to verify a change while saving only the declared outputs.
73
+ */
74
+ readonly projectOutputs?: (outputs: Outputs) => Schema.JsonObject;
75
+ }
76
+
77
+ type LifecycleCommand =
78
+ | Extract<ResourceCommand, { _tag: "Read" | "Create" | "Destroy" }>
79
+ | {
80
+ readonly _tag: "Update";
81
+ readonly node: ResourceNode;
82
+ readonly current: ResourceNode;
83
+ readonly command: ResourceCommand;
84
+ };
85
+
86
+ /**
87
+ * The ResourceLifecycle service executes a resource's mutations and
88
+ * confirms the resulting identity or absence before reporting success.
89
+ * A registration can opt into the same removal workflow for create rollback.
90
+ *
91
+ * @see ../../../docs/application/resource-operations.md (Resource lifecycle operations)
92
+ * @see ../../../node_modules/effect/src/Context.ts (Service with a parameterized make)
93
+ * @see ../../../node_modules/effect/src/Effect.ts (acquireUseRelease)
94
+ */
95
+ export class ResourceLifecycle extends Context.Service<ResourceLifecycle>()(
96
+ "nomoss/core/resource/resourceLifecycle",
97
+ {
98
+ make: Effect.fn("ResourceLifecycle.make")(function* <
99
+ Outputs extends Schema.JsonObject,
100
+ Identity,
101
+ Failure,
102
+ Services,
103
+ OwnershipFailure = never,
104
+ >(
105
+ resource: ResourceLifecycleLookup<
106
+ Outputs,
107
+ Identity,
108
+ Failure,
109
+ Services,
110
+ OwnershipFailure
111
+ >,
112
+ timeout: Duration.Input,
113
+ ) {
114
+ const model = yield* ResourceModel;
115
+ const stabilization = yield* ResourceStabilization;
116
+
117
+ const { projectOutputs: persisted = Fn.identity } = resource;
118
+
119
+ // The lookup operation compares immutable ownership when a provider can
120
+ // reuse an address. A mismatch leaves the resource under its current owner.
121
+ // See ../../../node_modules/effect/src/Effect.ts (transposeOption).
122
+ const lookup = Effect.fnUntraced(function* (
123
+ identity: Identity,
124
+ node: ResourceNode,
125
+ ) {
126
+ const observed = yield* resource.lookup(identity, node);
127
+
128
+ const conflict = Option.fromUndefinedOr(resource.ownership).pipe(
129
+ Option.filter(({ matches }) =>
130
+ Option.exists(observed, (outputs) => !matches(identity, outputs)),
131
+ ),
132
+ );
133
+ yield* conflict.pipe(
134
+ Option.map(({ onMismatch }) => Effect.fail(onMismatch(identity))),
135
+ Effect.transposeOption,
136
+ );
137
+
138
+ return observed;
139
+ });
140
+
141
+ /**
142
+ * The read operation returns a validated observation of the identified
143
+ * resource. It passes through confirmed absence and fails if the provider
144
+ * returns a primary identifier that differs from the one on the node.
145
+ *
146
+ * @see ./model.ts (ResourceModel.withObservedOutputs)
147
+ */
148
+ const read = Effect.fnUntraced(function* (node: ResourceNode) {
149
+ const identity = yield* model.decodeOutputs(node, resource.identity);
150
+
151
+ const observed = yield* lookup(identity, node);
152
+
153
+ return yield* observed.pipe(
154
+ Option.map(
155
+ Effect.fnUntraced(function* (outputs) {
156
+ const identified = yield* model.withObservedOutputs(
157
+ node,
158
+ persisted(outputs),
159
+ );
160
+ return { node: identified, outputs };
161
+ }),
162
+ ),
163
+ Effect.transposeOption,
164
+ );
165
+ });
166
+
167
+ /**
168
+ * The confirm operation waits for the provider to report the expected
169
+ * state at the recorded identity. A read failure or an expired deadline
170
+ * prevents the mutation from being reported as complete.
171
+ *
172
+ * @see ./resourceStabilization.ts (waitForPresence)
173
+ */
174
+ const confirm = Effect.fnUntraced(function* <E, R>(
175
+ node: ResourceNode,
176
+ accepts: (outputs: Outputs) => Effect.Effect<boolean, E, R>,
177
+ ) {
178
+ const observed = yield* read(node).pipe(
179
+ Effect.flatMap(
180
+ Option.match({
181
+ onNone: () => Effect.succeedNone,
182
+ onSome: (state) =>
183
+ Effect.succeed(state).pipe(Effect.when(accepts(state.outputs))),
184
+ }),
185
+ ),
186
+ stabilization.waitForPresence(timeout),
187
+ );
188
+
189
+ return observed;
190
+ });
191
+
192
+ /**
193
+ * The reconcile operation applies desired state at its stable
194
+ * identity. It reads the provider first so a retry can skip a
195
+ * mutation that already took effect. The caller confirms the result.
196
+ *
197
+ * @see ../../../node_modules/effect/src/Effect.ts (when)
198
+ */
199
+ const reconcile = Effect.fn("ResourceLifecycle.reconcile")(function* <
200
+ E,
201
+ R,
202
+ >(
203
+ node: ResourceNode,
204
+ { outputs: desired, request }: PreparedResourceChange<Outputs, E, R>,
205
+ matches: (
206
+ desired: Outputs,
207
+ observed: Outputs,
208
+ ) => boolean = Fn.constTrue,
209
+ ) {
210
+ const identified = yield* model.withOutputs(node, persisted(desired));
211
+ const observed = yield* read(identified);
212
+
213
+ const alreadyApplied = Option.exists(observed, ({ outputs }) =>
214
+ matches(desired, outputs),
215
+ );
216
+
217
+ yield* request.pipe(Effect.when(Effect.succeed(!alreadyApplied)));
218
+
219
+ return desired;
220
+ });
221
+
222
+ /**
223
+ * The remove operation lets teardown and create rollback share the same
224
+ * prerequisites. If a preparatory read finds the resource gone, removal
225
+ * is complete. If the resource is still present, a successful delete
226
+ * requires a later read to confirm its absence.
227
+ *
228
+ * @see ./resourceStabilization.ts (observeWithinAttempts and waitForAbsence)
229
+ * @see ../../../node_modules/effect/src/Effect.ts (transposeOption)
230
+ */
231
+ const remove = <
232
+ E,
233
+ R,
234
+ CleanupFailure = never,
235
+ CleanupServices = never,
236
+ Blocked = never,
237
+ >(
238
+ request: (
239
+ identity: Identity,
240
+ node: ResourceNode,
241
+ ) => Effect.Effect<unknown, E, R>,
242
+ verification?: {
243
+ readonly prerequisite?: RemovalPrerequisite<Outputs, Blocked>;
244
+ readonly cleanup?: (
245
+ outputs: Outputs,
246
+ node: ResourceNode,
247
+ ) => Effect.Effect<unknown, CleanupFailure, CleanupServices>;
248
+ },
249
+ ) =>
250
+ Effect.fn("ResourceLifecycle.remove")(function* ({
251
+ node,
252
+ }: {
253
+ readonly node: ResourceNode;
254
+ }) {
255
+ const identity = yield* model.decodeOutputs(node, resource.identity);
256
+
257
+ const observation = lookup(identity, node);
258
+
259
+ const deleteResource = Effect.fnUntraced(function* () {
260
+ yield* request(identity, node);
261
+
262
+ yield* observation.pipe(stabilization.waitForAbsence(timeout));
263
+ });
264
+
265
+ yield* Option.match(Option.fromUndefinedOr(verification), {
266
+ onNone: deleteResource,
267
+ onSome: ({ prerequisite, cleanup = () => Effect.void }) =>
268
+ Effect.gen(function* () {
269
+ const ready = yield* Option.match(
270
+ Option.fromUndefinedOr(prerequisite),
271
+ {
272
+ onNone: () => observation,
273
+ onSome: ({ accepts, repeats, onBlocked }) =>
274
+ Effect.gen(function* () {
275
+ const satisfied = (state: Option.Option<Outputs>) =>
276
+ Option.isNone(state) || accepts(state.value);
277
+
278
+ const observed = yield* observation.pipe(
279
+ stabilization.observeWithinAttempts(
280
+ satisfied,
281
+ repeats,
282
+ ),
283
+ );
284
+
285
+ yield* observed.pipe(
286
+ Option.filter((outputs) => !accepts(outputs)),
287
+ Option.map((outputs) =>
288
+ Effect.fail(onBlocked(outputs)),
289
+ ),
290
+ Effect.transposeOption,
291
+ );
292
+
293
+ return observed;
294
+ }),
295
+ },
296
+ );
297
+
298
+ yield* ready.pipe(
299
+ Option.map((outputs) =>
300
+ cleanup(outputs, node).pipe(
301
+ Effect.andThen(deleteResource()),
302
+ ),
303
+ ),
304
+ Effect.transposeOption,
305
+ );
306
+ }),
307
+ });
308
+
309
+ const result: ResourceCommandResult = ResourceCommandResult.Destroyed(
310
+ { node },
311
+ );
312
+
313
+ return result;
314
+ });
315
+
316
+ /**
317
+ * The create operation waits for the provider to report the required
318
+ * configuration before returning outputs. A checkpoint schema
319
+ * must be able to read the saved create result because a retry can
320
+ * confirm that result without creating another instance. Registrations
321
+ * that choose rollback use the create response to remove an instance
322
+ * after failed or interrupted confirmation.
323
+ *
324
+ * @see ../../../docs/application/resource-operations.md (Create a resource)
325
+ * @see ../../../node_modules/effect/src/Effect.ts (acquireUseRelease)
326
+ */
327
+ const create = <
328
+ E,
329
+ R,
330
+ ConfigureFailure = never,
331
+ ConfigureServices = never,
332
+ RollbackFailure = never,
333
+ RollbackServices = never,
334
+ >(
335
+ request: (node: ResourceNode) => Effect.Effect<Outputs, E, R>,
336
+ options: {
337
+ readonly matches?: (created: Outputs, observed: Outputs) => boolean;
338
+ readonly configure?: (
339
+ node: ResourceNode,
340
+ outputs: Outputs,
341
+ ) => Effect.Effect<
342
+ (observed: Outputs) => boolean,
343
+ ConfigureFailure,
344
+ ConfigureServices
345
+ >;
346
+ } & (
347
+ | {
348
+ readonly checkpoint?: never;
349
+ readonly rollback?: (command: {
350
+ readonly node: ResourceNode;
351
+ }) => Effect.Effect<unknown, RollbackFailure, RollbackServices>;
352
+ }
353
+ | {
354
+ readonly checkpoint: Schema.Schema<Outputs>;
355
+ readonly rollback?: never;
356
+ }
357
+ ) = {},
358
+ ) =>
359
+ Effect.fn("ResourceLifecycle.create")(function* ({
360
+ node,
361
+ }: {
362
+ readonly node: ResourceNode;
363
+ }) {
364
+ const {
365
+ matches = Fn.constTrue,
366
+ configure,
367
+ rollback,
368
+ checkpoint,
369
+ } = options;
370
+
371
+ const creation = yield* ResourceCommandCreation;
372
+
373
+ const finish = Effect.fnUntraced(function* (created: Outputs) {
374
+ const identified = yield* model.withOutputs(
375
+ node,
376
+ persisted(created),
377
+ );
378
+
379
+ const observed = yield* confirm(identified, (outputs) =>
380
+ Effect.succeed(matches(created, outputs)),
381
+ );
382
+
383
+ const confirmed = yield* Option.match(
384
+ Option.fromUndefinedOr(configure),
385
+ {
386
+ onNone: () => Effect.succeed(observed),
387
+ onSome: (configure) =>
388
+ configure(identified, observed.outputs).pipe(
389
+ Effect.flatMap((accepts) =>
390
+ confirm(identified, (outputs) =>
391
+ Effect.succeed(accepts(outputs)),
392
+ ),
393
+ ),
394
+ ),
395
+ },
396
+ );
397
+
398
+ const result: ResourceCommandResult = ResourceCommandResult.Created(
399
+ {
400
+ node: confirmed.node,
401
+ },
402
+ );
403
+
404
+ return result;
405
+ });
406
+
407
+ const submit = Effect.fnUntraced(function* () {
408
+ return yield* Match.value({ checkpoint, creation }).pipe(
409
+ Match.when(
410
+ { checkpoint: Match.defined, creation: { _tag: "Some" } },
411
+ ({
412
+ checkpoint,
413
+ creation: {
414
+ value: { submitted, record },
415
+ },
416
+ }) =>
417
+ Option.match(submitted, {
418
+ onSome: (pending) =>
419
+ model.decodeOutputs(pending, checkpoint),
420
+ onNone: () =>
421
+ Effect.uninterruptibleMask((restore) =>
422
+ restore(request(node)).pipe(
423
+ Effect.tap((outputs) =>
424
+ model
425
+ .withOutputs(node, persisted(outputs))
426
+ .pipe(Effect.flatMap(record)),
427
+ ),
428
+ ),
429
+ ),
430
+ }),
431
+ ),
432
+ Match.orElse(() => request(node)),
433
+ );
434
+ });
435
+
436
+ return yield* Option.match(Option.fromUndefinedOr(rollback), {
437
+ onNone: () => Effect.flatMap(submit(), finish),
438
+ onSome: (remove) =>
439
+ Effect.acquireUseRelease(submit(), finish, (created, exit) =>
440
+ Effect.suspend(() =>
441
+ remove({ node: { ...node, outputs: persisted(created) } }),
442
+ ).pipe(
443
+ Effect.when(Effect.succeed(Exit.isFailure(exit))),
444
+ Effect.asVoid,
445
+ ),
446
+ ),
447
+ });
448
+ });
449
+
450
+ /**
451
+ * The update operation uses the saved resource identity to confirm
452
+ * a change. The provider mutation supplies a condition that a later
453
+ * read must satisfy before the stack can save the updated outputs.
454
+ *
455
+ * @see ../../../docs/application/resource-operations.md (Update a resource)
456
+ */
457
+ const update = <
458
+ E,
459
+ R,
460
+ ConfirmFailure = never,
461
+ ConfirmServices = never,
462
+ VerifyFailure = never,
463
+ VerifyServices = never,
464
+ >(
465
+ request: (
466
+ node: ResourceNode,
467
+ current: ResourceNode,
468
+ ) => Effect.Effect<
469
+ Confirmation<Outputs, ConfirmFailure, ConfirmServices>,
470
+ E,
471
+ R
472
+ >,
473
+ verify?: (
474
+ node: ResourceNode,
475
+ outputs: Outputs,
476
+ ) => Effect.Effect<unknown, VerifyFailure, VerifyServices>,
477
+ ) =>
478
+ Effect.fn("ResourceLifecycle.update")(function* ({
479
+ node,
480
+ current,
481
+ }: Extract<LifecycleCommand, { _tag: "Update" }>) {
482
+ const accepts = yield* request(node, current);
483
+
484
+ const identified = yield* model.withOutputs(node, current.outputs);
485
+ const observed = yield* confirm(identified, (outputs) =>
486
+ Match.value(accepts(outputs)).pipe(
487
+ Match.when(Match.boolean, (ready) => Effect.succeed(ready)),
488
+ Match.orElse((effect) => effect),
489
+ ),
490
+ );
491
+
492
+ yield* Option.fromUndefinedOr(verify).pipe(
493
+ Option.map((verify) => verify(identified, observed.outputs)),
494
+ Effect.transposeOption,
495
+ );
496
+
497
+ const result: ResourceCommandResult = ResourceCommandResult.Updated({
498
+ node: observed.node,
499
+ });
500
+
501
+ return result;
502
+ });
503
+
504
+ /**
505
+ * The observedUpdate method reads the current provider state before
506
+ * submitting a mutation that depends on it. The mutation supplies the
507
+ * condition that will confirm the resulting change.
508
+ *
509
+ * @see ../../../node_modules/effect/src/Effect.ts (fromOption)
510
+ */
511
+ const observedUpdate = <
512
+ Input,
513
+ E,
514
+ R,
515
+ ApplyFailure,
516
+ ApplyServices,
517
+ ConfirmFailure = never,
518
+ ConfirmServices = never,
519
+ >(
520
+ prepare: (
521
+ node: ResourceNode,
522
+ current: ResourceNode,
523
+ ) => Effect.Effect<Input, E, R>,
524
+ apply: (
525
+ outputs: Outputs,
526
+ input: Input,
527
+ ) => Effect.Effect<
528
+ Confirmation<Outputs, ConfirmFailure, ConfirmServices>,
529
+ ApplyFailure,
530
+ ApplyServices
531
+ >,
532
+ ) =>
533
+ Effect.fnUntraced(function* (
534
+ node: ResourceNode,
535
+ current: ResourceNode,
536
+ ) {
537
+ const desired = yield* prepare(node, current);
538
+
539
+ const observed = yield* read(current).pipe(
540
+ Effect.flatMap(Effect.fromOption),
541
+ );
542
+
543
+ return yield* apply(observed.outputs, desired);
544
+ });
545
+
546
+ /**
547
+ * The observe operation lets reconciliation distinguish a missing resource
548
+ * from a failed read. For a present resource, the registration can return
549
+ * additional details without adding them to the saved outputs.
550
+ *
551
+ * @see ../../../docs/application/resource-operations.md (Read a saved resource)
552
+ */
553
+ const observe = <E = never, R = never>(
554
+ { node }: { readonly node: ResourceNode },
555
+ project: (
556
+ outputs: Outputs,
557
+ ) => Effect.Effect<Schema.JsonObject, E, R> = Effect.succeed,
558
+ ) =>
559
+ Effect.gen(function* () {
560
+ const observed = yield* read(node);
561
+
562
+ const present = yield* observed.pipe(
563
+ Option.map(({ node, outputs }) =>
564
+ project(outputs).pipe(
565
+ Effect.map((observed) =>
566
+ ResourceObservation.Present({ node, observed }),
567
+ ),
568
+ ),
569
+ ),
570
+ Effect.transposeOption,
571
+ );
572
+
573
+ const result: ResourceCommandResult = ResourceCommandResult.Observed({
574
+ observation: Option.getOrElse(present, () =>
575
+ ResourceObservation.Missing({ node }),
576
+ ),
577
+ });
578
+
579
+ return result;
580
+ }).pipe(Effect.withSpan("ResourceLifecycle.observe"));
581
+
582
+ return { observe, create, update, observedUpdate, remove, reconcile };
583
+ }),
584
+ },
585
+ ) {
586
+ /**
587
+ * The commands function matches the actions supported by a native
588
+ * lifecycle. The matcher infers the return type from the registered actions.
589
+ *
590
+ * @see ../../../docs/development/effect-native-development.md (Let Effect infer channels at the call site)
591
+ * @see ../../../node_modules/effect/src/Match.ts (typeTags)
592
+ */
593
+ static commands = Match.typeTags<LifecycleCommand>();
594
+
595
+ /**
596
+ * The execute method runs a direct command or the corresponding step in a
597
+ * reviewed plan. Only a reviewed update supplies the saved node required
598
+ * for confirmation.
599
+ *
600
+ * @see ../../../node_modules/effect/src/Match.ts (tag and when)
601
+ * @see ../../../node_modules/effect/src/Effect.ts (fromOption and flatMap)
602
+ */
603
+ static execute<E, R>(
604
+ run: (
605
+ action: LifecycleCommand,
606
+ ) => Effect.Effect<ResourceCommandResult, E, R>,
607
+ ) {
608
+ const execute = Effect.fn("ResourceLifecycle.execute")(
609
+ (command: ResourceCommand) => {
610
+ const action = Match.value(command).pipe(
611
+ Match.tag("Apply", ({ decision }) => decision),
612
+ Match.orElse((direct) => direct),
613
+ );
614
+
615
+ const lifecycleAction = Match.value(action).pipe(
616
+ Match.tag("Read", "Create", "Destroy", (supported) => supported),
617
+ Match.tag("Delete", ({ node }) => ResourceCommand.Destroy({ node })),
618
+ Match.when({ _tag: "Update", current: Match.defined }, (update) => ({
619
+ ...update,
620
+ command,
621
+ })),
622
+ Match.option,
623
+ );
624
+
625
+ return Effect.fromOption(
626
+ lifecycleAction,
627
+ () => new ResourceCommandUnsupported({ command }),
628
+ ).pipe(Effect.flatMap(run));
629
+ },
630
+ );
631
+
632
+ return { execute };
633
+ }
634
+
635
+ /**
636
+ * The unsupported method records the original command when a registration
637
+ * has no operation for it.
638
+ *
639
+ * @see ./lifecycle.ts (ResourceCommandUnsupported)
640
+ */
641
+ static unsupported({ command }: { readonly command: ResourceCommand }) {
642
+ return Effect.fail(new ResourceCommandUnsupported({ command }));
643
+ }
644
+ }