With Privacy masking on, a value masked once is "known" and masked
wherever a later request has it (redact/known.go). When a rule first
finds a value in a later turn (a tool printing DB_PASSWORD=...), that
value was masked from then on in the system prompt and the turns already
sent with it as written. The prefix the vendor had cached changed, so
the whole prompt was written to the cache again and none of it was read.
Reported by audie on Discord: DeepSeek Harness on a routing group served
by a custom Anthropic-compatible provider. Adjacent turns 10s apart had
cache_read 0 and cache_write 412K to 427K.
Fix: Mask and MaskJSON (the vendor-bound paths, via masking.Masker) now
use sentKnownSpans. A text masked before gets the known values it had
the first time, keyed by a sha256 of the text and the options. Only
hashes and spans are kept, never a value, in two generations of 65536.
New text still gets every known value masked. Scrub (archives, sessions,
OTel; local only) keeps the latest known set.
Unchanged: if magpie restarts, or a text falls out of both generations,
the text is masked under the current known set once, as before.
What was run:
- internal/redact TestSentTextKeepsItsMasking and internal/gateway
TestMaskedConversationKeepsItsCachedPrefix. The gateway test sends a
dsh-shaped Chat Completions conversation to a group with an Anthropic
member, and a Claude Code-shaped /v1/messages one. It checks that each
turn's system, tools and earlier messages reach the fake upstream byte
for byte. Both tests are red with sentKnownSpans forced back to
knownSpans: the system prompt became "{{SECRET_...}}" on turn 3. Both
are green with the fix.
- With masking off, a probe of the same dsh shape had the system, tools
and earlier turns byte-identical on every turn.
- gofmt -l: clean. go vet: darwin, GOOS=linux -tags nogui ./...,
GOOS=windows ./...: clean.
- Full suite at 8187919d7 (this tree, on origin/main bf2e067ef):
set -o pipefail; go test -tags nogui -timeout 45m ./..., through the
suite lock. 69 packages ok, none red (gateway 519s, agent 393s).
Not tried: audie's real setup. Their Privacy setting isn't known; with
masking off this is not the cause, and every breakpoint reading 0
(even the unchanged system block) points at the provider's own cache or
pool. Not tried against a real Anthropic or relay account. GUI not
touched, so the GUI suite was not run. Windows tests not run.
58 lines
2.4 KiB
Go
58 lines
2.4 KiB
Go
package redact
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// A text sent before goes as it went, though a value in it became known
|
|
// since: a vendor caches a conversation by its bytes, and masking the
|
|
// value now in a system prompt already sent with it rewrote the whole
|
|
// prompt to the cache on the next turn (audie on Discord: 400K tokens
|
|
// written, none read, ten seconds after the turn before). New text has
|
|
// the value masked as before.
|
|
func TestSentTextKeepsItsMasking(t *testing.T) {
|
|
resetSent()
|
|
t.Cleanup(resetSent)
|
|
o := Options{Secrets: true, Personal: true}
|
|
const secret = "Rv9sentPass27x"
|
|
system := "You are dsh. Deploy notes: the staging pass is " + secret + " for now."
|
|
first, n := Mask(system, o)
|
|
if first != system || n != 0 {
|
|
t.Fatalf("no rule finds it here, and nothing is known: %q", first)
|
|
}
|
|
body := `{"messages":[{"role":"system","content":"` + system + `"},{"role":"user","content":"go"}]}`
|
|
firstBody, _ := MaskJSON([]byte(body), o)
|
|
|
|
// a tool's output in the next turn has it where a rule finds it
|
|
if out, _ := Mask("DB_PASSWORD="+secret, o); strings.Contains(out, secret) {
|
|
t.Fatalf("the rule should mask it: %q", out)
|
|
}
|
|
|
|
// the system prompt and the body sent before go as they went
|
|
if again, _ := Mask(system, o); again != first {
|
|
t.Fatalf("a text sent before was masked again:\n%q\nwas\n%q", again, first)
|
|
}
|
|
if again, _ := MaskJSON([]byte(body), o); string(again) != string(firstBody) {
|
|
t.Fatalf("a body sent before was masked again:\n%s\nwas\n%s", again, firstBody)
|
|
}
|
|
// with more after it, the earlier text is the same bytes
|
|
longer := `{"messages":[{"role":"system","content":"` + system + `"},{"role":"user","content":"go"},{"role":"assistant","content":"the pass is ` + secret + `"}]}`
|
|
got, _ := MaskJSON([]byte(longer), o)
|
|
if !strings.HasPrefix(string(got), string(firstBody[:strings.Index(string(firstBody), `,{"role":"user"`)])) {
|
|
t.Fatalf("the prefix changed: %s", got)
|
|
}
|
|
// the new text has the value as its placeholder
|
|
if strings.Count(string(got), secret) != 1 {
|
|
t.Fatalf("the new turn has the value as written: %s", got)
|
|
}
|
|
if out, _ := Mask("now the pass is "+secret+" again", o); strings.Contains(out, secret) {
|
|
t.Fatalf("a known value went out in new text: %q", out)
|
|
}
|
|
|
|
// what is covered changing is another text: masked with what is known
|
|
if out, _ := Mask(system, Options{Secrets: true}); strings.Contains(out, secret) {
|
|
t.Fatalf("other settings, masked as known: %q", out)
|
|
}
|
|
}
|