Files
magpie/internal/gateway/codex_compact_model_test.go
T
yetone 19782923c5 codex: the compaction magpie makes can go on a model of its own (0neB1aze on X)
Codex's memories had a model of their own, its compaction did not. Codex's row has a compaction model square (compact_model, settings.CodexCompactModel), read by the gateway on each compaction (codexCompactTo, before codexCompact). Only a compaction magpie makes is moved: it asks for a plain-text summary any model reads back. One a ChatGPT account seals, or a pick that compacts natively, is left, since only the writing account reads it (#1099); a disabled, unserved or key-barred pick is left with a log line. The text sent to the model is unchanged.

Run on mote: TestCodexCompactOnItsOwnModel (red with the move taken out) and TestCodexCompactToLeavesNative; go test for gui, settings, settingsapi, agent, provider, gateway; darwin, linux and windows vet; the GUI files in both engines. Not tried: a real Codex session compacting on the chosen model.
2026-11-11 20:57:11 +08:00

79 lines
2.5 KiB
Go

package gateway
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// summaryUpstream is a Responses upstream that summarises and counts the
// bodies it was sent.
func summaryUpstream(t *testing.T) (url string, count func() int) {
t.Helper()
var mu sync.Mutex
n := 0
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.Copy(io.Discard, r.Body)
mu.Lock()
n++
mu.Unlock()
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, summaryReply)
}))
t.Cleanup(up.Close)
return up.URL + "/v1", func() int { mu.Lock(); defer mu.Unlock(); return n }
}
// 0neB1aze on X: Codex's memories can be put on a model of its own, its
// compaction could not. A compaction magpie makes goes on the model set
// for it; unset, it stays on the session's.
func TestCodexCompactOnItsOwnModel(t *testing.T) {
setup(t, provider.Responses, &fake{t: t})
sessionURL, session := summaryUpstream(t)
otherURL, other := summaryUpstream(t)
for _, p := range []provider.Provider{
{ID: "fake", Name: "Fake", Key: "k", Models: []string{"m1"}, Responses: sessionURL},
{ID: "cheap", Name: "Cheap", Key: "k", Models: []string{"m2"}, Responses: otherURL},
} {
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
code, body := codexPost(t, systemCompaction)
if code != 200 || session() != 1 || other() != 0 {
t.Fatalf("unset: %d %s; session %d, other %d", code, body, session(), other())
}
if err := provider.SetCodexCompactModel("cheap/m2"); err != nil {
t.Fatal(err)
}
code, body = codexPost(t, systemCompaction)
if code != 200 || session() != 1 || other() != 1 {
t.Fatalf("set: %d %s; session %d, other %d", code, body, session(), other())
}
if sum := compactedText(t, body); !strings.Contains(sum, "SUMMARY") {
t.Errorf("summary %q", sum)
}
}
// A compaction a ChatGPT account seals, or one put on such a model, is
// left where it is: only the account that wrote the summary reads it.
func TestCodexCompactToLeavesNative(t *testing.T) {
fresh(t)
if err := provider.SetCodexCompactModel("cheap/m2"); err != nil {
t.Fatal(err)
}
r := httptest.NewRequest("POST", "/", nil)
never := func(string) bool { return false }
if to := codexCompactTo(r, "fake/m1", true, never); to != "" {
t.Errorf("a native compaction moved to %s", to)
}
if to := codexCompactTo(r, "fake/m1", false, func(string) bool { return true }); to != "" {
t.Errorf("moved to a model that compacts natively: %s", to)
}
}