Skip to main content

artifactize_core/config/
eval.rs

1//! An Eval declaration: its local id, title, profile, instruction with the typed slots it was
2//! written with, a runtime eval's inputs, and the priority at which an Agent eval turns RED.
3
4use super::{ArtifactName, MutexTag, Profile, ProfileKind, identifier};
5use crate::scope::LogicalTarget;
6use serde::{Deserialize, Serialize};
7use std::ops::Range;
8
9/// The instruction a reviewer reads, as saved with every request.
10#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
11pub struct EvalPayload {
12    pub instruction: String,
13}
14
15/// An Eval's id within its Artifact, such as `follows-style`, validated when its declaration is
16/// read.
17#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
18#[serde(transparent)]
19pub struct LocalEvalId(String);
20
21impl<'de> Deserialize<'de> for LocalEvalId {
22    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
23        String::deserialize(deserializer)?
24            .parse()
25            .map_err(serde::de::Error::custom)
26    }
27}
28
29impl std::str::FromStr for LocalEvalId {
30    type Err = String;
31    fn from_str(value: &str) -> Result<Self, Self::Err> {
32        identifier(value, "Eval id")?;
33        Ok(Self(value.to_owned()))
34    }
35}
36
37impl LocalEvalId {
38    pub fn as_str(&self) -> &str {
39        &self.0
40    }
41}
42
43impl PartialEq<&str> for LocalEvalId {
44    fn eq(&self, other: &&str) -> bool {
45        self.0 == *other
46    }
47}
48
49impl std::ops::Deref for LocalEvalId {
50    type Target = str;
51    fn deref(&self) -> &str {
52        &self.0
53    }
54}
55
56impl std::fmt::Display for LocalEvalId {
57    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58        self.0.fmt(f)
59    }
60}
61
62pub use artifactize_harness::verdict::Priority;
63
64/// Who a tool serves: a reviewing model or a person.
65#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
66#[serde(rename_all = "lowercase")]
67pub enum Audience {
68    Agent,
69    Human,
70}
71
72/// What a slot of an instruction names.
73#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
74#[serde(tag = "kind", rename_all = "lowercase")]
75pub enum Slot {
76    Artf {
77        artifact: ArtifactName,
78    },
79    Path {
80        artifact: ArtifactName,
81        path: LogicalTarget,
82    },
83}
84
85impl Slot {
86    pub fn artifact(&self) -> &ArtifactName {
87        match self {
88            Self::Artf { artifact } | Self::Path { artifact, .. } => artifact,
89        }
90    }
91}
92
93/// A slot and the bytes of the rendered instruction it fills.
94#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
95pub struct InstructionSlot {
96    #[serde(serialize_with = "range")]
97    pub range: Range<usize>,
98    #[serde(flatten)]
99    pub slot: Slot,
100}
101
102fn range<S: serde::Serializer>(range: &Range<usize>, serializer: S) -> Result<S::Ok, S::Error> {
103    [range.start, range.end].serialize(serializer)
104}
105
106#[derive(Debug, Clone, Serialize)]
107#[serde(rename_all = "camelCase")]
108pub struct EvalDeclaration {
109    pub id: LocalEvalId,
110    pub title: String,
111    #[serde(skip_serializing_if = "Vec::is_empty")]
112    pub mutex: Vec<MutexTag>,
113    #[serde(flatten)]
114    body: EvalBody,
115}
116
117#[derive(Debug, Clone, Serialize)]
118#[serde(rename_all = "camelCase")]
119struct EvalBody {
120    profile: Profile,
121    /// The named profile or `name!` macro it refers to, as written.
122    #[serde(skip_serializing_if = "Option::is_none")]
123    profile_name: Option<String>,
124    /// Required for Agent and Human evals; a runtime eval's is unhashed prose.
125    #[serde(skip_serializing_if = "Option::is_none")]
126    payload: Option<EvalPayload>,
127    #[serde(skip_serializing_if = "Vec::is_empty")]
128    slots: Vec<InstructionSlot>,
129    /// A runtime eval's input Artifacts.
130    #[serde(skip_serializing_if = "Vec::is_empty")]
131    inputs: Vec<ArtifactName>,
132    /// An Agent eval's bar: an issue at this priority or a more important one makes it RED.
133    /// Without it, any issue does.
134    #[serde(skip_serializing_if = "Option::is_none")]
135    red_at: Option<Priority>,
136}
137
138/// What an expanded eval declares, before it is checked.
139pub(super) struct Declared {
140    pub id: LocalEvalId,
141    pub title: String,
142    pub mutex: Vec<MutexTag>,
143    pub profile: Profile,
144    pub profile_name: Option<String>,
145    pub instruction: Option<(String, Vec<InstructionSlot>)>,
146    pub inputs: Vec<ArtifactName>,
147    pub red_at: Option<Priority>,
148}
149
150impl EvalDeclaration {
151    /// The fields every profile needs together. Expansion has placed each field already, so
152    /// what is left are rules across them.
153    pub(super) fn new(declared: Declared) -> Result<Self, String> {
154        let kind = declared.profile.kind();
155        if !declared.mutex.is_empty() && kind == ProfileKind::Human {
156            return Err("mutex is supported only on runtime and Agent evals.".into());
157        }
158        if kind != ProfileKind::Agent && declared.red_at.is_some() {
159            return Err("red_at is supported only on Agent evals.".into());
160        }
161        if kind != ProfileKind::Runtime && !declared.inputs.is_empty() {
162            return Err("inputs are supported only on runtime evals.".into());
163        }
164        super::validation::text(&declared.title, "Eval title")?;
165        let (payload, slots) = match declared.instruction {
166            Some((instruction, slots)) => {
167                super::validation::text(&instruction, "Eval instruction")?;
168                (Some(EvalPayload { instruction }), slots)
169            }
170            None if kind == ProfileKind::Runtime => (None, Vec::new()),
171            None => return Err("Agent and Human evals need an instruction.".into()),
172        };
173        if kind == ProfileKind::Runtime && !slots.is_empty() {
174            return Err("A runtime eval's instruction names no Artifact.".into());
175        }
176        Ok(Self {
177            id: declared.id,
178            title: declared.title,
179            mutex: declared.mutex,
180            body: EvalBody {
181                profile: declared.profile,
182                profile_name: declared.profile_name,
183                payload,
184                slots,
185                inputs: declared.inputs,
186                red_at: declared.red_at,
187            },
188        })
189    }
190
191    pub fn profile(&self) -> &Profile {
192        &self.body.profile
193    }
194    pub fn profile_name(&self) -> Option<&str> {
195        self.body.profile_name.as_deref()
196    }
197    pub fn payload(&self) -> Option<&EvalPayload> {
198        self.body.payload.as_ref()
199    }
200    pub fn instruction(&self) -> Option<&str> {
201        self.payload().map(|payload| payload.instruction.as_str())
202    }
203    pub fn slots(&self) -> &[InstructionSlot] {
204        &self.body.slots
205    }
206    pub fn inputs(&self) -> &[ArtifactName] {
207        &self.body.inputs
208    }
209    pub fn red_at(&self) -> Option<Priority> {
210        self.body.red_at
211    }
212}
213
214#[cfg(test)]
215mod tests;