* test(session): cover SQLite persistence and document the provider * test(session): align coverage with the latest SQLite implementation * test(session): adapt storage coverage to the current SQLite backend * feat(session)!: pass fork source through agent creation * refactor: prepare forks privately and append child segment events * refactor: choose fork sharing in SQLite creation transaction * fix(session): preserve caller-prepared fork seeds * feat(session): flush fixed fork prefix before child creation * fix(session): persist fork sources and await parent drain * chore: remove test and documentation changes from fork PR * refactor(agent-loop): reuse agent fork source type * fix(session): retain SQLite handle and JSONL errors after rebase * perf(session): create forks from the caller's history without reloading the parent The fork source carries the complete child history from seq 0. A live parent's log supplies the shared prefix; a cold parent's prefix is used as persistence returned it to the caller. The factory no longer reopens the parent, and the JSONL backend copies the supplied history instead of decoding the parent log again. The SQLite backend drains a live parent writer before sharing its persisted prefix, so the factory no longer flushes the parent or tracks owner disposal during that wait; its fork creation serializes child events without the read-side validation and reports publication failures through the shared wrapper. * refactor(session-query): report the persisted prefix length on live observations A live observation reports its event count as the persisted prefix, so the fork command's boundary fallback only covers observations from other producers. * test(session): exclude the SQLite POSIX lease from Windows coverage The SQLite copy of the session write lease executes its POSIX face only off Windows, matching the JSONL exclusion. * test(session): report legacy opener failures on stderr The two-process suite asserts only the error name; the message and cause now reach the inherited stderr so a CI failure identifies the SQLite error. * refactor(agent): keep seed as the constructor input and narrow the fork source CreateAgentOptions.seed stays the only constructor history; source names the parent and cut a seeded fork shares. The factory stores the constructed log with that source at creation, so no fork-only seed selection remains in the loop. * test(session): complete fork regressions and documentation * test(session): propagate current SQLite fixtures through fork regressions * test(session): describe fork creation from the caller's history The fork source now carries the complete child history and the factory no longer reopens or flushes the parent, so the fake factories, source expectations, and cancellation cases follow that flow: a sharing backend drains the parent writer itself, JSONL copies the supplied history, and observations always report the persisted prefix. Documentation, the upgrade guide, and the generated catalogs describe the same behavior. * test(session): cover fork creation without a factory flush The in-process fork provider's cases no longer wait for a parent flush: the live log supplies the completed prefix and cancellation is exercised while child storage is pending. The Remote fork command's boundary fallback is covered by an observation that omits its persisted count. * test(session): drop redundant observation assertions in the fork fallback case * test(session): describe fork creation with seed beside a parent source The agent factory keeps seed as the constructor history and source names only the parent and cut, so the fakes, source expectations, and documentation follow that split; the case where a live parent replaced the supplied prefix no longer applies. * refactor(session): name the fork parent option parentSource `CreateAgentOptions.source` and `SessionPersistenceCreateOptions.source` become `parentSource`: the field names the parent Session a seeded fork shares its prefix with, and the shorter name read as a generic origin. * refactor(session-query): report persistedEventCount on cold observations only A live observation holds the Session's own events, none of them read-time repairs, so its stored prefix is its whole log and the field said nothing; only a cold read appends interrupted-turn closers in memory and needs to tell the fork cut where the stored prefix ends.
130 lines
7.3 KiB
TypeScript
130 lines
7.3 KiB
TypeScript
/** Fork creation stores the supplied history; a sharing backend persists the parent prefix first. */
|
|
import { Context } from '@deepseek-ai/cordis'
|
|
import { mkdtemp, rm } from 'node:fs/promises'
|
|
import { tmpdir } from 'node:os'
|
|
import { join } from 'node:path'
|
|
import { DatabaseSync } from 'node:sqlite'
|
|
import { expect, it, onTestFinished, vi } from 'vitest'
|
|
import { SessionId, SessionSeq, SessionLogOffset } from '@deepseek-ai/dsh-session'
|
|
import { buildForkSeed } from '@deepseek-ai/dsh-session/fork'
|
|
import * as persistence from '@deepseek-ai/dsh-session-persistence-jsonl'
|
|
import * as sqlitePersistence from '@deepseek-ai/dsh-session-persistence-sqlite'
|
|
import { mountAgentLoopTestDependencies, mountAgentLoopTestHarness } from '@deepseek-ai/dsh-agent-loop-testkit'
|
|
|
|
async function fixture(storage: 'sqlite' | 'jsonl' = 'sqlite') {
|
|
const root = await mkdtemp(join(tmpdir(), 'dsh-agent-fork-source-'))
|
|
const ctx = new Context()
|
|
onTestFinished(async () => { try { await ctx.fiber.dispose() } finally { await rm(root, { recursive: true, force: true }) } })
|
|
await mountAgentLoopTestDependencies(ctx)
|
|
await ctx.plugin(storage === 'sqlite' ? sqlitePersistence : persistence, { root, compression: 'none' })
|
|
await mountAgentLoopTestHarness(ctx)
|
|
return { ctx, root }
|
|
}
|
|
|
|
it.each(['sqlite', 'jsonl'] as const)('stores a prepared open-prefix seed without changing its marker or repair order (%s)', async (storage) => {
|
|
const { ctx } = await fixture(storage)
|
|
const parent = ctx.sessions.create(SessionId('parent'))
|
|
const writer = await ctx.sessionPersistence.create(parent.header)
|
|
onTestFinished(() => writer.close())
|
|
parent.append('turn/start', { turn: 1 })
|
|
const seed = buildForkSeed(parent.snapshotEvents(), SessionSeq(0))
|
|
const stat = vi.spyOn(ctx.sessionPersistence, 'stat')
|
|
const open = vi.spyOn(ctx.sessionPersistence, 'open')
|
|
onTestFinished(() => { stat.mockRestore(); open.mockRestore() })
|
|
const child = await ctx.agents.create({
|
|
sessionId: SessionId('child'), inheritedEventCount: SessionLogOffset(1),
|
|
meta: { isSeeded: true }, seed, parentSource: { sessionId: parent.id, endSeq: SessionLogOffset(1) },
|
|
})
|
|
expect(stat).not.toHaveBeenCalledWith(parent.id)
|
|
expect(open).not.toHaveBeenCalledWith(parent.id, expect.anything(), expect.anything())
|
|
expect(child.agent.session.snapshotEvents()).toEqual(seed)
|
|
expect(seed).toMatchObject([
|
|
{ type: 'turn/start', seq: 0 }, { type: 'session/end-seed', seq: 1, data: { inherited: true } },
|
|
{ type: 'turn/end', seq: 2, data: { reason: { kind: 'forked' } } },
|
|
])
|
|
await child.dispose()
|
|
const reader = await ctx.sessionPersistence.open(child.agent.id, 'read')
|
|
try { expect((await reader.read()).events).toEqual(seed) } finally { await reader.close() }
|
|
})
|
|
|
|
it('continues a JSONL child from its creation history while the parent keeps writing', async () => {
|
|
const { ctx } = await fixture('jsonl')
|
|
const parent = ctx.sessions.create(SessionId('parent'))
|
|
const writer = await ctx.sessionPersistence.create(parent.header)
|
|
onTestFinished(() => writer.close())
|
|
parent.append('turn/start', { turn: 1 })
|
|
parent.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
|
|
const seed = buildForkSeed(parent.snapshotEvents(), SessionSeq(1))
|
|
const child = await ctx.agents.create({
|
|
sessionId: SessionId('child'), inheritedEventCount: SessionLogOffset(2), meta: { isSeeded: true },
|
|
seed, parentSource: { sessionId: parent.id, endSeq: SessionLogOffset(2) },
|
|
})
|
|
parent.append('turn/start', { turn: 2 })
|
|
parent.append('turn/end', { turn: 2, reason: { kind: 'completed' } })
|
|
child.agent.session.append('turn/start', { turn: 2 })
|
|
child.agent.session.append('turn/end', { turn: 2, reason: { kind: 'completed' } })
|
|
await ctx.sessions.flush(parent)
|
|
await ctx.sessions.flush(child.agent.session)
|
|
await child.dispose()
|
|
const reader = await ctx.sessionPersistence.open(child.agent.id, 'read')
|
|
try {
|
|
const events = (await reader.read()).events
|
|
expect(events).toEqual(child.agent.session.snapshotEvents())
|
|
expect(events.slice(0, seed.length)).toEqual(seed)
|
|
expect(events.slice(seed.length)).toMatchObject([
|
|
{ type: 'turn/start', seq: 3 }, { type: 'turn/end', seq: 4 },
|
|
])
|
|
expect(events.map(event => event.seq)).toEqual([0, 1, 2, 3, 4])
|
|
} finally { await reader.close() }
|
|
})
|
|
|
|
it.each([0, 1, 2, 4])('flushes the selected prefix with initial durable count %s before later parent writes', async (persistedCount) => {
|
|
const { ctx, root } = await fixture()
|
|
const parent = ctx.sessions.create(SessionId('parent'))
|
|
const writer = await ctx.sessionPersistence.create(parent.header)
|
|
onTestFinished(() => writer.close())
|
|
parent.append('turn/start', { turn: 1 })
|
|
if (persistedCount === 1) await writer.flush()
|
|
parent.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
|
|
if (persistedCount === 2) await writer.flush()
|
|
const prefix = parent.snapshotEvents()
|
|
parent.append('turn/start', { turn: 2 })
|
|
parent.append('turn/end', { turn: 2, reason: { kind: 'completed' } })
|
|
if (persistedCount === 4) await writer.flush()
|
|
const seed = buildForkSeed(prefix, SessionSeq(1))
|
|
const create = ctx.sessionPersistence.create.bind(ctx.sessionPersistence)
|
|
const creating = vi.spyOn(ctx.sessionPersistence, 'create').mockImplementation(async (header, options) => {
|
|
expect(options?.parentSource).toMatchObject({ sessionId: parent.id, endSeq: prefix.length, events: seed })
|
|
expect((await ctx.sessionPersistence.stat(parent.id))?.eventCount ?? 0).toBe(persistedCount)
|
|
const childWriter = await create(header, options)
|
|
// The sharing backend drained the parent writer before referencing its prefix.
|
|
expect((await ctx.sessionPersistence.stat(parent.id))?.eventCount).toBe(4)
|
|
expect((await childWriter.read()).events).toEqual(seed)
|
|
parent.append('turn/start', { turn: 3 })
|
|
parent.append('turn/end', { turn: 3, reason: { kind: 'completed' } })
|
|
await ctx.sessions.flush(parent)
|
|
return childWriter
|
|
})
|
|
onTestFinished(() => { creating.mockRestore() })
|
|
const child = await ctx.agents.create({ sessionId: SessionId('child'), inheritedEventCount: SessionLogOffset(2),
|
|
meta: { isSeeded: true }, seed, parentSource: { sessionId: parent.id, endSeq: SessionLogOffset(2) } })
|
|
expect(child.agent.session.inheritedEventCount).toBe(2)
|
|
expect(child.agent.session.snapshotEvents().slice(0, 2)).toEqual(prefix)
|
|
await child.dispose()
|
|
const db = new DatabaseSync(join(root, '.segments/sessions.sqlite'))
|
|
try {
|
|
const tail = db.prepare('SELECT parent_end_seq, base_seq FROM segments WHERE segment_uuid = (SELECT tail_segment_uuid FROM traces WHERE trace_uuid = ?)').get(child.agent.id)
|
|
expect(tail).toMatchObject({ parent_end_seq: prefix.length, base_seq: prefix.length })
|
|
const childRows = db.prepare('SELECT payload FROM events WHERE segment_uuid = (SELECT tail_segment_uuid FROM traces WHERE trace_uuid = ?) ORDER BY seq').all(child.agent.id)
|
|
expect(childRows.map((row) => {
|
|
const event: unknown = JSON.parse(String(row.payload))
|
|
return event
|
|
})).toMatchObject([
|
|
{ type: 'session/end-seed', seq: 2, data: { inherited: true } },
|
|
])
|
|
expect((await ctx.sessionPersistence.stat(parent.id))?.eventCount).toBe(6)
|
|
} finally { db.close() }
|
|
const reader = await ctx.sessionPersistence.open(child.agent.id, 'read')
|
|
try { expect((await reader.read()).events).toEqual(child.agent.session.snapshotEvents()) } finally { await reader.close() }
|
|
})
|