Skip to content

fix: key Cursor conversations on x-opencode-session-id - #33

Closed
zcl0621 wants to merge 1 commit into
oakimov:mainfrom
zcl0621:fix/child-session-key
Closed

zcl0621 wants to merge 1 commit into
oakimov:mainfrom
zcl0621:fix/child-session-key

Conversation

@zcl0621

@zcl0621 zcl0621 commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Summary

OpenCode 2.x sets x-session-id, x-session-affinity and x-opencode-session to the parent session (or the fork source) on every child-session request, so that subagents share prompt-cache affinity with their parent. Only x-opencode-session-id identifies the session that is actually making the request. The relevant part of OpenCode 2.0.21's request builder:

"x-opencode-session-id": A.id,
"x-session-affinity": M, "X-Session-Id": M, "x-opencode-session": M,
// M = A.parentID ?? A.fork?.sessionID ?? A.id

opencodeSessionKey read x-session-id first, so every subagent Run running on a Cursor model was bound to its parent's Cursor conversation. Two things went wrong as a result:

  • The subagent superseded the parent's held Run (sessionManager.close: reason=superseded-by-new-run).
  • The subagent's turns were written into the parent's checkpoint, so the parent then resumed a conversation that no longer matched its own history. Users saw this as code=internal after spawning subagents.

It happens for every way of starting a subagent: built-in general, custom subagents, and a Cursor-native Task mapped to subagent. It happens whether or not the subagent uses the same model as the parent.

The fix is to read x-opencode-session-id first. The existing headers stay as fallbacks, so hosts that don't send it behave as before.

Test plan

  • New unit test: given OpenCode 2.x child-session headers, the child is keyed on its own session id and not the parent's.
  • bun test: 1215 pass, 0 fail. tsc --noEmit is clean.
  • Reproduced on OpenCode 2.0.21. A parent on cursor/gpt-5-mini spawned general (same model), a custom subagent on cursor/gemini-3-flash, and one on DeepSeek.
    • Before: the child Run arrived with the parent's key, superseded the parent's Run, and forced a conversation reset.
    • After: each child has its own key, with no reset and no internal error.

Related to #32: that PR detects and repairs a conversation that a foreign model has diverged, and this PR removes one major source of that divergence.

🤖 Generated with Claude Code

OpenCode 2.x sends x-session-id, x-session-affinity and x-opencode-session
as the parent (or fork source) session for every child session, so
subagents share prompt-cache affinity with their parent. Only
x-opencode-session-id names the requesting session. Keying on the
affinity ids made every subagent Run bind to its parent's Cursor
conversation: it superseded the parent's held Run and left the parent
resuming a checkpoint that now held the subagent's turns.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 1, 2026 05:12

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The focused change correctly addresses the session collision and includes appropriate regression coverage.

Review effort: Balanced
Findings: None

What changed in this PR

Prioritizes the requesting OpenCode session ID so subagents no longer reuse their parent’s Cursor conversation.

Changes:

  • Uses x-opencode-session-id before affinity fallback headers.
  • Adds regression coverage for parent/child session separation.
File Description
src/​language-model.ts Corrects session-key header precedence.
test/​conversation-id.test.ts Tests subagent conversation isolation.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@zcl0621

zcl0621 commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

Soak test results

I ran a local soak test with OpenCode 2.0.21 (@opencode/cli) and an isolated HOME, keeping one opencode2 serve up for the whole run. To keep costs down I used cheap models only: cursor/gpt-5-mini, cursor/gemini-3-flash, cursor/gpt-5.4-nano and deepseek/deepseek-flash. Five MCP servers were configured: everything, memory, sequential-thinking, filesystem and remote context7.

Build under test: this PR combined with #32 (foreign-history reseed), plus an unpublished follow-up that reseeds when a resumed checkpoint fails after missing KV blobs, plus a debug trace for raw end-stream error frames. Without this fix, a child Run arrives with the parent's key, supersedes the parent's held Run and forces a reset. I reproduced that in the same setup (see the PR description).

Every round ran opencode2 run -s <session> -m <model> "<prompt>" against the long-lived server. Each prompt forced at least one Cursor subagent, plus host tool calls by the parent.

Phase Sessions Runs What varied
1 2 in parallel 30 Parent model rotates across 3 Cursor models and DeepSeek; general, scout-cursor (Cursor) and scout-deepseek subagents
2 2 in parallel 80 Same as phase 1, plus a different file each round and a random 0–150 s idle between rounds to cross the 120 s semantic-idle lease. 156 subagent sessions in total
3 2 running concurrently 40 Parent alternates Cursor ↔ DeepSeek every round. Subagent alternates Cursor ↔ DeepSeek on a different beat, so all four parent/child combinations occur (94 subagent sessions: 49 Cursor, 45 DeepSeek). Between rounds: MCP disconnect and reconnect through /api/experimental/mcp/:server/{disconnect,connect}, skill add, modify and remove under .opencode/skills, and an opencode serve restart followed by continuing the same sessions. Each change happened 4 times

Results

  • 150 / 150 runs exited 0. No Cursor API error appeared in any run's output.

  • Counts from the provider debug log:

    Event Phase 1 Phase 2* Phase 3
    code=internal 0 0 0
    Run end-stream error frame 0 0 0
    replay suppressed 0 0 0
    real blob miss (found=false echoed=false) 0 0 0
    checkpoint unusable reseed 0 0 0
    conversation reset on a child session key 0 0 0
    conversation reset on a parent 18 (model-switch) 21 (model-switch) 10 (model-switch)

    *The debug log truncates itself at its 10 MiB cap, so the phase 2 counts cover only its last ~27 minutes (34 of the 80 runs). The exit codes and the absence of API errors cover all 80 runs.

  • Phase 2 checked session → conversation isolation: 37 OpenCode sessions mapped to 47 Cursor conversation ids, and no conversation id was shared by two sessions. Before this fix, every Cursor subagent bound to its parent's conversation.

  • Phase 3 confirmed the injected changes reached the model. After a skill was added the model reported its code (CODE-n-A), after the skill was modified it reported the new code (CODE-n-B), and after removal it reported none. A disconnected MCP server's tools were never called while it was down.

  • Every parent reset was the intended foreign-history:model-switch from fix: reseed Cursor after another model answered in the session #32, firing when the parent's Cursor model changed.

Observations (none of them caused a failure)

  • found=false echoed=true appears thousands of times per phase. Each one is an inline blob (the blob id is the content itself, starting {"role":) that is echoed back, which is expected. I'm mentioning it because a naive found=false grep makes it look like blob loss.
  • After a mid-session opencode serve restart, the first Run logged hydration skipped … status=missing. That is because the parent's snapshot had already been removed by a model-switch reset, and the parent's later turns ran on isolated helper conversations (fresh turn: isolating in-session helper, 15 times in phase 3), which are not persisted. Continuing after the restart worked in all 4 restarts × 2 sessions.
  • In OpenCode 2.x, isolating in-session helper also fires for the parent's own fresh turn while its previous Run still has a pending exec. That heuristic was written for helpers reusing the parent's session id. With this PR children no longer do that, so it may be worth revisiting separately. Out of scope here.

@zcl0621

zcl0621 commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

Closing in favor of a single consolidated PR. It combines this change with the related fixes found during soak testing on OpenCode 2.x (subagent session key, foreign-history reseed, system-notice continuations, missing-blob reseed) and includes the full test results. I'll link it here once it's open.

@zcl0621

zcl0621 commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

Consolidated PR: #34

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants