The best way to control Claude Code, Codex and more from any computer or phone
perf(client): reopening a finished command row no longer refetches its output (#34002)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Nov 10, 2026 at 10:42 UTC
05020d6fa9ac46b078812441adce64eea1a4fb26
3 files changed
+122
-18
packages/client-runtime/src/state/orchestration.ts
+31
-9
@@ -4,6 +4,7 @@ import {
4
type ThreadId,
5
type OrchestrationV2ThreadLinkMetadata,
6
type OrchestrationGetTurnDiffResult,
7
+ type OrchestrationV2GetTurnItemInput,
8
} from "@t3tools/contracts";
9
import * as Effect from "effect/Effect";
10
import * as Option from "effect/Option";
@@ -12,7 +13,11 @@ import * as SubscriptionRef from "effect/SubscriptionRef";
13
import type { HttpClient } from "effect/http";
14
import { Atom } from "effect/reactivity";
15
15
-import { applyCommandOutputFrame, EMPTY_COMMAND_OUTPUT } from "./commandOutput.ts";
16
+import {
17
+ applyCommandOutputFrame,
18
+ EMPTY_COMMAND_OUTPUT,
19
+ type CommandOutputView,
20
+} from "./commandOutput.ts";
21
22
import {
23
createEnvironmentQueryAtomFamily,
@@ -49,6 +54,19 @@ export function createOrchestrationEnvironmentAtoms<R, E>(
54
? {}
55
: { onAnswer: options.onCheckpointDiffAnswer };
56
const shellState = options.shellState;
57
+ const liveTurnItem = createEnvironmentRpcQueryAtomFamily(runtime, {
58
+ label: "environment-data:orchestration:turn-item",
59
+ tag: ORCHESTRATION_V2_WS_METHODS.getTurnItem,
60
+ staleTimeMs: 60_000,
61
+ idleTtlMs: 60_000,
62
+ });
63
+ // A settled item's revision is its updatedAt, so the answer for it never goes stale.
64
+ const settledTurnItem = createEnvironmentRpcQueryAtomFamily(runtime, {
65
+ label: "environment-data:orchestration:turn-item",
66
+ tag: ORCHESTRATION_V2_WS_METHODS.getTurnItem,
67
+ staleTimeMs: Number.POSITIVE_INFINITY,
68
+ idleTtlMs: 5 * 60_000,
69
+ });
70
const refreshThreadLookup = ({
71
environmentId,
72
input,
@@ -139,12 +157,15 @@ export function createOrchestrationEnvironmentAtoms<R, E>(
157
tag: ORCHESTRATION_V2_WS_METHODS.subscribeThread,
158
idleTtlMs: 0,
159
}),
142
- // One expanded command row's output. Mounted only while the row is open,
143
- // and released as soon as it closes, so hidden rows receive nothing.
160
+ // One expanded command row's output. A running command's subscription ends
161
+ // as soon as its row closes, so hidden rows receive nothing; a finished
162
+ // command's final output stays cached so reopening it does not refetch.
163
commandOutput: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
164
label: "environment-data:orchestration-v2:command-output",
165
tag: ORCHESTRATION_V2_WS_METHODS.subscribeCommandOutput,
166
idleTtlMs: 0,
167
+ completeWhen: (view: CommandOutputView) => !view.running,
168
+ completedTtlMs: 5 * 60_000,
169
// `scan` emits its seed first; skipping it keeps the row "loading" until the first frame.
170
transform: (frames) =>
171
frames.pipe(
@@ -185,12 +206,13 @@ export function createOrchestrationEnvironmentAtoms<R, E>(
206
label: "environment-data:orchestration:force-stop-thread",
207
tag: ORCHESTRATION_V2_WS_METHODS.forceStopThread,
208
}),
188
- turnItem: createEnvironmentRpcQueryAtomFamily(runtime, {
189
- label: "environment-data:orchestration:turn-item",
190
- tag: ORCHESTRATION_V2_WS_METHODS.getTurnItem,
191
- staleTimeMs: 60_000,
192
- idleTtlMs: 60_000,
193
- }),
209
+ turnItem: (target: {
210
+ readonly environmentId: EnvironmentId;
211
+ readonly input: OrchestrationV2GetTurnItemInput;
212
+ }) =>
213
+ target.input.revision === undefined || target.input.revision === "live"
214
+ ? liveTurnItem(target)
215
+ : settledTurnItem(target),
216
fullThreadDiff: createEnvironmentRpcQueryAtomFamily(runtime, {
217
label: "environment-data:orchestration:full-thread-diff",
218
tag: ORCHESTRATION_V2_WS_METHODS.getFullThreadDiff,
packages/client-runtime/src/state/runtime.test.ts
+57
@@ -392,6 +392,63 @@ describe("finite environment subscription lifecycle", () => {
392
}),
393
),
394
);
395
+ it.effect.each([
396
+ { completedTtlMs: undefined, subscriptions: 2 },
397
+ { completedTtlMs: 60_000, subscriptions: 1 },
398
+ ])(
399
+ "reopening a completed subscription subscribes $subscriptions time(s) with completedTtlMs $completedTtlMs",
400
+ ({ completedTtlMs, subscriptions }) =>
401
+ Effect.gen(function* () {
402
+ let subscribed = 0;
403
+ const environmentRegistry = EnvironmentRegistry.EnvironmentRegistry.of({
404
+ followStream: (_environmentId: EnvironmentId, source: Stream.Stream<unknown>) => source,
405
+ } as unknown as EnvironmentRegistry.EnvironmentRegistry["Service"]);
406
+ const runtime = Atom.runtime(
407
+ Layer.succeed(EnvironmentRegistry.EnvironmentRegistry, environmentRegistry),
408
+ );
409
+ const family = createEnvironmentSubscriptionAtomFamily(runtime, {
410
+ label: "test.completed-subscription",
411
+ subscribe: () =>
412
+ Stream.suspend(() => {
413
+ subscribed += 1;
414
+ return Stream.make({ complete: false }, { complete: true });
415
+ }),
416
+ completeWhen: (value) => value.complete,
417
+ idleTtlMs: 0,
418
+ ...(completedTtlMs === undefined ? {} : { completedTtlMs }),
419
+ });
420
+ const atom = family({ environmentId: QUERY_ENVIRONMENT.environmentId, input: undefined });
421
+ // Flushed after each close: idle removals have run by the time a user reopens a row.
422
+ const scheduled: Array<() => void> = [];
423
+ const registry = AtomRegistry.make({
424
+ scheduleTask: (task) => {
425
+ scheduled.push(task);
426
+ return () => undefined;
427
+ },
428
+ });
429
+ const open = Effect.gen(function* () {
430
+ const settled = yield* Deferred.make<boolean>();
431
+ const unsubscribe = registry.subscribe(
432
+ atom,
433
+ (result) => {
434
+ if (AsyncResult.isSuccess(result) && !result.waiting) {
435
+ Deferred.doneUnsafe(settled, Exit.succeed(result.value.complete));
436
+ }
437
+ },
438
+ { immediate: true },
439
+ );
440
+ const complete = yield* Deferred.await(settled);
441
+ unsubscribe();
442
+ for (const task of scheduled.splice(0)) task();
443
+ return complete;
444
+ });
445
+
446
+ expect(yield* open).toBe(true);
447
+ expect(yield* open).toBe(true);
448
+ expect(subscribed).toBe(subscriptions);
449
+ registry.dispose();
450
+ }),
451
+ );
452
});
453
454
describe("environment query lifecycle", () => {
packages/client-runtime/src/state/runtime.ts
+34
-9
@@ -122,6 +122,8 @@ interface EnvironmentSubscriptionAtomOptions<Input, A, E, R> {
122
readonly subscribe: (input: Input) => Stream.Stream<A, E, R>;
123
readonly completeWhen?: (value: A) => boolean;
124
readonly idleTtlMs?: number;
125
+ /** Keeps a completed value this long after its last reader leaves, so reopening skips a resubscribe. */
126
+ readonly completedTtlMs?: number;
127
}
128
129
export type SettledAsyncResult<A, E> = AsyncResult.Success<A, E> | AsyncResult.Failure<A, E>;
@@ -826,7 +828,12 @@ export function createEnvironmentSubscriptionAtomFamily<R, ER, Input, A, E>(
828
EnvironmentSupervisor.EnvironmentSupervisor | R
829
>,
830
) {
829
- const family = Atom.family((key: string) => {
831
+ const idleTtlMs = options.idleTtlMs ?? 5 * 60_000;
832
+ const label = (key: string) =>
833
+ options.sensitiveInput
834
+ ? `${options.label}:${parseEnvironmentRpcKey<Input>(key).environmentId}`
835
+ : `${options.label}:${key}`;
836
+ const live = Atom.family((key: string) => {
837
const target = parseEnvironmentRpcKey<Input>(key);
838
return runtime
839
.atom(
@@ -834,15 +841,31 @@ export function createEnvironmentSubscriptionAtomFamily<R, ER, Input, A, E>(
841
followStreamInEnvironment(target.environmentId, options.subscribe(target.input)),
842
).pipe(options.completeWhen ? Stream.takeUntil(options.completeWhen) : (stream) => stream),
843
)
837
- .pipe(
838
- Atom.setIdleTTL(options.idleTtlMs ?? 5 * 60_000),
839
- Atom.withLabel(
840
- options.sensitiveInput
841
- ? `${options.label}:${target.environmentId}`
842
- : `${options.label}:${key}`,
843
- ),
844
- );
844
+ .pipe(Atom.setIdleTTL(idleTtlMs), Atom.withLabel(label(key)));
845
});
846
+ const { completeWhen, completedTtlMs } = options;
847
+ if (completeWhen === undefined || completedTtlMs === undefined) {
848
+ return (target: { readonly environmentId: EnvironmentIdType; readonly input: Input }) =>
849
+ live(environmentRpcKey(target));
850
+ }
851
+ const completed = Atom.family((key: string) =>
852
+ Atom.make(Option.none<A>()).pipe(
853
+ Atom.setIdleTTL(completedTtlMs),
854
+ Atom.withLabel(`${label(key)}:completed`),
855
+ ),
856
+ );
857
+ // Reading `completed` first drops the live subscription once its value is final.
858
+ const family = Atom.family((key: string) =>
859
+ Atom.make((get) => {
860
+ const done = get(completed(key));
861
+ if (Option.isSome(done)) return AsyncResult.success(done.value);
862
+ const result = get(live(key));
863
+ if (AsyncResult.isSuccess(result) && !result.waiting && completeWhen(result.value)) {
864
+ get.set(completed(key), Option.some(result.value));
865
+ }
866
+ return result;
867
+ }).pipe(Atom.setIdleTTL(idleTtlMs), Atom.withLabel(label(key))),
868
+ );
869
return (target: { readonly environmentId: EnvironmentIdType; readonly input: Input }) =>
870
family(environmentRpcKey(target));
871
}
@@ -929,6 +952,7 @@ export function createEnvironmentRpcSubscriptionAtomFamily<
952
readonly tag: TTag;
953
readonly idleTtlMs?: number;
954
readonly completeWhen?: (value: B) => boolean;
955
+ readonly completedTtlMs?: number;
956
readonly transform?: (
957
stream: Stream.Stream<
958
EnvironmentRpcStreamValue<TTag>,
@@ -945,6 +969,7 @@ export function createEnvironmentRpcSubscriptionAtomFamily<
969
return createEnvironmentSubscriptionAtomFamily(runtime, {
970
label: options.label,
971
...(options.completeWhen === undefined ? {} : { completeWhen: options.completeWhen }),
972
+ ...(options.completedTtlMs === undefined ? {} : { completedTtlMs: options.completedTtlMs }),
973
...(options.idleTtlMs === undefined ? {} : { idleTtlMs: options.idleTtlMs }),
974
subscribe: (input: EnvironmentRpcInput<TTag>) => {
975
const stream = subscribe(options.tag, input);