fix: stop terminal open/switch from rewriting state per output chunk (#2536)
Opening a terminal rebuilt the WASM terminal twice and rewrote the persisted session snapshot on every streamed output chunk, so the cost grew with each open terminal and could crash under load. - Move PTY scrollback to a standalone buffers map keyed by directory and tab id; output no longer touches sessions, so the tab strip, the project-action monitor and the persist projection stay referentially stable while chunks stream. - Memoize partialize and add a dedup storage adapter that skips writes when the persisted projection is unchanged. - Reuse the idle terminal WebSocket across tab switches (15s grace) instead of re-authenticating and replaying the snapshot on every attach. - Key the viewport by directory + tab only so createSession no longer tears down and rebuilds the Ghostty terminal. - Reset the terminal in place on replay discontinuities instead of bumping the renderer generation. - Scan the chunk array from the end (O(1) per write) instead of findIndex. Adds regression tests for the buffer map, socket reuse and viewport key, and documents the store invariants.
This commit is contained in:
@@ -132,6 +132,51 @@ describe('terminal transport', () => {
|
||||
transport.dispose();
|
||||
});
|
||||
|
||||
test('reuses the open socket when switching between terminals', async () => {
|
||||
const sockets: FakeSocket[] = [];
|
||||
let authCalls = 0;
|
||||
const transport = new TerminalTransport({
|
||||
refreshAuth: async () => { authCalls += 1; },
|
||||
openSocket: () => { const socket = new FakeSocket(); sockets.push(socket); return socket; },
|
||||
});
|
||||
|
||||
const unsubscribeFirst = transport.subscribe('term-1', { onEvent: () => {} });
|
||||
await tick();
|
||||
sockets[0].open();
|
||||
await tick();
|
||||
expect(authCalls).toBe(1);
|
||||
|
||||
// Switching tabs detaches the old terminal before attaching the new one.
|
||||
unsubscribeFirst();
|
||||
transport.subscribe('term-2', { onEvent: () => {} });
|
||||
await tick();
|
||||
|
||||
expect(sockets).toHaveLength(1);
|
||||
expect(sockets[0].readyState).toBe(1);
|
||||
expect(authCalls).toBe(1);
|
||||
expect(sockets[0].sent.some((message) => message.t === 'detach' && message.s === 'term-1')).toBe(true);
|
||||
expect(sockets[0].sent.some((message) => message.t === 'attach' && message.s === 'term-2')).toBe(true);
|
||||
transport.dispose();
|
||||
});
|
||||
|
||||
test('disposing closes a socket that was being held for reuse', async () => {
|
||||
const sockets: FakeSocket[] = [];
|
||||
const transport = new TerminalTransport({
|
||||
refreshAuth: async () => '',
|
||||
openSocket: () => { const socket = new FakeSocket(); sockets.push(socket); return socket; },
|
||||
});
|
||||
|
||||
const unsubscribe = transport.subscribe('term-1', { onEvent: () => {} });
|
||||
await tick();
|
||||
sockets[0].open();
|
||||
await tick();
|
||||
|
||||
unsubscribe();
|
||||
expect(sockets[0].readyState).toBe(1);
|
||||
transport.dispose();
|
||||
expect(sockets[0].readyState).toBe(3);
|
||||
});
|
||||
|
||||
test('does not reconnect after the last subscriber detaches', async () => {
|
||||
let attempts = 0;
|
||||
const transport = new TerminalTransport({
|
||||
|
||||
Reference in New Issue
Block a user