gateway: the chat page's turn is held to its key's MaxConcurrency (#6688)

6bdeedc6f added the /deepseek-chat routes without judging them for a
gateway key's MaxConcurrency or the LazyCat manifest, so
TestKeySlotEveryRoute and TestLazyCatManifestListsTheClientRoutes were red
on main. The first was a real gap. The page's turn asks a model through
the Messages route's handler, called in process (chatInner), past
keySlots. A key capped at one slot could therefore run a chat page turn
beside an agent's request, with nothing waiting.

- keySlotRoutes: the page's messages route is lazy, so its rounds' tries
  take the key's slot (chatInner's clone keeps the hold in its context),
  and asr and tts are eager. The file upload and delete routes are free
  (keySlotFree): they are kept in magpie and never sent to a model.
- docs/reference.md's LazyCat manifest makes /deepseek-chat public, as it
  does the key page (/me), for a key's holder who isn't signed in to the
  box. The page's static files are keyless already; every call it makes
  still needs a gateway key.

gofmt realigned keySlotRoutes for the longer key.

Run: go test -tags nogui -count=1 -timeout 45m ./internal/gateway on
mica: ok (1005s). The new TestKeyConcurrencyChatPage is red with the
messages entry taken out of keySlotRoutes ("timed out waiting for the
chat page's turn waits for the key's slot"), broken by hand on mica.
This commit is contained in:
yetone
2026-11-15 00:38:06 +08:00
parent e9fcc62294
commit cd030691a3
4 changed files with 107 additions and 38 deletions
+1
View File
@@ -3868,6 +3868,7 @@ application:
- /alpha/search
- /mcp
- /me
- /deepseek-chat
- /join
- /magpie-ca.crt
services:
+11 -3
View File
@@ -1676,18 +1676,26 @@ first as the page's default) and sent as the `magpie_chat_model` cookie.
runs with `fetchPage` between rounds, all said to the page as one reply
in its own shapes (`chatTurn`). Each round goes through the gateway's
own Messages route, so routing, masking, limits and usage apply as to
any agent. The page shows sources only from text blocks' `citations`; a
model that names URLs in its text without citing gets no source chips.
any agent. A gateway key's MaxConcurrency holds a turn as it holds an
agent's request (`keySlotRoutes`, lazy: the rounds' tries take the
slot, which `chatInner`'s copy of the context carries), and the speech
routes eagerly; the file routes are free. The page shows sources only
from text blocks' `citations`; a model that names URLs in its text
without citing gets no source chips.
- Files the page uploads (`/anthropic/v1/files`) are kept in memory for
the key that uploaded them until magpie restarts: images, PDFs and
text; Word, PowerPoint and EPUB files are refused.
- Voice input and read-aloud (`/v1/tools/asr`, `/v1/tools/tts`) use the
first speech-to-text or text-to-speech model a provider serves, and say
so when there is none.
- A LazyCat box's manifest makes `/deepseek-chat` public, as it does the
key page, for a key's holder who isn't signed in to the box
(docs/reference.md).
Tests: `TestChatPageOpensWithoutAKey`, `TestChatPageConversation`,
`TestChatPageSearchesAndReadsLinks`, `TestChatPageFiles`,
`TestTextCallsCitationAfterItsBlock`, `gateway-chat-page.test.cjs`.
`TestTextCallsCitationAfterItsBlock`, `TestKeyConcurrencyChatPage`,
`gateway-chat-page.test.cjs`.
## Turning provider search off
+41 -35
View File
@@ -47,41 +47,47 @@ const (
// their key's MaxConcurrency, and how; every other route of routes() is in
// keySlotFree (key_slots_test.go).
var keySlotRoutes = map[string]keySlotKind{
"POST /v1/chat/completions": keySlotLazy,
"POST /chat/completions": keySlotLazy,
"POST /v1/responses": keySlotLazy,
"POST /responses": keySlotLazy,
"POST /v1/messages": keySlotLazy,
"POST /messages": keySlotLazy,
"POST /api/chat": keySlotLazy,
"POST /api/generate": keySlotLazy,
"POST /v1beta/models/{call...}": keySlotLazy,
CodexPath + "/": keySlotLazy,
"POST /v1/systemone": keySlotEager,
"POST /v1" + AlphaSearchPath: keySlotEager,
"POST " + AlphaSearchPath: keySlotEager,
"POST /v1/images/generations": keySlotEager,
"POST /images/generations": keySlotEager,
"POST /v1/images/edits": keySlotEager,
"POST /images/edits": keySlotEager,
"POST /v1/embeddings": keySlotEager,
"POST /embeddings": keySlotEager,
"POST /v1/rerank": keySlotEager,
"POST /rerank": keySlotEager,
"POST /v1/decisions": keySlotEager,
"POST /decisions": keySlotEager,
"POST /v1/audio/transcriptions": keySlotEager,
"POST /audio/transcriptions": keySlotEager,
"POST /v1/audio/translations": keySlotEager,
"POST /audio/translations": keySlotEager,
"POST /v1/audio/speech": keySlotEager,
"POST /audio/speech": keySlotEager,
"POST /v1/videos": keySlotEager,
"POST /videos": keySlotEager,
"POST " + NativePath + "{provider}/{path...}": keySlotEager,
"PUT " + NativePath + "{provider}/{path...}": keySlotEager,
"PATCH " + NativePath + "{provider}/{path...}": keySlotEager,
"DELETE " + NativePath + "{provider}/{path...}": keySlotEager,
"POST /v1/chat/completions": keySlotLazy,
"POST /chat/completions": keySlotLazy,
"POST /v1/responses": keySlotLazy,
"POST /responses": keySlotLazy,
"POST /v1/messages": keySlotLazy,
"POST /messages": keySlotLazy,
"POST /api/chat": keySlotLazy,
"POST /api/generate": keySlotLazy,
"POST /v1beta/models/{call...}": keySlotLazy,
CodexPath + "/": keySlotLazy,
"POST /v1/systemone": keySlotEager,
"POST /v1" + AlphaSearchPath: keySlotEager,
"POST " + AlphaSearchPath: keySlotEager,
"POST /v1/images/generations": keySlotEager,
"POST /images/generations": keySlotEager,
"POST /v1/images/edits": keySlotEager,
"POST /images/edits": keySlotEager,
"POST /v1/embeddings": keySlotEager,
"POST /embeddings": keySlotEager,
"POST /v1/rerank": keySlotEager,
"POST /rerank": keySlotEager,
"POST /v1/decisions": keySlotEager,
"POST /decisions": keySlotEager,
"POST /v1/audio/transcriptions": keySlotEager,
"POST /audio/transcriptions": keySlotEager,
"POST /v1/audio/translations": keySlotEager,
"POST /audio/translations": keySlotEager,
"POST /v1/audio/speech": keySlotEager,
"POST /audio/speech": keySlotEager,
"POST /v1/videos": keySlotEager,
"POST /videos": keySlotEager,
// DeepSeek's chat page: a turn's rounds go through the Messages
// route's tries inside it (chatInner keeps the hold), and its speech
// asks a model before its handler begins (chatpage.go)
"POST " + ChatPagePath + "/anthropic/v1/messages": keySlotLazy,
"POST " + ChatPagePath + "/v1/tools/asr": keySlotEager,
"POST " + ChatPagePath + "/v1/tools/tts": keySlotEager,
"POST " + NativePath + "{provider}/{path...}": keySlotEager,
"PUT " + NativePath + "{provider}/{path...}": keySlotEager,
"PATCH " + NativePath + "{provider}/{path...}": keySlotEager,
"DELETE " + NativePath + "{provider}/{path...}": keySlotEager,
}
// keyCapped says whether who's key, or the one it was issued under, has a
+54
View File
@@ -30,6 +30,8 @@ var keySlotFree = []string{
"POST " + claudeMCPPath + "{token}", "/mcp/{name}", "/mcp/run/{name}", SearchMCPPath, VisionMCPPath,
ImageMCPPath, BrowserMCPPath, MemoryMCPPath, ImageMCPFiles + "{name}", ToolsMCPPath, ToolsMCPOne + "{plugin}",
HubMCPPath + "{agent}", HubMCPAll, "/", "POST " + joinData, AdminPath,
// the chat page's files are kept in magpie, never sent to a model
"POST " + ChatPagePath + "/anthropic/v1/files", "DELETE " + ChatPagePath + "/anthropic/v1/files/{id}",
}
// Every route is judged for a key's MaxConcurrency: held (keySlotRoutes)
@@ -319,3 +321,55 @@ func TestKeyConcurrencyEager(t *testing.T) {
t.Fatal("a GET took a slot")
}
}
// DeepSeek's chat page asks a model through the Messages route's tries,
// called in process (chatInner): a turn there waits for its key's slot as
// an agent's request does, and holds it while it asks.
func TestKeyConcurrencyChatPage(t *testing.T) {
fresh(t)
v := &slowVendor{release: map[string]chan struct{}{}, quit: make(chan struct{})}
up := httptest.NewServer(v)
t.Cleanup(up.Close)
if err := provider.Save(provider.Provider{ID: "slow", Name: "Slow", Key: "k", Models: []string{"m"}, Chat: up.URL + "/v1"}); err != nil {
t.Fatal(err)
}
keys, secrets := newCaller(t, "Team")
keyCap(t, keys[0].ID, 1)
s := New()
gw := lanServer(t, s)
t.Cleanup(func() { close(v.quit) })
lane := func() Lane { return s.KeyLanes()[keys[0].ID] }
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
done := make(chan struct{}, 2)
post := func(path, body string) {
go func() {
defer func() { done <- struct{}{} }()
req, _ := http.NewRequestWithContext(ctx, "POST", gw.URL+path, strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+secrets[0])
req.AddCookie(&http.Cookie{Name: chatModelCookie, Value: "slow/m"})
if res, err := http.DefaultClient.Do(req); err == nil {
io.Copy(io.Discard, res.Body)
res.Body.Close()
}
}()
}
post("/v1/chat/completions", `{"model":"slow/m","stream":true,"messages":[{"role":"user","content":"a"}]}`)
within(t, "a at the vendor", func() bool { return len(v.seen()) == 1 })
post(ChatPagePath+"/anthropic/v1/messages", `{"model":"deepseek-flash","messages":[{"role":"user","content":"hi"}]}`)
within(t, "the chat page's turn waits for the key's slot", func() bool { return lane().Waiting == 1 })
if n := len(v.seen()); n != 1 {
t.Fatalf("the chat page reached the vendor with the key's one slot taken: %v", v.seen())
}
close(v.gate("a"))
within(t, "the chat page's turn at the vendor", func() bool { return len(v.seen()) == 2 })
if l := lane(); l.Busy != 1 || l.Waiting != 0 {
t.Fatalf("key lane while the chat page asks = %+v, want its one slot held", l)
}
cancel()
<-done
<-done
within(t, "every key slot given back", func() bool { return len(s.KeyLanes()) == 0 })
}