claudetui: a dialog is recognised by its option rows, not by its words in the history (@alisenray on X)

A resumed conversation that discussed "Bypass Permissions mode" made every
--resume fail at 45s ("asked to accept running in Bypass Permissions mode,
not answered") with the prompt box showing: ready() took the words anywhere
in the restored history for the dialog.

The trust and Bypass Permissions dialogs are now recognised by structure
(optionDialog): the heading, then the No row and the Yes row, with only the
dialog's footer under them. The Claude in Chrome offer, the text-style
screen, sign-in and the two first-run screens must have their words at the
foot of the screen (headingDialog, lastMatchNear), not far up in history.
The "doing" text of a late run uses the same rules.

Run: TestShimIgnoresDialogWordsInHistory (history with the phrase, option
rows, and every other dialog's words above the prompt) is red on the old
tui.go (0 of 3 lines in 30s, "asks whether to trust the folder") and green
now. mica-run go test ./internal/claudetui ok (191s), go vet and
GOOS=windows go vet clean, gofmt -l clean. A first version matched the
heading by its last occurrence and broke the trust dialog (its Yes row
holds "trust this folder"); the existing trust and bypass tests caught it.

Not run: the full suite (queued after the push), a real Claude Code
--resume. The heading-only screens (Chrome, text style, sign-in) are bounded
by distance from the foot, not by option rows, so a history that ends
within 1500 characters of those words with no prompt box could still match;
not seen in the report.
This commit is contained in:
yetone
2026-11-20 00:27:07 +08:00
parent b17d9e3fca
commit 228da69efd
2 changed files with 93 additions and 11 deletions
+73 -11
View File
@@ -498,6 +498,68 @@ var (
trustSettle = 300 * time.Millisecond
)
var (
// the option rows a question is answered on: the unselected row has
// no ❯, so they are matched by their words
noRow = lazyre.New(`No,\s*exit`)
trustRow = lazyre.New(`Yes,\s*I trust`)
bypassRow = lazyre.New(`Yes,\s*I accept`)
trustAsk = "trust this folder"
bypassAsk = "Bypass Permissions mode"
chromeAsk = "keep browser tools off"
themeAsk = "Choose the text style"
// the most a dialog draws under its option rows: its footer
// ("Enter to confirm · Esc to cancel"). A resumed conversation that
// talks of the dialog has its prompt box and status line there.
dialogFoot = 160
// the most a dialog draws under its heading: its options, and a
// preview of the choice
dialogBody = 1500
)
// tailAfter is how many characters of words follow byte offset end.
func tailAfter(words string, end int) int {
return utf8.RuneCountInString(words[end:])
}
// optionDialog is whether words end on a question: its heading, then its
// option rows, a No and the yes row, with only the dialog's footer under
// them. The question's words alone are not it: a resumed conversation's
// history can hold them (#, @alisenray: "Bypass Permissions mode" in the
// history failed every --resume at 45s with the prompt box showing).
func optionDialog(words, ask string, yes *lazyre.RE) bool {
h := strings.Index(words, ask)
if h < 0 {
return false
}
end := -1
for _, re := range []*lazyre.RE{noRow, yes} {
m := re.FindAllStringIndex(words, -1)
if len(m) == 0 || m[len(m)-1][0] < h {
return false
}
if e := m[len(m)-1][1]; e > end {
end = e
}
}
return tailAfter(words, end) <= dialogFoot
}
// headingDialog is whether words end on a screen drawn under heading ask
// (an onboarding screen, the Claude in Chrome offer, sign-in): the
// heading is in the bottom of the screen, not further up in history.
func headingDialog(words, ask string) bool {
h := strings.LastIndex(words, ask)
return h >= 0 && tailAfter(words, h+len(ask)) <= dialogBody
}
// lastMatchNear is whether re matches in words with at most n characters
// after its last match.
func lastMatchNear(re *lazyre.RE, words string, n int) bool {
m := re.FindAllStringIndex(words, -1)
return len(m) > 0 && tailAfter(words, m[len(m)-1][1]) <= n
}
// lastAt is where re's last match in p starts, -1 with none.
func lastAt(re *lazyre.RE, p string) int {
m := re.FindAllStringIndex(p, -1)
@@ -604,30 +666,30 @@ func (s *shim) ready() error {
now, _ := s.looks()
nowWords := strings.Join(strings.Fields(now+" "+p), " ")
doing := "showed no prompt"
if strings.Contains(nowWords, "trust this folder") {
if optionDialog(nowWords, trustAsk, trustRow) {
doing = asking
} else if strings.Contains(nowWords, "Bypass Permissions mode") {
} else if optionDialog(nowWords, bypassAsk, bypassRow) {
doing = bypassAsking
} else if strings.Contains(nowWords, "keep browser tools off") {
} else if headingDialog(nowWords, chromeAsk) {
doing = chromeAsking
}
s.doing.Store(doing)
switch {
case strings.Contains(p, "trust this folder") && os.Getenv(TrustEnv) != "1":
case optionDialog(words, trustAsk, trustRow) && os.Getenv(TrustEnv) != "1":
return errUntrusted
case strings.Contains(words, "Bypass Permissions mode") && os.Getenv(BypassEnv) != "1":
case optionDialog(words, bypassAsk, bypassRow) && os.Getenv(BypassEnv) != "1":
return fmt.Errorf("Claude Code's interactive mode runs in Bypass Permissions mode and asks to accept it, which only its user answers: %s", s.lastScreen())
case signIn.MatchString(p):
case signIn.MatchString(p) && lastMatchNear(signIn, words, dialogBody):
return fmt.Errorf("Claude Code's interactive mode asks to sign in: %s", s.lastScreen())
case firstRun.MatchString(words):
case lastMatchNear(firstRun, words, dialogFoot):
if err := onboard("notes", "\r"); err != nil {
return err
}
case terminalSetup.MatchString(words):
case lastMatchNear(terminalSetup, words, dialogFoot):
if err := onboard("terminal", "\x1b"); err != nil {
return err
}
case (strings.Contains(p, "trust this folder") || strings.Contains(words, "Bypass Permissions mode")) && answered < 3:
case (optionDialog(words, trustAsk, trustRow) || optionDialog(words, bypassAsk, bypassRow)) && answered < 3:
// the choice is the last one drawn after ❯, "No, exit" at
// first. A key sent as the question is first drawn can be lost
// to its being drawn again on No, so Enter goes only to a Yes
@@ -655,7 +717,7 @@ func (s *shim) ready() error {
_, _ = io.WriteString(s.pty, "\x1b[B")
}
}
case strings.Contains(words, "keep browser tools off"):
case headingDialog(words, chromeAsk):
// Claude in Chrome found: kept off, as the bridge runs it
// --no-chrome. Answered once the offer has stayed drawn, as
// the onboarding is: 2.1.319 turns away a key sent just after
@@ -664,7 +726,7 @@ func (s *shim) ready() error {
if err := onboard("chrome", "\x1b"); err != nil {
return fmt.Errorf("Claude Code's interactive mode asks whether to use Claude in Chrome and takes no answer; run Claude Code once in a terminal and answer it: %s", s.lastScreen())
}
case strings.Contains(words, "Choose the text style"):
case headingDialog(words, themeAsk):
if err := onboard("theme", "\r"); err != nil {
return err
}
+20
View File
@@ -346,6 +346,15 @@ func fakeClaude(args []string) {
time.AfterFunc(d, func() { _ = os.WriteFile(listed, nil, 0o600) })
}
streams := 0
// FAKE_HISTORY: a resumed conversation's history is drawn above the
// prompt, and it talks of the dialogs
if os.Getenv("FAKE_HISTORY") != "" {
fmt.Print("\r\n> Why does it say asked to accept running in Bypass\x1b[1CPermissions\x1b[1Cmode?\r\n" +
" ❯ No, exit\r\n ❯ Yes, I accept\r\n Do you trust this folder? Yes, I trust this folder\r\n" +
" Choose the text style that looks best; Select login method; Press Enter to continue;\r\n" +
" Enter to confirm · Esc to skip, keep browser tools off\r\n" +
strings.Repeat(" and more history of that conversation, long past the dialogs' words\r\n", 40))
}
fmt.Print("\r\n❯ ")
// the prompt's words, and the prompt as drawn, again as a paste
// starts and as one is taken in. As Claude Code 2.1.318 does, it draws
@@ -1182,6 +1191,17 @@ func TestShimAcceptsBypassDisclaimer(t *testing.T) {
}
}
// A resumed conversation whose history holds the dialogs' words and
// option rows is not a dialog: the prompt box is showing, and the run
// goes on, with no BypassEnv or TrustEnv to answer one (@alisenray on X:
// every --resume failed at 45s, "not answered", no dialog on screen).
func TestShimIgnoresDialogWordsInHistory(t *testing.T) {
out := runShim(t, []string{"FAKE_HISTORY=1"}, append([]string{fakePath(t)}, pArgs...), []string{"hello"}, 3)
if out[0]["subtype"] != "init" || out[1]["type"] != "assistant" {
t.Fatalf("got %v, want an interactive turn", out)
}
}
func TestPasteable(t *testing.T) {
for _, c := range []struct {
in string