forge-guardrails 0.1.2

Foundation types for an LLM-agent workflow framework
Documentation
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
//! Stable tool-call semantic scoring API.

use std::borrow::Cow;
use std::fmt;
use std::str::FromStr;
use std::sync::{Arc, LazyLock};

use serde::Serialize;

use crate::clients::base::ToolCall;
use crate::guardrails::classifier_artifact::{
    EXPECTED_LABELS, FINAL_RESPONSE_EXPECTED_LABELS, LEGACY_EXPECTED_LABELS,
};
use crate::guardrails::scoring_context::{
    ScoringContext, ScoringMetadata, WorkflowStateForScoring,
};

static DEFAULT_SCORING_EXECUTOR: LazyLock<ScoringExecutor> =
    LazyLock::new(ScoringExecutor::default);

/// Classifier operating mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ScorerMode {
    /// Do not run scoring or affect behavior.
    Disabled,
    /// Run scoring for telemetry only.
    #[default]
    Shadow,
    /// Allow high-confidence classifier output to request advisory nudges.
    Advisory,
    /// Allow high-confidence classifier output to block.
    Enforce,
}

impl ScorerMode {
    /// Return the stable lowercase mode name.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Disabled => "disabled",
            Self::Shadow => "shadow",
            Self::Advisory => "advisory",
            Self::Enforce => "enforce",
        }
    }
}

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

impl FromStr for ScorerMode {
    type Err = String;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value.trim().to_ascii_lowercase().as_str() {
            "disabled" => Ok(Self::Disabled),
            "shadow" => Ok(Self::Shadow),
            "advisory" => Ok(Self::Advisory),
            "enforce" => Ok(Self::Enforce),
            other => Err(format!(
                "classifier mode must be disabled, shadow, advisory, or enforce, got '{other}'"
            )),
        }
    }
}

/// Semantic classifier label for one candidate tool call.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ToolCallClass {
    /// Candidate appears valid.
    Valid,
    /// Candidate uses a known tool that is semantically wrong for the request.
    WrongToolSemantic,
    /// Candidate uses a plausible tool with semantically wrong argument values.
    WrongArgumentsSemantic,
    /// Candidate calls a tool when no tool is needed.
    ToolNotNeeded,
    /// Candidate should ask for clarification before tool use.
    NeedsClarification,
    /// Candidate corresponds to a deterministic guardrail failure class.
    DeterministicInvalid,
    /// Unknown label surfaced by an artifact.
    Unknown(String),
}

impl ToolCallClass {
    /// Return the stable classifier label name.
    pub fn as_label(&self) -> Cow<'_, str> {
        match self {
            Self::Valid => Cow::Borrowed("valid"),
            Self::WrongToolSemantic => Cow::Borrowed("wrong_tool_semantic"),
            Self::WrongArgumentsSemantic => Cow::Borrowed("wrong_arguments_semantic"),
            Self::ToolNotNeeded => Cow::Borrowed("tool_not_needed"),
            Self::NeedsClarification => Cow::Borrowed("needs_clarification"),
            Self::DeterministicInvalid => Cow::Borrowed("deterministic_invalid"),
            Self::Unknown(label) => Cow::Borrowed(label.as_str()),
        }
    }
}

/// One classifier label probability entry for telemetry.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct ClassifierTopKEntry {
    /// Label name in the classifier artifact.
    pub label: String,
    /// Softmax probability for this label.
    pub confidence: f32,
    /// Raw logit for this label.
    pub logit: f32,
}

/// Return sorted top-k label probabilities for tool-call logits.
///
/// Unknown label orders return an empty vector rather than emitting misleading
/// telemetry.
pub fn tool_call_top_k_from_logits(logits: &[f32]) -> Vec<ClassifierTopKEntry> {
    if logits.len() == EXPECTED_LABELS.len() {
        top_k_from_logits(&EXPECTED_LABELS, logits)
    } else if logits.len() == LEGACY_EXPECTED_LABELS.len() {
        top_k_from_logits(&LEGACY_EXPECTED_LABELS, logits)
    } else {
        Vec::new()
    }
}

/// Return sorted top-k label probabilities for final-response logits.
///
/// Unknown label orders return an empty vector rather than emitting misleading
/// telemetry.
pub fn final_response_top_k_from_logits(logits: &[f32]) -> Vec<ClassifierTopKEntry> {
    if logits.len() == FINAL_RESPONSE_EXPECTED_LABELS.len() {
        top_k_from_logits(&FINAL_RESPONSE_EXPECTED_LABELS, logits)
    } else {
        Vec::new()
    }
}

fn top_k_from_logits(labels: &[&str], logits: &[f32]) -> Vec<ClassifierTopKEntry> {
    let probs = softmax_for_telemetry(logits);
    let mut entries = labels
        .iter()
        .zip(logits.iter())
        .zip(probs.iter())
        .map(|((label, logit), confidence)| ClassifierTopKEntry {
            label: (*label).to_string(),
            confidence: *confidence,
            logit: *logit,
        })
        .collect::<Vec<_>>();
    entries.sort_by(|left, right| {
        right
            .confidence
            .total_cmp(&left.confidence)
            .then_with(|| left.label.cmp(&right.label))
    });
    entries.truncate(entries.len().min(8));
    entries
}

fn softmax_for_telemetry(logits: &[f32]) -> Vec<f32> {
    if logits.is_empty() {
        return Vec::new();
    }
    let max = logits.iter().copied().fold(f32::NEG_INFINITY, f32::max);
    let exps = logits
        .iter()
        .map(|logit| (*logit - max).exp())
        .collect::<Vec<_>>();
    let sum: f32 = exps.iter().sum();
    if sum == 0.0 || !sum.is_finite() {
        return vec![0.0; logits.len()];
    }
    exps.into_iter().map(|value| value / sum).collect()
}

/// Classifier recommendation after thresholds and mode are applied.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ClassifierAction {
    /// Allow execution.
    Allow,
    /// Record telemetry only.
    ShadowOnly,
    /// Produce an advisory nudge.
    AdvisoryNudge,
    /// Block execution.
    Block,
}

impl ClassifierAction {
    /// Return the stable lowercase action name.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Allow => "allow",
            Self::ShadowOnly => "shadow_only",
            Self::AdvisoryNudge => "advisory_nudge",
            Self::Block => "block",
        }
    }
}

/// Score for a single candidate tool call.
#[derive(Debug, Clone, PartialEq)]
pub struct ToolCallScore {
    /// Predicted semantic label.
    pub label: ToolCallClass,
    /// Softmax confidence for the selected label.
    pub confidence: f32,
    /// Raw classifier logits in label order.
    pub logits: Vec<f32>,
    /// Thresholded action recommendation.
    pub action: ClassifierAction,
    /// Classifier artifact or implementation version.
    pub model_version: String,
    /// End-to-end scoring latency in milliseconds.
    pub latency_ms: f64,
}

/// Synchronous scorer for one tool call after deterministic guardrails pass.
pub trait ToolCallScorer: Send + Sync {
    /// Score one candidate tool call.
    fn score(&self, ctx: &ScoringContext, candidate: &ToolCall) -> anyhow::Result<ToolCallScore>;
}

/// Bounded async executor for synchronous classifier scorers.
#[derive(Clone)]
pub struct ScoringExecutor {
    semaphore: Arc<tokio::sync::Semaphore>,
}

impl ScoringExecutor {
    /// Create a classifier scoring executor with bounded concurrency.
    pub fn new(max_concurrency: usize) -> Self {
        Self {
            semaphore: Arc::new(tokio::sync::Semaphore::new(max_concurrency.max(1))),
        }
    }

    /// Default scorer concurrency, bounded by CPU parallelism and capped at four.
    pub fn default_concurrency() -> usize {
        std::thread::available_parallelism()
            .map(|parallelism| parallelism.get())
            .unwrap_or(1)
            .clamp(1, 4)
    }

    /// Score one tool call on Tokio's blocking pool.
    pub async fn score_tool_call_async(
        &self,
        scorer: Arc<dyn ToolCallScorer>,
        ctx: Arc<ScoringContext>,
        candidate: ToolCall,
    ) -> anyhow::Result<ToolCallScore> {
        self.run_blocking("classifier scoring task failed", move || {
            scorer.score(&ctx, &candidate)
        })
        .await
    }

    async fn run_blocking<T, F>(&self, task_error: &'static str, task: F) -> anyhow::Result<T>
    where
        T: Send + 'static,
        F: FnOnce() -> anyhow::Result<T> + Send + 'static,
    {
        let permit = self
            .semaphore
            .clone()
            .acquire_owned()
            .await
            .map_err(|err| anyhow::anyhow!("classifier scoring semaphore closed: {err}"))?;
        tokio::task::spawn_blocking(move || {
            let _permit = permit;
            task()
        })
        .await
        .map_err(|err| anyhow::anyhow!("{task_error}: {err}"))?
    }
}

impl Default for ScoringExecutor {
    fn default() -> Self {
        Self::new(Self::default_concurrency())
    }
}

/// Shared async scoring pipeline for tool-call and final-response classifiers.
#[derive(Clone)]
pub struct ScoringPipeline {
    tool_call_scorer: Option<Arc<dyn ToolCallScorer>>,
    final_response_scorer: Option<Arc<dyn FinalResponseScorer>>,
    executor: ScoringExecutor,
}

impl ScoringPipeline {
    /// Create a pipeline with the default bounded scoring executor.
    pub fn new(
        tool_call_scorer: Option<Arc<dyn ToolCallScorer>>,
        final_response_scorer: Option<Arc<dyn FinalResponseScorer>>,
    ) -> Self {
        Self {
            tool_call_scorer,
            final_response_scorer,
            executor: (*DEFAULT_SCORING_EXECUTOR).clone(),
        }
    }

    /// Create a pipeline with an explicit scoring executor.
    pub fn with_executor(
        tool_call_scorer: Option<Arc<dyn ToolCallScorer>>,
        final_response_scorer: Option<Arc<dyn FinalResponseScorer>>,
        executor: ScoringExecutor,
    ) -> Self {
        Self {
            tool_call_scorer,
            final_response_scorer,
            executor,
        }
    }

    /// Score candidate tool calls and return the first applicable classifier nudge.
    pub async fn score_tool_calls<F, E>(
        &self,
        ctx: Arc<ScoringContext>,
        candidates: &[ToolCall],
        mut on_score: F,
        mut on_error: E,
    ) -> Option<String>
    where
        F: FnMut(&ToolCall, &ToolCallScore),
        E: FnMut(&ToolCall, &anyhow::Error),
    {
        let scorer = self.tool_call_scorer.clone()?;
        let mut nudge = None;
        for candidate in candidates {
            match self
                .executor
                .score_tool_call_async(scorer.clone(), ctx.clone(), candidate.clone())
                .await
            {
                Ok(score) => {
                    on_score(candidate, &score);
                    if matches!(
                        score.action,
                        ClassifierAction::AdvisoryNudge | ClassifierAction::Block
                    ) {
                        let content =
                            crate::prompts::classifier_nudge(score.label.as_label().as_ref());
                        if score.action == ClassifierAction::Block || nudge.is_none() {
                            nudge = Some(content);
                        }
                    }
                }
                Err(err) => on_error(candidate, &err),
            }
        }
        nudge
    }

    /// Score one final-response candidate and return an applicable classifier nudge.
    pub async fn score_final_response<F, E>(
        &self,
        ctx: Arc<FinalResponseContext>,
        mut on_score: F,
        mut on_error: E,
    ) -> Option<String>
    where
        F: FnMut(&FinalResponseScore),
        E: FnMut(&anyhow::Error),
    {
        let scorer = self.final_response_scorer.clone()?;
        match self.executor.score_final_response_async(scorer, ctx).await {
            Ok(score) => {
                on_score(&score);
                if matches!(
                    score.action,
                    ClassifierAction::AdvisoryNudge | ClassifierAction::Block
                ) {
                    Some(crate::prompts::classifier_nudge(
                        score.label.as_label().as_ref(),
                    ))
                } else {
                    None
                }
            }
            Err(err) => {
                on_error(&err);
                None
            }
        }
    }
}

impl Default for ScoringPipeline {
    fn default() -> Self {
        Self::new(None, None)
    }
}

/// Tool result included in final-response scoring.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FinalResponseToolResult {
    /// Tool name that produced the result.
    pub tool_name: String,
    /// Text payload returned by the tool.
    pub content: String,
}

/// Complete final-response scoring context.
#[derive(Debug, Clone, PartialEq)]
pub struct FinalResponseContext {
    /// Classifier input schema version.
    pub schema_version: String,
    /// User request being satisfied.
    pub user_request: String,
    /// Current workflow state.
    pub workflow_state: WorkflowStateForScoring,
    /// Required facts or contracts when known.
    pub required_facts: Vec<String>,
    /// Ordered tool names called before the final response.
    pub tool_trace: Vec<String>,
    /// Tool results available to ground the response.
    pub tool_results: Vec<FinalResponseToolResult>,
    /// Candidate final response text.
    pub candidate_final_response: String,
    /// Optional generic eval or workflow contracts.
    pub metadata: Option<ScoringMetadata>,
}

/// Semantic classifier label for one candidate final response.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FinalResponseClass {
    /// Candidate final response appears valid.
    ValidFinalResponse,
    /// Candidate omits a required fact present in tool output.
    MissingToolFact,
    /// Candidate contradicts a tool result.
    ContradictsToolResult,
    /// Candidate contains an unsupported claim.
    UnsupportedClaim,
    /// Candidate fails to acknowledge missing data.
    FailedToAcknowledgeDataGap,
    /// Unknown label surfaced by an artifact.
    Unknown(String),
}

impl FinalResponseClass {
    /// Return the stable classifier label name.
    pub fn as_label(&self) -> Cow<'_, str> {
        match self {
            Self::ValidFinalResponse => Cow::Borrowed("valid_final_response"),
            Self::MissingToolFact => Cow::Borrowed("missing_tool_fact"),
            Self::ContradictsToolResult => Cow::Borrowed("contradicts_tool_result"),
            Self::UnsupportedClaim => Cow::Borrowed("unsupported_claim"),
            Self::FailedToAcknowledgeDataGap => Cow::Borrowed("failed_to_acknowledge_data_gap"),
            Self::Unknown(label) => Cow::Borrowed(label.as_str()),
        }
    }
}

/// Score for a candidate final response.
#[derive(Debug, Clone, PartialEq)]
pub struct FinalResponseScore {
    /// Predicted semantic label.
    pub label: FinalResponseClass,
    /// Softmax confidence for the selected label.
    pub confidence: f32,
    /// Raw classifier logits in label order.
    pub logits: Vec<f32>,
    /// Thresholded action recommendation.
    pub action: ClassifierAction,
    /// Classifier artifact or implementation version.
    pub model_version: String,
    /// End-to-end scoring latency in milliseconds.
    pub latency_ms: f64,
}

/// Synchronous scorer for a terminal response after deterministic checks pass.
pub trait FinalResponseScorer: Send + Sync {
    /// Score one candidate final response.
    fn score(&self, ctx: &FinalResponseContext) -> anyhow::Result<FinalResponseScore>;
}

impl ScoringExecutor {
    /// Score one final response on Tokio's blocking pool.
    pub async fn score_final_response_async(
        &self,
        scorer: Arc<dyn FinalResponseScorer>,
        ctx: Arc<FinalResponseContext>,
    ) -> anyhow::Result<FinalResponseScore> {
        self.run_blocking("final-response scoring task failed", move || {
            scorer.score(&ctx)
        })
        .await
    }
}

/// Score one tool call on the shared bounded scoring executor.
pub async fn score_tool_call_async(
    scorer: Arc<dyn ToolCallScorer>,
    ctx: Arc<ScoringContext>,
    candidate: ToolCall,
) -> anyhow::Result<ToolCallScore> {
    DEFAULT_SCORING_EXECUTOR
        .score_tool_call_async(scorer, ctx, candidate)
        .await
}

/// Score one final response on the shared bounded scoring executor.
pub async fn score_final_response_async(
    scorer: Arc<dyn FinalResponseScorer>,
    ctx: Arc<FinalResponseContext>,
) -> anyhow::Result<FinalResponseScore> {
    DEFAULT_SCORING_EXECUTOR
        .score_final_response_async(scorer, ctx)
        .await
}

/// Serialize final-response verifier input with the published v1 format.
pub fn serialize_final_response_state_v1(ctx: &FinalResponseContext) -> String {
    let ws = &ctx.workflow_state;
    let results = ctx
        .tool_results
        .iter()
        .map(|result| format!("{}: {}", result.tool_name, json_string(&result.content)))
        .collect::<Vec<_>>()
        .join("\n");
    let metadata = ctx.metadata.as_ref();

    format!(
        "SCHEMA_VERSION:\n{}\n\nUSER_REQUEST:\n{}\n\nWORKFLOW_STATE:\nrequired_steps={}\ncompleted_steps={}\npending_steps={}\nterminal_tools={}\nrecent_errors={}\n\nREQUIRED_FACTS:\n{}\n\nTOOL_TRACE:\n{}\n\nTOOL_RESULTS:\n{}\n\nCANDIDATE_FINAL_RESPONSE:\n{}\n\nSCORING_METADATA:\nscenario_family={}\nrequires_transform={}\nrequires_synthesis={}\nrequires_all_tool_facts={}\nmust_acknowledge_missing_data={}",
        ctx.schema_version,
        ctx.user_request,
        py_list(&ws.required_steps),
        py_list(&ws.completed_steps),
        py_list(&ws.pending_steps),
        py_list(&ws.terminal_tools),
        py_list(&ws.recent_errors),
        py_list(&ctx.required_facts),
        py_list(&ctx.tool_trace),
        results,
        ctx.candidate_final_response,
        optional_json_string(metadata.and_then(|value| value.scenario_family.as_deref())),
        optional_json_bool(metadata.and_then(|value| value.requires_transform)),
        optional_json_bool(metadata.and_then(|value| value.requires_synthesis)),
        optional_json_bool(metadata.and_then(|value| value.requires_all_tool_facts)),
        optional_json_bool(metadata.and_then(|value| value.must_acknowledge_missing_data)),
    )
}

fn py_list(values: &[String]) -> String {
    if values.is_empty() {
        return "[]".to_string();
    }
    let body = values
        .iter()
        .map(|value| format!("'{}'", value.replace('\\', "\\\\").replace('\'', "\\'")))
        .collect::<Vec<_>>()
        .join(", ");
    format!("[{body}]")
}

fn json_string(value: &str) -> String {
    serde_json::to_string(value).unwrap_or_else(|_| "\"\"".to_string())
}

fn optional_json_string(value: Option<&str>) -> String {
    value.map(json_string).unwrap_or_else(|| "null".to_string())
}

fn optional_json_bool(value: Option<bool>) -> &'static str {
    match value {
        Some(true) => "true",
        Some(false) => "false",
        None => "null",
    }
}

/// Deterministic no-op scorer used by tests and disabled configurations.
#[derive(Debug, Default)]
pub struct NoopToolCallScorer;

impl ToolCallScorer for NoopToolCallScorer {
    fn score(&self, _ctx: &ScoringContext, _candidate: &ToolCall) -> anyhow::Result<ToolCallScore> {
        Ok(ToolCallScore {
            label: ToolCallClass::Valid,
            confidence: 1.0,
            logits: Vec::new(),
            action: ClassifierAction::Allow,
            model_version: "noop".to_string(),
            latency_ms: 0.0,
        })
    }
}

/// Deterministic no-op final-response scorer used by tests and disabled configurations.
#[derive(Debug, Default)]
pub struct NoopFinalResponseScorer;

impl FinalResponseScorer for NoopFinalResponseScorer {
    fn score(&self, _ctx: &FinalResponseContext) -> anyhow::Result<FinalResponseScore> {
        Ok(FinalResponseScore {
            label: FinalResponseClass::ValidFinalResponse,
            confidence: 1.0,
            logits: Vec::new(),
            action: ClassifierAction::Allow,
            model_version: "noop".to_string(),
            latency_ms: 0.0,
        })
    }
}