Fix Phase 2b edge case: mostRecentPacketTime when all nodes have lastHeard=0

- Add takeIf check to distinguish lastHeard=0 (never heard) from no nodes
- Previously: maxOfOrNull returns 0, Elvis operator doesn't trigger (0 is not null)
- Now: takeIf { it > 0 } filters out zero, falling back to current time
- Ensures API returns meaningful timestamp instead of epoch 1970

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
James Rich
2026-05-21 15:54:50 -05:00
parent 3e9fdde9a6
commit 175602102e

View File

@@ -260,7 +260,10 @@ class AiFunctionProviderImpl(
// Find most recent packet: max lastHeard across all nodes (convert seconds to ms)
val mostRecentPacketTimeMs =
nodeMap.values.maxOfOrNull { it.lastHeard }?.toLong()?.times(MS_PER_SEC)
nodeMap.values.maxOfOrNull { it.lastHeard }
?.takeIf { it > 0 }
?.toLong()
?.times(MS_PER_SEC)
?: clock.now().toEpochMilliseconds()
// Get local device uptime from its DeviceMetrics (node #0 is typically the local device)