Skip to content

fix(tui): attach auth token when editor port comes from env#32481

Open
timbit123 wants to merge 1 commit into
anomalyco:devfrom
timbit123:fix/tui-editor-configured-port-auth-token
Open

fix(tui): attach auth token when editor port comes from env#32481
timbit123 wants to merge 1 commit into
anomalyco:devfrom
timbit123:fix/tui-editor-configured-port-auth-token

Conversation

@timbit123

@timbit123 timbit123 commented Jun 15, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #29570
Closes #26852

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Fixes the editor file/selection context not syncing when opencode runs in a VSCode / Cursor integrated terminal.

The Claude Code extension injects CLAUDE_CODE_SSE_PORT into integrated terminals and writes a matching ~/.claude/ide/<port>.lock whose authToken its WebSocket server requires. External terminals don't get the env var, so they already worked via lock-file discovery. Two bugs on the configured-port path:

  1. Missing auth token (WSL2/VS Code integration stopped working: Editor context (/editor, focus, selections) is not syncing with OpenCode session #29570): the connection was opened without the token, so the server closed it with 1008 Unauthorized and no selection_changed events ever arrived.
  2. Stale port (IDE context awareness: stale CLAUDE_CODE_SSE_PORT env var blocks lock file fallback #26852): when the env var pointed at a dead port (e.g. left over from a previous VSCode session, its lock already gone), opencode kept retrying it and never fell back to lock discovery.

Now, for a configured port we read its ~/.claude/ide/<port>.lock and attach the token; if that lock is missing we fall back to scanning ~/.claude/ide for the live server matching the working directory. This runs on every (re)connect, so if the server dies mid-session it recovers on its own. A lockless OPENCODE_EDITOR_SSE_PORT override still connects directly. Also extracted a small readEditorLock helper so the by-directory and by-port lookups don't duplicate the parse/validate logic.

How did you verify your code works?

  • Reproduced against the running Claude Code server: tokenless connect → 1008 Unauthorized; with the lock token → handshake OK + selection_changed. A dead CLAUDE_CODE_SSE_PORT falls back to the live server.
  • Added regression tests: a configured port attaches the lock token, and a stale configured port (no lock) falls back to discovery.
  • bun test test/cli/tui/editor-context.test.tsx passes (7); tsgo --noEmit passes for packages/tui and packages/opencode.

Screenshots / recordings

N/A — not a visual change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Jun 15, 2026
@timbit123 timbit123 force-pushed the fix/tui-editor-configured-port-auth-token branch 2 times, most recently from dad47d8 to ad32734 Compare June 15, 2026 21:23
@github-actions github-actions Bot removed needs:issue needs:compliance This means the issue will auto-close after 2 hours. labels Jun 15, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

CLAUDE_CODE_SSE_PORT connections were opened without the lock file's auth
token, so the IDE server rejected them with 1008 in integrated terminals.
And when that port was stale, opencode never fell back to lock discovery.

Read the token from ~/.claude/ide/<port>.lock, and fall back to scanning
~/.claude/ide for the live server when the configured port has no lock.
@timbit123 timbit123 force-pushed the fix/tui-editor-configured-port-auth-token branch from ad32734 to 1430727 Compare June 15, 2026 22:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant