clawft-core 0.6.11

Core engine for clawft (agent loop, message bus, context management)
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
//! Autonomous skill creation (C4a).
//!
//! Detects repeated task patterns and auto-generates skill definitions.
//! Generated skills are installed in `~/.clawft/skills/` in a "pending"
//! state and require user approval before activation.
//!
//! **Disabled by default** -- must be opted into via configuration.
//!
//! # Security
//!
//! Auto-generated skills have minimal permissions:
//! - No shell access
//! - No network access
//! - Filesystem limited to workspace directory
//!
//! # Pattern Detection
//!
//! The detector tracks sequences of tool calls. When the same sequence
//! appears at least `threshold` times (default: 3), a skill candidate
//! is generated.

use std::collections::HashMap;
use std::path::{Path, PathBuf};

use tracing::{debug, info};

use crate::pipeline::learner::TrajectoryLearner;
use crate::pipeline::mutation::{auto_select_strategy, mutate_prompt, TrajectoryHint};

// ---------------------------------------------------------------------------
// Configuration
// ---------------------------------------------------------------------------

/// Configuration for autonomous skill creation.
#[derive(Debug, Clone)]
pub struct AutogenConfig {
    /// Whether autonomous skill creation is enabled.
    /// Default: `false` (disabled).
    pub enabled: bool,
    /// Minimum number of pattern repetitions before suggesting a skill.
    /// Default: 3.
    pub threshold: usize,
    /// Maximum number of pending skills allowed at once.
    /// Prevents unbounded growth. Default: 10.
    pub max_pending: usize,
    /// Directory where generated skills are installed.
    /// Default: `~/.clawft/skills/`
    pub install_dir: Option<PathBuf>,
}

impl Default for AutogenConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            threshold: 3,
            max_pending: 10,
            install_dir: None,
        }
    }
}

impl AutogenConfig {
    /// Get the effective install directory, defaulting to `~/.clawft/skills/`.
    pub fn install_dir(&self) -> PathBuf {
        self.install_dir.clone().unwrap_or_else(|| {
            #[cfg(feature = "native")]
            {
                dirs::home_dir()
                    .unwrap_or_else(|| PathBuf::from("."))
                    .join(".clawft")
                    .join("skills")
            }
            #[cfg(not(feature = "native"))]
            {
                PathBuf::from(".clawft").join("skills")
            }
        })
    }
}

// ---------------------------------------------------------------------------
// Pattern Detection
// ---------------------------------------------------------------------------

/// A sequence of tool calls forming a pattern.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ToolCallPattern {
    /// Ordered list of tool names in the pattern.
    pub tool_names: Vec<String>,
}

impl ToolCallPattern {
    /// Create a new pattern from tool names.
    pub fn new(tool_names: Vec<String>) -> Self {
        Self { tool_names }
    }

    /// Generate a suggested skill name from the pattern.
    pub fn suggested_name(&self) -> String {
        if self.tool_names.is_empty() {
            return "unnamed-skill".to_string();
        }
        let parts: Vec<&str> = self.tool_names.iter().map(|s| s.as_str()).collect();
        let name = parts.join("-then-");
        // Truncate to reasonable length
        if name.len() > 60 {
            format!("{}-auto", &name[..56])
        } else {
            format!("{name}-auto")
        }
    }

    /// Number of steps in the pattern.
    pub fn len(&self) -> usize {
        self.tool_names.len()
    }

    /// Whether the pattern is empty.
    pub fn is_empty(&self) -> bool {
        self.tool_names.is_empty()
    }
}

/// Detects repeated tool call patterns in the agent's execution history.
pub struct PatternDetector {
    /// Configuration for pattern detection.
    config: AutogenConfig,
    /// Counts of each observed pattern.
    pattern_counts: HashMap<ToolCallPattern, usize>,
    /// Recent tool calls (sliding window).
    recent_calls: Vec<String>,
    /// Maximum window size for pattern detection.
    max_window: usize,
    /// Patterns that have already been reported (to avoid duplicates).
    reported: HashMap<ToolCallPattern, bool>,
}

impl PatternDetector {
    /// Create a new pattern detector.
    pub fn new(config: AutogenConfig) -> Self {
        Self {
            config,
            pattern_counts: HashMap::new(),
            recent_calls: Vec::new(),
            max_window: 10,
            reported: HashMap::new(),
        }
    }

    /// Record a tool call.
    pub fn record_tool_call(&mut self, tool_name: &str) {
        if !self.config.enabled {
            return;
        }

        self.recent_calls.push(tool_name.to_string());

        // Keep window bounded
        if self.recent_calls.len() > self.max_window * 3 {
            self.recent_calls.drain(..self.max_window);
        }

        // Extract patterns of length 2..=max_window from recent calls
        let n = self.recent_calls.len();
        for len in 2..=self.max_window.min(n) {
            let pattern = ToolCallPattern::new(
                self.recent_calls[n - len..n].to_vec(),
            );
            *self.pattern_counts.entry(pattern).or_insert(0) += 1;
        }
    }

    /// Check for patterns that have reached the threshold.
    ///
    /// Returns newly detected patterns (patterns that just crossed the
    /// threshold and have not been reported before).
    pub fn detect_candidates(&mut self) -> Vec<ToolCallPattern> {
        if !self.config.enabled {
            return vec![];
        }

        let threshold = self.config.threshold;
        let mut candidates = Vec::new();

        for (pattern, count) in &self.pattern_counts {
            if *count >= threshold && !self.reported.contains_key(pattern) {
                candidates.push(pattern.clone());
            }
        }

        // Mark as reported
        for candidate in &candidates {
            self.reported.insert(candidate.clone(), true);
        }

        candidates
    }

    /// Get the count for a specific pattern.
    pub fn pattern_count(&self, pattern: &ToolCallPattern) -> usize {
        self.pattern_counts.get(pattern).copied().unwrap_or(0)
    }

    /// Reset all pattern tracking state.
    pub fn reset(&mut self) {
        self.pattern_counts.clear();
        self.recent_calls.clear();
        self.reported.clear();
    }

    /// Whether the detector is enabled.
    pub fn is_enabled(&self) -> bool {
        self.config.enabled
    }
}

// ---------------------------------------------------------------------------
// Skill Generation
// ---------------------------------------------------------------------------

/// A generated skill candidate awaiting user approval.
#[derive(Debug, Clone)]
pub struct SkillCandidate {
    /// Suggested skill name.
    pub name: String,
    /// Description of what the skill does.
    pub description: String,
    /// The tool call pattern that triggered generation.
    pub pattern: ToolCallPattern,
    /// Generated SKILL.md content.
    pub skill_md: String,
    /// Approval state.
    pub state: CandidateState,
}

/// State of a skill candidate in the approval pipeline.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CandidateState {
    /// Awaiting user review and approval.
    Pending,
    /// Approved by user, ready for installation.
    Approved,
    /// Rejected by user.
    Rejected,
}

/// Generates SKILL.md content from a detected pattern.
pub fn generate_skill_md(pattern: &ToolCallPattern) -> SkillCandidate {
    let name = pattern.suggested_name();
    let tools_list = pattern
        .tool_names
        .iter()
        .map(|t| format!("  - {t}"))
        .collect::<Vec<_>>()
        .join("\n");

    let description = format!(
        "Auto-generated skill: executes {} in sequence",
        pattern
            .tool_names
            .iter()
            .map(|t| format!("`{t}`"))
            .collect::<Vec<_>>()
            .join(" -> ")
    );

    let instructions = format!(
        "Execute the following tools in order:\n{}",
        pattern
            .tool_names
            .iter()
            .enumerate()
            .map(|(i, t)| format!("{}. Use the `{t}` tool", i + 1))
            .collect::<Vec<_>>()
            .join("\n")
    );

    let skill_md = format!(
        "---\n\
         name: {name}\n\
         description: \"{description}\"\n\
         version: 0.1.0\n\
         allowed-tools:\n\
         {tools_list}\n\
         user-invocable: false\n\
         autogenerated: true\n\
         ---\n\n\
         {instructions}\n"
    );

    SkillCandidate {
        name,
        description,
        pattern: pattern.clone(),
        skill_md,
        state: CandidateState::Pending,
    }
}

/// Install a skill candidate to the managed skills directory.
///
/// Creates a directory `{install_dir}/{name}/` with a `SKILL.md` file.
/// The skill is installed in "pending" state -- it will not be loaded
/// by the skill watcher until approved.
pub fn install_pending_skill(
    candidate: &SkillCandidate,
    install_dir: &Path,
) -> Result<PathBuf, String> {
    let skill_dir = install_dir.join(&candidate.name);

    std::fs::create_dir_all(&skill_dir)
        .map_err(|e| format!("create skill dir: {e}"))?;

    let skill_path = skill_dir.join("SKILL.md");
    std::fs::write(&skill_path, &candidate.skill_md)
        .map_err(|e| format!("write SKILL.md: {e}"))?;

    // Write a .pending marker file -- the skill watcher should
    // skip loading skills with this marker until approved.
    let marker_path = skill_dir.join(".pending");
    std::fs::write(&marker_path, "awaiting user approval")
        .map_err(|e| format!("write .pending marker: {e}"))?;

    info!(
        skill = %candidate.name,
        path = %skill_dir.display(),
        "installed pending skill"
    );

    Ok(skill_dir)
}

/// Approve a pending skill by removing the `.pending` marker.
pub fn approve_skill(skill_dir: &Path) -> Result<(), String> {
    let marker = skill_dir.join(".pending");
    if marker.exists() {
        std::fs::remove_file(&marker).map_err(|e| format!("remove .pending: {e}"))?;
        info!(skill_dir = %skill_dir.display(), "approved pending skill");
        Ok(())
    } else {
        Err("skill is not in pending state".into())
    }
}

/// Reject a pending skill by removing its directory.
pub fn reject_skill(skill_dir: &Path) -> Result<(), String> {
    if skill_dir.exists() {
        std::fs::remove_dir_all(skill_dir)
            .map_err(|e| format!("remove skill dir: {e}"))?;
        info!(skill_dir = %skill_dir.display(), "rejected and removed pending skill");
        Ok(())
    } else {
        Err("skill directory does not exist".into())
    }
}

/// Check if a skill directory is in pending state.
pub fn is_pending(skill_dir: &Path) -> bool {
    skill_dir.join(".pending").exists()
}

// ---------------------------------------------------------------------------
// Trajectory-based prompt improvement
// ---------------------------------------------------------------------------

/// Improve a generated skill's instructions using trajectory data.
///
/// If the [`TrajectoryLearner`] has successful patterns (trajectories
/// scoring >= 0.8), this converts them into [`TrajectoryHint`]s, selects
/// the best mutation strategy via [`auto_select_strategy`], and applies
/// [`mutate_prompt`] to the skill instructions.
///
/// Returns the (possibly improved) instructions. If there are no
/// successful patterns, the original instructions are returned unchanged.
pub fn improve_skill_instructions(
    instructions: &str,
    learner: &TrajectoryLearner,
) -> String {
    let best = learner.get_best_trajectories(10);
    let poor = learner.get_poor_trajectories(10);

    if best.is_empty() && poor.is_empty() {
        return instructions.to_string();
    }

    let hints: Vec<TrajectoryHint> = best
        .iter()
        .chain(poor.iter())
        .map(|st| TrajectoryHint {
            request_content: st
                .trajectory
                .request
                .messages
                .first()
                .map(|m| m.content.clone())
                .unwrap_or_default(),
            quality_score: st.trajectory.quality.overall,
            feedback: st.feedback.clone(),
        })
        .collect();

    let strategy = auto_select_strategy(&hints);
    debug!(?strategy, hints_count = hints.len(), "mutating skill instructions");
    mutate_prompt(instructions, &hints, strategy)
}

/// Generate a skill candidate and optionally improve it using trajectory data.
///
/// Combines [`generate_skill_md`] with [`improve_skill_instructions`] when
/// a [`TrajectoryLearner`] is available.
pub fn generate_skill_md_with_learning(
    pattern: &ToolCallPattern,
    learner: Option<&TrajectoryLearner>,
) -> SkillCandidate {
    let mut candidate = generate_skill_md(pattern);

    if let Some(learner) = learner {
        let patterns = learner.extract_successful_patterns();
        if !patterns.is_empty() {
            debug!(
                pattern_count = patterns.len(),
                "improving skill via trajectory learning"
            );
            candidate.skill_md = improve_skill_instructions(&candidate.skill_md, learner);
        }
    }

    candidate
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    fn enabled_config() -> AutogenConfig {
        AutogenConfig {
            enabled: true,
            threshold: 3,
            max_pending: 10,
            install_dir: None,
        }
    }

    fn disabled_config() -> AutogenConfig {
        AutogenConfig::default()
    }

    // -- AutogenConfig tests --

    #[test]
    fn config_default_is_disabled() {
        let config = AutogenConfig::default();
        assert!(!config.enabled);
        assert_eq!(config.threshold, 3);
        assert_eq!(config.max_pending, 10);
    }

    #[test]
    fn config_install_dir_default() {
        let config = AutogenConfig::default();
        let dir = config.install_dir();
        assert!(dir.to_string_lossy().contains(".clawft"));
        assert!(dir.to_string_lossy().contains("skills"));
    }

    // -- ToolCallPattern tests --

    #[test]
    fn pattern_suggested_name() {
        let pattern = ToolCallPattern::new(vec!["read_file".into(), "edit_file".into()]);
        let name = pattern.suggested_name();
        assert!(name.contains("read_file"));
        assert!(name.contains("edit_file"));
        assert!(name.ends_with("-auto"));
    }

    #[test]
    fn pattern_suggested_name_long_truncated() {
        let tools: Vec<String> = (0..20).map(|i| format!("tool_{i}")).collect();
        let pattern = ToolCallPattern::new(tools);
        let name = pattern.suggested_name();
        assert!(name.len() <= 65, "name too long: {}", name.len());
    }

    #[test]
    fn pattern_empty() {
        let pattern = ToolCallPattern::new(vec![]);
        assert!(pattern.is_empty());
        assert_eq!(pattern.len(), 0);
        assert_eq!(pattern.suggested_name(), "unnamed-skill");
    }

    // -- PatternDetector tests --

    #[test]
    fn detector_disabled_records_nothing() {
        let mut detector = PatternDetector::new(disabled_config());
        detector.record_tool_call("read_file");
        detector.record_tool_call("edit_file");
        let candidates = detector.detect_candidates();
        assert!(candidates.is_empty());
    }

    #[test]
    fn detector_does_not_fire_below_threshold() {
        let mut detector = PatternDetector::new(enabled_config());
        // Record pattern twice (below threshold of 3)
        for _ in 0..2 {
            detector.record_tool_call("read_file");
            detector.record_tool_call("edit_file");
        }
        let candidates = detector.detect_candidates();
        assert!(candidates.is_empty());
    }

    #[test]
    fn detector_fires_at_threshold() {
        let mut detector = PatternDetector::new(enabled_config());
        // Record pattern 3 times (at threshold)
        for _ in 0..3 {
            detector.record_tool_call("read_file");
            detector.record_tool_call("edit_file");
        }
        let candidates = detector.detect_candidates();
        assert!(
            !candidates.is_empty(),
            "should detect at least one pattern"
        );
    }

    #[test]
    fn detector_does_not_re_report() {
        let mut detector = PatternDetector::new(enabled_config());
        for _ in 0..5 {
            detector.record_tool_call("read_file");
            detector.record_tool_call("edit_file");
        }
        let first = detector.detect_candidates();
        assert!(!first.is_empty());

        // Second call should not re-report the same pattern
        let second = detector.detect_candidates();
        assert!(second.is_empty(), "should not re-report same pattern");
    }

    #[test]
    fn detector_reset_clears_state() {
        let mut detector = PatternDetector::new(enabled_config());
        for _ in 0..3 {
            detector.record_tool_call("a");
            detector.record_tool_call("b");
        }
        let _ = detector.detect_candidates();
        detector.reset();

        // After reset, same pattern should be detected again
        for _ in 0..3 {
            detector.record_tool_call("a");
            detector.record_tool_call("b");
        }
        let candidates = detector.detect_candidates();
        assert!(!candidates.is_empty());
    }

    #[test]
    fn detector_configurable_threshold() {
        let config = AutogenConfig {
            enabled: true,
            threshold: 5,
            ..Default::default()
        };
        let mut detector = PatternDetector::new(config);

        for _ in 0..4 {
            detector.record_tool_call("x");
            detector.record_tool_call("y");
        }
        assert!(detector.detect_candidates().is_empty(), "below threshold");

        detector.record_tool_call("x");
        detector.record_tool_call("y");
        assert!(!detector.detect_candidates().is_empty(), "at threshold");
    }

    // -- Skill generation tests --

    #[test]
    fn generate_skill_md_valid() {
        let pattern = ToolCallPattern::new(vec![
            "read_file".into(),
            "edit_file".into(),
            "write_file".into(),
        ]);
        let candidate = generate_skill_md(&pattern);

        assert!(candidate.name.contains("auto"));
        assert_eq!(candidate.state, CandidateState::Pending);
        assert!(candidate.skill_md.contains("---"));
        assert!(candidate.skill_md.contains("read_file"));
        assert!(candidate.skill_md.contains("edit_file"));
        assert!(candidate.skill_md.contains("write_file"));
        assert!(candidate.skill_md.contains("autogenerated: true"));
        assert!(candidate.skill_md.contains("user-invocable: false"));
    }

    #[test]
    fn generate_skill_md_description_contains_tools() {
        let pattern = ToolCallPattern::new(vec!["a".into(), "b".into()]);
        let candidate = generate_skill_md(&pattern);
        assert!(candidate.description.contains("`a`"));
        assert!(candidate.description.contains("`b`"));
    }

    // -- Install/approve/reject tests --

    #[test]
    fn install_pending_skill_creates_files() {
        let dir = std::env::temp_dir().join("clawft_autogen_install_test");
        let _ = std::fs::remove_dir_all(&dir);
        let _ = std::fs::create_dir_all(&dir);

        let pattern = ToolCallPattern::new(vec!["read_file".into(), "edit_file".into()]);
        let candidate = generate_skill_md(&pattern);

        let result = install_pending_skill(&candidate, &dir);
        assert!(result.is_ok());

        let skill_dir = result.unwrap();
        assert!(skill_dir.join("SKILL.md").exists());
        assert!(skill_dir.join(".pending").exists());
        assert!(is_pending(&skill_dir));

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn approve_skill_removes_marker() {
        let dir = std::env::temp_dir().join("clawft_autogen_approve_test");
        let _ = std::fs::remove_dir_all(&dir);
        let _ = std::fs::create_dir_all(&dir);

        let pattern = ToolCallPattern::new(vec!["a".into(), "b".into()]);
        let candidate = generate_skill_md(&pattern);
        let skill_dir = install_pending_skill(&candidate, &dir).unwrap();

        assert!(is_pending(&skill_dir));
        assert!(approve_skill(&skill_dir).is_ok());
        assert!(!is_pending(&skill_dir));
        assert!(skill_dir.join("SKILL.md").exists());

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn reject_skill_removes_directory() {
        let dir = std::env::temp_dir().join("clawft_autogen_reject_test");
        let _ = std::fs::remove_dir_all(&dir);
        let _ = std::fs::create_dir_all(&dir);

        let pattern = ToolCallPattern::new(vec!["x".into(), "y".into()]);
        let candidate = generate_skill_md(&pattern);
        let skill_dir = install_pending_skill(&candidate, &dir).unwrap();

        assert!(skill_dir.exists());
        assert!(reject_skill(&skill_dir).is_ok());
        assert!(!skill_dir.exists());

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn approve_non_pending_fails() {
        let dir = std::env::temp_dir().join("clawft_autogen_approve_nopend");
        let _ = std::fs::remove_dir_all(&dir);
        let _ = std::fs::create_dir_all(&dir);

        // No .pending marker
        let result = approve_skill(&dir);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("not in pending state"));

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn generated_skill_has_minimal_permissions() {
        let pattern = ToolCallPattern::new(vec!["read_file".into()]);
        let candidate = generate_skill_md(&pattern);

        // Generated skill should NOT have shell, network, or broad FS access
        assert!(!candidate.skill_md.contains("shell: true"));
        assert!(!candidate.skill_md.contains("network:"));
        // user-invocable should be false for safety
        assert!(candidate.skill_md.contains("user-invocable: false"));
    }

    #[test]
    fn is_enabled_reflects_config() {
        let enabled = PatternDetector::new(enabled_config());
        assert!(enabled.is_enabled());

        let disabled = PatternDetector::new(disabled_config());
        assert!(!disabled.is_enabled());
    }

    // -- Trajectory-based improvement tests --

    fn make_test_learner() -> TrajectoryLearner {
        use crate::pipeline::learner::TrajectoryLearnerConfig;
        use crate::pipeline::traits::{
            ChatRequest, LlmMessage, QualityScore, RoutingDecision, Trajectory,
        };
        use clawft_types::provider::{ContentBlock, LlmResponse, StopReason, Usage};

        let learner = TrajectoryLearner::new(TrajectoryLearnerConfig::default());

        let make_traj = |overall: f32, content: &str| Trajectory {
            request: ChatRequest {
                messages: vec![LlmMessage {
                    role: "user".into(),
                    content: content.into(),
                    tool_call_id: None,
                    tool_calls: None,
                }],
                tools: vec![],
                model: None,
                max_tokens: None,
                temperature: None,
                auth_context: None,
                complexity_boost: 0.0,
            },
            routing: RoutingDecision::default(),
            response: LlmResponse {
                id: "r".into(),
                content: vec![ContentBlock::Text {
                    text: "ok".into(),
                }],
                stop_reason: StopReason::EndTurn,
                usage: Usage {
                    input_tokens: 5,
                    output_tokens: 2,
                    total_tokens: 0,
                },
                metadata: std::collections::HashMap::new(),
            },
            quality: QualityScore {
                overall,
                relevance: overall,
                coherence: overall,
            },
        };

        // Record some good and poor trajectories
        use crate::pipeline::traits::LearningBackend;
        learner.record(&make_traj(0.95, "Write a function to sort a list"));
        learner.record(&make_traj(0.9, "Explain recursion with examples"));
        learner.record(&make_traj(0.3, "Fix the bug"));
        learner.record(&make_traj(0.4, "Summarize article"));

        learner
    }

    #[test]
    fn improve_skill_instructions_with_patterns() {
        let learner = make_test_learner();
        let original = "Use the read_file tool\nCheck the output";
        let improved = improve_skill_instructions(original, &learner);

        // Should be different since we have trajectory data
        assert!(!improved.is_empty());
    }

    #[test]
    fn improve_skill_instructions_empty_learner_returns_original() {
        use crate::pipeline::learner::TrajectoryLearnerConfig;
        let learner = TrajectoryLearner::new(TrajectoryLearnerConfig::default());
        let original = "Use the read_file tool";
        let result = improve_skill_instructions(original, &learner);
        assert_eq!(result, original);
    }

    #[test]
    fn generate_skill_md_with_learning_no_learner() {
        let pattern = ToolCallPattern::new(vec!["a".into(), "b".into()]);
        let candidate = generate_skill_md_with_learning(&pattern, None);
        assert!(candidate.skill_md.contains("autogenerated: true"));
    }

    #[test]
    fn generate_skill_md_with_learning_uses_learner() {
        let learner = make_test_learner();
        let pattern = ToolCallPattern::new(vec!["read_file".into(), "edit_file".into()]);
        let candidate = generate_skill_md_with_learning(&pattern, Some(&learner));

        // Should still be a valid skill
        assert!(candidate.skill_md.contains("read_file"));
        assert!(!candidate.skill_md.is_empty());
    }
}