Files
deepseek-harness/packages/session/session-persistence-sqlite/src/frame.ts
T
07ad3a4dc5 perf(session): merge short SQLite frames inside the append transaction (#8674)
* perf(session): merge short SQLite frames inside the append transaction

Live writes store one to three events per frame, so compression barely
works. When the newest short frames of a Segment reach frameEvents events,
the append transaction re-encodes them as full frames. The frames_no_delete
trigger is dropped from the unreleased schema and from existing databases.

Co-authored-by: Magolor <[email protected]>
Co-authored-by: Kexin Liu <[email protected]>

* perf(session): read SQLite frame payloads only for a merge

Scan only frame ranges to decide whether the trailing short frames merge,
then read their payloads. Append decoded lines one by one so a large
frame size cannot exceed the argument limit.

Co-authored-by: Magolor <[email protected]>
Co-authored-by: Kexin Liu <[email protected]>

* perf(session): cap SQLite frame merges by decoded bytes

A merge skips a run of short frames whose stored or decoded text exceeds
4 MiB, bounding how long it holds the event loop and the write lock. Tests
cover merged compression, the live event path, a failure after the merge's
delete, and large events; a seeded synthetic diagnostic measures size and
append cost. Documentation drops the one-frame-per-write wording and lists
the write stop after short-frame corruption.

Co-authored-by: Magolor <[email protected]>
Co-authored-by: Kexin Liu <[email protected]>

* perf(session): store short SQLite frames uncompressed until merged

A short frame below min(64 KiB, 2 MiB / frameEvents) of JSON text is stored
as text; the merge compresses it together with its neighbours. This skips a
compression on every live append that the merge would redo, and keeps any run
of such frames within the 4 MiB merge limit.

Co-authored-by: Magolor <[email protected]>
Co-authored-by: Kexin Liu <[email protected]>

* perf(session): seal short SQLite frames when a writer closes

* test(session): assert the reported seal failure without an untyped matcher

---------

Co-authored-by: Turtle <[email protected]>
Co-authored-by: Magolor <[email protected]>
Co-authored-by: Kexin Liu <[email protected]>
Co-authored-by: Magolor <[email protected]>
2026-11-23 18:09:42 +08:00

81 lines
3.7 KiB
TypeScript

/**
* Frame encoding for stored event runs: a frame holds consecutive events as
* JSON lines, Zstandard-compressed when the store's compression is `zstd` and
* the frame shrinks, otherwise as UTF-8 text. Rows compress together, so a
* frame of many events reaches the ratio of a compressed log file, while a
* frame of one event costs no more than storing it raw. @module
*/
import { constants, zstdCompressSync, zstdDecompress, zstdDecompressSync } from 'node:zlib'
import { promisify } from 'node:util'
const decompress = promisify(zstdDecompress)
/** Physical frame encoding selected by the plugin's `compression` config. */
export type FrameCompression = 'zstd' | 'none'
/** Stored `frames.codec` for UTF-8 JSON lines; a format constant, never reinterpreted. */
export const FRAME_CODEC_UTF8 = 0
/** Stored `frames.codec` for one Zstandard frame of UTF-8 JSON lines; a format constant, never reinterpreted. */
export const FRAME_CODEC_ZSTD = 1
/**
* Zstandard level for stored frames: the format default, chosen for decode
* speed on the read path; a level change needs no schema change because every
* frame is self-describing.
*/
const ZSTD_LEVEL = 3
const ZSTD_OPTIONS = { params: { [constants.ZSTD_c_compressionLevel]: ZSTD_LEVEL } }
/** One encoded frame: the stored bytes, their codec, and the number of events they hold. */
export interface EncodedFrame {
readonly codec: typeof FRAME_CODEC_UTF8 | typeof FRAME_CODEC_ZSTD
readonly bytes: Buffer
readonly count: number
}
/**
* Encode consecutive event JSON texts as a frame. Synchronous: the caller
* bounds the input, by `frameEvents` for a write batch and by a decoded-byte
* limit for a merge of short frames.
* @param lines - the batch's events, each serialized as one JSON text without newlines.
* @param compression - `zstd` compresses when that is smaller; `none` stores the text.
* @returns the frame to store.
*/
export function encodeFrame(lines: readonly string[], compression: FrameCompression): EncodedFrame {
const utf8 = Buffer.from(lines.join('\n'), 'utf8')
if (compression === 'zstd') {
const frame = zstdCompressSync(utf8, ZSTD_OPTIONS)
if (frame.length < utf8.length) return { codec: FRAME_CODEC_ZSTD, bytes: frame, count: lines.length }
}
return { codec: FRAME_CODEC_UTF8, bytes: utf8, count: lines.length }
}
/**
* Decode one stored frame back to its event JSON texts. A compressed frame
* decompresses on the libuv thread pool, so callers may decode several frames
* concurrently; a text frame decodes inline.
* @param codec - the frame's stored codec.
* @param bytes - the frame's stored bytes.
* @returns the JSON text of each event, in sequence order.
* @throws {RangeError} for an unknown codec; a corrupt frame rejects with the Zstandard error.
*/
export async function decodeFrame(codec: number, bytes: Uint8Array): Promise<string[]> {
if (codec === FRAME_CODEC_ZSTD) return (await decompress(bytes)).toString('utf8').split('\n')
return decodeFrameSync(codec, bytes)
}
/**
* Decode one stored frame on the calling thread, for use inside a synchronous
* transaction.
* @param codec - the frame's stored codec.
* @param bytes - the frame's stored bytes.
* @returns the JSON text of each event, in sequence order.
* @throws {RangeError} for an unknown codec; a corrupt frame throws the Zstandard error.
*/
export function decodeFrameSync(codec: number, bytes: Uint8Array): string[] {
switch (codec) {
case FRAME_CODEC_UTF8: return Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString('utf8').split('\n')
case FRAME_CODEC_ZSTD: return zstdDecompressSync(bytes).toString('utf8').split('\n')
default: throw new RangeError(`unknown SQLite frame codec ${String(codec)}`)
}
}