The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import { describe, expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as HttpClient from "effect/http/HttpClient"; |
| 6 | import { isHttpClientError } from "effect/http/HttpClientError"; |
| 7 | import * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import { waitUntilStable } from "../Utils/WorkerRequest.ts"; |
| 11 | import Stack from "./fixtures/do-abort/stack.ts"; |
| 12 | |
| 13 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 14 | providers: Cloudflare.providers(), |
| 15 | state: Cloudflare.state(), |
| 16 | }); |
| 17 | |
| 18 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 19 | |
| 20 | const stack = beforeAll(deploy(Stack)); |
| 21 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack)); |
| 22 | |
| 23 | let bust = 0; |
| 24 | /** |
| 25 | * `url` plus the per-attempt object `name` and a cache buster. Every attempt |
| 26 | * addresses a fresh Durable Object, so a retried attempt never inherits the |
| 27 | * `boots`/`failedPings` counters a failed attempt left behind. |
| 28 | */ |
| 29 | const withQuery = (url: string, name: string) => |
| 30 | Effect.sync(() => `${url}?name=${name}&cb=${Date.now()}-${bust++}`); |
| 31 | |
| 32 | const getJson = <T>( |
| 33 | client: HttpClient.HttpClient, |
| 34 | url: string, |
| 35 | name: string, |
| 36 | phase: "readiness" | "before abort" | "after abort", |
| 37 | ): Effect.Effect<T, unknown> => |
| 38 | withQuery(url, name).pipe( |
| 39 | Effect.flatMap((url) => client.get(url, { headers: { "cache-control": "no-cache" } })), |
| 40 | Effect.flatMap(HttpClientResponse.filterStatusOk), |
| 41 | Effect.tapError((error) => |
| 42 | Effect.gen(function* () { |
| 43 | if (error.reason._tag === "StatusCodeError") { |
| 44 | const response = error.reason.response; |
| 45 | const body = yield* response.text.pipe( |
| 46 | Effect.catch(() => Effect.succeed("<unreadable response body>")), |
| 47 | ); |
| 48 | yield* (response.status === 404 ? Effect.logDebug : Effect.logError)( |
| 49 | `${phase}: GET ${response.request.url} returned ${response.status}`, |
| 50 | body, |
| 51 | ); |
| 52 | } else { |
| 53 | yield* Effect.logError(`${phase}: GET ${url} failed`, error); |
| 54 | } |
| 55 | }), |
| 56 | ), |
| 57 | Effect.retry({ |
| 58 | while: (error) => |
| 59 | Effect.gen(function* () { |
| 60 | // Only the read-only `/ping` is retried — never `/fail-ping` (it |
| 61 | // bumps `failedPings`) and never `/abort`. Every phase retries: under |
| 62 | // load a ping between the steps below can hit the same transient |
| 63 | // platform errors as readiness, and the exact `boots`/`failedPings` |
| 64 | // assertions still catch any real reset. |
| 65 | if (new URL(url).pathname !== "/ping") return false; |
| 66 | if (error.reason._tag === "TransportError") return true; |
| 67 | if (error.reason._tag !== "StatusCodeError") return false; |
| 68 | const response = error.reason.response; |
| 69 | const html = (response.headers["content-type"] ?? "").includes("text/html"); |
| 70 | if (response.status === 404 && html) return true; |
| 71 | if (response.status !== 500) return false; |
| 72 | if (html) { |
| 73 | const body = yield* response.text.pipe(Effect.orElseSucceed(() => "")); |
| 74 | return ( |
| 75 | body.includes('<span class="cf-error-code">1104</span>') && |
| 76 | body.includes("Script not found") |
| 77 | ); |
| 78 | } |
| 79 | // The fixture flags an opaque `internal error; reference = …` RPC |
| 80 | // failure on `/ping` as retryable; only the read-only ping tolerates it. |
| 81 | return response.headers["x-do-readiness-retry"] === "true"; |
| 82 | }), |
| 83 | schedule: Schedule.spaced("3 seconds"), |
| 84 | times: 8, |
| 85 | }), |
| 86 | Effect.flatMap((res) => res.json as Effect.Effect<T>), |
| 87 | ); |
| 88 | |
| 89 | describe.skipIf(!!process.env.FAST)( |
| 90 | "DurableObjectState.abort resets the isolate", |
| 91 | { tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"] }, |
| 92 | () => { |
| 93 | test( |
| 94 | "abort resets the Durable Object so the constructor re-runs", |
| 95 | Effect.gen(function* () { |
| 96 | const { url } = yield* stack; |
| 97 | const client = yield* HttpClient.HttpClient; |
| 98 | const name = yield* Effect.sync(() => `abort-${crypto.randomUUID()}`); |
| 99 | yield* Effect.logInfo("Durable Object name", name); |
| 100 | |
| 101 | // Ready means three pings in a row reach the same Durable Object |
| 102 | // incarnation: a host still rolling out the new version answers 404, |
| 103 | // and a version swap restarts the object (boots changes), which would |
| 104 | // break the before/after-abort comparison below. |
| 105 | let lastBoots: number | undefined; |
| 106 | yield* waitUntilStable( |
| 107 | `Durable Object at ${url}`, |
| 108 | getJson<{ boots: number }>(client, `${url}/ping`, name, "readiness").pipe( |
| 109 | Effect.map(({ boots }) => { |
| 110 | const same = boots === lastBoots; |
| 111 | lastBoots = boots; |
| 112 | return same; |
| 113 | }), |
| 114 | ), |
| 115 | { consecutive: 2, timeout: "60 seconds" }, |
| 116 | ); |
| 117 | const before = yield* getJson<{ boots: number; ok: true }>( |
| 118 | client, |
| 119 | `${url}/ping`, |
| 120 | name, |
| 121 | "before abort", |
| 122 | ); |
| 123 | yield* Effect.logInfo("before abort", before); |
| 124 | expect(before.ok).toBe(true); |
| 125 | expect(before.boots).toBeGreaterThanOrEqual(1); |
| 126 | |
| 127 | const failure = yield* getJson(client, `${url}/fail-ping`, name, "before abort").pipe( |
| 128 | Effect.flip, |
| 129 | ); |
| 130 | if (!isHttpClientError(failure) || failure.reason._tag !== "StatusCodeError") { |
| 131 | return yield* Effect.die(failure); |
| 132 | } |
| 133 | expect(failure.reason.response.status).toBe(500); |
| 134 | expect(failure.reason.response.headers["x-do-readiness-retry"]).toBe("false"); |
| 135 | expect(yield* failure.reason.response.text).toContain("application-ping-failure"); |
| 136 | const unchanged = yield* getJson<{ boots: number; failedPings: number }>( |
| 137 | client, |
| 138 | `${url}/ping`, |
| 139 | name, |
| 140 | "before abort", |
| 141 | ); |
| 142 | yield* Effect.logInfo("after failed ping", unchanged); |
| 143 | expect(unchanged.boots).toBe(before.boots); |
| 144 | expect(unchanged.failedPings).toBe(1); |
| 145 | |
| 146 | const aborted = yield* withQuery(`${url}/abort`, name).pipe( |
| 147 | Effect.flatMap((url) => client.get(url, { headers: { "cache-control": "no-cache" } })), |
| 148 | Effect.flatMap(HttpClientResponse.filterStatusOk), |
| 149 | Effect.flatMap((response) => response.text), |
| 150 | ); |
| 151 | yield* Effect.logInfo("abort RPC", aborted); |
| 152 | expect(aborted).toContain("test abort"); |
| 153 | |
| 154 | const after = yield* getJson<{ boots: number; ok: true }>( |
| 155 | client, |
| 156 | `${url}/ping`, |
| 157 | name, |
| 158 | "after abort", |
| 159 | ); |
| 160 | yield* Effect.logInfo("after abort", after); |
| 161 | expect(after.ok).toBe(true); |
| 162 | expect(after.boots).toBe(before.boots + 1); |
| 163 | }).pipe(logLevel), |
| 164 | { timeout: 120_000 }, |
| 165 | ); |
| 166 | }, |
| 167 | ); |