Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(context-usage): extract and export context breakdown segment logic

Steven Enamakel2eec985d222a
diff --git a/app/src/features/conversations/aui/ContextUsage.test.tsx b/app/src/features/conversations/aui/ContextUsage.test.tsx
index 7952e51af6..cdc1b63159 100644
--- a/app/src/features/conversations/aui/ContextUsage.test.tsx
+++ b/app/src/features/conversations/aui/ContextUsage.test.tsx
@@ -6,7 +6,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
import { callCoreRpc } from '../../../services/coreRpcClient';
import chatRuntimeReducer, { hydrateThreadUsage } from '../../../store/chatRuntimeSlice';
-import { ContextUsage } from './ContextUsage';
+import { contextBreakdownSegments, ContextUsage } from './ContextUsage';
vi.mock('../../../services/coreRpcClient', () => ({ callCoreRpc: vi.fn() }));
@@ -120,3 +120,28 @@ describe('ContextUsage', () => {
expect(mockCall).toHaveBeenCalledTimes(2);
});
});
+
+describe('contextBreakdownSegments', () => {
+ const t = (key: string) => key.split('.').pop() ?? key;
+
+ it('folds repeated headings into one row and drops empty sections', () => {
+ const segments = contextBreakdownSegments(
+ {
+ sections: [
+ { label: '## Rules', bytes: 40, est_tokens: 10 },
+ { label: '### Rules', bytes: 80, est_tokens: 20 },
+ { label: '## Empty', bytes: 0, est_tokens: 0 },
+ { label: 'tools', bytes: 400, est_tokens: 100 },
+ ],
+ total_est_tokens: 130,
+ context_window: 0,
+ },
+ t
+ );
+
+ expect(segments.map(s => [s.label, s.tokens])).toEqual([
+ ['Rules', 30],
+ ['tools', 100],
+ ]);
+ });
+});
diff --git a/app/src/features/conversations/aui/ContextUsage.tsx b/app/src/features/conversations/aui/ContextUsage.tsx
index b8a4464f77..7dff905b5c 100644
--- a/app/src/features/conversations/aui/ContextUsage.tsx
+++ b/app/src/features/conversations/aui/ContextUsage.tsx
@@ -64,7 +64,7 @@ type BreakdownState =
* labels, strip markdown hashes off prompt headings, fold duplicate headings
* into one row (the element keys rows by label) and drop empty ones.
*/
-function toSegments(
+export function contextBreakdownSegments(
data: ContextBreakdownData,
t: (key: string) => string
): readonly ContextSegment[] {
@@ -163,7 +163,7 @@ export function ContextUsage({
const limit = breakdown.data.context_window > 0 ? breakdown.data.context_window : contextWindow;
body = (
<ContextBreakdown
- segments={toSegments(breakdown.data, t)}
+ segments={contextBreakdownSegments(breakdown.data, t)}
limit={limit}
title={t('conversations.composer.context.breakdownTitle')}
headroomLabel={t('conversations.composer.context.headroom')}