Auditor recorded output

← All 27 packs

Proven

Negative durations produce nonsensical output

buglow src/argus_scout/signals.py:8

The finding

Python's divmod uses floor division. For a negative timedelta, the first iteration returns n=-1 for days and a large positive remainder. For example, timedelta(seconds=-90) becomes -1d23h58m30s instead of -1m30s. This is wrong for any negative input, such as a timestamp slightly in the future due to clock skew, or an 'overdue by' calculation. The result also cannot be read back by parse_duration, even though the docstring says this function is its inverse.

    1| """Declarative signal rules: a PromQL query, a threshold, how long it must hold, and what to propose.
    2| 
    3| Rules live in a YAML file (``files.signals`` in ``scout.yaml``; see ``examples/signals.yaml``) and
    4| replace hard-coded analyzers: anything "query Prometheus, compare each series with a threshold, propose
    5| something about the ones that cross it" is a rule, not code. ::
    6| 
    7|     rules:
    8|       - name: disk-filling
    9|         query: 100 * (1 - node_filesystem_avail_bytes / node_filesystem_size_bytes)
   10|         op: ">"
   11|         threshold: 85
   12|         for: 15m
   13|         severity: warning
   14|         escalate:                       # optional: a further threshold raises the severity
   15|           - {threshold: 95, severity: critical}
   16|         labels_key: [instance, mountpoint]
   17|         rejection_ttl: 30d              # optional: a rejection is forgotten after this long
   18|         expires_after: 7d               # optional: an unanswered proposal goes stale after this long
   19|         recommendation:
   20|           title: "{mountpoint} on {instance} is {value:.0f}% full"

The work pack

Recommendation. Handle the sign separately: set sign = '-' if seconds < 0 else '', run the loop on abs(seconds), and prefix the result with sign. Alternatively, reject negative values explicitly if they are not meant to occur.

Acceptance criteria

  • Address: Negative durations produce nonsensical output
  • Keep the change scoped to the cited file/symbols unless impact requires otherwise
  • Handle the sign separately: set sign = '-' if seconds < 0 else '', run the loop on abs(seconds), and prefix the result with sign. Alternatively, reject negative values explicitly if they are not meant to occur.
  • Re-check src/argus_scout/signals.py after the fix

Status

  1. claimed
  2. resolved

The fix

b30d4ed520 fix: format_duration renders negative durations with a sign.

diff --git a/src/argus_scout/signals.py b/src/argus_scout/signals.pyindex a051622..19b228d 100644--- a/src/argus_scout/signals.py+++ b/src/argus_scout/signals.py@@ -137,11 +137,13 @@ def parse_duration(value: Any) -> timedelta:   def format_duration(value: timedelta) -> str:-    """The inverse of :func:`parse_duration`, compactly: ``timedelta(minutes=90)`` -> ``1h30m``."""+    """The inverse of :func:`parse_duration`, compactly: ``timedelta(minutes=90)`` -> ``1h30m``. A negative+    duration gets a leading ``-`` (``-1m30s``), never floor division's ``-1d23h58m30s``."""     seconds = int(value.total_seconds())     if seconds == 0:         return "0s"-    parts = []+    sign, seconds = ("-" if seconds < 0 else ""), abs(seconds)+    parts = [sign]     for unit, size in (("d", 86400), ("h", 3600), ("m", 60), ("s", 1)):         n, seconds = divmod(seconds, size)         if n:diff --git a/tests/test_signals.py b/tests/test_signals.pyindex c87e7ef..7e47ff5 100644--- a/tests/test_signals.py+++ b/tests/test_signals.py@@ -96,6 +96,8 @@ def test_parse_duration_refuses(raw): def test_format_duration():     assert format_duration(timedelta(minutes=90)) == "1h30m"     assert format_duration(timedelta(0)) == "0s"+    assert format_duration(timedelta(seconds=-90)) == "-1m30s"+    assert format_duration(-timedelta(days=1, hours=2)) == "-1d2h"   # --- templates ------------------------------------------------------------------------------------------------

How it was judged

Proven 1 test(s) fail on dev and pass with the fix: tests/test_signals.py::test_format_duration

Judged 24 Nov 2026, 04:51 UTC against dev at d0c0f66.

Teston devdev + patch
tests/test_signals.py::test_format_durationfailedpassed
The other 33 tests in the run (33 with the same result on both trees)
tests/test_signals.py::test_a_held_severity_renders_the_current_value_and_the_peakpassedpassed
tests/test_signals.py::test_a_rule_with_a_bad_template_is_invalidpassedpassed
tests/test_signals.py::test_bad_rules_are_skipped_and_reported_the_rest_loadpassedpassed
tests/test_signals.py::test_condition_breaking_resets_the_clockpassedpassed
tests/test_signals.py::test_dedup_key_is_stable_and_quotedpassedpassed
tests/test_signals.py::test_escalation_nan_and_duplicate_seriespassedpassed
tests/test_signals.py::test_evaluations_mergepassedpassed
tests/test_signals.py::test_example_file_loads_cleanlypassedpassed
tests/test_signals.py::test_example_rules_have_placeholder_values_onlypassedpassed
tests/test_signals.py::test_for_duration_holds_across_cyclespassedpassed
tests/test_signals.py::test_for_duration_survives_a_restartpassedpassed
tests/test_signals.py::test_outage_is_unknown_and_keeps_pending_statepassedpassed
tests/test_signals.py::test_parse_duration[0s-expected4]passedpassed
tests/test_signals.py::test_parse_duration[1h30m-expected2]passedpassed
tests/test_signals.py::test_parse_duration[30s-expected1]passedpassed
tests/test_signals.py::test_parse_duration[7d-expected3]passedpassed
tests/test_signals.py::test_parse_duration[90-expected0]passedpassed
tests/test_signals.py::test_parse_duration_refuses[-1]passedpassed
tests/test_signals.py::test_parse_duration_refuses[-5]passedpassed
tests/test_signals.py::test_parse_duration_refuses[1m1h]passedpassed
tests/test_signals.py::test_parse_duration_refuses[5x]passedpassed
tests/test_signals.py::test_parse_duration_refuses[True]passedpassed
tests/test_signals.py::test_parse_duration_refuses[]passedpassed
tests/test_signals.py::test_parse_duration_refuses[soon]passedpassed
tests/test_signals.py::test_peak_is_a_numeric_placeholderpassedpassed
tests/test_signals.py::test_removed_rules_lose_their_pending_statepassedpassed
tests/test_signals.py::test_render_fills_labels_and_numberspassedpassed
tests/test_signals.py::test_render_missing_label_is_a_placeholder_and_values_are_inertpassedpassed
tests/test_signals.py::test_rules_file_reloads_on_change_and_survives_a_bad_editpassedpassed
tests/test_signals.py::test_template_tricks_are_refused[{instance!r}-conversions]passedpassed
tests/test_signals.py::test_template_tricks_are_refused[{value:{threshold}}-nested]passedpassed
tests/test_signals.py::test_template_tricks_are_refused[{}-positional]passedpassed
tests/test_signals.py::test_threshold_renders_the_step_that_set_the_severitypassedpassed