echo_ts (v0.1.1293, Codex desktop, a GPT model on GitHub Copilot): each commentary paragraph was shown twice; fine around v0.1.1280. Cause: sinceec7f47432(v0.1.1287) a Codex turn on Copilot offering web_search is relayed, not translated. Copilot's /responses gives an item a new id in every event: output_item.added, each delta, output_item.done and response.completed's output all differ (checked 2026-10-19 on the owner's account, gpt-5.5 with web_search, in a sandbox HOME). Codex starts an item by the added's id and ends one by the done's, so the streamed message and the finished one were two messages. Translated, magpie's encoder had given each item one id of its own, so it didn't show before. Fix: idTidy (itemids.go), in passthrough before phaseTidy for every relayed Responses stream: each later event's item_id / item.id, and response.completed/incomplete/failed's output[i].id, take the id the item was added with. An upstream whose ids agree goes through byte for byte. docs/subsystems/gateway-routing.md says so. Tests: - TestCopilotItemKeepsItsID: a real Copilot reply (testdata, encrypted parts cut short) relayed through the handler from a fake Copilot (copilotSol now takes a reply); each output item goes by one id. - TestItemIDsThatAgreeGoAsTheyCame: OpenAI-shaped ids, fed in 7-byte pieces, come out unchanged. Broken by hand (idTidy off in passthrough): TestCopilotItemKeepsItsID FAIL "item 0 went by 3 ids", items 1-3 by 6, 8, 5. Restored: ok. Real use: built magpie serve in a sandbox HOME (port 3478, only the Copilot sign-in copied), the real codex-cli 0.162.1 exec --json with web_search on copilot/gpt-5.5: - before: web_search item.started and item.completed under different ids; - after: same id for each, and the follow-up requests carrying the kept ids back to Copilot were answered. A raw stream gave one id per item. Not tried in the Codex desktop app itself. Sibling found, not changed: on the translated path a reasoning item with a non-rs_ id and a summary reaches the client twice (filed #2974; Copilot gave summary [] in every capture). Run atb9d1cd36fplus this change: gofmt -l clean; go vet ./... for darwin, linux (-tags nogui) and windows ok; go test -timeout 45m ./...: 64 packages ok (internal/gateway 2023s), exit 0.
105 lines
3.5 KiB
Go
105 lines
3.5 KiB
Go
package gateway
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/tidwall/gjson"
|
|
)
|
|
|
|
// Copilot's /responses gives an item a new id in every event (the fixture
|
|
// is a real reply, gpt-5.5 offered web_search and a function, its
|
|
// encrypted parts cut short). Relayed to Codex, each item keeps the one
|
|
// id it was added with, in its events, its done and response.completed,
|
|
// so Codex shows the commentary once and ends the search it started
|
|
// (echo_ts on Discord).
|
|
func TestCopilotItemKeepsItsID(t *testing.T) {
|
|
reply, err := os.ReadFile("testdata/copilot_rotating_ids.sse")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c := copilotSol(t)
|
|
c.mu.Lock()
|
|
c.reply = string(reply)
|
|
c.mu.Unlock()
|
|
code, out := postTo(t, New(), "/v1/responses", `{"model":"copilot/gpt-6-luna","stream":true,"probe":"as sent",
|
|
"input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"Search for the latest Go release, then run go version."}]}],
|
|
"tools":[{"type":"function","name":"exec_command","description":"Runs a command","strict":false,"parameters":{"type":"object","properties":{"cmd":{"type":"string"}},"required":["cmd"]}},{"type":"web_search"}]}`)
|
|
if code != 200 {
|
|
t.Fatalf("%d %s", code, out)
|
|
}
|
|
if c.last(t)["probe"] != "as sent" {
|
|
t.Fatal("not relayed: the reply under test is Copilot's own")
|
|
}
|
|
ids := map[int64]map[string]bool{} // output_index → every id it was given
|
|
note := func(at int64, id string) {
|
|
if ids[at] == nil {
|
|
ids[at] = map[string]bool{}
|
|
}
|
|
ids[at][id] = true
|
|
}
|
|
texts := map[string]int{}
|
|
completed := false
|
|
for _, line := range strings.Split(out, "\n") {
|
|
data, ok := strings.CutPrefix(line, "data: ")
|
|
if !ok {
|
|
continue
|
|
}
|
|
e := gjson.Parse(data)
|
|
switch e.Get("type").String() {
|
|
case "response.completed":
|
|
completed = true
|
|
for i, it := range e.Get("response.output").Array() {
|
|
note(int64(i), it.Get("id").String())
|
|
}
|
|
continue
|
|
case "response.output_item.done":
|
|
if it := e.Get("item"); it.Get("type").String() == "message" {
|
|
texts[it.Get("content.0.text").String()]++
|
|
}
|
|
}
|
|
at := e.Get("output_index")
|
|
if !at.Exists() {
|
|
continue
|
|
}
|
|
if id := e.Get("item.id"); id.Exists() {
|
|
note(at.Int(), id.String())
|
|
}
|
|
if id := e.Get("item_id"); id.Exists() {
|
|
note(at.Int(), id.String())
|
|
}
|
|
}
|
|
if !completed || len(ids) != 4 {
|
|
t.Fatalf("completed %v, items %d: %s", completed, len(ids), out)
|
|
}
|
|
for at, seen := range ids {
|
|
if len(seen) != 1 {
|
|
t.Errorf("item %d went by %d ids", at, len(seen))
|
|
}
|
|
}
|
|
if len(texts) != 1 {
|
|
t.Errorf("messages done: %v", texts)
|
|
}
|
|
}
|
|
|
|
// An upstream whose ids agree, as OpenAI's do, goes through byte for byte.
|
|
func TestItemIDsThatAgreeGoAsTheyCame(t *testing.T) {
|
|
stream := sse(
|
|
`data: {"type":"response.created","response":{"id":"r1"}}`,
|
|
`data: {"type":"response.output_item.added","output_index":0,"item":{"type":"message","id":"msg_1","role":"assistant","content":[]}}`,
|
|
`data: {"type":"response.output_text.delta","output_index":0,"item_id":"msg_1","content_index":0,"delta":"a <b> & c"}`,
|
|
`data: {"type":"response.output_item.done","output_index":0,"item":{"type":"message","id":"msg_1","role":"assistant","content":[{"type":"output_text","text":"a <b> & c"}]}}`,
|
|
`data: {"type":"response.completed","response":{"id":"r1","output":[{"type":"message","id":"msg_1"}],"usage":{"input_tokens":1.0}}}`,
|
|
)
|
|
var got []byte
|
|
tidy := &idTidy{}
|
|
for i := 0; i < len(stream); i += 7 {
|
|
got = append(got, tidy.write([]byte(stream[i:min(i+7, len(stream))]))...)
|
|
}
|
|
got = append(got, tidy.flush()...)
|
|
if string(got) != stream {
|
|
t.Errorf("changed:\n%s\nwant:\n%s", got, stream)
|
|
}
|
|
}
|