@devries-cameron20 / t3code Mirror · read-only / commits / 28ddaf7591
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 24, 12:45 UTC

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

Co-authored-by: maria-rcks <254055478+maria-rcks@users.noreply.github.com>

maria committed Sep 2, 2026 at 17:26 UTC 28ddaf75917140e5e4355d4386bc5d14d9dad7b6
3 files changed +121 -20
apps/web/src/components/ChatView.logic.test.ts
+39
@@ -10,9 +10,11 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"
10
11 import type { Thread, ThreadShell } from "../types";
12 import type { CodexArtifactTemplate } from "@t3tools/client-runtime/codex-artifact-templates";
13 +import type { RightPanelSurface } from "../rightPanelStore";
14 import {
15 MAX_HIDDEN_MOUNTED_PREVIEW_THREADS,
16 MAX_HIDDEN_MOUNTED_TERMINAL_THREADS,
17 + agentControlledBrowserCloseConfirmation,
18 branchMismatchKey,
19 buildExpiredTerminalContextToastCopy,
20 buildLoadingThreadFromShell,
@@ -44,6 +46,43 @@ import {
46 toolGroupConsumesUpwardNavigation,
47 } from "./ChatView.logic";
48
49 +describe("agent browser close confirmation", () => {
50 + const surfaces = [
51 + { id: "browser:one", kind: "preview", resourceId: "tab-1" },
52 + { id: "browser:two", kind: "preview", resourceId: "tab-2" },
53 + { id: "diff", kind: "diff" },
54 + ] satisfies RightPanelSurface[];
55 +
56 + it("only warns for browsers under active agent control", () => {
57 + expect(
58 + agentControlledBrowserCloseConfirmation(surfaces, {
59 + "tab-1": { controller: "none" },
60 + "tab-2": { controller: "human" },
61 + }),
62 + ).toBeNull();
63 +
64 + expect(
65 + agentControlledBrowserCloseConfirmation([surfaces[0]!], {
66 + "tab-1": { controller: "agent" },
67 + }),
68 + ).toBe(
69 + [
70 + "Close browser while the agent is using it?",
71 + "The agent is actively controlling this browser. Closing it may interrupt the current browser action.",
72 + ].join("\n"),
73 + );
74 + });
75 +
76 + it("counts every agent-controlled browser in a bulk close", () => {
77 + expect(
78 + agentControlledBrowserCloseConfirmation(surfaces, {
79 + "tab-1": { controller: "agent" },
80 + "tab-2": { controller: "agent" },
81 + }),
82 + ).toContain("Close 2 browsers");
83 + });
84 +});
85 +
86 describe("isVideoPreviewRequestCurrent", () => {
87 it("rejects changed threads and replaced previews", () => {
88 expect(isVideoPreviewRequestCurrent("thread-1", "thread-2", 1, 1)).toBe(false);
apps/web/src/components/ChatView.logic.ts
+25
@@ -45,6 +45,8 @@ import {
45 import type { DraftThreadEnvMode } from "../composerDraftStore";
46 import type { ComposerSubmissionIntent } from "../composer-logic";
47 import type { TimelineEntry } from "../session-logic";
48 +import type { DesktopPreviewOverlay } from "../previewStateStore";
49 +import type { RightPanelSurface } from "../rightPanelStore";
50
51 export const LAST_INVOKED_SCRIPT_BY_PROJECT_KEY = "t3code:last-invoked-script-by-project";
52 export const MAX_HIDDEN_MOUNTED_TERMINAL_THREADS = 10;
@@ -53,6 +55,29 @@ export const ENVIRONMENT_RECONNECT_WARNING_GRACE_MS = 2_000;
55
56 export const LastInvokedScriptByProjectSchema = Schema.Record(ProjectId, Schema.String);
57
58 +export function agentControlledBrowserCloseConfirmation(
59 + surfaces: readonly RightPanelSurface[],
60 + desktopByTabId: Readonly<Record<string, Pick<DesktopPreviewOverlay, "controller"> | undefined>>,
61 +): string | null {
62 + const activeBrowserCount = surfaces.filter(
63 + (surface) =>
64 + surface.kind === "preview" &&
65 + surface.resourceId !== null &&
66 + desktopByTabId[surface.resourceId]?.controller === "agent",
67 + ).length;
68 + if (activeBrowserCount === 0) return null;
69 + if (activeBrowserCount === 1) {
70 + return [
71 + "Close browser while the agent is using it?",
72 + "The agent is actively controlling this browser. Closing it may interrupt the current browser action.",
73 + ].join("\n");
74 + }
75 + return [
76 + `Close ${activeBrowserCount} browsers while the agent is using them?`,
77 + "The agent is actively controlling these browsers. Closing them may interrupt the current browser actions.",
78 + ].join("\n");
79 +}
80 +
81 export function codexArtifactTemplatePromptToAppend(
82 currentDraft: string,
83 template: CodexArtifactTemplate,
apps/web/src/components/ChatView.tsx
+57 -20
@@ -343,6 +343,7 @@ import {
343 } from "./chat/draftHeroTransition";
344 import {
345 MAX_HIDDEN_MOUNTED_TERMINAL_THREADS,
346 + agentControlledBrowserCloseConfirmation,
347 branchMismatchKey,
348 buildExpiredTerminalContextToastCopy,
349 buildLocalDraftThread,
@@ -3872,6 +3873,27 @@ function ChatViewContent(props: ChatViewProps) {
3873 storeCloseTerminal,
3874 ],
3875 );
3876 + const closeAfterAgentBrowserConfirmation = useCallback(
3877 + (surfaces: readonly RightPanelSurface[], closeSurfaces: () => void) => {
3878 + const message = agentControlledBrowserCloseConfirmation(
3879 + surfaces,
3880 + activePreviewState.desktopByTabId,
3881 + );
3882 + if (!message) {
3883 + closeSurfaces();
3884 + return;
3885 + }
3886 + const localApi = readLocalApi();
3887 + if (!localApi) return;
3888 + void localApi.dialogs.confirm(message, { variant: "destructive" }).then(
3889 + (confirmed) => {
3890 + if (confirmed) closeSurfaces();
3891 + },
3892 + () => undefined,
3893 + );
3894 + },
3895 + [activePreviewState.desktopByTabId],
3896 + );
3897 const syncActivePreviewSurface = useCallback(() => {
3898 if (!activeThreadRef) return;
3899 const nextActiveSurface = selectActiveRightPanelSurface(
@@ -3882,14 +3904,26 @@ function ChatViewContent(props: ChatViewProps) {
3904 setActivePreviewTab(activeThreadRef, nextActiveSurface.resourceId);
3905 }
3906 }, [activeThreadRef]);
3907 + const finishRightPanelSurfaceClose = useCallback(
3908 + (surfaces: readonly RightPanelSurface[]) => {
3909 + if (!activeThreadRef) return;
3910 + cleanupRightPanelSurfaces(surfaces);
3911 + const store = useRightPanelStore.getState();
3912 + for (const surface of surfaces) {
3913 + store.closeSurface(activeThreadRef, surface.id);
3914 + }
3915 + syncActivePreviewSurface();
3916 + },
3917 + [activeThreadRef, cleanupRightPanelSurfaces, syncActivePreviewSurface],
3918 + );
3919 const closeRightPanelSurface = useCallback(
3920 (surface: RightPanelSurface) => {
3921 if (!activeThreadRef) return;
3888 - const finishClose = () => {
3889 - cleanupRightPanelSurfaces([surface]);
3890 - useRightPanelStore.getState().closeSurface(activeThreadRef, surface.id);
3891 - syncActivePreviewSurface();
3892 - };
3922 + const finishClose = () => finishRightPanelSurfaceClose([surface]);
3923 + if (surface.kind === "preview") {
3924 + closeAfterAgentBrowserConfirmation([surface], finishClose);
3925 + return;
3926 + }
3927 if (surface.kind !== "terminal") {
3928 finishClose();
3929 return;
@@ -3909,23 +3943,22 @@ function ChatViewContent(props: ChatViewProps) {
3943 [
3944 activeThreadRef,
3945 activeTerminalLabelsById,
3912 - cleanupRightPanelSurfaces,
3913 - syncActivePreviewSurface,
3946 + closeAfterAgentBrowserConfirmation,
3947 + finishRightPanelSurfaceClose,
3948 ],
3949 );
3950 const closeOtherRightPanelSurfaces = useCallback(
3951 (surface: RightPanelSurface) => {
3952 if (!activeThreadRef) return;
3953 const surfaces = rightPanelState.surfaces.filter((entry) => entry.id !== surface.id);
3920 - cleanupRightPanelSurfaces(surfaces);
3921 - useRightPanelStore.getState().closeOtherSurfaces(activeThreadRef, surface.id);
3922 - syncActivePreviewSurface();
3954 + const finishClose = () => finishRightPanelSurfaceClose(surfaces);
3955 + closeAfterAgentBrowserConfirmation(surfaces, finishClose);
3956 },
3957 [
3958 activeThreadRef,
3926 - cleanupRightPanelSurfaces,
3959 + closeAfterAgentBrowserConfirmation,
3960 + finishRightPanelSurfaceClose,
3961 rightPanelState.surfaces,
3928 - syncActivePreviewSurface,
3962 ],
3963 );
3964 const closeRightPanelSurfacesToRight = useCallback(
@@ -3934,22 +3967,26 @@ function ChatViewContent(props: ChatViewProps) {
3967 const surfaceIndex = rightPanelState.surfaces.findIndex((entry) => entry.id === surface.id);
3968 if (surfaceIndex < 0) return;
3969 const surfaces = rightPanelState.surfaces.slice(surfaceIndex + 1);
3937 - cleanupRightPanelSurfaces(surfaces);
3938 - useRightPanelStore.getState().closeSurfacesToRight(activeThreadRef, surface.id);
3939 - syncActivePreviewSurface();
3970 + const finishClose = () => finishRightPanelSurfaceClose(surfaces);
3971 + closeAfterAgentBrowserConfirmation(surfaces, finishClose);
3972 },
3973 [
3974 activeThreadRef,
3943 - cleanupRightPanelSurfaces,
3975 + closeAfterAgentBrowserConfirmation,
3976 + finishRightPanelSurfaceClose,
3977 rightPanelState.surfaces,
3945 - syncActivePreviewSurface,
3978 ],
3979 );
3980 const closeAllRightPanelSurfaces = useCallback(() => {
3981 if (!activeThreadRef) return;
3950 - cleanupRightPanelSurfaces(rightPanelState.surfaces);
3951 - useRightPanelStore.getState().closeAllSurfaces(activeThreadRef);
3952 - }, [activeThreadRef, cleanupRightPanelSurfaces, rightPanelState.surfaces]);
3982 + const finishClose = () => finishRightPanelSurfaceClose(rightPanelState.surfaces);
3983 + closeAfterAgentBrowserConfirmation(rightPanelState.surfaces, finishClose);
3984 + }, [
3985 + activeThreadRef,
3986 + closeAfterAgentBrowserConfirmation,
3987 + finishRightPanelSurfaceClose,
3988 + rightPanelState.surfaces,
3989 + ]);
3990 const copyRightPanelFilePath = useCallback((relativePath: string) => {
3991 if (typeof window === "undefined" || !navigator.clipboard?.writeText) {
3992 toastManager.add(