2026-03-28 09:01:09 +00:00
|
|
|
//! Handler for the `unblock` command.
|
|
|
|
|
//!
|
|
|
|
|
//! `{bot_name} unblock {number}` finds the blocked work item by number across
|
|
|
|
|
//! all pipeline stages, clears the `blocked` flag, resets `retry_count` to 0,
|
|
|
|
|
//! and returns a confirmation.
|
|
|
|
|
|
|
|
|
|
use super::CommandContext;
|
2026-04-13 14:07:08 +00:00
|
|
|
use crate::io::story_metadata::{
|
2026-04-15 13:49:14 +00:00
|
|
|
clear_front_matter_field_in_content, parse_front_matter, set_front_matter_field,
|
2026-04-13 14:07:08 +00:00
|
|
|
};
|
2026-03-28 09:01:09 +00:00
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
|
|
/// Handle the `unblock` command.
|
|
|
|
|
///
|
|
|
|
|
/// Parses `<number>` from `ctx.args`, locates the work item, checks that it is
|
|
|
|
|
/// blocked, clears the `blocked` flag, resets `retry_count` to 0, and returns
|
|
|
|
|
/// a Markdown confirmation string.
|
|
|
|
|
pub(super) fn handle_unblock(ctx: &CommandContext) -> Option<String> {
|
|
|
|
|
let num_str = ctx.args.trim();
|
|
|
|
|
|
|
|
|
|
if num_str.is_empty() || !num_str.chars().all(|c| c.is_ascii_digit()) {
|
|
|
|
|
return Some(format!(
|
|
|
|
|
"Usage: `{} unblock <number>` (e.g. `unblock 42`)",
|
2026-04-25 20:37:10 +00:00
|
|
|
ctx.services.bot_name
|
2026-03-28 09:01:09 +00:00
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-25 20:37:10 +00:00
|
|
|
Some(unblock_by_number(ctx.effective_root(), num_str))
|
2026-03-28 09:01:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Core unblock logic: find story by numeric prefix and reset its blocked state.
|
|
|
|
|
///
|
|
|
|
|
/// Returns a Markdown-formatted response string suitable for all transports.
|
|
|
|
|
/// Also used by the MCP `unblock` tool.
|
2026-04-09 23:00:01 +00:00
|
|
|
///
|
2026-04-15 13:49:14 +00:00
|
|
|
/// Lookup priority: CRDT → content store.
|
2026-03-28 09:01:09 +00:00
|
|
|
pub(crate) fn unblock_by_number(project_root: &Path, story_number: &str) -> String {
|
2026-04-15 13:49:14 +00:00
|
|
|
let (story_id, _, _, _) =
|
2026-04-09 23:00:01 +00:00
|
|
|
match crate::chat::lookup::find_story_by_number(project_root, story_number) {
|
|
|
|
|
Some(found) => found,
|
|
|
|
|
None => {
|
2026-04-13 14:07:08 +00:00
|
|
|
return format!("No story, bug, or spike with number **{story_number}** found.");
|
2026-03-28 09:01:09 +00:00
|
|
|
}
|
2026-04-09 23:00:01 +00:00
|
|
|
};
|
|
|
|
|
|
2026-04-15 13:49:14 +00:00
|
|
|
unblock_by_story_id(&story_id)
|
2026-03-28 09:01:09 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-08 03:03:59 +00:00
|
|
|
/// Unblock a story using the content store (DB-backed).
|
|
|
|
|
fn unblock_by_story_id(story_id: &str) -> String {
|
|
|
|
|
let contents = match crate::db::read_content(story_id) {
|
|
|
|
|
Some(c) => c,
|
|
|
|
|
None => return format!("Failed to read story content for **{story_id}**"),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let meta = match parse_front_matter(&contents) {
|
|
|
|
|
Ok(m) => m,
|
|
|
|
|
Err(e) => return format!("Failed to parse front matter for **{story_id}**: {e}"),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let story_name = meta.name.as_deref().unwrap_or(story_id).to_string();
|
|
|
|
|
let has_blocked = meta.blocked == Some(true);
|
|
|
|
|
let has_merge_failure = meta.merge_failure.is_some();
|
|
|
|
|
|
|
|
|
|
if !has_blocked && !has_merge_failure {
|
2026-04-13 14:07:08 +00:00
|
|
|
return format!("**{story_name}** ({story_id}) is not blocked. Nothing to unblock.");
|
2026-04-08 03:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut updated = contents;
|
|
|
|
|
if has_blocked {
|
|
|
|
|
updated = clear_front_matter_field_in_content(&updated, "blocked");
|
|
|
|
|
}
|
|
|
|
|
if has_merge_failure {
|
|
|
|
|
updated = clear_front_matter_field_in_content(&updated, "merge_failure");
|
|
|
|
|
}
|
|
|
|
|
updated = set_front_matter_field(&updated, "retry_count", "0");
|
|
|
|
|
|
|
|
|
|
crate::db::write_content(story_id, &updated);
|
2026-04-09 21:24:11 +00:00
|
|
|
let stage = crate::pipeline_state::read_typed(story_id)
|
|
|
|
|
.ok()
|
|
|
|
|
.flatten()
|
|
|
|
|
.map(|i| i.stage.dir_name().to_string())
|
2026-04-08 03:03:59 +00:00
|
|
|
.unwrap_or_else(|| "2_current".to_string());
|
|
|
|
|
crate::db::write_item_with_content(story_id, &stage, &updated);
|
|
|
|
|
|
|
|
|
|
let mut cleared = Vec::new();
|
2026-04-13 14:07:08 +00:00
|
|
|
if has_blocked {
|
|
|
|
|
cleared.push("blocked");
|
|
|
|
|
}
|
|
|
|
|
if has_merge_failure {
|
|
|
|
|
cleared.push("merge_failure");
|
|
|
|
|
}
|
|
|
|
|
format!(
|
|
|
|
|
"Unblocked **{story_name}** ({story_id}). Cleared: {}. Retry count reset to 0.",
|
|
|
|
|
cleared.join(", ")
|
|
|
|
|
)
|
2026-04-11 17:43:50 +00:00
|
|
|
}
|
2026-03-28 09:01:09 +00:00
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// Tests
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::super::{CommandDispatch, try_handle_command};
|
|
|
|
|
|
|
|
|
|
fn unblock_cmd_with_root(root: &std::path::Path, args: &str) -> Option<String> {
|
2026-04-25 20:37:10 +00:00
|
|
|
let services = crate::services::Services::new_test(root.to_path_buf(), "Timmy".to_string());
|
2026-03-28 09:01:09 +00:00
|
|
|
let room_id = "!test:example.com".to_string();
|
|
|
|
|
let dispatch = CommandDispatch {
|
2026-04-25 20:37:10 +00:00
|
|
|
services: &services,
|
|
|
|
|
project_root: &services.project_root,
|
2026-03-28 09:01:09 +00:00
|
|
|
bot_user_id: "@timmy:homeserver.local",
|
|
|
|
|
room_id: &room_id,
|
|
|
|
|
};
|
|
|
|
|
try_handle_command(&dispatch, &format!("@timmy unblock {args}"))
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-28 19:47:59 +00:00
|
|
|
use crate::chat::test_helpers::write_story_file;
|
2026-03-28 09:01:09 +00:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn unblock_command_is_registered() {
|
|
|
|
|
use super::super::commands;
|
|
|
|
|
assert!(
|
|
|
|
|
commands().iter().any(|c| c.name == "unblock"),
|
|
|
|
|
"unblock command must be in the registry"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn unblock_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("unblock"),
|
|
|
|
|
"help should list unblock command: {output}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn unblock_command_no_args_returns_usage() {
|
|
|
|
|
let tmp = tempfile::TempDir::new().unwrap();
|
|
|
|
|
let output = unblock_cmd_with_root(tmp.path(), "").unwrap();
|
|
|
|
|
assert!(
|
|
|
|
|
output.contains("Usage"),
|
|
|
|
|
"no args should show usage hint: {output}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn unblock_command_non_numeric_returns_usage() {
|
|
|
|
|
let tmp = tempfile::TempDir::new().unwrap();
|
|
|
|
|
let output = unblock_cmd_with_root(tmp.path(), "abc").unwrap();
|
|
|
|
|
assert!(
|
|
|
|
|
output.contains("Usage"),
|
|
|
|
|
"non-numeric arg should show usage hint: {output}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn unblock_command_not_found_returns_error() {
|
|
|
|
|
let tmp = tempfile::TempDir::new().unwrap();
|
|
|
|
|
let output = unblock_cmd_with_root(tmp.path(), "999").unwrap();
|
|
|
|
|
assert!(
|
|
|
|
|
output.contains("999") && output.contains("found"),
|
|
|
|
|
"not-found message should include number and 'found': {output}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn unblock_command_not_blocked_returns_error() {
|
|
|
|
|
let tmp = tempfile::TempDir::new().unwrap();
|
|
|
|
|
write_story_file(
|
|
|
|
|
tmp.path(),
|
|
|
|
|
"2_current",
|
|
|
|
|
"42_story_test.md",
|
|
|
|
|
"---\nname: Test Story\nretry_count: 2\n---\n# Story\n",
|
|
|
|
|
);
|
|
|
|
|
let output = unblock_cmd_with_root(tmp.path(), "42").unwrap();
|
|
|
|
|
assert!(
|
|
|
|
|
output.contains("not blocked"),
|
|
|
|
|
"non-blocked story should return not-blocked error: {output}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn unblock_command_clears_blocked_and_resets_retry_count() {
|
|
|
|
|
let tmp = tempfile::TempDir::new().unwrap();
|
2026-04-08 03:03:59 +00:00
|
|
|
// Use a high story number (9903) to avoid collisions with other tests in the
|
|
|
|
|
// global content store.
|
2026-03-28 09:01:09 +00:00
|
|
|
write_story_file(
|
|
|
|
|
tmp.path(),
|
|
|
|
|
"2_current",
|
2026-04-08 03:03:59 +00:00
|
|
|
"9903_story_stuck.md",
|
2026-03-28 09:01:09 +00:00
|
|
|
"---\nname: Stuck Story\nblocked: true\nretry_count: 5\n---\n# Story\n",
|
|
|
|
|
);
|
|
|
|
|
|
2026-04-08 03:03:59 +00:00
|
|
|
let output = unblock_cmd_with_root(tmp.path(), "9903").unwrap();
|
2026-03-28 09:01:09 +00:00
|
|
|
assert!(
|
|
|
|
|
output.contains("Unblocked") && output.contains("Stuck Story"),
|
|
|
|
|
"should confirm unblock with story name: {output}"
|
|
|
|
|
);
|
|
|
|
|
assert!(
|
2026-04-08 03:03:59 +00:00
|
|
|
output.contains("9903_story_stuck"),
|
2026-03-28 09:01:09 +00:00
|
|
|
"should include story_id in response: {output}"
|
|
|
|
|
);
|
|
|
|
|
|
2026-04-08 03:03:59 +00:00
|
|
|
// The unblock command writes back via the content store; read from there.
|
|
|
|
|
let contents = crate::db::read_content("9903_story_stuck")
|
|
|
|
|
.or_else(|| {
|
|
|
|
|
std::fs::read_to_string(
|
2026-04-13 14:07:08 +00:00
|
|
|
tmp.path()
|
|
|
|
|
.join(".huskies/work/2_current/9903_story_stuck.md"),
|
2026-04-08 03:03:59 +00:00
|
|
|
)
|
|
|
|
|
.ok()
|
|
|
|
|
})
|
|
|
|
|
.expect("story content should be readable after unblock");
|
2026-03-28 09:01:09 +00:00
|
|
|
assert!(
|
|
|
|
|
!contents.contains("blocked:"),
|
|
|
|
|
"blocked field should be removed: {contents}"
|
|
|
|
|
);
|
|
|
|
|
assert!(
|
|
|
|
|
contents.contains("retry_count: 0"),
|
|
|
|
|
"retry_count should be reset to 0: {contents}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn unblock_command_finds_story_in_any_stage() {
|
|
|
|
|
let tmp = tempfile::TempDir::new().unwrap();
|
2026-04-08 03:03:59 +00:00
|
|
|
// Use a high story number (9901) to avoid collisions with other tests in the
|
|
|
|
|
// global content store.
|
2026-03-28 09:01:09 +00:00
|
|
|
write_story_file(
|
|
|
|
|
tmp.path(),
|
|
|
|
|
"3_qa",
|
2026-04-08 03:03:59 +00:00
|
|
|
"9901_story_in_qa.md",
|
2026-03-28 09:01:09 +00:00
|
|
|
"---\nname: In QA\nblocked: true\nretry_count: 3\n---\n# Story\n",
|
|
|
|
|
);
|
|
|
|
|
|
2026-04-08 03:03:59 +00:00
|
|
|
let output = unblock_cmd_with_root(tmp.path(), "9901").unwrap();
|
2026-03-28 09:01:09 +00:00
|
|
|
assert!(
|
|
|
|
|
output.contains("Unblocked"),
|
|
|
|
|
"should unblock story in qa stage: {output}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn unblock_command_includes_story_id_in_response() {
|
|
|
|
|
let tmp = tempfile::TempDir::new().unwrap();
|
2026-04-08 03:03:59 +00:00
|
|
|
// Use a high story number (9902) to avoid collisions with other tests in the
|
|
|
|
|
// global content store.
|
2026-03-28 09:01:09 +00:00
|
|
|
write_story_file(
|
|
|
|
|
tmp.path(),
|
|
|
|
|
"1_backlog",
|
2026-04-08 03:03:59 +00:00
|
|
|
"9902_story_blocked_one.md",
|
2026-03-28 09:01:09 +00:00
|
|
|
"---\nname: Blocked One\nblocked: true\nretry_count: 2\n---\n",
|
|
|
|
|
);
|
|
|
|
|
|
2026-04-08 03:03:59 +00:00
|
|
|
let output = unblock_cmd_with_root(tmp.path(), "9902").unwrap();
|
2026-03-28 09:01:09 +00:00
|
|
|
assert!(
|
2026-04-08 03:03:59 +00:00
|
|
|
output.contains("9902_story_blocked_one"),
|
2026-03-28 09:01:09 +00:00
|
|
|
"response should include story_id: {output}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|