feat: role-aware health baselines (ADR-040)#29
Merged
Conversation
Add lib/health-baselines.ts — a dedicated module that computes and caches 30-day median baselines (completionRate, errorDensity, weeklyThroughput) per agent role. A new applyRoleBaseline() in lib/health.ts normalises each agent's raw sub-metrics relative to its role cohort's median before badge thresholds are applied, so a tester at 72% completion looks healthy if testers average 70% but alarming if they average 92%. Key design decisions: - BaselineNorms interface lives in health.ts (not health-baselines.ts) to avoid a circular import; structural typing lets RoleBaseline satisfy it - MIN_COHORT_SIZE = 3 guard falls back to flat thresholds for thin cohorts - BASELINE_TTL_MS = 5 min cache; BASELINE_WINDOW_MS = 30 days look-back - Feature-flagged via ROLE_BASELINES_ENABLED env var (default off) for safe A/B comparison and emergency rollback without a code revert - Explicit degradation ladder: cohort < 3 → flat thresholds; agent < 5 runs → skip; baseline metric null or 0 → skip; DB error → stale cache or null-metrics; new role → cohort = 0 → flat thresholds Files changed: - lib/health-baselines.ts (new) - lib/health.ts (+applyRoleBaseline, BaselineNorms, MIN_COHORT_SIZE_FOR_BASELINE) - lib/health-cache.ts (role lookup + feature flag integration) - lib/db/repositories/taskRunRepo.ts (+dbGetTaskRunsByRole) - __tests__/unit/health-baselines.test.ts (new, 34 tests) - __tests__/unit/health-cache.test.ts (extended, +4 integration tests) - context/decisions.md (ADR-040) - docs/agent-types.md (new role recalibration requirement documented) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.
Summary
lib/health-baselines.ts— a dedicated module that computes 30-day median baselines per agent role (medianCompletionRate,medianErrorDensity,medianWeeklyThroughput) with a 5-minute in-memory TTL cacheapplyRoleBaseline()tolib/health.tsthat normalises raw sub-metrics against the role's median before badge thresholds fire — a researcher at 72% completion looks healthy vs a 70% role norm, alarming vs a 92% normdbGetTaskRunsByRole()totaskRunRepo.tsfor efficient single-query role-cohort fetches (vs N per-agent queries)ROLE_BASELINES_ENABLED=truefeature flag inhealth-cache.ts— off by default for safe A/B rollout and zero-risk deploymentDegradation contract
Every failure mode has a defined fallback (no silent accuracy bugs):
null-metrics baseline → flat thresholdshasEnoughData: falseunchangedDesign decision:
BaselineNormsinhealth.tsapplyRoleBaselinelives inlib/health.ts. To avoid a circular import (health-baselines.ts → health.ts → health-baselines.ts), a minimalBaselineNormsinterface is defined inhealth.ts. TypeScript's structural typing meansRoleBaselinesatisfies it automatically — no explicitextendsneeded, no coupling.Feature flag
Set
ROLE_BASELINES_ENABLED=trueto activate. Recommended activation sequence:Prerequisites
fix/analytics-status-classification) mergedfeat/agent-health-metrics) mergedTest plan
__tests__/unit/health-baselines.test.ts— 34 new tests coveringmedianOf,computeRoleBaseline,getRoleBaselinecache TTL/invalidation, and allapplyRoleBaselinenormalisation rules and guards__tests__/unit/health-cache.test.ts— 4 new integration tests: flag off → raw metrics, flag on → normalised metrics, unknown agent → graceful degradation, cache hit after normalisation🤖 Generated with Claude Code