Files
magpie/internal/gateway/dsh_last_convs_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

75 lines
3.6 KiB
Go

package gateway
import (
"crypto/sha256"
"encoding/hex"
"slices"
"testing"
)
func dshMessageID(msg string) string {
sum := sha256.Sum256([]byte(msg))
return "magpie-" + hex.EncodeToString(sum[:12])
}
// dsh's requests carry no session id, and a fork copies its source's
// opening: the user messages of the request's turn are what tell a request
// still answering which branch it is of, newest first, each hashed as the
// branch's own session file has it (the message with its text alone). The
// turn's messages are the run its newest user message ends: the one typed
// and the runtime context dsh sends after it, not the turn before.
func TestDshLastConvsAreTheTurnsUserMessages(t *testing.T) {
body := []byte(`{"model":"m","messages":[` +
`{"role":"system","content":"s"},` +
`{"role":"user","content":"old"},` +
`{"role":"assistant","content":"a"},` +
`{"role":"user","content":"typed"},` +
`{"role":"user","content":[{"type":"text","text":"context","cache_control":{"type":"ephemeral"}}]},` +
`{"role":"assistant","content":null,"tool_calls":[{"id":"c1","type":"function","function":{"name":"f","arguments":"{}"}}]},` +
`{"role":"tool","tool_call_id":"c1","content":"out"}]}`)
want := []string{dshMessageID(`{"role":"user","content":"context"}`), dshMessageID(`{"role":"user","content":"typed"}`)}
if got := dshLastConvs("dsh", body); !slices.Equal(got, want) {
t.Fatalf("ids %v, want %v", got, want)
}
// Messages' later steps end on the answer to a tool call, a user
// message of tool_result parts: the turn's messages are before it
msgs := []byte(`{"model":"m","messages":[` +
`{"role":"user","content":"typed"},` +
`{"role":"assistant","content":[{"type":"tool_use","id":"t1","name":"f","input":{}}]},` +
`{"role":"user","content":[{"type":"tool_result","tool_use_id":"t1","content":"out"}]}]}`)
if got, want := dshLastConvs("dsh", msgs), []string{dshMessageID(`{"role":"user","content":"typed"}`)}; !slices.Equal(got, want) {
t.Fatalf("after a tool_result: %v, want %v", got, want)
}
}
// Only dsh's requests are read that way, only text messages, and no more
// than a session keeps of its turn.
func TestDshLastConvsOnlyForDshTextMessages(t *testing.T) {
body := []byte(`{"messages":[{"role":"user","content":"new"}]}`)
if got := dshLastConvs("pi", body); got != nil {
t.Errorf("another agent: %v, want none", got)
}
if got := dshLastConvs("dsh", []byte(`{"messages":[{"role":"assistant","content":"a"}]}`)); got != nil {
t.Errorf("no user message: %v, want none", got)
}
if got := dshLastConvs("dsh", []byte(`{"messages":[{"role":"user","content":[{"type":"image","url":"x"}]}]}`)); got != nil {
t.Errorf("no text part: %v, want none", got)
}
many := []byte(`{"messages":[{"role":"user","content":"1"},{"role":"user","content":"2"},{"role":"user","content":"3"},{"role":"user","content":"4"},{"role":"user","content":"5"}]}`)
if got := dshLastConvs("dsh", many); len(got) != 4 || got[0] != dshMessageID(`{"role":"user","content":"5"}`) {
t.Errorf("five messages: %v, want the newest four", got)
}
}
// A done route is named by its reply: the history keeps none of its turn's
// ids, which would only grow every dsh route of the 30 days kept.
func TestHistoryKeepsNoTurnIDs(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
now := historyNoon(t)
saveRoute(Route{ID: 1, Time: now, Agent: "dsh", Conv: "magpie-open", ResponseID: "chatcmpl-1", LastConvs: []string{"magpie-msg"}, Done: true})
_, routes, _ := History(now.Format(dayForm))
if len(routes) != 1 || routes[0].ResponseID != "chatcmpl-1" || routes[0].LastConvs != nil {
t.Fatalf("history route %+v, want its reply and no turn ids", routes)
}
}