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
73 changes: 34 additions & 39 deletions src/ccstatusline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@
import chalk from 'chalk';

import { runTUI } from './tui';
import type {
SkillsMetrics,
SpeedMetrics,
TokenMetrics
} from './types';
import type { SkillsMetrics } from './types';
import type { RenderContext } from './types/RenderContext';
import type { StatusJSON } from './types/StatusJSON';
import { StatusJSONSchema } from './types/StatusJSON';
import { getVisibleText } from './utils/ansi';
import { updateColorMap } from './utils/colors';
import {
ZERO_COMPACTION_STATS,
getCompactionStats
} from './utils/compaction';
import { ZERO_COMPACTION_STATS } from './utils/compaction';
import {
getConfigLoadError,
initConfigPath,
Expand All @@ -27,11 +20,7 @@ import {
refreshGitReviewCacheFromCli
} from './utils/git-review-cache';
import { handleHookInput } from './utils/hook-handler';
import {
getSessionDuration,
getSpeedMetricsCollection,
getTokenMetrics
} from './utils/jsonl';
import { getTranscriptAnalysis } from './utils/jsonl';
import { advanceGlobalPowerlineThemeIndex } from './utils/powerline-theme-index';
import {
buildConfigWarningBadge,
Expand Down Expand Up @@ -117,6 +106,11 @@ async function renderMultipleLines(data: StatusJSON) {

const speedWidgetTypes = new Set(['output-speed', 'input-speed', 'total-speed']);
const hasSpeedItems = lines.some(line => line.some(item => speedWidgetTypes.has(item.type)));
const hasCompactionWidget = lines.some(line => line.some(item => item.type === 'compaction-counter'));
const hasThinkingEffortWidget = lines.some(line => line.some(item => item.type === 'thinking-effort'));
const hasSessionNameWidget = lines.some(line => line.some(item => item.type === 'session-name'));
const needsTranscriptThinkingEffort = hasThinkingEffortWidget
&& (!data.effort || !('level' in data.effort));
const requestedSpeedWindows = new Set<number>();
for (const line of lines) {
for (const item of line) {
Expand All @@ -126,39 +120,34 @@ async function renderMultipleLines(data: StatusJSON) {
}
}

let tokenMetrics: TokenMetrics | null = null;
if (data.transcript_path) {
tokenMetrics = await getTokenMetrics(data.transcript_path);
}

let sessionDuration: string | null = null;
if (hasSessionClock && !hasSessionDurationInStatusJson(data) && data.transcript_path) {
sessionDuration = await getSessionDuration(data.transcript_path);
}

const usageData = await prefetchUsageDataIfNeeded(lines, data);

let speedMetrics: SpeedMetrics | null = null;
let windowedSpeedMetrics: Record<string, SpeedMetrics> | null = null;
if (hasSpeedItems && data.transcript_path) {
const speedMetricsCollection = await getSpeedMetricsCollection(data.transcript_path, {
const transcriptAnalysisPromise = data.transcript_path
? getTranscriptAnalysis(data.transcript_path, {
includeSessionDuration: hasSessionClock && !hasSessionDurationInStatusJson(data),
includeSpeedMetrics: hasSpeedItems,
includeSubagents: true,
windowSeconds: Array.from(requestedSpeedWindows)
});

speedMetrics = speedMetricsCollection.sessionAverage;
windowedSpeedMetrics = speedMetricsCollection.windowed;
}
speedWindowSeconds: Array.from(requestedSpeedWindows),
includeCompactionStats: hasCompactionWidget,
includeThinkingEffort: needsTranscriptThinkingEffort,
includeSessionName: hasSessionNameWidget
})
: Promise.resolve(null);
const [transcriptAnalysis, usageData] = await Promise.all([
transcriptAnalysisPromise,
prefetchUsageDataIfNeeded(lines, data)
]);

const tokenMetrics = transcriptAnalysis?.tokenMetrics ?? null;
const sessionDuration = transcriptAnalysis?.sessionDuration ?? null;
const speedMetrics = transcriptAnalysis?.speedMetricsCollection?.sessionAverage ?? null;
const windowedSpeedMetrics = transcriptAnalysis?.speedMetricsCollection?.windowed ?? null;

let skillsMetrics: SkillsMetrics | null = null;
if (data.session_id) {
skillsMetrics = getSkillsMetrics(data.session_id);
}

// Compaction stats — parse compact_boundary markers in this session's transcript
const hasCompactionWidget = lines.some(line => line.some(item => item.type === 'compaction-counter'));
const compactionData = hasCompactionWidget
? (data.transcript_path ? await getCompactionStats(data.transcript_path) : ZERO_COMPACTION_STATS)
? (transcriptAnalysis?.compactionData ?? ZERO_COMPACTION_STATS)
: null;

// Create render context
Expand All @@ -169,6 +158,12 @@ async function renderMultipleLines(data: StatusJSON) {
windowedSpeedMetrics,
usageData,
sessionDuration,
transcriptSessionName: hasSessionNameWidget
? (transcriptAnalysis?.sessionName ?? null)
: undefined,
transcriptThinkingEffort: needsTranscriptThinkingEffort
? (transcriptAnalysis?.thinkingEffort ?? null)
: undefined,
skillsMetrics,
compactionData,
terminalWidth: getTerminalWidth(),
Expand Down
2 changes: 2 additions & 0 deletions src/types/RenderContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export interface RenderContext {
windowedSpeedMetrics?: Record<string, SpeedMetrics> | null;
usageData?: RenderUsageData | null;
sessionDuration?: string | null;
transcriptSessionName?: string | null;
transcriptThinkingEffort?: { value: string; known: boolean } | null;
blockMetrics?: BlockMetrics | null;
skillsMetrics?: SkillsMetrics | null;
compactionData?: CompactionData | null;
Expand Down
30 changes: 24 additions & 6 deletions src/utils/__tests__/compaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,29 @@ import {
it
} from 'vitest';

import type { CompactionData } from '../../types/RenderContext';
import {
ZERO_COMPACTION_STATS,
computeCompactionStats,
getCompactionStats
accumulateCompactionStats,
createCompactionStats
} from '../compaction';
import { parseJsonlLine } from '../jsonl-lines';
import { getTranscriptAnalysis } from '../jsonl-metrics';

/** Folds raw records through the same accumulator the transcript scan uses. */
function computeCompactionStats(lines: readonly string[]): CompactionData {
const stats = createCompactionStats();
for (const line of lines) {
accumulateCompactionStats(stats, parseJsonlLine(line));
}

return stats;
}

async function compactionStatsFor(transcriptPath: string): Promise<CompactionData | null> {
const analysis = await getTranscriptAnalysis(transcriptPath, { includeCompactionStats: true });
return analysis.compactionData;
}

describe('computeCompactionStats', () => {
it('returns zeroed stats for no compaction markers', () => {
Expand Down Expand Up @@ -122,7 +140,7 @@ describe('computeCompactionStats', () => {
});
});

describe('getCompactionStats', () => {
describe('compaction stats over a transcript', () => {
let dir: string;

beforeEach(() => {
Expand All @@ -134,7 +152,7 @@ describe('getCompactionStats', () => {
});

it('returns zeroed stats when the transcript file does not exist', async () => {
await expect(getCompactionStats(path.join(dir, 'missing.jsonl'))).resolves.toEqual(ZERO_COMPACTION_STATS);
await expect(compactionStatsFor(path.join(dir, 'missing.jsonl'))).resolves.toEqual(ZERO_COMPACTION_STATS);
});

it('computes stats from a real-shaped transcript', async () => {
Expand All @@ -146,14 +164,14 @@ describe('getCompactionStats', () => {
JSON.stringify({ type: 'system', subtype: 'compact_boundary', content: 'Conversation compacted', compactMetadata: { trigger: 'auto', preTokens: 912661, postTokens: 30026 }, version: '2.1.161' })
].join('\n') + '\n';
fs.writeFileSync(file, content);
await expect(getCompactionStats(file)).resolves.toEqual({
await expect(compactionStatsFor(file)).resolves.toEqual({
count: 2,
byTrigger: { auto: 1, manual: 1, unknown: 0 },
tokensReclaimed: (837327 - 25443) + (912661 - 30026)
});
});

it('returns zeroed stats when the transcript path is not a readable file', async () => {
await expect(getCompactionStats(dir)).resolves.toEqual(ZERO_COMPACTION_STATS);
await expect(compactionStatsFor(dir)).resolves.toEqual(ZERO_COMPACTION_STATS);
});
});
Loading