Reconcile experiment variant counts against declared variants before computing balance stats - #58719
Merged
Merged
Conversation
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix stale variant labels in experiment state counts
Reconcile experiment variant counts against declared variants before computing balance stats
Sep 5, 2026
pelikhan
marked this pull request as ready for review
September 5, 2026 04:50
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The analysis still exposes the stale unreconciled total through ExperimentAnalysis.TotalRuns.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Reconciles persisted experiment counts with current variant declarations to prevent stale labels from skewing analysis.
Changes:
- Filters undeclared variant counts and recomputes totals.
- Applies reconciliation to displayed details and balance statistics.
- Adds regression and helper tests.
File summaries
| File | Description |
|---|---|
pkg/cli/experiments_command.go |
Reconciles loaded experiment details. |
pkg/cli/experiments_command_test.go |
Tests detail reconciliation. |
pkg/cli/experiments_analyze_statistics.go |
Filters counts and recalculates statistical totals. |
pkg/cli/experiments_analyze_statistics_test.go |
Tests stale-count exclusion. |
Review details
- Files reviewed: 4/4 changed files
- Comments generated: 1
- Review effort level: Balanced
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+244
to
+248
| total := exp.Total | ||
| if cfg != nil && len(cfg.Variants) > 0 { | ||
| total = sumVariantCounts(variantCounts) | ||
| } | ||
| a.Variants = buildVariantAnalyses(total, variantCounts, variantNames, expectedPcts, a.MinSamples, graderObservations) |
Contributor
|
🎉 This pull request is included in a new release. Release: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Persisted
state.Counts[name]keys are never reconciled against a workflow's currentexperiments.<name>.variantslist. When a variant set is renamed (e.g. model identifiers), stale labels from earlier runs stay mixed in with current ones forever, inflating the totals used by the chi-square balance test and skewing it toward "imbalanced" even when the real split is close to even.Root cause
experimentVariantCountsonly added missing declared variants with zero counts — it never dropped stale keys no longer incfg.Variants.exp.Total(which includes stale counts) against filtered per-variant counts, so expected vs. observed sums never matched.Changes
pkg/cli/experiments_analyze_statistics.gofilterDeclaredVariantCounts: drops any variant keys not present in a declared variants list.experimentVariantCountsnow always filters stale keys via this helper (previously only added missing declared ones).totalis now recomputed from the reconciled counts instead of the raw, possibly stale-inflatedexp.Total.pkg/cli/experiments_command.goreconcileExperimentDetailsWithConfigs, invoked inRunExperimentsAnalyze, which filtersExperimentDetails.Experiments[*].Variantsagainst the loaded frontmatter configs and recomputesTotal— so the displayed variant table and the computed statistics both reflect only currently-declared variants. Stale drops are logged once per experiment.Example
Scope note
deriveLastSelectedVariantinaudit_report_experiments.go(a legacy fallback heuristic for old state files without a run ledger) was left unchanged — it operates on a single downloaded run's artifact with no access to the workflow's frontmatter config, and plumbing that through would touch many unrelated audit/log call sites disproportionate to this fix.