> **文档类型**:plan(阶段计划书)| **所属路线图**:[`README.md`](README.md) > **阶段**:P7 · 可观测、断言与评测:让「跑偏」可被发现 | **预估**:10–12 人日 | **本文件对应**:总方案 `../agent-platform-upgrade-plan.md` 的第 4.8 节 > **本阶段收编缺陷**:F-39、F-40、F-41(共 3 条,明细见文末附录) > 上一阶段:[`P6`](P6-工具治理.md) | 下一阶段:[`P8`](P8-生产化.md) # 4.8 P7 · 可观测、断言与评测:让"跑偏"可被发现 ## 目标 建立"**看得见 → 查得到 → 测得准 → 改得掉**"的闭环:全链路 trace、事件日志重放、关键节点断言、Bad Case 归集、评测集回归、内容安全。这是面试官 A "日志重放机制、关键节点断言、Bad Case 闭环复盘"三件套的完整落地。 ## 当前问题 | 问题 | 证据 | |---|---| | 日志是 `fmt.Printf`,无 run_id/step_id | `(*Runner).notifyPlugins` in `internal/infrastructure/adk/adapter.go`(`myTestPlugin`) | | 无 trace,跨 LLM/工具调用无法串联 | 全局 | | 无断言,模型答非所问无人发现 | 全局 | | 无评测,改动靠"感觉" | 零测试 | | 无内容安全,违规输出直接返回 | 全局 | | 无成本度量 | 全局 | ## 设计 ### 4.8.1 Trace 模型(OpenTelemetry) | Span | 关键属性 | |---|---| | `agent.run` | `run.id`, `session.id`, `agent.id`, `tenant.id`, `run.status`, `run.steps`, `tokens.total` | | `agent.step` | `step.kind`, `step.name`, `step.attempt`, `step.duration_ms` | | `llm.call` | `gen_ai.system`, `gen_ai.request.model`, `gen_ai.usage.input_tokens`, `output_tokens`, `cache.read_tokens` | | `tool.call` | `tool.name`, `tool.effect`, `tool.attempt`, `tool.result_bytes`, `tool.degraded_level` | | `memory.assemble` | `memory.tokens`, `memory.breakdown.*`, `memory.compacted` | | `memory.retrieve` | `memory.hits`, `memory.top_score`, `memory.latency_ms` | | `policy.evaluate` | `policy.rule_id`, `policy.effect` | 采样策略:默认 `parentbased_traceidratio=0.1`;**出错与超预算的 Run 强制 100% 采样**(尾部采样需 collector,简化为"错误路径升级采样率")。 ### 4.8.2 结构化日志规范 ```go logger.Info("tool call failed", zap.String("event", "tool.call.failed"), zap.String("run_id", rc.Run.ID), zap.String("step_id", step.ID), zap.String("session_id", rc.Run.SessionID), zap.String("tenant_id", rc.Run.TenantID), zap.String("tool", call.Name), zap.Int("attempt", attempt), zap.String("error_class", failure.Class), zap.Duration("duration", elapsed), ) ``` **统一字段**(漏一个就没法关联):`event`、`run_id`、`step_id`、`session_id`、`tenant_id`、`agent_id`。用 `zap` 的 `With()` 在 Run 入口一次性挂上,后续全继承。 **禁止**:日志里出现 prompt 全文、工具原始返回、任何密钥。需要时用 `_debug=true` 开关 + 脱敏。 ### 4.8.3 关键节点断言(Assertion) ```go // internal/domain/evals/assertion.go type Phase string // pre_llm | post_tool | post_step | post_run type Assertion interface { Name() string Phase() Phase Check(ctx context.Context, env *Env) error } type Env struct { Run *runtime.Run Step *runtime.Step Input string Output string Tools []ToolObservation Context contextasm.Bundle } ``` 内置断言(配置开关,默认只告警不阻断): | 断言 | 阶段 | 规则 | 失败后果 | |---|---|---|---| | `non_empty_output` | post_run | 最终答案非空且 ≥ 10 字符 | 阻断,返回兜底话术 | | `budget_respected` | post_run | steps ≤ max,tokens ≤ max | 告警(说明预算配小了) | | `tool_result_valid` | post_tool | 符合 OutputSchema | 告警 + 标记 | | `no_policy_violation` | post_tool | 未调用被拒工具 | 阻断 | | `must_include` | post_run | 必须包含配置的关键词/事实 | 阻断(可配) | | `must_exclude` | post_run | 不得包含违禁内容 | 阻断 | | `citation_present` | post_run | 知识库问答必须带引用 | 告警 | | `no_tool_loop` | post_step | 同一工具同参数连续 3 次 | 阻断(防死循环,P0 预算的第二道保险) | | `latency_budget` | post_run | p95 超过阈值 | 告警 | ```yaml # configs/agents/testAgent03.assertions.yaml assertions: - {name: non_empty_output, action: block} - {name: no_tool_loop, action: block} - {name: must_exclude, patterns: ["