Skip to content

feat(adapters): observability interceptors - #1650

Closed
Glorf wants to merge 1 commit into
feat/adapter-basefrom
feat/adapter-observability
Closed

Glorf wants to merge 1 commit into
feat/adapter-basefrom
feat/adapter-observability

Conversation

@Glorf

@Glorf Glorf commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Adapter interceptor follow-up on feat/adapter-base. Adds 5 passive / mildly-normalizing interceptors:

  • log_tokens — log usage token counts + latency
  • response_stats — accumulate counts / tokens / latency
  • progress_tracking — optional webhook ping every N responses
  • reasoning — normalize <think>…</think> / reasoning field into reasoning_content
  • raise_client_errors — non-retriable 4xx → RuntimeError; 429/408 pass through

All registered as builtins in the interceptor registry. Tests: per-interceptor behavior + parity replay (5 scenarios) — 18 added.

Supersedes Glorf#3 (fork discontinued; rebased clean onto upstream feat/adapter-base).

@copy-pr-bot

copy-pr-bot Bot commented Jun 22, 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.

@github-actions

Copy link
Copy Markdown
Contributor

@Glorf
Glorf force-pushed the feat/adapter-base branch from 8e7dee2 to 4916143 Compare June 23, 2026 09:06
@Glorf
Glorf requested a review from a team as a code owner June 23, 2026 09:06
@Glorf
Glorf force-pushed the feat/adapter-observability branch 3 times, most recently from 349a6d7 to ca5ecd7 Compare June 23, 2026 11:54
@Glorf
Glorf force-pushed the feat/adapter-base branch 2 times, most recently from 45c8b8c to 73051c7 Compare June 23, 2026 12:29
@Glorf
Glorf force-pushed the feat/adapter-observability branch from ca5ecd7 to 1c267b4 Compare June 23, 2026 12:30
@Glorf
Glorf force-pushed the feat/adapter-base branch from 73051c7 to c4313fb Compare June 23, 2026 12:38
PR 2 of 4 — observability family. Built on feat/adapter-base.

Adds 5 interceptors for inspecting traffic without changing semantics:

  - log_tokens          response  log usage token counts + latency
  - response_stats      response  accumulate counts / tokens / latency
  - progress_tracking   response  optional webhook ping every N responses
  - reasoning           response  normalize <think>...</think> OR reasoning
                                  field into reasoning_content
  - raise_client_errors response  non-retriable 4xx -> RuntimeError;
                                  429/408 pass through (retriable)

All five are registered as builtins in InterceptorRegistry._BUILTIN.

Tests

  test_adapter_interceptors.py        per-interceptor behaviour
  test_adapter_parity_replay.py       captured-fixture parity (5 scenarios)
  adapter_fixtures/                   logging, log_tokens, response_stats,
                                      progress_tracking, raise_client_errors,
                                      reasoning JSON fixtures

60 tests pass (42 from feat/adapter-base + 18 added).

Docs

  fern/.../model-server/adapters.mdx  appends Observability section with
                                      per-interceptor YAML config snippets

Signed-off-by: Michal Bien <[email protected]>
@Glorf
Glorf force-pushed the feat/adapter-observability branch from 1c267b4 to 9da7222 Compare June 23, 2026 12:38
@anwithk
anwithk requested a review from ffrujeri June 23, 2026 16:07
@Glorf Glorf closed this Jun 24, 2026
@ffrujeri
ffrujeri requested a review from ananthsub June 24, 2026 20:46

@ffrujeri ffrujeri left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR — the 5 new interceptors are well-structured. Async patterns are correct, progress_tracking correctly routes webhooks through the global aiohttp client, asyncio.Lock usage is sound in both stateful interceptors, all 18 tests pass, and ruff is clean.

Two items worth a look before merging:

Generated by Claude Code (4 agents + devil's advocate)

pt,
ct,
tt,
resp.latency_ms,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nemo_gym/adapters/interceptors/log_tokens.py:46

The docs page advertises that log_tokens logs "token counts and latency", but in the install_middleware path latency_ms is always 0.0 — the middleware closure doesn't timestamp the upstream call (only proxy.py does). Would it make sense to either add timing to the middleware path, or note the limitation in the docs?

The same applies to response_stats, which logs total_latency_ms in its periodic summary.

assert result.status_code == 429


async def test_reasoning_normalize():

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests/unit_tests/test_adapter_interceptors_observability.py:70

reasoning has three normalization paths but only path c (<think>…</think> extraction) is tested here. Could you add coverage for the other two?

  • Path b — "reasoning" field renamed to "reasoning_content" — is the primary DeepSeek/QwQ use case and currently has zero coverage.
  • Path a — idempotency when reasoning_content is already present — is also untested.
async def test_reasoning_normalizes_reasoning_field():
    ic = InterceptorRegistry.create("reasoning")
    resp = _resp({"choices": [{"message": {"reasoning": "chain-of-thought", "content": "answer"}}]})
    result = await ic.intercept_response(resp)
    msg = result.body["choices"][0]["message"]
    assert "reasoning" not in msg
    assert msg["reasoning_content"] == "chain-of-thought"
    assert msg["content"] == "answer"


async def test_reasoning_idempotent_when_already_has_reasoning_content():
    ic = InterceptorRegistry.create("reasoning")
    resp = _resp({"choices": [{"message": {"reasoning_content": "existing", "content": "answer"}}]})
    result = await ic.intercept_response(resp)
    msg = result.body["choices"][0]["message"]
    assert msg["reasoning_content"] == "existing"  # not mutated
    assert "reasoning" not in msg

Glorf added a commit that referenced this pull request Jul 8, 2026
ECS Fargate `SandboxProvider` on top of the sandbox API (#1377, now
merged to `main`). Auto-mirrors public images to ECR on demand; SSH
reverse-tunnel for exec / file-transfer / model egress.

Rebased onto `main` now that the sandbox base (#1377) has landed — this
PR stands on its own and is ready for review.

**Sandbox-bound agents line of work:**
**ECS Fargate (this PR)** → adapter middleware base (#1646) →
sandbox-bound CLI agents + capture (#1647). Interceptor follow-ups on
the adapter base: #1649 (caching), #1650 (observability), #1651
(rewrites).

3 commits, +5974: ECS provider + engine, on-demand ECR mirroring + conda
activation fix, and 233 unit tests (engine.py to 99%). `uv.lock`
reconciled with main's security upgrades (#1657).

---------

Signed-off-by: Michal Bien <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
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