import type { SdkStartupFailure, SdkStartupRollbackResult } from "../startup-capability"; import { SDK_STATE_VERSION } from "./state-version"; export type LifecycleState = "accepted" | "effect_started" | "awaiting_ready" | "terminal_ok" | "terminal_error" | "terminal_uncertain"; export interface LifecycleWorktreeIntent { repoRoot: string; worktreePath: string; detached: boolean; baseRef: string; branchName?: string; } export interface LifecycleEffectIntent { sessionId: string; stateRoot: string; childOwnershipEstablished?: boolean; worktree?: LifecycleWorktreeIntent; } /** Durable lifecycle effects retained for exact replay; never implies rollback authority. */ export interface LifecycleCleanupProof { processExited: true; endpointRemoved: true; hostUnregistered: { state: "unregistered"; indexSeq: number; lifecycleRequestId?: string; } | { state: "not_registered"; }; rollback: { endpointGeneration: number | null; fenced: true; runtimeRemoved: true; hostStopped: true; brokerRegistrationReleased: true; }; } export interface LifecycleStartupFailureReceipt extends SdkStartupFailure { artifactDigest: string; rollback: SdkStartupRollbackResult; cleanupProof?: LifecycleCleanupProof; } export interface LifecycleDurableEffectsReceipt { worktree?: { cwdDigest: string; created: boolean; reused: boolean; branchDigest?: string; }; transcript?: { identityDigest: string; contentDigest: string; }; startup?: LifecycleStartupFailureReceipt; digest?: string; } export interface LifecycleLedgerEntry { operationKey?: string; fingerprint?: string; version: typeof SDK_STATE_VERSION; identity: string; requestHash: string; state: LifecycleState; intendedSessionId?: string; resultSessionId?: string; effectMarker?: string; effectIntent?: LifecycleEffectIntent; durableEffects?: LifecycleDurableEffectsReceipt; startupFailure?: LifecycleStartupFailureReceipt; endpointGeneration?: number; responseDigest?: string; response?: unknown; unresolvedCleanupResponse?: unknown; unresolvedCleanupResponseDigest?: string; uncertainCleanupSessionId?: string; uncertainCleanupSessionIds?: string[]; uncertainCleanupAllSessions?: true; ts: number; } export type BeginResult = { kind: "new"; entry: LifecycleLedgerEntry; } | { kind: "replay"; entry: LifecycleLedgerEntry; } | { kind: "idempotency_conflict"; } | { kind: "terminal_uncertain"; entry: LifecycleLedgerEntry; } | { kind: "in_progress"; entry: LifecycleLedgerEntry; }; export interface LifecycleLedgerLimits { maxBytes?: number; maxLineBytes?: number; maxRows?: number; /** * Cap for the `.corrupt` quarantine sidecar. Reaching it rotates one * generation aside instead of appending forever. */ maxCorruptBytes?: number; } export declare class LifecycleLedger { #private; constructor(agentDir: string, limits?: LifecycleLedgerLimits); open(): Promise; /** * Reads the durable terminal record for one request without recovering or changing the ledger. * * This intentionally accepts an unrelated unterminated final write: concurrent * appenders may have started a different row after this request's synced terminal * row. Complete rows are still decoded and validated strictly, and any incomplete * tail that identifies this request withholds proof. * * `terminal_uncertain` is a durable terminal record too: an operation that concluded * uncertainly persists that conclusion, and refusing to read it back would report a * synced row as unpersisted and replace its true reason with a generic one. */ readTerminal(identity: string, requestHash: string): Promise; findByOperationKey(operationKey: string): LifecycleLedgerEntry | undefined; /** * Legacy target-inclusive rows predate the operation/key index. Their opaque * identities cannot establish that a different target is safe, so callers * must reject rather than create a second admission. */ hasLegacyIdentity(): boolean; migrateIdentity(from: string, to: string, metadata: { operationKey: string; fingerprint: string; }): Promise; get warnings(): readonly string[]; begin(identity: string, requestHash: string, metadata?: { operationKey?: string; fingerprint?: string; }): Promise; transition(identity: string, state: LifecycleState, fields?: Omit, "identity" | "requestHash" | "state" | "ts">): Promise; assertSupportedStateVersions(): Promise; findCleanupPendingByDeleteTarget(target: { sessionId: string; sessionsRoot?: string; transcriptPath: string; cwd: string; }, excludingIdentity: string): LifecycleLedgerEntry | undefined; findCleanupPendingBySessionId(sessionId: string, excludingIdentity: string): LifecycleLedgerEntry | undefined; hasUncertainCleanupForSession(sessionId: string, excludingIdentity: string): boolean; get(identity: string): LifecycleLedgerEntry | undefined; findPendingCleanupByTarget(sessionId: string, cwd: string, transcriptPath: string): LifecycleLedgerEntry | undefined; }