DIAG: trace account-move events in the kept-card end-to-end test (temporary, will be reverted)

This commit is contained in:
Brennan Benson
2026-11-16 23:58:59 -08:00
parent 578c6a6bd2
commit ca2d4353be
6 changed files with 86 additions and 2 deletions
@@ -1,4 +1,5 @@
import type { SqlAgentSessionJournal } from '../../agent-session-journal/journal-sql-store'
import { traceAccountMove } from '../recovery/structured-agent-session-account-move-trace'
import type { StructuredAgentSessionMutationContext } from '../operations/structured-agent-session-mutation-context'
import type {
StructuredAgentSessionCaller,
@@ -62,6 +63,7 @@ export function wireStructuredAgentSessionQueuedMessages(
return false
}
const waiting = await waitingCards(journal)
traceAccountMove('idle?', waiting)
return waiting.person && !waiting.other
},
captureSelection: async (sessionId, journal) => {
@@ -71,6 +73,7 @@ export function wireStructuredAgentSessionQueuedMessages(
const record = ctx.deps.store.getRecord(sessionId)
// Only a waiting card from the person needs the account a start would use; with none, a
// hold has nothing left to keep.
traceAccountMove('capture', await waitingCards(journal))
const transition = (await waitingCards(journal)).person
? await ctx.accountTransition?.(sessionId)
: (ctx.accountMoveSettled?.(sessionId) ?? null)
@@ -1,4 +1,5 @@
import { afterEach, expect, it, vi } from 'vitest'
import { setAccountMoveTraceSink } from '../../recovery/structured-agent-session-account-move-trace'
import { readTestJournalSnapshot } from '../../../agent-session-journal/journal-observation.test-support'
import type { AgentMessageSource } from '../../../../../shared/agent-session/journal/agent-session-message-source'
import type { AgentChildWorkView } from '../../../../../shared/agent-status/child-work/agent-status-child-work-view'
@@ -349,3 +350,42 @@ it('sends a queued card back to wait when the old account turns out busy as it i
expect(chat.dispatchedTo('queued next')).toEqual([])
expect(chat.child()?.fence).toBe(chat.accountA)
})
it('delivers a kept queued card once on the new account after mail drains and the old work settles', async () => {
const trace: string[] = []
setAccountMoveTraceSink(trace)
try {
const chat = await chatOnAccountA()
const first = rig!.send('first turn')
await first.result
await eventually(() => expect(chat.dispatchedTo('first turn')).toEqual([chat.accountA]))
const queued = rig!.send('queued next', 'queue-if-active')
await queued.result
chat.switchTo('/homes/b')
chat.onNextAccountRead(() => chat.background([devServer()]))
trace.push('test: settle first turn')
await rig!.settleAccepted(first.id, 'first turn')
await eventually(async () =>
expect((await rig!.handoff(queued.id))?.rejection).toMatchObject({ neverReachedAgent: true })
)
trace.push('test: kept')
const mail = rig!.send('mail meanwhile', undefined, { internal: true, from: MAIL })
await mail.result
await eventually(() => expect(chat.mailDispatchedTo()).toEqual([chat.accountA]))
await rig!.settleAccepted(await rig!.handoffId(mail.id), 'mail meanwhile')
trace.push('test: mail settled')
chat.background([devServer({ membership: 'settled', state: 'done', outcome: 'succeeded' })])
trace.push('test: background settled')
await eventually(() => expect(chat.dispatchedTo('queued next')).toHaveLength(1))
expect(chat.dispatchedTo('queued next')).toEqual([chat.child()!.fence])
expect(chat.child()!.fence).not.toBe(chat.accountA)
expect(chat.child()?.accountHome).toEqual({ variable: 'CODEX_HOME', path: '/homes/b' })
} catch (error) {
console.error(`TRACE kept-card\n${trace.join('\n')}`)
throw error
} finally {
setAccountMoveTraceSink(null)
}
})
@@ -1,5 +1,6 @@
// Chats whose person-sent work waits for an account move, and the edges that may end the wait.
import { traceAccountMove } from './structured-agent-session-account-move-trace'
import type {
StructuredAgentSessionAccountMoveEdge,
StructuredAgentSessionHostDeps,
@@ -72,6 +73,7 @@ export class AccountMoveHolds {
stale(sessionId: string, level: 'work' | 'account'): void {
const hold = this.held.get(sessionId)
traceAccountMove('stale', level, 'hold', hold?.transition.waitsFor ?? null, hold?.stale ?? null)
if (hold && hold.stale !== 'account') {
hold.stale = level
}
@@ -89,6 +91,7 @@ export class AccountMoveHolds {
return
}
const wasHeld = this.held.has(sessionId)
traceAccountMove('observe', transition?.waitsFor ?? null, 'wasHeld', wasHeld)
if (!transition || !session?.child) {
if (session) {
delete session.awaitsAccountMove
@@ -0,0 +1,12 @@
// TEMPORARY CI diagnostic: records account-move events only while a test installs a sink.
let sink: string[] | null = null
export function setAccountMoveTraceSink(next: string[] | null): void {
sink = next
}
export function traceAccountMove(...parts: unknown[]): void {
sink?.push(
parts.map((part) => (typeof part === 'string' ? part : JSON.stringify(part))).join(' ')
)
}
@@ -1,4 +1,5 @@
import type { StructuredAgentSessionMutationContext } from '../operations/structured-agent-session-mutation-context'
import { traceAccountMove } from './structured-agent-session-account-move-trace'
import type { StructuredAgentSessionClientDelivery } from '../publication/subscribers/structured-agent-session-client-delivery'
import type { StructuredAgentSessionHost } from '../host/structured-agent-session-host'
import { StructuredAgentSessionUsageLimitContinue } from './structured-agent-session-usage-limit-continue'
@@ -34,6 +35,7 @@ export function childWorkChanged(
id: string
): void {
tasks.publish(id)
traceAccountMove('childWorkChanged', 'open', delivery.childWorkOpen(id))
if (!delivery.childWorkOpen(id)) {
queued.accountMoveEvidence(id)
}
@@ -1,5 +1,6 @@
// Host reads behind outgrown-child retirement and the account move a running child defers.
import { traceAccountMove } from './structured-agent-session-account-move-trace'
import type { AgentJournalSubmission } from '../../../../shared/agent-session/journal/agent-session-journal-types'
import type { AgentSessionAccountHome } from '../../../../shared/agent-session/lifecycle/accounts/agent-session-account-home'
import type {
@@ -119,6 +120,7 @@ export function createStructuredAgentSessionOutgrownChildHost(input: {
next,
deps.logger
)
traceAccountMove('keep', kept)
if (kept === 'failed') {
// Nothing else may wake a send left pending; try again shortly, never in a tight loop.
setTimeout(() => input.wake(sessionId), KEEP_RETRY_MS).unref?.()
@@ -129,10 +131,19 @@ export function createStructuredAgentSessionOutgrownChildHost(input: {
const session = sessions.get(sessionId)
const held = holds.reusable(session, sessionId)
if (held?.fresh) {
traceAccountMove('fresh', held.transition.waitsFor)
return held.transition
}
const reading = await unlessStopping(sessionId)
const read = holds.begin(sessionId)
traceAccountMove(
'begin',
read,
'known',
!!held,
'open',
clientDelivery.childWorkOpen(sessionId)
)
const transition = await readStructuredAgentSessionAccountTransition(reading, {
work: retainedWork(sessionId),
launchAccountHome: options?.launchHome ?? launchAccountHome(sessionId),
@@ -142,7 +153,17 @@ export function createStructuredAgentSessionOutgrownChildHost(input: {
holds.end(sessionId, read)
throw error
})
if (holds.end(sessionId, read) && options?.observe !== false) {
const newest = holds.end(sessionId, read)
traceAccountMove(
'end',
read,
transition?.waitsFor ?? null,
'newest',
newest,
'observe',
options?.observe !== false
)
if (newest && options?.observe !== false) {
holds.observe(sessions.get(sessionId), sessionId, transition)
}
return transition
@@ -151,7 +172,10 @@ export function createStructuredAgentSessionOutgrownChildHost(input: {
accountMoveHeld: (sessionId: string): boolean =>
holds.reusable(sessions.get(sessionId), sessionId)?.fresh === true,
/** Its child work settled: a hold waiting on it is read again. */
accountMoveEvidence: (sessionId: string) => holds.stale(sessionId, 'work'),
accountMoveEvidence: (sessionId: string) => {
traceAccountMove('evidence')
holds.stale(sessionId, 'work')
},
accountMoveSettled: (sessionId: string): null => {
holds.observe(sessions.get(sessionId), sessionId, null)
return null