Skip to content

chore(build): make preview discovery survive a slow cloudflare deploy - #134

Merged
Ardnived merged 5 commits into
mainfrom
chore/preview-discovery-timeout
Aug 8, 2026
Merged

Ardnived merged 5 commits into
mainfrom
chore/preview-discovery-timeout

Conversation

@Ardnived

@Ardnived Ardnived commented Aug 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The preview-discovery deadline was 6 minutes, which was not a margin over
Cloudflare's build time — it was that build time. Measured across 86
successful Cloudflare Pages: sahajatlas check runs on this repo (every commit
on main plus every PR head from #58 to #128), push → check run posted:

p50 p75 p90 p95 max
99s 138s 232s 373s 453s

360s sat below the 95th percentile. #124 came in at 397s, and since #99 made
a missing preview hard-fail a same-repo PR, that showed up as a red check on a
healthy commit.

The tail is queueing, not build size — the slow samples cluster in windows
where several PRs deploy at once, which is exactly how this repo is worked:

00:04:19Z  PR#126  280s
00:04:21Z  PR#127  393s
00:04:54Z  PR#125  453s
00:11:58Z  PR#124  397s   ← the failure in the ticket

An hour either side of that window, the same repo builds in 74–121s.

What changed

1. The deadline is 10 minutes, overridable via PREVIEW_TIMEOUT_MS — ~1.3×
the slowest sample ever observed, with headroom for a deeper queue. It fits
inside the Smoke job's existing timeout-minutes: 15 alongside install and the
specs, so that cap is untouched (with a comment on both sides saying the two move
together). The header comment records the measurement, not a restatement of the
constant.

2. "No preview" is no longer one outcome. A second output, preview_status,
names which path we took — and only one of them is a defect:

status meaning same-repo PR
found reachable URL for CF_PROJECT specs run
unreachable a URL was posted, never answered red — re-run
pending a deploy demonstrably exists, no usable URL yet red — re-run
absent no Cloudflare signal at all for this SHA red — investigate
failed our project's run finished and did not succeed red — read the Cloudflare log
error discovery itself failed (missing env, a throw) red — read the step

failed is the row that has to exist, and it came out of code review: a failed Cloudflare build still posts a check run, so folding it into "a deploy exists" would tell the reader to re-run a job that fails identically — training the exact habit this ticket set out to break, and making the transient-rate-limit advice unreachable in the one case it was written for. Evidence is therefore read from the run matching CF_PROJECT's own slug, not whichever run GitHub lists first: the -design sibling succeeding says nothing about the app's build.

The #99 invariant is untouched: every empty-URL path still fails a same-repo
PR, so a green Smoke check still means the specs ran. Only the instruction
differs — and an unrecognised status takes the loud branch on purpose. Forked PRs
still warn and exit 0; the script still exits 0 on every path.

3. The step summary says how long it waited and what it last saw, so a slow
deploy is legible without opening the raw log.

Correction: there IS a "still building" signal — this PR's own CI disproved my finding

I first reported the opposite, and it was wrong. This PR's Smoke run logged check run "Cloudflare Pages: sahajatlas" (in_progress) on seven consecutive polls before the URL appeared. Cloudflare does publish a building state.

The error is worth recording because it is repeatable, and because it survived a full code review and a security review — only the live run caught it:

Cloudflare sets started_at when it completes a check run, so every finished run reads started_at == completed_at. Sampling historical commits — which is what #124's evidence in the ticket and my own 230-run sweep both did — cannot see the in-progress window at all, and concludes it never existed.

So the ticket's open design question resolves the other way: adaptive extension on a live signal is available. (The check suite is genuinely useless either way — GitHub pre-creates one per installed app on every push, so Cloudflare's is indistinguishable from the vercel / railway / sentry suites of apps that post nothing.)

The flat deadline still stands, and the reason survives the correction: it is measured against the full duration distribution and clears the slowest build ever observed by ~1.3×, so extending adaptively would only change behaviour in cases a long-enough deadline already covers — at the price of a second timing rule. The door is now open if the queue ever outgrows the cap, and that is written down.

What the live state does buy is honest classification: pending is backed by our own project's run reporting in_progress, rather than only by the -design sibling or the bot comment. It also makes the failed guard's status === 'completed' test load-bearing rather than incidental — an in-progress run has a null conclusion and must never read as a failure.

The duration measurements above are unaffected — they are completed_at minus push time, which a retrospective query reports correctly.

Non-goals honoured

pick()'s hostname-boundary matching is behaviourally unchanged — it gained an
optional host parameter defaulting to the module's project, purely so the new
spec can exercise it. The four discovery sources (#122) and the bot/app author
restrictions on sources 3 and 4 are unchanged, and the new evidence gathering
never widens what reaches pick().

Verification

Exercised against the live GitHub API with GITHUB_OUTPUT / GITHUB_STEP_SUMMARY
pointed at temp files:

case how result
found #124 head, real project preview_url=https://2bdf873b.sahajatlas.pages.dev, 3s
pending same SHA, unknown project, 1s budget pending, evidence check run "Cloudflare Pages: sahajatlas" (success)
absent a SHA Cloudflare never touched absent, "no check run, no deployment, no bot comment"
error PR_HEAD_SHA unset error, exit 0

The ci.yml reporting step was extracted and run as a shell script for every
status plus an unknown one and a fork: the two instruction sets read differently,
all same-repo cases exit 1, the fork warns and exits 0.

scripts/get-cloudflare-preview-url.test.ts (new, 11 cases) pins pick()'s
label-boundary matching — previously untested, and the ticket's stated security
non-goal — alongside the classification and the two message registers.

pnpm lint && pnpm typecheck && pnpm test:run green.

This PR is its own last acceptance criterion: the Smoke job should discover
the preview and run the specs on the first attempt.

Follow-up noticed, not fixed here

Source 4 harvests URLs from the bot comment unscoped by SHA, and that comment
also carries a branch preview URL that pick() accepts — so in principle the
lane could smoke-test a previous commit's build. Circumstantially it doesn't today
(on #124 attempt 1 discovery found nothing for 6 minutes, so the bot clears the URL
cells while deploying), but narrowing a discovery source is #122's territory and an
explicit non-goal here. Worth its own ticket.

Closes #132

🤖 Generated with Claude Code

Ardnived and others added 3 commits August 7, 2026 19:42
The 6-minute deadline was not a margin over Cloudflare's build time, it WAS
that build time: across 86 measured `Cloudflare Pages: sahajatlas` check runs
on this repo the p95 is 373s, so 360s sat below it. #124 came in at 397s and
#99's hard-fail turned a healthy commit red.

Raise it to 10 minutes (overridable via PREVIEW_TIMEOUT_MS) and, more usefully,
stop collapsing every unhappy path into one empty output. A second
`preview_status` output names which it was, and ci.yml gives the two cases
different instructions: `absent` (no Cloudflare signal at all) says investigate,
`pending`/`unreachable` (a deploy demonstrably exists) says re-run. Both still
fail a same-repo PR, so the #99 invariant is untouched — a green Smoke check
still means the specs ran.

The wait is flat rather than adaptive because there is nothing to wait on:
across 230 check runs every one was already `completed` when first visible
(`started_at == completed_at`), and GitHub pre-creates a `queued` check suite
per installed app, so Cloudflare's is indistinguishable from the suites of apps
that never post. That verification is recorded in the script header and in
.claude/rules/tests.md rather than left to be re-derived.

Sources and `pick()` are unchanged; a new spec pins the latter's hostname
boundary, which was previously untested.

Closes #132

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Three follow-ups from exercising the script against the live API:
- `explain()` no longer restates the elapsed time that `emit()` already
  prefixes every line with (it read "after 18s … after 18s").
- the poll loop no longer sleeps past its own deadline, so the reported wait
  is the budget we set rather than the budget plus a trailing interval.
- ci.yml gives `error` its own message: the discovery step itself failed, which
  is not "Cloudflare posted nothing". An unrecognised status still takes the
  loud branch.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Self-triage of the branch diff:

- Source 3 accepts a check run whose NAME begins with "cloudflare pages" from
  ANY installed app, not only Cloudflare's, and that name is now quoted into the
  step summary. `report()` only defangs a LEADING `::`, so a newline in a crafted
  name could forge a summary line mid-string. `note()` flattens whitespace and
  caps at 120 chars before any API text is quoted.
- `explain()`'s `detail` arm was unreachable — `fail()` passes the error message
  straight to `emit()` and never routes through it. Dropped, so the three
  timeout outcomes are the whole function.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Aug 8, 2026 •

Copy link
Copy Markdown

Deploying sahajatlas with  Cloudflare Pages  Cloudflare Pages

Latest commit: 1fd0de8
Status: ✅  Deploy successful!
Preview URL: https://cb474d03.sahajatlas.pages.dev
Branch Preview URL: https://chore-preview-discovery-time.sahajatlas.pages.dev

View logs

Review triage. The headline finding: a FAILED Cloudflare build still posts a
check run, so counting any run as "a deploy exists" classified it `pending` and
told the reader to RE-RUN a job that fails identically — training the exact
habit #132 set out to break, and making the rate-limit advice on the `absent`
branch unreachable in the one case it was written for.

- add a `failed` status, taken from the run whose name matches CF_PROJECT's own
  slug (the `-design` sibling succeeding says nothing about the app's build, and
  its "(success)" was often the only evidence shown). It outranks every other
  signal, and ci.yml tells the reader to read the Cloudflare log.
- bound every request with AbortSignal.timeout. undici defaults to 300s, so one
  hung socket could overrun the budget by 5 minutes and trip the job's
  `timeout-minutes` — which CANCELS the job, skipping the reporting step and
  leaving a red check with no message at all. Strictly worse than the timeout
  this ticket fixes.
- validate + cap PREVIEW_TIMEOUT_MS. `600` read as seconds was a 0.6s deadline
  and a confident "the deploy did not happen"; document the unit.
- write GITHUB_OUTPUT before the step summary, so an unwritable summary can't
  take the contract down with it.
- narrow source 2's evidence to /cloudflare/ (bare `pages` matched
  `github-pages`) and require a Cloudflare creator on source 1.
- lowercase pick()'s host — an uppercase CF_PROJECT matched nothing and turned
  every same-repo PR red; resolve the main-module guard through realpathSync.
- correct three comments: pick() cites source 4 not 3, and the threat is a bot
  of some installed App rather than "anyone who can comment" (the Bot gate
  already blocks that); CF_PROJECT is a host match, not a substring.

Records the one finding NOT fixed here: the URL harvest is unscoped by SHA and
pick() accepts the branch alias, so the lane can verify the previous commit.
Pre-existing, #122's territory, an explicit non-goal of this ticket, and
tightening it blind would break discovery. Written down in tests.md.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Aug 8, 2026 •

Copy link
Copy Markdown

This PR's own CI disproved its headline finding. The discovery step logged
`check run "Cloudflare Pages: sahajatlas" (in_progress)` on seven consecutive
polls before the URL appeared — so Cloudflare DOES publish a building state,
and the claim that it does not was wrong in the script header, in tests.md and
in the PR body.

The error was methodological, and worth recording because it is repeatable:
Cloudflare sets `started_at` when it COMPLETES a check run, so every finished
run reads `started_at == completed_at`. Sampling historical commits — what
#124's evidence and my own 230-run sweep both did — cannot see the in-progress
window and concludes it never existed. Only watching a live build shows it.

No behaviour changes. The flat deadline stands on the duration data, which is
unaffected, and now costs less than it seemed to: adaptive extension is
genuinely available if the queue ever outgrows the cap. The correction also
makes the `failed` guard's `status === 'completed'` test load-bearing rather
than incidental — an in-progress run has a null conclusion.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@Ardnived
Ardnived merged commit 88e2ceb into main Aug 8, 2026
5 checks passed
@Ardnived
Ardnived deleted the chore/preview-discovery-timeout branch August 8, 2026 03:25
Ardnived added a commit that referenced this pull request Aug 8, 2026
One conflict, in .claude/rules/tests.md: this branch and #101 each added a
fourth jsdom spec to the same list, and both bumped its count to "Three".
Kept both entries, corrected the count to Four, and fixed #101's "least
optional of the three" to match. The ordinal in the report-form entry
still reads correctly — with sanitize.test.ts second, it is the third
cautionary tale.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

chore(build): make preview discovery survive a slow cloudflare deploy

1 participant