kumiho-construct 2026.5.11

Construct — memory-native AI agent runtime powered by Kumiho
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
//! Skill meta-tools exposed by the in-process MCP server.
//!
//! Instead of exposing every user skill as a standalone MCP tool (which could
//! easily balloon to 50+ entries), this module provides three compact
//! meta-tools that let external CLIs discover and invoke skills on demand:
//!
//! - `skills_list`     → no args; returns a JSON array of skill summaries.
//! - `skills_describe` → `{ skill_id }`; returns the full skill body + metadata.
//! - `skills_execute`  → `{ skill_id, arguments? }`; dispatches to the skill's
//!                       named `[[tools]]` entry OR returns the body for
//!                       markdown-only skills for the calling model to follow.
//!
//! All three are backed by the shared `crate::skills::load_skills_*` helpers
//! (the same code path `ReadSkillTool` uses) so they stay consistent with
//! the CLI-side view and don't duplicate the on-disk skill lookup.
//!
//! # Testability
//!
//! The tools accept a `SkillSource` so unit tests can hand in a tmp dir or a
//! mocked executor without needing to reach into the real `~/.construct` tree.

use crate::security::SecurityPolicy;
use crate::skills::Skill;
use crate::tools::traits::{Tool, ToolResult};
use async_trait::async_trait;
use serde_json::{Value, json};
use std::path::PathBuf;
use std::sync::Arc;

/// Abstraction over the skill store so we can inject a mock in tests.
///
/// The real implementation walks `workspace_dir/skills/` (and optionally the
/// open-skills mirror); tests can substitute a pre-built `Vec<Skill>`.
pub trait SkillSource: Send + Sync {
    fn load(&self) -> Vec<Skill>;
}

/// Production skill source: reads from disk using the same entry points as
/// `ReadSkillTool` + the gateway's `skills_to_prompt` path.
pub struct DiskSkillSource {
    workspace_dir: PathBuf,
    open_skills_enabled: bool,
    open_skills_dir: Option<String>,
}

impl DiskSkillSource {
    pub fn new(
        workspace_dir: PathBuf,
        open_skills_enabled: bool,
        open_skills_dir: Option<String>,
    ) -> Self {
        Self {
            workspace_dir,
            open_skills_enabled,
            open_skills_dir,
        }
    }
}

impl SkillSource for DiskSkillSource {
    fn load(&self) -> Vec<Skill> {
        crate::skills::load_skills_with_open_skills_settings(
            &self.workspace_dir,
            self.open_skills_enabled,
            self.open_skills_dir.as_deref(),
        )
    }
}

fn summarize_skill(skill: &Skill) -> Value {
    let location = skill
        .location
        .as_ref()
        .map(|p| p.to_string_lossy().into_owned());
    json!({
        "id": skill.name,
        "name": skill.name,
        "description": skill.description,
        "version": skill.version,
        "author": skill.author,
        "tags": skill.tags,
        "location": location,
        "tool_count": skill.tools.len(),
    })
}

fn find_skill<'a>(skills: &'a [Skill], id: &str) -> Option<&'a Skill> {
    skills
        .iter()
        .find(|s| s.name.eq_ignore_ascii_case(id.trim()))
}

// ── skills_list ──────────────────────────────────────────────────────────

/// List every skill visible to the daemon.
pub struct SkillsListTool {
    source: Arc<dyn SkillSource>,
}

impl SkillsListTool {
    pub fn new(source: Arc<dyn SkillSource>) -> Self {
        Self { source }
    }
}

#[async_trait]
impl Tool for SkillsListTool {
    fn name(&self) -> &str {
        "skills_list"
    }

    fn description(&self) -> &str {
        "List all Construct skills available to the local daemon. Returns a JSON array of { id, name, description, version, tags, tool_count, location } objects."
    }

    fn parameters_schema(&self) -> Value {
        json!({ "type": "object", "properties": {}, "additionalProperties": false })
    }

    async fn execute(&self, _args: Value) -> anyhow::Result<ToolResult> {
        let skills = self.source.load();
        let payload: Vec<Value> = skills.iter().map(summarize_skill).collect();
        Ok(ToolResult {
            success: true,
            output: serde_json::to_string_pretty(&payload)?,
            error: None,
        })
    }
}

// ── skills_describe ──────────────────────────────────────────────────────

/// Return the full markdown/manifest body of a single skill.
pub struct SkillsDescribeTool {
    source: Arc<dyn SkillSource>,
}

impl SkillsDescribeTool {
    pub fn new(source: Arc<dyn SkillSource>) -> Self {
        Self { source }
    }
}

#[async_trait]
impl Tool for SkillsDescribeTool {
    fn name(&self) -> &str {
        "skills_describe"
    }

    fn description(&self) -> &str {
        "Return the full body (markdown / SKILL.toml) plus metadata for a single Construct skill by id."
    }

    fn parameters_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "skill_id": {
                    "type": "string",
                    "description": "Exact skill name/id as returned by skills_list."
                }
            },
            "required": ["skill_id"]
        })
    }

    async fn execute(&self, args: Value) -> anyhow::Result<ToolResult> {
        let id = args
            .get("skill_id")
            .and_then(Value::as_str)
            .map(str::trim)
            .filter(|s| !s.is_empty());
        let Some(id) = id else {
            return Ok(ToolResult {
                success: false,
                output: String::new(),
                error: Some("skills_describe requires `skill_id`".into()),
            });
        };

        let skills = self.source.load();
        let Some(skill) = find_skill(&skills, id) else {
            let mut names: Vec<&str> = skills.iter().map(|s| s.name.as_str()).collect();
            names.sort_unstable();
            return Ok(ToolResult {
                success: false,
                output: String::new(),
                error: Some(format!(
                    "Unknown skill '{id}'. Available: {}",
                    if names.is_empty() {
                        "none".into()
                    } else {
                        names.join(", ")
                    }
                )),
            });
        };

        let body = if let Some(loc) = &skill.location {
            tokio::fs::read_to_string(loc).await.unwrap_or_default()
        } else {
            String::new()
        };

        let payload = json!({
            "id": skill.name,
            "description": skill.description,
            "version": skill.version,
            "author": skill.author,
            "tags": skill.tags,
            "tools": skill.tools.iter().map(|t| json!({
                "name": t.name,
                "description": t.description,
                "kind": t.kind,
            })).collect::<Vec<_>>(),
            "body": body,
            "location": skill.location.as_ref().map(|p| p.to_string_lossy().into_owned()),
        });

        Ok(ToolResult {
            success: true,
            output: serde_json::to_string_pretty(&payload)?,
            error: None,
        })
    }
}

// ── skills_execute ───────────────────────────────────────────────────────

/// Executor abstraction — produced by tests to capture calls.
#[async_trait]
pub trait SkillExecutor: Send + Sync {
    async fn run(
        &self,
        skill: &Skill,
        sub_tool: Option<&str>,
        arguments: Value,
    ) -> anyhow::Result<ToolResult>;
}

/// Default executor: delegates to the same `SkillShellTool` / `SkillHttpTool`
/// wrappers the agent loop uses. For markdown-only skills (no `[[tools]]`),
/// returns the skill body so the calling model can follow the instructions.
pub struct DefaultSkillExecutor {
    security: Arc<SecurityPolicy>,
}

impl DefaultSkillExecutor {
    pub fn new(security: Arc<SecurityPolicy>) -> Self {
        Self { security }
    }
}

#[async_trait]
impl SkillExecutor for DefaultSkillExecutor {
    async fn run(
        &self,
        skill: &Skill,
        sub_tool: Option<&str>,
        arguments: Value,
    ) -> anyhow::Result<ToolResult> {
        // Markdown-only skill: return body verbatim.
        if skill.tools.is_empty() {
            let body = if let Some(loc) = &skill.location {
                tokio::fs::read_to_string(loc).await.unwrap_or_default()
            } else {
                String::new()
            };
            return Ok(ToolResult {
                success: true,
                output: body,
                error: None,
            });
        }

        // Select the sub-tool: explicit arg wins; otherwise the first entry.
        let tool = if let Some(name) = sub_tool {
            skill.tools.iter().find(|t| t.name == name)
        } else {
            skill.tools.first()
        };

        let Some(tool) = tool else {
            return Ok(ToolResult {
                success: false,
                output: String::new(),
                error: Some(format!(
                    "Skill '{}' has no [[tools]] entry matching '{}'",
                    skill.name,
                    sub_tool.unwrap_or("(first)")
                )),
            });
        };

        match tool.kind.as_str() {
            "shell" | "script" => {
                let t = crate::tools::skill_tool::SkillShellTool::new(
                    &skill.name,
                    tool,
                    self.security.clone(),
                );
                t.execute(arguments).await
            }
            "http" => {
                let t = crate::tools::skill_http::SkillHttpTool::new(&skill.name, tool);
                t.execute(arguments).await
            }
            other => Ok(ToolResult {
                success: false,
                output: String::new(),
                error: Some(format!("Unsupported skill tool kind: {other}")),
            }),
        }
    }
}

/// MCP tool: `skills_execute`.
pub struct SkillsExecuteTool {
    source: Arc<dyn SkillSource>,
    executor: Arc<dyn SkillExecutor>,
}

impl SkillsExecuteTool {
    pub fn new(source: Arc<dyn SkillSource>, executor: Arc<dyn SkillExecutor>) -> Self {
        Self { source, executor }
    }
}

#[async_trait]
impl Tool for SkillsExecuteTool {
    fn name(&self) -> &str {
        "skills_execute"
    }

    fn description(&self) -> &str {
        "Execute a Construct skill by id. For markdown skills this returns the skill body; for skills with [[tools]] entries it invokes the named sub-tool (or the first one) with the supplied `arguments` object."
    }

    fn parameters_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "skill_id": { "type": "string", "description": "Skill id/name." },
                "tool": { "type": "string", "description": "Optional sub-tool name within the skill's [[tools]]." },
                "arguments": { "type": "object", "description": "Arguments for the skill sub-tool." }
            },
            "required": ["skill_id"]
        })
    }

    async fn execute(&self, args: Value) -> anyhow::Result<ToolResult> {
        let id = args
            .get("skill_id")
            .and_then(Value::as_str)
            .map(str::trim)
            .filter(|s| !s.is_empty());
        let Some(id) = id else {
            return Ok(ToolResult {
                success: false,
                output: String::new(),
                error: Some("skills_execute requires `skill_id`".into()),
            });
        };
        let sub = args.get("tool").and_then(Value::as_str);
        let arguments = args.get("arguments").cloned().unwrap_or_else(|| json!({}));

        let skills = self.source.load();
        let Some(skill) = find_skill(&skills, id) else {
            return Ok(ToolResult {
                success: false,
                output: String::new(),
                error: Some(format!("Unknown skill '{id}'")),
            });
        };

        self.executor.run(skill, sub, arguments).await
    }
}

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

    struct StaticSource(Vec<Skill>);
    impl SkillSource for StaticSource {
        fn load(&self) -> Vec<Skill> {
            self.0.clone()
        }
    }

    fn skill(name: &str) -> Skill {
        Skill {
            name: name.to_string(),
            description: format!("desc-{name}"),
            version: "0.1.0".into(),
            author: None,
            tags: vec!["t1".into()],
            tools: vec![],
            prompts: vec![],
            location: None,
        }
    }

    #[tokio::test]
    async fn skills_list_returns_store_contents() {
        let source: Arc<dyn SkillSource> =
            Arc::new(StaticSource(vec![skill("alpha"), skill("beta")]));
        let tool = SkillsListTool::new(source);
        let res = tool.execute(json!({})).await.unwrap();
        assert!(res.success);
        let v: Value = serde_json::from_str(&res.output).unwrap();
        let arr = v.as_array().unwrap();
        assert_eq!(arr.len(), 2);
        assert_eq!(arr[0]["id"], "alpha");
        assert_eq!(arr[1]["id"], "beta");
    }

    #[tokio::test]
    async fn skills_list_empty_returns_empty_array() {
        let source: Arc<dyn SkillSource> = Arc::new(StaticSource(vec![]));
        let tool = SkillsListTool::new(source);
        let res = tool.execute(json!({})).await.unwrap();
        assert!(res.success);
        assert_eq!(res.output.trim(), "[]");
    }

    #[tokio::test]
    async fn skills_describe_unknown_skill_errors_with_available_list() {
        let source: Arc<dyn SkillSource> = Arc::new(StaticSource(vec![skill("alpha")]));
        let tool = SkillsDescribeTool::new(source);
        let res = tool.execute(json!({ "skill_id": "zeta" })).await.unwrap();
        assert!(!res.success);
        assert!(res.error.as_deref().unwrap().contains("alpha"));
    }

    struct RecordingExecutor {
        calls: Mutex<Vec<(String, Option<String>, Value)>>,
        response: String,
    }
    #[async_trait]
    impl SkillExecutor for RecordingExecutor {
        async fn run(
            &self,
            skill: &Skill,
            sub: Option<&str>,
            arguments: Value,
        ) -> anyhow::Result<ToolResult> {
            self.calls.lock().unwrap().push((
                skill.name.clone(),
                sub.map(str::to_string),
                arguments,
            ));
            Ok(ToolResult {
                success: true,
                output: self.response.clone(),
                error: None,
            })
        }
    }

    #[tokio::test]
    async fn skills_execute_dispatches_to_executor_with_arguments() {
        let source: Arc<dyn SkillSource> = Arc::new(StaticSource(vec![skill("deploy")]));
        let exec = Arc::new(RecordingExecutor {
            calls: Mutex::new(Vec::new()),
            response: "shipped!".into(),
        });
        let tool = SkillsExecuteTool::new(source, exec.clone());
        let res = tool
            .execute(json!({
                "skill_id": "deploy",
                "tool": "run",
                "arguments": { "env": "prod" }
            }))
            .await
            .unwrap();
        assert!(res.success);
        assert_eq!(res.output, "shipped!");
        let calls = exec.calls.lock().unwrap();
        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0].0, "deploy");
        assert_eq!(calls[0].1.as_deref(), Some("run"));
        assert_eq!(calls[0].2["env"], "prod");
    }

    #[tokio::test]
    async fn skills_execute_markdown_skill_returns_body() {
        let tmp = tempfile::TempDir::new().unwrap();
        let skill_path = tmp.path().join("DEPLOY.md");
        std::fs::write(&skill_path, "# Deploy\nmarkdown body").unwrap();
        let mut s = skill("deploy");
        s.location = Some(skill_path);
        let source: Arc<dyn SkillSource> = Arc::new(StaticSource(vec![s]));
        let executor = Arc::new(DefaultSkillExecutor::new(Arc::new(
            SecurityPolicy::default(),
        )));
        let tool = SkillsExecuteTool::new(source, executor);
        let res = tool.execute(json!({ "skill_id": "deploy" })).await.unwrap();
        assert!(res.success);
        assert!(res.output.contains("markdown body"));
    }

    #[tokio::test]
    async fn disk_skill_source_reads_from_workspace_skills_dir() {
        let tmp = tempfile::TempDir::new().unwrap();
        let skill_dir = tmp.path().join("skills/widget");
        std::fs::create_dir_all(&skill_dir).unwrap();
        std::fs::write(skill_dir.join("SKILL.md"), "# Widget\nbody").unwrap();
        let source = DiskSkillSource::new(tmp.path().to_path_buf(), false, None);
        let loaded = source.load();
        assert!(loaded.iter().any(|s| s.name == "widget"));
    }

    // Suppress unused-import warnings in non-test builds.
    #[allow(dead_code)]
    fn _path_ref() -> PathBuf {
        PathBuf::new()
    }
}