Watch
0
0
Fork
You've already forked hyperhive
0

swarm ui: a terminal keeps 2000 rows and folds a burst of rows in at once

useSwarmTermStream appended each event with [...rows, row] and never dropped one, so a tab on a busy agent grew without bound in memory and DOM and each event cost a copy of the whole list. appendRows (a pure module, tested) folds a batch in with one copy, coalesces as before, and keeps the newest 2000. Events are collected for 50ms and folded in together, so a burst is one render.

The comments that described the old coalescing in useSwarmTermStream.ts's header and in termMsg.ts's TermEnvelope doc named a hook that no longer exists (useLiveStream), so both now point at term-rows.ts, where the rule lives.
This commit is contained in:
iris 2026-11-19 19:07:02 +01:00 • committed by mara
commit 0a72f212da
2 changed files with 129 additions and 29 deletions

View file

@ -0,0 +1,31 @@
// The rows a terminal keeps, and how a batch of new ones is folded in. A stream
// for a busy agent never ends, so the list is capped: the oldest rows drop off the
// front, and a tab open for days holds `MAX_ROWS` rows, not every row since.
import type { TermRow } from "@hive/shared/term-msg.js";
/** How many rows a terminal keeps. */
export const MAX_ROWS = 2000;
/**
* `rows` with `batch` appended, in order. A row with the same `coalesce_key` as
* the last one replaces it (keeping its `key`), so a progress line updates in
* place. At most `max` rows are kept, the newest. Copies the list once per
* batch, not once per row.
*/
export function appendRows(
rows: TermRow[],
batch: TermRow[],
max: number = MAX_ROWS,
): TermRow[] {
if (batch.length === 0) return rows;
const out = rows.slice();
for (const row of batch) {
const last = out[out.length - 1];
if (row.coalesce_key && last && last.coalesce_key === row.coalesce_key) {
out[out.length - 1] = { ...row, key: last.key };
} else {
out.push(row);
}
}
return out.length > max ? out.slice(out.length - max) : out;
}

View file

@ -1,14 +1,11 @@
// Live SSE consumer for the swarm-relayed agent terminal
// (`GET /api/agents/{name}/term/stream`, swarm-controller's
// `term_stream.rs`). Deliberately NOT `@hive/agent`'s `useLiveStream` —
// that hook's wire shape is `TermEnvelope` (`{seq, msgs: []}`) plus a
// history/backfill dance; the swarm relay forwards one bare `TermMsg`
// per SSE event, its replayed rows first and in order on the same
// stream, so there's nothing to buffer/dedupe/backfill against. The
// browser's own `Last-Event-ID` resumes a reconnect, so rows are kept
// across one. Same row-coalescing
// rule as `useLiveStream`'s `appendRow` (ported, not imported — one
// small helper isn't worth a cross-package hop for).
// `term_stream.rs`). The relay forwards one bare `TermMsg` per SSE event, its
// replayed rows first and in order on the same stream, so there is nothing to
// buffer, dedupe or backfill against. The browser's own `Last-Event-ID`
// resumes a reconnect, so rows are kept across one. Rows that arrive within
// 50 ms are folded in together, and `term-rows.ts` holds the rules for that:
// coalesce by key, and keep the newest `MAX_ROWS`.
import { useEffect, useRef, useState } from "preact/hooks";
import type { TermMsg, TermRow } from "@hive/shared/term-msg.js";
@ -17,16 +14,13 @@ import {
type ConnectionState,
} from "../../ui/stream/connection.js";
import { probeSession } from "../../ui/session/session.js";
import { appendRows } from "./term-rows.js";
export type { ConnectionState };
function appendRow(rows: TermRow[], row: TermRow): TermRow[] {
const last = rows[rows.length - 1];
if (row.coalesce_key && last && last.coalesce_key === row.coalesce_key) {
return [...rows.slice(0, -1), { ...row, key: last.key }];
}
return [...rows, row];
}
// Rows that arrive close together are folded in at once: a busy turn sends many a
// second, and each fold copies the list.
const BATCH_MS = 50;
export interface UseSwarmTermStreamResult {
rows: TermRow[];
@ -48,6 +42,18 @@ export function useSwarmTermStream(
setRows([]);
setConnection("connecting");
keySeqRef.current = 0;
let batch: TermRow[] = [];
let timer: ReturnType<typeof setTimeout> | null = null;
function flush() {
timer = null;
const next = batch;
batch = [];
setRows((prev) => appendRows(prev, next));
}
function push(row: TermRow) {
batch.push(row);
if (timer === null) timer = setTimeout(flush, BATCH_MS);
}
const es = new EventSource(streamUrl);
es.onopen = () => setConnection("open");
es.onmessage = (e) => {
@ -57,17 +63,15 @@ export function useSwarmTermStream(
try {
msg = JSON.parse(e.data);
} catch {
setRows((prev) =>
appendRow(prev, {
key,
level: "warn",
fromHistory: false,
summary: "[parse err] " + e.data,
}),
);
push({
key,
level: "warn",
fromHistory: false,
summary: "[parse err] " + e.data,
});
return;
}
setRows((prev) => appendRow(prev, { ...msg, key, fromHistory: false }));
push({ ...msg, key, fromHistory: false });
};
es.onerror = () => {
const state = connectionAfterError(es.readyState);
@ -75,7 +79,10 @@ export function useSwarmTermStream(
// The browser gave up: say whether the session ended, which `EventSource` does not tell.
if (state === "closed") probeSession();
};
return () => es.close();
return () => {
es.close();
if (timer !== null) clearTimeout(timer);
};
}, [streamUrl, attempt]);
return { rows, connection, reconnect: () => setAttempt((n) => n + 1) };