@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
90 lines • 4.58 kB
TypeScript
/**
* The restore orchestrator: `restoreTerminalState` replays the manifest after a restart. Every
* monitor is classified up front (expired, ephemeral with or without time left, durable) and the
* durable and still-live ephemeral ones are handed to their kind's handler, all concurrently.
* The result carries per-monitor outcomes and reasons, background sessions told apart as still
* running or exited, and an upper bound on how long the session was detached.
*/
import type { SidecarStore } from "../../../session-sidecar-store.ts";
import type { ChildProcessIdentity } from "./process-identity.ts";
import type { ManifestBackgroundSession, ManifestMonitor, TerminalManifest } from "./terminal-manifest.ts";
export { InvalidTerminalManifestError, parseTerminalManifest } from "./terminal-manifest-parse.ts";
export type RestoreOutcome = "restored" | "lost" | "muted" | "completed" | "attachedElsewhere";
export interface RestoreHandlerResult {
readonly outcome: RestoreOutcome;
/** Why a monitor could not come back (or what its restore did), shown per monitor in the digest. */
readonly reason?: string;
/** The watcher a crash left running: killed before the respawn, or left alone when unverifiable. */
readonly orphan?: {
readonly pid: number;
readonly action: "killed" | "unverified";
};
/** The re-spawned process, so the manifest can find it after the next crash. */
readonly runtime?: ChildProcessIdentity;
}
export interface RestoreContext {
/** Upper bound on how long the session was detached (never an exact gap). */
readonly downtimeMs: number;
/** Time left for an ephemeral watch whose deadline has not passed; undefined for durable ones. */
readonly remainingMs?: number;
}
export type RestoreHandler = (monitor: ManifestMonitor, context: RestoreContext) => RestoreHandlerResult | Promise<RestoreHandlerResult>;
export interface RestoreHandlers {
readonly "restartable-command": RestoreHandler;
readonly "checkpointed-file": RestoreHandler;
readonly "checkpointed-rpc": RestoreHandler;
}
/** The registry surface a restore handler needs to re-apply a persisted mute. */
export interface PersistedMuteRegistry {
pause(ids: readonly string[]): string[];
}
/**
* Re-apply a persisted `deliveryPaused` mute to a freshly restored monitor and report the
* outcome it contributes to the digest. The mute MUST be applied by the FRESH runtime id
* (bash_N/watch_N) the restore just allocated: `MonitorRegistry.pause` resolves records by
* runtime id only, so passing the persisted `mon_` id silently no-ops and the mute is lost.
*/
export declare function reapplyPersistedMute(registry: PersistedMuteRegistry, monitor: Pick<ManifestMonitor, "deliveryPaused">, runtimeId: string): RestoreOutcome;
/** Stub handlers: every monitor is reported lost until a caller plugs the real ones in. */
export declare const stubRestoreHandlers: RestoreHandlers;
export type MonitorRestoreOutcome = RestoreOutcome | "expired";
export interface MonitorRestoreResult {
readonly monitorId: string;
readonly description: string;
readonly kind: ManifestMonitor["runtimeKind"];
readonly outcome: MonitorRestoreOutcome;
readonly command?: string;
readonly path?: string;
readonly reason?: string;
readonly orphan?: RestoreHandlerResult["orphan"];
}
export interface BackgroundRestoreResult {
readonly id: string;
readonly command: string;
readonly outcome: "running" | "exited";
readonly pid?: number;
}
export interface RestoreDigest {
restored: number;
lost: number;
expired: number;
muted: number;
completed: number;
attachedElsewhere: number;
storeError: boolean;
readonly results: readonly MonitorRestoreResult[];
readonly backgroundSessions: readonly BackgroundRestoreResult[];
/** Upper bound on how long the session was detached; 0 when nothing was persisted. */
readonly downtimeMs: number;
}
export interface RestoreTerminalStateOptions {
readonly manifest: SidecarStore<TerminalManifest>;
readonly handlers?: Partial<RestoreHandlers>;
readonly now?: () => number;
/** The session transcript; its last entry written before this process started bounds the downtime. */
readonly sessionFile?: string;
readonly processStartedAtMs?: number;
readonly isBackgroundAlive?: (session: ManifestBackgroundSession) => Promise<boolean>;
}
export declare function restoreTerminalState(options: RestoreTerminalStateOptions): Promise<RestoreDigest>;
//# sourceMappingURL=restore.d.ts.map