feat(companion): add waitForIdle runtime drain helper
This commit is contained in:
@@ -12,6 +12,7 @@ export { CompanionHeartbeatLoop } from './heartbeatLoop.js';
|
||||
|
||||
export type {
|
||||
CompanionRuntimeClientOptions,
|
||||
WaitForIdleOptions,
|
||||
CompanionEventHandler,
|
||||
CompanionTypedEventHandler,
|
||||
CompanionEventName,
|
||||
|
||||
@@ -33,6 +33,7 @@ function createRuntimeMock(): {
|
||||
clearEventSubscriptions: ReturnType<typeof vi.fn>;
|
||||
listKnownEventNames: ReturnType<typeof vi.fn>;
|
||||
waitForEvent: ReturnType<typeof vi.fn>;
|
||||
waitForIdle: ReturnType<typeof vi.fn>;
|
||||
waitForAgentStream: ReturnType<typeof vi.fn>;
|
||||
waitForAgentTyping: ReturnType<typeof vi.fn>;
|
||||
waitForContextWarning: ReturnType<typeof vi.fn>;
|
||||
@@ -75,6 +76,7 @@ function createRuntimeMock(): {
|
||||
const clearEventSubscriptions = vi.fn(() => undefined);
|
||||
const listKnownEventNames = vi.fn(() => ['agent.stream', 'agent.typing', 'context_warning']);
|
||||
const waitForEvent = vi.fn(async () => ({ token: 'evented' }));
|
||||
const waitForIdle = vi.fn(async () => undefined);
|
||||
const waitForAgentStream = vi.fn(async () => ({ token: 'streamed' }));
|
||||
const waitForAgentTyping = vi.fn(async () => ({ active: true }));
|
||||
const waitForContextWarning = vi.fn(async () => ({ thresholdPct: 75, estimatedPct: 90 }));
|
||||
@@ -111,6 +113,7 @@ function createRuntimeMock(): {
|
||||
clearEventSubscriptions,
|
||||
listKnownEventNames,
|
||||
waitForEvent,
|
||||
waitForIdle,
|
||||
waitForAgentStream,
|
||||
waitForAgentTyping,
|
||||
waitForContextWarning,
|
||||
@@ -159,6 +162,7 @@ function createRuntimeMock(): {
|
||||
clearEventSubscriptions,
|
||||
listKnownEventNames,
|
||||
waitForEvent,
|
||||
waitForIdle,
|
||||
waitForAgentStream,
|
||||
waitForAgentTyping,
|
||||
waitForContextWarning,
|
||||
@@ -333,6 +337,15 @@ describe('platform companion clients', () => {
|
||||
expect(client.hasPendingWork).toBe(mock.hasPendingWork);
|
||||
});
|
||||
|
||||
it('platform waitForIdle forwards options to runtime client', async () => {
|
||||
const mock = createRuntimeMock();
|
||||
const client = new IOSCompanionClient({ runtime: mock.runtime, nodeId: 'ios-node' });
|
||||
|
||||
await client.waitForIdle({ timeoutMs: 250, pollIntervalMs: 10 });
|
||||
|
||||
expect(mock.waitForIdle).toHaveBeenCalledWith({ timeoutMs: 250, pollIntervalMs: 10 });
|
||||
});
|
||||
|
||||
it('macOS client forwards canvas methods to runtime client', async () => {
|
||||
const mock = createRuntimeMock();
|
||||
const client = new MacOSCompanionClient({ runtime: mock.runtime, nodeId: 'mac-node' });
|
||||
|
||||
@@ -22,6 +22,7 @@ import type {
|
||||
SetNodeLocationInput,
|
||||
SystemCapabilitiesResult,
|
||||
SystemNodesResult,
|
||||
WaitForIdleOptions,
|
||||
} from './runtimeClient.js';
|
||||
import {
|
||||
CompanionHeartbeatLoop,
|
||||
@@ -325,6 +326,10 @@ export class MacOSCompanionClient {
|
||||
return this.runtime.waitForContextWarning(options);
|
||||
}
|
||||
|
||||
waitForIdle(options?: WaitForIdleOptions): Promise<void> {
|
||||
return this.runtime.waitForIdle(options);
|
||||
}
|
||||
|
||||
private resolveSessionId(sessionId?: string): string {
|
||||
const resolved = sessionId ?? this.defaultSessionId;
|
||||
if (!resolved) {
|
||||
@@ -579,6 +584,10 @@ export class IOSCompanionClient {
|
||||
return this.runtime.waitForContextWarning(options);
|
||||
}
|
||||
|
||||
waitForIdle(options?: WaitForIdleOptions): Promise<void> {
|
||||
return this.runtime.waitForIdle(options);
|
||||
}
|
||||
|
||||
private resolveSessionId(sessionId?: string): string {
|
||||
const resolved = sessionId ?? this.defaultSessionId;
|
||||
if (!resolved) {
|
||||
@@ -831,6 +840,10 @@ export class AndroidCompanionClient {
|
||||
return this.runtime.waitForContextWarning(options);
|
||||
}
|
||||
|
||||
waitForIdle(options?: WaitForIdleOptions): Promise<void> {
|
||||
return this.runtime.waitForIdle(options);
|
||||
}
|
||||
|
||||
private resolveSessionId(sessionId?: string): string {
|
||||
const resolved = sessionId ?? this.defaultSessionId;
|
||||
if (!resolved) {
|
||||
|
||||
@@ -643,6 +643,56 @@ describe('CompanionRuntimeClient', () => {
|
||||
expect(client.hasPendingWork).toBe(false);
|
||||
});
|
||||
|
||||
it('waitForIdle resolves immediately when no work is pending', async () => {
|
||||
const client = new CompanionRuntimeClient({
|
||||
url: 'ws://127.0.0.1:1',
|
||||
});
|
||||
|
||||
await expect(client.waitForIdle()).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it('waitForIdle validates pollIntervalMs option', () => {
|
||||
const client = new CompanionRuntimeClient({
|
||||
url: 'ws://127.0.0.1:1',
|
||||
});
|
||||
|
||||
expect(() => client.waitForIdle({ pollIntervalMs: 0 })).toThrow(
|
||||
'pollIntervalMs must be a positive number',
|
||||
);
|
||||
});
|
||||
|
||||
it('waitForIdle resolves after pending event waiters are cleared', async () => {
|
||||
const client = new CompanionRuntimeClient({
|
||||
url: 'ws://127.0.0.1:1',
|
||||
});
|
||||
const pendingWait = client.waitForEvent('agent.stream', { timeoutMs: 10_000 }).catch(() => undefined);
|
||||
expect(client.pendingEventWaitCount).toBe(1);
|
||||
|
||||
const idle = client.waitForIdle({ timeoutMs: 1_000, pollIntervalMs: 5 });
|
||||
setTimeout(() => {
|
||||
client.clearEventSubscriptions();
|
||||
}, 20);
|
||||
|
||||
await expect(idle).resolves.toBeUndefined();
|
||||
await pendingWait;
|
||||
expect(client.pendingEventWaitCount).toBe(0);
|
||||
expect(client.hasPendingWork).toBe(false);
|
||||
});
|
||||
|
||||
it('waitForIdle rejects on timeout while work remains pending', async () => {
|
||||
const client = new CompanionRuntimeClient({
|
||||
url: 'ws://127.0.0.1:1',
|
||||
});
|
||||
const pendingWait = client.waitForEvent('agent.stream', { timeoutMs: 10_000 }).catch(() => undefined);
|
||||
|
||||
await expect(
|
||||
client.waitForIdle({ timeoutMs: 40, pollIntervalMs: 5 }),
|
||||
).rejects.toThrow('Timed out waiting for runtime idle state');
|
||||
|
||||
client.clearEventSubscriptions();
|
||||
await pendingWait;
|
||||
});
|
||||
|
||||
it('connects and performs node registration + capability discovery', async () => {
|
||||
if (!LISTEN_ALLOWED) {
|
||||
return;
|
||||
|
||||
@@ -41,6 +41,12 @@ export interface CompanionRuntimeClientOptions {
|
||||
websocketFactory?: (url: string) => WebSocket;
|
||||
}
|
||||
|
||||
export interface WaitForIdleOptions {
|
||||
timeoutMs?: number;
|
||||
pollIntervalMs?: number;
|
||||
signal?: AbortSignal;
|
||||
}
|
||||
|
||||
export type CompanionEventHandler = (event: string, data: unknown) => void;
|
||||
export type CompanionTypedEventHandler<TData = unknown> = (data: TData) => void;
|
||||
export type CompanionEventPredicate<TData = unknown> = (data: TData) => boolean;
|
||||
@@ -607,6 +613,74 @@ export class CompanionRuntimeClient {
|
||||
return this.waitForEvent<TData>(COMPANION_EVENT_NAMES.contextWarning, options);
|
||||
}
|
||||
|
||||
waitForIdle(options?: WaitForIdleOptions): Promise<void> {
|
||||
const pollIntervalMs = options?.pollIntervalMs ?? 25;
|
||||
if (!Number.isFinite(pollIntervalMs) || pollIntervalMs <= 0) {
|
||||
throw new Error('pollIntervalMs must be a positive number');
|
||||
}
|
||||
if (!this.hasPendingWork) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
const timeoutMs = options?.timeoutMs ?? this.requestTimeoutMs;
|
||||
const signal = options?.signal;
|
||||
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
let settled = false;
|
||||
let timeout: NodeJS.Timeout | null = null;
|
||||
let poll: NodeJS.Timeout | null = null;
|
||||
let abortCleanup: (() => void) | null = null;
|
||||
|
||||
const cleanup = () => {
|
||||
if (timeout) {
|
||||
clearTimeout(timeout);
|
||||
timeout = null;
|
||||
}
|
||||
if (poll) {
|
||||
clearInterval(poll);
|
||||
poll = null;
|
||||
}
|
||||
if (abortCleanup) {
|
||||
abortCleanup();
|
||||
abortCleanup = null;
|
||||
}
|
||||
};
|
||||
|
||||
const finish = (fn: () => void) => {
|
||||
if (settled) {
|
||||
return;
|
||||
}
|
||||
settled = true;
|
||||
cleanup();
|
||||
fn();
|
||||
};
|
||||
|
||||
const check = () => {
|
||||
if (!this.hasPendingWork) {
|
||||
finish(() => resolve());
|
||||
}
|
||||
};
|
||||
|
||||
timeout = setTimeout(() => {
|
||||
finish(() => reject(new Error('Timed out waiting for runtime idle state')));
|
||||
}, timeoutMs);
|
||||
poll = setInterval(check, pollIntervalMs);
|
||||
check();
|
||||
|
||||
if (signal) {
|
||||
const onAbort = () => {
|
||||
finish(() => reject(new Error('Aborted while waiting for runtime idle state')));
|
||||
};
|
||||
signal.addEventListener('abort', onAbort, { once: true });
|
||||
abortCleanup = () => {
|
||||
signal.removeEventListener('abort', onAbort);
|
||||
};
|
||||
if (signal.aborted) {
|
||||
onAbort();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
listKnownEventNames(): CompanionEventName[] {
|
||||
return Object.values(COMPANION_EVENT_NAMES);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user