Skip to content

Fix session error handling and add timeout protection#54

Merged
gricha merged 6 commits intomainfrom
fix/session-error-handling
Jan 8, 2026
Merged

Fix session error handling and add timeout protection#54
gricha merged 6 commits intomainfrom
fix/session-error-handling

Conversation

@gricha
Copy link
Copy Markdown
Owner

@gricha gricha commented Jan 8, 2026

Summary

  • Fix fire-and-forget message POST in OpenCode - Messages are now properly awaited with HTTP status validation, so errors propagate to users instead of silently failing
  • Add heartbeat monitoring for SSE connections - Detects connection loss within 45s (OpenCode sends heartbeats every 30s), showing "Connection lost" message instead of hanging
  • Add session verification on pickup - Checks if session still exists before using it, preventing stale session issues on mobile
  • Add timeout protection to Claude Code - Subprocess execution now has 5-minute overall timeout and 60s inactivity detection to prevent infinite hangs
  • Create shared utilities - SessionMonitor for timeout/activity tracking and SessionVerifier abstractions reduce code duplication

Test plan

  • Run unit tests: bun test test/chat/session-monitor.test.ts (26 tests)
  • Run unit tests: bun test test/chat/session-utils.test.ts (24 tests)
  • All 78 unit tests passing
  • Run Playwright e2e tests: cd web && playwright test e2e/connection-error-handling.spec.ts
  • Manual test: Pick up session on mobile, verify error messages appear if connection fails
  • Manual test: Let session idle, verify timeout messages are user-friendly

Files Changed

File Description
src/chat/opencode-server.ts Fixed fire-and-forget, added heartbeat monitoring, session verification
src/chat/base-claude-session.ts Added SessionMonitor integration for timeout protection
src/chat/session-monitor.ts New - Shared timeout/activity monitoring utilities
src/chat/session-utils.ts New - Session verification abstractions
test/chat/session-monitor.test.ts New - 26 unit tests
test/chat/session-utils.test.ts New - 24 unit tests
web/e2e/connection-error-handling.spec.ts New - Playwright e2e tests

🤖 Generated with Claude Code

gricha and others added 4 commits January 8, 2026 19:46
- Fix fire-and-forget message POST in OpenCode (now properly awaited)
- Add heartbeat monitoring for SSE connections (45s timeout)
- Add session verification on pickup to detect expired sessions
- Add timeout protection to Claude Code subprocess execution
- Create shared SessionMonitor utility for timeout/activity tracking
- Create SessionVerifier abstractions for session validation
- Add comprehensive unit tests (50 new tests)
- Add Playwright e2e tests for connection error handling

Fixes hanging messages on mobile when session state is stale.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Comment on lines +441 to +451
console.error(
`[opencode-server] No heartbeat received for ${timeSinceLastHeartbeat}ms, connection may be lost`
);
this.streamError = new Error('Connection to OpenCode server lost (no heartbeat)');
this.onMessage({
type: 'error',
content: 'Connection to OpenCode server lost. Please try again.',
timestamp: new Date().toISOString(),
});
proc.kill();
resolveDone!();

This comment was marked as outdated.

@gricha gricha merged commit 6c7dba2 into main Jan 8, 2026
6 checks passed
@gricha gricha deleted the fix/session-error-handling branch January 8, 2026 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant