Files
magpie/internal/sessions/calls_dsh_test.go
T
yetone 814a5ed73c agent: dsh asks each of magpie's models on its own API, a route per protocol (01huadalang on Discord)
dsh's llm-pi-ai sets one api for a whole route, so magpie's single
"magpie" route asked every model over Chat Completions. A GPT served on
Responses, or a Claude served on Messages, was translated from Chat, and
its sealed reasoning was lost between tool calls. 01huadalang asked for a
group per protocol.

What changed:
- dshPutRoute writes up to three routes from each model's catalog APIs
  (dshRouteOf): magpie (openai-completions, as before, for Chat models,
  Gemini-native ones, groups and agent-bridged sign-ins),
  magpie-responses (openai-responses, gateway /v1) and magpie-anthropic
  (anthropic-messages, the gateway root, forceAdaptiveThinking where
  gateway.AdaptiveThinking). A route left with no models is taken out.
  An api the user picked for a route in dsh's Models page is kept, and so
  are keys they added to it.
- The start (agent-default-model) names its model's route on a pick, an
  effort change, and the home layer's mirror. Sync moves a start on one of
  magpie's routes to the route its model is now on (dshStartOnItsRoute),
  keeping its mark, effort and other keys. A start on the user's own
  route stays.
- dshCheck checks each route's baseURL against its own api, and asks the
  route the start names to list its model.
- Disconnect, dshOurs, dshKeepPick and dshFillNewProfiles know all three
  routes. sessions' dshGatewayProvider counts dsh sessions on any of
  them as magpie's.
- docs/subsystems/agent-wiring.md: a line on the routes.

Known limit: a dsh session resumed on a model that has since moved
routes asks for the model to be picked again once, since dsh finds a
model by route and id.

Run:
- go test -tags nogui ./internal/agent (whole package, default order)
  and ./internal/sessions -run TestDsh: pass. New tests
  TestDshRoutesAModelOnItsNativeAPI,
  TestDshSyncMovesAStartToItsModelsRoute,
  TestDshCheckSaysAStartOnAnotherRoutesModel and
  TestDshGatewayProviderTakesEveryRouteOfMagpies. With dshRouteOf forced
  back to the single route, the three agent tests go red.
- Real dsh (headless, sandbox HOME and DSH_HOME) on patches magpie wrote,
  pointed at a fake gateway: the start on magpie answered from
  /v1/chat/completions, the one on magpie-responses from /v1/responses
  (reasoning.effort high), and the one on magpie-anthropic sent
  /v1/messages with thinking adaptive and output_config.effort high. dsh
  accepted all three routes. Its Anthropic turn then ended on my fake's
  SSE, not on the wiring.
- gofmt -l clean. go vet -tags nogui for darwin, GOOS=linux and
  GOOS=windows on internal/agent and internal/sessions.
2026-10-11 04:52:26 +08:00

132 lines
4.8 KiB
Go

package sessions
import (
"testing"
"time"
)
// dshCalls are the DeepSeek Harness calls Calls gives.
func dshCalls(t *testing.T) []Call {
t.Helper()
var out []Call
for _, c := range Calls(time.Time{}) {
if c.Agent == "dsh" {
out = append(out, c)
}
}
return out
}
// The Sessions page has read dsh's session files all along; the usage ledger
// had not, so dsh's tokens never reached the Usage page — which showed only
// what magpie's own gateway served. Every assistant/message is one call, with
// the model and the usage of the request that answered with it.
func TestDshCalls(t *testing.T) {
setupAgents(t)
cs := dshCalls(t)
by := map[string][]Call{}
for _, c := range cs {
by[c.Session] = append(by[c.Session], c)
}
// dsh-a's subagent (dsh-b) counts in the session it ran under, as
// OpenCode's does; dsh-c and dsh-d are forks, whose seeded events are
// the session they came from and are not spent again here
if len(cs) != 6 {
t.Fatalf("want 6 dsh calls, got %d: %+v", len(cs), cs)
}
// a call dsh sent through magpie's own gateway is the gateway's record
// to keep, as OpenCode's and ZCode's are: dsh sends the gateway no
// session header, so the two records cannot be paired afterwards (the
// token counts differ call to call) and the call would be counted
// twice — once by the gateway with the provider and account that
// really answered, once here as a nameless local session. dsh-gw holds
// one such call and one of its own.
// dsh-gw's own call is kept; only its gateway one is left out
if gw := by["dsh-gw"]; len(gw) != 1 || gw[0].Upstream != "jieyue" || gw[0].Tokens != (Tokens{30, 7, 0, 0, 0}) {
t.Fatalf("dsh-gw's own call: %+v", gw)
}
for _, c := range cs {
if c.Upstream == "magpie" || c.Upstream == "dial" {
t.Fatalf("a call through magpie's gateway was counted here: %+v", c)
}
if c.Tokens == (Tokens{700, 90, 5000, 0, 0}) {
t.Fatalf("the gateway's own call was counted here: %+v", c)
}
}
if len(by["dsh-a"]) != 3 || len(by["dsh-c"]) != 1 || len(by["dsh-d"]) != 1 {
t.Fatalf("by session: %d a, %d c, %d d (want 3, 1, 1)", len(by["dsh-a"]), len(by["dsh-c"]), len(by["dsh-d"]))
}
// the 999s are the older session.jsonl beside dsh-a's newest, not read
for _, c := range cs {
if c.Input == 999 {
t.Fatalf("the older file beside the newest was read: %+v", c)
}
}
// the seeded copy of dsh-a's first reply, which dsh-c and dsh-d each
// carry: spent once, in dsh-a
seeded := 0
for _, c := range cs {
if c.Tokens == (Tokens{100, 20, 1000, 0, 0}) {
seeded++
}
}
if seeded != 1 {
t.Fatalf("a seeded reply counted %d times, want once", seeded)
}
a := by["dsh-a"]
// newest first. dsh names the model it was told to use, and its reply's
// replay state names the one the vendor answered with when the two
// differ: Call.Model is what answered (as Claude Code's file has it),
// Call.Requested what was asked for, and Call.Upstream the provider the
// file recorded — all three are what the Requests table's columns read.
if a[0].Model != "deepseek-v4-flash-sg" || a[0].Requested != "deepseek-v4-flash" ||
a[0].Upstream != "workbuddy-ai" || a[0].Tokens != (Tokens{10, 5, 200, 0, 0}) {
t.Fatalf("dsh-a's newest call (asked deepseek-v4-flash, answered deepseek-v4-flash-sg): %+v", a[0])
}
if a[1].Tokens != (Tokens{7, 3, 0, 0, 0}) {
t.Fatalf("the subagent's call: %+v", a[1])
}
// answered with the very model asked for: not a swap, and the two agree
if a[2].Model != "deepseek-v4-pro" || a[2].Requested != "deepseek-v4-pro" ||
a[2].Upstream != "deepseek" || a[2].Tokens != (Tokens{100, 20, 1000, 0, 0}) ||
!a[2].Time.Equal(ms(1790503205000)) {
t.Fatalf("dsh-a's first call: %+v", a[2])
}
if c := by["dsh-c"][0]; c.Tokens != (Tokens{50, 10, 0, 0, 0}) {
t.Fatalf("dsh-c's own call: %+v", c)
}
if d := by["dsh-d"][0]; d.Tokens != (Tokens{1, 2, 0, 0, 0}) {
t.Fatalf("dsh-d's own call: %+v", d)
}
}
// A session that has not changed is not read again: what was read is kept,
// and a restart keeps it too.
func TestDshCallsKept(t *testing.T) {
setupAgents(t)
first := dshCalls(t)
if len(first) == 0 {
t.Fatal("no dsh calls read")
}
resetCalls()
again := dshCalls(t)
if len(again) != len(first) {
t.Fatalf("after a restart: %d calls, want %d", len(again), len(first))
}
}
// magpie writes a route per protocol into dsh (agent.dshKinds): a call on
// any of them went through the gateway and is the gateway's to count, while
// a route of the user's named like one is not taken for it.
func TestDshGatewayProviderTakesEveryRouteOfMagpies(t *testing.T) {
for p, want := range map[string]bool{
"magpie": true, "magpie-responses": true, "magpie-anthropic": true, "dial": true,
"magpie-mine": false, "jieyue": false, "": false,
} {
if got := dshGatewayProvider(dshSource{Provider: p}); got != want {
t.Errorf("%q: %v, want %v", p, got, want)
}
}
}