Mark timed-out spans and trace `httpx2` retry attempts in the gh-aw shim

Merged
DS
1mo ago
+342-51

This pull request was posted by Claude Code using claude-opus-5-5 on behalf of David.

  • Related: #10631. Its CI Review recovery needs this telemetry before tokens, retries and latency can be compared.

Why we make these changes

CI Review's Logfire traces hide the work that a timeout cuts off. Before state, at 5c8a6c12c:

Every span in both traces ends UNSET, cut off or not. None of the 192 chat spans has an HTTP child, so 429 retries are invisible.

  • Timed-out spans. OpenTelemetry does not record asyncio.CancelledError as an error, and pydantic-ai follows it. task() and _run_with_timeout now enter their caps through _wall_clock_cap. It is asyncio.timeout with the same durations (asyncio.wait_for uses it since Python 3.12), registered in a ContextVar. When a registered cap has expired, the _MarkTimedOutSpans capability sets error.type=timeout and ERROR status on the agent-run and chat spans the cap cuts off. The cap marks the span that holds it the same way, which for task() is the execute_tool Task span. Other cancellations stay unmarked, such as pydantic-ai stopping a request whose stream failed, and so do a sub-agent's own TimeoutErrors.
  • Usage on cut-off requests. These requests have no usage to record. Three of the four were cut mid-stream and carry output parts but no usage. pydantic-ai records message_start usage, so Z.ai's GLM stream reports usage only at the end. Usage a stream has already reported stays on the span.
  • Model HTTP spans. The runner lock pinned logfire 4.37.0 and opentelemetry-instrumentation-httpx 0.63b1, which cannot instrument httpx2. The runner now takes the pydantic-ai-slim[logfire] extra, locked to the repo's uv.lock versions (logfire 5.1.0, OTel 1.44.0 / 0.65b0). logfire.instrument_httpx() patches httpx2.AsyncHTTPTransport, so each rate_limit_retry_transport attempt gets its own span.
  • HTTP capture. instrument_httpx captures headers and response bodies, but not request bodies. A model request body repeats its chat span's gen_ai.input.messages. A 429's error body is still captured, and logfire.instrument_mcp() still records MCP payloads.

No model, limit, timeout duration, retry or concurrency changes.

New public surface

None.

User-visible behavior

Span in a CI Review traceBeforeAfter
invoke_agent or chat that a cap cuts offUNSETERROR, error.type=timeout
execute_tool Task whose sub-agent timed outUNSETERROR, error.type=timeout
HTTP span under a chat spannoneone per attempt, with status and headers

Verification

Tests in .github/scripts/test_pydantic_ai_runner.py. They run in-process on a clock-skipping event loop, with no real waits:

  • test_subagent_timeout_marks_the_spans_it_cuts_off, cut before and after the first chunk
  • test_run_timeout_marks_the_spans_it_cuts_off, cut in a tool and before and after the first chunk
  • test_a_failed_stream_is_not_marked_as_timed_out, under a run cap that has not expired
  • test_a_subagents_own_timeout_error_is_not_marked_as_timed_out
  • test_rate_limit_retry_attempts_each_get_an_http_span

In the runner's own locked environment, configure_observability() patches httpx2.AsyncHTTPTransport.handle_async_request.

After state: this PR's CI Review, run 39372847458, trace, at 22b4268. All 77 chat spans have an HTTP child. Its 2 sub-agent timeouts mark 6 spans timeout, and no other span is ERROR. The run exercised neither the run cap nor a 429. It also predates 0b5619c2e, which moved the Task-span marking into _wall_clock_cap. The branch merges cleanly with #10347's head, and the new tests pass on that merge.

What changes for existing users

Nothing. Only the repository's gh-aw runner telemetry changes.

Deliberate scope decisions
  • Threat-detection GLM spans. The workflow sources can fix this, and #10347 already does: its shared/engine-zai.md post-step emits gh-aw.detector.* spans from the AWF proxy's token log. This PR leaves it out so the two PRs do not carry conflicting copies.
  • Port for whichever of this PR and #10347 lands second. task()'s marking carries over, because #10347's agent.py reuses cli.task. Three pieces need porting:
    • build_agent() needs _MarkTimedOutSpans, under a public name. WorkflowPolicy needs to register its own expiry, self.timed_out, in _wall_clock_caps.
    • agent.py's instrument_httpx(capture_all=True) needs the same capture options.
    • stage-gh-aw-agent.sh needs the logfire extra on pydantic-ai-slim. Without its opentelemetry-instrumentation-httpx>=0.65b0 floor, httpx2 goes uninstrumented.
  • Result-line totals. These still exclude the usage of cut-off requests, because the client never receives it. Only the AWF proxy log has it.
  • Left unmarked. Neither the compaction summariser's own 120s cap (0 invocations in the baseline) nor task()'s generic failure path is marked.
  • task()'s timeout message. A TimeoutError raised inside a sub-agent still returns error: sub-agent timed out to the model, as on main. Only its span marking changed.
  • MCP HTTP spans. These export as detached root traces. That is unchanged here.

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
  • PR title is fit for the release changelog.