feat(companion): add event surface snapshot helper
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()`, `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:
|
- `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`, `cancelPendingEventWaits`, `listKnownEventNames`, `eventSubscriptionCount`, `subscribeAgentStream/Typing/ContextWarning`, `waitForEvent`, `waitForAnyEvent`, `waitForAgentStream/Typing/ContextWarning`)
|
- 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.
|
- `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
|
||||||
|
|||||||
@@ -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"
|
"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": {
|
"companion-runtime-cancel-pending-event-waits": {
|
||||||
"status": "completed",
|
"status": "completed",
|
||||||
"date": "2026-02-17",
|
"date": "2026-02-17",
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ export type {
|
|||||||
CompanionRuntimeClientOptions,
|
CompanionRuntimeClientOptions,
|
||||||
WaitForIdleOptions,
|
WaitForIdleOptions,
|
||||||
PendingWorkSnapshot,
|
PendingWorkSnapshot,
|
||||||
|
EventSurfaceSnapshot,
|
||||||
CompanionEventHandler,
|
CompanionEventHandler,
|
||||||
CompanionTypedEventHandler,
|
CompanionTypedEventHandler,
|
||||||
CompanionEventName,
|
CompanionEventName,
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ function createRuntimeMock(): {
|
|||||||
waitForEvent: ReturnType<typeof vi.fn>;
|
waitForEvent: ReturnType<typeof vi.fn>;
|
||||||
waitForIdle: ReturnType<typeof vi.fn>;
|
waitForIdle: ReturnType<typeof vi.fn>;
|
||||||
getPendingWorkSnapshot: ReturnType<typeof vi.fn>;
|
getPendingWorkSnapshot: ReturnType<typeof vi.fn>;
|
||||||
|
getEventSurfaceSnapshot: ReturnType<typeof vi.fn>;
|
||||||
waitForAgentStream: ReturnType<typeof vi.fn>;
|
waitForAgentStream: ReturnType<typeof vi.fn>;
|
||||||
waitForAgentTyping: ReturnType<typeof vi.fn>;
|
waitForAgentTyping: ReturnType<typeof vi.fn>;
|
||||||
waitForContextWarning: ReturnType<typeof vi.fn>;
|
waitForContextWarning: ReturnType<typeof vi.fn>;
|
||||||
@@ -86,6 +87,11 @@ function createRuntimeMock(): {
|
|||||||
pendingEventWaitCount: 1,
|
pendingEventWaitCount: 1,
|
||||||
hasPendingWork: true,
|
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 waitForAgentStream = vi.fn(async () => ({ token: 'streamed' }));
|
||||||
const waitForAgentTyping = vi.fn(async () => ({ active: true }));
|
const waitForAgentTyping = vi.fn(async () => ({ active: true }));
|
||||||
const waitForContextWarning = vi.fn(async () => ({ thresholdPct: 75, estimatedPct: 90 }));
|
const waitForContextWarning = vi.fn(async () => ({ thresholdPct: 75, estimatedPct: 90 }));
|
||||||
@@ -126,6 +132,7 @@ function createRuntimeMock(): {
|
|||||||
waitForEvent,
|
waitForEvent,
|
||||||
waitForIdle,
|
waitForIdle,
|
||||||
getPendingWorkSnapshot,
|
getPendingWorkSnapshot,
|
||||||
|
getEventSurfaceSnapshot,
|
||||||
waitForAgentStream,
|
waitForAgentStream,
|
||||||
waitForAgentTyping,
|
waitForAgentTyping,
|
||||||
waitForContextWarning,
|
waitForContextWarning,
|
||||||
@@ -180,6 +187,7 @@ function createRuntimeMock(): {
|
|||||||
waitForEvent,
|
waitForEvent,
|
||||||
waitForIdle,
|
waitForIdle,
|
||||||
getPendingWorkSnapshot,
|
getPendingWorkSnapshot,
|
||||||
|
getEventSurfaceSnapshot,
|
||||||
waitForAgentStream,
|
waitForAgentStream,
|
||||||
waitForAgentTyping,
|
waitForAgentTyping,
|
||||||
waitForContextWarning,
|
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 () => {
|
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' });
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import type {
|
import type {
|
||||||
CompanionEventName,
|
CompanionEventName,
|
||||||
CompanionEventEnvelope,
|
CompanionEventEnvelope,
|
||||||
|
EventSurfaceSnapshot,
|
||||||
CompanionEventHandler,
|
CompanionEventHandler,
|
||||||
CanvasClearResult,
|
CanvasClearResult,
|
||||||
CanvasDeleteResult,
|
CanvasDeleteResult,
|
||||||
@@ -301,6 +302,10 @@ export class MacOSCompanionClient {
|
|||||||
return this.runtime.getPendingWorkSnapshot();
|
return this.runtime.getPendingWorkSnapshot();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getEventSurfaceSnapshot(): EventSurfaceSnapshot {
|
||||||
|
return this.runtime.getEventSurfaceSnapshot();
|
||||||
|
}
|
||||||
|
|
||||||
waitForAnyEvent<TData = unknown>(
|
waitForAnyEvent<TData = unknown>(
|
||||||
eventNames: readonly (CompanionEventName | string)[],
|
eventNames: readonly (CompanionEventName | string)[],
|
||||||
options?: {
|
options?: {
|
||||||
@@ -571,6 +576,10 @@ export class IOSCompanionClient {
|
|||||||
return this.runtime.getPendingWorkSnapshot();
|
return this.runtime.getPendingWorkSnapshot();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getEventSurfaceSnapshot(): EventSurfaceSnapshot {
|
||||||
|
return this.runtime.getEventSurfaceSnapshot();
|
||||||
|
}
|
||||||
|
|
||||||
waitForAnyEvent<TData = unknown>(
|
waitForAnyEvent<TData = unknown>(
|
||||||
eventNames: readonly (CompanionEventName | string)[],
|
eventNames: readonly (CompanionEventName | string)[],
|
||||||
options?: {
|
options?: {
|
||||||
@@ -839,6 +848,10 @@ export class AndroidCompanionClient {
|
|||||||
return this.runtime.getPendingWorkSnapshot();
|
return this.runtime.getPendingWorkSnapshot();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getEventSurfaceSnapshot(): EventSurfaceSnapshot {
|
||||||
|
return this.runtime.getEventSurfaceSnapshot();
|
||||||
|
}
|
||||||
|
|
||||||
waitForAnyEvent<TData = unknown>(
|
waitForAnyEvent<TData = unknown>(
|
||||||
eventNames: readonly (CompanionEventName | string)[],
|
eventNames: readonly (CompanionEventName | string)[],
|
||||||
options?: {
|
options?: {
|
||||||
|
|||||||
@@ -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 () => {
|
it('waitForIdle resolves immediately when no work is pending', async () => {
|
||||||
const client = new CompanionRuntimeClient({
|
const client = new CompanionRuntimeClient({
|
||||||
url: 'ws://127.0.0.1:1',
|
url: 'ws://127.0.0.1:1',
|
||||||
|
|||||||
@@ -53,6 +53,12 @@ export interface PendingWorkSnapshot {
|
|||||||
hasPendingWork: boolean;
|
hasPendingWork: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface EventSurfaceSnapshot {
|
||||||
|
knownEventNames: CompanionEventName[];
|
||||||
|
eventSubscriptionCount: number;
|
||||||
|
pendingEventWaitCount: number;
|
||||||
|
}
|
||||||
|
|
||||||
export type CompanionEventHandler = (event: string, data: unknown) => void;
|
export type CompanionEventHandler = (event: string, data: unknown) => void;
|
||||||
export type CompanionTypedEventHandler<TData = unknown> = (data: TData) => void;
|
export type CompanionTypedEventHandler<TData = unknown> = (data: TData) => void;
|
||||||
export type CompanionEventPredicate<TData = unknown> = (data: TData) => boolean;
|
export type CompanionEventPredicate<TData = unknown> = (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<void> {
|
async connect(): Promise<void> {
|
||||||
if (this.connected) {
|
if (this.connected) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user