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
+179 -6
View File
@@ -302,6 +302,21 @@ export function toolDetail(part) {
return null;
}
/**
* The one-line command a background process is running, collapsed to a single
* line and elided past [TOOL_DETAIL_MAX_LENGTH] like a tool call's detail, so a
* long command line cannot flood the bar. A shell that reported no command reads
* as a generic `background process` rather than a blank line (ChatFormat.kt
* shellCommandSummary, which shares the tool-detail regex and bound).
*/
export function shellCommandSummary(command) {
const text = str(command).trim().replace(/\s+/g, " ");
if (text === "") return "background process";
return text.length <= TOOL_DETAIL_MAX_LENGTH
? text
: text.slice(0, TOOL_DETAIL_MAX_LENGTH - 1).trimEnd() + "…";
}
/**
* The INPUT section of a tool call: pretty-printed JSON once the arguments are an
* object/array, the raw partial-JSON string while still streaming, or empty when
@@ -795,6 +810,141 @@ export function chatStreamState(connectionKind, subscribed) {
return { tone: "not-live", label: STREAM_OFFLINE };
}
// ---------------------------------------------------------------------------
// Background shells (C11: ChatTranscript.kt BackgroundProcessBar, ChatStore.kt
// applyShell/shellsLoaded, ChatFormat.kt runningBackgroundShells/shellCommandSummary)
// ---------------------------------------------------------------------------
/**
* ChatShell.Status values (internal/protocol/chat.go). A running shell is one
* whose command has started and not yet exited; `exited` is final.
*/
export const SHELL_RUNNING = "running";
export const SHELL_EXITED = "exited";
/**
* Per-session in-memory observation bound (ChatStore.OBSERVED_SHELL_LIMIT): the
* retained shell tail a selected task can settle against. This is not task
* history, only what this client itself observed.
*/
export const OBSERVED_SHELL_LIMIT = 16;
/**
* How many running-task rows the bar draws before the list scrolls instead of
* growing (ChatTranscript.BACKGROUND_TASK_VISIBLE_ROWS).
*/
export const BACKGROUND_TASK_VISIBLE_ROWS = 3;
/** The bar's heading; its count line is `{n} running`. */
export const BACKGROUND_TASKS_TITLE = "Background tasks";
export function backgroundTasksRunningLabel(count) {
return `${count} running`;
}
/** A row's own words (ChatTranscript.BackgroundProcessLine). */
export const SHELL_ROW_LABEL = "Running";
export const SHELL_DETAILS_LABEL = "Details";
/**
* The detail pane's exact wording, verbatim from
* ChatTranscript.BackgroundProcessDetail. Kept as one frozen map so the pane and
* its unit test share one string instead of the copy drifting.
*/
export const SHELL_OUTPUT_LABELS = Object.freeze({
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",
});
/**
* The background processes still running for a session, in the order they
* started, from the shells the server reported and the session retains. A shell
* that has exited is never listed — that is what clears the bar — and the set is
* independent of the turn, because a background process outlives the tool call
* (and often the turn) that started it. Mirrors ChatFormat.runningBackgroundShells:
* only a non-blank id with status `running` is a task, and the list is never
* truncated here (the visible-row cap is a view concern).
*/
export function runningBackgroundShells(shells) {
return (shells ?? []).filter(
(shell) => !isBlank(shell?.id) && str(shell?.status) === SHELL_RUNNING,
);
}
/**
* Folds one shell frame (or a snapshot entry) into the retained shells, keyed by
* id and kept in observation order. A blank incoming command keeps the command
* already learned, so a later frame that omits it never blanks the row, and the
* list is trimmed to the last [OBSERVED_SHELL_LIMIT] entries (ChatStore.applyShell).
*/
export function mergeShellFrame(shells, shell) {
const id = str(shell?.id);
if (shell == null || isBlank(id)) return shells ?? [];
const list = (shells ?? []).slice();
const index = list.findIndex((candidate) => str(candidate?.id) === id);
const prior = index >= 0 ? list[index] : null;
const merged = {
...shell,
id,
command: isBlank(shell?.command) ? str(prior?.command) : shell.command,
};
if (index >= 0) list[index] = merged;
else list.push(merged);
return list.slice(-OBSERVED_SHELL_LIMIT);
}
/**
* Replaces the retained shells with the authoritative running snapshot while
* keeping only exits this client itself observed, capped like every other shell
* list (ChatStore.shellsLoaded). A shell the server no longer lists and that this
* client never saw exit simply drops out.
*/
export function shellsLoaded(shells, running) {
const byId = new Map();
for (const shell of shells ?? []) {
if (!isBlank(shell?.id) && str(shell?.status) === SHELL_EXITED) byId.set(shell.id, shell);
}
for (const shell of running ?? []) {
if (isBlank(shell?.id)) continue;
// Map.set keeps the first-insertion position and replaces the value, exactly
// like Kotlin's `observedExits + live` LinkedHashMap merge.
byId.set(shell.id, shell);
}
return [...byId.values()].slice(-OBSERVED_SHELL_LIMIT);
}
/** The five shapes a selected task's output area can take. */
export const SHELL_OUTPUT_LOADING = "loading";
export const SHELL_OUTPUT_ERROR = "error";
export const SHELL_OUTPUT_UNAVAILABLE = "unavailable";
export const SHELL_OUTPUT_EMPTY = "empty";
export const SHELL_OUTPUT_READY = "output";
/**
* Which branch the detail pane's output area shows for a selected task's
* [output] state, or null when there is nothing to show yet — the same decision
* order as ChatTranscript.BackgroundProcessDetail's `when` (loading, then error,
* then the server's `unavailable`, then an empty page, then a readable page), so
* the pane and its unit test share one derivation.
*/
export function shellOutputState(output) {
if (output == null) return null;
if (output.loading) return SHELL_OUTPUT_LOADING;
if (str(output.error) !== "") return SHELL_OUTPUT_ERROR;
const page = output.page ?? null;
if (page == null) return null;
if (page.unavailable === true) return SHELL_OUTPUT_UNAVAILABLE;
if (str(page.output) === "") return SHELL_OUTPUT_EMPTY;
return SHELL_OUTPUT_READY;
}
/**
* The shape an open conversation holds. The first six fields are the ones a
* history page fills; the rest is the live stream (see `ChatThread` in
@@ -815,6 +965,11 @@ export function newChatThread(sessionId = "") {
toolStartedAt: {},
lastDeltaAt: 0,
queued: false,
// The background shells this session has observed (C11), in observation
// order, the selected task and its bounded output pane.
shells: [],
selectedShellId: null,
shellOutput: null,
};
}
@@ -841,6 +996,19 @@ export function setThreadRunning(thread, running, now) {
return settledThread({ ...thread });
}
/**
* The reconnect repair for one thread (C5/C11): the settled form plus the shell
* observations dropped — a shell's exit may have been swallowed by the gap, so
* the retained shells are forgotten and re-learned from the re-subscribe's
* `chat_shells` snapshot. This is deliberately separate from [settledThread],
* which the step-ended frame also uses: a turn ending must never clear a
* background process that outlives it (ChatStore.streamResumed drops shells;
* ChatStore.setRunning does not).
*/
export function resumedThread(thread) {
return { ...settledThread(thread), shells: [], selectedShellId: null, shellOutput: null };
}
/**
* Non-negative elapsed time of an in-flight turn at [nowMillis], or 0 once it has
* settled. Pure, so the running indicator and its unit test share the derivation.
@@ -966,8 +1134,8 @@ export function threadPhase(thread, nowMillis) {
* thread. Any frame clears `queued` (the turn moved on); `user` echoes another
* client's prompt once (never this client's own), `text`/`reasoning` append to the
* part their kind started, `tool` merges field by field, `step` drives the running
* flag, and `shell`/`usage` are recognised but not folded here (C11 owns shells
* and the turn totals are not shown). The message that a delta or tool frame
* flag, and `shell` folds one background process into the retained set without
* ever marking the turn running (C11). The message that a delta or tool frame
* targets is created on first sight, so a live turn never gains a second bubble.
*/
export function applyChatEvent(thread, frame, now = Date.now()) {
@@ -984,9 +1152,12 @@ export function applyChatEvent(thread, frame, now = Date.now()) {
return applyTool(current, frame, now);
case EVENT_STEP:
return setThreadRunning(current, str(frame?.step?.status) !== STEP_ENDED, now);
// `shell` is C11's (it never marks the turn running) and `usage` is not
// folded; both still clear `queued`, which happened above.
// A shell frame only folds its shell into the retained set: it NEVER marks
// the thread running, because a background process is not the turn — a shell
// that outlives the turn keeps showing after the turn's own indicator is gone.
case EVENT_SHELL:
return { ...current, shells: mergeShellFrame(current.shells, frame?.shell) };
// `usage` is recognised but not folded; it still cleared `queued` above.
case EVENT_USAGE:
default:
return current;
@@ -1085,10 +1256,12 @@ function upsertParts(thread, frame, now, deltaArrivedAt, upsert) {
* The reconnect/resync replacement: the newest history page is authoritative, so
* the whole turn is replaced (no stale held message survives), the cursor and
* `hasMore` are replaced from the page, and any in-flight state is settled —
* every frame that was feeding a running turn is gone.
* every frame that was feeding a running turn is gone. The retained shells are
* dropped too (the reconnect re-fetches `chat_shells`), exactly like the Android
* `streamResumed` path.
*/
export function historyResync(thread, page) {
const settled = settledThread(thread ?? newChatThread());
const settled = resumedThread(thread ?? newChatThread());
return {
...settled,
messages: (page?.messages ?? []).slice(),