Skip to content

Commit 02ff06c

Browse files
wpfleger96npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7
andauthored
fix(relay-reconnect): resilient reconnect with fast-path, escalation, and polling (#1456)
Signed-off-by: Will Pfleger <[email protected]> Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@sprout-oss.stage.blox.sqprod.co>
1 parent 5bfd5ca commit 02ff06c

16 files changed

Lines changed: 1455 additions & 104 deletions

‎desktop/src-tauri/src/commands/relay_reconnect.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@
1010
// `include!`. See reconnect_hook_config.rs for why this is shared, not a module.
1111
include!("reconnect_hook_config.rs");
1212

13+
/// Returns `true` when an internal build has a reconnect hook configured.
14+
///
15+
/// Used by the frontend to decide whether to show "Waiting to reconnect…"
16+
/// copy and to skip escalation in OSS builds where the hook is a no-op.
17+
#[tauri::command]
18+
pub fn relay_reconnect_hook_configured() -> bool {
19+
option_env!("BUZZ_DESKTOP_BUILD_RELAY_RECONNECT_CMD").is_some()
20+
}
21+
1322
#[tauri::command]
1423
pub async fn relay_reconnect_hook() -> Result<(), String> {
1524
let Some(config_str) = option_env!("BUZZ_DESKTOP_BUILD_RELAY_RECONNECT_CMD") else {

‎desktop/src-tauri/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@ pub fn run() {
611611
set_prevent_sleep_active,
612612
get_agent_memory,
613613
relay_reconnect_hook,
614+
relay_reconnect_hook_configured,
614615
])
615616
.build(tauri::generate_context!())
616617
.expect("error while building tauri application");

‎desktop/src/features/onboarding/ui/ProfileStep.tsx‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as React from "react";
22
import { toast } from "sonner";
33

44
import { SidebarRelayConnectionCompactCard } from "@/features/sidebar/ui/SidebarRelayConnectionCard";
5+
import { useRelayConnection } from "@/shared/api/useRelayConnection";
56
import { useReconnectRelay } from "@/shared/api/useReconnectRelay";
67
import { cn } from "@/shared/lib/cn";
78
import { isRelayUnreachableError } from "@/shared/lib/relayError";
@@ -30,7 +31,15 @@ function OnboardingRelayConnectionErrorCard({
3031
isSaving: boolean;
3132
message: string;
3233
}) {
33-
const { isPending: isReconnectPending, reconnect } = useReconnectRelay();
34+
const {
35+
isPending: isReconnectPending,
36+
isWaitingOnReconnectHook,
37+
reconnect,
38+
} = useReconnectRelay();
39+
// Track whether a reconnect attempt was ever initiated from this card so we
40+
// don't call markSuccess() on a "connected" state that pre-dates any click.
41+
const hadActiveReconnectRef = React.useRef(false);
42+
const relayConnectionState = useRelayConnection();
3443
const [dismissedErrorMessage, setDismissedErrorMessage] = React.useState<
3544
string | null
3645
>(null);
@@ -73,23 +82,43 @@ function OnboardingRelayConnectionErrorCard({
7382
}, ONBOARDING_CONNECTIVITY_SUCCESS_AUTO_DISMISS_MS);
7483
}, [message]);
7584

85+
// Observe real relay connection state. When this card initiated a reconnect
86+
// attempt (phase-3 path: reconnect() returns false) and the relay later
87+
// becomes connected, mark success. Guard: only fire when we actually
88+
// started a reconnect so a pre-existing connected state doesn't trigger it.
89+
React.useEffect(() => {
90+
if (relayConnectionState === "connected" && hadActiveReconnectRef.current) {
91+
hadActiveReconnectRef.current = false;
92+
markSuccess();
93+
}
94+
}, [relayConnectionState, markSuccess]);
95+
7696
const runConnectivityAction = React.useCallback(
7797
(runAction: () => Promise<boolean | undefined>) => {
7898
if (reconnectActionPendingRef.current) {
7999
return;
80100
}
81101

102+
hadActiveReconnectRef.current = true;
82103
reconnectActionPendingRef.current = true;
83104
setIsReconnectActionPending(true);
84105
setHasSuccess(false);
85106
void Promise.resolve()
86107
.then(runAction)
87108
.then((didReconnect) => {
88109
if (didReconnect !== false) {
110+
// Synchronous success (phase 1) — clear the ref and mark success
111+
// immediately. The connection-state effect won't fire because the
112+
// ref was just cleared.
113+
hadActiveReconnectRef.current = false;
89114
markSuccess();
90115
}
116+
// didReconnect === false means phase 3 is active; the
117+
// connection-state effect will call markSuccess() when the relay
118+
// becomes connected.
91119
})
92120
.catch((error) => {
121+
hadActiveReconnectRef.current = false;
93122
const detail = error instanceof Error ? error.message : String(error);
94123
toast.error(`Could not reconnect to the relay. ${detail}`);
95124
})
@@ -116,6 +145,7 @@ function OnboardingRelayConnectionErrorCard({
116145
isActionDisabled={isActionPending}
117146
isConnected={hasSuccess}
118147
isReconnectPending={isActionPending}
148+
isWaitingOnReconnectHook={isWaitingOnReconnectHook}
119149
onDismiss={() => setDismissedErrorMessage(message)}
120150
onReconnect={handleReconnectRelay}
121151
surface="secondary"

‎desktop/src/features/sidebar/ui/AppSidebar.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,9 @@ export function AppSidebar({
772772
isReconnectPending={
773773
sidebarRelayConnectionCard.isRelayReconnectPending
774774
}
775+
isWaitingOnReconnectHook={
776+
sidebarRelayConnectionCard.isWaitingOnReconnectHook
777+
}
775778
onDismiss={
776779
sidebarRelayConnectionCard.onDismissRelayConnectionCard
777780
}

‎desktop/src/features/sidebar/ui/SidebarRelayConnectionCard.tsx‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type SidebarRelayConnectionCardProps = {
1212
className?: string;
1313
isConnected?: boolean;
1414
isReconnectPending: boolean;
15+
isWaitingOnReconnectHook?: boolean;
1516
onDismiss?: () => void;
1617
onReconnect: () => void;
1718
surface?: SidebarActionCardSurface;
@@ -24,6 +25,7 @@ export function SidebarRelayConnectionCard({
2425
isActionDisabled = false,
2526
isConnected = false,
2627
isReconnectPending,
28+
isWaitingOnReconnectHook = false,
2729
onDismiss,
2830
onReconnect,
2931
surface,
@@ -35,6 +37,7 @@ export function SidebarRelayConnectionCard({
3537
isActionDisabled={isActionDisabled}
3638
isConnected={isConnected}
3739
isReconnectPending={isReconnectPending}
40+
isWaitingOnReconnectHook={isWaitingOnReconnectHook}
3841
onDismiss={onDismiss}
3942
onReconnect={onReconnect}
4043
surface={surface}
@@ -49,11 +52,19 @@ export function SidebarRelayConnectionCompactCard({
4952
isActionDisabled = false,
5053
isConnected = false,
5154
isReconnectPending,
55+
isWaitingOnReconnectHook = false,
5256
onDismiss,
5357
onReconnect,
5458
surface,
5559
testId = "sidebar-relay-unreachable-compact",
5660
}: SidebarRelayConnectionCardProps) {
61+
const reconnectTitle = isWaitingOnReconnectHook
62+
? "Waiting to reconnect"
63+
: "Connecting";
64+
const reconnectDescription = isWaitingOnReconnectHook
65+
? "Complete any prompts opened by the reconnect helper to continue."
66+
: "Reconnecting";
67+
5768
return (
5869
<SidebarCompactActionCard
5970
actionAriaLabel={isConnected ? "Connected" : "Connect to relay"}
@@ -63,7 +74,7 @@ export function SidebarRelayConnectionCompactCard({
6374
isConnected
6475
? undefined
6576
: isReconnectPending
66-
? "Reconnecting"
77+
? reconnectDescription
6778
: "Click to connect"
6879
}
6980
dismissLabel="Dismiss relay notification"
@@ -89,7 +100,7 @@ export function SidebarRelayConnectionCompactCard({
89100
isConnected
90101
? "Connected"
91102
: isReconnectPending
92-
? "Connecting"
103+
? reconnectTitle
93104
: "Can't reach the relay"
94105
}
95106
tone={isConnected ? "success" : "neutral"}

‎desktop/src/features/sidebar/ui/useSidebarRelayConnectionCard.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,18 @@ export function useSidebarRelayConnectionCard(
8282
const [isWindowVisible, setIsWindowVisible] =
8383
React.useState(isDocumentVisible);
8484
const hasActiveRelayUnreachableError =
85-
hasRelayUnreachableError && !hasSuccess;
85+
hasRelayUnreachableError && !hasSuccess && !isRelayConnectionConnected;
8686
const isRelayConnectionActuallyDegraded =
8787
hasActiveRelayUnreachableError || isRelayConnectionStateDegraded;
8888
const isRelayConnectionSuccess = hasSuccess && isRelayConnectionConnected;
8989
const canShow = isRelayConnectionActuallyDegraded || isRelayConnectionSuccess;
9090
const show = canShow && !isDismissed;
9191
const wasProblemCardVisibleRef = React.useRef(false);
92-
const { isPending: isReconnectPending, reconnect } = useReconnectRelay();
92+
const {
93+
isPending: isReconnectPending,
94+
isWaitingOnReconnectHook,
95+
reconnect,
96+
} = useReconnectRelay();
9397
const [connectivityAction, setConnectivityAction] = React.useState<
9498
"relay-connection" | null
9599
>(null);
@@ -212,6 +216,7 @@ export function useSidebarRelayConnectionCard(
212216
hasRelayUnreachableError,
213217
isRelayConnectionSuccess,
214218
isRelayReconnectPending,
219+
isWaitingOnReconnectHook,
215220
onDismissRelayConnectionCard: () => {
216221
setRelayConnectivitySuccess(relayUrl, false);
217222
setIsDismissed(true);

0 commit comments

Comments
 (0)