Skip to content

Commit 62de936

Browse files
committed
fix(desktop): reconnect automation after request timeouts
A timed-out computer request evicts its automation registration while the desktop and WebSocket can remain healthy. The client waited for a new session, leaving a running desktop permanently listed as offline. Allow selected RPC subscriptions to reopen after normal completion, and enable that recovery for desktop automation. Wait one second before registering again without replaying old actions. Session changes, failures, and cancellation retain their existing behavior. Focused client, consumer, and broker tests pass, along with client-runtime and web typechecks and targeted lint. Independently reviewed by GPT-6.1-Sol at Extra High. Implemented by GPT-6-Astra at Max in Codex via T3 Code.
1 parent 05acba0 commit 62de936

3 files changed

Lines changed: 118 additions & 3 deletions

File tree

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

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
DEFAULT_SERVER_SETTINGS,
33
EnvironmentAuthorizationError,
44
EnvironmentId,
5+
type PreviewAutomationStreamEvent,
56
type RelayClientInstallProgressEvent,
67
type ServerConfigStreamEvent,
78
type ServerLifecycleStreamEvent,
@@ -89,6 +90,104 @@ const makeHarness = Effect.fn("TestEnvironmentRpc.makeHarness")(function* () {
8990
});
9091

9192
describe("environment RPC", () => {
93+
it.effect.each([false, true])(
94+
"reopens a completed automation registration only when configured (%s)",
95+
(reconnect) =>
96+
Effect.gen(function* () {
97+
const first = yield* Queue.unbounded<PreviewAutomationStreamEvent, Cause.Done>();
98+
const second = yield* Queue.unbounded<PreviewAutomationStreamEvent, Cause.Done>();
99+
const started = yield* Queue.unbounded<number>();
100+
const received = yield* Queue.unbounded<PreviewAutomationStreamEvent>();
101+
const ended = yield* Deferred.make<void>();
102+
let subscriptions = 0;
103+
const client = {
104+
[WS_METHODS.previewAutomationConnect]: () =>
105+
Stream.unwrap(
106+
Effect.gen(function* () {
107+
subscriptions += 1;
108+
yield* Queue.offer(started, subscriptions);
109+
return subscriptions === 1
110+
? Stream.fromQueue(first).pipe(
111+
Stream.ensuring(Deferred.succeed(ended, undefined)),
112+
)
113+
: Stream.fromQueue(second);
114+
}),
115+
),
116+
} as unknown as WsRpcProtocolClient;
117+
const { activeSession, supervisor, retryCount } = yield* makeHarness();
118+
yield* SubscriptionRef.set(activeSession, Option.some(session(client)));
119+
const fiber = yield* subscribe(
120+
WS_METHODS.previewAutomationConnect,
121+
{ clientId: "desktop", environmentId: TARGET.environmentId },
122+
reconnect ? { resubscribeOnCompletionAfter: "1 second" } : undefined,
123+
).pipe(
124+
Stream.runForEach((event) => Queue.offer(received, event)),
125+
Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
126+
Effect.forkChild,
127+
);
128+
expect(yield* Queue.take(started)).toBe(1);
129+
yield* Queue.offer(first, { type: "connected", connectionId: "first" });
130+
expect(yield* Queue.take(received)).toEqual({ type: "connected", connectionId: "first" });
131+
yield* Queue.end(first);
132+
yield* Deferred.await(ended);
133+
yield* TestClock.adjust("999 millis");
134+
expect(Option.isNone(yield* Queue.poll(started))).toBe(true);
135+
yield* TestClock.adjust("1 millis");
136+
if (reconnect) {
137+
expect(yield* Queue.take(started)).toBe(2);
138+
yield* Queue.offer(second, { type: "connected", connectionId: "second" });
139+
expect(yield* Queue.take(received)).toEqual({
140+
type: "connected",
141+
connectionId: "second",
142+
});
143+
}
144+
expect(subscriptions).toBe(reconnect ? 2 : 1);
145+
expect(yield* Ref.get(retryCount)).toBe(0);
146+
yield* Fiber.interrupt(fiber);
147+
yield* TestClock.adjust("5 seconds");
148+
expect(Option.isNone(yield* Queue.poll(started))).toBe(true);
149+
}),
150+
);
151+
152+
it.effect("cancels a completion retry when the supervisor replaces its session", () =>
153+
Effect.gen(function* () {
154+
const started = yield* Queue.unbounded<string>();
155+
const ended = yield* Deferred.make<void>();
156+
const firstClient = {
157+
[WS_METHODS.subscribeTerminalEvents]: () =>
158+
Stream.fromEffect(Queue.offer(started, "first")).pipe(
159+
Stream.drain,
160+
Stream.ensuring(Deferred.succeed(ended, undefined)),
161+
),
162+
} as unknown as WsRpcProtocolClient;
163+
const secondClient = {
164+
[WS_METHODS.subscribeTerminalEvents]: () =>
165+
Stream.fromEffect(Queue.offer(started, "second")).pipe(
166+
Stream.drain,
167+
Stream.concat(Stream.never),
168+
),
169+
} as unknown as WsRpcProtocolClient;
170+
const { activeSession, supervisor } = yield* makeHarness();
171+
yield* SubscriptionRef.set(activeSession, Option.some(session(firstClient)));
172+
const fiber = yield* subscribe(
173+
WS_METHODS.subscribeTerminalEvents,
174+
{},
175+
{ resubscribeOnCompletionAfter: "1 second" },
176+
).pipe(
177+
Stream.runDrain,
178+
Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
179+
Effect.forkChild,
180+
);
181+
expect(yield* Queue.take(started)).toBe("first");
182+
yield* Deferred.await(ended);
183+
yield* SubscriptionRef.set(activeSession, Option.some(session(secondClient)));
184+
expect(yield* Queue.take(started)).toBe("second");
185+
yield* TestClock.adjust("5 seconds");
186+
expect(Option.isNone(yield* Queue.poll(started))).toBe(true);
187+
yield* Fiber.interrupt(fiber);
188+
}),
189+
);
190+
92191
it.effect("reuses the session config stream instead of opening a duplicate subscription", () =>
93192
Effect.gen(function* () {
94193
const event: ServerConfigStreamEvent = {

‎packages/client-runtime/src/rpc/client.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import * as Context from "effect/Context";
1212
import * as Duration from "effect/Duration";
1313
import * as Effect from "effect/Effect";
1414
import * as Option from "effect/Option";
15+
import * as Schedule from "effect/Schedule";
1516
import * as Schema from "effect/Schema";
1617
import * as Stream from "effect/Stream";
1718
import * as SubscriptionRef from "effect/SubscriptionRef";
@@ -247,6 +248,8 @@ interface SubscriptionOptions<TTag extends EnvironmentSubscriptionRpcTag> {
247248
cause: Cause.Cause<EnvironmentRpcStreamFailure<TTag>>,
248249
) => Effect.Effect<void, never, never>;
249250
readonly retryExpectedFailureAfter?: Duration.Input;
251+
/** Reopen a successfully ended registration stream without replacing its healthy session. */
252+
readonly resubscribeOnCompletionAfter?: Duration.Input;
250253
readonly resubscribe?: Stream.Stream<unknown, never, never>;
251254
}
252255

@@ -313,6 +316,9 @@ function subscribeDynamicMapped<TTag extends EnvironmentSubscriptionRpcTag, A>(
313316
return stream.pipe(Stream.ensuring(completeObservation));
314317
}),
315318
).pipe(
319+
options?.resubscribeOnCompletionAfter === undefined
320+
? (stream) => stream
321+
: Stream.repeat(Schedule.spaced(options.resubscribeOnCompletionAfter)),
316322
Stream.tapCause((cause) =>
317323
options?.onDefect !== undefined &&
318324
cause.reasons.some(

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
import { WS_METHODS, type UserDesktopHumanInvokeInput } from "@t3tools/contracts";
1+
import {
2+
WS_METHODS,
3+
type PreviewAutomationHost,
4+
type UserDesktopHumanInvokeInput,
5+
} from "@t3tools/contracts";
26
import * as Stream from "effect/Stream";
37
import { Atom } from "effect/reactivity";
48

59
import type { EnvironmentRegistry } from "../connection/registry.ts";
10+
import { subscribe } from "../rpc/client.ts";
611
import {
712
createAtomCommandScheduler,
813
createEnvironmentRpcCommand,
914
createEnvironmentRpcQueryAtomFamily,
1015
createEnvironmentRpcSubscriptionAtomFamily,
16+
createEnvironmentSubscriptionAtomFamily,
1117
} from "./runtime.ts";
1218

1319
export const previewAutomationHostFocusConcurrencyKey = (value: {
@@ -70,9 +76,13 @@ export function createPreviewEnvironmentAtoms<R, E>(
7076
// unmounted projects stop contributing probe candidates on the server.
7177
idleTtlMs: 0,
7278
}),
73-
automationRequests: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
79+
automationRequests: createEnvironmentSubscriptionAtomFamily(runtime, {
7480
label: "environment-data:computer:automation-requests",
75-
tag: WS_METHODS.previewAutomationConnect,
81+
// Timeout eviction ends this registration, not the underlying WebSocket.
82+
subscribe: (input: PreviewAutomationHost) =>
83+
subscribe(WS_METHODS.previewAutomationConnect, input, {
84+
resubscribeOnCompletionAfter: "1 second",
85+
}),
7686
// Closing the host must close its stream and cancel pending desktop work.
7787
idleTtlMs: 0,
7888
}),

0 commit comments

Comments
 (0)