Skip to main content

artifactize_core/config/
profile.rs

1//! Execution profiles and reasoning controls declared by Evals.
2
3use super::{
4    Backend, ModelId,
5    location::Location,
6    validation::{self, positive_integer, present, script, text, timeout},
7};
8use serde::{Deserialize, Serialize};
9
10/// Declared effort is exact: unsupported backend efforts fail rather than remap.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
12#[serde(rename_all = "lowercase")]
13pub enum Reasoning {
14    None,
15    Minimal,
16    Low,
17    Medium,
18    High,
19    Xhigh,
20    Max,
21}
22impl Reasoning {
23    pub fn as_str(self) -> &'static str {
24        match self {
25            Self::None => "none",
26            Self::Minimal => "minimal",
27            Self::Low => "low",
28            Self::Medium => "medium",
29            Self::High => "high",
30            Self::Xhigh => "xhigh",
31            Self::Max => "max",
32        }
33    }
34}
35impl std::fmt::Display for Reasoning {
36    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        formatter.write_str(self.as_str())
38    }
39}
40
41#[derive(Debug, Clone, Serialize, Deserialize)]
42#[serde(tag = "kind", rename_all = "lowercase", deny_unknown_fields)]
43pub enum Profile {
44    Agent {
45        backend: Backend,
46        model: ModelId,
47        #[serde(default, deserialize_with = "present")]
48        reasoning: Option<Reasoning>,
49        #[serde(
50            rename(serialize = "timeoutMs", deserialize = "timeout_ms"),
51            default,
52            deserialize_with = "timeout",
53            serialize_with = "validation::milliseconds::serialize"
54        )]
55        timeout_ms: Option<std::time::Duration>,
56        #[serde(
57            rename(serialize = "maxToolCalls", deserialize = "max_tool_calls"),
58            default,
59            deserialize_with = "positive_integer"
60        )]
61        max_tool_calls: Option<u64>,
62        #[serde(
63            rename(serialize = "maxTokens", deserialize = "max_tokens"),
64            default,
65            deserialize_with = "positive_integer"
66        )]
67        max_tokens: Option<u64>,
68    },
69    Human {},
70    Runtime {
71        #[serde(serialize_with = "crate::platform::serialize_program")]
72        command: String,
73        args: Vec<crate::scope::Argument>,
74        #[serde(
75            rename(serialize = "timeoutMs", deserialize = "timeout_ms"),
76            default,
77            deserialize_with = "timeout",
78            serialize_with = "validation::milliseconds::serialize"
79        )]
80        timeout_ms: Option<std::time::Duration>,
81    },
82}
83
84/// Fields that an `.artf` profile alias invocation (`!alias(field = value, ...)`) may override.
85#[derive(Debug, Clone, Default, Deserialize)]
86#[serde(rename_all = "snake_case", deny_unknown_fields)]
87pub(super) struct AgentProfileOverrides {
88    #[serde(default, deserialize_with = "present")]
89    pub backend: Option<Backend>,
90    #[serde(default, deserialize_with = "present")]
91    pub model: Option<ModelId>,
92    #[serde(default, deserialize_with = "present")]
93    pub reasoning: Option<Reasoning>,
94    #[serde(default, deserialize_with = "timeout")]
95    pub timeout_ms: Option<std::time::Duration>,
96    #[serde(default, deserialize_with = "positive_integer")]
97    pub max_tool_calls: Option<u64>,
98    #[serde(default, deserialize_with = "positive_integer")]
99    pub max_tokens: Option<u64>,
100}
101
102impl Profile {
103    pub(super) fn apply_overrides(&self, overrides: AgentProfileOverrides) -> Result<Self, String> {
104        let Self::Agent {
105            backend,
106            model,
107            reasoning,
108            timeout_ms,
109            max_tool_calls,
110            max_tokens,
111        } = self
112        else {
113            return Err("Profile aliases support only agent profiles (kind = \"agent\").".into());
114        };
115        Ok(Self::Agent {
116            backend: overrides.backend.unwrap_or_else(|| backend.clone()),
117            model: overrides.model.unwrap_or_else(|| model.clone()),
118            reasoning: overrides.reasoning.or(*reasoning),
119            timeout_ms: overrides.timeout_ms.or(*timeout_ms),
120            max_tool_calls: overrides.max_tool_calls.or(*max_tool_calls),
121            max_tokens: overrides.max_tokens.or(*max_tokens),
122        })
123    }
124
125    pub(super) fn validate(&self, location: &Location<'_, '_>) -> Result<(), String> {
126        match self {
127            Self::Agent {
128                backend,
129                model,
130                reasoning,
131                ..
132            } => {
133                location
134                    .child("model")
135                    .check(text(model, "Agent profile model"))?;
136                location.child("backend").check(backend.validate_eval())?;
137                // A custom provider's API is known only where it runs, so its effort is
138                // checked there.
139                if let (Some(reasoning), Some(provider)) = (reasoning, backend.builtin()) {
140                    location.child("reasoning").check(
141                        crate::llm::Api::from(provider).validate_reasoning(reasoning.as_str()),
142                    )?;
143                }
144                Ok(())
145            }
146            Self::Human {} => Ok(()),
147            Self::Runtime { command, args, .. } => location.check(script(command, args)),
148        }
149    }
150
151    pub fn kind(&self) -> ProfileKind {
152        match self {
153            Self::Agent { .. } => ProfileKind::Agent,
154            Self::Human {} => ProfileKind::Human,
155            Self::Runtime { .. } => ProfileKind::Runtime,
156        }
157    }
158}
159
160/// The kind of reviewer a profile names, without its execution options.
161#[derive(
162    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, clap::ValueEnum,
163)]
164#[serde(rename_all = "lowercase")]
165pub enum ProfileKind {
166    /// Runtime evals: a command's exit code.
167    Runtime,
168    /// Agent evals: a model's review.
169    Agent,
170    /// Human evals: a person's sign-off.
171    Human,
172}
173
174impl ProfileKind {
175    pub fn name(self) -> &'static str {
176        match self {
177            Self::Runtime => "runtime",
178            Self::Agent => "agent",
179            Self::Human => "human",
180        }
181    }
182}