1use serde_json::{Value as Json, json};
7
8pub const EVAL_SCHEMA_URL: &str =
10 "https://raw.githubusercontent.com/BimaPangestu28/graphrt/main/schema/eval-v1.schema.json";
11
12pub const BUILTIN_ASSERTIONS: &[(&str, &str)] = &[
14 ("equals", "The output (or `transform` path) equals `value`; non-string values compare as JSON."),
15 ("contains", "The output contains the `value` text."),
16 ("icontains", "Case-insensitive `contains`."),
17 ("contains-any", "The output contains at least one of the `value` texts."),
18 ("icontains-any", "Case-insensitive `contains-any`."),
19 ("contains-all", "The output contains every `value` text (score: fraction found)."),
20 ("icontains-all", "Case-insensitive `contains-all`."),
21 ("starts-with", "The output starts with the `value` text."),
22 ("regex", "The output matches the `value` regular expression (Rust syntax; `(?i)` for case-insensitive)."),
23 ("is-json", "The output is JSON; when `value` is a JSON Schema, it must also match it."),
24 ("json-schema", "The output is JSON matching the `value` JSON Schema."),
25 ("latency", "The run took at most `threshold` milliseconds."),
26 ("tokens", "The run used at most `threshold` tokens (`value`: total, prompt or completion)."),
27 ("trajectory:tool-used", "Agents called the `value` tool(s); `{name, min, max}` bounds the count."),
28 ("trajectory:tool-sequence", "Agents called the `value` tools in order (`{steps, mode: in_order|exact}`)."),
29 ("trajectory:tool-args-match", "A call of `value.name` had `value.args` (`mode: partial|exact`)."),
30 ("trajectory:step-count", "The number of tool calls, agent steps or node runs is within `{min, max}`."),
31 ("retrieval:recall-at-k", "Fraction of the `value` document ids in the top `k` knowledge-retrieval hits."),
32 ("retrieval:precision-at-k", "Fraction of the top `k` hits that are relevant (`value` ids)."),
33 ("retrieval:mrr", "Reciprocal rank of the first relevant hit in the top `k`."),
34 ("verdict", "The run is a judge whose verdict (or `score` >= `threshold`) equals the `value` label."),
35];
36
37pub const HOST_ASSERTIONS: &[(&str, &str)] =
40 &[("judge", "Run the `workflow` judge on the output and read its verdict or score.")];
41
42fn assertion_types() -> Vec<Json> {
43 BUILTIN_ASSERTIONS
44 .iter()
45 .chain(HOST_ASSERTIONS)
46 .flat_map(|(kind, _)| [Json::String((*kind).to_owned()), Json::String(format!("not-{kind}"))])
47 .collect()
48}
49
50pub fn eval_file_schema() -> Json {
52 json!({
53 "$schema": "https://json-schema.org/draft/2020-12/schema",
54 "$id": EVAL_SCHEMA_URL,
55 "title": "graphrt eval file",
56 "description": "Test cases and assertions for `graphrt eval`. The `tests` / `assert` / `defaultTest` shape follows Promptfoo.",
57 "type": "object",
58 "required": ["workflow"],
59 "properties": {
60 "description": { "type": "string" },
61 "workflow": { "type": "string", "description": "Workflow under test, relative to this file." },
62 "providers": { "type": "string", "description": "Model providers file (e.g. with `kind: replay` entries), relative to this file." },
63 "knowledge": { "type": "string", "description": "Knowledge base config for knowledge-retrieval nodes, relative to this file." },
64 "mcp": { "type": "string", "description": "MCP servers file, relative to this file." },
65 "plugins": { "type": "array", "items": { "type": "string" }, "description": "Plugin directories, relative to this file." },
66 "dataset": { "type": "string", "description": "JSONL file: one `{id?, vars, expected?, tags?, metadata?, assert?}` object per line." },
67 "defaultTest": { "$ref": "#/$defs/defaultTest" },
68 "tests": { "type": "array", "items": { "$ref": "#/$defs/test" } },
69 "gate": {
70 "type": "object",
71 "additionalProperties": false,
72 "properties": {
73 "pass_rate": { "type": "number", "minimum": 0, "maximum": 1, "default": 1, "description": "Minimum fraction of passing test runs (every trial counts); below it `graphrt eval` exits with code 5." },
74 "pass_hat_k": { "type": "number", "minimum": 0, "maximum": 1, "description": "Minimum mean pass^k over the test cases (all k trials pass); use with `--trials`." },
75 "k": { "type": "integer", "minimum": 1, "description": "k of pass@k and pass^k (default: the number of trials)." },
76 "baseline": {
77 "type": "object",
78 "additionalProperties": false,
79 "description": "Tolerances against `--baseline`: any regression beyond them fails the gate.",
80 "properties": {
81 "tolerance": { "type": "number", "minimum": 0, "maximum": 1, "default": 0.05, "description": "Largest allowed drop of a metric's mean score." },
82 "test_tolerance": { "type": "number", "minimum": 0, "maximum": 1, "default": 0, "description": "Largest allowed drop of a test's pass rate across its trials." },
83 "metrics": { "type": "object", "additionalProperties": { "type": "number", "minimum": 0, "maximum": 1 }, "description": "Per-metric tolerances." }
84 }
85 }
86 }
87 },
88 "calibration": {
89 "type": "object",
90 "additionalProperties": false,
91 "description": "Judge calibration: the workflow is a judge and each `expected` is a human label (pass / fail). Reports agreement, TPR, TNR and Cohen's kappa.",
92 "properties": {
93 "unknown": { "enum": ["exclude", "fail"], "default": "exclude", "description": "`unknown` verdicts: left out of the confusion matrix, or counted as fail." },
94 "threshold": { "type": "number", "minimum": 0, "maximum": 1, "default": 0.5, "description": "Score at which a score-only judge says pass." },
95 "min_kappa": { "type": "number", "minimum": -1, "maximum": 1, "description": "Gate: minimum Cohen's kappa." },
96 "min_tpr": { "type": "number", "minimum": 0, "maximum": 1, "description": "Gate: minimum true positive rate." },
97 "min_tnr": { "type": "number", "minimum": 0, "maximum": 1, "description": "Gate: minimum true negative rate." }
98 }
99 }
100 },
101 "patternProperties": { "^x-": {} },
102 "additionalProperties": false,
103 "$defs": {
104 "test": {
105 "type": "object",
106 "properties": {
107 "id": { "type": "string", "description": "Unique test id (default: `test-<n>`)." },
108 "description": { "type": "string" },
109 "vars": { "type": "object", "description": "Start inputs of the workflow." },
110 "expected": { "description": "Expected output; a test without assertions checks `equals: expected`. Available as `{{expected}}`." },
111 "tags": { "type": "array", "items": { "type": "string" } },
112 "metadata": { "type": "object" },
113 "threshold": { "type": "number", "minimum": 0, "maximum": 1, "description": "Pass when the weighted assertion score reaches this, instead of requiring every assertion." },
114 "assert": { "type": "array", "items": { "$ref": "#/$defs/assertion" } }
115 },
116 "patternProperties": { "^x-": {} },
117 "additionalProperties": false
118 },
119 "defaultTest": {
120 "type": "object",
121 "description": "Merged into every test: vars and metadata as defaults, tags and assertions prepended.",
122 "properties": {
123 "description": { "type": "string" },
124 "vars": { "type": "object" },
125 "tags": { "type": "array", "items": { "type": "string" } },
126 "metadata": { "type": "object" },
127 "threshold": { "type": "number", "minimum": 0, "maximum": 1 },
128 "assert": { "type": "array", "items": { "$ref": "#/$defs/assertion" } }
129 },
130 "additionalProperties": false
131 },
132 "assertion": {
133 "type": "object",
134 "required": ["type"],
135 "properties": {
136 "type": {
137 "type": "string",
138 "description": "Assertion type; any type can be negated with a `not-` prefix.",
139 "anyOf": [ { "enum": assertion_types() }, { "type": "string" } ]
140 },
141 "value": { "description": "Expected value; strings may reference test variables as `{{name}}`." },
142 "threshold": { "type": "number" },
143 "weight": { "type": "number", "minimum": 0, "default": 1, "description": "Weight in the test score; 0 reports the result without failing the test." },
144 "metric": { "type": "string", "description": "Name the score is aggregated under in the summary." },
145 "transform": { "type": "string", "description": "Path to the value under test, e.g. `output.field` or `outputs.text` (no JavaScript)." },
146 "k": { "type": "integer", "minimum": 1, "description": "retrieval:*: rank cut-off." },
147 "node": { "type": "string", "description": "retrieval:*: only this knowledge-retrieval node." },
148 "workflow": { "type": "string", "description": "judge: the judge workflow file, relative to this eval file." },
149 "inputs": { "type": "object", "description": "judge: extra start inputs of the judge (values may reference test variables)." },
150 "unknown": { "enum": ["fail", "exclude"], "default": "fail", "description": "judge: an `unknown` verdict fails, or is reported without scoring or gating." }
151 }
152 }
153 }
154 })
155}
156
157#[cfg(test)]
158mod tests {
159 #![allow(clippy::unwrap_used)]
160
161 use super::*;
162 use crate::assertion::AssertionRegistry;
163
164 #[test]
165 fn every_builtin_type_is_listed() {
166 let listed: Vec<&str> = BUILTIN_ASSERTIONS.iter().map(|(k, _)| *k).collect();
167 let registry = AssertionRegistry::with_builtins();
168 let registered: Vec<&str> = registry.kinds().collect();
169 assert_eq!(listed, registered);
170 }
171}