Files
magpie/internal/maskrec/first.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

83 lines
2.4 KiB
Go

package maskrec
import "sync"
// Which values a record has for the first time, for the notification that
// a value was masked (Must2333 on X): told at once, and only once. A
// conversation's history is sent again in full every turn, so a value
// masked in its first turn is masked again in every turn after; only the
// record that masked it first is news.
//
// first is each placeholder by the key of the first record that had it.
// A placeholder is the same for the same value across restarts (redact's
// key is kept on disk), so the index is read from the days kept, the last
// keepDays, as well as from memory: a restart doesn't make the history of
// a conversation new again. Clear leaves it as it is: the user who
// cleared the records was told of those values already.
var firstLoad sync.Mutex // one reading of the days at a time
// noteFirst puts r's values in the index; called with store.mu held.
func noteFirst(m map[string]int64, r Record) {
for _, v := range r.Values {
if k, ok := m[v.Placeholder]; !ok || r.Key < k {
m[v.Placeholder] = r.Key
}
}
}
// loadFirst makes the index: what memory has at once, under the lock, then
// the days on disk read without it, since they can be many megabytes and
// Hold, on the request's way, takes the same lock. A record held while
// the days are read is put in the index by Hold, as store.first is there.
func loadFirst() {
store.mu.Lock()
store.first = map[string]int64{}
for _, r := range store.snips {
noteFirst(store.first, r)
}
dir := Dir()
store.mu.Unlock()
disk := map[string]int64{}
if !inTest() {
for _, d := range files(dir) {
read(d.path, func(r Record) { noteFirst(disk, r) })
}
}
store.mu.Lock()
for p, k := range disk {
if cur, ok := store.first[p]; !ok || k < cur {
store.first[p] = k
}
}
store.firstRead = true
store.mu.Unlock()
}
// Fresh are the values r masked that no record before it had: masked for
// the first time. A value sent as written (Action "warn") is not one
// masked. r is a record as Hold returned it, its Key set.
func Fresh(r Record) []Value {
firstLoad.Lock()
store.mu.Lock()
read := store.firstRead
store.mu.Unlock()
if !read {
loadFirst()
}
firstLoad.Unlock()
store.mu.Lock()
defer store.mu.Unlock()
var out []Value
for _, v := range r.Values {
if v.Action == "warn" {
continue
}
if k, ok := store.first[v.Placeholder]; ok && k < r.Key {
continue
}
out = append(out, v)
}
return out
}