Axel Burks on Discord (#feedback): his gateway in front of several Azure
accounts routes one conversation to one account by a session header, so
its reasoning can be read back. ${sessionId} was expanded only in Extra
body; in Headers it went out literally.
Now Sign expands ${sessionId} and ${threadId} in header values
(Provider.SentHeaders, conversation.go), with the id Extra body gets:
the agent's session header, else the id derived from the first user
message. The gateway carries it to Sign in the request's context
(WithConversation), on every API and count_tokens.
- ${threadId} is Codex's thread_id (turn metadata or thread-id header,
threadOf), which a subagent has of its own while sharing its lead's
session_id. No other agent names a thread; there it is the session's id.
Supported in Extra body too. Read only when a header or Extra body
names it (AsksThread).
- A request that is no conversation turn (model list, balance, embedding,
image, Azure/Kilo lists, a remote magpie's list) gets an id of its own,
"magpie-" and 12 random letters; Test and Detect use their
magpie-test-… id in headers and body alike. A literal placeholder is
never sent.
- Control characters (CR, LF, tab, NUL, DEL) are stripped from ids in a
header (headerSafe); with a CRLF id Go used to fail the request 502.
- GUI: Headers and Extra body hints name both placeholders, in en, zh,
zh-TW, ja, de. docs/subsystems/providers-accounts.md has a Headers row.
Run on mote at this head (rebased on 774baed6d):
- gofmt -l internal/: clean
- go vet ./... (darwin), GOOS=linux go vet -tags nogui ./..., GOOS=windows
go vet ./...: ok
- go test -tags nogui ./internal/provider ./internal/gateway -timeout 45m:
ok (121s, 570s)
- go test -tags nogui ./internal/gui: ok
- node --test header-session-hint (new, 5 languages x chromium/webkit),
extra-body, i18n-keys, reply-headers, provider-auth-header: 52/52
Broken by hand, each red, then restored: Sign sending raw p.Headers (the
literal goes out); no headerSafe (502 invalid header field value);
threadOf returning "" (TestThreadIDIsCodexThread); tested not putting its
id in ctx (TestHeadersTakeTheConversation); listHeaders raw (list sends
the literal); the de hint's suffix removed (chromium de red).
Not tried: a real Azure gateway or any real vendor; checked against local
httptest servers only. Codex's thread_id read from fixtures shaped like
Codex 0.162's metadata, not a live Codex run.
127 lines
5.2 KiB
Go
127 lines
5.2 KiB
Go
package gateway
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/tidwall/gjson"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// withHeaders gives the saved provider id the headers.
|
|
func withHeaders(t *testing.T, id string, h map[string]string) {
|
|
t.Helper()
|
|
p, err := provider.Find(id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p.Headers = h
|
|
if err := provider.Save(*p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// Axel Burks on Discord: a gateway in front of several Azure accounts
|
|
// routes one conversation to one account by a session id in a header,
|
|
// so its reasoning can be read back. ${sessionId} in a provider's header
|
|
// is the conversation's id, as in its extraBody — on every API, streamed
|
|
// or whole, relayed or translated, and on count_tokens.
|
|
func TestHeadersCarryTheSession(t *testing.T) {
|
|
head := map[string]string{"X-Session-Id": "${sessionId}", "X-Route": "acct/${sessionId}/${threadId}"}
|
|
for _, c := range []struct {
|
|
name, path, body string
|
|
proto provider.Protocol
|
|
reply, ctype string
|
|
}{
|
|
{"chat streamed", "/v1/chat/completions", `{"model":"m1","stream":true,"messages":[{"role":"user","content":"hi"}]}`, provider.Chat, chatReply, ""},
|
|
{"responses whole", "/v1/responses", `{"model":"m1","input":"hi"}`, provider.Responses, responsesReply, "application/json"},
|
|
{"messages", "/v1/messages", `{"model":"m1","max_tokens":16,"messages":[{"role":"user","content":"hi"}]}`, provider.Anthropic, anthropicReply(), ""},
|
|
{"messages to chat", "/v1/messages", `{"model":"m1","max_tokens":16,"messages":[{"role":"user","content":"hi"}]}`, provider.Chat, chatReply, ""},
|
|
{"count_tokens", "/v1/messages/count_tokens", `{"model":"m1","messages":[{"role":"user","content":"hi"}]}`, provider.Anthropic, `{"input_tokens":3}`, "application/json"},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
f := &fake{t: t, reply: c.reply, ctype: c.ctype}
|
|
setup(t, c.proto, f)
|
|
withHeaders(t, "fake", head)
|
|
code, body := postWith(t, c.path, c.body, map[string]string{"session_id": "sess-42"})
|
|
if code != 200 {
|
|
t.Fatalf("status %d: %s", code, body)
|
|
}
|
|
if got := f.head.Get("X-Session-Id"); got != "sess-42" {
|
|
t.Errorf("X-Session-Id %q", got)
|
|
}
|
|
if got := f.head.Get("X-Route"); got != "acct/sess-42/sess-42" {
|
|
t.Errorf("X-Route %q", got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Without a session header, the header's id is the one extraBody gets:
|
|
// derived from the first user message, the same every turn, never the
|
|
// literal.
|
|
func TestHeadersDerivedSessionMatchesExtraBody(t *testing.T) {
|
|
f := &fake{t: t, reply: chatReply}
|
|
setup(t, provider.Chat, f)
|
|
withExtraBody(t, "fake", `{"user":"${sessionId}"}`)
|
|
withHeaders(t, "fake", map[string]string{"X-Session-Id": "${sessionId}"})
|
|
ask := func(msgs string) string {
|
|
t.Helper()
|
|
if code, body := post(t, "/v1/chat/completions", `{"model":"m1","messages":`+msgs+`}`); code != 200 {
|
|
t.Fatalf("status %d: %s", code, body)
|
|
}
|
|
h := f.head.Get("X-Session-Id")
|
|
if b := gjson.GetBytes(f.got, "user").String(); h != b {
|
|
t.Errorf("header %q, body %q", h, b)
|
|
}
|
|
return h
|
|
}
|
|
one := ask(`[{"role":"user","content":"first"}]`)
|
|
again := ask(`[{"role":"user","content":"first"},{"role":"assistant","content":"OK"},{"role":"user","content":"next"}]`)
|
|
if !strings.HasPrefix(one, "magpie-") || one != again {
|
|
t.Errorf("ids %q %q", one, again)
|
|
}
|
|
}
|
|
|
|
// Codex names a thread apart from its session: a subagent shares its
|
|
// lead's session_id and has a thread_id of its own. ${threadId} is it, in
|
|
// headers and extraBody; ${sessionId} stays the session's.
|
|
func TestThreadIDIsCodexThread(t *testing.T) {
|
|
f := &fake{t: t, reply: responsesReply, ctype: "application/json"}
|
|
setup(t, provider.Responses, f)
|
|
withExtraBody(t, "fake", `{"metadata":{"thread":"${threadId}"}}`)
|
|
withHeaders(t, "fake", map[string]string{"X-Session-Id": "${sessionId}", "X-Thread-Id": "${threadId}"})
|
|
for _, c := range []struct {
|
|
name string
|
|
head map[string]string
|
|
want string
|
|
}{
|
|
{"turn metadata", map[string]string{"session_id": "root-1", "x-codex-turn-metadata": `{"session_id":"root-1","thread_id":"worker-1"}`}, "worker-1"},
|
|
{"thread-id header", map[string]string{"session-id": "root-1", "thread-id": "worker-2"}, "worker-2"},
|
|
{"none named", map[string]string{"session_id": "root-1"}, "root-1"},
|
|
} {
|
|
code, body := postWith(t, "/v1/responses", `{"model":"m1","input":"hi"}`, c.head)
|
|
if code != 200 {
|
|
t.Fatalf("%s: status %d: %s", c.name, code, body)
|
|
}
|
|
if s, th, b := f.head.Get("X-Session-Id"), f.head.Get("X-Thread-Id"), gjson.GetBytes(f.got, "metadata.thread").String(); s != "root-1" || th != c.want || b != c.want {
|
|
t.Errorf("%s: session %q, thread %q, body %q", c.name, s, th, b)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A session id is the agent's: a CR or LF in it never reaches a header,
|
|
// where it would split it or fail the request.
|
|
func TestHeaderSessionWithoutCRLF(t *testing.T) {
|
|
f := &fake{t: t, reply: chatReply}
|
|
setup(t, provider.Chat, f)
|
|
withHeaders(t, "fake", map[string]string{"X-Session-Id": "${sessionId}"})
|
|
code, body := postWith(t, "/v1/chat/completions", `{"model":"m1","messages":[{"role":"user","content":"hi"}]}`, map[string]string{"x-session-id": "a\r\nX-Evil: 1"})
|
|
if code != 200 {
|
|
t.Fatalf("status %d: %s", code, body)
|
|
}
|
|
if got := f.head.Get("X-Session-Id"); got != "aX-Evil: 1" || f.head.Get("X-Evil") != "" {
|
|
t.Errorf("X-Session-Id %q, headers %q", got, f.head)
|
|
}
|
|
}
|