perf(session): reuse immutable event snapshots

This commit is contained in:
_Kerman
2026-08-28 13:43:16 +08:00
parent 5660f44d29
commit bcfec8d1c3
43 changed files with 116 additions and 116 deletions
@@ -67,7 +67,7 @@ describe.skipIf(!process.env.DEEPSEEK_API_KEY)('coding task: fix a failing test
await waitForIdle(ctx, agent)
// The agent claims success…
const summary = finalText([...agent.session.snapshotEvents()]).toLowerCase()
const summary = finalText(agent.session.snapshotEvents()).toLowerCase()
expect(summary.length).toBeGreaterThan(0)
// …and the world agrees: the test passes when WE run it, and the test
@@ -56,7 +56,7 @@ describe.skipIf(!process.env.DEEPSEEK_API_KEY)('compaction: a long session compa
}], source: { kind: 'user' } }))
await waitForIdle(ctx, agent)
const events = [...agent.session.snapshotEvents()]
const events = agent.session.snapshotEvents()
// A compaction ran: the start…end bracket landed in the real log.
const starts = events.filter(e => e.type === 'compaction/start')
@@ -34,7 +34,7 @@ describe.skipIf(!process.env.DEEPSEEK_API_KEY)('full loop: real model + real bas
agent.followup(createUserMessage({ content: [{ type: 'text', text: 'Run `echo e2e-ok` with the bash tool and tell me its exact output.' }], source: { kind: 'user' } }))
await waitForIdle(ctx, agent)
const events = [...agent.session.snapshotEvents()]
const events = agent.session.snapshotEvents()
const calls = events.filter(event => event.type === 'tool/call')
expect(calls.length).toBeGreaterThan(0)
expect(calls.some(event => event.data.name === 'bash')).toBe(true)
@@ -96,7 +96,7 @@ export function waitForIdle(ctx: Context, agent: Agent): Promise<void> {
})
}
export function finalText(events: SessionEvent[]): string {
export function finalText(events: readonly SessionEvent[]): string {
const message = events.findLast(event => event.type === 'assistant/message')
if (message?.type !== 'assistant/message') return ''
return message.data.message.content
@@ -366,7 +366,7 @@ describe.skipIf(!process.env.DEEPSEEK_API_KEY)('PTC mode: real model writes a pr
+ 'and return only the joined string.',
}], source: { kind: 'user' } }))
await waitForIdle(ctx, agent)
const events: SessionEvent[] = [...agent.session.snapshotEvents()]
const events: readonly SessionEvent[] = agent.session.snapshotEvents()
// The wire contract: every request this session made offered EXACTLY ONE
// tool — run_code (the logged header snapshots the assembled list).
@@ -418,7 +418,7 @@ describe.skipIf(!process.env.DEEPSEEK_API_KEY)('PTC mode: real model writes a pr
}], source: { kind: 'user' } }))
await waitForIdle(ctx, handle.agent)
const events: SessionEvent[] = [...handle.agent.session.snapshotEvents()]
const events: readonly SessionEvent[] = handle.agent.session.snapshotEvents()
const dispatch = events.find(event => event.type === 'tool/code-dispatch' && event.data.name === 'read')
const outerResult = events.find(event => event.type === 'tool/result')
const workspaceContext = await vi.waitFor(() => {
@@ -63,6 +63,6 @@ describe.skipIf(!process.env.DEEPSEEK_API_KEY)('resume: continue a persisted ses
await waitForIdle(ctx, resumed)
// The model recalls it — only possible from the resumed history.
expect(finalText([...resumed.session.snapshotEvents()])).toContain(SECRET)
expect(finalText(resumed.session.snapshotEvents())).toContain(SECRET)
}, 180_000)
})
@@ -37,7 +37,7 @@ describe.skipIf(!process.env.DEEPSEEK_API_KEY)('todo_write: real model records a
+ 'Send all three in one todo_write call, then reply with the single word DONE.' }], source: { kind: 'user' } }))
await waitForIdle(ctx, agent)
const events = [...agent.session.snapshotEvents()]
const events = agent.session.snapshotEvents()
// The model actually called the tool.
const calls = events.filter(event => event.type === 'tool/call')