Files
huskies/server/src/chat/commands/move_story.rs
T
Timmy 69d91d7707 feat(929): delete db/yaml_legacy.rs entirely — CRDT is the sole source of truth
Final 929 sweep: every YAML-shaped helper is gone. No production code
parses or writes YAML front matter anywhere.

Surface removed:
- db/yaml_legacy.rs (FrontMatter/StoryMetadata structs, parse_front_matter,
  set_front_matter_field, yaml_residue marker) — file deleted.
- ItemMeta::from_yaml — deleted; callers pass typed ItemMeta::named(...) or
  ItemMeta::default() and use typed CRDT setters (set_depends_on,
  set_blocked, set_retry_count, set_agent, set_qa_mode, set_review_hold,
  set_item_type, set_epic, set_mergemaster_attempted) for the rest.
- write_coverage_baseline_to_story_file + read_coverage_percent_from_json —
  the coverage_baseline YAML field was write-only (nothing read it back);
  removed along with its caller in agent_tools/lifecycle.rs.
- update_story_in_file's generic `front_matter` HashMap parameter —
  tool_update_story now intercepts every known field name and routes it
  to a typed CRDT setter; unknown keys are rejected with an explicit error
  pointing at the typed setters. The function only takes user_story /
  description sections now.
- All 117 ItemMeta::from_yaml callsites migrated. Where tests previously
  passed a YAML-shaped content blob and relied on the helper to extract
  name/depends_on/blocked/agent/qa, they now pass:
    write_item_with_content(id, stage, content, ItemMeta::named("Foo"))
    crate::crdt_state::set_depends_on(id, &[...])    // when needed
    crate::crdt_state::set_blocked(id, true)         // when needed
    crate::crdt_state::set_agent(id, Some("..."))    // when needed
- write_story_content + write_story_file (test helper) now take an
  explicit `name: Option<&str>` instead of parsing it from content.
- db::ops::move_item_stage stopped re-parsing YAML on every stage
  transition; metadata is read straight from the CRDT view when mirroring
  the row into SQLite.

New CRDT setters added for symmetry:
- crdt_state::set_name (mirrors set_agent — explicit name updates).

cargo fmt --check, clippy --all-targets -- -D warnings, and the
2830-test suite all pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-12 20:55:25 +01:00

244 lines
8.2 KiB
Rust

//! Handler for the `move` command.
//!
//! `{bot_name} move {number} {stage}` finds the work item by number across all
//! pipeline stages, moves it to the specified stage, and returns a confirmation
//! with the story title, old stage, and new stage.
use super::CommandContext;
use crate::agents::move_story_to_stage;
/// Valid stage names accepted by the move command.
const VALID_STAGES: &[&str] = &["backlog", "current", "qa", "merge", "done"];
/// Handle the `move` command.
///
/// Parses `<number> <stage>` from `ctx.args`, locates the work item by its
/// numeric prefix, moves it to the target stage using the shared lifecycle
/// function, and returns a Markdown confirmation string.
pub(super) fn handle_move(ctx: &CommandContext) -> Option<String> {
let args = ctx.args.trim();
// Parse `number stage` from args.
let (num_str, stage_raw) = match args.split_once(char::is_whitespace) {
Some((n, s)) => (n.trim(), s.trim()),
None => {
return Some(format!(
"Usage: `{} move <number> <stage>`\n\nValid stages: {}",
ctx.services.bot_name,
VALID_STAGES.join(", ")
));
}
};
if num_str.is_empty() || !num_str.chars().all(|c| c.is_ascii_digit()) {
return Some(format!(
"Invalid story number: `{num_str}`. Usage: `{} move <number> <stage>`",
ctx.services.bot_name
));
}
let target_stage = stage_raw.to_ascii_lowercase();
if !VALID_STAGES.contains(&target_stage.as_str()) {
return Some(format!(
"Invalid stage: `{stage_raw}`. Valid stages: {}",
VALID_STAGES.join(", ")
));
}
// Find the story by numeric prefix: CRDT → content store → filesystem.
let (story_id, _stage_dir, _path, _content) =
match crate::chat::lookup::find_story_by_number(ctx.effective_root(), num_str) {
Some(found) => found,
None => {
return Some(format!(
"No story, bug, or spike with number **{num_str}** found."
));
}
};
// Display name comes from the CRDT name register (story 929).
let found_name =
crate::crdt_state::read_item(&story_id).and_then(|w| w.name().map(str::to_string));
let display_name = found_name.as_deref().unwrap_or(&story_id);
match move_story_to_stage(&story_id, &target_stage) {
Ok((from_stage, to_stage)) => Some(format!(
"Moved **{display_name}** from **{from_stage}** to **{to_stage}**."
)),
Err(e) => Some(format!("Failed to move story {num_str}: {e}")),
}
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::super::{CommandDispatch, try_handle_command};
fn move_cmd_with_root(root: &std::path::Path, args: &str) -> Option<String> {
let services = crate::services::Services::new_test(root.to_path_buf(), "Timmy".to_string());
let room_id = "!test:example.com".to_string();
let dispatch = CommandDispatch {
services: &services,
project_root: &services.project_root,
bot_user_id: "@timmy:homeserver.local",
room_id: &room_id,
};
try_handle_command(&dispatch, &format!("@timmy move {args}"))
}
use crate::chat::test_helpers::write_story_file;
#[test]
fn move_command_is_registered() {
use super::super::commands;
let found = commands().iter().any(|c| c.name == "move");
assert!(found, "move command must be in the registry");
}
#[test]
fn move_command_appears_in_help() {
let result = super::super::tests::try_cmd_addressed(
"Timmy",
"@timmy:homeserver.local",
"@timmy help",
);
let output = result.unwrap();
assert!(
output.contains("move"),
"help should list move command: {output}"
);
}
#[test]
fn move_command_no_args_returns_usage() {
let tmp = tempfile::TempDir::new().unwrap();
let output = move_cmd_with_root(tmp.path(), "").unwrap();
assert!(
output.contains("Usage"),
"no args should show usage hint: {output}"
);
}
#[test]
fn move_command_missing_stage_returns_usage() {
let tmp = tempfile::TempDir::new().unwrap();
let output = move_cmd_with_root(tmp.path(), "42").unwrap();
assert!(
output.contains("Usage"),
"missing stage should show usage hint: {output}"
);
}
#[test]
fn move_command_invalid_stage_returns_error() {
let tmp = tempfile::TempDir::new().unwrap();
let output = move_cmd_with_root(tmp.path(), "42 invalid_stage").unwrap();
assert!(
output.contains("Invalid stage"),
"invalid stage should return error: {output}"
);
}
#[test]
fn move_command_non_numeric_number_returns_error() {
let tmp = tempfile::TempDir::new().unwrap();
let output = move_cmd_with_root(tmp.path(), "abc current").unwrap();
assert!(
output.contains("Invalid story number"),
"non-numeric number should return error: {output}"
);
}
#[test]
fn move_command_not_found_returns_friendly_message() {
let tmp = tempfile::TempDir::new().unwrap();
let output = move_cmd_with_root(tmp.path(), "999 current").unwrap();
assert!(
output.contains("999") && output.contains("found"),
"not-found message should include number and 'found': {output}"
);
}
#[test]
fn move_command_moves_story_and_confirms() {
let tmp = tempfile::TempDir::new().unwrap();
write_story_file(
tmp.path(),
"1_backlog",
"42_story_some_feature.md",
"# Story 42\n",
Some("Some Feature"),
);
let output = move_cmd_with_root(tmp.path(), "42 current").unwrap();
assert!(
output.contains("Some Feature"),
"confirmation should include story name: {output}"
);
assert!(
output.contains("backlog"),
"confirmation should include old stage: {output}"
);
assert!(
output.contains("current"),
"confirmation should include new stage: {output}"
);
// Verify the story is still accessible in the content store after the move.
assert!(
crate::db::read_content("42_story_some_feature").is_some(),
"story should be in the content store after move"
);
}
#[test]
fn move_command_case_insensitive_stage() {
let tmp = tempfile::TempDir::new().unwrap();
write_story_file(
tmp.path(),
"2_current",
"8810_story_case_test.md",
"",
Some("CaseTest"),
);
let output = move_cmd_with_root(tmp.path(), "8810 BACKLOG").unwrap();
assert!(
output.contains("CaseTest") && output.contains("backlog"),
"stage matching should be case-insensitive: {output}"
);
}
#[test]
fn move_command_idempotent_when_already_in_target() {
let tmp = tempfile::TempDir::new().unwrap();
write_story_file(
tmp.path(),
"2_current",
"5_story_already_current.md",
"---\nname: Already Current\n---\n",
None,
);
// Moving to the stage it's already in should return a success message.
let output = move_cmd_with_root(tmp.path(), "5 current").unwrap();
assert!(
output.contains("Moved") || output.contains("current"),
"idempotent move should succeed: {output}"
);
}
#[test]
fn move_command_case_insensitive_command() {
let result = super::super::tests::try_cmd_addressed(
"Timmy",
"@timmy:homeserver.local",
"@timmy MOVE 1 backlog",
);
// Returns Some (the registry matched, regardless of result content)
assert!(result.is_some(), "MOVE should match case-insensitively");
}
}