Files
magpie/internal/agent/session_header_test.go
T
yetone 7a5536793e agent: Pi's and omp's turns are recorded by "Record gateway conversations", as OpenCode's are (akic404 on Discord)
akic404 runs a public magpie as the upstream and a local magpie that reaches
it as a remote magpie; with recording on, only OpenCode's conversations were
kept, on either side. The gateway records a turn only under the session the
client names (it never makes one up), and the configs magpie wrote for Pi and
omp didn't have them name one:

- Pi (1.1.0) sends x-session-affinity on Chat and Anthropic Messages only when
  the provider's compat says sendSessionAffinityHeaders (Responses sends
  session_id anyway). magpie's Pi provider block now sets it. A value the
  user set in that block is kept through a sync and a pick (piBlockKept, for
  pi, OmO and the aside's native Pi path).
- omp (18.6.1) sends its session header on Chat/Responses only to OpenAI
  itself, unless the provider's compat.promptCacheSessionHeader names one
  (pi-catalog 16.0.6+). magpie's omp provider entry now names
  x-session-affinity for omp 16.0.6 and later; an older or unknown version
  is left as it was.

withCaller already passes the session on to a remote magpie, so naming it
once is enough for both sides to record.

Run:
- Header dump (fake upstream LLM): real pi and omp with these provider
  blocks send x-session-affinity / session_id on every API; without them,
  nothing on Chat/Messages.
- End to end in sandbox HOMEs: an upstream (magpie web --gateway) and a
  downstream (serve, remote magpie to the upstream), both recording; the new
  binary wrote ~/.pi/agent/models.json and ~/.omp/agent/models.yml through
  `magpie pi …` / `magpie omp …`; real pi (Chat and Messages models) and real
  omp turns were recorded under gateway-conversations on both sides.
- TestPiNamesItsSessionToTheGateway and TestOmpNamesItsSessionToTheGateway
  fail on the old code (no compat; Compat field missing) and pass now.
- go test ./internal/agent (-timeout 40m), gofmt -l, go vet for darwin,
  GOOS=linux and GOOS=windows on internal/agent.
2026-10-11 01:13:52 +08:00

58 lines
2.3 KiB
Go

package agent
import (
"path/filepath"
"testing"
)
// akic404 on Discord: with "Record gateway conversations" on, OpenCode's
// turns were kept and Pi's and omp's weren't. The gateway records a turn
// only under the session the agent names, and Pi (1.1.0) names it on Chat
// and Anthropic's Messages API only when its provider's compat says
// sendSessionAffinityHeaders, omp (18.6.1) on Chat and Responses only to
// OpenAI itself. Both were run against a header dump: with the provider
// block magpie writes, Pi sends x-session-affinity on Chat and Messages
// and session_id on Responses, omp x-session-affinity on Chat and
// Responses (and X-Claude-Code-Session-Id on Messages, as before).
func TestPiNamesItsSessionToTheGateway(t *testing.T) {
home := syncHome(t)
piModels := filepath.Join(home, ".pi", "agent", "models.json")
if err := pi(home).Fields[0].Set("magpie/relay/glm-4.6"); err != nil {
t.Fatal(err)
}
compat, _ := providerAt(t, piModels, "providers")["compat"].(map[string]any)
if compat["sendSessionAffinityHeaders"] != true {
t.Fatalf("Pi isn't told to name its session:\n%s", readFile(piModels))
}
// one the user turned off stays off, through a sync and a pick
writeFile(t, piModels, `{"providers":{"magpie":{"name":"magpie","baseUrl":"http://127.0.0.1:3425/v1","api":"openai-completions","apiKey":"magpie","compat":{"sendSessionAffinityHeaders":false,"supportsStore":false},"models":[]}}}`)
SyncCatalog()
if err := pi(home).Fields[0].Set("magpie/relay/glm-4.6"); err != nil {
t.Fatal(err)
}
compat, _ = providerAt(t, piModels, "providers")["compat"].(map[string]any)
if compat["sendSessionAffinityHeaders"] != false || compat["supportsStore"] != false {
t.Fatalf("the user's own compat was written over:\n%s", readFile(piModels))
}
}
func TestOmpNamesItsSessionToTheGateway(t *testing.T) {
syncHome(t)
for _, c := range []struct {
version string
want string
}{
{"18.6.1", "x-session-affinity"},
{"16.0.6", "x-session-affinity"},
// before 16.0.6 omp had no promptCacheSessionHeader; an omp whose
// version isn't known is taken for an older one
{"16.0.5", ""},
{"", ""},
} {
if got := ompProviderAt("http://127.0.0.1:3425", c.version).Compat["promptCacheSessionHeader"]; got != c.want {
t.Errorf("omp %q: promptCacheSessionHeader %q, want %q", c.version, got, c.want)
}
}
}