@devries-cameron20 / t3code Mirror · read-only / commits / bd9ff7eb2a
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 21, 16:00 UTC

fix(client-runtime): recover thread list updates after stream defects (#33244)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

shivam committed Nov 14, 2026 at 11:51 UTC bd9ff7eb2a3bff5a12ea442af9d1d7af900ff2f2
2 files changed +87 -2
packages/client-runtime/src/state/shell-sync.test.ts
+68
@@ -8,6 +8,7 @@ import {
8 type ThreadPullRequestLink,
9 } from "@t3tools/contracts";
10 import { describe, expect, it } from "@effect/vitest";
11 +import * as Cause from "effect/Cause";
12 import * as Deferred from "effect/Deferred";
13 import * as Effect from "effect/Effect";
14 import * as Exit from "effect/Exit";
@@ -1580,6 +1581,73 @@ describe("environment shell cold start", () => {
1581 }),
1582 );
1583
1584 + it.effect("keeps syncing row status after a shell stream defect", () =>
1585 + Effect.gen(function* () {
1586 + const h = yield* makeColdStart(RECENT_ORIGIN);
1587 + const rowUpdate = (sequence: number, status: "completed" | "running") => ({
1588 + kind: "thread.updated" as const,
1589 + sequence,
1590 + location: "active" as const,
1591 + thread: { ...v2ThreadShell, status },
1592 + });
1593 + const liveRow = (sequence: number, status: "completed" | "running") =>
1594 + SubscriptionRef.changes(h.shellState).pipe(
1595 + Stream.filter(
1596 + (state) =>
1597 + state.status === "live" &&
1598 + Option.exists(
1599 + state.snapshot,
1600 + (snapshot) =>
1601 + snapshot.snapshotSequence === sequence &&
1602 + snapshot.threads.some(
1603 + (thread) => thread.id === v2ThreadShell.id && thread.status === status,
1604 + ),
1605 + ),
1606 + ),
1607 + Stream.runHead,
1608 + Effect.map(Option.getOrThrow),
1609 + );
1610 +
1611 + const first = yield* Queue.take(h.subscriptions);
1612 + yield* Queue.offerAll(first.events, [
1613 + rowUpdate(CACHED_SEQUENCE + 1, "completed"),
1614 + { kind: "synchronized" },
1615 + ]);
1616 + yield* liveRow(CACHED_SEQUENCE + 1, "completed");
1617 +
1618 + const failWithDefect = (events: typeof first.events) =>
1619 + Queue.failCause(events, Cause.die(new Error("shell invariant failed"))).pipe(
1620 + Effect.andThen(
1621 + SubscriptionRef.changes(h.shellState).pipe(
1622 + Stream.filter((state) =>
1623 + Option.exists(
1624 + state.error,
1625 + (error) => error === "Could not synchronize environment data.",
1626 + ),
1627 + ),
1628 + Stream.runHead,
1629 + ),
1630 + ),
1631 + );
1632 +
1633 + yield* failWithDefect(first.events);
1634 + yield* h.reconnect;
1635 + const resumed = yield* Queue.take(h.subscriptions);
1636 + expect(resumed.input).toMatchObject({ afterSequence: CACHED_SEQUENCE + 1 });
1637 + yield* Queue.offerAll(resumed.events, [
1638 + rowUpdate(CACHED_SEQUENCE + 2, "running"),
1639 + { kind: "synchronized" },
1640 + ]);
1641 + expect((yield* liveRow(CACHED_SEQUENCE + 2, "running")).error).toEqual(Option.none());
1642 +
1643 + // Without a reconnect or wakeup, the stream resubscribes once the backoff passes.
1644 + yield* failWithDefect(resumed.events);
1645 + yield* TestClock.adjust("250 millis");
1646 + const retried = yield* Queue.take(h.subscriptions);
1647 + expect(retried.input).toMatchObject({ afterSequence: CACHED_SEQUENCE + 2 });
1648 + }),
1649 + );
1650 +
1651 it.effect("reloads over HTTP when the server answers a resume with snapshot-required", () =>
1652 Effect.gen(function* () {
1653 const h = yield* makeColdStart(RECENT_ORIGIN);
packages/client-runtime/src/state/shell.ts
+19 -2
@@ -30,7 +30,7 @@ import * as ConnectionWakeups from "../connection/wakeups.ts";
30 import { safeErrorLogAttributes } from "../errors/safeLog.ts";
31 import * as Persistence from "../platform/persistence.ts";
32 import { runCachePersistence } from "./cachePersistence.ts";
33 -import { subscribeDynamicWithSession } from "../rpc/client.ts";
33 +import { subscribeDynamicWithSession, subscriptionRetryDelay } from "../rpc/client.ts";
34 import type { RpcSession } from "../rpc/session.ts";
35 import * as ShellSnapshotLoader from "./shellSnapshotHttp.ts";
36 import type { DeferredShellSnapshot } from "./shellPullRequests.ts";
@@ -203,6 +203,7 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
203 // Only a server snapshot renews it; `snapshot-required` drops it.
204 const origin = yield* Ref.make(cachedOrigin);
205 const reloadRequests = yield* Queue.unbounded<void>();
206 + let defectRetries = 0;
207 const latestLive = yield* Ref.make<Option.Option<LiveShell>>(Option.none());
208 const persistence = yield* Queue.sliding<OrchestrationV2ShellSnapshot>(1);
209
@@ -702,11 +703,27 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
703 };
704 }),
705 {
706 + // A defect parks the stream, so resubscribe after a backoff rather than wait for a wakeup.
707 + onDefect: (cause) =>
708 + setStreamError(Cause.squash(cause)).pipe(
709 + Effect.andThen(Effect.sleep(subscriptionRetryDelay("250 millis", defectRetries++))),
710 + Effect.andThen(Queue.offer(reloadRequests, undefined)),
711 + ),
712 onExpectedFailure: (cause) => setStreamError(Cause.squash(cause)),
713 retryExpectedFailureAfter: "250 millis",
714 resubscribe: resubscriptions,
715 },
709 - ).pipe(Stream.runForEachArray((entries) => applyItems(entries))),
716 + ).pipe(
717 + Stream.runForEachArray((entries) =>
718 + applyItems(entries).pipe(
719 + Effect.andThen(
720 + Effect.sync(() => {
721 + defectRetries = 0;
722 + }),
723 + ),
724 + ),
725 + ),
726 + ),
727 );
728 yield* SubscriptionRef.changes(supervisor.state).pipe(
729 Stream.runForEach((connectionState) => {