1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//! System judgment service.
//!
//! A judgment service answers *typed questions* about state: a probability, a
//! selected option, a graded level. Unlike [`crate::UtilityLlmService`], which
//! returns text a caller has to parse, this returns values the caller can act
//! on, so a malformed answer is not a failure mode a call site has to defend
//! against.
//!
//! Like the utility LLM service this is host-owned: configured once per
//! deployment, never agent- or session-configurable, and never an
//! agent-visible model provider. Capability internals reach it through
//! execution context; the vendor and wire format live in the host.
//!
//! Every question in one request is answered over the same state, in parallel,
//! and cannot see the other answers. Call sites should batch: the cost of an
//! extra question is tokens, the cost of an extra request is a round trip.
use std::collections::{BTreeMap, HashMap};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use crate::{AgentLoopError, Result};
/// One typed question.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum JudgmentQuestion {
/// Whether a condition holds, answered as the probability of yes.
Noul {
/// The yes/no question.
instructions: String,
/// What a yes means.
yes: Option<String>,
/// What a no means.
no: Option<String>,
},
/// Exactly one option from a defined set.
Choice {
/// What to decide.
instructions: String,
/// Option name and an optional description of it. At least two.
options: Vec<(String, Option<String>)>,
},
/// A position along ordered levels.
Score {
/// What to rate.
instructions: String,
/// Ordered level descriptions, lowest first. At least two.
levels: Vec<String>,
},
}
impl JudgmentQuestion {
/// A yes/no question with no explicit criteria.
pub fn noul(instructions: impl Into<String>) -> Self {
Self::Noul {
instructions: instructions.into(),
yes: None,
no: None,
}
}
/// A graded question over ordered levels.
pub fn score<L: Into<String>>(
instructions: impl Into<String>,
levels: impl IntoIterator<Item = L>,
) -> Self {
Self::Score {
instructions: instructions.into(),
levels: levels.into_iter().map(Into::into).collect(),
}
}
/// The primitive name, for logs and metrics.
pub fn kind(&self) -> &'static str {
match self {
Self::Noul { .. } => "noul",
Self::Choice { .. } => "choice",
Self::Score { .. } => "score",
}
}
}
/// One evaluation: state plus the questions to ask about it.
#[derive(Debug, Clone, Default)]
pub struct JudgmentRequest {
/// The content being judged: text, or structured data.
pub state: serde_json::Value,
/// Questions keyed by caller-chosen ids, in insertion order. Ids are for
/// the caller's code and are never sent to the model.
pub questions: Vec<(String, JudgmentQuestion)>,
/// Free-form request metadata for host-side attribution.
pub metadata: HashMap<String, String>,
}
impl JudgmentRequest {
/// Start a request over `state`.
pub fn new(state: impl Into<serde_json::Value>) -> Self {
Self {
state: state.into(),
questions: Vec::new(),
metadata: HashMap::new(),
}
}
/// Add a question under `id`.
pub fn ask(mut self, id: impl Into<String>, question: JudgmentQuestion) -> Self {
self.questions.push((id.into(), question));
self
}
/// Attach attribution metadata, such as the calling capability.
pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.metadata.insert(key.into(), value.into());
self
}
/// How many questions this request carries.
pub fn len(&self) -> usize {
self.questions.len()
}
/// Whether no question has been added yet.
pub fn is_empty(&self) -> bool {
self.questions.is_empty()
}
}
/// One typed answer.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum JudgmentAnswer {
/// Probability that the answer is yes, 0..=1.
///
/// A value near 0.5 means yes and no are near-equally likely — not medium
/// intensity.
Noul {
/// Probability of yes.
probability: f64,
},
/// The selected option and the distribution it came from.
Choice {
/// Highest-probability option.
selected: String,
/// Probability per option.
probabilities: BTreeMap<String, f64>,
/// Distribution concentration, 0..=1.
confidence: f64,
},
/// A probability-weighted position across the levels.
Score {
/// Weighted position; lands between levels.
score: f64,
/// Probability per level index.
probabilities: BTreeMap<usize, f64>,
/// Distribution concentration, 0..=1.
confidence: f64,
},
}
impl JudgmentAnswer {
/// The primitive name, for logs and metrics.
pub fn kind(&self) -> &'static str {
match self {
Self::Noul { .. } => "noul",
Self::Choice { .. } => "choice",
Self::Score { .. } => "score",
}
}
/// Probability of yes, for a noul answer.
pub fn probability_yes(&self) -> Option<f64> {
match self {
Self::Noul { probability } => Some(*probability),
_ => None,
}
}
/// Distribution concentration, for the two primitives that report it.
pub fn confidence(&self) -> Option<f64> {
match self {
Self::Noul { .. } => None,
Self::Choice { confidence, .. } | Self::Score { confidence, .. } => Some(*confidence),
}
}
/// Total probability mass at `level` or above, for a score answer.
///
/// This is the reading that an "any serious hit" rule needs: a bimodal
/// answer that is probably fine and possibly severe must not average into
/// fine.
pub fn probability_at_or_above(&self, level: usize) -> Option<f64> {
match self {
Self::Score { probabilities, .. } => Some(
probabilities
.iter()
.filter(|(index, _)| **index >= level)
.map(|(_, probability)| probability)
.sum(),
),
_ => None,
}
}
}
/// Token usage for one request.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct JudgmentUsage {
/// Tokens consumed by state and questions.
pub input_tokens: u64,
/// Tokens produced by the model.
pub output_tokens: u64,
}
/// The result of one evaluation.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct JudgmentOutcome {
/// The model that answered, as the service resolved it.
pub model: String,
/// One answer per question id.
pub answers: BTreeMap<String, JudgmentAnswer>,
/// Token usage for the request.
pub usage: JudgmentUsage,
}
impl JudgmentOutcome {
/// The answer under `id`, if present.
pub fn get(&self, id: &str) -> Option<&JudgmentAnswer> {
self.answers.get(id)
}
}
/// Host-owned service answering typed questions.
#[async_trait]
pub trait JudgmentService: Send + Sync {
/// Whether the deployment configured a real service.
fn is_configured(&self) -> bool;
/// Answer every question in `request` over its state.
async fn evaluate(&self, request: JudgmentRequest) -> Result<JudgmentOutcome>;
/// Implementation name, for logs.
fn name(&self) -> &'static str {
"JudgmentService"
}
}
/// The service a deployment gets when no judgment provider is configured.
#[derive(Debug, Clone, Default)]
pub struct DisabledJudgmentService;
#[async_trait]
impl JudgmentService for DisabledJudgmentService {
fn is_configured(&self) -> bool {
false
}
async fn evaluate(&self, _request: JudgmentRequest) -> Result<JudgmentOutcome> {
Err(AgentLoopError::llm(
"judgment service is disabled (no UTILITY_TYPESAFE_API_KEY configured)",
))
}
fn name(&self) -> &'static str {
"DisabledJudgmentService"
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn disabled_service_reports_itself_and_rejects_requests() {
let service = DisabledJudgmentService;
assert!(!service.is_configured());
let error = service
.evaluate(JudgmentRequest::new("anything").ask("q", JudgmentQuestion::noul("Yes?")))
.await
.unwrap_err();
assert!(
error.to_string().contains("UTILITY_TYPESAFE_API_KEY"),
"a disabled service must name the switch that enables it: {error}"
);
}
#[test]
fn requests_keep_question_order_and_carry_metadata() {
let request = JudgmentRequest::new(serde_json::json!({"text": "hi"}))
.ask("second", JudgmentQuestion::noul("b"))
.ask("first", JudgmentQuestion::score("a", ["low", "high"]))
.with_metadata("purpose", "guardrails");
assert_eq!(request.len(), 2);
assert_eq!(request.questions[0].0, "second");
assert_eq!(request.questions[1].1.kind(), "score");
assert_eq!(request.metadata["purpose"], "guardrails");
assert_eq!(request.state["text"], "hi");
}
#[test]
fn score_tail_mass_is_read_from_the_distribution_not_the_mean() {
// Probably fine, possibly severe: the mean says 0.6, the tail says 30%.
let answer = JudgmentAnswer::Score {
score: 0.6,
probabilities: BTreeMap::from([(0, 0.7), (1, 0.0), (2, 0.3)]),
confidence: 0.4,
};
assert_eq!(answer.probability_at_or_above(2), Some(0.3));
assert_eq!(answer.probability_at_or_above(0), Some(1.0));
assert_eq!(answer.probability_yes(), None);
assert_eq!(answer.confidence(), Some(0.4));
}
#[test]
fn noul_answers_have_no_separate_confidence() {
let answer = JudgmentAnswer::Noul { probability: 0.92 };
assert_eq!(answer.probability_yes(), Some(0.92));
assert_eq!(answer.confidence(), None);
assert_eq!(answer.probability_at_or_above(1), None);
}
}