claudetui: the Claude in Chrome offer is declined once it has stayed drawn, and an error never ends on an empty screen

sowhynot (Discord, magpie 0.1.3216, Claude Code 2.1.319, macOS, Claude
Pro, interactive mode): every request and the endpoint test failed with
"claude-tui: Claude Code showed no prompt in the 21.9s its caller waits
for it:" and nothing after the colon; `claude -p` worked.

Cause: where the Claude in Chrome extension is installed and Claude
Code's tengu_chrome_auto_enable flag is on, 2.1.319 opens its
ChromeAutoEnableDialog as it starts (a claude.ai sign-in, no answer
recorded). The helper cleared its screen and sent Esc the moment it saw
"keep browser tools off". The dialog turns away a key that comes just
after it opens (refuseInput/refusedWithin) and draws nothing again for
it, so the screen the helper read stayed empty: nothing matched again,
the run waited out its caller, and lastScreen() had nothing to say. The
decline is never recorded, so every run met it again.

Fix:
- The offer is answered through onboard(): Esc once it has stayed drawn
  trustSettle, at most onboardTries times, then an error that says the
  offer takes no answer.
- seen(): with nothing drawn since the screen was cleared for
  redrawWait (1.5s), ready() reads the screen as it stands (the grid),
  so a key lost to a dialog that draws nothing again is sent again.
- What the run hadn't done is read from the screen as it stands too:
  "asked whether to use Claude in Chrome, not answered".
- lastScreen(): with nothing drawn since a clear it gives the grid's
  text, and with nothing on it, "nothing on its screen (N bytes read)".

There is no -p fallback for the one-off runs (the endpoint test, a
/usage read) by design: the shim never turns into -p (TestShimNeverPrints).

Run, on mote at this tree (base 665b9aff1):
- go test -count=1 ./internal/claudetui/: ok (118s).
- go vet ./internal/claudetui/ for darwin, linux and windows; go build
  ./... (darwin), GOOS=windows go build ./..., GOOS=linux go build
  -tags nogui ./...: ok. gofmt -l: clean.
- win-run (xiaomi-gpu): TestShimDeclinesChromeOffer,
  TestShimEndsAtItsCallersDeadline, TestShimNeverPrints,
  TestShimGoesThroughOnboarding: ok.
- Real Claude Code 2.1.319 (npm) through this helper in sandbox HOMEs on
  mote: onboarded, classic and CLAUDE_CODE_NO_FLICKER=1 renderers, init
  emitted; folder not trusted (MAGPIE_CLAUDE_TUI_TRUST=1), and
  onboarding not done (theme, notes) plus trust: gone through to the
  prompt, the message sent (401 on the fake API key, as expected).

Broken by hand:
- The old tui.go with the new tests: TestShimDeclinesChromeOffer waits
  30s with no answer; TestShimEndsAtItsCallersDeadline/chrome offer
  unanswered gives "showed no prompt in the 3s its caller waits for it: "
  with nothing after it, the reporter's line.
- seen() never reading the grid: TestShimDeclinesChromeOffer red (the
  fake turns keys away for 700ms, past the first Esc).

Not tried: the Chrome offer itself in a real 2.1.319. It needs a
claude.ai OAuth sign-in whose token /api/oauth/validate accepts, the
extension's folder in a Chrome profile and the GrowthBook flag on; no
sign-in is available on mote. The fake draws 2.1.319's dialog text and
its refusal is modelled on the dialog's source. Not run: the full
go test ./... before the push (it runs after, as the rules say).

Reported-by: sowhynot
This commit is contained in:
yetone
2026-11-07 11:27:58 +08:00
parent 665b9aff12
commit 9e6687e3a5
2 changed files with 118 additions and 8 deletions
+61 -8
View File
@@ -36,6 +36,7 @@ import (
"sync"
"sync/atomic"
"time"
"unicode/utf8"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/wslrun"
@@ -81,6 +82,8 @@ type shim struct {
grid *grid
watch bool
panel string
// cleared is when the screen was last cleared (clearScreen)
cleared time.Time
// rows ticks when a user message (or one queued mid-turn) reaches the
// transcript: a pasted message was sent
@@ -369,9 +372,34 @@ func (s *shim) clearScreen() {
s.screenMu.Lock()
s.screen = s.screen[:0]
s.panel = ""
s.cleared = time.Now()
s.screenMu.Unlock()
}
// redrawWait is how long the screen may stay as it was after a key, nothing
// drawn since it was cleared, before it is read as it stands (seen): the
// key was lost, and Claude Code draws nothing again for it.
const redrawWait = 1500 * time.Millisecond
// seen is what to read the screen by: what Claude Code drew since it was
// cleared, or, nothing drawn since for redrawWait, the screen as it
// stands. A dialog that turns away a key sent just after it opens (Claude
// Code 2.1.319's Claude in Chrome offer) draws nothing again, and a run
// that read only what was drawn since the key waited out its caller with
// nothing to say (sowhynot).
func (s *shim) seen() string {
p := s.plain()
if strings.TrimSpace(p) != "" {
return p
}
s.screenMu.Lock()
defer s.screenMu.Unlock()
if s.grid == nil || time.Since(s.cleared) < redrawWait {
return p
}
return s.grid.text()
}
// screenCols and screenRows are the size of Claude Code's terminal, the
// pseudo-terminal (child_unix.go) and Windows' pseudo console
// (child_windows.go) made alike, and the grid the helper reads it on. A
@@ -398,11 +426,25 @@ func (s *shim) looks() (now, panel string) {
return now, s.panel
}
// lastScreen is the end of what Claude Code drew, for an error to say.
// lastScreen is the end of what Claude Code drew, for an error to say:
// with nothing drawn since the screen was cleared, the screen as it
// stands, and with nothing on it, that nothing was drawn. An error that
// ended on an empty screen said nothing of what Claude Code showed
// (sowhynot).
func (s *shim) lastScreen() string {
p := strings.Join(strings.Fields(s.plain()), " ")
if p == "" {
now, _ := s.looks()
p = strings.Join(strings.Fields(now), " ")
}
if p == "" {
return fmt.Sprintf("nothing on its screen (%d bytes read)", s.nread.Load())
}
if len(p) > 600 {
p = p[len(p)-600:]
for i := 0; i < len(p) && i < utf8.UTFMax && !utf8.RuneStart(p[0]); i++ {
p = p[1:]
}
}
return p
}
@@ -498,6 +540,7 @@ func (s *shim) ready() error {
}
asking := "asked whether to trust " + folder + ", not answered"
bypassAsking := "asked to accept running in Bypass Permissions mode, not answered"
chromeAsking := "asked whether to use Claude in Chrome, not answered"
onboard := func(screen, key string) error {
if shownAt[screen].IsZero() {
shownAt[screen] = time.Now()
@@ -515,13 +558,19 @@ func (s *shim) ready() error {
return nil
}
for {
p := s.plain()
p := s.seen()
words := strings.Join(strings.Fields(p), " ")
// what it hadn't done, by the screen as it stands: what was
// drawn since a key is nothing until the next screen is drawn
now, _ := s.looks()
nowWords := strings.Join(strings.Fields(now+" "+p), " ")
doing := "showed no prompt"
if strings.Contains(p, "trust this folder") {
if strings.Contains(nowWords, "trust this folder") {
doing = asking
} else if strings.Contains(words, "Bypass Permissions mode") {
} else if strings.Contains(nowWords, "Bypass Permissions mode") {
doing = bypassAsking
} else if strings.Contains(nowWords, "keep browser tools off") {
doing = chromeAsking
}
s.doing.Store(doing)
switch {
@@ -567,11 +616,15 @@ func (s *shim) ready() error {
_, _ = io.WriteString(s.pty, "\x1b[B")
}
}
case strings.Contains(p, "keep browser tools off"):
case strings.Contains(words, "keep browser tools off"):
// Claude in Chrome found: kept off, as the bridge runs it
// --no-chrome
s.clearScreen()
_, _ = io.WriteString(s.pty, "\x1b")
// --no-chrome. Answered once the offer has stayed drawn, as
// the onboarding is: 2.1.319 turns away a key sent just after
// it opens and draws nothing again, and an Esc sent at once
// left every run on it (sowhynot)
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"):
if err := onboard("theme", "\r"); err != nil {
return err
+57
View File
@@ -194,6 +194,37 @@ func fakeOnboard(r *bufio.Reader, how string) {
_ = os.WriteFile(filepath.Join(cfg, "onboarded"), nil, 0o600)
}
// fakeChrome shows Claude Code 2.1.319's Claude in Chrome offer, drawn
// where the extension is found and no answer is recorded: "No, keep
// browser tools off" chosen. As 2.1.319's dialogs do, it turns away a key
// that comes within refuse of its opening and draws nothing again for it;
// a later Esc is the offer declined, recorded in the config folder, and
// it isn't shown again. With "deaf" it takes no key.
func fakeChrome(r *bufio.Reader, how string) {
cfg := os.Getenv("CLAUDE_CONFIG_DIR")
if _, err := os.Stat(filepath.Join(cfg, "chrome-declined")); err == nil {
return
}
const refuse = 700 * time.Millisecond
fmt.Print("\x1b[2J\x1b[H\r\n ╭───────────────────────────────────────╮\r\n │\x1b[1CClaude\x1b[1Cin\x1b[1CChrome\x1b[1Cextension\x1b[1Cdetected\r\n │\r\n │ Claude will use your Chrome browser by default — navigating sites,\r\n │ filling forms, and capturing screenshots in your existing session.\r\n │\r\n │\x1b[1C\x1b[38;2;177;185;249m❯\x1b[39m\x1b[1CNo,\x1b[1Ckeep\x1b[1Cbrowser\x1b[1Ctools\x1b[1Coff\r\n │ Yes, use my browser\r\n ╰───────────────────────────────────────╯\r\n Enter to confirm · Esc to keep browser tools off\r\n")
opened := time.Now()
for {
c, err := r.ReadByte()
if err != nil {
os.Exit(0)
}
if how == "deaf" || time.Since(opened) < refuse {
continue
}
if c == 0x1b {
_ = os.MkdirAll(cfg, 0o700)
_ = os.WriteFile(filepath.Join(cfg, "chrome-declined"), nil, 0o600)
fmt.Print("\x1b[2J\x1b[H")
return
}
}
}
func fakeClaude(args []string) {
if slices.Contains(args, "-p") {
sc := bufio.NewScanner(os.Stdin)
@@ -237,6 +268,9 @@ func fakeClaude(args []string) {
if v := os.Getenv("FAKE_BYPASS"); v != "" {
fakeBypass(r, v == "deaf")
}
if v := os.Getenv("FAKE_CHROME"); v != "" {
fakeChrome(r, v)
}
// what it was started with, for the test to read
_ = os.WriteFile(filepath.Join(os.Getenv("CLAUDE_CONFIG_DIR"), "args"), []byte(strings.Join(args, "\n")), 0o600)
// FAKE_TAP: Bun loads the tap BUN_OPTIONS names, which says so, and
@@ -766,6 +800,10 @@ func TestShimEndsAtItsCallersDeadline(t *testing.T) {
// the disclaimer the spawned claude sat on for 45s, then died in a
// loop of them (#5042): said, never "showed no prompt"
{"bypass disclaimer unanswered", `{"type":"user","message":{"role":"user","content":"hello"}}`, "asked to accept running in Bypass Permissions mode, not answered in the", "Bypass Permissions mode", []string{"FAKE_BYPASS=deaf", BypassEnv + "=1"}},
// the Claude in Chrome offer taking no key: said, with what the
// screen shows. Its Esc cleared the screen, and the error ended
// on nothing (sowhynot)
{"chrome offer unanswered", `{"type":"user","message":{"role":"user","content":"hello"}}`, "asked whether to use Claude in Chrome, not answered in the", "No, keep browser tools off", []string{"FAKE_CHROME=deaf"}},
// FAKE_USAGE names no panel: nothing is drawn for /usage
{"no /usage panel", `{"type":"command","command":"/usage"}`, "hadn't drawn /usage's panel in the", "", []string{"FAKE_USAGE=" + filepath.Join(t.TempDir(), "none")}},
// the banner drawn again and no panel after it: never told as one
@@ -791,6 +829,25 @@ func TestShimEndsAtItsCallersDeadline(t *testing.T) {
}
}
// Claude Code 2.1.319's Claude in Chrome offer turns away a key sent just
// after it opens and draws nothing again: the Esc sent the moment it was
// drawn was lost, and every run waited on it until its caller gave up,
// "showed no prompt" and nothing after it (sowhynot, a /usage read and
// the endpoint test alike). It is declined once it has stayed drawn, and
// declined again when the screen stays as it was.
func TestShimDeclinesChromeOffer(t *testing.T) {
home := t.TempDir()
for run := range 2 {
out, stderr, err := runEnds(t, home, []string{"FAKE_CHROME=1"})
if err != nil || !strings.Contains(out, "echo: hello") || strings.Contains(out, "print mode") {
t.Fatalf("run %d ended %v, wrote %q, stderr %q", run, err, out, stderr)
}
}
if _, err := os.Stat(filepath.Join(home, "claude", "chrome-declined")); err != nil {
t.Fatal("the offer wasn't declined")
}
}
func TestShimTrustsWhenTold(t *testing.T) {
out := runShim(t, []string{"FAKE_TRUST=1", TrustEnv + "=1"}, append([]string{fakePath(t)}, pArgs...), []string{"hello"}, 3)
if out[0]["subtype"] != "init" || out[1]["type"] != "assistant" {