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.
64 lines
2.6 KiB
Go
64 lines
2.6 KiB
Go
package gateway
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/tidwall/gjson"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// openCodeSeed names the conversation a request to OpenCode's gateway is a
|
|
// turn of, for its x-opencode-session (provider.OpenCodeClient): the
|
|
// client's own x-opencode-session when it sent one, else the session the
|
|
// client names (X-Magpie-Session, Codex's session_id, Claude Code's
|
|
// session, Pi's affinity: sessionOf), else the body's prompt_cache_key
|
|
// (Codex's thread), else the conversation's first user message
|
|
// (conversationID). OpenCode itself sends one session id for every turn of
|
|
// a session (packages/core/src/session/affinity.ts), and Zen and Go route
|
|
// a session to the backend that has its prompt cached: every turn of one
|
|
// conversation names the same, and two conversations never one. Without
|
|
// the cache key, two of Codex's conversations in one folder, which open on
|
|
// the same environment message, were one session.
|
|
func openCodeSeed(in http.Header, body []byte) string {
|
|
if v := strings.TrimSpace(in.Get("x-opencode-session")); v != "" {
|
|
return v
|
|
}
|
|
if v := sessionOf(in); v != "" {
|
|
return v
|
|
}
|
|
if k := strings.TrimSpace(gjson.GetBytes(body, "prompt_cache_key").String()); k != "" {
|
|
return "prompt_cache_key\x00" + k
|
|
}
|
|
return conversationID(in, body)
|
|
}
|
|
|
|
// logOpenCodeRefusal writes to magpie's log what went to OpenCode's
|
|
// gateway with a request it refused: where, the names of the headers sent
|
|
// (never a value), and whether x-opencode-session was among them, by its
|
|
// first 8 characters. OpenCode answers a request without one 400
|
|
// "Request is missing x-opencode-session and cannot be routed
|
|
// efficiently" (luci on Discord), and this tells that apart from a
|
|
// session the user's own header pins every conversation to.
|
|
func logOpenCodeRefusal(p provider.Provider, req *http.Request, status int) {
|
|
var names []string
|
|
for k := range req.Header {
|
|
names = append(names, strings.ToLower(k))
|
|
}
|
|
slices.Sort(names)
|
|
session := "missing"
|
|
if v := req.Header.Get("x-opencode-session"); v != "" {
|
|
session = v[:min(len(v), 8)] + "…"
|
|
for k, v := range p.Headers {
|
|
// ${sessionId} in it is each conversation's own
|
|
if strings.EqualFold(k, "x-opencode-session") && !strings.Contains(v, provider.SessionPlaceholder) && !strings.Contains(v, provider.ThreadPlaceholder) {
|
|
session += " (the provider's own header: one session for every conversation)"
|
|
}
|
|
}
|
|
}
|
|
log.Printf("opencode: %s answered %d to %s %s%s; x-opencode-session %s; headers sent: %s",
|
|
p.ID, status, req.Method, req.URL.Host, req.URL.Path, session, strings.Join(names, ", "))
|
|
}
|