artifactize-harness 0.27.2

The Agent evaluation harness for Artifactize.
Documentation
use std::sync::LazyLock;

use serde_json::Value;

mod candidate;
pub use candidate::Candidate;
mod issues;
pub(crate) use issues::Issues;
pub use issues::verdict;

/// The schema supplies the fields and allowed priorities; the system prompt names no
/// separate format. A follow-up uses the same system prefix without a structured response.
pub(crate) fn review_system(mut system: String) -> String {
    system.push_str("\nPrioritize each finding by your judgment and explain what is wrong and where.\n\
        For the review, return only the structured answer matching the supplied schema.\n\
        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.");
    system
}

/// Prefix for a later question to a saved review; its result format does not apply here.
pub 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.";

/// Bound the raw model response before parsing; prose/whitespace can exceed the semantic result.
pub const MAX_RESPONSE_BYTES: usize = 1024 * 1024;
/// Keep the normalized result bounded in saved receipts, in UTF-16 units for JSON clients.
const MAX_RESULT_CHARS: usize = 256_000;
static RESULT_TOO_LARGE: LazyLock<String> =
    LazyLock::new(|| format!("over_size: normalized result exceeds {MAX_RESULT_CHARS} characters"));
/// Bound each diagnostic so a model's text cannot dominate the repair prompt.
const MAX_DIAGNOSTIC_CHARS: usize = 200;

/// Audit and usage live in separate rows; this bounds the persisted semantic result.
fn bounded_result(value: &Value) -> Result<(), &'static str> {
    if serde_json::to_string(value)
        .expect("result is JSON")
        .encode_utf16()
        .count()
        > MAX_RESULT_CHARS
    {
        return Err(&RESULT_TOO_LARGE);
    }
    Ok(())
}

fn bounded(text: String) -> String {
    if text.chars().count() > MAX_DIAGNOSTIC_CHARS {
        text.chars().take(MAX_DIAGNOSTIC_CHARS).collect::<String>() + "… (truncated)"
    } else {
        text
    }
}

/// An issue's priority: P0 is the most important and P3 the least. artifactize defines only
/// their order; the evaluator assigns them by its own judgement.
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
)]
pub enum Priority {
    P0,
    P1,
    P2,
    P3,
}

impl Priority {
    pub const ALL: [Self; 4] = [Self::P0, Self::P1, Self::P2, Self::P3];

    pub fn as_str(self) -> &'static str {
        match self {
            Self::P0 => "P0",
            Self::P1 => "P1",
            Self::P2 => "P2",
            Self::P3 => "P3",
        }
    }
}

impl std::fmt::Display for Priority {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// An Agent's issue: how important it is and why.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AgentIssue {
    pub priority: Priority,
    pub reason: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum Verdict {
    Green,
    Red,
}

impl Verdict {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Green => "GREEN",
            Self::Red => "RED",
        }
    }
}

/// The review's accepted issues, and the verdict derived from their priorities.
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct Judgment {
    pub verdict: Verdict,
    pub issues: Vec<AgentIssue>,
}