diff --git a/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt b/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt index 376c1672..6ae66254 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/AppViewModel.kt @@ -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) diff --git a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt index 89da75de..8d66087d 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatStore.kt @@ -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(parts.size) + val textIndices = HashMap>() + 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>() + 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() 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 diff --git a/internal/opencode/history.go b/internal/opencode/history.go index b6b72fa4..5fae622d 100644 --- a/internal/opencode/history.go +++ b/internal/opencode/history.go @@ -84,26 +84,29 @@ func readHistoryPage(ctx context.Context, sessionID string, r io.Reader) ([]prot } else if descending { p.take(nil, '{') found := false + fields := make(map[string]struct{}) for err == nil { key := &historyBuffer{limit: 256} if err = p.str(key, nil); err != nil { break } var name string - if !key.overflow { - _ = json.Unmarshal(key.Bytes(), &name) + if key.overflow || json.Unmarshal(key.Bytes(), &name) != nil { + err = fmt.Errorf("invalid history envelope field") + break } + if _, duplicate := fields[name]; duplicate { + err = fmt.Errorf("duplicate history envelope field %q", name) + break + } + fields[name] = struct{}{} if err = p.take(nil, ':'); err != nil { break } switch name { case "data": - if found { - err = fmt.Errorf("duplicate history data") - } else { - found = true - err = readMessages() - } + found = true + err = readMessages() case "cursor": cursor := &historyBuffer{limit: 64 << 10} err = p.value(cursor, 1, false) diff --git a/internal/opencode/history_test.go b/internal/opencode/history_test.go index b817a46d..8300dcc4 100644 --- a/internal/opencode/history_test.go +++ b/internal/opencode/history_test.go @@ -138,7 +138,8 @@ func TestHistoryLegacyLargeToolAndNonJSON(t *testing.T) { func TestHistoryRejectsMalformedIncludingDiscardedDetail(t *testing.T) { for _, raw := range []string{ `null`, `{"data":null}`, `{"data":{}}`, `{"data":[],"cursor":{"next":42}}`, - `{"data":[]} {}`, `{"data":[}`, `{"data":[],}`, `{"data":[],"x":01}`, + `{"data":[],"data":[]}`, `{"data":[],"cursor":{"next":"one"},"cursor":{"next":"two"}}`, + `{"data":[]} {}`, `{"data":[}`, `{"data":[],}`, `{"data":[],"x":01}`, `{"data":[],"x":1,"x":2}`, `{"data":[],"state":{"text":"` + strings.Repeat("x", 1<<20) + `\q"}}`, `{"data":[],"state":{"text":"` + strings.Repeat("x", 1<<20) + `\u12G4"}}`, `{"data":[],"state":{"text":"` + strings.Repeat("x", 1<<20),