Skip to content

fix: OpenCode sessions missing on Windows (#10332) + Pi Shift+Enter submits after tool (#9703) - #10511

Closed
itisvincent wants to merge 2 commits into
stablyai:mainfrom
itisvincent:fix/opencode-windows-path-and-pi-shift-enter
Closed

itisvincent wants to merge 2 commits into
stablyai:mainfrom
itisvincent:fix/opencode-windows-path-and-pi-shift-enter

Conversation

@itisvincent

@itisvincent itisvincent commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Two independent Windows bugs, one line of behavior change each.

1. OpenCode sessions missing from Agent Session History on Windows (#10332). OpenCode resolves its data directory through xdg-basedir on every platform, so on Windows its SQLite DB lands at ~/.local/share/opencode/opencode.db — not under %LOCALAPPDATA%. This is a known, still-open quirk on OpenCode's side (sst/opencode#8235 — "Config and Data directories follow the Linux XDG standard even on windows"). Orca's getOpenCodeDataDirectory() had a process.platform === 'win32' branch pointing at %LOCALAPPDATA%\opencode, which never exists. The readdir threw, the catch swallowed it, listOpenCodeDatabases() returned [], and the OpenCode SQLite scanner was never invoked — so every other agent showed up and only OpenCode was missing. Fix: drop the Windows branch, keeping the XDG_DATA_HOME override.

2. Pi Shift+Enter submits instead of inserting a newline after a tool call (#9703). TUI_AGENT_CONFIG['pi'] never set windowsShiftEnterEncoding, so on Windows Pi could only reach CSI-u via the live-KKP-flag path. Pi pushes the Kitty keyboard protocol once at startup and never re-pushes; when a tool spawns a subprocess that emits a reset sequence (RIS / DECSTR / alt-screen swap), the tracker's flags drop to 0, Orca falls back to Esc+CR, and Pi reads that as a plain Enter → submit. It self-heals on the next pane refocus, which is exactly the reported "works at start, breaks after a tool, fixed by clicking the pane" pattern. Fix: set windowsShiftEnterEncoding: 'csi-u' for pi, mirroring the existing Droid (#7668) and Grok (#8385) fixes, so the trusted route no longer depends on KKP-flag churn.

ELI5

Orca was looking for OpenCode's history file in the wrong folder on Windows, so the Agents panel always showed an empty list for OpenCode even though the history was there — we now look in the folder OpenCode actually uses. Separately, when chatting with the Pi agent on Windows, pressing Shift+Enter to add a new line would accidentally send the message instead, but only right after Pi used a tool. Orca now tells Pi's keypresses apart reliably, so Shift+Enter always just adds a new line.

Proof of fix

Fix A — OpenCode Windows path (#10332)

The regression test drives the real entry point (listOpenCodeDatabases) with process.platform stubbed to win32, a temp $HOME, %LOCALAPPDATA% populated, and a real opencode.db on disk at the XDG location.

With src/main/opencode-usage/scanner.ts checked out from origin/main:

 FAIL  src/main/opencode-usage/scanner.test.ts > listOpenCodeDatabases > finds opencode.db under ~/.local/share on Windows instead of %LOCALAPPDATA% (#10332)
AssertionError: expected [] to deeply equal [ Array(1) ]

- Expected
+ Received

- [
-   ".../orca-opencode-home-c70KOx/.local/share/opencode/opencode.db",
- ]
+ []

 ❯ src/main/opencode-usage/scanner.test.ts:76:43

 Test Files  1 failed (1)
      Tests  1 failed | 15 passed (16)

With the fix applied:

 Test Files  1 passed (1)
      Tests  16 passed (16)

Verified on a real Windows 11 box (win32 x64, Node v24.18.0), running both resolution strategies side by side against a seeded DB:

platform      : win32 | homedir: C:\Users\neil
LOCALAPPDATA  : C:\Users\neil\AppData\Local
MAIN  scans   : C:\Users\neil\AppData\Local\opencode -> dbs found: []
FIXED scans   : C:\Users\neil\.local\share\opencode -> dbs found: ["C:\\Users\\neil\\.local\\share\\opencode\\opencode.db"]

An independent directory listing on that same box confirms C:\Users\neil\.local\share\ is a real, populated location (it already holds claude and devex), while a recursive search for opencode.db under %LOCALAPPDATA% and %APPDATA% returns nothing.

Confirmed on a real Windows 11 box (win32 x64) — the premise is not inferred from the upstream issue, it is observed on disk:

EXISTS  C:\Users\neil\.local\share\opencode
missing C:\Users\neil\AppData\Local\opencode
missing C:\Users\neil\AppData\Roaming\opencode
XDG_DATA_HOME=(unset)

OpenCode's data directory is present at the XDG path and absent from both %LOCALAPPDATA% and %APPDATA% — exactly the directory main was reading, which is why readdir threw and the scanner silently returned []. (opencode.db itself is absent on that box because OpenCode has not been run there; the directory location is the claim under test.)

Fix B — Pi Shift+Enter (#9703)

With src/shared/tui-agent-config.ts checked out from origin/main:

 FAIL  src/renderer/src/components/terminal-pane/terminal-windows-shift-enter.test.ts > resolveWindowsShiftEnterEncoding > uses CSI-u for trusted Pi process evidence so Shift+Enter survives tool-subprocess churn (#9703)
AssertionError: expected 'alt-enter' to be 'csi-u' // Object.is equality

Expected: "csi-u"
Received: "alt-enter"

 ❯ src/renderer/src/components/terminal-pane/terminal-windows-shift-enter.test.ts:22:7

 Test Files  1 failed (1)
      Tests  1 failed | 8 passed (9)

With the fix applied:

 Test Files  1 passed (1)
      Tests  9 passed (9)

Proof of no regressions

$ npx vitest run --config config/vitest.config.ts src/main/opencode-usage/ src/main/ai-vault/
 Test Files  34 passed (34)
      Tests  224 passed (224)

$ npx vitest run --config config/vitest.config.ts \
    src/renderer/src/components/terminal-pane/terminal-shortcut-policy.test.ts \
    src/renderer/src/components/terminal-pane/keyboard-handlers.test.ts \
    src/renderer/src/components/terminal-pane/pty-connection.test.ts \
    src/renderer/src/components/terminal-pane/terminal-windows-shift-enter.test.ts
 Test Files  4 passed (4)
      Tests  542 passed (542)

$ npx tsc --noEmit -p config/tsconfig.node.json     # clean, exit 0
$ npx tsc --noEmit -p config/tsconfig.tc.web.json   # clean, exit 0
$ npx oxlint <the four changed files>               # clean, 0 warnings

No user-visible behavior changes beyond the two fixes. Justification:

  • Fix A only removes a process.platform === 'win32' branch. macOS and Linux never entered it, so their resolved path is byte-identical. The XDG_DATA_HOME and OPENCODE_DB overrides are unchanged and both are now covered by tests (the OPENCODE_DB case was added per review feedback). The only behavior that changes is on Windows, where the scanned directory moves from a path that never exists to the one that does.
  • Fix B adds one property to a single entry in TUI_AGENT_CONFIG. I diffed the file against origin/main: the only changed lines are inside the pi entry (a trailing comma, a comment, and the new key). Claude, Codex, OpenCode, Cursor, Gemini, Grok, Droid and every other agent config are byte-unchanged. Plain Enter is untouched — it is handled by a different branch of terminal-shortcut-policy.ts that never consults this config, and the Shift+Enter branch is the only reader. A new test pins that trusted agents without an override (gemini, cursor, codex) still resolve to alt-enter.

Compatibility

  • Platforms: Windows — both fixes verified. Fix A's path resolution was executed on a real Windows 11 box (output above); Fix B's resolver is unit-tested with the Windows code path exercised directly. macOS/Linux — Fix A provably unchanged (the removed branch was Windows-gated); Fix B is gated behind isWindowsTerminalHost() at the call site in terminal-shortcut-policy.ts, and an existing test already pins that off-Windows panes require active KKP negotiation regardless of the configured encoding. All paths use path.join; no separator is hardcoded. Note that listOpenCodeDatabases uses readdir plus a filename regex rather than a glob library, so there is no forward-slash-in-pattern hazard.
  • SSH / remote: No impact. Remote and WSL OpenCode sources are resolved separately in session-scanner-opencode-sources.ts, which already used ~/.local/share/opencode for WSL homes and is untouched by this PR. This change only affects the local-host lookup, which now agrees with the WSL logic instead of contradicting it.
  • Performance: No hot-path change. Fix A is one path.join in a scan that already hits the filesystem. Fix B is a record property read, and the call site is lazy — getWindowsShiftEnterEncoding is only invoked on Shift+Enter, never on ordinary keystrokes (an existing test asserts this).

Screenshots

No visual change.

Testing

  • pnpm lint (npx oxlint on the changed files)
  • pnpm typecheck (config/tsconfig.node.json + config/tsconfig.tc.web.json)
  • pnpm test — ran the affected suites rather than the full 30+ min suite: src/main/opencode-usage/, src/main/ai-vault/, and the four terminal-pane suites covering Shift+Enter (766 tests, all passing)
  • pnpm build
  • Added or updated high-quality tests that would catch regressions, or explained why tests were not needed

AI Review Report

Reviewed for correctness, cross-platform behavior, SSH/remote impact, performance, and regression surface.

The review flagged one real defect in the original patch, which is fixed here. The first version of the Fix A regression test exported a new pure resolver resolveOpenCodeDataDirectory({ env, homeDir }) and asserted against injected values. That test was vacuous: main's bug lives behind process.platform === 'win32', which the test never stubbed, so on any non-Windows CI host main's own logic satisfies the same assertion. Transplanting main's resolver verbatim and running the assertion against it confirmed it returns the expected value on macOS/Linux. On origin/main the test failed only with TypeError: resolveOpenCodeDataDirectory is not a function — i.e. it was detecting a missing symbol, not a wrong path, and would have kept passing if someone reintroduced the %LOCALAPPDATA% branch. Replaced it with a test that drives the real listOpenCodeDatabases entry point with process.platform stubbed to win32 and a real DB on disk; it now fails on main for the correct reason (see Proof of fix). That also made the exported options-injection API unnecessary, so getOpenCodeDataDirectory is back to a 4-line private function and the production diff shrank.

Cross-platform compatibility was checked explicitly for macOS, Linux, and Windows, covering path separators, drive letters, the glob-vs-readdir distinction, shortcut byte protocols, and the Windows-host gating of the Shift+Enter encoding — see Compatibility above for the per-platform findings. The review also confirmed neither bug is already fixed on main, and that the Fix B config change touches only the pi entry.

Security Audit

No new attack surface. No command execution, no network calls, no IPC surface, no auth or secret handling, no dependency changes.

  • Path handling: the change narrows the set of directories Orca reads — it removes a lookup location rather than adding one. The resolved directory is still derived from homedir() or the pre-existing XDG_DATA_HOME override, and the filename filter remains an anchored regex (/^opencode(?:-[A-Za-z0-9_.-]+)?\.db$/) applied to readdir entry names, so it cannot traverse outside the data directory. Reads stay wrapped in the existing try/catch.
  • Input handling: the Shift+Enter change routes a fixed, hardcoded byte sequence (\x1b[13;2u) and is only reachable via trusted foreground process evidence. Hook- and OSC-derived status remain excluded from routing authority, so PTY output still cannot forge it — the existing test covering that is unchanged and passing.

No follow-up security work needed.

Notes

  • Why these two fixes ship together: they were reported and submitted as one Windows-bugfix pair, and each is a genuinely independent one-line behavior change in a different subsystem (src/main/opencode-usage/ vs src/shared/) with no shared code, no shared test file, and no interaction. Splitting would add review overhead without reducing risk; each fix has its own separately-labelled failing-on-main proof above, so either can be reverted alone.
  • listOpenCodeDatabases() still swallows readdir errors and returns [], so a genuinely unreadable or missing data directory remains silent in the UI. That is what made [Bug]: OpenCode sessions missing from Agent Session History on Windows — getXdgDataHome() returns %LOCALAPPDATA% instead of ~/.local/share #10332 hard to diagnose in the first place. Surfacing an AiVaultScanIssue there is a worthwhile follow-up but is deliberately out of scope for a targeted bugfix.
  • Fix B addresses Pi specifically, per the issue. The underlying fragility the issue describes — every agent without windowsShiftEnterEncoding relies on live KKP flags that tool subprocesses can reset — still applies to the remaining agents. Making the KKP tracker resilient to reset sequences, or re-pushing KKP on focus return, would fix the whole class rather than one agent at a time; that is a larger change and a good follow-up.

Closes #10332
Closes #9703

Original patch by @itisvincent.

…i CSI-u Shift+Enter

OpenCode stores its SQLite DB at ~/.local/share/opencode/opencode on every
platform, including Windows. The OpenCode usage scanner's getXdgDataHome()
returned %LOCALAPPDATA% on Windows, so it looked under
%LOCALAPPDATA%/opencode (which does not exist), never ran the SQLite
session scanner, and OpenCode sessions were missing from Agent Session
History. Resolve the data directory to ~/.local/share/opencode on all
platforms (XDG_DATA_HOME still wins when set). Exports a pure
resolveOpenCodeDataDirectory() so the Windows path is regression-tested
without writing to the real home dir. (stablyai#10332)

Pi enables the Kitty keyboard protocol at startup and decodes CSI-u, but
TUI_AGENT_CONFIG['pi'] never set windowsShiftEnterEncoding, so on Windows
Shift+Enter fell back to the flaky live-KKP-flag path: it inserted a
newline after start/refocus but submitted the message after a tool ran a
subprocess (which reset the KKP flags). Set windowsShiftEnterEncoding:
'csi-u' for pi, mirroring the Droid fix, so the trusted CSI-u route is
used reliably. (stablyai#9703)

Co-authored-by: Vincent <[email protected]>
@coderabbitai

coderabbitai Bot commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The change updates OpenCode data-directory resolution to use XDG_DATA_HOME or a ~/.local/share/opencode fallback, with tests covering Windows behavior, environment precedence, and the OPENCODE_DB override. It also configures Pi to use CSI-u for Windows Shift+Enter and expands terminal encoding tests to distinguish trusted foreground evidence from launch ownership and trusted status alone.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes address both linked bugs by fixing OpenCode path resolution on Windows and adding Pi's Windows Shift+Enter encoding, with tests.
Out of Scope Changes check ✅ Passed The diff stays focused on the two linked fixes and their regression tests.
Title check ✅ Passed The title clearly summarizes both Windows bug fixes and matches the main changeset.
Description check ✅ Passed All required sections are present and substantive, including testing, screenshots, review, security, and notes.

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

Comment @coderabbitai help to get the list of available commands.

@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.

🧹 Nitpick comments (1)
src/main/opencode-usage/scanner.test.ts (1)

25-40: 🗄️ Data Integrity & Integration | 🔵 Trivial | ⚡ Quick win

Cover the OPENCODE_DB override in regression tests.

These tests cover XDG precedence and the fallback directory, but not the required OPENCODE_DB override. Add a scanner-level case asserting that an explicit database path still takes precedence over the derived directory.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 50549d2d-0e0c-4e26-974a-72929c670e8c

📥 Commits

Reviewing files that changed from the base of the PR and between a0971e0 and f1372a5.

📒 Files selected for processing (4)
  • src/main/opencode-usage/scanner.test.ts
  • src/main/opencode-usage/scanner.ts
  • src/renderer/src/components/terminal-pane/terminal-windows-shift-enter.test.ts
  • src/shared/tui-agent-config.ts

The previous regression test called a newly exported pure resolver with an
injected env/homeDir, so on any non-Windows CI host it passed against the
buggy code too: main's %LOCALAPPDATA% branch sits behind
`process.platform === 'win32'`, which the test never stubbed. It failed on
main only with "resolveOpenCodeDataDirectory is not a function".

Drive `listOpenCodeDatabases` with `process.platform` stubbed to win32 and a
real DB on disk instead, so the test fails on main for the actual reason
(scans %LOCALAPPDATA%, finds nothing). That removes the need for the exported
options-injection API, so `getOpenCodeDataDirectory` goes back to a 4-line
function. Also covers the OPENCODE_DB override per review feedback.

Co-authored-by: Orca <[email protected]>
@itisvincent

Copy link
Copy Markdown
Contributor Author

Splitting out the Shift+Enter half (#9703) into a clean, rebased PR since the OpenCode Windows-data-dir half (#10332) landed independently in #10362 and this PR now conflicts on scanner.ts.

The Pi CSI-u Shift+Enter fix is now here: #11769 (rebased onto current main, OpenCode changes dropped as redundant). Happy to close this one in favor of #11769 unless you'd prefer to repurpose it.

@nwparker

nwparker commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Closing as superseded — the linked issue(s) for this PR were fixed by another change that has already been merged.

Superseding merge: #11769, #10362

Thank you for the contribution! If anything remains unaddressed, please open a new issue or PR against current main.

@nwparker nwparker closed this Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

2 participants