mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-23 04:37:09 +00:00
perf(claude): scan transcripts incrementally (#1081)
* perf(claude): scan transcripts incrementally The claude session scanner re-read the entire transcript JSONL on every scan, so the cost of each poll grew with the length of the conversation. Track a byte offset per file instead and parse only the bytes appended since the previous scan. A trailing partial line — a write still in progress — is held back until its newline arrives. A file that shrank resets the cursor to 0; the base scanner's uuid dedup absorbs the re-sent events. A read that fails returns no events and leaves the cursor where it was, so a transient error is retried on the next scan rather than skipping content. The codex scanner received this in #1031; this extends the same improvement to the claude scanner. readSessionLog is exported for tests, mirroring readTranscriptRange there. * fix(claude): forward a complete final record with no trailing newline The incremental reader consumed only through the last newline, so a final JSONL record flushed without its terminating newline — at shutdown or on import — was held back as if it were a partial write and never forwarded until a later append supplied the newline. The previous whole-file reader parsed such a record. Consume a trailing segment when it already parses as a complete JSON value, and keep holding back a genuinely partial line (which parses as incomplete).
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
||||
import { createSessionScanner } from './sessionScanner'
|
||||
import { createSessionScanner, readSessionLog } from './sessionScanner'
|
||||
import { RawJSONLines } from '../types'
|
||||
import { mkdir, writeFile, appendFile, rm, readFile } from 'node:fs/promises'
|
||||
import { join } from 'node:path'
|
||||
@@ -144,4 +144,83 @@ describe('sessionScanner', () => {
|
||||
expect(content).toContain('readme.md')
|
||||
}
|
||||
})
|
||||
|
||||
it('reads only new bytes on a subsequent scan (incremental, no reparse of prior region)', async () => {
|
||||
const filePath = join(testDir, 'incremental.jsonl')
|
||||
const line1 = JSON.stringify({ type: 'user', uuid: 'u1', message: { content: 'first' } })
|
||||
const line2 = JSON.stringify({ type: 'user', uuid: 'u2', message: { content: 'second' } })
|
||||
await writeFile(filePath, line1 + '\n')
|
||||
|
||||
const first = await readSessionLog(filePath, 0)
|
||||
expect(first.events).toHaveLength(1)
|
||||
expect(first.nextCursor).toBe(Buffer.byteLength(line1 + '\n'))
|
||||
|
||||
await appendFile(filePath, line2 + '\n')
|
||||
const second = await readSessionLog(filePath, first.nextCursor)
|
||||
|
||||
// Starting from the cursor returned by the first scan, only the newly
|
||||
// appended line comes back — the already-seen line is not re-parsed.
|
||||
expect(second.events).toHaveLength(1)
|
||||
expect(second.events[0].event.type).toBe('user')
|
||||
if (second.events[0].event.type === 'user') {
|
||||
expect(second.events[0].event.uuid).toBe('u2')
|
||||
}
|
||||
expect(second.nextCursor).toBe(Buffer.byteLength(line1 + '\n' + line2 + '\n'))
|
||||
})
|
||||
|
||||
it('holds back an unterminated trailing line until its newline arrives', async () => {
|
||||
const filePath = join(testDir, 'partial.jsonl')
|
||||
const complete = JSON.stringify({ type: 'user', uuid: 'u1', message: { content: 'complete' } })
|
||||
const partial = JSON.stringify({ type: 'user', uuid: 'u2', message: { content: 'partial' } })
|
||||
|
||||
// Simulate a write in progress: a full line followed by a truncated one
|
||||
// with no trailing newline yet.
|
||||
await writeFile(filePath, complete + '\n' + partial.slice(0, 10))
|
||||
|
||||
const result = await readSessionLog(filePath, 0)
|
||||
expect(result.events).toHaveLength(1)
|
||||
if (result.events[0].event.type === 'user') {
|
||||
expect(result.events[0].event.uuid).toBe('u1')
|
||||
}
|
||||
// Cursor sits right after the complete line's newline, not at EOF — the
|
||||
// partial trailing bytes are left unconsumed.
|
||||
expect(result.nextCursor).toBe(Buffer.byteLength(complete + '\n'))
|
||||
|
||||
// Completing the line makes it available on the next scan.
|
||||
await appendFile(filePath, partial.slice(10) + '\n')
|
||||
const followUp = await readSessionLog(filePath, result.nextCursor)
|
||||
expect(followUp.events).toHaveLength(1)
|
||||
if (followUp.events[0].event.type === 'user') {
|
||||
expect(followUp.events[0].event.uuid).toBe('u2')
|
||||
}
|
||||
})
|
||||
|
||||
it('forwards a complete final record written without a trailing newline', async () => {
|
||||
const filePath = join(testDir, 'no-trailing-newline.jsonl')
|
||||
const line1 = JSON.stringify({ type: 'user', uuid: 'u1', message: { content: 'first' } })
|
||||
const line2 = JSON.stringify({ type: 'user', uuid: 'u2', message: { content: 'last' } })
|
||||
|
||||
// A prior line terminated by a newline, then a complete final record that
|
||||
// was flushed without its terminating newline (shutdown/import).
|
||||
await writeFile(filePath, line1 + '\n' + line2)
|
||||
|
||||
const result = await readSessionLog(filePath, 0)
|
||||
expect(result.events).toHaveLength(2)
|
||||
expect(result.events.map((e) => e.event.type === 'user' ? e.event.uuid : null)).toEqual(['u1', 'u2'])
|
||||
// The whole file is consumed even though it does not end in a newline.
|
||||
expect(result.nextCursor).toBe(Buffer.byteLength(line1 + '\n' + line2))
|
||||
})
|
||||
|
||||
it('forwards a single complete record with no newline at all', async () => {
|
||||
const filePath = join(testDir, 'single-no-newline.jsonl')
|
||||
const only = JSON.stringify({ type: 'user', uuid: 'u1', message: { content: 'only' } })
|
||||
await writeFile(filePath, only)
|
||||
|
||||
const result = await readSessionLog(filePath, 0)
|
||||
expect(result.events).toHaveLength(1)
|
||||
if (result.events[0].event.type === 'user') {
|
||||
expect(result.events[0].event.uuid).toBe('u1')
|
||||
}
|
||||
expect(result.nextCursor).toBe(Buffer.byteLength(only))
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user