foch 0.0.2

EU4 mod analysis and merge toolkit
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
// Patch set merging: given N mods' patch sets against a common base, merge
// them into a single resolved patch set with conflict detection.
#![allow(dead_code)]

use std::collections::{HashMap, HashSet};

use serde::{Deserialize, Serialize};

#[cfg(test)]
use crate::game::eu4::content::NamedContainerPolicy;
#[cfg(test)]
use crate::game::eu4::content::{DivergentBlockPolicy, ScalarMergePolicy};
use crate::game::eu4::content::{ListMergePolicy, MergeKeySource, MergePolicies};
use crate::game::eu4::script::parser::{AstStatement, AstValue};
use crate::model::{GamePath, GamePathBuf, HandlerResolutionRecord};

#[cfg(test)]
use super::super::conflict_handler::ConflictDecision;
use super::super::conflict_handler::ConflictHandler;
#[cfg(test)]
use super::super::conflict_handler::DeferHandler;
use super::super::error::MergeError;
use super::super::model::ExternalFileResolution;
use super::patch::{AstPath, ClausewitzPatch, ListItemTarget, fuzzy_rename_similarity};

// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------

/// Address of a patch — uniquely identifies what AST node is being changed.
#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub(crate) struct PatchAddress {
	pub path: AstPath,
	pub key: String,
}

/// A patch attributed to a specific mod.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct AttributedPatch {
	pub mod_id: String,
	pub precedence: usize,
	pub patch: ClausewitzPatch,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct PatchConflict {
	pub patches: Vec<AttributedPatch>,
	pub reason: String,
}

/// Result of merging patches at a single address.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PatchResolution {
	/// Single mod or all mods agree — apply this patch.
	Resolved(ClausewitzPatch),
	/// Auto-resolved by policy (e.g., union of list items, highest precedence).
	AutoMerged {
		result: ClausewitzPatch,
		strategy: String,
		contributing_mods: Vec<String>,
	},
	/// Irreconcilable conflict — needs manual resolution.
	Conflict {
		address: PatchAddress,
		patches: Vec<AttributedPatch>,
		reason: String,
	},
}

/// Result of merging all patch sets.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct PatchMergeResult {
	pub resolved: Vec<PatchResolution>,
	pub conflicts: Vec<PatchResolution>,
	pub stats: PatchMergeStats,
	pub handler_resolved_count: usize,
	pub handler_resolutions: Vec<HandlerResolutionRecord>,
	/// Whole-file decisions, keyed by the output file they replace.
	pub external_file_resolutions: HashMap<GamePathBuf, ExternalFileResolution>,
	pub keep_existing_paths: HashSet<GamePathBuf>,
}

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct PatchMergeStats {
	pub total_patches: usize,
	pub single_mod_patches: usize,
	pub convergent_patches: usize,
	pub auto_merged_patches: usize,
	pub conflict_patches: usize,
	/// One mod edited a property (SetValue/ReplaceBlock/InsertNode) while
	/// another removed it (RemoveNode); the edit was kept and the remove
	/// dropped, instead of reporting a mixed-kinds conflict.
	pub edit_over_remove_resolved: usize,
}

impl PatchMergeStats {
	pub(crate) fn accumulate(&mut self, nested: &Self) {
		self.total_patches += nested.total_patches;
		self.single_mod_patches += nested.single_mod_patches;
		self.convergent_patches += nested.convergent_patches;
		self.auto_merged_patches += nested.auto_merged_patches;
		self.conflict_patches += nested.conflict_patches;
		self.edit_over_remove_resolved += nested.edit_over_remove_resolved;
	}
}

fn patch_sort_key(patch: &ClausewitzPatch) -> (AstPath, String, u8, Vec<u8>) {
	let (path, key, operation_rank) = match patch {
		ClausewitzPatch::Rename {
			path,
			old_key,
			new_key,
		} => (path.clone(), format!("{old_key}\0{new_key}"), 0),
		ClausewitzPatch::RemoveNode { path, key, .. } => (path.clone(), key.clone(), 1),
		ClausewitzPatch::RemoveListItem { path, key, .. } => (path.clone(), key.clone(), 2),
		ClausewitzPatch::RemoveBlockItem { path, .. } => (path.clone(), String::new(), 3),
		ClausewitzPatch::SetValue { path, key, .. } => (path.clone(), key.clone(), 4),
		ClausewitzPatch::ReplaceBlock { path, key, .. } => (path.clone(), key.clone(), 5),
		ClausewitzPatch::InsertNode { path, key, .. } => (path.clone(), key.clone(), 6),
		ClausewitzPatch::AppendListItem { path, key, .. } => (path.clone(), key.clone(), 7),
		ClausewitzPatch::AppendBlockItem { path, .. } => (path.clone(), String::new(), 8),
	};
	let payload = bincode::serialize(patch).unwrap_or_else(|_| format!("{patch:?}").into_bytes());
	(path, key, operation_rank, payload)
}

fn sort_contributors(mod_patches: &mut [(String, usize, Vec<ClausewitzPatch>)]) {
	mod_patches.sort_by(|left, right| left.1.cmp(&right.1).then_with(|| left.0.cmp(&right.0)));
}

type PatchSourceOrder = (usize, String, usize);

fn address_group_order_key(
	address: &PatchAddress,
	source_orders: &HashMap<PatchAddress, PatchSourceOrder>,
) -> (usize, String, usize, AstPath, String) {
	let (precedence, mod_id, source_ordinal) =
		source_orders
			.get(address)
			.cloned()
			.unwrap_or((usize::MAX, String::new(), usize::MAX));
	(
		precedence,
		mod_id,
		source_ordinal,
		address.path.clone(),
		address.key.clone(),
	)
}

pub(crate) fn semantic_value_identity(value: &AstValue) -> String {
	crate::merge::semantic_fingerprint::value_fingerprint(value)
}

pub(crate) fn semantic_statement_identity(statement: &AstStatement) -> String {
	crate::merge::semantic_fingerprint::statement_fingerprint(statement)
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

mod address;
use address::patch_address;

mod conflict;
use conflict::{apply_conflict_decision, detect_cross_kind_sibling_conflicts};

mod resolve;
use resolve::resolve_address;

// ---------------------------------------------------------------------------
// Main entry point
// ---------------------------------------------------------------------------

/// Merge multiple mod patch sets into a single resolved set, leaving every
/// conflict unresolved.
///
/// Nested block merges use this. A conflict inside a block is reported to the
/// enclosing merge, which decides it for the file it is in, so nothing here
/// names a file or asks a handler.
///
/// `mod_patches`: Vec of `(mod_id, precedence, patches)` for each mod.
/// `policies`: The content family's merge policies for auto-resolution.
pub fn merge_patch_sets(
	mod_patches: Vec<(String, usize, Vec<ClausewitzPatch>)>,
	policies: &MergePolicies,
) -> Result<PatchMergeResult, MergeError> {
	merge_patch_sets_with(mod_patches, policies, None)
}

/// Merge the patch sets of `current_file`, letting `handler` decide each
/// conflict. Conflict ids and whole-file decisions name `current_file`.
pub(crate) fn merge_patch_sets_for_file(
	mod_patches: Vec<(String, usize, Vec<ClausewitzPatch>)>,
	policies: &MergePolicies,
	handler: &mut dyn ConflictHandler,
	current_file: &GamePath,
) -> Result<PatchMergeResult, MergeError> {
	merge_patch_sets_with(mod_patches, policies, Some((handler, current_file)))
}

fn merge_patch_sets_with(
	mut mod_patches: Vec<(String, usize, Vec<ClausewitzPatch>)>,
	policies: &MergePolicies,
	mut arbiter: Option<(&mut dyn ConflictHandler, &GamePath)>,
) -> Result<PatchMergeResult, MergeError> {
	let mut result = PatchMergeResult::default();
	sort_contributors(&mut mod_patches);

	// --- Pre-pass: collect renames and rewrite cross-mod addresses ---
	//
	// For each `Rename { path, old_key, new_key }` emitted by any mod, every
	// other mod's patches whose `(path, key)` match — or whose path traverses
	// `old_key` at that location — must be rewritten so they target the new
	// key instead. Otherwise the renaming mod's RemoveNode would conflict
	// with the modifier mod's edits at the old key.
	let rename_map = build_rename_map(&mod_patches);
	let mut mod_patches: Vec<(String, usize, Vec<ClausewitzPatch>)> = mod_patches
		.into_iter()
		.map(|(mod_id, prec, patches)| {
			let rewritten = patches
				.into_iter()
				.map(|p| rewrite_patch_for_renames(p, &rename_map))
				.collect();
			(mod_id, prec, rewritten)
		})
		.collect();
	sort_contributors(&mut mod_patches);
	let mod_patches = drop_prefixed_rename_duplicate_inserts(mod_patches);
	let mod_patches = normalize_singleton_list_inserts(mod_patches);
	let mod_patches = apply_list_policy(mod_patches, policies);
	let (mod_patches, prefix_edit_resolutions) =
		drop_removed_ancestors_with_sibling_descendant_edits(mod_patches, policies);
	result.stats.total_patches += prefix_edit_resolutions;
	result.stats.edit_over_remove_resolved += prefix_edit_resolutions;

	// Group patches by address, preserving attribution.
	let mut by_address: HashMap<PatchAddress, Vec<AttributedPatch>> = HashMap::new();
	let mut source_order_by_address: HashMap<PatchAddress, PatchSourceOrder> = HashMap::new();
	let mut source_ordinal_by_attribution: HashMap<(PatchAddress, usize, String), usize> =
		HashMap::new();

	for (mod_id, precedence, patches) in mod_patches {
		for (source_ordinal, patch) in patches.into_iter().enumerate() {
			result.stats.total_patches += 1;
			let addr = patch_address(&patch, policies);
			let source_order = (precedence, mod_id.clone(), source_ordinal);
			source_order_by_address
				.entry(addr.clone())
				.and_modify(|known| {
					if source_order < *known {
						known.clone_from(&source_order);
					}
				})
				.or_insert(source_order);
			source_ordinal_by_attribution
				.entry((addr.clone(), precedence, mod_id.clone()))
				.and_modify(|known| *known = (*known).min(source_ordinal))
				.or_insert(source_ordinal);
			by_address.entry(addr).or_default().push(AttributedPatch {
				mod_id: mod_id.clone(),
				precedence,
				patch,
			});
		}
	}

	// Cross-kind sibling conflict pre-check.
	//
	// `patch_address` fingerprints `RemoveNode` / `InsertNode` only for
	// Union-policy keys, where repeated named children are allowed to coexist.
	// That can split same-(path, key) patches of different kinds across
	// addresses — for example a fingerprinted `RemoveNode(owner)` versus an
	// unfingerprinted `SetValue(owner)`. Bucket by the kind-agnostic raw
	// `(path, key)` so these ambiguous sibling intents surface as one conflict
	// instead of applying independently.
	let mut cross_kind_conflicts =
		detect_cross_kind_sibling_conflicts(&by_address, &mut result.stats);
	for conflict in &mut cross_kind_conflicts {
		conflict.patches.sort_by(|left, right| {
			let left_address = patch_address(&left.patch, policies);
			let right_address = patch_address(&right.patch, policies);
			let left_ordinal = source_ordinal_by_attribution
				.get(&(left_address, left.precedence, left.mod_id.clone()))
				.copied()
				.unwrap_or(usize::MAX);
			let right_ordinal = source_ordinal_by_attribution
				.get(&(right_address, right.precedence, right.mod_id.clone()))
				.copied()
				.unwrap_or(usize::MAX);
			left.precedence
				.cmp(&right.precedence)
				.then_with(|| left.mod_id.cmp(&right.mod_id))
				.then_with(|| left_ordinal.cmp(&right_ordinal))
				.then_with(|| patch_sort_key(&left.patch).cmp(&patch_sort_key(&right.patch)))
		});
	}
	cross_kind_conflicts.sort_by_key(|conflict| {
		let (precedence, mod_id, source_ordinal) = conflict
			.split_addresses
			.iter()
			.filter_map(|address| source_order_by_address.get(address).cloned())
			.min()
			.unwrap_or((usize::MAX, String::new(), usize::MAX));
		(
			precedence,
			mod_id,
			source_ordinal,
			conflict.address.path.clone(),
			conflict.address.key.clone(),
		)
	});
	let cross_kind_addresses: HashSet<PatchAddress> = cross_kind_conflicts
		.iter()
		.flat_map(|conflict| conflict.split_addresses.iter().cloned())
		.collect();
	for addr in &cross_kind_addresses {
		by_address.remove(addr);
	}

	let mut address_groups = by_address.into_iter().collect::<Vec<_>>();
	address_groups
		.sort_by_key(|(address, _)| address_group_order_key(address, &source_order_by_address));
	let mut pending_resolutions = Vec::with_capacity(address_groups.len());
	for (addr, attributed) in address_groups {
		pending_resolutions.push(resolve_address(
			addr,
			attributed,
			policies,
			&mut result.stats,
		));
	}

	let total_conflicts = pending_resolutions
		.iter()
		.filter(|resolution| matches!(resolution, PatchResolution::Conflict { .. }))
		.count()
		+ cross_kind_conflicts.len();
	let mut current_conflict = 0;

	let mut decide = |result: &mut PatchMergeResult,
	                  address: PatchAddress,
	                  patches: Vec<AttributedPatch>,
	                  reason: String|
	 -> Result<(), MergeError> {
		let Some((handler, current_file)) = arbiter.as_mut() else {
			result.conflicts.push(PatchResolution::Conflict {
				address,
				patches,
				reason,
			});
			return Ok(());
		};
		current_conflict += 1;
		handler.set_conflict_progress(current_conflict, total_conflicts);
		apply_conflict_decision(
			result,
			&mut **handler,
			current_file,
			address,
			patches,
			reason,
		)
	};
	for resolution in pending_resolutions {
		match resolution {
			PatchResolution::Conflict {
				address,
				patches,
				reason,
			} => decide(&mut result, address, patches, reason)?,
			resolution => result.resolved.push(resolution),
		}
	}

	for cross_kind in cross_kind_conflicts {
		decide(
			&mut result,
			cross_kind.address,
			cross_kind.patches,
			cross_kind.reason,
		)?;
	}

	Ok(result)
}

fn drop_removed_ancestors_with_sibling_descendant_edits(
	mod_patches: Vec<(String, usize, Vec<ClausewitzPatch>)>,
	policies: &MergePolicies,
) -> (Vec<(String, usize, Vec<ClausewitzPatch>)>, usize) {
	if !policies.edit_wins_over_remove {
		return (mod_patches, 0);
	}

	let descendant_edits = mod_patches
		.iter()
		.flat_map(|(mod_id, _, patches)| {
			patches
				.iter()
				.filter(|patch| is_descendant_edit_patch(patch))
				.map(move |patch| (mod_id.clone(), patch_path_for_prefix_match(patch).to_vec()))
		})
		.collect::<Vec<_>>();
	let mut dropped = 0;
	let filtered = mod_patches
		.into_iter()
		.map(|(mod_id, precedence, patches)| {
			let patches = patches
				.into_iter()
				.filter(|patch| {
					let ClausewitzPatch::RemoveNode { path, key, .. } = patch else {
						return true;
					};
					let mut removed_path = path.clone();
					removed_path.push(key.clone());
					let has_sibling_descendant_edit =
						descendant_edits.iter().any(|(edit_mod_id, edit_path)| {
							edit_mod_id != &mod_id && edit_path.starts_with(&removed_path)
						});
					if has_sibling_descendant_edit {
						dropped += 1;
					}
					!has_sibling_descendant_edit
				})
				.collect();
			(mod_id, precedence, patches)
		})
		.collect();
	(filtered, dropped)
}

fn is_descendant_edit_patch(patch: &ClausewitzPatch) -> bool {
	matches!(
		patch,
		ClausewitzPatch::SetValue { .. }
			| ClausewitzPatch::ReplaceBlock { .. }
			| ClausewitzPatch::InsertNode { .. }
			| ClausewitzPatch::AppendListItem { .. }
			| ClausewitzPatch::AppendBlockItem { .. }
			| ClausewitzPatch::Rename { .. }
	)
}

fn patch_path_for_prefix_match(patch: &ClausewitzPatch) -> &[String] {
	match patch {
		ClausewitzPatch::SetValue { path, .. }
		| ClausewitzPatch::RemoveNode { path, .. }
		| ClausewitzPatch::InsertNode { path, .. }
		| ClausewitzPatch::AppendListItem { path, .. }
		| ClausewitzPatch::RemoveListItem { path, .. }
		| ClausewitzPatch::ReplaceBlock { path, .. }
		| ClausewitzPatch::AppendBlockItem { path, .. }
		| ClausewitzPatch::RemoveBlockItem { path, .. }
		| ClausewitzPatch::Rename { path, .. } => path,
	}
}

fn apply_list_policy(
	mod_patches: Vec<(String, usize, Vec<ClausewitzPatch>)>,
	policies: &MergePolicies,
) -> Vec<(String, usize, Vec<ClausewitzPatch>)> {
	let has_nested_semantic_identity = matches!(
		policies.nested_merge_key_source,
		MergeKeySource::ChildFieldValue { .. } | MergeKeySource::ContainerChildFieldValue { .. }
	);
	if policies.list == ListMergePolicy::Replace
		|| mod_patches.len() <= 1
		|| !has_nested_semantic_identity
	{
		return mod_patches;
	}
	let contributor_count = mod_patches
		.iter()
		.map(|(mod_id, _, _)| mod_id)
		.collect::<HashSet<_>>()
		.len();
	let mut removers_by_address: HashMap<PatchAddress, HashSet<String>> = HashMap::new();
	for (mod_id, _, patches) in &mod_patches {
		for patch in patches {
			if matches!(
				patch,
				ClausewitzPatch::RemoveListItem {
					value: AstValue::Scalar { .. },
					..
				}
			) {
				removers_by_address
					.entry(patch_address(patch, policies))
					.or_default()
					.insert(mod_id.clone());
			}
		}
	}

	mod_patches
		.into_iter()
		.map(|(mod_id, precedence, patches)| {
			let patches = patches
				.into_iter()
				.filter(|patch| {
					if !matches!(
						patch,
						ClausewitzPatch::RemoveListItem {
							value: AstValue::Scalar { .. },
							..
						}
					) {
						return true;
					}
					removers_by_address
						.get(&patch_address(patch, policies))
						.is_some_and(|removers| removers.len() == contributor_count)
				})
				.collect();
			(mod_id, precedence, patches)
		})
		.collect()
}

fn normalize_singleton_list_inserts(
	mod_patches: Vec<(String, usize, Vec<ClausewitzPatch>)>,
) -> Vec<(String, usize, Vec<ClausewitzPatch>)> {
	let list_addresses = mod_patches
		.iter()
		.flat_map(|(_, _, patches)| patches)
		.filter_map(|patch| match patch {
			ClausewitzPatch::AppendListItem { path, key, .. } => Some((path.clone(), key.clone())),
			_ => None,
		})
		.collect::<HashSet<_>>();
	if list_addresses.is_empty() {
		return mod_patches;
	}

	mod_patches
		.into_iter()
		.map(|(mod_id, precedence, patches)| {
			let patches = patches
				.into_iter()
				.map(|patch| match patch {
					ClausewitzPatch::InsertNode {
						path,
						key,
						statement: AstStatement::Assignment { value, .. },
					} if list_addresses.contains(&(path.clone(), key.clone())) => {
						ClausewitzPatch::AppendListItem {
							path,
							key,
							value,
							target_occurrence: ListItemTarget::new(0, 0, 0),
						}
					}
					other => other,
				})
				.collect();
			(mod_id, precedence, patches)
		})
		.collect()
}

const CROSS_MOD_INSERT_RENAME_THRESHOLD: f32 = 0.70;

fn drop_prefixed_rename_duplicate_inserts(
	mod_patches: Vec<(String, usize, Vec<ClausewitzPatch>)>,
) -> Vec<(String, usize, Vec<ClausewitzPatch>)> {
	let mut duplicate_losers: HashSet<usize> = HashSet::new();
	for left_idx in 0..mod_patches.len() {
		let Some(left) = single_root_insert(&mod_patches[left_idx].2) else {
			continue;
		};
		for right_idx in (left_idx + 1)..mod_patches.len() {
			let Some(right) = single_root_insert(&mod_patches[right_idx].2) else {
				continue;
			};
			if !prefixed_rename_insert_pair(left, right) {
				continue;
			}
			let loser = lower_precedence_insert(
				(left_idx, &mod_patches[left_idx].0, mod_patches[left_idx].1),
				(
					right_idx,
					&mod_patches[right_idx].0,
					mod_patches[right_idx].1,
				),
			);
			duplicate_losers.insert(loser);
		}
	}

	if duplicate_losers.is_empty() {
		return mod_patches;
	}

	mod_patches
		.into_iter()
		.enumerate()
		.map(|(idx, (mod_id, precedence, patches))| {
			if duplicate_losers.contains(&idx) {
				(mod_id, precedence, Vec::new())
			} else {
				(mod_id, precedence, patches)
			}
		})
		.collect()
}

fn single_root_insert(patches: &[ClausewitzPatch]) -> Option<(&str, &AstStatement)> {
	let [
		ClausewitzPatch::InsertNode {
			path,
			key,
			statement,
		},
	] = patches
	else {
		return None;
	};
	if path.is_empty() {
		Some((key, statement))
	} else {
		None
	}
}

fn prefixed_rename_insert_pair(left: (&str, &AstStatement), right: (&str, &AstStatement)) -> bool {
	let (left_key, left_stmt) = left;
	let (right_key, right_stmt) = right;
	if left_key == right_key || !prefixed_key_pair(left_key, right_key) {
		return false;
	}
	let (Some(left_value), Some(right_value)) = (
		assignment_statement_value(left_stmt),
		assignment_statement_value(right_stmt),
	) else {
		return false;
	};
	fuzzy_rename_similarity(left_value, right_value)
		.is_some_and(|score| score >= CROSS_MOD_INSERT_RENAME_THRESHOLD)
}

fn prefixed_key_pair(left: &str, right: &str) -> bool {
	left.contains(right) || right.contains(left)
}

fn assignment_statement_value(stmt: &AstStatement) -> Option<&AstValue> {
	match stmt {
		AstStatement::Assignment { value, .. } => Some(value),
		_ => None,
	}
}

fn lower_precedence_insert(left: (usize, &str, usize), right: (usize, &str, usize)) -> usize {
	let (left_idx, left_mod, left_precedence) = left;
	let (right_idx, right_mod, right_precedence) = right;
	if (left_precedence, left_mod) < (right_precedence, right_mod) {
		left_idx
	} else {
		right_idx
	}
}

// ---------------------------------------------------------------------------
// Per-address resolution
// ---------------------------------------------------------------------------

// ---------------------------------------------------------------------------
// Child modules
// ---------------------------------------------------------------------------

mod block_merge;
pub(crate) use block_merge::order_patches_by_source;
#[cfg(test)]
pub(crate) use block_merge::{
	ast_equal_ignoring_spans, child_identity, items_are_named_container,
	merge_named_container_bodies, rename_for_conflict,
};

mod rename;
use rename::{build_rename_map, rewrite_patch_for_renames};

#[cfg(test)]
mod tests;