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
+37 -29
View File
@@ -103,10 +103,9 @@ pub fn feature_branch_has_unmerged_changes(project_root: &Path, story_id: &str)
/// Spikes may transition directly from `3_qa/` to `5_done/`, skipping the merge stage.
pub fn move_story_to_done(story_id: &str) -> Result<(), String> {
let item = read_typed_or_err(story_id)?;
let dir = item.stage.dir_name();
// Idempotent: already at or past done.
if dir >= "5_done" {
if matches!(item.stage, Stage::Done { .. } | Stage::Archived { .. }) {
return Ok(());
}
@@ -134,10 +133,15 @@ pub fn move_story_to_done(story_id: &str) -> Result<(), String> {
/// Idempotent if already in `4_merge/`. Errors if not found in `2_current/` or `3_qa/`.
pub fn move_story_to_merge(story_id: &str) -> Result<(), String> {
let item = read_typed_or_err(story_id)?;
let dir = item.stage.dir_name();
// Idempotent: already at or past merge.
if dir >= "4_merge" {
if matches!(
item.stage,
Stage::Merge { .. }
| Stage::MergeFailure { .. }
| Stage::Done { .. }
| Stage::Archived { .. }
) {
return Ok(());
}
@@ -170,10 +174,16 @@ pub fn move_story_to_merge(story_id: &str) -> Result<(), String> {
/// Idempotent if already in `3_qa/`. Errors if not found in `2_current/`.
pub fn move_story_to_qa(story_id: &str) -> Result<(), String> {
let item = read_typed_or_err(story_id)?;
let dir = item.stage.dir_name();
// Idempotent: already at or past qa.
if dir >= "3_qa" {
if matches!(
item.stage,
Stage::Qa
| Stage::Merge { .. }
| Stage::MergeFailure { .. }
| Stage::Done { .. }
| Stage::Archived { .. }
) {
return Ok(());
}
@@ -349,16 +359,19 @@ fn map_stage_move_to_event(
/// Move any work item to an arbitrary pipeline stage by searching all stages.
///
/// Accepts `target_stage` as one of: `backlog`, `current`, `qa`, `merge`, `done`.
/// (`current` is the user-facing alias for the `coding` stage.)
/// 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(story_id: &str, target_stage: &str) -> Result<(String, String), String> {
// Validate target.
let target_dir = match target_stage {
"backlog" => "1_backlog",
"current" => "2_current",
"qa" => "3_qa",
"merge" => "4_merge",
"done" => "5_done",
// Validate target. We accept the user-facing aliases (which include
// "current" as the historical alias for "coding") and normalise to the
// canonical clean wire form for the idempotency check.
let target_wire = match target_stage {
"backlog" => "backlog",
"current" => "coding",
"qa" => "qa",
"merge" => "merge",
"done" => "done",
_ => {
return Err(format!(
"Invalid target_stage '{target_stage}'. Must be one of: backlog, current, qa, merge, done"
@@ -370,7 +383,7 @@ pub fn move_story_to_stage(story_id: &str, target_stage: &str) -> Result<(String
let from_name = stage_to_name(&item.stage);
// Idempotent: already in the target stage.
if item.stage.dir_name() == target_dir {
if item.stage.dir_name() == target_wire {
return Ok((target_stage.to_string(), target_stage.to_string()));
}
@@ -387,7 +400,7 @@ pub fn move_story_to_stage(story_id: &str, target_stage: &str) -> Result<(String
pub fn close_bug_to_archive(bug_id: &str) -> Result<(), String> {
let item = read_typed_or_err(bug_id)?;
if item.stage.dir_name() >= "5_done" {
if matches!(item.stage, Stage::Done { .. } | Stage::Archived { .. }) {
return Ok(());
}
@@ -415,7 +428,6 @@ fn stage_to_name(s: &Stage) -> &'static str {
Stage::MergeFailure { .. } => "merge_failure",
Stage::Done { .. } => "done",
Stage::Archived { .. } => "archived",
Stage::Frozen { .. } => "frozen",
}
}
@@ -444,8 +456,8 @@ mod tests {
.expect("item should exist in CRDT after move");
assert_eq!(
item.stage.dir_name(),
"2_current",
"item should be in 2_current after move"
"coding",
"item should be in coding after move"
);
}
@@ -476,8 +488,8 @@ mod tests {
.expect("item should exist in CRDT");
assert_eq!(
item.stage.dir_name(),
"5_done",
"item should be in 5_done after move"
"done",
"item should be in done after move"
);
}
@@ -540,11 +552,7 @@ mod tests {
let item = crate::pipeline_state::read_typed("99866_story_block_test")
.expect("read should succeed")
.expect("item should exist");
assert_eq!(
item.stage.dir_name(),
"2_current",
"should start in 2_current"
);
assert_eq!(item.stage.dir_name(), "coding", "should start in coding");
// Block via the state machine.
transition_to_blocked("99866_story_block_test", "retry limit exceeded")
@@ -556,8 +564,8 @@ mod tests {
.expect("item should exist after block");
assert_eq!(
item.stage.dir_name(),
"2_blocked",
"should be in 2_blocked after transition_to_blocked"
"blocked",
"should be in blocked after transition_to_blocked"
);
assert!(item.stage.is_blocked(), "is_blocked() should return true");
assert!(
@@ -575,8 +583,8 @@ mod tests {
.expect("item should exist after unblock");
assert_eq!(
item.stage.dir_name(),
"2_current",
"should return to 2_current after unblock"
"coding",
"should return to coding after unblock"
);
assert!(
matches!(item.stage, Stage::Coding),