use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;
use crate::evaluate::{Consultation, ModelTransport, Verdict};
use crate::join::{field_opt, model_number_unit, object_pairs};
use crate::reader::parse_document;
use crate::value::{Error, Result, Value};
use crate::writer::dump_value;
use crate::MODEL_NAME;
const SCORE_LEVELS: [&str; 10] = [
"no sign that the run is running away",
"a trace of concern and nothing corroborating it",
"one signal out of place and the rest steady",
"signals beginning to lean the wrong way",
"a mixed picture with real weight on the wrong side",
"several signals corroborating each other",
"the picture of a run losing its shape",
"strong evidence the run is running away",
"a runaway in all but the final proof",
"a runaway in progress",
];
pub fn judge_payload_value(model_name: &str, vignette: &str) -> Value {
let labels: Vec<Value> = [
(
"hold",
"The run should stand at its stage, where the picture is mixed, or the \
fall has not been earned by two consecutive clean evaluations, or the \
elevation is structural and proceeding to plan.",
),
(
"escalate",
"The run should stand one stage higher, where corroborating signals \
stand behind the rise, among them rising spend velocity and \
compounding missed proofs and new classes of action.",
),
(
"de-escalate",
"The run should stand one stage lower, where the signals that raised \
the stage have actually cleared and two consecutive clean evaluations \
stand behind the fall.",
),
]
.iter()
.map(|(label, criterion)| {
Value::Object(vec![
("label".to_string(), Value::Str(label.to_string())),
("criterion".to_string(), Value::Str(criterion.to_string())),
])
})
.collect();
let levels: Vec<Value> = SCORE_LEVELS
.iter()
.map(|level| Value::Str(level.to_string()))
.collect();
Value::Object(vec![
("model".to_string(), Value::Str(model_name.to_string())),
("state".to_string(), Value::Str(vignette.to_string())),
(
"questions".to_string(),
Value::Array(vec![
Value::Object(vec![
("kind".to_string(), Value::Str("choice".to_string())),
("labels".to_string(), Value::Array(labels)),
]),
Value::Object(vec![
("kind".to_string(), Value::Str("score".to_string())),
("levels".to_string(), Value::Array(levels)),
]),
]),
),
])
}
fn judge_payload(model_name: &str, vignette: &str) -> String {
let value = judge_payload_value(model_name, vignette);
let mut out = String::new();
dump_value(&value, 0, &mut out);
out
}
pub fn consultation_from_reply_body(body: &str, pinned: &str) -> Consultation {
let value = match parse_document(body.as_bytes()) {
Ok(value) => value,
Err(_) => return Consultation::MalformedReply,
};
let pairs = match object_pairs(&value) {
Some(pairs) => pairs,
None => return Consultation::MalformedReply,
};
let model_name = match field_opt(pairs, "model") {
Some(Value::Str(text)) => (*text).clone(),
_ => return Consultation::MalformedReply,
};
if model_name != pinned {
return Consultation::MalformedReply;
}
let choice = match field_opt(pairs, "choice") {
Some(value) => match object_pairs(value) {
Some(pairs) => pairs,
None => return Consultation::MalformedReply,
},
None => return Consultation::MalformedReply,
};
let verdict = match field_opt(choice, "label") {
Some(Value::Null) => {
if let Some(value) = field_opt(choice, "confidence") {
if model_number_unit(value).is_none() {
return Consultation::MalformedReply;
}
}
if let Some(value) = field_opt(pairs, "score") {
let level_ok = match object_pairs(value) {
Some(score_pairs) => matches!(
field_opt(score_pairs, "level"),
Some(Value::Int(level)) if (1..=10).contains(level)
),
None => false,
};
if !level_ok {
return Consultation::MalformedReply;
}
}
return Consultation::Abstention { model_name };
}
Some(Value::Str(text)) => match Verdict::from_label(text) {
Some(verdict) => verdict,
None => return Consultation::MalformedReply,
},
_ => return Consultation::MalformedReply,
};
let confidence = match field_opt(choice, "confidence") {
Some(value) => match model_number_unit(value) {
Some(number) => number,
None => return Consultation::MalformedReply,
},
None => return Consultation::MalformedReply,
};
let score_pairs = match field_opt(pairs, "score") {
Some(value) => match object_pairs(value) {
Some(pairs) => pairs,
None => return Consultation::MalformedReply,
},
None => return Consultation::MalformedReply,
};
let level = match field_opt(score_pairs, "level") {
Some(Value::Int(level)) if (1..=10).contains(level) => *level,
_ => return Consultation::MalformedReply,
};
Consultation::Verdict {
model_name,
verdict,
confidence,
score: (level - 1) as f64 / 9.0,
}
}
pub fn validate_judge_form(
judge_named: bool,
fixture_named: bool,
judge_argument_named: bool,
judge_timeout_named: bool,
) -> Result<()> {
if judge_named && fixture_named {
return Err(Error::InvalidRequest(
"exactly one judge form may stand named on one call".to_string(),
));
}
if !judge_named {
if judge_argument_named {
return Err(Error::InvalidRequest(
"a judge argument stands named with no judge program to carry it".to_string(),
));
}
if judge_timeout_named {
return Err(Error::InvalidRequest(
"a judge timeout stands named with no judge program to carry it".to_string(),
));
}
}
Ok(())
}
const REPLY_MAX_BYTES: usize = 64 * 1024;
const READER_GRACE: std::time::Duration = std::time::Duration::from_millis(500);
pub struct SubprocessTransport {
program: PathBuf,
args: Vec<String>,
timeout: std::time::Duration,
model_name: String,
}
impl SubprocessTransport {
pub fn new(
program: PathBuf,
args: Vec<String>,
timeout: std::time::Duration,
) -> SubprocessTransport {
SubprocessTransport {
program,
args,
timeout,
model_name: MODEL_NAME.to_string(),
}
}
pub fn with_model(
program: PathBuf,
args: Vec<String>,
timeout: std::time::Duration,
model_name: impl Into<String>,
) -> Result<SubprocessTransport> {
let model_name = model_name.into();
crate::validate_model_id(&model_name)?;
Ok(SubprocessTransport {
program,
args,
timeout,
model_name,
})
}
fn run_helper(&self, payload_path: &PathBuf) -> Consultation {
let Some(deadline) = std::time::Instant::now().checked_add(self.timeout) else {
return Consultation::CallFailed;
};
let mut command = std::process::Command::new(&self.program);
command
.args(&self.args)
.arg("--raw-payload")
.arg(payload_path)
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::null());
let mut child = loop {
match command.spawn() {
Ok(child) => break child,
Err(err)
if err.kind() == std::io::ErrorKind::ExecutableFileBusy
&& std::time::Instant::now() < deadline =>
{
std::thread::sleep(std::time::Duration::from_millis(5));
}
Err(_) => return Consultation::CallFailed,
}
};
let mut stdout = child.stdout.take().expect("stdout is piped");
let (sender, receiver) = std::sync::mpsc::channel();
std::thread::spawn(move || {
use std::io::Read;
let mut stored: Vec<u8> = Vec::new();
let mut total: usize = 0;
let mut chunk = [0u8; 8192];
loop {
match stdout.read(&mut chunk) {
Ok(0) => break,
Ok(read) => {
total += read;
if stored.len() <= REPLY_MAX_BYTES {
let keep = (REPLY_MAX_BYTES + 1 - stored.len()).min(read);
stored.extend_from_slice(&chunk[..keep]);
}
}
Err(_) => break,
}
}
let _ = sender.send((stored, total));
});
let status = loop {
match child.try_wait() {
Ok(Some(status)) => break Some(status),
Ok(None) => {
if std::time::Instant::now() >= deadline {
let _ = child.kill();
let _ = child.wait();
break None;
}
std::thread::sleep(std::time::Duration::from_millis(5));
}
Err(_) => break None,
}
};
let (body, total) = match receiver.recv_timeout(READER_GRACE) {
Ok(reply) => reply,
Err(_) => return Consultation::CallFailed,
};
let status = match status {
Some(status) => status,
None => return Consultation::CallFailed,
};
if !status.success() {
return Consultation::CallFailed;
}
if total > REPLY_MAX_BYTES {
return Consultation::MalformedReply;
}
let text = match String::from_utf8(body) {
Ok(text) => text,
Err(_) => return Consultation::MalformedReply,
};
let trimmed = text.trim();
if trimmed.is_empty() {
return Consultation::Silence;
}
consultation_from_reply_body(trimmed, &self.model_name)
}
}
impl ModelTransport for SubprocessTransport {
fn consult(&self, vignette: &str) -> Consultation {
let payload = judge_payload(&self.model_name, vignette);
let temp = match write_payload_temp(&payload) {
Ok(path) => path,
Err(_) => return Consultation::CallFailed,
};
let outcome = self.run_helper(&temp);
let _ = std::fs::remove_file(&temp);
outcome
}
fn pinned_model_name(&self) -> &str {
&self.model_name
}
}
static PAYLOAD_COUNTER: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
pub(crate) fn hex_text(bytes: &[u8]) -> String {
let mut text = String::with_capacity(bytes.len() * 2);
for byte in bytes {
text.push_str(&format!("{byte:02x}"));
}
text
}
fn write_payload_temp(payload: &str) -> std::io::Result<PathBuf> {
for _ in 0..16 {
let mut draw = [0u8; 8];
getrandom::fill(&mut draw).map_err(|err| std::io::Error::other(err.to_string()))?;
let suffix = hex_text(&draw);
let count = PAYLOAD_COUNTER.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
let path = std::env::temp_dir().join(format!(
"arvin-judge-{}-{count}-{suffix}",
std::process::id()
));
match OpenOptions::new().write(true).create_new(true).open(&path) {
Ok(mut handle) => {
if let Err(err) = handle.write_all(payload.as_bytes()) {
drop(handle);
let _ = std::fs::remove_file(&path);
return Err(err);
}
return Ok(path);
}
Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => continue,
Err(err) => return Err(err),
}
}
Err(std::io::Error::other(
"no exclusive temporary name could be taken",
))
}