import { StateDatabase } from "../state/database.js"; import { type JsonValue } from "../state/json.js"; import { type MutationActor, type OwnerType } from "../state/mutation.js"; import { type InitialWorkflowSettingsScope, type WorkflowFollowUpQueueRecord, type WorkflowFollowUpRecord, type WorkflowQueueFollowUpRequest, type WorkflowRemoveFollowUpRequest, type WorkflowSettingsChangeRequest, type WorkflowSettingsChangeResult, type WorkflowSettingsDefinition, type WorkflowSettingsScopeRecord } from "./settings.js"; import type { WorkflowDefinition, WorkflowDefinitionSnapshot, WorkflowEffectRecovery, WorkflowEffectReservation, WorkflowRunState, WorkflowStepRecord, WorkflowSessionBinding, WorkflowSessionCapture, WorkflowSessionEntryRecord, WorkflowSessionEventRecord, WorkflowTraceEvent, WorkflowUpdateInput, WorkflowUpdateRecord } from "./types.js"; export declare const RUN_STATE_SCHEMA: "pi-workflows.run-state.v1"; export declare const DEFINITION_SNAPSHOT_SCHEMA: "pi-workflows.definition-snapshot.v1"; export declare const SESSION_BINDING_SCHEMA: "pi-workflows.session-binding.v1"; export declare const SESSION_EVENT_SCHEMA: "pi-workflows.session-event.v1"; export declare const SESSION_CAPTURE_SCHEMA: "pi-workflows.session-capture.v1"; export declare const SESSION_EVENT_MAX_BYTES: number; export declare function workflowStateDatabasePath(homeDir?: string): string; export declare function createRunId(workflowName: string, now?: Date): string; export type RunWriteAuthority = { actor: MutationActor; ownerType: OwnerType; ownerId: string; token: string; generation: number; /** Duration used when a protected write renews this exact live claim. */ leaseMs?: number; }; export type WorkflowRunDisplayState = { status: WorkflowRunState["status"]; paused: boolean; error: string | null; }; export type WorkflowRunTerminalData = { runId: string; status: "completed" | "failed" | "timed_out" | "cancelled"; statusDetail: string | null; input: JsonValue; finalOutput: JsonValue | null; error: string | null; restartNumber: number; }; export type WorkflowRunStoreOptions = { authorityProvider?: (runId: string) => RunWriteAuthority | undefined; /** Server-owned projection updates that must commit with each run snapshot. */ snapshotLifecycle?: (context: { runId: string; state: WorkflowRunState; event: WorkflowTraceEvent; database: StateDatabase; now: number; }) => void; /** The global server may record an attested Pi session after the runner lease ends. */ allowServerSessionRecording?: boolean; state?: StateDatabase; readOnly?: boolean; }; /** The state boundary used by workflow code, including out-of-process runners. */ import { type WorkflowCheckpointState } from "./requests.js"; import { type WorkflowTransition } from "./transitions.js"; export interface WorkflowExecutionStore { readonly databasePath: string; initializeRun(workflow: WorkflowDefinition, state: WorkflowRunState, options?: InitializeWorkflowRunOptions): Promise; prepareRunResume(runId: string): Promise; readRunState(runId: string): WorkflowRunState | null | Promise; commitTransition(runId: string, transition: WorkflowTransition): Promise; publishUpdate(runId: string, nodeId: string, attemptId: string, update: WorkflowUpdateInput, options?: { signal?: AbortSignal; }): Promise<{ event: WorkflowTraceEvent; record: WorkflowUpdateRecord; }>; findSettingsScope(runId: string, mountPath: string, invocation: number): WorkflowSettingsScopeRecord | undefined | Promise; ensureSettingsScope(options: { runId: string; mountPath: string; invocation: number; settings: JsonValue; }): WorkflowSettingsScopeRecord | Promise; getSettingsScopeAtChange(scopeId: string, changeNumber: number): WorkflowSettingsScopeRecord | undefined | Promise; readCheckpoint(runId: string, attemptId: string): Promise; reserveEffect(options: { runId: string; attemptId: string; effectType: string; idempotencyKey: string; request: JsonValue; recovery: WorkflowEffectRecovery; }): Promise; settleEffect(options: { runId: string; effectId: string; attemptNumber: number; outcome: "applied" | "rejected" | "ambiguous" | "cancelled"; result?: JsonValue; error?: string; }): Promise; } export type SessionCaptureIntegrity = { status: "unavailable" | "recording" | "complete" | "failed" | "invalid"; diagnostics: string[]; }; export type SessionCaptureSegment = { attemptId: string; binding: WorkflowSessionBinding | null; entries: WorkflowSessionEntryRecord[]; events: WorkflowSessionEventRecord[]; capture: WorkflowSessionCapture | null; integrity: SessionCaptureIntegrity; }; export type LoadedWorkflowRun = { runId: string; state: WorkflowRunState; snapshot: WorkflowDefinitionSnapshot; traceEvents?: WorkflowTraceEvent[]; sessionBinding: WorkflowSessionBinding | null; sessionEntries: WorkflowSessionEntryRecord[]; sessionEvents: WorkflowSessionEventRecord[]; sessionCapture: WorkflowSessionCapture | null; sessionIntegrity: SessionCaptureIntegrity; sessionSegments: SessionCaptureSegment[]; settingsScopes?: WorkflowSettingsScopeRecord[]; followUpQueue?: WorkflowFollowUpQueueRecord | null; }; export type WorkflowRunViewCounts = { steps: number; trace: number; sessionEntries: number; sessionEvents: number; settings: number; followUps: number; updates: number; }; export type WorkflowRunViewRange = { start: number; limit: number; }; export type WorkflowRunViewRead = { runId: string; state: WorkflowRunState; snapshot: WorkflowDefinitionSnapshot; traceEvents: WorkflowTraceEvent[]; sessionBinding: WorkflowSessionBinding | null; sessionEntries: WorkflowSessionEntryRecord[]; sessionEvents: WorkflowSessionEventRecord[]; sessionCapture: WorkflowSessionCapture | null; sessionIntegrity: SessionCaptureIntegrity; settingsScopes: WorkflowSettingsScopeRecord[]; followUpQueue: WorkflowFollowUpQueueRecord | null; graphSteps: WorkflowStepRecord[]; takenTransitions: string[]; }; export type WorkflowRunViewReadOptions = { steps: WorkflowRunViewRange; trace: WorkflowRunViewRange; sessionEntries: WorkflowRunViewRange; sessionEvents: WorkflowRunViewRange; settings: WorkflowRunViewRange; followUps: WorkflowRunViewRange; updates: WorkflowRunViewRange; graphCursor: number; }; export type ReadWorkflowRunOptions = { includeTrace?: boolean; }; export type InitializeWorkflowRunOptions = { initialSettings?: InitialWorkflowSettingsScope[]; }; export declare class WorkflowRunStore { readonly databasePath: string; readonly state: StateDatabase; private readonly authorityProvider; private readonly snapshotLifecycle; private readonly allowServerSessionRecording; private readonly contexts; private readonly ownsState; private readonly mutations; private readonly workflowMessages; constructor(databasePath?: string, options?: WorkflowRunStoreOptions); close(): void; /** Server-only synchronization after a queue lifecycle mutation on the run resource. */ synchronizeRevision(runId: string): number; readCheckpoint(runId: string, attemptId: string): Promise; reserveEffect(options: { runId: string; attemptId: string; effectType: string; idempotencyKey: string; request: JsonValue; recovery: WorkflowEffectRecovery; }): Promise; settleEffect(options: { runId: string; effectId: string; attemptNumber: number; outcome: "applied" | "rejected" | "ambiguous" | "cancelled"; result?: JsonValue; error?: string; }): Promise; recoverApplyingEffects(runId: string): Promise<"safe" | "ambiguous">; private beginEffectAttempt; private insertEffectAttempt; initializeRun(workflow: WorkflowDefinition, state: WorkflowRunState, options?: InitializeWorkflowRunOptions): Promise; initializeRunFromSnapshot(snapshot: WorkflowDefinitionSnapshot, workflowName: string, state: WorkflowRunState, options?: InitializeWorkflowRunOptions): Promise; ensureSettingsScope(options: { runId: string; mountPath: string; invocation: number; settings: JsonValue; }): WorkflowSettingsScopeRecord; getSettingsScope(scopeId: string): WorkflowSettingsScopeRecord | undefined; getSettingsScopeAtChange(scopeId: string, changeNumber: number): WorkflowSettingsScopeRecord | undefined; listSettingsScopes(runId: string): WorkflowSettingsScopeRecord[]; private settingsScopesForRunView; private readSettingsScopeRange; findSettingsScope(runId: string, mountPath: string, invocation: number): WorkflowSettingsScopeRecord | undefined; changeSettings(definition: WorkflowSettingsDefinition, request: WorkflowSettingsChangeRequest): Promise; queueFollowUp(request: WorkflowQueueFollowUpRequest): { followUp: WorkflowFollowUpRecord; adopted: boolean; }; removeFollowUp(request: WorkflowRemoveFollowUpRequest): WorkflowFollowUpRecord; originSessionId(runId: string): string | undefined; readFollowUpQueue(runId: string): WorkflowFollowUpQueueRecord | undefined; private readFollowUpQueueRange; prepareRunResume(runId: string): Promise; markRunInterrupted(runId: string, reason?: string): Promise; publishUpdate(runId: string, nodeId: string, attemptId: string, update: WorkflowUpdateInput, options?: { signal?: AbortSignal; }): Promise<{ event: WorkflowTraceEvent; record: WorkflowUpdateRecord; }>; /** Server-only synchronous form for an already serialized command transaction. */ publishUpdateSynchronous(runId: string, nodeId: string, attemptId: string, update: WorkflowUpdateInput, options?: { signal?: AbortSignal; }): { event: WorkflowTraceEvent; record: WorkflowUpdateRecord; }; commitTransition(runId: string, transition: WorkflowTransition): Promise; hasSessionBinding(runId: string): Promise; listSessionSegments(runId: string): Promise; writeSessionBinding(runId: string, binding: WorkflowSessionBinding, attemptId?: string): Promise; appendSessionEntry(runId: string, entry: Record, attemptId?: string): Promise; appendSessionEventBatch(runId: string, records: WorkflowSessionEventRecord[], attemptId?: string): Promise; private recordSessionReplayCheckpoints; private sessionEventRecords; private viewerSessionCapture; writeSessionCapture(runId: string, capture: WorkflowSessionCapture, attemptId?: string): Promise; sessionCounts(runId: string, attemptId?: string): Promise<{ eventCount: number; entryCount: number; lastEventSeq: number; }>; readRunInput(runId: string): JsonValue | null; readRunFinalOutput(runId: string): JsonValue | null; readRunError(runId: string): string | null; readTerminalData(runId: string): WorkflowRunTerminalData | null; readRunState(runId: string): WorkflowRunState | null; runRevision(runId: string): number; readRun(runId: string, options?: ReadWorkflowRunOptions): LoadedWorkflowRun | null; readRunViewCounts(runId: string): WorkflowRunViewCounts | null; readRunView(runId: string, options: WorkflowRunViewReadOptions): WorkflowRunViewRead | null; traceCursorForStep(runId: string, stepCursor: number, traceTotal: number): number; persistViewContent(runId: string, content: Buffer, mediaType: string): string; readContentBlob(runId: string, digest: string, mediaType: string): { mediaType: string; content: Buffer; } | undefined; readDisplayState(runId: string): WorkflowRunDisplayState | null; readSessionReplayCheckpoint(runId: string, throughSequence: number): JsonValue | null; listRuns(options?: ReadWorkflowRunOptions): LoadedWorkflowRun[]; readLastTraceEvent(runId: string): WorkflowTraceEvent | null; private contextFor; private withRunLock; private assertSessionWriteAuthority; private resourceRevision; private requireResourceRevision; private bumpResource; private assertWriteAuthority; private persistRunState; private enqueueRunSettlementEffect; private viewerInspectorTargets; private initializeRunSettingsAndFollowUps; private requireRunAcceptsSettings; private verifySettingsProjections; private settingsScopeRow; private requireSettingsScopeRow; private settingsScopeRecord; private settingsChangeRow; private settingsChangeRecord; private assertSameSettingsChange; private followUpRowByRequest; private followUpRow; private requireFollowUpRow; private followUpRecord; private assertSameFollowUp; private assertFollowUpRemovalAuthority; private nextFollowUpOrder; private assertSettingsRouteCurrent; private transitionFollowUpsForRunState; private syncNodeAttempts; private syncRunSteps; private syncAttemptEntries; private findAttemptPromptEntry; private ensureAttempt; private nextAttemptNumber; private nextUpdateSequence; private insertRunSources; private readRunRow; private requireRunRow; private materializeRunState; private buildRunState; private readSteps; private readStepRange; private readLatestSteps; private readTakenTransitions; private mapStepRows; private readRunSources; private readActiveAttempt; private readHumanDecisionReceipt; private readUpdates; private readCurrentUpdates; private readJsonAs; private readText; private readDefinition; private traceEvents; private segmentRows; private segmentRow; private requireSegment; private loadSegment; private finalizeRecordingCaptures; } export declare function readWorkflowRun(runId: string, options?: ReadWorkflowRunOptions & { databasePath?: string; }): LoadedWorkflowRun | null; export declare function listWorkflowRuns(options?: ReadWorkflowRunOptions & { databasePath?: string; }): LoadedWorkflowRun[]; export declare function readLastTraceEvent(runId: string, options?: { databasePath?: string; }): WorkflowTraceEvent | null; export declare function createDefinitionSnapshot(workflow: WorkflowDefinition): WorkflowDefinitionSnapshot;