Files
magpie/internal/gateway/vscode_agent_host_test.go
T
yetone 6b3a6d42c0 agent: VS Code's Agents window (vscode_agent_host) counts as VS Code, its turns as Codex's
justruman on Discord (#general, 11-01): VS Code's Agents window showed
in Usage as a raw "vscode_agent_host" row, 23 calls, 7 failed, beside
the "VS Code" row.

Why: VS Code's agent host runs `codex app-server` with clientInfo
{name: "vscode_agent_host"} (microsoft/vscode main at 8fb6054,
src/vs/platform/agentHost/node/codex/codexAgent.ts CLIENT_INFO; it
bundles @openai/codex 0.163.0). Codex sends that name as its
originator and at the head of its User-Agent, and it reads the user's
~/.codex config, so with Codex connected to magpie its turns come here
under VS Code's name. No agent's UA list had it.

- Usage: vscode_agent_host is in VS Code's UA list
  (vscodeAgentHostUA), so those calls are VS Code's. Records are named
  again at read time (ReportedAs -> AgentOf), so calls already logged
  under the raw name fold into VS Code too. Insiders' and VSCodium's
  agent host send the same name and are counted as VS Code (Stable):
  nothing in the request tells them apart.
- Routing: fromCodex also takes vscode_agent_host (hostedCodex), so
  every Codex rule holds for those turns. The one that fails without
  it: one of Codex's own models asked there, which no account in
  magpie serves, was answered 404 "magpie knows no model" instead of
  going on Codex's sign-in (codexOwnOffMagpie). Also now applied:
  spawn_agent's model list, compaction told in the stream on a byte
  cap/overflow, Codex's headers to a keyed Codex-only relay.
- A test asked as Codex keeps only Codex's own names (codexNamed) as
  Codex's shape: the agent host's headers never become it.
- VS Code's Copilot-proxied names (vscode_codex, vscode_claude_code) go
  to GitHub, not magpie, and are not matched.
- docs/subsystems/gateway-routing.md says so.

Run on mote at this change rebased on origin/main 16ce5ed23; the
one commit since, e65bc8473 (#4802), changes internal/plugin and GUI
files only: gofmt -l empty; go vet ./internal/agent ./internal/gateway
./internal/usage for darwin, GOOS=linux -tags nogui and GOOS=windows
ok; go test -count=1 -timeout 45m on the three packages: agent ok,
usage ok, gateway ok except TestCodexPoolSaysTheClientsVersion
(#4807, filed at origin/main 79f83798f before this change; it sends
codex_cli_rs, which this change leaves as it was, and passes alone
at this head and at its base).

Broken by hand, each red: without hostedCodex in fromCodex,
TestVSCodeAgentHostIsCodex, TestVSCodeAgentHostOwnModelGoesOnSignIn
(404 "magpie knows no model \"gpt-6.1-sol\"") and
TestVSCodeAgentHostNotCodexTestShape (relay sent magpie/dev); with
SawClient ungated, TestVSCodeAgentHostNotCodexTestShape (as Codex sent
vscode_agent_host, 403); without the UA list entry, TestVSCodeAgentHostUA.

Not tried: a real VS Code Agents window against magpie (no VS Code on
this machine or mote). The UA and originator are from VS Code's and
Codex's source, not captured bytes, and the terminal part of the UA is
a guess (it isn't matched). The reporter's 7 failures weren't seen:
the 404 above is the failure class the source shows, not one confirmed
in their log. Other checks keyed on agent == "codex" (Codex memory
calls, image drawing on Codex's own models, title links, CodexBusy)
don't apply to these turns, since they are counted as VS Code.
2026-11-02 04:04:47 +08:00

100 lines
3.8 KiB
Go

package gateway
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// VS Code's Agents window runs `codex app-server` (main at 8fb6054,
// 11-01: @openai/codex 0.163.0) with clientInfo {name: "vscode_agent_host"}, in
// src/vs/platform/agentHost/node/codex/codexAgent.ts. Codex sends the name
// as its originator and at the head of its User-Agent, as it does Claude
// Code's plugin's (TestCodexPluginForClaudeCodeIsCodex). The headers are a
// Codex turn's under that name.
func vscodeAgentHostHeader() http.Header {
return http.Header{
"User-Agent": {"vscode_agent_host/0.163.0 (Mac OS 26.2.0; arm64) unknown (vscode_agent_host; 0.1.0)"},
"Originator": {"vscode_agent_host"},
"X-Codex-Window-Id": {"019a0f7e-0000-7000-8000-000000000001:0"},
"X-Codex-Turn-Metadata": {`{"turn_id":"019a0f7e-0000-7000-8000-000000000002","sandbox":"none"}`},
"Session-Id": {"019a0f7e-0000-7000-8000-000000000001"},
}
}
// Its turns are Codex's: a Codex rule holds for them. They are not Codex's
// own name, so they never stand for Codex in a test asked as Codex.
func TestVSCodeAgentHostIsCodex(t *testing.T) {
h := vscodeAgentHostHeader()
if !fromCodex(h) {
t.Fatal("VS Code's agent host's Codex turn isn't Codex's")
}
if !fromCodex(http.Header{"Originator": {"vscode_agent_host"}}) {
t.Fatal("by its originator alone, it isn't Codex's")
}
if codexNamed(h) {
t.Fatal("the agent host's name counted as Codex's own")
}
for _, other := range []string{"vscode_codex/0.163.0", "vscode_claude_code/2.1.0", "GitHubCopilotChat/0.69.0"} {
if fromCodex(http.Header{"User-Agent": {other}}) {
t.Errorf("%s counted as Codex", other)
}
}
}
// One of Codex's own models asked from the Agents window on Codex's
// magpie provider, which no account in magpie serves, goes on Codex's own
// sign-in as the Codex CLI's does (TestCodexOwnModelOnMagpieThreadGoesOnSignIn),
// not 404 "magpie knows no model" (justruman on Discord: 7 of 23 calls
// failed).
func TestVSCodeAgentHostOwnModelGoesOnSignIn(t *testing.T) {
_, models := codexOwnOffMagpieSetup(t, false)
req := codexOnMagpieRequest("127.0.0.1:51000")
for k, v := range vscodeAgentHostHeader() {
req.Header[k] = v
}
rec := httptest.NewRecorder()
New().Handler().ServeHTTP(rec, req)
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "pong") {
t.Fatalf("got %d %s", rec.Code, rec.Body.String())
}
if len(*models) != 1 || (*models)[0] != "gpt-6.1-sol" {
t.Fatalf("backend asked for %v", *models)
}
}
// Its headers go on to a keyed relay as Codex's do, but a test asked as
// Codex still asks as Codex itself: the agent host's name is no Codex
// official client a Codex-only relay lets in.
func TestVSCodeAgentHostNotCodexTestShape(t *testing.T) {
fresh(t)
f := &clientOnlyRelay{}
up := httptest.NewServer(f)
t.Cleanup(up.Close)
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Models: []string{"gpt-5.5"},
Chat: up.URL + "/v1", Responses: up.URL + "/v1"}); err != nil {
t.Fatal(err)
}
req := httptest.NewRequest("POST", "/v1/responses", strings.NewReader(`{"model":"relay/gpt-5.5","stream":true,"input":"hi"}`))
req.Header = vscodeAgentHostHeader()
req.Header.Set("Authorization", "Bearer "+Token)
New().Handler().ServeHTTP(httptest.NewRecorder(), req)
head, _, _ := f.last()
if head.Get("Originator") != "vscode_agent_host" {
t.Fatalf("the relay wasn't sent the agent host's turn as it sent it: %v", head)
}
p, err := provider.Find("relay")
if err != nil {
t.Fatal(err)
}
r := p.TestModels(provider.TestAs(context.Background(), provider.ClientCodex), []string{"gpt-5.5"})[0]
head, _, _ = f.last()
if !r.OK || !strings.HasPrefix(head.Get("User-Agent"), "codex_cli_rs/") {
t.Fatalf("as Codex after the agent host came: %+v, User-Agent %q", r, head.Get("User-Agent"))
}
}