@@ -57,6 +57,7 @@ import java.util.concurrent.atomic.AtomicLong
5757
5858// Bounds one-shot search list fetches like the primary session list.
5959internal const val SESSION_LIST_FETCH_LIMIT = 200
60+ internal const val SESSION_UNREAD_ACK_CAPABILITY = " session-unread-ack-contract"
6061private val QUESTION_REFRESH_RETRY_DELAYS_MS = longArrayOf(1_000L , 2_000L , 4_000L )
6162private val SWARM_REFRESH_RETRY_DELAYS_MS = longArrayOf(1_000L , 2_000L , 4_000L )
6263private 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,
0 commit comments