Skip to content
Closed
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
2 changes: 2 additions & 0 deletions packages/opentelemetry/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ export function createOpenTelemetryInstrumentation(
[ATTR.providerName]: request.providerName,
[ATTR.requestModel]: request.requestedModel,
[ATTR.requestStream]: true,
...(event.agentName ? { [ATTR.agentName]: event.agentName } : {}),
...(event.conversationId ? { [ATTR.conversationId]: event.conversationId } : {}),
...(request.reasoningLevel ? { [ATTR.reasoningLevel]: request.reasoningLevel } : {}),
...(request.maxTokens !== undefined ? { [ATTR.maxTokens]: request.maxTokens } : {}),
Expand Down Expand Up @@ -411,6 +412,7 @@ export function createOpenTelemetryInstrumentation(
env: {},
req: undefined,
log: { info() {}, warn() {}, error() {} },
emitData() {},
}));
const tracked = runs.get(runKey(operation));
if (tracked) tracked.awaitingWorkflowObservation = true;
Expand Down
36 changes: 36 additions & 0 deletions packages/opentelemetry/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ describe('createOpenTelemetryInstrumentation()', () => {
observation({
type: 'turn_request',
instanceId: 'instance-1',
agentName: 'assistant',
conversationId: 'conv_01KT3P3GZGFBCKHKMQ11A7H2HW',
operationId: 'op-1',
turnId: 'turn-1',
Expand Down Expand Up @@ -152,6 +153,8 @@ describe('createOpenTelemetryInstrumentation()', () => {
'gen_ai.provider.name': 'gateway',
'gen_ai.request.model': 'model-1',
'gen_ai.request.stream': true,
'gen_ai.agent.name': 'assistant',
'flue.agent.name': 'assistant',
'gen_ai.response.id': 'resp-1',
'gen_ai.response.model': 'model-actual',
'gen_ai.usage.input_tokens': 5,
Expand Down Expand Up @@ -437,6 +440,39 @@ describe('createOpenTelemetryInstrumentation()', () => {
expect(tracer.spans[0]?.attributes['gen_ai.tool.call.id']).toBe('call-1');
});

it('uses the closest agent name on delegated task and chat spans', () => {
const tracer = new RecordingTracer();
const instrumentation = createOpenTelemetryInstrumentation({ tracer: tracer as unknown as Tracer });
instrumentation.observe(observation({
type: 'task_start',
taskId: 'task-1',
prompt: 'delegate',
agent: 'researcher',
agentName: 'researcher',
conversationId: 'conv_child',
}), ctx);
instrumentation.observe(observation({
type: 'turn_request',
taskId: 'task-1',
turnId: 'turn-1',
purpose: 'agent',
agentName: 'researcher',
conversationId: 'conv_child',
request: {
providerId: 'p',
providerName: 'p',
requestedModel: 'm',
api: 'a',
input: { messages: [] },
},
}), ctx);

expect(tracer.spans.map((span) => span.name)).toEqual(['invoke_agent researcher', 'chat m']);
expect(tracer.spans[0]?.attributes['gen_ai.agent.name']).toBe('researcher');
expect(tracer.spans[1]?.attributes['gen_ai.agent.name']).toBe('researcher');
expect(tracer.spans[1]?.attributes['flue.agent.name']).toBe('researcher');
});

it('creates one span when a tool start is duplicated', () => {
const tracer = new RecordingTracer();
const instrumentation = createOpenTelemetryInstrumentation({ tracer: tracer as unknown as Tracer });
Expand Down
4 changes: 3 additions & 1 deletion packages/runtime/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ export function createFlueContext(config: FlueContextConfig): FlueContextInterna
...(config.runId === undefined ? { instanceId: config.id } : { runId: config.runId }),
...(config.dispatchId === undefined ? {} : { dispatchId: config.dispatchId }),
...(submissionId === undefined ? {} : { submissionId }),
...(config.agentName === undefined ? {} : { agentName: config.agentName }),
...(event.agentName === undefined && config.agentName !== undefined
? { agentName: config.agentName }
: {}),
v: 3,
eventIndex: eventIndex++,
timestamp: new Date().toISOString(),
Expand Down
9 changes: 8 additions & 1 deletion packages/runtime/src/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,11 @@ export class Harness implements FlueHarness {
const data = createEmptySessionData();
const eventCallback: FlueEventInputCallback | undefined = this.eventCallback
? (event, observation) => {
const agentName = event.agentName ?? taskAgent?.name;
this.eventCallback?.({
...event,
harness: event.harness ?? this.name,
...(agentName ? { agentName } : {}),
parentSession: event.parentSession ?? options.parentSession,
taskId: event.taskId ?? options.taskId,
}, observation);
Expand All @@ -288,7 +290,12 @@ export class Harness implements FlueHarness {
actions: taskConfig.actions ?? [],
createActionHarness: (actionOptions) => this.createActionHarness(actionOptions),
scopeSignal: this.scopeAbortController.signal,
executionContext: { ...this.executionContext, harness: this.name, taskId: options.taskId },
executionContext: {
...this.executionContext,
harness: this.name,
taskId: options.taskId,
...(taskAgent?.name ? { agentName: taskAgent.name } : {}),
},
});
}

Expand Down
1 change: 1 addition & 0 deletions packages/runtime/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1882,6 +1882,7 @@ export class Session implements FlueSession, AgentSubmissionSession {
taskId,
prompt: text,
agent: taskAgent?.name,
...(taskAgent?.name ? { agentName: taskAgent.name } : {}),
cwd: options?.cwd,
parentSession: this.name,
session: child.name,
Expand Down
35 changes: 33 additions & 2 deletions packages/runtime/test/session-operations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ function createProvider(models?: FauxModelDefinition[]): FauxProviderRegistratio

function createContext(
provider: FauxProviderRegistration,
options: { env?: SessionEnv; store?: SessionStore } = {},
options: { env?: SessionEnv; store?: SessionStore; agentName?: string } = {},
) {
return createFlueContext({
id: 'session-operations-instance',
agentName: options.agentName,
env: {},
agentConfig: {
resolveModel: (specifier) => {
Expand Down Expand Up @@ -977,7 +978,17 @@ describe('session.task()', () => {
return fauxAssistantMessage('Delegated profile response.');
},
]);
const ctx = createContext(provider);
const ctx = createContext(provider, { agentName: 'root-agent' });
const events: Array<{ type: string; agentName?: string; agent?: string; taskId?: string }> = [];
ctx.subscribeEvent((event) => {
if (
event.type === 'task_start' ||
event.type === 'operation_start' ||
event.type === 'turn_request'
) {
events.push(event);
}
});
const harness = await ctx.initializeRootHarness(
defineAgent(() => ({
model: `${provider.getModel().provider}/parent-model`,
Expand All @@ -999,6 +1010,26 @@ describe('session.task()', () => {
text: 'Delegated profile response.',
model: { provider: provider.getModel().provider, id: 'delegate-model' },
});
const taskStart = events.find((event) => event.type === 'task_start');
const childOperationStart = events.find(
(event) => event.type === 'operation_start' && event.taskId === taskStart?.taskId,
);
const childTurnRequest = events.find(
(event) => event.type === 'turn_request' && event.taskId === taskStart?.taskId,
);
expect(taskStart).toMatchObject({
type: 'task_start',
agent: 'code-reviewer',
agentName: 'code-reviewer',
});
expect(childOperationStart).toMatchObject({
type: 'operation_start',
agentName: 'code-reviewer',
});
expect(childTurnRequest).toMatchObject({
type: 'turn_request',
agentName: 'code-reviewer',
});
});

it('gives a subagent no parent tools or skills when its profile declares only name and instructions', async () => {
Expand Down
Loading