← Back to Pull requests

fix: streaming guardrail bugs in realtime proxy #13

Closed
unknown closed this pull request without merging on Jun 16, 2026 17:53 UTC
View on GitHub
unknown unknown commented on Jun 16, 2026 15:41 UTC
Author

Summary

This PR fixes three bugs in the WebSocket realtime guardrail path. Rebased onto keithmattix:streaming-guardrails tip (b00c0107). Out-of-band (concurrent) response support is tracked separately in agentgateway/agentgateway#2213.

Writer tasks dropped before flushing

Writer tasks were included as arms of tokio::select!. When either reader exited, the select exited immediately, dropping queued frames in the mpsc channels. Spawning the writers as independent tokio::spawn tasks lets them drain concurrently with readers; the readers' select is followed by tokio::join! on the writer handles to wait for remaining frames.

Empty HeaderMap passed to guardrail

begin_streaming_response_guard received HeaderMap::new() instead of the actual request headers. This is a silent failure for any webhook response guard configured with forwardHeaderMatches: the webhook would be called without the matched headers (e.g. an auth token or tenant ID), producing auth failures or incorrect evaluations at the external guardrail service.

Fixed by adding req_headers to RealtimeGuardContext and threading it through handle_upgrade to guarded_realtime_proxy. The headers are used by get_webhook_forward_headers when building the outbound webhook request; regex and Bedrock guards ignore them.

Double metric emission on FailOpen

apply_webhook recorded GuardrailAction::FailOpen internally then returned GuardrailOutcome::None, causing callers to also record GuardrailAction::Allow. Added a FailOpen variant to GuardrailOutcome so recording happens exactly once at the call site. Also changed apply_webhook_response to return GuardrailOutcome directly, fixing the same double-recording for the response webhook path (Mask/Reject/Pass were also double-recorded through apply_single_response_guard).

Tests

  • llm::policy::tests::webhook_fail_open_emits_single_metric — double metric emission on FailOpen
  • This test fails against keithmattix:streaming-guardrails without this patch. Full suite: 1304 tests, 0 failures.

    Test plan

  • cargo test -p agentgateway --lib -- webhook_fail_open_emits_single_metric

  • cargo test -p agentgateway
  • Diff: 276 lines (+207 / -69) across 3 files Reviewer: jbohanon Review duration: 132m
    All Vouch review confidence checks passed
    Residual risk 0.16 = Change Risk (0.40) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.40 · 276 lines diff across 3 files
    0.40
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.336
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #13"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 39.6% (MEDIUM) · Higher = larger blast radius
    39.6%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 15.6% · Safe to merge
    15.6%