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:
@@ -29,15 +29,14 @@ pub fn resolve_qa_mode(story_id: &str, default: QaMode) -> QaMode {
|
||||
.unwrap_or(default)
|
||||
}
|
||||
|
||||
/// Return `true` if the story is in the `Frozen` pipeline stage.
|
||||
/// Return `true` if the story's `frozen` CRDT flag is set (story 934, stage 4).
|
||||
///
|
||||
/// Checks the typed CRDT stage via `read_typed`. Used by the pipeline advance
|
||||
/// code to suppress stage transitions for frozen stories.
|
||||
/// Used by the pipeline advance code to suppress stage transitions for frozen
|
||||
/// stories. `frozen` is orthogonal to [`Stage`]: a frozen story still has its
|
||||
/// stage register intact but is paused until unfrozen.
|
||||
pub fn is_story_frozen_in_store(story_id: &str) -> bool {
|
||||
crate::pipeline_state::read_typed(story_id)
|
||||
.ok()
|
||||
.flatten()
|
||||
.map(|item| item.stage.is_frozen())
|
||||
crate::crdt_state::read_item(story_id)
|
||||
.map(|view| view.frozen())
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
|
||||
@@ -60,7 +60,6 @@ pub fn stage_metadata(stage: &str, item_id: &str) -> Option<(&'static str, Strin
|
||||
}
|
||||
Stage::Done { .. } => ("done", format!("huskies: done {item_id}")),
|
||||
Stage::Archived { .. } => ("accept", format!("huskies: accept {item_id}")),
|
||||
Stage::Frozen { .. } => ("freeze", format!("huskies: freeze {item_id}")),
|
||||
};
|
||||
Some((action, msg))
|
||||
}
|
||||
|
||||
@@ -1,18 +1,21 @@
|
||||
//! Periodic sweep of completed work items from `5_done` to `6_archived`.
|
||||
//! Periodic sweep of completed work items from `done` to `archived`.
|
||||
//!
|
||||
//! Items that have been in `5_done` for longer than the configured retention
|
||||
//! period are automatically promoted to `6_archived` via CRDT state transitions.
|
||||
//! Items in `Stage::Done` whose `merged_at` timestamp exceeds the configured
|
||||
//! retention duration are promoted to `Stage::Archived` via the canonical
|
||||
//! pipeline state machine (story 934, stage 5).
|
||||
|
||||
use crate::slog;
|
||||
use std::time::Duration;
|
||||
|
||||
/// Sweep items in `5_done` whose `merged_at` timestamp exceeds the retention
|
||||
/// duration to `6_archived` via CRDT state transitions.
|
||||
/// Sweep items in `Stage::Done` whose `merged_at` timestamp exceeds the
|
||||
/// retention duration to `Stage::Archived` via the typed transition table.
|
||||
///
|
||||
/// All state is read from and written to CRDT — no filesystem access.
|
||||
/// Worktree pruning is handled separately by the CRDT event subscriber.
|
||||
/// Routes through [`crate::pipeline_state::apply_transition`] so the
|
||||
/// `Done + Accepted → Archived` transition is validated and a
|
||||
/// `TransitionFired` event is emitted to subscribers (worktree pruning,
|
||||
/// matrix notifier, etc.).
|
||||
pub(crate) fn sweep_done_to_archived(done_retention: Duration) {
|
||||
use crate::pipeline_state::{PipelineEvent, Stage, read_all_typed, stage_dir_name, transition};
|
||||
use crate::pipeline_state::{PipelineEvent, Stage, apply_transition, read_all_typed};
|
||||
|
||||
for item in read_all_typed() {
|
||||
if let Stage::Done { merged_at, .. } = &item.stage {
|
||||
@@ -22,24 +25,10 @@ pub(crate) fn sweep_done_to_archived(done_retention: Duration) {
|
||||
.unwrap_or_default();
|
||||
if age >= done_retention {
|
||||
let story_id = item.story_id.0.clone();
|
||||
match transition(item.stage.clone(), PipelineEvent::Accepted) {
|
||||
Ok(new_stage) => {
|
||||
crate::crdt_state::write_item(
|
||||
&story_id,
|
||||
stage_dir_name(&new_stage),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Some(false),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
);
|
||||
slog!("[watcher] sweep: promoted {story_id} → 6_archived/");
|
||||
}
|
||||
match apply_transition(&story_id, PipelineEvent::Accepted, None) {
|
||||
Ok(_) => slog!("[watcher] sweep: promoted {story_id} → archived"),
|
||||
Err(e) => {
|
||||
slog!("[watcher] sweep: transition error for {story_id}: {e}");
|
||||
slog!("[watcher] sweep: transition error for {story_id}: {e}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,15 +51,15 @@ fn is_config_file_rejects_wrong_root() {
|
||||
|
||||
#[test]
|
||||
fn stage_metadata_returns_correct_actions() {
|
||||
let (action, msg) = stage_metadata("2_current", "42_story_foo").unwrap();
|
||||
let (action, msg) = stage_metadata("coding", "42_story_foo").unwrap();
|
||||
assert_eq!(action, "start");
|
||||
assert_eq!(msg, "huskies: start 42_story_foo");
|
||||
|
||||
let (action, msg) = stage_metadata("5_done", "42_story_foo").unwrap();
|
||||
let (action, msg) = stage_metadata("done", "42_story_foo").unwrap();
|
||||
assert_eq!(action, "done");
|
||||
assert_eq!(msg, "huskies: done 42_story_foo");
|
||||
|
||||
let (action, msg) = stage_metadata("6_archived", "42_story_foo").unwrap();
|
||||
let (action, msg) = stage_metadata("archived", "42_story_foo").unwrap();
|
||||
assert_eq!(action, "accept");
|
||||
assert_eq!(msg, "huskies: accept 42_story_foo");
|
||||
|
||||
@@ -157,7 +157,7 @@ fn sweep_uses_crdt_merged_at_not_utc_now() {
|
||||
let ten_seconds_ago = (chrono::Utc::now() - chrono::Duration::seconds(10)).timestamp() as f64;
|
||||
|
||||
// Write item in 5_done with an explicit past merged_at timestamp.
|
||||
crate::crdt_state::write_item(
|
||||
crate::crdt_state::write_item_str(
|
||||
"9883_story_sweep_merged_at",
|
||||
"5_done",
|
||||
Some("merged_at test"),
|
||||
@@ -190,7 +190,7 @@ fn sweep_keeps_item_newer_than_retention() {
|
||||
|
||||
let one_second_ago = (chrono::Utc::now() - chrono::Duration::seconds(1)).timestamp() as f64;
|
||||
|
||||
crate::crdt_state::write_item(
|
||||
crate::crdt_state::write_item_str(
|
||||
"9884_story_sweep_recent",
|
||||
"5_done",
|
||||
Some("recent merged_at test"),
|
||||
|
||||
Reference in New Issue
Block a user