diff --git a/apps/desktop/package.json b/apps/desktop/package.json index d91a9ee5e4cfe..9673486272042 100644 --- a/apps/desktop/package.json +++ b/apps/desktop/package.json @@ -21,6 +21,8 @@ "@t3tools/shared": "workspace:*", "@t3tools/ssh": "workspace:*", "@t3tools/tailscale": "workspace:*", + "@trycua/cua-driver": "catalog:", + "@ubjs/node": "catalog:", "dbus-next": "0.10.2", "effect": "catalog:", "electron": "44.10.1", diff --git a/apps/desktop/src/app/DesktopApp.test.ts b/apps/desktop/src/app/DesktopApp.test.ts index 9cc3a187e83f3..2b30a61a39237 100644 --- a/apps/desktop/src/app/DesktopApp.test.ts +++ b/apps/desktop/src/app/DesktopApp.test.ts @@ -58,6 +58,7 @@ import * as DesktopCliCommand from "./DesktopCliCommand.ts"; import * as DesktopCliShim from "./DesktopCliShim.ts"; import * as DesktopClientSettings from "../settings/DesktopClientSettings.ts"; import * as DesktopConnectionCatalogStore from "./DesktopConnectionCatalogStore.ts"; +import * as DesktopCuaDriver from "../cua/DesktopCuaDriver.ts"; import * as DesktopIpc from "../ipc/DesktopIpc.ts"; import * as DesktopLocalEnvironmentAuth from "../backend/DesktopLocalEnvironmentAuth.ts"; import * as DesktopManagedPolicy from "./DesktopManagedPolicy.ts"; @@ -204,6 +205,7 @@ const runStubbedDesktop = (stub: StubbedDesktop) => Layer.mock(DesktopCliCommand.DesktopCliCommand, {}), Layer.mock(DesktopClientSettings.DesktopClientSettings, {}), Layer.mock(DesktopConnectionCatalogStore.DesktopConnectionCatalogStore, {}), + Layer.mock(DesktopCuaDriver.DesktopCuaDriver, { listen: Effect.void }), Layer.mock(DesktopIpc.DesktopIpc, {}), Layer.mock(DesktopLocalEnvironmentAuth.DesktopLocalEnvironmentAuth, { getBearerToken: stub diff --git a/apps/desktop/src/app/DesktopApp.ts b/apps/desktop/src/app/DesktopApp.ts index 34476ee7855b5..b7ea859e8f9e4 100644 --- a/apps/desktop/src/app/DesktopApp.ts +++ b/apps/desktop/src/app/DesktopApp.ts @@ -36,6 +36,7 @@ import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts"; import * as DesktopShellEnvironment from "../shell/DesktopShellEnvironment.ts"; import * as DesktopStartupRecovery from "./DesktopStartupRecovery.ts"; import * as DesktopState from "./DesktopState.ts"; +import * as DesktopCuaDriver from "../cua/DesktopCuaDriver.ts"; import * as DesktopRemoteUpdates from "../updates/DesktopRemoteUpdates.ts"; import * as DesktopUpdates from "../updates/DesktopUpdates.ts"; import * as DesktopSnapShot from "../snapShot/DesktopSnapShot.ts"; @@ -340,6 +341,7 @@ const startup = Effect.gen(function* () { const preReadyElectronOptions = yield* DesktopPreReadyPlatform.DesktopPreReadyElectronOptions; const safeStorage = yield* ElectronSafeStorage.ElectronSafeStorage; const updates = yield* DesktopUpdates.DesktopUpdates; + const cuaDriver = yield* DesktopCuaDriver.DesktopCuaDriver; const environment = yield* DesktopEnvironment.DesktopEnvironment; const previewPasskeys = yield* PreviewPasskeys.PreviewPasskeys; @@ -402,6 +404,7 @@ const startup = Effect.gen(function* () { yield* updates.configure; yield* DesktopRemoteUpdates.listen; yield* DesktopStartupRecovery.listen; + yield* cuaDriver.listen; const backendStarted = yield* startLocalBackend.pipe( Effect.catchCause((cause) => fatalStartupCause("bootstrap", cause)), ); diff --git a/apps/desktop/src/backend/DesktopBackendManager.test.ts b/apps/desktop/src/backend/DesktopBackendManager.test.ts index ff5b7b8c08e4b..5e2a918aaf025 100644 --- a/apps/desktop/src/backend/DesktopBackendManager.test.ts +++ b/apps/desktop/src/backend/DesktopBackendManager.test.ts @@ -170,6 +170,9 @@ function makeTestInstance(input: MakeInstanceInput) { latest: Effect.succeedNone, changes: Stream.empty, encoded: input.desktopTelemetryStream ?? Stream.empty, + encodedForSource: () => input.desktopTelemetryStream ?? Stream.empty, + cuaRequests: Stream.empty, + publishCuaReport: () => Effect.void, handleControlForSource: () => Effect.void, removeControlSource: () => Effect.void, publishUpdateReport: () => Effect.void, diff --git a/apps/desktop/src/backend/DesktopBackendManager.ts b/apps/desktop/src/backend/DesktopBackendManager.ts index 8b4f154b5d4de..fd22e8b2ff695 100644 --- a/apps/desktop/src/backend/DesktopBackendManager.ts +++ b/apps/desktop/src/backend/DesktopBackendManager.ts @@ -943,7 +943,7 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* ( const program = runBackendProcess({ ...config.value, - desktopTelemetryStream: desktopTelemetryPublisher.encoded, + desktopTelemetryStream: desktopTelemetryPublisher.encodedForSource(spec.id), desktopBrowserStream: desktopBrowserHost.events, onDesktopBrowserCommand: desktopBrowserHost.handleCommandLine, onDesktopTelemetryControl: (message) => diff --git a/apps/desktop/src/backend/DesktopBackendPool.test.ts b/apps/desktop/src/backend/DesktopBackendPool.test.ts index 005a1cde1224a..6f102d07f0c1d 100644 --- a/apps/desktop/src/backend/DesktopBackendPool.test.ts +++ b/apps/desktop/src/backend/DesktopBackendPool.test.ts @@ -175,6 +175,9 @@ function layerPool( latest: Effect.succeedNone, changes: Stream.empty, encoded: Stream.empty, + encodedForSource: () => Stream.empty, + cuaRequests: Stream.empty, + publishCuaReport: () => Effect.void, handleControlForSource: () => Effect.void, removeControlSource: () => Effect.void, publishUpdateReport: () => Effect.void, diff --git a/apps/desktop/src/cua/DesktopCuaDriver.test.ts b/apps/desktop/src/cua/DesktopCuaDriver.test.ts new file mode 100644 index 0000000000000..adb9f1ab77733 --- /dev/null +++ b/apps/desktop/src/cua/DesktopCuaDriver.test.ts @@ -0,0 +1,310 @@ +import * as NodePath from "@effect/platform-node/NodePath"; +import { assert, describe, it } from "@effect/vitest"; +import { + DesktopHostTelemetryMessage, + type DesktopCuaDriverReport, + type DesktopCuaDriverRequest, +} from "@t3tools/contracts"; +import * as Fiber from "effect/Fiber"; +import * as Exit from "effect/Exit"; +import * as Scope from "effect/Scope"; +import * as TestClock from "effect/testing/TestClock"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as Queue from "effect/Queue"; +import * as Schema from "effect/Schema"; +import * as Stream from "effect/Stream"; + +import * as DesktopConfig from "../app/DesktopConfig.ts"; +import * as DesktopEnvironment from "../app/DesktopEnvironment.ts"; +import { DesktopTelemetryPublisher } from "../telemetry/DesktopTelemetryPublisher.ts"; +import { make, type DesktopCuaDriverDependencies } from "./DesktopCuaDriver.ts"; + +const encodeMessage = Schema.encodeEffect(Schema.fromJsonString(DesktopHostTelemetryMessage)); +const decodeMessage = Schema.decodeEffect(Schema.fromJsonString(DesktopHostTelemetryMessage)); + +const descriptor = { + command: "/native/driver", + args: ["mcp", "--session", "opaque"], + environment: [{ name: "CUA_SESSION", value: "test-value" }], +}; +const environmentLayer = (platform: NodeJS.Platform = "linux") => + DesktopEnvironment.layer({ + dirname: "/app", + homeDirectory: "/home/test", + platform, + processArch: "x64", + appVersion: "1.0.0", + appPath: "/app", + isPackaged: true, + resourcesPath: "/resources", + runningUnderArm64Translation: false, + }).pipe(Layer.provide(Layer.mergeAll(NodePath.layerPosix, DesktopConfig.layerTest({})))); + +const fixture = Effect.fn(function* ( + failStart = false, + denyPermissions = false, + control: { holdStart?: boolean; holdStop?: boolean } = {}, +) { + const requests = yield* Queue.unbounded(); + const reports = yield* Queue.unbounded(); + const exits: Array<() => void> = []; + const starting = yield* Queue.unbounded(); + const stopping = yield* Queue.unbounded(); + const destroyed = yield* Queue.unbounded(); + const startGate = Promise.withResolvers(); + const stopGate = Promise.withResolvers(); + let starts = 0; + let stops = 0; + let destroys = 0; + const dependencies: DesktopCuaDriverDependencies = { + loadElectron: async () => ({ + requestMacOSPermissions: () => ({ accessibility: false, screenRecording: false }), + hasRequiredMacOSPermissions: () => false, + }), + loadEmbedded: async () => ({ + EmbeddedCuaDriverHost: class { + async start() { + starts++; + Queue.offerUnsafe(starting, undefined); + if (control.holdStart) await startGate.promise; + if (failStart) throw new Error("private host details"); + return { + socketPath: "/socket", + pid: 100, + generation: String(starts), + driverVersion: "0.24", + contractVersion: "1", + mcpProtocolVersion: "1", + mcp: descriptor, + }; + } + async stop() { + stops++; + Queue.offerUnsafe(stopping, undefined); + if (control.holdStop) await stopGate.promise; + } + uniffiDestroy() { + destroys++; + Queue.offerUnsafe(destroyed, undefined); + } + waitForExit(generation: string, options?: { signal: AbortSignal }) { + return new Promise<{ generation: string; success: boolean }>((resolve) => { + exits.push(() => resolve({ generation, success: false })); + options?.signal.addEventListener( + "abort", + () => resolve({ generation, success: false }), + { once: true }, + ); + }); + } + }, + }), + }; + const publisher = DesktopTelemetryPublisher.of({ + latest: Effect.succeedNone, + changes: Stream.empty, + encoded: Stream.empty, + encodedForSource: () => Stream.empty, + handleControlForSource: () => Effect.void, + removeControlSource: () => Effect.void, + updateRequests: Stream.empty, + updateCommits: Stream.empty, + updateCancellations: Stream.empty, + publishUpdateReport: () => Effect.void, + cuaRequests: Stream.fromQueue(requests), + publishCuaReport: (report) => Queue.offer(reports, report).pipe(Effect.asVoid), + }); + const driver = yield* make(dependencies).pipe( + Effect.provideService(DesktopTelemetryPublisher, publisher), + Effect.provide(environmentLayer(denyPermissions ? "darwin" : "linux")), + ); + yield* driver.listen; + return { + restart: driver.restart, + request: (requestId: string, enabled: boolean) => + Queue.offer(requests, { version: 1, type: "cuaDriverRequest", requestId, enabled }), + next: Queue.take(reports), + starting: Queue.take(starting), + stopping: Queue.take(stopping), + destroyed: Queue.take(destroyed), + releaseStart: Effect.sync(() => startGate.resolve()), + releaseStop: Effect.sync(() => stopGate.resolve()), + exits, + counts: () => ({ starts, stops, destroys }), + }; +}); + +describe("DesktopCuaDriver", () => { + it.effect("returns the exact descriptor, reuses the host, and stops it once", () => + Effect.scoped( + Effect.gen(function* () { + const test = yield* fixture(); + yield* test.request("start", true); + const ready = yield* test.next; + const json = yield* encodeMessage(ready); + assert.deepEqual(yield* decodeMessage(json), { + version: 1, + type: "cuaDriverReport", + requestId: "start", + status: "ready", + mcp: { ...descriptor, socketPath: "/socket" }, + }); + yield* test.request("same", true); + assert.equal((yield* test.next).requestId, "same"); + yield* test.request("stop", false); + assert.equal((yield* test.next).status, "stopped"); + yield* test.request("stop-again", false); + yield* test.next; + assert.deepEqual(test.counts(), { starts: 1, stops: 1, destroys: 1 }); + }), + ), + ); + + it.effect("restart stops the active host, reports it, and lets a new session start one", () => + Effect.scoped( + Effect.gen(function* () { + const test = yield* fixture(); + yield* test.request("start", true); + assert.equal((yield* test.next).status, "ready"); + yield* test.restart; + const report = yield* test.next; + assert.deepEqual(report, { + version: 1, + type: "cuaDriverReport", + requestId: "start", + status: "stopped", + }); + yield* test.restart; + yield* test.request("again", true); + assert.equal((yield* test.next).status, "ready"); + assert.deepEqual(test.counts(), { starts: 2, stops: 1, destroys: 1 }); + }), + ), + ); + + it.effect("reports a crash against the active request and allows a new host", () => + Effect.scoped( + Effect.gen(function* () { + const test = yield* fixture(); + yield* test.request("first", true); + yield* test.next; + yield* test.request("current", true); + yield* test.next; + yield* Effect.sync(() => test.exits[0]!()); + const report = yield* test.next; + assert.equal(report.status, "unavailable"); + assert.equal(report.requestId, "current"); + yield* test.request("restart", true); + assert.equal((yield* test.next).status, "ready"); + assert.deepEqual(test.counts(), { starts: 2, stops: 1, destroys: 1 }); + }), + ), + ); + + it.effect("cleans up failed starts without exposing native error details", () => + Effect.scoped( + Effect.gen(function* () { + const test = yield* fixture(true); + yield* test.request("failed", true); + const report = yield* test.next; + assert.equal(report.status, "unavailable"); + if (report.status !== "ready") + assert.equal(report.message?.includes("private host details"), false); + assert.deepEqual(test.counts(), { starts: 1, stops: 1, destroys: 1 }); + }), + ), + ); + + it.effect("reports missing permissions without constructing a native host", () => + Effect.scoped( + Effect.gen(function* () { + const test = yield* fixture(false, true); + yield* test.request("permissions", true); + const report = yield* test.next; + assert.equal(report.status, "unavailable"); + if (report.status !== "ready") assert.include(report.message ?? "", "Accessibility"); + assert.deepEqual(test.counts(), { starts: 0, stops: 0, destroys: 0 }); + }), + ), + ); + + it.effect( + "times out a hanging start and cleans up its late result without publishing ready", + () => + Effect.scoped( + Effect.gen(function* () { + const test = yield* fixture(false, false, { holdStart: true }); + yield* test.request("hanging", true); + yield* test.starting; + yield* TestClock.adjust("23 seconds"); + assert.equal((yield* test.next).status, "unavailable"); + yield* test.request("disable", false); + yield* TestClock.adjust("3 seconds"); + assert.equal((yield* test.next).status, "unavailable"); + assert.deepEqual(test.counts(), { starts: 1, stops: 0, destroys: 0 }); + yield* test.releaseStart; + yield* test.destroyed; + yield* test.request("disabled", false); + const report = yield* test.next; + assert.equal(report.requestId, "disabled"); + assert.equal(report.status, "stopped"); + assert.deepEqual(test.counts(), { starts: 1, stops: 1, destroys: 1 }); + }), + ), + ); + + it.effect("bounds a hanging stop and prevents another native host until cleanup settles", () => + Effect.scoped( + Effect.gen(function* () { + const test = yield* fixture(false, false, { holdStop: true }); + yield* test.request("start", true); + yield* test.next; + yield* test.request("stop", false); + yield* test.stopping; + yield* TestClock.adjust("3 seconds"); + assert.equal((yield* test.next).status, "unavailable"); + yield* test.request("too-soon", true); + assert.equal((yield* test.next).status, "unavailable"); + assert.deepEqual(test.counts(), { starts: 1, stops: 1, destroys: 0 }); + yield* test.releaseStop; + yield* test.destroyed; + yield* test.request("restart", true); + assert.equal((yield* test.next).status, "ready"); + assert.deepEqual(test.counts(), { starts: 2, stops: 1, destroys: 1 }); + }), + ), + ); + + it.effect("closes its scope while start hangs and cleans up after late completion", () => + Effect.gen(function* () { + const scope = yield* Scope.make(); + const test = yield* fixture(false, false, { holdStart: true }).pipe( + Effect.provideService(Scope.Scope, scope), + ); + yield* test.request("start", true); + yield* test.starting; + const closing = yield* Scope.close(scope, Exit.void).pipe(Effect.forkChild); + yield* TestClock.adjust("3 seconds"); + yield* Fiber.join(closing); + assert.deepEqual(test.counts(), { starts: 1, stops: 0, destroys: 0 }); + yield* test.releaseStart; + yield* test.destroyed; + assert.deepEqual(test.counts(), { starts: 1, stops: 1, destroys: 1 }); + }), + ); + + it.effect("cleans up the active host when its scope closes", () => + Effect.gen(function* () { + const test = yield* Effect.scoped( + Effect.gen(function* () { + const test = yield* fixture(); + yield* test.request("start", true); + yield* test.next; + return test; + }), + ); + assert.deepEqual(test.counts(), { starts: 1, stops: 1, destroys: 1 }); + }), + ); +}); diff --git a/apps/desktop/src/cua/DesktopCuaDriver.ts b/apps/desktop/src/cua/DesktopCuaDriver.ts new file mode 100644 index 0000000000000..479e91219f4c2 --- /dev/null +++ b/apps/desktop/src/cua/DesktopCuaDriver.ts @@ -0,0 +1,344 @@ +import { CuaDriverMcpConfiguration, type DesktopCuaDriverRequest } from "@t3tools/contracts"; +import type { EmbeddedCuaDriverHost, EmbeddedDriverConnection } from "@trycua/cua-driver/embedded"; +import * as Cause from "effect/Cause"; +import * as Exit from "effect/Exit"; +import * as Context from "effect/Context"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; +import * as Schema from "effect/Schema"; +import * as Scope from "effect/Scope"; +import * as Semaphore from "effect/Semaphore"; +import * as Stream from "effect/Stream"; + +import * as DesktopEnvironment from "../app/DesktopEnvironment.ts"; +import * as DesktopTelemetryPublisher from "../telemetry/DesktopTelemetryPublisher.ts"; + +export interface DesktopCuaDriverDependencies { + readonly loadEmbedded: () => Promise<{ + readonly EmbeddedCuaDriverHost: new ( + path: string, + hostBundleId: string, + ) => Pick; + }>; + readonly loadElectron: () => Promise< + Pick< + typeof import("@trycua/cua-driver/electron"), + "requestMacOSPermissions" | "hasRequiredMacOSPermissions" + > + >; +} +const decodeMcpConfiguration = Schema.decodeEffect(CuaDriverMcpConfiguration); + +/** A step of hosting the driver failed; the request is then reported unavailable. */ +class DesktopCuaDriverHostError extends Schema.TaggedError()( + "DesktopCuaDriverHostError", + { step: Schema.String, cause: Schema.Defect() }, +) {} +const hostStep = (step: string) => (cause: unknown) => + new DesktopCuaDriverHostError({ step, cause }); + +// The SDK loads its native libraries from the archive's .unpacked sibling +// through our @ubjs/node patch, so a packaged app imports it in place. +const defaultDependencies: DesktopCuaDriverDependencies = { + loadEmbedded: () => import("@trycua/cua-driver/embedded"), + loadElectron: () => import("@trycua/cua-driver/electron"), +}; + +const resolveEmbeddedDriverPath = ( + environment: NodeJS.ProcessEnv, + desktop: Pick< + DesktopEnvironment.DesktopEnvironment["Service"], + "isPackaged" | "platform" | "resourcesPath" | "path" + >, +): Option.Option => + desktop.isPackaged + ? Option.some( + desktop.platform === "darwin" + ? desktop.path.join(desktop.resourcesPath, "cua-driver") + : desktop.path.join( + desktop.resourcesPath, + "cua-driver", + desktop.platform === "win32" ? "cua-driver.exe" : "cua-driver", + ), + ) + : Option.fromNullishOr(environment.T3CODE_CUA_DRIVER_PATH).pipe( + Option.map((value) => value.trim()), + Option.filter((value) => value.length > 0), + ); + +interface OwnedHost { + readonly host: Pick; + readonly abort: AbortController; + startPromise?: Promise; + monitorPromise?: Promise; + releasePromise?: Promise; +} + +interface HostedDriver { + readonly owned: OwnedHost; + readonly connection: EmbeddedDriverConnection; + requestId: string; +} + +export class DesktopCuaDriver extends Context.Service< + DesktopCuaDriver, + { + readonly listen: Effect.Effect; + /** + * Stops the running host so the next agent session starts a fresh one. + * macOS caches TCC decisions per process, so a driver started before the + * user granted access keeps seeing the old answer until it restarts. + */ + readonly restart: Effect.Effect; + } +>()("@t3tools/desktop/cua/DesktopCuaDriver") {} + +export const make = Effect.fn("desktop.cuaDriver.make")(function* ( + dependencyOverrides?: DesktopCuaDriverDependencies, +) { + const environment = yield* DesktopEnvironment.DesktopEnvironment; + const dependencies = dependencyOverrides ?? defaultDependencies; + const publisher = yield* DesktopTelemetryPublisher.DesktopTelemetryPublisher; + const scope = yield* Scope.Scope; + const mutex = yield* Semaphore.make(1); + let active: HostedDriver | undefined; + const hosts = new Set(); + + const cleanup = (owned: OwnedHost) => + Effect.suspend(() => { + if (active?.owned === owned) active = undefined; + if (!owned.releasePromise) { + owned.abort.abort(); + // A native call may ignore cancellation. Keep its host alive until it + // settles, then stop it and destroy it exactly once, even after timeout. + owned.releasePromise = (async () => { + try { + await owned.startPromise; + } catch { + /* Failed starts still own a host. */ + } + try { + await owned.host.stop(); + } catch { + /* Destruction must still run. */ + } + try { + await owned.monitorPromise; + } catch { + /* Cancellation ends the monitor. */ + } + try { + owned.host.uniffiDestroy(); + } catch { + /* Cleanup cannot crash Electron. */ + } + hosts.delete(owned); + })(); + } + return Effect.tryPromise({ + try: () => owned.releasePromise!, + catch: hostStep("release"), + }).pipe(Effect.interruptible, Effect.timeout("3 seconds"), Effect.ignoreCause); + }); + const stop = Effect.suspend(() => { + const current = [...hosts]; + active = undefined; + return Effect.forEach(current, cleanup, { concurrency: "unbounded", discard: true }); + }); + const retireNewHosts = Effect.suspend(() => { + active = undefined; + return Effect.forEach( + [...hosts].filter((owned) => !owned.releasePromise), + cleanup, + { concurrency: "unbounded", discard: true }, + ); + }); + yield* Effect.addFinalizer(() => retireNewHosts); + + const unavailable = (requestId: string, message: string) => + publisher.publishCuaReport({ + version: 1, + type: "cuaDriverReport", + requestId, + status: "unavailable", + message, + }); + const handle = Effect.fn("desktop.cuaDriver.handle")(function* ( + request: DesktopCuaDriverRequest, + ) { + if (!request.enabled) { + yield* stop; + yield* hosts.size === 0 + ? publisher.publishCuaReport({ + version: 1, + type: "cuaDriverReport", + requestId: request.requestId, + status: "stopped", + }) + : unavailable( + request.requestId, + "Cua Driver is still stopping. Wait for it to exit before starting another session.", + ); + return; + } + if (active) { + active.requestId = request.requestId; + yield* publisher.publishCuaReport({ + version: 1, + type: "cuaDriverReport", + requestId: request.requestId, + status: "ready", + mcp: { ...active.connection.mcp, socketPath: active.connection.socketPath }, + }); + return; + } + if (hosts.size > 0) { + yield* unavailable( + request.requestId, + "The previous Cua Driver is still stopping. Try a new agent session after it exits.", + ); + return; + } + const binaryPath = resolveEmbeddedDriverPath(process.env, environment); + if (Option.isNone(binaryPath)) { + yield* unavailable( + request.requestId, + "Cua Driver is not configured for this desktop installation.", + ); + return; + } + if (environment.platform === "darwin") { + const helpers = yield* Effect.tryPromise({ + try: dependencies.loadElectron, + catch: hostStep("load-electron"), + }); + const allowed = yield* Effect.try({ + try: () => helpers.hasRequiredMacOSPermissions(helpers.requestMacOSPermissions()), + catch: hostStep("macos-permissions"), + }); + if (!allowed) { + yield* unavailable( + request.requestId, + "T3 Code needs Accessibility and Screen Recording access before Cua Driver can start.", + ); + return; + } + } + const module = yield* Effect.tryPromise({ + try: dependencies.loadEmbedded, + catch: hostStep("load-sdk"), + }); + const owned = yield* Effect.try({ + try: () => { + const owned: OwnedHost = { + host: new module.EmbeddedCuaDriverHost(binaryPath.value, environment.appUserModelId), + abort: new AbortController(), + }; + hosts.add(owned); + return owned; + }, + catch: hostStep("create-host"), + }); + yield* Effect.gen(function* () { + const connection = yield* Effect.tryPromise({ + try: (signal) => { + signal.addEventListener("abort", () => owned.abort.abort(), { once: true }); + owned.startPromise = owned.host.start({ signal: owned.abort.signal }); + return owned.startPromise; + }, + catch: hostStep("start"), + }); + const mcp = yield* decodeMcpConfiguration({ + ...connection.mcp, + socketPath: connection.socketPath, + }); + yield* Effect.try({ + try: () => { + owned.monitorPromise = owned.host.waitForExit(connection.generation, { + signal: owned.abort.signal, + }); + void owned.monitorPromise.catch(() => undefined); + }, + catch: hostStep("monitor"), + }); + const hosted: HostedDriver = { owned, connection, requestId: request.requestId }; + active = hosted; + yield* publisher.publishCuaReport({ + version: 1, + type: "cuaDriverReport", + requestId: request.requestId, + status: "ready", + mcp, + }); + yield* Effect.tryPromise({ + try: () => owned.monitorPromise!, + catch: hostStep("monitor"), + }).pipe( + Effect.ignore, + Effect.andThen( + mutex.withPermits(1)( + Effect.suspend(() => { + if (active !== hosted) return Effect.void; + active = undefined; + return cleanup(owned).pipe( + Effect.andThen( + unavailable( + hosted.requestId, + "Cua Driver exited. A new agent session can try starting it again.", + ), + ), + ); + }), + ), + ), + Effect.forkIn(scope), + ); + }).pipe(Effect.onExit((exit) => (Exit.isFailure(exit) ? cleanup(owned) : Effect.void))); + }); + const restart = mutex.withPermits(1)( + Effect.suspend(() => { + const requestId = active?.requestId; + return stop.pipe( + Effect.andThen( + requestId === undefined + ? Effect.void + : publisher.publishCuaReport({ + version: 1, + type: "cuaDriverReport", + requestId, + status: "stopped", + }), + ), + ); + }), + ); + return DesktopCuaDriver.of({ + restart, + listen: publisher.cuaRequests.pipe( + Stream.runForEach((request) => + mutex.withPermits(1)( + handle(request).pipe( + Effect.timeout("20 seconds"), + Effect.catchCauseIf( + (cause) => !Cause.hasInterrupts(cause), + () => + retireNewHosts.pipe( + Effect.andThen( + unavailable( + request.requestId, + "Cua Driver could not start. Check its installation and operating system permissions.", + ), + ), + ), + ), + ), + ), + ), + Effect.forkScoped, + Effect.asVoid, + ), + }); +}); + +export const layer = Layer.effect(DesktopCuaDriver, make()); diff --git a/apps/desktop/src/ipc/DesktopIpcHandlers.ts b/apps/desktop/src/ipc/DesktopIpcHandlers.ts index fe65edd0ee1e9..c63d44cf5366d 100644 --- a/apps/desktop/src/ipc/DesktopIpcHandlers.ts +++ b/apps/desktop/src/ipc/DesktopIpcHandlers.ts @@ -73,6 +73,7 @@ import { setSnapShotAnimationDestination, setSnapShotShortcutSuppressed, } from "./methods/snapShot.ts"; +import { restartCuaDriver } from "./methods/cua.ts"; import * as PreviewIpc from "./methods/preview.ts"; import * as AppActivationIpc from "./methods/appActivation.ts"; import * as WebLinksIpc from "./methods/webLinks.ts"; @@ -118,6 +119,7 @@ export const installDesktopIpcHandlers = Effect.fn("desktop.ipc.installHandlers" yield* ipc.handle(getConnectionCatalog); yield* ipc.handle(getSnapShotState); yield* ipc.handle(setupSnapShot); + yield* ipc.handle(restartCuaDriver); yield* ipc.handle(previewSnapShotConfig); yield* ipc.handle(applySnapShotConfig); yield* ipc.handle(requestSnapShotPermissions); diff --git a/apps/desktop/src/ipc/channels.ts b/apps/desktop/src/ipc/channels.ts index d8652bd199713..54ad372640ac2 100644 --- a/apps/desktop/src/ipc/channels.ts +++ b/apps/desktop/src/ipc/channels.ts @@ -48,6 +48,7 @@ export const GET_LOCAL_ENVIRONMENT_BEARER_TOKEN_CHANNEL = export const GET_CLIENT_SETTINGS_CHANNEL = "desktop:get-client-settings"; export const SET_CLIENT_SETTINGS_CHANNEL = "desktop:set-client-settings"; export const SETUP_SNAP_SHOT_CHANNEL = "desktop:setup-snap-shot"; +export const RESTART_CUA_DRIVER_CHANNEL = "desktop:restart-cua-driver"; export const PREVIEW_SNAP_SHOT_CONFIG_CHANNEL = "desktop:preview-snap-shot-config"; export const APPLY_SNAP_SHOT_CONFIG_CHANNEL = "desktop:apply-snap-shot-config"; export const REQUEST_SNAP_SHOT_PERMISSIONS_CHANNEL = "desktop:request-snap-shot-permissions"; diff --git a/apps/desktop/src/ipc/methods/cua.ts b/apps/desktop/src/ipc/methods/cua.ts new file mode 100644 index 0000000000000..fdc8227caa9c9 --- /dev/null +++ b/apps/desktop/src/ipc/methods/cua.ts @@ -0,0 +1,16 @@ +import * as Effect from "effect/Effect"; +import * as Schema from "effect/Schema"; + +import * as DesktopCuaDriver from "../../cua/DesktopCuaDriver.ts"; +import * as IpcChannels from "../channels.ts"; +import * as DesktopIpc from "../DesktopIpc.ts"; + +/** Permission onboarding calls this once grants exist, so a driver that started before them is replaced. */ +export const restartCuaDriver = DesktopIpc.makeIpcMethod({ + channel: IpcChannels.RESTART_CUA_DRIVER_CHANNEL, + payload: Schema.Void, + result: Schema.Void, + handler: Effect.fn("desktop.ipc.cua.restart")(function* () { + yield* (yield* DesktopCuaDriver.DesktopCuaDriver).restart; + }), +}); diff --git a/apps/desktop/src/ipc/methods/window.ts b/apps/desktop/src/ipc/methods/window.ts index abf4ec929a7b7..5668dbba5350d 100644 --- a/apps/desktop/src/ipc/methods/window.ts +++ b/apps/desktop/src/ipc/methods/window.ts @@ -47,6 +47,10 @@ import * as ElectronWindow from "../../electron/ElectronWindow.ts"; import * as Electron from "electron"; import { msg } from "gt-i18n"; import * as MacPermissions from "../../permissions/MacPermissions.ts"; +import { + isMacPermissionGranted, + requestMacPermission, +} from "../../permissions/macPermissionStatus.ts"; import { safariPermissionCheck } from "../../preview/BrowserImport/SafariPermission.ts"; import { m } from "../../i18n/messages.ts"; import * as IpcChannels from "../channels.ts"; @@ -428,10 +432,12 @@ export const openSystemSettings = DesktopIpc.makeIpcMethod({ const environment = yield* DesktopEnvironment.DesktopEnvironment; if (environment.platform !== "darwin") return false; const owner = Electron.BrowserWindow.getFocusedWindow(); + // Let macOS list the app in the pane before the user lands there. + if (pane !== "full-disk-access") yield* Effect.promise(() => requestMacPermission(pane)); const opened = yield* shell.openSystemSettings(pane); if (opened && environment.isPackaged) { const permissions = yield* MacPermissions.MacPermissions; - const isGranted = yield* safariPermissionCheck; + const isGranted = pane === "full-disk-access" ? yield* safariPermissionCheck : undefined; yield* permissions.showHelper(pane, owner, isGranted); } return opened; @@ -598,9 +604,10 @@ export const checkSystemPermission = DesktopIpc.makeIpcMethod({ channel: IpcChannels.CHECK_SYSTEM_PERMISSION_CHANNEL, payload: SystemSettingsPaneSchema, result: Schema.Boolean, - handler: Effect.fn("desktop.ipc.window.checkSystemPermission")(function* () { + handler: Effect.fn("desktop.ipc.window.checkSystemPermission")(function* (pane) { const environment = yield* DesktopEnvironment.DesktopEnvironment; if (environment.platform !== "darwin") return false; + if (pane !== "full-disk-access") return isMacPermissionGranted(pane); const check = yield* safariPermissionCheck; return yield* Effect.promise(check); }), diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts index 564ada478ff9a..fd7971ed57d69 100644 --- a/apps/desktop/src/main.ts +++ b/apps/desktop/src/main.ts @@ -65,6 +65,7 @@ import * as DesktopSshEnvironment from "./ssh/DesktopSshEnvironment.ts"; import * as DesktopSshPasswordPrompts from "./ssh/DesktopSshPasswordPrompts.ts"; import * as DesktopState from "./app/DesktopState.ts"; import * as DesktopLegacyLocalStorage from "./app/DesktopLegacyLocalStorage.ts"; +import * as DesktopCuaDriver from "./cua/DesktopCuaDriver.ts"; import * as DesktopTelemetryPublisher from "./telemetry/DesktopTelemetryPublisher.ts"; import * as DesktopRendererHistory from "./telemetry/DesktopRendererHistory.ts"; import * as DesktopUpdatePolicy from "./updates/DesktopUpdatePolicy.ts"; @@ -224,6 +225,7 @@ const layerDesktopApplication = Layer.mergeAll( ), // Updates closes the same tunnels the SSH IPC opens before quitAndInstall. Layer.provideMerge(layerDesktopSsh), + Layer.provideMerge(DesktopCuaDriver.layer), Layer.provideMerge(layerDesktopWslBackend), Layer.provideMerge(layerDesktopLocalEnvironmentAuth), ); diff --git a/apps/desktop/src/permissions/MacPermissionHelper.ts b/apps/desktop/src/permissions/MacPermissionHelper.ts index cad9c5590609b..230485f49e340 100644 --- a/apps/desktop/src/permissions/MacPermissionHelper.ts +++ b/apps/desktop/src/permissions/MacPermissionHelper.ts @@ -12,14 +12,10 @@ import { } from "./MacSettingsWindow.ts"; import { MAC_PERMISSION_SETUP_TITLES, type MacPermission } from "./MacPermission.ts"; +import { isMacPermissionGranted } from "./macPermissionStatus.ts"; -const permissionGranted = (permission: MacPermission) => { - if (permission === "screen-recording") - return Electron.systemPreferences.getMediaAccessStatus("screen") === "granted"; - if (permission === "accessibility") - return Electron.systemPreferences.isTrustedAccessibilityClient(false); - return false; -}; +const permissionGranted = (permission: MacPermission) => + permission === "full-disk-access" ? false : isMacPermissionGranted(permission); function macAppBundlePath(executable: string): string | undefined { return /^(.+\.app)\/Contents\/MacOS\/[^/]+$/.exec(executable)?.[1]; diff --git a/apps/desktop/src/permissions/macPermissionStatus.ts b/apps/desktop/src/permissions/macPermissionStatus.ts new file mode 100644 index 0000000000000..248cc39f7c86b --- /dev/null +++ b/apps/desktop/src/permissions/macPermissionStatus.ts @@ -0,0 +1,30 @@ +import * as Electron from "electron"; + +import type { MacPermission } from "./MacPermission.ts"; + +/** TCC status for the panes a renderer can check; Full Disk Access has no API and is probed by callers. */ +export function isMacPermissionGranted(permission: Exclude) { + return permission === "accessibility" + ? Electron.systemPreferences.isTrustedAccessibilityClient(false) + : Electron.systemPreferences.getMediaAccessStatus("screen") === "granted"; +} + +/** + * Triggers the system prompt that adds T3 Code to the pane's list. Accessibility + * prompts through the trust check; Screen Recording prompts on first capture. + */ +export async function requestMacPermission(permission: Exclude) { + if (permission === "accessibility") { + Electron.systemPreferences.isTrustedAccessibilityClient(true); + return; + } + if (Electron.systemPreferences.getMediaAccessStatus("screen") !== "not-determined") return; + try { + await Electron.desktopCapturer.getSources({ + types: ["screen"], + thumbnailSize: { width: 1, height: 1 }, + }); + } catch { + // A denied probe leaves the status for the caller to read. + } +} diff --git a/apps/desktop/src/preload.ts b/apps/desktop/src/preload.ts index b4b15f6a7a4eb..3d23d5a27ba80 100644 --- a/apps/desktop/src/preload.ts +++ b/apps/desktop/src/preload.ts @@ -168,6 +168,7 @@ contextBridge.exposeInMainWorld("desktopBridge", { ipcRenderer.invoke(IpcChannels.REQUEST_SNAP_SHOT_PERMISSIONS_CHANNEL, includeAccessibility), getSnapShotState: () => ipcRenderer.invoke(IpcChannels.GET_SNAP_SHOT_STATE_CHANNEL), setupSnapShot: (action) => ipcRenderer.invoke(IpcChannels.SETUP_SNAP_SHOT_CHANNEL, action), + restartCuaDriver: () => ipcRenderer.invoke(IpcChannels.RESTART_CUA_DRIVER_CHANNEL, undefined), previewSnapShotConfig: (request) => ipcRenderer.invoke(IpcChannels.PREVIEW_SNAP_SHOT_CONFIG_CHANNEL, request), applySnapShotConfig: (id) => ipcRenderer.invoke(IpcChannels.APPLY_SNAP_SHOT_CONFIG_CHANNEL, id), diff --git a/apps/desktop/src/telemetry/DesktopTelemetryPublisher.test.ts b/apps/desktop/src/telemetry/DesktopTelemetryPublisher.test.ts index 3b0582e8131b7..a07fa0d45c120 100644 --- a/apps/desktop/src/telemetry/DesktopTelemetryPublisher.test.ts +++ b/apps/desktop/src/telemetry/DesktopTelemetryPublisher.test.ts @@ -1,3 +1,4 @@ +import { DesktopHostTelemetryMessage } from "@t3tools/contracts"; import * as NodeServices from "@effect/platform-node/NodeServices"; import { assert, describe, it } from "@effect/vitest"; import * as Deferred from "effect/Deferred"; @@ -6,6 +7,7 @@ import * as Option from "effect/Option"; import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; +import * as Fiber from "effect/Fiber"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Schema from "effect/Schema"; @@ -29,6 +31,10 @@ const layerHistory = Layer.succeed(DesktopRendererHistory.DesktopRendererHistory shutdown: Effect.void, }); +const decodeMessage = Schema.decodeUnknownEffect( + Schema.fromJsonString(DesktopHostTelemetryMessage), +); + function layerElectronApp( metrics: ReadonlyArray, onMetricsRead: () => void = () => undefined, @@ -125,6 +131,116 @@ describe("DesktopTelemetryPublisher", () => { assert.isDefined(closeFiber.pollUnsafe()); }), ); + + it.effect("routes Cua Driver requests and reports only through the primary backend", () => + Effect.gen(function* () { + const layerPower = Layer.succeed( + ElectronPowerMonitor.ElectronPowerMonitor, + ElectronPowerMonitor.ElectronPowerMonitor.of({ + isOnBatteryPower: Effect.succeed(false), + getSystemIdleTime: Effect.succeed(0), + getSystemIdleState: () => Effect.succeed("active"), + getCurrentThermalState: Effect.succeed("nominal"), + onSimpleEvent: () => Effect.void, + onThermalStateChange: () => Effect.void, + onSpeedLimitChange: () => Effect.void, + }), + ); + const layer = DesktopTelemetryPublisher.layer.pipe( + Layer.provide( + Layer.mergeAll( + layerElectronApp([]), + layerPower, + layerHistory, + Layer.mock(DesktopShellEnvironment.DesktopShellEnvironment, { updates: Stream.empty }), + ), + ), + ); + + yield* Effect.gen(function* () { + const publisher = yield* DesktopTelemetryPublisher.DesktopTelemetryPublisher; + const decoder = new TextDecoder(); + + yield* publisher.handleControlForSource("wsl:Ubuntu", { + version: 1, + type: "cuaDriverRequest", + requestId: "remote", + enabled: true, + }); + yield* publisher.handleControlForSource("primary", { + version: 1, + type: "cuaDriverRequest", + requestId: "local", + enabled: true, + }); + const cuaRequest = yield* Stream.runHead(publisher.cuaRequests); + assert.equal(Option.getOrThrow(cuaRequest).requestId, "local"); + yield* publisher.removeControlSource("wsl:Ubuntu"); + yield* publisher.removeControlSource("primary"); + const detachRequest = Option.getOrThrow(yield* Stream.runHead(publisher.cuaRequests)); + assert.equal(detachRequest.requestId, "desktop-primary-detached"); + assert.equal(detachRequest.enabled, false); + + const primaryAttached = yield* Deferred.make(); + const cuaReportFiber = yield* publisher.encodedForSource("primary").pipe( + Stream.mapEffect((bytes) => decodeMessage(decoder.decode(bytes).trim())), + Stream.tap((message) => + message.type === "desktopTelemetryHello" + ? Deferred.succeed(primaryAttached, undefined) + : Effect.void, + ), + Stream.filter((message) => message.type === "cuaDriverReport"), + Stream.runHead, + Effect.forkChild, + ); + yield* Deferred.await(primaryAttached); + const cuaReport = { + version: 1, + type: "cuaDriverReport", + requestId: "local", + status: "ready", + mcp: { + command: "/driver", + args: ["mcp"], + environment: [{ name: "SESSION", value: "opaque" }], + }, + } as const; + yield* publisher.publishCuaReport(cuaReport); + assert.deepEqual(Option.getOrThrow(yield* Fiber.join(cuaReportFiber)), cuaReport); + + // The update report is replayed to every source; it marks the end of the WSL stream. + yield* publisher.publishUpdateReport({ + version: 1, + type: "desktopUpdateStatus", + outcome: "up-to-date", + state: { + enabled: true, + status: "up-to-date", + channel: "latest", + currentVersion: "1.2.3", + hostArch: "arm64", + appArch: "arm64", + runningUnderArm64Translation: false, + availableVersion: null, + downloadedVersion: null, + releaseNotes: [], + downloadPercent: null, + checkedAt: null, + message: null, + errorContext: null, + canRetry: false, + omittedReleaseCount: 0, + }, + }); + const wslMessages = yield* publisher.encodedForSource("wsl:Ubuntu").pipe( + Stream.mapEffect((bytes) => decodeMessage(decoder.decode(bytes).trim())), + Stream.takeUntil((message) => message.type === "desktopUpdateStatus"), + Stream.runCollect, + ); + assert.isFalse(wslMessages.some((message) => message.type === "cuaDriverReport")); + }).pipe(Effect.provide(layer)); + }), + ); }); describe("DesktopRendererHistory", () => { diff --git a/apps/desktop/src/telemetry/DesktopTelemetryPublisher.ts b/apps/desktop/src/telemetry/DesktopTelemetryPublisher.ts index 415901878cafe..1e78170ec623e 100644 --- a/apps/desktop/src/telemetry/DesktopTelemetryPublisher.ts +++ b/apps/desktop/src/telemetry/DesktopTelemetryPublisher.ts @@ -1,5 +1,7 @@ import { DesktopHostTelemetryMessage, + type DesktopCuaDriverRequest, + type DesktopCuaDriverReport, type DesktopHostTelemetrySnapshot, type DesktopTelemetryControlMessage, type DesktopTelemetryCancelDesktopUpdate, @@ -64,6 +66,9 @@ export class DesktopTelemetryPublisher extends Context.Service< { readonly latest: Effect.Effect>; readonly changes: Stream.Stream; + readonly cuaRequests: Stream.Stream; + readonly publishCuaReport: (report: DesktopCuaDriverReport) => Effect.Effect; + readonly encodedForSource: (sourceId: string) => Stream.Stream; readonly encoded: Stream.Stream; readonly handleControlForSource: ( sourceId: string, @@ -174,6 +179,8 @@ export const make = Effect.fn("desktop.telemetryPublisher.make")(function* () { const sequence = yield* Ref.make(0); const latestUpdateReport = yield* Ref.make(Option.none()); const updateReportChanges = yield* PubSub.sliding(16); + const cuaRequests = yield* Queue.unbounded(); + const cuaReports = yield* PubSub.unbounded(); const updateRequestQueue = yield* Queue.unbounded(); const updateCommitQueue = yield* Queue.unbounded(); const updateCancellationQueue = yield* Queue.unbounded(); @@ -324,6 +331,10 @@ export const make = Effect.fn("desktop.telemetryPublisher.make")(function* () { message, ) => { switch (message.type) { + case "cuaDriverRequest": + return sourceId === "primary" + ? Queue.offer(cuaRequests, message).pipe(Effect.asVoid) + : Effect.void; case "setDiagnosticsDemand": return Ref.modify(diagnosticsDemandSources, (sources) => { const previous = sources.size > 0; @@ -370,6 +381,16 @@ export const make = Effect.fn("desktop.telemetryPublisher.make")(function* () { ? Effect.void : Queue.offer(sampleTriggers, undefined).pipe(Effect.asVoid), ), + Effect.andThen( + sourceId === "primary" + ? Queue.offer(cuaRequests, { + version: 1, + type: "cuaDriverRequest", + requestId: "desktop-primary-detached", + enabled: false, + }).pipe(Effect.asVoid) + : Effect.void, + ), ); const snapshots = Stream.unwrap( @@ -419,6 +440,24 @@ export const make = Effect.fn("desktop.telemetryPublisher.make")(function* () { latest: Ref.get(latest), changes: Stream.fromPubSub(changes), encoded, + encodedForSource: (sourceId) => + sourceId === "primary" + ? Stream.unwrap( + Effect.gen(function* () { + const subscription = yield* PubSub.subscribe(cuaReports); + return Stream.merge( + encoded, + Stream.fromSubscription(subscription).pipe( + Stream.map((report) => textEncoder.encode(`${encodeMessage(report)}\n`)), + ), + ); + }), + ) + : encoded, + cuaRequests: Stream.fromQueue(cuaRequests), + publishCuaReport: Effect.fn("DesktopTelemetryPublisher.publishCuaReport")((report) => + PubSub.publish(cuaReports, report).pipe(Effect.asVoid), + ), handleControlForSource, removeControlSource, publishUpdateReport, diff --git a/apps/desktop/src/updates/DesktopRemoteUpdates.test.ts b/apps/desktop/src/updates/DesktopRemoteUpdates.test.ts index 5cdf2ba1b56dc..34bdedcb6078b 100644 --- a/apps/desktop/src/updates/DesktopRemoteUpdates.test.ts +++ b/apps/desktop/src/updates/DesktopRemoteUpdates.test.ts @@ -55,6 +55,9 @@ function runRemoteUpdatesTest( latest: Effect.succeedNone, changes: Stream.empty, encoded: Stream.empty, + encodedForSource: () => Stream.empty, + cuaRequests: Stream.empty, + publishCuaReport: () => Effect.void, handleControlForSource: () => Effect.void, removeControlSource: () => Effect.void, publishUpdateReport: (report) => diff --git a/apps/server/package.json b/apps/server/package.json index bea918f407870..3495e0655b0d7 100644 --- a/apps/server/package.json +++ b/apps/server/package.json @@ -63,6 +63,8 @@ "@t3tools/source-control-gitcafe": "workspace:*", "@t3tools/source-control-github": "workspace:*", "@t3tools/source-control-gitlab": "workspace:*", + "@trycua/cua-driver": "catalog:", + "@ubjs/node": "catalog:", "character-entities": "2.0.2", "diff": "8.0.3", "effect": "catalog:", diff --git a/apps/server/src/cua/CuaDriver.test.ts b/apps/server/src/cua/CuaDriver.test.ts new file mode 100644 index 0000000000000..7d3fc531b18a3 --- /dev/null +++ b/apps/server/src/cua/CuaDriver.test.ts @@ -0,0 +1,579 @@ +import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; +import { it } from "@effect/vitest"; +import type { CuaDriverMcpConfiguration, DesktopCuaDriverReport } from "@t3tools/contracts"; +import type { EmbeddedDriverConnection } from "@trycua/cua-driver/embedded"; +import { Deferred, Effect, Exit, Fiber, Option, PubSub, Ref, Scope, Stream } from "effect"; +import { TestClock } from "effect/testing"; +import { describe, expect } from "vite-plus/test"; + +import * as CuaDriver from "./CuaDriver.ts"; +import * as ServerSettings from "../serverSettings.ts"; +import * as DesktopTelemetryReceiver from "../resourceTelemetry/DesktopTelemetryReceiver.ts"; + +type CuaDriverError = CuaDriver.CuaDriverError; +const makeStandaloneHostFactory = CuaDriver.makeStandaloneHostFactory; +const makeCuaDriver = (options: { + readonly createHost: Parameters[0]; + readonly enabled: Effect.Effect; + readonly changes: Stream.Stream; +}) => + Effect.gen(function* () { + const settings = yield* ServerSettings.ServerSettingsService; + const values = yield* settings.getSettings; + return yield* CuaDriver.make(options.createHost).pipe( + Effect.provideService(ServerSettings.ServerSettingsService, { + ...settings, + getSettings: options.enabled.pipe(Effect.map((enableCua) => ({ ...values, enableCua }))), + subscribeChanges: Effect.succeed( + options.changes.pipe(Stream.map((enableCua) => ({ ...values, enableCua }))), + ), + }), + ); + }).pipe(Effect.provide(ServerSettings.layerTest())); +const makeDesktopHostFactory = ( + receiver: Partial, +) => + CuaDriver.makeDesktopHostFactory().pipe( + Effect.provide([DesktopTelemetryReceiver.layerTest(receiver), NodeCrypto.layer]), + ); + +const mcp: CuaDriverMcpConfiguration = { command: "/driver", args: ["mcp"], environment: [] }; + +const standaloneFixture = Effect.fn(function* (failFirstDestroy = false) { + const start = Promise.withResolvers(); + const stop = Promise.withResolvers(); + const monitor = Promise.withResolvers(); + const started = Promise.withResolvers(); + const stopped = Promise.withResolvers(); + const watching = Promise.withResolvers(); + const destroyed = Promise.withResolvers(); + const destroyFailure = new Error("destroy failed"); + let creates = 0; + let stops = 0; + let destroys = 0; + const factory = yield* makeStandaloneHostFactory( + Effect.succeed("/driver"), + Effect.succeed({ + EmbeddedCuaDriverHost: class { + constructor() { + creates++; + } + start() { + started.resolve(); + return start.promise; + } + stop() { + stops++; + stopped.resolve(); + return stop.promise; + } + waitForExit(_generation: string, options?: { signal: AbortSignal }) { + watching.resolve(options!.signal); + return monitor.promise; + } + uniffiDestroy() { + destroys++; + destroyed.resolve(); + if (failFirstDestroy && destroys === 1) throw destroyFailure; + } + }, + }), + ); + const connection: EmbeddedDriverConnection = { + socketPath: "/socket", + pid: 1, + generation: "first", + driverVersion: "test", + contractVersion: "test", + mcpProtocolVersion: "test", + mcp: { command: mcp.command, args: [...mcp.args], environment: [...mcp.environment] }, + }; + return { + factory, + destroyFailure, + start, + stop, + monitor, + started, + stopped, + watching, + destroyed, + connection, + counts: () => ({ creates, stops, destroys }), + }; +}); + +describe("Cua Driver failure boundaries", () => { + it.effect("preserves SDK import failures without exposing their message", () => + Effect.gen(function* () { + const cause = new Error("private SDK loader details"); + const factory = yield* CuaDriver.makeStandaloneHostFactory( + Effect.succeed("/driver"), + Effect.fail(new CuaDriver.CuaDriverSdkLoadError({ cause })), + ); + const failure = yield* Effect.flip(factory); + expect(failure._tag).toBe("CuaDriverSdkLoadError"); + expect(failure.message).toBe("Could not load the Cua Driver SDK."); + if (failure._tag !== "CuaDriverSdkLoadError") throw new Error("Expected SDK failure"); + expect(failure.cause).toBe(cause); + }), + ); + + it.effect("preserves native constructor failures and identifies the binary", () => + Effect.gen(function* () { + const cause = new Error("private constructor details"); + const factory = yield* CuaDriver.makeStandaloneHostFactory( + Effect.succeed("/driver"), + Effect.succeed({ + EmbeddedCuaDriverHost: class { + constructor() { + throw cause; + } + start() { + return Promise.reject(cause); + } + stop() { + return Promise.resolve(); + } + waitForExit() { + return Promise.reject(cause); + } + uniffiDestroy() {} + }, + }), + ); + const failure = yield* Effect.flip(factory); + expect(failure._tag).toBe("CuaDriverHostCreateError"); + expect(failure.message).toBe("Could not create the Cua Driver host."); + if (failure._tag !== "CuaDriverHostCreateError") throw new Error("Expected host failure"); + expect(failure.binaryPath).toBe("/driver"); + expect(failure.cause).toBe(cause); + }), + ); + + it.effect("preserves native start and exit failures at their own boundaries", () => + Effect.gen(function* () { + const cause = new Error("private native details"); + const starting = yield* standaloneFixture(); + const failedHost = yield* starting.factory; + starting.start.reject(cause); + const startFailure = yield* Effect.flip(failedHost.start); + expect(startFailure._tag).toBe("CuaDriverStartError"); + expect(startFailure.message).toBe("Could not start Cua Driver."); + if (startFailure._tag !== "CuaDriverStartError") throw new Error("Expected start failure"); + expect(startFailure.cause).toBe(cause); + starting.stop.resolve(); + yield* failedHost.stop; + + const running = yield* standaloneFixture(); + const host = yield* running.factory; + running.start.resolve(running.connection); + yield* host.start; + running.monitor.reject(cause); + const exitFailure = yield* Effect.flip(host.waitForExit); + expect(exitFailure._tag).toBe("CuaDriverExitError"); + expect(exitFailure.message).toBe("Cua Driver exited unexpectedly."); + if (exitFailure._tag !== "CuaDriverExitError") throw new Error("Expected exit failure"); + expect(exitFailure.cause).toBe(cause); + running.stop.resolve(); + yield* host.stop; + }), + ); +}); + +describe("standalone Cua Driver ownership", () => { + it.effect("releases ownership after native destruction throws without retrying cleanup", () => + Effect.gen(function* () { + const f = yield* standaloneFixture(true); + const host = yield* f.factory; + f.start.resolve(f.connection); + yield* host.start; + f.stop.resolve(); + f.monitor.reject(new Error("monitor cancelled")); + const failure = yield* Effect.flip(host.stop); + expect(failure).toBeInstanceOf(CuaDriver.CuaDriverStopError); + expect(failure.message).toBe("Could not stop Cua Driver."); + if (failure._tag !== "CuaDriverStopError") throw new Error("Expected stop failure"); + expect(failure.binaryPath).toBe("/driver"); + expect(failure.cause).toBe(f.destroyFailure); + expect(Exit.isFailure(yield* Effect.exit(host.stop))).toBe(true); + expect(f.counts()).toEqual({ creates: 1, stops: 1, destroys: 1 }); + const replacement = yield* f.factory; + expect(yield* replacement.start).toEqual({ ...mcp, socketPath: "/socket" }); + yield* replacement.stop; + expect(f.counts()).toEqual({ creates: 2, stops: 2, destroys: 2 }); + }), + ); + + it.effect("retains a cancelled start until start and stop settle, with one cleanup", () => + Effect.gen(function* () { + const f = yield* standaloneFixture(); + const host = yield* f.factory; + const starting = yield* host.start.pipe(Effect.forkChild({ startImmediately: true })); + yield* Effect.promise(() => f.started.promise); + yield* Fiber.interrupt(starting); + const stopping = yield* host.stop.pipe(Effect.forkChild({ startImmediately: true })); + const stoppingAgain = yield* host.stop.pipe(Effect.forkChild({ startImmediately: true })); + expect(f.counts()).toEqual({ creates: 1, stops: 0, destroys: 0 }); + expect(Exit.isFailure(yield* Effect.exit(f.factory))).toBe(true); + f.start.resolve(f.connection); + yield* Effect.promise(() => f.stopped.promise); + expect(f.counts()).toEqual({ creates: 1, stops: 1, destroys: 0 }); + f.stop.resolve(); + yield* Effect.all([Fiber.join(stopping), Fiber.join(stoppingAgain)]); + expect(f.counts()).toEqual({ creates: 1, stops: 1, destroys: 1 }); + const replacement = yield* f.factory; + yield* replacement.stop; + expect(f.counts()).toEqual({ creates: 2, stops: 2, destroys: 2 }); + }), + ); + + it.effect("aborts the monitor and retains ownership after the stop waiter is cancelled", () => + Effect.gen(function* () { + const f = yield* standaloneFixture(); + const host = yield* f.factory; + f.start.resolve(f.connection); + expect(yield* host.start).toEqual({ ...mcp, socketPath: "/socket" }); + const signal = yield* Effect.promise(() => f.watching.promise); + const stopping = yield* host.stop.pipe(Effect.forkChild({ startImmediately: true })); + yield* Effect.promise(() => f.stopped.promise); + expect(signal.aborted).toBe(true); + yield* Fiber.interrupt(stopping); + f.stop.resolve(); + expect(Exit.isFailure(yield* Effect.exit(f.factory))).toBe(true); + expect(f.counts()).toEqual({ creates: 1, stops: 1, destroys: 0 }); + f.monitor.reject(new Error("monitor cancelled")); + yield* Effect.promise(() => f.destroyed.promise); + yield* host.stop; + expect(f.counts()).toEqual({ creates: 1, stops: 1, destroys: 1 }); + }), + ); + + it.effect("destroys failed starts after a rejected native stop settles", () => + Effect.gen(function* () { + const f = yield* standaloneFixture(); + const host = yield* f.factory; + const starting = yield* host.start.pipe(Effect.forkChild({ startImmediately: true })); + yield* Effect.promise(() => f.started.promise); + f.start.reject(new Error("start failed")); + expect(Exit.isFailure(yield* Fiber.await(starting))).toBe(true); + const stopping = yield* host.stop.pipe(Effect.forkChild({ startImmediately: true })); + yield* Effect.promise(() => f.stopped.promise); + f.stop.reject(new Error("stop failed")); + yield* Fiber.join(stopping); + expect(f.counts()).toEqual({ creates: 1, stops: 1, destroys: 1 }); + }), + ); +}); + +const fixture = Effect.fn(function* (initialEnabled = true) { + const enabled = yield* Ref.make(initialEnabled); + const changes = yield* PubSub.unbounded(); + const subscription = yield* PubSub.subscribe(changes); + const started = yield* Deferred.make(); + const stopped = yield* Deferred.make(); + const result = yield* Deferred.make(); + const exited = yield* Deferred.make(); + const watching = yield* Deferred.make(); + let starts = 0; + let stops = 0; + const service = yield* makeCuaDriver({ + enabled: Ref.get(enabled), + changes: Stream.fromSubscription(subscription), + createHost: Effect.succeed({ + start: Effect.sync(() => { + starts++; + }).pipe( + Effect.andThen(Deferred.succeed(started, undefined)), + Effect.andThen(Deferred.await(result)), + ), + stop: Effect.sync(() => { + stops++; + }).pipe(Effect.andThen(Deferred.succeed(stopped, undefined)), Effect.asVoid), + waitForExit: Deferred.succeed(watching, undefined).pipe( + Effect.andThen(Deferred.await(exited)), + ), + }), + }); + const setEnabled = (value: boolean) => + Ref.set(enabled, value).pipe(Effect.andThen(PubSub.publish(changes, value))); + return { + service, + started, + stopped, + result, + exited, + watching, + setEnabled, + starts: () => starts, + stops: () => stops, + }; +}); + +describe("CuaDriver", () => { + it.effect("bounds shutdown when the host never acknowledges stop", () => + Effect.gen(function* () { + const scope = yield* Scope.make(); + const stopping = yield* Deferred.make(); + const service = yield* makeCuaDriver({ + enabled: Effect.succeed(true), + changes: Stream.empty, + createHost: Effect.succeed({ + start: Effect.succeed(mcp), + stop: Deferred.succeed(stopping, undefined).pipe(Effect.andThen(Effect.never)), + waitForExit: Effect.never, + }), + }).pipe(Scope.provide(scope)); + expect(yield* service.acquire).toEqual(Option.some(mcp)); + const closing = yield* Scope.close(scope, Exit.void).pipe(Effect.forkChild); + yield* Deferred.await(stopping); + yield* TestClock.adjust("5 seconds"); + yield* Fiber.join(closing); + expect(yield* service.acquire).toEqual(Option.none()); + }), + ); + + it.effect("late cleanup from a revoked start cannot stop its replacement", () => + Effect.scoped( + Effect.gen(function* () { + const enabled = yield* Ref.make(true); + const changes = yield* PubSub.unbounded(); + const subscription = yield* PubSub.subscribe(changes); + const firstStarted = yield* Deferred.make(); + const firstStopped = yield* Deferred.make(); + const lateReady = yield* Deferred.make(); + const lateConsumed = yield* Deferred.make(); + let count = 0; + let oldStops = 0; + let newStops = 0; + const service = yield* makeCuaDriver({ + enabled: Ref.get(enabled), + changes: Stream.fromSubscription(subscription), + createHost: Effect.sync(() => { + count++; + return count === 1 + ? { + start: Deferred.succeed(firstStarted, undefined).pipe( + Effect.andThen(Deferred.await(lateReady)), + Effect.tap(() => Deferred.succeed(lateConsumed, undefined)), + ), + stop: Effect.sync(() => { + oldStops++; + }).pipe(Effect.andThen(Deferred.succeed(firstStopped, undefined)), Effect.asVoid), + waitForExit: Effect.never, + } + : { + start: Effect.succeed(mcp), + stop: Effect.sync(() => { + newStops++; + }), + waitForExit: Effect.never, + }; + }), + }); + const old = yield* service.acquire.pipe(Effect.forkChild); + yield* Deferred.await(firstStarted); + yield* Ref.set(enabled, false); + yield* PubSub.publish(changes, false); + yield* Deferred.await(firstStopped); + expect(yield* Fiber.join(old)).toEqual(Option.none()); + yield* Ref.set(enabled, true); + expect(yield* service.acquire).toEqual(Option.some(mcp)); + yield* Deferred.succeed(lateReady, mcp); + yield* Deferred.await(lateConsumed); + expect(yield* service.acquire).toEqual(Option.some(mcp)); + expect(count).toBe(2); + expect(oldStops).toBe(1); + expect(newStops).toBe(0); + }), + ), + ); + + it.effect("does not start a disabled host", () => + Effect.scoped( + Effect.gen(function* () { + const f = yield* fixture(false); + expect(yield* f.service.enabled).toBe(false); + expect(yield* f.service.acquire).toEqual(Option.none()); + expect(f.starts()).toBe(0); + }), + ), + ); + + it.effect("shares a lazy start across concurrent callers and owns shutdown", () => + Effect.gen(function* () { + const scope = yield* Scope.make(); + const f = yield* fixture().pipe(Scope.provide(scope)); + const first = yield* f.service.acquire.pipe(Effect.forkChild); + const second = yield* f.service.acquire.pipe(Effect.forkChild); + yield* Deferred.await(f.started); + yield* Deferred.succeed(f.result, mcp); + expect(yield* Fiber.join(first)).toEqual(Option.some(mcp)); + expect(yield* Fiber.join(second)).toEqual(Option.some(mcp)); + expect(f.starts()).toBe(1); + yield* Scope.close(scope, Exit.void); + expect(f.stops()).toBe(1); + }), + ); + + it.effect("caller cancellation cannot cancel the environment start", () => + Effect.scoped( + Effect.gen(function* () { + const f = yield* fixture(); + const caller = yield* f.service.acquire.pipe(Effect.forkChild); + yield* Deferred.await(f.started); + yield* Fiber.interrupt(caller); + yield* Deferred.succeed(f.result, mcp); + expect(yield* f.service.acquire).toEqual(Option.some(mcp)); + expect(f.starts()).toBe(1); + }), + ), + ); + + it.effect("disabling during start revokes waiters and rejects late readiness", () => + Effect.scoped( + Effect.gen(function* () { + const f = yield* fixture(); + const caller = yield* f.service.acquire.pipe(Effect.forkChild); + yield* Deferred.await(f.started); + yield* f.setEnabled(false); + yield* Deferred.await(f.stopped); + expect(yield* Fiber.join(caller)).toEqual(Option.none()); + yield* Deferred.succeed(f.result, mcp); + expect(yield* f.service.acquire).toEqual(Option.none()); + expect(f.stops()).toBe(1); + }), + ), + ); + + it.effect("shutdown during start resolves callers and stops the host", () => + Effect.gen(function* () { + const scope = yield* Scope.make(); + const f = yield* fixture().pipe(Scope.provide(scope)); + const caller = yield* f.service.acquire.pipe(Effect.forkChild); + yield* Deferred.await(f.started); + yield* Scope.close(scope, Exit.void); + expect(yield* Fiber.join(caller)).toEqual(Option.none()); + expect(f.stops()).toBe(1); + expect(yield* f.service.acquire).toEqual(Option.none()); + }), + ); + + it.effect("cleans a failed start and allows the next session to retry", () => + Effect.scoped( + Effect.gen(function* () { + const f = yield* fixture(); + const caller = yield* f.service.acquire.pipe(Effect.forkChild); + yield* Deferred.await(f.started); + yield* Deferred.fail( + f.result, + new CuaDriver.CuaDriverStartError({ + binaryPath: "/driver", + cause: new Error("test failure"), + }), + ); + expect(yield* Fiber.join(caller)).toEqual(Option.none()); + expect(f.stops()).toBe(1); + expect(yield* f.service.acquire).toEqual(Option.none()); + expect(f.starts()).toBe(2); + expect(f.stops()).toBe(2); + }), + ), + ); + + it.effect("bounds startup and cleans the failed attempt", () => + Effect.scoped( + Effect.gen(function* () { + const f = yield* fixture(); + const caller = yield* f.service.acquire.pipe(Effect.forkChild); + yield* Deferred.await(f.started); + yield* TestClock.adjust("30 seconds"); + expect(yield* Fiber.join(caller)).toEqual(Option.none()); + expect(f.stops()).toBe(1); + }), + ), + ); + + it.effect("clears a crashed generation without restarting it automatically", () => + Effect.scoped( + Effect.gen(function* () { + const f = yield* fixture(); + yield* Deferred.succeed(f.result, mcp); + expect(yield* f.service.acquire).toEqual(Option.some(mcp)); + yield* Deferred.await(f.watching); + yield* Deferred.succeed(f.exited, undefined); + yield* Deferred.await(f.stopped); + expect(f.starts()).toBe(1); + expect(yield* f.service.acquire).toEqual(Option.some(mcp)); + expect(f.starts()).toBe(2); + }), + ), + ); + + it.effect("subscribes before synchronous desktop replies and observes crashes", () => + Effect.scoped( + Effect.gen(function* () { + const reports = yield* PubSub.unbounded(); + let startId = ""; + const factory = yield* makeDesktopHostFactory({ + cuaReports: Stream.fromPubSub(reports), + requestCuaDriver: (requestId, enabled) => { + if (enabled) startId = requestId; + return PubSub.publish( + reports, + enabled + ? { version: 1, type: "cuaDriverReport", requestId, status: "ready", mcp } + : { version: 1, type: "cuaDriverReport", requestId, status: "stopped" }, + ).pipe(Effect.asVoid); + }, + }); + const host = yield* factory; + expect(yield* host.start).toEqual(mcp); + yield* PubSub.publish(reports, { + version: 1, + type: "cuaDriverReport", + requestId: startId, + status: "unavailable", + }); + yield* host.waitForExit; + yield* host.stop; + }), + ), + ); +}); + +describe("Cua Driver readiness", () => { + it.effect("remembers why the last start failed until a start succeeds", () => + Effect.scoped( + Effect.gen(function* () { + let attempts = 0; + const service = yield* makeCuaDriver({ + enabled: Effect.succeed(true), + changes: Stream.empty, + createHost: Effect.suspend(() => { + attempts++; + return attempts === 1 + ? Effect.fail( + new CuaDriver.CuaDriverDesktopUnavailableError({ + requestId: "first", + detail: "T3 Code needs Accessibility and Screen Recording access.", + }), + ) + : Effect.succeed({ + start: Effect.succeed(mcp), + stop: Effect.void, + waitForExit: Effect.never, + }); + }), + }); + expect(yield* service.lastFailure).toEqual(Option.none()); + expect(yield* service.acquire).toEqual(Option.none()); + expect(yield* service.lastFailure).toEqual( + Option.some("T3 Code needs Accessibility and Screen Recording access."), + ); + expect(yield* service.acquire).toEqual(Option.some(mcp)); + expect(yield* service.lastFailure).toEqual(Option.none()); + }), + ), + ); +}); diff --git a/apps/server/src/cua/CuaDriver.ts b/apps/server/src/cua/CuaDriver.ts new file mode 100644 index 0000000000000..9c7d389b64b36 --- /dev/null +++ b/apps/server/src/cua/CuaDriver.ts @@ -0,0 +1,516 @@ +import * as NodeModule from "node:module"; + +import type { CuaDriverMcpConfiguration, DesktopCuaDriverReport } from "@t3tools/contracts"; +import type { EmbeddedCuaDriverHost, EmbeddedDriverConnection } from "@trycua/cua-driver/embedded"; +import * as Config from "effect/Config"; +import * as Context from "effect/Context"; +import * as SecureRandom from "effect/crypto/SecureRandom"; +import * as Deferred from "effect/Deferred"; +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; +import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; +import * as Path from "effect/Path"; +import * as Schema from "effect/Schema"; +import * as Scope from "effect/Scope"; +import * as Semaphore from "effect/Semaphore"; +import * as Stream from "effect/Stream"; + +import * as ServerConfig from "../config.ts"; +import * as CuaDriverInstallation from "./CuaDriverInstallation.ts"; +import type { CuaDriverInstallError } from "./CuaDriverInstallation.ts"; +import * as DesktopTelemetryReceiver from "../resourceTelemetry/DesktopTelemetryReceiver.ts"; +import * as ServerSettings from "../serverSettings.ts"; + +export class CuaDriverBusyError extends Schema.TaggedError()( + "CuaDriverBusyError", + {}, +) { + override get message() { + return "The previous Cua Driver is still stopping."; + } +} + +export class CuaDriverSdkLoadError extends Schema.TaggedError()( + "CuaDriverSdkLoadError", + { + cause: Schema.Unknown, + }, +) { + override get message() { + return "Could not load the Cua Driver SDK."; + } +} + +export class CuaDriverHostCreateError extends Schema.TaggedError()( + "CuaDriverHostCreateError", + { + binaryPath: Schema.String, + cause: Schema.Unknown, + }, +) { + override get message() { + return "Could not create the Cua Driver host."; + } +} + +export class CuaDriverStopError extends Schema.TaggedError()( + "CuaDriverStopError", + { + binaryPath: Schema.String, + cause: Schema.Unknown, + }, +) { + override get message() { + return "Could not stop Cua Driver."; + } +} + +export class CuaDriverStartError extends Schema.TaggedError()( + "CuaDriverStartError", + { + binaryPath: Schema.String, + cause: Schema.Unknown, + }, +) { + override get message() { + return "Could not start Cua Driver."; + } +} + +export class CuaDriverExitError extends Schema.TaggedError()( + "CuaDriverExitError", + { + binaryPath: Schema.String, + cause: Schema.Unknown, + }, +) { + override get message() { + return "Cua Driver exited unexpectedly."; + } +} + +export class CuaDriverDesktopRequestError extends Schema.TaggedError()( + "CuaDriverDesktopRequestError", + { + requestId: Schema.String, + cause: Schema.Unknown, + }, +) { + override get message() { + return "Could not request desktop Cua Driver."; + } +} + +export class CuaDriverDesktopStopError extends Schema.TaggedError()( + "CuaDriverDesktopStopError", + { + requestId: Schema.String, + cause: Schema.Unknown, + }, +) { + override get message() { + return "Could not stop desktop Cua Driver."; + } +} + +export class CuaDriverDesktopUnavailableError extends Schema.TaggedError()( + "CuaDriverDesktopUnavailableError", + { + requestId: Schema.String, + detail: Schema.optional(Schema.String), + }, +) { + override get message() { + return this.detail ?? "Desktop Cua Driver is unavailable."; + } +} + +export type CuaDriverError = + | CuaDriverInstallError + | CuaDriverBusyError + | CuaDriverSdkLoadError + | CuaDriverHostCreateError + | CuaDriverStopError + | CuaDriverStartError + | CuaDriverExitError + | CuaDriverDesktopRequestError + | CuaDriverDesktopStopError + | CuaDriverDesktopUnavailableError; + +export class CuaDriver extends Context.Service< + CuaDriver, + { + readonly enabled: Effect.Effect; + readonly acquire: Effect.Effect>; + /** Why the last start failed, until a later start succeeds. */ + readonly lastFailure: Effect.Effect>; + } +>()("t3/cua/CuaDriver") {} + +export interface CuaDriverHost { + readonly start: Effect.Effect; + readonly stop: Effect.Effect; + readonly waitForExit: Effect.Effect; +} + +const START_TIMEOUT = "30 seconds"; +const STOP_TIMEOUT = "5 seconds"; + +/** One environment owns the host; cancelling a session only cancels its wait. */ +export const make = Effect.fn("CuaDriver.make")(function* ( + createHost: Effect.Effect, +) { + const settings = yield* ServerSettings.ServerSettingsService; + const changes = yield* settings.subscribeChanges; + const enabled = settings.getSettings.pipe( + Effect.map((value) => value.enableCua), + Effect.orElseSucceed(() => false), + ); + const scope = yield* Effect.scope; + const workers = yield* Scope.fork(scope); + const mutex = yield* Semaphore.make(1); + type Attempt = { + readonly ready: Deferred.Deferred>; + host?: CuaDriverHost; + stopped?: boolean; + /** Message of the typed error that ended this attempt, for the readiness signal. */ + failure?: string; + }; + let current: Attempt | undefined; + let closed = false; + let lastFailure: string | undefined; + + const stop = (attempt: Attempt) => + Effect.suspend(() => { + if (!attempt.host || attempt.stopped) return Effect.void; + attempt.stopped = true; + return attempt.host.stop.pipe( + Effect.interruptible, + Effect.timeout(STOP_TIMEOUT), + Effect.catchCause(() => Effect.logWarning("Cua Driver host cleanup did not complete.")), + ); + }); + + const revoke = mutex.withPermits(1)( + Effect.gen(function* () { + const previous = current; + current = undefined; + if (previous) { + yield* Deferred.succeed(previous.ready, Option.none()); + yield* stop(previous); + } + }), + ); + + yield* Effect.addFinalizer(() => + Effect.sync(() => { + closed = true; + }).pipe(Effect.andThen(revoke)), + ); + yield* changes.pipe( + Stream.map((value) => value.enableCua), + Stream.runForEach((enabled) => (enabled ? Effect.void : revoke)), + Effect.forkIn(workers, { startImmediately: true }), + ); + + const launch = Effect.fn("CuaDriver.launch")( + function* (attempt: Attempt) { + const rememberFailure = (error: { readonly message: string }) => + Effect.sync(() => { + attempt.failure = error.message; + }); + const host = yield* createHost.pipe( + Effect.tapError(rememberFailure), + Effect.timeout(START_TIMEOUT), + ); + attempt.host = host; + if (closed || current !== attempt) { + yield* stop(attempt); + return; + } + const mcp = yield* host.start.pipe( + Effect.tapError(rememberFailure), + Effect.timeout(START_TIMEOUT), + ); + yield* mutex.withPermits(1)( + Effect.gen(function* () { + if (closed || current !== attempt || !(yield* enabled)) { + yield* stop(attempt); + yield* Deferred.succeed(attempt.ready, Option.none()); + if (current === attempt) current = undefined; + return; + } + lastFailure = undefined; + yield* Deferred.succeed(attempt.ready, Option.some(mcp)); + }), + ); + if (current !== attempt) return; + yield* host.waitForExit; + yield* mutex.withPermits(1)( + Effect.gen(function* () { + if (current !== attempt) return; + current = undefined; + yield* stop(attempt); + }), + ); + }, + (effect, attempt) => + effect.pipe( + Effect.catchCause(() => + mutex.withPermits(1)( + Effect.gen(function* () { + if (current === attempt) current = undefined; + lastFailure = attempt.failure ?? "Cua Driver could not start."; + yield* stop(attempt); + yield* Deferred.succeed(attempt.ready, Option.none()); + yield* Effect.logWarning( + "Cua Driver is unavailable; this coding session will continue without computer use.", + ); + }), + ), + ), + ), + ); + + const acquire = Effect.gen(function* () { + const attempt = yield* mutex + .withPermits(1)( + Effect.gen(function* () { + if (closed || !(yield* enabled)) return undefined; + if (current) return current; + const next: Attempt = { + ready: yield* Deferred.make>(), + }; + current = next; + yield* launch(next).pipe(Effect.interruptible, Effect.forkIn(workers)); + return next; + }), + ) + .pipe(Effect.uninterruptible); + return attempt ? yield* Deferred.await(attempt.ready) : Option.none(); + }); + + return CuaDriver.of({ + enabled: enabled, + acquire, + lastFailure: Effect.sync(() => Option.fromNullishOr(lastFailure)), + }); +}); + +type StandaloneHostModule = { + readonly EmbeddedCuaDriverHost: new ( + path: string, + hostBundleId: string, + ) => Pick; +}; + +// A Node single-executable cannot `import()` the external SDK from disk, and the SDK exposes +// only `import` conditions, so find its package on the usual paths and `require` the entry file. +const requireForCuaDriver = NodeModule.createRequire(import.meta.url); + +const loadEmbeddedCuaDriver = Effect.fn("CuaDriver.loadEmbeddedSdk")(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + for (const lookupPath of requireForCuaDriver.resolve.paths("@trycua/cua-driver") ?? []) { + const packageDir = path.join(lookupPath, "@trycua", "cua-driver"); + if ( + yield* fs + .exists(path.join(packageDir, "package.json")) + .pipe(Effect.orElseSucceed(() => false)) + ) { + return yield* Effect.try({ + try: () => + requireForCuaDriver(path.join(packageDir, "dist", "embedded.js")) as StandaloneHostModule, + catch: (cause) => new CuaDriverSdkLoadError({ cause }), + }); + } + } + return yield* new CuaDriverSdkLoadError({ + cause: "@trycua/cua-driver is not installed beside this server.", + }); +}); + +/** A timed-out native cleanup still owns the factory until every call settles. */ +export const makeStandaloneHostFactory = Effect.fn("CuaDriver.standaloneHostFactory")(function* ( + resolveBinary: Effect.Effect, + loadEmbedded: Effect.Effect, +) { + const mutex = yield* Semaphore.make(1); + let occupied = false; + // Return a lazy acquisition effect so one factory owns all subsequent host attempts. + return mutex.withPermits(1)( + Effect.gen(function* () { + if (occupied) { + return yield* new CuaDriverBusyError({}); + } + const binaryPath = yield* resolveBinary; + const { EmbeddedCuaDriverHost } = yield* loadEmbedded; + const host = yield* Effect.try({ + try: () => new EmbeddedCuaDriverHost(binaryPath, "com.t3tools.t3code.server"), + catch: (cause) => new CuaDriverHostCreateError({ binaryPath, cause }), + }); + occupied = true; + // @effect-diagnostics-next-line abortControllerInEffect:off - Stopping the host aborts its start and exit monitor independently of the starting fiber. + const abort = new AbortController(); + let starting: Promise | undefined; + let monitoring: Promise | undefined; + let releasing: Promise | undefined; + let retired = false; + const stop = Effect.tryPromise({ + try: () => { + if (!releasing) { + retired = true; + abort.abort(); + releasing = (async () => { + try { + await starting; + } catch { + /* Failed starts still own a host. */ + } + try { + await host.stop(); + } catch { + /* Destruction must still run. */ + } + try { + await monitoring; + } catch { + /* Cancellation ends the monitor. */ + } + try { + host.uniffiDestroy(); + } finally { + occupied = false; + } + })(); + } + return releasing; + }, + catch: (cause) => new CuaDriverStopError({ binaryPath, cause }), + }); + return { + start: Effect.tryPromise({ + try: async (signal) => { + if (retired) throw new Error("Cua Driver start was revoked."); + signal.addEventListener("abort", () => abort.abort(), { once: true }); + starting ??= (async () => { + const connection = await host.start({ signal: abort.signal }); + if (retired) throw new Error("Cua Driver start was revoked."); + monitoring = host.waitForExit(connection.generation, { signal: abort.signal }); + void monitoring.catch(() => undefined); + return connection; + })(); + const connection = await starting; + return { ...connection.mcp, socketPath: connection.socketPath }; + }, + catch: (cause) => new CuaDriverStartError({ binaryPath, cause }), + }), + stop, + waitForExit: Effect.tryPromise({ + try: async () => { + await monitoring; + }, + catch: (cause) => new CuaDriverExitError({ binaryPath, cause }), + }), + } satisfies CuaDriverHost; + }), + ); +}); + +export const makeDesktopHostFactory = Effect.fn("CuaDriver.desktopHostFactory")(function* () { + const receiver = yield* DesktopTelemetryReceiver.DesktopTelemetryReceiver; + const random = yield* SecureRandom.SecureRandom; + const pending = new Map>(); + const exits = new Map>(); + yield* receiver.cuaReports.pipe( + Stream.runForEach((report) => + Effect.gen(function* () { + const reply = pending.get(report.requestId); + if (reply) yield* Deferred.succeed(reply, report); + if (report.status !== "ready") { + const exit = exits.get(report.requestId); + if (exit) yield* Deferred.succeed(exit, undefined); + } + }), + ), + Effect.forkScoped({ startImmediately: true }), + ); + + // Subscribe once for the service, then allocate a fresh request for each lazy start. + return Effect.gen(function* () { + const requestId = yield* random.uuidV4(); + const ready = yield* Deferred.make(); + const exit = yield* Deferred.make(); + let requested = false; + pending.set(requestId, ready); + exits.set(requestId, exit); + return { + start: Effect.gen(function* () { + requested = true; + yield* receiver + .requestCuaDriver(requestId, true) + .pipe(Effect.mapError((cause) => new CuaDriverDesktopRequestError({ requestId, cause }))); + const report = yield* Deferred.await(ready); + if (report.status === "ready") return report.mcp; + if (report.message) + yield* Effect.logWarning("Cua Driver host reported unavailable.", { + message: report.message, + }); + return yield* new CuaDriverDesktopUnavailableError({ + requestId, + ...(report.message ? { detail: report.message } : {}), + }); + }), + stop: Effect.gen(function* () { + if (!requested) { + pending.delete(requestId); + exits.delete(requestId); + return; + } + const stopId = yield* random.uuidV4(); + const stopped = yield* Deferred.make(); + pending.set(stopId, stopped); + yield* receiver.requestCuaDriver(stopId, false).pipe( + Effect.mapError((cause) => new CuaDriverDesktopStopError({ requestId: stopId, cause })), + Effect.andThen(Deferred.await(stopped)), + Effect.ensuring( + Effect.sync(() => { + pending.delete(stopId); + pending.delete(requestId); + exits.delete(requestId); + }), + ), + ); + }), + waitForExit: Deferred.await(exit), + } satisfies CuaDriverHost; + }); +}); + +export const layer = Layer.effect( + CuaDriver, + Effect.gen(function* () { + const config = yield* ServerConfig.ServerConfig; + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + // The desktop host owns the driver it bundles. Elsewhere an explicit + // executable wins, then the pinned release T3 downloads on first use. + if (config.mode === "desktop" && config.desktopTelemetryControlFd !== undefined) { + return yield* make(yield* makeDesktopHostFactory()); + } + const override = Option.getOrUndefined( + yield* Config.String("T3CODE_CUA_DRIVER_PATH").pipe(Config.option), + )?.trim(); + const installation = yield* CuaDriverInstallation.CuaDriverInstallation; + return yield* make( + yield* makeStandaloneHostFactory( + override ? Effect.succeed(override) : installation.executable, + loadEmbeddedCuaDriver().pipe( + Effect.provideService(FileSystem.FileSystem, fs), + Effect.provideService(Path.Path, path), + ), + ), + ); + }), +).pipe(Layer.provide(CuaDriverInstallation.layer)); diff --git a/apps/server/src/cua/CuaDriverInstallation.test.ts b/apps/server/src/cua/CuaDriverInstallation.test.ts new file mode 100644 index 0000000000000..ef6691a269119 --- /dev/null +++ b/apps/server/src/cua/CuaDriverInstallation.test.ts @@ -0,0 +1,147 @@ +import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; +import * as NodeServices from "@effect/platform-node/NodeServices"; +import { expect, it } from "@effect/vitest"; +import * as HostProcess from "@t3tools/shared/HostProcess"; +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; +import * as Layer from "effect/Layer"; +import * as Path from "effect/Path"; +import * as Stream from "effect/Stream"; +import { HttpClient, HttpClientResponse } from "effect/http"; +import * as Digest from "effect/crypto/Digest"; +import * as Hex from "effect/encoding/Hex"; +import * as NodeZlib from "node:zlib"; + +import * as CuaDriverInstallation from "./CuaDriverInstallation.ts"; + +/** A stored ZIP with the Windows release's flat layout. */ +const makeZip = (entries: ReadonlyArray<{ readonly name: string; readonly data: string }>) => { + const records: Array = []; + const directory: Array = []; + let offset = 0; + for (const entry of entries) { + const name = Buffer.from(entry.name); + const data = Buffer.from(entry.data); + const crc = NodeZlib.crc32(data); + const local = Buffer.alloc(30); + local.writeUInt32LE(0x04034b50, 0); + local.writeUInt16LE(20, 4); + local.writeUInt32LE(crc, 14); + local.writeUInt32LE(data.length, 18); + local.writeUInt32LE(data.length, 22); + local.writeUInt16LE(name.length, 26); + const central = Buffer.alloc(46); + central.writeUInt32LE(0x02014b50, 0); + central.writeUInt16LE(20, 4); + central.writeUInt16LE(20, 6); + central.writeUInt32LE(crc, 16); + central.writeUInt32LE(data.length, 20); + central.writeUInt32LE(data.length, 24); + central.writeUInt16LE(name.length, 28); + central.writeUInt32LE(offset, 42); + records.push(local, name, data); + directory.push(central, name); + offset += local.length + name.length + data.length; + } + const centralDirectory = Buffer.concat(directory); + const end = Buffer.alloc(22); + end.writeUInt32LE(0x06054b50, 0); + end.writeUInt16LE(entries.length, 8); + end.writeUInt16LE(entries.length, 10); + end.writeUInt32LE(centralDirectory.length, 12); + end.writeUInt32LE(offset, 16); + return Buffer.concat([...records, centralDirectory, end]); +}; + +const FILES = ["cua-driver.exe", "cua-driver-uia.exe", "cua-cursor-theme.exe"]; +const archive = makeZip(FILES.map((name) => ({ name, data: name }))); + +const makeHarness = Effect.fn("test.makeCuaDriverInstallation")(function* ( + options: { readonly sha256?: string; readonly archive?: Buffer } = {}, +) { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const digest = yield* Digest.Digest; + const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cua-install-test-" }); + const body = options.archive ?? archive; + const requests: Array = []; + const installation = yield* CuaDriverInstallation.makeCuaDriverInstallation({ + baseDir, + release: { + version: "9.9.9", + archiveName: "cua-driver-fixture.zip", + format: "zip", + url: "https://github.com/trycua/cua/releases/download/fixture.zip", + bytes: body.byteLength, + sha256: options.sha256 ?? Hex.encode(yield* digest.digest(Digest.sha256, body)), + executable: "cua-driver.exe", + requiredFiles: FILES, + }, + }).pipe( + Effect.provideService(HostProcess.Platform, "win32"), + Effect.provideService(HostProcess.Architecture, "x64"), + Effect.provideService( + HttpClient.HttpClient, + HttpClient.make((request) => + Effect.sync(() => { + requests.push(request.url); + const response = HttpClientResponse.fromWeb(request, new Response(null)); + return Object.defineProperty(response, "stream", { value: Stream.make(body) }); + }), + ), + ), + ); + return { + installation, + fs, + path, + installRoot: path.join(baseDir, "tools", "cua-driver"), + requests, + }; +}); + +it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("CuaDriverInstallation", (it) => { + it.effect("installs the verified release once and replaces older versions", () => + Effect.gen(function* () { + const { installation, fs, path, installRoot, requests } = yield* makeHarness(); + yield* fs.makeDirectory(path.join(installRoot, "0.1.0"), { recursive: true }); + + const executable = yield* installation.executable; + + expect(executable).toBe(path.join(installRoot, "9.9.9", "cua-driver.exe")); + expect(yield* fs.readFileString(path.join(installRoot, "9.9.9", "cua-driver-uia.exe"))).toBe( + "cua-driver-uia.exe", + ); + expect(yield* fs.readDirectory(installRoot)).toEqual(["9.9.9"]); + expect(yield* installation.executable).toBe(executable); + expect(requests).toHaveLength(1); + }).pipe(Effect.scoped), + ); + + it.effect("installs nothing from a download that fails its checksum", () => + Effect.gen(function* () { + const { installation, fs, installRoot } = yield* makeHarness({ sha256: "0".repeat(64) }); + + const error = yield* Effect.flip(installation.executable); + + expect(error.message).toContain("SHA-256"); + expect(yield* fs.readDirectory(installRoot)).toEqual([]); + }).pipe(Effect.scoped), + ); + + it.effect("rejects an archive entry that escapes the install directory", () => + Effect.gen(function* () { + const unsafe = makeZip([ + ...FILES.map((name) => ({ name, data: name })), + { name: "../escape.exe", data: "x" }, + ]); + const { installation, fs, path, installRoot } = yield* makeHarness({ archive: unsafe }); + + // yauzl's strict file names or the install's own check refuses it. + yield* Effect.flip(installation.executable); + + expect(yield* fs.exists(path.join(path.dirname(installRoot), "escape.exe"))).toBe(false); + expect(yield* fs.readDirectory(installRoot)).toEqual([]); + }).pipe(Effect.scoped), + ); +}); diff --git a/apps/server/src/cua/CuaDriverInstallation.ts b/apps/server/src/cua/CuaDriverInstallation.ts new file mode 100644 index 0000000000000..ab43f0484a47f --- /dev/null +++ b/apps/server/src/cua/CuaDriverInstallation.ts @@ -0,0 +1,239 @@ +// @effect-diagnostics nodeBuiltinImport:off - Effect has no incremental digest. +import * as EffectNodeStream from "@effect/platform-node/NodeStream"; +import { cuaDriverRelease, type CuaDriverRelease } from "@t3tools/shared/cuaDriverRelease"; +import * as HostProcess from "@t3tools/shared/HostProcess"; +import * as Context from "effect/Context"; +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; +import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; +import * as Path from "effect/Path"; +import * as Schema from "effect/Schema"; +import * as Semaphore from "effect/Semaphore"; +import * as Stream from "effect/Stream"; +import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http"; +import { ChildProcess, ChildProcessSpawner } from "effect/process"; +import * as NodeCrypto from "node:crypto"; + +import * as ServerConfig from "../config.ts"; +import { openZipArchive } from "../zipArchive.ts"; + +export class CuaDriverInstallError extends Schema.TaggedError()( + "CuaDriverInstallError", + { + detail: Schema.String, + cause: Schema.optional(Schema.Defect()), + }, +) { + override get message() { + return this.detail; + } +} +const isInstallError = Schema.is(CuaDriverInstallError); +const wrapFailure = (detail: string) => (cause: unknown) => + isInstallError(cause) ? cause : new CuaDriverInstallError({ detail, cause }); + +/** + * The pinned Cua Driver for a server that runs without the desktop app. The + * desktop bundles the same release in its resources; a standalone `t3` server + * downloads it into T3's home the first time a session needs it. + */ +export class CuaDriverInstallation extends Context.Service< + CuaDriverInstallation, + { + /** Path of the installed executable, installing it first when missing. */ + readonly executable: Effect.Effect; + } +>()("t3/cua/CuaDriverInstallation") {} + +export const makeCuaDriverInstallation = Effect.fn("CuaDriverInstallation.make")( + function* (options: { readonly baseDir: string; readonly release?: CuaDriverRelease | null }) { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const http = yield* HttpClient.HttpClient; + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; + const platform = yield* HostProcess.Platform; + const arch = yield* HostProcess.Architecture; + const release = + options.release === undefined ? cuaDriverRelease(platform, arch) : options.release; + const installRoot = path.join(options.baseDir, "tools", "cua-driver"); + const gate = yield* Semaphore.make(1); + + // The version directory appears by one rename of a fully unpacked tree, so + // its executable is the complete-install marker. + const installed = (release: CuaDriverRelease) => { + const executable = path.join(installRoot, release.version, release.executable); + return fs.exists(executable).pipe( + Effect.map((exists) => (exists ? Option.some(executable) : Option.none())), + Effect.orElseSucceed(() => Option.none()), + ); + }; + + const download = Effect.fn("CuaDriverInstallation.download")( + function* (release: CuaDriverRelease, archivePath: string) { + const response = yield* http + .execute(HttpClientRequest.get(release.url)) + .pipe(Effect.flatMap(HttpClientResponse.filterStatusOk)); + const hash = NodeCrypto.createHash("sha256"); + let bytes = 0; + yield* response.stream.pipe( + Stream.tap((chunk) => + Effect.suspend(() => { + bytes += chunk.byteLength; + if (bytes > release.bytes) { + return Effect.fail( + new CuaDriverInstallError({ + detail: "The Cua Driver download was larger than the pinned release.", + }), + ); + } + hash.update(chunk); + return Effect.void; + }), + ), + Stream.run(fs.sink(archivePath, { flag: "wx", mode: 0o600 })), + ); + if (bytes !== release.bytes || hash.digest("hex") !== release.sha256) { + return yield* new CuaDriverInstallError({ + detail: + "The Cua Driver download failed its size or SHA-256 check. Nothing was installed.", + }); + } + }, + Effect.timeout("10 minutes"), + Effect.mapError( + wrapFailure( + "Could not download Cua Driver from GitHub. Check this machine's network access.", + ), + ), + ); + + const unsafeEntry = new CuaDriverInstallError({ + detail: "The Cua Driver archive contains an unexpected or unsafe entry.", + }); + const isSafeRelative = (relative: string) => { + const parts = relative.replace(/\/$/u, "").split("/"); + return ( + relative.length > 0 && + !relative.startsWith("/") && + !relative.includes("\\") && + parts.every((part) => part !== "" && part !== "." && part !== ".." && !part.includes(":")) + ); + }; + + // Linux and macOS archives are flat tarballs; `tar` lists them first so an + // entry outside the destination never reaches the disk. + const extractTar = Effect.fn("CuaDriverInstallation.extractTar")(function* ( + archivePath: string, + destination: string, + ) { + const run = (args: ReadonlyArray) => + spawner + .string(ChildProcess.make("tar", args, { stdin: "ignore", stderr: "ignore" })) + .pipe(Effect.mapError(wrapFailure("Could not unpack Cua Driver with tar."))); + const names = (yield* run(["-tzf", archivePath])).trim().split("\n"); + const types = (yield* run(["-tvzf", archivePath])).trim().split("\n"); + if ( + names.length > 1_000 || + names.length !== types.length || + types.some((line) => !["-", "d"].includes(line[0] ?? "")) || + !names.every(isSafeRelative) + ) { + return yield* unsafeEntry; + } + yield* fs.makeDirectory(destination); + yield* run(["-xzf", archivePath, "-C", destination]); + }); + + const extractZip = Effect.fn("CuaDriverInstallation.extractZip")(function* ( + archivePath: string, + destination: string, + ) { + const archive = yield* openZipArchive( + archivePath, + (detail, cause) => new CuaDriverInstallError({ detail, cause }), + ); + yield* fs.makeDirectory(destination); + for (;;) { + const entry = yield* archive.next; + if (!entry) break; + if (!isSafeRelative(entry.fileName) || (entry.generalPurposeBitFlag & 1) !== 0) { + return yield* unsafeEntry; + } + const target = path.join(destination, entry.fileName); + if (entry.fileName.endsWith("/")) { + yield* fs.makeDirectory(target, { recursive: true }); + continue; + } + yield* fs.makeDirectory(path.dirname(target), { recursive: true }); + yield* Effect.gen(function* () { + const readable = yield* archive.streamEntry(entry); + yield* EffectNodeStream.fromReadable({ + evaluate: () => readable, + onError: wrapFailure("Could not unpack Cua Driver."), + }).pipe(Stream.run(fs.sink(target, { flag: "wx" }))); + }).pipe(Effect.scoped); + } + }, Effect.scoped); + + const install = Effect.fn("CuaDriverInstallation.install")( + function* (release: CuaDriverRelease) { + yield* fs.makeDirectory(installRoot, { recursive: true }); + // Staging shares the install root's filesystem so publishing is one rename. + const staging = yield* fs.makeTempDirectoryScoped({ + directory: installRoot, + prefix: ".install-", + }); + const archivePath = path.join(staging, release.archiveName); + const unpacked = path.join(staging, "driver"); + yield* download(release, archivePath); + yield* release.format === "zip" + ? extractZip(archivePath, unpacked) + : extractTar(archivePath, unpacked); + for (const file of release.requiredFiles) { + if (!(yield* fs.exists(path.join(unpacked, file)))) { + return yield* new CuaDriverInstallError({ + detail: `The Cua Driver archive is missing ${file}.`, + }); + } + } + const destination = path.join(installRoot, release.version); + yield* fs.remove(destination, { recursive: true, force: true }); + yield* fs.rename(unpacked, destination); + // Older versions are never in use once this one is published. + for (const name of yield* fs.readDirectory(installRoot)) { + if (name !== release.version && !name.startsWith(".install-")) { + yield* fs.remove(path.join(installRoot, name), { recursive: true, force: true }); + } + } + return path.join(destination, release.executable); + }, + Effect.scoped, + Effect.mapError(wrapFailure("Could not save Cua Driver in T3's home directory.")), + ); + + const executable = gate.withPermit( + Effect.gen(function* () { + if (release === null) { + return yield* new CuaDriverInstallError({ + detail: `Cua publishes no driver for ${platform}-${arch}.`, + }); + } + const existing = yield* installed(release); + if (Option.isSome(existing)) return existing.value; + yield* Effect.logInfo("Installing Cua Driver.", { version: release.version }); + return yield* install(release); + }), + ); + + return CuaDriverInstallation.of({ executable }); + }, +); + +export const layer = Layer.effect( + CuaDriverInstallation, + Effect.gen(function* () { + const config = yield* ServerConfig.ServerConfig; + return yield* makeCuaDriverInstallation({ baseDir: config.baseDir }); + }), +); diff --git a/apps/server/src/resourceTelemetry/DesktopTelemetryReceiver.ts b/apps/server/src/resourceTelemetry/DesktopTelemetryReceiver.ts index ca77c9e26220c..3b4f1b2c3ba12 100644 --- a/apps/server/src/resourceTelemetry/DesktopTelemetryReceiver.ts +++ b/apps/server/src/resourceTelemetry/DesktopTelemetryReceiver.ts @@ -6,6 +6,7 @@ import * as NodeTls from "node:tls"; import * as NodeStream from "@effect/platform-node/NodeStream"; import { DesktopHostTelemetryMessage, + type DesktopCuaDriverReport, type DesktopHostTelemetryMessage as DesktopHostTelemetryMessageValue, type DesktopHostTelemetrySnapshot, type DesktopShellEnvironmentSnapshot, @@ -184,6 +185,11 @@ export class DesktopTelemetryReceiver extends Context.Service< never, Scope.Scope >; + readonly cuaReports: Stream.Stream; + readonly requestCuaDriver: ( + requestId: string, + enabled: boolean, + ) => Effect.Effect; readonly setDiagnosticsDemand: ( enabled: boolean, ) => Effect.Effect; @@ -421,6 +427,7 @@ export const make = Effect.fn("resourceTelemetry.desktopTelemetryReceiver.make") const changes = yield* PubSub.sliding(8); const healthChanges = yield* PubSub.sliding(4); const latestUpdateReport = yield* Ref.make(Option.none()); + const cuaReports = yield* PubSub.unbounded(); const updateReportChanges = yield* PubSub.sliding(16); const controlMutex = yield* Semaphore.make(1); const snapshotMutex = yield* Semaphore.make(1); @@ -602,6 +609,13 @@ export const make = Effect.fn("resourceTelemetry.desktopTelemetryReceiver.make") return recordContact.pipe(Effect.andThen(applyShellSnapshot(message))); } + if (message.type === "cuaDriverReport") { + return recordContact.pipe( + Effect.andThen(PubSub.publish(cuaReports, message)), + Effect.asVoid, + ); + } + if (message.type === "desktopUpdateStatus") { return recordContact.pipe( Effect.andThen(Ref.set(latestUpdateReport, Option.some(message))), @@ -718,6 +732,12 @@ export const make = Effect.fn("resourceTelemetry.desktopTelemetryReceiver.make") ), health: Ref.get(health), subscribeHealth: subscribeBeforeSnapshotWithoutMutex(healthChanges, Ref.get(health)), + cuaReports: Stream.fromPubSub(cuaReports), + requestCuaDriver: Effect.fn("DesktopTelemetryReceiver.requestCuaDriver")( + function (requestId, enabled) { + return sendControlMessage({ version: 1, type: "cuaDriverRequest", requestId, enabled }); + }, + ), setDiagnosticsDemand, announceServerReady: sendControlMessage({ version: 1, type: "serverReady" }).pipe( Effect.withSpan("DesktopTelemetryReceiver.announceServerReady"), @@ -789,6 +809,10 @@ export const layerTest = ( changes: Stream.empty, })), ), + cuaReports: Stream.empty, + requestCuaDriver: Effect.fn("DesktopTelemetryReceiver.requestCuaDriver")(function () { + return Effect.void; + }), setDiagnosticsDemand: Effect.fn("DesktopTelemetryReceiver.setDiagnosticsDemand")(function () { return Effect.void; }), diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts index 29e3143cbc638..4124df4e9da15 100644 --- a/apps/server/src/server.ts +++ b/apps/server/src/server.ts @@ -202,6 +202,7 @@ import * as ProcessResourceMonitor from "./diagnostics/ProcessResourceMonitor.ts import * as TraceDiagnostics from "./diagnostics/TraceDiagnostics.ts"; import * as TriageService from "./triage/TriageService.ts"; import * as DesktopTelemetryReceiver from "./resourceTelemetry/DesktopTelemetryReceiver.ts"; +import * as CuaDriver from "./cua/CuaDriver.ts"; import * as NativeTelemetryClient from "./resourceTelemetry/NativeTelemetryClient.ts"; import * as ResourceAttribution from "./resourceTelemetry/ResourceAttribution.ts"; import * as ResourceMonitorBinary from "./resourceTelemetry/ResourceMonitorBinary.ts"; @@ -302,6 +303,13 @@ const layerDesktopAppUpdate = DesktopAppUpdate.layer.pipe( Layer.provide(layerDesktopTelemetryReceiver), ); +// The environment's one Cua Driver host. It shares the receiver because the +// desktop backend asks its Electron parent for the driver over that fd. +const layerCuaDriver = CuaDriver.layer.pipe( + Layer.provide(layerServerSettings), + Layer.provide(layerDesktopTelemetryReceiver), +); + const layerBackground = BackgroundPolicy.layer.pipe( Layer.provide(layerHostPowerMonitor), Layer.provideMerge(layerServerSettings), @@ -760,6 +768,7 @@ const layerRuntimeCoreDependenciesBase = Layer.mergeAll( Layer.provide(layerServerSettings), ), CodexInstallation.CodexInstallation.layer, + layerCuaDriver, ), ), ); diff --git a/knip.jsonc b/knip.jsonc index 748d6864e2217..27554d9518bf2 100644 --- a/knip.jsonc +++ b/knip.jsonc @@ -68,12 +68,14 @@ // The Vite+ web build prerequisite. // Native and disk-loaded provider packages stay external to the bundle; the // CLI archive and desktop sidecar stage them from this manifest. + // @ubjs/node: direct only so staged release installs carry its asar patch. "ignoreDependencies": [ "@effect/platform-node-shared", "@t3tools/web", "@napi-rs/keyring", "ffi-rs", "transcribe-cpp", + "@ubjs/node", ], }, "apps/desktop": { @@ -96,7 +98,8 @@ ], // The injected runtime resolves Playwright by string; release tooling runs // electron-builder from this workspace, outside its package scripts. - "ignoreDependencies": ["playwright-core", "electron-builder"], + // @ubjs/node: direct only so staged release installs carry its asar patch. + "ignoreDependencies": ["playwright-core", "electron-builder", "@ubjs/node"], }, "apps/web": { // Worktree setup (scripts/setup-worktree.ts) runs this directly, and evidence diff --git a/packages/contracts/src/cua.ts b/packages/contracts/src/cua.ts new file mode 100644 index 0000000000000..c3734c1930322 --- /dev/null +++ b/packages/contracts/src/cua.ts @@ -0,0 +1,39 @@ +import * as Schema from "effect/Schema"; + +import { TrimmedNonEmptyString } from "./baseSchemas.ts"; + +/** Native host descriptor, confined to the desktop/server control pipe. */ +export const CuaDriverMcpConfiguration = Schema.Struct({ + command: TrimmedNonEmptyString, + args: Schema.Array(Schema.String), + environment: Schema.Array(Schema.Struct({ name: TrimmedNonEmptyString, value: Schema.String })), + /** The daemon's socket, so the server can attach its own SDK client beside the agent's MCP proxy. */ + socketPath: Schema.optionalKey(TrimmedNonEmptyString), +}); +export type CuaDriverMcpConfiguration = typeof CuaDriverMcpConfiguration.Type; + +export const DesktopCuaDriverRequest = Schema.Struct({ + version: Schema.Literal(1), + type: Schema.Literal("cuaDriverRequest"), + requestId: TrimmedNonEmptyString, + enabled: Schema.Boolean, +}); +export type DesktopCuaDriverRequest = typeof DesktopCuaDriverRequest.Type; + +export const DesktopCuaDriverReport = Schema.Union([ + Schema.Struct({ + version: Schema.Literal(1), + type: Schema.Literal("cuaDriverReport"), + requestId: TrimmedNonEmptyString, + status: Schema.Literal("ready"), + mcp: CuaDriverMcpConfiguration, + }), + Schema.Struct({ + version: Schema.Literal(1), + type: Schema.Literal("cuaDriverReport"), + requestId: TrimmedNonEmptyString, + status: Schema.Literals(["stopped", "unavailable"]), + message: Schema.optionalKey(Schema.String), + }), +]); +export type DesktopCuaDriverReport = typeof DesktopCuaDriverReport.Type; diff --git a/packages/contracts/src/index.ts b/packages/contracts/src/index.ts index c005fdd03900d..9a4c1d9f0ca9f 100644 --- a/packages/contracts/src/index.ts +++ b/packages/contracts/src/index.ts @@ -69,6 +69,7 @@ export * from "./scheduledTask.ts"; export * from "./worktreeMcp.ts"; export * from "./resourceTelemetry.ts"; export * from "./rpc.ts"; +export * from "./cua.ts"; export * from "./worktreeSetup.ts"; export * from "./secretRequest.ts"; export * from "./secretReference.ts"; diff --git a/packages/contracts/src/ipc.ts b/packages/contracts/src/ipc.ts index b44f8e77f239a..ff0dc0874e6cd 100644 --- a/packages/contracts/src/ipc.ts +++ b/packages/contracts/src/ipc.ts @@ -1053,7 +1053,11 @@ export const DesktopMediaReadSchema = Schema.Struct({ }); export type DesktopMediaRead = typeof DesktopMediaReadSchema.Type; -export const SystemSettingsPaneSchema = Schema.Literals(["full-disk-access"]); +export const SystemSettingsPaneSchema = Schema.Literals([ + "full-disk-access", + "accessibility", + "screen-recording", +]); export type SystemSettingsPane = typeof SystemSettingsPaneSchema.Type; export interface DesktopBridge { @@ -1098,6 +1102,8 @@ export interface DesktopBridge { requestSnapShotPermissions: (includeAccessibility: boolean) => Promise; getSnapShotState: () => Promise; setupSnapShot: (action: DesktopSnapShotSetupAction) => Promise; + /** Replaces a running Cua Driver host so newly granted macOS permissions apply. */ + restartCuaDriver: () => Promise; previewSnapShotConfig: ( request: DesktopCaptureConfigRequest, ) => Promise; diff --git a/packages/contracts/src/resourceTelemetry.ts b/packages/contracts/src/resourceTelemetry.ts index ff6fdc25997e4..0c33aed2d6bed 100644 --- a/packages/contracts/src/resourceTelemetry.ts +++ b/packages/contracts/src/resourceTelemetry.ts @@ -2,6 +2,7 @@ import * as Schema from "effect/Schema"; import { NonNegativeInt, PositiveInt, TrimmedNonEmptyString } from "./baseSchemas.ts"; import { HostPowerSnapshot } from "./background.ts"; +import { DesktopCuaDriverReport, DesktopCuaDriverRequest } from "./cua.ts"; import { DesktopUpdateChannelSchema, DesktopUpdateStateSchema } from "./ipc.ts"; export const RESOURCE_MONITOR_PROTOCOL_VERSION = 3 as const; @@ -339,6 +340,7 @@ export const DesktopShellEnvironmentSnapshot = Schema.Struct({ export type DesktopShellEnvironmentSnapshot = typeof DesktopShellEnvironmentSnapshot.Type; export const DesktopHostTelemetryMessage = Schema.Union([ + DesktopCuaDriverReport, DesktopHostTelemetryHello, DesktopShellEnvironmentSnapshot, DesktopHostTelemetrySnapshot, @@ -396,6 +398,7 @@ export type DesktopTelemetryServerReady = typeof DesktopTelemetryServerReady.Typ export const DesktopTelemetryControlMessage = Schema.Union([ DesktopTelemetryServerReady, + DesktopCuaDriverRequest, DesktopTelemetrySetDiagnosticsDemand, DesktopTelemetrySetHostPowerIntervals, DesktopTelemetryRequestDesktopUpdate, diff --git a/packages/contracts/src/settings.ts b/packages/contracts/src/settings.ts index 678f58294c66a..4f9be26eb4870 100644 --- a/packages/contracts/src/settings.ts +++ b/packages/contracts/src/settings.ts @@ -1412,6 +1412,8 @@ export const ServerSettings = Schema.Struct({ speechPostProcessingModelSelection: Schema.NullOr(ModelSelection).pipe( Schema.withDecodingDefault(Effect.succeed(null)), ), + /** Environment-owned opt-in for managed agent access to Cua Driver. */ + enableCua: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))), projectAgentBrowserAccessOverrides: Schema.Record(ProjectId, Schema.Boolean).pipe( Schema.withDecodingDefault(Effect.succeed({})), ), @@ -1870,6 +1872,7 @@ export const ServerSettingsPatch = Schema.Struct({ speechModelUnloadTimeout: Schema.optionalKey(SpeechModelUnloadTimeout), speechProjectCustomWords: Schema.optionalKey(SpeechCustomWords), speechPostProcessingModelSelection: Schema.optionalKey(Schema.NullOr(ModelSelection)), + enableCua: Schema.optionalKey(Schema.Boolean), projectAgentBrowserAccessOverrides: Schema.optionalKey( Schema.Record(ProjectId, Schema.NullOr(Schema.Boolean)), ), diff --git a/packages/shared/package.json b/packages/shared/package.json index 781d1a4e125e1..38d187377bf83 100644 --- a/packages/shared/package.json +++ b/packages/shared/package.json @@ -630,6 +630,10 @@ "./paletteSearchCopy": { "types": "./src/paletteSearchCopy.ts", "import": "./src/paletteSearchCopy.ts" + }, + "./cuaDriverRelease": { + "types": "./src/cuaDriverRelease.ts", + "import": "./src/cuaDriverRelease.ts" } }, "scripts": { diff --git a/packages/shared/src/cuaDriverRelease.ts b/packages/shared/src/cuaDriverRelease.ts new file mode 100644 index 0000000000000..acbe5915dc367 --- /dev/null +++ b/packages/shared/src/cuaDriverRelease.ts @@ -0,0 +1,71 @@ +/** + * The Cua Driver release T3 bundles and downloads. The catalog's SDK may trail it; it checks only + * contract and protocol versions. To bump, copy each size and SHA-256 from the release assets. + */ +export const CUA_DRIVER_VERSION = "0.38.0"; + +const ARCHIVES = { + "darwin-universal": { + archiveName: `cua-driver-rs-${CUA_DRIVER_VERSION}-darwin-universal-binary.tar.gz`, + bytes: 53_208_248, + sha256: "2bb8dc4a7e69185ffd04c4a9c0eab590bd2c54b72c2c1be13ab675be4f487732", + }, + "linux-x64": { + archiveName: `cua-driver-rs-${CUA_DRIVER_VERSION}-linux-x86_64-binary.tar.gz`, + bytes: 37_183_089, + sha256: "31ab9709dd91ce73677e49b9f3e75c6f0d3e4d9625c286c9e58262bfbea698dd", + }, + "linux-arm64": { + archiveName: `cua-driver-rs-${CUA_DRIVER_VERSION}-linux-arm64-binary.tar.gz`, + bytes: 36_674_648, + sha256: "80c9ae66bc7322052dd426c6133c96086b33894bf06c972bca8215384400b33d", + }, + "win32-x64": { + archiveName: `cua-driver-rs-${CUA_DRIVER_VERSION}-windows-x86_64-binary.zip`, + bytes: 35_890_999, + sha256: "5edaa74d0afa1b55a9668dbc4f32736507bd2bd4d75b18ca1dc8881bb12402b5", + }, + "win32-arm64": { + archiveName: `cua-driver-rs-${CUA_DRIVER_VERSION}-windows-arm64-binary.zip`, + bytes: 33_682_556, + sha256: "d25c8ebfffb34e4c095ea170baf47b87c35dc0d1116f42e46b39df2bcbff8700", + }, +} as const; + +export interface CuaDriverRelease { + readonly version: string; + readonly archiveName: string; + readonly format: "tar.gz" | "zip"; + readonly url: string; + readonly bytes: number; + readonly sha256: string; + /** Executable name at the archive root. */ + readonly executable: string; + /** Files the archive must contain for the driver and its SDK to run. */ + readonly requiredFiles: ReadonlyArray; +} + +/** The archive for a host, or null where Cua publishes no driver. macOS uses one universal build. */ +export function cuaDriverRelease(platform: NodeJS.Platform, arch: string): CuaDriverRelease | null { + const key = + platform === "darwin" && (arch === "arm64" || arch === "x64") + ? "darwin-universal" + : (platform === "linux" || platform === "win32") && (arch === "x64" || arch === "arm64") + ? (`${platform}-${arch}` as const) + : null; + if (key === null) return null; + const archive = ARCHIVES[key]; + const windows = platform === "win32"; + return { + version: CUA_DRIVER_VERSION, + ...archive, + format: windows ? "zip" : "tar.gz", + url: `https://github.com/trycua/cua/releases/download/cua-driver-rs-v${CUA_DRIVER_VERSION}/${archive.archiveName}`, + executable: windows ? "cua-driver.exe" : "cua-driver", + requiredFiles: windows + ? ["cua-driver.exe", "cua-driver-uia.exe", "cua-cursor-theme.exe"] + : platform === "linux" + ? ["cua-driver", "cua-cursor-theme", "wayland-helper/winrects@cua/extension.js"] + : ["cua-driver", "cua-cursor-theme"], + }; +} diff --git a/patches/@ubjs__node@0.31.0-3.patch b/patches/@ubjs__node@0.31.0-3.patch new file mode 100644 index 0000000000000..c6f7821758446 --- /dev/null +++ b/patches/@ubjs__node@0.31.0-3.patch @@ -0,0 +1,22 @@ +diff --git a/typescript/dist/resolve-lib.js b/typescript/dist/resolve-lib.js +index e8837bb2337c42eaffe15942923b8af39fca2b1a..b0806b7ebb9b4995ee6a318b66875a7ee9471219 100644 +--- a/typescript/dist/resolve-lib.js ++++ b/typescript/dist/resolve-lib.js +@@ -166,5 +166,16 @@ function resolveNpmPackage(crateName, callerUrl, npmPackageBase, tripleStyle) { + attempted: [binaryPath], + }); + } +- return binaryPath; ++ return resolveUnpackedAsarPath(binaryPath); ++} ++// dlopen cannot read inside an Electron asar archive. Packagers move native ++// libraries to the archive's `.asar.unpacked` sibling, so load them from there. ++function resolveUnpackedAsarPath(binaryPath) { ++ const segments = binaryPath.split(node_path_1.sep); ++ const asarIndex = segments.findLastIndex((segment) => segment.endsWith(".asar")); ++ if (asarIndex === -1) ++ return binaryPath; ++ segments[asarIndex] = `${segments[asarIndex]}.unpacked`; ++ const unpackedPath = segments.join(node_path_1.sep); ++ return (0, node_fs_1.existsSync)(unpackedPath) ? unpackedPath : binaryPath; + } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index af0be20508e16..70df957024569 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -200,6 +200,9 @@ catalogs: '@pierre/highlights': specifier: 0.5.0 version: 0.5.0 + '@trycua/cua-driver': + specifier: 0.38.0 + version: 0.38.0 '@types/plist': specifier: 3.0.5 version: 3.0.5 @@ -300,6 +303,7 @@ overrides: app-builder-lib: 26.16.1 electron-updater: 6.8.9 expo-modules-core: 58.0.30 + '@ubjs/node': 0.31.0-3 '@exodus/bytes>@noble/hashes': 1.8.0 '@react-navigation/core': 7.24.0 unenv: 2.0.0-rc.24 @@ -375,6 +379,7 @@ patchedDependencies: '@react-native-ai/apple@0.12.0': 2d09870c2848d185cb05b53ed823a46e12dba519324d8dd8e584e28731990f9d '@react-native-menu/menu@2.0.0': 6872ba71b9847c5a0210b1b896923dadc3860faedfe28d9f062bcedfe786ef1d '@react-navigation/native-stack@7.21.0': 990d575444c4af2ce8218b3b7a8280fe244c03abbdb32b822a2e862f791dbf42 + '@ubjs/node@0.31.0-3': e97d224c08570d77cb1dbac63fbcde2cc62ec45e6466b508272bb174a6ad75d0 alchemy@2.0.0-beta.85: 07cc8c7e14b3b9af143ddd40a059f117c27e55139fbf20bd5dde2eb30509cedd app-builder-lib@26.16.1: 0fbe9ae51783f5768ebe197d12d1fdb7d477a08c92e5db2f6356bcce62613c53 dbus-next@0.10.2: cfff57561b0ee59b5addb3b2e6c6f20906e967507a530ab67e8db8108e520ba4 @@ -454,6 +459,12 @@ importers: '@t3tools/tailscale': specifier: workspace:* version: link:../../packages/tailscale + '@trycua/cua-driver': + specifier: 'catalog:' + version: 0.38.0 + '@ubjs/node': + specifier: 0.31.0-3 + version: 0.31.0-3(patch_hash=e97d224c08570d77cb1dbac63fbcde2cc62ec45e6466b508272bb174a6ad75d0) dbus-next: specifier: 0.10.2 version: 0.10.2(patch_hash=cfff57561b0ee59b5addb3b2e6c6f20906e967507a530ab67e8db8108e520ba4) @@ -937,6 +948,12 @@ importers: '@t3tools/source-control-gitlab': specifier: workspace:* version: link:../../packages/source-control-gitlab + '@trycua/cua-driver': + specifier: 'catalog:' + version: 0.38.0 + '@ubjs/node': + specifier: 0.31.0-3 + version: 0.31.0-3(patch_hash=e97d224c08570d77cb1dbac63fbcde2cc62ec45e6466b508272bb174a6ad75d0) character-entities: specifier: 2.0.2 version: 2.0.2 @@ -7315,6 +7332,41 @@ packages: cpu: [x64] os: [win32] + '@trycua/cua-driver-darwin-arm64@0.38.0': + resolution: {integrity: sha512-NjPX+f6l5ncd9wY+yEjn0TTmXrs/s+Fy9pYgyfnyNhAKYa2GUTLpp2y6IJHy6ngRFYsMc1dcq5rh0dgOyReXVA==} + cpu: [arm64] + os: [darwin] + + '@trycua/cua-driver-darwin-x64@0.38.0': + resolution: {integrity: sha512-30vcHF1boPbDbSo1sJOcxz7CfjU2yc9YhZAtPotXPgscsUOREtz4xTdRmEYc5swkPuswAobNFcoXSWyQRagejA==} + cpu: [x64] + os: [darwin] + + '@trycua/cua-driver-linux-arm64-gnu@0.38.0': + resolution: {integrity: sha512-Mltrj2TzwiNCQ3GyK3HPPYyrjQbz2DFvxUevq2DxDqBtYSihMWRuuw9C2K3GLKHmKl/Wc6+3F54d+YlYh+E65w==} + cpu: [arm64] + os: [linux] + libc: [glibc] + + '@trycua/cua-driver-linux-x64-gnu@0.38.0': + resolution: {integrity: sha512-TADFP+6E9IXdRtAx1pPIFXyu2iumkolfW5WLWFKlF/QITXj3MmY+OHuBXWX+46OLQVMSEdxjsQAWU/wMiK/ixQ==} + cpu: [x64] + os: [linux] + libc: [glibc] + + '@trycua/cua-driver-win32-arm64-msvc@0.38.0': + resolution: {integrity: sha512-QiUlocMQpj8hffaoqlsQOgbRSmIxTMYp9bbPHqSubsOqUlQqp57nkRe92LvfGkFhhbExp58NXHgDypmJaDPpwg==} + cpu: [arm64] + os: [win32] + + '@trycua/cua-driver-win32-x64-msvc@0.38.0': + resolution: {integrity: sha512-fP+4QrWYaz4nyald7dbo/uEEFz88lMxqsnjOh4RKlAVOQlz5wmCjVpUKkuMtCQeDVdJc27iJfQYeZaHWRPYd8A==} + cpu: [x64] + os: [win32] + + '@trycua/cua-driver@0.38.0': + resolution: {integrity: sha512-VTjMW+yieglgarfPTOOA9BfArcx4AGT+Q6ALd3XaaLQfu3kWdmClj4g1PDo+eQK2KuebE1evF9XtEDV49Tw0UQ==} + '@ts-morph/common@0.29.0': resolution: {integrity: sha512-35oUmphHbJvQ/+UTwFNme/t2p3FoKiGJ5auTjjpNTop2dyREspirjMy82PLSC1pnDJ8ah1GU98hwpVt64YXQsg==} @@ -7485,6 +7537,56 @@ packages: '@types/yauzl@3.4.0': resolution: {integrity: sha512-NRPn5w6h8dhcnmx3YIRQcqMywY/+nND/uOkJessedcrowO3C0AssHp3tMJpxKAwOhFOo0OV1y9VtsC5hbKKBAw==} + '@ubjs/core@0.31.0-3': + resolution: {integrity: sha512-39XrJgUZ2VVb561sSnkXPhczNoeBsNiSRArecsV0JE7CJq69ajFkcn9/tBAUS2NpgHkLIDU+z6Ks2+1wXnboxg==} + + '@ubjs/node-darwin-arm64@0.31.0-3': + resolution: {integrity: sha512-GGQVPLkVo4Gc8qVLW4IGvS8bjl8eHXyeP4a97ntGmsAdXwE5gsS29o8xUEFROjhwHKD+9sgVeblCSWVG3CpHsw==} + cpu: [arm64] + os: [darwin] + + '@ubjs/node-darwin-x64@0.31.0-3': + resolution: {integrity: sha512-2sc47u4XFYOsbmP5EW+Gx8m/yGrYnfFDFQm6+kz7goSWTNg84eEiz3COs9HKJDVuNJ5Khv5XipTO8CFadMLXCw==} + cpu: [x64] + os: [darwin] + + '@ubjs/node-linux-arm64-gnu@0.31.0-3': + resolution: {integrity: sha512-YStVXhYz/5jvlWf/p4fhiVT72unYAbGugifFC9QmO/+hnroQDAQ5t8SARbsc15G4olMcamdIB+GETiUB7gmaYg==} + cpu: [arm64] + os: [linux] + libc: [glibc] + + '@ubjs/node-linux-arm64-musl@0.31.0-3': + resolution: {integrity: sha512-Izp4nvfy/LmibzFowAztkoDOksCR2fb2zl6fh1ojR1HEsg0rAGruxtI8d3fn8DI0lBXwqmn6SF///oD4mFNJPQ==} + cpu: [arm64] + os: [linux] + libc: [musl] + + '@ubjs/node-linux-x64-gnu@0.31.0-3': + resolution: {integrity: sha512-Xdm21blyg5U/kW6s7OMvgrr8coGTkUlt26DVR9x8gKISif+E3YwdEskbFScWqystAiJnfjw7xHEc8UMu0Qlz7Q==} + cpu: [x64] + os: [linux] + libc: [glibc] + + '@ubjs/node-linux-x64-musl@0.31.0-3': + resolution: {integrity: sha512-fFQ9BWS6i2LUH9SJgD9oEiKXXo/say59vHy7usFe1t7C2xvwvP34f5SuxXmWP9R8fHyA0aC4kIZ+TTwyHSv1Kw==} + cpu: [x64] + os: [linux] + libc: [musl] + + '@ubjs/node-win32-arm64-msvc@0.31.0-3': + resolution: {integrity: sha512-ID6rSz1NmPsWTNBBNAw4OnJ5Dj8pcbtNJtdPB3OxcGigLBd/e0x7buhSI7os6Mo5iYtEdCynBRQHFJwub5XSPg==} + cpu: [arm64] + os: [win32] + + '@ubjs/node-win32-x64-msvc@0.31.0-3': + resolution: {integrity: sha512-wevs+Y+szwcCUT8IJFbB4/1nfxyRv/51l8oG7FGUPWD1xLPyuHfJBG27C+PDeC7KRzes/2n6aLo4DGZbr/LYTw==} + cpu: [x64] + os: [win32] + + '@ubjs/node@0.31.0-3': + resolution: {integrity: sha512-qNMpi2LICNwxGXZyRF8fSDBSpbezyZbEsydrbiMPJOmtOWr4tmZIEl7jkWGHVGShoBvHfFo4eHp5B4UVP928Cg==} + '@ungap/structured-clone@1.3.1': resolution: {integrity: sha512-mUFwbeTqrVgDQxFveS+df2yfap6iuP20NAKAsBt5jDEoOTDew+zwLAOilHCeQJOVSvmgCX4ogqIrA0mnyr08yQ==} @@ -17352,6 +17454,36 @@ snapshots: '@transcribe-cpp/win32-x64-cpu-vulkan@0.4.1': optional: true + '@trycua/cua-driver-darwin-arm64@0.38.0': + optional: true + + '@trycua/cua-driver-darwin-x64@0.38.0': + optional: true + + '@trycua/cua-driver-linux-arm64-gnu@0.38.0': + optional: true + + '@trycua/cua-driver-linux-x64-gnu@0.38.0': + optional: true + + '@trycua/cua-driver-win32-arm64-msvc@0.38.0': + optional: true + + '@trycua/cua-driver-win32-x64-msvc@0.38.0': + optional: true + + '@trycua/cua-driver@0.38.0': + dependencies: + '@ubjs/core': 0.31.0-3 + '@ubjs/node': 0.31.0-3(patch_hash=e97d224c08570d77cb1dbac63fbcde2cc62ec45e6466b508272bb174a6ad75d0) + optionalDependencies: + '@trycua/cua-driver-darwin-arm64': 0.38.0 + '@trycua/cua-driver-darwin-x64': 0.38.0 + '@trycua/cua-driver-linux-arm64-gnu': 0.38.0 + '@trycua/cua-driver-linux-x64-gnu': 0.38.0 + '@trycua/cua-driver-win32-arm64-msvc': 0.38.0 + '@trycua/cua-driver-win32-x64-msvc': 0.38.0 + '@ts-morph/common@0.29.0': dependencies: minimatch: 10.2.6 @@ -17554,6 +17686,43 @@ snapshots: dependencies: '@types/node': 24.12.4 + '@ubjs/core@0.31.0-3': {} + + '@ubjs/node-darwin-arm64@0.31.0-3': + optional: true + + '@ubjs/node-darwin-x64@0.31.0-3': + optional: true + + '@ubjs/node-linux-arm64-gnu@0.31.0-3': + optional: true + + '@ubjs/node-linux-arm64-musl@0.31.0-3': + optional: true + + '@ubjs/node-linux-x64-gnu@0.31.0-3': + optional: true + + '@ubjs/node-linux-x64-musl@0.31.0-3': + optional: true + + '@ubjs/node-win32-arm64-msvc@0.31.0-3': + optional: true + + '@ubjs/node-win32-x64-msvc@0.31.0-3': + optional: true + + '@ubjs/node@0.31.0-3(patch_hash=e97d224c08570d77cb1dbac63fbcde2cc62ec45e6466b508272bb174a6ad75d0)': + optionalDependencies: + '@ubjs/node-darwin-arm64': 0.31.0-3 + '@ubjs/node-darwin-x64': 0.31.0-3 + '@ubjs/node-linux-arm64-gnu': 0.31.0-3 + '@ubjs/node-linux-arm64-musl': 0.31.0-3 + '@ubjs/node-linux-x64-gnu': 0.31.0-3 + '@ubjs/node-linux-x64-musl': 0.31.0-3 + '@ubjs/node-win32-arm64-msvc': 0.31.0-3 + '@ubjs/node-win32-x64-msvc': 0.31.0-3 + '@ungap/structured-clone@1.3.1': {} '@vercel/config@0.3.0': diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 04f881d6f124d..d7be946d3dafc 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -62,6 +62,8 @@ catalog: "@tailwindcss/node": 4.3.3 "@tailwindcss/oxide": 4.3.3 "@tailwindcss/vite": 4.3.3 + "@trycua/cua-driver": 0.38.0 + "@ubjs/node": 0.31.0-3 "@types/node": 24.12.4 "@types/plist": 3.0.5 "@types/react": ~19.3.0 @@ -132,6 +134,7 @@ overrides: app-builder-lib: 26.16.1 electron-updater: 6.8.9 expo-modules-core: 58.0.30 + "@ubjs/node": "catalog:" # Keep jsdom's optional peer consistent so Vite+ has one test-runner instance. "@exodus/bytes>@noble/hashes": "1.8.0" # Earlier React Navigation declarations create a circular type alias in TypeScript 7. @@ -284,6 +287,7 @@ patchedDependencies: react-native-svg@15.16.1: patches/react-native-svg@15.16.1.patch # Android: double-tap zoom no longer cancels itself, and taps near a link open it. react-native-pdf-jsi@6.1.0: patches/react-native-pdf-jsi@6.1.0.patch + "@ubjs/node@0.31.0-3": patches/@ubjs__node@0.31.0-3.patch # Unmet peers fail resolution, so a pin outside a dependency's peer range is # caught when the lockfile changes instead of scrolling past as a warning. diff --git a/scripts/build-desktop-artifact.test.ts b/scripts/build-desktop-artifact.test.ts index c9180ec3d3ae6..bc0ac40a8747c 100644 --- a/scripts/build-desktop-artifact.test.ts +++ b/scripts/build-desktop-artifact.test.ts @@ -25,6 +25,11 @@ import { buildDesktopBundles, copyDesktopBuildArtifacts, DesktopBuildManifestRestoreError, + CuaDriverBundleMissingFileError, + CuaDriverChecksumMismatchError, + resolveCuaDriverAsset, + stageCuaDriverBundle, + stageCuaDriverExecutable, BundleNotSelfContainedError, BuildCommandFailedError, parseWslRuntimeArchiveMembers, @@ -243,6 +248,133 @@ const makePackagedAppFixture = Effect.fn("test.makePackagedAppFixture")(function }); it.layer(NodeServices.layer)("build-desktop-artifact", (it) => { + it.effect("rejects corrupt release downloads before extraction or staging", () => + Effect.scoped( + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cua-checksum-" }); + const commands: string[] = []; + const spawnerLayer = Layer.succeed( + ChildProcessSpawner.ChildProcessSpawner, + ChildProcessSpawner.make((command) => { + const child = command as unknown as { command: string; args: readonly string[] }; + commands.push(child.command); + const output = child.args[child.args.indexOf("--output") + 1]; + return child.command === "curl" && output !== undefined + ? Effect.as(fs.writeFileString(output, "corrupted archive"), mockProcess(0)) + : Effect.succeed(mockProcess(0)); + }), + ); + const error = yield* Effect.flip( + stageCuaDriverExecutable({ + platform: "linux", + arch: "x64", + repoRoot: root, + stageRoot: root, + stageResourcesDir: path.join(root, "resources"), + verbose: false, + }).pipe(Effect.provide(spawnerLayer)), + ); + assert.instanceOf(error, CuaDriverChecksumMismatchError); + assert.deepStrictEqual(commands, ["curl"]); + assert.isFalse(yield* fs.exists(path.join(root, "resources", "cua-driver"))); + const cache = path.join(root, "node_modules/.cache/t3code/cua-driver"); + assert.deepStrictEqual(yield* fs.readDirectory(cache), []); + }), + ), + ); + + it("bundles the universal Cua Driver on every macOS arch", () => { + for (const arch of ["arm64", "x64", "universal"] as const) { + assert.include(resolveCuaDriverAsset("mac", arch).archiveName, "darwin-universal-binary"); + } + for (const platform of ["linux", "win"] as const) { + assert.throws(() => resolveCuaDriverAsset(platform, "universal"), /no release asset/); + } + }); + + it.effect.each([ + { platform: "mac", arch: "universal" }, + { platform: "linux", arch: "x64" }, + { platform: "linux", arch: "arm64" }, + { platform: "win", arch: "x64" }, + { platform: "win", arch: "arm64" }, + ] as const)( + "stages the $platform/$arch release layout with its companions", + ({ platform, arch }) => + Effect.scoped( + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cua-bundle-" }); + const extractDir = path.join(root, "extract"); + const stageResourcesDir = path.join(root, "resources"); + // The real `-binary` archive layouts: every file sits at the root. + const members = + platform === "win" + ? [ + "cua-driver.exe", + "cua-driver-uia.exe", + "cua-cursor-theme.exe", + "cua_driver_sdk.dll", + "cua_driver_node_runtime.node", + ] + : platform === "linux" + ? [ + "cua-driver", + "cua-cursor-theme", + "libcua_driver_sdk.so", + "cua_driver_node_runtime.node", + "wayland-helper/winrects@cua/extension.js", + ] + : ["cua-driver", "cua-cursor-theme", "libcua_driver_sdk.dylib"]; + for (const member of [...members, "cua_driver_abi.h"]) { + const target = path.join(extractDir, member); + yield* fs.makeDirectory(path.dirname(target), { recursive: true }); + yield* fs.writeFileString(target, member); + } + yield* stageCuaDriverBundle({ platform, arch, extractDir, stageResourcesDir }); + const destination = path.join(stageResourcesDir, "cua-driver"); + if (platform === "mac") { + assert.equal(yield* fs.readFileString(destination), "cua-driver"); + } else { + for (const member of members) { + assert.equal(yield* fs.readFileString(path.join(destination, member)), member); + } + } + if ((yield* HostProcess.Platform) !== "win32" && platform !== "win") { + const binary = platform === "mac" ? destination : path.join(destination, "cua-driver"); + assert.equal((yield* fs.stat(binary)).mode & 0o777, 0o755); + } + }), + ), + ); + + it.effect("rejects an incomplete Windows bundle before publishing a staged CLI", () => + Effect.scoped( + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cua-incomplete-" }); + const extractDir = path.join(root, "extract"); + const stageResourcesDir = path.join(root, "resources"); + yield* fs.makeDirectory(extractDir); + yield* fs.writeFileString(path.join(extractDir, "cua-driver.exe"), "cli"); + const error = yield* Effect.flip( + stageCuaDriverBundle({ + platform: "win", + arch: "x64", + extractDir, + stageResourcesDir, + }), + ); + assert.instanceOf(error, CuaDriverBundleMissingFileError); + assert.isFalse(yield* fs.exists(path.join(stageResourcesDir, "cua-driver"))); + }), + ), + ); + it.effect("rejects overlapping builds from another process until restoration finishes", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; diff --git a/scripts/build-desktop-artifact.ts b/scripts/build-desktop-artifact.ts index 9d34e2412b327..95c5942f5a620 100644 --- a/scripts/build-desktop-artifact.ts +++ b/scripts/build-desktop-artifact.ts @@ -19,6 +19,7 @@ import { fromYaml } from "@t3tools/shared/schemaYaml"; import * as HostProcess from "@t3tools/shared/HostProcess"; import { clerkFrontendApiHostnameFromPublishableKey } from "@t3tools/shared/relayAuth"; import { resolveSpawnCommand } from "@t3tools/shared/shell"; +import { CUA_DRIVER_VERSION, cuaDriverRelease } from "@t3tools/shared/cuaDriverRelease"; import rootPackageJson from "../package.json" with { type: "json" }; import desktopPackageJson from "../apps/desktop/package.json" with { type: "json" }; import gnomeCaptureBundle from "../apps/desktop/gnome-extension/bundle.json" with { type: "json" }; @@ -176,6 +177,21 @@ const PLATFORM_CONFIG: Record = { }, }; +/** The pinned Cua Driver archive a desktop build bundles; macOS uses one universal build. */ +export function resolveCuaDriverAsset( + platform: typeof BuildPlatform.Type, + arch: typeof BuildArch.Type, +) { + const release = cuaDriverRelease( + platform === "mac" ? "darwin" : platform === "win" ? "win32" : "linux", + platform === "mac" ? "arm64" : arch, + ); + if (release === null) { + throw new Error(`Cua Driver has no release asset for ${platform}/${arch}.`); + } + return release; +} + interface BuildCliInput { readonly platform: Option.Option; readonly target: Option.Option; @@ -318,6 +334,19 @@ export class DesktopBuildManifestRestoreError extends Schema.TaggedError()( + "CuaDriverChecksumMismatchError", + { + archiveName: Schema.String, + expected: Schema.String, + actual: Schema.String, + }, +) { + override get message(): string { + return `Cua Driver checksum mismatch for ${this.archiveName}.`; + } +} + export class BuildCommandFailedError extends Schema.TaggedError()( "BuildCommandFailedError", { @@ -612,6 +641,42 @@ if (!result.ok) throw new Error(result.error); result.value.destroy(); `; +const WINDOWS_CUA_DRIVER_PROBE_TIMEOUT = Duration.seconds(60); +const WINDOWS_CUA_DRIVER_NO_SESSION = "requires an interactive Windows user session"; + +// Checks the bundled driver's version, loads the SDK and its unpacked natives from app.asar, +// then starts the driver through it as the desktop does and lists its tools. +const WINDOWS_CUA_DRIVER_PROBE_SOURCE = ` +const { execFileSync } = await import("node:child_process"); +const { pathToFileURL } = await import("node:url"); +const [embeddedPath, clientPath, binaryPath, expectedVersion] = process.argv.slice(1); +const version = execFileSync(binaryPath, ["--version"], { encoding: "utf8" }).trim(); +if (version !== "cua-driver " + expectedVersion) { + throw new Error("The bundled Cua Driver reports " + JSON.stringify(version) + ", not " + expectedVersion + "."); +} +const { EmbeddedCuaDriverHost } = await import(pathToFileURL(embeddedPath).href); +const { CuaDriver } = await import(pathToFileURL(clientPath).href); +const signal = new AbortController().signal; +const host = new EmbeddedCuaDriverHost(binaryPath, "com.t3tools.t3code.verify"); +try { + const connection = await host.start({ signal }); + const client = CuaDriver.connect(connection.socketPath); + try { + const { tools } = JSON.parse(await client.listToolsJson({ signal })); + if (!tools.some((tool) => tool.name === "get_window_state")) { + throw new Error("The bundled Cua Driver lists no get_window_state tool."); + } + console.log("Cua Driver " + connection.driverVersion + " started and listed " + tools.length + " tools."); + } finally { + await client.shutdown().catch(() => undefined); + client.uniffiDestroy(); + } +} finally { + await host.stop({ signal }).catch(() => undefined); + host.uniffiDestroy(); +} +`; + export class ExternalizedBundleError extends Schema.TaggedError()( "ExternalizedBundleError", { sentinel: Schema.String, inlinedPackageCount: Schema.Number }, @@ -744,6 +809,19 @@ export class WindowsPrimaryNativeProbeError extends Schema.TaggedError()( + "WindowsCuaDriverProbeError", + { + executablePath: Schema.String, + exitCode: Schema.Number, + output: Schema.String, + }, +) { + override get message(): string { + return `The packaged Windows app could not start its bundled Cua Driver (exit ${this.exitCode}). Output:\n${this.output}`; + } +} + const WindowsPackagedPayloadValidationReason = Schema.Literals([ "packaged-app-missing", "sidecar-missing", @@ -991,6 +1069,8 @@ export const DESKTOP_FILE_EXCLUSIONS = [ "!apps/desktop/prod-resources/cursor-sdk/**/*", // The installed Claude CLI is supplied explicitly; exclude optional bundled executables, retaining SDK JavaScript. "!**/node_modules/@anthropic-ai/claude-agent-sdk-*/**/*", + "!apps/desktop/prod-resources/cua-driver", + "!apps/desktop/prod-resources/cua-driver/**/*", "!**/*.map", "!**/*.d.cts", "!apps/desktop/resources/browser-secret", @@ -1037,6 +1117,12 @@ export const WINDOWS_SERVER_ASAR_UNPACK_GLOBS = [ "**/*.so.*", "**/*.dylib", ] as const; +// The Cua SDK dlopens its native libraries, which cannot load from inside the archive. Our +// @ubjs/node patch loads them from the .unpacked sibling, so only the platform packages go there. +export const CUA_SDK_ASAR_UNPACK_GLOBS = [ + "node_modules/@trycua/cua-driver-*/**/*", + "node_modules/@ubjs/node-*/**/*", +]; // Keep sidecar exclusions aligned with the app; .bin symlinks are unused and unsupported by ASAR extraction. export const WINDOWS_SERVER_ASAR_IGNORE_GLOBS = [ "**/node_modules/@cursor/sdk-*", @@ -1091,7 +1177,13 @@ export const WSL_RUNTIME_EXTRA_RESOURCES = [ WSL_RUNTIME_ARCHIVE_EXTRA_RESOURCE, WSL_RUNTIME_ARCHIVE_HASH_EXTRA_RESOURCE, ] as const; +export const CUA_DRIVER_EXTRA_RESOURCE = { + from: "apps/desktop/prod-resources/cua-driver", + to: "cua-driver", +} as const; + export const DESKTOP_EXTRA_RESOURCES = [ + CUA_DRIVER_EXTRA_RESOURCE, { from: "apps/desktop/prod-resources/cursor-sdk", to: "node_modules/@cursor", @@ -2807,7 +2899,12 @@ export const createBuildConfig = Effect.fn("createBuildConfig")(function* ( // Disable smart unpack so native packages' JS/metadata stay archived and avoid slowing NSIS with loose files. ...(platform === "win" ? { asar: { smartUnpack: false }, asarUnpack: [WINDOWS_NATIVE_ASAR_UNPACK_GLOB] } - : { asarUnpack: ["**/node_modules/@transcribe-cpp/**/*.{dylib,so,so.*}"] }), + : { + asarUnpack: [ + "**/node_modules/@transcribe-cpp/**/*.{dylib,so,so.*}", + ...CUA_SDK_ASAR_UNPACK_GLOBS, + ], + }), extraResources: [ ...DESKTOP_EXTRA_RESOURCES, ...(platform === "linux" ? LINUX_CAPTURE_EXTRA_RESOURCES : []), @@ -2995,6 +3092,127 @@ export const createBuildConfig = Effect.fn("createBuildConfig")(function* ( return buildConfig; }); +export class CuaDriverBundleMissingFileError extends Schema.TaggedError()( + "CuaDriverBundleMissingFileError", + { filePath: Schema.String }, +) { + override get message(): string { + return `Cua Driver release bundle is missing required file: ${this.filePath}`; + } +} + +export const stageCuaDriverBundle = Effect.fn("stageCuaDriverBundle")(function* (input: { + readonly platform: typeof BuildPlatform.Type; + readonly arch: typeof BuildArch.Type; + readonly extractDir: string; + readonly stageResourcesDir: string; +}) { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const asset = resolveCuaDriverAsset(input.platform, input.arch); + for (const member of asset.requiredFiles) { + const filePath = path.join(input.extractDir, member); + if (!(yield* fs.exists(filePath)) || (yield* fs.stat(filePath)).type !== "File") { + return yield* new CuaDriverBundleMissingFileError({ filePath }); + } + } + + yield* fs.makeDirectory(input.stageResourcesDir, { recursive: true }); + const destination = path.join(input.stageResourcesDir, "cua-driver"); + if (input.platform === "mac") { + // osx-sign discovers this Mach-O alongside the other external resources. + // The SDK's own dylib comes from its npm package, so only the executable ships. + yield* fs.copyFile(path.join(input.extractDir, asset.executable), destination); + yield* fs.chmod(destination, 0o755); + } else { + yield* fs.copy(input.extractDir, destination); + if (input.platform === "linux") { + yield* fs.chmod(path.join(destination, "cua-driver"), 0o755); + yield* fs.chmod(path.join(destination, "cua-cursor-theme"), 0o755); + } + } +}); + +export const stageCuaDriverExecutable = Effect.fn("stageCuaDriverExecutable")(function* (input: { + readonly platform: typeof BuildPlatform.Type; + readonly arch: typeof BuildArch.Type; + readonly repoRoot: string; + readonly stageRoot: string; + readonly stageResourcesDir: string; + readonly verbose: boolean; +}) { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const asset = resolveCuaDriverAsset(input.platform, input.arch); + const cacheDir = path.join(input.repoRoot, "node_modules/.cache/t3code/cua-driver"); + const archivePath = path.join(cacheDir, asset.archiveName); + const extractDir = path.join(input.stageRoot, "cua-driver/extract"); + yield* fs.makeDirectory(cacheDir, { recursive: true }); + yield* fs.makeDirectory(extractDir, { recursive: true }); + + const checksum = (filePath: string) => + fs + .readFile(filePath) + .pipe( + Effect.map((contents) => NodeCrypto.createHash("sha256").update(contents).digest("hex")), + ); + if ((yield* fs.exists(archivePath)) && (yield* checksum(archivePath)) !== asset.sha256) { + yield* fs.remove(archivePath, { force: true }); + } + + if (!(yield* fs.exists(archivePath))) { + const temporaryArchivePath = `${archivePath}.${process.pid}.tmp`; + yield* Effect.gen(function* () { + yield* runCommand( + ChildProcess.make("curl", [ + "--fail", + "--location", + "--silent", + "--show-error", + "--output", + temporaryArchivePath, + asset.url, + ]), + { label: `download ${asset.archiveName}`, verbose: input.verbose }, + ); + const actualChecksum = yield* checksum(temporaryArchivePath); + if (actualChecksum !== asset.sha256) { + return yield* new CuaDriverChecksumMismatchError({ + archiveName: asset.archiveName, + expected: asset.sha256, + actual: actualChecksum, + }); + } + yield* fs.rename(temporaryArchivePath, archivePath); + }).pipe(Effect.ensuring(fs.remove(temporaryArchivePath, { force: true }).pipe(Effect.ignore))); + } + + // Windows archives are zips, and Windows targets also build on Linux runners. + const hostPlatform = yield* HostProcess.Platform; + const extractCommand = !asset.archiveName.endsWith(".zip") + ? ChildProcess.make("tar", ["-xzf", archivePath, "-C", extractDir]) + : hostPlatform === "win32" + ? ChildProcess.make("powershell.exe", [ + "-NoProfile", + "-NonInteractive", + "-Command", + `Expand-Archive -LiteralPath '${archivePath.replaceAll("'", "''")}' -DestinationPath '${extractDir.replaceAll("'", "''")}' -Force`, + ]) + : ChildProcess.make("unzip", ["-q", "-o", archivePath, "-d", extractDir]); + yield* runCommand(extractCommand, { + label: `extract ${asset.archiveName}`, + verbose: input.verbose, + }); + + yield* stageCuaDriverBundle({ + platform: input.platform, + arch: input.arch, + extractDir, + stageResourcesDir: input.stageResourcesDir, + }); + yield* Effect.log(`[desktop-artifact] Staged Cua Driver ${CUA_DRIVER_VERSION}.`); +}); + const assertPlatformBuildResources = Effect.fn("assertPlatformBuildResources")(function* ( platform: typeof BuildPlatform.Type, stageResourcesDir: string, @@ -3305,10 +3523,86 @@ export const verifyWindowsPrimaryFffNativeLoad = Effect.fn( ); }); +export const verifyWindowsCuaDriverStart = Effect.fn("desktopArtifact.verifyWindowsCuaDriverStart")( + function* (input: { + readonly packagedAppDir: string; + readonly appExecutableName: string; + readonly targetArch: typeof BuildArch.Type; + readonly verbose: boolean; + }) { + const hostPlatform = yield* HostProcess.Platform; + const hostArchitecture = yield* HostProcess.Architecture; + if (hostPlatform !== "win32" || hostArchitecture !== input.targetArch) return; + const path = yield* Path.Path; + const executablePath = path.join(input.packagedAppDir, input.appExecutableName); + const resourcesDir = path.join(input.packagedAppDir, "resources"); + const sdkDist = path.join( + resourcesDir, + "app.asar", + "node_modules", + "@trycua", + "cua-driver", + "dist", + ); + const probeEnv = { ...process.env }; + delete probeEnv.ELECTRON_NO_ASAR; + delete probeEnv.NODE_OPTIONS; + + yield* runCommand( + ChildProcess.make( + executablePath, + [ + "--no-global-search-paths", + "--input-type=module", + "--eval", + WINDOWS_CUA_DRIVER_PROBE_SOURCE, + path.join(sdkDist, "embedded.js"), + path.join(sdkDist, "index.js"), + path.join(resourcesDir, "cua-driver", "cua-driver.exe"), + CUA_DRIVER_VERSION, + ], + { + cwd: input.packagedAppDir, + stdout: "pipe", + stderr: "pipe", + env: { ...probeEnv, ELECTRON_RUN_AS_NODE: "1", NODE_PATH: "" }, + }, + ), + { label: "Windows Cua Driver probe", verbose: input.verbose }, + ).pipe( + Effect.timeout(WINDOWS_CUA_DRIVER_PROBE_TIMEOUT), + Effect.catchTags({ + TimeoutError: () => + Effect.fail( + new WindowsCuaDriverProbeError({ + executablePath, + exitCode: -1, + output: `The Cua Driver probe did not finish within ${Duration.toSeconds(WINDOWS_CUA_DRIVER_PROBE_TIMEOUT)}s.`, + }), + ), + // The driver refuses to start outside a signed-in desktop session (Windows Session 0). + // The version and SDK load checks have passed by then, so such a host only warns. + BuildCommandFailedError: (error) => + error.stderrTail?.includes(WINDOWS_CUA_DRIVER_NO_SESSION) + ? Effect.logWarning( + "[desktop-artifact] Cua Driver loaded but was not started: this host has no interactive Windows session.", + ) + : Effect.fail( + new WindowsCuaDriverProbeError({ + executablePath, + exitCode: error.exitCode, + output: `${error.stderrTail ?? ""}${error.stdoutTail ?? ""}`.trim(), + }), + ), + }), + ); + }, +); + /** - * The fff probe needs a host of the target's platform and architecture; the - * bundle self-check needs a Windows host. A cross build defers both to - * scripts/verify-windows-desktop-artifact.ts. + * The fff and Cua Driver probes need a host of the target's platform and + * architecture; the bundle self-check needs a Windows host. A cross build + * defers them to scripts/verify-windows-desktop-artifact.ts. */ export const verifyWindowsPackagedRuntime = Effect.fn( "desktopArtifact.verifyWindowsPackagedRuntime", @@ -3321,6 +3615,7 @@ export const verifyWindowsPackagedRuntime = Effect.fn( const path = yield* Path.Path; const asarPath = path.join(input.packagedAppDir, "resources", WINDOWS_SERVER_ASAR_RESOURCE); yield* verifyWindowsPrimaryFffNativeLoad({ ...input, asarPath }); + yield* verifyWindowsCuaDriverStart(input); const hostPlatform = yield* HostProcess.Platform; if (hostPlatform !== "win32") { yield* Effect.log( @@ -3841,6 +4136,15 @@ const buildDesktopArtifactUnlocked = Effect.fn("buildDesktopArtifactUnlocked")(f verbose: options.verbose, }); + yield* stageCuaDriverExecutable({ + platform: options.platform, + arch: options.arch, + repoRoot, + stageRoot, + stageResourcesDir, + verbose: options.verbose, + }); + yield* assertPlatformBuildResources( options.platform, stageResourcesDir, diff --git a/scripts/lib/cli-external-packages.test.ts b/scripts/lib/cli-external-packages.test.ts index 7ea1c3968452e..9cd86e76694c2 100644 --- a/scripts/lib/cli-external-packages.test.ts +++ b/scripts/lib/cli-external-packages.test.ts @@ -42,6 +42,9 @@ describe("shouldBundleCliDependency", () => { "koffi", "@clerk/electron-passkeys", "node-addon-api", + "@trycua/cua-driver/embedded", + "@trycua/cua-driver-darwin-arm64", + "@ubjs/node", ]) { assert.strictEqual(shouldBundleCliDependency(id), false, id); } diff --git a/scripts/lib/cli-external-packages.ts b/scripts/lib/cli-external-packages.ts index b3df99797cfad..cfd2de53aba18 100644 --- a/scripts/lib/cli-external-packages.ts +++ b/scripts/lib/cli-external-packages.ts @@ -15,6 +15,8 @@ const CLI_RUNTIME_EXTERNAL_PREFIXES = [ "@koromix/", "@napi-rs/keyring", "@clerk/electron-passkeys", + "@trycua/cua-driver", + "@ubjs/", "node-gyp-build", "node-addon-api", "bufferutil", diff --git a/scripts/lib/desktop-external-packages.ts b/scripts/lib/desktop-external-packages.ts index 68af7f32512c1..c22f28f48375f 100644 --- a/scripts/lib/desktop-external-packages.ts +++ b/scripts/lib/desktop-external-packages.ts @@ -6,6 +6,8 @@ const DESKTOP_RUNTIME_EXTERNAL_PREFIXES = [ "electron-webauthn", "ffi-rs", "@yuuang/", + "@trycua/cua-driver", + "@ubjs/", // Playwright reads its runtime bundle/driver from disk; inlining cannot replace those files. "playwright-core", ] as const; diff --git a/third-party-licenses.config.json b/third-party-licenses.config.json index 66eed772bebbf..342aa61b2a0a7 100644 --- a/third-party-licenses.config.json +++ b/third-party-licenses.config.json @@ -229,6 +229,19 @@ } ], "packageOverrides": [ + { + "repositoryUrl": "https://github.com/trycua/cua", + "generatedNotice": { + "licenseId": "MIT", + "copyrights": ["Copyright (c) 2025 Cua AI, Inc."] + } + }, + { + "repositoryUrl": "https://github.com/jhugman/uniffi-bindgen-react-native", + "generatedNotice": { + "licenseId": "MPL-2.0" + } + }, { "name": "entities", "version": "8.1.0", diff --git a/third-party-licenses/spdx/v3.28.0/MPL-2.0.json b/third-party-licenses/spdx/v3.28.0/MPL-2.0.json new file mode 100644 index 0000000000000..e29bb452ad017 --- /dev/null +++ b/third-party-licenses/spdx/v3.28.0/MPL-2.0.json @@ -0,0 +1,4 @@ +{ + "licenseId": "MPL-2.0", + "licenseText": "Mozilla Public License Version 2.0\n==================================\n\n1. Definitions\n--------------\n\n1.1. \"Contributor\"\n means each individual or legal entity that creates, contributes to\n the creation of, or owns Covered Software.\n\n1.2. \"Contributor Version\"\n means the combination of the Contributions of others (if any) used\n by a Contributor and that particular Contributor's Contribution.\n\n1.3. \"Contribution\"\n means Covered Software of a particular Contributor.\n\n1.4. \"Covered Software\"\n means Source Code Form to which the initial Contributor has attached\n the notice in Exhibit A, the Executable Form of such Source Code\n Form, and Modifications of such Source Code Form, in each case\n including portions thereof.\n\n1.5. \"Incompatible With Secondary Licenses\"\n means\n\n (a) that the initial Contributor has attached the notice described\n in Exhibit B to the Covered Software; or\n\n (b) that the Covered Software was made available under the terms of\n version 1.1 or earlier of the License, but not also under the\n terms of a Secondary License.\n\n1.6. \"Executable Form\"\n means any form of the work other than Source Code Form.\n\n1.7. \"Larger Work\"\n means a work that combines Covered Software with other material, in \n a separate file or files, that is not Covered Software.\n\n1.8. \"License\"\n means this document.\n\n1.9. \"Licensable\"\n means having the right to grant, to the maximum extent possible,\n whether at the time of the initial grant or subsequently, any and\n all of the rights conveyed by this License.\n\n1.10. \"Modifications\"\n means any of the following:\n\n (a) any file in Source Code Form that results from an addition to,\n deletion from, or modification of the contents of Covered\n Software; or\n\n (b) any new file in Source Code Form that contains any Covered\n Software.\n\n1.11. \"Patent Claims\" of a Contributor\n means any patent claim(s), including without limitation, method,\n process, and apparatus claims, in any patent Licensable by such\n Contributor that would be infringed, but for the grant of the\n License, by the making, using, selling, offering for sale, having\n made, import, or transfer of either its Contributions or its\n Contributor Version.\n\n1.12. \"Secondary License\"\n means either the GNU General Public License, Version 2.0, the GNU\n Lesser General Public License, Version 2.1, the GNU Affero General\n Public License, Version 3.0, or any later versions of those\n licenses.\n\n1.13. \"Source Code Form\"\n means the form of the work preferred for making modifications.\n\n1.14. \"You\" (or \"Your\")\n means an individual or a legal entity exercising rights under this\n License. For legal entities, \"You\" includes any entity that\n controls, is controlled by, or is under common control with You. For\n purposes of this definition, \"control\" means (a) the power, direct\n or indirect, to cause the direction or management of such entity,\n whether by contract or otherwise, or (b) ownership of more than\n fifty percent (50%) of the outstanding shares or beneficial\n ownership of such entity.\n\n2. License Grants and Conditions\n--------------------------------\n\n2.1. Grants\n\nEach Contributor hereby grants You a world-wide, royalty-free,\nnon-exclusive license:\n\n(a) under intellectual property rights (other than patent or trademark)\n Licensable by such Contributor to use, reproduce, make available,\n modify, display, perform, distribute, and otherwise exploit its\n Contributions, either on an unmodified basis, with Modifications, or\n as part of a Larger Work; and\n\n(b) under Patent Claims of such Contributor to make, use, sell, offer\n for sale, have made, import, and otherwise transfer either its\n Contributions or its Contributor Version.\n\n2.2. Effective Date\n\nThe licenses granted in Section 2.1 with respect to any Contribution\nbecome effective for each Contribution on the date the Contributor first\ndistributes such Contribution.\n\n2.3. Limitations on Grant Scope\n\nThe licenses granted in this Section 2 are the only rights granted under\nthis License. No additional rights or licenses will be implied from the\ndistribution or licensing of Covered Software under this License.\nNotwithstanding Section 2.1(b) above, no patent license is granted by a\nContributor:\n\n(a) for any code that a Contributor has removed from Covered Software;\n or\n\n(b) for infringements caused by: (i) Your and any other third party's\n modifications of Covered Software, or (ii) the combination of its\n Contributions with other software (except as part of its Contributor\n Version); or\n\n(c) under Patent Claims infringed by Covered Software in the absence of\n its Contributions.\n\nThis License does not grant any rights in the trademarks, service marks,\nor logos of any Contributor (except as may be necessary to comply with\nthe notice requirements in Section 3.4).\n\n2.4. Subsequent Licenses\n\nNo Contributor makes additional grants as a result of Your choice to\ndistribute the Covered Software under a subsequent version of this\nLicense (see Section 10.2) or under the terms of a Secondary License (if\npermitted under the terms of Section 3.3).\n\n2.5. Representation\n\nEach Contributor represents that the Contributor believes its\nContributions are its original creation(s) or it has sufficient rights\nto grant the rights to its Contributions conveyed by this License.\n\n2.6. Fair Use\n\nThis License is not intended to limit any rights You have under\napplicable copyright doctrines of fair use, fair dealing, or other\nequivalents.\n\n2.7. Conditions\n\nSections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted\nin Section 2.1.\n\n3. Responsibilities\n-------------------\n\n3.1. Distribution of Source Form\n\nAll distribution of Covered Software in Source Code Form, including any\nModifications that You create or to which You contribute, must be under\nthe terms of this License. You must inform recipients that the Source\nCode Form of the Covered Software is governed by the terms of this\nLicense, and how they can obtain a copy of this License. You may not\nattempt to alter or restrict the recipients' rights in the Source Code\nForm.\n\n3.2. Distribution of Executable Form\n\nIf You distribute Covered Software in Executable Form then:\n\n(a) such Covered Software must also be made available in Source Code\n Form, as described in Section 3.1, and You must inform recipients of\n the Executable Form how they can obtain a copy of such Source Code\n Form by reasonable means in a timely manner, at a charge no more\n than the cost of distribution to the recipient; and\n\n(b) You may distribute such Executable Form under the terms of this\n License, or sublicense it under different terms, provided that the\n license for the Executable Form does not attempt to limit or alter\n the recipients' rights in the Source Code Form under this License.\n\n3.3. Distribution of a Larger Work\n\nYou may create and distribute a Larger Work under terms of Your choice,\nprovided that You also comply with the requirements of this License for\nthe Covered Software. If the Larger Work is a combination of Covered\nSoftware with a work governed by one or more Secondary Licenses, and the\nCovered Software is not Incompatible With Secondary Licenses, this\nLicense permits You to additionally distribute such Covered Software\nunder the terms of such Secondary License(s), so that the recipient of\nthe Larger Work may, at their option, further distribute the Covered\nSoftware under the terms of either this License or such Secondary\nLicense(s).\n\n3.4. Notices\n\nYou may not remove or alter the substance of any license notices\n(including copyright notices, patent notices, disclaimers of warranty,\nor limitations of liability) contained within the Source Code Form of\nthe Covered Software, except that You may alter any license notices to\nthe extent required to remedy known factual inaccuracies.\n\n3.5. Application of Additional Terms\n\nYou may choose to offer, and to charge a fee for, warranty, support,\nindemnity or liability obligations to one or more recipients of Covered\nSoftware. However, You may do so only on Your own behalf, and not on\nbehalf of any Contributor. You must make it absolutely clear that any\nsuch warranty, support, indemnity, or liability obligation is offered by\nYou alone, and You hereby agree to indemnify every Contributor for any\nliability incurred by such Contributor as a result of warranty, support,\nindemnity or liability terms You offer. You may include additional\ndisclaimers of warranty and limitations of liability specific to any\njurisdiction.\n\n4. Inability to Comply Due to Statute or Regulation\n---------------------------------------------------\n\nIf it is impossible for You to comply with any of the terms of this\nLicense with respect to some or all of the Covered Software due to\nstatute, judicial order, or regulation then You must: (a) comply with\nthe terms of this License to the maximum extent possible; and (b)\ndescribe the limitations and the code they affect. Such description must\nbe placed in a text file included with all distributions of the Covered\nSoftware under this License. Except to the extent prohibited by statute\nor regulation, such description must be sufficiently detailed for a\nrecipient of ordinary skill to be able to understand it.\n\n5. Termination\n--------------\n\n5.1. The rights granted under this License will terminate automatically\nif You fail to comply with any of its terms. However, if You become\ncompliant, then the rights granted under this License from a particular\nContributor are reinstated (a) provisionally, unless and until such\nContributor explicitly and finally terminates Your grants, and (b) on an\nongoing basis, if such Contributor fails to notify You of the\nnon-compliance by some reasonable means prior to 60 days after You have\ncome back into compliance. Moreover, Your grants from a particular\nContributor are reinstated on an ongoing basis if such Contributor\nnotifies You of the non-compliance by some reasonable means, this is the\nfirst time You have received notice of non-compliance with this License\nfrom such Contributor, and You become compliant prior to 30 days after\nYour receipt of the notice.\n\n5.2. If You initiate litigation against any entity by asserting a patent\ninfringement claim (excluding declaratory judgment actions,\ncounter-claims, and cross-claims) alleging that a Contributor Version\ndirectly or indirectly infringes any patent, then the rights granted to\nYou by any and all Contributors for the Covered Software under Section\n2.1 of this License shall terminate.\n\n5.3. In the event of termination under Sections 5.1 or 5.2 above, all\nend user license agreements (excluding distributors and resellers) which\nhave been validly granted by You or Your distributors under this License\nprior to termination shall survive termination.\n\n************************************************************************\n* *\n* 6. Disclaimer of Warranty *\n* ------------------------- *\n* *\n* Covered Software is provided under this License on an \"as is\" *\n* basis, without warranty of any kind, either expressed, implied, or *\n* statutory, including, without limitation, warranties that the *\n* Covered Software is free of defects, merchantable, fit for a *\n* particular purpose or non-infringing. The entire risk as to the *\n* quality and performance of the Covered Software is with You. *\n* Should any Covered Software prove defective in any respect, You *\n* (not any Contributor) assume the cost of any necessary servicing, *\n* repair, or correction. This disclaimer of warranty constitutes an *\n* essential part of this License. No use of any Covered Software is *\n* authorized under this License except under this disclaimer. *\n* *\n************************************************************************\n\n************************************************************************\n* *\n* 7. Limitation of Liability *\n* -------------------------- *\n* *\n* Under no circumstances and under no legal theory, whether tort *\n* (including negligence), contract, or otherwise, shall any *\n* Contributor, or anyone who distributes Covered Software as *\n* permitted above, be liable to You for any direct, indirect, *\n* special, incidental, or consequential damages of any character *\n* including, without limitation, damages for lost profits, loss of *\n* goodwill, work stoppage, computer failure or malfunction, or any *\n* and all other commercial damages or losses, even if such party *\n* shall have been informed of the possibility of such damages. This *\n* limitation of liability shall not apply to liability for death or *\n* personal injury resulting from such party's negligence to the *\n* extent applicable law prohibits such limitation. Some *\n* jurisdictions do not allow the exclusion or limitation of *\n* incidental or consequential damages, so this exclusion and *\n* limitation may not apply to You. *\n* *\n************************************************************************\n\n8. Litigation\n-------------\n\nAny litigation relating to this License may be brought only in the\ncourts of a jurisdiction where the defendant maintains its principal\nplace of business and such litigation shall be governed by laws of that\njurisdiction, without reference to its conflict-of-law provisions.\nNothing in this Section shall prevent a party's ability to bring\ncross-claims or counter-claims.\n\n9. Miscellaneous\n----------------\n\nThis License represents the complete agreement concerning the subject\nmatter hereof. If any provision of this License is held to be\nunenforceable, such provision shall be reformed only to the extent\nnecessary to make it enforceable. Any law or regulation which provides\nthat the language of a contract shall be construed against the drafter\nshall not be used to construe this License against a Contributor.\n\n10. Versions of the License\n---------------------------\n\n10.1. New Versions\n\nMozilla Foundation is the license steward. Except as provided in Section\n10.3, no one other than the license steward has the right to modify or\npublish new versions of this License. Each version will be given a\ndistinguishing version number.\n\n10.2. Effect of New Versions\n\nYou may distribute the Covered Software under the terms of the version\nof the License under which You originally received the Covered Software,\nor under the terms of any subsequent version published by the license\nsteward.\n\n10.3. Modified Versions\n\nIf you create software not governed by this License, and you want to\ncreate a new license for such software, you may create and use a\nmodified version of this License if you rename the license and remove\nany references to the name of the license steward (except to note that\nsuch modified license differs from this License).\n\n10.4. Distributing Source Code Form that is Incompatible With Secondary\nLicenses\n\nIf You choose to distribute Source Code Form that is Incompatible With\nSecondary Licenses under the terms of this version of the License, the\nnotice described in Exhibit B of this License must be attached.\n\nExhibit A - Source Code Form License Notice\n-------------------------------------------\n\n This Source Code Form is subject to the terms of the Mozilla Public\n License, v. 2.0. If a copy of the MPL was not distributed with this\n file, You can obtain one at https://mozilla.org/MPL/2.0/.\n\nIf it is not possible or desirable to put the notice in a particular\nfile, then You may include the notice in a location (such as a LICENSE\nfile in a relevant directory) where a recipient would be likely to look\nfor such a notice.\n\nYou may add additional accurate notices of copyright ownership.\n\nExhibit B - \"Incompatible With Secondary Licenses\" Notice\n---------------------------------------------------------\n\n This Source Code Form is \"Incompatible With Secondary Licenses\", as\n defined by the Mozilla Public License, v. 2.0." +}