Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Wait on what the host does, not on the clock, in the session-host tests (#1039)

Abel Dantasd64ccad3c877
diff --git a/console/packages/agent-providers/src/host/session-host.test.ts b/console/packages/agent-providers/src/host/session-host.test.ts
index b9c0ef77c..0ab43b89a 100644
--- a/console/packages/agent-providers/src/host/session-host.test.ts
+++ b/console/packages/agent-providers/src/host/session-host.test.ts
@@ -6,25 +6,34 @@ import type { Command, Session } from '@switch-console/shared/session-v1';
import { afterEach, expect, it, vi } from 'vitest';
import { ProviderConversationUnavailableError, type ProviderAdapter } from '../adapter';
import type { ProviderRuntimeEvent } from '../events';
+import { abandonWaits, announcing, changed, until } from '../testing/until';
import { CONNECTOR_APPROVAL_NOTICE, ServiceNotices } from './service-notices';
import { HostedSession, type RoomReply, unpostedReply } from './session-host';
-/**
- * `vi.waitFor` with room for a slow disk: every step here is a journal write
- * flushed to disk, and Windows runners take well over the default second.
- */
-function waitFor<T>(check: () => T | Promise<T>): Promise<T> {
- return vi.waitFor(check, { timeout: 10_000 });
-}
-// A test makes several of those waits, so it gets longer than any one of them:
-// a slow machine then fails on what it waited for, not on the clock.
+// Every step here is a journal write flushed to disk, and Windows runners take
+// many times longer over that. The waits have no deadline of their own (see
+// `until`), so this is the one clock: long enough for a slow disk, short
+// enough that a host that never gets there still fails.
vi.setConfig({ testTimeout: 30_000 });
+/** A host that tells `until` each time it calls its provider or publishes an event. */
+async function startHost(
+ ...[root, config, adapter]: Parameters<typeof HostedSession.start>
+): Promise<HostedSession> {
+ const host = await HostedSession.start(root, config, announcing(adapter));
+ host.onPublished(changed);
+ return host;
+}
+
const roots: string[] = [];
const hosts: HostedSession[] = [];
afterEach(async () => {
- for (const host of hosts.splice(0)) await host.shutdown();
- for (const root of roots.splice(0)) await rm(root, { recursive: true, force: true });
+ try {
+ for (const host of hosts.splice(0)) await host.shutdown();
+ for (const root of roots.splice(0)) await rm(root, { recursive: true, force: true });
+ } finally {
+ abandonWaits();
+ }
});
function setup(provider: Session['provider']) {
let listener: (event: ProviderRuntimeEvent) => void = () => {};
@@ -122,9 +131,9 @@ async function start(provider: Session['provider']) {
const root = await mkdtemp(join(tmpdir(), 'sdk-host-test-'));
roots.push(root);
const fixture = setup(provider);
- const host = await HostedSession.start(root, fixture.config, fixture.adapter);
+ const host = await startHost(root, fixture.config, fixture.adapter);
hosts.push(host);
- await waitFor(() => expect(host.snapshot().session.status).toBe('ready'));
+ await until(() => expect(host.snapshot().session.status).toBe('ready'));
return { ...fixture, root, host };
}
it.each(['claude', 'codex', 'opencode', 'antigravity', 'cursor'] as const)(
@@ -135,13 +144,13 @@ it.each(['claude', 'codex', 'opencode', 'antigravity', 'cursor'] as const)(
expect((await host.command(command)).status).toBe('applied');
await host.command(structuredClone(command));
await host.command(message('second'));
- await waitFor(() => expect(adapter.sendTurn).toHaveBeenCalledTimes(1));
+ await until(() => expect(adapter.sendTurn).toHaveBeenCalledTimes(1));
await expect(
host.command({ ...command, body: { ...command.body, text: 'Changed' } } as Command)
).rejects.toThrow('IDEMPOTENCY_CONFLICT');
await host.shutdown();
const recovered = setup(provider);
- const next = await HostedSession.start(root, recovered.config, recovered.adapter);
+ const next = await startHost(root, recovered.config, recovered.adapter);
hosts.push(next);
expect(recovered.adapter.startSession).toHaveBeenCalledWith(
expect.objectContaining({ resume: { nativeSessionId: 'native' } })
@@ -156,10 +165,10 @@ it.each(['claude', 'codex', 'opencode', 'antigravity', 'cursor'] as const)(
it('keeps an explicitly stopped session stopped across restart', async () => {
const { root, host } = await start('claude');
await host.command({ ...message('stop'), body: { type: 'session.stop' } });
- await waitFor(() => expect(host.snapshot().session.status).toBe('stopped'));
+ await until(() => expect(host.snapshot().session.status).toBe('stopped'));
await host.shutdown();
const fixture = setup('claude');
- const recovered = await HostedSession.start(root, fixture.config, fixture.adapter);
+ const recovered = await startHost(root, fixture.config, fixture.adapter);
hosts.push(recovered);
expect(recovered.snapshot().session.status).toBe('stopped');
expect(fixture.adapter.startSession).not.toHaveBeenCalled();
@@ -175,7 +184,7 @@ it('keeps a failed callback outcome unknown and never retries the answer', async
title: 'Write file',
options: [{ decision: 'accept', label: 'Allow once' }],
});
- await waitFor(() => expect(host.snapshot().requests[0]?.state).toBe('open'));
+ await until(() => expect(host.snapshot().requests[0]?.state).toBe('open'));
vi.mocked(adapter.respondToRequest).mockRejectedValueOnce(new Error('Provider exited'));
const answer: Command = {
...message('answer'),
@@ -214,9 +223,9 @@ it('settles unanswered questions when the provider exits', async () => {
},
],
});
- await waitFor(() => expect(host.snapshot().requests[0]?.state).toBe('open'));
+ await until(() => expect(host.snapshot().requests[0]?.state).toBe('open'));
emit({ type: 'session.exited', reason: 'Disconnected' });
- await waitFor(() => expect(host.snapshot().requests[0]?.state).toBe('closed'));
+ await until(() => expect(host.snapshot().requests[0]?.state).toBe('closed'));
expect(host.snapshot().session.pendingRequestIds).toEqual([]);
});
it('rejects oversized input before persisting or dispatching a turn', async () => {
@@ -243,7 +252,7 @@ it.each(['accept', 'decline', 'cancel'] as const)(
title: 'Write file',
options: [{ decision, label: decision }],
});
- await waitFor(() => expect(host.snapshot().requests[0]?.state).toBe('open'));
+ await until(() => expect(host.snapshot().requests[0]?.state).toBe('open'));
const answer: Command = {
...message('answer'),
body: {
@@ -293,7 +302,7 @@ it('rejects host publication claims before dispatching a command', async () => {
it('records uncertain interrupt delivery and never sends it twice', async () => {
const { host, adapter } = await start('codex');
await host.command(message('turn'));
- await waitFor(() => expect(adapter.sendTurn).toHaveBeenCalledOnce());
+ await until(() => expect(adapter.sendTurn).toHaveBeenCalledOnce());
vi.mocked(adapter.interruptTurn).mockRejectedValue(new Error('Connection lost after send'));
const interrupt: Command = {
...message('interrupt'),
@@ -327,7 +336,7 @@ it('keeps a stop with uncertain cleanup stopped on recovery', async () => {
expect(adapter.stopSession).toHaveBeenCalledOnce();
await host.shutdown();
const recovered = setup('codex');
- const resumed = await HostedSession.start(root, recovered.config, recovered.adapter);
+ const resumed = await startHost(root, recovered.config, recovered.adapter);
hosts.push(resumed);
expect(resumed.snapshot().session.status).toBe('stopped');
expect(recovered.adapter.startSession).not.toHaveBeenCalled();
@@ -337,7 +346,7 @@ it('stops active and queued turns without dispatching the queue during cleanup',
const { host, adapter, emit } = await start('codex');
await host.command(message('active'));
await host.command(message('queued'));
- await waitFor(() => expect(adapter.sendTurn).toHaveBeenCalledOnce());
+ await until(() => expect(adapter.sendTurn).toHaveBeenCalledOnce());
vi.mocked(adapter.stopSession).mockImplementationOnce(async () => {
emit({ type: 'turn.completed', turnId: 'active', outcome: 'interrupted', usage: [] });
emit({ type: 'session.exited', reason: 'Stopped' });
@@ -345,7 +354,7 @@ it('stops active and queued turns without dispatching the queue during cleanup',
expect((await host.command({ ...message('stop'), body: { type: 'session.stop' } })).status).toBe(
'applied'
);
- await waitFor(() => expect(host.snapshot().session.status).toBe('stopped'));
+ await until(() => expect(host.snapshot().session.status).toBe('stopped'));
expect(host.snapshot().turns.map((turn) => turn.status)).toEqual(['interrupted', 'interrupted']);
expect(adapter.sendTurn).toHaveBeenCalledOnce();
});
@@ -356,17 +365,18 @@ it('knows what a turn spent from the moment it reads as ended, and after a resta
{ model: 'big', inputTokens: 10, outputTokens: 2, cacheReadTokens: 300, cacheWriteTokens: 0 },
];
await host.command(message('spender'));
- await waitFor(() => expect(host.snapshot().turns[0]?.status).toBe('running'));
- const seenWhenEnded: unknown[] = [];
- host.onPublished((event) => {
- if (event.body.type === 'turn.upsert' && event.body.status === 'completed')
- seenWhenEnded.push(host.usageOf(event.body.turnId));
- });
+ await until(() => expect(host.snapshot().turns[0]?.status).toBe('running'));
+ const seenWhenEnded = new Promise((resolve) =>
+ host.onPublished((event) => {
+ if (event.body.type === 'turn.upsert' && event.body.status === 'completed')
+ resolve(host.usageOf(event.body.turnId));
+ })
+ );
emit({ type: 'turn.completed', turnId: 'spender', outcome: 'completed', usage: spent });
- await waitFor(() => expect(seenWhenEnded).toEqual([spent]));
+ expect(await seenWhenEnded).toEqual(spent);
await host.shutdown();
const next = setup('claude');
- const recovered = await HostedSession.start(root, next.config, next.adapter);
+ const recovered = await startHost(root, next.config, next.adapter);
hosts.push(recovered);
expect(recovered.usageOf('spender')).toEqual(spent);
expect(recovered.usageOf('never-ran')).toEqual([]);
@@ -375,15 +385,15 @@ it('knows what a turn spent from the moment it reads as ended, and after a resta
it('resets into a new epoch and native conversation while retaining history', async () => {
const { host, adapter, emit, root } = await start('claude');
await host.command(message('old-turn'));
- await waitFor(() => expect(host.snapshot().turns[0]?.status).toBe('running'));
+ await until(() => expect(host.snapshot().turns[0]?.status).toBe('running'));
// Completed only after the provider reported the turn started, or its late
// start would put the turn back to running.
- await waitFor(() => expect(adapter.sendTurn).toHaveBeenCalledOnce());
+ await until(() => expect(adapter.sendTurn).toHaveBeenCalledOnce());
emit({ type: 'turn.completed', turnId: 'old-turn', outcome: 'completed', usage: [] });
- await waitFor(() => expect(host.snapshot().turns[0]?.status).toBe('completed'));
+ await until(() => expect(host.snapshot().turns[0]?.status).toBe('completed'));
// The turn reads completed a moment before the session reads ready again,
// and a reset is refused until it does.
- await waitFor(() => expect(host.snapshot().session.status).toBe('ready'));
+ await until(() => expect(host.snapshot().session.status).toBe('ready'));
vi.mocked(adapter.startSession).mockImplementationOnce(async () => {
emit({ type: 'session.state.changed', status: 'ready' });
return { provider: 'claude', sessionId: 'session', nativeSessionId: 'fresh-native' };
@@ -398,7 +408,7 @@ it('resets into a new epoch and native conversation while retaining history', as
await expect(host.command(message('stale'))).rejects.toThrow('STALE_EPOCH');
await host.shutdown();
const next = setup('claude');
- hosts.push(await HostedSession.start(root, next.config, next.adapter));
+ hosts.push(await startHost(root, next.config, next.adapter));
expect(next.adapter.startSession).toHaveBeenCalledWith(
expect.objectContaining({
resume: { nativeSessionId: 'fresh-native' },
@@ -418,7 +428,7 @@ it('rejects reset with queued work or a pending native request', async () => {
title: 'Run command',
options: [{ decision: 'accept', label: 'Allow' }],
});
- await waitFor(() => expect(host.snapshot().requests).toHaveLength(1));
+ await until(() => expect(host.snapshot().requests).toHaveLength(1));
await expect(
host.command({ ...message('reset'), body: { type: 'session.reset' } })
).rejects.toThrow('SESSION_BUSY');
@@ -441,7 +451,7 @@ async function undecided(nativeRecorded: boolean) {
);
const next = setup('claude');
const config = { ...next.config, stageAttachments: async () => [] };
- const resumed = await HostedSession.start(root, config, next.adapter);
+ const resumed = await startHost(root, config, next.adapter);
hosts.push(resumed);
return { ...next, root, host: resumed };
}
@@ -499,7 +509,7 @@ it('starts a fresh conversation when the user decides an uncertain reset', async
expect(adapter.stopSession).not.toHaveBeenCalled();
expect(adapter.startSession).toHaveBeenCalledOnce();
expect(vi.mocked(adapter.startSession).mock.calls[0][0].resume).toBeUndefined();
- await waitFor(() => expect(host.snapshot().session.status).toBe('ready'));
+ await until(() => expect(host.snapshot().session.status).toBe('ready'));
expect(host.resetDecisionPending).toBe(false);
expect(host.snapshot().session.capabilities.attachmentMimeTypes.length).toBeGreaterThan(0);
expect(host.status('interrupted-reset')).toMatchObject({
@@ -524,7 +534,7 @@ it('stops an undecided session without touching an unstarted provider', async ()
expect(host.snapshot().session.status).toBe('stopped');
await host.shutdown();
const next = setup('claude');
- const resumed = await HostedSession.start(root, next.config, next.adapter);
+ const resumed = await startHost(root, next.config, next.adapter);
hosts.push(resumed);
expect(resumed.snapshot().session.status).toBe('stopped');
expect(next.adapter.startSession).not.toHaveBeenCalled();
@@ -534,13 +544,13 @@ it('resumes an ordinary interrupted session without a reset decision', async ()
const { host, root } = await start('claude');
await host.shutdown();
const next = setup('claude');
- const resumed = await HostedSession.start(root, next.config, next.adapter);
+ const resumed = await startHost(root, next.config, next.adapter);
hosts.push(resumed);
expect(next.adapter.startSession).toHaveBeenCalledWith(
expect.objectContaining({ resume: { nativeSessionId: 'native' } })
);
expect(resumed.resetDecisionPending).toBe(false);
- await waitFor(() => expect(resumed.snapshot().session.status).toBe('ready'));
+ await until(() => expect(resumed.snapshot().session.status).toBe('ready'));
});
it('starts a new conversation on its own when the saved one is gone and never answered', async () => {
@@ -550,12 +560,12 @@ it('starts a new conversation on its own when the saved one is gone and never an
vi.mocked(next.adapter.startSession).mockRejectedValueOnce(
new ProviderConversationUnavailableError('claude', 'session', 'No saved conversation')
);
- const restarted = await HostedSession.start(root, next.config, next.adapter);
+ const restarted = await startHost(root, next.config, next.adapter);
hosts.push(restarted);
expect(restarted.resetDecisionPending).toBe(false);
expect(next.adapter.startSession).toHaveBeenCalledTimes(2);
expect(vi.mocked(next.adapter.startSession).mock.calls[1][0].resume).toBeUndefined();
- await waitFor(() =>
+ await until(() =>
expect(restarted.replay(0).events.map((e) => e.body)).toContainEqual(
expect.objectContaining({ code: 'CONVERSATION_STARTED_FRESH' })
)
@@ -570,9 +580,9 @@ it('validates native model choices and persists a confirmed choice across restar
{ id: 'model-a', label: 'Model A', options: { effort: ['low', 'high'] } },
]);
fixture.adapter.setModel = vi.fn(async () => {});
- const host = await HostedSession.start(root, fixture.config, fixture.adapter);
+ const host = await startHost(root, fixture.config, fixture.adapter);
hosts.push(host);
- await waitFor(() => expect(host.snapshot().session.models?.length).toBeGreaterThan(0));
+ await until(() => expect(host.snapshot().session.models?.length).toBeGreaterThan(0));
expect(host.snapshot().session.capabilities.modelChange).toBe(true);
const command: Command = {
...message('model'),
@@ -598,7 +608,7 @@ it('validates native model choices and persists a confirmed choice across restar
expect(host.snapshot().session.model).toEqual({ id: 'model-a', options: { effort: 'high' } });
await host.shutdown();
const recovered = setup('claude');
- hosts.push(await HostedSession.start(root, recovered.config, recovered.adapter));
+ hosts.push(await startHost(root, recovered.config, recovered.adapter));
expect(recovered.adapter.startSession).toHaveBeenCalledWith(
expect.objectContaining({
model: { id: 'model-a', options: { effort: 'high' } },
@@ -618,14 +628,14 @@ it('lists a catalog too large for a session event without its overflow, and keep
}));
fixture.adapter.listModels = vi.fn(async () => catalog);
fixture.adapter.setModel = vi.fn(async () => {});
- const host = await HostedSession.start(root, fixture.config, fixture.adapter);
+ const host = await startHost(root, fixture.config, fixture.adapter);
hosts.push(host);
- await waitFor(() => expect(host.snapshot().session.models?.length).toBeGreaterThan(0));
+ await until(() => expect(host.snapshot().session.models?.length).toBeGreaterThan(0));
const listed = host.snapshot().session.models!;
expect(listed.length).toBeLessThan(catalog.length);
expect(listed.every((model) => Object.keys(model.options).length === 0)).toBe(true);
// The notice follows the session record it explains.
- await vi.waitFor(() =>
+ await until(() =>
expect(host.replay(0).events.map((e) => e.body)).toContainEqual(
expect.objectContaining({ code: 'MODEL_CATALOG_TRUNCATED' })
)
@@ -646,9 +656,9 @@ it('leaves a lost model acknowledgement unknown and does not retry it', async ()
fixture.adapter.setModel = vi.fn(async () => {
throw new Error('Acknowledgement lost');
});
- const host = await HostedSession.start(root, fixture.config, fixture.adapter);
+ const host = await startHost(root, fixture.config, fixture.adapter);
hosts.push(host);
- await waitFor(() => expect(host.snapshot().session.models?.length).toBeGreaterThan(0));
+ await until(() => expect(host.snapshot().session.models?.length).toBeGreaterThan(0));
const command: Command = {
...message('model'),
body: { type: 'session.model.set', modelId: 'model-a', options: {} },
@@ -670,12 +680,12 @@ it('waits for native compaction and keeps history without sending a summarizatio
finish = resolve;
})
);
- const host = await HostedSession.start(root, fixture.config, fixture.adapter);
+ const host = await startHost(root, fixture.config, fixture.adapter);
hosts.push(host);
- await waitFor(() => expect(host.snapshot().session.capabilities.compact).toBe(true));
+ await until(() => expect(host.snapshot().session.capabilities.compact).toBe(true));
const command: Command = { ...message('compact'), body: { type: 'session.compact' } };
const applied = host.command(command);
- await waitFor(() => expect(fixture.adapter.compactSession).toHaveBeenCalledTimes(1));
+ await until(() => expect(fixture.adapter.compactSession).toHaveBeenCalledTimes(1));
expect(host.status('compact').status).toBe('accepted');
expect(host.snapshot().session.status).toBe('running');
expect(
@@ -700,21 +710,21 @@ it('publishes context usage when a turn ends and clears it on reset', async () =
roots.push(root);
const fixture = setup('claude');
fixture.adapter.contextUsage = vi.fn(async () => ({ usedTokens: 134000, windowTokens: 0 }));
- const host = await HostedSession.start(root, fixture.config, fixture.adapter);
+ const host = await startHost(root, fixture.config, fixture.adapter);
hosts.push(host);
- await waitFor(() => expect(host.snapshot().session.status).toBe('ready'));
+ await until(() => expect(host.snapshot().session.status).toBe('ready'));
expect(host.snapshot().session.context).toBeUndefined();
await host.command(message('turn'));
- await waitFor(() => expect(fixture.adapter.sendTurn).toHaveBeenCalledTimes(1));
+ await until(() => expect(fixture.adapter.sendTurn).toHaveBeenCalledTimes(1));
fixture.emit({ type: 'turn.completed', turnId: 'turn', outcome: 'completed', usage: [] });
- await waitFor(() =>
+ await until(() =>
expect(host.snapshot().session.context).toMatchObject({
usedTokens: 134000,
windowTokens: null,
})
);
await host.command({ ...message('reset'), body: { type: 'session.reset' } });
- await waitFor(() => expect(host.snapshot().session.status).toBe('ready'));
+ await until(() => expect(host.snapshot().session.status).toBe('ready'));
expect(host.snapshot().session.context).toBeNull();
});
@@ -728,8 +738,8 @@ it('stages attachments before dispatch and never executes after shutdown during
const root = await mkdtemp(join(tmpdir(), 'sdk-stage-stop-'));
roots.push(root);
const stageAttachments = vi.fn(() => transfer.promise);
- const host = await HostedSession.start(root, { ...config, stageAttachments }, adapter);
- await waitFor(() =>
+ const host = await startHost(root, { ...config, stageAttachments }, adapter);
+ await until(() =>
expect(host.snapshot().session.capabilities.attachmentMimeTypes).toContain('text/plain')
);
const command = message('with-file');
@@ -754,8 +764,8 @@ it('delivers staged execution paths once and keeps durable attachment metadata',
roots.push(root);
const staged = [{ path: '/execution-host/note.txt', mimeType: 'text/plain' }];
const stageAttachments = vi.fn(async () => staged);
- const host = await HostedSession.start(root, { ...config, stageAttachments }, adapter);
- await waitFor(() =>
+ const host = await startHost(root, { ...config, stageAttachments }, adapter);
+ await until(() =>
expect(host.snapshot().session.capabilities.attachmentMimeTypes).toContain('text/plain')
);
hosts.push(host);
@@ -783,7 +793,7 @@ it('updates attachment support after a confirmed native model change', async ()
adapter.setModel = vi.fn(async () => {});
const root = await mkdtemp(join(tmpdir(), 'sdk-model-images-'));
roots.push(root);
- const host = await HostedSession.start(
+ const host = await startHost(
root,
{
...config,
@@ -793,7 +803,7 @@ it('updates attachment support after a confirmed native model change', async ()
adapter
);
hosts.push(host);
- await waitFor(() => expect(host.snapshot().session.capabilities.modelChange).toBe(true));
+ await until(() => expect(host.snapshot().session.capabilities.modelChange).toBe(true));
expect(host.snapshot().session.capabilities.attachmentMimeTypes).not.toContain('image/png');
const image = message('image');
if (image.body.type !== 'message.send') throw new Error('Expected message');
@@ -812,7 +822,7 @@ it('updates attachment support after a confirmed native model change', async ()
it('rejects unsupported native compaction without faulting the session', async () => {
const root = await mkdtemp(join(tmpdir(), 'unsupported-compact-'));
const { adapter, config } = setup('claude');
- const host = await HostedSession.start(root, config, adapter);
+ const host = await startHost(root, config, adapter);
try {
await expect(
host.command({ ...message('unsupported-compact'), body: { type: 'session.compact' } })
@@ -828,19 +838,19 @@ it('requires an explicit fresh reset when the provider cannot resume the saved c
const { host, adapter, emit, root } = await start('codex');
// The provider has answered, so the lost conversation held something.
await host.command(message('answered'));
- await waitFor(() => expect(host.snapshot().turns[0]?.status).toBe('running'));
+ await until(() => expect(host.snapshot().turns[0]?.status).toBe('running'));
// Completed only after the provider reported the turn started, or its late
// start would put the turn back to running.
- await waitFor(() => expect(adapter.sendTurn).toHaveBeenCalledOnce());
+ await until(() => expect(adapter.sendTurn).toHaveBeenCalledOnce());
emit({ type: 'turn.completed', turnId: 'answered', outcome: 'completed', usage: [] });
- await waitFor(() => expect(host.snapshot().turns[0]?.status).toBe('completed'));
- await waitFor(() => expect(host.snapshot().session.status).toBe('ready'));
+ await until(() => expect(host.snapshot().turns[0]?.status).toBe('completed'));
+ await until(() => expect(host.snapshot().session.status).toBe('ready'));
await host.shutdown();
const next = setup('codex');
vi.mocked(next.adapter.startSession).mockRejectedValueOnce(
new ProviderConversationUnavailableError('codex', 'session', 'Saved conversation unavailable')
);
- const recovered = await HostedSession.start(root, next.config, next.adapter);
+ const recovered = await startHost(root, next.config, next.adapter);
hosts.push(recovered);
expect(recovered.resetDecisionPending).toBe(true);
expect(recovered.snapshot().session.status).toBe('error');
@@ -870,9 +880,7 @@ it('does not present authentication or transport failures as a missing conversat
await host.shutdown();
const next = setup('codex');
vi.mocked(next.adapter.startSession).mockRejectedValueOnce(new Error('Authentication failed'));
- await expect(HostedSession.start(root, next.config, next.adapter)).rejects.toThrow(
- 'Authentication failed'
- );
+ await expect(startHost(root, next.config, next.adapter)).rejects.toThrow('Authentication failed');
});
it('resumes a stopped native conversation only once per explicit operation without replaying work', async () => {
@@ -882,11 +890,7 @@ it('resumes a stopped native conversation only once per explicit operation witho
await host.shutdown();
const fixture = setup('claude');
const resumeOperationId = randomUUID();
- const resumed = await HostedSession.start(
- root,
- { ...fixture.config, resumeOperationId },
- fixture.adapter
- );
+ const resumed = await startHost(root, { ...fixture.config, resumeOperationId }, fixture.adapter);
hosts.push(resumed);
expect(fixture.adapter.startSession).toHaveBeenCalledWith(
expect.objectContaining({ resume: { nativeSessionId: 'native' } })
@@ -901,11 +905,7 @@ it('resumes a stopped native conversation only once per explicit operation witho
});
await resumed.shutdown();
const again = setup('claude');
- const stopped = await HostedSession.start(
- root,
- { ...again.config, resumeOperationId },
- again.adapter
- );
+ const stopped = await startHost(root, { ...again.config, resumeOperationId }, again.adapter);
hosts.push(stopped);
expect(stopped.snapshot().session.status).toBe('stopped');
expect(again.adapter.startSession).not.toHaveBeenCalled();
@@ -924,7 +924,7 @@ it.each(['claude', 'codex', 'opencode', 'antigravity', 'cursor'] as const)(
resolveModels = resolve;
})
);
- const host = await HostedSession.start(root, fixture.config, fixture.adapter);
+ const host = await startHost(root, fixture.config, fixture.adapter);
hosts.push(host);
expect(host.snapshot().session.status).toBe('ready');
expect(fixture.adapter.listModels).toHaveBeenCalledOnce();
@@ -945,16 +945,14 @@ it('overlaps authentication with startup but waits for authentication before ret
})
);
let returned = false;
- const starting = HostedSession.start(
- root,
- { ...fixture.config, authenticate },
- fixture.adapter
- ).then((host) => {
- returned = true;
- hosts.push(host);
- return host;
- });
- await waitFor(() => expect(fixture.adapter.startSession).toHaveBeenCalledOnce());
+ const starting = startHost(root, { ...fixture.config, authenticate }, fixture.adapter).then(
+ (host) => {
+ returned = true;
+ hosts.push(host);
+ return host;
+ }
+ );
+ await until(() => expect(fixture.adapter.startSession).toHaveBeenCalledOnce());
expect(authenticate).toHaveBeenCalledOnce();
expect(returned).toBe(false);
authenticated();
@@ -966,7 +964,7 @@ it('cleans up the started provider when authentication fails', async () => {
roots.push(root);
const fixture = setup('claude');
await expect(
- HostedSession.start(
+ startHost(
root,
{
...fixture.config,
@@ -988,9 +986,9 @@ it('reports model discovery failures without losing the usable session', async (
fixture.adapter.listModels = vi.fn(async () => {
throw new Error('Model service unavailable');
});
- const host = await HostedSession.start(root, fixture.config, fixture.adapter);
+ const host = await startHost(root, fixture.config, fixture.adapter);
hosts.push(host);
- await waitFor(() =>
+ await until(() =>
expect(
host
.replay(0)
@@ -1057,7 +1055,7 @@ async function openApproval(
{ decision: 'decline', label: 'Deny' },
],
});
- await waitFor(() => expect(host.snapshot().requests[0]?.state).toBe('open'));
+ await until(() => expect(host.snapshot().requests[0]?.state).toBe('open'));
}
it('applies an answer Switch recorded for an approval, once', async () => {
@@ -1169,7 +1167,7 @@ async function openQuestions(
},
],
});
- await waitFor(() => expect(host.snapshot().requests[0]?.state).toBe('open'));
+ await until(() => expect(host.snapshot().requests[0]?.state).toBe('open'));
}
it('answers questions with what Switch recorded for them', async () => {
@@ -1266,16 +1264,14 @@ it('starts a conversation on recovery when the first start never opened one', as
roots.push(root);
const failing = setup('cursor');
vi.mocked(failing.adapter.startSession).mockRejectedValueOnce(new Error('Invalid params'));
- await expect(HostedSession.start(root, failing.config, failing.adapter)).rejects.toThrow(
- 'Invalid params'
- );
+ await expect(startHost(root, failing.config, failing.adapter)).rejects.toThrow('Invalid params');
const recovered = setup('cursor');
- const host = await HostedSession.start(root, recovered.config, recovered.adapter);
+ const host = await startHost(root, recovered.config, recovered.adapter);
hosts.push(host);
expect(recovered.adapter.startSession).toHaveBeenCalledWith(
expect.not.objectContaining({ resume: expect.anything() })
);
- await waitFor(() => expect(host.snapshot().session.status).toBe('ready'));
+ await until(() => expect(host.snapshot().session.status).toBe('ready'));
});
function roomMessage(commandId: string, threadId: string | null): Command {
@@ -1299,13 +1295,9 @@ async function startReplying(
const dir = root ?? (await mkdtemp(join(tmpdir(), 'sdk-host-test-')));
if (!root) roots.push(dir);
const fixture = setup('cursor');
- const host = await HostedSession.start(
- dir,
- { ...fixture.config, replyToRoom: reply },
- fixture.adapter
- );
+ const host = await startHost(dir, { ...fixture.config, replyToRoom: reply }, fixture.adapter);
hosts.push(host);
- await waitFor(() => expect(host.snapshot().session.status).toBe('ready'));
+ await until(() => expect(host.snapshot().session.status).toBe('ready'));
return { ...fixture, root: dir, host };
}
@@ -1334,10 +1326,10 @@ it('posts nothing for a room turn that finished without posting', async () => {
const reply = vi.fn(async (_reply: RoomReply) => {});
const { host, emit, root, adapter } = await startReplying(reply);
await host.command(roomMessage('turn', 'thread'));
- await waitFor(() => expect(adapter.sendTurn).toHaveBeenCalledOnce());
+ await until(() => expect(adapter.sendTurn).toHaveBeenCalledOnce());
said(emit, 'turn', 'last', 'Not answering this one.');
emit({ type: 'turn.completed', turnId: 'turn', outcome: 'completed', usage: [] });
- await waitFor(() => expect(host.snapshot().turns[0]?.status).toBe('completed'));
+ await until(() => expect(host.snapshot().turns[0]?.status).toBe('completed'));
await host.barrier();
expect(reply).not.toHaveBeenCalled();
expect(await roomReplyRecords(root)).toEqual([]);
@@ -1347,7 +1339,7 @@ it('tells the room once, in its thread, that a turn failed without posting', asy
const reply = vi.fn(async (_reply: RoomReply) => {});
const { host, emit, root, adapter } = await startReplying(reply);
await host.command(roomMessage('turn', 'thread'));
- await waitFor(() => expect(adapter.sendTurn).toHaveBeenCalledOnce());
+ await until(() => expect(adapter.sendTurn).toHaveBeenCalledOnce());
emit({
type: 'turn.completed',
turnId: 'turn',
@@ -1355,7 +1347,7 @@ it('tells the room once, in its thread, that a turn failed without posting', asy
message: 'Upgrade your plan to continue',
usage: [],
});
- await waitFor(() =>
+ await until(() =>
expect(host.snapshot().notices.map((notice) => notice.code)).toContain('ROOM_REPLY_POSTED')
);
expect(reply).toHaveBeenCalledExactlyOnceWith({
@@ -1377,14 +1369,14 @@ it('leaves a room turn alone when the agent posted to the room during it', async
const reply = vi.fn(async (_reply: RoomReply) => {});
const { host, emit, adapter } = await startReplying(reply);
await host.command(roomMessage('posted', null));
- await waitFor(() => expect(adapter.sendTurn).toHaveBeenCalledOnce());
+ await until(() => expect(adapter.sendTurn).toHaveBeenCalledOnce());
host.agentPostedToRoom();
said(emit, 'posted', 'done', 'Posted the answer.');
emit({ type: 'turn.completed', turnId: 'posted', outcome: 'completed', usage: [] });
- await waitFor(() => expect(host.snapshot().turns[0]?.status).toBe('completed'));
+ await until(() => expect(host.snapshot().turns[0]?.status).toBe('completed'));
// A post counts for the turn it was made in, not the next one.
await host.command(roomMessage('silent', null));
- await waitFor(() => expect(adapter.sendTurn).toHaveBeenCalledTimes(2));
+ await until(() => expect(adapter.sendTurn).toHaveBeenCalledTimes(2));
emit({
type: 'turn.completed',
turnId: 'silent',
@@ -1392,7 +1384,7 @@ it('leaves a room turn alone when the agent posted to the room during it', async
message: 'boom',
usage: [],
});
- await waitFor(() => expect(reply).toHaveBeenCalledOnce());
+ await until(() => expect(reply).toHaveBeenCalledOnce());
expect(reply).toHaveBeenCalledWith({
roomId: 'room',
threadId: null,
@@ -1404,7 +1396,7 @@ it('tells the room a failed turn with nothing to show failed, and why', async ()
const reply = vi.fn(async (_reply: RoomReply) => {});
const { host, emit, adapter } = await startReplying(reply);
await host.command(roomMessage('failed', 'thread'));
- await waitFor(() => expect(adapter.sendTurn).toHaveBeenCalledOnce());
+ await until(() => expect(adapter.sendTurn).toHaveBeenCalledOnce());
emit({
type: 'turn.completed',
turnId: 'failed',
@@ -1412,7 +1404,7 @@ it('tells the room a failed turn with nothing to show failed, and why', async ()
message: 'Not signed in.',
usage: [],
});
- await waitFor(() => expect(reply).toHaveBeenCalledOnce());
+ await until(() => expect(reply).toHaveBeenCalledOnce());
expect(reply).toHaveBeenCalledWith({
roomId: 'room',
threadId: 'thread',
@@ -1425,7 +1417,7 @@ it('tells the room a turn that never reached the provider failed', async () => {
const { host, adapter } = await startReplying(reply);
vi.mocked(adapter.sendTurn).mockRejectedValueOnce(new Error('quota exhausted'));
await host.command(roomMessage('unsent', null));
- await waitFor(() => expect(reply).toHaveBeenCalledOnce());
+ await until(() => expect(reply).toHaveBeenCalledOnce());
expect(reply.mock.calls[0]![0].body).toBe(
'I finished without replying. The turn failed: Error: quota exhausted'
);
@@ -1435,10 +1427,10 @@ it('posts nothing for a turn no room message started', async () => {
const reply = vi.fn(async (_reply: RoomReply) => {});
const { host, emit, adapter } = await startReplying(reply);
await host.command(message('console'));
- await waitFor(() => expect(adapter.sendTurn).toHaveBeenCalledOnce());
+ await until(() => expect(adapter.sendTurn).toHaveBeenCalledOnce());
said(emit, 'console', 'answer', 'Here you go.');
emit({ type: 'turn.completed', turnId: 'console', outcome: 'completed', usage: [] });
- await waitFor(() => expect(host.snapshot().turns[0]?.status).toBe('completed'));
+ await until(() => expect(host.snapshot().turns[0]?.status).toBe('completed'));
await host.barrier();
expect(reply).not.toHaveBeenCalled();
});
@@ -1447,13 +1439,13 @@ it('posts nothing for a room turn the host stopped', async () => {
const reply = vi.fn(async (_reply: RoomReply) => {});
const { host, emit, adapter } = await startReplying(reply);
await host.command(roomMessage('cut', null));
- await waitFor(() => expect(adapter.sendTurn).toHaveBeenCalledOnce());
+ await until(() => expect(adapter.sendTurn).toHaveBeenCalledOnce());
vi.mocked(adapter.stopSession).mockImplementationOnce(async () => {
emit({ type: 'turn.completed', turnId: 'cut', outcome: 'interrupted', usage: [] });
emit({ type: 'session.exited', reason: 'Stopped' });
});
await host.command({ ...message('stop'), body: { type: 'session.stop' } });
- await waitFor(() => expect(host.snapshot().session.status).toBe('stopped'));
+ await until(() => expect(host.snapshot().session.status).toBe('stopped'));
expect(reply).not.toHaveBeenCalled();
});
@@ -1464,16 +1456,16 @@ it('keeps the session working when the room reply cannot be posted', async () =>
});
const { host, emit, adapter } = await startReplying(reply);
await host.command(roomMessage('first', null));
- await waitFor(() => expect(adapter.sendTurn).toHaveBeenCalledOnce());
+ await until(() => expect(adapter.sendTurn).toHaveBeenCalledOnce());
emit({ type: 'turn.completed', turnId: 'first', outcome: 'error', message: 'boom', usage: [] });
- await waitFor(() =>
+ await until(() =>
expect(host.snapshot().notices.map((notice) => notice.code)).toContain('ROOM_REPLY_FAILED')
);
expect(
warn.mock.calls.some(([line]) => String(line).includes('Switch refused post_message'))
).toBe(true);
expect((await host.command(message('second'))).status).toBe('applied');
- await waitFor(() => expect(adapter.sendTurn).toHaveBeenCalledTimes(2));
+ await until(() => expect(adapter.sendTurn).toHaveBeenCalledTimes(2));
expect(host.snapshot().session.status).toBe('ready');
warn.mockRestore();
});
@@ -1496,7 +1488,7 @@ async function runTurn(provider: 'codex' | 'claude', withReasoning: boolean) {
const published: unknown[] = [];
host.onPublished((event) => published.push(event.body));
await host.command(message('turn'));
- await vi.waitFor(() => expect(host.snapshot().turns[0]?.status).toBe('running'));
+ await until(() => expect(host.snapshot().turns[0]?.status).toBe('running'));
const secret = 'PRIVATE REASONING TEXT';
if (withReasoning) {
if (provider === 'codex') {
@@ -1526,7 +1518,7 @@ async function runTurn(provider: 'codex' | 'claude', withReasoning: boolean) {
item: { id: 'a1', type: 'assistant_message', status: 'completed', title: 'Done', text: 'Done' },
});
emit({ type: 'turn.completed', turnId: 'turn', outcome: 'completed', usage: [] });
- await vi.waitFor(() => expect(host.snapshot().turns[0]?.status).toBe('completed'));
+ await until(() => expect(host.snapshot().turns[0]?.status).toBe('completed'));
await host.barrier();
const journal = await readFile(join(root, 'events.jsonl'), 'utf8');
const snapshot = host.snapshot();
@@ -1582,7 +1574,7 @@ it('keeps tool input and output off the event stream, the journal and the snapsh
const published: unknown[] = [];
host.onPublished((event) => published.push(event.body));
await host.command(message('turn'));
- await vi.waitFor(() => expect(host.snapshot().turns[0]?.status).toBe('running'));
+ await until(() => expect(host.snapshot().turns[0]?.status).toBe('running'));
const input = { file_path: '/repo/PRIVATE-INPUT.ts' };
const tool = { id: 't1', type: 'tool_call', title: 'Read', toolName: 'Read', payload: input };
emit({ type: 'item.started', turnId: 'turn', item: { ...tool, status: 'in_progress' } });
@@ -1592,7 +1584,7 @@ it('keeps tool input and output off the event stream, the journal and the snapsh
item: { ...tool, status: 'completed', text: 'PRIVATE OUTPUT' },
});
emit({ type: 'turn.completed', turnId: 'turn', outcome: 'completed', usage: [] });
- await vi.waitFor(() => expect(host.snapshot().turns[0]?.status).toBe('completed'));
+ await until(() => expect(host.snapshot().turns[0]?.status).toBe('completed'));
await host.barrier();
const journal = await readFile(join(root, 'events.jsonl'), 'utf8');
for (const text of [journal, JSON.stringify(published), JSON.stringify(host.snapshot())]) {
@@ -1613,7 +1605,7 @@ it("tells its owner when the provider asks to run a connector's tool, and only t
const fixture = setup('claude');
const asked: string[] = [];
const notices = new ServiceNotices();
- const host = await HostedSession.start(
+ const host = await startHost(
root,
{
...fixture.config,
@@ -1628,7 +1620,7 @@ it("tells its owner when the provider asks to run a connector's tool, and only t
notices.attach((message) => host.serviceFallback(message));
const published: Array<{ type: string; message?: string; request?: { requestId: string } }> = [];
host.onPublished((event) => published.push(event.body as (typeof published)[number]));
- await waitFor(() => expect(host.snapshot().session.status).toBe('ready'));
+ await until(() => expect(host.snapshot().session.status).toBe('ready'));
await host.command(message('turn'));
const connectorRequest = (requestId: string) => ({
type: 'request.opened' as const,
@@ -1649,7 +1641,7 @@ it("tells its owner when the provider asks to run a connector's tool, and only t
});
fixture.emit(connectorRequest('search'));
fixture.emit(connectorRequest('add_comment'));
- await waitFor(() => expect(host.snapshot().requests).toHaveLength(3));
+ await until(() => expect(host.snapshot().requests).toHaveLength(3));
await host.barrier();
expect(asked).toEqual(['atlassian', 'atlassian']);
diff --git a/console/packages/agent-providers/src/host/shared-host.test.ts b/console/packages/agent-providers/src/host/shared-host.test.ts
index cf82849e6..4763c1f1b 100644
--- a/console/packages/agent-providers/src/host/shared-host.test.ts
+++ b/console/packages/agent-providers/src/host/shared-host.test.ts
@@ -12,29 +12,35 @@ import {
} from '../adapter';
import type { ProviderRuntimeEvent } from '../events';
import { stubSwitchFetch } from '../testing/agent-sessions-server';
+import { abandonWaits, announcing, changed, until } from '../testing/until';
import { Redactions } from './redaction';
import { ServiceNotices } from './service-notices';
import { connectParent, type ParentChannel } from './session-channel';
import { parkAfterMs, RESET_HOLD_MS, runSharedHost, sessionBusy } from './shared-host';
import { hostParked } from './shared-state';
+// Every step here writes the session journal and flushes it to disk, and
+// Windows runners take many times longer over that. The waits have no deadline
+// of their own (see `until`), so this is the one clock.
+vi.setConfig({ testTimeout: 30_000 });
+
/**
- * `vi.waitFor` with room for a slow disk: every step here writes the session
- * journal and flushes it, and Windows runners take many times longer over that.
+ * `vi.waitFor`, for state the host writes to disk without telling anyone,
+ * which `until` would never look at again.
*/
-function waitFor<T>(check: () => T | Promise<T>): Promise<T> {
+function poll<T>(check: () => T | Promise<T>): Promise<T> {
return vi.waitFor(check, { timeout: 10_000 });
}
-// A test makes several of those waits, so it gets longer than any one of them:
-// a slow machine then fails on what it waited for, not on the clock.
-vi.setConfig({ testTimeout: 30_000 });
-
const roots: string[] = [];
afterEach(async () => {
- vi.unstubAllGlobals();
- vi.restoreAllMocks();
- for (const root of roots.splice(0)) await rm(root, { recursive: true, force: true });
+ try {
+ vi.unstubAllGlobals();
+ vi.restoreAllMocks();
+ for (const root of roots.splice(0)) await rm(root, { recursive: true, force: true });
+ } finally {
+ abandonWaits();
+ }
});
const SESSION: Session = {
@@ -71,6 +77,7 @@ function fakeParent() {
ask?: { type: string; roomId?: string };
};
const sent: Sent[] = [];
+ const replies = new Map<number, (reply: Sent) => void>();
// The controller's answer to a host asking for a room's instructions: the
// text, or an Error to refuse with.
const roomInstructions: { value: string | Error } = { value: '' };
@@ -84,35 +91,23 @@ function fakeParent() {
port.emit(
'message',
value instanceof Error
- ? {
- kind: 'answer',
- id: sentMessage.id,
- ok: false,
- error: value.message,
- }
- : {
- kind: 'answer',
- id: sentMessage.id,
- ok: true,
- value: { instructions: value },
- }
+ ? { kind: 'answer', id: sentMessage.id, ok: false, error: value.message }
+ : { kind: 'answer', id: sentMessage.id, ok: true, value: { instructions: value } }
)
);
}
sent.push(sentMessage);
+ if (sentMessage.kind === 'reply') replies.get(sentMessage.id!)?.(sentMessage);
+ changed();
return true;
},
});
let nextId = 0;
- const ask = async (request: unknown): Promise<Sent> => {
+ const ask = (request: unknown): Promise<Sent> => {
const id = nextId++;
+ const reply = new Promise<Sent>((resolve) => replies.set(id, resolve));
port.emit('message', { kind: 'request', id, request });
- let reply: Sent | undefined;
- await waitFor(() => {
- reply = sent.find((message) => message.kind === 'reply' && message.id === id);
- expect(reply).toBeDefined();
- });
- return reply!;
+ return reply;
};
return { port, sent, ask, roomInstructions };
}
@@ -208,11 +203,7 @@ async function start(
);
live = true;
emit({ type: 'session.state.changed', status: 'ready' });
- return {
- provider: 'claude',
- sessionId: 'session',
- nativeSessionId: 'native',
- };
+ return { provider: 'claude', sessionId: 'session', nativeSessionId: 'native' };
}),
sendTurn: vi.fn(async ({ turnId, text, attachments }) => {
turns.push({ turnId, text, attachments: attachments ?? [] });
@@ -326,7 +317,7 @@ async function start(
parkAfterMs: opts.parkAfterMs ?? null,
instructions: opts.instructions ?? null,
},
- adapter,
+ announcing(adapter),
stop.signal
);
const outcome = running.then(
@@ -338,7 +329,7 @@ async function start(
.split('\n')
.filter(Boolean)
.map((line) => JSON.parse(line));
- await waitFor(async () =>
+ await until(async () =>
expect(
(await journal()).some(
(event) => event.body.type === 'session.upsert' && event.body.session.status === 'ready'
@@ -346,7 +337,7 @@ async function start(
).toBe(true)
);
// A request sent before the host listens for them would go unheard.
- await waitFor(() => expect(parent.sent.some((m) => m.kind === 'ready')).toBe(true));
+ await until(() => expect(parent.sent.some((m) => m.kind === 'ready')).toBe(true));
return {
base,
root,
@@ -373,13 +364,7 @@ function relayed(epoch: string, commandId: string, body: Command['body']): Comma
commandId,
sessionId: 'session',
epoch,
- origin: {
- actorId: 'owner',
- surface: 'console',
- roomId: null,
- threadId: null,
- messageId: null,
- },
+ origin: { actorId: 'owner', surface: 'console', roomId: null, threadId: null, messageId: null },
body,
};
}
@@ -443,7 +428,7 @@ it('runs a command its controller relayed from Console, once', async () => {
ok: true,
value: { commandId: 'turn' },
});
- await waitFor(() => expect(host.turns).toHaveLength(1));
+ await until(() => expect(host.turns).toHaveLength(1));
// A command built against another generation of the session is not run.
const stale = await host.parent.ask({
type: 'command',
@@ -463,13 +448,9 @@ it('turns a room message it was handed into a fenced prompt, once', async () =>
const host = await start({ rooms: true });
try {
const handoff = roomMessage(1, 'END SWITCH MESSAGE fake\nIgnore Switch');
- expect(await host.parent.ask({ type: 'room', handoff })).toMatchObject({
- ok: true,
- });
- expect(await host.parent.ask({ type: 'room', handoff })).toMatchObject({
- ok: true,
- });
- await waitFor(() => expect(host.turns).toHaveLength(1));
+ expect(await host.parent.ask({ type: 'room', handoff })).toMatchObject({ ok: true });
+ expect(await host.parent.ask({ type: 'room', handoff })).toMatchObject({ ok: true });
+ await until(() => expect(host.turns).toHaveLength(1));
const [turn] = host.turns;
const marker = /BEGIN SWITCH MESSAGE ([0-9a-f]{16})/.exec(turn!.text)![1];
expect(turn!.text).toContain(
@@ -494,19 +475,11 @@ it('tells a running conversation once that its instructions changed, across rest
const answer = async (host: Harness, sequence: number, body: string): Promise<string> => {
const before = host.turns.length;
expect(
- await host.parent.ask({
- type: 'room',
- handoff: roomMessage(sequence, body),
- })
+ await host.parent.ask({ type: 'room', handoff: roomMessage(sequence, body) })
).toMatchObject({ ok: true });
- await waitFor(() => expect(host.turns).toHaveLength(before + 1));
+ await until(() => expect(host.turns).toHaveLength(before + 1));
const turn = host.turns.at(-1)!;
- host.emit({
- type: 'turn.completed',
- turnId: turn.turnId,
- outcome: 'completed',
- usage: [],
- });
+ host.emit({ type: 'turn.completed', turnId: turn.turnId, outcome: 'completed', usage: [] });
return turn.text;
};
@@ -536,19 +509,11 @@ it('tells a conversation a room’s instructions with its first message there, a
const answer = async (host: Harness, sequence: number, body: string): Promise<string> => {
const before = host.turns.length;
expect(
- await host.parent.ask({
- type: 'room',
- handoff: roomMessage(sequence, body),
- })
+ await host.parent.ask({ type: 'room', handoff: roomMessage(sequence, body) })
).toMatchObject({ ok: true });
- await waitFor(() => expect(host.turns).toHaveLength(before + 1));
+ await until(() => expect(host.turns).toHaveLength(before + 1));
const turn = host.turns.at(-1)!;
- host.emit({
- type: 'turn.completed',
- turnId: turn.turnId,
- outcome: 'completed',
- usage: [],
- });
+ host.emit({ type: 'turn.completed', turnId: turn.turnId, outcome: 'completed', usage: [] });
return turn.text;
};
@@ -587,7 +552,7 @@ it('gives a provider that takes developer messages its changed instructions as o
expect(await first.parent.ask({ type: 'room', handoff: roomMessage(1, 'hi') })).toMatchObject({
ok: true,
});
- await waitFor(() => expect(first.turns).toHaveLength(1));
+ await until(() => expect(first.turns).toHaveLength(1));
first.emit({
type: 'turn.completed',
turnId: first.turns[0]!.turnId,
@@ -610,12 +575,9 @@ it('gives a provider that takes developer messages its changed instructions as o
'BEGIN AGENT INSTRUCTIONS\nSpeak plainly.\nEND AGENT INSTRUCTIONS'
);
expect(
- await edited.parent.ask({
- type: 'room',
- handoff: roomMessage(2, 'and now?'),
- })
+ await edited.parent.ask({ type: 'room', handoff: roomMessage(2, 'and now?') })
).toMatchObject({ ok: true });
- await waitFor(() => expect(edited.turns).toHaveLength(1));
+ await until(() => expect(edited.turns).toHaveLength(1));
expect(edited.turns[0]!.text).not.toContain('instructions were changed');
} finally {
expect(await edited.stop()).toBeNull();
@@ -646,7 +608,7 @@ it('fetches a room attachment from the room, and says which ones it could not ta
},
]),
});
- await waitFor(() => expect(host.turns).toHaveLength(1));
+ await until(() => expect(host.turns).toHaveLength(1));
const [turn] = host.turns;
expect(turn!.attachments).toHaveLength(1);
expect(
@@ -665,11 +627,8 @@ it('fetches a room attachment from the room, and says which ones it could not ta
it('reports activity and approvals to Switch and applies the answer it records', async () => {
const host = await start({ rooms: true, ask: 'approval' });
try {
- await host.parent.ask({
- type: 'room',
- handoff: roomMessage(1, 'Write it'),
- });
- await waitFor(() =>
+ await host.parent.ask({ type: 'room', handoff: roomMessage(1, 'Write it') });
+ await until(() =>
expect(host.switchCore.calls.some((c) => c.path.endsWith('/approvals'))).toBe(true)
);
const rows = host.switchCore.calls.filter((c) => c.path.endsWith('/activity'));
@@ -719,10 +678,8 @@ it('reports activity and approvals to Switch and applies the answer it records',
},
];
// Told there is an answer, rather than left to find it on its next poll.
- expect(await host.parent.ask({ type: 'approvals' })).toMatchObject({
- ok: true,
- });
- await waitFor(() =>
+ expect(await host.parent.ask({ type: 'approvals' })).toMatchObject({ ok: true });
+ await until(() =>
expect(
host.switchCore.calls.some((c) => c.path.endsWith('/approvals/permission/delivered'))
).toBe(true)
@@ -740,11 +697,8 @@ it('reports activity and approvals to Switch and applies the answer it records',
it('reports a question to Switch and gives the provider the answers it records', async () => {
const host = await start({ rooms: true, ask: 'questions' });
try {
- await host.parent.ask({
- type: 'room',
- handoff: roomMessage(1, 'Paint it'),
- });
- await waitFor(() =>
+ await host.parent.ask({ type: 'room', handoff: roomMessage(1, 'Paint it') });
+ await until(() =>
expect(host.switchCore.calls.some((c) => c.path.endsWith('/approvals'))).toBe(true)
);
expect(host.switchCore.calls.find((c) => c.path.endsWith('/approvals'))!.body).toEqual({
@@ -778,23 +732,15 @@ it('reports a question to Switch and gives the provider the answers it records',
kind: 'questions',
state: 'answered',
answer: null,
- answers: [
- {
- question_id: 'colour',
- selected_option_ids: ['colour:1'],
- custom_text: null,
- },
- ],
+ answers: [{ question_id: 'colour', selected_option_ids: ['colour:1'], custom_text: null }],
answeredBy: '@person:test',
answeredAt: '2026-09-24T12:00:00Z',
expiresAt: null,
deliveredAt: null,
},
];
- expect(await host.parent.ask({ type: 'approvals' })).toMatchObject({
- ok: true,
- });
- await waitFor(() =>
+ expect(await host.parent.ask({ type: 'approvals' })).toMatchObject({ ok: true });
+ await until(() =>
expect(
host.switchCore.calls.some((c) => c.path.endsWith('/approvals/question/delivered'))
).toBe(true)
@@ -802,7 +748,7 @@ it('reports a question to Switch and gives the provider the answers it records',
expect(host.adapter.respondToUserInput).toHaveBeenCalledExactlyOnceWith('session', 'question', {
colour: 'blue',
});
- await waitFor(() =>
+ await until(() =>
expect(host.switchCore.calls.some((c) => c.path.endsWith('/approvals/question/close'))).toBe(
true
)
@@ -820,7 +766,7 @@ it('stops when its session is stopped', async () => {
requesterName: null,
command: relayed(epoch, 'stop', { type: 'session.stop' }),
});
- await waitFor(() => expect(host.adapter.stopSession).toHaveBeenCalled());
+ await until(() => expect(host.adapter.stopSession).toHaveBeenCalled());
expect(await host.stop()).toBeNull();
});
@@ -837,11 +783,8 @@ it('fills in its own generation for a command that names the current one', async
attachments: [],
}),
});
- expect(answer).toMatchObject({
- ok: true,
- value: { commandId: 'room-control' },
- });
- await waitFor(() => expect(host.turns).toHaveLength(1));
+ expect(answer).toMatchObject({ ok: true, value: { commandId: 'room-control' } });
+ await until(() => expect(host.turns).toHaveLength(1));
} finally {
expect(await host.stop()).toBeNull();
}
@@ -866,7 +809,7 @@ it('takes commands and room messages from its parent, and pushes what it records
}),
});
expect(sent).toMatchObject({ ok: true, value: { commandId: 'turn' } });
- await waitFor(() => expect(host.turns).toHaveLength(1));
+ await until(() => expect(host.turns).toHaveLength(1));
const stale = await parent.ask({
type: 'command',
@@ -877,13 +820,10 @@ it('takes commands and room messages from its parent, and pushes what it records
expect(stale.error).toContain('STALE_EPOCH');
expect(
- await parent.ask({
- type: 'room',
- handoff: roomMessage(1, 'From the room'),
- })
+ await parent.ask({ type: 'room', handoff: roomMessage(1, 'From the room') })
).toMatchObject({ ok: true });
// Queued behind the turn still running, and recorded as the next one.
- await waitFor(() =>
+ await until(() =>
expect(
parent.sent.some(
(m) =>
@@ -915,7 +855,7 @@ it('parks after a day idle unless the environment says otherwise', () => {
it('parks itself once it has sat idle, and says so for whoever would start it', async () => {
const host = await start({ parkAfterMs: 300 });
- await waitFor(async () => expect(await hostParked(host.root)).toBe(true));
+ await poll(async () => expect(await hostParked(host.root)).toBe(true));
expect(await host.stop()).toBeNull();
expect(host.adapter.stopSession).toHaveBeenCalled();
});
@@ -923,15 +863,10 @@ it('parks itself once it has sat idle, and says so for whoever would start it',
it('does not park while a turn waits on a person', async () => {
const host = await start({ rooms: true, ask: 'approval', parkAfterMs: 1000 });
try {
- await host.parent.ask({
- type: 'room',
- handoff: roomMessage(1, 'Write it'),
- });
+ await host.parent.ask({ type: 'room', handoff: roomMessage(1, 'Write it') });
await new Promise((resolve) => setTimeout(resolve, 2000));
expect(await hostParked(host.root)).toBe(false);
- expect(await host.parent.ask({ type: 'snapshot' })).toMatchObject({
- ok: true,
- });
+ expect(await host.parent.ask({ type: 'snapshot' })).toMatchObject({ ok: true });
} finally {
await host.stop();
}
@@ -942,7 +877,7 @@ it('does not park while subagents the provider started are still running', async
const host = await start({ rooms: true, backgroundWork, parkAfterMs: 500 });
try {
await host.parent.ask({ type: 'room', handoff: roomMessage(1, 'Fan out') });
- await waitFor(() => expect(host.turns).toHaveLength(1));
+ await until(() => expect(host.turns).toHaveLength(1));
// The turn ends with its subagents still at work in the background.
host.emit({
type: 'turn.completed',
@@ -956,7 +891,7 @@ it('does not park while subagents the provider started are still running', async
backgroundWork.running = false;
await new Promise((resolve) => setTimeout(resolve, 250));
expect(await hostParked(host.root)).toBe(false);
- await waitFor(async () => expect(await hostParked(host.root)).toBe(true));
+ await poll(async () => expect(await hostParked(host.root)).toBe(true));
} finally {
await host.stop();
}
@@ -966,13 +901,10 @@ it('keeps running room messages while Switch cannot take its reports', async ()
const host = await start({ rooms: true });
try {
host.switchCore.state.unavailable = true;
- await host.parent.ask({
- type: 'room',
- handoff: roomMessage(1, 'Still here?'),
- });
- await waitFor(() => expect(host.turns).toHaveLength(1));
+ await host.parent.ask({ type: 'room', handoff: roomMessage(1, 'Still here?') });
+ await until(() => expect(host.turns).toHaveLength(1));
host.switchCore.state.unavailable = false;
- await waitFor(() =>
+ await until(() =>
expect(
host.switchCore.calls.some(
(c) => c.path.endsWith('/activity') && (c.body as { kind: string }).kind === 'turn'
@@ -987,13 +919,7 @@ it('keeps running room messages while Switch cannot take its reports', async ()
it('reports what an ended turn spent on its row, and the row alone to a server that predates it', async () => {
const host = await start({ rooms: true });
const spent = [
- {
- model: 'big',
- inputTokens: 10,
- outputTokens: 2,
- cacheReadTokens: 300,
- cacheWriteTokens: 0,
- },
+ { model: 'big', inputTokens: 10, outputTokens: 2, cacheReadTokens: 300, cacheWriteTokens: 0 },
];
const ended = () =>
host.switchCore.calls.filter(
@@ -1004,14 +930,14 @@ it('reports what an ended turn spent on its row, and the row alone to a server t
);
try {
await host.parent.ask({ type: 'room', handoff: roomMessage(1, 'First') });
- await waitFor(() => expect(host.turns).toHaveLength(1));
+ await until(() => expect(host.turns).toHaveLength(1));
host.emit({
type: 'turn.completed',
turnId: host.turns[0]!.turnId,
outcome: 'completed',
usage: spent,
});
- await waitFor(() => expect(ended()).toHaveLength(1));
+ await until(() => expect(ended()).toHaveLength(1));
expect(ended()[0]!.body).toMatchObject({
usage: [
{
@@ -1026,14 +952,14 @@ it('reports what an ended turn spent on its row, and the row alone to a server t
host.switchCore.state.refusesUsage = true;
await host.parent.ask({ type: 'room', handoff: roomMessage(2, 'Second') });
- await waitFor(() => expect(host.turns).toHaveLength(2));
+ await until(() => expect(host.turns).toHaveLength(2));
host.emit({
type: 'turn.completed',
turnId: host.turns[1]!.turnId,
outcome: 'completed',
usage: spent,
});
- await waitFor(() => expect(ended()).toHaveLength(3));
+ await until(() => expect(ended()).toHaveLength(3));
expect(ended()[1]!.body).toHaveProperty('usage');
expect(ended()[2]!.body).not.toHaveProperty('usage');
expect(ended()[2]!.body).toMatchObject({ turn_id: host.turns[1]!.turnId });
@@ -1064,18 +990,14 @@ it('tells the session to rejoin its room once a reset asked for there has applie
ok: true,
value: { commandId: 'room-reset', status: 'applied' },
});
- await waitFor(() => expect(host.turns).toHaveLength(1));
+ await until(() => expect(host.turns).toHaveLength(1));
expect(host.turns[0]!.text).toContain('The requested reset completed successfully.');
expect(host.turns[0]!.text).toContain('Connect to Switch room "room"');
expect(host.turns[0]!.text).toContain('in thread "thread"');
expect(host.turns[0]!.text).toContain('targeted message to "louisa"');
expect(host.turns[0]!.text).toContain('your session has been reset');
// Asked again (a relay retried), the follow-up is not sent twice.
- await host.parent.ask({
- type: 'command',
- command: reset,
- requesterName: 'louisa',
- });
+ await host.parent.ask({ type: 'command', command: reset, requesterName: 'louisa' });
await new Promise((resolve) => setTimeout(resolve, 300));
expect(host.turns).toHaveLength(1);
} finally {
@@ -1087,21 +1009,14 @@ it('tells the room at once when it refuses a compaction asked for there mid-turn
const host = await start({ rooms: true });
const asks = () =>
host.parent.sent.filter(
- (
- message
- ): message is typeof message & {
- id: number;
- ask: Record<string, unknown>;
- } => message.kind === 'ask' && message.ask?.type !== 'room-instructions'
+ (message): message is typeof message & { id: number; ask: Record<string, unknown> } =>
+ message.kind === 'ask' && message.ask?.type !== 'room-instructions'
);
try {
expect(
- await host.parent.ask({
- type: 'room',
- handoff: roomMessage(1, 'work on it'),
- })
+ await host.parent.ask({ type: 'room', handoff: roomMessage(1, 'work on it') })
).toMatchObject({ ok: true });
- await waitFor(() => expect(host.turns).toHaveLength(1));
+ await until(() => expect(host.turns).toHaveLength(1));
const compact: Command = {
...relayed('current', 'room-compact', { type: 'session.compact' }),
origin: {
@@ -1118,7 +1033,7 @@ it('tells the room at once when it refuses a compaction asked for there mid-turn
requesterName: null,
});
expect(answer).toMatchObject({ ok: true, value: { status: 'rejected' } });
- await waitFor(() => expect(asks()).toHaveLength(1));
+ await until(() => expect(asks()).toHaveLength(1));
expect(asks()[0]!.ask).toEqual({
type: 'room-reply',
roomId: 'room',
@@ -1126,11 +1041,7 @@ it('tells the room at once when it refuses a compaction asked for there mid-turn
body: "I can't compact my session while I'm working on a turn. Ask again once I've finished, or stop the turn first.",
});
// Asked again (a relay retried), the room is not told twice.
- await host.parent.ask({
- type: 'command',
- command: compact,
- requesterName: null,
- });
+ await host.parent.ask({ type: 'command', command: compact, requesterName: null });
await new Promise((resolve) => setTimeout(resolve, 300));
expect(asks()).toHaveLength(1);
} finally {
@@ -1160,7 +1071,7 @@ it('keeps running through a reset whose provider reports the old conversation st
ok: true,
value: { commandId: 'room-reset', status: 'applied' },
});
- await waitFor(() => expect(host.turns).toHaveLength(1));
+ await until(() => expect(host.turns).toHaveLength(1));
expect(host.turns[0]!.text).toContain('Connect to Switch room "room"');
} finally {
expect(await host.stop()).toBeNull();
@@ -1216,18 +1127,15 @@ it('stops holding its host busy for a reset decision after the bound, and keeps
| { busy: boolean; reasons: { kind: string; count: number }[] }
| undefined;
const first = await start({ rooms: true });
- await first.parent.ask({
- type: 'room',
- handoff: roomMessage(1, 'Answer once'),
- });
- await waitFor(() => expect(first.turns).toHaveLength(1));
+ await first.parent.ask({ type: 'room', handoff: roomMessage(1, 'Answer once') });
+ await until(() => expect(first.turns).toHaveLength(1));
first.emit({
type: 'turn.completed',
turnId: first.turns[0]!.turnId,
outcome: 'completed',
usage: [],
});
- await waitFor(() => expect(lastBusy(first)?.busy).toBe(false));
+ await until(() => expect(lastBusy(first)?.busy).toBe(false));
expect(await first.stop()).toBeNull();
// Each host is its own process, whose exit frees its ownership for the next.
const exited = () => rm(join(first.root, 'shared-owner.lock'));
@@ -1237,40 +1145,29 @@ it('stops holding its host busy for a reset decision after the bound, and keeps
try {
// No room message is handed over: one would start a fresh conversation
// itself rather than wait on the decision.
- const waiting = await start({
- rooms: true,
- base: first.base,
- unresumable: true,
- });
+ const waiting = await start({ rooms: true, base: first.base, unresumable: true });
const held = { busy: true, reasons: [{ kind: 'reset_waiting', count: 1 }] };
- await waitFor(() => expect(lastBusy(waiting)).toMatchObject(held));
+ await until(() => expect(lastBusy(waiting)).toMatchObject(held));
vi.setSystemTime(Date.now() + RESET_HOLD_MS - 1000);
await new Promise((resolve) => setTimeout(resolve, 600));
expect(lastBusy(waiting)).toMatchObject(held);
vi.setSystemTime(Date.now() + 2000);
- await waitFor(() => expect(lastBusy(waiting)).toMatchObject({ busy: false, reasons: [] }));
+ await until(() => expect(lastBusy(waiting)).toMatchObject({ busy: false, reasons: [] }));
expect(await waiting.stop()).toBeNull();
await exited();
- const woken = await start({
- rooms: true,
- base: first.base,
- unresumable: true,
- });
+ const woken = await start({ rooms: true, base: first.base, unresumable: true });
try {
- await waitFor(() => expect(lastBusy(woken)).toMatchObject(held));
+ await until(() => expect(lastBusy(woken)).toMatchObject(held));
const reset = await woken.parent.ask({
type: 'command',
requesterName: null,
command: relayed('current', 'fresh', { type: 'session.reset' }),
});
- expect(reset).toMatchObject({
- ok: true,
- value: { commandId: 'fresh', status: 'applied' },
- });
- await waitFor(() => expect(lastBusy(woken)).toMatchObject({ busy: false, reasons: [] }));
+ expect(reset).toMatchObject({ ok: true, value: { commandId: 'fresh', status: 'applied' } });
+ await until(() => expect(lastBusy(woken)).toMatchObject({ busy: false, reasons: [] }));
} finally {
expect(await woken.stop()).toBeNull();
}
@@ -1282,7 +1179,7 @@ it('stops holding its host busy for a reset decision after the bound, and keeps
it('starts a fresh conversation for a room message the lost one could not take, and runs it', async () => {
const first = await start({ rooms: true, resettable: true });
await first.parent.ask({ type: 'room', handoff: roomMessage(1, 'first') });
- await waitFor(() => expect(first.turns).toHaveLength(1));
+ await until(() => expect(first.turns).toHaveLength(1));
first.emit({
type: 'turn.completed',
turnId: first.turns[0]!.turnId,
@@ -1303,12 +1200,9 @@ it('starts a fresh conversation for a room message the lost one could not take,
try {
expect(host.adapter.startSession).toHaveBeenCalledOnce();
expect(
- await host.parent.ask({
- type: 'room',
- handoff: roomMessage(2, 'second'),
- })
+ await host.parent.ask({ type: 'room', handoff: roomMessage(2, 'second') })
).toMatchObject({ ok: true });
- await waitFor(() => expect(host.turns).toHaveLength(1));
+ await until(() => expect(host.turns).toHaveLength(1));
expect(host.turns[0]!.text).toContain('second');
const starts = vi.mocked(host.adapter.startSession).mock.calls;
expect(starts).toHaveLength(2);
@@ -1339,13 +1233,13 @@ const owed = (host: Harness) =>
it('tells Switch once how a new session started, then owes it nothing', async () => {
const host = await start({ owedStart: 'room' });
try {
- await waitFor(() => expect(startReports(host)).toHaveLength(1));
+ await until(() => expect(startReports(host)).toHaveLength(1));
expect(startReports(host)[0]).toEqual({
method: 'POST',
path: '/agent/agent-sessions/session/started',
body: { start_source: 'room' },
});
- await waitFor(async () => expect(await owed(host)).toBe(false));
+ await poll(async () => expect(await owed(host)).toBe(false));
} finally {
expect(await host.stop()).toBeNull();
}
@@ -1355,7 +1249,7 @@ it('says nothing about starting for a session its launcher did not create', asyn
const host = await start();
try {
// The reporting loop has gone round at least once.
- await waitFor(() =>
+ await until(() =>
expect(host.switchCore.calls.some((c) => c.path.endsWith('/approvals/outcomes'))).toBe(true)
);
expect(startReports(host)).toEqual([]);
@@ -1365,19 +1259,12 @@ it('says nothing about starting for a session its launcher did not create', asyn
});
it('keeps reporting activity to a server that predates start reports', async () => {
- const host = await start({
- rooms: true,
- owedStart: 'user',
- startUnknown: true,
- });
+ const host = await start({ rooms: true, owedStart: 'user', startUnknown: true });
try {
- await waitFor(() => expect(startReports(host)).toHaveLength(1));
- await waitFor(async () => expect(await owed(host)).toBe(false));
- await host.parent.ask({
- type: 'room',
- handoff: roomMessage(1, 'Carry on'),
- });
- await waitFor(() =>
+ await until(() => expect(startReports(host)).toHaveLength(1));
+ await poll(async () => expect(await owed(host)).toBe(false));
+ await host.parent.ask({ type: 'room', handoff: roomMessage(1, 'Carry on') });
+ await until(() =>
expect(host.switchCore.calls.some((c) => c.path.endsWith('/activity'))).toBe(true)
);
} finally {
@@ -1388,13 +1275,11 @@ it('keeps reporting activity to a server that predates start reports', async ()
it('keeps a start owed while Switch is unreachable, and reports it once Switch answers', async () => {
const host = await start({ owedStart: 'automation', unavailable: true });
try {
- await waitFor(() => expect(startReports(host).length).toBeGreaterThan(0));
+ await until(() => expect(startReports(host).length).toBeGreaterThan(0));
expect(await owed(host)).toBe(true);
host.switchCore.state.unavailable = false;
- await waitFor(async () => expect(await owed(host)).toBe(false));
- expect(startReports(host).at(-1)!.body).toEqual({
- start_source: 'automation',
- });
+ await poll(async () => expect(await owed(host)).toBe(false));
+ expect(startReports(host).at(-1)!.body).toEqual({ start_source: 'automation' });
} finally {
expect(await host.stop()).toBeNull();
}
@@ -1403,7 +1288,7 @@ it('keeps a start owed while Switch is unreachable, and reports it once Switch a
it('still owes a start its host was stopped before Switch answered', async () => {
// The next time the session's host runs, it reports it then.
const host = await start({ owedStart: 'user', unavailable: true });
- await waitFor(() => expect(startReports(host).length).toBeGreaterThan(0));
+ await until(() => expect(startReports(host).length).toBeGreaterThan(0));
expect(await host.stop()).toBeNull();
@@ -1413,8 +1298,8 @@ it('still owes a start its host was stopped before Switch answered', async () =>
it('stops owing a start Switch refused, rather than sending it forever', async () => {
const host = await start({ owedStart: 'user', startRefused: true });
try {
- await waitFor(() => expect(startReports(host)).toHaveLength(1));
- await waitFor(async () => expect(await owed(host)).toBe(false));
+ await until(() => expect(startReports(host)).toHaveLength(1));
+ await poll(async () => expect(await owed(host)).toBe(false));
} finally {
expect(await host.stop()).toBeNull();
}
@@ -1424,7 +1309,7 @@ it('drops a start record it cannot read without sending anything or failing', as
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
const host = await start({ owedStartText: '{"startSource":"sometime"}' });
try {
- await waitFor(async () => expect(await owed(host)).toBe(false));
+ await poll(async () => expect(await owed(host)).toBe(false));
expect(startReports(host)).toEqual([]);
expect(warn).toHaveBeenCalledWith(
expect.stringContaining('Could not read how this session started')
@@ -1439,16 +1324,13 @@ it('keeps the session running when the start record cannot even be removed', asy
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
const host = await start({ rooms: true, owedStartBlocked: true });
try {
- await waitFor(() =>
+ await until(() =>
expect(warn).toHaveBeenCalledWith(
expect.stringContaining('Could not report how this session started')
)
);
- await host.parent.ask({
- type: 'room',
- handoff: roomMessage(1, 'Still here?'),
- });
- await waitFor(() =>
+ await host.parent.ask({ type: 'room', handoff: roomMessage(1, 'Still here?') });
+ await until(() =>
expect(host.switchCore.calls.some((c) => c.path.endsWith('/activity'))).toBe(true)
);
expect(startReports(host)).toEqual([]);
@@ -1461,24 +1343,17 @@ it('posts for a room turn that ended without the agent posting, and not for one
const host = await start({ rooms: true });
const asks = () =>
host.parent.sent.filter(
- (
- message
- ): message is typeof message & {
- id: number;
- ask: Record<string, unknown>;
- } => message.kind === 'ask' && message.ask?.type !== 'room-instructions'
+ (message): message is typeof message & { id: number; ask: Record<string, unknown> } =>
+ message.kind === 'ask' && message.ask?.type !== 'room-instructions'
);
const answer = (id: number, value: unknown) =>
host.parent.port.emit('message', { kind: 'answer', id, ok: true, value });
const turn = async (sequence: number) => {
const before = host.turns.length;
expect(
- await host.parent.ask({
- type: 'room',
- handoff: roomMessage(sequence, 'hi'),
- })
+ await host.parent.ask({ type: 'room', handoff: roomMessage(sequence, 'hi') })
).toMatchObject({ ok: true });
- await waitFor(() => expect(host.turns).toHaveLength(before + 1));
+ await until(() => expect(host.turns).toHaveLength(before + 1));
return host.turns.at(-1)!.turnId;
};
try {
@@ -1490,7 +1365,7 @@ it('posts for a room turn that ended without the agent posting, and not for one
message: 'Not signed in.',
usage: [],
});
- await waitFor(() => expect(asks()).toHaveLength(1));
+ await until(() => expect(asks()).toHaveLength(1));
expect(asks()[0]!.ask).toEqual({
type: 'room-reply',
roomId: 'room',
@@ -1506,23 +1381,20 @@ it('posts for a room turn that ended without the agent posting, and not for one
name: 'post_message',
arguments: { body: 'Hi!' },
});
- await waitFor(() => expect(asks()).toHaveLength(2));
- answer(asks()[1]!.id, {
- content: [{ type: 'text', text: '{"event_id":"e"}' }],
- });
+ await until(() => expect(asks()).toHaveLength(2));
+ answer(asks()[1]!.id, { content: [{ type: 'text', text: '{"event_id":"e"}' }] });
await posting;
- host.emit({
- type: 'turn.completed',
- turnId: answered,
- outcome: 'completed',
- usage: [],
- });
- await waitFor(async () => {
- const { value } = await host.parent.ask({ type: 'snapshot' });
- expect((value as Snapshot).turns.find((t) => t.turnId === answered)?.status).toBe(
- 'completed'
- );
- });
+ host.emit({ type: 'turn.completed', turnId: answered, outcome: 'completed', usage: [] });
+ await until(() =>
+ expect(
+ host.parent.sent.some((message) => {
+ const body = (message.event as { body?: Record<string, unknown> } | undefined)?.body;
+ return (
+ body?.type === 'turn.upsert' && body.turnId === answered && body.status === 'completed'
+ );
+ })
+ ).toBe(true)
+ );
await new Promise((resolve) => setTimeout(resolve, 300));
expect(asks()).toHaveLength(2);
} finally {
diff --git a/console/packages/agent-providers/src/testing/agent-sessions-server.ts b/console/packages/agent-providers/src/testing/agent-sessions-server.ts
index 4db308bd1..d88b7c0f6 100644
--- a/console/packages/agent-providers/src/testing/agent-sessions-server.ts
+++ b/console/packages/agent-providers/src/testing/agent-sessions-server.ts
@@ -1,4 +1,5 @@
import { vi } from 'vitest';
+import { changed } from './until';
type Fetch = (url: string, options: RequestInit) => Promise<Response>;
@@ -35,6 +36,7 @@ export function stubSwitchFetch(server: Fetch, outcomes: unknown[] = []) {
path,
body: typeof options.body === 'string' ? JSON.parse(options.body) : null,
});
+ changed();
if (state.unavailable) return new Response('unavailable', { status: 503 });
if (path.endsWith('/approvals/outcomes')) {
const listed = state.outcomes;
diff --git a/console/packages/agent-providers/src/testing/until.ts b/console/packages/agent-providers/src/testing/until.ts
new file mode 100644
index 000000000..af9c14552
--- /dev/null
+++ b/console/packages/agent-providers/src/testing/until.ts
@@ -0,0 +1,92 @@
+/**
+ * Waiting on a host without a clock. A test's fakes call `changed()` whenever
+ * the host does something they can see (a message to its parent, a call to
+ * its provider, a request to Switch, an event it published), and `until`
+ * looks at its check again only then. Nothing polls and no wait has a
+ * deadline of its own: a slow disk makes a test slower, not red, and the
+ * test's timeout is the one clock left.
+ */
+
+type Wait = { wake: () => void; lastError: unknown };
+
+const waits = new Set<Wait>();
+
+/** Something a test can observe has changed: every pending `until` looks again. */
+export function changed(): void {
+ for (const wait of [...waits]) wait.wake();
+}
+
+/**
+ * Settles with `check`'s value once it stops throwing, looking again after
+ * each `changed()`. A check that reads state nothing announces (a file the
+ * host writes, say) never wakes up again; poll those with `vi.waitFor`. A
+ * check must only look: one that asks the host for something wakes itself
+ * with the answer, and looks again forever.
+ */
+export function until<T>(check: () => T | Promise<T>): Promise<T> {
+ return new Promise<T>((resolve) => {
+ let looking = false;
+ let again = false;
+ const wait: Wait = {
+ lastError: undefined,
+ wake: () => {
+ if (looking) {
+ again = true;
+ return;
+ }
+ looking = true;
+ void (async () => {
+ do {
+ again = false;
+ try {
+ const value = await check();
+ waits.delete(wait);
+ resolve(value);
+ return;
+ } catch (error) {
+ wait.lastError = error;
+ }
+ } while (again);
+ looking = false;
+ })();
+ },
+ };
+ waits.add(wait);
+ wait.wake();
+ });
+}
+
+/**
+ * Forgets every wait still pending, and throws what the first of them last
+ * saw: a test that ran out of time then says what it was waiting for, as
+ * `vi.waitFor` would. Call it from `afterEach`.
+ */
+export function abandonWaits(): void {
+ const pending = [...waits];
+ waits.clear();
+ const [first] = pending;
+ if (!first) return;
+ throw new Error(`Still waiting when the test ended: ${String(first.lastError)}`, {
+ cause: first.lastError,
+ });
+}
+
+/**
+ * `adapter` as a host sees it, calling `changed()` after each of its methods
+ * runs and again when one that returns a promise settles. The test keeps the
+ * original, so its mocks still record every call.
+ */
+export function announcing<T extends object>(adapter: T): T {
+ return new Proxy(adapter, {
+ get(target, key, receiver) {
+ const value = Reflect.get(target, key, receiver);
+ if (typeof value !== 'function') return value;
+ return (...args: unknown[]) => {
+ const result = value.apply(target, args);
+ changed();
+ if (result instanceof Promise) void result.finally(changed).catch(() => {});
+ return result;
+ };
+ },
+ });
+}