The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | /** |
| 2 | * Hot reload for user-supplied Dockerfile/context Cloudflare Containers |
| 3 | * under `alchemy dev`. |
| 4 | * |
| 5 | * These files are NOT imported by the stack, so `bun --watch` never re-runs |
| 6 | * the exec child for them — the reload rides two engine pieces this suite |
| 7 | * pins: |
| 8 | * |
| 9 | * 1. the LOCAL provider's diff recomputes the image hash fresh on every |
| 10 | * plan (the RPC sidecar's ArtifactStore outlives runs, so an unevicted |
| 11 | * memo would compare the FIRST run's hash forever and report noop); |
| 12 | * 2. the local worker runner fs-watches each Build-variant container's |
| 13 | * context and restarts the instance — which IS the docker rebuild — |
| 14 | * when the content fingerprint changes, with no deploy in between. |
| 15 | * |
| 16 | * Requires Docker; skipped when the daemon is unavailable. |
| 17 | */ |
| 18 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 19 | import { describe, expect } from "@alchemy.run/test"; |
| 20 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 21 | import * as Effect from "effect/Effect"; |
| 22 | import * as FileSystem from "effect/FileSystem"; |
| 23 | import * as HttpClient from "effect/http/HttpClient"; |
| 24 | import * as Path from "effect/Path"; |
| 25 | import { MinimumLogLevel } from "effect/References"; |
| 26 | import * as Schedule from "effect/Schedule"; |
| 27 | import * as pathe from "pathe"; |
| 28 | import type { ReloadEchoObject } from "./fixtures/reload/async-worker.ts"; |
| 29 | import { RELOAD_CONTAINER_PORT, RELOAD_CONTEXT_DIR } from "./fixtures/reload/container.ts"; |
| 30 | import ReloadContainerWorker from "./fixtures/reload/worker.ts"; |
| 31 | |
| 32 | const { test } = Test.make({ providers: Cloudflare.providers(), dev: true }); |
| 33 | |
| 34 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 35 | |
| 36 | const dockerfile = (marker: string) => |
| 37 | `FROM busybox:stable |
| 38 | COPY index.html /www/index.html |
| 39 | ENV BAKED_MARKER=${marker} |
| 40 | EXPOSE ${RELOAD_CONTAINER_PORT} |
| 41 | CMD ["sh", "-c", "echo -n \\"$BAKED_MARKER\\" > /www/baked.txt && exec httpd -f -p ${RELOAD_CONTAINER_PORT} -h /www"] |
| 42 | `; |
| 43 | |
| 44 | /** Poll the worker's proxy route until the file body matches. */ |
| 45 | const pollText = Effect.fn(function* (options: { |
| 46 | url: string; |
| 47 | path: string; |
| 48 | expected: string; |
| 49 | times?: number; |
| 50 | }) { |
| 51 | const client = yield* HttpClient.HttpClient; |
| 52 | const body = yield* client.get(`${options.url}${options.path}`).pipe( |
| 53 | Effect.flatMap((response) => response.text), |
| 54 | Effect.retry({ |
| 55 | while: (): boolean => true, |
| 56 | schedule: Schedule.spaced("2 seconds"), |
| 57 | times: options.times ?? 90, |
| 58 | }), |
| 59 | Effect.repeat({ |
| 60 | schedule: Schedule.spaced("2 seconds"), |
| 61 | until: (b): boolean => b.trim() === options.expected, |
| 62 | times: options.times ?? 90, |
| 63 | }), |
| 64 | ); |
| 65 | expect(body.trim()).toBe(options.expected); |
| 66 | }); |
| 67 | |
| 68 | /** |
| 69 | * Async Worker hosting a container-backed DO, with a `MARKER` env var whose |
| 70 | * change restarts the Worker (a new workerd generation) WITHOUT touching the |
| 71 | * container image. |
| 72 | */ |
| 73 | const reloadEnvWorker = (marker: string) => |
| 74 | Cloudflare.Worker("ReloadEnvContainerWorker", { |
| 75 | main: pathe.resolve(import.meta.dirname, "fixtures/reload/async-worker.ts"), |
| 76 | env: { |
| 77 | ECHO: Cloudflare.Container<ReloadEchoObject>("ECHO", { |
| 78 | className: "ReloadEchoObject", |
| 79 | image: "mendhak/http-https-echo:latest", |
| 80 | }), |
| 81 | MARKER: marker, |
| 82 | }, |
| 83 | }); |
| 84 | |
| 85 | /** One request into the container; fails unless the echo server answered. */ |
| 86 | const echo = Effect.fn(function* (url: string) { |
| 87 | const client = yield* HttpClient.HttpClient; |
| 88 | const response = yield* client.get(new URL("/hello", url)); |
| 89 | const text = yield* response.text; |
| 90 | if (response.status !== 200 || !text.includes("method")) { |
| 91 | return yield* Effect.fail( |
| 92 | new Error(`container request failed: ${response.status} ${text.slice(0, 500)}`), |
| 93 | ); |
| 94 | } |
| 95 | return text; |
| 96 | }); |
| 97 | |
| 98 | describe.sequential( |
| 99 | "LocalContainerReload", |
| 100 | { |
| 101 | tags: [ |
| 102 | "provider:cloudflare", |
| 103 | "provider:cloudflare:container", |
| 104 | "provider:cloudflare:worker", |
| 105 | "local", |
| 106 | ], |
| 107 | }, |
| 108 | () => { |
| 109 | test.provider( |
| 110 | "context/Dockerfile edits rebuild the running container — with and without a deploy", |
| 111 | (stack) => |
| 112 | Effect.gen(function* () { |
| 113 | const fs = yield* FileSystem.FileSystem; |
| 114 | const path = yield* Path.Path; |
| 115 | |
| 116 | yield* stack.destroy(); |
| 117 | |
| 118 | // Materialize the build context at the fixture's fixed path. |
| 119 | yield* fs.makeDirectory(RELOAD_CONTEXT_DIR, { recursive: true }); |
| 120 | yield* fs.writeFileString( |
| 121 | path.join(RELOAD_CONTEXT_DIR, "Dockerfile"), |
| 122 | dockerfile("baked-v1"), |
| 123 | ); |
| 124 | yield* fs.writeFileString(path.join(RELOAD_CONTEXT_DIR, "index.html"), "content-v1\n"); |
| 125 | |
| 126 | const deploy = Effect.gen(function* () { |
| 127 | const worker = yield* ReloadContainerWorker; |
| 128 | return { worker }; |
| 129 | }); |
| 130 | |
| 131 | const first = yield* stack.deploy(deploy); |
| 132 | expect(first.worker.url).toMatch(/^http:\/\/localhost:\d+/); |
| 133 | const url = first.worker.url!; |
| 134 | |
| 135 | // First contact builds the image and boots the container. |
| 136 | yield* pollText({ url, path: "/index.html", expected: "content-v1" }); |
| 137 | yield* pollText({ url, path: "/baked.txt", expected: "baked-v1" }); |
| 138 | |
| 139 | // ── 1. content change + REDEPLOY: the diff must see it (regression: |
| 140 | // the sidecar-lifetime artifact memo made every later plan compare |
| 141 | // the first run's hash and noop forever) ── |
| 142 | yield* fs.writeFileString(path.join(RELOAD_CONTEXT_DIR, "index.html"), "content-v2\n"); |
| 143 | yield* stack.deploy(deploy); |
| 144 | yield* pollText({ url, path: "/index.html", expected: "content-v2" }); |
| 145 | |
| 146 | // ── 2. content change, NO deploy: the worker runner's context |
| 147 | // watcher must rebuild + restart on its own ── |
| 148 | yield* fs.writeFileString(path.join(RELOAD_CONTEXT_DIR, "index.html"), "content-v3\n"); |
| 149 | yield* pollText({ url, path: "/index.html", expected: "content-v3" }); |
| 150 | |
| 151 | // ── 3. the DOCKERFILE itself, NO deploy ── |
| 152 | yield* fs.writeFileString( |
| 153 | path.join(RELOAD_CONTEXT_DIR, "Dockerfile"), |
| 154 | dockerfile("baked-v2"), |
| 155 | ); |
| 156 | yield* pollText({ url, path: "/baked.txt", expected: "baked-v2" }); |
| 157 | // The content file survived the Dockerfile rebuild. |
| 158 | yield* pollText({ url, path: "/index.html", expected: "content-v3" }); |
| 159 | |
| 160 | yield* stack.destroy(); |
| 161 | }).pipe(logLevel), |
| 162 | { timeout: 600_000 }, |
| 163 | ); |
| 164 | |
| 165 | // Regression: a Worker restart while its container is RUNNING. The |
| 166 | // replacement workerd generation used to start while the previous one |
| 167 | // (and its container) was still up, so the new generation "recovered" |
| 168 | // the running container — and the previous generation's teardown then |
| 169 | // removed that container (and its networking sidecar) out from under |
| 170 | // it: `Recovered running container without a running networking |
| 171 | // sidecar`, then `container <id> is not running` on the next request. |
| 172 | test.provider( |
| 173 | "a running container keeps serving across Worker restarts", |
| 174 | (stack) => |
| 175 | Effect.gen(function* () { |
| 176 | yield* stack.destroy(); |
| 177 | |
| 178 | const first = yield* stack.deploy(reloadEnvWorker("v1")); |
| 179 | expect(first.url).toMatch(/^http:\/\/localhost:\d+/); |
| 180 | const url = first.url!; |
| 181 | |
| 182 | // First contact pulls the image and boots the container, which |
| 183 | // then stays running (`sleepAfter`). |
| 184 | yield* echo(url).pipe( |
| 185 | Effect.timeout("30 seconds"), |
| 186 | Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 60 }), |
| 187 | ); |
| 188 | |
| 189 | for (const marker of ["v2", "v3", "v4"]) { |
| 190 | yield* stack.deploy(reloadEnvWorker(marker)); |
| 191 | yield* pollText({ url, path: "/marker", expected: marker, times: 15 }); |
| 192 | // No retries: the container must answer straight away, and keep |
| 193 | // answering, from the new generation. |
| 194 | for (let i = 0; i < 3; i++) { |
| 195 | yield* echo(url).pipe(Effect.timeout("60 seconds")); |
| 196 | yield* Effect.sleep("1 second"); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | yield* stack.destroy(); |
| 201 | }).pipe(logLevel), |
| 202 | { timeout: 600_000 }, |
| 203 | ); |
| 204 | }, |
| 205 | ); |