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
+142 -8
View File
@@ -325,7 +325,7 @@ func (c *Chat) Stream(ctx context.Context, id string, ready func(), emit func(pr
func scanChatEvents(ctx context.Context, body io.Reader, sessionID string, emit func(protocol.ChatEventFrame) error) error {
scanner := bufio.NewScanner(body)
scanner.Buffer(make([]byte, 0, 64<<10), MaxResponse)
lastMessageID := ""
state := &ChatStreamState{}
for scanner.Scan() {
if err := ctx.Err(); err != nil {
return err
@@ -338,7 +338,7 @@ func scanChatEvents(ctx context.Context, body io.Reader, sessionID string, emit
if len(payload) == 0 {
continue
}
frame, ok, err := NormalizeChatEvent(payload, sessionID, &lastMessageID)
frame, ok, err := NormalizeChatEvent(payload, sessionID, state)
if err != nil {
return err
}
@@ -346,7 +346,7 @@ func scanChatEvents(ctx context.Context, body io.Reader, sessionID string, emit
continue
}
if frame.MessageID != "" {
lastMessageID = frame.MessageID
state.LastMessageID = frame.MessageID
}
if err := emit(frame); err != nil {
return err
@@ -372,7 +372,8 @@ func isChatEventType(t string) bool {
"session.usage.updated",
"session.tool.input.started", "session.tool.input.ended",
"session.tool.called", "session.tool.progress",
"session.tool.success", "session.tool.failed":
"session.tool.success", "session.tool.failed",
"shell.created", "shell.exited":
return true
}
return false
@@ -403,13 +404,92 @@ type chatEvent struct {
Input int `json:"input"`
Output int `json:"output"`
} `json:"tokens"`
// The shell family. shell.created nests the started command under info;
// shell.exited carries the id, the final status and the exit code at the top
// level. Exit is a pointer so an exit of 0 is distinguishable from absent.
Status string `json:"status"`
Exit *int `json:"exit"`
Info *chatShellInfo `json:"info"`
}
// chatShellInfo is the Shell.Info envelope a shell.created event nests under
// "info". The session that started the shell is reported in metadata.sessionID
// (the agent's shell tool sets it); only the fields the protocol surfaces are
// read, and the shell's cwd/file are deliberately not carried so no path or
// credential leaks into a frame.
type chatShellInfo struct {
ID string `json:"id"`
Status string `json:"status"`
Command string `json:"command"`
Metadata map[string]any `json:"metadata"`
}
// shellSessionID reads the session that started a shell from the created event's
// metadata, or "" when the event carries none. The agent's shell tool sets
// metadata.sessionID; a shell started by anything else is unattributed and is
// ignored, so a shell that cannot be shown to belong to this session never
// becomes one of its frames.
func (i *chatShellInfo) shellSessionID() string {
if i == nil || i.Metadata == nil {
return ""
}
id, _ := i.Metadata["sessionID"].(string)
return id
}
// ChatStreamState is the per-session memory one event stream folds across its
// frames, so a frame that omits context can still be attributed. It is the
// single state NormalizeChatEvent reads and writes; a caller that maps a lone
// event passes an empty state (or nil when it needs no attribution).
//
// - LastMessageID is the most recent assistant message id, so a later tool or
// step event that omits it still attributes every frame to a stable message.
// - Shells holds the ids of the shells this session has started and not yet
// seen exit. shell.exited carries no session id, so the id remembered here is
// what attributes the exit to its session: another session's shell is an
// unknown id and is ignored.
type ChatStreamState struct {
LastMessageID string
Shells map[string]struct{}
}
// knowsShell reports whether id is a shell this stream has already surfaced for
// the session, and so is one this stream may now report the exit of.
func (s *ChatStreamState) knowsShell(id string) bool {
if s == nil || id == "" {
return false
}
_, ok := s.Shells[id]
return ok
}
// markShell remembers id as a shell of this session.
func (s *ChatStreamState) markShell(id string) {
if s == nil || id == "" {
return
}
if s.Shells == nil {
s.Shells = map[string]struct{}{}
}
s.Shells[id] = struct{}{}
}
// forgetShell drops id once its exit has been surfaced.
func (s *ChatStreamState) forgetShell(id string) {
if s == nil || s.Shells == nil {
return
}
delete(s.Shells, id)
}
// NormalizeChatEvent maps one OpenCode firehose `data:` payload to a
// protocol.ChatEventFrame. ok is false for an event type this adapter does not
// protocol.ChatEventFrame, folding the small per-session context ([stream]) it
// needs across frames. ok is false for an event type this adapter does not
// surface or one belonging to a different session, so the caller can skip it
// without emitting a frame. Malformed JSON returns an error and never a frame.
func NormalizeChatEvent(payload []byte, sessionID string, lastMessageID *string) (protocol.ChatEventFrame, bool, error) {
// stream may be nil when the caller maps a lone event and needs no attribution;
// a nil stream simply means no inherited message id and no remembered shell.
func NormalizeChatEvent(payload []byte, sessionID string, stream *ChatStreamState) (protocol.ChatEventFrame, bool, error) {
var outer struct {
Type string `json:"type"`
Data json.RawMessage `json:"data"`
@@ -432,6 +512,14 @@ func NormalizeChatEvent(payload []byte, sessionID string, lastMessageID *string)
if err := json.Unmarshal(outer.Data, &data); err != nil {
return protocol.ChatEventFrame{}, false, fmt.Errorf("malformed OpenCode event data: %w", err)
}
// The shell events are location-scoped, not session-scoped: the session that
// started a shell is reported in the created event's metadata, and the exit
// names no session at all, so it is attributed through the ids this stream
// already surfaced. They are mapped before the session check the other events
// use, which their absent data.sessionID could never pass.
if outer.Type == "shell.created" || outer.Type == "shell.exited" {
return normalizeShellEvent(outer.Type, data, sessionID, stream)
}
if data.SessionID != sessionID {
return protocol.ChatEventFrame{}, false, nil
}
@@ -439,8 +527,8 @@ func NormalizeChatEvent(payload []byte, sessionID string, lastMessageID *string)
if messageID == "" {
messageID = data.MessageIDAlt
}
if messageID == "" && lastMessageID != nil {
messageID = *lastMessageID
if messageID == "" && stream != nil {
messageID = stream.LastMessageID
}
// Every pushed frame must carry a stable message id so the client can
// attribute it; without one this is not a chat frame.
@@ -481,6 +569,52 @@ func NormalizeChatEvent(payload []byte, sessionID string, lastMessageID *string)
return frame, true, nil
}
// normalizeShellEvent maps one shell.created/shell.exited payload onto a
// protocol.ChatEventShell frame. A shell belongs to the session whose id the
// created event carries in info.metadata.sessionID, so a shell started by any
// other session is ignored (ok=false) rather than shown in this session. The
// exit event carries no session at all, so it is emitted only for a shell id
// this stream already surfaced for the session; any other id is another
// session's and is ignored. An event that carries no shell id at all cannot be
// keyed by the client and is malformed, so it fails the stream instead of
// producing a frame the client cannot attribute.
func normalizeShellEvent(eventType string, data chatEvent, sessionID string, stream *ChatStreamState) (protocol.ChatEventFrame, bool, error) {
if eventType == "shell.created" {
if data.Info == nil || data.Info.ID == "" {
return protocol.ChatEventFrame{}, false, fmt.Errorf("malformed OpenCode shell.created event")
}
if data.Info.shellSessionID() != sessionID {
return protocol.ChatEventFrame{}, false, nil
}
status := data.Info.Status
if status == "" {
status = protocol.ChatShellRunning
}
stream.markShell(data.Info.ID)
return protocol.ChatEventFrame{
SessionID: sessionID,
Kind: protocol.ChatEventShell,
Shell: &protocol.ChatShell{ID: data.Info.ID, Status: status, Command: data.Info.Command},
}, true, nil
}
if data.ID == "" {
return protocol.ChatEventFrame{}, false, fmt.Errorf("malformed OpenCode shell.exited event")
}
if !stream.knowsShell(data.ID) {
return protocol.ChatEventFrame{}, false, nil
}
stream.forgetShell(data.ID)
status := data.Status
if status == "" {
status = protocol.ChatShellExited
}
return protocol.ChatEventFrame{
SessionID: sessionID,
Kind: protocol.ChatEventShell,
Shell: &protocol.ChatShell{ID: data.ID, Status: status, Exit: data.Exit},
}, 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
+4 -5
View File
@@ -17,11 +17,11 @@ import (
)
func TestNormalizeChatEvent(t *testing.T) {
last := "msg_prev"
prior := &ChatStreamState{LastMessageID: "msg_prev"}
cases := []struct {
name string
payload string
last *string
state *ChatStreamState
wantOK bool
wantKind string
wantMsg string
@@ -38,14 +38,13 @@ func TestNormalizeChatEvent(t *testing.T) {
{name: "tool completed", payload: `{"type":"session.tool.success","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","name":"shell","content":[{"type":"text","text":"ok"}]}}`, wantOK: true, wantKind: protocol.ChatEventTool, wantStatus: protocol.ChatToolCompleted},
{name: "tool failed", payload: `{"type":"session.tool.failed","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","name":"shell","error":{"type":"tool","message":"boom"}}}`, wantOK: true, wantKind: protocol.ChatEventTool, wantStatus: protocol.ChatToolError},
{name: "usage", payload: `{"type":"session.usage.updated","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","tokens":{"input":12,"output":34},"cost":0.001}}`, wantOK: true, wantKind: protocol.ChatEventUsage, wantUsage: &protocol.ChatUsage{InputTokens: 12, OutputTokens: 34, Cost: 0.001}},
{name: "message id fallback", payload: `{"type":"session.step.started","data":{"sessionID":"ses_1","index":1}}`, last: &last, wantOK: true, wantKind: protocol.ChatEventStep, wantMsg: "msg_prev", wantStep: protocol.ChatStepStarted},
{name: "message id fallback", payload: `{"type":"session.step.started","data":{"sessionID":"ses_1","index":1}}`, state: prior, wantOK: true, wantKind: protocol.ChatEventStep, wantMsg: "msg_prev", wantStep: protocol.ChatStepStarted},
{name: "foreign session skipped", payload: `{"type":"session.text.delta","data":{"sessionID":"ses_other","delta":"x"}}`, wantOK: false},
{name: "unknown type skipped", payload: `{"type":"session.created","data":{"sessionID":"ses_1"}}`, wantOK: false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
lastID := tc.last
frame, ok, err := NormalizeChatEvent([]byte(tc.payload), "ses_1", lastID)
frame, ok, err := NormalizeChatEvent([]byte(tc.payload), "ses_1", tc.state)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
+118 -6
View File
@@ -137,8 +137,7 @@ func TestChatDeltaPartIDsNeverCollide(t *testing.T) {
}
ids := map[string]string{}
for kind, payload := range payloads {
last := ""
frame, ok, err := NormalizeChatEvent([]byte(payload), "ses_1", &last)
frame, ok, err := NormalizeChatEvent([]byte(payload), "ses_1", &ChatStreamState{})
if err != nil || !ok {
t.Fatalf("%s delta: ok=%v err=%v", kind, ok, err)
}
@@ -205,8 +204,7 @@ func TestChatToolLifecycleCarriesNameAndInput(t *testing.T) {
}
for _, tc := range frames {
t.Run(tc.name, func(t *testing.T) {
last := ""
frame, ok, err := NormalizeChatEvent([]byte(tc.payload), "ses_1", &last)
frame, ok, err := NormalizeChatEvent([]byte(tc.payload), "ses_1", &ChatStreamState{})
if err != nil || !ok {
t.Fatalf("ok=%v err=%v", ok, err)
}
@@ -246,8 +244,7 @@ func TestChatToolLifecycleCarriesNameAndInput(t *testing.T) {
// 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)
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", &ChatStreamState{})
if err != nil || !ok {
t.Fatalf("ok=%v err=%v", ok, err)
}
@@ -266,6 +263,121 @@ func TestChatToolInputFromPartialText(t *testing.T) {
}
}
// TestChatShellEventMapping pins the shell mapping: a shell.created belonging to
// the session surfaces a ChatEventShell frame carrying the shell's id, status and
// command, and the matching shell.exited carries its id, status and exit code.
// The frames carry no message id (a shell outlives the tool call that started
// it), and the exit is a pointer so an exit of 0 is not mistaken for "absent".
func TestChatShellEventMapping(t *testing.T) {
state := &ChatStreamState{}
created, ok, err := NormalizeChatEvent([]byte(`{"type":"shell.created","data":{"info":{"id":"sh_1","status":"running","command":"npm run dev","cwd":"/work","metadata":{"sessionID":"ses_1"}}}}`), "ses_1", state)
if err != nil || !ok {
t.Fatalf("shell.created: ok=%v err=%v", ok, err)
}
if created.Kind != protocol.ChatEventShell || created.Shell == nil {
t.Fatalf("created frame = %+v", created)
}
if created.MessageID != "" {
t.Fatalf("shell frame carried a message id: %q", created.MessageID)
}
if created.Shell.ID != "sh_1" || created.Shell.Status != protocol.ChatShellRunning || created.Shell.Command != "npm run dev" {
t.Fatalf("created shell = %+v", created.Shell)
}
exited, ok, err := NormalizeChatEvent([]byte(`{"type":"shell.exited","data":{"id":"sh_1","exit":0,"status":"exited"}}`), "ses_1", state)
if err != nil || !ok {
t.Fatalf("shell.exited: ok=%v err=%v", ok, err)
}
if exited.Shell == nil || exited.Shell.ID != "sh_1" || exited.Shell.Status != protocol.ChatShellExited {
t.Fatalf("exited shell = %+v", exited.Shell)
}
if exited.Shell.Exit == nil || *exited.Shell.Exit != 0 {
t.Fatalf("exit code = %v, want a present 0", exited.Shell.Exit)
}
// A second exit for the same shell is not this session's any more: the id was
// forgotten once its exit surfaced.
if _, ok, err := NormalizeChatEvent([]byte(`{"type":"shell.exited","data":{"id":"sh_1","exit":0,"status":"exited"}}`), "ses_1", state); err != nil || ok {
t.Fatalf("repeated exit produced a frame: ok=%v err=%v", ok, err)
}
}
// TestChatShellEventForeignSessionIgnored pins the session scoping the shell
// events need: they are location-scoped, so the session that started a shell is
// named only in the created event's metadata. A shell started by another session
// is ignored, and its exit — which names no session at all — is ignored too,
// because the id was never remembered for this session.
func TestChatShellEventForeignSessionIgnored(t *testing.T) {
state := &ChatStreamState{}
foreign := `{"type":"shell.created","data":{"info":{"id":"sh_foreign","status":"running","command":"sleep 120","metadata":{"sessionID":"ses_other"}}}}`
if frame, ok, err := NormalizeChatEvent([]byte(foreign), "ses_1", state); err != nil || ok {
t.Fatalf("another session's shell produced a frame: %+v ok=%v err=%v", frame, ok, err)
}
if _, ok, err := NormalizeChatEvent([]byte(`{"type":"shell.exited","data":{"id":"sh_foreign","exit":0,"status":"exited"}}`), "ses_1", state); err != nil || ok {
t.Fatalf("another session's shell exit produced a frame: ok=%v err=%v", ok, err)
}
// An unattributed shell (no metadata.sessionID) is not this session's either.
unattributed := `{"type":"shell.created","data":{"info":{"id":"sh_anon","status":"running","command":"ls","metadata":{}}}}`
if _, ok, err := NormalizeChatEvent([]byte(unattributed), "ses_1", state); err != nil || ok {
t.Fatalf("unattributed shell produced a frame: ok=%v err=%v", ok, err)
}
}
// TestChatMalformedShellEventFailsStream pins that a shell event which cannot be
// keyed (no shell id) fails the stream instead of producing a frame the client
// cannot attribute.
func TestChatMalformedShellEventFailsStream(t *testing.T) {
cases := []string{
`{"type":"shell.created","data":{"info":{"status":"running","metadata":{"sessionID":"ses_1"}}}}`,
`{"type":"shell.exited","data":{"exit":0,"status":"exited"}}`,
}
for _, payload := range cases {
if _, ok, err := NormalizeChatEvent([]byte(payload), "ses_1", &ChatStreamState{}); err == nil || ok {
t.Fatalf("malformed shell event %s: ok=%v err=%v", payload, ok, err)
}
}
}
// TestChatStreamSurfacesShellLifecycle drives the whole mapping through the SSE
// read loop: a shell started for the session surfaces as a shell frame, a shell
// started by another session is dropped, and the matching exit is surfaced only
// for the session's own shell. It pins that the shell context the exit needs is
// carried across frames by the stream's retained state, not by one payload.
func TestChatStreamSurfacesShellLifecycle(t *testing.T) {
frames := []string{
`{"id":"evt_1","type":"shell.created","data":{"info":{"id":"sh_1","status":"running","command":"npm run dev","metadata":{"sessionID":"ses_1"}}}}`,
`{"id":"evt_2","type":"shell.created","data":{"info":{"id":"sh_other","status":"running","command":"sleep 120","metadata":{"sessionID":"ses_other"}}}}`,
`{"id":"evt_3","type":"session.text.delta","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","ordinal":0,"delta":"hi"}}`,
`{"id":"evt_4","type":"shell.exited","data":{"id":"sh_other","exit":0,"status":"exited"}}`,
`{"id":"evt_5","type":"shell.exited","data":{"id":"sh_1","exit":0,"status":"exited"}}`,
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
for _, frame := range frames {
_, _ = w.Write([]byte("data: " + frame + "\n\n"))
}
}))
defer srv.Close()
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
var got []protocol.ChatEventFrame
if err := chat.Stream(ctx, "ses_1", nil, func(frame protocol.ChatEventFrame) error {
got = append(got, frame)
return nil
}); err != nil {
t.Fatalf("stream: %v", err)
}
var shellIDs []string
for _, frame := range got {
if frame.Kind == protocol.ChatEventShell && frame.Shell != nil {
shellIDs = append(shellIDs, frame.Shell.ID+":"+frame.Shell.Status)
}
}
if len(shellIDs) != 2 || shellIDs[0] != "sh_1:running" || shellIDs[1] != "sh_1:exited" {
t.Fatalf("shell frames = %v, want only this session's created and exited", shellIDs)
}
}
// 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.
+31
View File
@@ -80,6 +80,17 @@ const (
ChatEventStep = "step"
ChatEventUsage = "usage"
ChatEventUser = "user"
// ChatEventShell reports the lifecycle of one shell the agent started. It
// carries no ChatPart and no message id: a shell can outlive the tool call
// that started it, so it is a frame of its own rather than a tool part.
ChatEventShell = "shell"
)
// ChatShell.Status values, mirroring the OpenCode shell events. A running shell
// is one whose command has started and not yet exited; exited is final.
const (
ChatShellRunning = "running"
ChatShellExited = "exited"
)
// ChatStep.Status values, mirroring the OpenCode session.step events.
@@ -292,6 +303,10 @@ type ChatEventFrame struct {
Part *ChatPart `json:"part,omitempty"`
Step *ChatStep `json:"step,omitempty"`
Usage *ChatUsage `json:"usage,omitempty"`
// Shell is the payload of a ChatEventShell frame: the shell's id, its
// status, the command it is running and (once it has exited) its exit code.
// It is nil on every other kind.
Shell *ChatShell `json:"shell,omitempty"`
// Client is the sending device's label on a user frame (ChatEventUser), so a
// transcript can say who sent the message. It is empty on assistant frames.
Client string `json:"client,omitempty"`
@@ -310,6 +325,22 @@ type ChatUsage struct {
Cost float64 `json:"cost,omitempty"`
}
// ChatShell is the state of one shell command the agent started, carried by a
// ChatEventShell frame. ID is the OpenCode shell id and is stable across its
// created and exited events, so a client keys a running process by it. Status is
// ChatShellRunning or ChatShellExited; Command is the command line the shell was
// given (present on the created event); Exit is the process exit code and is
// present only once the shell has exited — it is a pointer so an exit of 0 is
// distinguishable from "not exited yet". No credential, environment or output is
// carried: the command line is the only text, and only so the operator can name
// what is running.
type ChatShell struct {
ID string `json:"id"`
Status string `json:"status,omitempty"`
Command string `json:"command,omitempty"`
Exit *int `json:"exit,omitempty"`
}
// ChatPermissionFrame is the server-pushed chat_permission payload: one pending
// OpenCode permission request the phone must approve or reject. It is a distinct
// Message field (like ChatEvent) rather than a ChatEvent.Kind, so it does not
+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.