feat: add file change tracking to session metrics (#6094)

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: Jacob Richman <jacob314@gmail.com>
This commit is contained in:
Arya Gummadi 2025-08-18 22:57:53 -07:00 committed by GitHub
parent da396bd566
commit 8f8082fe3d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 291 additions and 35 deletions

View File

@ -60,6 +60,10 @@ describe('<SessionSummaryDisplay />', () => {
totalDecisions: { accept: 0, reject: 0, modify: 0 },
byName: {},
},
files: {
totalLinesAdded: 42,
totalLinesRemoved: 15,
},
};
const { lastFrame } = renderWithMockedStats(metrics);

View File

@ -50,6 +50,10 @@ describe('<StatsDisplay />', () => {
totalDecisions: { accept: 0, reject: 0, modify: 0 },
byName: {},
},
files: {
totalLinesAdded: 0,
totalLinesRemoved: 0,
},
};
const { lastFrame } = renderWithMockedStats(zeroMetrics);
@ -96,6 +100,10 @@ describe('<StatsDisplay />', () => {
totalDecisions: { accept: 0, reject: 0, modify: 0 },
byName: {},
},
files: {
totalLinesAdded: 0,
totalLinesRemoved: 0,
},
};
const { lastFrame } = renderWithMockedStats(metrics);
@ -139,6 +147,10 @@ describe('<StatsDisplay />', () => {
},
},
},
files: {
totalLinesAdded: 0,
totalLinesRemoved: 0,
},
};
const { lastFrame } = renderWithMockedStats(metrics);
@ -172,6 +184,10 @@ describe('<StatsDisplay />', () => {
},
},
},
files: {
totalLinesAdded: 0,
totalLinesRemoved: 0,
},
};
const { lastFrame } = renderWithMockedStats(metrics);
@ -206,6 +222,10 @@ describe('<StatsDisplay />', () => {
totalDecisions: { accept: 0, reject: 0, modify: 0 },
byName: {},
},
files: {
totalLinesAdded: 0,
totalLinesRemoved: 0,
},
};
const { lastFrame } = renderWithMockedStats(metrics);
@ -228,6 +248,10 @@ describe('<StatsDisplay />', () => {
totalDecisions: { accept: 0, reject: 0, modify: 0 },
byName: {},
},
files: {
totalLinesAdded: 0,
totalLinesRemoved: 0,
},
};
const { lastFrame } = renderWithMockedStats(metrics);
expect(lastFrame()).toMatchSnapshot();
@ -244,6 +268,10 @@ describe('<StatsDisplay />', () => {
totalDecisions: { accept: 0, reject: 0, modify: 0 },
byName: {},
},
files: {
totalLinesAdded: 0,
totalLinesRemoved: 0,
},
};
const { lastFrame } = renderWithMockedStats(metrics);
expect(lastFrame()).toMatchSnapshot();
@ -260,12 +288,68 @@ describe('<StatsDisplay />', () => {
totalDecisions: { accept: 0, reject: 0, modify: 0 },
byName: {},
},
files: {
totalLinesAdded: 0,
totalLinesRemoved: 0,
},
};
const { lastFrame } = renderWithMockedStats(metrics);
expect(lastFrame()).toMatchSnapshot();
});
});
describe('Code Changes Display', () => {
it('displays Code Changes when line counts are present', () => {
const metrics: SessionMetrics = {
models: {},
tools: {
totalCalls: 1,
totalSuccess: 1,
totalFail: 0,
totalDurationMs: 100,
totalDecisions: { accept: 0, reject: 0, modify: 0 },
byName: {},
},
files: {
totalLinesAdded: 42,
totalLinesRemoved: 18,
},
};
const { lastFrame } = renderWithMockedStats(metrics);
const output = lastFrame();
expect(output).toContain('Code Changes:');
expect(output).toContain('+42');
expect(output).toContain('-18');
expect(output).toMatchSnapshot();
});
it('hides Code Changes when no lines are added or removed', () => {
const metrics: SessionMetrics = {
models: {},
tools: {
totalCalls: 1,
totalSuccess: 1,
totalFail: 0,
totalDurationMs: 100,
totalDecisions: { accept: 0, reject: 0, modify: 0 },
byName: {},
},
files: {
totalLinesAdded: 0,
totalLinesRemoved: 0,
},
};
const { lastFrame } = renderWithMockedStats(metrics);
const output = lastFrame();
expect(output).not.toContain('Code Changes:');
expect(output).toMatchSnapshot();
});
});
describe('Title Rendering', () => {
const zeroMetrics: SessionMetrics = {
models: {},
@ -277,6 +361,10 @@ describe('<StatsDisplay />', () => {
totalDecisions: { accept: 0, reject: 0, modify: 0 },
byName: {},
},
files: {
totalLinesAdded: 0,
totalLinesRemoved: 0,
},
};
it('renders the default title when no title prop is provided', () => {

View File

@ -7,7 +7,7 @@
import React from 'react';
import { Box, Text } from 'ink';
import Gradient from 'ink-gradient';
import { Colors } from '../colors.js';
import { theme } from '../semantic-colors.js';
import { formatDuration } from '../utils/formatters.js';
import { useSessionStats, ModelMetrics } from '../contexts/SessionContext.js';
import {
@ -29,7 +29,7 @@ const StatRow: React.FC<StatRowProps> = ({ title, children }) => (
<Box>
{/* Fixed width for the label creates a clean "gutter" for alignment */}
<Box width={28}>
<Text color={Colors.LightBlue}>{title}</Text>
<Text color={theme.text.link}>{title}</Text>
</Box>
{children}
</Box>
@ -111,12 +111,12 @@ const ModelUsageTable: React.FC<{
<Text>{modelMetrics.api.totalRequests}</Text>
</Box>
<Box width={inputTokensWidth} justifyContent="flex-end">
<Text color={Colors.AccentYellow}>
<Text color={theme.status.warning}>
{modelMetrics.tokens.prompt.toLocaleString()}
</Text>
</Box>
<Box width={outputTokensWidth} justifyContent="flex-end">
<Text color={Colors.AccentYellow}>
<Text color={theme.status.warning}>
{modelMetrics.tokens.candidates.toLocaleString()}
</Text>
</Box>
@ -125,12 +125,12 @@ const ModelUsageTable: React.FC<{
{cacheEfficiency > 0 && (
<Box flexDirection="column" marginTop={1}>
<Text>
<Text color={Colors.AccentGreen}>Savings Highlight:</Text>{' '}
<Text color={theme.status.success}>Savings Highlight:</Text>{' '}
{totalCachedTokens.toLocaleString()} ({cacheEfficiency.toFixed(1)}
%) of input tokens were served from the cache, reducing costs.
</Text>
<Box height={1} />
<Text color={Colors.Gray}>
<Text color={theme.text.secondary}>
» Tip: For a full token breakdown, run `/stats model`.
</Text>
</Box>
@ -150,7 +150,7 @@ export const StatsDisplay: React.FC<StatsDisplayProps> = ({
}) => {
const { stats } = useSessionStats();
const { metrics } = stats;
const { models, tools } = metrics;
const { models, tools, files } = metrics;
const computed = computeSessionStats(metrics);
const successThresholds = {
@ -169,18 +169,18 @@ export const StatsDisplay: React.FC<StatsDisplayProps> = ({
const renderTitle = () => {
if (title) {
return Colors.GradientColors && Colors.GradientColors.length > 0 ? (
<Gradient colors={Colors.GradientColors}>
return theme.ui.gradient && theme.ui.gradient.length > 0 ? (
<Gradient colors={theme.ui.gradient}>
<Text bold>{title}</Text>
</Gradient>
) : (
<Text bold color={Colors.AccentPurple}>
<Text bold color={theme.text.accent}>
{title}
</Text>
);
}
return (
<Text bold color={Colors.AccentPurple}>
<Text bold color={theme.text.accent}>
Session Stats
</Text>
);
@ -189,7 +189,7 @@ export const StatsDisplay: React.FC<StatsDisplayProps> = ({
return (
<Box
borderStyle="round"
borderColor={Colors.Gray}
borderColor={theme.border.default}
flexDirection="column"
paddingY={1}
paddingX={2}
@ -204,8 +204,8 @@ export const StatsDisplay: React.FC<StatsDisplayProps> = ({
<StatRow title="Tool Calls:">
<Text>
{tools.totalCalls} ({' '}
<Text color={Colors.AccentGreen}> {tools.totalSuccess}</Text>{' '}
<Text color={Colors.AccentRed}> {tools.totalFail}</Text> )
<Text color={theme.status.success}> {tools.totalSuccess}</Text>{' '}
<Text color={theme.status.error}> {tools.totalFail}</Text> )
</Text>
</StatRow>
<StatRow title="Success Rate:">
@ -215,12 +215,25 @@ export const StatsDisplay: React.FC<StatsDisplayProps> = ({
<StatRow title="User Agreement:">
<Text color={agreementColor}>
{computed.agreementRate.toFixed(1)}%{' '}
<Text color={Colors.Gray}>
<Text color={theme.text.secondary}>
({computed.totalDecisions} reviewed)
</Text>
</Text>
</StatRow>
)}
{files &&
(files.totalLinesAdded > 0 || files.totalLinesRemoved > 0) && (
<StatRow title="Code Changes:">
<Text>
<Text color={theme.status.success}>
+{files.totalLinesAdded}
</Text>{' '}
<Text color={theme.status.error}>
-{files.totalLinesRemoved}
</Text>
</Text>
</StatRow>
)}
</Section>
<Section title="Performance">
@ -233,7 +246,7 @@ export const StatsDisplay: React.FC<StatsDisplayProps> = ({
<SubStatRow title="API Time:">
<Text>
{formatDuration(computed.totalApiTime)}{' '}
<Text color={Colors.Gray}>
<Text color={theme.text.secondary}>
({computed.apiTimePercent.toFixed(1)}%)
</Text>
</Text>
@ -241,7 +254,7 @@ export const StatsDisplay: React.FC<StatsDisplayProps> = ({
<SubStatRow title="Tool Time:">
<Text>
{formatDuration(computed.totalToolTime)}{' '}
<Text color={Colors.Gray}>
<Text color={theme.text.secondary}>
({computed.toolTimePercent.toFixed(1)}%)
</Text>
</Text>

View File

@ -9,6 +9,7 @@ exports[`<SessionSummaryDisplay /> > renders the summary display with a title 1`
│ Session ID: │
│ Tool Calls: 0 ( ✔ 0 ✖ 0 ) │
│ Success Rate: 0.0% │
│ Code Changes: +42 -15 │
│ │
│ Performance │
│ Wall Time: 1h 23m 45s │

View File

@ -1,5 +1,46 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`<StatsDisplay /> > Code Changes Display > displays Code Changes when line counts are present 1`] = `
"╭──────────────────────────────────────────────────────────────────────────────────────────────────╮
│ │
│ Session Stats │
│ │
│ Interaction Summary │
│ Session ID: test-session-id │
│ Tool Calls: 1 ( ✔ 1 ✖ 0 ) │
│ Success Rate: 100.0% │
│ Code Changes: +42 -18 │
│ │
│ Performance │
│ Wall Time: 1s │
│ Agent Active: 100ms │
│ » API Time: 0s (0.0%) │
│ » Tool Time: 100ms (100.0%) │
│ │
│ │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯"
`;
exports[`<StatsDisplay /> > Code Changes Display > hides Code Changes when no lines are added or removed 1`] = `
"╭──────────────────────────────────────────────────────────────────────────────────────────────────╮
│ │
│ Session Stats │
│ │
│ Interaction Summary │
│ Session ID: test-session-id │
│ Tool Calls: 1 ( ✔ 1 ✖ 0 ) │
│ Success Rate: 100.0% │
│ │
│ Performance │
│ Wall Time: 1s │
│ Agent Active: 100ms │
│ » API Time: 0s (0.0%) │
│ » Tool Time: 100ms (100.0%) │
│ │
│ │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯"
`;
exports[`<StatsDisplay /> > Conditional Color Tests > renders success rate in green for high values 1`] = `
"╭──────────────────────────────────────────────────────────────────────────────────────────────────╮
│ │

View File

@ -44,6 +44,8 @@ export interface ComputedSessionStats {
agreementRate: number;
totalCachedTokens: number;
totalPromptTokens: number;
totalLinesAdded: number;
totalLinesRemoved: number;
}
// Defines the final "value" of our context, including the state

View File

@ -121,6 +121,10 @@ describe('computeSessionStats', () => {
totalDecisions: { accept: 0, reject: 0, modify: 0 },
byName: {},
},
files: {
totalLinesAdded: 0,
totalLinesRemoved: 0,
},
};
const result = computeSessionStats(metrics);
@ -137,6 +141,8 @@ describe('computeSessionStats', () => {
agreementRate: 0,
totalPromptTokens: 0,
totalCachedTokens: 0,
totalLinesAdded: 0,
totalLinesRemoved: 0,
});
});
@ -163,6 +169,10 @@ describe('computeSessionStats', () => {
totalDecisions: { accept: 0, reject: 0, modify: 0 },
byName: {},
},
files: {
totalLinesAdded: 0,
totalLinesRemoved: 0,
},
};
const result = computeSessionStats(metrics);
@ -197,6 +207,10 @@ describe('computeSessionStats', () => {
totalDecisions: { accept: 0, reject: 0, modify: 0 },
byName: {},
},
files: {
totalLinesAdded: 0,
totalLinesRemoved: 0,
},
};
const result = computeSessionStats(metrics);
@ -215,6 +229,10 @@ describe('computeSessionStats', () => {
totalDecisions: { accept: 6, reject: 2, modify: 2 },
byName: {},
},
files: {
totalLinesAdded: 0,
totalLinesRemoved: 0,
},
};
const result = computeSessionStats(metrics);
@ -234,6 +252,10 @@ describe('computeSessionStats', () => {
totalDecisions: { accept: 0, reject: 0, modify: 0 },
byName: {},
},
files: {
totalLinesAdded: 0,
totalLinesRemoved: 0,
},
};
const result = computeSessionStats(metrics);
@ -244,4 +266,27 @@ describe('computeSessionStats', () => {
expect(result.successRate).toBe(0);
expect(result.agreementRate).toBe(0);
});
it('should correctly include line counts', () => {
const metrics: SessionMetrics = {
models: {},
tools: {
totalCalls: 0,
totalSuccess: 0,
totalFail: 0,
totalDurationMs: 0,
totalDecisions: { accept: 0, reject: 0, modify: 0 },
byName: {},
},
files: {
totalLinesAdded: 42,
totalLinesRemoved: 18,
},
};
const result = computeSessionStats(metrics);
expect(result.totalLinesAdded).toBe(42);
expect(result.totalLinesRemoved).toBe(18);
});
});

View File

@ -34,7 +34,7 @@ export function calculateCacheHitRate(metrics: ModelMetrics): number {
export const computeSessionStats = (
metrics: SessionMetrics,
): ComputedSessionStats => {
const { models, tools } = metrics;
const { models, tools, files } = metrics;
const totalApiTime = Object.values(models).reduce(
(acc, model) => acc + model.api.totalLatencyMs,
0,
@ -80,5 +80,7 @@ export const computeSessionStats = (
agreementRate,
totalCachedTokens,
totalPromptTokens,
totalLinesAdded: files.totalLinesAdded,
totalLinesRemoved: files.totalLinesRemoved,
};
};

View File

@ -108,6 +108,10 @@ describe('UiTelemetryService', () => {
},
byName: {},
},
files: {
totalLinesAdded: 0,
totalLinesRemoved: 0,
},
});
expect(service.getLastPromptTokenCount()).toBe(0);
});
@ -342,9 +346,9 @@ describe('UiTelemetryService', () => {
ToolConfirmationOutcome.ProceedOnce,
);
service.addEvent({
...JSON.parse(JSON.stringify(new ToolCallEvent(toolCall))),
...structuredClone(new ToolCallEvent(toolCall)),
'event.name': EVENT_TOOL_CALL,
});
} as ToolCallEvent & { 'event.name': typeof EVENT_TOOL_CALL });
const metrics = service.getMetrics();
const { tools } = metrics;
@ -376,9 +380,9 @@ describe('UiTelemetryService', () => {
ToolConfirmationOutcome.Cancel,
);
service.addEvent({
...JSON.parse(JSON.stringify(new ToolCallEvent(toolCall))),
...structuredClone(new ToolCallEvent(toolCall)),
'event.name': EVENT_TOOL_CALL,
});
} as ToolCallEvent & { 'event.name': typeof EVENT_TOOL_CALL });
const metrics = service.getMetrics();
const { tools } = metrics;
@ -410,9 +414,9 @@ describe('UiTelemetryService', () => {
ToolConfirmationOutcome.ModifyWithEditor,
);
service.addEvent({
...JSON.parse(JSON.stringify(new ToolCallEvent(toolCall))),
...structuredClone(new ToolCallEvent(toolCall)),
'event.name': EVENT_TOOL_CALL,
});
} as ToolCallEvent & { 'event.name': typeof EVENT_TOOL_CALL });
const metrics = service.getMetrics();
const { tools } = metrics;
@ -426,9 +430,9 @@ describe('UiTelemetryService', () => {
it('should process a ToolCallEvent without a decision', () => {
const toolCall = createFakeCompletedToolCall('test_tool', true, 100);
service.addEvent({
...JSON.parse(JSON.stringify(new ToolCallEvent(toolCall))),
...structuredClone(new ToolCallEvent(toolCall)),
'event.name': EVENT_TOOL_CALL,
});
} as ToolCallEvent & { 'event.name': typeof EVENT_TOOL_CALL });
const metrics = service.getMetrics();
const { tools } = metrics;
@ -462,13 +466,13 @@ describe('UiTelemetryService', () => {
);
service.addEvent({
...JSON.parse(JSON.stringify(new ToolCallEvent(toolCall1))),
...structuredClone(new ToolCallEvent(toolCall1)),
'event.name': EVENT_TOOL_CALL,
});
} as ToolCallEvent & { 'event.name': typeof EVENT_TOOL_CALL });
service.addEvent({
...JSON.parse(JSON.stringify(new ToolCallEvent(toolCall2))),
...structuredClone(new ToolCallEvent(toolCall2)),
'event.name': EVENT_TOOL_CALL,
});
} as ToolCallEvent & { 'event.name': typeof EVENT_TOOL_CALL });
const metrics = service.getMetrics();
const { tools } = metrics;
@ -497,13 +501,13 @@ describe('UiTelemetryService', () => {
const toolCall1 = createFakeCompletedToolCall('tool_A', true, 100);
const toolCall2 = createFakeCompletedToolCall('tool_B', false, 200);
service.addEvent({
...JSON.parse(JSON.stringify(new ToolCallEvent(toolCall1))),
...structuredClone(new ToolCallEvent(toolCall1)),
'event.name': EVENT_TOOL_CALL,
});
} as ToolCallEvent & { 'event.name': typeof EVENT_TOOL_CALL });
service.addEvent({
...JSON.parse(JSON.stringify(new ToolCallEvent(toolCall2))),
...structuredClone(new ToolCallEvent(toolCall2)),
'event.name': EVENT_TOOL_CALL,
});
} as ToolCallEvent & { 'event.name': typeof EVENT_TOOL_CALL });
const metrics = service.getMetrics();
const { tools } = metrics;
@ -629,4 +633,42 @@ describe('UiTelemetryService', () => {
expect(spy).toHaveBeenCalledOnce();
});
});
describe('Tool Call Event with Line Count Metadata', () => {
it('should aggregate valid line count metadata', () => {
const toolCall = createFakeCompletedToolCall('test_tool', true, 100);
const event = {
...structuredClone(new ToolCallEvent(toolCall)),
'event.name': EVENT_TOOL_CALL,
metadata: {
ai_added_lines: 10,
ai_removed_lines: 5,
},
} as ToolCallEvent & { 'event.name': typeof EVENT_TOOL_CALL };
service.addEvent(event);
const metrics = service.getMetrics();
expect(metrics.files.totalLinesAdded).toBe(10);
expect(metrics.files.totalLinesRemoved).toBe(5);
});
it('should ignore null/undefined values in line count metadata', () => {
const toolCall = createFakeCompletedToolCall('test_tool', true, 100);
const event = {
...structuredClone(new ToolCallEvent(toolCall)),
'event.name': EVENT_TOOL_CALL,
metadata: {
ai_added_lines: null,
ai_removed_lines: undefined,
},
} as ToolCallEvent & { 'event.name': typeof EVENT_TOOL_CALL };
service.addEvent(event);
const metrics = service.getMetrics();
expect(metrics.files.totalLinesAdded).toBe(0);
expect(metrics.files.totalLinesRemoved).toBe(0);
});
});
});

View File

@ -63,6 +63,10 @@ export interface SessionMetrics {
};
byName: Record<string, ToolCallStats>;
};
files: {
totalLinesAdded: number;
totalLinesRemoved: number;
};
}
const createInitialModelMetrics = (): ModelMetrics => ({
@ -96,6 +100,10 @@ const createInitialMetrics = (): SessionMetrics => ({
},
byName: {},
},
files: {
totalLinesAdded: 0,
totalLinesRemoved: 0,
},
});
export class UiTelemetryService extends EventEmitter {
@ -171,7 +179,7 @@ export class UiTelemetryService extends EventEmitter {
}
private processToolCall(event: ToolCallEvent) {
const { tools } = this.#metrics;
const { tools, files } = this.#metrics;
tools.totalCalls++;
tools.totalDurationMs += event.duration_ms;
@ -209,6 +217,16 @@ export class UiTelemetryService extends EventEmitter {
tools.totalDecisions[event.decision]++;
toolStats.decisions[event.decision]++;
}
// Aggregate line count data from metadata
if (event.metadata) {
if (event.metadata['ai_added_lines'] !== undefined) {
files.totalLinesAdded += event.metadata['ai_added_lines'];
}
if (event.metadata['ai_removed_lines'] !== undefined) {
files.totalLinesRemoved += event.metadata['ai_removed_lines'];
}
}
}
}