{"record":{"id":"9c3e5b786a4b965e","repo":"JuliusBrussee/caveman","slug":"where-cohort-arms-comparedunits-plus-exclu","errorCode":null,"errorMessage":"${where} cohort arms (${comparedUnits}) plus exclusions (${excluded}) do not account for its ${item.cohort.family_unit_count} family units","messagePattern":"(.+?) cohort arms \\((.+?)\\) plus exclusions \\((.+?)\\) do not account for its (.+?) family units","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/shared/contracts/scripts/validate-continuous-improvement.mjs","lineNumber":155,"sourceCode":"    const where = `report fixture ${reportPaths[index]}: case ${item.id}`;\n    const family = familiesByID.get(item.cohort.task_family_id);\n    if (!family) throw new Error(`${where} cohort references a task family that is not in this report`);\n    const familyUnits = new Set(family.analysis_unit_ids);\n    if (item.cohort.family_unit_count !== familyUnits.size) throw new Error(`${where} cohort family unit count disagrees with the task family`);\n    const roles = item.cohort.arms.map((arm) => arm.role);\n    if (roles.join(\",\") !== \"baseline,alternative\") throw new Error(`${where} cohort arms are not baseline then alternative`);\n    const armVariants = new Map();\n    let comparedUnits = 0;\n    for (const arm of item.cohort.arms) {\n      const variant = variantsByID.get(arm.variant_id);\n      if (!variant) throw new Error(`${where} cohort arm ${arm.role} references a workflow variant that is not in this report`);\n      if (variant.task_family_id !== family.id) throw new Error(`${where} cohort arm ${arm.role} uses a variant of another task family`);\n      armVariants.set(arm.role, variant);\n      comparedUnits += arm.unit_count;\n    }\n    const excluded = item.cohort.excluded_units.reduce((total, exclusion) => total + exclusion.unit_count, 0);\n    if (comparedUnits + excluded !== item.cohort.family_unit_count) {\n      throw new Error(`${where} cohort arms (${comparedUnits}) plus exclusions (${excluded}) do not account for its ${item.cohort.family_unit_count} family units`);\n    }\n\n    const selectedPerArm = new Map();\n    for (const trace of item.representative_traces) {\n      const variant = armVariants.get(trace.arm);\n      if (!variant || variant.id !== trace.variant_id) throw new Error(`${where} representative trace names a variant that is not that arm's variant`);\n      if (!unitsByID.has(trace.analysis_unit_id)) throw new Error(`${where} representative trace ${trace.analysis_unit_id} is not an analysis unit of this report`);\n      if (!familyUnits.has(trace.analysis_unit_id)) throw new Error(`${where} representative trace ${trace.analysis_unit_id} is not a member of the cohort's task family`);\n      if (!variant.analysis_unit_ids.includes(trace.analysis_unit_id)) throw new Error(`${where} representative trace ${trace.analysis_unit_id} did not run that arm's workflow variant`);\n      selectedPerArm.set(trace.arm, (selectedPerArm.get(trace.arm) ?? 0) + 1);\n    }\n    for (const [arm, count] of selectedPerArm) {\n      if (count > 3) throw new Error(`${where} arm ${arm} carries ${count} representative traces, more than three`);\n    }\n\n    const slice = item.causal_slice;\n    const sliceVariant = variantsByID.get(slice.variant_id);\n    if (!sliceVariant) throw new Error(`${where} causal slice references a workflow variant that is not in this report`);","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/shared/contracts/scripts/validate-continuous-improvement.mjs#L137-L173","documentation":"This is a fixture-integrity error thrown by the continuous-improvement report validator in packages/shared/contracts. A cohort case is supposed to partition the task family's analysis units into compared arms plus excluded units; the validator sums each arm's unit_count and each exclusion's unit_count and requires the total to equal cohort.family_unit_count. It throws when the partition arithmetic does not add up, meaning the fixture's counts were edited by hand or computed from a different unit population.","triggerScenarios":"Running the validation script when a report fixture's cohort has arms/exclusions whose unit_count values sum to more or fewer than family_unit_count — e.g. after hand-editing unit counts, adding an arm without rebalancing, or updating family_unit_count after the family gained/lost analysis units without updating arms or exclusions.","commonSituations":"Hand-authoring or regenerating report fixtures and adjusting family_unit_count without touching arm unit_counts; adding a third arm while the validator requires exactly baseline+alternative; stale fixtures after analysis units were re-grouped into a different task family; a fixture generator bug that counts exclusions twice or omits them.","solutions":["Recompute each arm's unit_count and each exclusion's unit_count from the family's analysis_unit_ids and set cohort.family_unit_count to that family's size so comparedUnits + excluded equals it","Regenerate the fixture with the canonical generator instead of hand-editing counts","Check for duplicate/overlapping exclusions or a missing exclusion entry that would break the partition","Confirm the cohort still uses exactly the arms baseline,alternative in that order; extra or renamed arms break the accounting"],"exampleFix":"// before\n\"family_unit_count\": 24,\n\"arms\": [\n  { \"role\": \"baseline\", \"unit_count\": 10 },\n  { \"role\": \"alternative\", \"unit_count\": 10 }\n],\n\"excluded_units\": []\n// after\n\"family_unit_count\": 24,\n\"arms\": [\n  { \"role\": \"baseline\", \"unit_count\": 12 },\n  { \"role\": \"alternative\", \"unit_count\": 12 }\n],\n\"excluded_units\": [{ \"unit_id\": \"u9\", \"unit_count\": 0, \"reason\": \"ineligible\" }]","handlingStrategy":"validation","validationCode":"const familyUnits = new Set(family.analysis_unit_ids);\nconst compared = cohort.arms.reduce((t, a) => t + a.unit_count, 0);\nconst excluded = cohort.excluded_units.reduce((t, e) => t + e.unit_count, 0);\nif (compared + excluded !== cohort.family_unit_count) {\n  throw new Error(`cohort accounting mismatch: ${compared} compared + ${excluded} excluded != ${cohort.family_unit_count}`);\n}","typeGuard":"function isBalancedCohort(cohort) {\n  const sum = cohort.arms.reduce((t, a) => t + a.unit_count, 0)\n    + cohort.excluded_units.reduce((t, e) => t + e.unit_count, 0);\n  return sum === cohort.family_unit_count;\n}","tryCatchPattern":"try {\n  validateReport(report);\n} catch (err) {\n  if (err.message.includes('do not account for its')) {\n    console.error('Cohort unit accounting mismatch; regenerate the fixture.');\n    process.exitCode = 1;\n  } else throw err;\n}","preventionTips":["Never hand-edit cohort unit counts; regenerate fixtures from the dataset snapshot","Recompute family_unit_count from family.analysis_unit_ids.length whenever the family changes","Add a CI check that arms + exclusions sum to the family size for every case"],"tags":["validation","fixtures","data-integrity"],"backgroundTag":"schema-validation-failed","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}