diff --git a/CHANGELOG.md b/CHANGELOG.md index 72d27b81..599ae179 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- **A catalog's digest survives its object key order** (`activeagent`). The + canonical document sorted the keys of a scenario's extension fields only, + so the same catalog written with the objects it otherwise carries — the + catalog's, a product's and a set's extension fields, their `criteria`, + `pass` bars and `judge`, and a scenario's `params` — in another order + hashed differently. Catalog documents live in a JSON column, which hands + keys back in its own order (jsonb sorts them), so re-importing a document + the dashboard itself exported could preview as a change and write a new + digest. Every object in the document is now canonicalized, as + `docs/framework/evaluations.md` already said it was. `Catalog#to_h` is + unchanged, and a document that was already in canonical order keeps its + digest. - **RubyLLM totals count cached tokens** (`activeagent`). ruby_llm reports input apart from cache reads and writes, and the provider's `usage.total_tokens` fell back to input plus output, so a prompt that diff --git a/lib/active_agent/evals/catalog.rb b/lib/active_agent/evals/catalog.rb index bb0a9a3c..c5e2c624 100644 --- a/lib/active_agent/evals/catalog.rb +++ b/lib/active_agent/evals/catalog.rb @@ -176,7 +176,7 @@ def to_h "name" => name, "description" => description, "products" => products.map { |product| product_document(product) } - }.merge(metadata)) + }.merge(canonical(metadata))) end def to_yaml @@ -389,10 +389,10 @@ def product_document(product) "name" => product.name, "description" => product.description, "agent" => product.agent, - "criteria" => product.criteria, - "pass" => product.pass, + "criteria" => canonical(product.criteria), + "pass" => canonical(product.pass), "sets" => product.sets.map { |set| set_document(set) } - }.merge(product.metadata)) + }.merge(canonical(product.metadata))) end def set_document(set) @@ -400,32 +400,37 @@ def set_document(set) "key" => set.key, "name" => set.name, "description" => set.description, - "judge" => set.judge, - "criteria" => set.criteria, - "pass" => set.pass, + "judge" => canonical(set.judge), + "criteria" => canonical(set.criteria), + "pass" => canonical(set.pass), "scenarios" => set.scenarios.map { |scenario| scenario_document(scenario) } - }.merge(set.metadata)) + }.merge(canonical(set.metadata))) end def scenario_document(scenario) compact({ "key" => scenario.key, "prompt" => scenario.prompt, - "expect" => scenario.expectations, + "expect" => canonical(scenario.expectations), "notes" => scenario.notes, "tags" => scenario.tags, - "params" => scenario.params, + "params" => canonical(scenario.params), "production_only" => (true if scenario.production_only?) - }).merge(canonical_metadata(scenario.metadata.to_h.deep_stringify_keys.except(*SCENARIO_KEYS))) + }).merge(canonical(scenario.metadata.to_h.deep_stringify_keys.except(*SCENARIO_KEYS))) end # JSON object storage may reorder keys; array order remains meaningful. - def canonical_metadata(value) + # Every object the canonical document carries goes through here — + # including the catalog's, a product's and a set's own extension fields, + # their criteria, pass bars and judge, and a scenario's params — so the + # digest fingerprints the same document whatever order the storage + # handed the keys back in. + def canonical(value) case value when Hash - value.keys.sort.to_h { |key| [ key, canonical_metadata(value[key]) ] } + value.keys.sort.to_h { |key| [ key, canonical(value[key]) ] } when Array - value.map { |entry| canonical_metadata(entry) } + value.map { |entry| canonical(entry) } else value end diff --git a/test/evals/catalog_metadata_test.rb b/test/evals/catalog_metadata_test.rb index 057fdd62..ab613534 100644 --- a/test/evals/catalog_metadata_test.rb +++ b/test/evals/catalog_metadata_test.rb @@ -58,6 +58,54 @@ def scenario_document(catalog) assert_not_equal original.digest, ActiveAgent::Evals::Catalog.new(document(reordered)).digest end + # A catalog document is stored in a json column (jsonb on PostgreSQL), which + # may hand every object's keys back in another order, and the digest + # fingerprints the canonical document. Only a scenario's extension fields + # were canonicalized, so the same catalog written with its catalog, product, + # set, criteria, pass-block, judge or params object keys in another order + # read as two different catalogs: a re-import of the file the dashboard + # itself exported looked like a change. + test "every object the document carries is canonical, not just a scenario's extensions" do + pairs = { + "catalog metadata" => [ + { "catalog" => "support", "owner" => "platform", "region" => "eu", "products" => [ { "key" => "triage" } ] }, + { "catalog" => "support", "region" => "eu", "owner" => "platform", "products" => [ { "key" => "triage" } ] } + ], + "product metadata" => [ + { "catalog" => "support", "products" => [ { "key" => "triage", "lane" => "alpha", "tier" => "gold" } ] }, + { "catalog" => "support", "products" => [ { "key" => "triage", "tier" => "gold", "lane" => "alpha" } ] } + ], + "product criteria" => [ + { "catalog" => "support", "products" => [ { "key" => "triage", "criteria" => { "shared" => [], "strict" => [] } } ] }, + { "catalog" => "support", "products" => [ { "key" => "triage", "criteria" => { "strict" => [], "shared" => [] } } ] } + ], + "product pass" => [ + { "catalog" => "support", "products" => [ { "key" => "triage", "pass" => { "threshold" => 0.8, "min_pass_rate" => 0.9 } } ] }, + { "catalog" => "support", "products" => [ { "key" => "triage", "pass" => { "min_pass_rate" => 0.9, "threshold" => 0.8 } } ] } + ], + "set judge" => [ + { "catalog" => "support", "products" => [ { "key" => "triage", "sets" => [ { "key" => "smoke", "judge" => { "kind" => "rules", "model" => "m" } } ] } ] }, + { "catalog" => "support", "products" => [ { "key" => "triage", "sets" => [ { "key" => "smoke", "judge" => { "model" => "m", "kind" => "rules" } } ] } ] } + ], + "scenario params" => [ + { "catalog" => "support", "products" => [ { "key" => "triage", "sets" => [ { "key" => "smoke", "scenarios" => [ { + "key" => "hello", "prompt" => "Say hello.", "params" => { "locale" => "en", "tone" => "terse" } + } ] } ] } ] }, + { "catalog" => "support", "products" => [ { "key" => "triage", "sets" => [ { "key" => "smoke", "scenarios" => [ { + "key" => "hello", "prompt" => "Say hello.", "params" => { "tone" => "terse", "locale" => "en" } + } ] } ] } ] } + ] + } + + pairs.each do |place, (first_document, second_document)| + first = ActiveAgent::Evals::Catalog.new(first_document) + second = ActiveAgent::Evals::Catalog.new(second_document) + + assert_equal first.to_h, second.to_h, place + assert_equal first.digest, second.digest, "#{place} keys in another order must read as the same catalog" + end + end + test "a literal metadata extension can have any YAML value without becoming the serialization container" do [ nil, false, 0, [], "opaque" ].each do |value| catalog = ActiveAgent::Evals::Catalog.new(document({ "metadata" => value }))