mod agent_log; mod agents; mod config; mod http; mod io; mod llm; pub mod log_buffer; mod state; mod store; mod workflow; mod worktree; use crate::agents::AgentPool; use crate::http::build_routes; use crate::http::context::AppContext; use crate::http::{remove_port_file, resolve_port, write_port_file}; use crate::io::fs::find_story_kit_root; use crate::state::SessionState; use crate::store::JsonFileStore; use crate::workflow::WorkflowState; use poem::Server; use poem::listener::TcpListener; use std::path::PathBuf; use std::sync::Arc; use tokio::sync::broadcast; #[tokio::main] async fn main() -> Result<(), std::io::Error> { let app_state = Arc::new(SessionState::default()); let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")); let store = Arc::new( JsonFileStore::from_path(PathBuf::from("store.json")).map_err(std::io::Error::other)?, ); // Auto-detect a .story_kit/ project in cwd or parent directories. if let Some(project_root) = find_story_kit_root(&cwd) { io::fs::open_project( project_root.to_string_lossy().to_string(), &app_state, store.as_ref(), ) .await .unwrap_or_else(|e| { slog!("Warning: failed to auto-open project at {project_root:?}: {e}"); project_root.to_string_lossy().to_string() }); // Validate agent config for the detected project root. config::ProjectConfig::load(&project_root) .unwrap_or_else(|e| panic!("Invalid project.toml: {e}")); } else { // No .story_kit/ found — fall back to cwd so existing behaviour is preserved. *app_state.project_root.lock().unwrap() = Some(cwd.clone()); } let workflow = Arc::new(std::sync::Mutex::new(WorkflowState::default())); let port = resolve_port(); let agents = Arc::new(AgentPool::new(port)); // Filesystem watcher: broadcast channel for work/ pipeline changes. let (watcher_tx, _) = broadcast::channel::(1024); if let Some(ref root) = *app_state.project_root.lock().unwrap() { let work_dir = root.join(".story_kit").join("work"); if work_dir.is_dir() { io::watcher::start_watcher(work_dir, root.clone(), watcher_tx.clone()); } } // Reconciliation progress channel: startup reconciliation → WebSocket clients. let (reconciliation_tx, _) = broadcast::channel::(64); // Permission channel: MCP prompt_permission → WebSocket handler. let (perm_tx, perm_rx) = tokio::sync::mpsc::unbounded_channel(); // Capture project root, agents Arc, and reconciliation sender before ctx // is consumed by build_routes. let startup_root: Option = app_state.project_root.lock().unwrap().clone(); let startup_agents = Arc::clone(&agents); let startup_reconciliation_tx = reconciliation_tx.clone(); let ctx = AppContext { state: app_state, store, workflow, agents, watcher_tx, reconciliation_tx, perm_tx, perm_rx: Arc::new(tokio::sync::Mutex::new(perm_rx)), }; let app = build_routes(ctx); // On startup: // 1. Reconcile any stories whose agent work was committed while the server was // offline (worktree has commits ahead of master but pipeline didn't advance). // 2. Auto-assign free agents to remaining unassigned work in the pipeline. if let Some(root) = startup_root { tokio::spawn(async move { slog!( "[startup] Reconciling completed worktrees from previous session." ); startup_agents .reconcile_on_startup(&root, &startup_reconciliation_tx) .await; slog!( "[auto-assign] Scanning pipeline stages for unassigned work." ); startup_agents.auto_assign_available_work(&root).await; }); } let addr = format!("127.0.0.1:{port}"); println!( "\x1b[95;1m ____ _ _ ___ _ \n / ___|| |_ ___ _ __| | _|_ _| |_ \n \\___ \\| __/ _ \\| '__| |/ /| || __|\n ___) | || (_) | | | < | || |_ \n |____/ \\__\\___/|_| |_|\\_\\___|\\__|\n\x1b[0m" ); println!("STORYKIT_PORT={port}"); println!("\x1b[96;1mFrontend:\x1b[0m \x1b[94mhttp://{addr}\x1b[0m"); println!("\x1b[92;1mOpenAPI Docs:\x1b[0m \x1b[94mhttp://{addr}/docs\x1b[0m"); let port_file = write_port_file(&cwd, port); let result = Server::new(TcpListener::bind(&addr)).run(app).await; if let Some(ref path) = port_file { remove_port_file(path); } result } #[cfg(test)] mod tests { use super::*; #[test] #[should_panic(expected = "Invalid project.toml: Duplicate agent name")] fn panics_on_duplicate_agent_names() { let tmp = tempfile::tempdir().unwrap(); let sk = tmp.path().join(".story_kit"); std::fs::create_dir_all(&sk).unwrap(); std::fs::write( sk.join("project.toml"), r#" [[agent]] name = "coder" [[agent]] name = "coder" "#, ) .unwrap(); config::ProjectConfig::load(tmp.path()) .unwrap_or_else(|e| panic!("Invalid project.toml: {e}")); } }