Skip to content

fix(pty): fix a batch of Windows terminal and PTY daemon issues - #66793

Open
neriousy wants to merge 6 commits into
devfrom
windows-pty-fixes
Open

neriousy wants to merge 6 commits into
devfrom
windows-pty-fixes

Conversation

@neriousy

@neriousy neriousy commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Issue for this PR

Closes #61644
Closes #51051
Closes #65655
Closes #65666

Refs #65175, #63241

Type of change

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

What does this PR do?

A batch of Windows terminal fixes, one commit each:

No feature flag: these are fixes. The daemon change reaches users with the next @opencode/pty release.

Not in this PR: signing opencode-pty.exe in release-process-daemon.yml. That needs the Windows job to log in to Azure under the release environment, which I can't set up or test from here.

How did you verify your code works?

On Windows 11 (26H2), with pwsh 7, PowerShell 5.1, cmd and Git Bash:

  • New terminating_a_terminal_ends_a_descendant_that_cannot_leave_its_console (tests/runtime.rs). It hangs on dev until the 45 s watchdog and passes in about 1.5 s with the fix. The full daemon suite, cargo fmt --check and cargo clippy --all-targets --all-features -D warnings pass.
  • A stress loop over the daemon protocol: Git Bash terminals deleted 0.15 to 1.2 s after creation. On dev, 12 of 150 deletes never answered, and 7 to 15 of 200 hung with 8 loops in parallel. With the fix, 0 of 150 hung, the slowest took 1.5 s, and no conhost or git was left behind. pwsh, PowerShell and cmd terminals were unaffected before and after.
  • New core test replaces a daemon that takes requests but no longer answers them, on TestClock, --rerun-each 10. The existing "does not replay a dispatched mutating request" test still passes. Also new: a Windows test for the refused-spawn message.
  • New server test opens a session terminal in the configured shell. It fails on dev with pwsh.EXE instead of the configured shell.
  • New terminalScrollKey unit tests, including that Shift+Home and Shift+End are left to the shell; all 135 terminal tests in gui-plugins pass. I checked the paste bytes against pwsh, PowerShell and cmd PTYs.
  • bun run typecheck passes (36 tasks) and lint:changed is clean. bun run lint fails only on check-greys in services/site, which this PR doesn't touch. In core, rejects incompatible daemons… fails on Windows before and after this change because it spawns sleep.

Screenshots / recordings

No UI change beyond key handling and paste.

Checklist

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

Added by the opencode bot (AI-generated).

Related: #65175, #63241

… on Windows

Terminate killed only a terminal's root process, then waited without limit for the ConPTY output to end. A program that cannot leave the console (a suspended one, as when Git Bash's prompt starts git while its MSYS parent is killed) kept that output open, so the delete never answered and the console host leaked. The root shell now runs in a job; when teardown has not finished after a grace period, terminate ends what is left of the job, and it stops waiting after another one.
A request whose reply goes missing never reset the registration, so a daemon that still accepted connections but answered nothing failed every later shell and terminal request until the server restarted. When a ping on a fresh connection gets no reply either, give the daemon up so the next request starts a fresh one. Also keep the daemon's stderr in a capped log beside its runtime directories, and name Smart App Control and application control policies when Windows refuses to start the binary.
Session terminals ignored the shell config and fell back to \, which is usually unset on Windows, so they always opened PowerShell while the shell tool used the configured shell. Resolve the configured shell in the session's location, with the config loaded now, when the client sends no command.
…Shift+PageUp

A Windows clipboard ends lines with CR LF, which PowerShell read as Enter plus a stray line feed; pasted line breaks now go to the PTY as one CR. Shift+PageUp, PageDown, Home and End move through the scrollback on the normal screen, as in other terminals, and still reach programs on the alternate screen.
The persistent terminal smoke test expected the PTY root to hold only the daemon's runtime directory; it now also holds opencode-pty.log.

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

The configured-shell fix and the unresponsive-daemon replacement work as described. I checked both by running the new server and core tests on the base, where they fail, and on this branch, where they pass. I couldn't run the Windows job-object teardown (no Rust toolchain, and it's Windows-only) or the desktop terminal's paste and scroll-key changes here.

Public API: no HTTP, SDK, config or CLI changes. Session terminals created without command now open the configured shell instead of $SHELL, and the daemon now writes <state>/pty/opencode-pty.log.

Concerns, in order:

  • The new log file breaks the existing server test creates two persistent terminals for one session through the client API. It passes on the base and fails here.
  • The configured shell isn't loaded yet when a terminal opens on a cold location.
  • Taking over Shift+Home and Shift+End also takes them from PowerShell's line editor (PSReadLine), which uses them to select text.
  • The new server test is heavy for what it checks.

Related: #65175, #63241


// One log for every daemon of this root, beside their runtime directories, which their sweep leaves alone. It starts
// over once it passes 1 MB.
async function openLog(root: string) {

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.

The log goes in root, next to the daemon's runtime directories. That breaks creates two persistent terminals for one session through the client API in packages/server/test/persistent-pty.test.ts: it reads fs.readdir(fixture.directory) and expects exactly one entry, the daemon directory, and now gets opencode-pty.log as well. It passes on the base and fails with this PR. Either update that test or move the log somewhere the runtime-root listing doesn't see. Separately, the 1 MB check only runs when a daemon is spawned, so a long-lived daemon can still grow the file without limit.

// Terminals are process-global and the configured shell belongs to a location, so read it in the session's
// location, with whatever config is loaded now, to open the shell the shell tool runs. Without the session or its
// location the terminal keeps the default shell.
const configuredShell = (sessionID: string) =>

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.

Because this reads whatever config is loaded at that moment, a terminal opened before the location's plugins have loaded still gets the default shell. That includes the first terminal after a cold start, which is exactly the Windows case #51051 is about. The new test has to poll /api/shell until the shell shows up before it opens a terminal. Users who open a terminal right after launch will still sometimes get PowerShell. Could this wait for the location's config to be ready, even if not for all plugins?


// Programs on the alternate screen (vim, less) keep these keys; on the normal screen they move through the
// scrollback, as in other terminals.
const scroll = terminalScrollKey(event)

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.

On the normal screen this always takes Shift+Home and Shift+End. PowerShell's line editor (PSReadLine), the main Windows shell, binds them by default to select to the start and end of the line, and fish and zsh users sometimes bind them too. Windows Terminal leaves Shift+Home and Shift+End to the shell and only scrolls with Ctrl+Shift+Home and Ctrl+Shift+End. Shift+PageUp and Shift+PageDown look safe; Home and End could be limited to Ctrl+Shift, or not taken at all.

const base = HttpServer.formatAddress(server.address)
const session = yield* request(base, "POST", "/api/session", { location: { directory: fixture.root } })
const sessionID = Schema.decodeUnknownSync(Schema.Struct({ id: Session.ID }))(session.data).id
// The configured shell arrives with the location's plugins, and the server answers with what is loaded, so wait

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.

This test is heavy for what it checks: a 30 s timeout, a real-time loop that polls /api/shell with 20 ms sleeps until the config loads, and a full server boot. Its timing depends on how fast the location boots, which makes it likely to be flaky on slow CI. If the handler waited for config (see the comment in the handler), this could shrink to one request and one assertion. Otherwise, a smaller check of the shell-resolution step would cover the fix.

PowerShell's PSReadLine selects to the start and end of the line with Shift+Home and Shift+End. Go to the top and bottom of the scrollback with Ctrl+Shift+Home and Ctrl+Shift+End instead, as Windows Terminal does, and also page with its Ctrl+Shift+PageUp and Ctrl+Shift+PageDown.

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

The new commits fix two earlier points. The runtime-directory test now accounts for the log file and passes. Shift+Home and Shift+End now go to the shell; Ctrl+Shift+Home and Ctrl+Shift+End go to the top and bottom. The rest of the log thread is still open: the 1 MB limit is only checked when a daemon starts, so a long-running daemon can still grow the log without limit. The points on reading the configured shell before config loads, and on the size of the server test, are also unchanged. Checked by running the server persistent-pty tests (8 pass) and the gui-plugins key tests (8 pass), plus a gui-plugins type check. Public API is unchanged.

This branch has not been deployed

No deployments
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