Skip to content
TypeScript

fix(web): confirm closing agent-controlled browsers (#9272)

maria28ddaf759171
diff --git a/apps/web/src/components/ChatView.logic.test.ts b/apps/web/src/components/ChatView.logic.test.ts
index e23febd1e07..290e3435abc 100644
--- a/apps/web/src/components/ChatView.logic.test.ts
+++ b/apps/web/src/components/ChatView.logic.test.ts
@@ -10,9 +10,11 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"
import type { Thread, ThreadShell } from "../types";
import type { CodexArtifactTemplate } from "@t3tools/client-runtime/codex-artifact-templates";
+import type { RightPanelSurface } from "../rightPanelStore";
import {
MAX_HIDDEN_MOUNTED_PREVIEW_THREADS,
MAX_HIDDEN_MOUNTED_TERMINAL_THREADS,
+ agentControlledBrowserCloseConfirmation,
branchMismatchKey,
buildExpiredTerminalContextToastCopy,
buildLoadingThreadFromShell,
@@ -44,6 +46,43 @@ import {
toolGroupConsumesUpwardNavigation,
} from "./ChatView.logic";
+describe("agent browser close confirmation", () => {
+ const surfaces = [
+ { id: "browser:one", kind: "preview", resourceId: "tab-1" },
+ { id: "browser:two", kind: "preview", resourceId: "tab-2" },
+ { id: "diff", kind: "diff" },
+ ] satisfies RightPanelSurface[];
+
+ it("only warns for browsers under active agent control", () => {
+ expect(
+ agentControlledBrowserCloseConfirmation(surfaces, {
+ "tab-1": { controller: "none" },
+ "tab-2": { controller: "human" },
+ }),
+ ).toBeNull();
+
+ expect(
+ agentControlledBrowserCloseConfirmation([surfaces[0]!], {
+ "tab-1": { controller: "agent" },
+ }),
+ ).toBe(
+ [
+ "Close browser while the agent is using it?",
+ "The agent is actively controlling this browser. Closing it may interrupt the current browser action.",
+ ].join("\n"),
+ );
+ });
+
+ it("counts every agent-controlled browser in a bulk close", () => {
+ expect(
+ agentControlledBrowserCloseConfirmation(surfaces, {
+ "tab-1": { controller: "agent" },
+ "tab-2": { controller: "agent" },
+ }),
+ ).toContain("Close 2 browsers");
+ });
+});
+
describe("isVideoPreviewRequestCurrent", () => {
it("rejects changed threads and replaced previews", () => {
expect(isVideoPreviewRequestCurrent("thread-1", "thread-2", 1, 1)).toBe(false);
diff --git a/apps/web/src/components/ChatView.logic.ts b/apps/web/src/components/ChatView.logic.ts
index 20620b65e62..e9a18d01687 100644
--- a/apps/web/src/components/ChatView.logic.ts
+++ b/apps/web/src/components/ChatView.logic.ts
@@ -45,6 +45,8 @@ import {
import type { DraftThreadEnvMode } from "../composerDraftStore";
import type { ComposerSubmissionIntent } from "../composer-logic";
import type { TimelineEntry } from "../session-logic";
+import type { DesktopPreviewOverlay } from "../previewStateStore";
+import type { RightPanelSurface } from "../rightPanelStore";
export const LAST_INVOKED_SCRIPT_BY_PROJECT_KEY = "t3code:last-invoked-script-by-project";
export const MAX_HIDDEN_MOUNTED_TERMINAL_THREADS = 10;
@@ -53,6 +55,29 @@ export const ENVIRONMENT_RECONNECT_WARNING_GRACE_MS = 2_000;
export const LastInvokedScriptByProjectSchema = Schema.Record(ProjectId, Schema.String);
+export function agentControlledBrowserCloseConfirmation(
+ surfaces: readonly RightPanelSurface[],
+ desktopByTabId: Readonly<Record<string, Pick<DesktopPreviewOverlay, "controller"> | undefined>>,
+): string | null {
+ const activeBrowserCount = surfaces.filter(
+ (surface) =>
+ surface.kind === "preview" &&
+ surface.resourceId !== null &&
+ desktopByTabId[surface.resourceId]?.controller === "agent",
+ ).length;
+ if (activeBrowserCount === 0) return null;
+ if (activeBrowserCount === 1) {
+ return [
+ "Close browser while the agent is using it?",
+ "The agent is actively controlling this browser. Closing it may interrupt the current browser action.",
+ ].join("\n");
+ }
+ return [
+ `Close ${activeBrowserCount} browsers while the agent is using them?`,
+ "The agent is actively controlling these browsers. Closing them may interrupt the current browser actions.",
+ ].join("\n");
+}
+
export function codexArtifactTemplatePromptToAppend(
currentDraft: string,
template: CodexArtifactTemplate,
diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx
index 86843444691..305773955d0 100644
--- a/apps/web/src/components/ChatView.tsx
+++ b/apps/web/src/components/ChatView.tsx
@@ -343,6 +343,7 @@ import {
} from "./chat/draftHeroTransition";
import {
MAX_HIDDEN_MOUNTED_TERMINAL_THREADS,
+ agentControlledBrowserCloseConfirmation,
branchMismatchKey,
buildExpiredTerminalContextToastCopy,
buildLocalDraftThread,
@@ -3872,6 +3873,27 @@ function ChatViewContent(props: ChatViewProps) {
storeCloseTerminal,
],
);
+ const closeAfterAgentBrowserConfirmation = useCallback(
+ (surfaces: readonly RightPanelSurface[], closeSurfaces: () => void) => {
+ const message = agentControlledBrowserCloseConfirmation(
+ surfaces,
+ activePreviewState.desktopByTabId,
+ );
+ if (!message) {
+ closeSurfaces();
+ return;
+ }
+ const localApi = readLocalApi();
+ if (!localApi) return;
+ void localApi.dialogs.confirm(message, { variant: "destructive" }).then(
+ (confirmed) => {
+ if (confirmed) closeSurfaces();
+ },
+ () => undefined,
+ );
+ },
+ [activePreviewState.desktopByTabId],
+ );
const syncActivePreviewSurface = useCallback(() => {
if (!activeThreadRef) return;
const nextActiveSurface = selectActiveRightPanelSurface(
@@ -3882,14 +3904,26 @@ function ChatViewContent(props: ChatViewProps) {
setActivePreviewTab(activeThreadRef, nextActiveSurface.resourceId);
}
}, [activeThreadRef]);
+ const finishRightPanelSurfaceClose = useCallback(
+ (surfaces: readonly RightPanelSurface[]) => {
+ if (!activeThreadRef) return;
+ cleanupRightPanelSurfaces(surfaces);
+ const store = useRightPanelStore.getState();
+ for (const surface of surfaces) {
+ store.closeSurface(activeThreadRef, surface.id);
+ }
+ syncActivePreviewSurface();
+ },
+ [activeThreadRef, cleanupRightPanelSurfaces, syncActivePreviewSurface],
+ );
const closeRightPanelSurface = useCallback(
(surface: RightPanelSurface) => {
if (!activeThreadRef) return;
- const finishClose = () => {
- cleanupRightPanelSurfaces([surface]);
- useRightPanelStore.getState().closeSurface(activeThreadRef, surface.id);
- syncActivePreviewSurface();
- };
+ const finishClose = () => finishRightPanelSurfaceClose([surface]);
+ if (surface.kind === "preview") {
+ closeAfterAgentBrowserConfirmation([surface], finishClose);
+ return;
+ }
if (surface.kind !== "terminal") {
finishClose();
return;
@@ -3909,23 +3943,22 @@ function ChatViewContent(props: ChatViewProps) {
[
activeThreadRef,
activeTerminalLabelsById,
- cleanupRightPanelSurfaces,
- syncActivePreviewSurface,
+ closeAfterAgentBrowserConfirmation,
+ finishRightPanelSurfaceClose,
],
);
const closeOtherRightPanelSurfaces = useCallback(
(surface: RightPanelSurface) => {
if (!activeThreadRef) return;
const surfaces = rightPanelState.surfaces.filter((entry) => entry.id !== surface.id);
- cleanupRightPanelSurfaces(surfaces);
- useRightPanelStore.getState().closeOtherSurfaces(activeThreadRef, surface.id);
- syncActivePreviewSurface();
+ const finishClose = () => finishRightPanelSurfaceClose(surfaces);
+ closeAfterAgentBrowserConfirmation(surfaces, finishClose);
},
[
activeThreadRef,
- cleanupRightPanelSurfaces,
+ closeAfterAgentBrowserConfirmation,
+ finishRightPanelSurfaceClose,
rightPanelState.surfaces,
- syncActivePreviewSurface,
],
);
const closeRightPanelSurfacesToRight = useCallback(
@@ -3934,22 +3967,26 @@ function ChatViewContent(props: ChatViewProps) {
const surfaceIndex = rightPanelState.surfaces.findIndex((entry) => entry.id === surface.id);
if (surfaceIndex < 0) return;
const surfaces = rightPanelState.surfaces.slice(surfaceIndex + 1);
- cleanupRightPanelSurfaces(surfaces);
- useRightPanelStore.getState().closeSurfacesToRight(activeThreadRef, surface.id);
- syncActivePreviewSurface();
+ const finishClose = () => finishRightPanelSurfaceClose(surfaces);
+ closeAfterAgentBrowserConfirmation(surfaces, finishClose);
},
[
activeThreadRef,
- cleanupRightPanelSurfaces,
+ closeAfterAgentBrowserConfirmation,
+ finishRightPanelSurfaceClose,
rightPanelState.surfaces,
- syncActivePreviewSurface,
],
);
const closeAllRightPanelSurfaces = useCallback(() => {
if (!activeThreadRef) return;
- cleanupRightPanelSurfaces(rightPanelState.surfaces);
- useRightPanelStore.getState().closeAllSurfaces(activeThreadRef);
- }, [activeThreadRef, cleanupRightPanelSurfaces, rightPanelState.surfaces]);
+ const finishClose = () => finishRightPanelSurfaceClose(rightPanelState.surfaces);
+ closeAfterAgentBrowserConfirmation(rightPanelState.surfaces, finishClose);
+ }, [
+ activeThreadRef,
+ closeAfterAgentBrowserConfirmation,
+ finishRightPanelSurfaceClose,
+ rightPanelState.surfaces,
+ ]);
const copyRightPanelFilePath = useCallback((relativePath: string) => {
if (typeof window === "undefined" || !navigator.clipboard?.writeText) {
toastManager.add(