Routing's Kind description for composer_predictions still sent users to Settings › General › Codex composer predictions. That setting moved to Codex's row on the Agents page. chiperman's commit (5e372e168) fixed the Routing text in five languages. It is ported here with their zh, zh-TW and ja; the de wording is mine. Grepping for other texts that still name the old Settings place for a setting that moved into an agent's row found four more, now fixed: - app.js compactField, the provider editor's "Compact at": empty follows Long conversations, now "in their settings on the Agents page (else in Settings)". The field's text is one string now, and the header comment no longer repeats it, so app.js stays inside its budget (unchanged). - gateway sealedWayOut: "Codex subagents on any model" is in Codex's settings on magpie's Agents page, not "magpie's Settings". - gateway codex_titles: "Codex composer predictions are off" and "Codex titles are off" answers name the same place. - cli `magpie model` help text. Each gateway message has a test that fails on the old wording, and the GUI files assert the new place and the absence of the old one. Run on origin/main46c418832with this commit and #3429's after it (the pushed pair; the Go and GUI runs below were at the pair's head): - gofmt -l: nothing. go vet (darwin) of gui/gateway/cli, GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: clean. - go test -count=1 -timeout 45m ./internal/gui/ ./internal/cli/ ./internal/gateway/: ok (gui 133s, cli 39s, gateway 405s) - GUI, both engines: compact-at, routing-kind, mobile-web, sidebar, refresh-page, gateway-port: 64/76; the 12 reds are sidebar's page list missing Insights › Masking, red on origin/main too (#3498). With that list given "masking" for the run only, sidebar is 18/18. - Broken by hand: the old app.js/i18n text makes compact-at fail on "says where Long conversations is"; the old Routing text makes routing-kind fail. - Whole GUI suite (both engines, -concurrency 4) at the pre-rebase pair c28576084: 12,519 tests, 12,350 pass, 169 fail in 31 files. Each red file was run alone at that head and at origin/main641b1217f. All are red on main too or pass alone, and each has an issue: the 30 files of #3527's list (#3497, #3498, #3488, #3523, #2648, #3479, #3099, #3402, #3404, #3500, #3408-#3415, #3094, #2174, #3091, #3104, #3406), gateway-caller-keys (#2572, #1671), plugin-github (red alone on main, #3218), mobile-web (passes against the commit's base; after the rebase it passes at this head), codex-resets-used (#3535), routing-busy-frames (#3557) and zhipu-team (#3558). signin-port-blocked's 11 were ENOSPC from a full disk mid-run; 40/40 alone. - Not tried: a real Codex session hitting the sealed-task or titles message; the text was read in the tests only. Co-authored-by: chiperman <34889778+chiperman@users.noreply.github.com>
185 lines
8.3 KiB
Go
185 lines
8.3 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
// predictionSchema is the schema ChatGPT.app's composer predictions ask
|
|
// for, as its main bundle builds it ({suggestion: string|null}).
|
|
const predictionSchema = `{"$schema":"https://json-schema.org/draft/2020-12/schema","type":"object","properties":{"suggestion":{"anyOf":[{"type":"string"},{"type":"null"}]}},"required":["suggestion"],"additionalProperties":false}`
|
|
|
|
// codexPredictionRequest is a composer prediction as ChatGPT.app's Codex
|
|
// sends one through its ChatGPT sign-in (#1970, pluo): a hidden turn on a
|
|
// fork of the thread, on the thread's own model, its thread_source
|
|
// composer_predictions, its prompt ending in the schema.
|
|
func codexPredictionRequest(t *testing.T, s *Server, path, model string) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
prompt := "Predict the user's next message.\n\nReturn only JSON matching this schema: " + predictionSchema + "."
|
|
in, _ := json.Marshal(prompt)
|
|
body := `{"model":"` + model + `","stream":true,"input":[` +
|
|
`{"type":"message","role":"user","content":[{"type":"input_text","text":"fix the login bug"}]},` +
|
|
`{"type":"message","role":"assistant","content":[{"type":"output_text","text":"Fixed it; the tests pass."}]},` +
|
|
`{"type":"message","role":"user","content":[{"type":"input_text","text":` + string(in) + `}]}],` +
|
|
`"text":{"format":{"type":"json_schema","name":"codex_output_schema","strict":true,"schema":` + predictionSchema + `}}}`
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest("POST", path, strings.NewReader(body))
|
|
req.Header.Set("Authorization", "Bearer chatgpt-token")
|
|
req.Header.Set("session_id", "prediction-thread")
|
|
req.Header.Set("x-codex-turn-metadata", `{"thread_source":"composer_predictions","forked_from_thread_id":"main-chat"}`)
|
|
s.Handler().ServeHTTP(rec, req)
|
|
return rec
|
|
}
|
|
|
|
func predictionReply(text string) string {
|
|
b, _ := json.Marshal(text)
|
|
return sse(
|
|
`data: {"id":"c1","choices":[{"index":0,"delta":{"content":`+string(b)+`}}]}`,
|
|
`data: {"id":"c1","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":30,"completion_tokens":8}}`,
|
|
`data: [DONE]`)
|
|
}
|
|
|
|
func saveCodexPredictions(t *testing.T, v string) {
|
|
t.Helper()
|
|
st := settings.Load()
|
|
st.CodexPredictions = v
|
|
if err := settings.Save(st); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// Codex's composer predictions go where Settings says (#1970): as they
|
|
// came, to Codex's own sign-in, by default; off, answered here with no
|
|
// prediction and sent nowhere; or to a model of magpie's, its answer
|
|
// handed back as the {"suggestion": …} Codex reads. Each stays a
|
|
// prediction in the Usage view.
|
|
func TestCodexPredictionsSetting(t *testing.T) {
|
|
f := &fake{t: t, reply: predictionReply(`{"suggestion":"Run the tests again"}`)}
|
|
setup(t, provider.Chat, f)
|
|
chatgptCalls := 0
|
|
chatgpt(t, func(w http.ResponseWriter, r *http.Request) {
|
|
io.ReadAll(r.Body)
|
|
chatgptCalls++
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
io.WriteString(w, sse(`data: {"type":"response.completed","response":{"id":"r1","output":[{"type":"message","role":"assistant","content":[{"type":"output_text","text":"{\"suggestion\":\"From ChatGPT\"}"}]}],"usage":{"input_tokens":9,"output_tokens":2}}}`))
|
|
})
|
|
s := New()
|
|
|
|
rec := codexPredictionRequest(t, s, CodexPath+"/responses", "gpt-6-astra")
|
|
if text, _ := outputText(t, rec.Body.String()); chatgptCalls != 1 || f.calls != 0 || text != `{"suggestion":"From ChatGPT"}` {
|
|
t.Fatalf("default: chatgpt %d, magpie model %d, %q", chatgptCalls, f.calls, text)
|
|
}
|
|
|
|
saveCodexPredictions(t, "off")
|
|
rec = codexPredictionRequest(t, s, CodexPath+"/responses", "gpt-6-astra")
|
|
if text, n := outputText(t, rec.Body.String()); rec.Code != 200 || n != 1 || text != `{"suggestion":null}` || chatgptCalls != 1 || f.calls != 0 {
|
|
t.Fatalf("off: %d %q, %d items, chatgpt %d, magpie model %d", rec.Code, rec.Body.String(), n, chatgptCalls, f.calls)
|
|
}
|
|
|
|
saveCodexPredictions(t, "fake/m1")
|
|
rec = codexPredictionRequest(t, s, CodexPath+"/responses", "gpt-6-astra")
|
|
if text, _ := outputText(t, rec.Body.String()); rec.Code != 200 || text != `{"suggestion":"Run the tests again"}` || f.calls != 1 || chatgptCalls != 1 {
|
|
t.Fatalf("routed: %d %q, magpie model %d, chatgpt %d", rec.Code, rec.Body.String(), f.calls, chatgptCalls)
|
|
}
|
|
var sent struct {
|
|
Model string `json:"model"`
|
|
}
|
|
json.Unmarshal(f.got, &sent)
|
|
if sent.Model != "m1" {
|
|
t.Errorf("the magpie model was asked for %q", sent.Model)
|
|
}
|
|
|
|
// a turn of the conversation is not a prediction: it goes as before
|
|
rec = httptest.NewRecorder()
|
|
req := httptest.NewRequest("POST", CodexPath+"/responses", strings.NewReader(`{"model":"gpt-6-astra","stream":true,"input":"hi"}`))
|
|
req.Header.Set("Authorization", "Bearer chatgpt-token")
|
|
s.Handler().ServeHTTP(rec, req)
|
|
if chatgptCalls != 2 || f.calls != 1 {
|
|
t.Errorf("a plain turn: chatgpt %d, magpie model %d", chatgptCalls, f.calls)
|
|
}
|
|
|
|
var kinds []string
|
|
for _, u := range usage.Load(time.Time{}) {
|
|
kinds = append(kinds, u.Provider+":"+u.Kind)
|
|
if u.Provider == "magpie" && (!u.IsRejected() || u.Status != 200) {
|
|
t.Errorf("an answered-here prediction %+v", u)
|
|
}
|
|
// and says where the setting is: Codex's row on the Agents page
|
|
// since f1e4516d, not Settings (#3425)
|
|
if u.Provider == "magpie" && !strings.Contains(u.Error, "in Codex's settings on magpie's Agents page") {
|
|
t.Errorf("an answered-here prediction says %q", u.Error)
|
|
}
|
|
}
|
|
want := "openai:composer_predictions magpie:composer_predictions fake:composer_predictions openai:"
|
|
if got := strings.Join(kinds, " "); got != want {
|
|
t.Errorf("ledger %s\nwant %s", got, want)
|
|
}
|
|
}
|
|
|
|
// A model that talks instead of answering with the object gives Codex no
|
|
// prediction, never its words as the user's next message, and the call
|
|
// is in the Usage view as the failure it is.
|
|
func TestNoPrediction(t *testing.T) {
|
|
f := &fake{t: t, reply: predictionReply("Sure! The user will probably ask you to run the tests again.")}
|
|
setup(t, provider.Chat, f)
|
|
saveCodexPredictions(t, "fake/m1")
|
|
s := New()
|
|
rec := codexPredictionRequest(t, s, CodexPath+"/responses", "gpt-6-astra")
|
|
if text, n := outputText(t, rec.Body.String()); rec.Code != 200 || n != 1 || text != `{"suggestion":null}` {
|
|
t.Fatalf("talking reply: %d %q", rec.Code, rec.Body.String())
|
|
}
|
|
var last usage.Record
|
|
for _, u := range usage.Load(time.Time{}) {
|
|
last = u
|
|
}
|
|
if last.Kind != "composer_predictions" || !strings.HasPrefix(last.Error, "prediction: ") || !last.Failed() {
|
|
t.Fatalf("unusable reply was not flagged: %+v", last)
|
|
}
|
|
}
|
|
|
|
// Codex on a magpie model (an "OpenAI" provider table pointed at magpie,
|
|
// #1301), with nothing set: the model asked for writes the prediction,
|
|
// its answer wrapped as Codex reads it.
|
|
func TestCodexPredictionOnMagpie(t *testing.T) {
|
|
for _, path := range []string{"/v1/responses", "/backend-api/codex/responses"} {
|
|
t.Run(path, func(t *testing.T) {
|
|
f := &fake{t: t, reply: predictionReply("```json\n{\"suggestion\": \" Commit it \"}\n```")}
|
|
setup(t, provider.Chat, f)
|
|
s := New()
|
|
rec := codexPredictionRequest(t, s, path, "fake/m1")
|
|
if text, _ := outputText(t, rec.Body.String()); rec.Code != 200 || text != `{"suggestion":"Commit it"}` || f.calls != 1 {
|
|
t.Fatalf("on magpie: %d %q, calls %d", rec.Code, rec.Body.String(), f.calls)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPredictionJSON(t *testing.T) {
|
|
for in, want := range map[string]struct {
|
|
out string
|
|
ok bool
|
|
}{
|
|
`{"suggestion":"Run it"}`: {`{"suggestion":"Run it"}`, true},
|
|
`{"suggestion":null}`: {`{"suggestion":null}`, true},
|
|
`{"suggestion":" "}`: {`{"suggestion":null}`, true},
|
|
"<think>hm</think>\n{\"suggestion\":\"Ship it\"}": {`{"suggestion":"Ship it"}`, true},
|
|
"Run the tests": {`{"suggestion":null}`, false},
|
|
`{"description":"x"}`: {`{"suggestion":null}`, false},
|
|
`{"suggestion":3}`: {`{"suggestion":null}`, false},
|
|
"": {`{"suggestion":null}`, false},
|
|
} {
|
|
if got, ok := predictionJSON(in); got != want.out || ok != want.ok {
|
|
t.Errorf("predictionJSON(%q) = %s %v, want %s %v", in, got, ok, want.out, want.ok)
|
|
}
|
|
}
|
|
}
|