Skip to content

Commit ea3ecc3

Browse files
fix(clients): wait for a server terminal metadata snapshot
1 parent 843357e commit ea3ecc3

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import {
33
EnvironmentId,
44
type TerminalAttachInput,
55
type TerminalAttachStreamEvent,
6+
type TerminalMetadataStreamEvent,
7+
type TerminalSummary,
68
WS_METHODS,
79
} from "@t3tools/contracts";
810
import * as Cause from "effect/Cause";
@@ -388,6 +390,83 @@ describe("finite environment subscription lifecycle", () => {
388390
});
389391

390392
describe("environment query lifecycle", () => {
393+
it.effect(
394+
"terminal metadata waits for snapshots and retains known sessions across restarts",
395+
() =>
396+
Effect.gen(function* () {
397+
const harness = yield* makeEnvironmentQueryHarness(Effect.void);
398+
const started = yield* Queue.unbounded<void>();
399+
const events = yield* Queue.unbounded<TerminalMetadataStreamEvent>();
400+
const receivedCheckout = yield* Deferred.make<void>();
401+
const receivedEmpty = yield* Deferred.make<void>();
402+
const checkout: TerminalSummary = {
403+
threadId: "thread-1",
404+
terminalId: "terminal-1",
405+
cwd: "/repo/root/packages/app",
406+
worktreePath: null,
407+
status: "running",
408+
pid: 123,
409+
exitCode: null,
410+
exitSignal: null,
411+
hasRunningSubprocess: false,
412+
label: "Terminal 1",
413+
updatedAt: "2026-04-01T00:00:00.000Z",
414+
};
415+
const client = {
416+
[WS_METHODS.subscribeTerminalMetadata]: () =>
417+
Stream.fromEffect(Queue.offer(started, undefined)).pipe(
418+
Stream.drain,
419+
Stream.concat(Stream.fromQueue(events)),
420+
),
421+
} as unknown as RpcSession.RpcSession["client"];
422+
const session = { ...QUERY_RPC_SESSION, client };
423+
yield* SubscriptionRef.set(harness.supervisorSession, Option.some(session));
424+
const atoms = createTerminalEnvironmentAtoms(harness.runtime);
425+
const atom = atoms.metadata({
426+
environmentId: QUERY_ENVIRONMENT.environmentId,
427+
input: null,
428+
});
429+
const registry = AtomRegistry.make();
430+
const observed: Array<ReadonlyArray<TerminalSummary>> = [];
431+
const unsubscribe = registry.subscribe(
432+
atom,
433+
(result) => {
434+
if (!AsyncResult.isSuccess(result)) return;
435+
observed.push(result.value);
436+
if (result.value.length > 0) Deferred.doneUnsafe(receivedCheckout, Exit.void);
437+
else Deferred.doneUnsafe(receivedEmpty, Exit.void);
438+
},
439+
{ immediate: true },
440+
);
441+
yield* Effect.addFinalizer(() =>
442+
Effect.sync(() => {
443+
unsubscribe();
444+
registry.dispose();
445+
}),
446+
);
447+
448+
yield* Queue.take(started);
449+
expect(selectEnvironmentQueryData(registry.get(atom))).toBeNull();
450+
expect(observed).toEqual([]);
451+
452+
yield* Queue.offer(events, { type: "snapshot", terminals: [checkout] });
453+
yield* Deferred.await(receivedCheckout);
454+
expect(selectEnvironmentQueryData(registry.get(atom))).toEqual([checkout]);
455+
456+
yield* SubscriptionRef.set(harness.supervisorSession, Option.some({ ...session }));
457+
yield* Queue.take(started);
458+
expect(selectEnvironmentQueryData(registry.get(atom))).toEqual([checkout]);
459+
460+
yield* SubscriptionRef.update(harness.streamGeneration, (generation) => generation + 1);
461+
yield* Queue.take(started);
462+
expect(selectEnvironmentQueryData(registry.get(atom))).toEqual([checkout]);
463+
464+
yield* Queue.offer(events, { type: "snapshot", terminals: [] });
465+
yield* Deferred.await(receivedEmpty);
466+
expect(selectEnvironmentQueryData(registry.get(atom))).toEqual([]);
467+
}),
468+
);
469+
391470
it.effect("terminal attach resubscribes with resume only after the stream replied", () =>
392471
Effect.gen(function* () {
393472
const harness = yield* makeEnvironmentQueryHarness(Effect.void);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ export function createTerminalEnvironmentAtoms<R, E>(
7373
subscribe: (_input: null) =>
7474
subscribe(WS_METHODS.subscribeTerminalMetadata, {}).pipe(
7575
Stream.scan((): ReadonlyArray<TerminalSummary> => [], applyTerminalMetadataStreamEvent),
76+
// The scan seed is not an authoritative empty server snapshot.
77+
Stream.drop(1),
7678
),
7779
}),
7880
open: createEnvironmentRpcCommand(runtime, {

0 commit comments

Comments
 (0)