diff --git a/openpilot/starpilot/flm/offroad_partial.py b/openpilot/starpilot/flm/offroad_partial.py index 8ddf24f96c..2215f09b4a 100644 --- a/openpilot/starpilot/flm/offroad_partial.py +++ b/openpilot/starpilot/flm/offroad_partial.py @@ -12,7 +12,9 @@ from openpilot.starpilot.flm.offroad_route import ( RouteInputError, RouteInputCancelled, route_reader, source_revisions, ) from openpilot.starpilot.galaxy.drive_analysis import _route_names, _closed_source, _Incomplete -from openpilot.starpilot.galaxy.drive_history import _segment_files, DIR_FLAGS, SEGMENT_NAME +from openpilot.starpilot.galaxy.drive_history import _segment_files, DIR_FLAGS, SEGMENT_NAME, MAX_ROOT_ENTRIES + +MAX_LONG_REVIEW_SEGMENTS = 32 def partial_inventory(root, route_id, *, permitted): @@ -24,8 +26,10 @@ def partial_inventory(root, route_id, *, permitted): match = SEGMENT_NAME.fullmatch(name) assert match is not None number = int(match.group('number')) - if number >= MAX_SEGMENTS or number in numbers: - raise RouteInputError(route_id, 'Ambiguous or oversized partial recording') + if number >= MAX_ROOT_ENTRIES: + raise RouteInputError(route_id, 'Partial recording exceeds the inventory bound') + if number in numbers: + raise RouteInputError(route_id, 'Duplicate original segment number') numbers[number] = name if not numbers: raise RouteInputError(route_id, 'No original segments available') @@ -56,6 +60,11 @@ def walk_partial_route(root, route, *, permitted, consume): started = time.monotonic() receipts, decoded, omitted, contexts, seen = [], [], [], set(), set() cp_hash, first, last, wall, total = None, None, 0, 0, 0 + available = [row['number'] for row in route['segments'] if not row['unavailable']] + selected = set(available) + if len(route['segments']) > MAX_SEGMENTS and len(available) > MAX_LONG_REVIEW_SEGMENTS: + selected = {available[i * (len(available) - 1) // (MAX_LONG_REVIEW_SEGMENTS - 1)] for i in range(MAX_LONG_REVIEW_SEGMENTS)} + byte_bound = False def allowed(): return permitted() and time.monotonic() - started <= MAX_ANALYSIS_SECONDS @@ -68,11 +77,16 @@ def walk_partial_route(root, route, *, permitted, consume): if segment['unavailable']: omitted.append((number, 'Segment is open, missing or unavailable')) continue + if number not in selected or byte_bound: + omitted.append((number, 'Outside the bounded partial-drive review')) + continue try: with _closed_source(root, name, segment['files'], allowed, prefer_rlog=True) as (raw, codec, current, _): + if total + len(raw) > MAX_TOTAL_BYTES: + byte_bound = True + omitted.append((number, 'Partial input byte bound reached')) + continue total += len(raw) - if total > MAX_TOTAL_BYTES: - raise RouteInputCancelled('Partial input byte bound exceeded') receipts[-1] = (name, hashlib.sha256(raw).hexdigest(), len(raw)) if receipts[-1][1] in seen: omitted.append((number, 'Original segment bytes already reviewed')) diff --git a/openpilot/starpilot/flm/offroad_route.py b/openpilot/starpilot/flm/offroad_route.py index 53f65e288b..5a100825e8 100644 --- a/openpilot/starpilot/flm/offroad_route.py +++ b/openpilot/starpilot/flm/offroad_route.py @@ -119,7 +119,7 @@ def source_revisions(root, route): segments = route.get('segments', ()) if not segments and type(route.get('routeId')) is str and LOCAL_ID.fullmatch(route['routeId']): try: - segments = [{'segmentName': name} for name in sorted(_route_names(Path(root), route['routeId'], lambda: True))[:MAX_SEGMENTS]] + segments = [{'segmentName': name} for name in sorted(_route_names(Path(root), route['routeId'], lambda: True))] except _Incomplete: segments = () for segment in segments: diff --git a/openpilot/starpilot/flm/tests/test_offroad_long_partial.py b/openpilot/starpilot/flm/tests/test_offroad_long_partial.py new file mode 100644 index 0000000000..fdb9ae0988 --- /dev/null +++ b/openpilot/starpilot/flm/tests/test_offroad_long_partial.py @@ -0,0 +1,65 @@ +"""Declared long recordings: bounded suggestions, never automatic qualification.""" + +from pathlib import Path +from unittest.mock import patch + +import pytest + +from openpilot.starpilot.flm.offroad_partial import MAX_LONG_REVIEW_SEGMENTS, partial_inventory, walk_partial_route +from openpilot.starpilot.flm.offroad_route import PartialRouteReceipt, RouteInputError, latest_route, source_revisions +from openpilot.starpilot.flm.offroad_store import KEY, RouteStorage +from openpilot.starpilot.flm.tests import test_offroad_coordinate_policy as fixtures +from openpilot.starpilot.flm.tests import test_offroad_fault_isolation as recordings + + +def test_355_segment_drive_gets_bounded_partial_review_and_preserves_saved_tune(tmp_path): + root = tmp_path / 'logs' + maker = recordings.TestPartialDriveReview() + for number in range(355): + name, path, _ = maker.segment(root, 18, number, ending='endOfRoute' if number == 354 else 'endOfSegment') + params, worker = recordings.TestRouteFaultIsolation().setup_worker(str(tmp_path), name) + saved = fixtures.TestSavedSetupCapacity.row(1) + assert RouteStorage(params).save(saved, expected=None, authorized=lambda: True) + before = params.get(KEY) + inventory = latest_route(params, root, permitted=lambda: True, inventory_only=True) + assert inventory['selection_error'] == 'Latest drive is open, incomplete or unavailable' + revisions = source_revisions(root, inventory) + result = worker.run_once() + assert result['partial_drive'] and result['partial_segments'] == [MAX_LONG_REVIEW_SEGMENTS, 355] + assert result['analysis_counts']['recorded_lateral_request_rows'] == MAX_LONG_REVIEW_SEGMENTS * 50 + assert result['configuration_changed'] is False and params.get(KEY) == before + document = worker.cache.get(result['assessment_sha256']) + receipt = document['assessment']['route'] + assert receipt['decoded_segments'][0] == 0 and receipt['decoded_segments'][-1] == 354 + assert len(receipt['omitted_segments']) == 355 - MAX_LONG_REVIEW_SEGMENTS + assert receipt['completed_wall_ns'] == 0 and document['kind'] == 'partial_assessment' + # An unchanged long route is cached, while a late repair/damage is noticed. + with patch('openpilot.starpilot.flm.offroad_partial.analyze_partial', side_effect=AssertionError('Unchanged input was repeated')): + assert worker.run_once()['partial_drive'] + path.write_bytes(b'damaged late segment') + assert source_revisions(root, inventory) != revisions + later = worker.run_once() + assert later['partial_segments'] == [MAX_LONG_REVIEW_SEGMENTS - 1, 355] + assert params.get(KEY) == before and worker.cache.get(result['assessment_sha256']) == document + + +def test_byte_limit_retains_only_already_decoded_complete_segments(tmp_path): + maker = recordings.TestPartialDriveReview() + name, first, _ = maker.segment(tmp_path, 18, 0) + maker.segment(tmp_path, 18, 1, ending='endOfRoute') + route = partial_inventory(tmp_path, name, permitted=lambda: True) + consumed = [] + with patch('openpilot.starpilot.flm.offroad_partial.MAX_TOTAL_BYTES', first.stat().st_size): + receipt = walk_partial_route(tmp_path, route, permitted=lambda: True, consume=lambda n, rows: consumed.append(n)) + assert type(receipt) is PartialRouteReceipt and consumed == [0] + assert receipt.omitted_segments == ((1, 'Partial input byte bound reached'),) + assert receipt.completed_wall_ns == 0 + + +def test_real_duplicate_segment_numbers_still_refuse(tmp_path): + name, first, _ = recordings.TestPartialDriveReview().segment(tmp_path, 18, 0) + alias = Path(str(first.parent)[:-1] + '00') + alias.mkdir() + (alias / first.name).write_bytes(first.read_bytes()) + with pytest.raises(RouteInputError, match='Duplicate original segment number'): + partial_inventory(tmp_path, name, permitted=lambda: True) diff --git a/openpilot/starpilot/galaxy/drive_history.py b/openpilot/starpilot/galaxy/drive_history.py index 9ab24ca882..1c798008c9 100644 --- a/openpilot/starpilot/galaxy/drive_history.py +++ b/openpilot/starpilot/galaxy/drive_history.py @@ -167,8 +167,11 @@ class DriveHistory: result['scanIncomplete'] = True continue existing = routes.get(route_id) - if existing is not None and (number in existing or len(existing) >= self.max_segments_per_route): - omit(route_id, 'Duplicate segment or route segment limit exceeded') + if existing is not None and number in existing: + omit(route_id, 'Duplicate original segment number') + continue + if existing is not None and len(existing) >= self.max_segments_per_route: + omit(route_id, 'Route segment limit exceeded') continue if segment_count >= self.max_segments: result['scanIncomplete'] = True diff --git a/prebuilt.json b/prebuilt.json index 62e664dcbf..b9a1e97ec5 100644 --- a/prebuilt.json +++ b/prebuilt.json @@ -1 +1 @@ -{"protected_inventory_sha256": "aaf94cb59dc0e5d644f82884683be3b973f96cd1f9d86d33950c0c1a3a55de5b", "schema_version": 1} +{"protected_inventory_sha256": "bcd4344331c028b2779153fefd6653a01caf6d0cd74f790023f7f0cca053ddcb", "schema_version": 1}