diff --git a/CHANGELOG.md b/CHANGELOG.md index ddfe9c15..47b6587d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -71,6 +71,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Copy fixture contents when `context.repo_fixture` names a directory symlink in the `none` and OpenSandbox runtimes. Local uploads no longer fail with a directory read error, and OpenSandbox no longer silently uploads an empty fixture. +- Reject a `rule_based` rule item that sets more than one matcher. The judge + evaluates one matcher per item and ignored the rest, so an item with + `output_contains`, `exit_code` and `files_exist` checked only the output and + could pass with a failed exit and a missing file. Separate success items + must all pass. Each matching failure item independently fails the case. - Reject a `rule_based` matcher that is present but holds no values to match. `output_contains: {all: []}`, `output_matches: {}`, `turn_response_contains` without `contains_all`/`contains_any`, and `turn_response_not_contains` diff --git a/docs/guide/writing-evals.md b/docs/guide/writing-evals.md index 020ef4cf..ccdfa335 100644 --- a/docs/guide/writing-evals.md +++ b/docs/guide/writing-evals.md @@ -1045,6 +1045,10 @@ judge: > **Evaluation order:** `failure` outranks `success`. If any `failure` rule matches, the case fails immediately. Otherwise every `success` rule must pass. +Each rule item accepts one matcher. Older versions evaluated only the first recognized matcher in an item. An item with multiple matchers now fails validation. + +To require several success conditions, put each matcher in its own `success` item. Split a `failure` item only if each condition should independently fail the case. If failure requires both conditions, express them together in a single check, such as a script judge. + > **MCP tool names:** `tool_called`, `tool_called_in_turn`, and `tool_not_called_in_turn` compare `name` exactly with the tool name the Agent Engine recorded. Claude Code, Codex, and Qoder CLI record MCP tools as `mcp____`, where `` is the server `name` from `mcp.servers`. `output_contains` performs literal substring checks. Use `output_matches` when you need regular expressions; patterns use Go `regexp` syntax and invalid patterns fail config validation. A matcher that is present but lists no values is rejected by config validation: an empty `output_contains`, `output_matches`, `turn_response_contains`, or `turn_response_not_contains` would otherwise be reported as passing with zero checks. The same applies to the empty entry: an empty literal is a substring of every response and an empty pattern matches every response, so `all: [""]` cannot keep the false-positive pass. Whitespace is not empty — `["\n"]` checks that the output is a single line and `[" "]` rejects a double space, and both are compared as written. diff --git a/docs/zh/guide/writing-evals.md b/docs/zh/guide/writing-evals.md index 6421153f..ab41ef5d 100644 --- a/docs/zh/guide/writing-evals.md +++ b/docs/zh/guide/writing-evals.md @@ -897,6 +897,10 @@ judge: > **评估逻辑**:`failure` 优先于 `success`。任何一条 `failure` 规则匹配即立即失败;否则所有 `success` 规则必须全部满足才算通过。 +每个规则项只能包含一个匹配条件。旧版本只检查同一项中第一个可识别的条件。现在,包含多个匹配条件的规则项无法通过校验。 + +如果多个成功条件必须同时成立,请将每个条件写成单独的 `success` 项。只有在任一条件成立都应判定失败时,才把条件拆成多个 `failure` 项。如果两个条件同时成立才算失败,请在一次检查中组合判断,例如使用脚本裁判。 + > **MCP 工具名**:`tool_called`、`tool_called_in_turn` 和 `tool_not_called_in_turn` 会把 `name` 与 Agent Engine 记录的工具名做精确匹配。Claude Code、Codex 和 Qoder CLI 记录的 MCP 工具名格式为 `mcp____`,其中 `` 是 `mcp.servers` 中的 `name`。 `output_contains` 执行字面 substring 检查。需要正则表达式时使用 `output_matches`;pattern 使用 Go `regexp` 语法,非法 pattern 会在配置校验阶段失败。 matcher 存在但没有任何值时会被配置校验拒绝:空的 `output_contains`、`output_matches`、`turn_response_contains` 或 `turn_response_not_contains` 否则会以零检查被报告为通过。空条目(`""`)同样会被拒绝:空字面量是任何响应的子串,空 pattern 匹配任何响应,因此 `all: [""]` 无法继续制造假通过。空白本身不算空——`["\n"]` 可以断言输出只有一行,`[" "]` 可以拒绝双空格,两者都按原样比较。 diff --git a/internal/config/validator.go b/internal/config/validator.go index a8ed51f7..8cd48aea 100644 --- a/internal/config/validator.go +++ b/internal/config/validator.go @@ -303,6 +303,12 @@ func validateJudgeTypeAndLocalFields(judge JudgeConfig) []string { errs = append(errs, validateOutputMatchesRules("judge.failure", judge.Failure)...) errs = append(errs, validateRecognizableRules("judge.success", judge.Success)...) errs = append(errs, validateRecognizableRules("judge.failure", judge.Failure)...) + // Only the rule_based judge evaluates these rules; other judge types + // ignore them, so their matcher count does not matter. + if judge.Type == judgeTypeRuleBased { + errs = append(errs, validateSingleMatcherRules("judge.success", judge.Success)...) + errs = append(errs, validateSingleMatcherRules("judge.failure", judge.Failure)...) + } errs = append(errs, validateNonVacuousRules("judge.success", judge.Success)...) errs = append(errs, validateNonVacuousRules("judge.failure", judge.Failure)...) @@ -356,21 +362,54 @@ func validateRecognizableRules(field string, rules []Rule) []string { return errs } +// validateSingleMatcherRules rejects a rule that sets more than one matcher. +// The rule_based judge evaluates exactly one matcher per rule and ignores the +// rest, so an item holding output_contains, exit_code and files_exist checks +// only the output and can pass with a failed exit and a missing file. +func validateSingleMatcherRules(field string, rules []Rule) []string { + var errs []string + for i, rule := range rules { + if matchers := ruleMatcherNames(rule); len(matchers) > 1 { + errs = append(errs, fmt.Sprintf( + "%s[%d]: assertion sets more than one matcher (%s); use one matcher per rule item", + field, i, strings.Join(matchers, ", "))) + } + } + return errs +} + +// ruleMatcherNames lists the matcher fields a rule sets, in the order the +// runtime dispatcher checks them. +func ruleMatcherNames(rule Rule) []string { + var names []string + for _, matcher := range []struct { + name string + set bool + }{ + {"output_contains", rule.OutputContains != nil}, + {"output_matches", rule.OutputMatches != nil}, + {"exit_code", rule.ExitCode != nil}, + {"tool_called", rule.ToolCalled != nil}, + {"files_exist", len(rule.FilesExist) > 0}, + {"files_not_exist", len(rule.FilesNotExist) > 0}, + {"turn_response_contains", rule.TurnResponseContains != nil}, + {"turn_response_not_contains", rule.TurnResponseNotContains != nil}, + {"tool_called_in_turn", rule.ToolCalledInTurn != nil}, + {"tool_not_called_in_turn", rule.ToolNotCalledInTurn != nil}, + } { + if matcher.set { + names = append(names, matcher.name) + } + } + return names +} + // ruleHasRecognizableField reports whether the rule sets at least one matcher // field that the runtime dispatcher in internal/judge can evaluate. The empty // collections (for example files_exist: []) mirror the runtime dispatch, which // treats them the same as absent fields. func ruleHasRecognizableField(rule Rule) bool { - return rule.OutputContains != nil || - rule.OutputMatches != nil || - rule.ExitCode != nil || - rule.ToolCalled != nil || - len(rule.FilesExist) > 0 || - len(rule.FilesNotExist) > 0 || - rule.TurnResponseContains != nil || - rule.TurnResponseNotContains != nil || - rule.ToolCalledInTurn != nil || - rule.ToolNotCalledInTurn != nil + return len(ruleMatcherNames(rule)) > 0 } // validateNonVacuousRules rejects matchers that are present but hold no values diff --git a/internal/config/validator_test.go b/internal/config/validator_test.go index 3029beec..0b4a404f 100644 --- a/internal/config/validator_test.go +++ b/internal/config/validator_test.go @@ -132,6 +132,86 @@ func TestValidator_ValidateEvalConfig(t *testing.T) { wantErr: true, errMsg: "judge.success[0]: assertion has no recognizable matcher field", }, + { + name: "success rule with several matchers is rejected", + cfg: &EvalConfig{ + SchemaVersion: "v1alpha1", + Environment: Environment{Type: "none"}, + Engine: EngineConfig{ + Name: "claude_code", + Model: ModelConfig{ + Provider: "anthropic", + Name: "claude-sonnet-4-6", + }, + }, + Cases: CasesConfig{ + Files: []string{"evals/cases/test.yaml"}, + }, + Judge: JudgeConfig{ + Type: "rule_based", + Success: []Rule{{ + OutputContains: &OutputContainsRule{All: []string{"done"}}, + ExitCode: new(0), + FilesExist: []string{"report.md"}, + }}, + }, + }, + wantErr: true, + errMsg: "judge.success[0]: assertion sets more than one matcher (output_contains, exit_code, files_exist); use one matcher per rule item", + }, + { + name: "failure rule with several matchers is rejected", + cfg: &EvalConfig{ + SchemaVersion: "v1alpha1", + Environment: Environment{Type: "none"}, + Engine: EngineConfig{ + Name: "claude_code", + Model: ModelConfig{ + Provider: "anthropic", + Name: "claude-sonnet-4-6", + }, + }, + Cases: CasesConfig{ + Files: []string{"evals/cases/test.yaml"}, + }, + Judge: JudgeConfig{ + Type: "rule_based", + Failure: []Rule{{ + OutputContains: &OutputContainsRule{All: []string{"done"}}, + ExitCode: new(0), + FilesExist: []string{"report.md"}, + }}, + }, + }, + wantErr: true, + errMsg: "judge.failure[0]: assertion sets more than one matcher (output_contains, exit_code, files_exist)", + }, + { + name: "script judge ignores unused rules with several matchers", + cfg: &EvalConfig{ + SchemaVersion: "v1alpha1", + Environment: Environment{Type: "none"}, + Engine: EngineConfig{ + Name: "claude_code", + Model: ModelConfig{ + Provider: "anthropic", + Name: "claude-sonnet-4-6", + }, + }, + Cases: CasesConfig{ + Files: []string{"evals/cases/test.yaml"}, + }, + Judge: JudgeConfig{ + Type: "script", + ScriptPath: "evals/judge.sh", + Success: []Rule{{ + ExitCode: new(0), + FilesExist: []string{"report.md"}, + }}, + }, + }, + wantErr: false, + }, { name: "empty failure rule is rejected", cfg: &EvalConfig{