Commit df57e0fb
df57e0fb752eMerged
Scott Nixon <[email protected]> ยท 5 months ago ยท 1 file ยท ~1 modified
Description
feat(attractor-pipeline): implement QualityHandler GREEN (attractor-4n9)
Details
- Commit
- df57e0fb752e
- Parent
- 0c2ca1a42c36
crates/attractor-pipeline/src/handlers/quality_handler.rsmodifiedView file
@@ -1,12 +1,14 @@
1+
use std::collections::HashMap;2+
13
use async_trait::async_trait;2-
use attractor_types::{Context, Outcome, Result};4+
use attractor_dot::AttributeValue;5+
use attractor_types::{AttractorError, Context, Outcome, Result, StageStatus};36
47
use crate::graph::{PipelineGraph, PipelineNode};58
use crate::handler::NodeHandler;69
710
// ---------------------------------------------------------------------------8-
// QualityHandler โ runs a pipe-delimited list of quality checks (box shape9-
// with node_type="quality"). This is a STUB โ real implementation pending.11+
// QualityHandler โ runs a pipe-separated list of shell quality checks1012
// ---------------------------------------------------------------------------1113
1214
pub struct QualityHandler;@@ -19,12 +21,148 @@
1921
2022
async fn execute(2123
&self,22-
_node: &PipelineNode,23-
_context: &Context,24+
node: &PipelineNode,25+
context: &Context,2426
_graph: &PipelineGraph,2527
) -> Result<Outcome> {26-
// STUB: real implementation not yet written.27-
// Tests referencing this stub will fail at runtime (RED phase of TDD).28-
todo!("QualityHandler not yet implemented")28+
let node_id = &node.id;29+
30+
// Check 1: enabled=false in node attrs โ skip with success31+
if let Some(AttributeValue::Boolean(false)) = node.raw_attrs.get("enabled") {32+
return Ok(Outcome {33+
status: StageStatus::Success,34+
preferred_label: None,35+
suggested_next_ids: vec![],36+
context_updates: HashMap::new(),37+
notes: "Quality checks disabled via node attribute".into(),38+
failure_reason: None,39+
});40+
}41+
42+
// Check 2: quality_disabled=true in runtime context โ skip with success43+
if context44+
.get("quality_disabled")45+
.await46+
.and_then(|v| v.as_bool())47+
.unwrap_or(false)48+
{49+
return Ok(Outcome {50+
status: StageStatus::Success,51+
preferred_label: None,52+
suggested_next_ids: vec![],53+
context_updates: HashMap::new(),54+
notes: "Quality checks disabled via runtime flag".into(),55+
failure_reason: None,56+
});57+
}58+
59+
// Get quality_checks attribute (required)60+
let checks_str = match node.raw_attrs.get("quality_checks") {61+
Some(AttributeValue::String(s)) => s.clone(),62+
_ => {63+
return Err(AttractorError::HandlerError {64+
handler: "quality".into(),65+
node: node_id.clone(),66+
message: "missing required attribute 'quality_checks'".into(),67+
})68+
}69+
};70+
71+
// Split on '|' to get individual commands72+
let commands: Vec<&str> = checks_str.split('|').collect();73+
74+
// Apply timeout from node config, defaulting to 10 minutes75+
let timeout_dur = node76+
.timeout77+
.unwrap_or(std::time::Duration::from_secs(600));78+
79+
let mut results: Vec<serde_json::Value> = Vec::new();80+
let mut all_passed = true;81+
let mut failure_summaries: Vec<String> = Vec::new();82+
83+
for cmd in &commands {84+
let cmd = cmd.trim();85+
86+
let output =87+
match tokio::time::timeout(timeout_dur, tokio::process::Command::new("sh")88+
.arg("-c")89+
.arg(cmd)90+
.output())91+
.await92+
{93+
Ok(Ok(o)) => o,94+
Ok(Err(e)) => {95+
return Err(AttractorError::HandlerError {96+
handler: "quality".into(),97+
node: node_id.clone(),98+
message: format!("failed to spawn command '{cmd}': {e}"),99+
})100+
}101+
Err(_) => {102+
return Err(AttractorError::CommandTimeout {103+
timeout_ms: timeout_dur.as_millis() as u64,104+
})105+
}106+
};107+
108+
let passed = output.status.success();109+
let exit_code = output.status.code().unwrap_or(-1);110+
let stderr_raw = String::from_utf8_lossy(&output.stderr);111+
let stderr = if stderr_raw.len() > 8192 {112+
stderr_raw[..8192].to_string()113+
} else {114+
stderr_raw.trim_end().to_string()115+
};116+
117+
results.push(serde_json::json!({118+
"cmd": cmd,119+
"exit_code": exit_code,120+
"passed": passed,121+
"stderr": stderr,122+
}));123+
124+
if !passed {125+
all_passed = false;126+
if !stderr.is_empty() {127+
failure_summaries.push(stderr.clone());128+
}129+
break; // fail-fast: stop on first failure130+
}131+
}132+
133+
let mut context_updates: HashMap<String, serde_json::Value> = HashMap::new();134+
context_updates.insert(135+
format!("{node_id}.results"),136+
serde_json::Value::Array(results),137+
);138+
context_updates.insert(139+
format!("{node_id}.completed"),140+
serde_json::Value::Bool(all_passed),141+
);142+
143+
if !all_passed {144+
let summary = failure_summaries.join("\n");145+
context_updates.insert(146+
format!("{node_id}.failure_summary"),147+
serde_json::Value::String(summary),148+
);149+
Ok(Outcome {150+
status: StageStatus::Fail,151+
preferred_label: None,152+
suggested_next_ids: vec![],153+
context_updates,154+
notes: String::new(),155+
failure_reason: Some("one or more quality checks failed".into()),156+
})157+
} else {158+
Ok(Outcome {159+
status: StageStatus::Success,160+
preferred_label: None,161+
suggested_next_ids: vec![],162+
context_updates,163+
notes: String::new(),164+
failure_reason: None,165+
})166+
}29167
}30168
}