Files
magpie/internal/maskrec/maskrec.go
T
yetone 5637cca141 settings: a notification when masking swaps a value for the first time (Must2333 on X)
Must2333 on X: "我还希望能即时的知道发生了替换,但不是通过随时监控网关的方式".
They want to know when magpie masks something without watching the
gateway or the 脱敏 page.

Settings › Usage has a new row, "Masking alert" (zh 脱敏提醒), after the
task done alert. Off/On, off by default. When on, a value masked for the
first time gives a system notification through the same notifier as the
other alerts. It says only how many new values, each kind and its count,
and the agents that sent them (two named, "+N" for more). It never says
the value, any piece of it, or the Head hint the 脱敏 page keeps. A click
opens the 脱敏 page on the latest record (clickView, "masking&key=<key>").

Why off by default: every other notification in this group is off until
turned on; turning one on asks macOS for notification permission; for
most users a notice each time masking works is noise, since masking's
expected behaviour is quiet protection. It is one row, in the group that
already holds the alerts, hidden while masking is switched off
(data-feature="redact") and in gateway mode.

Staying quiet:
- First time only: maskrec.Fresh says which values a record has that no
  record before it had. A conversation sends its whole history each turn,
  so a value is masked again every turn after its first. The index is by
  placeholder (HMAC-stable across restarts), filled from memory and from
  the days kept on disk, so a restart doesn't make a history new again.
  The days are read off the lock, since Hold takes it on the request's
  way. Values sent as written (Action "warn") are left out.
- A burst is one notification: records gather for 3s (maskGather).
- At most one every minute (maskGap); what comes in between is said
  together after it. At most 5000 records wait.
- With the setting off, nothing is kept.

The gateway tells the app through gateway.OnMasked, after maskrec.Hold,
with Snips and Back taken out; nil under `magpie serve`.

Strings: en, zh, zh-TW, ja, de, in i18n.js (the row) and in
mask_alert.go (the notification's title and kind names, the 脱敏 page's
own words for the kinds). docs/subsystems/gateway-routing.md has the
bullet. The core script budget isn't raised: main's 10-29 budget leaves
room for the row's 287 bytes.

Tests, each broken by hand and seen red:
- TestMaskAlertFirstTimeOnly: masks through redact.Mask as the gateway
  does; the history masked again says nothing, a new email in turn 4 is
  said alone. Red when flush counts r.Values instead of the fresh ones.
- TestMaskAlertNeverSaysTheValue: no 4-character piece of the key, email
  or phone, and no Head, in any of the 5 languages. Red with the Head
  added to the kind's name.
- TestMaskAlertCoalesces: a burst of 5 from 3 agents is one wait of
  maskGather and one notification ("+1"); two more 10s later wait
  maskGap-10s and are said as one; off keeps nothing. Red without the
  waiting check (5 waits) and without the maskGap wait ("waits 3s want
  50s").
- TestFreshOnlyTheFirstTime, TestFreshHeldWhileReading (maskrec). Red
  with loadFirst skipping the disk read (the history new after a
  restart), and with the index broken (both).
- TestDismissedAlertOpensNothing covers the mask notice's click.
- mask-alert.test.cjs: every language at 900 and 380px, both engines:
  the row after the task done alert, Off at first, On saves maskAlert,
  another save keeps it, Off saves false, no click moves the page, no
  left border, gone with masking switched off. Red with maskAlert taken
  out of prefsKeep, and with data-feature taken off the row.

Run:
- This change on main 2b6ff0fbf (its parent here, c93d9e0c0, changes
  only usage's Codex effort cache after it), on mote: go test ./internal/gui
  ./internal/maskrec ./internal/settings ./internal/gateway; GUI files
  mask-alert, turn-done, gateway-mode, settings-search, i18n-keys (68
  pass, 0 fail); gofmt -l; go vet for darwin, GOOS=windows, and
  GOOS=linux -tags nogui, all ok. internal/gui's one red is
  TestMainWindowCorners/macOS_26_SDK, #3949, red on main too.
- At the same change on main ddf31ca30, 23 commits back: the full Go
  suite on mote, -timeout 45m. Every package ok but internal/gui: the
  budget (since fixed by the rebase), TestMainWindowCorners (#3949), and
  nine real-window AppKit tests killed at their deadlines while mote's
  pboard was down (#4125). Run again once pboard was back, all nine
  passed.
- The whole GUI suite, both engines, every language, on the Windows box
  (mote's GUI run was cut short by the pboard crash, #4125): 14235
  tests, 13951 pass, 284 fail in 65 files. The same files at ddf31ca30:
  291 fail. All 22 mask-alert tests pass there. The 8 files red only at
  this change were run again at both commits and each goes red and
  green on its own, mostly WebKit (#4124).
- The real app on mote, in a sandbox HOME, with a fake upstream: what
  the upstream got was placeholders only ({{API_KEY_…}}, {{EMAIL_…}},
  {{PHONE_…}}). Three turns in a minute became one notification 3s after
  the first, and a new email in a later turn the next one, exactly 60.0s
  after.

Not tried:
- The banner itself: macOS refused notification permission to the
  sandbox bundle (ad hoc signed and dev signed, "didGrant: 0 hasError:
  1"), and no one was at mote's screen to allow it. What was seen is the
  send, in the app's log.
- Windows and Linux notifications, and a click from a real banner.
- Values masked again after the 30 days the records are kept are said
  again. Blocked requests and warn-only values aren't notified. No
  session id is shown.
2026-10-29 06:38:04 +08:00

519 lines
15 KiB
Go

// Package maskrec keeps the record of what masking swapped for
// placeholders before a request went to a vendor, for Settings → Privacy
// to show: which kinds, found by which finder, going to whom.
//
// A value itself is never written down. A record holds each value's
// placeholder, its kind, its finder, and a hint (its first character or a
// key's prefix, and its length). The value behind a placeholder is known
// only to the masker's memory while magpie runs (gwmw.Teller.Value), and
// the text around it as the vendor saw it is kept in memory only, for the
// latest records.
//
// A day is a file of its own, one request a line, under
// ~/.config/magpie/masking, kept keepDays days and keepBytes in all.
package maskrec
import (
"bufio"
"bytes"
"cmp"
"encoding/json"
"errors"
"os"
"path/filepath"
"slices"
"sort"
"strings"
"sync"
"time"
"github.com/klauspost/compress/zstd"
"github.com/yetone/magpie/internal/appdir"
)
const (
keepDays = 30
keepBytes = 100 << 20
// keepSnips is how many of the latest records memory keeps, with the
// text around their values
keepSnips = 400
// keepSent is how many of the latest requests memory keeps whole, as
// the vendor got them, up to sentMax each: the 脱敏 page shows a
// request's text from it, its values put back from the masker's
// memory, and sentBudget in all, as stored: zstd, since a request is
// JSON that repeats itself and a busy gateway held 64 MB of them
// (#2278). A screenshot's base64 hardly compresses, so the budget is
// half what it was whole.
keepSent = 100
sentMax = 4 << 20
sentBudget = 32 << 20
dayForm = "2006-01-02"
)
// SentMax is the longest request a record keeps as sent; a longer one is
// kept as nothing, so a caller need not copy it.
const SentMax = sentMax
// Value is one value a request had masked, as often as it had it.
type Value struct {
Placeholder string `json:"p"`
Kind string `json:"k"`
By string `json:"b"` // the finder: rule, yours, word, model or known
Head string `json:"h,omitempty"` // its first character, or a key's prefix
Len int `json:"l"` // its length in characters
N int `json:"n"` // the places the request had it
// Action: "" masked, "warn" sent as written (the user asked to be
// told only), "block" masked and the request held back
Action string `json:"a,omitempty"`
}
// Record is one request's masking.
type Record struct {
Key int64 `json:"key"` // unique, later records larger
Time time.Time `json:"t"`
Route int64 `json:"route,omitempty"` // its route in the trace, where it has one
What string `json:"what"` // chat, search, image, video, embeddings, audio, count
Agent string `json:"agent,omitempty"`
Session string `json:"session,omitempty"`
Provider string `json:"provider,omitempty"`
Model string `json:"model,omitempty"`
N int `json:"n"` // values swapped, each place counted
Values []Value `json:"values,omitempty"`
// Blocked: the request was held back, sent to no one, for a value
// of a kind the user set to block
Blocked bool `json:"blocked,omitempty"`
// Snips: by placeholder, the text around its first place as the
// vendor saw it; in memory only
Snips map[string]string `json:"snips,omitempty"`
// Took is how long masking the request took, in microseconds
Took int64 `json:"took,omitempty"`
// Back: by placeholder, the places the answer had it put back; in
// memory only, told once the answer is done (Restored)
Back map[string]int `json:"back,omitempty"`
// Sent is the request as the vendor got it; in memory only, for the
// latest keepSent, and never in a list (SentOf)
Sent string `json:"-"`
}
// Dir is where the days are kept.
var Dir = func() string { return filepath.Join(appdir.Config(), "masking") }
// clock is what day it is; a variable for the tests.
var clock = time.Now
var store struct {
mu sync.Mutex
last int64
pruned time.Time
days map[string]*dayAgg // a day's file → what it holds, as of its size
snips []Record // the latest records, with their snips
sent []kept // the latest records' Sent, oldest first
// sentBytes is their bodies in all, as stored
sentBytes int
// first is each placeholder by the key of the first record that had
// it, and firstRead whether the days on disk are in it (first.go)
first map[string]int64
firstRead bool
}
// kept is a record's Sent, zstd.
type kept struct {
key int64
body []byte
}
var (
sentEnc = sync.OnceValue(func() *zstd.Encoder {
e, _ := zstd.NewWriter(nil, zstd.WithEncoderLevel(zstd.SpeedFastest), zstd.WithEncoderCRC(false),
zstd.WithWindowSize(1<<20), zstd.WithEncoderConcurrency(2), zstd.WithLowerEncoderMem(true))
return e
})
sentDec = sync.OnceValue(func() *zstd.Decoder {
d, _ := zstd.NewReader(nil, zstd.WithDecoderConcurrency(1), zstd.WithDecoderLowmem(true),
zstd.WithDecoderMaxMemory(sentMax))
return d
})
)
// inTest says Add writes nothing: in a test binary testenv isolated, a
// write still going as a test ends would put masking/ back into its
// temporary HOME (see the gateway's keepsRoutes).
func inTest() bool { return os.Getenv("MAGPIE_TEST_SANDBOX") != "" && !ownTest }
// ownTest is set by this package's tests, which read what they write.
var ownTest = false
// Add records r, its Values made from hits as they came: one for each
// placeholder, counted. It returns r as recorded. Errors are swallowed:
// keeping the record must never break a request.
func Add(r Record) Record {
r, done := Hold(r)
done("", "")
return r
}
// Hold is Add for a request whose provider isn't known yet: a group's
// member that fails passes it to the next, so the one it is planned for
// may never get it (Max You and Linka on Discord: the records named the
// group's first member, which no request had reached). r is in memory at
// once, for its route's badge and the answer's Restored, and goes on disk,
// where the summary counts it, once done is told the provider and model
// that had it last ("" keeps r's). Only done's first call counts.
func Hold(r Record) (Record, func(provider, model string)) {
if len(r.Values) == 0 && r.N == 0 {
return r, func(string, string) {}
}
r.Values = merged(r.Values)
if r.Time.IsZero() {
r.Time = clock()
}
var body []byte
if r.Sent != "" && len(r.Sent) <= sentMax {
// the encoder may make room for all of Sent; keep what it wrote
body = bytes.Clone(sentEnc().EncodeAll([]byte(r.Sent), nil))
}
r.Sent = ""
store.mu.Lock()
defer store.mu.Unlock()
r.Key = r.Time.UnixNano()
if r.Key <= store.last {
r.Key = store.last + 1
}
store.last = r.Key
if body != nil {
store.sent = append(store.sent, kept{r.Key, body})
store.sentBytes += len(body)
}
for len(store.sent) > keepSent || store.sentBytes > sentBudget {
store.sentBytes -= len(store.sent[0].body)
store.sent[0] = kept{}
store.sent = store.sent[1:]
}
store.snips = append(store.snips, r)
if store.first != nil {
noteFirst(store.first, r)
}
if len(store.snips) > keepSnips {
store.snips = append([]Record(nil), store.snips[len(store.snips)-keepSnips:]...)
}
var once sync.Once
return r, func(provider, model string) { once.Do(func() { settle(r, provider, model) }) }
}
// settle gives r the provider and model that had it, in memory, and
// writes it to its day.
func settle(r Record, provider, model string) {
r.Provider, r.Model = cmp.Or(provider, r.Provider), cmp.Or(model, r.Model)
store.mu.Lock()
defer store.mu.Unlock()
if i := at(r.Key); i >= 0 {
store.snips[i].Provider, store.snips[i].Model = r.Provider, r.Model
}
if inTest() {
return
}
disk := r
disk.Snips, disk.Back = nil, nil
b, err := json.Marshal(disk)
if err != nil {
return
}
dir := Dir()
if os.MkdirAll(dir, 0o700) != nil {
return
}
path := filepath.Join(dir, r.Time.Local().Format(dayForm)+".jsonl")
f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o600)
if err != nil {
return
}
before, _ := f.Seek(0, 2)
_, err = f.Write(append(b, '\n'))
f.Close()
// a day already read takes the record as it is written, rather than
// being read again
if a := store.days[path]; err == nil && a != nil && a.size == before {
a.add(disk)
a.size += int64(len(b) + 1)
}
if now := clock(); now.Sub(store.pruned) > time.Hour {
store.pruned = now
prune(dir, now)
}
}
// Clear deletes every record kept: each day's file, and what memory has
// of them (their snips, the requests' text). A day goes whole, never
// rewritten, so nothing is left half-cleared for a reader; a request still
// going is written to a new day's file once it is done. A day that can't
// be removed stays, and is named in the error. What a placeholder stood
// for is the masker's, in memory alone, and stays there for the answers
// still to come.
func Clear() error {
store.mu.Lock()
defer store.mu.Unlock()
var errs []error
for _, d := range files(Dir()) {
if err := os.Remove(d.path); err != nil && !errors.Is(err, os.ErrNotExist) {
errs = append(errs, err)
continue
}
delete(store.days, d.path)
}
store.snips, store.sent, store.sentBytes = nil, nil, 0
return errors.Join(errs...)
}
// merged is vs with one Value for each placeholder, its N the sum, in the
// order they first came. A value found by a finder and later only known
// (masked before) keeps the finder that found it.
func merged(vs []Value) []Value {
at := map[string]int{}
var out []Value
for _, v := range vs {
if v.N == 0 {
v.N = 1
}
if i, ok := at[v.Placeholder]; ok {
out[i].N += v.N
if out[i].By == "known" && v.By != "known" {
out[i].By = v.By
}
if v.Action == "block" {
out[i].Action = v.Action
}
continue
}
at[v.Placeholder] = len(out)
out = append(out, v)
}
return out
}
type dayFile struct{ day, path string }
// files are the days kept, oldest first.
func files(dir string) []dayFile {
es, _ := os.ReadDir(dir)
var out []dayFile
for _, e := range es {
n := e.Name()
if day, ok := strings.CutSuffix(n, ".jsonl"); ok {
if _, err := time.Parse(dayForm, day); err == nil {
out = append(out, dayFile{day, filepath.Join(dir, n)})
}
}
}
sort.Slice(out, func(i, j int) bool { return out[i].day < out[j].day })
return out
}
// prune drops the days older than keepDays, then the oldest until what is
// left fits keepBytes.
func prune(dir string, now time.Time) {
oldest := now.Local().AddDate(0, 0, -keepDays).Format(dayForm)
fs := files(dir)
var total int64
sizes := make([]int64, len(fs))
for i, d := range fs {
if fi, err := os.Stat(d.path); err == nil {
sizes[i] = fi.Size()
total += sizes[i]
}
}
for i, d := range fs {
if d.day >= oldest && total <= keepBytes {
break
}
if os.Remove(d.path) == nil {
total -= sizes[i]
delete(store.days, d.path)
}
}
}
// read calls each with the records of path, in the order written; a line
// that doesn't parse, one half-written by another magpie, is skipped.
func read(path string, each func(Record)) int64 {
f, err := os.Open(path)
if err != nil {
return 0
}
defer f.Close()
var size int64
sc := bufio.NewScanner(f)
sc.Buffer(make([]byte, 64<<10), 16<<20)
for sc.Scan() {
line := sc.Bytes()
size += int64(len(line) + 1)
if len(bytes.TrimSpace(line)) == 0 {
continue
}
var r Record
if json.Unmarshal(line, &r) == nil {
each(r)
}
}
return size
}
// at is where memory has the record of key, -1 where it hasn't.
func at(key int64) int {
i := sort.Search(len(store.snips), func(i int) bool { return store.snips[i].Key >= key })
if i < len(store.snips) && store.snips[i].Key == key {
return i
}
return -1
}
// withSnips is r with what memory has for it: its snips and what the
// answer put back.
func withSnips(r Record) Record {
if i := at(r.Key); i >= 0 {
r.Snips, r.Back = store.snips[i].Snips, store.snips[i].Back
}
return r
}
// Restored tells the record of key what its answer put back: by
// placeholder, the places. Memory only, as the snips are.
func Restored(key int64, put map[string]int) {
if key == 0 || len(put) == 0 {
return
}
store.mu.Lock()
defer store.mu.Unlock()
if i := at(key); i >= 0 {
back := map[string]int{}
for p, n := range store.snips[i].Back {
back[p] = n
}
for p, n := range put {
back[p] += n
}
store.snips[i].Back = back
}
}
// SentOf is the request of key as the vendor got it, while memory keeps
// it.
func SentOf(key int64) (string, bool) {
var body []byte
store.mu.Lock()
for _, k := range store.sent {
if k.key == key {
body = k.body
break
}
}
store.mu.Unlock()
if body == nil {
return "", false
}
b, err := sentDec().DecodeAll(body, nil)
if err != nil {
return "", false
}
return string(b), true
}
// Filter narrows List: by kind (one, or several split by commas), by
// finder, by agent or provider, "" is any; Blocked keeps only the requests
// masking held back.
type Filter struct {
Kind, By, Agent, Provider string
Blocked bool
}
func (f Filter) takes(r Record) bool {
if f.Agent != "" && r.Agent != f.Agent || f.Provider != "" && r.Provider != f.Provider || f.Blocked && !r.Blocked {
return false
}
if f.Kind == "" && f.By == "" {
return true
}
kinds := strings.Split(f.Kind, ",")
for _, v := range r.Values {
if (f.Kind == "" || slices.Contains(kinds, v.Kind)) && (f.By == "" || v.By == f.By) {
return true
}
}
return false
}
// List is up to limit records before key (0: the latest), newest first,
// that f takes, and whether there are more.
func List(before int64, limit int, f Filter) ([]Record, bool) {
store.mu.Lock()
defer store.mu.Unlock()
if limit <= 0 {
limit = 50
}
var out []Record
fs := files(Dir())
for i := len(fs) - 1; i >= 0; i-- {
if before > 0 && time.Unix(0, before).Local().Format(dayForm) < fs[i].day {
continue
}
var day []Record
read(fs[i].path, func(r Record) {
if (before == 0 || r.Key < before) && f.takes(r) {
day = append(day, r)
}
})
// a record goes on disk once its request is done (Hold), so a
// long one comes after later ones: the list is in key order, as
// the next page's before needs
slices.SortStableFunc(day, func(a, b Record) int { return cmp.Compare(a.Key, b.Key) })
for j := len(day) - 1; j >= 0; j-- {
if len(out) == limit {
return out, true
}
out = append(out, withSnips(day[j]))
}
}
if inTest() {
// nothing on disk: the records in memory
for j := len(store.snips) - 1; j >= 0; j-- {
r := store.snips[j]
if (before == 0 || r.Key < before) && f.takes(r) {
if len(out) == limit {
return out, true
}
out = append(out, r)
}
}
}
return out, false
}
// Get is the record of key, with its snips while memory has them.
func Get(key int64) (Record, bool) {
store.mu.Lock()
defer store.mu.Unlock()
if i := at(key); i >= 0 {
return store.snips[i], true
}
day := time.Unix(0, key).Local().Format(dayForm)
var got Record
ok := false
read(filepath.Join(Dir(), day+".jsonl"), func(r Record) {
if r.Key == key {
got, ok = r, true
}
})
return got, ok
}
// OfRoute is the record of the trace's route id, while memory has it.
func OfRoute(id int64) (Record, bool) {
store.mu.Lock()
defer store.mu.Unlock()
for j := len(store.snips) - 1; j >= 0; j-- {
if store.snips[j].Route == id {
return store.snips[j], true
}
}
return Record{}, false
}