Expose native chat compaction activity and retained replay

This commit is contained in:
2026-10-20 22:17:13 +01:00
parent d8b111913e
commit 4969d667b3
17 changed files with 666 additions and 26 deletions
+72 -3
View File
@@ -63,6 +63,8 @@ type Chat struct {
emitted map[string]bool
closed bool
connectionErr error
compactions map[string]protocol.ChatEventFrame
endedTurns map[string]string
}
type pendingForm struct {
@@ -194,6 +196,7 @@ func (c *Chat) readLoop() {
func (c *Chat) failPending(err error) {
c.mu.Lock()
c.compactions = nil
c.connectionErr = err
c.approvals = nil
c.forms = map[string]pendingForm{}
@@ -271,6 +274,42 @@ func (c *Chat) respond(id json.RawMessage, result any) {
func (c *Chat) emit(frame protocol.ChatEventFrame) {
c.mu.Lock()
if frame.Kind == protocol.ChatEventCompaction {
if frame.Compaction == nil {
c.mu.Unlock()
return
}
if frame.NativeTurnID != "" && c.endedTurns[frame.SessionID] == frame.NativeTurnID {
c.mu.Unlock()
return
}
if current := c.turns[frame.SessionID]; current != "" && frame.NativeTurnID != "" && current != frame.NativeTurnID {
c.mu.Unlock()
return
}
if frame.Compaction.Status == "running" {
if c.compactions == nil {
c.compactions = map[string]protocol.ChatEventFrame{}
}
c.compactions[frame.SessionID] = frame
} else {
previous := c.compactions[frame.SessionID]
if previous.Compaction != nil && frame.Compaction.ID != "" && previous.Compaction.ID != frame.Compaction.ID {
c.mu.Unlock()
return
}
delete(c.compactions, frame.SessionID)
}
}
if frame.Kind == protocol.ChatEventStep && frame.Step != nil && frame.Step.Status == protocol.ChatStepEnded {
delete(c.compactions, frame.SessionID)
if frame.Step.TurnID != "" {
if c.endedTurns == nil {
c.endedTurns = map[string]string{}
}
c.endedTurns[frame.SessionID] = frame.Step.TurnID
}
}
set := c.subs[frame.SessionID]
var targets []chan protocol.ChatEventFrame
for ch := range set {
@@ -294,6 +333,9 @@ func (c *Chat) subscribe(id string) chan protocol.ChatEventFrame {
c.subs[id] = map[chan protocol.ChatEventFrame]struct{}{}
}
c.subs[id][ch] = struct{}{}
if frame, ok := c.compactions[id]; ok {
ch <- frame
}
c.mu.Unlock()
return ch
}
@@ -727,9 +769,19 @@ func (c *Chat) handleNotification(msg rpcMessage) {
c.mu.Unlock()
c.emit(stepFrame(params.ThreadID, protocol.ChatStepEnded, completedID))
case "item/started":
c.handleItem(params.ThreadID, params.ItemID, params.Item, false)
c.handleItem(params.ThreadID, params.ItemID, params.Item, false, params.TurnID)
case "item/completed":
c.handleItem(params.ThreadID, params.ItemID, params.Item, true)
c.handleItem(params.ThreadID, params.ItemID, params.Item, true, params.TurnID)
case "thread/compacted":
// Deprecated ContextCompactedNotification has only threadId/turnId.
// Modern item lifecycles are correlated more precisely; never let this
// unkeyed legacy duplicate clear a newer modern compaction item.
c.mu.Lock()
modern := c.compactions[params.ThreadID].Compaction != nil
c.mu.Unlock()
if !modern {
c.emit(protocol.ChatEventFrame{SessionID: params.ThreadID, NativeTurnID: params.TurnID, Kind: protocol.ChatEventCompaction, Compaction: &protocol.ChatCompaction{Status: "completed"}})
}
case "item/agentMessage/delta", "item/agentMessage/streaming":
if params.ItemID != "" && params.Delta != "" {
c.markEmitted(params.ItemID)
@@ -745,7 +797,7 @@ func (c *Chat) handleNotification(msg rpcMessage) {
}
}
func (c *Chat) handleItem(threadID, itemID string, raw json.RawMessage, completed bool) {
func (c *Chat) handleItem(threadID, itemID string, raw json.RawMessage, completed bool, nativeTurnID ...string) {
var record item
if err := json.Unmarshal(raw, &record); err != nil {
return
@@ -754,6 +806,19 @@ func (c *Chat) handleItem(threadID, itemID string, raw json.RawMessage, complete
record.ID = itemID
}
switch record.Type {
case "contextCompaction":
if record.ID == "" {
return
}
status := "running"
if completed {
status = "completed"
}
frame := protocol.ChatEventFrame{SessionID: threadID, Kind: protocol.ChatEventCompaction, Compaction: &protocol.ChatCompaction{Status: status, ID: record.ID}}
if len(nativeTurnID) > 0 {
frame.NativeTurnID = nativeTurnID[0]
}
c.emit(frame)
case "agentMessage":
if completed && record.Text != "" && !c.wasEmitted(record.ID) {
c.emit(textFrame(threadID, record.ID, protocol.ChatEventText, record.Text))
@@ -867,6 +932,10 @@ func (t thread) chatSession() protocol.ChatSession {
func (i item) chatMessage(sessionID string) (protocol.ChatMessage, bool) {
msg := protocol.ChatMessage{ID: i.ID, SessionID: sessionID}
switch i.Type {
case "contextCompaction":
// Historical ThreadItem has no lifecycle status. It is neither a
// synthetic tool nor evidence that compaction is currently running.
return protocol.ChatMessage{}, false
case "userMessage":
msg.Role = protocol.ChatRoleUser
if i.ClientID != "" {
+119
View File
@@ -0,0 +1,119 @@
package codex
import (
"context"
"encoding/json"
"testing"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func TestCompactionHistoryReadbackHasNoSyntheticToolOrRunningGuess(t *testing.T) {
c := modelTestChat(t, func(method string, _ json.RawMessage) any {
if method != "thread/turns/list" {
t.Errorf("unexpected method: %s", method)
}
return json.RawMessage(`{"data":[{"id":"turn","status":"inProgress","items":[{"type":"contextCompaction","id":"compact"}]}],"nextCursor":null}`)
})
page, err := c.Messages(context.Background(), fixtureThread, 50, "")
if err != nil || len(page.Messages) != 0 {
t.Fatalf("synthetic compaction transcript: %+v %v", page, err)
}
readback, err := c.latestTurn(context.Background(), fixtureThread)
if err != nil || !activeTurn(readback.turn) || len(c.compactions) != 0 {
t.Fatal("history guessed running compaction")
}
}
func TestCompactionTerminalReadbackSettlesRetainedActivity(t *testing.T) {
c := modelTestChat(t, func(method string, _ json.RawMessage) any {
if method != "thread/turns/list" {
t.Errorf("unexpected method: %s", method)
}
return json.RawMessage(`{"data":[{"id":"turn","status":"completed","items":[{"type":"contextCompaction","id":"compact"}]}],"nextCursor":null}`)
})
c.mu.Lock()
c.turns[fixtureThread] = "turn"
c.mu.Unlock()
c.emit(protocol.ChatEventFrame{SessionID: fixtureThread, NativeTurnID: "turn", Kind: protocol.ChatEventCompaction, Compaction: &protocol.ChatCompaction{ID: "compact", Status: "running"}})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var frames []protocol.ChatEventFrame
err := c.Stream(ctx, fixtureThread, nil, func(f protocol.ChatEventFrame) error {
frames = append(frames, f)
if f.Step != nil && f.Step.Status == protocol.ChatStepEnded {
cancel()
}
return nil
})
if err != context.Canceled || len(frames) != 2 || frames[0].Compaction == nil || frames[1].Step == nil || len(c.compactions) != 0 {
t.Fatalf("terminal readback: %+v %v", frames, err)
}
}
// Typed schema fixtures from openai/codex@c465563aa9626d99420062135b187d299f904f81:
// app-server-protocol/schema/typescript/v2/{ThreadItem,ItemStartedNotification,
// ItemCompletedNotification,ContextCompactedNotification}.ts and
// app-server-protocol/src/protocol/common.rs. No live compaction was triggered.
func TestCompactionNotificationsAreActivityNotTools(t *testing.T) {
c := &Chat{subs: map[string]map[chan protocol.ChatEventFrame]struct{}{}, turns: map[string]string{"thread": "turn_new"}}
ch := c.subscribe("thread")
other := c.subscribe("other")
notify := func(method, turnID, itemID string) {
raw, _ := json.Marshal(map[string]any{"threadId": "thread", "turnId": turnID, "item": map[string]any{"id": itemID, "type": "contextCompaction"}})
c.handleNotification(rpcMessage{Method: method, Params: raw})
}
notify("item/started", "turn_old", "old")
if len(ch) != 0 {
t.Fatal("old-turn compaction surfaced")
}
notify("item/started", "turn_new", "compact_1")
f := <-ch
if f.Kind != protocol.ChatEventCompaction || f.MessageID != "" || f.Part != nil || f.Step != nil || f.Compaction.ID != "compact_1" || f.NativeTurnID != "turn_new" || f.Compaction.Status != "running" {
t.Fatalf("start: %+v", f)
}
if len(other) != 0 {
t.Fatal("cross-thread compaction leaked")
}
late := c.subscribe("thread")
if replay := <-late; replay.Compaction == nil || replay.Compaction.ID != "compact_1" {
t.Fatalf("late watcher replay: %+v", replay)
}
notify("item/started", "turn_new", "compact_2")
<-ch
notify("item/completed", "turn_new", "compact_1")
if len(ch) != 0 {
t.Fatal("old item completion cleared new compaction")
}
// Legacy completion is unkeyed; ignore it while a modern keyed item runs.
notify("thread/compacted", "turn_new", "")
if len(ch) != 0 {
t.Fatal("legacy duplicate cleared keyed compaction")
}
notify("item/completed", "turn_new", "compact_2")
f = <-ch
if f.Compaction.Status != "completed" || c.turns["thread"] != "turn_new" {
t.Fatal("compaction completion ended native turn")
}
if len(c.compactions) != 0 {
t.Fatal("completed compaction retained")
}
if _, ok := (item{Type: "contextCompaction", ID: "historic"}).chatMessage("thread"); ok {
t.Fatal("compaction history became a fake tool/message")
}
notify("thread/compacted", "turn_old", "")
if len(ch) != 0 {
t.Fatal("old legacy turn completion surfaced")
}
notify("thread/compacted", "turn_new", "")
if f := <-ch; f.Compaction.Status != "completed" {
t.Fatal("typed legacy completion unsupported")
}
// Turn terminal state blocks even a late same-turn start.
c.emit(stepFrame("thread", protocol.ChatStepEnded, "turn_new"))
<-ch
notify("item/started", "turn_new", "late_old")
if len(ch) != 0 {
t.Fatal("terminal turn regained compaction")
}
}
+14
View File
@@ -59,6 +59,20 @@ func (c *Chat) streamWithReadback(ctx context.Context, id string, ready func(),
c.mu.Unlock()
c.emit(stepFrame(id, protocol.ChatStepStarted, last.turn.ID))
}
// ThreadItem.contextCompaction has no running/completed field in history;
// never infer running from its presence. A terminal turn can, however,
// authoritatively settle a retained live compaction after a stream reattach.
if terminalTurn(last.turn) {
c.mu.Lock()
matching := c.turns[id] == last.turn.ID
if matching {
delete(c.turns, id)
}
c.mu.Unlock()
if matching {
c.emit(stepFrame(id, protocol.ChatStepEnded, last.turn.ID))
}
}
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
+43 -1
View File
@@ -749,6 +749,11 @@ func (c *Chat) Stream(ctx context.Context, id string, ready func(), emit func(pr
return err
}
defer body.Close()
defer func() {
state.mu.Lock()
state.Compaction = nil
state.mu.Unlock()
}()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var delivery sync.Mutex
@@ -759,16 +764,30 @@ func (c *Chat) Stream(ctx context.Context, id string, ready func(), emit func(pr
busy = value
state.mu.Lock()
state.Active = value
if !value {
state.Compaction = nil
}
state.mu.Unlock()
}
deliver := func(frame protocol.ChatEventFrame) error {
delivery.Lock()
defer delivery.Unlock()
if frame.Kind == protocol.ChatEventStep || frame.Kind == protocol.ChatEventText || frame.Kind == protocol.ChatEventTool || frame.Kind == protocol.ChatEventReasoning {
if frame.Kind == protocol.ChatEventStep || frame.Kind == protocol.ChatEventText || frame.Kind == protocol.ChatEventTool || frame.Kind == protocol.ChatEventReasoning || (frame.Compaction != nil && frame.Compaction.Status == "running") {
setBusy(true)
}
return emit(frame)
}
// Recover a compaction already underway before this SSE attachment. The
// event body is already open, so any concurrent terminal event follows this
// snapshot in the scanner, rather than being lost between read and subscribe.
if compaction := c.readCompaction(ctx, id); compaction != nil {
state.mu.Lock()
state.Compaction = compaction
state.mu.Unlock()
if err := deliver(protocol.ChatEventFrame{SessionID: id, Kind: protocol.ChatEventCompaction, Compaction: compaction}); err != nil {
return err
}
}
// V2 /api/session/active describes foreground drains, not individual
// provider steps. Poll it even after reconnect: a busy drain may have
// started before this SSE subscription. Failed readbacks never imply idle.
@@ -872,6 +891,7 @@ func isChatEventType(t string) bool {
case "session.text.delta", "session.reasoning.delta",
"session.step.started", "session.step.streamed", "session.step.ended",
"session.usage.updated",
"session.compaction.started", "session.compaction.ended", "session.compaction.failed",
"session.tool.input.started", "session.tool.input.ended",
"session.tool.called", "session.tool.progress",
"session.tool.success", "session.tool.failed",
@@ -1028,6 +1048,7 @@ func (i *chatShellInfo) shellSessionID() string {
// - Forms maps a pending form id to its session, so form.replied/cancelled
// (which carry no session) can be attributed to the right chat.
type ChatStreamState struct {
Compaction *protocol.ChatCompaction
mu sync.Mutex
Active bool // last observed foreground drain, retained across SSE reconnects
LastMessageID string
@@ -1157,6 +1178,27 @@ func NormalizeChatEvent(payload []byte, sessionID string, stream *ChatStreamStat
if data.SessionID != sessionID {
return protocol.ChatEventFrame{}, false, nil
}
// Verified V2 SessionEvent.Compaction schema (upstream 3fdb13bc):
// only sessionID is shared across started/ended/failed; no message id.
if strings.HasPrefix(outer.Type, "session.compaction.") {
status := "running"
if outer.Type == "session.compaction.ended" {
status = "completed"
}
if outer.Type == "session.compaction.failed" {
status = "failed"
}
compaction := &protocol.ChatCompaction{Status: status}
if stream != nil {
stream.mu.Lock()
stream.Compaction = nil
if status == "running" {
stream.Compaction = compaction
}
stream.mu.Unlock()
}
return protocol.ChatEventFrame{SessionID: sessionID, Kind: protocol.ChatEventCompaction, Compaction: compaction}, true, nil
}
messageID := data.MessageID
if messageID == "" {
messageID = data.MessageIDAlt
+36
View File
@@ -0,0 +1,36 @@
package opencode
import (
"context"
"encoding/json"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// The canonical V2 Session.Message.Compaction union exposes status in history.
// Do not expose its summary, recent context or provider state as assistant text.
func (c *Chat) readCompaction(ctx context.Context, id string) *protocol.ChatCompaction {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
raw, err := c.call(ctx, "get", "/api/session/"+id+"/message?limit=50", nil)
if err != nil {
return nil
}
var page struct {
Data []struct {
Type string `json:"type"`
Status string `json:"status"`
} `json:"data"`
}
if json.Unmarshal(raw, &page) != nil {
return nil
}
for _, message := range page.Data {
if message.Type == "compaction" && message.Status == "running" {
// No id: public started/ended events cannot correlate history ids.
return &protocol.ChatCompaction{Status: "running"}
}
}
return nil
}
+118
View File
@@ -0,0 +1,118 @@
package opencode
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// Schema-derived fixtures, not a captured live provider run:
// anomalyco/opencode@3fdb13bc262a16b3e8f8ecab4381f619963d9bfd
// packages/schema/src/session-event.ts (Compaction), and canonical V2 OpenAPI
// Session.Message.Compaction.{Running,Completed,Failed}.
func TestNormalizeCompactionIsMessageLessSessionActivity(t *testing.T) {
state := &ChatStreamState{LastMessageID: "previous_assistant"}
for _, tc := range []struct{ event, status string }{
{"started", "running"}, {"ended", "completed"}, {"started", "running"}, {"failed", "failed"},
} {
payload := []byte(fmt.Sprintf(`{"type":"session.compaction.%s","data":{"sessionID":"ses_1","reason":"auto"}}`, tc.event))
frame, ok, err := NormalizeChatEvent(payload, "ses_1", state)
if err != nil || !ok || frame.Kind != protocol.ChatEventCompaction || frame.Compaction.Status != tc.status || frame.MessageID != "" || frame.Part != nil || frame.Step != nil {
t.Fatalf("invalid compaction: %+v %v %v", frame, ok, err)
}
if (state.Compaction != nil) != (tc.status == "running") {
t.Fatalf("retained state: %+v", state.Compaction)
}
}
_, ok, err := NormalizeChatEvent([]byte(`{"type":"session.compaction.started","data":{"sessionID":"ses_other","reason":"auto"}}`), "ses_1", state)
if ok || err != nil || state.Compaction != nil {
t.Fatal("foreign session contaminated compaction")
}
_, ok, err = NormalizeChatEvent([]byte(`{"type":"session.compaction.delta","data":{"sessionID":"ses_1","text":"private summary"}}`), "ses_1", state)
if ok || err != nil {
t.Fatal("compaction summary must not become transcript content")
}
}
func TestCompactionSnapshotAndForegroundCompletion(t *testing.T) {
events := make(chan string, 8)
active := make(chan struct{})
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/api/session/ses_1/message":
fmt.Fprint(w, `{"data":[{"type":"compaction","id":"msg_compact","status":"running","reason":"auto","summary":"private","recent":"private","time":{"created":1}}]}`)
case "/api/session/active":
select {
case <-active:
fmt.Fprint(w, `{"data":{}}`)
default:
fmt.Fprint(w, `{"data":{"ses_1":{}}}`)
}
case "/api/event":
w.Header().Set("Content-Type", "text/event-stream")
w.WriteHeader(200)
w.(http.Flusher).Flush()
for {
select {
case <-r.Context().Done():
return
case event := <-events:
fmt.Fprintf(w, "data: %s\n\n", event)
w.(http.Flusher).Flush()
}
}
default:
http.NotFound(w, r)
}
}))
defer srv.Close()
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
frames := make(chan protocol.ChatEventFrame, 8)
done := make(chan error, 1)
go func() {
done <- chat.Stream(ctx, "ses_1", nil, func(f protocol.ChatEventFrame) error { frames <- f; return nil })
}()
next := func() protocol.ChatEventFrame {
t.Helper()
select {
case f := <-frames:
return f
case <-ctx.Done():
t.Fatal("missing frame")
return protocol.ChatEventFrame{}
}
}
if f := next(); f.Compaction == nil || f.Compaction.Status != "running" || f.MessageID != "" {
t.Fatalf("snapshot: %+v", f)
}
events <- `{"type":"session.step.ended","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","finish":"tool-calls"}}`
if f := next(); f.Step == nil || !f.Step.Intermediate {
t.Fatalf("provider step: %+v", f)
}
state := chat.streamState("ses_1")
state.mu.Lock()
running := state.Compaction != nil
state.mu.Unlock()
if !running {
t.Fatal("intermediate provider step cleared compaction")
}
close(active)
if f := next(); f.Step == nil || f.Step.Status != protocol.ChatStepEnded || f.Step.Intermediate {
t.Fatalf("foreground completion: %+v", f)
}
cancel()
<-done
state.mu.Lock()
defer state.mu.Unlock()
if state.Compaction != nil {
t.Fatal("stream/foreground completion retained stuck compaction")
}
}
+3
View File
@@ -96,6 +96,9 @@ func TestChatStreamFiltersFrames(t *testing.T) {
prompts.Add(1)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"data":{"id":"msg_1","sessionID":"ses_1","type":"user"}}`))
case r.Method == http.MethodGet && r.URL.Path == "/api/session/ses_1/message":
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"data":[]}`))
case r.Method == http.MethodGet && r.URL.Path == "/api/event":
w.Header().Set("Content-Type", "text/event-stream")
for _, frame := range frames {
+5
View File
@@ -204,6 +204,11 @@ func TestChatStreamRealV2Shapes(t *testing.T) {
`{"id":"evt_5","created":5,"type":"session.step.ended","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","finish":"stop","tokens":{"input":94,"output":304},"cost":0.0005}}`,
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/api/session/ses_1/message" {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"data":[]}`))
return
}
if r.Method != http.MethodGet || r.URL.Path != "/api/event" {
t.Errorf("unexpected request %s %s", r.Method, r.URL.Path)
return
+23 -12
View File
@@ -101,13 +101,14 @@ const (
// text of a prompt another client sent, so a transcript can show it live; it is
// never sent back to the client that sent the prompt.
const (
ChatEventText = ChatPartText
ChatEventReasoning = ChatPartReasoning
ChatEventTool = ChatPartTool
ChatEventStep = "step"
ChatEventResync = "resync"
ChatEventUsage = "usage"
ChatEventUser = "user"
ChatEventText = ChatPartText
ChatEventReasoning = ChatPartReasoning
ChatEventTool = ChatPartTool
ChatEventStep = "step"
ChatEventResync = "resync"
ChatEventUsage = "usage"
ChatEventUser = "user"
ChatEventCompaction = "compaction"
// 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.
@@ -418,11 +419,14 @@ type ChatTime struct {
// tool carries a full Part with its state, step marks turn progress and usage
// reports the turn's totals.
type ChatEventFrame struct {
SessionID string `json:"session_id"`
MessageID string `json:"message_id,omitempty"`
Kind string `json:"kind"`
Delta string `json:"delta,omitempty"`
Part *ChatPart `json:"part,omitempty"`
// NativeTurnID scopes session activity to a harness turn when available.
NativeTurnID string `json:"native_turn_id,omitempty"`
Compaction *ChatCompaction `json:"compaction,omitempty"`
SessionID string `json:"session_id"`
MessageID string `json:"message_id,omitempty"`
Kind string `json:"kind"`
Delta string `json:"delta,omitempty"`
Part *ChatPart `json:"part,omitempty"`
// Attachments carries the file parts of a user turn another client sent.
// A frame has one Part, so the prompt's files travel alongside its text.
Attachments []ChatPart `json:"attachments,omitempty"`
@@ -441,6 +445,13 @@ type ChatEventFrame struct {
OperationID string `json:"operation_id,omitempty"`
}
// ChatCompaction is session activity, never a transcript message or tool.
// ID is optional: OpenCode V2 compaction events have no shared lifecycle id.
type ChatCompaction struct {
Status string `json:"status"` // running | completed | failed
ID string `json:"id,omitempty"`
}
// ChatStep is a progress marker for the current step of a turn.
type ChatStep struct {
Status string `json:"status"`
+2
View File
@@ -45,10 +45,12 @@ func handleChatClose(ctx context.Context, request protocol.Message, broker chatB
// idle session is never interrupted.
interrupted := hub.turnInFlight(sessionID)
if interrupted {
resetCompaction := hub.compactionReset(sessionID)
if err := broker.Interrupt(ctx, sessionID); err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + sessionID + " could not be stopped: " + err.Error()})
return
}
resetCompaction()
}
if err := registry.CloseOperatorSession(ctx, sessionID); err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
+2
View File
@@ -42,10 +42,12 @@ func handleChatSimple(ctx context.Context, request protocol.Message, broker chat
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
return
}
resetCompaction := hub.compactionReset(query.SessionID)
if err := broker.Interrupt(ctx, query.SessionID); err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
resetCompaction()
_ = write(protocol.Message{Type: protocol.ChatInterrupt, ID: request.ID})
return
}
+64
View File
@@ -0,0 +1,64 @@
package server
import "git.a-wan.net/alan/pipelines/internal/protocol"
// Capture the exact activity before interrupt I/O. A late interrupt reply may
// clear that activity, but must not clear a newer compaction or replacement room.
func (h *chatHub) compactionReset(sessionID string) func() {
h.mu.Lock()
room := h.rooms[sessionID]
var current *protocol.ChatCompaction
if turn := h.turns[sessionID]; turn != nil {
current = turn.compaction
}
h.mu.Unlock()
return func() {
h.mu.Lock()
defer h.mu.Unlock()
if room != nil && h.rooms[sessionID] == room && h.turns[sessionID] != nil && h.turns[sessionID].compaction == current {
h.clearCompactionLocked(room)
}
}
}
// Replay and lifecycle updates enqueue under the fold lock so an older replay
// cannot overtake a terminal event. Compaction never releases the turn slot.
func (h *chatHub) replayCompaction(sub *chatSub) {
h.mu.Lock()
slow := false
if h.rooms[sub.room.sessionID] == sub.room && sub.room.subs[sub.clientID] == sub {
if current := h.turnLocked(sub.room.sessionID).compaction; current != nil {
copy := *current
frame := protocol.ChatEventFrame{SessionID: sub.room.sessionID, Kind: protocol.ChatEventCompaction, Compaction: &copy, NativeTurnID: sub.room.nativeTurnID}
slow = !enqueueChatLocked(sub, protocol.Message{Type: protocol.ChatEvent, ChatEvent: &frame})
}
}
h.mu.Unlock()
if slow {
sub.close()
}
}
// Loss of observability is not successful completion; clear only the activity
// label, preserving the harness turn and queue slot for authoritative readback.
func (h *chatHub) clearCompactionLocked(room *chatRoom) {
turn := h.turns[room.sessionID]
if turn == nil || turn.compaction == nil {
return
}
copy := *turn.compaction
copy.Status = "failed"
turn.compaction = nil
frame := protocol.ChatEventFrame{SessionID: room.sessionID, Kind: protocol.ChatEventCompaction, Compaction: &copy, NativeTurnID: room.nativeTurnID}
if room.buffering {
room.buffered = append(room.buffered, frame)
return
}
for _, sub := range room.subs {
if !enqueueChatLocked(sub, protocol.Message{Type: protocol.ChatEvent, ChatEvent: &frame}) {
// close detaches under h.mu; never block the folding lock. A full
// queue must reconnect rather than silently miss terminal cleanup.
go sub.close()
}
}
}
+108
View File
@@ -0,0 +1,108 @@
package server
import (
"context"
"testing"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func TestCompactionRetainedReplayIsolationAndTerminalClear(t *testing.T) {
h := newChatHub()
room := newChatRoom(context.Background(), "ses_compact")
defer room.cancel()
room.running, room.nativeTurnID = true, "turn_new"
room.turnDone = make(chan struct{})
h.rooms[room.sessionID] = room
sub := &chatSub{hub: h, room: room, clientID: "watcher", ch: make(chan protocol.Message, 32), done: make(chan struct{})}
room.subs[sub.clientID] = sub
emit := func(id, status, nativeID string) {
h.emitRoom(room, protocol.ChatEventFrame{SessionID: room.sessionID, NativeTurnID: nativeID, Kind: protocol.ChatEventCompaction, Compaction: &protocol.ChatCompaction{ID: id, Status: status}})
}
emit("old", "running", "turn_old")
if len(sub.ch) != 0 {
t.Fatal("old native turn surfaced")
}
emit("first", "running", "turn_new")
<-sub.ch
if got := h.snapshot(room.sessionID); len(got) != 0 {
t.Fatalf("fake message: %+v", got)
}
h.replayCompaction(sub)
if f := (<-sub.ch).ChatEvent; f == nil || f.Compaction.ID != "first" || f.MessageID != "" {
t.Fatalf("replay without assistant: %+v", f)
}
other := newChatRoom(context.Background(), "ses_other")
defer other.cancel()
h.rooms[other.sessionID] = other
otherSub := &chatSub{hub: h, room: other, clientID: "other", ch: make(chan protocol.Message, 4), done: make(chan struct{})}
other.subs[otherSub.clientID] = otherSub
h.replayCompaction(otherSub)
if len(otherSub.ch) != 0 {
t.Fatal("foreign-session replay")
}
resetOld := h.compactionReset(room.sessionID)
emit("second", "running", "turn_new")
<-sub.ch
resetOld()
emit("first", "completed", "turn_new")
if len(sub.ch) != 0 || h.turns[room.sessionID].compaction.ID != "second" {
t.Fatal("old lifecycle cleared newer compaction")
}
h.emitRoom(room, protocol.ChatEventFrame{SessionID: room.sessionID, Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded, Intermediate: true}})
<-sub.ch
if h.turns[room.sessionID].compaction == nil || !room.running {
t.Fatal("provider step settled compaction or turn")
}
emit("second", "completed", "turn_new")
<-sub.ch
if !room.running {
t.Fatal("compaction completion released turn queue")
}
emit("third", "running", "turn_new")
<-sub.ch
h.emitRoom(room, protocol.ChatEventFrame{SessionID: room.sessionID, Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded, TurnID: "turn_new"}})
if f := (<-sub.ch).ChatEvent; f.Compaction == nil || f.Compaction.Status != "failed" {
t.Fatalf("terminal cleanup: %+v", f)
}
<-sub.ch // authoritative step completion
if room.running || h.turns[room.sessionID].compaction != nil {
t.Fatal("terminal state stuck")
}
emit("third", "running", "turn_new")
if len(sub.ch) != 0 {
t.Fatal("late old-turn activity revived label")
}
}
func TestCompactionTransportLossAndInterruptClearDoNotReleaseTurn(t *testing.T) {
for _, mode := range []string{"transport", "interrupt", "detach"} {
t.Run(mode, func(t *testing.T) {
h := newChatHub()
room := newChatRoom(context.Background(), "ses_compact")
defer room.cancel()
h.rooms[room.sessionID] = room
room.running = true
sub := &chatSub{hub: h, room: room, clientID: "watcher", ch: make(chan protocol.Message, 8), done: make(chan struct{})}
room.subs[sub.clientID] = sub
h.emitRoom(room, protocol.ChatEventFrame{SessionID: room.sessionID, Kind: protocol.ChatEventCompaction, Compaction: &protocol.ChatCompaction{Status: "running", ID: "compact"}})
<-sub.ch
switch mode {
case "interrupt":
h.compactionReset(room.sessionID)()
case "detach":
h.detach(room.sessionID, sub)
case "transport":
result := make(chan error, 1)
result <- nil
// Cancellation prevents any reconnect I/O; drain still clears retained
// activity on the exact room whose stream ended.
room.cancel()
h.drainStream(room, result)
}
if h.turns[room.sessionID].compaction != nil || !room.running {
t.Fatal("cleanup stuck label or released live turn")
}
})
}
}
+23 -8
View File
@@ -64,14 +64,15 @@ type chatMessage struct {
}
type chatTurn struct {
sessionID string
order []string
messages map[string]*chatMessage
step *protocol.ChatStep
usage *protocol.ChatUsage
size int
overflow bool
updated time.Time
compaction *protocol.ChatCompaction
sessionID string
order []string
messages map[string]*chatMessage
step *protocol.ChatStep
usage *protocol.ChatUsage
size int
overflow bool
updated time.Time
// permissions holds the request ids already pushed to the client so a
// repeated poll cannot push the same pending request twice; permissionOrder
// bounds it to chatPermissionRetainedLimit.
@@ -143,7 +144,21 @@ func (h *chatHub) applyLocked(frame protocol.ChatEventFrame) {
}
turn := h.turnLocked(frame.SessionID)
switch frame.Kind {
case protocol.ChatEventCompaction:
if frame.Compaction == nil {
return
}
if frame.Compaction.Status == "running" {
copy := *frame.Compaction
turn.compaction = &copy
} else if turn.compaction != nil && (frame.Compaction.ID == "" || turn.compaction.ID == frame.Compaction.ID) {
turn.compaction = nil
}
return
case protocol.ChatEventStep:
if frame.Step != nil && frame.Step.Status == protocol.ChatStepEnded && !frame.Step.Intermediate {
turn.compaction = nil
}
turn.step = frame.Step
return
case protocol.ChatEventUsage:
+29 -1
View File
@@ -301,6 +301,7 @@ func (h *chatHub) detach(sessionID string, sub *chatSub) {
}
delete(room.subs, sub.clientID)
if len(room.subs) == 0 {
h.clearCompactionLocked(room)
delete(h.rooms, sessionID)
room.cancel()
// ReleaseShells is a local identity-cache operation (no broker I/O).
@@ -374,6 +375,26 @@ func (h *chatHub) emitRoom(source *chatRoom, frame protocol.ChatEventFrame) {
h.mu.Unlock()
return
}
if frame.Kind == protocol.ChatEventCompaction {
if frame.Compaction == nil {
h.mu.Unlock()
return
}
if room != nil && frame.NativeTurnID != "" && room.nativeTurnID != "" && frame.NativeTurnID != room.nativeTurnID {
h.mu.Unlock()
return
}
current := h.turnLocked(frame.SessionID).compaction
lastStep := h.turnLocked(frame.SessionID).step
if frame.NativeTurnID != "" && lastStep != nil && lastStep.Status == protocol.ChatStepEnded && !lastStep.Intermediate && lastStep.TurnID == frame.NativeTurnID {
h.mu.Unlock()
return
}
if frame.Compaction.Status != "running" && current != nil && frame.Compaction.ID != "" && frame.Compaction.ID != current.ID {
h.mu.Unlock()
return
}
}
if room != nil && frame.Kind == protocol.ChatEventStep && frame.Step != nil && frame.Step.TurnID != "" && !frame.Step.Intermediate {
if frame.Step.Status == protocol.ChatStepStarted {
room.nativeTurnID = frame.Step.TurnID
@@ -383,6 +404,9 @@ func (h *chatHub) emitRoom(source *chatRoom, frame protocol.ChatEventFrame) {
return
}
}
if room != nil && frame.Kind == protocol.ChatEventStep && frame.Step != nil && frame.Step.Status == protocol.ChatStepEnded && !frame.Step.Intermediate {
h.clearCompactionLocked(room)
}
h.applyLocked(frame)
buffered := false
var targets []*chatSub
@@ -401,7 +425,7 @@ func (h *chatHub) emitRoom(source *chatRoom, frame protocol.ChatEventFrame) {
}
// Shell replay and live lifecycle delivery must enqueue in the same order
// as folding. Otherwise an exit can overtake an older running replay.
if frame.Kind == protocol.ChatEventShell && !buffered {
if (frame.Kind == protocol.ChatEventShell || frame.Kind == protocol.ChatEventCompaction) && !buffered {
msg := protocol.Message{Type: protocol.ChatEvent, ChatEvent: &frame}
var slow []*chatSub
for _, sub := range targets {
@@ -484,6 +508,7 @@ func (h *chatHub) replaceShellsLocked(sessionID string, shells []protocol.ChatSh
// finishTurnLocked marks the room's turn finished and releases the turn runner's
// wait. Callers must hold h.mu.
func (h *chatHub) finishTurnLocked(room *chatRoom) {
h.clearCompactionLocked(room)
room.running = false
room.nativeTurnID = ""
if room.turnDone != nil {
@@ -807,6 +832,9 @@ func (h *chatHub) startStream(room *chatRoom, broker chatBroker) error {
func (h *chatHub) drainStream(room *chatRoom, result <-chan error) {
err := <-result
h.mu.Lock()
if h.rooms[room.sessionID] == room {
h.clearCompactionLocked(room)
}
room.streaming = false
h.mu.Unlock()
if err != nil && room.ctx.Err() == nil {
+1
View File
@@ -243,4 +243,5 @@ func handleChatSubscribe(request protocol.Message, broker chatBroker, hub *chatH
// carry a shell — a shell outlives the turn and is not a turn — so this is the
// shell half of the resume.
hub.replayShells(sub)
hub.replayCompaction(sub)
}
+4 -1
View File
@@ -883,6 +883,9 @@ func TestChatInterruptEndsRunningPrompt(t *testing.T) {
interruptOnce.Do(func() { close(interrupted) })
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{}`))
case r.Method == http.MethodGet && r.URL.Path == "/api/session/ses_1/message":
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"data":[]}`))
default:
t.Errorf("unexpected request %s %s", r.Method, r.URL.Path)
}
@@ -1384,7 +1387,7 @@ func realAdapterChat(t *testing.T, frames []string) []protocol.ChatEventFrame {
prompts.Add(1)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"data":{"id":"msg_1","sessionID":"ses_1","type":"user"}}`))
case r.Method == http.MethodGet && (r.URL.Path == "/api/session/ses_1/permission" || r.URL.Path == "/api/session/ses_1/form"):
case r.Method == http.MethodGet && (r.URL.Path == "/api/session/ses_1/permission" || r.URL.Path == "/api/session/ses_1/form" || r.URL.Path == "/api/session/ses_1/message"):
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"location":{"directory":"/work"},"data":[]}`))
case r.Method == http.MethodGet && r.URL.Path == "/api/shell":