-
Notifications
You must be signed in to change notification settings - Fork 0
fix(spans): propagate gen_ai.conversation.id to execute_tool spans #108
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // SPDX-FileCopyrightText: 2026 CoreWeave, Inc. | ||
| // SPDX-License-Identifier: MIT | ||
| // SPDX-PackageName: weave-claude-code | ||
|
|
||
| // Regression test: a lost root span (hard crash) orphaned already-exported | ||
| // tool spans that had no conversation id of their own. | ||
|
|
||
| import { test } from 'node:test'; | ||
| import assert from 'node:assert/strict'; | ||
| import * as fs from 'node:fs'; | ||
| import * as os from 'node:os'; | ||
| import * as path from 'node:path'; | ||
| import { | ||
| BasicTracerProvider, | ||
| InMemorySpanExporter, | ||
| SimpleSpanProcessor, | ||
| } from '@opentelemetry/sdk-trace-base'; | ||
| import { GlobalDaemon } from '../src/daemon.ts'; | ||
| import { ATTR, OP } from '../src/genaiSpans.ts'; | ||
|
|
||
| function setupTracer() { | ||
| const exporter = new InMemorySpanExporter(); | ||
| const provider = new BasicTracerProvider({ spanProcessors: [new SimpleSpanProcessor(exporter)] }); | ||
| return { tracer: provider.getTracer('test'), exporter, provider }; | ||
| } | ||
|
|
||
| function makeDaemon(tracer: unknown) { | ||
| const logFile = path.join(os.tmpdir(), `wcp-toolconv-${process.pid}.log`); | ||
| const d = new GlobalDaemon('/tmp/unused-toolconv.sock', logFile, 'e/p', 'k', 'https://x', false, 'claude-code'); | ||
| (d as unknown as { tracer: unknown }).tracer = tracer; | ||
| return d as unknown as { routeEvent(p: Record<string, unknown>): Promise<void> }; | ||
| } | ||
|
|
||
| test('execute_tool spans carry gen_ai.conversation.id so they stitch even without their root', async () => { | ||
| const sid = 'sess-tool-conv'; | ||
| const dir = fs.mkdtempSync(path.join(os.homedir(), '.weave-toolconv-itest-')); | ||
| const file = path.join(dir, `${sid}.jsonl`); | ||
| fs.writeFileSync(file, JSON.stringify({ type: 'user', timestamp: '2026-01-01T00:00:00.000Z', message: { role: 'user', content: [{ type: 'text', text: 'go' }] } }) + '\n'); | ||
|
|
||
| const { tracer, exporter, provider } = setupTracer(); | ||
| const d = makeDaemon(tracer); | ||
| try { | ||
| await d.routeEvent({ hook_event_name: 'SessionStart', session_id: sid, transcript_path: file, source: 'startup', cwd: '/x' }); | ||
| await d.routeEvent({ hook_event_name: 'UserPromptSubmit', session_id: sid, prompt: 'go' }); | ||
| // The assistant response the tool_use belongs to must be in the transcript before | ||
| // PreToolUse, so the daemon can parent the tool span under the right chat span. | ||
| fs.appendFileSync(file, JSON.stringify({ type: 'assistant', timestamp: '2026-01-01T00:00:02.000Z', message: { role: 'assistant', id: 'msgA', model: 'claude-opus-4-8', usage: { input_tokens: 1, output_tokens: 1 }, content: [{ type: 'tool_use', id: 'tool_1', name: 'Bash', input: { command: 'ls' } }], stop_reason: 'tool_use' } }) + '\n'); | ||
| await d.routeEvent({ hook_event_name: 'PreToolUse', session_id: sid, tool_use_id: 'tool_1', tool_name: 'Bash', tool_input: { command: 'ls' } }); | ||
| await d.routeEvent({ hook_event_name: 'PostToolUse', session_id: sid, tool_use_id: 'tool_1', tool_response: 'ok' }); | ||
| await provider.forceFlush(); | ||
|
|
||
| const tool = exporter.getFinishedSpans().find((s) => s.attributes[ATTR.OPERATION_NAME] === OP.EXECUTE_TOOL); | ||
| assert.ok(tool, 'tool span exported'); | ||
| // conversationId === sessionId for a fresh (non-resumed) session. | ||
| assert.equal(tool.attributes[ATTR.CONVERSATION_ID], sid, 'tool span carries the conversation id'); | ||
| } finally { | ||
| fs.rmSync(dir, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.