/** * Centralized Data Safety Manager * * SINGLE POINT OF SAFETY for ALL CLEO data operations. * * Design Principles: * - All data operations MUST flow through this layer * - Zero-config safety: works automatically for all callers * - Atomic operations: verify + checkpoint + log in single transaction * - Recoverable: every operation leaves system in valid state * * This eliminates the need for wrapper functions and ensures safety * at the architectural level, not as an afterthought. * * @task T4739 * @epic T4732 */ import type { Session, Task } from '@cleocode/contracts'; import type { ArchiveFile, DataAccessor } from './data-accessor.js'; /** Safety violation error */ export declare class DataSafetyError extends Error { code: 'COLLISION' | 'WRITE_FAILED' | 'VERIFICATION_FAILED' | 'SEQUENCE_INVALID'; context?: Record | undefined; constructor(message: string, code: 'COLLISION' | 'WRITE_FAILED' | 'VERIFICATION_FAILED' | 'SEQUENCE_INVALID', context?: Record | undefined); } /** Safety configuration - can be overridden per-operation */ export interface SafetyOptions { /** Verify data was written (default: true) */ verify: boolean; /** Create git checkpoint (default: true) */ checkpoint: boolean; /** Validate sequence (default: true) */ validateSequence: boolean; /** Strict mode - throw on any issue (default: true) */ strict: boolean; } /** Statistics for monitoring */ interface SafetyStats { writes: number; verifications: number; checkpoints: number; errors: number; lastCheckpoint: Date | null; } /** Get current safety statistics */ export declare function getSafetyStats(): SafetyStats; /** Reset safety statistics (for testing) */ export declare function resetSafetyStats(): void; /** * Safe wrapper for DataAccessor.saveSessions() */ export declare function safeSaveSessions(accessor: DataAccessor, data: Session[], cwd?: string, options?: Partial): Promise; /** * Safe wrapper for DataAccessor.saveArchive() */ export declare function safeSaveArchive(accessor: DataAccessor, data: ArchiveFile, cwd?: string, options?: Partial): Promise; /** * Safe wrapper for single-task write operations (T5034). * * Performs: * 1. Sequence validation * 2. Write operation (caller-provided function) * 3. Git checkpoint * * Verification is lightweight — no full-file read-back. The write * itself is a targeted SQL operation that either succeeds or throws. */ export declare function safeSingleTaskWrite(_accessor: DataAccessor, taskId: string, writeFn: () => Promise, cwd?: string, options?: Partial): Promise; /** * Safe wrapper for DataAccessor.appendLog() * * Note: Log appends are fire-and-forget (no verification) * but we still checkpoint to ensure data is committed. */ export declare function safeAppendLog(accessor: DataAccessor, entry: Record, cwd?: string, options?: Partial): Promise; /** * Run comprehensive data integrity check. * Validates all data files and sequence consistency. */ export declare function runDataIntegrityCheck(accessor: DataAccessor, cwd?: string): Promise<{ passed: boolean; errors: string[]; warnings: string[]; stats: SafetyStats; }>; /** * Force immediate checkpoint. * Use before destructive operations. */ export declare function forceSafetyCheckpoint(context: string, cwd?: string): Promise; /** * Disable all safety for current process. * DANGEROUS - only use for recovery operations. */ export declare function disableSafety(): void; /** * Re-enable safety after being disabled. */ export declare function enableSafety(): void; /** Per-operation safety configuration for the task-level wrappers. */ export interface SafetyConfig { /** Enable write verification (default: true) */ verifyWrites: boolean; /** Enable collision detection (default: true) */ detectCollisions: boolean; /** Enable sequence validation (default: true) */ validateSequence: boolean; /** Enable auto-checkpoint (default: true) */ autoCheckpoint: boolean; /** Throw on safety violations (default: true) */ strictMode: boolean; } /** Safety violation error raised by the task-level wrappers. */ export declare class SafetyError extends Error { code: string; details?: Record | undefined; constructor(message: string, code: string, details?: Record | undefined); } /** * Check if a task ID already exists (collision detection). * @throws SafetyError if task exists and strict mode is enabled */ export declare function checkTaskExists(taskId: string, cwd?: string, config?: Partial): Promise; /** * Verify a task was actually written to the database. * @throws SafetyError if verification fails */ export declare function verifyTaskWrite(taskId: string, expectedData?: Partial, cwd?: string, config?: Partial): Promise; /** * Validate and repair sequence if necessary. * @returns true if sequence was valid or successfully repaired */ export declare function validateAndRepairSequence(cwd?: string, config?: Partial): Promise<{ valid: boolean; repaired: boolean; oldCounter?: number; newCounter?: number; }>; /** * Trigger auto-checkpoint after a successful task write. */ export declare function triggerCheckpoint(context: string, cwd?: string, config?: Partial): Promise; /** * Safely create a task with all safety mechanisms. * Wraps the actual createTask operation. */ export declare function safeCreateTask(createFn: () => Promise, task: Task, cwd?: string, config?: Partial): Promise; /** * Safely update a task with all safety mechanisms. */ export declare function safeUpdateTask(updateFn: () => Promise, taskId: string, _updates: Partial, cwd?: string, config?: Partial): Promise; /** * Safely delete a task with all safety mechanisms. */ export declare function safeDeleteTask(deleteFn: () => Promise, taskId: string, cwd?: string, config?: Partial): Promise; /** * Verify a session write landed in the database. * @throws SafetyError if verification fails in strict mode */ export declare function verifySessionWrite(sessionId: string, cwd?: string, config?: Partial): Promise; /** * Safely create a session with all safety mechanisms. */ export declare function safeCreateSession(createFn: () => Promise, session: Session, cwd?: string, config?: Partial): Promise; /** * Force a checkpoint before destructive operations. * Use this before migrations, bulk updates, etc. */ export declare function forceCheckpointBeforeOperation(operation: string, cwd?: string): Promise; /** * Run a sequence-focused integrity check keyed only by working directory. * * Distinct from the accessor-based {@link runDataIntegrityCheck} above — this * variant needs no {@link DataAccessor} and reports sequence issues/repairs * only. Retained for the lightweight `cwd`-only integrity probe. */ export declare function runSequenceIntegrityCheck(cwd?: string): Promise<{ passed: boolean; issues: string[]; repairs: string[]; }>; export {}; //# sourceMappingURL=data-safety-central.d.ts.map