mur-common 2.92.0

Shared types and traits for the MUR ecosystem
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
use super::*;

#[test]
fn visibility_defaults_to_indexed_and_parses_on_demand() {
    let yaml = r#"
name: vis-default
version: 0.1.0
publisher: human:test
description: test
category: workflow
content:
  abstract: test
"#;
    let m: SkillManifest = serde_yaml_ng::from_str(yaml).unwrap();
    assert_eq!(m.visibility, Visibility::Indexed);

    let yaml2 = format!("{yaml}visibility: on_demand\n");
    let m2: SkillManifest = serde_yaml_ng::from_str(&yaml2).unwrap();
    assert_eq!(m2.visibility, Visibility::OnDemand);

    // Default is omitted on serialize (keeps existing manifests signature-stable).
    let out = serde_yaml_ng::to_string(&m).unwrap();
    assert!(!out.contains("visibility"));
    let out2 = serde_yaml_ng::to_string(&m2).unwrap();
    assert!(out2.contains("visibility: on_demand"));
}

#[test]
fn procedure_step_dag_fields_roundtrip() {
    let yaml = r#"
description: deploy the app
command: "fly deploy --app {{app_name}}"
id: deploy
depends_on: [build, test]
on_failure: retry
retry:
  max_retries: 2
  backoff_secs: 5
timeout_secs: 300
needs_approval: true
"#;
    let step: ProcedureStep = serde_yaml_ng::from_str(yaml).unwrap();
    assert_eq!(step.id.as_deref(), Some("deploy"));
    assert_eq!(step.depends_on, vec!["build", "test"]);
    assert_eq!(step.on_failure, FailureAction::Retry);
    assert_eq!(step.retry.as_ref().unwrap().max_retries, 2);
    assert_eq!(step.timeout_secs, Some(300));
    assert!(step.needs_approval);

    // Legacy step without any DAG fields parses with defaults.
    let legacy: ProcedureStep =
        serde_yaml_ng::from_str("description: run tests\ntool: Bash\n").unwrap();
    assert!(legacy.id.is_none());
    assert!(legacy.depends_on.is_empty());
    assert_eq!(legacy.on_failure, FailureAction::Abort);
    assert!(!legacy.needs_approval);
}

#[test]
fn procedure_step_parses_delegate_to() {
    let yaml = "description: hand off to qa\ndelegate_to: qa\n";
    let s: ProcedureStep = serde_yaml_ng::from_str(yaml).unwrap();
    assert_eq!(s.delegate_to.as_deref(), Some("qa"));
    // Absent → None (every existing skill.yaml still parses).
    let s2: ProcedureStep = serde_yaml_ng::from_str("description: local step\n").unwrap();
    assert_eq!(s2.delegate_to, None);
}

#[test]
fn variable_accepts_legacy_default_value_alias() {
    // Legacy workflow YAML used `default_value`; the unified type aliases it.
    let v: Variable =
        serde_yaml_ng::from_str("name: app\ntype: string\nrequired: true\ndefault_value: my-api\n")
            .unwrap();
    assert_eq!(v.default.as_deref(), Some("my-api"));
    assert_eq!(v.var_type, VarType::String);

    // Modern form `default:` parses too, and choices default empty.
    let v2: Variable = serde_yaml_ng::from_str("name: env\ntype: string\ndefault: prod\n").unwrap();
    assert_eq!(v2.default.as_deref(), Some("prod"));
    assert!(v2.choices.is_empty());
}

#[test]
fn variable_all_vartypes_parse() {
    for t in ["string", "path", "url", "number", "bool", "array"] {
        let v: Variable = serde_yaml_ng::from_str(&format!("name: x\ntype: {t}\n")).unwrap();
        assert_eq!(v.var_type.to_string(), t);
    }
}

#[test]
fn full_manifest_roundtrips() {
    let yaml = r#"
name: research-prices
version: 1.0.0
publisher: human:david
description: Search product prices
category: workflow
hosts: [mur-agent]
content:
  abstract: Searches product prices.
  procedure:
    variables:
      - name: product_name
        type: string
        required: true
    steps:
      - description: Navigate
        tool: browser.navigate
triggers:
  - type: command
    pattern: /research-prices
priority: normal
"#;
    let m: SkillManifest = serde_yaml_ng::from_str(yaml).unwrap();
    assert_eq!(m.name, "research-prices");
    assert_eq!(m.category, Category::Workflow);
    assert_eq!(m.content.mode(), Some(ContentMode::Workflow));
    let back = serde_yaml_ng::to_string(&m).unwrap();
    let m2: SkillManifest = serde_yaml_ng::from_str(&back).unwrap();
    assert_eq!(m2.name, m.name);
}

#[test]
fn context_mode_detected() {
    let c = Content {
        r#abstract: "a".into(),
        context: Some("ctx".into()),
        procedure: None,
        command: None,
        note: None,
    };
    assert_eq!(c.mode(), Some(ContentMode::Context));
}

#[test]
fn empty_content_returns_no_mode() {
    let c = Content {
        r#abstract: "a".into(),
        context: None,
        procedure: None,
        command: None,
        note: None,
    };
    assert_eq!(c.mode(), None);
}

#[test]
fn mode_returns_note_when_only_note_populated() {
    let c = Content {
        r#abstract: "a".into(),
        context: None,
        procedure: None,
        command: None,
        note: Some("# body".into()),
    };
    assert_eq!(c.mode(), Some(ContentMode::Note));
}

#[test]
fn mode_returns_none_when_note_and_context_both_populated() {
    let c = Content {
        r#abstract: "a".into(),
        context: Some("ctx".into()),
        procedure: None,
        command: None,
        note: Some("# body".into()),
    };
    assert_eq!(c.mode(), None);
}

#[test]
fn skill_without_evolution_log_defaults_to_empty() {
    // YAML without evolution_log field must parse and default to vec![].
    let yaml = r#"
name: no-evol
version: 0.1.0
publisher: human:test
description: test
category: workflow
content:
  abstract: test
"#;
    let m: SkillManifest = serde_yaml_ng::from_str(yaml).unwrap();
    assert!(m.evolution_log.is_empty());
}

#[test]
fn skill_with_evolution_log_roundtrips() {
    let yaml = r#"
name: with-evol
version: 0.1.0
publisher: human:test
description: test
category: workflow
content:
  abstract: test
evolution_log:
  - version: "0.1.0"
    generation: 0
    source: "human:test"
    changes: "Initial"
    timestamp: "2026-01-01T00:00:00Z"
"#;
    let m: SkillManifest = serde_yaml_ng::from_str(yaml).unwrap();
    assert_eq!(m.evolution_log.len(), 1);
    assert_eq!(m.evolution_log[0].version, "0.1.0");
    // Round-trip.
    let back = serde_yaml_ng::to_string(&m).unwrap();
    let m2: SkillManifest = serde_yaml_ng::from_str(&back).unwrap();
    assert_eq!(m2.evolution_log.len(), 1);
    assert_eq!(m2.evolution_log[0].generation, 0);
}

#[test]
fn exact_keyword_returns_pattern_for_keyword_triggers() {
    let t = Trigger {
        kind: TriggerKind::Keyword,
        pattern: Some("search".into()),
    };
    assert_eq!(t.exact_keyword(), Some("search"));
}

#[test]
fn exact_keyword_returns_none_for_non_keyword_triggers() {
    let t = Trigger {
        kind: TriggerKind::Command,
        pattern: Some("run".into()),
    };
    assert_eq!(t.exact_keyword(), None);

    let t = Trigger {
        kind: TriggerKind::SessionStart,
        pattern: None,
    };
    assert_eq!(t.exact_keyword(), None);

    let t = Trigger {
        kind: TriggerKind::Manual,
        pattern: None,
    };
    assert_eq!(t.exact_keyword(), None);
}

#[test]
fn exact_keyword_returns_none_when_pattern_is_none() {
    let t = Trigger {
        kind: TriggerKind::Keyword,
        pattern: None,
    };
    assert_eq!(t.exact_keyword(), None);
}

#[test]
fn skill_scope_serde_and_default() {
    // Default is User.
    assert_eq!(SkillScope::default(), SkillScope::User);
    assert!(SkillScope::User.is_user());
    assert!(!SkillScope::Project.is_user());
    assert!(!SkillScope::Fleet.is_user());

    // Serde: lowercase in YAML.
    let yaml = r#"
name: scoped-skill
version: 0.1.0
publisher: human:test
description: test
category: workflow
scope: fleet
fleet: prod
project: null
content:
  abstract: test
"#;
    let m: SkillManifest = serde_yaml_ng::from_str(yaml).unwrap();
    assert_eq!(m.scope, SkillScope::Fleet);
    assert_eq!(m.fleet, Some("prod".into()));
    assert_eq!(m.project, None);

    // Round-trip preserves scope.
    let back = serde_yaml_ng::to_string(&m).unwrap();
    let m2: SkillManifest = serde_yaml_ng::from_str(&back).unwrap();
    assert_eq!(m2.scope, SkillScope::Fleet);
    assert_eq!(m2.fleet, Some("prod".into()));

    // Missing scope defaults to User.
    let yaml_no_scope = r#"
name: default-scope
version: 0.1.0
publisher: human:test
description: test
category: workflow
content:
  abstract: test
"#;
    let m3: SkillManifest = serde_yaml_ng::from_str(yaml_no_scope).unwrap();
    assert_eq!(m3.scope, SkillScope::User);
    assert!(m3.fleet.is_none());
    assert!(m3.project.is_none());
}

#[test]
fn scope_visible_matrix() {
    // user + enterprise always visible
    assert!(scope_visible(
        SkillScope::User,
        None,
        None,
        None,
        None,
        None,
        None
    ));
    assert!(scope_visible(
        SkillScope::Enterprise,
        None,
        None,
        None,
        None,
        None,
        None
    ));
    // fleet skill visible only when active fleet matches
    assert!(scope_visible(
        SkillScope::Fleet,
        Some("dev"),
        None,
        None,
        Some("dev"),
        None,
        None
    ));
    assert!(!scope_visible(
        SkillScope::Fleet,
        Some("dev"),
        None,
        None,
        Some("ops"),
        None,
        None
    ));
    assert!(!scope_visible(
        SkillScope::Fleet,
        Some("dev"),
        None,
        None,
        None,
        None,
        None
    ));
    // project skill visible only when active project matches
    assert!(scope_visible(
        SkillScope::Project,
        None,
        Some("/p"),
        None,
        None,
        Some("/p"),
        None
    ));
    assert!(!scope_visible(
        SkillScope::Project,
        None,
        Some("/p"),
        None,
        None,
        Some("/q"),
        None
    ));
}

#[test]
fn team_scope_visibility() {
    // matches when active_team == skill_team
    assert!(scope_visible(
        SkillScope::Team,
        None,
        None,
        Some("org-xyz"),
        None,
        None,
        Some("org-xyz"),
    ));
    // mismatch → false
    assert!(!scope_visible(
        SkillScope::Team,
        None,
        None,
        Some("org-abc"),
        None,
        None,
        Some("org-xyz"),
    ));
    // no active_team → fail-closed
    assert!(!scope_visible(
        SkillScope::Team,
        None,
        None,
        Some("org-xyz"),
        None,
        None,
        None,
    ));
    // no skill_team selector → never injects (None == None guard)
    assert!(!scope_visible(
        SkillScope::Team,
        None,
        None,
        None,
        None,
        None,
        Some("org-xyz"),
    ));
}

#[test]
fn governance_ref_roundtrip() {
    let yaml = "name: t\nversion: 1.0.0\npublisher: human:test\ndescription: t\ncategory: workflow\ncontent:\n  abstract: t\ngovernance:\n  org_id: org-1\n  constitution_hash: abc\n";
    let m: SkillManifest = serde_yaml_ng::from_str(yaml).unwrap();
    let g = m.governance.unwrap();
    assert_eq!(g.org_id, "org-1");
    assert_eq!(g.constitution_hash, "abc");
}

#[test]
fn governance_ref_absent_is_none() {
    let m: SkillManifest = serde_yaml_ng::from_str("name: t\nversion: 1.0.0\npublisher: human:test\ndescription: t\ncategory: workflow\ncontent:\n  abstract: t\n").unwrap();
    assert!(m.governance.is_none());
}

#[test]
fn team_field_roundtrip() {
    let yaml = "name: t\nversion: 1.0.0\npublisher: human:test\ndescription: t\ncategory: workflow\ncontent:\n  abstract: t\nscope: team\nteam: org-1\n";
    let m: SkillManifest = serde_yaml_ng::from_str(yaml).unwrap();
    assert_eq!(m.scope, SkillScope::Team);
    assert_eq!(m.team.as_deref(), Some("org-1"));
}