Skip to content

fix(pty): retain output by line and enforce buffer limits - #94

Open
BaconDroid wants to merge 13 commits into
shekohex:mainfrom
BaconDroid:fix/upstream-pr70
Open

BaconDroid wants to merge 13 commits into
shekohex:mainfrom
BaconDroid:fix/upstream-pr70

Conversation

@BaconDroid

Copy link
Copy Markdown

Problem

The RingBuffer used a flat string with naive slicing for overflow, which:

  1. Could not enforce line-count limits independently from byte-size
  2. Made it impossible to know which lines were discarded
  3. Had no configurable maximum number of lines (only character count)

Fix

Replace the string-based buffer with a line-array implementation that enforces
both a configurable line count limit (default 50,000) and byte-size limit
(default 1MB). Lines are discarded in one batch when either limit is reached;
a single oversized line keeps only its tail.

Changes across 6 files:

  • buffer.ts — complete rewrite: line-array with dual limits + configurable env vars
  • tools/read.ts — fix pagination offset calculation to use result.offset
  • tools/read.txt — document new dual-limit behavior
  • output-manager.ts — clamp negative offsets defensively
  • notification-manager.ts — simplify exit notification (use findLast)
  • session-lifecycle.ts — simplify status transition, remove unnecessary flush call

Verification

  • ✅ tsc --noEmit passes clean
  • ✅ bun run build:plugin succeeds

dhaern and others added 13 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>
…x#72)

Resolve relative workdirs against the session project directory before
permission checks and manager.spawn. Without resolution, permission
checks receive a raw relative path that may not align with the actual
working tree. Pass the project directory through to V2 tools for
consistent context.
…hekohex#70)

Replace string-based RingBuffer with a line-array implementation that
supports both byte-size and line-count limits. Lines are discarded in
one batch when either limit is reached, and oversized lines keep only
their tail.

Also: clamp negative offsets, simplify exit status transition, and
fix pagination offset in read() output.
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