use std::path::PathBuf;
use std::sync::{Arc, atomic::AtomicBool};
use crate::embed::lifecycle::embed_edit_body::{EditBodyParts, RouteContext};
use crate::embed::lifecycle::public_api::EmbedSourceRefusal;
mod wire;
pub use wire::*;
pub use crate::edit_safety::structural_rewrite::StructuralRewrite as StructuralRewriteRequest;
#[derive(Clone, PartialEq, Eq)]
pub struct EditBody {
pub(crate) parts: EditBodyParts,
}
impl EditBody {
pub(crate) fn new(parts: EditBodyParts) -> Self {
Self { parts }
}
pub fn render(&self) -> String {
self.parts.render(None)
}
}
impl std::fmt::Debug for EditBody {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("EditBody")
.field("rendered_bytes", &self.render().len())
.finish()
}
}
impl serde::Serialize for EditBody {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&self.render())
}
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct EditTarget {
pub path: String,
pub name: String,
pub kind: Option<String>,
pub symbol_line: Option<u32>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct EditGuard {
pub(crate) root: PathBuf,
pub(crate) path: String,
pub(crate) name: String,
pub(crate) kind: String,
pub(crate) symbol_line: u32,
pub(crate) source_version: u64,
pub(crate) publication_identity: String,
pub(crate) serving_publication_identity: String,
pub(crate) publication_generation: u64,
pub(crate) content_generation: u64,
pub(crate) authority_publication: crate::lifecycle_identity::PublicationIdentity,
pub(crate) content_hash: String,
pub(crate) symbol_hash: String,
pub(crate) selector: String,
}
impl EditGuard {
pub fn path(&self) -> &str {
&self.path
}
pub fn source_version(&self) -> u64 {
self.source_version
}
pub fn publication_identity(&self) -> &str {
&self.publication_identity
}
pub fn serving_publication_identity(&self) -> &str {
&self.serving_publication_identity
}
pub fn publication_generation(&self) -> u64 {
self.publication_generation
}
pub fn content_generation(&self) -> u64 {
self.content_generation
}
pub fn content_hash(&self) -> &str {
&self.content_hash
}
pub fn symbol_hash(&self) -> &str {
&self.symbol_hash
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct EditPlan {
pub guard: EditGuard,
pub byte_range: (u32, u32),
pub line_range: (u32, u32),
pub reference_count: usize,
}
pub struct ReplaceRequest {
pub guard: EditGuard,
pub new_body: String,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InsertPosition {
Before,
After,
}
pub struct InsertRequest {
pub guard: EditGuard,
pub position: InsertPosition,
pub content: String,
}
pub struct DeleteRequest {
pub guard: EditGuard,
}
pub struct EditWithinRequest {
pub guard: EditGuard,
pub old_text: String,
pub new_text: String,
pub replace_all: bool,
pub occurrence: Option<u32>,
pub near_line: Option<u32>,
}
pub enum BatchEditAction {
Replace(ReplaceRequest),
Insert(InsertRequest),
Delete(DeleteRequest),
EditWithin(EditWithinRequest),
}
impl BatchEditAction {
pub(crate) fn guard(&self) -> &EditGuard {
match self {
Self::Replace(request) => &request.guard,
Self::Insert(request) => &request.guard,
Self::Delete(request) => &request.guard,
Self::EditWithin(request) => &request.guard,
}
}
}
pub struct BatchEditRequest {
pub actions: Vec<BatchEditAction>,
}
#[cfg(test)]
mod process_replay_tests {
use super::*;
use crate::embed::{EmbeddedSourceSpec, ProcessIndexRuntime, SourceRuntimePhase};
use std::fs;
use std::path::Path;
use std::process::Stdio;
use std::time::{Duration, Instant};
fn wait_current(handle: &crate::embed::EmbeddedSourceHandle) {
let deadline = Instant::now() + Duration::from_secs(15);
while handle.runtime_view().phase != SourceRuntimePhase::Current {
assert!(Instant::now() < deadline, "source failed to publish");
std::thread::sleep(Duration::from_millis(10));
}
}
fn authority(root: &Path) -> EditApplyAuthority {
EditApplyAuthority::for_source_root(
fs::canonicalize(root).expect("canonical root"),
"fixture-room/process-replay".to_string(),
Arc::new(AtomicBool::new(false)),
)
.expect("host authority")
}
#[test]
fn completed_batch_replay_survives_process_restart() {
let Ok(root_var) = std::env::var("SYMFORGE_EMBED_REPLAY_CHILD_ROOT") else {
let repository = tempfile::tempdir().expect("temporary repository");
git2::Repository::init(repository.path()).expect("initialize repository");
fs::create_dir_all(repository.path().join("src")).unwrap();
fs::write(
repository.path().join("src/lib.rs"),
b"pub fn number() -> u32 { 1 }\n",
)
.unwrap();
let wire = tempfile::tempdir().expect("private fixture directory");
let wire_path = wire.path().join("request.json");
let child = crate::process_util::hidden_command(
std::env::current_exe().expect("test executable"),
)
.arg("completed_batch_replay_survives_process_restart")
.arg("--nocapture")
.env("SYMFORGE_EMBED_REPLAY_CHILD_ROOT", repository.path())
.env("SYMFORGE_EMBED_REPLAY_GUARD", &wire_path)
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.expect("child test process");
assert!(child.success(), "child failed to complete guarded mutation");
let request: WireEditRequest =
serde_json::from_slice(&fs::read(&wire_path).expect("serialized edit request"))
.expect("strict wire edit request");
let runtime = ProcessIndexRuntime::acquire().expect("acquire fresh process runtime");
let handle = runtime
.open_embedded_source(EmbeddedSourceSpec::current_worktree(
repository.path().to_path_buf(),
))
.expect("open published source");
wait_current(&handle);
let authority = authority(repository.path());
let replayed = handle
.apply_wire_edit(&request, &authority, "fixture-process-op")
.expect("completed operation replay");
assert!(matches!(replayed, WireEditReply::BatchEditApplied(result) if result.replayed));
assert_eq!(
fs::read(repository.path().join("src/lib.rs")).unwrap(),
b"pub fn number() -> u32 { 2 }\n"
);
let stale = handle.apply_wire_edit(&request, &authority, "fixture-process-new-op");
assert!(matches!(
stale,
Err(EditError::Edit(EditErrorKind::StaleGeneration))
));
return;
};
let root = PathBuf::from(root_var);
let runtime = ProcessIndexRuntime::acquire().expect("acquire child runtime");
let handle = runtime
.open_embedded_source(EmbeddedSourceSpec::current_worktree(root.clone()))
.expect("open child source");
wait_current(&handle);
let plan = handle
.edit_plan(&EditTarget {
path: "src/lib.rs".to_string(),
name: "number".to_string(),
kind: None,
symbol_line: None,
})
.expect("child plan");
let request = WireEditRequest::ApplyBatchEdit {
actions: vec![WireBatchEditAction::Replace {
guard: WireEditGuard::from(&plan.guard),
new_body: "pub fn number() -> u32 { 2 }".to_string(),
}],
};
let applied = handle
.apply_wire_edit(&request, &authority(&root), "fixture-process-op")
.expect("child apply");
assert!(matches!(applied, WireEditReply::BatchEditApplied(result) if !result.replayed));
fs::write(
std::env::var("SYMFORGE_EMBED_REPLAY_GUARD").expect("wire path"),
serde_json::to_vec(&request).unwrap(),
)
.expect("save serialized request");
}
#[test]
fn wire_structural_rewrite_replays_after_the_tree_converges() {
let repository = tempfile::tempdir().expect("temporary repository");
git2::Repository::init(repository.path()).expect("initialize repository");
fs::create_dir_all(repository.path().join("src")).unwrap();
fs::write(
repository.path().join("src/lib.rs"),
b"pub fn run() { old_call(1); }\n",
)
.unwrap();
fs::write(
repository.path().join("src/other.rs"),
b"pub fn idle() {}\n",
)
.unwrap();
let unknown = serde_json::from_value::<WireEditRequest>(serde_json::json!({
"kind": "apply_structural_rewrite",
"rewrite": {"pattern": "old_call($A)", "template": "new_call($A)", "dry_run": true},
}));
assert!(unknown.is_err(), "an unknown rewrite key must be refused");
let request: WireEditRequest = serde_json::from_value(serde_json::json!({
"kind": "apply_structural_rewrite",
"rewrite": {"pattern": "old_call($A)", "template": "new_call($A)"},
}))
.expect("strict wire rewrite request");
let runtime = ProcessIndexRuntime::acquire().expect("process runtime");
let handle = runtime
.open_embedded_source(EmbeddedSourceSpec::current_worktree(
repository.path().to_path_buf(),
))
.expect("open source");
wait_current(&handle);
let preview: WireEditRequest = serde_json::from_value(serde_json::json!({
"kind": "preview_structural_rewrite",
"rewrite": {"pattern": "old_call($A)", "template": "new_call($A)"},
}))
.expect("strict wire preview request");
let previewed = handle.preview_wire_edit(&preview).expect("wire preview");
assert!(
matches!(&previewed, WireEditReply::BatchEditPreview(result) if result.files.len() == 1)
);
let authority = authority(repository.path());
let applied = handle
.apply_wire_edit(&request, &authority, "structural-rewrite-op")
.expect("wire apply");
assert!(matches!(applied, WireEditReply::BatchEditApplied(result) if !result.replayed));
let written = b"pub fn run() { new_call(1); }\n";
assert_eq!(
fs::read(repository.path().join("src/lib.rs")).unwrap(),
written
);
wait_current(&handle);
let replayed = handle
.apply_wire_edit(&request, &authority, "structural-rewrite-op")
.expect("completed rewrite replays");
assert!(matches!(replayed, WireEditReply::BatchEditApplied(result) if result.replayed));
assert_eq!(
fs::read(repository.path().join("src/lib.rs")).unwrap(),
written
);
let fresh = handle.apply_wire_edit(&request, &authority, "structural-rewrite-new-op");
assert!(
matches!(
&fresh,
Err(EditError::Edit(EditErrorKind::StructuralRewriteRefused { message }))
if message.contains("matched nothing")
),
"a new key re-plans the converged tree"
);
}
}
pub struct BatchInsertRequest {
pub targets: Vec<EditGuard>,
pub position: InsertPosition,
pub content: String,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BatchRenamePlan {
pub guard: EditGuard,
pub code_only: bool,
pub affected_paths: Vec<String>,
pub confident_sites: usize,
pub uncertain_sites: usize,
}
pub struct BatchRenameRequest {
pub plan: BatchRenamePlan,
pub new_name: String,
}
#[derive(Clone, PartialEq, Eq, serde::Serialize)]
pub struct BatchRenamePreview {
pub changes: BatchEditPreview,
pub rendered: String,
pub truncated: bool,
}
impl std::fmt::Debug for BatchRenamePreview {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BatchRenamePreview")
.field("changes", &self.changes)
.field("rendered_bytes", &self.rendered.len())
.field("truncated", &self.truncated)
.finish()
}
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize)]
pub struct BatchFilePreview {
pub path: String,
pub old_file_hash: String,
pub proposed_file_hash: String,
}
#[derive(Clone, PartialEq, Eq, serde::Serialize)]
pub struct BatchEditPreview {
pub files: Vec<BatchFilePreview>,
pub action_count: usize,
pub publication_identity: String,
pub source_version: u64,
pub rendered: String,
pub truncated: bool,
pub redacted: bool,
pub body: EditBody,
}
impl std::fmt::Debug for BatchEditPreview {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BatchEditPreview")
.field("files", &self.files)
.field("action_count", &self.action_count)
.field("publication_identity", &self.publication_identity)
.field("source_version", &self.source_version)
.field("rendered_bytes", &self.rendered.len())
.field("truncated", &self.truncated)
.field("redacted", &self.redacted)
.field("body", &self.body)
.finish()
}
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize)]
pub struct BatchEditApplied {
pub files: Vec<(String, String)>,
pub replayed: bool,
pub refresh_ticket_identity: Option<String>,
pub body: EditBody,
}
pub struct RoutedBatchPart<'a, R = BatchEditRequest> {
pub handle: &'a crate::embed::EmbeddedSourceHandle,
pub authority: &'a EditApplyAuthority,
pub request: R,
pub working_directory: Option<PathBuf>,
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize)]
pub struct RoutedBatchApplied {
pub parts: Vec<BatchEditApplied>,
pub replayed: bool,
pub body: EditBody,
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize)]
pub struct EditWithinPreview {
pub change: StructuralPreview,
pub replacement_count: usize,
pub untargeted_extra: usize,
}
#[derive(Clone, PartialEq, Eq, serde::Serialize)]
pub struct StructuralPreview {
pub path: String,
pub source_version: u64,
pub publication_identity: String,
pub old_file_hash: String,
pub proposed_file_hash: String,
pub old_bytes: usize,
pub inserted_bytes: usize,
pub rendered: String,
pub truncated: bool,
pub redacted: bool,
pub body: EditBody,
}
impl std::fmt::Debug for StructuralPreview {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("StructuralPreview")
.field("path", &self.path)
.field("source_version", &self.source_version)
.field("publication_identity", &self.publication_identity)
.field("old_file_hash", &self.old_file_hash)
.field("proposed_file_hash", &self.proposed_file_hash)
.field("old_bytes", &self.old_bytes)
.field("inserted_bytes", &self.inserted_bytes)
.field("rendered_bytes", &self.rendered.len())
.field("truncated", &self.truncated)
.field("redacted", &self.redacted)
.field("body", &self.body)
.finish()
}
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize)]
pub struct StructuralApplied {
pub path: String,
pub post_image_hash: String,
pub replayed: bool,
pub refresh_ticket_identity: Option<String>,
pub body: EditBody,
}
#[derive(Clone, PartialEq, Eq, serde::Serialize)]
pub struct ReplacePreview {
pub path: String,
pub source_version: u64,
pub publication_identity: String,
pub old_file_hash: String,
pub proposed_file_hash: String,
pub old_bytes: usize,
pub inserted_bytes: usize,
pub rendered: String,
pub truncated: bool,
pub redacted: bool,
pub body: EditBody,
}
impl std::fmt::Debug for ReplacePreview {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ReplacePreview")
.field("path", &self.path)
.field("source_version", &self.source_version)
.field("publication_identity", &self.publication_identity)
.field("old_file_hash", &self.old_file_hash)
.field("proposed_file_hash", &self.proposed_file_hash)
.field("old_bytes", &self.old_bytes)
.field("inserted_bytes", &self.inserted_bytes)
.field("rendered_bytes", &self.rendered.len())
.field("truncated", &self.truncated)
.field("redacted", &self.redacted)
.field("body", &self.body)
.finish()
}
}
pub struct EditApplyAuthority {
pub(crate) root: PathBuf,
pub(crate) scope: String,
pub(crate) cancel: Arc<AtomicBool>,
}
impl EditApplyAuthority {
pub fn for_source_root(
root: PathBuf,
scope: String,
cancel: Arc<AtomicBool>,
) -> Result<Self, EditErrorKind> {
if !root.is_absolute() || scope.trim().is_empty() {
return Err(EditErrorKind::WriteAuthorityRefused);
}
Ok(Self {
root,
scope,
cancel,
})
}
}
#[derive(Clone, Copy)]
pub struct AdmittedEditTarget<'a> {
pub handle: &'a crate::embed::EmbeddedSourceHandle,
pub authority: &'a EditApplyAuthority,
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize)]
pub struct ResolvedEditTarget {
pub working_directory: PathBuf,
pub rerouted: bool,
pub target_path: PathBuf,
pub indexed_path: PathBuf,
}
impl ResolvedEditTarget {
pub fn reroute_suffix(&self) -> String {
crate::embed::lifecycle::guidance::edit_route::format_reroute_suffix(
Some(&self.working_directory),
self.rerouted,
&self.target_path,
&self.indexed_path,
)
}
}
pub struct EditRoute<'a> {
pub target: Option<AdmittedEditTarget<'a>>,
pub resolved: ResolvedEditTarget,
pub(crate) path: String,
pub(crate) bound_root: PathBuf,
pub(crate) context: RouteContext,
}
impl EditRoute<'_> {
pub fn render_body(&self, body: &EditBody) -> String {
body.parts.render(Some(&self.context))
}
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize)]
pub struct ReplaceApplied {
pub path: String,
pub post_image_hash: String,
pub replayed: bool,
pub refresh_ticket_identity: Option<String>,
pub body: EditBody,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EditErrorKind {
InvalidPath,
FileNotAdmitted,
SymbolNotFound,
AmbiguousSymbol {
candidate_lines: Vec<u32>,
},
UnsafeContent,
StaleGeneration,
StaleContent,
WriteAuthorityRefused,
WriteConflict,
WriteUncertain,
ReplayUnavailable,
ReplayConflict,
Cancelled,
InvalidReplacement,
ConflictingTargeting,
OccurrenceOutOfRange {
requested: u32,
total: usize,
},
TextNotFound,
WorkingDirectoryNotAdmitted,
WorkingDirectoryNotAWorktree,
TargetFileMissing,
ProjectConfigTrustEnforced {
message: String,
},
StructuralRewriteRefused {
message: String,
},
ProjectWideMutationRefused {
message: String,
},
}
#[derive(Debug)]
pub enum EditError {
Source(EmbedSourceRefusal),
Edit(EditErrorKind),
}
impl From<EmbedSourceRefusal> for EditError {
fn from(value: EmbedSourceRefusal) -> Self {
Self::Source(value)
}
}