fix(agents): drop the task list focus when its focused element is removed (#7752)

Clicking a task in the Agents tool window replaces the list with the conversation while the list still has focus; React sends no blur for an element it removes, so the agentTaskListFocus context stayed on and Del in the editor asked to delete the open conversation. The same happened after picking a session from the history popup, archiving a row, or removing the last task while its search field had focus. The context is now cleared when the list element goes away and whenever focus lands outside the list.

Closes #7725

Co-authored-by: og-orca <[email protected]>
Цей коміт міститься в:
Orca authored and GitHub committed 2026-12-03 18:07:49 +08:00
1 джерело c1b45b037d
коміт 0425004ecd
3 files changed
+91 -1

No files matched your search

+39
Переглянути файл
@@ -117,3 +117,42 @@ test("docks a session tab dropped on the tool window", async ({ page }) => {
await expect(page.getByRole("tree", { name: "Agent tasks" })).toHaveCount(0);
await expect(page.locator("span[title='Repro task']").first()).toBeVisible();
});
test("Delete edits the file after opening a conversation in the tool window", async ({ page }) => {
await page.goto("/");
await page.getByRole("tree", { name: "Project" }).waitFor();
await page.getByRole("treeitem", { name: "README.md", exact: true }).dblclick();
await page.getByRole("tab", { name: "README.md" }).waitFor();
await page.evaluate(async (task) => {
const storePath = "/src/features/agents/store.ts";
const modelPath = "/src/features/agents/model.ts";
const contributionsPath = "/src/workbench/contributions.ts";
const store = await import(storePath);
const { TOOL_WINDOW } = await import(modelPath);
const { toolWindowService } = await import(contributionsPath);
store.useAgents.setState({
loaded: true,
error: null,
providers: [],
tasks: new Map([[task.id, task]]),
unread: new Set(),
docked: null,
sessionLocation: "toolWindow",
});
toolWindowService().show(TOOL_WINDOW, true);
}, TASK);
await page.getByRole("tree", { name: "Agent tasks" }).getByText("Repro task").click();
await expect(page.locator("span[title='Repro task']").first()).toBeVisible();
await expect(page.getByRole("tree", { name: "Agent tasks" })).toHaveCount(0);
const line = page
.getByRole("textbox", { name: /README/ })
.locator(".cm-line")
.first();
await line.click();
await page.keyboard.press("Home");
const text = (await line.textContent())!;
await page.keyboard.press("Delete");
await expect(line).toHaveText(text.slice(1));
await expect(page.getByRole("dialog", { name: "Delete Agent Task" })).toHaveCount(0);
});
+40
Переглянути файл
@@ -3,6 +3,7 @@ import { act, fireEvent, render, renderHook, screen, waitFor, within } from "@te
import type { AgentTask } from "@/bindings";
import * as actions from "@/platform/actions";
import { registerActions } from "@/platform/actions";
import { getContext } from "@/platform/context";
import { EDITOR_TAB_MIME, type EditorInput } from "@/workbench/contributions";
import { confirm, confirmChecked, showError } from "@/workbench/messages";
import { promptInput } from "@/workbench/inputDialogs";
@@ -281,6 +282,45 @@ describe("Agents docked view", () => {
expect(screen.getByTestId("docked-session")).toBe(session);
});
it("stops being the focused task list when a docked conversation takes its place", () => {
setup();
screen.getByRole("tree", { name: "Agent tasks" }).focus();
expect(getContext().agentTaskListFocus).toBe(true);
act(() => useAgents.setState({ docked: "one" }));
expect(screen.queryByRole("tree", { name: "Agent tasks" })).toBeNull();
expect(getContext().agentTaskListFocus).toBe(false);
});
it("stops being the focused task list when picking a session closes the history", () => {
useAgents.setState({ docked: "one" });
setup();
fireEvent.click(screen.getByRole("button", { name: "Agent sessions" }));
const tree = screen.getByRole("tree", { name: "Agent tasks" });
tree.focus();
expect(getContext().agentTaskListFocus).toBe(true);
fireEvent.click(within(tree).getByText("two"));
expect(screen.queryByRole("dialog", { name: "Agent sessions" })).toBeNull();
expect(getContext().agentTaskListFocus).toBe(false);
});
it.each([
["row button", () => within(row("two") as HTMLElement).getByRole("button", { name: "Archive" }), ["one"]],
["search field", () => screen.getByLabelText("Search tasks"), []],
])(
"stops being the focused task list when its focused %s is removed and focus moves on",
(_, focused, left: string[]) => {
const { container } = render(<button type="button">Elsewhere</button>);
setup();
const element = focused();
act(() => element.focus());
expect(getContext().agentTaskListFocus).toBe(true);
act(() => useAgents.setState({ tasks: new Map(left.map((id) => [id, task(id)])) }));
expect(element).not.toBeInTheDocument();
act(() => container.querySelector("button")!.focus());
expect(getContext().agentTaskListFocus).toBe(false);
},
);
it("gives the whole tool window to the conversation when the docked task is the only one", () => {
useAgents.setState({ tasks: new Map([task("one")].map((t) => [t.id, t])), docked: "one" });
setup();
+12 -1
Переглянути файл
@@ -215,7 +215,6 @@ export function AgentsView() {
useEffect(() => {
selectedRef.current = selected;
}, [selected]);
useEffect(() => () => setFocusedTaskList(null), []);
const organize = useAgents((s) => s.organize);
const pinned = useMemo(() => new Set(organize.pinned), [organize.pinned]);
const [query, setQuery] = useState("");
@@ -271,6 +270,18 @@ export function AgentsView() {
/** Every task the current drag carries, in list order; set when it starts. */
const draggedIdsRef = useRef<string[]>([]);
const [tasksEl, setTasksEl] = useState<HTMLDivElement | null>(null);
useEffect(() => {
if (!tasksEl) return;
// A focused element that React removes gets no blur: a row's button, or the list behind a docked session.
const onFocusIn = (e: FocusEvent) => {
if (!(e.target instanceof Element && tasksEl.contains(e.target))) setFocusedTaskList(null);
};
document.addEventListener("focusin", onFocusIn);
return () => {
document.removeEventListener("focusin", onFocusIn);
setFocusedTaskList(null);
};
}, [tasksEl]);
const [dropIndicator, setDropIndicator] = useState<DropIndicator | null>(null);
useEffect(() => {
if (!tasksEl) return;