huskies: merge 738_refactor_delete_fs_shadow_code_from_lifecycle_rs_and_the_work_directory_watcher

This commit is contained in:
dave
2026-04-27 19:51:27 +00:00
parent 63a30a9319
commit 615e1c7f73
18 changed files with 105 additions and 883 deletions
+54 -71
View File
@@ -1,4 +1,7 @@
//! Story lifecycle helpers — file creation, archival, and stage transitions for pipeline items.
//! Story lifecycle helpers — archival and stage transitions for pipeline items.
//!
//! All pipeline state lives in the CRDT. These functions never consult the
//! filesystem for work-item data — CRDT lookup failures propagate as errors.
use std::path::Path;
use std::process::Command;
@@ -25,7 +28,6 @@ pub(crate) fn item_type_from_id(item_id: &str) -> &'static str {
/// `sources` stages, then updates the stage. Optionally clears front-matter
/// fields from the stored content. Returns the source stage on success.
fn move_item<'a>(
_project_root: &Path,
story_id: &str,
sources: &'a [&'a str],
target_dir: &str,
@@ -81,20 +83,6 @@ fn move_item<'a>(
return Ok(Some(src_dir));
}
// Item not found in CRDT — check the content store as a migration
// fallback. This handles items that were imported into the DB but
// haven't yet been replicated into the CRDT layer. Unlike the old
// filesystem fallback (removed — see story 517), this path reads
// from the authoritative in-memory DB and cannot cause state drift.
if let Some(mut content) = crate::db::read_content(story_id) {
for field in fields_to_clear {
content = clear_front_matter_field_in_content(&content, field);
}
crate::db::write_item_with_content(story_id, target_dir, &content);
slog!("[lifecycle] Moved '{story_id}' to work/{target_dir}/ (content store fallback)");
return Ok(Some(sources[0]));
}
if missing_ok {
slog!("[lifecycle] Work item '{story_id}' not found; skipping move to work/{target_dir}/");
return Ok(None);
@@ -114,17 +102,8 @@ fn move_item<'a>(
/// etc.) are left untouched. This prevents coders from accidentally demoting a story
/// that has already advanced past the coding stage.
/// Idempotent: if already in `2_current/`, returns Ok. If not found, logs and returns Ok.
pub fn move_story_to_current(project_root: &Path, story_id: &str) -> Result<(), String> {
move_item(
project_root,
story_id,
&["1_backlog"],
"2_current",
&[],
true,
&[],
)
.map(|_| ())
pub fn move_story_to_current(story_id: &str) -> Result<(), String> {
move_item(story_id, &["1_backlog"], "2_current", &[], true, &[]).map(|_| ())
}
/// Check whether a feature branch `feature/story-{story_id}` exists and has
@@ -162,9 +141,8 @@ pub fn feature_branch_has_unmerged_changes(project_root: &Path, story_id: &str)
///
/// Idempotent if already in `5_done/` or `6_archived/`. Errors if not found in any earlier stage.
/// Spikes may transition directly from `3_qa/` to `5_done/`, skipping the merge stage.
pub fn move_story_to_done(project_root: &Path, story_id: &str) -> Result<(), String> {
pub fn move_story_to_done(story_id: &str) -> Result<(), String> {
move_item(
project_root,
story_id,
&["2_current", "3_qa", "4_merge"],
"5_done",
@@ -178,9 +156,8 @@ pub fn move_story_to_done(project_root: &Path, story_id: &str) -> Result<(), Str
/// Move a story/bug from `work/2_current/` or `work/3_qa/` to `work/4_merge/`.
///
/// Idempotent if already in `4_merge/`. Errors if not found in `2_current/` or `3_qa/`.
pub fn move_story_to_merge(project_root: &Path, story_id: &str) -> Result<(), String> {
pub fn move_story_to_merge(story_id: &str) -> Result<(), String> {
move_item(
project_root,
story_id,
&["2_current", "3_qa"],
"4_merge",
@@ -194,9 +171,8 @@ pub fn move_story_to_merge(project_root: &Path, story_id: &str) -> Result<(), St
/// Move a story/bug from `work/2_current/` to `work/3_qa/`.
///
/// Idempotent if already in `3_qa/`. Errors if not found in `2_current/`.
pub fn move_story_to_qa(project_root: &Path, story_id: &str) -> Result<(), String> {
pub fn move_story_to_qa(story_id: &str) -> Result<(), String> {
move_item(
project_root,
story_id,
&["2_current"],
"3_qa",
@@ -208,13 +184,8 @@ pub fn move_story_to_qa(project_root: &Path, story_id: &str) -> Result<(), Strin
}
/// Move a story from `work/3_qa/` back to `work/2_current/`, clearing `review_hold` and writing notes.
pub fn reject_story_from_qa(
project_root: &Path,
story_id: &str,
notes: &str,
) -> Result<(), String> {
pub fn reject_story_from_qa(story_id: &str, notes: &str) -> Result<(), String> {
let moved = move_item(
project_root,
story_id,
&["3_qa"],
"2_current",
@@ -240,11 +211,7 @@ pub fn reject_story_from_qa(
/// Accepts `target_stage` as one of: `backlog`, `current`, `qa`, `merge`, `done`.
/// Idempotent: if the item is already in the target stage, returns Ok.
/// Returns `(from_stage, to_stage)` on success.
pub fn move_story_to_stage(
project_root: &Path,
story_id: &str,
target_stage: &str,
) -> Result<(String, String), String> {
pub fn move_story_to_stage(story_id: &str, target_stage: &str) -> Result<(String, String), String> {
const STAGES: &[(&str, &str)] = &[
("backlog", "1_backlog"),
("current", "2_current"),
@@ -267,16 +234,8 @@ pub fn move_story_to_stage(
let all_dirs: Vec<&str> = STAGES.iter().map(|(_, dir)| *dir).collect();
match move_item(
project_root,
story_id,
&all_dirs,
target_dir,
&[],
false,
&[],
)
.map_err(|_| format!("Work item '{story_id}' not found in any pipeline stage."))?
match move_item(story_id, &all_dirs, target_dir, &[], false, &[])
.map_err(|_| format!("Work item '{story_id}' not found in any pipeline stage."))?
{
Some(src_dir) => {
let from_stage = STAGES
@@ -293,9 +252,8 @@ pub fn move_story_to_stage(
/// Move a bug from `work/2_current/` or `work/1_backlog/` to `work/5_done/`.
///
/// Idempotent if already in `5_done/`. Errors if not found in `2_current/` or `1_backlog/`.
pub fn close_bug_to_archive(project_root: &Path, bug_id: &str) -> Result<(), String> {
pub fn close_bug_to_archive(bug_id: &str) -> Result<(), String> {
move_item(
project_root,
bug_id,
&["2_current", "1_backlog"],
"5_done",
@@ -313,32 +271,57 @@ mod tests {
// ── move_story_to_current tests ────────────────────────────────────────────
#[test]
fn move_story_to_current_from_content_store() {
// Seed via the content store (the DB's in-memory representation).
// CRDT is not initialised in unit tests, so move_item uses the
// content-store fallback which re-imports to the target stage.
fn move_story_to_current_from_crdt() {
// Seed via CRDT — the sole source of truth for pipeline state.
crate::db::ensure_content_store();
crate::db::write_content(
crate::db::write_item_with_content(
"99950_story_lifecycle",
"1_backlog",
"---\nname: Lifecycle Test\n---\n# Story\n",
);
let tmp = tempfile::tempdir().unwrap();
move_story_to_current(tmp.path(), "99950_story_lifecycle").unwrap();
move_story_to_current("99950_story_lifecycle").unwrap();
// Verify the content store now has the item (imported at target stage).
let content = crate::db::read_content("99950_story_lifecycle")
.expect("item should be in content store after move");
assert!(
content.contains("Lifecycle Test"),
"content should be preserved after move"
// Verify the CRDT now has the item in 2_current.
let item = crate::pipeline_state::read_typed("99950_story_lifecycle")
.expect("CRDT read should succeed")
.expect("item should exist in CRDT after move");
assert_eq!(
item.stage.dir_name(),
"2_current",
"item should be in 2_current after move"
);
}
#[test]
fn move_story_to_current_noop_when_not_found() {
let tmp = tempfile::tempdir().unwrap();
assert!(move_story_to_current(tmp.path(), "99_missing").is_ok());
assert!(move_story_to_current("99_missing").is_ok());
}
/// Lifecycle operation runs to completion using only CRDT state;
/// no `.huskies/work/<stage>/` tree is consulted because no `project_root`
/// is passed — the functions operate purely on the CRDT.
#[test]
fn move_story_uses_only_crdt_no_fs_shadow() {
crate::db::ensure_content_store();
crate::db::write_item_with_content(
"99951_story_crdt_only",
"2_current",
"---\nname: CRDT Only Test\n---\n# Story\n",
);
// No filesystem path is involved — lifecycle functions no longer
// accept a project_root, proving they never touch the filesystem.
move_story_to_done("99951_story_crdt_only").unwrap();
let item = crate::pipeline_state::read_typed("99951_story_crdt_only")
.expect("CRDT read should succeed")
.expect("item should exist in CRDT");
assert_eq!(
item.stage.dir_name(),
"5_done",
"item should be in 5_done after move"
);
}
// ── item_type_from_id tests ────────────────────────────────────────────────