Skip to content

fix(token-id-capture): classify auxiliary model requests - #2787

Merged
cmunley1 merged 2 commits into
NVIDIA-NeMo:mainfrom
ananthsub:ansubramania/model-aux-calls-independent
Sep 23, 2026
Merged

cmunley1 merged 2 commits into
NVIDIA-NeMo:mainfrom
ananthsub:ansubramania/model-aux-calls-independent

Conversation

@ananthsub

@ananthsub ananthsub commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

Why

External agent harnesses can probe optional model-server routes while using the rollout-correlated token-capture URL. Gym previously treated every route outside /v1/responses, /v1/chat/completions, and /v1/messages as an uncaptured generation, so an unimplemented discovery request such as GET /v1/models could mark an otherwise complete rollout as untrainable.

What changed

  • forward auxiliary requests before deciding whether they make capture incomplete
  • treat only HTTP 404 and 405 as harmless unimplemented probes
  • keep every other undeclared outcome fail-closed, including redirects, 204 responses, other 4xx responses, 5xx responses, exceptions, cancellation, and handlers that never start a response
  • preserve the rollout and training-capture prefix across root-relative and same-origin redirects so a followed redirect cannot bypass capture
  • let model servers declare exact successful non-generating routes once, rather than maintaining per-harness allowlists
  • register Gym's first-party POST /tokenize and GET /get_inner_vllm_config routes as non-generating
  • validate custom declarations as exact method/path pairs with no wildcards, query strings, fragments, or non-string methods
  • document the behavior and the optional custom-model configuration
flowchart TD
    A[Capture-prefixed model request] --> B{Captured generation route?}
    B -- Yes --> C[Capture model response and token evidence]
    B -- No --> D[Forward auxiliary request]
    D --> E{Exact model-owned non-generating route?}
    E -- Yes --> F[Keep rollout complete]
    E -- No --> G{Downstream outcome}
    G -- HTTP 404 or 405 --> F
    G -- Redirect --> H[Preserve capture prefix]
    H --> I[Mark rollout incomplete]
    G -- Any other HTTP status --> I
    G -- Exception, cancellation, or no response start --> I
    I --> J[Return or propagate downstream outcome]
Loading

No /v1/models endpoint or harness-specific exception is required. If the model server does not implement the route, its 404 is sufficient. A custom model server only needs token_id_capture_non_generating_requests when it implements a successful auxiliary route whose response cannot contain policy-generated content.

Runtime validation

  • Hermes completed a two-call tool-use rollout with complete capture state; a correlated GET /v1/models probe returned 404 without creating an incomplete marker
  • Claude Code, Cline, Codex, Kilo Code, OpenClaw, OpenCode, and Pi produced no additional auxiliary policy-model requests in representative rollouts
  • unsupported successful routes and non-404/405 outcomes remained fail-closed

Validation

  • uv run pytest tests/unit_tests/test_token_id_capture.py responses_api_models/vllm_model_with_compaction/tests/test_app.py -q
  • scoped pre-commit checks for changed files
  • cd fern && npm run check
  • representative runtime rollout or direct model-route probe for all eight harnesses

Compatibility

The token-entry schema is unchanged. Existing harness and model configurations continue to work. The new model-server declaration is optional and only applies to implemented successful auxiliary routes.

Closes #2678.

@copy-pr-bot

copy-pr-bot Bot commented Aug 26, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@ananthsub
ananthsub requested a review from cmunley1 August 26, 2026 11:20
@ananthsub

Copy link
Copy Markdown
Contributor Author

/ok to test 1ff7f8d

@ananthsub

Copy link
Copy Markdown
Contributor Author

/claude review

@claude

claude Bot commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

SHIP — no reliability concerns.

Focused, correct refinement to _CaptureMiddleware's fail-closed handling of unclassified (dialect is None) auxiliary model requests. Previously every unobserved correlated call marked its rollout incomplete before forwarding; now the middleware defers the decision to the response start and only marks incomplete on a 2xx-with-body response, exempting HEAD, the built-in GET /v1/models, and requests an agent explicitly declares under token_id_capture_non_generating_requests.

Verified the correctness-critical points:

  • Still fails closed for the dangerous case. A successful unclassified route (2xx, not 204/205) that isn't declared still marks the rollout incomplete — proven by test_successful_unknown_capture_path_fails_closed. Failed probes (4xx/5xx) and empty responses no longer poison capture, which is the intended relaxation.
  • Marking happens before http.response.start reaches the client, inside _send_unobserved, so the incomplete flag is durable before any content leaves the middleware. Idempotency guarded by marked_incomplete.
  • mark_incomplete failure is swallowed and logged rather than propagating — consistent with prior behavior; the request still forwards.
  • Config validation is strict. NonGeneratingRequest rejects wildcards, query strings, fragments, and non-absolute paths (extra="forbid", method normalized to uppercase), so a declaration can't silently widen into a generating route. Broad-declaration rejection is covered by the parametrized test.
  • Declarations resolved globally across all agents and merged with the built-in GET /v1/models set; safe because non-generating-ness is a property of the model endpoint, not the calling agent.

Async paths unchanged — no new HTTP clients, no ray.get(), all awaits present. Test coverage is thorough (discovery, failed probes, successful-unknown fail-closed, BYO declared route). No blockers or risks.

@github-actions github-actions Bot added the sla:review-overdue Review response is over the one-business-day SLA label Aug 27, 2026
@cmunley1

Copy link
Copy Markdown
Contributor

claude review

nemo_gym/base_responses_api_model.py
● 1131 [correctness] The new status filter only marks 2xx as generating, so a 3xx redirect on an unclassified path is no longer marked incomplete — and the middleware already rewrote scope['path'], so the redirect Location drops the capture prefix and sends the client to the bare generating endpoint with zero capture.
● 722 [correctness] _NON_GENERATING_REQUESTS exempts only ('GET','/v1/models'), which no Gym model server serves, while Gym's first-party non-generating routes POST /tokenize and GET /get_inner_vllm_config are not exempted.
● 1150 [correctness] mark_incomplete now runs only inside the send callback, so any unclassified request whose downstream never emits http.response.start — an unhandled exception or client disconnect — is silently not marked incomplete. The old code marked unconditionally before forwarding.
● 1108 [correctness] The unconditional method == 'HEAD' exemption is a method-wide wildcard, contradicting the NonGeneratingRequest validator that explicitly rejects wildcards, and it is undocumented and untested.
● 1130 [efficiency] logger.info with an eagerly-evaluated f-string fires on every auxiliary request including already-exempted ones, and is inconsistent with the lazy %s logging eight lines below in the same function.
tests/unit_tests/test_token_id_capture.py
● 669 [test-coverage] test_model_discovery_does_not_mark_capture_incomplete never exercises _NON_GENERATING_REQUESTS: no GET /v1/models route is registered, so the request 404s and is exempted by the failed-probe branch instead.
● 677 [test-coverage] test_failed_unknown_probe_does_not_mark_capture_incomplete parametrizes four paths that all resolve identically to a 404 on an unregistered route, covering one branch four times.
nemo_gym/token_id_capture/config.py
● 265 [correctness] non_generating_requests_for_agents unions declarations from every server entry and every agent with no agent-name filter, so one agent's allowlist exempts that route for all agents — contradicting the field docstring 'Exact metadata or health requests this agent can send'.
● 108 [correctness] _normalize_method uses mode='before' with str(value), so non-string YAML scalars are coerced into bogus method names that pass .isalpha() and are silently accepted rather than rejected.
● 279 [correctness] non_generating_requests_for_agents calls model_validate() with no error context and no try/except, so one malformed declaration anywhere in the run config crashes the model server at setup_webserver() with a ValidationError naming no agent or server.
nemo_gym/base_responses_api_agent.py
● 63 [altitude] The allowlist is declared on the agent, but routes belong to the model server, which already knows which of its own routes generate because it registers them. That inversion is the direct cause of the /tokenize gap.
fern/versions/latest/pages/training-tutorials/external-agent-harnesses.mdx
● 45 [docs] A new agent YAML knob (token_id_capture_non_generating_requests) and a new user-visible failure mode ship with no docs update, on the canonical page for the sibling token_id_capture flag.

@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

cmunley1
cmunley1 previously approved these changes Sep 18, 2026
@github-actions github-actions Bot removed the sla:review-overdue Review response is over the one-business-day SLA label Sep 18, 2026
@ananthsub
ananthsub force-pushed the ansubramania/model-aux-calls-independent branch from 3d31116 to 5821f7e Compare September 23, 2026 16:10
Avoid masking rollouts for failed discovery probes while keeping successful unknown routes fail-closed.

Signed-off-by: Ananth Subramaniam <[email protected]>
Base auxiliary-call handling on observed response outcomes so unimplemented harness probes remain harmless while redirects and failures cannot bypass capture safeguards.

Signed-off-by: Ananth Subramaniam <[email protected]>
@cmunley1
cmunley1 added this pull request to the merge queue Sep 23, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/35895820905

Merged via the queue into NVIDIA-NeMo:main with commit ef4fe4f Sep 23, 2026
42 checks passed
@qiaochuz-nv

Copy link
Copy Markdown
Contributor

POR Test Summary — Auxiliary Token-Capture Requests (#2678)

Verdict: PASS — 4/4 named cases

Added black-box regression coverage for this PR's fail-open/fail-closed boundary and ran it on a clean Gym checkout at 325fb49.

Scenario Result Observed behavior
Discovery probes PASS 12 capture-prefixed 404/405 requests left rollout capture complete; a successful undeclared control route marked it incomplete
Health routes PASS Declared GET /health stayed complete; four undeclared successful liveness routes failed closed
Unobserved generation PASS Two trailing-slash generation requests marked capture incomplete, and both redirects retained the capture prefix
Real Hermes rollout PASS Hermes sent 9 auxiliary 404/405 requests plus 1 generation call; the only incomplete marker came from generation without token IDs

The checks use the durable token-capture state/intent/marker files plus model-server access logs. They verify both halves of the contract: harmless discovery does not poison completeness, while an unclassified successful route that could generate never appears complete.

  • QA testcase: token_capture_auxiliary_requests
  • QA run: 20261006T064806296338Z-166aaa4b
  • Duration: 5m 39s
  • Execution: real inference-provider call for generation; no model mocks or product source patches

This is post-merge POR coverage of the behavior introduced by #2787.

This branch was previously deployed

1 inactive deployment
public — 14189370 Deployed Sep 23, 2026 by copy-pr-bot[bot] via release / finalize / notify #3577
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.

fix(token-id-capture): ignore auxiliary model-server requests when tracking completeness

3 participants