Files
kandev/apps/web/lib/state/slices/session-runtime/output-caps.test.ts
T
José Almeida 3b2e1396fa perf: fix board lag, memory growth, and rebase-driven CPU spikes (#1427)
* perf(kanban): memoize derived board data to stop re-render storms

The board rebuilt the `tasks` array (and `stepOptions`/`sharedProps`) on every
store change, producing fresh object identities that defeated downstream memos
and re-rendered every card on each WebSocket message. Wrap them in useMemo so
the board only re-renders when its inputs actually change.

* perf(session-runtime): bound runtime memory and dedupe git-status compare

Three runtime-store fixes for the crash a heavy rebase triggered:

- Cap shell + terminal output buffers at 2MB (matching the existing process
  buffer) so a chatty shell can't grow the store without limit.
- Purge per-session runtime state (messages, turns, shell/process buffers, and
  the bySessionId maps) when a session is removed; env-scoped maps are dropped
  only once no other session shares the environment.
- Run setGitStatus's deep per-file diff comparison once per update and return
  whether anything changed, instead of recomputing it 2-3x. Under a massive
  rebase (thousands of files, frequent updates) that comparison was the
  dominant CPU cost and drove the render storm.

* refactor(session-runtime): drop now-unused gitStatusWouldMutate

The git-status handler no longer pre-checks for changes (setGitStatus now
returns whether it changed), leaving gitStatusWouldMutate with no callers.

* fix(session-runtime): enforce terminal cap on first and oversized chunks

appendTerminalChunk only evicted when more than one chunk was buffered, so a
single chunk larger than the 2MB cap slipped through untrimmed; the new-terminal
branch also pushed the first chunk without going through the cap at all. Clamp
each chunk to the tail of the cap and route the new-terminal path through
appendTerminalChunk. Addresses CodeRabbit review.
2026-06-18 13:30:41 +01:00

78 lines
3.2 KiB
TypeScript

import { describe, it, expect, beforeEach } from "vitest";
import { create } from "zustand";
import { immer } from "zustand/middleware/immer";
import { createSessionRuntimeSlice } from "./session-runtime-slice";
import type { SessionRuntimeSlice } from "./types";
const MAX_BYTES = 2 * 1024 * 1024;
function makeStore() {
return create<SessionRuntimeSlice>()(immer<SessionRuntimeSlice>(createSessionRuntimeSlice));
}
describe("shell + terminal output caps", () => {
let store: ReturnType<typeof makeStore>;
beforeEach(() => {
store = makeStore();
});
it("appendShellOutput keeps the tail bounded at the cap", () => {
const chunk = "x".repeat(512 * 1024);
for (let i = 0; i < 10; i += 1) {
store.getState().appendShellOutput("session-1", chunk);
}
const output = store.getState().shell.outputs["session-1"];
expect(output.length).toBe(MAX_BYTES);
});
it("appendShellOutput preserves the most recent bytes", () => {
store.getState().appendShellOutput("session-1", "a".repeat(MAX_BYTES));
store.getState().appendShellOutput("session-1", "TAIL");
const output = store.getState().shell.outputs["session-1"];
expect(output.endsWith("TAIL")).toBe(true);
expect(output.length).toBe(MAX_BYTES);
});
it("appendShellOutput leaves small buffers untouched", () => {
store.getState().appendShellOutput("session-1", "hello ");
store.getState().appendShellOutput("session-1", "world");
expect(store.getState().shell.outputs["session-1"]).toBe("hello world");
});
it("setTerminalOutput drops oldest chunks once over the cap", () => {
const chunk = "y".repeat(512 * 1024);
for (let i = 0; i < 10; i += 1) {
store.getState().setTerminalOutput("term-1", chunk);
}
const terminal = store.getState().terminal.terminals.find((t) => t.id === "term-1");
const total = terminal?.output.reduce((sum, c) => sum + c.length, 0) ?? 0;
expect(total).toBeLessThanOrEqual(MAX_BYTES);
// Never empties the buffer entirely while data is flowing.
expect(terminal?.output.length ?? 0).toBeGreaterThan(0);
});
it("setTerminalOutput keeps small buffers as separate chunks", () => {
store.getState().setTerminalOutput("term-1", "a");
store.getState().setTerminalOutput("term-1", "b");
const terminal = store.getState().terminal.terminals.find((t) => t.id === "term-1");
expect(terminal?.output).toEqual(["a", "b"]);
});
it("setTerminalOutput clamps a single oversized chunk to the cap", () => {
// A lone chunk bigger than the cap must not slip through untrimmed.
store.getState().setTerminalOutput("term-1", "z".repeat(MAX_BYTES + 1024));
const terminal = store.getState().terminal.terminals.find((t) => t.id === "term-1");
const total = terminal?.output.reduce((sum, c) => sum + c.length, 0) ?? 0;
expect(total).toBe(MAX_BYTES);
});
it("setTerminalOutput enforces the cap on the first chunk of a new terminal", () => {
// The new-terminal branch must go through the same cap as appends.
store.getState().setTerminalOutput("fresh", "q".repeat(MAX_BYTES + 4096));
const terminal = store.getState().terminal.terminals.find((t) => t.id === "fresh");
const total = terminal?.output.reduce((sum, c) => sum + c.length, 0) ?? 0;
expect(total).toBe(MAX_BYTES);
});
});