From bc2fc4ea2bfd106d96fc80bd783fd0dad1fabc19 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Fri, 13 Nov 2026 01:12:31 -0800 Subject: [PATCH] test(agent-session): await the queued-operation results instead of polling a settled flag The operation-order cases polled a settled flag and the compact spy with vi.waitFor's 1 s default. Under SQL worker contention the goal or rewind answers after more than a second, so the poll gave up while the operation was still on its way (CI: "expected false to be true", then "closed by quit" from the unhandled result). Await the result itself and a promise the compact stub resolves. With a 30 ms SQL reply delay injected locally plus CPU load, 3 repeats: the old file failed "releases a startup goal" (expected false to be true) and "answers a goal" (compact called 0 times); the awaited version passed all. --- ...nt-session-running-operation-order.test.ts | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-running-operation-order.test.ts b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-running-operation-order.test.ts index 1035ec17a22..5143102ab0a 100644 --- a/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-running-operation-order.test.ts +++ b/src/main/native-chat/agent-session-wire/host/testing/structured-agent-session-running-operation-order.test.ts @@ -17,8 +17,11 @@ it.each(['goal', 'rewind'] as const)( 'answers a %s while an earlier message is queued behind compact', async (operation) => { const { host, dispatch } = hostTestState() - const compact = vi.fn(async () => ({ state: 'accepted' as const, providerIdentity: null })) - host.deps.adapter.compact = compact + const compacting = Promise.withResolvers() + host.deps.adapter.compact = vi.fn(async () => { + compacting.resolve() + return { state: 'accepted' as const, providerIdentity: null } + }) host.deps.adapter.changeThreadGoal = vi.fn(async () => ({ ok: true as const })) host.deps.adapter.rewindSupport = () => ({ supported: true }) await attach() @@ -26,7 +29,7 @@ it.each(['goal', 'rewind'] as const)( expect( await host.send(CALLER, { envelope: envelope('agentSession.send', { body }), body }) ).toMatchObject({ ok: true }) - await vi.waitFor(() => expect(compact).toHaveBeenCalledOnce()) + await compacting.promise const queued = hostTestMessage('After compact') const queuedEnvelope = envelope('agentSession.send', { body: queued }) expect( @@ -49,11 +52,6 @@ it.each(['goal', 'rewind'] as const)( itemId, expectedEpoch }) - let settled = false - void result.then(() => { - settled = true - }) - await vi.waitFor(() => expect(settled).toBe(true)) expect(await result).toMatchObject( operation === 'goal' ? { ok: true } : { ok: false, refusal: { rewindReason: 'busy' } } ) @@ -89,11 +87,6 @@ it('releases a startup goal when earlier delivery parks behind compact', async ( }) await host.collaboratorsForTests().serialize(SESSION, async () => {}) opened.resolve({ reportedOptions: { model: '' } }) - let settled = false - void result.then(() => { - settled = true - }) - await vi.waitFor(() => expect(settled).toBe(true)) expect(await result).toMatchObject({ ok: true }) expect(dispatch).not.toHaveBeenCalled() })