Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(desktop): close the output preview when the tasks panel hides
skylerc274574bc591
diff --git a/apps/desktop/src/features/chat/background-tasks/dock-state.ts b/apps/desktop/src/features/chat/background-tasks/dock-state.tsindex 30ec2ce90..5da41b986 100644--- a/apps/desktop/src/features/chat/background-tasks/dock-state.ts+++ b/apps/desktop/src/features/chat/background-tasks/dock-state.ts@@ -1,3 +1,6 @@+import type { BackgroundTaskSummary } from "../../../lib/api";+import type { SessionTodo } from "@pi-desktop/shared";+/*** View state for the shared Todo/background-task container.*@@ -96,6 +99,30 @@ export type DockSides = {inProgressTodos: number;};+/**+ * Both sides of the container, described the way the tab rule expects.+ *+ * The task side describes the rows the container can render, and the todo side+ * comes from the caller, which owns that read. The render rule and the badge+ * rule consume this one description, so they cannot disagree about which tasks+ * are live in the session.+ */+export function sidesFor(+ rows: readonly BackgroundTaskSummary[],+ todos: readonly SessionTodo[],+): DockSides {+ let runningTasks = 0;+ for (const task of rows) {+ if (task.state === "running") runningTasks += 1;+ }+ return {+ hasTodo: todos.length > 0,+ hasTasks: rows.length > 0,+ runningTasks,+ inProgressTodos: todos.filter((todo) => todo.status === "in_progress").length,+ };+}+export function resolveDockTabFor(state: DockSessionState, sides: DockSides): DockTab {return resolveDockTab(state,@@ -106,6 +133,18 @@ 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 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).+ */+export function tasksPanelVisible(state: DockSessionState, sides: DockSides): boolean {+ return !state.collapsed && resolveDockTabFor(state, sides) === "tasks";+}+/*** 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.tsindex 709c766a1..fa7f884be 100644--- a/apps/desktop/src/features/chat/background-tasks/useBackgroundTasks.ts+++ b/apps/desktop/src/features/chat/background-tasks/useBackgroundTasks.ts@@ -2,7 +2,6 @@ import { useCallback, useEffect, useMemo, useRef, useState } from "react";import { api, type BackgroundTaskOutput, type BackgroundTaskSummary } from "../../../lib/api";import { selectVisibleTasks } from "./bar-visibility";import {- type DockSides,EMPTY_DOCK_VIEW,type DockTab,type DockViewState,@@ -11,6 +10,8 @@ import {resolveDockTabFor,selectTab,sessionDockState,+ sidesFor,+ tasksPanelVisible,toggleCollapsed,} from "./dock-state";import type { SessionTodo } from "@pi-desktop/shared";@@ -18,29 +19,6 @@ import type { SessionTodo } from "@pi-desktop/shared";const REFRESH_MS = 1_000;type Failure = "load" | "output" | "stop" | "remove";-/**- * Both sides of the container, described the way the tab rule expects.- *- * The task side describes the rows the container can render, and the todo side- * comes from the caller, which owns that read. The render rule and the badge- * rule consume this one description, so they cannot disagree about which tasks- * are live in the session.- */-function sidesFor(- rows: readonly BackgroundTaskSummary[],- todos: readonly SessionTodo[],-): DockSides {- let runningTasks = 0;- for (const task of rows) {- if (task.state === "running") runningTasks += 1;- }- return {- hasTodo: todos.length > 0,- hasTasks: rows.length > 0,- runningTasks,- inProgressTodos: todos.filter((todo) => todo.status === "in_progress").length,- };-}/*** Background tasks for one session, plus the shared container's view state.*@@ -245,10 +223,30 @@ export function useBackgroundTasks(// 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);// 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.+ useEffect(() => {+ if (!reading) setExpandedId(null);+ }, [reading, expandedId]);++ // A preview belongs to the session it was opened in; a session switch+ // closes it.+ useEffect(() => {+ setExpandedId(null);+ }, [sessionId]);+return {/** The session's rows the container may render: live, or being read. */tasks: visibleTasks,diff --git a/apps/desktop/test/background-tasks-bar-visibility.test.mjs b/apps/desktop/test/background-tasks-bar-visibility.test.mjsindex 57a1339e0..47bd0e1ed 100644--- a/apps/desktop/test/background-tasks-bar-visibility.test.mjs+++ b/apps/desktop/test/background-tasks-bar-visibility.test.mjs@@ -11,6 +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 task = (id, state, extra = {}) => ({id,@@ -59,3 +60,49 @@ test("a running task is visible whether or not its output is open", () => {assert.deepEqual(ids(selectVisibleTasks([running])), ["a"]);assert.deepEqual(ids(selectVisibleTasks([running], "a")), ["a"]);});++/*+ * 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).+ */++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);++ 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);+});++test("regression: a finished row whose preview left the screen cannot reappear", () => {+ const todos = [{ status: "in_progress" }];+ const started = task("a", "running");+ const dock = { chosenTab: "tasks", collapsed: false };++ // 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);++ // → 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.+ const ended = { ...started, state: "exited", exitCode: 0 };+ assert.deepEqual(selectVisibleTasks([ended], null), []);+});diff --git a/apps/desktop/test/background-tasks-dock-state.test.mjs b/apps/desktop/test/background-tasks-dock-state.test.mjsindex e06fea61e..e9bf06333 100644--- a/apps/desktop/test/background-tasks-dock-state.test.mjs+++ b/apps/desktop/test/background-tasks-dock-state.test.mjs@@ -20,6 +20,8 @@ const {resolveDockTabFor,selectTab,sessionDockState,+ sidesFor,+ tasksPanelVisible,toggleCollapsed,} = await import("../src/features/chat/background-tasks/dock-state.ts");@@ -104,6 +106,32 @@ 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("sides describe the rows as the tab rule sees them", () => {+ const rows = [+ { state: "running" },+ { state: "exited" },+ { state: "exited" },+ ];+ const todos = [{ status: "in_progress" }, { status: "completed" }];+ const sides = sidesFor(rows, todos);+ assert.equal(sides.hasTasks, true);+ assert.equal(sides.runningTasks, 1, "only a running row counts as live work");+ assert.equal(sides.inProgressTodos, 1);+});+test("collapse is one container-wide condition, both tabs share it", () => {const opened = toggleCollapsed(EMPTY_DOCK_VIEW);assert.equal(opened.collapsed, false);diff --git a/apps/desktop/test/background-tasks.test.mjs b/apps/desktop/test/background-tasks.test.mjsindex 532c0ebf9..29a82b098 100644--- a/apps/desktop/test/background-tasks.test.mjs+++ b/apps/desktop/test/background-tasks.test.mjs@@ -155,6 +155,13 @@ test("the container shows only live work: a finished row leaves as soon as it enassert.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\)/);+ // 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// the control.assert.match(controller, /if \(!task \|\| task\.state === "running" \|\| task\.continuation === "pending"\)/);@@ -170,6 +177,9 @@ test("the container filters to the active session and badges its running work",// The badge is the running-row count, derived from the rows — never view// state a tab click can clear (#2632).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"/);});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.mdindex f071176fb..b20135d04 100644--- a/docs/spec/03-runtime/03-tools-and-permissions.md+++ b/docs/spec/03-runtime/03-tools-and-permissions.md@@ -694,8 +694,11 @@ Host execution baseline:- A finished background task leaves the container as soon as its end statearrives: 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 (#2632). A task that had already finished when- the renderer first loaded is registry history and never appears.+ 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+ 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 rowof a task that just ended, the only ended row the container still shows; adiff --git a/docs/spec/06-delivery/e2e/tools-and-workspace.md b/docs/spec/06-delivery/e2e/tools-and-workspace.mdindex 1cae3b9a6..0454da5ba 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 evenbefore 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 the tab badge falls with+ 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 withthe running count, so finished commands do not accumulate above the- composer (#2632).+ composer (#2632, #2634).- **Specs linked**: `03-runtime/03-tools-and-permissions.md` (§5),`03-runtime/06-host-rpc-protocol.md` (§4)- **Acceptance criterion**: C, E, F