Files
magpie/internal/gateway/dsh_conv_test.go
T
yetoneandFlandern1211 16e79fc6b0 dsh: a request still answering shows under its fork, not its source (#5542)
The Routing page's live card for a dsh fork's request showed under the
source until the reply was written: it has no reply id yet, and the
opening it is keyed by is the one the fork copied. The messages of the
request's turn are in the branch's own session file before the request
goes out, so they name it:

- gateway: Route.LastConvs, the ids of the user messages the request's
  turn carries, newest first (dshLastConvs, dsh only, at most 4);
- sessions: TurnConvs, the ids of the newest turn's user messages, reset
  at turn/start, not kept from a fork's seed, at most 4 (dshTurnKept);
  dsh's parse version goes to 3;
- gui: ownerBy asks the reply id, then the turn's messages, then the
  opening (ownerOfRoute on the Routing page).

Changed from Flandern1211's patch (957caf84) in review:
- the turn lookup has the reply id's guard (forkedFrom): a "继续" in
  another conversation's newest turn no longer takes a route;
- in dsh's captured requests (sessions/testdata/dsh-gateway) the newest
  user message is the runtime context, the same for every session of a
  folder, so it names no one: the gateway reads the whole run of the
  turn's user messages (typed + context), and passes over Messages'
  tool_result steps, where the patch returned nil;
- looseKeys asks for a turn's keys only while the route answers and for
  convsKept, not for every older route: those keys are never listed
  once a newer turn starts, and would relist every agent's sessions on
  each 2s poll;
- one spelling per message and at most 4 kept (a v3 file has no
  turn/start, so TurnConvs grew with every message, 3 ids each);
- no TurnConvs on Context's records (it counts done calls, named by
  reply), LastConvs dropped from the history (saveRoute) and from what
  the page is sent;
- dshLastConvs reads messages by their members, not decoded: 0.49 ms
  on a 2.2 MB body of 800 tool steps (convOf 0.30 ms), 1.10 ms before.

Run on mote at 2be7aaeea (this change on 9730e0199; then rebased over
ec7a18d12, which changes GUI strings and .cjs tests only, no Go):
- go test -count=1 -timeout 45m ./internal/sessions/ ./internal/gui/
  ./internal/gateway/ ./internal/agent/: ok (sessions 30s, gui 158s,
  gateway 952s, agent 262s).
- CGO_ENABLED=0 GOOS=linux and GOOS=windows go vet -tags nogui of the
  three packages: clean; gofmt -l internal: clean.
- No GUI asset changed, so no .cjs run.

Broken by hand, each red: LastConvs not set in serve
(TestDshRoutesCarryTheSessionConvs on the real captured requests), no
turn/start reset, no cap, no guard on the turn lookup, no looseKeys
bound, no LastConvs keys in looseKeys (end-to-end Routing test stays on
the source), no strip of the page's routes, no tool_result skip, no
strip in saveRoute.

Not tried: a real dsh fork live (no dsh session files on this Mac or
mote); the contributor measured their version on magpie web.

Co-authored-by: Flandern1211 <163982805+Flandern1211@users.noreply.github.com>
2026-11-08 22:25:58 +08:00

183 lines
6.3 KiB
Go

package gateway
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/sessions"
)
// Kumo31 on Discord: dsh's chat-completions and messages routes send no
// session id, so the gateway keys each request by its first prompt's hash
// (conversationID), and the Context page listed dsh's sessions as
// "magpie-a…" where the Sessions page has their titles. The requests here
// are the ones real dsh 0.2.0-rc.2 sent for the two sessions beside them,
// a turn and its title call on each route, headers and body as they came:
// every one's conversation id is one the session's file gives (Convs).
const dshFixture = "../sessions/testdata/dsh-gateway"
const dshChat, dshMsgs = "session-508e7b0a-37c0-41f7-ac62-e48ab3146111", "session-8d93476d-ca34-461c-9e8e-7031c1dedfff"
// dshRequests are the captured requests, by the session each was sent in.
var dshRequests = map[string]string{
"completions-turn.json": dshChat,
"completions-title.json": dshChat,
"messages-turn.json": dshMsgs,
"messages-title.json": dshMsgs,
}
// dshFixtureSessions lists the fixture's dsh sessions, by id.
func dshFixtureSessions(t *testing.T) map[string]sessions.Session {
t.Helper()
// dsh's layout: $DSH_HOME/sessions/<project>/<session>/session.v4.jsonl
home := t.TempDir()
if err := os.CopyFS(filepath.Join(home, "sessions"), os.DirFS(filepath.Join(filepath.FromSlash(dshFixture), "sessions"))); err != nil {
t.Fatal(err)
}
t.Setenv("DSH_HOME", home)
sessions.Reset()
t.Cleanup(sessions.Reset)
byID := map[string]sessions.Session{}
for _, s := range sessions.List(0) {
if s.Agent == "dsh" {
byID[s.ID] = s
}
}
if len(byID) != 2 || byID[dshChat].Title == "" || byID[dshMsgs].Title == "" {
t.Fatalf("sessions %+v", byID)
}
return byID
}
// dshRequest is a captured request's path, headers and body as dsh sent them.
func dshRequest(t *testing.T, name string) (string, http.Header, []byte) {
t.Helper()
b, err := os.ReadFile(filepath.Join(filepath.FromSlash(dshFixture), "requests", name))
if err != nil {
t.Fatal(err)
}
head, body, _ := bytes.Cut(b, []byte("\n"))
var captured struct {
Path string `json:"path"`
Headers map[string]string `json:"headers"`
}
if err := json.Unmarshal(head, &captured); err != nil {
t.Fatal(err)
}
h := http.Header{}
for k, v := range captured.Headers {
h.Set(k, v)
}
return captured.Path, h, body
}
func TestDshSessionConvsAreTheGatewayIDs(t *testing.T) {
fresh(t)
byID := dshFixtureSessions(t)
for name, id := range dshRequests {
_, h, body := dshRequest(t, name)
conv := conversationID(h, body)
if !strings.HasPrefix(conv, "magpie-") {
t.Fatalf("%s: dsh sent a session id now (%s); the join isn't needed for it", name, conv)
}
if !slices.Contains(byID[id].Convs, conv) {
t.Errorf("%s: %s is not among %s's %v", name, conv, id, byID[id].Convs)
}
for other, s := range byID {
if other != id && slices.Contains(s.Convs, conv) {
t.Errorf("%s: %s is %s's too", name, conv, other)
}
}
t.Logf("%s %s", name, conv)
}
}
// The same requests sent through the gateway are traced under those ids:
// what reaches conversationID is the body dsh sent, not one rewritten on
// the way in.
func TestDshRoutesCarryTheSessionConvs(t *testing.T) {
fresh(t)
byID := dshFixtureSessions(t)
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.Copy(io.Discard, r.Body)
w.Header().Set("Content-Type", "application/json")
if strings.HasSuffix(r.URL.Path, "/messages") {
io.WriteString(w, `{"id":"msg_1","type":"message","role":"assistant","model":"m1","content":[{"type":"text","text":"ok"}],"stop_reason":"end_turn","usage":{"input_tokens":9,"output_tokens":1}}`)
return
}
io.WriteString(w, `{"id":"c1","object":"chat.completion","model":"m1","choices":[{"index":0,"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}],"usage":{"prompt_tokens":9,"completion_tokens":1}}`)
}))
t.Cleanup(up.Close)
if err := provider.Save(provider.Provider{ID: "fake", Name: "FAKE", Key: "k", Models: []string{"m1"}, Chat: up.URL + "/v1", Anthropic: up.URL}); err != nil {
t.Fatal(err)
}
s := New()
for name, id := range dshRequests {
path, h, body := dshRequest(t, name)
// only the model is changed, to one this gateway has
body = bytes.Replace(body, []byte(`"model":"group/flash"`), []byte(`"model":"fake/m1"`), 1)
body = bytes.Replace(body, []byte(`"stream":true`), []byte(`"stream":false`), 1)
r := httptest.NewRequest("POST", path, bytes.NewReader(body))
r.Header = h.Clone()
r.Header.Del("Content-Length")
r.Header.Del("Accept-Encoding")
if strings.HasSuffix(name, "-turn.json") {
// package agent names dsh by its User-Agent, which this
// package's tests don't register: its token says the same
r.Header.Set("Authorization", "Bearer "+TokenFor("dsh"))
}
w := httptest.NewRecorder()
s.Handler().ServeHTTP(w, r)
if w.Code != 200 {
t.Fatalf("%s: %d %s", name, w.Code, w.Body)
}
rs := s.Trace(context.Background(), 0, 0).Routes
var last Route
for _, x := range rs {
if x.ID > last.ID {
last = x
}
}
// (its agent is package agent's to name: "dsh" by its User-Agent,
// which gui's TestContextDshSessionTitles checks)
if last.Session != "" || !slices.Contains(byID[id].Convs, last.Conv) {
t.Errorf("%s: route session %q conv %q, not one of %s's %v", name, last.Session, last.Conv, id, byID[id].Convs)
}
if !strings.HasSuffix(name, "-turn.json") {
continue
}
// the messages of its turn, as its own file keeps them, name the
// session while the request is still answering (gui's ownerLast):
// the one typed is that session's alone, the runtime context dsh
// sends after it both sessions' of the folder
own := 0
for _, c := range last.LastConvs {
if !slices.Contains(byID[id].TurnConvs, c) {
t.Errorf("%s: turn message %s is not among %s's %v", name, c, id, byID[id].TurnConvs)
}
alone := true
for other, s := range byID {
if other != id && slices.Contains(s.TurnConvs, c) {
alone = false
}
}
if alone {
own++
}
}
if len(last.LastConvs) != 2 || own != 1 {
t.Errorf("%s: turn messages %v, %d of them %s's alone; want the typed one and the runtime context", name, last.LastConvs, own, id)
}
}
}