Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 12 additions & 4 deletions docs/PI.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,18 @@ pointed there (`PI_CODING_AGENT_DIR=<that directory>`): accounts, MCP
servers and packages added either way are there for both. Conversations are files under its `sessions/`, which is what a session
resumes from after a restart and what deleting a conversation removes.

A profile's token never touches a file: the driver hands the session's
process the profile in one environment variable and the token in another,
which the registration names (`$CODEDECK_PI_PROVIDER_TOKEN`), so Pi reads
the token from its own environment.
A profile's token never touches a file, nor Pi's environment: the profile
travels in an environment variable, the token on a pipe of its own (file
descriptor 3), which the CodeDeck extension reads once as Pi starts and
registers as the profile's key. Everything Pi starts — the model's shells,
background shells, sub-agents, MCP and language servers — inherits Pi's
environment, and on Linux any process of the same user can read a process's
starting environment back from `/proc`, so a token there would be the
model's to print. A sub-agent's Pi gets the token on its own pipe the same
way. An operator's own Pi executable that wraps Pi must hand file
descriptor 3 on to it; one that does not leaves the profile's requests
without a key (Pi's error says so, and the bridge log has the extension's
line naming the missing token).

Pi runs with `--offline`, version checks and telemetry off: it makes no
network request of its own beyond the model's. Its model catalog is therefore
Expand Down
20 changes: 19 additions & 1 deletion packages/agent-host/src/drivers/pi/__tests__/fakePi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ import { EventEmitter } from 'node:events';
interface FakeStdin extends EventEmitter {
destroyed: boolean;
write(chunk: string): boolean;
end(): void;
end(chunk?: string): void;
}

export interface FakeChild extends EventEmitter {
stdout: EventEmitter;
stderr: EventEmitter;
stdin: FakeStdin;
/** stdin, stdout, stderr and the provider token's pipe (fd 3). */
stdio: [FakeStdin, EventEmitter, EventEmitter, FakeStdin];
pid: number;
kill(signal?: string): boolean;
}
Expand Down Expand Up @@ -78,6 +80,10 @@ export class FakePi {
readonly uiResponses: Array<Record<string, unknown>> = [];
/** Dialogs answered by allow/deny, by their call id. */
readonly answers = new Map<string, string>();
/** What the driver wrote on the token pipe (fd 3), and whether it closed
* it — as the extension reads it, to the end. */
token = '';
tokenClosed = false;
private readonly options: FakePiOptions;
private nextCall = 0;
private pendingDialog: { uiId: string; call: ScriptedCall } | undefined;
Expand Down Expand Up @@ -106,6 +112,18 @@ export class FakePi {
},
}) as FakeStdin;
child.stdin = stdin;
const tokenPipe = Object.assign(new EventEmitter(), {
destroyed: false,
write: (chunk: string): boolean => {
this.token += chunk;
return true;
},
end: (chunk?: string): void => {
if (chunk !== undefined) this.token += chunk;
this.tokenClosed = true;
},
}) as FakeStdin;
child.stdio = [stdin, child.stdout, child.stderr, tokenPipe];
child.pid = 4242;
child.kill = (): boolean => {
if (!ended) {
Expand Down
23 changes: 17 additions & 6 deletions packages/agent-host/src/drivers/pi/__tests__/piDriver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface Harness {
driver: PiDriver;
fake: FakePi;
home: string;
spawns: Array<{ command: string; args: string[]; env: Record<string, string>; cwd: string }>;
spawns: Array<{ command: string; args: string[]; env: Record<string, string>; cwd: string; stdio: string[] }>;
logs: string[];
session?: DriverSession;
}
Expand All @@ -42,8 +42,8 @@ function withDriver(options: { fake?: FakePiOptions; killTree?: (pid: number) =>
runtime,
home,
baseEnv: {},
spawnFn: ((command: string, args: string[], opts: { env: Record<string, string>; cwd: string }) => {
spawns.push({ command, args, env: opts.env, cwd: opts.cwd });
spawnFn: ((command: string, args: string[], opts: { env: Record<string, string>; cwd: string; stdio: string[] }) => {
spawns.push({ command, args, env: opts.env, cwd: opts.cwd, stdio: opts.stdio });
return fake.child;
}) as never,
...(options.killTree ? { killTree: options.killTree } : {}),
Expand Down Expand Up @@ -181,7 +181,7 @@ describe('PiSession startup', () => {
expect(ended).not.toHaveProperty('resumeLost');
});

it('binds a provider profile through the environment, token kept out of the profile JSON', async () => {
it("binds a provider profile through the environment, and hands its token over on Pi's token pipe only", async () => {
const h = withDriver();
await ready(h, {
model: 'fake-model-1',
Expand All @@ -197,8 +197,19 @@ describe('PiSession startup', () => {
expect(spawn.args[spawn.args.indexOf('--model') + 1]).toBe('codedeck-fake/fake-model-1');
const profile = JSON.parse(spawn.env.CODEDECK_PI_PROVIDER!) as Record<string, unknown>;
expect(profile).toMatchObject({ id: 'codedeck-fake', label: 'Fake', baseUrl: 'https://api.example.com/v1' });
expect(JSON.stringify(profile)).not.toContain('sk-secret');
expect(spawn.env.CODEDECK_PI_PROVIDER_TOKEN).toBe('sk-secret');
// Nowhere in the environment, under any name: everything Pi starts
// inherits it.
expect(JSON.stringify(spawn.env)).not.toContain('sk-secret');
expect(spawn.stdio).toHaveLength(4);
expect(h.fake.token).toBe('sk-secret');
expect(h.fake.tokenClosed).toBe(true);
});

it('opens no token pipe for a session without a profile', async () => {
const h = withDriver();
await ready(h);
expect(h.spawns[0]!.stdio).toHaveLength(3);
expect(h.fake.token).toBe('');
});

it('refuses a provider profile with an insecure endpoint before it starts', () => {
Expand Down
64 changes: 49 additions & 15 deletions packages/agent-host/src/drivers/pi/__tests__/piExtension.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* The CodeDeck Pi extension: the module the driver writes into Pi's home. It
* must gate every tool call and register a bound provider profile with the
* token read from the environment; and the text the host carries must import
* token read from its pipe; and the text the host carries must import
* nothing Pi does not provide.
*/
import { mkdtempSync, readFileSync, writeFileSync } from 'node:fs';
Expand All @@ -11,7 +11,7 @@ import { describe, expect, it } from 'vitest';
import { bundledPiExtension, installPiExtension, PI_PERMISSION_PREFIX, piExtensionPath } from '../extension';
import type { CatalogModel } from '../extension/catalog';
import { installGate } from '../extension/gate';
import { registerProfile } from '../extension/provider';
import { configLiteral, readProviderToken, registerProfile } from '../extension/provider';
import { installShellCheck } from '../extension/shells';
import { subagentTools, taskResult } from '../extension/task';

Expand Down Expand Up @@ -55,6 +55,9 @@ describe('the provider profile', () => {
function registered(
env: Record<string, string | undefined>,
catalog: () => CatalogModel[] = () => [],
/** `null`: no token arrived. */
token: string | null = 'sk-value',
warnings: string[] = [],
): { id: string; config: Record<string, unknown> } | undefined {
let provider: { id: string; config: Record<string, unknown> } | undefined;
registerProfile(
Expand All @@ -64,31 +67,62 @@ describe('the provider profile', () => {
}) as never,
},
env,
token ?? undefined,
catalog,
(line) => warnings.push(line),
);
return provider;
}

it('registers a bound profile at the endpoint the driver resolved, with the token reference', () => {
const provider = registered({
CODEDECK_PI_PROVIDER: JSON.stringify({
id: 'codedeck-fake',
label: 'Fake',
baseUrl: 'https://api.example.com/v1',
models: [{ id: 'fake-model-1', label: 'Fake model', contextWindow: 64_000 }],
}),
CODEDECK_PI_PROVIDER_TOKEN: 'sk-value',
});
const FAKE_PROFILE = JSON.stringify({
id: 'codedeck-fake',
label: 'Fake',
baseUrl: 'https://api.example.com/v1',
models: [{ id: 'fake-model-1', label: 'Fake model', contextWindow: 64_000 }],
});

it('registers a bound profile at the endpoint the driver resolved, with the token as its key', () => {
const provider = registered({ CODEDECK_PI_PROVIDER: FAKE_PROFILE });
expect(provider?.id).toBe('codedeck-fake');
expect(provider?.config).toMatchObject({
name: 'Fake',
baseUrl: 'https://api.example.com/v1',
api: 'openai-completions',
apiKey: '$CODEDECK_PI_PROVIDER_TOKEN',
apiKey: 'sk-value',
models: [expect.objectContaining({ id: 'fake-model-1', name: 'Fake model', contextWindow: 64_000 })],
});
// The secret itself never reaches the registration.
expect(JSON.stringify(provider)).not.toContain('sk-value');
});

it("escapes a token Pi would read as a variable or a command (a real Pi resolves it: piFakeModel)", () => {
expect(configLiteral('sk-plain')).toBe('sk-plain');
expect(configLiteral('sk$HOME')).toBe('sk$$HOME');
expect(configLiteral('$$')).toBe('$$$$');
expect(configLiteral('!cat x')).toBe('$!cat x');
expect(configLiteral('!a$b')).toBe('$!a$$b');
});

it('registers a profile whose token did not arrive keyless, and says so', () => {
const warnings: string[] = [];
const provider = registered({ CODEDECK_PI_PROVIDER: FAKE_PROFILE }, () => [], null, warnings);
expect(provider?.config).not.toHaveProperty('apiKey');
expect(warnings).toEqual([expect.stringContaining("no token arrived for the provider profile 'codedeck-fake'")]);
});

it("reads the token from its pipe only for a Pi started with a profile, and a pipe that is not there gives none", () => {
const fds: number[] = [];
const read = (fd: number): string => {
fds.push(fd);
return 'sk-piped';
};
expect(readProviderToken({}, read)).toBeUndefined();
expect(fds).toEqual([]);
expect(readProviderToken({ CODEDECK_PI_PROVIDER: FAKE_PROFILE }, read)).toBe('sk-piped');
expect(fds).toEqual([3]);
const missing = (): string => {
throw Object.assign(new Error('bad file descriptor'), { code: 'EBADF' });
};
expect(readProviderToken({ CODEDECK_PI_PROVIDER: FAKE_PROFILE }, missing)).toBeUndefined();
expect(readProviderToken({ CODEDECK_PI_PROVIDER: FAKE_PROFILE }, () => '')).toBeUndefined();
});

it('takes what Pi knows of a model, through a gateway route or a vendor prefix', () => {
Expand Down
69 changes: 66 additions & 3 deletions packages/agent-host/src/drivers/pi/__tests__/piFakeModel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,16 @@ interface FakeModelServer {
stop(): Promise<void>;
}

/**
* The provider profile's token. The model server refuses any other, so a
* turn that runs proves Pi sent this one. It holds what Pi's configuration
* syntax would read as a command (a leading `!`) and as a variable (`$`), so
* it arrives intact only if the extension's escaping matches Pi's resolver.
*/
const PROFILE_TOKEN = '!codedeck$fake-token';

function startFakeModel(timeoutMs = 15_000): Promise<FakeModelServer> {
const child = spawn(process.execPath, [MODEL_SERVER, '--port', '0'], { stdio: ['ignore', 'pipe', 'pipe'] });
const child = spawn(process.execPath, [MODEL_SERVER, '--port', '0', '--key', PROFILE_TOKEN], { stdio: ['ignore', 'pipe', 'pipe'] });
return new Promise((resolve, reject) => {
let output = '';
const timer = setTimeout(() => {
Expand Down Expand Up @@ -137,7 +145,7 @@ live('Pi against the fake model', () => {
id: 'codedeck-fake',
label: 'Fake model',
baseUrl: modelUrl,
authToken: 'codedeck-fake-model',
authToken: PROFILE_TOKEN,
models: [{ id: FAKE_MODEL_ID, label: 'Fake model' }],
defaultModel: FAKE_MODEL_ID,
},
Expand Down Expand Up @@ -353,6 +361,61 @@ live('Pi against the fake model', () => {
TURN_MS,
);

/**
* A shell command that writes its own environment to `env-<name>.txt` and,
* where there is a /proc, the initial environment of the Pi that started it
* to `pi-environ-<name>.txt` — readable by any process of the same user,
* and unchanged by a variable Pi deletes later. Shell builtins only: the
* session's environment has no PATH (`baseEnv: {}`).
*/
const dumpEnvironment = (name: string): string =>
`export -p > env-${name}.txt; ` +
`if [ -r /proc/$PPID/environ ]; then while IFS= read -r -d '' v; do echo "$v"; done < /proc/$PPID/environ > pi-environ-${name}.txt; fi; true`;

/** What a dump holds; the token must be nowhere in it. */
function readDump(file: string): string {
const text = readFileSync(path.join(cwd, file), 'utf8');
// The dump is the real thing: Pi's own environment, not an empty file.
expect(text, file).toContain('PI_CODING_AGENT_DIR');
return text;
}

it(
"keeps the profile's token out of every process the model starts: its shell, a background shell, and Pi's own environment",
async () => {
const ctx = start({ permission: () => ({ outcome: 'selected', optionId: 'allow' }) });
await turn(ctx, `/tool ${dumpEnvironment('shell')}`);
await turn(ctx, `/background ${dumpEnvironment('background')}`);
await ctx.waitFor(
(event) => event.type === 'entries' && event.entries.some((entry) => entry.entryType === 'background_task' && entry.status === 'completed'),
TURN_MS,
);

for (const name of ['shell', 'background']) {
expect(readDump(`env-${name}.txt`)).not.toContain(PROFILE_TOKEN);
if (process.platform === 'linux') expect(readDump(`pi-environ-${name}.txt`)).not.toContain(PROFILE_TOKEN);
}
// No output shown on the phone carries it either.
expect(JSON.stringify(ctx.events)).not.toContain(PROFILE_TOKEN);
},
TURN_MS,
);

it(
"gives a sub-agent's Pi the token on its pipe, and keeps it out of the sub-agent's shell",
async () => {
const ctx = start({ permission: () => ({ outcome: 'selected', optionId: 'allow' }) });
await turn(ctx, `/task /tool ${dumpEnvironment('subagent')}`);

// The sub-agent's own requests carried the token: the model server
// refuses any other, and its command ran.
expect(readDump('env-subagent.txt')).not.toContain(PROFILE_TOKEN);
if (process.platform === 'linux') expect(readDump('pi-environ-subagent.txt')).not.toContain(PROFILE_TOKEN);
expect(JSON.stringify(ctx.events)).not.toContain(PROFILE_TOKEN);
},
TURN_MS,
);

it(
"sizes a gateway's model from Pi's own catalog, which the extension reads inside Pi",
async () => {
Expand All @@ -368,7 +431,7 @@ live('Pi against the fake model', () => {
id: 'codedeck-fake',
label: 'Fake gateway',
baseUrl: modelUrl,
authToken: 'codedeck-fake-model',
authToken: PROFILE_TOKEN,
models: [{ id: routed }],
defaultModel: routed,
},
Expand Down
12 changes: 6 additions & 6 deletions packages/agent-host/src/drivers/pi/__tests__/piLsp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { describe, expect, it } from 'vitest';
import { mergeDiagnostics, type DiagnosticsResult, type SpawnServer } from '../extension/lsp/client';
import { changedFile, formatDiagnostics, LSP_ENV, MAX_REPORTED, ProjectServers, registerDiagnostics } from '../extension/lsp/diagnostics';
import { serverFor, type Machine } from '../extension/lsp/servers';
import { PROVIDER_TOKEN_ENV } from '../extension/provider';
import { PROVIDER_ENV } from '../extension/provider';

const FAKE_SERVER = fileURLToPath(new URL('./fakeLanguageServer.mjs', import.meta.url));

Expand Down Expand Up @@ -295,13 +295,13 @@ describe('diagnostics after a change, per kind of server', () => {
expect(killed).toHaveLength(1);
});

it("starts the server without the provider profile's token", async () => {
const p = project('pull', { env: { [PROVIDER_TOKEN_ENV]: 'sk-secret', HOME_MARK: 'kept' } });
it('starts the server without the provider profile', async () => {
const p = project('pull', { env: { [PROVIDER_ENV]: '{"id":"p"}', HOME_MARK: 'kept' } });
try {
expect(await p.change(`ENV ${PROVIDER_TOKEN_ENV}\nENV HOME_MARK\n`)).toBe(
`rust-analyzer reports 2 errors in main.rs:\nmain.rs:1:1 error E1: ${PROVIDER_TOKEN_ENV} is unset\nmain.rs:2:1 error E1: HOME_MARK is set`,
expect(await p.change(`ENV ${PROVIDER_ENV}\nENV HOME_MARK\n`)).toBe(
`rust-analyzer reports 2 errors in main.rs:\nmain.rs:1:1 error E1: ${PROVIDER_ENV} is unset\nmain.rs:2:1 error E1: HOME_MARK is set`,
);
expect(p.env()?.[PROVIDER_TOKEN_ENV]).toBeUndefined();
expect(p.env()?.[PROVIDER_ENV]).toBeUndefined();
} finally {
p.servers.stopAll();
}
Expand Down
19 changes: 17 additions & 2 deletions packages/agent-host/src/drivers/pi/__tests__/piMemory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function tempDir(prefix = 'codedeck-pi-memory-'): string {
return dir;
}

function harness(env: Record<string, string | undefined> = {}): {
function harness(env: Record<string, string | undefined> = {}, secrets?: string[]): {
home: string;
run: (cwd: string, params: Record<string, unknown>) => Promise<ToolResult>;
section: (cwd: string) => string | undefined;
Expand All @@ -57,7 +57,7 @@ function harness(env: Record<string, string | undefined> = {}): {
if (event === 'before_agent_start') hook = handler;
}) as never,
},
{ home, env },
{ home, env, ...(secrets ? { secrets } : {}) },
);
return {
home,
Expand Down Expand Up @@ -264,6 +264,21 @@ describe('the memory tool', () => {
expect(saved.content[0]!.text).toContain("Saved memory 'ordinary'");
});

it("refuses the provider profile's token, which is in no variable", async () => {
const token = 'sk-profile-token-value';
const memory = harness({}, [token]);
const message = await refusal(memory.run, tempDir(), {
action: 'save',
name: 'leak',
description: 'A note about the endpoint.',
content: `Use ${token} for the gateway.`,
});
expect(message).toBe(
"memory refused: the note looks like it contains a secret (the provider profile's token). Save where the secret lives, never the secret itself.",
);
expect(message).not.toContain(token);
});

it('keeps two working directories apart, even with the same basename', async () => {
const memory = harness();
const root = tempDir();
Expand Down
Loading
Loading