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