Skip to content

Commit 6d1cf27

Browse files
authored
Merge branch 'main' into fix/midturn-overflow-async-tools
2 parents fe6cc41 + 776986f commit 6d1cf27

330 files changed

Lines changed: 12839 additions & 4433 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import ai.openclaw.app.chat.MessageSpeechClient
2929
import ai.openclaw.app.chat.MessageSpeechController
3030
import ai.openclaw.app.chat.MessageSpeechState
3131
import ai.openclaw.app.chat.OutgoingAttachment
32+
import ai.openclaw.app.chat.SESSION_UNREAD_ACK_CAPABILITY
3233
import ai.openclaw.app.chat.SessionBranch
3334
import ai.openclaw.app.chat.SessionForkResult
3435
import ai.openclaw.app.chat.SessionRewindResult
@@ -1330,6 +1331,7 @@ class NodeRuntime private constructor(
13301331
private val gatewayMethodsLock = Any()
13311332
private var gatewayApprovalRpcFamily = GatewayApprovalRpcFamily.Unavailable
13321333
private var gatewayAdvertisedMethods: Set<String>? = null
1334+
private var gatewayAdvertisedCapabilities: Set<String>? = null
13331335
private var gatewayMethodsEpoch = 0L
13341336

13351337
@Volatile internal var gatewayDataRequestOverrideForTests: GatewayDataRequestOverride? = null
@@ -1408,6 +1410,7 @@ class NodeRuntime private constructor(
14081410
_gatewayVersion.value = hello.serverVersion
14091411
_gatewayUpdateAvailable.value = hello.updateAvailable
14101412
replaceGatewayMethods(hello.methods)
1413+
replaceGatewayCapabilities(hello.capabilities)
14111414
val operatorScopes = normalizeOperatorScopes(hello.authScopes)
14121415
_operatorScopes.value = operatorScopes
14131416
// Pairing capabilities require positive hello advertisement; an unknown catalog grants none.
@@ -1681,6 +1684,7 @@ class NodeRuntime private constructor(
16811684
_gatewayVersion.value = null
16821685
_gatewayUpdateAvailable.value = null
16831686
replaceGatewayMethods(null)
1687+
replaceGatewayCapabilities(null)
16841688
_operatorScopes.value = emptyList()
16851689
_devicePairingCapabilities.value = GatewayDevicePairingCapabilities()
16861690
_gatewayAccentArgb.value = null
@@ -1929,6 +1933,7 @@ class NodeRuntime private constructor(
19291933
currentDefaultAgentId = { gatewayDefaultAgentId.value },
19301934
currentDefaultAgentRevision = gatewayDefaultAgentRevision::get,
19311935
gatewayAdvertisesMethod = ::gatewayAdvertisesMethod,
1936+
gatewayAdvertisesCapability = ::gatewayAdvertisesCapability,
19321937
commandOutbox = chatCommandOutbox,
19331938
recordModelRecent = prefs::recordModelRecent,
19341939
onSessionDeleted = ::publishChatSessionDeletion,
@@ -1945,6 +1950,7 @@ class NodeRuntime private constructor(
19451950
json = json,
19461951
requestGateway = AndroidScreenshotFixture::request,
19471952
gatewayAdvertisesMethod = { _ -> true },
1953+
gatewayAdvertisesCapability = { _ -> true },
19481954
)
19491955
}.also {
19501956
it.applyMainSessionKey(_mainSessionKey.value)
@@ -2906,6 +2912,7 @@ class NodeRuntime private constructor(
29062912
_remoteAddress.value = "Mac Studio on local network"
29072913
_gatewayVersion.value = BuildConfig.VERSION_NAME
29082914
replaceGatewayMethods(setOf(GatewayMethod.DesktopObserve.rawValue))
2915+
replaceGatewayCapabilities(setOf(SESSION_UNREAD_ACK_CAPABILITY))
29092916
_gatewayControlPage.value =
29102917
GatewayControlPage(
29112918
baseUrl = AndroidScreenshotFixture.controlUiBaseUrl,
@@ -7484,6 +7491,14 @@ class NodeRuntime private constructor(
74847491

74857492
private fun gatewayAdvertisesMethod(method: String): Boolean? = synchronized(gatewayMethodsLock) { gatewayAdvertisedMethods?.let { method in it } }
74867493

7494+
private fun replaceGatewayCapabilities(capabilities: Set<String>?) {
7495+
synchronized(gatewayMethodsLock) {
7496+
gatewayAdvertisedCapabilities = capabilities
7497+
}
7498+
}
7499+
7500+
private fun gatewayAdvertisesCapability(capability: String): Boolean? = synchronized(gatewayMethodsLock) { gatewayAdvertisedCapabilities?.let { capability in it } }
7501+
74877502
private fun captureGatewayMethods(): GatewayMethodsSnapshot =
74887503
synchronized(gatewayMethodsLock) {
74897504
GatewayMethodsSnapshot(

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

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import java.util.concurrent.atomic.AtomicLong
5757

5858
// Bounds one-shot search list fetches like the primary session list.
5959
internal const val SESSION_LIST_FETCH_LIMIT = 200
60+
internal const val SESSION_UNREAD_ACK_CAPABILITY = "session-unread-ack-contract"
6061
private val QUESTION_REFRESH_RETRY_DELAYS_MS = longArrayOf(1_000L, 2_000L, 4_000L)
6162
private val SWARM_REFRESH_RETRY_DELAYS_MS = longArrayOf(1_000L, 2_000L, 4_000L)
6263
private const val WEAR_AGENT_PULSE_SWARM_MAX_ROWS = 1_000
@@ -113,6 +114,7 @@ class ChatController internal constructor(
113114
private val requestGatewayForGateway: suspend (gatewayId: String, method: String, paramsJson: String?) -> String =
114115
{ _, method, paramsJson -> requestGateway(method, paramsJson) },
115116
private val gatewayAdvertisesMethod: (method: String) -> Boolean? = { null },
117+
private val gatewayAdvertisesCapability: (capability: String) -> Boolean? = { null },
116118
private val captureSettingsRequestLease: (gatewayScope: ChatCacheScope?) -> GatewaySession.RequestLease? =
117119
{ gatewayScope ->
118120
GatewaySession.RequestLease(endpointStableId = gatewayScope?.gatewayId.orEmpty()) { method, paramsJson, _ ->
@@ -156,6 +158,7 @@ class ChatController internal constructor(
156158
currentDefaultAgentId: () -> String? = { "main" },
157159
currentDefaultAgentRevision: () -> Long = { 0L },
158160
gatewayAdvertisesMethod: (method: String) -> Boolean? = { null },
161+
gatewayAdvertisesCapability: (capability: String) -> Boolean? = { null },
159162
commandOutbox: ChatCommandOutbox? = null,
160163
recordModelRecent: (String) -> Unit = {},
161164
onSessionDeleted: (ChatSessionDeletion) -> Unit = {},
@@ -172,6 +175,7 @@ class ChatController internal constructor(
172175
session.requestForEndpoint(gatewayId, method, paramsJson)
173176
},
174177
gatewayAdvertisesMethod = gatewayAdvertisesMethod,
178+
gatewayAdvertisesCapability = gatewayAdvertisesCapability,
175179
captureSettingsRequestLease = { gatewayScope ->
176180
session.captureRequestLease(gatewayScope?.gatewayId)
177181
},
@@ -619,6 +623,8 @@ class ChatController internal constructor(
619623
// server-confirmed read (unread=false) arrives, so fresh activity on the open
620624
// session re-acknowledges without patch loops (lastReadAt is stamped server-side).
621625
private var unreadPatchSessionKey: String? = null
626+
private var unreadActivationObserved = false
627+
private var unreadActivationMarkedUnreadAt: Long? = null
622628
private var unreadPatchRequested = false
623629

624630
// Armed on disconnect so the next health event refetches history and re-adopts
@@ -833,6 +839,8 @@ class ChatController internal constructor(
833839
applyThinkingMetadata(null)
834840
sessionsListArchived = false
835841
unreadPatchSessionKey = null
842+
unreadActivationObserved = false
843+
unreadActivationMarkedUnreadAt = null
836844
unreadPatchRequested = false
837845
_commands.value = emptyList()
838846
_modelCatalog.value = emptyList()
@@ -1050,6 +1058,7 @@ class ChatController internal constructor(
10501058
pinned: Boolean? = null,
10511059
archived: Boolean? = null,
10521060
unread: Boolean? = null,
1061+
unreadExpectation: ChatSessionUnreadExpectation? = null,
10531062
): Boolean {
10541063
val sessionKey = key.trim().takeIf { it.isNotEmpty() } ?: return false
10551064
val capturedOwnerAgentId =
@@ -1082,6 +1091,10 @@ class ChatController internal constructor(
10821091
if (pinned != null) put("pinned", JsonPrimitive(pinned))
10831092
if (archived != null) put("archived", JsonPrimitive(archived))
10841093
if (unread != null) put("unread", JsonPrimitive(unread))
1094+
if (unreadExpectation != null) {
1095+
val marker = unreadExpectation.markedUnreadAt
1096+
put("expectedMarkedUnreadAt", marker?.let(::JsonPrimitive) ?: JsonNull)
1097+
}
10851098
}
10861099
if (archived == true) {
10871100
requestGatewayWithTimeout("sessions.patch", params.toString(), 10 * 60_000L)
@@ -2348,6 +2361,8 @@ class ChatController internal constructor(
23482361
private fun prepareSessionSelection(key: String) {
23492362
if (key != unreadPatchSessionKey) {
23502363
unreadPatchSessionKey = key
2364+
unreadActivationObserved = false
2365+
unreadActivationMarkedUnreadAt = null
23512366
unreadPatchRequested = false
23522367
}
23532368
acknowledgeUnreadIfNeeded(key, _sessions.value.firstOrNull { it.key == key })
@@ -6655,6 +6670,8 @@ class ChatController internal constructor(
66556670
archived = obj["archived"].asBooleanOrNull(),
66566671
unread = obj["unread"].asBooleanOrNull(),
66576672
lastReadAt = obj["lastReadAt"].asLongOrNull(),
6673+
markedUnreadAt = obj["markedUnreadAt"].asLongOrNull(),
6674+
hasMarkedUnreadMetadata = "markedUnreadAt" in obj,
66586675
agentStatus = parseSessionAgentStatus(obj["agentStatus"]),
66596676
hasAgentStatusMetadata = "agentStatus" in obj,
66606677
observerDigest =
@@ -6958,19 +6975,44 @@ class ChatController internal constructor(
69586975
requireActive: Boolean = false,
69596976
) {
69606977
if (key.isEmpty() || key != unreadPatchSessionKey) return
6961-
if (entry?.unread == false) {
6978+
if (entry == null) return
6979+
if (!unreadActivationObserved) {
6980+
unreadActivationObserved = true
6981+
unreadActivationMarkedUnreadAt = entry.markedUnreadAt
6982+
}
6983+
if (entry.unread == false) {
6984+
unreadActivationMarkedUnreadAt = null
69626985
unreadPatchRequested = false
69636986
return
69646987
}
6965-
if (entry?.unread != true || unreadPatchRequested) return
6988+
val markedUnreadAt = entry.markedUnreadAt
6989+
if (markedUnreadAt != null && markedUnreadAt != unreadActivationMarkedUnreadAt) {
6990+
return
6991+
}
6992+
if (entry.unread != true || unreadPatchRequested) return
69666993
// switchSession acknowledges before _sessionKey updates; background upserts only
69676994
// re-acknowledge the session that is currently open.
69686995
if (requireActive && key != _sessionKey.value) return
69696996
unreadPatchRequested = true
6970-
_sessions.value = _sessions.value.map { if (it.key == key) it.copy(unread = false) else it }
6997+
// Native app and Gateway releases can skew. Only current Gateways accept
6998+
// the closed-schema conditional acknowledgement field.
6999+
val unreadExpectation =
7000+
if (gatewayAdvertisesCapability(SESSION_UNREAD_ACK_CAPABILITY) == true) {
7001+
ChatSessionUnreadExpectation(entry.markedUnreadAt)
7002+
} else {
7003+
null
7004+
}
69717005
scope.launch {
69727006
// A failed read patch must unlatch the episode so later snapshots retry.
6973-
if (!patchSession(key = key, ownerAgentId = entry.ownerAgentId, unread = false) && unreadPatchSessionKey == key) {
7007+
if (
7008+
!patchSession(
7009+
key = key,
7010+
ownerAgentId = entry.ownerAgentId,
7011+
unread = false,
7012+
unreadExpectation = unreadExpectation,
7013+
) &&
7014+
unreadPatchSessionKey == key
7015+
) {
69747016
unreadPatchRequested = false
69757017
}
69767018
}
@@ -7594,6 +7636,10 @@ internal fun mergeChatSessionEntry(
75947636
archived = next.archived ?: existing.archived,
75957637
unread = next.unread ?: existing.unread,
75967638
lastReadAt = next.lastReadAt ?: existing.lastReadAt,
7639+
markedUnreadAt =
7640+
if (next.hasMarkedUnreadMetadata) next.markedUnreadAt else existing.markedUnreadAt,
7641+
hasMarkedUnreadMetadata =
7642+
existing.hasMarkedUnreadMetadata || next.hasMarkedUnreadMetadata,
75977643
agentStatus = if (next.hasAgentStatusMetadata) next.agentStatus else existing.agentStatus,
75987644
hasAgentStatusMetadata = existing.hasAgentStatusMetadata || next.hasAgentStatusMetadata,
75997645
observerDigest = observerDigest,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ data class ChatSessionEntry(
316316
val archived: Boolean? = null,
317317
val unread: Boolean? = null,
318318
val lastReadAt: Long? = null,
319+
val markedUnreadAt: Long? = null,
320+
val hasMarkedUnreadMetadata: Boolean = markedUnreadAt != null,
319321
val agentStatus: ChatSessionAgentStatus? = null,
320322
val hasAgentStatusMetadata: Boolean = agentStatus != null,
321323
val observerDigest: SessionObserverDigest? = null,
@@ -352,6 +354,10 @@ data class ChatSessionEntry(
352354
status != null || startedAt != null || endedAt != null || runtimeMs != null || outputTokens != null,
353355
)
354356

357+
data class ChatSessionUnreadExpectation(
358+
val markedUnreadAt: Long?,
359+
)
360+
355361
data class ChatSessionAgentStatus(
356362
val note: String,
357363
val expiresAt: Long,

‎apps/android/app/src/main/java/ai/openclaw/app/gateway/GatewaySession.kt‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ data class GatewayHelloSummary(
215215
val authRole: String? = null,
216216
val authScopes: List<String> = emptyList(),
217217
val methods: Set<String>? = null,
218+
val capabilities: Set<String>? = null,
218219
)
219220

220221
data class GatewayUpdateAvailableSummary(
@@ -1442,6 +1443,13 @@ class GatewaySession(
14421443
.asArrayOrNull()
14431444
?.mapNotNull { it.asStringOrNull()?.trim()?.takeIf { method -> method.isNotEmpty() } }
14441445
?.toSet()
1446+
val capabilities =
1447+
obj["features"]
1448+
.asObjectOrNull()
1449+
?.get("capabilities")
1450+
.asArrayOrNull()
1451+
?.mapNotNull { it.asStringOrNull()?.trim()?.takeIf { capability -> capability.isNotEmpty() } }
1452+
?.toSet()
14451453
val authObj = obj["auth"].asObjectOrNull()
14461454
val deviceToken = authObj?.get("deviceToken").asStringOrNull()
14471455
val authRole = authObj?.get("role").asStringOrNull() ?: options.role
@@ -1506,6 +1514,7 @@ class GatewaySession(
15061514
authRole = authRole,
15071515
authScopes = authScopes,
15081516
methods = methods,
1517+
capabilities = capabilities,
15091518
),
15101519
)
15111520
}

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,23 @@ class ChatControllerCommandControlsTest {
553553
assertEquals(2, requests.count { it.first == "sessions.patch" })
554554
}
555555

556+
@Test
557+
fun explicitMarkReadUsesLegacyCompatiblePayloadOnCurrentGateway() =
558+
runTest {
559+
val (controller, requests) =
560+
chatControllerTestSetup {
561+
gatewayAdvertisesCapability = { it == SESSION_UNREAD_ACK_CAPABILITY }
562+
}
563+
564+
assertTrue(controller.patchSession(key = "main", unread = false))
565+
advanceUntilIdle()
566+
567+
val patch = requests.single { it.first == "sessions.patch" }.second.orEmpty()
568+
assertTrue(patch.contains("\"unread\":false"))
569+
assertFalse(patch.contains("readIntent"))
570+
assertFalse(patch.contains("expectedMarkedUnreadAt"))
571+
}
572+
556573
@Test
557574
fun archivingOrDeletingTheOpenSessionFallsBackToMain() =
558575
runTest {
@@ -602,6 +619,13 @@ class ChatControllerCommandControlsTest {
602619
)
603620
advanceUntilIdle()
604621
assertEquals(1, requests.count { it.first == "sessions.patch" })
622+
assertFalse(
623+
requests
624+
.single { it.first == "sessions.patch" }
625+
.second
626+
.orEmpty()
627+
.contains("expectedMarkedUnreadAt"),
628+
)
605629

606630
// Server-confirmed read resets the episode; a stale duplicate must not re-patch.
607631
controller.handleGatewayEvent(
@@ -617,6 +641,55 @@ class ChatControllerCommandControlsTest {
617641
assertEquals(2, requests.count { it.first == "sessions.patch" })
618642
}
619643

644+
@Test
645+
fun manualUnreadOnOpenSessionSurvivesRunUpdatesUntilReactivation() =
646+
runTest {
647+
val (controller, requests) =
648+
chatControllerTestSetup {
649+
gatewayAdvertisesCapability = { it == SESSION_UNREAD_ACK_CAPABILITY }
650+
respond(
651+
"sessions.list",
652+
"""{"sessions":[{"key":"main","unread":false},{"key":"other","unread":false}]}""",
653+
)
654+
}
655+
656+
controller.refreshSessions()
657+
advanceUntilIdle()
658+
controller.switchSession("main")
659+
advanceUntilIdle()
660+
661+
controller.handleGatewayEvent(
662+
"sessions.changed",
663+
"""{"sessionKey":"main","session":{"key":"main","agentId":"main","unread":true,"markedUnreadAt":100}}""",
664+
)
665+
advanceUntilIdle()
666+
assertEquals(0, requests.count { it.first == "sessions.patch" })
667+
val retained = controller.sessions.value.first { it.key == "main" }
668+
assertEquals(true, retained.unread)
669+
assertEquals(100L, retained.markedUnreadAt)
670+
671+
controller.handleGatewayEvent(
672+
"sessions.changed",
673+
"""{"sessionKey":"main","session":{"key":"main","agentId":"main","unread":true,"markedUnreadAt":100,"hasActiveRun":true,"status":"running"}}""",
674+
)
675+
controller.handleGatewayEvent(
676+
"sessions.changed",
677+
"""{"sessionKey":"main","session":{"key":"main","agentId":"main","unread":true,"markedUnreadAt":100,"hasActiveRun":false,"status":"done"}}""",
678+
)
679+
advanceUntilIdle()
680+
assertEquals(0, requests.count { it.first == "sessions.patch" })
681+
682+
controller.switchSession("other")
683+
advanceUntilIdle()
684+
controller.switchSession("main")
685+
advanceUntilIdle()
686+
687+
val patch = requests.single { it.first == "sessions.patch" }.second.orEmpty()
688+
assertTrue(patch.contains("\"unread\":false"))
689+
assertTrue(patch.contains("\"expectedMarkedUnreadAt\":100"))
690+
assertFalse(patch.contains("readIntent"))
691+
}
692+
620693
@Test
621694
fun startNewChatWithoutLoadedParentCreatesFirstSession() =
622695
runTest {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ class ChatControllerSessionPolicyTest {
181181
archived = false,
182182
unread = true,
183183
lastReadAt = 10L,
184+
markedUnreadAt = 15L,
184185
lastActivityAt = 20L,
185186
)
186187
val next = ChatSessionEntry(key = "agent:main:phone", updatedAtMs = 2L)
@@ -193,6 +194,7 @@ class ChatControllerSessionPolicyTest {
193194
assertEquals(false, merged.archived)
194195
assertEquals(true, merged.unread)
195196
assertEquals(10L, merged.lastReadAt)
197+
assertEquals(15L, merged.markedUnreadAt)
196198
assertEquals(20L, merged.lastActivityAt)
197199
}
198200

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,7 @@ class ChatControllerTranscriptCacheTest {
837837
"archived": false,
838838
"unread": true,
839839
"lastReadAt": 10,
840+
"markedUnreadAt": 15,
840841
"lastActivityAt": 20
841842
}]
842843
}
@@ -854,6 +855,7 @@ class ChatControllerTranscriptCacheTest {
854855
assertEquals(false, session.archived)
855856
assertEquals(true, session.unread)
856857
assertEquals(10L, session.lastReadAt)
858+
assertEquals(15L, session.markedUnreadAt)
857859
assertEquals(20L, session.lastActivityAt)
858860
}
859861

0 commit comments

Comments
 (0)