Mark timed-out spans and trace `httpx2` retry attempts in the gh-aw shim
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:
- Run 39358200683, trace: the run cap killed the root, and 2 of 3 sub-agents timed out.
- Run 39360027162, trace: 1 of 3 sub-agents timed out.
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.CancelledErroras an error, and pydantic-ai follows it.task()and_run_with_timeoutnow enter their caps through_wall_clock_cap. It isasyncio.timeoutwith the same durations (asyncio.wait_foruses it since Python 3.12), registered in aContextVar. When a registered cap has expired, the_MarkTimedOutSpanscapability setserror.type=timeoutandERRORstatus on the agent-run andchatspans the cap cuts off. The cap marks the span that holds it the same way, which fortask()is theexecute_tool Taskspan. Other cancellations stay unmarked, such as pydantic-ai stopping a request whose stream failed, and so do a sub-agent's ownTimeoutErrors. - 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_startusage, 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
logfire4.37.0 andopentelemetry-instrumentation-httpx0.63b1, which cannot instrumenthttpx2. The runner now takes thepydantic-ai-slim[logfire]extra, locked to the repo'suv.lockversions (logfire 5.1.0, OTel 1.44.0 / 0.65b0).logfire.instrument_httpx()patcheshttpx2.AsyncHTTPTransport, so eachrate_limit_retry_transportattempt gets its own span. - HTTP capture.
instrument_httpxcaptures headers and response bodies, but not request bodies. A model request body repeats itschatspan'sgen_ai.input.messages. A 429's error body is still captured, andlogfire.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 trace | Before | After |
|---|---|---|
invoke_agent or chat that a cap cuts off | UNSET | ERROR, error.type=timeout |
execute_tool Task whose sub-agent timed out | UNSET | ERROR, error.type=timeout |
HTTP span under a chat span | none | one 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 chunktest_run_timeout_marks_the_spans_it_cuts_off, cut in a tool and before and after the first chunktest_a_failed_stream_is_not_marked_as_timed_out, under a run cap that has not expiredtest_a_subagents_own_timeout_error_is_not_marked_as_timed_outtest_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.mdpost-step emitsgh-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'sagent.pyreusescli.task. Three pieces need porting:build_agent()needs_MarkTimedOutSpans, under a public name.WorkflowPolicyneeds to register its own expiry,self.timed_out, in_wall_clock_caps.agent.py'sinstrument_httpx(capture_all=True)needs the same capture options.stage-gh-aw-agent.shneeds thelogfireextra onpydantic-ai-slim. Without itsopentelemetry-instrumentation-httpx>=0.65b0floor,httpx2goes 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. ATimeoutErrorraised inside a sub-agent still returnserror: sub-agent timed outto the model, as onmain. 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.