Add WebSocket disconnect recovery and slow RPC toast UX (#1730)
Co-authored-by: Hwanseo Choi <[email protected]> Co-authored-by: codex <[email protected]>
This commit is contained in:
20 files changed
+1837
-104
No files matched your search
@@ -197,6 +197,7 @@ import {
|
||||
useServerConfig,
|
||||
useServerKeybindings,
|
||||
} from "~/rpc/serverState";
|
||||
import { sanitizeThreadErrorMessage } from "~/rpc/transportError";
|
||||
|
||||
const ATTACHMENT_PREVIEW_HANDOFF_TTL_MS = 5000;
|
||||
const IMAGE_SIZE_LIMIT_LABEL = `${Math.round(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES / (1024 * 1024))}MB`;
|
||||
@@ -1599,17 +1600,18 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
const setThreadError = useCallback(
|
||||
(targetThreadId: ThreadId | null, error: string | null) => {
|
||||
if (!targetThreadId) return;
|
||||
const nextError = sanitizeThreadErrorMessage(error);
|
||||
if (useStore.getState().threads.some((thread) => thread.id === targetThreadId)) {
|
||||
setStoreThreadError(targetThreadId, error);
|
||||
setStoreThreadError(targetThreadId, nextError);
|
||||
return;
|
||||
}
|
||||
setLocalDraftErrorsByThreadId((existing) => {
|
||||
if ((existing[targetThreadId] ?? null) === error) {
|
||||
if ((existing[targetThreadId] ?? null) === nextError) {
|
||||
return existing;
|
||||
}
|
||||
return {
|
||||
...existing,
|
||||
[targetThreadId]: error,
|
||||
[targetThreadId]: nextError,
|
||||
};
|
||||
});
|
||||
},
|
||||
@@ -3153,14 +3155,14 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
createdAt: new Date().toISOString(),
|
||||
})
|
||||
.catch((err: unknown) => {
|
||||
setStoreThreadError(
|
||||
setThreadError(
|
||||
activeThreadId,
|
||||
err instanceof Error ? err.message : "Failed to submit approval decision.",
|
||||
);
|
||||
});
|
||||
setRespondingRequestIds((existing) => existing.filter((id) => id !== requestId));
|
||||
},
|
||||
[activeThreadId, setStoreThreadError],
|
||||
[activeThreadId, setThreadError],
|
||||
);
|
||||
|
||||
const onRespondToUserInput = useCallback(
|
||||
@@ -3181,14 +3183,14 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
createdAt: new Date().toISOString(),
|
||||
})
|
||||
.catch((err: unknown) => {
|
||||
setStoreThreadError(
|
||||
setThreadError(
|
||||
activeThreadId,
|
||||
err instanceof Error ? err.message : "Failed to submit user input.",
|
||||
);
|
||||
});
|
||||
setRespondingUserInputRequestIds((existing) => existing.filter((id) => id !== requestId));
|
||||
},
|
||||
[activeThreadId, setStoreThreadError],
|
||||
[activeThreadId, setThreadError],
|
||||
);
|
||||
|
||||
const setActivePendingUserInputQuestionIndex = useCallback(
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import type { WsConnectionStatus } from "../rpc/wsConnectionState";
|
||||
import { shouldAutoReconnect } from "./WebSocketConnectionSurface";
|
||||
|
||||
function makeStatus(overrides: Partial<WsConnectionStatus> = {}): WsConnectionStatus {
|
||||
return {
|
||||
attemptCount: 0,
|
||||
closeCode: null,
|
||||
closeReason: null,
|
||||
connectedAt: null,
|
||||
disconnectedAt: null,
|
||||
hasConnected: false,
|
||||
lastError: null,
|
||||
lastErrorAt: null,
|
||||
nextRetryAt: null,
|
||||
online: true,
|
||||
phase: "idle",
|
||||
reconnectAttemptCount: 0,
|
||||
reconnectMaxAttempts: 8,
|
||||
reconnectPhase: "idle",
|
||||
socketUrl: null,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe("WebSocketConnectionSurface.logic", () => {
|
||||
it("forces reconnect on online when the app was offline", () => {
|
||||
expect(
|
||||
shouldAutoReconnect(
|
||||
makeStatus({
|
||||
disconnectedAt: "2026-04-03T20:00:00.000Z",
|
||||
online: false,
|
||||
phase: "disconnected",
|
||||
}),
|
||||
"online",
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("forces reconnect on focus only for previously connected disconnected states", () => {
|
||||
expect(
|
||||
shouldAutoReconnect(
|
||||
makeStatus({
|
||||
hasConnected: true,
|
||||
online: true,
|
||||
phase: "disconnected",
|
||||
reconnectAttemptCount: 3,
|
||||
reconnectPhase: "waiting",
|
||||
}),
|
||||
"focus",
|
||||
),
|
||||
).toBe(true);
|
||||
|
||||
expect(
|
||||
shouldAutoReconnect(
|
||||
makeStatus({
|
||||
hasConnected: false,
|
||||
online: true,
|
||||
phase: "disconnected",
|
||||
reconnectAttemptCount: 1,
|
||||
reconnectPhase: "waiting",
|
||||
}),
|
||||
"focus",
|
||||
),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("forces reconnect on focus for exhausted reconnect loops", () => {
|
||||
expect(
|
||||
shouldAutoReconnect(
|
||||
makeStatus({
|
||||
hasConnected: true,
|
||||
online: true,
|
||||
phase: "disconnected",
|
||||
reconnectAttemptCount: 8,
|
||||
reconnectPhase: "exhausted",
|
||||
}),
|
||||
"focus",
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,544 @@
|
||||
import { AlertTriangle, CloudOff, LoaderCircle, RotateCw } from "lucide-react";
|
||||
import { type ReactNode, useEffect, useEffectEvent, useRef, useState } from "react";
|
||||
|
||||
import { APP_DISPLAY_NAME } from "../branding";
|
||||
import { type SlowRpcAckRequest, useSlowRpcAckRequests } from "../rpc/requestLatencyState";
|
||||
import { useServerConfig } from "../rpc/serverState";
|
||||
import {
|
||||
exhaustWsReconnectIfStillWaiting,
|
||||
getWsConnectionStatus,
|
||||
getWsConnectionUiState,
|
||||
setBrowserOnlineStatus,
|
||||
type WsConnectionStatus,
|
||||
type WsConnectionUiState,
|
||||
useWsConnectionStatus,
|
||||
WS_RECONNECT_MAX_ATTEMPTS,
|
||||
} from "../rpc/wsConnectionState";
|
||||
import { Button } from "./ui/button";
|
||||
import { toastManager } from "./ui/toast";
|
||||
import { getWsRpcClient } from "~/wsRpcClient";
|
||||
|
||||
const FORCED_WS_RECONNECT_DEBOUNCE_MS = 5_000;
|
||||
type WsAutoReconnectTrigger = "focus" | "online";
|
||||
|
||||
const connectionTimeFormatter = new Intl.DateTimeFormat(undefined, {
|
||||
day: "numeric",
|
||||
hour: "numeric",
|
||||
minute: "2-digit",
|
||||
month: "short",
|
||||
second: "2-digit",
|
||||
});
|
||||
|
||||
function formatConnectionMoment(isoDate: string | null): string | null {
|
||||
if (!isoDate) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return connectionTimeFormatter.format(new Date(isoDate));
|
||||
}
|
||||
|
||||
function formatRetryCountdown(nextRetryAt: string, nowMs: number): string {
|
||||
const remainingMs = Math.max(0, new Date(nextRetryAt).getTime() - nowMs);
|
||||
return `${Math.max(1, Math.ceil(remainingMs / 1000))}s`;
|
||||
}
|
||||
|
||||
function describeOfflineToast(): string {
|
||||
return "WebSocket disconnected. Waiting for network.";
|
||||
}
|
||||
|
||||
function formatReconnectAttemptLabel(status: WsConnectionStatus): string {
|
||||
const reconnectAttempt = Math.max(
|
||||
1,
|
||||
Math.min(status.reconnectAttemptCount, WS_RECONNECT_MAX_ATTEMPTS),
|
||||
);
|
||||
return `Attempt ${reconnectAttempt}/${status.reconnectMaxAttempts}`;
|
||||
}
|
||||
|
||||
function describeExhaustedToast(): string {
|
||||
return "Retries exhausted trying to reconnect";
|
||||
}
|
||||
|
||||
function buildReconnectTitle(status: WsConnectionStatus): string {
|
||||
if (status.nextRetryAt === null) {
|
||||
return "Disconnected from T3 Server";
|
||||
}
|
||||
|
||||
return "Disconnected from T3 Server";
|
||||
}
|
||||
|
||||
function describeRecoveredToast(
|
||||
previousDisconnectedAt: string | null,
|
||||
connectedAt: string | null,
|
||||
): string {
|
||||
const reconnectedAtLabel = formatConnectionMoment(connectedAt);
|
||||
const disconnectedAtLabel = formatConnectionMoment(previousDisconnectedAt);
|
||||
|
||||
if (disconnectedAtLabel && reconnectedAtLabel) {
|
||||
return `Disconnected at ${disconnectedAtLabel} and reconnected at ${reconnectedAtLabel}.`;
|
||||
}
|
||||
|
||||
if (reconnectedAtLabel) {
|
||||
return `Connection restored at ${reconnectedAtLabel}.`;
|
||||
}
|
||||
|
||||
return "Connection restored.";
|
||||
}
|
||||
|
||||
function describeSlowRpcAckToast(requests: ReadonlyArray<SlowRpcAckRequest>): ReactNode {
|
||||
const count = requests.length;
|
||||
const thresholdSeconds = Math.round((requests[0]?.thresholdMs ?? 0) / 1000);
|
||||
|
||||
return `${count} request${count === 1 ? "" : "s"} waiting longer than ${thresholdSeconds}s.`;
|
||||
}
|
||||
|
||||
export function shouldAutoReconnect(
|
||||
status: WsConnectionStatus,
|
||||
trigger: WsAutoReconnectTrigger,
|
||||
): boolean {
|
||||
const uiState = getWsConnectionUiState(status);
|
||||
|
||||
if (trigger === "online") {
|
||||
return (
|
||||
uiState === "offline" ||
|
||||
uiState === "reconnecting" ||
|
||||
uiState === "error" ||
|
||||
status.reconnectPhase === "exhausted"
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
status.online &&
|
||||
status.hasConnected &&
|
||||
(uiState === "reconnecting" || status.reconnectPhase === "exhausted")
|
||||
);
|
||||
}
|
||||
|
||||
function buildBlockingCopy(
|
||||
uiState: WsConnectionUiState,
|
||||
status: WsConnectionStatus,
|
||||
): {
|
||||
readonly description: string;
|
||||
readonly eyebrow: string;
|
||||
readonly title: string;
|
||||
} {
|
||||
if (uiState === "connecting") {
|
||||
return {
|
||||
description: `Opening the WebSocket connection to the ${APP_DISPLAY_NAME} server and waiting for the initial config snapshot.`,
|
||||
eyebrow: "Starting Session",
|
||||
title: `Connecting to ${APP_DISPLAY_NAME}`,
|
||||
};
|
||||
}
|
||||
|
||||
if (uiState === "offline") {
|
||||
return {
|
||||
description:
|
||||
"Your browser is offline, so the web client cannot reach the T3 server. Reconnect to the network and the app will retry automatically.",
|
||||
eyebrow: "Offline",
|
||||
title: "WebSocket connection unavailable",
|
||||
};
|
||||
}
|
||||
|
||||
if (status.lastError?.trim()) {
|
||||
return {
|
||||
description: `${status.lastError} Verify that the T3 server is running and reachable, then reload the app if needed.`,
|
||||
eyebrow: "Connection Error",
|
||||
title: "Cannot reach the T3 server",
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
description:
|
||||
"The web client could not complete its initial WebSocket connection to the T3 server. It will keep retrying in the background.",
|
||||
eyebrow: "Connection Error",
|
||||
title: "Cannot reach the T3 server",
|
||||
};
|
||||
}
|
||||
|
||||
function buildConnectionDetails(status: WsConnectionStatus, uiState: WsConnectionUiState): string {
|
||||
const details = [
|
||||
`state: ${uiState}`,
|
||||
`online: ${status.online ? "yes" : "no"}`,
|
||||
`attempts: ${status.attemptCount}`,
|
||||
];
|
||||
|
||||
if (status.socketUrl) {
|
||||
details.push(`socket: ${status.socketUrl}`);
|
||||
}
|
||||
if (status.connectedAt) {
|
||||
details.push(`connectedAt: ${status.connectedAt}`);
|
||||
}
|
||||
if (status.disconnectedAt) {
|
||||
details.push(`disconnectedAt: ${status.disconnectedAt}`);
|
||||
}
|
||||
if (status.lastErrorAt) {
|
||||
details.push(`lastErrorAt: ${status.lastErrorAt}`);
|
||||
}
|
||||
if (status.lastError) {
|
||||
details.push(`lastError: ${status.lastError}`);
|
||||
}
|
||||
if (status.closeCode !== null) {
|
||||
details.push(`closeCode: ${status.closeCode}`);
|
||||
}
|
||||
if (status.closeReason) {
|
||||
details.push(`closeReason: ${status.closeReason}`);
|
||||
}
|
||||
|
||||
return details.join("\n");
|
||||
}
|
||||
|
||||
function WebSocketBlockingState({
|
||||
status,
|
||||
uiState,
|
||||
}: {
|
||||
readonly status: WsConnectionStatus;
|
||||
readonly uiState: WsConnectionUiState;
|
||||
}) {
|
||||
const copy = buildBlockingCopy(uiState, status);
|
||||
const disconnectedAt = formatConnectionMoment(status.disconnectedAt ?? status.lastErrorAt);
|
||||
const Icon =
|
||||
uiState === "connecting" ? LoaderCircle : uiState === "offline" ? CloudOff : AlertTriangle;
|
||||
|
||||
return (
|
||||
<div className="relative flex min-h-screen items-center justify-center overflow-hidden bg-background px-4 py-10 text-foreground sm:px-6">
|
||||
<div className="pointer-events-none absolute inset-0 opacity-90">
|
||||
<div className="absolute inset-x-0 top-0 h-56 bg-[radial-gradient(48rem_18rem_at_top,color-mix(in_srgb,var(--color-amber-500)_16%,transparent),transparent)]" />
|
||||
<div className="absolute inset-0 bg-[linear-gradient(145deg,color-mix(in_srgb,var(--background)_92%,var(--color-black))_0%,var(--background)_56%)]" />
|
||||
</div>
|
||||
|
||||
<section className="relative w-full max-w-xl rounded-[1.75rem] border border-border/80 bg-card/92 p-6 shadow-2xl shadow-black/20 backdrop-blur-md sm:p-8">
|
||||
<div className="flex items-start justify-between gap-4">
|
||||
<div>
|
||||
<p className="text-[11px] font-semibold tracking-[0.18em] text-muted-foreground uppercase">
|
||||
{copy.eyebrow}
|
||||
</p>
|
||||
<h1 className="mt-3 text-2xl font-semibold tracking-tight sm:text-3xl">{copy.title}</h1>
|
||||
</div>
|
||||
<div className="rounded-2xl border border-border/70 bg-background/80 p-3 text-foreground shadow-sm">
|
||||
<Icon className={uiState === "connecting" ? "size-5 animate-spin" : "size-5"} />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p className="mt-3 text-sm leading-relaxed text-muted-foreground">{copy.description}</p>
|
||||
|
||||
<div className="mt-5 grid gap-3 rounded-2xl border border-border/70 bg-background/60 p-4 text-sm sm:grid-cols-2">
|
||||
<div>
|
||||
<p className="text-[11px] font-semibold tracking-[0.16em] text-muted-foreground uppercase">
|
||||
Connection
|
||||
</p>
|
||||
<p className="mt-1 font-medium text-foreground">
|
||||
{uiState === "connecting"
|
||||
? "Opening WebSocket"
|
||||
: uiState === "offline"
|
||||
? "Waiting for network"
|
||||
: "Retrying server connection"}
|
||||
</p>
|
||||
</div>
|
||||
<div>
|
||||
<p className="text-[11px] font-semibold tracking-[0.16em] text-muted-foreground uppercase">
|
||||
Latest Event
|
||||
</p>
|
||||
<p className="mt-1 font-medium text-foreground">{disconnectedAt ?? "Pending"}</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-5 flex flex-wrap gap-2">
|
||||
<Button size="sm" onClick={() => window.location.reload()}>
|
||||
<RotateCw />
|
||||
Reload app
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<details className="group mt-5 overflow-hidden rounded-lg border border-border/70 bg-background/55">
|
||||
<summary className="cursor-pointer list-none px-3 py-2 text-xs font-medium text-muted-foreground">
|
||||
<span className="group-open:hidden">Show connection details</span>
|
||||
<span className="hidden group-open:inline">Hide connection details</span>
|
||||
</summary>
|
||||
<pre className="max-h-56 overflow-auto border-t border-border/70 bg-background/80 px-3 py-2 text-xs text-foreground/85">
|
||||
{buildConnectionDetails(status, uiState)}
|
||||
</pre>
|
||||
</details>
|
||||
</section>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function WebSocketConnectionCoordinator() {
|
||||
const status = useWsConnectionStatus();
|
||||
const [nowMs, setNowMs] = useState(() => Date.now());
|
||||
const lastForcedReconnectAtRef = useRef(0);
|
||||
const toastIdRef = useRef<ReturnType<typeof toastManager.add> | null>(null);
|
||||
const toastResetTimerRef = useRef<number | null>(null);
|
||||
const previousUiStateRef = useRef<WsConnectionUiState>(getWsConnectionUiState(status));
|
||||
const previousDisconnectedAtRef = useRef<string | null>(status.disconnectedAt);
|
||||
|
||||
const runReconnect = useEffectEvent((showFailureToast: boolean) => {
|
||||
if (toastResetTimerRef.current !== null) {
|
||||
window.clearTimeout(toastResetTimerRef.current);
|
||||
toastResetTimerRef.current = null;
|
||||
}
|
||||
lastForcedReconnectAtRef.current = Date.now();
|
||||
void getWsRpcClient()
|
||||
.reconnect()
|
||||
.catch((error) => {
|
||||
if (!showFailureToast) {
|
||||
console.warn("Automatic WebSocket reconnect failed", { error });
|
||||
return;
|
||||
}
|
||||
toastManager.add({
|
||||
type: "error",
|
||||
title: "Reconnect failed",
|
||||
description: error instanceof Error ? error.message : "Unable to restart the WebSocket.",
|
||||
data: {
|
||||
dismissAfterVisibleMs: 8_000,
|
||||
hideCopyButton: true,
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
const syncBrowserOnlineStatus = useEffectEvent(() => {
|
||||
setBrowserOnlineStatus(navigator.onLine !== false);
|
||||
});
|
||||
const triggerManualReconnect = useEffectEvent(() => {
|
||||
runReconnect(true);
|
||||
});
|
||||
const triggerAutoReconnect = useEffectEvent((trigger: WsAutoReconnectTrigger) => {
|
||||
const currentStatus =
|
||||
trigger === "online" ? setBrowserOnlineStatus(true) : getWsConnectionStatus();
|
||||
|
||||
if (!shouldAutoReconnect(currentStatus, trigger)) {
|
||||
return;
|
||||
}
|
||||
if (Date.now() - lastForcedReconnectAtRef.current < FORCED_WS_RECONNECT_DEBOUNCE_MS) {
|
||||
return;
|
||||
}
|
||||
|
||||
runReconnect(false);
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
const handleOnline = () => {
|
||||
triggerAutoReconnect("online");
|
||||
};
|
||||
const handleFocus = () => {
|
||||
triggerAutoReconnect("focus");
|
||||
};
|
||||
|
||||
syncBrowserOnlineStatus();
|
||||
window.addEventListener("online", handleOnline);
|
||||
window.addEventListener("offline", syncBrowserOnlineStatus);
|
||||
window.addEventListener("focus", handleFocus);
|
||||
return () => {
|
||||
window.removeEventListener("online", handleOnline);
|
||||
window.removeEventListener("offline", syncBrowserOnlineStatus);
|
||||
window.removeEventListener("focus", handleFocus);
|
||||
};
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (status.reconnectPhase !== "waiting" || status.nextRetryAt === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
setNowMs(Date.now());
|
||||
const intervalId = window.setInterval(() => {
|
||||
setNowMs(Date.now());
|
||||
}, 1_000);
|
||||
|
||||
return () => {
|
||||
window.clearInterval(intervalId);
|
||||
};
|
||||
}, [status.nextRetryAt, status.reconnectPhase]);
|
||||
|
||||
useEffect(() => {
|
||||
if (
|
||||
status.reconnectPhase !== "waiting" ||
|
||||
status.nextRetryAt === null ||
|
||||
!status.online ||
|
||||
!status.hasConnected
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const nextRetryAt = status.nextRetryAt;
|
||||
const timeoutMs = Math.max(0, new Date(nextRetryAt).getTime() - Date.now()) + 1_500;
|
||||
const timeoutId = window.setTimeout(() => {
|
||||
exhaustWsReconnectIfStillWaiting(nextRetryAt);
|
||||
}, timeoutMs);
|
||||
|
||||
return () => {
|
||||
window.clearTimeout(timeoutId);
|
||||
};
|
||||
}, [
|
||||
status.hasConnected,
|
||||
status.nextRetryAt,
|
||||
status.online,
|
||||
status.reconnectAttemptCount,
|
||||
status.reconnectPhase,
|
||||
]);
|
||||
|
||||
useEffect(() => {
|
||||
const uiState = getWsConnectionUiState(status);
|
||||
const previousUiState = previousUiStateRef.current;
|
||||
const previousDisconnectedAt = previousDisconnectedAtRef.current;
|
||||
const shouldShowReconnectToast = status.hasConnected && uiState === "reconnecting";
|
||||
const shouldShowOfflineToast = uiState === "offline" && status.disconnectedAt !== null;
|
||||
const shouldShowExhaustedToast = status.hasConnected && status.reconnectPhase === "exhausted";
|
||||
|
||||
if (
|
||||
toastResetTimerRef.current !== null &&
|
||||
(shouldShowReconnectToast || shouldShowOfflineToast || shouldShowExhaustedToast)
|
||||
) {
|
||||
window.clearTimeout(toastResetTimerRef.current);
|
||||
toastResetTimerRef.current = null;
|
||||
}
|
||||
|
||||
if (shouldShowReconnectToast || shouldShowOfflineToast || shouldShowExhaustedToast) {
|
||||
const toastPayload = shouldShowOfflineToast
|
||||
? {
|
||||
description: describeOfflineToast(),
|
||||
timeout: 0,
|
||||
title: "Offline",
|
||||
type: "warning" as const,
|
||||
data: {
|
||||
hideCopyButton: true,
|
||||
},
|
||||
}
|
||||
: shouldShowExhaustedToast
|
||||
? {
|
||||
actionProps: {
|
||||
children: "Retry",
|
||||
onClick: triggerManualReconnect,
|
||||
},
|
||||
description: describeExhaustedToast(),
|
||||
timeout: 0,
|
||||
title: "Disconnected from T3 Server",
|
||||
type: "error" as const,
|
||||
data: {
|
||||
hideCopyButton: true,
|
||||
},
|
||||
}
|
||||
: {
|
||||
actionProps: {
|
||||
children: "Retry now",
|
||||
onClick: triggerManualReconnect,
|
||||
},
|
||||
description:
|
||||
status.nextRetryAt === null
|
||||
? `Reconnecting... ${formatReconnectAttemptLabel(status)}`
|
||||
: `Reconnecting in ${formatRetryCountdown(status.nextRetryAt, nowMs)}... ${formatReconnectAttemptLabel(status)}`,
|
||||
timeout: 0,
|
||||
title: buildReconnectTitle(status),
|
||||
type: "loading" as const,
|
||||
data: {
|
||||
hideCopyButton: true,
|
||||
},
|
||||
};
|
||||
|
||||
if (toastIdRef.current) {
|
||||
toastManager.update(toastIdRef.current, toastPayload);
|
||||
} else {
|
||||
toastIdRef.current = toastManager.add(toastPayload);
|
||||
}
|
||||
} else if (toastIdRef.current) {
|
||||
toastManager.close(toastIdRef.current);
|
||||
toastIdRef.current = null;
|
||||
}
|
||||
|
||||
if (
|
||||
uiState === "connected" &&
|
||||
(previousUiState === "offline" || previousUiState === "reconnecting") &&
|
||||
previousDisconnectedAt !== null
|
||||
) {
|
||||
const successToast = {
|
||||
description: describeRecoveredToast(previousDisconnectedAt, status.connectedAt),
|
||||
title: "Reconnected to T3 Server",
|
||||
type: "success" as const,
|
||||
timeout: 0,
|
||||
data: {
|
||||
dismissAfterVisibleMs: 8_000,
|
||||
hideCopyButton: true,
|
||||
},
|
||||
};
|
||||
|
||||
if (toastIdRef.current) {
|
||||
toastManager.update(toastIdRef.current, successToast);
|
||||
} else {
|
||||
toastIdRef.current = toastManager.add(successToast);
|
||||
}
|
||||
|
||||
toastResetTimerRef.current = window.setTimeout(() => {
|
||||
toastIdRef.current = null;
|
||||
toastResetTimerRef.current = null;
|
||||
}, 8_250);
|
||||
}
|
||||
|
||||
previousUiStateRef.current = uiState;
|
||||
previousDisconnectedAtRef.current = status.disconnectedAt;
|
||||
}, [nowMs, status]);
|
||||
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (toastResetTimerRef.current !== null) {
|
||||
window.clearTimeout(toastResetTimerRef.current);
|
||||
}
|
||||
};
|
||||
}, []);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export function SlowRpcAckToastCoordinator() {
|
||||
const slowRequests = useSlowRpcAckRequests();
|
||||
const status = useWsConnectionStatus();
|
||||
const toastIdRef = useRef<ReturnType<typeof toastManager.add> | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (getWsConnectionUiState(status) !== "connected") {
|
||||
if (toastIdRef.current) {
|
||||
toastManager.close(toastIdRef.current);
|
||||
toastIdRef.current = null;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (slowRequests.length === 0) {
|
||||
if (toastIdRef.current) {
|
||||
toastManager.close(toastIdRef.current);
|
||||
toastIdRef.current = null;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const nextToast = {
|
||||
description: describeSlowRpcAckToast(slowRequests),
|
||||
timeout: 0,
|
||||
title: "Some requests are slow",
|
||||
type: "warning" as const,
|
||||
};
|
||||
|
||||
if (toastIdRef.current) {
|
||||
toastManager.update(toastIdRef.current, nextToast);
|
||||
} else {
|
||||
toastIdRef.current = toastManager.add(nextToast);
|
||||
}
|
||||
}, [slowRequests, status]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export function WebSocketConnectionSurface({ children }: { readonly children: ReactNode }) {
|
||||
const serverConfig = useServerConfig();
|
||||
const status = useWsConnectionStatus();
|
||||
|
||||
if (serverConfig === null) {
|
||||
const uiState = getWsConnectionUiState(status);
|
||||
return (
|
||||
<WebSocketBlockingState
|
||||
status={status}
|
||||
uiState={uiState === "connected" ? "connecting" : uiState}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
return children;
|
||||
}
|
||||
@@ -23,6 +23,7 @@ export type ThreadToastData = {
|
||||
threadId?: ThreadId | null;
|
||||
tooltipStyle?: boolean;
|
||||
dismissAfterVisibleMs?: number;
|
||||
hideCopyButton?: boolean;
|
||||
};
|
||||
|
||||
const toastManager = Toast.createToastManager<ThreadToastData>();
|
||||
@@ -308,15 +309,15 @@ function Toasts({ position = "top-right" }: { position: ToastPosition }) {
|
||||
<div className="flex min-w-0 flex-1 flex-col gap-0.5">
|
||||
<div className="flex items-center justify-between gap-1">
|
||||
<Toast.Title
|
||||
className="min-w-0 break-words font-medium"
|
||||
className="min-w-0 wrap-break-word font-medium"
|
||||
data-slot="toast-title"
|
||||
/>
|
||||
{toast.type === "error" && typeof toast.description === "string" && (
|
||||
<CopyErrorButton text={toast.description} />
|
||||
)}
|
||||
{toast.type === "error" &&
|
||||
typeof toast.description === "string" &&
|
||||
!toast.data?.hideCopyButton && <CopyErrorButton text={toast.description} />}
|
||||
</div>
|
||||
<Toast.Description
|
||||
className="min-w-0 select-text break-words text-muted-foreground"
|
||||
className="min-w-0 select-text wrap-break-word text-muted-foreground"
|
||||
data-slot="toast-description"
|
||||
/>
|
||||
</div>
|
||||
@@ -402,15 +403,17 @@ function AnchoredToasts() {
|
||||
<div className="flex min-w-0 flex-1 flex-col gap-0.5">
|
||||
<div className="flex items-center gap-1">
|
||||
<Toast.Title
|
||||
className="min-w-0 break-words font-medium"
|
||||
className="min-w-0 wrap-break-word font-medium"
|
||||
data-slot="toast-title"
|
||||
/>
|
||||
{toast.type === "error" && typeof toast.description === "string" && (
|
||||
<CopyErrorButton text={toast.description} />
|
||||
)}
|
||||
{toast.type === "error" &&
|
||||
typeof toast.description === "string" &&
|
||||
!toast.data?.hideCopyButton && (
|
||||
<CopyErrorButton text={toast.description} />
|
||||
)}
|
||||
</div>
|
||||
<Toast.Description
|
||||
className="min-w-0 select-text break-words text-muted-foreground"
|
||||
className="min-w-0 select-text wrap-break-word text-muted-foreground"
|
||||
data-slot="toast-description"
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
export type OrchestrationRecoveryReason = "bootstrap" | "sequence-gap" | "replay-failed";
|
||||
export type OrchestrationRecoveryReason =
|
||||
| "bootstrap"
|
||||
| "sequence-gap"
|
||||
| "resubscribe"
|
||||
| "replay-failed";
|
||||
|
||||
export interface OrchestrationRecoveryPhase {
|
||||
kind: "snapshot" | "replay";
|
||||
|
||||
@@ -16,6 +16,11 @@ import { Throttler } from "@tanstack/react-pacer";
|
||||
|
||||
import { APP_DISPLAY_NAME } from "../branding";
|
||||
import { AppSidebarLayout } from "../components/AppSidebarLayout";
|
||||
import {
|
||||
SlowRpcAckToastCoordinator,
|
||||
WebSocketConnectionCoordinator,
|
||||
WebSocketConnectionSurface,
|
||||
} from "../components/WebSocketConnectionSurface";
|
||||
import { Button } from "../components/ui/button";
|
||||
import { AnchoredToastProvider, ToastProvider, toastManager } from "../components/ui/toast";
|
||||
import { resolveAndPersistPreferredEditor } from "../editorPreferences";
|
||||
@@ -73,9 +78,13 @@ function RootRouteView() {
|
||||
<AnchoredToastProvider>
|
||||
<ServerStateBootstrap />
|
||||
<EventRouter />
|
||||
<AppSidebarLayout>
|
||||
<Outlet />
|
||||
</AppSidebarLayout>
|
||||
<WebSocketConnectionCoordinator />
|
||||
<SlowRpcAckToastCoordinator />
|
||||
<WebSocketConnectionSurface>
|
||||
<AppSidebarLayout>
|
||||
<Outlet />
|
||||
</AppSidebarLayout>
|
||||
</WebSocketConnectionSurface>
|
||||
</AnchoredToastProvider>
|
||||
</ToastProvider>
|
||||
);
|
||||
@@ -428,8 +437,8 @@ function EventRouter() {
|
||||
queueMicrotask(flushPendingDomainEvents);
|
||||
};
|
||||
|
||||
const recoverFromSequenceGap = async (): Promise<void> => {
|
||||
if (!recovery.beginReplayRecovery("sequence-gap")) {
|
||||
const runReplayRecovery = async (reason: "sequence-gap" | "resubscribe"): Promise<void> => {
|
||||
if (!recovery.beginReplayRecovery(reason)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -466,7 +475,7 @@ function EventRouter() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
void recoverFromSequenceGap();
|
||||
void runReplayRecovery(reason);
|
||||
} else if (replayCompletion.shouldReplay && import.meta.env.MODE !== "test") {
|
||||
console.warn(
|
||||
"[orchestration-recovery]",
|
||||
@@ -505,7 +514,7 @@ function EventRouter() {
|
||||
syncServerReadModel(snapshot);
|
||||
reconcileSnapshotDerivedState();
|
||||
if (recovery.completeSnapshotRecovery(snapshot.snapshotSequence)) {
|
||||
void recoverFromSequenceGap();
|
||||
void runReplayRecovery("sequence-gap");
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
@@ -522,18 +531,29 @@ function EventRouter() {
|
||||
const fallbackToSnapshotRecovery = async (): Promise<void> => {
|
||||
await runSnapshotRecovery("replay-failed");
|
||||
};
|
||||
const unsubDomainEvent = api.orchestration.onDomainEvent((event) => {
|
||||
const action = recovery.classifyDomainEvent(event.sequence);
|
||||
if (action === "apply") {
|
||||
pendingDomainEvents.push(event);
|
||||
schedulePendingDomainEventFlush();
|
||||
return;
|
||||
}
|
||||
if (action === "recover") {
|
||||
flushPendingDomainEvents();
|
||||
void recoverFromSequenceGap();
|
||||
}
|
||||
});
|
||||
const unsubDomainEvent = api.orchestration.onDomainEvent(
|
||||
(event) => {
|
||||
const action = recovery.classifyDomainEvent(event.sequence);
|
||||
if (action === "apply") {
|
||||
pendingDomainEvents.push(event);
|
||||
schedulePendingDomainEventFlush();
|
||||
return;
|
||||
}
|
||||
if (action === "recover") {
|
||||
flushPendingDomainEvents();
|
||||
void runReplayRecovery("sequence-gap");
|
||||
}
|
||||
},
|
||||
{
|
||||
onResubscribe: () => {
|
||||
if (disposed) {
|
||||
return;
|
||||
}
|
||||
flushPendingDomainEvents();
|
||||
void runReplayRecovery("resubscribe");
|
||||
},
|
||||
},
|
||||
);
|
||||
const unsubTerminalEvent = api.terminal.onEvent((event) => {
|
||||
const thread = useStore.getState().threads.find((entry) => entry.id === event.threadId);
|
||||
if (thread && thread.archivedAt !== null) {
|
||||
|
||||
@@ -1,9 +1,22 @@
|
||||
import { WsRpcGroup } from "@t3tools/contracts";
|
||||
import { Effect, Layer } from "effect";
|
||||
import { Duration, Effect, Layer, Schedule } from "effect";
|
||||
import { RpcClient, RpcSerialization } from "effect/unstable/rpc";
|
||||
import * as Socket from "effect/unstable/socket/Socket";
|
||||
|
||||
import { resolveServerUrl } from "../lib/utils";
|
||||
import {
|
||||
acknowledgeRpcRequest,
|
||||
clearAllTrackedRpcRequests,
|
||||
trackRpcRequestSent,
|
||||
} from "./requestLatencyState";
|
||||
import {
|
||||
getWsReconnectDelayMsForRetry,
|
||||
recordWsConnectionAttempt,
|
||||
recordWsConnectionClosed,
|
||||
recordWsConnectionErrored,
|
||||
recordWsConnectionOpened,
|
||||
WS_RECONNECT_MAX_RETRIES,
|
||||
} from "./wsConnectionState";
|
||||
|
||||
export const makeWsRpcProtocolClient = RpcClient.make(WsRpcGroup);
|
||||
|
||||
@@ -17,11 +30,75 @@ export function createWsRpcProtocolLayer(url?: string) {
|
||||
protocol: window.location.protocol === "https:" ? "wss" : "ws",
|
||||
pathname: "/ws",
|
||||
});
|
||||
const trackingWebSocketConstructorLayer = Layer.succeed(
|
||||
Socket.WebSocketConstructor,
|
||||
(socketUrl, protocols) => {
|
||||
recordWsConnectionAttempt(socketUrl);
|
||||
const socket = new globalThis.WebSocket(socketUrl, protocols);
|
||||
|
||||
socket.addEventListener(
|
||||
"open",
|
||||
() => {
|
||||
recordWsConnectionOpened();
|
||||
},
|
||||
{ once: true },
|
||||
);
|
||||
socket.addEventListener(
|
||||
"error",
|
||||
() => {
|
||||
clearAllTrackedRpcRequests();
|
||||
recordWsConnectionErrored("Unable to connect to the T3 server WebSocket.");
|
||||
},
|
||||
{ once: true },
|
||||
);
|
||||
socket.addEventListener(
|
||||
"close",
|
||||
(event) => {
|
||||
clearAllTrackedRpcRequests();
|
||||
recordWsConnectionClosed({
|
||||
code: event.code,
|
||||
reason: event.reason,
|
||||
});
|
||||
},
|
||||
{ once: true },
|
||||
);
|
||||
|
||||
return socket;
|
||||
},
|
||||
);
|
||||
const socketLayer = Socket.layerWebSocket(resolvedUrl).pipe(
|
||||
Layer.provide(Socket.layerWebSocketConstructorGlobal),
|
||||
Layer.provide(trackingWebSocketConstructorLayer),
|
||||
);
|
||||
const retryPolicy = Schedule.addDelay(Schedule.recurs(WS_RECONNECT_MAX_RETRIES), (retryCount) =>
|
||||
Effect.succeed(Duration.millis(getWsReconnectDelayMsForRetry(retryCount) ?? 0)),
|
||||
);
|
||||
const protocolLayer = Layer.effect(
|
||||
RpcClient.Protocol,
|
||||
Effect.map(
|
||||
RpcClient.makeProtocolSocket({
|
||||
retryPolicy,
|
||||
retryTransientErrors: true,
|
||||
}),
|
||||
(protocol) => ({
|
||||
...protocol,
|
||||
run: (writeResponse) =>
|
||||
protocol.run((response) => {
|
||||
if (response._tag === "Chunk" || response._tag === "Exit") {
|
||||
acknowledgeRpcRequest(response.requestId);
|
||||
} else if (response._tag === "ClientProtocolError" || response._tag === "Defect") {
|
||||
clearAllTrackedRpcRequests();
|
||||
}
|
||||
return writeResponse(response);
|
||||
}),
|
||||
send: (request, transferables) => {
|
||||
if (request._tag === "Request") {
|
||||
trackRpcRequestSent(request.id, request.tag);
|
||||
}
|
||||
return protocol.send(request, transferables);
|
||||
},
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
return RpcClient.layerProtocolSocket({ retryTransientErrors: true }).pipe(
|
||||
Layer.provide(Layer.mergeAll(socketLayer, RpcSerialization.layerJson)),
|
||||
);
|
||||
return protocolLayer.pipe(Layer.provide(Layer.mergeAll(socketLayer, RpcSerialization.layerJson)));
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import {
|
||||
acknowledgeRpcRequest,
|
||||
getSlowRpcAckRequests,
|
||||
resetRequestLatencyStateForTests,
|
||||
trackRpcRequestSent,
|
||||
SLOW_RPC_ACK_THRESHOLD_MS,
|
||||
MAX_TRACKED_RPC_ACK_REQUESTS,
|
||||
} from "./requestLatencyState";
|
||||
|
||||
describe("requestLatencyState", () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
resetRequestLatencyStateForTests();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("marks unary requests as slow when the ack threshold is exceeded", () => {
|
||||
trackRpcRequestSent("1", "server.getConfig");
|
||||
vi.advanceTimersByTime(SLOW_RPC_ACK_THRESHOLD_MS - 1);
|
||||
expect(getSlowRpcAckRequests()).toEqual([]);
|
||||
|
||||
vi.advanceTimersByTime(1);
|
||||
expect(getSlowRpcAckRequests()).toMatchObject([
|
||||
{
|
||||
requestId: "1",
|
||||
tag: "server.getConfig",
|
||||
thresholdMs: SLOW_RPC_ACK_THRESHOLD_MS,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("clears the slow request once the server acknowledges it", () => {
|
||||
trackRpcRequestSent("1", "git.status");
|
||||
vi.advanceTimersByTime(SLOW_RPC_ACK_THRESHOLD_MS);
|
||||
expect(getSlowRpcAckRequests()).toHaveLength(1);
|
||||
|
||||
acknowledgeRpcRequest("1");
|
||||
expect(getSlowRpcAckRequests()).toEqual([]);
|
||||
});
|
||||
|
||||
it("ignores long-lived subscribe requests", () => {
|
||||
trackRpcRequestSent("1", "subscribeServerConfig");
|
||||
vi.advanceTimersByTime(SLOW_RPC_ACK_THRESHOLD_MS * 2);
|
||||
|
||||
expect(getSlowRpcAckRequests()).toEqual([]);
|
||||
});
|
||||
|
||||
it("evicts the oldest pending requests once the tracker reaches capacity", () => {
|
||||
for (let index = 0; index < MAX_TRACKED_RPC_ACK_REQUESTS + 1; index += 1) {
|
||||
trackRpcRequestSent(String(index), "server.getConfig");
|
||||
}
|
||||
|
||||
vi.advanceTimersByTime(SLOW_RPC_ACK_THRESHOLD_MS);
|
||||
|
||||
const slowRequests = getSlowRpcAckRequests();
|
||||
expect(slowRequests).toHaveLength(MAX_TRACKED_RPC_ACK_REQUESTS);
|
||||
expect(slowRequests[0]?.requestId).toBe("1");
|
||||
expect(slowRequests.at(-1)?.requestId).toBe(String(MAX_TRACKED_RPC_ACK_REQUESTS));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,127 @@
|
||||
import { useAtomValue } from "@effect/atom-react";
|
||||
import { Atom } from "effect/unstable/reactivity";
|
||||
|
||||
import { appAtomRegistry } from "./atomRegistry";
|
||||
|
||||
export const SLOW_RPC_ACK_THRESHOLD_MS = 2_500;
|
||||
export const MAX_TRACKED_RPC_ACK_REQUESTS = 256;
|
||||
|
||||
export interface SlowRpcAckRequest {
|
||||
readonly requestId: string;
|
||||
readonly startedAt: string;
|
||||
readonly startedAtMs: number;
|
||||
readonly tag: string;
|
||||
readonly thresholdMs: number;
|
||||
}
|
||||
|
||||
interface PendingRpcAckRequest {
|
||||
readonly request: SlowRpcAckRequest;
|
||||
readonly timeoutId: ReturnType<typeof setTimeout>;
|
||||
}
|
||||
|
||||
const pendingRpcAckRequests = new Map<string, PendingRpcAckRequest>();
|
||||
|
||||
const slowRpcAckRequestsAtom = Atom.make<ReadonlyArray<SlowRpcAckRequest>>([]).pipe(
|
||||
Atom.keepAlive,
|
||||
Atom.withLabel("slow-rpc-ack-requests"),
|
||||
);
|
||||
|
||||
function setSlowRpcAckRequests(requests: ReadonlyArray<SlowRpcAckRequest>) {
|
||||
appAtomRegistry.set(slowRpcAckRequestsAtom, [...requests]);
|
||||
}
|
||||
|
||||
function getSlowRpcAckRequestsValue(): ReadonlyArray<SlowRpcAckRequest> {
|
||||
return appAtomRegistry.get(slowRpcAckRequestsAtom);
|
||||
}
|
||||
|
||||
function shouldTrackRpcAck(tag: string): boolean {
|
||||
return !tag.startsWith("subscribe");
|
||||
}
|
||||
|
||||
export function getSlowRpcAckRequests(): ReadonlyArray<SlowRpcAckRequest> {
|
||||
return getSlowRpcAckRequestsValue();
|
||||
}
|
||||
|
||||
export function trackRpcRequestSent(requestId: string, tag: string): void {
|
||||
if (!shouldTrackRpcAck(tag)) {
|
||||
return;
|
||||
}
|
||||
|
||||
clearTrackedRpcRequest(requestId);
|
||||
evictOldestPendingRpcRequestIfNeeded();
|
||||
|
||||
const startedAtMs = Date.now();
|
||||
const request: SlowRpcAckRequest = {
|
||||
requestId,
|
||||
startedAt: new Date(startedAtMs).toISOString(),
|
||||
startedAtMs,
|
||||
tag,
|
||||
thresholdMs: SLOW_RPC_ACK_THRESHOLD_MS,
|
||||
};
|
||||
const timeoutId = setTimeout(() => {
|
||||
pendingRpcAckRequests.delete(requestId);
|
||||
appendSlowRpcAckRequest(request);
|
||||
}, SLOW_RPC_ACK_THRESHOLD_MS);
|
||||
|
||||
pendingRpcAckRequests.set(requestId, {
|
||||
request,
|
||||
timeoutId,
|
||||
});
|
||||
}
|
||||
|
||||
export function acknowledgeRpcRequest(requestId: string): void {
|
||||
clearTrackedRpcRequest(requestId);
|
||||
const slowRequests = getSlowRpcAckRequestsValue();
|
||||
if (!slowRequests.some((request) => request.requestId === requestId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
setSlowRpcAckRequests(slowRequests.filter((request) => request.requestId !== requestId));
|
||||
}
|
||||
|
||||
export function clearAllTrackedRpcRequests(): void {
|
||||
for (const pending of pendingRpcAckRequests.values()) {
|
||||
clearTimeout(pending.timeoutId);
|
||||
}
|
||||
pendingRpcAckRequests.clear();
|
||||
setSlowRpcAckRequests([]);
|
||||
}
|
||||
|
||||
function clearTrackedRpcRequest(requestId: string): void {
|
||||
const pending = pendingRpcAckRequests.get(requestId);
|
||||
if (!pending) {
|
||||
return;
|
||||
}
|
||||
|
||||
clearTimeout(pending.timeoutId);
|
||||
pendingRpcAckRequests.delete(requestId);
|
||||
}
|
||||
|
||||
function appendSlowRpcAckRequest(request: SlowRpcAckRequest): void {
|
||||
const requests = [...getSlowRpcAckRequestsValue(), request];
|
||||
if (requests.length <= MAX_TRACKED_RPC_ACK_REQUESTS) {
|
||||
setSlowRpcAckRequests(requests);
|
||||
return;
|
||||
}
|
||||
|
||||
setSlowRpcAckRequests(requests.slice(-MAX_TRACKED_RPC_ACK_REQUESTS));
|
||||
}
|
||||
|
||||
function evictOldestPendingRpcRequestIfNeeded(): void {
|
||||
while (pendingRpcAckRequests.size >= MAX_TRACKED_RPC_ACK_REQUESTS) {
|
||||
const oldestRequestId = pendingRpcAckRequests.keys().next().value;
|
||||
if (oldestRequestId === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
clearTrackedRpcRequest(oldestRequestId);
|
||||
}
|
||||
}
|
||||
|
||||
export function resetRequestLatencyStateForTests(): void {
|
||||
clearAllTrackedRpcRequests();
|
||||
}
|
||||
|
||||
export function useSlowRpcAckRequests(): ReadonlyArray<SlowRpcAckRequest> {
|
||||
return useAtomValue(slowRpcAckRequestsAtom);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { isTransportConnectionErrorMessage, sanitizeThreadErrorMessage } from "./transportError";
|
||||
|
||||
describe("transportError", () => {
|
||||
it("detects websocket transport failures", () => {
|
||||
expect(isTransportConnectionErrorMessage("SocketCloseError: 1006")).toBe(true);
|
||||
expect(isTransportConnectionErrorMessage("Unable to connect to the T3 server WebSocket.")).toBe(
|
||||
true,
|
||||
);
|
||||
expect(isTransportConnectionErrorMessage("SocketOpenError: Timeout")).toBe(true);
|
||||
});
|
||||
|
||||
it("preserves non-transport thread errors", () => {
|
||||
expect(sanitizeThreadErrorMessage("Turn failed")).toBe("Turn failed");
|
||||
expect(sanitizeThreadErrorMessage("Select a base branch before sending.")).toBe(
|
||||
"Select a base branch before sending.",
|
||||
);
|
||||
});
|
||||
|
||||
it("drops transport failures from thread surfaces", () => {
|
||||
expect(sanitizeThreadErrorMessage("SocketCloseError: 1006")).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,23 @@
|
||||
const TRANSPORT_ERROR_PATTERNS = [
|
||||
/\bSocketCloseError\b/i,
|
||||
/\bSocketOpenError\b/i,
|
||||
/Unable to connect to the T3 server WebSocket\./i,
|
||||
/\bping timeout\b/i,
|
||||
] as const;
|
||||
|
||||
export function isTransportConnectionErrorMessage(message: string | null | undefined): boolean {
|
||||
if (typeof message !== "string") {
|
||||
return false;
|
||||
}
|
||||
|
||||
const normalizedMessage = message.trim();
|
||||
if (normalizedMessage.length === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return TRANSPORT_ERROR_PATTERNS.some((pattern) => pattern.test(normalizedMessage));
|
||||
}
|
||||
|
||||
export function sanitizeThreadErrorMessage(message: string | null | undefined): string | null {
|
||||
return isTransportConnectionErrorMessage(message) ? null : (message ?? null);
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import {
|
||||
exhaustWsReconnectIfStillWaiting,
|
||||
getWsConnectionStatus,
|
||||
getWsReconnectDelayMsForRetry,
|
||||
getWsConnectionUiState,
|
||||
recordWsConnectionAttempt,
|
||||
recordWsConnectionClosed,
|
||||
recordWsConnectionErrored,
|
||||
recordWsConnectionOpened,
|
||||
resetWsConnectionStateForTests,
|
||||
setBrowserOnlineStatus,
|
||||
WS_RECONNECT_MAX_ATTEMPTS,
|
||||
} from "./wsConnectionState";
|
||||
|
||||
describe("wsConnectionState", () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(new Date("2026-04-03T20:30:00.000Z"));
|
||||
resetWsConnectionStateForTests();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("treats a disconnected browser as offline once the websocket drops", () => {
|
||||
recordWsConnectionAttempt("ws://localhost:3020/ws");
|
||||
recordWsConnectionOpened();
|
||||
recordWsConnectionClosed({ code: 1006, reason: "offline" });
|
||||
setBrowserOnlineStatus(false);
|
||||
|
||||
expect(getWsConnectionUiState(getWsConnectionStatus())).toBe("offline");
|
||||
});
|
||||
|
||||
it("stays in the initial connecting state until the first disconnect", () => {
|
||||
recordWsConnectionAttempt("ws://localhost:3020/ws");
|
||||
|
||||
expect(getWsConnectionStatus()).toMatchObject({
|
||||
attemptCount: 1,
|
||||
hasConnected: false,
|
||||
phase: "connecting",
|
||||
});
|
||||
expect(getWsConnectionUiState(getWsConnectionStatus())).toBe("connecting");
|
||||
});
|
||||
|
||||
it("schedules the next retry after a failed websocket attempt", () => {
|
||||
recordWsConnectionAttempt("ws://localhost:3020/ws");
|
||||
recordWsConnectionErrored("Unable to connect to the T3 server WebSocket.");
|
||||
|
||||
const firstRetryDelayMs = getWsReconnectDelayMsForRetry(0);
|
||||
if (firstRetryDelayMs === null) {
|
||||
throw new Error("Expected an initial retry delay.");
|
||||
}
|
||||
|
||||
expect(getWsConnectionStatus()).toMatchObject({
|
||||
nextRetryAt: new Date(Date.now() + firstRetryDelayMs).toISOString(),
|
||||
reconnectAttemptCount: 1,
|
||||
reconnectPhase: "waiting",
|
||||
});
|
||||
});
|
||||
|
||||
it("marks the reconnect cycle as exhausted after the final attempt fails", () => {
|
||||
for (let attempt = 0; attempt < WS_RECONNECT_MAX_ATTEMPTS; attempt += 1) {
|
||||
recordWsConnectionAttempt("ws://localhost:3020/ws");
|
||||
recordWsConnectionErrored("Unable to connect to the T3 server WebSocket.");
|
||||
}
|
||||
|
||||
expect(getWsConnectionStatus()).toMatchObject({
|
||||
nextRetryAt: null,
|
||||
reconnectAttemptCount: WS_RECONNECT_MAX_ATTEMPTS,
|
||||
reconnectPhase: "exhausted",
|
||||
});
|
||||
});
|
||||
|
||||
it("can exhaust a stalled final retry window when no new attempt starts", () => {
|
||||
recordWsConnectionAttempt("ws://localhost:3020/ws");
|
||||
recordWsConnectionOpened();
|
||||
|
||||
for (let attempt = 0; attempt < WS_RECONNECT_MAX_ATTEMPTS - 1; attempt += 1) {
|
||||
recordWsConnectionAttempt("ws://localhost:3020/ws");
|
||||
recordWsConnectionErrored("Unable to connect to the T3 server WebSocket.");
|
||||
}
|
||||
|
||||
const finalRetryDelayMs = getWsReconnectDelayMsForRetry(WS_RECONNECT_MAX_ATTEMPTS - 2);
|
||||
if (finalRetryDelayMs === null) {
|
||||
throw new Error("Expected a final retry delay.");
|
||||
}
|
||||
|
||||
const statusBeforeExhaust = getWsConnectionStatus();
|
||||
expect(statusBeforeExhaust).toMatchObject({
|
||||
nextRetryAt: new Date(Date.now() + finalRetryDelayMs).toISOString(),
|
||||
reconnectAttemptCount: 7,
|
||||
reconnectPhase: "waiting",
|
||||
});
|
||||
|
||||
const nextRetryAt = statusBeforeExhaust.nextRetryAt;
|
||||
if (!nextRetryAt) {
|
||||
throw new Error("Expected a scheduled retry.");
|
||||
}
|
||||
|
||||
vi.setSystemTime(new Date(Date.now() + finalRetryDelayMs + 1_000));
|
||||
exhaustWsReconnectIfStillWaiting(nextRetryAt);
|
||||
|
||||
expect(getWsConnectionStatus()).toMatchObject({
|
||||
nextRetryAt: null,
|
||||
reconnectAttemptCount: WS_RECONNECT_MAX_ATTEMPTS,
|
||||
reconnectPhase: "exhausted",
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,219 @@
|
||||
import { useAtomValue } from "@effect/atom-react";
|
||||
import { Atom } from "effect/unstable/reactivity";
|
||||
|
||||
import { appAtomRegistry } from "./atomRegistry";
|
||||
|
||||
export type WsConnectionUiState = "connected" | "connecting" | "error" | "offline" | "reconnecting";
|
||||
export type WsReconnectPhase = "attempting" | "exhausted" | "idle" | "waiting";
|
||||
|
||||
export const WS_RECONNECT_INITIAL_DELAY_MS = 1_000;
|
||||
export const WS_RECONNECT_BACKOFF_FACTOR = 2;
|
||||
export const WS_RECONNECT_MAX_DELAY_MS = 64_000;
|
||||
export const WS_RECONNECT_MAX_RETRIES = 7;
|
||||
export const WS_RECONNECT_MAX_ATTEMPTS = WS_RECONNECT_MAX_RETRIES + 1;
|
||||
|
||||
export interface WsConnectionStatus {
|
||||
readonly attemptCount: number;
|
||||
readonly closeCode: number | null;
|
||||
readonly closeReason: string | null;
|
||||
readonly connectedAt: string | null;
|
||||
readonly disconnectedAt: string | null;
|
||||
readonly hasConnected: boolean;
|
||||
readonly lastError: string | null;
|
||||
readonly lastErrorAt: string | null;
|
||||
readonly nextRetryAt: string | null;
|
||||
readonly online: boolean;
|
||||
readonly phase: "idle" | "connecting" | "connected" | "disconnected";
|
||||
readonly reconnectAttemptCount: number;
|
||||
readonly reconnectMaxAttempts: number;
|
||||
readonly reconnectPhase: WsReconnectPhase;
|
||||
readonly socketUrl: string | null;
|
||||
}
|
||||
|
||||
const INITIAL_WS_CONNECTION_STATUS = Object.freeze<WsConnectionStatus>({
|
||||
attemptCount: 0,
|
||||
closeCode: null,
|
||||
closeReason: null,
|
||||
connectedAt: null,
|
||||
disconnectedAt: null,
|
||||
hasConnected: false,
|
||||
lastError: null,
|
||||
lastErrorAt: null,
|
||||
nextRetryAt: null,
|
||||
online: typeof navigator === "undefined" ? true : navigator.onLine !== false,
|
||||
phase: "idle",
|
||||
reconnectAttemptCount: 0,
|
||||
reconnectMaxAttempts: WS_RECONNECT_MAX_ATTEMPTS,
|
||||
reconnectPhase: "idle",
|
||||
socketUrl: null,
|
||||
});
|
||||
|
||||
export const wsConnectionStatusAtom = Atom.make(INITIAL_WS_CONNECTION_STATUS).pipe(
|
||||
Atom.keepAlive,
|
||||
Atom.withLabel("ws-connection-status"),
|
||||
);
|
||||
|
||||
function isoNow() {
|
||||
return new Date().toISOString();
|
||||
}
|
||||
|
||||
function updateWsConnectionStatus(
|
||||
updater: (current: WsConnectionStatus) => WsConnectionStatus,
|
||||
): WsConnectionStatus {
|
||||
const nextStatus = updater(getWsConnectionStatus());
|
||||
appAtomRegistry.set(wsConnectionStatusAtom, nextStatus);
|
||||
return nextStatus;
|
||||
}
|
||||
|
||||
export function getWsConnectionStatus(): WsConnectionStatus {
|
||||
return appAtomRegistry.get(wsConnectionStatusAtom);
|
||||
}
|
||||
|
||||
export function getWsConnectionUiState(status: WsConnectionStatus): WsConnectionUiState {
|
||||
if (status.phase === "connected") {
|
||||
return "connected";
|
||||
}
|
||||
|
||||
if (!status.online && (status.disconnectedAt !== null || status.phase === "disconnected")) {
|
||||
return "offline";
|
||||
}
|
||||
|
||||
if (!status.hasConnected) {
|
||||
return status.phase === "disconnected" ? "error" : "connecting";
|
||||
}
|
||||
|
||||
return "reconnecting";
|
||||
}
|
||||
|
||||
export function recordWsConnectionAttempt(socketUrl: string): WsConnectionStatus {
|
||||
return updateWsConnectionStatus((current) => ({
|
||||
...current,
|
||||
attemptCount: current.attemptCount + 1,
|
||||
nextRetryAt: null,
|
||||
phase: "connecting",
|
||||
reconnectAttemptCount: current.phase === "connected" ? 1 : current.reconnectAttemptCount + 1,
|
||||
reconnectPhase: "attempting",
|
||||
socketUrl,
|
||||
}));
|
||||
}
|
||||
|
||||
export function recordWsConnectionOpened(): WsConnectionStatus {
|
||||
return updateWsConnectionStatus((current) => ({
|
||||
...current,
|
||||
closeCode: null,
|
||||
closeReason: null,
|
||||
connectedAt: isoNow(),
|
||||
disconnectedAt: null,
|
||||
hasConnected: true,
|
||||
nextRetryAt: null,
|
||||
phase: "connected",
|
||||
reconnectAttemptCount: 0,
|
||||
reconnectPhase: "idle",
|
||||
}));
|
||||
}
|
||||
|
||||
export function recordWsConnectionErrored(message?: string | null): WsConnectionStatus {
|
||||
return updateWsConnectionStatus((current) =>
|
||||
applyDisconnectState(current, {
|
||||
lastError: message?.trim() ? message : current.lastError,
|
||||
lastErrorAt: isoNow(),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export function recordWsConnectionClosed(details?: {
|
||||
readonly code?: number;
|
||||
readonly reason?: string;
|
||||
}): WsConnectionStatus {
|
||||
return updateWsConnectionStatus((current) =>
|
||||
applyDisconnectState(current, {
|
||||
closeCode: details?.code ?? current.closeCode,
|
||||
closeReason: details?.reason?.trim() ? details.reason : current.closeReason,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export function setBrowserOnlineStatus(online: boolean): WsConnectionStatus {
|
||||
return updateWsConnectionStatus((current) => ({
|
||||
...current,
|
||||
online,
|
||||
}));
|
||||
}
|
||||
|
||||
export function resetWsReconnectBackoff(): WsConnectionStatus {
|
||||
return updateWsConnectionStatus((current) => ({
|
||||
...current,
|
||||
nextRetryAt: null,
|
||||
reconnectAttemptCount: 0,
|
||||
reconnectPhase: "idle",
|
||||
}));
|
||||
}
|
||||
|
||||
export function exhaustWsReconnectIfStillWaiting(expectedNextRetryAt: string): WsConnectionStatus {
|
||||
return updateWsConnectionStatus((current) => {
|
||||
if (
|
||||
current.reconnectPhase !== "waiting" ||
|
||||
current.nextRetryAt !== expectedNextRetryAt ||
|
||||
!current.online ||
|
||||
!current.hasConnected
|
||||
) {
|
||||
return current;
|
||||
}
|
||||
|
||||
return {
|
||||
...current,
|
||||
nextRetryAt: null,
|
||||
reconnectAttemptCount: current.reconnectMaxAttempts,
|
||||
reconnectPhase: "exhausted",
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export function resetWsConnectionStateForTests(): void {
|
||||
appAtomRegistry.set(wsConnectionStatusAtom, INITIAL_WS_CONNECTION_STATUS);
|
||||
}
|
||||
|
||||
export function useWsConnectionStatus(): WsConnectionStatus {
|
||||
return useAtomValue(wsConnectionStatusAtom);
|
||||
}
|
||||
|
||||
export function getWsReconnectDelayMsForRetry(retryIndex: number): number | null {
|
||||
if (!Number.isInteger(retryIndex) || retryIndex < 0 || retryIndex >= WS_RECONNECT_MAX_RETRIES) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Math.min(
|
||||
Math.round(WS_RECONNECT_INITIAL_DELAY_MS * WS_RECONNECT_BACKOFF_FACTOR ** retryIndex),
|
||||
WS_RECONNECT_MAX_DELAY_MS,
|
||||
);
|
||||
}
|
||||
|
||||
function applyDisconnectState(
|
||||
current: WsConnectionStatus,
|
||||
updates: Partial<
|
||||
Pick<WsConnectionStatus, "closeCode" | "closeReason" | "lastError" | "lastErrorAt">
|
||||
>,
|
||||
): WsConnectionStatus {
|
||||
const disconnectedAt = current.disconnectedAt ?? isoNow();
|
||||
const nextRetryDelayMs =
|
||||
current.nextRetryAt !== null || current.reconnectPhase === "exhausted"
|
||||
? null
|
||||
: getWsReconnectDelayMsForRetry(Math.max(0, current.reconnectAttemptCount - 1));
|
||||
|
||||
return {
|
||||
...current,
|
||||
...updates,
|
||||
disconnectedAt,
|
||||
nextRetryAt:
|
||||
nextRetryDelayMs === null
|
||||
? current.nextRetryAt
|
||||
: new Date(Date.now() + nextRetryDelayMs).toISOString(),
|
||||
phase: "disconnected",
|
||||
reconnectPhase:
|
||||
current.reconnectPhase === "waiting" || current.reconnectPhase === "exhausted"
|
||||
? current.reconnectPhase
|
||||
: nextRetryDelayMs === null
|
||||
? "exhausted"
|
||||
: "waiting",
|
||||
};
|
||||
}
|
||||
@@ -19,6 +19,7 @@ import {
|
||||
derivePendingApprovals,
|
||||
derivePendingUserInputs,
|
||||
} from "./session-logic";
|
||||
import { sanitizeThreadErrorMessage } from "./rpc/transportError";
|
||||
import { type ChatMessage, type Project, type SidebarThreadSummary, type Thread } from "./types";
|
||||
|
||||
// ── State ────────────────────────────────────────────────────────────
|
||||
@@ -165,7 +166,7 @@ function mapThread(thread: OrchestrationThread): Thread {
|
||||
session: thread.session ? mapSession(thread.session) : null,
|
||||
messages: thread.messages.map(mapMessage),
|
||||
proposedPlans: thread.proposedPlans.map(mapProposedPlan),
|
||||
error: thread.session?.lastError ?? null,
|
||||
error: sanitizeThreadErrorMessage(thread.session?.lastError),
|
||||
createdAt: thread.createdAt,
|
||||
archivedAt: thread.archivedAt,
|
||||
updatedAt: thread.updatedAt,
|
||||
@@ -894,7 +895,7 @@ export function applyOrchestrationEvent(state: AppState, event: OrchestrationEve
|
||||
return updateThreadState(state, event.payload.threadId, (thread) => ({
|
||||
...thread,
|
||||
session: mapSession(event.payload.session),
|
||||
error: event.payload.session.lastError ?? null,
|
||||
error: sanitizeThreadErrorMessage(event.payload.session.lastError),
|
||||
latestTurn:
|
||||
event.payload.session.status === "running" && event.payload.session.activeTurnId !== null
|
||||
? buildLatestTurn({
|
||||
|
||||
@@ -243,6 +243,20 @@ describe("wsNativeApi", () => {
|
||||
expect(onDomainEvent).toHaveBeenCalledWith(orchestrationEvent);
|
||||
});
|
||||
|
||||
it("forwards orchestration stream subscription options to the RPC client", async () => {
|
||||
const { createWsNativeApi } = await import("./wsNativeApi");
|
||||
|
||||
const api = createWsNativeApi();
|
||||
const onDomainEvent = vi.fn();
|
||||
const onResubscribe = vi.fn();
|
||||
|
||||
api.orchestration.onDomainEvent(onDomainEvent, { onResubscribe });
|
||||
|
||||
expect(rpcClientMock.orchestration.onDomainEvent).toHaveBeenCalledWith(onDomainEvent, {
|
||||
onResubscribe,
|
||||
});
|
||||
});
|
||||
|
||||
it("sends orchestration dispatch commands as the direct RPC payload", async () => {
|
||||
rpcClientMock.orchestration.dispatchCommand.mockResolvedValue({ sequence: 1 });
|
||||
const { createWsNativeApi } = await import("./wsNativeApi");
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { type ContextMenuItem, type NativeApi } from "@t3tools/contracts";
|
||||
|
||||
import { showContextMenuFallback } from "./contextMenuFallback";
|
||||
import { resetRequestLatencyStateForTests } from "./rpc/requestLatencyState";
|
||||
import { resetServerStateForTests } from "./rpc/serverState";
|
||||
import { resetWsConnectionStateForTests } from "./rpc/wsConnectionState";
|
||||
import { __resetWsRpcClientForTests, getWsRpcClient } from "./wsRpcClient";
|
||||
|
||||
let instance: { api: NativeApi } | null = null;
|
||||
@@ -9,7 +11,9 @@ let instance: { api: NativeApi } | null = null;
|
||||
export function __resetWsNativeApiForTests() {
|
||||
instance = null;
|
||||
__resetWsRpcClientForTests();
|
||||
resetRequestLatencyStateForTests();
|
||||
resetServerStateForTests();
|
||||
resetWsConnectionStateForTests();
|
||||
}
|
||||
|
||||
export function createWsNativeApi(): NativeApi {
|
||||
@@ -98,7 +102,8 @@ export function createWsNativeApi(): NativeApi {
|
||||
rpcClient.orchestration
|
||||
.replayEvents({ fromSequenceExclusive })
|
||||
.then((events) => [...events]),
|
||||
onDomainEvent: (callback) => rpcClient.orchestration.onDomainEvent(callback),
|
||||
onDomainEvent: (callback, options) =>
|
||||
rpcClient.orchestration.onDomainEvent(callback, options),
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -10,12 +10,17 @@ import {
|
||||
import { Effect, Stream } from "effect";
|
||||
|
||||
import { type WsRpcProtocolClient } from "./rpc/protocol";
|
||||
import { resetWsReconnectBackoff } from "./rpc/wsConnectionState";
|
||||
import { WsTransport } from "./wsTransport";
|
||||
|
||||
type RpcTag = keyof WsRpcProtocolClient & string;
|
||||
type RpcMethod<TTag extends RpcTag> = WsRpcProtocolClient[TTag];
|
||||
type RpcInput<TTag extends RpcTag> = Parameters<RpcMethod<TTag>>[0];
|
||||
|
||||
interface StreamSubscriptionOptions {
|
||||
readonly onResubscribe?: () => void;
|
||||
}
|
||||
|
||||
type RpcUnaryMethod<TTag extends RpcTag> =
|
||||
RpcMethod<TTag> extends (input: any, options?: any) => Effect.Effect<infer TSuccess, any, any>
|
||||
? (input: RpcInput<TTag>) => Promise<TSuccess>
|
||||
@@ -28,7 +33,7 @@ type RpcUnaryNoArgMethod<TTag extends RpcTag> =
|
||||
|
||||
type RpcStreamMethod<TTag extends RpcTag> =
|
||||
RpcMethod<TTag> extends (input: any, options?: any) => Stream.Stream<infer TEvent, any, any>
|
||||
? (listener: (event: TEvent) => void) => () => void
|
||||
? (listener: (event: TEvent) => void, options?: StreamSubscriptionOptions) => () => void
|
||||
: never;
|
||||
|
||||
interface GitRunStackedActionOptions {
|
||||
@@ -37,6 +42,7 @@ interface GitRunStackedActionOptions {
|
||||
|
||||
export interface WsRpcClient {
|
||||
readonly dispose: () => Promise<void>;
|
||||
readonly reconnect: () => Promise<void>;
|
||||
readonly terminal: {
|
||||
readonly open: RpcUnaryMethod<typeof WS_METHODS.terminalOpen>;
|
||||
readonly write: RpcUnaryMethod<typeof WS_METHODS.terminalWrite>;
|
||||
@@ -113,6 +119,10 @@ export async function __resetWsRpcClientForTests() {
|
||||
export function createWsRpcClient(transport = new WsTransport()): WsRpcClient {
|
||||
return {
|
||||
dispose: () => transport.dispose(),
|
||||
reconnect: async () => {
|
||||
resetWsReconnectBackoff();
|
||||
await transport.reconnect();
|
||||
},
|
||||
terminal: {
|
||||
open: (input) => transport.request((client) => client[WS_METHODS.terminalOpen](input)),
|
||||
write: (input) => transport.request((client) => client[WS_METHODS.terminalWrite](input)),
|
||||
@@ -120,8 +130,12 @@ export function createWsRpcClient(transport = new WsTransport()): WsRpcClient {
|
||||
clear: (input) => transport.request((client) => client[WS_METHODS.terminalClear](input)),
|
||||
restart: (input) => transport.request((client) => client[WS_METHODS.terminalRestart](input)),
|
||||
close: (input) => transport.request((client) => client[WS_METHODS.terminalClose](input)),
|
||||
onEvent: (listener) =>
|
||||
transport.subscribe((client) => client[WS_METHODS.subscribeTerminalEvents]({}), listener),
|
||||
onEvent: (listener, options) =>
|
||||
transport.subscribe(
|
||||
(client) => client[WS_METHODS.subscribeTerminalEvents]({}),
|
||||
listener,
|
||||
options,
|
||||
),
|
||||
},
|
||||
projects: {
|
||||
searchEntries: (input) =>
|
||||
@@ -179,10 +193,18 @@ export function createWsRpcClient(transport = new WsTransport()): WsRpcClient {
|
||||
getSettings: () => transport.request((client) => client[WS_METHODS.serverGetSettings]({})),
|
||||
updateSettings: (patch) =>
|
||||
transport.request((client) => client[WS_METHODS.serverUpdateSettings]({ patch })),
|
||||
subscribeConfig: (listener) =>
|
||||
transport.subscribe((client) => client[WS_METHODS.subscribeServerConfig]({}), listener),
|
||||
subscribeLifecycle: (listener) =>
|
||||
transport.subscribe((client) => client[WS_METHODS.subscribeServerLifecycle]({}), listener),
|
||||
subscribeConfig: (listener, options) =>
|
||||
transport.subscribe(
|
||||
(client) => client[WS_METHODS.subscribeServerConfig]({}),
|
||||
listener,
|
||||
options,
|
||||
),
|
||||
subscribeLifecycle: (listener, options) =>
|
||||
transport.subscribe(
|
||||
(client) => client[WS_METHODS.subscribeServerLifecycle]({}),
|
||||
listener,
|
||||
options,
|
||||
),
|
||||
},
|
||||
orchestration: {
|
||||
getSnapshot: () =>
|
||||
@@ -197,10 +219,11 @@ export function createWsRpcClient(transport = new WsTransport()): WsRpcClient {
|
||||
transport
|
||||
.request((client) => client[ORCHESTRATION_WS_METHODS.replayEvents](input))
|
||||
.then((events) => [...events]),
|
||||
onDomainEvent: (listener) =>
|
||||
onDomainEvent: (listener, options) =>
|
||||
transport.subscribe(
|
||||
(client) => client[WS_METHODS.subscribeOrchestrationDomainEvents]({}),
|
||||
listener,
|
||||
options,
|
||||
),
|
||||
},
|
||||
};
|
||||
|
||||
@@ -5,6 +5,12 @@ import {
|
||||
__resetClientTracingForTests,
|
||||
configureClientTracing,
|
||||
} from "./observability/clientTracing";
|
||||
import { getSlowRpcAckRequests, resetRequestLatencyStateForTests } from "./rpc/requestLatencyState";
|
||||
import {
|
||||
getWsConnectionStatus,
|
||||
getWsConnectionUiState,
|
||||
resetWsConnectionStateForTests,
|
||||
} from "./rpc/wsConnectionState";
|
||||
import { WsTransport } from "./wsTransport";
|
||||
|
||||
type WsEventType = "open" | "message" | "close" | "error";
|
||||
@@ -57,6 +63,10 @@ class MockWebSocket {
|
||||
this.emit("message", { data, type: "message" });
|
||||
}
|
||||
|
||||
error() {
|
||||
this.emit("error", { type: "error" });
|
||||
}
|
||||
|
||||
private emit(type: WsEventType, event?: WsEvent) {
|
||||
const listeners = this.listeners.get(type);
|
||||
if (!listeners) return;
|
||||
@@ -93,7 +103,10 @@ async function waitFor(assertion: () => void, timeoutMs = 1_000): Promise<void>
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.useRealTimers();
|
||||
sockets.length = 0;
|
||||
resetRequestLatencyStateForTests();
|
||||
resetWsConnectionStateForTests();
|
||||
|
||||
Object.defineProperty(globalThis, "window", {
|
||||
configurable: true,
|
||||
@@ -107,14 +120,20 @@ beforeEach(() => {
|
||||
desktopBridge: undefined,
|
||||
},
|
||||
});
|
||||
Object.defineProperty(globalThis, "navigator", {
|
||||
configurable: true,
|
||||
value: { onLine: true },
|
||||
});
|
||||
|
||||
globalThis.WebSocket = MockWebSocket as unknown as typeof WebSocket;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(async () => {
|
||||
globalThis.WebSocket = originalWebSocket;
|
||||
globalThis.fetch = originalFetch;
|
||||
void __resetClientTracingForTests();
|
||||
resetRequestLatencyStateForTests();
|
||||
resetWsConnectionStateForTests();
|
||||
await __resetClientTracingForTests();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
@@ -148,6 +167,184 @@ describe("WsTransport", () => {
|
||||
await transport.dispose();
|
||||
});
|
||||
|
||||
it("tracks initial connection failures for the app error state", async () => {
|
||||
const transport = new WsTransport("ws://localhost:3020");
|
||||
|
||||
await waitFor(() => {
|
||||
expect(sockets).toHaveLength(1);
|
||||
});
|
||||
|
||||
const socket = getSocket();
|
||||
expect(getWsConnectionStatus()).toMatchObject({
|
||||
attemptCount: 1,
|
||||
phase: "connecting",
|
||||
socketUrl: "ws://localhost:3020/ws",
|
||||
});
|
||||
|
||||
socket.error();
|
||||
socket.close(1006, "server unavailable");
|
||||
|
||||
await waitFor(() => {
|
||||
expect(getWsConnectionStatus()).toMatchObject({
|
||||
closeCode: 1006,
|
||||
closeReason: "server unavailable",
|
||||
hasConnected: false,
|
||||
lastError: "Unable to connect to the T3 server WebSocket.",
|
||||
phase: "disconnected",
|
||||
});
|
||||
});
|
||||
expect(getWsConnectionUiState(getWsConnectionStatus())).toBe("error");
|
||||
|
||||
await transport.dispose();
|
||||
});
|
||||
|
||||
it("surfaces reconnecting state after a live socket disconnects", async () => {
|
||||
const transport = new WsTransport("ws://localhost:3020");
|
||||
|
||||
await waitFor(() => {
|
||||
expect(sockets).toHaveLength(1);
|
||||
});
|
||||
|
||||
const socket = getSocket();
|
||||
socket.open();
|
||||
|
||||
await waitFor(() => {
|
||||
expect(getWsConnectionStatus()).toMatchObject({
|
||||
hasConnected: true,
|
||||
phase: "connected",
|
||||
});
|
||||
});
|
||||
|
||||
socket.close(1013, "try again later");
|
||||
|
||||
await waitFor(() => {
|
||||
expect(getWsConnectionStatus()).toMatchObject({
|
||||
closeReason: "try again later",
|
||||
hasConnected: true,
|
||||
});
|
||||
});
|
||||
expect(getWsConnectionUiState(getWsConnectionStatus())).toBe("reconnecting");
|
||||
|
||||
await transport.dispose();
|
||||
});
|
||||
|
||||
it("reconnects the websocket session without disposing the transport", async () => {
|
||||
const transport = new WsTransport("ws://localhost:3020");
|
||||
|
||||
await waitFor(() => {
|
||||
expect(sockets).toHaveLength(1);
|
||||
});
|
||||
|
||||
const firstSocket = getSocket();
|
||||
firstSocket.open();
|
||||
|
||||
await waitFor(() => {
|
||||
expect(getWsConnectionStatus()).toMatchObject({
|
||||
hasConnected: true,
|
||||
phase: "connected",
|
||||
});
|
||||
});
|
||||
|
||||
await transport.reconnect();
|
||||
|
||||
await waitFor(() => {
|
||||
expect(sockets).toHaveLength(2);
|
||||
});
|
||||
|
||||
const secondSocket = getSocket();
|
||||
expect(secondSocket).not.toBe(firstSocket);
|
||||
expect(firstSocket.readyState).toBe(MockWebSocket.CLOSED);
|
||||
|
||||
const requestPromise = transport.request((client) =>
|
||||
client[WS_METHODS.serverUpsertKeybinding]({
|
||||
command: "terminal.toggle",
|
||||
key: "ctrl+k",
|
||||
}),
|
||||
);
|
||||
|
||||
secondSocket.open();
|
||||
|
||||
await waitFor(() => {
|
||||
expect(secondSocket.sent).toHaveLength(1);
|
||||
});
|
||||
|
||||
const requestMessage = JSON.parse(secondSocket.sent[0] ?? "{}") as { id: string };
|
||||
secondSocket.serverMessage(
|
||||
JSON.stringify({
|
||||
_tag: "Exit",
|
||||
requestId: requestMessage.id,
|
||||
exit: {
|
||||
_tag: "Success",
|
||||
value: {
|
||||
keybindings: [],
|
||||
issues: [],
|
||||
},
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
await expect(requestPromise).resolves.toEqual({
|
||||
keybindings: [],
|
||||
issues: [],
|
||||
});
|
||||
|
||||
await transport.dispose();
|
||||
});
|
||||
|
||||
it("marks unary requests as slow until the first server ack arrives", async () => {
|
||||
const transport = new WsTransport("ws://localhost:3020");
|
||||
|
||||
const requestPromise = transport.request((client) =>
|
||||
client[WS_METHODS.serverUpsertKeybinding]({
|
||||
command: "terminal.toggle",
|
||||
key: "ctrl+k",
|
||||
}),
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(sockets).toHaveLength(1);
|
||||
});
|
||||
|
||||
const socket = getSocket();
|
||||
socket.open();
|
||||
|
||||
await waitFor(() => {
|
||||
expect(socket.sent).toHaveLength(1);
|
||||
});
|
||||
|
||||
const requestMessage = JSON.parse(socket.sent[0] ?? "{}") as { id: string };
|
||||
await waitFor(() => {
|
||||
expect(getSlowRpcAckRequests()).toMatchObject([
|
||||
{
|
||||
requestId: requestMessage.id,
|
||||
tag: WS_METHODS.serverUpsertKeybinding,
|
||||
},
|
||||
]);
|
||||
}, 5_000);
|
||||
|
||||
socket.serverMessage(
|
||||
JSON.stringify({
|
||||
_tag: "Exit",
|
||||
requestId: requestMessage.id,
|
||||
exit: {
|
||||
_tag: "Success",
|
||||
value: {
|
||||
keybindings: [],
|
||||
issues: [],
|
||||
},
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
await expect(requestPromise).resolves.toEqual({
|
||||
keybindings: [],
|
||||
issues: [],
|
||||
});
|
||||
expect(getSlowRpcAckRequests()).toEqual([]);
|
||||
|
||||
await transport.dispose();
|
||||
}, 10_000);
|
||||
|
||||
it("sends unary RPC requests and resolves successful exits", async () => {
|
||||
const transport = new WsTransport("ws://localhost:3020");
|
||||
|
||||
@@ -257,10 +454,12 @@ describe("WsTransport", () => {
|
||||
it("re-subscribes stream listeners after the stream exits", async () => {
|
||||
const transport = new WsTransport("ws://localhost:3020");
|
||||
const listener = vi.fn();
|
||||
const onResubscribe = vi.fn();
|
||||
|
||||
const unsubscribe = transport.subscribe(
|
||||
(client) => client[WS_METHODS.subscribeServerLifecycle]({}),
|
||||
listener,
|
||||
{ onResubscribe },
|
||||
);
|
||||
await waitFor(() => {
|
||||
expect(sockets).toHaveLength(1);
|
||||
@@ -308,6 +507,7 @@ describe("WsTransport", () => {
|
||||
.find((message) => message._tag === "Request" && message.id !== firstRequest.id);
|
||||
expect(nextRequest).toBeDefined();
|
||||
});
|
||||
expect(onResubscribe).toHaveBeenCalledOnce();
|
||||
|
||||
const secondRequest = socket.sent
|
||||
.map((message) => JSON.parse(message) as { _tag?: string; id?: string; tag?: string })
|
||||
@@ -346,6 +546,52 @@ describe("WsTransport", () => {
|
||||
await transport.dispose();
|
||||
});
|
||||
|
||||
it("does not fire onResubscribe when the first stream attempt exits before any value", async () => {
|
||||
const transport = new WsTransport("ws://localhost:3020");
|
||||
const listener = vi.fn();
|
||||
const onResubscribe = vi.fn();
|
||||
|
||||
const unsubscribe = transport.subscribe(
|
||||
(client) => client[WS_METHODS.subscribeServerLifecycle]({}),
|
||||
listener,
|
||||
{ onResubscribe },
|
||||
);
|
||||
await waitFor(() => {
|
||||
expect(sockets).toHaveLength(1);
|
||||
});
|
||||
|
||||
const socket = getSocket();
|
||||
socket.open();
|
||||
|
||||
await waitFor(() => {
|
||||
expect(socket.sent).toHaveLength(1);
|
||||
});
|
||||
|
||||
const firstRequest = JSON.parse(socket.sent[0] ?? "{}") as { id: string };
|
||||
socket.serverMessage(
|
||||
JSON.stringify({
|
||||
_tag: "Exit",
|
||||
requestId: firstRequest.id,
|
||||
exit: {
|
||||
_tag: "Success",
|
||||
value: null,
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
const nextRequest = socket.sent
|
||||
.map((message) => JSON.parse(message) as { _tag?: string; id?: string })
|
||||
.find((message) => message._tag === "Request" && message.id !== firstRequest.id);
|
||||
expect(nextRequest).toBeDefined();
|
||||
});
|
||||
expect(onResubscribe).not.toHaveBeenCalled();
|
||||
expect(listener).not.toHaveBeenCalled();
|
||||
|
||||
unsubscribe();
|
||||
await transport.dispose();
|
||||
});
|
||||
|
||||
it("streams finite request events without re-subscribing", async () => {
|
||||
const transport = new WsTransport("ws://localhost:3020");
|
||||
const listener = vi.fn();
|
||||
@@ -429,11 +675,21 @@ describe("WsTransport", () => {
|
||||
};
|
||||
const transport = {
|
||||
disposed: false,
|
||||
clientScope: {} as never,
|
||||
runtime,
|
||||
session: {
|
||||
clientScope: {} as never,
|
||||
runtime,
|
||||
},
|
||||
closeSession: (
|
||||
WsTransport.prototype as unknown as {
|
||||
closeSession: (session: {
|
||||
clientScope: unknown;
|
||||
runtime: { dispose: () => Promise<void>; runPromise: () => Promise<void> };
|
||||
}) => Promise<void>;
|
||||
}
|
||||
).closeSession,
|
||||
} as unknown as WsTransport;
|
||||
|
||||
WsTransport.prototype.dispose.call(transport);
|
||||
void WsTransport.prototype.dispose.call(transport);
|
||||
|
||||
expect(runtime.runPromise).toHaveBeenCalledTimes(1);
|
||||
expect(runtime.dispose).not.toHaveBeenCalled();
|
||||
|
||||
+171
-45
@@ -1,15 +1,26 @@
|
||||
import { Duration, Effect, Exit, Layer, ManagedRuntime, Option, Scope, Stream } from "effect";
|
||||
import {
|
||||
Cause,
|
||||
Duration,
|
||||
Effect,
|
||||
Exit,
|
||||
Layer,
|
||||
ManagedRuntime,
|
||||
Option,
|
||||
Scope,
|
||||
Stream,
|
||||
} from "effect";
|
||||
import { RpcClient } from "effect/unstable/rpc";
|
||||
|
||||
import { ClientTracingLive, configureClientTracing } from "./observability/clientTracing";
|
||||
import {
|
||||
createWsRpcProtocolLayer,
|
||||
makeWsRpcProtocolClient,
|
||||
type WsRpcProtocolClient,
|
||||
} from "./rpc/protocol";
|
||||
import { RpcClient } from "effect/unstable/rpc";
|
||||
import { ClientTracingLive, configureClientTracing } from "./observability/clientTracing";
|
||||
|
||||
interface SubscribeOptions {
|
||||
readonly retryDelay?: Duration.Input;
|
||||
readonly onResubscribe?: () => void;
|
||||
}
|
||||
|
||||
interface RequestOptions {
|
||||
@@ -17,6 +28,13 @@ interface RequestOptions {
|
||||
}
|
||||
|
||||
const DEFAULT_SUBSCRIPTION_RETRY_DELAY_MS = Duration.millis(250);
|
||||
const NOOP: () => void = () => undefined;
|
||||
|
||||
interface TransportSession {
|
||||
readonly clientPromise: Promise<WsRpcProtocolClient>;
|
||||
readonly clientScope: Scope.Closeable;
|
||||
readonly runtime: ManagedRuntime.ManagedRuntime<RpcClient.Protocol, never>;
|
||||
}
|
||||
|
||||
function formatErrorMessage(error: unknown): string {
|
||||
if (error instanceof Error && error.message.trim().length > 0) {
|
||||
@@ -26,21 +44,16 @@ function formatErrorMessage(error: unknown): string {
|
||||
}
|
||||
|
||||
export class WsTransport {
|
||||
private readonly runtime: ManagedRuntime.ManagedRuntime<RpcClient.Protocol, never>;
|
||||
private readonly clientScope: Scope.Closeable;
|
||||
private readonly clientPromise: Promise<WsRpcProtocolClient>;
|
||||
private readonly tracingReady: Promise<void>;
|
||||
private readonly url: string | undefined;
|
||||
private disposed = false;
|
||||
private reconnectChain: Promise<void> = Promise.resolve();
|
||||
private session: TransportSession;
|
||||
|
||||
constructor(url?: string) {
|
||||
this.url = url;
|
||||
this.tracingReady = configureClientTracing();
|
||||
this.runtime = ManagedRuntime.make(
|
||||
Layer.mergeAll(createWsRpcProtocolLayer(url), ClientTracingLive),
|
||||
);
|
||||
this.clientScope = this.runtime.runSync(Scope.make());
|
||||
this.clientPromise = this.runtime.runPromise(
|
||||
Scope.provide(this.clientScope)(makeWsRpcProtocolClient),
|
||||
);
|
||||
this.session = this.createSession();
|
||||
}
|
||||
|
||||
async request<TSuccess>(
|
||||
@@ -52,8 +65,9 @@ export class WsTransport {
|
||||
}
|
||||
|
||||
await this.tracingReady;
|
||||
const client = await this.clientPromise;
|
||||
return await this.runtime.runPromise(Effect.suspend(() => execute(client)));
|
||||
const session = this.session;
|
||||
const client = await session.clientPromise;
|
||||
return await session.runtime.runPromise(Effect.suspend(() => execute(client)));
|
||||
}
|
||||
|
||||
async requestStream<TValue>(
|
||||
@@ -65,8 +79,9 @@ export class WsTransport {
|
||||
}
|
||||
|
||||
await this.tracingReady;
|
||||
const client = await this.clientPromise;
|
||||
await this.runtime.runPromise(
|
||||
const session = this.session;
|
||||
const client = await session.clientPromise;
|
||||
await session.runtime.runPromise(
|
||||
Stream.runForEach(connect(client), (value) =>
|
||||
Effect.sync(() => {
|
||||
try {
|
||||
@@ -89,16 +104,132 @@ export class WsTransport {
|
||||
}
|
||||
|
||||
let active = true;
|
||||
const retryDelayMs = options?.retryDelay ?? DEFAULT_SUBSCRIPTION_RETRY_DELAY_MS;
|
||||
const cancel = this.runtime.runCallback(
|
||||
let hasReceivedValue = false;
|
||||
const retryDelayMs = Duration.toMillis(
|
||||
Duration.fromInputUnsafe(options?.retryDelay ?? DEFAULT_SUBSCRIPTION_RETRY_DELAY_MS),
|
||||
);
|
||||
let cancelCurrentStream: () => void = NOOP;
|
||||
|
||||
void (async () => {
|
||||
for (;;) {
|
||||
if (!active || this.disposed) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
if (hasReceivedValue) {
|
||||
try {
|
||||
options?.onResubscribe?.();
|
||||
} catch {
|
||||
// Swallow reconnect hook errors so the stream can recover.
|
||||
}
|
||||
}
|
||||
|
||||
const session = this.session;
|
||||
const runningStream = this.runStreamOnSession(
|
||||
session,
|
||||
connect,
|
||||
listener,
|
||||
() => active,
|
||||
() => {
|
||||
hasReceivedValue = true;
|
||||
},
|
||||
);
|
||||
cancelCurrentStream = runningStream.cancel;
|
||||
await runningStream.completed;
|
||||
cancelCurrentStream = NOOP;
|
||||
} catch (error) {
|
||||
cancelCurrentStream = NOOP;
|
||||
if (!active || this.disposed) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.warn("WebSocket RPC subscription disconnected", {
|
||||
error: formatErrorMessage(error),
|
||||
});
|
||||
await sleep(retryDelayMs);
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
||||
return () => {
|
||||
active = false;
|
||||
cancelCurrentStream();
|
||||
};
|
||||
}
|
||||
|
||||
async reconnect() {
|
||||
if (this.disposed) {
|
||||
throw new Error("Transport disposed");
|
||||
}
|
||||
|
||||
const reconnectOperation = this.reconnectChain.then(async () => {
|
||||
if (this.disposed) {
|
||||
throw new Error("Transport disposed");
|
||||
}
|
||||
|
||||
const previousSession = this.session;
|
||||
this.session = this.createSession();
|
||||
await this.closeSession(previousSession);
|
||||
});
|
||||
|
||||
this.reconnectChain = reconnectOperation.catch(() => undefined);
|
||||
await reconnectOperation;
|
||||
}
|
||||
|
||||
async dispose() {
|
||||
if (this.disposed) {
|
||||
return;
|
||||
}
|
||||
this.disposed = true;
|
||||
await this.closeSession(this.session);
|
||||
}
|
||||
|
||||
private closeSession(session: TransportSession) {
|
||||
return session.runtime.runPromise(Scope.close(session.clientScope, Exit.void)).finally(() => {
|
||||
session.runtime.dispose();
|
||||
});
|
||||
}
|
||||
|
||||
private createSession(): TransportSession {
|
||||
const runtime = ManagedRuntime.make(
|
||||
Layer.mergeAll(createWsRpcProtocolLayer(this.url), ClientTracingLive),
|
||||
);
|
||||
const clientScope = runtime.runSync(Scope.make());
|
||||
return {
|
||||
runtime,
|
||||
clientScope,
|
||||
clientPromise: runtime.runPromise(Scope.provide(clientScope)(makeWsRpcProtocolClient)),
|
||||
};
|
||||
}
|
||||
|
||||
private runStreamOnSession<TValue>(
|
||||
session: TransportSession,
|
||||
connect: (client: WsRpcProtocolClient) => Stream.Stream<TValue, Error, never>,
|
||||
listener: (value: TValue) => void,
|
||||
isActive: () => boolean,
|
||||
markValueReceived: () => void,
|
||||
): {
|
||||
readonly cancel: () => void;
|
||||
readonly completed: Promise<void>;
|
||||
} {
|
||||
let resolveCompleted!: () => void;
|
||||
let rejectCompleted!: (error: unknown) => void;
|
||||
const completed = new Promise<void>((resolve, reject) => {
|
||||
resolveCompleted = resolve;
|
||||
rejectCompleted = reject;
|
||||
});
|
||||
const cancel = session.runtime.runCallback(
|
||||
Effect.promise(() => this.tracingReady).pipe(
|
||||
Effect.flatMap(() => Effect.promise(() => this.clientPromise)),
|
||||
Effect.flatMap(() => Effect.promise(() => session.clientPromise)),
|
||||
Effect.flatMap((client) =>
|
||||
Stream.runForEach(connect(client), (value) =>
|
||||
Effect.sync(() => {
|
||||
if (!active) {
|
||||
if (!isActive()) {
|
||||
return;
|
||||
}
|
||||
|
||||
markValueReceived();
|
||||
try {
|
||||
listener(value);
|
||||
} catch {
|
||||
@@ -107,33 +238,28 @@ export class WsTransport {
|
||||
}),
|
||||
),
|
||||
),
|
||||
Effect.catch((error) => {
|
||||
if (!active || this.disposed) {
|
||||
return Effect.interrupt;
|
||||
}
|
||||
return Effect.sync(() => {
|
||||
console.warn("WebSocket RPC subscription disconnected", {
|
||||
error: formatErrorMessage(error),
|
||||
});
|
||||
}).pipe(Effect.andThen(Effect.sleep(retryDelayMs)));
|
||||
}),
|
||||
Effect.forever,
|
||||
),
|
||||
{
|
||||
onExit: (exit) => {
|
||||
if (Exit.isSuccess(exit)) {
|
||||
resolveCompleted();
|
||||
return;
|
||||
}
|
||||
|
||||
rejectCompleted(Cause.squash(exit.cause));
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
return () => {
|
||||
active = false;
|
||||
cancel();
|
||||
return {
|
||||
cancel,
|
||||
completed,
|
||||
};
|
||||
}
|
||||
|
||||
async dispose() {
|
||||
if (this.disposed) {
|
||||
return;
|
||||
}
|
||||
this.disposed = true;
|
||||
await this.runtime.runPromise(Scope.close(this.clientScope, Exit.void)).finally(() => {
|
||||
this.runtime.dispose();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function sleep(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(resolve, ms);
|
||||
});
|
||||
}
|
||||
@@ -180,6 +180,11 @@ export interface NativeApi {
|
||||
input: OrchestrationGetFullThreadDiffInput,
|
||||
) => Promise<OrchestrationGetFullThreadDiffResult>;
|
||||
replayEvents: (fromSequenceExclusive: number) => Promise<OrchestrationEvent[]>;
|
||||
onDomainEvent: (callback: (event: OrchestrationEvent) => void) => () => void;
|
||||
onDomainEvent: (
|
||||
callback: (event: OrchestrationEvent) => void,
|
||||
options?: {
|
||||
onResubscribe?: () => void;
|
||||
},
|
||||
) => () => void;
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user