wangb on Discord: 「请求存档开启, 但是 systemone 的请求没有记录下来」. The Routing page then said "magpie 只保留最近 40 个请求的请求体和响应体… 要保留 每个请求的,请在「用量 › 请求」顶部打开「请求存档」", as if the archive were off. Why: only serve() made an archive wire (archiving). The two System One paths never reached it: - /v1/systemone, which an app sends (serveDecideTries, also used for a decision group), opened a Route but recorded no Call. It was in neither Recent calls nor the archive, so the Routing page could never open its bodies. - the question a routing group's Jev classifier asks for a turn (askJev / jevLevelled -> systemOne -> postDecide) only appended a usage row. What changed: - serveDecideTries captures its reply, makes the wire as serve does, names it on the Route from the start (Route.Archive), and records a Call (Route, status, error, fallbacks, usage, bodies) when it ends. So the call is in Recent calls and, with the archive on, in the archive. Its usage row carries Archive. - systemOne keeps the call magpie made itself through a new archiveOwn. It keeps the request as it went (method, path, the headers magpie signed it with, the body in the provider's shape) and the answer, with secrets scrubbed as for any call. Its "routing" usage row names the archive entry. postDecide became postDecideSent, which also returns the request it sent. archive's queueing moved into queueArchive, which both share. archiveID makes the name. - Routing's "Request and response" for a request in neither the ring nor the archive: with the archive on (gateway.archive.on in /api/providers) it says the archive is on and this request isn't in it (made while it was off, or not saved). It no longer says to turn the archive on. New string in zh, zh-TW, ja and de. - docs/subsystems/gui-shell.md says which calls are archived and which still aren't. Still not archived (unchanged): Codex's own backend (codexUpstreamOn), draw, embeddings and video side routes, and replies magpie writes itself (answeredHere). Masking makes no outgoing call. Chat classifiers and Codex titles to a model already go through serve and are archived. Run on mote at base4b407bb79, then rebased onto0285f572f, whose one commit changes only internal/gui/whatsnew*.go (nothing here reads it): - TestArchiveKeepsSystemOne and TestArchiveKeepsJevClassifier (new) pass. Broken by hand: with archiving(...) taken out of serveDecideTries, the first is red ("uploaded []"). With archiveOwn returning at once, the second is red (only the chat turn uploaded). - routing-bodies.test.cjs, chromium and webkit, every language, 440px: the new "archive on" case passes, and the archive-off case asserts the old text. Against the old client.js the new case is red in all 5 languages (chromium). i18n-keys passes. - gofmt -l clean. go vet ./... (darwin) and GOOS=windows go vet ./internal/gateway ./internal/gui are clean. GOOS=linux go vet -tags nogui is also clean for both (without nogui, wails' linux cgo doesn't build in a cross vet). - go test ./internal/gateway: ok except TestCodexPoolSaysTheClientsVersion (#4807: red in a whole package run on mote, passes alone). - go test ./internal/gui (an earlier base,834c6c7df): ok except TestMainWindowCorners (#3949, macOS 27). - The other routing-* GUI files (an earlier base,834c6c7df): the whole set was cut short when mote rebooted. Six files went red: routing-many-keys, order-unknown, row-swap, rule-drag, rule-remove and rules-line. Run alone they are red the same way at base834c6c7dfwithout this change (filed: #4082, #4627, #3799, #3789, #3096, #4585). Not tried: a real Jev or System One account. The tests use fake System One servers. Not tried either: an S3 bucket (memBucket), the local disk archive, or the GUI's whole suite.
140 lines
5.0 KiB
Go
140 lines
5.0 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"
|
|
)
|
|
|
|
// archivedOf reads every object in b back, by the call's provider.
|
|
func archivedOf(t *testing.T, b *memBucket) map[string][]Archived {
|
|
t.Helper()
|
|
b.mu.Lock()
|
|
defer b.mu.Unlock()
|
|
out := map[string][]Archived{}
|
|
for name, data := range b.objs {
|
|
var a Archived
|
|
if err := json.Unmarshal(data, &a); err != nil {
|
|
t.Fatalf("%s: %v", name, err)
|
|
}
|
|
if strings.Contains(string(data), archiveKey) || strings.Contains(string(data), "magpie-gateway-key") || strings.Contains(string(data), "Bearer k") {
|
|
t.Errorf("a secret in %s:\n%s", name, data)
|
|
}
|
|
out[a.Call.Provider] = append(out[a.Call.Provider], a)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// With the request archive on, a System One request an app sends magpie
|
|
// (/v1/systemone) is kept as a chat request is, and its route, its Recent
|
|
// call and its usage row name where (wangb on Discord: 请求存档开启, 但是
|
|
// systemone 的请求没有记录下来). Off, nothing is kept.
|
|
func TestArchiveKeepsSystemOne(t *testing.T) {
|
|
fresh(t)
|
|
jev := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
io.ReadAll(r.Body)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
io.WriteString(w, `{"model":"jev-latest","answers":{"intent":{"type":"choice","choice":"bug","confidence":0.9}},"usage":{"input_tokens":10,"output_tokens":2}}`)
|
|
}))
|
|
defer jev.Close()
|
|
if err := provider.Save(provider.Provider{ID: "load-a", Name: "A", Key: "ka-secret", Decide: jev.URL + "/v1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b := &memBucket{objs: map[string][]byte{}}
|
|
archiveTo(t, b)
|
|
s := New()
|
|
post := func() {
|
|
t.Helper()
|
|
body := `{"model":"load-a/jev-latest","questions":{"intent":{"type":"choice","criteria":{"bug":"a bug report"}}},"state":{"message":"it crashes, token ` + archiveKey + `"}}`
|
|
req := httptest.NewRequest("POST", "/v1/systemone", strings.NewReader(body))
|
|
req.Header.Set("Authorization", "Bearer magpie-gateway-key")
|
|
rec := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(rec, req)
|
|
if rec.Code != 200 || !strings.Contains(rec.Body.String(), `"bug"`) {
|
|
t.Fatalf("%d %s", rec.Code, rec.Body)
|
|
}
|
|
archivePending.Wait()
|
|
}
|
|
|
|
post()
|
|
if len(b.objs) != 0 || s.Recent()[0].Archive != "" {
|
|
t.Fatalf("archived while off: %v", keys(b.objs))
|
|
}
|
|
|
|
settings.Save(settings.Settings{RequestArchive: true})
|
|
post()
|
|
got := archivedOf(t, b)["load-a"]
|
|
if len(got) != 1 || len(b.objs) != 1 {
|
|
t.Fatalf("uploaded %v", keys(b.objs))
|
|
}
|
|
a := got[0]
|
|
c := s.Recent()[0] // the newest first
|
|
r := s.trace.routes[len(s.trace.routes)-1]
|
|
if c.Archive == "" || c.Archive != a.ID || r.Archive != a.ID || c.Route != r.ID {
|
|
t.Fatalf("call %q, route %q (%d/%d), archived %q", c.Archive, r.Archive, c.Route, r.ID, a.ID)
|
|
}
|
|
if a.Request.Path != "/v1/systemone" || !strings.Contains(a.Request.Body, "it crashes") || a.Request.Headers["Authorization"][0] != "[REDACTED]" ||
|
|
a.Response.Status != 200 || !strings.Contains(a.Response.Body, `"bug"`) || a.Call.Model != "load-a/jev-latest" || a.Call.Status != 200 {
|
|
t.Errorf("archived %+v", a)
|
|
}
|
|
n := 0
|
|
for _, row := range usage.Load(time.Time{}) {
|
|
if row.Provider == "load-a" && row.Archive != "" {
|
|
if row.Archive != a.ID {
|
|
t.Errorf("usage row names %q, archived %q", row.Archive, a.ID)
|
|
}
|
|
n++
|
|
}
|
|
}
|
|
if n != 1 {
|
|
t.Errorf("%d usage rows name the archive", n)
|
|
}
|
|
}
|
|
|
|
// With the request archive on, the System One question a routing group's
|
|
// Jev classifier asks for a turn, which magpie makes itself, is kept too:
|
|
// the request as it went, its key taken out, and the answer; its usage
|
|
// row, magpie's own, names it.
|
|
func TestArchiveKeepsJevClassifier(t *testing.T) {
|
|
s, _, _, j := jevved(t, "", provider.Rule{Use: "b/big", Intent: "debugging"})
|
|
j.choice, j.sure = "debugging", 0.7
|
|
b := &memBucket{objs: map[string][]byte{}}
|
|
archiveTo(t, b)
|
|
settings.Save(settings.Settings{RequestArchive: true})
|
|
if _, r := postOK(t, s, "s1", chat("why does this crash?", nil, 0, "")); r.Rule.Classified.Intent != "debugging" {
|
|
t.Fatalf("classified %+v", r.Rule.Classified)
|
|
}
|
|
archivePending.Wait()
|
|
got := archivedOf(t, b)
|
|
jev := got["ts"]
|
|
if len(jev) != 1 || len(got["b"]) != 1 {
|
|
t.Fatalf("uploaded %v", keys(b.objs))
|
|
}
|
|
a := jev[0]
|
|
if a.Request.Method != "POST" || a.Request.Path != "/v1/systemone" || a.Request.Headers["Authorization"][0] != "[REDACTED]" ||
|
|
!strings.Contains(a.Request.Body, "why does this crash?") || !strings.Contains(a.Response.Body, "debugging") ||
|
|
a.Response.Status != 200 || a.Call.Kind != "routing" || a.Call.Model != "ts/jev-latest" || a.Call.Archive != a.ID {
|
|
t.Errorf("archived %+v", a)
|
|
}
|
|
n := 0
|
|
for _, row := range usage.Load(time.Time{}) {
|
|
if row.Provider == "ts" {
|
|
if row.Archive != a.ID || row.Kind != "routing" {
|
|
t.Errorf("Jev's row %+v, archived %q", row, a.ID)
|
|
}
|
|
n++
|
|
}
|
|
}
|
|
if n != 1 {
|
|
t.Errorf("%d rows of Jev's", n)
|
|
}
|
|
}
|