Skip to content

Commit f9a30dd

Browse files
t3dotggclaude
andcommitted
fix(client-runtime): a socket-only open that hits a defect retries over HTTP
A defect parks the subscription, so a malformed socket snapshot left a readable thread empty on a healthy connection. The first socket-only open now resubscribes once, and that attempt loads over HTTP. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent c37d460 commit f9a30dd

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

‎packages/client-runtime/src/state/threads-sync.test.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ const BASE_PROJECTION: OrchestrationV2ThreadProjection = {
8080
thread: { ...v2Projection.thread, title: "Cached thread" },
8181
};
8282

83-
type TestThreadInput = OrchestrationV2ThreadStreamItem | Error;
83+
const TEST_DEFECT = { defect: "Malformed snapshot" } as const;
84+
85+
type TestThreadInput = OrchestrationV2ThreadStreamItem | Error | typeof TEST_DEFECT;
8486

8587
function testSession(client: WsRpcProtocolClient, config: object = {}): RpcSession.RpcSession {
8688
return {
@@ -144,7 +146,11 @@ const makeHarness = Effect.fn("TestEnvironmentThreads.makeHarness")(function* (o
144146
const streamFrom = (queue: Queue.Queue<TestThreadInput>) =>
145147
Stream.fromQueue(queue).pipe(
146148
Stream.mapEffect((input) =>
147-
input instanceof Error ? Effect.fail(input) : Effect.succeed(input),
149+
input === TEST_DEFECT
150+
? Effect.die(input)
151+
: input instanceof Error
152+
? Effect.fail(input)
153+
: Effect.succeed(input),
148154
),
149155
);
150156
const client = {
@@ -796,6 +802,15 @@ describe("EnvironmentThreads", () => {
796802
status: "deleted",
797803
loads: [0, 1],
798804
},
805+
// A defect never retries on its own, so the socket-only open resubscribes once.
806+
{
807+
label: "a socket-only open that hits a defect asks HTTP",
808+
socketSnapshots: true,
809+
httpSnapshot: { _tag: "missing" },
810+
failure: TEST_DEFECT,
811+
status: "deleted",
812+
loads: [0, 1],
813+
},
799814
// Authorization failures are never retried, so the error must show at once.
800815
{
801816
label: "a socket-only open shows an authorization error",

‎packages/client-runtime/src/state/threads.ts‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,13 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
953953
// apart from a failed load.
954954
const socketOpenTried = yield* Ref.make(false);
955955
const socketOnlyOpen = yield* Ref.make(false);
956+
const failedSocketOnlyOpen = Effect.gen(function* () {
957+
return (
958+
(yield* Ref.get(socketOnlyOpen)) && Option.isNone((yield* SubscriptionRef.get(state)).data)
959+
);
960+
});
961+
// A defect parks the subscription, so a socket-only open resubscribes once.
962+
const httpRetries = yield* Queue.unbounded<void>();
956963
const markSynchronizing = Effect.gen(function* () {
957964
if (yield* Ref.get(resumingLive)) return;
958965
yield* SubscriptionRef.update(state, (current) =>
@@ -1067,18 +1074,18 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
10671074
};
10681075
}),
10691076
{
1070-
onDefect: () => setStreamError("Could not synchronize the thread."),
10711077
// A socket-only open that fails retries over HTTP, which tells a
10721078
// missing thread apart from a failure, before showing an error.
1079+
onDefect: Effect.fn("EnvironmentThreadState.onDefect")(function* () {
1080+
if (yield* failedSocketOnlyOpen) return yield* Queue.offer(httpRetries, undefined);
1081+
yield* setStreamError("Could not synchronize the thread.");
1082+
}),
10731083
onExpectedFailure: Effect.fn("EnvironmentThreadState.onExpectedFailure")(function* (cause) {
1074-
const retryOverHttp =
1075-
!isAuthorizationFailure(cause) &&
1076-
(yield* Ref.get(socketOnlyOpen)) &&
1077-
Option.isNone((yield* SubscriptionRef.get(state)).data);
1084+
const retryOverHttp = !isAuthorizationFailure(cause) && (yield* failedSocketOnlyOpen);
10781085
if (!retryOverHttp) yield* setStreamError(formatThreadError(cause));
10791086
}),
10801087
retryExpectedFailureAfter: "250 millis",
1081-
resubscribe: foregroundResubscriptions,
1088+
resubscribe: Stream.merge(foregroundResubscriptions, Stream.fromQueue(httpRetries)),
10821089
},
10831090
).pipe(Stream.runForEachArray(applyItems)),
10841091
);

0 commit comments

Comments
 (0)