Files
magpie/internal/gateway/codex_compact_v1_test.go
T
yetone 9275abcddb gateway: Codex's compaction on a CC Switch relay table that magpie points at /v1 completes, with a summary magpie makes, instead of failing "remote compaction v2 expected exactly one compaction output item, got 0 from 1 output items" (#1301, ylacmilan)
Codex compacts remotely (compaction v2: a compaction_trigger item on
/responses, and exactly one compaction item back) for any provider table
named "OpenAI" (codex-rs ProviderCapabilities::from_config, is_openai), and
CC Switch names its relay tables so. While a magpie model is on, magpie
points those tables at /v1 (takeTables). /v1/responses relayed the trigger
as it came; the relay answered with a message, and Codex counted 0
compaction items out of 1. Only CodexPath turned the trigger into a summary
request (codexInput, codexCompact).

handle now does the same for a Responses request on /v1 that carries a
compaction_trigger: the model summarises, and the summary goes back as
magpie's compaction item. A later turn's magpie1: compaction item reaches
the model as the summary's text, not as an item the relay can't open.

TestCodexCompactionTriggerOnV1 fails without the change (one
output_item.done of type "message", the relay's reply, where Codex needs one
"compaction") and passes with it.

Real use: codex-cli 0.160.1 in a sandbox CODEX_HOME with
[model_providers.custom] name = "OpenAI", base_url = magpie's /v1 (built
from this tree, `magpie serve` in a sandbox HOME on 127.0.0.1:3592), model
token/gpt-6.1-sol on a fake Responses relay that reports 90K tokens and
model_auto_compact_token_limit = 1000. Old build: `codex exec resume`
printed the reporter's exact error, and the relay got the
compaction_trigger. New build: "context compacted", the relay got Codex's
compaction prompt, and the next two turns went on with the summary as a
message. go test ./internal/gateway, go vet (darwin, GOOS=linux,
GOOS=windows) pass. Not tried: the reporter's own relay and CC Switch
version.
2026-10-08 19:16:29 +08:00

61 lines
3.0 KiB
Go

package gateway
import (
"encoding/base64"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// #1301: Codex compacts remotely (compaction v2: a compaction_trigger on
// /responses, exactly one compaction item back) for any provider table
// named "OpenAI", as CC Switch names its relay tables. magpie points those
// tables at /v1 while a magpie model is on, so the trigger reached /v1, went
// on to the relay as it came, and the relay's message came back: "remote
// compaction v2 expected exactly one compaction output item, got 0 from 1
// output items". On /v1 the summary is magpie's to make, as on Codex's
// backend path, and magpie's summary goes back to the model as text.
func TestCodexCompactionTriggerOnV1(t *testing.T) {
f := &fake{t: t, reply: sse(
`data: {"type":"response.created","response":{"id":"resp_r1","status":"in_progress","output":[]}}`,
`data: {"type":"response.output_item.done","output_index":0,"item":{"type":"message","role":"assistant","content":[{"type":"output_text","text":"SUMMARY"}]}}`,
`data: {"type":"response.completed","response":{"id":"resp_r1","status":"completed","output":[]}}`)}
setup(t, provider.Responses, f)
code, body := post(t, "/v1/responses", `{"model":"fake/m1","instructions":"You are Codex","stream":true,"store":false,
"include":["reasoning.encrypted_content"],"prompt_cache_key":"thread-1","tool_choice":"auto","parallel_tool_calls":true,
"tools":[{"type":"function","name":"shell","parameters":{"type":"object"}}],
"input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"fix the bug"}]},{"type":"compaction_trigger"}]}`)
if code != 200 {
t.Fatalf("%d %s", code, body)
}
if strings.Contains(string(f.got), "compaction_trigger") || !strings.Contains(string(f.got), "CONTEXT CHECKPOINT COMPACTION") {
t.Errorf("relay got %s", f.got)
}
// what Codex's collect_compaction_output counts: output_item.done items
var done []map[string]any
for _, e := range events(body) {
if e["type"] == "response.output_item.done" {
done = append(done, e["item"].(map[string]any))
}
}
if len(done) != 1 || done[0]["type"] != "compaction" {
t.Fatalf("Codex wants exactly one compaction output item, got %v\n%s", done, body)
}
enc, _ := done[0]["encrypted_content"].(string)
if b, _ := base64.StdEncoding.DecodeString(strings.TrimPrefix(enc, magpieCompaction)); string(b) != "SUMMARY" {
t.Errorf("compaction holds %q", enc)
}
// the next turn carries the compaction item; the relay gets its text
f.reply = sse(`data: {"type":"response.completed","response":{"id":"resp_r2","status":"completed","output":[]}}`)
code, body = post(t, "/v1/responses", `{"model":"fake/m1","stream":true,
"input":[{"type":"compaction","encrypted_content":"`+enc+`"},{"type":"message","role":"user","content":[{"type":"input_text","text":"go on"}]}]}`)
if code != 200 {
t.Fatalf("%d %s", code, body)
}
if strings.Contains(string(f.got), magpieCompaction) || !strings.Contains(string(f.got), "SUMMARY") {
t.Errorf("relay got %s", f.got)
}
}