Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

privacy: a request masking would change can go as written to a model you picked

yetone9101bb8879b2
diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md
index 8eb73ad0f..f2bcfd5ce 100644
--- a/docs/subsystems/gateway-routing.md
+++ b/docs/subsystems/gateway-routing.md
@@ -137,7 +137,7 @@ recorded as a prediction failure (`noPrediction`).
## Runtime path
1. `handle(proto)` reads the body and the model (`requestBody`, `requestModel`), then calls `serveAgent`. Every route that reads an agent's body (each API, count_tokens, Gemini's, embeddings, images, videos, System One, the Codex backend path) reads it through `readBoundedRequestBody` in [`request_bounds.go`](../../internal/gateway/request_bounds.go). It decodes a `Content-Encoding` of `gzip` or `zstd` (`decodedBody`; Codex sends zstd to a custom provider's `/v1/responses` too, #1223) and drops the header, so the provider is sent plain JSON, with one exception: a ChatGPT account's turn (POST `CodexBase`/responses, from the accounts loop in `forwardOnce` or relayed by `codexUpstreamOn`) is compressed again with zstd and a window of at most 2 MiB, as Codex's own client sends it (`provider.CodexCompressed`; openai/codex `responses_request_compression` and `http-client/src/request.rs`). It is compressed once the body is final, and again on each try. `/responses/compact`, an API key's requests and every other provider stay plain. Sent plain, a 1.1 MB text-only turn was answered 503 (ylorn on X). The plain body is what magpie keeps and shows, and `codexDropped` judges a reset by the bytes that went on the wire. `codexDropped` also reads Envoy's 507 "exceeded request buffer limit while retrying upstream" (the backend failed and the front couldn't retry a body that large) as that reset, at any size, told as a 503 so magpie, Codex and pi ask again and nobody rests (#2034). The size limit (`defaultBodyLimit`, 512 MB) holds for the decoded bytes (413), and any other encoding is refused 415. It was 128 MB until a long Codex session that had read many images went past it decoded, its inline screenshots resent each turn, and got the 413 on all five of Codex's retries (#1422, `TestImageHeavyCodexTurnIsRead`). magpie's own 413 says it is magpie's gateway limit, with the size, and that the provider was not asked (`overLimit`); `writeError` adds to any 413 it isn't holding that this is a byte limit, not the model's context, and to `/compact` or start a new conversation (`TestOverLimitSaysWhoseAndWhatToDo`). Every shape but Gemini's types a 413 `request_too_large`, Anthropic's own type, which pi's overflow table knows: pi compacts and tries once more rather than stopping on an OpenAI-shaped `api_error` (@wilsonxuuuu on X, `TestPayloadTooLargeSaysRequestTooLargeToPi`). `keyLimited` reads a limited key's compressed body the same way, so its reservation is the decoded size. `serveAgent` runs the [middleware](gateway-middleware.md) chain, then `serve`.
-2. `serve` redacts secrets in the request (`redacted`; `redact.MaskJSON` leaves ids alone, and in any text a backend's id (a UUID, 32 hex digits or more, or a digest of 16 to 31 hex digits in one case with letters and digits both, a cache key or a short commit, which went as a key before: _kaichen on X, 10-28; `digestShaped`), or an element token cua-driver hands the model for computer use (`s0000002a:14`, #3782), goes as written: no rule, model label or value known from before masks a piece of one (a phone number at the end of `…-a13800138000`, a card number across a UUID's groups), and a whole one is masked only by the user's own words and rules or as a credential, by the password, API key and token rules or a secret rule whose field is named for one (`api_key=`, `refresh_token`), never one named for an id, a cursor, a request or a digest (`secret_id`, `next_page_token`, `Idempotency-Key`, `secret_hash`), nor cua-driver's own `element_token` (any other name with `element` in it, `element_api_key`, is a credential's as before), a bare `key` (not after api, access, secret, private, signing or license: `keyPhrase`), a field named for a cache, commit, object, blob, file, content, revision, partition, shard, bucket, dedup, lookup, row or sort key, a serial number, or what the model takes a UUID or an element token for, nor an element token known from before (one the model labelled in prose a turn earlier) (`noIDs`, [`internal/redact/ids.go`](../../internal/redact/ids.go)); and a tool's name and its MCP server's (`namespace`, `server_label`, `server_name`, #3143); the request's tool definitions and output schemas, found by their place in each protocol (top-level `tools`, `tool_choice`, `response_format`, `text`, `generationConfig`, `stream_options`, the client's ids in `client_metadata` and the like, and the tools of a Responses `additional_tools` or `tool_search_output` item), go as written save their descriptions, so a word the user masks can't break a grammar or an enum (#1911); and what the vendor sealed with a signature, a thinking block, Gemini's thoughts or Responses' encrypted reasoning, goes back as the vendor wrote it: only the values masked elsewhere in the request, and secrets masked before, are put back as their placeholders; a value masked once, at least 8 characters long, is masked again wherever a request has it, as an exact match whatever words are around it, so what the vendor wrote about a placeholder doesn't go back with the value in it, `redact/known.go`; a text masked for a vendor before, such as a system prompt or a turn already sent, is masked as it was then, so a value a rule first finds later in a conversation doesn't change the prompt the vendor has cached and rewrite all of it to the cache, `sentKnownSpans`), resolves the model to a provider or a group, and refuses what can't be served. The reply has the values put back on its way to the agent (`redact.Writer`, a stream event by event, a whole body at its end, before the writer outside it that names the member, `memberWriter`, writes what it holds: the other way round a whole reply with a value masked in its request went out a 200 with an empty body, #4129, `TestMaskedWholeReplyNamesMember`): a placeholder the model wrote as it was given, or as models rewrite one (spaces inside the braces, a brace or both dropped, another case), is the value again when what is left names one magpie made, and one split across events is held until it is whole (`redact.Restore`, `partialTail`, [`internal/redact/redact.go`](../../internal/redact/redact.go)). A request whose provider and fallbacks, or whose group's models, all skip redaction (`unredactedRoute`, `provider.SkipsRedaction`: set *Send requests unmasked*, `Unredacted`, with every address on this machine or the local network) goes to them as the agent wrote it and isn't masked on its way, since a request is masked only once its route is known (#3973: masking a body whose masked copy was thrown away took up to minutes of magpie's own time). The log, recent calls and the request archive keep it as it went (the archive's bodies have their secrets taken out, `redact.ScrubJSON`, as every call's do), whether the archive is on or off; an observer that wants the bodies (the OTLP export, a JS plugin's) is handed it masked, masked once when it is first handed it after a try (`Call.maskIn`, `laterMasked`), never before the request is sent. A request turned away before it is routed is logged masked (`maskedCopy`). count_tokens and embeddings follow the same rule. What a routing group's Jev is told of the turn (the message, the user's message before it and the reply to it) is masked all the same unless Jev's own provider skips redaction too (`maskedState` in `askJev`, [`decide.go`](../../internal/gateway/decide.go)): a remote Jev is none of the group's members. The masking is the built-in plugin `redact`'s (`gwmw.Masker`, [Plugin runtime](plugin-runtime.md)); when the settings ask for masking and no masker serves, or it fails, the request is a 503 and nothing is sent (`redacted`, `gwmw.Masking`).
+2. `serve` redacts secrets in the request (`redacted`; `redact.MaskJSON` leaves ids alone, and in any text a backend's id (a UUID, 32 hex digits or more, or a digest of 16 to 31 hex digits in one case with letters and digits both, a cache key or a short commit, which went as a key before: _kaichen on X, 10-28; `digestShaped`), or an element token cua-driver hands the model for computer use (`s0000002a:14`, #3782), goes as written: no rule, model label or value known from before masks a piece of one (a phone number at the end of `…-a13800138000`, a card number across a UUID's groups), and a whole one is masked only by the user's own words and rules or as a credential, by the password, API key and token rules or a secret rule whose field is named for one (`api_key=`, `refresh_token`), never one named for an id, a cursor, a request or a digest (`secret_id`, `next_page_token`, `Idempotency-Key`, `secret_hash`), nor cua-driver's own `element_token` (any other name with `element` in it, `element_api_key`, is a credential's as before), a bare `key` (not after api, access, secret, private, signing or license: `keyPhrase`), a field named for a cache, commit, object, blob, file, content, revision, partition, shard, bucket, dedup, lookup, row or sort key, a serial number, or what the model takes a UUID or an element token for, nor an element token known from before (one the model labelled in prose a turn earlier) (`noIDs`, [`internal/redact/ids.go`](../../internal/redact/ids.go)); and a tool's name and its MCP server's (`namespace`, `server_label`, `server_name`, #3143); the request's tool definitions and output schemas, found by their place in each protocol (top-level `tools`, `tool_choice`, `response_format`, `text`, `generationConfig`, `stream_options`, the client's ids in `client_metadata` and the like, and the tools of a Responses `additional_tools` or `tool_search_output` item), go as written save their descriptions, so a word the user masks can't break a grammar or an enum (#1911); and what the vendor sealed with a signature, a thinking block, Gemini's thoughts or Responses' encrypted reasoning, goes back as the vendor wrote it: only the values masked elsewhere in the request, and secrets masked before, are put back as their placeholders; a value masked once, at least 8 characters long, is masked again wherever a request has it, as an exact match whatever words are around it, so what the vendor wrote about a placeholder doesn't go back with the value in it, `redact/known.go`; a text masked for a vendor before, such as a system prompt or a turn already sent, is masked as it was then, so a value a rule first finds later in a conversation doesn't change the prompt the vendor has cached and rewrite all of it to the cache, `sentKnownSpans`), resolves the model to a provider or a group, and refuses what can't be served. The reply has the values put back on its way to the agent (`redact.Writer`, a stream event by event, a whole body at its end, before the writer outside it that names the member, `memberWriter`, writes what it holds: the other way round a whole reply with a value masked in its request went out a 200 with an empty body, #4129, `TestMaskedWholeReplyNamesMember`): a placeholder the model wrote as it was given, or as models rewrite one (spaces inside the braces, a brace or both dropped, another case), is the value again when what is left names one magpie made, and one split across events is held until it is whole (`redact.Restore`, `partialTail`, [`internal/redact/redact.go`](../../internal/redact/redact.go)). A request whose provider and fallbacks, or whose group's models, all skip redaction (`unredactedRoute`, `provider.SkipsRedaction`: set *Send requests unmasked*, `Unredacted`, with every address on this machine or the local network) goes to them as the agent wrote it and isn't masked on its way, since a request is masked only once its route is known (#3973: masking a body whose masked copy was thrown away took up to minutes of magpie's own time). The log, recent calls and the request archive keep it as it went (the archive's bodies have their secrets taken out, `redact.ScrubJSON`, as every call's do), whether the archive is on or off; an observer that wants the bodies (the OTLP export, a JS plugin's) is handed it masked, masked once when it is first handed it after a try (`Call.maskIn`, `laterMasked`), never before the request is sent. A request turned away before it is routed is logged masked (`maskedCopy`). count_tokens and embeddings follow the same rule. A request masking would change can go to such a model instead (jessyoungyu on X: 「{{百度}}这家公司怎么样」 can't be answered from a placeholder): with Settings › Privacy's *Instead of masking, send to* set (`RedactRouteTo`, `POST /api/settings/redact-route`, which takes only a model `MaskRoute` passes: `unredactedRoute` for its provider and fallbacks, or its group), an agent's turn that masking would change (`maskHits.n` > 0: a warn-only hit doesn't count, and a held-back one stays held back) goes as written to that model, the model asked for left alone, in `serve` after the request is masked (`maskRouteFor` in [`redact.go`](../../internal/gateway/redact.go); the masked copy is thrown away, so streaming and every API go as for any route). If it fails, the request fails: its fallbacks are all unmasked ones by `unredactedRoute`, and nothing is masked and sent to the model asked for. It is judged again on each request, so a route that has since got a vendor's address, a fallback elsewhere, was turned off, or isn't one of a gateway key's models is ignored and the request is masked as before. Only an agent's own turn is moved (`agentTurn`, set in `serveAgent` and cleared by `ownAsk`): a title, a search stand-in, a picture described, System One and Jev stay masked. count_tokens is counted on the route (`countTokens`), and a turn of Codex's own model on its ChatGPT sign-in goes there too (`codexMaskRoute` in `codexBackend`; masked once to decide, only while it is set). Unset, as by default, nothing changes. Tests: `TestMaskedRequestGoesToTheModelSet`, `TestCodexSigninTurnGoesToTheModelSet`, `TestMaskRouteOnlyForTheAgentsTurn`, `TestMaskRouteTakesOnlyUnmaskedRoutes` ([`mask_route_test.go`](../../internal/gateway/mask_route_test.go)) and `TestRedactRouteSetting` in internal/gui. What a routing group's Jev is told of the turn (the message, the user's message before it and the reply to it) is masked all the same unless Jev's own provider skips redaction too (`maskedState` in `askJev`, [`decide.go`](../../internal/gateway/decide.go)): a remote Jev is none of the group's members. The masking is the built-in plugin `redact`'s (`gwmw.Masker`, [Plugin runtime](plugin-runtime.md)); when the settings ask for masking and no masker serves, or it fails, the request is a 503 and nothing is sent (`redacted`, `gwmw.Masking`).
- With *Mask personal data* on, each kind in `redact.Categories` ([`internal/redact/more.go`](../../internal/redact/more.go)) is masked unless the user turned it off (`redactKinds` in settings, `Options.Kinds`). On by default: US SSNs, passport numbers, IBANs that pass mod 97, birthdays next to a word that says so (a date over a year ago: one in the last year or ahead is the day an agent runs on, `dateOK`); the word must be right before the date, so a log line's or a file name's date is none, and a time beside the date doesn't make it a log's: a date of birth stored as a time (`1990-05-11T16:00:00.000Z`) is one (`TestBirthdayLogDates`; #3878), MAC addresses, and serial numbers next to S/N or the like. Eleven digits from 13 to 19 are a mainland mobile's form and an id's too, so they are a phone only with +86 or 86 before them, as the value of a field whose name says phone (phone, mobile, tel, cell, contact, 手机, 电话, a field named `number`), or in prose with no id's word right before them (order, id, no., #, 订单号, 编号, 单号, 流水号); under order_id or uid, after 订单号, or with a letter or digit against them they go as written (`phoneAt` in [`internal/redact/phone.go`](../../internal/redact/phone.go); _kaichen on X, 10-28). Off by default: the user name in a home folder, user@host in a prompt or ssh command, public IPv4 and IPv6 addresses (never private, loopback, link-local, CGNAT or documentation ranges) and cloud bucket names, since agents send them in nearly every request and the model must copy the placeholder exactly inside a path or URL. When the user name is on, it goes as a made-up name of 8 lowercase letters, the same for that name every turn and put back in replies like any placeholder (`standIn` in [`shape.go`](../../internal/redact/shape.go)), not as `{{USERNAME_…}}`: with mimo-v2.6-flash a model read `C:\Users\{{…}}` as a temporary folder and set out to delete it (eerttreeeert on X, 10-28). A public IP address goes the same way as an address from the documentation ranges (192.0.2.0/24, 198.51.100.0/24 and 203.0.113.0/24, host .101 to .254; 2001:db8::/32 for IPv6), the same one for that address while magpie runs and never another address's, put back only where it stands as a whole address, in a streamed tool call's arguments too (`ipStandIn`, `restoreAddr`; `TestIPGoesAsADocumentationAddress`). Told `{{IP_…}}`, models asked the user for the real address or searched `~/.ssh` for it instead of running ssh (0x0_air on X, 10-28); past the 462 IPv4 stand-ins an address goes as its placeholder. An EVM address (`0x` and 40 hex digits) is public, so the local model's finds leave it as written unless a key's name is right before it; a 64-digit private key is still masked (`evmRe` in `modelFits`; `TestEVMAddressGoesAsWritten`). Context rules run only near their words (`rule.matches`). What reads as an address but ends in a file's extension that no domain has (`…@2x.jpg`, `[email protected]`; `fileExt` in `realEmail`), or a scale before an extension that is a domain too (`[email protected]`, `[email protected]`; `scaleExt`), is a file's name, for the rule and the local model alike (#3029). A domain needs a dot and labels a host can have, so a disassembly's symbols go as written for both too: `system@plt`, `[email protected]`, `memcpy@GLIBC_2.14`, `foo@@GLIBC_2.2.5`, and `system@got` (`.got` is a top-level domain; `mailHost` in `realEmail`, `TestSymbolIsNoEmail`; ch35tnut_ on X, 10-28). `[email protected]` is still an address. The Settings rows come from `REDACT_KINDS` in app.js, which `TestRedactKindsAreRedacts` holds to the same ids and defaults.
- With *Find with a local model* on (`masking.ModelWanted`: on by default since 10-28, `settings.RedactModel` the user turning it on and `RedactModelOff` off, this computer's own, kept by `KeepOwn`; a user with a model downloaded or picked and neither flag turned it off before the default, and `RedactModelAuto` marks the default's own download), a local model finds what no rule can, a name or an address in free text, under the same switches: a name, address, phone, email or account number is personal data, a secret a secret (`modelKinds`, `modelSpans` in [`internal/redact/model.go`](../../internal/redact/model.go)). `MaskJSON` asks it once per request, for every text it would mask, each once (`withModel`), but for what the agent defines (its tools' and schemas' descriptions, under `defKeys`), which the rules alone mask, as with the model off, after the rest of the request so a value the model found in a message is masked there too as known: those were 52,577 of Claude Code 2.1.296's 67,532 bytes the model read, and a 4-core server's first request waited out the gateway's 2 minutes (niuniu_8964 on X; 11.5s cold at 4 procs on a Ryzen AI Max+, 2.6s without them, and on three real Claude Code requests the model masked nothing there the rules didn't); its spans join the rules' with a rule's kind kept where both find a value, and ids a vendor or tool made (`req_…`, `toolu_…`) are never taken, nor a snake_case word as a name (Codex's `sequential_cutoff`, #3144), nor as a secret what names a thing: a UUID, a digest with its algorithm's name, a file's path (`namesThing`; the model takes all three for secrets). A package's name goes as written, for the model and the rules alike (dniray on Discord, v0.1.1917): the module a line of code loads (`from ' drizzle-orm '`, `require('jsonwebtoken')`, `from x import`, `use x::`, with an editor's line number or a diff's mark before it; `moduleAt`, `codeHead`), a manifest's dependency pinned at a version (`"drizzle-orm": "^0.36.4"`, TOML's `x = "9"`; `dependencyAt`), a folder under node_modules or site-packages, the module an error could not find (`Cannot find module 'x'`, `No module named 'x'`; `missingModuleAt`), a package an install names (`pnpm add x`; `installedAt`) or pins with @ (`[email protected]`; `pinnedAt`), all in `packageAt`; a value known from an earlier mask (`knownSpans`) goes as written there too, since once the model took the name for a secret anywhere in a request it was masked on the import line as known (dniray, v0.1.1965), a requirement's name before its pinned version (`django-rest-passwordreset==1.4.0`; `pinnedAfter`), a lock file's integrity hash (`sha512-…`; `sriRe` in `namesThing`), a version as the value of a field named for a secret (`"js-tokens": "^4.0.0-beta.1"`; `versionValue` in `fieldAt` and `keyAt`), and a package pinned at its version as user@host has it (`[email protected]:` in pnpm-lock.yaml; `notPackagePin`); `TestPackageNamesGoAsWritten` and `TestModelPackageNamesNotMasked` hold them. The model misses many Chinese names (张伟 after 给, 陈建国 after 客户, however low its threshold) and runs a name it finds on into the words after it (「王芳,她下周一」), so with Privacy Filter a Chinese name is also found by its surname and a word around it that says a person is meant (客户, 我叫, 给…发, …先生, …说, joined by 和 to another name; 和 or 与 says so only after a person, 吴敏和郑浩 but not 判断和边界, 发 or 打 only as the verb, 发了个 but not 关键发现, and a sentence starts after punctuation, not after a space after code, `gh pr view` 易触发; xuanwo on X; 一下 only after a verb that asks a person, 问一下张伟 but not 改一下文案, a word any subject takes after it, 已经, 明天 or 他, only with a person's cue before it, and 说明 is no 说; zeewell on X), and the model's are cut at punctuation and titles ([`zhname.go`](../../internal/redact/zhname.go)); a surname with nothing around it (王国的, 张开的) is left, and so is a common word that starts with one (`zhWords`: 文案, 文档, 方案; 毛利, 毛重, 毛巾, since 毛利 is a margin far more often than the Japanese surname, so 毛利低 goes as written: _kaichen on X, 10-28; the model still finds 毛利兰, and neither model nor rule masks 毛利小五郎), the model's own name too when it is that word whole. A name the model or the cues take is also no name when it is an ordinary word (`zhWordShaped`, `zhWordNotName`, the one check both go through): a colour (黄绿色, 红色, any that ends in 色 or is colour characters alone: hh05928822 on X after v0.1.1850), a word that starts with a surname from `zhWords` or `zhDict` (about 1,550 two-character words from jieba's dictionary, [`zhdict.txt`](../../internal/redact/zhdict.txt), its header saying how they were chosen; 边界, 应该, 尚可), or such a word with what makes a longer word after it (关键词, 关键字: wangjunxiao128 on X; 应用层, 毛利率), unless a role before it or a title after it says a person is meant (客户方圆, 江山老师), where a name that is also a word is masked. The surnames stay (黄 starts 黄绿色 and 黄晓明), and 毛利兰, 关羽 and 黄晓明 are still masked. With AstrLink Guard only a nickname is (小王, 老李, `zhNick`; `redact.Options.NamesByModel`): Guard at 0.8 finds the full names those cues did in zhname_test's sentences (张伟, 陈建国, 欧阳娜娜…), and misses the nicknames, 孙经理, and all but the surname of 王老板 and 邓总; the cues took words for names (关键发现; xuanwo on X). The model is the user's pick (`settings.RedactModelName`, `pii.Choose`; the app's menu beside the switch, `pmPick`): AstrLink Guard (QuantumNous/astrlink-guard at a pinned revision, Apache-2.0, a 39 MB int8 Multilingual-MiniLM token classifier for Chinese and English), run in magpie's own process on the CPU by [`internal/pii/guard`](../../internal/pii/guard) (an ONNX reader, the XLM-R tokenizer and int8 kernels in Go, NEON/SDOT on arm64 and SSE2 on amd64; its labels named as Privacy Filter's, `guardLabels`); a text over one window, 510 tokens, is read in windows that overlap by 128, and on the CPU as many windows at once as it has cores, half of `GOMAXPROCS` and 4 at most (`guard.Cores`: on every core a long history unread kept a 32-core Linux box at 13 cores' worth for as long as requests came, xinvxueyuan and shensmart on Discord, 10-28): one window's matmuls split over the cores left them idle between, and a 4-core server's first Claude Code request waited minutes (niuniu_8964 on X; 22,488 tokens took 23.9s at 4 procs, 17.9s at once, and 48.8s against 30.5s at 2), and on a Mac through CoreML on the Neural Engine or the GPU ([`coreml.go`](../../internal/pii/guard/coreml.go), [`internal/pii/guard/coreml`](../../internal/pii/guard/coreml); yyhhyyyyyy on X; Settings › Privacy names which, from CoreML's own compute plan of the compiled model, `MLComputePlan` on macOS 14.4 and later, the device most of its program's estimated cost is on (`coreml.Plan`, `Model.Backend`, `Status.Via` and `Device`): it said "on the CPU" under CoreML, yyhhyyyyyy on Discord, 10-27; an M5 Pro's plan puts all of it on the GPU; after a CoreML read fails it says the CPU and why until one doesn't): the same network from the same int8 weights in float16, written from the downloaded model into a program made by `coreml_gen.py` and compiled once into the model's folder, 512 tokens in about 12 ms where the CPU takes 400, its probabilities within .07 of the publisher's fp32 model where the CPU's are within .13 (the CPU quantizes activations as the int8 model does, CoreML doesn't), so a span near the threshold can fall the other side (a go.mod module path is a secret at .81 on CoreML, .79 on the CPU, .785 in fp32); a window under 16 tokens (`coremlMin`), a Mac before macOS 13 or any failure goes to the CPU, loaded on the first request and let go after 10 minutes with none; or OpenAI's Privacy Filter. With no pick it is AstrLink Guard, the default; a user who had Privacy Filter on before there was a choice keeps it only while AstrLink Guard downloads, which the first masked request starts and which then saves AstrLink Guard as the pick (`masking.picked`, `toGuard`; an hour between tries after a failure). Measured for that default (#3578): on 234 segments of real code and config it found 2 spans where Privacy Filter found 100 (38 of them go.sum hashes after redact's own checks), and with the rules it found 42 of 42 test values to Privacy Filter's 41. Each model has its own download, cache file and turn (`backend`). AstrLink Guard's turn has `guard.Cores()` places (`guardTurn`), so that many requests read at once, and they share those cores: every window, of any request, takes one of `guard.Cores()` tokens before it reads and more only while they are free (`guard.take`), so requests together never read on more cores than one did (the 4 promised xinvxueyuan and shensmart above); the model loads once (`guardLoading`), the idle timer lets go only when no place is held (`guardIdle`), and a text one request is reading another waits for rather than reading it too (`backend.reading`). Before, one request read at a time, and an agent's parallel requests queued behind it (yyhhyyyyyy on Discord, 10-28, who asked for 16 at once). Measured at 1, 2, 4 and 8 requests at once (a Ryzen AI Max+ 395 under WSL, 4 cores): 512-byte requests 4.2, 6.1, 8.1 and 8.4 a second where one at a time was 4.2 whatever came, on 1.9 to 3.9 cores, memory flat at about 150 MB; 4 KB requests already read their 3 windows on 3.6 cores alone, so they stay at 0.6 a second (Windows on the same box: 7.5 to 12.1 a second at 1 to 8, 4 KB 0.7 to 0.8, on at most 3.8 cores). Each request on cores of its own would read 20 a second at 8, on 12 cores, and 4 KB at 8 took 20 cores and 521 MB (953 MB on an M5 Pro), so the bound is the cores, not the count, and there is no setting for it. Through CoreML the GPU is the bound: on an M5 Pro 4 KB requests read 23, 20, 25 and 25 a second at 1, 2, 4 and 8, a model per request no faster, so CoreML reads one window at a time, and only the windows under `coremlMin` or after a failure use the CPU's tokens. Both find at a floor of 0.5 (`pii.Floor`) and give each span its score (AstrLink Guard: the least of its tokens' probabilities, rounded to a float32 so a cached span reads back the same; Privacy Filter: pf-serve's own), and masking keeps those at the model's threshold or over (`pii.Threshold` in `masking.model`): the user's for that model (`settings.RedactModelScore`, this computer's own; Settings → Privacy's Confidence threshold, `pmScore`, 0.5 to 0.95), else the model's own (`MinScore`: Privacy Filter 0.5, AstrLink Guard 0.8, its publisher's). A value known from before follows the same threshold: each value masked remembers the highest score anything found it with (a rule's counts as sure), and one only the model found, under the threshold now, is no longer masked again as known (`noteScores`, `knownSpans` in [`known.go`](../../internal/redact/known.go); before, a raised slider changed nothing for a value masked once: _kaichen on X, 10-28). The defaults stay because on the eval a higher one mostly loses values the checks after the model would have kept (Privacy Filter at 0.85 lost a Chinese name; AstrLink Guard at 0.9 a phone in a tool result), while the false alarms it would drop are mostly dropped by those checks already (Yayoi_no_yume on X). Measured again on 2000 segments of real agent traffic (Codex and Claude Code sessions: prompts, tool arguments and results, a quarter with Chinese) and 30 more labelled cases: AstrLink Guard at 0.88 rather than 0.8 drops 3 false alarms (a model id, a sandbox path word, a token counter) and lets a colleague's name and a signed name through; Privacy Filter at 0.88 rather than 0.5 drops 11 (timestamps in paths, log lines, a plugin id) and lets 6 real names and a phone through (4 of the names in the real traffic). Lowering Guard to 0.5 finds 2 more names in the real traffic and 3 more labelled values, for 10 more false alarms (9 of them Claude Code's `<total_tokens>` counter). Those false alarms are shapes a check can turn away; a threshold can't without losing real values. Privacy Filter is a q8 GGUF (1.6 GB), run by `pf-serve` ([`native/privacy-filter`](../../native/privacy-filter), built from privacy-filter.cpp on ggml and released under `pf-v*` tags; on a GPU it holds the model once, in the device's memory, #3590; every pf-serve keeps the engine's own MoE chunk, `PF_MOE_CHUNK` at the window's 4096, which an RX 470 through Vulkan read 6-9% faster than 2048 in interleaved runs and Metal ~6% faster, and inherits a value the user set): Settings → Privacy's On downloads both (`pii.Download`, each checked against its pinned sha256, through magpie's proxy (`netproxy.Func`, as its other requests; hh0592821 on Discord, whose download went direct before), from hf-mirror first in China (`source.chinaFirst`) and from hf-mirror after Hugging Face everywhere else, the switch off too (`source.modelMirror`), so a poisoned or blocked huggingface.co doesn't stop it; hf-mirror serves AstrLink Guard's files itself and sends Privacy Filter's back to Hugging Face) and saves the setting only once they are on disk (`/api/privacy-model`, [`privacymodel.go`](../../internal/gui/privacymodel.go)). `pii.Find` starts `pf-serve` on the first request (Metal, Vulkan, else the CPU; one that dies on the GPU, doesn't load there in 40s (`gpuLoadTimeout`, well inside the gateway's 2 minutes), or doesn't answer in time there twice in a row, is started on the CPU, and Settings says why; a request it didn't answer in time once is tried on the GPU again, since a slow request on a weak GPU is no lost GPU (#3682). The GPU is tried again 10 minutes later, doubling to 2 hours while it keeps failing, with no restart (`gpuRetryAfter`), and one that answers is the model's again), joins short texts into 12 KB chunks and maps the spans back, cuts a text over 12 KB into pieces of 12 KB at most that overlap by 1 KB (`pieces`: after a line's end, else a space, of those in the piece's second half the one whose 48 bytes before it hash least (`landmark`), so a line edited early in a text leaves the pieces after it as they were, else at a rune's start), each kept by its own hash, their spans put back at the text's offsets and merged where pieces overlap (`merge`: one found twice is one, one cut by a piece's end is whole), and a value of 8 bytes or more found in a text is found wherever else it is in it, as sure as its surest find (`spread`: the model scored an address on 400 lines alike 0.9 on some and 0.5 on most, wangwu25685333 on X), and asks pf-serve a chunk at a time (#3682: a 480 KB text whole took an RX 470/580 90s and kept nothing when cut off), keeps the last 20000 answers by hash, on disk too (`found-<model>.bin` beside the model: hashes, offsets, labels and each span's score, never text; a `pfound01` file from before scores is read with every span sure, so it masks as it did; written through temp-and-rename, a bad read taken as empty), so a restart doesn't ask the model about every history again (#3682), and ends it after 10 minutes with no request. Its requests take turns on its one pipe (`turn`), and a request reads the cache again when its turn comes; a request waits for its turn only while its context lives (`pii.FindContext`, through `gwmw.ContextTeller`), so one whose client went away leaves the queue (the gateway's 2 minutes leave masking's time out, #3682); the chunk pf-serve is on is finished and kept, so a request sent again asks only about what is left, and between chunks the turn is given up and taken again, by the request waiting with the least left to read (`takeTurn`, `queue`), so a short request goes before the rest of every long one (wangwu25685333 on X: a 5 KB request behind two long ones on 4 cores waited 8.3s in the order they came, 4.5s now). The status (`pii.Now`), `pii.Stop` and the idle timer never wait for a load or a request: `srv.mu` is held only to read or set the state, and `Stop` ends a load or a request under way at once. A request held before routing while it waits for the model is answered 504 saying so, and how to send without it (`unroutedMsg`); one of an older tag (`oldTags`, magpie updated since the download) runs until the pinned tag's is fetched (`upgrade`, on its start), which is then started on the next request and the old deleted ([`internal/pii`](../../internal/pii)). A failure names what ended `pf-serve`: magpie's own end of a request it didn't answer in time, else its exit status or signal and its last stderr lines past ggml's startup chatter (`died`, `lastSaid`). After one, requests are held back at once with that failure, not after a load each, until the model is tried again 30s later, doubling to 10 minutes while it keeps failing (`heldBack`, `retryAfter`; lazyeo on Discord, an M2 whose every request waited out the 504). Until the model is on disk the rules mask alone, and its download starts (`masking.fetch`, an hour between tries; the default's, done, saved by `settings.UseDefaultRedactModel` unless the user picked or turned it off meanwhile; cancelling it is turning it off). A model the user turned on that fails (didn't start, isn't on disk, held back after a failure) panics, so the request is held back with the 503 above rather than sent with the rules' masking alone (`masking.model`, strict); the default's finds nothing then, and the rules mask alone. A model that is only slow is waited for at most 2 seconds from the request's masking start when on by default (`masking.defaultWait`) and 15 seconds when the user turned it on (`masking.strictWait`): its turn, its load and its reading. Past that, or when the request's context ends first, `pii.FindContext` answers with what was known of the request's texts by then (its cache: the history read on earlier turns, and the chunks read meanwhile) and a `*pii.Partial` saying how many it hadn't read; `masking.model` masks those with the model's finds and the rest with the rules alone, logging it at most once a minute (`notePartial`), and the request goes. Nothing the rules mask goes as written. Before, the user's model was waited for with no bound and any end of the wait held the request back whole, its known finds thrown away: in a burst of requests with text the model hadn't read, xRayySummers on X (0.1.1877, Linux) saw masking take 1.2s to 290s in one minute and each request it ended was "masking failed, so nothing was sent". Reproduced on 8 cores of a Linux CPU: AstrLink Guard reads about 6 KB a second there (a 125 KB history 20.8s), and six requests at once with 67 to 145 KB of new tool results each took 62 to 110s, all ending near the last, since the turn goes round between chunks; with the bound each went at 15.0s with the history's finds and the model's for 1 to 4 of its 9 texts. `masking.find` gives a model that doesn't stop for its context 100ms to answer with what it knew (`handBack`) before giving up on it. AstrLink Guard reads on the CPU, so a long Codex history it hasn't seen took minutes there, and every request queued behind it showed masking with no bound (shensmart on Discord, 10-28, magpie in Docker). The wait is given up even where `Find` doesn't stop for its context. A request that stops waiting leaves the chunks after the one being read (`backend.find`), so it doesn't hold the turn; that chunk's answers are kept for the next request. Each model reads a request's unread texts newest first, the last text and a long one's last piece before the rest (`backend.missing`), since an agent's history is oldest first and what a request adds is at its end; AstrLink Guard joins them into chunks of 4 KB, not Privacy Filter's 12 KB (`backend.chunk`, `guardChunk`), so the chunk a request leaves the model finishing past its wait is about a second on 4 cores. The default's request is `pii.Bounded`: what one went without the model's reading is remembered by hash (`backend.sent`), and a request after it reads what it adds first and that backlog `backlogCap` (4 KB) at most, nothing found for the rest, so a history resumed or never read is read a little a request rather than on every core for each one. Measured on a 2 MB, 200-turn Codex history never read, 30 requests 3s apart (`TestLiveHistoryCost`, gated on `MAGPIE_GUARD_FILES`): on a 32-core Linux box 33 to 60 CPU seconds a request before, 23 minutes of CPU over the session's 108s (13 cores busy), 6 to 10 after, 4 minutes over 90s (2.7 cores), and on an M5 Pro's CoreML 2.2 CPU seconds a request for the first three before, the first alone after; nothing runs on after the last request either way. A request the user turned the model on for still reads all of it, waited for `strictWait`.
- With *Mask secrets* on, the user's own masking rules (`redactRules`, `redact.Rule`: a placeholder name and a prefix or an RE2 pattern, [`internal/redact/rules.go`](../../internal/redact/rules.go)) run before magpie's. Settings → Privacy adds them, removes them, and edits one in place (`POST /api/settings/redact-rule` with the rule's index and its current value, `editRedactRule` in [`internal/gui/api.go`](../../internal/gui/api.go)). Every rule is checked by `redact.CheckRules` when settings are saved; an edit that doesn't compile, matches the empty string, repeats another rule or names a rule no longer at that index is refused with the rules left as they were. The masker reads the rules from settings on each call (`options` in [`internal/masking`](../../internal/masking/masking.go)), so a change applies to the next request.
@@ -1228,7 +1228,7 @@ included). `previousResponse` in
## Verification
```sh
-go test -tags nogui ./internal/gateway -run 'TestGroupAsksOneRestingMemberNotEach|TestLoneRestingMemberStillAsked|TestRetrievalGroupAsksOneRestingMember|TestProbeOne|TestLoop|TestLongReplyThatDoesNotLoop|TestQuiet|TestSilentHeld|TestRotateSpreadsSessions|TestRouting|TestSmartRouting|TestFallback|TestNoFallbackForOtherErrors|TestLastFallbackErrorReachesTheAgent|TestRateLimitedSinksToTheBack|TestTryNow|TestRenewedToldToTheRoutingPage|TestGroupRateLimitedSinks|TestAffinity|TestSeveralKeysOnTakeOverFromEachOther|TestModelNotInPlanRestsTheModelNotTheKey|TestSubscriptionAccountsTakeOver|TestGroup|TestTrace|TestImageTool|TestCodexNoCredits|TestCodexCredits|TestWindowCapsHoldTheAccount|TestAccountCap|TestClaudeGoesOnMessagesWhereTheRelayHasThem|TestAutoRelaysOnTheClientsOwnAPI|TestSearchOfferedStaysOnTheClientsAPI|TestSearchFollowsTheSearchersProxy|TestCodexSearchAnsweredOnResponses|TestAgentEffortOnCursorLocal|TestAzureCompactionWithoutMagpiesReasoning|TestCodexCompact|TestProtection|TestSearchedConversation|TestUnsearchedConversationUnchanged|TestNotAnAPIReply|TestAPIReplyUntouched|TestCodexBackendWebPage|TestSub2APIKeyLimitIsQuota|TestKeysWeighedByTheirWindows|TestGroupWeighsKeyWindows|TestKeyOutOfItsWindowRestsUntilReset|TestKeyPoolErrorDoesNotRestForWindow|TestKeyBackOnceItsWindowIsNotFull|TestRestingKeyReadAgainInAnOrderedGroup|TestClaudeAccountBackOnceItsWindowRenews|TestThinkingStreams|TestHeldThinkingKeepsTheAgentAlive|TestCodexHeardWithinItsIdleTimeout|TestRefusalAfterThinkingFailsOver|TestGeminiEmptyRepliesAskedAgain|TestLocalUnredacted|TestCORS|TestForeignPagesRefused|TestAIStudioThinking|TestGeminiThoughtsThroughLocalProxy|TestGeminiRefusedLevel|TestGeminiThinkingOffAtItsLowest|TestThinkingOff|TestTranslatedEffortLoggedAsSent|TestTranslatedThinkingOffRefused|TestSub2APIThinkingOffRefused|TestRefusesThinkingOff|TestClaudeFiveFiveThinkingOffOnTheWire|TestSamplingAsTheModelTakesIt|TestHaikuFiveFiveSamplingOnTheWire|TestGeminiThoughtsMarkedInExtraContent|TestGeminiThinkingLevelRefused|TestGeminiMinimalRefusedTwiceAtOnce|TestPassthroughGeminiMinimalRefused|TestPassthroughEffortNoneRefused|TestPassthroughMinimalRefusedNoneTaken|TestOtherOffNamed|TestClaudeSignInNotToldItsSignInFailed|TestSignInWriterReleasesA403|TestSignInBetaStaysWithMagpie|TestStructuredOutput|TestResponsesTextKeepsVerbosityBesideFormat|TestRefusedFormatToldInWords|RPM|TestQueued|TestCustomGeminiUpstream|TestWholeNums|TestGrokCalls|TestOnlyGrokCalls|TestGrokModel|TestGrokPluginCalls'
+go test -tags nogui ./internal/gateway -run 'TestGroupAsksOneRestingMemberNotEach|TestLoneRestingMemberStillAsked|TestRetrievalGroupAsksOneRestingMember|TestProbeOne|TestLoop|TestLongReplyThatDoesNotLoop|TestQuiet|TestSilentHeld|TestRotateSpreadsSessions|TestRouting|TestSmartRouting|TestFallback|TestNoFallbackForOtherErrors|TestLastFallbackErrorReachesTheAgent|TestRateLimitedSinksToTheBack|TestTryNow|TestRenewedToldToTheRoutingPage|TestGroupRateLimitedSinks|TestAffinity|TestSeveralKeysOnTakeOverFromEachOther|TestModelNotInPlanRestsTheModelNotTheKey|TestSubscriptionAccountsTakeOver|TestGroup|TestTrace|TestImageTool|TestCodexNoCredits|TestCodexCredits|TestWindowCapsHoldTheAccount|TestAccountCap|TestClaudeGoesOnMessagesWhereTheRelayHasThem|TestAutoRelaysOnTheClientsOwnAPI|TestSearchOfferedStaysOnTheClientsAPI|TestSearchFollowsTheSearchersProxy|TestCodexSearchAnsweredOnResponses|TestAgentEffortOnCursorLocal|TestAzureCompactionWithoutMagpiesReasoning|TestCodexCompact|TestProtection|TestSearchedConversation|TestUnsearchedConversationUnchanged|TestNotAnAPIReply|TestAPIReplyUntouched|TestCodexBackendWebPage|TestSub2APIKeyLimitIsQuota|TestKeysWeighedByTheirWindows|TestGroupWeighsKeyWindows|TestKeyOutOfItsWindowRestsUntilReset|TestKeyPoolErrorDoesNotRestForWindow|TestKeyBackOnceItsWindowIsNotFull|TestRestingKeyReadAgainInAnOrderedGroup|TestClaudeAccountBackOnceItsWindowRenews|TestThinkingStreams|TestHeldThinkingKeepsTheAgentAlive|TestCodexHeardWithinItsIdleTimeout|TestRefusalAfterThinkingFailsOver|TestGeminiEmptyRepliesAskedAgain|TestLocalUnredacted|TestMaskedRequestGoesToTheModelSet|TestCodexSigninTurnGoesToTheModelSet|TestMaskRoute|TestCORS|TestForeignPagesRefused|TestAIStudioThinking|TestGeminiThoughtsThroughLocalProxy|TestGeminiRefusedLevel|TestGeminiThinkingOffAtItsLowest|TestThinkingOff|TestTranslatedEffortLoggedAsSent|TestTranslatedThinkingOffRefused|TestSub2APIThinkingOffRefused|TestRefusesThinkingOff|TestClaudeFiveFiveThinkingOffOnTheWire|TestSamplingAsTheModelTakesIt|TestHaikuFiveFiveSamplingOnTheWire|TestGeminiThoughtsMarkedInExtraContent|TestGeminiThinkingLevelRefused|TestGeminiMinimalRefusedTwiceAtOnce|TestPassthroughGeminiMinimalRefused|TestPassthroughEffortNoneRefused|TestPassthroughMinimalRefusedNoneTaken|TestOtherOffNamed|TestClaudeSignInNotToldItsSignInFailed|TestSignInWriterReleasesA403|TestSignInBetaStaysWithMagpie|TestStructuredOutput|TestResponsesTextKeepsVerbosityBesideFormat|TestRefusedFormatToldInWords|RPM|TestQueued|TestCustomGeminiUpstream|TestWholeNums|TestGrokCalls|TestOnlyGrokCalls|TestGrokModel|TestGrokPluginCalls'
go test -tags nogui ./internal/gateway -run 'TestMemberTooSmallIsNotAsked|TestMemberTooSmallIsTheLastResort|TestClaudeAccountsSpentFallBackToCodex|TestConversationOutgrowingItsMemberMoves|TestRoomyLeavesWhatItCantJudge|TestPayloadTooLarge|TestOverflow|TestContextOverflow|TestRelayInputCap|TestEveryVendorsOverflow'
go test -tags nogui ./internal/gateway -run 'TestPlain403|TestOrderedKeepsAnAccountAt98|TestPromptRefusalRestsNobody'
go test -tags nogui ./internal/gateway -run 'TestVertex'
diff --git a/docs/subsystems/gui-shell.md b/docs/subsystems/gui-shell.md
index 54d66cd93..b9bf0cc33 100644
--- a/docs/subsystems/gui-shell.md
+++ b/docs/subsystems/gui-shell.md
@@ -18,7 +18,7 @@ three.
| The app's page in a browser | Settings › Network and sharing › Browser access (`renderWebPage` in `plugins/gateway/settings.js`, drawn with the Web pages rows) has the app serve its own page to a browser while it runs, so a tunnel (Cloudflare Tunnel, SSH) or a computer on the network can reach it without `magpie web` kept running in a terminal (jiakun_zhao on X). Off, This computer (`127.0.0.1`, which a tunnel's service points at) or Local network (every address; a "Not encrypted" row without `--tls-cert`). It is `magpie web`'s page and guard, not a second kind: `startAppPage` (from `Run`, after `startBackend`) serves `webServed` over a `Handler` of its own, whose `Windows` is `appWebHost` (a browser tab for `isWeb`, but not `ownWeb`, so the app's windows aren't taken for `magpie web`'s: `webPage` and gateway mode stay the app's; quitting quits the app, and a restart to update from it brings back the windows the app has up, `restartFrom`). The key is always asked for: no mode serves without it. It lives with the mode and port in `web-page.json` beside settings.json (0600, never in settings.json, so sync and backup don't carry it), is made the first time the page is turned on, kept across restarts (a browser's cookie stays good), and replaced by New key, which turns away every browser signed in with the old one (the guard is swapped atomically, `appPage.cur`). A file that can't be read, or names no known mode, is off and says why; Settings rewrites it only given a whole choice. A port another program holds (`magpie web`; the magpie before a restart to update) is said and tried again every 4 s (`appPageRetry`); the port must be 1024 up and not the gateway's. `GET/POST /api/web-page` are on the app's own windows' API only, never a browser's, so a browser on the page can neither read the key it came in without nor turn the page off under itself. `magpie web` on the port the app serves prints the app's link and opens it rather than starting a second (`gui.AppPage`, `samePort`). Sessions are warmed once a process however many `Handler`s there are (`warmOnce`). See `TestAppPageServesOnlyWithItsKey`, `TestAppPageWaitsForABusyPort`, `TestAppPageUnreadFileServesNothing`, `TestAppPageTurnsDownBadChoices`, `TestAppPageBrowserIsATab` and `browser-access.test.cjs`. | [`apppage.go`](../../internal/gui/apppage.go), [`web_cli.go`](../../internal/cli/web_cli.go) |
| The page | `app.js` keeps one state object per view and renders it into a list, with no framework. The views have their own files: `plugins.js` and `context.js`, and those a plugin adds under `assets/plugins/<id>/` (the Gateway, Sessions, Library, Routing and Usage pages, see [plugin runtime](plugin-runtime.md)) (Usage's Context tab, and the context window card the Routing page draws under a request's story, fed by `/api/context` in `context.go`, which scores and tags each agent; clicking a part, a cell or an item opens its text from `/api/context/text`, the last 10 requests' masked bodies in memory (`openCtxText`), rendered 100 items and 20000 characters at a time; its "Tuned for you" cards are fed by `/api/tune` in `tune.go`, see [agent wiring](agent-wiring.md)). Strings go through `t()` with translations in `i18n.js`, and a plugin's own in its `i18n.js` (zh, zh-TW, ja and de; the English text is the key and the fallback). zh-TW is Traditional Chinese with Taiwan's words; a system language of zh-TW, zh-HK, zh-MO or any zh-Hant picks it, and the tray menu and notifications follow (`trayLang`). Dropdowns use `openProtoMenu`. Every app menu (`openProtoMenu`, `openRowMenu`, `openSessCombo`) is placed by `placeMenu`: under its anchor, or over it when the window runs out below; one that fits neither way takes the larger room, its height held to it, and scrolls inside itself, and a scroll inside a menu doesn't close it (#1437, `removed-groups-menu-scroll.test.cjs`). The highest any popover goes is `popTop`: 8px under `header.top` on a Mac window, where the traffic lights are drawn over the page, and 8px from the top elsewhere; `placeMenu`, the field picker's `placePop` (which also holds `#pop`'s height to that room) and an agent's model list (`openAgentModels`) all use it (#1573, `popover-mac-chrome.test.cjs`). Scrolling is done only by `scrollOnPurpose(e)` with the reader's event. | [`assets/app.js`](../../internal/gui/assets/app.js), [`assets/i18n.js`](../../internal/gui/assets/i18n.js) |
| Window navigation | In the window from 1024 points wide (`SIDE_AT`, the same width as app.css's `side-nav` rules), `layoutNav` moves the brand, the pages (`#nav`) and Settings into the sidebar (`#side`): the pages in four groups (Work, Models, Insights, Extend; a group none of whose pages shows, as in gateway mode, leaves out its name), the gateway's address at its foot (`sideGwRead`, read from `/api/providers` only while there is a sidebar, or drawn at once from boot.js's `sideGateway`; on a page other machines open, `magpie web`, it is `MAGPIE_PUBLIC_URL` while they are let in, as the Gateway page's address and About's Gateway URL are, noahhrreion on Discord; a port applied in Settings redraws it), and the header a breadcrumb (`renderCrumb`). A page whose head holds only its tools (Library, Plugins, Settings) has them in the title bar after the breadcrumb, `tools` setting the room between it and the window's own buttons. When they don't fit there the tools show their icons alone (`tools-tight`; each keeps its name as `aria-label` and in its tooltip), then the breadcrumb drops its leading parts, and last the page's name is cut at its end, so no tool lies over the title (#2625, `title-bar-tools.test.cjs`). The open page's own tabs (`NAV_SUBS`: Usage, Library, Plugins, Settings) are listed under it as `.nav-s` rows (`role="tab"`, `data-of` the page tab's id) and the page's strip is hidden; a row's click clicks that tab at once, the selection (`.nav-hl`) having set off as the row was pressed (`navSubGlide`; back if the press leaves the row), and is the list's only shape: a row hovered or pressed has no ground of its own, and a `MutationObserver` draws the list again when the strip changes. A page's tabs share its one scroll, so a row that picks another tab opens it at its top (`scrollOnPurpose` with the row's click); the tab already open, picked again, leaves the page where it is (`sidebar-tab-top.test.cjs`). The list opens and folds with an iOS-like spring (`navSubEnter`, `navSubLeave`; none under reduced motion), and a folding list is out of reach at once. The open page's own row is marked the same way, by one shape (`.nav-top-hl`, the rows' own ground given up to it, hovered or pressed too) that `navTopGo` moves to the row picked on a damped spring, as iOS moves a selection (`NAV_SPRING`, SwiftUI's response 0.42s and damping 0.84; `navTopStep` steps it each frame): it sets off as the row is pressed and comes back if the press leaves the row or the page isn't left, turns with the speed it has when the press changes its mind, and follows its row while the list above it folds, landing where the row ends up. It is put in place, not sent, when the window opens on a page or the rows move under it at rest (a `ResizeObserver`), is away while Settings is open (it has its own selection at the foot), comes back fading in on its row, jumps under reduced motion, and isn't drawn in the header's tabs, which have their `.thumb`. `#nav` is `position: relative` in the sidebar for it, so `.seg`'s centring `left` and `top` are reset. See `sidebar-glide.test.cjs`. Narrower, the page beside a sidebar would be narrower than Usage's requests table needs (768 points), so they go back into the header's row, as a Mac sidebar gives way in a narrow window. At every width the reader can pick the sidebar or the header's tabs from the title bar's button (`#sideToggle`, ⌃⌘S or Ctrl+Shift+S; `toggleSideNav`), kept in localStorage `magpie.navLayout` (`side` or `top`) and so laid out from the first frame; none picked, the sidebar comes from `SIDE_AT`. Picked in a narrow window the sidebar is narrower (184 points under 860), and magpie web on a phone keeps its own tab bar and has no button. The two layouts trade places in a view transition (`navMorph`, app.css `.nav-morph`): the sidebar slides, each page's tab flies to its place in the other, and the page slides as one (its old image dropped, so no line prints twice); without view transitions, under reduced motion, or in the Linux app the change is at once: WebKitGTK with compositing off (`WEBKIT_DISABLE_COMPOSITING_MODE`, which `webkit_linux.go` sets under Wayland) crashes in a view transition. Elsewhere a transition still going after `MORPH_MAX` (1.5s) is skipped, so a webview that never ends one can't leave the window on the old layout's picture. See `side-toggle.test.cjs`. A window made wider or narrower drops what a click held (`heldWidth`): the page laid out again isn't the click's doing. The reader's own place is kept instead for a second while the page is laid out again (`relaid`, a `ResizeObserver` on the open page's parts calling `backToReader`): crossing `SIDE_AT` the browser clamps the scroll to each layout it passes through, and the page ended short of its foot (#1672, `text-size.test.cjs`). See `sidebar.test.cjs`. A sidebar longer than a short window keeps no scroll bar beside its pages: the native bars of `.side-scroll` and `.side-foot` are hidden, and `sideBars` lays a thin bar over each box's edge that shows while it scrolls, fades about a second after it stops (kept while hovered or dragged), can be dragged or clicked to page, and is gone when the box fits, the same in WebKit, WebView2 and WebKitGTK. See `sidebar-overlay-scrollbar.test.cjs`. The window opens on the page the reader last left, as a Mac app comes back as it was (molvqingtai and lc on Discord): `show` keeps the page in localStorage `magpie.lastView` (which the app's webview keeps across restarts, as it keeps `magpie.navLayout`), and `keptView` opens it at start. A page asked for in the address (`?view=`, the tray's links, a reload) wins, Agents too; a kept name that is no page opens the first page, and one gateway mode leaves out opens Providers (`show`). Opening on it scrolls nothing. See `last-page.test.cjs`. | [`assets/app.js`](../../internal/gui/assets/app.js), [`assets/app.css`](../../internal/gui/assets/app.css) |
-| Settings › Privacy | The page opens on what masking kept from vendors (`drawMaskObs` in app.js, fed by `/api/masking/summary`): the values masked over 7 or 30 days with a chart of the days, the kinds, what only the local model found, agents to providers, and the values masked most, each shown only while pointed at (`maskTip`, `/api/masking/value`, which says it is gone after a restart). Its records (`drawMaskRecs`, `/api/masking/records` and `/api/masking/record`) take the page's place, filtered by kind, finder, agent or provider, a day at a time, each opening on the text the vendor saw with its placeholders marked. `maskedBadge`, on the Gateway page's recent calls and the Routing page's log, opens a request's own record (`openMaskRecord`), in amber with the count sent as written when every value found was on Warn. Below the card, *What to mask*, *How it's found* (the rules, always on while anything is masked, and the local model on top of them), *When a value is found* (Mask, Warn or Block per kind, the secret kinds first and the personal ones behind *Show all kinds*, and the *Never mask* list; `renderMaskActions`), *Try it* (`renderMaskTry`, the text as the vendor would get it from `/api/masking/preview`, kept in no storage) and *Sharing*. *Agents' session history* (`renderSessionReads`, read from `/api/sessions/reads` only while the Privacy part is shown) has *Agents found later* and a row per agent found, the choice the Sessions page asks first on a fresh install; see [gateway sessions](gateway-sessions.md#which-agents-sessions-magpie-reads). See [gateway routing](gateway-routing.md) for what is recorded. | [`assets/app.js`](../../internal/gui/assets/app.js), [`masking.go`](../../internal/gui/masking.go) |
+| Settings › Privacy | The page opens on what masking kept from vendors (`drawMaskObs` in app.js, fed by `/api/masking/summary`): the values masked over 7 or 30 days with a chart of the days, the kinds, what only the local model found, agents to providers, and the values masked most, each shown only while pointed at (`maskTip`, `/api/masking/value`, which says it is gone after a restart). Its records (`drawMaskRecs`, `/api/masking/records` and `/api/masking/record`) take the page's place, filtered by kind, finder, agent or provider, a day at a time, each opening on the text the vendor saw with its placeholders marked. `maskedBadge`, on the Gateway page's recent calls and the Routing page's log, opens a request's own record (`openMaskRecord`), in amber with the count sent as written when every value found was on Warn. Below the card, *What to mask*, *How it's found* (the rules, always on while anything is masked, and the local model on top of them), *When a value is found* (Mask, Warn or Block per kind, the secret kinds first and the personal ones behind *Show all kinds*, and the *Never mask* list; `renderMaskActions`), *Try it* (`renderMaskTry`, the text as the vendor would get it from `/api/masking/preview`, kept in no storage) and *Sharing*. *Agents' session history* (`renderSessionReads`, read from `/api/sessions/reads` only while the Privacy part is shown) has *Agents found later* and a row per agent found, the choice the Sessions page asks first on a fresh install; see [gateway sessions](gateway-sessions.md#which-agents-sessions-magpie-reads). While anything is masked, *Instead of masking, send to* (`redactRouteRow` in `plugins/redact/settings.js`, the redact plugin's part of Settings, which wraps `renderRedact` so app.js carries none of it) picks, in the app menu, a model a request masking would change goes to as written: Off by default, and only the models `/api/settings` lists as `maskRouteModels` (every route of theirs unmasked, `gateway.MaskRoute`), saved with `POST /api/settings/redact-route` (`redact-route.test.cjs`). See [gateway routing](gateway-routing.md) for what is recorded. | [`assets/app.js`](../../internal/gui/assets/app.js), [`masking.go`](../../internal/gui/masking.go) |
| Built-in plugins | Plugins › Built-in, the page's first tab (`drawBuiltin` in `plugins.js`, rows by `renderBuiltins` in `app.js`), lists magpie's built-in plugins, each with a name, a line on what it is and a switch: Routing groups, Library, Observability, Sessions, Warm-up and check-in, and the Gateway and Routing pages (Essential). A plugin on with settings has their rows under its row, drawn from its schema by the kernel's `ui.form`, and a change posts `{id, config}`. The Gateway page is drawn by its plugin's own scripts through the window's kernel (`kernel.js`, `assets/plugins/gateway/`); see the [plugin runtime](plugin-runtime.md), "The window's kernel". They are `plugins/*` registered with the [plugin runtime](plugin-runtime.md); `GET /api/plugins/builtin` says each one (`builtinJSON`: on is active, the user's own choice, what of it is in use) and `POST {id, on}` (or `{id, config}`) writes plugins.yml. A page marks what goes with a plugin `data-feature="<id>"`. Off, `featureStyle` hides everything so marked (rows drawn later too), `navHides` leaves its page's tab out of the sidebar and the header, `show` opens Agents for an address kept for it, and `setSetTab`, `partShows` and `findSettings` leave its Settings tab and rows out, of the search too. One in use stays on: its switch is disabled and the row says what uses it, and the POST refuses it off. A plugins.yml that doesn't parse is said above the list and no switch moves. `boot.js` says which are off (`featuresOff`) and their settings (`pluginConfig`) before the first paint, and `settings.builtins` carries the list on every state read. The TUI doesn't follow them. See `builtins.test.cjs`, `builtins_test.go`. | [`builtins.go`](../../internal/gui/builtins.go), [`assets/plugins.js`](../../internal/gui/assets/plugins.js), [`assets/app.js`](../../internal/gui/assets/app.js) |
| No GUI build | `-tags nogui` builds magpie without the GUI: `hasGUI` is false and `runGUI` says to use `magpie tui`. The files of `internal/gui` that need Wails build under `!nogui`. | [`gui_off.go`](../../internal/cli/gui_off.go) |
| The terminal quitting | `magpie tui` that served the gateway with an agent connected hands the gateway on as it quits, on Linux where nothing opens magpie at login (a WSL distro, a server over SSH; zaidu259434 on X): `keepGateway` starts magpie's service when it is installed, installs it (`daemon.Install`) when the user never removed it (`daemon.Removed`, a mark `magpie daemon uninstall` leaves), and otherwise starts `magpie serve` in its own session (`serveInBackground`, log in magpie's cache). Elsewhere, with nothing connected, or where magpie opens at login, it only says the gateway stopped. | [`keep.go`](../../internal/tui/keep.go) |
diff --git a/internal/gateway/codex_backend.go b/internal/gateway/codex_backend.go
index 61e40bbda..e5b5bcd5a 100644
--- a/internal/gateway/codex_backend.go
+++ b/internal/gateway/codex_backend.go
@@ -256,6 +256,18 @@ func (s *Server) codexBackend(w http.ResponseWriter, r *http.Request) {
s.serveAgent(w, r, provider.Responses, body)
return
}
+ // a turn on Codex's own sign-in that masking would change goes
+ // to the model the user set for that, as written, as one through
+ // magpie's accounts does in serve (maskRouteFor)
+ if to := codexMaskRoute(r, rest, body); to != "" {
+ body, compact := codexInputAs(withModel(body, to), true, s.nativeCompactionFor(to))
+ if compact {
+ s.codexCompact(w, r, body)
+ return
+ }
+ s.serveAgent(w, r, provider.Responses, body)
+ return
+ }
body, _ = codexInput(body, false)
if r.Header.Get(AccountHeader) != "" {
// relayed as it came, it would go to Codex's own sign-in only
diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go
index c9bc09be0..4476e3bc4 100644
--- a/internal/gateway/gateway.go
+++ b/internal/gateway/gateway.go
@@ -1446,13 +1446,19 @@ func (s *Server) countTokens(w http.ResponseWriter, r *http.Request) {
// and the one as written where generation sends that, masked only
// then (#3973)
if _, ms, isGroup := provider.FindGroup(id); !ok || !unredactedRoute(p, isGroup, ms) {
- var unmask func()
- w, body, unmask, err = redacted(r.Context(), w, body)
- defer unmask()
+ rw, b, done, hits, err := redactedHits(r.Context(), w, body)
if err != nil {
writeError(w, provider.Anthropic, maskStatus(err), err.Error())
return
}
+ // counted where generation goes: the model set for a request
+ // masking would change, as written (maskRouteFor)
+ if to := maskRouteFor(agentTurn(r), hits); to != "" {
+ p, model, ok = provider.Resolve(to)
+ } else {
+ w, body = rw, b
+ defer done()
+ }
}
if level := desktopSubagentEffort(r, agentOf(r), body, pick); level != "" && ok && !provider.Reasonless(p.ID, model) {
body = withFixedEffort(provider.Anthropic, body, fitLevel(level, p.Efforts(model)))
@@ -2031,8 +2037,22 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
writeError(w, from, call.Status, err.Error())
return
}
- capture.ResponseWriter, body, unmask, maskedHits = rw, b, done, hits
- } else if usage.ObservedBodies() != usage.NoBodies {
+ if to := maskRouteFor(r, hits); to != "" {
+ // masking would change it, and the user set a model of
+ // their own for that (Settings › Privacy, jessyoungyu on
+ // X): it goes there as written, the masked copy and its
+ // writer let go unused, and on from there only where
+ // unmasked requests may go; its failure is the answer
+ log.Printf("gateway: %d value(s) masking would change, sent as written to %s (Settings › Privacy)", hits.n, to)
+ p, model, _ = provider.Resolve(to)
+ g, ms, isGroup = provider.FindGroup(to)
+ g = g.Live()
+ sentWhole = true
+ } else {
+ capture.ResponseWriter, body, unmask, maskedHits = rw, b, done, hits
+ }
+ }
+ if sentWhole && usage.ObservedBodies() != usage.NoBodies {
call.maskIn = &laterMasked{ctx: context.WithoutCancel(r.Context()), plain: body}
}
maskDone = true
diff --git a/internal/gateway/mask_route_test.go b/internal/gateway/mask_route_test.go
new file mode 100644
index 000000000..87a17b12b
--- /dev/null
+++ b/internal/gateway/mask_route_test.go
@@ -0,0 +1,218 @@
+package gateway
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "github.com/yetone/magpie/internal/access"
+ "github.com/yetone/magpie/internal/provider"
+ "github.com/yetone/magpie/internal/settings"
+)
+
+// jessyoungyu on X: 「{{百度}}这家公司怎么样」 can't be answered by a model
+// that gets the placeholder. With a model set for requests masking would
+// change (settings.RedactRouteTo), one whose provider the user set to go
+// unmasked on this machine or the network, such a request goes there as
+// written and never to the vendor; one masking leaves alone goes where it
+// was asked; unset, as by default, it is masked as before.
+
+const routeWord = "百度"
+
+// maskRouteSetup is the vendor ("fake", Messages, masked) and a local
+// model ("near/qwen", Chat, set to go unmasked); routeTo is the setting.
+func maskRouteSetup(t *testing.T, routeTo string, near func(*provider.Provider)) (vendor, local *fake) {
+ t.Helper()
+ vendor = &fake{ctype: "application/json", reply: `{"id":"msg_1","type":"message","role":"assistant","model":"m1","content":[{"type":"text","text":"vendor"}],"stop_reason":"end_turn","usage":{"input_tokens":3,"output_tokens":1}}`}
+ setup(t, provider.Anthropic, vendor)
+ // a Chat provider is asked to stream whether the agent streams or not
+ local = &fake{reply: sse(`data: {"id":"c1","object":"chat.completion.chunk","model":"qwen","choices":[{"index":0,"delta":{"role":"assistant","content":"local"}}]}`,
+ `data: {"id":"c1","object":"chat.completion.chunk","model":"qwen","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":1}}`,
+ `data: [DONE]`)}
+ up := httptest.NewServer(local)
+ t.Cleanup(up.Close)
+ p := provider.Provider{ID: "near", Name: "Near", Key: "k", Models: []string{"qwen"}, Chat: up.URL + "/v1", Unredacted: true}
+ if near != nil {
+ near(&p)
+ }
+ if err := provider.Save(p); err != nil {
+ t.Fatal(err)
+ }
+ if err := settings.Save(settings.Settings{Redact: true, RedactWords: []string{routeWord}, RedactRouteTo: routeTo}); err != nil {
+ t.Fatal(err)
+ }
+ return vendor, local
+}
+
+func messagesBody(text string, stream bool) string {
+ s := ""
+ if stream {
+ s = `"stream":true,`
+ }
+ return `{"model":"fake/m1",` + s + `"max_tokens":10,"messages":[{"role":"user","content":"` + text + `"}]}`
+}
+
+func TestMaskedRequestGoesToTheModelSet(t *testing.T) {
+ ask := routeWord + "这家公司怎么样"
+ t.Run("unset, as by default: masked to the vendor", func(t *testing.T) {
+ vendor, local := maskRouteSetup(t, "", nil)
+ if code, reply := post(t, "/v1/messages", messagesBody(ask, false)); code != 200 || !strings.Contains(reply, "vendor") {
+ t.Fatalf("%d %s", code, reply)
+ }
+ if strings.Contains(string(vendor.got), routeWord) || !strings.Contains(string(vendor.got), "{{TERM_") || local.calls != 0 {
+ t.Fatalf("vendor got %s, local asked %d times", vendor.got, local.calls)
+ }
+ })
+ t.Run("set: as written to the local model, nothing to the vendor", func(t *testing.T) {
+ vendor, local := maskRouteSetup(t, "near/qwen", nil)
+ code, reply := post(t, "/v1/messages", messagesBody(ask, false))
+ if code != 200 || !strings.Contains(reply, "local") {
+ t.Fatalf("%d %s", code, reply)
+ }
+ if vendor.calls != 0 || !strings.Contains(string(local.got), ask) || strings.Contains(string(local.got), "{{") || !strings.Contains(string(local.got), `"qwen"`) {
+ t.Fatalf("vendor asked %d times; local got %s", vendor.calls, local.got)
+ }
+ })
+ t.Run("set, streamed", func(t *testing.T) {
+ vendor, local := maskRouteSetup(t, "near/qwen", nil)
+ code, reply := post(t, "/v1/messages", messagesBody(ask, true))
+ if code != 200 || !strings.Contains(reply, "local") || !strings.Contains(reply, "message_stop") {
+ t.Fatalf("%d %s", code, reply)
+ }
+ if vendor.calls != 0 || !strings.Contains(string(local.got), ask) {
+ t.Fatalf("vendor asked %d times; local got %s", vendor.calls, local.got)
+ }
+ })
+ t.Run("set, nothing to mask: to the model asked for", func(t *testing.T) {
+ vendor, local := maskRouteSetup(t, "near/qwen", nil)
+ if code, reply := post(t, "/v1/messages", messagesBody("hello there", false)); code != 200 || !strings.Contains(reply, "vendor") {
+ t.Fatalf("%d %s", code, reply)
+ }
+ if local.calls != 0 || vendor.calls != 1 {
+ t.Fatalf("local asked %d, vendor %d", local.calls, vendor.calls)
+ }
+ })
+ t.Run("set, the local model fails: the request fails, nothing to the vendor", func(t *testing.T) {
+ vendor, local := maskRouteSetup(t, "near/qwen", nil)
+ local.code, local.ctype, local.reply = 500, "application/json", `{"error":{"message":"down"}}`
+ if code, reply := post(t, "/v1/messages", messagesBody(ask, false)); code < 400 {
+ t.Fatalf("%d %s", code, reply)
+ }
+ if vendor.calls != 0 || local.calls == 0 {
+ t.Fatalf("vendor asked %d, local %d", vendor.calls, local.calls)
+ }
+ })
+ t.Run("set, but it falls back to a vendor: masked as before", func(t *testing.T) {
+ vendor, local := maskRouteSetup(t, "near/qwen", func(p *provider.Provider) { p.Fallback = []string{"fake/m1"} })
+ if code, reply := post(t, "/v1/messages", messagesBody(ask, false)); code != 200 || !strings.Contains(reply, "vendor") {
+ t.Fatalf("%d %s", code, reply)
+ }
+ if local.calls != 0 || strings.Contains(string(vendor.got), routeWord) {
+ t.Fatalf("local asked %d; vendor got %s", local.calls, vendor.got)
+ }
+ })
+ t.Run("set, but no longer set to go unmasked: masked as before", func(t *testing.T) {
+ vendor, local := maskRouteSetup(t, "near/qwen", func(p *provider.Provider) { p.Unredacted = false })
+ if code, reply := post(t, "/v1/messages", messagesBody(ask, false)); code != 200 || !strings.Contains(reply, "vendor") {
+ t.Fatalf("%d %s", code, reply)
+ }
+ if local.calls != 0 || strings.Contains(string(vendor.got), routeWord) {
+ t.Fatalf("local asked %d; vendor got %s", local.calls, vendor.got)
+ }
+ })
+ t.Run("set: counted on the local model, as written", func(t *testing.T) {
+ vendor, local := maskRouteSetup(t, "near/qwen", nil)
+ code, reply := post(t, "/v1/messages/count_tokens", `{"model":"fake/m1","messages":[{"role":"user","content":"`+ask+`"}]}`)
+ if code != 200 {
+ t.Fatalf("%d %s", code, reply)
+ }
+ if vendor.calls != 0 {
+ t.Fatalf("counted on the vendor: %s", vendor.got)
+ }
+ if !strings.Contains(string(local.got), ask) && local.calls != 0 {
+ t.Fatalf("local counted %s", local.got)
+ }
+ })
+}
+
+// A turn of Codex's on one of its own models, relayed on its ChatGPT
+// sign-in, goes to the model set too, and the ChatGPT backend gets none.
+func TestCodexSigninTurnGoesToTheModelSet(t *testing.T) {
+ for _, routeTo := range []string{"", "near/qwen"} {
+ t.Run("route "+routeTo, func(t *testing.T) {
+ _, local := maskRouteSetup(t, routeTo, nil)
+ chatgpt := &fake{reply: sse(`data: {"type":"response.created","response":{"id":"r1","model":"gpt-5.5"}}`,
+ `data: {"type":"response.output_text.delta","delta":"chatgpt"}`,
+ `data: {"type":"response.completed","response":{"id":"r1","usage":{"input_tokens":7,"output_tokens":1}}}`)}
+ up := httptest.NewServer(chatgpt)
+ t.Cleanup(up.Close)
+ was := provider.CodexBase
+ provider.CodexBase = up.URL + "/backend-api/codex"
+ t.Cleanup(func() { provider.CodexBase = was })
+ code, reply := codexPost(t, `{"model":"gpt-5.5","stream":true,"store":false,"input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"`+routeWord+`这家公司怎么样"}]}]}`)
+ if code != http.StatusOK {
+ t.Fatalf("%d %s", code, reply)
+ }
+ if routeTo == "" {
+ if local.calls != 0 || chatgpt.calls != 1 || strings.Contains(string(chatgpt.got), routeWord) {
+ t.Fatalf("local %d; ChatGPT got %s", local.calls, chatgpt.got)
+ }
+ return
+ }
+ if chatgpt.calls != 0 || !strings.Contains(string(local.got), routeWord) || !strings.Contains(reply, "local") {
+ t.Fatalf("ChatGPT asked %d; local got %s; answered %s", chatgpt.calls, local.got, reply)
+ }
+ })
+ }
+}
+
+// What the GUI lists and the setting takes: a model whose every route goes
+// unmasked, not a vendor's, nor one that falls back to one.
+func TestMaskRouteTakesOnlyUnmaskedRoutes(t *testing.T) {
+ maskRouteSetup(t, "", nil)
+ if !MaskRoute("near/qwen") {
+ t.Fatal("near/qwen goes unmasked")
+ }
+ for _, id := range []string{"", "fake/m1", "nowhere/x"} {
+ if MaskRoute(id) {
+ t.Fatalf("%q taken", id)
+ }
+ }
+ maskRouteSetup(t, "", func(p *provider.Provider) { p.Fallback = []string{"fake/m1"} })
+ if MaskRoute("near/qwen") {
+ t.Fatal("one falling back to a vendor taken")
+ }
+}
+
+// Only an agent's own turn goes to the model set: magpie's own asks made
+// on its behalf (a title, a search stand-in, a picture described) are
+// masked as before, and so is a turn on a gateway key the model set is
+// not one of the key's models.
+func TestMaskRouteOnlyForTheAgentsTurn(t *testing.T) {
+ maskRouteSetup(t, "near/qwen", nil)
+ hit := maskHits{n: 1}
+ r := httptest.NewRequest("POST", "/v1/messages", nil)
+ if got := maskRouteFor(r, hit); got != "" {
+ t.Fatalf("not an agent's turn: %q", got)
+ }
+ turn := agentTurn(r)
+ if got := maskRouteFor(turn, hit); got != "near/qwen" {
+ t.Fatalf("the agent's turn: %q", got)
+ }
+ if got := maskRouteFor(turn, maskHits{}); got != "" {
+ t.Fatalf("nothing masked: %q", got)
+ }
+ if got := maskRouteFor(turn.WithContext(ownAsk(turn.Context())), hit); got != "" {
+ t.Fatalf("magpie's own ask: %q", got)
+ }
+ keyed := func(models ...string) *http.Request {
+ return turn.WithContext(access.WithIdentity(turn.Context(), access.Identity{KeyID: "k1", Models: models}))
+ }
+ if got := maskRouteFor(keyed("fake/*"), hit); got != "" {
+ t.Fatalf("a key without it: %q", got)
+ }
+ if got := maskRouteFor(keyed("fake/*", "near/*"), hit); got != "near/qwen" {
+ t.Fatalf("a key with it: %q", got)
+ }
+}
diff --git a/internal/gateway/middleware.go b/internal/gateway/middleware.go
index dc99ad997..76714ab7b 100644
--- a/internal/gateway/middleware.go
+++ b/internal/gateway/middleware.go
@@ -21,6 +21,9 @@ import (
// magpie's to read.
func (s *Server) serveAgent(w http.ResponseWriter, r *http.Request, from provider.Protocol, body []byte) {
withGatewaySession(w, r)
+ // the agent's own turn, which the user's model for requests masking
+ // would change is for (maskRouteFor)
+ r = agentTurn(r)
// a Responses request naming the response it follows is given the
// conversation whole before anything reads it (prev_response.go)
w, r, body, kept := previousResponse(w, r, from, body)
@@ -98,9 +101,11 @@ func triedOn(ctx context.Context, p provider.Provider, model string, body []byte
// onProvider on one runtime, and the gateway died of "concurrent map read
// and map write" (#3695). Nor does the hooks' ctx describe them: a
// description's body is Chat and a search's Anthropic, whatever
-// ctx.protocol names, and ctx.model and ctx.stream are the agent's.
+// ctx.protocol names, and ctx.model and ctx.stream are the agent's. Nor
+// is it the agent's turn, which a model set for requests masking would
+// change takes (maskRouteFor): it is masked as magpie's own asks are.
func ownAsk(ctx context.Context) context.Context {
- return context.WithValue(ctx, triedKey{}, []gwmw.Tried(nil))
+ return context.WithValue(context.WithValue(ctx, triedKey{}, []gwmw.Tried(nil)), agentTurnKey{}, false)
}
// middlewareWanted: plugins.json has a JS middleware on, so plugins/middleware
diff --git a/internal/gateway/redact.go b/internal/gateway/redact.go
index 5115e16d6..0f38c1f82 100644
--- a/internal/gateway/redact.go
+++ b/internal/gateway/redact.go
@@ -5,6 +5,8 @@ import (
"errors"
"fmt"
"net/http"
+ "net/http/httptest"
+ "strings"
"sync"
"time"
@@ -312,3 +314,80 @@ func unredactedRoute(p provider.Provider, isGroup bool, ms []provider.Member) bo
}
return true
}
+
+// agentTurnKey marks a request an agent sent (serveAgent, count_tokens,
+// a Codex turn on its own sign-in), the ones the user's route for masked
+// requests is for (maskRouteFor); magpie's own asks made on an agent's
+// context take it off (ownAsk).
+type agentTurnKey struct{}
+
+func agentTurn(r *http.Request) *http.Request {
+ return r.WithContext(context.WithValue(r.Context(), agentTurnKey{}, true))
+}
+
+func isAgentTurn(r *http.Request) bool {
+ on, _ := r.Context().Value(agentTurnKey{}).(bool)
+ return on
+}
+
+// MaskRoute says id may be the model an agent's request goes to as
+// written when masking would change it (settings.RedactRouteTo): it
+// resolves, is on, holds a conversation, and every place it can go is
+// one the user set to go unmasked (unredactedRoute), its fallbacks and a
+// group's members too, so nothing of it reaches a vendor.
+func MaskRoute(id string) bool {
+ id = strings.TrimSpace(id)
+ if id == "" {
+ return false
+ }
+ if _, off := idOff(id); off {
+ return false
+ }
+ p, model, ok := provider.Resolve(id)
+ if !ok || p.DecidesModel(model) {
+ return false
+ }
+ _, ms, isGroup := provider.FindGroup(id)
+ return unredactedRoute(p, isGroup, ms)
+}
+
+// maskRouteFor is the model the user set for an agent's request that
+// masking changed (h counts a value masked), the request to go there as
+// written instead of masked (jessyoungyu on X); "" leaves it masked: none
+// set, nothing masked (a value only warned of goes as written anyway),
+// magpie's own ask, or a model that can't take it whole now (MaskRoute)
+// or that the calling key may not use. Its failures are its own: what it
+// can go on to is only where the user set requests to go unmasked, and
+// the request is never handed back to be masked and sent to a vendor.
+func maskRouteFor(r *http.Request, h maskHits) string {
+ if h.n == 0 || !isAgentTurn(r) {
+ return ""
+ }
+ to := strings.TrimSpace(settings.Shared().RedactRouteTo)
+ if !MaskRoute(to) {
+ return ""
+ }
+ if who, held := keyHolds(r); held {
+ p, model, _ := provider.Resolve(to)
+ g, ms, isGroup := provider.FindGroup(to)
+ if isGroup && !groupAllowed(who, g, ms) || !isGroup && !modelAllowed(who, p, model) {
+ return ""
+ }
+ }
+ return to
+}
+
+// codexMaskRoute is maskRouteFor for a turn of Codex's on its own ChatGPT
+// sign-in (codexUpstreamOn masks it there): masked here only when a
+// model is set that can take it, so nobody else's turn is masked twice.
+// A turn held back (redact.Block) is left to be held back there.
+func codexMaskRoute(r *http.Request, rest string, body []byte) string {
+ if rest != "/responses" || !MaskRoute(settings.Shared().RedactRouteTo) {
+ return ""
+ }
+ _, _, _, hits, err := redactedHits(r.Context(), httptest.NewRecorder(), body)
+ if err != nil {
+ return ""
+ }
+ return maskRouteFor(agentTurn(r), hits)
+}
diff --git a/internal/gui/api.go b/internal/gui/api.go
index 46de8c878..339435c36 100644
--- a/internal/gui/api.go
+++ b/internal/gui/api.go
@@ -439,6 +439,10 @@ type settingsJSON struct {
VisionEffortNow string `json:"visionEffortNow,omitempty"`
// the models Codex's thread titles may be sent to (CodexTitles, #705)
TitleModels []modelRef `json:"titleModels"`
+ // the models a request masking would change may go to as written
+ // (RedactRouteTo): those whose every route is set to go unmasked
+ // (gateway.MaskRoute)
+ MaskRouteModels []modelRef `json:"maskRouteModels"`
// what each of the disabled models (#1867) turns off now, by pattern
DisabledHits map[string][]string `json:"disabledHits,omitempty"`
// the model magpie's generate_image tool draws with when ImageGen
@@ -707,7 +711,9 @@ func settingsState() settingsJSON {
s.VisionNotMarked = append(s.VisionNotMarked, cmp.Or(e.Provider.Name, e.Provider.ID))
}
}
- s.TitleModels = []modelRef{}
+ s.TitleModels, s.MaskRouteModels = []modelRef{}, []modelRef{}
+ // only a provider set to go unmasked can take one (most have none)
+ unmaskedAny := slices.ContainsFunc(provider.All(), provider.Provider.SkipsRedaction)
for _, pat := range s.DisabledModels {
if s.DisabledHits == nil {
s.DisabledHits = map[string][]string{}
@@ -721,6 +727,9 @@ func settingsState() settingsJSON {
m.Provider, m.PName = "", e.Group
}
s.TitleModels = append(s.TitleModels, m)
+ if (e.Group != "" || e.Provider.SkipsRedaction()) && unmaskedAny && gateway.MaskRoute(e.ID) {
+ s.MaskRouteModels = append(s.MaskRouteModels, m)
+ }
}
}
searchState(&s)
@@ -1423,6 +1432,8 @@ func Handler(w Windows, gw *gateway.Server) http.Handler {
// in their Agents rows
in.CodexSubagentModel = cur.CodexSubagentModel
in.DesktopSubagentModel = cur.DesktopSubagentModel
+ // and the model for requests masking would change (redact-route)
+ in.RedactRouteTo = cur.RedactRouteTo
in.DesktopSubagentEffort = cur.DesktopSubagentEffort
in.ChinaMirror = cur.ChinaMirror // the Plugins page's, set on its own
// which Codex accounts spend a reset by themselves, set on the Usage card
@@ -1808,6 +1819,28 @@ func Handler(w Windows, gw *gateway.Server) http.Handler {
}
writeJSON(rw, settingsState())
})
+ // the model an agent's request masking would change goes to as
+ // written (jessyoungyu on X): "" masks it as before; else a model whose
+ // every route the user set to go unmasked (gateway.MaskRoute)
+ mux.HandleFunc("POST /api/settings/redact-route", func(rw http.ResponseWriter, r *http.Request) {
+ var in struct{ Model string }
+ if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
+ fail(rw, err)
+ return
+ }
+ v := strings.TrimSpace(in.Model)
+ if v != "" && !gateway.MaskRoute(v) {
+ fail(rw, fmt.Errorf("%s can't take requests unmasked: pick a model on this computer or your network whose provider is set to Send requests unmasked, with no fallback elsewhere", v))
+ return
+ }
+ s := settings.Load()
+ s.RedactRouteTo = v
+ if err := settings.Save(s); err != nil {
+ fail(rw, err)
+ return
+ }
+ writeJSON(rw, settingsState())
+ })
// the model Codex's auto-review runs on (#938): "" as Codex picks it,
// or a model's id, named in every entry of Codex's list
mux.HandleFunc("POST /api/settings/codex-auto-review", func(rw http.ResponseWriter, r *http.Request) {
diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.js
index b6689b5c3..a1a8da347 100644
--- a/internal/gui/assets/i18n.js
+++ b/internal/gui/assets/i18n.js
@@ -975,6 +975,11 @@ const I18N = {
"Privacy": "隐私",
"Mask secrets": "脱敏密钥",
"Keep a key's form": "保留密钥格式",
+ "Model for requests masking would change": "命中脱敏时改用的模型",
+ "Masked and sent to the model asked for, as before": "脱敏后照常发给所请求的模型",
+ "A request masking would change goes as written to this model, which you set to take requests unmasked, so it answers about the real names. If it fails, the request fails: it is never masked and sent elsewhere": "会被脱敏改动的请求原样发给这个模型(你已把它设为请求不脱敏),它看得到真实名称来回答。它失败时请求就失败:绝不会脱敏后改发别处",
+ "Only a model you set to take requests unmasked can be picked: in the editor of a provider on this computer or your network, turn on Send requests unmasked": "只能选设为请求不脱敏的模型:在本机或局域网上的供应商编辑页里打开「请求不脱敏」",
+ "Instead of masking, send to": "命中脱敏时改用",
"A key goes as a made-up one of the same form (sk_… stays sk_…), not a placeholder, and comes back as it was": "密钥以同样格式的虚构密钥发出(sk_… 仍是 sk_…),而非占位符,回来时还原",
"Local network": "局域网",
"Public address": "公网地址",
@@ -5098,6 +5103,11 @@ const I18N = {
"Privacy": "隱私",
"Mask secrets": "脫敏金鑰",
"Keep a key's form": "保留金鑰格式",
+ "Model for requests masking would change": "命中脫敏時改用的模型",
+ "Masked and sent to the model asked for, as before": "脫敏後照常送給所請求的模型",
+ "A request masking would change goes as written to this model, which you set to take requests unmasked, so it answers about the real names. If it fails, the request fails: it is never masked and sent elsewhere": "會被脫敏改動的請求原樣送給這個模型(你已把它設為請求不脫敏),它看得到真實名稱來回答。它失敗時請求就失敗:絕不會脫敏後改送別處",
+ "Only a model you set to take requests unmasked can be picked: in the editor of a provider on this computer or your network, turn on Send requests unmasked": "只能選設為請求不脫敏的模型:在本機或區域網路上的供應商編輯頁裡開啟「請求不脫敏」",
+ "Instead of masking, send to": "命中脫敏時改用",
"A key goes as a made-up one of the same form (sk_… stays sk_…), not a placeholder, and comes back as it was": "金鑰以同樣格式的虛構金鑰送出(sk_… 仍是 sk_…),而非佔位符,回來時還原",
"Local network": "區域網",
"Public address": "公開位址",
@@ -9193,6 +9203,11 @@ const I18N = {
"Privacy": "プライバシー",
"Mask secrets": "シークレットをマスク",
"Keep a key's form": "キーの形式を保つ",
+ "Model for requests masking would change": "マスク対象を含むリクエストのモデル",
+ "Masked and sent to the model asked for, as before": "マスクして、指定されたモデルにこれまでどおり送る",
+ "A request masking would change goes as written to this model, which you set to take requests unmasked, so it answers about the real names. If it fails, the request fails: it is never masked and sent elsewhere": "マスクで変わるリクエストを、リクエストをマスクしない設定にしたこのモデルへそのまま送るので、実際の名前について答えられます。失敗したらリクエストは失敗し、マスクして他へ送ることはありません",
+ "Only a model you set to take requests unmasked can be picked: in the editor of a provider on this computer or your network, turn on Send requests unmasked": "リクエストをマスクしない設定のモデルだけ選べます:このコンピュータかネットワーク上のプロバイダの編集画面で「リクエストをマスクしない」をオンにしてください",
+ "Instead of masking, send to": "マスクせずに送る先",
"A key goes as a made-up one of the same form (sk_… stays sk_…), not a placeholder, and comes back as it was": "キーはプレースホルダーではなく同じ形式の架空のキー(sk_… は sk_… のまま)として送られ、戻るときに元に戻ります",
"Local network": "ローカルネットワーク",
"Public address": "公開アドレス",
@@ -13302,6 +13317,11 @@ const I18N = {
"Privacy": "Datenschutz",
"Mask secrets": "Geheimnisse maskieren",
"Keep a key's form": "Schlüsselformat beibehalten",
+ "Model for requests masking would change": "Modell für Anfragen, die Maskieren ändern würde",
+ "Masked and sent to the model asked for, as before": "Maskiert an das angefragte Modell, wie bisher",
+ "A request masking would change goes as written to this model, which you set to take requests unmasked, so it answers about the real names. If it fails, the request fails: it is never masked and sent elsewhere": "Eine Anfrage, die Maskieren ändern würde, geht unverändert an dieses Modell, das du Anfragen unmaskiert nehmen lässt, und es antwortet zu den echten Namen. Scheitert es, scheitert die Anfrage: Sie wird nie maskiert anderswohin gesendet",
+ "Only a model you set to take requests unmasked can be picked: in the editor of a provider on this computer or your network, turn on Send requests unmasked": "Nur ein Modell, das Anfragen unmaskiert nimmt, ist wählbar: Schalte im Editor eines Anbieters auf diesem Computer oder in deinem Netzwerk „Anfragen unmaskiert senden“ ein",
+ "Instead of masking, send to": "Statt zu maskieren senden an",
"A key goes as a made-up one of the same form (sk_… stays sk_…), not a placeholder, and comes back as it was": "Ein Schlüssel geht als erfundener Schlüssel derselben Form (sk_… bleibt sk_…) statt als Platzhalter und kommt unverändert zurück",
"Local network": "Lokales Netzwerk",
"Public address": "Öffentliche Adresse",
diff --git a/internal/gui/assets/index.html b/internal/gui/assets/index.html
index f2f9978c3..f9e7bb0f6 100644
--- a/internal/gui/assets/index.html
+++ b/internal/gui/assets/index.html
@@ -541,6 +541,7 @@
<script src="plugins/gateway/settings.js"></script>
<script src="plugins/search/settings.js"></script>
<script src="plugins/sessions/settings.js"></script>
+<script src="plugins/redact/settings.js"></script>
<script src="start.js"></script>
</body>
</html>
diff --git a/internal/gui/assets/plugins/redact/settings.js b/internal/gui/assets/plugins/redact/settings.js
new file mode 100644
index 000000000..fe31a3999
--- /dev/null
+++ b/internal/gui/assets/plugins/redact/settings.js
@@ -0,0 +1,47 @@
+// Settings › Privacy's Instead of masking, send to (the redact plugin's part
+// of Settings). Run after app.js, whose helpers it shares (el, icon,
+// openPicker, modelOpt, prefWrite, api), and before start.js draws
+// Settings. It wraps renderRedact, so app.js carries none of it: the row
+// shows while anything is masked, after the masked-words rows.
+
+// redactRouteRow: where a request masking would change goes instead
+// (redactRouteTo, jessyoungyu on X): Off masks it as before; a model whose
+// provider the user set to Send requests unmasked (maskRouteModels, on
+// this computer or the network) gets it as written, so 「百度这家公司怎么样」
+// is answered about Baidu, not a placeholder. Picked in the app's menu,
+// saved on its own (settings/redact-route).
+function redactRouteRow(s) {
+ const models = s.maskRouteModels || [];
+ const v = s.redactRouteTo || "";
+ const b = el("button", "rt-cond on");
+ b.type = "button";
+ b.setAttribute("aria-label", t("Model for requests masking would change"));
+ const m = models.find((x) => x.id === v);
+ if (v) b.append(icon(m?.icon || "generic"), el("span", "", m ? `${m.name || m.id} · ${m.providerName}` : v));
+ else b.append(el("span", "", t("Off")));
+ b.disabled = !models.length && !v;
+ b.onclick = (ev) => openPicker({ id: "", name: "", fields: [] }, { key: "redactRouteTo", label: "model", value: v, options: [
+ { value: "", label: t("Off"), note: t("Masked and sent to the model asked for, as before"), icon: "generic", reset: true },
+ ...models.map(modelOpt)],
+ onPick: (id) => {
+ if (id === v) return;
+ prefWrite(api("settings/redact-route", { model: id }));
+ } }, b, ev);
+ const sub = models.length || v
+ ? t("A request masking would change goes as written to this model, which you set to take requests unmasked, so it answers about the real names. If it fails, the request fails: it is never masked and sent elsewhere")
+ : t("Only a model you set to take requests unmasked can be picked: in the editor of a provider on this computer or your network, turn on Send requests unmasked");
+ const r = el("div", "row pref"), who = el("div", "who"), val = el("div", "val");
+ who.append(el("div", "name", t("Instead of masking, send to")), el("div", "sub", sub));
+ val.append(b);
+ r.append(who, val);
+ r.id = "redactRouteRow";
+ const after = $("#redactWordsWholeRow");
+ if (after) after.after(r); else $("#redactList").append(r);
+}
+{
+ const draw = renderRedact;
+ renderRedact = function (s, keep) {
+ draw(s, keep);
+ if (s.redact || s.redactPersonal || (s.redactWords || []).length) redactRouteRow(s);
+ };
+}
diff --git a/internal/gui/redact_route_test.go b/internal/gui/redact_route_test.go
new file mode 100644
index 000000000..99cce9b02
--- /dev/null
+++ b/internal/gui/redact_route_test.go
@@ -0,0 +1,82 @@
+package gui
+
+import (
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/yetone/magpie/internal/provider"
+ "github.com/yetone/magpie/internal/settings"
+)
+
+// The model requests masking would change go to instead (jessyoungyu on
+// X) is set on its own: Settings › Privacy lists only models whose every
+// route goes unmasked, on this computer or the network; one that would
+// reach a vendor is refused with the reason; Off clears it; and the
+// general save leaves it as it was.
+func TestRedactRouteSetting(t *testing.T) {
+ h := t.TempDir()
+ t.Setenv("HOME", h)
+ t.Setenv("USERPROFILE", h)
+ t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
+ t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
+ for _, p := range []provider.Provider{
+ {ID: "cloud", Name: "Cloud", Key: "k", Models: []string{"big"}, Chat: "https://api.example.com/v1"},
+ {ID: "near", Name: "Near", Key: "k", Models: []string{"qwen"}, Chat: "http://127.0.0.1:11434/v1", Unredacted: true},
+ {ID: "leaky", Name: "Leaky", Key: "k", Models: []string{"m"}, Chat: "http://127.0.0.1:11435/v1", Unredacted: true, Fallback: []string{"cloud/big"}},
+ } {
+ if err := provider.Save(p); err != nil {
+ t.Fatal(err)
+ }
+ }
+ srv := Handler(nil, nil)
+ do := func(method, path, body string) *httptest.ResponseRecorder {
+ rec := httptest.NewRecorder()
+ srv.ServeHTTP(rec, httptest.NewRequest(method, path, strings.NewReader(body)))
+ return rec
+ }
+ var st struct {
+ MaskRouteModels []struct{ ID string } `json:"maskRouteModels"`
+ RedactRouteTo string `json:"redactRouteTo"`
+ }
+ read := func() {
+ t.Helper()
+ st.MaskRouteModels, st.RedactRouteTo = nil, ""
+ rec := do("GET", "/api/settings", "")
+ if err := json.Unmarshal(rec.Body.Bytes(), &st); err != nil {
+ t.Fatalf("%v: %s", err, rec.Body)
+ }
+ }
+ read()
+ if len(st.MaskRouteModels) != 1 || st.MaskRouteModels[0].ID != "near/qwen" || st.RedactRouteTo != "" {
+ t.Fatalf("listed %+v, set %q", st.MaskRouteModels, st.RedactRouteTo)
+ }
+ for _, id := range []string{"cloud/big", "leaky/m", "nowhere/x"} {
+ if rec := do("POST", "/api/settings/redact-route", `{"model":"`+id+`"}`); rec.Code == http.StatusOK || !strings.Contains(rec.Body.String(), "unmasked") {
+ t.Fatalf("%s taken: %d %s", id, rec.Code, rec.Body)
+ }
+ }
+ if settings.Load().RedactRouteTo != "" {
+ t.Fatal("a refused model was saved")
+ }
+ if rec := do("POST", "/api/settings/redact-route", `{"model":"near/qwen"}`); rec.Code != http.StatusOK || settings.Load().RedactRouteTo != "near/qwen" {
+ t.Fatalf("near/qwen: %d %s", rec.Code, rec.Body)
+ }
+ // the general save doesn't carry it, and keeps it
+ if rec := do("POST", "/api/settings", `{"redact":true}`); rec.Code != http.StatusOK || settings.Load().RedactRouteTo != "near/qwen" {
+ t.Fatalf("general save: %d %s, now %q", rec.Code, rec.Body, settings.Load().RedactRouteTo)
+ }
+ read()
+ if st.RedactRouteTo != "near/qwen" {
+ t.Fatalf("read back %q", st.RedactRouteTo)
+ }
+ if rec := do("POST", "/api/settings/redact-route", `{"model":""}`); rec.Code != http.StatusOK || settings.Load().RedactRouteTo != "" {
+ t.Fatalf("off: %d %s", rec.Code, rec.Body)
+ }
+ if err := settings.Save(settings.Settings{RedactRouteTo: "qwen"}); err == nil {
+ t.Fatal("a bare name saved")
+ }
+}
diff --git a/internal/gui/settings_keep_test.go b/internal/gui/settings_keep_test.go
index 2caae0fd1..dc6207abf 100644
--- a/internal/gui/settings_keep_test.go
+++ b/internal/gui/settings_keep_test.go
@@ -185,6 +185,7 @@ func TestSettingsSaveKeepsWhatItDoesNotSend(t *testing.T) {
RedactModel: true,
RedactModelName: "astrlink-guard",
RedactModelScore: map[string]float64{"astrlink-guard": 0.9},
+ RedactRouteTo: "fake/m1",
}
was.RedactModelOff, was.RedactModelAuto = true, true
was.DesktopSubagentEffort = "high"
diff --git a/internal/gui/tests/redact-route.test.cjs b/internal/gui/tests/redact-route.test.cjs
new file mode 100644
index 000000000..80bff13da
--- /dev/null
+++ b/internal/gui/tests/redact-route.test.cjs
@@ -0,0 +1,132 @@
+// Run with Node's test runner and Playwright on the module path; see README.md.
+// Settings › Privacy's "Instead of masking, send to" (jessyoungyu on X:
+// 「{{百度}}这家公司怎么样」 can't be answered from a placeholder): a row
+// while anything is masked, Off unless the user set it, so every request
+// is masked as before. Its button opens the app menu (never a native
+// select) with Off and only the models listed as able to take requests
+// unmasked; a pick posts settings/redact-route and shows the model, and
+// Off clears it. With none listed it says how to get one. In every
+// language, Chromium and WebKit, in a wide window and a narrow one.
+const assert = require("node:assert/strict");
+const fs = require("node:fs/promises");
+const path = require("node:path");
+const { test } = require("node:test");
+const { chromium, webkit } = require("playwright");
+const { reach, watchSettingsReads, settingsDrawn } = require("./reader.cjs");
+
+const assets = path.resolve(__dirname, "../assets");
+const words = {
+ en: { name: "Instead of masking, send to", off: "Off" },
+ zh: { name: "命中脱敏时改用", off: "关闭" },
+ "zh-TW": { name: "命中脫敏時改用", off: "關閉" },
+ ja: { name: "マスクせずに送る先", off: "オフ" },
+ de: { name: "Statt zu maskieren senden an", off: "Aus" },
+};
+const near = { id: "near/qwen", name: "qwen3", provider: "near", providerName: "Ollama" };
+
+function serve(lang, posted, { redact = true, models = [near] } = {}) {
+ let settings = { lang, theme: "light", redact, redactPersonal: false, redactWords: redact ? ["百度"] : [],
+ titleModels: [{ id: "cloud/big", name: "big", provider: "cloud", providerName: "Cloud" }, ...models], maskRouteModels: models };
+ const state = { agents: [], profiles: [], settings };
+ return async (r) => {
+ const url = new URL(r.request().url());
+ const json = (data) => r.fulfill({ json: data });
+ if (url.pathname === "/boot.js") return r.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` });
+ if (url.pathname === "/wails/runtime.js") return r.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
+ if (url.pathname === "/api/state") return json(state);
+ if (url.pathname === "/api/settings/redact-route") {
+ const { model } = JSON.parse(r.request().postData());
+ posted.push(model);
+ settings = { ...settings, redactRouteTo: model || undefined };
+ return json(settings);
+ }
+ if (url.pathname === "/api/settings") {
+ if (r.request().method() === "POST") settings = { ...settings, ...JSON.parse(r.request().postData()) };
+ return json(settings);
+ }
+ if (url.pathname === "/api/groups") return json({ models: [], groups: [], pools: [] });
+ if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true, url: "http://127.0.0.1:3999" } });
+ if (url.pathname.startsWith("/api/")) return json({});
+ const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
+ const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)];
+ await r.fulfill({ body: await fs.readFile(file), contentType });
+ };
+}
+
+async function open(engine, lang, width, posted, opts) {
+ const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
+ const page = await (await browser.newContext({ viewport: { width, height: 1400 } })).newPage();
+ page.setDefaultTimeout(5000);
+ const errors = [];
+ page.on("pageerror", (e) => errors.push(e.message));
+ await page.route("**/*", serve(lang, posted, opts));
+ await watchSettingsReads(page);
+ await page.goto("http://magpie.test/?view=settings&tab=privacy");
+ await page.locator("#redactList .row").first().waitFor();
+ await settingsDrawn(page);
+ return { browser, page, errors };
+}
+
+const pop = "#pop:not([hidden]) #list li";
+
+for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
+ for (const [lang, w] of Object.entries(words)) {
+ for (const width of [1100, 640]) {
+ test(`${engine} ${lang} ${width}px: a request masking would change goes to the model picked`, async (t) => {
+ const posted = [];
+ const { browser, page, errors } = await open(engine, lang, width, posted);
+ t.after(() => browser.close());
+ const row = page.locator("#redactRouteRow");
+ await reach(page, row);
+ const m = await row.evaluate((r) => {
+ const sub = r.querySelector(".sub"), list = document.querySelector("#redactList").getBoundingClientRect(), b = r.getBoundingClientRect();
+ return {
+ name: r.querySelector(".name").textContent, sub: sub.textContent,
+ button: r.querySelector("button.rt-cond")?.textContent.trim(),
+ select: r.querySelectorAll("select").length,
+ inside: b.left >= list.left - 1 && b.right <= list.right + 1,
+ wide: document.documentElement.scrollWidth > document.documentElement.clientWidth,
+ };
+ });
+ assert.equal(m.name, w.name);
+ assert.equal(m.button, w.off, "off unless the user set it");
+ assert.equal(m.select, 0, "no native select");
+ assert(m.inside && !m.wide, "inside the page");
+ if (lang !== "en") assert(!m.sub.startsWith("A request masking"), `its words translated: ${m.sub}`);
+
+ await row.locator("button.rt-cond").click();
+ await page.locator(pop).first().waitFor();
+ const items = await page.locator(pop).allTextContents();
+ assert(items[0].startsWith(w.off), `Off first: ${items}`);
+ assert.equal(items.filter((x) => x.includes("qwen3")).length, 1, `the one model: ${items}`);
+ assert(!items.some((x) => x.includes("big") || x.includes("Cloud")), `only a model that takes requests unmasked: ${items}`);
+ await page.locator(pop).filter({ hasText: "qwen3" }).first().click();
+ await page.waitForFunction(() => document.querySelector("#redactRouteRow button.rt-cond")?.textContent.includes("qwen3"));
+ assert.deepEqual(posted, ["near/qwen"]);
+ assert.match(await row.locator("button.rt-cond").textContent(), /qwen3 · Ollama/);
+
+ await page.locator("#redactRouteRow button.rt-cond").click();
+ await page.locator(pop).first().waitFor();
+ await page.locator(pop).filter({ hasText: w.off }).first().click();
+ await page.waitForFunction((off) => document.querySelector("#redactRouteRow button.rt-cond")?.textContent.trim() === off, w.off);
+ assert.deepEqual(posted, ["near/qwen", ""]);
+ assert.deepEqual(errors, []);
+ });
+ }
+ }
+ test(`${engine}: with no model to take it, the row says how to get one`, async (t) => {
+ const { browser, page, errors } = await open(engine, "en", 1100, [], { models: [] });
+ t.after(() => browser.close());
+ const row = page.locator("#redactRouteRow");
+ await reach(page, row);
+ assert(await row.locator("button.rt-cond").isDisabled());
+ assert.match(await row.locator(".sub").textContent(), /Send requests unmasked/);
+ assert.deepEqual(errors, []);
+ });
+ test(`${engine}: no row while nothing is masked`, async (t) => {
+ const { browser, page, errors } = await open(engine, "en", 1100, [], { redact: false });
+ t.after(() => browser.close());
+ assert.equal(await page.locator("#redactRouteRow").count(), 0);
+ assert.deepEqual(errors, []);
+ });
+}
diff --git a/internal/settings/settings.go b/internal/settings/settings.go
index 61c603cb4..1a8b70a24 100644
--- a/internal/settings/settings.go
+++ b/internal/settings/settings.go
@@ -163,6 +163,13 @@ type Settings struct {
// as written whatever finds them, each whole, in any case (#1977).
RedactActions map[string]string `json:"redactActions,omitempty"`
RedactAllow []string `json:"redactAllow,omitempty"`
+ // RedactRouteTo is the model an agent's request goes to, as written,
+ // when masking would change it (jessyoungyu on X: 「百度这家公司怎么样」
+ // reached a cloud model as a placeholder): "" masks it as before. Only
+ // a model whose every route the user set to go unmasked counts
+ // (provider.SkipsRedaction, the gateway's unredactedRoute), judged on
+ // each request; one that isn't now is as unset, masked.
+ RedactRouteTo string `json:"redactRouteTo,omitempty"`
// LAN shares the gateway on the local network; remote callers must use
// named caller keys. LANKey is retained for older Magpie versions.
// LAN is this computer's own (KeepOwn; Lullaby on Discord: sharing
@@ -1759,6 +1766,10 @@ func saveOwn(s Settings, recording, reads bool) error {
if s.CodexSubagentModel != "" && !strings.Contains(s.CodexSubagentModel, "/") {
return fmt.Errorf("the model for Codex's subagents must be a model's id such as codex/gpt-5.5, not %q", s.CodexSubagentModel)
}
+ s.RedactRouteTo = strings.TrimSpace(s.RedactRouteTo)
+ if s.RedactRouteTo != "" && !strings.Contains(s.RedactRouteTo, "/") {
+ return fmt.Errorf("the model for requests masking would change must be a model's id such as ollama/qwen3, not %q", s.RedactRouteTo)
+ }
s.DesktopSubagentModel = strings.TrimSpace(s.DesktopSubagentModel)
if s.DesktopSubagentModel != "" && !strings.Contains(s.DesktopSubagentModel, "/") {
return fmt.Errorf("the model for Claude Desktop's subagents must be a model's id such as deepseek/deepseek-v4, not %q", s.DesktopSubagentModel)
diff --git a/site/public/docs/ja/masking.html b/site/public/docs/ja/masking.html
index fef8b091c..0fe732419 100644
--- a/site/public/docs/ja/masking.html
+++ b/site/public/docs/ja/masking.html
@@ -154,6 +154,7 @@
<li><b>画像とファイルのデータはそのまま送られます。</b>スクリーンショット内の文字はマスクされません。</li>
<li><b>動かなければ送りません。</b>マスクがオンなのに動かないとき(<span class="ui">プライバシーマスキング</span> プラグインを手で止めた、自分でオンにしたローカルモデルが応答しない)、リクエストは何も送らなかったことを示す 503 になります。マスクなしで送ることはありません。既定でオンのモデルが応答しないときは、ルールだけでマスクします。</li>
<li><b>ローカルモデルは外せます。</b>すべてのアドレスがこのコンピュータかローカルネットワーク上にあるプロバイダ(Ollama、LM Studio)は、<span class="ui">リクエストをマスクしない</span> でそのまま送れます。CLI では <code>magpie provider set ollama unmasked=yes</code>。ログにはマスク後の内容が残ります。</li>
+ <li><b>マスクされるリクエストをそちらへ送ることもできます。</b><span class="ui">設定 › プライバシー</span> の <span class="ui">マスクせずに送る先</span> でそのようなモデルを選ぶと、マスクの対象を含むエージェントのリクエストはそのままそのモデルへ送られ、本当の名前のまま答えられます。マスクするものがないリクエストはこれまでどおりの送り先へ。そのモデルが失敗したらリクエストも失敗し、マスクしてベンダーへ送り直すことはありません。初期設定はオフです。</li>
<li><b>モデルにはプレースホルダーしか見えません。</b>マスクされた値を読んだり推論したりはできず、受け渡すだけです。モデルが読む必要のある値は <span class="ui">マスクしない</span> へ。</li>
<li><b>ルールは形で見つけます。</b>他の国の形式の電話番号や自由文中の名前には、<span class="ui">ローカルモデルで検出</span>、または自分の <span class="ui">マスクする語</span> と <span class="ui">マスクルール</span> が必要です。まず自分のテキストを <span class="ui">試す</span> で確かめてください。</li>
</ul>
diff --git a/site/public/docs/masking.html b/site/public/docs/masking.html
index 19c754913..c1285f9c0 100644
--- a/site/public/docs/masking.html
+++ b/site/public/docs/masking.html
@@ -154,6 +154,7 @@
<li><b>Images and file data are sent as they are.</b> Text inside a screenshot is not masked.</li>
<li><b>It fails closed.</b> If masking is on and can’t run (the <span class="ui">Privacy masking</span> plugin turned off by hand, or the local model you turned on can’t answer), the request gets a 503 that says nothing was sent. It is never sent unmasked instead. The model on by default that can’t answer leaves the rules to mask alone.</li>
<li><b>Local models can skip it.</b> <span class="ui">Send requests unmasked</span> on a provider whose every address is on this computer or your local network (Ollama, LM Studio) sends its requests as written; <code>magpie provider set ollama unmasked=yes</code> does the same. The logs keep the masked body.</li>
+ <li><b>Or send what masking would change to one of them.</b> In <span class="ui">Settings › Privacy</span>, <span class="ui">Instead of masking, send to</span> picks such a model. An agent’s request that masking would change then goes to it as written, so it can answer about the real names, and a request with nothing to mask goes where it was going. If that model fails, the request fails: it is never masked and sent to a vendor instead. Off by default.</li>
<li><b>The model sees placeholders.</b> It can’t read or reason about a masked value; it can only pass it on. Put a value it must read under <span class="ui">Never mask</span>.</li>
<li><b>Rules find shapes.</b> A phone number in another country’s format or a name in free text needs <span class="ui">Find with a local model</span>, or your own <span class="ui">Masked words</span> and <span class="ui">Masking rules</span>. Try your own text in <span class="ui">Try it</span> first.</li>
</ul>
diff --git a/site/public/docs/zh/masking.html b/site/public/docs/zh/masking.html
index 0d8080f9e..9465d9e47 100644
--- a/site/public/docs/zh/masking.html
+++ b/site/public/docs/zh/masking.html
@@ -154,6 +154,7 @@
<li><b>图片和文件数据原样发送。</b>截图里的文字不会被遮。</li>
<li><b>失败即不发送。</b>脱敏已打开却运行不了(手动关掉了 <span class="ui">隐私遮盖</span> 插件,或你打开的本地模型没响应),请求返回 503,说明什么都没发出,绝不会退回成不脱敏直接发送。默认打开的模型没响应时,只用规则脱敏。</li>
<li><b>本地模型可以跳过。</b>供应商的每个地址都在本机或局域网(Ollama、LM Studio)时,打开它的 <span class="ui">请求不脱敏</span>,请求就原样发出;命令行是 <code>magpie provider set ollama unmasked=yes</code>。日志里保存的仍是脱敏后的内容。</li>
+ <li><b>也可以把会被脱敏的请求改发给它。</b>在 <span class="ui">设置 › 隐私</span> 的 <span class="ui">命中脱敏时改用</span> 里选一个这样的模型。智能体的请求一旦命中脱敏规则,就原样发给它,它能按真实名称回答;没有要脱敏内容的请求照常发往原来的模型。这个模型失败时请求直接失败,绝不会脱敏后改发给云端厂商。默认关闭。</li>
<li><b>模型只看到占位符。</b>它无法读懂或推理被遮掉的值,只能原样传递。需要模型读的值放进 <span class="ui">从不脱敏</span>。</li>
<li><b>规则按格式匹配。</b>其他国家格式的电话号码、自由文本里的人名,需要 <span class="ui">用本地模型查找</span>,或者自己的 <span class="ui">脱敏词</span> 和 <span class="ui">自定义脱敏规则</span>。先用 <span class="ui">试一试</span> 测一下你自己的文本。</li>
</ul>