Story 42: Deterministic worktree management via REST/MCP API
Add REST and MCP endpoints for creating, listing, and removing worktrees. Includes worktree lifecycle management and cleanup operations. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use crate::config::ProjectConfig;
|
||||
use crate::http::context::{AppContext, OpenApiResult, bad_request};
|
||||
use poem_openapi::{Object, OpenApi, Tags, payload::Json};
|
||||
use crate::worktree;
|
||||
use poem_openapi::{Object, OpenApi, Tags, param::Path, payload::Json};
|
||||
use serde::Serialize;
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -40,6 +41,25 @@ struct AgentConfigInfoResponse {
|
||||
max_budget_usd: Option<f64>,
|
||||
}
|
||||
|
||||
#[derive(Object)]
|
||||
struct CreateWorktreePayload {
|
||||
story_id: String,
|
||||
}
|
||||
|
||||
#[derive(Object, Serialize)]
|
||||
struct WorktreeInfoResponse {
|
||||
story_id: String,
|
||||
worktree_path: String,
|
||||
branch: String,
|
||||
base_branch: String,
|
||||
}
|
||||
|
||||
#[derive(Object, Serialize)]
|
||||
struct WorktreeListEntry {
|
||||
story_id: String,
|
||||
path: String,
|
||||
}
|
||||
|
||||
pub struct AgentsApi {
|
||||
pub ctx: Arc<AppContext>,
|
||||
}
|
||||
@@ -177,4 +197,70 @@ impl AgentsApi {
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
|
||||
/// Create a git worktree for a story under .story_kit/worktrees/{story_id}.
|
||||
#[oai(path = "/agents/worktrees", method = "post")]
|
||||
async fn create_worktree(
|
||||
&self,
|
||||
payload: Json<CreateWorktreePayload>,
|
||||
) -> OpenApiResult<Json<WorktreeInfoResponse>> {
|
||||
let project_root = self
|
||||
.ctx
|
||||
.agents
|
||||
.get_project_root(&self.ctx.state)
|
||||
.map_err(bad_request)?;
|
||||
|
||||
let info = self
|
||||
.ctx
|
||||
.agents
|
||||
.create_worktree(&project_root, &payload.0.story_id)
|
||||
.await
|
||||
.map_err(bad_request)?;
|
||||
|
||||
Ok(Json(WorktreeInfoResponse {
|
||||
story_id: payload.0.story_id,
|
||||
worktree_path: info.path.to_string_lossy().to_string(),
|
||||
branch: info.branch,
|
||||
base_branch: info.base_branch,
|
||||
}))
|
||||
}
|
||||
|
||||
/// List all worktrees under .story_kit/worktrees/.
|
||||
#[oai(path = "/agents/worktrees", method = "get")]
|
||||
async fn list_worktrees(&self) -> OpenApiResult<Json<Vec<WorktreeListEntry>>> {
|
||||
let project_root = self
|
||||
.ctx
|
||||
.agents
|
||||
.get_project_root(&self.ctx.state)
|
||||
.map_err(bad_request)?;
|
||||
|
||||
let entries = worktree::list_worktrees(&project_root).map_err(bad_request)?;
|
||||
|
||||
Ok(Json(
|
||||
entries
|
||||
.into_iter()
|
||||
.map(|e| WorktreeListEntry {
|
||||
story_id: e.story_id,
|
||||
path: e.path.to_string_lossy().to_string(),
|
||||
})
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
|
||||
/// Remove a git worktree and its feature branch for a story.
|
||||
#[oai(path = "/agents/worktrees/:story_id", method = "delete")]
|
||||
async fn remove_worktree(&self, story_id: Path<String>) -> OpenApiResult<Json<bool>> {
|
||||
let project_root = self
|
||||
.ctx
|
||||
.agents
|
||||
.get_project_root(&self.ctx.state)
|
||||
.map_err(bad_request)?;
|
||||
|
||||
let config = ProjectConfig::load(&project_root).map_err(bad_request)?;
|
||||
worktree::remove_worktree_by_story_id(&project_root, &story_id.0, &config)
|
||||
.await
|
||||
.map_err(bad_request)?;
|
||||
|
||||
Ok(Json(true))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user