import { SessionManager } from "@caupulican/pi-agent-core/session"; import type { AgentSession } from "./agent-session.ts"; import type { AgentSessionRuntimeDiagnostic, AgentSessionServices } from "./agent-session-services.ts"; import type { ReplacedSessionContext, SessionStartEvent } from "./extensions/index.ts"; import type { CreateAgentSessionResult } from "./sdk.ts"; /** * Result returned by runtime creation. * * The caller gets the created session, its cwd-bound services, and all * diagnostics collected during setup. */ export interface CreateAgentSessionRuntimeResult extends CreateAgentSessionResult { services: AgentSessionServices; diagnostics: AgentSessionRuntimeDiagnostic[]; } /** Session-owned background runtime that must not survive a session replacement. */ export interface AgentSessionRuntimeResource { start(session: AgentSession): Promise; stop(): Promise | void; } /** Internal activation hooks used by the interactive session-resume flow. */ export interface AgentSessionSwitchOptions { cwdOverride?: string; /** Runs against the installed replacement after host rebind and before supervisors restart. */ beforeSessionResourcesStart?: (session: AgentSession) => Promise | void; withSession?: (ctx: ReplacedSessionContext) => Promise; } /** * Creates a full runtime for a target cwd and session manager. * * The factory closes over process-global fixed inputs, recreates cwd-bound * services for the effective cwd, resolves session options against those * services, and finally creates the AgentSession. */ export type CreateAgentSessionRuntimeFactory = (options: { cwd: string; agentDir: string; sessionManager: SessionManager; sessionStartEvent?: SessionStartEvent; }) => Promise; /** * Thrown when /import references a JSONL file path that does not exist. */ export declare class SessionImportFileNotFoundError extends Error { readonly filePath: string; constructor(filePath: string); } /** A teardown or activation failure at the replacement boundary, including rollback outcome. */ export declare class SessionReplacementRuntimeError extends Error { readonly phase: "teardown" | "activation"; readonly recovered: boolean; readonly transitionError: unknown; readonly recoveryError: unknown | undefined; constructor(phase: "teardown" | "activation", transitionError: unknown, recovered: boolean, recoveryError?: unknown); } /** A post-commit extension callback failed; the replacement itself remains the active session. */ export declare class SessionReplacementCallbackError extends Error { readonly replacementCommitted = true; readonly callbackError: unknown; constructor(callbackError: unknown); } /** * Owns the current AgentSession plus its cwd-bound services. * * Session replacement methods construct the candidate runtime before retiring * the current one. If preparation fails, the working session remains active. */ export declare class AgentSessionRuntime { private rebindSession?; private beforeSessionInvalidate?; private _session; private _services; private readonly createRuntime; private _diagnostics; private _modelFallbackMessage?; private _replacementInFlight; private _disposeInFlight; private _disposeStarted; private _disposed; private readonly disposeProgress; private readonly sessionResources; constructor(_session: AgentSession, _services: AgentSessionServices, createRuntime: CreateAgentSessionRuntimeFactory, _diagnostics?: AgentSessionRuntimeDiagnostic[], _modelFallbackMessage?: string); get services(): AgentSessionServices; get session(): AgentSession; get cwd(): string; get diagnostics(): readonly AgentSessionRuntimeDiagnostic[]; get modelFallbackMessage(): string | undefined; /** True from replacement admission until rebind and replacement callbacks have settled. */ get isReplacing(): boolean; setRebindSession(rebindSession?: (session: AgentSession) => Promise): void; /** * Set a synchronous callback that runs after `session_shutdown` handlers finish * but before the current session is invalidated. * * This is for host-owned UI teardown that must not yield to the event loop, * such as detaching extension-provided TUI components before the old extension * context becomes stale. */ setBeforeSessionInvalidate(beforeSessionInvalidate?: () => void): void; /** Register a resource whose lifecycle follows the active session (supervisors, watchers). */ registerSessionResource(resource: AgentSessionRuntimeResource): void; private emitBeforeSwitch; private emitBeforeFork; private teardownCurrent; private discardPrepared; private removeCreatedSessionFile; private prepareReplacement; private commitReplacement; private snapshot; private apply; private activateReplacement; private restorePreviousAfterRuntimeFailure; private shutdownRuntime; private runSessionReplacement; switchSession(sessionPath: string, options?: AgentSessionSwitchOptions): Promise<{ cancelled: boolean; }>; private switchSessionUnserialized; newSession(options?: { parentSession?: string; setup?: (sessionManager: SessionManager) => Promise; withSession?: (ctx: ReplacedSessionContext) => Promise; }): Promise<{ cancelled: boolean; }>; private newSessionUnserialized; fork(entryId: string, options?: { position?: "before" | "at"; withSession?: (ctx: ReplacedSessionContext) => Promise; }): Promise<{ cancelled: boolean; selectedText?: string; }>; private forkUnserialized; /** * Import a session JSONL file and switch runtime state to the imported session. * * @returns `{ cancelled: true }` when cancelled by `session_before_switch`, otherwise `{ cancelled: false }`. * @throws {SessionImportFileNotFoundError} When the input path does not exist. * @throws {MissingSessionCwdError} When the imported session cwd cannot be resolved and no override is provided. */ importFromJsonl(inputPath: string, cwdOverride?: string): Promise<{ cancelled: boolean; }>; private importFromJsonlUnserialized; dispose(): Promise; } /** * Create the initial runtime from a runtime factory and initial session target. * * The same factory is stored on the returned AgentSessionRuntime and reused for * later /new, /resume, /fork, and import flows. */ export declare function createAgentSessionRuntime(createRuntime: CreateAgentSessionRuntimeFactory, options: { cwd: string; agentDir: string; sessionManager: SessionManager; sessionStartEvent?: SessionStartEvent; }): Promise; export { type AgentSessionRuntimeDiagnostic, type AgentSessionServices, type CreateAgentSessionFromServicesOptions, type CreateAgentSessionServicesOptions, createAgentSessionFromServices, createAgentSessionServices, } from "./agent-session-services.ts"; //# sourceMappingURL=agent-session-runtime.d.ts.map