Files
magpie/internal/gateway/reasoning_state_claude_codex_test.go
T
yetone 79c8667c92 gateway: Codex gives Claude its signed thinking back through encrypted_content
Codex calling a Claude model through magpie (mysterious_dove_96933 on
Discord): a reply's reasoning items carried only the summary, so Claude's
thinking signature and a redacted_thinking block's data were gone. On
the next request of a tool loop magpie sent Anthropic the turn as
[tool_use] alone, with thinking on, which Anthropic documents as a 400
("final assistant message must start with a thinking block"), and the
model lost its reasoning across calls either way.

Now the Anthropic decoders name a signature as Claude's (KSig with
sealAnthropic, Part.SignedBy). The Responses encoder and renderResponses
give each thinking block a reasoning item whose encrypted_content is
"magpie-claude1:" and the block's JSON in base64 (internal/claudeseal):
its summary beside it, an empty summary for a block signed with no
text, and redacted_thinking as an item of its own. parseResponses reads
it back into the block as Claude wrote it, in order, so anthropicBody
sends it as before for an Anthropic client. A Gemini thought's
signature isn't named and isn't sealed.

The envelope never reaches a Responses API: withoutClaudeSeals in
passthrough and forwardOnce (the item keeps its summary, one with none
goes), codexInputAs leaves such an item out for Codex's own models, and
codexUpstreamOn takes it out before the ChatGPT backend. OpenAI's own
encrypted_content is untouched. sessions.PortCodexRollouts writes such
an item as its summary, so a thread resumed against OpenAI itself isn't
refused. redact.MaskJSON already keeps a reasoning item's seal whole
(no change); TestMaskJSONKeepsClaudeBlocks holds that.

Run, at 5e3dd1d52 plus this change:
- go test -count=1 -timeout 45m ./internal/gateway/ ./internal/sessions/
  ./internal/redact/ ./internal/claudeseal/: sessions, redact,
  claudeseal ok; gateway ok but for TestResponsesStreamToolThenHiddenThinking
  (#1786), whose hidden cases wanted no reasoning item, the loss this
  fixes. They now want one item carrying the sealed block with an empty
  summary, and its guarantee (a whole stream, no panic) is unchanged;
  re-run green.
- gofmt -l clean; go vet (darwin) on the touched packages, GOOS=linux
  go vet -tags nogui ./internal/..., GOOS=windows go vet on the touched
  packages: clean.
- TestCodexToolLoopGivesClaudeItsThinkingBack (a fake Anthropic: signed
  thinking, redacted_thinking, a text-less signed block, tool_use; a
  Codex-shaped request with store false and the encrypted_content
  include; then the follow-up) fails on 5e3dd1d52: "stream=true:
  Anthropic was sent 1 assistant blocks, want 4". Passes now, streamed
  and not.
- Real: a sandbox HOME with only OpenRouter's key (Anthropic endpoint,
  anthropic/claude-opus-5.5, served by Claude Platform on AWS), magpie
  serve on its own port, a Codex-shaped two-turn tool loop at effort
  high, streamed and not: turn 1 gave a reasoning item with
  magpie-claude1: (3.1KB), turn 2 was 200 through /v1/responses →
  /v1/messages and answered from the tool output.

Broken by hand:
- withoutClaudeSeals a no-op: TestClaudeBlocksNeverGoToAResponsesAPI
  red, the relay sent magpie-claude1: items.
- codexInputAs not leaving them out: red, "ChatGPT backend would be
  sent …".
- sealing any signature (no sealAnthropic check):
  TestGeminiSignatureIsNotSealedAsClaudes red.
- portValue's reasoning branch off: TestPortLineDropsClaudeBlocks red.

Not tried: the 400 itself. Through OpenRouter, Opus 5.5 answered turn 2
with the envelope stripped too (the old shape), so this route shows
only the lost reasoning, not the refusal. Not tried on api.anthropic.com
directly, Bedrock or Vertex keys, or Claude's subscription (whose tool
loop stays inside Claude Code's run). Codex resuming a ported thread
against OpenAI with such items wasn't run.
2026-10-27 11:26:54 +08:00

276 lines
12 KiB
Go

package gateway
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"github.com/yetone/magpie/internal/claudeseal"
"github.com/yetone/magpie/internal/provider"
)
// Codex calling a Claude model through magpie (mysterious_dove_96933 on
// Discord): Claude's thinking is signed, and a redacted_thinking block is
// sealed whole, and Anthropic wants both back intact on the next request
// of a tool loop. Codex keeps only what a reply's reasoning items carry,
// so they carry Claude's blocks in encrypted_content, and the next
// request has them back, in order, as Claude wrote them.
// claudeToolTurn is a Messages stream as Anthropic's extended thinking
// docs give one ending in a tool call: a signed thinking block, a
// redacted_thinking block, a block signed with no text (display omitted),
// then the call.
var claudeToolTurn = []string{
`{"type":"message_start","message":{"id":"msg_01","type":"message","role":"assistant","model":"claude-sonnet-4-5","content":[],"usage":{"input_tokens":10,"output_tokens":1}}}`,
`{"type":"content_block_start","index":0,"content_block":{"type":"thinking","thinking":"","signature":""}}`,
`{"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":"I should list "}}`,
`{"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":"the files."}}`,
`{"type":"content_block_delta","index":0,"delta":{"type":"signature_delta","signature":"ErUBCkYIBxgCIkB2signed"}}`,
`{"type":"content_block_stop","index":0}`,
`{"type":"content_block_start","index":1,"content_block":` + redactedBlock + `}`,
`{"type":"content_block_stop","index":1}`,
`{"type":"content_block_start","index":2,"content_block":{"type":"thinking","thinking":"","signature":""}}`,
`{"type":"content_block_delta","index":2,"delta":{"type":"signature_delta","signature":"EqQBCkYIBxgCKkDomitted"}}`,
`{"type":"content_block_stop","index":2}`,
`{"type":"content_block_start","index":3,"content_block":{"type":"tool_use","id":"toolu_01","name":"shell","input":{}}}`,
`{"type":"content_block_delta","index":3,"delta":{"type":"input_json_delta","partial_json":"{\"command\":[\"ls\"]}"}}`,
`{"type":"content_block_stop","index":3}`,
`{"type":"message_delta","delta":{"stop_reason":"tool_use","stop_sequence":null},"usage":{"output_tokens":30}}`,
`{"type":"message_stop"}`,
}
// claudeTurnBlocks are the blocks claudeToolTurn's message is made of.
var claudeTurnBlocks = []string{
`{"type":"thinking","thinking":"I should list the files.","signature":"ErUBCkYIBxgCIkB2signed"}`,
redactedBlock,
`{"type":"thinking","thinking":"","signature":"EqQBCkYIBxgCKkDomitted"}`,
`{"type":"tool_use","id":"toolu_01","name":"shell","input":{"command":["ls"]}}`,
}
// claudeUp answers a request without a tool result with claudeToolTurn,
// and one with a result with a line of text, keeping what it was sent.
type claudeUp struct {
mu sync.Mutex
sent []string
}
func (u *claudeUp) ServeHTTP(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
u.mu.Lock()
u.sent = append(u.sent, string(b))
u.mu.Unlock()
w.Header().Set("Content-Type", "text/event-stream")
lines := claudeToolTurn
if strings.Contains(string(b), `"tool_result"`) {
lines = []string{
`{"type":"message_start","message":{"id":"msg_02","type":"message","role":"assistant","model":"claude-sonnet-4-5","content":[],"usage":{"input_tokens":10,"output_tokens":1}}}`,
`{"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}`,
`{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Two files."}}`,
`{"type":"content_block_stop","index":0}`,
`{"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"output_tokens":3}}`,
`{"type":"message_stop"}`,
}
}
for _, l := range lines {
var ev struct {
Type string `json:"type"`
}
json.Unmarshal([]byte(l), &ev)
io.WriteString(w, "event: "+ev.Type+"\ndata: "+l+"\n\n")
}
}
// codexLoopAsk is a request as Codex sends one with store off: the reasoning
// items of a reply come back in input with their encrypted_content.
func codexLoopAsk(stream bool, input string) string {
s := "false"
if stream {
s = "true"
}
return `{"model":"a/claude-sonnet-4-5","instructions":"You are a coding agent.","input":` + input + `,` +
`"tools":[{"type":"function","name":"shell","description":"Runs a command","strict":false,"parameters":{"type":"object","properties":{"command":{"type":"array","items":{"type":"string"}}},"required":["command"]}}],` +
`"tool_choice":"auto","parallel_tool_calls":false,"reasoning":{"effort":"medium","summary":"auto"},"store":false,"stream":` + s +
`,"include":["reasoning.encrypted_content"],"prompt_cache_key":"t1"}`
}
// replyItems are a Responses reply's output items, from its
// response.output_item.done events when it streamed.
func replyItems(t *testing.T, body []byte, stream bool) []map[string]any {
t.Helper()
var items []map[string]any
if !stream {
var out struct {
Output []map[string]any `json:"output"`
}
if err := json.Unmarshal(body, &out); err != nil {
t.Fatalf("%v: %s", err, body)
}
return out.Output
}
for _, ev := range sseData(t, body) {
if ev["type"] == "response.output_item.done" {
items = append(items, ev["item"].(map[string]any))
}
}
return items
}
// The tool loop as Codex runs it against a Claude model on an Anthropic
// API key: the second request reaches Anthropic with the first reply's
// signed thinking, redacted_thinking and text-less signed block, in
// order and as Claude wrote them, before the tool_use, streamed or not.
func TestCodexToolLoopGivesClaudeItsThinkingBack(t *testing.T) {
for _, stream := range []bool{true, false} {
fresh(t)
up := &claudeUp{}
srv := httptest.NewServer(up)
t.Cleanup(srv.Close)
if err := provider.Save(provider.Provider{ID: "a", Name: "A", Key: "k", Anthropic: srv.URL, Models: []string{"claude-sonnet-4-5"}}); err != nil {
t.Fatal(err)
}
s := New()
user := `{"type":"message","role":"user","content":[{"type":"input_text","text":"What is here?"}]}`
rec := httptest.NewRecorder()
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/responses", strings.NewReader(codexLoopAsk(stream, "["+user+"]"))))
if rec.Code != 200 {
t.Fatalf("stream=%t turn 1: %d %s", stream, rec.Code, rec.Body)
}
// Codex keeps each item as it came, less a reasoning item's id
// (it doesn't send one back with store off), then the call's output
input := []string{user}
var call string
for _, it := range replyItems(t, rec.Body.Bytes(), stream) {
delete(it, "status")
if it["type"] == "reasoning" {
delete(it, "id")
}
if it["type"] == "function_call" {
call, _ = it["call_id"].(string)
}
b, _ := json.Marshal(it)
input = append(input, string(b))
}
if call == "" {
t.Fatalf("stream=%t: no call in %s", stream, rec.Body)
}
input = append(input, `{"type":"function_call_output","call_id":"`+call+`","output":"a.go\nb.go"}`)
rec = httptest.NewRecorder()
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/responses", strings.NewReader(codexLoopAsk(stream, "["+strings.Join(input, ",")+"]"))))
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "Two files.") {
t.Fatalf("stream=%t turn 2: %d %s", stream, rec.Code, rec.Body)
}
up.mu.Lock()
last := up.sent[len(up.sent)-1]
up.mu.Unlock()
var q struct {
Messages []struct {
Role string `json:"role"`
Content []json.RawMessage `json:"content"`
} `json:"messages"`
}
if err := json.Unmarshal([]byte(last), &q); err != nil {
t.Fatal(err)
}
var got []json.RawMessage
for _, m := range q.Messages {
if m.Role == "assistant" {
got = append(got, m.Content...)
}
}
if len(got) != len(claudeTurnBlocks) {
t.Fatalf("stream=%t: Anthropic was sent %d assistant blocks, want %d:\n%s", stream, len(got), len(claudeTurnBlocks), last)
}
for i, w := range claudeTurnBlocks {
var g map[string]any
json.Unmarshal(got[i], &g)
delete(g, "cache_control")
if !jsonIs(t, g, w) {
t.Fatalf("stream=%t: block %d:\n got %s\nwant %s\nsent %s", stream, i, got[i], w, last)
}
}
}
}
// claudeConversation is a Codex conversation that went through Claude and
// now goes on with another model: a reasoning item with a summary and
// Claude's block, one with Claude's block alone, and one OpenAI sealed.
func claudeConversation() string {
think := claudeThinking("I should list the files.", "ErUBCkYIBxgCIkB2signed")
redacted := claudeseal.Seal([]byte(redactedBlock))
return `{"type":"message","role":"user","content":[{"type":"input_text","text":"What is here?"}]},` +
`{"type":"reasoning","summary":[{"type":"summary_text","text":"I should list the files."}],"encrypted_content":"` + think + `"},` +
`{"type":"reasoning","summary":[],"encrypted_content":"` + redacted + `"},` +
`{"type":"function_call","call_id":"call_1","name":"shell","arguments":"{\"command\":[\"ls\"]}"},` +
`{"type":"function_call_output","call_id":"call_1","output":"a.go"},` +
`{"type":"reasoning","id":"rs_openai","summary":[],"encrypted_content":"gAAAA-openai-sealed"},` +
`{"type":"message","role":"user","content":[{"type":"input_text","text":"and now?"}]}`
}
// Claude's blocks are Claude's: a Responses API (a relay here, the
// ChatGPT backend for Codex's own models) is sent each item as it was
// before magpie carried them, its summary alone or nothing, and never the
// envelope, which OpenAI would refuse as invalid_encrypted_content. What
// OpenAI sealed goes on untouched.
func TestClaudeBlocksNeverGoToAResponsesAPI(t *testing.T) {
fresh(t)
u := &relayUpstream{}
saveRelay(t, u)
body := `{"model":"relay/gpt-5.4","stream":true,"store":false,"include":["reasoning.encrypted_content"],"input":[` + claudeConversation() + `]}`
if rec := codexPostTo(New(), body); rec.Code != 200 {
t.Fatalf("%d %s", rec.Code, rec.Body)
}
asked := u.asked()
if len(asked) != 1 {
t.Fatalf("relay asked %d times", len(asked))
}
sent, _ := json.Marshal(asked[0])
if strings.Contains(string(sent), claudeseal.Mark) || !strings.Contains(string(sent), "gAAAA-openai-sealed") {
t.Fatalf("relay was sent %s", sent)
}
if !strings.Contains(string(sent), "I should list the files.") {
t.Fatalf("the summary went missing: %s", sent)
}
// Codex's own model on the ChatGPT backend
out, _ := codexInputAs([]byte(`{"model":"gpt-5.4","input":[`+claudeConversation()+`]}`), false, false)
if strings.Contains(string(out), claudeseal.Mark) || !strings.Contains(string(out), "gAAAA-openai-sealed") {
t.Fatalf("ChatGPT backend would be sent %s", out)
}
if strings.Count(string(out), `"reasoning"`) != 1 {
t.Fatalf("want only OpenAI's reasoning item: %s", out)
}
// and nothing to take out, the body goes byte for byte
plain := []byte(`{"input":[{"type":"reasoning","summary":[],"encrypted_content":"gAAAA"}],"x":1}`)
if got := withoutClaudeSeals(plain); string(got) != string(plain) {
t.Fatalf("%s became %s", plain, got)
}
}
// Only Claude's signature goes in encrypted_content: a Gemini thought's
// (an unnamed KSig, or a Part with no SignedBy) isn't sealed as Claude's,
// streamed or not.
func TestGeminiSignatureIsNotSealedAsClaudes(t *testing.T) {
rec := httptest.NewRecorder()
enc := &responsesEncoder{w: newSSEWriter(rec), model: "m"}
enc.event(Event{Kind: KThinkStart})
enc.event(Event{Kind: KThink, Text: "thought"})
enc.event(Event{Kind: KSig, Text: "gemini-sig"})
enc.event(Event{Kind: KThinkStart})
enc.event(Event{Kind: KSig, Text: "gemini-bare"})
enc.event(Event{Kind: KText, Text: "hi"})
enc.event(Event{Kind: KStop, Stop: "stop"})
enc.finish()
if strings.Contains(rec.Body.String(), "encrypted_content") || strings.Contains(rec.Body.String(), "gemini-") {
t.Fatalf("stream: %s", rec.Body)
}
b := renderResponses(Result{Parts: []Part{{Kind: Thinking, Text: "thought", Signature: "gemini-sig"}, {Kind: Thinking, Signature: "gemini-bare"}, {Kind: Text, Text: "hi"}}}, "m", nil)
if strings.Contains(string(b), "encrypted_content") || strings.Contains(string(b), "gemini-") {
t.Fatalf("render: %s", b)
}
}