Skip to content

Commit 0836dd8

Browse files
committed
fix(ios): bound advancing session roster scans
1 parent 5dba666 commit 0836dd8

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

‎apps/ios/Sources/RootSidebarModel.swift‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import OpenClawKit
55
import OpenClawProtocol
66

77
struct ChatSessionRosterSnapshot: Sendable {
8+
private static let maximumPageCount = 50
9+
private static let maximumSessionCount = 10000
10+
811
let sessions: [OpenClawChatSessionEntry]
912
let isCached: Bool
1013
let totalCount: Int?
@@ -30,9 +33,16 @@ struct ChatSessionRosterSnapshot: Sendable {
3033
var rowIndices: [String: Int] = [:]
3134
var totalCount: Int?
3235
var offset = 0
36+
var pageCount = 0
3337

3438
while true {
3539
try Task.checkCancellation()
40+
// Match sessions.list's bounded scan so a changing Gateway snapshot
41+
// cannot keep a sidebar refresh alive forever.
42+
guard pageCount < Self.maximumPageCount, sessions.count < Self.maximumSessionCount else {
43+
return Self(sessions: sessions, isCached: false, totalCount: totalCount, isComplete: false)
44+
}
45+
pageCount += 1
3646
let response: OpenClawChatSessionsListResponse
3747
do {
3848
response = try await fetchPage(offset)
@@ -51,6 +61,9 @@ struct ChatSessionRosterSnapshot: Sendable {
5161
if let index = rowIndices[session.key] {
5262
sessions[index] = session
5363
} else {
64+
guard sessions.count < Self.maximumSessionCount else {
65+
return Self(sessions: sessions, isCached: false, totalCount: totalCount, isComplete: false)
66+
}
5467
rowIndices[session.key] = sessions.count
5568
sessions.append(session)
5669
}

‎apps/ios/Tests/RootTabsPresentationTests.swift‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,31 @@ struct RootTabsPresentationTests {
112112
#expect(snapshot.isComplete)
113113
}
114114

115+
@Test func `session roster bounds an endlessly advancing gateway snapshot without dropping rows`() async throws {
116+
var requestCount = 0
117+
118+
let snapshot = try await ChatSessionRosterSnapshot.collect { offset in
119+
requestCount += 1
120+
guard requestCount <= 51 else { throw URLError(.networkConnectionLost) }
121+
let sessions = (offset..<(offset + 200)).map { Self.sessionEntry(key: "session-\($0)") }
122+
return OpenClawChatSessionsListResponse(
123+
ts: nil,
124+
path: nil,
125+
count: sessions.count,
126+
totalCount: offset + 400,
127+
offset: offset,
128+
nextOffset: offset + sessions.count,
129+
hasMore: true,
130+
defaults: nil,
131+
sessions: sessions)
132+
}
133+
134+
#expect(requestCount == 50)
135+
#expect(snapshot.sessions.count == 10000)
136+
#expect(snapshot.sessions.last?.key == "session-9999")
137+
#expect(!snapshot.isComplete)
138+
}
139+
115140
@Test func `session roster preserves successful pages when a later request fails`() async throws {
116141
let firstPage = (0..<200).map { Self.sessionEntry(key: "session-\($0)") }
117142

0 commit comments

Comments
 (0)