rhei-cli 0.3.0

Command-line driver for the Rhei agent runtime.
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
    use super::*;
    use rhei_core::parse;
    use std::fs;

    fn sample_machine() -> StateMachine {
        let yaml = r#"
name: test-sm
version: 1.0
states:
  pending: { description: "not started" }
  in-progress: { description: "doing" }
  completed: { description: "done", final: true }
"#;
        StateMachine::from_yaml_str(yaml).expect("states load")
    }

    #[test]
    fn loads_state_machine_with_models_and_state_selectors() {
        let yaml = r#"
name: multi-model
version: 1.0
models:
  - gpt-5
  - claude-sonnet
states:
  draft:
    description: planned
    visits: 2
    all_models:
      - gpt-5
      - claude-sonnet
  review:
    description: reviewed
    model: claude-sonnet
  done:
    description: done
    final: true
"#;

        let machine = StateMachine::from_yaml_str(yaml).expect("states load");
        assert_eq!(machine.models, vec!["gpt-5", "claude-sonnet"]);
        assert_eq!(machine.states["draft"].visits, Some(2));
        assert_eq!(machine.states["draft"].all_models, vec!["gpt-5", "claude-sonnet"]);
        assert_eq!(machine.states["review"].model.as_deref(), Some("claude-sonnet"));
    }

    #[test]
    fn rejects_state_machine_with_unknown_state_model() {
        let yaml = r#"
name: multi-model
version: 1.0
models:
  - gpt-5
states:
  draft:
    description: planned
    model: claude-sonnet
"#;

        let err = StateMachine::from_yaml_str(yaml).expect_err("should reject unknown model");
        assert!(err.to_string().contains("references unknown model 'claude-sonnet'"));
    }

    #[test]
    fn rejects_state_machine_with_conflicting_state_model_selectors() {
        let yaml = r#"
name: multi-model
version: 1.0
models:
  - gpt-5
states:
  draft:
    description: planned
    all_models:
      - gpt-5
    model: gpt-5
"#;

        let err =
            StateMachine::from_yaml_str(yaml).expect_err("should reject conflicting selectors");
        assert!(err.to_string().contains("cannot set both 'all_models' and 'model'"));
    }

    #[test]
    fn rejects_state_machine_with_unknown_all_models_entry() {
        let yaml = r#"
name: multi-model
version: 1.0
models:
  - gpt-5
states:
  draft:
    description: planned
    all_models:
      - claude-sonnet
"#;

        let err =
            StateMachine::from_yaml_str(yaml).expect_err("should reject unknown all_models entry");
        assert!(err
            .to_string()
            .contains("references unknown model 'claude-sonnet' in 'all_models'"));
    }

    #[test]
    fn parses_execution_target_with_mode_and_provider() {
        let target = parse_execution_target("claude-code[yolo]:anthropic:claude-opus-4-7")
            .expect("target should parse");

        assert_eq!(target.agent, "claude-code");
        assert_eq!(target.mode.as_deref(), Some("yolo"));
        assert_eq!(target.provider.as_deref(), Some("anthropic"));
        assert_eq!(target.model, "claude-opus-4-7");
        assert_eq!(target.slug(), "claude-code-yolo-anthropic-claude-opus-4-7");
    }

    #[test]
    fn loads_state_machine_with_target_selectors() {
        let yaml = r#"
name: multi-target
version: 1.0
states:
  analyze:
    description: analyze
    all_targets:
      - claude-code[yolo]:anthropic:claude-opus-4-7
      - codex[yolo]:openai:gpt-5-codex
  done:
    description: done
    final: true
"#;

        let machine = StateMachine::from_yaml_str(yaml).expect("states load");
        assert_eq!(machine.states["analyze"].all_targets.len(), 2);
        assert_eq!(
            machine.states["analyze"].all_targets[0],
            "claude-code[yolo]:anthropic:claude-opus-4-7"
        );
    }

    #[test]
    fn rejects_explicit_empty_all_targets() {
        let yaml = r#"
name: multi-target
version: 1.0
states:
  analyze:
    description: analyze
    all_targets: []
  done:
    description: done
    final: true
"#;

        let err = StateMachine::from_yaml_str(yaml).expect_err("empty all_targets rejected");
        assert!(err.to_string().contains("all_targets must be a non-empty list"));
    }

    #[test]
    fn rejects_state_machine_with_conflicting_target_and_model_selectors() {
        let yaml = r#"
name: multi-target
version: 1.0
models:
  - gpt-5
states:
  analyze:
    description: analyze
    target: codex[yolo]:openai:gpt-5-codex
    model: gpt-5
"#;

        let err =
            StateMachine::from_yaml_str(yaml).expect_err("should reject conflicting selectors");
        assert!(err.to_string().contains("cannot combine 'target' or 'all_targets'"));
    }

    #[test]
    fn rejects_state_machine_with_zero_visits() {
        let yaml = r#"
name: multi-model
version: 1.0
states:
  draft:
    description: planned
    visits: 0
"#;

        let err = StateMachine::from_yaml_str(yaml).expect_err("should reject zero visits");
        assert!(err.to_string().contains("visits: 0"));
    }

    #[test]
    fn loads_state_machine_with_artifact_contracts() {
        let yaml = r#"
name: artifacts
version: 1.0
states:
  review:
    description: review work
    inputs:
      - name: implementation
        path: runtime/results/{task_id}.md
        format: markdown
    outputs:
      - name: findings
        path: runtime/findings/{task_id}.md
        description: Review findings
  done:
    description: done
    final: true
"#;

        let machine = StateMachine::from_yaml_str(yaml).expect("states load");
        assert_eq!(machine.states["review"].inputs.len(), 1);
        assert_eq!(machine.states["review"].outputs.len(), 1);
        assert_eq!(machine.states["review"].outputs[0].name, "findings");
    }

    #[test]
    fn rejects_duplicate_artifact_names_in_same_state_field() {
        let yaml = r#"
name: artifacts
version: 1.0
states:
  review:
    description: review work
    outputs:
      - name: findings
        path: runtime/findings/a.md
      - name: findings
        path: runtime/findings/b.md
"#;

        let err = StateMachine::from_yaml_str(yaml).expect_err("should reject duplicate names");
        assert!(err.to_string().contains("duplicate artifact name 'findings'"));
    }

    #[test]
    fn rejects_absolute_artifact_paths() {
        let yaml = r#"
name: artifacts
version: 1.0
states:
  review:
    description: review work
    outputs:
      - name: findings
        path: /tmp/findings.md
"#;

        let err = StateMachine::from_yaml_str(yaml).expect_err("should reject absolute path");
        assert!(err.to_string().contains("must use a relative path"));
    }

    #[test]
    fn rejects_if_condition_referencing_undeclared_input() {
        let yaml = r#"
name: test
version: 1.0
states:
  implement:
    description: do work
    instructions: |
      {if input.typo.exists}
      Read the notes.
      {endif}
    inputs:
      - name: notes
        path: runtime/notes/{task_id}.md
        optional: true
"#;
        let err =
            StateMachine::from_yaml_str(yaml).expect_err("should reject unknown input reference");
        assert!(err.to_string().contains("'typo' is not a declared input"));
    }

    #[test]
    fn rejects_if_condition_with_unsupported_form() {
        let yaml = r#"
name: test
version: 1.0
states:
  implement:
    description: do work
    instructions: |
      {if meta.flag}
      Extra instructions.
      {endif}
"#;
        let err =
            StateMachine::from_yaml_str(yaml).expect_err("should reject unsupported condition");
        assert!(err.to_string().contains("not a recognised condition"));
    }

    #[test]
    fn accepts_if_condition_referencing_declared_input() {
        let yaml = r#"
name: test
version: 1.0
states:
  implement:
    description: do work
    instructions: |
      {if input.notes.exists}
      Read the notes.
      {endif}
    inputs:
      - name: notes
        path: runtime/notes/{task_id}.md
        optional: true
  done:
    description: done
    final: true
"#;
        StateMachine::from_yaml_str(yaml).expect("valid condition should load");
    }

    #[test]
    fn rejects_nested_template_conditionals() {
        let yaml = r#"
name: test
version: 1.0
states:
  implement:
    description: do work
    instructions: |
      {if input.notes.exists}
      Read the notes.
      {if mcp.grafana.available}
      Check Grafana.
      {endif}
      {endif}
    inputs:
      - name: notes
        path: runtime/notes/{task_id}.md
        optional: true
    mcp_servers:
      - grafana
  done:
    description: done
    final: true
"#;
        let err = StateMachine::from_yaml_str(yaml).expect_err("nested conditionals rejected");
        assert!(err.to_string().contains("nested conditional blocks"));
    }

    #[test]
    fn accepts_if_condition_in_personality_referencing_declared_input() {
        let yaml = r#"
name: test
version: 1.0
states:
  implement:
    description: do work
    personality: |
      {if input.context.exists}
      Use context from {input.context.path}.
      {endif}
    inputs:
      - name: context
        path: runtime/context/{task_id}.md
        optional: true
  done:
    description: done
    final: true
"#;
        StateMachine::from_yaml_str(yaml).expect("valid condition in personality should load");
    }

    #[test]
    fn rejects_artifact_paths_that_escape_workspace_root() {
        let yaml = r#"
name: artifacts
version: 1.0
states:
  review:
    description: review work
    outputs:
      - name: findings
        path: ../../outside.md
"#;

        let err = StateMachine::from_yaml_str(yaml).expect_err("should reject escaping path");
        assert!(err.to_string().contains("escapes the workspace root"));
    }

    #[test]
    fn detects_missing_state_and_bad_dependency_and_cycle() {
        let input = r#"# Rhei: Example
## Tasks

### Task 1: A
**State:** pending
**Prior:** Task 3

#### Task 1.1: s
**State:** pending

### Task 2: B
**State:** invalid_state

### Task 3: C
**State:** pending
**Prior:** Task 1
"#;
        let rhei = parse(input).expect("parse ok");
        let sm = sample_machine();
        let report = validate_with_machine(&rhei, &sm);

        assert!(report.has_errors());
        // Expect: Task 1 depends on 3 (exists), Task 3 depends on 1 => cycle
        // Also: Task 2 has invalid state
        let joined = report.errors.join("\n");
        assert!(joined.contains("invalid state"));
        assert!(joined.contains("Circular dependency detected"));
    }

    #[test]
    fn ok_when_valid() {
        let input = r#"# Rhei: Example
## Tasks

### Task 1: A
**State:** pending

#### Task 1.1: s
**State:** pending

### Task 2: B
**State:** in-progress
**Prior:** Task 1
"#;
        let rhei = parse(input).expect("parse ok");
        let sm = sample_machine();
        let report = validate_with_machine(&rhei, &sm);

        assert!(!report.has_errors(), "unexpected errors: {:?}", report.errors);
    }

    #[test]
    fn warns_when_gating_state_declares_agent() {
        let input = r#"# Rhei: Example
## Tasks

### Task 1: Review
**State:** review
"#;
        let yaml = r#"
name: gate-agent
version: 1.0
states:
  review:
    description: review
    gating: true
    agent: codex
  done:
    description: done
    final: true
"#;
        let rhei = parse(input).expect("parse ok");
        let machine = StateMachine::from_yaml_str(yaml).expect("states load");
        let report = validate_with_machine(&rhei, &machine);
        assert!(report.errors.is_empty(), "warning should not be fatal: {report:?}");
        assert!(
            report.warnings.iter().any(|warning| warning.contains("gating state")),
            "expected gating-agent warning, got {report:?}"
        );
    }

    #[test]
    fn accepts_counted_state_suffix_within_budget() {
        let input = r#"# Rhei: Example
## Tasks

### Task 1: A
**State:** pending-2
"#;
        let rhei = parse(input).expect("parse ok");
        let machine = StateMachine::from_yaml_str(
            r#"
name: example
version: 1.0
states:
  pending:
    description: queued
    visits: 3
  done:
    description: done
    final: true
"#,
        )
        .expect("states load");

        let report = validate_with_machine(&rhei, &machine);
        assert!(!report.has_errors(), "unexpected errors: {:?}", report.errors);
    }

    #[test]
    fn rejects_counted_state_suffix_of_one() {
        let input = r#"# Rhei: Example
## Tasks

### Task 1: A
**State:** pending-1
"#;
        let rhei = parse(input).expect("parse ok");
        let machine = StateMachine::from_yaml_str(
            r#"
name: example
version: 1.0
states:
  pending:
    description: queued
    visits: 3
  done:
    description: done
    final: true
"#,
        )
        .expect("states load");

        let report = validate_with_machine(&rhei, &machine);
        assert!(report.has_errors(), "expected counted suffix validation error");
        assert!(
            report.errors.iter().any(|err| err.contains("Visit suffix '-1' is not allowed")),
            "unexpected errors: {:?}",
            report.errors
        );
    }

    #[test]
    fn rejects_counted_state_suffix_when_state_has_no_visits() {
        let input = r#"# Rhei: Example
## Tasks

### Task 1: A
**State:** pending-2
"#;
        let rhei = parse(input).expect("parse ok");
        let machine = sample_machine();

        let report = validate_with_machine(&rhei, &machine);
        assert!(report.has_errors(), "expected counted suffix validation error");
        assert!(
            report.errors.iter().any(|err| err.contains("does not declare 'visits'")),
            "unexpected errors: {:?}",
            report.errors
        );
    }

    #[test]
    fn reports_missing_numeric_dependency() {
        let input = r#"# Rhei: Example
## Tasks

### Task 1: A
**State:** pending
**Prior:** Task 9

### Task 2: B
**State:** in-progress
"#;
        let rhei = parse(input).expect("parse ok");
        let sm = sample_machine();
        let report = validate_with_machine(&rhei, &sm);

        assert!(report.has_errors(), "expected missing dependency error");
        let joined = report.errors.join("\n");
        assert!(
            joined.contains("Task 1 depends on missing Task 9"),
            "did not find expected message; got:\n{}",
            joined
        );
    }