Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(login): handle auth failure + guard double /login
A rejected device-code poll (e.g. incorrect_device_code) previously
escaped as an unhandled rejection and killed the process. Surface it as
an error card with a clean message (strip the Error: prefix), and guard
against a double Enter starting two concurrent device-code flows — the
second code supersedes the first, so polling the first 401s.

Co-Authored-By: Claude <noreply@anthropic.com>
  • Loading branch information
wangcansunking and claude committed Jun 25, 2026
commit 6382fbece7a163b9621f0f418f4c4e4388c67deb
4 changes: 3 additions & 1 deletion e2e/RESULTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ and update this file (paste the summary).
blocking token poll, so the Repl showed nothing and the user could never authorize. Split device
login into `beginDeviceLogin` (returns the code immediately) + `complete()` (polls), and gave the
TUI a non-blocking `/login` branch that renders the verification URL + code first, then a
completion card. Full suite green: `npm test` → **238 passed** (47 files), `npm run test:e2e` →
completion card. Also hardened the failure path: auth errors (e.g. `incorrect_device_code`) render
a clean error card instead of crashing the process, and a double Enter no longer starts two
device-code flows. Full suite green: `npm test` → **240 passed** (47 files), `npm run test:e2e` →
**31 passed** (4 files), tsc build clean.

- **2026-06-23** — Added `ToolCallExtractor` (recovers text-emitted `<function_calls>`/`<invoke>`
Expand Down
9 changes: 7 additions & 2 deletions src/tui/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export function App({
const [screen, setScreen] = useState<Screen>(pickModelOnStart && loadModels ? { kind: "model" } : null);
const [, setNow] = useState(0); // ticks the live loading line while the assistant streams
const abortRef = useRef<AbortController | null>(null); // current turn's interrupt handle
const loginInFlight = useRef(false); // guards against starting a second device-login flow
const add = (e: Entry) => setEntries((p) => [...p, e].slice(-100));
const refreshStatus = () => { if (readStatus) setStatus(readStatus()); };

Expand Down Expand Up @@ -157,10 +158,14 @@ export function App({
if (t === "/login" && login) {
// Show the verification URL + code right away, then resolve a completion card once the user
// authorizes. Done as a special case (not a registry command) because the slash registry only
// renders a command's final return value — it can't surface the code mid-poll.
// renders a command's final return value — it can't surface the code mid-poll. Guarded so a
// double Enter doesn't start two device-code flows (polling a superseded code 401s).
if (loginInFlight.current) { add({ type: "card", title: "/login", tone: "info", lines: ["already waiting for authorization…"] }); return; }
loginInFlight.current = true;
void login((lines) => add({ type: "card", title: "/login", tone: "info", lines }))
.then((lines) => add({ type: "card", title: "/login", tone: "ok", lines }))
.catch((e) => add({ type: "card", title: "/login", tone: "error", lines: [`login failed: ${String(e)}`] }));
.catch((e) => add({ type: "card", title: "/login", tone: "error", lines: [`login failed: ${e instanceof Error ? e.message : String(e)}`] }))
.finally(() => { loginInFlight.current = false; });
return;
}
if (setup && loadModels && (t === "/setup-claude" || t === "/setup-codex")) {
Expand Down
33 changes: 33 additions & 0 deletions tests/tui/app-interaction.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,39 @@ describe("TUI: /login surfaces the device code before the poll resolves", () =>
await tick(80);
expect(lastFrame()).toContain("GitHub authorization complete.");
});

it("renders an error card (not a crash) when authorization fails", async () => {
// A rejected poll (e.g. expired/incorrect device code) must surface as an error card. The old
// path let the rejection escape as an unhandled rejection and killed the whole process.
const login = (show: (lines: string[]) => void) => {
show(["Open https://github.com/login/device and enter code: AB-12"]);
return Promise.reject(new Error("authorization failed: incorrect_device_code"));
};
const { stdin, lastFrame } = render(<App registry={reg()} title="m" login={login} />);
await tick();
stdin.write("/login");
await tick();
stdin.write("\r");
await tick(80);
const f = lastFrame() ?? "";
expect(f).toContain("login failed");
expect(f).toContain("incorrect_device_code");
// The raw "Error:" prefix should be stripped — show a clean message.
expect(f).not.toContain("Error:");
});

it("ignores a second /login while one is already in flight", async () => {
// The user can hit Enter on /login twice; without a guard that starts two device-code flows,
// and polling a superseded code fails with incorrect_device_code. Only one flow should start.
let starts = 0;
const gate = new Promise<string[]>(() => {}); // never resolves — login stays pending
const login = (show: (lines: string[]) => void) => { starts++; show([`code ${starts}`]); return gate; };
const { stdin } = render(<App registry={reg()} title="m" login={login} />);
await tick();
stdin.write("/login"); await tick(); stdin.write("\r"); await tick(60);
stdin.write("/login"); await tick(); stdin.write("\r"); await tick(60);
expect(starts).toBe(1);
});
});

describe("TUI: model picker", () => {
Expand Down
Loading