mirror of
https://github.com/affaan-m/everything-claude-code.git
synced 2026-04-09 19:03:28 +08:00
Compare commits
2 Commits
main
...
00a2a7163a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
00a2a7163a | ||
|
|
b032846806 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -83,6 +83,9 @@ temp/
|
|||||||
*.bak
|
*.bak
|
||||||
*.backup
|
*.backup
|
||||||
|
|
||||||
|
# Rust build artifacts
|
||||||
|
ecc2/target/
|
||||||
|
|
||||||
# Bootstrap pipeline outputs
|
# Bootstrap pipeline outputs
|
||||||
# Generated lock files in tool subdirectories
|
# Generated lock files in tool subdirectories
|
||||||
.opencode/package-lock.json
|
.opencode/package-lock.json
|
||||||
|
|||||||
2016
ecc2/Cargo.lock
generated
Normal file
2016
ecc2/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
52
ecc2/Cargo.toml
Normal file
52
ecc2/Cargo.toml
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
[package]
|
||||||
|
name = "ecc-tui"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
description = "ECC 2.0 — Agentic IDE control plane with TUI dashboard"
|
||||||
|
license = "MIT"
|
||||||
|
authors = ["Affaan Mustafa <me@affaanmustafa.com>"]
|
||||||
|
repository = "https://github.com/affaan-m/everything-claude-code"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
# TUI
|
||||||
|
ratatui = "0.29"
|
||||||
|
crossterm = "0.28"
|
||||||
|
|
||||||
|
# Async runtime
|
||||||
|
tokio = { version = "1", features = ["full"] }
|
||||||
|
|
||||||
|
# State store
|
||||||
|
rusqlite = { version = "0.32", features = ["bundled"] }
|
||||||
|
|
||||||
|
# Git integration
|
||||||
|
git2 = "0.19"
|
||||||
|
|
||||||
|
# Serialization
|
||||||
|
serde = { version = "1", features = ["derive"] }
|
||||||
|
serde_json = "1"
|
||||||
|
toml = "0.8"
|
||||||
|
|
||||||
|
# CLI
|
||||||
|
clap = { version = "4", features = ["derive"] }
|
||||||
|
|
||||||
|
# Logging & tracing
|
||||||
|
tracing = "0.1"
|
||||||
|
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||||
|
|
||||||
|
# Error handling
|
||||||
|
anyhow = "1"
|
||||||
|
thiserror = "2"
|
||||||
|
|
||||||
|
# Time
|
||||||
|
chrono = { version = "0.4", features = ["serde"] }
|
||||||
|
|
||||||
|
# UUID for session IDs
|
||||||
|
uuid = { version = "1", features = ["v4"] }
|
||||||
|
|
||||||
|
# Directory paths
|
||||||
|
dirs = "6"
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
lto = true
|
||||||
|
codegen-units = 1
|
||||||
|
strip = true
|
||||||
36
ecc2/src/comms/mod.rs
Normal file
36
ecc2/src/comms/mod.rs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
use anyhow::Result;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::session::store::StateStore;
|
||||||
|
|
||||||
|
/// Message types for inter-agent communication.
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub enum MessageType {
|
||||||
|
/// Task handoff from one agent to another
|
||||||
|
TaskHandoff { task: String, context: String },
|
||||||
|
/// Agent requesting information from another
|
||||||
|
Query { question: String },
|
||||||
|
/// Response to a query
|
||||||
|
Response { answer: String },
|
||||||
|
/// Notification of completion
|
||||||
|
Completed {
|
||||||
|
summary: String,
|
||||||
|
files_changed: Vec<String>,
|
||||||
|
},
|
||||||
|
/// Conflict detected (e.g., two agents editing the same file)
|
||||||
|
Conflict { file: String, description: String },
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Send a structured message between sessions.
|
||||||
|
pub fn send(db: &StateStore, from: &str, to: &str, msg: &MessageType) -> Result<()> {
|
||||||
|
let content = serde_json::to_string(msg)?;
|
||||||
|
let msg_type = match msg {
|
||||||
|
MessageType::TaskHandoff { .. } => "task_handoff",
|
||||||
|
MessageType::Query { .. } => "query",
|
||||||
|
MessageType::Response { .. } => "response",
|
||||||
|
MessageType::Completed { .. } => "completed",
|
||||||
|
MessageType::Conflict { .. } => "conflict",
|
||||||
|
};
|
||||||
|
db.send_message(from, to, &content, msg_type)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
92
ecc2/src/config/mod.rs
Normal file
92
ecc2/src/config/mod.rs
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
use anyhow::Result;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
#[serde(default)]
|
||||||
|
pub struct Config {
|
||||||
|
pub db_path: PathBuf,
|
||||||
|
pub worktree_root: PathBuf,
|
||||||
|
pub max_parallel_sessions: usize,
|
||||||
|
pub max_parallel_worktrees: usize,
|
||||||
|
pub session_timeout_secs: u64,
|
||||||
|
pub heartbeat_interval_secs: u64,
|
||||||
|
pub default_agent: String,
|
||||||
|
pub cost_budget_usd: f64,
|
||||||
|
pub token_budget: u64,
|
||||||
|
pub theme: Theme,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub enum Theme {
|
||||||
|
Dark,
|
||||||
|
Light,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Config {
|
||||||
|
fn default() -> Self {
|
||||||
|
let home = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
|
||||||
|
Self {
|
||||||
|
db_path: home.join(".claude").join("ecc2.db"),
|
||||||
|
worktree_root: PathBuf::from("/tmp/ecc-worktrees"),
|
||||||
|
max_parallel_sessions: 8,
|
||||||
|
max_parallel_worktrees: 6,
|
||||||
|
session_timeout_secs: 3600,
|
||||||
|
heartbeat_interval_secs: 30,
|
||||||
|
default_agent: "claude".to_string(),
|
||||||
|
cost_budget_usd: 10.0,
|
||||||
|
token_budget: 500_000,
|
||||||
|
theme: Theme::Dark,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Config {
|
||||||
|
pub fn load() -> Result<Self> {
|
||||||
|
let config_path = dirs::home_dir()
|
||||||
|
.unwrap_or_else(|| PathBuf::from("."))
|
||||||
|
.join(".claude")
|
||||||
|
.join("ecc2.toml");
|
||||||
|
|
||||||
|
if config_path.exists() {
|
||||||
|
let content = std::fs::read_to_string(&config_path)?;
|
||||||
|
let config: Config = toml::from_str(&content)?;
|
||||||
|
Ok(config)
|
||||||
|
} else {
|
||||||
|
Ok(Config::default())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::Config;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn default_includes_positive_budget_thresholds() {
|
||||||
|
let config = Config::default();
|
||||||
|
|
||||||
|
assert!(config.cost_budget_usd > 0.0);
|
||||||
|
assert!(config.token_budget > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn missing_budget_fields_fall_back_to_defaults() {
|
||||||
|
let legacy_config = r#"
|
||||||
|
db_path = "/tmp/ecc2.db"
|
||||||
|
worktree_root = "/tmp/ecc-worktrees"
|
||||||
|
max_parallel_sessions = 8
|
||||||
|
max_parallel_worktrees = 6
|
||||||
|
session_timeout_secs = 3600
|
||||||
|
heartbeat_interval_secs = 30
|
||||||
|
default_agent = "claude"
|
||||||
|
theme = "Dark"
|
||||||
|
"#;
|
||||||
|
|
||||||
|
let config: Config = toml::from_str(legacy_config).unwrap();
|
||||||
|
let defaults = Config::default();
|
||||||
|
|
||||||
|
assert_eq!(config.cost_budget_usd, defaults.cost_budget_usd);
|
||||||
|
assert_eq!(config.token_budget, defaults.token_budget);
|
||||||
|
}
|
||||||
|
}
|
||||||
97
ecc2/src/main.rs
Normal file
97
ecc2/src/main.rs
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
mod comms;
|
||||||
|
mod config;
|
||||||
|
mod observability;
|
||||||
|
mod session;
|
||||||
|
mod tui;
|
||||||
|
mod worktree;
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
|
use clap::Parser;
|
||||||
|
use tracing_subscriber::EnvFilter;
|
||||||
|
|
||||||
|
#[derive(Parser, Debug)]
|
||||||
|
#[command(name = "ecc", version, about = "ECC 2.0 — Agentic IDE control plane")]
|
||||||
|
struct Cli {
|
||||||
|
#[command(subcommand)]
|
||||||
|
command: Option<Commands>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(clap::Subcommand, Debug)]
|
||||||
|
enum Commands {
|
||||||
|
/// Launch the TUI dashboard
|
||||||
|
Dashboard,
|
||||||
|
/// Start a new agent session
|
||||||
|
Start {
|
||||||
|
/// Task description for the agent
|
||||||
|
#[arg(short, long)]
|
||||||
|
task: String,
|
||||||
|
/// Agent type (claude, codex, custom)
|
||||||
|
#[arg(short, long, default_value = "claude")]
|
||||||
|
agent: String,
|
||||||
|
/// Create a dedicated worktree for this session
|
||||||
|
#[arg(short, long)]
|
||||||
|
worktree: bool,
|
||||||
|
},
|
||||||
|
/// List active sessions
|
||||||
|
Sessions,
|
||||||
|
/// Show session details
|
||||||
|
Status {
|
||||||
|
/// Session ID or alias
|
||||||
|
session_id: Option<String>,
|
||||||
|
},
|
||||||
|
/// Stop a running session
|
||||||
|
Stop {
|
||||||
|
/// Session ID or alias
|
||||||
|
session_id: String,
|
||||||
|
},
|
||||||
|
/// Run as background daemon
|
||||||
|
Daemon,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<()> {
|
||||||
|
tracing_subscriber::fmt()
|
||||||
|
.with_env_filter(EnvFilter::from_default_env())
|
||||||
|
.init();
|
||||||
|
|
||||||
|
let cli = Cli::parse();
|
||||||
|
|
||||||
|
let cfg = config::Config::load()?;
|
||||||
|
let db = session::store::StateStore::open(&cfg.db_path)?;
|
||||||
|
|
||||||
|
match cli.command {
|
||||||
|
Some(Commands::Dashboard) | None => {
|
||||||
|
tui::app::run(db, cfg).await?;
|
||||||
|
}
|
||||||
|
Some(Commands::Start {
|
||||||
|
task,
|
||||||
|
agent,
|
||||||
|
worktree: use_worktree,
|
||||||
|
}) => {
|
||||||
|
let session_id =
|
||||||
|
session::manager::create_session(&db, &cfg, &task, &agent, use_worktree).await?;
|
||||||
|
println!("Session started: {session_id}");
|
||||||
|
}
|
||||||
|
Some(Commands::Sessions) => {
|
||||||
|
let sessions = session::manager::list_sessions(&db)?;
|
||||||
|
for s in sessions {
|
||||||
|
println!("{} [{}] {}", s.id, s.state, s.task);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(Commands::Status { session_id }) => {
|
||||||
|
let id = session_id.unwrap_or_else(|| "latest".to_string());
|
||||||
|
let status = session::manager::get_status(&db, &id)?;
|
||||||
|
println!("{status}");
|
||||||
|
}
|
||||||
|
Some(Commands::Stop { session_id }) => {
|
||||||
|
session::manager::stop_session(&db, &session_id).await?;
|
||||||
|
println!("Session stopped: {session_id}");
|
||||||
|
}
|
||||||
|
Some(Commands::Daemon) => {
|
||||||
|
println!("Starting ECC daemon...");
|
||||||
|
session::daemon::run(db, cfg).await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
54
ecc2/src/observability/mod.rs
Normal file
54
ecc2/src/observability/mod.rs
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
use anyhow::Result;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::session::store::StateStore;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct ToolCallEvent {
|
||||||
|
pub session_id: String,
|
||||||
|
pub tool_name: String,
|
||||||
|
pub input_summary: String,
|
||||||
|
pub output_summary: String,
|
||||||
|
pub duration_ms: u64,
|
||||||
|
pub risk_score: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToolCallEvent {
|
||||||
|
/// Compute risk score based on tool type and input patterns.
|
||||||
|
pub fn compute_risk(tool_name: &str, input: &str) -> f64 {
|
||||||
|
let mut score: f64 = 0.0;
|
||||||
|
|
||||||
|
// Destructive tools get higher base risk
|
||||||
|
match tool_name {
|
||||||
|
"Bash" => score += 0.3,
|
||||||
|
"Write" => score += 0.2,
|
||||||
|
"Edit" => score += 0.1,
|
||||||
|
_ => score += 0.05,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dangerous patterns in bash commands
|
||||||
|
if tool_name == "Bash" {
|
||||||
|
if input.contains("rm -rf") || input.contains("--force") {
|
||||||
|
score += 0.4;
|
||||||
|
}
|
||||||
|
if input.contains("git push") || input.contains("git reset") {
|
||||||
|
score += 0.3;
|
||||||
|
}
|
||||||
|
if input.contains("sudo") || input.contains("chmod 777") {
|
||||||
|
score += 0.5;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
score.min(1.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn log_tool_call(db: &StateStore, event: &ToolCallEvent) -> Result<()> {
|
||||||
|
db.send_message(
|
||||||
|
&event.session_id,
|
||||||
|
"observability",
|
||||||
|
&serde_json::to_string(event)?,
|
||||||
|
"tool_call",
|
||||||
|
)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
46
ecc2/src/session/daemon.rs
Normal file
46
ecc2/src/session/daemon.rs
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
use anyhow::Result;
|
||||||
|
use std::time::Duration;
|
||||||
|
use tokio::time;
|
||||||
|
|
||||||
|
use super::store::StateStore;
|
||||||
|
use super::SessionState;
|
||||||
|
use crate::config::Config;
|
||||||
|
|
||||||
|
/// Background daemon that monitors sessions, handles heartbeats,
|
||||||
|
/// and cleans up stale resources.
|
||||||
|
pub async fn run(db: StateStore, cfg: Config) -> Result<()> {
|
||||||
|
tracing::info!("ECC daemon started");
|
||||||
|
|
||||||
|
let heartbeat_interval = Duration::from_secs(cfg.heartbeat_interval_secs);
|
||||||
|
let timeout = Duration::from_secs(cfg.session_timeout_secs);
|
||||||
|
|
||||||
|
loop {
|
||||||
|
if let Err(e) = check_sessions(&db, timeout) {
|
||||||
|
tracing::error!("Session check failed: {e}");
|
||||||
|
}
|
||||||
|
|
||||||
|
time::sleep(heartbeat_interval).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_sessions(db: &StateStore, timeout: Duration) -> Result<()> {
|
||||||
|
let sessions = db.list_sessions()?;
|
||||||
|
|
||||||
|
for session in sessions {
|
||||||
|
if session.state != SessionState::Running {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let elapsed = chrono::Utc::now()
|
||||||
|
.signed_duration_since(session.updated_at)
|
||||||
|
.to_std()
|
||||||
|
.unwrap_or(Duration::ZERO);
|
||||||
|
|
||||||
|
if elapsed > timeout {
|
||||||
|
tracing::warn!("Session {} timed out after {:?}", session.id, elapsed);
|
||||||
|
db.update_state(&session.id, &SessionState::Failed)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
76
ecc2/src/session/manager.rs
Normal file
76
ecc2/src/session/manager.rs
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
use anyhow::Result;
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
use super::store::StateStore;
|
||||||
|
use super::{Session, SessionMetrics, SessionState};
|
||||||
|
use crate::config::Config;
|
||||||
|
use crate::worktree;
|
||||||
|
|
||||||
|
pub async fn create_session(
|
||||||
|
db: &StateStore,
|
||||||
|
cfg: &Config,
|
||||||
|
task: &str,
|
||||||
|
agent_type: &str,
|
||||||
|
use_worktree: bool,
|
||||||
|
) -> Result<String> {
|
||||||
|
let id = uuid::Uuid::new_v4().to_string()[..8].to_string();
|
||||||
|
let now = chrono::Utc::now();
|
||||||
|
|
||||||
|
let wt = if use_worktree {
|
||||||
|
Some(worktree::create_for_session(&id, cfg)?)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
let session = Session {
|
||||||
|
id: id.clone(),
|
||||||
|
task: task.to_string(),
|
||||||
|
agent_type: agent_type.to_string(),
|
||||||
|
state: SessionState::Pending,
|
||||||
|
worktree: wt,
|
||||||
|
created_at: now,
|
||||||
|
updated_at: now,
|
||||||
|
metrics: SessionMetrics::default(),
|
||||||
|
};
|
||||||
|
|
||||||
|
db.insert_session(&session)?;
|
||||||
|
Ok(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn list_sessions(db: &StateStore) -> Result<Vec<Session>> {
|
||||||
|
db.list_sessions()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_status(db: &StateStore, id: &str) -> Result<SessionStatus> {
|
||||||
|
let session = db
|
||||||
|
.get_session(id)?
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("Session not found: {id}"))?;
|
||||||
|
Ok(SessionStatus(session))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn stop_session(db: &StateStore, id: &str) -> Result<()> {
|
||||||
|
db.update_state(id, &SessionState::Stopped)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct SessionStatus(Session);
|
||||||
|
|
||||||
|
impl fmt::Display for SessionStatus {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
let s = &self.0;
|
||||||
|
writeln!(f, "Session: {}", s.id)?;
|
||||||
|
writeln!(f, "Task: {}", s.task)?;
|
||||||
|
writeln!(f, "Agent: {}", s.agent_type)?;
|
||||||
|
writeln!(f, "State: {}", s.state)?;
|
||||||
|
if let Some(ref wt) = s.worktree {
|
||||||
|
writeln!(f, "Branch: {}", wt.branch)?;
|
||||||
|
writeln!(f, "Worktree: {}", wt.path.display())?;
|
||||||
|
}
|
||||||
|
writeln!(f, "Tokens: {}", s.metrics.tokens_used)?;
|
||||||
|
writeln!(f, "Tools: {}", s.metrics.tool_calls)?;
|
||||||
|
writeln!(f, "Files: {}", s.metrics.files_changed)?;
|
||||||
|
writeln!(f, "Cost: ${:.4}", s.metrics.cost_usd)?;
|
||||||
|
writeln!(f, "Created: {}", s.created_at)?;
|
||||||
|
write!(f, "Updated: {}", s.updated_at)
|
||||||
|
}
|
||||||
|
}
|
||||||
59
ecc2/src/session/mod.rs
Normal file
59
ecc2/src/session/mod.rs
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
pub mod daemon;
|
||||||
|
pub mod manager;
|
||||||
|
pub mod store;
|
||||||
|
|
||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::fmt;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct Session {
|
||||||
|
pub id: String,
|
||||||
|
pub task: String,
|
||||||
|
pub agent_type: String,
|
||||||
|
pub state: SessionState,
|
||||||
|
pub worktree: Option<WorktreeInfo>,
|
||||||
|
pub created_at: DateTime<Utc>,
|
||||||
|
pub updated_at: DateTime<Utc>,
|
||||||
|
pub metrics: SessionMetrics,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||||
|
pub enum SessionState {
|
||||||
|
Pending,
|
||||||
|
Running,
|
||||||
|
Idle,
|
||||||
|
Completed,
|
||||||
|
Failed,
|
||||||
|
Stopped,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for SessionState {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
SessionState::Pending => write!(f, "pending"),
|
||||||
|
SessionState::Running => write!(f, "running"),
|
||||||
|
SessionState::Idle => write!(f, "idle"),
|
||||||
|
SessionState::Completed => write!(f, "completed"),
|
||||||
|
SessionState::Failed => write!(f, "failed"),
|
||||||
|
SessionState::Stopped => write!(f, "stopped"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct WorktreeInfo {
|
||||||
|
pub path: PathBuf,
|
||||||
|
pub branch: String,
|
||||||
|
pub base_branch: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||||
|
pub struct SessionMetrics {
|
||||||
|
pub tokens_used: u64,
|
||||||
|
pub tool_calls: u64,
|
||||||
|
pub files_changed: u32,
|
||||||
|
pub duration_secs: u64,
|
||||||
|
pub cost_usd: f64,
|
||||||
|
}
|
||||||
186
ecc2/src/session/store.rs
Normal file
186
ecc2/src/session/store.rs
Normal file
@@ -0,0 +1,186 @@
|
|||||||
|
use anyhow::Result;
|
||||||
|
use rusqlite::Connection;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
use super::{Session, SessionMetrics, SessionState};
|
||||||
|
|
||||||
|
pub struct StateStore {
|
||||||
|
conn: Connection,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StateStore {
|
||||||
|
pub fn open(path: &Path) -> Result<Self> {
|
||||||
|
let conn = Connection::open(path)?;
|
||||||
|
let store = Self { conn };
|
||||||
|
store.init_schema()?;
|
||||||
|
Ok(store)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn init_schema(&self) -> Result<()> {
|
||||||
|
self.conn.execute_batch(
|
||||||
|
"
|
||||||
|
CREATE TABLE IF NOT EXISTS sessions (
|
||||||
|
id TEXT PRIMARY KEY,
|
||||||
|
task TEXT NOT NULL,
|
||||||
|
agent_type TEXT NOT NULL,
|
||||||
|
state TEXT NOT NULL DEFAULT 'pending',
|
||||||
|
worktree_path TEXT,
|
||||||
|
worktree_branch TEXT,
|
||||||
|
worktree_base TEXT,
|
||||||
|
tokens_used INTEGER DEFAULT 0,
|
||||||
|
tool_calls INTEGER DEFAULT 0,
|
||||||
|
files_changed INTEGER DEFAULT 0,
|
||||||
|
duration_secs INTEGER DEFAULT 0,
|
||||||
|
cost_usd REAL DEFAULT 0.0,
|
||||||
|
created_at TEXT NOT NULL,
|
||||||
|
updated_at TEXT NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS tool_log (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
session_id TEXT NOT NULL REFERENCES sessions(id),
|
||||||
|
tool_name TEXT NOT NULL,
|
||||||
|
input_summary TEXT,
|
||||||
|
output_summary TEXT,
|
||||||
|
duration_ms INTEGER,
|
||||||
|
risk_score REAL DEFAULT 0.0,
|
||||||
|
timestamp TEXT NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS messages (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
from_session TEXT NOT NULL,
|
||||||
|
to_session TEXT NOT NULL,
|
||||||
|
content TEXT NOT NULL,
|
||||||
|
msg_type TEXT NOT NULL DEFAULT 'info',
|
||||||
|
read INTEGER DEFAULT 0,
|
||||||
|
timestamp TEXT NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_sessions_state ON sessions(state);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_tool_log_session ON tool_log(session_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_messages_to ON messages(to_session, read);
|
||||||
|
",
|
||||||
|
)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn insert_session(&self, session: &Session) -> Result<()> {
|
||||||
|
self.conn.execute(
|
||||||
|
"INSERT INTO sessions (id, task, agent_type, state, worktree_path, worktree_branch, worktree_base, created_at, updated_at)
|
||||||
|
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)",
|
||||||
|
rusqlite::params![
|
||||||
|
session.id,
|
||||||
|
session.task,
|
||||||
|
session.agent_type,
|
||||||
|
session.state.to_string(),
|
||||||
|
session.worktree.as_ref().map(|w| w.path.to_string_lossy().to_string()),
|
||||||
|
session.worktree.as_ref().map(|w| w.branch.clone()),
|
||||||
|
session.worktree.as_ref().map(|w| w.base_branch.clone()),
|
||||||
|
session.created_at.to_rfc3339(),
|
||||||
|
session.updated_at.to_rfc3339(),
|
||||||
|
],
|
||||||
|
)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_state(&self, session_id: &str, state: &SessionState) -> Result<()> {
|
||||||
|
self.conn.execute(
|
||||||
|
"UPDATE sessions SET state = ?1, updated_at = ?2 WHERE id = ?3",
|
||||||
|
rusqlite::params![
|
||||||
|
state.to_string(),
|
||||||
|
chrono::Utc::now().to_rfc3339(),
|
||||||
|
session_id,
|
||||||
|
],
|
||||||
|
)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_metrics(&self, session_id: &str, metrics: &SessionMetrics) -> Result<()> {
|
||||||
|
self.conn.execute(
|
||||||
|
"UPDATE sessions SET tokens_used = ?1, tool_calls = ?2, files_changed = ?3, duration_secs = ?4, cost_usd = ?5, updated_at = ?6 WHERE id = ?7",
|
||||||
|
rusqlite::params![
|
||||||
|
metrics.tokens_used,
|
||||||
|
metrics.tool_calls,
|
||||||
|
metrics.files_changed,
|
||||||
|
metrics.duration_secs,
|
||||||
|
metrics.cost_usd,
|
||||||
|
chrono::Utc::now().to_rfc3339(),
|
||||||
|
session_id,
|
||||||
|
],
|
||||||
|
)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn list_sessions(&self) -> Result<Vec<Session>> {
|
||||||
|
let mut stmt = self.conn.prepare(
|
||||||
|
"SELECT id, task, agent_type, state, worktree_path, worktree_branch, worktree_base,
|
||||||
|
tokens_used, tool_calls, files_changed, duration_secs, cost_usd,
|
||||||
|
created_at, updated_at
|
||||||
|
FROM sessions ORDER BY updated_at DESC",
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let sessions = stmt
|
||||||
|
.query_map([], |row| {
|
||||||
|
let state_str: String = row.get(3)?;
|
||||||
|
let state = match state_str.as_str() {
|
||||||
|
"running" => SessionState::Running,
|
||||||
|
"idle" => SessionState::Idle,
|
||||||
|
"completed" => SessionState::Completed,
|
||||||
|
"failed" => SessionState::Failed,
|
||||||
|
"stopped" => SessionState::Stopped,
|
||||||
|
_ => SessionState::Pending,
|
||||||
|
};
|
||||||
|
|
||||||
|
let worktree_path: Option<String> = row.get(4)?;
|
||||||
|
let worktree = worktree_path.map(|p| super::WorktreeInfo {
|
||||||
|
path: std::path::PathBuf::from(p),
|
||||||
|
branch: row.get::<_, String>(5).unwrap_or_default(),
|
||||||
|
base_branch: row.get::<_, String>(6).unwrap_or_default(),
|
||||||
|
});
|
||||||
|
|
||||||
|
let created_str: String = row.get(12)?;
|
||||||
|
let updated_str: String = row.get(13)?;
|
||||||
|
|
||||||
|
Ok(Session {
|
||||||
|
id: row.get(0)?,
|
||||||
|
task: row.get(1)?,
|
||||||
|
agent_type: row.get(2)?,
|
||||||
|
state,
|
||||||
|
worktree,
|
||||||
|
created_at: chrono::DateTime::parse_from_rfc3339(&created_str)
|
||||||
|
.unwrap_or_default()
|
||||||
|
.with_timezone(&chrono::Utc),
|
||||||
|
updated_at: chrono::DateTime::parse_from_rfc3339(&updated_str)
|
||||||
|
.unwrap_or_default()
|
||||||
|
.with_timezone(&chrono::Utc),
|
||||||
|
metrics: SessionMetrics {
|
||||||
|
tokens_used: row.get(7)?,
|
||||||
|
tool_calls: row.get(8)?,
|
||||||
|
files_changed: row.get(9)?,
|
||||||
|
duration_secs: row.get(10)?,
|
||||||
|
cost_usd: row.get(11)?,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})?
|
||||||
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
|
|
||||||
|
Ok(sessions)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_session(&self, id: &str) -> Result<Option<Session>> {
|
||||||
|
let sessions = self.list_sessions()?;
|
||||||
|
Ok(sessions
|
||||||
|
.into_iter()
|
||||||
|
.find(|s| s.id == id || s.id.starts_with(id)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn send_message(&self, from: &str, to: &str, content: &str, msg_type: &str) -> Result<()> {
|
||||||
|
self.conn.execute(
|
||||||
|
"INSERT INTO messages (from_session, to_session, content, msg_type, timestamp)
|
||||||
|
VALUES (?1, ?2, ?3, ?4, ?5)",
|
||||||
|
rusqlite::params![from, to, content, msg_type, chrono::Utc::now().to_rfc3339()],
|
||||||
|
)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
52
ecc2/src/tui/app.rs
Normal file
52
ecc2/src/tui/app.rs
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
use anyhow::Result;
|
||||||
|
use crossterm::{
|
||||||
|
event::{self, Event, KeyCode, KeyModifiers},
|
||||||
|
execute,
|
||||||
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
||||||
|
};
|
||||||
|
use ratatui::prelude::*;
|
||||||
|
use std::io;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use super::dashboard::Dashboard;
|
||||||
|
use crate::config::Config;
|
||||||
|
use crate::session::store::StateStore;
|
||||||
|
|
||||||
|
pub async fn run(db: StateStore, cfg: Config) -> Result<()> {
|
||||||
|
enable_raw_mode()?;
|
||||||
|
let mut stdout = io::stdout();
|
||||||
|
execute!(stdout, EnterAlternateScreen)?;
|
||||||
|
|
||||||
|
let backend = CrosstermBackend::new(stdout);
|
||||||
|
let mut terminal = Terminal::new(backend)?;
|
||||||
|
|
||||||
|
let mut dashboard = Dashboard::new(db, cfg);
|
||||||
|
|
||||||
|
loop {
|
||||||
|
terminal.draw(|frame| dashboard.render(frame))?;
|
||||||
|
|
||||||
|
if event::poll(Duration::from_millis(250))? {
|
||||||
|
if let Event::Key(key) = event::read()? {
|
||||||
|
match (key.modifiers, key.code) {
|
||||||
|
(KeyModifiers::CONTROL, KeyCode::Char('c')) => break,
|
||||||
|
(_, KeyCode::Char('q')) => break,
|
||||||
|
(_, KeyCode::Tab) => dashboard.next_pane(),
|
||||||
|
(KeyModifiers::SHIFT, KeyCode::BackTab) => dashboard.prev_pane(),
|
||||||
|
(_, KeyCode::Char('j')) | (_, KeyCode::Down) => dashboard.scroll_down(),
|
||||||
|
(_, KeyCode::Char('k')) | (_, KeyCode::Up) => dashboard.scroll_up(),
|
||||||
|
(_, KeyCode::Char('n')) => dashboard.new_session(),
|
||||||
|
(_, KeyCode::Char('s')) => dashboard.stop_selected(),
|
||||||
|
(_, KeyCode::Char('r')) => dashboard.refresh(),
|
||||||
|
(_, KeyCode::Char('?')) => dashboard.toggle_help(),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dashboard.tick().await;
|
||||||
|
}
|
||||||
|
|
||||||
|
disable_raw_mode()?;
|
||||||
|
execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
490
ecc2/src/tui/dashboard.rs
Normal file
490
ecc2/src/tui/dashboard.rs
Normal file
@@ -0,0 +1,490 @@
|
|||||||
|
use ratatui::{
|
||||||
|
prelude::*,
|
||||||
|
widgets::{Block, Borders, List, ListItem, Paragraph, Tabs, Wrap},
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::widgets::{budget_state, format_currency, format_token_count, BudgetState, TokenMeter};
|
||||||
|
use crate::config::Config;
|
||||||
|
use crate::session::store::StateStore;
|
||||||
|
use crate::session::{Session, SessionState};
|
||||||
|
|
||||||
|
pub struct Dashboard {
|
||||||
|
db: StateStore,
|
||||||
|
cfg: Config,
|
||||||
|
sessions: Vec<Session>,
|
||||||
|
selected_pane: Pane,
|
||||||
|
selected_session: usize,
|
||||||
|
show_help: bool,
|
||||||
|
scroll_offset: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
|
enum Pane {
|
||||||
|
Sessions,
|
||||||
|
Output,
|
||||||
|
Metrics,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
struct AggregateUsage {
|
||||||
|
total_tokens: u64,
|
||||||
|
total_cost_usd: f64,
|
||||||
|
token_state: BudgetState,
|
||||||
|
cost_state: BudgetState,
|
||||||
|
overall_state: BudgetState,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Dashboard {
|
||||||
|
pub fn new(db: StateStore, cfg: Config) -> Self {
|
||||||
|
let sessions = db.list_sessions().unwrap_or_default();
|
||||||
|
Self {
|
||||||
|
db,
|
||||||
|
cfg,
|
||||||
|
sessions,
|
||||||
|
selected_pane: Pane::Sessions,
|
||||||
|
selected_session: 0,
|
||||||
|
show_help: false,
|
||||||
|
scroll_offset: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn render(&self, frame: &mut Frame) {
|
||||||
|
let chunks = Layout::default()
|
||||||
|
.direction(Direction::Vertical)
|
||||||
|
.constraints([
|
||||||
|
Constraint::Length(3), // Header
|
||||||
|
Constraint::Min(10), // Main content
|
||||||
|
Constraint::Length(3), // Status bar
|
||||||
|
])
|
||||||
|
.split(frame.area());
|
||||||
|
|
||||||
|
self.render_header(frame, chunks[0]);
|
||||||
|
|
||||||
|
if self.show_help {
|
||||||
|
self.render_help(frame, chunks[1]);
|
||||||
|
} else {
|
||||||
|
let main_chunks = Layout::default()
|
||||||
|
.direction(Direction::Horizontal)
|
||||||
|
.constraints([
|
||||||
|
Constraint::Percentage(35), // Session list
|
||||||
|
Constraint::Percentage(65), // Output/details
|
||||||
|
])
|
||||||
|
.split(chunks[1]);
|
||||||
|
|
||||||
|
self.render_sessions(frame, main_chunks[0]);
|
||||||
|
|
||||||
|
let right_chunks = Layout::default()
|
||||||
|
.direction(Direction::Vertical)
|
||||||
|
.constraints([
|
||||||
|
Constraint::Percentage(70), // Output
|
||||||
|
Constraint::Percentage(30), // Metrics
|
||||||
|
])
|
||||||
|
.split(main_chunks[1]);
|
||||||
|
|
||||||
|
self.render_output(frame, right_chunks[0]);
|
||||||
|
self.render_metrics(frame, right_chunks[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.render_status_bar(frame, chunks[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_header(&self, frame: &mut Frame, area: Rect) {
|
||||||
|
let running = self
|
||||||
|
.sessions
|
||||||
|
.iter()
|
||||||
|
.filter(|s| s.state == SessionState::Running)
|
||||||
|
.count();
|
||||||
|
let total = self.sessions.len();
|
||||||
|
|
||||||
|
let title = format!(" ECC 2.0 | {running} running / {total} total ");
|
||||||
|
let tabs = Tabs::new(vec!["Sessions", "Output", "Metrics"])
|
||||||
|
.block(Block::default().borders(Borders::ALL).title(title))
|
||||||
|
.select(match self.selected_pane {
|
||||||
|
Pane::Sessions => 0,
|
||||||
|
Pane::Output => 1,
|
||||||
|
Pane::Metrics => 2,
|
||||||
|
})
|
||||||
|
.highlight_style(
|
||||||
|
Style::default()
|
||||||
|
.fg(Color::Cyan)
|
||||||
|
.add_modifier(Modifier::BOLD),
|
||||||
|
);
|
||||||
|
|
||||||
|
frame.render_widget(tabs, area);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_sessions(&self, frame: &mut Frame, area: Rect) {
|
||||||
|
let items: Vec<ListItem> = self
|
||||||
|
.sessions
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, s)| {
|
||||||
|
let state_icon = match s.state {
|
||||||
|
SessionState::Running => "●",
|
||||||
|
SessionState::Idle => "○",
|
||||||
|
SessionState::Completed => "✓",
|
||||||
|
SessionState::Failed => "✗",
|
||||||
|
SessionState::Stopped => "■",
|
||||||
|
SessionState::Pending => "◌",
|
||||||
|
};
|
||||||
|
let style = if i == self.selected_session {
|
||||||
|
Style::default()
|
||||||
|
.fg(Color::Cyan)
|
||||||
|
.add_modifier(Modifier::BOLD)
|
||||||
|
} else {
|
||||||
|
Style::default()
|
||||||
|
};
|
||||||
|
let text = format!(
|
||||||
|
"{state_icon} {} [{}] {}",
|
||||||
|
&s.id[..8.min(s.id.len())],
|
||||||
|
s.agent_type,
|
||||||
|
s.task
|
||||||
|
);
|
||||||
|
ListItem::new(text).style(style)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let border_style = if self.selected_pane == Pane::Sessions {
|
||||||
|
Style::default().fg(Color::Cyan)
|
||||||
|
} else {
|
||||||
|
Style::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
let list = List::new(items).block(
|
||||||
|
Block::default()
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.title(" Sessions ")
|
||||||
|
.border_style(border_style),
|
||||||
|
);
|
||||||
|
frame.render_widget(list, area);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_output(&self, frame: &mut Frame, area: Rect) {
|
||||||
|
let content = if let Some(session) = self.sessions.get(self.selected_session) {
|
||||||
|
format!(
|
||||||
|
"Agent output for session {}...\n\n(Live streaming coming soon)",
|
||||||
|
session.id
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
"No sessions. Press 'n' to start one.".to_string()
|
||||||
|
};
|
||||||
|
|
||||||
|
let border_style = if self.selected_pane == Pane::Output {
|
||||||
|
Style::default().fg(Color::Cyan)
|
||||||
|
} else {
|
||||||
|
Style::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
let paragraph = Paragraph::new(content).block(
|
||||||
|
Block::default()
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.title(" Output ")
|
||||||
|
.border_style(border_style),
|
||||||
|
);
|
||||||
|
frame.render_widget(paragraph, area);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_metrics(&self, frame: &mut Frame, area: Rect) {
|
||||||
|
let border_style = if self.selected_pane == Pane::Metrics {
|
||||||
|
Style::default().fg(Color::Cyan)
|
||||||
|
} else {
|
||||||
|
Style::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
let block = Block::default()
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.title(" Metrics ")
|
||||||
|
.border_style(border_style);
|
||||||
|
let inner = block.inner(area);
|
||||||
|
frame.render_widget(block, area);
|
||||||
|
|
||||||
|
if inner.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let chunks = Layout::default()
|
||||||
|
.direction(Direction::Vertical)
|
||||||
|
.constraints([
|
||||||
|
Constraint::Length(2),
|
||||||
|
Constraint::Length(2),
|
||||||
|
Constraint::Min(1),
|
||||||
|
])
|
||||||
|
.split(inner);
|
||||||
|
|
||||||
|
let aggregate = self.aggregate_usage();
|
||||||
|
frame.render_widget(
|
||||||
|
TokenMeter::tokens(
|
||||||
|
"Token Budget",
|
||||||
|
aggregate.total_tokens,
|
||||||
|
self.cfg.token_budget,
|
||||||
|
),
|
||||||
|
chunks[0],
|
||||||
|
);
|
||||||
|
frame.render_widget(
|
||||||
|
TokenMeter::currency(
|
||||||
|
"Cost Budget",
|
||||||
|
aggregate.total_cost_usd,
|
||||||
|
self.cfg.cost_budget_usd,
|
||||||
|
),
|
||||||
|
chunks[1],
|
||||||
|
);
|
||||||
|
frame.render_widget(
|
||||||
|
Paragraph::new(self.selected_session_metrics_text()).wrap(Wrap { trim: true }),
|
||||||
|
chunks[2],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_status_bar(&self, frame: &mut Frame, area: Rect) {
|
||||||
|
let text = " [n]ew session [s]top [Tab] switch pane [j/k] scroll [?] help [q]uit ";
|
||||||
|
let aggregate = self.aggregate_usage();
|
||||||
|
let (summary_text, summary_style) = self.aggregate_cost_summary();
|
||||||
|
let block = Block::default()
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.border_style(aggregate.overall_state.style());
|
||||||
|
let inner = block.inner(area);
|
||||||
|
frame.render_widget(block, area);
|
||||||
|
|
||||||
|
if inner.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let summary_width = summary_text
|
||||||
|
.len()
|
||||||
|
.min(inner.width.saturating_sub(1) as usize) as u16;
|
||||||
|
let chunks = Layout::default()
|
||||||
|
.direction(Direction::Horizontal)
|
||||||
|
.constraints([Constraint::Min(1), Constraint::Length(summary_width)])
|
||||||
|
.split(inner);
|
||||||
|
|
||||||
|
frame.render_widget(
|
||||||
|
Paragraph::new(text).style(Style::default().fg(Color::DarkGray)),
|
||||||
|
chunks[0],
|
||||||
|
);
|
||||||
|
frame.render_widget(
|
||||||
|
Paragraph::new(summary_text)
|
||||||
|
.style(summary_style)
|
||||||
|
.alignment(Alignment::Right),
|
||||||
|
chunks[1],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_help(&self, frame: &mut Frame, area: Rect) {
|
||||||
|
let help = vec![
|
||||||
|
"Keyboard Shortcuts:",
|
||||||
|
"",
|
||||||
|
" n New session",
|
||||||
|
" s Stop selected session",
|
||||||
|
" Tab Next pane",
|
||||||
|
" S-Tab Previous pane",
|
||||||
|
" j/↓ Scroll down",
|
||||||
|
" k/↑ Scroll up",
|
||||||
|
" r Refresh",
|
||||||
|
" ? Toggle help",
|
||||||
|
" q/C-c Quit",
|
||||||
|
];
|
||||||
|
|
||||||
|
let paragraph = Paragraph::new(help.join("\n")).block(
|
||||||
|
Block::default()
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.title(" Help ")
|
||||||
|
.border_style(Style::default().fg(Color::Yellow)),
|
||||||
|
);
|
||||||
|
frame.render_widget(paragraph, area);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn next_pane(&mut self) {
|
||||||
|
self.selected_pane = match self.selected_pane {
|
||||||
|
Pane::Sessions => Pane::Output,
|
||||||
|
Pane::Output => Pane::Metrics,
|
||||||
|
Pane::Metrics => Pane::Sessions,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn prev_pane(&mut self) {
|
||||||
|
self.selected_pane = match self.selected_pane {
|
||||||
|
Pane::Sessions => Pane::Metrics,
|
||||||
|
Pane::Output => Pane::Sessions,
|
||||||
|
Pane::Metrics => Pane::Output,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn scroll_down(&mut self) {
|
||||||
|
if self.selected_pane == Pane::Sessions && !self.sessions.is_empty() {
|
||||||
|
self.selected_session = (self.selected_session + 1).min(self.sessions.len() - 1);
|
||||||
|
} else {
|
||||||
|
self.scroll_offset = self.scroll_offset.saturating_add(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn scroll_up(&mut self) {
|
||||||
|
if self.selected_pane == Pane::Sessions {
|
||||||
|
self.selected_session = self.selected_session.saturating_sub(1);
|
||||||
|
} else {
|
||||||
|
self.scroll_offset = self.scroll_offset.saturating_sub(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_session(&mut self) {
|
||||||
|
// TODO: Open a dialog to create a new session
|
||||||
|
tracing::info!("New session dialog requested");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn stop_selected(&mut self) {
|
||||||
|
if let Some(session) = self.sessions.get(self.selected_session) {
|
||||||
|
let _ = self.db.update_state(&session.id, &SessionState::Stopped);
|
||||||
|
self.refresh();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn refresh(&mut self) {
|
||||||
|
self.sessions = self.db.list_sessions().unwrap_or_default();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn toggle_help(&mut self) {
|
||||||
|
self.show_help = !self.show_help;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn tick(&mut self) {
|
||||||
|
// Periodic refresh every few ticks
|
||||||
|
self.sessions = self.db.list_sessions().unwrap_or_default();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn aggregate_usage(&self) -> AggregateUsage {
|
||||||
|
let total_tokens = self
|
||||||
|
.sessions
|
||||||
|
.iter()
|
||||||
|
.map(|session| session.metrics.tokens_used)
|
||||||
|
.sum();
|
||||||
|
let total_cost_usd = self
|
||||||
|
.sessions
|
||||||
|
.iter()
|
||||||
|
.map(|session| session.metrics.cost_usd)
|
||||||
|
.sum::<f64>();
|
||||||
|
let token_state = budget_state(total_tokens as f64, self.cfg.token_budget as f64);
|
||||||
|
let cost_state = budget_state(total_cost_usd, self.cfg.cost_budget_usd);
|
||||||
|
|
||||||
|
AggregateUsage {
|
||||||
|
total_tokens,
|
||||||
|
total_cost_usd,
|
||||||
|
token_state,
|
||||||
|
cost_state,
|
||||||
|
overall_state: token_state.max(cost_state),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn selected_session_metrics_text(&self) -> String {
|
||||||
|
if let Some(session) = self.sessions.get(self.selected_session) {
|
||||||
|
let metrics = &session.metrics;
|
||||||
|
format!(
|
||||||
|
"Selected {} [{}]\nTokens {} | Tools {} | Files {}\nCost ${:.4} | Duration {}s",
|
||||||
|
&session.id[..8.min(session.id.len())],
|
||||||
|
session.state,
|
||||||
|
format_token_count(metrics.tokens_used),
|
||||||
|
metrics.tool_calls,
|
||||||
|
metrics.files_changed,
|
||||||
|
metrics.cost_usd,
|
||||||
|
metrics.duration_secs
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
"No metrics available".to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn aggregate_cost_summary(&self) -> (String, Style) {
|
||||||
|
let aggregate = self.aggregate_usage();
|
||||||
|
let mut text = if self.cfg.cost_budget_usd > 0.0 {
|
||||||
|
format!(
|
||||||
|
"Aggregate cost {} / {}",
|
||||||
|
format_currency(aggregate.total_cost_usd),
|
||||||
|
format_currency(self.cfg.cost_budget_usd),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
format!(
|
||||||
|
"Aggregate cost {} (no budget)",
|
||||||
|
format_currency(aggregate.total_cost_usd)
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
match aggregate.overall_state {
|
||||||
|
BudgetState::Warning => text.push_str(" | Budget warning"),
|
||||||
|
BudgetState::OverBudget => text.push_str(" | Budget exceeded"),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
(text, aggregate.overall_state.style())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn aggregate_cost_summary_text(&self) -> String {
|
||||||
|
self.aggregate_cost_summary().0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
use chrono::Utc;
|
||||||
|
|
||||||
|
use super::Dashboard;
|
||||||
|
use crate::config::Config;
|
||||||
|
use crate::session::store::StateStore;
|
||||||
|
use crate::session::{Session, SessionMetrics, SessionState};
|
||||||
|
use crate::tui::widgets::BudgetState;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn aggregate_usage_sums_tokens_and_cost_with_warning_state() {
|
||||||
|
let db = StateStore::open(Path::new(":memory:")).unwrap();
|
||||||
|
let mut cfg = Config::default();
|
||||||
|
cfg.token_budget = 10_000;
|
||||||
|
cfg.cost_budget_usd = 10.0;
|
||||||
|
|
||||||
|
let mut dashboard = Dashboard::new(db, cfg);
|
||||||
|
dashboard.sessions = vec![
|
||||||
|
session("sess-1", 4_000, 3.50),
|
||||||
|
session("sess-2", 4_500, 4.80),
|
||||||
|
];
|
||||||
|
|
||||||
|
let aggregate = dashboard.aggregate_usage();
|
||||||
|
|
||||||
|
assert_eq!(aggregate.total_tokens, 8_500);
|
||||||
|
assert!((aggregate.total_cost_usd - 8.30).abs() < 1e-9);
|
||||||
|
assert_eq!(aggregate.token_state, BudgetState::Warning);
|
||||||
|
assert_eq!(aggregate.cost_state, BudgetState::Warning);
|
||||||
|
assert_eq!(aggregate.overall_state, BudgetState::Warning);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn aggregate_cost_summary_mentions_total_cost() {
|
||||||
|
let db = StateStore::open(Path::new(":memory:")).unwrap();
|
||||||
|
let mut cfg = Config::default();
|
||||||
|
cfg.cost_budget_usd = 10.0;
|
||||||
|
|
||||||
|
let mut dashboard = Dashboard::new(db, cfg);
|
||||||
|
dashboard.sessions = vec![session("sess-1", 3_500, 8.25)];
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
dashboard.aggregate_cost_summary_text(),
|
||||||
|
"Aggregate cost $8.25 / $10.00 | Budget warning"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn session(id: &str, tokens_used: u64, cost_usd: f64) -> Session {
|
||||||
|
let now = Utc::now();
|
||||||
|
Session {
|
||||||
|
id: id.to_string(),
|
||||||
|
task: "Budget tracking".to_string(),
|
||||||
|
agent_type: "claude".to_string(),
|
||||||
|
state: SessionState::Running,
|
||||||
|
worktree: None,
|
||||||
|
created_at: now,
|
||||||
|
updated_at: now,
|
||||||
|
metrics: SessionMetrics {
|
||||||
|
tokens_used,
|
||||||
|
tool_calls: 0,
|
||||||
|
files_changed: 0,
|
||||||
|
duration_secs: 0,
|
||||||
|
cost_usd,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
3
ecc2/src/tui/mod.rs
Normal file
3
ecc2/src/tui/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
pub mod app;
|
||||||
|
mod dashboard;
|
||||||
|
mod widgets;
|
||||||
281
ecc2/src/tui/widgets.rs
Normal file
281
ecc2/src/tui/widgets.rs
Normal file
@@ -0,0 +1,281 @@
|
|||||||
|
use ratatui::{
|
||||||
|
prelude::*,
|
||||||
|
text::{Line, Span},
|
||||||
|
widgets::{Gauge, Paragraph, Widget},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub(crate) const WARNING_THRESHOLD: f64 = 0.8;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
pub(crate) enum BudgetState {
|
||||||
|
Unconfigured,
|
||||||
|
Normal,
|
||||||
|
Warning,
|
||||||
|
OverBudget,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BudgetState {
|
||||||
|
pub(crate) const fn is_warning(self) -> bool {
|
||||||
|
matches!(self, Self::Warning | Self::OverBudget)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn badge(self) -> Option<&'static str> {
|
||||||
|
match self {
|
||||||
|
Self::Warning => Some("warning"),
|
||||||
|
Self::OverBudget => Some("over budget"),
|
||||||
|
Self::Unconfigured => Some("no budget"),
|
||||||
|
Self::Normal => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn style(self) -> Style {
|
||||||
|
let base = Style::default().fg(match self {
|
||||||
|
Self::Unconfigured => Color::DarkGray,
|
||||||
|
Self::Normal => Color::DarkGray,
|
||||||
|
Self::Warning => Color::Yellow,
|
||||||
|
Self::OverBudget => Color::Red,
|
||||||
|
});
|
||||||
|
|
||||||
|
if self.is_warning() {
|
||||||
|
base.add_modifier(Modifier::BOLD)
|
||||||
|
} else {
|
||||||
|
base
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
enum MeterFormat {
|
||||||
|
Tokens,
|
||||||
|
Currency,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub(crate) struct TokenMeter<'a> {
|
||||||
|
title: &'a str,
|
||||||
|
used: f64,
|
||||||
|
budget: f64,
|
||||||
|
format: MeterFormat,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> TokenMeter<'a> {
|
||||||
|
pub(crate) fn tokens(title: &'a str, used: u64, budget: u64) -> Self {
|
||||||
|
Self {
|
||||||
|
title,
|
||||||
|
used: used as f64,
|
||||||
|
budget: budget as f64,
|
||||||
|
format: MeterFormat::Tokens,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn currency(title: &'a str, used: f64, budget: f64) -> Self {
|
||||||
|
Self {
|
||||||
|
title,
|
||||||
|
used,
|
||||||
|
budget,
|
||||||
|
format: MeterFormat::Currency,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn state(&self) -> BudgetState {
|
||||||
|
budget_state(self.used, self.budget)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ratio(&self) -> f64 {
|
||||||
|
budget_ratio(self.used, self.budget)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn clamped_ratio(&self) -> f64 {
|
||||||
|
self.ratio().clamp(0.0, 1.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn title_line(&self) -> Line<'static> {
|
||||||
|
let mut spans = vec![Span::styled(
|
||||||
|
self.title.to_string(),
|
||||||
|
Style::default()
|
||||||
|
.fg(Color::Gray)
|
||||||
|
.add_modifier(Modifier::BOLD),
|
||||||
|
)];
|
||||||
|
|
||||||
|
if let Some(badge) = self.state().badge() {
|
||||||
|
spans.push(Span::raw(" "));
|
||||||
|
spans.push(Span::styled(format!("[{badge}]"), self.state().style()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Line::from(spans)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn display_label(&self) -> String {
|
||||||
|
if self.budget <= 0.0 {
|
||||||
|
return match self.format {
|
||||||
|
MeterFormat::Tokens => format!("{} tok used | no budget", self.used_label()),
|
||||||
|
MeterFormat::Currency => format!("{} spent | no budget", self.used_label()),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
format!(
|
||||||
|
"{} / {}{} ({}%)",
|
||||||
|
self.used_label(),
|
||||||
|
self.budget_label(),
|
||||||
|
self.unit_suffix(),
|
||||||
|
(self.ratio() * 100.0).round() as u64
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn used_label(&self) -> String {
|
||||||
|
match self.format {
|
||||||
|
MeterFormat::Tokens => format_token_count(self.used.max(0.0).round() as u64),
|
||||||
|
MeterFormat::Currency => format_currency(self.used.max(0.0)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn budget_label(&self) -> String {
|
||||||
|
match self.format {
|
||||||
|
MeterFormat::Tokens => format_token_count(self.budget.max(0.0).round() as u64),
|
||||||
|
MeterFormat::Currency => format_currency(self.budget.max(0.0)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn unit_suffix(&self) -> &'static str {
|
||||||
|
match self.format {
|
||||||
|
MeterFormat::Tokens => " tok",
|
||||||
|
MeterFormat::Currency => "",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Widget for TokenMeter<'_> {
|
||||||
|
fn render(self, area: Rect, buf: &mut Buffer) {
|
||||||
|
if area.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut gauge_area = area;
|
||||||
|
if area.height > 1 {
|
||||||
|
let chunks = Layout::default()
|
||||||
|
.direction(Direction::Vertical)
|
||||||
|
.constraints([Constraint::Length(1), Constraint::Min(1)])
|
||||||
|
.split(area);
|
||||||
|
Paragraph::new(self.title_line()).render(chunks[0], buf);
|
||||||
|
gauge_area = chunks[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
Gauge::default()
|
||||||
|
.ratio(self.clamped_ratio())
|
||||||
|
.label(self.display_label())
|
||||||
|
.gauge_style(
|
||||||
|
Style::default()
|
||||||
|
.fg(gradient_color(self.ratio()))
|
||||||
|
.add_modifier(Modifier::BOLD),
|
||||||
|
)
|
||||||
|
.style(Style::default().fg(Color::DarkGray))
|
||||||
|
.use_unicode(true)
|
||||||
|
.render(gauge_area, buf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn budget_ratio(used: f64, budget: f64) -> f64 {
|
||||||
|
if budget <= 0.0 {
|
||||||
|
0.0
|
||||||
|
} else {
|
||||||
|
used / budget
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn budget_state(used: f64, budget: f64) -> BudgetState {
|
||||||
|
if budget <= 0.0 {
|
||||||
|
BudgetState::Unconfigured
|
||||||
|
} else if used / budget >= 1.0 {
|
||||||
|
BudgetState::OverBudget
|
||||||
|
} else if used / budget >= WARNING_THRESHOLD {
|
||||||
|
BudgetState::Warning
|
||||||
|
} else {
|
||||||
|
BudgetState::Normal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn gradient_color(ratio: f64) -> Color {
|
||||||
|
const GREEN: (u8, u8, u8) = (34, 197, 94);
|
||||||
|
const YELLOW: (u8, u8, u8) = (234, 179, 8);
|
||||||
|
const RED: (u8, u8, u8) = (239, 68, 68);
|
||||||
|
|
||||||
|
let clamped = ratio.clamp(0.0, 1.0);
|
||||||
|
if clamped <= WARNING_THRESHOLD {
|
||||||
|
interpolate_rgb(GREEN, YELLOW, clamped / WARNING_THRESHOLD)
|
||||||
|
} else {
|
||||||
|
interpolate_rgb(
|
||||||
|
YELLOW,
|
||||||
|
RED,
|
||||||
|
(clamped - WARNING_THRESHOLD) / (1.0 - WARNING_THRESHOLD),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn format_currency(value: f64) -> String {
|
||||||
|
format!("${value:.2}")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn format_token_count(value: u64) -> String {
|
||||||
|
let digits = value.to_string();
|
||||||
|
let mut formatted = String::with_capacity(digits.len() + digits.len() / 3);
|
||||||
|
|
||||||
|
for (index, ch) in digits.chars().rev().enumerate() {
|
||||||
|
if index != 0 && index % 3 == 0 {
|
||||||
|
formatted.push(',');
|
||||||
|
}
|
||||||
|
formatted.push(ch);
|
||||||
|
}
|
||||||
|
|
||||||
|
formatted.chars().rev().collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn interpolate_rgb(from: (u8, u8, u8), to: (u8, u8, u8), ratio: f64) -> Color {
|
||||||
|
let ratio = ratio.clamp(0.0, 1.0);
|
||||||
|
let channel = |start: u8, end: u8| -> u8 {
|
||||||
|
(f64::from(start) + (f64::from(end) - f64::from(start)) * ratio).round() as u8
|
||||||
|
};
|
||||||
|
|
||||||
|
Color::Rgb(
|
||||||
|
channel(from.0, to.0),
|
||||||
|
channel(from.1, to.1),
|
||||||
|
channel(from.2, to.2),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use ratatui::{buffer::Buffer, layout::Rect, style::Color, widgets::Widget};
|
||||||
|
|
||||||
|
use super::{gradient_color, BudgetState, TokenMeter};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn warning_state_starts_at_eighty_percent() {
|
||||||
|
let meter = TokenMeter::tokens("Token Budget", 80, 100);
|
||||||
|
|
||||||
|
assert_eq!(meter.state(), BudgetState::Warning);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn gradient_runs_from_green_to_yellow_to_red() {
|
||||||
|
assert_eq!(gradient_color(0.0), Color::Rgb(34, 197, 94));
|
||||||
|
assert_eq!(gradient_color(0.8), Color::Rgb(234, 179, 8));
|
||||||
|
assert_eq!(gradient_color(1.0), Color::Rgb(239, 68, 68));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn token_meter_renders_compact_usage_label() {
|
||||||
|
let meter = TokenMeter::tokens("Token Budget", 4_000, 10_000);
|
||||||
|
let area = Rect::new(0, 0, 48, 2);
|
||||||
|
let mut buffer = Buffer::empty(area);
|
||||||
|
|
||||||
|
meter.render(area, &mut buffer);
|
||||||
|
|
||||||
|
let rendered = buffer
|
||||||
|
.content()
|
||||||
|
.chunks(area.width as usize)
|
||||||
|
.flat_map(|row| row.iter().map(|cell| cell.symbol()))
|
||||||
|
.collect::<String>();
|
||||||
|
|
||||||
|
assert!(rendered.contains("4,000 / 10,000 tok (40%)"));
|
||||||
|
}
|
||||||
|
}
|
||||||
84
ecc2/src/worktree/mod.rs
Normal file
84
ecc2/src/worktree/mod.rs
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
use anyhow::{Context, Result};
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
|
use crate::config::Config;
|
||||||
|
use crate::session::WorktreeInfo;
|
||||||
|
|
||||||
|
/// Create a new git worktree for an agent session.
|
||||||
|
pub fn create_for_session(session_id: &str, cfg: &Config) -> Result<WorktreeInfo> {
|
||||||
|
let branch = format!("ecc/{session_id}");
|
||||||
|
let path = cfg.worktree_root.join(session_id);
|
||||||
|
|
||||||
|
// Get current branch as base
|
||||||
|
let base = get_current_branch()?;
|
||||||
|
|
||||||
|
std::fs::create_dir_all(&cfg.worktree_root)
|
||||||
|
.context("Failed to create worktree root directory")?;
|
||||||
|
|
||||||
|
let output = Command::new("git")
|
||||||
|
.args(["worktree", "add", "-b", &branch])
|
||||||
|
.arg(&path)
|
||||||
|
.arg("HEAD")
|
||||||
|
.output()
|
||||||
|
.context("Failed to run git worktree add")?;
|
||||||
|
|
||||||
|
if !output.status.success() {
|
||||||
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||||
|
anyhow::bail!("git worktree add failed: {stderr}");
|
||||||
|
}
|
||||||
|
|
||||||
|
tracing::info!(
|
||||||
|
"Created worktree at {} on branch {}",
|
||||||
|
path.display(),
|
||||||
|
branch
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(WorktreeInfo {
|
||||||
|
path,
|
||||||
|
branch,
|
||||||
|
base_branch: base,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Remove a worktree and its branch.
|
||||||
|
pub fn remove(path: &PathBuf) -> Result<()> {
|
||||||
|
let output = Command::new("git")
|
||||||
|
.args(["worktree", "remove", "--force"])
|
||||||
|
.arg(path)
|
||||||
|
.output()
|
||||||
|
.context("Failed to remove worktree")?;
|
||||||
|
|
||||||
|
if !output.status.success() {
|
||||||
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||||
|
tracing::warn!("Worktree removal warning: {stderr}");
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// List all active worktrees.
|
||||||
|
pub fn list() -> Result<Vec<String>> {
|
||||||
|
let output = Command::new("git")
|
||||||
|
.args(["worktree", "list", "--porcelain"])
|
||||||
|
.output()
|
||||||
|
.context("Failed to list worktrees")?;
|
||||||
|
|
||||||
|
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||||
|
let worktrees: Vec<String> = stdout
|
||||||
|
.lines()
|
||||||
|
.filter(|l| l.starts_with("worktree "))
|
||||||
|
.map(|l| l.trim_start_matches("worktree ").to_string())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
Ok(worktrees)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_current_branch() -> Result<String> {
|
||||||
|
let output = Command::new("git")
|
||||||
|
.args(["rev-parse", "--abbrev-ref", "HEAD"])
|
||||||
|
.output()
|
||||||
|
.context("Failed to get current branch")?;
|
||||||
|
|
||||||
|
Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user