jsonwalk

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 5, 2026 License: GPL-3.0 Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func Detokenize(raw []byte, tf func(string) string) []byte

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

func DetokenizeValue(raw []byte, tf func(string) string) []byte

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

func Tokenize(raw []byte, tf func(string) (string, error)) ([]byte, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL