Files
kandev/apps/web/lib/state/slices/session-runtime/purge-session.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

67 lines
2.7 KiB
TypeScript

import { describe, it, expect, beforeEach } from "vitest";
import { create } from "zustand";
import { immer } from "zustand/middleware/immer";
import { createSessionRuntimeSlice, purgeSessionRuntimeState } from "./session-runtime-slice";
import type { SessionRuntimeSlice } from "./types";
function makeStore() {
return create<SessionRuntimeSlice>()(immer<SessionRuntimeSlice>(createSessionRuntimeSlice));
}
describe("purgeSessionRuntimeState", () => {
let store: ReturnType<typeof makeStore>;
beforeEach(() => {
store = makeStore();
});
it("drops per-session maps, process output, and env-scoped buffers", () => {
const s = store.getState();
s.registerSessionEnvironment("session-1", "env-1");
s.appendShellOutput("session-1", "shell noise");
s.setShellStatus("session-1", { available: true });
s.setContextWindow("session-1", { size: 1, used: 1, remaining: 0, efficiency: 1 });
s.setSessionTodos("session-1", [{ description: "do", status: "pending" }]);
s.upsertProcessStatus({
processId: "proc-1",
sessionId: "session-1",
kind: "dev",
status: "running",
});
s.appendProcessOutput("proc-1", "process noise");
store.setState((draft) => {
purgeSessionRuntimeState(draft, "session-1");
});
const after = store.getState();
expect(after.environmentIdBySessionId["session-1"]).toBeUndefined();
expect(after.contextWindow.bySessionId["session-1"]).toBeUndefined();
expect(after.sessionTodos.bySessionId["session-1"]).toBeUndefined();
expect(after.processes.processIdsBySessionId["session-1"]).toBeUndefined();
expect(after.processes.devProcessBySessionId["session-1"]).toBeUndefined();
expect(after.processes.processesById["proc-1"]).toBeUndefined();
expect(after.processes.outputsByProcessId["proc-1"]).toBeUndefined();
// env-scoped buffers gone because no other session references env-1.
expect(after.shell.outputs["env-1"]).toBeUndefined();
expect(after.shell.statuses["env-1"]).toBeUndefined();
});
it("retains env-scoped buffers while another session shares the environment", () => {
const s = store.getState();
s.registerSessionEnvironment("session-1", "env-shared");
s.registerSessionEnvironment("session-2", "env-shared");
s.appendShellOutput("session-1", "shared shell");
store.setState((draft) => {
purgeSessionRuntimeState(draft, "session-1");
});
const after = store.getState();
expect(after.environmentIdBySessionId["session-1"]).toBeUndefined();
expect(after.environmentIdBySessionId["session-2"]).toBe("env-shared");
// session-2 still uses env-shared, so its shell output must survive.
expect(after.shell.outputs["env-shared"]).toBe("shared shell");
});
});