← Back to Pull requests

fix(table): no negative or overflowed values between nearby snapped points #7766

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 06, 2026 20:54 UTC
Author

Issue

Refs #5855 and #6852.

Two snapped points on one directed road can get equal integer weight offsets, because each partial offset is truncated on its own. When the target lies behind the source, the table search sees a graph weight of zero instead of a negative one, and it accepts the backwards pairing as a direct path. Duration and distance are truncated separately, so they can come out negative:

  • On a car one-way road, -0.1 s becomes 214748364.6 s once scale_factor=1.6 hits the overflow guard.

  • With a traffic signal between two points at one node, master gives -2 s.

  • Next to a pedestrian area, the table reports a distance that is too short.
  • The snapped distances also had two float problems of their own, which gave -0.5 m on a legal path between two points on a long road.

    I've benefited from all the AI tools I had on my hands including but not limited to Codex 6. Astra, Sol; Gemini 4 Neon, Claude 5. Opus, Sonnet :robot:

    Fix

    1. Compare the exact position at a zero-weight seed meeting

    The table now does what requiresForwardForce and requiresBackwardForce do for point-to-point routing, but it compares positions on the geometry instead of rounded weights, which can tie.

    Where: isSourceDownstream() in many_to_many.hpp. It is called from three places, and only when all of these hold:

  • the graph weight of the meeting is exactly zero (a negative one was already rejected),

  • both searches are still on their seed at that node (parent == node on the heap entry and the bucket; in MLD one-to-many, the manual first step at a node that is also a target, at_start).
  • So the extra work is two short scans of the candidate lists, on a rare path. There is no benchmark.

    Call sites:

    | File | Search | Shape |
    |---|---|---|
    | many_to_many_ch.cpp forwardRoutingStep | CH many-to-many | all |
    | many_to_many_mld.cpp forwardRoutingStep | MLD many-to-many, forward and reverse | square, rectangular |
    | many_to_many_mld.cpp oneToManySearch | MLD one-to-many and many-to-one | 1×N, N×1 |

    If the source is downstream, the meeting is rejected like a negative one. CH then tries the node's loop edge, so addLoopWeight's assert is relaxed from < 0 to <= 0. MLD skips the meeting, and the search finds the detour elsewhere or leaves the cell unreachable.

    forwardRoutingStep gets candidates_list and target_indices, because a bucket only stores the target column, not its phantoms.

    Why not check the signs of duration and distance? The first version of this PR did that. Review showed that an area approach walk hides it. Only the approach weight is subtracted before the check. Approach duration and distance stay in the totals. In the plaza scenario, the backwards road stretch is -0.084 m and the walk is 6.49 m, so the total stays positive and the table reports 6.4 m. The legal path is 6.6 m.

    2. Three new PhantomNode fields (92 → 104 bytes)

    The existing fields cannot order two points that tie:

  • the integer weight and duration offsets are what tie,

  • location is rounded to the 1e-6 coordinate grid, so two projections closer than one grid step get the same location,

  • fwd_segment_position counts every geometry segment, including empty ones.
  • | Field | Why |
    |---|---|
    | double order_ratio | The exact, unrounded ratio from the projection on the segment. Ratio 1 is stored as ratio 0 on the next segment, so the same position always compares equal, whichever segment it was snapped from. |
    | std::uint16_t forward_order_segment | The number of segments before the point that have a length, a weight or a duration in the forward direction. |
    | std::uint16_t reverse_order_segment | The same for the reverse direction. |

    The geometry keeps a zero-length segment where compression removed a node. It holds the node's penalty, such as a traffic signal. Without a penalty it is empty, and two points on either side of it are at one position. With a penalty it separates them, because passing it takes time. A penalty can apply in one direction only, so each direction keeps its own count. uint16_t matches fwd_segment_position.

    3. A distance that grows with the position

    distanceAlongSegment() in coordinate_calculation, used in geospatial_query.hpp.

    The distance to a snapped point was measured to its rounded coordinate with greatCircleDistance. That picks its scale by the mean latitude of the two coordinates, and the scale changes every 0.05°. Two points 0.3 m apart on a 4 km road could get different scales, and the earlier one got the larger distance.

    The new function measures to the exact point at the ratio, using the scale of the whole segment. The distance therefore grows with the ratio and never exceeds the segment's length. The reverse distance is the length minus this distance. The ratio is in Web Mercator, so the function maps the point back to WGS84 rather than computing length ratio, which is wrong on long segments at high latitudes.

    4. Reverse offset summed from the end

    The forward offset adds segments from the start of the geometry. The reverse offset added them from the point toward the end, so two points summed the same segments in a different order. After a 2,200 km segment, the 0.1 m segments were lost in rounding for one point but not the other. The reverse offset is now summed from the end, the same way as the forward one.

    Edge cases

    Covered:

  • target behind source on a one-way, a reversed one-way and a two-way road

  • forward and reverse segments

  • a legal path that must win over a cheaper backwards one on a two-way road

  • the detour around a one-way loop is still found

  • ties between points 0.17 m and 30 m apart

  • two different projections that round to one coordinate

  • a traffic signal between two points at one node, in both travel directions

  • an area approach walk

  • a junction with several snapping candidates, where only one pairing is backwards

  • the same position reached from two segments, or from opposite sides of the road, costs 0

  • the same coordinate later on the road is still a different position

  • a distance-scale change, high latitude, and a long segment before short ones

  • scale_factor 1 and 1.6

  • all MLD search shapes and CH
  • Not covered:

  • Route, trip legs and match. They still use requiresForwardForce and requiresBackwardForce on rounded weights. Moving them to the new position compare is a behaviour change for those plugins, so it is left for a follow-up. Trip's matrix goes through the table and is fixed.

  • The CH loop branch. In CH, the loop-edge branch of forwardRoutingStep updates weight, duration and distance with separate std::min calls, so a cell can mix values from two paths. This is on master, this PR leaves it alone, and no test shows a wrong result from it.

  • No benchmark. The new check only runs at zero-weight seed meetings.

  • PhantomNode grows by 12 bytes. Computing the position on demand from the facade would avoid that, but it is a larger change.
  • Tests

    The tests assert routing results, not internal offsets. Most scenarios send four request shapes: square, one source, one target, and rectangular. Each one reaches a different search in MLD (manyToMany forward, oneToMany forward, oneToMany reverse, manyToMany reverse), so they are not repeats of each other.

    car uses the real profile, for side-road penalties and traffic signals. testbot gives exact control over the geometry, the speeds and the speed files.

    "Fails on base" was measured on the merge base 364ddca49, with CH and MLD under mmap. The results are the same for both.

    | Feature | Scenario | What it pins | Fails on base |
    |---|---|---|---|
    | car | Nearby reachable points retain positive durations when scaled (scale 1, 1.6) | the original bug, both directions; scale 1.6 hits the overflow guard | yes |
    | car | Nearby points with zero rounded duration still have positive distance | the distance is kept when the duration rounds to 0 | yes |
    | car | Equal rounded weights do not allow travel against a one-way road | unreachable, not negative, on forward segments | yes |
    | car | … against a reversed one-way road | the same on reverse segments | yes |
    | car | Nearby points on a one-way loop take the reachable detour | a rejected meeting still finds the 3900 m detour | yes |
    | car | A traffic signal between two points at one node keeps their order | forward_order_segment counts the zero-length penalty segment | yes |
    | car | … on a reversed one-way road | the same for reverse_order_segment | yes |
    | foot | An approach walk cannot hide backwards travel on the road | why sign checks are not enough | yes |
    | testbot | A junction keeps its legal alternatives when a zero-cost pairing is backwards | only the backwards pairing is rejected; the loop is used | yes |
    | testbot | Unrelated junction candidates do not make reverse one-way travel reachable | isSourceDownstream picks the phantom at the meeting node | yes |
    | testbot | Points that snap to the same rounded coordinate keep their order (scale 1, 1.6) | needs order_ratio, not location | yes |
    | testbot | A backwards path on a two-way road does not replace the legal path (scale 1, 1.6) | a cheaper backwards path cannot replace the legal one | yes |
    | testbot | … same rounded coordinate … on a reversed one-way road (scale 1, 1.6) | the reverse count and direction | yes |
    | testbot | Points far apart with equal rounded weights keep their order | ties are not only sub-metre, so no distance tolerance would work | yes |
    | testbot | Points on a long road keep a positive distance where the distance scale changes | distanceAlongSegment | yes |
    | testbot | A long segment before short segments keeps a positive reverse distance | reverse offset summed from the end | yes |
    | testbot | Adjacent snapped segments at the same vertex need no detour | guard: ratio 1 equals ratio 0 on the next segment | no |
    | testbot | Points on opposite sides of the road share one position | guard: one position costs 0 both ways | no |
    | testbot | A point on a long road at a high latitude has the same distance in the table and the route | guard: no length
    ratio shortcut | no |
    | testbot | Returning to the same coordinate does not erase intervening travel | guard: the order segment, not the coordinate, decides | no |

    On base, 20 of the 24 scenario runs fail. The four guards pass on base by design: they make sure the fix does not reject legal zero-cost paths and does not break distances.

    Validation

  • At 6ccde60c2, 83 scenarios pass in all six configurations: CH and MLD × mmap, directly and datastore. They come from the three table_quantization features, foot/area_table_trip, testbot/distance_matrix and testbot/duration_matrix.

  • Formatted with clang-format 22.
  • Tasklist

    - [x] self-review code for correctness and following the coding guidelines
    - [x] add tests (see testing)
    - [ ] review
    - [ ] adjust for comments

    Requirements / Relations

    Scoped to the table search and phantom snapping. It does not claim to resolve every routing issue in #5855 or #6852. No query parameters, response formats, profiles or dataset formats change.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: kucukaslan Review duration: 34625m
    All Vouch review confidence checks passed
    Residual risk 0.30 = Change Risk (0.57) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.57 · 650 lines diff across 8 files
    0.57
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #7766"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 57% (MEDIUM) · Higher = larger blast radius
    57%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 30.4% · Safe to merge
    30.4%