Commit Graph
9 Commits
Author SHA1 Message Date
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
yetone b904cc22bf maskrec: the latest requests' bodies are kept zstd in memory, in 32 MiB (#2278)
The 脱敏 page shows a recent request as the vendor got it, so memory
kept the latest 100 bodies whole, up to 64 MiB. apple-ouyang's heap
profile on #2278 had gateway.sentOf holding 64 MB of them, a sixth of
the live heap, kept for a page that is rarely open.

Each body is now compressed (zstd, fastest, outside the store's lock)
as Hold takes it, held apart from the record, and decoded only when
SentOf asks. The budget counts stored bytes and is halved to 32 MiB:
a request carrying a screenshot's base64 hardly compresses. On real
local session files zstd -1 makes Claude Code's text 4 to 5 times
smaller, and a Codex rollout with screenshots 1.4 times.

Records in memory no longer hold a body at all, so Get has nothing
to clear.

Run at this tree (rebased on origin/main 97b6a36c5), on mote:
- go vet -tags nogui for linux, windows, darwin: ok (maskrec, gateway)
- go test -race ./internal/maskrec: ok
- go test -race -run 'Mask|Sent|Redact|Privacy|Tail' ./internal/gateway: ok
Before the rebase (at 6104e88aa), vet of ./... on three OSes was ok,
and the whole race run of internal/gateway and internal/gui had three
reds not of this change: TestTailOfReadsInPlace (fixed on main since,
4ca64c21b), TestMainWindowCorners (#3949), and a race in
TestPluginPageStartsItsHost (fixed in the next commit).

Broken by hand: keeping as many bytes as the body (the encoded body
padded to its size) turns TestSentKeptCompressed red: "a 1048712-byte
request is kept in 1069709 bytes".

Not tried: the footprint of a live gateway under real masked traffic.

Reported-by: apple-ouyang
2026-10-28 22:29:10 +08:00
yetone f76e4d1f5c gateway: a chat body is copied for its masked record only when the record keeps it (#4004)
Every route that masks a body passed string(body) to noteMasked, which
copied the whole request though noteMasked keeps nothing when nothing
was masked, and only the first 4 MiB-or-less body otherwise
(maskrec's sentMax). A 44 MiB Codex turn with nothing to mask was
copied whole on every request for nothing.

sentOf copies only for a record that keeps it. maskrec.SentMax exports
the bound it checks.

TestMaskedBodyCopiedOnlyWhenKept: with nothing masked, and over
SentMax, nothing is allocated; under it the body is kept. Red with
sentOf returning string(body) always (2,097,152 bytes allocated for a
2 MiB body).

Run (mote-run at aa8ded04e, both commits, on 56602426d):
- gofmt -l clean; go vet ./internal/gateway ./internal/maskrec for
  linux, windows and darwin: ok.
- go test -count=1 -timeout 45m ./internal/gateway ./internal/maskrec:
  ok (gateway 533s).
- The new test red with the old code put back (above), green with it.
- The allocation and peak numbers: a scratch test of one 44 MiB
  Responses turn sent on as Chat (not committed), GOGC=10 gctrace for
  the peak.

Reported-by: jihuayu
2026-10-28 20:00:08 +08:00
yetone 630fd1d691 masking: Clear records… deletes what masking kept, after a confirm (maxyou2090)
maxyou2090 (#feedback) turned on strict masking and it recorded many
requests, with no way to delete them short of finding
~/.config/magpie/masking by hand.

Insights › Masking's list now ends in "Clear records…" (beside "Kept on
this computer for 30 days", shown while anything is kept). It asks first
with the app's confirm dialog (Cancel / Clear, red), then posts
/api/masking/clear and reads the page again: an empty list and card. A
clear that fails says why in the status pill and the list stays.

maskrec.Clear removes each day's file whole under the store's lock (a
day is removed, never rewritten, so there is no half-written file to
guard against), forgets each day's cached summary, and drops the text
around placeholders and the bodies kept in memory. A day it can't
remove stays, is still listed, and is named in the error. Keys keep
rising, so a record after the clear never takes an old key. What the
masker holds for each placeholder stays: a placeholder still in an
agent's context is put back in its answers. GET on the route does
nothing. Strings in zh, zh-TW, ja and de. gateway-routing.md says it.

Tests:
- internal/maskrec TestClear (three days and a stray file: the days go,
  the stray stays, List/Sum/Get/SentOf are empty, keys keep rising, a
  new Add counts) and TestClearSaysWhatStays (a directory it can't
  write: the error names 2026-10-26.jsonl and the record is still
  listed). With Clear stubbed to return nil both go red
  (maskrec_test.go:220 "left in ...", :260 "err <nil>").
- internal/gui TestMaskingRecordsAPI: GET clears nothing, POST clears,
  records/summary empty, record/sent 404. Red with Clear stubbed
  (masking_test.go:127).
- GUI masking-records.test.cjs: "Clear records asks first, then
  empties the page" in Chromium and WebKit, en/zh/zh-TW/ja/de, 1100 and
  640px (Cancel sends nothing; Clear posts once; nothing wider than the
  window; the click scrolls nothing; nothing left in English), and "a
  clear that fails says why and keeps the list". Red on the old page.js
  (the button never appears).

Run, at this commit (on origin/main 44a626bed; vet, maskrec, the gui Masking tests and the 22 new GUI cases again after that rebase):
- gofmt -l internal/maskrec internal/gui: clean. go vet on darwin,
  GOOS=linux -tags nogui and GOOS=windows for both packages: ok.
- go test ./internal/maskrec: ok. go test -run 'Masking|Budget|Script'
  ./internal/gui: ok.
- GUI: masking-records.test.cjs whole and i18n-keys.test.cjs, both
  engines: 86 pass, 0 fail (before the rebase onto a6c808b68, which
  touched no GUI file); the 22 new cases again after it: pass.
- Full suite, go test -tags nogui ./... -timeout 45m, at bdc25ec13: not
  a record. The machine's data volume was full (897 of 926 GiB, 3.2 GiB
  free afterwards), and backup, mcpauth, davsync, provider, cli, gateway
  and gui failed on "no space left on device" or failed to build.
  maskrec passed in it. The whole GUI suite was not run for the same
  reason.

Not tried: clearing in the real app with a real masking folder.
2026-10-26 18:07:19 +08:00
yetone 49f15d1244 masking: a record names the provider the request went to, not the group's first member (Max You and Linka on Discord)
Linka on Discord: Insights › Masking's records said WorkBuddy AI, the
first member of their auto group, for requests that Codex in the same
group had answered. Max You: providers no route sends to show up under
Privacy all the same.

Cause: serve() recorded the masking (noteMasked) as the route began,
with p.ID, the first planned provider, before any try. A request the
first member failed, or a member resting or unable to serve, went on
to the next, and the record (on disk, and counted into the flow and
the provider filter) kept the first. Embeddings recorded tries[0] and
video the same way.

Fix: maskrec.Hold keeps a record in memory at once (the route's badge
and the answer's Restored need its key) and writes it to disk only
when done is told the provider and model of the last try.
noteMaskedUntil wraps it, and serve, embeddings and video call done
in a defer with call.Provider and the model tried last. A request
that never reached a try keeps the planned one, as before. Since a
long request's line is now written after later ones, List sorts each
day's lines by key, so paging with before skips nothing.

Old records keep the name they were written with; there is no record
of which member had them. They leave with the 30 days.

Tests:
- TestMaskedRecordNamesTheProviderThatHadIt (gateway): a group a/m,
  b/m, a answers 500, b 200, with Mask secrets on; chat and
  embeddings. On the old code both subtests fail:
    masked_sent_test.go:96: recorded on "a", the request went to b
- TestHoldNamesWhoHadIt (maskrec): the held record goes on disk with
  done's provider and model, done's second call counts for nothing,
  Sum counts that provider, and List pages a late-written record in
  key order. With the sort taken out it fails on the first page.

Run at this tree (base a1e91e500), sandbox HOME, caches pinned:
- gofmt -l clean; go vet darwin, linux (nogui), windows on gateway
  and maskrec; builds darwin, linux nogui, windows.
- go test -tags nogui -count=1 -timeout 45m ./internal/maskrec/
  ./internal/gui/ ./internal/gateway/: ok, ok (75s), ok (523s).
- The full ./... suite was not run: other sessions held the machine's
  suite slot.

- Rebased onto ef0a03e13 (no change under internal/gateway or
  internal/maskrec since a1e91e500); builds on 3 OSes and the Mask/Hold
  tests of both packages ok again at the pushed head.

No GUI file changed. Not tried: a real group failover in the app.
2026-10-26 15:29:36 +08:00
yetone 51667a34c6 maskrec: records narrow by several kinds, by who found them, and to held-back requests
The Masking page's facets ask for a set of kinds (Secrets is API keys,
passwords, private keys...), for what found the values (rules or the
local model), and for the requests held back. Filter.Kind takes a comma
list, Filter.By and Filter.Blocked are new, and /api/masking/records
passes by= and blocked=1 through.

Run: go test ./internal/maskrec ./internal/gui -run 'Filter|Records|Masking|MainView|ShowMain' ok.
2026-10-24 19:41:03 +08:00
yetone 9b57261b62 masking: a record keeps its request as sent, how long masking took and what the answer put back
For the 观测 › 脱敏 page's request view. In memory only: the masked body
of the latest 100 requests (each up to 4MB, 64MB in all), and by
placeholder how many places of the answer had the value put back
(redact.Writer.Put, counted once per place even when a stream splits
the placeholder). Took (µs) is written to disk too; it holds nothing of
the request.

GET /api/masking/sent?key= gives the body, POST /api/masking/values the
values the masker still remembers, both no-store.

Run: go test ./internal/maskrec ./internal/redact ./internal/gui
-run TestMaskingRecordsAPI and ./internal/gateway -run TestMasked; ok.
Broken by hand: without toldBack, TestMaskedRecordKeepsSentAndBack is red.
2026-10-24 19:41:02 +08:00
yetone b00c82acfa masking: each kind is masked, sent with a warning or held back; a Never mask list and Try it (#1977)
Settings › Privacy gets two sections under What to mask:

- When a value is found: Mask, Warn or Block per kind (redactActions;
  a kind left out is masked). Warn sends the value as written and records
  it with a placeholder nobody remembers, not counted as masked. Block
  masks it as before and the gateway answers 400 naming the kind and the
  value's hint, never the value, with nothing sent (maskBlocked); the
  record is kept as Blocked. The secret kinds come first, the personal
  ones behind Show all kinds. A Never mask list (redactAllow) lets a whole
  value go as written in any case; a longer value it is part of is masked.
- Try it: POST /api/masking/preview masks a text as a request would
  (redact.Preview) and remembers nothing: no placeholder stands for it
  afterwards and its values aren't masked later as seen before. The page
  shows the vendor's text, what was found and what would be done, and
  keeps the text in no storage.

The masking card counts requests held back and values sent as written; a
call whose values only went as written gets an amber badge on Recent calls
and the Routing log. zh, zh-TW, ja and de throughout. Docs: gateway-routing
and gui-shell.

Broken by hand: the new GUI badge assertion fails on the old maskedBadge
(no .warn badge drawn); TestSettingsSaveKeepsWhatItDoesNotSend went red
until the two settings were in its fixture; "{n} held back" turned
i18n-keys red against the routing plugin's "{n} held" and was renamed.

Run at this tree (base origin/main 9b0f48612):
- gofmt clean; go vet on macOS, GOOS=linux -tags nogui, GOOS=windows.
- go test -count=1 -timeout 45m redact, maskrec, settings, gwmw, gui,
  gateway: ok.
- GUI, both engines: every Privacy file, mask-actions, masking-records,
  routing-bodies, i18n-keys, old-webkit: 263/263.
- Whole GUI suite earlier at 083b1c8e7's base, beside 7 other runs:
  53 red; alone, the ones left red fail the same on origin/main
  6fc532198 and are filed (#3179, #2228, #2448, #2276, #2549, #2163,
  #2399), except privacy-usage-stats and account-discovery, which this
  change's taller page moved off screen; both fixed here.

Not tried: a real vendor request held back by Block from a real agent.
2026-10-22 04:07:55 +08:00
yetone f21dd99f5a privacy: every masking is recorded, and Settings › Privacy shows it
What was masked is now kept, so the user can see it. Settings › Privacy
opens on a summary card: how many values were kept from vendors over 1,
7 or 30 days, by kind, by finder (rules, your words, the local model,
known values) and by where they went, with a day strip. The page is
grouped into What to mask, How it's found (rules and the local model are
layers, not a toggle: the model adds names and addresses rules can't
see) and Sharing. A records view lists each masked request: its kinds,
finders, placeholders and, while magpie runs, the line around each
value. Gateway › Recent calls and the Routing log carry a masked-N badge
that opens the record.

Originals never go to disk. internal/maskrec writes day files under
~/.config/magpie/masking (0600, 30 days, 100MB at most) holding kinds,
finders, placeholders, a first character or key prefix and a length.
The lines around values are kept in memory for the last 400 records
only, and /api/masking/value answers only while the masker remembers a
placeholder. Nothing is written under MAGPIE_TEST_SANDBOX.

A masker says what it masked through gwmw.Teller (JSONHits, TextHits,
Value); a plugin masker that doesn't is counted only, so older redact
plugins keep working. Every route that masks notes it: chat, Responses,
Codex backend and search, embeddings, images, video, transcription,
decide and the search MCP query. Masking still fails closed: a masker
that panics on any path refuses the request (TestMaskingOnAndOff's fake
now panics on JSONHits and TextHits too, since the gateway asks a Teller
through those).

app.js grows to 1,314,432 bytes, so its budget rises to 1,335,000 and
the core's to 1,528,800 (+1.5%): Settings is the core's own page.

Run at this head (rebased on origin/main a3ebed202):
- go vet clean on macOS, GOOS=linux -tags nogui and GOOS=windows;
  go test ./internal/gateway ok (561s).
- On origin/main c922225f1 (one commit back, the packages either side
  touches): agent, gateway, gui, gwmw, maskrec, provider, redact ok.
- On origin/main 592899467: go test ./... -timeout 45m, 65 packages ok;
  agent and gateway hit the 45m timeout under load ~26 (each mid-way
  through a short test, not a hang) and pass alone with -timeout 100m
  (1133s, 890s).
- GUI, on 592899467, the 42 files this change or main's new commits reach (privacy-*,
  redact-*, masking-records, settings-*, gateway-*, routing-bodies,
  routing-request-metrics, routing-log-icons, account-discovery,
  click-scroll, i18n-keys, old-webkit), both engines, every language:
  475 tests, 475 pass. routing-bodies is green here since 2543fb2c1.

Run on the previous base, origin/main c84864ffc (the whole suites):
- go test ./... -timeout 45m: every package ok (66), exit 0. An earlier
  run found TestMaskingOnAndOff/panics (its fake, above) and
  TestCoreScriptBudget (the budget, above); both fixed.
- gofmt -l clean; go vet on macOS, GOOS=linux -tags nogui and
  GOOS=windows clean; node --check on the changed scripts.
- GUI suite, both engines, every language, --test-concurrency=6:
  11533 tests, 11483 pass, 50 fail. Each failing file rerun alone here
  and on origin/main:
  - red the same on main, filed: codex-predictions (#2624),
    usage-checkin (#2549), usage-agents-hide (#2276), usage-fold
    (#2163), s3-sync (#2448), quota-count-wrap and
    quota-name-translated (#2228), routing-request-metrics (#2571),
    routing-bodies (#2559: red at its "second request pressed" step;
    this change's badge checks before and after it pass),
    routing-rule-remove (#3096, filed now: 0 2 1 alone here, 0 0 2 on
    c84864ffc).
  - load, pass alone: group-member-weight (#2387), routing-model-rest,
    context-tune (Chromium didn't launch in 180s at load average ~45;
    0 0 0 alone on both). An earlier whole run's library-chip-fan red is
    #3072 (filed), plugin-card-update #3079, lightweight #2638,
    segs-glide #2683.
  - mine, fixed: old-webkit, three ":hover, :focus-visible" lists
    Safari before 15.4 drops; split, 6/6.
- Broken by hand, each went red: masked values kept on disk
  (TestNoValueOnDisk), a snip that runs past an escaped quote
  (TestSnipStaysInItsString), the shared-line dedup in the records view
  (masking-records, 10 of 12).
- Real app in a sandbox HOME with the local model on: the summary card,
  flow, chips and records view in light and dark; persons and
  addresses, Chinese names among them, recorded by=model; a grep of the
  masking folder and the sandbox config for the masked values found
  none.
2026-10-21 06:12:37 +08:00