web: background-tasks bar — running shells, capped list, bounded output (C11)

Add the background-tasks bar to an open conversation: heading
"Background tasks" plus "{n} running", shown only while at least one
shell is running. Rows are capped at 3 (BACKGROUND_TASK_VISIBLE_ROWS)
and the list scrolls beyond; each row is a play glyph, "Running", a
one-line shellCommandSummary and "Details" while selected. Tapping a
row toggles its detail pane ("Command"/"Task identity"/"Status:
running" plus the bounded output: spinner, error + "Retry",
"Output unavailable for this task.", "No output yet.", or the
monospace output and "Output is bounded to 64 KiB." with "Refresh").

Fold shell frames without ever marking the turn running, merge a blank
command from the previous frame, list only running shells (an exited
shell is never a task) and drop/relearn shells on reconnect via
chat_shells. Refresh always rereads the bounded 64 KiB page from cursor
0. Update PARITY.md C11 with its captures and the replayed-vs-real note.

Checks: node --check on every touched file; cd web && node --test test/
(456 pass).
This commit is contained in:
2026-10-19 08:22:16 +01:00
parent 27a1605127
commit 8e5ce8494b
9 changed files with 1239 additions and 18 deletions
+183
View File
@@ -5,6 +5,8 @@ import { test } from "node:test";
import assert from "node:assert/strict";
import {
BACKGROUND_TASKS_TITLE,
BACKGROUND_TASK_VISIBLE_ROWS,
CHAT_COMMANDS,
CHAT_COMMAND_GLYPH,
CHAT_FOLLOW_THRESHOLD_PX,
@@ -14,6 +16,17 @@ import {
ChatTurn,
MAX_ATTACHMENT_BYTES,
MAX_RENDERED_DIFF_LINES,
OBSERVED_SHELL_LIMIT,
SHELL_DETAILS_LABEL,
SHELL_EXITED,
SHELL_OUTPUT_EMPTY,
SHELL_OUTPUT_ERROR,
SHELL_OUTPUT_LABELS,
SHELL_OUTPUT_LOADING,
SHELL_OUTPUT_READY,
SHELL_OUTPUT_UNAVAILABLE,
SHELL_ROW_LABEL,
SHELL_RUNNING,
TOOL_DETAIL_MAX_LENGTH,
TOOL_LONG_RUNNING_MILLIS,
TURN_PHASE_THINKING,
@@ -24,6 +37,7 @@ import {
attachmentDisplayName,
attachmentError,
attachmentFiles,
backgroundTasksRunningLabel,
chatBottomDistancePx,
chatCommandEffect,
chatCommandForEnter,
@@ -49,6 +63,7 @@ import {
historyResync,
lineDiff,
mergeHistoryPage,
mergeShellFrame,
mergeToolPart,
newChatThread,
parseChatInput,
@@ -56,7 +71,9 @@ import {
prettyToolInput,
queuedAfterFrame,
reasoningLabel,
resumedThread,
runningAfterFrame,
runningBackgroundShells,
runningToolElapsedMillis,
runningToolStamps,
sessionActivity,
@@ -68,6 +85,9 @@ import {
sessionTitle,
sessionWorkLabel,
settledThread,
shellCommandSummary,
shellOutputState,
shellsLoaded,
threadElapsedMillis,
threadPhase,
threadRunningTools,
@@ -1168,3 +1188,166 @@ test("the running indicator's elapsed and per-tool timing use the thread's own s
assert.deepEqual(runningToolStamps({ t1: 42 }, thread.messages, 9999), { t1: 42 });
assert.deepEqual(runningToolStamps({ t1: 42 }, [], 9999), {});
});
// --- C11: background tasks (shells) -----------------------------------------
// The pure model the background-tasks bar is built on: running-only filtering,
// the visible-row cap, the one-line command summary, shell-frame folding and the
// detail pane's state selection and wording. Mirror of
// ChatFormat.runningBackgroundShells/shellCommandSummary, ChatStore.applyShell/
// shellsLoaded and ChatTranscript.BackgroundProcessBar/-Detail.
const shell = (overrides = {}) => ({ id: "sh1", status: "running", command: "go test ./...", ...overrides });
test("runningBackgroundShells lists only non-blank running shells, in order", () => {
const shells = [
shell({ id: "a", command: "one" }),
shell({ id: "b", status: SHELL_EXITED, command: "two" }),
shell({ id: "", command: "three" }),
shell({ id: "d", status: "running", command: "four" }),
shell({ id: "e", status: undefined }),
];
assert.deepEqual(
runningBackgroundShells(shells).map((s) => s.id),
["a", "d"],
);
assert.deepEqual(runningBackgroundShells([]), []);
assert.deepEqual(runningBackgroundShells(undefined), []);
});
test("the visible-row cap is three and never truncates the model", () => {
assert.equal(BACKGROUND_TASK_VISIBLE_ROWS, 3);
const running = Array.from({ length: 5 }, (_, i) => shell({ id: `s${i}` }));
// The cap is a view/CSS concern; every running shell stays in the model.
assert.equal(runningBackgroundShells(running).length, 5);
});
test("shellCommandSummary collapses whitespace, names a blank command and elides past 72", () => {
assert.equal(SHELL_ROW_LABEL, "Running");
assert.equal(shellCommandSummary(""), "background process");
assert.equal(shellCommandSummary(" "), "background process");
assert.equal(shellCommandSummary(null), "background process");
assert.equal(shellCommandSummary(" go test\n./...\t-x "), "go test ./... -x");
// Exactly 72 characters is not elided.
const seventyTwo = "a".repeat(TOOL_DETAIL_MAX_LENGTH);
assert.equal(shellCommandSummary(seventyTwo), seventyTwo);
// Longer: 71 kept characters, trailing whitespace trimmed, then one ellipsis.
const long = `${"b".repeat(TOOL_DETAIL_MAX_LENGTH)} tail`;
const elided = shellCommandSummary(long);
assert.equal(elided, `${"b".repeat(TOOL_DETAIL_MAX_LENGTH - 1)}…`);
assert.equal(elided.length, TOOL_DETAIL_MAX_LENGTH);
assert.ok(elided.endsWith("…"));
// A 71-char word followed by a space then more trims the trailing space first.
const word = `${"c".repeat(TOOL_DETAIL_MAX_LENGTH - 1)} rest`;
assert.equal(shellCommandSummary(word), `${"c".repeat(TOOL_DETAIL_MAX_LENGTH - 1)}…`);
});
test("mergeShellFrame keeps a blank command from the previous frame and caps the list", () => {
const first = mergeShellFrame([], shell({ id: "a", command: "go build" }));
assert.deepEqual(first, [shell({ id: "a", command: "go build" })]);
// A later frame that omits the command never blanks the retained one.
const merged = mergeShellFrame(first, shell({ id: "a", command: "" }));
assert.equal(merged.length, 1);
assert.equal(merged[0].command, "go build");
// A frame that carries a command replaces it; the position is preserved.
const renamed = mergeShellFrame(merged, shell({ id: "a", command: "go vet" }));
assert.equal(renamed[0].command, "go vet");
// A blank id (or null shell) is ignored outright.
assert.equal(mergeShellFrame(renamed, shell({ id: "" })).length, 1);
assert.deepEqual(mergeShellFrame(renamed, null), renamed);
// The retained list is capped at OBSERVED_SHELL_LIMIT, keeping the newest.
let many = [];
for (let i = 0; i < OBSERVED_SHELL_LIMIT + 4; i += 1) {
many = mergeShellFrame(many, shell({ id: `s${i}`, command: `c${i}` }));
}
assert.equal(many.length, OBSERVED_SHELL_LIMIT);
assert.equal(many.at(-1).id, `s${OBSERVED_SHELL_LIMIT + 3}`);
assert.equal(many[0].id, "s4");
});
test("shellsLoaded replaces the live set but retains exits this client observed", () => {
const existing = [
shell({ id: "r1", status: SHELL_RUNNING, command: "old run" }),
shell({ id: "e1", status: SHELL_EXITED, command: "finished" }),
];
const loaded = shellsLoaded(existing, [shell({ id: "r2", status: SHELL_RUNNING, command: "new run" })]);
assert.deepEqual(loaded.map((s) => s.id), ["e1", "r2"]);
// r1 is neither in the snapshot nor a locally observed exit, so it drops out.
assert.ok(!loaded.some((s) => s.id === "r1"));
// An exit the client never saw exit is not invented.
assert.ok(!loaded.some((s) => s.id === "r1"));
});
test("shellOutputState picks the detail pane's branch in the app's order", () => {
assert.equal(shellOutputState(null), null);
assert.equal(shellOutputState(undefined), null);
assert.equal(shellOutputState({}), null);
assert.equal(shellOutputState({ loading: true }), SHELL_OUTPUT_LOADING);
assert.equal(shellOutputState({ error: "boom" }), SHELL_OUTPUT_ERROR);
// Error outranks a page if both were set.
assert.equal(shellOutputState({ error: "boom", page: { output: "x" } }), SHELL_OUTPUT_ERROR);
assert.equal(shellOutputState({ page: { unavailable: true } }), SHELL_OUTPUT_UNAVAILABLE);
assert.equal(shellOutputState({ page: { output: "" } }), SHELL_OUTPUT_EMPTY);
assert.equal(shellOutputState({ page: { output: "line\n" } }), SHELL_OUTPUT_READY);
assert.equal(shellOutputState({ page: { output: "x", truncated: true } }), SHELL_OUTPUT_READY);
});
test("the detail pane's wording is the app's verbatim copy", () => {
assert.equal(BACKGROUND_TASKS_TITLE, "Background tasks");
assert.equal(backgroundTasksRunningLabel(3), "3 running");
assert.equal(SHELL_DETAILS_LABEL, "Details");
assert.deepEqual(SHELL_OUTPUT_LABELS, {
COMMAND: "Command",
UNAVAILABLE: "Unavailable",
TASK_IDENTITY: "Task identity",
STATUS_RUNNING: "Status: running",
OUTPUT_UNAVAILABLE: "Output unavailable for this task.",
NO_OUTPUT: "No output yet.",
OUTPUT: "Output",
OUTPUT_BOUNDED: "Output is bounded to 64 KiB.",
RETRY: "Retry",
REFRESH: "Refresh",
});
});
test("a shell frame folds into the thread and never marks the turn running", () => {
const folded = applyChatEvent(
newChatThread("s1"),
evt("shell", { shell: { id: "sh1", status: "running", command: "sleep 5" } }),
1000,
);
assert.equal(folded.running, false);
assert.equal(folded.runningSince, 0);
assert.deepEqual(folded.shells, [{ id: "sh1", status: "running", command: "sleep 5" }]);
// A shell frame that ends the shell removes it from the running set at once.
const exited = applyChatEvent(
folded,
evt("shell", { shell: { id: "sh1", status: "exited", command: "" } }),
1001,
);
assert.equal(exited.shells[0].status, "exited");
assert.equal(exited.shells[0].command, "sleep 5");
assert.equal(runningBackgroundShells(exited.shells).length, 0);
});
test("a step that ends does not clear a background shell that outlives the turn", () => {
let thread = applyChatEvent(
newChatThread("s1"),
evt("shell", { shell: { id: "sh1", status: "running", command: "sleep 5" } }),
1000,
);
thread = applyChatEvent(thread, evt("step", { step: { status: "started" } }), 1000);
thread = applyChatEvent(thread, evt("step", { step: { status: "ended" } }), 2000);
assert.equal(thread.running, false);
assert.equal(runningBackgroundShells(thread.shells).length, 1);
// settledThread alone keeps the shell; only the reconnect repair drops it.
assert.equal(settledThread(thread).shells.length, 1);
const resumed = resumedThread(thread);
assert.deepEqual(resumed.shells, []);
assert.equal(resumed.selectedShellId, null);
assert.equal(resumed.shellOutput, null);
});