Skip to main content

Crate codex_patcher

Crate codex_patcher 

Source
Expand description

Codex Patcher: Automated code patching system for Rust

A robust patching system built on byte-span replacement primitives with tree-sitter and ast-grep integration for structural code queries.

§Architecture

All edit operations compile down to a single primitive: Edit, which represents a verified byte-span replacement. Intelligence lives in span acquisition (via tree-sitter, ast-grep, compiler diagnostics), not in the application logic.

§Safety

  • All edits verify expected before-text before applying
  • Atomic file writes (tempfile + fsync + rename)
  • Workspace boundary enforcement
  • UTF-8 validation
  • Idempotent operations

§Example

use codex_patcher::{Edit, EditVerification};
use std::path::PathBuf;

let edit = Edit::new(
    PathBuf::from("src/main.rs"),
    0,
    5,
    "HELLO",
    "hello",
);

match edit.apply() {
    Ok(result) => println!("Edit applied: {:?}", result),
    Err(e) => eprintln!("Edit failed: {}", e),
}

Re-exports§

pub use config::apply_patches;
pub use config::load_from_path;
pub use config::load_from_str;
pub use config::matches_requirement;
pub use config::ApplicationError;
pub use config::ConfigError;
pub use config::PatchConfig;
pub use config::PatchResult;
pub use config::VersionError;
pub use edit::Edit;
pub use edit::EditError;
pub use edit::EditResult;
pub use edit::EditVerification;
pub use safety::SafetyError;
pub use safety::WorkspaceGuard;
pub use ts::LocatorResult;
pub use ts::QueryEngine;
pub use ts::QueryMatch;
pub use ts::RustParser;
pub use ts::StructuralLocator;
pub use ts::StructuralTarget;
pub use ts::TreeSitterError;
pub use validate::syn_validate;
pub use validate::ErrorLocation;
pub use validate::ParseValidator;
pub use validate::SelectorValidator;
pub use validate::ValidatedEdit;
pub use validate::ValidationError;

Modules§

cache
Thread-local pattern compilation cache for ast-grep patterns.
compiler
Compiler integration for parsing diagnostics and applying auto-fixes.
config
edit
fuzzy
Fuzzy matching for patch text search
pool
Thread-local parser pooling for performance optimization.
safety
sg
ast-grep integration for pattern-based Rust code matching.
toml
ts
Tree-sitter integration for structural Rust code queries.
validate
Validation module for ensuring edit safety.