Files
magpie/internal/gateway/deepseek_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

86 lines
3.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gateway
import (
"net/http"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// A Claude Code conversation on one of two DeepSeek keys stays on it
// across a pause DeepSeek still has it cached through (on X: "Claude code
// 通过 Magpie 链接 DeepSeek 缓存命中率只有 50%"). DeepSeek clears an unused
// cache after hours, and a real conversation read 98–99% back after 7 and
// 26 minutes idle, but five minutes was its cold, so routing moved the
// conversation to the other key, which read none of it from cache.
// Another vendor's key is cold at five minutes as before.
func TestDeepSeekConversationStaysPastFiveMinutes(t *testing.T) {
deepseek := func(key string) candidate {
return candidate{p: provider.Provider{ID: "deepseek", Key: key, Chat: "https://api.deepseek.com/v1", Anthropic: "https://api.deepseek.com/anthropic"}, model: "deepseek-v4-pro", rest: "deepseek"}
}
other := func(key string) candidate {
return candidate{p: provider.Provider{ID: "ant", Key: key, Anthropic: "https://api.anthropic.com"}, model: "claude-sonnet-5-5", rest: "ant"}
}
first := []byte(`{"messages":[{"role":"user","content":"fix the bug"}]}`)
again := []byte(`{"messages":[{"role":"user","content":"fix the bug"},{"role":"assistant","content":"done"},{"role":"user","content":"now the tests"}]}`)
for i, c := range []struct {
name string
last, next candidate
ago time.Duration
why string
}{
{"DeepSeek, 6 minutes on", deepseek("sk-a"), deepseek("sk-b"), 6 * time.Minute, "cache"},
{"DeepSeek, 26 minutes on", deepseek("sk-a"), deepseek("sk-b"), 26 * time.Minute, "cache"},
{"DeepSeek, past its window", deepseek("sk-a"), deepseek("sk-b"), deepseekCold + time.Minute, "cold"},
{"another vendor, 6 minutes on", other("sk-a"), other("sk-b"), 6 * time.Minute, "cold"},
{"another vendor, 4 minutes on", other("sk-a"), other("sk-b"), 4 * time.Minute, "cache"},
} {
h := http.Header{}
h.Set("x-claude-code-session-id", "deepseek-cold-"+string(rune('a'+i)))
cs := []candidate{c.next, c.last}
pl := planned{order: []Weighed{{}, {}}}
_, _, _, key := affine("t", "", false, false, false, false, h, provider.Anthropic, first, first, cs, pl)
answered(key, c.last, 1, Usage{Input: 300, CacheRead: 13696})
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.Anthropic, 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)
}
want := c.next
if 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())
}
}
}
// A group's rules keep a DeepSeek conversation's effort through the same
// pause (keepsEffort): the window comes from coldAfter.
func TestDeepSeekColdAfter(t *testing.T) {
ds := provider.Provider{ID: "deepseek", Anthropic: "https://api.deepseek.com/anthropic"}
if got := coldAfter(ds); got != deepseekCold {
t.Fatalf("DeepSeek's cold is %v, want %v", got, deepseekCold)
}
for _, p := range []provider.Provider{
{ID: "ant", Anthropic: "https://api.anthropic.com"},
{ID: "relay", Chat: "https://relay.example.com/v1"},
} {
if got := coldAfter(p); got != cacheCold {
t.Fatalf("%s's cold is %v, want %v", p.ID, got, cacheCold)
}
}
at := time.Date(2026, 10, 31, 9, 0, 0, 0, time.UTC)
tr := turnRule{effort: "high", at: at, answered: at, size: 40_000, cold: coldAfter(ds)}
if !keepsEffort(tr, "low", 41_000, at.Add(26*time.Minute)) {
t.Fatal("a DeepSeek conversation 26 minutes on dropped its effort")
}
}