Skip to content

Commit a6bae8f

Browse files
authored
fix(android): preserve current-branch input across reconnect (#132954)
Record canonical history branch evidence before publishing it or admitting new input. Keep branch-list dispatch readiness separate, preserve exact committed revisions across callers, and retain explicit retry for stale input. Fixes #132948. Real-Room regressions and isolated native reconnect/retry proof cover the repair. No SQLite schema change.
1 parent bee48f7 commit a6bae8f

7 files changed

Lines changed: 872 additions & 331 deletions

File tree

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

Lines changed: 48 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ data class ChatOutboxBranchState(
137137
val revision: Int,
138138
)
139139

140+
sealed interface ChatOutboxBranchEvidence {
141+
val previousState: ChatOutboxBranchState
142+
143+
data class History(
144+
override val previousState: ChatOutboxBranchState,
145+
) : ChatOutboxBranchEvidence
146+
147+
data class BranchListing(
148+
override val previousState: ChatOutboxBranchState,
149+
val leafEntryIds: Set<String>,
150+
) : ChatOutboxBranchEvidence
151+
}
152+
140153
data class ChatOutboxMutationLease(
141154
val revision: Int,
142155
val startedAtMs: Long,
@@ -342,23 +355,18 @@ interface ChatCommandOutbox {
342355
lease: ChatOutboxMutationLease? = null,
343356
): ChatOutboxBranchState? = null
344357

345-
suspend fun updateLastActiveLeafEntryId(
346-
gatewayId: String,
347-
scope: ChatOutboxScope,
348-
leafEntryId: String,
349-
expectedEpoch: Int,
350-
expectedRevision: Int,
351-
): Boolean = false
352-
358+
/**
359+
* Returns committed authority. History publication never adopts earlier input; branch listing
360+
* retains the admission boundary captured before the request or ambiguous local mutation.
361+
*/
353362
suspend fun reconcileBranchScope(
354363
gatewayId: String,
355364
scope: ChatOutboxScope,
356-
previousState: ChatOutboxBranchState,
365+
evidence: ChatOutboxBranchEvidence,
357366
activeLeafEntryId: String?,
358-
branchLeafEntryIds: Set<String>,
359367
activeTranscriptEntryIds: Set<String>,
360368
lastError: String,
361-
): Boolean = false
369+
): ChatOutboxBranchState? = null
362370

363371
suspend fun confirmBranchChange(
364372
gatewayId: String,
@@ -1149,59 +1157,38 @@ class RoomChatCommandOutbox internal constructor(
11491157
lease: ChatOutboxMutationLease?,
11501158
): ChatOutboxBranchState? = updateBranchMutationState(gatewayId, scope, needsReconciliation = true, lease = lease)
11511159

1152-
override suspend fun updateLastActiveLeafEntryId(
1153-
gatewayId: String,
1154-
scope: ChatOutboxScope,
1155-
leafEntryId: String,
1156-
expectedEpoch: Int,
1157-
expectedRevision: Int,
1158-
): Boolean {
1159-
val gateway = scopedGatewayId(gatewayId) ?: return false
1160-
val normalized = normalizedScope(scope) ?: return false
1161-
val leaf = leafEntryId.trim().takeIf { it.isNotEmpty() } ?: return false
1162-
return database.withTransaction {
1163-
ensureBranchStorageLocked()
1164-
ensureBranchScopeLocked(gateway, normalized)
1165-
val state = readBranchStateLocked(gateway, normalized) ?: return@withTransaction false
1166-
if (
1167-
state.epoch != expectedEpoch ||
1168-
state.revision != expectedRevision ||
1169-
state.switchPendingSinceMs != null ||
1170-
state.needsReconciliation ||
1171-
unresolvedCountLocked(gateway, normalized, includingFailed = false) > 0
1172-
) {
1173-
return@withTransaction false
1174-
}
1175-
writeBranchStateLocked(gateway, normalized, expectedEpoch, leaf, expectedRevision = expectedRevision)
1176-
}
1177-
}
1178-
11791160
override suspend fun reconcileBranchScope(
11801161
gatewayId: String,
11811162
scope: ChatOutboxScope,
1182-
previousState: ChatOutboxBranchState,
1163+
evidence: ChatOutboxBranchEvidence,
11831164
activeLeafEntryId: String?,
1184-
branchLeafEntryIds: Set<String>,
11851165
activeTranscriptEntryIds: Set<String>,
11861166
lastError: String,
1187-
): Boolean {
1188-
val gateway = scopedGatewayId(gatewayId) ?: return false
1189-
val normalized = normalizedScope(scope) ?: return false
1167+
): ChatOutboxBranchState? {
1168+
val gateway = scopedGatewayId(gatewayId) ?: return null
1169+
val normalized = normalizedScope(scope) ?: return null
11901170
val leaf = activeLeafEntryId?.trim()?.takeIf { it.isNotEmpty() }
1191-
if (activeLeafEntryId != null && leaf == null) return false
1171+
if (activeLeafEntryId != null && leaf == null) return null
1172+
val previousState = evidence.previousState
1173+
val listing = evidence as? ChatOutboxBranchEvidence.BranchListing
11921174
return database.withTransaction {
11931175
ensureBranchStorageLocked()
11941176
ensureBranchScopeLocked(gateway, normalized)
1195-
val expiredLease = expireBranchSwitchLeaseLocked(gateway, normalized, System.currentTimeMillis())
1196-
val state = readBranchStateLocked(gateway, normalized) ?: return@withTransaction false
1197-
if ((!expiredLease && state.revision != previousState.revision) || state.switchPendingSinceMs != null) {
1198-
return@withTransaction false
1177+
var state = readBranchStateLocked(gateway, normalized) ?: return@withTransaction null
1178+
// Expiry may retire this captured lease, but must never authorize an older response.
1179+
if (state.revision != previousState.revision || state.epoch != previousState.epoch) return@withTransaction null
1180+
if (listing != null && expireBranchSwitchLeaseLocked(gateway, normalized, System.currentTimeMillis())) {
1181+
state = readBranchStateLocked(gateway, normalized) ?: return@withTransaction null
11991182
}
1183+
if (state.switchPendingSinceMs != null) return@withTransaction null
12001184
val pending = unresolvedCountLocked(gateway, normalized, includingFailed = true)
12011185
val previousLeaf = previousState.lastActiveLeafEntryId
12021186
val canAdoptQueuedDuringReconciliation =
1203-
previousState.needsReconciliation && !previousState.hadDeliverableCommands
1204-
if (leaf != null && previousLeaf != null && previousLeaf != leaf && previousLeaf in branchLeafEntryIds) {
1187+
listing != null && previousState.needsReconciliation && !previousState.hadDeliverableCommands
1188+
val advancedOnActivePath = previousLeaf?.let(activeTranscriptEntryIds::contains) == true
1189+
val knownBranchSwitch =
1190+
leaf != null && previousLeaf != null && previousLeaf != leaf && listing?.leafEntryIds?.contains(previousLeaf) == true
1191+
if (knownBranchSwitch || (previousLeaf != leaf && !advancedOnActivePath && canAdoptQueuedDuringReconciliation)) {
12051192
installConfirmedBranchChangeLocked(
12061193
gateway,
12071194
normalized,
@@ -1211,30 +1198,22 @@ class RoomChatCommandOutbox internal constructor(
12111198
adoptQueuedCommands = canAdoptQueuedDuringReconciliation,
12121199
)
12131200
} else {
1214-
val advancedOnActivePath = previousLeaf?.let(activeTranscriptEntryIds::contains) == true
1215-
if (previousLeaf != leaf && !advancedOnActivePath && canAdoptQueuedDuringReconciliation) {
1216-
installConfirmedBranchChangeLocked(
1217-
gateway,
1218-
normalized,
1219-
state.epoch,
1220-
leaf,
1221-
lastError,
1222-
adoptQueuedCommands = true,
1223-
)
1224-
return@withTransaction true
1225-
}
1226-
if (
1227-
previousLeaf != leaf &&
1228-
!advancedOnActivePath &&
1229-
((pending > 0 && (previousState.hadPendingCommands || previousState.needsReconciliation)) || leaf == null)
1230-
) {
1201+
// Before history publication every existing row predates the visible branch, including
1202+
// input admitted while its RPC was in flight. Only explicit branch reconciliation adopts.
1203+
val earlierInput =
1204+
pending > 0 &&
1205+
(evidence is ChatOutboxBranchEvidence.History || previousState.hadPendingCommands || previousState.needsReconciliation)
1206+
if (previousLeaf != leaf && !advancedOnActivePath && (earlierInput || leaf == null)) {
12311207
parkPendingCommandsLocked(gateway, normalized, lastError)
12321208
}
12331209
if (!writeBranchStateLocked(gateway, normalized, state.epoch, leaf, expectedRevision = state.revision)) {
1234-
return@withTransaction false
1210+
return@withTransaction null
12351211
}
12361212
}
1237-
true
1213+
readBranchStateLocked(gateway, normalized)?.copy(
1214+
hadPendingCommands = unresolvedCountLocked(gateway, normalized, includingFailed = true) > 0,
1215+
hadDeliverableCommands = unresolvedCountLocked(gateway, normalized, includingFailed = false) > 0,
1216+
)
12381217
}
12391218
}
12401219

0 commit comments

Comments
 (0)