← Back to Pull requests

[Router] Parse the request body once per request #1230

Open
unknown opened this pull request with 290 lines diff across 3 files
View on GitHub
unknown unknown commented on Nov 22, 2026 05:52 UTC
Author

Hi Team,

route_general_request parses the request body into request_json to route it, then calls process_request, which runs json.loads(body) again just to read stream and model. That second parse runs once per attempt, so it repeats on every failover retry.

This PR passes the already parsed request_json into process_request. The new argument is optional: direct callers that don't pass it get the same parse and the same 400 for an unparsable body as before.

request_json always matches the bytes sent to the backend: the alias path updates it in place, the request rewriter re-parses it, and PriorityRouter re-serialises the body from it.

Related to #707. The memray profile there lists json.loads / raw_decode as the largest source of allocation churn in the router. This change removes one of the two body parses per request. It does not claim to fix the RSS growth reported there.

This touches the same parse block in process_request as #1067, so whichever lands second needs a small rebase.

Measurements

process_request called in-process against a fake backend, with a 10-turn chat body of each size. Time is the mean of 2000 requests; memory is the median peak traced allocation per request over 200 requests (tracemalloc).

| body | bytes | before µs | after µs | before KiB | after KiB |
|---|---:|---:|---:|---:|---:|
| tiny chat | 547 | 10.6 | 8.5 | 4.7 | 3.3 |
| 2 KB prompt | 2467 | 11.8 | 8.3 | 7.9 | 3.3 |
| 16 KB prompt | 16507 | 18.1 | 8.5 | 35.3 | 3.3 |
| 64 KB prompt | 64507 | 41.2 | 9.8 | 129.0 | 3.3 |
| 256 KB prompt | 256507 | 120.7 | 8.7 | 504.0 | 3.3 |

The cost removed grows with the prompt size, while process_request itself stays flat. Across the whole request, the body is now parsed once instead of twice.

Tests

  • New test_process_request_reuses_given_request_json checks that the body isn't parsed again when request_json is passed. It fails on main.

  • pytest src/tests: 284 passed.
  • ---

  • Make sure the code changes pass the pre-commit checks.

  • Sign-off your commit by using <code>-s</code> when doing <code>git commit</code>

  • Try to classify PRs for easy understanding of the type of changes, such as [Bugfix], [Feat], and [CI]`.
  • Diff: 290 lines (+217 / -73) across 3 files Reviewer: chrikrah Review duration: 3399m
    All Vouch review confidence checks passed
    Residual risk 0.23 = Change Risk (0.40) × (1 − Review Confidence (0.43)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.40 · 290 lines diff across 3 files
    0.40
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    42%
    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
    Shallow Review Depth shallow_review_depth
    0.344
    Diff Size diff_size
    0.339
    Real PR Conversation Timeline
    1 comments
    "Review active on #1230"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 43.0% 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.9% (MEDIUM) · Higher = larger blast radius
    39.9%
    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: 43.0% · Lower = rushed or shallow review
    43.0%
    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: 22.7% · Safe to merge
    22.7%