feat(companion): add idle observability alias

This commit is contained in:
William Valentin
2026-02-16 22:00:08 -08:00
parent 699f848fc5
commit 45ea084cf0
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. - `system.capabilities` returns gateway protocol and node policy snapshot.
Companion runtime helper: 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, 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`, `hasPendingWork`, 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, 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`, `hasPendingWork`, `idle`, and `getPendingWorkSnapshot()`.
- `src/companion/platformClients.ts` provides platform-focused wrappers: - `src/companion/platformClients.ts` provides platform-focused wrappers:
- `MacOSCompanionClient` (`platform: "macos"`, APNs push registration) - `MacOSCompanionClient` (`platform: "macos"`, APNs push registration)
- `IOSCompanionClient` (`platform: "ios"`, 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 - optional `defaultSessionId` for canvas helper calls so `sessionId` can be omitted per call
- lifecycle passthroughs for connection state/teardown (`connected`, `dispose(code?, reason?)`) - 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`) - stream passthrough helpers (`subscribeEvents`, `subscribeEvent`, `clearEventSubscriptions`, `listKnownEventNames`, `eventSubscriptionCount`, `subscribeAgentStream/Typing/ContextWarning`, `waitForEvent`, `waitForAnyEvent`, `waitForAgentStream/Typing/ContextWarning`)
- runtime observability/control passthroughs (`pendingRequestCount`, `pendingEventWaitCount`, `hasPendingWork`, `getPendingWorkSnapshot()`, `connected`, `waitForIdle()`) - runtime observability/control passthroughs (`pendingRequestCount`, `pendingEventWaitCount`, `hasPendingWork`, `idle`, `getPendingWorkSnapshot()`, `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. - `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 ## Canvas / A2UI Foundation
+15
View File
@@ -1012,6 +1012,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" "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-idle-alias-observability": {
"status": "completed",
"date": "2026-02-17",
"updated": "2026-02-17",
"summary": "Added runtime `idle` convenience alias (`!hasPendingWork`) with platform passthrough getters for clearer readiness checks in companion app code.",
"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": { "browser-tools-activation-clarity": {
"status": "completed", "status": "completed",
"date": "2026-02-17", "date": "2026-02-17",
+13
View File
@@ -43,6 +43,7 @@ function createRuntimeMock(): {
pendingRequestCount: number; pendingRequestCount: number;
pendingEventWaitCount: number; pendingEventWaitCount: number;
hasPendingWork: boolean; hasPendingWork: boolean;
idle: boolean;
connected: boolean; connected: boolean;
} { } {
const connect = vi.fn(async () => undefined); const connect = vi.fn(async () => undefined);
@@ -91,6 +92,7 @@ function createRuntimeMock(): {
const pendingRequestCount = 2; const pendingRequestCount = 2;
const pendingEventWaitCount = 1; const pendingEventWaitCount = 1;
const hasPendingWork = true; const hasPendingWork = true;
const idle = false;
const connected = true; const connected = true;
const runtime = { const runtime = {
@@ -137,6 +139,9 @@ function createRuntimeMock(): {
get hasPendingWork() { get hasPendingWork() {
return hasPendingWork; return hasPendingWork;
}, },
get idle() {
return idle;
},
get connected() { get connected() {
return connected; return connected;
}, },
@@ -179,6 +184,7 @@ function createRuntimeMock(): {
pendingRequestCount, pendingRequestCount,
pendingEventWaitCount, pendingEventWaitCount,
hasPendingWork, hasPendingWork,
idle,
connected, connected,
}; };
} }
@@ -345,6 +351,13 @@ describe('platform companion clients', () => {
expect(client.hasPendingWork).toBe(mock.hasPendingWork); expect(client.hasPendingWork).toBe(mock.hasPendingWork);
}); });
it('platform idle forwards runtime getter value', async () => {
const mock = createRuntimeMock();
const client = new IOSCompanionClient({ runtime: mock.runtime, nodeId: 'ios-node' });
expect(client.idle).toBe(mock.idle);
});
it('platform waitForIdle forwards options to runtime client', async () => { it('platform waitForIdle forwards options to runtime client', async () => {
const mock = createRuntimeMock(); const mock = createRuntimeMock();
const client = new IOSCompanionClient({ runtime: mock.runtime, nodeId: 'ios-node' }); const client = new IOSCompanionClient({ runtime: mock.runtime, nodeId: 'ios-node' });
+12
View File
@@ -289,6 +289,10 @@ export class MacOSCompanionClient {
return this.runtime.hasPendingWork; return this.runtime.hasPendingWork;
} }
get idle(): boolean {
return this.runtime.idle;
}
getPendingWorkSnapshot(): PendingWorkSnapshot { getPendingWorkSnapshot(): PendingWorkSnapshot {
return this.runtime.getPendingWorkSnapshot(); return this.runtime.getPendingWorkSnapshot();
} }
@@ -551,6 +555,10 @@ export class IOSCompanionClient {
return this.runtime.hasPendingWork; return this.runtime.hasPendingWork;
} }
get idle(): boolean {
return this.runtime.idle;
}
getPendingWorkSnapshot(): PendingWorkSnapshot { getPendingWorkSnapshot(): PendingWorkSnapshot {
return this.runtime.getPendingWorkSnapshot(); return this.runtime.getPendingWorkSnapshot();
} }
@@ -811,6 +819,10 @@ export class AndroidCompanionClient {
return this.runtime.hasPendingWork; return this.runtime.hasPendingWork;
} }
get idle(): boolean {
return this.runtime.idle;
}
getPendingWorkSnapshot(): PendingWorkSnapshot { getPendingWorkSnapshot(): PendingWorkSnapshot {
return this.runtime.getPendingWorkSnapshot(); return this.runtime.getPendingWorkSnapshot();
} }
+3
View File
@@ -632,15 +632,18 @@ describe('CompanionRuntimeClient', () => {
await client.connect(); await client.connect();
expect(client.pendingRequestCount).toBe(0); expect(client.pendingRequestCount).toBe(0);
expect(client.hasPendingWork).toBe(false); expect(client.hasPendingWork).toBe(false);
expect(client.idle).toBe(true);
const pending = client.call('system.capabilities'); const pending = client.call('system.capabilities');
expect(client.pendingRequestCount).toBe(1); expect(client.pendingRequestCount).toBe(1);
expect(client.hasPendingWork).toBe(true); expect(client.hasPendingWork).toBe(true);
expect(client.idle).toBe(false);
client.disconnect(); client.disconnect();
await expect(pending).rejects.toThrow('Disconnected'); await expect(pending).rejects.toThrow('Disconnected');
expect(client.pendingRequestCount).toBe(0); expect(client.pendingRequestCount).toBe(0);
expect(client.hasPendingWork).toBe(false); expect(client.hasPendingWork).toBe(false);
expect(client.idle).toBe(true);
}); });
it('returns pending work snapshot', async () => { it('returns pending work snapshot', async () => {
+4
View File
@@ -326,6 +326,10 @@ export class CompanionRuntimeClient {
return this.pendingRequestCount > 0 || this.pendingEventWaitCount > 0; return this.pendingRequestCount > 0 || this.pendingEventWaitCount > 0;
} }
get idle(): boolean {
return !this.hasPendingWork;
}
getPendingWorkSnapshot(): PendingWorkSnapshot { getPendingWorkSnapshot(): PendingWorkSnapshot {
return { return {
pendingRequestCount: this.pendingRequestCount, pendingRequestCount: this.pendingRequestCount,