Skip to content
Source
Sign inSign up
pingdotgg/t3code

perf(server): Cursor item ordinals no longer copy a session-wide map per item #13866

Mergedjuliusmarmingemerged 1 commit intot3code/codex-turn-mapping fromrefs/pull/13866/headmerged by juliusmarminge View on GitHub

juliusmarmingecommented

The Cursor V2 adapter has the same item-ordinal problem as Claude (#13864). Every item ordinal of the session sat in one Map that was copied on each new item (new Map(current) inside Ref.update) and never pruned, as was the per-turn counter map beside it. A long session paid O(items) memory and O(items²) copying.

What changed

The ordinal map moves onto the turn (ActiveCursorTurn.itemOrdinals). It is mutated in place and goes away when the turn settles. The counter map is gone, because the map's size is the counter. OpenCode and Pi already allocate ordinals this way.

Why per-turn is safe

Every resolveItemOrdinal call passes the active turn. The keys are Cursor call ids, text and reasoning segment ids scoped to run.runId, plan:/todos: call ids, and terminal-failure:${providerTurnId}. A subagent keeps its turnItemOrdinal on its own record (existing?.turnItemOrdinal ?? resolveItemOrdinal(...)), and that record also lives on the turn. Before the fix I instrumented resolveItemOrdinal to flag any hit on an ordinal a different turn had allocated, then ran the full replay suites plus the Cursor adapter tests: 79 Cursor allocations, 0 cross-turn hits. The instrumentation was not committed.

Benchmark

A scratch, uncommitted script ran the resolver exactly as it was before and after, in 10 turns, resolving each item twice (first sight plus one later update):

N itemsbeforeafter
1,00060 ms1 ms
10,0007.6 s4 ms
50,000253 s37 ms

In #13864, the end-to-end benchmark through the real Claude adapter showed the same curve (50k items: 325.6 s before, 2.1 s after). A 50k-entry map retains about 1.8 MiB for the life of the session. After the fix, that memory is bounded by the largest single turn.

Verification

  • vp test run with CursorAdapterV2.test.ts, OrchestratorReplayFixtures.integration.test.ts (which includes the 11 Cursor replay fixtures) and OrchestratorReplayRecovery.integration.test.ts: 3 files, 110 tests pass. The replay fixtures are unchanged.
  • Before the fix, the cross-turn instrumentation run described above (14 files, 566 tests) found 0 Cursor hits.
  • vp exec tsc --noEmit -p . in apps/server: no errors.
  • vp lint on the touched file: only the existing layer unused-variable warning.
  • Not run: repo-wide checks, live Cursor.

Model: Claude Opus 5.5 (Claude Code)

🤖 Generated with Claude Code


Devin Review
    1. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted

    2. @@ -2242,3 +2230,4 @@
      22422230 providerTurnId,
      22432231 startedAt,
      22442232 completed,
      2233+ itemOrdinals: new Map(),

      This changes backend behavior: reusing a native item ID in a later turn now assigns a new turn-local ordinal. Could you add a focused adapter test covering repeated IDs across turns and stable ordinals within a turn? The existing two-turn test does not assert ordinals. This needs a test-file change, so no inline fix is applicable.

      Posted via Macroscope — Effect Service Conventions
    3. Thread transfer impact

      ✅ Thread transfer remains within every enforced ceiling.

      ℹ️ No successful main baseline artifact is available yet. This run establishes the initial measurement.

      ProviderMetricMain baselineThis PRImpactPR ceiling
      CodexTotal thread wire—4.9 KiB—6.8 KiB✅
      CodexThread snapshot wire—3.7 KiB—4.9 KiB✅
      CodexLive turn WebSocket wire—1.1 KiB—2.0 KiB✅
      CodexLive turn WebSocket decoded—20.4 KiB—29.3 KiB✅
      CodexLive turn messages—1—8✅
      ClaudeTotal thread wire—4.9 KiB—6.8 KiB✅
      ClaudeThread snapshot wire—3.7 KiB—4.9 KiB✅
      ClaudeLive turn WebSocket wire—1.2 KiB—2.0 KiB✅
      ClaudeLive turn WebSocket decoded—20.8 KiB—29.3 KiB✅
      ClaudeLive turn messages—2—8✅

      Baseline: unavailable · PR result: 6eee08e · Source CI: success

      Scenario and decoded snapshot size

      10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.

      • Codex decoded thread snapshot: 106.1 KiB
      • Claude decoded thread snapshot: 106.4 KiB

      Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.

    4. Approvability

      Verdict: Approved at 6eee08e

      Macroscope's review found this PR approvable — This is a focused server-side optimization that bounds Cursor ordinal state to the active turn while preserving stable ordinals within each turn. It changes later-turn reuse of native IDs to use new turn-local ordinals, but introduces no schema, configuration, security, billing, or deployment impact.

      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    5. juliusmarminge merged this2d359e3

    6. juliusmarminge closed this

Sign in to join this conversation.