{"record":{"id":"e2ac05eac8063828","repo":"JuliusBrussee/caveman","slug":"where-cohort-family-unit-count-disagrees-with-t","errorCode":null,"errorMessage":"${where} cohort family unit count disagrees with the task family","messagePattern":"(.+?) cohort family unit count disagrees with the task family","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/shared/contracts/scripts/validate-continuous-improvement.mjs","lineNumber":141,"sourceCode":"    }\n    if (motif.support_run_count !== runs) throw new Error(`${where} support run count ${motif.support_run_count} != ${runs}`);\n    const expectedShare = runs > 0 ? weighted / runs : 0;\n    if (Math.abs(motif.structural_cost_share - expectedShare) > 1e-6) {\n      throw new Error(`${where} structural_cost_share ${motif.structural_cost_share} != ${expectedShare}`);\n    }\n  }\n  motifCount += report.motifs.length;\n\n  // The causal investigation of every case: the cohort's arms, the traces it\n  // selected from them, and the backward hard-dependency slice.\n  const unitsByID = new Map(report.analysis_units.map((unit) => [unit.id, unit]));\n  const familiesByID = new Map(report.task_families.map((family) => [family.id, family]));\n  for (const item of report.cases) {\n    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) {","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/shared/contracts/scripts/validate-continuous-improvement.mjs#L123-L159","documentation":"After resolving the cohort's task family, the validator computes the true number of distinct units in that family (`new Set(family.analysis_unit_ids).size`) and requires the cohort's declared `family_unit_count` to equal it exactly. A mismatch means the cohort claims a different population size than the family it belongs to, so the error is thrown.","triggerScenarios":"Running the validator when `item.cohort.family_unit_count` (a number) differs from the deduplicated size of `family.analysis_unit_ids` for the referenced family — e.g. units were added to or removed from the family without updating the cohort's count, or the count was set to the raw array length while the array contains duplicate ids.","commonSituations":"Manually editing analysis_unit_ids in a fixture; duplicate ids in analysis_unit_ids making the Set smaller than the recorded count; partial report regeneration where families changed but cohorts kept stale counts.","solutions":["Recompute family_unit_count as the number of unique ids in the referenced family's analysis_unit_ids and update the cohort.","Remove duplicate ids from the family's analysis_unit_ids if the count was intended to reflect the raw array length.","Regenerate the report fixture so family and cohort metadata stay consistent."],"exampleFix":"// before\nanalysis_unit_ids: [\"u1\", \"u2\", \"u2\"] // cohort.family_unit_count: 3\n\n// after\nanalysis_unit_ids: [\"u1\", \"u2\"] // cohort.family_unit_count: 2","handlingStrategy":"validation","validationCode":"const families = new Map(report.task_families.map((f) => [f.id, f]));\nfor (const c of report.cases) {\n  const fam = families.get(c.cohort.task_family_id);\n  if (fam && c.cohort.family_unit_count !== new Set(fam.analysis_unit_ids).size) {\n    throw new Error(`case ${c.id}: family_unit_count ${c.cohort.family_unit_count} != ${new Set(fam.analysis_unit_ids).size}`);\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  validateReport(report);\n} catch (err) {\n  if (String(err.message).includes('family unit count disagrees')) {\n    console.error('Stale cohort unit count; recompute from family analysis_unit_ids:', err.message);\n  } else throw err;\n}","preventionTips":["Compute family_unit_count programmatically (dedup with Set) instead of hardcoding it in fixtures.","Deduplicate analysis_unit_ids when authoring families so raw length equals unique count.","Whenever a family's unit list changes, regenerate all cohorts that reference it in the same commit.","Snapshot-test the count derivation so silent drift is caught."],"tags":["fixture-validation","count-mismatch","cohort"],"backgroundTag":"invalid-argument-value","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"}