shensmart, with personal data masked, saw {{PHONE_…}} left in the
model's replies. Every reply path already goes through redact.Writer
(serve, Codex's upstream, count_tokens, embeddings, image and video
prompts; plugin-served providers write into the same wrapped writer),
and an exact placeholder came back on all of them, split across
Anthropic, Chat Completions, Responses and Gemini SSE events alike. What
didn't come back is a placeholder the model wrote differently from how it
was given: Jinja-style {{ PHONE_x }}, {{PHONE_x} or {PHONE_x}, the bare
PHONE_x, or the hash in upper case. Restore matched only the exact form.
Restore now finds a placeholder's body (KIND_ and the 8 of the hash), in
any case, at a word boundary, and puts the value back when it names one
magpie made, taking with it the braces and spaces the model put around
it (both braces of a side, or one of each side; a lone { on one side is
the text's own). The 40-bit keyed hash means no other text matches.
For streams, partialTail also holds a tail like "{{ PH", "{{PHONE_x}"
(the second brace still to come) and a last word that starts like a kind
magpie has masked (PH, PHONE_b5pu), and Writer.event now looks for the
tail before restoring, so a placeholder whose closing brace is in the
next event is not put back with that brace left behind in the text.
Not changed: the values stay in memory only. The placeholder key is kept
in redact.key, so after a restart the same value gets the same
placeholder, and the agent's history holds the real value (it was put
back), so the next request brings it again; checked by masking, clearing
the map and key as a restart does, and masking the history again.
Writing the map to disk would keep every masked secret in one file.
Run: go test ./internal/redact ./internal/gateway (sandbox HOME), gofmt,
go vet and GOOS=linux/windows go vet on internal/redact.
TestRestoreRewrittenPlaceholder, TestWriterStreamRewrittenPlaceholder and
TestWriterStreamToolArgsRewritten fail on the old redact.go/writer.go;
with the old writer.go alone the stream test fails with "13987654321 }}"
(a stray brace); with wordTail turned off it fails with a bare
PHONE_zdx44tzn left in the text.