feat(observability): carry the client's trace through a turn

Merged
SA
2mo ago
+830-8

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 bind and inherit.
  • Routes (/message, /prompt_async, /command): mint the turn's messageID if the client did not send one, bind traceparent to it, and pass it on. A noReply prompt 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);
    • traceparent with a fresh child span per call, and x-request-id = <trace>-<span>;
    • the pair is skipped entirely if request headers or the provider's configured headers set either.
  • Logs: trace=<id> on the llm lines and the session.processor error line.
  • Telemetry: ai.operation.id, stamped at track time and only from an event's own session_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.id present, _operation_id not in properties, other sessions unstamped.
  • test/session/llm.test.ts, on LLM.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-id keeps 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 through SessionPrompt.Service with 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 on main.
  • Static checks: bun typecheck and analyze.ts --markers --strict are clean.
  • End to end:
    • serve from this branch, against a header-recording gateway: a traced prompt_async made two gateway calls (title and turn), both with the trace and distinct spans; the following untraced prompt on the same session carried none.
    • serve stderr lines are tagged trace=.

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.