Skip to content

Canonicalize every object in a catalog's canonical document - #727

Draft
dajiaohuang wants to merge 1 commit into
activeagents:mainfrom
dajiaohuang:fix/catalog-digest-key-order
Draft

dajiaohuang wants to merge 1 commit into
activeagents:mainfrom
dajiaohuang:fix/catalog-digest-key-order

Conversation

@dajiaohuang

Copy link
Copy Markdown

A catalog's digest depends on its object key order

Catalog#digest is how two copies of a scenario catalog are compared without
diffing them, and docs/framework/evaluations.md states the contract the
digest is meant to hold:

Object key order does not change that digest, including after a JSON database
column reorders the keys. Array order is preserved and remains significant.

Only a scenario's extension fields were actually canonicalized. Every other
object the canonical document carries went into the YAML in whatever order it
arrived in, so the same catalog hashed differently depending on key order.

Path and wrong result

A catalog document is stored in a JSON column (jsonb on PostgreSQL, which
sorts object keys; json elsewhere). ScenarioCatalogImport writes it with
parse(document).digest (actionagent/app/services/action_agent/scenario_catalog_import.rb:55)
and ScenarioCatalog#refresh_document! rewrites it from the records
(scenario_catalog.rb:97). Because the digest was order-sensitive for these
objects, a document the dashboard exported and re-imported could compare
unequal and preview as a change, writing a new digest for a catalog whose
content never changed.

Affected objects, each proven below: the catalog's, a product's and a set's
extension fields, a product's criteria and pass, a set's criteria,
pass and judge, and a scenario's params. A scenario's extension fields
were already sorted, and its expect block is written from the Scenario
struct in a fixed shape, so those two were already stable.

Proof

The failing case, against pristine 9e802e7, using only the public
ActiveAgent::Evals::Catalog API:

require "active_agent/evals"

a = "catalog: support\nowner: platform\nregion: eu\nproducts:\n  - key: triage\n    lane: alpha\n    tier: gold\n"
b = "catalog: support\nregion: eu\nowner: platform\nproducts:\n  - key: triage\n    tier: gold\n    lane: alpha\n"

ca = ActiveAgent::Evals::Catalog.parse(a)
cb = ActiveAgent::Evals::Catalog.parse(b)

puts "to_h equal?  #{ca.to_h == cb.to_h}"
puts "digest equal #{ca.digest == cb.digest}"

Before, on base 9e802e7:

to_h equal?  true
digest equal false
digest A     765a853ac1e6b52d425746d031ed8e11f4e127eac21602e4f40fd818ecd3035f
digest B     3b483ac5070caa65fd6e775fc1097d18849ffa39abca5456b81d7a9792f4d8d3

After:

to_h equal?  true
digest equal true
digest A     765a853ac1e6b52d425746d031ed8e11f4e127eac21602e4f40fd818ecd3035f
digest B     765a853ac1e6b52d425746d031ed8e11f4e127eac21602e4f40fd818ecd3035f

The regression test is the discriminating check, run on both sides with the
same command and the same file:

bundle exec ruby -Itest test/evals/catalog_metadata_test.rb
  • base 9e802e7, library reverted, test unchanged: 1 failures
    (catalog metadata keys in another order must read as the same catalog, two
    different 64-hex digests), 9 runs, 41 assertions
  • with the fix: 9 runs, 51 assertions, 0 failures, 0 errors, 0 skips

The fix

lib/active_agent/evals/catalog.rb: the canonical-document writer passed the
catalog's, a product's and a set's extension fields, their criteria, pass
and judge, and a scenario's params straight through, while
canonical_metadata sorted only a scenario's extensions. Those call sites now
go through that same step, renamed canonical since it is no longer about
metadata alone. Nothing else changes: the sort was left as keys.sort (every
object reaching it has already been deep-stringified by Catalog#initialize),
Catalog#to_h is unchanged, and a document already written in canonical order
keeps its digest byte for byte.

What I validated

  • test/evals/catalog_metadata_test.rb, catalog_test.rb,
    standalone_load_test.rb, suite_test.rb, pass_thresholds_test.rb,
    scenario_parser_test.rb, report_test.rb, test/cli/cli_test.rb —
    230+ runs green on the final head.
  • The dashboard side that consumes the digest: actionagent/test/'s
    scenario_catalog_test.rb, catalog_scenario_metadata_test.rb,
    catalog_pass_bars_test.rb, catalog_product_criteria_test.rb,
    scenario_catalog_entries_api_test.rb, scenario_catalogs_api_test.rb —
    green. A wider 269-run sweep of the catalog, evals and CLI files was green
    too.
  • The before/after runs above, same command, same test file.

What I did not do

  • I did not run the full bin/test suite to completion on the final head. I
    ran it once on the pristine base (before the change) and it reported 12
    failures, all of which I traced to this checkout's line endings
    (core.autocrlf had rewritten the generator templates to CRLF and converted
    a binary PDF fixture); after re-checking out with LF, the two affected files
    passed, so I recorded them as environmental rather than related to this
    change.
  • I did not run rubocop: it is not in the rails8.gemfile bundle in this
    checkout (can't find executable rubocop for gem rubocop), so lint is
    unverified here and will be checked by CI's lint job.
  • I did not test against PostgreSQL/jsonb directly. The jsonb key-sorting
    behaviour is the motivation for the fix, not something this change relies
    on: the test proves order-independence with plain Ruby hashes, which is the
    stronger statement.
  • No CHANGELOG/docs behaviour change was needed beyond the entry added under
    Unreleased → Fixed, because the docs already described the behaviour this PR
    restores.

Catalog#to_yaml writes a canonical document whose digest is how a catalog
is compared, and docs/framework/evaluations.md says object key order does
not change that digest. Only a scenario's extension fields were sorted,
though, so the same catalog written with any other object's keys in another
order hashed differently.

A catalog document lives in a JSON column, which hands keys back in its own
order -- jsonb sorts them -- so a document the dashboard exported and
re-imported could preview as a change and write a new digest.

Route the catalog's, a product's and a set's extension fields, their
criteria, pass bars and judge, and a scenario's params and expect through the
same canonicalizing step the scenario extensions already used.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant