Repository navigation
fix(ui): stop mouse movement from typing escape codes into a focused agent #1074
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // Package inputguard frames terminal input for Bubble Tea. Complete messages | ||
| // are handed over whole; a broken SGR report is cut at the byte that cannot | ||
| // belong to it, a CSI keeps its intermediate bytes, and a short SS3 key or | ||
| // half a UTF-8 character waits briefly for its remaining bytes. | ||
| package inputguard | ||
|
|
||
| import "unicode/utf8" | ||
|
|
||
| const esc = 0x1b | ||
|
|
||
| // completeLen is the length of the longest prefix that ends on a message | ||
| // boundary: after a whole escape sequence or rune. A tail inside a sequence is | ||
| // left for the next read. | ||
| func completeLen(b []byte) int { | ||
| i, done := 0, 0 | ||
| for i < len(b) { | ||
| if b[i] != esc { | ||
| _, size := utf8.DecodeRune(b[i:]) | ||
| if size == 1 && !utf8.FullRune(b[i:]) { | ||
| return done | ||
| } | ||
| i += size | ||
| done = i | ||
| continue | ||
| } | ||
| end := escapeEnd(b, i) | ||
| if end < 0 { | ||
| return done | ||
| } | ||
| i, done = end, end | ||
| } | ||
| return done | ||
| } | ||
|
|
||
| // escapeEnd returns the index just past the escape sequence starting at i, or | ||
| // -1 while the bytes seen so far could still grow into one. CSI parameter and | ||
| // intermediate bytes are kept, so mode reports such as ?2026;1$y stay whole. | ||
| // Anything else is an ESC-prefixed key, which is how Bubble Tea reads it too. | ||
| func escapeEnd(b []byte, i int) int { | ||
| if i+1 >= len(b) { | ||
| return -1 | ||
| } | ||
| switch b[i+1] { | ||
| case '[': | ||
| sgr := i+2 < len(b) && b[i+2] == '<' | ||
| j := i + 2 | ||
| for j < len(b) && b[j] >= 0x30 && b[j] <= 0x3f { | ||
| j++ | ||
| } | ||
| for j < len(b) && b[j] >= 0x20 && b[j] <= 0x2f { | ||
| if b[j] == '$' && j > i+2 { | ||
| // urxvt ends its Shift+Home/End reports on '$' with no final. | ||
| if params := string(b[i+2 : j]); params == "7" || params == "8" { | ||
| return j + 1 | ||
| } | ||
| } | ||
| j++ | ||
| } | ||
| if j >= len(b) { | ||
| return -1 | ||
| } | ||
| if b[j] < 0x40 || b[j] > 0x7e { | ||
| return -1 | ||
| } | ||
| if sgr && b[j] != 'M' && b[j] != 'm' { | ||
| // An SGR report ends only on M or m; anything else is a break | ||
| // the caller turns into a key press. | ||
| return -1 | ||
| } | ||
| if b[j] == 'M' && j == i+2 { | ||
| // X10 mouse: the final byte carries three more. | ||
| if j+4 > len(b) { | ||
| return -1 | ||
| } | ||
| return j + 4 | ||
| } | ||
| return j + 1 | ||
| case 'O': | ||
| // SS3 carries one more byte. | ||
| if i+2 >= len(b) { | ||
| return -1 | ||
| } | ||
| return i + 3 | ||
| } | ||
| // An escape marking alt carries one rune. | ||
| if !utf8.FullRune(b[i+1:]) { | ||
| return -1 | ||
| } | ||
| _, size := utf8.DecodeRune(b[i+1:]) | ||
| return i + 1 + size | ||
|
Comment on lines
+86
to
+90
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep an Alt-prefixed escape sequence together. If Frame the sequence after the Alt prefix before declaring the message complete.
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
sed -n '1,120p' internal/inputguard/guard.goRepository: wainmann/agent-manager
Length of output: 2169
Buffer control strings until their terminator arrives.
escapeEndtreatsESC ](OSC),ESC P(DCS),ESC X(SOS),ESC ^(PM), andESC _(APC) as complete two-byte Alt-prefixed runes. These introducers start strings that end with BEL or ST (ESC \). When a read ends inside such a string,completeLenreleases the partial string to Bubble Tea. The text after the introducer then arrives as ordinary input. This contradicts thecompleteLencontract that a tail inside a sequence is left for the next read, and the PR's goal of keeping OSC and control-string messages buffered.Add a control-string case to
escapeEndfor],P,X,^, and_. Scan forward for BEL orESC \. Return the index after the terminator when one is found, and return -1 when none is found, so the bytes are held. Before this change, confirm thatGuard.Readhas a bounded release path for a held control string. Without it, an unterminated OSC can hold later input, including Ctrl+C. Update the tests so they cover split terminators and Ctrl+C recovery.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not fixed in this PR. OSC/DCS/APC are pre-existing control-string ownership issues between
systheme.Detect()and the stdin reader, tracked in #1081. This PR documents the resulting keys/text in Known limits and deliberately does not add a control-string parser that would starvesystheme. Leaving this open for a maintainer scope decision.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Analysis chain
Script executed:
Length of output: 50408
@NewbieToEverythingYou’re right. I incorrectly stated that buffering control strings was a goal of this PR. The PR documents terminal replies as a known limit, and#1081tracks the reader-ownership issue.I’m not marking the finding as addressed. I’ll leave the scope decision to a maintainer.
Learnings used
You are interacting with an AI system.