Merge main into session-html-only: combine the html-only session export with chat-tool-group and chat-activity

This commit is contained in:
2026-10-18 10:06:54 +01:00
18 changed files with 1471 additions and 158 deletions
+92
View File
@@ -21,6 +21,11 @@ const chatRetainedLimit = opencode.MaxResponse
// least-recently-updated turn is evicted once the cap is exceeded.
const chatMaxTurns = 128
// chatShellLimit bounds how many running shells one session retains for replay
// to a client that reattaches, so a runaway turn cannot grow the retained state
// without limit. It matches the approval-replay bound.
const chatShellLimit = 16
// chatHub retains the accumulated turn state per session, independently of any
// single connection, so a reconnecting client can re-sync what it missed. State
// is keyed by session and then by message id (a session holds many turns), and
@@ -77,6 +82,14 @@ type chatTurn struct {
// chatPermissionRetainedLimit like the dedup set above.
resolutions map[string]*chatResolution
resolutionOrder []string
// shells holds the session's still-running shell commands, keyed by the
// shell id the stream gave them, so a client that reattaches can be re-synced
// the background processes it cannot have seen. shellOrder preserves start
// order and bounds the retained set to chatShellLimit; a turn that has
// finished retains nothing here, so a background process surviving the turn
// is never mistaken for the turn still running.
shells map[string]protocol.ChatShell
shellOrder []string
}
// chatResolution is the shared answer to one permission request. answered is
@@ -128,6 +141,11 @@ func (h *chatHub) apply(frame protocol.ChatEventFrame) {
case protocol.ChatEventUsage:
turn.usage = frame.Usage
return
case protocol.ChatEventShell:
// A shell frame carries no message id (it outlives the tool call that
// started it), so it is folded before the message-id gate below.
turn.applyShell(frame.Shell)
return
}
if frame.MessageID == "" {
return
@@ -210,6 +228,58 @@ func (h *chatHub) apply(frame protocol.ChatEventFrame) {
}
}
// applyShell folds one shell frame into the session's retained shells. A running
// shell is upserted (keyed by its id) so a client that reattaches can be
// re-synced the processes still running; an exited shell is dropped, which is
// what clears the indicator. The retained set is bounded both by count
// (chatShellLimit) and by the same per-session byte cap the other retained state
// uses, so a long turn with many shells cannot grow it without limit. A shell
// exit for a shell the session never retained is ignored, so a frame that is not
// this session's cannot clear another session's process.
func (t *chatTurn) applyShell(shell *protocol.ChatShell) {
if shell == nil || shell.ID == "" {
return
}
if shell.Status == protocol.ChatShellExited {
old, ok := t.shells[shell.ID]
if !ok {
return
}
t.shellOrder = removeShellID(t.shellOrder, shell.ID)
delete(t.shells, shell.ID)
t.shrink(len(old.Command))
return
}
if t.shells == nil {
t.shells = map[string]protocol.ChatShell{}
}
if _, ok := t.shells[shell.ID]; !ok {
if !t.retain(len(shell.Command)) {
return
}
t.shellOrder = append(t.shellOrder, shell.ID)
}
t.shells[shell.ID] = *shell
for len(t.shellOrder) > chatShellLimit {
id := t.shellOrder[0]
t.shellOrder = t.shellOrder[1:]
if dropped, ok := t.shells[id]; ok {
delete(t.shells, id)
t.shrink(len(dropped.Command))
}
}
}
// removeShellID drops id from order, preserving the order of the rest.
func removeShellID(order []string, id string) []string {
for i, existing := range order {
if existing == id {
return append(order[:i:i], order[i+1:]...)
}
}
return order
}
// 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
@@ -379,3 +449,25 @@ func (h *chatHub) snapshot(sessionID string) []protocol.ChatMessage {
}
return messages
}
// shellSnapshot returns the session's still-running shells, in the order they
// started, so a client attaching to the session's stream can be re-synced the
// background processes it cannot have seen. It is the shell half of the resume
// path — the message snapshot cannot carry a shell, because a shell is not a
// turn — and returns nil when the session has none running. A shell that has
// exited is not retained, so a stale process is never replayed.
func (h *chatHub) shellSnapshot(sessionID string) []protocol.ChatShell {
h.mu.Lock()
defer h.mu.Unlock()
turn := h.turns[sessionID]
if turn == nil || len(turn.shells) == 0 {
return nil
}
shells := make([]protocol.ChatShell, 0, len(turn.shells))
for _, id := range turn.shellOrder {
if shell, ok := turn.shells[id]; ok {
shells = append(shells, shell)
}
}
return shells
}
+10
View File
@@ -119,4 +119,14 @@ func handleChatSubscribe(request protocol.Message, broker chatBroker, hub *chatH
frame := pending
sub.send(protocol.Message{Type: protocol.ChatPermission, ChatPermission: &frame})
}
// Replay the background processes still running, so a device that attaches
// after they started (a reconnect, or a chat opened from another device)
// shows them rather than nothing until they exit. The message snapshot cannot
// carry a shell — a shell outlives the turn and is not a turn — so this is the
// shell half of the resume.
for _, shell := range hub.shellSnapshot(query.SessionID) {
running := shell
frame := protocol.ChatEventFrame{SessionID: query.SessionID, Kind: protocol.ChatEventShell, Shell: &running}
sub.send(protocol.Message{Type: protocol.ChatEvent, ChatEvent: &frame})
}
}
+100
View File
@@ -939,6 +939,106 @@ func TestChatHubRetainsToolNameAndInputAcrossLifecycle(t *testing.T) {
}
}
// TestChatHubRetainsShellOutlivingTurn pins the background-process state: a shell
// the agent started survives the turn that started it, so a client re-syncing the
// session still sees the process running, and its exit clears it. An exit for a
// shell the session never retained (another session's) clears nothing.
func TestChatHubRetainsShellOutlivingTurn(t *testing.T) {
hub := newChatHub()
hub.apply(protocol.ChatEventFrame{SessionID: "ses_1", Kind: protocol.ChatEventShell, Shell: &protocol.ChatShell{ID: "sh_1", Status: protocol.ChatShellRunning, Command: "npm run dev"}})
// The turn that started the shell finishes while the shell keeps running.
hub.apply(protocol.ChatEventFrame{SessionID: "ses_1", MessageID: "msg_1", Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded}})
shells := hub.shellSnapshot("ses_1")
if len(shells) != 1 || shells[0].ID != "sh_1" || shells[0].Command != "npm run dev" || shells[0].Status != protocol.ChatShellRunning {
t.Fatalf("running shell not retained past the turn: %+v", shells)
}
// An exit for a shell this session never started is not this session's.
hub.apply(protocol.ChatEventFrame{SessionID: "ses_1", Kind: protocol.ChatEventShell, Shell: &protocol.ChatShell{ID: "sh_other", Status: protocol.ChatShellExited}})
if shells := hub.shellSnapshot("ses_1"); len(shells) != 1 {
t.Fatalf("foreign exit cleared the running shell: %+v", shells)
}
exit := 0
hub.apply(protocol.ChatEventFrame{SessionID: "ses_1", Kind: protocol.ChatEventShell, Shell: &protocol.ChatShell{ID: "sh_1", Status: protocol.ChatShellExited, Exit: &exit}})
if shells := hub.shellSnapshot("ses_1"); shells != nil {
t.Fatalf("exited shell still retained: %+v", shells)
}
}
// TestChatPromptStreamsShellFrame pins that a shell frame reaches the watching
// client like every other frame kind, carrying its payload and no message id.
func TestChatPromptStreamsShellFrame(t *testing.T) {
broker := scriptedChat()
broker.script = append([]protocol.ChatEventFrame{
{SessionID: "ses_1", Kind: protocol.ChatEventShell, Shell: &protocol.ChatShell{ID: "sh_1", Status: protocol.ChatShellRunning, Command: "sleep 120"}},
}, broker.script...)
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)
}
var ack protocol.Message
if err := wsjson.Read(ctx, conn, &ack); err != nil {
t.Fatal(err)
}
frames := readChatTurn(t, ctx, conn)
if len(frames) == 0 || frames[0].Kind != protocol.ChatEventShell {
t.Fatalf("first frame = %+v, want the shell frame", frames)
}
shell := frames[0]
if shell.MessageID != "" || shell.Shell == nil || shell.Shell.ID != "sh_1" || shell.Shell.Command != "sleep 120" || shell.Shell.Status != protocol.ChatShellRunning {
t.Fatalf("shell frame = %+v", shell)
}
}
// TestChatSubscribeReplaysRunningShell pins the shell half of the resume: a
// client attaching to a session's stream after a shell started is replayed the
// still-running shell, so a reconnect (or a chat opened from another device)
// shows the background process instead of nothing until it exits.
func TestChatSubscribeReplaysRunningShell(t *testing.T) {
broker := scriptedChat()
broker.script = append([]protocol.ChatEventFrame{
{SessionID: "ses_1", Kind: protocol.ChatEventShell, Shell: &protocol.ChatShell{ID: "sh_1", Status: protocol.ChatShellRunning, Command: "sleep 120"}},
}, broker.script...)
ctx, _, conns := dialChatConns(t, Options{Chat: broker}, 2)
if err := wsjson.Write(ctx, conns[0], protocol.Message{Type: protocol.ChatPrompt, ID: "r1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "hi"}}); err != nil {
t.Fatal(err)
}
var ack protocol.Message
if err := wsjson.Read(ctx, conns[0], &ack); err != nil {
t.Fatal(err)
}
frames := readChatTurn(t, ctx, conns[0])
sawShell := false
for _, frame := range frames {
if frame.Kind == protocol.ChatEventShell && frame.Shell != nil && frame.Shell.ID == "sh_1" {
sawShell = true
}
}
if !sawShell {
t.Fatalf("shell frame not fanned out: %+v", frames)
}
// The second client attaches only now; the running shell must be replayed.
if err := wsjson.Write(ctx, conns[1], protocol.Message{Type: protocol.ChatSubscribe, ID: "sub1", ChatSubscribe: &protocol.ChatQuery{SessionID: "ses_1"}}); err != nil {
t.Fatal(err)
}
var subscription protocol.Message
if err := wsjson.Read(ctx, conns[1], &subscription); err != nil {
t.Fatal(err)
}
if subscription.Type != protocol.ChatSubscribe || subscription.ID != "sub1" {
t.Fatalf("subscribe ack = %+v", subscription)
}
var replayed protocol.Message
if err := wsjson.Read(ctx, conns[1], &replayed); err != nil {
t.Fatal(err)
}
if replayed.Type != protocol.ChatEvent || replayed.ChatEvent == nil || replayed.ChatEvent.Kind != protocol.ChatEventShell {
t.Fatalf("replay frame = %+v", replayed)
}
if replayed.ChatEvent.Shell == nil || replayed.ChatEvent.Shell.ID != "sh_1" || replayed.ChatEvent.Shell.Status != protocol.ChatShellRunning {
t.Fatalf("replayed shell = %+v", replayed.ChatEvent.Shell)
}
}
// 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.