Files
magpie/internal/redact/token_count_test.go
T
yetone 03f609e3fd redact: a token count is not a secret (Discord, steven_4422; #3775)
steven_4422 in #feedback, with a screenshot of the Masking page: "total
number of tokens are masked as secret". Claude Code's system reminder
<total_tokens>1499872 tokens left</total_tokens> went upstream as
<total_tokens>{{SECRET_dkfgb3bf}} tokens left</total_tokens>, listed under
Secrets and found by the local model. #3775 had the same with AstrLink
Guard (<total_tokens>14902266).

Cause: the model labels the count `secret`; modelFits lets a hex run
(hexRe, which a run of decimal digits matches) through when secretCued
finds a key word right before it, and secretCued takes a cue anywhere in
the name before the value, with letters after it: the tag's name
total_tokens ends "token" + "s", so the count was "called a token". The
same held for "total_tokens": N, max_tokens: N and token_count=N.

Fix: a value of decimal digits alone that the model calls a secret is
masked only where the name right before it (then =, :, is, a tag's >,
quotes) ends with a key's, a token's, a password's or a PIN's word
(numberNamedSecret: api_key=, "token": , PIN:, user_pin=, 密码是,
<token>…). A name that runs on past the word counts what it names.
Rules are unchanged (numeric keys after key=/token: labels, phones,
ID numbers keep their own rules). The eval's token-count case now comes
out right for AstrLink Guard, so its #3775 marker is off.

Tests: TestTokenCountIsNoSecret uses the reporter's bytes in a Claude
Code user message's system reminder (the reporter's 7-digit counts, and
8-digit ones as Claude Code sends on a large window; the model's
len >= 8 secret check already let the 7-digit ones go, so the reporter's
exact values may have been 8 digits as read off the screenshot), plus a
usage object, max_tokens, token_count and "spin:". TestNamedNumber-
SecretStillMasked keeps api_key=, "token":, token is, PIN:, user_pin=,
password:, 密码是 and <token>…</token> masked.

Broken by hand: with model.go as on main, TestTokenCountIsNoSecret is
red for the 8-digit counts, total_tokens, max_tokens and token_count
(PIN:/user_pin= were not masked on main either; now they are).

Run (mote-run, base origin/main 93b31f5b7):
- gofmt -l clean; go vet ./internal/redact, GOOS=linux -tags nogui and
  GOOS=windows go vet ./internal/redact ./internal/masking ./internal/pii:
  clean.
- go test -timeout 45m ./internal/redact/... ./internal/masking
  ./internal/pii ./internal/otlp ./internal/sessions ./internal/settings
  ./internal/gui ./internal/gateway: all ok except
  - redact TestMaskEval: the token-count case's known issue was now
    fixed (marker removed, green after);
  - gui: TestCoreScriptBudget (green at 19594cc1c), TestSettingsSave-
    KeepsWhatItDoesNotSend (#4514, red on main, gui untouched here) and
    ten AppKit real-window tests killed at their deadlines on mote
    (#2645, #4125).
- Rebased on origin/main 19594cc1c: go test ./internal/redact/... ok,
  gofmt clean, TestCoreScriptBudget ok, #4514 still red there.

Not tried: the real local model (AstrLink Guard / Privacy Filter) on the
reporter's request; the gateway package was not run again after the
rebase (the commits in between touch usage, provider and the GUI).
2026-10-31 14:19:02 +08:00

61 lines
2.6 KiB
Go

package redact
import (
"strings"
"testing"
)
// The count Claude Code puts in a system reminder each turn is no secret,
// though the local model calls it one: steven_4422 on Discord (10-31) saw
// <total_tokens>{{SECRET_dkfgb3bf}} tokens left</total_tokens> on the
// Masking page, found by the local model. The tag's name has "token" in
// it, which secretCued took for the digits' name. The reporter's counts
// are as written, and the same counts at eight digits, which Claude Code
// sends on a large window (14902266) and the model's length check lets in.
func TestTokenCountIsNoSecret(t *testing.T) {
for _, n := range []string{"1499872", "1493809", "14998720", "14938090"} {
reminder := "<system-reminder>\n<total_tokens>" + n + " tokens left</total_tokens>\n</system-reminder>"
f := &fakeModel{values: map[string]string{n: "secret"}}
o := Options{Secrets: true, Personal: true, Model: f.find}
body := `{"model":"claude-opus-4-1","system":[{"type":"text","text":"You are Claude Code."}],"messages":[{"role":"user","content":[{"type":"text","text":` + jsonQuote(reminder) + `},{"type":"text","text":"go on"}]}]}`
if got, k := MaskJSON([]byte(body), o); k != 0 || string(got) != body {
t.Errorf("%s: %d masked: %s", n, k, got)
}
}
// a count in a usage object or a setting is no secret either (each
// its own number: one masked is known, and masked wherever it is)
for _, c := range []struct{ text, v string }{
{`{"usage":{"total_tokens": 14998721}}`, "14998721"},
{`max_tokens: 32000000`, "32000000"},
{`token_count=14998722`, "14998722"},
{`spin: 14998723`, "14998723"},
} {
f := &fakeModel{values: map[string]string{c.v: "secret"}}
o := Options{Secrets: true, Model: f.find}
if out, k := Mask(c.text, o); k != 0 {
t.Errorf("%q: %d masked: %q", c.text, k, out)
}
}
}
// Digits the model calls a secret are masked where a secret's name gives
// them to it: a key's, a token's, a password's or a PIN's.
func TestNamedNumberSecretStillMasked(t *testing.T) {
for _, c := range []struct{ text, v string }{
{"api_key=48213097715", "48213097715"},
{`{"token": "73914682055"}`, "73914682055"},
{"the access token is 73914682055", "73914682055"},
{"PIN: 73914682", "73914682"},
{"user_pin=73914682", "73914682"},
{"wifi password: 83920175", "83920175"},
{"密码是83920175", "83920175"},
{"<token>73914682055</token>", "73914682055"},
} {
f := &fakeModel{values: map[string]string{c.v: "secret"}}
o := Options{Secrets: true, Model: f.find}
if out, _ := Mask(c.text, o); strings.Contains(out, c.v) {
t.Errorf("%q: not masked: %q", c.text, out)
}
}
}