From 8455148c14702a26d7df03f54a4efa9156837d48 Mon Sep 17 00:00:00 2001 From: Florian Pfaff <6773539+FlorianPfaff@users.noreply.github.com> Date: Sat, 1 Aug 2026 17:51:26 +0200 Subject: [PATCH 1/2] Add one-shot tracklet edge overflow fix --- .../agent-tracklet-edge-distance-fix.yml | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 .github/workflows/agent-tracklet-edge-distance-fix.yml diff --git a/.github/workflows/agent-tracklet-edge-distance-fix.yml b/.github/workflows/agent-tracklet-edge-distance-fix.yml new file mode 100644 index 0000000000..ca6ca0d91c --- /dev/null +++ b/.github/workflows/agent-tracklet-edge-distance-fix.yml @@ -0,0 +1,134 @@ +name: Apply tracklet edge distance overflow fix + +on: + push: + branches: + - agent/fix-tracklet-edge-distance-overflow-20260801 + +permissions: + contents: write + +jobs: + patch-and-validate: + if: github.actor != 'github-actions[bot]' + runs-on: ubuntu-latest + steps: + - name: Check out branch + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.13' + + - name: Apply focused source and regression patch + run: | + python - <<'PY' + from pathlib import Path + + source_path = Path("src/pyrecest/tracking/tracklet_graph.py") + source = source_path.read_text(encoding="utf-8") + old_source = """ distance = float(\n np.linalg.norm(np.asarray(right_state) - np.asarray(left_state))\n )\n""" + new_source = """ delta = np.asarray(right_state) - np.asarray(left_state)\n distance = float(np.hypot.reduce(np.abs(delta)))\n""" + if source.count(old_source) != 1: + raise RuntimeError("expected exactly one overflow-prone tracklet norm") + source_path.write_text(source.replace(old_source, new_source), encoding="utf-8") + + test_path = Path("tests/tracking/test_tracklet_graph.py") + tests = test_path.read_text(encoding="utf-8") + anchor = "\n\ndef test_duplicate_tracklet_ids_are_rejected() -> None:\n" + regression = """ + + def test_constant_velocity_edge_cost_preserves_extreme_finite_distance() -> None: + left = Tracklet( + "left", + 0.0, + 1.0, + np.zeros(2), + np.zeros(2), + ) + right_state = np.full(2, 1.0e308) + right = Tracklet( + "right", + 2.0, + 3.0, + right_state, + right_state, + ) + edge_cost = constant_velocity_edge_cost() + + with np.errstate(over="raise", invalid="raise"): + cost = edge_cost(left, right) + + assert np.isfinite(cost) + assert cost == pytest.approx(np.hypot(1.0e308, 1.0e308)) + """ + regression = "\n".join(line[10:] if line.startswith(" ") else line for line in regression.splitlines()) + if anchor not in tests: + raise RuntimeError("tracklet regression insertion anchor not found") + if "test_constant_velocity_edge_cost_preserves_extreme_finite_distance" in tests: + raise RuntimeError("tracklet regression already exists") + test_path.write_text(tests.replace(anchor, regression + anchor), encoding="utf-8") + PY + + - name: Install focused validation dependency + run: python -m pip install 'numpy>=2.0,<2.5' + + - name: Validate the repaired implementation + run: | + python -m py_compile \ + src/pyrecest/tracking/tracklet_graph.py \ + tests/tracking/test_tracklet_graph.py + python - <<'PY' + import importlib.util + import sys + from pathlib import Path + + import numpy as np + + source_path = Path("src/pyrecest/tracking/tracklet_graph.py") + spec = importlib.util.spec_from_file_location( + "_tracklet_graph_validation", source_path + ) + if spec is None or spec.loader is None: + raise RuntimeError("could not load tracklet graph module") + module = importlib.util.module_from_spec(spec) + sys.modules[spec.name] = module + spec.loader.exec_module(module) + + left = module.Tracklet("left", 0.0, 1.0, np.zeros(2), np.zeros(2)) + right_state = np.full(2, 1.0e308) + right = module.Tracklet( + "right", 2.0, 3.0, right_state, right_state + ) + edge_cost = module.constant_velocity_edge_cost() + with np.errstate(over="raise", invalid="raise"): + cost = edge_cost(left, right) + expected = np.hypot(1.0e308, 1.0e308) + if not np.isfinite(cost) or not np.isclose(cost, expected): + raise AssertionError((cost, expected)) + + ordinary_left = module.Tracklet( + "ordinary-left", 0.0, 1.0, [0.0, 0.0], [0.0, 0.0] + ) + ordinary_right = module.Tracklet( + "ordinary-right", 2.0, 3.0, [3.0, 4.0], [3.0, 4.0] + ) + ordinary_cost = edge_cost(ordinary_left, ordinary_right) + if ordinary_cost != 5.0: + raise AssertionError(ordinary_cost) + PY + + - name: Commit validated fix and remove one-shot workflow + run: | + rm .github/workflows/agent-tracklet-edge-distance-fix.yml + git config user.name 'github-actions[bot]' + git config user.email '41898282+github-actions[bot]@users.noreply.github.com' + git add \ + src/pyrecest/tracking/tracklet_graph.py \ + tests/tracking/test_tracklet_graph.py \ + .github/workflows/agent-tracklet-edge-distance-fix.yml + git commit -m 'Fix overflow in tracklet edge distances' + git push origin HEAD:${GITHUB_REF_NAME} From 8518b5ec676216fd9b9ee5ea45900be90b011ba0 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 1 Aug 2026 15:51:48 +0000 Subject: [PATCH 2/2] Fix overflow in tracklet edge distances --- .../agent-tracklet-edge-distance-fix.yml | 134 ------------------ src/pyrecest/tracking/tracklet_graph.py | 5 +- tests/tracking/test_tracklet_graph.py | 24 ++++ 3 files changed, 26 insertions(+), 137 deletions(-) delete mode 100644 .github/workflows/agent-tracklet-edge-distance-fix.yml diff --git a/.github/workflows/agent-tracklet-edge-distance-fix.yml b/.github/workflows/agent-tracklet-edge-distance-fix.yml deleted file mode 100644 index ca6ca0d91c..0000000000 --- a/.github/workflows/agent-tracklet-edge-distance-fix.yml +++ /dev/null @@ -1,134 +0,0 @@ -name: Apply tracklet edge distance overflow fix - -on: - push: - branches: - - agent/fix-tracklet-edge-distance-overflow-20260801 - -permissions: - contents: write - -jobs: - patch-and-validate: - if: github.actor != 'github-actions[bot]' - runs-on: ubuntu-latest - steps: - - name: Check out branch - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - - name: Set up Python - uses: actions/setup-python@v5 - with: - python-version: '3.13' - - - name: Apply focused source and regression patch - run: | - python - <<'PY' - from pathlib import Path - - source_path = Path("src/pyrecest/tracking/tracklet_graph.py") - source = source_path.read_text(encoding="utf-8") - old_source = """ distance = float(\n np.linalg.norm(np.asarray(right_state) - np.asarray(left_state))\n )\n""" - new_source = """ delta = np.asarray(right_state) - np.asarray(left_state)\n distance = float(np.hypot.reduce(np.abs(delta)))\n""" - if source.count(old_source) != 1: - raise RuntimeError("expected exactly one overflow-prone tracklet norm") - source_path.write_text(source.replace(old_source, new_source), encoding="utf-8") - - test_path = Path("tests/tracking/test_tracklet_graph.py") - tests = test_path.read_text(encoding="utf-8") - anchor = "\n\ndef test_duplicate_tracklet_ids_are_rejected() -> None:\n" - regression = """ - - def test_constant_velocity_edge_cost_preserves_extreme_finite_distance() -> None: - left = Tracklet( - "left", - 0.0, - 1.0, - np.zeros(2), - np.zeros(2), - ) - right_state = np.full(2, 1.0e308) - right = Tracklet( - "right", - 2.0, - 3.0, - right_state, - right_state, - ) - edge_cost = constant_velocity_edge_cost() - - with np.errstate(over="raise", invalid="raise"): - cost = edge_cost(left, right) - - assert np.isfinite(cost) - assert cost == pytest.approx(np.hypot(1.0e308, 1.0e308)) - """ - regression = "\n".join(line[10:] if line.startswith(" ") else line for line in regression.splitlines()) - if anchor not in tests: - raise RuntimeError("tracklet regression insertion anchor not found") - if "test_constant_velocity_edge_cost_preserves_extreme_finite_distance" in tests: - raise RuntimeError("tracklet regression already exists") - test_path.write_text(tests.replace(anchor, regression + anchor), encoding="utf-8") - PY - - - name: Install focused validation dependency - run: python -m pip install 'numpy>=2.0,<2.5' - - - name: Validate the repaired implementation - run: | - python -m py_compile \ - src/pyrecest/tracking/tracklet_graph.py \ - tests/tracking/test_tracklet_graph.py - python - <<'PY' - import importlib.util - import sys - from pathlib import Path - - import numpy as np - - source_path = Path("src/pyrecest/tracking/tracklet_graph.py") - spec = importlib.util.spec_from_file_location( - "_tracklet_graph_validation", source_path - ) - if spec is None or spec.loader is None: - raise RuntimeError("could not load tracklet graph module") - module = importlib.util.module_from_spec(spec) - sys.modules[spec.name] = module - spec.loader.exec_module(module) - - left = module.Tracklet("left", 0.0, 1.0, np.zeros(2), np.zeros(2)) - right_state = np.full(2, 1.0e308) - right = module.Tracklet( - "right", 2.0, 3.0, right_state, right_state - ) - edge_cost = module.constant_velocity_edge_cost() - with np.errstate(over="raise", invalid="raise"): - cost = edge_cost(left, right) - expected = np.hypot(1.0e308, 1.0e308) - if not np.isfinite(cost) or not np.isclose(cost, expected): - raise AssertionError((cost, expected)) - - ordinary_left = module.Tracklet( - "ordinary-left", 0.0, 1.0, [0.0, 0.0], [0.0, 0.0] - ) - ordinary_right = module.Tracklet( - "ordinary-right", 2.0, 3.0, [3.0, 4.0], [3.0, 4.0] - ) - ordinary_cost = edge_cost(ordinary_left, ordinary_right) - if ordinary_cost != 5.0: - raise AssertionError(ordinary_cost) - PY - - - name: Commit validated fix and remove one-shot workflow - run: | - rm .github/workflows/agent-tracklet-edge-distance-fix.yml - git config user.name 'github-actions[bot]' - git config user.email '41898282+github-actions[bot]@users.noreply.github.com' - git add \ - src/pyrecest/tracking/tracklet_graph.py \ - tests/tracking/test_tracklet_graph.py \ - .github/workflows/agent-tracklet-edge-distance-fix.yml - git commit -m 'Fix overflow in tracklet edge distances' - git push origin HEAD:${GITHUB_REF_NAME} diff --git a/src/pyrecest/tracking/tracklet_graph.py b/src/pyrecest/tracking/tracklet_graph.py index 063b992eca..710fb01b55 100644 --- a/src/pyrecest/tracking/tracklet_graph.py +++ b/src/pyrecest/tracking/tracklet_graph.py @@ -207,9 +207,8 @@ def edge_cost(left: Tracklet, right: Tracklet) -> float: dt = max(gap, 1.0e-9) left_state = left.end_state[state_slice] right_state = right.start_state[state_slice] - distance = float( - np.linalg.norm(np.asarray(right_state) - np.asarray(left_state)) - ) + delta = np.asarray(right_state) - np.asarray(left_state) + distance = float(np.hypot.reduce(np.abs(delta))) speed = distance / dt if max_speed_value is not None and speed > max_speed_value: return float("inf") diff --git a/tests/tracking/test_tracklet_graph.py b/tests/tracking/test_tracklet_graph.py index 57b96398b0..3617daffcc 100644 --- a/tests/tracking/test_tracklet_graph.py +++ b/tests/tracking/test_tracklet_graph.py @@ -51,6 +51,30 @@ def test_k_best_tracklet_paths_prefers_feasible_low_cost_chain() -> None: assert paths[0].length == 2 +def test_constant_velocity_edge_cost_preserves_extreme_finite_distance() -> None: + left = Tracklet( + "left", + 0.0, + 1.0, + np.zeros(2), + np.zeros(2), + ) + right_state = np.full(2, 1.0e308) + right = Tracklet( + "right", + 2.0, + 3.0, + right_state, + right_state, + ) + edge_cost = constant_velocity_edge_cost() + + with np.errstate(over="raise", invalid="raise"): + cost = edge_cost(left, right) + + assert np.isfinite(cost) + assert cost == pytest.approx(np.hypot(1.0e308, 1.0e308)) + def test_duplicate_tracklet_ids_are_rejected() -> None: tracklets = [ _tracklet("dup", 0.0, 1.0, 0.0, 1.0),