From d14f82cd847583f2aea3fcb58a89194e9f456683 Mon Sep 17 00:00:00 2001 From: William Valentin Date: Mon, 16 Feb 2026 20:54:23 -0800 Subject: [PATCH] feat(companion): add hasPendingWork runtime observability --- README.md | 4 ++-- docs/plans/state.json | 15 +++++++++++++++ src/companion/platformClients.test.ts | 13 +++++++++++++ src/companion/platformClients.ts | 12 ++++++++++++ src/companion/runtimeClient.test.ts | 6 ++++++ src/companion/runtimeClient.ts | 4 ++++ 6 files changed, 52 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a1109ef..55daad0 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()`) 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/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`, `pendingEventWaitCount`, and `hasPendingWork`. - `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`, `pendingEventWaitCount`, `connected`) + - runtime observability passthroughs (`pendingRequestCount`, `pendingEventWaitCount`, `hasPendingWork`, `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 diff --git a/docs/plans/state.json b/docs/plans/state.json index ad0ff3d..6a2643b 100644 --- a/docs/plans/state.json +++ b/docs/plans/state.json @@ -885,6 +885,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-has-pending-work-observability": { + "status": "completed", + "date": "2026-02-17", + "updated": "2026-02-17", + "summary": "Added runtime `hasPendingWork` convenience getter (derived from pending RPC + event waits) and platform passthrough getters for lightweight busy-state checks.", + "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", diff --git a/src/companion/platformClients.test.ts b/src/companion/platformClients.test.ts index 73c6ac6..3c71bb5 100644 --- a/src/companion/platformClients.test.ts +++ b/src/companion/platformClients.test.ts @@ -40,6 +40,7 @@ function createRuntimeMock(): { eventSubscriptionCount: number; pendingRequestCount: number; pendingEventWaitCount: number; + hasPendingWork: boolean; connected: boolean; } { const connect = vi.fn(async () => undefined); @@ -81,6 +82,7 @@ function createRuntimeMock(): { const eventSubscriptionCount = 3; const pendingRequestCount = 2; const pendingEventWaitCount = 1; + const hasPendingWork = true; const connected = true; const runtime = { @@ -122,6 +124,9 @@ function createRuntimeMock(): { get pendingEventWaitCount() { return pendingEventWaitCount; }, + get hasPendingWork() { + return hasPendingWork; + }, get connected() { return connected; }, @@ -161,6 +166,7 @@ function createRuntimeMock(): { eventSubscriptionCount, pendingRequestCount, pendingEventWaitCount, + hasPendingWork, connected, }; } @@ -320,6 +326,13 @@ describe('platform companion clients', () => { expect(client.pendingEventWaitCount).toBe(mock.pendingEventWaitCount); }); + it('platform hasPendingWork forwards runtime getter value', async () => { + const mock = createRuntimeMock(); + const client = new IOSCompanionClient({ runtime: mock.runtime, nodeId: 'ios-node' }); + + expect(client.hasPendingWork).toBe(mock.hasPendingWork); + }); + 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 a2cfc0d..5738df7 100644 --- a/src/companion/platformClients.ts +++ b/src/companion/platformClients.ts @@ -283,6 +283,10 @@ export class MacOSCompanionClient { return this.runtime.pendingEventWaitCount; } + get hasPendingWork(): boolean { + return this.runtime.hasPendingWork; + } + waitForAnyEvent( eventNames: readonly (CompanionEventName | string)[], options?: { @@ -533,6 +537,10 @@ export class IOSCompanionClient { return this.runtime.pendingEventWaitCount; } + get hasPendingWork(): boolean { + return this.runtime.hasPendingWork; + } + waitForAnyEvent( eventNames: readonly (CompanionEventName | string)[], options?: { @@ -781,6 +789,10 @@ export class AndroidCompanionClient { return this.runtime.pendingEventWaitCount; } + get hasPendingWork(): boolean { + return this.runtime.hasPendingWork; + } + waitForAnyEvent( eventNames: readonly (CompanionEventName | string)[], options?: { diff --git a/src/companion/runtimeClient.test.ts b/src/companion/runtimeClient.test.ts index 3e0be8c..2c8b653 100644 --- a/src/companion/runtimeClient.test.ts +++ b/src/companion/runtimeClient.test.ts @@ -306,12 +306,14 @@ describe('CompanionRuntimeClient', () => { url: 'ws://127.0.0.1:1', }); expect(client.pendingEventWaitCount).toBe(0); + expect(client.hasPendingWork).toBe(false); const awaited = client.waitForEvent<{ seq: number }>('agent.stream', { timeoutMs: 2000, predicate: (data) => data.seq === 2, }); expect(client.pendingEventWaitCount).toBe(1); + expect(client.hasPendingWork).toBe(true); (client as unknown as { handleMessage: (raw: string) => void }).handleMessage( JSON.stringify({ @@ -330,6 +332,7 @@ describe('CompanionRuntimeClient', () => { await expect(awaited).resolves.toEqual({ seq: 2 }); expect(client.pendingEventWaitCount).toBe(0); + expect(client.hasPendingWork).toBe(false); }); it('waitForEvent rejects on timeout', async () => { @@ -628,13 +631,16 @@ describe('CompanionRuntimeClient', () => { }); await client.connect(); expect(client.pendingRequestCount).toBe(0); + expect(client.hasPendingWork).toBe(false); const pending = client.call('system.capabilities'); expect(client.pendingRequestCount).toBe(1); + expect(client.hasPendingWork).toBe(true); client.disconnect(); await expect(pending).rejects.toThrow('Disconnected'); expect(client.pendingRequestCount).toBe(0); + expect(client.hasPendingWork).toBe(false); }); it('connects and performs node registration + capability discovery', async () => { diff --git a/src/companion/runtimeClient.ts b/src/companion/runtimeClient.ts index 8cf1611..c1e6e8b 100644 --- a/src/companion/runtimeClient.ts +++ b/src/companion/runtimeClient.ts @@ -310,6 +310,10 @@ export class CompanionRuntimeClient { return this.pendingEventWaits.size; } + get hasPendingWork(): boolean { + return this.pendingRequestCount > 0 || this.pendingEventWaitCount > 0; + } + async connect(): Promise { if (this.connected) { return;