collet 0.1.0

Relentless agentic coding orchestrator with zero-drop agent loops
Documentation
// ── Worker attach mode ─────────────────────────────────────────────────────

/// Which view the main output panel is showing.
#[derive(Debug, Clone, PartialEq, Default)]
pub enum ViewMode {
    /// Normal chat output.
    #[default]
    Main,
    /// Attached to a specific swarm worker — output area shows that worker's stream.
    WorkerAttached { agent_id: String },
}

/// Maximum bytes kept in each worker's `full_stream` ring buffer.
/// Older content is truncated from the front when this limit is exceeded.
const MAX_WORKER_STREAM_BYTES: usize = 64 * 1024; // 64 KB
/// Maximum tool events kept per worker.
const MAX_WORKER_TOOL_EVENTS: usize = 200;

/// Full streaming state for an attached worker view.
#[derive(Debug, Clone, Default)]
pub struct WorkerDetailState {
    /// Streaming text accumulated from SwarmAgentToken events (ring-buffered).
    pub full_stream: String,
    /// Tool call/result log for this worker (bounded).
    pub tool_events: Vec<WorkerToolEvent>,
    /// Scroll offset within the worker view.
    pub scroll_offset: u16,
    /// Auto-scroll to bottom on new content.
    pub auto_scroll: bool,
}

impl WorkerDetailState {
    /// Append text to the stream, truncating the front if the buffer exceeds the cap.
    pub fn push_stream(&mut self, text: &str) {
        self.full_stream.push_str(text);
        if self.full_stream.len() > MAX_WORKER_STREAM_BYTES {
            let trim = self.full_stream.len() - MAX_WORKER_STREAM_BYTES;
            // Find the next char boundary after the trim point.
            let boundary = self.full_stream.ceil_char_boundary(trim);
            self.full_stream.drain(..boundary);
        }
    }

    /// Add a tool event, dropping the oldest if the cap is exceeded.
    pub fn push_tool_event(&mut self, event: WorkerToolEvent) {
        if self.tool_events.len() >= MAX_WORKER_TOOL_EVENTS {
            self.tool_events
                .drain(..self.tool_events.len() - MAX_WORKER_TOOL_EVENTS + 1);
        }
        self.tool_events.push(event);
    }
}

/// A tool event within a worker's detail view.
#[derive(Debug, Clone)]
pub struct WorkerToolEvent {
    pub name: String,
    pub args_preview: String,
    pub result_preview: String,
    pub success: bool,
}

// ── Swarm mode UI state ────────────────────────────────────────────────────

/// Swarm execution status for TUI display.
#[derive(Debug, Clone)]
pub struct SwarmUiStatus {
    /// Active collaboration mode label (e.g. "FORK", "HIVE", "FLOCK").
    pub mode_label: String,
    /// Per-agent status entries.
    pub agents: Vec<SwarmAgentUiEntry>,
    /// Overall hive phase.
    pub phase: SwarmPhase,
    /// Detected conflicts awaiting resolution.
    pub pending_conflicts: Vec<(String, Vec<String>)>,
}

/// Per-agent entry in swarm status.
#[derive(Debug, Clone)]
pub struct SwarmAgentUiEntry {
    pub agent_id: String,
    pub name: String,
    pub task_preview: String,
    pub status: SwarmAgentStatus,
    pub iteration: u32,
    pub modified_files: Vec<String>,
    pub tool_calls: u32,
    /// Input (prompt) tokens consumed by this agent.
    pub input_tokens: u64,
    /// Output (completion) tokens produced by this agent.
    pub output_tokens: u64,
    /// Full agent response text, populated when agent completes.
    pub output: String,
    /// Name of the tool currently being executed (live update).
    pub current_tool: Option<String>,
    /// Last ~200 chars of the agent's streaming output (ring buffer).
    pub streaming_preview: String,
    /// Timestamp of the last activity event from this agent.
    pub last_activity: Option<std::time::Instant>,
}

/// Status of an individual swarm agent.
#[derive(Debug, Clone, PartialEq)]
pub enum SwarmAgentStatus {
    Pending,
    Running,
    Paused,
    Completed { success: bool },
}

/// Phase of swarm execution.
#[derive(Debug, Clone, PartialEq)]
pub enum SwarmPhase {
    Analyzing,
    PlanReview,
    Executing,
    MergingResults,
    ResolvingConflicts,
    Done,
}

/// An architect plan awaiting user review.
#[derive(Debug, Clone)]
pub struct PendingPlan {
    /// The plan text produced by the architect agent.
    pub plan: String,
    /// The original user task message.
    pub user_msg: String,
    /// System prompt to carry into the code phase.
    pub system_prompt: String,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ToolLogEntry {
    pub name: String,
    pub args_preview: String,
    /// Full args JSON preserved for file_edit/file_write mini diff rendering.
    pub args_full: Option<String>,
    pub result_preview: String,
    pub success: bool,
    /// Unique call ID for exact ToolCall↔ToolResult matching in parallel execution.
    pub call_id: Option<String>,
}

/// Pre-aggregated changed-file entry for sidebar display.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ChangedFileEntry {
    pub path: String,
    pub additions: usize,
    pub deletions: usize,
}

/// Real-time process monitor data for debug mode sidebar.
#[derive(Debug, Clone, Default)]
pub struct DebugMonitor {
    /// Resident memory in bytes.
    pub memory_bytes: u64,
    /// CPU usage percentage (0.0 - 100.0+).
    pub cpu_percent: f32,
    /// Total prompt (input) tokens across all API calls.
    pub input_tokens: u64,
    /// Total completion (output) tokens across all API calls.
    pub output_tokens: u64,
    /// Total number of tool calls executed.
    pub total_tool_calls: u32,
    /// Total number of API calls.
    pub total_api_calls: u32,
    /// Number of active agents (main + subagents).
    pub active_agents: usize,

    // ── Agent performance metrics ──
    /// Average tool call latency in ms.
    pub tool_latency_avg_ms: f64,
    /// Maximum tool call latency in ms.
    pub tool_latency_max_ms: u64,
    /// Average API call latency in ms.
    pub api_latency_avg_ms: f64,
    /// Maximum API call latency in ms.
    pub api_latency_max_ms: u64,
    /// Tool success rate (0.0 - 100.0).
    pub tool_success_rate: f32,
    /// Average tokens consumed per iteration.
    pub tokens_per_iteration: f64,
    /// Average tool calls per iteration.
    pub tools_per_iteration: f64,
    /// Most called tools (name, count) — top 3.
    pub top_tools: Vec<(String, u32)>,
    /// Total RSS of MCP child processes (bytes).
    pub mcp_memory_bytes: u64,
    /// Total RSS of LSP child processes (bytes).
    pub lsp_memory_bytes: u64,
}