Skip to content
Merged
Changes from 12 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
85921a5
chore(kickoff): start #30 workstream
wren-nutthouse Mar 16, 2026
5bb4351
Merge branch 'main' into wren/issue-30-kickoff
nutt-adam Mar 16, 2026
d8209bf
docs: refine kickoff plan for issue #30 state machine and ledger
wren-nutthouse Mar 16, 2026
0c1a64b
merge main into wren/issue-30-kickoff
wren-nutthouse Mar 16, 2026
5eacd51
docs(issue-30): clarify transition validity and acceptance status
wren-nutthouse Mar 16, 2026
505454a
Merge remote-tracking branch 'origin/main' into wren/issue-30-kickoff
wren-nutthouse Mar 16, 2026
efcf807
Merge remote-tracking branch 'origin/main' into wren/issue-30-kickoff
wren-nutthouse Mar 16, 2026
90ff68c
Merge remote-tracking branch 'origin/main' into wren/issue-30-kickoff
wren-nutthouse Mar 16, 2026
844fe41
Merge remote-tracking branch 'origin/main' into wren/issue-30-kickoff
wren-nutthouse Mar 16, 2026
58bce31
feat: scaffold SDLC run ledger state model
wren-nutthouse Mar 16, 2026
26ef9c6
fix(state): address CodeRabbit findings for issue #30 PR
wren-nutthouse Mar 16, 2026
c16730c
fix: harden run-ledger lock + true idempotent retries
wren-nutthouse Mar 16, 2026
37c98c1
fix(state): validate step_id for workflow state paths
wren-nutthouse Mar 16, 2026
dc4a7e8
fix(state): make run-ledger lock panic-safe and harden tests
wren-nutthouse Mar 16, 2026
35d9242
fix: use unique temp filename for run-ledger atomic saves
wren-nutthouse Mar 16, 2026
c76b548
fix: enforce allowlist IDs in run ledger paths
wren-nutthouse Mar 16, 2026
6858731
Merge remote-tracking branch 'origin/main' into wren/issue-30-kickoff
wren-nutthouse Mar 16, 2026
189367f
Merge remote-tracking branch 'origin/main' into wren/issue-30-kickoff
wren-nutthouse Mar 16, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
299 changes: 299 additions & 0 deletions src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,115 @@ pub struct WorkflowStepOutcomeRecord {
pub side_effects: Option<Value>,
}

#[allow(dead_code)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum SdlcRunState {
Selected,
Branched,
Implemented,
Tested,
Docs,
PrOpen,
Reviewed,
ReadyToMerge,
Merged,
}

impl SdlcRunState {
#[allow(dead_code)]
fn can_transition_to(&self, next: &SdlcRunState) -> bool {
matches!(
(self, next),
(SdlcRunState::Selected, SdlcRunState::Branched)
| (SdlcRunState::Branched, SdlcRunState::Implemented)
| (SdlcRunState::Implemented, SdlcRunState::Tested)
| (SdlcRunState::Tested, SdlcRunState::Docs)
| (SdlcRunState::Docs, SdlcRunState::PrOpen)
| (SdlcRunState::PrOpen, SdlcRunState::Reviewed)
| (SdlcRunState::Reviewed, SdlcRunState::ReadyToMerge)
| (SdlcRunState::ReadyToMerge, SdlcRunState::Merged)
)
}
}

fn validate_run_id(run_id: &str) -> Result<()> {
if run_id.is_empty() || run_id.contains('/') || run_id.contains('\\') || run_id.contains("..") {
return Err(TuttiError::State(format!(
"invalid run_id '{run_id}': must not contain path separators or traversal segments"
)));
}
Ok(())
}

fn with_run_ledger_lock<T>(project_root: &Path, op: impl FnOnce() -> Result<T>) -> Result<T> {
let lock_dir = project_root.join(".tutti").join("state").join("run-ledger");
std::fs::create_dir_all(&lock_dir)?;
let lock_path = lock_dir.join(".transition.lock");
let stale_after = std::time::Duration::from_secs(30);

let mut acquired = false;
for _ in 0..50 {
match std::fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&lock_path)
{
Ok(_) => {
acquired = true;
break;
}
Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => {
if let Ok(meta) = std::fs::metadata(&lock_path)
&& let Ok(modified) = meta.modified()
&& let Ok(age) = std::time::SystemTime::now().duration_since(modified)
&& age > stale_after
{
let _ = std::fs::remove_file(&lock_path);
continue;
}
std::thread::sleep(std::time::Duration::from_millis(20));
}
Err(err) => return Err(err.into()),
}
}

if !acquired {
return Err(TuttiError::State(
"timed out waiting for run-ledger transition lock".to_string(),
));
}

let result = op();
let _ = std::fs::remove_file(lock_path);
result
}

#[allow(dead_code)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SdlcTransitionRecord {
pub from: SdlcRunState,
pub to: SdlcRunState,
pub timestamp: DateTime<Utc>,
pub actor: String,
#[serde(default)]
pub reason: Option<String>,
}

#[allow(dead_code)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SdlcRunLedgerRecord {
pub run_id: String,
pub issue_number: u64,
pub repository: String,
pub workflow_name: String,
pub state: SdlcRunState,
pub updated_at: DateTime<Utc>,
pub actor: String,
#[serde(default)]
pub transitions: Vec<SdlcTransitionRecord>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ActivityState {
Expand Down Expand Up @@ -144,6 +253,7 @@ pub fn ensure_tutti_dir(project_root: &Path) -> Result<PathBuf> {
"state/workflow-checkpoints",
"state/workflow-intents",
"state/workflow-outputs",
"state/run-ledger",
"worktrees",
"handoffs",
"logs",
Expand Down Expand Up @@ -301,6 +411,7 @@ pub fn save_workflow_output(
step_id: &str,
json: &serde_json::Value,
) -> Result<PathBuf> {
validate_run_id(run_id)?;
let dir = project_root
.join(".tutti")
.join("state")
Expand All @@ -318,6 +429,7 @@ pub fn save_workflow_checkpoint(
run_id: &str,
json: &serde_json::Value,
) -> Result<PathBuf> {
validate_run_id(run_id)?;
let dir = project_root
.join(".tutti")
.join("state")
Expand All @@ -333,6 +445,7 @@ pub fn load_workflow_checkpoint(
project_root: &Path,
run_id: &str,
) -> Result<Option<serde_json::Value>> {
validate_run_id(run_id)?;
let path = project_root
.join(".tutti")
.join("state")
Expand All @@ -346,12 +459,85 @@ pub fn load_workflow_checkpoint(
Ok(Some(value))
}

#[allow(dead_code)]
pub fn save_sdlc_run_ledger(project_root: &Path, ledger: &SdlcRunLedgerRecord) -> Result<PathBuf> {
validate_run_id(&ledger.run_id)?;
let dir = project_root.join(".tutti").join("state").join("run-ledger");
std::fs::create_dir_all(&dir)?;
let path = dir.join(format!("{}.json", ledger.run_id));
let body = serde_json::to_string_pretty(ledger)?;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
std::fs::write(&path, body)?;
Ok(path)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

#[allow(dead_code)]
pub fn load_sdlc_run_ledger(
project_root: &Path,
run_id: &str,
) -> Result<Option<SdlcRunLedgerRecord>> {
validate_run_id(run_id)?;
let path = project_root
.join(".tutti")
.join("state")
.join("run-ledger")
.join(format!("{run_id}.json"));
if !path.exists() {
return Ok(None);
}
let body = std::fs::read_to_string(path)?;
let record = serde_json::from_str(&body).map_err(|e| TuttiError::State(e.to_string()))?;
Ok(Some(record))
}

#[allow(dead_code)]
pub fn transition_sdlc_run_ledger(
project_root: &Path,
run_id: &str,
next: SdlcRunState,
actor: &str,
reason: Option<String>,
) -> Result<SdlcRunLedgerRecord> {
validate_run_id(run_id)?;
with_run_ledger_lock(project_root, || {
let mut ledger = load_sdlc_run_ledger(project_root, run_id)?.ok_or_else(|| {
TuttiError::State(format!("missing SDLC run ledger for run_id '{run_id}'"))
})?;
let previous = ledger.state.clone();

if previous == next {
return Ok(ledger);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

if !previous.can_transition_to(&next) {
return Err(TuttiError::State(format!(
"invalid SDLC transition: {:?} -> {:?}",
previous, next
)));
}

let now = Utc::now();
ledger.transitions.push(SdlcTransitionRecord {
from: previous,
to: next.clone(),
timestamp: now,
actor: actor.to_string(),
reason,
});
ledger.state = next;
ledger.updated_at = now;
ledger.actor = actor.to_string();
save_sdlc_run_ledger(project_root, &ledger)?;
Ok(ledger)
})
}

pub fn save_workflow_intent(
project_root: &Path,
run_id: &str,
step_id: &str,
record: &WorkflowStepIntentRecord,
) -> Result<PathBuf> {
validate_run_id(run_id)?;
let dir = project_root
.join(".tutti")
.join("state")
Expand All @@ -369,6 +555,7 @@ pub fn load_workflow_intent(
run_id: &str,
step_id: &str,
) -> Result<Option<WorkflowStepIntentRecord>> {
validate_run_id(run_id)?;
let path = project_root
.join(".tutti")
.join("state")
Expand Down Expand Up @@ -822,4 +1009,116 @@ mod tests {

std::fs::remove_dir_all(&dir).unwrap();
}

#[test]
fn sdlc_ledger_round_trip_and_transition() {
let dir =
std::env::temp_dir().join(format!("tutti-test-sdlc-ledger-{}", std::process::id()));
ensure_tutti_dir(&dir).unwrap();

let record = SdlcRunLedgerRecord {
run_id: "run-ledger-1".to_string(),
issue_number: 30,
repository: "nutthouse/tutti".to_string(),
workflow_name: "readiness".to_string(),
state: SdlcRunState::Selected,
updated_at: Utc::now(),
actor: "wren".to_string(),
transitions: vec![],
};

let path = save_sdlc_run_ledger(&dir, &record).unwrap();
assert!(path.exists());

let updated = transition_sdlc_run_ledger(
&dir,
"run-ledger-1",
SdlcRunState::Branched,
"wren",
Some("created branch".to_string()),
)
.unwrap();

assert_eq!(updated.state, SdlcRunState::Branched);
assert_eq!(updated.transitions.len(), 1);

std::fs::remove_dir_all(&dir).unwrap();
}

#[test]
fn sdlc_ledger_rejects_invalid_transition() {
let dir = std::env::temp_dir().join(format!(
"tutti-test-sdlc-ledger-invalid-{}",
std::process::id()
));
ensure_tutti_dir(&dir).unwrap();

let record = SdlcRunLedgerRecord {
run_id: "run-ledger-2".to_string(),
issue_number: 30,
repository: "nutthouse/tutti".to_string(),
workflow_name: "readiness".to_string(),
state: SdlcRunState::Selected,
updated_at: Utc::now(),
actor: "wren".to_string(),
transitions: vec![],
};

save_sdlc_run_ledger(&dir, &record).unwrap();
let err =
transition_sdlc_run_ledger(&dir, "run-ledger-2", SdlcRunState::Reviewed, "wren", None)
.unwrap_err();

assert!(err.to_string().contains("invalid SDLC transition"));
std::fs::remove_dir_all(&dir).unwrap();
}

#[test]
fn sdlc_ledger_allows_idempotent_transition_retry() {
let dir = std::env::temp_dir().join(format!(
"tutti-test-sdlc-ledger-idempotent-{}",
std::process::id()
));
ensure_tutti_dir(&dir).unwrap();

let record = SdlcRunLedgerRecord {
run_id: "run-ledger-3".to_string(),
issue_number: 30,
repository: "nutthouse/tutti".to_string(),
workflow_name: "readiness".to_string(),
state: SdlcRunState::Branched,
updated_at: Utc::now(),
actor: "wren".to_string(),
transitions: vec![],
};

save_sdlc_run_ledger(&dir, &record).unwrap();
let updated = transition_sdlc_run_ledger(
&dir,
"run-ledger-3",
SdlcRunState::Branched,
"retry-agent",
Some("network retry".to_string()),
)
.unwrap();

assert_eq!(updated.state, SdlcRunState::Branched);
assert!(updated.transitions.is_empty());
assert_eq!(updated.actor, "wren");
std::fs::remove_dir_all(&dir).unwrap();
}

#[test]
fn run_id_with_path_segments_is_rejected() {
let dir = std::env::temp_dir().join(format!(
"tutti-test-runid-validation-{}",
std::process::id()
));
ensure_tutti_dir(&dir).unwrap();

let err = load_workflow_checkpoint(&dir, "../escape").unwrap_err();
assert!(err.to_string().contains("invalid run_id"));

std::fs::remove_dir_all(&dir).unwrap();
}
}
Loading