Skip to content

Improve agent process resiliency#81

Merged
gricha merged 2 commits intomainfrom
agent-resiliency-fixes
Jan 10, 2026
Merged

Improve agent process resiliency#81
gricha merged 2 commits intomainfrom
agent-resiliency-fixes

Conversation

@gricha
Copy link
Copy Markdown
Owner

@gricha gricha commented Jan 10, 2026

Summary

Fix multiple issues that could crash the agent process with unhandled promise rejections.

Root Cause

The agent crashed while you were away with:

error: SSE stream ended unexpectedly without session.idle

This was caused by an unhandled promise rejection in the OpenCode SSE stream processing.

Fixes

  1. Global error handlers (src/agent/index.ts)

    • Add process.on('unhandledRejection') handler
    • Add process.on('uncaughtException') handler
    • Prevents any unhandled errors from crashing the process
  2. Session index file watcher (src/worker/session-index.ts)

    • Wrap async setTimeout callback with .catch()
  3. Eager image pull (src/docker/eager-pull.ts)

    • Add .catch() to all fire-and-forget async calls
  4. OpenCode SSE stream (src/session-manager/adapters/opencode.ts)

    • Add .catch() to IIFE async block
  5. Process exit handlers (src/session-manager/adapters/claude.ts, src/terminal/base-handler.ts)

    • Add .catch() to process.exited promise chains

Test plan

  • Start agent, verify it doesn't crash on SSE stream errors
  • Verify errors are logged instead of crashing
  • Run agent for extended period without crashes

🤖 Generated with Claude Code

Fix multiple issues that could crash the agent process:

- Add global unhandledRejection and uncaughtException handlers
- Fix fire-and-forget async in session-index.ts file watcher
- Fix fire-and-forget async in eager-pull.ts image pull
- Add catch handler to SSE stream IIFE in opencode adapter
- Add catch handlers to process.exited promises in adapters

These fixes prevent unhandled promise rejections from crashing the
entire agent process. Errors are now logged instead of crashing.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Comment thread src/agent/index.ts
Comment on lines +5 to +12
process.on('unhandledRejection', (reason, promise) => {
console.error('[agent] Unhandled promise rejection:', reason);
console.error('[agent] Promise:', promise);
});

process.on('uncaughtException', (err) => {
console.error('[agent] Uncaught exception:', err);
});

This comment was marked as outdated.

Sentry code review correctly identified that uncaughtException leaves
the process in an undefined state. Exit with code 1 so systemd can
restart the agent cleanly.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@gricha gricha merged commit 5d3a757 into main Jan 10, 2026
8 checks passed
@gricha gricha deleted the agent-resiliency-fixes branch January 10, 2026 21:20
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