/** * Pi RPC integration — spawns the `pi` coding-agent CLI. * Mirrors `edge-client/src/session/pi_rpc.rs`. * * Edge-client is a transport layer: forward prompt to pi stdin, forward * ALL events from pi stdout to the buffer queue, handle buffering/ACK/reconnect. * * ## systemd-run wrapping (Phase 4 #3) * When `systemd-run --user --scope` is available AND resource limits are set, * pi is spawned under a per-agent transient scope that enforces MemoryMax / * CPUQuota / TasksMax via Linux cgroups. * * ## Session continuity (GAP-017/P5) * Each agent_id gets a stable pi session ID. On respawn, the same ID is reused * so pi reopens its session file (conversation context survives). */ import * as child_process from 'node:child_process'; import type { CmdGateConfig } from '../config/cmd-gate.js'; /** RPC command sent to pi stdin. Mirrors Rust RpcCommand. */ export interface RpcCommand { /** Correlation id (= msg_id of cloud command). */ readonly id?: string; /** Command type: "prompt" | "abort" | "set_model" | etc. */ readonly type: string; /** Prompt message. */ readonly message?: string; /** Streaming behavior: "steer" | "followUp". */ readonly streamingBehavior?: string; /** Additional fields (provider, modelId, level, etc.). */ readonly [key: string]: unknown; } /** Event received from pi stdout. Mirrors Rust PiEvent. */ export interface PiEvent { /** Event type: "agent_start" | "agent_end" | "tool_execution_start" | etc. */ readonly type: string; /** All other fields from pi (flatten passthrough). */ readonly [key: string]: unknown; } import type { StreamMessage } from '../network/ws-client.js'; export type { StreamMessage }; export interface ResourcesConfig { readonly memory_max_mb: number; readonly cpu_quota_percent: number; readonly tasks_max: number; } /** True if any resource limit is set (non-zero). Mirrors Rust has_limits(). */ export declare function hasLimits(config: ResourcesConfig): boolean; /** * Probe whether `systemd-run` is available. Caches the result (probe once * per process). Returns false on non-Linux or if `systemd-run` isn't on PATH. * Mirrors Rust `systemd_run_available()`. */ export declare function systemdRunAvailable(): boolean; /** * Reset the systemd-run probe cache (for testing). * @internal */ export declare function resetSystemdRunCache(): void; /** * Build the wrapped program + arg vector for a pi spawn, optionally under * a per-agent `systemd-run --user --scope` transient unit. * * When `wrap` is false, returns `(piPath, originalArgs)` unchanged. * When `wrap` is true, returns `("systemd-run", ["--user", "--scope", ...])`. * * Pure function — no I/O, fully unit-testable. */ export declare function wrapWithCgroup(piPath: string, originalArgs: readonly string[], agentId: string, resources: ResourcesConfig, wrap: boolean): { program: string; args: string[]; }; export interface PiProcessOptions { readonly agentId: string; readonly piPath: string; readonly machineId: string; readonly resources: ResourcesConfig; /** Stable pi session ID for GAP-017/P5 session continuity. */ readonly resumeSessionId?: string; /** Path override for spawning (default: child_process.spawn). */ readonly spawnFn?: typeof child_process.spawn; /** * AGT-03 cmd-gate config (FIX 2026-07-14, F-12). When supplied, every * `tool_execution_start` event is evaluated; `Deny` aborts the tool call * and `RequireApproval` emits a synthetic extension_ui_request before * forwarding. Mirrors `edge-client/src/session/pi_rpc.rs` integration. */ readonly cmdGate?: CmdGateConfig; /** * Owner operator id — attached to synthetic audit events (cmd_gate_denied, * extension_ui_request, fail_fast_*) so the dashboard can attribute * decisions to operators across the fleet. FIX 2026-07-14, G-9. * Optional because older call sites pass without it. */ readonly operatorId?: string; } export declare class PiProcess { private readonly agentId; private readonly machineId; private readonly emitter; private child; private alive; private streaming; private readonly sessionId; private parseErrors; private currentMsgId; private readonly cmdGate; private readonly operatorId; constructor(opts: PiProcessOptions); /** * Spawn pi process. Mirrors Rust `PiProcess::spawn`. * Returns a promise that resolves when the process is spawned. */ spawn(opts: PiProcessOptions): Promise; private handleStdoutLine; private extractEventMsgId; /** Send a command to pi stdin. */ private writeCommand; /** Send a prompt command. Mirrors Rust PiProcess::send_prompt. */ sendPrompt(msgId: string, message: string, streamingBehavior?: string): void; /** Send an abort command. Mirrors Rust PiProcess::send_abort. */ sendAbort(): void; /** Forward extension_ui_response to pi. */ sendExtensionUIResponse(requestId: string, response: Record): void; /** Switch LLM model. Mirrors Rust PiProcess::set_model. */ setModel(provider: string, modelId: string): void; /** Set thinking level. Mirrors Rust PiProcess::set_thinking_level. */ setThinkingLevel(level: string): void; /** Manual context compaction. Mirrors Rust PiProcess::compact. */ compact(): void; /** Get available models. Mirrors Rust PiProcess::get_available_models. */ getAvailableModels(id: string): void; /** Is the agent currently streaming (between agent_start and agent_end)? */ isStreaming(): boolean; /** Is the pi process still alive (stdout not EOF)? GAP-003 health check. */ isAlive(): boolean; /** Number of unparseable stdout lines (Q4 GAP-020 health counter). */ getParseErrorCount(): number; /** Captured pi session ID (GAP-017/P5). */ getPersistedSessionId(): string | undefined; /** Register a handler for StreamMessage events. */ onEvent(handler: (msg: StreamMessage) => void): void; /** Register a handler for exit events. */ onExit(handler: (agentId: string) => void): void; /** Register a handler for stderr log lines. */ onStderr(handler: (line: string) => void): void; /** * Graceful shutdown: send abort, close stdin, kill child. * Mirrors Rust PiProcess::terminate. */ terminate(): void; private buildEnvContext; } export declare function promptCommand(id: string, message: string): RpcCommand; export declare function promptWithStreamingCommand(id: string, message: string, behavior: string): RpcCommand; export declare function abortCommand(): RpcCommand; export declare function setModelCommand(provider: string, modelId: string): RpcCommand; export declare function setThinkingLevelCommand(level: string): RpcCommand; export declare function compactCommand(): RpcCommand; export declare function getAvailableModelsCommand(id: string): RpcCommand; /** * Generate a pi-compatible session ID (RFC 4122 v4 UUID). * Mirrors Rust `new_session_id()`. */ export declare function newPiSessionId(): string; /** * Extract pi sessionId from event payload. * Mirrors Rust `extract_session_id`. */ export declare function extractSessionId(eventType: string, payload: Record): string | undefined; /** * Resolve msg_id for a forwarded event. Mirrors Rust `event_msg_id`. * Prefers pi's own id in payload, falls back to currentMsgId. */ export declare function eventMsgId(payload: Record, fallback: string): string;