fix(claude-local): skip the setup-token login-method line (#22652)

## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - People connect a Claude subscription from "Connect a model".
Paperclip runs `claude setup-token` in a terminal and reads the sign-in
link from the output
> - `parseSetupTokenPrompt` binds the link only when the browser-code
prompt follows the URL block. It skips repeated URL rows and the copy
hint, and it fails closed on any other line
> - Claude Code 2.1.306 prints a new line, `Login method pre-selected:
Subscription Plan (Claude Pro/Max)`, between the copy hint and the
prompt
> - The parser returns null for that layout, and the runner waits for
the 5-minute timeout. The dialog stays on "Preparing browser sign-in…",
and the log says only "timed out"
> - This pull request skips the login-method line with a bounded pattern
and logs one fixed line when the prompt is on screen but the parser does
not bind it
> - The benefit is that Claude subscription sign-in works on Claude Code
2.1.306, and the next layout change shows up in the log at once

## Linked Issues or Issue Description

Fixes #19949

Refs #19958 (the same fix by @adity982, written before #19211 changed
the copy-hint check; this pull request ports it onto `master` and widens
the pattern to other method names). Refs #19211, #15550 (earlier
copy-hint fixes). Refs #20050 (PTY window size, a separate fix). Refs
#22520 (the 2.1.330 short link, a separate fix in the URL validator).

The plain-text URL reassembly defect from the same issue is in a
separate pull request, as the triage comment asks.

## What Changed

- `setup-token-parse.ts`: add `LOGIN_METHOD_LINE_RE`.
`findBrowserCodePrompt` skips a line that matches it. The method name is
1 to 80 characters of prose with no `:`, so a URL or a `code:value`
cannot hide in it. Any other unknown line still returns null.
- `setup-token-parse.ts`: add `hasSetupTokenPromptLine`. It returns true
when the output holds the prompt line. It binds no value.
- `setup-token-runner.ts`: when the prompt line is on screen and
`parseSetupTokenPrompt` still returns null, log one fixed line:
`Setup-token login: the browser-code prompt is on screen, but the parser
did not recognize the sign-in screen.` The line holds no input bytes,
and the runner logs it at most one time per run.
- Tests: the 2.1.306 layout (OSC 8 link per row, CHA-spaced words, copy
hint, login-method line, prompt), the login-method line without a copy
hint, four malformed login-method lines that must fail closed, and two
runner tests for the new log line.

## Verification

- `pnpm exec vitest run packages/adapters/claude-local/src/server/`: 396
passed, 2 skipped.
- The two positive parser tests fail on `master` and pass with this
change.
- `pnpm --filter @paperclipai/adapter-claude-local typecheck` passes.
- I ran the parser on a synthetic 2.1.306 screen built from the output
in #19949. `master` returns null. This branch returns the full URL.
- Not run: a live `claude setup-token` sign-in on 2.1.306.

## Risks

- Low risk. The change adds one allowlisted line shape. The parser keeps
its fail-closed contract for every other line.
- A future CLI can print another new line. The parser then still returns
null, but the new log line names the cause.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, with shell, file
edit, and code execution tools.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge
This commit is contained in:
Devin Foley authored and GitHub committed 2026-11-23 11:52:16 -08:00
1 parent d28becf0f5
commit 2181df544c
4 files changed
+86 -2

No files matched your search

@@ -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");
@@ -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;
@@ -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();
@@ -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);
}