2026-04-04 21:43:29 +00:00
|
|
|
//! Handler for the `depends` command.
|
|
|
|
|
//!
|
|
|
|
|
//! `{bot_name} depends <number> <dep1> [dep2 ...]` locates the work item by
|
|
|
|
|
//! number across all pipeline stages and writes (or updates) the `depends_on`
|
|
|
|
|
//! front-matter field with the provided dependency numbers.
|
|
|
|
|
//!
|
|
|
|
|
//! Passing no dependency numbers clears the field entirely.
|
|
|
|
|
|
|
|
|
|
use super::CommandContext;
|
|
|
|
|
use crate::io::story_metadata::{parse_front_matter, write_depends_on};
|
|
|
|
|
|
|
|
|
|
/// All pipeline stage directories searched when finding a work item by number.
|
|
|
|
|
const SEARCH_DIRS: &[&str] = &[
|
|
|
|
|
"1_backlog",
|
|
|
|
|
"2_current",
|
|
|
|
|
"3_qa",
|
|
|
|
|
"4_merge",
|
|
|
|
|
"5_done",
|
|
|
|
|
"6_archived",
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/// Handle the `depends` command.
|
|
|
|
|
///
|
|
|
|
|
/// Syntax: `depends <number> [dep1 dep2 ...]`
|
|
|
|
|
///
|
|
|
|
|
/// - `depends 484 477 478` — set story 484's dependencies to [477, 478]
|
|
|
|
|
/// - `depends 484` — clear all dependencies for story 484
|
|
|
|
|
pub(super) fn handle_depends(ctx: &CommandContext) -> Option<String> {
|
|
|
|
|
let args = ctx.args.trim();
|
|
|
|
|
|
|
|
|
|
if args.is_empty() {
|
|
|
|
|
return Some(format!(
|
|
|
|
|
"Usage: `{} depends <number> [dep1 dep2 ...]`\n\nExamples:\n\
|
|
|
|
|
• `{0} depends 484 477 478` — set depends_on: [477, 478]\n\
|
|
|
|
|
• `{0} depends 484` — clear all dependencies",
|
|
|
|
|
ctx.bot_name
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut parts = args.split_whitespace();
|
|
|
|
|
let num_str = parts.next().unwrap_or("");
|
|
|
|
|
|
|
|
|
|
if !num_str.chars().all(|c| c.is_ascii_digit()) || num_str.is_empty() {
|
|
|
|
|
return Some(format!(
|
|
|
|
|
"Invalid story number: `{num_str}`. Usage: `{} depends <number> [dep1 dep2 ...]`",
|
|
|
|
|
ctx.bot_name
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse dependency numbers.
|
|
|
|
|
let mut deps: Vec<u32> = Vec::new();
|
|
|
|
|
for token in parts {
|
|
|
|
|
match token.parse::<u32>() {
|
|
|
|
|
Ok(n) => deps.push(n),
|
|
|
|
|
Err(_) => {
|
|
|
|
|
return Some(format!(
|
|
|
|
|
"Invalid dependency number: `{token}`. All numbers must be positive integers."
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find the story file across all pipeline stages by numeric prefix.
|
2026-04-08 03:03:59 +00:00
|
|
|
// Try the content store / CRDT state first, then fall back to filesystem.
|
2026-04-04 21:43:29 +00:00
|
|
|
let mut found: Option<(std::path::PathBuf, String)> = None;
|
|
|
|
|
|
2026-04-08 03:03:59 +00:00
|
|
|
// --- DB-first lookup ---
|
|
|
|
|
for id in crate::db::all_content_ids() {
|
|
|
|
|
let file_num = id.split('_').next().unwrap_or("");
|
2026-04-09 21:24:11 +00:00
|
|
|
if file_num == num_str && let Ok(Some(item)) = crate::pipeline_state::read_typed(&id) {
|
2026-04-08 03:03:59 +00:00
|
|
|
let path = ctx
|
|
|
|
|
.project_root
|
|
|
|
|
.join(".huskies")
|
|
|
|
|
.join("work")
|
2026-04-09 21:24:11 +00:00
|
|
|
.join(item.stage.dir_name())
|
2026-04-08 03:03:59 +00:00
|
|
|
.join(format!("{id}.md"));
|
|
|
|
|
found = Some((path, id));
|
|
|
|
|
break;
|
2026-04-04 21:43:29 +00:00
|
|
|
}
|
2026-04-08 03:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- Filesystem fallback ---
|
|
|
|
|
if found.is_none() {
|
|
|
|
|
'outer: for stage_dir in SEARCH_DIRS {
|
|
|
|
|
let dir = ctx
|
|
|
|
|
.project_root
|
|
|
|
|
.join(".huskies")
|
|
|
|
|
.join("work")
|
|
|
|
|
.join(stage_dir);
|
|
|
|
|
if !dir.exists() {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if let Ok(entries) = std::fs::read_dir(&dir) {
|
|
|
|
|
for entry in entries.flatten() {
|
|
|
|
|
let path = entry.path();
|
|
|
|
|
if path.extension().and_then(|e| e.to_str()) != Some("md") {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if let Some(stem) = path.file_stem().and_then(|s| s.to_str()) {
|
|
|
|
|
let file_num = stem
|
|
|
|
|
.split('_')
|
|
|
|
|
.next()
|
|
|
|
|
.filter(|s| !s.is_empty() && s.chars().all(|c| c.is_ascii_digit()))
|
|
|
|
|
.unwrap_or("");
|
|
|
|
|
if file_num == num_str {
|
|
|
|
|
found = Some((path.to_path_buf(), stem.to_string()));
|
|
|
|
|
break 'outer;
|
|
|
|
|
}
|
2026-04-04 21:43:29 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let (path, story_id) = match found {
|
|
|
|
|
Some(f) => f,
|
|
|
|
|
None => {
|
|
|
|
|
return Some(format!(
|
|
|
|
|
"No story, bug, or spike with number **{num_str}** found."
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-08 03:03:59 +00:00
|
|
|
// Try the content store first, then fall back to reading from disk.
|
|
|
|
|
let story_name = crate::db::read_content(&story_id)
|
|
|
|
|
.or_else(|| std::fs::read_to_string(&path).ok())
|
2026-04-04 21:43:29 +00:00
|
|
|
.and_then(|c| parse_front_matter(&c).ok())
|
|
|
|
|
.and_then(|m| m.name)
|
|
|
|
|
.unwrap_or_else(|| story_id.clone());
|
|
|
|
|
|
|
|
|
|
match write_depends_on(&path, &deps) {
|
|
|
|
|
Ok(()) if deps.is_empty() => Some(format!(
|
|
|
|
|
"Cleared all dependencies for **{story_name}** ({story_id})."
|
|
|
|
|
)),
|
|
|
|
|
Ok(()) => {
|
|
|
|
|
let nums: Vec<String> = deps.iter().map(|n| n.to_string()).collect();
|
|
|
|
|
Some(format!(
|
|
|
|
|
"Set depends_on: [{}] for **{story_name}** ({story_id}).",
|
|
|
|
|
nums.join(", ")
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
Err(e) => Some(format!("Failed to update dependencies for {story_id}: {e}")),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// Tests
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use crate::agents::AgentPool;
|
|
|
|
|
use std::collections::HashSet;
|
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
|
|
|
|
|
use super::super::{CommandDispatch, try_handle_command};
|
|
|
|
|
|
|
|
|
|
fn depends_cmd_with_root(root: &std::path::Path, args: &str) -> Option<String> {
|
|
|
|
|
let agents = Arc::new(AgentPool::new_test(3000));
|
|
|
|
|
let ambient_rooms = Arc::new(Mutex::new(HashSet::new()));
|
|
|
|
|
let room_id = "!test:example.com".to_string();
|
|
|
|
|
let dispatch = CommandDispatch {
|
|
|
|
|
bot_name: "Timmy",
|
|
|
|
|
bot_user_id: "@timmy:homeserver.local",
|
|
|
|
|
project_root: root,
|
|
|
|
|
agents: &agents,
|
|
|
|
|
ambient_rooms: &ambient_rooms,
|
|
|
|
|
room_id: &room_id,
|
|
|
|
|
};
|
|
|
|
|
try_handle_command(&dispatch, &format!("@timmy depends {args}"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
use crate::chat::test_helpers::write_story_file;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn depends_command_is_registered() {
|
|
|
|
|
use super::super::commands;
|
|
|
|
|
assert!(
|
|
|
|
|
commands().iter().any(|c| c.name == "depends"),
|
|
|
|
|
"depends command must be in the registry"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn depends_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("depends"), "help should list depends command: {output}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn depends_no_args_returns_usage() {
|
|
|
|
|
let tmp = tempfile::TempDir::new().unwrap();
|
|
|
|
|
let output = depends_cmd_with_root(tmp.path(), "").unwrap();
|
|
|
|
|
assert!(output.contains("Usage"), "no args should show usage: {output}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn depends_non_numeric_number_returns_error() {
|
|
|
|
|
let tmp = tempfile::TempDir::new().unwrap();
|
|
|
|
|
let output = depends_cmd_with_root(tmp.path(), "abc 1 2").unwrap();
|
|
|
|
|
assert!(
|
|
|
|
|
output.contains("Invalid story number"),
|
|
|
|
|
"non-numeric story number should error: {output}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn depends_not_found_returns_error() {
|
|
|
|
|
let tmp = tempfile::TempDir::new().unwrap();
|
|
|
|
|
let output = depends_cmd_with_root(tmp.path(), "999 1 2").unwrap();
|
|
|
|
|
assert!(
|
|
|
|
|
output.contains("999") && output.contains("found"),
|
|
|
|
|
"not-found should mention number: {output}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn depends_invalid_dep_number_returns_error() {
|
|
|
|
|
let tmp = tempfile::TempDir::new().unwrap();
|
|
|
|
|
write_story_file(
|
|
|
|
|
tmp.path(),
|
|
|
|
|
"1_backlog",
|
|
|
|
|
"42_story_foo.md",
|
|
|
|
|
"---\nname: Foo\n---\n",
|
|
|
|
|
);
|
|
|
|
|
let output = depends_cmd_with_root(tmp.path(), "42 abc").unwrap();
|
|
|
|
|
assert!(
|
|
|
|
|
output.contains("Invalid dependency number"),
|
|
|
|
|
"non-numeric dep should error: {output}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn depends_sets_deps_and_writes_to_file() {
|
|
|
|
|
let tmp = tempfile::TempDir::new().unwrap();
|
|
|
|
|
write_story_file(
|
|
|
|
|
tmp.path(),
|
|
|
|
|
"1_backlog",
|
|
|
|
|
"42_story_foo.md",
|
|
|
|
|
"---\nname: Foo\n---\n",
|
|
|
|
|
);
|
|
|
|
|
let output = depends_cmd_with_root(tmp.path(), "42 477 478").unwrap();
|
|
|
|
|
assert!(
|
|
|
|
|
output.contains("477") && output.contains("478"),
|
|
|
|
|
"response should mention dep numbers: {output}"
|
|
|
|
|
);
|
|
|
|
|
let contents = std::fs::read_to_string(
|
|
|
|
|
tmp.path().join(".huskies/work/1_backlog/42_story_foo.md"),
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
assert!(
|
|
|
|
|
contents.contains("depends_on: [477, 478]"),
|
|
|
|
|
"file should have depends_on set: {contents}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn depends_clears_deps_when_no_deps_given() {
|
|
|
|
|
let tmp = tempfile::TempDir::new().unwrap();
|
|
|
|
|
write_story_file(
|
|
|
|
|
tmp.path(),
|
|
|
|
|
"2_current",
|
|
|
|
|
"10_story_bar.md",
|
|
|
|
|
"---\nname: Bar\ndepends_on: [477]\n---\n",
|
|
|
|
|
);
|
|
|
|
|
let output = depends_cmd_with_root(tmp.path(), "10").unwrap();
|
|
|
|
|
assert!(
|
|
|
|
|
output.contains("Cleared"),
|
|
|
|
|
"should confirm clearing deps: {output}"
|
|
|
|
|
);
|
|
|
|
|
let contents = std::fs::read_to_string(
|
|
|
|
|
tmp.path().join(".huskies/work/2_current/10_story_bar.md"),
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
assert!(
|
|
|
|
|
!contents.contains("depends_on"),
|
|
|
|
|
"file should have depends_on cleared: {contents}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn depends_finds_story_in_any_stage() {
|
|
|
|
|
let tmp = tempfile::TempDir::new().unwrap();
|
|
|
|
|
write_story_file(
|
|
|
|
|
tmp.path(),
|
|
|
|
|
"3_qa",
|
|
|
|
|
"55_story_inqa.md",
|
|
|
|
|
"---\nname: In QA\n---\n",
|
|
|
|
|
);
|
|
|
|
|
let output = depends_cmd_with_root(tmp.path(), "55 100").unwrap();
|
|
|
|
|
assert!(
|
|
|
|
|
output.contains("In QA") || output.contains("55_story_inqa"),
|
|
|
|
|
"should find story in qa stage: {output}"
|
|
|
|
|
);
|
|
|
|
|
assert!(output.contains("100"), "should mention dep 100: {output}");
|
|
|
|
|
}
|
|
|
|
|
}
|