fix: prevent terminal query response leakage#17
Merged
Conversation
- Add DISCONNECTING state to match documented state machine (§5.1) - Add ttyStateCheckInterval to track polling timer - Clear polling timer in disconnectTTY() to prevent leaks - Add max retries (10) with timeout protection for state transitions - Force transition to IDLE after max retries as recovery mechanism - Remove redundant local variable isReady, use ttyState uniformly - Document terminal protocol timing in ARCHITECTURE.md §5 This ensures robust state management during instance switching and prevents race conditions when connectTTY() is called rapidly. Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
…iltering Problem: When switching terminal windows or after TUI programs exit, xterm.js sends terminal query responses that appear as garbage text in the shell (e.g., ;1R, rgb:0b0b/1010/2020, ?2027;0$y). Root cause: xterm.js generates responses to terminal queries and forwards them via term.onData() callback, which then get sent back to PTY and displayed. Changes: - Add ttyState state machine (IDLE→CONNECTING→HANDSHAKING→READY) - Prevent terminal focus until WebSocket is READY - Add isTerminalQueryResponse() filter in term.onData() - Fix ttyState reset on instance stop - Prevent WebSocket reconnection for stopped instances Documentation: - Add §5.9 Terminal Query Response Filtering to ARCHITECTURE.md - Add detailed code comments linking to TERMINAL_FILTER_REVIEW.md - TERMINAL_FILTER_REVIEW.md documents known limitations and risks Known limitations (see docs/TERMINAL_FILTER_REVIEW.md): - Cannot distinguish user-intentional ESC sequences from auto responses - Regex may have edge cases or false positives - Some response types not explicitly covered
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ttyState) to prevent query leakageisTerminalQueryResponse()filter to block xterm.js auto-generated responsesttyStatereset on instance stop and prevent reconnection for stopped instancesARCHITECTURE.md§5.9Problem
When switching terminal windows or after TUI programs (like
opencodeCLI) exit, xterm.js sends terminal query responses that appear as garbage text in the shell:;1R(cursor position report fragment)rgb:0b0b/1010/2020(OSC color response fragment)?2027;0$y(DEC private mode report fragment)Root Cause
Changes
IDLE → CONNECTING → HANDSHAKING → READY → DISCONNECTINGttyState === 'READY'isTerminalQueryResponse()blocks known response patternsttyStateproperly reset when instance stopsDocumentation
docs/TERMINAL_FILTER_REVIEW.mdwith detailed analysisdocs/ARCHITECTURE.md§5.9 with data flow diagramKnown Limitations
docs/TERMINAL_FILTER_REVIEW.mdfor complete analysis:Test Plan
go build ./...