diff --git a/packages/adapters/claude-local/src/server/setup-token-parse.test.ts b/packages/adapters/claude-local/src/server/setup-token-parse.test.ts index 2439e09d379..4205453ae4e 100644 --- a/packages/adapters/claude-local/src/server/setup-token-parse.test.ts +++ b/packages/adapters/claude-local/src/server/setup-token-parse.test.ts @@ -366,6 +366,38 @@ describe("parseSetupTokenPrompt", () => { expect(result?.prompt).toBe(SETUP_TOKEN_PROMPT); }); + it("skips the 2.1.306 login-method line after the copy hint", () => { + const rows = hardWrap(CLAUDE_COM_URL, 78) + .split("\n") + .map((row) => osc8Hyperlink(CLAUDE_COM_URL, row)); + const hint = chaSpaced("Hold Shift while selecting to use your terminal's native copy".split(" ")); + const method = chaSpaced("Login method pre-selected: Subscription Plan (Claude Pro/Max)".split(" ")); + const text = [PREAMBLE_LINE, ...rows, hint, method, PROMPT_LINE].join("\n"); + const result = parseSetupTokenPrompt(text); + expect(result?.url).toBe(CLAUDE_COM_URL); + expect(result?.prompt).toBe(SETUP_TOKEN_PROMPT); + }); + + it("skips the login-method line when no copy hint precedes it", () => { + const text = [ + PREAMBLE_LINE, + CLAUDE_COM_URL, + "Login method pre-selected: Claude account with subscription", + PROMPT_LINE, + ].join("\n"); + expect(parseSetupTokenPrompt(text)?.url).toBe(CLAUDE_COM_URL); + }); + + it.each([ + "Login method pre-selected: https://claude.com.example/x", + "Login method pre-selected: ABCD-EFGHJ:code", + "Login method pre-selected:", + "Login method selected: Subscription Plan", + ])("returns null for a malformed login-method line: %s", (line) => { + const text = [PREAMBLE_LINE, CLAUDE_COM_URL, line, PROMPT_LINE].join("\n"); + expect(parseSetupTokenPrompt(text)).toBeNull(); + }); + it("returns null when a copy hint names an unknown key", () => { const hint = "Hold Ctrl while selecting to use your terminal's native copy"; const text = [PREAMBLE_LINE, CLAUDE_COM_URL, hint, PROMPT_LINE].join("\n"); diff --git a/packages/adapters/claude-local/src/server/setup-token-parse.ts b/packages/adapters/claude-local/src/server/setup-token-parse.ts index bd538584522..d4f704d67aa 100644 --- a/packages/adapters/claude-local/src/server/setup-token-parse.ts +++ b/packages/adapters/claude-local/src/server/setup-token-parse.ts @@ -188,6 +188,8 @@ const COPY_HINT_LINE_RE = /^Hold\s+(?:Shift|Option|Fn|Shift\s+\(Option\s+in\s+iT const MAX_COPY_HINT_LINES = 3; +const LOGIN_METHOD_LINE_RE = /^Login\s+method\s+pre-selected:\s+[A-Za-z][A-Za-z0-9 ()/+.,-]{0,79}$/; + // The maximum number of characters between the end of the URL preamble line and // the start of the URL. The Claude UI prints the URL right after the preamble. // The parser accepts a URL start only inside this window, so a URL far from the @@ -229,6 +231,12 @@ export function hasSetupTokenOAuthError(text: string): boolean { return /OAuth\s+error\s*:/.test(stripTerminalControls(text)); } +export function hasSetupTokenPromptLine(text: string): boolean { + return stripTerminalControls(text) + .split("\n") + .some((line) => PROMPT_LINE_RE.test(line.trim())); +} + /** * Returns true when `value` is a valid `scope` value: one to eight * space-separated tokens, a total length of at most {@link MAX_SCOPE_LENGTH}, @@ -378,8 +386,10 @@ function reassembleUrl(lines: string[], startLine: number): SetupTokenUrlMatch | * parser skips a line that repeats `url`. The `claude` 2.1.291 UI also prints a * copy hint before the prompt, so the parser skips the up to {@link * MAX_COPY_HINT_LINES} non-blank lines that together match {@link - * COPY_HINT_LINE_RE}. The parser then binds the prompt on the first non-blank - * line that is not a repeat and not the copy hint. That line must match the + * COPY_HINT_LINE_RE}. The `claude` 2.1.306 UI also prints a login-method status + * line, so the parser skips a line that matches {@link LOGIN_METHOD_LINE_RE}. + * The parser then binds the prompt on the first non-blank line that is not a + * repeat, the copy hint, or the login-method line. That line must match the * exact prompt shape. Returns null when that line is not the prompt. */ function findBrowserCodePrompt(lines: string[], endLine: number, url: string): string | null { @@ -390,6 +400,7 @@ function findBrowserCodePrompt(lines: string[], endLine: number, url: string): s // punctuation first, the same way the repeated-URL comparison needs, so a // repeated URL with a trailing punctuation mark still matches. if (trimmed.replace(TRAILING_PUNCTUATION_RE, "") === url) continue; + if (LOGIN_METHOD_LINE_RE.test(trimmed)) continue; const hintEnd = copyHintEndLine(lines, i); if (hintEnd !== null) { i = hintEnd; diff --git a/packages/adapters/claude-local/src/server/setup-token-runner.test.ts b/packages/adapters/claude-local/src/server/setup-token-runner.test.ts index 8482efac1f6..4d2b953fe20 100644 --- a/packages/adapters/claude-local/src/server/setup-token-runner.test.ts +++ b/packages/adapters/claude-local/src/server/setup-token-runner.test.ts @@ -191,6 +191,41 @@ describe("runSetupTokenLogin", () => { expect(fake.disposes.count).toBe(1); }); + it("logs one fixed line when the prompt is on screen but the parser does not bind it", async () => { + const unrecognized = PROMPT_OUTPUT.replace(VALID_URL, `${VALID_URL}\nan unknown status line`); + const fake = createFakeDriver({ chunks: [unrecognized, unrecognized] }); + const logs: string[] = []; + const onPrompt = vi.fn(); + const result = await runSetupTokenLogin(fake.driver, { + onPrompt, + provideCode: async () => BROWSER_CODE, + timeoutMs: 20, + log: (line) => { + logs.push(line); + }, + }); + expect(result.outcome).toBe("timeout"); + expect(onPrompt).not.toHaveBeenCalled(); + const unrecognizedLogs = logs.filter((line) => line.includes("did not recognize the sign-in screen")); + expect(unrecognizedLogs).toHaveLength(1); + expect(logs.join("\n")).not.toContain(VALID_URL); + expect(logs.join("\n")).not.toContain("an unknown status line"); + }); + + it("does not log the unrecognized-screen line before the prompt appears", async () => { + const fake = createFakeDriver(); + const logs: string[] = []; + await runSetupTokenLogin(fake.driver, { + onPrompt: () => {}, + provideCode: async () => BROWSER_CODE, + timeoutMs: 20, + log: (line) => { + logs.push(line); + }, + }); + expect(logs.some((line) => line.includes("did not recognize the sign-in screen"))).toBe(false); + }); + it("stops the process on a cancellation", async () => { const fake = createFakeDriver(); const controller = new AbortController(); diff --git a/packages/adapters/claude-local/src/server/setup-token-runner.ts b/packages/adapters/claude-local/src/server/setup-token-runner.ts index f1d16644371..86d3c6cfccf 100644 --- a/packages/adapters/claude-local/src/server/setup-token-runner.ts +++ b/packages/adapters/claude-local/src/server/setup-token-runner.ts @@ -9,6 +9,7 @@ import { parseSetupTokenCredential, parseSetupTokenPrompt, hasSetupTokenOAuthError, + hasSetupTokenPromptLine, type SetupTokenPrompt, } from "./setup-token-parse.js"; @@ -218,6 +219,7 @@ export async function runSetupTokenLogin( } let promptSurfaced = false; + let unrecognizedPromptLogged = false; let codeSubmitted = false; let submitStarted = false; let oauthError = false; @@ -327,6 +329,10 @@ export async function runSetupTokenLogin( } return; } + if (!unrecognizedPromptLogged && hasSetupTokenPromptLine(buffer)) { + unrecognizedPromptLogged = true; + log("[paperclip] Setup-token login: the browser-code prompt is on screen, but the parser did not recognize the sign-in screen."); + } if (buffer.length > CLAUDE_SETUP_TOKEN_MAX_BUFFER_CHARS) { buffer = buffer.slice(buffer.length - CLAUDE_SETUP_TOKEN_MAX_BUFFER_CHARS); }