eidos-ekf 0.1.0

EKF package contract support for Eidos.
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
use saphyr::{MarkedYaml, Scalar, ScalarStyle, YamlData, YamlLoader};
use saphyr_parser::Parser;
use std::collections::BTreeMap;
use std::path::Path;

use crate::model::*;
use crate::paths::{malformed, set_source_list, unknown_field};

pub fn parse_manifest_file(path: &Path) -> Result<(Manifest, Vec<EkfDiagnostic>), String> {
    let text = std::fs::read_to_string(path)
        .map_err(|err| format!("failed to read EKF manifest '{}': {err}", path.display()))?;
    Ok(parse_manifest(&text))
}

pub fn parse_manifest(text: &str) -> (Manifest, Vec<EkfDiagnostic>) {
    let mut manifest = Manifest::default();
    let mut diagnostics = Vec::new();

    // Scalars are kept in raw representation form so manifest values retain their source
    // spelling ("0.96" stays "0.96"); the scanner still resolves quoting and escapes.
    let mut parser = Parser::new_from_str(text);
    let mut loader = YamlLoader::<MarkedYaml>::default();
    loader.early_parse(false);
    if let Err(err) = parser.load(&mut loader, true) {
        diagnostics.push(EkfDiagnostic {
            severity: DiagnosticSeverity::Warning,
            kind: "yaml_error".into(),
            message: format!("EKF manifest is not valid YAML: {err}"),
            source: Some(format!("line {}", err.marker().line())),
        });
        return (manifest, diagnostics);
    }

    let Some(root) = loader.into_documents().into_iter().next() else {
        return (manifest, diagnostics);
    };
    match &root.data {
        YamlData::Mapping(entries) => {
            for (key_node, value) in entries {
                map_root_entry(&mut manifest, key_node, value, &mut diagnostics);
            }
        }
        _ if is_missing_value(&root) => {}
        _ => diagnostics.push(EkfDiagnostic {
            severity: DiagnosticSeverity::Warning,
            kind: "yaml_error".into(),
            message: "EKF manifest root must be a mapping".into(),
            source: Some(format!("line {}", node_line(&root))),
        }),
    }
    (manifest, diagnostics)
}

pub(crate) fn map_root_entry(
    manifest: &mut Manifest,
    key_node: &MarkedYaml,
    value: &MarkedYaml,
    diagnostics: &mut Vec<EkfDiagnostic>,
) {
    let line = node_line(key_node);
    let Some(key) = scalar_string(key_node) else {
        diagnostics.push(malformed(line, "mapping keys must be scalars"));
        return;
    };
    match key.as_str() {
        "ekf_version" => manifest.ekf_version = scalar_value(value, "ekf_version", diagnostics),
        "name" => manifest.name = scalar_value(value, "name", diagnostics),
        "partitions" => map_partitions(value, manifest, diagnostics),
        "sources" => map_sources(value, manifest, diagnostics),
        "trust" => map_trust(value, manifest, diagnostics),
        "gates" => map_scalar_groups(
            value,
            &mut manifest.gates,
            "gates",
            "unsupported_inline_gate",
            "gate sections must be block mappings in EKF v0.1",
            diagnostics,
        ),
        "relations" => map_scalar_groups(
            value,
            &mut manifest.relations,
            "relations",
            "unsupported_inline_relation",
            "relation profiles must be block mappings in EKF v0.1",
            diagnostics,
        ),
        other => diagnostics.push(unknown_field(line, other)),
    }
}

pub(crate) fn map_partitions(
    node: &MarkedYaml,
    manifest: &mut Manifest,
    diagnostics: &mut Vec<EkfDiagnostic>,
) {
    let items = match &node.data {
        YamlData::Sequence(items) => items,
        _ if is_missing_value(node) => return,
        _ => {
            diagnostics.push(malformed(node_line(node), "partitions must be a list"));
            return;
        }
    };
    for item in items {
        let YamlData::Mapping(fields) = &item.data else {
            diagnostics.push(malformed(
                node_line(item),
                "partition entries must be mappings",
            ));
            continue;
        };
        let mut partition = PartitionManifest::default();
        for (key_node, value) in fields {
            let line = node_line(key_node);
            let Some(key) = scalar_string(key_node) else {
                diagnostics.push(malformed(line, "mapping keys must be scalars"));
                continue;
            };
            match key.as_str() {
                "id" => {
                    partition.id =
                        scalar_value(value, "partitions.id", diagnostics).unwrap_or_default();
                }
                "title" => partition.title = scalar_value(value, "partitions.title", diagnostics),
                "source" => {
                    partition.source =
                        scalar_value(value, "partitions.source", diagnostics).unwrap_or_default();
                }
                "roots" => partition.roots = string_list(value, "partitions.roots", diagnostics),
                "role" => partition.role = scalar_value(value, "partitions.role", diagnostics),
                "languages" => {
                    partition.languages = string_list(value, "partitions.languages", diagnostics);
                }
                "tags" => partition.tags = string_list(value, "partitions.tags", diagnostics),
                other => diagnostics.push(unknown_field(line, other)),
            }
        }
        if partition_has_content(&partition) {
            manifest.partitions.push(partition);
        }
    }
}

pub(crate) fn map_sources(
    node: &MarkedYaml,
    manifest: &mut Manifest,
    diagnostics: &mut Vec<EkfDiagnostic>,
) {
    let entries = match &node.data {
        YamlData::Mapping(entries) => entries,
        _ if is_missing_value(node) => return,
        _ => {
            diagnostics.push(malformed(node_line(node), "sources must be a mapping"));
            return;
        }
    };
    for (key_node, value) in entries {
        let line = node_line(key_node);
        let Some(key) = scalar_string(key_node) else {
            diagnostics.push(malformed(line, "mapping keys must be scalars"));
            continue;
        };
        match key.as_str() {
            "docs" | "skills" | "agents" | "capabilities" | "brief_profiles" | "workflows"
            | "evals" => {
                let values = string_list(value, &format!("sources.{key}"), diagnostics);
                set_source_list(&mut manifest.sources, &key, values);
            }
            "code" => map_code_sources(value, manifest, line, diagnostics),
            other => diagnostics.push(unknown_field(line, other)),
        }
    }
}

pub(crate) fn map_code_sources(
    node: &MarkedYaml,
    manifest: &mut Manifest,
    line: usize,
    diagnostics: &mut Vec<EkfDiagnostic>,
) {
    let items = match &node.data {
        YamlData::Sequence(items) => items,
        _ if is_missing_value(node) => return,
        _ => {
            diagnostics.push(EkfDiagnostic {
                severity: DiagnosticSeverity::Warning,
                kind: "unsupported_inline_code_sources".into(),
                message: "sources.code must be a list of code entries".into(),
                source: Some(format!("line {line}")),
            });
            return;
        }
    };
    for item in items {
        let YamlData::Mapping(fields) = &item.data else {
            diagnostics.push(malformed(
                node_line(item),
                "code source entries must be mappings",
            ));
            continue;
        };
        let mut code = CodeSource::default();
        for (key_node, value) in fields {
            let line = node_line(key_node);
            let Some(key) = scalar_string(key_node) else {
                diagnostics.push(malformed(line, "mapping keys must be scalars"));
                continue;
            };
            match key.as_str() {
                "path" => {
                    code.path =
                        scalar_value(value, "sources.code.path", diagnostics).unwrap_or_default();
                }
                "source" => code.source = scalar_value(value, "sources.code.source", diagnostics),
                "languages" => {
                    code.languages = string_list(value, "sources.code.languages", diagnostics);
                }
                other => diagnostics.push(unknown_field(line, other)),
            }
        }
        if code.path.trim().is_empty() {
            diagnostics.push(EkfDiagnostic {
                severity: DiagnosticSeverity::Warning,
                kind: "code_source_missing_path".into(),
                message: "code source entry is missing path".into(),
                source: Some(format!("line {}", node_line(item))),
            });
        } else {
            manifest.sources.code.push(code);
        }
    }
}

pub(crate) fn map_trust(
    node: &MarkedYaml,
    manifest: &mut Manifest,
    diagnostics: &mut Vec<EkfDiagnostic>,
) {
    let entries = match &node.data {
        YamlData::Mapping(entries) => entries,
        _ if is_missing_value(node) => return,
        _ => {
            diagnostics.push(malformed(node_line(node), "trust must be a mapping"));
            return;
        }
    };
    for (key_node, value) in entries {
        let line = node_line(key_node);
        let Some(key) = scalar_string(key_node) else {
            diagnostics.push(malformed(line, "mapping keys must be scalars"));
            continue;
        };
        match scalar_string(value) {
            Some(scalar) if !scalar.is_empty() => {
                manifest.trust.insert(key, scalar);
            }
            Some(_) => {}
            None => diagnostics.push(malformed(
                node_line(value),
                &format!("trust.{key} must be a scalar"),
            )),
        }
    }
}

/// Map a section of `group -> key -> scalar` mappings (gates and relation profiles).
pub(crate) fn map_scalar_groups(
    node: &MarkedYaml,
    target: &mut BTreeMap<String, BTreeMap<String, String>>,
    section: &str,
    inline_kind: &str,
    inline_message: &str,
    diagnostics: &mut Vec<EkfDiagnostic>,
) {
    let groups = match &node.data {
        YamlData::Mapping(groups) => groups,
        _ if is_missing_value(node) => return,
        _ => {
            diagnostics.push(malformed(
                node_line(node),
                &format!("{section} must be a mapping"),
            ));
            return;
        }
    };
    for (group_node, group_value) in groups {
        let line = node_line(group_node);
        let Some(group) = scalar_string(group_node) else {
            diagnostics.push(malformed(line, "mapping keys must be scalars"));
            continue;
        };
        if is_missing_value(group_value) {
            target.entry(group).or_default();
            continue;
        }
        let YamlData::Mapping(entries) = &group_value.data else {
            diagnostics.push(EkfDiagnostic {
                severity: DiagnosticSeverity::Warning,
                kind: inline_kind.into(),
                message: inline_message.into(),
                source: Some(format!("line {}", node_line(group_value))),
            });
            continue;
        };
        let group_map = target.entry(group).or_default();
        for (key_node, value) in entries {
            let line = node_line(key_node);
            let Some(key) = scalar_string(key_node) else {
                diagnostics.push(malformed(line, "mapping keys must be scalars"));
                continue;
            };
            match group_entry_string(value) {
                Some(scalar) => {
                    group_map.insert(key, scalar);
                }
                None => diagnostics.push(malformed(
                    node_line(value),
                    &format!("{section}.{key} must be a scalar or a list of scalars"),
                )),
            }
        }
    }
}

/// Group entry values stay strings in the manifest; lists are kept in their inline `[a, b]`
/// form so downstream consumers (`parse_inline_list`) split them back apart.
pub(crate) fn group_entry_string(node: &MarkedYaml) -> Option<String> {
    if let YamlData::Sequence(items) = &node.data {
        let items: Option<Vec<String>> = items.iter().map(scalar_string).collect();
        return items.map(|items| format!("[{}]", items.join(", ")));
    }
    scalar_string(node)
}

pub(crate) fn node_line(node: &MarkedYaml) -> usize {
    node.span.start.line()
}

/// A scalar's string form: the raw source text for representation nodes (quotes already resolved
/// by the scanner), or a minimal rendering for early-parsed values.
pub(crate) fn scalar_string(node: &MarkedYaml) -> Option<String> {
    match &node.data {
        YamlData::Representation(text, _, _) => Some(text.to_string()),
        YamlData::Value(scalar) => Some(match scalar {
            Scalar::Null => String::new(),
            Scalar::Boolean(value) => value.to_string(),
            Scalar::Integer(value) => value.to_string(),
            Scalar::FloatingPoint(value) => value.to_string(),
            Scalar::String(value) => value.to_string(),
        }),
        YamlData::Tagged(_, inner) => scalar_string(inner),
        _ => None,
    }
}

/// Whether the node stands for an absent value (`key:` with nothing after it).
pub(crate) fn is_missing_value(node: &MarkedYaml) -> bool {
    match &node.data {
        YamlData::Representation(text, ScalarStyle::Plain, _) => text.is_empty(),
        YamlData::Value(Scalar::Null) | YamlData::BadValue => true,
        _ => false,
    }
}

/// Scalar value for `field`; a non-scalar node becomes a diagnostic and `None`.
pub(crate) fn scalar_value(
    node: &MarkedYaml,
    field: &str,
    diagnostics: &mut Vec<EkfDiagnostic>,
) -> Option<String> {
    let value = scalar_string(node);
    if value.is_none() {
        diagnostics.push(malformed(
            node_line(node),
            &format!("{field} must be a scalar"),
        ));
    }
    value
}

/// String list for `field`: accepts a sequence (flow or block) of scalars or a bare scalar.
pub(crate) fn string_list(
    node: &MarkedYaml,
    field: &str,
    diagnostics: &mut Vec<EkfDiagnostic>,
) -> Vec<String> {
    match &node.data {
        YamlData::Sequence(items) => items
            .iter()
            .filter_map(|item| {
                let scalar = scalar_string(item);
                if scalar.is_none() {
                    diagnostics.push(malformed(
                        node_line(item),
                        &format!("{field} entries must be scalars"),
                    ));
                }
                scalar.filter(|value| !value.is_empty())
            })
            .collect(),
        _ if is_missing_value(node) => Vec::new(),
        _ => scalar_value(node, field, diagnostics)
            .into_iter()
            .filter(|value| !value.is_empty())
            .collect(),
    }
}

pub(crate) fn partition_has_content(partition: &PartitionManifest) -> bool {
    !partition.id.trim().is_empty()
        || !partition.source.trim().is_empty()
        || !partition.roots.is_empty()
        || partition.title.is_some()
        || partition.role.is_some()
        || !partition.languages.is_empty()
        || !partition.tags.is_empty()
}