JuliusBrussee/caveman · error · Error

cohort family unit count disagrees with the task family

Error message

${where} cohort family unit count disagrees with the task family

What it means

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.

Solutions

  1. Recompute family_unit_count as the number of unique ids in the referenced family's analysis_unit_ids and update the cohort.
  2. Remove duplicate ids from the family's analysis_unit_ids if the count was intended to reflect the raw array length.
  3. Regenerate the report fixture so family and cohort metadata stay consistent.

Example fix

// before
analysis_unit_ids: ["u1", "u2", "u2"] // cohort.family_unit_count: 3

// after
analysis_unit_ids: ["u1", "u2"] // cohort.family_unit_count: 2
Defensive patterns

Strategy: validation

Validate before calling

const families = new Map(report.task_families.map((f) => [f.id, f]));
for (const c of report.cases) {
  const fam = families.get(c.cohort.task_family_id);
  if (fam && c.cohort.family_unit_count !== new Set(fam.analysis_unit_ids).size) {
    throw new Error(`case ${c.id}: family_unit_count ${c.cohort.family_unit_count} != ${new Set(fam.analysis_unit_ids).size}`);
  }
}

Try / catch

try {
  validateReport(report);
} catch (err) {
  if (String(err.message).includes('family unit count disagrees')) {
    console.error('Stale cohort unit count; recompute from family analysis_unit_ids:', err.message);
  } else throw err;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/e2ac05eac8063828. Report an issue: GitHub.

Appendix: source

Thrown at packages/shared/contracts/scripts/validate-continuous-improvement.mjs:141

    }
    if (motif.support_run_count !== runs) throw new Error(`${where} support run count ${motif.support_run_count} != ${runs}`);
    const expectedShare = runs > 0 ? weighted / runs : 0;
    if (Math.abs(motif.structural_cost_share - expectedShare) > 1e-6) {
      throw new Error(`${where} structural_cost_share ${motif.structural_cost_share} != ${expectedShare}`);
    }
  }
  motifCount += report.motifs.length;

  // The causal investigation of every case: the cohort's arms, the traces it
  // selected from them, and the backward hard-dependency slice.
  const unitsByID = new Map(report.analysis_units.map((unit) => [unit.id, unit]));
  const familiesByID = new Map(report.task_families.map((family) => [family.id, family]));
  for (const item of report.cases) {
    const where = `report fixture ${reportPaths[index]}: case ${item.id}`;
    const family = familiesByID.get(item.cohort.task_family_id);
    if (!family) throw new Error(`${where} cohort references a task family that is not in this report`);
    const familyUnits = new Set(family.analysis_unit_ids);
    if (item.cohort.family_unit_count !== familyUnits.size) throw new Error(`${where} cohort family unit count disagrees with the task family`);
    const roles = item.cohort.arms.map((arm) => arm.role);
    if (roles.join(",") !== "baseline,alternative") throw new Error(`${where} cohort arms are not baseline then alternative`);
    const armVariants = new Map();
    let comparedUnits = 0;
    for (const arm of item.cohort.arms) {
      const variant = variantsByID.get(arm.variant_id);
      if (!variant) throw new Error(`${where} cohort arm ${arm.role} references a workflow variant that is not in this report`);
      if (variant.task_family_id !== family.id) throw new Error(`${where} cohort arm ${arm.role} uses a variant of another task family`);
      armVariants.set(arm.role, variant);
      comparedUnits += arm.unit_count;
    }
    const excluded = item.cohort.excluded_units.reduce((total, exclusion) => total + exclusion.unit_count, 0);
    if (comparedUnits + excluded !== item.cohort.family_unit_count) {
      throw new Error(`${where} cohort arms (${comparedUnits}) plus exclusions (${excluded}) do not account for its ${item.cohort.family_unit_count} family units`);
    }

    const selectedPerArm = new Map();
    for (const trace of item.representative_traces) {

View on GitHub (pinned to 3ee70a1026)