/** * Shared types for the tool write-ahead log extension. */ export type OperationStatus = | "pending" | "success" | "failed" | "partial" | "blocked" | "orphaned"; /** * Query scope hierarchy (narrow → wide): * session — one Pi session (session_id) * project — one project, identified by project_key (encoded cwd) * global — entire WAL database */ export type QueryScope = "session" | "project" | "global"; export type ProjectIdSource = "explicit" | "git" | "cwd"; export interface ToolOperationRow { id: string; tool_call_id: string; /** * Stable project identity for WAL scoping. * Prefer git remote (+ relpath) or explicit id over cwd encoding, * so the same repo keeps one key across machines. */ project_key: string | null; /** How project_key was derived: explicit | git | cwd. */ project_key_source: string | null; session_id: string | null; session_file: string | null; cwd: string | null; tool_name: string; input_json: string; input_hash: string | null; status: OperationStatus; started_at: number; ended_at: number | null; duration_ms: number | null; is_error: number | null; output_text: string | null; output_json: string | null; details_json: string | null; error_message: string | null; expected_ok: number | null; mismatch_reason: string | null; turn_index: number | null; created_at: number; updated_at: number; } export interface BeginOperationInput { toolCallId: string; toolName: string; input: unknown; projectKey?: string | null; projectKeySource?: ProjectIdSource | string | null; sessionId?: string | null; sessionFile?: string | null; cwd?: string | null; turnIndex?: number | null; /** When re-recording the same toolCallId, refresh input from tool_call. */ source: "tool_execution_start" | "tool_call"; } export interface CompleteOperationInput { toolCallId: string; toolName: string; input?: unknown; isError: boolean; content?: unknown; details?: unknown; /** Prefer tool_result over tool_execution_end when both fire. */ source: "tool_result" | "tool_execution_end"; /** Optional explicit status override (e.g. blocked). */ status?: OperationStatus; errorMessage?: string | null; /** Narrow lookup when completing (avoids cross-session collisions). */ sessionId?: string | null; projectKey?: string | null; } export interface QueryOptions { /** Default when neither sessionId nor projectKey is set: unconstrained (global). */ projectKey?: string; sessionId?: string; status?: OperationStatus | OperationStatus[]; toolName?: string; limit?: number; offset?: number; sinceMs?: number; } export interface OperationStats { total: number; pending: number; success: number; failed: number; partial: number; blocked: number; orphaned: number; expectedOk: number; expectedNotOk: number; } export interface ProjectSummary { project_key: string; project_key_source: string | null; cwd: string | null; total: number; pending: number; sessions: number; last_started_at: number | null; } /** Max chars stored for serialized output / details. */ export const MAX_STORED_CHARS = 64 * 1024; export const DEFAULT_RECENT_LIMIT = 20; export const DEFAULT_ORPHAN_AGE_MS = 5 * 60 * 1000;