-
Notifications
You must be signed in to change notification settings - Fork 532
Replace sleep(3000) with exponential backoff polling for trial activation #3705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devin-ai-integration
wants to merge
3
commits into
main
Choose a base branch
from
devin/1770469282-exponential-backoff-trial-polling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+198
−45
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
c99f132
Replace sleep(3000) with exponential backoff polling for trial activa…
devin-ai-integration[bot] bb5284d
Fix useEffect dependency bug: use refs for auth/store to prevent abor…
devin-ai-integration[bot] 02a16d0
Fire analytics on timeout too — trial is started on Stripe regardless…
devin-ai-integration[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import { useMutation } from "@tanstack/react-query"; | ||
| import { useCallback, useEffect, useRef } from "react"; | ||
|
|
||
| import { postBillingStartTrial } from "@hypr/api-client"; | ||
| import { createClient } from "@hypr/api-client/client"; | ||
| import { commands as analyticsCommands } from "@hypr/plugin-analytics"; | ||
|
|
||
| import { useAuth } from "../auth"; | ||
| import { env } from "../env"; | ||
| import { | ||
| pollForTrialActivation, | ||
| type PollResult, | ||
| } from "../utils/poll-trial-activation"; | ||
|
|
||
| type UseTrialActivationOptions = { | ||
| onActivated?: () => void; | ||
| onTimeout?: () => void; | ||
| onError?: (error: unknown) => void; | ||
| }; | ||
|
|
||
| export function useTrialActivation(options: UseTrialActivationOptions = {}) { | ||
| const auth = useAuth(); | ||
| const abortControllerRef = useRef<AbortController | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| return () => { | ||
| abortControllerRef.current?.abort(); | ||
| }; | ||
| }, []); | ||
|
|
||
| const mutation = useMutation({ | ||
| mutationFn: async (): Promise<PollResult> => { | ||
| const headers = auth?.getHeaders(); | ||
| if (!headers) { | ||
| throw new Error("Not authenticated"); | ||
| } | ||
|
|
||
| const client = createClient({ baseUrl: env.VITE_API_URL, headers }); | ||
| const { error } = await postBillingStartTrial({ | ||
| client, | ||
| query: { interval: "monthly" }, | ||
| }); | ||
| if (error) { | ||
| throw error; | ||
| } | ||
|
|
||
| abortControllerRef.current?.abort(); | ||
| const abortController = new AbortController(); | ||
| abortControllerRef.current = abortController; | ||
|
|
||
| return pollForTrialActivation({ | ||
| refreshSession: () => auth.refreshSession(), | ||
| signal: abortController.signal, | ||
| }); | ||
| }, | ||
| onSuccess: (result) => { | ||
| if (result.status === "activated" || result.status === "timeout") { | ||
| void analyticsCommands.event({ event: "trial_started", plan: "pro" }); | ||
| const trialEndDate = new Date(); | ||
| trialEndDate.setDate(trialEndDate.getDate() + 14); | ||
| void analyticsCommands.setProperties({ | ||
| email: auth?.session?.user.email, | ||
| user_id: auth?.session?.user.id, | ||
| set: { | ||
| plan: "pro", | ||
| trial_end_date: trialEndDate.toISOString(), | ||
| }, | ||
| }); | ||
| if (result.status === "activated") { | ||
| options.onActivated?.(); | ||
| } else { | ||
| options.onTimeout?.(); | ||
| } | ||
| } | ||
| }, | ||
| onError: (error) => { | ||
| options.onError?.(error); | ||
| }, | ||
| }); | ||
|
|
||
| const cancel = useCallback(() => { | ||
| abortControllerRef.current?.abort(); | ||
| abortControllerRef.current = null; | ||
| }, []); | ||
|
|
||
| return { | ||
| startTrial: mutation.mutate, | ||
| startTrialAsync: mutation.mutateAsync, | ||
| isPending: mutation.isPending, | ||
| isError: mutation.isError, | ||
| error: mutation.error, | ||
| cancel, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import type { Session } from "@supabase/supabase-js"; | ||
|
|
||
| import { commands as authCommands } from "@hypr/plugin-auth"; | ||
|
|
||
| const INITIAL_DELAY_MS = 1000; | ||
| const MAX_DELAY_MS = 5000; | ||
| const BACKOFF_FACTOR = 1.5; | ||
| const MAX_ATTEMPTS = 10; | ||
|
|
||
| export type PollResult = | ||
| | { status: "activated"; session: Session } | ||
| | { status: "timeout" } | ||
| | { status: "aborted" }; | ||
|
|
||
| type PollOptions = { | ||
| refreshSession: () => Promise<Session | null>; | ||
| signal?: AbortSignal; | ||
| }; | ||
|
|
||
| export async function pollForTrialActivation( | ||
| options: PollOptions, | ||
| ): Promise<PollResult> { | ||
| let delay = INITIAL_DELAY_MS; | ||
|
|
||
| for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) { | ||
| if (options.signal?.aborted) { | ||
| return { status: "aborted" }; | ||
| } | ||
|
|
||
| try { | ||
| await new Promise<void>((resolve, reject) => { | ||
| const timer = setTimeout(resolve, delay); | ||
| if (options.signal) { | ||
| const onAbort = () => { | ||
| clearTimeout(timer); | ||
| reject(new DOMException("Aborted", "AbortError")); | ||
| }; | ||
| options.signal.addEventListener("abort", onAbort, { once: true }); | ||
| } | ||
| }); | ||
| } catch (e) { | ||
| if (e instanceof DOMException && e.name === "AbortError") { | ||
| return { status: "aborted" }; | ||
| } | ||
| throw e; | ||
| } | ||
|
|
||
| if (options.signal?.aborted) { | ||
| return { status: "aborted" }; | ||
| } | ||
|
|
||
| try { | ||
| const session = await options.refreshSession(); | ||
| if (session) { | ||
| const result = await authCommands.decodeClaims(session.access_token); | ||
| if (result.status === "ok") { | ||
| const entitlements = result.data.entitlements ?? []; | ||
| if (entitlements.includes("hyprnote_pro")) { | ||
| return { status: "activated", session }; | ||
| } | ||
| } | ||
| } | ||
| } catch (error) { | ||
| console.warn( | ||
| `Trial activation poll attempt ${attempt + 1} failed:`, | ||
| error, | ||
| ); | ||
| } | ||
|
|
||
| delay = Math.min(delay * BACKOFF_FACTOR, MAX_DELAY_MS); | ||
| } | ||
|
|
||
| return { status: "timeout" }; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Onboarding gets stuck on loading screen in React StrictMode (dev mode)
In
final.tsx, the combination ofhasHandledRefguard and the abort cleanup causes the component to get permanently stuck in the loading state when React StrictMode is enabled.Root Cause: StrictMode double-effect execution + hasHandledRef + abort early return
In React 18 StrictMode (which is enabled at
apps/desktop/src/main.tsx:133), effects fire, cleanup, then fire again:hasHandledRef.currentisfalse→ set totrue,abortControllercreated,handle()starts asyncabortController.abort()fireshasHandledRef.currentistrue→ returns early, no newhandle()is startedThe
handle()from step 1 is still executing asynchronously. When it reachespollForTrialActivation, the signal is already aborted, so it returns{ status: "aborted" }. Then at line 66:This returns from
handle()without callingsetIsLoading(false)at line 75. Since the second effect run was blocked byhasHandledRef, no newhandle()ever runs. The component is permanently stuck showing the loading spinner.Impact: During development, the onboarding final step is completely broken — users see an infinite spinner. Production builds are unaffected since StrictMode is typically stripped.
(Refers to lines 38-41)
Prompt for agents
Was this helpful? React with 👍 or 👎 to provide feedback.