diff --git a/docs/subsystems/claude-subscription-bridge.md b/docs/subsystems/claude-subscription-bridge.md index 20c7c1877..0b5bf7876 100644 --- a/docs/subsystems/claude-subscription-bridge.md +++ b/docs/subsystems/claude-subscription-bridge.md @@ -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 `/magpie/claude-tui-broken`, which holds the number of failed diff --git a/internal/claudetui/tui.go b/internal/claudetui/tui.go index 59ccd7402..616759605 100644 --- a/internal/claudetui/tui.go +++ b/internal/claudetui/tui.go @@ -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 diff --git a/internal/claudetui/tui_test.go b/internal/claudetui/tui_test.go index 63451aaa1..8e98c21b6 100644 --- a/internal/claudetui/tui_test.go +++ b/internal/claudetui/tui_test.go @@ -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)