Repository navigation
Description
Description
The trace context injected into provider requests can reference the previous step's LLM span. In multi-step sessions, the gateway spans for step N parent the LLM span of step N-1 even though that span has already ended, so per-request gateway telemetry lands under the wrong LLM call in the trace.
Root cause
Two factors combine:
- opencode publishes
session.step.startedlate relative to the request. In the session runner, the start snapshot is forked andstartAssistantawaits it before publishingsession.step.started, explicitly so the capture "can overlap the provider request". Thesession.model.requesthook for step N therefore always fires before the plugin can observe step N's ownsession.step.startedevent. - The durable event bus lags the request loop.
session.step.endedof step N-1 is delivered to plugins with roughly 100ms of latency, while the next step's request hook fires ~70-90ms after step N-1 completed.
As a result, handleModelRequest reads a stale activeLlm entry (still holding step N-1's span) and injects it through injectTraceContext(active.spanContext, event.headers). The existing await ctx.tracing.eventQueue cannot cover this case: the session.step.ended event is already published but not yet delivered to the subscriber, so it is not part of the chain being awaited.
Step 1 is unaffected because activeLlm is empty at that point and the provisional-span path creates a fresh span that handleStepStarted later adopts.
Observed
On a session with three LLM steps, every follow-up gateway request carried the previous step's traceparent (timestamps from an exported trace):
llm1: 45.298 -> 48.335
llm2: 48.355 -> 51.045
llm3: 51.065 -> 52.895
nginx request 1 at 45.558 -> parent llm1 (correct, first request)
nginx request 2 at 48.427 -> parent llm1 (wrong: starts 92ms after llm1 ended, during llm2's window)
nginx request 3 at 51.137 -> parent llm2 (wrong: starts 92ms after llm2 ended, during llm3's window)
The behavior is timing-dependent (the race window is roughly the bus delivery latency), so it can pass or fail intermittently between runs.
Introduced by the propagation feature in #108; present in 2.1.0.
Suggested fix
Implemented in #147: when the recorded activeLlm entry exists, handleModelRequest waits a bounded settle window (200ms, 15ms ticks, re-checking after each event-queue drain) for the queued step events to land:
- if the entry is cleared (the previous
session.step.endedwas processed), a fresh provisional span is created and injected, andhandleStepStartedlater adopts it — the gateway span then pairs exactly with its own step's LLM span; - if the entry is replaced (this step's
session.step.startedwas processed during the window), that span is injected directly; - if nothing changes within the window, the request is a genuine in-flight retry and the current span is injected, matching the previous behavior.
#147 also fixes a related gap: uncorrelated auxiliary requests (e.g. title generation) previously fell through without any injection when no execution was active, leaking the host process's own traceparent as the parent of the resulting gateway span.
Activity
enternal111 commentedon Nov 16, 2026
Cross-linking: the settle-window fix and the auxiliary-request fallback live in #147 (commits 2995897 and db85949). E2E verification there shows all gateway spans pairing exactly with their own step's LLM span in multi-step sessions.