Files
sub2api/backend/internal/handler/openai_codex_call_output_gate_test.go
feeeei d29660bfd7 fix(openai): 兼容长对话 Codex 定时 heartbeat 归一化与未转义 instructions
- normalizeCodexAutomationBootstrap 改为 allowHistoricalContext = true,允许与可明确配对的历史调用上下文及 previous_response_id 共存
- normalizeCodexCallOutputBootstrap 放行服务端托管 *_call、带 id/call_id 的 tool_search_call 以及同帧合法兄弟 delegation 与 agent ExternalMessage,避免互相死锁
- validCodexAutomationHeartbeat 改为按精确外层标签边界提取 <instructions> 原文,兼容未 XML 转义的 &、<tag> 与正文内字面量 </instructions>,同时拒绝重复标签与分裂信封
2026-11-24 12:37:48 +08:00

64 lines
2.7 KiB
Go

//go:build unit
package handler
import (
"math/rand"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestCodexCallOutputGateSkipsDeclaredToolsWithoutCandidate(t *testing.T) {
body := []byte(`{"model":"gpt-5","tools":[{"type":"function","name":"automation_update"},{"type":"function","name":"create_thread"}],` +
`"input":[{"type":"function_call","call_id":"c1","name":"shell"},{"type":"function_call_output","call_id":"c1","output":"ok"}]}`)
require.False(t, mayContainCodexCallOutputCandidate(body, "automation_update"))
require.False(t, mayContainCodexCallOutputCandidate(body, "create_thread", "send_message_to_thread"))
for name, normalize := range map[string]func([]byte) ([]byte, bool){
"untrusted": normalizeCodexUntrustedContext,
"automation": normalizeCodexAutomationBootstrap,
"delegation": normalizeCodexDelegationBootstrap,
} {
t.Run(name, func(t *testing.T) {
allocs := testing.AllocsPerRun(10, func() {
got, changed := normalize(body)
if changed || &got[0] != &body[0] {
t.Fatal("non-candidate body must be returned unchanged")
}
})
require.Zero(t, allocs)
})
}
}
// Whenever the ungated bootstrap path rewrites a body, the gate must admit it.
func TestCodexCallOutputGateAdmitsEveryRewrite(t *testing.T) {
u := func(hex string) string { return `\` + "u" + hex }
output := mustJSON(t, codexAutomationBootstrap("wiki", "never", automationBootstrapPrompt))
items := []string{
`{"type":"function_call_output","namespace":"codex_app","name":"automation_update","output":` + output + `}`,
`{"type":"function_call_output","namespace":"codex_app","name":"` + u("0061") + `utomation_update","output":` + output + `}`,
`{"` + u("0074") + `ype":"function_call_output","namespace":"codex_app","name":"automation_update","output":` + output + `}`,
`{"type":"function_call_output","namespace":"codex_app","name":"automation_update","name":"other","output":` + output + `}`,
`{"type":"function_call_output","call_id":"c","output":"ok"}`,
`{"type":"message","role":"user","content":"automation_update function_call_output caf` + u("00e9") + `"}`,
`{"type":"function_call","call_id":"c","name":"automation_update"}`,
}
rng := rand.New(rand.NewSource(11))
rewrites := 0
for i := 0; i < 3000; i++ {
var parts []string
for n := rng.Intn(3); n >= 0; n-- {
parts = append(parts, items[rng.Intn(len(items))])
}
body := []byte(`{"model":"gpt-5","tools":[{"type":"function","name":"automation_update"}],"input":[` + strings.Join(parts, ",") + `]}`)
_, rewritten := normalizeCodexCallOutputBootstrap(body, isCodexAutomationCandidate, true)
if rewritten {
rewrites++
require.True(t, mayContainCodexCallOutputCandidate(body, "automation_update"), "%s", body)
}
}
require.Positive(t, rewrites)
}