Skip to content

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

redact: the local model's secrets leave UUIDs, digests and paths, and client_metadata goes as written

yetoned3db461bbb23
diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md
index 0adaf760b..fd0d5d114 100644
--- a/docs/subsystems/gateway-routing.md
+++ b/docs/subsystems/gateway-routing.md
@@ -75,9 +75,9 @@ 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 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` 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`), 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): 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; the log and recent calls keep the masked body. 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 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`), 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): 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; the log and recent calls keep the masked body. 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`).
- 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, MAC addresses, and serial numbers next to S/N or the like. 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. 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`, `fileExt` in `realEmail`) is a file's name, for the rule and the local model alike (#3029). 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 (`settings.RedactModel`, this computer's own, kept by `KeepOwn`), 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`); 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). 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 it on 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), and the model's are cut at punctuation and titles ([`zhname.go`](../../internal/redact/zhname.go)); a surname with nothing around it (王国的, 张开的) is left. The model is OpenAI's Privacy Filter as 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): Settings → Privacy's On downloads both (`pii.Download`, each checked against its pinned sha256, from hf-mirror first in China, `source.chinaFirst`) 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 is started on the CPU for the rest of the run), joins short texts into 12 KB chunks and maps the spans back, keeps the last 20000 answers by hash, and ends it after 10 minutes with no request ([`internal/pii`](../../internal/pii)). A model that is on and can't answer (not on disk, didn't start, hung) panics, so the request is held back with the 503 above rather than sent with the rules' masking alone (`masking.model`).
+ - With *Find with a local model* on (`settings.RedactModel`, this computer's own, kept by `KeepOwn`), 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`); 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). 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 it on 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), and the model's are cut at punctuation and titles ([`zhname.go`](../../internal/redact/zhname.go)); a surname with nothing around it (王国的, 张开的) is left. The model is OpenAI's Privacy Filter as 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): Settings → Privacy's On downloads both (`pii.Download`, each checked against its pinned sha256, from hf-mirror first in China, `source.chinaFirst`) 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 is started on the CPU for the rest of the run), joins short texts into 12 KB chunks and maps the spans back, keeps the last 20000 answers by hash, and ends it after 10 minutes with no request ([`internal/pii`](../../internal/pii)). A model that is on and can't answer (not on disk, didn't start, hung) panics, so the request is held back with the 503 above rather than sent with the rules' masking alone (`masking.model`).
- 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.
- Each request that had anything masked is recorded as it goes to the vendor (`noteMasked` in [`masked.go`](../../internal/gateway/masked.go), from chat, the Codex backend, search, images, video, System One, embeddings and audio; count_tokens is masked and not recorded). The masker says what it swapped through `gwmw.Teller` (`JSONHits`, `TextHits`: each placeholder's kind, its finder, `redact.ByRule`, `ByYours`, `ByWord`, `ByModel` or `ByKnown`, and a hint of its first characters and its length), and [`internal/maskrec`](../../internal/maskrec/maskrec.go) keeps one line per request in a file per day under `~/.config/magpie/masking`, 30 days and 100 MB in all: the agent, session, provider, model, and each placeholder with its kind, finder and hint, never the value. The text around each placeholder as the vendor saw it is kept in memory only, for the last 400 records, and what a placeholder stood for only in the masker's own memory (`Teller.Value`); after a restart neither is there. A recent call (`Call.Masked`) and the route trace (`Route.Masked`) carry the counts and the record's key, which the Gateway and Routing pages' badge opens in Settings → Privacy (`/api/masking/*`, [`internal/gui/masking.go`](../../internal/gui/masking.go)). A masker without `Teller`, a plugin's, masks as before and records nothing. Under a test binary testenv isolated nothing is written (`maskrec.inTest`).
- `DecidesModel` gives a 400.
diff --git a/internal/redact/json.go b/internal/redact/json.go
index 86c77ba23..df00352ed 100644
--- a/internal/redact/json.go
+++ b/internal/redact/json.go
@@ -120,13 +120,16 @@ func scan(body []byte) (signed, args, defs []string) {
// (Anthropic, Chat Completions, Responses, Gemini) and the choice among
// them, the output's schema (Chat's response_format, Responses' text,
// Anthropic's output_format and output_config, Gemini's generationConfig),
-// Responses' reasoning and include, and how the reply streams
+// Responses' reasoning and include, how the reply streams
// (stream_options, whose reasoning_summary_delivery the vendor checks is
-// "sequential" or "sequential_cutoff", #3144).
+// "sequential" or "sequential_cutoff", #3144), and the client's ids that
+// no model reads (client_metadata: Codex's installation, session, thread,
+// turn and window, as a JSON string in x-codex-turn-metadata, which the
+// vendor and magpie's own sessions match a turn by).
var defKeys = []string{
"tools", "tool_choice", "functions", "function_call", "toolConfig", "tool_config",
"response_format", "text", "reasoning", "include", "output_format", "output_config",
- "generationConfig", "generation_config", "stream_options",
+ "generationConfig", "generation_config", "stream_options", "client_metadata",
}
// defItems are the Responses input items that carry tools: Responses
diff --git a/internal/redact/model.go b/internal/redact/model.go
index ba9bc6254..3791da248 100644
--- a/internal/redact/model.go
+++ b/internal/redact/model.go
@@ -1,6 +1,7 @@
package redact
import (
+ "regexp"
"strings"
"unicode"
"unicode/utf8"
@@ -96,7 +97,7 @@ func isSpace(c byte) bool { return c == ' ' || c == '\t' || c == '\n' || c == '\
func modelTakes(kind, v string) bool {
switch kind {
case "SECRET":
- return len(v) >= 8 && !madeID(v)
+ return len(v) >= 8 && !madeID(v) && !namesThing(v)
case "PERSON":
return utf8.RuneCountInString(v) >= 2 && strings.IndexFunc(v, unicode.IsLetter) >= 0 && !snakeWord(v)
case "ACCOUNT":
@@ -122,6 +123,28 @@ func snakeWord(v string) bool {
return true
}
+// namesThing says v, which the model took for a secret, names a thing
+// rather than unlocking one: a UUID (a session's, a turn's, a request's;
+// window_id's has a :0 after it), a digest with its algorithm's name
+// (sha256 9b74…), a file's path. The model takes all three for secrets;
+// Codex's turn metadata went as {{SECRET_…}} in each id (#1977). A key or
+// a password in a field named for one is still the rules' (secretNames).
+func namesThing(v string) bool {
+ if u, n, ok := strings.Cut(v, ":"); ok && n != "" && strings.Trim(n, digits) == "" {
+ v = u
+ }
+ if uuidRe.MatchString(v) || digestRe.MatchString(v) {
+ return true
+ }
+ return !strings.ContainsAny(v, " \t\n") && (strings.HasPrefix(v, "/") || strings.HasPrefix(v, "~/") ||
+ strings.HasPrefix(v, "./") || strings.HasPrefix(v, "../") || len(v) > 3 && v[1] == ':' && v[2] == '\\')
+}
+
+var (
+ uuidRe = regexp.MustCompile(`^(?i)[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`)
+ digestRe = regexp.MustCompile(`^(?i)(?:md5|sha-?(?:1|224|256|384|512)|blake2b?|blake3)[ :=-]+[0-9a-f]{32,128}$`)
+)
+
// madeID says v is an id of the shape vendors and tools give theirs: a
// short lower-case prefix, an underscore, then letters and digits
// (req_011CS…, toolu_01A…, call_x9…, msg_…).
diff --git a/internal/redact/modelids_test.go b/internal/redact/modelids_test.go
new file mode 100644
index 000000000..a09c56fdf
--- /dev/null
+++ b/internal/redact/modelids_test.go
@@ -0,0 +1,51 @@
+package redact
+
+import (
+ "strings"
+ "testing"
+)
+
+// The local model took the ids in Codex's turn metadata for secrets, and
+// each went as {{SECRET_…}}: the installation's, the session's, the
+// turn's, the window's (#1977, the owner's records). It does the same to a
+// request id in a log and a digest named by its algorithm; those are what
+// it labelled "secret" when run on these texts. They go as written, in
+// client_metadata and anywhere else. A password and a key it finds are
+// still masked.
+func TestModelSecretIsNoID(t *testing.T) {
+ meta := `{"installation_id":"6f1c2a0e-8b7d-4c3a-9e21-5d4b3c2a1f00","session_id":"019a0b1c-2d3e-7f40-8a1b-2c3d4e5f6a7b","agent_name":"/root","turn_id":"019a0b1c-9999-7f40-8a1b-2c3d4e5f6a7b","window_id":"019a0b1c-2d3e-7f40-8a1b-2c3d4e5f6a7b:0","window_number":0,"request_kind":"turn"}`
+ f := &fakeModel{values: map[string]string{
+ "6f1c2a0e-8b7d-4c3a-9e21-5d4b3c2a1f00": "secret",
+ "019a0b1c-2d3e-7f40-8a1b-2c3d4e5f6a7b": "secret",
+ "019a0b1c-9999-7f40-8a1b-2c3d4e5f6a7b": "secret",
+ "019a0b1c-2d3e-7f40-8a1b-2c3d4e5f6a7b:0": "secret",
+ "4f2a9c1e-77b3-4d1a-b2e0-9c8d7e6f5a4b": "secret",
+ "sha256 9b74c9897bac770ffc029102a200c5de2b6e0f3c7f1d0f2a7c3b5e1d9f8a7c6b": "secret",
+ "/Users/vdxz/.codex/skills/wrangler/SKILL.md": "secret",
+ "Tr0ub4dor&3xq": "secret",
+ "9f8e7d6c5b4a39281706f5e4d3c2b1a0": "secret",
+ }}
+ o := Options{Secrets: true, Personal: true, Model: f.find}
+
+ body := `{"model":"gpt-5.5","client_metadata":{"x-codex-turn-metadata":` + quoteJSON(meta) + `},"input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"request id 4f2a9c1e-77b3-4d1a-b2e0-9c8d7e6f5a4b failed; sha256 9b74c9897bac770ffc029102a200c5de2b6e0f3c7f1d0f2a7c3b5e1d9f8a7c6b; - wrangler: Cloudflare Workers CLI (file: /Users/vdxz/.codex/skills/wrangler/SKILL.md). my password is Tr0ub4dor&3xq and the api key is 9f8e7d6c5b4a39281706f5e4d3c2b1a0"}]}]}`
+ out, n := MaskJSON([]byte(body), o)
+ got := string(out)
+ if n != 2 || strings.Contains(got, "Tr0ub4dor&3xq") || strings.Contains(got, "9f8e7d6c5b4a39281706f5e4d3c2b1a0") {
+ t.Fatalf("%d masked, want the password and the key: %s", n, got)
+ }
+ for _, v := range []string{quoteJSON(meta), "request id 4f2a9c1e-77b3-4d1a-b2e0-9c8d7e6f5a4b failed", "sha256 9b74c9897bac", "(file: /Users/vdxz/.codex/skills/wrangler/SKILL.md)"} {
+ if !strings.Contains(got, v) {
+ t.Errorf("%s went masked: %s", v, got)
+ }
+ }
+
+ // a word the user masks is kept in client_metadata too: no model reads it
+ w := `{"client_metadata":{"x-codex-turn-metadata":"{\"agent_name\":\"/atlas\"}"},"input":"ship atlas"}`
+ if out, n := MaskJSON([]byte(w), Options{Words: []string{"atlas"}}); n != 1 || !strings.Contains(string(out), `/atlas`) {
+ t.Fatalf("%d masked: %s", n, out)
+ }
+}
+
+func quoteJSON(s string) string {
+ return `"` + strings.NewReplacer(`\`, `\\`, `"`, `\"`).Replace(s) + `"`
+}