Files
magpie/internal/gateway/codex_plugin_agent_test.go
T
yetone a60493ab60 gateway: Codex run by Claude Code's Codex plugin is counted as Codex, not Claude Code
wyztony on X: "claude通过/codex skill时,用量不会记录codex的记录".
OpenAI's Codex plugin for Claude Code (codex-plugin-cc 1.0.6,
/codex:rescue and /codex:review) runs `codex app-server` with clientInfo
named "Claude Code". Codex 0.162.1 then sends "originator: Claude Code"
and "User-Agent: Claude Code/0.162.1 (...) (Claude Code; 1.0.6)", so
agentOf read every such turn as Claude Code's ("claude") and the Usage
page showed none of them under Codex.

agentOf now takes a "Claude Code" name back to Codex when the request
carries Codex's own turn headers (x-codex-window-id or
x-codex-turn-metadata), which Claude Code itself (claude-cli/...) never
sends. An app running Codex as its own engine under its own name (Lody)
keeps its name, as TestFromCodex already holds. fromCodex is unchanged,
so nothing sent upstream changes. Every path keyed on agentOf
(Codex subagent pick, spawn_agent models, drawing route, Codex backend
records, titles, search, active sessions, callerOf) follows.

Run:
- TestCodexPluginForClaudeCodeIsCodex: red on the old code with the
  check turned off by hand ("plugin task: recorded as \"Claude\", want
  \"codex\""), green with it. Cases: the plugin's captured headers,
  originator only, claude-cli, the name without Codex's turn headers,
  Lody with them.
- Real use in a sandbox HOME: codex-cli 0.162.1 wired to a sandbox
  `magpie serve` on a fake Responses upstream, driven by the plugin's own
  codex-companion.mjs (db52e28). Before: task and review rows were
  "agent":"claude". After: both "agent":"codex". Plain `codex exec` (a
  skill-folder /codex skill) was already "codex" through the gateway and
  read from its rollout when direct.
- gofmt; go vet darwin, linux, windows.
- Full suite (go test ./... -timeout 45m, one suite on the machine) was
  started at this tree and was still running at push; per the owner's
  ship-at-once rule, a red it finds goes in its own commit.

Not changed, outside magpie: with Codex not routed through magpie,
/codex:review runs an ephemeral thread and Codex writes no rollout, so
there is nothing on disk for magpie to read; /codex:rescue writes one
and was already counted as Codex. Past rows stay under Claude Code.
2026-10-25 23:02:33 +08:00

78 lines
2.9 KiB
Go

package gateway
import (
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/usage"
)
// OpenAI's Codex plugin for Claude Code (/codex:rescue, /codex:review) runs
// `codex app-server` under the name "Claude Code", and Codex 0.162.1 sends
// that as its originator and User-Agent. Its calls are Codex's, not Claude
// Code's (wyztony on X: "claude通过/codex skill时,用量不会记录codex的记录").
// The headers are the ones captured from codex-companion.mjs through a
// sandbox gateway. Claude Code itself, a "Claude Code" name without Codex's
// turn headers, and an app that runs Codex as its own engine (Lody) keep
// the agent they had.
func TestCodexPluginForClaudeCodeIsCodex(t *testing.T) {
setHome(t, t.TempDir())
f := &fake{t: t, reply: sse(
`data: {"type":"response.created","response":{"id":"r1","model":"m1"}}`,
`data: {"type":"response.completed","response":{"id":"r1","output":[],"usage":{"input_tokens":5,"output_tokens":2}}}`)}
setup(t, provider.Responses, f)
const plugin = "Claude Code/0.162.1 (Mac OS 26.2.0; arm64) dumb (Claude Code; 1.0.6)"
turn := map[string]string{
"X-Codex-Window-Id": "019a0f7e-0000-7000-8000-000000000001:0",
"X-Codex-Turn-Metadata": `{"turn_id":"019a0f7e-0000-7000-8000-000000000002","sandbox":"none"}`,
"X-Codex-Beta-Features": "remote_compaction_v2",
"Session-Id": "019a0f7e-0000-7000-8000-000000000001",
"Thread-Id": "019a0f7e-0000-7000-8000-000000000001",
}
for i, c := range []struct {
name, ua, originator string
codexTurn bool
want string
}{
{"plugin task", plugin, "Claude Code", true, "codex"},
{"plugin, originator only", "", "Claude Code", true, "codex"},
{"claude code itself", "claude-cli/2.1.0 (external, cli)", "", false, usage.AgentOf("claude-cli/2.1.0 (external, cli)")},
{"the name without codex's turn", plugin, "", false, usage.AgentOf(plugin)},
{"lody runs codex as its engine", "lody/1.2.0", "lody", true, usage.AgentOf("lody/1.2.0")},
} {
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/v1/responses", strings.NewReader(`{"model":"fake/m1","stream":true,"input":"hi"}`))
req.Header.Set("Authorization", "Bearer "+Token)
req.Header.Set("User-Agent", c.ua)
if c.originator != "" {
req.Header.Set("Originator", c.originator)
}
if c.codexTurn {
for k, v := range turn {
req.Header.Set(k, v)
}
}
New().Handler().ServeHTTP(rec, req)
if rec.Code != 200 {
t.Fatalf("%s: %d %s", c.name, rec.Code, rec.Body.String())
}
var got []usage.Record
for range 100 {
if got = usage.Load(time.Time{}); len(got) > i {
break
}
time.Sleep(20 * time.Millisecond)
}
if len(got) <= i || got[i].Agent != c.want {
var agent string
if len(got) > i {
agent = got[i].Agent
}
t.Fatalf("%s: recorded as %q, want %q", c.name, agent, c.want)
}
}
}