diff --git a/packages/app/src/screens/workspace/workspace-screen.tsx b/packages/app/src/screens/workspace/workspace-screen.tsx index 0c30fb5bfc..6915d861f8 100644 --- a/packages/app/src/screens/workspace/workspace-screen.tsx +++ b/packages/app/src/screens/workspace/workspace-screen.tsx @@ -2003,9 +2003,8 @@ function WorkspaceScreenContent({ const agent = useSessionStore.getState().sessions[normalizedServerId]?.agents?.get(agentId) ?? null; let closePolicy = resolveCloseAgentTabPolicy(agent); - const isRunning = agent?.status === "running"; - if (isRunning && closePolicy.kind === "archive-on-close") { + if (closePolicy.kind === "archive-on-close" && closePolicy.confirmArchive) { const confirmed = await confirmDialog({ title: t("workspace.tabs.confirmations.archiveRunningAgentTitle"), message: t("workspace.tabs.confirmations.archiveRunningAgentMessage"), diff --git a/packages/app/src/subagents/close-tab-policy.test.ts b/packages/app/src/subagents/close-tab-policy.test.ts index cdce429b00..1534d35a80 100644 --- a/packages/app/src/subagents/close-tab-policy.test.ts +++ b/packages/app/src/subagents/close-tab-policy.test.ts @@ -2,20 +2,51 @@ import { describe, expect, it } from "vitest"; import { resolveCloseAgentTabPolicy } from "./close-tab-policy"; describe("resolveCloseAgentTabPolicy", () => { - it("archives root agents when their tab closes", () => { - expect(resolveCloseAgentTabPolicy({ parentAgentId: null })).toEqual({ + it("archives an idle root agent without asking", () => { + expect( + resolveCloseAgentTabPolicy({ + parentAgentId: null, + status: "idle", + backgroundWork: { count: 0 }, + }), + ).toEqual({ kind: "archive-on-close", confirmArchive: false }); + }); + + it("asks before archiving a running root agent", () => { + expect(resolveCloseAgentTabPolicy({ parentAgentId: null, status: "running" })).toEqual({ kind: "archive-on-close", + confirmArchive: true, }); }); + it("asks before archiving a root agent whose turn ended while background work still runs", () => { + expect( + resolveCloseAgentTabPolicy({ + parentAgentId: null, + status: "idle", + backgroundWork: { count: 1 }, + }), + ).toEqual({ kind: "archive-on-close", confirmArchive: true }); + }); + it("keeps subagent tab close layout-only", () => { - expect(resolveCloseAgentTabPolicy({ parentAgentId: "parent-agent" })).toEqual({ - kind: "layout-only", - }); + expect( + resolveCloseAgentTabPolicy({ + parentAgentId: "parent-agent", + status: "running", + backgroundWork: { count: 1 }, + }), + ).toEqual({ kind: "layout-only" }); }); it("preserves the existing archive fallback when the agent is missing", () => { - expect(resolveCloseAgentTabPolicy(null)).toEqual({ kind: "archive-on-close" }); - expect(resolveCloseAgentTabPolicy(undefined)).toEqual({ kind: "archive-on-close" }); + expect(resolveCloseAgentTabPolicy(null)).toEqual({ + kind: "archive-on-close", + confirmArchive: false, + }); + expect(resolveCloseAgentTabPolicy(undefined)).toEqual({ + kind: "archive-on-close", + confirmArchive: false, + }); }); }); diff --git a/packages/app/src/subagents/close-tab-policy.ts b/packages/app/src/subagents/close-tab-policy.ts index fa82e738b9..dc44472c66 100644 --- a/packages/app/src/subagents/close-tab-policy.ts +++ b/packages/app/src/subagents/close-tab-policy.ts @@ -1,13 +1,21 @@ import type { Agent } from "@/stores/session-store"; -export type CloseAgentTabPolicy = { kind: "archive-on-close" } | { kind: "layout-only" }; +export type CloseAgentTabPolicy = + | { kind: "archive-on-close"; confirmArchive: boolean } + | { kind: "layout-only" }; export function resolveCloseAgentTabPolicy( - agent: Pick | null | undefined, + agent: Pick | null | undefined, ): CloseAgentTabPolicy { if (agent?.parentAgentId) { return { kind: "layout-only" }; } - return { kind: "archive-on-close" }; + return { kind: "archive-on-close", confirmArchive: hasRunningWork(agent) }; +} + +// Archiving stops the agent's process, which also stops background work it left running after +// its turn ended, such as a background subagent or shell. +function hasRunningWork(agent: Pick | null | undefined) { + return agent?.status === "running" || (agent?.backgroundWork?.count ?? 0) > 0; }