Skip to content

Commit c44dad4

Browse files
committed
fix: resume after socket shell snapshot
1 parent c7aba01 commit c44dad4

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ describe("environment shell synchronization", () => {
165165
updatedAt: "2026-06-07T00:00:00.000Z",
166166
};
167167
const events = yield* Queue.unbounded<OrchestrationShellStreamItem>();
168+
const wakeups = yield* Queue.unbounded<ConnectionWakeups.ConnectionWakeup>();
168169
const subscribeInputs = yield* Queue.unbounded<{
169170
readonly afterSequence?: number;
170171
readonly requestCompletionMarker?: boolean;
@@ -213,6 +214,10 @@ describe("environment shell synchronization", () => {
213214
Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
214215
Effect.provideService(Persistence.EnvironmentCacheStore, cache),
215216
Effect.provideService(ShellSnapshotLoader, snapshotLoader),
217+
Effect.provideService(
218+
ConnectionWakeups.ConnectionWakeups,
219+
ConnectionWakeups.ConnectionWakeups.of({ changes: Stream.fromQueue(wakeups) }),
220+
),
216221
);
217222

218223
const subscribeInput = yield* Queue.take(subscribeInputs);
@@ -233,6 +238,12 @@ describe("environment shell synchronization", () => {
233238
const live = yield* SubscriptionRef.get(shellState);
234239
expect(Option.getOrThrow(live.snapshot)).toEqual(resetSnapshot);
235240
expect(yield* Ref.get(loaderCalls)).toBe(1);
241+
242+
yield* Queue.offer(wakeups, "application-active");
243+
const resumedInput = yield* Queue.take(subscribeInputs);
244+
expect(resumedInput.afterSequence).toBe(resetSnapshot.snapshotSequence);
245+
expect(resumedInput.requestCompletionMarker).toBe(true);
246+
expect(yield* Ref.get(loaderCalls)).toBe(1);
236247
}),
237248
);
238249

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
7373
});
7474
const awaitingCompletion = yield* Ref.make(false);
7575
const lastAuthoritativeSession = yield* Ref.make<RpcSession | null>(null);
76+
const activeSubscriptionSession = yield* Ref.make<RpcSession | null>(null);
7677
const persistence = yield* Queue.sliding<OrchestrationShellSnapshot>(1);
7778

7879
const persist = Effect.fn("EnvironmentShellState.persist")(function* (
@@ -168,6 +169,12 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
168169
status: waiting ? "synchronizing" : "live",
169170
error: Option.none(),
170171
});
172+
if (item.kind === "snapshot") {
173+
const session = yield* Ref.get(activeSubscriptionSession);
174+
if (session !== null) {
175+
yield* Ref.set(lastAuthoritativeSession, session);
176+
}
177+
}
171178
yield* Queue.offer(persistence, nextSnapshot);
172179
});
173180

@@ -182,6 +189,7 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
182189
subscribeDynamic(
183190
ORCHESTRATION_WS_METHODS.subscribeShell,
184191
Effect.fn("EnvironmentShellState.makeSubscribeInput")(function* (session) {
192+
yield* Ref.set(activeSubscriptionSession, session);
185193
const supportsCompletionMarker = yield* session.initialConfig.pipe(
186194
Effect.map((config) => config.shellResumeCompletionMarker === true),
187195
Effect.orElseSucceed(() => false),
@@ -213,7 +221,6 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
213221
const httpSnapshot = yield* snapshotLoader.load(prepared);
214222
if (Option.isSome(httpSnapshot)) {
215223
yield* applyItem({ kind: "snapshot", snapshot: httpSnapshot.value });
216-
yield* Ref.set(lastAuthoritativeSession, session);
217224
canResume = true;
218225
current = yield* SubscriptionRef.get(state);
219226
}

0 commit comments

Comments
 (0)