Skip to content

fix(verifiers_agent): never store the policy server's session cookie - #3316

Merged
cmunley1 merged 1 commit into
mainfrom
dmolinari/fix-verifiers-agent
Sep 11, 2026
Merged

cmunley1 merged 1 commit into
mainfrom
dmolinari/fix-verifiers-agent

Conversation

@d-molinari

@d-molinari d-molinari commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

fixes severe rollout routing unbalance when training with verifiers_agent

Context

vllm_model picks a vLLM engine per session (sha256(session_id) % len(base_urls) in _resolve_client) and mints the session id per cookie jar (server_utils.setup_session_middleware). openai.AsyncOpenAI sits on an httpx client that persists cookies, so the single client this agent shares across every rollout in its process was one session and therefore one engine.

Keep the shared client -- the pool has to stay hot -- and give it a CookieJar whose set_cookie is a no-op. Every request is then cookie-less, the router mints a fresh session id per request, and sha256 spreads them over every engine. This is what Gym's own aiohttp client already does with a DummyCookieJar. Pass the bare CookieJar: httpx.Cookies adopts a CookieJar instance as-is but copies an httpx.Cookies into a fresh stdlib jar, which would silently discard the no-store behaviour.

A client per rollout also spreads the load but each rollout's single pooled connection sat idle through its tool phases, the router's uvicorn closes idle connections after 30 s, and the next turn raced that close.

Tested by driving the client through an httpx.MockTransport that sets Set-Cookie on every reply: three consecutive chat completions send no Cookie header, the jar is _NoStoreCookieJar, and _get_client() returns one shared client. A positive control shows a stock AsyncOpenAI does replay the cookie from the second request on, so the suite is known to be sensitive; the cookie test fails if the cookies= argument is dropped.

Squashed from 19bfe25, a100181 and 2e7b26a, without the configurable client timeouts those carried; those land separately.

Checklist

  • I have read the contributing guidelines.
  • The change is focused; unrelated "drive-by" edits are tracked as separate issues/PRs.
  • Tests added or updated and pass locally, or N/A for docs-only / non-code changes (so CI unit/server checks pass when applicable).
  • Pre-commit checks pass locally (pre-commit run --all-files) (so CI lint/format/copyright pass).
  • All commits have DCO sign-off (git commit -s) (so the DCO check passes).

vllm_model picks a vLLM engine per session (sha256(session_id) % len(base_urls)
in _resolve_client) and mints the session id per cookie jar
(server_utils.setup_session_middleware). openai.AsyncOpenAI sits on an httpx
client that persists cookies, so the single client this agent shares across
every rollout in its process was one session and therefore one engine: on CMH
job 3670120 (2026-09-10) 512 concurrent rollouts ran on 6 of 48 engines while
42 sat idle, and the router's outbound connection table showed exactly those
six.

Keep the shared client -- the pool has to stay hot -- and give it a CookieJar
whose set_cookie is a no-op. Every request is then cookie-less, the router
mints a fresh session id per request, and sha256 spreads them over every
engine. This is what Gym's own aiohttp client already does with a
DummyCookieJar. Pass the bare CookieJar: httpx.Cookies adopts a CookieJar
instance as-is but copies an httpx.Cookies into a fresh stdlib jar, which
would silently discard the no-store behaviour.

A client per rollout also spreads the load (CMH 3670792 reached 48 of 48
engines) but is the wrong fix: each rollout's single pooled connection sat idle
through its tool phases, the router's uvicorn closes idle connections after
30 s, and the next turn raced that close -- 468 of 512 rollouts aborted with
APIConnectionError -> ReadError(BrokenResourceError), where the shared-client
runs had zero.

Tested by driving the client through an httpx.MockTransport that sets
Set-Cookie on every reply: three consecutive chat completions send no Cookie
header, the jar is _NoStoreCookieJar, and _get_client() returns one shared
client. A positive control shows a stock AsyncOpenAI does replay the cookie
from the second request on, so the suite is known to be sensitive; the cookie
test fails if the cookies= argument is dropped.

Squashed from 19bfe25, a100181 and 2e7b26a, without the configurable
client timeouts those carried; those land separately.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Signed-off-by: Daniele Molinari <[email protected]>
@copy-pr-bot

copy-pr-bot Bot commented Sep 11, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@d-molinari
d-molinari requested a review from cmunley1 September 11, 2026 15:51
@d-molinari
d-molinari marked this pull request as ready for review September 11, 2026 15:51
@cmunley1

Copy link
Copy Markdown
Contributor

/ok to test ec9dc16

@cmunley1
cmunley1 merged commit d109dec into main Sep 11, 2026
44 checks passed
@cmunley1
cmunley1 deleted the dmolinari/fix-verifiers-agent branch September 11, 2026 18:46
cmunley1 pushed a commit that referenced this pull request Sep 14, 2026
Addresses the **P2** review comment on #3321. Targets that branch rather
than `main`, so the diff here is only the route fix and it lands
together with the prefix work.

## The gap

`rollout_id_from_run` only covers `/run`, where rollout collection
injects `_ng_rollout_id` into the request body. The supported
`/ng-rollout/<id>/v1/responses` route carries the id in the **path**
instead.

There is no `RolloutContextMiddleware` on agents to recover it either —
that is installed on resources servers (`base_resources_server.py:152`);
agents register explicit prefixed routes
(`base_responses_api_agent.py:81`) and nothing sets the contextvar on
them. So `current_rollout_id()` is `None` on that route.

A call on the prefixed Responses route therefore built an **unprefixed**
client and lost capture — the same silent failure #3321 set out to fix,
on the other entry point.

## The fix

`responses` already receives the `Request`, so resolve the id from the
run body first and fall back to `request.path_params`. Gated on the same
`_capture_correlation_enabled` as the body path, so a run with capture
off keeps the shared unprefixed client and nothing changes for it.

## Tests

Driven through the **real FastAPI routes**, not by patching
`rollout_id_from_run` — the earlier unit test read a synthetic
`body.rollout_id` and so never exercised the protocol, which was the
second half of the P2 comment. These POST through the app so the path
parameter is populated the way the server populates it.

| test | asserts |
|---|---|
| `test_prefixed_route_correlates_the_model_client` |
`/ng-rollout/7-2/v1/responses` → `http://policy/ng-rollout/7-2/v1` |
| `test_unprefixed_route_stays_unprefixed` | `/v1/responses` →
`http://policy/v1` |
| `test_prefixed_route_is_inert_when_capture_is_disabled` | capture off
→ shared unprefixed client |

The first fails without this change with `'http://policy/v1' !=
'http://policy/ng-rollout/7-2/v1'`. The other two are negative controls
and pass either way by design.

## One thing to be aware of

`test_policy_client_never_replays_the_session_cookie` and
`test_a_default_openai_client_would_replay_the_session_cookie` (from
#3316) fail in my sandbox — but they fail on the **unmodified branch
head** too, so they are not caused by this change. They look like they
need network egress my sandbox denies. Worth a second pair of eyes in a
normal environment.

Signed-off-by: Wojciech Prazuch <[email protected]>

This branch was previously deployed

1 inactive deployment
public — ec9dc160 Deployed Sep 11, 2026 by copy-pr-bot[bot] via release / finalize / notify #2810
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants