Negative durations produce nonsensical output
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 onabs(seconds), and prefix the result withsign. Alternatively, reject negative values explicitly if they are not meant to occur. - Re-check
src/argus_scout/signals.pyafter the fix
Status
- claimed
- resolved
The fix
b30d4ed520 fix: format_duration renders negative durations with a sign.
@@ -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:@@ -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.
| Test | on dev | dev + patch |
|---|---|---|
tests/test_signals.py::test_format_duration | failed | passed |
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_peak | passed | passed |
tests/test_signals.py::test_a_rule_with_a_bad_template_is_invalid | passed | passed |
tests/test_signals.py::test_bad_rules_are_skipped_and_reported_the_rest_load | passed | passed |
tests/test_signals.py::test_condition_breaking_resets_the_clock | passed | passed |
tests/test_signals.py::test_dedup_key_is_stable_and_quoted | passed | passed |
tests/test_signals.py::test_escalation_nan_and_duplicate_series | passed | passed |
tests/test_signals.py::test_evaluations_merge | passed | passed |
tests/test_signals.py::test_example_file_loads_cleanly | passed | passed |
tests/test_signals.py::test_example_rules_have_placeholder_values_only | passed | passed |
tests/test_signals.py::test_for_duration_holds_across_cycles | passed | passed |
tests/test_signals.py::test_for_duration_survives_a_restart | passed | passed |
tests/test_signals.py::test_outage_is_unknown_and_keeps_pending_state | passed | passed |
tests/test_signals.py::test_parse_duration[0s-expected4] | passed | passed |
tests/test_signals.py::test_parse_duration[1h30m-expected2] | passed | passed |
tests/test_signals.py::test_parse_duration[30s-expected1] | passed | passed |
tests/test_signals.py::test_parse_duration[7d-expected3] | passed | passed |
tests/test_signals.py::test_parse_duration[90-expected0] | passed | passed |
tests/test_signals.py::test_parse_duration_refuses[-1] | passed | passed |
tests/test_signals.py::test_parse_duration_refuses[-5] | passed | passed |
tests/test_signals.py::test_parse_duration_refuses[1m1h] | passed | passed |
tests/test_signals.py::test_parse_duration_refuses[5x] | passed | passed |
tests/test_signals.py::test_parse_duration_refuses[True] | passed | passed |
tests/test_signals.py::test_parse_duration_refuses[] | passed | passed |
tests/test_signals.py::test_parse_duration_refuses[soon] | passed | passed |
tests/test_signals.py::test_peak_is_a_numeric_placeholder | passed | passed |
tests/test_signals.py::test_removed_rules_lose_their_pending_state | passed | passed |
tests/test_signals.py::test_render_fills_labels_and_numbers | passed | passed |
tests/test_signals.py::test_render_missing_label_is_a_placeholder_and_values_are_inert | passed | passed |
tests/test_signals.py::test_rules_file_reloads_on_change_and_survives_a_bad_edit | passed | passed |
tests/test_signals.py::test_template_tricks_are_refused[{instance!r}-conversions] | passed | passed |
tests/test_signals.py::test_template_tricks_are_refused[{value:{threshold}}-nested] | passed | passed |
tests/test_signals.py::test_template_tricks_are_refused[{}-positional] | passed | passed |
tests/test_signals.py::test_threshold_renders_the_step_that_set_the_severity | passed | passed |