From 6eee08e20a6ce52f666fc2b0372ea713964c1178 Mon Sep 17 00:00:00 2001 From: Julius Marminge <51714798+juliusmarminge@users.noreply.github.com> Date: Sat, 26 Sep 2026 13:17:39 -0700 Subject: [PATCH] perf(server): Cursor item ordinals no longer copy a session-wide map per item The Cursor V2 adapter kept every item ordinal of the session in one Map that it copied on each new item and never pruned, so a long session paid O(items) memory and O(items^2) copying. Every lookup comes from the turn that allocated the ordinal (a subagent keeps its ordinal on its own record), so the map now lives on the turn, is mutated in place, and goes away when the turn settles. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../Adapters/CursorAdapterV2.ts | 35 +++++++------------ 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/CursorAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CursorAdapterV2.ts index 20a6b5280b4c..69ef6d899c7a 100644 --- a/apps/server/src/orchestration-v2/Adapters/CursorAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CursorAdapterV2.ts @@ -858,6 +858,8 @@ interface ActiveCursorTurn { readonly providerTurnId: OrchestrationV2ProviderTurn["id"]; readonly startedAt: DateTime.Utc; readonly completed: Deferred.Deferred; + // Item ordinals allocated in this turn. No later turn looks items up here. + readonly itemOrdinals: Map; readonly tools: Map; readonly subagents: Map; readonly assistant: ActiveCursorTextStream; @@ -908,35 +910,21 @@ export function makeCursorAdapterV2( const events = yield* Queue.unbounded(); const liveAgent = yield* Ref.make(null); const activeTurn = yield* Ref.make(null); - const itemOrdinals = yield* Ref.make(new Map()); - const nextItemOrdinalsByTurn = yield* Ref.make(new Map()); const planIds = yield* Ref.make(new Map()); const emitProviderEvent = (event: ProviderAdapterV2Event) => Queue.offer(events, event).pipe(Effect.asVoid); - const resolveItemOrdinal = Effect.fnUntraced(function* ( - context: ActiveCursorTurn, - nativeItemId: string, - ) { - const existing = (yield* Ref.get(itemOrdinals)).get(nativeItemId); - if (existing !== undefined) { - return existing; - } - const nextWithinTurn = yield* Ref.modify(nextItemOrdinalsByTurn, (current) => { - const next = (current.get(context.run.runId) ?? 0) + 1; - const updated = new Map(current); - updated.set(context.run.runId, next); - return [next, updated]; - }); - const ordinal = context.input.providerTurnOrdinal * 100 + nextWithinTurn; - yield* Ref.update(itemOrdinals, (current) => { - const updated = new Map(current); - updated.set(nativeItemId, ordinal); - return updated; + const resolveItemOrdinal = (context: ActiveCursorTurn, nativeItemId: string) => + Effect.sync(() => { + const existing = context.itemOrdinals.get(nativeItemId); + if (existing !== undefined) { + return existing; + } + const ordinal = context.input.providerTurnOrdinal * 100 + context.itemOrdinals.size + 1; + context.itemOrdinals.set(nativeItemId, ordinal); + return ordinal; }); - return ordinal; - }); const resolvePlanId = Effect.fnUntraced(function* ( context: ActiveCursorTurn, @@ -2242,6 +2230,7 @@ export function makeCursorAdapterV2( providerTurnId, startedAt, completed, + itemOrdinals: new Map(), tools: new Map(), subagents: new Map(), assistant: {