claudetui: Claude Code's first-run notes send the run to -p at once, not after 45s

A config folder whose onboarding isn't done (one only -p was run in,
given CLAUDE_CODE_OAUTH_TOKEN) has the interactive mode show "Security
notes: … Press Enter to continue". The helper knew the sign-in screen
but not this one, so it waited out ReadyWait (45s) before running -p.
Now firstRun is matched like signIn: the run is -p at once and the
start is marked broken. Agreeing to the notes is left to the user.

The match is on the screen's words with any space between them one
(strings.Fields): the real 2.1.316 screen didn't match a plain regexp,
since Claude Code draws some spaces by moving the cursor.

Run:
- mote: go test ./internal/claudetui -count=1 ok; go vet for darwin,
  windows, linux; gofmt clean.
- Broken by hand: with the case off, TestShimFallsBackToPrint/first-run
  notes goes red ("wrote 0 lines of 1 in 30s").
- Real, on xiaomi-gpu (Windows 11), Claude Code 2.1.316 (official dist,
  sha256 checked), a real subscription token, a fresh config folder:
  before, "showed no prompt in 45s" and -p at 48s; with the first regexp
  still 45s (the space case above); now "shows its first-run notes" and
  the -p answer at 7.9s and 16.9s (two runs), streamed.

Not tried: WSL with a fresh folder (same helper code path); macOS/Linux
native with a fresh folder.
This commit is contained in:
yetone
2026-11-01 01:43:35 +08:00
parent 6a900e0d04
commit 88a0262bf2
3 changed files with 19 additions and 1 deletions
@@ -270,7 +270,8 @@ interactive mode can't answer a control request to stop a turn.
If the interactive Claude Code doesn't start or show its prompt within 45
seconds (`claudetui.ReadyWait`), or asks how to sign in (`signIn`: a
Claude Code signed in for `-p` only, as in a container given
`CLAUDE_CODE_OAUTH_TOKEN`), the helper becomes `claude -p` with the same
`CLAUDE_CODE_OAUTH_TOKEN`), or shows its first-run notes (`firstRun`:
"Security notes", a config folder whose onboarding isn't done), the helper becomes `claude -p` with the same
arguments (`printMode`, `exec` on the same pid, its input still unread),
so the turn is answered all the same, and it writes
`<cache>/magpie/claude-tui-broken`, which holds the number of failed
+9
View File
@@ -319,6 +319,13 @@ var errUntrusted = errors.New("Claude Code asks whether to trust the folder, whi
// ~/.claude.json has no onboarding done): nothing the helper may answer.
var signIn = regexp.MustCompile(`Select login method|Log in with your Claude account|claude\.ai/oauth/authorize`)
// firstRun is the notes Claude Code's interactive mode shows where its
// onboarding isn't done (a config folder only -p was run in, given
// CLAUDE_CODE_OAUTH_TOKEN): agreeing to them is the user's, as is what
// comes after them. It is matched in the screen's words, any space
// between them one: Claude Code draws some by moving the cursor.
var firstRun = regexp.MustCompile(`Security notes:.*Press Enter to continue`)
// ready waits for Claude Code's prompt. Asked whether to trust the folder
// it runs in, it is told yes, as a person would, when TrustEnv says so.
func (s *shim) ready() error {
@@ -332,6 +339,8 @@ func (s *shim) ready() error {
return errUntrusted
case signIn.MatchString(p):
return fmt.Errorf("Claude Code's interactive mode asks to sign in: %s", s.lastScreen())
case firstRun.MatchString(strings.Join(strings.Fields(p), " ")):
return fmt.Errorf("Claude Code's interactive mode shows its first-run notes, its onboarding not done: %s", s.lastScreen())
case strings.Contains(p, "trust this folder") && 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
+8
View File
@@ -98,6 +98,13 @@ func fakeClaude(args []string) {
time.Sleep(time.Minute)
os.Exit(0)
}
if os.Getenv("FAKE_NOTES") == "1" {
// onboarding not done: the first-run notes, as 2.1.316 draws them,
// a space a move of the cursor or a no-break space
fmt.Print("\r\n Security\x1b[1Cnotes:\r\n\r\n 1. Claude can make mistakes\r\n You should always review Claude's responses\r\n\r\n 2. Due to prompt injection risks, only use it with code you trust\r\n\r\n Press\u00a0Enter\x1b[1Cto\x1b[1Ccontinue…\r\n")
time.Sleep(time.Minute)
os.Exit(0)
}
sid := ""
if i := slices.Index(args, "--session-id"); i >= 0 {
sid = args[i+1]
@@ -401,6 +408,7 @@ func TestShimFallsBackToPrint(t *testing.T) {
{"untrusted folder", []string{"FAKE_TRUST=1"}, false},
// at once, not after readyWait: runShim waits 30s (#4504)
{"asks to sign in", []string{"FAKE_LOGIN=1"}, true},
{"first-run notes", []string{"FAKE_NOTES=1"}, true},
} {
t.Run(c.name, func(t *testing.T) {
out := runShim(t, c.env, append([]string{fakePath(t)}, pArgs...), []string{"hello"}, 1)