Repository navigation
fix(web): report browser profiles only to connected environments #18148
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,23 @@ | ||
| import { AuthPreviewOperateScope, type BrowserProfile } from "@t3tools/contracts"; | ||
| import { useEffect } from "react"; | ||
| import { | ||
| AuthPreviewOperateScope, | ||
| type BrowserProfile, | ||
| type EnvironmentId, | ||
| type ServerConfig, | ||
| } from "@t3tools/contracts"; | ||
| import { isAtomCommandInterrupted } from "@t3tools/client-runtime/state/runtime"; | ||
| import { useEffect, useRef } from "react"; | ||
|
|
||
| import { useClientSettings, useClientSettingsHydrated } from "~/hooks/useSettings"; | ||
| import { useServerConfigs } from "~/state/entities"; | ||
| import { useEnvironments } from "~/state/environments"; | ||
| import { useConnectedEnvironmentIds, useEnvironments } from "~/state/environments"; | ||
| import { previewEnvironment } from "~/state/preview"; | ||
| import { useEnvironmentsWithScope } from "~/state/session"; | ||
| import { useAtomCommand } from "~/state/use-atom-command"; | ||
|
|
||
| /** A failed report retries this many times, waiting longer each time. */ | ||
| const MAX_REPORT_RETRIES = 3; | ||
| const REPORT_RETRY_DELAY_MS = 5_000; | ||
|
|
||
| const selectProfiles = (settings: { readonly browserProfiles: ReadonlyArray<BrowserProfile> }) => | ||
| settings.browserProfiles; | ||
| const selectDefaultProfileId = (settings: { readonly browserDefaultProfileId: string }) => | ||
|
|
@@ -17,7 +27,8 @@ const selectDefaultProfileId = (settings: { readonly browserDefaultProfileId: st | |
| * Tells each environment that hosts browser tabs which profiles this client | ||
| * has, so agents can open tabs under them. Profiles live in client settings; | ||
| * the server keeps only the latest report in memory. A reconnect brings a new | ||
| * server config, which sends the report again. | ||
| * server config, which sends the report again. Disconnected environments and | ||
| * unchanged reports are skipped: session and config churn re-runs the effect. | ||
| */ | ||
| export function BrowserProfileReporter() { | ||
| const hydrated = useClientSettingsHydrated(); | ||
|
|
@@ -26,15 +37,77 @@ export function BrowserProfileReporter() { | |
| const { environments } = useEnvironments(); | ||
| const serverConfigs = useServerConfigs(); | ||
| const operable = useEnvironmentsWithScope(environments, AuthPreviewOperateScope); | ||
| const connected = useConnectedEnvironmentIds(); | ||
| const report = useAtomCommand(previewEnvironment.reportProfiles, { reportFailure: false }); | ||
| const reported = useRef( | ||
| new Map< | ||
| EnvironmentId, | ||
| { | ||
| readonly config: ServerConfig; | ||
| readonly profiles: ReadonlyArray<BrowserProfile>; | ||
| readonly defaultProfileId: string; | ||
| } | ||
| >(), | ||
| ); | ||
| /** Consecutive failed reports per connected environment. */ | ||
| const failures = useRef(new Map<EnvironmentId, number>()); | ||
| const retryTimer = useRef<ReturnType<typeof setTimeout> | null>(null); | ||
| /** The latest report pass, for a retry to run with current settings. */ | ||
| const sweep = useRef<() => void>(() => {}); | ||
|
|
||
| useEffect( | ||
| () => () => { | ||
| if (retryTimer.current !== null) clearTimeout(retryTimer.current); | ||
| }, | ||
| [], | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| if (!hydrated) return; | ||
| for (const [environmentId, config] of serverConfigs) { | ||
| if (!config.environment.capabilities.serverBrowser || !operable.has(environmentId)) continue; | ||
| void report({ environmentId, input: { profiles, defaultProfileId } }); | ||
| } | ||
| }, [defaultProfileId, hydrated, operable, profiles, report, serverConfigs]); | ||
| const reportChanged = () => { | ||
| for (const environmentId of reported.current.keys()) { | ||
| if (!connected.includes(environmentId)) reported.current.delete(environmentId); | ||
| } | ||
| for (const environmentId of failures.current.keys()) { | ||
| if (!connected.includes(environmentId)) failures.current.delete(environmentId); | ||
| } | ||
| for (const environmentId of connected) { | ||
| const config = serverConfigs.get(environmentId); | ||
| if (!config?.environment.capabilities.serverBrowser || !operable.has(environmentId)) | ||
| continue; | ||
| const last = reported.current.get(environmentId); | ||
| if ( | ||
| last?.config === config && | ||
| last.profiles === profiles && | ||
| last.defaultProfileId === defaultProfileId | ||
| ) { | ||
| continue; | ||
| } | ||
| const entry = { config, profiles, defaultProfileId }; | ||
| reported.current.set(environmentId, entry); | ||
| void report({ environmentId, input: { profiles, defaultProfileId } }).then((result) => { | ||
| if (result._tag === "Success") { | ||
| failures.current.delete(environmentId); | ||
| return; | ||
| } | ||
| // A newer report replaced this one, which needs no retry. | ||
| if (isAtomCommandInterrupted(result) || reported.current.get(environmentId) !== entry) { | ||
| return; | ||
| } | ||
| reported.current.delete(environmentId); | ||
| const attempt = (failures.current.get(environmentId) ?? 0) + 1; | ||
| failures.current.set(environmentId, attempt); | ||
| if (attempt > MAX_REPORT_RETRIES || retryTimer.current !== null) return; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium An environment that has reached 🤖 Copy this AI Prompt to have your agent fix this:
Comment on lines
+98
to
+100
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win Reset the retry count for a changed report tuple. After one environment exhausts its retries, 🤖 Prompt for AI Agents |
||
| retryTimer.current = setTimeout(() => { | ||
| retryTimer.current = null; | ||
| sweep.current(); | ||
| }, REPORT_RETRY_DELAY_MS * attempt); | ||
|
Comment on lines
+101
to
+104
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win Prevent a retry timer from starting after unmount. Unmount cleanup clears a timer that already exists. If an in-flight report fails after that cleanup, this callback creates a new timer. The timer can then run the retained sweep after unmount. Guard the completion callback with a mounted or generation check. React runs the effect cleanup when it removes the component. (react.dev) 🤖 Prompt for AI Agents |
||
| }); | ||
| } | ||
| }; | ||
| sweep.current = reportChanged; | ||
| reportChanged(); | ||
| }, [connected, defaultProfileId, hydrated, operable, profiles, report, serverConfigs]); | ||
|
|
||
| return null; | ||
| } | ||
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.
🟡 Medium
browser/BrowserProfileReporter.tsx:59A report that fails after
BrowserProfileReporterunmounts schedules a retry that callssweep.current()and sends reports using stale connection and settings values. The cleanup clears only a timer that already exists; invalidatesweep.currentduring cleanup so a late completion cannot trigger another report.🤖 Copy this AI Prompt to have your agent fix this: