Skip to content

Commit d747f4c

Browse files
saphidclaude
authored andcommitted
fix(client-runtime): terminals fit their pane again after a reconnect
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a036489 commit d747f4c

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

‎packages/client-runtime/src/state/terminalSession.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,14 @@ describe("terminal session reducers", () => {
245245
snapshot: BASE_SNAPSHOT,
246246
});
247247
let cursor = readTerminalOutputUpdate(state.output, INITIAL_TERMINAL_OUTPUT_CURSOR).cursor;
248+
const attachedGeneration = state.output.generation;
249+
// A socket reconnect resubscribes the same stream, so the server's new snapshot gets no seed.
248250
state = applyTerminalAttachStreamEvent(state, { type: "snapshot", snapshot: BASE_SNAPSHOT });
249251
const repeated = readTerminalOutputUpdate(state.output, cursor);
250252
expect(repeated).toMatchObject({ type: "reset", data: "hello" });
251253
expect(state.version).toBe(2);
254+
// Viewers replay their measured grid once per generation; the new attachment holds the old size.
255+
expect(state.output.generation).not.toBe(attachedGeneration);
252256
cursor = repeated.cursor;
253257

254258
state = applyTerminalAttachStreamEvent(state, {

‎packages/client-runtime/src/state/terminalSession.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,21 @@ export function applyTerminalAttachStreamEvent(
162162
maxBufferBytes = DEFAULT_MAX_TERMINAL_BUFFER_BYTES,
163163
): TerminalBufferState {
164164
switch (event.type) {
165-
case "snapshot":
165+
case "snapshot": {
166+
if (current.version === 0) {
167+
return terminalBufferStateFromSnapshot(event.snapshot, maxBufferBytes, current);
168+
}
169+
// A later snapshot is a new server attachment, as after a socket reconnect. It starts a
170+
// new generation because viewers replay their measured grid once per generation.
171+
const reattached = {
172+
...current,
173+
output: { ...current.output, generation: ++terminalAttachGeneration },
174+
};
166175
return {
167-
...terminalBufferStateFromSnapshot(event.snapshot, maxBufferBytes, current),
168-
lifecycleVersion:
169-
current.version === 0 ? current.lifecycleVersion : current.lifecycleVersion + 1,
176+
...terminalBufferStateFromSnapshot(event.snapshot, maxBufferBytes, reattached),
177+
lifecycleVersion: current.lifecycleVersion + 1,
170178
};
179+
}
171180
case "restarted":
172181
return {
173182
...terminalBufferStateFromSnapshot(event.snapshot, maxBufferBytes, current),

0 commit comments

Comments
 (0)