fix(claude-local): join every row of a hard-wrapped setup-token URL (#22653)

## 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
> - When the CLI prints the link as plain text, the terminal hard-wraps
it over several rows. `reassembleUrl` joins the rows and validates the
result
> - It returns the first join that validates. `state` is the last query
key and needs only 16 characters, so a join that stops inside `state`
can validate one row early
> - The leftover row then sits where the prompt should be. Prompt
binding returns null, and the sign-in dialog hangs. No truncated URL
gets out, because the parser fails closed
> - This pull request uses the wrap shape: every row but the last has
the same width, so the parser joins until a row is shorter than the
first row
> - The benefit is that the plain-text path works at every terminal
width, not only at the widths where the break misses `state`

## Linked Issues or Issue Description

Refs #19949 (the plain-text path section). The fix follows the change
that @aron-intframe proposed and measured in the first comment on that
issue.

The login-method line defect from the same issue is in a separate pull
request, as the triage comment asks. Refs #20050 (PTY window size).

## What Changed

- `setup-token-parse.ts`: `reassembleUrl` no longer returns at the first
join that validates. If the first row validates on its own, the URL is
unwrapped, and the parser stops there as before. Otherwise it joins
until a row is shorter than the first row, and it returns the last join
that validated.
- Tests: the 64-column `claude.com` wrap from #19949, a sweep of every
width from 40 to 200 columns for both accepted URLs, and a test that a
two-row wrap with a valid first row still fails closed.

## Verification

- `pnpm exec vitest run packages/adapters/claude-local/src/server/`: 391
passed, 2 skipped.
- The 64-column test and the width sweep fail on `master` and pass with
this change.
- `pnpm --filter @paperclipai/adapter-claude-local typecheck` passes.
- With a 385-character URL like the one in #19949, `master` fails at 49
of 161 widths from 40 to 200. This branch passes at all 161.

## Risks

- Low risk. Each existing test passes, including the code-like value
after the URL, the repeated OSC 8 rows, and the hostile line between
fragments.
- One gap stays: a two-row wrap whose first row already holds 16 or more
`state` characters. One row gives no width to compare, so the parser
fails closed as it does today. A 200-column PTY (#20050) avoids this for
current URL lengths.

## 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:21 -08:00
1 parent 2181df544c
commit c294f21a34
2 files changed
+40 -6

No files matched your search

@@ -122,6 +122,36 @@ describe("parseSetupTokenPrompt", () => {
expect(result?.prompt).toBe(SETUP_TOKEN_PROMPT);
});
it("keeps joining a hard-wrapped URL past a full-width row that already validates", () => {
const wrapped = hardWrap(CLAUDE_COM_URL, 64).split("\n");
const lastRow = wrapped[wrapped.length - 1];
expect(STATE.length - lastRow.length).toBeGreaterThanOrEqual(16);
const text = [
PREAMBLE_LINE,
...wrapped,
"Hold Shift while selecting to use your terminal's native copy",
PROMPT_LINE,
].join("\n");
const result = parseSetupTokenPrompt(text);
expect(result?.url).toBe(CLAUDE_COM_URL);
expect(result?.prompt).toBe(SETUP_TOKEN_PROMPT);
});
it("returns the full URL for every hard-wrap width from 40 to 200 columns", () => {
for (const url of [CLAUDE_AI_URL, CLAUDE_COM_URL]) {
for (let width = 40; width <= 200; width += 1) {
const text = [PREAMBLE_LINE, hardWrap(url, width), PROMPT_LINE].join("\n");
expect(parseSetupTokenPrompt(text)?.url, `${url.slice(0, 20)} at ${width}`).toBe(url);
}
}
});
it("returns null, not a truncated URL, when a two-row wrap leaves the first row valid", () => {
const width = CLAUDE_COM_URL.length - 8;
const text = [PREAMBLE_LINE, hardWrap(CLAUDE_COM_URL, width), PROMPT_LINE].join("\n");
expect(parseSetupTokenPrompt(text)).toBeNull();
});
it("returns the URL when the output arrives split across chunks", () => {
// The streaming caller accumulates chunks and re-parses the whole buffer. The
// chunk boundary falls inside the URL token with no line break, so the joined
@@ -360,22 +360,26 @@ function validateAuthUrl(candidate: string): string | null {
* The parser starts at `startLine`, then joins at most {@link MAX_URL_LINES}
* consecutive URL-character-only physical lines into at most {@link
* MAX_URL_LENGTH} characters. It validates the joined string after each join and
* returns the first joined string that passes {@link validateAuthUrl}. A CLI that
* emits the whole URL on one line passes on the first join. A CLI that wraps the
* URL passes once the parser joins the last fragment. Returns null when no bounded
* join validates.
* returns the last joined string that passes {@link validateAuthUrl} before a
* row shorter than the first row ends the wrap. A CLI that emits the whole URL
* on one line passes on the first join. A CLI that wraps the URL passes once the
* parser joins the last fragment. Returns null when no bounded join validates.
*/
function reassembleUrl(lines: string[], startLine: number): SetupTokenUrlMatch | null {
let joined = "";
let firstWidth = 0;
let match: SetupTokenUrlMatch | null = null;
for (let offset = 0; offset < MAX_URL_LINES && startLine + offset < lines.length; offset += 1) {
const trimmed = lines[startLine + offset].trim();
if (trimmed.length === 0 || !URL_CHARS_ONLY_RE.test(trimmed)) break;
if (offset === 0) firstWidth = trimmed.length;
joined += trimmed;
if (joined.length > MAX_URL_LENGTH) break;
const url = validateAuthUrl(joined);
if (url) return { url, endLine: startLine + offset };
if (url) match = { url, endLine: startLine + offset };
if (offset === 0 ? match !== null : trimmed.length < firstWidth) break;
}
return null;
return match;
}
/**