Skip to content

fix(web): reconnect session sockets automatically (backport from dhaern's #78) - #92

Closed
BaconDroid wants to merge 11 commits into
shekohex:mainfrom
BaconDroid:main
Closed

BaconDroid wants to merge 11 commits into
shekohex:mainfrom
BaconDroid:main

Conversation

@BaconDroid

Copy link
Copy Markdown

Summary

Backport of dhaern's PR #78 to the upstream repo. Adds automatic WebSocket reconnection for session sockets, fixing terminal output loss when the browser tab is hidden or network disconnects.

Changes

  • New: src/web/client/hooks/session-socket.ts — reusable connectSessionSocket() helper that manages WebSocket lifecycle with automatic retry on close
  • Refactor: use-web-socket.ts — stripped down from 139 lines of socket logic to delegation via the new helper
  • Simplify: app.tsx, terminal-renderer.tsx, use-session-manager.ts — removed manual socket management in favor of the new hook
  • Add: tests for session-socket.ts and updated websocket.test.ts
  • Update: bun.lock

Author Credit

Original work by dhaern — PR #78. Backported and integrated into this fork.

Motivation

Without automatic reconnection, users lose their PTY output whenever the browser tab loses focus or network briefly drops. The new connectSessionSocket() function handles reconnection transparently on ws.onclose.

Verification

  • Merged cleanly from dhaern's branch (refs/pull/78/head) — no conflicts with existing V2 fork work
  • Test suite updated to cover the new socket helper

dhaern and others added 11 commits October 6, 2026 16:50
Add rawData to subscribed replies to order snapshots before live output.
Write directly to xterm, isolate session sockets, and retry after disconnects.
Reset the terminal in band (RIS) so queued writes and a half-parsed escape
sequence cannot corrupt a snapshot. Keep HTTP input fallback and existing
raw-buffer endpoints.
The `. ` entrypoint exported only named symbols — no default at all — so
the V2 loader's bare-ref resolution schema-failed with
`Missing key at ["default"]` (shekohex#67). The documented `opencode-pty/v2`
subpath ref cannot install either (the V2 loader parses subpath refs
as npm install specs), leaving no published-package path to load the
plugin on V2.

Add the dual-mode default export (the pattern opencode-mem shipped in
2.27.0 via PR #311): `{ id, server, setup }` — V1 (>= 1.18.29) calls
`server()`, V2 calls `setup()`. The `./v2` subpath export stays for
direct-import consumers. Correct the README's V2 example, which used
the V1 `plugin` field (silently ignored by V2) and recommended the
un-installable subpath ref.

Adopted from jss79's shekohex#84.

Refs shekohex#67, shekohex#85, shekohex#83.

Co-authored-by: Jeff Sheldon <jeff@recurly.com>
`tsc -p tsconfig.build.json` failed on src/web/server/server.ts:109 —
Bun's `ws.subscribe()` returns a boolean and the `websocket.open` handler
is typed `void | Promise<void>` — so the build script exited non-zero and
only the emit-on-error side effect produced dist. Wrap the call so the
build passes on its own terms instead of depending on a partial emit.
…t loading

shekohex#86: 0.4.0 loaded `active` on V2 but registered no `pty_*` tools, because the
shipped adapter never called `ctx.tool.transform`. Loading is not
registration — the new test imports the *built* `dist/index.js` and
`dist/src/v2/index.js`, runs each `setup()` against a recording
`ctx.tool`, and asserts all five tools arrive with a name, description,
input schema and execute. Removing the registration from dist makes the
test fail, so a stale build cannot ship again.

shekohex#85/shekohex#83/shekohex#67: the README now documents the bare ref plus the `plugins`
key, that `opencode-pty/v2` cannot be used as a plugin spec, the
directory-entry resolution rules, and the local-clone fallback for the
`<git dep preparation failed>` install failure.
The V2 adapter wired no authorizer, so `pty_spawn` and `pty_write` ran
any command regardless of the host's `permissions` rules — a configured
`deny` was silently bypassed.

opencode V2 has no `config.get()` on the plugin context, so the rules
cannot be read and re-implemented the way the V1 path reads
`permission.bash`. `ctx.permission.assert` is the only documented surface
that evaluates them, including saved approvals and agent rules, and it
prompts when a rule resolves to `ask` — so the V2 authorizer delegates to
it with action `shell` and the exact command line as the resource.

The check needed to know which tool call it belongs to, so the
`PermissionAuthorizer` contract takes an optional
`PermissionRequestContext` (session, agent, message, call id), which the
PTY tools derive defensively from either host's tool context. A
context-less call keeps the old two-argument shape, so V1 behaviour and
its tests are unchanged.

Safe default: when `assert` exists but the check cannot be attributed to a
session, the authorizer denies rather than allows — allowing would
reintroduce this bug. A host without `ctx.permission` gets a startup
warning and no checks, matching the pre-2.0 fallback.
A workdir of `/workspace/app/../etc` normalised naively started with the
location prefix, so it was treated as inside the project and skipped the
`external_directory` check entirely. Resolve `.` and `..` lexically on both
sides, and pass the resolved path to the host so a rule is matched against
the real boundary.
A `github:` entry is installed by npm, and two things broke it here.

The repository ships sources only: `dist` is gitignored and nothing builds it
on install, so the resolved entrypoint did not exist. `prepare` now runs the
plugin build, and `dist` is committed as well, so both a git install and a
plain clone resolve.

Plain `npm install` failed with ERESOLVE on `@opentui/core`: the pinned
devDependency (`>=0.4.5`) conflicts with what `@opencode/plugin@2.2.5` needs
(`>=0.8.0`). The dev range now matches, and a repo-level `legacy-peer-deps`
keeps a consumer's install from inheriting the conflict.

`files` now ships `dist` with the built entrypoints the dual-mode default
export points at.
`prepare` runs during a `github:` install and needs the full dev tree plus
`bun` for the copyfiles step, so it made the git path fail harder than the
missing entrypoint it was meant to fix. `dist` is committed, so nothing has to
build at install time.
npm runs `prepack` while preparing a `github:` install, and it ran
`bun build:prod` - tsc, copyfiles over the network, then a vite build of the
web UI. In the installer's sandbox that fails, which is what surfaced as
`<git dep preparation failed>`; a plain `npm install` from a shell succeeded
because nothing needed packing. `dist` is committed, so packing has nothing to
build. `release.sh` still runs the build explicitly before publishing.
Backport of shekohex#78 by dhaern. Adds automatic WebSocket
reconnection for session sockets, fixing the issue where terminal output
stops when the browser tab is hidden or the network briefly disconnects.

New: src/web/client/hooks/session-socket.ts - reusable connectSessionSocket()
that handles reconnection on close, message dispatching, and graceful shutdown.

Refactors use-web-socket.ts to use the new helper (139 → 0 lines of socket logic).
Simplifies app.tsx and terminal-renderer.tsx to remove manual socket management.

Co-authored-by: dhaern <unknown>
@BaconDroid BaconDroid closed this Nov 16, 2026
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.

3 participants