rhei-cli 0.2.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
576
577
578
579
580
581
582
583
584
585
586
587
588
589

/// Load settings from a JSON file, returning defaults if the file doesn't exist.
#[cfg(test)]
fn load_settings(path: &Path) -> MietteResult<RheiSettings> {
    Ok(load_settings_document(path)?.typed)
}

fn json_field_present(raw: &serde_json::Value, key: &str) -> bool {
    raw.as_object().map(|obj| obj.contains_key(key)).unwrap_or(false)
}

fn json_child<'a>(raw: &'a serde_json::Value, key: &str) -> &'a serde_json::Value {
    raw.as_object().and_then(|obj| obj.get(key)).unwrap_or(&serde_json::Value::Null)
}

fn json_nested_field_present(raw: &serde_json::Value, section: &str, key: &str) -> bool {
    json_child(raw, section).as_object().map(|obj| obj.contains_key(key)).unwrap_or(false)
}

fn merge_model_agent_binding(
    existing: &mut ModelAgentBinding,
    project: ModelAgentBinding,
    project_raw: &serde_json::Value,
) {
    if json_field_present(project_raw, "args") {
        existing.args = project.args;
    }
    if json_field_present(project_raw, "autonomous_args") {
        existing.autonomous_args = project.autonomous_args;
    }
    if json_field_present(project_raw, "timeout") {
        existing.timeout = project.timeout;
    }
}

fn load_merged_settings_for_completion(plan_root: &Path) -> RheiSettings {
    // Shell completion must not fail because a project settings file is half-written.
    load_merged_settings(plan_root)
        .unwrap_or_else(|_| RheiSettings { agents: built_in_agents(), ..Default::default() })
}

const PROJECT_SETTINGS_RELATIVE_PATH: &str = ".agents/rhei/settings.json";

fn project_settings_path(plan_root: &Path) -> PathBuf {
    plan_root.join(PROJECT_SETTINGS_RELATIVE_PATH)
}

/// Load merged settings: built-ins, then global, then project-level overrides.
fn load_merged_settings(plan_root: &Path) -> MietteResult<RheiSettings> {
    let global = match home_dir() {
        Ok(home) => load_settings_document(&home.join(".config/rhei/settings.json"))?,
        Err(_) => empty_settings_document(),
    };

    // §FS-rhei-agents.1.1: project settings live under the agent config tree.
    let project = load_settings_document(&project_settings_path(plan_root))?;
    let project_raw = &project.raw;
    let global = global.typed;
    let project = project.typed;

    // Agent registry: built-ins seed the map; global then project entries
    // replace an id wholesale when present.
    let mut agents = built_in_agents();
    for (id, profile) in global.agents {
        agents.insert(id, profile);
    }
    for (id, profile) in project.agents {
        agents.insert(id, profile);
    }

    // Registries merge by id: start with global, override by project.
    let mut mcp_servers = global.mcp_servers.clone();
    for (id, profile) in project.mcp_servers {
        mcp_servers.insert(id, profile);
    }
    let mut skills = global.skills.clone();
    for (id, profile) in project.skills {
        skills.insert(id, profile);
    }
    // `models` merge by model id; within a matching id, `models.<id>.agents`
    // is deep-merged by agent id.
    // §FS-rhei-agents.1.3: Merge models by id and model-agent bindings by agent id.
    let mut models = global.models.clone();
    for (id, project_profile) in project.models {
        let project_model_raw = json_child(json_child(project_raw, "models"), &id);
        match models.get_mut(&id) {
            Some(existing) => {
                if json_field_present(project_model_raw, "provider") {
                    existing.provider = project_profile.provider;
                }
                if json_field_present(project_model_raw, "model") {
                    existing.model = project_profile.model;
                }
                if json_field_present(project_model_raw, "default_agent") {
                    existing.default_agent = project_profile.default_agent;
                }
                for (agent_id, binding) in project_profile.agents {
                    let project_binding_raw =
                        json_child(json_child(project_model_raw, "agents"), &agent_id);
                    match existing.agents.get_mut(&agent_id) {
                        Some(existing_binding) => merge_model_agent_binding(
                            existing_binding,
                            binding,
                            project_binding_raw,
                        ),
                        None => {
                            existing.agents.insert(agent_id, binding);
                        }
                    }
                }
            }
            None => {
                models.insert(id, project_profile);
            }
        }
    }

    // `defaults.mcp_servers` / `defaults.skills`: project replaces global
    // wholesale when present (including an explicit empty list).
    let defaults = SettingsDefaults {
        model: if json_nested_field_present(project_raw, "defaults", "model") {
            project.defaults.model
        } else {
            global.defaults.model
        },
        agent: if json_nested_field_present(project_raw, "defaults", "agent") {
            project.defaults.agent
        } else {
            global.defaults.agent
        },
        agent_mode: if json_nested_field_present(project_raw, "defaults", "agent_mode") {
            project.defaults.agent_mode
        } else {
            global.defaults.agent_mode
        },
        agent_timeout: if json_nested_field_present(project_raw, "defaults", "agent_timeout") {
            project.defaults.agent_timeout
        } else {
            global.defaults.agent_timeout
        },
        program_timeout: if json_nested_field_present(project_raw, "defaults", "program_timeout") {
            project.defaults.program_timeout
        } else {
            global.defaults.program_timeout
        },
        mcp_servers: if json_nested_field_present(project_raw, "defaults", "mcp_servers") {
            project.defaults.mcp_servers
        } else {
            global.defaults.mcp_servers
        },
        skills: if json_nested_field_present(project_raw, "defaults", "skills") {
            project.defaults.skills
        } else {
            global.defaults.skills
        },
    };

    Ok(RheiSettings {
        agent: if json_field_present(project_raw, "agent") { project.agent } else { global.agent },
        agent_mode: if json_field_present(project_raw, "agent_mode") {
            project.agent_mode
        } else {
            global.agent_mode
        },
        model: if json_field_present(project_raw, "model") { project.model } else { global.model },
        agent_timeout: if json_field_present(project_raw, "agent_timeout") {
            project.agent_timeout
        } else {
            global.agent_timeout
        },
        program_timeout: if json_field_present(project_raw, "program_timeout") {
            project.program_timeout
        } else {
            global.program_timeout
        },
        defaults,
        agents,
        models,
        mcp_servers,
        skills,
        snapshots: if json_field_present(project_raw, "snapshots") && project.snapshots.is_none() {
            None
        } else {
            merge_snapshot_settings(global.snapshots, project.snapshots)
        },
    })
}

/// The agents the merged registry knows, so an error does not leave an author
/// guessing at names nothing else lists. §FS-rhei-agents.1.1
fn known_agents_hint(settings: &RheiSettings) -> String {
    if settings.agents.is_empty() {
        return "no agents are configured; declare one under `agents` in .agents/rhei/settings.json"
            .to_string();
    }
    let names: Vec<&str> = settings.agents.keys().map(String::as_str).collect();
    format!("known agents: {}", names.join(", "))
}

/// The modes one agent declares, listed the way an invalid state lists its
/// allowed states. §FS-rhei-agents.1.1
fn known_modes_hint(profile: &CustomAgentProfile) -> String {
    if profile.modes.is_empty() {
        return "it declares no modes".to_string();
    }
    let modes: Vec<&str> = profile.modes.keys().map(String::as_str).collect();
    format!("known modes: {}", modes.join(", "))
}

fn validate_machine_settings_references(
    machine: &rhei_validator::StateMachine,
    settings: &RheiSettings,
) -> Vec<String> {
    let mut errors = Vec::new();

    // Agent registry self-validation: `command` is required, and
    // `mcp_flag` and `mcp_config_flag` are mutually exclusive per
    // §FS-rhei-agents.1.1.2: Validate agent transport profile settings.
    for (id, profile) in &settings.agents {
        if profile.command.is_empty() {
            errors.push(format!(
                "agent '{}' has an empty 'command'; the `command` field is required",
                id
            ));
        }
        if profile.mcp_flag.is_some() && profile.mcp_config_flag.is_some() {
            errors.push(format!(
                "agent '{}' declares both 'mcp_flag' and 'mcp_config_flag'; \
                 they are mutually exclusive",
                id
            ));
        }
    }

    // MCP server registry self-validation: exactly one of `command`/`url`;
    // §FS-rhei-agents.1.1.4: Validate MCP server registry entries.
    for (id, profile) in &settings.mcp_servers {
        match (profile.command.is_some(), profile.url.is_some()) {
            (false, false) => errors.push(format!(
                "mcp_servers.'{}' must declare exactly one of 'command' or 'url'",
                id
            )),
            (true, true) => errors.push(format!(
                "mcp_servers.'{}' declares both 'command' and 'url'; they are \
                 mutually exclusive",
                id
            )),
            (false, true) => {
                if profile.transport.as_deref().map_or(true, str::is_empty) {
                    errors.push(format!(
                        "mcp_servers.'{}' uses 'url' but does not declare 'transport'; \
                         set transport to 'sse' or 'websocket'",
                        id
                    ));
                }
            }
            (true, false) => {}
        }
    }

    // Model registry self-validation: `provider` and `model` are required
    // §FS-rhei-agents.1.1.3: Validate model profile registry entries.
    for (id, profile) in &settings.models {
        if profile.provider.as_deref().map_or(true, str::is_empty) {
            errors.push(format!("models.'{}' is missing required field 'provider'", id));
        }
        if profile.model.as_deref().map_or(true, str::is_empty) {
            errors.push(format!("models.'{}' is missing required field 'model'", id));
        }
    }

    validate_mcp_entries_known(
        "defaults.mcp_servers",
        settings.defaults.mcp_servers.as_deref(),
        &settings.mcp_servers,
        &mut errors,
    );
    validate_skill_entries_known(
        "defaults.skills",
        settings.defaults.skills.as_deref(),
        &settings.skills,
        &mut errors,
    );

    for (state_name, state) in &machine.states {
        validate_mcp_entries_known(
            &format!("state '{state_name}' mcp_servers"),
            state.mcp_servers.as_deref(),
            &settings.mcp_servers,
            &mut errors,
        );
        validate_skill_entries_known(
            &format!("state '{state_name}' skills"),
            state.skills.as_deref(),
            &settings.skills,
            &mut errors,
        );

        if let Some(agent) = state.agent.as_ref() {
            let Some(profile) = settings.agents.get(agent.id()) else {
                errors.push(format!(
                    "state '{}' references unknown agent '{}' ({})",
                    state_name,
                    agent.id(),
                    known_agents_hint(settings)
                ));
                continue;
            };
            if let Some(mode) = state.agent_mode.as_deref() {
                if !profile.modes.is_empty() && !profile.modes.contains_key(mode) {
                    errors.push(format!(
                        "state '{}' references unknown mode '{}' for agent '{}' ({})",
                        state_name,
                        mode,
                        agent.id(),
                        known_modes_hint(profile)
                    ));
                }
            }
        }

        let selectors = state
            .target
            .iter()
            .cloned()
            .chain(state.all_targets.iter().cloned())
            .collect::<Vec<_>>();
        for selector in selectors {
            match parse_execution_target(&selector) {
                Ok(target) => {
                    let Some(profile) = settings.agents.get(target.agent.as_str()) else {
                        errors.push(format!(
                            "state '{}' references unknown target agent '{}' in '{}' ({})",
                            state_name,
                            target.agent,
                            selector,
                            known_agents_hint(settings)
                        ));
                        continue;
                    };
                    if let Some(mode) = target.mode.as_deref() {
                        if !profile.modes.contains_key(mode) {
                            errors.push(format!(
                                "state '{}' references unknown target mode '{}' for agent '{}' in '{}' ({})",
                                state_name,
                                mode,
                                target.agent,
                                selector,
                                known_modes_hint(profile)
                            ));
                        }
                    }
                }
                Err(err) => errors.push(format!(
                    "state '{}' has invalid target selector '{}': {}",
                    state_name, selector, err
                )),
            }
        }

        if state.snapshot.as_ref().and_then(|snapshot| snapshot.emit.as_ref()).is_some()
            || state.snapshot.as_ref().and_then(|snapshot| snapshot.inherit.as_ref()).is_some()
        {
            // Settings-aware snapshot checks need the merged agent/model
            // registry, so they live in the CLI validation layer rather than
            // §FS-rhei-snapshots.9.2 §FS-rhei-snapshots.11: Registry-aware checks.
            match resolve_agent_invocations(machine, state_name, settings, &default_run_options()) {
                Ok(invocations) if invocations.is_empty() => {
                    errors.push(format!(
                        "state '{}' declares snapshot operations but no effective target tuple resolves (snapshot-requires-target)",
                        state_name
                    ));
                }
                Ok(invocations) => {
                    let mut seen_slugs: HashMap<String, String> = HashMap::new();
                    for invocation in &invocations {
                        let Some(slug) = resolved_agent_target_slug(invocation) else {
                            errors.push(format!(
                                "state '{}' declares snapshot operations but agent '{}' does not resolve provider and model (snapshot-requires-target)",
                                state_name,
                                invocation.agent.id()
                            ));
                            continue;
                        };
                        if let Some(previous) =
                            seen_slugs.insert(slug.clone(), invocation.agent.id().to_string())
                        {
                            errors.push(format!(
                                "state '{}' has multiple resolved invocations for agents '{}' and '{}' that normalize to snapshot target slug '{}'",
                                state_name,
                                previous,
                                invocation.agent.id(),
                                slug
                            ));
                        }
                        if state
                            .snapshot
                            .as_ref()
                            .and_then(|snapshot| snapshot.emit.as_ref())
                            .is_some()
                            && !profile_has_snapshot_layout(&invocation.profile.session)
                        {
                            errors.push(format!(
                                "state '{}' declares snapshot.emit but agent '{}' has no supported snapshot session layout (unsupported-snapshot-session)",
                                state_name,
                                invocation.agent.id()
                            ));
                        }
                        if state
                            .snapshot
                            .as_ref()
                            .and_then(|snapshot| snapshot.inherit.as_ref())
                            .is_some_and(|inherit| inherit.required == Some(true))
                            && !profile_has_snapshot_preload(&invocation.profile.session)
                        {
                            errors.push(format!(
                                "state '{}' declares required snapshot.inherit but agent '{}' has no supported snapshot preload strategy (unsupported-snapshot-session)",
                                state_name,
                                invocation.agent.id()
                            ));
                        }
                    }
                }
                Err(err) => errors.push(format!(
                    "state '{}' declares snapshot operations but no effective target tuple resolves: {} (snapshot-requires-target)",
                    state_name, err
                )),
            }
        }
    }

    errors
}

fn validate_task_execution_override_settings_references(
    rhei: &rhei_core::ast::Rhei,
    settings: &RheiSettings,
) -> Vec<String> {
    fn visit(task: &rhei_core::ast::Task, settings: &RheiSettings, errors: &mut Vec<String>) {
        if let Some(selector) = task.target.as_deref() {
            match parse_execution_target(selector) {
                Ok(target) => {
                    let Some(profile) = settings.agents.get(target.agent.as_str()) else {
                        errors.push(format!(
                            "Task {} references unknown target agent '{}' in **Target:** '{}' ({})",
                            task.id,
                            target.agent,
                            selector,
                            known_agents_hint(settings)
                        ));
                        return;
                    };
                    if let Some(mode) = target.mode.as_deref() {
                        if !profile.modes.contains_key(mode) {
                            errors.push(format!(
                                "Task {} references unknown target mode '{}' for agent '{}' in **Target:** '{}' ({})",
                                task.id,
                                mode,
                                target.agent,
                                selector,
                                known_modes_hint(profile)
                            ));
                        }
                    }
                }
                Err(_) => {
                    // Shape errors are reported by the semantic validator.
                }
            }
        }

        for child in &task.children {
            visit(child, settings, errors);
        }
    }

    // §FS-rhei-plan-language.3.11: Task `**Target:**` uses state target registry checks.
    let mut errors = Vec::new();
    for task in &rhei.tasks {
        visit(task, settings, &mut errors);
    }
    errors
}

fn validate_mcp_entries_known(
    label: &str,
    entries: Option<&[StateMcpEntry]>,
    registry: &BTreeMap<String, McpServerProfile>,
    errors: &mut Vec<String>,
) {
    for entry in entries.unwrap_or(&[]) {
        if !entry.is_inline() && !registry.contains_key(entry.id()) {
            errors.push(format!("{label} references unknown mcp server '{}'", entry.id()));
        }
    }
}

fn validate_skill_entries_known(
    label: &str,
    entries: Option<&[StateSkillEntry]>,
    registry: &BTreeMap<String, SkillProfile>,
    errors: &mut Vec<String>,
) {
    for entry in entries.unwrap_or(&[]) {
        if !entry.is_inline() && !registry.contains_key(entry.id()) {
            errors.push(format!("{label} references unknown skill '{}'", entry.id()));
        }
    }
}

fn validate_snapshot_plan_context(
    loaded: &LoadedPlan,
    machines: &ResolvedMachineSet,
) -> Vec<String> {
    let mut errors = Vec::new();
    for task in &loaded.rhei.tasks {
        let machine = machines.machine_for_task_str(&task.id.to_string());
        let state_name = normalized_state_name(task.state.as_str(), machine);
        if machine
            .states
            .get(&state_name)
            .and_then(|state| state.snapshot.as_ref())
            .and_then(|snapshot| snapshot.inherit.as_ref())
            .and_then(|inherit| inherit.from_axis.as_deref())
            == Some("ancestor")
        {
            errors.push(format!(
                "Task {} is a root task in state '{}' but that state declares snapshot.inherit.from: ancestor (snapshot root tasks have no ancestor)",
                task.id, state_name
            ));
        }
    }
    errors
}

fn snapshot_orphan_validation_warnings(
    workspace_root: &Path,
    loaded: &LoadedPlan,
    machines: &ResolvedMachineSet,
    settings: &RheiSettings,
) -> MietteResult<Vec<String>> {
    let cache_root = snapshot_cache_dir(settings, workspace_root);
    if !cache_root.exists() {
        return Ok(Vec::new());
    }
    let records = read_snapshot_records(&cache_root)?;
    let mut warnings = Vec::new();
    for record in records {
        // A snapshot belongs to one ticket; judge it under that ticket's
        // machine. §DA-per-rhei-state-machines
        let machine = machines.machine_for_task_str(&record.task_id);
        if snapshot_record_is_orphaned_for_loaded(&record, loaded, machine, settings) {
            warnings.push(format!(
                "snapshot {} is orphaned relative to the current plan/state machine",
                record.display_ref()
            ));
        }
    }
    Ok(warnings)
}

fn snapshot_record_is_orphaned_for_loaded(
    record: &SnapshotRecord,
    loaded: &LoadedPlan,
    machine: &rhei_validator::StateMachine,
    settings: &RheiSettings,
) -> bool {
    let task_exists =
        flatten_tasks(&loaded.rhei).into_iter().any(|task| task.id.to_string() == record.task_id);
    if !task_exists {
        return true;
    }
    if !machine.states.contains_key(&record.emitting_state) {
        return true;
    }
    let Ok(slugs) = effective_target_slugs_for_state(machine, &record.emitting_state, settings)
    else {
        return true;
    };
    slugs.is_empty() || !slugs.contains(&record.target_slug)
}

fn profile_has_snapshot_layout(session: &Option<serde_json::Value>) -> bool {
    session.as_ref().is_some_and(snapshot_emit_session_supported)
}

fn profile_has_snapshot_preload(session: &Option<serde_json::Value>) -> bool {
    session.as_ref().is_some_and(snapshot_preload_session_supported)
}