Skip to content

Fix flaky Cloudflare mock startup - #768

Merged
kody-bot merged 3 commits into
mainfrom
cursor/fix-flaky-cloudflare-mock-test-9902
Jul 16, 2026
Merged

kody-bot merged 3 commits into
mainfrom
cursor/fix-flaky-cloudflare-mock-test-9902

Conversation

@kentcdodds

@kentcdodds kentcdodds commented Jul 16, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • let Wrangler atomically allocate both the mock server and inspector ports instead of probing and releasing ports before startup
  • discover Wrangler's assigned public origin from ANSI-normalized readiness output, then verify the mock metadata endpoint with bounded probes
  • fail immediately with captured Wrangler diagnostics when the child exits, and allow 60 seconds for startup under loaded CI runners
  • align both integration-test timeouts with the helper's startup budget

Root cause

get-port only proved a port was free and then released it. During npm run validate, unit, Playwright, and MCP suites run concurrently and can start competing Wrangler processes, so another process could bind either selected port before this helper's Wrangler did. The helper then discarded Wrangler's output and polled the dead origin for 25 seconds, producing only mock cloudflare timeout.

Wrangler now binds port 0 atomically and reports the assigned origin. Its CI output includes ANSI control sequences inside the URL, so the helper strips those sequences before parsing the readiness line. Each metadata probe is bounded to one second (or the remaining startup budget) so a half-open response cannot bypass the overall deadline.

Test evidence

  • targeted Cloudflare mock tests: 2/2 passed
  • ANSI/CI reproduction (FORCE_COLOR=3): 2/2 passed after reproducing the pre-fix timeout
  • cross-process ANSI startup stress: 12/12 artifacts workflow runs passed with 6 concurrent Vitest processes
  • npm run validate: passed (263 unit files / 864 unit tests, 18 Playwright tests, MCP suite, typecheck, lint, and formatting)
  • PR checks: Validate, preview resources, Cursor Bugbot, and CodeRabbit passed
System recap — composes existing primitives (low risk)

Mode: recap · Base: main @ 75cd1b6c · Head: 2eda3994

Classification: composes — this PR changes only the local test harness and test time budgets; no production primitive behavior or contract changes.

Primitives touched

Primitive Group Impact
artifacts-repos storage composes — stabilizes the local mock used to verify the existing Artifacts REST workflow

System map

The focused integration test continues to exercise the existing Artifacts repository contract through a more reliable Wrangler mock startup path.

Legend: green = composes (wiring only) · amber = extended by this PR · red = new primitive · gray = context (unchanged, included only when an edge crosses it).

flowchart LR
	artifactsRepos["artifacts-repos<br/>Artifacts repos"]:::touched
	classDef touched fill:#1a7f37,color:#fff
	classDef extended fill:#9a6700,color:#fff
	classDef added fill:#cf222e,color:#fff
	classDef untouched fill:#57606a,color:#fff
Loading
Open in Web Open in Cursor 

Summary by CodeRabbit

  • Bug Fixes
    • Improved Cloudflare mock startup detection with more reliable readiness checks and clearer errors for early process exit or startup timeout.
    • Updated the local Cloudflare dev server launch behavior to use dynamic ports and enhanced logging for better stability.
    • Increased Vitest timeouts for email-sending and Cloudflare artifacts workflow tests to reduce intermittent failures in slower environments.

Co-authored-by: Kent C. Dodds <me+github@kentcdodds.com>
@coderabbitai

coderabbitai Bot commented Jul 16, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Cloudflare mock startup now derives its origin from Wrangler output, validates readiness, detects early process exits, and uses dynamic localhost ports. The email and Artifacts mock tests now have explicit 75-second timeouts.

Changes

Cloudflare mock startup

Layer / File(s) Summary
Process-aware readiness probing
packages/worker/src/test-support/cloudflare-mock-server.ts
The mock server parses Wrangler output, checks /__mocks/meta, races readiness against process exit, reports specific startup errors, and launches with dynamic ports bound to 127.0.0.1.

Extended mock-dependent test timeouts

Layer / File(s) Summary
Extended mock-dependent test timeouts
packages/worker/src/app/email/cloudflare-email.node.test.ts, packages/worker/src/repo/artifacts-mock-cloudflare.node.test.ts
Both tests use explicit 75-second Vitest timeouts.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant startCloudflareMock
  participant wrangler_dev
  participant waitForCloudflareMock
  startCloudflareMock->>wrangler_dev: spawn with dynamic localhost ports
  wrangler_dev-->>waitForCloudflareMock: emit ready origin
  waitForCloudflareMock->>wrangler_dev: request /__mocks/meta
  wrangler_dev-->>waitForCloudflareMock: readiness response or process exit
  waitForCloudflareMock-->>startCloudflareMock: return origin or startup error
Loading

Possibly related PRs

  • kentcdodds/kody#209: Introduces the Artifacts REST mock flow whose startup support and timeout are updated here.
  • kentcdodds/kody#721: Also modifies the sendCloudflareEmail test in the same test file.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: stabilizing Cloudflare mock startup.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch cursor/fix-flaky-cloudflare-mock-test-9902

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@kody-bot
kody-bot marked this pull request as ready for review July 16, 2026 20:29
@github-actions

github-actions Bot commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

🔎 Preview deployed: https://kody-pr-768.kody-a99.workers.dev

Worker: kody-pr-768
D1: kody-pr-768-db
KV: kody-pr-768-oauth-kv

Mocks:

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/worker/src/test-support/cloudflare-mock-server.ts`:
- Around line 28-39: Update the metadata probe loop around fetch in the mock
server startup flow to use a per-attempt AbortController signal bounded by the
remaining 60-second deadline, so a nonresponsive socket cannot stall startup;
pass the signal to fetch, and ensure response.body is canceled in a finally
block for every completed attempt while preserving retries for failures and
non-OK responses.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: be835460-d2fe-4adf-a356-7d2700b1ffff

📥 Commits

Reviewing files that changed from the base of the PR and between 75cd1b6 and cda102d.

📒 Files selected for processing (3)
  • packages/worker/src/app/email/cloudflare-email.node.test.ts
  • packages/worker/src/repo/artifacts-mock-cloudflare.node.test.ts
  • packages/worker/src/test-support/cloudflare-mock-server.ts

Comment thread packages/worker/src/test-support/cloudflare-mock-server.ts
cursoragent and others added 2 commits July 16, 2026 20:48
Co-authored-by: Kent C. Dodds <me+github@kentcdodds.com>
Co-authored-by: Kent C. Dodds <me+github@kentcdodds.com>
@kody-bot
kody-bot merged commit 7193029 into main Jul 16, 2026
5 checks passed
@kody-bot
kody-bot deleted the cursor/fix-flaky-cloudflare-mock-test-9902 branch July 16, 2026 21:03
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.

3 participants