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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
use crate::game::eu4::content::{
	DivergentBlockPolicy, MergePolicies, NamedContainerPolicy, ScalarMergePolicy,
};
use crate::game::eu4::script::parser::{AstStatement, AstValue, ScalarValue};

use super::super::patch::{ClausewitzPatch, patches_semantically_equal};
use super::address::patch_kind;
use super::block_merge::{
	synthesize_boolean_or, synthesize_scroll_stacked_insert, try_recursive_block_merge,
	try_recursive_insert_merge, try_recursive_remove_replace_merge,
	try_replace_block_named_container_merge, try_union_block_merge,
};
use super::rename::resolve_renames;
use super::{AttributedPatch, PatchAddress, PatchMergeStats, PatchResolution};

/// Try to parse an `AstValue` as f64 for numeric merge policies.
fn try_parse_f64(val: &AstValue) -> Option<f64> {
	match val {
		AstValue::Scalar {
			value: ScalarValue::Number(n),
			..
		} => n.parse::<f64>().ok(),
		_ => None,
	}
}

fn number_text(value: &AstValue) -> Option<&str> {
	match value {
		AstValue::Scalar {
			value: ScalarValue::Number(number),
			..
		} => Some(number),
		_ => None,
	}
}

/// Build a synthetic numeric scalar reusing the span from a reference value.
fn make_number_value(text: String, reference_span: &AstValue) -> AstValue {
	AstValue::Scalar {
		value: ScalarValue::Number(text),
		span: reference_span.span().clone(),
	}
}

fn is_coordinate_scalar_key(key: &str) -> bool {
	matches!(key, "x" | "y")
}

fn is_gui_layout_scalar_key(key: &str) -> bool {
	is_coordinate_scalar_key(key) || matches!(key, "maxWidth" | "maxHeight")
}

fn is_gui_reference_scalar_key(key: &str) -> bool {
	matches!(key, "spriteType")
}

fn all_values_numeric(values: impl IntoIterator<Item = AstValue>) -> bool {
	values
		.into_iter()
		.all(|value| try_parse_f64(&value).is_some())
}

/// Patch kinds that represent a mod *modifying an existing* property at an
/// address (as opposed to adding, removing, or list/rename ops). Used by the
/// edit-vs-remove resolution: a modification wins over a sibling `RemoveNode`.
/// `InsertNode` is intentionally excluded — an add-vs-remove collision is a
/// genuine presence disagreement and stays a conflict.
fn is_edit_kind(kind: &str) -> bool {
	matches!(kind, "SetValue" | "ReplaceBlock")
}

pub(super) fn resolve_address(
	addr: PatchAddress,
	attributed: Vec<AttributedPatch>,
	policies: &MergePolicies,
	stats: &mut PatchMergeStats,
) -> PatchResolution {
	// --- Single mod case ---
	if attributed.len() == 1 {
		stats.single_mod_patches += 1;
		return PatchResolution::Resolved(attributed.into_iter().next().unwrap().patch);
	}

	// --- Convergence: all patches semantically equal (ignoring spans and
	// comment/whitespace trivia) ---
	if attributed
		.windows(2)
		.all(|w| patches_semantically_equal(&w[0].patch, &w[1].patch))
	{
		stats.convergent_patches += 1;
		return PatchResolution::Resolved(attributed.into_iter().next().unwrap().patch);
	}

	// --- Mixed patch kinds (e.g. InsertNode + RemoveNode) → conflict, EXCEPT
	// edit-vs-remove which resolves edit-wins (see below). ---
	let kinds: Vec<&str> = attributed.iter().map(|a| patch_kind(&a.patch)).collect();
	let has_mixed_kinds = kinds.windows(2).any(|w| w[0] != w[1]);

	if has_mixed_kinds {
		let distinct: std::collections::BTreeSet<&str> = kinds.iter().copied().collect();
		if distinct == std::collections::BTreeSet::from(["RemoveNode", "ReplaceBlock"])
			&& policies.divergent_block_policy_for_key(&addr.key) == DivergentBlockPolicy::Recurse
			&& let Some(resolution) =
				try_recursive_remove_replace_merge(&addr, &attributed, policies, stats)
		{
			return resolution;
		}

		// edit-wins: when the only divergence is one mod editing a property
		// (SetValue / ReplaceBlock / InsertNode) while another removes it
		// (RemoveNode), keep the edit and drop the removes. A "remove" at a GUI
		// property is typically the mod simply not re-shipping that field (e.g.
		// a trimmed widget copy), not an intentional delete that should veto
		// another mod's edit. Only fires when the non-remove patches share a
		// single edit kind; genuinely irreconcilable mixes still conflict, and
		// edit-vs-edit (no removes) flows through the same-kind resolvers which
		// preserve real scalar-disagreement conflicts.
		let edit_kinds: Vec<&str> = distinct
			.iter()
			.copied()
			.filter(|kind| *kind != "RemoveNode")
			.collect();
		if distinct.contains("RemoveNode")
			&& policies.edit_wins_over_remove
			&& edit_kinds.len() == 1
			&& is_edit_kind(edit_kinds[0])
		{
			let kept_kind = edit_kinds[0];
			let edits: Vec<AttributedPatch> = attributed
				.into_iter()
				.filter(|a| patch_kind(&a.patch) != "RemoveNode")
				.collect();
			stats.edit_over_remove_resolved += 1;
			if edits.len() == 1 {
				return PatchResolution::Resolved(edits.into_iter().next().unwrap().patch);
			}
			return match kept_kind {
				"SetValue" => resolve_set_values(addr, edits, policies, stats),
				"ReplaceBlock" => resolve_replace_blocks(addr, edits, policies, stats),
				_ => unreachable!("is_edit_kind restricts to the two handled kinds"),
			};
		}

		stats.conflict_patches += 1;
		return PatchResolution::Conflict {
			address: addr,
			reason: format!("mixed patch kinds at same address: {}", kinds.join(", ")),
			patches: attributed,
		};
	}

	// From here on, all patches have the same variant kind.
	let kind = kinds[0];
	match kind {
		"InsertNode" => resolve_insert_nodes(addr, attributed, policies, stats),
		"AppendListItem" => resolve_append_list_items(addr, attributed, stats),
		"SetValue" => resolve_set_values(addr, attributed, policies, stats),
		"RemoveNode" => resolve_remove_convergent(addr, attributed, stats),
		"RemoveListItem" => resolve_remove_list_items(addr, attributed, stats),
		"ReplaceBlock" => resolve_replace_blocks(addr, attributed, policies, stats),
		"AppendBlockItem" => resolve_append_block_items(addr, attributed, stats),
		"RemoveBlockItem" => resolve_remove_block_items(addr, attributed, stats),
		"Rename" => resolve_renames(addr, attributed, stats),
		_ => {
			stats.conflict_patches += 1;
			PatchResolution::Conflict {
				address: addr,
				reason: format!("unhandled patch kind: {kind}"),
				patches: attributed,
			}
		}
	}
}

// ---------------------------------------------------------------------------
// Kind-specific resolvers
// ---------------------------------------------------------------------------

/// Multiple mods inserting new nodes at the same path with different keys →
/// compatible if keys differ (both apply). Conflict if same key with
/// different statements.
fn resolve_insert_nodes(
	addr: PatchAddress,
	attributed: Vec<AttributedPatch>,
	policies: &MergePolicies,
	stats: &mut PatchMergeStats,
) -> PatchResolution {
	// Extract the inserted statements.
	let stmts: Vec<_> = attributed
		.iter()
		.map(|a| match &a.patch {
			ClausewitzPatch::InsertNode { statement, .. } => statement,
			_ => unreachable!(),
		})
		.collect();

	// If all statements are semantically equal → convergent.
	if stmts
		.windows(2)
		.all(|w| crate::merge::patch::ast_statements_semantically_equal(w[0], w[1]))
	{
		stats.convergent_patches += 1;
		return PatchResolution::Resolved(attributed.into_iter().next().unwrap().patch);
	}

	if policies.named_container == NamedContainerPolicy::ScrollStack
		&& let Some(synth) = synthesize_scroll_stacked_insert(&attributed)
	{
		let mods: Vec<String> = attributed.iter().map(|a| a.mod_id.clone()).collect();
		stats.auto_merged_patches += 1;
		return PatchResolution::AutoMerged {
			result: synth,
			strategy: "scroll_stack".to_string(),
			contributing_mods: mods,
		};
	}

	if let Some(resolution) = resolve_assignment_insert_nodes(&addr, &attributed, policies, stats) {
		return resolution;
	}

	// BooleanOr policy: when each contributor inserts a block-bodied
	// statement under the same key, combine their bodies as a single
	// `OR = { ... }` of disjuncts and emit one synthesized InsertNode.
	if policies.divergent_block_policy_for_key(&addr.key) == DivergentBlockPolicy::BooleanOr
		&& let Some(synth) = synthesize_boolean_or(&addr, &attributed)
	{
		let mods: Vec<String> = attributed.iter().map(|a| a.mod_id.clone()).collect();
		stats.auto_merged_patches += 1;
		return PatchResolution::AutoMerged {
			result: synth,
			strategy: "boolean_or".to_string(),
			contributing_mods: mods,
		};
	}

	if policies.divergent_block_policy_for_key(&addr.key) == DivergentBlockPolicy::Recurse
		&& let Some(resolution) = try_recursive_insert_merge(&addr, &attributed, policies, stats)
	{
		return resolution;
	}

	// Different statements at the same (path, key) from sibling mods → real
	// conflict. The fingerprint scheme already routed list-like content
	// (Union policy) through distinct addresses earlier; anything that
	// reaches this default branch is a unique-key collision the engine
	// must escalate instead of silently picking the highest-precedence
	// patch. Per the project rule, ambiguous merges must surface as
	// conflicts rather than fall back to LastWriter behind the user's back.
	let summaries: Vec<String> = attributed
		.iter()
		.map(|a| match &a.patch {
			ClausewitzPatch::InsertNode { statement, .. } => format!(
				"`{}` from `{}`",
				statement_text_for_reason(statement),
				a.mod_id
			),
			_ => unreachable!(),
		})
		.collect();
	stats.conflict_patches += 1;
	PatchResolution::Conflict {
		address: addr,
		reason: format!(
			"sibling mods inserted divergent statements at the same key: {}",
			summaries.join(", ")
		),
		patches: attributed,
	}
}

fn resolve_assignment_insert_nodes(
	_addr: &PatchAddress,
	attributed: &[AttributedPatch],
	policies: &MergePolicies,
	stats: &mut PatchMergeStats,
) -> Option<PatchResolution> {
	let inserted: Vec<(&AttributedPatch, &AstValue)> = attributed
		.iter()
		.filter_map(|patch| match &patch.patch {
			ClausewitzPatch::InsertNode {
				statement:
					AstStatement::Assignment {
						value: value @ AstValue::Scalar { .. },
						..
					},
				..
			} => Some((patch, value)),
			_ => None,
		})
		.collect();
	if inserted.len() != attributed.len() {
		return None;
	}

	match policies.scalar {
		ScalarMergePolicy::Conflict => None,
		ScalarMergePolicy::LastWriter => {
			let winner = inserted
				.iter()
				.max_by_key(|(patch, _)| (patch.precedence, patch.mod_id.clone()))?
				.0;
			stats.auto_merged_patches += 1;
			Some(auto_merge_insert_winner(attributed, winner, "LastWriter"))
		}
		ScalarMergePolicy::CoordinateFirstWriter => {
			if !is_coordinate_scalar_key(&_addr.key)
				|| !inserted
					.iter()
					.all(|(_, value)| try_parse_f64(value).is_some())
			{
				return None;
			}
			let winner = inserted
				.iter()
				.min_by_key(|(patch, _)| (patch.precedence, patch.mod_id.clone()))?
				.0;
			let mods: Vec<String> = attributed.iter().map(|a| a.mod_id.clone()).collect();
			stats.auto_merged_patches += 1;
			Some(PatchResolution::AutoMerged {
				result: winner.patch.clone(),
				strategy: "CoordinateFirstWriter".to_string(),
				contributing_mods: mods,
			})
		}
		ScalarMergePolicy::GuiWidget => {
			if is_gui_layout_scalar_key(&_addr.key)
				&& inserted
					.iter()
					.all(|(_, value)| try_parse_f64(value).is_some())
			{
				let winner = inserted
					.iter()
					.min_by_key(|(patch, _)| (patch.precedence, patch.mod_id.clone()))?
					.0;
				stats.auto_merged_patches += 1;
				return Some(auto_merge_insert_winner(
					attributed,
					winner,
					"GuiLayoutFirstWriter",
				));
			}
			if is_gui_reference_scalar_key(&_addr.key) {
				let winner = inserted
					.iter()
					.max_by_key(|(patch, _)| (patch.precedence, patch.mod_id.clone()))?
					.0;
				stats.auto_merged_patches += 1;
				return Some(auto_merge_insert_winner(
					attributed,
					winner,
					"GuiReferenceLastWriter",
				));
			}
			None
		}
		ScalarMergePolicy::Sum
		| ScalarMergePolicy::Avg
		| ScalarMergePolicy::Max
		| ScalarMergePolicy::Min => {
			let values: Vec<AstValue> =
				inserted.iter().map(|(_, value)| (*value).clone()).collect();
			let merged_value = merge_numeric_values(&values, policies.scalar)?;
			let first = attributed.first()?;
			let result = match &first.patch {
				ClausewitzPatch::InsertNode {
					path,
					key,
					statement: AstStatement::Assignment { key_span, span, .. },
				} => ClausewitzPatch::InsertNode {
					path: path.clone(),
					key: key.clone(),
					statement: AstStatement::Assignment {
						key: key.clone(),
						key_span: key_span.clone(),
						value: merged_value,
						span: span.clone(),
					},
				},
				_ => return None,
			};
			let mods: Vec<String> = attributed.iter().map(|a| a.mod_id.clone()).collect();
			stats.auto_merged_patches += 1;
			Some(PatchResolution::AutoMerged {
				result,
				strategy: format!("{:?}", policies.scalar),
				contributing_mods: mods,
			})
		}
	}
}

fn auto_merge_insert_winner(
	attributed: &[AttributedPatch],
	winner: &AttributedPatch,
	strategy: &str,
) -> PatchResolution {
	let mods: Vec<String> = attributed.iter().map(|a| a.mod_id.clone()).collect();
	PatchResolution::AutoMerged {
		result: winner.patch.clone(),
		strategy: strategy.to_string(),
		contributing_mods: mods,
	}
}

/// Multiple mods appending the same list item. Because `patch_address`
/// includes the value fingerprint, every patch in this group has identical
/// `value` → always convergent.
fn resolve_append_list_items(
	_addr: PatchAddress,
	attributed: Vec<AttributedPatch>,
	stats: &mut PatchMergeStats,
) -> PatchResolution {
	stats.convergent_patches += 1;
	PatchResolution::Resolved(attributed.into_iter().next().unwrap().patch)
}

/// Multiple mods setting the same scalar to different values. Resolve via
/// `policies.scalar`.
fn resolve_set_values(
	addr: PatchAddress,
	attributed: Vec<AttributedPatch>,
	policies: &MergePolicies,
	stats: &mut PatchMergeStats,
) -> PatchResolution {
	match policies.scalar {
		ScalarMergePolicy::Conflict => conflict_set_values(addr, attributed, stats),
		ScalarMergePolicy::Sum
		| ScalarMergePolicy::Avg
		| ScalarMergePolicy::Max
		| ScalarMergePolicy::Min => resolve_numeric_set_values(addr, attributed, policies.scalar, stats),
		ScalarMergePolicy::LastWriter => resolve_last_writer_set_value(attributed, stats),
		ScalarMergePolicy::CoordinateFirstWriter => {
			let numeric = all_values_numeric(attributed.iter().map(|a| match &a.patch {
				ClausewitzPatch::SetValue { new_value, .. } => new_value.clone(),
				_ => unreachable!(),
			}));
			if is_coordinate_scalar_key(&addr.key) && numeric {
				resolve_first_writer_set_value(attributed, stats)
			} else {
				conflict_set_values(addr, attributed, stats)
			}
		}
		ScalarMergePolicy::GuiWidget => {
			let numeric = all_values_numeric(attributed.iter().map(|a| match &a.patch {
				ClausewitzPatch::SetValue { new_value, .. } => new_value.clone(),
				_ => unreachable!(),
			}));
			if is_gui_layout_scalar_key(&addr.key) && numeric {
				resolve_first_writer_set_value_with_strategy(
					attributed,
					stats,
					"GuiLayoutFirstWriter",
				)
			} else if is_gui_reference_scalar_key(&addr.key) {
				resolve_last_writer_set_value_with_strategy(
					attributed,
					stats,
					"GuiReferenceLastWriter",
				)
			} else {
				conflict_set_values(addr, attributed, stats)
			}
		}
	}
}

fn conflict_set_values(
	addr: PatchAddress,
	attributed: Vec<AttributedPatch>,
	stats: &mut PatchMergeStats,
) -> PatchResolution {
	// Sibling mods at the same scalar leaf cannot be silently merged: there is
	// no dependency-graph signal saying which mod's value should win. Surface a
	// conflict so the user (or `[[resolutions]]`) can pick.
	let new_values: Vec<String> = attributed
		.iter()
		.map(|a| match &a.patch {
			ClausewitzPatch::SetValue { new_value, .. } => {
				format!("`{}` from `{}`", value_text_for_reason(new_value), a.mod_id)
			}
			_ => unreachable!(),
		})
		.collect();
	stats.conflict_patches += 1;
	PatchResolution::Conflict {
		address: addr,
		reason: format!(
			"sibling mods set the same scalar to divergent values: {}",
			new_values.join(", ")
		),
		patches: attributed,
	}
}

/// Best-effort textual rendering of an `AstValue` for human-readable conflict
/// messages. Falls back to a structural marker when the value is a block.
fn value_text_for_reason(value: &AstValue) -> String {
	match value {
		AstValue::Scalar { value, .. } => value.as_text(),
		AstValue::Block { .. } => "<block>".to_string(),
	}
}

fn statement_text_for_reason(stmt: &AstStatement) -> String {
	match stmt {
		AstStatement::Assignment { key, value, .. } => {
			format!("{key} = {}", value_text_for_reason(value))
		}
		AstStatement::Item { value, .. } => value_text_for_reason(value),
		AstStatement::Comment { .. } => "<comment>".to_string(),
	}
}

fn resolve_numeric_set_values(
	addr: PatchAddress,
	attributed: Vec<AttributedPatch>,
	policy: ScalarMergePolicy,
	stats: &mut PatchMergeStats,
) -> PatchResolution {
	// Collect all new_values (owned) so we don't hold borrows across the move.
	let new_values: Vec<AstValue> = attributed
		.iter()
		.map(|a| match &a.patch {
			ClausewitzPatch::SetValue { new_value, .. } => new_value.clone(),
			_ => unreachable!(),
		})
		.collect();

	let Some(merged_value) = merge_numeric_values(&new_values, policy) else {
		stats.conflict_patches += 1;
		return PatchResolution::Conflict {
			address: addr,
			reason: format!(
				"numeric merge policy {:?} but not all values are numeric",
				policy
			),
			patches: attributed,
		};
	};
	let mods: Vec<String> = attributed.iter().map(|a| a.mod_id.clone()).collect();
	let first = attributed.into_iter().next().unwrap();
	let strategy = format!("{policy:?}");

	let result = match first.patch {
		ClausewitzPatch::SetValue {
			path,
			key,
			old_value,
			..
		} => ClausewitzPatch::SetValue {
			path,
			key,
			old_value,
			new_value: merged_value,
		},
		_ => unreachable!(),
	};

	stats.auto_merged_patches += 1;
	PatchResolution::AutoMerged {
		result,
		strategy,
		contributing_mods: mods,
	}
}

fn merge_numeric_values(values: &[AstValue], policy: ScalarMergePolicy) -> Option<AstValue> {
	let inputs = values.iter().map(number_text).collect::<Option<Vec<_>>>()?;
	let merged = policy.reduce_numeric_values(inputs)?;
	Some(make_number_value(merged, values.first()?))
}

fn resolve_last_writer_set_value(
	attributed: Vec<AttributedPatch>,
	stats: &mut PatchMergeStats,
) -> PatchResolution {
	resolve_last_writer_set_value_with_strategy(attributed, stats, "LastWriter")
}

fn resolve_last_writer_set_value_with_strategy(
	attributed: Vec<AttributedPatch>,
	stats: &mut PatchMergeStats,
	strategy: &str,
) -> PatchResolution {
	let mods: Vec<String> = attributed.iter().map(|a| a.mod_id.clone()).collect();
	let winner = attributed
		.into_iter()
		.max_by_key(|patch| (patch.precedence, patch.mod_id.clone()))
		.expect("at least one attributed patch");
	stats.auto_merged_patches += 1;
	PatchResolution::AutoMerged {
		result: winner.patch,
		strategy: strategy.to_string(),
		contributing_mods: mods,
	}
}

fn resolve_first_writer_set_value(
	attributed: Vec<AttributedPatch>,
	stats: &mut PatchMergeStats,
) -> PatchResolution {
	resolve_first_writer_set_value_with_strategy(attributed, stats, "CoordinateFirstWriter")
}

fn resolve_first_writer_set_value_with_strategy(
	attributed: Vec<AttributedPatch>,
	stats: &mut PatchMergeStats,
	strategy: &str,
) -> PatchResolution {
	let mods: Vec<String> = attributed.iter().map(|a| a.mod_id.clone()).collect();
	let winner = attributed
		.into_iter()
		.min_by_key(|patch| (patch.precedence, patch.mod_id.clone()))
		.expect("at least one attributed patch");
	stats.auto_merged_patches += 1;
	PatchResolution::AutoMerged {
		result: winner.patch,
		strategy: strategy.to_string(),
		contributing_mods: mods,
	}
}

/// Both mods remove the same node → convergent (apply once).
fn resolve_remove_convergent(
	_addr: PatchAddress,
	attributed: Vec<AttributedPatch>,
	stats: &mut PatchMergeStats,
) -> PatchResolution {
	// All are RemoveNode at same address. Even if the removed statement
	// differs slightly (span), the intent is the same.
	stats.convergent_patches += 1;
	PatchResolution::Resolved(attributed.into_iter().next().unwrap().patch)
}

/// Multiple mods removing the same list item. Because `patch_address`
/// includes the value fingerprint, every patch in this group has identical
/// `value` → always convergent.
fn resolve_remove_list_items(
	_addr: PatchAddress,
	attributed: Vec<AttributedPatch>,
	stats: &mut PatchMergeStats,
) -> PatchResolution {
	stats.convergent_patches += 1;
	PatchResolution::Resolved(attributed.into_iter().next().unwrap().patch)
}

/// Multiple mods appending the same in-block Item value (e.g. both add `TRE`
/// to `allowed_tags`). Because `patch_address` includes the value fingerprint,
/// every patch in this group has identical `value` → always convergent.
fn resolve_append_block_items(
	_addr: PatchAddress,
	attributed: Vec<AttributedPatch>,
	stats: &mut PatchMergeStats,
) -> PatchResolution {
	stats.convergent_patches += 1;
	PatchResolution::Resolved(attributed.into_iter().next().unwrap().patch)
}

/// Multiple mods removing the same in-block Item value. Same convergence
/// reasoning as `resolve_append_block_items`.
fn resolve_remove_block_items(
	_addr: PatchAddress,
	attributed: Vec<AttributedPatch>,
	stats: &mut PatchMergeStats,
) -> PatchResolution {
	stats.convergent_patches += 1;
	PatchResolution::Resolved(attributed.into_iter().next().unwrap().patch)
}

/// Multiple mods replacing the same block → if `DivergentBlockPolicy::BooleanOr`,
/// wrap each contributor's body in `OR = { ... }` and emit a synthesized
/// `ReplaceBlock`. Otherwise try a named-container 3-way merge. Fall back to
/// a conflict when neither strategy applies.
fn resolve_replace_blocks(
	addr: PatchAddress,
	attributed: Vec<AttributedPatch>,
	policies: &MergePolicies,
	stats: &mut PatchMergeStats,
) -> PatchResolution {
	// Defensive: although `resolve_address` already collapses semantically
	// equal patches, callers may invoke this resolver directly. Treat
	// format-only divergence (whitespace / comment trivia) as convergent.
	if attributed
		.windows(2)
		.all(|w| patches_semantically_equal(&w[0].patch, &w[1].patch))
	{
		let mods: Vec<String> = attributed.iter().map(|a| a.mod_id.clone()).collect();
		stats.auto_merged_patches += 1;
		return PatchResolution::AutoMerged {
			result: attributed.into_iter().next().unwrap().patch,
			strategy: "semantic_equivalence".to_string(),
			contributing_mods: mods,
		};
	}

	let block_policy = policies.divergent_block_policy_for_key(&addr.key);
	if block_policy == DivergentBlockPolicy::BooleanOr
		&& let Some(synth) = synthesize_boolean_or(&addr, &attributed)
	{
		let mods: Vec<String> = attributed.iter().map(|a| a.mod_id.clone()).collect();
		stats.auto_merged_patches += 1;
		return PatchResolution::AutoMerged {
			result: synth,
			strategy: "boolean_or".to_string(),
			contributing_mods: mods,
		};
	}

	if block_policy == DivergentBlockPolicy::Union
		&& let Some(merged) = try_union_block_merge(&attributed)
	{
		let mods: Vec<String> = attributed.iter().map(|a| a.mod_id.clone()).collect();
		stats.auto_merged_patches += 1;
		return PatchResolution::AutoMerged {
			result: merged,
			strategy: "union_block".to_string(),
			contributing_mods: mods,
		};
	}

	if block_policy == DivergentBlockPolicy::Recurse
		&& let Some(resolution) = try_recursive_block_merge(&addr, &attributed, policies, stats)
	{
		return resolution;
	}

	if let Some(merged) = try_replace_block_named_container_merge(&attributed, policies) {
		let mods: Vec<String> = attributed.iter().map(|a| a.mod_id.clone()).collect();
		stats.auto_merged_patches += 1;
		return PatchResolution::AutoMerged {
			result: merged,
			strategy: "named_container_union".to_string(),
			contributing_mods: mods,
		};
	}

	// Identical or semantically-equivalent replacements were already caught
	// above, so reaching here means different replacements → conflict.
	stats.conflict_patches += 1;
	PatchResolution::Conflict {
		address: addr,
		reason: "multiple mods replace the same block with different content".to_string(),
		patches: attributed,
	}
}