Claude Code's subagents send their lead's x-claude-code-session-id, so
an extraBody of {"prompt_cache_key": "${sessionId}"} put every subagent
of a conversation on one key, more traffic than a relay keeps on one
machine: the reporter's subagent turns read 0 or only the shared
~22K system prefix after full hits on the turn before.
${subagentId} is now the request's x-claude-code-agent-id (subOf),
which a subagent's turns send and the conversation's own never do: ""
for the lead. "${sessionId}:${subagentId}" gives the lead one key and
each subagent its own. It is opt-in: a value without it goes out byte
for byte as before, Test and Detect fill it with "", and other agents
get "" (never the literal). Conversation carries it (Session, Thread,
Subagent) and Conversation.expand fills it, so a provider's Headers
take it too, as they take the other two; ggbdpq's patch left a header
naming it to go out as written, which the providers doc says never
happens to a placeholder.
The default prompt_cache_key magpie fills (ownCacheKey, cacheKeyOf)
stays per conversation; the subagent is not folded in. The one
measurement on the default path (tingxueren on this issue: 452 Claude
Code requests, 98.21% weighted hit with a conversation-level key) shows
no overload, the overload was seen on two pooled relays, and folding
would move every default user's keys unasked. The placeholder covers
the reporter's case; the default can be revisited with data.
Patch by ggbdpq (branch hunt50-e-sessionid-cache, b25c8ab82), rebased;
headers added here.
Run (mote, macOS):
- go test -run 'TestExtraBody|TestWithExtraBody|TestHeaders|TestOwnCacheKey|Conversation'
./internal/gateway ./internal/provider: ok both.
- Red on origin/main's product code with the new tests:
TestExtraBodySubagentId "the lead's key \"sess-1:${subagentId}\"",
TestHeadersCarryTheSubagent "X-Route \"sess-1/${subagentId}\"".
- Broken by hand: Subagent: subOf(in) dropped in forwardOnce,
TestExtraBodySubagentId red ("a subagent's key \"sess-1:\"").
- gofmt -l clean. go vet (darwin, GOOS=windows, GOOS=linux) ok on
gateway and provider. Full suite at b80d1ba7b (this commit with the
#3835 one on top, rebased on ddb1701fe), mote: go test -tags nogui
-count=1 -timeout 45m ./... , 81 packages ok, no FAIL (gateway 755s,
provider 253s). Other sessions' suites ran on mote at the same time
(load ~20 to 30).
- Rebased on 2f65a33aa (gateway.go and providers-accounts.md changed
upstream, in unrelated lines): gateway, provider, plugin and library
rerun with -count=1 -timeout 45m, all ok.
- Then rebased on a0cb8b646 (Ark web search, gateway.go): go vet and
-run 'TestClaude|TestAutoMode|TestExtraBody|TestHeaders|TestAudit|
TestServerReview|Ark|Search' on gateway and provider at the pushed
head, ok.
Not tried: a real pooled relay; no GUI hint names ${subagentId} (the
editor's hint text and its translations are unchanged; the providers
doc names it).
Co-authored-by: ggbdpq <ggbdpq@gmail.com>
141 lines
4.9 KiB
Go
141 lines
4.9 KiB
Go
package provider
|
|
|
|
// The conversation a request is part of, for ${sessionId} and ${threadId}
|
|
// in a provider's Headers and ExtraBody (Axel Burks on Discord: a gateway
|
|
// in front of several Azure accounts routes a conversation to one account
|
|
// by a session id in a header, so its sealed reasoning can be read back).
|
|
//
|
|
// - ${sessionId} is the session the agent names (its session header),
|
|
// else the id magpie derives from the conversation's first user
|
|
// message: the same for every turn of it.
|
|
// - ${threadId} is the thread the agent names within that session, where
|
|
// one names a thread apart from its session: Codex's thread_id (its
|
|
// turn metadata, or its thread-id header), which a subagent has of its
|
|
// own while it shares its lead's session_id. No other agent names one,
|
|
// and there a conversation is one thread: ${threadId} is ${sessionId}.
|
|
// - A request that is no turn of a conversation — a model list, a
|
|
// balance, an embedding or image, the editor's Test and Detect — goes
|
|
// on a conversation of its own: a new id each time ("magpie-" and 12
|
|
// random letters; Test and Detect's "magpie-test-…", the same one in
|
|
// its headers and body). A literal "${sessionId}" is never sent.
|
|
// - In a header the ids go without control characters (CR, LF, tab…):
|
|
// an id is the agent's, and a header can't carry them.
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"maps"
|
|
"strings"
|
|
)
|
|
|
|
// ThreadPlaceholder is what a header or an extraBody string writes for the
|
|
// thread's id.
|
|
const ThreadPlaceholder = "${threadId}"
|
|
|
|
// SubagentPlaceholder is what a header or an extraBody string writes for
|
|
// the Claude Code subagent a request is of (yetone/magpie#2937).
|
|
const SubagentPlaceholder = "${subagentId}"
|
|
|
|
// Conversation is what ${sessionId}, ${threadId} and ${subagentId} stand
|
|
// for in a request: Thread "" is the session's own thread; Subagent is
|
|
// the Claude Code subagent the request is of (x-claude-code-agent-id), ""
|
|
// on the conversation's own turns, which never send it.
|
|
type Conversation struct{ Session, Thread, Subagent string }
|
|
|
|
type conversationCtxKey struct{}
|
|
|
|
// WithConversation is ctx carrying the conversation its requests are part
|
|
// of, for the headers Sign puts on them.
|
|
func WithConversation(ctx context.Context, c Conversation) context.Context {
|
|
return context.WithValue(ctx, conversationCtxKey{}, c)
|
|
}
|
|
|
|
// conversationOf is the conversation ctx carries, else one of the
|
|
// request's own.
|
|
func conversationOf(ctx context.Context) Conversation {
|
|
if c, ok := ctx.Value(conversationCtxKey{}).(Conversation); ok && c.Session != "" {
|
|
return c
|
|
}
|
|
return OwnConversation("magpie-")
|
|
}
|
|
|
|
// OwnConversation is a conversation of a request's own: prefix and 12
|
|
// random letters, its thread the same.
|
|
func OwnConversation(prefix string) Conversation {
|
|
return Conversation{Session: prefix + strings.ToLower(rand.Text()[:12])}
|
|
}
|
|
|
|
// expand is s with the placeholders as c's ids.
|
|
func (c Conversation) expand(s string) string {
|
|
if !strings.Contains(s, "${") {
|
|
return s
|
|
}
|
|
thread := c.Thread
|
|
if thread == "" {
|
|
thread = c.Session
|
|
}
|
|
return strings.NewReplacer(SessionPlaceholder, c.Session, ThreadPlaceholder, thread, SubagentPlaceholder, c.Subagent).Replace(s)
|
|
}
|
|
|
|
// headerSafe is id without the bytes a header value can't carry: control
|
|
// characters, CR and LF among them.
|
|
func headerSafe(id string) string {
|
|
return strings.Map(func(r rune) rune {
|
|
if r < 0x20 || r == 0x7f {
|
|
return -1
|
|
}
|
|
return r
|
|
}, id)
|
|
}
|
|
|
|
// placeheld says s names the conversation.
|
|
func placeheld(s string) bool {
|
|
return strings.Contains(s, SessionPlaceholder) || strings.Contains(s, ThreadPlaceholder) || strings.Contains(s, SubagentPlaceholder)
|
|
}
|
|
|
|
// AsksThread says p's headers or extraBody name ${threadId}, which the
|
|
// gateway reads out of the request only then.
|
|
func (p Provider) AsksThread() bool {
|
|
for _, v := range p.Headers {
|
|
if strings.Contains(v, ThreadPlaceholder) {
|
|
return true
|
|
}
|
|
}
|
|
for _, v := range p.ExtraBody {
|
|
if strings.Contains(string(v), ThreadPlaceholder) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// SentHeaders are p's Headers as they go out on a request of ctx's
|
|
// conversation (conversationOf): the placeholders in their values as its
|
|
// ids, without control characters. p.Headers itself when no value has
|
|
// one.
|
|
func (p Provider) SentHeaders(ctx context.Context) map[string]string {
|
|
return expandHeaders(p.Headers, func() Conversation { return conversationOf(ctx) })
|
|
}
|
|
|
|
// expandHeaders is h with the placeholders in its values as the ids of
|
|
// the conversation conv gives, asked only when a value has one.
|
|
func expandHeaders(h map[string]string, conv func() Conversation) map[string]string {
|
|
var out map[string]string
|
|
var c Conversation
|
|
for k, v := range h {
|
|
if !placeheld(v) {
|
|
continue
|
|
}
|
|
if out == nil {
|
|
out = maps.Clone(h)
|
|
c = conv()
|
|
c = Conversation{Session: headerSafe(c.Session), Thread: headerSafe(c.Thread), Subagent: headerSafe(c.Subagent)}
|
|
}
|
|
out[k] = c.expand(v)
|
|
}
|
|
if out == nil {
|
|
return h
|
|
}
|
|
return out
|
|
}
|