Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,7 @@ export class GlobalDaemon {
toolName,
toolUseId,
toolInput,
conversationId: session.conversationId,
displayName: toolDisplayName(toolName, toolInput),
});
session.pendingToolCalls.set(toolUseId, { span: toolSpan, toolName, toolInput });
Expand Down
3 changes: 3 additions & 0 deletions src/genaiSpans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ type ToolSpanArgs = {
toolName: string;
toolUseId: string;
toolInput: Record<string, unknown>;
/** Stitching key — same as the enclosing turn's `gen_ai.conversation.id`. */
conversationId: string;
displayName?: string;
};

Expand All @@ -377,6 +379,7 @@ export function startToolSpan(tracer: Tracer, parentSpan: Span, args: ToolSpanAr
[ATTR.TOOL_NAME]: args.toolName,
[ATTR.TOOL_CALL_ID]: args.toolUseId,
[ATTR.TOOL_CALL_ARGUMENTS]: jsonStr(args.toolInput),
[ATTR.CONVERSATION_ID]: args.conversationId,
};
if (args.displayName) attrs[ATTR.WEAVE_DISPLAY_NAME] = args.displayName;

Expand Down
2 changes: 2 additions & 0 deletions tests/interleaved-assistant-spans.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ test('chat span parents per-block assistant_text and execute_tool children in tr
toolName: 'Edit',
toolUseId: 'toolu_01',
toolInput: { file_path: '/foo.ts' },
conversationId: 'conv-1',
});
t1.end();
emitAssistantTextSpan(tracer, chat, {
Expand All @@ -84,6 +85,7 @@ test('chat span parents per-block assistant_text and execute_tool children in tr
toolName: 'Edit',
toolUseId: 'toolu_02',
toolInput: { file_path: '/foo.test.ts' },
conversationId: 'conv-1',
});
t2.end();
emitAssistantTextSpan(tracer, chat, {
Expand Down
59 changes: 59 additions & 0 deletions tests/tool-span-conversation-id.test.ts
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();
Comment thread
rgao-coreweave marked this conversation as resolved.

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 });
}
});
Loading