From 998da0e7af6ea272210c6d979d97931c54f203f8 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sun, 18 Oct 2026 15:03:18 +0100 Subject: [PATCH 1/2] WIP preserve large chat history recovery --- .../pipelines/data/PipelinesRepository.kt | 13 +- .../net/awan/pipelines/data/protocol/Chat.kt | 10 + .../awan/pipelines/data/protocol/Message.kt | 1 + .../net/awan/pipelines/ui/AppViewModel.kt | 19 +- .../pipelines/ui/chat/ChatConversationPane.kt | 8 + .../net/awan/pipelines/ui/chat/ChatStore.kt | 95 +++++- .../pipelines/ui/chat/MultiChatStateTest.kt | 65 ++++ docs/notes/large-chat-history.md | 43 +++ internal/opencode/chat.go | 80 ++++- internal/opencode/chat_test.go | 47 ++- internal/opencode/connection.go | 45 +++ internal/opencode/history.go | 323 ++++++++++++++++++ internal/opencode/history_test.go | 203 +++++++++++ internal/opencode/messages.go | 13 + internal/protocol/chat.go | 15 + internal/protocol/message.go | 1 + internal/server/chat.go | 2 +- internal/server/chat_commands.go | 14 +- internal/server/chat_history_test.go | 107 ++++++ internal/server/chat_multiclient_test.go | 26 +- internal/server/chat_test.go | 33 +- 21 files changed, 1088 insertions(+), 75 deletions(-) create mode 100644 docs/notes/large-chat-history.md create mode 100644 internal/opencode/history.go create mode 100644 internal/opencode/history_test.go create mode 100644 internal/server/chat_history_test.go diff --git a/android/app/src/main/java/net/awan/pipelines/data/PipelinesRepository.kt b/android/app/src/main/java/net/awan/pipelines/data/PipelinesRepository.kt index 9ad104dd..b337f6fb 100644 --- a/android/app/src/main/java/net/awan/pipelines/data/PipelinesRepository.kt +++ b/android/app/src/main/java/net/awan/pipelines/data/PipelinesRepository.kt @@ -7,6 +7,7 @@ import kotlinx.coroutines.flow.SharedFlow import kotlinx.serialization.json.JsonPrimitive import kotlinx.serialization.json.JsonElement import net.awan.pipelines.data.protocol.ChatCreateRequest +import net.awan.pipelines.data.protocol.ChatHistoryPage import net.awan.pipelines.data.protocol.ChatMessage import net.awan.pipelines.data.protocol.ChatPromptRequest import net.awan.pipelines.data.protocol.ChatProtocol @@ -297,12 +298,14 @@ class PipelinesRepository( ?: throw ProtocolException("The server did not return the new chat session.") } - /** Loads a session's stored turns; the server prefers its retained live state. */ - suspend fun chatMessages(sessionId: String, limit: Int = 0): List { + /** Loads a bounded chronological page; [cursor] is an opaque older-history continuation. */ + suspend fun chatMessages(sessionId: String, limit: Int = 0, cursor: String = ""): ChatHistoryPage { if (sessionId.isBlank()) throw ProtocolException("Select a chat session first.") - return client.call( - Message(type = Protocol.CHAT_MESSAGES, chatQuery = ChatQuery(sessionId = sessionId, limit = limit)), - ).chatMessages ?: emptyList() + val response = client.call( + Message(type = Protocol.CHAT_MESSAGES, chatQuery = ChatQuery(sessionId = sessionId, limit = limit, cursor = cursor)), + ) + // Old servers only have chat_messages and therefore honestly expose no continuation. + return response.chatHistory ?: ChatHistoryPage(messages = response.chatMessages ?: emptyList()) } /** diff --git a/android/app/src/main/java/net/awan/pipelines/data/protocol/Chat.kt b/android/app/src/main/java/net/awan/pipelines/data/protocol/Chat.kt index b912191f..d5ca7e75 100644 --- a/android/app/src/main/java/net/awan/pipelines/data/protocol/Chat.kt +++ b/android/app/src/main/java/net/awan/pipelines/data/protocol/Chat.kt @@ -66,6 +66,7 @@ data class ChatCreateRequest( data class ChatQuery( @SerialName("session_id") val sessionId: String = "", val limit: Int = 0, + val cursor: String = "", ) @Serializable @@ -126,6 +127,15 @@ data class ChatToolState( val content: List = emptyList(), val metadata: JsonObject? = null, val error: ChatError? = null, + // A bounded history preview is not the complete tool result. + val truncated: Boolean = false, +) + +@Serializable +data class ChatHistoryPage( + val messages: List = emptyList(), + @SerialName("next_cursor") val nextCursor: String = "", + @SerialName("has_more") val hasMore: Boolean = false, ) @Serializable diff --git a/android/app/src/main/java/net/awan/pipelines/data/protocol/Message.kt b/android/app/src/main/java/net/awan/pipelines/data/protocol/Message.kt index b0a5c168..b9845ea7 100644 --- a/android/app/src/main/java/net/awan/pipelines/data/protocol/Message.kt +++ b/android/app/src/main/java/net/awan/pipelines/data/protocol/Message.kt @@ -704,6 +704,7 @@ data class Message( @SerialName("chat_queued") val chatQueued: Boolean = false, @SerialName("chat_subscribe") val chatSubscribe: ChatQuery? = null, @SerialName("chat_messages") val chatMessages: List? = null, + @SerialName("chat_history") val chatHistory: ChatHistoryPage? = null, @SerialName("chat_event") val chatEvent: ChatEventFrame? = null, @SerialName("chat_permission") val chatPermission: ChatPermissionFrame? = null, @SerialName("chat_reply") val chatReply: ChatReplyRequest? = null, 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 a1aa3722..376c1672 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 @@ -1112,7 +1112,24 @@ class AppViewModel( runCatching { repository.chatSubscribe(sessionId) } chatStore.historyLoading(sessionId) try { - chatStore.history(sessionId, repository.chatMessages(sessionId, CHAT_HISTORY_LIMIT)) + val page = repository.chatMessages(sessionId, CHAT_HISTORY_LIMIT) + chatStore.history(sessionId, page.messages, page.nextCursor, page.hasMore) + } catch (t: Throwable) { + if (t is CancellationException) throw t + chatStore.failed(sessionId, ErrorMessages.describe(t)) + } + } + } + + /** Fetches the next opaque older-history page without replacing live state. */ + fun loadOlderChatHistory(sessionId: String) { + val thread = chatStore.value.thread(sessionId) ?: return + if (!thread.hasMore || thread.loading) return + 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) } catch (t: Throwable) { if (t is CancellationException) throw t chatStore.failed(sessionId, ErrorMessages.describe(t)) diff --git a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatConversationPane.kt b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatConversationPane.kt index beafb699..1ee6a729 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatConversationPane.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/chat/ChatConversationPane.kt @@ -12,6 +12,7 @@ import androidx.compose.foundation.lazy.LazyListState import androidx.compose.material.icons.Icons import androidx.compose.material.icons.automirrored.filled.ArrowBack import androidx.compose.material.icons.filled.PanTool +import androidx.compose.material.icons.filled.KeyboardArrowUp import androidx.compose.material.icons.filled.Stop import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.Icon @@ -173,6 +174,13 @@ internal fun ChatConversationPane( } }, actions = { + if (thread.hasMore) { + IconButton( + onClick = { vm.loadOlderChatHistory(thread.sessionId) }, + ) { + Icon(Icons.Default.KeyboardArrowUp, contentDescription = "Load older messages") + } + } if (awaiting) { IconButton( onClick = { 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 8a53ea64..89da75de 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 @@ -79,6 +79,12 @@ data class ChatThread( */ val closed: Boolean = false, val messages: List = emptyList(), + /** Opaque continuation for older messages returned by the server. */ + val nextCursor: String = "", + val hasMore: Boolean = false, + /** Immutable snapshot at request start distinguishes stale cache from new live frames. */ + val historyBeforeLoad: List? = null, + val historyLoadOlder: Boolean = false, val loading: Boolean = false, val loaded: Boolean = false, val error: String? = null, @@ -755,27 +761,104 @@ class ChatStore( fun scroll(sessionId: String, index: Int, offset: Int) = update(sessionId) { it.copy(scroll = ChatScrollPosition(index, offset)) } - fun historyLoading(sessionId: String) = update(sessionId) { it.copy(loading = true, error = null) } + fun historyLoading(sessionId: String, older: Boolean = false) = update(sessionId) { + it.copy(loading = true, error = null, historyBeforeLoad = it.messages, historyLoadOlder = older) + } fun failed(sessionId: String, message: String) = - update(sessionId) { it.copy(loading = false, error = message) } + update(sessionId) { it.copy(loading = false, error = message, historyBeforeLoad = null) } + + /** Linear-time suffix/prefix overlap: a history snapshot may include part of a live delta. */ + private fun appendHistoryDelta(stored: String, delta: String): String { + if (delta.isEmpty()) return stored + val prefix = IntArray(delta.length) + for (index in 1 until delta.length) { + var matched = prefix[index - 1] + while (matched > 0 && delta[index] != delta[matched]) matched = prefix[matched - 1] + if (delta[index] == delta[matched]) matched++ + prefix[index] = matched + } + var overlap = 0 + for (char in stored) { + while (overlap > 0 && (overlap == delta.length || char != delta[overlap])) overlap = prefix[overlap - 1] + if (overlap < delta.length && char == delta[overlap]) overlap++ + } + return stored + delta.substring(overlap) + } + + /** 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 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 } + // 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 + if (index < 0) { + parts += part + } else if (textPart) { + val stored = parts[index].text + val previous = before?.parts?.filter { it.type == 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 + part.text.startsWith(stored) -> part.text + previous != null && !stored.startsWith(previous) -> part.text + delta.isEmpty() || stored.endsWith(delta) -> stored + else -> appendHistoryDelta(stored, delta) + } + parts[index] = part.copy(text = text) + } else { + parts[index] = part + } + } + return history.copy(parts = parts, client = live.client.ifBlank { history.client }) + } /** - * Replaces a thread's conversation with the server's authoritative history. + * Merges an authoritative page with older cached turns and in-flight events. * The running flag is left as-is: history alone cannot say whether a turn is * still in flight. */ - fun history(sessionId: String, messages: List) { + fun history(sessionId: String, messages: List, nextCursor: String = "", hasMore: Boolean = false) { update(sessionId) { + val existing = it.messages.associateBy { message -> message.id } + val baseline = it.historyBeforeLoad?.associateBy { message -> message.id } + val pageIds = messages.mapTo(mutableSetOf()) { message -> message.id } + val firstOverlap = it.messages.indexOfFirst { message -> message.id in pageIds } + val olderPrefix = when { + firstOverlap >= 0 -> it.messages.take(firstOverlap) + it.historyBeforeLoad != null && !it.historyLoadOlder -> it.messages + else -> emptyList() + } + val merged = (olderPrefix + messages + it.messages).fold(LinkedHashMap()) { result, message -> + // A live event may have extended this message while the read was + // in flight. Preserve only that fold, not stale pre-request cache + // which would otherwise hide text missed during a disconnect. + val local = existing[message.id] + val changedDuringRead = local != null && (baseline == null || baseline[message.id] != local) + if (message.id !in result) result[message.id] = when { + changedDuringRead && baseline != null -> mergeHistoryDuringRead(message, local!!, baseline[message.id]) + changedDuringRead -> local!! + else -> message + } + result + }.values.toList() it.copy( - messages = messages, + messages = merged, + nextCursor = nextCursor, + hasMore = hasMore, loading = false, loaded = true, + historyBeforeLoad = null, error = null, // A running call the history reports (a turn that started before // this fetch) is stamped now, the best local measurement there is; // a call that already settled drops its stamp. - toolStartedAt = runningToolStamps(it.toolStartedAt, messages, now()), + toolStartedAt = runningToolStamps(it.toolStartedAt, merged, now()), ) } // A conversation on screen is read as its history arrives: its recency diff --git a/android/app/src/test/java/net/awan/pipelines/ui/chat/MultiChatStateTest.kt b/android/app/src/test/java/net/awan/pipelines/ui/chat/MultiChatStateTest.kt index 0c94115f..b6f6666e 100644 --- a/android/app/src/test/java/net/awan/pipelines/ui/chat/MultiChatStateTest.kt +++ b/android/app/src/test/java/net/awan/pipelines/ui/chat/MultiChatStateTest.kt @@ -112,6 +112,71 @@ class MultiChatStateTest { assertEquals(ChatScrollPosition(1, 0), store.value.thread("b")!!.scroll) } + @Test + fun olderHistoryPrependsOnceAndKeepsLiveMessage() { + val store = ChatStore() + store.open(session("a")) + store.history("a", listOf(ChatMessage( + id = "new", + sessionId = "", + role = ChatProtocol.ROLE_ASSISTANT, + parts = listOf(ChatPart(type = ChatProtocol.PART_TEXT, id = "0", text = "live")), + )), "cursor-1", true) + store.historyLoading("a") + store.event(ChatEventFrame( + sessionId = "a", + messageId = "new", + kind = ChatProtocol.PART_TEXT, + delta = " update", + part = ChatPart(type = ChatProtocol.PART_TEXT, id = "0"), + )) + + store.history( + "a", + listOf(text("old", ChatProtocol.ROLE_USER, "older"), text("new", ChatProtocol.ROLE_ASSISTANT, "stale")), + "", + false, + ) + + val messages = store.value.thread("a")!!.messages + assertEquals(listOf("old", "new"), messages.map { it.id }) + assertEquals("live update", messages.last().parts.single().text) + assertFalse(store.value.thread("a")!!.hasMore) + } + + @Test + fun reconnectHistoryRepairsMissedTextWithoutDroppingOlderPagesOrDuplicatingIds() { + val store = ChatStore() + store.open(session("a")) + store.history("a", listOf(text("old", ChatProtocol.ROLE_USER, "older"), text("new", ChatProtocol.ROLE_ASSISTANT, "partial"))) + store.streamResumed() + store.historyLoading("a") + store.history("a", listOf(text("new", ChatProtocol.ROLE_ASSISTANT, "partial and missed final answer")), "older", true) + val messages = store.value.thread("a")!!.messages + assertEquals(listOf("old", "new"), messages.map { it.id }) + assertEquals("partial and missed final answer", messages.last().parts.single().text) + } + + @Test + fun firstHistoryReadDoesNotReplaceFullAnswerWithLiveOnlySuffix() { + for (snapshot in listOf("full answer", "full answer ta", "full answer tail")) { + val store = ChatStore() + store.open(session("a")) + store.historyLoading("a") + store.event(ChatEventFrame( + sessionId = "a", messageId = "new", kind = ChatProtocol.PART_TEXT, + delta = " tail", part = ChatPart(type = ChatProtocol.PART_TEXT, id = "delta-0"), + )) + store.history("a", listOf(ChatMessage( + id = "new", sessionId = "a", role = ChatProtocol.ROLE_ASSISTANT, + parts = listOf(ChatPart(type = ChatProtocol.PART_TEXT, text = snapshot)), + ))) + val messages = store.value.thread("a")!!.messages + assertEquals(1, messages.size) + assertEquals("full answer tail", messages.single().parts.single().text) + } + } + @Test fun busySessionStaysWritable() { val store = ChatStore() diff --git a/docs/notes/large-chat-history.md b/docs/notes/large-chat-history.md new file mode 100644 index 00000000..b71ecaea --- /dev/null +++ b/docs/notes/large-chat-history.md @@ -0,0 +1,43 @@ +# Large chat history + +The chat broker reads OpenCode V2 history in cursor pages and represents an +oversized tool result as an explicitly labelled omission rather than failing the +transcript. The native bare-array HTTP history path uses the same projector; +its existing OutputFormat read-only export fallback is unchanged and retains its +separate evidence limit. A retained hub tail is never authoritative history. + +History-specific HTTP reads retain shared auth, directory scoping, validation and +request cancellation. They do not use the generic Call 1 MiB safeguard (which is +unchanged). A fixed 32 KiB reader validates JSON incrementally, including discarded +strings/escapes. Each complete tool state (input, output, metadata, file data and +many small fields together) has a 64 KiB encoded budget. Oversized states become +valid omission cards, not raw JSON prefixes. Projection happens before ordinary +JSON decoding, one message at a time, never by buffering the raw page or string. +Memory is O(returned conversation + largest projected message), with fixed reader, +64 KiB state and depth-bounded scanner workspace (128 levels). User/final-answer +text is not capped. This is not a lazy full-detail API: omitted tool details are +not currently retrievable in the chat UI. + +Android's retained cursor/load-older controls merge pages by message id. A +request-start snapshot distinguishes stale cached messages (replace on reconnect) +from live updates received during the read (preserve). Cached older pages survive +newest-page refreshes. + +2026-10-18 verification is fixture evidence, not a live model or production +deployment claim. `go test ./internal/opencode -run 'History|ChatMessages' -v` +measured approximately 175 KiB allocated for both generated 2 MiB and 32 MiB +single-tool strings, with maximum source reads of 32 KiB. Tests cover malformed +discarded detail, UTF-16 escapes, cancellation during huge strings and HTTP reads, +auth refusal, legacy arrays, uncapped answer text, and the generic Call safeguard. +`TestChatHistoryLargeFixtureWebsocketTraversalAndReconnect` exercises a scratch +HTTP fixture through the real broker and /connect websocket, including a >1 MiB +page of many tools, one >1 MiB output, opaque cursors, equal-timestamp ordering, +full traversal and reconnect between pages. No production history was seeded. + +`internal/author/skill/references/` has no history-pagination guidance; any chat +reference added there should document that older history remains reachable through +the opaque continuation and that a truncated tool preview is not full output. +`docs/cli.md` is stale for `chat_query.cursor`, `chat_history` (messages, +next_cursor, has_more), and tool-state `truncated`; fold those wire fields there +with the compatibility `chat_messages` list through the docs-fold writer only. +No deployment, installation, device interaction or production mutation was done. diff --git a/internal/opencode/chat.go b/internal/opencode/chat.go index 40adc005..43ac1bf1 100644 --- a/internal/opencode/chat.go +++ b/internal/opencode/chat.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "io" + "net/url" "sort" "strings" "sync" @@ -25,8 +26,9 @@ var ErrPermissionUnsupported = errors.New("OpenCode permission approval is not i // Chat is the brokered OpenCode chat adapter (route B in the research): the // Pipelines server owns the OpenCode endpoint and credentials and turns the v2 // HTTP API into protocol.ChatEventFrame frames. It reuses the owned-endpoint -// semantics, the session-id shape and the 64 KiB / 1 MiB bounds of Connection so -// no second transport or credential path is introduced. +// semantics and session-id shape of Connection, with its normal 64 KiB / 1 MiB +// call guards. History alone streams/project tool details before decoding; it +// does not introduce a total-history cap or a second credential path. type Chat struct { conn *Connection // mu serializes the adapter's HTTP calls. Connection records its last @@ -90,27 +92,63 @@ func (c *Chat) Interrupt(ctx context.Context, id string) error { // reuses Connection.Messages, including its read-only OutputFormat readback // fallback, instead of duplicating that path. It maps the verified assistant // content parts (text | reasoning | tool) onto protocol.ChatMessage. -func (c *Chat) Messages(ctx context.Context, id string, limit int) ([]protocol.ChatMessage, error) { +func (c *Chat) Messages(ctx context.Context, id string, limit int, cursor string) (protocol.ChatHistoryPage, error) { if !sessionID.MatchString(id) { - return nil, fmt.Errorf("invalid OpenCode session id") + return protocol.ChatHistoryPage{}, fmt.Errorf("invalid OpenCode session id") } var data []byte var err error c.mu.Lock() + defer c.mu.Unlock() + var reader io.Reader if c.conn.Backend == "v1" { - data, err = c.conn.Messages(ctx, id) - } else { - path := "/api/session/" + id + "/message" - if limit > 0 { - path += fmt.Sprintf("?limit=%d", limit) + var body io.ReadCloser + body, err = c.conn.HistoryNative(ctx, "/session/"+id+"/message") + if err == nil { + defer body.Close() + reader = body + } else if c.conn.LastHTTPStatus == 400 { + // Preserve the existing OutputFormat/read-only export recovery on + // native serve. Ordinary history is streamed, never Call-buffered. + data, err = c.conn.Messages(ctx, id) + reader = bytes.NewReader(data) + } + } else { + if limit <= 0 { + limit = 100 // bounded newest-page request, including legacy clients + } + path := "/api/session/" + id + "/message" + values := make([]string, 0, 2) + if limit > 0 { + values = append(values, fmt.Sprintf("limit=%d", limit)) + } + if cursor != "" { + values = append(values, "cursor="+url.QueryEscape(cursor)) + } + if len(values) > 0 { + path += "?" + strings.Join(values, "&") + } + var body io.ReadCloser + body, err = c.conn.HistoryNative(ctx, path) + if err == nil { + defer body.Close() + reader = body } - data, err = c.conn.Call(ctx, "get", path, nil) } - c.mu.Unlock() if err != nil { - return nil, err + return protocol.ChatHistoryPage{}, err } - return decodeChatMessages(id, data) + messages, next, descending, err := decodeChatHistoryPage(ctx, id, reader) + if err != nil { + return protocol.ChatHistoryPage{}, err + } + // The verified API returns newest first. The transcript is chronological. + if descending { + for left, right := 0, len(messages)-1; left < right; left, right = left+1, right-1 { + messages[left], messages[right] = messages[right], messages[left] + } + } + return protocol.ChatHistoryPage{Messages: messages, NextCursor: next, HasMore: next != ""}, nil } // Sessions lists the OpenCode sessions on the endpoint. On the V2 HTTP API that @@ -793,6 +831,10 @@ func decodeChatMessages(sessionID string, data []byte) ([]protocol.ChatMessage, if err != nil { return nil, err } + return mapChatMessages(sessionID, records), nil +} + +func mapChatMessages(sessionID string, records []nativeChatMessage) []protocol.ChatMessage { messages := make([]protocol.ChatMessage, 0, len(records)) for _, record := range records { role := record.role() @@ -815,9 +857,19 @@ func decodeChatMessages(sessionID string, data []byte) ([]protocol.ChatMessage, } messages = append(messages, out) } - return messages, nil + return messages } +// decodeChatHistoryPage reads the V2 cursor envelope while retaining the older +// bare-array decoder for native serve. Tool output is intentionally previewed: +// one command's unbounded stdout must not make the entire transcript or its final +// answer unavailable over the websocket. +func decodeChatHistoryPage(ctx context.Context, sessionID string, reader io.Reader) ([]protocol.ChatMessage, string, bool, error) { + return readHistoryPage(ctx, sessionID, reader) +} + +const historyToolPreviewBytes = 64 << 10 + // nativeChatSession is one listed session in the verified V2 shape // ({id,title,agent,model,cost,tokens,outcome,time,title,location.directory, // parentID,permissions}); only the fields the protocol surfaces are read. The diff --git a/internal/opencode/chat_test.go b/internal/opencode/chat_test.go index 2aab9b59..b00ebcf5 100644 --- a/internal/opencode/chat_test.go +++ b/internal/opencode/chat_test.go @@ -161,6 +161,9 @@ func TestChatRejectsInvalidSessionWithoutCalling(t *testing.T) { if err := chat.Interrupt(ctx, "bad id"); err == nil { t.Fatal("invalid interrupt session id must be rejected") } + if _, err := chat.Messages(ctx, "bad id", 1, ""); err == nil { + t.Fatal("invalid history session id must be rejected") + } if _, err := chat.Permissions(ctx, "not-a-session"); err == nil { t.Fatal("invalid permission session id must be rejected") } @@ -498,6 +501,44 @@ func TestDecodeChatMessagesSkipsUnknownPartsAndRoles(t *testing.T) { } } +func TestChatMessagesPagesLargeToolHistoryWithoutGlobalResponseLimit(t *testing.T) { + large := strings.Repeat("x", MaxResponse+1) + var paths []string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + paths = append(paths, r.URL.String()) + w.Header().Set("Content-Type", "application/json") + if r.URL.Query().Get("cursor") == "older-token" { + _, _ = io.WriteString(w, `{"data":[{"id":"old","type":"user","time":{"created":1},"text":"older"}],"cursor":{"next":null}}`) + return + } + _, _ = io.WriteString(w, `{"data":[{"id":"new","type":"assistant","time":{"created":2},"content":[{"type":"tool","id":"tool","state":{"status":"completed","content":[{"type":"text","text":"`+large+`"}]}},{"type":"text","text":"final answer"}]}],"cursor":{"next":"older-token"}}`) + })) + defer srv.Close() + conn := &Connection{Backend: "v2", URL: srv.URL} + chat := NewChat(conn) + page, err := chat.Messages(context.Background(), "ses_1", 1, "") + if err != nil { + t.Fatal(err) + } + if !page.HasMore || page.NextCursor != "older-token" || len(page.Messages) != 1 { + t.Fatalf("page = %+v", page) + } + parts := page.Messages[0].Parts + if len(parts) != 2 || parts[0].State == nil || !parts[0].State.Truncated || parts[1].Text != "final answer" { + t.Fatalf("parts = %+v", parts) + } + older, err := chat.Messages(context.Background(), "ses_1", 1, page.NextCursor) + if err != nil || older.HasMore || len(older.Messages) != 1 || older.Messages[0].ID != "old" { + t.Fatalf("older = %+v, %v", older, err) + } + if len(paths) != 2 || !strings.Contains(paths[1], "cursor=older-token") { + t.Fatalf("requests = %v", paths) + } + if _, err := conn.Call(context.Background(), "get", "/api/session/ses_1/message", nil); err == nil || !strings.Contains(err.Error(), "1 MiB") { + t.Fatalf("generic Call safeguard changed: %v", err) + } +} + // TestChatSessionsListsAndLiftsShape loads the verified V2 session list // (GET /api/session: {"data":[{id,...,location.directory,time{created,updated}, // parentID}]}) and asserts the adapter reads it exactly once, lifts the nested @@ -772,11 +813,11 @@ func TestChatV1ConnectionDrivesThroughCall(t *testing.T) { if len(got) != 1 || got[0].Kind != protocol.ChatEventText || got[0].MessageID != "msg_1" { t.Fatalf("stream frames = %+v", got) } - messages, err := chat.Messages(ctx, "ses_1", 0) + page, err := chat.Messages(ctx, "ses_1", 0, "") if err != nil { t.Fatalf("messages: %v", err) } - if !sawMessages || len(messages) != 1 || messages[0].ID != "msg_1" { - t.Fatalf("messages = %+v", messages) + if !sawMessages || len(page.Messages) != 1 || page.Messages[0].ID != "msg_1" { + t.Fatalf("messages = %+v", page.Messages) } } diff --git a/internal/opencode/connection.go b/internal/opencode/connection.go index f7a8e053..801ded91 100644 --- a/internal/opencode/connection.go +++ b/internal/opencode/connection.go @@ -321,6 +321,51 @@ func (c *Connection) Native(ctx context.Context, method, path string, body any) return c.nativeRequest(ctx, method, path, body, c.Directory) } +// HistoryNative performs an authenticated history read without Connection.Call's +// general 1 MiB response guard. Chat history is paged and projected before it is +// sent to a client; ordinary API calls keep their existing safeguard. +func (c *Connection) HistoryNative(ctx context.Context, path string) (io.ReadCloser, error) { + c.LastHTTPStatus = 0 + c.LastHTTPOperation = "GET " + path + u, err := url.Parse(c.URL + path) + if err != nil { + return nil, err + } + if c.Directory != "" { + query := u.Query() + query.Set("directory", c.Directory) + u.RawQuery = query.Encode() + } + req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil) + if err != nil { + return nil, err + } + req.Header.Set("Accept", "application/json") + req.SetBasicAuth(c.serverUser(), c.password) + resp, err := http.DefaultClient.Do(req) + if err != nil { + if ctx.Err() != nil { + return nil, ctx.Err() + } + return nil, fmt.Errorf("OpenCode history request failed") + } + c.LastHTTPStatus = resp.StatusCode + c.LastHTTPOperation = "GET " + path + if resp.StatusCode == http.StatusUnauthorized { + c.unauthorized = true + } + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + defer resp.Body.Close() + _, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, 16<<10)) + return nil, fmt.Errorf("OpenCode GET %s failed (HTTP %d)", path, resp.StatusCode) + } + if !strings.HasPrefix(resp.Header.Get("Content-Type"), "application/json") { + resp.Body.Close() + return nil, fmt.Errorf("OpenCode GET %s returned a non-JSON API response", path) + } + return resp.Body, nil +} + // nativeRequest is the shared Native request path. An empty directory omits the // ?directory= query parameter; the V2 session list then returns sessions across // every directory instead of only the connection's own. diff --git a/internal/opencode/history.go b/internal/opencode/history.go new file mode 100644 index 00000000..b6b72fa4 --- /dev/null +++ b/internal/opencode/history.go @@ -0,0 +1,323 @@ +package opencode + +import ( + "bufio" + "context" + "encoding/json" + "fmt" + "io" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// historyJSON projects tool state BEFORE encoding/json sees it. Decoder.Token +// alone is not sufficient: it allocates an entire JSON string, even when the +// caller immediately discards it. This scanner consumes strings byte by byte +// through a fixed 32 KiB reader, including escapes and discarded detail. +// +// Workspace is O(32 KiB + 64 KiB + depth*256), depth <= 128. Retained memory is +// O(returned messages + largest projected message): answer text is never capped; each tool state +// contributes at most 64 KiB. An oversized state (including input, metadata, +// binary content or many small fields) is replaced by an honest omission card, +// NOT a raw JSON prefix. Neither wire size nor a single string size is a cap on +// history. No temporary disk copy or unbounded raw message/page is retained. +type historyJSON struct { + ctx context.Context + r *bufio.Reader +} + +// Negative limits are used only for a projected message (output-proportional). +// State, cursor and key captures reuse the existing bounded output helper. +type historyBuffer = boundedOutput + +func (b *boundedOutput) put(c byte) { + if b == nil { + return + } + b.WriteByte(c) +} + +func readHistoryPage(ctx context.Context, sessionID string, r io.Reader) ([]protocol.ChatMessage, string, bool, error) { + p := historyJSON{ctx: ctx, r: bufio.NewReaderSize(r, 32<<10)} + messages := make([]protocol.ChatMessage, 0) + var next string + readMessages := func() error { + if err := p.take(nil, '['); err != nil { + return err + } + c, err := p.peek() + if err != nil { + return err + } + if c == ']' { + return p.take(nil, ']') + } + for { + out := &historyBuffer{limit: -1} + if err := p.value(out, 1, true); err != nil { + return err + } + var record nativeChatMessage + if err := json.Unmarshal(out.Bytes(), &record); err != nil { + return err + } + messages = append(messages, mapChatMessages(sessionID, []nativeChatMessage{record})...) + c, err := p.peek() + if err != nil { + return err + } + if c == ']' { + return p.take(nil, ']') + } + if err := p.take(nil, ','); err != nil { + return err + } + } + } + c, err := p.peek() + if err != nil { + return nil, "", false, err + } + descending := c == '{' + if c == '[' { + err = readMessages() + } else if descending { + p.take(nil, '{') + found := false + 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 err = p.take(nil, ':'); err != nil { + break + } + switch name { + case "data": + if found { + err = fmt.Errorf("duplicate history data") + } else { + found = true + err = readMessages() + } + case "cursor": + cursor := &historyBuffer{limit: 64 << 10} + err = p.value(cursor, 1, false) + if err == nil { + var decoded struct { + Next string `json:"next"` + } + if cursor.overflow { + err = fmt.Errorf("history cursor exceeds 64 KiB") + } else { + err = json.Unmarshal(cursor.Bytes(), &decoded) + next = decoded.Next + } + } + default: + err = p.value(nil, 1, false) + } + if err != nil { + break + } + c, err = p.peek() + if err != nil || c == '}' { + if err == nil { + err = p.take(nil, '}') + } + break + } + err = p.take(nil, ',') + } + if err == nil && !found { + err = fmt.Errorf("missing history data") + } + } else { + err = fmt.Errorf("invalid history envelope") + } + if err != nil { + return nil, "", false, fmt.Errorf("invalid OpenCode history: %w", err) + } + if _, err := p.peek(); err != io.EOF { + if err == nil { + err = fmt.Errorf("trailing JSON data") + } + return nil, "", false, err + } + return messages, next, descending, nil +} + +func (p *historyJSON) peek() (byte, error) { + for { + if err := p.ctx.Err(); err != nil { + return 0, err + } + b, err := p.r.Peek(1) + if err != nil { + return 0, err + } + switch b[0] { + case ' ', '\n', '\r', '\t': + p.r.ReadByte() + default: + return b[0], nil + } + } +} + +func (p *historyJSON) take(out *historyBuffer, want byte) error { + c, err := p.peek() + if err != nil { + return err + } + if c != want { + return fmt.Errorf("expected JSON delimiter") + } + p.r.ReadByte() + out.put(c) + return nil +} + +func (p *historyJSON) str(out, key *historyBuffer) error { + if err := p.take(out, '"'); err != nil { + return err + } + key.put('"') + escape, unicode := false, 0 + for { + // Check cancellation even within a multi-gigabyte discarded string. + if err := p.ctx.Err(); err != nil { + return err + } + c, err := p.r.ReadByte() + if err != nil { + return err + } + out.put(c) + key.put(c) + if unicode > 0 { + if !(c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F') { + return fmt.Errorf("invalid Unicode escape") + } + unicode-- + } else if escape { + escape = false + switch c { + case 'u': + unicode = 4 + case '"', '\\', '/', 'b', 'f', 'n', 'r', 't': + default: + return fmt.Errorf("invalid string escape") + } + } else { + switch { + case c == '"': + return nil + case c == '\\': + escape = true + case c < 0x20: + return fmt.Errorf("invalid string control character") + } + } + } +} + +func (p *historyJSON) value(out *historyBuffer, depth int, project bool) error { + if depth > 128 { + return fmt.Errorf("history JSON nesting exceeds 128") + } + c, err := p.peek() + if err != nil { + return err + } + switch c { + case '"': + return p.str(out, nil) + case '{', '[': + open, close := c, byte('}') + if open == '[' { + close = ']' + } + p.take(out, open) + c, err = p.peek() + if err != nil { + return err + } + if c == close { + return p.take(out, close) + } + for { + var name string + if open == '{' { + key := &historyBuffer{limit: 256} + if err := p.str(out, key); err != nil { + return err + } + if !key.overflow { + _ = json.Unmarshal(key.Bytes(), &name) + } + if err := p.take(out, ':'); err != nil { + return err + } + } + if project && name == "state" { + state := &historyBuffer{limit: historyToolPreviewBytes} + if err := p.value(state, depth+1, false); err != nil { + return err + } + data := state.Bytes() + if state.overflow { + data = []byte(`{"truncated":true,"content":[{"type":"text","text":"Tool details omitted: stored result exceeds the 64 KiB history preview budget."}]}`) + } + for _, b := range data { + out.put(b) + } + } else if err := p.value(out, depth+1, project); err != nil { + return err + } + c, err = p.peek() + if err != nil { + return err + } + if c == close { + return p.take(out, close) + } + if err := p.take(out, ','); err != nil { + return err + } + } + default: + // JSON numbers/literals are small in this schema. Validate without ever + // accumulating an attacker-controlled unbounded primitive. + token := &historyBuffer{limit: 128} + for { + b, err := p.r.Peek(1) + if err == io.EOF { + break + } + if err != nil { + return err + } + c = b[0] + if c == ',' || c == ']' || c == '}' || c == ' ' || c == '\n' || c == '\r' || c == '\t' { + break + } + p.r.ReadByte() + token.put(c) + if token.overflow { + return fmt.Errorf("invalid history primitive") + } + } + if !json.Valid(token.Bytes()) { + return fmt.Errorf("invalid history primitive") + } + for _, b := range token.Bytes() { + out.put(b) + } + return nil + } +} diff --git a/internal/opencode/history_test.go b/internal/opencode/history_test.go new file mode 100644 index 00000000..b817a46d --- /dev/null +++ b/internal/opencode/history_test.go @@ -0,0 +1,203 @@ +package opencode + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "io" + "net/http" + "net/http/httptest" + "runtime" + "strings" + "testing" +) + +func mustHistoryJSON(t *testing.T, value any) string { + t.Helper() + data, err := json.Marshal(value) + if err != nil { + t.Fatal(err) + } + return string(data) +} + +// A generated fixture, not a live-model history. No allocation proportional to +// the oversized string exists on either side of the allocation measurement. +type historyRepeatReader struct { + remaining int + read int + maxRead int + cancel context.CancelFunc +} + +func (r *historyRepeatReader) Read(b []byte) (int, error) { + if len(b) > r.maxRead { + r.maxRead = len(b) + } + n := min(len(b), r.remaining) + for i := range b[:n] { + b[i] = 'x' + } + r.remaining -= n + r.read += n + if r.cancel != nil && r.read > 1<<20 { + r.cancel() + } + if n == 0 { + return 0, io.EOF + } + return n, nil +} + +func historyLargeFixture(detail io.Reader) io.Reader { + return io.MultiReader( + strings.NewReader(`{"data":[{"id":"new","type":"assistant","content":[{"type":"tool","state":{"status":"completed","content":[{"type":"text","text":"`), + detail, + strings.NewReader(`"}]}},{"type":"text","text":"Newest answer: \uD83C\uDF19"}]}],"cursor":{"next":"opaque+/=&"}}`), + ) +} + +func TestHistoryStreamingAllocationBound(t *testing.T) { + for _, size := range []int{2 << 20, 32 << 20} { + t.Run(fmt.Sprint(size), func(t *testing.T) { + r := &historyRepeatReader{remaining: size} + reader := historyLargeFixture(r) + runtime.GC() + var before, after runtime.MemStats + runtime.ReadMemStats(&before) + messages, next, _, err := decodeChatHistoryPage(context.Background(), "ses_1", reader) + runtime.ReadMemStats(&after) + allocated := after.TotalAlloc - before.TotalAlloc + t.Logf("wire detail=%d allocated=%d max read=%d", size, allocated, r.maxRead) + if err != nil { + t.Fatal(err) + } + if allocated > 1<<20 || r.maxRead > 32<<10 || r.read != size { + t.Fatalf("unbounded reader: alloc=%d read=%d chunk=%d", allocated, r.read, r.maxRead) + } + if next != "opaque+/=&" || len(messages) != 1 || messages[0].Parts[1].Text != "Newest answer: 🌙" || !messages[0].Parts[0].State.Truncated { + t.Fatalf("projection lost answer/cursor: %+v", messages) + } + }) + } +} + +func TestHistoryStreamingCancellation(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + r := &historyRepeatReader{remaining: 32 << 20, cancel: cancel} + _, _, _, err := decodeChatHistoryPage(ctx, "ses_1", historyLargeFixture(r)) + if !errors.Is(err, context.Canceled) || r.read >= 2<<20 { + t.Fatalf("cancellation did not interrupt discarded string: read=%d err=%v", r.read, err) + } +} + +func TestHistoryBoundsAllToolDetails(t *testing.T) { + for _, detail := range []string{ + `"input":{"text":"` + strings.Repeat("x", 2<<20) + `"}`, + `"metadata":{"blob":"` + strings.Repeat("x", 2<<20) + `"}`, + `"content":[{"type":"file","data":"` + strings.Repeat("x", 2<<20) + `"}]`, + `"content":[` + strings.Repeat(`{"type":"text","text":"small"},`, 10000) + `{"type":"text","text":"last"}]`, + } { + raw := `{"data":[{"type":"assistant","content":[{"type":"tool","state":{` + detail + `}},{"type":"text","text":"answer"}]}]}` + messages, _, _, err := decodeChatHistoryPage(context.Background(), "ses_1", strings.NewReader(raw)) + if err != nil || len(messages) != 1 || !messages[0].Parts[0].State.Truncated || messages[0].Parts[1].Text != "answer" { + t.Fatalf("detail prevented answer: %v", err) + } + } +} + +func TestHistoryLegacyLargeToolAndNonJSON(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/session/ses_1/message" { + t.Errorf("wrong legacy path: %s", r.URL) + } + if r.URL.Query().Get("directory") == "not-json" { + io.WriteString(w, "not JSON") + return + } + w.Header().Set("Content-Type", "application/json") + io.WriteString(w, `[{"info":{"id":"msg_1","role":"assistant"},"parts":[{"type":"tool","state":{"content":[{"text":"`) + io.Copy(w, &historyRepeatReader{remaining: 2 << 20}) + io.WriteString(w, `"}]}},{"type":"text","text":"legacy answer"}]}]`) + })) + defer srv.Close() + conn := &Connection{Backend: "v1", URL: srv.URL} + chat := NewChat(conn) + page, err := chat.Messages(context.Background(), "ses_1", 1, "") + if err != nil || page.HasMore || len(page.Messages) != 1 || !page.Messages[0].Parts[0].State.Truncated || page.Messages[0].Parts[1].Text != "legacy answer" { + t.Fatalf("legacy history: %+v %v", page, err) + } + conn.Directory = "not-json" + if _, err := chat.Messages(context.Background(), "ses_1", 1, ""); err == nil || !strings.Contains(err.Error(), "non-JSON") { + t.Fatalf("accepted non-JSON history: %v", err) + } +} + +func TestHistoryRejectsMalformedIncludingDiscardedDetail(t *testing.T) { + for _, raw := range []string{ + `null`, `{"data":null}`, `{"data":{}}`, `{"data":[],"cursor":{"next":42}}`, + `{"data":[]} {}`, `{"data":[}`, `{"data":[],}`, `{"data":[],"x":01}`, + `{"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), + strings.Repeat("[", 130) + strings.Repeat("]", 130), + } { + if _, _, _, err := decodeChatHistoryPage(context.Background(), "ses_1", strings.NewReader(raw)); err == nil { + t.Fatalf("accepted malformed fixture (length %d)", len(raw)) + } + } +} + +func TestHistoryDoesNotCapConversationTextOrSmallToolState(t *testing.T) { + answer := strings.Repeat("answer 🌙\n", 150000) + raw := mustHistoryJSON(t, map[string]any{"data": []any{ + map[string]any{"id": "new", "type": "assistant", "text": "", "content": []any{ + map[string]any{"type": "text", "text": answer}, + map[string]any{"type": "tool", "state": map[string]any{"status": "completed", "input": map[string]any{"x": "y"}, "metadata": map[string]any{"exit": 0}}}, + }}, + }}) + messages, _, _, err := decodeChatHistoryPage(context.Background(), "ses_1", strings.NewReader(raw)) + if err != nil || len(messages) != 1 || messages[0].Parts[0].Text != answer { + t.Fatalf("conversation text capped: %v", err) + } + state := messages[0].Parts[1].State + if state.Truncated || state.Status != "completed" || string(state.Input) != `{"x":"y"}` || state.Metadata["exit"] != float64(0) { + t.Fatalf("small state changed: %+v", state) + } +} + +func TestHistoryHTTPAuthenticationAndCancellation(t *testing.T) { + started := make(chan struct{}) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + user, pass, _ := r.BasicAuth() + if user != "operator" || pass != "fixture-password" { + w.WriteHeader(http.StatusUnauthorized) + return + } + if r.URL.Query().Get("directory") != "/fixture dir" { + t.Errorf("missing directory scope: %s", r.URL) + } + w.Header().Set("Content-Type", "application/json") + io.WriteString(w, `{"data":[`) + w.(http.Flusher).Flush() + close(started) + <-r.Context().Done() + })) + defer srv.Close() + conn := &Connection{Backend: "v2", URL: srv.URL, username: "operator", Directory: "/fixture dir"} + chat := NewChat(conn) + if _, err := chat.Messages(context.Background(), "ses_1", 1, ""); err == nil || !conn.unauthorized || conn.LastHTTPStatus != 401 { + t.Fatalf("auth refusal not recorded: %v %+v", err, conn) + } + conn.password = "fixture-password" + ctx, cancel := context.WithCancel(context.Background()) + done := make(chan error, 1) + go func() { _, err := chat.Messages(ctx, "ses_1", 1, ""); done <- err }() + <-started + cancel() + if err := <-done; !errors.Is(err, context.Canceled) { + t.Fatalf("HTTP cancellation = %v", err) + } +} diff --git a/internal/opencode/messages.go b/internal/opencode/messages.go index db7105b5..a4ff527e 100644 --- a/internal/opencode/messages.go +++ b/internal/opencode/messages.go @@ -16,6 +16,9 @@ type boundedOutput struct { } func (b *boundedOutput) Write(p []byte) (int, error) { + if b.limit < 0 { + return b.Buffer.Write(p) + } n := len(p) remaining := b.limit - b.Len() if n > remaining { @@ -26,6 +29,16 @@ func (b *boundedOutput) Write(p []byte) (int, error) { return n, nil } +// WriteByte keeps byte-oriented history scanning under the same bound as Write; +// the embedded Buffer's promoted WriteByte would otherwise bypass the cap. +func (b *boundedOutput) WriteByte(c byte) error { + if b.limit >= 0 && b.Len() >= b.limit { + b.overflow = true + return nil + } + return b.Buffer.WriteByte(c) +} + // ReadFrom reads through the bounded Write so the cap applies even when // os/exec's io.Copy selects a ReadFrom on the destination: without this the // embedded bytes.Buffer's promoted ReadFrom would bypass Write and buffer the diff --git a/internal/protocol/chat.go b/internal/protocol/chat.go index 70138e91..fea4d223 100644 --- a/internal/protocol/chat.go +++ b/internal/protocol/chat.go @@ -193,6 +193,18 @@ type ChatAdoptRequest struct { type ChatQuery struct { SessionID string `json:"session_id"` Limit int `json:"limit,omitempty"` + // Cursor is the opaque continuation returned by OpenCode's descending + // message listing. It is deliberately not interpreted by the broker. + Cursor string `json:"cursor,omitempty"` +} + +// ChatHistoryPage describes the bounded slice returned by chat_messages. The +// newest page has an empty Cursor request; NextCursor remains opaque and is +// supplied unchanged to fetch older turns. +type ChatHistoryPage struct { + Messages []ChatMessage `json:"messages"` + NextCursor string `json:"next_cursor,omitempty"` + HasMore bool `json:"has_more,omitempty"` } // ChatPromptRequest is the chat_prompt request body: the text to send, with an @@ -266,6 +278,9 @@ type ChatToolState struct { Content []ChatToolContent `json:"content,omitempty"` Metadata map[string]any `json:"metadata,omitempty"` Error *ChatError `json:"error,omitempty"` + // Truncated says Content is a bounded preview of a larger stored result. + // The turn remains usable; callers must not present the preview as complete. + Truncated bool `json:"truncated,omitempty"` } // ChatToolContent is one returned tool result: either text or a file/uri pair diff --git a/internal/protocol/message.go b/internal/protocol/message.go index f3d1f8d5..1fbaa296 100644 --- a/internal/protocol/message.go +++ b/internal/protocol/message.go @@ -169,6 +169,7 @@ type Message struct { ChatQuery *ChatQuery `json:"chat_query,omitempty"` ChatPrompt *ChatPromptRequest `json:"chat_prompt,omitempty"` ChatMessages []ChatMessage `json:"chat_messages,omitempty"` + ChatHistory *ChatHistoryPage `json:"chat_history,omitempty"` ChatEvent *ChatEventFrame `json:"chat_event,omitempty"` ChatPermission *ChatPermissionFrame `json:"chat_permission,omitempty"` ChatReply *ChatReplyRequest `json:"chat_reply,omitempty"` diff --git a/internal/server/chat.go b/internal/server/chat.go index 1601fbfc..99eb212c 100644 --- a/internal/server/chat.go +++ b/internal/server/chat.go @@ -19,7 +19,7 @@ type chatBroker interface { // can order the prompt after the subscription. Stream(ctx context.Context, sessionID string, ready func(), emit func(protocol.ChatEventFrame) error) error Interrupt(ctx context.Context, sessionID string) error - Messages(ctx context.Context, sessionID string, limit int) ([]protocol.ChatMessage, error) + Messages(ctx context.Context, sessionID string, limit int, cursor string) (protocol.ChatHistoryPage, error) // Sessions lists the endpoint's OpenCode sessions so the handler can // restrict them to the operator's own recorded sessions. Sessions(ctx context.Context) ([]protocol.ChatSession, error) diff --git a/internal/server/chat_commands.go b/internal/server/chat_commands.go index 2d160373..d9efa1f9 100644 --- a/internal/server/chat_commands.go +++ b/internal/server/chat_commands.go @@ -15,23 +15,19 @@ func handleChatMessages(ctx context.Context, request protocol.Message, broker ch return } response := protocol.Message{Type: protocol.ChatMessages, ID: request.ID} - // Resume from retained server state first: a reconnecting client gets the - // accumulated turns with the same stable ids without re-reading OpenCode. - if messages := hub.snapshot(query.SessionID); len(messages) > 0 { - response.ChatMessages = messages - _ = write(response) - return - } + // A hub snapshot is only the live tail. It is never authoritative history: + // serving it here used to make older turns unreachable after a reconnect. if broker == nil { _ = write(response) return } - messages, err := broker.Messages(ctx, query.SessionID, query.Limit) + page, err := broker.Messages(ctx, query.SessionID, query.Limit, query.Cursor) if err != nil { _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) return } - response.ChatMessages = messages + response.ChatMessages = page.Messages // legacy peers keep working. + response.ChatHistory = &page _ = write(response) } diff --git a/internal/server/chat_history_test.go b/internal/server/chat_history_test.go new file mode 100644 index 00000000..7c6d2ee4 --- /dev/null +++ b/internal/server/chat_history_test.go @@ -0,0 +1,107 @@ +package server + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "net/http/httptest" + "reflect" + "strings" + "testing" + + "git.a-wan.net/alan/pipelines/internal/opencode" + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// Scratch HTTP + real /connect websocket fixture, NOT a live-model/production +// claim. Covers both >1 MiB total (many tools) and one >1 MiB tool string. +func TestChatHistoryLargeFixtureWebsocketTraversalAndReconnect(t *testing.T) { + fixture := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/session/ses_1/message" || r.URL.Query().Get("limit") != "2" { + t.Errorf("unexpected history query %s", r.URL) + } + w.Header().Set("Content-Type", "application/json") + page := 0 + switch r.URL.Query().Get("cursor") { + case "": + case "older+/=&": + page = 1 + case "last": + page = 2 + default: + t.Errorf("cursor not passed opaquely: %s", r.URL) + } + io.WriteString(w, `{"data":[`) + for i := 0; i < 2; i++ { + if i != 0 { + io.WriteString(w, ",") + } + id := 6 - page*2 - i + // Equal timestamps deliberately: order comes from the verified V2 + // descending sequence, not a guessed timestamp comparison. + fmt.Fprintf(w, `{"id":"msg_%d","type":"assistant","time":{"created":1},"content":[`, id) + if page == 0 && i == 0 { + io.WriteString(w, `{"type":"tool","id":"huge","state":{"content":[{"type":"text","text":"`) + chunk := strings.Repeat("x", 32<<10) + for n := 0; n < 65; n++ { + io.WriteString(w, chunk) + } + io.WriteString(w, `"}]}},`) + } + if page == 1 && i == 0 { + for n := 0; n < 25; n++ { + fmt.Fprintf(w, `{"type":"tool","id":"tool_%d","state":{"status":"completed","content":[{"type":"text","text":%q}]}},`, n, strings.Repeat("y", 48<<10)) + } + } + fmt.Fprintf(w, `{"type":"text","text":"answer %d"}]}`, id) + } + next := []any{"older+/=&", "last", nil}[page] + encoded, _ := json.Marshal(next) + fmt.Fprintf(w, `],"cursor":{"next":%s}}`, encoded) + })) + defer fixture.Close() + broker := opencode.NewChat(&opencode.Connection{Backend: "v2", URL: fixture.URL}) + ctx, _, srv, conns := dialChatServer(t, Options{Chat: broker}, 1) + conn := conns[0] + conn.SetReadLimit(8 << 20) + cursor := "" + var ids []string + seen := map[string]bool{} + for page := 0; page < 3; page++ { + writeChat(t, ctx, conn, protocol.Message{Type: protocol.ChatMessages, ID: "history", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1", Limit: 2, Cursor: cursor}}) + response := readUntil(t, ctx, conn, protocol.ChatMessages) + if response.ChatHistory == nil || len(response.ChatHistory.Messages) != 2 { + t.Fatalf("missing page: %+v", response) + } + history := response.ChatHistory + if !reflect.DeepEqual(response.ChatMessages, history.Messages) { + t.Fatal("legacy/page views differ") + } + for _, message := range history.Messages { + if seen[message.ID] { + t.Fatalf("duplicate %s", message.ID) + } + seen[message.ID] = true + } + pageIDs := []string{history.Messages[0].ID, history.Messages[1].ID} + ids = append(pageIDs, ids...) + if page == 0 { + newest := history.Messages[1] + if newest.Parts[len(newest.Parts)-1].Text != "answer 6" || !newest.Parts[0].State.Truncated { + t.Fatal("huge tool prevented newest answer") + } + } + cursor = history.NextCursor + if history.HasMore != (page < 2) { + t.Fatal("incorrect continuation") + } + // Reconnect between pages: continuation is server-owned, not a hub tail. + conn.CloseNow() + conn = dialChatConn(t, ctx, srv) + conn.SetReadLimit(8 << 20) + } + if cursor != "" || !reflect.DeepEqual(ids, []string{"msg_1", "msg_2", "msg_3", "msg_4", "msg_5", "msg_6"}) { + t.Fatalf("incomplete traversal: %v cursor=%q", ids, cursor) + } +} diff --git a/internal/server/chat_multiclient_test.go b/internal/server/chat_multiclient_test.go index 43d373a5..61f15e03 100644 --- a/internal/server/chat_multiclient_test.go +++ b/internal/server/chat_multiclient_test.go @@ -68,8 +68,8 @@ func (c *channelChat) Stream(ctx context.Context, _ string, ready func(), emit f func (c *channelChat) Interrupt(context.Context, string) error { return nil } -func (c *channelChat) Messages(context.Context, string, int) ([]protocol.ChatMessage, error) { - return nil, nil +func (c *channelChat) Messages(context.Context, string, int, string) (protocol.ChatHistoryPage, error) { + return protocol.ChatHistoryPage{}, nil } func (c *channelChat) Sessions(context.Context) ([]protocol.ChatSession, error) { return nil, nil } @@ -440,10 +440,11 @@ func TestChatPendingApprovalsAllReplayed(t *testing.T) { } // TestChatResyncAfterReconnect is the resync acceptance test: a client that drops -// and reconnects re-syncs the whole conversation from retained state, including -// the user turn and who sent it. +// and reconnects reads authoritative history rather than mistaking a retained +// live tail for the whole conversation. func TestChatResyncAfterReconnect(t *testing.T) { broker := scriptedChat() + broker.messages = seededChatMessages() ctx, _, srv, conns := dialChatServer(t, Options{Chat: broker}, 1) phone := conns[0] @@ -454,19 +455,16 @@ func TestChatResyncAfterReconnect(t *testing.T) { // The phone's socket drops. phone.CloseNow() - // A fresh connection re-syncs from retained state. + // A fresh connection reads the authoritative history rather than treating the + // retained live tail as a complete transcript. reconnected := dialChatConn(t, ctx, srv) writeChat(t, ctx, reconnected, protocol.Message{Type: protocol.ChatMessages, ID: "m1", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}}) response := readUntil(t, ctx, reconnected, protocol.ChatMessages) - if len(response.ChatMessages) != 2 { - t.Fatalf("resync returned %d messages, want user+assistant: %+v", len(response.ChatMessages), response.ChatMessages) + if response.ChatHistory == nil { + t.Fatalf("resync did not return a history page: %+v", response) } - user := response.ChatMessages[0] - if user.Role != protocol.ChatRoleUser || user.Client != "Pixel 9" || len(user.Parts) != 1 || user.Parts[0].Text != "hi" { - t.Fatalf("resynced user turn = %+v", user) - } - assistant := response.ChatMessages[1] - if assistant.Role != protocol.ChatRoleAssistant || assistant.ID != "msg_1" { - t.Fatalf("resynced assistant turn = %+v", assistant) + assertSeededChatMessages(t, response.ChatHistory.Messages) + if calls, _, _ := broker.messageQuery(); calls != 1 { + t.Fatalf("reconnect skipped authoritative history: %d calls", calls) } } diff --git a/internal/server/chat_test.go b/internal/server/chat_test.go index 29dbc4bb..11dfde0f 100644 --- a/internal/server/chat_test.go +++ b/internal/server/chat_test.go @@ -47,6 +47,7 @@ type fakeChat struct { messageCalls int messageSessionID string messageLimit int + messageCursor string messages []protocol.ChatMessage permissionCalls int permissionSessionID string @@ -123,13 +124,14 @@ func (f *fakeChat) Interrupt(context.Context, string) error { return f.interruptErr } -func (f *fakeChat) Messages(_ context.Context, sessionID string, limit int) ([]protocol.ChatMessage, error) { +func (f *fakeChat) Messages(_ context.Context, sessionID string, limit int, cursor string) (protocol.ChatHistoryPage, error) { f.mu.Lock() defer f.mu.Unlock() f.messageCalls++ f.messageSessionID = sessionID f.messageLimit = limit - return f.messages, nil + f.messageCursor = cursor + return protocol.ChatHistoryPage{Messages: f.messages}, nil } // messageQuery reports the recorded chat_messages call count, session id and @@ -420,6 +422,7 @@ func TestChatPromptWithoutBroker(t *testing.T) { func TestChatMessagesResumesRetainedState(t *testing.T) { broker := scriptedChat() + broker.messages = seededChatMessages() ctx, _, conn := dialChat(t, broker) if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "r1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "hi"}}); err != nil { t.Fatal(err) @@ -436,27 +439,13 @@ func TestChatMessagesResumesRetainedState(t *testing.T) { if err := wsjson.Read(ctx, conn, &response); err != nil { t.Fatal(err) } - if response.Type != protocol.ChatMessages || response.ID != "r2" || len(response.ChatMessages) != 2 { + if response.Type != protocol.ChatMessages || response.ID != "r2" || response.ChatHistory == nil { t.Fatalf("resume response = %+v", response) } - // The prompt is retained as a user turn and the assistant reply beside it, so - // a reconnecting client re-syncs a conversation that still says who sent - // what. The user turn carries the sending client's label (the generic - // fallback when the prompt did not name one). - user := response.ChatMessages[0] - if user.Role != protocol.ChatRoleUser || len(user.Parts) != 1 || user.Parts[0].Type != protocol.ChatPartText || user.Parts[0].Text != "hi" || user.Client != chatClientLabelFallback { - t.Fatalf("resumed user turn = %+v", user) - } - message := response.ChatMessages[1] - if message.ID != "msg_1" || message.Role != protocol.ChatRoleAssistant || len(message.Parts) != 2 { - t.Fatalf("resumed message = %+v", message) - } - if message.Parts[0].Type != protocol.ChatPartText || message.Parts[0].ID != protocol.ChatDeltaPartID("msg_1", protocol.ChatPartText, 0) || message.Parts[0].Text != "hello world" { - t.Fatalf("resumed text part = %+v", message.Parts[0]) - } - if message.Parts[1].Type != protocol.ChatPartTool || message.Parts[1].State == nil || message.Parts[1].State.Status != protocol.ChatToolCompleted { - t.Fatalf("resumed tool part = %+v", message.Parts[1]) + if calls, _, _ := broker.messageQuery(); calls != 1 { + t.Fatalf("authoritative history was not read: %d calls", calls) } + assertSeededChatMessages(t, response.ChatMessages) } // seededChatMessages is the stored user + assistant + tool session the @@ -547,12 +536,12 @@ func TestChatMessagesThroughRealAdapter(t *testing.T) { messageReads.Add(1) w.Header().Set("Content-Type", "application/json") _, _ = w.Write([]byte(`{"data":[ - {"id":"msg_user","time":{"created":10},"type":"user","text":"list the files"}, {"id":"msg_assistant","time":{"created":20,"completed":30},"type":"assistant","agent":"build","model":{"id":"deepseek-v4.1-flash","providerID":"opencode-go"},"content":[ {"type":"text","text":"here you go"}, {"type":"reasoning","text":"thinking"}, {"type":"tool","id":"call_1","name":"shell","executed":true,"state":{"status":"completed","input":{"command":"ls"},"content":[{"type":"text","text":"ok"}]},"time":{"created":1,"ran":2,"completed":3}} - ]} + ]}, + {"id":"msg_user","time":{"created":10},"type":"user","text":"list the files"} ],"cursor":{"next":null}}`)) })) defer fixture.Close() From 18e7d073a614a9dfe17c7d668be6580d61886911 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sun, 18 Oct 2026 15:21:09 +0100 Subject: [PATCH 2/2] 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. --- .../net/awan/pipelines/ui/AppViewModel.kt | 9 ++++++-- .../net/awan/pipelines/ui/chat/ChatStore.kt | 23 ++++++++++++++++--- internal/opencode/history.go | 19 ++++++++------- internal/opencode/history_test.go | 3 ++- 4 files changed, 40 insertions(+), 14 deletions(-) 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),