diff --git a/README.md b/README.md index d6706cf..aaf3240 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()`, `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()`, `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()`, `listKnownEventNames()`, `eventSubscriptionCount`) plus in-flight observability via `pendingRequestCount`, `pendingEventWaitCount`, `hasPendingWork`, `idle`, and `getPendingWorkSnapshot()`. - `src/companion/platformClients.ts` provides platform-focused wrappers: - `MacOSCompanionClient` (`platform: "macos"`, APNs push registration) - `IOSCompanionClient` (`platform: "ios"`, APNs push registration) @@ -1200,7 +1200,7 @@ Companion runtime helper: - `createHeartbeatLoop()` convenience helper that returns a bound `CompanionHeartbeatLoop` - 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`) + - 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()`) - `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. diff --git a/docs/plans/state.json b/docs/plans/state.json index 8f9460c..1c8b6bb 100644 --- a/docs/plans/state.json +++ b/docs/plans/state.json @@ -1040,6 +1040,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-cancel-pending-event-waits": { + "status": "completed", + "date": "2026-02-17", + "updated": "2026-02-17", + "summary": "Added `cancelPendingEventWaits()` on runtime and platform wrappers so pending waiters can be cancelled without clearing active event subscriptions.", + "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 6e5e4cd..2b3a459 100644 --- a/src/companion/platformClients.test.ts +++ b/src/companion/platformClients.test.ts @@ -31,6 +31,7 @@ function createRuntimeMock(): { subscribeEvents: ReturnType; subscribeEvent: ReturnType; clearEventSubscriptions: ReturnType; + cancelPendingEventWaits: ReturnType; listKnownEventNames: ReturnType; waitForEvent: ReturnType; waitForIdle: ReturnType; @@ -76,6 +77,7 @@ function createRuntimeMock(): { const subscribeEvents = vi.fn(() => () => undefined); const subscribeEvent = vi.fn(() => () => undefined); const clearEventSubscriptions = vi.fn(() => undefined); + const cancelPendingEventWaits = vi.fn(() => undefined); const listKnownEventNames = vi.fn(() => ['agent.stream', 'agent.typing', 'context_warning']); const waitForEvent = vi.fn(async () => ({ token: 'evented' })); const waitForIdle = vi.fn(async () => undefined); @@ -119,6 +121,7 @@ function createRuntimeMock(): { subscribeEvents, subscribeEvent, clearEventSubscriptions, + cancelPendingEventWaits, listKnownEventNames, waitForEvent, waitForIdle, @@ -172,6 +175,7 @@ function createRuntimeMock(): { subscribeEvents, subscribeEvent, clearEventSubscriptions, + cancelPendingEventWaits, listKnownEventNames, waitForEvent, waitForIdle, @@ -313,6 +317,15 @@ describe('platform companion clients', () => { expect(mock.clearEventSubscriptions).toHaveBeenCalledOnce(); }); + it('platform cancelPendingEventWaits forwards to runtime client', async () => { + const mock = createRuntimeMock(); + const client = new IOSCompanionClient({ runtime: mock.runtime, nodeId: 'ios-node' }); + + client.cancelPendingEventWaits('manual'); + + expect(mock.cancelPendingEventWaits).toHaveBeenCalledWith('manual'); + }); + it('platform listKnownEventNames forwards to runtime client', async () => { const mock = createRuntimeMock(); const client = new IOSCompanionClient({ runtime: mock.runtime, nodeId: 'ios-node' }); diff --git a/src/companion/platformClients.ts b/src/companion/platformClients.ts index 43c218e..ecb4e7a 100644 --- a/src/companion/platformClients.ts +++ b/src/companion/platformClients.ts @@ -269,6 +269,10 @@ export class MacOSCompanionClient { this.runtime.clearEventSubscriptions(); } + cancelPendingEventWaits(reason?: string): void { + this.runtime.cancelPendingEventWaits(reason); + } + listKnownEventNames(): CompanionEventName[] { return this.runtime.listKnownEventNames(); } @@ -535,6 +539,10 @@ export class IOSCompanionClient { this.runtime.clearEventSubscriptions(); } + cancelPendingEventWaits(reason?: string): void { + this.runtime.cancelPendingEventWaits(reason); + } + listKnownEventNames(): CompanionEventName[] { return this.runtime.listKnownEventNames(); } @@ -799,6 +807,10 @@ export class AndroidCompanionClient { this.runtime.clearEventSubscriptions(); } + cancelPendingEventWaits(reason?: string): void { + this.runtime.cancelPendingEventWaits(reason); + } + listKnownEventNames(): CompanionEventName[] { return this.runtime.listKnownEventNames(); } diff --git a/src/companion/runtimeClient.test.ts b/src/companion/runtimeClient.test.ts index 0364b24..47a8095 100644 --- a/src/companion/runtimeClient.test.ts +++ b/src/companion/runtimeClient.test.ts @@ -389,6 +389,29 @@ describe('CompanionRuntimeClient', () => { await awaited; }); + it('cancelPendingEventWaits rejects waiters without clearing subscriptions', async () => { + const client = new CompanionRuntimeClient({ + url: 'ws://127.0.0.1:1', + }); + const handler = vi.fn(); + client.subscribeEvents(handler); + + const awaited = expect( + client.waitForEvent('agent.stream', { timeoutMs: 10_000 }), + ).rejects.toThrow('manually cancelled'); + client.cancelPendingEventWaits('manually cancelled'); + await awaited; + + (client as unknown as { handleMessage: (raw: string) => void }).handleMessage( + JSON.stringify({ + id: 99, + event: 'agent.stream', + data: { token: 'still-subscribed' }, + }), + ); + expect(handler).toHaveBeenCalledWith('agent.stream', { token: 'still-subscribed' }); + }); + it('waitForEvent rejects immediately on disconnect', async () => { const client = new CompanionRuntimeClient({ url: 'ws://127.0.0.1:1', diff --git a/src/companion/runtimeClient.ts b/src/companion/runtimeClient.ts index ae8adb2..e3fa849 100644 --- a/src/companion/runtimeClient.ts +++ b/src/companion/runtimeClient.ts @@ -435,6 +435,10 @@ export class CompanionRuntimeClient { this.rejectEventWaits(new Error('Event subscriptions cleared')); } + cancelPendingEventWaits(reason = 'Event waits cancelled'): void { + this.rejectEventWaits(new Error(reason)); + } + subscribeEvent( eventName: CompanionEventName | string, handler: CompanionTypedEventHandler,