Skip to content

Commit a9dcdf3

Browse files
fix(fixtures): translate shell resume cursors after replay
1 parent 44215da commit a9dcdf3

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

‎scripts/fixtures/issue-16534/replay.js‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,24 @@
146146

147147
globalThis.WebSocket = class extends NativeWebSocket {
148148
send(data) {
149-
super.send(data);
150-
if (!options.enabled || typeof data !== "string") return;
149+
if (!options.enabled || typeof data !== "string") return super.send(data);
151150
try {
152-
const { value } = parseFrame(data);
151+
const { value, ndjson } = parseFrame(data);
152+
let changed = false;
153153
for (const request of Array.isArray(value) ? value : [value]) {
154154
if (request?._tag === "Request" && request.tag === "orchestration.subscribeShell") {
155+
const afterSequence = request.payload?.afterSequence;
156+
// Foreground resumes reuse this socket's client cursor. A new RPC
157+
// session owns a new socket and reloads the authoritative HTTP shell.
158+
const offset =
159+
typeof afterSequence === "number" ? (this.latestShellStream?.offset ?? 0) : 0;
160+
if (offset !== 0) {
161+
request.payload.afterSequence -= offset;
162+
changed = true;
163+
}
155164
const stream = {
156-
sequence: shellSequence,
157-
offset: 0,
165+
sequence: afterSequence ?? shellSequence,
166+
offset,
158167
emit: (node) =>
159168
this.replayDeliver({
160169
_tag: "Chunk",
@@ -163,6 +172,7 @@
163172
}),
164173
};
165174
this.shellStreams.set(request.id, stream);
175+
this.latestShellStream = stream;
166176
latestShellStream = stream;
167177
if (delayedThread && !delayedStream) delayedStream = stream;
168178
} else if (
@@ -172,10 +182,16 @@
172182
this.threadRequests.add(request.id);
173183
}
174184
}
185+
if (changed)
186+
data = ndjson
187+
? value.map((item) => JSON.stringify(item)).join("\n") + "\n"
188+
: JSON.stringify(value);
175189
} catch {
176190
// Non-JSON socket traffic belongs to the native protocol handler.
177191
}
192+
super.send(data);
178193
}
194+
latestShellStream = null;
179195
shellStreams = new Map();
180196
threadRequests = new Set();
181197
replayDeliver;

0 commit comments

Comments
 (0)