2026-04-26 21:11:09 +00:00
|
|
|
//! Refactor item MCP tools.
|
|
|
|
|
|
2026-04-27 01:32:08 +00:00
|
|
|
#![allow(unused_imports, dead_code)]
|
|
|
|
|
#[allow(unused_imports)]
|
2026-04-26 21:11:09 +00:00
|
|
|
use crate::agents::{
|
|
|
|
|
close_bug_to_archive, feature_branch_has_unmerged_changes, move_story_to_done,
|
|
|
|
|
};
|
2026-05-08 14:24:20 +00:00
|
|
|
use crate::db::yaml_legacy::parse_front_matter;
|
2026-04-26 21:11:09 +00:00
|
|
|
use crate::http::context::AppContext;
|
|
|
|
|
use crate::http::workflow::{
|
|
|
|
|
add_criterion_to_file, check_criterion_in_file, create_bug_file, create_refactor_file,
|
|
|
|
|
create_spike_file, create_story_file, edit_criterion_in_file, list_bug_files,
|
|
|
|
|
list_refactor_files, load_pipeline_state, load_upcoming_stories, remove_criterion_from_file,
|
|
|
|
|
update_story_in_file, validate_story_dirs,
|
|
|
|
|
};
|
2026-05-12 19:14:54 +01:00
|
|
|
use crate::io::story_metadata::{check_archived_deps_from_list, parse_unchecked_todos};
|
2026-04-26 21:11:09 +00:00
|
|
|
use crate::service::story::parse_test_cases;
|
|
|
|
|
use crate::slog_warn;
|
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
|
use crate::workflow::{TestCaseResult, TestStatus, evaluate_acceptance_with_coverage};
|
|
|
|
|
use serde_json::{Value, json};
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
use std::fs;
|
|
|
|
|
|
|
|
|
|
pub(crate) fn tool_create_refactor(args: &Value, ctx: &AppContext) -> Result<String, String> {
|
|
|
|
|
let name = args
|
|
|
|
|
.get("name")
|
|
|
|
|
.and_then(|v| v.as_str())
|
|
|
|
|
.ok_or("Missing required argument: name")?;
|
|
|
|
|
let description = args.get("description").and_then(|v| v.as_str());
|
2026-04-27 16:26:15 +00:00
|
|
|
let acceptance_criteria: Vec<String> = args
|
2026-04-26 21:11:09 +00:00
|
|
|
.get("acceptance_criteria")
|
2026-04-27 16:26:15 +00:00
|
|
|
.and_then(|v| serde_json::from_value(v.clone()).ok())
|
|
|
|
|
.ok_or("Missing required argument: acceptance_criteria")?;
|
|
|
|
|
if acceptance_criteria.is_empty() {
|
|
|
|
|
return Err("acceptance_criteria must contain at least one entry".to_string());
|
|
|
|
|
}
|
|
|
|
|
const JUNK_AC: &[&str] = &["", "todo", "tbd", "fixme", "xxx", "???"];
|
|
|
|
|
let all_junk = acceptance_criteria
|
|
|
|
|
.iter()
|
|
|
|
|
.all(|ac| JUNK_AC.contains(&ac.trim().to_lowercase().as_str()));
|
|
|
|
|
if all_junk {
|
|
|
|
|
return Err(
|
|
|
|
|
"acceptance_criteria must contain at least one real entry (not just TODO/TBD/FIXME/XXX/???)."
|
|
|
|
|
.to_string(),
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-04-26 21:11:09 +00:00
|
|
|
let depends_on: Option<Vec<u32>> = args
|
|
|
|
|
.get("depends_on")
|
|
|
|
|
.and_then(|v| serde_json::from_value(v.clone()).ok());
|
|
|
|
|
|
|
|
|
|
let root = ctx.state.get_project_root()?;
|
|
|
|
|
let refactor_id = create_refactor_file(
|
|
|
|
|
&root,
|
|
|
|
|
name,
|
|
|
|
|
description,
|
2026-04-27 16:26:15 +00:00
|
|
|
Some(&acceptance_criteria),
|
2026-04-26 21:11:09 +00:00
|
|
|
depends_on.as_deref(),
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
|
|
Ok(format!("Created refactor: {refactor_id}"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn tool_list_refactors(ctx: &AppContext) -> Result<String, String> {
|
|
|
|
|
let root = ctx.state.get_project_root()?;
|
|
|
|
|
let refactors = list_refactor_files(&root)?;
|
|
|
|
|
serde_json::to_string_pretty(&json!(
|
|
|
|
|
refactors
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|(id, name)| json!({ "refactor_id": id, "name": name }))
|
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
))
|
|
|
|
|
.map_err(|e| format!("Serialization error: {e}"))
|
|
|
|
|
}
|
2026-04-27 16:26:15 +00:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
use crate::http::test_helpers::test_ctx;
|
|
|
|
|
use serde_json::json;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn tool_create_refactor_rejects_missing_acceptance_criteria() {
|
|
|
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
|
|
|
let ctx = test_ctx(tmp.path());
|
|
|
|
|
let result = tool_create_refactor(&json!({"name": "My Refactor"}), &ctx);
|
|
|
|
|
assert!(result.is_err());
|
|
|
|
|
assert!(
|
|
|
|
|
result.unwrap_err().contains("acceptance_criteria"),
|
|
|
|
|
"error should mention acceptance_criteria"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn tool_create_refactor_rejects_empty_acceptance_criteria() {
|
|
|
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
|
|
|
let ctx = test_ctx(tmp.path());
|
|
|
|
|
let result = tool_create_refactor(
|
|
|
|
|
&json!({"name": "My Refactor", "acceptance_criteria": []}),
|
|
|
|
|
&ctx,
|
|
|
|
|
);
|
|
|
|
|
assert!(result.is_err());
|
|
|
|
|
assert!(
|
|
|
|
|
result.unwrap_err().contains("acceptance_criteria"),
|
|
|
|
|
"error should mention acceptance_criteria"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn tool_create_refactor_accepts_single_criterion() {
|
|
|
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
|
|
|
let ctx = test_ctx(tmp.path());
|
|
|
|
|
let result = tool_create_refactor(
|
|
|
|
|
&json!({"name": "Single Criterion Refactor", "acceptance_criteria": ["Code is clean"]}),
|
|
|
|
|
&ctx,
|
|
|
|
|
);
|
|
|
|
|
assert!(result.is_ok(), "expected ok: {result:?}");
|
|
|
|
|
assert!(result.unwrap().contains("Created refactor:"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn tool_create_refactor_rejects_all_junk_acceptance_criteria() {
|
|
|
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
|
|
|
let ctx = test_ctx(tmp.path());
|
|
|
|
|
let result = tool_create_refactor(
|
|
|
|
|
&json!({"name": "Junk Refactor", "acceptance_criteria": ["TODO", "TBD"]}),
|
|
|
|
|
&ctx,
|
|
|
|
|
);
|
|
|
|
|
assert!(result.is_err());
|
|
|
|
|
assert!(
|
|
|
|
|
result.unwrap_err().contains("real entry"),
|
|
|
|
|
"error should mention real entry"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn tool_create_refactor_accepts_mixed_junk_and_real_acceptance_criteria() {
|
|
|
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
|
|
|
let ctx = test_ctx(tmp.path());
|
|
|
|
|
let result = tool_create_refactor(
|
|
|
|
|
&json!({"name": "Mixed Refactor", "acceptance_criteria": ["TODO", "Real AC"]}),
|
|
|
|
|
&ctx,
|
|
|
|
|
);
|
|
|
|
|
assert!(result.is_ok(), "expected ok for mixed AC: {result:?}");
|
|
|
|
|
assert!(result.unwrap().contains("Created refactor:"));
|
|
|
|
|
}
|
|
|
|
|
}
|