The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
fix(cli): report expired QR sign-in approvals (#1515)
Danny Ward3fa934eecb07
diff --git a/cli/src/authCommand.spec.ts b/cli/src/authCommand.spec.tsindex fcaa9dafe..539730a45 100644--- a/cli/src/authCommand.spec.ts+++ b/cli/src/authCommand.spec.ts@@ -498,6 +498,25 @@ describe('harness login --json', () => {expect(calls.map((c) => c.url)).not.toContain('/api/auth/qr/cancel')}, 30_000)+ it('reports an expired spoken-code claim as EXPIRED after the person says yes', async () => {+ const root = tempRoot()+ const { base } = await fakeBackend({+ routes: {+ '/api/auth/qr/start': () => ({ code: 'hnq_x', pollToken: 'hnp_poll', expiresIn: 120, spokenCode: '12345678' }),+ '/api/auth/qr/poll': () => ({ status: 'approved', email: '[email protected]' }),+ '/api/auth/qr/claim': () => new Refusal(401, 'QR_INVALID', 'Nothing to claim: scan again.'),+ '/api/auth/qr/cancel': () => ({ cancelled: true }),+ },+ })+ const login = jsonChild(root, base, ['login', '--force', '--code', '--json'])+ expect(await login.next()).toEqual({ type: 'code', code: '12345678', expiresIn: 120 })+ expect(await login.next()).toEqual({ type: 'confirm', email: '[email protected]' })+ login.child.stdin.end('yes\n')+ expect(await login.next()).toMatchObject({ type: 'result', status: 'error', code: 'EXPIRED' })+ expect(await login.exit).toBe(1)+ expect(existsSync(join(root, 'auth', 'session.json'))).toBe(false)+ }, 30_000)+it('already signed in, an app that closes its pipe at once still gets the one success line', async () => {// Nothing waits on a person here, so the app going away is no reason to stop: the answer it// asked for is already on its way, and a CANCELLED here would read as a failed sign-in.diff --git a/cli/src/lib/qrSignIn.spec.ts b/cli/src/lib/qrSignIn.spec.tsindex 843cee89b..24b3c2d07 100644--- a/cli/src/lib/qrSignIn.spec.ts+++ b/cli/src/lib/qrSignIn.spec.ts@@ -3,7 +3,7 @@ import { claimTicket, qrSignIn, qrSignInLink, type QrSignInDeps } from './qrSignimport { terminalQr } from './terminalQr.js'/** A backend that answers the QR sign-in routes from a script of poll answers. */-function backend(polls: Array<{ status: string; email?: string }>, opts: { extend?: 'ok' | 'fail'; claim?: 'ok' | 'fail'; spoken?: 'unsupported' | 'rejected' } = {}) {+function backend(polls: Array<{ status: string; email?: string }>, opts: { extend?: 'ok' | 'fail'; claim?: 'ok' | 'fail' | 'expired' | 'unauthorized'; spoken?: 'unsupported' | 'rejected' } = {}) {const calls: Array<{ path: string; body: unknown }> = []let clock = 0const post = vi.fn(async (path: string, body: unknown) => {@@ -22,6 +22,8 @@ function backend(polls: Array<{ status: string; email?: string }>, opts: { exten}if (path === '/api/auth/qr/claim') {if (opts.claim === 'fail') throw new Error('Nothing to claim')+ if (opts.claim === 'expired') throw Object.assign(new Error('Nothing to claim: scan again.'), { status: 401, code: 'QR_INVALID' })+ if (opts.claim === 'unauthorized') throw Object.assign(new Error('Not authorized.'), { status: 401, code: 'UNAUTHORIZED' })return { token: 'hna_x', refreshToken: 'hnr_x', expiresIn: 3600, autonomousEnv: 'prod', email: '[email protected]' }}if (path === '/api/auth/qr/cancel') return { cancelled: true }@@ -91,6 +93,53 @@ describe('qrSignIn', () => {expect(new Set(d.shown)).toEqual(new Set([qrSignInLink('hnq_code')]))})+ it('does not ask the person to confirm an approval delivered after the code expired', async () => {+ const b = backend([{ status: 'approved', email: '[email protected]' }])+ const d = b.deps()+ const post = d.post+ d.post = async <T>(path: string, body: unknown): Promise<T> => {+ if (path === '/api/auth/qr/poll') await d.sleep?.(121_000)+ return post<T>(path, body)+ }++ const r = await qrSignIn(d)++ expect(r).toMatchObject({ ok: false, code: 'EXPIRED' })+ expect(d.confirm).not.toHaveBeenCalled()+ expect(b.paths()).not.toContain('/api/auth/qr/claim')+ expect(b.paths()).toContain('/api/auth/qr/cancel')+ })++ it('reports expiry when the person confirms after the code ran out', async () => {+ const b = backend([{ status: 'approved', email: '[email protected]' }])+ const d = b.deps()+ d.confirm = vi.fn(async () => {+ await d.sleep?.(121_000)+ return true+ })++ const r = await qrSignIn(d)++ expect(r).toMatchObject({ ok: false, code: 'EXPIRED' })+ expect(b.paths()).not.toContain('/api/auth/qr/claim')+ expect(b.paths()).toContain('/api/auth/qr/cancel')+ })++ it('reports an expired claim as EXPIRED', async () => {+ const approved = [{ status: 'approved', email: '[email protected]' }]+ expect(await qrSignIn(backend(approved, { claim: 'expired' }).deps())).toMatchObject({ ok: false, code: 'EXPIRED' })+ })++ it('preserves an unrelated unauthorized claim failure', async () => {+ const approved = [{ status: 'approved', email: '[email protected]' }]+ expect(await qrSignIn(backend(approved, { claim: 'unauthorized' }).deps())).toMatchObject({ ok: false, code: 'BACKEND_ERROR' })+ })++ it('preserves other claim failures', async () => {+ const approved = [{ status: 'approved', email: '[email protected]' }]+ expect(await qrSignIn(backend(approved, { claim: 'fail' }).deps())).toMatchObject({ ok: false, code: 'BACKEND_ERROR' })+ })+it('gives up when the code can live no longer', async () => {const b = backend(Array.from({ length: 200 }, () => ({ status: 'pending' })), { extend: 'fail' })const r = await qrSignIn(b.deps())@@ -123,6 +172,23 @@ describe('qrSignIn with a spoken code', () => {expect(d.confirm).toHaveBeenCalledWith('[email protected]')})+ it('does not ask the person to confirm an approval delivered after the spoken code expired', async () => {+ const b = backend([{ status: 'approved', email: '[email protected]' }])+ const { d } = spokenDeps(b)+ const post = d.post+ d.post = async <T>(path: string, body: unknown): Promise<T> => {+ if (path === '/api/auth/qr/poll') await d.sleep?.(121_000)+ return post<T>(path, body)+ }++ const r = await qrSignIn(d)++ expect(r).toMatchObject({ ok: false, code: 'EXPIRED' })+ expect(d.confirm).not.toHaveBeenCalled()+ expect(b.paths()).not.toContain('/api/auth/qr/claim')+ expect(b.paths()).toContain('/api/auth/qr/cancel')+ })+it('says the same code again each time it is kept alive', async () => {const polls: Array<{ status: string; email?: string }> = Array.from({ length: 50 }, () => ({ status: 'pending' }))const b = backend([...polls, { status: 'approved', email: '[email protected]' }])diff --git a/cli/src/lib/qrSignIn.ts b/cli/src/lib/qrSignIn.tsindex 7888a70e1..1489fcd20 100644--- a/cli/src/lib/qrSignIn.ts+++ b/cli/src/lib/qrSignIn.ts@@ -73,6 +73,7 @@ export async function qrSignIn(deps: QrSignInDeps): Promise<QrSignInResult> {}const { pollToken } = startedconst expired = `The code expired. Run \`harness login${spoken ? ' --code' : ''}\` again.`+ const expiredResult = { ok: false as const, code: 'EXPIRED' as const, message: expired }const link = qrSignInLink(started.code)const cancel = async (): Promise<void> => { await deps.post('/api/auth/qr/cancel', { pollToken }).catch(() => {}) }const spokenCode = started.spokenCode@@ -108,17 +109,29 @@ export async function qrSignIn(deps: QrSignInDeps): Promise<QrSignInResult> {if (state.status === 'pending') continueif (state.status === 'denied') return { ok: false, code: 'DENIED', message: 'Sign-in was denied on the phone.' }if (state.status !== 'approved' || !state.email) {- return { ok: false, code: 'EXPIRED', message: expired }+ return expiredResult}- if (!(await deps.confirm(state.email))) {+ // #1507: a delayed poll or confirmation can outlast the code; don't ask or claim after expiry.+ if (expiresAt <= now()) {+ await cancel()+ return expiredResult+ }+ const confirmed = await deps.confirm(state.email)+ if (!confirmed) {await cancel()return { ok: false, code: 'CANCELLED', message: `Not signed in as ${state.email}.` }}+ if (expiresAt <= now()) {+ await cancel()+ return expiredResult+ }try {const tokens = await deps.post<QrSignInTokens>('/api/auth/qr/claim', { pollToken })if (!tokens?.token) throw new Error('the backend returned no session')return { ok: true, tokens: { ...tokens, email: tokens.email ?? state.email } }} catch (err) {+ const failure = err as { status?: number; code?: string }+ if (failure.status === 401 && failure.code === 'QR_INVALID') return expiredResultreturn { ok: false, code: 'BACKEND_ERROR', message: (err as Error).message }}}