test: adopt saved snapshots in direct Claude sinks

This commit is contained in:
Brennan Benson
2026-11-10 04:17:20 -08:00
parent 73797bffac
commit 4380f2dce6
6 changed files with 30 additions and 8 deletions
@@ -1,3 +1,4 @@
import { followTestSinkSnapshots } from '../native-chat/agent-session-wire/host/testing/structured-agent-session-direct-sink.test-fixture'
import { readTestJournalItems } from '../native-chat/agent-session-journal/journal-observation.test-support'
import { openTestAgentSessionDatabase } from '../runtime/agent-session/records/agent-session-record-store-test-harness'
import { mkdtemp, rm } from 'node:fs/promises'
@@ -55,6 +56,7 @@ async function statusRowsFor(frames: Record<string, unknown>[]) {
mintEpoch: () => 'epoch-1'
})
const deferred = createDeferredStructuredAgentSessionEventSink(testEventSinkLogging())
await followTestSinkSnapshots(deferred, journal)
deferred.bind({ journal, fence: 1, publish: vi.fn() })
const translator = createClaudeJournalTranslator({ sink: deferred.sink, fallbackIdPrefix: '1' })
for (const frame of frames) {
@@ -1,3 +1,4 @@
import { followTestSinkSnapshots } from '../native-chat/agent-session-wire/host/testing/structured-agent-session-direct-sink.test-fixture'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
@@ -80,6 +81,7 @@ async function wiredSession() {
const deferred = createDeferredStructuredAgentSessionEventSink(
testEventSinkLogging(parent.sessionId)
)
await followTestSinkSnapshots(deferred, journal)
deferred.bind({ journal, fence: 7, publish: () => void publish() })
const run = await producer(host, deferred.sink)
const describe = (views: ReturnType<typeof host.getStructuredChildWorkViews>) =>
@@ -1,3 +1,4 @@
import { followTestSinkSnapshots } from '../native-chat/agent-session-wire/host/testing/structured-agent-session-direct-sink.test-fixture'
import { readTestJournalItems } from '../native-chat/agent-session-journal/journal-observation.test-support'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
@@ -114,9 +115,10 @@ async function openJournal(): Promise<AgentSessionJournal> {
})
}
function acquire(journal: AgentSessionJournal) {
async function acquire(journal: AgentSessionJournal) {
const deferred = createDeferredStructuredAgentSessionEventSink(testEventSinkLogging())
const translator = createClaudeJournalTranslator({ sink: deferred.sink, coalesceMs: 0 })
await followTestSinkSnapshots(deferred, journal)
deferred.bind({ journal, fence: 1, publish: () => {} })
const answers: ((value: unknown) => void)[] = []
const unbind = bindClaudeContextUsageCapture(
@@ -148,7 +150,7 @@ const ring = async (journal: AgentSessionJournal) =>
describe('context usage across a restart', () => {
it('resets the ring at a /compact that is the first thing after a restart, then reads the fresh report', async () => {
const journal = await openJournal()
const before = acquire(journal)
const before = await acquire(journal)
before.translator.handle(initFrame(900))
before.translator.handle(userFrame('turn-a', 1_000))
before.translator.handle(assistantFrame('reply-a', 2_000, 150_000))
@@ -157,7 +159,7 @@ describe('context usage across a restart', () => {
expect(await ring(journal)).toMatchObject({ usedTokens: 150_000, windowTokens: 1_000_000 })
before.release()
const after = acquire(journal)
const after = await acquire(journal)
after.translator.handle(compactBoundary(10_000))
await after.settle()
expect(await ring(journal)).toBeNull()
@@ -178,7 +180,7 @@ describe('context usage across a restart', () => {
it('never brings a settled turn back to running with a context write that ran late', async () => {
const journal = await openJournal()
const live = acquire(journal)
const live = await acquire(journal)
live.translator.handle(initFrame(900))
live.translator.handle(userFrame('turn-a', 1_000))
live.translator.handle(assistantFrame('reply-a', 2_000, 20_000))
@@ -1,3 +1,4 @@
import { followTestSinkSnapshots } from '../native-chat/agent-session-wire/host/testing/structured-agent-session-direct-sink.test-fixture'
import { readTestJournalItems } from '../native-chat/agent-session-journal/journal-observation.test-support'
import { agentSessionConversationView } from '../../shared/agent-session/lifecycle/agent-session-conversation-view'
import {
@@ -62,9 +63,10 @@ async function openJournal(): Promise<AgentSessionJournal> {
})
}
function translate(journal: AgentSessionJournal) {
async function translate(journal: AgentSessionJournal) {
const deferred = createDeferredStructuredAgentSessionEventSink(testEventSinkLogging())
const translator = createClaudeJournalTranslator({ sink: deferred.sink, coalesceMs: 0 })
await followTestSinkSnapshots(deferred, journal)
deferred.bind({ journal, fence: 1, publish: () => {} })
const settle = async (): Promise<void> => {
await expect(deferred.drained()).resolves.toEqual({ ok: true })
@@ -73,7 +75,7 @@ function translate(journal: AgentSessionJournal) {
}
function runTurn(
translator: ReturnType<typeof translate>['translator'],
translator: Awaited<ReturnType<typeof translate>>['translator'],
turnId: string,
at: number,
sizes: number[]
@@ -178,7 +180,7 @@ async function catchUp(
describe('context usage for a turn row outside the loaded page', () => {
it('marks a live context write on a turn row the window cannot take, and the host answer carries it', async () => {
const journal = await openJournal()
const { translator, settle } = translate(journal)
const { translator, settle } = await translate(journal)
runTurn(translator, 'turn-a', 1_000, [120_000, 130_000, 140_000, 150_000])
translator.handle(resultFrame(2_000, { 'claude-fable-5-1[1m]': { contextWindow: 1_000_000 } }))
runTurn(translator, 'turn-b', 3_000, [200_000, 210_000, 220_000, 230_000])
@@ -201,7 +203,7 @@ describe('context usage for a turn row outside the loaded page', () => {
it('keeps the ring on the newest facts while a live turn outgrows the retained window', async () => {
const journal = await openJournal()
const { translator, settle } = translate(journal)
const { translator, settle } = await translate(journal)
runTurn(translator, 'turn-a', 1_000, [120_000])
translator.handle(resultFrame(2_000, { 'claude-fable-5-1[1m]': { contextWindow: 1_000_000 } }))
runTurn(translator, 'turn-b', 3_000, [])
@@ -1,3 +1,4 @@
import { followTestSinkSnapshots } from '../native-chat/agent-session-wire/host/testing/structured-agent-session-direct-sink.test-fixture'
import { readTestJournalItems } from '../native-chat/agent-session-journal/journal-observation.test-support'
import { openTestAgentSessionDatabase } from '../runtime/agent-session/records/agent-session-record-store-test-harness'
import { execFileSync, type ChildProcess } from 'node:child_process'
@@ -281,6 +282,7 @@ describe('Claude stream-json connection', () => {
mintEpoch: () => 'epoch-1'
})
const deferred = createDeferredStructuredAgentSessionEventSink(testEventSinkLogging())
await followTestSinkSnapshots(deferred, journal)
deferred.bind({ journal, fence: 1, publish: vi.fn() })
const translator = createClaudeJournalTranslator({ sink: deferred.sink })
let settled = false
@@ -0,0 +1,12 @@
import type { SqlAgentSessionJournal } from '../../../agent-session-journal/journal-sql-store'
import type { DeferredStructuredAgentSessionEventSink } from '../../journal-ingress/structured-agent-session-event-sink'
import { StructuredAgentSessionSnapshotRoutes } from '../structured-agent-session-snapshot-routes'
import { testEventSinkLogging } from './structured-agent-session-logger.test-fixture'
export async function followTestSinkSnapshots(
sink: DeferredStructuredAgentSessionEventSink,
journal: SqlAgentSessionJournal
): Promise<void> {
const routes = new StructuredAgentSessionSnapshotRoutes(testEventSinkLogging().logger)
await sink.followSnapshots(routes.observe(journal.identity.sessionId, journal))
}