Check the daemon's subagent list before closing an agent tab

The app only holds provider subagents for agents it has rendered or
heard about while connected. After a reload, a restored tab that was
never activated had no subagents loaded, so closing it still archived
the agent without asking while its background subagent ran. Before
deciding, the tab close now refreshes that agent's subagent list from
the daemon, and falls back to the loaded list if the request fails.

Refs #10038

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
paseo-bot[bot]
2026-11-18 05:09:17 +00:00
co-authored by Claude Opus 5.5
parent 20d7ae6cd5
commit 3e34be1228
5 changed files with 124 additions and 12 deletions
@@ -211,7 +211,7 @@ import {
classifyBulkClosableTabs,
closeBulkWorkspaceTabs,
} from "@/screens/workspace/workspace-bulk-close";
import { resolveCloseAgentTabPolicy } from "@/subagents";
import { loadCloseAgentTabPolicy, resolveCloseAgentTabPolicy } from "@/subagents";
import { useProviderSubagentStore } from "@/subagents/provider-store";
import {
getPanelInstanceAttributes,
@@ -246,6 +246,12 @@ const EMPTY_WORKSPACE_SCRIPTS: WorkspaceDescriptor["scripts"] = [];
const EMPTY_PINNED_AGENT_IDS = new Set<string>();
const EMPTY_SET = new Set<string>();
type WorkspaceSession = ReturnType<typeof useSessionStore.getState>["sessions"][string];
function providerSubagentListClient(session: WorkspaceSession | undefined) {
return session?.serverInfo?.features?.providerSubagents === true ? session.client : null;
}
function getWorkspaceSetupStatus(workspace: WorkspaceDescriptor | null | undefined) {
return workspace?.setupStatus ?? null;
}
@@ -2001,12 +2007,12 @@ function WorkspaceScreenContent({
return;
}
const agent =
useSessionStore.getState().sessions[normalizedServerId]?.agents?.get(agentId) ?? null;
let closePolicy = resolveCloseAgentTabPolicy(
const session = useSessionStore.getState().sessions[normalizedServerId];
const agent = session?.agents?.get(agentId) ?? null;
let closePolicy = await loadCloseAgentTabPolicy({
agent,
useProviderSubagentStore.getState().descriptors,
);
providerSubagents: providerSubagentListClient(session),
});
if (closePolicy.kind === "archive-on-close" && closePolicy.confirmArchive) {
const confirmed = await confirmDialog({
@@ -1,7 +1,7 @@
import { describe, expect, it } from "vitest";
import { afterEach, describe, expect, it } from "vitest";
import type { ProviderSubagentDescriptorPayload } from "@getpaseo/protocol/messages";
import { resolveCloseAgentTabPolicy } from "./close-tab-policy";
import { providerSubagentKey } from "./provider-store";
import { loadCloseAgentTabPolicy, resolveCloseAgentTabPolicy } from "./close-tab-policy";
import { providerSubagentKey, useProviderSubagentStore } from "./provider-store";
const SERVER_ID = "server-1";
const AGENT_ID = "agent-1";
@@ -17,6 +17,44 @@ function rootAgent(overrides: Partial<Parameters<typeof resolveCloseAgentTabPoli
};
}
function descriptor(
parentAgentId: string,
id: string,
status: ProviderSubagentDescriptorPayload["status"],
): ProviderSubagentDescriptorPayload {
return {
id,
parentAgentId,
provider: "claude",
title: null,
description: "Run tool calls then wait",
status,
createdAt: "2026-11-18T00:00:00.000Z",
updatedAt: "2026-11-18T00:00:00.000Z",
toolCallId: "call-1",
};
}
function fakeDaemon(lists: Record<string, ProviderSubagentDescriptorPayload[]>) {
return {
listProviderSubagents: async (parentAgentId: string) => ({
requestId: "request-1",
parentAgentId,
subagents: lists[parentAgentId] ?? [],
error: null,
}),
};
}
afterEach(() => {
useProviderSubagentStore.setState({
descriptors: new Map(),
timelines: new Map(),
hiddenFromTrack: new Set(),
trackedParents: new Set(),
});
});
function providerSubagents(
entries: Array<{
parentAgentId: string;
@@ -100,3 +138,39 @@ describe("resolveCloseAgentTabPolicy", () => {
});
});
});
describe("loadCloseAgentTabPolicy", () => {
it("asks for a background subagent this client has not loaded yet, such as a tab restored after a reload", async () => {
const daemon = fakeDaemon({ [AGENT_ID]: [descriptor(AGENT_ID, "sub-1", "running")] });
await expect(
loadCloseAgentTabPolicy({ agent: rootAgent(), providerSubagents: daemon }),
).resolves.toEqual({ kind: "archive-on-close", confirmArchive: true });
});
it("falls back to the subagents already loaded when the daemon cannot list them", async () => {
useProviderSubagentStore
.getState()
.applyUpdate(SERVER_ID, {
kind: "upsert",
subagent: descriptor(AGENT_ID, "sub-1", "running"),
});
const disconnected = {
listProviderSubagents: async () => {
throw new Error("Transport not connected");
},
};
await expect(
loadCloseAgentTabPolicy({ agent: rootAgent(), providerSubagents: disconnected }),
).resolves.toEqual({ kind: "archive-on-close", confirmArchive: true });
});
it("archives without asking when the daemon lists no running subagent", async () => {
const daemon = fakeDaemon({ [AGENT_ID]: [descriptor(AGENT_ID, "sub-1", "completed")] });
await expect(
loadCloseAgentTabPolicy({ agent: rootAgent(), providerSubagents: daemon }),
).resolves.toEqual({ kind: "archive-on-close", confirmArchive: false });
});
});
+29 -1
View File
@@ -1,6 +1,11 @@
import type { ProviderSubagentDescriptorPayload } from "@getpaseo/protocol/messages";
import type { Agent } from "@/stores/session-store";
import { hasRunningProviderSubagents } from "./provider-store";
import {
hasRunningProviderSubagents,
refreshProviderSubagents,
useProviderSubagentStore,
type ProviderSubagentListClient,
} from "./provider-store";
export type CloseAgentTabPolicy =
| { kind: "archive-on-close"; confirmArchive: boolean }
@@ -22,6 +27,29 @@ export function resolveCloseAgentTabPolicy(
};
}
/**
* Resolves the policy against the daemon's current subagent list. This client only holds the
* subagents of agents it has rendered or heard about while connected, so a tab restored after a
* reload would otherwise close without asking while its background subagent runs.
*/
export async function loadCloseAgentTabPolicy(input: {
agent: CloseTabAgent | null | undefined;
/** Null when the host does not report provider subagents or no client is connected. */
providerSubagents: ProviderSubagentListClient | null;
}): Promise<CloseAgentTabPolicy> {
const { agent, providerSubagents } = input;
const cached = resolveCloseAgentTabPolicy(agent, useProviderSubagentStore.getState().descriptors);
if (!agent || !providerSubagents || cached.kind !== "archive-on-close" || cached.confirmArchive) {
return cached;
}
try {
await refreshProviderSubagents(providerSubagents, agent.serverId, agent.id);
} catch {
return cached;
}
return resolveCloseAgentTabPolicy(agent, useProviderSubagentStore.getState().descriptors);
}
// Archiving stops the agent's process, which also stops work it left running after its turn
// ended: background subagents and background shells.
function hasRunningWork(
+5 -1
View File
@@ -2,7 +2,11 @@ export type { SubagentRow } from "./select";
export { selectSubagentsForParent, useSubagentsForParent } from "./select";
export { useArchiveSubagent } from "./use-archive-subagent";
export { useDetachSubagent } from "./use-detach-subagent";
export { resolveCloseAgentTabPolicy, type CloseAgentTabPolicy } from "./close-tab-policy";
export {
loadCloseAgentTabPolicy,
resolveCloseAgentTabPolicy,
type CloseAgentTabPolicy,
} from "./close-tab-policy";
export { isWorkspaceRootAgent } from "./workspace-root-policy";
export {
useArchiveFinishedSubagents,
+1 -1
View File
@@ -69,7 +69,7 @@ export function providerSubagentLifecycleStatus(
return "idle";
}
type ProviderSubagentListClient = Pick<DaemonClient, "listProviderSubagents">;
export type ProviderSubagentListClient = Pick<DaemonClient, "listProviderSubagents">;
const pendingListRequests = new WeakMap<ProviderSubagentListClient, Map<string, Promise<void>>>();