Harden paged chat history loading

Close the per-thread history loading gate before launching requests, make live/history part reconciliation linear, and reject duplicate history envelope fields.

Complete task-owned history paging work remains preserved in the preceding recovery commit. Checked packaged templates and packaged authoring skill references: neither needs an update; docs/notes/large-chat-history.md records the stale shared documentation.
This commit is contained in:
2026-10-18 15:21:09 +01:00
parent ff1775e11f
commit 18e7d073a6
4 changed files with 40 additions and 14 deletions
@@ -1105,12 +1105,15 @@ class AppViewModel(
fun undoRemoveChat() = chatStore.undoRemove()
fun loadChatHistory(sessionId: String) {
if (chatStore.value.thread(sessionId)?.loading == true) return
// Set the gate before launching so repeated taps/events cannot queue two
// reads before the coroutine gets a chance to start.
chatStore.historyLoading(sessionId)
viewModelScope.launch {
// Watch the session's shared stream so frames from any client's turn
// arrive live. Best-effort: an older server answers an error frame,
// which must not stop the history from loading.
runCatching { repository.chatSubscribe(sessionId) }
chatStore.historyLoading(sessionId)
try {
val page = repository.chatMessages(sessionId, CHAT_HISTORY_LIMIT)
chatStore.history(sessionId, page.messages, page.nextCursor, page.hasMore)
@@ -1125,8 +1128,10 @@ class AppViewModel(
fun loadOlderChatHistory(sessionId: String) {
val thread = chatStore.value.thread(sessionId) ?: return
if (!thread.hasMore || thread.loading) return
// Capture both the request-start snapshot and cursor before launch; this
// makes the per-thread loading gate effective for repeated requests.
chatStore.historyLoading(sessionId, older = true)
viewModelScope.launch {
chatStore.historyLoading(sessionId, older = true)
try {
val page = repository.chatMessages(sessionId, CHAT_HISTORY_LIMIT, thread.nextCursor)
chatStore.history(sessionId, page.messages, page.nextCursor, page.hasMore)
@@ -789,19 +789,36 @@ class ChatStore(
/** Keep the full stored answer when a live-only suffix arrived during its first read. */
private fun mergeHistoryDuringRead(history: ChatMessage, live: ChatMessage, before: ChatMessage?): ChatMessage {
val parts = history.parts.toMutableList()
val partIndices = HashMap<String, Int>(parts.size)
val textIndices = HashMap<String, MutableList<Int>>()
for ((index, part) in parts.withIndex()) {
if (part.id.isNotBlank()) partIndices[part.id] = index
if (part.type == ChatProtocol.PART_TEXT || part.type == ChatProtocol.PART_REASONING) {
textIndices.getOrPut(part.type) { mutableListOf() }.add(index)
}
}
val beforeText = HashMap<String, MutableList<ChatPart>>()
before?.parts?.forEach { part ->
if (part.type == ChatProtocol.PART_TEXT || part.type == ChatProtocol.PART_REASONING) {
beforeText.getOrPut(part.type) { mutableListOf() }.add(part)
}
}
val ordinals = mutableMapOf<String, Int>()
for (part in live.parts) {
val ordinal = ordinals.getOrDefault(part.type, 0)
ordinals[part.type] = ordinal + 1
val textPart = part.type == ChatProtocol.PART_TEXT || part.type == ChatProtocol.PART_REASONING
val byId = parts.indexOfFirst { part.id.isNotBlank() && it.id == part.id }
val byId = partIndices[part.id]
// V2 stored text has no id; deltas have a stable generated ordinal id.
val index = if (byId >= 0) byId else if (textPart) parts.indices.filter { parts[it].type == part.type }.getOrNull(ordinal) ?: -1 else -1
val index = byId ?: if (textPart) textIndices[part.type]?.getOrNull(ordinal) ?: -1 else -1
if (index < 0) {
parts += part
val appendedIndex = parts.lastIndex
if (part.id.isNotBlank()) partIndices[part.id] = appendedIndex
if (textPart) textIndices.getOrPut(part.type) { mutableListOf() }.add(appendedIndex)
} else if (textPart) {
val stored = parts[index].text
val previous = before?.parts?.filter { it.type == part.type }?.getOrNull(ordinal)?.text
val previous = beforeText[part.type]?.getOrNull(ordinal)?.text
val delta = if (previous != null && part.text.startsWith(previous)) part.text.removePrefix(previous) else part.text
val text = when {
part.text.isEmpty() || stored.endsWith(part.text) -> stored