skilllite-evolution 0.1.15

SkillLite Evolution: self-evolving prompts, skills, and memory
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
//! SkillLite Evolution: self-evolving prompts, skills, and memory.
//!
//! EVO-1: Feedback collection + evaluation system + structured memory.
//! EVO-2: Prompt externalization + seed data mechanism.
//! EVO-3: Evolution engine core + evolution prompt design.
//! EVO-5: Polish + transparency (audit, degradation, CLI, time trends).
//!
//! Interacts with the agent through the [`EvolutionLlm`] trait for LLM completion.

pub mod external_learner;
pub mod feedback;
pub mod memory_learner;
pub mod prompt_learner;
pub mod seed;
pub mod skill_synth;

use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};

use anyhow::Result;
use rusqlite::{params, Connection};
use skilllite_core::config::env_keys::evolution as evo_keys;

// ─── EvolutionLlm trait: agent integration ────────────────────────────────────

/// Minimal message format for evolution LLM calls (no tool calling).
#[derive(Debug, Clone)]
pub struct EvolutionMessage {
    pub role: String,
    pub content: Option<String>,
}

impl EvolutionMessage {
    pub fn user(content: &str) -> Self {
        Self {
            role: "user".to_string(),
            content: Some(content.to_string()),
        }
    }

    pub fn system(content: &str) -> Self {
        Self {
            role: "system".to_string(),
            content: Some(content.to_string()),
        }
    }
}

/// LLM completion interface for evolution.
///
/// The agent implements this trait to provide LLM access. Evolution uses it
/// for prompt learning, skill synthesis, and external knowledge extraction.
#[async_trait::async_trait]
pub trait EvolutionLlm: Send + Sync {
    /// Non-streaming chat completion. Returns the assistant's text content.
    async fn complete(
        &self,
        messages: &[EvolutionMessage],
        model: &str,
        temperature: f64,
    ) -> Result<String>;
}

// ─── LLM response post-processing ────────────────────────────────────────────

/// Strip reasoning/thinking blocks emitted by various models.
/// Handles `<think>`, `<thinking>`, `<reasoning>` tags (DeepSeek, QwQ, open-source variants).
/// Returns the content after the last closing tag, or the original string if none found.
/// Should be called at the LLM layer so all downstream consumers get clean output.
pub fn strip_think_blocks(content: &str) -> &str {
    const CLOSING_TAGS: &[&str] = &["</think>", "</thinking>", "</reasoning>"];
    const OPENING_TAGS: &[&str] = &[
        "<think>",
        "<think\n",
        "<thinking>",
        "<thinking\n",
        "<reasoning>",
        "<reasoning\n",
    ];

    // Case 1: find the last closing tag, take content after it
    let mut best_end: Option<usize> = None;
    for tag in CLOSING_TAGS {
        if let Some(pos) = content.rfind(tag) {
            let end = pos + tag.len();
            if best_end.is_none_or(|bp| end > bp) {
                best_end = Some(end);
            }
        }
    }
    if let Some(end) = best_end {
        let after = content[end..].trim();
        if !after.is_empty() {
            return after;
        }
    }

    // Case 2: unclosed think tag (model hit token limit mid-thought).
    // Take content before the opening tag if it contains useful text.
    if best_end.is_none() {
        for tag in OPENING_TAGS {
            if let Some(pos) = content.find(tag) {
                let before = content[..pos].trim();
                if !before.is_empty() {
                    return before;
                }
            }
        }
    }

    content
}

// ─── EVO-5: Evolution mode ───────────────────────────────────────────────────

/// Which dimensions of evolution are enabled.
#[derive(Debug, Clone, PartialEq)]
pub enum EvolutionMode {
    All,
    PromptsOnly,
    MemoryOnly,
    SkillsOnly,
    Disabled,
}

impl EvolutionMode {
    pub fn from_env() -> Self {
        match std::env::var("SKILLLITE_EVOLUTION").ok().as_deref() {
            None | Some("1") | Some("true") | Some("") => Self::All,
            Some("0") | Some("false") => Self::Disabled,
            Some("prompts") => Self::PromptsOnly,
            Some("memory") => Self::MemoryOnly,
            Some("skills") => Self::SkillsOnly,
            Some(other) => {
                tracing::warn!(
                    "Unknown SKILLLITE_EVOLUTION value '{}', defaulting to all",
                    other
                );
                Self::All
            }
        }
    }

    pub fn is_disabled(&self) -> bool {
        matches!(self, Self::Disabled)
    }

    pub fn prompts_enabled(&self) -> bool {
        matches!(self, Self::All | Self::PromptsOnly)
    }

    pub fn memory_enabled(&self) -> bool {
        matches!(self, Self::All | Self::MemoryOnly)
    }

    pub fn skills_enabled(&self) -> bool {
        matches!(self, Self::All | Self::SkillsOnly)
    }
}

// ─── SkillAction (used by should_evolve) ──────────────────────────────────────

/// Action type for skill evolution.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SkillAction {
    #[default]
    None,
    Generate,
    Refine,
}

// ─── Concurrency: evolution mutex ────────────────────────────────────────────

static EVOLUTION_IN_PROGRESS: AtomicBool = AtomicBool::new(false);

pub fn try_start_evolution() -> bool {
    EVOLUTION_IN_PROGRESS
        .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
        .is_ok()
}

pub fn finish_evolution() {
    EVOLUTION_IN_PROGRESS.store(false, Ordering::SeqCst);
}

/// Result of attempting to run evolution. Distinguishes "skipped (busy)" from "no scope" from "ran (with or without changes)".
#[derive(Debug, Clone)]
pub enum EvolutionRunResult {
    /// Another evolution run was already in progress; this invocation did not run.
    SkippedBusy,
    /// No evolution scope (e.g. thresholds not met, or evolution disabled).
    NoScope,
    /// Evolution ran. `Some(txn_id)` if changes were produced, `None` if run completed with no changes.
    Completed(Option<String>),
}

impl EvolutionRunResult {
    /// Returns the txn_id if evolution completed with changes.
    pub fn txn_id(&self) -> Option<&str> {
        match self {
            Self::Completed(Some(id)) => Some(id.as_str()),
            _ => None,
        }
    }
}

// ─── Atomic file writes (re-export from skilllite-fs) ─────────────────────────

pub use skilllite_fs::atomic_write;

// ─── 5.2 进化触发条件(从环境变量读取,默认与原硬编码一致)────────────────────────

/// 进化触发阈值,均由环境变量配置,未设置时使用下列默认值。
#[derive(Debug, Clone)]
pub struct EvolutionThresholds {
    pub cooldown_hours: f64,
    pub recent_days: i64,
    pub recent_limit: i64,
    pub meaningful_min_tools: i64,
    pub meaningful_threshold_skills: i64,
    pub meaningful_threshold_memory: i64,
    pub meaningful_threshold_prompts: i64,
    pub failures_min_prompts: i64,
    pub replans_min_prompts: i64,
    pub repeated_pattern_min_count: i64,
    pub repeated_pattern_min_success_rate: f64,
}

impl Default for EvolutionThresholds {
    fn default() -> Self {
        Self {
            cooldown_hours: 1.0,
            recent_days: 7,
            recent_limit: 100,
            meaningful_min_tools: 2,
            meaningful_threshold_skills: 3,
            meaningful_threshold_memory: 3,
            meaningful_threshold_prompts: 5,
            failures_min_prompts: 2,
            replans_min_prompts: 2,
            repeated_pattern_min_count: 3,
            repeated_pattern_min_success_rate: 0.8,
        }
    }
}

/// 进化触发场景:不设或 default 时与原有默认行为完全一致。
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EvolutionProfile {
    /// 与不设 EVO_PROFILE 时一致(当前默认阈值)
    Default,
    /// 演示/内测:冷却短、阈值低,进化更频繁
    Demo,
    /// 生产/省成本:冷却长、阈值高,进化更少
    Conservative,
}

impl EvolutionThresholds {
    /// 预设:演示场景,进化更频繁
    fn demo_preset() -> Self {
        Self {
            cooldown_hours: 0.25,
            recent_days: 3,
            recent_limit: 50,
            meaningful_min_tools: 1,
            meaningful_threshold_skills: 1,
            meaningful_threshold_memory: 1,
            meaningful_threshold_prompts: 2,
            failures_min_prompts: 1,
            replans_min_prompts: 1,
            repeated_pattern_min_count: 2,
            repeated_pattern_min_success_rate: 0.7,
        }
    }

    /// 预设:保守场景,进化更少、省成本
    fn conservative_preset() -> Self {
        Self {
            cooldown_hours: 4.0,
            recent_days: 14,
            recent_limit: 200,
            meaningful_min_tools: 2,
            meaningful_threshold_skills: 5,
            meaningful_threshold_memory: 5,
            meaningful_threshold_prompts: 8,
            failures_min_prompts: 3,
            replans_min_prompts: 3,
            repeated_pattern_min_count: 4,
            repeated_pattern_min_success_rate: 0.85,
        }
    }

    pub fn from_env() -> Self {
        let parse_i64 = |key: &str, default: i64| {
            std::env::var(key)
                .ok()
                .and_then(|v| v.parse().ok())
                .unwrap_or(default)
        };
        let parse_f64 = |key: &str, default: f64| {
            std::env::var(key)
                .ok()
                .and_then(|v| v.parse().ok())
                .unwrap_or(default)
        };
        let profile = match std::env::var(evo_keys::SKILLLITE_EVO_PROFILE)
            .ok()
            .as_deref()
            .map(str::trim)
            .filter(|s| !s.is_empty())
        {
            Some("demo") => EvolutionProfile::Demo,
            Some("conservative") => EvolutionProfile::Conservative,
            _ => EvolutionProfile::Default,
        };
        let base = match profile {
            EvolutionProfile::Default => Self::default(),
            EvolutionProfile::Demo => Self::demo_preset(),
            EvolutionProfile::Conservative => Self::conservative_preset(),
        };
        Self {
            cooldown_hours: parse_f64(evo_keys::SKILLLITE_EVO_COOLDOWN_HOURS, base.cooldown_hours),
            recent_days: parse_i64(evo_keys::SKILLLITE_EVO_RECENT_DAYS, base.recent_days),
            recent_limit: parse_i64(evo_keys::SKILLLITE_EVO_RECENT_LIMIT, base.recent_limit),
            meaningful_min_tools: parse_i64(
                evo_keys::SKILLLITE_EVO_MEANINGFUL_MIN_TOOLS,
                base.meaningful_min_tools,
            ),
            meaningful_threshold_skills: parse_i64(
                evo_keys::SKILLLITE_EVO_MEANINGFUL_THRESHOLD_SKILLS,
                base.meaningful_threshold_skills,
            ),
            meaningful_threshold_memory: parse_i64(
                evo_keys::SKILLLITE_EVO_MEANINGFUL_THRESHOLD_MEMORY,
                base.meaningful_threshold_memory,
            ),
            meaningful_threshold_prompts: parse_i64(
                evo_keys::SKILLLITE_EVO_MEANINGFUL_THRESHOLD_PROMPTS,
                base.meaningful_threshold_prompts,
            ),
            failures_min_prompts: parse_i64(
                evo_keys::SKILLLITE_EVO_FAILURES_MIN_PROMPTS,
                base.failures_min_prompts,
            ),
            replans_min_prompts: parse_i64(
                evo_keys::SKILLLITE_EVO_REPLANS_MIN_PROMPTS,
                base.replans_min_prompts,
            ),
            repeated_pattern_min_count: parse_i64(
                evo_keys::SKILLLITE_EVO_REPEATED_PATTERN_MIN_COUNT,
                base.repeated_pattern_min_count,
            ),
            repeated_pattern_min_success_rate: parse_f64(
                evo_keys::SKILLLITE_EVO_REPEATED_PATTERN_MIN_SUCCESS_RATE,
                base.repeated_pattern_min_success_rate,
            ),
        }
    }
}

// ─── Evolution scope ──────────────────────────────────────────────────────────

#[derive(Debug, Default)]
pub struct EvolutionScope {
    pub skills: bool,
    pub skill_action: SkillAction,
    pub memory: bool,
    pub prompts: bool,
    pub decision_ids: Vec<i64>,
}

impl EvolutionScope {
    /// 返回用于 evolution_run 日志展示的「进化方向」中文描述(供 evotown 等前端展示)
    pub fn direction_label(&self) -> String {
        let mut parts: Vec<&str> = Vec::new();
        if self.prompts {
            parts.push("规则与示例");
        }
        if self.skills {
            parts.push("技能");
        }
        if self.memory {
            parts.push("记忆");
        }
        if parts.is_empty() {
            return String::new();
        }
        parts.join("")
    }
}

pub fn should_evolve(conn: &Connection) -> Result<EvolutionScope> {
    should_evolve_impl(conn, EvolutionMode::from_env(), false)
}

pub fn should_evolve_with_mode(conn: &Connection, mode: EvolutionMode) -> Result<EvolutionScope> {
    should_evolve_impl(conn, mode, false)
}

/// When force=true (e.g. manual `skilllite evolution run`), bypass decision thresholds.
fn should_evolve_impl(
    conn: &Connection,
    mode: EvolutionMode,
    force: bool,
) -> Result<EvolutionScope> {
    if mode.is_disabled() {
        return Ok(EvolutionScope::default());
    }

    let thresholds = EvolutionThresholds::from_env();

    let today_evolutions: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM evolution_log WHERE date(ts) = date('now')",
            [],
            |row| row.get(0),
        )
        .unwrap_or(0);
    let max_per_day: i64 = std::env::var(evo_keys::SKILLLITE_MAX_EVOLUTIONS_PER_DAY)
        .ok()
        .and_then(|v| v.parse().ok())
        .unwrap_or(20);
    if today_evolutions >= max_per_day {
        return Ok(EvolutionScope::default());
    }

    if !force {
        let last_evo_hours: f64 = conn
            .query_row(
                "SELECT COALESCE(
                    (julianday('now') - julianday(MAX(ts))) * 24,
                    999.0
                ) FROM evolution_log",
                [],
                |row| row.get(0),
            )
            .unwrap_or(999.0);
        if last_evo_hours < thresholds.cooldown_hours {
            return Ok(EvolutionScope::default());
        }
    }

    let recent_condition = format!("ts >= datetime('now', '-{} days')", thresholds.recent_days);
    let recent_limit = thresholds.recent_limit;

    let (meaningful, failures, replans): (i64, i64, i64) = conn.query_row(
        &format!(
            "SELECT
                COUNT(CASE WHEN total_tools >= {} THEN 1 END),
                COUNT(CASE WHEN failed_tools > 0 THEN 1 END),
                COUNT(CASE WHEN replans > 0 THEN 1 END)
             FROM decisions WHERE {}",
            thresholds.meaningful_min_tools, recent_condition
        ),
        [],
        |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)),
    )?;

    let mut stmt = conn.prepare(&format!(
        "SELECT id FROM decisions WHERE {} ORDER BY ts DESC LIMIT {}",
        recent_condition, recent_limit
    ))?;
    let ids: Vec<i64> = stmt
        .query_map([], |row| row.get(0))?
        .filter_map(|r| r.ok())
        .collect();

    // Group by tool_sequence_key (new) when available; fall back to task_description for
    // older decisions that predate the tool_sequence_key column.
    // COALESCE(NULLIF(key,''), desc) ensures empty-string keys also fall back.
    let repeated_patterns: i64 = conn
        .query_row(
            &format!(
                "SELECT COUNT(*) FROM (
                SELECT COALESCE(NULLIF(tool_sequence_key, ''), task_description) AS pattern_key,
                       COUNT(*) AS cnt,
                       SUM(CASE WHEN task_completed = 1 THEN 1 ELSE 0 END) AS successes
                FROM decisions
                WHERE {} AND (tool_sequence_key IS NOT NULL OR task_description IS NOT NULL)
                  AND total_tools >= 1
                GROUP BY pattern_key
                HAVING cnt >= {} AND CAST(successes AS REAL) / cnt >= {}
            )",
                recent_condition,
                thresholds.repeated_pattern_min_count,
                thresholds.repeated_pattern_min_success_rate
            ),
            [],
            |row| row.get(0),
        )
        .unwrap_or(0);

    let mut scope = EvolutionScope {
        decision_ids: ids.clone(),
        ..Default::default()
    };

    if force && !ids.is_empty() {
        // Manual trigger: bypass thresholds, enable all enabled modes
        if mode.skills_enabled() {
            scope.skills = true;
            scope.skill_action = if repeated_patterns > 0 {
                SkillAction::Generate
            } else {
                SkillAction::Refine
            };
        }
        if mode.memory_enabled() {
            scope.memory = true;
        }
        if mode.prompts_enabled() {
            scope.prompts = true;
        }
    } else {
        if mode.skills_enabled()
            && meaningful >= thresholds.meaningful_threshold_skills
            && (failures > 0 || repeated_patterns > 0)
        {
            scope.skills = true;
            scope.skill_action = if repeated_patterns > 0 {
                SkillAction::Generate
            } else {
                SkillAction::Refine
            };
        }
        if mode.memory_enabled() && meaningful >= thresholds.meaningful_threshold_memory {
            scope.memory = true;
        }
        if mode.prompts_enabled()
            && meaningful >= thresholds.meaningful_threshold_prompts
            && (failures >= thresholds.failures_min_prompts
                || replans >= thresholds.replans_min_prompts)
        {
            scope.prompts = true;
        }
    }

    Ok(scope)
}

// ─── Gatekeeper (L1-L3) ───────────────────────────────────────────────────────

const ALLOWED_EVOLUTION_PATHS: &[&str] = &["prompts", "memory", "skills/_evolved"];

/// L1 path gatekeeper. When skills_root is Some, also allows target under skills_root/_evolved
/// (project-level skill evolution).
pub fn gatekeeper_l1_path(chat_root: &Path, target: &Path, skills_root: Option<&Path>) -> bool {
    for allowed in ALLOWED_EVOLUTION_PATHS {
        let allowed_dir = chat_root.join(allowed);
        if target.starts_with(&allowed_dir) {
            return true;
        }
    }
    if let Some(sr) = skills_root {
        let evolved = sr.join("_evolved");
        if target.starts_with(&evolved) {
            return true;
        }
    }
    false
}

pub fn gatekeeper_l1_template_integrity(filename: &str, new_content: &str) -> Result<()> {
    let missing = seed::validate_template(filename, new_content);
    if !missing.is_empty() {
        anyhow::bail!(
            "Gatekeeper L1b: evolved template '{}' is missing required placeholders {:?}",
            filename,
            missing
        );
    }
    Ok(())
}

pub fn gatekeeper_l2_size(new_rules: usize, new_examples: usize, new_skills: usize) -> bool {
    new_rules <= 5 && new_examples <= 3 && new_skills <= 1
}

const SENSITIVE_PATTERNS: &[&str] = &[
    "api_key",
    "api-key",
    "apikey",
    "secret",
    "password",
    "passwd",
    "token",
    "bearer",
    "private_key",
    "private-key",
    "-----BEGIN",
    "-----END",
    "skip scan",
    "bypass",
    "disable security",
    "eval(",
    "exec(",
    "__import__",
];

pub fn gatekeeper_l3_content(content: &str) -> Result<()> {
    let lower = content.to_lowercase();
    for pattern in SENSITIVE_PATTERNS {
        if lower.contains(pattern) {
            anyhow::bail!(
                "Gatekeeper L3: evolution product contains sensitive pattern: '{}'",
                pattern
            );
        }
    }
    Ok(())
}

// ─── Snapshots ────────────────────────────────────────────────────────────────

fn versions_dir(chat_root: &Path) -> std::path::PathBuf {
    chat_root.join("prompts").join("_versions")
}

pub fn create_snapshot(chat_root: &Path, txn_id: &str, files: &[&str]) -> Result<Vec<String>> {
    let snap_dir = versions_dir(chat_root).join(txn_id);
    std::fs::create_dir_all(&snap_dir)?;
    let prompts = chat_root.join("prompts");
    let mut backed_up = Vec::new();
    for name in files {
        let src = prompts.join(name);
        if src.exists() {
            let dst = snap_dir.join(name);
            std::fs::copy(&src, &dst)?;
            backed_up.push(name.to_string());
        }
    }
    prune_snapshots(chat_root, 10);
    Ok(backed_up)
}

pub fn restore_snapshot(chat_root: &Path, txn_id: &str) -> Result<()> {
    let snap_dir = versions_dir(chat_root).join(txn_id);
    if !snap_dir.exists() {
        anyhow::bail!("Snapshot not found: {}", txn_id);
    }
    let prompts = chat_root.join("prompts");
    for entry in std::fs::read_dir(&snap_dir)? {
        let entry = entry?;
        let dst = prompts.join(entry.file_name());
        std::fs::copy(entry.path(), &dst)?;
    }
    tracing::info!("Restored snapshot {}", txn_id);
    Ok(())
}

fn prune_snapshots(chat_root: &Path, keep: usize) {
    let vdir = versions_dir(chat_root);
    if !vdir.exists() {
        return;
    }
    let mut dirs: Vec<_> = std::fs::read_dir(&vdir)
        .ok()
        .into_iter()
        .flatten()
        .filter_map(|e| e.ok())
        .filter(|e| e.file_type().map(|t| t.is_dir()).unwrap_or(false))
        .collect();
    if dirs.len() <= keep {
        return;
    }
    dirs.sort_by_key(|e| e.file_name());
    let to_remove = dirs.len() - keep;
    for entry in dirs.into_iter().take(to_remove) {
        let _ = std::fs::remove_dir_all(entry.path());
    }
}

// ─── Changelog ───────────────────────────────────────────────────────────────

#[derive(serde::Serialize)]
struct ChangelogEntry {
    txn_id: String,
    ts: String,
    files: Vec<String>,
    changes: Vec<ChangeDetail>,
    reason: String,
}

#[derive(serde::Serialize)]
struct ChangeDetail {
    #[serde(rename = "type")]
    change_type: String,
    id: String,
}

pub fn append_changelog(
    chat_root: &Path,
    txn_id: &str,
    files: &[String],
    changes: &[(String, String)],
    reason: &str,
) -> Result<()> {
    let vdir = versions_dir(chat_root);
    std::fs::create_dir_all(&vdir)?;
    let path = vdir.join("changelog.jsonl");

    let entry = ChangelogEntry {
        txn_id: txn_id.to_string(),
        ts: chrono::Utc::now().to_rfc3339(),
        files: files.to_vec(),
        changes: changes
            .iter()
            .map(|(t, id)| ChangeDetail {
                change_type: t.clone(),
                id: id.clone(),
            })
            .collect(),
        reason: reason.to_string(),
    };

    let mut line = serde_json::to_string(&entry)?;
    line.push('\n');

    use std::io::Write;
    let mut file = std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(&path)?;
    file.write_all(line.as_bytes())?;
    Ok(())
}

// ─── Audit log ───────────────────────────────────────────────────────────────

pub fn log_evolution_event(
    conn: &Connection,
    chat_root: &Path,
    event_type: &str,
    target_id: &str,
    reason: &str,
    txn_id: &str,
) -> Result<()> {
    let ts = chrono::Utc::now().to_rfc3339();

    conn.execute(
        "INSERT INTO evolution_log (ts, type, target_id, reason, version) VALUES (?1, ?2, ?3, ?4, ?5)",
        params![ts, event_type, target_id, reason, txn_id],
    )?;

    let log_path = chat_root.join("evolution.log");
    let entry = serde_json::json!({
        "ts": ts,
        "type": event_type,
        "id": target_id,
        "reason": reason,
        "txn_id": txn_id,
    });
    let mut line = serde_json::to_string(&entry)?;
    line.push('\n');
    use std::io::Write;
    let mut file = std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(&log_path)?;
    file.write_all(line.as_bytes())?;

    skilllite_core::observability::audit_evolution_event(event_type, target_id, reason, txn_id);

    Ok(())
}

// ─── Mark decisions evolved ───────────────────────────────────────────────────

pub fn mark_decisions_evolved(conn: &Connection, ids: &[i64]) -> Result<()> {
    if ids.is_empty() {
        return Ok(());
    }
    let placeholders: Vec<String> = ids.iter().map(|_| "?".to_string()).collect();
    let sql = format!(
        "UPDATE decisions SET evolved = 1 WHERE id IN ({})",
        placeholders.join(",")
    );
    let mut stmt = conn.prepare(&sql)?;
    let params: Vec<Box<dyn rusqlite::types::ToSql>> = ids
        .iter()
        .map(|id| Box::new(*id) as Box<dyn rusqlite::types::ToSql>)
        .collect();
    let param_refs: Vec<&dyn rusqlite::types::ToSql> = params.iter().map(|p| p.as_ref()).collect();
    stmt.execute(param_refs.as_slice())?;
    Ok(())
}

// ─── Run evolution (main entry point) ──────────────────────────────────────────

/// Run a full evolution cycle.
///
/// Returns [EvolutionRunResult]: SkippedBusy if another run in progress, NoScope if nothing to evolve, Completed(txn_id) otherwise.
/// When force=true (manual trigger), bypass decision thresholds.
/// skills_root: project-level dir (workspace/.skills). When None, skips skill evolution.
pub async fn run_evolution<L: EvolutionLlm>(
    chat_root: &Path,
    skills_root: Option<&Path>,
    llm: &L,
    api_base: &str,
    api_key: &str,
    model: &str,
    force: bool,
) -> Result<EvolutionRunResult> {
    if !try_start_evolution() {
        return Ok(EvolutionRunResult::SkippedBusy);
    }

    let result =
        run_evolution_inner(chat_root, skills_root, llm, api_base, api_key, model, force).await;

    finish_evolution();
    result
}

async fn run_evolution_inner<L: EvolutionLlm>(
    chat_root: &Path,
    skills_root: Option<&Path>,
    llm: &L,
    _api_base: &str,
    _api_key: &str,
    model: &str,
    force: bool,
) -> Result<EvolutionRunResult> {
    let conn = feedback::open_evolution_db(chat_root)?;
    let scope = should_evolve_impl(&conn, EvolutionMode::from_env(), force)?;
    if !scope.prompts && !scope.memory && !scope.skills {
        return Ok(EvolutionRunResult::NoScope);
    }
    let txn_id = format!("evo_{}", chrono::Utc::now().format("%Y%m%d_%H%M%S"));
    tracing::info!(
        "Starting evolution txn={} (prompts={}, memory={}, skills={})",
        txn_id,
        scope.prompts,
        scope.memory,
        scope.skills
    );
    let snapshot_files = if scope.prompts {
        create_snapshot(
            chat_root,
            &txn_id,
            &[
                "rules.json",
                "examples.json",
                "planning.md",
                "execution.md",
                "system.md",
            ],
        )?
    } else {
        Vec::new()
    };

    // Drop conn before async work (Connection is !Send, cannot hold across .await).
    drop(conn);

    let mut all_changes: Vec<(String, String)> = Vec::new();
    let mut reason_parts: Vec<String> = Vec::new();

    // Run prompts / skills / memory evolution in parallel. Each module uses block_in_place
    // to batch its DB operations (one open per module), so we get both parallelism and fewer opens.
    let (prompt_res, skills_res, memory_res) = tokio::join!(
        async {
            if scope.prompts {
                prompt_learner::evolve_prompts(chat_root, llm, model, &txn_id).await
            } else {
                Ok(Vec::new())
            }
        },
        async {
            if scope.skills {
                let generate = true;
                skill_synth::evolve_skills(
                    chat_root,
                    skills_root,
                    llm,
                    model,
                    &txn_id,
                    generate,
                    force,
                )
                .await
            } else {
                Ok(Vec::new())
            }
        },
        async {
            if scope.memory {
                memory_learner::evolve_memory(chat_root, llm, model, &txn_id).await
            } else {
                Ok(Vec::new())
            }
        },
    );

    if scope.prompts {
        match prompt_res {
            Ok(changes) => {
                if !changes.is_empty() {
                    reason_parts.push(format!("{} prompt changes", changes.len()));
                }
                all_changes.extend(changes);
            }
            Err(e) => tracing::warn!("Prompt evolution failed: {}", e),
        }
    }
    if scope.skills {
        match skills_res {
            Ok(changes) => {
                if !changes.is_empty() {
                    reason_parts.push(format!("{} skill changes", changes.len()));
                }
                all_changes.extend(changes);
            }
            Err(e) => tracing::warn!("Skill evolution failed: {}", e),
        }
    }
    if scope.memory {
        match memory_res {
            Ok(changes) => {
                if !changes.is_empty() {
                    reason_parts.push(format!("{} memory knowledge update(s)", changes.len()));
                }
                all_changes.extend(changes);
            }
            Err(e) => tracing::warn!("Memory evolution failed: {}", e),
        }
    }

    // Run external learning before changelog so its changes and modified files are in the same txn entry.
    match external_learner::run_external_learning(chat_root, llm, model, &txn_id).await {
        Ok(ext_changes) => {
            if !ext_changes.is_empty() {
                tracing::info!("EVO-6: {} external changes applied", ext_changes.len());
                reason_parts.push(format!("{} external change(s)", ext_changes.len()));
                all_changes.extend(ext_changes);
            }
        }
        Err(e) => tracing::warn!("EVO-6 external learning failed (non-fatal): {}", e),
    }

    {
        let conn = feedback::open_evolution_db(chat_root)?;

        for (ctype, cid) in &all_changes {
            log_evolution_event(&conn, chat_root, ctype, cid, "prompt evolution", &txn_id)?;
        }

        if scope.prompts {
            if let Err(e) = prompt_learner::update_reusable_status(&conn, chat_root) {
                tracing::warn!("Failed to update reusable status: {}", e);
            }
        }

        mark_decisions_evolved(&conn, &scope.decision_ids)?;
        let _ = feedback::update_daily_metrics(&conn);
        let auto_rolled_back = check_auto_rollback(&conn, chat_root)?;
        if auto_rolled_back {
            tracing::info!("EVO: auto-rollback triggered for txn={}", txn_id);
            let _ = log_evolution_event(
                &conn,
                chat_root,
                "evolution_judgement",
                "rollback",
                "Auto-rollback triggered due to performance degradation",
                &txn_id,
            );
        } else {
            let _ = log_evolution_event(
                &conn,
                chat_root,
                "evolution_judgement",
                "no_rollback",
                "No auto-rollback triggered",
                &txn_id,
            );
        }
        // let _ = feedback::export_judgement(&conn, &chat_root.join("JUDGEMENT.md")); // Removed for refactor
        if let Ok(Some(summary)) = feedback::build_latest_judgement(&conn) {
            let _ = log_evolution_event(
                &conn,
                chat_root,
                "evolution_judgement",
                summary.judgement.as_str(),
                &summary.reason,
                &txn_id,
            );
            // Insert new judgement output to file here
            let judgement_output = format!(
                "## Evolution Judgement\n\n**Judgement:** {}\n\n**Reason:** {}\n",
                summary.judgement.as_str(),
                summary.reason
            );
            let judgement_path = chat_root.join("JUDGEMENT.md");
            if let Err(e) = skilllite_fs::atomic_write(&judgement_path, &judgement_output) {
                tracing::warn!("Failed to write JUDGEMENT.md: {}", e);
            }
        }

        if all_changes.is_empty() {
            // 即使无变更也记录一次,便于前端时间线展示进化运行记录(含本轮选择的进化方向)
            let dir = scope.direction_label();
            let reason = if dir.is_empty() {
                "进化运行完成,无新规则/技能产出".to_string()
            } else {
                format!("方向: {};进化运行完成,无新规则/技能产出", dir)
            };
            let _ = log_evolution_event(&conn, chat_root, "evolution_run", "run", &reason, &txn_id);
            return Ok(EvolutionRunResult::Completed(None));
        }

        let dir = scope.direction_label();
        let reason = if dir.is_empty() {
            reason_parts.join("; ")
        } else {
            format!("方向: {}{}", dir, reason_parts.join("; "))
        };
        // 记录本轮进化运行(含方向),便于前端时间线统一展示
        let _ = log_evolution_event(&conn, chat_root, "evolution_run", "run", &reason, &txn_id);

        // 只记录内容真正发生变化的文件:用快照与当前版本逐一对比。
        // snapshot_files 是进化前备份的全量清单,但实际修改的往往只是其中一部分
        // (如 rules.json / examples.json),planning.md 等通常未被触碰。
        let snap_dir = versions_dir(chat_root).join(&txn_id);
        let prompts_dir = chat_root.join("prompts");
        let mut modified_files: Vec<String> = snapshot_files
            .iter()
            .filter(|fname| {
                let snap_path = snap_dir.join(fname);
                let curr_path = prompts_dir.join(fname);
                match (std::fs::read(&snap_path), std::fs::read(&curr_path)) {
                    (Ok(old), Ok(new)) => old != new,
                    _ => false,
                }
            })
            .cloned()
            .collect();

        // External learner writes to prompts/rules.json; include it when external merged/promoted rules but snapshot didn't cover it (e.g. no scope.prompts).
        if all_changes
            .iter()
            .any(|(t, _)| t == "external_rule_added" || t == "external_rule_promoted")
        {
            const EXTERNAL_RULES_FILE: &str = "rules.json";
            if !modified_files.iter().any(|f| f == EXTERNAL_RULES_FILE) {
                let rules_path = prompts_dir.join(EXTERNAL_RULES_FILE);
                if rules_path.exists() {
                    modified_files.push(EXTERNAL_RULES_FILE.to_string());
                }
            }
        }

        append_changelog(chat_root, &txn_id, &modified_files, &all_changes, &reason)?;

        let _decisions_path = chat_root.join("DECISIONS.md");
        // let _ = feedback::export_decisions_md(&conn, &decisions_path); // Removed for refactor

        tracing::info!("Evolution txn={} complete: {}", txn_id, reason);
    }

    Ok(EvolutionRunResult::Completed(Some(txn_id)))
}

pub fn query_changes_by_txn(conn: &Connection, txn_id: &str) -> Vec<(String, String)> {
    let mut stmt =
        match conn.prepare("SELECT type, target_id FROM evolution_log WHERE version = ?1") {
            Ok(s) => s,
            Err(_) => return Vec::new(),
        };
    stmt.query_map(params![txn_id], |row| {
        Ok((
            row.get::<_, String>(0)?,
            row.get::<_, Option<String>>(1)?.unwrap_or_default(),
        ))
    })
    .ok()
    .into_iter()
    .flatten()
    .filter_map(|r| r.ok())
    .collect()
}

pub fn format_evolution_changes(changes: &[(String, String)]) -> Vec<String> {
    changes
        .iter()
        .filter_map(|(change_type, id)| {
            let msg = match change_type.as_str() {
                "rule_added" => format!("\u{1f4a1} 已学习新规则: {}", id),
                "rule_updated" => format!("\u{1f504} 已优化规则: {}", id),
                "rule_retired" => format!("\u{1f5d1}\u{fe0f} 已退役低效规则: {}", id),
                "example_added" => format!("\u{1f4d6} 已新增示例: {}", id),
                "skill_generated" => format!("\u{2728} 已自动生成 Skill: {}", id),
                "skill_pending" => format!(
                    "\u{1f4a1} 新 Skill {} 待确认(运行 `skilllite evolution confirm {}` 加入)",
                    id, id
                ),
                "skill_refined" => format!("\u{1f527} 已优化 Skill: {}", id),
                "skill_retired" => format!("\u{1f4e6} 已归档 Skill: {}", id),
                "evolution_judgement" => {
                    let label = match id.as_str() {
                        "promote" => "保留",
                        "keep_observing" => "继续观察",
                        "rollback" => "回滚",
                        _ => id,
                    };
                    format!("\u{1f9ed} 本轮判断: {}", label)
                }
                "auto_rollback" => format!("\u{26a0}\u{fe0f} 检测到质量下降,已自动回滚: {}", id),
                "reusable_promoted" => format!("\u{2b06}\u{fe0f} 规则晋升为通用: {}", id),
                "reusable_demoted" => format!("\u{2b07}\u{fe0f} 规则降级为低效: {}", id),
                "external_rule_added" => format!("\u{1f310} 已从外部来源学习规则: {}", id),
                "external_rule_promoted" => format!("\u{2b06}\u{fe0f} 外部规则晋升为优质: {}", id),
                "source_paused" => format!("\u{23f8}\u{fe0f} 信源可达性过低,已暂停: {}", id),
                "source_retired" => format!("\u{1f5d1}\u{fe0f} 已退役低质量信源: {}", id),
                "source_discovered" => format!("\u{1f50d} 发现新信源: {}", id),
                "memory_knowledge_added" => format!("\u{1f4da} 已沉淀知识库(实体与关系): {}", id),
                _ => return None,
            };
            Some(msg)
        })
        .collect()
}

// ─── Shutdown hook ────────────────────────────────────────────────────────────

pub fn on_shutdown(chat_root: &Path) {
    if !try_start_evolution() {
        return;
    }
    if let Ok(conn) = feedback::open_evolution_db(chat_root) {
        let _ = feedback::update_daily_metrics(&conn);
        // let _ = feedback::export_decisions_md(&conn, &chat_root.join("DECISIONS.md")); // Removed for refactor
    }
    finish_evolution();
}

// ─── Auto-rollback ───────────────────────────────────────────────────────────

/// Executes the rollback actions (restoring snapshot, logging).
fn execute_evolution_rollback(
    conn: &Connection,
    chat_root: &Path,
    txn_id: &str,
    reason: &str,
) -> Result<()> {
    tracing::warn!("Evolution rollback executed: {} (txn={})", reason, txn_id);
    restore_snapshot(chat_root, txn_id)?;

    conn.execute(
        "UPDATE evolution_log SET type = type || '_rolled_back' WHERE version = ?1",
        params![txn_id],
    )?;

    log_evolution_event(
        conn,
        chat_root,
        "auto_rollback",
        txn_id,
        reason,
        &format!("rollback_{}", txn_id),
    )?;
    Ok(())
}
pub fn check_auto_rollback(conn: &Connection, chat_root: &Path) -> Result<bool> {
    let mut stmt = conn.prepare(
        "SELECT date, first_success_rate, user_correction_rate
         FROM evolution_metrics
         WHERE date > date('now', '-5 days')
         ORDER BY date DESC LIMIT 4",
    )?;
    let metrics: Vec<(String, f64, f64)> = stmt
        .query_map([], |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)))?
        .filter_map(|r| r.ok())
        .collect();

    if metrics.len() < 3 {
        return Ok(false);
    }

    let fsr_declining = metrics.windows(2).take(3).all(|w| w[0].1 < w[1].1 - 0.10);
    let ucr_rising = metrics.windows(2).take(3).all(|w| w[0].2 > w[1].2 + 0.20);

    if fsr_declining || ucr_rising {
        let reason = if fsr_declining {
            "first_success_rate declined >10% for 3 consecutive days"
        } else {
            "user_correction_rate rose >20% for 3 consecutive days"
        };

        let last_txn: Option<String> = conn
            .query_row(
                "SELECT DISTINCT version FROM evolution_log
                 WHERE type NOT LIKE '%_rolled_back'
                 ORDER BY ts DESC LIMIT 1",
                [],
                |row| row.get(0),
            )
            .ok();

        if let Some(txn_id) = last_txn {
            execute_evolution_rollback(conn, chat_root, &txn_id, reason)?;
            return Ok(true);
        }
    }

    Ok(false)
}

#[cfg(test)]
mod lib_tests {
    use super::*;
    use std::path::Path;
    use std::sync::Mutex;

    static EVO_LOCK: Mutex<()> = Mutex::new(());

    #[test]
    fn strip_think_blocks_after_closing_tag() {
        let s = "<think>\nhidden\n</think>\nvisible reply";
        assert_eq!(strip_think_blocks(s), "visible reply");
    }

    #[test]
    fn strip_think_blocks_plain_text_unchanged() {
        let s = "no think tags here";
        assert_eq!(strip_think_blocks(s), s);
    }

    #[test]
    fn strip_think_blocks_reasoning_tag() {
        let s = "<reasoning>x</reasoning>\nhello";
        assert_eq!(strip_think_blocks(s), "hello");
    }

    #[test]
    fn evolution_message_constructors() {
        let u = EvolutionMessage::user("u");
        assert_eq!(u.role, "user");
        assert_eq!(u.content.as_deref(), Some("u"));
        let sy = EvolutionMessage::system("s");
        assert_eq!(sy.role, "system");
    }

    #[test]
    fn evolution_mode_capability_flags() {
        assert!(EvolutionMode::All.prompts_enabled());
        assert!(EvolutionMode::All.memory_enabled());
        assert!(EvolutionMode::All.skills_enabled());
        assert!(EvolutionMode::PromptsOnly.prompts_enabled());
        assert!(!EvolutionMode::PromptsOnly.memory_enabled());
        assert!(!EvolutionMode::MemoryOnly.prompts_enabled());
        assert!(EvolutionMode::MemoryOnly.memory_enabled());
        assert!(EvolutionMode::Disabled.is_disabled());
    }

    #[test]
    fn evolution_run_result_txn_id() {
        assert_eq!(
            EvolutionRunResult::Completed(Some("t1".into())).txn_id(),
            Some("t1")
        );
        assert_eq!(EvolutionRunResult::SkippedBusy.txn_id(), None);
    }

    #[test]
    fn gatekeeper_l2_size_bounds() {
        assert!(gatekeeper_l2_size(5, 3, 1));
        assert!(!gatekeeper_l2_size(6, 0, 0));
        assert!(!gatekeeper_l2_size(0, 4, 0));
        assert!(!gatekeeper_l2_size(0, 0, 2));
    }

    #[test]
    fn gatekeeper_l3_rejects_secret_pattern() {
        assert!(gatekeeper_l3_content("safe text").is_ok());
        assert!(gatekeeper_l3_content("has api_key in body").is_err());
    }

    #[test]
    fn gatekeeper_l1_path_allows_prompts_under_chat_root() {
        let root = Path::new("/home/u/.skilllite/chat");
        let target = root.join("prompts/rules.json");
        assert!(gatekeeper_l1_path(root, &target, None));
        let bad = Path::new("/etc/passwd");
        assert!(!gatekeeper_l1_path(root, bad, None));
    }

    #[test]
    fn try_start_evolution_is_exclusive() {
        let _g = EVO_LOCK.lock().expect("evo lock");
        finish_evolution();
        assert!(try_start_evolution());
        assert!(!try_start_evolution());
        finish_evolution();
    }

    #[test]
    fn evolution_thresholds_default_nonzero_cooldown() {
        let t = EvolutionThresholds::default();
        assert!(t.cooldown_hours > 0.0);
        assert!(t.recent_days > 0);
    }
}