From ca2d4353be5abcb337cdc9fc2aba30e63e7b05b6 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:47:10 -0800 Subject: [PATCH] DIAG: trace account-move events in the kept-card end-to-end test (temporary, will be reverted) --- .../structured-agent-session-queued-wiring.ts | 3 ++ ...nt-session-account-transition-hold.test.ts | 40 +++++++++++++++++++ ...ctured-agent-session-account-move-holds.ts | 3 ++ ...ctured-agent-session-account-move-trace.ts | 12 ++++++ ...tructured-agent-session-host-background.ts | 2 + ...tured-agent-session-outgrown-child-host.ts | 28 ++++++++++++- 6 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 src/main/native-chat/agent-session-wire/recovery/structured-agent-session-account-move-trace.ts diff --git a/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-queued-wiring.ts b/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-queued-wiring.ts index 7b279515b33..2013a3613c2 100644 --- a/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-queued-wiring.ts +++ b/src/main/native-chat/agent-session-wire/delivery/structured-agent-session-queued-wiring.ts @@ -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) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-account-transition-hold.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-account-transition-hold.test.ts index 4f90689fdca..aa52420f067 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-account-transition-hold.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-account-transition-hold.test.ts @@ -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) + } +}) diff --git a/src/main/native-chat/agent-session-wire/recovery/structured-agent-session-account-move-holds.ts b/src/main/native-chat/agent-session-wire/recovery/structured-agent-session-account-move-holds.ts index 3d4620d6e4e..66f214d9609 100644 --- a/src/main/native-chat/agent-session-wire/recovery/structured-agent-session-account-move-holds.ts +++ b/src/main/native-chat/agent-session-wire/recovery/structured-agent-session-account-move-holds.ts @@ -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 diff --git a/src/main/native-chat/agent-session-wire/recovery/structured-agent-session-account-move-trace.ts b/src/main/native-chat/agent-session-wire/recovery/structured-agent-session-account-move-trace.ts new file mode 100644 index 00000000000..7a686061fbf --- /dev/null +++ b/src/main/native-chat/agent-session-wire/recovery/structured-agent-session-account-move-trace.ts @@ -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(' ') + ) +} diff --git a/src/main/native-chat/agent-session-wire/recovery/structured-agent-session-host-background.ts b/src/main/native-chat/agent-session-wire/recovery/structured-agent-session-host-background.ts index 8fe286d3059..84925e8063a 100644 --- a/src/main/native-chat/agent-session-wire/recovery/structured-agent-session-host-background.ts +++ b/src/main/native-chat/agent-session-wire/recovery/structured-agent-session-host-background.ts @@ -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) } diff --git a/src/main/native-chat/agent-session-wire/recovery/structured-agent-session-outgrown-child-host.ts b/src/main/native-chat/agent-session-wire/recovery/structured-agent-session-outgrown-child-host.ts index f7186218a01..634987a27da 100644 --- a/src/main/native-chat/agent-session-wire/recovery/structured-agent-session-outgrown-child-host.ts +++ b/src/main/native-chat/agent-session-wire/recovery/structured-agent-session-outgrown-child-host.ts @@ -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