jessyoungyu on X: when a masking rule hits, send the request to a self-hosted model instead, so a prompt like 「百度这家公司怎么样」 gets an answer about Baidu, not about a placeholder. Settings › Privacy › Instead of masking, send to (settings redactRouteTo). It is Off by default, and nothing changes for a user who leaves it Off. It is picked in the app menu, and only models whose every route is unmasked can be chosen: the provider is set to Send requests unmasked, it is on this computer or the network, it has no account, and any fallback or group member is unmasked too (gateway.MaskRoute, the same rule as provider.SkipsRedaction). The setting is saved on its own with POST /api/settings/redact-route, which checks it with MaskRoute. The general settings save keeps the value. Decisions: - When a value would actually be masked on an agent's turn (maskHits.n > 0), serve sends the request to the chosen model unmasked. Warn-only hits don't count, and Block still answers 400. - If that model fails, the request fails. It is never masked and re-sent to the model the agent asked for, and never goes to a cloud model with the unmasked text. Falling back to a masked cloud request would answer a different question in the background. The user picked this route because masking breaks the answer. - Streaming works the same as on any route. - The route is checked again on every request. A model that is gone or no longer unmasked, or that the caller's gateway key may not use, means the request is masked as before. - Covered: agents' Messages, Chat, Responses and Gemini turns (serve, marked by serveAgent), count_tokens, Codex magpie-model turns, and Codex own-sign-in /responses (codexMaskRoute). - Not covered: magpie's own asks (ownAsk clears the mark), embeddings, audio, images and video. These stay masked. GUI: redactRouteRow lives in plugins/redact/settings.js and wraps renderRedact, so app.js is byte-for-byte unchanged and the core budget is not raised. Strings are added in zh, zh-TW, ja and de. Docs: gateway-routing.md, gui-shell.md, and site/public/docs masking (en, zh, ja). Tests: internal/gateway/mask_route_test.go (TestMaskedRequestGoesToTheModelSet: unset, set, streamed, nothing to mask, local fails, falls back to a vendor, no longer unmasked, count_tokens; TestCodexSigninTurnGoesToTheModelSet; TestMaskRouteTakesOnlyUnmaskedRoutes; TestMaskRouteOnlyForTheAgentsTurn); internal/gui/redact_route_test.go; settings_keep_test.go; internal/gui/tests/redact-route.test.cjs (every language, chromium and webkit, 1100 and 640 wide). Broken by hand, each red: - serveAgent without the agent mark; - MaskRoute taking any route (TestMaskedRequestGoesToTheModelSet, TestMaskRouteTakesOnlyUnmaskedRoutes; at the pushed head); - the codex hook removed; - count_tokens without the mark; - ownAsk not clearing it; - no key check; - no reroute in serve; - the GUI: no keep in the general save, no MaskRoute check on save, and the picker listing every model; - the plugin script left out of index.html (redact-route.test.cjs 22 of 24 red; at the pushed head). Run on mote: - at 221ede767's tree, this change onf5806ed21(pushed on44d697993, which adds only app.css lines and a Gateway page GUI test): go test -tags nogui ./internal/gui ./internal/settings ok (the core budget is green again since ec789445d; #4307, filed and closed); the mask-route tests in ./internal/gateway ok; GOOS=linux go vet -tags nogui ./internal/gui ./internal/gateway ok; GUI: node --test of redact-route, redact-*, privacy-*, mask-*, masking-*, settings-*, i18n-keys, account-discovery, sessions-reads and gui-de/ja/zh-tw: 643 pass, 0 fail. gofmt -l clean. - at the same change onaf8f3a939, one rebase back (the only difference is index.html's script order and main's own commits): go test -timeout 45m ./internal/gateway (488s) ./internal/redact ./internal/settings ./internal/provider ok; GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./..., go vet ./... ok. The whole Go suite was not run; every package this change touches was run above. Not tried: a real self-hosted model (Ollama) with a real agent. The local model is a fake Chat server in the tests.
219 lines
9.5 KiB
Go
219 lines
9.5 KiB
Go
package gateway
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/access"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// jessyoungyu on X: 「{{百度}}这家公司怎么样」 can't be answered by a model
|
|
// that gets the placeholder. With a model set for requests masking would
|
|
// change (settings.RedactRouteTo), one whose provider the user set to go
|
|
// unmasked on this machine or the network, such a request goes there as
|
|
// written and never to the vendor; one masking leaves alone goes where it
|
|
// was asked; unset, as by default, it is masked as before.
|
|
|
|
const routeWord = "百度"
|
|
|
|
// maskRouteSetup is the vendor ("fake", Messages, masked) and a local
|
|
// model ("near/qwen", Chat, set to go unmasked); routeTo is the setting.
|
|
func maskRouteSetup(t *testing.T, routeTo string, near func(*provider.Provider)) (vendor, local *fake) {
|
|
t.Helper()
|
|
vendor = &fake{ctype: "application/json", reply: `{"id":"msg_1","type":"message","role":"assistant","model":"m1","content":[{"type":"text","text":"vendor"}],"stop_reason":"end_turn","usage":{"input_tokens":3,"output_tokens":1}}`}
|
|
setup(t, provider.Anthropic, vendor)
|
|
// a Chat provider is asked to stream whether the agent streams or not
|
|
local = &fake{reply: sse(`data: {"id":"c1","object":"chat.completion.chunk","model":"qwen","choices":[{"index":0,"delta":{"role":"assistant","content":"local"}}]}`,
|
|
`data: {"id":"c1","object":"chat.completion.chunk","model":"qwen","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":1}}`,
|
|
`data: [DONE]`)}
|
|
up := httptest.NewServer(local)
|
|
t.Cleanup(up.Close)
|
|
p := provider.Provider{ID: "near", Name: "Near", Key: "k", Models: []string{"qwen"}, Chat: up.URL + "/v1", Unredacted: true}
|
|
if near != nil {
|
|
near(&p)
|
|
}
|
|
if err := provider.Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := settings.Save(settings.Settings{Redact: true, RedactWords: []string{routeWord}, RedactRouteTo: routeTo}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return vendor, local
|
|
}
|
|
|
|
func messagesBody(text string, stream bool) string {
|
|
s := ""
|
|
if stream {
|
|
s = `"stream":true,`
|
|
}
|
|
return `{"model":"fake/m1",` + s + `"max_tokens":10,"messages":[{"role":"user","content":"` + text + `"}]}`
|
|
}
|
|
|
|
func TestMaskedRequestGoesToTheModelSet(t *testing.T) {
|
|
ask := routeWord + "这家公司怎么样"
|
|
t.Run("unset, as by default: masked to the vendor", func(t *testing.T) {
|
|
vendor, local := maskRouteSetup(t, "", nil)
|
|
if code, reply := post(t, "/v1/messages", messagesBody(ask, false)); code != 200 || !strings.Contains(reply, "vendor") {
|
|
t.Fatalf("%d %s", code, reply)
|
|
}
|
|
if strings.Contains(string(vendor.got), routeWord) || !strings.Contains(string(vendor.got), "{{TERM_") || local.calls != 0 {
|
|
t.Fatalf("vendor got %s, local asked %d times", vendor.got, local.calls)
|
|
}
|
|
})
|
|
t.Run("set: as written to the local model, nothing to the vendor", func(t *testing.T) {
|
|
vendor, local := maskRouteSetup(t, "near/qwen", nil)
|
|
code, reply := post(t, "/v1/messages", messagesBody(ask, false))
|
|
if code != 200 || !strings.Contains(reply, "local") {
|
|
t.Fatalf("%d %s", code, reply)
|
|
}
|
|
if vendor.calls != 0 || !strings.Contains(string(local.got), ask) || strings.Contains(string(local.got), "{{") || !strings.Contains(string(local.got), `"qwen"`) {
|
|
t.Fatalf("vendor asked %d times; local got %s", vendor.calls, local.got)
|
|
}
|
|
})
|
|
t.Run("set, streamed", func(t *testing.T) {
|
|
vendor, local := maskRouteSetup(t, "near/qwen", nil)
|
|
code, reply := post(t, "/v1/messages", messagesBody(ask, true))
|
|
if code != 200 || !strings.Contains(reply, "local") || !strings.Contains(reply, "message_stop") {
|
|
t.Fatalf("%d %s", code, reply)
|
|
}
|
|
if vendor.calls != 0 || !strings.Contains(string(local.got), ask) {
|
|
t.Fatalf("vendor asked %d times; local got %s", vendor.calls, local.got)
|
|
}
|
|
})
|
|
t.Run("set, nothing to mask: to the model asked for", func(t *testing.T) {
|
|
vendor, local := maskRouteSetup(t, "near/qwen", nil)
|
|
if code, reply := post(t, "/v1/messages", messagesBody("hello there", false)); code != 200 || !strings.Contains(reply, "vendor") {
|
|
t.Fatalf("%d %s", code, reply)
|
|
}
|
|
if local.calls != 0 || vendor.calls != 1 {
|
|
t.Fatalf("local asked %d, vendor %d", local.calls, vendor.calls)
|
|
}
|
|
})
|
|
t.Run("set, the local model fails: the request fails, nothing to the vendor", func(t *testing.T) {
|
|
vendor, local := maskRouteSetup(t, "near/qwen", nil)
|
|
local.code, local.ctype, local.reply = 500, "application/json", `{"error":{"message":"down"}}`
|
|
if code, reply := post(t, "/v1/messages", messagesBody(ask, false)); code < 400 {
|
|
t.Fatalf("%d %s", code, reply)
|
|
}
|
|
if vendor.calls != 0 || local.calls == 0 {
|
|
t.Fatalf("vendor asked %d, local %d", vendor.calls, local.calls)
|
|
}
|
|
})
|
|
t.Run("set, but it falls back to a vendor: masked as before", func(t *testing.T) {
|
|
vendor, local := maskRouteSetup(t, "near/qwen", func(p *provider.Provider) { p.Fallback = []string{"fake/m1"} })
|
|
if code, reply := post(t, "/v1/messages", messagesBody(ask, false)); code != 200 || !strings.Contains(reply, "vendor") {
|
|
t.Fatalf("%d %s", code, reply)
|
|
}
|
|
if local.calls != 0 || strings.Contains(string(vendor.got), routeWord) {
|
|
t.Fatalf("local asked %d; vendor got %s", local.calls, vendor.got)
|
|
}
|
|
})
|
|
t.Run("set, but no longer set to go unmasked: masked as before", func(t *testing.T) {
|
|
vendor, local := maskRouteSetup(t, "near/qwen", func(p *provider.Provider) { p.Unredacted = false })
|
|
if code, reply := post(t, "/v1/messages", messagesBody(ask, false)); code != 200 || !strings.Contains(reply, "vendor") {
|
|
t.Fatalf("%d %s", code, reply)
|
|
}
|
|
if local.calls != 0 || strings.Contains(string(vendor.got), routeWord) {
|
|
t.Fatalf("local asked %d; vendor got %s", local.calls, vendor.got)
|
|
}
|
|
})
|
|
t.Run("set: counted on the local model, as written", func(t *testing.T) {
|
|
vendor, local := maskRouteSetup(t, "near/qwen", nil)
|
|
code, reply := post(t, "/v1/messages/count_tokens", `{"model":"fake/m1","messages":[{"role":"user","content":"`+ask+`"}]}`)
|
|
if code != 200 {
|
|
t.Fatalf("%d %s", code, reply)
|
|
}
|
|
if vendor.calls != 0 {
|
|
t.Fatalf("counted on the vendor: %s", vendor.got)
|
|
}
|
|
if !strings.Contains(string(local.got), ask) && local.calls != 0 {
|
|
t.Fatalf("local counted %s", local.got)
|
|
}
|
|
})
|
|
}
|
|
|
|
// A turn of Codex's on one of its own models, relayed on its ChatGPT
|
|
// sign-in, goes to the model set too, and the ChatGPT backend gets none.
|
|
func TestCodexSigninTurnGoesToTheModelSet(t *testing.T) {
|
|
for _, routeTo := range []string{"", "near/qwen"} {
|
|
t.Run("route "+routeTo, func(t *testing.T) {
|
|
_, local := maskRouteSetup(t, routeTo, nil)
|
|
chatgpt := &fake{reply: sse(`data: {"type":"response.created","response":{"id":"r1","model":"gpt-5.5"}}`,
|
|
`data: {"type":"response.output_text.delta","delta":"chatgpt"}`,
|
|
`data: {"type":"response.completed","response":{"id":"r1","usage":{"input_tokens":7,"output_tokens":1}}}`)}
|
|
up := httptest.NewServer(chatgpt)
|
|
t.Cleanup(up.Close)
|
|
was := provider.CodexBase
|
|
provider.CodexBase = up.URL + "/backend-api/codex"
|
|
t.Cleanup(func() { provider.CodexBase = was })
|
|
code, reply := codexPost(t, `{"model":"gpt-5.5","stream":true,"store":false,"input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"`+routeWord+`这家公司怎么样"}]}]}`)
|
|
if code != http.StatusOK {
|
|
t.Fatalf("%d %s", code, reply)
|
|
}
|
|
if routeTo == "" {
|
|
if local.calls != 0 || chatgpt.calls != 1 || strings.Contains(string(chatgpt.got), routeWord) {
|
|
t.Fatalf("local %d; ChatGPT got %s", local.calls, chatgpt.got)
|
|
}
|
|
return
|
|
}
|
|
if chatgpt.calls != 0 || !strings.Contains(string(local.got), routeWord) || !strings.Contains(reply, "local") {
|
|
t.Fatalf("ChatGPT asked %d; local got %s; answered %s", chatgpt.calls, local.got, reply)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// What the GUI lists and the setting takes: a model whose every route goes
|
|
// unmasked, not a vendor's, nor one that falls back to one.
|
|
func TestMaskRouteTakesOnlyUnmaskedRoutes(t *testing.T) {
|
|
maskRouteSetup(t, "", nil)
|
|
if !MaskRoute("near/qwen") {
|
|
t.Fatal("near/qwen goes unmasked")
|
|
}
|
|
for _, id := range []string{"", "fake/m1", "nowhere/x"} {
|
|
if MaskRoute(id) {
|
|
t.Fatalf("%q taken", id)
|
|
}
|
|
}
|
|
maskRouteSetup(t, "", func(p *provider.Provider) { p.Fallback = []string{"fake/m1"} })
|
|
if MaskRoute("near/qwen") {
|
|
t.Fatal("one falling back to a vendor taken")
|
|
}
|
|
}
|
|
|
|
// Only an agent's own turn goes to the model set: magpie's own asks made
|
|
// on its behalf (a title, a search stand-in, a picture described) are
|
|
// masked as before, and so is a turn on a gateway key the model set is
|
|
// not one of the key's models.
|
|
func TestMaskRouteOnlyForTheAgentsTurn(t *testing.T) {
|
|
maskRouteSetup(t, "near/qwen", nil)
|
|
hit := maskHits{n: 1}
|
|
r := httptest.NewRequest("POST", "/v1/messages", nil)
|
|
if got := maskRouteFor(r, hit); got != "" {
|
|
t.Fatalf("not an agent's turn: %q", got)
|
|
}
|
|
turn := agentTurn(r)
|
|
if got := maskRouteFor(turn, hit); got != "near/qwen" {
|
|
t.Fatalf("the agent's turn: %q", got)
|
|
}
|
|
if got := maskRouteFor(turn, maskHits{}); got != "" {
|
|
t.Fatalf("nothing masked: %q", got)
|
|
}
|
|
if got := maskRouteFor(turn.WithContext(ownAsk(turn.Context())), hit); got != "" {
|
|
t.Fatalf("magpie's own ask: %q", got)
|
|
}
|
|
keyed := func(models ...string) *http.Request {
|
|
return turn.WithContext(access.WithIdentity(turn.Context(), access.Identity{KeyID: "k1", Models: models}))
|
|
}
|
|
if got := maskRouteFor(keyed("fake/*"), hit); got != "" {
|
|
t.Fatalf("a key without it: %q", got)
|
|
}
|
|
if got := maskRouteFor(keyed("fake/*", "near/*"), hit); got != "near/qwen" {
|
|
t.Fatalf("a key with it: %q", got)
|
|
}
|
|
}
|