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.
179 lines
6.2 KiB
Go
179 lines
6.2 KiB
Go
package gui
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/maskrec"
|
|
"github.com/yetone/magpie/internal/redact"
|
|
)
|
|
|
|
// fakeMaskAlerts is maskAlerts on a clock the test moves, its flush run
|
|
// when the test says, what it said kept.
|
|
type fakeMaskAlerts struct {
|
|
*maskAlerts
|
|
clock time.Time
|
|
waits []time.Duration
|
|
flushF func()
|
|
said []maskNote
|
|
on bool
|
|
}
|
|
|
|
func newFakeMaskAlerts(t *testing.T) *fakeMaskAlerts {
|
|
t.Helper()
|
|
f := &fakeMaskAlerts{clock: time.Date(2026, 10, 29, 10, 0, 0, 0, time.Local), on: true}
|
|
f.maskAlerts = newMaskAlerts(func() bool { return f.on }, func(n maskNote) { f.said = append(f.said, n) })
|
|
f.now = func() time.Time { return f.clock }
|
|
f.after = func(d time.Duration, fn func()) { f.waits = append(f.waits, d); f.flushF = fn }
|
|
return f
|
|
}
|
|
|
|
// fire runs the flush set, as its wait is over.
|
|
func (f *fakeMaskAlerts) fire(t *testing.T) {
|
|
t.Helper()
|
|
if f.flushF == nil {
|
|
t.Fatal("no notification was set to be said")
|
|
}
|
|
fn := f.flushF
|
|
f.flushF = nil
|
|
f.clock = f.clock.Add(f.waits[len(f.waits)-1])
|
|
fn()
|
|
}
|
|
|
|
// mask masks text as the gateway does, and records it as noteMasked does,
|
|
// for agent: the record OnMasked is told.
|
|
func mask(t *testing.T, text, agent string) maskrec.Record {
|
|
t.Helper()
|
|
var vals []maskrec.Value
|
|
o := redact.Options{Secrets: true, Personal: true, Seen: func(h redact.Hit) {
|
|
vals = append(vals, maskrec.Value{Placeholder: h.Placeholder, Kind: h.Kind, By: h.By, Head: h.Head, Len: h.Len, Action: h.Action})
|
|
}}
|
|
out, n := redact.Mask(text, o)
|
|
if n == 0 {
|
|
t.Fatalf("nothing masked in %q", text)
|
|
}
|
|
if out == text {
|
|
t.Fatalf("text went as written: %q", out)
|
|
}
|
|
return maskrec.Add(maskrec.Record{What: "chat", Agent: agent, N: n, Values: vals})
|
|
}
|
|
|
|
// A conversation sends its history again every turn, so a value masked in
|
|
// its first turn is masked in each turn after. Only the first is said;
|
|
// a value new in a later turn is said on its own.
|
|
func TestMaskAlertFirstTimeOnly(t *testing.T) {
|
|
f := newFakeMaskAlerts(t)
|
|
key := "sk-ant-api03-" + strings.Repeat("Q7vR2mXk", 5)
|
|
turn1 := "deploy with ANTHROPIC_API_KEY=" + key
|
|
f.add(mask(t, turn1, "claude"))
|
|
f.fire(t)
|
|
if len(f.said) != 1 || f.said[0].Kinds["API_KEY"] != 1 || f.said[0].total() != 1 {
|
|
t.Fatalf("turn 1 said %+v, want one API key", f.said)
|
|
}
|
|
// turns 2 and 3: the history again, the key with it, nothing new
|
|
f.clock = f.clock.Add(5 * time.Minute)
|
|
f.add(mask(t, turn1+"\nok, done. what next?", "claude"))
|
|
f.add(mask(t, turn1+"\nok, done. what next?\nrun the tests", "claude"))
|
|
f.fire(t)
|
|
if len(f.said) != 1 {
|
|
t.Fatalf("the history masked again was said again: %+v", f.said[1:])
|
|
}
|
|
// turn 4 brings an email the conversation never had
|
|
f.clock = f.clock.Add(5 * time.Minute)
|
|
f.add(mask(t, turn1+"\nmail the report to wang.lei@example-corp.cn", "claude"))
|
|
f.fire(t)
|
|
if len(f.said) != 2 || f.said[1].Kinds["EMAIL"] != 1 || f.said[1].total() != 1 {
|
|
t.Fatalf("turn 4 said %+v, want the email alone", f.said[1:])
|
|
}
|
|
}
|
|
|
|
// What is said is the kind, how many and the agent: never the value, no
|
|
// piece of it, nor the hint the 脱敏 page keeps of it (a key's prefix).
|
|
func TestMaskAlertNeverSaysTheValue(t *testing.T) {
|
|
f := newFakeMaskAlerts(t)
|
|
key := "sk-proj-" + strings.Repeat("Zx9Lm3Pq", 5)
|
|
mail, phone := "lfqzw.xu@xqjvb-yd.cn", "13987654321"
|
|
r := mask(t, "OPENAI_API_KEY="+key+"\ncontact "+mail+" or "+phone, "codex")
|
|
for _, v := range r.Values {
|
|
if v.Head == "" {
|
|
t.Fatalf("the record has no hint to leak, the test proves nothing: %+v", r.Values)
|
|
}
|
|
}
|
|
f.add(r)
|
|
f.fire(t)
|
|
if len(f.said) != 1 || f.said[0].total() != 3 {
|
|
t.Fatalf("said %+v, want the key, the email and the phone", f.said)
|
|
}
|
|
for _, lang := range []string{"en", "zh", "zh-TW", "ja", "de"} {
|
|
title, body := maskAlertText(lang, f.said[0], agentName)
|
|
text := title + "\n" + body
|
|
for _, v := range []string{key, mail, phone} {
|
|
for i := 0; i+4 <= len(v); i++ {
|
|
if strings.Contains(text, v[i:i+4]) {
|
|
t.Fatalf("%s says %q of %q: %s", lang, v[i:i+4], v, text)
|
|
}
|
|
}
|
|
}
|
|
for _, v := range r.Values {
|
|
if len(v.Head) > 1 && strings.Contains(text, v.Head) {
|
|
t.Fatalf("%s says the hint %q: %s", lang, v.Head, text)
|
|
}
|
|
}
|
|
if !strings.Contains(body, "Codex") || !strings.Contains(title, "3") {
|
|
t.Fatalf("%s doesn't say how many or who: %s", lang, text)
|
|
}
|
|
}
|
|
}
|
|
|
|
// An agent's tool calls come one after another, each a request with a new
|
|
// value: they are said once, together. What comes while the last one is
|
|
// fresh waits a minute from it, and is said then, together too.
|
|
func TestMaskAlertCoalesces(t *testing.T) {
|
|
f := newFakeMaskAlerts(t)
|
|
texts := []string{
|
|
"GITHUB_TOKEN=ghp_" + strings.Repeat("aB3dE5fG7h", 4),
|
|
"db: postgres://app:Hunter2Secret9@db.internal:5432/app",
|
|
"write to zhao.min@mail-host.cn",
|
|
"call 13711112222 tomorrow",
|
|
"AWS key AKIAQWERTYUIOPASDFGH",
|
|
}
|
|
for i, s := range texts {
|
|
f.add(mask(t, s, []string{"claude", "codex", "opencode"}[i%3]))
|
|
}
|
|
if len(f.waits) != 1 || f.waits[0] != maskGather {
|
|
t.Fatalf("waits %v, want one of %v for the burst", f.waits, maskGather)
|
|
}
|
|
f.fire(t)
|
|
if len(f.said) != 1 || f.said[0].total() != len(texts) {
|
|
t.Fatalf("said %d notifications %+v, want one of %d values", len(f.said), f.said, len(texts))
|
|
}
|
|
if got := f.said[0].Agents; len(got) != 3 {
|
|
t.Fatalf("agents %v, want the three that sent them", got)
|
|
}
|
|
if _, body := maskAlertText("en", f.said[0], agentName); !strings.Contains(body, "+1") {
|
|
t.Fatalf("a third agent isn't counted: %s", body)
|
|
}
|
|
// ten seconds on, two more: said a minute after the last, as one
|
|
f.clock = f.clock.Add(10 * time.Second)
|
|
f.add(mask(t, "the admin is sun.li@corp-mail.cn", "claude"))
|
|
f.add(mask(t, "her phone is 15822223333", "claude"))
|
|
if w := f.waits[len(f.waits)-1]; w != maskGap-10*time.Second {
|
|
t.Fatalf("waits %v after one said 10s ago, want %v", w, maskGap-10*time.Second)
|
|
}
|
|
if len(f.waits) != 2 {
|
|
t.Fatalf("waits %v, want one more for both", f.waits)
|
|
}
|
|
f.fire(t)
|
|
if len(f.said) != 2 || f.said[1].total() != 2 {
|
|
t.Fatalf("said %+v, want the two together", f.said[1:])
|
|
}
|
|
// turned off, nothing is kept or said
|
|
f.on = false
|
|
f.clock = f.clock.Add(time.Hour)
|
|
f.add(mask(t, "and wu.qiang@corp-mail.cn", "claude"))
|
|
if f.flushF != nil || len(f.pending) != 0 {
|
|
t.Fatal("a record was kept with the alert off")
|
|
}
|
|
}
|