KeyError when the current plan is excluded by comparable_plans
The finding
comparable_plans(tariff) returns a filtered list plus left_out reasons, so some plans can be dropped. Nothing ensures the current plan tariff.plan survives that filter. If it is dropped (for example, a plan structure the cost model can't compare), costs[tariff.plan.name] raises KeyError and the rule evaluation crashes.
This is made worse because result.evaluated.add(rule.name) runs just before the lookup. The rule is recorded as evaluated, yet it produces no recommendation and is not recorded as unknown. That could let the engine treat an existing recommendation as resolved.
LAST_RUN was also already updated, so the rule is then throttled for rule.every before it fails again.
450| cheaper_rate=tariff.format_rate(cheaper.rate),
451| starts_at=f"{starts_at:%H:%M}",
452| starts_in=format_duration(starts_in),
453| energy=f"{kwh:.2f}",
454| saving=format_money(saving, tariff.currency, tariff.minor_digits),
455| currency=tariff.currency,
456| unit=rule.unit,
457| )
458| result.append(_hit(rule, key, sample.labels, sample.value, rule.min_shiftable, since, fields))
459| state.update(rule.name, pending)
460|
461|
462| async def _plan(
463| rule: TariffPlanRule,
464| client: PrometheusClient,
465| tariff: Tariff,
466| now: datetime,
467| tz: tzinfo,
468| state: SignalState,
469| result: Evaluation,
470| ) -> None:
471| last = state.pending(rule.name).get(LAST_RUN)
472| if last is not None and now - last < rule.every:
473| return # not due: neither evaluated nor unknown, so the engine leaves its recommendation alone
474| try:The work pack
Recommendation. Check tariff.plan.name in costs before marking the rule evaluated. If it is missing, set result.unknown[rule.name] to a reason such as "current plan not comparable" and return. Move result.evaluated.add(rule.name) after this check.
Acceptance criteria
- Address: KeyError when the current plan is excluded by comparable_plans
- Keep the change scoped to the cited file/symbols unless impact requires otherwise
- Check
tariff.plan.name in costsbefore marking the rule evaluated. If it is missing, setresult.unknown[rule.name]to a reason such as "current plan not comparable" and return. Moveresult.evaluated.add(rule.name)after this check. - Re-check
src/argus_scout/tariff_rules.pyafter the fix
Status
- claimed
- wontfix
How it was judged
The agent checked the finding against the code and found it wrong. No fix, no test run: the pack was closed with this note, which a person can read and overturn.
Not reachable: comparable_plans() starts its list with tariff.plan (plans = [tariff.plan]) and filters only tariff.comparison_plans, so the current plan is always in costs and costs[tariff.plan.name] cannot raise KeyError.