/** * Session recovery and reconsumption after agent disconnection. * * Enables agents to rebuild session context from persisted tracker files * (.md and .json) combined with the OpenCode SDK REST API. Follows D-05: * on plugin load, reads `project-continuity.json` to initialize the session * map. When an agent reconnects, normal hook flow resumes — this module * provides the gap-filling methods for missed messages. * * All methods are best-effort: they catch errors internally and return * partial results rather than throwing. * * @module session-tracker/recovery/session-recovery */ import type { OpenCodeClient } from "../../../shared/session-api.js"; import type { ProjectSessionEntry } from "../types.js"; /** * Result of a reconsumption operation comparing persisted file * content with live SDK messages to identify gaps. */ export interface ReconsumptionResult { /** The session identifier that was reconsumed. */ sessionID: string; /** Messages present in the SDK but missing from the persisted file. */ missingMessages: unknown[]; /** Messages present in the persisted file. */ persistedMessages: string[]; /** Total message count from SDK. */ totalSdkMessages: number; /** Total turn count observed in the persisted file. */ totalPersistedTurns: number; } /** * Rebuilt context combining persisted file content with SDK messages. */ export interface SessionContext { /** The session identifier. */ sessionID: string; /** Raw content of the persisted .md file. */ fileContent: string | null; /** Messages retrieved from the SDK. */ messages: unknown[]; } /** * Recovers session context after agent disconnection. * * Reads persisted session tracker files and combines them with * live SDK data to provide gap analysis and context rebuilding. */ export declare class SessionRecovery { private client; private projectRoot; /** * @param deps - Injected dependencies. * @param deps.client - The OpenCode SDK client for REST API queries. * @param deps.projectRoot - Absolute path to the project root. */ constructor(deps: { client: OpenCodeClient; projectRoot: string; }); /** * Initializes recovery by reading `project-continuity.json` and building * an in-memory session map. * * Called once during plugin startup per D-05. This is initialization, * not a separate recovery phase — normal hook flow handles ongoing capture. * * @returns A `Map` of session IDs to their project-level metadata entries. * Returns an empty map if the index file is missing or corrupt. */ initialize(): Promise>; /** * Compares persisted session file content with SDK messages to identify * gaps in the captured data. * * Uses `client.session.messages()` to retrieve the full message history * from the OpenCode runtime, then compares it against the content of the * persisted `.md` file to detect messages that were missed. * * @param sessionID - The session identifier to reconsumer. * @returns Gap analysis result, or partial data if SDK call fails. */ reconsumeSession(sessionID: string): Promise; /** * Rebuilds the full session context by combining persisted file content * with SDK message data. * * @param sessionID - The session identifier to rebuild. * @returns The combined session context for agent reconsumption. */ rebuildSessionContext(sessionID: string): Promise; /** * Checks whether a session file is parseable and structurally valid. * * Used to detect incomplete files after a crash. Because all writes use * atomic rename (D-03), incomplete files should not exist in normal * operation. However, this provides an additional safety check. * * @param filePath - Absolute path to the session file. * @returns `true` if the file exists and is structurally valid. */ isSessionFileParseable(filePath: string): Promise; /** * Reads and parses `project-continuity.json` from the session tracker root. * * @returns The parsed project index, or `null` if missing or corrupt. */ private readProjectIndex; /** * Reads the persisted session `.md` file content using safe path construction. * * Applies path safety via `safeSessionPath()` and input validation via * `isValidSessionID()` before ANY path operations (CR-01). * * @param sessionID - The session identifier. * @returns The file content, or `null` if the file is missing. */ private readSessionFile; /** * Reads either a main-session markdown file or a root-owned child JSON file. * * Child sessions are registered in project continuity with `mainFile` ending * in `.json` and `dir` pointing at their root main directory. Recovery must * honor that index instead of assuming every session has its own `.md` file. * * @param sessionID - Session identifier to read. * @returns Rendered persisted context, or null when missing. */ private readPersistedSessionContent; /** * Reads a child session JSON file using project-continuity path metadata. * * @param sessionID - Child session identifier to read. * @returns Rendered child context, or null when the index has no child entry. */ private readChildContextFromProjectIndex; /** * Reads child `.json` records owned by a root main session and renders them * into append-only recovery markdown. * * @param rootSessionID - Root main session whose directory owns child files. * @returns Markdown context for child turns, journeys, and last messages. */ private readRootOwnedChildContext; /** * Renders a child session record into recovery markdown. * * @param record - Child session record loaded from disk. * @returns Markdown summary preserving full child context fields. */ private renderChildContext; } //# sourceMappingURL=session-recovery.d.ts.map