Files
Magolor cbc6457226 feat(session)!: store SQLite events as rows with derived tables
The opt-in SQLite Session store kept each durable write as one compressed
frame, so SQL could not see an event's sequence, type or time. It now
stores one row per event: the envelope members are columns and data is
JSON text, or one Zstandard frame when it is large. Segments use integer
ids and a segment_paths table that lists each Segment's history, so the
session_* views return a Session's events in one join, forks included.

Derived tables for model calls, tool calls, tool results and compactions
are written in the events' transaction and rebuilt from events when a
table is missing, its version changes, or its derivation was absent.

Opening a database runs ordered upgrade steps under src/migrations. The
first rewrites the frame layout (schema version 1) in place; a frame it
cannot read is set aside without blocking the other Sessions.

BREAKING CHANGE: the frameEvents setting is removed, and a database
upgraded to schema version 2 cannot be opened by earlier releases.
2026-11-25 17:23:45 +08:00

82 lines
3.5 KiB
TypeScript

/** Shared persistence semantics and SQLite plugin activation. @module */
import { describe, expect, it } from 'vitest'
import { DatabaseSync } from 'node:sqlite'
import { readdir } from 'node:fs/promises'
import { join } from 'node:path'
import { meta, oneTurnLog, runPersistenceContract } from '../../session-persistence/tests/contract.ts'
import { persistenceFixture } from './fixtures/persistence.ts'
import { headSegment, storedEvents } from './fixtures/store.ts'
runPersistenceContract('sqlite', async () => {
const fixture = await persistenceFixture()
const backend = await fixture.mount('sqlite')
return {
persistence: backend.persistence,
dispose: () => backend.context.fiber.dispose(),
reopen: async () => {
const reopened = await fixture.mount('sqlite')
return { persistence: reopened.persistence, dispose: () => reopened.context.fiber.dispose() }
},
}
})
describe('SQLite persistence plugin', () => {
it('registers the service before creating the database and initializes on first create', async () => {
const fixture = await persistenceFixture()
const { persistence } = await fixture.mount('sqlite')
expect(persistence).toBeDefined()
expect(await persistence.list()).toEqual([])
expect(await readdir(fixture.root)).toEqual([])
const writer = await persistence.create(meta('first-create'))
await writer.flush()
await writer.close()
expect(await readdir(join(fixture.root, '.segments'))).toContain('sessions.sqlite')
})
it('stores one complete logical event stream in an independent root segment', async () => {
const fixture = await persistenceFixture()
const { persistence, context } = await fixture.mount('sqlite')
const header = meta('root-segment', '/workspace')
const events = oneTurnLog()
const writer = await persistence.create(header)
await writer.append(events)
await writer.close()
await context.fiber.dispose()
const db = new DatabaseSync(join(fixture.root, '.segments', 'sessions.sqlite'))
try {
const stored = db.prepare('SELECT header FROM sessions WHERE session_id = ?').get(header.id)
expect(JSON.parse(String(stored?.header))).toEqual({ ...header, delegationDepth: 0 })
const head = headSegment(db, header.id)
expect(db.prepare('SELECT parent_id, base_seq FROM segments WHERE segment_id = ?').get(head)).toEqual({ parent_id: null, base_seq: 0 })
expect(storedEvents(db, head)).toEqual(events)
} finally {
db.close()
}
})
it('keeps the default JSONL provider independent of SQLite storage', async () => {
const fixture = await persistenceFixture()
const { persistence } = await fixture.mount('jsonl')
const writer = await persistence.create(meta('default-jsonl', '/workspace'))
await writer.append(oneTurnLog())
await writer.close()
expect(await readdir(fixture.root)).not.toContain('.segments')
})
it('drains live Session events before provider disposal', async () => {
const fixture = await persistenceFixture()
const { persistence, context } = await fixture.mount('sqlite')
const session = context.sessions.create(meta('shutdown-drain').id)
await persistence.create(session.header)
const events = [
session.append('turn/start', { turn: 1 }),
session.append('turn/end', { turn: 1, reason: { kind: 'completed' } }),
]
await context.fiber.dispose()
const reopened = await fixture.mount('sqlite')
const reader = await reopened.persistence.open(session.id, 'read')
expect((await reader.read()).events).toEqual(events)
await reader.close()
})
})