feat(companion): add hasPendingWork runtime observability

This commit is contained in:
William Valentin
2026-02-16 20:54:23 -08:00
parent 7e556c5815
commit d14f82cd84
6 changed files with 52 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 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
+15
View File
@@ -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",
+13
View File
@@ -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' });
+12
View File
@@ -283,6 +283,10 @@ export class MacOSCompanionClient {
return this.runtime.pendingEventWaitCount;
}
get hasPendingWork(): boolean {
return this.runtime.hasPendingWork;
}
waitForAnyEvent<TData = unknown>(
eventNames: readonly (CompanionEventName | string)[],
options?: {
@@ -533,6 +537,10 @@ export class IOSCompanionClient {
return this.runtime.pendingEventWaitCount;
}
get hasPendingWork(): boolean {
return this.runtime.hasPendingWork;
}
waitForAnyEvent<TData = unknown>(
eventNames: readonly (CompanionEventName | string)[],
options?: {
@@ -781,6 +789,10 @@ export class AndroidCompanionClient {
return this.runtime.pendingEventWaitCount;
}
get hasPendingWork(): boolean {
return this.runtime.hasPendingWork;
}
waitForAnyEvent<TData = unknown>(
eventNames: readonly (CompanionEventName | string)[],
options?: {
+6
View File
@@ -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 () => {
+4
View File
@@ -310,6 +310,10 @@ export class CompanionRuntimeClient {
return this.pendingEventWaits.size;
}
get hasPendingWork(): boolean {
return this.pendingRequestCount > 0 || this.pendingEventWaitCount > 0;
}
async connect(): Promise<void> {
if (this.connected) {
return;