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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
//! Request handler: bridges HTTP layer and inference with guardrails.
//!
//! handle_chat_completions is the main entry point for /v1/chat/completions.
//! It converts inbound OpenAI messages, runs inference with validation/retry,
//! then strips respond() calls from output.

use crate::clients::base::{LLMClient, LLMRequestOptions, LLMResponse, TextResponse, ToolCall};
use crate::context::manager::ContextManager;
use crate::error::StreamError;
use crate::guardrails::{FinalResponseScorer, StepEnforcer, ToolCallScorer};
use crate::proxy::{
    extract_passthrough, extract_sampling, openai_to_messages, strip_respond_calls,
    OpenAiMessageError,
};
use crate::schema_compression::{
    compress_tool_schemas, patch_anthropic_tool_schemas, SchemaCompressionMode,
};
use crate::tool_output::{ToolOutputCompressionConfig, ToolOutputCompressionState};
use crate::tool_policy::{
    evaluate_tool_call_policy, ToolCallPolicyConfig, ToolCallPolicyRequestState,
};
use crate::tools::respond::RESPOND_TOOL_NAME;
use anyllm_translate::anthropic::streaming::StreamEvent;
use futures_core::Stream;
use indexmap::IndexSet;
use serde_json::Value;
use std::fmt;
use std::pin::Pin;
use std::sync::Arc;
use tokio::sync::Mutex;

mod anthropic;
mod classifier_log;
mod compression;
mod nudge;
mod passthrough;
mod prior_tool_results;
mod request_contract;
mod response_shape;
mod scoring;
mod telemetry;
mod tool_specs;
mod training_capture;

pub use anthropic::{
    handle_anthropic_messages, handle_anthropic_messages_with_scorer,
    handle_anthropic_messages_with_scorers,
    handle_anthropic_messages_with_scorers_and_tool_controls,
    handle_anthropic_messages_with_scorers_and_tool_output_compression,
    handle_anthropic_messages_with_scorers_tool_controls_and_headers,
};
use compression::{
    compress_proxy_tool_results,
    init_proxy_tool_output_compression_log_sink_from_env as init_compression_log_sink_from_env,
    patch_anthropic_tool_results,
    shutdown_proxy_tool_output_compression_log_sink as shutdown_compression_log_sink,
};
use nudge::{
    emit_proxy_classifier_nudge_or_error, emit_proxy_final_response_tool_nudge_or_error,
    emit_proxy_step_nudge_or_error, emit_proxy_tool_policy_nudge_or_error,
    emit_proxy_user_classifier_nudge_or_error, synthetic_respond_tool_call,
};
pub use passthrough::run_passthrough;
use prior_tool_results::record_completed_proxy_tool_results;
#[cfg(test)]
use request_contract::sanitize_guarded_anthropic_body;
use request_contract::{
    add_proxy_respond_tool_if_needed, extract_forge_bool_field, extract_forge_debug_context,
    extract_proxy_step_contract, extract_schema_compression, extract_stream_include_usage,
    extract_tool_call_policy_config, extract_tool_output_compression_config,
    sanitize_guarded_request_options, strip_forge_extension_from_body,
    validate_proxy_step_contract, FORGE_EXTENSION_FIELD, FORGE_REQUIRED_STEPS_FIELD,
    FORGE_RETURN_RAW_ON_GUARDRAIL_FAILURE_FIELD,
};
#[cfg(test)]
use response_shape::{collect_anthropic_events, collect_openai_events};
use response_shape::{text_content_result, text_response_result, tool_calls_result};
use scoring::{score_proxy_final_text, score_proxy_final_tool_calls, score_proxy_tool_calls};
pub use tool_specs::parse_tool_specs;

/// Initialize the optional bounded proxy classifier JSONL sink from environment.
pub fn init_proxy_classifier_log_sink_from_env() {
    classifier_log::init_proxy_classifier_log_sink_from_env();
}

/// Initialize the optional bounded private training-capture JSONL sink from environment.
pub fn init_proxy_training_capture_sink_from_env() {
    training_capture::init_proxy_training_capture_sink_from_env();
}

/// Initialize the optional bounded proxy tool-output compression JSONL sink from environment.
pub fn init_proxy_tool_output_compression_log_sink_from_env() {
    init_compression_log_sink_from_env();
}

/// Shut down the optional bounded proxy classifier JSONL sink.
pub fn shutdown_proxy_classifier_log_sink() {
    classifier_log::shutdown_proxy_classifier_log_sink();
}

/// Shut down the optional bounded private training-capture JSONL sink.
pub fn shutdown_proxy_training_capture_sink() {
    training_capture::shutdown_proxy_training_capture_sink();
}

/// Shut down the optional bounded proxy tool-output compression JSONL sink.
pub fn shutdown_proxy_tool_output_compression_log_sink() {
    shutdown_compression_log_sink();
}

/// Stream of OpenAI chat completion chunk objects.
pub type OpenAiEventStream = Pin<Box<dyn Stream<Item = Result<Value, StreamError>> + Send>>;

/// Stream of Anthropic Messages API SSE events.
pub type AnthropicEventStream =
    Pin<Box<dyn Stream<Item = Result<StreamEvent, StreamError>> + Send>>;

/// Result of handling a chat completion request.
pub enum HandlerResult {
    /// Non-streaming: single OpenAI response object.
    Response(Value),
    /// Streaming: OpenAI response chunk objects.
    StreamBody(OpenAiEventStream),
    /// Non-streaming provider-native Anthropic response object.
    AnthropicResponse(Value),
    /// Streaming provider-native Anthropic events.
    AnthropicStreamBody(AnthropicEventStream),
}

const PROXY_STEP_INDEX: i64 = 0;

impl fmt::Debug for HandlerResult {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Response(value) => f.debug_tuple("Response").field(value).finish(),
            Self::StreamBody(_) => f.write_str("StreamBody(<openai event stream>)"),
            Self::AnthropicResponse(value) => {
                f.debug_tuple("AnthropicResponse").field(value).finish()
            }
            Self::AnthropicStreamBody(_) => {
                f.write_str("AnthropicStreamBody(<anthropic event stream>)")
            }
        }
    }
}

/// Error class for OpenAI-compatible chat completion request handling.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HandlerError {
    /// The request is invalid or malformed.
    BadRequest(String),
    /// Backend or guarded inference failed after the request was accepted, with
    /// no typed upstream HTTP status (collapses to a generic 502).
    Upstream(String),
    /// A backend request failed with a known upstream HTTP status, threaded as a
    /// typed value so the proxy never re-parses it from the message text.
    UpstreamStatus {
        /// Human-readable error message.
        message: String,
        /// The upstream HTTP/API status code (`0` for a transport failure).
        status: i64,
    },
}

impl HandlerError {
    /// Returns the underlying error message.
    pub fn message(&self) -> &str {
        match self {
            Self::BadRequest(message)
            | Self::Upstream(message)
            | Self::UpstreamStatus { message, .. } => message,
        }
    }
}

impl fmt::Display for HandlerError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.message())
    }
}

impl std::error::Error for HandlerError {}

impl From<OpenAiMessageError> for HandlerError {
    fn from(error: OpenAiMessageError) -> Self {
        Self::BadRequest(error.to_string())
    }
}

/// Converts a terminal inference error into a [`HandlerError`], preserving the
/// typed upstream status when it is available so the proxy never re-parses it
/// from the message text.
fn upstream_handler_error(err: crate::error::ForgeError) -> HandlerError {
    use crate::error::ForgeError;
    match &err {
        // Non-streaming backend failures carry the status as a typed value.
        ForgeError::Backend(backend) => HandlerError::UpstreamStatus {
            message: backend.to_string(),
            status: backend.status_code(),
        },
        // A stream-start failure arrives as a `StreamError` whose message is, by
        // construction, "Backend error (status N): body". Recover the status
        // here at the known upstream boundary rather than parsing arbitrary
        // handler messages (which could embed the marker for unrelated reasons).
        ForgeError::Stream(stream) => {
            match crate::error::BackendError::status_from_display(&stream.message) {
                Some(status) => HandlerError::UpstreamStatus {
                    message: stream.to_string(),
                    status,
                },
                None => HandlerError::Upstream(stream.to_string()),
            }
        }
        _ => HandlerError::Upstream(err.to_string()),
    }
}

/// Converts an upstream-boundary String error (from the no-tools passthrough,
/// whose message is a `BackendError`/`StreamError` Display) into a
/// [`HandlerError`], recovering the typed status from the marker when present.
/// Passthrough messages are always genuine backend/stream errors, so this never
/// sees a guardrail-wrapped message that could embed the marker spuriously.
fn upstream_handler_error_from_message(message: String) -> HandlerError {
    match crate::error::BackendError::status_from_display(&message) {
        Some(status) => HandlerError::UpstreamStatus { message, status },
        None => HandlerError::Upstream(message),
    }
}

/// Result of handling an Anthropic Messages API request.
pub enum AnthropicHandlerResult {
    /// Non-streaming: single Anthropic message response object.
    Response(Value),
    /// Streaming: Anthropic SSE events.
    StreamBody(AnthropicEventStream),
}

impl fmt::Debug for AnthropicHandlerResult {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Response(value) => f.debug_tuple("Response").field(value).finish(),
            Self::StreamBody(_) => f.write_str("StreamBody(<anthropic event stream>)"),
        }
    }
}

/// Error class for Anthropic request handling.
#[derive(Debug)]
pub enum AnthropicHandlerError {
    /// The request is invalid or malformed.
    BadRequest(String),
    /// An error occurred in the upstream OpenAI/inference handler, with no typed
    /// upstream HTTP status (collapses to a generic 502).
    Upstream(String),
    /// A backend request failed with a known upstream HTTP status, threaded as a
    /// typed value so the proxy never re-parses it from the message text.
    UpstreamStatus {
        /// Human-readable error message.
        message: String,
        /// The upstream HTTP/API status code (`0` for a transport failure).
        status: i64,
    },
    /// An internal server or serialization error occurred.
    Internal(String),
}

impl AnthropicHandlerError {
    /// Returns the underlying error message.
    pub fn message(&self) -> &str {
        match self {
            Self::BadRequest(message)
            | Self::Upstream(message)
            | Self::UpstreamStatus { message, .. }
            | Self::Internal(message) => message,
        }
    }
}

/// Main handler for /v1/chat/completions.
///
/// When no tools are present, passes through to backend directly (no guardrails).
/// When tools are present, conditionally injects Forge's reserved respond tool,
/// runs inference with validation/retry, then strips respond() calls from output.
///
/// Sampling fields are extracted per-request and passed as a dict (or None);
/// they never persist on the client between calls.
#[allow(clippy::too_many_arguments)]
pub async fn handle_chat_completions<C: LLMClient + 'static>(
    body: &Value,
    client: &Arc<C>,
    context_manager: &Arc<Mutex<ContextManager>>,
    max_retries: i32,
    rescue_enabled: bool,
) -> Result<HandlerResult, HandlerError> {
    handle_chat_completions_with_scorer(
        body,
        client,
        context_manager,
        max_retries,
        rescue_enabled,
        None,
    )
    .await
}

/// Main handler with an optional shadow classifier scorer.
#[allow(clippy::too_many_arguments)]
pub async fn handle_chat_completions_with_scorer<C: LLMClient + 'static>(
    body: &Value,
    client: &Arc<C>,
    context_manager: &Arc<Mutex<ContextManager>>,
    max_retries: i32,
    rescue_enabled: bool,
    scorer: Option<Arc<dyn ToolCallScorer>>,
) -> Result<HandlerResult, HandlerError> {
    handle_chat_completions_with_scorers(
        body,
        client,
        context_manager,
        max_retries,
        rescue_enabled,
        scorer,
        None,
    )
    .await
}

/// Main handler with optional tool-call and final-response scorers.
#[allow(clippy::too_many_arguments)]
pub async fn handle_chat_completions_with_scorers<C: LLMClient + 'static>(
    body: &Value,
    client: &Arc<C>,
    context_manager: &Arc<Mutex<ContextManager>>,
    max_retries: i32,
    rescue_enabled: bool,
    scorer: Option<Arc<dyn ToolCallScorer>>,
    final_response_scorer: Option<Arc<dyn FinalResponseScorer>>,
) -> Result<HandlerResult, HandlerError> {
    handle_chat_completions_with_scorers_and_tool_controls(
        body,
        client,
        context_manager,
        max_retries,
        rescue_enabled,
        scorer,
        final_response_scorer,
        ToolOutputCompressionConfig::disabled(),
        None,
        ToolCallPolicyConfig::disabled(),
        SchemaCompressionMode::Disabled,
    )
    .await
}

/// Main handler with optional scorers and tool-output compression.
#[allow(clippy::too_many_arguments)]
pub async fn handle_chat_completions_with_scorers_and_tool_output_compression<
    C: LLMClient + 'static,
>(
    body: &Value,
    client: &Arc<C>,
    context_manager: &Arc<Mutex<ContextManager>>,
    max_retries: i32,
    rescue_enabled: bool,
    scorer: Option<Arc<dyn ToolCallScorer>>,
    final_response_scorer: Option<Arc<dyn FinalResponseScorer>>,
    default_tool_output_compression: ToolOutputCompressionConfig,
    tool_output_state: Option<Arc<ToolOutputCompressionState>>,
) -> Result<HandlerResult, HandlerError> {
    handle_chat_completions_with_scorers_and_tool_controls(
        body,
        client,
        context_manager,
        max_retries,
        rescue_enabled,
        scorer,
        final_response_scorer,
        default_tool_output_compression,
        tool_output_state,
        ToolCallPolicyConfig::disabled(),
        SchemaCompressionMode::Disabled,
    )
    .await
}

/// Main handler with optional scorers, tool-output compression, and tool-call policy.
#[allow(clippy::too_many_arguments)]
pub async fn handle_chat_completions_with_scorers_and_tool_controls<C: LLMClient + 'static>(
    body: &Value,
    client: &Arc<C>,
    context_manager: &Arc<Mutex<ContextManager>>,
    max_retries: i32,
    rescue_enabled: bool,
    scorer: Option<Arc<dyn ToolCallScorer>>,
    final_response_scorer: Option<Arc<dyn FinalResponseScorer>>,
    default_tool_output_compression: ToolOutputCompressionConfig,
    tool_output_state: Option<Arc<ToolOutputCompressionState>>,
    default_tool_call_policy: ToolCallPolicyConfig,
    default_schema_compression: SchemaCompressionMode,
) -> Result<HandlerResult, HandlerError> {
    handle_chat_completions_impl(
        body,
        client,
        context_manager,
        max_retries,
        rescue_enabled,
        None,
        None,
        scorer,
        final_response_scorer,
        default_tool_output_compression,
        tool_output_state,
        default_tool_call_policy,
        default_schema_compression,
    )
    .await
}

#[allow(clippy::too_many_arguments)]
pub(super) async fn handle_chat_completions_impl<C: LLMClient + 'static>(
    body: &Value,
    client: &Arc<C>,
    context_manager: &Arc<Mutex<ContextManager>>,
    max_retries: i32,
    rescue_enabled: bool,
    inbound_anthropic_body: Option<Value>,
    anthropic_headers: Option<Vec<(String, String)>>,
    scorer: Option<Arc<dyn ToolCallScorer>>,
    final_response_scorer: Option<Arc<dyn FinalResponseScorer>>,
    default_tool_output_compression: ToolOutputCompressionConfig,
    tool_output_state: Option<Arc<ToolOutputCompressionState>>,
    default_tool_call_policy: ToolCallPolicyConfig,
    default_schema_compression: SchemaCompressionMode,
) -> Result<HandlerResult, HandlerError> {
    let messages = body
        .get("messages")
        .and_then(|m| m.as_array())
        .ok_or_else(|| HandlerError::BadRequest("missing or invalid messages field".to_string()))?;

    let model_name = body
        .get("model")
        .and_then(|m| m.as_str())
        .unwrap_or("unknown");

    let stream = body
        .get("stream")
        .and_then(|s| s.as_bool())
        .unwrap_or(false);
    let stream_include_usage = extract_stream_include_usage(body)?;

    let tools_raw = match body.get("tools") {
        Some(Value::Array(tools)) => tools.clone(),
        Some(_) => {
            return Err(HandlerError::BadRequest(
                "tools must be an array".to_string(),
            ));
        }
        None => Vec::new(),
    };
    let step_contract = extract_proxy_step_contract(body)?;
    let return_raw_on_guardrail_failure =
        extract_forge_bool_field(body, FORGE_RETURN_RAW_ON_GUARDRAIL_FAILURE_FIELD)?;
    let tool_output_compression =
        extract_tool_output_compression_config(body, &default_tool_output_compression)?;
    let schema_compression = extract_schema_compression(body, default_schema_compression)?;
    let forge_debug_context = extract_forge_debug_context(body)?;
    let tool_call_policy = extract_tool_call_policy_config(body, &default_tool_call_policy)?;

    let sampling = extract_sampling(body);
    let mut passthrough = extract_passthrough(body);
    if let Some(raw) = inbound_anthropic_body.as_ref() {
        preserve_rebuilt_anthropic_fields(raw, &mut passthrough);
    }
    let mut request_options = LLMRequestOptions {
        sampling,
        passthrough,
        inbound_anthropic_body: inbound_anthropic_body
            .map(strip_forge_extension_from_body)
            .map(Arc::new),
        initial_openai_messages: None,
        anthropic_headers,
        preserve_provider_response: false,
    };

    // Convert inbound OpenAI messages to internal format.
    let mut internal_msgs = openai_to_messages(messages)?;
    let tool_output_updates = compress_proxy_tool_results(
        &mut internal_msgs,
        &tool_output_compression,
        tool_output_state.as_deref(),
        forge_debug_context.as_ref(),
    );
    if !tool_output_updates.is_empty() {
        if let Some(body) = request_options.inbound_anthropic_body.take() {
            let mut patched = body.as_ref().clone();
            if patch_anthropic_tool_results(&mut patched, &tool_output_updates) {
                request_options.inbound_anthropic_body = Some(Arc::new(patched));
            } else {
                tracing::warn!(
                    "failed to patch compressed tool outputs into Anthropic request body; falling back to rebuilt body which may discard custom metadata or cache_control flags"
                );
            }
        }
    }
    // Patch schema descriptions in the Anthropic body before locking it in.
    if schema_compression != SchemaCompressionMode::Disabled {
        if let Some(body) = request_options.inbound_anthropic_body.as_ref() {
            let mut patched = body.as_ref().clone();
            if patch_anthropic_tool_schemas(&mut patched, schema_compression) {
                request_options.inbound_anthropic_body = Some(Arc::new(patched));
            }
        }
    }
    if request_options.inbound_anthropic_body.is_some() {
        request_options.preserve_provider_response = true;
        request_options.initial_openai_messages = Some(Arc::from(
            crate::core::inference::fold_and_serialize(
                &internal_msgs,
                client.api_format().as_str(),
            )
            .into_boxed_slice(),
        ));
    }

    // If no tools, pass through directly.
    if tools_raw.is_empty() {
        if let Some(contract) = step_contract.as_ref() {
            if !contract.required_steps.is_empty() {
                return Err(HandlerError::BadRequest(format!(
                    "{FORGE_EXTENSION_FIELD}.{FORGE_REQUIRED_STEPS_FIELD} requires tools"
                )));
            }
        }
        let api_format = client.api_format().as_str();
        let serialized = crate::core::inference::fold_and_serialize(&internal_msgs, api_format);
        return run_passthrough(
            client,
            &serialized,
            None,
            request_options,
            model_name,
            stream,
            stream_include_usage,
        )
        .await
        .map_err(upstream_handler_error_from_message);
    }

    // Parse client tools strictly, then add Forge's reserved terminal tool only
    // when the request has not declared a real terminal tool.
    let mut tool_specs = parse_tool_specs(&tools_raw)?;
    compress_tool_schemas(&mut tool_specs, schema_compression);
    let respond_injected =
        add_proxy_respond_tool_if_needed(&mut tool_specs, step_contract.as_ref());

    let tool_names: IndexSet<String> = tool_specs.iter().map(|s| s.name.clone()).collect();
    let step_contract = validate_proxy_step_contract(step_contract, &tool_names, respond_injected)?;
    let request_options =
        sanitize_guarded_request_options(request_options, step_contract.as_ref())?;
    let validator = crate::guardrails::ResponseValidator::from_tool_specs(
        tool_specs.clone(),
        rescue_enabled,
        None,
    );
    let mut error_tracker = crate::guardrails::ErrorTracker::new(max_retries, 2);
    let mut tool_call_counter = 0;
    let mut step_enforcer = step_contract.map(|contract| {
        let mut enforcer = StepEnforcer::new(
            contract.required_steps,
            contract.terminal_tools.into_iter().collect(),
            None,
            3,
            2,
        );
        record_completed_proxy_tool_results(messages, &internal_msgs, &mut enforcer);
        enforcer
    });

    let mut accepted_usage = None;
    let mut accepted_usage_details = None;
    let mut accepted_provider_response = None;
    let mut accepted_provider_events = None;
    let mut tool_call_policy_state = ToolCallPolicyRequestState::new();
    let response = loop {
        let step_hint = step_enforcer
            .as_ref()
            .map(StepEnforcer::summary_hint)
            .unwrap_or_default();
        let inference_result = crate::core::inference::run_inference_with_options_shared_context(
            &mut internal_msgs,
            client.as_ref(),
            context_manager.as_ref(),
            &validator,
            &mut error_tracker,
            &tool_specs,
            &mut tool_call_counter,
            PROXY_STEP_INDEX,
            &step_hint,
            Some(max_retries + 1),
            stream,
            None,
            request_options.clone(),
        )
        .await;

        let result = match inference_result {
            Ok(Some(result)) => result,
            Ok(None) => break LLMResponse::Text(TextResponse::new("")),
            Err(crate::error::ForgeError::ToolCall(err)) => {
                telemetry::capture_guardrail_exhausted(
                    "deterministic_tool_validation_exhausted",
                    &[],
                    &[],
                    Some(error_tracker.consecutive_retries()),
                    Some(error_tracker.max_retries()),
                    Some(stream),
                );
                if !return_raw_on_guardrail_failure {
                    return Err(HandlerError::Upstream(format!(
                        "model failed guarded tool-call validation after retries: {}",
                        err
                    )));
                }
                let raw = err.raw_response.unwrap_or_default();
                let usage = client.last_usage();
                let usage_details = client.last_usage_details();
                return Ok(text_content_result(
                    &raw,
                    model_name,
                    stream,
                    stream_include_usage,
                    usage.as_ref(),
                    usage_details.as_ref(),
                ));
            }
            Err(err) => return Err(upstream_handler_error(err)),
        };

        tool_call_counter = result.tool_call_counter;

        let result_usage = result.usage;
        let result_usage_details = result.usage_details;
        let result_provider_response = result.provider_response;
        let result_provider_events = result.provider_events;
        let response = result.response;
        let Some(enforcer) = step_enforcer.as_mut() else {
            match response {
                LLMResponse::ToolCalls(tool_calls) => {
                    if let Some(nudge) = evaluate_tool_call_policy(
                        &tool_calls,
                        &tool_specs,
                        &tool_call_policy,
                        &mut tool_call_policy_state,
                    ) {
                        emit_proxy_tool_policy_nudge_or_error(
                            &mut error_tracker,
                            tool_calls,
                            &mut internal_msgs,
                            &mut tool_call_counter,
                            &nudge.content,
                        )
                        .map_err(HandlerError::Upstream)?;
                        continue;
                    }
                    if let Some(nudge) = score_proxy_tool_calls(
                        scorer.clone(),
                        &internal_msgs,
                        &tool_calls,
                        None,
                        &tool_specs,
                    )
                    .await
                    {
                        emit_proxy_classifier_nudge_or_error(
                            &mut error_tracker,
                            tool_calls,
                            &mut internal_msgs,
                            &mut tool_call_counter,
                            &nudge,
                        )
                        .map_err(HandlerError::Upstream)?;
                        continue;
                    }
                    if let Some(nudge) = score_proxy_final_tool_calls(
                        final_response_scorer.clone(),
                        &internal_msgs,
                        &tool_calls,
                        None,
                        &tool_specs,
                    )
                    .await
                    {
                        emit_proxy_final_response_tool_nudge_or_error(
                            &mut error_tracker,
                            tool_calls,
                            &mut internal_msgs,
                            &mut tool_call_counter,
                            &nudge,
                        )
                        .map_err(HandlerError::Upstream)?;
                        continue;
                    }
                    accepted_usage = result_usage;
                    accepted_usage_details = result_usage_details;
                    accepted_provider_response = result_provider_response;
                    accepted_provider_events = result_provider_events;
                    break LLMResponse::ToolCalls(tool_calls);
                }
                LLMResponse::Text(text) => {
                    if let Some(nudge) = score_proxy_final_text(
                        final_response_scorer.clone(),
                        &internal_msgs,
                        &text.content,
                        None,
                        &tool_specs,
                    )
                    .await
                    {
                        emit_proxy_user_classifier_nudge_or_error(
                            &mut error_tracker,
                            &mut internal_msgs,
                            &nudge,
                        )
                        .map_err(HandlerError::Upstream)?;
                        continue;
                    }
                    accepted_usage = result_usage;
                    accepted_usage_details = result_usage_details;
                    accepted_provider_response = result_provider_response;
                    accepted_provider_events = result_provider_events;
                    break LLMResponse::Text(text);
                }
            }
        };

        match response {
            LLMResponse::ToolCalls(tool_calls) => {
                if !enforcer.is_satisfied() {
                    let step_check = enforcer.check(&tool_calls);
                    if step_check.needs_nudge {
                        emit_proxy_step_nudge_or_error(
                            enforcer,
                            step_check,
                            tool_calls,
                            &mut internal_msgs,
                            &mut tool_call_counter,
                        )
                        .map_err(HandlerError::Upstream)?;
                        continue;
                    }
                }

                if let Some(nudge) = evaluate_tool_call_policy(
                    &tool_calls,
                    &tool_specs,
                    &tool_call_policy,
                    &mut tool_call_policy_state,
                ) {
                    emit_proxy_tool_policy_nudge_or_error(
                        &mut error_tracker,
                        tool_calls,
                        &mut internal_msgs,
                        &mut tool_call_counter,
                        &nudge.content,
                    )
                    .map_err(HandlerError::Upstream)?;
                    continue;
                }
                if let Some(nudge) = score_proxy_tool_calls(
                    scorer.clone(),
                    &internal_msgs,
                    &tool_calls,
                    Some(enforcer),
                    &tool_specs,
                )
                .await
                {
                    emit_proxy_classifier_nudge_or_error(
                        &mut error_tracker,
                        tool_calls,
                        &mut internal_msgs,
                        &mut tool_call_counter,
                        &nudge,
                    )
                    .map_err(HandlerError::Upstream)?;
                    continue;
                }
                if let Some(nudge) = score_proxy_final_tool_calls(
                    final_response_scorer.clone(),
                    &internal_msgs,
                    &tool_calls,
                    Some(enforcer),
                    &tool_specs,
                )
                .await
                {
                    emit_proxy_final_response_tool_nudge_or_error(
                        &mut error_tracker,
                        tool_calls,
                        &mut internal_msgs,
                        &mut tool_call_counter,
                        &nudge,
                    )
                    .map_err(HandlerError::Upstream)?;
                    continue;
                }
                accepted_usage = result_usage;
                accepted_usage_details = result_usage_details;
                accepted_provider_response = result_provider_response;
                accepted_provider_events = result_provider_events;
                break LLMResponse::ToolCalls(tool_calls);
            }
            LLMResponse::Text(text) => {
                if !enforcer.is_satisfied() {
                    let tool_calls = vec![synthetic_respond_tool_call(&text)];
                    let step_check = enforcer.check(&tool_calls);
                    if step_check.needs_nudge {
                        emit_proxy_step_nudge_or_error(
                            enforcer,
                            step_check,
                            tool_calls,
                            &mut internal_msgs,
                            &mut tool_call_counter,
                        )
                        .map_err(HandlerError::Upstream)?;
                        continue;
                    }
                }

                if let Some(nudge) = score_proxy_final_text(
                    final_response_scorer.clone(),
                    &internal_msgs,
                    &text.content,
                    Some(enforcer),
                    &tool_specs,
                )
                .await
                {
                    emit_proxy_user_classifier_nudge_or_error(
                        &mut error_tracker,
                        &mut internal_msgs,
                        &nudge,
                    )
                    .map_err(HandlerError::Upstream)?;
                    continue;
                }
                accepted_usage = result_usage;
                accepted_usage_details = result_usage_details;
                accepted_provider_response = result_provider_response;
                accepted_provider_events = result_provider_events;
                break LLMResponse::Text(text);
            }
        }
    };

    let usage = accepted_usage;
    let usage_details = accepted_usage_details;
    let provider_response = accepted_provider_response;
    let provider_events = accepted_provider_events;

    let handler_result = match response {
        LLMResponse::Text(ref text) => text_response_result(
            text,
            model_name,
            stream,
            stream_include_usage,
            usage.as_ref(),
            usage_details.as_ref(),
        ),
        LLMResponse::ToolCalls(ref calls) => {
            let (real_calls, respond_text) = strip_respond_calls(calls);
            training_capture::emit_proxy_training_tool_call_candidates(
                &internal_msgs,
                &real_calls,
                step_enforcer.as_ref(),
                &tool_specs,
            );

            if real_calls.is_empty() {
                // Pure respond: convert to text.
                let text = respond_text.unwrap_or_default();
                text_content_result(
                    &text,
                    model_name,
                    stream,
                    stream_include_usage,
                    usage.as_ref(),
                    usage_details.as_ref(),
                )
            } else if respond_text.is_none() {
                if stream {
                    if let Some(events) = provider_events {
                        anthropic_stream_result(events)
                    } else {
                        tool_calls_result(
                            &real_calls,
                            model_name,
                            stream,
                            stream_include_usage,
                            usage.as_ref(),
                            usage_details.as_ref(),
                        )
                    }
                } else if let Some(value) = provider_response {
                    HandlerResult::AnthropicResponse(value)
                } else {
                    tool_calls_result(
                        &real_calls,
                        model_name,
                        stream,
                        stream_include_usage,
                        usage.as_ref(),
                        usage_details.as_ref(),
                    )
                }
            } else {
                // Real tool calls: return only those calls and drop respond.
                tool_calls_result(
                    &real_calls,
                    model_name,
                    stream,
                    stream_include_usage,
                    usage.as_ref(),
                    usage_details.as_ref(),
                )
            }
        }
    };

    Ok(handler_result)
}

fn preserve_rebuilt_anthropic_fields(
    raw: &Value,
    passthrough: &mut Option<serde_json::Map<String, Value>>,
) {
    let mut insert = |key: &str| {
        let Some(value) = raw.get(key) else {
            return;
        };
        passthrough
            .get_or_insert_with(serde_json::Map::new)
            .entry(key.to_string())
            .or_insert_with(|| value.clone());
    };
    insert("thinking");
    insert("output_config");
}

/// Remove respond() calls, keeping only real tool calls.
pub fn filter_respond(calls: &[ToolCall]) -> Vec<ToolCall> {
    calls
        .iter()
        .filter(|c| c.tool != RESPOND_TOOL_NAME)
        .cloned()
        .collect()
}

/// Convert LLM response to OpenAI format (streaming or non-streaming).
pub fn process_response(response: &LLMResponse, model_name: &str, stream: bool) -> HandlerResult {
    match response {
        LLMResponse::ToolCalls(calls) => {
            tool_calls_result(calls, model_name, stream, false, None, None)
        }
        LLMResponse::Text(text) => {
            text_response_result(text, model_name, stream, false, None, None)
        }
    }
}

fn anthropic_stream_result(events: Vec<Value>) -> HandlerResult {
    HandlerResult::AnthropicStreamBody(Box::pin(async_stream::stream! {
        for event in events {
            match serde_json::from_value::<StreamEvent>(event) {
                Ok(event) => yield Ok(event),
                Err(err) => {
                    yield Err(StreamError::new(err.to_string()));
                    return;
                }
            }
        }
    }))
}

#[cfg(test)]
mod tests;