diff --git a/apps/desktop/src/preview/Manager.test.ts b/apps/desktop/src/preview/Manager.test.ts index 5bda1a9fe4a6..ccd870317348 100644 --- a/apps/desktop/src/preview/Manager.test.ts +++ b/apps/desktop/src/preview/Manager.test.ts @@ -1,3 +1,4 @@ +import * as NodeFileSystem from "@effect/platform-node/NodeFileSystem"; import * as NodeVM from "node:vm"; import * as Schema from "effect/Schema"; import { it as effectIt } from "@effect/vitest"; @@ -34,6 +35,98 @@ import * as PreviewManager from "./Manager.ts"; const encodePreviewManagerError = Schema.encodeSync(PreviewManager.PreviewManagerError); import * as PreviewPasskeys from "./Passkeys.ts"; +describe("copyToDownloads", () => { + effectIt.effect("preserves concurrent downloads with the same filename", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const directory = yield* fs.makeTempDirectoryScoped(); + const downloads = directory + "/downloads"; + yield* fs.makeDirectory(downloads); + const first = directory + "/first.csv"; + const second = directory + "/second.csv"; + yield* fs.writeFileString(first, "first export"); + yield* fs.writeFileString(second, "second export"); + const checked = yield* Deferred.make(); + let checks = 0; + const concurrentFs = FileSystem.FileSystem.of({ + ...fs, + exists: (target) => + target === downloads + "/export.csv" + ? Effect.gen(function* () { + checks += 1; + if (checks === 2) yield* Deferred.succeed(checked, undefined); + yield* Deferred.await(checked); + return false; + }) + : fs.exists(target), + }); + const targets = yield* Effect.all( + [ + PreviewManager.copyToDownloads(downloads, first, "export.csv"), + PreviewManager.copyToDownloads(downloads, second, "export.csv"), + ], + { concurrency: "unbounded" }, + ).pipe(Effect.provideService(FileSystem.FileSystem, concurrentFs)); + expect((yield* fs.readDirectory(downloads)).sort()).toEqual(["export (1).csv", "export.csv"]); + const contents = yield* Effect.all([ + fs.readFileString(downloads + "/export.csv"), + fs.readFileString(downloads + "/export (1).csv"), + ]); + expect(contents.sort()).toEqual(["first export", "second export"]); + expect(new Set(targets).size).toBe(2); + }).pipe(Effect.provide(Layer.merge(NodeFileSystem.layer, Path.layer)), Effect.scoped), + ); + effectIt.effect("leaves existing downloads intact when all 100 names are occupied", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const directory = yield* fs.makeTempDirectoryScoped(); + const downloads = directory + "/downloads"; + yield* fs.makeDirectory(downloads); + const source = directory + "/source.csv"; + yield* fs.writeFileString(source, "new export"); + yield* Effect.forEach( + Array.from({ length: 100 }, (_, attempt) => attempt), + (attempt) => + fs.writeFileString( + downloads + (attempt === 0 ? "/export.csv" : "/export (" + attempt + ").csv"), + "existing export", + ), + ); + const exit = yield* Effect.exit( + PreviewManager.copyToDownloads(downloads, source, "export.csv"), + ); + expect(Exit.isFailure(exit)).toBe(true); + if (Exit.isFailure(exit)) { + expect(Option.getOrThrow(Cause.findErrorOption(exit.cause))).toMatchObject({ + cause: { code: "EEXIST" }, + }); + } + expect(yield* fs.readDirectory(downloads)).toHaveLength(100); + const contents = yield* Effect.forEach(yield* fs.readDirectory(downloads), (name) => + fs.readFileString(downloads + "/" + name), + ); + expect(new Set(contents)).toEqual(new Set(["existing export"])); + }).pipe(Effect.provide(Layer.merge(NodeFileSystem.layer, Path.layer)), Effect.scoped), + ); + + effectIt.effect("preserves non-collision copy errors without creating a download", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const directory = yield* fs.makeTempDirectoryScoped(); + const exit = yield* Effect.exit( + PreviewManager.copyToDownloads(directory, directory + "/missing.csv", "export.csv"), + ); + expect(Exit.isFailure(exit)).toBe(true); + if (Exit.isFailure(exit)) { + expect(Option.getOrThrow(Cause.findErrorOption(exit.cause))).toMatchObject({ + cause: { code: "ENOENT" }, + }); + } + expect(yield* fs.readDirectory(directory)).toEqual([]); + }).pipe(Effect.provide(Layer.merge(NodeFileSystem.layer, Path.layer)), Effect.scoped), + ); +}); + describe("fitPictureInPictureContentSize", () => { it("preserves the PiP content area across aspect-ratio changes", () => { expect(PreviewManager.fitPictureInPictureContentSize([480, 320], 16 / 9)).toEqual([523, 294]); diff --git a/apps/desktop/src/preview/Manager.ts b/apps/desktop/src/preview/Manager.ts index 8338d98a18eb..185aa89e1064 100644 --- a/apps/desktop/src/preview/Manager.ts +++ b/apps/desktop/src/preview/Manager.ts @@ -5,6 +5,10 @@ * elements live in the renderer; we only attach listeners and forward state * here). Single layer-scoped browser session partition. */ +// @effect-diagnostics-next-line nodeBuiltinImport:off -- FileSystem.copyFile has no exclusive-copy flag. +import * as NodeFS from "node:fs"; +// @effect-diagnostics-next-line nodeBuiltinImport:off -- COPYFILE_EXCL atomically prevents replacement by concurrent writers. +import * as NodeFSP from "node:fs/promises"; import { DesktopPreviewRecordingInputSchema, DESKTOP_PREVIEW_RECORDING_CAPTURE_TRIGGER, @@ -501,6 +505,7 @@ interface ExpectedAgentInput { readonly expiresAt: number; } type OpenLinkListener = (event: DesktopPreviewOpenLinkEvent) => Effect.Effect; + /** * Protocols a preview page may open in a real popup window. * @@ -3717,26 +3722,6 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function // a page put a modal over the whole app. Downloads from a page the agent drove // last go to the artifact directory instead. Downloads the human starts keep // the Save dialog. - /** Puts a copy in Downloads under a name that never replaces a file already there. */ - const copyToDownloads = Effect.fn("PreviewManager.copyToDownloads")(function* ( - source: string, - fileName: string, - ) { - const directory = app.getPath("downloads"); - const extension = path.extname(fileName); - const stem = path.basename(fileName, extension) || "download"; - for (let attempt = 0; attempt < 100; attempt += 1) { - const target = path.join( - directory, - attempt === 0 ? `${stem}${extension}` : `${stem} (${attempt})${extension}`, - ); - if (yield* fileSystem.exists(target)) continue; - yield* fileSystem.copyFile(source, target); - shell.showItemInFolder(target); - return; - } - }); - const installDownloadHandler = (session: Session) => { if (downloadHandlerSessions.has(session)) return; downloadHandlerSessions.add(session); @@ -3749,7 +3734,9 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function item.once("done", (_doneEvent, state) => { if (state !== "completed") return; runFork( - copyToDownloads(item.getSavePath(), item.getFilename()).pipe( + copyToDownloads(app.getPath("downloads"), item.getSavePath(), item.getFilename()).pipe( + Effect.provideService(Path.Path, path), + Effect.tap((target) => Effect.sync(() => shell.showItemInFolder(target))), Effect.catch((error) => Effect.logWarning("preview download copy failed", { cause: error }), ), @@ -5538,6 +5525,35 @@ export class PreviewManager extends Context.Service< } >()("@t3tools/desktop/preview/Manager/PreviewManager") {} +/** Puts a copy in Downloads under a name that never replaces a file already there. */ +export const copyToDownloads = Effect.fn("PreviewManager.copyToDownloads")(function* ( + directory: string, + source: string, + fileName: string, +) { + const path = yield* Path.Path; + const extension = path.extname(fileName); + const stem = path.basename(fileName, extension) || "download"; + for (let attempt = 0; ; attempt += 1) { + const target = path.join( + directory, + attempt === 0 ? `${stem}${extension}` : `${stem} (${attempt})${extension}`, + ); + const copied = yield* Effect.tryPromise(() => + NodeFSP.copyFile(source, target, NodeFS.constants.COPYFILE_EXCL), + ).pipe( + Effect.as(true), + Effect.catch((error) => { + const cause = error.cause; + return attempt < 99 && cause instanceof Error && "code" in cause && cause.code === "EEXIST" + ? Effect.succeed(false) + : Effect.fail(error); + }), + ); + if (copied) return target; + } +}); + /** @public Service construction is part of the canonical Effect module API. */ export const make = Effect.gen(function* PreviewManagerMake() { const environment = yield* DesktopEnvironment.DesktopEnvironment; diff --git a/apps/mobile/src/features/browser/BrowserCloneRouteScreen.tsx b/apps/mobile/src/features/browser/BrowserCloneRouteScreen.tsx index 9a0cf3bc6ca5..affbffacb99b 100644 --- a/apps/mobile/src/features/browser/BrowserCloneRouteScreen.tsx +++ b/apps/mobile/src/features/browser/BrowserCloneRouteScreen.tsx @@ -35,6 +35,7 @@ export function BrowserCloneRouteScreen({ route }: Props) { useEffect(() => { if (AsyncResult.isSuccess(event) && event.value.threadId === threadId) refreshList(); }, [event, threadId, refreshList]); + const canOperate = useAtomValue(previewEnvironment.cloneInvoke.permissionAtom(environmentId)); const invoke = useAtomCommand(previewEnvironment.cloneInvoke, { reportFailure: false }); const [selectedTab, setSelectedTab] = useState(null); const [frame, setFrame] = useState(null); @@ -58,7 +59,7 @@ export function BrowserCloneRouteScreen({ route }: Props) { setFrame(null); setError(null); failedRef.current = false; - if (!tabId || !active || !focused) return; + if (!tabId || !active || !focused || !canOperate) return; const session = createBrowserCloneSession( { environmentId, @@ -95,7 +96,7 @@ export function BrowserCloneRouteScreen({ route }: Props) { session.dispose(); if (sessionRef.current === session) sessionRef.current = null; }; - }, [active, focused, environmentId, threadId, tabId, generation, invoke]); + }, [active, focused, environmentId, threadId, tabId, generation, invoke, canOperate]); const failed = useCallback((session: ReturnType) => { if (sessionRef.current === session) { failedRef.current = true; @@ -155,31 +156,33 @@ export function BrowserCloneRouteScreen({ route }: Props) { }} > - - - {tabs.map((tab) => ( - setSelectedTab(tab.tabId)} - className={`max-w-64 border px-3 py-2 ${tab.tabId === tabId ? "border-white/50 bg-white/10" : "border-white/10"}`} - > - - {tab.navStatus._tag === "Idle" - ? "New tab" - : tab.navStatus.title || tab.navStatus.url} - - - ))} - - - {tab?.runtime === "server" && ( + {canOperate && ( + + + {tabs.map((tab) => ( + setSelectedTab(tab.tabId)} + className={`max-w-64 border px-3 py-2 ${tab.tabId === tabId ? "border-white/50 bg-white/10" : "border-white/10"}`} + > + + {tab.navStatus._tag === "Idle" + ? "New tab" + : tab.navStatus.title || tab.navStatus.url} + + + ))} + + + )} + {canOperate && tab?.runtime === "server" && ( @@ -194,7 +197,21 @@ export function BrowserCloneRouteScreen({ route }: Props) { Open server browser )} - {!tabId || error || AsyncResult.isFailure(list) ? ( + {!canOperate ? ( + + + Browser access is not permitted. Ask the environment owner to grant browser control, or + pair again with a link that allows it. + + navigation.goBack()} + className="px-5 py-3" + > + Done + + + ) : !tabId || error || AsyncResult.isFailure(list) ? ( {AsyncResult.isFailure(list) diff --git a/apps/server/src/cloud/ManagedEndpointRuntime.test.ts b/apps/server/src/cloud/ManagedEndpointRuntime.test.ts index b4a6122a443e..077e34033f37 100644 --- a/apps/server/src/cloud/ManagedEndpointRuntime.test.ts +++ b/apps/server/src/cloud/ManagedEndpointRuntime.test.ts @@ -1053,6 +1053,122 @@ describe("CloudManagedEndpointRuntime", () => { }), ); + it.effect.each(["registration", "config change", "disable", "shutdown"] as const)( + "keeps the serving connector across failed replacements until %s", + (finish) => + Effect.gen(function* () { + const killed: Array = []; + yield* Effect.scoped( + Effect.gen(function* () { + const olderClient = { + status: "available", + executablePath: "/managed/older/cloudflared", + source: "managed", + version: "2025.9.0", + } as const; + const pinnedClient = { + ...olderClient, + executablePath: "/managed/pinned/cloudflared", + version: RelayClient.CLOUDFLARED_VERSION, + }; + let installed = false; + let nextPid = 1; + const releaseInstall = yield* Deferred.make(); + const olderStopped = yield* Deferred.make(); + const spawned = yield* Queue.unbounded<{ + pid: number; + exit: Deferred.Deferred; + output: Queue.Queue; + }>(); + const spawner = ChildProcessSpawner.make(() => + Effect.gen(function* () { + const pid = nextPid++; + const exit = yield* Deferred.make(); + const output = yield* Queue.unbounded(); + const handle = makeHandle({ + pid, + exitCode: Deferred.await(exit), + output: Stream.fromQueue(output), + onKill: () => { + killed.push(pid); + if (pid === 1) Deferred.doneUnsafe(olderStopped, Effect.void); + }, + }); + yield* Effect.addFinalizer(() => handle.kill().pipe(Effect.ignore)); + yield* Queue.offer(spawned, { pid, exit, output }); + return handle; + }), + ); + const runtime = yield* buildCloudManagedEndpointRuntime( + spawner, + Layer.succeed( + RelayClient.RelayClient, + RelayClient.RelayClient.of({ + resolve: Effect.sync(() => (installed ? pinnedClient : olderClient)), + install: Deferred.await(releaseInstall).pipe( + Effect.andThen( + Effect.sync(() => { + installed = true; + return pinnedClient; + }), + ), + ), + installWithProgress: () => Effect.die("unused"), + pruneManagedVersions: Effect.void, + }), + ), + ); + const config = { providerKind: "cloudflare_tunnel", connectorToken: "token" } as const; + yield* runtime.applyConfig(config); + const older = yield* Queue.take(spawned); + yield* Queue.offer( + older.output, + new TextEncoder().encode("Registered tunnel connection\n"), + ); + yield* Stream.runHead(runtime.tunnelConnected); + yield* Deferred.succeed(releaseInstall, undefined); + const first = yield* Queue.take(spawned); + yield* runtime.applyConfig(config); + yield* TestClock.adjust(Duration.zero); + yield* Deferred.succeed(first.exit, ChildProcessSpawner.ExitCode(1)); + const second = yield* Queue.take(spawned); + yield* runtime.applyConfig(config); + expect(killed).toEqual([first.pid]); + + yield* Deferred.succeed(second.exit, ChildProcessSpawner.ExitCode(1)); + yield* TestClock.adjust(Duration.zero); + yield* TestClock.adjust(Duration.seconds(1)); + const third = yield* Queue.take(spawned); + expect(killed).toEqual([first.pid, second.pid]); + expect(yield* runtime.applyConfig(config)).toMatchObject({ + status: "running", + pid: third.pid, + }); + + if (finish === "registration") { + yield* Queue.offer( + third.output, + new TextEncoder().encode("Registered tunnel connection\n"), + ); + yield* Deferred.await(olderStopped); + expect(killed).toEqual([first.pid, second.pid, older.pid]); + } else if (finish === "config change") { + yield* runtime.applyConfig({ ...config, connectorToken: "changed-token" }); + expect(killed).toContain(older.pid); + expect(killed).toContain(third.pid); + } else if (finish === "disable") { + expect(yield* runtime.applyConfig(null)).toEqual({ status: "disabled" }); + expect(killed).toContain(older.pid); + expect(killed).toContain(third.pid); + } + }), + ); + expect(killed.toSorted((a, b) => a - b)).toEqual( + finish === "config change" ? [1, 2, 3, 4, 5] : [1, 2, 3, 4], + ); + }), + ); + it.effect("installs a missing relay client in the background and then starts it", () => Effect.gen(function* () { const pinnedClient = { diff --git a/apps/server/src/cloud/ManagedEndpointRuntime.ts b/apps/server/src/cloud/ManagedEndpointRuntime.ts index 0365cc28e619..9a10f328ebc7 100644 --- a/apps/server/src/cloud/ManagedEndpointRuntime.ts +++ b/apps/server/src/cloud/ManagedEndpointRuntime.ts @@ -180,6 +180,7 @@ export const make = Effect.gen(function* () { const relayClient = yield* RelayClient.RelayClient; const httpClient = HttpClient.withScope(yield* HttpClient.HttpClient); const activeRef = yield* Ref.make(null); + const servingPreviousRef = yield* Ref.make(null); const desiredConfigRef = yield* Ref.make(null); const recoveryRequests = yield* Queue.sliding(1); const tunnelConnections = yield* Queue.sliding(1); @@ -199,6 +200,11 @@ export const make = Effect.gen(function* () { yield* stopConnector(active); }); + const stopPrevious = Effect.gen(function* () { + const previous = yield* Ref.getAndSet(servingPreviousRef, null); + yield* stopConnector(previous); + }); + const superviseConnector = (connector: ActiveConnector) => Effect.gen(function* () { const result = yield* Effect.result(connector.child.exitCode); @@ -313,6 +319,7 @@ export const make = Effect.gen(function* () { }); // Detach the old connector so the reconcile starts a new one beside it. const previous = yield* Ref.getAndSet(activeRef, null); + if (previous) yield* Ref.set(servingPreviousRef, previous); const status = yield* reconcileConfig(desiredConfig); const next = yield* Ref.get(activeRef); if (!next) { @@ -324,21 +331,14 @@ export const make = Effect.gen(function* () { ? yield* previous.child.isRunning.pipe(Effect.orElseSucceed(() => false)) : false; if (previous && previousRunning) { + yield* Ref.set(servingPreviousRef, null); yield* Ref.set(activeRef, previous); } else { - yield* stopConnector(previous); + if (previous) yield* stopPrevious; yield* Queue.offer(recoveryRequests, desiredConfig); } return; } - // Runs in the new connector's scope: the old one stops when the new - // one registers, or as soon as the new one is stopped or replaced. - if (previous) { - yield* Deferred.await(next.registered).pipe( - Effect.ensuring(stopConnector(previous)), - Effect.forkIn(next.scope), - ); - } }), ), ), @@ -527,12 +527,15 @@ export const make = Effect.gen(function* () { reconcileConfig = Effect.fn("CloudManagedEndpointRuntime.reconcileConfig")(function* (config) { if (!config || config.providerKind !== "cloudflare_tunnel") { yield* stopActive; + yield* stopPrevious; return config ? { status: "unsupported", providerKind: config.providerKind } : { status: "disabled" }; } const nextConfigKey = runtimeConfigKey(config); + const previous = yield* Ref.get(servingPreviousRef); + if (previous && previous.configKey !== nextConfigKey) yield* stopPrevious; const active = yield* Ref.get(activeRef); if (active?.configKey === nextConfigKey) { const isRunning = yield* active.child.isRunning.pipe(Effect.orElseSucceed(() => false)); @@ -645,6 +648,20 @@ export const make = Effect.gen(function* () { yield* Effect.forkIn(observeConnectorOutput(connector), connectorScope); yield* Effect.forkIn(superviseConnector(connector), connectorScope); yield* Effect.forkIn(watchConnectorRegistration(connector), connectorScope); + // Each replacement owns its registration waiter; the runtime owns the + // serving connector until registration, a config change, or shutdown. + if (yield* Ref.get(servingPreviousRef)) { + yield* Deferred.await(connector.registered).pipe( + Effect.andThen( + reconcileSemaphore.withPermits(1)( + Effect.gen(function* () { + if ((yield* Ref.get(activeRef)) === connector) yield* stopPrevious; + }), + ), + ), + Effect.forkIn(connectorScope), + ); + } yield* Effect.forkIn(watchConnectorReadiness(connector), connectorScope); return { status: "running", diff --git a/apps/server/src/usage/UsageService.test.ts b/apps/server/src/usage/UsageService.test.ts index af26c30a4770..7fda9bdccf69 100644 --- a/apps/server/src/usage/UsageService.test.ts +++ b/apps/server/src/usage/UsageService.test.ts @@ -25,6 +25,7 @@ import * as Exit from "effect/Exit"; import * as Fiber from "effect/Fiber"; import * as FileSystem from "effect/FileSystem"; import * as Context from "effect/Context"; +import * as Crypto from "effect/Crypto"; import * as Layer from "effect/Layer"; import * as Scheduler from "effect/Scheduler"; import * as Schema from "effect/Schema"; @@ -33,6 +34,12 @@ import { HttpClient, HttpClientResponse } from "effect/http"; import * as ServerConfig from "../config.ts"; import * as ServerSettings from "../serverSettings.ts"; +import { readCursorCredential } from "@t3tools/provider-cursor/server/accountUsage"; +import { + decodeCursorAccountCaches, + encodeCursorAccountCaches, + CURSOR_ACCOUNT_CACHE_FILE_NAME, +} from "../../../../packages/provider-cursor/src/server/accountCache.ts"; import * as CursorAccountReader from "@t3tools/provider-cursor/server/CursorAccountReader"; import * as CursorKeychain from "@t3tools/provider-cursor/server/CursorKeychain"; import * as CursorUsageAccounts from "@t3tools/provider-cursor/server/CursorUsageAccounts"; @@ -102,10 +109,21 @@ const layerCursorUsageAccounts = ( * A service with its own Cursor account caches over `read`, as a fresh server * process has. `awaitPersisted` also waits for the account cache writes. */ -const makeWithCursor = (read: CursorAccountReader.CursorAccountReader["Service"]["read"]) => +const makeWithCursor = ( + read: CursorAccountReader.CursorAccountReader["Service"]["read"], + resolve: ( + source: Parameters[0], + ) => ReturnType = (source) => readCursorCredential(source), +) => Effect.gen(function* () { + const readerContext = yield* Effect.context(); const context = yield* Layer.build( - layerCursorUsageAccounts(Layer.succeed(CursorAccountReader.CursorAccountReader, { read })), + layerCursorUsageAccounts( + Layer.succeed(CursorAccountReader.CursorAccountReader, { + read, + resolve: (source) => resolve(source).pipe(Effect.provideContext(readerContext)), + }), + ), ); const accounts = Context.get(context, CursorUsageAccounts.CursorUsageAccounts); const service = yield* UsageService.make.pipe(Effect.provideContext(context)); @@ -258,11 +276,11 @@ function makeFakeCursor() { return { state, read }; } -const writeCursorLogin = (home: string) => +const writeCursorLogin = (home: string, accessToken = "token-a") => Effect.promise(async () => { const authPath = NodePath.join(home, "config", "cursor", "auth.json"); await NodeFSP.mkdir(NodePath.dirname(authPath), { recursive: true }); - await NodeFSP.writeFile(authPath, "{}"); + await NodeFSP.writeFile(authPath, JSON.stringify({ accessToken })); }); function cursorSource(summary: { readonly sources: readonly UsageSource[] }) { @@ -845,6 +863,363 @@ describe("UsageService", () => { }).pipe(Effect.scoped, Effect.provide(TestClock.layer())), ); + it.live.each([null, "Old login failed."])( + "ignores an old Cursor refresh completing after a login switch with error %s", + (oldError) => + Effect.gen(function* () { + const { settings, home } = yield* setup; + yield* writeCursorLogin(home); + yield* TestClock.setTime(CURSOR_NOW); + const old = makeFakeCursor(); + const current = makeFakeCursor(); + const started = yield* Deferred.make(); + const gate = yield* Deferred.make(); + old.state.gate = gate; + old.state.error = oldError; + old.state.events = [{ timestampMs: CURSOR_NOW - 3 * HOUR_MS, outputTokens: 5 }]; + current.state.accountKey = "account-b"; + current.state.events = [{ timestampMs: CURSOR_NOW - 3 * HOUR_MS, outputTokens: 11 }]; + yield* Effect.gen(function* () { + const service = yield* makeWithCursor((credential, sinceMs, untilMs) => + Effect.gen(function* () { + if ( + typeof credential !== "string" && + credential.kind === "token" && + credential.accessToken === "token-b" + ) { + return yield* current.read(credential, sinceMs, untilMs); + } + yield* Deferred.succeed(started, undefined); + return yield* old.read(credential, sinceMs, untilMs); + }), + ); + const oldRead = yield* service + .readSummary({ ...WINDOW, awaitRefresh: true }) + .pipe(Effect.forkChild); + yield* Deferred.await(started); + yield* writeCursorLogin(home, "token-b"); + const switched = yield* service.readSummary({ ...WINDOW, awaitRefresh: true }); + assert.strictEqual(totalOutputTokens(switched), 11); + yield* Deferred.succeed(gate, undefined); + yield* Fiber.join(oldRead); + const fresh = yield* service.readSummary({ ...WINDOW, awaitRefresh: true }); + assert.strictEqual(totalOutputTokens(fresh), 11); + assert.strictEqual(cursorSource(fresh)?.status, "ok"); + yield* service.awaitPersisted; + const config = yield* ServerConfig.ServerConfig; + const persisted = yield* Effect.promise(() => + NodeFSP.readFile( + NodePath.join(config.stateDir, "usage-cursor-account-cache-v1.json"), + "utf8", + ), + ); + assert.notInclude(persisted, "account-a"); + assert.notInclude(persisted, "token-a"); + assert.notInclude(persisted, "token-b"); + }).pipe( + Effect.provide( + layerService({ prefix: "usage-service-cursor-switch-inflight", home, settings }), + ), + ); + }).pipe(Effect.scoped, Effect.provide(TestClock.layer())), + ); + + it.live.each([false, true])( + "keeps the newer Cursor identity when older preparation resolves after refresh completed %s", + (refreshCompleted) => + Effect.gen(function* () { + const { settings, home } = yield* setup; + yield* writeCursorLogin(home); + yield* TestClock.setTime(CURSOR_NOW); + const old = makeFakeCursor(); + const current = makeFakeCursor(); + current.state.accountKey = "account-b"; + current.state.events = [{ timestampMs: CURSOR_NOW - 3 * HOUR_MS, outputTokens: 11 }]; + const resolvingOld = yield* Deferred.make(); + const releaseOld = yield* Deferred.make(); + const refreshingCurrent = yield* Deferred.make(); + const releaseCurrent = yield* Deferred.make(); + yield* Effect.gen(function* () { + const service = yield* makeWithCursor( + (credential, sinceMs, untilMs) => + Effect.gen(function* () { + if ( + typeof credential !== "string" && + credential.kind === "token" && + credential.accessToken === "token-b" + ) { + yield* Deferred.succeed(refreshingCurrent, undefined); + if (!refreshCompleted) yield* Deferred.await(releaseCurrent); + return yield* current.read(credential, sinceMs, untilMs); + } + return yield* old.read(credential, sinceMs, untilMs); + }), + (source) => + Effect.gen(function* () { + const resolved = yield* readCursorCredential(source); + if (resolved.kind === "token" && resolved.accessToken === "token-a") { + yield* Deferred.succeed(resolvingOld, undefined); + yield* Deferred.await(releaseOld); + } + return resolved; + }), + ); + const first = yield* service + .readSummary({ ...WINDOW, awaitRefresh: true }) + .pipe(Effect.forkChild); + yield* Deferred.await(resolvingOld); + yield* writeCursorLogin(home, "token-b"); + const second = yield* service + .readSummary({ ...WINDOW, awaitRefresh: true }) + .pipe(Effect.forkChild); + yield* Deferred.await(refreshingCurrent); + if (refreshCompleted) + assert.strictEqual(totalOutputTokens(yield* Fiber.join(second)), 11); + yield* Deferred.succeed(releaseOld, undefined); + yield* Fiber.join(first); + yield* Deferred.succeed(releaseCurrent, undefined); + assert.strictEqual(totalOutputTokens(yield* Fiber.join(second)), 11); + yield* service.awaitPersisted; + const config = yield* ServerConfig.ServerConfig; + const persisted = yield* Effect.promise(() => + NodeFSP.readFile( + NodePath.join(config.stateDir, CURSOR_ACCOUNT_CACHE_FILE_NAME), + "utf8", + ), + ); + assert.notInclude(persisted, "account-a"); + assert.include(persisted, "account-b"); + assert.strictEqual( + totalOutputTokens(yield* service.readSummary({ ...WINDOW, awaitRefresh: true })), + 11, + ); + assert.strictEqual(current.state.calls.length, 1); + }).pipe( + Effect.provide( + layerService({ prefix: "usage-service-cursor-prepare-order", home, settings }), + ), + ); + }).pipe(Effect.scoped, Effect.provide(TestClock.layer())), + ); + + it.live("does not reuse a Cursor failure or history after credentials change", () => + Effect.gen(function* () { + const { settings, home } = yield* setup; + yield* writeCursorLogin(home); + yield* TestClock.setTime(CURSOR_NOW); + const cursor = makeFakeCursor(); + cursor.state.error = "Old login failed."; + yield* Effect.gen(function* () { + const service = yield* makeWithCursor(cursor.read); + assert.strictEqual( + cursorSource(yield* service.readSummary({ ...WINDOW, awaitRefresh: true }))?.message, + "Old login failed.", + ); + yield* writeCursorLogin(home, "token-b"); + cursor.state.error = null; + cursor.state.accountKey = "account-b"; + cursor.state.events = [{ timestampMs: CURSOR_NOW - 3 * HOUR_MS, outputTokens: 11 }]; + assert.strictEqual( + totalOutputTokens(yield* service.readSummary({ ...WINDOW, awaitRefresh: true })), + 11, + ); + yield* replaceFile(NodePath.join(home, "config", "cursor", "auth.json"), "invalid json"); + const invalid = yield* service.readSummary({ ...WINDOW, awaitRefresh: true }); + assert.strictEqual(totalOutputTokens(invalid), 0); + assert.strictEqual(cursorSource(invalid)?.message, "Cursor credentials could not be read."); + yield* service.awaitPersisted; + }).pipe( + Effect.provide( + layerService({ prefix: "usage-service-cursor-switch-failure", home, settings }), + ), + ); + }).pipe(Effect.scoped, Effect.provide(TestClock.layer())), + ); + + it.live.each([false, true, "legacy"])( + "discards fresh Cursor usage after a login switch with restart %s", + (restart) => + Effect.gen(function* () { + const { settings, home } = yield* setup; + yield* writeCursorLogin(home); + yield* TestClock.setTime(CURSOR_NOW); + const cursor = makeFakeCursor(); + cursor.state.events = [{ timestampMs: CURSOR_NOW - 3 * HOUR_MS, outputTokens: 5 }]; + yield* Effect.gen(function* () { + const make = makeWithCursor(cursor.read); + const first = yield* make; + assert.strictEqual( + totalOutputTokens(yield* first.readSummary({ ...WINDOW, awaitRefresh: true })), + 5, + ); + yield* first.awaitPersisted; + if (restart === "legacy") { + const config = yield* ServerConfig.ServerConfig; + const cachePath = NodePath.join(config.stateDir, CURSOR_ACCOUNT_CACHE_FILE_NAME); + const document = yield* Effect.promise(() => NodeFSP.readFile(cachePath, "utf8")); + const cache = [ + ...decodeCursorAccountCaches(decodeUnknownJsonString(document)).values(), + ][0]; + assert.isDefined(cache); + if (cache !== undefined) { + yield* Effect.promise(() => + NodeFSP.writeFile( + cachePath, + encodeCursorAccountCaches( + new Map([[NodePath.join(home, "config", "cursor", "auth.json"), cache]]), + ), + ), + ); + } + } + + yield* writeCursorLogin(home, "token-b"); + cursor.state.accountKey = "account-b"; + cursor.state.events = [{ timestampMs: CURSOR_NOW - 3 * HOUR_MS, outputTokens: 11 }]; + const service = restart ? yield* make : first; + const switched = yield* service.readSummary({ ...WINDOW, awaitRefresh: true }); + assert.strictEqual(totalOutputTokens(switched), 11); + assert.strictEqual(cursorSource(switched)?.fingerprint.volumeId, "account-b"); + assert.strictEqual(cursor.state.calls.length, 2); + yield* service.awaitPersisted; + }).pipe( + Effect.provide( + layerService({ prefix: `usage-service-cursor-switch-${restart}`, home, settings }), + ), + ); + }).pipe(Effect.scoped, Effect.provide(TestClock.layer())), + ); + + it.live.each([false, true])( + "does not reuse Keychain history or failures after the login changes with legacy cache %s", + (legacy) => + Effect.gen(function* () { + const { settings, home } = yield* setup; + yield* TestClock.setTime(CURSOR_NOW); + let token = "token-a"; + const cursor = makeFakeCursor(); + cursor.state.events = [{ timestampMs: CURSOR_NOW - 3 * HOUR_MS, outputTokens: 5 }]; + yield* Effect.gen(function* () { + const make = makeWithCursor(cursor.read, (source) => + readCursorCredential( + source, + Effect.sync(() => token), + ), + ); + let service = yield* make; + assert.strictEqual( + totalOutputTokens(yield* service.readSummary({ ...WINDOW, awaitRefresh: true })), + 5, + ); + if (legacy) { + yield* service.awaitPersisted; + const config = yield* ServerConfig.ServerConfig; + const cachePath = NodePath.join(config.stateDir, CURSOR_ACCOUNT_CACHE_FILE_NAME); + const document = yield* Effect.promise(() => NodeFSP.readFile(cachePath, "utf8")); + const cache = [ + ...decodeCursorAccountCaches(decodeUnknownJsonString(document)).values(), + ][0]; + assert.isDefined(cache); + if (cache !== undefined) + yield* Effect.promise(() => + NodeFSP.writeFile( + cachePath, + encodeCursorAccountCaches(new Map([["keychain", cache]])), + ), + ); + service = yield* make; + } + token = "token-b"; + cursor.state.accountKey = "account-b"; + cursor.state.error = "New login failed."; + const failed = yield* service.readSummary({ ...WINDOW, awaitRefresh: true }); + assert.strictEqual(totalOutputTokens(failed), 0); + assert.strictEqual(cursorSource(failed)?.message, "New login failed."); + token = "token-c"; + cursor.state.accountKey = "account-c"; + cursor.state.error = null; + cursor.state.events = [{ timestampMs: CURSOR_NOW - 3 * HOUR_MS, outputTokens: 11 }]; + const switched = yield* service.readSummary({ ...WINDOW, awaitRefresh: true }); + assert.strictEqual(totalOutputTokens(switched), 11); + assert.strictEqual(cursorSource(switched)?.fingerprint.volumeId, "account-c"); + assert.strictEqual(cursor.state.calls.length, 3); + yield* service.awaitPersisted; + }).pipe( + Effect.provide( + layerService({ + prefix: "usage-service-cursor-keychain-switch", + home, + platform: "darwin", + settings: { ...settings, cursorKeychainUsageEnabled: true }, + }), + ), + ); + }).pipe(Effect.scoped, Effect.provide(TestClock.layer())), + ); + + it.live("observes Keychain login switches and logout through the production account reader", () => + Effect.gen(function* () { + const { settings, home } = yield* setup; + yield* TestClock.setTime(CURSOR_NOW); + let token: string | null = "token-a"; + let reads = 0; + const keychain = CursorKeychain.make( + Effect.sync(() => { + reads++; + return token; + }), + ); + const cursor = makeFakeCursor(); + cursor.state.events = [{ timestampMs: CURSOR_NOW - 3 * HOUR_MS, outputTokens: 5 }]; + yield* Effect.gen(function* () { + assert.strictEqual(yield* keychain.accessToken, "token-a"); + const context = yield* Layer.build( + Layer.fresh(CursorAccountReader.layer).pipe( + Layer.provide(Layer.succeed(CursorKeychain.CursorKeychain, keychain)), + ), + ); + const reader = Context.get(context, CursorAccountReader.CursorAccountReader); + const service = yield* makeWithCursor(cursor.read, reader.resolve); + assert.strictEqual( + totalOutputTokens(yield* service.readSummary({ ...WINDOW, awaitRefresh: true })), + 5, + ); + token = "token-b"; + cursor.state.accountKey = "account-b"; + cursor.state.events = [{ timestampMs: CURSOR_NOW - 3 * HOUR_MS, outputTokens: 11 }]; + const switched = yield* service.readSummary({ ...WINDOW, awaitRefresh: true }); + assert.strictEqual(totalOutputTokens(switched), 11); + assert.strictEqual(cursorSource(switched)?.fingerprint.volumeId, "account-b"); + token = null; + const loggedOut = yield* service.readSummary({ ...WINDOW, awaitRefresh: true }); + assert.strictEqual(totalOutputTokens(loggedOut), 0); + assert.strictEqual( + cursorSource(loggedOut)?.message, + "Cursor account history needs a macOS Keychain CLI login on this server.", + ); + assert.strictEqual(yield* keychain.accessToken, null); + assert.strictEqual(cursor.state.calls.length, 2); + assert.isAtLeast(reads, 4); + yield* service.awaitPersisted; + const config = yield* ServerConfig.ServerConfig; + const persisted = yield* Effect.promise(() => + NodeFSP.readFile(NodePath.join(config.stateDir, CURSOR_ACCOUNT_CACHE_FILE_NAME), "utf8"), + ); + assert.notInclude(persisted, "account-a"); + assert.notInclude(persisted, "account-b"); + }).pipe( + Effect.provide( + layerService({ + prefix: "usage-service-cursor-real-keychain", + home, + platform: "darwin", + settings: { ...settings, cursorKeychainUsageEnabled: true }, + }), + ), + ); + }).pipe(Effect.scoped, Effect.provide(TestClock.layer())), + ); + it.live("restores the Cursor account cache after a restart", () => Effect.gen(function* () { const { settings, home } = yield* setup; diff --git a/apps/server/src/usage/UsageService.ts b/apps/server/src/usage/UsageService.ts index 2259c1d9f773..74e3001c92ee 100644 --- a/apps/server/src/usage/UsageService.ts +++ b/apps/server/src/usage/UsageService.ts @@ -647,42 +647,64 @@ export const make = Effect.gen(function* () { } satisfies ScannedDir; }); + const prepareScans = (settings: ServerSettingsValue) => + Effect.forEach( + scanReaders, + ({ driver, reader }) => + usageInstances(driver, settings).pipe( + Effect.flatMap((instances) => + (reader.prepare + ? reader.prepare({ instances, settings }) + : Effect.succeed({ cacheKey: "", scan: reader.scan }) + ).pipe( + Effect.map((snapshot) => ({ + provider: reader.provider, + cacheKey: snapshot.cacheKey, + scan: (input: { + windowStartMs: number; + retentionCutoffMs: number; + awaitRefresh: boolean; + }) => snapshot.scan({ ...input, instances, settings }), + })), + ), + ), + Effect.provideContext(readerContext), + ), + { concurrency: "unbounded" }, + ); + type PreparedScans = Effect.Success>; + const collectDirs = Effect.fn("UsageService.collectDirs")(function* ( windowStartMs: number, settings: ServerSettingsValue, retentionCutoffMs: number, awaitRefresh: boolean, + preparedScans: PreparedScans, ) { const dirs = yield* resolveTranscriptDirs(settings, retentionCutoffMs).pipe( Effect.provideContext(readerContext), ); const scans = Effect.forEach( - scanReaders, - ({ driver, reader }) => - usageInstances(driver, settings) - .pipe( - Effect.flatMap((instances) => - reader.scan({ instances, settings, windowStartMs, retentionCutoffMs, awaitRefresh }), - ), - ) - .pipe( - Effect.flatMap((sources) => - Effect.forEach(sources, ({ volumeId, ...source }) => - Effect.map( - volumeId === undefined - ? Effect.promise(() => readDirectoryVolumeId(source.dir)) - : Effect.succeed(volumeId), - (resolved): ScannedDir => ({ - ...source, - provider: reader.provider, - volumeId: resolved, - }), - ), + preparedScans, + ({ provider, scan }) => + scan({ windowStartMs, retentionCutoffMs, awaitRefresh }).pipe( + Effect.flatMap((sources) => + Effect.forEach(sources, ({ volumeId, ...source }) => + Effect.map( + volumeId === undefined + ? Effect.promise(() => readDirectoryVolumeId(source.dir)) + : Effect.succeed(volumeId), + (resolved): ScannedDir => ({ + ...source, + provider, + volumeId: resolved, + }), ), ), - Effect.provideContext(readerContext), ), + Effect.provideContext(readerContext), + ), { concurrency: "unbounded" }, ); @@ -700,14 +722,8 @@ export const make = Effect.gen(function* () { const scanSummary = Effect.fn("UsageService.scanSummary")(function* ( input: UsageSummaryInput, settings: ServerSettingsValue, + preparedScans: PreparedScans, ) { - if (input.sinceDay > input.untilDay) { - return yield* new UsageReadError({ - reason: "invalidWindow", - detail: `sinceDay '${input.sinceDay}' is after untilDay '${input.untilDay}'`, - }); - } - let hourlyWindow: { readonly sinceTimeMs: number; readonly untilTimeMs: number } | null = null; if (input.resolution === "hour") { const sinceTime = @@ -754,7 +770,13 @@ export const make = Effect.gen(function* () { const [, scannedDirs] = yield* Effect.all( [ ensureRates(false), - collectDirs(windowStartMs, settings, retentionCutoffMs, input.awaitRefresh === true), + collectDirs( + windowStartMs, + settings, + retentionCutoffMs, + input.awaitRefresh === true, + preparedScans, + ), ], { concurrency: 2 }, ); @@ -896,8 +918,19 @@ export const make = Effect.gen(function* () { ]); const readSummary = Effect.fn("UsageService.readSummary")(function* (input: UsageSummaryInput) { + if (input.sinceDay > input.untilDay) { + return yield* new UsageReadError({ + reason: "invalidWindow", + detail: `sinceDay '${input.sinceDay}' is after untilDay '${input.untilDay}'`, + }); + } + const settings = yield* readSettings; - const key = scanKey(input, settings); + const preparedScans = yield* prepareScans(settings); + const key = encodeUsageRecordKey([ + scanKey(input, settings), + preparedScans.map((scan) => scan.cacheKey), + ]); const deferred = yield* Effect.uninterruptible( Effect.gen(function* () { const existing = inflightScans.get(key); @@ -911,7 +944,7 @@ export const make = Effect.gen(function* () { // the fibers awaiting it; a finished scan warms the cache either way. // The cache write is registered before the waiters resume, so they // can await it, but its fiber starts after they have the summary. - yield* scanSummary(input, settings).pipe( + yield* scanSummary(input, settings, preparedScans).pipe( Effect.onExit((exit) => Effect.sync(() => inflightScans.delete(key)).pipe( Effect.andThen(Exit.isSuccess(exit) ? schedulePersist : Effect.void), diff --git a/apps/web/src/components/preview/useBrowserClone.ts b/apps/web/src/components/preview/useBrowserClone.ts index 4573e03df2c9..1f3a8000e550 100644 --- a/apps/web/src/components/preview/useBrowserClone.ts +++ b/apps/web/src/components/preview/useBrowserClone.ts @@ -1,5 +1,7 @@ "use client"; +import { useAtomValue } from "@effect/atom-react"; + import type { PreviewAutomationFrame, PreviewCloneInput, @@ -18,6 +20,9 @@ export function useBrowserClone( tabId: string | null, visible: boolean, ) { + const canOperate = useAtomValue( + previewEnvironment.cloneInvoke.permissionAtom(threadRef.environmentId), + ); const invoke = useAtomCommand(previewEnvironment.cloneInvoke, { reportFailure: false }); const [generation, setGeneration] = useState(0); const [frame, setFrame] = useState(null); @@ -29,7 +34,7 @@ export function useBrowserClone( setFrame(null); setError(null); failedRef.current = false; - if (!visible || !tabId) return; + if (!visible || !tabId || !canOperate) return; const session = createBrowserCloneSession( { environmentId, threadId, tabId, cloneId: createPreviewAutomationClientId() }, async (input) => { @@ -73,7 +78,7 @@ export function useBrowserClone( session.dispose(); if (sessionRef.current === session) sessionRef.current = null; }; - }, [environmentId, threadId, tabId, visible, generation, invoke]); + }, [environmentId, threadId, tabId, visible, generation, invoke, canOperate]); const fail = useCallback((session: ReturnType) => { if (sessionRef.current === session) { failedRef.current = true; diff --git a/apps/web/src/components/settings/ProviderInstanceCard.tsx b/apps/web/src/components/settings/ProviderInstanceCard.tsx index b65a200424ce..44f2c8c925a1 100644 --- a/apps/web/src/components/settings/ProviderInstanceCard.tsx +++ b/apps/web/src/components/settings/ProviderInstanceCard.tsx @@ -1,5 +1,8 @@ "use client"; +import { useAtomValue } from "@effect/atom-react"; +import { providerLoginEnvironment } from "../../state/providerLogin"; + import { Spinner } from "~/components/ui/spinner"; import { @@ -904,6 +907,9 @@ export function ProviderInstanceCard({ const rawHomePath = (instance.config as { readonly homePath?: unknown } | undefined)?.homePath; const homePathLine = typeof rawHomePath === "string" && rawHomePath.trim().length > 0 ? rawHomePath.trim() : null; + const canManageLogin = useAtomValue( + providerLoginEnvironment.write.permissionAtom(environmentId ?? null), + ); const canLogin = DRIVERS_WITH_IN_APP_LOGIN.has(String(instance.driver)) && environmentId !== undefined && @@ -1448,7 +1454,7 @@ export function ProviderInstanceCard({ setIsLoginOpen(true)}> + diff --git a/packages/client-runtime/src/state/commandPermissions.test.ts b/packages/client-runtime/src/state/commandPermissions.test.ts index 58a77730a401..e77208acd28a 100644 --- a/packages/client-runtime/src/state/commandPermissions.test.ts +++ b/packages/client-runtime/src/state/commandPermissions.test.ts @@ -8,6 +8,8 @@ import { describe, expect, it } from "@effect/vitest"; import { vi } from "vite-plus/test"; import { AuthOrchestrationOperateScope, + AuthProvidersManageScope, + AuthPreviewOperateScope, AuthSettingsWriteScope, AuthSourceControlWriteScope, ThreadId, @@ -309,3 +311,38 @@ it.effect("rejects protected unary and streamed RPCs outside a guarded command", expect(writes).toBe(0); }), ); + +it.effect.each([ + { method: WS_METHODS.providerLoginWrite, requiredScope: AuthProvidersManageScope }, + { method: WS_METHODS.providerLoginResize, requiredScope: AuthProvidersManageScope }, + { method: WS_METHODS.providerLoginCancel, requiredScope: AuthProvidersManageScope }, + { method: WS_METHODS.previewCloneInvoke, requiredScope: AuthPreviewOperateScope }, + { method: WS_METHODS.previewAutomationRespond, requiredScope: AuthPreviewOperateScope }, + { method: WS_METHODS.previewAutomationFocusHost, requiredScope: AuthPreviewOperateScope }, +] as const)("requires the destination grant for $method", ({ method, requiredScope }) => + Effect.scoped( + Effect.gen(function* () { + const registry = yield* setup; + const command = createCommandPermissions(runtime, method); + registry.set(sessions(env), AsyncResult.success(grant(true))); + expect(registry.get(command.permissionAtom(env))).toBe(false); + const denied = yield* command.authorize(registry, env).pipe(Effect.flip); + expect(denied.requiredPermission).toBe(requiredScope); + registry.set( + sessions(env), + AsyncResult.success({ + ...grant(false), + scopes: [requiredScope], + permissions: [requiredScope], + }), + ); + expect(registry.get(command.permissionAtom(env))).toBe(true); + yield* command.authorize(registry, env); + registry.set(sessions(other), AsyncResult.success(grant(false))); + expect(registry.get(command.permissionAtom(other))).toBe(false); + expect((yield* command.authorize(registry, other).pipe(Effect.flip)).requiredPermission).toBe( + requiredScope, + ); + }), + ), +); diff --git a/packages/client-runtime/src/state/threadHistoryController.test.ts b/packages/client-runtime/src/state/threadHistoryController.test.ts index f449f1120a23..c66549994c88 100644 --- a/packages/client-runtime/src/state/threadHistoryController.test.ts +++ b/packages/client-runtime/src/state/threadHistoryController.test.ts @@ -1,9 +1,23 @@ -import type { EnvironmentId, ThreadId } from "@t3tools/contracts"; +import { MessageId, TurnItemId, type EnvironmentId, type ThreadId } from "@t3tools/contracts"; import { describe, expect, it } from "@effect/vitest"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; +import * as SubscriptionRef from "effect/SubscriptionRef"; +import { HttpClient, HttpClientResponse } from "effect/http"; +import { + AVAILABLE_CONNECTION_STATE, + PrimaryConnectionTarget, + type PreparedConnection, +} from "../connection/model.ts"; +import * as EnvironmentSupervisor from "../connection/supervisor.ts"; +import * as Persistence from "../platform/persistence.ts"; +import type { RpcSession } from "../rpc/session.ts"; +import { v2Now, v2Projection, v2ThreadId } from "./orchestrationV2TestFixtures.ts"; import * as ThreadHistoryController from "./threadHistoryController.ts"; +import { makeEnvironmentThreadState } from "./threads.ts"; +import * as ThreadSnapshotLoader from "./threadSnapshotHttp.ts"; const ENV = "env-history" as EnvironmentId; const THREAD = "thread-history" as ThreadId; @@ -29,6 +43,173 @@ function handler( } describe("ThreadHistoryController", () => { + it.effect("keeps 40 search turns in order after activity fails and a normal 20-turn retry", () => + Effect.gen(function* () { + const controller = yield* ThreadHistoryController.ThreadHistoryController; + const target = new PrimaryConnectionTarget({ + environmentId: ENV, + label: "History test", + httpBaseUrl: "https://history.example.test", + wsBaseUrl: "wss://history.example.test", + }); + const message = (index: number) => ({ + id: TurnItemId.make(`message-item-${index}`), + type: "assistant_message" as const, + threadId: v2ThreadId, + runId: null, + nodeId: null, + providerThreadId: null, + providerTurnId: null, + nativeItemRef: null, + parentItemId: null, + ordinal: index * 2 + 1, + status: "completed" as const, + title: null, + messageId: MessageId.make(`message-${index}`), + text: `Message ${index}`, + streaming: false, + startedAt: v2Now, + completedAt: v2Now, + updatedAt: v2Now, + }); + const activity = (index: number) => ({ + ...message(index), + id: TurnItemId.make(`activity-${index}`), + type: "command_execution" as const, + ordinal: index * 2 + 2, + input: "pwd", + output: "", + exitCode: 0, + }); + const projected = (item: ReturnType | ReturnType) => ({ + position: 0, + visibility: "local" as const, + sourceThreadId: v2ThreadId, + sourceItemId: item.id, + item, + }); + const cold = projected(message(40)); + const projection = { ...v2Projection, turnItems: [cold.item], visibleTurnItems: [cold] }; + const state = yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE); + const session = yield* SubscriptionRef.make(Option.none()); + const prepared = yield* SubscriptionRef.make>( + Option.some({ + environmentId: ENV, + label: target.label, + httpBaseUrl: target.httpBaseUrl, + socketUrl: target.wsBaseUrl, + httpAuthorization: null, + target, + }), + ); + const requests: Array<{ view: string | null; through: string | null }> = []; + const threadState = yield* makeEnvironmentThreadState(v2ThreadId).pipe( + Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, { + target, + state, + session, + prepared, + connect: Effect.void, + disconnect: Effect.void, + retryNow: Effect.void, + }), + Effect.provideService(ThreadSnapshotLoader.ThreadSnapshotLoader, { + load: () => Effect.succeed({ _tag: "unavailable" }), + }), + Effect.provideService(Persistence.EnvironmentCacheStore, { + loadThread: () => + Effect.succeedSome({ + snapshotSequence: 14, + projection, + historyCursor: "search-cursor", + hasMoreHistory: true, + }), + saveThread: () => Effect.void, + removeThread: () => Effect.void, + loadShell: () => Effect.succeedNone, + saveShell: () => Effect.void, + loadServerConfig: () => Effect.succeedNone, + saveServerConfig: () => Effect.void, + loadVcsRefs: () => Effect.succeedNone, + saveVcsRefs: () => Effect.void, + removeVcsRefs: () => Effect.void, + clearVcsRefs: () => Effect.void, + clear: () => Effect.void, + }), + Effect.provideService( + HttpClient.HttpClient, + HttpClient.make((request, url) => { + expect(url.searchParams.get("cursor")).toBe("search-cursor"); + const view = url.searchParams.get("view"); + const through = url.searchParams.get("throughEntryId"); + requests.push({ view, through }); + if (view === null && through !== null) { + return Effect.succeed( + HttpClientResponse.fromWeb( + request, + new Response("Activity unavailable", { status: 503 }), + ), + ); + } + const items = + view === "conversation" + ? Array.from({ length: 40 }, (_, index) => projected(message(index))) + : Array.from({ length: 20 }, (_, index) => [ + projected(message(index + 20)), + projected(activity(index + 20)), + ]).flat(); + return Effect.succeed( + HttpClientResponse.fromWeb( + request, + Response.json({ + snapshotSequence: 14, + items, + nextCursor: "older-cursor", + hasMoreHistory: true, + }), + ), + ); + }), + ), + ); + expect(yield* controller.loadEarlier(ENV, v2ThreadId, "message-0")).toMatchObject({ + _tag: "error", + }); + const failed = yield* SubscriptionRef.get(threadState); + expect(failed.history).toMatchObject({ + historyCursor: "search-cursor", + loading: false, + expanded: true, + }); + expect(failed.history.error).not.toBeNull(); + expect( + Option.getOrThrow(failed.data).visibleTurnItems.map((row) => row.sourceItemId), + ).toEqual(Array.from({ length: 41 }, (_, index) => `message-item-${index}`)); + expect(yield* controller.loadEarlier(ENV, v2ThreadId)).toEqual({ _tag: "loaded" }); + const retried = yield* SubscriptionRef.get(threadState); + expect( + Option.getOrThrow(retried.data).visibleTurnItems.map((row) => row.sourceItemId), + ).toEqual([ + ...Array.from({ length: 20 }, (_, index) => `message-item-${index}`), + ...Array.from({ length: 20 }, (_, index) => [ + `message-item-${index + 20}`, + `activity-${index + 20}`, + ]).flat(), + "message-item-40", + ]); + expect(retried.history).toMatchObject({ + historyCursor: "older-cursor", + loading: false, + error: null, + }); + expect(requests).toEqual([ + { view: "conversation", through: "message-0" }, + { view: null, through: "message-0" }, + { view: null, through: null }, + ]); + }).pipe(Effect.provide(Layer.fresh(ThreadHistoryController.layer))), + ); + it.effect("does not let an older finalizer delete a newer registration", () => Effect.gen(function* () { const controller = yield* ThreadHistoryController.ThreadHistoryController; diff --git a/packages/client-runtime/src/state/threadHistoryMerge.test.ts b/packages/client-runtime/src/state/threadHistoryMerge.test.ts index 56ef3d235df9..101f1d9891ab 100644 --- a/packages/client-runtime/src/state/threadHistoryMerge.test.ts +++ b/packages/client-runtime/src/state/threadHistoryMerge.test.ts @@ -90,6 +90,74 @@ describe("threadHistoryMerge", () => { ]); }); + it("fills an overlapping retry page in place without moving older conversation rows", () => { + const loaded = [row(1), row(3), row(5), row(7), row(9)]; + const live = { ...row(5), item: { ...row(5).item, output: "live output" } }; + const projection = { + ...v2Projection, + turnItems: loaded.map((entry) => + entry.sourceItemId === live.sourceItemId ? live.item : entry.item, + ), + visibleTurnItems: loaded.map((entry) => + entry.sourceItemId === live.sourceItemId ? live : entry, + ), + }; + const merged = mergeOlderHistoryIntoProjection(projection, [ + row(4), + row(5), + row(6), + row(7), + row(7), + row(8), + ]); + expect(merged.visibleTurnItems.map((entry) => entry.sourceItemId)).toEqual([ + "item-1", + "item-3", + "item-4", + "item-5", + "item-6", + "item-7", + "item-8", + "item-9", + ]); + expect(merged.visibleTurnItems[3]?.item).toBe(live.item); + expect(merged.visibleTurnItems.map((entry) => entry.position)).toEqual([ + 0, 1, 2, 3, 4, 5, 6, 7, + ]); + expect(new Set(merged.turnItems.map((item) => item.id)).size).toBe(8); + expect(mergeOlderHistoryIntoProjection(merged, [row(4), row(5), row(6), row(7), row(8)])).toBe( + merged, + ); + }); + + it("preserves existing gaps between overlap anchors", () => { + const loaded = [row(0), row(2), row(3), row(6), row(8)]; + const projection = { + ...v2Projection, + turnItems: loaded.map((entry) => entry.item), + visibleTurnItems: loaded, + }; + const merged = mergeOlderHistoryIntoProjection(projection, [ + row(1), + row(2), + row(4), + row(5), + row(6), + row(7), + ]); + expect(merged.visibleTurnItems.map((entry) => entry.sourceItemId)).toEqual([ + "item-0", + "item-1", + "item-2", + "item-3", + "item-4", + "item-5", + "item-6", + "item-7", + "item-8", + ]); + }); + it("prepends older rows and dedupes by source identity", () => { const recent = [row(2), row(3)]; const projection = { @@ -157,6 +225,30 @@ describe("threadHistoryMerge", () => { ]); }); + it("anchors an inherited retry by source identity while retaining its older prefix", () => { + const inherited = (index: number) => ({ + ...row(index), + visibility: "inherited" as const, + sourceThreadId: "fork-source" as never, + }); + const local = row(1); + const projection = { + ...v2Projection, + turnItems: [local.item], + visibleTurnItems: [inherited(0), inherited(2), local], + }; + const merged = mergeOlderHistoryIntoProjection(projection, [inherited(1), inherited(2)]); + expect( + merged.visibleTurnItems.map((entry) => [entry.sourceThreadId, entry.sourceItemId]), + ).toEqual([ + ["fork-source", "item-0"], + ["fork-source", "item-1"], + ["fork-source", "item-2"], + [v2ThreadId, "item-1"], + ]); + expect(merged.turnItems).toEqual([local.item]); + }); + it("does not resurrect a local item hidden while an older page was in flight", () => { const stalePageRow = row(1); const currentItem = { diff --git a/packages/client-runtime/src/state/threadHistoryMerge.ts b/packages/client-runtime/src/state/threadHistoryMerge.ts index ef8bd219b9ea..034e33309cf1 100644 --- a/packages/client-runtime/src/state/threadHistoryMerge.ts +++ b/packages/client-runtime/src/state/threadHistoryMerge.ts @@ -112,12 +112,34 @@ export function mergeOlderHistoryIntoProjection( turnItemById.set(String(row.sourceItemId), row.item); } if (page.length === 0) return projection; - // Server page order fills gaps between rows from a partial conversation load. - // Existing rows supply live values; rows outside this range keep their order. - const visible = [ - ...page, - ...projection.visibleTurnItems.filter((row) => !pageKeys.has(projectedItemKey(row))), - ]; + // Shared rows anchor a retry page inside a larger conversation-only load. + // Keep existing gaps and rows outside the page in their current order. + const pageIndexByKey = new Map(); + let lastOverlapIndex = -1; + for (const [index, row] of page.entries()) { + const key = projectedItemKey(row); + pageIndexByKey.set(key, index); + if (currentByKey.has(key)) lastOverlapIndex = index; + } + const visible: OrchestrationV2ProjectedTurnItem[] = []; + let pageOffset = 0; + if (lastOverlapIndex === -1) { + for (const row of page) visible.push(row); + pageOffset = page.length; + } + for (const row of projection.visibleTurnItems) { + const pageIndex = pageIndexByKey.get(projectedItemKey(row)); + if (pageIndex === undefined) { + visible.push(row); + continue; + } + for (const pageRow of page.slice(pageOffset, pageIndex + 1)) visible.push(pageRow); + pageOffset = pageIndex + 1; + if (pageIndex === lastOverlapIndex) { + for (const pageRow of page.slice(pageOffset)) visible.push(pageRow); + pageOffset = page.length; + } + } if ( visible.length === projection.visibleTurnItems.length && visible.every((row, index) => row === projection.visibleTurnItems[index]) diff --git a/packages/contracts/src/clientRpcPermissions.ts b/packages/contracts/src/clientRpcPermissions.ts index 12c3b479d5df..df3ee817899c 100644 --- a/packages/contracts/src/clientRpcPermissions.ts +++ b/packages/contracts/src/clientRpcPermissions.ts @@ -3,6 +3,8 @@ import { GitPreparePullRequestThreadInput } from "./git.ts"; import { AuthEnvironmentMaintainScope, AuthOrchestrationOperateScope, + AuthProvidersManageScope, + AuthPreviewOperateScope, AuthSettingsWriteScope, AuthSourceControlWriteScope, type AuthEnvironmentScope, @@ -12,6 +14,12 @@ import { WS_METHODS } from "./rpc.ts"; /** Incremental client enforcement; the server still authorizes every request. */ export const CLIENT_GUARDED_RPC_SCOPES = { + [WS_METHODS.providerLoginWrite]: AuthProvidersManageScope, + [WS_METHODS.providerLoginResize]: AuthProvidersManageScope, + [WS_METHODS.providerLoginCancel]: AuthProvidersManageScope, + [WS_METHODS.previewCloneInvoke]: AuthPreviewOperateScope, + [WS_METHODS.previewAutomationRespond]: AuthPreviewOperateScope, + [WS_METHODS.previewAutomationFocusHost]: AuthPreviewOperateScope, [WS_METHODS.serverRunStorageCleanup]: AuthSettingsWriteScope, // Endpoint checks exist to configure exports, so they need the same grant as saving them. [WS_METHODS.serverCheckOtlpEndpoint]: AuthSettingsWriteScope, diff --git a/packages/provider-core/src/server/usage.ts b/packages/provider-core/src/server/usage.ts index f69e9b77175d..c4e3e811605c 100644 --- a/packages/provider-core/src/server/usage.ts +++ b/packages/provider-core/src/server/usage.ts @@ -179,6 +179,19 @@ export type ProviderUsageReader = | { readonly kind: "scan"; readonly provider: UsageProviderKind; + /** Snapshots mutable account identity before the server shares an in-flight scan. */ + readonly prepare?: ( + input: Pick, "instances" | "settings">, + ) => Effect.Effect< + { + readonly cacheKey: string; + readonly scan: ( + input: ProviderUsageScanInput, + ) => Effect.Effect, UsageReadError, R>; + }, + UsageReadError, + R + >; /** Fails only when the whole usage read cannot go on; an unreadable source is a status. */ readonly scan: ( input: ProviderUsageScanInput, diff --git a/packages/provider-cursor/src/server/CursorAccountReader.ts b/packages/provider-cursor/src/server/CursorAccountReader.ts index 886bfe2f8737..f9e7e738a696 100644 --- a/packages/provider-cursor/src/server/CursorAccountReader.ts +++ b/packages/provider-cursor/src/server/CursorAccountReader.ts @@ -12,14 +12,20 @@ import * as HttpClient from "effect/http/HttpClient"; import * as Layer from "effect/Layer"; import type { CursorCredentialSource } from "./accountCache.ts"; -import { readCursorAccountUsage, type CursorAccountUsageReadResult } from "./accountUsage.ts"; +import { + readCursorAccountUsage, + readCursorCredential, + type CursorResolvedCredential, + type CursorAccountUsageReadResult, +} from "./accountUsage.ts"; import * as CursorKeychain from "./CursorKeychain.ts"; export class CursorAccountReader extends Context.Service< CursorAccountReader, { + readonly resolve: (source: CursorCredentialSource) => Effect.Effect; readonly read: ( - credentialSource: CursorCredentialSource, + credentialSource: CursorCredentialSource | CursorResolvedCredential, sinceMs: number, untilMs: number, ) => Effect.Effect; @@ -35,6 +41,10 @@ export const layer = Layer.effect( FileSystem.FileSystem | Crypto.Crypto | HttpClient.HttpClient >(); return CursorAccountReader.of({ + resolve: (source) => + readCursorCredential(source, CursorKeychain.freshAccessToken(keychain)).pipe( + Effect.provideContext(context), + ), // Only a Keychain login asks the Keychain. The dashboard reader turns // its outcome, a failure included, into the source's message. read: (credentialSource, sinceMs, untilMs) => diff --git a/packages/provider-cursor/src/server/CursorKeychain.test.ts b/packages/provider-cursor/src/server/CursorKeychain.test.ts index ebdcdbd15fe5..a4dcc518f147 100644 --- a/packages/provider-cursor/src/server/CursorKeychain.test.ts +++ b/packages/provider-cursor/src/server/CursorKeychain.test.ts @@ -30,25 +30,64 @@ describe("CursorKeychain", () => { }), ); - it.effect("gives up on an unanswered prompt and reuses it on the next read", () => + it.effect("shares fresh reads and updates the cache after login changes and logout", () => Effect.gen(function* () { let reads = 0; - const allow = yield* Deferred.make(); + let token: string | null = "token-a"; + const started = yield* Deferred.make(); + const allow = yield* Deferred.make(); const keychain = CursorKeychain.make( - Effect.suspend(() => { + Effect.gen(function* () { reads++; - return Deferred.await(allow); + if (reads === 2) { + yield* Deferred.succeed(started, undefined); + yield* Deferred.await(allow); + } + return token; }), - Duration.millis(1), ); - const timedOut = yield* keychain.accessToken.pipe(Effect.forkChild); - yield* TestClock.adjust(Duration.millis(1)); - const error = yield* Fiber.join(timedOut).pipe(Effect.flip); - assert.isTrue(CursorKeychain.isCursorKeychainTimeoutError(error)); - const retry = yield* keychain.accessToken.pipe(Effect.forkChild); - yield* Deferred.succeed(allow, "token"); - assert.strictEqual(yield* Fiber.join(retry), "token"); - assert.strictEqual(reads, 1); + assert.strictEqual(yield* keychain.accessToken, "token-a"); + token = "token-b"; + const first = yield* CursorKeychain.freshAccessToken(keychain).pipe(Effect.forkChild); + yield* Deferred.await(started); + // Both callers enroll before the blocked native read completes. + const second = yield* CursorKeychain.freshAccessToken(keychain).pipe( + Effect.forkChild({ startImmediately: true }), + ); + assert.strictEqual(yield* keychain.accessToken, "token-a"); + yield* Deferred.succeed(allow, undefined); + assert.strictEqual(yield* Fiber.join(first), "token-b"); + assert.strictEqual(yield* Fiber.join(second), "token-b"); + assert.strictEqual(yield* keychain.accessToken, "token-b"); + assert.strictEqual(reads, 2); + token = null; + assert.isNull(yield* CursorKeychain.freshAccessToken(keychain)); + assert.isNull(yield* keychain.accessToken); }), ); + + it.effect.each([false, true])( + "gives up on an unanswered prompt and reuses it on the next read with fresh %s", + (fresh) => + Effect.gen(function* () { + let reads = 0; + const allow = yield* Deferred.make(); + const keychain = CursorKeychain.make( + Effect.suspend(() => { + reads++; + return Deferred.await(allow); + }), + Duration.millis(1), + ); + const read = fresh ? CursorKeychain.freshAccessToken(keychain) : keychain.accessToken; + const timedOut = yield* read.pipe(Effect.forkChild); + yield* TestClock.adjust(Duration.millis(1)); + const error = yield* Fiber.join(timedOut).pipe(Effect.flip); + assert.isTrue(CursorKeychain.isCursorKeychainTimeoutError(error)); + const retry = yield* read.pipe(Effect.forkChild({ startImmediately: true })); + yield* Deferred.succeed(allow, "token"); + assert.strictEqual(yield* Fiber.join(retry), "token"); + assert.strictEqual(reads, 1); + }), + ); }); diff --git a/packages/provider-cursor/src/server/CursorKeychain.ts b/packages/provider-cursor/src/server/CursorKeychain.ts index 30beb6703434..11d3a327c628 100644 --- a/packages/provider-cursor/src/server/CursorKeychain.ts +++ b/packages/provider-cursor/src/server/CursorKeychain.ts @@ -23,6 +23,14 @@ import * as Schema from "effect/Schema"; const CACHE_MS = 5 * 60_000; const DEFAULT_TIMEOUT = Duration.seconds(30); +const FreshAccessToken = Context.Reference("CursorKeychain/FreshAccessToken", { + defaultValue: () => false, +}); + +/** Bypasses the saved token while still joining an outstanding Keychain prompt. */ +export const freshAccessToken = (keychain: CursorKeychain["Service"]) => + keychain.accessToken.pipe(Effect.provideService(FreshAccessToken, true)); + const requireForKeyring = NodeModule.createRequire(import.meta.url); /** Nobody answered the macOS Keychain prompt in time. */ @@ -104,8 +112,9 @@ export const make = ( ); const accessToken = Effect.gen(function* () { + const fresh = yield* FreshAccessToken; const nowMs = yield* Clock.currentTimeMillis; - if (cached !== null && cached.untilMs > nowMs) return cached.token; + if (!fresh && cached !== null && cached.untilMs > nowMs) return cached.token; const done = yield* startRead; const token = yield* Deferred.await(done).pipe(Effect.timeoutOption(timeout)); if (Option.isNone(token)) return yield* new CursorKeychainTimeoutError(); diff --git a/packages/provider-cursor/src/server/CursorUsageAccounts.ts b/packages/provider-cursor/src/server/CursorUsageAccounts.ts index 24f4ff87afff..6ef10b8950ef 100644 --- a/packages/provider-cursor/src/server/CursorUsageAccounts.ts +++ b/packages/provider-cursor/src/server/CursorUsageAccounts.ts @@ -35,10 +35,12 @@ import { isCursorCacheFresh, mergeCursorFetch, type CursorAccountCache, - type CursorCredentialSource, } from "./accountCache.ts"; +import type { CursorResolvedCredential } from "./accountUsage.ts"; import * as CursorAccountReader from "./CursorAccountReader.ts"; +const encodeCredentialKey = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown)); + const CURSOR_ACCOUNT_READ_ERROR = "Cursor account usage could not be read."; /** The cache file is narrowed by hand in `accountCache`, so JSON is enough here. */ @@ -46,6 +48,12 @@ const decodeCacheFile = Schema.decodeUnknownEffect( Schema.fromJsonString(Schema.Unknown as unknown as Schema.Codec), ); +type ScanInput = { + readonly windowStartMs: number; + readonly retentionCutoffMs: number; + readonly awaitRefresh: boolean; +}; + export class CursorUsageAccounts extends Context.Service< CursorUsageAccounts, { @@ -57,6 +65,10 @@ export class CursorUsageAccounts extends Context.Service< readonly retentionCutoffMs: number; readonly awaitRefresh: boolean; }) => Effect.Effect>; + readonly prepare: (keychainUsageEnabled: boolean) => Effect.Effect<{ + readonly cacheKey: string; + readonly scan: (input: ScanInput) => Effect.Effect>; + }>; /** Waits for every cache write scheduled so far, as a restart would need. */ readonly awaitPersisted: Effect.Effect; } @@ -71,16 +83,18 @@ const make = Effect.gen(function* () { const platform = yield* HostProcess.Platform; const cachePath = path.join(host.paths.stateDir, CURSOR_ACCOUNT_CACHE_FILE_NAME); - /** Account caches by credential source. */ + /** Account caches by credential source and token fingerprint. */ const caches = new Map(); + let activeCredentialKey: string | undefined; + let preparationGeneration = 0; let cacheDirty = false; /** - * The last failed refresh per credential source, standing for a TTL so a + * The last failed refresh per credential, standing for a TTL so a * client refetching a broken login does not refetch Cursor each time. A * `null` error means there is no login: no source to report. */ const failures = new Map(); - /** The refresh in flight per credential source, which every read joins. */ + /** The refresh in flight per credential, which every read joins. */ const refreshes = new Map>(); /** Loads the persisted caches once; concurrent first readers await the same load. */ @@ -129,7 +143,7 @@ const make = Effect.gen(function* () { /** Fetches what one account cache is missing, then persists it if anything changed. */ const refreshAccount = Effect.fn("CursorUsageAccounts.refreshAccount")(function* ( - credential: CursorCredentialSource, + credential: CursorResolvedCredential, credentialKey: string, retentionStartMs: number, ) { @@ -142,6 +156,7 @@ const make = Effect.gen(function* () { }; let base = caches.get(credentialKey); let fetched = yield* fetchMissing(base); + if (activeCredentialKey !== credentialKey) return; // Another login's history replaces the cached account's, even if reading it fails. if ( base !== undefined && @@ -152,6 +167,7 @@ const make = Effect.gen(function* () { cacheDirty = true; base = undefined; fetched = yield* fetchMissing(base); + if (activeCredentialKey !== credentialKey) return; } const { range, result } = fetched; if (result.missing || result.error !== null || result.accountKey === null) { @@ -181,7 +197,7 @@ const make = Effect.gen(function* () { /** Joins the refresh in flight, or starts one. */ const startRefresh = ( - credential: CursorCredentialSource, + credential: CursorResolvedCredential, credentialKey: string, retentionStartMs: number, ) => @@ -197,9 +213,10 @@ const make = Effect.gen(function* () { yield* refreshAccount(credential, credentialKey, retentionStartMs).pipe( Effect.catchCause(() => Clock.currentTimeMillis.pipe( - Effect.map((atMs) => - failures.set(credentialKey, { atMs, error: CURSOR_ACCOUNT_READ_ERROR }), - ), + Effect.map((atMs) => { + if (activeCredentialKey === credentialKey) + failures.set(credentialKey, { atMs, error: CURSOR_ACCOUNT_READ_ERROR }); + }), ), ), Effect.ensuring( @@ -220,21 +237,21 @@ const make = Effect.gen(function* () { * waits for it, and anything else answers from the cache marked `refreshing`. */ const accountSource = Effect.fn("CursorUsageAccounts.accountSource")(function* ( - credential: CursorCredentialSource, + credential: CursorResolvedCredential, + credentialKey: string, authPath: string, windowStartMs: number, retentionStartMs: number, awaitRefresh: boolean, ) { - // No saved login means there is no account source to report, not a setup error. - if ( - typeof credential === "string" && - !(yield* fileSystem.exists(credential).pipe(Effect.orElseSucceed(() => true))) - ) { - return null; + if (credential.kind === "unavailable") { + if (credential.missing && credential.error === null) return null; + return { + dir: authPath, + files: null, + message: credential.error ?? CURSOR_ACCOUNT_READ_ERROR, + } satisfies ProviderUsageScan; } - yield* ensureLoaded; - const credentialKey = typeof credential === "string" ? credential : "keychain"; const nowMs = yield* Clock.currentTimeMillis; const recentFailure = failures.get(credentialKey); let refreshing = false; @@ -280,62 +297,101 @@ const make = Effect.gen(function* () { } satisfies ProviderUsageScan; }); - const scan: CursorUsageAccounts["Service"]["scan"] = Effect.fn("CursorUsageAccounts.scan")( - function* ({ keychainUsageEnabled, windowStartMs, retentionCutoffMs, awaitRefresh }) { - const home = yield* HostProcess.HomeDirectory; - const userHome = - (platform === "win32" ? hostEnvironment["USERPROFILE"] : hostEnvironment["HOME"]) || home; - const configHome = hostEnvironment["XDG_CONFIG_HOME"]?.trim(); - const cursorHome = - platform === "darwin" - ? path.join(userHome, "Library", "Application Support") - : platform === "win32" - ? hostEnvironment["APPDATA"] || path.join(userHome, "AppData", "Roaming") - : configHome && path.isAbsolute(configHome) - ? configHome - : path.join(userHome, ".config"); - const authPath = - platform === "darwin" - ? path.join(userHome, ".cursor", "auth.json") - : path.join(cursorHome, platform === "win32" ? "Cursor" : "cursor", "auth.json"); - const credentialStore = hostEnvironment["AGENT_CLI_CREDENTIAL_STORE"]; - const loginUnavailable = - Boolean(hostEnvironment["CURSOR_AUTH_TOKEN"]?.trim()) || - Boolean(hostEnvironment["CURSOR_API_KEY"]?.trim()) || - credentialStore === "memory"; - const useKeychain = platform === "darwin" && credentialStore !== "file"; - if (useKeychain && !loginUnavailable && !keychainUsageEnabled) { - return [ - { - dir: authPath, - volumeId: "", - files: null, - message: "Cursor account usage is off on this environment.", - action: "enableCursorKeychain", - }, - ]; + const prepare: CursorUsageAccounts["Service"]["prepare"] = Effect.fn( + "CursorUsageAccounts.prepare", + )(function* (keychainUsageEnabled) { + const generation = ++preparationGeneration; + const home = yield* HostProcess.HomeDirectory; + const userHome = + (platform === "win32" ? hostEnvironment["USERPROFILE"] : hostEnvironment["HOME"]) || home; + const configHome = hostEnvironment["XDG_CONFIG_HOME"]?.trim(); + const cursorHome = + platform === "darwin" + ? path.join(userHome, "Library", "Application Support") + : platform === "win32" + ? hostEnvironment["APPDATA"] || path.join(userHome, "AppData", "Roaming") + : configHome && path.isAbsolute(configHome) + ? configHome + : path.join(userHome, ".config"); + const authPath = + platform === "darwin" + ? path.join(userHome, ".cursor", "auth.json") + : path.join(cursorHome, platform === "win32" ? "Cursor" : "cursor", "auth.json"); + const credentialStore = hostEnvironment["AGENT_CLI_CREDENTIAL_STORE"]; + const loginUnavailable = + Boolean(hostEnvironment["CURSOR_AUTH_TOKEN"]?.trim()) || + Boolean(hostEnvironment["CURSOR_API_KEY"]?.trim()) || + credentialStore === "memory"; + const useKeychain = platform === "darwin" && credentialStore !== "file"; + if (useKeychain && !loginUnavailable && !keychainUsageEnabled) { + return { + cacheKey: "keychain-disabled", + scan: () => + Effect.succeed([ + { + dir: authPath, + volumeId: "", + files: null, + message: "Cursor account usage is off on this environment.", + action: "enableCursorKeychain", + }, + ]), + }; + } + if (loginUnavailable) { + return { + cacheKey: "login-unavailable", + scan: () => + Effect.succeed([ + { + dir: authPath, + files: null, + message: "Cursor account history needs a Cursor CLI login on this server.", + }, + ]), + }; + } + yield* ensureLoaded; + const sourceKey = useKeychain ? "keychain" : authPath; + const resolved = yield* accountReader.resolve(useKeychain ? { kind: "keychain" } : authPath); + const cacheKey = + resolved.kind === "token" ? encodeCredentialKey([sourceKey, resolved.fingerprint]) : ""; + // A slower credential resolution keeps its request's snapshot, but cannot + // replace the identity observed by a newer preparation. + if (generation === preparationGeneration) { + activeCredentialKey = cacheKey; + const prefix = encodeCredentialKey([sourceKey]).slice(0, -1) + ","; + for (const key of caches.keys()) { + if (key !== cacheKey && (key === sourceKey || key.startsWith(prefix))) { + caches.delete(key); + cacheDirty = true; + } } - if (loginUnavailable) { - return [ - { - dir: authPath, - files: null, - message: "Cursor account history needs a Cursor CLI login on this server.", - }, - ]; + for (const key of failures.keys()) { + if (key !== cacheKey && (key === sourceKey || key.startsWith(prefix))) failures.delete(key); } - const source = yield* accountSource( - useKeychain ? { kind: "keychain" } : authPath, - authPath, - windowStartMs, - retentionCutoffMs, - awaitRefresh, - ); - return source === null ? [] : [source]; - }, - ); + if (cacheDirty) yield* schedulePersist; + } + return { + cacheKey: resolved.kind === "token" ? cacheKey : encodeCredentialKey(resolved), + scan: ({ windowStartMs, retentionCutoffMs, awaitRefresh }: ScanInput) => + Effect.gen(function* () { + const source = yield* accountSource( + resolved, + cacheKey, + authPath, + windowStartMs, + retentionCutoffMs, + awaitRefresh, + ); + return source === null ? [] : [source]; + }), + }; + }); - return CursorUsageAccounts.of({ scan, awaitPersisted }); + const scan: CursorUsageAccounts["Service"]["scan"] = (input) => + prepare(input.keychainUsageEnabled).pipe(Effect.flatMap((snapshot) => snapshot.scan(input))); + return CursorUsageAccounts.of({ scan, prepare, awaitPersisted }); }); export const layer = Layer.effect(CursorUsageAccounts, make); diff --git a/packages/provider-cursor/src/server/accountUsage.test.ts b/packages/provider-cursor/src/server/accountUsage.test.ts index 3924576b43dc..fe2da115fa78 100644 --- a/packages/provider-cursor/src/server/accountUsage.test.ts +++ b/packages/provider-cursor/src/server/accountUsage.test.ts @@ -11,7 +11,7 @@ import * as HttpClientResponse from "effect/http/HttpClientResponse"; import * as Path from "effect/Path"; import * as CursorKeychain from "./CursorKeychain.ts"; -import { readCursorAccountUsage } from "./accountUsage.ts"; +import { readCursorAccountUsage, readCursorCredential } from "./accountUsage.ts"; /** A scoped temporary directory holding `auth.json` with `accessToken`. */ const writeAuth = (accessToken: string) => @@ -68,6 +68,46 @@ const readAccountUsage = ( ); describe("Cursor account history", () => { + it.effect.each(["file", "keychain"] as const)( + "uses the snapshotted %s login even if credentials change before the API read", + (source) => + Effect.gen(function* () { + const token = (subject: string) => + "header." + + Buffer.from(JSON.stringify({ sub: subject })).toString("base64url") + + ".signature"; + const first = token("auth|first"); + let currentToken = first; + const { authPath } = yield* writeAuth(first); + const keychainToken = Effect.sync(() => currentToken); + const resolved = yield* readCursorCredential( + source === "file" ? authPath : { kind: "keychain" }, + keychainToken, + ); + currentToken = token("auth|second"); + const fileSystem = yield* FileSystem.FileSystem; + yield* fileSystem.writeFileString(authPath, JSON.stringify({ accessToken: currentToken })); + let requests = 0; + const result = yield* readAccountUsage( + resolved, + 0, + 1781000000000, + ({ headers }) => { + requests++; + assert.strictEqual( + headers.get("cookie"), + "WorkosCursorSessionToken=" + encodeURIComponent("first::" + first), + ); + return Response.json({ totalUsageEventsCount: 0, usageEventsDisplay: [] }); + }, + keychainToken, + ); + assert.strictEqual(requests, 1); + assert.isNull(result.error); + assert.isFalse(result.missing); + }).pipe(Effect.provide(NodeServices.layer)), + ); + it.effect("reads Cursor account history with the default macOS Keychain login", () => Effect.gen(function* () { const accessToken = `header.${Buffer.from(JSON.stringify({ sub: "auth|demo" })).toString("base64url")}.signature`; diff --git a/packages/provider-cursor/src/server/accountUsage.ts b/packages/provider-cursor/src/server/accountUsage.ts index 11976a2e0f1d..45827b6a1416 100644 --- a/packages/provider-cursor/src/server/accountUsage.ts +++ b/packages/provider-cursor/src/server/accountUsage.ts @@ -102,27 +102,20 @@ const DASHBOARD_USAGE_URL = "https://cursor.com/api/dashboard/get-filtered-usage /** The session cookie must never follow a redirect off cursor.com. */ const NO_REDIRECT: RequestInit = { redirect: "error" }; -/** - * Dashboard usage includes headless agents and reports fresh input separately - * from cache reads. `keychainToken` is only run for a Keychain login. - */ -export const readCursorAccountUsage = Effect.fn("readCursorAccountUsage")(function* ( +export type CursorResolvedCredential = + | { readonly kind: "token"; readonly accessToken: string; readonly fingerprint: string } + | { readonly kind: "unavailable"; readonly missing: boolean; readonly error: string | null }; + +export const readCursorCredential = Effect.fn("readCursorCredential")(function* ( credentialSource: string | { readonly kind: "keychain" }, - sinceMs: number, - endDate: number, keychainToken: Effect.Effect< string | null, | CursorKeychain.CursorKeychainTimeoutError | CursorKeychain.CursorKeychainReadError | CursorAccountUsageInvalidError > = invalid("No Keychain reader"), -): Effect.fn.Return< - CursorAccountUsageReadResult, - never, - FileSystem.FileSystem | Crypto.Crypto | HttpClient.HttpClient -> { +): Effect.fn.Return { const fileSystem = yield* FileSystem.FileSystem; - const httpClient = yield* HttpClient.HttpClient; const credential = yield* Effect.result< unknown, | PlatformError.PlatformError @@ -146,8 +139,7 @@ export const readCursorAccountUsage = Effect.fn("readCursorAccountUsage")(functi PlatformError.isPlatformError(cause) && cause.reason._tag === "NotFound"; return { - accountKey: null, - records: [], + kind: "unavailable", missing, error: missing ? null @@ -161,8 +153,7 @@ export const readCursorAccountUsage = Effect.fn("readCursorAccountUsage")(functi const accessToken = credential.success; if (typeof accessToken !== "string" || !accessToken) { return { - accountKey: null, - records: [], + kind: "unavailable", missing: true, error: typeof credentialSource === "string" @@ -170,6 +161,39 @@ export const readCursorAccountUsage = Effect.fn("readCursorAccountUsage")(functi : "Cursor account history needs a macOS Keychain CLI login on this server.", }; } + const fingerprint = yield* Effect.result(accountHash(accessToken)); + if (Result.isFailure(fingerprint)) + return { kind: "unavailable", missing: false, error: "Cursor credentials could not be read." }; + return { kind: "token", accessToken, fingerprint: fingerprint.success }; +}); + +/** + * Dashboard usage includes headless agents and reports fresh input separately + * from cache reads. `keychainToken` is only run for a Keychain login. + */ +export const readCursorAccountUsage = Effect.fn("readCursorAccountUsage")(function* ( + credentialSource: string | { readonly kind: "keychain" } | CursorResolvedCredential, + sinceMs: number, + endDate: number, + keychainToken: Effect.Effect< + string | null, + | CursorKeychain.CursorKeychainTimeoutError + | CursorKeychain.CursorKeychainReadError + | CursorAccountUsageInvalidError + > = invalid("No Keychain reader"), +): Effect.fn.Return< + CursorAccountUsageReadResult, + never, + FileSystem.FileSystem | Crypto.Crypto | HttpClient.HttpClient +> { + const httpClient = yield* HttpClient.HttpClient; + const resolved = + typeof credentialSource === "string" || credentialSource.kind === "keychain" + ? yield* readCursorCredential(credentialSource, keychainToken) + : credentialSource; + if (resolved.kind === "unavailable") + return { accountKey: null, records: [], missing: resolved.missing, error: resolved.error }; + const accessToken = resolved.accessToken; let accountKey: string | null = null; const read = Effect.gen(function* () { const payload = Base64Url.decodeString(accessToken.split(".")[1] ?? ""); diff --git a/packages/provider-cursor/src/server/driver.ts b/packages/provider-cursor/src/server/driver.ts index 2c748bb70d5e..a9073907d180 100644 --- a/packages/provider-cursor/src/server/driver.ts +++ b/packages/provider-cursor/src/server/driver.ts @@ -79,6 +79,10 @@ export const CursorDriver: ProviderDriver< usage: { kind: "scan", provider: "cursor", + prepare: ({ settings }) => + CursorUsageAccounts.CursorUsageAccounts.pipe( + Effect.flatMap((accounts) => accounts.prepare(settings.cursorKeychainUsageEnabled)), + ), scan: ({ settings, windowStartMs, retentionCutoffMs, awaitRefresh }) => CursorUsageAccounts.CursorUsageAccounts.pipe( Effect.flatMap((accounts) =>