feat(934): typed Stage enum replaces directory-string state model

The state machine's `Stage` enum becomes the source of truth for pipeline
state. Six stages of work land together:

  1. Clean wire vocabulary (`coding`, `merge`, `merge_failure`, ...) replaces
     legacy directory-style strings (`2_current`, `4_merge`, ...) on the wire.
     `Stage::from_dir` accepted both during deployment; new writes always
     emit the clean form via `stage_dir_name`. Lexicographic `dir >= "5_done"`
     checks in lifecycle.rs become typed `matches!` checks since the new
     vocabulary doesn't sort in pipeline order.
  2. `crdt_state::write_item` takes typed `&Stage`, serialising via
     `stage_dir_name` at the CRDT boundary. `#[cfg(test)] write_item_str`
     parses legacy strings for test fixtures.
  3. `WorkItem::stage()` returns typed `crdt_state::Stage`; `stage_str()`
     is gone from the public API. Projection dispatches on the typed enum.
  4. `frozen` becomes an orthogonal CRDT register. `Stage::Frozen` and
     `PipelineEvent::Freeze`/`Unfreeze` are removed; `transition_to_frozen`/
     `unfrozen` set the flag directly without touching the stage register.
  5. Watcher sweep and `tool_update_story`'s `blocked` setter route through
     `apply_transition` so the typed transition table validates every
     stage change. `update_story` gains a `frozen` field for symmetry.
  6. One-shot startup migration rewrites pre-934 directory-style stage
     registers (and sets `frozen=true` on items previously at `7_frozen`).
     `Stage::from_dir` drops legacy aliases. The db boundary keeps a small
     normaliser so callers with legacy strings (MCP, tests) still work.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Timmy
2026-05-12 22:31:59 +01:00
parent 93443e2ff1
commit d78dd9e8f9
55 changed files with 783 additions and 584 deletions
+2 -2
View File
@@ -275,7 +275,7 @@ async fn get_work_item_content_returns_content_from_backlog() {
)
.unwrap();
// Story 929: name lives in the typed CRDT register, not in YAML on disk.
crate::crdt_state::write_item(
crate::crdt_state::write_item_str(
"42_story_foo",
"1_backlog",
Some("Foo Story"),
@@ -310,7 +310,7 @@ async fn get_work_item_content_returns_content_from_current() {
"---\nname: \"Bar Story\"\n---\n\nBar content.",
)
.unwrap();
crate::crdt_state::write_item(
crate::crdt_state::write_item_str(
"43_story_bar",
"2_current",
Some("Bar Story"),
+10 -13
View File
@@ -17,21 +17,18 @@ pub(super) async fn tool_merge_agent_work(
// Check CRDT stage before attempting merge — if already done or archived,
// return success immediately to avoid spurious error notifications.
if let Some(item) = crate::crdt_state::read_item(story_id)
&& crate::pipeline_state::Stage::from_dir(item.stage_str()).is_some_and(|s| {
matches!(
s,
crate::pipeline_state::Stage::Done { .. }
| crate::pipeline_state::Stage::Archived { .. }
)
})
&& matches!(
item.stage(),
crate::crdt_state::Stage::Done | crate::crdt_state::Stage::Archived
)
{
let stage_name = item.stage().as_dir().to_string();
return serde_json::to_string_pretty(&json!({
"story_id": story_id,
"status": "completed",
"success": true,
"message": format!(
"Story '{}' is already in '{}' — no merge needed.",
story_id, item.stage_str()
"Story '{story_id}' is already in '{stage_name}' — no merge needed.",
),
}))
.map_err(|e| format!("Serialization error: {e}"));
@@ -283,7 +280,7 @@ mod tests {
#[tokio::test]
async fn tool_merge_agent_work_already_done_returns_success() {
crate::crdt_state::init_for_test();
crate::crdt_state::write_item(
crate::crdt_state::write_item_str(
"99_story_already_done",
"5_done",
Some("Already done story"),
@@ -304,13 +301,13 @@ mod tests {
let v: serde_json::Value = serde_json::from_str(&body).unwrap();
assert_eq!(v["status"], "completed");
assert_eq!(v["success"], true);
assert!(v["message"].as_str().unwrap().contains("5_done"));
assert!(v["message"].as_str().unwrap().contains("done"));
}
#[tokio::test]
async fn tool_merge_agent_work_already_archived_returns_success() {
crate::crdt_state::init_for_test();
crate::crdt_state::write_item(
crate::crdt_state::write_item_str(
"98_story_already_archived",
"6_archived",
Some("Already archived story"),
@@ -331,7 +328,7 @@ mod tests {
let v: serde_json::Value = serde_json::from_str(&body).unwrap();
assert_eq!(v["status"], "completed");
assert_eq!(v["success"], true);
assert!(v["message"].as_str().unwrap().contains("6_archived"));
assert!(v["message"].as_str().unwrap().contains("archived"));
}
#[tokio::test]
+5 -5
View File
@@ -158,16 +158,16 @@ pub(super) async fn tool_status(args: &Value, ctx: &AppContext) -> Result<String
let root = ctx.state.get_project_root()?;
// Read from CRDT/DB content store — verify the item is in 2_current.
// Read from CRDT/DB content store — verify the item is in coding.
let typed_item = crate::pipeline_state::read_typed(story_id)
.map_err(|e| format!("Failed to read pipeline state: {e}"))?
.ok_or_else(|| format!(
"Story '{story_id}' not found in work/2_current/. Check the story_id and ensure it is in the current stage."
"Story '{story_id}' not found in coding stage. Check the story_id and ensure it is in the current stage."
))?;
if typed_item.stage.dir_name() != "2_current" {
if !matches!(typed_item.stage, crate::pipeline_state::Stage::Coding) {
return Err(format!(
"Story '{story_id}' not found in work/2_current/. Check the story_id and ensure it is in the current stage."
"Story '{story_id}' not found in coding stage. Check the story_id and ensure it is in the current stage."
));
}
@@ -353,7 +353,7 @@ mod tests {
let ctx = crate::http::context::AppContext::new_test(tmp.path().to_path_buf());
let result = tool_status(&json!({"story_id": "999_story_nonexistent"}), &ctx).await;
assert!(result.is_err());
assert!(result.unwrap_err().contains("not found in work/2_current/"));
assert!(result.unwrap_err().contains("not found in coding stage"));
}
#[tokio::test]
+14 -10
View File
@@ -126,16 +126,20 @@ pub(crate) fn tool_show_epic(args: &Value, _ctx: &AppContext) -> Result<String,
continue;
};
if member_view.epic() == Some(epic_id) {
let stage_name = match &item.stage {
Stage::Upcoming | Stage::Backlog => "backlog",
Stage::Coding => "current",
Stage::Qa => "qa",
Stage::Merge { .. } => "merge",
Stage::Done { .. } => "done",
Stage::Archived { .. } => "archived",
Stage::MergeFailure { .. } => "merge_failure",
Stage::Frozen { .. } => "frozen",
Stage::Blocked { .. } => "blocked",
// Frozen is now an orthogonal CRDT flag (story 934, stage 4).
let stage_name = if member_view.frozen() {
"frozen"
} else {
match &item.stage {
Stage::Upcoming | Stage::Backlog => "backlog",
Stage::Coding => "current",
Stage::Qa => "qa",
Stage::Merge { .. } => "merge",
Stage::Done { .. } => "done",
Stage::Archived { .. } => "archived",
Stage::MergeFailure { .. } => "merge_failure",
Stage::Blocked { .. } => "blocked",
}
};
member_items.push(json!({
"story_id": sid,
@@ -75,10 +75,7 @@ mod tests {
let item = crate::pipeline_state::read_typed(story_id)
.expect("read_typed should succeed")
.expect("item should be present");
assert!(
item.stage.is_frozen(),
"stage should be frozen after MCP freeze"
);
assert!(item.is_frozen(), "stage should be frozen after MCP freeze");
}
#[test]
@@ -109,7 +106,7 @@ mod tests {
.expect("read_typed should succeed")
.expect("item should be present");
assert!(
!item.stage.is_frozen(),
!item.is_frozen(),
"stage should not be frozen after MCP unfreeze"
);
}
@@ -71,12 +71,23 @@ pub(crate) fn tool_update_story(args: &Value, ctx: &AppContext) -> Result<String
}
}
"blocked" => {
if let Some(b) = value.as_bool() {
crate::crdt_state::set_blocked(story_id, b);
} else if value.as_str() == Some("true") {
crate::crdt_state::set_blocked(story_id, true);
} else if value.as_str() == Some("false") {
crate::crdt_state::set_blocked(story_id, false);
// Story 934, stage 5: blocked is now a stage transition,
// not a raw register write. Route through the state
// machine so invalid sources (Done/Archived/Upcoming) are
// rejected and downstream subscribers see a TransitionFired.
let want_blocked = match value {
Value::Bool(b) => Some(*b),
Value::String(s) if s == "true" => Some(true),
Value::String(s) if s == "false" => Some(false),
_ => None,
};
if let Some(true) = want_blocked {
crate::agents::lifecycle::transition_to_blocked(
story_id,
"Set via update_story",
)?;
} else if let Some(false) = want_blocked {
crate::agents::lifecycle::transition_to_unblocked(story_id)?;
}
}
"review_hold" => {
@@ -88,6 +99,24 @@ pub(crate) fn tool_update_story(args: &Value, ctx: &AppContext) -> Result<String
crate::crdt_state::set_review_hold(story_id, false);
}
}
"frozen" => {
// Story 934, stage 4: frozen is an orthogonal CRDT flag.
// Route through the state-machine API so callers see the
// canonical NotFound behaviour if the story is missing.
let want_frozen = match value {
Value::Bool(b) => Some(*b),
Value::String(s) if s == "true" => Some(true),
Value::String(s) if s == "false" => Some(false),
_ => None,
};
match want_frozen {
Some(true) => crate::pipeline_state::transition_to_frozen(story_id)
.map_err(|e| e.to_string())?,
Some(false) => crate::pipeline_state::transition_to_unfrozen(story_id)
.map_err(|e| e.to_string())?,
None => {}
}
}
"retry_count" => {
let n = value
.as_i64()
@@ -105,7 +134,8 @@ pub(crate) fn tool_update_story(args: &Value, ctx: &AppContext) -> Result<String
return Err(format!(
"Unknown front_matter field '{other}'. Story 929 removed the generic \
YAML pass-through; supported keys: name, agent, qa, epic, type, \
depends_on, blocked, review_hold, retry_count, mergemaster_attempted."
depends_on, blocked, frozen, review_hold, retry_count, \
mergemaster_attempted."
));
}
}
+6 -1
View File
@@ -137,6 +137,12 @@ pub fn load_pipeline_state(ctx: &AppContext) -> Result<PipelineState, String> {
},
epic_id,
};
// Frozen items (CRDT flag) are routed to the backlog bucket regardless
// of their underlying stage — they're paused, not progressing.
if item.is_frozen() {
state.backlog.push(story);
continue;
}
match &item.stage {
Stage::Upcoming => state.backlog.push(story), // upcoming shown with backlog
Stage::Backlog => state.backlog.push(story),
@@ -147,7 +153,6 @@ pub fn load_pipeline_state(ctx: &AppContext) -> Result<PipelineState, String> {
Stage::MergeFailure { .. } => state.merge.push(story), // show merge failures with merge
Stage::Done { .. } => state.done.push(story),
Stage::Archived { .. } => {} // skip archived
Stage::Frozen { .. } => state.backlog.push(story), // show frozen with backlog
}
}