Skip to content

Commit 823b64d

Browse files
committed
perf(client-runtime): resume the shell from its cursor after a reconnect
A shell loaded from a server snapshot now resumes from its sequence on a new session instead of downloading the full HTTP shell again. Cached data, or a snapshot taken with other omissions, still reloads in full.
1 parent 08a1729 commit 823b64d

2 files changed

Lines changed: 36 additions & 16 deletions

File tree

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -633,12 +633,12 @@ describe("environment shell synchronization", () => {
633633
it.effect("opts in to shell omissions and counts authoritative snapshots and removals", () =>
634634
Effect.gen(function* () {
635635
const events = yield* Queue.unbounded<OrchestrationV2ShellStreamItem>();
636-
const subscribeInput = yield* Deferred.make<unknown>();
636+
const subscribeInputs = yield* Queue.unbounded<unknown>();
637637
const loadOptions = yield* Ref.make<ReadonlyArray<unknown>>([]);
638638
const client = {
639639
[ORCHESTRATION_V2_WS_METHODS.subscribeShell]: (input: unknown) =>
640640
Stream.unwrap(
641-
Deferred.succeed(subscribeInput, input).pipe(Effect.as(Stream.fromQueue(events))),
641+
Queue.offer(subscribeInputs, input).pipe(Effect.as(Stream.fromQueue(events))),
642642
),
643643
} as unknown as WsRpcProtocolClient;
644644
const familySession: RpcSession.RpcSession = {
@@ -649,10 +649,11 @@ describe("environment shell synchronization", () => {
649649
shellOlderSettled: true,
650650
} as never),
651651
};
652+
const activeSession = yield* SubscriptionRef.make(Option.some(familySession));
652653
const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
653654
target: TARGET,
654655
state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE),
655-
session: yield* SubscriptionRef.make(Option.some(familySession)),
656+
session: activeSession,
656657
prepared: yield* SubscriptionRef.make(Option.some(PREPARED)),
657658
connect: Effect.void,
658659
disconnect: Effect.void,
@@ -687,7 +688,7 @@ describe("environment shell synchronization", () => {
687688
SubscriptionRef.changes(shellState).pipe(Stream.filter(predicate), Stream.runHead);
688689

689690
// Both transports must omit, or omitted children would come back through the socket.
690-
expect(yield* Deferred.await(subscribeInput)).toMatchObject({
691+
expect(yield* Queue.take(subscribeInputs)).toMatchObject({
691692
afterSequence: 1,
692693
omitIdleSubagents: true,
693694
omitOlderSettled: true,
@@ -723,6 +724,15 @@ describe("environment shell synchronization", () => {
723724
yield* stateWhere((state) => state.generation > loaded.generation),
724725
);
725726
expect(reset.removedAt.size).toBe(0);
727+
728+
// A reconnect resumes the server-loaded shell from its cursor, with no second HTTP load.
729+
yield* SubscriptionRef.set(activeSession, Option.some({ ...familySession }));
730+
expect(yield* Queue.take(subscribeInputs)).toMatchObject({
731+
afterSequence: 5,
732+
omitIdleSubagents: true,
733+
omitOlderSettled: true,
734+
});
735+
expect(yield* Ref.get(loadOptions)).toHaveLength(1);
726736
}),
727737
);
728738

@@ -892,7 +902,14 @@ describe("environment shell synchronization", () => {
892902
);
893903
yield* Deferred.await(firstFillStarted);
894904

895-
yield* SubscriptionRef.set(activeSession, Option.some(session(client)));
905+
// New omissions make the next session reload instead of resuming.
906+
yield* SubscriptionRef.set(
907+
activeSession,
908+
Option.some({
909+
...session(client),
910+
initialConfig: Effect.succeed({ shellThreadFamilies: true } as never),
911+
}),
912+
);
896913
yield* SubscriptionRef.changes(shellState).pipe(
897914
Stream.filter(
898915
(state) => Option.isSome(state.snapshot) && state.snapshot.value.snapshotSequence === 2,

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import { safeErrorLogAttributes } from "../errors/safeLog.ts";
2727
import * as Persistence from "../platform/persistence.ts";
2828
import { runCachePersistence } from "./cachePersistence.ts";
2929
import { subscribeDynamic } from "../rpc/client.ts";
30-
import type { RpcSession } from "../rpc/session.ts";
3130
import * as ShellSnapshotLoader from "./shellSnapshotHttp.ts";
3231
import type { DeferredShellSnapshot } from "./shellPullRequests.ts";
3332
import {
@@ -105,8 +104,10 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
105104
status: shellStatusForSnapshot(cachedSnapshot),
106105
});
107106
const awaitingCompletion = yield* Ref.make(false);
108-
const lastAuthoritativeSession = yield* Ref.make<RpcSession | null>(null);
109-
const activeSubscriptionSession = yield* Ref.make<RpcSession | null>(null);
107+
// The omission options of the subscription whose snapshot the in-memory
108+
// shell last came from; null while it holds only cached data.
109+
const authoritativeOmission = yield* Ref.make<string | null>(null);
110+
const activeSubscriptionOmission = yield* Ref.make<string | null>(null);
110111
const latestLiveSnapshot = yield* Ref.make<Option.Option<OrchestrationV2ShellSnapshot>>(
111112
Option.none(),
112113
);
@@ -274,9 +275,9 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
274275
}
275276
yield* SubscriptionRef.set(state, next);
276277
if (receivedSnapshot) {
277-
const session = yield* Ref.get(activeSubscriptionSession);
278-
if (session !== null) {
279-
yield* Ref.set(lastAuthoritativeSession, session);
278+
const omission = yield* Ref.get(activeSubscriptionOmission);
279+
if (omission !== null) {
280+
yield* Ref.set(authoritativeOmission, omission);
280281
}
281282
}
282283
if (next.snapshot !== initial.snapshot && Option.isSome(next.snapshot)) {
@@ -362,7 +363,6 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
362363
subscribeDynamic(
363364
ORCHESTRATION_V2_WS_METHODS.subscribeShell,
364365
Effect.fn("EnvironmentShellState.makeSubscribeInput")(function* (session) {
365-
yield* Ref.set(activeSubscriptionSession, session);
366366
const { omitIdleSubagents, omitOlderSettled } = yield* session.initialConfig.pipe(
367367
Effect.map((config) => ({
368368
omitIdleSubagents: config.shellThreadFamilies === true,
@@ -371,13 +371,16 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
371371
})),
372372
Effect.orElseSucceed(() => ({ omitIdleSubagents: false, omitOlderSettled: false })),
373373
);
374+
const omissionKey = `${omitIdleSubagents}:${omitOlderSettled}`;
375+
yield* Ref.set(activeSubscriptionOmission, omissionKey);
374376
yield* Ref.set(awaitingCompletion, true);
375377
yield* setSynchronizing;
376378

377-
// Foreground resubscriptions on the same live session can resume from
378-
// the in-memory cursor. A new session reloads the authoritative HTTP
379-
// snapshot so a valid cursor cannot preserve incomplete cached data.
380-
const hasAuthoritativeSnapshot = (yield* Ref.get(lastAuthoritativeSession)) === session;
379+
// A shell that came from a server snapshot resumes from its in-memory
380+
// cursor, on this session or a new one. Cached data, or a snapshot taken
381+
// with other omissions, reloads the authoritative HTTP snapshot so a
382+
// valid cursor cannot preserve incomplete data.
383+
const hasAuthoritativeSnapshot = (yield* Ref.get(authoritativeOmission)) === omissionKey;
381384
let canResume = hasAuthoritativeSnapshot;
382385
let current = yield* SubscriptionRef.get(state);
383386
if (!hasAuthoritativeSnapshot || Option.isNone(current.snapshot)) {

0 commit comments

Comments
 (0)