fix(web): delay transient reconnect warnings (#5670)

Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
This commit is contained in:
t3-code[bot] authored and GitHub committed 2026-08-07 23:17:18 -04:00
1 parent 4eaf5ef8bb
commit ed886fe181
3 files changed
+77 -2

No files matched your search

+40 -1
View File
@@ -6,7 +6,7 @@ import {
ThreadId,
TurnId,
} from "@t3tools/contracts";
import { describe, expect, it } from "vite-plus/test";
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
import type { Thread, ThreadShell } from "../types";
import {
@@ -19,13 +19,16 @@ import {
createLocalDispatchSnapshot,
deriveComposerSendState,
dismissBranchMismatchForSession,
ENVIRONMENT_RECONNECT_WARNING_GRACE_MS,
getStartedThreadModelChangeBlockReason,
hasEnvironmentReconnectWarningGraceElapsed,
hasServerAcknowledgedLocalDispatch,
isBranchMismatchDismissedForSession,
reconcileMountedTerminalThreadIds,
reconcileRetainedMountedThreadIds,
resolveThreadMetadataUpdateForNextTurn,
resolveSendEnvMode,
scheduleEnvironmentReconnectWarning,
startNewThreadForProject,
shouldShowBranchMismatchBanner,
shouldWriteThreadErrorToCurrentServerThread,
@@ -36,6 +39,42 @@ const projectId = ProjectId.make("project-1");
const threadId = ThreadId.make("thread-1");
const now = "2026-03-29T00:00:00.000Z";
describe("environment reconnect warning grace", () => {
afterEach(() => vi.useRealTimers());
it("shows a persistent reconnect after the grace period", () => {
vi.useFakeTimers();
const showWarning = vi.fn();
scheduleEnvironmentReconnectWarning(showWarning);
vi.advanceTimersByTime(ENVIRONMENT_RECONNECT_WARNING_GRACE_MS - 1);
expect(showWarning).not.toHaveBeenCalled();
vi.advanceTimersByTime(1);
expect(showWarning).toHaveBeenCalledOnce();
});
it("cancels the warning when the connection recovers during the grace period", () => {
vi.useFakeTimers();
const showWarning = vi.fn();
const cancel = scheduleEnvironmentReconnectWarning(showWarning);
cancel();
vi.advanceTimersByTime(ENVIRONMENT_RECONNECT_WARNING_GRACE_MS);
expect(showWarning).not.toHaveBeenCalled();
});
it("does not reuse elapsed grace from another environment", () => {
const anotherEnvironmentId = EnvironmentId.make("environment-remote");
expect(hasEnvironmentReconnectWarningGraceElapsed(environmentId, environmentId)).toBe(true);
expect(hasEnvironmentReconnectWarningGraceElapsed(anotherEnvironmentId, environmentId)).toBe(
false,
);
});
});
function makeThread(overrides: Partial<Thread> = {}): Thread {
return {
id: threadId,
+13
View File
@@ -25,9 +25,22 @@ import type { DraftThreadEnvMode } from "../composerDraftStore";
export const LAST_INVOKED_SCRIPT_BY_PROJECT_KEY = "t3code:last-invoked-script-by-project";
export const MAX_HIDDEN_MOUNTED_TERMINAL_THREADS = 10;
export const MAX_HIDDEN_MOUNTED_PREVIEW_THREADS = 3;
export const ENVIRONMENT_RECONNECT_WARNING_GRACE_MS = 2_000;
export const LastInvokedScriptByProjectSchema = Schema.Record(ProjectId, Schema.String);
export function scheduleEnvironmentReconnectWarning(showWarning: () => void): () => void {
const timeoutId = globalThis.setTimeout(showWarning, ENVIRONMENT_RECONNECT_WARNING_GRACE_MS);
return () => globalThis.clearTimeout(timeoutId);
}
export function hasEnvironmentReconnectWarningGraceElapsed(
activeEnvironmentId: EnvironmentId | null,
elapsedEnvironmentId: EnvironmentId | null,
): boolean {
return activeEnvironmentId !== null && activeEnvironmentId === elapsedEnvironmentId;
}
export function startNewThreadForProject(
projectRef: ScopedProjectRef | null,
handleNewThread: (projectRef: ScopedProjectRef) => Promise<void>,
+24 -1
View File
@@ -283,6 +283,8 @@ import {
createLocalDispatchSnapshot,
deriveComposerSendState,
dismissBranchMismatchForSession,
hasEnvironmentReconnectWarningGraceElapsed,
scheduleEnvironmentReconnectWarning,
hasServerAcknowledgedLocalDispatch,
isBranchMismatchDismissedForSession,
shouldShowBranchMismatchBanner,
@@ -1733,6 +1735,24 @@ function ChatViewContent(props: ChatViewProps) {
const activeEnvironmentConnectionPhase = activeEnvironment?.connection.phase ?? "available";
const activeEnvironmentUnavailable =
activeEnvironment !== null && activeEnvironmentConnectionPhase !== "connected";
const activeReconnectingEnvironmentId =
activeEnvironmentConnectionPhase === "connecting" ||
activeEnvironmentConnectionPhase === "reconnecting"
? (activeEnvironment?.environmentId ?? null)
: null;
const [reconnectWarningGraceElapsedEnvironmentId, setReconnectWarningGraceElapsedEnvironmentId] =
useState<EnvironmentId | null>(null);
const reconnectWarningGraceElapsed = hasEnvironmentReconnectWarningGraceElapsed(
activeReconnectingEnvironmentId,
reconnectWarningGraceElapsedEnvironmentId,
);
useEffect(() => {
setReconnectWarningGraceElapsedEnvironmentId(null);
if (activeReconnectingEnvironmentId === null) return;
return scheduleEnvironmentReconnectWarning(() =>
setReconnectWarningGraceElapsedEnvironmentId(activeReconnectingEnvironmentId),
);
}, [activeReconnectingEnvironmentId]);
const activeEnvironmentUnavailableLabel = activeEnvironment?.label ?? null;
const activeEnvironmentUnavailableState = useMemo<EnvironmentUnavailableState | null>(() => {
if (!activeEnvironmentUnavailable || !activeEnvironmentUnavailableLabel || !activeEnvironment) {
@@ -1965,7 +1985,9 @@ function ChatViewContent(props: ChatViewProps) {
// While an update runs, transient connect blips are expected (the server
// restarts) and the update banner already shows progress. Hard failure
// phases still surface so the Reconnect action stays reachable.
const suppressUnavailableBanner = updateRunning && environmentReconnecting;
const suppressUnavailableBanner =
environmentReconnecting &&
(updateRunning || (!reconnectingThroughVersionSkew && !reconnectWarningGraceElapsed));
if (activeEnvironmentUnavailableState && unavailableConnection && !suppressUnavailableBanner) {
if (reconnectingThroughVersionSkew) {
items.push({
@@ -2093,6 +2115,7 @@ function ChatViewContent(props: ChatViewProps) {
return items;
}, [
activeEnvironmentUnavailableState,
reconnectWarningGraceElapsed,
handleReconnectActiveEnvironment,
navigate,
setDismissedVersionMismatchKey,