hsjzg58958 on X: a vendor limited an account for 300 seconds, and magpie asked it again about every half a second as the agent retried, until the vendor flagged two accounts. Cause: a candidate resting on a 429 was still "tried all the same" when it was the last one left (restLast/probeOne), and the last one left was never rested at all, so every client retry went upstream. Codex on its own sign-in is relayed by codexUpstreamOn outside the candidate loop and had no hold at all. Fix: - timedHeld/vendorTimed (fallback.go): when every candidate left rests on a 429 whose time the vendor named (Retry-After, anthropic-ratelimit-*- reset, x-ratelimit-reset-*, resets_in_seconds/resets_at), none is asked; the agent gets 429 with Retry-After = when the first is back. A ChatGPT account out of its allowance is answered as the backend's usage_limit_reached with its x-codex-* windows (Rest.out/outHeader). - The last one left now rests too on such a 429 (vendorSaidWhen); a 429 naming no time rests nobody, as before. - codexHolds (codex_held.go): Codex's own relayed sign-in is held till the vendor's time; its retries are answered locally with the stored body, x-codex-* headers and Retry-After. Lifted by restarted() for quota holds. - Not held: LimitRestOff accounts (the user's setting), auto-reset weeks, rests "there" (answered by the vendor's own hold), Codex's API key. Run: - New TestRateLimitedKeysHeldUntilTheirReset (4 signals x 1 and 2 keys) and TestRateLimitedCodexAccountsHeldUntilTheirReset (3 refusals x 1 and 2 accounts): red atfc9ce34d8(each key/account asked on every retry), green after. - Broken by hand: held branch off -> every case red (4 extra hits); last-one rest off -> 1-key cases red, Retry-After lost; codexHeld off -> 1-account Codex cases red. - mote: go test ./internal/gateway -run 'TestRateLimited|TestAllResting| TestQuotaHeld|TestGroupSpent|TestCodex|TestRetryAfter|TestHeldLimit' ok; TestCodexLeavingModelCompactsLocally updated (the used-up account is now asked once, then held) and ok. - gofmt -l clean; go vet, GOOS=linux -tags nogui and GOOS=windows go vet and build ok. - mote, whole internal/gateway package at the rebased head 46199a6ae (on80161247a): ok but for TestStrictAffinityPassesTheErrorOn, which asserted the bug itself ("resting now, it is still the one asked"). A strict conversation's account resting till ChatGPT's named reset now gets its own usage-limit 429 from the rest, with nobody asked; updated, and with TestRateLimited|TestCodexLeavingModel|TestCodexEveryAccountOut ok at this head. - The full ./... suite was queued behind six other sessions' suites on mote's lock and not run before the push; queued again at this head. Not tried: a real vendor account limited live (no account was rate limited to test on); Codex API-key relay is not held (OpenAI's platform headers are Codex's own to read); plugin providers are held only through the shared candidate loop; a held Codex passthrough body's resets_in_seconds is stale by the time elapsed (Retry-After is exact).
110 lines
4.4 KiB
Go
110 lines
4.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/tidwall/gjson"
|
|
"github.com/yetone/magpie/internal/codexsummary"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// hiwise_aigc on X: Codex's allowance used up, an existing thread switched
|
|
// to a magpie model failed every turn with ChatGPT's usage limit, "还会走
|
|
// gpt模型", restarts or not; a new thread worked. Before a turn on a model
|
|
// with a smaller window Codex compacts the thread on the model it was on
|
|
// (reason model_downshift). Under magpie's provider, the one Codex has
|
|
// while its sign-in is used up, that went to the used-up account, and
|
|
// Codex asks the turn's own model instead only under its provider named
|
|
// "OpenAI": the refusal was the turn's. Compacted locally, the thread goes
|
|
// on on the model it was switched to. A compaction for the thread's own
|
|
// model running out of room (context_limit) still says the refusal, and
|
|
// so does the old model's compaction on CodexPath, where Codex's "OpenAI"
|
|
// provider asks the turn's model itself.
|
|
func TestCodexLeavingModelCompactsLocally(t *testing.T) {
|
|
codexSignedIn(t)
|
|
old := codexHeldKnown
|
|
codexHeldKnown = func() bool { return true }
|
|
t.Cleanup(func() { codexHeldKnown = old })
|
|
var tried, models []string
|
|
usedUp(t, &tried, &models)
|
|
if err := provider.Save(provider.Provider{ID: "codex", Models: []string{"gpt-5.5"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
meta := func(reason string) string {
|
|
return `{"session_id":"th1","thread_id":"th1","turn_id":"tu1","request_kind":"compaction","compaction":{"trigger":"auto","reason":"` +
|
|
reason + `","implementation":"responses_compaction_v2","phase":"pre_turn","strategy":"memento"}}`
|
|
}
|
|
body := func(meta string) string {
|
|
cm, _ := json.Marshal(map[string]string{"x-codex-turn-metadata": meta})
|
|
return `{"model":"gpt-5.5","stream":true,"prompt_cache_key":"th1","client_metadata":` + string(cm) + `,"input":[` +
|
|
`{"type":"message","role":"user","content":[{"type":"input_text","text":"build the parser"}]},` +
|
|
`{"type":"message","role":"assistant","content":[{"type":"output_text","text":"the parser is built"}]},` +
|
|
`{"type":"compaction_trigger"}]}`
|
|
}
|
|
post := func(path, reason string, header bool) (int, string) {
|
|
t.Helper()
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest("POST", path, strings.NewReader(body(meta(reason))))
|
|
if path == CodexPath+"/responses" {
|
|
req.Header.Set("Authorization", "Bearer chatgpt-token")
|
|
req.Header.Set("chatgpt-account-id", "acct-1")
|
|
}
|
|
if header {
|
|
req.Header.Set(turnMetadataHeader, meta(reason))
|
|
}
|
|
New().Handler().ServeHTTP(rec, req)
|
|
return rec.Code, rec.Body.String()
|
|
}
|
|
summary := func(sse string) string {
|
|
for _, l := range strings.Split(sse, "\n") {
|
|
d, ok := strings.CutPrefix(l, "data: ")
|
|
if !ok || gjson.Get(d, "type").Str != "response.output_item.done" {
|
|
continue
|
|
}
|
|
msg, ours := codexsummary.Message(gjson.Get(d, "item.encrypted_content").Str)
|
|
if !ours {
|
|
return ""
|
|
}
|
|
b, _ := json.Marshal(msg)
|
|
return string(b)
|
|
}
|
|
return ""
|
|
}
|
|
|
|
for _, c := range []struct {
|
|
reason string
|
|
header bool
|
|
}{{"model_downshift", true}, {"comp_hash_changed", true}, {"model_downshift", false}} {
|
|
tried = nil
|
|
code, out := post("/v1/responses", c.reason, c.header)
|
|
if code != 200 {
|
|
t.Fatalf("%s (header %v): %d %s", c.reason, c.header, code, out)
|
|
}
|
|
// asked once; refused with the time it is back, it is held till
|
|
// then, and the next compactions are answered from that refusal
|
|
// without asking it again (timedHeld, hsjzg58958 on X)
|
|
want := "acct-1"
|
|
if c.reason != "model_downshift" || !c.header {
|
|
want = ""
|
|
}
|
|
if strings.Join(tried, ",") != want {
|
|
t.Fatalf("%s: the old model was asked of %v, want %q", c.reason, tried, want)
|
|
}
|
|
if s := summary(out); !strings.Contains(s, "build the parser") || !strings.Contains(s, "the parser is built") {
|
|
t.Fatalf("%s: no local summary of the thread: %q\n%s", c.reason, s, out)
|
|
}
|
|
}
|
|
|
|
// the thread's own model out of room: the refusal is said, as before
|
|
if code, out := post("/v1/responses", "context_limit", true); code != 429 || !strings.Contains(out, "usage limit") {
|
|
t.Fatalf("context_limit: %d %s", code, out)
|
|
}
|
|
// Codex's own provider asks the turn's model by itself
|
|
if code, out := post(CodexPath+"/responses", "model_downshift", true); code != 429 || !strings.Contains(out, "usage limit") {
|
|
t.Fatalf("CodexPath model_downshift: %d %s", code, out)
|
|
}
|
|
}
|