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, 02:00 UTC
perf(mobile): threads open over the socket without an extra HTTP round trip (#33306)
Cold thread opens use the bounded socket snapshot once and fall back to HTTP after any failure, defect, or dropped session. Connects reuse a descriptor a check just read, only over HTTPS, only on an id match, within 5 s. Per-frame socket deflate from #33172 was dropped because the iOS socket already negotiates permessage-deflate. Continues #33172. Co-authored-by: Bil0000 <62337003+Bil0000@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Theo Browne committed
Nov 9, 2026 at 04:01 UTC
dc3fdc6a3eb6e7fd4db28585ac5f2c8632bdfcf9
9 files changed
+373
-104
packages/client-runtime/src/authorization/layer.test.ts
+61
@@ -2,6 +2,7 @@ import {
2
AuthStandardClientScopes,
3
EnvironmentId,
4
ORCHESTRATION_PROTOCOL_VERSION,
5
+ type ExecutionEnvironmentDescriptor,
6
} from "@t3tools/contracts";
7
import { type RelayEnvironmentConnectResponse } from "@t3tools/contracts/relay";
8
import { describe, expect, it } from "@effect/vitest";
@@ -317,6 +318,66 @@ describe("RemoteEnvironmentAuthorization", () => {
318
}),
319
);
320
321
+ it.effect("reuses a matching descriptor a check just read, once, while fresh, over HTTPS", () =>
322
+ Effect.gen(function* () {
323
+ const descriptor = {
324
+ ...DESCRIPTOR,
325
+ orchestrationProtocolVersion: ORCHESTRATION_PROTOCOL_VERSION,
326
+ } as ExecutionEnvironmentDescriptor;
327
+ const harness = yield* makeHarness({
328
+ responses: [
329
+ websocketTicket("reused"),
330
+ ...Array.from({ length: 5 }, () => [
331
+ Response.json(descriptor),
332
+ websocketTicket("fresh"),
333
+ ]).flat(),
334
+ ],
335
+ });
336
+
337
+ yield* Effect.gen(function* () {
338
+ const remote = yield* RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization;
339
+ const authorizeAt = (httpBaseUrl: string) =>
340
+ remote.authorizeBearer({
341
+ expectedEnvironmentId: ENVIRONMENT_ID,
342
+ httpBaseUrl,
343
+ wsBaseUrl: httpBaseUrl.replace(/^http/, "ws"),
344
+ bearerToken: "bearer-token",
345
+ connectionMethod: "direct",
346
+ });
347
+ const authorize = authorizeAt(ENDPOINT.httpBaseUrl);
348
+ const remember = (value: Option.Option<ExecutionEnvironmentDescriptor>) =>
349
+ remote.rememberCheckedDescriptor(ENDPOINT.httpBaseUrl, value);
350
+
351
+ yield* remember(Option.some(descriptor));
352
+ yield* authorize;
353
+ // Used once.
354
+ yield* authorize;
355
+ // Stale.
356
+ yield* remember(Option.some(descriptor));
357
+ yield* TestClock.adjust("6 seconds");
358
+ yield* authorize;
359
+ // A mismatch reads again, so a host back on the right environment is not blocked.
360
+ yield* remember(Option.some({ ...descriptor, environmentId: "other" as never }));
361
+ yield* authorize;
362
+ // A failed check drops an older match.
363
+ yield* remember(Option.some(descriptor));
364
+ yield* remember(Option.none());
365
+ yield* authorize;
366
+ // After a network change, a plain LAN address can belong to another machine.
367
+ yield* remote.rememberCheckedDescriptor("http://192.168.1.2:3773", Option.some(descriptor));
368
+ yield* authorizeAt("http://192.168.1.2:3773");
369
+ }).pipe(Effect.provide(harness.layer));
370
+
371
+ expect(harness.fetch.calls.map(([url]) => new URL(String(url)).pathname)).toEqual([
372
+ "/api/auth/websocket-ticket/rpc",
373
+ ...Array.from({ length: 5 }, () => [
374
+ "/.well-known/t3/environment",
375
+ "/api/auth/websocket-ticket/rpc",
376
+ ]).flat(),
377
+ ]);
378
+ }),
379
+ );
380
+
381
it.effect("blocks a warm direct bearer prepare when the environment changes identity", () =>
382
Effect.gen(function* () {
383
const harness = yield* makeHarness({
packages/client-runtime/src/authorization/service.ts
+37
-3
@@ -82,6 +82,11 @@ export class RemoteEnvironmentAuthorization extends Context.Service<
82
readonly expectedEnvironmentId: EnvironmentId;
83
readonly rejectedAccessToken?: string;
84
}) => Effect.Effect<AuthorizedRemoteHttpEnvironment, ConnectionAttemptError>;
85
+ /** Lets the next direct authorization of this address skip its own descriptor read. */
86
+ readonly rememberCheckedDescriptor: (
87
+ httpBaseUrl: string,
88
+ descriptor: Option.Option<ExecutionEnvironmentDescriptor>,
89
+ ) => Effect.Effect<void>;
90
/**
91
* Renews a live socket's token before it expires, so the server moves the
92
* socket to the new session instead of closing it. Runs until interrupted.
@@ -95,6 +100,7 @@ export class RemoteEnvironmentAuthorization extends Context.Service<
100
101
const CACHED_ENDPOINT_SOCKET_TIMEOUT_MS = 3_000;
102
const DPOP_AUTHORIZATION_TIMEOUT_MS = 30_000;
103
+const CHECKED_DESCRIPTOR_TTL_NANOS = 5_000_000_000n;
104
105
function mapDpopSocketError(error: RemoteEnvironmentAuthError | ConnectionAttemptError) {
106
return error._tag === "ConnectionTransientError" || error._tag === "ConnectionBlockedError"
@@ -135,6 +141,14 @@ export const make = Effect.gen(function* () {
141
}
142
>();
143
144
+ // A route or host check hands its descriptor to the connect it leads to, once,
145
+ // so the connect skips a round trip. Only over HTTPS: a plain LAN address can
146
+ // belong to another machine after a network change.
147
+ const checkedDescriptors = new Map<
148
+ string,
149
+ { readonly descriptor: ExecutionEnvironmentDescriptor; readonly at: bigint }
150
+ >();
151
+
152
/**
153
* A saved LAN address can belong to another machine on a different network.
154
*/
@@ -144,9 +158,18 @@ export const make = Effect.gen(function* () {
158
readonly httpBaseUrl: string;
159
readonly connectionMethod: ClientConnectionMethod;
160
}) {
147
- const descriptor = yield* fetchDescriptor(input.httpBaseUrl, input.connectionMethod).pipe(
148
- Effect.provideService(HttpClient.HttpClient, httpClient),
149
- );
161
+ const checked = checkedDescriptors.get(input.httpBaseUrl);
162
+ checkedDescriptors.delete(input.httpBaseUrl);
163
+ // A remembered mismatch still reads again, so a host that answers for the
164
+ // right environment a moment later is not blocked.
165
+ const descriptor =
166
+ checked !== undefined &&
167
+ checked.descriptor.environmentId === input.expectedEnvironmentId &&
168
+ (yield* Clock.monotonicTimeNanos) - checked.at <= CHECKED_DESCRIPTOR_TTL_NANOS
169
+ ? checked.descriptor
170
+ : yield* fetchDescriptor(input.httpBaseUrl, input.connectionMethod).pipe(
171
+ Effect.provideService(HttpClient.HttpClient, httpClient),
172
+ );
173
if (descriptor.environmentId !== input.expectedEnvironmentId) {
174
return yield* environmentMismatchError({
175
expected: input.expectedEnvironmentId,
@@ -527,6 +550,17 @@ export const make = Effect.gen(function* () {
550
551
return RemoteEnvironmentAuthorization.of({
552
authorizeBearer,
553
+ // Untraced: it runs on every route and host check.
554
+ rememberCheckedDescriptor: Effect.fnUntraced(function* (httpBaseUrl, descriptor) {
555
+ if (Option.isNone(descriptor) || !httpBaseUrl.startsWith("https:")) {
556
+ checkedDescriptors.delete(httpBaseUrl);
557
+ return;
558
+ }
559
+ checkedDescriptors.set(httpBaseUrl, {
560
+ descriptor: descriptor.value,
561
+ at: yield* Clock.monotonicTimeNanos,
562
+ });
563
+ }),
564
authorizeDpop: Effect.fn("environment.authorization")(function (input) {
565
return authorizeDpop(input);
566
}),
packages/client-runtime/src/connection/driver.ts
+8
-2
@@ -1,5 +1,6 @@
1
import * as Context from "effect/Context";
2
import * as Effect from "effect/Effect";
3
+import * as Option from "effect/Option";
4
import * as Exit from "effect/Exit";
5
import * as Fiber from "effect/Fiber";
6
import * as Layer from "effect/Layer";
@@ -152,10 +153,15 @@ export const make = Effect.gen(function* () {
153
httpBaseUrl,
154
timeoutMs: ROUTE_CHECK_TIMEOUT_MS,
155
}).pipe(
156
+ Effect.option,
157
+ // The connect this check leads to skips its own read. Every answer, even a
158
+ // failure, replaces what an older check remembered.
159
+ Effect.tap((descriptor) => remote.rememberCheckedDescriptor(httpBaseUrl, descriptor)),
160
Effect.map((descriptor): RouteCheck =>
156
- descriptor.environmentId === environmentId ? "answered" : "silent",
161
+ Option.exists(descriptor, (value) => value.environmentId === environmentId)
162
+ ? "answered"
163
+ : "silent",
164
),
158
- Effect.orElseSucceed((): RouteCheck => "silent"),
165
// A cached descriptor would answer for a host that cannot be reached.
166
Effect.provideService(FetchHttpClient.RequestInit, { cache: "no-store" }),
167
Effect.provideService(HttpClient.HttpClient, httpClient),
packages/client-runtime/src/connection/resolver.test.ts
+1
@@ -108,6 +108,7 @@ const makeDependencies = Effect.fn("TestConnectionResolver.makeDependencies")((o
108
},
109
})),
110
authorizeDpopHttp: () => Effect.die("unused"),
111
+ rememberCheckedDescriptor: () => Effect.void,
112
renewDpopBeforeExpiry: () => Effect.die("unused"),
113
});
114
const ssh = ClientCapabilities.SshEnvironmentGateway.of({
packages/client-runtime/src/rpc/client.ts
+7
-5
@@ -106,6 +106,12 @@ export class EnvironmentRpcSubscriptionObserver extends Context.Reference<{
106
export const isRpcClientError = Schema.is(RpcClientError.RpcClientError);
107
const isEnvironmentAuthorizationError = Schema.is(EnvironmentAuthorizationError);
108
109
+/** A subscription never retries an authorization failure on its own. */
110
+export const isAuthorizationFailure = (cause: Cause.Cause<unknown>) =>
111
+ cause.reasons.some(
112
+ (reason) => reason._tag === "Fail" && isEnvironmentAuthorizationError(reason.error),
113
+ );
114
+
115
const MAX_EXPECTED_FAILURE_RETRY_DELAY_MS = 30_000;
116
117
export type EnvironmentRpcInput<TTag extends EnvironmentRpcTag> = Parameters<RpcMethod<TTag>>[0];
@@ -359,13 +365,9 @@ function subscribeDynamicMapped<TTag extends EnvironmentSubscriptionRpcTag, A>(
365
const handled = Stream.fromEffect(options.onExpectedFailure(cause)).pipe(
366
Stream.drain,
367
);
362
- const isAuthorizationFailure = cause.reasons.some(
363
- (reason) =>
364
- reason._tag === "Fail" && isEnvironmentAuthorizationError(reason.error),
365
- );
368
if (
369
options.retryExpectedFailureAfter === undefined ||
368
- isAuthorizationFailure
370
+ isAuthorizationFailure(cause)
371
) {
372
return handled;
373
}
packages/client-runtime/src/state/environmentHttpAuth.test.ts
+1
@@ -146,6 +146,7 @@ function makeHarness(reply: (requestNumber: number) => Response | Promise<Respon
146
},
147
};
148
}),
149
+ rememberCheckedDescriptor: () => Effect.void,
150
renewDpopBeforeExpiry: () => Effect.die("HTTP requests must not renew a socket's token."),
151
});
152
const signer = ManagedRelay.ManagedRelayDpopSigner.of({
packages/client-runtime/src/state/threads-atoms.test.ts
+81
-82
@@ -53,7 +53,7 @@ const SNAPSHOT: OrchestrationV2ThreadDetailSnapshot = { snapshotSequence: 7, pro
53
54
const makeHarness = Effect.fn("TestThreadAtoms.makeHarness")(function* (options?: {
55
readonly snapshot?: OrchestrationV2ThreadDetailSnapshot;
56
- readonly snapshotUnavailable?: boolean;
56
+ readonly socketSnapshot?: false;
57
}) {
58
const subscriptions = yield* Queue.unbounded<{
59
readonly afterSequence: number | undefined;
@@ -87,6 +87,10 @@ const makeHarness = Effect.fn("TestThreadAtoms.makeHarness")(function* (options?
87
}).pipe(Effect.andThen(Deferred.succeed(closed, undefined))),
88
);
89
yield* Queue.offer(subscriptions, { afterSequence: input.afterSequence, events, closed });
90
+ // Like a paging server, a fresh subscribe opens with the bounded snapshot.
91
+ if (input.afterSequence === undefined && options?.socketSnapshot !== false) {
92
+ yield* Queue.offer(events, { kind: "snapshot", ...snapshot });
93
+ }
94
return Stream.fromQueue(events);
95
}),
96
),
@@ -191,9 +195,6 @@ const makeHarness = Effect.fn("TestThreadAtoms.makeHarness")(function* (options?
195
load: () =>
196
Effect.sync(() => {
197
httpLoads += 1;
194
- if (options?.snapshotUnavailable === true) {
195
- return { _tag: "unavailable" as const };
196
- }
198
return {
199
_tag: "present" as const,
200
snapshot,
@@ -286,7 +287,7 @@ describe("createEnvironmentThreadStateAtoms", () => {
287
expect(currentThread(h.registry, h.stateAtom)).toBe(before);
288
const next = yield* Queue.take(h.subscriptions);
289
expect(next.afterSequence).toBe(8);
289
- expect(h.counts()).toEqual({ httpLoads: 1, diskLoads: 1, opened: 2, active: 1 });
290
+ expect(h.counts()).toEqual({ httpLoads: 0, diskLoads: 1, opened: 2, active: 1 });
291
remount();
292
yield* Deferred.await(next.closed);
293
}),
@@ -316,7 +317,7 @@ describe("createEnvironmentThreadStateAtoms", () => {
317
const retrying = yield* h.loadEarlier().pipe(Effect.forkScoped);
318
const retried = yield* Queue.take(h.olderLoads);
319
expect(retried.cursor).toBe("older-1");
319
- expect(h.counts().httpLoads).toBe(1);
320
+ expect(h.counts().httpLoads).toBe(0);
321
remount();
322
yield* Deferred.await(next.closed);
323
yield* Deferred.await(retried.closed);
@@ -324,86 +325,84 @@ describe("createEnvironmentThreadStateAtoms", () => {
325
}),
326
);
327
327
- it.effect(
328
- "merges older history after an HTTP failure falls back to a bounded socket snapshot",
329
- () =>
330
- Effect.gen(function* () {
331
- const h = yield* makeHarness({ snapshotUnavailable: true });
332
- const unmount = h.registry.mount(h.stateAtom);
333
- const subscription = yield* Queue.take(h.subscriptions);
334
- expect(subscription.afterSequence).toBeUndefined();
328
+ it.effect("opens from the bounded socket snapshot without HTTP and merges older history", () =>
329
+ Effect.gen(function* () {
330
+ const h = yield* makeHarness({ socketSnapshot: false });
331
+ const unmount = h.registry.mount(h.stateAtom);
332
+ const subscription = yield* Queue.take(h.subscriptions);
333
+ expect(subscription.afterSequence).toBeUndefined();
334
336
- yield* Queue.offer(subscription.events, {
337
- kind: "snapshot",
338
- snapshotSequence: 12,
339
- projection: THREAD,
340
- historyCursor: "socket-older-1",
341
- hasMoreHistory: true,
342
- latestLocalTurnOrdinal: 4,
343
- });
344
- yield* Queue.offer(subscription.events, { kind: "synchronized" });
345
- yield* observeState(
346
- h.registry,
347
- h.stateAtom,
348
- (state) => state.status === "live" && state.history.historyCursor === "socket-older-1",
349
- );
335
+ yield* Queue.offer(subscription.events, {
336
+ kind: "snapshot",
337
+ snapshotSequence: 12,
338
+ projection: THREAD,
339
+ historyCursor: "socket-older-1",
340
+ hasMoreHistory: true,
341
+ latestLocalTurnOrdinal: 4,
342
+ });
343
+ yield* Queue.offer(subscription.events, { kind: "synchronized" });
344
+ yield* observeState(
345
+ h.registry,
346
+ h.stateAtom,
347
+ (state) => state.status === "live" && state.history.historyCursor === "socket-older-1",
348
+ );
349
351
- const loading = yield* h.loadEarlier().pipe(Effect.forkScoped);
352
- const request = yield* Queue.take(h.olderLoads);
353
- expect(request.cursor).toBe("socket-older-1");
354
- const olderItem = {
355
- id: TurnItemId.make("socket-fallback-older-item"),
356
- type: "command_execution" as const,
357
- threadId: THREAD_ID,
358
- runId: null,
359
- nodeId: null,
360
- providerThreadId: null,
361
- providerTurnId: null,
362
- nativeItemRef: null,
363
- parentItemId: null,
364
- ordinal: 1,
365
- status: "completed" as const,
366
- title: "Earlier command",
367
- input: "pwd",
368
- output: "/workspace",
369
- exitCode: 0,
370
- startedAt: THREAD.thread.createdAt,
371
- completedAt: THREAD.thread.createdAt,
372
- updatedAt: THREAD.thread.createdAt,
373
- };
374
- yield* Deferred.succeed(request.response, {
375
- snapshotSequence: 12,
376
- items: [
377
- {
378
- position: 0,
379
- visibility: "local",
380
- sourceThreadId: THREAD_ID,
381
- sourceItemId: olderItem.id,
382
- item: olderItem,
383
- },
384
- ],
385
- nextCursor: null,
386
- hasMoreHistory: false,
387
- });
388
- expect(yield* Fiber.join(loading)).toEqual({ _tag: "loaded" });
350
+ const loading = yield* h.loadEarlier().pipe(Effect.forkScoped);
351
+ const request = yield* Queue.take(h.olderLoads);
352
+ expect(request.cursor).toBe("socket-older-1");
353
+ const olderItem = {
354
+ id: TurnItemId.make("socket-fallback-older-item"),
355
+ type: "command_execution" as const,
356
+ threadId: THREAD_ID,
357
+ runId: null,
358
+ nodeId: null,
359
+ providerThreadId: null,
360
+ providerTurnId: null,
361
+ nativeItemRef: null,
362
+ parentItemId: null,
363
+ ordinal: 1,
364
+ status: "completed" as const,
365
+ title: "Earlier command",
366
+ input: "pwd",
367
+ output: "/workspace",
368
+ exitCode: 0,
369
+ startedAt: THREAD.thread.createdAt,
370
+ completedAt: THREAD.thread.createdAt,
371
+ updatedAt: THREAD.thread.createdAt,
372
+ };
373
+ yield* Deferred.succeed(request.response, {
374
+ snapshotSequence: 12,
375
+ items: [
376
+ {
377
+ position: 0,
378
+ visibility: "local",
379
+ sourceThreadId: THREAD_ID,
380
+ sourceItemId: olderItem.id,
381
+ item: olderItem,
382
+ },
383
+ ],
384
+ nextCursor: null,
385
+ hasMoreHistory: false,
386
+ });
387
+ expect(yield* Fiber.join(loading)).toEqual({ _tag: "loaded" });
388
390
- const state = h.registry.get(h.stateAtom);
391
- expect(
392
- Option.getOrThrow(state.data).visibleTurnItems.map((row) => row.sourceItemId),
393
- ).toEqual([olderItem.id]);
394
- expect(state.history).toMatchObject({
395
- historyCursor: null,
396
- hasMoreHistory: false,
397
- loading: false,
398
- error: null,
399
- expanded: true,
400
- latestLocalTurnOrdinal: 4,
401
- });
402
- expect(h.counts().httpLoads).toBe(1);
389
+ const state = h.registry.get(h.stateAtom);
390
+ expect(Option.getOrThrow(state.data).visibleTurnItems.map((row) => row.sourceItemId)).toEqual(
391
+ [olderItem.id],
392
+ );
393
+ expect(state.history).toMatchObject({
394
+ historyCursor: null,
395
+ hasMoreHistory: false,
396
+ loading: false,
397
+ error: null,
398
+ expanded: true,
399
+ latestLocalTurnOrdinal: 4,
400
+ });
401
+ expect(h.counts().httpLoads).toBe(0);
402
404
- unmount();
405
- yield* Deferred.await(subscription.closed);
406
- }),
403
+ unmount();
404
+ yield* Deferred.await(subscription.closed);
405
+ }),
406
);
407
});
408
packages/client-runtime/src/state/threads-sync.test.ts
+139
-6
@@ -1,4 +1,5 @@
1
import {
2
+ EnvironmentAuthorizationError,
3
EnvironmentId,
4
EventId,
5
MessageId,
@@ -79,12 +80,17 @@ const BASE_PROJECTION: OrchestrationV2ThreadProjection = {
80
thread: { ...v2Projection.thread, title: "Cached thread" },
81
};
82
82
-type TestThreadInput = OrchestrationV2ThreadStreamItem | Error;
83
+const TEST_DEFECT = { defect: "Malformed snapshot" } as const;
84
84
-function testSession(client: WsRpcProtocolClient): RpcSession.RpcSession {
85
+type TestThreadInput = OrchestrationV2ThreadStreamItem | Error | typeof TEST_DEFECT;
86
+
87
+function testSession(
88
+ client: WsRpcProtocolClient,
89
+ initialConfig: Effect.Effect<object> = Effect.succeed({}),
90
+): RpcSession.RpcSession {
91
return {
92
client,
87
- initialConfig: Effect.succeed({} as never),
93
+ initialConfig: initialConfig as never,
94
subscribeServerConfig: (input = {}) => client.subscribeServerConfig(input),
95
ready: Effect.void,
96
probe: Effect.void,
@@ -118,6 +124,8 @@ const makeHarness = Effect.fn("TestEnvironmentThreads.makeHarness")(function* (o
124
readonly loadCached?: ReturnType<Persistence.EnvironmentCacheStore["Service"]["loadThread"]>;
125
readonly saveThread?: Persistence.EnvironmentCacheStore["Service"]["saveThread"];
126
readonly historyPaging?: "enabled" | "no-http" | "no-controller";
127
+ readonly socketSnapshots?: true;
128
+ readonly initialConfig?: Effect.Effect<object>;
129
readonly historyHttpClient?: HttpClient.HttpClient;
130
readonly getTurnItem?: (input: {
131
readonly itemId: TurnItemId;
@@ -142,7 +150,11 @@ const makeHarness = Effect.fn("TestEnvironmentThreads.makeHarness")(function* (o
150
const streamFrom = (queue: Queue.Queue<TestThreadInput>) =>
151
Stream.fromQueue(queue).pipe(
152
Stream.mapEffect((input) =>
145
- input instanceof Error ? Effect.fail(input) : Effect.succeed(input),
153
+ input === TEST_DEFECT
154
+ ? Effect.die(input)
155
+ : input instanceof Error
156
+ ? Effect.fail(input)
157
+ : Effect.succeed(input),
158
),
159
);
160
const client = {
@@ -170,8 +182,11 @@ const makeHarness = Effect.fn("TestEnvironmentThreads.makeHarness")(function* (o
182
[ORCHESTRATION_V2_WS_METHODS.getTurnItem]: (input: { readonly itemId: TurnItemId }) =>
183
options?.getTurnItem?.(input) ?? Effect.die("Unexpected getTurnItem request"),
184
} as unknown as WsRpcProtocolClient;
185
+ const sessionConfig =
186
+ options?.initialConfig ??
187
+ Effect.succeed(options?.socketSnapshots ? { threadSnapshotPagination: true } : {});
188
const supervisorSession = yield* SubscriptionRef.make<Option.Option<RpcSession.RpcSession>>(
174
- Option.some(testSession(client)),
189
+ Option.some(testSession(client, sessionConfig)),
190
);
191
const prepared = yield* SubscriptionRef.make<Option.Option<PreparedConnection>>(
192
Option.some(PREPARED),
@@ -290,7 +305,10 @@ const makeHarness = Effect.fn("TestEnvironmentThreads.makeHarness")(function* (o
305
savedThreads,
306
removedThreads,
307
wakeups,
293
- replaceSession: SubscriptionRef.set(supervisorSession, Option.some(testSession(client))),
308
+ replaceSession: SubscriptionRef.set(
309
+ supervisorSession,
310
+ Option.some(testSession(client, sessionConfig)),
311
+ ),
312
};
313
});
314
@@ -780,6 +798,121 @@ describe("EnvironmentThreads", () => {
798
}),
799
);
800
801
+ it.effect.each([
802
+ // The HTTP retry decides, so a missing thread never flashes an error.
803
+ {
804
+ label: "a socket-only open asks HTTP",
805
+ socketSnapshots: true,
806
+ httpSnapshot: { _tag: "missing" },
807
+ failure: new Error("Thread not found"),
808
+ status: "deleted",
809
+ loads: [0, 1],
810
+ },
811
+ // A defect never retries on its own, so the socket-only open resubscribes once.
812
+ {
813
+ label: "a socket-only open that hits a defect asks HTTP",
814
+ socketSnapshots: true,
815
+ httpSnapshot: { _tag: "missing" },
816
+ failure: TEST_DEFECT,
817
+ status: "deleted",
818
+ loads: [0, 1],
819
+ },
820
+ // Authorization failures are never retried, so the error must show at once.
821
+ {
822
+ label: "a socket-only open shows an authorization error",
823
+ socketSnapshots: true,
824
+ httpSnapshot: { _tag: "missing" },
825
+ failure: new EnvironmentAuthorizationError({
826
+ message: "Not allowed",
827
+ requiredScope: "orchestration:read",
828
+ }),
829
+ status: "empty",
830
+ loads: [0, 0],
831
+ },
832
+ // HTTP already answered this attempt, so the error shows before the usual retry.
833
+ {
834
+ label: "an HTTP open shows the error",
835
+ socketSnapshots: undefined,
836
+ httpSnapshot: { _tag: "unavailable" },
837
+ failure: new Error("Socket closed"),
838
+ status: "empty",
839
+ loads: [1, 2],
840
+ },
841
+ ] as const)(
842
+ "after a cold open fails, $label",
843
+ ({ socketSnapshots, httpSnapshot, failure, status, loads: [before, after] }) =>
844
+ Effect.gen(function* () {
845
+ const harness = yield* makeHarness({
846
+ httpSnapshot,
847
+ ...(socketSnapshots ? { socketSnapshots } : {}),
848
+ });
849
+ yield* SubscriptionRef.changes(harness.subscriptionCount).pipe(
850
+ Stream.filter((count) => count === 1),
851
+ Stream.runHead,
852
+ );
853
+ expect(yield* Ref.get(harness.lastAcceptBoundedSnapshot)).toBe(true);
854
+ expect(yield* Ref.get(harness.loaderCalls)).toBe(before);
855
+
856
+ yield* Queue.offer(harness.inputs, failure);
857
+ yield* TestClock.adjust("250 millis");
858
+ const settled = yield* awaitThreadState(
859
+ harness.observed,
860
+ (value) => value.status === "deleted" || Option.isSome(value.error),
861
+ );
862
+ expect(settled.status).toBe(status);
863
+ expect(yield* Ref.get(harness.loaderCalls)).toBe(after);
864
+ }),
865
+ );
866
+
867
+ it.effect("retries a defective socket-only open over HTTP only once", () =>
868
+ Effect.gen(function* () {
869
+ let configReads = 0;
870
+ const harness = yield* makeHarness({
871
+ httpSnapshot: { _tag: "missing" },
872
+ // The retry's config read dies too, so a stale flag would resubscribe forever.
873
+ initialConfig: Effect.suspend(() =>
874
+ ++configReads === 1
875
+ ? Effect.succeed({ threadSnapshotPagination: true })
876
+ : Effect.die("Config lost"),
877
+ ),
878
+ });
879
+ yield* SubscriptionRef.changes(harness.subscriptionCount).pipe(
880
+ Stream.filter((count) => count === 1),
881
+ Stream.runHead,
882
+ );
883
+
884
+ yield* Queue.offer(harness.inputs, TEST_DEFECT);
885
+ const settled = yield* awaitThreadState(harness.observed, (value) =>
886
+ Option.isSome(value.error),
887
+ );
888
+ expect(settled.status).toBe("empty");
889
+ expect(configReads).toBe(2);
890
+ }),
891
+ );
892
+
893
+ it.effect("asks HTTP when a socket-only open loses its session before any data", () =>
894
+ Effect.gen(function* () {
895
+ const harness = yield* makeHarness({
896
+ httpSnapshot: { _tag: "missing" },
897
+ socketSnapshots: true,
898
+ });
899
+ yield* SubscriptionRef.changes(harness.subscriptionCount).pipe(
900
+ Stream.filter((count) => count === 1),
901
+ Stream.runHead,
902
+ );
903
+ expect(yield* Ref.get(harness.loaderCalls)).toBe(0);
904
+
905
+ // A timed-out session never reaches onExpectedFailure.
906
+ yield* harness.replaceSession;
907
+ const settled = yield* awaitThreadState(
908
+ harness.observed,
909
+ (value) => value.status === "deleted",
910
+ );
911
+ expect(settled.status).toBe("deleted");
912
+ expect(yield* Ref.get(harness.loaderCalls)).toBe(1);
913
+ }),
914
+ );
915
+
916
it.effect("resumes a warm cache via afterSequence without an HTTP fetch", () =>
917
Effect.gen(function* () {
918
const harness = yield* makeHarness({ cached: BASE_PROJECTION });
packages/client-runtime/src/state/threads.ts
+38
-6
@@ -32,7 +32,7 @@ import * as ConnectionWakeups from "../connection/wakeups.ts";
32
import * as Persistence from "../platform/persistence.ts";
33
import { runCachePersistence } from "./cachePersistence.ts";
34
import * as ManagedRelay from "../relay/managedRelay.ts";
35
-import { request, subscribeDynamic } from "../rpc/client.ts";
35
+import { isAuthorizationFailure, request, subscribeDynamic } from "../rpc/client.ts";
36
import { parseThreadKey, threadKey } from "./entities.ts";
37
import { applyOrchestrationV2ProjectionEvent } from "./orchestrationV2Projection.ts";
38
import { THREAD_SNAPSHOT_IDLE_TTL_MS } from "./threadRetention.ts";
@@ -948,6 +948,18 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
948
// status. A replacement session or foreground resubscribe on the same scope
949
// may have missed events, so those show sync progress until confirmed.
950
const resumingLive = yield* Ref.make(initialState.status === "live");
951
+ // A cold open tries the socket snapshot once. Any later cold attempt, after a
952
+ // failure or a dropped session, asks HTTP, which also tells a missing thread
953
+ // apart from a failed load.
954
+ const socketOpenTried = yield* Ref.make(false);
955
+ 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>();
963
const markSynchronizing = Effect.gen(function* () {
964
if (yield* Ref.get(resumingLive)) return;
965
yield* SubscriptionRef.update(state, (current) =>
@@ -970,12 +982,21 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
982
return yield* Effect.never;
983
}
984
973
- yield* Effect.ignore(session.initialConfig);
985
+ const config = yield* Effect.option(session.initialConfig);
986
yield* Ref.set(awaitingCompletion, true);
987
yield* markSynchronizing;
988
yield* Ref.set(resumingLive, false);
989
978
- if (Option.isNone(current.data)) {
990
+ // A server that pages snapshots sends the same bounded window over the
991
+ // socket, so a cold open can skip the HTTP round trip.
992
+ const socketOnly =
993
+ Option.isNone(current.data) &&
994
+ canLoadHistory &&
995
+ Option.exists(config, (value) => value.threadSnapshotPagination === true) &&
996
+ !(yield* Ref.getAndSet(socketOpenTried, true));
997
+ yield* Ref.set(socketOnlyOpen, socketOnly);
998
+
999
+ if (Option.isNone(current.data) && !socketOnly) {
1000
const prepared = yield* SubscriptionRef.get(supervisor.prepared).pipe(
1001
Effect.flatMap(
1002
Option.match({
@@ -1053,10 +1074,21 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
1074
};
1075
}),
1076
{
1056
- onDefect: () => setStreamError("Could not synchronize the thread."),
1057
- onExpectedFailure: (cause) => setStreamError(formatThreadError(cause)),
1077
+ // A socket-only open that fails retries over HTTP, which tells a
1078
+ // missing thread apart from a failure, before showing an error.
1079
+ onDefect: Effect.fn("EnvironmentThreadState.onDefect")(function* () {
1080
+ if (yield* failedSocketOnlyOpen) {
1081
+ yield* Ref.set(socketOnlyOpen, false);
1082
+ return yield* Queue.offer(httpRetries, undefined);
1083
+ }
1084
+ yield* setStreamError("Could not synchronize the thread.");
1085
+ }),
1086
+ onExpectedFailure: Effect.fn("EnvironmentThreadState.onExpectedFailure")(function* (cause) {
1087
+ const retryOverHttp = !isAuthorizationFailure(cause) && (yield* failedSocketOnlyOpen);
1088
+ if (!retryOverHttp) yield* setStreamError(formatThreadError(cause));
1089
+ }),
1090
retryExpectedFailureAfter: "250 millis",
1059
- resubscribe: foregroundResubscriptions,
1091
+ resubscribe: Stream.merge(foregroundResubscriptions, Stream.fromQueue(httpRetries)),
1092
},
1093
).pipe(Stream.runForEachArray(applyItems)),
1094
);