Expose native chat compaction activity and retained replay
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user