The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 23, 04:15 UTC
fix(server): browser control replies no longer wait behind old frames on slow links (#20290)
Co-authored-by: PR Batch Tester <agent@local.test> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
shivam committed
Oct 19, 2026 at 00:42 UTC
7c68c7dc8739e199e2668d0c02f467d83a9f5218
2 files changed
+99
-12
apps/server/src/preview/ServerBrowserStream.test.ts
+73
@@ -153,6 +153,79 @@ it.effect.each([
153
}).pipe(Effect.scoped),
154
);
155
156
+it.effect("sends replies ahead of stale frames and only the newest frame after an ack", () =>
157
+ Effect.gen(function* () {
158
+ const output = yield* Queue.make<ServerBrowser.ServerBrowserViewerOutput>();
159
+ const released: number[] = [];
160
+ const frame = (id: number): ServerBrowser.ServerBrowserViewerOutput => ({
161
+ _tag: "frame",
162
+ data: new Uint8Array([id]),
163
+ ack: Effect.sync(() => void released.push(id)),
164
+ });
165
+ const browser = ServerBrowser.ServerBrowser.of({
166
+ clearProfile: () => Effect.void,
167
+ reportProfiles: () => Effect.void,
168
+ openDownload: () => Effect.succeedNone,
169
+ answerFileChooser: () => Effect.succeed(false),
170
+ attachViewer: () => Effect.succeed({ output, input: () => Effect.void }),
171
+ });
172
+ const services = yield* Layer.build(
173
+ HttpRouter.serve(
174
+ routeLayer.pipe(
175
+ Layer.provide(Layer.succeed(ServerBrowser.ServerBrowser, browser)),
176
+ Layer.provide(platformLayer),
177
+ ),
178
+ { disableListenLog: true },
179
+ ).pipe(
180
+ Layer.provideMerge(NodeHttpServer.layerTest),
181
+ Layer.provide(makeAuth([AuthOrchestrationReadScope]).layer),
182
+ ),
183
+ );
184
+ const server = Context.get(services, HttpServer.HttpServer);
185
+ const origin = HttpServer.formatAddress(server.address).replace(/^http/, "ws");
186
+ const received: Array<number | string> = [];
187
+ let next = Promise.withResolvers<void>();
188
+ const socket = yield* Effect.acquireRelease(
189
+ Effect.sync(
190
+ () =>
191
+ new WebSocket(
192
+ `${origin}/api/preview-stream/ws?threadId=thread&tabId=tab&wsTicket=one-use-ticket`,
193
+ ),
194
+ ),
195
+ (socket) => Effect.sync(() => socket.close()),
196
+ );
197
+ socket.binaryType = "arraybuffer";
198
+ socket.addEventListener("message", (event) => {
199
+ received.push(
200
+ typeof event.data === "string"
201
+ ? JSON.parse(event.data).type
202
+ : new Uint8Array(event.data as ArrayBuffer)[0]!,
203
+ );
204
+ next.resolve();
205
+ });
206
+
207
+ yield* Queue.offer(output, frame(1));
208
+ yield* Effect.promise(() => next.promise);
209
+ next = Promise.withResolvers();
210
+ yield* Queue.offerAll(output, [
211
+ frame(2),
212
+ frame(3),
213
+ frame(4),
214
+ { _tag: "probe", x: 0, y: 0, editable: false },
215
+ ]);
216
+ yield* Effect.promise(() => next.promise);
217
+ expect(received).toEqual([1, "probe"]);
218
+ // Frames 2 and 3 were superseded before they could be sent.
219
+ expect(released).toEqual([2, 3]);
220
+
221
+ next = Promise.withResolvers();
222
+ socket.send(JSON.stringify({ type: "ack" }));
223
+ yield* Effect.promise(() => next.promise);
224
+ expect(received).toEqual([1, "probe", 4]);
225
+ expect(released).toEqual([2, 3, 1]);
226
+ }).pipe(Effect.scoped),
227
+);
228
+
229
it.effect.each([
230
{ scopes: [], error: undefined, status: 403 },
231
{ scopes: [AuthOrchestrationOperateScope], error: undefined, status: 403 },
apps/server/src/preview/ServerBrowserStream.ts
+26
-12
@@ -31,7 +31,6 @@ const TAB_GONE_CODE = 4404;
31
32
const DEFAULT_QUALITY = 70;
33
const MAX_SOCKET_BUFFER_BYTES = 8 * 1024 * 1024;
34
-const MAX_UNACKNOWLEDGED_FRAMES = 64;
34
const textDecoder = new TextDecoder();
35
36
const intParam = (params: URLSearchParams, name: string, fallback: number, max: number) => {
@@ -39,6 +38,8 @@ const intParam = (params: URLSearchParams, name: string, fallback: number, max:
38
return Number.isFinite(value) && value > 0 ? Math.min(Math.round(value), max) : fallback;
39
};
40
41
+type Frame = Extract<ServerBrowser.ServerBrowserViewerOutput, { readonly _tag: "frame" }>;
42
+
43
const isAck = (message: unknown) =>
44
typeof message === "object" && message !== null && "type" in message && message.type === "ack";
45
@@ -124,8 +125,11 @@ const makeHandler = (browser: ServerBrowser.ServerBrowser["Service"]) =>
125
}
126
const viewer = attached.viewer;
127
// `write` returns once the frame is queued, so Chromium's ack for each
127
- // frame waits for the viewer's `ack` message instead.
128
- const unacknowledged: Array<Effect.Effect<void>> = [];
128
+ // frame waits for the viewer's `ack` message instead. Only one frame
129
+ // is on the wire and only the newest waits behind it, so replies never
130
+ // queue behind stale images on a slow link.
131
+ let inFlight: Effect.Effect<void> | null = null;
132
+ let pending: Frame | null = null;
133
const disconnectSlowViewer = Effect.sync(() => transport.destroy()).pipe(
134
Effect.andThen(Effect.interrupt),
135
);
@@ -137,16 +141,20 @@ const makeHandler = (browser: ServerBrowser.ServerBrowser["Service"]) =>
141
}
142
return writer.write(data);
143
});
144
+ const sendFrame = (frame: Frame) => {
145
+ inFlight = frame.ack;
146
+ return write(frame.data);
147
+ };
148
const sendOutput = Queue.take(viewer.output).pipe(
149
Effect.flatMap((output) => {
150
switch (output._tag) {
143
- case "frame":
144
- if (unacknowledged.length >= MAX_UNACKNOWLEDGED_FRAMES) {
145
- return disconnectSlowViewer;
146
- }
147
- return write(output.data).pipe(
148
- Effect.andThen(Effect.sync(() => unacknowledged.push(output.ack))),
149
- );
151
+ case "frame": {
152
+ if (inFlight === null) return sendFrame(output);
153
+ const superseded = pending;
154
+ pending = output;
155
+ // Never sent, but Chromium still waits for its ack.
156
+ return superseded ? Effect.forkScoped(superseded.ack) : Effect.void;
157
+ }
158
case "viewport":
159
case "control":
160
case "clipboard":
@@ -176,9 +184,15 @@ const makeHandler = (browser: ServerBrowser.ServerBrowser["Service"]) =>
184
return canOperate
185
? viewer.input(message).pipe(Effect.forkScoped, Effect.asVoid)
186
: Effect.void;
179
- const ack = unacknowledged.shift();
187
+ const ack = inFlight;
188
+ const next = pending;
189
+ inFlight = null;
190
+ pending = null;
191
// Forked: Chromium acks are paced and must not hold up input.
181
- return ack ? Effect.forkScoped(ack).pipe(Effect.asVoid) : Effect.void;
192
+ return Effect.andThen(
193
+ ack ? Effect.forkScoped(ack) : Effect.void,
194
+ next ? sendFrame(next) : Effect.void,
195
+ );
196
};
197
const receiveInput = reader.pull.pipe(
198
Effect.flatMap((chunks) => Effect.forEach(chunks, receive, { discard: true })),