From 1fc61a23a7a0ff89a5ea60bc428c52afabf90c04 Mon Sep 17 00:00:00 2001 From: immutable dcramer Date: Wed, 17 Jun 2026 21:30:34 +0000 Subject: [PATCH] fix(tracing): seed log context before route-thinking to fix gen_ai.conversation.id on gen_ai.chat spans In the queue-worker path (POST /api/internal/agent/continue), spanContext is computed before selectTurnThinkingLevel runs but is not yet installed into AsyncLocalStorage. As a result, the gen_ai.chat span emitted by the Haiku router call does not inherit gen_ai.conversation.id, violating OTel GenAI semconv which marks that attribute Conditionally Required on all gen_ai.chat spans. The Slack inline path is unaffected because slack-runtime.ts wraps the full turn in withSpan('chat.turn', ..., context) which seeds ALS with conversationId before selectTurnThinkingLevel runs. Fix: wrap the awaited selectTurnThinkingLevel call in withLogContext(spanContext) so the route-thinking subtree (chat.route_thinking + its gen_ai.chat span) inherits gen_ai.conversation.id in both execution paths. Co-Authored-By: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com> --- packages/junior/src/chat/respond.ts | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/packages/junior/src/chat/respond.ts b/packages/junior/src/chat/respond.ts index 48efca037..d8698a641 100644 --- a/packages/junior/src/chat/respond.ts +++ b/packages/junior/src/chat/respond.ts @@ -20,6 +20,7 @@ import { serializeGenAiAttribute, setSpanAttributes, setTags, + withLogContext, withSpan, type LogContext, } from "@/chat/logging"; @@ -924,19 +925,21 @@ export async function generateAssistantReply( } as PiMessage, ]; - thinkingSelection = await selectTurnThinkingLevel({ - completeObject, - conversationContext: context.conversationContext, - context: { - threadId: context.correlation?.threadId, - channelId: context.correlation?.channelId, - requesterId: context.correlation?.requesterId, - runId: context.correlation?.runId, - }, - currentTurnBlocks: routerBlocks, - fastModelId: botConfig.fastModelId, - messageText: userInput, - }); + thinkingSelection = await withLogContext(spanContext, () => + selectTurnThinkingLevel({ + completeObject, + conversationContext: context.conversationContext, + context: { + threadId: context.correlation?.threadId, + channelId: context.correlation?.channelId, + requesterId: context.correlation?.requesterId, + runId: context.correlation?.runId, + }, + currentTurnBlocks: routerBlocks, + fastModelId: botConfig.fastModelId, + messageText: userInput, + }), + ); setSpanAttributes({ "gen_ai.request.model": botConfig.modelId, "app.ai.reasoning_effort": thinkingSelection.thinkingLevel,