Skip to content

fix(native-chat): reopen an OpenCode chat in the folder OpenCode moved it to - #42590

Merged
AmoabaKelvin merged 1 commit into
mainfrom
opencode-moved-session-reopen
Nov 15, 2026
Merged

AmoabaKelvin merged 1 commit into
mainfrom
opencode-moved-session-reopen

Conversation

@AmoabaKelvin

@AmoabaKelvin AmoabaKelvin commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

ELI5

OpenCode can move a conversation into another folder on its own, for example into a worktree it just created. When Orca later reopened that chat, it asked OpenCode for the conversation in the chat's original folder. OpenCode said no, and Orca quietly started a brand-new conversation instead. Now Orca asks OpenCode where the conversation lives and reopens it there.

Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​55 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​55
Prod 8 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​112 $\color{#cf222e}{\Huge{\mathbf{−}}}$​28 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​84

What Changed

Before: an OpenCode 2.x chat whose conversation OpenCode had moved could not be reopened after a restart or reconnect. Orca showed "OpenCode couldn't reopen its earlier session, so this chat continues in a new one" and carried on in a new, empty OpenCode session. Session History still listed the old conversation, but its row opened the chat, and the chat now continued the new conversation. That is the "resume B opens A" report in #41652.

After: the chat reopens the same OpenCode conversation in the folder it moved to, with no warning row and no new session.

How it works:

  • OpenCode 2.x refuses session/load from the wrong folder with -32602 and data { sessionId, cwd } ("session X does not belong to cwd: Y").
    • The OpenCode dialect recognises exactly that refusal through a new sessionInOtherFolder hook, beside the existing sessionLost hook.
    • It matches on the structured data, not the message text.
  • On that refusal, acpSessionOtherFolder (in acp-session-reopen-failure.ts, next to acpReopenTakeover) pages ACP session/list with no cwd. That lists every folder's sessions. It finds the conversation's current folder.
  • The handshake then retries the load there once.
  • Any other refusal, a failed lookup, or a failed retry goes through the existing takeover unchanged.
  • The chat's own folder, process cwd and Orca's file and branch views still use the workspace. Only the ACP session is opened where OpenCode keeps it.
  • AcpAgentConnection.listSessions now takes { cwd?, cursor? }. Its one existing caller, the Antigravity host probe, still passes { cwd }.

Why

The takeover exists for a conversation the agent really lost. A moved conversation isn't lost: OpenCode still has it, just in another folder. Replacing it hid the user's history behind a new session and broke Session History's resume.

Alternatives considered:

  • Refusing with a "conversation moved" error: smaller, but it leaves the chat unopenable.
  • Pinning the moved folder as the chat's launch directory: built in an earlier version of this branch. Review showed that Orca's renderer ties branch controls, remote file addressing and image links to the workspace identity. A pin pointing at another folder made those disagree, so it was dropped. Looking the folder up on each reopen costs one refused load plus a session/list page for moved chats only, and changes nothing else.

Linked Issue

Related to #41652: this fixes the wrong-session resume.

That issue also reports the active session tab disappearing from mobile. This PR doesn't prove or fix that part. Settling it needs:

  • the host and phone versions;
  • the host's <profile>/logs/main.trace.ndjson from the incident window;
  • whether the chat showed the "couldn't reopen its earlier session" row;
  • select id, directory, time_updated from session_v2 from the reporter's opencode.db for the two sessions.

Visual Proof

The UI is unchanged. What changes is which conversation the chat continues. These are from a live run instead of screenshots: a worktree-built headless orca serve, real OpenCode 2.2.7 on a free model, an isolated HOME/XDG_* and a folder workspace. The chat was created in proj and replied. OpenCode's own session.move then moved the conversation to proj-moved, and it took effect on the chat's next turn. After that the serve was restarted and a message was sent again.

Before, on origin/main: a second OpenCode session appears in the old folder, and the chat's chain records restore-failed:

--- opencode sessions
ses_e231ac23cffegw7c0ntyCW1400|/tmp/omsh2/proj-moved|4
ses_e231a9f2cffeD1lWmKIUFAuKYM|/tmp/omsh2/proj|2
--- orca chat record
opencode2_0bb9c369…|ses_e231ac23…:created:- > ses_e231a9f2…:created:restore-failed

After, on this branch, with two restarts: one OpenCode session, and every reopen resumes it in proj-moved:

--- opencode sessions
ses_e22df3815ffeWDl9kYNTYlD8UJ|/tmp/omsh3/proj-moved|8
--- orca chat record
opencode2_777bb138…|ses_e22df381…:created:- > ses_e22df381…:resumed:- > ses_e22df381…:resumed:-

Testing

  • Live run as above: the before case on origin/main, and the after case with three starts on this branch.

  • I also checked OpenCode 2.2.7's refusal directly over ACP:

    • session/load from another folder → -32602 … does not belong to cwd;
    • from the parent folder → refused too;
    • from the session's own folder → loads;
    • after session.move → the old folder is refused and the new one loads.
  • One regression test, acp-structured-session-adapter-resume.test.ts, replays that captured refusal. It expects a second load in the listed folder and no session/new. It fails on main: one load, then a takeover.

  • pnpm check:changed passes. The focused ACP tests pass, and so do the 92 test files test-dependents lists, except server-ingest-structured-status.test.ts, which fails the same way on main (unable to open database file).

  • Platform: macOS. Linux and Windows aren't affected differently: OpenCode normalises the folder comparison itself (FSUtil.resolve), and Orca only passes OpenCode's own reported path back.

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

  • Every added test fails without this change and avoids the test anti-patterns

Risk

Riskiest part: a reopened moved chat now runs its ACP session in a folder that differs from the workspace. OpenCode itself chose that folder; before this change the chat lost the conversation instead.

  • Composer @ mentions still resolve against the workspace, as they did on main while a moved conversation was live.
  • If the moved folder was deleted, OpenCode still lists it. The retry then either loads it (its tools fail there) or is refused. A refusal goes to the old takeover.

Narrow trigger: only OpenCode's exact -32602 { sessionId, cwd } refusal for the same session and folder. Agents that don't advertise sessionCapabilities.list keep today's takeover. OpenCode 1.x never sends this refusal.

Wire and versions: no new wire fields or saved data, so older phones and paired hosts aren't affected.

Antigravity: the probe's call changed shape only. It still sends { cwd: folder }.

Back out: a plain revert.

Level: Low

AI Disclosure

Review

Two review-loop rounds (Codex + Claude):

  • Round 1:
    • A failed session/list lookup skipped the takeover. Fixed: lookup and retry failures now go through the takeover policy.
    • Renderer link and composer folders didn't follow a pinned folder. Made moot when the pin was dropped.
  • Round 2:
    • Antigravity's probe would have sent its folder as a cursor. Fixed: listSessions({ cwd?, cursor? }).
    • Branch controls, remote file addressing and image context disagreeing with a pinned folder. Moot: the pin and its renderer changes were removed, so the renderer matches main.

This touches the native-chat reopen and ownership area from #30930 (@brennanb2025).

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Ensure no issues in: Security, Cross-platform support (Linux, Windows, Mac), Remote SSH, Mobile, general backwards compatibility, performance

  • Mobile and remote hosts: the handshake runs on the execution host, so local, paired and SSH hosts and both phone apps get the fix with no client changes.
  • Folder workspaces: covered. The live run used a folder workspace.
  • Performance: a moved chat pays one refused load plus session/list pages on each reopen. Other chats pay nothing.

Follow-ups, deliberately not in this PR:

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • I stated the risk: what could break, for whom, and how to back it out
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

…d it to

OpenCode 2.x moves a conversation with its session_move tool and then refuses session/load from the old folder. Look the session up with session/list and load it where it lives instead of replacing it with a new session.
@coderabbitai

coderabbitai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Walkthrough

The ACP dialect now identifies OpenCode errors that indicate a session belongs to another folder. The connection exposes paginated session listing, and the resume flow uses it to find the session’s folder and retry the reopen there. The added test covers a session moved from /workspace/project to /workspace/project-worktree.

Merge Risk: Low · up to f1768

Reopening an OpenCode 2.x chat from its moved folder should work as described. A misbehaving agent that repeats a pagination cursor could stall the reopen before it falls back to a new session. The regression test also does not check that the session lookup omits the folder. Both are small, localized fixes.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the change: an OpenCode chat is reopened in the folder OpenCode moved its conversation to. The changeset adds a sessionInOtherFolder hook, a session/list lookup, and a retry of session…
Description check Passed The description follows the repository template closely. It has ELI5, What Changed, Why, Linked Issue, Visual Proof, Testing, Risk, AI Disclosure, Review, Agent skill upstream boundary, Notes, and Che…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the change: an OpenCode chat is reopened in the folder OpenCode moved its conversation to. The changeset adds a sessionInOtherFolder hook, a session/list lookup, and a retry of session/load in the reported folder, so the title matches the main change. It follows the fix(scope) convention and is concise.

Full details: Description check

Explanation

The description follows the repository template closely. It has ELI5, What Changed, Why, Linked Issue, Visual Proof, Testing, Risk, AI Disclosure, Review, Agent skill upstream boundary, Notes, and Checklist sections. It explains the before and after behavior, the mechanism, the alternatives considered, and the rollback path. The Visual Proof section gives live-run output instead of screenshots, which is acceptable for a backend behavior change. The Linked Issue section cites #41652 as related, not fixed. The template asks for a linked issue for outside contributors, and the author does not say whether they are a maintainer. The AI Disclosure section is empty. The Testing checkboxes are ticked, but the Testing section lists a single platform (macOS) only.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Nitpick comments: 1

src/main/acp/session/acp-structured-session-adapter-resume.test.ts: 1

254-254: Trivial · Maintainability & Code Quality · Quick win

Assert that the session lookup omits cwd.

The handler returns the moved session regardless of frame.params. If lookup incorrectly sends cwd: '/workspace/project', this test can still pass. Assert that session/list was sent and that its parameters omit cwd. This protects the unscoped lookup required by the PR objective.

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 24079969-ae29-453a-be67-993ea07200f8
Commits

Reviewing files that changed from the base of the PR and between 285ddd1 and f176805.

Files selected for processing: 9

  • src/main/acp/acp-dialects/acp-dialect.ts
  • src/main/acp/acp-dialects/opencode-dialect.ts
  • src/main/acp/process/acp-agent-connection.ts
  • src/main/acp/process/acp-structured-connection.ts
  • src/main/acp/process/testing/acp-scripted-connection.test-fixture.ts
  • src/main/acp/session/acp-structured-session-adapter-resume.test.ts
  • src/main/acp/session/start/acp-session-reopen-failure.ts
  • src/main/acp/session/start/acp-structured-handshake.ts
  • src/main/antigravity/antigravity-host-acp.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review.

Comment on lines +79 to +80
cursor = page.nextCursor ?? undefined
} while (cursor)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Minor · Stability & Availability · Quick win

Stop paging when a session/list cursor repeats.

A page without the target session can repeat its nextCursor. The loop then sends the same session/list request again. It exits only when the agent answers differently, the start is abandoned or closed, or a call times out. Until then, the reopen does not reach takeover. This affects only an agent that returns a repeated cursor. Track the cursors already requested and throw when one repeats. The existing lookup-failure path then applies takeover.

Suggested fix

--- "a/src/main/acp/session/start/acp-session-reopen-failure.ts"
+++ "b/src/main/acp/session/start/acp-session-reopen-failure.ts"
@@ -66,18 +66,24 @@
   ) {
     return null
   }
   let cursor: string | undefined
+  const seen = new Set<string>()
   do {
     const page = sessionListPage.parse(
       await input.connection.listSessions(cursor ? { cursor } : {})
     )
     input.assertStarting()
     const listed = page.sessions.find((session) => session.sessionId === sessionId)
     if (listed) {
       return listed.cwd === cwd ? null : listed.cwd
     }
-    cursor = page.nextCursor ?? undefined
+    const next = page.nextCursor ?? undefined
+    if (next && seen.has(next)) {
+      throw new Error('session/list returned a repeated cursor')
+    }
+    if (next) seen.add(next)
+    cursor = next
   } while (cursor)
   return null
 }
 

@AmoabaKelvin
AmoabaKelvin merged commit 01eb509 into main Nov 15, 2026
47 checks passed
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.

1 participant