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
+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]