/** * Session persistence — mirrors `edge-client/src/session/persistence.rs`. * * B5 — PROCESS-restart persistence for the `(agent_id → pi session_id)` map. * * `SessionManager` previously kept this map in-memory only; when the edge-client * daemon restarted (systemd/crash/deploy) the map was lost and pi was respawned * fresh (history gone), even though pi's session files on disk are intact. * We persist the map to `~/.config/edge-ai-agent/sessions.json` so a restarted * edge-client can resume the same pi session via `--session-id`. * * ## Design * - The inner map is guarded by a mutex. * - `getOrInit` for an EXISTING agent_id is a fast map lookup (no factory, no I/O). * - `save` runs only when a NEW id is assigned. The snapshot is cloned under * the lock and the atomic write happens outside the lock so concurrent lookups * are not stalled on I/O. * * ## Atomic write * Write to `.tmp` then rename over `` (same-dir rename is atomic * on POSIX and on Windows since Node 14+). * * ## Encrypted at rest (optional) * Uses AES-256-GCM via `src/crypto/aes-gcm.ts` with the buffer_key from * `loadBufferKey(settings)` when provided. */ /** Session persistence options. */ export interface PersistenceOptions { /** Path to the sessions.json file. Defaults to `/sessions.json`. */ readonly filePath?: string; /** If true, persist to disk. If false, in-memory only (for tests). */ readonly persist?: boolean; /** AES-256-GCM key for encryption at rest (optional). */ readonly encryptionKey?: Uint8Array; } export declare class SessionIdStore { private readonly map; private readonly filePath; private readonly persist; private readonly encryptionKey; constructor(opts?: PersistenceOptions); /** * Get the session ID for `agent_id`, or initialize via `factory` + persist. * * Returns the existing ID if present (fast path: one map lookup, no factory). * Otherwise calls `factory()` (which generates a UUID), inserts it, and * persists to disk (best-effort). * * Mirrors Rust `SessionIdStore::get_or_init`. */ getOrInit(agentId: string, factory: () => string): Promise; /** * Atomic write to disk. Best-effort; logs a warning on error and never throws. * No-op for the in-memory variant. * * FIX (2026-07-14, F-22): also writes `.sig` containing the * HMAC-SHA256 of the serialized map, keyed off `loadDeviceSecret()`. If * `device_secret` is unset (no relay-issued secret yet), the sidecar is * SKIPPED — migration: first save seeds both files. On load, if the * sidecar is present and the HMAC mismatches, the map is treated as * empty (defense-in-depth; operator can inspect manually). * * Mirrors Rust `SessionIdStore::save` + `persistence.rs::hmac_session`. */ save(): Promise; /** * Load the map from disk. Missing/corrupt file → empty map. * * FIX (2026-07-14, F-22): verify HMAC sidecar if present. Tampering * (or rotating device_secret without re-mint) → empty map + warn. * Migration: missing sidecar but file present → seed it on next save. * * Mirrors Rust `SessionIdStore::load` + `persistence.rs::verify_session`. */ private loadFromDisk; /** * Check if an agent_id has a stored session ID. */ has(agentId: string): boolean; /** * Get the stored session ID for an agent_id, or undefined. */ get(agentId: string): string | undefined; /** * Get the number of stored sessions. */ size(): number; /** * Create an in-memory only store (for tests). No file I/O. */ static inMemory(): SessionIdStore; } /** Session state to persist. Mirrors Rust session persistence fields. */ export interface SessionState { /** Working directory for the session. */ readonly cwd: string; /** Environment variables for the session. */ readonly env: Record; /** Agent ID associated with this session. */ readonly agentId: string; /** Timestamp when the session was opened (Unix epoch ms). */ readonly openedAt: number; } /** * Persist full session state to disk (encrypted at rest). * Mirrors Rust persistence.rs session state persistence. * * Uses AES-256-GCM encryption when a key is provided. */ export declare function persistSessionState(state: SessionState, filePath: string, encryptionKey?: Uint8Array): void; /** * Load session state from disk (decrypted if key provided). * Returns undefined if the file doesn't exist or is corrupt. */ export declare function loadSessionState(filePath: string, encryptionKey?: Uint8Array): SessionState | undefined;