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

View file

@ -0,0 +1,57 @@
// How a terminal folds new rows in: order, coalescing, and the cap. Run by
// `npm test`.
import assert from "node:assert/strict";
import { test } from "node:test";
import { appendRows, MAX_ROWS } from "../src/agent/terminal/term-rows.ts";
const row = (key, extra = {}) => ({ key, summary: key, ...extra });
test("a batch is appended in order", () => {
const out = appendRows([row("a")], [row("b"), row("c")]);
assert.deepEqual(
out.map((r) => r.key),
["a", "b", "c"],
);
});
test("an empty batch returns the same list, so nothing re-renders", () => {
const rows = [row("a")];
assert.equal(appendRows(rows, []), rows);
});
test("the input list is not changed", () => {
const rows = [row("a")];
appendRows(rows, [row("b")]);
assert.equal(rows.length, 1);
});
test("a row with the last row's coalesce key replaces it and keeps its key", () => {
const rows = [row("a"), row("b", { coalesce_key: "p", summary: "1/3" })];
const out = appendRows(rows, [
row("c", { coalesce_key: "p", summary: "2/3" }),
row("d", { coalesce_key: "p", summary: "3/3" }),
]);
assert.deepEqual(
out.map((r) => [r.key, r.summary]),
[
["a", "a"],
["b", "3/3"],
],
);
});
test("a coalesce key that is not the last row's appends", () => {
const rows = [row("a", { coalesce_key: "p" }), row("b")];
const out = appendRows(rows, [row("c", { coalesce_key: "p" })]);
assert.equal(out.length, 3);
});
test("only the newest rows are kept past the cap", () => {
const rows = Array.from({ length: 5 }, (_, i) => row("r" + i));
const out = appendRows(rows, [row("r5"), row("r6")], 4);
assert.deepEqual(
out.map((r) => r.key),
["r3", "r4", "r5", "r6"],
);
assert.ok(MAX_ROWS >= 1000);
});