diff --git a/.changeset/deploy-token-probe.md b/.changeset/deploy-token-probe.md new file mode 100644 index 00000000..06f79dc7 --- /dev/null +++ b/.changeset/deploy-token-probe.md @@ -0,0 +1,5 @@ +--- +"@cloudhal/cli": patch +--- + +Workers Builds deploys no longer fail when the new deployment token is still propagating (#245). After uploading the Workers, `cloudhal deploy` checks whether the deployment already has the site. That check counted any answer other than 200 as "absent", so a 401 from the token not yet having spread led to a push that the deployment refused with "diverged". Now only a 404 means absent: a 401 is retried while the token spreads, any other answer is an error and nothing is pushed, and a push refused because the deployment already has the site counts as already deployed. `cloudhal push` keeps its divergence error. diff --git a/packages/cli/src/commands/sync.ts b/packages/cli/src/commands/sync.ts index 560dd5e7..bcda0956 100644 --- a/packages/cli/src/commands/sync.ts +++ b/packages/cli/src/commands/sync.ts @@ -69,21 +69,39 @@ async function target(options: SyncOptions) { return { cfg, url, token, fetch: f }; } +/** + * `ifAbsent`: whether the deployment already has the site. Only a 200 (it has it) and a 404 (it + * does not) are answers. A 401 throws an error `whileTokenPropagates` retries: right after a + * Workers Builds deploy the edge can still check the new token against the previous admin + * Worker, and pushing then would hit the site the deployment does have. Anything else throws + * too, and nothing is pushed. + */ +async function deploymentHasSite(f: typeof fetch, url: URL, auth: Record) { + const probe = await f(url, { headers: auth }); + if (probe.status === 200) return true; + if (probe.status === 404) return false; + throw new Error( + `checking whether the deployment has the site failed (${probe.status}): ${brief(await probe.text())}; nothing was pushed`, + ); +} + +/** A bootstrap PUT refused because the deployment already has this site (SyncConflictError). */ +const alreadyHasSite = (status: number, body: string) => + status === 409 && body.includes('"error":"conflict"') && body.includes("already has site"); + export async function pushSite( options: SyncOptions & { ifAbsent?: boolean }, ): Promise<{ pushed: boolean; message: string }> { const log = options.log ?? (() => {}); const { cfg, url, token, fetch: f } = await target(options); const auth = { authorization: `Bearer ${token}` }; - if (options.ifAbsent) { - const probe = await f(url, { headers: auth }); - if (probe.status === 200) { - log( - "the deployment already has this site; leaving it as the source of truth (use `cloudhal pull`)", - ); - return { pushed: false, message: "already deployed" }; - } - } + const keepDeployed = () => { + log( + "the deployment already has this site; leaving it as the source of truth (use `cloudhal pull`)", + ); + return { pushed: false, message: "already deployed" }; + }; + if (options.ifAbsent && (await deploymentHasSite(f, url, auth))) return keepDeployed(); const local = await openLocalStore(path.join(options.projectDir, cfg.dataDir)); let bundle: SiteBundle; try { @@ -118,6 +136,9 @@ export async function pushSite( body: JSON.stringify(bundle), }); const body = await res.text(); + // The probe said absent, yet the site is there (it arrived between the two): same outcome. + if (options.ifAbsent && !options.replace && alreadyHasSite(res.status, body)) + return keepDeployed(); if (res.status === 409 && /"error":"schedules_(pending|running)"/.test(body)) { const parsed = JSON.parse(body) as { error: string; diff --git a/packages/cli/test/push-if-absent.test.ts b/packages/cli/test/push-if-absent.test.ts new file mode 100644 index 00000000..479d2cd2 --- /dev/null +++ b/packages/cli/test/push-if-absent.test.ts @@ -0,0 +1,128 @@ +/** + * `pushSite({ ifAbsent })`, the push `cloudhal deploy` makes after uploading the Workers. On + * 2026-11-15, a Workers Builds deploy of cloudhal.com failed: the probe for the site got 401, + * because the new deployment token had not reached the edge yet. It was read as "absent", and + * the bootstrap PUT that followed got 409 for the site the deployment had. Only 200 and 404 + * answer the probe now; a 401 is retried by `whileTokenPropagates`. A bootstrap 409 for that + * site is "already deployed". + */ +import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; +import os from "node:os"; +import path from "node:path"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { whileTokenPropagates } from "../src/commands/deploy/resources.ts"; +import { init, pushSite, readProjectConfig } from "../src/index.ts"; + +const ADMIN = "https://push-admin.example.workers.dev"; +const TOKEN = "test-deploy-token"; +const CONFLICT = JSON.stringify({ + error: "conflict", + message: + 'target already has site site_x with different content (1 node(s) differ: n1); pull first or pass mode "replace"', + details: { differing: ["n1"] }, +}); + +type Call = { method: string; path: string }; + +/** A fetch that answers the bundle probe with `probes` in turn and the bundle PUT with `put`. */ +function stub(probes: number[], put: { status: number; body: string }) { + const calls: Call[] = []; + const queue = [...probes]; + const f = (async (input: URL | RequestInfo, init?: RequestInit) => { + const url = new URL(String(input)); + const method = init?.method ?? "GET"; + calls.push({ method, path: url.pathname }); + expect(new Headers(init?.headers).get("authorization")).toBe(`Bearer ${TOKEN}`); + if (url.pathname.endsWith("/bundle") && method === "GET") { + const status = queue.length > 1 ? (queue.shift() as number) : (queue[0] as number); + const body = status === 404 ? '{"error":"not_found","message":"site not found"}' : "{}"; + return new Response(status === 200 ? "{}" : body, { status }); + } + if (url.pathname.endsWith("/bundle") && method === "PUT") + return new Response(put.body, { status: put.status }); + // Media: the deployment has every file already. + return new Response(null, { status: 200 }); + }) as typeof fetch; + const puts = () => calls.filter((c) => c.method === "PUT" && c.path.endsWith("/bundle")).length; + const probesMade = () => + calls.filter((c) => c.method === "GET" && c.path.endsWith("/bundle")).length; + return { f, puts, probesMade }; +} + +describe("pushSite({ ifAbsent }) after a deploy", () => { + let tmp: string; + let projectDir: string; + const lines: string[] = []; + const log = (l: string) => lines.push(l); + + beforeAll(async () => { + tmp = await mkdtemp(path.join(os.tmpdir(), "cloudhal-push-if-absent-")); + projectDir = path.join(tmp, "site"); + await init({ dir: projectDir, name: "Push If Absent" }); + const cfg = await readProjectConfig(projectDir); + const dir = path.join(projectDir, cfg.dataDir, "deploy"); + await mkdir(dir, { recursive: true }); + await writeFile( + path.join(dir, "secrets.json"), + JSON.stringify({ CLOUDHAL_DEPLOY_TOKEN: TOKEN }), + ); + }); + afterAll(async () => { + await rm(tmp, { recursive: true, force: true }); + }); + + const push = (f: typeof fetch, extra: { ifAbsent?: boolean; replace?: boolean } = {}) => + pushSite({ projectDir, adminUrl: ADMIN, fetch: f, log, ifAbsent: true, ...extra }); + + it("retries a 401 probe while the token spreads, then leaves the deployed site alone", async () => { + const s = stub([401, 401, 200], { status: 500, body: "must not be called" }); + lines.length = 0; + const result = await whileTokenPropagates(() => push(s.f), { log, waitMs: 1 }); + expect(result).toEqual({ pushed: false, message: "already deployed" }); + expect(s.probesMade()).toBe(3); + expect(s.puts()).toBe(0); + expect(lines.join("\n")).toContain("does not accept the new token yet"); + expect(lines.join("\n")).toContain("already has this site"); + }); + + it("pushes the site when the probe answers 404", async () => { + const s = stub([404], { status: 200, body: '{"ok":true}' }); + expect(await push(s.f)).toEqual({ pushed: true, message: '{"ok":true}' }); + expect(s.puts()).toBe(1); + }); + + it("fails on any other probe answer and pushes nothing", async () => { + for (const status of [500, 502, 403]) { + const s = stub([status], { status: 200, body: '{"ok":true}' }); + const err = push(s.f); + await expect(err).rejects.toThrow(`failed (${status})`); + await expect(err).rejects.toThrow(/nothing was pushed/); + expect(s.puts()).toBe(0); + } + // Not retried by whileTokenPropagates: only a 401 is the token spreading. + const s = stub([500], { status: 200, body: "{}" }); + await expect(whileTokenPropagates(() => push(s.f), { log, waitMs: 1 })).rejects.toThrow( + /\(500\)/, + ); + expect(s.probesMade()).toBe(1); + }); + + it("takes a bootstrap 409 for the same site as already deployed", async () => { + const s = stub([404], { status: 409, body: CONFLICT }); + lines.length = 0; + expect(await push(s.f)).toEqual({ pushed: false, message: "already deployed" }); + expect(lines.join("\n")).toContain("already has this site"); + }); + + it("keeps the divergence error for `cloudhal push` (no ifAbsent)", async () => { + const s = stub([], { status: 409, body: CONFLICT }); + await expect(push(s.f, { ifAbsent: false })).rejects.toThrow(/diverged from your local copy/); + expect(s.probesMade()).toBe(0); + // Other 409s under ifAbsent are still errors. + const other = stub([404], { + status: 409, + body: '{"error":"schedules_running","message":"a release is running"}', + }); + await expect(push(other.f)).rejects.toThrow(/a release is running/); + }); +});