@phren/cli
Version:
Knowledge layer for AI agents - CLI, MCP server, and data layer
162 lines (161 loc) • 7.09 kB
TypeScript
import { type IncomingMessage, type ServerResponse } from "node:http";
import { type Json } from "./protocol.js";
import { type AskpassDelivery, type ProcessRecord, type SudoReply, type SudoSession } from "./sudo.js";
/**
* A secret typed on the phone, handed to one command's stdin.
*
* `phren secret-exec [--prompt <text>] [--plist <key> [--field K=V]...] -- <command...>`
* starts the command with a fresh pipe on its stdin, then runs the Hook's
* askpass script (`<bridge>/askpass --secret-exec`) with its stdout on that
* same pipe and closes its own copy. The askpass helper asks the Hook over
* agent.sock, as `sudo -A`'s does, and writes what the phone sends (one line,
* or a plist with the secret under `<key>`) into the pipe. The agent that ran
* secret-exec never holds the pipe, so the secret reaches the command and
* nothing else: not the agent's stdout, stderr, logs or transcript.
*
* Before asking the phone the Hook checks the chain (`verifySecretAsker`): the
* helper is this Hook's node running its bundle as `askpass --secret-exec`
* with nothing that loads other code, its parent is the installed script,
* that script has exactly one sibling (the command), and the helper's stdout
* is a pipe that no process of the owner's reads except that command and its
* descendants. The connection is bound to the helper the same way sudo's is.
* The Hook reads the command line itself (never what the caller claims) and
* the phone shows it, the prompt, the stdin format and the session asking,
* and needs an approval per run. The secret is never logged, stored or kept
* past the write. Only phones that subscribe to `secrets` frames can answer
* (an older app never sees these requests); with none open the request is
* refused at once.
*/
export declare const SECRET_FLAG = "--secret-exec";
export declare const MAX_SECRET_PENDING = 4;
export declare const SECRET_SPEC_ENV = "PHREN_SECRET_SPEC";
/** How the secret is written to the command's stdin. */
export interface SecretSpec {
prompt?: string;
format: "line" | "plist";
key?: string;
fields?: Record<string, string>;
}
/** A spec from the helper's request, or undefined when anything is off. */
export declare function secretSpec(value: unknown): SecretSpec | undefined;
/** What the command reads on stdin. */
export declare function secretPayload(spec: SecretSpec, secret: string): string;
/** A secret as the phone may send it: one line, nothing a listener would cut. */
export declare const validSecret: (value: unknown) => value is string;
/** What the phone sees. Never carries the secret. */
export interface SecretRequestView {
id: string;
kind: "secret";
computer: string;
/** The command that will read the secret, as the Hook read it from the process table. */
command: string;
/** The text the agent gave, shown as the agent's words, not the system's. */
prompt?: string;
stdin: {
format: "line";
} | {
format: "plist";
key: string;
fields?: Record<string, string>;
};
account?: string;
cwd?: string;
session?: SudoSession;
askedAt: string;
expiresAt: string;
}
/** Every process's parent, from `ps`. */
export declare function processTable(): Promise<Map<number, number>>;
export declare const childrenIn: (table: Map<number, number>, parent: number) => number[];
export declare function descendantsIn(table: Map<number, number>, root: number): Set<number>;
/** The owner's processes, other than `exclude`, that hold the pipe on `pid`'s
* stdout (on macOS, its read end). Undefined when stdout is no pipe or the
* Hook cannot tell. Processes of other users (root) are never listed. */
export declare function pipeHolders(pid: number, exclude: number[]): Promise<number[] | undefined>;
export interface SecretCheck {
processes: (pid: number) => Promise<ProcessRecord | undefined>;
table: () => Promise<Map<number, number>>;
holders: (pid: number, exclude: number[]) => Promise<number[] | undefined>;
connection: (pid: number) => Promise<"asker" | "other" | "unknown">;
node: string;
bundles: string[];
script: string;
}
/** The chain from the helper to the one command it feeds, or why it is refused. */
export declare function verifySecretAsker(pid: number, check: SecretCheck): Promise<{
command: ProcessRecord;
commandPid: number;
} | {
refused: string;
}>;
export interface SecretVerified {
command: ProcessRecord;
deliver: (secret: string) => Promise<SudoReply>;
}
/** Whether, now, only the command and what it started hold the helper's stdout. */
export declare function stillOnlyTheCommand(pid: number, commandPid: number, check?: Pick<SecretCheck, "processes" | "table" | "holders">): Promise<boolean>;
export interface SecretBrokerOptions {
computer: () => string;
describe?: (place: {
server: string;
workspace: string;
tab: string;
pane: string;
}) => Promise<Pick<SudoSession, "source" | "label"> | undefined>;
label?: (dispatchId: string) => Promise<string | undefined>;
verify?: (pid: number, hookFd: number | undefined, delivery?: AskpassDelivery) => Promise<SecretVerified | {
refused: string;
}>;
holdMs?: number;
account?: string;
now?: () => number;
}
export declare class SecretBroker {
private options;
private pending;
private events;
private watchers;
constructor(options: SecretBrokerOptions);
list(): SecretRequestView[];
/** Overview sockets that asked for `secrets` frames; each is a phone that can answer. */
subscribe(listener: (requests: SecretRequestView[]) => void): () => void;
private changed;
ask(body: Json, respond: (reply: SudoReply) => void, hookFd?: number): Promise<{
cancel: () => void;
}>;
/** The phone's answer, single use. */
answer(id: string, answer: {
secret: string;
} | {
deny: true;
}): boolean;
close(): void;
/** The agent.sock `/secret` route. */
handle(req: IncomingMessage, res: ServerResponse): Promise<void>;
}
/** `POST /v1/secrets/answer`. Checked by hand: a schema error could echo the value it refused. */
export declare function secretAnswer(data: Json): {
id: string;
answer: {
secret: string;
} | {
deny: true;
};
};
export declare class SecretAnswerError extends Error {
constructor();
}
/** The helper, `askpass --secret-exec`: asks the Hook, writes the payload to
* stdout (the command's stdin pipe), and wipes its copy. */
export declare function secretAskpass(env?: NodeJS.ProcessEnv): Promise<number>;
export declare const SECRET_EXEC_USAGE: string;
/** Parsed `phren secret-exec` arguments. */
export declare function secretExecArgs(args: string[]): {
spec: SecretSpec;
command: string[];
};
/** `phren secret-exec`. Exits with the command's status, or 1 when the
* secret was not given (the command is stopped then). */
export declare function runSecretExec(args: string[], options?: {
script?: string;
}): Promise<number>;