Files
magpie/internal/gateway/codex_compact_input_kind_test.go
T
yetoneandMmx233 ff415f39cb gateway: a compaction refused for an input kind it doesn't take is asked again as text (#5181, #5259)
Command Code's /responses takes messages, function calls, their outputs
and reasoning only. A Codex subagent's compaction carries agent_message,
custom_tool_call and compaction_trigger items, and Command Code answered
400 unsupported_input_item (param input). codexCompact didn't count that
as a reason to ask again with the conversation as text, so the 400 went
back to Codex three times and compactFails held the conversation for
5 minutes.

inputKindRefused now asks again as text on unsupported_input_item or
"unknown item type" (Grok's 422) in a 400, 422 or 502 (another magpie in
front wraps it), or in a stream's error / response.failed event. A 401,
403 or 429 with the same words still goes back as it came. itemText
reads an agent_message as the user's turn, so the subagent's task and
follow-ups are in the text summary and in the local one.

No plugin change: the Command Code plugin passes /responses through, and
the fallback covers it direct, through a remote magpie and in a stream.

Run (mote):
- go test -run 'TestCodexCompact|TestCodexCompacts|AgentMessage' ./internal/gateway: ok
- go test -timeout 45m -count=1 ./internal/gateway: ok (809.6s)
- gofmt -l clean; go vet ./internal/gateway native, GOOS=linux -tags
  nogui and GOOS=windows: ok
Broken by hand:
- origin/main's codex_backend.go: every AsksAgainAsText subtest red
  (400 / 502 back to Codex).
- agent_message case removed: the text lacks the subagent's task.
- status guard loosened: the 401/403/429 test red.
Not tried: a real Command Code account (the keys on this machine are
answered 401), the reporters' own sessions; why the compaction reached
/responses rather than chat is not confirmed.

Reported-by: Mmx233 <36563672+Mmx233@users.noreply.github.com>
Reported-by: Edward4226 <Edward4226@users.noreply.github.com>
Reported-by: mahiro233 (Discord)
Co-authored-by: Mmx233 <36563672+Mmx233@users.noreply.github.com>
2026-11-06 16:04:03 +08:00

139 lines
6.0 KiB
Go

package gateway
import (
"encoding/json"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// commandCodeInputRefusal is Command Code's answer, byte for byte as #5181
// and #5259 quote it, to a Responses request with an input item other than
// a message, a function call, its output or reasoning.
const commandCodeInputRefusal = `{"error":{"message":"input items may be messages, function calls, function call outputs and reasoning. Item references need stored responses, which are not kept.","type":"invalid_request_error","param":"input","code":"unsupported_input_item"}}`
// compactSubagentRequest is a Codex subagent's compaction, as #5259's: its
// task and a follow-up came as agent_message items, among calls Command
// Code doesn't take either.
const compactSubagentRequest = `{"model":"fake/m1","stream":true,"store":false,"prompt_cache_key":"subagent-1","input":[
{"type":"agent_message","author":"/root","recipient":"/root/research","content":[{"type":"input_text","text":"Message Type: NEW_TASK\nPayload:\nCompare Vault secret rotation with rollout restart timing."}]},
{"type":"custom_tool_call","id":"ctc_1","call_id":"call_1","name":"apply_patch","input":"*** Begin Patch\n*** Add File: notes.md\n+rotation results\n*** End Patch"},
{"type":"custom_tool_call_output","call_id":"call_1","output":"Success. Updated the following files:\nA notes.md"},
{"type":"message","role":"assistant","content":[{"type":"output_text","text":"The rotation results are in notes.md."}]},
{"type":"agent_message","author":"/root","recipient":"/root/research","content":[{"type":"input_text","text":"Message Type: MESSAGE\nPayload:\nKeep the observed event order in the summary."}]},
{"type":"compaction_trigger"}]}`
// commandCodeTakes is whether Command Code's /responses takes a request's
// input as its refusal says: messages, function calls, their outputs and
// reasoning, nothing else.
func commandCodeTakes(t *testing.T, body []byte) bool {
t.Helper()
var q struct {
Input []struct {
Type string `json:"type"`
} `json:"input"`
}
if err := json.Unmarshal(body, &q); err != nil {
t.Fatalf("summary request: %v: %s", err, body)
}
for _, it := range q.Input {
switch it.Type {
case "", "message", "function_call", "function_call_output", "reasoning":
default:
return false
}
}
return true
}
// #5181, #5259: Command Code turned a Codex compaction away for an input
// item it doesn't take, and magpie passed the 400 to Codex, three times,
// until the guard held the conversation for 5 minutes. Refused so — as
// the 400 itself, through another magpie in front of it (as a 400 or its
// 502), or in the summary's stream — the summary is asked again with the
// conversation as text, which Command Code takes, and the subagent's task
// is in it.
func TestCodexCompactUnsupportedInputItemAsksAgainAsText(t *testing.T) {
remote := `{"error":{"message":` + quote("Remote magpie: Command Code: "+commandCodeInputRefusal) + `,"type":"invalid_request_error"}}`
for _, tc := range []struct {
name string
status int
body string
}{
{"400", 400, commandCodeInputRefusal},
{"another magpie's 400", 400, remote},
{"another magpie's 502", 502, remote},
{"stream error", 200, sse(`data: {"type":"error",` + strings.TrimPrefix(commandCodeInputRefusal, "{"))},
{"failed response", 200, sse(`data: {"type":"response.failed","response":{"id":"r1","status":"failed",` + strings.TrimPrefix(commandCodeInputRefusal, "{") + `}`)},
} {
t.Run(tc.name, func(t *testing.T) {
f := &fake{t: t}
f.refuse = func(b []byte) (int, string) {
f.reply = wholeSummary(provider.Responses)
if !commandCodeTakes(t, b) {
if tc.status != 200 {
return tc.status, tc.body
}
f.reply = tc.body
}
return 0, ""
}
setup(t, provider.Responses, f)
s := New()
// more than the guard's count: none of them is held
for i := range compactFailsMost + 1 {
rec := codexPostTo(s, compactSubagentRequest)
if rec.Code != 200 {
t.Fatalf("try %d: %d %s", i, rec.Code, rec.Body)
}
if got := compactedText(t, rec.Body.String()); got != "SUMMARY" {
t.Fatalf("try %d: summary %q, want the model's", i, got)
}
}
if !commandCodeTakes(t, f.got) {
t.Errorf("asked again with what Command Code refuses: %s", f.got)
}
for _, want := range []string{"Compare Vault secret rotation", "Keep the observed event order", "The rotation results are in notes.md"} {
if !strings.Contains(string(f.got), want) {
t.Errorf("the text lacks %q: %s", want, f.got)
}
}
})
}
}
// Refused as text too, the summary is magpie's own, the subagent's task
// and follow-up in it as what was asked, and Codex goes on.
func TestCodexCompactUnsupportedInputItemCompactsLocally(t *testing.T) {
f := &fake{t: t, refuse: func([]byte) (int, string) { return 400, commandCodeInputRefusal }}
setup(t, provider.Responses, f)
code, body := codexPost(t, compactSubagentRequest)
if code != 200 {
t.Fatalf("%d %s", code, body)
}
got := compactedText(t, body)
for _, want := range []string{"without a model", "input items may be messages", "Compare Vault secret rotation", "Keep the observed event order", "The rotation results are in notes.md."} {
if !strings.Contains(got, want) {
t.Errorf("local summary lacks %q: %s", want, got)
}
}
if f.calls != 2 {
t.Errorf("asked %d times, want the request and its text", f.calls)
}
}
// The same words from a key or account refusal, or in a reply that
// didn't fail, are no refusal of the input: a 401, 403 or 429 still goes
// back to Codex as it came, never asked as text.
func TestCodexCompactUnsupportedInputWordsOnAnAccountFailureStay(t *testing.T) {
for _, status := range []int{401, 403, 429} {
f := &fake{t: t, refuse: func([]byte) (int, string) { return status, commandCodeInputRefusal }}
setup(t, provider.Responses, f)
code, body := codexPost(t, compactSubagentRequest)
if code != status || strings.Contains(string(f.got), "This is the conversation so far") {
t.Errorf("%d: %d %s (last asked %s)", status, code, body, f.got)
}
}
}