From 29523bcdac72e711d3f941d2cc4f853a7a358934 Mon Sep 17 00:00:00 2001 From: William Valentin Date: Mon, 16 Feb 2026 22:22:18 -0800 Subject: [PATCH] feat(companion): add event surface snapshot helper --- README.md | 4 ++-- docs/plans/state.json | 16 ++++++++++++++++ src/companion/index.ts | 1 + src/companion/platformClients.test.ts | 22 ++++++++++++++++++++++ src/companion/platformClients.ts | 13 +++++++++++++ src/companion/runtimeClient.test.ts | 18 ++++++++++++++++++ src/companion/runtimeClient.ts | 14 ++++++++++++++ 7 files changed, 86 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 84e3d40..72284ed 100644 --- a/README.md +++ b/README.md @@ -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()`, `waitForIdle()` for pending-work drain synchronization) and event helpers (`subscribeEvents()`, `subscribeEvent()`, `subscribeAgentStream()`, `subscribeAgentTyping()`, `subscribeContextWarning()`, `waitForEvent()` with timeout/predicate/abort support plus event-name/timeout validation and deterministic teardown cancellation including socket-close rejection, `waitForAnyEvent()` with event-list/timeout validation, `waitForAgentStream()`, `waitForAgentTyping()`, `waitForContextWarning()`, `clearEventSubscriptions()`, `cancelPendingEventWaits()` returning cancelled waiter count, `listKnownEventNames()`, `eventSubscriptionCount`) plus in-flight observability via `pendingRequestCount`, `pendingEventWaitCount`, `hasPendingWork`, `idle`, and `getPendingWorkSnapshot()`. +- `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()`, `waitForIdle()` for pending-work drain synchronization) and event helpers (`subscribeEvents()`, `subscribeEvent()`, `subscribeAgentStream()`, `subscribeAgentTyping()`, `subscribeContextWarning()`, `waitForEvent()` with timeout/predicate/abort support plus event-name/timeout validation and deterministic teardown cancellation including socket-close rejection, `waitForAnyEvent()` with event-list/timeout validation, `waitForAgentStream()`, `waitForAgentTyping()`, `waitForContextWarning()`, `clearEventSubscriptions()`, `cancelPendingEventWaits()` returning cancelled waiter count, `listKnownEventNames()`, `eventSubscriptionCount`) plus in-flight observability via `pendingRequestCount`, `pendingEventWaitCount`, `hasPendingWork`, `idle`, `getPendingWorkSnapshot()`, and `getEventSurfaceSnapshot()`. - `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`, `cancelPendingEventWaits`, `listKnownEventNames`, `eventSubscriptionCount`, `subscribeAgentStream/Typing/ContextWarning`, `waitForEvent`, `waitForAnyEvent`, `waitForAgentStream/Typing/ContextWarning`) - - runtime observability/control passthroughs (`pendingRequestCount`, `pendingEventWaitCount`, `hasPendingWork`, `idle`, `getPendingWorkSnapshot()`, `connected`, `waitForIdle()`) + - runtime observability/control passthroughs (`pendingRequestCount`, `pendingEventWaitCount`, `hasPendingWork`, `idle`, `getPendingWorkSnapshot()`, `getEventSurfaceSnapshot()`, `connected`, `waitForIdle()`) - `src/companion/heartbeatLoop.ts` provides `CompanionHeartbeatLoop` for periodic heartbeat scheduling (`publishHeartbeat`) with start/stop safety, optional interval jitter (`jitterRatio`) to spread load (with safe normalization for invalid random samples), `tickNow()` for manual sends, success/error hooks, loop observability (`successCount`, `lastSuccessAt`, `failureCount`, `lastFailure`, `getState()`), and optional auto-stop after repeated failures. ## Canvas / A2UI Foundation diff --git a/docs/plans/state.json b/docs/plans/state.json index 5f9af61..904e87a 100644 --- a/docs/plans/state.json +++ b/docs/plans/state.json @@ -1040,6 +1040,22 @@ ], "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-event-surface-snapshot-helper": { + "status": "completed", + "date": "2026-02-17", + "updated": "2026-02-17", + "summary": "Added `getEventSurfaceSnapshot()` on runtime and platform wrappers for one-call event-surface inspection (`knownEventNames`, `eventSubscriptionCount`, `pendingEventWaitCount`).", + "files_modified": [ + "src/companion/runtimeClient.ts", + "src/companion/runtimeClient.test.ts", + "src/companion/platformClients.ts", + "src/companion/platformClients.test.ts", + "src/companion/index.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" + }, "companion-runtime-cancel-pending-event-waits": { "status": "completed", "date": "2026-02-17", diff --git a/src/companion/index.ts b/src/companion/index.ts index bddf374..749c47e 100644 --- a/src/companion/index.ts +++ b/src/companion/index.ts @@ -14,6 +14,7 @@ export type { CompanionRuntimeClientOptions, WaitForIdleOptions, PendingWorkSnapshot, + EventSurfaceSnapshot, CompanionEventHandler, CompanionTypedEventHandler, CompanionEventName, diff --git a/src/companion/platformClients.test.ts b/src/companion/platformClients.test.ts index dc17b95..74861e0 100644 --- a/src/companion/platformClients.test.ts +++ b/src/companion/platformClients.test.ts @@ -36,6 +36,7 @@ function createRuntimeMock(): { waitForEvent: ReturnType; waitForIdle: ReturnType; getPendingWorkSnapshot: ReturnType; + getEventSurfaceSnapshot: ReturnType; waitForAgentStream: ReturnType; waitForAgentTyping: ReturnType; waitForContextWarning: ReturnType; @@ -86,6 +87,11 @@ function createRuntimeMock(): { pendingEventWaitCount: 1, hasPendingWork: true, })); + const getEventSurfaceSnapshot = vi.fn(() => ({ + knownEventNames: ['agent.stream', 'agent.typing', 'context_warning'], + eventSubscriptionCount: 3, + pendingEventWaitCount: 1, + })); const waitForAgentStream = vi.fn(async () => ({ token: 'streamed' })); const waitForAgentTyping = vi.fn(async () => ({ active: true })); const waitForContextWarning = vi.fn(async () => ({ thresholdPct: 75, estimatedPct: 90 })); @@ -126,6 +132,7 @@ function createRuntimeMock(): { waitForEvent, waitForIdle, getPendingWorkSnapshot, + getEventSurfaceSnapshot, waitForAgentStream, waitForAgentTyping, waitForContextWarning, @@ -180,6 +187,7 @@ function createRuntimeMock(): { waitForEvent, waitForIdle, getPendingWorkSnapshot, + getEventSurfaceSnapshot, waitForAgentStream, waitForAgentTyping, waitForContextWarning, @@ -395,6 +403,20 @@ describe('platform companion clients', () => { }); }); + it('platform getEventSurfaceSnapshot forwards to runtime client', async () => { + const mock = createRuntimeMock(); + const client = new IOSCompanionClient({ runtime: mock.runtime, nodeId: 'ios-node' }); + + const snapshot = client.getEventSurfaceSnapshot(); + + expect(mock.getEventSurfaceSnapshot).toHaveBeenCalledOnce(); + expect(snapshot).toEqual({ + knownEventNames: ['agent.stream', 'agent.typing', 'context_warning'], + eventSubscriptionCount: 3, + pendingEventWaitCount: 1, + }); + }); + it('macOS client forwards canvas methods to runtime client', async () => { const mock = createRuntimeMock(); const client = new MacOSCompanionClient({ runtime: mock.runtime, nodeId: 'mac-node' }); diff --git a/src/companion/platformClients.ts b/src/companion/platformClients.ts index a9e04e2..19d3f78 100644 --- a/src/companion/platformClients.ts +++ b/src/companion/platformClients.ts @@ -1,6 +1,7 @@ import type { CompanionEventName, CompanionEventEnvelope, + EventSurfaceSnapshot, CompanionEventHandler, CanvasClearResult, CanvasDeleteResult, @@ -301,6 +302,10 @@ export class MacOSCompanionClient { return this.runtime.getPendingWorkSnapshot(); } + getEventSurfaceSnapshot(): EventSurfaceSnapshot { + return this.runtime.getEventSurfaceSnapshot(); + } + waitForAnyEvent( eventNames: readonly (CompanionEventName | string)[], options?: { @@ -571,6 +576,10 @@ export class IOSCompanionClient { return this.runtime.getPendingWorkSnapshot(); } + getEventSurfaceSnapshot(): EventSurfaceSnapshot { + return this.runtime.getEventSurfaceSnapshot(); + } + waitForAnyEvent( eventNames: readonly (CompanionEventName | string)[], options?: { @@ -839,6 +848,10 @@ export class AndroidCompanionClient { return this.runtime.getPendingWorkSnapshot(); } + getEventSurfaceSnapshot(): EventSurfaceSnapshot { + return this.runtime.getEventSurfaceSnapshot(); + } + waitForAnyEvent( eventNames: readonly (CompanionEventName | string)[], options?: { diff --git a/src/companion/runtimeClient.test.ts b/src/companion/runtimeClient.test.ts index 9d4b7d3..f4cfe50 100644 --- a/src/companion/runtimeClient.test.ts +++ b/src/companion/runtimeClient.test.ts @@ -703,6 +703,24 @@ describe('CompanionRuntimeClient', () => { }); }); + it('returns event surface snapshot', async () => { + const client = new CompanionRuntimeClient({ + url: 'ws://127.0.0.1:1', + }); + const pendingWait = client.waitForEvent('agent.stream', { timeoutMs: 10_000 }).catch(() => undefined); + const unsubscribe = client.subscribeEvents(() => undefined); + + expect(client.getEventSurfaceSnapshot()).toEqual({ + knownEventNames: ['agent.stream', 'agent.typing', 'context_warning'], + eventSubscriptionCount: 2, + pendingEventWaitCount: 1, + }); + + unsubscribe(); + client.clearEventSubscriptions(); + await pendingWait; + }); + it('waitForIdle resolves immediately when no work is pending', async () => { const client = new CompanionRuntimeClient({ url: 'ws://127.0.0.1:1', diff --git a/src/companion/runtimeClient.ts b/src/companion/runtimeClient.ts index 5a4faee..668dc8c 100644 --- a/src/companion/runtimeClient.ts +++ b/src/companion/runtimeClient.ts @@ -53,6 +53,12 @@ export interface PendingWorkSnapshot { hasPendingWork: boolean; } +export interface EventSurfaceSnapshot { + knownEventNames: CompanionEventName[]; + eventSubscriptionCount: number; + pendingEventWaitCount: number; +} + export type CompanionEventHandler = (event: string, data: unknown) => void; export type CompanionTypedEventHandler = (data: TData) => void; export type CompanionEventPredicate = (data: TData) => boolean; @@ -338,6 +344,14 @@ export class CompanionRuntimeClient { }; } + getEventSurfaceSnapshot(): EventSurfaceSnapshot { + return { + knownEventNames: this.listKnownEventNames(), + eventSubscriptionCount: this.eventSubscriptionCount, + pendingEventWaitCount: this.pendingEventWaitCount, + }; + } + async connect(): Promise { if (this.connected) { return;