The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect } from "@alchemy.run/test"; |
| 2 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 3 | import * as Cause from "effect/Cause"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Exit from "effect/Exit"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | /** |
| 8 | * Destroy robustness: a destroy (or the GC phase of a deploy) must delete |
| 9 | * everything it can, aggregate every failure, and never let one bad row |
| 10 | * block the rest of the teardown. |
| 11 | * |
| 12 | * - a failing provider `delete` must not interrupt sibling deletions |
| 13 | * - an upstream's delete is skipped (state retained) when a dependent fails |
| 14 | * - a "zombie" state row whose resource type has no registered provider |
| 15 | * (the type was removed/renamed without an alias) is FATAL at plan time: |
| 16 | * the plan dies with a typed `MissingProviderError` and nothing is |
| 17 | * deployed or destroyed — the program and state disagree, and without |
| 18 | * the provider the row cannot be deleted anyway |
| 19 | * - an attr-less row whose `read` recovery fails must not block siblings |
| 20 | */ |
| 21 | import { MissingProviderError } from "@/Provider"; |
| 22 | import { Stack } from "@/Stack"; |
| 23 | import { State, type ResourceState } from "@/State"; |
| 24 | import { TestLayers, TestResource, TestResourceHooks } from "./test.resources.ts"; |
| 25 | |
| 26 | const { test } = Test.make({ providers: TestLayers() }); |
| 27 | |
| 28 | const getState = Effect.fn(function* (fqn: string) { |
| 29 | const state = yield* yield* State; |
| 30 | const stk = yield* Stack; |
| 31 | return yield* state.get({ stack: stk.name, stage: stk.stage, fqn }); |
| 32 | }); |
| 33 | |
| 34 | const clearState = Effect.fn(function* (fqn: string) { |
| 35 | const state = yield* yield* State; |
| 36 | const stk = yield* Stack; |
| 37 | yield* state.delete({ stack: stk.name, stage: stk.stage, fqn }); |
| 38 | }); |
| 39 | |
| 40 | const seed = Effect.fn(function* (fqn: string, value: ResourceState) { |
| 41 | const state = yield* yield* State; |
| 42 | const stk = yield* Stack; |
| 43 | yield* state.set({ stack: stk.name, stage: stk.stage, fqn, value }); |
| 44 | }); |
| 45 | |
| 46 | const instanceId = "852f6ec2e19b66589825efe14dca2971"; |
| 47 | |
| 48 | const hooks = (impl: { |
| 49 | delete?: (id: string) => Effect.Effect<void, any>; |
| 50 | read?: (id: string) => Effect.Effect<any, any>; |
| 51 | }): Layer.Layer<TestResourceHooks> => Layer.succeed(TestResourceHooks, impl); |
| 52 | |
| 53 | const failsWith = (exit: Exit.Exit<unknown, unknown>, tag: string): boolean => |
| 54 | Exit.isFailure(exit) && |
| 55 | exit.cause.reasons.some( |
| 56 | (r) => Cause.isFailReason(r) && (r.error as { _tag?: string } | undefined)?._tag === tag, |
| 57 | ); |
| 58 | |
| 59 | describe("destroy aggregates failures", { tags: ["unit", "local"] }, () => { |
| 60 | test.provider("a failing delete does not interrupt sibling deletions", (stack) => |
| 61 | Effect.gen(function* () { |
| 62 | yield* stack.deploy( |
| 63 | Effect.gen(function* () { |
| 64 | yield* TestResource("A", { string: "a" }); |
| 65 | yield* TestResource("B", { string: "b" }); |
| 66 | yield* TestResource("C", { string: "c" }); |
| 67 | }), |
| 68 | ); |
| 69 | |
| 70 | const exit = yield* stack.destroy().pipe( |
| 71 | Effect.provide( |
| 72 | hooks({ |
| 73 | delete: (id) => |
| 74 | id === "A" |
| 75 | ? Effect.fail(new Error("A refuses to die")) |
| 76 | : id === "B" |
| 77 | ? // Slow sibling: before aggregation, A's failure |
| 78 | // interrupted B mid-delete and left its row behind. |
| 79 | Effect.sleep("150 millis") |
| 80 | : Effect.void, |
| 81 | }), |
| 82 | ), |
| 83 | Effect.exit, |
| 84 | ); |
| 85 | |
| 86 | // The destroy still fails overall... |
| 87 | expect(Exit.isFailure(exit)).toBe(true); |
| 88 | // ...but every deletable resource was deleted. |
| 89 | expect(yield* getState("B")).toBeUndefined(); |
| 90 | expect(yield* getState("C")).toBeUndefined(); |
| 91 | // The failed row keeps its state for the next attempt. |
| 92 | expect((yield* getState("A"))?.status).toBe("deleting"); |
| 93 | }), |
| 94 | ); |
| 95 | |
| 96 | test.provider("an upstream's delete is skipped when a dependent fails", (stack) => |
| 97 | Effect.gen(function* () { |
| 98 | yield* stack.deploy( |
| 99 | Effect.gen(function* () { |
| 100 | const a = yield* TestResource("A", { string: "a" }); |
| 101 | yield* TestResource("B", { string: a.string }); |
| 102 | }), |
| 103 | ); |
| 104 | |
| 105 | const deleted: string[] = []; |
| 106 | const exit = yield* stack.destroy().pipe( |
| 107 | Effect.provide( |
| 108 | hooks({ |
| 109 | delete: (id) => |
| 110 | id === "B" |
| 111 | ? Effect.fail(new Error("B refuses to die")) |
| 112 | : Effect.sync(() => void deleted.push(id)), |
| 113 | }), |
| 114 | ), |
| 115 | Effect.exit, |
| 116 | ); |
| 117 | |
| 118 | expect(Exit.isFailure(exit)).toBe(true); |
| 119 | // A still has a dependent in the cloud — its physical delete must |
| 120 | // not have been attempted and its state must be retained. |
| 121 | expect(deleted).not.toContain("A"); |
| 122 | expect(yield* getState("A")).toBeDefined(); |
| 123 | expect(yield* getState("B")).toBeDefined(); |
| 124 | }), |
| 125 | ); |
| 126 | |
| 127 | test.provider("an attr-less row with failing read recovery does not block siblings", (stack) => |
| 128 | Effect.gen(function* () { |
| 129 | yield* stack.deploy( |
| 130 | Effect.gen(function* () { |
| 131 | yield* TestResource("A", { string: "a" }); |
| 132 | }), |
| 133 | ); |
| 134 | // An interrupted create: `creating` with no attr snapshot. |
| 135 | yield* seed("Zombie", { |
| 136 | instanceId, |
| 137 | providerVersion: 0, |
| 138 | logicalId: "Zombie", |
| 139 | fqn: "Zombie", |
| 140 | namespace: undefined, |
| 141 | resourceType: "Test.TestResource", |
| 142 | status: "creating", |
| 143 | props: { string: "z" }, |
| 144 | attr: undefined, |
| 145 | bindings: [], |
| 146 | downstream: [], |
| 147 | }); |
| 148 | |
| 149 | const exit = yield* stack.destroy().pipe( |
| 150 | Effect.provide( |
| 151 | hooks({ |
| 152 | read: () => Effect.fail(new Error("read exploded")), |
| 153 | // Slow sibling so a fail-fast interruption would be observable. |
| 154 | delete: () => Effect.sleep("150 millis"), |
| 155 | }), |
| 156 | ), |
| 157 | Effect.exit, |
| 158 | ); |
| 159 | |
| 160 | expect(Exit.isFailure(exit)).toBe(true); |
| 161 | expect(yield* getState("A")).toBeUndefined(); |
| 162 | // The unrecoverable row is retained, not silently dropped. |
| 163 | expect(yield* getState("Zombie")).toBeDefined(); |
| 164 | }), |
| 165 | ); |
| 166 | }); |
| 167 | |
| 168 | describe("zombie rows (no registered provider)", { tags: ["unit", "local"] }, () => { |
| 169 | const ghostRow = (fqn: string): ResourceState => ({ |
| 170 | instanceId, |
| 171 | providerVersion: 0, |
| 172 | logicalId: fqn, |
| 173 | fqn, |
| 174 | namespace: undefined, |
| 175 | resourceType: "Test.Vanished", |
| 176 | status: "created", |
| 177 | props: { name: "ghost" }, |
| 178 | attr: { name: "ghost" }, |
| 179 | bindings: [], |
| 180 | downstream: [], |
| 181 | }); |
| 182 | |
| 183 | const diesWithMissingProvider = ( |
| 184 | exit: Exit.Exit<unknown, unknown>, |
| 185 | ): MissingProviderError | undefined => { |
| 186 | if (!Exit.isFailure(exit)) return undefined; |
| 187 | const reason = exit.cause.reasons.find( |
| 188 | (r) => Cause.isDieReason(r) && r.defect instanceof MissingProviderError, |
| 189 | ); |
| 190 | return reason && Cause.isDieReason(reason) |
| 191 | ? (reason.defect as MissingProviderError) |
| 192 | : undefined; |
| 193 | }; |
| 194 | |
| 195 | test.provider("destroy dies at plan time and destroys nothing", (stack) => |
| 196 | Effect.gen(function* () { |
| 197 | yield* stack.deploy( |
| 198 | Effect.gen(function* () { |
| 199 | yield* TestResource("A", { string: "a" }); |
| 200 | }), |
| 201 | ); |
| 202 | yield* seed("Ghost", ghostRow("Ghost")); |
| 203 | |
| 204 | const exit = yield* stack.destroy().pipe(Effect.exit); |
| 205 | |
| 206 | const defect = diesWithMissingProvider(exit); |
| 207 | expect(defect?.resourceType).toBe("Test.Vanished"); |
| 208 | expect(defect?.fqn).toBe("Ghost"); |
| 209 | // Fatal at plan time: NOTHING was destroyed — A's state is intact. |
| 210 | expect((yield* getState("A"))?.status).toBe("created"); |
| 211 | expect(yield* getState("Ghost")).toBeDefined(); |
| 212 | |
| 213 | // Remediation path: clear the zombie row (as the error message |
| 214 | // instructs), then destroy proceeds normally. |
| 215 | yield* clearState("Ghost"); |
| 216 | yield* stack.destroy(); |
| 217 | expect(yield* getState("A")).toBeUndefined(); |
| 218 | }), |
| 219 | ); |
| 220 | |
| 221 | test.provider("a deploy with a zombie orphan dies at plan time and applies nothing", (stack) => |
| 222 | Effect.gen(function* () { |
| 223 | yield* seed("Ghost", ghostRow("Ghost")); |
| 224 | |
| 225 | const exit = yield* stack |
| 226 | .deploy( |
| 227 | Effect.gen(function* () { |
| 228 | const a = yield* TestResource("A", { string: "a" }); |
| 229 | return a.string; |
| 230 | }), |
| 231 | ) |
| 232 | .pipe(Effect.exit); |
| 233 | |
| 234 | expect(diesWithMissingProvider(exit)?.resourceType).toBe("Test.Vanished"); |
| 235 | // Fatal at plan time: the program was NOT applied. |
| 236 | expect(yield* getState("A")).toBeUndefined(); |
| 237 | expect(yield* getState("Ghost")).toBeDefined(); |
| 238 | |
| 239 | yield* clearState("Ghost"); |
| 240 | }), |
| 241 | ); |
| 242 | }); |