artifactize_core/agent/verdict/
findings.rs1use serde::{Deserialize, Serialize};
5use serde_json::{Map, Value, json};
6
7use super::{MAX_RESPONSE_BYTES, ValidatedResult, bounded, bounded_result};
8use crate::{config::Priority, runtime::Verdict};
9
10#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
12#[serde(deny_unknown_fields)]
13pub struct Finding {
14 pub priority: Priority,
15 pub reason: String,
16}
17
18#[derive(Deserialize)]
21#[serde(deny_unknown_fields)]
22struct Answer {
23 findings: Vec<Finding>,
24}
25
26pub fn verdict(findings: &[Finding], red_at: Option<Priority>) -> Verdict {
29 if findings
30 .iter()
31 .any(|finding| red_at.is_none_or(|bar| finding.priority <= bar))
32 {
33 Verdict::Red
34 } else {
35 Verdict::Green
36 }
37}
38
39pub(in crate::agent) struct Findings {
41 pub schema: Value,
42 red_at: Option<Priority>,
43}
44
45#[derive(Debug, thiserror::Error)]
47#[error("{message}")]
48pub(in crate::agent) struct ModelError {
49 message: &'static str,
50 detail: Option<String>,
51}
52
53impl From<&'static str> for ModelError {
54 fn from(message: &'static str) -> Self {
55 Self {
56 message,
57 detail: None,
58 }
59 }
60}
61
62impl Findings {
63 pub fn new(red_at: Option<Priority>) -> Self {
66 let priorities: Vec<_> = Priority::ALL.iter().map(|p| p.as_str()).collect();
67 Self {
68 schema: json!({
69 "type":"object",
70 "properties":{
71 "findings":{
72 "type":"array",
73 "description":"Every finding of the review; empty when there is none.",
74 "items":{
75 "type":"object",
76 "properties":{
77 "priority":{
78 "type":"string",
79 "enum":priorities,
80 "description":"P0 is the most important, P3 the least.",
81 },
82 "reason":{"type":"string"},
83 },
84 "required":["priority","reason"],
85 "additionalProperties":false,
86 },
87 },
88 },
89 "required":["findings"],
90 "additionalProperties":false,
91 }),
92 red_at,
93 }
94 }
95
96 pub fn parse_model(&self, text: &str) -> Result<ValidatedResult, ModelError> {
97 if text.len() > MAX_RESPONSE_BYTES {
98 return Err("over_size: final JSON exceeds 1 MiB".into());
99 }
100 if text.trim().is_empty() {
101 return Err("empty: return one JSON object".into());
102 }
103 let value: Value = serde_json::from_str(text).map_err(
104 |_| "not_json: return exactly one JSON object, without prose or code fences",
105 )?;
106 let answer = Answer::deserialize(&value).map_err(|error| ModelError {
107 message: "schema_mismatch: result must be an object with a findings list, each finding a priority P0 to P3 and a reason",
108 detail: Some(bounded(error.to_string())),
109 })?;
110 if answer
111 .findings
112 .iter()
113 .any(|finding| finding.reason.trim().is_empty())
114 {
115 return Err("schema_mismatch: every finding needs a reason".into());
116 }
117 bounded_result(&value)?;
118 Ok(ValidatedResult {
119 verdict: verdict(&answer.findings, self.red_at),
120 fields: Map::from_iter([("findings".into(), json!(answer.findings))]),
121 })
122 }
123
124 pub fn repair_detail(error: ModelError) -> String {
126 match error.detail {
127 Some(detail) => format!("{}\n- {detail}", error.message),
128 None => error.message.into(),
129 }
130 }
131}
132
133#[cfg(test)]
134mod tests;