Commit Graph
4 Commits
Author SHA1 Message Date
yetone a5315c70d5 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.
2026-10-28 17:36:40 +08:00
yetone ea7c0067e0 privacy: each local model's confidence threshold is the user's to set
Yayoi_no_yume on X asked for the privacy model's threshold to be
adjustable: they read it as fixed at 0.5, and from their own use find
0.85-0.95 right for AstrLink. The 0.5 is Privacy Filter's (pf-serve's
argv default); AstrLink Guard already masked at its publisher's 0.8.

Both models now find at a floor of 0.5 (pii.Floor) and give each span
its score: Guard's least token probability (rounded through float32,
so a span read back from the cache file sits on the same side of a
threshold), pf-serve's own. masking.model keeps the spans at
pii.Threshold or over: settings.RedactModelScore[model] when it is in
[0.5, 1), else the model's MinScore (Privacy Filter 0.5, AstrLink
Guard 0.8). Settings -> Privacy shows a Confidence threshold row under
the model (app menu, 0.5 to 0.95 and Default), /api/privacy-model
takes do:"score". The setting is this computer's own, like the model.

The defaults stay. TestMaskEval's cases, rules + model + magpie's
checks, by threshold:

  Privacy Filter 0.5-0.8   masked 41/42  kept 112/113
  Privacy Filter 0.85-0.95 masked 40/42  (a Chinese name lost)
  AstrLink Guard 0.5-0.7   masked 42/42  kept 112/113
  AstrLink Guard 0.8-0.85  masked 41/42  (a phone in a tool result)
  AstrLink Guard 0.9       masked 40/42
  AstrLink Guard 0.95      masked 40/42  kept 113/113

The model alone does raise fewer false alarms higher up (Privacy
Filter's found-in-keep falls from 47 at 0.5 to 27 at 0.95, Guard's
from 8 to 2), but the checks after it drop most of those already, so
a higher threshold mostly costs values that should be masked.

Cache file: found-<model>.bin is now "pfound02" with a float32 score
per span. A "pfound01" file is still read, each span with score 1, so
texts cached before mask as they did and aren't asked again. For those
texts a higher threshold drops nothing and a lower one doesn't add a
Guard span scored 0.5-0.8 until the cache turns over (20000 answers).
merge() keeps the surer score of spans it joins. The eval recordings
(eval-model.json, eval-astrlink.json) were made again with scores,
from a scratch cache, the live found files untouched.

Run in the worktree at 11dcc743f + this change:
- go vet / gofmt -l on the touched packages: clean
- go test -count=1 -timeout 45m ./internal/pii/... ./internal/masking
  ./internal/redact ./internal/settings ./internal/gui ./internal/gateway: all ok (gateway 546s, rerun
  after an earlier run hit "no space left on device" on this machine;
  every failure in that run was a TempDir mkdir). This run caught
  TestSettingsSaveKeepsWhatItDoesNotSend wanting RedactModelScore set;
  it is now
- GUI: privacy-model.test.cjs 64/64 (chromium + webkit, 5 languages,
  1100 and 640px; the new threshold menu test among them),
  i18n-keys 4/4, redact-kinds, redact-rule-edit,
  redact-rule-row, redact-shape, redact-words-any-case all pass.
  masking-records 82/82, routing-masking 10/10, privacy-usage-stats 4/4,
  mask-actions 22/22. privacy-hides-accounts was 3/4 once (webkit en,
  page.goto's 5s at load ~290, the class of #3525, commented there),
  then 4/4 three times. Not run: the rest of the GUI suite.
Rebased on 508911e43 (which touched internal/pii fetch and mirror): at
that head, go build ./..., Linux and Windows go vet of pii, masking,
settings and redact, gofmt -l, and go test of pii/..., masking, redact,
settings and gui all ok; gateway not run again after the rebase.
Broken by hand: masking.model keeping every span (TestModelThreshold
  red); Threshold taking any value (TestModelThreshold red); the cache
  writing 1 for every score (TestRestartReadsWhatWasFound red); merge
  not keeping the surer score (TestMergeAcrossPieces red, after its
  joined span was made surer than the one it joins).
Not tried: the real app's window, Windows, Privacy Filter on a GPU
with the new score read (pf-serve itself is unchanged; it always sent
the score).
2026-10-27 14:10:33 +08:00
yetone 552cdf8071 redact: a value the model spans from the delimiter before it is masked (#3591)
The model's span may start at the token before a value: the `":"` of
a JSON field, a CSV row's comma. A phone number in
{"phone":"+86 139 1234 5678"} came as `":"+86 139 1234 5678`, and in a
CSV row as `,+81 90-1234-5678`, which the PHONE check took for no
number, and it went as written. A name came as `"Wei Zhang"` and was
masked with its quotes, so the JSON lost them. modelSpans now trims
spaces and `"',;:` from both ends of a name's, a phone's, an
address's, an account's and an email's span; a secret's only spaces,
since a password may start or end with a quote.

Found by TestMaskEval: its 3 #3591 phones are no longer Known, and
"both" reads 33/34 masked, 0/40 wrongly (from 30/34).

Run: go test ./internal/redact and ./internal/pii ok; -run
'Mask|Redact|Privacy' ./internal/gui ./internal/gateway ok; go vet
darwin, linux, windows; gofmt -l clean.
Broken by hand: edge = isSpace for every kind: TestModelSpanLeavesDelimiters
red (both phones as written, the name's quotes gone) and TestMaskEval
red on the 3 phones.
Not tried: the full suite (internal/proc's #3567 is red on main), a
real request through the gateway with the model on.
2026-10-25 17:44:52 +08:00
yetone c0dad80da1 redact, pii: a local model finds what the masking rules can't (#1977)
WIP on pf/serve, recovered after the scratchpad holding it was wiped.
internal/pii downloads OpenAI's Privacy Filter (q8 GGUF, sha256 pinned)
and pf-serve, runs it on the GPU first, and answers redact's Model;
settings.RedactModel turns it on, per computer. Off by default.

Run: go vet and go test -race on internal/redact, pii, source,
settings (all ok). Not yet released: pf-serve's serveSHA is empty
until pf-v1 is tagged.
2026-10-18 23:12:41 +08:00