feat(companion): expose pending request observability
This commit is contained in:
@@ -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()`) 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`).
|
- `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/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,6 +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 passthroughs (`pendingRequestCount`, `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.
|
- `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
|
## Canvas / A2UI Foundation
|
||||||
|
|||||||
@@ -843,6 +843,21 @@
|
|||||||
],
|
],
|
||||||
"test_status": "pnpm test:run src/companion/heartbeatLoop.test.ts src/companion/platformClients.test.ts src/companion/runtimeClient.test.ts src/companion/platformClients.integration.test.ts + pnpm typecheck passing"
|
"test_status": "pnpm test:run src/companion/heartbeatLoop.test.ts src/companion/platformClients.test.ts src/companion/runtimeClient.test.ts src/companion/platformClients.integration.test.ts + pnpm typecheck passing"
|
||||||
},
|
},
|
||||||
|
"companion-runtime-pending-request-observability": {
|
||||||
|
"status": "completed",
|
||||||
|
"date": "2026-02-17",
|
||||||
|
"updated": "2026-02-17",
|
||||||
|
"summary": "Added runtime `pendingRequestCount` observability and platform passthrough getters so companion runtimes can inspect in-flight RPC request cardinality.",
|
||||||
|
"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",
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ function createRuntimeMock(): {
|
|||||||
waitForContextWarning: ReturnType<typeof vi.fn>;
|
waitForContextWarning: ReturnType<typeof vi.fn>;
|
||||||
waitForAnyEvent: ReturnType<typeof vi.fn>;
|
waitForAnyEvent: ReturnType<typeof vi.fn>;
|
||||||
eventSubscriptionCount: number;
|
eventSubscriptionCount: number;
|
||||||
|
pendingRequestCount: number;
|
||||||
connected: boolean;
|
connected: boolean;
|
||||||
} {
|
} {
|
||||||
const connect = vi.fn(async () => undefined);
|
const connect = vi.fn(async () => undefined);
|
||||||
@@ -77,6 +78,7 @@ function createRuntimeMock(): {
|
|||||||
const waitForContextWarning = vi.fn(async () => ({ thresholdPct: 75, estimatedPct: 90 }));
|
const waitForContextWarning = vi.fn(async () => ({ thresholdPct: 75, estimatedPct: 90 }));
|
||||||
const waitForAnyEvent = vi.fn(async () => ({ event: 'agent.stream', data: { token: 'any' } }));
|
const waitForAnyEvent = vi.fn(async () => ({ event: 'agent.stream', data: { token: 'any' } }));
|
||||||
const eventSubscriptionCount = 3;
|
const eventSubscriptionCount = 3;
|
||||||
|
const pendingRequestCount = 2;
|
||||||
const connected = true;
|
const connected = true;
|
||||||
|
|
||||||
const runtime = {
|
const runtime = {
|
||||||
@@ -112,6 +114,9 @@ function createRuntimeMock(): {
|
|||||||
get eventSubscriptionCount() {
|
get eventSubscriptionCount() {
|
||||||
return eventSubscriptionCount;
|
return eventSubscriptionCount;
|
||||||
},
|
},
|
||||||
|
get pendingRequestCount() {
|
||||||
|
return pendingRequestCount;
|
||||||
|
},
|
||||||
get connected() {
|
get connected() {
|
||||||
return connected;
|
return connected;
|
||||||
},
|
},
|
||||||
@@ -149,6 +154,7 @@ function createRuntimeMock(): {
|
|||||||
waitForContextWarning,
|
waitForContextWarning,
|
||||||
waitForAnyEvent,
|
waitForAnyEvent,
|
||||||
eventSubscriptionCount,
|
eventSubscriptionCount,
|
||||||
|
pendingRequestCount,
|
||||||
connected,
|
connected,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -294,6 +300,13 @@ describe('platform companion clients', () => {
|
|||||||
expect(client.eventSubscriptionCount).toBe(mock.eventSubscriptionCount);
|
expect(client.eventSubscriptionCount).toBe(mock.eventSubscriptionCount);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('platform pendingRequestCount forwards runtime getter value', async () => {
|
||||||
|
const mock = createRuntimeMock();
|
||||||
|
const client = new IOSCompanionClient({ runtime: mock.runtime, nodeId: 'ios-node' });
|
||||||
|
|
||||||
|
expect(client.pendingRequestCount).toBe(mock.pendingRequestCount);
|
||||||
|
});
|
||||||
|
|
||||||
it('macOS client forwards canvas methods to runtime client', async () => {
|
it('macOS client forwards canvas methods to runtime client', async () => {
|
||||||
const mock = createRuntimeMock();
|
const mock = createRuntimeMock();
|
||||||
const client = new MacOSCompanionClient({ runtime: mock.runtime, nodeId: 'mac-node' });
|
const client = new MacOSCompanionClient({ runtime: mock.runtime, nodeId: 'mac-node' });
|
||||||
|
|||||||
@@ -275,6 +275,10 @@ export class MacOSCompanionClient {
|
|||||||
return this.runtime.eventSubscriptionCount;
|
return this.runtime.eventSubscriptionCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get pendingRequestCount(): number {
|
||||||
|
return this.runtime.pendingRequestCount;
|
||||||
|
}
|
||||||
|
|
||||||
waitForAnyEvent<TData = unknown>(
|
waitForAnyEvent<TData = unknown>(
|
||||||
eventNames: readonly (CompanionEventName | string)[],
|
eventNames: readonly (CompanionEventName | string)[],
|
||||||
options?: {
|
options?: {
|
||||||
@@ -517,6 +521,10 @@ export class IOSCompanionClient {
|
|||||||
return this.runtime.eventSubscriptionCount;
|
return this.runtime.eventSubscriptionCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get pendingRequestCount(): number {
|
||||||
|
return this.runtime.pendingRequestCount;
|
||||||
|
}
|
||||||
|
|
||||||
waitForAnyEvent<TData = unknown>(
|
waitForAnyEvent<TData = unknown>(
|
||||||
eventNames: readonly (CompanionEventName | string)[],
|
eventNames: readonly (CompanionEventName | string)[],
|
||||||
options?: {
|
options?: {
|
||||||
@@ -757,6 +765,10 @@ export class AndroidCompanionClient {
|
|||||||
return this.runtime.eventSubscriptionCount;
|
return this.runtime.eventSubscriptionCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get pendingRequestCount(): number {
|
||||||
|
return this.runtime.pendingRequestCount;
|
||||||
|
}
|
||||||
|
|
||||||
waitForAnyEvent<TData = unknown>(
|
waitForAnyEvent<TData = unknown>(
|
||||||
eventNames: readonly (CompanionEventName | string)[],
|
eventNames: readonly (CompanionEventName | string)[],
|
||||||
options?: {
|
options?: {
|
||||||
|
|||||||
@@ -593,6 +593,44 @@ describe('CompanionRuntimeClient', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('tracks pendingRequestCount for in-flight RPCs', async () => {
|
||||||
|
class FakeWebSocket extends EventEmitter {
|
||||||
|
readyState: number = WebSocket.CONNECTING;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
queueMicrotask(() => {
|
||||||
|
this.readyState = WebSocket.OPEN;
|
||||||
|
this.emit('open');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
send(_payload: string, callback?: (error?: Error) => void): void {
|
||||||
|
callback?.();
|
||||||
|
}
|
||||||
|
|
||||||
|
close(_code?: number, _reason?: string): void {
|
||||||
|
this.readyState = WebSocket.CLOSED;
|
||||||
|
this.emit('close');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const client = new CompanionRuntimeClient({
|
||||||
|
url: 'ws://127.0.0.1:1',
|
||||||
|
websocketFactory: () => new FakeWebSocket() as unknown as WebSocket,
|
||||||
|
requestTimeoutMs: 10_000,
|
||||||
|
});
|
||||||
|
await client.connect();
|
||||||
|
expect(client.pendingRequestCount).toBe(0);
|
||||||
|
|
||||||
|
const pending = client.call('system.capabilities');
|
||||||
|
expect(client.pendingRequestCount).toBe(1);
|
||||||
|
|
||||||
|
client.disconnect();
|
||||||
|
await expect(pending).rejects.toThrow('Disconnected');
|
||||||
|
expect(client.pendingRequestCount).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
it('connects and performs node registration + capability discovery', async () => {
|
it('connects and performs node registration + capability discovery', async () => {
|
||||||
if (!LISTEN_ALLOWED) {
|
if (!LISTEN_ALLOWED) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -302,6 +302,10 @@ export class CompanionRuntimeClient {
|
|||||||
return this.eventHandlers.size;
|
return this.eventHandlers.size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get pendingRequestCount(): number {
|
||||||
|
return this.pending.size;
|
||||||
|
}
|
||||||
|
|
||||||
async connect(): Promise<void> {
|
async connect(): Promise<void> {
|
||||||
if (this.connected) {
|
if (this.connected) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user