TypeScript
fix(client-runtime): terminals fit their pane again after a reconnect (#29696)
Alex Southwellee265cc53999
diff --git a/packages/client-runtime/src/state/terminalSession.test.ts b/packages/client-runtime/src/state/terminalSession.test.tsindex 7cbcd14f819..4eef70e0997 100644--- a/packages/client-runtime/src/state/terminalSession.test.ts+++ b/packages/client-runtime/src/state/terminalSession.test.ts@@ -245,10 +245,14 @@ describe("terminal session reducers", () => {snapshot: BASE_SNAPSHOT,});let cursor = readTerminalOutputUpdate(state.output, INITIAL_TERMINAL_OUTPUT_CURSOR).cursor;+ const attachedGeneration = state.output.generation;+ // A socket reconnect resubscribes the same stream, so the server's new snapshot gets no seed.state = applyTerminalAttachStreamEvent(state, { type: "snapshot", snapshot: BASE_SNAPSHOT });const repeated = readTerminalOutputUpdate(state.output, cursor);expect(repeated).toMatchObject({ type: "reset", data: "hello" });expect(state.version).toBe(2);+ // Viewers replay their measured grid once per generation; the new attachment holds the old size.+ expect(state.output.generation).not.toBe(attachedGeneration);cursor = repeated.cursor;state = applyTerminalAttachStreamEvent(state, {diff --git a/packages/client-runtime/src/state/terminalSession.ts b/packages/client-runtime/src/state/terminalSession.tsindex f67184ef710..e03dc305076 100644--- a/packages/client-runtime/src/state/terminalSession.ts+++ b/packages/client-runtime/src/state/terminalSession.ts@@ -162,12 +162,21 @@ export function applyTerminalAttachStreamEvent(maxBufferBytes = DEFAULT_MAX_TERMINAL_BUFFER_BYTES,): TerminalBufferState {switch (event.type) {- case "snapshot":+ case "snapshot": {+ if (current.version === 0) {+ return terminalBufferStateFromSnapshot(event.snapshot, maxBufferBytes, current);+ }+ // A later snapshot is a new server attachment, as after a socket reconnect. It starts a+ // new generation because viewers replay their measured grid once per generation.+ const reattached = {+ ...current,+ output: { ...current.output, generation: ++terminalAttachGeneration },+ };return {- ...terminalBufferStateFromSnapshot(event.snapshot, maxBufferBytes, current),- lifecycleVersion:- current.version === 0 ? current.lifecycleVersion : current.lifecycleVersion + 1,+ ...terminalBufferStateFromSnapshot(event.snapshot, maxBufferBytes, reattached),+ lifecycleVersion: current.lifecycleVersion + 1,};+ }case "restarted":return {...terminalBufferStateFromSnapshot(event.snapshot, maxBufferBytes, current),