9 months ago
680797bPhase 3 of the desktop audit: core agent and client library improvements. **Cancel support (session + agent + protocol):** - Add `cancel` ClientMessage type to protocol.ts with validation - Add `cancel()` method to AgentSession that aborts the running turn via AbortController and rejects any pending ask/approval deferreds - Wire AbortController through session → runTurn → generateText so the AI SDK can abort in-flight API calls when the user cancels - Suppress abort/cancel error messages from being emitted as errors to the client (they're user-initiated, not failures) - AbortController is created fresh for each sendUserMessage and cleaned up in the finally block **Ping/pong keepalive (protocol + server + client):** - Add `ping` ClientMessage and `pong` ServerEvent to the protocol - Server handles ping before session ID validation (no session needed) - AgentSocket starts a configurable ping interval (default 30s) on connect, stops on close. Keeps connections alive through proxies and load balancers that have idle timeouts - Client filters pong events internally — not surfaced to consumers **Auto-reconnect with exponential backoff (AgentSocket):** - New `autoReconnect` option (default: false, opt-in) - On unexpected disconnect (not intentional close), schedules reconnection attempts with exponential backoff (500ms base, 30s max cap) plus random jitter to prevent thundering herd - Configurable `maxReconnectAttempts` (default: 10) before giving up - Messages sent while disconnected are queued and flushed after the new server_hello handshake completes - Intentional close() clears reconnection timers, ping timers, and the send queue - Ready deferred is reset on each reconnection attempt so consumers can await the new session **Message history cap (session.ts):** - Cap message history to MAX_MESSAGE_HISTORY (200) entries before each turn to prevent unbounded memory growth in long sessions - Trims from the beginning, preserving recent context **Test fixes:** - Add `appendTranscriptBatch` mock to both desktop test files to match the new store import from Phase 2 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Parent003c0a7