feat(trace): M40 span 写侧脱敏 + 共享上游错误收敛 helper

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 全绿。
This commit is contained in:
AskaEth committed 2026-10-26 13:17:42 +08:00
1 parent 240b59708d
commit 003d12a712
18 files changed
+1612 -71

No files matched your search

+14 -3
View File
@@ -84,10 +84,18 @@ func TestRedactEventScrubsNestedValues(t *testing.T) {
// TestNodePropsAndLogAreRedacted asserts the published graph — both a tool node's
// result_summary prop and its log events — never carries a credential. This is the
// design/19 §3 red line at the last point before publication.
//
// M40 之后 result_summary 是**生产侧**写入的已脱敏摘要(loop/recordToolResult),
// 读侧只做白名单投影,不再从结果原文反推。因此这里同时钉住两件事:白名单键
// 的值仍被逐层清洗;而任何「结果原文」形态的 prop(表外的 result)根本不出图,
// 哪怕某个生产者又把它塞了回来。
func TestNodePropsAndLogAreRedacted(t *testing.T) {
t.Parallel()
rec := spanRec("t1", "step1", "tool.http", SpanKindTool, SpanStatusOK,
map[string]any{"result": map[string]any{"body": `{"api_key":"sk-abcdef0123456789"}`}})
map[string]any{
"result_summary": `{"api_key":"sk-abcdef0123456789"}`,
"result": map[string]any{"body": `{"api_key":"sk-abcdef0123456789"}`},
})
rec.Events = []Event{{
Type: "tool.done",
Props: map[string]any{
@@ -112,8 +120,11 @@ func TestNodePropsAndLogAreRedacted(t *testing.T) {
if !strings.Contains(string(blob), redactedMarker) {
t.Fatalf("published graph carries no redaction marker: %s", blob)
}
if got := node.Props["result_summary"]; !strings.Contains(got.(string), redactedMarker) {
t.Fatalf("result_summary prop = %v, want it scrubbed", got)
if got, ok := node.Props["result_summary"].(string); !ok || !strings.Contains(got, redactedMarker) {
t.Fatalf("result_summary prop = %v, want it scrubbed", node.Props["result_summary"])
}
if _, ok := node.Props["result"]; ok {
t.Fatalf("表外 prop result 出图了:%+v(读侧只投影白名单)", node.Props)
}
// The round's own input/output labels are scrubbed as well.
if in := nodeByID(g, "input").Label; strings.Contains(in, "abcdef1234567890") {