Ask before closing a tab archives an agent with background work

Closing a root agent's tab archives it, and archiving stops background
work the agent left running after its turn ended, such as a background
subagent. The "Archive running agent?" confirmation only checked
status === "running", so an idle agent with background work was
archived without asking. The close-tab policy now decides whether to
confirm, and counts background work as running.

Closes #10038

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
paseo-bot[bot]
2026-11-18 04:48:35 +00:00
co-authored by Claude Opus 5.5
parent 91e7293773
commit 32e7df0554
3 changed files with 50 additions and 12 deletions
@@ -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"),
@@ -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,
});
});
});
+11 -3
View File
@@ -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<Agent, "parentAgentId"> | null | undefined,
agent: Pick<Agent, "parentAgentId" | "status" | "backgroundWork"> | 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<Agent, "status" | "backgroundWork"> | null | undefined) {
return agent?.status === "running" || (agent?.backgroundWork?.count ?? 0) > 0;
}