Skip to content

Commit e7405d4

Browse files
committed
fix(sessions): fence Android history run authority
1 parent 38c05d1 commit e7405d4

2 files changed

Lines changed: 55 additions & 8 deletions

File tree

‎apps/android/app/src/main/java/ai/openclaw/app/chat/ChatController.kt‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3848,7 +3848,6 @@ class ChatController internal constructor(
38483848
?.runId
38493849
?.trim()
38503850
?.takeIf { it.isNotEmpty() }
3851-
latestAppliedInFlightRunId = snapshotRunId
38523851
val optimisticRunIds = runIdsToReconcile.filterTo(mutableSetOf()) { optimisticMessagesByRunId.containsKey(it) }
38533852
prunePersistedOptimisticMessages(history.messages)
38543853
if (snapshotRunId == null) {
@@ -3901,7 +3900,7 @@ class ChatController internal constructor(
39013900
// All live history paths (bootstrap, reconnect recovery, cache-first
39023901
// replace) adopt the gateway's in-flight run snapshot so restored
39033902
// runs keep their pending state and streaming text.
3904-
adoptInFlightRun(history, runIdsOwnedAfterRequest)
3903+
latestAppliedInFlightRunId = adoptInFlightRun(history, runIdsOwnedAfterRequest)
39053904
publishRunPresentation()
39063905
history.thinkingLevel
39073906
?.trim()
@@ -6177,20 +6176,21 @@ class ChatController internal constructor(
61776176
private fun adoptInFlightRun(
61786177
history: ChatHistory,
61796178
runIdsOwnedAfterRequest: Set<String>,
6180-
) {
6181-
val run = history.inFlightRun ?: return
6182-
val runId = run.runId.trim().takeIf { it.isNotEmpty() } ?: return
6183-
if (runIdsOwnedAfterRequest.isNotEmpty() && runId !in runIdsOwnedAfterRequest) return
6179+
): String? {
6180+
val run = history.inFlightRun ?: return null
6181+
val runId = run.runId.trim().takeIf { it.isNotEmpty() } ?: return null
6182+
if (runIdsOwnedAfterRequest.isNotEmpty() && runId !in runIdsOwnedAfterRequest) return null
61846183
synchronized(pendingRuns) {
61856184
// A different locally-owned run means this snapshot predates it; ignore.
6186-
if (pendingRuns.isNotEmpty() && runId !in pendingRuns) return
6187-
if (pendingRuns.isEmpty() && unresolvedRepliesByRunId.isNotEmpty() && !unresolvedRepliesByRunId.containsKey(runId)) return
6185+
if (pendingRuns.isNotEmpty() && runId !in pendingRuns) return null
6186+
if (pendingRuns.isEmpty() && unresolvedRepliesByRunId.isNotEmpty() && !unresolvedRepliesByRunId.containsKey(runId)) return null
61886187
pendingRuns.add(runId)
61896188
}
61906189
armPendingRunTimeout(runId)
61916190
if (run.text.isNotEmpty()) {
61926191
_streamingAssistantText.value = run.text
61936192
}
6193+
return runId
61946194
}
61956195

61966196
private fun armPendingRunTimeout(runId: String) {

‎apps/android/app/src/test/java/ai/openclaw/app/chat/ChatControllerReconnectRestoreTest.kt‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,53 @@ class ChatControllerReconnectRestoreTest {
909909
assertEquals(listOf("local work"), controller.messageTexts)
910910
}
911911

912+
@Test
913+
fun rejectedHistoryPredecessorCannotAuthorizeItsObserverDigest() =
914+
runTest {
915+
val gateway = ScriptedGateway(json)
916+
val controller = loadController(gateway, history(emptyList()))
917+
gateway.respondWith(
918+
"sessions.list",
919+
"""{"sessions":[{"key":"main","hasActiveRun":true,"status":"running"}]}""",
920+
)
921+
controller.handleGatewayEvent(
922+
"sessions.changed",
923+
"""{"reason":"start","sessionKey":"main","session":{"key":"main","hasActiveRun":true,"status":"running"}}""",
924+
)
925+
runCurrent()
926+
assertEquals(1, controller.sessions.value.size)
927+
928+
val historyStarted = CompletableDeferred<Unit>()
929+
val releaseHistory = CompletableDeferred<String>()
930+
gateway.respond("chat.history") {
931+
historyStarted.complete(Unit)
932+
releaseHistory.await()
933+
}
934+
controller.refresh()
935+
runCurrent()
936+
historyStarted.await()
937+
938+
gateway.respondChatSend(status = "started")
939+
assertTrue(controller.sendMessageAwaitAcceptance("successor work", "off", emptyList()))
940+
releaseHistory.complete(
941+
history(emptyList(), inFlightRun = "run-predecessor" to "stale work"),
942+
)
943+
runCurrent()
944+
assertEquals(1, controller.pendingRunCount.value)
945+
assertNull(controller.streamingAssistantText.value)
946+
947+
controller.handleGatewayEvent(
948+
"session.observer",
949+
"""{"sessionKey":"main","runId":"run-predecessor","revision":1,"updatedAt":200,"headline":"Stale predecessor","health":"on-track"}""",
950+
)
951+
952+
assertNull(
953+
controller.sessions.value
954+
.single()
955+
.observerDigest,
956+
)
957+
}
958+
912959
@Test
913960
fun reconnectRetiresPersistedLocalRunBeforeAdoptingOtherRun() =
914961
runTest {

0 commit comments

Comments
 (0)