refactor(agent): share the SQLite session cache (#526)

This commit is contained in:
Yiling-J
2026-11-11 17:05:34 +08:00
committed by GitHub
parent 35a0e82284
commit 00fa5fed75
8 changed files with 509 additions and 98 deletions
+3 -2
View File
@@ -14,8 +14,9 @@ const root = join(
instance,
)
export async function openSessionCache(sessionId: string, mode: 'read' | 'write') {
const directory = join(root, mode, encodeURIComponent(sessionId))
// Call only while the session has no open connections; live readers borrow the writer's storage.
export async function openSessionCache(sessionId: string) {
const directory = join(root, encodeURIComponent(sessionId))
await mkdir(directory, { recursive: true, mode: 0o700 })
const path = join(directory, 'session.sqlite')
const marker = join(directory, 'sync.json')
@@ -202,7 +202,7 @@ describe('SQL migration to Pi Durable', () => {
(await prisma.agentSession.findUniqueOrThrow({ where: { id: child.id } })).accountedUsage,
).toMatchObject({ totalTokens: 17, cost: 0.2 })
// Read the native ledger without hydrating image references.
const cache = await openSessionCache(child.id, 'read')
const cache = await openSessionCache(child.id)
const harness = await Harness.open(
cache.storage,
{ models: createModels(), registry: createRegistry() },
+288 -4
View File
@@ -1,12 +1,16 @@
import { BACKGROUND_CONTEXT as context } from '@earendil-works/chord/context'
import { createModels, fauxAssistantMessage, fauxProvider } from '@earendil-works/pi-ai'
import { Harness, UsageDoc, createRegistry } from '@earendil-works/pi-durable'
import { Harness, ROOT_CONVERSATION_ID, UsageDoc, createRegistry } from '@earendil-works/pi-durable'
import { prisma } from '@shumai/db'
import { acquireAdvisoryLock } from '@shumai/db/src/advisory-lock'
import * as advisoryLock from '@shumai/db/src/advisory-lock'
import { setupTestDbHooks } from '@shumai/db/test'
import { rm, writeFile } from 'node:fs/promises'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { s3Service } from '../s3/s3'
import { openSessionCache } from './session-cache'
import * as sessionCache from './session-cache'
import * as sessionStorage from './session-storage'
import { agentSessionService } from './session-service'
import { appendSessionEntries } from './session-test-utils'
@@ -63,7 +67,7 @@ describe('Native session storage and PostgreSQL synchronization', () => {
const entries = await appendSessionEntries(session.id, [
{ kind: 'pi.user', model: [{ role: 'user', content: 'safe', timestamp: 1 }] },
])
const cache = await openSessionCache(session.id, 'read')
const cache = await openSessionCache(session.id)
await cache.storage.close(context)
if (failure === 'missing') await rm(cache.path)
else if (failure === 'corrupt') await writeFile(cache.path, 'invalid sqlite')
@@ -84,6 +88,283 @@ describe('Native session storage and PostgreSQL synchronization', () => {
await next.release()
})
it('reads locally committed entries while PostgreSQL persistence is pending', async () => {
const { session } = await fixture()
const started = Promise.withResolvers<void>()
const resume = Promise.withResolvers<void>()
const synchronize = sessionStorage.synchronizeStorage
let paused = false
vi.spyOn(sessionStorage, 'synchronizeStorage').mockImplementation((storage, callbacks) =>
synchronize(storage, {
...callbacks,
persist: async (commits) => {
if (paused) {
started.resolve()
await resume.promise
}
await callbacks.persist(commits)
},
}),
)
const writer = await agentSessionService.openWriter(session.id)
const harness = await Harness.open(
writer.storage,
{ models: createModels(), registry: createRegistry() },
context,
)
const root = await harness.root(context)
paused = true
let acknowledged = false
const commit = root.commit(
(tx) => tx.appendEntry(root.id, { kind: 'pi.user', data: { text: 'locally committed' } }),
context,
)
void commit.then(() => {
acknowledged = true
})
let closing: Promise<void> | undefined
try {
await started.promise
expect(await prisma.agentSessionCommit.count({ where: { sessionId: session.id } })).toBe(1)
expect(await agentSessionService.getEntries(session.id)).toMatchObject([
{ kind: 'pi.user', data: { text: 'locally committed' } },
])
expect(acknowledged).toBe(false)
closing = writer.storage.close(context)
// Closing drains persistence before entering the lifecycle queue, so reads still work.
expect(await agentSessionService.getEntries(session.id)).toHaveLength(1)
} finally {
resume.resolve()
await commit
await closing
await harness.close(context)
}
expect(acknowledged).toBe(true)
// A history read must not close the writer's connection.
await agentSessionService.withWriter(session.id, async (storage) => {
expect(await storage.conversation(root.id, context)).toBeDefined()
})
})
it('uses one cache directory for concurrent initial reads and subsequent writes', async () => {
const { session } = await fixture()
const paths: string[] = []
const openCache = sessionCache.openSessionCache
vi.spyOn(sessionCache, 'openSessionCache').mockImplementation(async (...args) => {
const cache = await openCache(...args)
paths.push(cache.path)
return cache
})
expect(
await Promise.all([
agentSessionService.getEntries(session.id),
agentSessionService.listEntries(session.id),
]),
).toEqual([[], { items: [], next: undefined }])
const [written] = await Promise.all([
appendSessionEntries(session.id, [{ kind: 'pi.user', data: { text: 'shared cache' } }]),
agentSessionService.getEntries(session.id),
])
expect(await agentSessionService.getEntries(session.id)).toEqual(written)
expect(new Set(paths).size).toBe(1)
})
it('keeps history compact while the live writer hydrates image references', async () => {
const { session } = await fixture()
const image = Buffer.from('image bytes')
vi.spyOn(s3Service, 'getObject').mockResolvedValue({ buffer: image, contentType: 'image/png' })
await agentSessionService.withWriter(session.id, async (storage) => {
const harness = await Harness.open(
storage,
{ models: createModels(), registry: createRegistry() },
context,
)
try {
const root = await harness.root(context)
const entry = await root.commit(
(tx) =>
tx.appendEntry(root.id, {
kind: 'pi.user',
data: { sourceKeys: ['image-key'] },
model: [
{
role: 'user',
content: [
{ type: 'image', data: image.toString('base64'), mimeType: 'image/png' },
],
timestamp: 1,
},
],
}),
context,
)
expect(JSON.stringify((await storage.entry(entry.id, context))?.entry)).toContain(
image.toString('base64'),
)
const history = await agentSessionService.getEntries(session.id)
expect(JSON.stringify(history)).toContain('__S3_DATA__')
expect(JSON.stringify(history)).not.toContain(image.toString('base64'))
expect((await agentSessionService.listEntries(session.id)).items).toEqual(history)
} finally {
await harness.close(context)
}
})
})
it('waits for an admitted history read before closing the live writer', async () => {
const { session } = await fixture()
const entered = Promise.withResolvers<void>()
const resume = Promise.withResolvers<void>()
const openCache = sessionCache.openSessionCache
let paused = false
vi.spyOn(sessionCache, 'openSessionCache').mockImplementation(async (...args) => {
const cache = await openCache(...args)
const scan = cache.storage.scanEntries.bind(cache.storage)
cache.storage.scanEntries = async (...query) => {
if (paused) {
entered.resolve()
await resume.promise
}
return scan(...query)
}
return cache
})
const writer = await agentSessionService.openWriter(session.id)
const harness = await Harness.open(
writer.storage,
{ models: createModels(), registry: createRegistry() },
context,
)
const root = await harness.root(context)
await root.commit((tx) => tx.appendEntry(root.id, { kind: 'pi.user', data: {} }), context)
paused = true
const read = agentSessionService.getEntries(session.id)
await entered.promise
let closed = false
const closing = writer.storage.close(context).then(() => {
closed = true
})
try {
expect(closed).toBe(false)
} finally {
resume.resolve()
expect(await read).toHaveLength(1)
await closing
await harness.close(context)
}
expect(closed).toBe(true)
})
it('drains a writer being opened before shutdown removes the shared cache', async () => {
const { session } = await fixture()
const entered = Promise.withResolvers<void>()
const resume = Promise.withResolvers<void>()
const openCache = sessionCache.openSessionCache
vi.spyOn(sessionCache, 'openSessionCache').mockImplementationOnce(async (...args) => {
const cache = await openCache(...args)
entered.resolve()
await resume.promise
return cache
})
const opening = agentSessionService.openWriter(session.id)
await entered.promise
const shutdown = agentSessionService.closeAll()
try {
expect(agentSessionService.closeAll()).toBe(shutdown)
await expect(agentSessionService.getEntries(session.id)).rejects.toThrow('service is closing')
} finally {
resume.resolve()
await shutdown
}
const writer = await opening
await expect(writer.storage.conversation(ROOT_CONVERSATION_ID, context)).rejects.toThrow(
'closed',
)
expect(await agentSessionService.getEntries(session.id)).toEqual([])
})
it('cancels a writer waiting for ownership when the service shuts down', async () => {
const { session } = await fixture()
const held = await acquireAdvisoryLock(`agent-session:${session.id}`)
const opening = agentSessionService.openWriter(session.id)
const rejected = expect(opening).rejects.toThrow('Session service is closing')
try {
await agentSessionService.closeAll()
await rejected
} finally {
await held.release()
}
expect(await agentSessionService.getEntries(session.id)).toEqual([])
})
it('drains a writer that lost ownership before a replacement rebuilds its shared cache', async () => {
const { session } = await fixture()
const acquire = advisoryLock.acquireAdvisoryLock
const lost = new AbortController()
let release: (() => Promise<void>) | undefined
vi.spyOn(advisoryLock, 'acquireAdvisoryLock').mockImplementationOnce(async (...args) => {
const lock = await acquire(...args)
release = lock.release
return { ...lock, signal: lost.signal }
})
const started = Promise.withResolvers<void>()
const resume = Promise.withResolvers<void>()
const synchronize = sessionStorage.synchronizeStorage
vi.spyOn(sessionStorage, 'synchronizeStorage').mockImplementationOnce((storage, callbacks) =>
synchronize(storage, {
...callbacks,
persist: async (commits) => {
started.resolve()
await resume.promise
await callbacks.persist(commits)
},
}),
)
const opens = vi.spyOn(sessionCache, 'openSessionCache')
const first = await agentSessionService.openWriter(session.id)
const closing = vi.spyOn(first.storage, 'close')
const acknowledge = vi.fn()
const commit = first.storage.commit(
[
[
{ type: 'conversation', value: { id: ROOT_CONVERSATION_ID } },
{
type: 'entry',
value: {
id: await first.storage.mintId(),
conversationId: ROOT_CONVERSATION_ID,
kind: 'pi.user',
data: { text: 'unmirrored' },
},
},
],
],
acknowledge,
context,
)
const rejected = expect(commit).rejects.toThrow('lock lost')
await started.promise
lost.abort(new Error('lock lost'))
await release?.()
const replacement = agentSessionService.openWriter(session.id)
try {
await vi.waitFor(() => expect(closing).toHaveBeenCalledOnce())
expect(await agentSessionService.getEntries(session.id)).toMatchObject([
{ data: { text: 'unmirrored' } },
])
expect(opens).toHaveBeenCalledTimes(1)
} finally {
resume.resolve()
await rejected
await replacement
}
expect(acknowledge).not.toHaveBeenCalled()
await first.storage.close(context)
expect(await agentSessionService.getEntries(session.id)).toEqual([])
expect(opens).toHaveBeenCalledTimes(2)
await (await replacement).storage.close(context)
})
it('refuses a fenced writer and rebuilds its unacknowledged SQLite commit', async () => {
const { session } = await fixture()
const writer = await agentSessionService.openWriter(session.id)
@@ -97,7 +378,7 @@ describe('Native session storage and PostgreSQL synchronization', () => {
[
{
type: 'conversation',
value: { id: 1 as import('@earendil-works/pi-durable').ConversationId },
value: { id: ROOT_CONVERSATION_ID },
},
],
],
@@ -139,9 +420,11 @@ describe('Native session storage and PostgreSQL synchronization', () => {
{ model: { provider: faux.provider.id, modelId: faux.getModel().id } },
context,
)
return await (
const receipt = await (
await root.submit({ type: 'input', requestId: 'once', content: 'question' }, context)
).wait(context)
expect(await agentSessionService.getEntryForRequest(session.id, 'once')).toBeDefined()
return receipt
} finally {
await harness.close(context)
}
@@ -180,6 +463,7 @@ describe('Native session storage and PostgreSQL synchronization', () => {
})
}, context),
).rejects.toThrow('mirror unavailable')
expect(failure).toHaveBeenCalledTimes(1)
failure.mockRestore()
await harness.close(context)
expect(await prisma.aiUsage.count({ where: { teamId: team.id } })).toBe(0)
+123 -72
View File
@@ -14,6 +14,8 @@ import { quotaService } from '../quota/quota-service'
import { openSessionCache, removeSessionCaches } from './session-cache'
import { hydrateStorage, synchronizeStorage, type CommitReceipt } from './session-storage'
type SessionReader = Pick<Storage, 'conversation' | 'scanEntries' | 'submissionByRequest' | 'entry'>
type UsageOwner = { teamId: string; userId?: string; role?: TeamMemberRole | null }
export async function readSessionUsage(storage: Storage): Promise<PrismaJson.AgentSessionUsage> {
@@ -36,70 +38,95 @@ export async function readSessionUsage(storage: Storage): Promise<PrismaJson.Age
}
export class AgentSessionService {
private readonly readers = new Map<string, Promise<unknown>>()
private readonly writers = new Set<Storage>()
private readonly access = new Map<string, Promise<unknown>>()
private readonly writers = new Map<string, { storage: Storage; reader: SessionReader }>()
private closing: Promise<void> | undefined
private shutdown = new AbortController()
async openWriter(sessionId: string, context: Context = BACKGROUND_CONTEXT, owner?: UsageOwner) {
const lock = await acquireAdvisoryLock(`agent-session:${sessionId}`, context.abortSignal)
if (this.closing) throw new Error('Session service is closing')
const shutdown = this.shutdown.signal
const signal = context.abortSignal ? AbortSignal.any([context.abortSignal, shutdown]) : shutdown
const lock = await acquireAdvisoryLock(`agent-session:${sessionId}`, signal)
let storage: Storage | undefined
try {
const session = await prisma.agentSession.update({
where: { id: sessionId },
data: { writerVersion: { increment: 1 } },
})
const cache = await openSessionCache(sessionId, 'write')
let head = cache.seq
storage = hydrateStorage(
synchronizeStorage(cache.storage, {
dirty: cache.dirty,
clean: cache.clean,
persist: async (commits: readonly CommitReceipt[]) => {
lock.signal.throwIfAborted()
if (!commits.length) return
await prisma.$transaction(async (tx) => {
const rows = await tx.$queryRaw<
Array<{ writerVersion: number }>
>`SELECT writer_version AS "writerVersion" FROM agent_sessions WHERE id = ${sessionId} FOR UPDATE`
if (rows[0]?.writerVersion !== session.writerVersion)
throw new Error('Session writer ownership was lost')
const latest = await tx.agentSessionCommit.findFirst({
where: { sessionId },
orderBy: { seq: 'desc' },
select: { seq: true },
})
if (Number(latest?.seq ?? 0) !== head)
throw new Error('Session changed while writing')
if (owner)
await this.recordUsage(sessionId, await readSessionUsage(cache.storage), owner, tx)
await tx.agentSessionCommit.createMany({
data: commits.map(({ seq, writes }) => ({
sessionId,
seq: BigInt(seq),
data: JSON.parse(
JSON.stringify({ version: 1, writes }),
) as PrismaJson.AgentSessionCommitData,
})),
})
})
head = commits[commits.length - 1].seq
},
}),
// A previous writer may still be draining after losing its advisory-lock connection.
const previous = await this.withCacheAccess(sessionId, async () =>
this.writers.get(sessionId),
)
const close = storage.close.bind(storage)
let closed = false
const owned = storage
owned.close = async (context) => {
if (closed) return
closed = true
this.writers.delete(owned)
try {
await close(context)
} finally {
await lock.release()
await previous?.storage.close(BACKGROUND_CONTEXT)
return await this.withCacheAccess(sessionId, async () => {
shutdown.throwIfAborted()
lock.signal.throwIfAborted()
const session = await prisma.agentSession.update({
where: { id: sessionId },
data: { writerVersion: { increment: 1 } },
})
const cache = await openSessionCache(sessionId)
storage = cache.storage
// Capture native reads before writer hydration decorates this instance.
const reader: SessionReader = {
conversation: storage.conversation.bind(storage),
scanEntries: storage.scanEntries.bind(storage),
submissionByRequest: storage.submissionByRequest.bind(storage),
entry: storage.entry.bind(storage),
}
}
this.writers.add(owned)
return { storage: owned, signal: lock.signal }
const close = storage.close.bind(storage)
storage.close = (context) =>
this.withCacheAccess(sessionId, async () => {
try {
await close(context)
} finally {
if (this.writers.get(sessionId)?.storage === cache.storage)
this.writers.delete(sessionId)
await lock.release()
}
})
let head = cache.seq
storage = hydrateStorage(
synchronizeStorage(cache.storage, {
dirty: cache.dirty,
clean: cache.clean,
persist: async (commits: readonly CommitReceipt[]) => {
lock.signal.throwIfAborted()
if (!commits.length) return
await prisma.$transaction(async (tx) => {
const rows = await tx.$queryRaw<
Array<{ writerVersion: number }>
>`SELECT writer_version AS "writerVersion" FROM agent_sessions WHERE id = ${sessionId} FOR UPDATE`
if (rows[0]?.writerVersion !== session.writerVersion)
throw new Error('Session writer ownership was lost')
const latest = await tx.agentSessionCommit.findFirst({
where: { sessionId },
orderBy: { seq: 'desc' },
select: { seq: true },
})
if (Number(latest?.seq ?? 0) !== head)
throw new Error('Session changed while writing')
if (owner)
await this.recordUsage(
sessionId,
await readSessionUsage(cache.storage),
owner,
tx,
)
await tx.agentSessionCommit.createMany({
data: commits.map(({ seq, writes }) => ({
sessionId,
seq: BigInt(seq),
data: JSON.parse(
JSON.stringify({ version: 1, writes }),
) as PrismaJson.AgentSessionCommitData,
})),
})
})
head = commits[commits.length - 1].seq
},
}),
)
this.writers.set(sessionId, { storage, reader })
return { storage, signal: lock.signal }
})
} catch (error) {
try {
await storage?.close(BACKGROUND_CONTEXT)
@@ -123,34 +150,58 @@ export class AgentSessionService {
}
}
async closeAll() {
await Promise.allSettled([...this.writers].map((storage) => storage.close(BACKGROUND_CONTEXT)))
await Promise.allSettled(this.readers.values())
await removeSessionCaches()
closeAll(): Promise<void> {
if (this.closing) return this.closing
this.shutdown.abort(new Error('Session service is closing'))
return (this.closing ??= (async () => {
// An admitted open may publish its writer after shutdown starts.
await Promise.allSettled(this.access.values())
await Promise.allSettled(
[...this.writers.values()].map(({ storage }) => storage.close(BACKGROUND_CONTEXT)),
)
await Promise.allSettled(this.access.values())
await removeSessionCaches()
})().finally(() => {
this.shutdown = new AbortController()
this.closing = undefined
}))
}
private async read<T>(sessionId: string, use: (storage: Storage) => Promise<T>): Promise<T> {
const previous = this.readers.get(sessionId)
/** Serialize file initialization, history reads and final close, never PostgreSQL mirroring. */
private async withCacheAccess<T>(sessionId: string, use: () => Promise<T>): Promise<T> {
const previous = this.access.get(sessionId)
const current = (async () => {
await previous?.catch(() => {})
return use()
})()
this.access.set(sessionId, current)
try {
return await current
} finally {
if (this.access.get(sessionId) === current) this.access.delete(sessionId)
}
}
private async read<T>(
sessionId: string,
use: (storage: SessionReader) => Promise<T>,
): Promise<T> {
if (this.closing) throw new Error('Session service is closing')
return this.withCacheAccess(sessionId, async () => {
const session = await prisma.agentSession.findUnique({
where: { id: sessionId },
select: { id: true },
})
if (!session) throw new Error('Session not found')
const cache = await openSessionCache(sessionId, 'read')
const writer = this.writers.get(sessionId)
if (writer) return use(writer.reader)
const cache = await openSessionCache(sessionId)
try {
return await use(cache.storage)
} finally {
await cache.storage.close(BACKGROUND_CONTEXT)
}
})()
this.readers.set(sessionId, current)
try {
return await current
} finally {
if (this.readers.get(sessionId) === current) this.readers.delete(sessionId)
}
})
}
async listEntries(
@@ -12,6 +12,7 @@ import {
Harness,
GenerationTask,
hook,
ROOT_CONVERSATION_ID,
type StorageWrite,
} from '@earendil-works/pi-durable'
import { openNodeSqliteStorage } from '@earendil-works/pi-durable/storage/sqlite/node'
@@ -28,6 +29,42 @@ describe('native Durable content storage', () => {
setupTestDbHooks()
afterEach(() => vi.restoreAllMocks())
it('keeps an uncertain commit dirty and refuses subsequent writes', async () => {
const directory = await mkdtemp(join(tmpdir(), 'shumai-failed-session-'))
const dirty = vi.fn(async () => {})
const clean = vi.fn(async () => {})
const persist = vi.fn(async () => {
throw new Error('mirror unavailable')
})
const storage = synchronizeStorage(
await openNodeSqliteStorage(join(directory, 'session.sqlite')),
{
dirty,
clean,
persist,
},
)
const acknowledge = vi.fn()
try {
await expect(
storage.commit(
[[{ type: 'conversation', value: { id: ROOT_CONVERSATION_ID } }]],
acknowledge,
context,
),
).rejects.toThrow('mirror unavailable')
expect(await storage.conversation(ROOT_CONVERSATION_ID, context)).toBeDefined()
await expect(storage.commit([[]], acknowledge, context)).rejects.toThrow('mirror unavailable')
expect(dirty).toHaveBeenCalledTimes(1)
expect(persist).toHaveBeenCalledTimes(1)
expect(clean).not.toHaveBeenCalled()
expect(acknowledge).not.toHaveBeenCalled()
} finally {
await storage.close(context)
await rm(directory, { recursive: true, force: true })
}
})
it('hydrates generation and compaction after reopening while both persisted copies stay compact', async () => {
const directory = await mkdtemp(join(tmpdir(), 'shumai-content-'))
const path = join(directory, 'session.sqlite')
+29 -9
View File
@@ -23,16 +23,36 @@ export function synchronizeStorage(
},
): Storage {
const commit = storage.commit.bind(storage)
storage.commit = async (batches, receive, context) => {
await callbacks.dirty()
const compact = batches.map(compactSessionWrites)
const receipts: Array<{ index: number; seq: Seq }> = []
const result = await commit(compact, (index, seq) => receipts.push({ index, seq }), context)
await callbacks.persist(receipts.map(({ index, seq }) => ({ seq, writes: compact[index] })))
await callbacks.clean(receipts.at(-1)?.seq)
for (const { index, seq } of receipts) receive(index, seq)
return result
const close = storage.close.bind(storage)
let pending = Promise.resolve()
let failure: { error: unknown } | undefined
let closing: Promise<void> | undefined
storage.commit = (batches, receive, context) => {
if (closing) return Promise.reject(new Error('Session storage is closing'))
const current = pending.then(async () => {
if (failure) throw failure.error
try {
await callbacks.dirty()
const compact = batches.map(compactSessionWrites)
const receipts: Array<{ index: number; seq: Seq }> = []
const result = await commit(compact, (index, seq) => receipts.push({ index, seq }), context)
await callbacks.persist(receipts.map(({ index, seq }) => ({ seq, writes: compact[index] })))
await callbacks.clean(receipts.at(-1)?.seq)
for (const { index, seq } of receipts) receive(index, seq)
return result
} catch (error) {
// An uncertain local commit must never be marked clean by a later write.
failure = { error }
throw error
}
})
pending = current.then(
() => {},
() => {},
)
return current
}
storage.close = (context) => (closing ??= pending.then(() => close(context)))
return storage
}