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
15 changes: 13 additions & 2 deletions apps/daemon/src/runtimes/runs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ function summarizeAssistantMessageEvents(events) {
let upstreamErrorCount = 0;
let provider;
let model;
let usageProvider;
let usageModel;
let fallbackOrdinal = 0;
const countErrorClass = (value) => {
if (value === 'rate_limited') rateLimitedCount += 1;
Expand All @@ -212,6 +214,15 @@ function summarizeAssistantMessageEvents(events) {
for (const record of events) {
if (record?.event !== 'agent' || !record.data || typeof record.data !== 'object') continue;
const data = record.data;
if (data.type === 'usage') {
if (typeof data.provider === 'string' && data.provider.trim()) {
usageProvider = data.provider.trim();
}
if (typeof data.model === 'string' && data.model.trim()) {
usageModel = data.model.trim();
}
continue;
}
if (data.type !== 'diagnostic') continue;
if (data.name === 'model_retry') {
retryCount += 1;
Expand Down Expand Up @@ -282,8 +293,8 @@ function summarizeAssistantMessageEvents(events) {
rateLimitedCount,
timeoutCount,
upstreamErrorCount,
provider,
model,
provider: provider ?? usageProvider,
model: model ?? usageModel,
};
}

Expand Down
58 changes: 58 additions & 0 deletions apps/daemon/tests/runtimes/runs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,64 @@ describe('chat run service shutdown', () => {
vi.useRealTimers();
});

it('uses runtime usage attribution when the adapter has no message lifecycle', () => {
const runs = createRuns();
const run = runs.create({
projectId: 'project-1',
conversationId: 'conv-1',
agentId: 'deepseek-harness',
}) as any;
run.model = 'default';
runs.emit(run, 'agent', {
type: 'usage',
provider: 'deepseek-official',
model: 'deepseek-v4-flash',
usage: { input_tokens: 10, output_tokens: 2 },
});
runs.finish(run, 'succeeded', 0, null);

const diagnostics = runs.statusBody(run).executionDiagnostics;
expect(diagnostics?.environment).toMatchObject({
requestedModel: { state: 'available', value: 'default' },
provider: { state: 'available', value: 'deepseek-official' },
resolvedModel: { state: 'available', value: 'deepseek-v4-flash' },
});
expect(diagnostics?.assistantMessages.count).toMatchObject({
state: 'not_collected',
missingReason: 'assistant_message_lifecycle_not_exposed_by_runtime',
});
});

it('keeps lifecycle attribution ahead of the usage fallback', () => {
const runs = createRuns();
const run = runs.create({
projectId: 'project-1',
conversationId: 'conv-1',
agentId: 'amr',
}) as any;
runs.emit(run, 'agent', {
type: 'usage',
provider: 'usage-provider',
model: 'usage-model',
usage: { input_tokens: 10, output_tokens: 2 },
});
runs.emit(run, 'agent', {
type: 'diagnostic',
name: 'assistant_message_lifecycle',
phase: 'end',
status: 'completed',
assistantMessageIndex: 1,
provider: 'lifecycle-provider',
model: 'lifecycle-model',
});
runs.finish(run, 'succeeded', 0, null);

expect(runs.statusBody(run).executionDiagnostics?.environment).toMatchObject({
provider: { state: 'available', value: 'lifecycle-provider' },
resolvedModel: { state: 'available', value: 'lifecycle-model' },
});
});

it('keeps model-step percentiles unavailable until the documented sample minimum', () => {
const runs = createRuns();
const run = runs.create({ projectId: 'project-1', conversationId: 'conv-1', agentId: 'amr' }) as any;
Expand Down
Loading