stderr 的 JSONL 是无主人在场的落盘面,却在此前原样落工具结果(result)、
子代理回复(text/summary)与上游报文,等于一次 tail 就能拷走整段对话。
本切片交付两件地基:
1) 写侧脱敏(trace.JSONRecorder)
- 新增 trace/span_props.go:safePropKeys 白名单与 projectSpanProps 投影,
读侧(safeSpanProps)与写侧共用同一张表、同一个投影函数;
- WriteSpan 落盘前经 trace.SanitizeRecord 投影:白名单键清洗,表外键只留
Digest(bytes/runes/sha256 前缀,收进 omitted),事件先 RedactEvent 再把
正文键折成 Digest。落到磁盘的只有元数据,正文一个字节都不留;
- 新增 trace/digest.go:Digest/DigestOf/DigestOfValue,给「脱敏后仍要能诊断」
一个构造上不含正文的取值形态(体积 + 指纹),两条投影都按类型放行它。
2) 共享上游错误体收敛 helper(给 M41/M42,禁止三家各写一份)
- trace.UpstreamError(upstream, status, contentType, body):只保留 status /
contentType / bodyLen / body sha256 前缀 / 上游 JSON 的 error.code /
error.type / 截断 128 rune 的 error.message,其余一律丢弃,响应体原文绝不
进 error 或日志;*UpstreamErrorInfo 提供 Error()/Attrs()/Unwrap()(errors.Is
可判 ErrUpstream 走降级链);
- trace.HostOf / trace.MaskQuery:日志里的 URL 只回来源,凭据类 query 与
fragment 值打码为 ***,user:pass@ 整段打码。
3) 剥掉三处正文上 span
- loop:tool span 不再放 result,改放 result_summary(与 tool_execution.end
帧同源、已脱敏截断)与 result_digest;
- orchestration:subagent span 不再放 text,subagent.done 事件不再放 summary,
改记 status/steps/chars/sha256;
- automations:AddRuleByName 不再 %v 整个 payload,改记 subject/type/bytes/sha256。
测试:以哨兵正文在落盘字节上断言(JSONL / slog 输出),并分别对「编排层收到的
原始 span」与「写侧投影」两层各钉一遍;UpstreamError 用回显 input 的 422 报文
与 HTML 错误页各钉一条;解析出/未解析出 JSON 都不回原文。以上均做过变异验证
(去掉保护即变红)。
验收:go build ./... + go vet ./... + go test -count=1 ./... 全绿;
trace/loop/orchestration 另跑 -race 全绿。
67 lines
2.9 KiB
Go
67 lines
2.9 KiB
Go
package trace
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"sync"
|
|
)
|
|
|
|
// Writer persists a completed span. Implementations must be safe for concurrent
|
|
// use, as WriteSpan may be called from several goroutines. A Writer such as
|
|
// *JSONRecorder provides structured local logging for the M9 panel.
|
|
type Writer interface {
|
|
// WriteSpan persists a single completed span and returns any encoding or
|
|
// write error.
|
|
WriteSpan(rec SpanRecord) error
|
|
}
|
|
|
|
// JSONRecorder writes each completed span as one line of JSON (JSONL) to an
|
|
// underlying io.Writer. It is safe for concurrent use.
|
|
//
|
|
// 🔴 它是**脱敏边界**,不是搬运工:stderr 的 JSONL 是无主人在场的落盘面(会被
|
|
// 日志采集、tail、粘贴进工单),所以 WriteSpan 在落盘前把记录投影一遍 ——
|
|
// props 走共享白名单(与出图同一张表,span_props.go),表外键只留 Digest;事件
|
|
// 先经 RedactEvent(凭据形状打码 + 截断),正文键的值再折成 Digest。这样即使上游
|
|
// 某天又往 span 里塞了正文(工具结果、子代理回复、上游报文),它也到不了磁盘上。
|
|
//
|
|
// 端口/文件格式不变:一行一个 span 对象,只是这一行里不再有正文。
|
|
type JSONRecorder struct {
|
|
mu sync.Mutex
|
|
enc *json.Encoder
|
|
}
|
|
|
|
// NewJSONRecorder builds a JSONRecorder that appends one JSON object per span to
|
|
// out.
|
|
func NewJSONRecorder(out io.Writer) *JSONRecorder {
|
|
return &JSONRecorder{enc: json.NewEncoder(out)}
|
|
}
|
|
|
|
// WriteSpan encodes rec as JSON followed by a newline. Concurrent calls are
|
|
// serialized so a single span object is never interleaved with another. rec 在
|
|
// 编码前先经 SanitizeRecord 投影 —— 落盘的永远是脱敏后的形态。
|
|
func (r *JSONRecorder) WriteSpan(rec SpanRecord) error {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
if err := r.enc.Encode(SanitizeRecord(rec)); err != nil {
|
|
return fmt.Errorf("trace: encode span %q: %w", rec.ID, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SanitizeRecord 返回 rec 的写侧形态:props 走白名单投影(表外键折成 Digest),
|
|
// events 逐条脱敏(凭据形状打码 + 截断 + 正文键折成 Digest)。它不改动调用方持有
|
|
// 的 rec 的其它字段(id/name/kind/status/时间),因此脱敏后的记录仍能按链路串起来。
|
|
//
|
|
// 导出是因为「写出去」的出口不止一个:stderr 的 JSONL 由 *JSONRecorder 负责,
|
|
// 而网关的 trace 环形缓冲(admin 可观测面)、离线导出等出口都拿同一份 SpanRecord。
|
|
// 它们应当复用**同一个**投影,而不是各写一份 —— 各写一份必然漂移,漂移的方向总是
|
|
// 「某个出口开始原样吐正文」。当前 HTTP 响应形状受 M40 红线冻结,那些出口尚未接入;
|
|
// 接入时应直接调用本函数。
|
|
func SanitizeRecord(rec SpanRecord) SpanRecord {
|
|
out := rec
|
|
out.Props = writeSpanProps(rec.Props)
|
|
out.Events = writeSpanEvents(rec.Events)
|
|
return out
|
|
}
|