Skip to content

refactor(shared,ui): extract the Protect check lifecycle into shared internal helpers - #9391

Open
mwickett wants to merge 2 commits into
mainfrom
mwickett/prot-855-extract-protect-check-runner
Open

refactor(shared,ui): extract the Protect check lifecycle into shared internal helpers#9391
mwickett wants to merge 2 commits into
mainfrom
mwickett/prot-855-extract-protect-check-runner

Conversation

@mwickett

@mwickett mwickett commented Aug 11, 2026

Copy link
Copy Markdown
Member

Description

Groundwork for Protect challenge support in custom-flow (headless) apps, where the challenge experience will be fully Clerk-owned: rendered automatically (modal by default, or into a clerk-protect-check placement marker), with no customer-facing challenge API — the same contract the bot-protection CAPTCHA already has.

This PR extracts the framework-free half of the challenge lifecycle out of @clerk/ui's useProtectCheckRunner into @clerk/shared/internal/clerk-js/protectCheckLifecycle, so the upcoming clerk-js managed-challenge host can drive the exact same lifecycle the prebuilt components use, with no duplication. No behavioral changes and no public API changes.

  • New internal module protectCheckLifecycle exposing:
    • executeProtectCheckWithTimeout — container ownership (clears stale widgets), the script timeout race, one-way caller-signal linking, and the zombie-script setWidgetVisible guard
    • submitProtectCheckProof — proof submission with protect_check_already_resolved treated as soft success (reload + continue)
    • isProtectCheckExpired, MAX_EXPIRED_RELOADS, PROTECT_CHECK_SCRIPT_TIMEOUT_MS
  • @clerk/ui's useProtectCheckRunner now drives its lifecycle through those helpers and keeps only the React orchestration (effect keying on the challenge token, mount/cancel races, flushSync visibility handshake, card error state, the no-RHC fail-closed guard — which must stay in the consumer layer since the shared module compiles with __BUILD_DISABLE_RHC__ hard-coded false).
  • The module is only loaded via the RHC-gated dynamic import(), so no-RHC bundles (chrome-extension, clerk.no-rhc.js) continue to dead-code-eliminate the loader entirely.
  • executeProtectCheck itself is untouched.

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

@changeset-bot

changeset-bot Bot commented Aug 11, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: c740264

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 23 packages
Name Type
@clerk/shared Patch
@clerk/ui Patch
@clerk/astro Patch
@clerk/backend Patch
@clerk/chrome-extension Patch
@clerk/clerk-js Patch
@clerk/electron Patch
@clerk/expo-passkeys Patch
@clerk/expo Patch
@clerk/express Patch
@clerk/fastify Patch
@clerk/headless Patch
@clerk/hono Patch
@clerk/localizations Patch
@clerk/msw Patch
@clerk/nextjs Patch
@clerk/nuxt Patch
@clerk/react-router Patch
@clerk/react Patch
@clerk/tanstack-react-start Patch
@clerk/testing Patch
@clerk/vue Patch
@clerk/swingset Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel

vercel Bot commented Aug 11, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
clerk-js-sandbox Ready Ready Preview Aug 13, 2026 11:06pm
swingset Ready Ready Preview Aug 13, 2026 11:06pm

Request Review

@pkg-pr-new

pkg-pr-new Bot commented Aug 11, 2026

Copy link
Copy Markdown

Open in StackBlitz

@clerk/astro

npm i https://pkg.pr.new/@clerk/astro@9391

@clerk/backend

npm i https://pkg.pr.new/@clerk/backend@9391

@clerk/chrome-extension

npm i https://pkg.pr.new/@clerk/chrome-extension@9391

@clerk/clerk-js

npm i https://pkg.pr.new/@clerk/clerk-js@9391

@clerk/electron

npm i https://pkg.pr.new/@clerk/electron@9391

@clerk/electron-passkeys

npm i https://pkg.pr.new/@clerk/electron-passkeys@9391

@clerk/eslint-plugin

npm i https://pkg.pr.new/@clerk/eslint-plugin@9391

@clerk/expo

npm i https://pkg.pr.new/@clerk/expo@9391

@clerk/expo-google-signin

npm i https://pkg.pr.new/@clerk/expo-google-signin@9391

@clerk/expo-passkeys

npm i https://pkg.pr.new/@clerk/expo-passkeys@9391

@clerk/express

npm i https://pkg.pr.new/@clerk/express@9391

@clerk/fastify

npm i https://pkg.pr.new/@clerk/fastify@9391

@clerk/hono

npm i https://pkg.pr.new/@clerk/hono@9391

@clerk/localizations

npm i https://pkg.pr.new/@clerk/localizations@9391

@clerk/nextjs

npm i https://pkg.pr.new/@clerk/nextjs@9391

@clerk/nuxt

npm i https://pkg.pr.new/@clerk/nuxt@9391

@clerk/react

npm i https://pkg.pr.new/@clerk/react@9391

@clerk/react-router

npm i https://pkg.pr.new/@clerk/react-router@9391

@clerk/shared

npm i https://pkg.pr.new/@clerk/shared@9391

@clerk/tanstack-react-start

npm i https://pkg.pr.new/@clerk/tanstack-react-start@9391

@clerk/testing

npm i https://pkg.pr.new/@clerk/testing@9391

@clerk/ui

npm i https://pkg.pr.new/@clerk/ui@9391

@clerk/upgrade

npm i https://pkg.pr.new/@clerk/upgrade@9391

@clerk/vue

npm i https://pkg.pr.new/@clerk/vue@9391

commit: c740264

@github-actions

github-actions Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

API Changes Report

Generated by Break Check on 2026-08-13T23:07:35.781Z

Summary

Metric Count
Packages analyzed 19
Packages with changes 1
🔴 Breaking changes 0
🟡 Non-breaking changes 0
🟢 Additions 1

@clerk/shared

Current version: 4.29.0
Recommended bump: MINOR → 4.30.0

Subpath ./internal/clerk-js/protectCheckLifecycle

🟢 Additions (1)

Added: ./internal/clerk-js/protectCheckLifecycle

New subpath export ./internal/clerk-js/protectCheckLifecycle (8 exported members)


Report generated by Break Check

Last ran on c740264.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f6193d98ba

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +1 to +2
// Moved to the public `@clerk/shared/protect-check` entry; this subpath is kept as an alias so
// existing imports keep resolving.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Remove the comment that restates the alias

This comment only narrates that the following re-export preserves the existing subpath, which is already evident from the code. Remove it rather than adding a multi-line restatement; repository guidance permits comments only for critical, non-obvious rationale and requires them to be terse.

AGENTS.md reference: AGENTS.md:L13-L13

Useful? React with 👍 / 👎.

@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: 969c4c1d-e13a-4b88-9c1d-d46fcf708967

📥 Commits

Reviewing files that changed from the base of the PR and between f6193d9 and b1e90f9.

📒 Files selected for processing (7)
  • .changeset/protect-check-lifecycle-extraction.md
  • .changeset/protect-check-ui-refactor.md
  • packages/shared/src/internal/clerk-js/__tests__/protectCheckLifecycle.test.ts
  • packages/shared/src/internal/clerk-js/protectCheckLifecycle.ts
  • packages/ui/src/components/SignIn/__tests__/SignInProtectCheck.test.tsx
  • packages/ui/src/components/SignUp/__tests__/SignUpProtectCheck.test.tsx
  • packages/ui/src/hooks/useProtectCheckRunner.ts
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • clerk/clerk_go (manual)
  • clerk/dashboard (manual)
  • clerk/accounts (manual)
  • clerk/backoffice (manual)
  • clerk/clerk (manual)
  • clerk/clerk-docs (manual)
  • clerk/cloudflare-workers (manual)
  • clerk/clerk-ios (auto-detected)
  • clerk/cli (auto-detected)
  • clerk/clerk-android (auto-detected)
🚧 Files skipped from review as they are similar to previous changes (4)
  • packages/ui/src/components/SignIn/tests/SignInProtectCheck.test.tsx
  • packages/ui/src/hooks/useProtectCheckRunner.ts
  • packages/ui/src/components/SignUp/tests/SignUpProtectCheck.test.tsx
  • .changeset/protect-check-ui-refactor.md

📝 Walkthrough

Walkthrough

The change extracts Protect Check timeout, cancellation, cleanup, and proof-submission recovery into shared lifecycle helpers. The UI runner now delegates execution and submission to these helpers. Sign-in and sign-up tests mock the timeout-based executor while preserving other lifecycle implementations. New shared tests cover execution and submission states. Two changesets document patch releases.

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

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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.
Title check ✅ Passed The title clearly and concisely describes extracting Protect check lifecycle logic into shared internal helpers.
Description check ✅ Passed The description directly explains the lifecycle extraction, shared helpers, preserved behavior, and intended custom-flow support.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
packages/ui/src/components/SignIn/__tests__/SignInProtectCheck.test.tsx (1)

22-22: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Preserve the shared helper signature in both mocks.

Both tests cast executeProtectCheckWithTimeout to ReturnType<typeof vi.fn>. This removes compile-time validation of mock parameters and return values. Use vi.mocked(executeProtectCheckWithTimeout) or an equivalent MockedFunction type in both files.

  • packages/ui/src/components/SignIn/__tests__/SignInProtectCheck.test.tsx#L22-L22: Replace the broad cast with a typed Vitest mock.
  • packages/ui/src/components/SignUp/__tests__/SignUpProtectCheck.test.tsx#L23-L23: Replace the broad cast with a typed Vitest mock.

As per coding guidelines, use precise TypeScript types and avoid broad mock types when a narrower type is available.

🤖 Prompt for 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.

In `@packages/ui/src/components/SignIn/__tests__/SignInProtectCheck.test.tsx` at
line 22, Replace the broad ReturnType<typeof vi.fn> casts for
executeProtectCheckWithTimeout with vi.mocked or an equivalent precise
MockedFunction type in both
packages/ui/src/components/SignIn/__tests__/SignInProtectCheck.test.tsx:22-22
and
packages/ui/src/components/SignUp/__tests__/SignUpProtectCheck.test.tsx:23-23,
preserving compile-time validation of the shared helper’s parameters and return
value.

Source: Coding guidelines

packages/shared/src/protect-check/executeProtectCheck.ts (1)

59-72: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use PROTECT_CHECK_ERROR_CODES for all thrown error codes in executeProtectCheck.ts. Replace the ten hardcoded literals at lines 60, 65, 70, 108, 121, 126, 131, 144, 148, and 155.

🤖 Prompt for 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.

In `@packages/shared/src/protect-check/executeProtectCheck.ts` around lines 59 -
72, Update all thrown error objects in executeProtectCheck to use the
corresponding constants from PROTECT_CHECK_ERROR_CODES instead of hardcoded
error-code string literals, including the validation and later failure paths
identified in the comment. Preserve each error’s existing semantic code while
centralizing the values through that shared constant.
🤖 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.

Nitpick comments:
In `@packages/shared/src/protect-check/executeProtectCheck.ts`:
- Around line 59-72: Update all thrown error objects in executeProtectCheck to
use the corresponding constants from PROTECT_CHECK_ERROR_CODES instead of
hardcoded error-code string literals, including the validation and later failure
paths identified in the comment. Preserve each error’s existing semantic code
while centralizing the values through that shared constant.

In `@packages/ui/src/components/SignIn/__tests__/SignInProtectCheck.test.tsx`:
- Line 22: Replace the broad ReturnType<typeof vi.fn> casts for
executeProtectCheckWithTimeout with vi.mocked or an equivalent precise
MockedFunction type in both
packages/ui/src/components/SignIn/__tests__/SignInProtectCheck.test.tsx:22-22
and
packages/ui/src/components/SignUp/__tests__/SignUpProtectCheck.test.tsx:23-23,
preserving compile-time validation of the shared helper’s parameters and return
value.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: 307fd263-40d0-4c7f-9b07-63201917dc3d

📥 Commits

Reviewing files that changed from the base of the PR and between b9d25d7 and f6193d9.

📒 Files selected for processing (14)
  • .changeset/protect-check-public-entry.md
  • .changeset/protect-check-ui-refactor.md
  • packages/shared/package.json
  • packages/shared/src/internal/clerk-js/protectCheck.ts
  • packages/shared/src/protect-check/__tests__/executeProtectCheck.test.ts
  • packages/shared/src/protect-check/__tests__/lifecycle.test.ts
  • packages/shared/src/protect-check/errors.ts
  • packages/shared/src/protect-check/executeProtectCheck.ts
  • packages/shared/src/protect-check/index.ts
  • packages/shared/src/protect-check/lifecycle.ts
  • packages/shared/tsdown.config.mts
  • packages/ui/src/components/SignIn/__tests__/SignInProtectCheck.test.tsx
  • packages/ui/src/components/SignUp/__tests__/SignUpProtectCheck.test.tsx
  • packages/ui/src/hooks/useProtectCheckRunner.ts
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • clerk/clerk_go (manual)
  • clerk/dashboard (manual)
  • clerk/accounts (manual)
  • clerk/backoffice (manual)
  • clerk/clerk (manual)
  • clerk/clerk-docs (manual)
  • clerk/cloudflare-workers (manual)
  • clerk/cli (auto-detected)
  • clerk/clerk-ios (auto-detected)
  • clerk/clerk-android (auto-detected)

@mwickett
mwickett force-pushed the mwickett/prot-855-extract-protect-check-runner branch from f6193d9 to b1e90f9 Compare August 12, 2026 18:00
@mwickett mwickett changed the title feat(shared,ui): promote the Protect check runner to a public @clerk/shared/protect-check entry refactor(shared,ui): extract the Protect check lifecycle into shared internal helpers Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants