mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 10:58:10 +00:00
Port Claude tests to SQL journal named reads
This commit is contained in:
@@ -7,6 +7,11 @@ import {
|
||||
import type { AgentSessionRecord } from '../../shared/agent-session/lifecycle/agent-session-record'
|
||||
import { agentChildWorkLiveness } from '../../shared/agent-status/child-work/agent-status-child-work-liveness'
|
||||
import { conversationCommandBlocked } from '../native-chat/agent-session-wire/operations/structured-conversation-command-admission'
|
||||
import type { AgentSessionJournal } from '../native-chat/agent-session-journal/journal-store'
|
||||
import type {
|
||||
JournalWorkAnswer,
|
||||
JournalWorkRead
|
||||
} from '../native-chat/agent-session-journal/journal-work-read'
|
||||
import type { CapturedFrame } from './claude-captured-frame-builders.test-fixture'
|
||||
import { RESUMED_BY_MESSAGE } from './claude-captured-task-frames.test-fixture'
|
||||
import { hostWithParent, parent, producer } from './claude-child-work-producer-harness.test-fixture'
|
||||
@@ -18,6 +23,23 @@ afterEach(() => vi.restoreAllMocks())
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: admission reads only the lease and the command/rewind records, all absent here.
|
||||
const RECORD = { lease: {} } as unknown as AgentSessionRecord
|
||||
|
||||
/** A live generation with no turn, prompt or owed send: only child work can block. */
|
||||
const IDLE_WORK: JournalWorkAnswer<'state'>['state'] = {
|
||||
liveFence: 7,
|
||||
allowsObservation: true,
|
||||
working: false,
|
||||
commandAdmission: { promptPending: false, owesSend: false, handoverOwesSend: false },
|
||||
owesAnySend: false,
|
||||
owedEnteredSubmissions: [],
|
||||
owedSendingSubmissions: [],
|
||||
activeTurn: null,
|
||||
latestTurn: null,
|
||||
actionablePrompts: [],
|
||||
restatedSubmissions: [],
|
||||
viewKey: '',
|
||||
workViewKey: ''
|
||||
}
|
||||
|
||||
/** Host records retain live work after provider roster removal. */
|
||||
async function droppedFromRoster() {
|
||||
const host = hostWithParent()
|
||||
@@ -33,17 +55,11 @@ async function droppedFromRoster() {
|
||||
{
|
||||
sessionId: 'session-1',
|
||||
fence: 7,
|
||||
journal: {
|
||||
itemsRead: () => ({ items: [] }),
|
||||
cursor: () => ({ epoch: 'e', sequence: 0 }),
|
||||
item: () => null,
|
||||
submissions: () => [],
|
||||
inputOrder: { recoverableSubmissions: () => [] },
|
||||
runningTurn: () => null,
|
||||
itemFence: () => undefined,
|
||||
visitItems: () => undefined,
|
||||
wroteBeforeOpen: () => false
|
||||
},
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: admission reads only through readWork.
|
||||
journal: {} as AgentSessionJournal,
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: admission reads only the state answer.
|
||||
readWork: async <K extends JournalWorkRead['kind']>() =>
|
||||
({ kind: 'state', state: IDLE_WORK }) as JournalWorkAnswer<K>,
|
||||
adapter: run.adapter
|
||||
},
|
||||
RECORD,
|
||||
@@ -55,7 +71,7 @@ async function droppedFromRoster() {
|
||||
{ membership: 'live', stoppable: true, providerId: 'agent-1', kind: 'agent' }
|
||||
])
|
||||
expect(run.adapter.backgroundTaskState('session-1')).toBeNull()
|
||||
expect(blocked()?.message).toBe('Stop background tasks before using this command.')
|
||||
expect((await blocked())?.message).toBe('Stop background tasks before using this command.')
|
||||
// Snapshot stop targets before dispatch; settle only acknowledged records.
|
||||
const stop = async (taskId?: string) => {
|
||||
const targets = agentChildWorkStopTargets(records(), taskId)
|
||||
@@ -87,7 +103,7 @@ describe('a background Stop acts on the host child records', () => {
|
||||
expect(stopCalls()).toEqual([{ subtype: 'stop_task', params: { taskId: 'agent-1' } }])
|
||||
expect(records()).toMatchObject([{ membership: 'settled', outcome: 'cancelled' }])
|
||||
expect(basis()).toEqual(['stop-acknowledged'])
|
||||
expect(blocked()).toBeNull()
|
||||
expect(await blocked()).toBeNull()
|
||||
})
|
||||
|
||||
it('leaves a task that ended on its own frame as it ended: its row offers no stop', async () => {
|
||||
|
||||
@@ -1,65 +1,59 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { AgentJournalSubmission } from '../../shared/agent-session/journal/agent-session-journal-types'
|
||||
import { mkdtemp, rm } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { AGENT_JOURNAL_THREAD_SCOPE } from '../../shared/agent-session/journal/agent-session-journal-types'
|
||||
import { structuredRunningChildWork } from '../../shared/agent-status/child-work/agent-child-work-listing'
|
||||
import type { AgentHookServer } from '../agent-hooks/server'
|
||||
import type { StructuredAgentSessionJournalProjections } from '../native-chat/agent-session-wire/observations/structured-agent-session-status-journal-projection'
|
||||
import { readTestJournalItems } from '../native-chat/agent-session-journal/journal-observation.test-support'
|
||||
import { createTrackedJournalOpener } from '../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support'
|
||||
import { createDeferredStructuredAgentSessionEventSink } from '../native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink'
|
||||
import { testEventSinkLogging } from '../native-chat/agent-session-wire/host/testing/structured-agent-session-logger.test-fixture'
|
||||
import { StructuredAgentSessionStatusFeed } from '../native-chat/agent-session-wire/observations/structured-agent-session-status-feed'
|
||||
import { indexedStatusFeedSession } from '../native-chat/agent-session-wire/observations/testing/structured-agent-session-status-feed-test-session.test-fixture'
|
||||
import type { CapturedFrame } from './claude-captured-frame-builders.test-fixture'
|
||||
import { backgroundWakeCapture } from './claude-captured-fold-steer-frames.test-fixture'
|
||||
import { MOVED_TO_BACKGROUND } from './claude-captured-task-frames.test-fixture'
|
||||
import { hostWithParent, parent, producer } from './claude-child-work-producer-harness.test-fixture'
|
||||
import { PROVIDER_SESSION_ID } from './structured/claude-structured-session-test-support'
|
||||
import {
|
||||
identityFor,
|
||||
PROVIDER_SESSION_ID
|
||||
} from './structured/claude-structured-session-test-support'
|
||||
import { createStructuredAgentSessionLogger } from '../native-chat/agent-session-wire/host/structured-agent-session-logger'
|
||||
|
||||
vi.mock('../telemetry/client', () => ({ track: vi.fn() }))
|
||||
vi.mock('../telemetry/cohort-classifier', () => ({ getCohortAtEmit: vi.fn(() => ({})) }))
|
||||
afterEach(() => vi.restoreAllMocks())
|
||||
|
||||
type Journal = Parameters<StructuredAgentSessionJournalProjections['read']>[0]
|
||||
const journals = createTrackedJournalOpener()
|
||||
let root: string
|
||||
|
||||
beforeEach(async () => {
|
||||
root = await mkdtemp(join(tmpdir(), 'orca-claude-wake-retention-'))
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await journals.closeAll()
|
||||
await rm(root, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
async function wiredSession() {
|
||||
const host: AgentHookServer = hostWithParent()
|
||||
const run = await producer(host)
|
||||
const submissions: AgentJournalSubmission[] = []
|
||||
let sequence = 0
|
||||
const journal = {
|
||||
cursor: () => ({ epoch: 'epoch-1', sequence: ++sequence }),
|
||||
lastActivityAt: () => 1,
|
||||
submissions: (): ReturnType<Journal['submissions']> =>
|
||||
submissions.map((submission) => ({ ...submission })),
|
||||
item: (itemId: string): ReturnType<Journal['item']> => {
|
||||
const item = run.journalItems.get(itemId)
|
||||
return item ? { ...item, revision: 1, observedAt: run.now() } : null
|
||||
},
|
||||
statusRead(): ReturnType<Journal['statusRead']> {
|
||||
return this.snapshot()
|
||||
},
|
||||
inputOrder: {
|
||||
inputCount: () => submissions.length,
|
||||
inputOrdinal: (id: string) => submissions.findIndex((s) => s.clientMessageId === id),
|
||||
recoverableSubmissions: () =>
|
||||
submissions.filter((s) => s.dispatchState === 'pending' || s.dispatchState === 'unknown')
|
||||
},
|
||||
context: { floor: () => null },
|
||||
stopMarks: { latest: () => null, revision: () => 0 },
|
||||
itemFence: () => undefined,
|
||||
wroteBeforeOpen: () => false,
|
||||
runningTurn: () => null,
|
||||
snapshot: () => ({
|
||||
items: [...run.journalItems.values()]
|
||||
.sort((a, b) => a.sequence - b.sequence)
|
||||
.map((item) => ({ ...item, revision: 1, observedAt: run.now() })),
|
||||
submissions: submissions.map((submission) => ({ ...submission }))
|
||||
})
|
||||
}
|
||||
const journal = await journals.open({
|
||||
identity: identityFor(parent.sessionId),
|
||||
stateDirectory: root
|
||||
})
|
||||
const feed = new StructuredAgentSessionStatusFeed({
|
||||
logger: createStructuredAgentSessionLogger(),
|
||||
sessions: new Map([
|
||||
[
|
||||
parent.sessionId,
|
||||
{
|
||||
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the fixture supplies the feed's cursor, clock, Stop marks, context floor, snapshot, submissions, item and provenance reads.
|
||||
journal: journal as unknown as Journal,
|
||||
...indexedStatusFeedSession({
|
||||
journal,
|
||||
child: { generation: 'spawn-9', fence: 7, phase: 'ready' },
|
||||
provider: 'claude'
|
||||
}),
|
||||
params: {
|
||||
location: {
|
||||
executionHostId: parent.executionHostId,
|
||||
@@ -82,53 +76,68 @@ async function wiredSession() {
|
||||
readChildWork: (subject) => host.getStructuredChildWorkViews(subject)
|
||||
})
|
||||
})
|
||||
const publish = () => feed.publish(parent.sessionId)
|
||||
const deferred = createDeferredStructuredAgentSessionEventSink(
|
||||
testEventSinkLogging(parent.sessionId)
|
||||
)
|
||||
deferred.bind({ journal, fence: 7, publish: () => void publish() })
|
||||
const run = await producer(host, deferred.sink)
|
||||
const describe = (views: ReturnType<typeof host.getStructuredChildWorkViews>) =>
|
||||
views.map((view) => `${view.description}:${view.membership}:${view.outcome ?? ''}`)
|
||||
const records = () => describe(host.getStructuredChildWorkViews(parent))
|
||||
const running = () =>
|
||||
describe(structuredRunningChildWork(host.getStructuredChildWorkViews(parent)))
|
||||
const replay = (frames: readonly CapturedFrame[]) => {
|
||||
const replay = async (frames: readonly CapturedFrame[]) => {
|
||||
for (const captured of frames) {
|
||||
run.replay([captured])
|
||||
feed.publish(parent.sessionId)
|
||||
expect(await deferred.drained()).toEqual({ ok: true })
|
||||
await publish()
|
||||
}
|
||||
}
|
||||
const send = (clientMessageId: string): AgentJournalSubmission => {
|
||||
const submission: AgentJournalSubmission = {
|
||||
const send = async (clientMessageId: string): Promise<string> => {
|
||||
await journal.appendSubmission({
|
||||
clientMessageId,
|
||||
fence: 7,
|
||||
payloadFingerprint: clientMessageId,
|
||||
dispatchState: 'pending',
|
||||
providerItemId: null,
|
||||
reason: null,
|
||||
submittedAt: run.now(),
|
||||
resolvedAt: null
|
||||
}
|
||||
submissions.push(submission)
|
||||
feed.publish(parent.sessionId)
|
||||
return submission
|
||||
fence: 7,
|
||||
body: { kind: 'message', role: 'user', blocks: [{ type: 'text', text: clientMessageId }] }
|
||||
})
|
||||
await journal.resolveDispatch({
|
||||
clientMessageId,
|
||||
state: 'pending',
|
||||
fence: 7,
|
||||
turnScope: AGENT_JOURNAL_THREAD_SCOPE
|
||||
})
|
||||
await publish()
|
||||
return clientMessageId
|
||||
}
|
||||
const accept = (submission: AgentJournalSubmission) => {
|
||||
submission.dispatchState = 'accepted'
|
||||
feed.publish(parent.sessionId)
|
||||
const accept = async (clientMessageId: string) => {
|
||||
await journal.resolveDispatch({
|
||||
clientMessageId,
|
||||
state: 'accepted',
|
||||
fence: 7,
|
||||
providerIdentity: null
|
||||
})
|
||||
await publish()
|
||||
}
|
||||
const rootTurns = () =>
|
||||
[...run.journalItems.values()].filter((item) => !item.agentId && item.body.kind === 'turn')
|
||||
const rootTurns = async () =>
|
||||
(await readTestJournalItems(journal)).filter(
|
||||
(item) => !item.agentId && item.body.kind === 'turn'
|
||||
)
|
||||
return { run, feed, records, running, replay, send, accept, rootTurns }
|
||||
}
|
||||
|
||||
describe("a finished child's record stays until the user's next turn", () => {
|
||||
it("keeps a background agent through Claude's own wake turn, then retires it at the next send", async () => {
|
||||
const session = await wiredSession()
|
||||
session.replay(MOVED_TO_BACKGROUND)
|
||||
expect(session.rootTurns()).toHaveLength(2)
|
||||
await session.replay(MOVED_TO_BACKGROUND)
|
||||
expect(await session.rootTurns()).toHaveLength(2)
|
||||
expect(session.records()).toContain('Run 45s sleep command:settled:succeeded')
|
||||
expect(session.running()).toEqual([])
|
||||
|
||||
const next = session.send('next-send')
|
||||
const next = await session.send('next-send')
|
||||
// Submitting is not acceptance; a refused send must not retire retained work.
|
||||
expect(session.records()).toContain('Run 45s sleep command:settled:succeeded')
|
||||
session.accept(next)
|
||||
await session.accept(next)
|
||||
expect(session.records()).toEqual([])
|
||||
})
|
||||
|
||||
@@ -139,19 +148,19 @@ describe("a finished child's record stays until the user's next turn", () => {
|
||||
first: 'first-uuid',
|
||||
steer: 'steer-uuid'
|
||||
})
|
||||
session.accept(session.send('first-uuid'))
|
||||
session.replay(firstTurn)
|
||||
await session.accept(await session.send('first-uuid'))
|
||||
await session.replay(firstTurn)
|
||||
expect(session.running()).toEqual(['Sleep then print marker:live:'])
|
||||
|
||||
const wakeInit = wake.findIndex((captured) => captured.frame.subtype === 'init')
|
||||
session.replay(wake.slice(0, wakeInit + 1))
|
||||
expect(session.rootTurns()).toHaveLength(2)
|
||||
await session.replay(wake.slice(0, wakeInit + 1))
|
||||
expect(await session.rootTurns()).toHaveLength(2)
|
||||
expect(session.records()).toEqual(['Sleep then print marker:settled:succeeded'])
|
||||
expect(session.running()).toEqual([])
|
||||
|
||||
session.accept(session.send('steer-uuid'))
|
||||
await session.accept(await session.send('steer-uuid'))
|
||||
expect(session.records()).toEqual([])
|
||||
session.replay(wake.slice(wakeInit + 1))
|
||||
await session.replay(wake.slice(wakeInit + 1))
|
||||
expect(session.records()).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
@@ -90,8 +90,9 @@ export function hostWithParent(): AgentHookServer {
|
||||
return host
|
||||
}
|
||||
|
||||
/** With `host`, evidence goes through the host's own ingest instead of straight to reconciliation. */
|
||||
export async function producer(host?: AgentHookServer) {
|
||||
/** With `host`, evidence goes through the host's own ingest instead of straight to reconciliation;
|
||||
* with `events`, rows go to that sink instead of the in-memory recorder. */
|
||||
export async function producer(host?: AgentHookServer, events?: StructuredAgentSessionEventSink) {
|
||||
const claude = fakeClaude()
|
||||
const store = createAgentStatusStore({ epoch: 'epoch-1', mode: 'authority' })
|
||||
expect(store.applyMutation({ parent: { subject: parent } })).not.toBeNull()
|
||||
@@ -157,7 +158,7 @@ export async function producer(host?: AgentHookServer) {
|
||||
identity: identityFor(),
|
||||
fence: 7,
|
||||
spawnToken: 'spawn-9',
|
||||
events: journal
|
||||
events: events ?? journal
|
||||
})
|
||||
const send = (message: Record<string, unknown>): Delivery[] => {
|
||||
const from = deliveries.length
|
||||
|
||||
@@ -142,8 +142,8 @@ function acquire(journal: AgentSessionJournal) {
|
||||
return { translator, answers, settle, detach, reattach, release }
|
||||
}
|
||||
|
||||
const ring = (journal: AgentSessionJournal) =>
|
||||
selectStructuredAgentContextUsage(journal.snapshot().items)
|
||||
const ring = async (journal: AgentSessionJournal) =>
|
||||
selectStructuredAgentContextUsage(await readTestJournalItems(journal))
|
||||
|
||||
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 () => {
|
||||
@@ -154,20 +154,20 @@ describe('context usage across a restart', () => {
|
||||
before.translator.handle(assistantFrame('reply-a', 2_000, 150_000))
|
||||
before.translator.handle(resultFrame(3_000))
|
||||
await before.settle()
|
||||
expect(ring(journal)).toMatchObject({ usedTokens: 150_000, windowTokens: 1_000_000 })
|
||||
expect(await ring(journal)).toMatchObject({ usedTokens: 150_000, windowTokens: 1_000_000 })
|
||||
before.release()
|
||||
|
||||
const after = acquire(journal)
|
||||
after.translator.handle(compactBoundary(10_000))
|
||||
await after.settle()
|
||||
expect(ring(journal)).toBeNull()
|
||||
expect(await ring(journal)).toBeNull()
|
||||
|
||||
after.translator.handle(resultFrame(11_000))
|
||||
await after.settle()
|
||||
expect(ring(journal)).toBeNull()
|
||||
expect(await ring(journal)).toBeNull()
|
||||
after.answers.at(-1)?.(postCompactionReport)
|
||||
await after.settle()
|
||||
expect(ring(journal)).toMatchObject({
|
||||
expect(await ring(journal)).toMatchObject({
|
||||
usedTokens: 40_000,
|
||||
windowTokens: 1_000_000,
|
||||
estimated: false,
|
||||
|
||||
@@ -114,8 +114,15 @@ function readOptions(journal: AgentSessionJournal, agents: StructuredAgentRegist
|
||||
return readStructuredAgentSessionOptions(context, SESSION, { readsWithoutModel: true })
|
||||
}
|
||||
|
||||
function attachTail(journal: AgentSessionJournal, limit: number): StructuredAgentSessionState {
|
||||
const read = readAgentSessionHistory(journal, { sessionId: SESSION, direction: 'tail', limit })
|
||||
async function attachTail(
|
||||
journal: AgentSessionJournal,
|
||||
limit: number
|
||||
): Promise<StructuredAgentSessionState> {
|
||||
const read = await readAgentSessionHistory(journal, {
|
||||
sessionId: SESSION,
|
||||
direction: 'tail',
|
||||
limit
|
||||
})
|
||||
if (!read.ok) {
|
||||
throw new Error('tail read failed')
|
||||
}
|
||||
@@ -125,11 +132,11 @@ function attachTail(journal: AgentSessionJournal, limit: number): StructuredAgen
|
||||
})
|
||||
}
|
||||
|
||||
function applyLive(
|
||||
async function applyLive(
|
||||
journal: AgentSessionJournal,
|
||||
state: StructuredAgentSessionState
|
||||
): StructuredAgentSessionState {
|
||||
const read = readAgentSessionHistory(journal, {
|
||||
): Promise<StructuredAgentSessionState> {
|
||||
const read = await readAgentSessionHistory(journal, {
|
||||
sessionId: SESSION,
|
||||
direction: 'after',
|
||||
cursor: state.cursor ?? undefined
|
||||
@@ -156,14 +163,14 @@ function applyLive(
|
||||
const hasTurnRow = (state: StructuredAgentSessionState): boolean =>
|
||||
state.items.some((item) => readAgentJournalTurn(item.body) !== null)
|
||||
|
||||
function catchUp(
|
||||
async function catchUp(
|
||||
journal: AgentSessionJournal,
|
||||
state: StructuredAgentSessionState
|
||||
): StructuredAgentSessionState {
|
||||
let next = applyLive(journal, state)
|
||||
): Promise<StructuredAgentSessionState> {
|
||||
let next = await applyLive(journal, state)
|
||||
while (next.cursor?.sequence !== state.cursor?.sequence) {
|
||||
state = next
|
||||
next = applyLive(journal, state)
|
||||
next = await applyLive(journal, state)
|
||||
}
|
||||
return next
|
||||
}
|
||||
@@ -176,13 +183,13 @@ describe('context usage for a turn row outside the loaded page', () => {
|
||||
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])
|
||||
await settle()
|
||||
const attached = attachTail(journal, 3)
|
||||
const attached = await attachTail(journal, 3)
|
||||
expect(hasTurnRow(attached)).toBe(false)
|
||||
expect(attached.unloadedTurnRevisions).toBeUndefined()
|
||||
|
||||
translator.handle(assistantFrame('turn-b-reply-late', 4_000, 250_000))
|
||||
await settle()
|
||||
const live = applyLive(journal, attached)
|
||||
const live = await applyLive(journal, attached)
|
||||
|
||||
expect(hasTurnRow(live)).toBe(false)
|
||||
expect(live.unloadedTurnRevisions).toBe(1)
|
||||
@@ -201,7 +208,7 @@ describe('context usage for a turn row outside the loaded page', () => {
|
||||
await settle()
|
||||
const hostAnswer = async () =>
|
||||
(await readOptions(journal, claudeAndCodexAgents())).contextUsage?.current
|
||||
let state = attachTail(journal, 200)
|
||||
let state = await attachTail(journal, 200)
|
||||
let host = await hostAnswer()
|
||||
const respond = (index: number): void => {
|
||||
translator.handle(assistantFrame(`turn-b-reply-${index}`, 4_000 + index, 200_000 + index))
|
||||
@@ -214,7 +221,7 @@ describe('context usage for a turn row outside the loaded page', () => {
|
||||
}
|
||||
}
|
||||
await settle()
|
||||
state = catchUp(journal, state)
|
||||
state = await catchUp(journal, state)
|
||||
expect(hasTurnRow(state)).toBe(true)
|
||||
|
||||
let stepsAfterTrim = 0
|
||||
@@ -222,7 +229,7 @@ describe('context usage for a turn row outside the loaded page', () => {
|
||||
respond(index++)
|
||||
await settle()
|
||||
const seen = state.unloadedTurnRevisions
|
||||
state = catchUp(journal, state)
|
||||
state = await catchUp(journal, state)
|
||||
if (state.unloadedTurnRevisions !== seen) {
|
||||
host = await hostAnswer()
|
||||
}
|
||||
|
||||
@@ -8,16 +8,16 @@ import { join } from 'node:path'
|
||||
import { isDeepStrictEqual } from 'node:util'
|
||||
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
|
||||
import { parseAgentJournalItemKey } from '../../shared/agent-session/journal/agent-session-journal-item-key'
|
||||
import type {
|
||||
AgentJournalRenderItem,
|
||||
AgentJournalResolution
|
||||
import {
|
||||
AGENT_JOURNAL_THREAD_SCOPE,
|
||||
type AgentJournalRenderItem,
|
||||
type AgentJournalResolution
|
||||
} from '../../shared/agent-session/journal/agent-session-journal-types'
|
||||
import type { AgentChildWorkEvidence } from '../../shared/agent-status/child-work/agent-status-child-work-evidence'
|
||||
import type { AgentStatusStructuredSessionSubject } from '../../shared/agent-status/contracts/agent-status-subject'
|
||||
import { AgentHookServer } from '../agent-hooks/server'
|
||||
import type { AgentSessionJournal } from '../native-chat/agent-session-journal/journal-store'
|
||||
import { createTrackedJournalOpener } from '../native-chat/agent-session-journal/store-lifecycle/journal-host-database-test-support'
|
||||
import { StructuredAgentSessionCurrentWork } from '../native-chat/agent-session-wire/observations/structured-agent-session-current-work'
|
||||
import { createDeferredStructuredAgentSessionEventSink } from '../native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink'
|
||||
import { testEventSinkLogging } from '../native-chat/agent-session-wire/host/testing/structured-agent-session-logger.test-fixture'
|
||||
import { createStructuredAgentSessionLogger } from '../native-chat/agent-session-wire/host/structured-agent-session-logger'
|
||||
@@ -125,19 +125,28 @@ async function pipeline() {
|
||||
const viewOf = (providerId: string) =>
|
||||
published &&
|
||||
server.getStructuredChildWorkViews(published).find((view) => view.providerId === providerId)
|
||||
// Read the cards in command order, so each entry sees exactly the journal it published.
|
||||
const askersRead: Promise<void>[] = []
|
||||
const record = (subject: AgentStatusStructuredSessionSubject): void => {
|
||||
published = subject
|
||||
const cards = pendingCards(journal.snapshot().items)
|
||||
publishes.push({
|
||||
const entry: Publish = {
|
||||
waiting: server
|
||||
.getStructuredChildWorkViews(subject)
|
||||
.flatMap((view) =>
|
||||
view.state === 'waiting' && view.membership === 'live' ? [view.providerId ?? '?'] : []
|
||||
),
|
||||
askers: cards.flatMap((card) => (card.agentId ? [card.agentId] : [])),
|
||||
askers: [],
|
||||
row: parentRow(server),
|
||||
unwaited: parentRow(unwaited)
|
||||
})
|
||||
}
|
||||
publishes.push(entry)
|
||||
askersRead.push(
|
||||
journal.readInOrder(async (reads) => {
|
||||
entry.askers = pendingCards(await readTestJournalItems({ reads })).flatMap((card) =>
|
||||
card.agentId ? [card.agentId] : []
|
||||
)
|
||||
})
|
||||
)
|
||||
}
|
||||
const feed = new StructuredAgentSessionStatusFeed({
|
||||
logger: createStructuredAgentSessionLogger(),
|
||||
@@ -217,13 +226,15 @@ async function pipeline() {
|
||||
expect(await deferred.drained()).toEqual({ ok: true })
|
||||
await new Promise((resolve) => setTimeout(resolve, 0))
|
||||
}
|
||||
await Promise.all(askersRead)
|
||||
}
|
||||
const hostRecords =
|
||||
(resolution: Pick<AgentJournalResolution, 'state' | 'selectedOptionId'>, itemId = cardId()) =>
|
||||
(resolution: Pick<AgentJournalResolution, 'state' | 'selectedOptionId'>, itemId?: string) =>
|
||||
async (): Promise<void> => {
|
||||
await deferred.drained()
|
||||
const recorded = itemId ?? (await cardId())
|
||||
const card = pendingCards(await readTestJournalItems(journal)).find(
|
||||
(item) => item.itemId === itemId
|
||||
(item) => item.itemId === recorded
|
||||
)
|
||||
const identity = card && parseAgentJournalItemKey(card.itemId)
|
||||
if (card?.body.kind !== 'approval' || !identity) {
|
||||
@@ -232,14 +243,11 @@ async function pipeline() {
|
||||
await journal.appendItem(
|
||||
identity,
|
||||
{ ...card.body, resolution: { ...resolution, resolvedBy: 'client-1', resolvedAt: now() } },
|
||||
{
|
||||
fence: FENCE,
|
||||
turnScope: new StructuredAgentSessionCurrentWork(journal, FENCE).turnScope()
|
||||
}
|
||||
{ fence: FENCE, turnScope: card.turnScope ?? AGENT_JOURNAL_THREAD_SCOPE }
|
||||
)
|
||||
}
|
||||
const cardId = (): string => {
|
||||
const card = pendingCards(journal.snapshot().items)[0]
|
||||
const cardId = async (): Promise<string> => {
|
||||
const card = pendingCards(await readTestJournalItems(journal))[0]
|
||||
if (!card) {
|
||||
throw new Error('no pending card')
|
||||
}
|
||||
@@ -317,15 +325,15 @@ async function pipeline() {
|
||||
|
||||
type Pipeline = Awaited<ReturnType<typeof pipeline>>
|
||||
|
||||
function answer(
|
||||
async function answer(
|
||||
run: Pipeline,
|
||||
optionId: 'allow' | 'deny',
|
||||
commit = run.hostRecords({ state: 'resolved', selectedOptionId: optionId }),
|
||||
itemId = run.cardId()
|
||||
itemId?: string
|
||||
) {
|
||||
return run.adapter.answerPrompt({
|
||||
sessionId: SESSION,
|
||||
itemId,
|
||||
itemId: itemId ?? (await run.cardId()),
|
||||
kind: 'approval',
|
||||
response: { kind: 'option', optionId },
|
||||
fence: FENCE,
|
||||
@@ -344,7 +352,7 @@ describe("a Claude subagent's permission request", () => {
|
||||
run.deferred.unbind()
|
||||
run.raise('req-late', 'toolu_late', ASKER)
|
||||
await new Promise((resolve) => setTimeout(resolve, 0))
|
||||
run.publishStatus()
|
||||
await run.publishStatus()
|
||||
expect(run.publishes.at(-1)?.waiting).toEqual([])
|
||||
run.deferred.bind(run.target)
|
||||
await run.settle()
|
||||
@@ -376,9 +384,10 @@ describe("a Claude subagent's permission request", () => {
|
||||
run.raise('req-two', 'toolu_two_bash', 'agent-two')
|
||||
await run.settle()
|
||||
expect([...(run.publishes.at(-1)?.waiting ?? [])].sort()).toEqual([ASKER, 'agent-two'].sort())
|
||||
const cardOf = (agentId: string) =>
|
||||
pendingCards(run.journal.snapshot().items).find((card) => card.agentId === agentId)?.itemId
|
||||
const first = cardOf(ASKER)
|
||||
const cardOf = async (agentId: string) =>
|
||||
pendingCards(await readTestJournalItems(run.journal)).find((card) => card.agentId === agentId)
|
||||
?.itemId
|
||||
const first = await cardOf(ASKER)
|
||||
await answer(
|
||||
run,
|
||||
'allow',
|
||||
@@ -387,7 +396,7 @@ describe("a Claude subagent's permission request", () => {
|
||||
)
|
||||
await run.settle()
|
||||
expect(run.publishes.at(-1)?.waiting).toEqual(['agent-two'])
|
||||
const second = cardOf('agent-two')
|
||||
const second = await cardOf('agent-two')
|
||||
await answer(
|
||||
run,
|
||||
'allow',
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { readTestJournalItems } from '../native-chat/agent-session-journal/journal-observation.test-support'
|
||||
import { mkdtemp, rm } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
@@ -123,14 +124,15 @@ async function acquire(journal: AgentSessionJournal) {
|
||||
return { translator, bind, settle, exit }
|
||||
}
|
||||
|
||||
const proseRow = (journal: AgentSessionJournal, text: string): AgentJournalRenderItem | undefined =>
|
||||
journal
|
||||
.snapshot()
|
||||
.items.find(
|
||||
(item) =>
|
||||
item.body.kind === 'message' &&
|
||||
item.body.blocks.some((block) => block.type === 'text' && block.text === text)
|
||||
)
|
||||
const proseRow = async (
|
||||
journal: AgentSessionJournal,
|
||||
text: string
|
||||
): Promise<AgentJournalRenderItem | undefined> =>
|
||||
(await readTestJournalItems(journal)).find(
|
||||
(item) =>
|
||||
item.body.kind === 'message' &&
|
||||
item.body.blocks.some((block) => block.type === 'text' && block.text === text)
|
||||
)
|
||||
|
||||
describe('a Claude subagent resumed after its provider restarted', () => {
|
||||
it('keeps the canonical id its first run was given, so one child stays one child', async () => {
|
||||
@@ -161,7 +163,7 @@ describe('a Claude subagent resumed after its provider restarted', () => {
|
||||
first.translator.handle(taskFailed('agent-a', 'toolu_spawn_a'))
|
||||
first.translator.handle(result('result-a'))
|
||||
await first.exit()
|
||||
expect(proseRow(journal, 'A reads the rig recipes')).toMatchObject({ agentId: 'agent-a' })
|
||||
expect(await proseRow(journal, 'A reads the rig recipes')).toMatchObject({ agentId: 'agent-a' })
|
||||
|
||||
// The acquisition's saved answers apply before its sink binds.
|
||||
const second = await acquire(journal)
|
||||
@@ -182,18 +184,18 @@ describe('a Claude subagent resumed after its provider restarted', () => {
|
||||
second.bind()
|
||||
await second.settle()
|
||||
|
||||
expect(proseRow(journal, 'A restarts the receiver')).toMatchObject({
|
||||
expect(await proseRow(journal, 'A restarts the receiver')).toMatchObject({
|
||||
agentId: 'agent-a',
|
||||
providerParentRef: 'toolu_spawn_a',
|
||||
producerKind: 'agent'
|
||||
})
|
||||
expect(proseRow(journal, 'B checks the rig')).toMatchObject({
|
||||
expect(await proseRow(journal, 'B checks the rig')).toMatchObject({
|
||||
agentId: 'agent-b',
|
||||
providerParentRef: 'toolu_spawn_b'
|
||||
})
|
||||
expect(proseRow(journal, 'A reads the rig recipes')?.attempt).toBeUndefined()
|
||||
expect(proseRow(journal, 'A restarts the receiver')?.attempt).toBe(2)
|
||||
expect(proseRow(journal, 'B checks the rig')?.attempt).toBe(2)
|
||||
expect((await proseRow(journal, 'A reads the rig recipes'))?.attempt).toBeUndefined()
|
||||
expect((await proseRow(journal, 'A restarts the receiver'))?.attempt).toBe(2)
|
||||
expect((await proseRow(journal, 'B checks the rig'))?.attempt).toBe(2)
|
||||
await second.exit()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -12,7 +12,6 @@ import {
|
||||
} from '../native-chat/agent-session-wire/rewind/structured-rewind-cut'
|
||||
import { agentJournalItemKey } from '../../shared/agent-session/journal/agent-session-journal-item-key'
|
||||
import { readAgentSessionHistory } from '../native-chat/agent-session-wire/publication/history/agent-session-history-page'
|
||||
import { projectJournalBatch } from '../native-chat/agent-session-wire/publication/history/agent-session-journal-batch'
|
||||
import {
|
||||
replacePage,
|
||||
mergeItems
|
||||
@@ -148,7 +147,7 @@ describe('Claude chat catch-up', () => {
|
||||
const key = agentJournalItemKey({ provider: 'claude', sessionId: SID, uuid })
|
||||
expect(await reopened.reads.canonicalItemId(key)).toBe(key)
|
||||
expect(
|
||||
structuredRewindCut(reopened, {
|
||||
await structuredRewindCut(reopened, {
|
||||
itemId: agentJournalItemKey({ provider: 'orca', clientMessageId }),
|
||||
provider: 'claude',
|
||||
nativeId: SID
|
||||
@@ -202,31 +201,30 @@ describe('Claude chat catch-up', () => {
|
||||
const key = agentJournalItemKey({ provider: 'claude', sessionId: SID, uuid: 'cmd-1' })
|
||||
expect(await c.open('a1')).toBe(true)
|
||||
const held = await readTestJournalItems(c.journal)
|
||||
expect(c.rows().filter((row) => row === 'user:follow up')).toHaveLength(1)
|
||||
expect((await c.rows()).filter((row) => row === 'user:follow up')).toHaveLength(1)
|
||||
expect((await c.journal.reads.item(key))?.turnScope).toEqual({ kind: 'unknown' })
|
||||
expect(isStructuredRewindTarget(c.journal, key)).toBe(false)
|
||||
expect(await isStructuredRewindTarget(c.journal, key)).toBe(false)
|
||||
expect(held.filter((row) => row.body.kind === 'turn')).toHaveLength(1)
|
||||
expect(await c.open('a1')).toBe(true)
|
||||
expect(await readTestJournalItems(c.journal)).toEqual(held)
|
||||
const beforeReplay = await c.journal.reads.item(key)
|
||||
const reopened = await c.replay()
|
||||
expect(await reopened.reads.item(key)).toEqual(beforeReplay)
|
||||
expect(isStructuredRewindTarget(reopened, key)).toBe(false)
|
||||
const page = readAgentSessionHistory(reopened, { sessionId: SESSION, direction: 'tail' })
|
||||
const delta = reopened.readSince(cursor)
|
||||
expect(await isStructuredRewindTarget(reopened, key)).toBe(false)
|
||||
const page = await readAgentSessionHistory(reopened, {
|
||||
sessionId: SESSION,
|
||||
direction: 'tail'
|
||||
})
|
||||
const delta = await readAgentSessionHistory(reopened, {
|
||||
sessionId: SESSION,
|
||||
direction: 'after',
|
||||
cursor
|
||||
})
|
||||
if (!page.ok || !delta.ok) {
|
||||
throw new Error('history unavailable')
|
||||
}
|
||||
const batch = projectJournalBatch({
|
||||
rows: delta.rows,
|
||||
snapshot: await readTestJournalSnapshot(reopened),
|
||||
afterSequence: cursor.sequence
|
||||
})
|
||||
if (!batch.ok) {
|
||||
throw new Error('batch unavailable')
|
||||
}
|
||||
const snapshotClient = replacePage(page.page, 1)
|
||||
const liveClient = mergeItems([], batch.batch.items, [])
|
||||
const liveClient = mergeItems([], delta.page.items, [])
|
||||
expect(snapshotClient.items.find((item) => item.itemId === key)).toEqual(
|
||||
await c.journal.reads.item(key)
|
||||
)
|
||||
@@ -251,20 +249,17 @@ describe('Claude chat catch-up', () => {
|
||||
const resumed = items.find((row) => row.body.kind === 'turn' && row.body.turnId === 'a2')
|
||||
expect(resumed?.body).toMatchObject({ userItemId: resumed?.itemId })
|
||||
const opener = agentJournalItemKey({ provider: 'claude', sessionId: SID, uuid: 'p3' })
|
||||
expect(isStructuredRewindTarget(c.journal, opener)).toBe(true)
|
||||
expect(await isStructuredRewindTarget(c.journal, opener)).toBe(true)
|
||||
expect(await readTestJournalSubmissions(c.journal)).toEqual(submissions)
|
||||
expect(c.rows().filter((row) => row === 'user:follow up')).toHaveLength(1)
|
||||
expect((await c.rows()).filter((row) => row === 'user:follow up')).toHaveLength(1)
|
||||
expect(c.warn).not.toHaveBeenCalled()
|
||||
const cut = structuredRewindCut(c.journal, {
|
||||
itemId: opener,
|
||||
provider: 'claude',
|
||||
nativeId: SID
|
||||
})
|
||||
if (!cut) {
|
||||
throw new Error('ordinary rewind unavailable')
|
||||
}
|
||||
await c.journal.replaceEpochItems('handle_forked', 1, cut.kept)
|
||||
expect((await c.journal.reads.item(key))?.turnScope).toEqual({ kind: 'unknown' })
|
||||
expect(
|
||||
await structuredRewindCut(c.journal, {
|
||||
itemId: opener,
|
||||
provider: 'claude',
|
||||
nativeId: SID
|
||||
})
|
||||
).not.toBeNull()
|
||||
expect((await (await c.replay()).reads.item(key))?.turnScope).toEqual({ kind: 'unknown' })
|
||||
}
|
||||
)
|
||||
@@ -279,7 +274,7 @@ describe('Claude chat catch-up', () => {
|
||||
const c = await chat(project, cwd)
|
||||
await c.write([...chatTurn, ...outsideTurn])
|
||||
expect(await c.open('a1')).toBe(true)
|
||||
expect(c.rows()).toContain('assistant:answer 2')
|
||||
expect(await c.rows()).toContain('assistant:answer 2')
|
||||
}
|
||||
)
|
||||
|
||||
@@ -350,7 +345,7 @@ describe('Claude chat catch-up', () => {
|
||||
})
|
||||
expect(await c.journal.reads.item(turn.itemId)).toEqual(turn)
|
||||
expect(await c.journal.reads.item(thinking.itemId)).toEqual(thinking)
|
||||
expect(c.rows().some((row) => row.startsWith('(continued-outside'))).toBe(false)
|
||||
expect((await c.rows()).some((row) => row.startsWith('(continued-outside'))).toBe(false)
|
||||
const completed = await readTestJournalItems(c.journal)
|
||||
expect(await c.open('boundary')).toBe(true)
|
||||
expect(await readTestJournalItems(c.journal)).toEqual(completed)
|
||||
@@ -378,7 +373,7 @@ describe('Claude chat catch-up', () => {
|
||||
return tail
|
||||
})
|
||||
expect(await c.open('a1')).toBe(false)
|
||||
expect(c.rows()).not.toContain('user:ask 2')
|
||||
expect(await c.rows()).not.toContain('user:ask 2')
|
||||
const running = (await readTestJournalItems(c.journal)).findLast(
|
||||
(item) => item.body.kind === 'turn'
|
||||
)
|
||||
@@ -390,11 +385,11 @@ describe('Claude chat catch-up', () => {
|
||||
const c = await chat()
|
||||
await c.live()
|
||||
await c.write(chatTurn)
|
||||
const shown = c.rows()
|
||||
const shown = await c.rows()
|
||||
expect(shown).toEqual(['user:ask 1', 'assistant:answer 1'])
|
||||
|
||||
await c.open('a1')
|
||||
expect(c.rows()).toEqual(shown)
|
||||
expect(await c.rows()).toEqual(shown)
|
||||
|
||||
await c.write(outsideTurn)
|
||||
await c.open('a1')
|
||||
@@ -405,13 +400,13 @@ describe('Claude chat catch-up', () => {
|
||||
'tool:Read:completed',
|
||||
'assistant:answer 2'
|
||||
]
|
||||
expect(c.rows()).toEqual(caughtUp)
|
||||
expect(await c.rows()).toEqual(caughtUp)
|
||||
expect(await readTestJournalSubmissions(c.journal)).toHaveLength(1)
|
||||
|
||||
// Opening again, or from a leaf left before the chat's own streamed turn by a crash, adds nothing.
|
||||
await c.open('a1')
|
||||
await c.open('p1')
|
||||
expect(c.rows()).toEqual(caughtUp)
|
||||
expect(await c.rows()).toEqual(caughtUp)
|
||||
expect(c.warn).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
@@ -420,7 +415,7 @@ describe('Claude chat catch-up', () => {
|
||||
items = 10_000
|
||||
const c = await chat()
|
||||
await c.live()
|
||||
const expected = [...c.rows(), '(continued-outside)']
|
||||
const expected = [...(await c.rows()), '(continued-outside)']
|
||||
const rows: Record<string, unknown>[] = [
|
||||
reply('saved-leaf', 'earlier-prompt', 'msg_saved', { type: 'text', text: 'earlier' }, 0),
|
||||
prompt('p1', 'saved-leaf', 'ask 1', 1),
|
||||
@@ -445,7 +440,7 @@ describe('Claude chat catch-up', () => {
|
||||
const submissions = await readTestJournalSubmissions(c.journal)
|
||||
await c.write(rows)
|
||||
expect(await c.open('saved-leaf')).toBe(true)
|
||||
expect(c.rows()).toEqual(expected)
|
||||
expect(await c.rows()).toEqual(expected)
|
||||
expect(await readTestJournalSubmissions(c.journal)).toEqual(submissions)
|
||||
await c.journal.appendLifecycleBatch({
|
||||
settlementId: 'perf-padding',
|
||||
@@ -553,7 +548,7 @@ describe('Claude chat catch-up', () => {
|
||||
reply('next-answer', 'next', 'msg_next', { type: 'text', text: 'next answer' }, 6)
|
||||
])
|
||||
expect(await c.open('p1')).toBe(true)
|
||||
expect(c.rows()).toEqual([
|
||||
expect(await c.rows()).toEqual([
|
||||
'user:ask 1',
|
||||
'assistant:answer 1',
|
||||
'user:follow up',
|
||||
@@ -577,7 +572,7 @@ describe('Claude chat catch-up', () => {
|
||||
uuid: 'unknown-input'
|
||||
})
|
||||
expect((await c.journal.reads.item(unknown))?.turnScope).toEqual({ kind: 'unknown' })
|
||||
expect(isStructuredRewindTarget(c.journal, unknown)).toBe(false)
|
||||
expect(await isStructuredRewindTarget(c.journal, unknown)).toBe(false)
|
||||
const answer = (await readTestJournalItems(c.journal)).find(
|
||||
(item) =>
|
||||
item.body.kind === 'message' &&
|
||||
@@ -607,7 +602,7 @@ describe('Claude chat catch-up', () => {
|
||||
...bookkeeping
|
||||
])
|
||||
await c.open('a1')
|
||||
expect(c.rows().slice(2)).toEqual([
|
||||
expect((await c.rows()).slice(2)).toEqual([
|
||||
'(continued-outside)',
|
||||
'user:ask long',
|
||||
`assistant:${answer}`
|
||||
@@ -619,7 +614,7 @@ describe('Claude chat catch-up', () => {
|
||||
await c.live()
|
||||
await c.write([...chatTurn, ...outsideTurn])
|
||||
await c.open('missing')
|
||||
expect(c.rows()).toEqual(['user:ask 1', 'assistant:answer 1'])
|
||||
expect(await c.rows()).toEqual(['user:ask 1', 'assistant:answer 1'])
|
||||
expect(c.warn).toHaveBeenCalledWith(
|
||||
'Claude chat did not catch up on turns made outside it',
|
||||
expect.objectContaining({ reason: 'leaf-not-in-tail' })
|
||||
|
||||
Reference in New Issue
Block a user