fix(agents): close open session view when archiving a task (#7440)

Archiving a task closes its session and Changes tabs, returns a docked Agents tool window to the task list, and clears lastTask / lastComposer so Send to Agent and layout switches do not target the read-only task. Closing a detached window of an archived task keeps its draft without reopening it. Reopening an already archived task from the Archived group still shows it.
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-12-02 17:06:27 +08:00
1 джерело 7f20fc0c54
коміт 04006f2ea7
5 files changed
+106 -9

No files matched your search

+7
Переглянути файл
@@ -126,6 +126,13 @@ describe("Send to Agent from the Project view", () => {
]);
});
it("sends references to New Task instead of an archived task", () => {
setup({ task: { ...task(BACK, BACK, "local"), status: "archived" } });
expect(sendPaths([`${BACK}/pom.xml`])).toBe(true);
expect(sent(HOME).text).toBe("@pom.xml");
expect(sent("t").text).toBe("");
});
it("a path outside every project shows the notice instead of doing nothing", () => {
setup({ members: [BACK, FRONT], homeRoot: BACK });
expect(sendPaths(["/etc/hosts"])).toBe(false);
+32 -6
Переглянути файл
@@ -77,6 +77,7 @@ import {
SESSION_KIND,
TOOL_WINDOW,
appendEntry,
changesKey,
entriesKey,
isBusy,
isInterrupted,
@@ -745,6 +746,15 @@ function dropTaskCache(taskId: string) {
useAgents.setState((s) => withoutTaskCache(s, taskId) ?? s);
}
function closeTaskTabs(taskId: string) {
const editors = editorService();
const close = editors.closeInput;
if (!close) return;
const closeChanges = editors.openTabs?.().some((t) => t.input.key === changesKey(taskId));
const first = close(sessionKey(taskId));
if (closeChanges) void Promise.resolve(first).then(() => close(changesKey(taskId)));
}
function isTaskOpenInUi(taskId: string): boolean {
if (useAgents.getState().docked === taskId) return true;
return (editorService().openTabs?.() ?? []).some(
@@ -832,6 +842,14 @@ function onEvent(e: AgentEvent) {
else if (prev?.unread && !e.task.unread && isBusy(e.task.status)) setUnread(e.task.id, false, false);
if (e.task.status === "archived") {
setUnread(e.task.id, false, false);
if (prev && prev.status !== "archived") {
const patch: Partial<AgentsState> = {};
if (s.docked === e.task.id) patch.docked = null;
if (s.lastTask === e.task.id) patch.lastTask = null;
if (s.lastComposer === e.task.id) patch.lastComposer = HOME;
if (Object.keys(patch).length) useAgents.setState(patch);
closeTaskTabs(e.task.id);
}
if (prev?.status !== "archived" && !isTaskOpenInUi(e.task.id)) dropTaskCache(e.task.id);
}
if (prev?.status !== e.task.status && !isBusy(e.task.status) && e.task.status !== "archived") {
@@ -864,10 +882,12 @@ function onEvent(e: AgentEvent) {
...withoutTaskCache(s, e.id),
organize,
...(s.docked === e.id ? { docked: null } : {}),
...(s.lastTask === e.id ? { lastTask: null } : {}),
...(s.lastComposer === e.id ? { lastComposer: HOME } : {}),
});
clearAgentTaskFontZoom(sessionKey(e.id));
clearDismissedAssets(e.id);
void editorService().closeInput?.(sessionKey(e.id));
closeTaskTabs(e.id);
break;
}
case "truncated": {
@@ -1242,10 +1262,11 @@ export function setSessionLocation(location: SessionLocation) {
const active = editorService().activeInput?.();
const activeTask = active?.kind === SESSION_KIND ? (active.data as { task: string }).task : null;
const home = s.homeOpen || active?.kind === HOME_KIND;
const lastTask = s.lastTask ? s.tasks.get(s.lastTask) : undefined;
const task =
s.docked ??
activeTask ??
(s.lastTask && s.tasks.has(s.lastTask) ? s.lastTask : null) ??
(lastTask && lastTask.status !== "archived" ? lastTask.id : null) ??
[...s.tasks.values()]
.filter((t) => t.status !== "archived")
.sort((a, b) => (b.updatedMs ?? b.createdMs ?? 0) - (a.updatedMs ?? a.createdMs ?? 0))[0]?.id;
@@ -1968,26 +1989,31 @@ export async function setTaskMemoryOverrides(task: string, overrides: AgentMemor
/** The composer the user is looking at: the Agents surface on show, else the one typed in last. */
function activeComposerKey(): string {
const s = useAgents.getState();
const editableTask = (id: unknown): id is string => {
if (typeof id !== "string") return false;
const task = s.tasks.get(id);
return !!task && task.status !== "archived";
};
// The tool window shows one conversation at a time; its inline New Task page wins.
if (s.sessionLocation === "toolWindow") {
if (s.homeOpen) return HOME;
if (s.docked && s.tasks.has(s.docked)) return s.docked;
if (editableTask(s.docked)) return s.docked;
}
const active = editorService().activeInput?.();
if (active?.kind === HOME_KIND) return HOME;
if (active?.kind === SESSION_KIND) {
const task = (active.data as { task?: unknown } | undefined)?.task;
if (typeof task === "string" && s.tasks.has(task)) return task;
if (editableTask(task)) return task;
}
// No agent tab has focus (the Project view has it): the one used last wins.
for (const tab of editorService().openTabs?.() ?? []) {
if (tab.input.kind === HOME_KIND) return HOME;
if (tab.input.kind === SESSION_KIND) {
const task = (tab.input.data as { task?: unknown } | undefined)?.task;
if (typeof task === "string" && s.tasks.has(task)) return task;
if (editableTask(task)) return task;
}
}
return s.lastComposer !== HOME && s.tasks.has(s.lastComposer) ? s.lastComposer : HOME;
return s.lastComposer !== HOME && editableTask(s.lastComposer) ? s.lastComposer : HOME;
}
/** The project a New Task started now would run in (the New Task page's project picker). */
+12
Переглянути файл
@@ -205,6 +205,18 @@ describe("moving a conversation to a lightweight editor window", () => {
expect(backend.unsubscribe).not.toHaveBeenCalled();
});
it("keeps the draft without reopening an archived conversation when its window closes", async () => {
await detach();
receive({ type: "draft", task: "running-task", label: CHILD, draft: { text: "Typed before archive" } });
backend.state.tasks.set("running-task", { status: "archived", title: "Fix the build" });
receive({ type: "closed", task: "running-task", label: CHILD });
expect(backend.updateComposer).toHaveBeenCalledWith("running-task", {
...backend.draft,
text: "Typed before archive",
});
expect(backend.openTask).not.toHaveBeenCalled();
});
it("counts a conversation in a focused window as read", async () => {
await detach();
backend.state.unread.add("running-task");
+4 -1
Переглянути файл
@@ -132,8 +132,11 @@ function restore(task: string, entry: Detached) {
if (detached.get(task) !== entry) return;
detached.delete(task);
void entry.stop?.();
if (!useAgents.getState().tasks.has(task)) return;
const current = useAgents.getState().tasks.get(task);
if (!current) return;
updateComposer(task, entry.draft);
// An archived task closed its window on purpose: keep the draft without reopening it here.
if (current.status === "archived") return;
openTask(task);
void focusWindow(getCurrentWindow().label);
}
+51 -2
Переглянути файл
@@ -16,8 +16,9 @@ import { openChangeDiff } from "./session/ChangesTab";
import * as actions from "@/platform/actions";
import { commands } from "@/platform/ipc";
import { deferred } from "@/test/deferred";
import { SESSION_KIND, sessionKey, TASK_DRAG } from "./model";
import { CHANGES_KIND, changesKey, SESSION_KIND, sessionKey, TASK_DRAG } from "./model";
import {
HOME,
connect,
disconnect,
loadEntries,
@@ -362,8 +363,9 @@ describe("agent unread activity", () => {
expect(afterArchived.changes.has("two")).toBe(false);
expect(afterArchived.groupedChanges.has("two")).toBe(false);
// An archived task that is still open in a tab keeps its entries until closed.
// Archiving an open task requests closing its tab; its entries drop once the tab closes.
emit({ type: "task", task: task("open", "archived") });
expect(backend.closeInput).toHaveBeenCalledWith(sessionKey("open"));
expect(useAgents.getState().entries.has("open")).toBe(true);
backend.tabs = [];
const dispose = activate();
@@ -376,6 +378,34 @@ describe("agent unread activity", () => {
}
});
it("closes an archived task's session and changes tabs, clears active references, and leaves reopened archived tasks open", async () => {
backend.tabs = [
{
input: { kind: SESSION_KIND, key: sessionKey("one"), data: { task: "one" } },
title: "one",
icon: "toolwindow-agents",
lastActive: 1,
},
{
input: { kind: CHANGES_KIND, key: changesKey("one"), data: { task: "one" } },
title: "Changes: one",
icon: "diff",
lastActive: 2,
},
];
useAgents.setState({ lastTask: "one", lastComposer: "one" });
emit({ type: "task", task: task("one", "archived") });
expect(backend.closeInput).toHaveBeenCalledWith(sessionKey("one"));
await vi.waitFor(() => expect(backend.closeInput).toHaveBeenCalledWith(changesKey("one")));
expect(useAgents.getState().lastTask).toBeNull();
expect(useAgents.getState().lastComposer).toBe(HOME);
// Subsequent updates on an already-archived task (e.g. reopened from Archived) do not close it again.
backend.closeInput.mockClear();
emit({ type: "task", task: { ...task("one", "archived"), title: "Renamed" } });
expect(backend.closeInput).not.toHaveBeenCalled();
});
it.each([false, true])(
"releases the previously docked task when another task or New Task replaces it (via New Task: %s)",
(viaHome) => {
@@ -466,6 +496,25 @@ describe("docking a conversation in the tool window", () => {
expect(useAgents.getState().docked).toBeNull();
});
it("undocks an archived task and drops its cache without disturbing another docked task", () => {
useAgents.setState((s) => ({
tasks: new Map(s.tasks).set("two", task("two", "ready")),
entries: new Map(s.entries)
.set("one", [{ type: "agent", text: "Answer one" }])
.set("two", [{ type: "agent", text: "Answer two" }]),
}));
act(() => dockTask("one"));
// Archiving a different task keeps the currently docked conversation open.
emit({ type: "task", task: task("two", "archived") });
expect(useAgents.getState().docked).toBe("one");
expect(useAgents.getState().entries.has("two")).toBe(false);
// Archiving the docked task returns the tool window to the task list and evicts its cache.
emit({ type: "task", task: task("one", "archived") });
expect(useAgents.getState().docked).toBeNull();
expect(useAgents.getState().entries.has("one")).toBe(false);
});
it("drops a dragged task into the editor: undocking when docked, opening a tab otherwise", () => {
const dispose = activate();
const execute = vi.spyOn(actions, "executeAction");