From bc2071ef4288574ce00a7a979747fcd2ee57b67e Mon Sep 17 00:00:00 2001 From: lingniu Date: Wed, 8 Jul 2026 14:41:53 +0800 Subject: [PATCH] fix(stats): enforce latest source endpoint only for projected daily mileage --- .superpowers/sdd/task-3-report.md | 14 ++++++++++++++ .../internal/stats/source_mileage.go | 2 +- .../internal/stats/source_mileage_test.go | 8 +++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/.superpowers/sdd/task-3-report.md b/.superpowers/sdd/task-3-report.md index eb74f4bb..993e5f01 100644 --- a/.superpowers/sdd/task-3-report.md +++ b/.superpowers/sdd/task-3-report.md @@ -66,3 +66,17 @@ cd go/vehicle-gateway && go test ./internal/stats -run TestProjectDailyMileageSe cd go/vehicle-gateway && go test ./internal/stats -count=1 # PASS: lingniu-vehicle-ingest/go/vehicle-gateway/internal/stats ``` + +## Fix (reviewer findings #2) + +Corrected the remaining `projectDailyMileageSQL` contract issue so `trusted_source_endpoint` uses only `vehicle_data_source.latest_source_endpoint`: + +- In `go/vehicle-gateway/internal/stats/source_mileage.go`, changed the projection from: + `COALESCE(NULLIF(ds.latest_source_endpoint, ''), s.source_endpoint)` + to: + `ds.latest_source_endpoint`. +- In `go/vehicle-gateway/internal/stats/source_mileage_test.go`, extended `TestProjectDailyMileageSelectsCandidateAndMarksSource` assertions to: + - require `ds.latest_source_endpoint` is selected, and + - explicitly verify no fallback expression using `s.source_endpoint` remains. + +This enforces the strict source-of-truth rule that endpoint data for final projection must come from latest connection metadata only, even when it is null/empty. diff --git a/go/vehicle-gateway/internal/stats/source_mileage.go b/go/vehicle-gateway/internal/stats/source_mileage.go index ccf572a4..9f8d65ba 100644 --- a/go/vehicle-gateway/internal/stats/source_mileage.go +++ b/go/vehicle-gateway/internal/stats/source_mileage.go @@ -192,7 +192,7 @@ SELECT s.latest_total_mileage_km, s.source_key, s.phone, - COALESCE(NULLIF(ds.latest_source_endpoint, ''), s.source_endpoint), + ds.latest_source_endpoint, s.sample_count FROM vehicle_daily_mileage_source s LEFT JOIN vehicle_data_source ds diff --git a/go/vehicle-gateway/internal/stats/source_mileage_test.go b/go/vehicle-gateway/internal/stats/source_mileage_test.go index 780cf50b..f64b7ec4 100644 --- a/go/vehicle-gateway/internal/stats/source_mileage_test.go +++ b/go/vehicle-gateway/internal/stats/source_mileage_test.go @@ -104,12 +104,18 @@ func TestProjectDailyMileageSelectsCandidateAndMarksSource(t *testing.T) { "ORDER BY COALESCE(ds.trust_priority, 100)", "s.quality_status = '" + QualityOK + "'", "s.daily_mileage_km BETWEEN 0 AND", - "COALESCE(NULLIF(ds.latest_source_endpoint, ''), s.source_endpoint)", + "ds.latest_source_endpoint", } { if !strings.Contains(projectFinal, want) { t.Fatalf("project query missing %q: %s", want, projectFinal) } } + if strings.Contains(projectFinal, "COALESCE(NULLIF(ds.latest_source_endpoint, ''), s.source_endpoint)") { + t.Fatalf("project query should not fallback to source endpoint: %s", projectFinal) + } + if strings.Contains(projectFinal, ", s.source_endpoint") { + t.Fatalf("project query should only project latest_source_endpoint: %s", projectFinal) + } if !strings.Contains(markSelected, "UPDATE vehicle_daily_mileage_source s") || !strings.Contains(markSelected, "SET s.is_selected = 1") { t.Fatalf("mark query should flag the elected source: %s", markSelected) }