Documentation
¶
Overview ¶
Package jsonwalk locates and surgically rewrites the content-carrying string fields inside an Anthropic Messages API request or response body (system prompt, message text, tool_result content, tool_use input), leaving every other byte (key order, protocol fields, unrelated values) untouched. Byte preservation matters here for a concrete reason: reserializing the whole body through a generic decode/re-encode (e.g. into map[string]any) does not preserve JSON key order, and Claude Code's own prompt-cache breakpoints are a prefix match keyed on exact bytes, so an incidental key reordering downstream of a redacted field would silently invalidate caching for everything after it.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Detokenize ¶
Detokenize walks raw (a full non-streaming Messages API response body, whose content lives in a top-level "content" array rather than "messages") and replaces every content-carrying string with tf(original). For streaming (SSE) responses, callers reconstruct one content block at a time from buffered deltas and should call redact.Engine.Detokenize directly on that block's known text/input field rather than going through this walker; the block type is already known at that point, so a generic walk adds nothing. tf has no error return: per redact.Engine.Detokenize's fail-open policy, an unrecognized token passes through as literal text rather than erroring. If the walk itself hits a structural problem (should not happen against Anthropic's own response shape), Detokenize returns raw unchanged rather than a partially-rewritten body; the safe failure direction on this path is "Claude Code sees a token it can't resolve," never a corrupted response.
func DetokenizeValue ¶
DetokenizeValue walks an arbitrary standalone JSON value (not necessarily wrapped in a Messages API envelope) and replaces every string leaf AND object key with tf(original) via redactValue, preserving all other structure and byte layout. Used for a tool_use.input object reconstructed on its own from buffered SSE deltas during streaming, where there is no surrounding "messages"/"content" wrapper to route through; the same shape apply's inputPaths handling covers for the non-streaming case, via the same redactValue.
func Tokenize ¶
Tokenize walks raw (a Messages API request body) and replaces every content-carrying string with tf(original). Fails closed: if tf errors for any string, Tokenize returns that error and no output, and callers must not forward a request after an error here, since that could mean real PII reaching the model un-redacted.
Also fails closed on raw itself not being valid JSON. gjson (which collectPaths and apply are both built on) is a lenient, non-validating parser: Get on malformed JSON doesn't error, it just returns a zero-value Result, so collectPaths silently finds zero paths and apply's loop over those zero paths returns raw byte-for-byte unchanged with a nil error. Without this explicit check up front, a request body that happens not to be well-formed JSON (a client bug, a truncated/corrupted body, a body deliberately crafted to probe this exact gap) would sail through this function looking like a completely successful, no-op tokenize (the caller has no way to distinguish that from "valid JSON containing nothing to redact"), and whatever real PII the malformed body actually contained reaches the model completely unredacted: a malformed-JSON body containing an AWS-key-shaped string would be forwarded upstream byte-for-byte identical to the input.
Types ¶
This section is empty.