mirror of
https://github.com/outbackdingo/optimclaw.git
synced 2026-08-26 15:40:18 +00:00
Fix division by zero panic in ValueEstimator::is_profitable (#139)
* fix: prevent division-by-zero panic in ValueEstimator::is_profitable Guard against Decimal division by zero when price is zero. rust_decimal::Decimal panics on division by zero (unlike f64 which returns infinity), so we short-circuit before the division. When price is zero, a job is only profitable if the estimated cost is negative (i.e., we get paid to do it). Add test covering zero-price scenarios including the negative cost edge case. * style: fix pre-existing rustfmt and clippy issues in llm module Fix formatting and lint issues that cause CI Code Style check to fail: - src/llm/mod.rs: fix method chain indentation - src/llm/rig_adapter.rs: collapse multi-line single-expression statements, fix collapsible_if clippy warning
This commit is contained in:
@@ -40,6 +40,11 @@ impl ValueEstimator {
|
||||
|
||||
/// Check if a job is profitable at a given price.
|
||||
pub fn is_profitable(&self, price: Decimal, estimated_cost: Decimal) -> bool {
|
||||
if price.is_zero() {
|
||||
// With a zero price, the job is only profitable if the cost is negative.
|
||||
// This results in a positive profit and an effectively infinite margin.
|
||||
return estimated_cost < Decimal::ZERO;
|
||||
}
|
||||
let margin = (price - estimated_cost) / price;
|
||||
margin >= self.min_margin
|
||||
}
|
||||
@@ -104,4 +109,15 @@ mod tests {
|
||||
let margin = estimator.calculate_margin(dec!(100.0), dec!(70.0));
|
||||
assert_eq!(margin, dec!(0.30)); // 30%
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_profitability_zero_price() {
|
||||
let estimator = ValueEstimator::new();
|
||||
|
||||
// Zero price should return false, not panic
|
||||
assert!(!estimator.is_profitable(Decimal::ZERO, dec!(10.0)));
|
||||
assert!(!estimator.is_profitable(Decimal::ZERO, Decimal::ZERO));
|
||||
// Negative cost with zero price is profitable (we get paid to do it)
|
||||
assert!(estimator.is_profitable(Decimal::ZERO, dec!(-10.0)));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user