Skip to content

patchy login end to end: the handoff, the confirm page, the token #131

Closed

Description

@allisonmahmood

Question

Build patchy login on a throwaway branch and drive it the way an agent will, so the shape of the handoff can be felt rather than argued. Blocked on How agent-driven CLIs hand off a device login and settles Q1 and Q2 of Device login: the protocol, and the machine token's columns.

What the prototype must let us try:

  1. The handoff from inside an agent. Run patchy login --json through an agent's shell tool. Does the agent get the URL and code to the human before the process exits? Try the shapes the research surfaced (two-step on one command; blocking in a terminal and two-step under --json; blocking with the handoff on stderr) and record what each felt like from both seats.
  2. The URL. /login/device?code=XXXX-XXXX with the user code as the lookup key, behind the door from the door prototype (prototype/login-door), with the company and person named from the session and the machine name prefilled from the CLI's hostname. Does the page read as "a machine is being authorised" and nothing else? Does a bare /login/device need to exist?
  3. Deny, expire, and the second run. Press deny; wait out the ten minutes; run patchy login again with a login pending, and again after it completed.

Resolution: which CLI shape, what the URL carries, and the copy of the confirm page, with the branch linked as the asset. The branch is thrown away; the spec carries the decision.

From the research (How agent-driven CLIs hand off a device login, resolved 2026-09-04)

  • The two shapes to try in item 1 are now concrete: Stripe's, which blocks for a human at a TTY and otherwise (--json, non-TTY, or an agent detected from CLAUDECODE, CURSOR_AGENT, CODEX_* and kin) writes one JSON document and exits 0, with patchy login --complete <code> polling on the second call; and the always two-step shape from round one on Device login: the protocol, and the machine token's columns #122, where patchy login starts or resumes on the same command with no mode switch. Both print one JSON document per run naming the next command (Netlify's agent_next_steps). Try each from an agent's shell tool and from a terminal.
  • Print the bare code beside the URL even though the URL carries it (Wrangler, Turborepo and Railway do, citing RFC 8628 §3.3.1 and the agent transcript), and persist the device code, interval and expiry to the state dir as Stripe does, so the second call has what it needs.
  • Supabase's confirm page is the only open-source copy in the set; the research comment quotes it as the floor for item 2.

Activity

  1. added
    wayfinder:prototype
    on Sep 3, 2026
  2. allisonmahmood commented on Sep 3, 2026

    @allisonmahmood
    OwnerAuthor

    Prototype built; the agent seat is done, the human seat is yours

    Asset: prototype/patchy-login (one commit on top of prototype/login-door). This worktree's dev instance is up at http://127.0.0.1:35558 with the Clerk dev keys; pnpm dev in any checkout of the branch gives you your own. The ticket stays open: the shape and the copy are picks, not findings.

    What is on the branch

    • Wire. POST /api/login/device starts a login (device code, user code, the confirm URL with and without the code, interval, expiry); POST /api/login/device/token polls it (pending / slow_down, complete with the token exactly once, or a 410 saying expired, denied or unknown). Both are admitted by the guard beside the mint and start is per-address limited like the mint. In docs/API.md.
    • Rows. prototype_device_logins (migration 0091). Confirm mints an upload token on the user's company account with the name the person typed, revokes the previous key when it is the same account's (Q4's rule), and the poll that reports the outcome deletes the row.
    • The confirm page behind the door at /login/device?code=XXXX-XXXX, in three copy variants switched with ?v=a|b|c and a bar at the bottom: A leads with the code (Vercel's shape), B with the machine and a receipt table, C with a three-line checklist. Undoored with sample data at /prototype/login/device?v=a (&relogin=1 adds the re-login line, &again=1 the item-9 notice). Reverse states: denied, expired (410), already answered (410), unknown code (404), bare /login/device.
    • The CLI. patchy login [--complete [code]] [--wait <s>] [--block], PATCHY_LOGIN_SHAPE=stripe (default) or two-step. A login credential outranks the dev env's seeded token so whoami shows it inside a worktree.
    • scripts/prototype/patchy-login.mjs <deviceUrl> confirm|deny|look [--v b] [--machine NAME] [--delay 70] [--extras] drives the page headlessly as a +clerk_test user and screenshots every variant.

    Item 1, the handoff, from the agent seat

    I ran every shape through my own shell tool (Claude Code; CLAUDECODE is set, stdin is not a terminal).

    Stripe's shape, --json: one document in 1.4 s, exit 0:

    {"ok":true,"status":"awaiting_confirmation","verificationUrl":"http://127.0.0.1:35558/login/device?code=HLSS-RDRG","verificationUrlBare":"http://127.0.0.1:35558/login/device","userCode":"HLSS-RDRG","expiresAt":"2026-09-03T22:32:28.782Z","interval":5,"next":"patchy login --complete HLSS-RDRG","agentNextSteps":"Show the person the URL and the code and ask them to confirm it in a browser where they are signed in. Do not open a browser yourself. Then run the next command; it waits up to a minute and says pending if they have not confirmed yet.","notWaitingBecause":"--json"}

    Stripe's shape, no flag: the agent gate fired on its own (CLAUDECODE is set), same handoff as text lines plus Then run: patchy login --complete HLSS-RDRG. Then --complete --wait 0 answered pending with the same next; after the browser confirmed, --complete answered logged_in with the machine { id, name } and the credentials path, and whoami printed API token: allison-laptop (tok_…).

    Blocking (--block, and the human path under a pty with the agent variables unset): the CLI wrote the handoff at +2 s and sat in Waiting...; my tool call returned at +17 s with everything at once. Same with --json --block, where the handoff rides on stderr and stdout stays empty until the document. From this seat the stream is irrelevant: nothing reaches the agent before exit. Item 1's "blocking with the handoff on stderr" is dead on arrival for an agent, and only the two non-blocking shapes work.

    Always two-step: patchy login starts, patchy login again resumes (Not confirmed yet. The code QMCP-DMVW is good for 10 more minutes. Then run: patchy login). No agent detection needed at all.

    What told the two apart in practice:

    • A second patchy login while one is pending starts a fresh code under Stripe's shape and orphans the old one; under two-step it resumes. The orphan matters: the old page still confirms, and a token minted for a code nobody polls sits in the row until the sweep an hour after expiry.
    • --complete <code> with the wrong code is a local refusal naming the live one (has code BFHT-VHMW, not MWQH-TBFW), a cheap check that the agent and the person are talking about the same login.

    Item 2, the URL

    /login/device?code=… survives the door, the Account Portal and the handshake: 4.9–5.1 s and one click from the pasted link to the page, signed out. The page names the company and the person from the session, prefills the machine name from the CLI's hostname (t1e here) or, on a re-login, the old key's name with Replaces the key named allison-laptop, which stops working when you confirm. The bare /login/device is a message page: Open the link your terminal printed. patchy login prints a link that carries its own code, so there is nothing to type here.

    Item 3, the reverse states

    • Deny (from variant B): Nothing was logged in. The code MLCL-ZTWL is dead and no key was made. Then --complete: The login was denied in the browser. Nothing was saved. Run: patchy login, exit 2; the previous key is untouched.
    • Expired (waited the ten minutes): the page says This code has expired. A login code lasts ten minutes. Run patchy login again…. The CLI refused from its own record without polling (The pending login for … expired. Run: patchy login, exit 1 local), then No login is pending… on the next call. So the server's expired answer is only ever reached when the CLI's clock is behind, and the row waits for the sweep.
    • Again after it completed: the start sends the old key's id; the page prefills its name and shows the replaces line; confirm revokes it in the same transaction.
    • Device login: the protocol, and the machine token's columns #122 item 9 (Confirm pressed 70 s after the page loaded): 303 POST /login/device → 200 GET /login/device?code=… through a refresh handshake, nothing minted, and the person is back on the same page. I added a notice for that return: Your sign-in was refreshed before that went through. Check the code and press Confirm again.

    Screenshots of every state are under .local/prototype/ in this worktree (r1-confirm-a|b|c, r1-bare, r1-unknown, r2-after-deny, r7-after-confirm).

    What only you can settle

    1. The CLI shape. My recommendation: Stripe's gate (block for a human at a TTY, one document otherwise) plus two-step's resume: patchy login with a live login pending polls it instead of minting a new code, and --complete stays as the explicit, checkable form. That collapses the two shapes to one difference, whether a human waits in-process.
    2. The copy. Flip A/B/C on a real code, or on /prototype/login/device. The ticket wants the chosen page's text in the spec.
    3. The bare page: keep it as the message above, or 404 it.
    4. Expired from the CLI: local (it knew from its own record, no network) or rejected (make it poll so the server says so and deletes the row).

    Run pnpm patchy login in a real terminal for the blocking feel, then from your own Claude Code for the agent one. Prototype gaps, not questions: the token sits on the company account (the branch has no user-owned tokens), no last_used_at/expires_at, no Your machines, no patchy logout.

  3. allisonmahmood commented on Sep 3, 2026

    @allisonmahmood
    OwnerAuthor

    Resolution (2026-09-04)

    Allison drove the prototype from the human seat and picked; the agent seat's record is the comment above. The asset is prototype/patchy-login, kept as PR #132, closed unmerged on purpose. The branch is thrown away; this comment and #122 carry the decision.

    The CLI shape: Stripe's gate, plus resume

    • patchy login blocks only for a human at a TTY: stdin is a terminal, no --json, and none of the agent variables is set (CLAUDECODE, CLAUDE_CODE_ENTRYPOINT, CURSOR_AGENT, CODEX_SANDBOX, CODEX_SANDBOX_NETWORK_DISABLED, GEMINI_CLI, OPENCODE, CLINE_ACTIVE, AI_AGENT, CI; the spec owns the list). It prints the handoff, waits until the code expires or the person answers, and saves the key. isatty alone is the wrong gate: an agent's shell tool has a TTY and still sees nothing before exit.
    • Otherwise it writes one JSON document to stdout and exits 0: the confirm URL with the code, the bare URL, the code, expiresAt, interval, next (the command to run) and agentNextSteps (show the person the URL and code, never open a browser, then run next). Text mode prints the same as lines with Then run: … and one line saying why it did not wait.
    • A second patchy login while a live login is pending resumes it rather than minting a new code, so an agent that reruns the command never orphans a code the page can still confirm. patchy login --complete [code] is the explicit form: it refuses a code that is not the pending one (local, naming the live one), polls at the interval (plus five on slow_down), and after --wait seconds (default 60) answers { status: "pending", next, agentNextSteps }, exit 0. On complete it saves the token with source: "login" and the machine { id, name }, forgets the pending record, and prints Logged in to <instance> as <company>. This machine is "<name>".
    • Denied, expired and unknown are the server's answers, exit 2 rejected. The CLI polls even when its own record says the code has expired, so the server reports it and deletes the row (Allison's pick over a local refusal). Copy: The login was denied in the browser. Nothing was saved. Run: patchy login / The login expired before it was confirmed (codes last ten minutes). Run: patchy login / No login is pending for code XXXX-XXXX on <instance>; it may already have been reported. Run: patchy login.
    • The device code, user code, both URLs, interval and expiry persist in the state dir (device-login.json, owner-only, one entry per instance) so the second call has what the poll needs. The start request carries os.hostname() as the machine-name hint and, on a re-login, the stored key's id. A login credential outranks the dev env's seeded token.

    The URL and the page

    • /login/device?code=XXXX-XXXX, the user code as the lookup key, behind the door; the code survives the door, the Account Portal and the handshake (one click, ~5 s from the pasted link). The page confirms the code, never asks for it. /login/device with no code is a message page and nothing more: Open the link your terminal printed. patchy login prints a link that carries its own code, so there is nothing to type here. If someone sent you here to type a code, don't: that is the trick the link is designed to avoid.
    • Copy: variant A, the code first. Kicker Device login. Lede Is this the code on your terminal?, then the code in a large mono headline. Body: A terminal just ran patchy login and wants to publish at as (<email>). If the code matches, name the machine and confirm. If you didn't run it, deny: nothing happens. Field Machine name, prefilled from the CLI's hostname, or on a re-login from the old key's name with Replaces the key named <name>, which stops working when you confirm. Buttons Confirm and Deny. Foot: The code expires in N minutes. The key it makes works for 90 days, or 30 days unused, and can be revoked any time on Your machines.
    • After confirm: <machine> is logged in. It can publish and manage patches at as <email> until you revoke it on Your machines. Your terminal already knows; you can close this tab. After deny: Nothing was logged in. The code XXXX-XXXX is dead and no key was made. If you didn't run patchy login, there is nothing else to do. Expired (410): This code has expired. A login code lasts ten minutes. Run patchy login again on the machine and open the new link it prints. Already answered (410): This code was already used. Unknown (404): Nothing is waiting for this code. Check the link you opened, or run patchy login again…
    • Device login: the protocol, and the machine token's columns #122 item 9 confirmed in place: a Confirm on a stale session is a 303 to the GET with the code, the door refreshes the session, and the page comes back with Your sign-in was refreshed before that went through. Check the code and press Confirm again. Nothing is minted on that round trip.
    • A blank or over-long name re-renders the page with Give the machine a name, up to 64 characters. (422).

    Facts for the spec

    Handoff round trip 1.4 s; sign-in plus door to the page 4.9–5.1 s and one click; the confirm page's own hops are one read. Confirm mints the token, revokes the old key when it is the same user's, and stores the plaintext until the poll that reports it deletes the row (the prototype's shortcut; the spec may pick a better hand-off than a plaintext column, but "the row leaves with the poll that reports it" stands). The prototype's tokens sit on the company account; the real machine token is the user's, per #122.

  4. allisonmahmood commented on Sep 3, 2026

    @allisonmahmood
    OwnerAuthor

    Resolved above; the branch and PR #132 are the asset.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

wayfinder:prototype

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions