Files
magpie/internal/gateway/plugin_session_test.go
T
yetone a00b53f7b1 gateway: a plugin's fetch is told the session the agent named and its Claude Code subagent (plugins#620)
magpie builds each plugin's request itself, so the agent's own headers
(x-claude-code-session-id, x-claude-code-agent-id, x-opencode-session)
never reached a plugin. Devin's cache keepalive therefore keyed Claude
Code Auto's permission check by its text alone, and on the translated
path magpie merges consecutive same-role messages, so a check whose
history grew changed its first message in the middle and got a new key:
its older pings kept running beside the new ones (zelong77, plugins#620,
still seen on devin 0.1.33).

forwardOnce now sets X-Magpie-Session (sessionOf: the caller's own
X-Magpie-Session, else the agent's session header) and
X-Magpie-Subagent (Claude Code's agent id) on a plugin's request, after
deleting any the caller sent, and host.js's MAGPIE_ONLY list strips both
from what the plugin sends on, so the vendor never gets them. A request
naming no session has neither. devin 0.1.34 keys its pings by them.
docs/subsystems/provider-plugins.md says so.

Run at origin/main 1829cd52f plus these three commits (mote, macOS):
- gofmt -l internal/gateway internal/provider internal/plugin: clean.
- GOOS=linux and GOOS=windows go vet of the three: ok.
- go test -count=1 -timeout 45m ./internal/plugin ./internal/provider
  ./internal/gateway: plugin ok, provider ok; gateway one red,
  TestCodexPoolSaysTheClientsVersion, which passes alone (-count=3)
  and is another test's Codex identity leaking into it, filed as #4807,
  untouched by this change.
- -v: TestPluginToldTheSession and TestMovedBuiltinsSayTheirPlugin pass.
- Rebased onto origin/main 52195b6b0 (it brings #3388's provider
  change): gofmt clean, linux and windows vet ok,
  internal/provider and internal/plugin ok, internal/gateway -run
  'TestPlugin|TestMoved|Devin|WorkBuddy' ok. Pushed on 9f641daea,
  whose Codex app-server change (codex_app_server.go, codex_daemon.go,
  login_usage.go) touches none of these files; not run again on it.

Broken by hand:
- without the two Set lines in forwardOnce: TestPluginToldTheSession red
  (the plugin told nothing), on relayed Anthropic and translated Chat.
- without the MAGPIE_ONLY entries in host.js: red, the vendor got
  X-Magpie-Session.

Not tried: a real Devin account and a real Claude Code Auto session.
2026-11-02 18:32:47 +08:00

94 lines
3.9 KiB
Go

package gateway
import (
"io"
"net/http"
"net/http/httptest"
"slices"
"strings"
"sync"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// A plugin's fetch is told the session the agent named and the Claude Code
// subagent a request is of (SessionToPluginHeader, SubagentToPluginHeader):
// the agent's own headers never reach a plugin, so Devin's cache keepalive
// keyed Claude Code Auto's permission check by its text alone, and a check
// whose history grew kept its older pings running beside the new ones
// (magpie-community/plugins#620, zelong77). The headers reach the plugin
// and never the vendor; a request that names no session has neither.
func TestPluginToldTheSession(t *testing.T) {
var mu sync.Mutex
var got []http.Header
pid := besideFake(t, "fakeco", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.Copy(io.Discard, r.Body)
mu.Lock()
got = append(got, r.Header.Clone())
mu.Unlock()
w.Header().Set("Content-Type", "text/event-stream")
if r.URL.Path != "/v1/messages" {
io.WriteString(w, `data: {"id":"c","object":"chat.completion.chunk","model":"fake-1","choices":[{"index":0,"delta":{"role":"assistant","content":"hi"},"finish_reason":null}]}`+"\n\n")
io.WriteString(w, `data: {"id":"c","object":"chat.completion.chunk","model":"fake-1","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}`+"\n\ndata: [DONE]\n\n")
return
}
for _, e := range []string{
`{"type":"message_start","message":{"id":"m","type":"message","role":"assistant","model":"fake-claude","content":[],"usage":{"input_tokens":1,"output_tokens":0}}}`,
`{"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}`,
`{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"hi"}}`,
`{"type":"content_block_stop","index":0}`,
`{"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":1}}`,
`{"type":"message_stop"}`,
} {
io.WriteString(w, "data: "+e+"\n\n")
}
}))
// to a model on Anthropic Messages, relayed, and to one on Chat,
// translated, unstreamed as Claude Code's permission check asks
for _, model := range []string{"fake-claude", "fake-1"} {
for _, tc := range []struct {
name string
headers map[string]string
session, subagent string // "" for not told
}{
{"claude code's lead", map[string]string{"X-Claude-Code-Session-Id": "cc-s1"}, "cc-s1", ""},
{"claude code's subagent", map[string]string{"X-Claude-Code-Session-Id": "cc-s1", "X-Claude-Code-Agent-Id": "agent-7"}, "cc-s1", "agent-7"},
{"magpie's own session header first", map[string]string{"X-Magpie-Session": "mine", "X-Claude-Code-Session-Id": "cc-s1"}, "mine", ""},
{"opencode", map[string]string{"X-Opencode-Session": "ses_1"}, "ses_1", ""},
{"no session named", nil, "", ""},
} {
tc.name = model + ", " + tc.name
mu.Lock()
got = nil
mu.Unlock()
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/v1/messages", strings.NewReader(`{"model":"`+pid+`/`+model+`","max_tokens":64,"stream":false,"messages":[{"role":"user","content":"hi"}]}`))
req.Header.Set("anthropic-version", "2023-06-01")
for k, v := range tc.headers {
req.Header.Set(k, v)
}
New().Handler().ServeHTTP(rec, req)
if rec.Code != 200 {
t.Fatalf("%s: %d %s", tc.name, rec.Code, rec.Body)
}
mu.Lock()
sent := slices.Clone(got)
mu.Unlock()
if len(sent) == 0 {
t.Fatalf("%s: the vendor got nothing", tc.name)
}
for _, h := range sent {
for _, k := range []string{provider.SessionToPluginHeader, provider.SubagentToPluginHeader} {
if v := h.Values(k); len(v) > 0 {
t.Errorf("%s: the vendor got %s: %q", tc.name, k, v)
}
}
if s, a := h.Get("X-Saw-Session"), h.Get("X-Saw-Subagent"); s != tc.session || a != tc.subagent {
t.Errorf("%s: the plugin was told session %q, subagent %q; want %q, %q", tc.name, s, a, tc.session, tc.subagent)
}
}
}
}
}