Keep long Claude stream records valid in the retained evidence

RedactEvidence truncates at 16 KB and appends a line break, which cut long
stream-json records in two. The session export parses that stream, so the
first real Claude stage (run 346) parked on "Session export pending". The
stream is now redacted record by record without truncation.
This commit is contained in:
2026-11-11 10:48:42 +00:00
parent ac4627b8f2
commit a3fdecbbcb
3 changed files with 75 additions and 1 deletions
+54
View File
@@ -105,6 +105,60 @@ func RedactEvidence(text string) string {
return text
}
// RedactEvidenceJSON redacts one JSON record and keeps it one valid JSON
// record: sensitive keys and credentials inside string values are replaced as
// in [RedactEvidence], but nothing is truncated and no line break is added,
// so a retained JSONL stream stays parseable however long a record is. A line
// that is not JSON falls back to [RedactEvidence].
func RedactEvidenceJSON(line []byte) string {
var data any
decoder := json.NewDecoder(strings.NewReader(string(line)))
decoder.UseNumber()
if !json.Valid(line) || decoder.Decode(&data) != nil {
return RedactEvidence(string(line))
}
redactText := func(text string) string {
text = credentialURL.ReplaceAllString(text, "${1}[REDACTED]@")
return credentialText.ReplaceAllStringFunc(text, func(match string) string {
parts := credentialText.FindStringSubmatch(match)
if parts[1] != "" {
return parts[1] + "[REDACTED]"
}
if parts[2] != "" {
return parts[2] + "[REDACTED]"
}
return "[REDACTED]"
})
}
var clean func(any) any
clean = func(v any) any {
switch x := v.(type) {
case map[string]any:
for k, v := range x {
if sensitiveKey.MatchString(k) {
x[k] = "[REDACTED]"
} else {
x[k] = clean(v)
}
}
return x
case []any:
for i, v := range x {
x[i] = clean(v)
}
return x
case string:
return redactText(x)
}
return v
}
encoded, err := json.Marshal(clean(data))
if err != nil {
return RedactEvidence(string(line))
}
return string(encoded)
}
// TruncateRunes is the exported form of truncateRunes. Callers outside the stage
// package that must bound text (for example the one-line summariser) reuse this
// single UTF-8-safe truncation instead of re-implementing rune handling.