@devries-cameron20 / t3code Mirror · read-only / commits / 2917f28465
Fork 0

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, 03:15 UTC

perf(mobile): faster thread sync, connects, and reconnects on bad wifi (#28487)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <51714798+juliusmarminge@users.noreply.github.com>

Bilal Bakr committed Nov 6, 2026 at 03:29 UTC 2917f28465588b5f6afc43191f7beac953e109c0
14 files changed +493 -108
apps/mobile/src/connection/runtime.ts
+5 -1
@@ -36,7 +36,11 @@ type ConnectionLayerSource =
36
37 const layerProvidedClientConnection = layerSnapshotLoader.pipe(
38 Layer.provideMerge(
39 - Connection.layerWithOptions({ usageLimitSources: true, usageLimitsCommand: true }),
39 + Connection.layerWithOptions({
40 + usageLimitSources: true,
41 + usageLimitsCommand: true,
42 + providerCatalogsOnDemand: true,
43 + }),
44 ),
45 Layer.provideMerge(
46 Layer.mergeAll(
apps/mobile/src/state/server.ts
+1
@@ -13,6 +13,7 @@ export const serverEnvironment = createServerEnvironmentAtoms(connectionAtomRunt
13 initialConfigValueAtom: environmentSession.initialConfigValueAtom,
14 usageLimitSources: true,
15 usageLimitsCommand: true,
16 + providerCatalogsOnDemand: true,
17 });
18 export const describeOutdatedServer = createOutdatedServerDescriptionCommand(connectionAtomRuntime);
19 export const updateOutdatedServer = createOutdatedServerUpdateCommand(connectionAtomRuntime);
apps/server/src/terminal/OutputProtocol.test.ts
+7 -3
@@ -1,5 +1,6 @@
1 import { assert, describe, it } from "@effect/vitest";
2 import {
3 + ORCHESTRATION_V2_WS_METHODS,
4 type TerminalAttachStreamEvent,
5 TerminalAttachStreamEvent as TerminalAttachStreamEventSchema,
6 WS_METHODS,
@@ -13,7 +14,7 @@ import * as Stream from "effect/Stream";
14 import * as TestClock from "effect/testing/TestClock";
15 import { Rpc, RpcGroup, RpcMessage, RpcSerialization, RpcServer } from "effect/rpc";
16
16 -import { terminalOutputStream, withTerminalOutputWindow } from "./OutputProtocol.ts";
17 +import { terminalOutputStream, withOutputWindow } from "./OutputProtocol.ts";
18
19 describe("terminal output window", () => {
20 it.effect.each([
@@ -21,6 +22,9 @@ describe("terminal output window", () => {
22 { tag: WS_METHODS.terminalObserve, size: 1, limit: 8 },
23 { tag: WS_METHODS.terminalAttach, size: 64 * 1024, limit: 1 },
24 { tag: WS_METHODS.terminalObserve, size: 64 * 1024, limit: 1 },
25 + { tag: WS_METHODS.subscribeServerConfig, size: 1, limit: 8 },
26 + { tag: ORCHESTRATION_V2_WS_METHODS.subscribeShell, size: 1, limit: 8 },
27 + { tag: ORCHESTRATION_V2_WS_METHODS.subscribeThread, size: 64 * 1024, limit: 8 },
28 { tag: WS_METHODS.subscribeTerminalMetadata, size: 1, limit: 1 },
29 ])("limits $tag with $size-byte values to $limit pending chunks", ({ tag, size, limit }) =>
30 Effect.gen(function* () {
@@ -48,7 +52,7 @@ describe("terminal output window", () => {
52 );
53 yield* RpcServer.make(group).pipe(
54 Effect.provide(group.toLayerHandler(tag, () => Stream.fromQueue(output))),
51 - Effect.provideService(RpcServer.Protocol, withTerminalOutputWindow(protocol)),
55 + Effect.provideService(RpcServer.Protocol, withOutputWindow(protocol)),
56 Effect.forkScoped,
57 );
58 const write = yield* Deferred.await(receive);
@@ -191,7 +195,7 @@ describe("terminal output stream", () => {
195 ),
196 ),
197 ),
194 - Effect.provideService(RpcServer.Protocol, withTerminalOutputWindow(protocol)),
198 + Effect.provideService(RpcServer.Protocol, withOutputWindow(protocol)),
199 Effect.forkScoped,
200 );
201 const write = yield* Deferred.await(receive);
apps/server/src/terminal/OutputProtocol.ts
+57 -20
@@ -1,4 +1,9 @@
1 -import { type TerminalAttachStreamEvent, type TerminalEvent, WS_METHODS } from "@t3tools/contracts";
1 +import {
2 + ORCHESTRATION_V2_WS_METHODS,
3 + type TerminalAttachStreamEvent,
4 + type TerminalEvent,
5 + WS_METHODS,
6 +} from "@t3tools/contracts";
7 import type * as Arr from "effect/Array";
8 import * as Cause from "effect/Cause";
9 import * as Effect from "effect/Effect";
@@ -11,31 +16,62 @@ const MAX_PENDING_BYTES = 64 * 1024;
16 const isFull = (sizes: number[]) =>
17 sizes.length >= MAX_PENDING_CHUNKS ||
18 sizes.reduce((total, size) => total + size, 0) >= MAX_PENDING_BYTES;
19 +// Terminal output is small chunks, so its window also caps bytes. The other streams
20 +// window by count only: sizing a multi-MB thread snapshot would serialize it twice.
21 +const BYTE_WINDOWED_STREAMS: ReadonlySet<string> = new Set([
22 + WS_METHODS.terminalAttach,
23 + WS_METHODS.terminalObserve,
24 +]);
25 +// One ACK round trip per chunk stalls sync on high-latency links.
26 +const COUNT_WINDOWED_STREAMS: ReadonlySet<string> = new Set([
27 + WS_METHODS.subscribeServerConfig,
28 + ORCHESTRATION_V2_WS_METHODS.subscribeShell,
29 + ORCHESTRATION_V2_WS_METHODS.subscribeThread,
30 +]);
31
15 -export function withTerminalOutputWindow(
32 +interface Window {
33 + readonly bytes: boolean;
34 + readonly sizes: number[];
35 +}
36 +
37 +export function withOutputWindow(
38 protocol: RpcServer.Protocol["Service"],
39 ): RpcServer.Protocol["Service"] {
18 - const windows = new Map<string, number[]>();
40 + // RpcServer drops a disconnected client before its streams exit, so their Exit never
41 + // reaches send; departed clients are pruned when a window opens instead.
42 + const clients = new Map<number, Map<string, Window>>();
43 let receive: Parameters<RpcServer.Protocol["Service"]["run"]>[0];
44 + const windowFor = (clientId: number, requestId: string) => clients.get(clientId)?.get(requestId);
45 + const openWindow = Effect.fnUntraced(function* (
46 + clientId: number,
47 + requestId: string,
48 + bytes: boolean,
49 + ) {
50 + const connected = yield* protocol.clientIds;
51 + for (const id of clients.keys()) if (!connected.has(id)) clients.delete(id);
52 + let windows = clients.get(clientId);
53 + if (!windows) clients.set(clientId, (windows = new Map()));
54 + if (!windows.has(requestId)) windows.set(requestId, { bytes, sizes: [] });
55 + });
56 return {
57 ...protocol,
58 run: (write) => {
59 receive = write;
60 return protocol.run((clientId, message) =>
61 Effect.suspend(() => {
26 - if (
27 - message._tag === "Request" &&
28 - (message.tag === WS_METHODS.terminalAttach ||
29 - message.tag === WS_METHODS.terminalObserve)
30 - ) {
31 - const key = `${clientId}:${message.id}`;
32 - if (!windows.has(key)) windows.set(key, []);
62 + if (message._tag === "Request") {
63 + const bytes = BYTE_WINDOWED_STREAMS.has(message.tag);
64 + if (bytes || COUNT_WINDOWED_STREAMS.has(message.tag)) {
65 + return openWindow(clientId, String(message.id), bytes).pipe(
66 + Effect.andThen(write(clientId, message)),
67 + );
68 + }
69 } else if (message._tag === "Ack") {
34 - const window = windows.get(`${clientId}:${message.requestId}`);
70 + const window = windowFor(clientId, String(message.requestId));
71 if (window) {
36 - const wasFull = isFull(window);
37 - window.shift();
38 - if (!wasFull || isFull(window)) return Effect.void;
72 + const wasFull = isFull(window.sizes);
73 + window.sizes.shift();
74 + if (!wasFull || isFull(window.sizes)) return Effect.void;
75 }
76 }
77 return write(clientId, message);
@@ -46,14 +82,15 @@ export function withTerminalOutputWindow(
82 Effect.suspend(() => {
83 const send = protocol.send(clientId, response, transferables);
84 if (response._tag === "Exit") {
49 - windows.delete(`${clientId}:${response.requestId}`);
85 + clients.get(clientId)?.delete(String(response.requestId));
86 } else if (response._tag === "Chunk") {
51 - const window = windows.get(`${clientId}:${response.requestId}`);
87 + const window = windowFor(clientId, String(response.requestId));
88 if (window) {
53 - // @effect-diagnostics-next-line globalJsonInEffect:off
54 - const size = Buffer.byteLength(JSON.stringify(response));
55 - window.push(size);
56 - if (!isFull(window)) {
89 + window.sizes.push(
90 + // @effect-diagnostics-next-line globalJsonInEffect:off
91 + window.bytes ? Buffer.byteLength(JSON.stringify(response)) : 0,
92 + );
93 + if (!isFull(window.sizes)) {
94 return send.pipe(
95 Effect.andThen(() =>
96 receive(clientId, {
apps/server/src/ws.test.ts
+79 -4
@@ -116,6 +116,7 @@ import * as WorkspacePaths from "./workspace/WorkspacePaths.ts";
116 import * as ServerSecretStore from "./auth/ServerSecretStore.ts";
117 import * as ServerConfigModule from "./config.ts";
118 import * as Keybindings from "./keybindings.ts";
119 +import { USAGE_LIMITS_COMMAND } from "@t3tools/shared/usageLimits";
120 import * as ServerSettings from "./serverSettings.ts";
121 import * as ManagedPolicy from "./policy/ManagedPolicy.ts";
122
@@ -1024,13 +1025,21 @@ it.effect("on-demand clients get provider config without workspace snapshots", (
1025 auth: { status: "authenticated" },
1026 checkedAt: "2026-10-25T00:00:00.000Z",
1027 models: [],
1027 - slashCommands: [{ name: "machine-command" }],
1028 - skills: [],
1028 + slashCommands: [{ name: "machine-command", description: "A machine command" }],
1029 + skills: [
1030 + {
1031 + name: "machine-skill",
1032 + description: "Machine skill",
1033 + path: "/skills/machine-skill",
1034 + enabled: true,
1035 + },
1036 + ],
1037 workspaceSnapshots: [
1038 snapshotFor("/a", "2026-10-25T00:00:00.000Z"),
1039 snapshotFor("/b", "2026-10-25T00:00:00.000Z"),
1040 ],
1041 };
1042 + let scansCwd = true;
1043 const providerChanges = yield* Queue.unbounded<ReadonlyArray<ServerProvider>>();
1044 const pulled = yield* Queue.unbounded<void>();
1045 const responses = yield* Queue.unbounded<unknown>();
@@ -1047,6 +1056,20 @@ it.effect("on-demand clients get provider config without workspace snapshots", (
1056 ),
1057 refreshWorkspaceSnapshot: () => Effect.succeed([provider]),
1058 }),
1059 + Layer.mock(ProviderInstanceRegistry.ProviderInstanceRegistry)({
1060 + listInstances: Effect.sync(() => [
1061 + (scansCwd
1062 + ? { instanceId, snapshotForCwd: () => Effect.die("unused") }
1063 + : { instanceId }) as never,
1064 + ]),
1065 + getInstance: () =>
1066 + Effect.sync(
1067 + () =>
1068 + (scansCwd
1069 + ? { instanceId, snapshotForCwd: () => Effect.die("unused") }
1070 + : { instanceId }) as never,
1071 + ),
1072 + }),
1073 Layer.mock(UsageLimitSources.UsageLimitSources)({
1074 current: Effect.succeed([]),
1075 streamChanges: Stream.make([]),
@@ -1116,12 +1139,17 @@ it.effect("on-demand clients get provider config without workspace snapshots", (
1139 _tag: "Request",
1140 id: configRequest,
1141 tag: WS_METHODS.subscribeServerConfig,
1119 - payload: { workspaceSnapshotsOnDemand: true },
1142 + payload: { workspaceSnapshotsOnDemand: true, providerCatalogsOnDemand: true },
1143 headers: Headers.empty,
1144 });
1145 const snapshot = (yield* Queue.take(responses)) as ServerConfigStreamEvent;
1146 if (snapshot.type !== "snapshot") return yield* Effect.die("Missing initial snapshot");
1147 assert.isUndefined(snapshot.config.providers[0]?.workspaceSnapshots);
1148 + // The cwd snapshot the client fetches carries the catalog, so only command names are sent.
1149 + assert.deepEqual(snapshot.config.providers[0]?.slashCommands, [{ name: "machine-command" }]);
1150 + assert.deepEqual(snapshot.config.providers[0]?.skills, [
1151 + { name: "machine-skill", path: "/skills/machine-skill", enabled: true },
1152 + ]);
1153 yield* server.write(0, { _tag: "Ack", requestId: configRequest });
1154
1155 // A rescan that only touches a snapshot sends nothing; the next real change does.
@@ -1130,13 +1158,34 @@ it.effect("on-demand clients get provider config without workspace snapshots", (
1158 ]);
1159 yield* Queue.take(pulled);
1160 yield* TestClock.adjust("1 second");
1133 - yield* Queue.offer(providerChanges, [{ ...provider, status: "warning" }]);
1161 + // Late /usage-limits discovery still reaches the client, so it refetches its cwd snapshot.
1162 + yield* Queue.offer(providerChanges, [
1163 + {
1164 + ...provider,
1165 + status: "warning",
1166 + slashCommands: [...provider.slashCommands, USAGE_LIMITS_COMMAND],
1167 + },
1168 + ]);
1169 yield* Queue.take(pulled);
1170 yield* TestClock.adjust("1 second");
1171 const update = (yield* Queue.take(responses)) as ServerConfigStreamEvent;
1172 if (update.type !== "providerStatuses") return yield* Effect.die("Expected provider update");
1173 assert.equal(update.payload.providers[0]?.status, "warning");
1174 assert.isUndefined(update.payload.providers[0]?.workspaceSnapshots);
1175 + assert.deepEqual(update.payload.providers[0]?.slashCommands, [
1176 + { name: "machine-command" },
1177 + { name: USAGE_LIMITS_COMMAND.name },
1178 + ]);
1179 + yield* server.write(0, { _tag: "Ack", requestId: configRequest });
1180 + // A replacement that cannot scan a cwd sends its full machine catalog.
1181 + scansCwd = false;
1182 + yield* Queue.offer(providerChanges, [{ ...provider, status: "warning" }]);
1183 + yield* Queue.take(pulled);
1184 + yield* TestClock.adjust("1 second");
1185 + const replaced = (yield* Queue.take(responses)) as ServerConfigStreamEvent;
1186 + if (replaced.type !== "providerStatuses") return yield* Effect.die("Expected provider update");
1187 + assert.deepEqual(replaced.payload.providers[0]?.slashCommands, provider.slashCommands);
1188 + assert.deepEqual(replaced.payload.providers[0]?.skills, provider.skills);
1189
1190 yield* server.write(0, {
1191 _tag: "Request",
@@ -1152,6 +1201,32 @@ it.effect("on-demand clients get provider config without workspace snapshots", (
1201 refreshed.providers.map((entry) => [entry.workspaceSnapshots, entry.slashCommands]),
1202 [[[snapshotFor("/b", "2026-10-25T00:00:00.000Z")], []]],
1203 );
1204 + // A failed scan still gives a client without the machine catalog its commands.
1205 + scansCwd = true;
1206 + yield* server.write(0, {
1207 + _tag: "Request",
1208 + id: RpcMessage.RequestId("refresh-failed-scan"),
1209 + tag: WS_METHODS.serverRefreshProviders,
1210 + payload: {
1211 + instanceId,
1212 + cwd: "/c",
1213 + workspaceSnapshotsOnDemand: true,
1214 + providerCatalogsOnDemand: true,
1215 + },
1216 + headers: Headers.empty,
1217 + });
1218 + const fallback = (yield* Queue.take(responses)) as {
1219 + readonly providers: ReadonlyArray<ServerProvider>;
1220 + };
1221 + assert.deepEqual(fallback.providers[0]?.workspaceSnapshots, [
1222 + {
1223 + cwd: "/c",
1224 + checkedAt: provider.checkedAt,
1225 + slashCommands: provider.slashCommands,
1226 + slashCommandsPending: true,
1227 + skills: provider.skills,
1228 + },
1229 + ]);
1230 yield* server.write(0, { _tag: "Interrupt", requestId: configRequest, interruptors: [] });
1231 }).pipe(
1232 Effect.provide(
apps/server/src/ws.ts
+75 -18
@@ -188,7 +188,7 @@ import { invalidateSettingsSecrets } from "./sourceControl/sourceControlHostSecr
188 import * as DatabaseStorage from "./persistence/DatabaseStorage.ts";
189 import * as StorageCleanup from "./storageCleanup.ts";
190 import * as TerminalManager from "./terminal/Manager.ts";
191 -import { terminalOutputStream, withTerminalOutputWindow } from "./terminal/OutputProtocol.ts";
191 +import { terminalOutputStream, withOutputWindow } from "./terminal/OutputProtocol.ts";
192 import * as ServerBrowser from "./preview/ServerBrowser.ts";
193 import * as DeviceService from "./device/DeviceService.ts";
194 import { remoteSshDeviceHosts } from "./device/localSshDeviceHost.ts";
@@ -426,8 +426,44 @@ export const resolveOpenDiscoveryForConfig = <T, D, E1, R1, E2, R2, E3, R3, E4,
426 },
427 { concurrency: "unbounded" },
428 ).pipe(Effect.map(({ editorConfig, ...endpoints }) => ({ ...editorConfig, ...endpoints })));
429 -function withoutWorkspaceSnapshots(providers: ReadonlyArray<ServerProvider>): ServerProvider[] {
430 - return providers.map(({ workspaceSnapshots: _workspaceSnapshots, ...provider }) => provider);
429 +// A failed scan leaves no cwd snapshot, so a client without the machine catalog gets it
430 +// as a pending snapshot, which it shows while it retries the scan.
431 +function workspaceSnapshotOrCatalog(
432 + provider: ServerProvider,
433 + cwd: string,
434 + catalogOnDemand: boolean,
435 +) {
436 + const snapshots = (provider.workspaceSnapshots ?? []).filter((snapshot) => snapshot.cwd === cwd);
437 + if (snapshots.length > 0 || !catalogOnDemand) return snapshots;
438 + return [
439 + {
440 + cwd,
441 + checkedAt: provider.checkedAt,
442 + slashCommands: provider.slashCommands,
443 + slashCommandsPending: true,
444 + skills: provider.skills,
445 + },
446 + ];
447 +}
448 +
449 +// On-demand catalog clients read full entries from the cwd snapshot they fetch. Names still
450 +// change the config when the machine catalog does, so the client refetches, and skill names
451 +// and labels keep skill links working where no snapshot is fetched.
452 +function withoutWorkspaceSnapshots(
453 + providers: ReadonlyArray<ServerProvider>,
454 + catalogsPerCwd: ReadonlySet<string> = new Set(),
455 +): ServerProvider[] {
456 + return providers.map(({ workspaceSnapshots: _workspaceSnapshots, ...provider }) =>
457 + catalogsPerCwd.has(provider.instanceId)
458 + ? {
459 + ...provider,
460 + slashCommands: provider.slashCommands.map(({ name }) => ({ name })),
461 + skills: provider.skills.map(
462 + ({ description: _d, shortDescription: _s, ...skill }) => skill,
463 + ),
464 + }
465 + : provider,
466 + );
467 }
468
469 function unexpectedCompatibilityError(error: never): never {
@@ -1860,6 +1896,7 @@ export const layerWsRpc = (
1896 readonly usageLimitsCommand: boolean;
1897 readonly workspaceSnapshotsOnDemand: boolean;
1898 readonly deferNetworkDiscovery: boolean;
1899 + readonly catalogsPerCwd?: ReadonlySet<string>;
1900 }) {
1901 const keybindingsConfig = yield* keybindings.loadConfigState;
1902 const currentProviders = yield* providerRegistry.getProviders;
@@ -1867,7 +1904,7 @@ export const layerWsRpc = (
1904 ? withUsageLimitsCommands(currentProviders, yield* usageLimitSources.current)
1905 : currentProviders;
1906 const providers = options.workspaceSnapshotsOnDemand
1870 - ? withoutWorkspaceSnapshots(commandProviders)
1907 + ? withoutWorkspaceSnapshots(commandProviders, options.catalogsPerCwd)
1908 : commandProviders;
1909 const settings = ServerSettings.redactServerSettingsForClient(
1910 yield* serverSettings.getSettings,
@@ -2498,6 +2535,9 @@ export const layerWsRpc = (
2535 if (instanceId === undefined || cwd === undefined) {
2536 return { providers: withoutWorkspaceSnapshots(commandProviders) };
2537 }
2538 + const catalogOnDemand =
2539 + input.providerCatalogsOnDemand === true &&
2540 + (yield* providerInstances.getInstance(instanceId))?.snapshotForCwd !== undefined;
2541 return {
2542 providers: commandProviders
2543 .filter((provider) => provider.instanceId === instanceId)
@@ -2507,9 +2547,7 @@ export const layerWsRpc = (
2547 models: [],
2548 slashCommands: [],
2549 skills: [],
2510 - workspaceSnapshots: (provider.workspaceSnapshots ?? []).filter(
2511 - (snapshot) => snapshot.cwd === cwd,
2512 - ),
2550 + workspaceSnapshots: workspaceSnapshotOrCatalog(provider, cwd, catalogOnDemand),
2551 })),
2552 };
2553 }),
@@ -3234,7 +3272,22 @@ export const layerWsRpc = (
3272 Effect.gen(function* () {
3273 const usageLimitsCommand = input.usageLimitsCommand === true;
3274 const workspaceSnapshotsOnDemand = input.workspaceSnapshotsOnDemand === true;
3237 - // Buffer changes through discovery and the initial snapshot's RPC ACK wait.
3275 + // Only providers that can scan a cwd answer the client's on-demand catalog fetch.
3276 + // Read per update, since a replaced instance can change that.
3277 + const currentCatalogsPerCwd =
3278 + workspaceSnapshotsOnDemand && input.providerCatalogsOnDemand === true
3279 + ? providerInstances.listInstances.pipe(
3280 + Effect.map(
3281 + (instances) =>
3282 + new Set(
3283 + instances
3284 + .filter((instance) => instance.snapshotForCwd !== undefined)
3285 + .map((instance) => instance.instanceId),
3286 + ),
3287 + ),
3288 + )
3289 + : Effect.succeed(new Set<string>());
3290 + // Buffer changes through discovery and the initial snapshot's send.
3291 const keybindingsChanges = yield* keybindings.subscribeChanges;
3292 const settingsChanges = yield* serverSettings.subscribeChanges;
3293 const remoteTargets = yield* Effect.forkScoped(remoteOpenTargets.resolveTargets(), {
@@ -3250,6 +3303,7 @@ export const layerWsRpc = (
3303 usageLimitsCommand,
3304 workspaceSnapshotsOnDemand,
3305 deferNetworkDiscovery: true,
3306 + catalogsPerCwd: yield* currentCatalogsPerCwd,
3307 });
3308 const keybindingsUpdates = keybindingsChanges.pipe(
3309 Stream.map((event) => ({
@@ -3272,16 +3326,19 @@ export const layerWsRpc = (
3326 usageLimitsCommand ? sameUsageLimitCommandCoverage : () => true,
3327 ),
3328 ),
3275 - (providers, sources) => {
3276 - const commandProviders = usageLimitsCommand
3277 - ? withUsageLimitsCommands(providers, sources)
3278 - : providers;
3279 - // Stripped before the de-dupe below, so snapshot-only changes send nothing.
3280 - return workspaceSnapshotsOnDemand
3281 - ? withoutWorkspaceSnapshots(commandProviders)
3282 - : commandProviders;
3283 - },
3329 + (providers, sources) =>
3330 + usageLimitsCommand ? withUsageLimitsCommands(providers, sources) : providers,
3331 ).pipe(
3332 + // Stripped before the de-dupe below, so snapshot-only changes send nothing.
3333 + Stream.mapEffect((commandProviders) =>
3334 + workspaceSnapshotsOnDemand
3335 + ? currentCatalogsPerCwd.pipe(
3336 + Effect.map((catalogsPerCwd) =>
3337 + withoutWorkspaceSnapshots(commandProviders, catalogsPerCwd),
3338 + ),
3339 + )
3340 + : Effect.succeed(commandProviders),
3341 + ),
3342 // Compare initial paired values with the snapshot; blindly dropping them loses changes between snapshot and subscription.
3343 (updates) => Stream.concat(rpcInitialItems([config.providers]), updates),
3344 Stream.changesWith(
@@ -3480,7 +3537,7 @@ export const layer = Layer.unwrap(
3537 const rpcWebSocketHttpEffect = yield* Effect.gen(function* () {
3538 const { protocol, httpEffect } = yield* RpcServer.makeProtocolWithHttpEffectWebsocket;
3539 yield* RpcServer.make(ServerWsRpcGroup, WS_RPC_SERVER_OPTIONS).pipe(
3483 - Effect.provideService(RpcServer.Protocol, withTerminalOutputWindow(protocol)),
3540 + Effect.provideService(RpcServer.Protocol, withOutputWindow(protocol)),
3541 Effect.provide(
3542 Layer.merge(
3543 RpcAuthorization.layer(session.scopes, (method, payload) => {
packages/client-runtime/src/connection/supervisor.test.ts
+58 -11
@@ -571,7 +571,7 @@ describe("EnvironmentSupervisor", () => {
571 );
572 expect(Option.isNone(yield* SubscriptionRef.get(supervisor.prepared))).toBe(true);
573
574 - yield* TestClock.adjust("3 seconds");
574 + yield* TestClock.adjust("250 millis");
575 yield* awaitState(
576 supervisor.state,
577 (state) => state.phase === "connected" && state.generation === 2,
@@ -794,6 +794,26 @@ describe("EnvironmentSupervisor", () => {
794 }).pipe(Effect.provide(TestClock.layer())),
795 );
796
797 + it.effect("replaces a silent remote socket without the wait after a long resume", () =>
798 + Effect.gen(function* () {
799 + const harness = yield* makeSilentSocketHarness();
800 + const supervisor = yield* EnvironmentSupervisor.make(RELAY_ENTRY, {
801 + initiallyDesired: true,
802 + }).pipe(Effect.provide(harness.dependencies));
803 +
804 + yield* awaitState(supervisor.state, (state) => state.phase === "connected");
805 + yield* harness.wake("application-active-long");
806 + yield* TestClock.adjust("499 millis");
807 + expect(yield* Ref.get(harness.sessionCount)).toBe(1);
808 +
809 + yield* TestClock.adjust("1 millis");
810 + yield* awaitState(
811 + supervisor.state,
812 + (state) => state.phase === "connected" && state.generation === 2,
813 + );
814 + }).pipe(Effect.provide(TestClock.layer())),
815 + );
816 +
817 it.effect("replaces a silent socket even while the network reports offline", () =>
818 Effect.gen(function* () {
819 const harness = yield* makeSilentSocketHarness();
@@ -869,11 +889,11 @@ describe("EnvironmentSupervisor", () => {
889 yield* awaitState(supervisor.state, (state) => state.phase === "connected");
890 yield* harness.wake("application-active-probe");
891 yield* TestClock.adjust("1 second");
872 - yield* Ref.update(frames, (count) => count + 1);
873 - yield* TestClock.adjust("1 second");
892 yield* harness.wake("network-changed");
875 - // The running check sees the frame at 2.5 s, so a fresh one starts then.
876 - yield* TestClock.adjust("2999 millis");
893 + yield* TestClock.adjust("500 millis");
894 + yield* Ref.update(frames, (count) => count + 1);
895 + // The running check sees the frame at 1.6 s, so a fresh one starts then.
896 + yield* TestClock.adjust("2599 millis");
897 expect(yield* Ref.get(harness.sessionCount)).toBe(1);
898
899 yield* TestClock.adjust("1 millis");
@@ -967,22 +987,49 @@ describe("EnvironmentSupervisor", () => {
987 }).pipe(Effect.provide(TestClock.layer())),
988 );
989
970 - it.effect("keeps waiting for the probe when a frame arrives during the host check", () =>
990 + it.effect.each(["application-active-probe", "application-active-long"] as const)(
991 + "keeps waiting for the probe when a frame arrives during the host check (%s)",
992 + (wakeup) =>
993 + Effect.gen(function* () {
994 + const frames = yield* Ref.make(0);
995 + const harness = yield* makeHarness({
996 + prepare: (_attempt, target) => Effect.succeed(preparedFor(target)),
997 + initialConfig: () => Effect.succeed(PROBE_CAPABLE_CONFIG),
998 + probe: (attempt) => (attempt === 1 ? Effect.sleep("2 seconds") : Effect.void),
999 + inboundFrames: Ref.get(frames),
1000 + checkHost: () => Ref.update(frames, (count) => count + 1).pipe(Effect.as("answered")),
1001 + });
1002 + const supervisor = yield* EnvironmentSupervisor.make(LAN_ENTRY, {
1003 + initiallyDesired: true,
1004 + }).pipe(Effect.provide(harness.dependencies));
1005 +
1006 + yield* awaitState(supervisor.state, (state) => state.phase === "connected");
1007 + yield* harness.wake(wakeup);
1008 + yield* TestClock.adjust("5 seconds");
1009 +
1010 + expect(yield* Ref.get(harness.sessionCount)).toBe(1);
1011 + expect((yield* SubscriptionRef.get(supervisor.state)).generation).toBe(1);
1012 + }).pipe(Effect.provide(TestClock.layer())),
1013 + );
1014 +
1015 + it.effect("keeps a long-resume socket that delivers a frame during a slow host check", () =>
1016 Effect.gen(function* () {
1017 const frames = yield* Ref.make(0);
1018 const harness = yield* makeHarness({
1019 prepare: (_attempt, target) => Effect.succeed(preparedFor(target)),
1020 initialConfig: () => Effect.succeed(PROBE_CAPABLE_CONFIG),
976 - probe: (attempt) => (attempt === 1 ? Effect.sleep("2 seconds") : Effect.void),
1021 + probe: (attempt) => (attempt === 1 ? Effect.sleep("2500 millis") : Effect.void),
1022 inboundFrames: Ref.get(frames),
978 - checkHost: () => Ref.update(frames, (count) => count + 1).pipe(Effect.as("answered")),
1023 + checkHost: () => Effect.sleep("2 seconds").pipe(Effect.as("answered")),
1024 });
980 - const supervisor = yield* EnvironmentSupervisor.make(LAN_ENTRY, {
1025 + const supervisor = yield* EnvironmentSupervisor.make(RELAY_ENTRY, {
1026 initiallyDesired: true,
1027 }).pipe(Effect.provide(harness.dependencies));
1028
1029 yield* awaitState(supervisor.state, (state) => state.phase === "connected");
985 - yield* harness.wake("application-active-probe");
1030 + yield* harness.wake("application-active-long");
1031 + yield* TestClock.adjust("700 millis");
1032 + yield* Ref.update(frames, (count) => count + 1);
1033 yield* TestClock.adjust("5 seconds");
1034
1035 expect(yield* Ref.get(harness.sessionCount)).toBe(1);
@@ -1049,7 +1096,7 @@ describe("EnvironmentSupervisor", () => {
1096 yield* awaitState(supervisor.state, (state) => state.phase === "connected");
1097 yield* harness.wake("application-active-long");
1098 yield* harness.setNetworkStatus("offline");
1052 - yield* TestClock.adjust("2999 millis");
1099 + yield* TestClock.adjust("1499 millis");
1100 expect(yield* Ref.get(harness.sessionCount)).toBe(1);
1101
1102 yield* TestClock.adjust("1 millis");
packages/client-runtime/src/connection/supervisor.ts
+48 -18
@@ -42,6 +42,7 @@ import {
42
43 const RETRY_BASE_DELAY_MS = 1_000;
44 const RETRY_MAX_DELAY_MS = 300_000;
45 +const SESSION_LOSS_RETRY_DELAY_MS = 250;
46 // A cold server with a large database can take longer than 15 seconds to
47 // answer, and that is not a reason to drop a working connection.
48 const CONNECTION_ESTABLISHMENT_TIMEOUT = "15 seconds";
@@ -49,13 +50,17 @@ const establishmentTimeout = Duration.fromInputUnsafe(CONNECTION_ESTABLISHMENT_T
50 const synchronizationTimeout = Duration.fromInputUnsafe("75 seconds");
51 const CONNECTION_PROBE_TIMEOUT = "15 seconds";
52 const QUICK_CONNECTION_PROBE_TIMEOUT = "3 seconds";
53 +// After a long suspension pooled connections are often dead too, so the host
54 +// check can hang with the socket. A replacement is cheaper than the wait.
55 +const LONG_RESUME_PROBE_TIMEOUT = "1500 millis";
56 // A probe still unanswered this long checks the host with a fresh request. If
57 // the host answers while the socket stays silent, the socket is dead.
58 const SILENT_SOCKET_CHECK_AFTER = "500 millis";
59 const SILENT_SOCKET_RECHECK_EVERY = "1 second";
60 +const FRAME_CHECK_EVERY = "100 millis";
61 // Off the LAN one lost packet can stall a live socket for a second, so a remote
57 -// socket must stay silent this long. It must end before
58 -// QUICK_CONNECTION_PROBE_TIMEOUT so a long resume replaces a silent socket
62 +// socket must stay silent this long, except after a long resume. It must end
63 +// before QUICK_CONNECTION_PROBE_TIMEOUT so a resume replaces a silent socket
64 // instead of timing out.
65 const REMOTE_SILENT_SOCKET_AFTER = "2500 millis";
66 const BACKOFF_RESET_AFTER_MS = 30_000;
@@ -668,7 +673,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
673 // answer reconnects as quickly as a missing one, even on a remote
674 // route where a reconnect takes longer than the wait it replaces.
675 if (next.reason === "application-active-long") {
671 - return QUICK_CONNECTION_PROBE_TIMEOUT;
676 + return LONG_RESUME_PROBE_TIMEOUT;
677 }
678 if (next.reason !== "application-active-probe" && next.reason !== "network-changed") {
679 return undefined;
@@ -693,6 +698,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
698 const awaitSilentSocket = Effect.fn("EnvironmentSupervisor.awaitSilentSocket")(function* (
699 lease: ConnectedLease,
700 routeKind: ConnectionRouteKind | undefined,
701 + longResume: boolean,
702 ) {
703 const startedAt = yield* Clock.monotonicTimeNanos;
704 yield* Effect.sleep(SILENT_SOCKET_CHECK_AFTER);
@@ -707,17 +713,26 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
713 const socketSilent = lease.session.inboundFrames.pipe(
714 Effect.map((frames) => frames === framesBefore),
715 );
710 - // A radio still waking up or a stale pooled connection can fail one check.
711 - while ((yield* driver.checkHost(lease.prepared)) !== "answered") {
712 - if (!(yield* socketSilent)) return "alive" as const;
713 - yield* Effect.sleep(SILENT_SOCKET_RECHECK_EVERY);
714 - }
715 - if (!answersQuickly(routeKind)) {
716 - const silentFor = (yield* Clock.monotonicTimeNanos) - startedAt;
717 - const remaining = Duration.toNanosUnsafe(REMOTE_SILENT_SOCKET_AFTER) - silentFor;
718 - if (remaining > 0n) yield* Effect.sleep(Duration.nanos(remaining));
719 - }
720 - return (yield* socketSilent) ? ("silent" as const) : ("alive" as const);
716 + // The session only counts frames, so a host check slower than the probe
717 + // deadline would otherwise hide a frame until after the socket is replaced.
718 + const frameArrives = Effect.sleep(FRAME_CHECK_EVERY).pipe(
719 + Effect.andThen(socketSilent),
720 + Effect.repeat({ while: (silent) => silent }),
721 + Effect.as("alive" as const),
722 + );
723 + const hostAnswersWhileSilent = Effect.gen(function* () {
724 + // A radio still waking up or a stale pooled connection can fail one check.
725 + while ((yield* driver.checkHost(lease.prepared)) !== "answered") {
726 + yield* Effect.sleep(SILENT_SOCKET_RECHECK_EVERY);
727 + }
728 + if (!answersQuickly(routeKind) && !longResume) {
729 + const silentFor = (yield* Clock.monotonicTimeNanos) - startedAt;
730 + const remaining = Duration.toNanosUnsafe(REMOTE_SILENT_SOCKET_AFTER) - silentFor;
731 + if (remaining > 0n) yield* Effect.sleep(Duration.nanos(remaining));
732 + }
733 + return (yield* socketSilent) ? ("silent" as const) : ("alive" as const);
734 + });
735 + return yield* Effect.race(hostAnswersWhileSilent, frameArrives);
736 });
737
738 // Returning to the app, an explicit retry, and the network reporting offline
@@ -764,7 +779,7 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
779 let recheck = false;
780 let longResume = isLongResume(next);
781 const startSilentSocketCheck = Effect.gen(function* () {
767 - silentSocket = yield* Effect.forkChild(awaitSilentSocket(lease, routeKind));
782 + silentSocket = yield* Effect.forkChild(awaitSilentSocket(lease, routeKind, longResume));
783 recheck = false;
784 });
785 // The web and desktop foreground probe allows a busy host 15 seconds,
@@ -784,7 +799,8 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
799 { discard: true },
800 ),
801 );
787 - let deadline = (yield* Clock.monotonicTimeNanos) + Duration.toNanosUnsafe(probeTimeout);
802 + let longResumeStartedAt = yield* Clock.monotonicTimeNanos;
803 + let deadline = longResumeStartedAt + Duration.toNanosUnsafe(probeTimeout);
804 for (;;) {
805 const remaining = deadline - (yield* Clock.monotonicTimeNanos);
806 const probeEvent = yield* Effect.raceAllFirst([
@@ -805,6 +821,12 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
821 ]);
822 if (probeEvent._tag === "SocketAlive") {
823 silentSocket = undefined;
824 + // A frame shows the socket still works, so its probe gets the usual resume time.
825 + if (longResume) {
826 + const aliveDeadline =
827 + longResumeStartedAt + Duration.toNanosUnsafe(QUICK_CONNECTION_PROBE_TIMEOUT);
828 + if (aliveDeadline > deadline) deadline = aliveDeadline;
829 + }
830 if (recheck) yield* startSilentSocketCheck;
831 continue;
832 }
@@ -839,8 +861,11 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
861 }
862 const signalTimeout = probeTimeoutFor(probeEvent.signal, routeKind);
863 if (signalTimeout !== undefined) {
864 + if (isLongResume(probeEvent.signal) && !longResume) {
865 + longResume = true;
866 + longResumeStartedAt = yield* Clock.monotonicTimeNanos;
867 + }
868 yield* armSilentSocketCheck(probeEvent.signal);
843 - longResume ||= isLongResume(probeEvent.signal);
869 const signalDeadline =
870 (yield* Clock.monotonicTimeNanos) + Duration.toNanosUnsafe(signalTimeout);
871 if (signalDeadline < deadline) deadline = signalDeadline;
@@ -1155,8 +1180,13 @@ export const make = Effect.fn("EnvironmentSupervisor.make")(function* (
1180 continue;
1181 }
1182
1183 + // The first loss of a live session retries almost at once; the ladder
1184 + // still counts it, so a host that keeps dropping sessions backs off.
1185 + const firstLoss = outcome.established && failureCount === 0;
1186 failureCount += 1;
1159 - const delayMs = retryDelayMs(failureCount - 1, yield* Random.next);
1187 + const delayMs = firstLoss
1188 + ? Math.round(SESSION_LOSS_RETRY_DELAY_MS * (0.5 + (yield* Random.next) / 2))
1189 + : retryDelayMs(failureCount - 1, yield* Random.next);
1190 pendingRetry = Option.map(attemptSpan, (previousAttempt) => ({
1191 previousAttempt,
1192 failureCount,
packages/client-runtime/src/rpc/session.test.ts
+32
@@ -404,6 +404,38 @@ describe("RpcSessionFactory", () => {
404 }),
405 );
406
407 + it.effect("ends a stream at once when its socket closes", () =>
408 + Effect.gen(function* () {
409 + const { factory, sockets } = yield* makeFactory();
410 + const session = yield* factory.connect(PREPARED);
411 + const readyFiber = yield* Effect.forkChild(session.ready);
412 + const socket = yield* awaitSocket(sockets);
413 + socket.open();
414 + yield* completeInitialConfig(socket);
415 + yield* Fiber.join(readyFiber);
416 + // Like the supervisor: the closed session interrupts its streams, and a
417 + // stream interrupt must not wait for a socket that is gone.
418 + const ended = yield* Effect.forkChild(
419 + Effect.raceFirst(
420 + Effect.flip(session.closed),
421 + session.client[WS_METHODS.subscribeServerLifecycle]({}).pipe(
422 + Stream.runDrain,
423 + Effect.andThen(Effect.never),
424 + ),
425 + ),
426 + );
427 + yield* awaitRequest(socket, 1);
428 + socket.close(1006);
429 + yield* TestClock.adjust("999 millis");
430 +
431 + expect(ended.pollUnsafe()).toBeDefined();
432 + // A request made after the close still fails instead of waiting forever.
433 + const lateProbe = yield* Effect.forkChild(session.probe);
434 + yield* TestClock.adjust("999 millis");
435 + expect(lateProbe.pollUnsafe()?._tag).toBe("Failure");
436 + }).pipe(Effect.scoped, Effect.provide(TestClock.layer())),
437 + );
438 +
439 it.effect("replays current config and broadcasts updates to every subscriber", () =>
440 Effect.scoped(
441 Effect.gen(function* () {
packages/client-runtime/src/rpc/session.ts
+14 -1
@@ -66,6 +66,7 @@ export interface RpcSessionOptions {
66 readonly environmentThemes?: boolean;
67 readonly usageLimitSources?: boolean;
68 readonly usageLimitsCommand?: boolean;
69 + readonly providerCatalogsOnDemand?: boolean;
70 }
71
72 export class RpcSessionFactory extends Context.Service<
@@ -175,6 +176,7 @@ export const make = Effect.fn("RpcSessionFactory.make")(function* (
176 ...(options.environmentThemes === true ? { environmentThemes: true } : {}),
177 ...(options.usageLimitSources === true ? { usageLimitSources: true } : {}),
178 ...(options.usageLimitsCommand === true ? { usageLimitsCommand: true } : {}),
179 + ...(options.providerCatalogsOnDemand === true ? { providerCatalogsOnDemand: true } : {}),
180 };
181 // Config state merges the snapshots it fetches back in, so no subscriber needs them broadcast.
182 const onDemandSnapshots = (input: ServerConfigSubscriptionInput) => ({
@@ -247,7 +249,18 @@ export const make = Effect.fn("RpcSessionFactory.make")(function* (
249 retryTransientErrors: false,
250 retryPolicy: Schedule.recurs(0),
251 onPingTimeout: Ref.set(pingTimedOut, true),
250 - }),
252 + }).pipe(
253 + // Teardown sends an interrupt per open stream, and a write to a closed
254 + // socket waits out the client's 1s interrupt timeout before reconnecting.
255 + // Requests still go through, so they fail with the protocol error.
256 + Effect.map((protocol) => ({
257 + ...protocol,
258 + send: (clientId, request, transferables) =>
259 + request._tag !== "Request" && Deferred.isDoneUnsafe(disconnected)
260 + ? Effect.void
261 + : protocol.send(clientId, request, transferables),
262 + })),
263 + ),
264 ).pipe(
265 Layer.provide(
266 Layer.mergeAll(
packages/client-runtime/src/state/server.ts
+5
@@ -365,6 +365,7 @@ export interface ServerConfigSubscriptionOptions {
365 readonly environmentThemes?: boolean;
366 readonly usageLimitSources?: boolean;
367 readonly usageLimitsCommand?: boolean;
368 + readonly providerCatalogsOnDemand?: boolean;
369 }
370
371 // Configs are JSON-shaped. Missing and undefined object fields persist identically.
@@ -486,6 +487,7 @@ export const makeEnvironmentServerConfigState = Effect.fn("EnvironmentServerConf
487 ...(subscription.environmentThemes === true ? { environmentThemes: true } : {}),
488 ...(subscription.usageLimitSources === true ? { usageLimitSources: true } : {}),
489 ...(subscription.usageLimitsCommand === true ? { usageLimitsCommand: true } : {}),
490 + ...(subscription.providerCatalogsOnDemand === true ? { providerCatalogsOnDemand: true } : {}),
491 }).pipe(
492 Stream.runForEach((event) =>
493 Effect.gen(function* () {
@@ -755,6 +757,7 @@ export function createServerEnvironmentAtoms<R, E>(
757 readonly environmentThemes?: boolean;
758 readonly usageLimitSources?: boolean;
759 readonly usageLimitsCommand?: boolean;
760 + readonly providerCatalogsOnDemand?: boolean;
761 },
762 ) {
763 const configScheduler = createAtomCommandScheduler();
@@ -770,6 +773,7 @@ export function createServerEnvironmentAtoms<R, E>(
773 ...(options.environmentThemes === true ? { environmentThemes: true } : {}),
774 ...(options.usageLimitSources === true ? { usageLimitSources: true } : {}),
775 ...(options.usageLimitsCommand === true ? { usageLimitsCommand: true } : {}),
776 + ...(options.providerCatalogsOnDemand === true ? { providerCatalogsOnDemand: true } : {}),
777 }),
778 )
779 .pipe(
@@ -1401,6 +1405,7 @@ export function createServerEnvironmentAtoms<R, E>(
1405 ...input,
1406 workspaceSnapshotsOnDemand: true,
1407 ...(options.usageLimitsCommand === true ? { usageLimitsCommand: true } : {}),
1408 + ...(options.providerCatalogsOnDemand === true ? { providerCatalogsOnDemand: true } : {}),
1409 }),
1410 onSuccess: ({ environmentId, input }, registry, result) =>
1411 Effect.sync(() => {
packages/client-runtime/src/state/shell-sync.test.ts
+46 -6
@@ -56,7 +56,10 @@ const LIVE_SHELL_SNAPSHOT: OrchestrationV2ShellSnapshot = {
56 function session(client: WsRpcProtocolClient): RpcSession.RpcSession {
57 return {
58 client,
59 - initialConfig: Effect.succeed({ shellResumeCompletionMarker: true } as never),
59 + initialConfig: Effect.succeed({
60 + shellResumeCompletionMarker: true,
61 + environment: { serverVersion: "1.0.0" },
62 + } as never),
63 subscribeServerConfig: (input = {}) => client.subscribeServerConfig(input),
64 ready: Effect.void,
65 probe: Effect.void,
@@ -817,12 +820,12 @@ describe("environment shell synchronization", () => {
820 it.effect("opts in to shell omissions and counts authoritative snapshots and removals", () =>
821 Effect.gen(function* () {
822 const events = yield* Queue.unbounded<OrchestrationV2ShellStreamItem>();
820 - const subscribeInput = yield* Deferred.make<unknown>();
823 + const subscribeInputs = yield* Queue.unbounded<unknown>();
824 const loadOptions = yield* Ref.make<ReadonlyArray<unknown>>([]);
825 const client = {
826 [ORCHESTRATION_V2_WS_METHODS.subscribeShell]: (input: unknown) =>
827 Stream.unwrap(
825 - Deferred.succeed(subscribeInput, input).pipe(Effect.as(Stream.fromQueue(events))),
828 + Queue.offer(subscribeInputs, input).pipe(Effect.as(Stream.fromQueue(events))),
829 ),
830 } as unknown as WsRpcProtocolClient;
831 const familySession: RpcSession.RpcSession = {
@@ -831,12 +834,14 @@ describe("environment shell synchronization", () => {
834 shellResumeCompletionMarker: true,
835 shellThreadFamilies: true,
836 shellOlderSettled: true,
837 + environment: { serverVersion: "1.0.0", databaseMigration: 70 },
838 } as never),
839 };
840 + const activeSession = yield* SubscriptionRef.make(Option.some(familySession));
841 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
842 target: TARGET,
843 state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE),
839 - session: yield* SubscriptionRef.make(Option.some(familySession)),
844 + session: activeSession,
845 prepared: yield* SubscriptionRef.make(Option.some(PREPARED)),
846 connect: Effect.void,
847 disconnect: Effect.void,
@@ -871,7 +876,7 @@ describe("environment shell synchronization", () => {
876 SubscriptionRef.changes(shellState).pipe(Stream.filter(predicate), Stream.runHead);
877
878 // Both transports must omit, or omitted children would come back through the socket.
874 - expect(yield* Deferred.await(subscribeInput)).toMatchObject({
879 + expect(yield* Queue.take(subscribeInputs)).toMatchObject({
880 afterSequence: 1,
881 omitIdleSubagents: true,
882 omitOlderSettled: true,
@@ -907,6 +912,31 @@ describe("environment shell synchronization", () => {
912 yield* stateWhere((state) => state.generation > loaded.generation),
913 );
914 expect(reset.removedAt.size).toBe(0);
915 +
916 + // A reconnect resumes the server-loaded shell from its cursor, with no second HTTP load.
917 + yield* SubscriptionRef.set(activeSession, Option.some({ ...familySession }));
918 + expect(yield* Queue.take(subscribeInputs)).toMatchObject({
919 + afterSequence: 5,
920 + omitIdleSubagents: true,
921 + omitOlderSettled: true,
922 + });
923 + expect(yield* Ref.get(loadOptions)).toHaveLength(1);
924 +
925 + // A migrated server may have rewritten rows without events, so it reloads.
926 + yield* SubscriptionRef.set(
927 + activeSession,
928 + Option.some({
929 + ...familySession,
930 + initialConfig: familySession.initialConfig.pipe(
931 + Effect.map((config) => ({
932 + ...config,
933 + environment: { ...config.environment, databaseMigration: 71 },
934 + })),
935 + ),
936 + }),
937 + );
938 + yield* Queue.take(subscribeInputs);
939 + expect(yield* Ref.get(loadOptions)).toHaveLength(2);
940 }),
941 );
942
@@ -1076,7 +1106,17 @@ describe("environment shell synchronization", () => {
1106 );
1107 yield* Deferred.await(firstFillStarted);
1108
1079 - yield* SubscriptionRef.set(activeSession, Option.some(session(client)));
1109 + // New omissions make the next session reload instead of resuming.
1110 + yield* SubscriptionRef.set(
1111 + activeSession,
1112 + Option.some({
1113 + ...session(client),
1114 + initialConfig: Effect.succeed({
1115 + shellThreadFamilies: true,
1116 + environment: { serverVersion: "1.0.0" },
1117 + } as never),
1118 + }),
1119 + );
1120 yield* SubscriptionRef.changes(shellState).pipe(
1121 Stream.filter(
1122 (state) => Option.isSome(state.snapshot) && state.snapshot.value.snapshotSequence === 2,
packages/client-runtime/src/state/shell.ts
+62 -26
@@ -26,7 +26,7 @@ import * as ConnectionWakeups from "../connection/wakeups.ts";
26 import { safeErrorLogAttributes } from "../errors/safeLog.ts";
27 import * as Persistence from "../platform/persistence.ts";
28 import { runCachePersistence } from "./cachePersistence.ts";
29 -import { subscribeDynamic } from "../rpc/client.ts";
29 +import { subscribeDynamicWithSession } from "../rpc/client.ts";
30 import type { RpcSession } from "../rpc/session.ts";
31 import * as ShellSnapshotLoader from "./shellSnapshotHttp.ts";
32 import type { DeferredShellSnapshot } from "./shellPullRequests.ts";
@@ -82,6 +82,16 @@ function withoutDeferredPullRequests(value: DeferredShellSnapshot): Orchestratio
82
83 const SHELL_SYNCHRONIZATION_ERROR_MESSAGE = "Could not synchronize environment data.";
84
85 +function groupBySession<A>(entries: ReadonlyArray<readonly [RpcSession, A]>) {
86 + const groups: Array<[RpcSession, Array<A>]> = [];
87 + for (const [session, value] of entries) {
88 + const last = groups.at(-1);
89 + if (last?.[0] === session) last[1].push(value);
90 + else groups.push([session, [value]]);
91 + }
92 + return groups;
93 +}
94 +
95 export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")(function* () {
96 const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor;
97 const cache = yield* Persistence.EnvironmentCacheStore;
@@ -106,8 +116,11 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
116 status: shellStatusForSnapshot(cachedSnapshot),
117 });
118 const awaitingCompletion = yield* Ref.make(false);
109 - const lastAuthoritativeSession = yield* Ref.make<RpcSession | null>(null);
110 - const activeSubscriptionSession = yield* Ref.make<RpcSession | null>(null);
119 + // The omissions and server build of the subscription whose snapshot the
120 + // in-memory shell last came from; null while it holds only cached data.
121 + const authoritativeOmission = yield* Ref.make<string | null>(null);
122 + // Items buffered from a previous session keep that session's key.
123 + const sessionOmissions = new WeakMap<RpcSession, string | null>();
124 const latestLiveSnapshot = yield* Ref.make<Option.Option<OrchestrationV2ShellSnapshot>>(
125 Option.none(),
126 );
@@ -208,12 +221,13 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
221
222 const applyItems = Effect.fn("EnvironmentShellState.applyItems")(function* (
223 items: ReadonlyArray<OrchestrationV2ShellStreamItem>,
224 + omission: string | null,
225 pendingLinkIds?: ReadonlySet<ThreadId>,
226 ) {
227 const initial = yield* SubscriptionRef.get(state);
228 let waiting = yield* Ref.get(awaitingCompletion);
229 let next = initial;
216 - let receivedSnapshot = false;
230 + let receivedAuthoritativeSnapshot = false;
231 let deltas: Array<ShellStreamEvent> = [];
232 const flushDeltas = () => {
233 if (deltas.length > 0 && Option.isSome(next.snapshot)) {
@@ -267,7 +281,7 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
281 ? undefined
282 : { resolvedRepositoryIdentityRoots: item.resolvedRepositoryIdentityRoots },
283 );
270 - receivedSnapshot = true;
284 + if (authoritative) receivedAuthoritativeSnapshot = true;
285 next = {
286 snapshot: Option.some(nextSnapshot),
287 status: waiting ? "synchronizing" : "live",
@@ -288,11 +302,8 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
302 yield* Ref.set(latestLiveSnapshot, next.snapshot);
303 }
304 yield* SubscriptionRef.set(state, next);
291 - if (receivedSnapshot) {
292 - const session = yield* Ref.get(activeSubscriptionSession);
293 - if (session !== null) {
294 - yield* Ref.set(lastAuthoritativeSession, session);
295 - }
305 + if (receivedAuthoritativeSnapshot && omission !== null) {
306 + yield* Ref.set(authoritativeOmission, omission);
307 }
308 if (next.snapshot !== initial.snapshot && Option.isSome(next.snapshot)) {
309 yield* Queue.offer(persistence, next.snapshot.value);
@@ -374,25 +385,42 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
385
386 yield* setSynchronizing;
387 yield* Effect.forkScoped(
377 - subscribeDynamic(
388 + subscribeDynamicWithSession(
389 ORCHESTRATION_V2_WS_METHODS.subscribeShell,
390 Effect.fn("EnvironmentShellState.makeSubscribeInput")(function* (session) {
380 - yield* Ref.set(activeSubscriptionSession, session);
381 - const { omitIdleSubagents, omitOlderSettled } = yield* session.initialConfig.pipe(
382 - Effect.map((config) => ({
383 - omitIdleSubagents: config.shellThreadFamilies === true,
384 - omitOlderSettled:
385 - config.shellOlderSettled === true && config.shellThreadFamilies === true,
386 - })),
387 - Effect.orElseSucceed(() => ({ omitIdleSubagents: false, omitOlderSettled: false })),
388 - );
391 + const { omitIdleSubagents, omitOlderSettled, omissionKey } =
392 + yield* session.initialConfig.pipe(
393 + Effect.map((config) => {
394 + const omitIdleSubagents = config.shellThreadFamilies === true;
395 + const omitOlderSettled = config.shellOlderSettled === true && omitIdleSubagents;
396 + // Migrations rewrite projections without events, so an upgraded server reloads.
397 + const { serverVersion, databaseMigration } = config.environment;
398 + return {
399 + omitIdleSubagents,
400 + omitOlderSettled,
401 + omissionKey: JSON.stringify([
402 + omitIdleSubagents,
403 + omitOlderSettled,
404 + serverVersion,
405 + databaseMigration ?? null,
406 + ]),
407 + };
408 + }),
409 + Effect.orElseSucceed(() => ({
410 + omitIdleSubagents: false,
411 + omitOlderSettled: false,
412 + omissionKey: null,
413 + })),
414 + );
415 + sessionOmissions.set(session, omissionKey);
416 yield* Ref.set(awaitingCompletion, true);
417 yield* setSynchronizing;
418
392 - // Foreground resubscriptions on the same live session can resume from
393 - // the in-memory cursor. A new session reloads the authoritative HTTP
394 - // snapshot so a valid cursor cannot preserve incomplete cached data.
395 - const hasAuthoritativeSnapshot = (yield* Ref.get(lastAuthoritativeSession)) === session;
419 + // A shell from a server snapshot resumes from its cursor, on this session or a
420 + // new one. Cached data, other omissions, or another server build reload the
421 + // authoritative HTTP snapshot so a valid cursor cannot preserve stale data.
422 + const hasAuthoritativeSnapshot =
423 + omissionKey !== null && (yield* Ref.get(authoritativeOmission)) === omissionKey;
424 let canResume = hasAuthoritativeSnapshot;
425 let current = yield* SubscriptionRef.get(state);
426 if (!hasAuthoritativeSnapshot || Option.isNone(current.snapshot)) {
@@ -427,7 +455,7 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
455 ? []
456 : httpSnapshot.value.threads.map((thread) => thread.id),
457 );
430 - yield* applyItems([{ kind: "snapshot", snapshot }], pendingLinkIds);
458 + yield* applyItems([{ kind: "snapshot", snapshot }], omissionKey, pendingLinkIds);
459 canResume = true;
460 current = yield* SubscriptionRef.get(state);
461 if (Option.isSome(current.snapshot)) {
@@ -454,7 +482,15 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
482 retryExpectedFailureAfter: "250 millis",
483 resubscribe: foregroundResubscriptions,
484 },
457 - ).pipe(Stream.runForEachArray((items) => applyItems(items))),
485 + ).pipe(
486 + Stream.runForEachArray((entries) =>
487 + Effect.forEach(
488 + groupBySession(entries),
489 + ([session, items]) => applyItems(items, sessionOmissions.get(session) ?? null),
490 + { discard: true },
491 + ),
492 + ),
493 + ),
494 );
495 yield* SubscriptionRef.changes(supervisor.state).pipe(
496 Stream.runForEach((connectionState) => {
packages/contracts/src/rpc.ts
+4
@@ -655,6 +655,8 @@ export const WsServerRefreshProvidersRpc = Rpc.make(WS_METHODS.serverRefreshProv
655 workspaceSnapshotsOnDemand: Schema.optional(Schema.Boolean),
656 /** As on `subscribeServerConfig`: add /usage-limits to the replied snapshot. */
657 usageLimitsCommand: Schema.optional(Schema.Boolean),
658 + /** As on `subscribeServerConfig`: a failed cwd scan replies with the machine catalog as a pending snapshot. */
659 + providerCatalogsOnDemand: Schema.optional(Schema.Boolean),
660 }),
661 success: ServerProviderUpdatedPayload,
662 error: Schema.Union([EnvironmentAuthorizationError, ProviderSetupError]),
@@ -1893,6 +1895,8 @@ export const WsSubscribeServerConfigRpc = Rpc.make(WS_METHODS.subscribeServerCon
1895 usageLimitsCommand: Schema.optional(Schema.Boolean),
1896 /** Omit per-cwd provider snapshots; the client fetches its cwd through `server.refreshProviders`. */
1897 workspaceSnapshotsOnDemand: Schema.optional(Schema.Boolean),
1898 + /** With on-demand snapshots, also omit machine slash commands and skills a cwd snapshot replaces. */
1899 + providerCatalogsOnDemand: Schema.optional(Schema.Boolean),
1900 }),
1901 success: ServerConfigStreamEvent,
1902 error: Schema.Union([KeybindingsConfigError, ServerSettingsError, EnvironmentAuthorizationError]),