Skip to main content

apply_patch_with_validation

Function apply_patch_with_validation 

Source
pub fn apply_patch_with_validation(
    file_path: &Path,
    start: usize,
    end: usize,
    new_content: &str,
    workspace_dir: &Path,
    language: Language,
    analyzer_mode: AnalyzerMode,
    strict: bool,
    skip: bool,
) -> Result<(String, String)>
Expand description

Apply a patch with comprehensive validation and automatic rollback.

This function:

  1. Pre-verification (file state, workspace resources, graph sync)
  2. Computes hash of original file
  3. Replaces [start..end] byte span with new_content
  4. Writes to temp file, fsyncs, atomic rename
  5. Runs tree-sitter reparse gate (language-specific)
  6. Runs compiler validation gate (language-specific)
  7. Runs rust-analyzer gate (if enabled and Rust)
  8. On any failure, rolls back atomically

§Rollback Behavior

If any validation gate fails after patching, the original content is restored atomically. The rope mutation (remove + insert) happens in memory first, then the result is written to a temp file. If validation fails, we restore the original content.

§State Tracking

  • before_hash: Content hash before patching
  • replaced: Original bytes for rollback
  • after_hash: Content hash after patching (for verification)

§Arguments

  • file_path - Path to the file to patch
  • start - Start byte offset (inclusive)
  • end - End byte offset (exclusive)
  • new_content - Replacement content
  • workspace_dir - Directory containing project config for validation
  • language - Programming language for validation gates
  • analyzer_mode - rust-analyzer mode (off/path/explicit, Rust only)

§Returns

  • Ok((before_hash, after_hash)) - SHA-256 hashes before/after patch
  • Err(SpliceError) - Validation failure with automatic rollback