Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/client-runtime/src/state/terminalSession.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
17 changes: 13 additions & 4 deletions packages/client-runtime/src/state/terminalSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Loading