Files
huskies/server/src/agents/pool/auto_assign/story_checks.rs
T

368 lines
14 KiB
Rust
Raw Normal View History

//! Front-matter checks for story files: review holds, blocked state, and merge failures.
use std::path::Path;
/// Read story contents from the DB content store (CRDT-backed).
fn read_story_contents(_project_root: &Path, story_id: &str) -> Option<String> {
crate::db::read_content(story_id)
}
/// Read the optional `agent:` pin for a story.
///
/// After story 871 the agent assignment lives in the CRDT typed register
/// (`PipelineItemView.agent`), not the YAML front matter. We check the CRDT
/// first; falling back to legacy YAML parsing keeps behaviour intact for any
/// stories whose CRDT entry doesn't yet have the field set.
pub(super) fn read_story_front_matter_agent(
project_root: &Path,
_stage_dir: &str,
story_id: &str,
) -> Option<String> {
if let Some(view) = crate::crdt_state::read_item(story_id)
&& let Some(agent) = view.agent.as_ref()
&& !agent.is_empty()
{
return Some(agent.clone());
}
2026-05-08 14:24:20 +00:00
use crate::db::yaml_legacy::parse_front_matter;
let contents = read_story_contents(project_root, story_id)?;
parse_front_matter(&contents).ok()?.agent
}
2026-05-12 14:43:27 +00:00
/// Return `true` if the story is in the `Frozen` pipeline stage.
///
/// In the typed CRDT model, `Frozen` is the authoritative representation of
/// stories that are held for human review (replacing the legacy
/// `review_hold: true` YAML front-matter field). The typed stage register is
/// the only source consulted — stale YAML is ignored.
pub(super) fn has_review_hold(_project_root: &Path, _stage_dir: &str, story_id: &str) -> bool {
crate::pipeline_state::read_typed(story_id)
.ok()
2026-05-12 14:43:27 +00:00
.flatten()
.map(|item| item.stage.is_frozen())
.unwrap_or(false)
}
2026-05-12 14:43:27 +00:00
/// Return `true` if the story is blocked via the typed `Stage::Blocked` or
/// `Stage::MergeFailure` variant (or the legacy `Archived(Blocked)` state).
///
/// The typed pipeline stage register is the only source consulted — the legacy
/// `blocked: true` YAML front-matter field is no longer checked.
pub(super) fn is_story_blocked(_project_root: &Path, _stage_dir: &str, story_id: &str) -> bool {
crate::pipeline_state::read_typed(story_id)
.ok()
2026-05-12 14:43:27 +00:00
.flatten()
.map(|item| item.stage.is_blocked())
.unwrap_or(false)
}
/// Return `true` if the story's merge failure contains a git content-conflict
/// marker (`"Merge conflict"` or `"CONFLICT (content):"`).
///
/// Used by the auto-assigner to decide whether to spawn mergemaster automatically.
/// The typed stage register is consulted first; the CRDT content store is then
/// scanned for conflict markers (the projection layer does not carry the reason
/// string). No YAML front-matter parsing is performed.
pub(super) fn has_content_conflict_failure(
_project_root: &Path,
_stage_dir: &str,
story_id: &str,
) -> bool {
let is_merge_failure = crate::pipeline_state::read_typed(story_id)
.ok()
.flatten()
.map(|item| {
matches!(
item.stage,
crate::pipeline_state::Stage::MergeFailure { .. }
)
})
.unwrap_or(false);
if !is_merge_failure {
return false;
}
// The projection does not carry the reason string; read the raw content
// from the CRDT content store and scan for conflict markers.
crate::db::read_content(story_id)
.map(|content| {
content.contains("Merge conflict") || content.contains("CONFLICT (content):")
})
.unwrap_or(false)
}
/// Return `true` if the CRDT `mergemaster_attempted` register is set for this story.
///
/// Used to prevent the auto-assigner from repeatedly spawning mergemaster for
/// the same story after a failed mergemaster session. The CRDT register is the
/// only source consulted — the legacy YAML field is no longer checked.
pub(super) fn has_mergemaster_attempted(
_project_root: &Path,
_stage_dir: &str,
story_id: &str,
) -> bool {
crate::crdt_state::read_item(story_id)
.and_then(|view| view.mergemaster_attempted)
.unwrap_or(false)
}
/// Return `true` if the story has any `depends_on` entries that are not yet in
/// `5_done` or `6_archived`.
///
/// Reads dependency state from the CRDT document first. Falls back to the
/// filesystem when the CRDT layer is not initialised.
pub(super) fn has_unmet_dependencies(project_root: &Path, stage_dir: &str, story_id: &str) -> bool {
// Prefer CRDT-based check.
let crdt_deps = crate::crdt_state::check_unmet_deps_crdt(story_id);
if !crdt_deps.is_empty() {
return true;
}
// If the CRDT had the item and returned empty deps, it means all are met.
if crate::pipeline_state::read_typed(story_id)
.ok()
.flatten()
.is_some()
{
return false;
}
// Fallback: filesystem check (CRDT not initialised or item not yet in CRDT).
!crate::io::story_metadata::check_unmet_deps(project_root, stage_dir, story_id).is_empty()
}
/// Return the list of dependency story numbers that are in `6_archived` (satisfied
/// via archive rather than via a clean `5_done` completion).
///
/// Used to emit a warning when backlog promotion fires because one or more deps were
/// archived. Returns an empty `Vec` when no deps are archived. Reads from CRDT
/// first; falls back to filesystem when CRDT is not initialised.
pub(super) fn check_archived_dependencies(
project_root: &Path,
stage_dir: &str,
story_id: &str,
) -> Vec<u32> {
// Prefer CRDT-based check when the item is known to CRDT.
if crate::pipeline_state::read_typed(story_id)
.ok()
.flatten()
.is_some()
{
return crate::crdt_state::check_archived_deps_crdt(story_id);
}
// Fallback: filesystem.
crate::io::story_metadata::check_archived_deps(project_root, stage_dir, story_id)
}
2026-04-29 22:12:23 +00:00
/// Return `true` if the story is in the `Frozen` pipeline stage.
///
/// Checks the typed CRDT stage via `read_typed`.
pub(super) fn is_story_frozen(_project_root: &Path, _stage_dir: &str, story_id: &str) -> bool {
crate::pipeline_state::read_typed(story_id)
.ok()
2026-04-29 22:12:23 +00:00
.flatten()
.map(|item| item.stage.is_frozen())
.unwrap_or(false)
}
// ── Tests ──────────────────────────────────────────────────────────────────
#[cfg(test)]
mod tests {
use super::*;
2026-05-12 14:43:27 +00:00
// ── has_review_hold ───────────────────────────────────────────────────────
#[test]
2026-05-12 14:43:27 +00:00
fn has_review_hold_returns_true_when_frozen() {
crate::crdt_state::init_for_test();
crate::db::ensure_content_store();
let tmp = tempfile::tempdir().unwrap();
2026-05-12 14:43:27 +00:00
crate::db::write_item_with_content(
"890_spike_frozen",
"7_frozen",
"---\nname: Frozen Spike\n---\n# Spike\n",
crate::db::ItemMeta::named("Frozen Spike"),
);
assert!(has_review_hold(tmp.path(), "3_qa", "890_spike_frozen"));
}
#[test]
fn has_review_hold_returns_false_for_qa_stage() {
crate::crdt_state::init_for_test();
crate::db::ensure_content_store();
2026-05-12 14:43:27 +00:00
let tmp = tempfile::tempdir().unwrap();
crate::db::write_item_with_content(
2026-05-12 14:43:27 +00:00
"890_spike_active_qa",
"3_qa",
2026-05-12 14:43:27 +00:00
"---\nname: Active QA Spike\n---\n# Spike\n",
crate::db::ItemMeta::named("Active QA Spike"),
);
2026-05-12 14:43:27 +00:00
assert!(!has_review_hold(tmp.path(), "3_qa", "890_spike_active_qa"));
}
#[test]
2026-05-12 14:43:27 +00:00
fn has_review_hold_returns_false_when_story_unknown() {
let tmp = tempfile::tempdir().unwrap();
2026-05-12 14:43:27 +00:00
assert!(!has_review_hold(tmp.path(), "3_qa", "99_spike_missing"));
}
2026-05-12 14:43:27 +00:00
// ── is_story_blocked — regression: typed stage is sole authority ──────────
#[test]
2026-05-12 14:43:27 +00:00
fn is_story_blocked_set_via_typed_stage_returns_true() {
crate::crdt_state::init_for_test();
crate::db::ensure_content_store();
let tmp = tempfile::tempdir().unwrap();
2026-05-12 14:43:27 +00:00
crate::db::write_item_with_content(
"890_story_blocked_set",
"2_blocked",
"---\nname: Blocked Story\n---\n",
crate::db::ItemMeta::named("Blocked Story"),
);
assert!(is_story_blocked(
tmp.path(),
"2_blocked",
"890_story_blocked_set"
));
}
#[test]
fn is_story_blocked_cleared_via_typed_stage_returns_false() {
crate::crdt_state::init_for_test();
crate::db::ensure_content_store();
let tmp = tempfile::tempdir().unwrap();
// First set to blocked.
crate::db::write_item_with_content(
"890_story_blocked_clear",
"2_blocked",
"---\nname: Clearable Story\n---\n",
crate::db::ItemMeta::named("Clearable Story"),
);
// Then clear by transitioning to an active stage.
crate::db::write_item_with_content(
"890_story_blocked_clear",
"2_current",
"---\nname: Clearable Story\n---\n",
crate::db::ItemMeta::named("Clearable Story"),
);
assert!(!is_story_blocked(
tmp.path(),
"2_current",
"890_story_blocked_clear"
));
}
2026-05-12 14:43:27 +00:00
#[test]
fn is_story_blocked_stale_yaml_is_ignored() {
crate::crdt_state::init_for_test();
crate::db::ensure_content_store();
let tmp = tempfile::tempdir().unwrap();
// YAML front matter says `blocked: true`, but the typed CRDT stage is backlog.
// After removing the YAML fallback, the function must return false.
crate::db::write_item_with_content(
"890_story_stale_yaml",
"1_backlog",
"---\nname: Stale\nblocked: true\n---\n",
crate::db::ItemMeta::named("Stale"),
);
assert!(
!is_story_blocked(tmp.path(), "1_backlog", "890_story_stale_yaml"),
"stale YAML `blocked: true` must not be reported as blocked when typed stage is Backlog"
);
}
// ── has_unmet_dependencies ────────────────────────────────────────────────
#[test]
fn has_unmet_dependencies_returns_true_when_dep_not_done() {
let tmp = tempfile::tempdir().unwrap();
let current = tmp.path().join(".huskies/work/2_current");
std::fs::create_dir_all(&current).unwrap();
std::fs::write(
current.join("10_story_blocked.md"),
"---\nname: Blocked\ndepends_on: [999]\n---\n",
)
.unwrap();
assert!(has_unmet_dependencies(
tmp.path(),
"2_current",
"10_story_blocked"
));
}
#[test]
fn has_unmet_dependencies_returns_false_when_dep_done() {
let tmp = tempfile::tempdir().unwrap();
let current = tmp.path().join(".huskies/work/2_current");
let done = tmp.path().join(".huskies/work/5_done");
std::fs::create_dir_all(&current).unwrap();
std::fs::create_dir_all(&done).unwrap();
std::fs::write(done.join("999_story_dep.md"), "---\nname: Dep\n---\n").unwrap();
std::fs::write(
current.join("10_story_ok.md"),
"---\nname: Ok\ndepends_on: [999]\n---\n",
)
.unwrap();
assert!(!has_unmet_dependencies(
tmp.path(),
"2_current",
"10_story_ok"
));
}
#[test]
fn has_unmet_dependencies_returns_false_when_no_deps() {
let tmp = tempfile::tempdir().unwrap();
let current = tmp.path().join(".huskies/work/2_current");
std::fs::create_dir_all(&current).unwrap();
std::fs::write(current.join("5_story_free.md"), "---\nname: Free\n---\n").unwrap();
assert!(!has_unmet_dependencies(
tmp.path(),
"2_current",
"5_story_free"
));
}
// ── Bug 503: archived-dep visibility ─────────────────────────────────────
/// check_archived_dependencies returns dep IDs that are in 6_archived.
#[test]
fn check_archived_dependencies_returns_archived_ids() {
let tmp = tempfile::tempdir().unwrap();
let backlog = tmp.path().join(".huskies/work/1_backlog");
let archived = tmp.path().join(".huskies/work/6_archived");
std::fs::create_dir_all(&backlog).unwrap();
std::fs::create_dir_all(&archived).unwrap();
std::fs::write(
archived.join("500_spike_crdt.md"),
"---\nname: CRDT Spike\n---\n",
)
.unwrap();
std::fs::write(
backlog.join("503_story_dependent.md"),
"---\nname: Dependent\ndepends_on: [500]\n---\n",
)
.unwrap();
let archived_deps =
check_archived_dependencies(tmp.path(), "1_backlog", "503_story_dependent");
assert_eq!(archived_deps, vec![500]);
}
/// check_archived_dependencies returns empty when dep is in 5_done (not archived).
#[test]
fn check_archived_dependencies_empty_when_dep_in_done() {
let tmp = tempfile::tempdir().unwrap();
let backlog = tmp.path().join(".huskies/work/1_backlog");
let done = tmp.path().join(".huskies/work/5_done");
std::fs::create_dir_all(&backlog).unwrap();
std::fs::create_dir_all(&done).unwrap();
std::fs::write(done.join("490_story_done.md"), "---\nname: Done\n---\n").unwrap();
std::fs::write(
backlog.join("503_story_waiting.md"),
"---\nname: Waiting\ndepends_on: [490]\n---\n",
)
.unwrap();
let archived_deps =
check_archived_dependencies(tmp.path(), "1_backlog", "503_story_waiting");
assert!(archived_deps.is_empty());
}
}