feat(observability): carry the client's trace through a turn
Merged
SA
2mo ago+830+830+012345678901234567890123456789-8-8-0123456789
Issue for this PR
No public issue. The consumer is the IDE extension, which sends a W3C traceparent with each prompt so a failure can be followed across the extension, serve and the Altimate gateway.
Type of change
- Bug fix
- New feature
- Refactor / code improvement
- Documentation
What does this PR do?
When a turn fails behind the Altimate gateway, the gateway's in-stream error reaches the client as UnknownError, with nothing linking it to the backend request that failed. This PR carries the client's trace through the turn.
altimate/observability/trace-context.ts(flat exports):- the trace is bound to the turn's user message, not the session, so overlapping prompts keep their own trace;
- it rejects all-zero ids, upper-case hex and unknown versions;
- one bounded insert path is shared by
bindandinherit.
- Routes (
/message,/prompt_async,/command): mint the turn'smessageIDif the client did not send one, bindtraceparentto it, and pass it on. AnoReplyprompt binds nothing, and a prompt that fails before its loop releases its binding. - Loop (
session/prompt.ts): each step activates its turn, which telemetry uses. A generation releases exactly the turns it ran when it ends. Unbound entry points (/summarize,/init, v2 API) stay untraced. - Messages the loop writes itself (compaction marker and continue/replay, subtask summary, validator retry) inherit the turn they continue. Subagents inherit the parent turn through the parent assistant's
parentID. - Gateway calls (
session/llm.ts):- Altimate providers only (
altimate-backend,altimate-free); traceparentwith a fresh child span per call, andx-request-id=<trace>-<span>;- the pair is skipped entirely if request headers or the provider's configured headers set either.
- Altimate providers only (
- Logs:
trace=<id>on thellmlines and thesession.processorerror line. - Telemetry:
ai.operation.id, stamped at track time and only from an event's ownsession_id.
How did you verify your code works?
test/altimate/trace-context.test.ts:- parsing;
- bind / inherit / clear, and bounds through
inherit; - release ownership and the active turn with overlap;
- header unit tests;
- real App Insights envelopes from
Telemetry.track→ flush:ai.operation.idpresent,_operation_idnot in properties, other sessions unstamped.
test/session/llm.test.ts, onLLM.stream's real outgoing request:- an Altimate provider gets the trace with a fresh span;
- an untraced turn sends nothing;
- a non-Altimate provider sends nothing;
- a configured
x-request-idkeeps both headers out.
test/server/trace-context-routes.test.ts: the routes mint and bind the id, honour a client id, and bind nothing without the header.test/session/trace-context-loop.test.ts, real turns throughSessionPrompt.Servicewith a header-recording LLM server:- traced, then untraced, then traced differently on one session, with release after each turn;
- overflow → auto-compaction → continuation, every call carrying the turn's trace. It fails if the continuation's inheritance is removed.
- Related suites: 598 pass across 23 files. The one failure,
prompt.test.ts"concurrent loop callers all receive same error result", also fails onmain. - Static checks:
bun typecheckandanalyze.ts --markers --strictare clean. - End to end:
servefrom this branch, against a header-recording gateway: a tracedprompt_asyncmade two gateway calls (title and turn), both with the trace and distinct spans; the following untraced prompt on the same session carried none.servestderr lines are taggedtrace=.
Not covered: the /api HttpApi prompt handlers (the extension uses /session/*), helper calls that mint their own message (skill-selector, enhance-prompt, ai-review), and SDK retries reusing a call's span.
Screenshots / recordings
No UI change.
Checklist
- I have tested my changes locally
- I have not included unrelated changes in this PR
🤖 Generated with Claude Code
Summary by CodeRabbit
- New Features
- Trace information now follows prompt turns, continuations, and subagent tasks, helping connect related activity across a session.
- Telemetry and supported provider requests include trace identifiers when available. Existing trace headers are respected, and unrelated providers do not receive added trace headers.
- Relevant process errors now include trace information when available.
- Tests
- Added coverage for trace propagation, session tracking, telemetry correlation, and provider request headers.