Skip to content

Commit 8172747

Browse files
fix(server): prevent enabling retired desktop Cua hosts
1 parent fe06f03 commit 8172747

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

‎apps/server/src/cua/CuaDriver.test.ts‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ const healthy: DesktopTelemetryReceiver.DesktopTelemetryReceiverHealth = {
309309
const desktopFixture = Effect.fnUntraced(function* (
310310
synchronous = false,
311311
healthChanges: Stream.Stream<DesktopTelemetryReceiver.DesktopTelemetryReceiverHealth> = Stream.empty,
312+
beforeHealth = Effect.void,
312313
) {
313314
const reports = yield* PubSub.unbounded<DesktopCuaDriverReport>();
314315
const requested = yield* Deferred.make<string>();
@@ -319,7 +320,7 @@ const desktopFixture = Effect.fnUntraced(function* (
319320
Effect.provide(
320321
DesktopTelemetryReceiver.layerTest({
321322
health: Effect.succeed(healthy),
322-
subscribeHealth: Effect.succeed({ latest: healthy, changes: healthChanges }),
323+
subscribeHealth: beforeHealth.pipe(Effect.as({ latest: healthy, changes: healthChanges })),
323324
cuaReports: Stream.fromPubSub(reports),
324325
requestCuaDriver: (requestId, enabled) =>
325326
Effect.gen(function* () {
@@ -392,6 +393,37 @@ it.effect.each([false, true])(
392393
}).pipe(Effect.provide([ServerSettings.layerTest(), NodeCrypto.layer]), Effect.scoped),
393394
);
394395

396+
it.effect("does not enable a desktop host retired while subscribing to health", () =>
397+
Effect.gen(function* () {
398+
const subscribing = yield* Deferred.make<void>();
399+
const releaseHealth = yield* Deferred.make<void>();
400+
const startFinished =
401+
yield* Deferred.make<Exit.Exit<CuaDriverMcpConfiguration, CuaDriver.CuaDriverError>>();
402+
const f = yield* desktopFixture(
403+
false,
404+
Stream.empty,
405+
Deferred.succeed(subscribing, undefined).pipe(Effect.andThen(Deferred.await(releaseHealth))),
406+
);
407+
const scope = yield* Scope.make();
408+
const { service } = yield* makeService(
409+
f.factory.pipe(
410+
Effect.map((host) => ({
411+
...host,
412+
start: host.start.pipe(Effect.onExit((exit) => Deferred.succeed(startFinished, exit))),
413+
})),
414+
),
415+
).pipe(Effect.provideService(Scope.Scope, scope));
416+
const acquiring = yield* service.acquire.pipe(Effect.forkChild);
417+
yield* Deferred.await(subscribing);
418+
yield* Scope.close(scope, Exit.void);
419+
expect(yield* Fiber.join(acquiring)).toEqual(Option.none());
420+
yield* Deferred.succeed(releaseHealth, undefined);
421+
const result = yield* Deferred.await(startFinished);
422+
expect(result).toEqual(Exit.fail(new CuaDriver.CuaDriverHostError({ operation: "desktop" })));
423+
expect(f.requests).toEqual([]);
424+
}).pipe(Effect.provide([ServerSettings.layerTest(), NodeCrypto.layer]), Effect.scoped),
425+
);
426+
395427
it.effect("preserves the desktop driver through degraded telemetry until the receiver stops", () =>
396428
Effect.gen(function* () {
397429
const streamQueue =

‎apps/server/src/cua/CuaDriver.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ export const makeDesktopHostFactory = Effect.fn("CuaDriver.makeDesktopHostFactor
330330
const ready = yield* Deferred.make<CuaDriverMcpConfiguration, CuaDriverError>();
331331
const stopped = yield* Deferred.make<void>();
332332
let requested = false;
333+
let retired = false;
333334
const unavailable = new CuaDriverHostError({ operation: "desktop" });
334335
const start = Effect.gen(function* () {
335336
// Start the subscription before writing: the desktop may reply synchronously.
@@ -366,13 +367,15 @@ export const makeDesktopHostFactory = Effect.fn("CuaDriver.makeDesktopHostFactor
366367
),
367368
Effect.forkIn(scope, { startImmediately: true }),
368369
);
370+
if (retired) return yield* unavailable;
369371
requested = true;
370372
yield* receiver
371373
.requestCuaDriver(requestId, true)
372374
.pipe(Effect.mapError((cause) => new CuaDriverHostError({ operation: "desktop", cause })));
373375
return yield* Deferred.await(ready);
374376
});
375377
const stop = Effect.gen(function* () {
378+
retired = true;
376379
if (!requested) return;
377380
yield* receiver
378381
.requestCuaDriver(requestId, false)

0 commit comments

Comments
 (0)