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
pub(crate) mod runtime;

use crate::check::runtime::{build_overlap_findings, build_runtime_state_from_input};
use crate::game::eu4::analysis::rules::{
	check_dependency_misuse, check_duplicate_mod_identity, check_duplicate_scripted_effect,
	check_file_conflict, check_missing_dependency, check_missing_descriptor, check_required_fields,
	check_version_mismatch,
};
use crate::game::eu4::analysis::{AnalyzeOptions, analyze_visibility_with_vanilla_index};
use crate::game::eu4::base::vanilla_index::{VanillaSymbolIndex, VanillaSymbolSource};
use crate::game::eu4::content::eu4;
use crate::input::request::{CheckOptions, InputRequest};
use crate::input::{
	InputResolveErrorKind, LoadedModSnapshot, ResolvedInput, ResolvedInputContributor,
	resolve_input,
};
use crate::merge::dag::{ModDag, ModId, build_mod_dag};
use crate::merge::namespace::{
	KeyContributor, build_family_key_index, detect_key_conflicts, group_by_family,
};
use crate::model::{
	AnalysisMeta, AnalysisMode, CheckContext, CheckResult, DocumentFamily, FamilyParseStats,
	Finding, FindingChannel, GamePathBuf, ParseFamilyStats, ParseIssueReportItem, SemanticIndex,
	Severity, SymbolDefinition, case_only_path_groups,
};
use std::collections::{BTreeMap, HashMap, HashSet};
use std::time::Instant;

/// Tracing target for `foch check` (and `foch merge`'s revalidation pass)
/// progress events. The CLI enables INFO-level output for this target on the
/// relevant commands so users see a stage-by-stage progress trail instead of
/// staring at a silent terminal during the 30-second-plus end-to-end run.
pub const CHECK_PROGRESS_TARGET: &str = "crate::check::progress";

fn run_progress_stage<T, F, S>(stage: &'static str, f: F, summarize: S) -> T
where
	F: FnOnce() -> T,
	S: FnOnce(&T) -> String,
{
	tracing::info!(target: CHECK_PROGRESS_TARGET, "check {stage}: start");
	let started = Instant::now();
	let value = f();
	let elapsed_ms = started.elapsed().as_millis() as u64;
	crate::merge::timing::record_stage(&format!("check {stage}"), started);
	let summary = summarize(&value);
	if summary.is_empty() {
		tracing::info!(target: CHECK_PROGRESS_TARGET, elapsed_ms, "check {stage}: done");
	} else {
		tracing::info!(
			target: CHECK_PROGRESS_TARGET,
			elapsed_ms,
			summary = %summary,
			"check {stage}: done",
		);
	}
	value
}

#[derive(Clone, Debug)]
struct GameBaseSemanticSnapshot {
	index: SemanticIndex,
	parsed_files: usize,
	parse_error_count: usize,
	parse_stats: ParseFamilyStats,
}

impl VanillaSymbolSource for ResolvedInput {
	fn visit_vanilla_definitions(&self, visit: &mut dyn FnMut(&SymbolDefinition)) {
		let Some(installed) = self.installed_base_snapshot.as_ref() else {
			return;
		};
		let index = installed.snapshot.to_semantic_index();
		for definition in &index.definitions {
			visit(definition);
		}
	}
}

pub fn run_checks(request: InputRequest) -> CheckResult {
	run_checks_with_options(request, CheckOptions::default())
}

pub fn run_checks_with_options(request: InputRequest, options: CheckOptions) -> CheckResult {
	let mut result = CheckResult::default();

	let resolved = run_progress_stage(
		"resolve input",
		|| resolve_input(&request, options.include_game_base),
		|res| match res {
			Ok(input) => {
				let cache_hits = input
					.mod_snapshots
					.iter()
					.flatten()
					.filter(|snapshot| snapshot.cache_hit)
					.count();
				let cache_misses = input
					.mod_snapshots
					.iter()
					.flatten()
					.filter(|snapshot| !snapshot.cache_hit)
					.count();
				format!(
					"mods={} inventory_files={} cache_hits={} cache_misses={}",
					input.mods.len(),
					input.file_inventory.len(),
					cache_hits,
					cache_misses
				)
			}
			Err(_) => "failed".to_string(),
		},
	);
	let resolved = match resolved {
		Ok(input) => input,
		Err(err) => {
			if err.kind == InputResolveErrorKind::PlaylistFormat {
				result.findings.push(Finding {
					rule_id: "playset-parse-error".to_string(),
					severity: Severity::Error,
					channel: FindingChannel::Strict,
					message: "failed to parse Playset JSON".to_string(),
					mod_id: None,
					path: None,
					source_file: Some(err.path),
					evidence: Some(err.message),
					line: None,
					column: None,
					confidence: Some(1.0),
				});
				result.recompute_channels();
				return result;
			}
			result.push_fatal_error(err.message);
			return result;
		}
	};

	let base_semantic = run_progress_stage(
		"base semantic index",
		|| {
			resolved
				.installed_base_snapshot
				.as_ref()
				.map(|installed| GameBaseSemanticSnapshot {
					index: installed.snapshot.to_semantic_index(),
					parsed_files: installed.snapshot.parsed_files,
					parse_error_count: installed.snapshot.parse_error_count,
					parse_stats: installed.snapshot.parse_stats.clone(),
				})
		},
		|_| String::new(),
	);
	let mod_parsed_files_count: usize = resolved
		.mod_snapshots
		.iter()
		.flatten()
		.map(|snapshot| snapshot.parsed_files)
		.sum();
	let mod_parse_error_count: usize = resolved
		.mod_snapshots
		.iter()
		.flatten()
		.map(|snapshot| snapshot.parse_error_count)
		.sum();
	let mod_parse_stats = resolved
		.mod_snapshots
		.iter()
		.flatten()
		.fold(ParseFamilyStats::default(), |acc, snapshot| {
			sum_parse_family_stats(acc, snapshot.parse_stats.clone())
		});
	let mod_semantic_index = run_progress_stage(
		"mod semantic indexes",
		|| merge_mod_snapshots(&resolved.mod_snapshots),
		|_| String::new(),
	);
	let parsed_files_count = mod_parsed_files_count
		+ base_semantic
			.as_ref()
			.map_or(0, |snapshot| snapshot.parsed_files);
	let base_parse_error_count = base_semantic
		.as_ref()
		.map_or(0, |snapshot| snapshot.parse_error_count);
	let total_parse_stats = base_semantic
		.as_ref()
		.map_or(mod_parse_stats.clone(), |snapshot| {
			sum_parse_family_stats(snapshot.parse_stats.clone(), mod_parse_stats.clone())
		});
	let semantic_index = run_progress_stage(
		"merge semantic indexes",
		|| match base_semantic {
			Some(snapshot) => {
				let mut index = snapshot.index;
				index.append(mod_semantic_index);
				index
			}
			None => mod_semantic_index,
		},
		|index| {
			format!(
				"definitions={} references={} scopes={}",
				index.definitions.len(),
				index.references.len(),
				index.scopes.len()
			)
		},
	);
	let game_version = resolved
		.installed_base_snapshot
		.as_ref()
		.map(|installed| installed.snapshot.game_version.clone());
	let vanilla_symbol_index = run_progress_stage(
		"vanilla symbol index",
		|| VanillaSymbolIndex::build(&resolved),
		|_| String::new(),
	);
	let (mod_dag, _dag_diagnostics) = build_mod_dag(&resolved.mods);
	let runtime_overlap_findings = if options.analysis_mode == AnalysisMode::Semantic {
		let runtime_overlap = run_progress_stage(
			"runtime overlap",
			|| {
				build_runtime_state_from_input(&resolved)
					.map(|state| build_overlap_findings(&state))
			},
			|runtime_overlap| match runtime_overlap {
				Ok(findings) => format!("findings={}", findings.len()),
				Err(_) => "failed".to_string(),
			},
		);
		match runtime_overlap {
			Ok(findings) => findings,
			Err(error) => {
				result.push_fatal_error(format!("failed to build runtime overlap state: {error}"));
				return result;
			}
		}
	} else {
		Vec::new()
	};
	let ctx = CheckContext {
		playlist_path: resolved.playlist_path.clone(),
		playlist: resolved.playlist,
		mods: resolved.mods,
		semantic_index,
	};

	run_progress_stage(
		"structural rules",
		|| {
			result.findings.extend(check_required_fields(&ctx));
			result.findings.extend(check_duplicate_mod_identity(&ctx));
			result.findings.extend(check_missing_descriptor(&ctx));
			result.findings.extend(check_file_conflict(&ctx));
			result.findings.extend(check_missing_dependency(&ctx));
			if let Some(game_version) = game_version.as_deref() {
				result
					.findings
					.extend(check_version_mismatch(&ctx, game_version));
			}
		},
		|_| String::new(),
	);

	if options.analysis_mode == AnalysisMode::Semantic {
		// Names already covered by the runtime overlap module (`mergeable-overlap`
		// or `cross-mod-overshadow`). `duplicate-scripted-effect` and
		// `cross-file-duplicate-symbol` are redundant for these names — the
		// overlap finding is more specific. Collect the covered names so we can
		// suppress the duplicates downstream.
		let overlap_covered_names: HashSet<String> = runtime_overlap_findings
			.iter()
			.filter(|finding| {
				finding.rule_id == "cross-mod-overshadow" || finding.rule_id == "mergeable-overlap"
			})
			.filter_map(|finding| extract_overlap_symbol_name(&finding.message))
			.collect();

		let diagnostics = run_progress_stage(
			"semantic visibility",
			|| {
				analyze_visibility_with_vanilla_index(
					&ctx.semantic_index,
					&AnalyzeOptions {
						mode: options.analysis_mode,
					},
					Some(&vanilla_symbol_index),
				)
			},
			|d| format!("strict={} advisory={}", d.strict.len(), d.advisory.len()),
		);
		result.findings.extend(
			diagnostics
				.strict
				.into_iter()
				.filter(|finding| finding.rule_id != "cross-mod-overshadow"),
		);
		result.findings.extend(
			diagnostics
				.advisory
				.into_iter()
				.filter(|finding| finding.rule_id != "mergeable-overlap"),
		);
		result.findings.extend(runtime_overlap_findings);
		result.findings.extend(run_progress_stage(
			"dependency misuse",
			|| check_dependency_misuse(&ctx),
			|findings| format!("findings={}", findings.len()),
		));
		let namespace_findings = run_progress_stage(
			"namespace conflicts",
			|| check_namespace_conflicts(&resolved.file_inventory, &mod_dag),
			|findings| format!("findings={}", findings.len()),
		);
		result
			.findings
			.extend(namespace_findings.into_iter().filter(|finding| {
				!finding
					.evidence
					.as_deref()
					.and_then(extract_namespace_key)
					.is_some_and(|key| overlap_covered_names.contains(key))
			}));
	} else {
		// Basic mode: no overlap module runs, so the heuristic
		// `duplicate-scripted-effect` still provides value for scripted-effect
		// duplicates.
		result
			.findings
			.extend(check_duplicate_scripted_effect(&ctx));
	}
	result
		.findings
		.extend(check_case_only_path_collisions(&resolved.file_inventory));

	result.analysis_meta = AnalysisMeta {
		text_documents: ctx.semantic_index.documents.len(),
		parsed_files: parsed_files_count,
		parse_errors: mod_parse_error_count + base_parse_error_count,
		parse_stats: total_parse_stats,
		scopes: ctx.semantic_index.scopes.len(),
		symbol_definitions: ctx.semantic_index.definitions.len(),
		symbol_references: ctx.semantic_index.references.len(),
		alias_usages: ctx.semantic_index.alias_usages.len(),
	};
	result.parse_issue_report = run_progress_stage(
		"parse issue report",
		|| build_parse_issue_report(&ctx.semantic_index),
		|_| String::new(),
	);

	result.recompute_channels();
	// The resolved input and semantic index hold every parsed document.
	crate::platform::memory::release_in_background((
		ctx,
		resolved.mod_snapshots,
		resolved.installed_base_snapshot,
		resolved.file_inventory,
		resolved.script_cache,
	));
	result
}

fn merge_mod_snapshots(snapshots: &[Option<LoadedModSnapshot>]) -> SemanticIndex {
	let mut merged = SemanticIndex::default();
	for snapshot in snapshots.iter().flatten() {
		merged.append(snapshot.semantic_index.clone());
	}
	merged
}

fn sum_parse_family_stats(lhs: ParseFamilyStats, rhs: ParseFamilyStats) -> ParseFamilyStats {
	ParseFamilyStats {
		clausewitz_mainline: sum_family_parse_stats(
			lhs.clausewitz_mainline,
			rhs.clausewitz_mainline,
		),
		localisation: sum_family_parse_stats(lhs.localisation, rhs.localisation),
		csv: sum_family_parse_stats(lhs.csv, rhs.csv),
		json: sum_family_parse_stats(lhs.json, rhs.json),
	}
}

fn sum_family_parse_stats(lhs: FamilyParseStats, rhs: FamilyParseStats) -> FamilyParseStats {
	FamilyParseStats {
		documents: lhs.documents + rhs.documents,
		parse_failed_documents: lhs.parse_failed_documents + rhs.parse_failed_documents,
		parse_issue_count: lhs.parse_issue_count + rhs.parse_issue_count,
	}
}

fn build_parse_issue_report(index: &SemanticIndex) -> Vec<ParseIssueReportItem> {
	let family_lookup = index
		.documents
		.iter()
		.map(|item| {
			(
				(item.mod_id.as_str(), item.path.as_game_path()),
				item.family,
			)
		})
		.collect::<std::collections::HashMap<_, _>>();
	let mut items: Vec<ParseIssueReportItem> = index
		.parse_issues
		.iter()
		.map(|issue| ParseIssueReportItem {
			family: family_lookup
				.get(&(issue.mod_id.as_str(), issue.path.as_game_path()))
				.copied()
				.unwrap_or(DocumentFamily::Clausewitz),
			mod_id: issue.mod_id.clone(),
			path: issue.path.clone(),
			line: issue.line,
			column: issue.column,
			message: issue.message.clone(),
		})
		.collect();
	items.sort_by(|lhs, rhs| {
		(
			format!("{:?}", lhs.family),
			lhs.mod_id.as_str(),
			&lhs.path,
			lhs.line,
			lhs.column,
			lhs.message.as_str(),
		)
			.cmp(&(
				format!("{:?}", rhs.family),
				rhs.mod_id.as_str(),
				&rhs.path,
				rhs.line,
				rhs.column,
				rhs.message.as_str(),
			))
	});
	items
}

/// Target content families for cross-file key conflict detection.
const NAMESPACE_CHECK_FAMILIES: &[&str] = &["common/scripted_effects", "common/scripted_triggers"];

/// Detect cross-file key conflicts in high-value content families.
///
/// Only checks `scripted_effects` and `scripted_triggers` — the families
/// where two mods silently redefining the same key is a common source of
/// broken gameplay.
/// One finding per group of game paths that differ only in case. They stay
/// separate files, but a case-insensitive filesystem holds only one of them
/// and exact-spelling family and vanilla matches miss the others. The finding
/// is attributed to the first spelling's last (loaded) contributor.
fn check_case_only_path_collisions(
	file_inventory: &BTreeMap<GamePathBuf, Vec<ResolvedInputContributor>>,
) -> Vec<Finding> {
	case_only_path_groups(file_inventory.keys().map(GamePathBuf::as_game_path))
		.into_iter()
		.map(|group| {
			let evidence = group
				.iter()
				.map(|path| {
					let mods = file_inventory[*path]
						.iter()
						.map(|contributor| contributor.mod_id.as_str())
						.collect::<Vec<_>>()
						.join(", ");
					format!("{path} [{mods}]")
				})
				.collect::<Vec<_>>()
				.join("; ");
			let primary = group[0];
			Finding {
				rule_id: "case-only-path-collision".to_string(),
				severity: Severity::Warning,
				channel: FindingChannel::Advisory,
				message: format!(
					"{} game paths differ only in letter case; a case-insensitive filesystem keeps only one",
					group.len()
				),
				mod_id: file_inventory[primary]
					.last()
					.map(|contributor| contributor.mod_id.clone()),
				path: Some(primary.to_owned()),
				source_file: None,
				evidence: Some(evidence),
				line: None,
				column: None,
				confidence: Some(1.0),
			}
		})
		.collect()
}

fn check_namespace_conflicts(
	file_inventory: &BTreeMap<GamePathBuf, Vec<ResolvedInputContributor>>,
	mod_dag: &ModDag,
) -> Vec<Finding> {
	let profile = eu4();
	let families_by_id = group_by_family(file_inventory, profile);

	let mut findings = Vec::new();

	for family_id in NAMESPACE_CHECK_FAMILIES {
		let Some(family_files) = families_by_id.get(*family_id) else {
			continue;
		};
		let Some(descriptor) = profile.descriptor_for_root_family(family_id) else {
			continue;
		};
		let Some(merge_key_source) = descriptor.merge_key_source else {
			continue;
		};

		let index = build_family_key_index(family_id, merge_key_source, family_files, profile);
		let conflicts = detect_key_conflicts(&index);

		for conflict in &conflicts {
			let mut seen_mods = HashSet::new();
			let non_base: Vec<_> = conflict
				.contributors
				.iter()
				.filter(|c| !c.is_base_game)
				.filter(|c| seen_mods.insert(c.mod_id.as_str()))
				.collect();
			if non_base.len() < 2 {
				continue;
			}

			let leaves = leaf_namespace_contributors(&non_base, mod_dag);
			if leaves.len() < 2 {
				continue;
			}

			let primary = leaves[0];
			let evidence = format!(
				"key={}; mods=[{}]",
				conflict.key,
				leaves
					.iter()
					.map(|c| format!("{}:{}", c.mod_id, c.file_path))
					.collect::<Vec<_>>()
					.join(", ")
			);

			findings.push(Finding {
				rule_id: "cross-file-duplicate-symbol".to_string(),
				severity: Severity::Warning,
				channel: FindingChannel::Advisory,
				message: format!("duplicate key is defined by {} sibling mods", leaves.len()),
				mod_id: Some(primary.mod_id.clone()),
				path: Some(primary.file_path.clone()),
				source_file: None,
				evidence: Some(evidence),
				line: None,
				column: None,
				confidence: Some(0.9),
			});
		}
	}

	findings
}

fn leaf_namespace_contributors<'a>(
	contributors: &[&'a KeyContributor],
	mod_dag: &ModDag,
) -> Vec<&'a KeyContributor> {
	let ancestor_sets: HashMap<&str, HashSet<ModId>> = contributors
		.iter()
		.map(|contributor| {
			let mod_id = ModId(contributor.mod_id.clone());
			(
				contributor.mod_id.as_str(),
				transitive_mod_ancestors(mod_dag, &mod_id),
			)
		})
		.collect();

	contributors
		.iter()
		.copied()
		.filter(|candidate| {
			let candidate_id = ModId(candidate.mod_id.clone());
			!contributors.iter().any(|other| {
				other.mod_id != candidate.mod_id
					&& ancestor_sets
						.get(other.mod_id.as_str())
						.is_some_and(|ancestors| ancestors.contains(&candidate_id))
			})
		})
		.collect()
}

fn transitive_mod_ancestors(mod_dag: &ModDag, mod_id: &ModId) -> HashSet<ModId> {
	let mut ancestors = HashSet::new();
	let mut pending: Vec<ModId> = mod_dag.parents_of(mod_id).to_vec();

	while let Some(parent) = pending.pop() {
		if ancestors.insert(parent.clone()) {
			pending.extend(mod_dag.parents_of(&parent).iter().cloned());
		}
	}

	ancestors
}

/// Pull the symbol name out of an overlap finding message such as
/// `跨 Mod 重合定义会改变解析目标: scripted_effect foo_bar` or
/// `跨 Mod 重合定义可自动合并: scripted_trigger baz`.
fn extract_overlap_symbol_name(message: &str) -> Option<String> {
	let after_colon = message.rsplit_once(':')?.1.trim();
	let (_kind, name) = after_colon.split_once(' ')?;
	let name = name.trim();
	if name.is_empty() {
		None
	} else {
		Some(name.to_string())
	}
}

/// Pull the conflict key from a `cross-file-duplicate-symbol` evidence string
/// of the form `key=NAME; mods=[...]`.
fn extract_namespace_key(evidence: &str) -> Option<&str> {
	let rest = evidence.strip_prefix("key=")?;
	let end = rest.find(';').unwrap_or(rest.len());
	Some(rest[..end].trim())
}