GET /api/providers carried up to 40 recent gateway calls with their request and response previews (up to 256 KB each) in gateway.calls, and every provider write returned the same state. The Providers page never shows them; only an opened row on Gateway does. With 40 calls of ~300 KB each, /api/providers was 21,019,635 bytes on origin/main. Taken from xiaohei-info's patch (bb26b802, attached to #1521 as external PRs are closed): providersState drops the bodies from the calls it sends; GET /api/gateway/call returns one call's previews and truncation flags with Cache-Control: no-store, 404 once it has left the ring; the page fetches on open, dedupes a pending read, keeps loaded bodies across refreshes, drops them with their call, shows "Fetching…" and a "Try again" on a failed read; the strings in zh, zh-TW, ja and de; the Go and Playwright tests; the gui-shell note. Changed from the patch: a call is asked for by a number the gateway gives it in the ring (Call.ID, Server.RecentCall), not by time, agent and model, which two calls of one agent to one model can share. The page keys a row by that id too (callRow), so two such calls no longer open, or show the bodies of, each other. The tests ask with ids, and the Playwright fixture gives two calls the same time, agent and model. The route is on the same mux as the rest of the API, so magpie web's key guard covers it, and the gateway's own port doesn't serve it. The bodies are the ones the ring kept, masked as before (redacted runs before capture). Nothing else reads gateway.calls' bodies: Routing reads /api/gateway/trace, and the tray, TUI, CLI and /api/state don't read the ring. Run: - Real use: magpie web built from origin/main and from this commit, sandbox HOME, a local upstream answering 300 KB, 40 calls of 300 KB. /api/providers 21,019,635 bytes before, 45,525 after (calls: 14,503 bytes). Opening a row in Chromium and WebKit fetched /api/gateway/call once (524,412 bytes) and showed both bodies with their "first 256 KB" marks. Without the key: 401, also for a rebound Host; on the gateway port: 404; an evicted id: 404. - TestRecentCallBodiesOnDemand fails with the bodies left in providersState ("providers must keep call metadata without sending bodies"), and with RecentCall ignoring the id ("the second call's detail isn't its own"). - recent-call-bodies.test.cjs fails on main's app.js, and with callRow keyed by time|agent|model again. - GUI: gateway-fold, json-tree, phone-rows, recent-calls-more, request-archive, recent-call-bodies, sse-body: 24/24 in Chromium and 24/24 in WebKit (en, zh, zh-TW, ja, de). - gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui on internal/gui and internal/gateway. - Full GUI suite: 5225 tests, 71 failures under load ~200, none in a gateway or recent-call file. The 20 files with failures, rerun on this commit and on origin/main: 54 and 58 fail; what fails on one only (Sessions checkbox colour, Sessions paging scroll, account dragging, ZCode block) moves between runs on both. - go test -tags nogui ./...: every package ok on the rebased head. Co-authored-by: xiaohei-info <14011749+xiaohei-info@users.noreply.github.com>
128 lines
4.9 KiB
Go
128 lines
4.9 KiB
Go
package gui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
func TestRecentCallBodiesOnDemand(t *testing.T) {
|
|
sandboxHome(t)
|
|
long := strings.Repeat("body", 70<<10)
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
io.WriteString(w, `{"choices":[{"message":{"role":"assistant","content":"`+long+`"}}]}`)
|
|
}))
|
|
defer up.Close()
|
|
if err := provider.Save(provider.Provider{ID: "recent", Name: "Recent", Chat: up.URL, Key: "fixture"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := gateway.New()
|
|
was := served.Load()
|
|
served.Store(s)
|
|
t.Cleanup(func() { served.Store(was) })
|
|
mux := http.NewServeMux()
|
|
providerRoutes(mux, nil)
|
|
get := func(path string) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
w := httptest.NewRecorder()
|
|
mux.ServeHTTP(w, httptest.NewRequest("GET", path, nil))
|
|
return w
|
|
}
|
|
query := func(c gateway.Call) string {
|
|
return "/api/gateway/call?" + url.Values{"id": {strconv.FormatUint(c.ID, 10)}}.Encode()
|
|
}
|
|
ask := func(content string) gateway.Call {
|
|
t.Helper()
|
|
w := httptest.NewRecorder()
|
|
r := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"recent/m","messages":[{"role":"user","content":"`+content+`"}]}`))
|
|
r.Header.Set("User-Agent", "codex")
|
|
s.Handler().ServeHTTP(w, r)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("gateway: %d %.200s", w.Code, w.Body)
|
|
}
|
|
return s.Recent()[0]
|
|
}
|
|
first := ask(long)
|
|
w := get("/api/providers")
|
|
var state providersJSON
|
|
if err := json.Unmarshal(w.Body.Bytes(), &state); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if w.Code != http.StatusOK || len(state.Gateway.Calls) != 1 {
|
|
t.Fatalf("summary: %d, %d calls", w.Code, len(state.Gateway.Calls))
|
|
}
|
|
if c := state.Gateway.Calls[0]; c.RequestBody != "" || c.ResponseBody != "" || !c.Time.Equal(first.Time) || c.Model != first.Model || c.Status != first.Status {
|
|
t.Fatal("providers must keep call metadata without sending bodies")
|
|
}
|
|
// Saves return the same summary, not another copy of every body.
|
|
w = httptest.NewRecorder()
|
|
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/providers/arrange", strings.NewReader(`{"order":["recent"]}`)))
|
|
if w.Code != http.StatusOK || strings.Contains(w.Body.String(), `"requestBody"`) || strings.Contains(w.Body.String(), `"responseBody"`) {
|
|
t.Fatal("a provider write returned call bodies")
|
|
}
|
|
w = get(query(first))
|
|
var detail gateway.Call
|
|
if err := json.Unmarshal(w.Body.Bytes(), &detail); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if w.Code != http.StatusOK || detail.RequestBody != first.RequestBody || detail.ResponseBody != first.ResponseBody || !detail.RequestTruncated || !detail.ResponseTruncated || w.Header().Get("Cache-Control") != "no-store" {
|
|
t.Fatal("the detail must keep both captured previews and truncation flags")
|
|
}
|
|
if s.Recent()[0].RequestBody == "" || s.Recent()[0].ResponseBody == "" {
|
|
t.Fatal("building a summary changed the gateway's captured bodies")
|
|
}
|
|
guard := webGuard("session", "fixture", 0, mux)
|
|
w = httptest.NewRecorder()
|
|
guard.ServeHTTP(w, httptest.NewRequest("GET", query(first), nil))
|
|
if w.Code != http.StatusUnauthorized {
|
|
t.Fatal("body detail bypassed the web session")
|
|
}
|
|
// the next call, the same agent's to the same model, is told apart
|
|
// from the first by its id, not by when it came (#1521)
|
|
second := ask("second " + long)
|
|
if second.ID == first.ID {
|
|
t.Fatalf("two calls share id %d", first.ID)
|
|
}
|
|
if err := json.Unmarshal(get(query(second)).Body.Bytes(), &detail); err != nil || !strings.Contains(detail.RequestBody, "second") {
|
|
t.Fatal("the second call's detail isn't its own")
|
|
}
|
|
if err := json.Unmarshal(get(query(first)).Body.Bytes(), &detail); err != nil || strings.Contains(detail.RequestBody, "second") {
|
|
t.Fatal("the first call's detail isn't its own")
|
|
}
|
|
for _, path := range []string{"/api/gateway/call", "/api/gateway/call?id=x", "/api/gateway/call?id=999999", "/api/gateway/call?time=" + url.QueryEscape(first.Time.Format(time.RFC3339Nano))} {
|
|
if w := get(path); w.Code != http.StatusNotFound {
|
|
t.Fatalf("unknown call: %d", w.Code)
|
|
}
|
|
}
|
|
for range 40 {
|
|
ask(long)
|
|
}
|
|
w = get("/api/providers")
|
|
if err := json.Unmarshal(w.Body.Bytes(), &state); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
summary, _ := json.Marshal(state.Gateway.Calls)
|
|
captured, _ := json.Marshal(s.Recent())
|
|
if len(state.Gateway.Calls) != 40 || len(summary)*100 >= len(captured) {
|
|
t.Fatalf("recent summaries: %d bytes, captured calls: %d bytes", len(summary), len(captured))
|
|
}
|
|
t.Logf("40 calls: %d bytes of metadata instead of %d bytes with bodies", len(summary), len(captured))
|
|
if w := get(query(first)); w.Code != http.StatusNotFound {
|
|
t.Fatalf("an evicted call returned %d", w.Code)
|
|
}
|
|
served.Store(nil)
|
|
if w := get(query(first)); w.Code != http.StatusNotFound {
|
|
t.Fatalf("a gateway served elsewhere returned %d", w.Code)
|
|
}
|
|
}
|