Skip to content

Commit 1915569

Browse files
saphidcodex
andauthored
perf(client-runtime): merge shell deltas once per received batch (#18416)
Co-authored-by: SAPHID <[email protected]> Co-authored-by: GPT-6.1 Sol <[email protected]>
1 parent 88483a5 commit 1915569

4 files changed

Lines changed: 409 additions & 35 deletions

File tree

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

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,190 @@ const makePersistenceHarness = Effect.fn("test.makeShellPersistenceHarness")(fun
123123
});
124124

125125
describe("environment shell synchronization", () => {
126+
it.effect.each([
127+
{ bufferSize: Infinity, expectedSequences: [51], existingRows: false },
128+
{ bufferSize: Infinity, expectedSequences: [51], existingRows: true },
129+
// RpcClient defaults to a 16-event buffer, which splits larger server chunks.
130+
{ bufferSize: 16, expectedSequences: [17, 33, 49, 51], existingRows: false },
131+
{ bufferSize: 16, expectedSequences: [17, 33, 49, 51], existingRows: true },
132+
])("batches $bufferSize events (existing: $existingRows)", (input) =>
133+
Effect.gen(function* () {
134+
const { bufferSize, expectedSequences, existingRows } = input;
135+
const events = yield* Queue.bounded<OrchestrationV2ShellStreamItem>(bufferSize);
136+
let rowVisits = 0;
137+
const rows = Array.from({ length: 50 }, (_, index) => ({
138+
...v2ThreadShell,
139+
get id() {
140+
rowVisits += 1;
141+
return ThreadId.make(`thread-${index}`);
142+
},
143+
}));
144+
const batchSnapshot = { ...LIVE_SHELL_SNAPSHOT, threads: existingRows ? rows : [] };
145+
const client = {
146+
[ORCHESTRATION_V2_WS_METHODS.subscribeShell]: () => Stream.fromQueue(events),
147+
} as unknown as WsRpcProtocolClient;
148+
const supervisorState = yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE);
149+
const activeSession = yield* SubscriptionRef.make<Option.Option<RpcSession.RpcSession>>(
150+
Option.some(session(client)),
151+
);
152+
const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
153+
target: TARGET,
154+
state: supervisorState,
155+
session: activeSession,
156+
prepared: yield* SubscriptionRef.make(Option.some(PREPARED)),
157+
connect: Effect.void,
158+
disconnect: Effect.void,
159+
retryNow: Effect.void,
160+
} satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
161+
const cache = Persistence.EnvironmentCacheStore.of({
162+
loadShell: () => Effect.succeedNone,
163+
saveShell: () => Effect.void,
164+
loadThread: () => Effect.succeedNone,
165+
saveThread: () => Effect.void,
166+
removeThread: () => Effect.void,
167+
loadServerConfig: () => Effect.succeedNone,
168+
saveServerConfig: () => Effect.void,
169+
loadVcsRefs: () => Effect.succeedNone,
170+
saveVcsRefs: () => Effect.void,
171+
removeVcsRefs: () => Effect.void,
172+
clearVcsRefs: () => Effect.void,
173+
clear: () => Effect.void,
174+
});
175+
const shellState = yield* makeEnvironmentShellState().pipe(
176+
Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
177+
Effect.provideService(Persistence.EnvironmentCacheStore, cache),
178+
Effect.provideService(
179+
ShellSnapshotLoader.ShellSnapshotLoader,
180+
ShellSnapshotLoader.ShellSnapshotLoader.of({
181+
load: () => Effect.succeedNone,
182+
}),
183+
),
184+
);
185+
yield* SubscriptionRef.set(supervisorState, {
186+
desired: true,
187+
network: "online",
188+
phase: "connected",
189+
stage: null,
190+
attempt: 1,
191+
generation: 1,
192+
lastFailure: null,
193+
retryAt: null,
194+
});
195+
yield* Queue.offer(events, { kind: "snapshot", snapshot: batchSnapshot });
196+
yield* Queue.offer(events, { kind: "synchronized" });
197+
yield* SubscriptionRef.changes(shellState).pipe(
198+
Stream.filter((state) => state.status === "live"),
199+
Stream.runHead,
200+
);
201+
202+
// Observe before publishing so no batch can arrive before the subscription.
203+
const observed = yield* SubscriptionRef.changes(shellState).pipe(
204+
Stream.drop(1),
205+
Stream.takeUntil(
206+
(state) => Option.isSome(state.snapshot) && state.snapshot.value.snapshotSequence === 51,
207+
),
208+
Stream.runCollect,
209+
Effect.forkScoped({ startImmediately: true }),
210+
);
211+
rowVisits = 0;
212+
yield* Queue.offerAll(
213+
events,
214+
Array.from({ length: 50 }, (_, index) => ({
215+
kind: "thread.updated" as const,
216+
sequence: 2 + index,
217+
location: "active" as const,
218+
thread: {
219+
...v2ThreadShell,
220+
get id() {
221+
rowVisits += 1;
222+
return ThreadId.make(`thread-${index}`);
223+
},
224+
title: "Updated",
225+
},
226+
})),
227+
);
228+
const states = yield* Fiber.join(observed);
229+
if (existingRows) {
230+
// Index each received batch once; distinct updates must not rescan every row.
231+
expect(rowVisits).toBeLessThanOrEqual(50 * (expectedSequences.length + 10));
232+
}
233+
const snapshots = states.map((state) => Option.getOrThrow(state.snapshot));
234+
expect(snapshots.map((snapshot) => snapshot.snapshotSequence)).toEqual(expectedSequences);
235+
expect(snapshots.at(-1)!.threads.map((thread) => thread.id)).toEqual(
236+
Array.from({ length: 50 }, (_, index) => `thread-${index}`),
237+
);
238+
239+
const removedObserved = yield* SubscriptionRef.changes(shellState).pipe(
240+
Stream.filter(
241+
(state) => Option.isSome(state.snapshot) && state.snapshot.value.snapshotSequence === 52,
242+
),
243+
Stream.runHead,
244+
Effect.forkScoped({ startImmediately: true }),
245+
);
246+
yield* Queue.offerAll(events, [
247+
{ kind: "future.event", sequence: 99 } as never,
248+
{
249+
kind: "thread.removed",
250+
sequence: 52,
251+
location: "active",
252+
threadId: ThreadId.make("thread-0"),
253+
},
254+
{
255+
kind: "thread.removed",
256+
sequence: 51,
257+
location: "active",
258+
threadId: ThreadId.make("thread-1"),
259+
},
260+
]);
261+
const removedState = Option.getOrThrow(yield* Fiber.join(removedObserved));
262+
expect(removedState.generation).toBe(states.at(-1)!.generation);
263+
expect([...removedState.removedAt]).toEqual([[ThreadId.make("thread-0"), 52]]);
264+
265+
// An authoritative snapshot resets even a higher live cursor in the same batch.
266+
const resetRow = { ...v2ThreadShell, id: ThreadId.make("reset-row") };
267+
const resetObserved = yield* SubscriptionRef.changes(shellState).pipe(
268+
Stream.filter(
269+
(state) => Option.isSome(state.snapshot) && state.snapshot.value.snapshotSequence === 4,
270+
),
271+
Stream.runHead,
272+
Effect.forkScoped({ startImmediately: true }),
273+
);
274+
yield* Queue.offerAll(events, [
275+
{
276+
kind: "thread.removed",
277+
sequence: 52,
278+
location: "active",
279+
threadId: ThreadId.make("thread-0"),
280+
},
281+
{
282+
kind: "snapshot",
283+
snapshot: { ...LIVE_SHELL_SNAPSHOT, snapshotSequence: 2, threads: [resetRow] },
284+
},
285+
{ kind: "thread.removed", sequence: 2, location: "active", threadId: resetRow.id },
286+
{
287+
kind: "thread.updated",
288+
sequence: 3,
289+
location: "active",
290+
thread: { ...resetRow, title: "After reset" },
291+
},
292+
{ kind: "synchronized" },
293+
{
294+
kind: "thread.updated",
295+
sequence: 4,
296+
location: "active",
297+
thread: { ...resetRow, title: "After completion" },
298+
},
299+
]);
300+
const resetState = Option.getOrThrow(yield* Fiber.join(resetObserved));
301+
expect(resetState.status).toBe("live");
302+
expect(resetState.generation).toBeGreaterThan(removedState.generation);
303+
expect(resetState.removedAt.size).toBe(0);
304+
expect(Option.getOrThrow(resetState.snapshot).threads).toEqual([
305+
{ ...resetRow, title: "After completion" },
306+
]);
307+
}).pipe(Effect.scoped),
308+
);
309+
126310
it.effect("skips unchanged shell writes, persists resets and flushes changed rows", () =>
127311
Effect.gen(function* () {
128312
const saved = yield* SubscriptionRef.make<ReadonlyArray<OrchestrationV2ShellSnapshot>>([]);

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

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ import type { RpcSession } from "../rpc/session.ts";
3131
import * as ShellSnapshotLoader from "./shellSnapshotHttp.ts";
3232
import type { DeferredShellSnapshot } from "./shellPullRequests.ts";
3333
import {
34-
applyShellStreamEvent,
34+
applyShellStreamEvents,
35+
type ShellStreamEvent,
3536
mergeShellSnapshotProjects,
3637
reuseUnchangedThreadShells,
3738
sameThreadPullRequests,
@@ -211,57 +212,71 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
211212
) {
212213
const initial = yield* SubscriptionRef.get(state);
213214
let waiting = yield* Ref.get(awaitingCompletion);
214-
/* oxlint-disable oxc/no-accumulating-spread -- This copies a fixed-shape state record, not a growing collection. */
215215
let next = initial;
216216
let receivedSnapshot = false;
217+
let deltas: Array<ShellStreamEvent> = [];
218+
const flushDeltas = () => {
219+
if (deltas.length > 0 && Option.isSome(next.snapshot)) {
220+
let sequence = next.snapshot.value.snapshotSequence;
221+
let removedAt: Map<ThreadId, number> | undefined;
222+
for (const delta of deltas) {
223+
if (delta.sequence <= sequence) continue;
224+
switch (delta.kind) {
225+
case "thread.removed":
226+
removedAt ??= new Map(next.removedAt);
227+
removedAt.set(delta.threadId, delta.sequence);
228+
break;
229+
case "thread.updated":
230+
case "project.updated":
231+
case "project.removed":
232+
break;
233+
default:
234+
continue;
235+
}
236+
sequence = delta.sequence;
237+
}
238+
next = {
239+
...next,
240+
removedAt: removedAt ?? next.removedAt,
241+
snapshot: Option.some(applyShellStreamEvents(next.snapshot.value, deltas)),
242+
status: waiting ? "synchronizing" : "live",
243+
error: Option.none(),
244+
};
245+
}
246+
deltas = [];
247+
};
217248
for (const item of items) {
249+
if (item.kind !== "snapshot" && item.kind !== "synchronized") {
250+
deltas.push(item);
251+
continue;
252+
}
253+
// Snapshots can reset the cursor; completion markers change the sync status.
254+
flushDeltas();
218255
if (item.kind === "synchronized") {
219256
waiting = false;
220257
if (Option.isSome(next.snapshot)) {
221258
next = { ...next, status: "live", error: Option.none() };
222259
}
223260
continue;
224261
}
225-
const authoritative =
226-
item.kind === "snapshot" && item.resolvedRepositoryIdentityRoots === undefined;
227-
const nextSnapshot =
228-
item.kind === "snapshot"
229-
? mergeShellSnapshotProjects(
230-
Option.getOrNull(next.snapshot),
231-
item.snapshot,
232-
item.resolvedRepositoryIdentityRoots === undefined
233-
? undefined
234-
: {
235-
resolvedRepositoryIdentityRoots: item.resolvedRepositoryIdentityRoots,
236-
},
237-
)
238-
: Option.match(next.snapshot, {
239-
onNone: () => null,
240-
onSome: (snapshot) =>
241-
item.sequence > snapshot.snapshotSequence
242-
? applyShellStreamEvent(snapshot, item)
243-
: snapshot,
244-
});
245-
if (nextSnapshot === null) continue;
246-
receivedSnapshot ||= item.kind === "snapshot";
247-
const removal =
248-
item.kind === "thread.removed" &&
249-
Option.exists(next.snapshot, (snapshot) => item.sequence > snapshot.snapshotSequence)
250-
? item
251-
: null;
262+
const authoritative = item.resolvedRepositoryIdentityRoots === undefined;
263+
const nextSnapshot = mergeShellSnapshotProjects(
264+
Option.getOrNull(next.snapshot),
265+
item.snapshot,
266+
item.resolvedRepositoryIdentityRoots === undefined
267+
? undefined
268+
: { resolvedRepositoryIdentityRoots: item.resolvedRepositoryIdentityRoots },
269+
);
270+
receivedSnapshot = true;
252271
next = {
253272
snapshot: Option.some(nextSnapshot),
254273
status: waiting ? "synchronizing" : "live",
255274
error: Option.none(),
256275
generation: authoritative ? ++lastShellGeneration : next.generation,
257-
removedAt: authoritative
258-
? EMPTY_REMOVALS
259-
: removal === null
260-
? next.removedAt
261-
: new Map(next.removedAt).set(removal.threadId, removal.sequence),
276+
removedAt: authoritative ? EMPTY_REMOVALS : next.removedAt,
262277
};
263278
}
264-
/* oxlint-enable oxc/no-accumulating-spread */
279+
flushDeltas();
265280
yield* Ref.set(awaitingCompletion, waiting);
266281
if (next === initial) return;
267282
if (pendingLinkIds !== undefined && Option.isSome(next.snapshot)) {

0 commit comments

Comments
 (0)