Files
magpie/internal/redact/allowkept.go
T
yetone 327fb57659 redact: Never mask holds from the next text on, earlier turns stay masked (NicoleS76448271 on X)
A value let go from the request view's menu (Never mask) went as written
in the conversation's earlier turns too, so the prompt the vendor had
cached no longer matched and the whole conversation was read again at
full price. Now a text sent masked on a route keeps that value masked,
with the same placeholder, while magpie remembers the text; new texts
(the next turn, another conversation) send it as written. It holds for
values added in Settings too.

allowkept.go remembers a maphash of (route, text) and of the value,
never the value, in two generations of 2^17 like sentSpans' memo. Only a
request with a route (Options.sent and Route) keeps it; a search's query
or an embedding's input has the value as written at once. Keeping a
value masked is the safe side: a collision masks a value the user let
go, and a text no longer remembered (after a restart) sends it as
written, as before. The menu's message now says earlier messages stay
masked so the cache holds (en, zh, zh-TW, ja, de). The request view's
inline Never mask was already there (yew_fence on Discord).

Run (mote refused SSH for ~30 min at load ~125, so on the Linux box):
- wsl-run go test ./internal/redact/ ./internal/masking/ ./plugins/redact/
  ok, at the pushed head b9d1f00a5 too.
- wsl-run go test -run "Mask|Redact|Allow|Sent" ./internal/gateway/ and
  -tags nogui ./internal/gui/: ok.
- go vet: GOOS=windows (redact, gui), GOOS=darwin (redact), linux -tags
  nogui (redact, gui): ok. gofmt -l clean.
- node --test mask-value-menu, i18n-keys, i18n-en-no-cjk (chromium and
  webkit, every language): ok. masking-records: the same reds as fresh
  origin/main b35867730 (24 there, 23 here), filed as #5830, commented.
Broken by hand: apply without the maskedBefore check makes
TestAllowKeepsSentTextMasked red (the earlier turn goes as written).
Not tried: a real Anthropic request reading the cache across the
change; macOS (mote down); the full suite.
2026-11-10 18:50:45 +08:00

92 lines
2.7 KiB
Go

package redact
import (
"hash/maphash"
"strings"
"sync"
)
// Never mask (Options.Allow) holds from the next text on, not for a text
// already sent masked (NicoleS76448271 on X). A conversation sends its
// earlier turns again with each request, and a vendor caches the prompt
// by its bytes: had a value added to Never mask gone as written in those
// turns too, every turn before it would no longer match what the vendor
// cached, and the whole conversation would be read again at full price.
// So a value masked in a text sent on a route is masked there again,
// with the same placeholder, while this magpie remembers that text; a
// new text (the next turn, another conversation) sends it as written.
//
// Keeping a value masked is the safe side of the user's two asks, so a
// hash collision or a text no longer remembered errs as it should: the
// first masks a value the user let go, the second sends it as written
// (as before this memo, and after a restart).
//
// What is remembered is a hash of the route, the text and the value,
// never the value. Only a request for a vendor with a route (Options.sent
// and Route, as sentSpans) counts: a search's query or an embedding's
// input is nothing a vendor caches.
const allowKeptEntries = 1 << 17
type keptKey struct{ text, value uint64 }
var allowKept struct {
sync.Mutex
cur, old map[keptKey]struct{}
}
var keptSeed = maphash.MakeSeed()
// keptText is the hash a text s sent on route is remembered by.
func keptText(route, s string) uint64 {
var h maphash.Hash
h.SetSeed(keptSeed)
h.WriteString(route)
h.WriteByte(0)
h.WriteString(s)
return h.Sum64()
}
func keptValue(v string) uint64 {
return maphash.String(keptSeed, strings.ToLower(strings.TrimSpace(v)))
}
// maskedBefore says v was masked in the text text hashes to, and keeps
// that remembered past the next turnover.
func maskedBefore(text uint64, v string) bool {
k := keptKey{text, keptValue(v)}
allowKept.Lock()
defer allowKept.Unlock()
if _, ok := allowKept.cur[k]; ok {
return true
}
if _, ok := allowKept.old[k]; ok {
keptAdd(k)
return true
}
return false
}
// keepMasked remembers v was masked in the text text hashes to.
func keepMasked(text uint64, v string) {
k := keptKey{text, keptValue(v)}
allowKept.Lock()
defer allowKept.Unlock()
if _, ok := allowKept.cur[k]; !ok {
keptAdd(k)
}
}
// keptAdd is called with allowKept held: two generations, as sentAdd.
func keptAdd(k keptKey) {
if allowKept.cur == nil || len(allowKept.cur) >= allowKeptEntries {
allowKept.old, allowKept.cur = allowKept.cur, make(map[keptKey]struct{})
}
allowKept.cur[k] = struct{}{}
}
func resetKept() {
allowKept.Lock()
defer allowKept.Unlock()
allowKept.cur, allowKept.old = nil, nil
}