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

Co-authored-by: maria-rcks <[email protected]>
This commit is contained in:
mariaandmaria-rcks authored and GitHub committed 2026-09-02 17:26:34 -04:00
1 parent 5a9b56291f
commit 28ddaf7591
3 files changed
+121 -20

No files matched your search

@@ -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);
+25
View File
@@ -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,
+57 -20
View File
@@ -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(