vvrfxyz on X: "脱敏功能是不是会降低缓存命中率 我开了脱敏后 dsh命中率暴跌".
Cause: the local model (AstrLink Guard, on by default) is waited for
2s a request (masking's defaultWait). A tool's output it hasn't read
by then goes with the rules' masking alone (pii.Partial). On the next
turn the model has read it, finds the names in it, and the same history
text now goes with {{PERSON_…}} in it. Everything from that message on
is a new prompt to the vendor, so its prefix cache misses. 50f52b540
(10-23) kept the known values of a text sent before as they went, but
not the model's finds.
Reproduced with real dsh 0.2.1-rc.2 (headless profile, sandbox HOME):
three turns in one session (read team.md, customers.csv and
notes/standup.md; check contracts.md; summarise the customers) through
a sandbox magpie with masking and the default model on, to a recording
Chat Completions upstream. dsh's own request bodies were recorded and
replayed in order, 3s apart, through a fresh sandbox magpie (found
cache cleared) on Linux (WSL2 magpie-desktop, -tags nogui).
- Old (origin/main without this change): "the local model read 5 of a request's
7 texts in 2s". At request 7, message 6 (customers.csv's tool output)
changed at byte 252: "1000,Fatima Rossi,Client 0 GmbH" became
"1000,{{PERSON_2rmvxto6}},Client 0 GmbH". 4,793 of that request's
18,380 history bytes were sent as before (26%). Over the whole
conversation 97,815 of 111,402 resent bytes (87.8%) were the same.
It happened on both runs.
- New: 108,378 of 108,378 (100%), the same Partial in the log.
- On mote (M5 Pro, CoreML) the model read everything inside 2s. Old
code kept 100% of the prefix over the same 3 turns, and after a
restart of magpie too (suspect 3, the key and the in-memory maps,
was not the cause). Suspects 1 (known values) and 4 (re-encoding)
were not the cause either: every other request kept its prefix byte
for byte.
Fix:
- redact: sentKnownSpans becomes sentSpans. It remembers, per text and
options, both the known values and the model's finds the text first
went with (sentModel, sentKnown). The key now includes Options.Route,
and for the model also the threshold, whether there is a model, and
NamesByModel.
- learnLate: what the model finds in such a text since is made known
(placeholder, scores) without being put in the text, so new text has
it masked as before.
- The gateway gives masking each request's route (maskedFor,
masking.WithRoute): the providers its model, the model's fallbacks or
a group's members are served by, the same set unredactedRoute walks.
Codex's own sign-in passes "codex", or the account's provider.
- A request with no route (search, embeddings, a text prompt) keeps
the model's finds of now, and its known values as before.
Trade-off: a name the model reads late in a text sent before keeps
going as written on that route. That route's vendor was sent it as
written already. Within a route of several providers (fallbacks, a
group), a member that wasn't the first to get it may be sent it in
that form, as it could have been on the first turn's failover. In the
Linux replay, customers.csv's ~110 names stay as written on later
turns, where old code masked them from turn 2 on. The new turns'
masking is unchanged (the same placeholder counts in messages 9, 12
and 14). Another route, never sent the text, gets it masked with what
is known and found now. That is stricter than before for known values,
which were kept whatever the vendor. The alternative, waiting for the
model before any text goes, is what defaultWait was cut to 2s to stop
(shensmart, 10-28).
Tests:
- TestModelReadingLateKeepsTheSentPrefix (internal/masking): a model
that reads a text only after its first request. Turn 2's history must
be byte-identical, the name masked in new text, and masked on another
route and with no route.
- TestMaskedPrefixKeptOnlyOnItsRoute (internal/gateway): a system
prompt kept as written on its provider, masked on another.
- Broken by hand on mote, each red:
- sentModel not remembered: "turn 2 rewrote message 2";
- learnLate removed: "the new turn has the name the model found as
written";
- maskedFor returning ctx without a route: "another provider, never
sent it, got the value as written".
Each was green restored. TestMaskedConversationKeepsItsCachedPrefix
and TestSentTextKeepsItsMasking stay green.
What was run, at this commit on origin/main 4b4c4a806, on mote
(after rebasing on e863f520c, which touches internal/gateway: gofmt, vet
of gateway/masking/redact, redact and masking whole, and gateway -run
"Mask|Redact|Sent|Effort|Route" ok again):
- gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./...,
GOOS=windows go vet ./...: clean.
- Targeted at this head: go test ./internal/redact ./internal/masking
./internal/gateway (ok), with the two new tests and
TestMaskedConversationKeepsItsCachedPrefix, TestSentTextKeepsItsMasking.
- Full suite (go test -timeout 45m ./...) was still running on mote at
push (load ~110, other sessions), pushed per the ship-at-once rule; its
result goes on the release and any red gets its own issue.
- GUI suite not run: no GUI file, API or string changed.
Not tried: vvrfxyz's own machine and vendor (DeepSeek's real cache_hit
counts), and Windows.
188 lines
8.3 KiB
Go
188 lines
8.3 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// With Privacy masking on, a conversation's earlier turns reach the
|
|
// vendor as the same bytes on every turn, so its prompt cache is read: a
|
|
// value a rule finds in one turn's tool output is masked from then on in
|
|
// new text, and the system prompt and the turns already sent with it as
|
|
// written go as they went. Masking it there too rewrote the whole prompt
|
|
// to the cache on the next turn (audie on Discord: DeepSeek Harness on a
|
|
// group with a custom Anthropic provider, 412K to 427K tokens written and
|
|
// none read on adjacent turns, ten seconds apart). Here as dsh sends it,
|
|
// Chat Completions to a group, and as Claude Code sends it, Messages.
|
|
func TestMaskedConversationKeepsItsCachedPrefix(t *testing.T) {
|
|
fresh(t)
|
|
var mu sync.Mutex
|
|
var bodies []map[string]json.RawMessage
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
var q map[string]json.RawMessage
|
|
b, _ := io.ReadAll(r.Body)
|
|
json.Unmarshal(b, &q)
|
|
mu.Lock()
|
|
bodies = append(bodies, q)
|
|
mu.Unlock()
|
|
if string(q["stream"]) == "true" {
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
io.WriteString(w, "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"m\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"usage\":{\"input_tokens\":1,\"output_tokens\":0}}}\n\n"+
|
|
"event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\n"+
|
|
"event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"ok\"}}\n\n"+
|
|
"event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\n"+
|
|
"event: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"end_turn\"},\"usage\":{\"output_tokens\":1}}\n\n"+
|
|
"event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n")
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
io.WriteString(w, `{"id":"m","type":"message","role":"assistant","content":[{"type":"text","text":"ok"}],"stop_reason":"end_turn","usage":{"input_tokens":2,"output_tokens":1}}`)
|
|
}))
|
|
t.Cleanup(up.Close)
|
|
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Models: []string{"claude-opus-5-5"}, Anthropic: up.URL + "/v1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := provider.SaveGroup(provider.Group{ID: "flash", Name: "flash", Members: []string{"relay/claude-opus-5-5"}, Routing: provider.Ordered}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := settings.Save(settings.Settings{Redact: true, RedactPersonal: true}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := New()
|
|
for _, c := range []struct {
|
|
name, path, secret string
|
|
body func(turns []string) string
|
|
turn func(i int, out string) []string
|
|
}{
|
|
{"chat", "/v1/chat/completions", "Hq4cacheDsh81z",
|
|
func(turns []string) string {
|
|
return `{"model":"group/flash","messages":[{"role":"system","content":"You are dsh. Staging deploys with Hq4cacheDsh81z for now."},` +
|
|
`{"role":"user","content":"read the env file"}` + strings.Join(turns, "") + `],` +
|
|
`"tools":[{"type":"function","function":{"name":"bash","description":"run","parameters":{"type":"object","properties":{"cmd":{"type":"string"}}}}}]}`
|
|
},
|
|
func(i int, out string) []string {
|
|
id := fmt.Sprintf("call_%d", i)
|
|
return []string{`,{"role":"assistant","content":"","tool_calls":[{"id":"` + id + `","type":"function","function":{"name":"bash","arguments":"{\"cmd\":\"cat .env\"}"}}]}`,
|
|
`,{"role":"tool","tool_call_id":"` + id + `","content":"` + out + `"}`}
|
|
}},
|
|
{"messages", "/v1/messages", "Wm6cacheCc29qy",
|
|
func(turns []string) string {
|
|
return `{"model":"relay/claude-opus-5-5","max_tokens":1024,"system":"You are Claude. Staging deploys with Wm6cacheCc29qy for now.",` +
|
|
`"tools":[{"name":"bash","description":"run","input_schema":{"type":"object","properties":{"cmd":{"type":"string"}}}}],` +
|
|
`"messages":[{"role":"user","content":"read the env file"}` + strings.Join(turns, "") + `]}`
|
|
},
|
|
func(i int, out string) []string {
|
|
id := fmt.Sprintf("toolu_%d", i)
|
|
return []string{`,{"role":"assistant","content":[{"type":"tool_use","id":"` + id + `","name":"bash","input":{"cmd":"cat .env"}}]}`,
|
|
`,{"role":"user","content":[{"type":"tool_result","tool_use_id":"` + id + `","content":"` + out + `"}]}`}
|
|
}},
|
|
} {
|
|
mu.Lock()
|
|
bodies = nil
|
|
mu.Unlock()
|
|
var turns []string
|
|
for i := range 3 {
|
|
out := "PORT=8080"
|
|
if i == 1 {
|
|
out = "DB_PASSWORD=" + c.secret // a rule finds it here first
|
|
}
|
|
turns = append(turns, c.turn(i, out)...)
|
|
if code, raw := postJSON(t, s, c.path, c.body(turns)); code != 200 {
|
|
t.Fatalf("%s turn %d: %d %s", c.name, i, code, raw)
|
|
}
|
|
}
|
|
mu.Lock()
|
|
got := bodies
|
|
mu.Unlock()
|
|
if len(got) != 3 {
|
|
t.Fatalf("%s: %d requests upstream", c.name, len(got))
|
|
}
|
|
for i := 1; i < len(got); i++ {
|
|
before, now := got[i-1], got[i]
|
|
for _, k := range []string{"system", "tools"} {
|
|
if string(before[k]) != string(now[k]) {
|
|
t.Errorf("%s turn %d: %s changed:\n%s\nwas\n%s", c.name, i, k, now[k], before[k])
|
|
}
|
|
}
|
|
var a, b []json.RawMessage
|
|
json.Unmarshal(before["messages"], &a)
|
|
json.Unmarshal(now["messages"], &b)
|
|
// the turns before, up to the one the cache mark was on
|
|
for j := 0; j < len(a)-1; j++ {
|
|
if string(a[j]) != string(b[j]) {
|
|
t.Errorf("%s turn %d: message %d changed:\n%s\nwas\n%s", c.name, i, j, b[j], a[j])
|
|
}
|
|
}
|
|
}
|
|
last := got[len(got)-1]
|
|
if !strings.Contains(string(last["system"]), c.secret) {
|
|
t.Errorf("%s: the system prompt, sent with the value as written, has it masked now: %s", c.name, last["system"])
|
|
}
|
|
if strings.Contains(string(last["messages"]), c.secret) {
|
|
t.Errorf("%s: the value a rule found went out: %s", c.name, last["messages"])
|
|
}
|
|
}
|
|
}
|
|
|
|
// A text goes as it went only where it went: the system prompt sent to
|
|
// one provider with a value as written, before a rule found the value,
|
|
// keeps it as written there, and has it masked on its way to another
|
|
// provider, which was never sent it (masking's route, maskedFor).
|
|
func TestMaskedPrefixKeptOnlyOnItsRoute(t *testing.T) {
|
|
fresh(t)
|
|
var mu sync.Mutex
|
|
got := map[string][]string{}
|
|
serve := func(name string) *httptest.Server {
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
b, _ := io.ReadAll(r.Body)
|
|
mu.Lock()
|
|
got[name] = append(got[name], string(b))
|
|
mu.Unlock()
|
|
w.Header().Set("Content-Type", "application/json")
|
|
io.WriteString(w, `{"id":"m","type":"message","role":"assistant","content":[{"type":"text","text":"ok"}],"stop_reason":"end_turn","usage":{"input_tokens":2,"output_tokens":1}}`)
|
|
}))
|
|
t.Cleanup(up.Close)
|
|
if err := provider.Save(provider.Provider{ID: name, Name: name, Key: "k", Models: []string{"claude-opus-5-5"}, Anthropic: up.URL + "/v1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return up
|
|
}
|
|
serve("first")
|
|
serve("second")
|
|
if err := settings.Save(settings.Settings{Redact: true, RedactPersonal: true}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := New()
|
|
const secret = "Zr5routeKeep42q"
|
|
system := `"system":"You are Claude. Staging deploys with ` + secret + ` for now."`
|
|
send := func(to, msgs string) {
|
|
t.Helper()
|
|
if code, raw := postJSON(t, s, "/v1/messages", `{"model":"`+to+`/claude-opus-5-5","max_tokens":64,`+system+`,"messages":[`+msgs+`]}`); code != 200 {
|
|
t.Fatalf("%s: %d %s", to, code, raw)
|
|
}
|
|
}
|
|
send("first", `{"role":"user","content":"hi"}`)
|
|
send("first", `{"role":"user","content":"hi"},{"role":"assistant","content":"ok"},{"role":"user","content":"DB_PASSWORD=`+secret+`"}`)
|
|
send("second", `{"role":"user","content":"hi"}`)
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if len(got["first"]) != 2 || len(got["second"]) != 1 {
|
|
t.Fatalf("upstream got %d and %d", len(got["first"]), len(got["second"]))
|
|
}
|
|
if !strings.Contains(got["first"][1], "deploys with "+secret) {
|
|
t.Errorf("the first provider's system prompt, sent with the value as written, was rewritten: %s", got["first"][1])
|
|
}
|
|
if strings.Contains(got["second"][0], secret) {
|
|
t.Errorf("another provider, never sent it, got the value as written: %s", got["second"][0])
|
|
}
|
|
}
|