Files
magpie/internal/gateway/chatgpt_cold_test.go
T
yetone 6c3f113cee routing: a rule's hours over give the conversation back; looser times (MinglunZhu on X)
MinglunZhu asked twice for a provider by time of day and said the time
setting in Routing rules is awkward. Three causes:

- A conversation a night rule sent to its member stayed there after the
  hours: no rule matched the new turn, so Auto affinity kept it on the
  night's member by its cache (Smart and every routing but In order).
  ruleFor now sets RuleHit.Ended when the last turn's rule had hours
  that are over, and affine lets the conversation go (Affinity.Why
  "hours"); the trace says so in en, zh, zh-TW, ja, de. Tool rounds of
  a turn begun inside the hours stay put; Session, Strict and In turn
  keep their own way.
- The time boxes took only ASCII "HH:MM", while their numeric keypad
  on a phone has no colon and a Chinese IME types ":". clockOf (GUI)
  and clockMinutes (CLI, saved files) now read "22", "2200", "930",
  "22:00", "22.00", "22点30" and full-width digits; "9:5" is still
  no time. time=22~08 takes ~ and ~ for the dash.
- A new rule starts with an intent box; hours took an × and a +. Adding
  another condition now drops the intent box while it is empty.

A time rule in a nested group doesn't give its conversation back yet:
#5977.

Run (mote): go test ./internal/provider (ok); go test -run
'TestRuleHoursOver|TestRuleTime|TestRule|Affinity|StandIn|Session|TestParse|Clock'
./internal/gateway (ok); go vet darwin, GOOS=linux -tags nogui,
GOOS=windows for gateway and provider; gofmt. GUI, chromium and webkit:
routing-time (8/8 after the new trace test's fix), and 28 rule/affinity/
i18n files (routing-rule-*, routing-time*, routing-intent-entry,
routing-folder, routing-pause*, routing-smart-intent, routing-save*,
routing-rules-line, routing-affinity-*, routing-behind-why,
routing-kept-again, routing-session-folder, i18n-keys, i18n-en-no-cjk):
477/481, the 4 reds the new trace test's first wording, fixed and rerun.

Broken by hand: affine's "hours" case off -> TestRuleHoursOverGiveThe
ConversationBack red (Smart stays on kb by cache); origin's
grouprule.go/ruleparse.go -> TestRuleTimeTypedLoosely red; client.js's
"hours" case and the intent give-way off -> routing-time 0/4 in
chromium; the old clockOf -> routing-time red on "9:00".

Not tried: the real app with a real night/day pair of providers; the
full gateway package and full GUI suite (the full Go suite runs after
the tag); Windows run of the touched packages (vet only).
2026-11-10 22:57:20 +08:00

84 lines
4.0 KiB
Go

package gateway
import (
"net/http"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// A conversation on one of two ChatGPT accounts stays on it across a
// pause the ChatGPT backend still has it cached through (leelantao on X:
// two Plus accounts spent 90% in a day). Five minutes was every vendor's
// cold, so after a coffee break routing moved the conversation to the
// other account, which read none of it from cache and refused the
// reasoning the first had sealed. An API key's conversation is cold at
// five minutes as before, and a ChatGPT one past chatGPTCold.
func TestChatGPTConversationStaysPastFiveMinutes(t *testing.T) {
chatgpt := func(user string) candidate {
return candidate{p: provider.Provider{ID: "codex", Account: &provider.Account{Agent: "codex", User: user}}, model: "gpt-6-sol", rest: "codex"}
}
keyed := func(key string) candidate {
return candidate{p: provider.Provider{ID: "oai", Key: key, Responses: "https://api.openai.com/v1"}, model: "gpt-6-sol", rest: "oai"}
}
first := []byte(`{"input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"fix the bug"}]}]}`)
again := []byte(`{"input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"fix the bug"}]},{"type":"message","role":"assistant","content":[{"type":"output_text","text":"done"}]},{"type":"message","role":"user","content":[{"type":"input_text","text":"now the tests"}]}]}`)
for i, c := range []struct {
name string
last, next candidate
ago time.Duration
why string
}{
{"ChatGPT, 6 minutes on", chatgpt("a@example.com"), chatgpt("b@example.com"), 6 * time.Minute, "cache"},
{"ChatGPT, 12 minutes on", chatgpt("a@example.com"), chatgpt("b@example.com"), 12 * time.Minute, "cache"},
{"ChatGPT, past its window", chatgpt("a@example.com"), chatgpt("b@example.com"), chatGPTCold + time.Minute, "cold"},
{"an API key, 6 minutes on", keyed("sk-a"), keyed("sk-b"), 6 * time.Minute, "cold"},
{"an API key, 4 minutes on", keyed("sk-a"), keyed("sk-b"), 4 * time.Minute, "cache"},
} {
h := http.Header{}
h.Set("x-claude-code-session-id", "chatgpt-cold-"+string(rune('a'+i)))
// the other account goes first by routing: Smart puts the one with
// more left first, which after a turn on the last is the other
cs := []candidate{c.next, c.last}
pl := planned{order: []Weighed{{}, {}}}
_, _, _, key := affine("t", "", false, false, false, false, h, provider.Responses, first, first, cs, pl)
answered(key, c.last, 1, Usage{Input: 1200, CacheRead: 20000, CacheWrite: 1200})
t.Cleanup(func() { sticks.Lock(); delete(sticks.m, key); sticks.Unlock() })
sticks.Lock()
st := sticks.m[key]
st.at = st.at.Add(-c.ago)
sticks.m[key] = st
sticks.Unlock()
got, _, a, _ := affine("t", "", false, false, false, false, h, provider.Responses, again, again, cs, pl)
if a.Why != c.why || a.Kept != (c.why == "cache") {
t.Fatalf("%s: why %q kept %v, want %q", c.name, a.Why, a.Kept, c.why)
}
if want := c.next; c.why == "cache" {
want = c.last
if got[0].who() != want.who() {
t.Fatalf("%s: went to %s, want %s", c.name, got[0].who(), want.who())
}
} else if got[0].who() != want.who() {
t.Fatalf("%s: went to %s, want routing's %s", c.name, got[0].who(), want.who())
}
}
}
// A group's rules keep a ChatGPT conversation's effort through the same
// pause (keepsEffort): a lower pick then reads none of the cache back.
func TestChatGPTKeepsEffortPastFiveMinutes(t *testing.T) {
at := time.Date(2026, 10, 27, 9, 0, 0, 0, time.UTC)
tr := turnRule{effort: "high", at: at, answered: at, size: 40_000, cold: chatGPTCold}
if !keepsEffort(tr, "low", 41_000, at.Add(12*time.Minute)) {
t.Fatal("a ChatGPT conversation 12 minutes on dropped its effort")
}
if keepsEffort(tr, "low", 41_000, at.Add(chatGPTCold+time.Minute)) {
t.Fatal("kept its effort past the ChatGPT backend's window")
}
tr.cold = 0
if keepsEffort(tr, "low", 41_000, at.Add(6*time.Minute)) {
t.Fatal("another vendor's conversation kept its effort past five minutes")
}
}