Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(desktop): base the output-preview pin on the panel on screen

skylere7862780353f
diff --git a/apps/desktop/src/features/chat/background-tasks/dock-state.ts b/apps/desktop/src/features/chat/background-tasks/dock-state.ts
index 0540f5b26..3ed1107dd 100644
--- a/apps/desktop/src/features/chat/background-tasks/dock-state.ts
+++ b/apps/desktop/src/features/chat/background-tasks/dock-state.ts
@@ -150,6 +150,25 @@ export function visibleDockTab(
return resolveDockTabFor(state, sides);
}
+/**
+ * Whether the output preview is on screen: a row is open, the container is
+ * expanded, and the panel on screen is Tasks.
+ *
+ * The resolved panel, not the remembered choice, decides: choosing Todo ends a
+ * read only while the Todo side still has content, so clearing every todo —
+ * which falls back to Tasks without changing the choice — leaves the read
+ * alive (#2634). A tab the container chose on its own never interrupts the
+ * read either, because `visibleDockTab` keeps Tasks on screen for it.
+ */
+export function isReading(
+ state: DockSessionState,
+ sides: DockSides,
+ expandedId: string | null,
+): boolean {
+ if (expandedId === null || state.collapsed) return false;
+ return resolveDockTabFor(state, sides) === "tasks" || state.chosenTab === null;
+}
+
/**
* The user picked a tab. Only this writes the remembered choice, so a tab the
* container chose on its own never overwrites what the user wanted.
diff --git a/apps/desktop/src/features/chat/background-tasks/useBackgroundTasks.ts b/apps/desktop/src/features/chat/background-tasks/useBackgroundTasks.ts
index 696c01083..add04d871 100644
--- a/apps/desktop/src/features/chat/background-tasks/useBackgroundTasks.ts
+++ b/apps/desktop/src/features/chat/background-tasks/useBackgroundTasks.ts
@@ -6,6 +6,7 @@ import {
type DockTab,
type DockViewState,
badgeLabel,
+ isReading,
pruneDockView,
selectTab,
sessionDockState,
@@ -221,22 +222,24 @@ export function useBackgroundTasks(
// The tab and badge rules see the rows the container can render, so a task
// that already left it cannot decide which panel is on screen.
const sides = sidesFor(visibleTasks, todos);
- // Whether the output preview is on screen: expanded, container open, and
- // the user has not chosen the Todo tab. The auto tab rule never takes the
- // panel away from an open preview, so a read cannot be cut short by the
- // last running task ending (#2634).
- const reading = expandedId !== null && !dock.collapsed && dock.chosenTab !== "todo";
+ // Whether the output preview is on screen. The panel that resolves decides,
+ // not the remembered choice: a remembered Todo whose side emptied has
+ // already fallen back to Tasks, and a read on that panel is a visible read.
+ // A tab the container chose on its own never takes the panel away from an
+ // open preview, so a read cannot be cut short by the last running task
+ // ending (#2634).
+ const reading = isReading(dock, sides, expandedId);
const activeTab: DockTab = visibleDockTab(dock, sides, reading);
// The badge mirrors the session's live work: it rises when a task starts and
// falls as tasks end, and no view action can change it (#2632).
const badge = sides.runningTasks;
// The pin on a row being read ends when its preview leaves the screen —
- // collapse, or a switch to Todo — so a finished row cannot reappear later
- // with no output on screen (#2634). A running task keeps its preview across
- // the switch: the row itself is live work and stays, so coming back to the
- // tasks tab picks up the same read; if the task ends while hidden, the pin
- // goes with it.
+ // collapse, or the panel on screen becoming Todo — so a finished row cannot
+ // reappear later with no output on screen (#2634). A running task keeps its
+ // preview across the switch: the row itself is live work and stays, so
+ // coming back to the tasks tab picks up the same read; if the task ends
+ // while hidden, the pin goes with it.
useEffect(() => {
if (expandedId === null || reading) return;
const pinnedTask = sessionTasks.find((task) => task.id === expandedId);
diff --git a/apps/desktop/test/background-tasks-bar-visibility.test.mjs b/apps/desktop/test/background-tasks-bar-visibility.test.mjs
index b9d8920c5..0ca6c7ea3 100644
--- a/apps/desktop/test/background-tasks-bar-visibility.test.mjs
+++ b/apps/desktop/test/background-tasks-bar-visibility.test.mjs
@@ -11,7 +11,7 @@ import test from "node:test";
register(new URL("./helpers/ts-import-hooks.mjs", import.meta.url));
const { selectVisibleTasks } = await import("../src/features/chat/background-tasks/bar-visibility.ts");
-const { sidesFor, visibleDockTab } = await import("../src/features/chat/background-tasks/dock-state.ts");
+const { isReading, sidesFor, visibleDockTab } = await import("../src/features/chat/background-tasks/dock-state.ts");
const task = (id, state, extra = {}) => ({
id,
@@ -63,29 +63,27 @@ test("a running task is visible whether or not its output is open", () => {
/*
* The pin on a row being read, driven the way the hook composes it: the rows
- * carry the pin, the sides describe them, `reading` says whether the preview
+ * carry the pin, the sides describe them, `isReading` says whether the preview
* is on screen, and `visibleDockTab` keeps the tasks panel under it. When the
* preview leaves the screen the hook closes the pin — unless the task is
* still running, whose preview survives the switch and picks up the same
* read when the user returns (#2634).
*/
-/** The hook's on-screen rule for the preview: expanded, open, not on Todo. */
-const reading = (expandedId, dock) => expandedId !== null && !dock.collapsed && dock.chosenTab !== "todo";
-
test("regression: the row being read stays while its preview is on screen", () => {
// start → open output: the task finishes while the user reads it.
const running = task("a", "running");
const dock = { chosenTab: "tasks", collapsed: false };
const rows = selectVisibleTasks([running], "a");
- assert.equal(visibleDockTab(dock, sidesFor(rows, []), reading("a", dock)), "tasks");
+ const sides = sidesFor(rows, []);
+ assert.equal(visibleDockTab(dock, sides, isReading(dock, sides, "a")), "tasks");
const ended = { ...running, state: "exited", exitCode: 0 };
assert.deepEqual(ids(selectVisibleTasks([ended], "a")), ["a"]);
// The pinned row keeps the tasks side non-empty, so the panel — and the
// read — survives the task ending underneath it.
- const pinnedRows = selectVisibleTasks([ended], "a");
- assert.equal(visibleDockTab(dock, sidesFor(pinnedRows, []), reading("a", dock)), "tasks");
+ const pinnedSides = sidesFor(selectVisibleTasks([ended], "a"), []);
+ assert.equal(visibleDockTab(dock, pinnedSides, isReading(dock, pinnedSides, "a")), "tasks");
});
test("regression: the auto tab rule does not take the panel from an open read", () => {
@@ -98,11 +96,12 @@ test("regression: the auto tab rule does not take the panel from an open read",
const ended = { ...running, state: "exited", exitCode: 0 };
const rows = selectVisibleTasks([ended], "a");
- assert.equal(reading("a", dock), true);
+ const sides = sidesFor(rows, todos);
+ assert.equal(isReading(dock, sides, "a"), true);
// The panel stays on the tasks tab, so the row being read stays too.
- assert.equal(visibleDockTab(dock, sidesFor(rows, todos), reading("a", dock)), "tasks");
+ assert.equal(visibleDockTab(dock, sides, isReading(dock, sides, "a")), "tasks");
// Choosing Todo — the one user action that ends the read — still does.
- assert.equal(reading("a", { ...dock, chosenTab: "todo" }), false);
+ assert.equal(isReading({ ...dock, chosenTab: "todo" }, sides, "a"), false);
});
test("regression: a finished row whose preview left the screen cannot reappear", () => {
@@ -112,14 +111,15 @@ test("regression: a finished row whose preview left the screen cannot reappear",
// start → open output: the preview is on screen, the row is pinned.
const rows = selectVisibleTasks([started], "a");
- assert.equal(reading("a", dock), true);
+ const sides = sidesFor(rows, todos);
+ assert.equal(isReading(dock, sides, "a"), true);
// → collapse (or switch to Todo): the preview is no longer on screen, so
// the hook closes the pin — the running task's pin survives, but it goes
// as soon as the task ends while hidden. That the hook closes it is pinned
// by the source contract test in background-tasks.test.mjs.
- assert.equal(reading("a", { ...dock, collapsed: true }), false);
- assert.equal(reading("a", { ...dock, chosenTab: "todo" }), false);
+ assert.equal(isReading({ ...dock, collapsed: true }, sides, "a"), false);
+ assert.equal(isReading({ ...dock, chosenTab: "todo" }, sides, "a"), false);
// → finish while hidden, then reopen: the ended row must not reappear.
const ended = { ...started, state: "exited", exitCode: 0 };
@@ -131,17 +131,19 @@ test("regression: a running task's preview survives switching away and back", ()
// task's log, switch to Todo and back — the read picks up where it left
// off (#2634).
const started = task("a", "running");
+ const todos = [{ status: "in_progress" }];
const dock = { chosenTab: "tasks", collapsed: false };
- assert.equal(reading("a", dock), true);
+ const sides = sidesFor(selectVisibleTasks([started], "a"), todos);
+ assert.equal(isReading(dock, sides, "a"), true);
// Switch to Todo: the preview leaves the screen, but the task runs, so the
// hook keeps the pin.
- assert.equal(reading("a", { ...dock, chosenTab: "todo" }), false);
+ assert.equal(isReading({ ...dock, chosenTab: "todo" }, sides, "a"), false);
assert.equal(started.state, "running");
// Switch back: the preview is on screen again, the row is live, and the
// same read continues.
const rows = selectVisibleTasks([started], "a");
assert.deepEqual(ids(rows), ["a"]);
- assert.equal(visibleDockTab(dock, sidesFor(rows, []), reading("a", dock)), "tasks");
+ assert.equal(visibleDockTab(dock, sides, isReading(dock, sides, "a")), "tasks");
});
diff --git a/apps/desktop/test/background-tasks-dock-state.test.mjs b/apps/desktop/test/background-tasks-dock-state.test.mjs
index 9a0ea471f..aab95fdcd 100644
--- a/apps/desktop/test/background-tasks-dock-state.test.mjs
+++ b/apps/desktop/test/background-tasks-dock-state.test.mjs
@@ -15,6 +15,7 @@ const {
EMPTY_SESSION_DOCK,
badgeLabel,
chooseTab,
+ isReading,
pruneDockView,
resolveDockTab,
resolveDockTabFor,
@@ -121,6 +122,37 @@ test("a row being read keeps the tasks panel on screen", () => {
assert.equal(resolveDockTabFor(onTodo, sides), "todo");
});
+test("the resolved panel, not the remembered choice, decides a read", () => {
+ // The follow-up regression: a user chose Todo, then removed every todo, so
+ // the panel fell back to Tasks without changing the choice. A read on that
+ // panel is a visible read and must survive the task ending (#2634).
+ const rememberedTodo = { ...EMPTY_SESSION_DOCK, chosenTab: "todo", collapsed: false };
+ const readRow = "task-1";
+ const openSides = { hasTodo: false, hasTasks: true, runningTasks: 1, inProgressTodos: 0 };
+ // Before the todos clear, the choice is live: the read is not on screen.
+ const withTodos = { hasTodo: true, hasTasks: true, runningTasks: 1, inProgressTodos: 1 };
+ assert.equal(isReading(rememberedTodo, withTodos, readRow), false);
+ // After the clear, the panel on screen is Tasks — through the task ending.
+ assert.equal(isReading(rememberedTodo, openSides, readRow), true);
+ const ended = { ...openSides, runningTasks: 0 };
+ assert.equal(isReading(rememberedTodo, ended, readRow), true);
+ // Todos coming back ends the read: the remembered Todo is on screen again.
+ assert.equal(isReading(rememberedTodo, withTodos, readRow), false);
+});
+
+test("an automatic tab never interrupts a read, a collapse always does", () => {
+ const sides = { hasTodo: true, hasTasks: true, runningTasks: 0, inProgressTodos: 1 };
+ // No choice recorded: the open read keeps Tasks on screen.
+ const auto = { ...EMPTY_SESSION_DOCK, collapsed: false };
+ assert.equal(isReading(auto, sides, "task-1"), true);
+ // A choice of Tasks reads as before.
+ const onTasks = { ...auto, chosenTab: "tasks" };
+ assert.equal(isReading(onTasks, sides, "task-1"), true);
+ // No open row, or a collapsed container: nothing on screen to read.
+ assert.equal(isReading(auto, sides, null), false);
+ assert.equal(isReading({ ...auto, collapsed: true }, sides, "task-1"), false);
+});
+
test("sides describe the rows as the tab rule sees them", () => {
const rows = [
{ state: "running" },
diff --git a/apps/desktop/test/background-tasks.test.mjs b/apps/desktop/test/background-tasks.test.mjs
index 6464e5523..45dcf801f 100644
--- a/apps/desktop/test/background-tasks.test.mjs
+++ b/apps/desktop/test/background-tasks.test.mjs
@@ -156,10 +156,12 @@ test("the container shows only live work: a finished row leaves as soon as it en
assert.match(visibility, /task\.id === pinnedId/);
assert.match(controller, /selectVisibleTasks\(sessionTasks, expandedId\)/);
// The pin on a row being read ends when its preview leaves the screen —
- // collapse, or a switch to Todo — unless the task still runs, whose
- // preview survives the switch and picks up the same read on return
- // (#2634).
- assert.match(controller, /const reading = expandedId !== null && !dock\.collapsed && dock\.chosenTab !== "todo"/);
+ // collapse, or a switch to a Todo panel that is actually visible — unless
+ // the task still runs, whose preview survives the switch and picks up the
+ // same read on return (#2634). The panel that resolves decides, not the
+ // remembered choice: a remembered Todo whose side emptied has already
+ // fallen back to Tasks.
+ assert.match(controller, /const reading = isReading\(dock, sides, expandedId\)/);
assert.match(controller, /if \(expandedId === null \|\| reading\) return;/);
assert.match(controller, /if \(pinnedTask\?\.state !== "running"\) setExpandedId\(null\)/);
// The effect re-runs on every session task snapshot: that is what closes
diff --git a/docs/spec/03-runtime/03-tools-and-permissions.md b/docs/spec/03-runtime/03-tools-and-permissions.md
index 9114271ad..79351932f 100644
--- a/docs/spec/03-runtime/03-tools-and-permissions.md
+++ b/docs/spec/03-runtime/03-tools-and-permissions.md
@@ -696,12 +696,15 @@ Host execution baseline:
Only the row whose output preview is open stays on screen, so a log being read
cannot vanish underneath it — and the auto tab rule never switches the panel
away from an open read (#2632, #2634). The preview pin ends when its preview
- leaves the screen — collapse, or a switch to Todo — unless the task still
- runs: a running task's preview survives the switch, so coming back to the
- tasks tab picks up the same read; a task that ends while hidden takes its
- pin with it, so a finished row cannot reappear later with no output on
- screen. A task that had already finished when the renderer first
- loaded is registry history and never appears.
+ leaves the screen — collapse, or the panel on screen becoming Todo — unless
+ the task still runs: a running task's preview survives the switch, so coming
+ back to the tasks tab picks up the same read; a task that ends while hidden
+ takes its pin with it, so a finished row cannot reappear later with no output
+ on screen. The panel that resolves decides, not the remembered choice: a
+ remembered Todo whose side emptied has already fallen back to Tasks, and a
+ read opened there is a visible read that survives the task ending. A task
+ that had already finished when the renderer first loaded is registry history
+ and never appears.
- **Removal is a view action with a live-task guard.** Only a terminal task
(`exited`, `failed`, `stopped`) can be removed — in practice the expanded row
of a task that just ended, the only ended row the container still shows; a
diff --git a/docs/spec/06-delivery/e2e/ui-shell.md b/docs/spec/06-delivery/e2e/ui-shell.md
index 7772f0a00..27c162307 100644
--- a/docs/spec/06-delivery/e2e/ui-shell.md
+++ b/docs/spec/06-delivery/e2e/ui-shell.md
@@ -32,13 +32,19 @@
- **Preconditions:** Isolated Electron renderer with synthetic todos and a running
task; production TaskDock, hook and store, with only host IPC stubbed.
- **Steps:** Expand the dock, select Todo, select Background tasks, open its log,
- switch to Todo and back. Repeat with completed todos.
+ switch to Todo and back. Repeat with completed todos. Then choose Todo, remove
+ every todo so the panel falls back to Background tasks without changing the
+ choice, open the log, and let the running task finish.
- **Expected:** Only the selected panel appears. Todo contains no task rows,
Stop control or log; Background tasks contains no checklist. The shared live
count remains visible, switching never sends Stop, and the open log survives.
+ A read opened on the panel a remembered Todo fell back to stays a visible
+ read: the log stays on screen through the task ending instead of being
+ dropped mid-read.
- **Entry:** `node scripts/e2e-task-dock-tabs.mjs`.
- **Specs:** `03-runtime/03-tools-and-permissions.md`, shared task container.
-- **Acceptance:** Both switch directions preserve task state and log selection.
+- **Acceptance:** Both switch directions preserve task state and log selection,
+ and a fallback-panel read survives the task ending.
- **Milestone:** Maintenance. **Status:** Automated renderer interaction test.
## E2E-SETTINGS-ai-tab-pickers-use-in-app-menus
diff --git a/scripts/e2e/task-dock-tabs.tsx b/scripts/e2e/task-dock-tabs.tsx
index 745f35129..1b200ab1e 100644
--- a/scripts/e2e/task-dock-tabs.tsx
+++ b/scripts/e2e/task-dock-tabs.tsx
@@ -19,12 +19,14 @@ async function until(predicate: () => boolean, message: string) {
}
const command = `# ${"long-argument-".repeat(30)}\n sleep 60 # end of command`;
let stops = 0;
+// The fixture task finishes when the #2634 follow-up scenario asks it to.
+let taskState: "running" | "exited" = "running";
Object.defineProperty(window, "piDesktop", { value: {
invoke: async (channel: string) => {
let data: unknown;
if (channel === IPC.invoke.backgroundTasksList) {
data = { tasks: [{ id: "task-1", sessionId: "fixture", command,
- commandShellId: "bash", state: "running", elapsedMs: 1000 }], version: 1 };
+ commandShellId: "bash", state: taskState, elapsedMs: 1000 }], version: 1 };
} else if (channel === IPC.invoke.backgroundTasksOutput) {
data = { id: "task-1", state: "running", stdout: "fixture log output", stderr: "", truncated: false };
} else if (channel === IPC.invoke.todosGet) {
@@ -82,8 +84,32 @@ async function probe() {
assertPanel(false);
assert(document.querySelector("pre")?.textContent?.includes("fixture log output"), "Open log must survive switching");
}
+ // #2634 follow-up: a remembered Todo whose side emptied falls back to
+ // Tasks without changing the choice. A read on that panel is a visible
+ // read, so it must survive the task ending.
+ flushSync(() => useAppStore.setState({ sessionTodos: { fixture: {
+ sessionId: "fixture", revision: 3, updatedAt: 3,
+ todos: [{ content: "fixture checklist item", status: "in_progress", priority: "high" }],
+ } } }));
+ select("Todo");
+ assertPanel(true);
+ flushSync(() => useAppStore.setState({ sessionTodos: { fixture: {
+ sessionId: "fixture", revision: 4, updatedAt: 4, todos: [],
+ } } }));
+ assertPanel(false);
+ const followRow = document.querySelector<HTMLElement>(".task-main");
+ assert(followRow, "Task row missing after the todo clear");
+ // The read from the previous scenario may still be open: close and reopen
+ // so the scenario opens its own read after the todo clear.
+ if (followRow.getAttribute("aria-expanded") === "true") flushSync(() => followRow.click());
+ assert(followRow.getAttribute("aria-expanded") !== "true", "Row must close before reopening");
+ flushSync(() => followRow.click());
+ await until(() => !!document.querySelector("pre")?.textContent?.includes("fixture log output"), "Log must load after the todo clear");
+ taskState = "exited";
+ await until(() => document.querySelector(".task-row")?.getAttribute("data-state") === "exited", "Task must finish");
+ await until(() => !!document.querySelector("pre")?.textContent?.includes("fixture log output"), "Open log must survive the task ending");
assert(stops === 0, "Switching tabs must never stop a task");
- return { ok: true, scenarios: 2 };
+ return { ok: true, scenarios: 3 };
} finally { flushSync(() => root.unmount()); }
}
Object.assign(globalThis, { taskDockTabsProbe: probe });