dist_agent_lang 1.0.12

Hybrid programming with library and CLI support for Off/On-chain network integration
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
//! Agent skills: extensible skill registry, .skill.dal loader, and prompt builder.
//! See docs/SKILLS_DESIGN_PLAN.md.
//!
//! Skills are named bundles (description, tools) that define what an agent can do.
//! Built-in skills: development, creative, office, home, project_init.
//! User-defined skills are loaded from `.skill.dal` files at a configurable path.
//! Persistence is the default for the registry: loaded once at first use.

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

// ── Skill definition ────────────────────────────────────────────────

/// A skill definition: name, optional category, description, and granted tools.
#[derive(Debug, Clone, PartialEq)]
pub struct SkillDefinition {
    pub name: String,
    pub category: Option<SkillCategory>,
    pub description: String,
    /// Tool IDs or tags this skill grants (e.g. "read_file", "write_file").
    /// Empty means "description-only" — no additional tools beyond base.
    pub tools: Vec<String>,
    /// Whether this is a built-in skill (cannot be overridden by user definitions).
    pub builtin: bool,
}

/// Skill categories — the four pillars of the default learning path.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum SkillCategory {
    Development,
    Creative,
    Office,
    Home,
}

impl SkillCategory {
    pub fn from_str(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "development" | "dev" => Some(Self::Development),
            "creative" => Some(Self::Creative),
            "office" => Some(Self::Office),
            "home" => Some(Self::Home),
            _ => None,
        }
    }

    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Development => "development",
            Self::Creative => "creative",
            Self::Office => "office",
            Self::Home => "home",
        }
    }
}

// ── Skill registry ──────────────────────────────────────────────────

/// In-memory registry of all known skills (built-in + user-defined).
#[derive(Debug, Clone)]
pub struct SkillRegistry {
    skills: HashMap<String, SkillDefinition>,
}

/// Default skill set for the DAL assistant (smart agent assist).
/// When an agent has no explicit skills, use these.
pub const DEFAULT_LEARNING_PATH_SKILLS: &[&str] = &["development", "creative", "office", "home"];

/// Hard skill: always available to every agent.
pub const PROJECT_INIT_SKILL: &str = "project_init";

/// Base tools (all agents): reply, run, search, ask_user, dal_init.
const BASE_TOOLS: &str = "reply, run (shell), search, ask_user, dal_init";

/// Development skill tools.
const DEVELOPMENT_TOOL_IDS: &[&str] = &[
    "read_file",
    "write_file",
    "list_dir",
    "dal_check",
    "dal_run",
];

impl SkillRegistry {
    /// Create an empty registry.
    pub fn new() -> Self {
        Self {
            skills: HashMap::new(),
        }
    }

    /// Create a registry pre-populated with built-in skills.
    pub fn with_builtins() -> Self {
        let mut reg = Self::new();
        reg.register(SkillDefinition {
            name: "project_init".to_string(),
            category: Some(SkillCategory::Development),
            description: "Initialize and set up a DAL project (dal.toml + entry file); run dal init or create layout.".to_string(),
            tools: vec!["dal_init".to_string()],
            builtin: true,
        });
        reg.register(SkillDefinition {
            name: "development".to_string(),
            category: Some(SkillCategory::Development),
            description: "Coding, scripts, DAL projects: read/edit files, dal check/run, debug, explain code.".to_string(),
            tools: DEVELOPMENT_TOOL_IDS.iter().map(|s| s.to_string()).collect(),
            builtin: true,
        });
        reg.register(SkillDefinition {
            name: "creative".to_string(),
            category: Some(SkillCategory::Creative),
            description:
                "Writing, design, ideation: drafts, outlines, copy, storytelling, tone, brainstorm."
                    .to_string(),
            tools: vec![],
            builtin: true,
        });
        reg.register(SkillDefinition {
            name: "office".to_string(),
            category: Some(SkillCategory::Office),
            description: "Tasks, scheduling, docs, meetings: summarize, action lists, email/note drafts, coordination.".to_string(),
            tools: vec![],
            builtin: true,
        });
        reg.register(SkillDefinition {
            name: "home".to_string(),
            category: Some(SkillCategory::Home),
            description: "Personal tasks, routines, health, family, hobbies: reminders, recipes, how-tos, lists.".to_string(),
            tools: vec![],
            builtin: true,
        });
        reg
    }

    /// Register a skill definition. User-defined skills cannot override built-ins.
    pub fn register(&mut self, skill: SkillDefinition) {
        if let Some(existing) = self.skills.get(&skill.name) {
            if existing.builtin && !skill.builtin {
                log::warn!(
                    "Ignoring user-defined skill '{}': cannot override built-in.",
                    skill.name
                );
                return;
            }
        }
        self.skills.insert(skill.name.clone(), skill);
    }

    /// Register multiple skill definitions (e.g. from a loaded file).
    pub fn register_all(&mut self, skills: Vec<SkillDefinition>) {
        for skill in skills {
            self.register(skill);
        }
    }

    /// Look up a skill by name.
    pub fn get(&self, name: &str) -> Option<&SkillDefinition> {
        self.skills.get(name)
    }

    /// All registered skill names.
    pub fn names(&self) -> Vec<&str> {
        self.skills.keys().map(|s| s.as_str()).collect()
    }

    /// Number of registered skills.
    pub fn len(&self) -> usize {
        self.skills.len()
    }

    pub fn is_empty(&self) -> bool {
        self.skills.is_empty()
    }

    /// Resolve a list of skill names against the registry.
    /// Returns the definitions found; unknown names are logged and skipped.
    pub fn resolve(&self, names: &[String]) -> Vec<&SkillDefinition> {
        names
            .iter()
            .filter_map(|name| {
                let def = self.skills.get(name.as_str());
                if def.is_none() {
                    log::debug!("Skill '{}' not found in registry; skipping.", name);
                }
                def
            })
            .collect()
    }

    /// Collect the union of tool IDs granted by the given skill names.
    pub fn tools_for_skills(&self, names: &[String]) -> Vec<String> {
        let mut tools = Vec::new();
        let mut seen = std::collections::HashSet::new();
        for def in self.resolve(names) {
            for tool in &def.tools {
                if seen.insert(tool.clone()) {
                    tools.push(tool.clone());
                }
            }
        }
        tools
    }

    /// Build skill descriptions block for the given skill names.
    pub fn descriptions_for_skills(&self, names: &[String]) -> Vec<String> {
        self.resolve(names)
            .into_iter()
            .filter(|d| !d.description.is_empty())
            .map(|d| format!("- {}: {}", d.name, d.description))
            .collect()
    }

    /// List all registered skills as (name, description). For DAL skills::list().
    pub fn list_names_and_descriptions(&self) -> Vec<(String, String)> {
        self.skills
            .iter()
            .map(|(name, def)| (name.clone(), def.description.clone()))
            .collect()
    }
}

// ── .skill.dal loader ───────────────────────────────────────────────

/// Parse skill definitions from a `.skill.dal` file or string.
///
/// Format (DAL-native declarative):
/// ```text
/// skill "ms_office" {
///   category "office"
///   description "Use MS Office tools (Word, Excel, Outlook) via run or scripts."
///   tools "run" "search"
/// }
/// ```
///
/// Multiple skill blocks per file are supported.
pub fn parse_skill_dal(source: &str) -> Result<Vec<SkillDefinition>, String> {
    let mut skills = Vec::new();
    let mut chars = source.char_indices().peekable();

    while let Some(&(i, _)) = chars.peek() {
        skip_whitespace_and_comments(&mut chars);
        if chars.peek().is_none() {
            break;
        }

        let rest = &source[chars.peek().map(|&(i, _)| i).unwrap_or(source.len())..];
        if rest.starts_with("skill")
            && rest[5..].starts_with(|c: char| c.is_whitespace() || c == '"')
        {
            for _ in 0..5 {
                chars.next();
            }
            skip_whitespace_and_comments(&mut chars);

            let name = parse_quoted_string(&mut chars, source)
                .ok_or_else(|| format!("Expected quoted skill name near byte {}", i))?;

            skip_whitespace_and_comments(&mut chars);

            expect_char(&mut chars, '{').map_err(|_| {
                format!("Expected '{{' after skill name '{}' near byte {}", name, i)
            })?;

            let mut category: Option<SkillCategory> = None;
            let mut description = String::new();
            let mut tools: Vec<String> = Vec::new();

            loop {
                skip_whitespace_and_comments(&mut chars);
                if chars.peek().map(|&(_, c)| c) == Some('}') {
                    chars.next();
                    break;
                }
                if chars.peek().is_none() {
                    return Err(format!("Unterminated skill block '{}'", name));
                }

                let keyword = parse_identifier(&mut chars, source);
                skip_whitespace_and_comments(&mut chars);

                match keyword.as_str() {
                    "category" => {
                        let val = parse_quoted_string(&mut chars, source).ok_or_else(|| {
                            format!("Expected quoted category in skill '{}'", name)
                        })?;
                        category = SkillCategory::from_str(&val);
                    }
                    "description" => {
                        description = parse_quoted_string(&mut chars, source).ok_or_else(|| {
                            format!("Expected quoted description in skill '{}'", name)
                        })?;
                    }
                    "tools" => {
                        while let Some(tool) = try_parse_quoted_string(&mut chars, source) {
                            tools.push(tool);
                            skip_whitespace_and_comments(&mut chars);
                        }
                    }
                    other if other.is_empty() => {
                        return Err(format!("Unexpected character in skill '{}' block", name));
                    }
                    other => {
                        log::warn!("Unknown field '{}' in skill '{}'; skipping.", other, name);
                        skip_to_newline(&mut chars);
                    }
                }
            }

            skills.push(SkillDefinition {
                name,
                category,
                description,
                tools,
                builtin: false,
            });
        } else {
            skip_to_newline(&mut chars);
        }
    }

    Ok(skills)
}

/// Load skill definitions from a single `.skill.dal` file.
pub fn load_skill_file(path: &Path) -> Result<Vec<SkillDefinition>, String> {
    let source =
        std::fs::read_to_string(path).map_err(|e| format!("Failed to read {:?}: {}", path, e))?;
    parse_skill_dal(&source)
}

/// Load skill definitions from a path (file or directory of `.skill.dal` files).
pub fn load_skills_from_path(path: &Path) -> Result<Vec<SkillDefinition>, String> {
    if path.is_file() {
        return load_skill_file(path);
    }
    if path.is_dir() {
        let mut all = Vec::new();
        let entries =
            std::fs::read_dir(path).map_err(|e| format!("Failed to read dir {:?}: {}", path, e))?;
        let mut paths: Vec<PathBuf> = entries
            .filter_map(|e| e.ok())
            .map(|e| e.path())
            .filter(|p| {
                p.extension()
                    .and_then(|ext| ext.to_str())
                    .map(|ext| ext == "dal")
                    .unwrap_or(false)
                    && p.file_name()
                        .and_then(|n| n.to_str())
                        .map(|n| n.contains(".skill."))
                        .unwrap_or(false)
            })
            .collect();
        paths.sort();
        for p in paths {
            match load_skill_file(&p) {
                Ok(defs) => all.extend(defs),
                Err(e) => log::warn!("Skipping {:?}: {}", p, e),
            }
        }
        return Ok(all);
    }
    Ok(Vec::new())
}

// ── Parser helpers ──────────────────────────────────────────────────

type CharIter<'a> = std::iter::Peekable<std::str::CharIndices<'a>>;

fn skip_whitespace_and_comments(chars: &mut CharIter) {
    loop {
        match chars.peek() {
            Some(&(_, c)) if c.is_whitespace() => {
                chars.next();
            }
            Some(&(_, '#')) => {
                skip_to_newline(chars);
            }
            Some(&(_, '/')) => {
                let mut clone = chars.clone();
                clone.next();
                if clone.peek().map(|&(_, c)| c) == Some('/') {
                    skip_to_newline(chars);
                } else {
                    break;
                }
            }
            _ => break,
        }
    }
}

fn skip_to_newline(chars: &mut CharIter) {
    while let Some(&(_, c)) = chars.peek() {
        chars.next();
        if c == '\n' {
            break;
        }
    }
}

fn parse_quoted_string(chars: &mut CharIter, _source: &str) -> Option<String> {
    if chars.peek().map(|&(_, c)| c) != Some('"') {
        return None;
    }
    chars.next(); // consume opening "
    let mut result = String::new();
    while let Some(&(_, c)) = chars.peek() {
        chars.next();
        if c == '"' {
            return Some(result);
        }
        if c == '\\' {
            if let Some(&(_, escaped)) = chars.peek() {
                chars.next();
                match escaped {
                    'n' => result.push('\n'),
                    't' => result.push('\t'),
                    '"' => result.push('"'),
                    '\\' => result.push('\\'),
                    other => {
                        result.push('\\');
                        result.push(other);
                    }
                }
                continue;
            }
        }
        result.push(c);
    }
    None // unterminated string
}

fn try_parse_quoted_string(chars: &mut CharIter, source: &str) -> Option<String> {
    if chars.peek().map(|&(_, c)| c) == Some('"') {
        parse_quoted_string(chars, source)
    } else {
        None
    }
}

fn parse_identifier(chars: &mut CharIter, _source: &str) -> String {
    let mut result = String::new();
    while let Some(&(_, c)) = chars.peek() {
        if c.is_alphanumeric() || c == '_' {
            result.push(c);
            chars.next();
        } else {
            break;
        }
    }
    result
}

fn expect_char(chars: &mut CharIter, expected: char) -> Result<(), ()> {
    if chars.peek().map(|&(_, c)| c) == Some(expected) {
        chars.next();
        Ok(())
    } else {
        Err(())
    }
}

// ── Skills config ───────────────────────────────────────────────────

/// Resolve the skills path from env and agent.toml/dal.toml.
/// Returns None if no skills path is configured and no default directory exists.
pub fn resolve_skills_path() -> Option<PathBuf> {
    if let Ok(p) = std::env::var("DAL_SKILLS_PATH") {
        let path = PathBuf::from(p);
        if path.exists() {
            return Some(path);
        }
        log::warn!("DAL_SKILLS_PATH={:?} does not exist.", path);
    }

    let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));

    for name in &["agent.toml", "dal.toml"] {
        let toml_path = cwd.join(name);
        if let Ok(content) = std::fs::read_to_string(&toml_path) {
            if let Ok(table) = content.parse::<toml::Table>() {
                if let Some(sp) = table
                    .get("agent")
                    .and_then(|v| v.as_table())
                    .and_then(|t| t.get("skills_path"))
                    .and_then(|v| v.as_str())
                {
                    let mut p = PathBuf::from(sp);
                    if !p.is_absolute() {
                        p = cwd.join(p);
                    }
                    if p.exists() {
                        return Some(p);
                    }
                    log::warn!("skills_path={:?} from {} does not exist.", p, name);
                }
            }
        }
    }

    // Default: .dal/ directory if it contains any .skill.dal files
    let dal_dir = cwd.join(".dal");
    if dal_dir.is_dir() {
        if let Ok(entries) = std::fs::read_dir(&dal_dir) {
            let has_skill_files = entries.filter_map(|e| e.ok()).any(|e| {
                e.path()
                    .file_name()
                    .and_then(|n| n.to_str())
                    .map(|n| n.contains(".skill.") && n.ends_with(".dal"))
                    .unwrap_or(false)
            });
            if has_skill_files {
                return Some(dal_dir);
            }
        }
    }

    None
}

// ── Global registry (loaded once) ───────────────────────────────────

fn get_global_registry() -> &'static Mutex<SkillRegistry> {
    static REGISTRY: OnceLock<Mutex<SkillRegistry>> = OnceLock::new();
    REGISTRY.get_or_init(|| {
        let mut reg = SkillRegistry::with_builtins();
        if let Some(path) = resolve_skills_path() {
            match load_skills_from_path(&path) {
                Ok(defs) => {
                    let count = defs.len();
                    reg.register_all(defs);
                    if count > 0 {
                        log::info!("Loaded {} user-defined skill(s) from {:?}", count, path);
                    }
                }
                Err(e) => {
                    log::warn!("Failed to load user skills from {:?}: {}", path, e);
                }
            }
        }
        Mutex::new(reg)
    })
}

/// Get a clone of the global skill registry.
pub fn global_registry() -> SkillRegistry {
    get_global_registry().lock().unwrap().clone()
}

/// List skill names and descriptions. If path is None, uses the global registry (built-ins + resolved skills_path). If path is Some(p), loads from that directory only.
pub fn list_skills(path: Option<&Path>) -> Result<Vec<(String, String)>, String> {
    let list = if let Some(p) = path {
        let defs = load_skills_from_path(p)?;
        defs.into_iter().map(|d| (d.name, d.description)).collect()
    } else {
        global_registry().list_names_and_descriptions()
    };
    Ok(list)
}

/// Register additional skills into the global registry (e.g. from runtime or tests).
pub fn register_global_skills(skills: Vec<SkillDefinition>) {
    let mut reg = get_global_registry().lock().unwrap();
    reg.register_all(skills);
}

// ── Programmatic encouragement ──────────────────────────────────────

/// Fixed prompt block encouraging the agent to use search and past experience.
/// Always included in every agent prompt.
pub const ENCOURAGEMENT_BLOCK: &str = "\
When you don't have a specific tool for a task, use **search** or **run** to find documentation, \
APIs, or commands you need. Use your past executions in context as examples of how you've solved \
similar problems. If you've successfully discovered information through search or run before, \
apply the same approach to new challenges.";

/// Build an optional meta-from-memory sentence when the agent has relevant history.
/// Inspects the provided context (e.g. evolve content or memory summary) for evidence
/// of past tool use (search, run) and returns a reinforcing sentence if found.
pub fn meta_from_memory(context_text: &str) -> Option<String> {
    if context_text.is_empty() {
        return None;
    }
    let lower = context_text.to_lowercase();
    let has_search = lower.contains("search") || lower.contains("searched");
    let has_run = lower.contains("\"action\":\"run\"") || lower.contains("used run");

    if has_search && has_run {
        Some("In past executions you have used search and run when you didn't have a dedicated tool. This approach has worked well — apply it again when needed.".to_string())
    } else if has_search {
        Some("In past executions you have used search to find information when you didn't have a dedicated tool. Continue this approach when needed.".to_string())
    } else if has_run {
        Some("In past executions you have used run to execute commands when you didn't have a dedicated tool. Continue this approach when needed.".to_string())
    } else {
        None
    }
}

/// Build a reinforcement note for memory when the agent successfully discovers something via search or run.
pub fn reinforcement_note(action: &str, discovery: &str) -> String {
    format!(
        "Used {} to discover: {}. Apply this pattern in future when no direct tool is available.",
        action, discovery
    )
}

// ── tools_description_for_skills (backward-compatible API) ──────────

/// Build the tools_description string for the agent prompt from the given skill names.
/// Uses the global registry. Always includes project_init (hard skill) and encouragement.
/// If skills is empty, uses DEFAULT_LEARNING_PATH_SKILLS.
/// This is the main entry point used by agent_serve.rs.
pub fn tools_description_for_skills(skills: &[String]) -> String {
    let registry = global_registry();
    tools_description_for_skills_with_registry(skills, &registry, None)
}

/// Build the tools_description string using an explicit registry and optional memory context.
/// Richer variant for callers that have access to memory/evolve content.
pub fn tools_description_for_skills_with_registry(
    skills: &[String],
    registry: &SkillRegistry,
    memory_context: Option<&str>,
) -> String {
    let mut effective: Vec<String> = skills.to_vec();
    if effective.is_empty() {
        effective = DEFAULT_LEARNING_PATH_SKILLS
            .iter()
            .map(|s| (*s).to_string())
            .collect();
    }
    if !effective.iter().any(|s| s == PROJECT_INIT_SKILL) {
        effective.insert(0, PROJECT_INIT_SKILL.to_string());
    }

    let skill_descriptions = registry.descriptions_for_skills(&effective);
    let resolved_tools = registry.tools_for_skills(&effective);

    let skills_block = if skill_descriptions.is_empty() {
        "You are a helpful assistant.".to_string()
    } else {
        format!(
            "You are a DAL assistant serving your principal. Skills:\n{}",
            skill_descriptions.join("\n")
        )
    };

    let has_development = effective.iter().any(|s| s == "development");
    let dev_tools_str = DEVELOPMENT_TOOL_IDS.join(", ");
    let tools_list = if has_development {
        format!("{}; {}", BASE_TOOLS, dev_tools_str)
    } else {
        let mut extra: Vec<&str> = Vec::new();
        for tool in &resolved_tools {
            let t = tool.as_str();
            if !BASE_TOOLS.contains(t) && !DEVELOPMENT_TOOL_IDS.contains(&t) {
                extra.push(t);
            }
        }
        if extra.is_empty() {
            BASE_TOOLS.to_string()
        } else {
            format!("{}; {}", BASE_TOOLS, extra.join(", "))
        }
    };

    let json_tools = if has_development {
        r#"Reply with JSON: {"action":"reply","text":"..."} or {"action":"run","cmd":"..."} or {"action":"search","query":"..."} or {"action":"ask_user","message":"..."} or {"action":"dal_init","template":"general"|"chain"|"iot"|"agent"} or {"action":"read_file","path":"..."} or {"action":"write_file","path":"...","contents":"..."} or {"action":"list_dir","path":"."} or {"action":"dal_check","path":"file.dal"} or {"action":"dal_run","path":"file.dal"}. Paths are relative to working directory."#
    } else {
        r#"Reply with JSON: {"action":"reply","text":"..."} or {"action":"run","cmd":"..."} or {"action":"search","query":"..."} or {"action":"ask_user","message":"..."} or {"action":"dal_init","template":"general"|"chain"|"iot"|"agent"} to initialize a DAL project (omit template for general)."#
    };

    let meta_sentence = memory_context
        .and_then(|ctx| meta_from_memory(ctx))
        .unwrap_or_default();

    let encouragement = if meta_sentence.is_empty() {
        ENCOURAGEMENT_BLOCK.to_string()
    } else {
        format!("{} {}", ENCOURAGEMENT_BLOCK, meta_sentence)
    };

    format!(
        "{} Available tools: {}. {} {} When the objective is satisfied, use reply with your final answer. Respect the constraints below.",
        skills_block, tools_list, json_tools, encouragement
    )
}

// ── Tests ───────────────────────────────────────────────────────────

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

    #[test]
    fn default_skills_produce_non_empty_description() {
        let desc = tools_description_for_skills(&[]);
        assert!(desc.contains("assistant"));
        assert!(desc.contains("development"));
        assert!(desc.contains("creative"));
        assert!(desc.contains("office"));
        assert!(desc.contains("home"));
        assert!(desc.contains("project_init"));
        assert!(desc.contains("reply"));
        assert!(desc.contains("ask_user"));
    }

    #[test]
    fn explicit_skills_used() {
        let desc = tools_description_for_skills(&["development".to_string()]);
        assert!(desc.contains("development"));
        assert!(desc.contains("project_init"));
    }

    #[test]
    fn encouragement_always_present() {
        let desc = tools_description_for_skills(&[]);
        assert!(desc.contains("When you don't have a specific tool"));
        assert!(desc.contains("search"));
    }

    #[test]
    fn registry_builtins() {
        let reg = SkillRegistry::with_builtins();
        assert!(reg.get("development").is_some());
        assert!(reg.get("creative").is_some());
        assert!(reg.get("office").is_some());
        assert!(reg.get("home").is_some());
        assert!(reg.get("project_init").is_some());
        assert_eq!(reg.len(), 5);
    }

    #[test]
    fn registry_user_skill_additive() {
        let mut reg = SkillRegistry::with_builtins();
        reg.register(SkillDefinition {
            name: "ms_office".to_string(),
            category: Some(SkillCategory::Office),
            description: "Use MS Office tools via run or scripts.".to_string(),
            tools: vec!["run".to_string()],
            builtin: false,
        });
        assert_eq!(reg.len(), 6);
        assert!(reg.get("ms_office").is_some());
        assert_eq!(
            reg.get("ms_office").unwrap().description,
            "Use MS Office tools via run or scripts."
        );
    }

    #[test]
    fn registry_cannot_override_builtin() {
        let mut reg = SkillRegistry::with_builtins();
        reg.register(SkillDefinition {
            name: "development".to_string(),
            category: Some(SkillCategory::Development),
            description: "Overridden!".to_string(),
            tools: vec![],
            builtin: false,
        });
        assert!(reg.get("development").unwrap().builtin);
        assert_ne!(reg.get("development").unwrap().description, "Overridden!");
    }

    #[test]
    fn registry_resolve_skills() {
        let mut reg = SkillRegistry::with_builtins();
        reg.register(SkillDefinition {
            name: "samsung_smart_home".to_string(),
            category: Some(SkillCategory::Home),
            description: "Control Samsung SmartThings devices.".to_string(),
            tools: vec!["run".to_string(), "search".to_string()],
            builtin: false,
        });
        let resolved = reg.resolve(&[
            "development".to_string(),
            "samsung_smart_home".to_string(),
            "nonexistent".to_string(),
        ]);
        assert_eq!(resolved.len(), 2);
    }

    #[test]
    fn registry_tools_union() {
        let mut reg = SkillRegistry::with_builtins();
        reg.register(SkillDefinition {
            name: "social_media".to_string(),
            category: Some(SkillCategory::Office),
            description: "Social media management.".to_string(),
            tools: vec![
                "run".to_string(),
                "search".to_string(),
                "custom_api".to_string(),
            ],
            builtin: false,
        });
        let tools = reg.tools_for_skills(&["development".to_string(), "social_media".to_string()]);
        assert!(tools.contains(&"read_file".to_string()));
        assert!(tools.contains(&"custom_api".to_string()));
        assert!(tools.contains(&"run".to_string()));
    }

    #[test]
    fn user_skill_in_prompt() {
        let mut reg = SkillRegistry::with_builtins();
        reg.register(SkillDefinition {
            name: "ms_office".to_string(),
            category: Some(SkillCategory::Office),
            description: "Use MS Office tools (Word, Excel, Outlook) via run or scripts."
                .to_string(),
            tools: vec!["run".to_string()],
            builtin: false,
        });
        let desc = tools_description_for_skills_with_registry(
            &["office".to_string(), "ms_office".to_string()],
            &reg,
            None,
        );
        assert!(desc.contains("ms_office"));
        assert!(desc.contains("MS Office"));
    }

    #[test]
    fn parse_skill_dal_basic() {
        let source = r#"
skill "ms_office" {
  category "office"
  description "Use MS Office tools (Word, Excel, Outlook) via run or scripts."
  tools "run" "search"
}

skill "samsung_smart_home" {
  category "home"
  description "Control and query Samsung SmartThings devices."
  tools "run" "search"
}
"#;
        let skills = parse_skill_dal(source).unwrap();
        assert_eq!(skills.len(), 2);
        assert_eq!(skills[0].name, "ms_office");
        assert_eq!(skills[0].category, Some(SkillCategory::Office));
        assert!(skills[0].description.contains("MS Office"));
        assert_eq!(
            skills[0].tools,
            vec!["run".to_string(), "search".to_string()]
        );
        assert!(!skills[0].builtin);
        assert_eq!(skills[1].name, "samsung_smart_home");
        assert_eq!(skills[1].category, Some(SkillCategory::Home));
    }

    #[test]
    fn parse_skill_dal_with_comments() {
        let source = r#"
# My custom skills
skill "social_media_manager" {
  category "office"
  description "Manage social media accounts, schedule posts, analyze engagement."
  tools "run" "search"
}
// Another comment style
"#;
        let skills = parse_skill_dal(source).unwrap();
        assert_eq!(skills.len(), 1);
        assert_eq!(skills[0].name, "social_media_manager");
    }

    #[test]
    fn parse_skill_dal_no_category() {
        let source = r#"
skill "custom_tool" {
  description "A custom tool with no category."
  tools "run"
}
"#;
        let skills = parse_skill_dal(source).unwrap();
        assert_eq!(skills.len(), 1);
        assert_eq!(skills[0].category, None);
    }

    #[test]
    fn parse_skill_dal_description_only() {
        let source = r#"
skill "advisor" {
  category "creative"
  description "Provide strategic advice and brainstorming."
}
"#;
        let skills = parse_skill_dal(source).unwrap();
        assert_eq!(skills.len(), 1);
        assert!(skills[0].tools.is_empty());
    }

    #[test]
    fn parse_skill_dal_empty() {
        let skills = parse_skill_dal("").unwrap();
        assert!(skills.is_empty());
    }

    #[test]
    fn parse_skill_dal_unterminated_error() {
        let source = r#"skill "broken" {"#;
        assert!(parse_skill_dal(source).is_err());
    }

    #[test]
    fn meta_from_memory_both() {
        let ctx =
            r#"I searched for the API documentation. Then I used run to install the package."#;
        let meta = meta_from_memory(ctx);
        assert!(meta.is_some());
        assert!(meta.unwrap().contains("search and run"));
    }

    #[test]
    fn meta_from_memory_search_only() {
        let meta = meta_from_memory("I searched for the docs.");
        assert!(meta.is_some());
        assert!(meta.unwrap().contains("search"));
    }

    #[test]
    fn meta_from_memory_empty() {
        assert!(meta_from_memory("").is_none());
    }

    #[test]
    fn meta_from_memory_no_match() {
        assert!(meta_from_memory("The agent replied with the answer.").is_none());
    }

    #[test]
    fn reinforcement_note_format() {
        let note = reinforcement_note("search", "MS Office API endpoint");
        assert!(note.contains("search"));
        assert!(note.contains("MS Office API endpoint"));
    }

    #[test]
    fn load_skills_from_tempdir() {
        let dir = tempfile::tempdir().unwrap();
        let file_path = dir.path().join("office.skill.dal");
        std::fs::write(
            &file_path,
            r#"
skill "ms_office" {
  category "office"
  description "Use MS Office tools."
  tools "run"
}
"#,
        )
        .unwrap();

        let skills = load_skills_from_path(dir.path()).unwrap();
        assert_eq!(skills.len(), 1);
        assert_eq!(skills[0].name, "ms_office");
    }

    #[test]
    fn load_skills_ignores_non_skill_files() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("agent.dal"), "// not a skill file").unwrap();
        std::fs::write(
            dir.path().join("my.skill.dal"),
            r#"skill "test" { description "test" }"#,
        )
        .unwrap();

        let skills = load_skills_from_path(dir.path()).unwrap();
        assert_eq!(skills.len(), 1);
    }

    #[test]
    fn skill_category_roundtrip() {
        for name in &["development", "dev", "creative", "office", "home"] {
            assert!(
                SkillCategory::from_str(name).is_some(),
                "Failed for {}",
                name
            );
        }
        assert!(SkillCategory::from_str("nonexistent").is_none());
        assert_eq!(SkillCategory::Development.as_str(), "development");
    }

    #[test]
    fn full_integration_user_skill_in_registry_and_prompt() {
        let mut reg = SkillRegistry::with_builtins();
        let source = r#"
skill "calendar_scheduler" {
  category "office"
  description "Manage a custom calendar platform for scheduling meetings and events."
  tools "run" "search"
}
"#;
        let defs = parse_skill_dal(source).unwrap();
        reg.register_all(defs);

        let desc = tools_description_for_skills_with_registry(
            &["office".to_string(), "calendar_scheduler".to_string()],
            &reg,
            Some("Previously I searched for the calendar API and used run to create an event."),
        );
        assert!(desc.contains("calendar_scheduler"));
        assert!(desc.contains("custom calendar platform"));
        assert!(desc.contains("When you don't have a specific tool"));
        assert!(desc.contains("past executions you have used search and run"));
    }
}