Files

120 lines
4.7 KiB
Go

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")
}
}