perf(claude-usage): reject non-assistant transcript lines before parsing them (#18640)

Only assistant records carry usage, but the parser ran JSON.parse on every line
first and checked the type after. Claude transcripts interleave user and
tool-result lines that routinely embed whole files or captured command output,
so the scanner built and discarded a full object graph for each of them.

A substring gate on the line rejects them first: median 50.5ms to 8.4ms over
20000 realistic lines.
This commit is contained in:
Neil
2026-09-04 14:33:39 -07:00
committed by GitHub
parent a7223be538
commit c1850e3cc7
2 changed files with 60 additions and 0 deletions
@@ -0,0 +1,40 @@
import { describe, expect, it } from 'vitest'
import { parseClaudeUsageRecord } from './transcript-record-parser'
function assistantLine(overrides: Record<string, unknown> = {}): string {
return JSON.stringify({
type: 'assistant',
sessionId: 'session-1',
timestamp: '2026-01-01T00:00:00.000Z',
message: { usage: { input_tokens: 3, output_tokens: 5 } },
...overrides
})
}
describe('assistant-record prefilter', () => {
it('still parses an ordinary assistant record', () => {
expect(parseClaudeUsageRecord(assistantLine())?.inputTokens).toBe(3)
})
it('rejects a user record that never mentions assistant', () => {
const userLine = JSON.stringify({
type: 'user',
sessionId: 'session-1',
timestamp: '2026-01-01T00:00:00.000Z',
message: { content: 'x'.repeat(200) }
})
expect(parseClaudeUsageRecord(userLine)).toBeNull()
})
it('rejects a non-assistant record that happens to contain the word assistant', () => {
const userLine = JSON.stringify({
type: 'user',
sessionId: 'session-1',
timestamp: '2026-01-01T00:00:00.000Z',
message: { content: 'ask the assistant about this' }
})
expect(parseClaudeUsageRecord(userLine)).toBeNull()
})
})
@@ -84,10 +84,30 @@ function dedupeClaudeUsageTurns(
return deduped
}
/**
* Necessary condition for `JSON.parse(line).type === 'assistant'`, checked before the parse.
*
* Sound for any transcript written by a standard JSON serializer: `JSON.stringify` (which writes
* these files) escapes only quotes, backslashes and control characters, never ASCII letters, so
* the decoded value can only be `assistant` if the line spells it literally. The gate over-admits
* freely — the `parsed.type` check below stays authoritative.
*
* A `\u`-escape fallback was measured and rejected: it costs a second full-line scan and made
* transcripts whose tool results contain control characters 1.43x slower overall.
*/
function mayEncodeAssistantType(line: string): boolean {
return line.includes('assistant')
}
function parseClaudeUsageSourceRecord(
line: string,
fallbackSessionId: string | null = null
): ClaudeUsageParsedSourceTurn | null {
// Only assistant records carry usage, but transcripts interleave user/tool-result lines that
// routinely embed whole files. Reject those before paying for a full parse.
if (!mayEncodeAssistantType(line)) {
return null
}
let parsed: ClaudeUsageSourceRecord
try {
parsed = JSON.parse(line) as ClaudeUsageSourceRecord