Accept story 38: Auto-Open Project on Server Startup
Server detects .story_kit/ in cwd or parent directories at startup and automatically opens the project. MCP tools work immediately without manual project-open step. Falls back to cwd when no .story_kit/ found. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -41,14 +41,49 @@ fn remove_port_file(path: &Path) {
|
||||
let _ = std::fs::remove_file(path);
|
||||
}
|
||||
|
||||
/// Walk from `start` up through parent directories, returning the first
|
||||
/// directory that contains a `.story_kit/` subdirectory, or `None`.
|
||||
fn find_story_kit_root(start: &Path) -> Option<PathBuf> {
|
||||
let mut current = start.to_path_buf();
|
||||
loop {
|
||||
if current.join(".story_kit").is_dir() {
|
||||
return Some(current);
|
||||
}
|
||||
if !current.pop() {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[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("."));
|
||||
*app_state.project_root.lock().unwrap() = Some(cwd.clone());
|
||||
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| {
|
||||
eprintln!("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));
|
||||
@@ -70,10 +105,6 @@ async fn main() -> Result<(), std::io::Error> {
|
||||
println!("\x1b[96;1mFrontend:\x1b[0m \x1b[94mhttp://{addr}\x1b[0m");
|
||||
println!("\x1b[92;1mOpenAPI Docs:\x1b[0m \x1b[94mhttp://{addr}/docs\x1b[0m");
|
||||
|
||||
// Validate agent config at startup — panic on invalid project.toml.
|
||||
config::ProjectConfig::load(&cwd)
|
||||
.unwrap_or_else(|e| panic!("Invalid project.toml: {e}"));
|
||||
|
||||
let port_file = write_port_file(&cwd, port);
|
||||
|
||||
let result = Server::new(TcpListener::bind(&addr)).run(app).await;
|
||||
@@ -136,4 +167,45 @@ name = "coder"
|
||||
remove_port_file(&path);
|
||||
assert!(!path.exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_story_kit_root_returns_cwd_when_story_kit_in_cwd() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
std::fs::create_dir_all(tmp.path().join(".story_kit")).unwrap();
|
||||
|
||||
let result = find_story_kit_root(tmp.path());
|
||||
assert_eq!(result, Some(tmp.path().to_path_buf()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_story_kit_root_returns_parent_when_story_kit_in_parent() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
std::fs::create_dir_all(tmp.path().join(".story_kit")).unwrap();
|
||||
let child = tmp.path().join("subdir").join("nested");
|
||||
std::fs::create_dir_all(&child).unwrap();
|
||||
|
||||
let result = find_story_kit_root(&child);
|
||||
assert_eq!(result, Some(tmp.path().to_path_buf()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_story_kit_root_returns_none_when_no_story_kit() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
// No .story_kit/ created
|
||||
|
||||
let result = find_story_kit_root(tmp.path());
|
||||
assert_eq!(result, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_story_kit_root_prefers_nearest_ancestor() {
|
||||
// If both cwd and a parent have .story_kit/, return cwd (nearest).
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
std::fs::create_dir_all(tmp.path().join(".story_kit")).unwrap();
|
||||
let child = tmp.path().join("inner");
|
||||
std::fs::create_dir_all(child.join(".story_kit")).unwrap();
|
||||
|
||||
let result = find_story_kit_root(&child);
|
||||
assert_eq!(result, Some(child));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user