/** * Rehearsal System Types * * The Rehearsal system provides impact analysis for destructive operations * before they are executed. This is a novel safety feature that goes beyond * simple dry-run by actually analyzing what would be affected. */ import type { WorkAtRisk } from '../episodes/types.js'; /** * Severity level of the operation's potential impact */ export type ImpactSeverity = 'low' | 'medium' | 'high' | 'critical'; /** * Recommendation based on impact analysis */ export type RehearsalRecommendation = 'proceed' | 'caution' | 'confirm' | 'abort'; /** * Category of destructive operation */ export type OperationCategory = 'git' | 'file' | 'database' | 'network' | 'system' | 'unknown'; /** * Information about a file that would be affected */ export interface AffectedFile { /** * Path to the file */ path: string; /** * Type of change that would occur */ changeType: 'delete' | 'overwrite' | 'reset' | 'modify'; /** * Number of lines that would be affected (if applicable) */ linesAffected?: number; /** * When the file was last modified */ lastModified?: Date; /** * Size of the file in bytes */ size?: number; /** * Whether this file has uncommitted changes (for git operations) */ hasUncommittedChanges?: boolean; } /** * Detailed impact analysis of a destructive operation */ export interface RehearsalImpact { /** * Total number of files that would be affected */ filesAffected: number; /** * Total lines of code that would be changed/lost */ linesAffected: number; /** * List of affected files with details */ affectedFiles: AffectedFile[]; /** * Human-readable time investment summary * e.g., "Files modified in the last 2 hours" */ timeInvestment?: string; /** * Estimated hours of work that could be lost */ estimatedWorkHours?: number; /** * Size of data that would be affected (in bytes) */ dataSize?: number; /** * Human-readable summary of the impact */ summary: string; } /** * Result of rehearsing a destructive operation */ export interface RehearsalResult { /** * The original operation/command being rehearsed */ operation: string; /** * Category of the operation */ category: OperationCategory; /** * Whether this operation is considered destructive */ isDestructive: boolean; /** * Whether the operation can be reversed/undone */ isReversible: boolean; /** * Detailed impact analysis */ impact: RehearsalImpact; /** * Severity of the potential impact */ severity: ImpactSeverity; /** * Warning messages about potential issues */ warnings: string[]; /** * Recommendation on how to proceed */ recommendation: RehearsalRecommendation; /** * Suggested safer alternatives (if any) */ alternatives?: string[]; /** * Work episodes at risk from this operation (populated by episodic memory) */ workAtRisk?: WorkAtRisk; /** * Time taken to perform the analysis (ms) */ analysisTimeMs: number; } /** * Context provided to analyzers for better impact assessment */ export interface RehearsalContext { /** * Working directory for the operation */ workingDirectory: string; /** * Whether we're in a git repository */ isGitRepo?: boolean; /** * Current git branch (if in a git repo) */ currentBranch?: string; /** * Files that have been modified in this session */ sessionModifiedFiles?: string[]; /** * Start time of the current session (for time investment calculation) */ sessionStartTime?: Date; /** * Additional context from the agent */ agentContext?: Record; } /** * Interface for operation-specific analyzers */ export interface RehearsalAnalyzer { /** * Unique identifier for this analyzer */ readonly id: string; /** * Human-readable name */ readonly name: string; /** * Category of operations this analyzer handles */ readonly category: OperationCategory; /** * Patterns that this analyzer can handle */ readonly patterns: RegExp[]; /** * Check if this analyzer can handle the given operation */ canAnalyze(operation: string): boolean; /** * Analyze the operation and return impact assessment */ analyze(operation: string, context: RehearsalContext): Promise; } /** * Options for the RehearsalManager */ export interface RehearsalManagerOptions { /** * Working directory (defaults to process.cwd()) */ workingDirectory?: string; /** * Custom analyzers to add */ analyzers?: RehearsalAnalyzer[]; /** * Whether to include built-in analyzers (default: true) */ includeBuiltinAnalyzers?: boolean; /** * Minimum severity to trigger warnings (default: 'medium') */ warningThreshold?: ImpactSeverity; /** * Callback when a rehearsal is performed */ onRehearsal?: (result: RehearsalResult) => void; /** * Session start time for time investment calculations */ sessionStartTime?: Date; /** * Track files modified during session */ trackSessionFiles?: boolean; } /** * Event types emitted by RehearsalManager */ export type RehearsalEventType = 'rehearsal_start' | 'rehearsal_complete' | 'warning_triggered'; /** * Events emitted during rehearsal */ export interface RehearsalEvent { type: RehearsalEventType; operation: string; result?: RehearsalResult; timestamp: Date; } /** * Handler for rehearsal events */ export type RehearsalEventHandler = (event: RehearsalEvent) => void;