/** * Session manager — mirrors `edge-client/src/session/manager.rs`. * * Owns PTY shell sessions + SFTP sessions + Pi RPC sessions. * Keys are session_id UUIDs. Implements the request lifecycle: * - Create a session: assign a UUID session_id, call underlying manager. * - Incoming frames from binary WS routed by session_id. * - Heartbeat metadata (activeSessionCount, shellSessionCount, piSessionCount). * - Cleanup on edge-client shutdown (kill all child processes, flush SFTP). */ import { ShellManager } from './shell.js'; import type { FrameTypeValue } from './shell-frame.js'; import { SftpForwarder } from './sftp.js'; import { PiProcess } from './pi-rpc.js'; import type { ResourcesConfig, StreamMessage } from './pi-rpc.js'; import { SessionIdStore } from './persistence.js'; /** Session type discrimination. */ export type SessionType = 'shell' | 'sftp' | 'pi'; /** Heartbeat metadata. Mirrors Rust SessionManager heartbeat fields. */ export interface HeartbeatMetadata { readonly activeSessionCount: number; readonly shellSessionCount: number; readonly piSessionCount: number; } /** Callback for heartbeat metadata updates. */ export type HeartbeatHandler = (metadata: HeartbeatMetadata) => void; /** Options for the session manager. */ export interface SessionManagerOptions { readonly machineId: string; readonly shellEnabled: boolean; readonly piPath?: string; readonly resources?: ResourcesConfig; /** * Owner operator id — forwarded to PiProcess so synthetic audit events * (cmd_gate_denied, extension_ui_request, fail_fast_*) carry the * operator attribution. FIX 2026-07-14, G-9. */ readonly operatorId?: string; } export declare class SessionManager { private readonly machineId; private readonly piPath; private readonly resources; private readonly operatorId; /** Active sessions keyed by session_id. */ private readonly sessions; /** Shell manager (PTY sessions). */ private readonly shell; /** SFTP forwarder (file browser RPC). */ private readonly sftp; /** Pi processes keyed by agent_id (not session_id). */ private readonly piProcesses; /** Persistence store for pi session IDs. */ private readonly sessionIdStore; /** Event emitter for outgoing messages. */ private readonly emitter; constructor(opts: SessionManagerOptions); /** * Open a new shell session. Mirrors Rust `ShellManager::open` lifecycle. * Returns the assigned session_id. */ openShellSession(cols: number, rows: number, shell?: string): Promise; /** * Handle a shell-related frame from the binary WS. * Routes to the appropriate underlying manager. */ handleShellFrame(frameType: FrameTypeValue, sessionId: string, payload: Buffer): void; /** * Handle an SFTP frame from the binary WS. * Delegates to the SFTP forwarder. */ handleSftpFrame(sessionId: string, frameType: number, payload: Buffer): void; /** * Get or create a Pi process for an agent_id. * Mirrors Rust `SessionManager::get_or_create_session`. */ getOrCreatePiSession(agentId: string): Promise; /** * Forward a prompt to a Pi process for agent_id. * Mirrors Rust `SessionManager::execute_command`. */ executeCommand(agentId: string, msgId: string, prompt: string): Promise; /** * Forward extension_ui_response to a Pi process. * Mirrors Rust `SessionManager::forward_extension_ui_response`. */ forwardExtensionUIResponse(agentId: string, requestId: string, response: Record): Promise; /** * Switch model for a Pi process. * Mirrors Rust `SessionManager::set_model`. */ setModel(agentId: string, provider: string, modelId: string): Promise; /** * Set thinking level for a Pi process. * Mirrors Rust `SessionManager::set_thinking_level`. */ setThinkingLevel(agentId: string, level: string): Promise; /** * Trigger compaction for a Pi process. * Mirrors Rust `SessionManager::compact`. */ compact(agentId: string): Promise; /** * Forward get_available_models to a Pi process. * Mirrors Rust `SessionManager::get_available_models`. */ getAvailableModels(agentId: string, id: string): Promise; /** * Terminate a Pi session for agent_id. * Mirrors Rust `SessionManager::terminate_session`. */ terminateSession(agentId: string): Promise; /** * Terminate all Pi sessions. Mirrors Rust `SessionManager::terminate_all`. */ terminateAll(): Promise; /** Get current heartbeat metadata. */ heartbeatMetadata(): HeartbeatMetadata; /** Register a heartbeat handler. */ onHeartbeat(handler: HeartbeatHandler): void; private emitHeartbeat; /** * Close all sessions (edge shutdown). Mirrors Rust `ShellManager::close_all` * + `SessionManager::terminate_all`. */ closeAll(): Promise; /** Subscribe to raw wire frames from shell sessions. */ onWireFrame(handler: (frame: Buffer) => void): void; /** Subscribe to SFTP response frames. */ onSftpFrame(handler: (sessionId: string, frameType: number, payload: Buffer) => void): void; /** Subscribe to pi events (StreamMessage). */ onPiEvent(handler: (msg: StreamMessage) => void): void; /** Subscribe to pi exit events. */ onPiExit(handler: (agentId: string) => void): void; /** Subscribe to shell exit events. */ onShellExited(handler: (sessionId: string, code: number) => void): void; /** Get the shell manager (for binary WS wiring). */ getShellManager(): ShellManager; /** Get the SFTP forwarder (for binary WS wiring). */ getSftpForwarder(): SftpForwarder; /** Get the session ID store (for persistence). */ getSessionIdStore(): SessionIdStore; }