Skip to content

fix: clamp negative turn duration penalties to prevent negative route durations#7416

Merged
DennisOSRM merged 1 commit intomasterfrom
fix_negative_turns
Mar 20, 2026
Merged

fix: clamp negative turn duration penalties to prevent negative route durations#7416
DennisOSRM merged 1 commit intomasterfrom
fix_negative_turns

Conversation

@DennisOSRM
Copy link
Copy Markdown
Collaborator

Issue

Fixes #5967

Negative turn duration penalties were not being clamped during traffic updates or at extraction time, unlike turn weight penalties which were. This caused edge.data.duration to go negative, producing physically impossible negative route durations.

Was this change primarily generated using an AI tool? Yes — GitHub Copilot (claude-sonnet-4.6) was used to assist with analysis, implementation, and test writing.

Tasklist

Requirements / Relations

No other PRs required.

Changes

src/updater/updater.cpp — core fix (traffic update path):

  • In update_edge, clamp the turn duration penalty using num_nodes as the floor (deciseconds), mirroring the existing weight clamping block. Log a warning when the penalty is too negative.
  • In updateTurnPenalties, add a logWARNING for negative turn_duration_penalty values (mirrors existing weight warning).

src/extractor/scripting_environment_lua.cpp — extraction-time fix:

  • In ProcessTurn, clamp turn.duration to -MAXIMAL_TURN_PENALTY / 10.0 (seconds). This prevents int16_t overflow and keeps durations in a physically meaningful range. Uses the fixed decisecond resolution (÷10), not GetMaxTurnWeight() which is scaled by weight_precision and only applies to weights.

features/testbot/traffic_turn_penalties.feature — tests:

  • New scenario reproducing issue Negative route durations #5967: a penalty of -70 on a middle turn that would yield -10s total without the fix, now correctly produces ~40s.
  • Updated two existing c→g speed expectations from 72 km/h71 km/h. The old code masked the negative penalty via a phantom-node first-edge clamp in routing_base.hpp; with the fix applied upstream the route is 0.1s longer, changing the truncated speed by 1 km/h.

Testing

All 363 testbot scenarios pass with MLD. Turn penalty feature file also verified with CH.

…5967)

Negative turn duration penalties were not being clamped during traffic
updates or at extraction time, unlike turn weight penalties. This caused
edge durations to go negative, producing physically impossible negative
route durations.

Changes:
- src/updater/updater.cpp: Clamp turn duration penalty in update_edge
  using num_nodes as the floor (mirrors existing weight clamping).
  Add logWARNING when a negative turn duration penalty would underflow.
- src/extractor/scripting_environment_lua.cpp: Clamp turn.duration at
  extraction time in ProcessTurn with a lower bound of
  -GetMaxTurnWeight(), preventing int16_t overflow and matching the
  existing upper-bound handling of turn.weight.
- features/testbot/traffic_turn_penalties.feature: Add reproducer
  scenario for issue #5967 (penalty -70 on middle turn, verifying
  duration is clamped to positive). Update two existing speed
  expectations (72->71 km/h) to match the corrected behaviour where
  negative penalties are no longer silently swallowed by the
  phantom-node first-edge clamp in routing_base.hpp.

Fixes #5967

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@DennisOSRM DennisOSRM merged commit c4675c0 into master Mar 20, 2026
23 checks passed
@DennisOSRM DennisOSRM deleted the fix_negative_turns branch March 20, 2026 18:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Negative route durations

1 participant