diff --git a/docs/PI.md b/docs/PI.md index 4a9261c0..fb7a0acd 100644 --- a/docs/PI.md +++ b/docs/PI.md @@ -15,10 +15,10 @@ an executable). A session needs a model Pi can reach, any of: -- **An account** connected on the phone (the agent's page → Accounts): an - API key for any of Pi's providers, or a sign-in to a subscription where - Pi has one (Claude Pro/Max, ChatGPT Plus/Pro, GitHub Copilot, xAI and - others). Pi's own code stores it, in its home (below), exactly where its +- **An account** connected on the phone (the agent's page → AI providers → + Connect a provider): an API key for any of Pi's providers, or a sign-in to + a subscription where Pi has one (Claude Pro/Max, ChatGPT Plus/Pro through + OpenAI Codex, GitHub Copilot, xAI and others). Pi's own code stores it, in its home (below), exactly where its `/login` would. The model picker then lists that provider's models. - **A provider profile** added on the phone (the agent's page → AI providers): any OpenAI-compatible endpoint — a provider's own API or a @@ -33,7 +33,7 @@ A session needs a model Pi can reach, any of: levels and the longest answer. A size the endpoint states wins; a model Pi does not know runs at 128k context without reasoning. - **The bridge's environment**: the keys Pi reads itself, such as - `ANTHROPIC_API_KEY` or `OPENROUTER_API_KEY`. Accounts shows them as the + `ANTHROPIC_API_KEY` or `OPENROUTER_API_KEY`. The phone shows them as the machine's, which only the machine can change. ## Accounts @@ -45,10 +45,20 @@ by Pi only when a session uses it. A sign-in gives a page to open on any device. Some providers then show a code to enter there and finish on their own; the others send the browser back to an address on the bridge's machine, which a phone cannot open: paste -the address it fails to load, and Pi finishes the sign-in with it. A -provider whose login asks something more than a key or a sign-in (a -resource name, a region) is refused with Pi's question — run Pi's own -`/login` on the machine for it. +the address it fails to load, and Pi finishes the sign-in with it. + +What else a login asks is on the form, in Pi's own words: Cloudflare's +account and gateway IDs beside the key, how to sign in to Anthropic or +OpenAI Codex (a page, or a code for another device), a GitHub Enterprise +domain (blank for github.com). Pi asks these as its login goes, so the +bridge finds them when the phone lists the providers, by running each +login with every way off the machine blocked (the network, listening, +other programs) and its placeholder answers kept in a scratch file deleted +afterwards. Where a login offers several ways and only one works without +the machine's own setup (Amazon Bedrock's bearer token, Google Vertex's API +key, rather than an AWS profile or a credentials file), the phone offers +that one. A way that needs the machine itself is not offered — run Pi's +own `/login` there for it. ## Its own home diff --git a/packages/agent-host/src/drivers/pi/__tests__/fakePi.ts b/packages/agent-host/src/drivers/pi/__tests__/fakePi.ts index 31a139bd..601688da 100644 --- a/packages/agent-host/src/drivers/pi/__tests__/fakePi.ts +++ b/packages/agent-host/src/drivers/pi/__tests__/fakePi.ts @@ -146,6 +146,11 @@ export class FakePi { this.child.stdout.emit('data', Buffer.from(`${JSON.stringify(record)}\n`)); } + /** Write one raw line on stdout, as a Pi that printed something else would. */ + print(line: string): void { + this.child.stdout.emit('data', Buffer.from(`${line}\n`)); + } + private respond(id: unknown, command: string, data?: unknown): void { this.emit({ ...(typeof id === 'string' ? { id } : {}), type: 'response', command, success: true, ...(data !== undefined ? { data } : {}) }); } @@ -254,14 +259,16 @@ export class FakePi { this.say('recovered', { retried: 2 }); return; } - if (text === '/give-up') { - this.failedAttempt(1); - this.failedAttempt(2); - this.failedAttempt(3); - this.attemptError(); + if (text === '/give-up' || text.startsWith('/give-up ')) { + // `/give-up `: every attempt fails with that provider error. + const error = text.slice('/give-up'.length).trim() || RATE_LIMITED; + this.failedAttempt(1, error); + this.failedAttempt(2, error); + this.failedAttempt(3, error); + this.attemptError(error); this.emit({ type: 'turn_end', message: {}, toolResults: [] }); this.emit({ type: 'agent_end', messages: [] }); - this.emit({ type: 'auto_retry_end', success: false, attempt: 3, finalError: RATE_LIMITED }); + this.emit({ type: 'auto_retry_end', success: false, attempt: 3, finalError: error }); this.emit({ type: 'agent_settled', aborted: false }); return; } @@ -270,10 +277,11 @@ export class FakePi { this.failedAttempt(1); return; } - if (text === '/bad-request') { - // An error Pi does not retry: settled at once. + if (text === '/bad-request' || text.startsWith('/bad-request ')) { + // An error Pi does not retry: settled at once. `/bad-request ` + // names the provider's error. this.emit({ type: 'agent_start' }); - this.attemptError('400: invalid request'); + this.attemptError(text.slice('/bad-request'.length).trim() || '400: invalid request'); this.settle(false); return; } @@ -281,13 +289,13 @@ export class FakePi { } /** One failed request that Pi then retries. */ - private failedAttempt(attempt: number): void { + private failedAttempt(attempt: number, error = RATE_LIMITED): void { if (attempt === 1) this.emit({ type: 'agent_start' }); - this.attemptError(); + this.attemptError(error); this.emit({ type: 'turn_end', message: {}, toolResults: [] }); this.emit({ type: 'agent_end', messages: [] }); // Pi retries up to three times, each retry announced before it starts. - this.emit({ type: 'auto_retry_start', attempt, maxAttempts: 3, delayMs: 2000 * attempt, errorMessage: RATE_LIMITED }); + this.emit({ type: 'auto_retry_start', attempt, maxAttempts: 3, delayMs: 2000 * attempt, errorMessage: error }); this.emit({ type: 'agent_start' }); } diff --git a/packages/agent-host/src/drivers/pi/__tests__/piAccounts.test.ts b/packages/agent-host/src/drivers/pi/__tests__/piAccounts.test.ts index 3711e1bc..d05945ad 100644 --- a/packages/agent-host/src/drivers/pi/__tests__/piAccounts.test.ts +++ b/packages/agent-host/src/drivers/pi/__tests__/piAccounts.test.ts @@ -74,6 +74,56 @@ real('Pi accounts, through the installed Pi', () => { await expect(manager.change('openrouter', { kind: 'remove', connection }, noEnd)).rejects.toThrow(/only the machine/); expect(existsSync(path.join(home, 'auth.json')) ? readFileSync(path.join(home, 'auth.json'), 'utf8') : '{}').not.toContain('sk-or-env'); }, 60_000); + + it('lists what each login asks before it starts, and stores nothing while finding out', async () => { + const { accounts: manager, home } = accounts(); + const all = await manager.list(true); + const method = (id: string, kind: string) => all.find((a) => a.id === id)?.methods?.find((m) => m.kind === kind); + // A key login with questions of its own. + expect(method('cloudflare-workers-ai', 'key')?.fields).toEqual([ + { key: 'Enter Cloudflare account ID', title: 'Cloudflare account ID', required: true }, + ]); + // A key login with several ways, of which only the key works from a phone. + expect(method('amazon-bedrock', 'key')).toBeDefined(); + expect(method('amazon-bedrock', 'key')?.fields).toBeUndefined(); + // Sign-ins that ask first: a choice of how, and a domain left blank for github.com. + expect(method('anthropic', 'sign-in')?.fields).toEqual([ + expect.objectContaining({ + key: 'Select Anthropic login method:', + required: true, + default: 'browser', + options: [expect.objectContaining({ id: 'browser' }), expect.objectContaining({ id: 'copy_code' })], + }), + ]); + expect(method('github-copilot', 'sign-in')?.fields).toEqual([ + { key: 'GitHub Enterprise URL/domain (blank for github.com)', title: 'GitHub Enterprise URL/domain (blank for github.com)', placeholder: 'company.ghe.com' }, + ]); + // Finding out ran Pi's logins on placeholders: none of it reached Pi's home. + const auth = path.join(home, 'auth.json'); + expect(existsSync(auth) ? JSON.parse(readFileSync(auth, 'utf8')) : {}).toEqual({}); + expect(await manager.list(false)).toEqual([]); + }, 60_000); + + it('stores a key with the answers its login asks for, and refuses it without them', async () => { + const { accounts: manager, home } = accounts(); + await expect( + manager.change('cloudflare-workers-ai', { kind: 'key', method: 'key', key: 'cf-codedeck-test' }, noEnd), + ).rejects.toThrow(/Cloudflare account ID is needed/); + await manager.change( + 'cloudflare-workers-ai', + { kind: 'key', method: 'key', key: 'cf-codedeck-test', answers: { 'Enter Cloudflare account ID': 'acct-1' } }, + noEnd, + ); + const stored = JSON.parse(readFileSync(path.join(home, 'auth.json'), 'utf8')) as Record; + expect(stored['cloudflare-workers-ai']).toEqual({ type: 'api_key', key: 'cf-codedeck-test', env: { CLOUDFLARE_ACCOUNT_ID: 'acct-1' } }); + }, 60_000); + + it('takes the key way of a login that offers several', async () => { + const { accounts: manager, home } = accounts(); + await manager.change('amazon-bedrock', { kind: 'key', method: 'key', key: 'bedrock-codedeck-test' }, noEnd); + const stored = JSON.parse(readFileSync(path.join(home, 'auth.json'), 'utf8')) as Record; + expect(stored['amazon-bedrock']).toEqual({ type: 'api_key', key: 'bedrock-codedeck-test' }); + }, 60_000); }); /** A scripted helper: records what the driver sends, and answers with the @@ -109,14 +159,37 @@ class ScriptedHelper { } } -function scripted(helper: ScriptedHelper, now = () => 1_000): PiAccounts { +/** The paths of a login that asks nothing but its key, and of a sign-in + * that goes straight to its page. */ +const PLAIN = { + keyPaths: [{ prompts: [{ type: 'secret', message: 'Key' }], end: 'done' }], + signInPaths: [{ prompts: [], end: 'user' }], +}; + +/** A helper that lists one provider, as Pi's helper does before a login. */ +function listing(provider: string, paths: Record = PLAIN): ScriptedHelper { + const helper = new ScriptedHelper(); + helper.onSend = (record) => { + if (record.op !== 'list') return; + setImmediate(() => { + helper.say({ type: 'list', providers: [{ id: provider, name: provider, key: 'Key', signIn: 'Sign in', ...paths }], credentials: [] }); + helper.exit(0); + }); + }; + return helper; +} + +/** A manager whose helpers are, in turn: the listing of `provider` a key or + * a sign-in reads first, then `helper`. */ +function scripted(helper: ScriptedHelper, provider: string, paths?: Record, now = () => 1_000): PiAccounts { const home = mkdtempSync(path.join(tmpdir(), 'codedeck-pi-accounts-')); + const queue = [listing(provider, paths), helper]; return new PiAccounts({ home, entryPoint: async () => path.join(home, 'dist', 'bundle', 'cli.js'), env: () => ({}), changed: () => {}, - spawnFn: (() => helper.child) as never, + spawnFn: (() => (queue.shift() ?? helper).child) as never, now, }); } @@ -141,7 +214,7 @@ describe('Pi sign-in', () => { } }; const ends: SignInState[] = []; - const manager = scripted(helper); + const manager = scripted(helper, 'anthropic'); const started = await manager.change('anthropic', { kind: 'sign-in', method: 'sign-in' }, (end) => ends.push(end)); expect('signIn' in started && started.signIn).toMatchObject({ status: 'started', @@ -161,7 +234,7 @@ describe('Pi sign-in', () => { helper.onSend = (record) => { if (record.op === 'login') setImmediate(() => helper.say({ type: 'notify', event: { type: 'auth_url', url: 'https://example.com/a' } })); }; - const manager = scripted(helper); + const manager = scripted(helper, 'xai'); const started = await manager.change('xai', { kind: 'sign-in', method: 'sign-in' }, noEnd); const attemptId = 'signIn' in started ? started.signIn.attemptId : ''; await manager.change('xai', { kind: 'finish', attempt: attemptId, callbackUrl: 'http://localhost:1/cb?code=z' }, noEnd); @@ -179,7 +252,7 @@ describe('Pi sign-in', () => { } }; const ends: SignInState[] = []; - const manager = scripted(helper); + const manager = scripted(helper, 'github-copilot'); const started = await manager.change('github-copilot', { kind: 'sign-in', method: 'sign-in' }, (end) => ends.push(end)); expect('signIn' in started && started.signIn.attempt).toEqual( expect.objectContaining({ url: 'https://github.com/login/device', instructions: expect.stringContaining('ABCD-1234'), expiresAt: 1_000 + 900_000 }), @@ -198,7 +271,7 @@ describe('Pi sign-in', () => { if (record.op === 'login') setImmediate(() => helper.say({ type: 'notify', event: { type: 'auth_url', url: 'https://example.com/a' } })); }; const ends: SignInState[] = []; - const manager = scripted(helper); + const manager = scripted(helper, 'openai'); const started = await manager.change('openai', { kind: 'sign-in', method: 'sign-in' }, (end) => ends.push(end)); const attemptId = 'signIn' in started ? started.signIn.attemptId : ''; const cancelled = await manager.change('openai', { kind: 'cancel', attempt: attemptId }, noEnd); @@ -207,6 +280,37 @@ describe('Pi sign-in', () => { await tick(); expect(ends).toEqual([]); }); + + it('answers the choice Pi asks before its page with the one made on the phone', async () => { + const how = 'Select Anthropic login method:'; + const choice = (id: string) => ({ + prompts: [{ type: 'select', message: how, options: [{ id: 'browser', label: 'Browser' }, { id: 'copy_code', label: 'Copy code' }], choice: id }], + end: 'user', + }); + const helper = new ScriptedHelper(); + helper.onSend = (record) => { + if (record.op === 'login') { + setImmediate(() => helper.say({ type: 'prompt', id: 'p1', prompt: { type: 'select', message: how } })); + } + if (record.op === 'answer') { + setImmediate(() => helper.say({ type: 'notify', event: { type: 'auth_url', url: 'https://claude.ai/oauth/authorize?code=true' } })); + } + }; + const manager = scripted(helper, 'anthropic', { signInPaths: [choice('browser'), choice('copy_code')] }); + const started = await manager.change('anthropic', { kind: 'sign-in', method: 'sign-in', answers: { [how]: 'copy_code' } }, noEnd); + expect(helper.sent).toContainEqual({ op: 'answer', id: 'p1', value: 'copy_code' }); + expect('signIn' in started && started.signIn.status).toBe('started'); + }); + + it('refuses a choice the phone could not have been offered', async () => { + const how = 'Sign in how:'; + const helper = new ScriptedHelper(); + const manager = scripted(helper, 'radius', { + signInPaths: [{ prompts: [{ type: 'select', message: how, options: [{ id: 'browser', label: 'Browser' }, { id: 'device-code', label: 'Code' }], choice: 'browser' }], end: 'user' }, { prompts: [{ type: 'select', message: how, choice: 'device-code' }], end: 'user' }], + }); + await expect(manager.change('radius', { kind: 'sign-in', method: 'sign-in', answers: { [how]: 'carrier-pigeon' } }, noEnd)).rejects.toThrow(/Choose one/); + expect(helper.sent).toEqual([]); + }); }); describe('Pi key login', () => { @@ -223,7 +327,7 @@ describe('Pi key login', () => { }); } }; - await expect(scripted(helper).change('azure', { kind: 'key', method: 'key', key: 'k' }, noEnd)).rejects.toThrow(/Azure resource name/); + await expect(scripted(helper, 'azure').change('azure', { kind: 'key', method: 'key', key: 'k' }, noEnd)).rejects.toThrow(/Azure resource name/); }); it('keeps the key out of an error Pi gives back', async () => { @@ -237,7 +341,7 @@ describe('Pi key login', () => { }); } }; - const failure = scripted(helper).change('openrouter', { kind: 'key', method: 'key', key: 'sk-secret-value' }, noEnd); + const failure = scripted(helper, 'openrouter').change('openrouter', { kind: 'key', method: 'key', key: 'sk-secret-value' }, noEnd); await expect(failure).rejects.toThrow(/invalid key/); await expect(failure).rejects.not.toThrow(/sk-secret-value/); }); diff --git a/packages/agent-host/src/drivers/pi/__tests__/piDriver.test.ts b/packages/agent-host/src/drivers/pi/__tests__/piDriver.test.ts index 4c4a77da..9394a13a 100644 --- a/packages/agent-host/src/drivers/pi/__tests__/piDriver.test.ts +++ b/packages/agent-host/src/drivers/pi/__tests__/piDriver.test.ts @@ -212,6 +212,57 @@ describe('PiSession startup', () => { expect(h.fake.token).toBe(''); }); + describe('a profile token quoted back by a failure', () => { + const TOKEN = 'sk-codedeck-profile-secret-0123456789'; + // Any piece of it long enough to be worth anything. + const PIECE = TOKEN.slice(0, 16); + const provider = { + id: 'codedeck-fake', + label: 'Fake', + baseUrl: 'https://api.example.com', + authToken: TOKEN, + models: [{ id: 'fake-model-1', label: 'Fake model' }], + }; + + it('is not in the errors and retries the phone is shown', async () => { + const h = withDriver(); + const ctx = await ready(h, { model: 'fake-model-1', provider }); + // An endpoint that echoes the header it refused, the token straddling + // the point where a retry line is cut short (it starts at column 100). + await turn(h.session!, ctx, `/give-up 401 Unauthorized: ${'x'.repeat(59)} Authorization: Bearer ${TOKEN}`); + await turn(h.session!, ctx, `/bad-request invalid api key ${TOKEN}`); + const shown = JSON.stringify(entries(ctx)); + expect(shown).toContain('401 Unauthorized'); + expect(shown).toContain('invalid api key'); + expect(shown).not.toContain(PIECE); + }); + + it('is not in what Pi printed, in the log or in the reason the session ended', async () => { + const h = withDriver({ fake: { exitOnStart: { code: 1, stderr: `Error: the endpoint refused ${TOKEN}` } } }); + const ctx = start(h, { model: 'fake-model-1', provider }); + const ended = await ctx.ended(); + expect(ended).toMatchObject({ error: expect.stringContaining('the endpoint refused') }); + expect(JSON.stringify(ended)).not.toContain(PIECE); + expect(h.logs.join('\n')).toContain('the endpoint refused'); + expect(h.logs.join('\n')).not.toContain(PIECE); + }); + + it('is not in the log when an extension fails or Pi prints a line that is not a record', async () => { + const h = withDriver(); + await ready(h, { model: 'fake-model-1', provider }); + h.fake.emit({ type: 'extension_error', error: `registerProvider refused apiKey ${TOKEN}` }); + // A line that starts with it: a parse error quotes the line's start, + // cut where the redaction could no longer find it. + h.fake.print(`${TOKEN} was not accepted`); + h.fake.emit({ type: 'response', success: false, error: `bad command near ${TOKEN}` }); + const logged = h.logs.join('\n'); + expect(logged).toContain('an extension failed'); + expect(logged).toContain('dropped a line'); + expect(logged).not.toContain(PIECE); + expect(logged).not.toContain(TOKEN.slice(0, 8)); + }); + }); + it('refuses a provider profile with an insecure endpoint before it starts', () => { const h = withDriver(); expect(() => diff --git a/packages/agent-host/src/drivers/pi/accounts.ts b/packages/agent-host/src/drivers/pi/accounts.ts index edae7cf8..9765b39f 100644 --- a/packages/agent-host/src/drivers/pi/accounts.ts +++ b/packages/agent-host/src/drivers/pi/accounts.ts @@ -14,8 +14,16 @@ * open (`auth_url`) or a code to enter on any device (`device_code`) — and, * for a page, asks for the address the browser lands on (`manual_code`), * since the provider sends it back to a listener on this machine a phone - * cannot reach. A login that asks anything else is refused with Pi's own - * question: only Pi's interactive `/login` can hold that conversation. + * cannot reach. + * + * Pi asks a login's other questions as it goes (Cloudflare's account ID, how + * to sign in to Anthropic, a GitHub Enterprise domain), while a phone needs + * them on the form before it starts. So the listing finds them by running + * each login cut off from the network (authHelper's askedBy), offers them as + * the method's fields keyed by Pi's own question, and a login answers Pi's + * prompts from the phone's answers (questionsOf, answersFor). A question the + * listing did not find is refused with Pi's own words: only Pi's + * interactive `/login` can hold that conversation. */ import { randomUUID } from 'node:crypto'; import { spawn, type ChildProcess } from 'node:child_process'; @@ -23,7 +31,7 @@ import { pathToFileURL } from 'node:url'; import * as path from 'node:path'; import type { AccountChangeResult, AccountManager, SignInState } from '../../sdk/driver'; import { redactSecrets } from '../../sdk/redact'; -import type { AccountChange, AccountConnection, AccountMethod, AgentAccount } from '../../sdk/types'; +import type { AccountChange, AccountConnection, AccountField, AccountMethod, AgentAccount } from '../../sdk/types'; import { installPiAuthHelper } from './authHelper'; import { PiLineDecoder, type PiRecord, type PiSpawn } from './rpc'; @@ -46,6 +54,25 @@ interface HelperProvider { /** The sign-in's name ("Anthropic (Claude Pro/Max)"), when there is one. */ signIn?: string; status?: { configured?: boolean; source?: string; label?: string }; + /** What the key login asks, when the listing looked (authHelper's askedBy). */ + keyPaths?: LoginPath[]; + /** What the sign-in asks, likewise. */ + signInPaths?: LoginPath[]; +} + +/** One way through a login: the questions met, and how it ended. */ +interface LoginPath { + prompts: Array<{ type: string; message: string; placeholder?: string; options?: Array<{ id: string; label: string }>; choice?: string }>; + /** `done`, `user` (handed to the user), `offline` (would go online), `failed`. */ + end: string; +} + +/** What a login asks the phone, and the choices it makes itself. */ +export interface LoginQuestions { + /** Keyed by Pi's own question, which is what its prompt says. */ + fields: AccountField[]; + /** A choice with one way that works from a phone: answered without asking. */ + auto: Record; } /** One helper process: an operation sent, its records read back. */ @@ -136,10 +163,8 @@ export class PiAccounts implements AccountManager { } async list(all: boolean): Promise { - let listed: PiRecord | undefined; - await this.once({ op: 'list' }, (record) => { - if (record.type === 'list') listed = record; - }); + // Every provider is for connecting one, so it comes with its questions. + const listed = await this.listing(all ? { questions: true } : {}); const providers = Array.isArray(listed?.providers) ? (listed.providers as HelperProvider[]) : []; const stored = new Map(); for (const raw of Array.isArray(listed?.credentials) ? listed.credentials : []) { @@ -158,7 +183,7 @@ export class PiAccounts implements AccountManager { try { switch (change.kind) { case 'key': - await this.storeKey(account, change.key); + await this.storeKey(account, change.key, await this.answers(account, 'key', change.answers)); this.options.changed(); return { accounts: await this.list(false) }; case 'remove': @@ -172,7 +197,7 @@ export class PiAccounts implements AccountManager { // Pi keeps one stored access per provider: it is the active one. return { accounts: await this.list(false) }; case 'sign-in': - return { signIn: await this.signIn(account, ended) }; + return { signIn: await this.signIn(account, await this.answers(account, 'sign-in', change.answers), ended) }; case 'finish': return { signIn: this.finish(account, change.attempt, change.callbackUrl) }; case 'cancel': @@ -195,9 +220,39 @@ export class PiAccounts implements AccountManager { this.attempts.clear(); } - private async storeKey(account: string, key: string): Promise { + /** Pi's listing, read from one helper run. */ + private async listing(request: { provider?: string; questions?: boolean }): Promise { + let listed: PiRecord | undefined; + await this.once({ op: 'list', ...request }, (record) => { + if (record.type === 'list') listed = record; + }); + return listed; + } + + /** + * What to tell Pi when its login for `account` asks: the questions it asks + * that way, read again from the installed Pi, answered from the phone's + * answers (or their defaults) and Pi's one workable choice. + */ + private async answers(account: string, kind: 'key' | 'sign-in', given: Record | undefined): Promise> { + const listed = await this.listing({ provider: account, questions: true }); + const provider = (Array.isArray(listed?.providers) ? (listed.providers as HelperProvider[]) : []).find((p) => p.id === account); + if (!provider) throw new Error(`Pi has no provider "${account}".`); + const questions = questionsOf(kind, kind === 'key' ? provider.keyPaths : provider.signInPaths); + if (!questions) { + throw new Error( + kind === 'key' + ? `Pi's key login for ${provider.name} needs more than a phone can give; run Pi's own /login on the machine.` + : `Pi's sign-in for ${provider.name} cannot be finished from a phone; run Pi's own /login on the machine.`, + ); + } + return answersFor(questions, given ?? {}); + } + + private async storeKey(account: string, key: string, answers: Map): Promise { let refused: string | undefined; let answered = false; + const given = new Set(); const run = await this.start({ op: 'login', provider: account, method: 'api_key' }, (record) => { if (record.type !== 'prompt') return; const prompt = record.prompt as { type?: string; message?: string } | undefined; @@ -206,6 +261,11 @@ export class PiAccounts implements AccountManager { run.send({ op: 'answer', id: record.id, value: key }); return; } + const answer = answerOnce(answers, given, prompt); + if (answer !== undefined) { + run.send({ op: 'answer', id: record.id, value: answer }); + return; + } refused = prompt?.message ?? 'a question'; run.send({ op: 'cancel' }); }, OPERATION_TIMEOUT_MS); @@ -216,9 +276,10 @@ export class PiAccounts implements AccountManager { if (failure !== undefined) throw new Error(failure); } - private async signIn(account: string, ended: (end: SignInState) => void): Promise { + private async signIn(account: string, answers: Map, ended: (end: SignInState) => void): Promise { const attemptId = randomUUID(); let begun = false; + const given = new Set(); let settle!: { resolve(state: SignInState): void; reject(error: Error): void }; const first = new Promise((resolve, reject) => { settle = { resolve, reject }; @@ -247,6 +308,11 @@ export class PiAccounts implements AccountManager { } return; } + const answer = answerOnce(answers, given, prompt); + if (answer !== undefined) { + run.send({ op: 'answer', id, value: answer }); + return; + } // A choice or a field this flow cannot put to the phone. run.send({ op: 'cancel' }); const why = `Pi asks "${prompt?.message ?? 'a question'}" for this sign-in, which only Pi's own /login can answer.`; @@ -358,8 +424,14 @@ export class PiAccounts implements AccountManager { * access comes from now — never the secret itself. */ export function toAccount(provider: HelperProvider, storedType: string | undefined): AgentAccount { const methods: AccountMethod[] = []; - if (provider.key) methods.push({ id: PI_KEY_METHOD, kind: 'key', label: provider.key }); - if (provider.signIn) methods.push({ id: PI_SIGN_IN_METHOD, kind: 'sign-in', label: provider.signIn }); + const method = (id: string, kind: 'key' | 'sign-in', label: string, paths: LoginPath[] | undefined): void => { + // A way Pi offers that cannot work from a phone is not offered there. + const questions = questionsOf(kind, paths); + if (!questions) return; + methods.push({ id, kind, label, ...(questions.fields.length > 0 ? { fields: questions.fields } : {}) }); + }; + if (provider.key) method(PI_KEY_METHOD, 'key', provider.key, provider.keyPaths); + if (provider.signIn) method(PI_SIGN_IN_METHOD, 'sign-in', provider.signIn, provider.signInPaths); const connections: AccountConnection[] = []; const status = provider.status; if (status?.configured) { @@ -386,6 +458,121 @@ export function toAccount(provider: HelperProvider, storedType: string | undefin }; } +/** + * The questions a login asks, from the ways through it the listing found; + * null when no way works from a phone. A key login works when it asks for + * exactly one secret — the key the phone sends — and finishes without + * sending the user anywhere; a sign-in works when it asks no secret and + * ends by sending the user to a page or a code (or going online to get + * one). A choice is asked only among the options that lead somewhere that + * works, and not at all when only one does; a question that follows a + * choice is asked only when that choice is made. Unknown paths (the listing + * did not look) ask nothing. + */ +export function questionsOf(kind: 'key' | 'sign-in', paths: LoginPath[] | undefined): LoginQuestions | null { + if (paths === undefined) return { fields: [], auto: {} }; + const works = (path: LoginPath): boolean => { + const secrets = path.prompts.filter((p) => p.type === 'secret').length; + if (path.prompts.some((p) => p.type !== 'secret' && p.type !== 'text' && p.type !== 'select')) return false; + return kind === 'key' + ? secrets === 1 && (path.end === 'done' || path.end === 'offline') + : secrets === 0 && (path.end === 'user' || path.end === 'offline'); + }; + const kept = paths.filter(works); + if (kept.length === 0) return null; + + // Each choice's options that lead to a working path. + const options = new Map>(); + for (const path of kept) { + for (const p of path.prompts) { + if (p.type === 'select' && p.choice !== undefined) { + options.set(p.message, (options.get(p.message) ?? new Set()).add(p.choice)); + } + } + } + const auto: Record = {}; + for (const [message, ids] of options) if (ids.size === 1) auto[message] = [...ids][0]!; + + // The choices a path made that the phone is asked for, up to a point. + const choicesBefore = (path: LoginPath, index: number): Array<{ key: string; value: string }> => + path.prompts + .slice(0, index) + .filter((p) => p.type === 'select' && p.choice !== undefined && auto[p.message] === undefined) + .map((p) => ({ key: p.message, value: p.choice! })); + const holds = (path: LoginPath, when: Array<{ key: string; value: string }>): boolean => + when.every((c) => path.prompts.some((p) => p.type === 'select' && p.message === c.key && p.choice === c.value)); + + const fields: AccountField[] = []; + const seen = new Set(); + for (const path of kept) { + for (const p of path.prompts) { + if (p.type === 'secret' || auto[p.message] !== undefined || seen.has(p.message)) continue; + seen.add(p.message); + const asking = kept.filter((k) => k.prompts.some((q) => q.message === p.message)); + // What every path that asks it chose first: the condition to ask it on. + let when = choicesBefore(asking[0]!, asking[0]!.prompts.findIndex((q) => q.message === p.message)); + for (const other of asking.slice(1)) { + const before = choicesBefore(other, other.prompts.findIndex((q) => q.message === p.message)); + when = when.filter((c) => before.some((b) => b.key === c.key && b.value === c.value)); + } + // The phone shows a question when all its conditions hold; a question + // asked on some other mix of choices cannot be put that way. + if (kept.some((k) => holds(k, when) !== asking.includes(k))) return null; + const field: AccountField = { key: p.message, title: titleOf(p.message) }; + if (p.type === 'select') { + const ids = options.get(p.message)!; + field.options = (p.options ?? []).filter((o) => ids.has(o.id)).map((o) => ({ id: o.id, label: o.label })); + field.required = true; + field.default = field.options[0]?.id ?? null; + } else { + // Pi stores a key login's answers as they are, and checks a + // sign-in's with the provider, which says what a blank one means + // (GitHub's: github.com). + if (kind === 'key') field.required = true; + if (p.placeholder) field.placeholder = p.placeholder; + } + if (when.length > 0) field.when = when; + fields.push(field); + } + } + return { fields, auto }; +} + +/** Pi's answers for a login: its own choices, and the phone's answers to the + * questions asked on them. Throws on a missing or impossible answer. */ +export function answersFor(questions: LoginQuestions, given: Record): Map { + const answers = new Map(Object.entries(questions.auto)); + const effective = (field: AccountField): string => (given[field.key] ?? field.default ?? '').trim(); + const values = new Map(questions.fields.map((f) => [f.key, effective(f)])); + for (const field of questions.fields) { + const asked = (field.when ?? []).every((c) => ((values.get(c.key) ?? '') === c.value) !== (c.not ?? false)); + if (!asked) continue; + const value = values.get(field.key) ?? ''; + if (field.options && field.options.length > 0 && !field.options.some((o) => o.id === value)) { + throw new Error(`Choose one of the ${field.title} options.`); + } + if (field.required && value === '') throw new Error(`${field.title} is needed.`); + answers.set(field.key, value); + } + return answers; +} + +/** The answer to a prompt Pi asks, once: a login that asks the same question + * again did not take the answer, and gets no second one. */ +function answerOnce(answers: Map, given: Set, prompt: { type?: string; message?: string } | undefined): string | undefined { + const message = prompt?.message; + if (message === undefined || prompt?.type === 'secret' || given.has(message)) return undefined; + const answer = answers.get(message); + if (answer !== undefined) given.add(message); + return answer; +} + +/** A question as a field's title: "Enter Cloudflare account ID" → "Cloudflare account ID". */ +function titleOf(message: string): string { + const bare = message.replace(/^(enter|select)\s+/i, '').replace(/:\s*$/, '').trim(); + return bare.charAt(0).toUpperCase() + bare.slice(1); +} + function describeSource(source: string | undefined): string { switch (source) { case 'environment': diff --git a/packages/agent-host/src/drivers/pi/authHelper.ts b/packages/agent-host/src/drivers/pi/authHelper.ts index 181b037b..d12699f1 100644 --- a/packages/agent-host/src/drivers/pi/authHelper.ts +++ b/packages/agent-host/src/drivers/pi/authHelper.ts @@ -13,12 +13,16 @@ * * It speaks JSON lines on stdio, one operation per process: * - * in {op:"list"} out {type:"list", providers:[…], credentials:[…]} + * in {op:"list", provider?, questions?} out {type:"list", providers:[…], credentials:[…]} * in {op:"login", provider, method} out {type:"notify", event} | {type:"prompt", id, prompt} * in {op:"answer", id, value} …, then {type:"done"} * in {op:"cancel"} out {type:"error", message} (aborted) * in {op:"logout", provider} out {type:"done"} * + * With `questions`, each provider also carries what its logins ask + * (`keyPaths`, `signInPaths`: see askedBy), found with this process cut off + * from the network; `provider` limits the list to one. + * * A key reaches it on stdin, never in its arguments or environment, which * other processes on the machine can read. Nothing it writes carries a * secret: a provider's status says where its key comes from, not what it is. @@ -31,13 +35,26 @@ interface PiBundle { ModelRuntime: { create(options: Record): Promise }; } +/** Node's own modules, as the generated module imports them. */ +interface NodeModules { + fs: typeof import('node:fs'); + os: typeof import('node:os'); + path: typeof import('node:path'); + net: typeof import('node:net'); + tls: typeof import('node:tls'); + http: typeof import('node:http'); + https: typeof import('node:https'); + childProcess: typeof import('node:child_process'); +} + /** * The helper's body. Keep it self-contained: see the module comment. Pi's - * bundle is imported by the generated module itself and handed in, rather - * than with `import()` in here: a transform (vitest's, for one) may rewrite - * that expression into a helper of its own, which the child does not have. + * bundle and Node's modules are imported by the generated module itself and + * handed in, rather than with `import()` in here: a transform (vitest's, for + * one) may rewrite that expression into a helper of its own, which the child + * does not have. */ -export async function codedeckPiAuthHelper(pi: PiBundle): Promise { +export async function codedeckPiAuthHelper(pi: PiBundle, node: NodeModules): Promise { const write = (record: unknown): void => { process.stdout.write(JSON.stringify(record) + '\n'); }; @@ -50,6 +67,124 @@ export async function codedeckPiAuthHelper(pi: PiBundle): Promise { let nextPrompt = 0; let pending = ''; + /** + * What each provider's logins ask, found by running them through Pi's own + * `login` with nothing able to leave the machine: every way out (fetch, + * sockets, listening, child processes) throws, the answers are + * placeholders, and whatever a login stores lands in a scratch + * `auth.json` deleted afterwards. A login is followed down every option of + * each choice it offers; each path records the questions met on it and + * how it ended — `done` (returned), `user` (it would now send the user + * somewhere: a page, a code, a pasted address), `offline` (it would now go + * online) or `failed`. This process ends after the listing, so nothing it + * blocks is needed again. + */ + const askedBy = async (providers: Array>): Promise => { + const scratchDir = node.fs.mkdtempSync(node.path.join(node.os.tmpdir(), 'codedeck-pi-questions-')); + try { + const scratch = await pi.ModelRuntime.create({ + authPath: scratchDir + '/auth.json', + modelsPath: String(process.env.PI_CODING_AGENT_DIR || '') + '/models.json', + }); + const offline = new Error('codedeck: offline'); + const refuse = function (): never { + throw offline; + }; + globalThis.fetch = refuse as never; + node.net.connect = node.net.createConnection = refuse as never; + node.net.Server.prototype.listen = refuse as never; + node.tls.connect = refuse as never; + node.http.request = node.http.get = node.https.request = node.https.get = refuse as never; + for (const name of ['spawn', 'spawnSync', 'exec', 'execSync', 'execFile', 'execFileSync', 'fork']) { + (node.childProcess as Record)[name] = refuse; + } + // A refresh Pi started on its own before the block, or a login left + // behind once its path ended, may now fail on its own time; one + // provider's leftovers must not cost the whole listing. + process.on('unhandledRejection', function () {}); + process.on('uncaughtException', function () {}); + + const walk = async (provider: string, method: string, choices: string[]) => { + const prompts: Array> = []; + const controller = new AbortController(); + const handOver = new Error('codedeck: hand over'); + let picked = 0; + let branch: string[] | undefined; + let end: string; + const timer = setTimeout(function () { + controller.abort(); + }, 5_000); + const aborted = new Promise(function (_resolve, reject) { + controller.signal.addEventListener('abort', function () { + reject(offline); + }); + }); + aborted.catch(function () {}); + try { + await Promise.race([ + scratch.login(provider, method, { + signal: controller.signal, + // A page or a code is where the user takes over; anything else + // (a note, a link to read) is not. + notify: function (event: { type?: string }) { + if (event && (event.type === 'auth_url' || event.type === 'device_code')) throw handOver; + }, + prompt: async function (prompt: { type: string; message: string; placeholder?: string; options?: Array<{ id: string; label: string }> }) { + if (prompt.type === 'manual_code') throw handOver; + const seen: Record = { type: prompt.type, message: prompt.message }; + if (prompt.placeholder) seen.placeholder = prompt.placeholder; + prompts.push(seen); + if (prompt.type !== 'select') return 'codedeck-placeholder'; + seen.options = (prompt.options || []).map(function (option) { + return { id: option.id, label: option.label }; + }); + const choice = choices[picked++]; + if (choice === undefined) { + branch = (prompt.options || []).map(function (option) { + return option.id; + }); + throw handOver; + } + seen.choice = choice; + return choice; + }, + }), + aborted, + ]); + end = 'done'; + } catch (error) { + end = error === handOver ? 'user' : error === offline || controller.signal.aborted ? 'offline' : 'failed'; + } finally { + clearTimeout(timer); + controller.abort(); + } + return { prompts, end, branch }; + }; + const explore = async (provider: string, method: string) => { + const paths: Array> = []; + const pending: string[][] = [[]]; + // A bound on the paths followed: Pi's logins have at most three. + while (pending.length > 0 && paths.length < 32) { + const choices = pending.shift()!; + const found = await walk(provider, method, choices); + if (found.branch) { + for (const id of found.branch) pending.push(choices.concat([id])); + continue; + } + paths.push({ prompts: found.prompts, end: found.end }); + } + return paths; + }; + for (const provider of providers) { + const id = String(provider.id); + if (provider.key !== undefined) provider.keyPaths = await explore(id, 'api_key'); + if (provider.signIn !== undefined) provider.signInPaths = await explore(id, 'oauth'); + } + } finally { + node.fs.rmSync(scratchDir, { recursive: true, force: true }); + } + }; + const run = async (command: Record): Promise => { const dir = String(process.env.PI_CODING_AGENT_DIR || ''); const runtime = await pi.ModelRuntime.create({ @@ -58,20 +193,26 @@ export async function codedeckPiAuthHelper(pi: PiBundle): Promise { }); try { if (command.op === 'list') { - const providers = runtime.getProviders().map(function (provider: { - id: string; - name: string; - auth: { apiKey?: { name: string; login?: unknown }; oauth?: { name: string } }; - }) { - return { - id: provider.id, - name: provider.name, - ...(provider.auth.apiKey && provider.auth.apiKey.login ? { key: provider.auth.apiKey.name } : {}), - ...(provider.auth.oauth ? { signIn: provider.auth.oauth.name } : {}), - status: runtime.getProviderAuthStatus(provider.id), - }; - }); + const providers = runtime + .getProviders() + .filter(function (provider: { id: string }) { + return command.provider === undefined || provider.id === command.provider; + }) + .map(function (provider: { + id: string; + name: string; + auth: { apiKey?: { name: string; login?: unknown }; oauth?: { name: string } }; + }) { + return { + id: provider.id, + name: provider.name, + ...(provider.auth.apiKey && provider.auth.apiKey.login ? { key: provider.auth.apiKey.name } : {}), + ...(provider.auth.oauth ? { signIn: provider.auth.oauth.name } : {}), + status: runtime.getProviderAuthStatus(provider.id), + }; + }); const credentials = await runtime.listCredentials(); + if (command.questions === true) await askedBy(providers); write({ type: 'list', providers, credentials }); return; } @@ -166,9 +307,17 @@ export function codedeckPiAuthHelperSource(): string { return ( '// Written by CodeDeck+ — manages Pi accounts with Pi\'s own code. Regenerated\n' + '// whenever it changes; do not edit.\n' + + "import * as fs from 'node:fs';\n" + + "import * as os from 'node:os';\n" + + "import * as path from 'node:path';\n" + + "import net from 'node:net';\n" + + "import tls from 'node:tls';\n" + + "import http from 'node:http';\n" + + "import https from 'node:https';\n" + + "import childProcess from 'node:child_process';\n" + 'const pi = await import(process.argv[2]);\n' + `const main = ${codedeckPiAuthHelper.toString()};\n` + - 'main(pi);\n' + 'main(pi, { fs, os, path, net, tls, http, https, childProcess });\n' ); } diff --git a/packages/agent-host/src/drivers/pi/driver.ts b/packages/agent-host/src/drivers/pi/driver.ts index 8945d215..79734cbd 100644 --- a/packages/agent-host/src/drivers/pi/driver.ts +++ b/packages/agent-host/src/drivers/pi/driver.ts @@ -32,6 +32,7 @@ import type { Driver, DriverSession, SessionContext, SessionMcpState } from '../ import type { ProviderHttp } from '../../sdk/net'; import { OPENAI_API, checkToken, listModels } from '../../sdk/providerApi'; import type { EndpointModel } from '../../sdk/providerModels'; +import { redactSecrets } from '../../sdk/redact'; import { now, PERMISSION_ALLOW, PERMISSION_DENY, toolLocations, unknownToolReporter } from '../../sdk/tools'; import { truncateToolResult } from '../../sdk/transcript'; import { PartialCoalescer } from '../../sdk/partial'; @@ -244,6 +245,11 @@ export class PiSession implements DriverSession { private readonly env: Record; /** The bound profile's token, for Pi's token pipe; never in `env`. */ private readonly token: string | undefined; + /** Every credential value this session was handed. What Pi says when + * something fails — its stderr, a provider's error reply — can quote + * them (an endpoint echoing the header it refused), and it travels to + * the log and the phone, so each such text goes through `safe` first. */ + private readonly secrets: string[]; /** The running process once it accepts commands; rejected if the session * ends first. An option changed while Pi starts waits on it, rather than * being dropped as if it applied. */ @@ -263,6 +269,7 @@ export class PiSession implements DriverSession { this.started.catch(() => {}); this.env = buildPiEnv(params, deps.home, deps.baseEnv); this.token = piProviderToken(params); + this.secrets = [...Object.values(params.credentials ?? {}), ...Object.values(params.env ?? {}), ...(this.token !== undefined ? [this.token] : [])]; this.reportUnknownTool = unknownToolReporter((message) => ctx.log(message)); this.coalescer = new PartialCoalescer((update) => { if (!this.ended) this.ctx.emit({ type: 'partial', ...update }); @@ -297,13 +304,14 @@ export class PiSession implements DriverSession { onEvent: (record) => this.event(record), onUiRequest: (record) => this.uiRequest(record), onExit: (code, signal) => this.exited(code, signal), - onStderr: (line) => { + onStderr: (raw) => { + const line = this.safe(raw); this.lastStderr = line; if (PI_SESSION_MISSING.test(line)) this.sessionMissing = true; this.deps.log(`[pi] ${line}`); }, onBadLine: (line, reason) => { - this.deps.log(`[pi] dropped a line from Pi (${reason})${line ? `: ${line.slice(0, 200)}` : ''}`); + this.deps.log(`[pi] dropped a line from Pi (${this.safe(reason)})${line ? `: ${this.safe(line).slice(0, 200)}` : ''}`); }, ...(this.deps.spawnFn !== undefined ? { spawnFn: this.deps.spawnFn } : {}), }); @@ -430,11 +438,11 @@ export class PiSession implements DriverSession { if (record.success === false) { // Pi gave up: its final error is the turn's, said once. this.heldError = undefined; - this.emit([{ entryType: 'error', text: str(record.finalError) ?? 'Pi gave up after retrying', timestamp: now() }]); + this.emit([{ entryType: 'error', text: this.safe(str(record.finalError) ?? 'Pi gave up after retrying'), timestamp: now() }]); } return; case 'extension_error': - this.deps.log(`[pi] an extension failed: ${String(record.error ?? record.event ?? 'unknown')}`); + this.deps.log(`[pi] an extension failed: ${this.safe(String(record.error ?? record.event ?? 'unknown'))}`); return; default: return; @@ -480,7 +488,7 @@ export class PiSession implements DriverSession { // shown: Pi may retry the request (`auto_retry_start` follows), and a // failed attempt that a later one recovers from is not the turn's error. for (const index of assistant?.parts.keys() ?? []) this.coalescer.complete(`${assistant!.key}:${index}`); - this.heldError = errorMessage ?? 'Pi reported an error'; + this.heldError = this.safe(errorMessage ?? 'Pi reported an error'); return; } // An aborted response is the user's own interrupting: its partial content @@ -679,14 +687,21 @@ export class PiSession implements DriverSession { /** Pi will try the failed request again: one status line says so, in * place of the attempt's error. */ + /** `text` without any credential this session was handed. */ + private safe(text: string): string { + return redactSecrets(text, this.secrets); + } + private retrying(record: PiRecord): void { this.heldError = undefined; const attempt = num(record.attempt); const of = num(record.maxAttempts); const delay = num(record.delayMs); // The first line, briefly: a provider's error can be a page of JSON, and - // the full text is the error entry's if Pi gives up. - const firstLine = str(record.errorMessage)?.split('\n')[0]; + // the full text is the error entry's if Pi gives up. Redacted before it + // is cut: a cut secret would no longer be found. + const message = str(record.errorMessage); + const firstLine = message === undefined ? undefined : this.safe(message).split('\n')[0]; const why = firstLine !== undefined && firstLine.length > 120 ? `${firstLine.slice(0, 119)}…` : firstLine; const when = delay !== undefined ? ` in ${Math.max(1, Math.round(delay / 1000))}s` : ''; const count = attempt !== undefined && of !== undefined ? ` (${attempt} of ${of})` : ''; @@ -752,7 +767,7 @@ export class PiSession implements DriverSession { this.openUi.add(id); void this.answerUi(id, method, record) .catch((error: unknown) => { - this.deps.log(`[pi] could not answer a dialog: ${error instanceof Error ? error.message : String(error)}`); + this.deps.log(`[pi] could not answer a dialog: ${this.safe(error instanceof Error ? error.message : String(error))}`); this.respond({ type: 'extension_ui_response', id, cancelled: true }); }) .finally(() => this.openUi.delete(id)); diff --git a/packages/agent-host/src/drivers/pi/rpc.ts b/packages/agent-host/src/drivers/pi/rpc.ts index 400468f0..2053317f 100644 --- a/packages/agent-host/src/drivers/pi/rpc.ts +++ b/packages/agent-host/src/drivers/pi/rpc.ts @@ -69,8 +69,10 @@ export class PiLineDecoder { let value: unknown; try { value = JSON.parse(line); - } catch (error) { - this.onBadLine(line, error instanceof Error ? error.message : String(error)); + } catch { + // Not the parser's message: it quotes the line's start, cut where a + // secret in it could no longer be recognised. The line goes whole. + this.onBadLine(line, 'not JSON'); continue; } if (typeof value !== 'object' || value === null || Array.isArray(value)) {