cortex-skills 0.1.0

Round-trip compiler between readable SKILL.md Markdown workflows and typed process graphs.
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
use std::collections::{BTreeMap, HashMap, HashSet};

use cortex_domain::{
    EdgeKind, GRAPH_SCHEMA_VERSION, GraphDocument, GraphEdge, GraphNode, NodeKind, Position,
};
use serde_json::Value;

use crate::SkillError;
use crate::import_helpers::{
    dependency_numbers, kind_annotation, provenance, semantic_config, slug, source_stem,
    strip_annotations, unquote,
};

#[derive(Default)]
struct Frontmatter {
    name: Option<String>,
    description: Option<String>,
    extra: BTreeMap<String, String>,
}

#[derive(Clone, Copy)]
enum ListMarker {
    Numbered(u32),
    Checklist(bool),
    Bullet,
}

#[derive(Clone, Copy)]
struct ListItem<'a> {
    marker: ListMarker,
    text: &'a str,
    indent: usize,
}

struct Builder<'a> {
    source: &'a str,
    nodes: Vec<GraphNode>,
    edges: Vec<GraphEdge>,
    headings: Vec<(usize, String)>,
    steps: Vec<String>,
    dependency_hints: Vec<(String, Vec<usize>)>,
    order: usize,
    edge_count: usize,
}

/// Compile a Markdown skill into a validated, provenance-bearing graph.
pub fn import_skill_markdown(source: &str, markdown: &str) -> Result<GraphDocument, SkillError> {
    // A UTF-8 BOM is invisible and common in files written on Windows. Left
    // in place it makes the first line `\u{feff}---` instead of `---`, so the
    // frontmatter block is not recognised at all and the document silently
    // imports with no name, description, or licence — found by importing a
    // real library, not by reading the code.
    let markdown = markdown.strip_prefix('\u{feff}').unwrap_or(markdown);
    let (frontmatter, body, body_line) = split_frontmatter(markdown)?;
    let first_heading = body.lines().find_map(parse_heading).map(|(_, text)| text);
    let name = frontmatter
        .name
        .clone()
        .or(first_heading.map(str::to_owned))
        .unwrap_or_else(|| source_stem(source));
    let description = frontmatter.description.clone().unwrap_or_default();
    let provenance = provenance(source, 1, markdown);
    let mut root_config = HashMap::new();
    root_config.insert("role".to_owned(), Value::String("skill".to_owned()));
    root_config.insert("order".to_owned(), Value::from(0));

    let mut builder = Builder {
        source,
        nodes: vec![GraphNode {
            id: "skill".to_owned(),
            kind: NodeKind::Skill,
            label: name.clone(),
            description: description.clone(),
            position: Position { x: 0.0, y: 0.0 },
            execution: None,
            provenance: vec![provenance],
            config: root_config,
        }],
        edges: Vec::new(),
        headings: Vec::new(),
        steps: Vec::new(),
        dependency_hints: Vec::new(),
        order: 0,
        edge_count: 0,
    };
    builder.parse_body(body, body_line, &name);
    builder.add_dependencies();

    let mut metadata = HashMap::from([
        ("compiler".to_owned(), "cortex-skills".to_owned()),
        ("description".to_owned(), description),
        ("source".to_owned(), source.to_owned()),
    ]);
    for (key, value) in frontmatter.extra {
        metadata.insert(format!("frontmatter.{key}"), value);
    }
    let graph = GraphDocument {
        schema_version: GRAPH_SCHEMA_VERSION.to_owned(),
        id: format!("{}-skill", slug(&name)),
        name,
        revision: 0,
        nodes: builder.nodes,
        edges: builder.edges,
        metadata,
    };
    graph.validate()?;
    Ok(graph)
}

impl Builder<'_> {
    fn parse_body(&mut self, body: &str, first_line: usize, skill_name: &str) {
        let lines: Vec<&str> = body.lines().collect();
        let mut index = 0;
        let mut skipped_title = false;
        while index < lines.len() {
            let line = lines[index];
            let source_line = first_line + index;
            if line.trim().is_empty() {
                index += 1;
                continue;
            }
            if let Some((level, text)) = parse_heading(line) {
                if !skipped_title && level == 1 && text == crate::heading_text(skill_name) {
                    skipped_title = true;
                } else if !text.trim().is_empty() {
                    // An empty heading carries no workflow meaning. Skipping
                    // it keeps a stray `##` from failing the whole import on
                    // the empty-label invariant.
                    self.add_heading(level, text, source_line);
                }
                index += 1;
                continue;
            }
            if let Some(item) = parse_list_item(line) {
                // Likewise for a stray `- ` or `1. ` with no text: it is
                // valid Markdown and must not abort the document.
                if !item.text.trim().is_empty() {
                    self.add_step(item, source_line);
                }
                index += 1;
                continue;
            }
            if line.trim_start().starts_with("```") {
                let start = index;
                index += 1;
                while index < lines.len() {
                    let closed = lines[index].trim_start().starts_with("```");
                    index += 1;
                    if closed {
                        break;
                    }
                }
                self.add_guidance(&lines[start..index].join("\n"), source_line);
                continue;
            }
            let start = index;
            index += 1;
            while index < lines.len()
                && !lines[index].trim().is_empty()
                && parse_heading(lines[index]).is_none()
                && parse_list_item(lines[index]).is_none()
                && !lines[index].trim_start().starts_with("```")
            {
                index += 1;
            }
            self.add_guidance(&lines[start..index].join("\n"), source_line);
        }
    }

    fn add_heading(&mut self, level: usize, text: &str, line: usize) {
        while self.headings.last().is_some_and(|(seen, _)| *seen >= level) {
            self.headings.pop();
        }
        self.order += 1;
        let id = format!("section-{}", self.order);
        let mut config = semantic_config("section", self.order, line);
        config.insert("headingLevel".to_owned(), Value::from(level));
        self.add_node(&id, NodeKind::Skill, text, "", level, line, config);
        self.link_parent(&id);
        self.headings.push((level, id));
    }

    fn add_step(&mut self, item: ListItem<'_>, line: usize) {
        self.order += 1;
        let step_number = self.steps.len() + 1;
        let id = format!("step-{step_number}");
        let mut config = semantic_config("workflow_step", self.order, line);
        config.insert("indent".to_owned(), Value::from(item.indent));
        // A step is deterministic work unless the author says otherwise.
        // Inferring "this sentence sounds like an approval" from prose would
        // put a guess where the whole project puts an explicit decision, so
        // the kind is annotated or it is not there.
        let kind = kind_annotation(item.text).unwrap_or(NodeKind::Deterministic);
        if kind != NodeKind::Deterministic {
            config.insert(
                "declaredKind".to_owned(),
                Value::String(kind.as_str().to_owned()),
            );
        }
        match item.marker {
            ListMarker::Numbered(number) => {
                config.insert("marker".to_owned(), Value::String("numbered".to_owned()));
                config.insert("listNumber".to_owned(), Value::from(number));
            }
            ListMarker::Checklist(checked) => {
                config.insert("marker".to_owned(), Value::String("checklist".to_owned()));
                config.insert("checked".to_owned(), Value::Bool(checked));
            }
            ListMarker::Bullet => {
                config.insert("marker".to_owned(), Value::String("bullet".to_owned()));
            }
        }
        let depth = self.headings.last().map_or(1, |(level, _)| level + 1);
        // Annotations are structure, not prose: they belong in the graph, not
        // in a label a human has to read around.
        let label = strip_annotations(item.text);
        self.add_node(&id, kind, &label, "", depth, line, config);
        self.link_parent(&id);
        if let Some(previous) = self.steps.last().cloned() {
            self.add_edge(previous, id.clone(), EdgeKind::Sequence, "next step", None);
        }
        self.dependency_hints
            .push((id.clone(), dependency_numbers(item.text)));
        self.steps.push(id);
    }

    fn add_guidance(&mut self, text: &str, line: usize) {
        self.order += 1;
        let id = format!("guidance-{}", self.order);
        let config = semantic_config("guidance", self.order, line);
        let depth = self.headings.last().map_or(1, |(level, _)| level + 1);
        self.add_node(
            &id,
            NodeKind::Skill,
            &guidance_label(text),
            text,
            depth,
            line,
            config,
        );
        self.link_parent(&id);
    }

    #[allow(clippy::too_many_arguments)]
    fn add_node(
        &mut self,
        id: &str,
        kind: NodeKind,
        label: &str,
        description: &str,
        depth: usize,
        line: usize,
        config: HashMap<String, Value>,
    ) {
        self.nodes.push(GraphNode {
            id: id.to_owned(),
            kind,
            label: label.trim().to_owned(),
            description: description.trim().to_owned(),
            position: Position {
                x: Self::coordinate(depth, 220.0),
                y: Self::coordinate(self.order, 110.0),
            },
            execution: None,
            provenance: vec![provenance(self.source, line, label)],
            config,
        });
    }

    fn coordinate(index: usize, step: f64) -> f64 {
        f64::from(u32::try_from(index).unwrap_or(u32::MAX)) * step
    }

    fn link_parent(&mut self, child: &str) {
        let parent = self.headings.last().map_or("skill", |(_, id)| id.as_str());
        self.add_edge(
            parent.to_owned(),
            child.to_owned(),
            EdgeKind::Context,
            "contains",
            None,
        );
    }

    fn add_dependencies(&mut self) {
        let mut seen = HashSet::new();
        for (target, numbers) in self.dependency_hints.clone() {
            for number in numbers {
                let Some(source) = self.steps.get(number.saturating_sub(1)).cloned() else {
                    continue;
                };
                if source != target && seen.insert((source.clone(), target.clone())) {
                    self.add_edge(
                        source,
                        target.clone(),
                        EdgeKind::Conditional,
                        "explicit dependency",
                        Some(format!("depends on step {number}")),
                    );
                }
            }
        }
    }

    fn add_edge(
        &mut self,
        from: String,
        to: String,
        kind: EdgeKind,
        label: &str,
        condition: Option<String>,
    ) {
        self.edge_count += 1;
        self.edges.push(GraphEdge {
            id: format!("edge-{}", self.edge_count),
            from,
            to,
            kind,
            label: label.to_owned(),
            condition,
        });
    }
}

fn split_frontmatter(markdown: &str) -> Result<(Frontmatter, &str, usize), SkillError> {
    if markdown
        .lines()
        .next()
        .is_none_or(|line| line.trim() != "---")
    {
        return Ok((Frontmatter::default(), markdown, 1));
    }
    let mut frontmatter = Frontmatter::default();
    let mut line_number = 1;
    let mut closed = false;
    let mut consumed = 0;
    for segment in markdown.split_inclusive('\n') {
        let line = segment.trim_end_matches(['\r', '\n']);
        consumed += segment.len();
        if line_number == 1 {
            line_number += 1;
            continue;
        }
        if line.trim() == "---" {
            closed = true;
            break;
        }
        let trimmed = line.trim();
        if trimmed.is_empty() || trimmed.starts_with('#') {
            line_number += 1;
            continue;
        }
        let (key, value) = trimmed.split_once(':').ok_or_else(|| {
            SkillError::InvalidFrontmatter(format!("expected key: value at line {line_number}"))
        })?;
        let key = key.trim().to_ascii_lowercase();
        if key.is_empty() {
            return Err(SkillError::InvalidFrontmatter(format!(
                "empty key at line {line_number}"
            )));
        }
        let value = unquote(value.trim());
        match key.as_str() {
            "name" => frontmatter.name = Some(value),
            "description" => frontmatter.description = Some(value),
            _ => {
                frontmatter.extra.insert(key, value);
            }
        }
        line_number += 1;
    }
    if !closed {
        return Err(SkillError::InvalidFrontmatter(
            "opening delimiter has no closing delimiter".to_owned(),
        ));
    }
    Ok((frontmatter, &markdown[consumed..], line_number + 1))
}

fn parse_heading(line: &str) -> Option<(usize, &str)> {
    let trimmed = line.trim_start();
    let level = trimmed.bytes().take_while(|byte| *byte == b'#').count();
    if !(1..=6).contains(&level) || trimmed.as_bytes().get(level) != Some(&b' ') {
        return None;
    }
    Some((level, trimmed[level + 1..].trim()))
}

fn parse_list_item(line: &str) -> Option<ListItem<'_>> {
    let trimmed = line.trim_start();
    let indent = line.len() - trimmed.len();
    for (prefix, checked) in [("- [ ] ", false), ("- [x] ", true), ("- [X] ", true)] {
        if let Some(text) = trimmed.strip_prefix(prefix) {
            return Some(ListItem {
                marker: ListMarker::Checklist(checked),
                text: text.trim(),
                indent,
            });
        }
    }
    for prefix in ["- ", "* ", "+ "] {
        if let Some(text) = trimmed.strip_prefix(prefix) {
            return Some(ListItem {
                marker: ListMarker::Bullet,
                text: text.trim(),
                indent,
            });
        }
    }
    let digits = trimmed.bytes().take_while(u8::is_ascii_digit).count();
    if digits == 0 {
        return None;
    }
    let delimiter = trimmed.as_bytes().get(digits)?;
    if !matches!(delimiter, b'.' | b')') || trimmed.as_bytes().get(digits + 1) != Some(&b' ') {
        return None;
    }
    let number = trimmed[..digits].parse().ok()?;
    Some(ListItem {
        marker: ListMarker::Numbered(number),
        text: trimmed[digits + 2..].trim(),
        indent,
    })
}

/// A readable title for a guidance block.
///
/// The first line of a fenced block is the fence itself, so using it made
/// nodes on the canvas read `` ```text `` — three backticks and a language
/// tag, telling a reader nothing. The first line with content is what the
/// block is about.
fn guidance_label(text: &str) -> String {
    let joined = text
        .lines()
        .map(str::trim)
        .filter(|line| !line.is_empty() && !line.starts_with("```"))
        .collect::<Vec<_>>()
        .join(" ");
    if joined.is_empty() {
        return "Guidance".to_owned();
    }
    // Prose wraps across source lines, so the first *line* cuts a sentence in
    // half. The first sentence is what the block is about.
    let sentence = joined
        .find(". ")
        .map_or(joined.as_str(), |end| &joined[..=end]);
    let trimmed = sentence.trim();
    if trimmed.chars().count() <= MAX_GUIDANCE_LABEL_CHARS {
        return trimmed.to_owned();
    }
    trimmed
        .char_indices()
        .take(MAX_GUIDANCE_LABEL_CHARS)
        .filter(|(_, character)| character.is_whitespace())
        .last()
        .map_or_else(
            || trimmed.chars().take(MAX_GUIDANCE_LABEL_CHARS).collect(),
            |(at, _)| trimmed[..at].to_owned(),
        )
}

/// A guidance title longer than this stops being a title. The full text stays
/// in the node description either way.
const MAX_GUIDANCE_LABEL_CHARS: usize = 88;