From 0728e1e82e33e1b4e898336e0df4435d31bdf362 Mon Sep 17 00:00:00 2001 From: eyeveil <64274427+eyeveil@users.noreply.github.com> Date: Fri, 9 Oct 2026 20:45:28 +0200 Subject: [PATCH 1/3] fix(server): preserve relay connections through failed updates --- .../src/cloud/ManagedEndpointRuntime.test.ts | 116 ++++++++++++++++++ .../src/cloud/ManagedEndpointRuntime.ts | 35 ++++-- 2 files changed, 142 insertions(+), 9 deletions(-) diff --git a/apps/server/src/cloud/ManagedEndpointRuntime.test.ts b/apps/server/src/cloud/ManagedEndpointRuntime.test.ts index 19ada4264683..38c591db6664 100644 --- a/apps/server/src/cloud/ManagedEndpointRuntime.test.ts +++ b/apps/server/src/cloud/ManagedEndpointRuntime.test.ts @@ -934,6 +934,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 a774c7755083..ec48e5b56c77 100644 --- a/apps/server/src/cloud/ManagedEndpointRuntime.ts +++ b/apps/server/src/cloud/ManagedEndpointRuntime.ts @@ -163,6 +163,7 @@ export const make = Effect.gen(function* () { const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; const relayClient = yield* RelayClient.RelayClient; 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); @@ -181,6 +182,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); @@ -295,6 +301,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) { @@ -306,21 +313,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), - ); - } }), ), ), @@ -461,12 +461,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)); @@ -568,6 +571,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), + ); + } return { status: "running", providerKind: "cloudflare_tunnel", From e0524afd2202867567efb01f14c47e16d7766da9 Mon Sep 17 00:00:00 2001 From: eyeveil <64274427+eyeveil@users.noreply.github.com> Date: Fri, 9 Oct 2026 20:45:29 +0200 Subject: [PATCH 2/3] fix(client): preserve conversation order on history retries --- .../src/state/threadHistoryController.test.ts | 183 +++++++++++++++++- .../src/state/threadHistoryMerge.test.ts | 92 +++++++++ .../src/state/threadHistoryMerge.ts | 34 +++- 3 files changed, 302 insertions(+), 7 deletions(-) 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]) From 728a8fe0272106b3f4cc221cb880282cde2eb948 Mon Sep 17 00:00:00 2001 From: eyeveil <64274427+eyeveil@users.noreply.github.com> Date: Fri, 9 Oct 2026 20:45:30 +0200 Subject: [PATCH 3/3] fix(server): invalidate Cursor usage on account changes --- apps/server/src/usage/UsageService.test.ts | 165 ++++++++++++++++++++- apps/server/src/usage/UsageService.ts | 157 +++++++++++++------- apps/server/src/usage/cursorUsageReader.ts | 45 ++++-- 3 files changed, 304 insertions(+), 63 deletions(-) diff --git a/apps/server/src/usage/UsageService.test.ts b/apps/server/src/usage/UsageService.test.ts index 52e98f0eaf7d..2308433682f1 100644 --- a/apps/server/src/usage/UsageService.test.ts +++ b/apps/server/src/usage/UsageService.test.ts @@ -33,6 +33,11 @@ import { HttpClient, HttpClientResponse } from "effect/http"; import * as ServerConfig from "../config.ts"; import * as ServerSettings from "../serverSettings.ts"; import * as CursorUsageReader from "./cursorUsageReader.ts"; +import { + decodeCursorAccountCaches, + encodeCursorAccountCaches, + CURSOR_ACCOUNT_CACHE_FILE_NAME, +} from "./cursorAccountCache.ts"; import type { UsageRecord } from "./usageTranscripts.ts"; import * as UsageService from "./UsageService.ts"; @@ -221,11 +226,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[] }) { @@ -502,6 +507,162 @@ 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* UsageService.make.pipe( + Effect.provideService(CursorUsageReader.CursorAccountReader, { + read: (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("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* UsageService.make.pipe( + Effect.provideService(CursorUsageReader.CursorAccountReader, { read: 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 = UsageService.make.pipe( + Effect.provideService(CursorUsageReader.CursorAccountReader, { read: 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("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 76bdf21f377a..14019d890997 100644 --- a/apps/server/src/usage/UsageService.ts +++ b/apps/server/src/usage/UsageService.ts @@ -223,11 +223,44 @@ export const make = Effect.gen(function* () { const antigravityCache = makeAntigravityUsageCache(); const sourceCache = new Map(); let cacheDirty = false; - /** Cursor account caches by credential source. */ + const cursorUserHome = + (platform === "win32" ? hostEnvironment["USERPROFILE"] : hostEnvironment["HOME"]) || + NodeOS.homedir(); + const configHome = hostEnvironment["XDG_CONFIG_HOME"]?.trim(); + const cursorHome = + platform === "darwin" + ? path.join(cursorUserHome, "Library", "Application Support") + : platform === "win32" + ? hostEnvironment["APPDATA"] || path.join(cursorUserHome, "AppData", "Roaming") + : configHome && path.isAbsolute(configHome) + ? configHome + : path.join(cursorUserHome, ".config"); + const cursorAuthPath = + platform === "darwin" + ? path.join(cursorUserHome, ".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"; + const cursorCredential: CursorCredentialSource = useKeychain + ? { kind: "keychain" } + : cursorAuthPath; + const cursorSourceKey = typeof cursorCredential === "string" ? cursorCredential : "keychain"; + type ResolvedCursorCredential = Awaited< + ReturnType + >; + const cursorCredentialKey = (resolved: ResolvedCursorCredential) => + resolved.kind === "token" ? encodeUsageRecordKey([cursorSourceKey, resolved.fingerprint]) : ""; + + /** Cursor account caches by credential source and token fingerprint. */ const cursorCaches = new Map(); + let activeCursorCredentialKey: string | undefined; let cursorCacheDirty = 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. */ @@ -235,7 +268,7 @@ export const make = Effect.gen(function* () { string, { readonly atMs: number; readonly error: string | null } >(); - /** The refresh in flight per credential source, which every read joins. */ + /** The refresh in flight per credential, which every read joins. */ const cursorRefreshes = new Map>(); const isWithinDirectory = (filePath: string, dir: string) => { const relative = path.relative(dir, filePath); @@ -658,7 +691,7 @@ export const make = Effect.gen(function* () { /** Fetches what one account cache is missing, then persists it if anything changed. */ const refreshCursorAccount = Effect.fn("UsageService.refreshCursorAccount")(function* ( - credential: CursorCredentialSource, + credential: CursorUsageReader.CursorResolvedCredential, credentialKey: string, retentionStartMs: number, ) { @@ -671,6 +704,7 @@ export const make = Effect.gen(function* () { }; let base = cursorCaches.get(credentialKey); let fetched = yield* fetchMissing(base); + if (activeCursorCredentialKey !== credentialKey) return; // Another login's history replaces the cached account's, even if reading it fails. if ( base !== undefined && @@ -681,6 +715,7 @@ export const make = Effect.gen(function* () { cursorCacheDirty = true; base = undefined; fetched = yield* fetchMissing(base); + if (activeCursorCredentialKey !== credentialKey) return; } const { range, result } = fetched; if (result.missing || result.error !== null || result.accountKey === null) { @@ -711,7 +746,7 @@ export const make = Effect.gen(function* () { /** Joins the refresh in flight, or starts one. */ const startCursorRefresh = ( - credential: CursorCredentialSource, + credential: CursorUsageReader.CursorResolvedCredential, credentialKey: string, retentionStartMs: number, ) => @@ -727,9 +762,11 @@ export const make = Effect.gen(function* () { yield* refreshCursorAccount(credential, credentialKey, retentionStartMs).pipe( Effect.catchCause(() => Clock.currentTimeMillis.pipe( - Effect.map((atMs) => - cursorFailures.set(credentialKey, { atMs, error: CURSOR_ACCOUNT_READ_ERROR }), - ), + Effect.map((atMs) => { + if (activeCursorCredentialKey === credentialKey) { + cursorFailures.set(credentialKey, { atMs, error: CURSOR_ACCOUNT_READ_ERROR }); + } + }), ), ), Effect.ensuring( @@ -744,26 +781,54 @@ export const make = Effect.gen(function* () { }), ); + const prepareCursorCredential = Effect.fnUntraced(function* (settings: ServerSettingsValue) { + if (loginUnavailable || (useKeychain && !settings.cursorKeychainUsageEnabled)) return null; + yield* ensureScanCacheLoaded; + const sourceKey = cursorSourceKey; + const resolved = yield* Effect.promise(() => + CursorUsageReader.readCursorCredential(cursorCredential), + ); + const credentialKey = cursorCredentialKey(resolved); + activeCursorCredentialKey = credentialKey; + const sourcePrefix = encodeUsageRecordKey([sourceKey]).slice(0, -1) + ","; + // Source-only keys from older servers cannot prove which login fetched them. + for (const key of cursorCaches.keys()) { + if (key !== credentialKey && (key === sourceKey || key.startsWith(sourcePrefix))) { + cursorCaches.delete(key); + cursorCacheDirty = true; + } + } + for (const key of cursorFailures.keys()) { + if (key !== credentialKey && (key === sourceKey || key.startsWith(sourcePrefix))) { + cursorFailures.delete(key); + } + } + return resolved; + }); + /** * The Cursor account source. A cache inside its TTL, or a refresh that failed * inside it, answers directly. Otherwise a refresh starts: `awaitRefresh` * waits for it, and anything else answers from the cache marked `refreshing`. */ const cursorAccountSource = Effect.fn("UsageService.cursorAccountSource")(function* ( - credential: CursorCredentialSource, + resolved: ResolvedCursorCredential, 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; + const credentialKey = cursorCredentialKey(resolved); + if (resolved.kind === "unavailable") { + if (resolved.missing && resolved.error === null) return null; + return { + provider: "cursor", + dir: authPath, + volumeId: yield* Effect.promise(() => readDirectoryVolumeId(authPath)), + files: null, + message: resolved.error ?? CURSOR_ACCOUNT_READ_ERROR, + } satisfies ScannedDir; } - const credentialKey = typeof credential === "string" ? credential : "keychain"; const nowMs = yield* Clock.currentTimeMillis; const recentFailure = cursorFailures.get(credentialKey); let refreshing = false; @@ -771,7 +836,7 @@ export const make = Effect.gen(function* () { (recentFailure === undefined || nowMs - recentFailure.atMs >= CURSOR_ACCOUNT_TTL_MS) && !isCursorCacheFresh(cursorCaches.get(credentialKey), nowMs) ) { - const refresh = yield* startCursorRefresh(credential, credentialKey, retentionStartMs); + const refresh = yield* startCursorRefresh(resolved, credentialKey, retentionStartMs); if (awaitRefresh) yield* Deferred.await(refresh); else refreshing = true; } @@ -817,6 +882,7 @@ export const make = Effect.gen(function* () { settings: ServerSettingsValue, retentionCutoffMs: number, awaitRefresh: boolean, + cursorLogin: ResolvedCursorCredential | null, ) { // The home resolvers ask for `Path` themselves; satisfy them from the // instance we already hold so the scan stays context-free. @@ -930,27 +996,6 @@ export const make = Effect.gen(function* () { }); const cursor = Effect.gen(function* () { - const cursorUserHome = - (platform === "win32" ? hostEnvironment["USERPROFILE"] : hostEnvironment["HOME"]) || home; - const configHome = hostEnvironment["XDG_CONFIG_HOME"]?.trim(); - const cursorHome = - platform === "darwin" - ? path.join(cursorUserHome, "Library", "Application Support") - : platform === "win32" - ? hostEnvironment["APPDATA"] || path.join(cursorUserHome, "AppData", "Roaming") - : configHome && path.isAbsolute(configHome) - ? configHome - : path.join(cursorUserHome, ".config"); - const cursorAuthPath = - platform === "darwin" - ? path.join(cursorUserHome, ".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 && !settings.cursorKeychainUsageEnabled) { return [ { @@ -974,8 +1019,9 @@ export const make = Effect.gen(function* () { } satisfies ScannedDir, ]; } + if (cursorLogin === null) return []; const source = yield* cursorAccountSource( - useKeychain ? { kind: "keychain" } : cursorAuthPath, + cursorLogin, cursorAuthPath, windowStartMs, retentionCutoffMs, @@ -1008,14 +1054,8 @@ export const make = Effect.gen(function* () { const scanSummary = Effect.fn("UsageService.scanSummary")(function* ( input: UsageSummaryInput, settings: ServerSettingsValue, + cursorLogin: ResolvedCursorCredential | null, ) { - 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 = @@ -1062,7 +1102,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, + cursorLogin, + ), ], { concurrency: 2 }, ); @@ -1187,7 +1233,7 @@ export const make = Effect.gen(function* () { const inflightScans = new Map>(); const scanKey = (input: UsageSummaryInput, settings: ServerSettingsValue): string => - JSON.stringify([ + encodeUsageRecordKey([ input.timeZone, input.sinceDay, input.untilDay, @@ -1202,8 +1248,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 cursorLogin = yield* prepareCursorCredential(settings); + const key = encodeUsageRecordKey([ + scanKey(input, settings), + cursorLogin === null ? null : cursorCredentialKey(cursorLogin), + ]); const deferred = yield* Effect.uninterruptible( Effect.gen(function* () { const existing = inflightScans.get(key); @@ -1217,7 +1274,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, cursorLogin).pipe( Effect.onExit((exit) => Effect.sync(() => inflightScans.delete(key)).pipe( Effect.andThen(Exit.isSuccess(exit) ? schedulePersist : Effect.void), diff --git a/apps/server/src/usage/cursorUsageReader.ts b/apps/server/src/usage/cursorUsageReader.ts index 95b87f1984bb..48bd4f30110a 100644 --- a/apps/server/src/usage/cursorUsageReader.ts +++ b/apps/server/src/usage/cursorUsageReader.ts @@ -69,14 +69,20 @@ function boundaryOverlap(previous: readonly string[], current: readonly string[] return lengths.at(-1) ?? 0; } -/** Dashboard usage includes headless agents and reports fresh input separately from cache reads. */ -export async function readCursorAccountUsage( +export type CursorResolvedCredential = { + readonly kind: "token"; + readonly accessToken: string; + readonly fingerprint: string; +}; + +/** Captures the saved login so cache lookup and its refresh use the same credential. */ +export async function readCursorCredential( credentialSource: string | { readonly kind: "keychain" }, - sinceMs: number, - endDate: number, - request: (url: string, init: RequestInit) => Promise = globalThis.fetch, keychainToken: () => Promise = readMacCursorAccessToken, -): Promise { +): Promise< + | CursorResolvedCredential + | { readonly kind: "unavailable"; readonly missing: boolean; readonly error: string | null } +> { let accessToken: unknown; try { accessToken = @@ -86,8 +92,7 @@ export async function readCursorAccountUsage( } catch (cause) { const missing = typeof credentialSource === "string" && object(cause).code === "ENOENT"; return { - accountKey: null, - records: [], + kind: "unavailable", missing, error: missing ? null @@ -100,8 +105,7 @@ export async function readCursorAccountUsage( } if (typeof accessToken !== "string" || !accessToken) { return { - accountKey: null, - records: [], + kind: "unavailable", missing: true, error: typeof credentialSource === "string" @@ -109,6 +113,25 @@ export async function readCursorAccountUsage( : "Cursor account history needs a macOS Keychain CLI login on this server.", }; } + return { kind: "token", accessToken, fingerprint: accountHash(accessToken) }; +} + +/** Dashboard usage includes headless agents and reports fresh input separately from cache reads. */ +export async function readCursorAccountUsage( + credentialSource: string | { readonly kind: "keychain" } | CursorResolvedCredential, + sinceMs: number, + endDate: number, + request: (url: string, init: RequestInit) => Promise = globalThis.fetch, + keychainToken: () => Promise = readMacCursorAccessToken, +): Promise { + const credential = + typeof credentialSource !== "string" && credentialSource.kind === "token" + ? credentialSource + : await readCursorCredential(credentialSource, keychainToken); + if (credential.kind === "unavailable") { + return { accountKey: null, records: [], missing: credential.missing, error: credential.error }; + } + const { accessToken } = credential; let accountKey: string | null = null; const cancel = new AbortController(); try { @@ -299,7 +322,7 @@ export class CursorAccountReader extends Context.Service< CursorAccountReader, { readonly read: ( - credentialSource: string | { readonly kind: "keychain" }, + credentialSource: string | { readonly kind: "keychain" } | CursorResolvedCredential, sinceMs: number, untilMs: number, ) => Effect.Effect;