From bc4027a7be4df0d649e0e407d3bd3bab0ca93455 Mon Sep 17 00:00:00 2001 From: TDanks2000 <40835340+TDanks2000@users.noreply.github.com> Date: Sat, 10 Oct 2026 18:56:40 +0100 Subject: [PATCH 1/2] fix(web): report browser profiles only to connected environments, once per change The reporter re-sent profiles to every environment with a cached config whenever any session or config changed, including disconnected ones: 500+ failed requests in one user's traces. It now reports to connected environments when the report or config changed, and retries a failed report. Co-Authored-By: Claude Opus 5.5 --- .../src/browser/BrowserProfileReporter.tsx | 52 ++++++++++++++++--- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/apps/web/src/browser/BrowserProfileReporter.tsx b/apps/web/src/browser/BrowserProfileReporter.tsx index e148f79d72afb..d69c7d5e59f7c 100644 --- a/apps/web/src/browser/BrowserProfileReporter.tsx +++ b/apps/web/src/browser/BrowserProfileReporter.tsx @@ -1,9 +1,14 @@ -import { AuthPreviewOperateScope, type BrowserProfile } from "@t3tools/contracts"; -import { useEffect } from "react"; +import { + AuthPreviewOperateScope, + type BrowserProfile, + type EnvironmentId, + type ServerConfig, +} from "@t3tools/contracts"; +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"; @@ -17,7 +22,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 +32,45 @@ 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; + readonly defaultProfileId: string; + } + >(), + ); 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 } }); + for (const environmentId of reported.current.keys()) { + if (!connected.includes(environmentId)) reported.current.delete(environmentId); } - }, [defaultProfileId, hydrated, operable, profiles, report, serverConfigs]); + 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) => { + // Unsent: the next run of this effect tries again. + if (result._tag === "Failure" && reported.current.get(environmentId) === entry) { + reported.current.delete(environmentId); + } + }); + } + }, [connected, defaultProfileId, hydrated, operable, profiles, report, serverConfigs]); return null; } From 81288f45ae6acc37ba37bba708a8e54f58e94145 Mon Sep 17 00:00:00 2001 From: TDanks2000 <40835340+TDanks2000@users.noreply.github.com> Date: Sat, 10 Oct 2026 19:25:54 +0100 Subject: [PATCH 2/2] fix(web): retry a failed browser profile report Forgetting a failed report did not re-run the effect, so it stayed unsent until unrelated state changed. A failure now schedules up to three retries with growing delays, cleared on unmount and reset on disconnect. Co-Authored-By: Claude Opus 5.5 --- .../src/browser/BrowserProfileReporter.tsx | 79 ++++++++++++++----- 1 file changed, 58 insertions(+), 21 deletions(-) diff --git a/apps/web/src/browser/BrowserProfileReporter.tsx b/apps/web/src/browser/BrowserProfileReporter.tsx index d69c7d5e59f7c..31e30932321ec 100644 --- a/apps/web/src/browser/BrowserProfileReporter.tsx +++ b/apps/web/src/browser/BrowserProfileReporter.tsx @@ -4,6 +4,7 @@ import { 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"; @@ -13,6 +14,10 @@ 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 }) => settings.browserProfiles; const selectDefaultProfileId = (settings: { readonly browserDefaultProfileId: string }) => @@ -44,32 +49,64 @@ export function BrowserProfileReporter() { } >(), ); + /** Consecutive failed reports per connected environment. */ + const failures = useRef(new Map()); + const retryTimer = useRef | 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 of reported.current.keys()) { - if (!connected.includes(environmentId)) reported.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 reportChanged = () => { + for (const environmentId of reported.current.keys()) { + if (!connected.includes(environmentId)) reported.current.delete(environmentId); } - const entry = { config, profiles, defaultProfileId }; - reported.current.set(environmentId, entry); - void report({ environmentId, input: { profiles, defaultProfileId } }).then((result) => { - // Unsent: the next run of this effect tries again. - if (result._tag === "Failure" && reported.current.get(environmentId) === entry) { - 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; + retryTimer.current = setTimeout(() => { + retryTimer.current = null; + sweep.current(); + }, REPORT_RETRY_DELAY_MS * attempt); + }); + } + }; + sweep.current = reportChanged; + reportChanged(); }, [connected, defaultProfileId, hydrated, operable, profiles, report, serverConfigs]); return null;