chat: fold an agent turn into one reply, split reasoning from answer, keep tool name/input

The phone's chat transcript rendered one agent reply as ~36 bubbles, each with
its own thinking block, and a finished tool call lost its name and its input.

- Turn fold (android): ChatFormat.kt gains the pure ChatTurn / TranscriptRow /
  transcriptRows fold - each run of consecutive assistant messages between two of
  the operator's messages is one agent turn - and ChatTranscript renders one
  bubble per turn: the turn's single collapsible thinking block (all its
  reasoning, in order, growing live while it streams), then its tool-call chips,
  then one answer block of just the turn's text parts. AssistantParts/MessagePart
  are replaced by AgentTurnItem/UserMessageItem; ChatConversationPane counts the
  folded rows.
- Delta part ids (server + client): internal/protocol/chat.go adds
  ChatDeltaPartID(messageID, kind, ordinal), mirrored as ChatProtocol.deltaPartId
  in data/protocol/Chat.kt, and NormalizeChatEvent keys a text or reasoning delta
  on it. OpenCode numbers a message's text and reasoning parts independently from
  0, so the old "%s#%d" gave the first text delta and first reasoning part the
  same id: the answer was appended into the thinking block, and the server's
  retained state (chat_hub.go) re-collided on resume. ChatStore's upsert now
  routes a delta through the kind-aware deltaPartId fold, so it stays robust even
  against a peer that still shares one id.
- Tool fields (server + client): NormalizeChatEvent takes a call's arguments from
  session.tool.input.ended's text as well as session.tool.called's input object
  (toolInput), and chat_hub.go's new mergeChatToolPart folds each tool frame onto
  the retained part so the progress/success frames that omit name and input never
  blank them. ChatFormat.kt's mergeToolPart mirrors that on the client, so the
  chip and panel header keep the real tool name and the panel shows INPUT beside
  OUTPUT.

Tests: chat_v2_test.go and chat_test.go add the text/reasoning id-collision and
the tool name+input retention cases; ChatReasoningTurnTest, ChatTranscriptTest
and ChatRunningWorkTest cover the turn fold, the collision and the tool lifecycle.

Checks: Go build, vet, gofmt and the package tests pass (go build ./... &&
go vet ./...; gofmt -l . empty; go test ./internal/server ./internal/opencode
./internal/protocol ./internal/stage ./internal/pipeline; go test ./...).
Android passes offline with --no-daemon (assembleDebug and the full
testDebugUnitTest suite, 660 tests). The on-phone observation was not performed:
the device lock was held by another run and adb was not available here.

Documentation inventory checked: no packaged OpenCode skill reference in
internal/author/skill/references/ and no packaged template documents the app's
chat transcript, this delta part-id derivation or the tool-field retention, and
no internal/author/ managed file inventory entry names any touched file, so none
of them needs updating. The shared android/README.md behaviour statements are
made stale by this change and are recorded instead in
docs/notes/chat-transcript-turn-fold.md for the docs-fold writer, per the repo
rule that an item never edits a shared document itself.
This commit is contained in:
2026-10-18 01:47:52 +01:00
parent 0d1ddc73a4
commit 798e958cb7
14 changed files with 801 additions and 161 deletions
+29 -3
View File
@@ -388,6 +388,7 @@ type chatEvent struct {
Delta string `json:"delta"`
ID string `json:"id"`
Name string `json:"name"`
Text string `json:"text"`
Input json.RawMessage `json:"input"`
Content []protocol.ChatToolContent `json:"content"`
Metadata map[string]any `json:"metadata"`
@@ -451,11 +452,11 @@ func NormalizeChatEvent(payload []byte, sessionID string, lastMessageID *string)
case outer.Type == "session.text.delta":
frame.Kind = protocol.ChatEventText
frame.Delta = data.Delta
frame.Part = &protocol.ChatPart{Type: protocol.ChatPartText, ID: fmt.Sprintf("%s#%d", messageID, data.Ordinal)}
frame.Part = &protocol.ChatPart{Type: protocol.ChatPartText, ID: protocol.ChatDeltaPartID(messageID, protocol.ChatPartText, data.Ordinal)}
case outer.Type == "session.reasoning.delta":
frame.Kind = protocol.ChatEventReasoning
frame.Delta = data.Delta
frame.Part = &protocol.ChatPart{Type: protocol.ChatPartReasoning, ID: fmt.Sprintf("%s#%d", messageID, data.Ordinal)}
frame.Part = &protocol.ChatPart{Type: protocol.ChatPartReasoning, ID: protocol.ChatDeltaPartID(messageID, protocol.ChatPartReasoning, data.Ordinal)}
case outer.Type == "session.step.started" || outer.Type == "session.step.streamed" || outer.Type == "session.step.ended":
frame.Kind = protocol.ChatEventStep
frame.Step = &protocol.ChatStep{Status: strings.TrimPrefix(outer.Type, "session.step."), Index: stepIndex(data)}
@@ -465,7 +466,7 @@ func NormalizeChatEvent(payload []byte, sessionID string, lastMessageID *string)
return protocol.ChatEventFrame{}, false, nil
}
frame.Kind = protocol.ChatEventTool
state := &protocol.ChatToolState{Status: status, Input: data.Input, Content: data.Content, Metadata: data.Metadata, Error: data.Error}
state := &protocol.ChatToolState{Status: status, Input: toolInput(data), Content: data.Content, Metadata: data.Metadata, Error: data.Error}
frame.Part = &protocol.ChatPart{Type: protocol.ChatPartTool, ID: data.ID, Name: data.Name, State: state}
case outer.Type == "session.usage.updated":
frame.Kind = protocol.ChatEventUsage
@@ -480,6 +481,31 @@ func NormalizeChatEvent(payload []byte, sessionID string, lastMessageID *string)
return frame, true, nil
}
// toolInput picks a tool call's arguments from whichever frame carries them:
// session.tool.called carries the parsed `input` object, session.tool.input.ended
// carries the same arguments as a `text` JSON string, and progress/success carry
// neither. Text that already parses as JSON is kept as that value (so an object
// pretty-prints); a partial string is carried as a JSON string so the frame stays
// valid and the client can show it while streaming. It returns nil when the frame
// carries no arguments, so the consumer retains what it already learned.
func toolInput(data chatEvent) json.RawMessage {
if len(data.Input) > 0 && string(data.Input) != "null" {
return data.Input
}
text := strings.TrimSpace(data.Text)
if text == "" {
return nil
}
if json.Valid([]byte(text)) {
return json.RawMessage(text)
}
encoded, err := json.Marshal(text)
if err != nil {
return nil
}
return encoded
}
// toolStatus maps the verified tool event types onto the ToolState lifecycle.
func toolStatus(eventType string) (string, bool) {
switch eventType {
+145
View File
@@ -2,6 +2,8 @@ package opencode
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"path/filepath"
@@ -121,6 +123,149 @@ func TestChatStreamRealV2Shapes(t *testing.T) {
}
}
// TestChatDeltaPartIDsNeverCollide pins the broker's part-id namespace: OpenCode
// numbers one message's text and reasoning parts independently from 0, so a
// message's first text delta and its first reasoning delta share an ordinal. The
// two must still land on distinct part ids — otherwise the client appends the
// answer into the thinking block and the server's retained state re-collides on a
// resume. The id is the shared ChatDeltaPartID derivation, so it is deterministic
// across a reconnect.
func TestChatDeltaPartIDsNeverCollide(t *testing.T) {
payloads := map[string]string{
"text": `{"type":"session.text.delta","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","ordinal":0,"delta":"answer"}}`,
"reasoning": `{"type":"session.reasoning.delta","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","ordinal":0,"delta":"think"}}`,
}
ids := map[string]string{}
for kind, payload := range payloads {
last := ""
frame, ok, err := NormalizeChatEvent([]byte(payload), "ses_1", &last)
if err != nil || !ok {
t.Fatalf("%s delta: ok=%v err=%v", kind, ok, err)
}
if frame.Part == nil {
t.Fatalf("%s delta carried no part", kind)
}
if frame.Part.Type != kind {
t.Fatalf("%s delta part type = %q", kind, frame.Part.Type)
}
ids[kind] = frame.Part.ID
}
if ids["text"] == ids["reasoning"] {
t.Fatalf("text and reasoning share part id %q", ids["text"])
}
if ids["text"] != protocol.ChatDeltaPartID("msg_1", protocol.ChatPartText, 0) {
t.Fatalf("text part id = %q, want the stable derivation", ids["text"])
}
if ids["reasoning"] != protocol.ChatDeltaPartID("msg_1", protocol.ChatPartReasoning, 0) {
t.Fatalf("reasoning part id = %q, want the stable derivation", ids["reasoning"])
}
}
// TestChatToolLifecycleCarriesNameAndInput pins what the broker learns from each
// verified tool frame: input.started carries the name, input.ended carries the
// arguments as its `text` string, called carries the parsed input object, and
// progress/success carry neither — so those frames must surface no name and no
// input, leaving the retention to the consumer instead of blanking the field.
func TestChatToolLifecycleCarriesNameAndInput(t *testing.T) {
frames := []struct {
name string
payload string
wantStatus string
wantName string
wantInput string // the JSON encoding expected, "" for absent
}{
{
name: "input.started names the call",
payload: `{"type":"session.tool.input.started","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","name":"shell"}}`,
wantStatus: protocol.ChatToolStreaming,
wantName: "shell",
},
{
name: "input.ended carries the arguments as text",
payload: `{"type":"session.tool.input.ended","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","text":"{\"command\":\"ls\"}"}}`,
wantStatus: protocol.ChatToolStreaming,
wantInput: `{"command":"ls"}`,
},
{
name: "called carries the parsed input object",
payload: `{"type":"session.tool.called","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","input":{"command":"ls"}}}`,
wantStatus: protocol.ChatToolRunning,
wantInput: `{"command":"ls"}`,
},
{
name: "progress carries metadata only",
payload: `{"type":"session.tool.progress","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","metadata":{"exit":0}}}`,
wantStatus: protocol.ChatToolRunning,
},
{
name: "success carries the output and neither name nor input",
payload: `{"type":"session.tool.success","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","content":[{"type":"text","text":"ok"}],"metadata":{"exit":0},"executed":true}}`,
wantStatus: protocol.ChatToolCompleted,
},
}
for _, tc := range frames {
t.Run(tc.name, func(t *testing.T) {
last := ""
frame, ok, err := NormalizeChatEvent([]byte(tc.payload), "ses_1", &last)
if err != nil || !ok {
t.Fatalf("ok=%v err=%v", ok, err)
}
if frame.Part == nil || frame.Part.State == nil {
t.Fatalf("tool frame = %+v", frame)
}
if frame.Part.State.Status != tc.wantStatus {
t.Fatalf("status = %q, want %q", frame.Part.State.Status, tc.wantStatus)
}
if frame.Part.Name != tc.wantName {
t.Fatalf("name = %q, want %q", frame.Part.Name, tc.wantName)
}
if tc.wantInput == "" {
if len(frame.Part.State.Input) != 0 {
t.Fatalf("input = %s, want absent", frame.Part.State.Input)
}
return
}
if !json.Valid(frame.Part.State.Input) {
t.Fatalf("input %s is not valid JSON", frame.Part.State.Input)
}
var want, got any
if err := json.Unmarshal([]byte(tc.wantInput), &want); err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(frame.Part.State.Input, &got); err != nil {
t.Fatalf("input %s: %v", frame.Part.State.Input, err)
}
if fmt.Sprintf("%v", want) != fmt.Sprintf("%v", got) {
t.Fatalf("input = %s, want %s", frame.Part.State.Input, tc.wantInput)
}
})
}
}
// TestChatToolInputFromPartialText pins the streaming case: input.ended's text
// that is not yet a complete JSON value must still surface, carried as a JSON
// string so the frame stays valid and the client can show the partial arguments.
func TestChatToolInputFromPartialText(t *testing.T) {
last := ""
frame, ok, err := NormalizeChatEvent([]byte(`{"type":"session.tool.input.ended","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","text":"{\"command\":\"l"}}`), "ses_1", &last)
if err != nil || !ok {
t.Fatalf("ok=%v err=%v", ok, err)
}
if frame.Part == nil || frame.Part.State == nil {
t.Fatalf("frame = %+v", frame)
}
if !json.Valid(frame.Part.State.Input) {
t.Fatalf("partial input %s is not valid JSON", frame.Part.State.Input)
}
var got string
if err := json.Unmarshal(frame.Part.State.Input, &got); err != nil {
t.Fatalf("partial input is not a JSON string: %s (%v)", frame.Part.State.Input, err)
}
if got != `{"command":"l` {
t.Fatalf("partial input text = %q", got)
}
}
// TestOpenStreamRejectsUnknownBackend pins that the shared construction path
// validates the backend before any process is started, so OpenStream never
// launches a serve for an unknown backend.
+22 -2
View File
@@ -1,6 +1,9 @@
package protocol
import "encoding/json"
import (
"encoding/json"
"fmt"
)
// Chat requests and responses are brokered by the Pipelines server (route B in
// the OpenCode chat research): the phone keeps talking only to /connect and the
@@ -213,8 +216,21 @@ type ChatMessage struct {
Parts []ChatPart `json:"parts"`
}
// ChatDeltaPartID is the stable part id an incremental text or reasoning delta
// stream addresses. OpenCode numbers one message's text and reasoning parts
// independently from 0, so the kind is part of the id: for a single message its
// first text delta and its first reasoning delta are distinct ids and a delta can
// never be appended into the other kind's part. The derivation is deterministic,
// so a reconnect or a resume re-addresses the same part, and it is mirrored in
// the client's ChatProtocol.deltaPartId (data/protocol/Chat.kt).
func ChatDeltaPartID(messageID, kind string, ordinal int) string {
return fmt.Sprintf("%s#%s#%d", messageID, kind, ordinal)
}
// ChatPart is one element of a ChatMessage. Type selects the meaning: text and
// reasoning use Text and ID, tool uses ID, Name, Executed, State and Time.
// reasoning use Text and ID, tool uses ID, Name, Executed, State and Time. A text
// or reasoning part's ID is ChatDeltaPartID(message id, kind, ordinal), so the two
// kinds never share an id within a message.
type ChatPart struct {
Type string `json:"type"`
ID string `json:"id,omitempty"`
@@ -227,6 +243,10 @@ type ChatPart struct {
// ChatToolState is the lifecycle of a tool call. Input is a raw JSON value: a
// partial-JSON string while streaming, an object once running or completed.
// OpenCode spreads one call across several frames (name in input.started, the
// arguments in input.ended's text or called's input, then progress/success
// carrying neither), so Input is absent on the frames that do not carry it and
// every consumer retains the value it already learned instead of blanking it.
// Content is the tool's textual/file result, Metadata its provider stats,
// Error the structured failure for status "error".
type ChatToolState struct {
+68 -13
View File
@@ -108,10 +108,12 @@ func (h *chatHub) clock() time.Time {
}
// apply folds one pushed frame into the retained state. Text and reasoning
// deltas append to their message's part; a tool frame upserts the full part
// (accounting for its whole size) so the latest lifecycle status, input,
// content, metadata, error and time are retained under the frame's message id.
// Step and usage frames update the session's latest progress.
// deltas append to their message's part; a tool frame upserts the part (merging
// the fields it carries onto what is retained, and accounting for the whole
// size) so the latest lifecycle status, name, input, content, metadata, error and
// time are retained under the frame's message id and a later frame that omits a
// field never blanks it. Step and usage frames update the session's latest
// progress.
func (h *chatHub) apply(frame protocol.ChatEventFrame) {
if frame.SessionID == "" {
return
@@ -181,29 +183,82 @@ func (h *chatHub) apply(frame protocol.ChatEventFrame) {
if frame.Part == nil || frame.Part.ID == "" || turn.overflow {
return
}
// The tool part is retained whole, so size the complete part (input,
// content, metadata, error and time) and charge the difference against
// the cap on every upsert, not just the first-seen name.
incoming := chatPartBytes(frame.Part)
// The tool part is retained whole, so size the complete, merged part
// (input, content, metadata, error and time) and charge the difference
// against the cap on every upsert, not just the first-seen name.
part, ok := message.parts[frame.Part.ID]
if !ok {
if !turn.retain(incoming) {
merged := mergeChatToolPart(nil, frame.Part)
if !turn.retain(chatPartBytes(&merged)) {
return
}
part = &chatPart{}
message.order = append(message.order, frame.Part.ID)
message.parts[frame.Part.ID] = part
} else if previous := chatPartBytes(&part.part); incoming > previous {
message.parts[frame.Part.ID] = &chatPart{part: merged}
return
}
merged := mergeChatToolPart(&part.part, frame.Part)
incoming := chatPartBytes(&merged)
previous := chatPartBytes(&part.part)
if incoming > previous {
if !turn.retain(incoming - previous) {
return
}
} else {
turn.shrink(previous - incoming)
}
part.part = *frame.Part
part.part = merged
}
}
// mergeChatToolPart folds one tool frame onto the part retained from the frames
// before it. A tool call is reported across several frames that each carry only
// part of it — input.started the name, input.ended the arguments as text, called
// the parsed input, progress metadata, and success the output with no name and no
// input — so a field the broker already learned is never blanked by a later frame
// that omits it. A non-empty incoming field wins; otherwise the retained value is
// kept.
func mergeChatToolPart(previous, incoming *protocol.ChatPart) protocol.ChatPart {
if previous == nil {
return *incoming
}
merged := *incoming
if merged.Type == "" {
merged.Type = previous.Type
}
if merged.Name == "" {
merged.Name = previous.Name
}
merged.Executed = merged.Executed || previous.Executed
if merged.Time == nil {
merged.Time = previous.Time
}
switch {
case incoming.State == nil:
merged.State = previous.State
case previous.State == nil:
// The incoming state stands on its own; merged already carries it.
default:
state := *incoming.State
if state.Status == "" {
state.Status = previous.State.Status
}
if len(state.Input) == 0 {
state.Input = previous.State.Input
}
if len(state.Content) == 0 {
state.Content = previous.State.Content
}
if state.Metadata == nil {
state.Metadata = previous.State.Metadata
}
if state.Error == nil {
state.Error = previous.State.Error
}
merged.State = &state
}
return merged
}
// chatPartBytes estimates the retained size of a part so the per-session cap
// accounts for its whole state instead of only the first-seen name. It walks the
// fields rather than re-marshalling, so the cost matches the bytes it stores.
+47 -3
View File
@@ -267,8 +267,8 @@ func chatFrame(kind, delta string, part *protocol.ChatPart, step *protocol.ChatS
func scriptedChat() *fakeChat {
return &fakeChat{script: []protocol.ChatEventFrame{
chatFrame(protocol.ChatEventText, "hello ", &protocol.ChatPart{Type: protocol.ChatPartText, ID: "msg_1#0"}, nil),
chatFrame(protocol.ChatEventText, "world", &protocol.ChatPart{Type: protocol.ChatPartText, ID: "msg_1#0"}, nil),
chatFrame(protocol.ChatEventText, "hello ", &protocol.ChatPart{Type: protocol.ChatPartText, ID: protocol.ChatDeltaPartID("msg_1", protocol.ChatPartText, 0)}, nil),
chatFrame(protocol.ChatEventText, "world", &protocol.ChatPart{Type: protocol.ChatPartText, ID: protocol.ChatDeltaPartID("msg_1", protocol.ChatPartText, 0)}, nil),
chatFrame(protocol.ChatEventTool, "", &protocol.ChatPart{Type: protocol.ChatPartTool, ID: "call_1", Name: "shell", State: &protocol.ChatToolState{Status: protocol.ChatToolCompleted, Input: []byte(`{"command":"ls"}`)}}, nil),
chatFrame(protocol.ChatEventStep, "", nil, &protocol.ChatStep{Status: protocol.ChatStepEnded, Index: 1}),
}}
@@ -451,7 +451,7 @@ func TestChatMessagesResumesRetainedState(t *testing.T) {
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 != "msg_1#0" || message.Parts[0].Text != "hello world" {
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 {
@@ -895,6 +895,50 @@ func TestChatHubChargesToolStateBytes(t *testing.T) {
}
}
// TestChatHubRetainsToolNameAndInputAcrossLifecycle pins that a tool call keeps
// its name and its input for its whole lifecycle in the retained state a resume
// serves: the name arrives only in input.started, the arguments in input.ended's
// text or called's input, and then progress/success carry neither, so no later
// frame may blank a field the hub already learned.
func TestChatHubRetainsToolNameAndInputAcrossLifecycle(t *testing.T) {
hub := newChatHub()
apply := func(part *protocol.ChatPart) {
hub.apply(protocol.ChatEventFrame{SessionID: "ses_1", MessageID: "msg_1", Kind: protocol.ChatEventTool, Part: part})
}
// input.started: the name, no arguments yet.
apply(&protocol.ChatPart{Type: protocol.ChatPartTool, ID: "call_1", Name: "shell", State: &protocol.ChatToolState{Status: protocol.ChatToolStreaming}})
// input.ended/called: the arguments, no name.
apply(&protocol.ChatPart{Type: protocol.ChatPartTool, ID: "call_1", State: &protocol.ChatToolState{Status: protocol.ChatToolRunning, Input: json.RawMessage(`{"command":"ls"}`)}})
// progress: metadata only.
apply(&protocol.ChatPart{Type: protocol.ChatPartTool, ID: "call_1", State: &protocol.ChatToolState{Status: protocol.ChatToolRunning, Metadata: map[string]any{"exit": 0}}})
// success: the output, no name and no input.
apply(&protocol.ChatPart{Type: protocol.ChatPartTool, ID: "call_1", State: &protocol.ChatToolState{Status: protocol.ChatToolCompleted, Content: []protocol.ChatToolContent{{Type: "text", Text: "ok"}}}})
messages := hub.snapshot("ses_1")
if len(messages) != 1 || len(messages[0].Parts) != 1 {
t.Fatalf("snapshot = %+v", messages)
}
part := messages[0].Parts[0]
if part.Name != "shell" {
t.Fatalf("tool name was blanked by a later frame: %+v", part)
}
if part.State == nil || part.State.Status != protocol.ChatToolCompleted {
t.Fatalf("tool state = %+v", part.State)
}
var input struct {
Command string `json:"command"`
}
if err := json.Unmarshal(part.State.Input, &input); err != nil || input.Command != "ls" {
t.Fatalf("tool input was blanked by a later frame: %s (err %v)", part.State.Input, err)
}
if len(part.State.Content) != 1 || part.State.Content[0].Text != "ok" {
t.Fatalf("tool content = %+v", part.State.Content)
}
if part.State.Metadata["exit"] == nil {
t.Fatalf("tool metadata was blanked by a later frame: %+v", part.State.Metadata)
}
}
// TestChatPromptSubscribesBeforePrompt pins the subscribe-before-prompt order:
// the firehose has no replay cursor, so the subscription must be attached before
// the prompt is posted or deltas emitted in between are lost.