@devries-cameron20 / t3code Mirror · read-only / commits / 8ab024f31a
Fork 0

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 22, 04:30 UTC

fix(client-runtime): a thread keeps syncing after a stream defect (#31071)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Nov 4, 2026 at 11:24 UTC 8ab024f31a6d285015c4fb84e52a0183f614e237
2 files changed +34 -13
packages/client-runtime/src/rpc/client.test.ts
+28 -13
@@ -8,12 +8,12 @@ import { describe, expect, it } from "@effect/vitest";
8 import * as Cause from "effect/Cause";
9 import * as Deferred from "effect/Deferred";
10 import * as Effect from "effect/Effect";
11 -import * as Exit from "effect/Exit";
11 import * as Fiber from "effect/Fiber";
12 import * as Option from "effect/Option";
13 import * as Queue from "effect/Queue";
14 import * as Ref from "effect/Ref";
15 import * as Stream from "effect/Stream";
16 +import * as TestClock from "effect/testing/TestClock";
17 import * as SubscriptionRef from "effect/SubscriptionRef";
18 import { RpcClientError } from "effect/rpc";
19
@@ -347,7 +347,7 @@ describe("environment RPC", () => {
347 );
348
349 it.effect.each(["input", "stream"] as const)(
350 - "does not classify %s subscription defects as expected failures",
350 + "reports %s subscription defects and resubscribes on the next session",
351 (where) =>
352 Effect.gen(function* () {
353 const defect = new Error("subscription invariant failed");
@@ -355,25 +355,35 @@ describe("environment RPC", () => {
355 let inputs = 0;
356 let streams = 0;
357 const observedDefects: unknown[] = [];
358 - const client = {
358 + const defectObserved = yield* Deferred.make<void>();
359 + const resubscribed = yield* Deferred.make<void>();
360 + const firstClient = {
361 [WS_METHODS.subscribeTerminalMetadata]: () => {
362 streams += 1;
363 return where === "stream" ? Stream.die(defect) : Stream.never;
364 },
365 } as unknown as WsRpcProtocolClient;
366 + const secondClient = {
367 + [WS_METHODS.subscribeTerminalMetadata]: () => {
368 + Deferred.doneUnsafe(resubscribed, Effect.void);
369 + return Stream.never;
370 + },
371 + } as unknown as WsRpcProtocolClient;
372 const { activeSession, supervisor } = yield* makeHarness();
373
366 - yield* SubscriptionRef.set(activeSession, Option.some(session(client)));
367 - const exit = yield* subscribeDynamicWithSession(
374 + yield* SubscriptionRef.set(activeSession, Option.some(session(firstClient)));
375 + const subscriptionFiber = yield* subscribeDynamicWithSession(
376 WS_METHODS.subscribeTerminalMetadata,
377 () =>
370 - Effect.sync(() => {
378 + Effect.suspend(() => {
379 inputs += 1;
372 - }).pipe(Effect.andThen(where === "input" ? Effect.die(defect) : Effect.succeed({}))),
380 + return where === "input" && inputs === 1 ? Effect.die(defect) : Effect.succeed({});
381 + }),
382 {
383 onDefect: (cause) =>
384 Effect.sync(() => {
385 observedDefects.push(Cause.squash(cause));
386 + Deferred.doneUnsafe(defectObserved, Effect.void);
387 }),
388 onExpectedFailure: () =>
389 Effect.sync(() => {
@@ -384,18 +394,23 @@ describe("environment RPC", () => {
394 ).pipe(
395 Stream.runDrain,
396 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
387 - Effect.exit,
397 + Effect.forkChild,
398 );
399 + yield* Deferred.await(defectObserved);
400
390 - expect(Exit.isFailure(exit)).toBe(true);
391 - if (Exit.isFailure(exit)) {
392 - expect(Cause.hasDies(exit.cause)).toBe(true);
393 - expect(Cause.squash(exit.cause)).toBe(defect);
394 - }
401 expect(inputs).toBe(1);
402 expect(streams).toBe(where === "input" ? 0 : 1);
403 expect(expectedFailureCount).toBe(0);
404 expect(observedDefects).toEqual([defect]);
405 +
406 + // Let the defect settle before the next session arrives, as it does on a real device.
407 + yield* TestClock.adjust("1 second");
408 + expect(subscriptionFiber.pollUnsafe()).toBeUndefined();
409 + yield* SubscriptionRef.set(activeSession, Option.some(session(secondClient)));
410 + yield* Deferred.await(resubscribed);
411 + yield* Fiber.interrupt(subscriptionFiber);
412 +
413 + expect(inputs).toBe(2);
414 }),
415 );
416 });
packages/client-runtime/src/rpc/client.ts
+6
@@ -236,6 +236,7 @@ export function runStreamGuarded<TTag extends EnvironmentStreamCommandRpcTag>(
236 }
237
238 interface SubscriptionOptions<TTag extends EnvironmentSubscriptionRpcTag> {
239 + /** Reports defects; the subscription then waits for the next session or resubscribe. */
240 readonly onDefect?: (
241 cause: Cause.Cause<EnvironmentRpcStreamFailure<TTag>>,
242 ) => Effect.Effect<void, never, never>;
@@ -371,6 +372,11 @@ function subscribeDynamicMapped<TTag extends EnvironmentSubscriptionRpcTag, A>(
372 Stream.concat(subscribeToSession()),
373 );
374 }
375 + // A reported defect parks until the next session or resubscribe. Failing would
376 + // end the whole subscription, so a mounted screen never syncs again.
377 + if (options?.onDefect !== undefined && Cause.hasDies(cause)) {
378 + return Stream.empty;
379 + }
380 return Stream.failCause(cause);
381 }),
382 ),