climate_ref_core.regression
#
Regression-baseline primitives for diagnostic test cases.
This package holds the building blocks for the two-bundle regression model, where we capture and commit a small, sanitised committed bundle of artefacts for a test case. These committed bundles are tracked in git and form the regression baseline for the test case.
We also snapshot the native files persisted by the execution. These files are typically large and not always portable, so we keep them out of git and refer to them by their sha256 digest in the committed bundle. These data will be able to be fetched from an object store in CI and replayed locally for debugging.
COMMITTED_BUNDLE_FILES = ('series.json', 'diagnostic.json', 'output.json')
module-attribute
#
The committed CMEC artefacts tracked in git.
Their digests are tracked in :attr:Manifest.committed.
SCHEMA_VERSION = 2
module-attribute
#
Current manifest schema version.
Action
#
Bases: Enum
The verification action the CI gate selects for a single test case.
Source code in packages/climate-ref-core/src/climate_ref_core/regression/gate.py
FAIL = 'fail'
class-attribute
instance-attribute
#
The change is not permissible (unauthorised baseline change or bad version).
REPLAY = 'replay'
class-attribute
instance-attribute
#
Replay the cached native baseline and compare to the committed bundle.
SKIP = 'skip'
class-attribute
instance-attribute
#
Nothing relevant changed, or the case is not under regression management.
GateDecision
#
The gate's decision for one test case: an :class:Action and why.
Source code in packages/climate-ref-core/src/climate_ref_core/regression/gate.py
Manifest
#
The on-disk manifest for a test case regression bundle.
Serialised as manifest.json with stable key ordering and a trailing newline,
so repeated dumps are byte-identical.
Source code in packages/climate-ref-core/src/climate_ref_core/regression/manifest.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | |
catalog_hash = None
class-attribute
instance-attribute
#
Hash of the test case input catalog.yaml (its _metadata.hash) that produced this baseline.
This couples the baseline to its inputs.
The CI gate fails a case whose live catalog hash no longer matches this value.
committed
instance-attribute
#
Digests of committed regression JSON artefacts: {relpath: sha256}.
diagnostic_version
instance-attribute
#
Author-declared Diagnostic.version at mint time.
Monotonic. The gate fails a case whose in-code Diagnostic.version exceeds this value
(a stale baseline that must be re-minted), and one whose in-code version drops below it.
native
instance-attribute
#
Digests of curated native output files: {relpath: NativeEntry}.
schema
instance-attribute
#
Manifest schema version; equals :data:SCHEMA_VERSION for current manifests.
test_case_version
instance-attribute
#
Monotonic, author-bumped version coupling the bundle to its native outputs.
dump(path)
#
Write the manifest to manifest.json.
Keys are stably ordered (sort_keys=True) and a trailing newline is added,
so dump followed by load round-trips byte-identically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to write the manifest to. |
required |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/manifest.py
load(path)
classmethod
#
Load a manifest from manifest.json.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to the manifest file. |
required |
Returns:
| Type | Description |
|---|---|
Manifest
|
The parsed manifest. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the manifest is missing required keys or has malformed native entries (e.g. hand-edited or written by an incompatible version). |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/manifest.py
loads(text, *, source='<string>')
classmethod
#
Parse a manifest from its JSON text.
Used when the manifest does not live on disk at parse time,
e.g. when reading the base-branch copy via git show for the CI coupling gate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The manifest JSON. |
required |
source
|
str
|
A label for the text's origin, used in error messages. |
'<string>'
|
Returns:
| Type | Description |
|---|---|
Manifest
|
The parsed manifest. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the manifest is missing required keys or has malformed native entries (e.g. hand-edited or written by an incompatible version). |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/manifest.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | |
NativeEntry
#
A single curated native output file recorded in the manifest.
Source code in packages/climate-ref-core/src/climate_ref_core/regression/manifest.py
NativeStore
#
Content-addressed store for native baseline blobs, keyed by sha256 hex digest.
A local store (a file:// URL or a bare path) always reads and writes without credentials.
A remote store reads anonymously through :mod:pooch,
and writes only when it is given an :class:S3WriteConfig.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
Where blobs are read from: an |
required | |
cache_dir
|
Local directory pooch caches anonymous remote downloads in. A local store does not use it. |
required | |
write
|
The S3 endpoint, bucket and credentials a remote store writes with.
|
required |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 | |
root = field(init=False)
class-attribute
instance-attribute
#
The local store root, or None when this store is remote.
fetch(digest, dest)
#
Fetch the blob identified by digest and write it to dest.
The sha256 of the written file is verified to equal digest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
digest
|
str
|
The sha256 hex digest of the blob to fetch. |
required |
dest
|
Path
|
Destination path to write the blob to. Parent directories are created if they do not exist. |
required |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the blob is not present in the store. |
ValueError
|
If the fetched blob's sha256 does not match |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
has(digest)
#
Return True if the blob identified by digest is available.
A local store checks its canonical path,
a writable remote store HEADs the object,
and an anonymous remote store checks the pooch cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
digest
|
str
|
The sha256 hex digest of the blob. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
preflight()
#
Verify the store is reachable and usable before relying on it.
A local store's root is created if needed and checked for writability.
A writable remote store performs a cheap authenticated HEAD on a sentinel key,
which is expected to be absent:
a 404 means the request authenticated and the store is usable,
while 401 / 403 become actionable errors,
so a misconfigured credential is caught before the (slow) diagnostic run rather than after.
head_object is used rather than head_bucket,
so the check works with least-privilege, object-scoped tokens.
An anonymous remote store has nothing to verify up front. It has no credentials, and every read is hash-checked per blob.
Raises:
| Type | Description |
|---|---|
NativeStoreUnavailableError
|
If the store cannot be reached or used, with an operator-facing message. |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
put(path)
#
Store the file at path and return its sha256 hex digest.
The blob is content-addressed, so storing one that is already present is skipped and re-minting is cheap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to the file to store. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The sha256 hex digest of the stored blob. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If this is an anonymous remote store, which cannot write. |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
NativeStoreUnavailableError
#
Bases: RuntimeError
Raised when a native store cannot be reached or used.
Covers rejected credentials, a missing bucket, or an unwritable local directory. The message is operator-facing and actionable (it names the env vars / path to check), so callers can surface it directly.
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
S3WriteConfig
#
Where a remote store writes, and how it authenticates.
Routing (endpoint_url and bucket) is non-secret and comes from the application config.
Credentials are resolved from the environment at build time and are never persisted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint_url
|
S3 API endpoint for the bucket's account, without the bucket
(e.g. |
required | |
bucket
|
Name of the R2 bucket (e.g. |
required | |
access_key_id
|
R2 access-key id, or |
required | |
secret_access_key
|
R2 secret-access-key, or |
required | |
profile
|
Named AWS/R2 profile to authenticate with, or |
required |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
__attrs_post_init__()
#
Fail fast at construction (mint startup) when the routing config is missing.
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
client()
#
Return the cached boto3 S3 client for this endpoint and these credentials.
Tolerance
#
Float comparison tolerance for bundle regression checks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rtol
|
Relative tolerance — the allowed proportional difference between expected
and actual float values (default |
required | |
atol
|
Absolute tolerance — the floor difference always permitted,
regardless of magnitude (default |
required |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/compare.py
assert_bundle_regression(expected_path, actual_path, *, slug, tol=Tolerance(), replacements)
#
Assert that a regenerated committed-bundle JSON file matches the committed copy.
Algorithm:
- Byte-equal fast path — if the raw bytes of both files are identical, return immediately.
- Parse both files as JSON.
- Rewrite both dict keys and leaf string values in the regenerated
(
actual) document usingreplacements, applied longest-key-first so that a shorter placeholder cannot pre-empt a longer overlapping one. - Call :func:
compare_json_contentwith the given tolerance. - Raise
AssertionErrorwith the full mismatch list and a remediation hint if any mismatches are found.
The replacements map follows the convention used throughout the testing
infrastructure: keys are real runtime values (absolute paths, recipe-dir
timestamps), values are the committed-bundle placeholders (e.g.
"<OUTPUT_DIR>", "<TEST_DATA_DIR>", "<RECIPE_RUN>").
Only the actual document is rewritten: the committed expected file is
assumed to already contain placeholders, which
:func:~climate_ref_core.regression.capture.write_committed_bundle guarantees
at capture time. A hand-edited baseline with raw paths will surface as ordinary
value mismatches.
Both <OUTPUT_DIR> and <RECIPE_RUN> participate in dict-KEY rewriting
because ESMValTool's output.json embeds absolute paths as object keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expected_path
|
Path
|
Path to the committed (on-disk) bundle file containing placeholders. |
required |
actual_path
|
Path
|
Path to the regenerated bundle file containing real runtime paths. |
required |
slug
|
str
|
Diagnostic slug used in error messages. |
required |
tol
|
Tolerance
|
Float comparison tolerance. |
Tolerance()
|
replacements
|
dict[str, str]
|
Mapping of |
required |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If the bundles differ beyond tolerance after sanitisation. |
Notes
If expected_path does not exist (a legacy regression without a committed bundle),
the comparison is skipped silently and the function returns.
Source code in packages/climate-ref-core/src/climate_ref_core/regression/compare.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |
build_native_snapshot(base_dir, relpaths)
#
Record a sha256 + size snapshot of each persisted native file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_dir
|
Path
|
The per-execution results directory the relpaths are resolved against. |
required |
relpaths
|
list[Path]
|
The persisted files (relative to |
required |
Returns:
| Type | Description |
|---|---|
dict[str, NativeEntry]
|
Mapping of POSIX relpath -> :class: |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/capture.py
build_native_store(config, *, writable)
#
Build a :class:NativeStore from a native-store config object.
Accepts any object that exposes url, cache_dir, s3_endpoint_url and bucket
(satisfying :class:_NativeStoreConfigProtocol), so callers pass config.native_store
rather than the full :class:~climate_ref.config.Config.
With writable=False the returned store is anonymous and credential-free,
which suits the CI read and replay paths.
With writable=True and a remote URL the S3 endpoint and bucket come from the config,
and authentication is read from the environment
(REF_NATIVE_STORE_ACCESS_KEY_ID / REF_NATIVE_STORE_SECRET_ACCESS_KEY,
else REF_NATIVE_STORE_PROFILE, else boto3's default chain),
so secrets never live in the persisted config.
A local store is always readable and writable, so writable makes no difference to it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
_NativeStoreConfigProtocol
|
A config object providing |
required |
writable
|
bool
|
Whether the store must be able to write. |
required |
Returns:
| Type | Description |
|---|---|
NativeStore
|
The configured store. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the URL scheme is unrecognised, or a writable remote store is requested without an S3 endpoint / bucket configured. |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/store.py
compare_json_content(expected, actual, *, tol, path='')
#
Recursively compare two parsed JSON values with float tolerance.
Rules:
- Floats: compared with relative tolerance tol.rtol
and absolute tolerance tol.atol.
- Ints, strings, bools, None: exact equality.
- Lists: element-by-element, same length required.
- Dicts: key sets must match; values compared recursively.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expected
|
Any
|
The reference (committed) parsed JSON value. |
required |
actual
|
Any
|
The regenerated parsed JSON value. |
required |
tol
|
Tolerance
|
Float comparison tolerance. |
required |
path
|
str
|
Dot-/bracket-notation path prefix for error messages (empty at top level). |
''
|
Returns:
| Type | Description |
|---|---|
list[str]
|
A list of human-readable mismatch descriptions. An empty list means the values are equivalent within tolerance. |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/compare.py
compute_committed_digests(regression_dir)
#
Compute sha256 digests of the committed regression JSON artefacts.
The digests are taken over the bytes exactly as they sit on disk (placeholder text included), so a CI recompute is deterministic. Only files that exist are included.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
regression_dir
|
Path
|
The test case |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Mapping of |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/manifest.py
decide_coupling(manifest, base_manifest, *, extraction_changed=False, committed_integrity_ok=True, catalog_integrity_ok=True, code_diagnostic_version=None)
#
Decide how CI should verify a single test case's regression baseline.
The decision is pure: it performs no I/O. All on-disk reality is summarised by its arguments, namely the two manifests, whether the diagnostic's extraction code changed, and whether the committed bundle on disk still matches the current manifest's digests.
The gate fails if any state cannot be positively verified. A deleted managed manifest, a committed bundle that drifted from its manifest, an input catalog that drifted from its manifest is a failure rather than a silent skip.
Changes to the native baseline are not failures.
This is due to the workflow for minting requires credentials.
This means fork contributors cannot author or edit native blobs.
replay is therefore only selected when native blobs actually exist to replay.
An absent or removed native baseline downgrades to skip, never fail.
See the module docstring for the meaning of each :class:Action.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manifest
|
Manifest | None
|
The current |
required |
base_manifest
|
Manifest | None
|
The |
required |
extraction_changed
|
bool
|
Whether code that influences |
False
|
committed_integrity_ok
|
bool
|
Whether the committed bundle on disk matches the current manifest's Ignored when |
True
|
catalog_integrity_ok
|
bool
|
Whether the test case's input Always |
True
|
code_diagnostic_version
|
int | None
|
The diagnostic's current in-code |
None
|
Returns:
| Type | Description |
|---|---|
GateDecision
|
The gate's decision, pairing an :class: |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/gate.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | |
materialise_native(native, store, dest)
#
Materialise a native snapshot from a store into a destination directory.
For each (relpath, entry) the blob is fetched from store (keyed by its
sha256 digest) to dest / relpath, creating parent directories as needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
native
|
dict[str, NativeEntry]
|
Mapping of relpath -> :class: |
required |
store
|
NativeStore
|
A content-addressed :class: |
required |
dest
|
Path
|
The destination directory the snapshot is materialised into. |
required |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/capture.py
paths_under(changed_files, roots)
#
Return whether any changed file lies within one of the given directory roots.
A small helper for deriving the extraction_changed signal from a pull request's changed-file list.
Paths are compared textually as POSIX-style, repo-relative strings,
so callers must normalise both changed_files and roots to the same convention
(e.g. git diff --name-only output and a package source directory relative to the repo root).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
changed_files
|
Iterable[str]
|
Repo-relative paths changed in the pull request. |
required |
roots
|
Iterable[str]
|
Repo-relative directory prefixes to test against. A trailing slash is optional; an empty root never matches. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in packages/climate-ref-core/src/climate_ref_core/regression/gate.py
sha256_bytes(data)
#
sha256_file(path)
#
Compute the sha256 digest of a file.
Reuses :func:pooch.hashes.file_hash so the digest agrees with pooch elsewhere.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to the file to hash. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The hex-encoded sha256 digest. |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/manifest.py
verify_committed_integrity(manifest, regression_dir)
#
Check that the committed regression artefacts match the manifest digests.
Used by the CI integrity check. An empty return value means the bundle is intact.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manifest
|
Manifest
|
The manifest holding the expected committed digests. |
required |
regression_dir
|
Path
|
The test case |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
A list of human-readable mismatch descriptions; empty when everything matches. |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/manifest.py
write_committed_bundle(source_dir, regression_dir, *, placeholders)
#
Write the sanitised committed CMEC bundle into regression_dir.
Copies each committed artefact present in source_dir into regression_dir,
rewrites absolute paths to portable placeholders in place
(:meth:~climate_ref_core.output_files.PlaceholderMap.sanitise),
then canonicalises every committed JSON file -- rounding floats and redacting host/user CMEC
provenance into a deterministic on-disk form (:func:_canonicalise_committed_bundle).
When a committed artefact is absent from source_dir,
any stale copy left in regression_dir from a previous capture is removed so it is not re-digested.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_dir
|
Path
|
Directory holding the freshly persisted CMEC artefacts (the per-execution results directory). |
required |
regression_dir
|
Path
|
The destination |
required |
placeholders
|
PlaceholderMap
|
The placeholder map for this execution, already bound to the output directory via
:meth: |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
The committed digests |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in packages/climate-ref-core/src/climate_ref_core/regression/capture.py
sub-packages#
| Sub-package | Description |
|---|---|
| _quantise | Float quantisation for committed regression bundles. |
| capture | Capture of regression baselines from a diagnostic execution. |
| compare | Content comparison utilities for regression testing. |
| gate | CI coupling gate for test case regression bundles. |
| manifest | Manifest model and digest utilities for test case regression bundles. |
| store | Data store for native bundles. |