package engine // strict.go -- 严格模式配置. // // 精妙之处(CLEVER): 来自早期方案 inc-4977 事故的教训. // 安全评估时不能让引擎静默修改模型看到的上下文-- // 合成的 tool_result 可能影响评估结果的准确性. // // 测试/安全评估环境开启,生产环境关闭. // 每个"静默修复"的地方都检查严格模式:开了就崩溃,关了就修复+记录. // // 使用示例: // // // 安全评估环境 // engine := engine.New(&Config{ // StrictMode: &StrictMode{ // ToolResultPairing: true, // 配对异常 → panic // CompactFailure: true, // 压缩失败 → panic // NormalizerError: true, // 规范化异常 → panic // }, // }) // // // 生产环境 // engine := engine.New(&Config{ // StrictMode: nil, // 或不设置,所有异常静默修复+记录 // }) import ( "fmt" ) // StrictMode 严格模式配置. type StrictMode struct { ToolResultPairing bool // 消息配对异常:true=panic false=修复 CompactFailure bool // 压缩失败:true=panic false=降级 NormalizerError bool // 规范化异常:true=panic false=跳过 } // Check 在严格模式开启时 panic,否则记录 observer 事件. // // 精妙之处(CLEVER): 一行代码同时处理严格模式和可观测性. // 调用点只需要 `strictMode.Check("condition", enabled, observer, "detail")`, // 不需要自己写 if/else + observer.Event(). // 替代方案:每个调用点自己写 if strict { panic } else { observer.Event() }(重复代码 20+ 处). func (s *StrictMode) Check(condition string, enabled bool, observer EventObserver, detail string) { if enabled { panic(fmt.Sprintf("strict mode violation: %s. Detail: %s", condition, detail)) } if observer != nil { observer.Event("strict_mode_would_fail", map[string]any{ "condition": condition, "detail": detail, }) } } // CheckToolResultPairing 检查 tool_result 配对异常. func (s *StrictMode) CheckToolResultPairing(observer EventObserver, detail string) { s.Check("tool_result_pairing_mismatch", s.ToolResultPairing, observer, detail) } // CheckCompactFailure 检查压缩失败. func (s *StrictMode) CheckCompactFailure(observer EventObserver, detail string) { s.Check("compact_failure", s.CompactFailure, observer, detail) } // CheckNormalizerError 检查规范化异常. func (s *StrictMode) CheckNormalizerError(observer EventObserver, detail string) { s.Check("normalizer_error", s.NormalizerError, observer, detail) }