Skip to content

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

fix(desktop): keep an open read alive across panel switches

skylered05f5899ed2
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 5da41b986..0540f5b26 100644
--- a/apps/desktop/src/features/chat/background-tasks/dock-state.ts
+++ b/apps/desktop/src/features/chat/background-tasks/dock-state.ts
@@ -134,15 +134,20 @@ export function resolveDockTabFor(state: DockSessionState, sides: DockSides): Do
}
/**
- * Whether the tasks panel is the one on screen. A collapsed container shows no
- * panel at all, so it never counts as looking at the tasks.
+ * The tab actually on screen.
*
- * The hook keeps the output-preview pin alive only while this holds: a row
- * being read stays only while its preview is actually on screen, so collapsing
- * the container or switching to Todo ends the read (#2632, #2634).
+ * A row being read keeps the tasks panel on screen: the auto tab rule does not
+ * switch to Todo underneath an open preview, so the last running task ending
+ * mid-read does not pull the panel away. A user tab choice still decides —
+ * choosing Todo is what ends a read (#2634).
*/
-export function tasksPanelVisible(state: DockSessionState, sides: DockSides): boolean {
- return !state.collapsed && resolveDockTabFor(state, sides) === "tasks";
+export function visibleDockTab(
+ state: DockSessionState,
+ sides: DockSides,
+ reading: boolean,
+): DockTab {
+ if (reading) return "tasks";
+ return resolveDockTabFor(state, sides);
}
/**
diff --git a/apps/desktop/src/features/chat/background-tasks/useBackgroundTasks.ts b/apps/desktop/src/features/chat/background-tasks/useBackgroundTasks.ts
index fa7f884be..696c01083 100644
--- a/apps/desktop/src/features/chat/background-tasks/useBackgroundTasks.ts
+++ b/apps/desktop/src/features/chat/background-tasks/useBackgroundTasks.ts
@@ -7,12 +7,11 @@ import {
type DockViewState,
badgeLabel,
pruneDockView,
- resolveDockTabFor,
selectTab,
sessionDockState,
sidesFor,
- tasksPanelVisible,
toggleCollapsed,
+ visibleDockTab,
} from "./dock-state";
import type { SessionTodo } from "@pi-desktop/shared";
@@ -222,24 +221,27 @@ 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);
- const activeTab: DockTab = resolveDockTabFor(dock, sides);
- // The tasks panel on screen is the one place a preview is visible; anywhere
- // else — collapsed, or the Todo tab — the read is over (#2634).
- const reading = tasksPanelVisible(dock, sides);
+ // 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";
+ 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 lives only while its preview is on screen:
- // once the panel hides — collapse, or a switch to Todo — the preview
- // closes, so a finished row cannot reappear later with no output on screen
- // (#2634). While the panel stays on screen, the row persists until the
- // reader closes it. `expandedId` is a dependency so a pin written while the
- // panel is hidden (the hidden panel is still keyboard-focusable) is cleared
- // too.
+ // 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.
useEffect(() => {
- if (!reading) setExpandedId(null);
- }, [reading, expandedId]);
+ if (expandedId === null || reading) return;
+ const pinnedTask = sessionTasks.find((task) => task.id === expandedId);
+ if (pinnedTask?.state !== "running") setExpandedId(null);
+ }, [reading, expandedId, sessionTasks]);
// A preview belongs to the session it was opened in; a session switch
// closes it.
diff --git a/apps/desktop/test/background-tasks-bar-visibility.test.mjs b/apps/desktop/test/background-tasks-bar-visibility.test.mjs
index 47bd0e1ed..b9d8920c5 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, tasksPanelVisible } = await import("../src/features/chat/background-tasks/dock-state.ts");
+const { sidesFor, visibleDockTab } = await import("../src/features/chat/background-tasks/dock-state.ts");
const task = (id, state, extra = {}) => ({
id,
@@ -62,25 +62,47 @@ test("a running task is visible whether or not its output is open", () => {
});
/*
- * The pin on a row being read lives only while its preview is on screen.
- * The pieces the hook composes are pure, so the regression drives them the
- * way the hook does: the rows carry the pin, the sides describe them, and
- * `tasksPanelVisible` decides whether the preview is on screen; when it is
- * not, the hook closes the preview (`expandedId` becomes null) (#2634).
+ * 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
+ * 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(tasksPanelVisible(dock, sidesFor(rows, [])), true);
+ assert.equal(visibleDockTab(dock, sidesFor(rows, []), reading("a", dock)), "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.
- assert.equal(tasksPanelVisible(dock, sidesFor(selectVisibleTasks([ended], "a"), [])), true);
+ const pinnedRows = selectVisibleTasks([ended], "a");
+ assert.equal(visibleDockTab(dock, sidesFor(pinnedRows, []), reading("a", dock)), "tasks");
+});
+
+test("regression: the auto tab rule does not take the panel from an open read", () => {
+ // A user who never chose a tab, with an in-progress todo, reads the only
+ // running task's output; the task ends mid-read.
+ const todos = [{ status: "in_progress" }];
+ const dock = { chosenTab: null, collapsed: false };
+ const running = task("a", "running");
+ assert.equal(visibleDockTab(dock, sidesFor([running], todos), false), "tasks");
+
+ const ended = { ...running, state: "exited", exitCode: 0 };
+ const rows = selectVisibleTasks([ended], "a");
+ assert.equal(reading("a", dock), 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");
+ // Choosing Todo — the one user action that ends the read — still does.
+ assert.equal(reading("a", { ...dock, chosenTab: "todo" }), false);
});
test("regression: a finished row whose preview left the screen cannot reappear", () => {
@@ -90,19 +112,36 @@ 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(tasksPanelVisible(dock, sidesFor(rows, todos)), true);
+ assert.equal(reading("a", dock), true);
// → collapse (or switch to Todo): the preview is no longer on screen, so
- // the hook closes it — `expandedId` becomes null before the task ends.
- assert.equal(tasksPanelVisible({ ...dock, collapsed: true }, sidesFor(rows, todos)), false);
- assert.equal(
- tasksPanelVisible({ ...dock, chosenTab: "todo" }, sidesFor(rows, todos)),
- false,
- );
-
- // → finish, then reopen: the ended row must not reappear. That the hook
- // really closes the preview when the panel hides is asserted by the source
- // contract test in background-tasks.test.mjs.
+ // 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);
+
+ // → finish while hidden, then reopen: the ended row must not reappear.
const ended = { ...started, state: "exited", exitCode: 0 };
assert.deepEqual(selectVisibleTasks([ended], null), []);
});
+
+test("regression: a running task's preview survives switching away and back", () => {
+ // The E2E scenario E2E-TASK-DOCK-tab-content-isolation: open a running
+ // task's log, switch to Todo and back — the read picks up where it left
+ // off (#2634).
+ const started = task("a", "running");
+ const dock = { chosenTab: "tasks", collapsed: false };
+ assert.equal(reading("a", dock), 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(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");
+});
diff --git a/apps/desktop/test/background-tasks-dock-state.test.mjs b/apps/desktop/test/background-tasks-dock-state.test.mjs
index e9bf06333..9a0ea471f 100644
--- a/apps/desktop/test/background-tasks-dock-state.test.mjs
+++ b/apps/desktop/test/background-tasks-dock-state.test.mjs
@@ -21,8 +21,8 @@ const {
selectTab,
sessionDockState,
sidesFor,
- tasksPanelVisible,
toggleCollapsed,
+ visibleDockTab,
} = await import("../src/features/chat/background-tasks/dock-state.ts");
const SESSION = "session-1";
@@ -106,17 +106,19 @@ test("one populated side renders alone, so the tab list is not rendered", () =>
assert.equal(sides.hasTodo && sides.hasTasks, false);
});
-test("the tasks panel is on screen only when expanded and selected", () => {
- // This decides whether a preview can be on screen at all: the pin on a
- // row being read lives only while this holds (#2634).
- const sides = { hasTodo: true, hasTasks: true, runningTasks: 1, inProgressTodos: 1 };
- const open = { ...EMPTY_SESSION_DOCK, collapsed: false };
- assert.equal(tasksPanelVisible(open, sides), true);
- // A collapsed container shows no panel at all.
- assert.equal(tasksPanelVisible(EMPTY_SESSION_DOCK, sides), false);
- // The todo panel on screen is not the tasks panel either.
- const onTodo = { ...open, chosenTab: "todo" };
- assert.equal(tasksPanelVisible(onTodo, sides), false);
+test("a row being read keeps the tasks panel on screen", () => {
+ // Without this guard the auto tab rule switches to Todo when the last
+ // running task ends, taking the panel — and the row being read — away
+ // mid-read (#2634).
+ const sides = { hasTodo: true, hasTasks: true, runningTasks: 0, inProgressTodos: 1 };
+ const auto = { ...EMPTY_SESSION_DOCK, collapsed: false };
+ assert.equal(resolveDockTabFor(auto, sides), "todo");
+ assert.equal(visibleDockTab(auto, sides, true), "tasks");
+ // Not reading: the auto rule decides as before.
+ assert.equal(visibleDockTab(auto, sides, false), "todo");
+ // A user tab choice still decides — reading never overrides Todo.
+ const onTodo = { ...auto, chosenTab: "todo" };
+ assert.equal(resolveDockTabFor(onTodo, sides), "todo");
});
test("sides describe the rows as the tab rule sees them", () => {
diff --git a/apps/desktop/test/background-tasks.test.mjs b/apps/desktop/test/background-tasks.test.mjs
index 29a82b098..6464e5523 100644
--- a/apps/desktop/test/background-tasks.test.mjs
+++ b/apps/desktop/test/background-tasks.test.mjs
@@ -155,11 +155,16 @@ test("the container shows only live work: a finished row leaves as soon as it en
assert.match(visibility, /task\.continuation === "pending"/);
assert.match(visibility, /task\.id === pinnedId/);
assert.match(controller, /selectVisibleTasks\(sessionTasks, expandedId\)/);
- // The pin on a row being read lives only while its preview is on screen:
- // collapse or a switch to Todo closes the preview, so the row cannot
- // reappear later with no output on screen (#2634).
- assert.match(controller, /const reading = tasksPanelVisible\(dock, sides\)/);
- assert.match(controller, /if \(!reading\) setExpandedId\(null\)/);
+ // 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"/);
+ 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
+ // the pin when a running task ends while its panel is hidden.
+ assert.match(controller, /\}, \[reading, expandedId, sessionTasks\]\)/);
// A session switch closes the preview too.
assert.match(controller, /useEffect\(\(\) => \{\n setExpandedId\(null\);\n \}, \[sessionId\]\)/);
// Removal is refused for a live task in the state layer, not just by hiding
@@ -179,7 +184,10 @@ test("the container filters to the active session and badges its running work",
assert.match(controller, /const badge = sides\.runningTasks/);
// Both sides and the panel rule come from dock-state, so the render rule,
// the badge rule, and the preview rule share one tab decision.
- assert.match(controller, /import \{[\s\S]*?sidesFor,[\s\S]*?tasksPanelVisible,[\s\S]*?\} from "\.\/dock-state"/);
+ assert.match(controller, /import \{[\s\S]*?sidesFor,[\s\S]*?visibleDockTab,[\s\S]*?\} from "\.\/dock-state"/);
+ // A row being read keeps the tasks panel on screen: the auto tab rule does
+ // not switch to Todo underneath an open preview (#2634).
+ assert.match(controller, /visibleDockTab\(dock, sides, reading\)/);
});
test("deleting a session signals its tasks from the shared cleanup path only", () => {
diff --git a/docs/spec/03-runtime/03-tools-and-permissions.md b/docs/spec/03-runtime/03-tools-and-permissions.md
index b20135d04..9114271ad 100644
--- a/docs/spec/03-runtime/03-tools-and-permissions.md
+++ b/docs/spec/03-runtime/03-tools-and-permissions.md
@@ -694,10 +694,13 @@ Host execution baseline:
- A finished background task leaves the container as soon as its end state
arrives: the badge falls and the row goes, with no post-exit grace window.
Only the row whose output preview is open stays on screen, so a log being read
- cannot vanish underneath it — and the preview pin lives only while the tasks
- panel is on screen: collapsing the container or switching to Todo closes the
- preview, so a finished row cannot reappear later with no output on screen
- (#2632, #2634). A task that had already finished when the renderer first
+ 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.
- **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
diff --git a/docs/spec/06-delivery/e2e/tools-and-workspace.md b/docs/spec/06-delivery/e2e/tools-and-workspace.md
index 0454da5ba..dc9f641d6 100644
--- a/docs/spec/06-delivery/e2e/tools-and-workspace.md
+++ b/docs/spec/06-delivery/e2e/tools-and-workspace.md
@@ -1272,9 +1272,11 @@ journeys cover successful reads, saves and directory browsing.
command above the output, preserving whitespace and wrapping to fit even
before the task produces any output.
An ended task's row leaves the bar as soon as its end state arrives — only
- the row whose output preview is open stays, and only while the tasks panel
- is on screen (collapsing the container or switching to Todo closes the
- preview, so the row cannot reappear later) — and the tab badge falls with
+ the row whose output preview is open stays (the auto tab rule never switches
+ the panel away from an open read), and the 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, while a task that ends while
+ hidden takes its row with it — and the tab badge falls with
the running count, so finished commands do not accumulate above the
composer (#2632, #2634).
- **Specs linked**: `03-runtime/03-tools-and-permissions.md` (§5),