feat(companion): expose pending event wait observability

This commit is contained in:
William Valentin
2026-02-16 20:52:49 -08:00
parent df86fbd08e
commit 21a57c88b9
6 changed files with 49 additions and 2 deletions
+2 -2
View File
@@ -1190,7 +1190,7 @@ Methods:
- `system.capabilities` returns gateway protocol and node policy snapshot.
Companion runtime helper:
- `src/companion/runtimeClient.ts` provides a typed Node/WebSocket client for companion runtimes (macOS/iOS/Android workers) with wrappers for `node.register`, `node.capabilities.get`, `node.location.set/get`, `node.status.set`, `node.push_token.set`, `system.capabilities`, `system.nodes`, and canvas artifact RPCs (`canvas.put/get/list/delete/clear`), plus convenience helpers (`bootstrapNode`, optional `autoConnect`, `dispose()`) and event helpers (`subscribeEvents()`, `subscribeEvent()`, `subscribeAgentStream()`, `subscribeAgentTyping()`, `subscribeContextWarning()`, `waitForEvent()` with timeout/predicate/abort support, non-empty event-name validation, and deterministic teardown cancellation (including socket-close rejection), `waitForAnyEvent()` (with non-empty event-name-list validation), `waitForAgentStream()`, `waitForAgentTyping()`, `waitForContextWarning()`, `clearEventSubscriptions()`, `listKnownEventNames()`, `eventSubscriptionCount`) plus in-flight RPC observability via `pendingRequestCount`.
- `src/companion/runtimeClient.ts` provides a typed Node/WebSocket client for companion runtimes (macOS/iOS/Android workers) with wrappers for `node.register`, `node.capabilities.get`, `node.location.set/get`, `node.status.set`, `node.push_token.set`, `system.capabilities`, `system.nodes`, and canvas artifact RPCs (`canvas.put/get/list/delete/clear`), plus convenience helpers (`bootstrapNode`, optional `autoConnect`, `dispose()`) and event helpers (`subscribeEvents()`, `subscribeEvent()`, `subscribeAgentStream()`, `subscribeAgentTyping()`, `subscribeContextWarning()`, `waitForEvent()` with timeout/predicate/abort support, non-empty event-name validation, and deterministic teardown cancellation (including socket-close rejection), `waitForAnyEvent()` (with non-empty event-name-list validation), `waitForAgentStream()`, `waitForAgentTyping()`, `waitForContextWarning()`, `clearEventSubscriptions()`, `listKnownEventNames()`, `eventSubscriptionCount`) plus in-flight observability via `pendingRequestCount` and `pendingEventWaitCount`.
- `src/companion/platformClients.ts` provides platform-focused wrappers:
- `MacOSCompanionClient` (`platform: "macos"`, APNs push registration)
- `IOSCompanionClient` (`platform: "ios"`, APNs push registration)
@@ -1201,7 +1201,7 @@ Companion runtime helper:
- optional `defaultSessionId` for canvas helper calls so `sessionId` can be omitted per call
- lifecycle passthroughs for connection state/teardown (`connected`, `dispose(code?, reason?)`)
- stream passthrough helpers (`subscribeEvents`, `subscribeEvent`, `clearEventSubscriptions`, `listKnownEventNames`, `eventSubscriptionCount`, `subscribeAgentStream/Typing/ContextWarning`, `waitForEvent`, `waitForAnyEvent`, `waitForAgentStream/Typing/ContextWarning`)
- runtime observability passthroughs (`pendingRequestCount`, `connected`)
- runtime observability passthroughs (`pendingRequestCount`, `pendingEventWaitCount`, `connected`)
- `src/companion/heartbeatLoop.ts` provides `CompanionHeartbeatLoop` for periodic heartbeat scheduling (`publishHeartbeat`) with start/stop safety, optional interval jitter (`jitterRatio`) to spread load, `tickNow()` for manual sends, success/error hooks, loop observability (`successCount`, `lastSuccessAt`, `failureCount`, `lastFailure`, `getState()`), and optional auto-stop after repeated failures.
## Canvas / A2UI Foundation
+15
View File
@@ -858,6 +858,21 @@
],
"test_status": "pnpm test:run src/companion/runtimeClient.test.ts src/companion/platformClients.test.ts src/companion/heartbeatLoop.test.ts src/companion/platformClients.integration.test.ts + pnpm typecheck passing"
},
"companion-runtime-pending-event-wait-observability": {
"status": "completed",
"date": "2026-02-17",
"updated": "2026-02-17",
"summary": "Added runtime `pendingEventWaitCount` observability and platform passthrough getters so companion runtimes can inspect active waiter cardinality for `waitFor*` event flows.",
"files_modified": [
"src/companion/runtimeClient.ts",
"src/companion/runtimeClient.test.ts",
"src/companion/platformClients.ts",
"src/companion/platformClients.test.ts",
"README.md",
"docs/plans/state.json"
],
"test_status": "pnpm test:run src/companion/runtimeClient.test.ts src/companion/platformClients.test.ts src/companion/heartbeatLoop.test.ts src/companion/platformClients.integration.test.ts + pnpm typecheck passing"
},
"browser-tools-activation-clarity": {
"status": "completed",
"date": "2026-02-17",
+13
View File
@@ -39,6 +39,7 @@ function createRuntimeMock(): {
waitForAnyEvent: ReturnType<typeof vi.fn>;
eventSubscriptionCount: number;
pendingRequestCount: number;
pendingEventWaitCount: number;
connected: boolean;
} {
const connect = vi.fn(async () => undefined);
@@ -79,6 +80,7 @@ function createRuntimeMock(): {
const waitForAnyEvent = vi.fn(async () => ({ event: 'agent.stream', data: { token: 'any' } }));
const eventSubscriptionCount = 3;
const pendingRequestCount = 2;
const pendingEventWaitCount = 1;
const connected = true;
const runtime = {
@@ -117,6 +119,9 @@ function createRuntimeMock(): {
get pendingRequestCount() {
return pendingRequestCount;
},
get pendingEventWaitCount() {
return pendingEventWaitCount;
},
get connected() {
return connected;
},
@@ -155,6 +160,7 @@ function createRuntimeMock(): {
waitForAnyEvent,
eventSubscriptionCount,
pendingRequestCount,
pendingEventWaitCount,
connected,
};
}
@@ -307,6 +313,13 @@ describe('platform companion clients', () => {
expect(client.pendingRequestCount).toBe(mock.pendingRequestCount);
});
it('platform pendingEventWaitCount forwards runtime getter value', async () => {
const mock = createRuntimeMock();
const client = new IOSCompanionClient({ runtime: mock.runtime, nodeId: 'ios-node' });
expect(client.pendingEventWaitCount).toBe(mock.pendingEventWaitCount);
});
it('macOS client forwards canvas methods to runtime client', async () => {
const mock = createRuntimeMock();
const client = new MacOSCompanionClient({ runtime: mock.runtime, nodeId: 'mac-node' });
+12
View File
@@ -279,6 +279,10 @@ export class MacOSCompanionClient {
return this.runtime.pendingRequestCount;
}
get pendingEventWaitCount(): number {
return this.runtime.pendingEventWaitCount;
}
waitForAnyEvent<TData = unknown>(
eventNames: readonly (CompanionEventName | string)[],
options?: {
@@ -525,6 +529,10 @@ export class IOSCompanionClient {
return this.runtime.pendingRequestCount;
}
get pendingEventWaitCount(): number {
return this.runtime.pendingEventWaitCount;
}
waitForAnyEvent<TData = unknown>(
eventNames: readonly (CompanionEventName | string)[],
options?: {
@@ -769,6 +777,10 @@ export class AndroidCompanionClient {
return this.runtime.pendingRequestCount;
}
get pendingEventWaitCount(): number {
return this.runtime.pendingEventWaitCount;
}
waitForAnyEvent<TData = unknown>(
eventNames: readonly (CompanionEventName | string)[],
options?: {
+3
View File
@@ -305,11 +305,13 @@ describe('CompanionRuntimeClient', () => {
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',
});
expect(client.pendingEventWaitCount).toBe(0);
const awaited = client.waitForEvent<{ seq: number }>('agent.stream', {
timeoutMs: 2000,
predicate: (data) => data.seq === 2,
});
expect(client.pendingEventWaitCount).toBe(1);
(client as unknown as { handleMessage: (raw: string) => void }).handleMessage(
JSON.stringify({
@@ -327,6 +329,7 @@ describe('CompanionRuntimeClient', () => {
);
await expect(awaited).resolves.toEqual({ seq: 2 });
expect(client.pendingEventWaitCount).toBe(0);
});
it('waitForEvent rejects on timeout', async () => {
+4
View File
@@ -306,6 +306,10 @@ export class CompanionRuntimeClient {
return this.pending.size;
}
get pendingEventWaitCount(): number {
return this.pendingEventWaits.size;
}
async connect(): Promise<void> {
if (this.connected) {
return;