Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 19 additions & 14 deletions lib/active_agent/evals/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -389,43 +389,48 @@ 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)
compact({
"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
Expand Down
48 changes: 48 additions & 0 deletions test/evals/catalog_metadata_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }))
Expand Down