Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion apps/web/src/components/ChatView.logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions apps/web/src/components/ChatView.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down
25 changes: 24 additions & 1 deletion apps/web/src/components/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ import {
createLocalDispatchSnapshot,
deriveComposerSendState,
dismissBranchMismatchForSession,
hasEnvironmentReconnectWarningGraceElapsed,
scheduleEnvironmentReconnectWarning,
hasServerAcknowledgedLocalDispatch,
isBranchMismatchDismissedForSession,
shouldShowBranchMismatchBanner,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -2093,6 +2115,7 @@ function ChatViewContent(props: ChatViewProps) {
return items;
}, [
activeEnvironmentUnavailableState,
reconnectWarningGraceElapsed,
handleReconnectActiveEnvironment,
navigate,
setDismissedVersionMismatchKey,
Expand Down
Loading