Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
502 changes: 502 additions & 0 deletions cortex-agents/src/background/events.rs

Large diffs are not rendered by default.

896 changes: 896 additions & 0 deletions cortex-agents/src/background/executor.rs

Large diffs are not rendered by default.

523 changes: 523 additions & 0 deletions cortex-agents/src/background/messaging.rs

Large diffs are not rendered by default.

63 changes: 63 additions & 0 deletions cortex-agents/src/background/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//! Background agent execution system.
//!
//! This module provides infrastructure for running agents in the background
//! with async communication, event broadcasting, and lifecycle management.
//!
//! # Architecture
//!
//! ```text
//! BackgroundAgentManager
//! ├── BackgroundAgent (tokio task)
//! │ ├── status channel (mpsc)
//! │ ├── cancel channel (oneshot)
//! │ └── mailbox (AgentMailbox)
//! ├── Event broadcaster (broadcast)
//! └── Agent registry (HashMap)
//! ```
//!
//! # Example
//!
//! ```rust,ignore
//! use cortex_agents::background::{
//! BackgroundAgentManager, AgentConfig, AgentEvent
//! };
//!
//! // Create manager
//! let mut manager = BackgroundAgentManager::new(5);
//!
//! // Subscribe to events
//! let mut events = manager.subscribe();
//!
//! // Spawn a background agent
//! let id = manager.spawn(AgentConfig::new("Search for patterns")).await?;
//!
//! // Monitor events
//! while let Ok(event) = events.recv().await {
//! match event {
//! AgentEvent::Progress { id, message } => println!("{}: {}", id, message),
//! AgentEvent::Completed { id, result } => {
//! println!("Agent {} completed: {:?}", id, result);
//! break;
//! }
//! _ => {}
//! }
//! }
//! ```
//!
//! # Safety & Limits
//!
//! - Maximum concurrent agents: configurable (default 5)
//! - Automatic timeout: 30 minutes per agent
//! - RAII cleanup: agents are cancelled when manager is dropped
//! - Isolated contexts: agents don't share credentials

pub mod events;
pub mod executor;
pub mod messaging;

pub use events::{AgentEvent, Notification, NotificationLevel, NotificationManager};
pub use executor::{
AgentConfig, AgentResult, AgentStatus, BackgroundAgent, BackgroundAgentError,
BackgroundAgentManager,
};
pub use messaging::{AgentMailbox, AgentMessage, MessageContent, MessageRouter};
37 changes: 37 additions & 0 deletions cortex-agents/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,37 @@
//! let decision = decide_routing(&tasks);
//! assert_eq!(decision.mode, DispatchMode::Parallel);
//! ```
//!
//! # Background Agents
//!
//! The background module provides infrastructure for running agents as background
//! tokio tasks with async messaging:
//!
//! ```rust,ignore
//! use cortex_agents::background::{BackgroundAgentManager, AgentConfig, AgentEvent};
//!
//! // Create manager
//! let mut manager = BackgroundAgentManager::new(5);
//!
//! // Subscribe to events
//! let mut events = manager.subscribe();
//!
//! // Spawn a background agent
//! let id = manager.spawn(AgentConfig::new("Search for patterns")).await?;
//!
//! // Monitor events
//! while let Ok(event) = events.recv().await {
//! match event {
//! AgentEvent::Completed { id, summary, .. } => {
//! println!("Agent {} completed: {}", id, summary);
//! break;
//! }
//! _ => {}
//! }
//! }
//! ```

pub mod background;
pub mod collab;
pub mod control;
pub mod custom;
Expand Down Expand Up @@ -179,6 +209,13 @@ pub use routing::{
can_parallelize, decide_routing, estimate_duration, DispatchMode, RoutingDecision, TaskInfo,
};

// Re-export background agent types
pub use background::{
AgentConfig, AgentEvent, AgentMailbox, AgentMessage, AgentResult, AgentStatus, BackgroundAgent,
BackgroundAgentError, BackgroundAgentManager, MessageContent, MessageRouter, Notification,
NotificationLevel, NotificationManager,
};

use thiserror::Error;

#[derive(Error, Debug)]
Expand Down
1 change: 1 addition & 0 deletions cortex-tui/src/commands/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ impl CommandExecutor {
"init" => self.cmd_init(cmd),
"commands" | "cmds" => CommandResult::Async("commands:list".to_string()),
"agents" | "subagents" => CommandResult::OpenModal(ModalType::Agents),
"tasks" | "bg" | "background" => CommandResult::OpenModal(ModalType::Tasks),
"skills" | "sk" => CommandResult::OpenModal(ModalType::Skills),
"skill" | "invoke" => self.cmd_skill(cmd),
"skill-reload" | "sr" => CommandResult::Async("skills:reload".to_string()),
Expand Down
9 changes: 9 additions & 0 deletions cortex-tui/src/commands/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,15 @@ pub fn register_builtin_commands(registry: &mut CommandRegistry) {
false,
));

registry.register(CommandDef::new(
"tasks",
&["bg", "background"],
"View and manage background tasks",
"/tasks",
CommandCategory::General,
false,
));

registry.register(CommandDef::new(
"skills",
&["sk"],
Expand Down
3 changes: 3 additions & 0 deletions cortex-tui/src/commands/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ pub enum ModalType {
Upgrade,
/// Agents manager modal for listing and creating agents
Agents,
/// Background tasks view modal
Tasks,
/// Skills manager modal for listing and invoking skills
Skills,
}
Expand Down Expand Up @@ -183,6 +185,7 @@ impl ModalType {
ModalType::Login => "Login",
ModalType::Upgrade => "Upgrade",
ModalType::Agents => "Agents",
ModalType::Tasks => "Background Tasks",
ModalType::Skills => "Skills",
}
}
Expand Down
4 changes: 2 additions & 2 deletions cortex-tui/src/runner/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ use tokio_stream::StreamExt;

use crate::actions::{ActionContext, ActionMapper, KeyAction};
use crate::app::{
AppState, AppView, ApprovalMode, AutocompleteItem, AutocompleteTrigger, FocusTarget,
PendingToolResult, SubagentDisplayStatus, SubagentTaskDisplay,
AppState, AppView, ApprovalMode, ApprovalState, AutocompleteItem, AutocompleteTrigger,
FocusTarget, PendingToolResult, SubagentDisplayStatus, SubagentTaskDisplay,
};
use crate::bridge::{SessionBridge, StreamController, adapt_event};
use crate::commands::{
Expand Down
3 changes: 3 additions & 0 deletions cortex-tui/src/views/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
//! - [`MinimalSessionView`](minimal_session::MinimalSessionView) - Minimalist terminal-style chat
//! - [`ApprovalView`](approval::ApprovalView) - Tool approval modal
//! - [`QuestionPromptView`](question_prompt::QuestionPromptView) - Interactive question prompt
//! - [`TasksView`](tasks::TasksView) - Background tasks monitoring view
//! - [`tool_call`] - Tool call display types

pub mod approval;
pub mod minimal_session;
pub mod question_prompt;
pub mod tasks;
pub mod tool_call;

// Re-exports
pub use approval::ApprovalView;
pub use minimal_session::MinimalSessionView;
pub use question_prompt::{QuestionClickZones, QuestionHit, QuestionPromptView};
pub use tasks::{TaskDisplay, TaskStatus, TasksView};
Loading
Loading