redact: the local model doesn't read an agent's tool definitions (niuniu_8964 on X)

码妞 on X: the official container on a 4-core, 8 GB server, AstrLink Guard
on, Claude Code's requests sat at 脱敏中 for minutes and timed out, load
1.78, a short conversation. aca0f0d8a made Guard read a text's windows
at once; this takes away most of what it reads.

MaskJSON asked the model about every text it masks, the tools'
descriptions and their schemas' too. On Claude Code 2.1.296's own request
(internal/gateway/testdata/workdir) that was 67,532 bytes, 52,577 of them
definitions: the agent's or a tool server's text, the same in every
request. The per-text cache only helps after the first request has been
read whole, so a fresh install (or new tools, or a Claude Code update)
paid for all of it at once, past the gateway's 2 minutes on a slow CPU.

Now what is under the request's definitions (defKeys, defItems) is never
sent to the model; it is masked as with the model off, by the rules
alone (emails, phones, keys, the user's words still are). It is masked
after the rest of the request, so a value the model found in a message
is masked in a definition too, as known. The system prompt and messages
are read as before.

Measured on magpie-desktop (WSL, Ryzen AI Max+ 395, taskset to the CPUs
named), real Guard model (rev 7216b6a3, sha256s checked), that request
through redact.MaskJSON with guardBackend, cold cache:
  4 CPUs: 129 texts, 67,532 bytes, 11.5s before; 7 texts, 14,955 bytes, 2.6s now
  2 CPUs: 20.9s before, 4.9s now
  rules alone 13-15ms; the same body warm 13ms, the next turn 36-44ms,
  before and after.
On three real Claude Code requests (claude-code-2.1.296.json,
context/claude-code.json, dsh-gateway messages-turn.json), masked with
the model at 0.8 and with the rules alone, every top-level key came out
the same: the model masked nothing in their definitions that the rules
didn't.

No time cap was added. The reporter turned the model on (the default-on
model is from today, ae0327945), so a request there is held for the
model and never falls back to the rules; a cap would only turn the wait
into an error. Less to read is the fix.

Tests:
- TestModelNotAskedAboutDefinitions (new): on Claude Code 2.1.296's and
  Codex 0.162.1's own requests no definition's text is asked about, and
  every long text said is.
- TestDefinitionMaskedAsWithoutModel (new): a tool's description and a
  schema property's come out as with no model; the email, the key and
  the phone in them are masked.
- TestModelAskedOnceForRequest: now asks about the message only, and the
  description's name is still masked (as known).

Run:
- mote-run: go vet ./internal/redact ./internal/masking, GOOS=linux and
  GOOS=windows go vet of them: ok. go test ./internal/redact/...
  ./internal/masking/... ./internal/pii/... ./internal/gwmw/...: ok.
  go test -run "Mask|Redact|Privacy|Unmasked|Masked|Placeholder"
  ./internal/gateway ./internal/middleware ./internal/gui: ok.
- wsl-run: go test ./internal/redact ./internal/masking: ok; the stage
  timings above (a scratch test, not committed).
- gofmt -l: clean.
- The vet and the test lines above ran again after rebasing onto
  d6df72a2b (its own redact change), and redact, masking and the
  gateway masking tests again on 25fc92d59: ok.

Broken by hand:
- Definitions sent to the model again: TestModelNotAskedAboutDefinitions
  red ("the model was asked about a definition's "Launch a new agent…""),
  and TestModelAskedOnceForRequest red.
- Definitions masked in the first pass, before the messages:
  TestModelAskedOnceForRequest red ("description":"find Rina Kowalczyk"
  went as written).

Not tried: the reporter's server, the Docker image itself, or a real
Claude Code session through it; a Chinese full name only in a tool's
description is no longer masked (the model was what found one; the rules
mask Chinese names only beside the model). The full go test ./... wasn't
run: mote had another session's full suite on it at load ~216, and only
internal/redact changed.
This commit is contained in:
yetone
2026-10-28 17:36:40 +08:00
parent 1feee38d17
commit a5315c70d5
3 changed files with 117 additions and 5 deletions
File diff suppressed because one or more lines are too long
+24 -2
View File
@@ -386,11 +386,21 @@ func MaskJSON(body []byte, o Options) ([]byte, int) {
masked := func(path, key, s string) bool {
return !keepIn(path, key, s, args, defs) && (signed == nil || !under(path, signed))
}
// What the agent defines (its tools' and schemas' descriptions) is
// masked by the rules alone, as with the model off: it is the agent's
// or a tool server's text, the same in every request, and no one's
// conversation. Read by the model it was most of a request's text,
// 52,577 of Claude Code 2.1.296's 67,532 bytes, and on a 4-core
// server the first request waited out the gateway's 2 minutes while
// the model read it (niuniu_8964 on X); on three real Claude Code
// requests the model masked nothing there the rules didn't.
defsO := o
defsO.Model = nil
if o.Model != nil {
// the model is asked once, for all of the request's texts
var texts []string
if _, ok := walk(body, func(path, key, s string) string {
if masked(path, key, s) {
if masked(path, key, s) && !under(path, defs) {
texts = append(texts, s)
}
return s
@@ -405,14 +415,26 @@ func MaskJSON(body []byte, o Options) ([]byte, int) {
seen = append(seen, v, p)
return p
}
// the definitions after the rest, so a value the model found in what
// is said is masked in them too, as known
out, ok := walk(body, func(path, key, s string) string {
if !masked(path, key, s) {
if !masked(path, key, s) || under(path, defs) {
return s
}
t, n := mask(s, o, put, asJSON(key, ""))
total += n
return t
})
if ok && len(defs) > 0 {
out, ok = walk(out, func(path, key, s string) string {
if !masked(path, key, s) || !under(path, defs) {
return s
}
t, n := mask(s, defsO, put, asJSON(key, ""))
total += n
return t
})
}
if !ok {
t, n := Mask(string(body), o)
return []byte(t), n
+92 -2
View File
@@ -1,6 +1,8 @@
package redact
import (
"encoding/json"
"os"
"regexp"
"slices"
"strings"
@@ -147,8 +149,9 @@ func TestModelAskedOnceForRequest(t *testing.T) {
}
got := slices.Clone(f.asked[0])
slices.Sort(got)
// "ok" is too short to hold anything
if want := []string{"find " + name, "hi " + name}; !slices.Equal(got, want) {
// "ok" is too short to hold anything, and the tool's description is
// the rules' alone (TestModelNotAskedAboutDefinitions)
if want := []string{"hi " + name}; !slices.Equal(got, want) {
t.Fatalf("asked about %q, want %q", got, want)
}
// the description and the two messages; the thinking block goes back
@@ -157,3 +160,90 @@ func TestModelAskedOnceForRequest(t *testing.T) {
t.Fatalf("%d masked: %s", n, out)
}
}
// What an agent defines for the vendor (its tools' and schemas'
// descriptions) is never read by the model: it was most of a request's
// text, and the model reading it held a 4-core server's first Claude Code
// request past the gateway's 2 minutes (niuniu_8964 on X). On Claude Code
// 2.1.296's own request the model was asked about 67,532 bytes, 52,577 of
// them its tools' descriptions; on Codex 0.162.1's, its tools too. What
// the user and the agent say (the system prompt, the messages) still is.
func TestModelNotAskedAboutDefinitions(t *testing.T) {
for _, p := range []string{"../gateway/testdata/workdir/claude-code-2.1.296.json", "testdata/codex-0.162.1-request.json"} {
raw, err := os.ReadFile(p)
if err != nil {
t.Fatal(err)
}
var v any
if err := json.Unmarshal(raw, &v); err != nil {
t.Fatal(err)
}
defs := definitions(v)
var inDefs, said []string
walk(raw, func(path, key, s string) string {
if len(s) >= 3 && !kept[key] && !isID(key) {
if under(path, defs) {
inDefs = append(inDefs, s)
} else {
said = append(said, s)
}
}
return s
})
if len(inDefs) == 0 || len(said) == 0 {
t.Fatalf("%s: %d texts in definitions, %d said: not the request the test is about", p, len(inDefs), len(said))
}
var asked []string
MaskJSON(raw, Options{Secrets: true, Personal: true, Model: func(ts []string) [][]Found {
asked = append(asked, ts...)
return make([][]Found, len(ts))
}})
n := 0
for _, s := range asked {
n += len(s)
}
for _, d := range inDefs {
if slices.Contains(asked, d) && !slices.Contains(said, d) {
t.Errorf("%s: the model was asked about a definition's %.80q", p, d)
}
}
for _, s := range said {
if len(s) > 200 && !slices.Contains(asked, s) {
t.Errorf("%s: the model wasn't asked about %.80q", p, s)
}
}
t.Logf("%s: %d texts, %d bytes asked", p, len(asked), n)
}
}
// A definition is masked as it is with the model off: by the rules (an
// email, a key), and not by what the model would have found in it.
func TestDefinitionMaskedAsWithoutModel(t *testing.T) {
const name, other = "Rina Kowalczyk", "Tomasz Brzezinski"
f := &fakeModel{values: map[string]string{name: "private_person", other: "private_person"}}
body := `{"model":"claude-x","tools":[{"name":"crm","description":"Ask ` + other + ` (tomasz.b@acme-corp.com) with key sk-ant-api03-` + strings.Repeat("Ab3", 30) + `",` +
`"input_schema":{"type":"object","properties":{"who":{"type":"string","description":"a customer, e.g. ` + other + `, tel 13800138000"}}}}],` +
`"messages":[{"role":"user","content":"call ` + name + ` today"}]}`
without, _ := MaskJSON([]byte(body), Options{Secrets: true, Personal: true})
with, _ := MaskJSON([]byte(body), Options{Secrets: true, Personal: true, Model: f.find})
var a, b map[string]any
if err := json.Unmarshal(without, &a); err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(with, &b); err != nil {
t.Fatal(err)
}
ta, _ := json.Marshal(a["tools"])
tb, _ := json.Marshal(b["tools"])
if string(ta) != string(tb) {
t.Fatalf("tools with the model:\n%s\nwithout:\n%s", tb, ta)
}
for _, v := range []string{"tomasz.b@acme-corp.com", "sk-ant-api03-", "13800138000"} {
if strings.Contains(string(tb), v) {
t.Errorf("%q went as written in a definition: %s", v, tb)
}
}
if strings.Contains(string(with), name) {
t.Errorf("the message's name went as written: %s", with)
}
}