diff --git a/e2e/agentsDock.spec.ts b/e2e/agentsDock.spec.ts
index ab54ff5f8..e354d0f56 100644
--- a/e2e/agentsDock.spec.ts
+++ b/e2e/agentsDock.spec.ts
@@ -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);
+});
diff --git a/src/features/agents/AgentsView.test.tsx b/src/features/agents/AgentsView.test.tsx
index 23be822cb..a1a8a608e 100644
--- a/src/features/agents/AgentsView.test.tsx
+++ b/src/features/agents/AgentsView.test.tsx
@@ -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();
+ 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();
diff --git a/src/features/agents/AgentsView.tsx b/src/features/agents/AgentsView.tsx
index 636d723de..c4296cbab 100644
--- a/src/features/agents/AgentsView.tsx
+++ b/src/features/agents/AgentsView.tsx
@@ -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([]);
const [tasksEl, setTasksEl] = useState(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(null);
useEffect(() => {
if (!tasksEl) return;