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.
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -0,0 +1,77 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -78,9 +78,36 @@ func agentOf(r *http.Request) string {
|
||||
// and it is shown its models as with its key (mamba on Discord)
|
||||
return "cursor-local"
|
||||
}
|
||||
if codexForClaudeCode(r.Header) {
|
||||
return "codex"
|
||||
}
|
||||
return usage.AgentOf(ua)
|
||||
}
|
||||
|
||||
// codexForClaudeCode is a Codex turn that Claude Code started under its
|
||||
// own name. OpenAI's Codex plugin for Claude Code (/codex:review,
|
||||
// /codex:rescue) runs `codex app-server` with clientInfo named "Claude
|
||||
// Code", and Codex makes that its originator and User-Agent ("Claude
|
||||
// Code/0.162.1 (Mac OS 26.2.0; arm64) … (Claude Code; 1.0.6)"), so the
|
||||
// turn read as Claude Code's, and its calls were counted under Claude
|
||||
// Code, never Codex (wyztony on X). Claude Code's own requests say
|
||||
// claude-cli/ and never carry the headers Codex puts on each turn
|
||||
// (x-codex-window-id, x-codex-turn-metadata), so the name with those is
|
||||
// Codex's. Only Claude Code's name is taken back this way: an app that
|
||||
// runs Codex as its own engine and names it after itself (Lody) is
|
||||
// counted as that app.
|
||||
func codexForClaudeCode(h http.Header) bool {
|
||||
if h.Get("X-Codex-Window-Id") == "" && h.Get("X-Codex-Turn-Metadata") == "" {
|
||||
return false
|
||||
}
|
||||
for _, v := range []string{h.Get("User-Agent"), h.Get("Originator")} {
|
||||
if strings.HasPrefix(strings.ToLower(strings.TrimSpace(v)), "claude code") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// startedByAgent says whether a request is from no agent magpie knows but
|
||||
// from something an agent started with its environment (a hook's script): it
|
||||
// came with the plain gateway token, not one magpie gave an agent, and
|
||||
|
||||
Reference in New Issue
Block a user