Skip to main content

artifactize_harness/
verdict.rs

1use std::sync::LazyLock;
2
3use serde_json::Value;
4
5mod candidate;
6pub use candidate::Candidate;
7mod issues;
8pub(crate) use issues::Issues;
9pub use issues::verdict;
10
11/// The schema supplies the fields and allowed priorities; the system prompt names no
12/// separate format. A follow-up uses the same system prefix without a structured response.
13pub(crate) fn review_system(mut system: String) -> String {
14    system.push_str("\nPrioritize each finding by your judgment and explain what is wrong and where.\n\
15        For the review, return only the structured answer matching the supplied schema.\n\
16        If a person later asks a follow-up question about this review, answer that question in plain text instead, not JSON; the issues stay as recorded.");
17    system
18}
19
20/// Prefix for a later question to a saved review; its result format does not apply here.
21pub const FOLLOW_UP: &str = "Follow-up question from a person about the review above. This is not a new review: the issues stay as recorded, and the instruction to return one JSON object applied only to the review. Answer in plain text, not JSON. You may use the tools again.";
22
23/// Bound the raw model response before parsing; prose/whitespace can exceed the semantic result.
24pub const MAX_RESPONSE_BYTES: usize = 1024 * 1024;
25/// Keep the normalized result bounded in saved receipts, in UTF-16 units for JSON clients.
26const MAX_RESULT_CHARS: usize = 256_000;
27static RESULT_TOO_LARGE: LazyLock<String> =
28    LazyLock::new(|| format!("over_size: normalized result exceeds {MAX_RESULT_CHARS} characters"));
29/// Bound each diagnostic so a model's text cannot dominate the repair prompt.
30const MAX_DIAGNOSTIC_CHARS: usize = 200;
31
32/// Audit and usage live in separate rows; this bounds the persisted semantic result.
33fn bounded_result(value: &Value) -> Result<(), &'static str> {
34    if serde_json::to_string(value)
35        .expect("result is JSON")
36        .encode_utf16()
37        .count()
38        > MAX_RESULT_CHARS
39    {
40        return Err(&RESULT_TOO_LARGE);
41    }
42    Ok(())
43}
44
45fn bounded(text: String) -> String {
46    if text.chars().count() > MAX_DIAGNOSTIC_CHARS {
47        text.chars().take(MAX_DIAGNOSTIC_CHARS).collect::<String>() + "… (truncated)"
48    } else {
49        text
50    }
51}
52
53/// An issue's priority: P0 is the most important and P3 the least. artifactize defines only
54/// their order; the evaluator assigns them by its own judgement.
55#[derive(
56    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
57)]
58pub enum Priority {
59    P0,
60    P1,
61    P2,
62    P3,
63}
64
65impl Priority {
66    pub const ALL: [Self; 4] = [Self::P0, Self::P1, Self::P2, Self::P3];
67
68    pub fn as_str(self) -> &'static str {
69        match self {
70            Self::P0 => "P0",
71            Self::P1 => "P1",
72            Self::P2 => "P2",
73            Self::P3 => "P3",
74        }
75    }
76}
77
78impl std::fmt::Display for Priority {
79    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80        f.write_str(self.as_str())
81    }
82}
83
84/// An Agent's issue: how important it is and why.
85#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
86#[serde(deny_unknown_fields)]
87pub struct AgentIssue {
88    pub priority: Priority,
89    pub reason: String,
90}
91
92#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
93#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
94pub enum Verdict {
95    Green,
96    Red,
97}
98
99impl Verdict {
100    pub fn as_str(self) -> &'static str {
101        match self {
102            Self::Green => "GREEN",
103            Self::Red => "RED",
104        }
105    }
106}
107
108/// The review's accepted issues, and the verdict derived from their priorities.
109#[derive(Debug, Clone, PartialEq, serde::Serialize)]
110pub struct Judgment {
111    pub verdict: Verdict,
112    pub issues: Vec<AgentIssue>,
113}