KeiSeiKit-1.0/_primitives/_rust/kei-refactor-engine/src/patch.rs
Parfii-bot 19ee220e0a feat(primitives): 4 Rust crates for deep-sleep — conflict-scan, refactor-engine, graph-check, store
- kei-conflict-scan: rules/hooks/blocks/orphans/CP detection (6 tests)
- kei-refactor-engine: plan-mode + advisory patch format, zero-conflict guarantee (5 tests)
- kei-graph-check: wikilinks/handoffs/block-refs validator (4 tests)
- kei-store: trait + 5 backends (filesystem/github/forgejo/gitea prod, s3 stub) (8 tests)

1916 LOC Rust total; all files <200 LOC; 23/23 tests pass.
2026-04-22 08:28:22 +08:00

56 lines
1.9 KiB
Rust

//! Patch synthesizer — writes a unified-diff file for `git apply` preview.
//!
//! This crate NEVER runs git. Per RULE 0.13 the orchestrator is the only
//! party that commits. We emit `.patch` text the user reads + applies.
//!
//! Only items whose resolution == AutoApply are materialised here; the
//! zero-conflict guarantee keeps `requires_human_decision` items out.
use crate::plan::{Plan, PlanItem, Resolution};
use anyhow::Result;
use std::fs;
use std::path::Path;
pub fn write_patch(plan: &Plan, branch: &str, out_file: &Path) -> Result<usize> {
let auto = plan.auto_items();
let mut body = String::new();
body.push_str(&header(branch, auto.len(), plan.manual_items().len()));
for item in &auto {
body.push_str(&hunk_for(item));
}
fs::write(out_file, body)?;
Ok(auto.len())
}
fn header(branch: &str, auto: usize, manual: usize) -> String {
format!(
"# kei-refactor-engine preview patch\n\
# Branch intent: {branch}\n\
# Auto-apply items: {auto}\n\
# Human-decision items (NOT in this patch, see plan): {manual}\n\
# Review with `git apply --check <file>` before merging.\n\n"
)
}
fn hunk_for(item: &PlanItem) -> String {
// Conservative: we do not invent file content. We emit an annotated
// comment block per item so the user sees intent, not fabricated code.
let files = item.files.join(", ");
format!(
"--- a/{file}\n+++ b/{file}\n# INTENT ({cat}/{sev}): {why}\n# FILES: {files}\n# EXAMPLE: {ex}\n# TRADEOFF: {tr}\n\n",
file = item.files.first().cloned().unwrap_or_else(|| "<unknown>".into()),
cat = item.category,
sev = item.severity,
why = item.why,
files = files,
ex = item.example,
tr = item.tradeoff,
)
}
pub fn excluded_manual(plan: &Plan) -> Vec<&PlanItem> {
plan.items
.iter()
.filter(|i| i.resolution == Resolution::RequiresHumanDecision)
.collect()
}