fix(opencode): resume the OpenCode session on follow-ups instead of starting an empty one#3617
fix(opencode): resume the OpenCode session on follow-ups instead of starting an empty one#3617vdmkotai wants to merge 8 commits into
Conversation
…tarting an empty one The OpenCode adapter always called session.create and never read or emitted a resume cursor, so the upstream ses_… id lived only in memory. When that in-memory binding was lost — the ProviderSessionReaper stopping an idle session (~30 min) or an app/server restart — the next follow-up in the same visible thread was sent to a brand-new, empty OpenCode session. t3code kept rendering its own projection DB, so the user still saw the full history while the model had no context (issue pingdotgg#3604). Mirror the Grok/Cursor/Codex resume pattern, entirely within the adapter: - Emit resumeCursor { schemaVersion, sessionId } on the started ProviderSession (and echo it from sendTurn) so ProviderService persists it into provider_session_runtime.resume_cursor_json. - On startSession, when a cursor is present, validate the id with session.get and re-adopt that session instead of creating a new one. OpenCode scopes history by session id, so prompting the same id restores the full prior conversation. A missing/closed session (or any get failure) falls back to a fresh session so a stale cursor can't wedge the thread. - Only abort the upstream session in the start race-cleanup when we actually created it; never abort a session we merely resumed. The persistence/recovery plumbing is provider-agnostic and already feeds a persisted cursor back into startSession on a reaped/restarted follow-up (ProviderService falls back to the stored binding cursor when the reactor passes none), so no changes outside the adapter are needed. Adds regression tests: fresh-session cursor emission, resume re-adopting the persisted id (no create), follow-up turns targeting the resumed id, stale-cursor fallback to create, and malformed-cursor rejection. Fixes pingdotgg#3604
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
ApprovabilityVerdict: Needs human review This PR fundamentally changes OpenCode session lifecycle behavior - adding session resumption, directory-change forking, and sophisticated error detection. While well-tested and addressing a real bug, the scope of changes to core session management warrants human review. You can customize Macroscope's approvability policy. Learn more. |
…ass) Addresses review feedback on pingdotgg#3617 (macroscope + Cursor Bugbot + a deep multi-agent review) without widening scope beyond the adapter: - Re-apply permissions on resume. `session.create` is the only place the runtimeMode permission ruleset is set, so re-adopting a session skipped it; the reactor restarts with the persisted cursor on a runtime-mode change, which would leave a resumed OpenCode session on stale (e.g. full-access) permissions. Resume now calls session.update with buildOpenCodePermissionRules(runtimeMode). - Don't resume into the wrong directory. OpenCode routes a prompt to the session's own stored directory, so reusing a session created under a different cwd would silently run there. Resume now starts a fresh session when the re-adopted session's directory differs from the requested cwd. - Distinguish "not found" from transient failures. The SDK client uses throwOnError:true, so session.get rejects on any non-2xx. Only a confirmed 404 / NotFoundError now falls back to creating a fresh session; transport/auth/server errors propagate instead of silently resetting a live thread to an empty session. Tests model throwOnError:true (session.get rejects) and add coverage for the permission re-application, cwd-mismatch fallback, and transient-error propagation paths.
|
Thanks for the reviews — addressed in 7be6629. Three hardening changes, all kept within the adapter:
Tests updated to model |
Address review (Cursor Bugbot, high): isOpenCodeNotFound only walked the `cause` chain checking a numeric `status`, so it relied on the 404 sitting at one specific nesting and ignored `response.status` and the OpenCodeRuntimeError `detail` string. Reworked it into a bounded BFS that also checks `statusCode`, nested `response.status`, the NotFoundError `name`/`body`, and `message`/`detail` text, descending cause/body/error/data. Export it and add direct unit tests across every shape (incl. the real wrapped-Error production shape and a response.status-only 404), plus the transient/auth/network cases that must still propagate.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit b984684. Configure here.
…e text Address review (Cursor Bugbot): isOpenCodeNotFound matched the free-text message/detail, so a non-404 error whose text merely contains 'not found' (a 500 saying 'upstream X not found', an auth error, or a serialized body from openCodeRuntimeErrorDetail) was misclassified as a missing session and silently started a fresh one. Decide only on structured signals — a numeric 404 (status/statusCode/nested response.status) or an explicit NotFoundError name — which already cover the real throwOnError:true production shape (cause.status=404 + body.name). Update unit tests to assert free-text-only inputs (incl. a 500 whose message contains 'not found') now propagate.
|
UPDATE: |
…ontext The directory-mismatch guard added while hardening this PR started a fresh, EMPTY session whenever the resumed session's stored directory differed from the requested cwd. Its stated rationale -- "OpenCode routes a prompt to the session's own stored directory, so resuming under a different cwd would run in the wrong tree" -- does not hold: OpenCode resolves tool execution, snapshots and file ops from the per-request directory param (instance context), not from session.info.directory. So the guard solved a non-problem and introduced a real one: any time a thread's cwd changes (most commonly when it moves from the project root into a git worktree between turns) the whole conversation was stranded in the old session and the follow-up landed in an empty one -- the exact pingdotgg#3604 symptom this PR set out to fix. Fix: when the persisted session exists but was created under a different directory, fork it INTO the requested directory (client.session.fork({ sessionID, directory })) instead of creating an empty session. OpenCode clones the full message history into a new session bound to the requested worktree, so the follow-up keeps its context and tools still run on the correct tree. The fork id becomes the durable resume cursor. A genuinely missing (404) session still starts fresh, unchanged. Verified against the OpenCode source that fork copies all messages/parts with fresh ids and stamps the new session's directory/path from the request context. Test: the former "starts fresh on directory mismatch" case now asserts the session is forked with history (fork called, no session.create, cursor -> fork id) via a new fork mock; the not-found path still starts fresh.
|
Pushed a follow-up correcting the directory-mismatch handling that was added during hardening. Problem: the guard that started a fresh session when the resumed session's stored directory differed from the requested cwd was justified by "OpenCode routes a prompt to the session's own stored directory." That premise doesn't hold — OpenCode resolves tool execution, snapshots and file ops from the per-request Fix: when the persisted session exists but was created under a different directory, Verified against the OpenCode source that fork copies all messages/parts with fresh ids and stamps the new session's directory/path from the request context. The former "starts fresh on directory mismatch" test now asserts fork-with-history. |

Fixes #3604.
What
The OpenCode adapter never read or emitted a resume cursor, so the upstream
ses_…id lived only in process memory. When that in-memory binding was lost —ProviderSessionReaperstopping an idle session (~30 min), or an app/server restart — the next follow-up in the same visible thread was sent to a brand-new, empty OpenCode session. t3code kept rendering its own projection DB, so the user still saw the full history while the model had no context.This makes the OpenCode adapter resumable, mirroring the existing Grok/Cursor/Codex pattern, entirely within
apps/server/src/provider/Layers/OpenCodeAdapter.ts:startSessionnow emitsresumeCursor: { schemaVersion, sessionId }on the returnedProviderSession(andsendTurnechoes it), soProviderServicepersists it intoprovider_session_runtime.resume_cursor_json.startSessionvalidates the id withsession.getand re-adopts that session instead of callingsession.create. OpenCode scopes history by session id, so prompting the same id restores the full prior conversation.session.getfailure) falls back to a fresh session, so a stale cursor can never wedge the thread.Why
This is the documented root cause in #3604 (with DB-level evidence of two OpenCode sessions per visible thread). The persistence/recovery plumbing is already provider-agnostic:
ProviderService.startSessionfalls back to the stored binding cursor when the reactor passes none, so no changes are needed outside the adapter — it just needed the adapter to start producing and consuming a cursor like every other provider already does.Scope
Intentionally small and focused — 2 files, no contract / persistence / orchestration changes:
apps/server/src/provider/Layers/OpenCodeAdapter.ts(+118/-20)apps/server/src/provider/Layers/OpenCodeAdapter.test.ts(+147) — regression tests for: fresh-session cursor emission, resume re-adopting the persisted id (nocreate), follow-up turns targeting the resumed id, stale-cursor fallback tocreate, and malformed/foreign-cursor rejection.Validation
tsgo --noEmitclean;OpenCodeAdapter.test.ts(21 tests) plus the fullsrc/provider+src/orchestrationsuites (531 tests) pass.Note
Medium Risk
Changes provider session lifecycle and error handling for OpenCode threads; misclassified errors could still lose context, but the PR explicitly hardens against silent fresh sessions on non-404 failures.
Overview
Fixes #3604 by making the OpenCode adapter produce and consume a versioned
resumeCursor(schemaVersion+sessionId) so follow-ups reuse the same upstreamses_…conversation after idle reaper or restart, instead of silently starting an empty session.startSessionnow parses the persisted cursor, probes withsession.get, and either re-adopts the session (withsession.updateto re-apply permissions for the currentruntimeMode), forks into a new working directory when cwd changed (preserving history), or creates fresh only on a confirmed not-found. Transient/auth/server errors propagate viaisOpenCodeNotFound(structured 404/NotFoundErroronly—no message substring matching).sendTurnechoes the cursor; concurrent-start cleanup aborts only when this call created a session, not when resuming.Tests extend the OpenCode runtime mock (
get/update/fork) and add regression coverage for resume, stale cursor, bad cursor, transient failures, cwd fork, and not-found classification.Reviewed by Cursor Bugbot for commit 0d6eb88. Bugbot is set up for automated code reviews on this repo. Configure here.
Note
Resume existing OpenCode sessions on follow-up turns instead of creating new ones
startSessionin OpenCodeAdapter.ts now parses a versionedresumeCursorfrom input and probes the OpenCode server viasession.getto check if the session still exists.session.updateinstead of callingsession.create.session.forkto preserve conversation history while binding to the new directory.sendTurnnow includes the activeresumeCursorin its result so callers can persist it across turns.Macroscope summarized 0d6eb88.