Repository navigation
fix: keep turn spans parented correctly under event-ordering races #147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
dbe104b
53bb292
5c2d260
b116d73
bf9b4e3
cf7de4e
1781a6e
2995897
db85949
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,5 +86,5 @@ | |
| }, | ||
| "type": "module", | ||
| "types": "dist/index.d.ts", | ||
| "version": "2.1.0" | ||
| } | ||
| "version": "2.1.0-contentgate.23" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ import { AGENT_NAME, INPUT_MIME_TYPE, INPUT_VALUE, LLM_INPUT_MESSAGES, LLM_MODEL | |
| import type { HandlerContext } from "../types.ts" | ||
| import { injectTraceContext } from "../trace-context.ts" | ||
| import { genAiProviderName, isTraceEnabled, resolveRunContext, setBoundedMap } from "../util.ts" | ||
| import { ensureRunSpan } from "./session.ts" | ||
|
|
||
| /** The `session.hook("model.request", ...)` event shape consumed by this handler. */ | ||
| export type ModelRequestEvent = { | ||
|
|
@@ -30,6 +31,11 @@ function textParts(parts: readonly unknown[]): string { | |
| .slice(0, 1_000) | ||
| } | ||
|
|
||
| /** How long a primary model request waits for queued step events before trusting the recorded active LLM span. */ | ||
| const ACTIVE_LLM_SETTLE_MS = 200 | ||
| /** Granularity of the active-LLM settle loop. */ | ||
| const ACTIVE_LLM_SETTLE_TICK_MS = 15 | ||
|
|
||
| /** Stores a bounded text-only preview of the primary model-visible request. */ | ||
| export function captureModelContext(event: ModelVisibleContextEvent, ctx: HandlerContext): void { | ||
| const entries = [ | ||
|
|
@@ -66,13 +72,49 @@ export function applyModelContext( | |
|
|
||
| /** Injects the active LLM span's W3C trace context into outbound model requests. */ | ||
| export async function handleModelRequest(event: ModelRequestEvent, ctx: HandlerContext): Promise<void> { | ||
| if (event.kind !== "primary") return | ||
| const providerID = event.model.providerID | ||
| const propagate = ctx.tracePropagationProviders.has(providerID) || ctx.tracePropagationProviders.has("*") | ||
| if (!propagate && !ctx.tracing.modelContexts.has(event.sessionID)) return | ||
| if (!isTraceEnabled("llm", ctx)) return | ||
| await ctx.tracing.eventQueue | ||
| if (event.kind !== "primary") { | ||
| if (propagate && isTraceEnabled("session", ctx)) { | ||
| const dispatch = ctx.tracing.subagentParents.get(event.sessionID) | ||
| if (dispatch) { | ||
| injectTraceContext(dispatch.spanContext, event.headers) | ||
| } else if (ctx.tracing.activeExecutions.has(event.sessionID)) { | ||
| ensureRunSpan(event.sessionID, Date.now(), ctx) | ||
| const runContext = ctx.tracing.runSpanContexts.get(event.sessionID) | ||
| if (runContext) injectTraceContext(runContext, event.headers) | ||
| } else { | ||
| const parentID = ctx.tracing.sessionIdentity.get(event.sessionID)?.parentID | ||
| const parentContext = parentID ? ctx.tracing.runSpanContexts.get(parentID) : undefined | ||
| const fallback = parentContext ?? ctx.tracing.runSpanContexts.get(event.sessionID) | ||
| if (fallback) injectTraceContext(fallback, event.headers) | ||
| } | ||
| } | ||
| return | ||
| } | ||
| if (!isTraceEnabled("llm", ctx)) return | ||
| ensureRunSpan(event.sessionID, Date.now(), ctx) | ||
| let active = ctx.tracing.activeLlm.get(event.sessionID) | ||
| if (active) { | ||
| // The next step's request can fire before session.step.ended of the previous step is | ||
| // delivered (opencode publishes session.step.started only after a forked snapshot that | ||
| // overlaps the provider request, and the durable bus lags the request loop), so the | ||
| // recorded active LLM span may already belong to a finished step. Wait briefly for the | ||
| // queued step events to land: if the entry is cleared or replaced we propagate the fresh | ||
| // context instead, and if nothing changes we are looking at a genuine in-flight retry. | ||
| const deadline = Date.now() + ACTIVE_LLM_SETTLE_MS | ||
| while (Date.now() < deadline) { | ||
| await new Promise((resolve) => setTimeout(resolve, ACTIVE_LLM_SETTLE_TICK_MS)) | ||
| await ctx.tracing.eventQueue | ||
| const current = ctx.tracing.activeLlm.get(event.sessionID) | ||
| if (!current || current.spanContext.spanId !== active.spanContext.spanId) { | ||
|
Comment on lines
+108
to
+112
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
| active = current | ||
| break | ||
| } | ||
| } | ||
| } | ||
| if (!active) { | ||
| const span = ctx.tracer.startSpan( | ||
| `${ctx.tracePrefix}llm`, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.