rho-coding-agent 1.23.0

A lightweight agent harness inspired by Pi
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
//! Draft mutation and validated save for user-defined agent files.
//!
//! Mutators live on [`AgentDefinition`] so the TUI editor does not re-encode
//! runtime-axis invariants. Save serializes, re-parses, and replaces the file
//! only when the on-disk contents still match the edit session baseline.

use std::path::Path;

use super::{
    parse_definition, parse_tools_list_text, serialize_definition, AgentDefinition, AgentRuntime,
    AgentRuntimeSpec, ClaudeAgentConfig, ClaudeToolPolicy, ModelPolicy, ModelSelection,
    PromptPolicy, ReasoningLevel, ToolCapability, ToolPolicy,
};

impl AgentDefinition {
    /// Switches prompt policy while preserving the existing body.
    pub(crate) fn set_prompt_policy_kind(&mut self, value: &str) -> bool {
        let body = match &self.prompt {
            PromptPolicy::Extend(text) | PromptPolicy::Replace(text) => text.clone(),
        };
        self.prompt = match value {
            "extend" => PromptPolicy::Extend(body),
            "replace" => PromptPolicy::Replace(body),
            _ => return false,
        };
        true
    }

    /// Replaces the prompt body while preserving extend/replace.
    pub(crate) fn set_prompt_body(&mut self, body: String) {
        self.prompt = match &self.prompt {
            PromptPolicy::Extend(_) => PromptPolicy::Extend(body),
            PromptPolicy::Replace(_) => PromptPolicy::Replace(body),
        };
    }

    /// Switches runtime, carrying compatible model/reasoning and resetting the rest.
    pub(crate) fn switch_runtime_kind(&mut self, value: &str) -> bool {
        let next = match value {
            "rho" => AgentRuntime::Rho,
            "claude-cli" => AgentRuntime::ClaudeCli,
            _ => return false,
        };
        if self.runtime.runtime() == next {
            return true;
        }
        let reasoning = self.reasoning();
        let model_policy = self.model_policy().into_owned();
        self.runtime = build_runtime_spec(next, &model_policy, reasoning);
        true
    }

    /// Applies a model-policy keyword for the current runtime.
    pub(crate) fn set_model_policy_kind(&mut self, value: &str) -> bool {
        let is_claude = self.runtime.runtime() == AgentRuntime::ClaudeCli;
        let policy = match value {
            "inherit" => {
                self.set_model_selection(None, None);
                ModelPolicy::Inherit
            }
            "prefer" if !is_claude => ModelPolicy::Prefer(self.current_selection()),
            "require" if !is_claude => ModelPolicy::Require(self.current_selection()),
            "select" => ModelPolicy::Select(self.current_selection()),
            _ => return false,
        };
        self.set_model_policy(policy);
        true
    }

    pub(crate) fn current_selection(&self) -> ModelSelection {
        match self.model_policy().as_ref() {
            ModelPolicy::Prefer(selection)
            | ModelPolicy::Require(selection)
            | ModelPolicy::Select(selection) => ModelSelection {
                provider: selection.provider.clone(),
                model: selection.model.clone(),
            },
            ModelPolicy::Inherit => ModelSelection {
                provider: None,
                model: String::new(),
            },
        }
    }

    pub(crate) fn set_reasoning_kind(&mut self, value: &str) -> bool {
        let level = if value == "inherit" {
            None
        } else {
            match value.parse::<ReasoningLevel>() {
                Ok(level) => Some(level),
                Err(_) => return false,
            }
        };
        self.set_reasoning(level);
        true
    }

    pub(crate) fn set_inherit_claude_config(&mut self, value: &str) -> bool {
        let inherit = match value {
            "yes" => true,
            "no" => false,
            _ => return false,
        };
        if let AgentRuntimeSpec::ClaudeCli(config) = &mut self.runtime {
            config.inherit_claude_config = inherit;
            true
        } else {
            false
        }
    }

    pub(crate) fn set_description_text(&mut self, value: String) {
        self.description = value;
    }

    pub(crate) fn set_model_text(&mut self, value: String) {
        let trimmed = value.trim().to_string();
        if self.runtime.runtime() == AgentRuntime::ClaudeCli {
            if let AgentRuntimeSpec::ClaudeCli(config) = &mut self.runtime {
                config.model = (!trimmed.is_empty()).then_some(trimmed);
            }
            return;
        }
        if trimmed.is_empty() {
            self.set_model_policy(ModelPolicy::Inherit);
            return;
        }
        let provider = match self.model_policy().as_ref() {
            ModelPolicy::Prefer(selection)
            | ModelPolicy::Require(selection)
            | ModelPolicy::Select(selection) => selection.provider.clone(),
            ModelPolicy::Inherit => None,
        };
        let policy = match self.model_policy().as_ref() {
            ModelPolicy::Prefer(_) => ModelPolicy::Prefer(ModelSelection {
                provider,
                model: trimmed,
            }),
            ModelPolicy::Require(_) => ModelPolicy::Require(ModelSelection {
                provider,
                model: trimmed,
            }),
            _ => ModelPolicy::Select(ModelSelection {
                provider,
                model: trimmed,
            }),
        };
        self.set_model_policy(policy);
    }

    pub(crate) fn set_provider_text(&mut self, value: String) {
        let trimmed = value.trim().to_string();
        let model = match self.model_policy().as_ref() {
            ModelPolicy::Prefer(selection)
            | ModelPolicy::Require(selection)
            | ModelPolicy::Select(selection) => selection.model.clone(),
            ModelPolicy::Inherit => return,
        };
        let provider = (!trimmed.is_empty()).then_some(trimmed);
        let policy = match self.model_policy().as_ref() {
            ModelPolicy::Prefer(_) => ModelPolicy::Prefer(ModelSelection { provider, model }),
            ModelPolicy::Require(_) => ModelPolicy::Require(ModelSelection { provider, model }),
            _ => ModelPolicy::Select(ModelSelection { provider, model }),
        };
        self.set_model_policy(policy);
    }

    pub(crate) fn set_model_selection(&mut self, provider: Option<String>, model: Option<String>) {
        if self.runtime.runtime() == AgentRuntime::ClaudeCli {
            if let AgentRuntimeSpec::ClaudeCli(config) = &mut self.runtime {
                config.model = model.filter(|value| !value.is_empty());
            }
            return;
        }
        let policy = match (self.model_policy().as_ref(), model) {
            (ModelPolicy::Prefer(_), Some(model)) if !model.is_empty() => {
                ModelPolicy::Prefer(ModelSelection { provider, model })
            }
            (ModelPolicy::Require(_), Some(model)) if !model.is_empty() => {
                ModelPolicy::Require(ModelSelection { provider, model })
            }
            (_, Some(model)) if !model.is_empty() => {
                ModelPolicy::Select(ModelSelection { provider, model })
            }
            _ => ModelPolicy::Inherit,
        };
        self.set_model_policy(policy);
    }

    pub(crate) fn set_model_policy(&mut self, policy: ModelPolicy) {
        match &mut self.runtime {
            AgentRuntimeSpec::Rho { model, .. } => *model = policy,
            AgentRuntimeSpec::ClaudeCli(config) => {
                config.model = match policy {
                    ModelPolicy::Inherit => None,
                    ModelPolicy::Prefer(selection)
                    | ModelPolicy::Require(selection)
                    | ModelPolicy::Select(selection) => Some(selection.model),
                };
            }
        }
    }

    pub(crate) fn set_reasoning(&mut self, level: Option<ReasoningLevel>) {
        let level = if self.runtime.runtime() == AgentRuntime::ClaudeCli {
            level.filter(|level| !matches!(level, ReasoningLevel::Off | ReasoningLevel::Minimal))
        } else {
            level
        };
        match &mut self.runtime {
            AgentRuntimeSpec::Rho { reasoning, .. } => *reasoning = level,
            AgentRuntimeSpec::ClaudeCli(config) => config.reasoning = level,
        }
    }

    /// Parses tools text (`all`, `[]`, or a bracket list) into the runtime policy.
    pub(crate) fn set_tools_text(&mut self, value: &str) -> Result<(), String> {
        let trimmed = value.trim();
        match &mut self.runtime {
            AgentRuntimeSpec::Rho { tools, .. } => {
                if trimmed == "all" {
                    *tools = ToolPolicy::All;
                    return Ok(());
                }
                let names = parse_tools_list_text(trimmed)?;
                let mut capabilities = std::collections::BTreeSet::new();
                for name in names {
                    capabilities.insert(ToolCapability::parse(name));
                }
                *tools = ToolPolicy::Allow(capabilities);
                Ok(())
            }
            AgentRuntimeSpec::ClaudeCli(config) => {
                let names = parse_tools_list_text(trimmed)?;
                config.tools = if names.is_empty() {
                    ClaudeToolPolicy::None
                } else {
                    ClaudeToolPolicy::Allow(names)
                };
                Ok(())
            }
        }
    }

    pub(crate) fn tools_text(&self) -> String {
        match &self.runtime {
            AgentRuntimeSpec::Rho {
                tools: ToolPolicy::All,
                ..
            } => "all".into(),
            AgentRuntimeSpec::Rho {
                tools: ToolPolicy::Allow(tools),
                ..
            } => format!(
                "[{}]",
                tools
                    .iter()
                    .map(ToString::to_string)
                    .collect::<Vec<_>>()
                    .join(", ")
            ),
            AgentRuntimeSpec::ClaudeCli(config) => match &config.tools {
                ClaudeToolPolicy::None => "[]".into(),
                ClaudeToolPolicy::Allow(tools) => format!("[{}]", tools.join(", ")),
            },
        }
    }

    pub(crate) fn model_text(&self) -> String {
        match self.model_policy().as_ref() {
            ModelPolicy::Inherit => String::new(),
            ModelPolicy::Prefer(selection)
            | ModelPolicy::Require(selection)
            | ModelPolicy::Select(selection) => selection.model.clone(),
        }
    }

    pub(crate) fn provider_text(&self) -> String {
        match self.model_policy().as_ref() {
            ModelPolicy::Prefer(selection)
            | ModelPolicy::Require(selection)
            | ModelPolicy::Select(selection) => selection.provider.clone().unwrap_or_default(),
            ModelPolicy::Inherit => String::new(),
        }
    }

    pub(crate) fn tools_badge(&self) -> String {
        match &self.runtime {
            AgentRuntimeSpec::Rho {
                tools: ToolPolicy::All,
                ..
            } => "all".into(),
            AgentRuntimeSpec::Rho {
                tools: ToolPolicy::Allow(tools),
                ..
            } if tools.is_empty() => "none".into(),
            AgentRuntimeSpec::Rho {
                tools: ToolPolicy::Allow(tools),
                ..
            } => tools
                .iter()
                .map(ToString::to_string)
                .collect::<Vec<_>>()
                .join(", "),
            AgentRuntimeSpec::ClaudeCli(config) => {
                let tools = config.tools.as_slice();
                if tools.is_empty() {
                    "none".into()
                } else {
                    tools.join(", ")
                }
            }
        }
    }

    pub(crate) fn model_badge(&self) -> String {
        match self.model_policy().as_ref() {
            ModelPolicy::Inherit => "inherit".into(),
            ModelPolicy::Prefer(selection) => format!("prefer {}", selection.model),
            ModelPolicy::Require(selection) => format!("require {}", selection.model),
            ModelPolicy::Select(selection) => selection.model.clone(),
        }
    }

    pub(crate) fn model_policy_badge(&self) -> String {
        match self.model_policy().as_ref() {
            ModelPolicy::Inherit => "inherit".into(),
            ModelPolicy::Prefer(_) => "prefer".into(),
            ModelPolicy::Require(_) => "require".into(),
            ModelPolicy::Select(_) => "select".into(),
        }
    }

    /// Friendly pre-checks for constraints the parser also enforces.
    pub(crate) fn validate_for_edit(&self) -> Option<String> {
        if self.description.chars().count() > 1024 {
            return Some("description must be at most 1024 characters".into());
        }
        if self.description.trim().is_empty() {
            return Some("description is required".into());
        }
        if let PromptPolicy::Replace(body) = &self.prompt {
            if body.trim().is_empty() {
                return Some("prompt policy 'replace' requires a non-empty prompt body".into());
            }
        }
        None
    }
}

fn build_runtime_spec(
    runtime: AgentRuntime,
    model_policy: &ModelPolicy,
    reasoning: Option<ReasoningLevel>,
) -> AgentRuntimeSpec {
    match runtime {
        AgentRuntime::Rho => {
            let model = match model_policy {
                ModelPolicy::Inherit => ModelPolicy::Inherit,
                ModelPolicy::Prefer(selection)
                | ModelPolicy::Require(selection)
                | ModelPolicy::Select(selection) => ModelPolicy::Select(ModelSelection {
                    provider: selection.provider.clone(),
                    model: selection.model.clone(),
                }),
            };
            AgentRuntimeSpec::Rho {
                tools: ToolPolicy::All,
                model,
                reasoning,
            }
        }
        AgentRuntime::ClaudeCli => {
            let reasoning = reasoning
                .filter(|level| !matches!(level, ReasoningLevel::Off | ReasoningLevel::Minimal));
            let model = match model_policy {
                ModelPolicy::Inherit => None,
                ModelPolicy::Prefer(selection)
                | ModelPolicy::Require(selection)
                | ModelPolicy::Select(selection) => Some(selection.model.clone()),
            };
            AgentRuntimeSpec::ClaudeCli(ClaudeAgentConfig {
                tools: ClaudeToolPolicy::None,
                inherit_claude_config: false,
                model,
                reasoning,
            })
        }
    }
}

/// Saves a draft when the file still matches `original_contents`.
pub(crate) fn save_definition(
    draft: &AgentDefinition,
    path: &Path,
    original_contents: &str,
) -> Result<String, SaveDefinitionError> {
    let contents = serialize_definition(draft);
    if let Err(error) = parse_definition(path, draft.id.as_str(), &contents) {
        return Err(SaveDefinitionError::Validation(error.to_string()));
    }

    let lock_path = path.with_file_name(format!(
        ".{}.rho-edit.lock",
        path.file_name()
            .and_then(|name| name.to_str())
            .unwrap_or("agent")
    ));
    let mut lock_options = std::fs::OpenOptions::new();
    lock_options.read(true).write(true).create(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        lock_options.custom_flags(libc::O_NOFOLLOW);
    }
    let lock_file = lock_options.open(&lock_path).map_err(|error| {
        SaveDefinitionError::Write(format!("could not open edit lock: {error}"))
    })?;
    let _lock_guard = FileLockGuard {
        file: lock_file,
        path: lock_path,
    };
    fs2::FileExt::try_lock_exclusive(&_lock_guard.file).map_err(|error| {
        SaveDefinitionError::Write(format!("could not lock agent file: {error}"))
    })?;

    let current_contents = std::fs::read_to_string(path)
        .map_err(|error| SaveDefinitionError::Write(error.to_string()))?;
    if current_contents != original_contents {
        return Err(SaveDefinitionError::Conflict);
    }
    crate::config_writer::replace_regular_file_atomically(path, contents.as_bytes())
        .map_err(|error| SaveDefinitionError::Write(error.to_string()))?;
    Ok(contents)
}

struct FileLockGuard {
    file: std::fs::File,
    path: std::path::PathBuf,
}

impl Drop for FileLockGuard {
    fn drop(&mut self) {
        let _ = fs2::FileExt::unlock(&self.file);
        let _ = std::fs::remove_file(&self.path);
    }
}

#[derive(Debug, PartialEq, Eq)]
pub(crate) enum SaveDefinitionError {
    Validation(String),
    Conflict,
    Write(String),
}

impl std::fmt::Display for SaveDefinitionError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Validation(message) => write!(formatter, "agent validation failed: {message}"),
            Self::Conflict => write!(formatter, "agent file changed since editing began"),
            Self::Write(message) => write!(formatter, "could not write agent file: {message}"),
        }
    }
}

impl std::error::Error for SaveDefinitionError {}

#[cfg(test)]
#[path = "edit_tests.rs"]
mod tests;