Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
1,243 changes: 130 additions & 1,113 deletions src/work_item_pipeline.rs

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions src/work_item_pipeline/cap_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use super::{sdd_test_support::*, *};

#[tokio::test]
async fn sixth_fix_round_fails_the_step() {
let send: flare_workflow::json::SendMessage = std::sync::Arc::new(
move |inv: flare_workflow::json::StepInvocation| {
let p = inv.prompt;
Box::pin(async move {
let r = if p.contains("judge") {
r#"{"action":"fix_round","rationale":"x","ledger_line":"x","task_model_tier":null}"#
} else {
"REVIEW_ISSUES: x"
};
Ok((r.to_string(), 5u64, 5u64))
})
},
);
let mut d = one_task_data();
d.fix_round = MAX_FIX_ROUNDS;
d.review_issues = Some("x".to_string());
d.last_report = Some("x".to_string());
let step = build_sdd_loop_step("a".to_string(), "b".to_string(), send);
let mut ctx = WorkflowContext::new(Default::default(), d);
assert!(matches!(
step.executor.execute(&mut ctx).await.expect("x"),
StepResult::Failure
));
}
#[tokio::test]
async fn max_tasks_processed_bound_fails_the_step() {
let (send, _) = mock_send(vec![]);
let mut d = one_task_data();
d.current_task_index = MAX_TASKS_PROCESSED;
let step = build_sdd_loop_step("a".to_string(), "b".to_string(), send);
let mut ctx = WorkflowContext::new(Default::default(), d);
assert!(matches!(
step.executor.execute(&mut ctx).await.expect("x"),
StepResult::Failure
));
}
80 changes: 80 additions & 0 deletions src/work_item_pipeline/judge_decision_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use super::*;

#[test]
fn parses_valid_decision() {
let reply = r#"{"action":"advance_task","rationale":"spec met","ledger_line":"Task 0: complete","task_model_tier":null}"#;
let decision = parse_judge_decision(reply).expect("valid JSON parses");
assert_eq!(decision.action, JudgeAction::AdvanceTask);
assert_eq!(decision.ledger_line, "Task 0: complete");
}

#[test]
fn parses_decision_wrapped_in_prose_by_stripping_to_the_json_object() {
// Agents sometimes wrap JSON in a sentence despite instructions;
// strip to the first {...} span before parsing.
let reply = "Here is my decision:\n{\"action\":\"complete_pipeline\",\"rationale\":\"all tasks done\",\"ledger_line\":\"Pipeline: complete\",\"task_model_tier\":null}\nDone.";
let decision = parse_judge_decision(reply).expect("parses after stripping");
assert_eq!(decision.action, JudgeAction::CompletePipeline);
}

#[test]
fn rejects_malformed_json() {
let err = parse_judge_decision("not json at all").unwrap_err();
assert!(matches!(err, JudgeParseError::InvalidJson(_)));
}

#[test]
fn rejects_unknown_action_value() {
let reply =
r#"{"action":"do_a_barrel_roll","rationale":"x","ledger_line":"x","task_model_tier":null}"#;
let err = parse_judge_decision(reply).unwrap_err();
assert!(matches!(err, JudgeParseError::InvalidJson(_)));
}

#[test]
fn parses_decision_from_a_json_fenced_code_block() {
let reply = "Here's my decision:\n```json\n{\"action\":\"advance_task\",\"rationale\":\"spec met\",\"ledger_line\":\"Task 0: complete\",\"task_model_tier\":null}\n```\nThanks.";
let decision = parse_judge_decision(reply).expect("parses fenced block");
assert_eq!(decision.action, JudgeAction::AdvanceTask);
}

#[test]
fn parses_decision_from_a_bare_fenced_code_block_with_no_language_tag() {
let reply = "```\n{\"action\":\"skip_task\",\"rationale\":\"x\",\"ledger_line\":\"x\",\"task_model_tier\":null}\n```";
let decision = parse_judge_decision(reply).expect("parses bare fenced block");
assert_eq!(decision.action, JudgeAction::SkipTask);
}

#[test]
fn extracts_only_the_first_object_when_trailing_prose_has_its_own_unrelated_braces() {
// A naive first-`{`-to-last-`}` span would run from the real
// object's opening brace all the way through the unrelated `{cfg}`
// in the trailing sentence, producing invalid combined JSON.
let reply = "{\"action\":\"advance_task\",\"rationale\":\"spec met\",\"ledger_line\":\"Task 0: complete\",\"task_model_tier\":null}\nNote: this respects the {cfg} override.";
let decision = parse_judge_decision(reply).expect("parses despite trailing unrelated braces");
assert_eq!(decision.action, JudgeAction::AdvanceTask);
}

#[test]
fn a_brace_inside_a_json_string_value_does_not_confuse_balance_tracking() {
let reply = r#"{"action":"advance_task","rationale":"uses a {placeholder} pattern","ledger_line":"x","task_model_tier":null}"#;
let decision = parse_judge_decision(reply).expect("brace inside string value is inert");
assert_eq!(decision.action, JudgeAction::AdvanceTask);
}

#[test]
fn rejects_syntactically_valid_json_missing_a_required_field() {
// Reproduction of the live 2026-08-15 production failure (item
// #478): a well-formed JSON object that's simply missing `action`
// must still be a genuine, retried parse failure -- not silently
// defaulted.
let reply = r#"{"rationale":"x","ledger_line":"x","task_model_tier":null}"#;
let err = parse_judge_decision(reply).unwrap_err();
assert!(matches!(err, JudgeParseError::InvalidJson(msg) if msg.contains("action")));
}

#[test]
fn rejects_an_empty_reply() {
let err = parse_judge_decision("").unwrap_err();
assert!(matches!(err, JudgeParseError::InvalidJson(_)));
}
87 changes: 87 additions & 0 deletions src/work_item_pipeline/pipeline_assembly_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use super::*;

#[test]
fn sdd_pipeline_has_two_steps_with_correct_dependency() {
let send: flare_workflow::json::SendMessage =
std::sync::Arc::new(|_: flare_workflow::json::StepInvocation| {
Box::pin(async { Ok((String::new(), 0, 0)) })
});
let pipeline = build_work_item_pipeline_with_sender(
agent_registry::Agent::ClaudeCode,
agent_registry::Agent::ClaudeCode,
"Fix the null pointer in parser.rs".to_string(),
None,
std::sync::Arc::new(AgentflareMcp::default()),
"item-1".to_string(),
"opencode:test".to_string(),
None,
send,
);
assert_eq!(pipeline.steps.len(), 2);
assert_eq!(pipeline.steps[0].id.to_string(), "sdd_loop");
assert_eq!(pipeline.steps[1].id.to_string(), "finalize");
assert_eq!(pipeline.steps[1].depends_on, vec![StepId::new("sdd_loop")]);
}

#[tokio::test]
async fn sdd_loop_dispatches_implementer_and_review_roles_on_their_own_agents() {
let (send, calls) = super::sdd_test_support::mock_send(vec![
"DONE: added the flag",
r#"{"action":"advance_task","rationale":"looks done","ledger_line":"Task 0: implementer done","task_model_tier":null}"#,
]);
let pipeline = build_work_item_pipeline_with_sender(
agent_registry::Agent::Opencode,
agent_registry::Agent::ClaudeCode,
"Fix the null pointer in parser.rs".to_string(),
None,
std::sync::Arc::new(AgentflareMcp::default()),
"item-1".to_string(),
"opencode:test".to_string(),
None,
send,
);
let mut ctx =
WorkflowContext::new(Default::default(), super::sdd_test_support::one_task_data());
pipeline.steps[0]
.executor
.execute(&mut ctx)
.await
.expect("executes");

let recorded = calls.lock().unwrap();
assert_eq!(
recorded[0].0, "opencode",
"implementer role must dispatch on implementer_agent"
);
assert_eq!(
recorded[1].0, "claude-code",
"judge role must dispatch on review_agent"
);
}

/// Regression test: `sdd_loop`'s per-iteration engine timeout must not
/// fall back to `flare_workflow::WorkflowDefinition::new`'s 300s
/// library default -- a real implementer/reviewer/judge dispatch
/// routinely exceeds that, and `execute_loop` (`loops.rs`) kills the
/// whole iteration the instant it's hit ("Step timed out after 300s"),
/// which is exactly the failure every SDD-dispatched item hit before
/// this fix. It must instead line up with `supervisor::WORK_JOB_TIMEOUT_SECS`,
/// the outer job's own hard-cap budget this step runs inside.
#[test]
fn sdd_loop_timeout_matches_work_job_timeout_not_library_default() {
let send: flare_workflow::json::SendMessage =
std::sync::Arc::new(|_: flare_workflow::json::StepInvocation| {
Box::pin(async { Ok((String::new(), 0, 0)) })
});
let step = build_sdd_loop_step("agent".to_string(), "agent".to_string(), send);
let configured_timeout = step.timeout.expect("sdd_loop must set an explicit timeout");
assert_eq!(
configured_timeout,
std::time::Duration::from_secs(crate::supervisor::WORK_JOB_TIMEOUT_SECS)
);
assert_ne!(
configured_timeout,
std::time::Duration::from_secs(300),
"sdd_loop must not fall back to the flare-workflow library's 300s default"
);
}
120 changes: 120 additions & 0 deletions src/work_item_pipeline/prompt_builder_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
use super::*;

fn sample_task() -> SddTask {
SddTask {
id: 0,
title: "Add flag".to_string(),
body: "Add --verbose".to_string(),
model_tier: None,
}
}

#[test]
fn implementer_prompt_includes_task_body() {
let prompt = build_implementer_prompt(&sample_task(), None);
assert!(prompt.contains("Add --verbose"));
}

#[test]
fn implementer_prompt_includes_fix_context_when_present() {
let prompt = build_implementer_prompt(&sample_task(), Some("Reviewer found: missing test"));
assert!(prompt.contains("Reviewer found: missing test"));
}

#[test]
fn review_analyst_prompt_forbids_writing_code() {
let prompt = build_review_analyst_prompt(&sample_task(), None);
assert!(prompt.contains("analysis only"));
assert!(prompt.contains("Do not write, edit, or commit any code"));
assert!(prompt.contains("Add --verbose"));
assert!(!prompt.contains("You are implementing"));
}

#[test]
fn review_analyst_prompt_includes_fix_context_when_present() {
let prompt =
build_review_analyst_prompt(&sample_task(), Some("Second reviewer found: gap in X"));
assert!(prompt.contains("Second reviewer found: gap in X"));
}

#[test]
fn review_of_analysis_prompt_does_not_mention_code_quality() {
let prompt = build_review_of_analysis_prompt(&sample_task(), "Found no issues");
assert!(prompt.contains("Found no issues"));
assert!(prompt.contains("REVIEW_APPROVED"));
assert!(!prompt.contains("code quality"));
}

#[test]
fn judge_prompt_notes_review_only_mode_when_set() {
let prompt = build_judge_prompt(&[sample_task()], 0, &[], "Findings: none", true);
assert!(prompt.contains("review-only task"));
}

#[test]
fn judge_prompt_omits_review_only_note_by_default() {
let prompt = build_judge_prompt(&[sample_task()], 0, &[], "DONE: implemented flag", false);
assert!(!prompt.contains("review-only task"));
}

#[test]
fn judge_prompt_instructs_json_only_output() {
let prompt = build_judge_prompt(&[sample_task()], 0, &[], "DONE: implemented flag", false);
assert!(prompt.contains("JSON"));
assert!(prompt.contains("DONE: implemented flag"));
}

#[test]
fn judge_prompt_includes_ledger_history() {
let ledger = vec!["Task 0: fix round 1/5 (1 addressed)".to_string()];
let prompt = build_judge_prompt(&[sample_task()], 0, &ledger, "REVIEW_APPROVED", false);
assert!(prompt.contains("fix round 1/5"));
}

#[test]
fn judge_prompt_formats_multiple_tasks_on_separate_lines() {
let tasks = vec![
SddTask {
id: 0,
title: "Add flag".to_string(),
body: "Add --verbose".to_string(),
model_tier: None,
},
SddTask {
id: 1,
title: "Fix bug".to_string(),
body: "Fix null pointer".to_string(),
model_tier: None,
},
SddTask {
id: 2,
title: "Add docs".to_string(),
body: "Document the flag".to_string(),
model_tier: None,
},
];
let prompt = build_judge_prompt(&tasks, 1, &[], "Test role reply", false);

// Split the prompt by newlines and verify each task appears on its own line
let lines: Vec<&str> = prompt.lines().collect();

// Find the "Plan:" section and verify tasks are listed with proper line breaks
let task_lines: Vec<&str> = lines
.iter()
.filter(|line| {
line.contains("Add flag") || line.contains("Fix bug") || line.contains("Add docs")
})
.copied()
.collect();

// All three task titles should appear as separate lines (not concatenated)
assert_eq!(
task_lines.len(),
3,
"Expected 3 separate lines for 3 tasks, got: {:?}",
task_lines
);
assert!(task_lines[0].contains("Add flag"));
assert!(task_lines[1].contains("Fix bug") && task_lines[1].contains("<- current"));
assert!(task_lines[2].contains("Add docs"));
}
31 changes: 31 additions & 0 deletions src/work_item_pipeline/review_only_detection_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use super::detect_review_only;
use serde_json::json;

#[test]
fn explicit_task_type_metadata_wins() {
assert!(detect_review_only(
"implement the fix",
&json!({"task_type": "review"})
));
}

#[test]
fn review_only_phrase_in_description_is_detected() {
assert!(detect_review_only(
"REVIEW ONLY — do not fix, do not push, do not open a PR.",
&json!({}),
));
}

#[test]
fn plain_implementation_request_is_not_review_only() {
assert!(!detect_review_only(
"Fix the null pointer in parser.rs",
&json!({}),
));
}

#[test]
fn missing_metadata_field_does_not_panic() {
assert!(!detect_review_only("implement it", &json!({"size": "M"})));
}
24 changes: 24 additions & 0 deletions src/work_item_pipeline/sdd_data_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use super::*;

#[test]
fn work_item_data_roundtrips_sdd_fields() {
let data = WorkItemData {
tasks: vec![SddTask {
id: 0,
title: "Add config flag".to_string(),
body: "Add --verbose flag to CLI".to_string(),
model_tier: Some(TaskModelTier::Mechanical),
}],
current_task_index: 0,
fix_round: 0,
ledger: vec!["Task 0: dispatched".to_string()],
last_report: None,
..Default::default()
};
let json = serde_json::to_string(&data).expect("serialize");
let back: WorkItemData = serde_json::from_str(&json).expect("deserialize");
assert_eq!(back.tasks.len(), 1);
assert_eq!(back.tasks[0].title, "Add config flag");
assert_eq!(back.current_task_index, 0);
assert_eq!(back.ledger, vec!["Task 0: dispatched".to_string()]);
}
Loading
Loading