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
+11 -8
View File
@@ -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)
+2 -1
View File
@@ -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),