Skip to content

feat: implement background agents and async messaging system#422

Open
echobt wants to merge 3 commits intomasterfrom
feature/background-agents-async-messaging
Open

feat: implement background agents and async messaging system#422
echobt wants to merge 3 commits intomasterfrom
feature/background-agents-async-messaging

Conversation

@echobt
Copy link
Contributor

@echobt echobt commented Jan 27, 2026

Summary

This PR implements the complete background worker architecture and asynchronous messaging system for the CLI as specified in ORCHESTRATE/AGENT_1_BACKGROUND_AGENTS.md.

Changes

New Background Agent Module (cortex-agents/src/background/)

  • executor.rs: BackgroundAgentManager for spawning and managing background tasks

    • Configurable max concurrent agents (default: 5)
    • Automatic timeout after 30 minutes
    • RAII cleanup with proper cancellation support
    • Event broadcasting for monitoring
  • messaging.rs: Inter-agent async messaging system

    • AgentMailbox for per-agent message queues
    • MessageRouter for routing messages between agents
    • Support for notifications, requests/responses, data sharing, and status updates
  • events.rs: Event system for background agents

    • AgentEvent enum for lifecycle events (Started, Progress, Completed, Failed, Cancelled)
    • NotificationManager for collecting and displaying notifications
    • Support for TodoUpdated events for progress tracking

TUI Enhancements (cortex-tui/)

  • New /tasks command (aliases: /bg, /background) for viewing background tasks
  • TasksView component for displaying task status, duration, and progress
  • ModalType::Tasks for the background tasks modal

Tests

  • 21 new unit tests covering:
    • Background agent spawning and cancellation
    • Max concurrent agent limits
    • Event subscription and notification
    • Inter-agent messaging
    • Notification management

Verification

  • cargo check -p cortex-agents
  • cargo check -p cortex-tui
  • cargo fmt
  • cargo test -p cortex-agents -- background ✅ (21 tests pass)

Related

  • Implements requirements from ORCHESTRATE/AGENT_1_BACKGROUND_AGENTS.md

This commit adds the complete background worker architecture and asynchronous
messaging system for the CLI as specified in ORCHESTRATE/AGENT_1_BACKGROUND_AGENTS.md.

## New Features

### Background Agent Module (cortex-agents/src/background/)
- **executor.rs**: BackgroundAgentManager for spawning and managing background tasks
  - Configurable max concurrent agents (default: 5)
  - Automatic timeout after 30 minutes
  - RAII cleanup with proper cancellation support
  - Event broadcasting for monitoring

- **messaging.rs**: Inter-agent async messaging system
  - AgentMailbox for per-agent message queues
  - MessageRouter for routing messages between agents
  - Support for notifications, requests/responses, data sharing, and status updates

- **events.rs**: Event system for background agents
  - AgentEvent enum for lifecycle events (Started, Progress, Completed, Failed, Cancelled)
  - NotificationManager for collecting and displaying notifications
  - Support for TodoUpdated events for progress tracking

### TUI Enhancements (cortex-tui/)
- New /tasks command (aliases: /bg, /background) for viewing background tasks
- TasksView component for displaying task status, duration, and progress
- ModalType::Tasks for the background tasks modal

## Tests
- 21 new unit tests covering:
  - Background agent spawning and cancellation
  - Max concurrent agent limits
  - Event subscription and notification
  - Inter-agent messaging
  - Notification management

## Implementation Details
- Uses tokio for async runtime with mpsc/broadcast channels
- Proper error handling with BackgroundAgentError enum
- Isolated agent contexts (no shared credentials)
- Grace period for cancellation (5 seconds)
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.

2 participants