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(composer): keep finished background tasks out of the composer bar
vastsae6cf13ae86de
diff --git a/apps/desktop/src/components/BackgroundTasksBar.tsx b/apps/desktop/src/components/BackgroundTasksBar.tsxindex 5dbd25016..16d838121 100644--- a/apps/desktop/src/components/BackgroundTasksBar.tsx+++ b/apps/desktop/src/components/BackgroundTasksBar.tsx@@ -11,11 +11,17 @@ const NO_OUTPUT = "—";* Shows chat-started background commands (dev servers, watchers) above the* composer. The lifecycle lives in host-core — a task survives turn aborts* and renderer reloads; this bar only reflects registry state and can stop a- * task on request. Every task is listed; running ones float to the top.+ * task on request.+ *+ * Only work that is still running, or that the renderer just watched finish,+ * earns a row here: a command that ended long ago is history, not something+ * the user is waiting on, and it must not sit above the composer forever.+ * Running tasks float to the top.*/export function BackgroundTasksBar(): React.JSX.Element | null {const { t } = useTranslation();- const { tasks, expandedId, output, stoppingIds, error, toggleOutput, stop } = useBackgroundTasks();+ const { tasks, expandedId, output, stoppingIds, error, toggleOutput, stop, dismiss } =+ useBackgroundTasks();const outputText = useMemo(() => formatOutput(output, expandedId ?? ""),[output, expandedId],@@ -77,7 +83,17 @@ export function BackgroundTasksBar(): React.JSX.Element | null {>{stopping ? t("backgroundTasks.stopping") : t("backgroundTasks.stop")}</Button>- ) : null}+ ) : (+ <Button+ type="button"+ variant="ghost"+ size="sm"+ className="background-task-action"+ onClick={() => dismiss(task.id)}+ >+ {t("backgroundTasks.dismiss")}+ </Button>+ )}{expandedId === task.id ? (<pre className="background-task-output">{outputText || NO_OUTPUT}diff --git a/apps/desktop/src/features/chat/background-tasks/bar-visibility.ts b/apps/desktop/src/features/chat/background-tasks/bar-visibility.tsnew file mode 100644index 000000000..f8e30bd72--- /dev/null+++ b/apps/desktop/src/features/chat/background-tasks/bar-visibility.ts@@ -0,0 +1,140 @@+import type { BackgroundTaskSummary } from "../../../lib/api";++/**+ * How long a finished task stays in the bar after the renderer watched it+ * running. Long enough to read the exit code, short enough that a command that+ * completed minutes ago does not become a permanent resident above the+ * composer.+ */+export const ENDED_VISIBLE_MS = 10_000;++/** Visibility bookkeeping owned by the renderer, never persisted or sent. */+export type BarVisibility = {+ /** Tasks observed as running at least once in this renderer session. */+ seenRunning: ReadonlySet<string>;+ /** Subset of `seenRunning` that has finished since, with the observation time. */+ endedAt: ReadonlyMap<string, number>;+ /** Tasks the user closed from the bar. */+ dismissed: ReadonlySet<string>;+};++export const EMPTY_VISIBILITY: BarVisibility = {+ seenRunning: new Set(),+ endedAt: new Map(),+ dismissed: new Set(),+};++function pruneSet(source: ReadonlySet<string>, liveIds: ReadonlySet<string>): Set<string> {+ const next = new Set<string>();+ for (const id of source) {+ if (liveIds.has(id)) next.add(id);+ }+ return next;+}++function pruneMap(+ source: ReadonlyMap<string, number>,+ liveIds: ReadonlySet<string>,+): Map<string, number> {+ const next = new Map<string, number>();+ for (const [id, at] of source) {+ if (liveIds.has(id)) next.set(id, at);+ }+ return next;+}++/**+ * Fold one registry snapshot into the visibility bookkeeping.+ *+ * A task that is already finished the first time the renderer sees it is+ * registry history — it may have ended before the window opened — so it never+ * becomes visible. Only a task observed running gets the post-exit grace+ * window. Entries for tasks the registry dropped are pruned, so the+ * bookkeeping cannot grow across a long session.+ *+ * Returns the previous state when nothing changed, so callers can skip work.+ */+export function observeTasks(+ state: BarVisibility,+ tasks: readonly BackgroundTaskSummary[],+ now: number,+): BarVisibility {+ const liveIds = new Set(tasks.map((task) => task.id));+ const seenRunning = pruneSet(state.seenRunning, liveIds);+ const endedAt = pruneMap(state.endedAt, liveIds);+ const dismissed = pruneSet(state.dismissed, liveIds);+ let changed =+ seenRunning.size !== state.seenRunning.size ||+ endedAt.size !== state.endedAt.size ||+ dismissed.size !== state.dismissed.size;++ for (const task of tasks) {+ if (task.state === "running") {+ if (!seenRunning.has(task.id)) {+ seenRunning.add(task.id);+ changed = true;+ }+ if (endedAt.delete(task.id)) changed = true;+ continue;+ }+ if (seenRunning.has(task.id) && !endedAt.has(task.id)) {+ endedAt.set(task.id, now);+ changed = true;+ }+ }++ if (!changed) return state;+ return { seenRunning, endedAt, dismissed };+}++/**+ * Tasks the bar may render right now: running ones always, finished ones only+ * inside the grace window or while the user is reading their output+ * (`pinnedId`), and never ones the user closed.+ */+export function selectVisibleTasks(+ state: BarVisibility,+ tasks: readonly BackgroundTaskSummary[],+ now: number,+ pinnedId: string | null = null,+): BackgroundTaskSummary[] {+ return tasks.filter((task) => {+ if (state.dismissed.has(task.id)) return false;+ if (task.state === "running") return true;+ // Reading the log of a finished command must not be interrupted by the+ // grace window expiring underneath it.+ if (task.id === pinnedId) return true;+ const ended = state.endedAt.get(task.id);+ return ended !== undefined && now - ended < ENDED_VISIBLE_MS;+ });+}++/**+ * When the next grace window closes, or `null` when no visible row is about to+ * expire. The caller schedules a single timer for this instant instead of+ * polling for the bar to shrink.+ */+export function nextExpiryAt(+ state: BarVisibility,+ tasks: readonly BackgroundTaskSummary[],+ now: number,+ pinnedId: string | null = null,+): number | null {+ let earliest: number | null = null;+ for (const task of selectVisibleTasks(state, tasks, now, pinnedId)) {+ if (task.id === pinnedId) continue;+ const ended = state.endedAt.get(task.id);+ if (ended === undefined) continue;+ const expiry = ended + ENDED_VISIBLE_MS;+ if (earliest === null || expiry < earliest) earliest = expiry;+ }+ return earliest;+}++/** Hide one row from the bar. Runs nowhere near the process lifecycle. */+export function dismissTask(state: BarVisibility, id: string): BarVisibility {+ if (state.dismissed.has(id)) return state;+ const dismissed = new Set(state.dismissed);+ dismissed.add(id);+ return { ...state, dismissed };+}diff --git a/apps/desktop/src/features/chat/background-tasks/useBackgroundTasks.ts b/apps/desktop/src/features/chat/background-tasks/useBackgroundTasks.tsindex 6fd6f7b2f..e7cbc7277 100644--- a/apps/desktop/src/features/chat/background-tasks/useBackgroundTasks.ts+++ b/apps/desktop/src/features/chat/background-tasks/useBackgroundTasks.ts@@ -1,11 +1,23 @@import { useCallback, useEffect, useRef, useState } from "react";import { api, type BackgroundTaskOutput, type BackgroundTaskSummary } from "../../../lib/api";+import {+ type BarVisibility,+ EMPTY_VISIBILITY,+ dismissTask,+ nextExpiryAt,+ observeTasks,+ selectVisibleTasks,+} from "./bar-visibility";const REFRESH_MS = 1_000;type Failure = "load" | "output" | "stop";export function useBackgroundTasks() {const [tasks, setTasks] = useState<BackgroundTaskSummary[]>([]);+ // Renderer-local view state: which tasks the bar may show. The registry in+ // host-core stays the source of truth for the processes themselves.+ const [visibility, setVisibility] = useState<BarVisibility>(EMPTY_VISIBILITY);+ const [clock, setClock] = useState(() => Date.now());const [expandedId, setExpandedId] = useState<string | null>(null);const [output, setOutput] = useState<BackgroundTaskOutput | null>(null);const [stoppingIds, setStoppingIds] = useState<Set<string>>(() => new Set());@@ -31,7 +43,10 @@ export function useBackgroundTasks() {try {const result = await api.backgroundTasksList();if (disposed) return;+ const observedAt = Date.now();setTasks(result.tasks);+ setVisibility((current) => observeTasks(current, result.tasks, observedAt));+ setClock(observedAt);setError((current) => current === "load" ? null : current);pollAgain = result.tasks.some((task) => task.state === "running");} catch (failure) {@@ -63,6 +78,17 @@ export function useBackgroundTasks() {};}, []);+ // Only what the bar should render: running tasks, a finished task during its+ // post-exit grace window, and the row whose output the user is reading.+ const visibleTasks = selectVisibleTasks(visibility, tasks, clock, expandedId);++ useEffect(() => {+ const expiry = nextExpiryAt(visibility, tasks, clock, expandedId);+ if (expiry === null) return;+ const timer = setTimeout(() => setClock(Date.now()), Math.max(0, expiry - clock));+ return () => clearTimeout(timer);+ }, [visibility, tasks, clock, expandedId]);+const selectedState = tasks.find((task) => task.id === expandedId)?.state;useEffect(() => {setOutput(null);@@ -98,6 +124,11 @@ export function useBackgroundTasks() {setError((current) => current === "output" ? null : current);}, []);+ const dismiss = useCallback((id: string) => {+ setVisibility((current) => dismissTask(current, id));+ setExpandedId((current) => current === id ? null : current);+ }, []);+const stop = useCallback(async (id: string) => {if (pendingStops.current.has(id)) return;pendingStops.current.add(id);@@ -117,5 +148,14 @@ export function useBackgroundTasks() {}}, []);- return { tasks, expandedId, output, stoppingIds, error, toggleOutput, stop };+ return {+ tasks: visibleTasks,+ expandedId,+ output,+ stoppingIds,+ error,+ toggleOutput,+ stop,+ dismiss,+ };}diff --git a/apps/desktop/test/background-tasks-bar-visibility.test.mjs b/apps/desktop/test/background-tasks-bar-visibility.test.mjsnew file mode 100644index 000000000..55a490c2e--- /dev/null+++ b/apps/desktop/test/background-tasks-bar-visibility.test.mjs@@ -0,0 +1,112 @@+import assert from "node:assert/strict";+import { register } from "node:module";+import test from "node:test";++/*+ * Background-tasks bar visibility. The registry in host-core keeps finished+ * tasks as history; the bar must not turn that history into permanent rows+ * above the composer. These tests pin the renderer-side rule with a controlled+ * clock, so "did this row disappear yet" is decided by explicit timestamps+ * instead of wall time.+ */++register(new URL("./helpers/ts-import-hooks.mjs", import.meta.url));+const {+ ENDED_VISIBLE_MS,+ EMPTY_VISIBILITY,+ dismissTask,+ nextExpiryAt,+ observeTasks,+ selectVisibleTasks,+} = await import("../src/features/chat/background-tasks/bar-visibility.ts");++const task = (id, state, exitCode) => ({+ id,+ sessionId: "session-1",+ command: `cmd ${id}`,+ commandShellId: "bash",+ state,+ elapsedMs: 1_000,+ exitCode,+});++const ids = (tasks) => tasks.map((entry) => entry.id);++test("a task that is already finished when first observed never gets a row", () => {+ const finished = [task("xcodebuild", "exited", 0), task("go-vet", "exited", 0)];+ const state = observeTasks(EMPTY_VISIBILITY, finished, 1_000);++ assert.deepEqual(selectVisibleTasks(state, finished, 1_000), []);+ assert.equal(nextExpiryAt(state, finished, 1_000), null);+});++test("a task observed running shows, keeps a grace window after exit, then leaves", () => {+ const running = [task("a", "running")];+ let state = observeTasks(EMPTY_VISIBILITY, running, 1_000);+ assert.deepEqual(ids(selectVisibleTasks(state, running, 1_000)), ["a"]);++ const ended = [task("a", "exited", 0)];+ state = observeTasks(state, ended, 5_000);+ assert.deepEqual(ids(selectVisibleTasks(state, ended, 5_000)), ["a"]);+ assert.deepEqual(ids(selectVisibleTasks(state, ended, 5_000 + ENDED_VISIBLE_MS - 1)), ["a"]);+ assert.deepEqual(selectVisibleTasks(state, ended, 5_000 + ENDED_VISIBLE_MS), []);+});++test("a row the user closed stays closed across later snapshots", () => {+ let state = observeTasks(EMPTY_VISIBILITY, [task("a", "running")], 0);+ const ended = [task("a", "exited", 0)];+ state = observeTasks(state, ended, 100);+ assert.equal(selectVisibleTasks(state, ended, 100).length, 1);++ state = dismissTask(state, "a");+ assert.deepEqual(selectVisibleTasks(state, ended, 100), []);+ state = observeTasks(state, ended, 200);+ assert.deepEqual(selectVisibleTasks(state, ended, 200), []);+});++test("the row whose output is open is not pulled away by the grace window", () => {+ let state = observeTasks(EMPTY_VISIBILITY, [task("a", "running")], 0);+ const ended = [task("a", "exited", 0)];+ state = observeTasks(state, ended, 100);+ const muchLater = 100 + ENDED_VISIBLE_MS * 3;++ assert.deepEqual(ids(selectVisibleTasks(state, ended, muchLater, "a")), ["a"]);+ assert.equal(nextExpiryAt(state, ended, muchLater, "a"), null);+});++test("running tasks never expire and a dismissed running task stays hidden", () => {+ const running = [task("server", "running")];+ let state = observeTasks(EMPTY_VISIBILITY, running, 0);+ assert.equal(nextExpiryAt(state, running, 10 * ENDED_VISIBLE_MS), null);++ state = dismissTask(state, "server");+ assert.deepEqual(selectVisibleTasks(state, running, 0), []);+});++test("expiry reports the earliest deadline among the rows still visible", () => {+ let state = observeTasks(EMPTY_VISIBILITY, [task("a", "running"), task("b", "running")], 0);+ state = observeTasks(state, [task("a", "exited", 0), task("b", "running")], 1_000);+ const both = [task("a", "exited", 0), task("b", "exited", 0)];+ state = observeTasks(state, both, 2_000);++ assert.equal(nextExpiryAt(state, both, 2_000), 1_000 + ENDED_VISIBLE_MS);+ assert.deepEqual(ids(selectVisibleTasks(state, both, 1_000 + ENDED_VISIBLE_MS)), ["b"]);+ assert.equal(nextExpiryAt(state, both, 1_000 + ENDED_VISIBLE_MS), 2_000 + ENDED_VISIBLE_MS);+});++test("bookkeeping is pruned when the registry drops a task, so it cannot grow", () => {+ let state = observeTasks(EMPTY_VISIBILITY, [task("a", "running"), task("b", "running")], 0);+ state = dismissTask(state, "a");+ assert.deepEqual([...state.seenRunning].sort(), ["a", "b"]);++ state = observeTasks(state, [task("b", "running")], 10);+ assert.deepEqual([...state.seenRunning], ["b"]);+ assert.deepEqual([...state.dismissed], []);+ assert.equal(state.endedAt.size, 0);+});++test("an unchanged snapshot keeps the previous state object", () => {+ const running = [task("a", "running")];+ const state = observeTasks(EMPTY_VISIBILITY, running, 0);+ assert.equal(observeTasks(state, running, 500), state);+});diff --git a/apps/desktop/test/background-tasks.test.mjs b/apps/desktop/test/background-tasks.test.mjsindex 1653be781..9ed9f75fd 100644--- a/apps/desktop/test/background-tasks.test.mjs+++ b/apps/desktop/test/background-tasks.test.mjs@@ -131,3 +131,20 @@ test("the bar styles exist and mark a running task with a pulse", () => {test("the running/stopped/failed/exited labels are translated", () => {assert.match(i18nEn, /backgroundTasks: \{[^}]*running: "Running"/);});++test("the bar shows only live work: a finished row expires and can be closed", async () => {+ const visibility = await readFile(+ new URL("../src/features/chat/background-tasks/bar-visibility.ts", import.meta.url),+ "utf8",+ );+ // The grace window is a named constant, not a magic number in the component.+ assert.match(visibility, /export const ENDED_VISIBLE_MS = 10_000;/);+ assert.match(controller, /selectVisibleTasks\(visibility, tasks, clock, expandedId\)/);+ assert.match(controller, /observeTasks\(current, result\.tasks, observedAt\)/);+ assert.match(controller, /dismissTask\(current, id\)/);+ // A finished row offers closing instead of stopping: it must be able to leave+ // the composer without touching the process lifecycle.+ assert.match(bar, /\{t\("backgroundTasks\.dismiss"\)\}/);+ assert.match(bar, /onClick=\{\(\) => dismiss\(task\.id\)\}/);+ assert.match(i18nEn, /backgroundTasks: \{[^}]*dismiss: "Dismiss"/);+});diff --git a/docs/spec/03-runtime/03-tools-and-permissions.md b/docs/spec/03-runtime/03-tools-and-permissions.mdindex b1b1c7f67..175d26013 100644--- a/docs/spec/03-runtime/03-tools-and-permissions.md+++ b/docs/spec/03-runtime/03-tools-and-permissions.md@@ -437,6 +437,13 @@ Host execution baseline:Plan/Ask decisions, then the input. There is no configurable display priority.The list scrolls within `min(200px, 24vh)` so additional tasks do not displacethe decision surfaces. Existing approval and Ask input gating is unchanged.+- A finished background task is not permanent furniture. Its row lives for a+ 10-second grace window after the renderer observed the task end, so the exit+ code stays readable, then it leaves the bar. A task that had already finished+ when the renderer first loaded is registry history and never appears. Closing+ a finished row removes it immediately; an open output preview pins its row so+ a log being read cannot vanish underneath it. Closing is a view action only —+ the host keeps its bounded task history.- Only one output preview is open at a time. While its task runs, the Rendererrefreshes that preview at one-second intervals after the previous read settles;collapsed outputs are not fetched. The preview shows at most 8,000 stdoutdiff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.mdindex 13fd6a8c6..e6d4c5364 100644--- a/docs/spec/06-delivery/04-e2e-test-plan.md+++ b/docs/spec/06-delivery/04-e2e-test-plan.md@@ -7850,6 +7850,8 @@ must keep splitting are covered by `markdown-blocks.test.mjs`.fetched and refreshed; old responses cannot erase it. Each stream remainsbounded to 256KiB and retains its final marker after exit. A Stop failure isvisible without an unhandled rejection and the control remains retryable.+ An ended task keeps its row only for a short grace window and can be closed+ from the bar, so finished commands do not accumulate above the composer.- **Specs linked**: `03-runtime/03-tools-and-permissions.md` (§5),`03-runtime/06-host-rpc-protocol.md` (§4)- **Acceptance criterion**: C, E, F@@ -7861,7 +7863,10 @@ must keep splitting are covered by `markdown-blocks.test.mjs`.exercises production React components in isolated Electron at 1280px, 700px,and 390px widths. Host tests cover exit-before-drain and shutdown under outputlock contention. The full agent-to-Desktop journey and an 83-minute paced- 100,000-line soak remain manual; the automated volume test is accelerated.)+ 100,000-line soak remain manual; the automated volume test is accelerated.+ Renderer visibility (grace window, close action, pinned output preview) is+ covered by `apps/desktop/test/background-tasks-bar-visibility.test.mjs` with a+ controlled clock; the multi-second hand-off itself is not timed in Electron.)#### E2E-117: Agent/Plan/Goal UX and locales contain no Chat controlsdiff --git a/packages/i18n/src/locales/de/index.ts b/packages/i18n/src/locales/de/index.tsindex 9b951917b..0fa00141c 100644--- a/packages/i18n/src/locales/de/index.ts+++ b/packages/i18n/src/locales/de/index.ts@@ -688,6 +688,7 @@ export const de = {output: "Ausgabe",stop: "Stoppen",stopping: "Stoppe…",+ dismiss: "Schließen",},planHistory: {pending: "Genehmigung ausstehend",diff --git a/packages/i18n/src/locales/en/index.ts b/packages/i18n/src/locales/en/index.tsindex e1c607da4..7b16fca98 100644--- a/packages/i18n/src/locales/en/index.ts+++ b/packages/i18n/src/locales/en/index.ts@@ -700,6 +700,7 @@ export const en = {output: "Output",stop: "Stop",stopping: "Stopping…",+ dismiss: "Dismiss",},planHistory: {pending: "Awaiting approval",diff --git a/packages/i18n/src/locales/es/index.ts b/packages/i18n/src/locales/es/index.tsindex b27866b95..205a5595a 100644--- a/packages/i18n/src/locales/es/index.ts+++ b/packages/i18n/src/locales/es/index.ts@@ -688,6 +688,7 @@ export const es = {output: "Salida",stop: "Detener",stopping: "Deteniendo…",+ dismiss: "Cerrar",},planHistory: {pending: "Pendiente de aprobación",diff --git a/packages/i18n/src/locales/fr/index.ts b/packages/i18n/src/locales/fr/index.tsindex 00a2ec63e..f015a5604 100644--- a/packages/i18n/src/locales/fr/index.ts+++ b/packages/i18n/src/locales/fr/index.ts@@ -688,6 +688,7 @@ export const fr = {output: "Sortie",stop: "Arrêter",stopping: "Arrêt…",+ dismiss: "Fermer",},planHistory: {pending: "En attente d’approbation",diff --git a/packages/i18n/src/locales/ko/index.ts b/packages/i18n/src/locales/ko/index.tsindex 0c9e1fa25..5c5b1723f 100644--- a/packages/i18n/src/locales/ko/index.ts+++ b/packages/i18n/src/locales/ko/index.ts@@ -697,6 +697,7 @@ export const ko = {output: "출력",stop: "중지",stopping: "중지 중…",+ dismiss: "닫기",},planHistory: {pending: "승인 대기",diff --git a/packages/i18n/src/locales/pt-BR/index.ts b/packages/i18n/src/locales/pt-BR/index.tsindex 0c8e52c29..48036f423 100644--- a/packages/i18n/src/locales/pt-BR/index.ts+++ b/packages/i18n/src/locales/pt-BR/index.ts@@ -687,6 +687,7 @@ export const ptBR = {output: "Saída",stop: "Parar",stopping: "Parando…",+ dismiss: "Fechar",},planHistory: {pending: "Aguardando aprovação",diff --git a/packages/i18n/src/locales/tr/index.ts b/packages/i18n/src/locales/tr/index.tsindex a885b2d4b..10d7331cf 100644--- a/packages/i18n/src/locales/tr/index.ts+++ b/packages/i18n/src/locales/tr/index.ts@@ -697,6 +697,7 @@ export const tr = {output: "Çıktı",stop: "Durdur",stopping: "Durduruluyor…",+ dismiss: "Kapat",},planHistory: {pending: "Onay bekleniyor",diff --git a/packages/i18n/src/locales/zh-CN/index.ts b/packages/i18n/src/locales/zh-CN/index.tsindex a89ab066e..b193bfda2 100644--- a/packages/i18n/src/locales/zh-CN/index.ts+++ b/packages/i18n/src/locales/zh-CN/index.ts@@ -693,6 +693,7 @@ export const zhCN = {output: "输出",stop: "停止",stopping: "停止中…",+ dismiss: "关闭",},planHistory: {pending: "待审批",diff --git a/packages/i18n/src/locales/zh-TW/index.ts b/packages/i18n/src/locales/zh-TW/index.tsindex 93b9aae2d..a954bec61 100644--- a/packages/i18n/src/locales/zh-TW/index.ts+++ b/packages/i18n/src/locales/zh-TW/index.ts@@ -693,6 +693,7 @@ export const zhTW = {output: "輸出",stop: "停止",stopping: "停止中…",+ dismiss: "關閉",},planHistory: {pending: "待審批",