From 175602102e7f916b36c05225dae43158fc79850e Mon Sep 17 00:00:00 2001 From: James Rich Date: Thu, 21 May 2026 15:54:50 -0500 Subject: [PATCH] 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> --- .../org/meshtastic/core/data/ai/AiFunctionProviderImpl.kt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/data/src/commonMain/kotlin/org/meshtastic/core/data/ai/AiFunctionProviderImpl.kt b/core/data/src/commonMain/kotlin/org/meshtastic/core/data/ai/AiFunctionProviderImpl.kt index 5628f4ca3..95e09b11d 100644 --- a/core/data/src/commonMain/kotlin/org/meshtastic/core/data/ai/AiFunctionProviderImpl.kt +++ b/core/data/src/commonMain/kotlin/org/meshtastic/core/data/ai/AiFunctionProviderImpl.kt @@ -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)