From bd5be2bb3452ae3782ed7014a5536323ccc742be Mon Sep 17 00:00:00 2001 From: qi-hey <63509385+qi-hey@users.noreply.github.com> Date: Tue, 24 Nov 2026 06:04:02 +0800 Subject: [PATCH 1/2] fix(mobile): hide finished Android Live Update when viewed or covered by alerts --- .../AgentNotifications.kt | 55 +++++++++++----- .../T3AgentNotificationsModule.kt | 7 ++- .../AgentNotificationsTest.kt | 62 +++++++++++++++++++ 3 files changed, 109 insertions(+), 15 deletions(-) diff --git a/apps/mobile/modules/t3-agent-notifications/android/src/main/java/expo/modules/t3agentnotifications/AgentNotifications.kt b/apps/mobile/modules/t3-agent-notifications/android/src/main/java/expo/modules/t3agentnotifications/AgentNotifications.kt index fdfd144ebb9cc..d0d5566bbf253 100644 --- a/apps/mobile/modules/t3-agent-notifications/android/src/main/java/expo/modules/t3agentnotifications/AgentNotifications.kt +++ b/apps/mobile/modules/t3-agent-notifications/android/src/main/java/expo/modules/t3agentnotifications/AgentNotifications.kt @@ -97,6 +97,27 @@ object AgentNotifications { threadOnScreen = path } + @Synchronized + fun setThreadOnScreen(context: Context, path: String?) { + threadOnScreen = path + if (path == null) return + val prefs = context.getSharedPreferences(STORE, Context.MODE_PRIVATE) + if (prefs.getBoolean("lastActive", false)) return + val lastTerminal = prefs.getString("lastTerminal", null) ?: return + if (lastTerminal.endsWith("\ntrue") && lastTerminal.substringBefore('\n') == path) { + prefs.edit().putString("coveredTerminal", lastTerminal).apply() + cancelActivity(context) + } + } + + private fun isThreadOnScreen(path: String?): Boolean { + val resumed = ProcessLifecycleOwner.get().lifecycle.currentState.isAtLeast( + Lifecycle.State.RESUMED + ) + val visibleThread = threadOnScreen + return resumed && visibleThread != null && path == visibleThread + } + @Synchronized fun dismiss(context: Context) { context.getSharedPreferences( @@ -145,11 +166,7 @@ object AgentNotifications { ?: prefs.getStringSet("seenAlerts", emptySet()).orEmpty().toList() if (alertId != null && alertId !in seen) { // Consume suppressed alerts so retries cannot resurface them later. - val resumed = ProcessLifecycleOwner.get().lifecycle.currentState.isAtLeast( - Lifecycle.State.RESUMED - ) - val visibleThread = threadOnScreen - val onScreen = resumed && visibleThread != null && data["alert_path"] == visibleThread + val onScreen = isThreadOnScreen(data["alert_path"]) if (!onScreen) { postAlert(context, scheme, data, alertId) } @@ -226,10 +243,18 @@ object AgentNotifications { updatedAt: Long, alerted: Boolean ) { - val terminal = singleTerminal(data) - // Only a one-row card on the alert's own thread is known to show nothing more - // than the alert. Grouped alerts and cards with other rows stay visible. - val covered = terminal?.takeIf { alerted && data["alert_path"] == data["activity_path"] } + val terminal = terminalKey(data) + val singleRow = data.containsKey("activity_line_0") && !data.containsKey("activity_line_1") + // Hide a finished card when its alert already covered the visible rows or the + // user is already looking at the finished thread on screen. + val covered = terminal?.takeIf { + if (singleRow) { + (alerted && data["alert_path"] == data["activity_path"]) || + isThreadOnScreen(data["activity_path"]) + } else { + alerted && data["alert_path"] == "/" + } + } if (covered != null) coverTerminal(prefs, covered, updatedAt) if (updatedAt < prefs.getLong("lastUpdate", 0)) { // A newer replay of this same finish may already show the card. @@ -261,14 +286,16 @@ object AgentNotifications { } } - /** Identifies a finished one-thread card by its path, phase and absolute expiry. */ - private fun singleTerminal(data: Map): String? { + /** Identifies a finished card by its path, phase, scope and absolute expiry. */ + private fun terminalKey(data: Map): String? { + if (data["active"] == "true") return null val phase = data["activity_phase"] val finished = phase == "completed" || phase == "failed" - val single = data.containsKey("activity_line_0") && !data.containsKey("activity_line_1") + val hasRow = data.containsKey("activity_line_0") + val single = hasRow && !data.containsKey("activity_line_1") val expiresAt = data["activity_expires_at"] - return "${data["activity_path"]}\n$phase\n$expiresAt" - .takeIf { finished && single && expiresAt != null } + return "${data["activity_path"]}\n$phase\n$expiresAt\n$single" + .takeIf { finished && hasRow && expiresAt != null } } private fun isCovered(prefs: SharedPreferences, terminal: String?) = diff --git a/apps/mobile/modules/t3-agent-notifications/android/src/main/java/expo/modules/t3agentnotifications/T3AgentNotificationsModule.kt b/apps/mobile/modules/t3-agent-notifications/android/src/main/java/expo/modules/t3agentnotifications/T3AgentNotificationsModule.kt index cbef915cb7dad..d49b954b5a1b1 100644 --- a/apps/mobile/modules/t3-agent-notifications/android/src/main/java/expo/modules/t3agentnotifications/T3AgentNotificationsModule.kt +++ b/apps/mobile/modules/t3-agent-notifications/android/src/main/java/expo/modules/t3agentnotifications/T3AgentNotificationsModule.kt @@ -31,7 +31,12 @@ class T3AgentNotificationsModule : Module() { } Function("setThreadOnScreen") { path: String? -> - AgentNotifications.setThreadOnScreen(path) + val context = appContext.reactContext + if (context != null) { + AgentNotifications.setThreadOnScreen(context, path) + } else { + AgentNotifications.setThreadOnScreen(path) + } } Function("openLiveUpdateSettings") { diff --git a/apps/mobile/modules/t3-agent-notifications/android/src/test/java/expo/modules/t3agentnotifications/AgentNotificationsTest.kt b/apps/mobile/modules/t3-agent-notifications/android/src/test/java/expo/modules/t3agentnotifications/AgentNotificationsTest.kt index 41224273d2ec4..125e61cd6977d 100644 --- a/apps/mobile/modules/t3-agent-notifications/android/src/test/java/expo/modules/t3agentnotifications/AgentNotificationsTest.kt +++ b/apps/mobile/modules/t3-agent-notifications/android/src/test/java/expo/modules/t3agentnotifications/AgentNotificationsTest.kt @@ -381,6 +381,7 @@ class AgentNotificationsTest : AgentNotificationsFixture() { "activity_phase" to "running", ) AgentNotifications.receive(context, data) + AgentNotifications.setThreadOnScreen("/threads/environment/other") val working = manager.activeNotifications.single().notification assertEquals( "Update dashboard", @@ -824,4 +825,65 @@ class AgentNotificationsCompletionTest : AgentNotificationsFixture() { AgentNotifications.receive(context, update("revoked-permission", true)) assertEquals(5, manager.activeNotifications.size) } + + @Test + fun foregroundOnScreenCompletionHidesFinishedActivityAndStaysHiddenOnReplay() { + lifecycle.currentState = Lifecycle.State.RESUMED + AgentNotifications.receive(context, update("running", true)) + assertEquals(1, manager.activeNotifications.count { it.tag == "t3-agent-activity" }) + + val completion = finished("on-screen-done", System.currentTimeMillis() + 900000) + AgentNotifications.receive(context, completion) + assertTrue(manager.activeNotifications.isEmpty()) + + lifecycle.currentState = Lifecycle.State.CREATED + AgentNotifications.receive(context, completion.filterKeys { !it.startsWith("alert_") }) + assertTrue(manager.activeNotifications.isEmpty()) + } + + @Test + fun viewingFinishedThreadClearsRetainedActivityWithoutClearingActiveWork() { + val expiresAt = System.currentTimeMillis() + 900000 + AgentNotifications.receive(context, update("running", true)) + AgentNotifications.setThreadOnScreen(context, "/threads/environment/thread") + assertEquals(1, manager.activeNotifications.count { it.tag == "t3-agent-activity" }) + + AgentNotifications.setThreadOnScreen(context, null) + val completion = finished("retained", expiresAt).filterKeys { !it.startsWith("alert_") } + AgentNotifications.receive(context, completion) + assertEquals(1, manager.activeNotifications.count { it.tag == "t3-agent-activity" }) + + AgentNotifications.setThreadOnScreen(context, "/threads/environment/thread") + assertTrue(manager.activeNotifications.none { it.tag == "t3-agent-activity" }) + + // Replaying the same completion after viewing must not restore the card. + AgentNotifications.setThreadOnScreen(context, null) + AgentNotifications.receive(context, completion) + assertTrue(manager.activeNotifications.none { it.tag == "t3-agent-activity" }) + + // Viewing one thread does not clear a multi-row finished card with another result. + val multiRow = finished("multi-retained", expiresAt + 60000) + .filterKeys { !it.startsWith("alert_") } + + ("activity_line_1" to "Done\tMuted thread\tOther project") + AgentNotifications.receive(context, multiRow) + assertEquals(1, manager.activeNotifications.count { it.tag == "t3-agent-activity" }) + AgentNotifications.setThreadOnScreen(context, "/threads/environment/thread") + assertEquals(1, manager.activeNotifications.count { it.tag == "t3-agent-activity" }) + } + + @Test + fun groupedCompletionAlertHidesFinishedMultiRowActivity() { + val expiresAt = System.currentTimeMillis() + 900000 + val grouped = finished("grouped-done", expiresAt) + mapOf( + "activity_line_1" to "Done\tSecond thread\tOther project", + "alert_title" to "2 agents finished", + "alert_body" to "Test thread, Second thread", + "alert_path" to "/", + ) + AgentNotifications.receive(context, grouped) + assertEquals("t3-agent-alert", manager.activeNotifications.single().tag) + + AgentNotifications.receive(context, grouped.filterKeys { !it.startsWith("alert_") }) + assertEquals("t3-agent-alert", manager.activeNotifications.single().tag) + } } From a7ddd56a83cb822aa19d0b5bdd1ba848ec1e3599 Mon Sep 17 00:00:00 2001 From: qi-hey <63509385+qi-hey@users.noreply.github.com> Date: Tue, 24 Nov 2026 06:41:31 +0800 Subject: [PATCH 2/2] fix(mobile): satisfy detekt return-count and complexity limits in AgentNotifications --- .../AgentNotifications.kt | 43 ++++++++++++------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/apps/mobile/modules/t3-agent-notifications/android/src/main/java/expo/modules/t3agentnotifications/AgentNotifications.kt b/apps/mobile/modules/t3-agent-notifications/android/src/main/java/expo/modules/t3agentnotifications/AgentNotifications.kt index d0d5566bbf253..0eaa87770ae6d 100644 --- a/apps/mobile/modules/t3-agent-notifications/android/src/main/java/expo/modules/t3agentnotifications/AgentNotifications.kt +++ b/apps/mobile/modules/t3-agent-notifications/android/src/main/java/expo/modules/t3agentnotifications/AgentNotifications.kt @@ -100,13 +100,18 @@ object AgentNotifications { @Synchronized fun setThreadOnScreen(context: Context, path: String?) { threadOnScreen = path - if (path == null) return - val prefs = context.getSharedPreferences(STORE, Context.MODE_PRIVATE) - if (prefs.getBoolean("lastActive", false)) return - val lastTerminal = prefs.getString("lastTerminal", null) ?: return - if (lastTerminal.endsWith("\ntrue") && lastTerminal.substringBefore('\n') == path) { - prefs.edit().putString("coveredTerminal", lastTerminal).apply() - cancelActivity(context) + if (path != null) { + val prefs = context.getSharedPreferences(STORE, Context.MODE_PRIVATE) + val lastTerminal = prefs + .takeUnless { it.getBoolean("lastActive", false) } + ?.getString("lastTerminal", null) + val matchesSingleRow = lastTerminal != null && + lastTerminal.endsWith("\ntrue") && + lastTerminal.substringBefore('\n') == path + if (matchesSingleRow) { + prefs.edit().putString("coveredTerminal", lastTerminal).apply() + cancelActivity(context) + } } } @@ -244,17 +249,9 @@ object AgentNotifications { alerted: Boolean ) { val terminal = terminalKey(data) - val singleRow = data.containsKey("activity_line_0") && !data.containsKey("activity_line_1") // Hide a finished card when its alert already covered the visible rows or the // user is already looking at the finished thread on screen. - val covered = terminal?.takeIf { - if (singleRow) { - (alerted && data["alert_path"] == data["activity_path"]) || - isThreadOnScreen(data["activity_path"]) - } else { - alerted && data["alert_path"] == "/" - } - } + val covered = coveredTerminal(data, terminal, alerted) if (covered != null) coverTerminal(prefs, covered, updatedAt) if (updatedAt < prefs.getLong("lastUpdate", 0)) { // A newer replay of this same finish may already show the card. @@ -286,6 +283,20 @@ object AgentNotifications { } } + private fun coveredTerminal( + data: Map, + terminal: String?, + alerted: Boolean + ): String? = terminal?.takeIf { + val singleRow = data.containsKey("activity_line_0") && !data.containsKey("activity_line_1") + if (singleRow) { + (alerted && data["alert_path"] == data["activity_path"]) || + isThreadOnScreen(data["activity_path"]) + } else { + alerted && data["alert_path"] == "/" + } + } + /** Identifies a finished card by its path, phase, scope and absolute expiry. */ private fun terminalKey(data: Map): String? { if (data["active"] == "true") return null