Home

mweinbach / agent-coworker

publicmweinbach/agent-coworker
Overview Code History Branches Pull requestsIssuesInsights
main
HomeOverview Code PRsIssues

feat: agent cancel support, client auto-reconnect, message history cap

9 months ago

680797b
Authored
mweinbach2/10/2026, 12:55:50 AM
Phase 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

7 files changed
  • apps/desktop/test/settings-nav.test.ts+1−0
  • apps/desktop/test/thread-archive.test.ts+1−0
  • src/agent.ts+4−1
  • src/client/agentSocket.ts+137−5
  • src/server/protocol.ts+6−1
  • src/server/session.ts+37−1
  • src/server/startServer.ts+15−0