{"record":{"id":"2510172331237de1","repo":"JuliusBrussee/caveman","slug":"where-representative-trace-trace-analysis-uni-251017","errorCode":null,"errorMessage":"${where} representative trace ${trace.analysis_unit_id} is not a member of the cohort's task family","messagePattern":"(.+?) representative trace (.+?) is not a member of the cohort's task family","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/shared/contracts/scripts/validate-continuous-improvement.mjs","lineNumber":163,"sourceCode":"    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`);\n    if (sliceVariant.id !== armVariants.get(\"baseline\").id) throw new Error(`${where} causal slice is not taken from the baseline arm's variant`);\n    const sliceNodes = new Set(sliceVariant.nodes.map((node) => node.id));\n    if (slice.manifestation_node_id !== \"\" && !sliceNodes.has(slice.manifestation_node_id)) {\n      throw new Error(`${where} manifestation node ${slice.manifestation_node_id} is not a node of variant ${sliceVariant.id}`);\n    }\n    if (item.diagnosis.manifestation.node_id !== slice.manifestation_node_id) {\n      throw new Error(`${where} diagnosis manifestation node disagrees with its causal slice`);\n    }","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/shared/contracts/scripts/validate-continuous-improvement.mjs#L145-L181","documentation":"The validator rejects a representative trace whose analysis_unit_id exists in the report but is not a member of the cohort's task family (absent from family.analysis_unit_ids). Each cohort case studies one task family, so its traces must come from units belonging to that family; this catches traces pulled from units of a different family.","triggerScenarios":"A representative trace's analysis_unit_id is in report.analysis_units but not in task_families[cohort.task_family_id].analysis_unit_ids — e.g. after reassigning units between task families, or copying a case from a fixture with different family membership.","commonSituations":"Re-grouping analysis units into new task families without regenerating cases; a case's cohort.task_family_id changed but traces still reference old-family units; fixtures merged from different dataset snapshots.","solutions":["Replace the trace's analysis_unit_id with a unit that belongs to the cohort's task family (is in family.analysis_unit_ids), or move the unit into that family in the fixture","Update cohort.task_family_id if the traces genuinely belong to a different family","Regenerate the fixture from the current dataset snapshot so family membership and traces agree"],"exampleFix":"// before\n\"cohort\": { \"task_family_id\": \"family-a\" },\n\"representative_traces\": [{ \"arm\": \"alternative\", \"variant_id\": \"v2\", \"analysis_unit_id\": \"unit-b1\" }]\n// unit-b1 belongs to family-b\n// after\n\"representative_traces\": [{ \"arm\": \"alternative\", \"variant_id\": \"v2\", \"analysis_unit_id\": \"unit-a3\" }]\n// unit-a3 is in family-a.analysis_unit_ids","handlingStrategy":"validation","validationCode":"const familiesByID = new Map(report.task_families.map((f) => [f.id, f]));\nfor (const item of report.cases) {\n  const famUnits = new Set(familiesByID.get(item.cohort.task_family_id).analysis_unit_ids);\n  for (const t of item.representative_traces) {\n    if (!famUnits.has(t.analysis_unit_id)) {\n      throw new Error(`trace ${t.analysis_unit_id} not in family ${item.cohort.task_family_id}`);\n    }\n  }\n}","typeGuard":"function unitInFamily(unitId, cohort, familiesByID) {\n  const family = familiesByID.get(cohort.task_family_id);\n  return family !== undefined && family.analysis_unit_ids.includes(unitId);\n}","tryCatchPattern":"try {\n  validateReport(report);\n} catch (err) {\n  if (err.message.includes(\"not a member of the cohort's task family\")) {\n    console.error('Trace unit belongs to a different task family; regenerate the case.');\n    process.exitCode = 1;\n  } else throw err;\n}","preventionTips":["When re-grouping analysis units into families, regenerate all cohort cases from scratch","Never copy cases between report fixtures with different family membership","Derive cohort.task_family_id from the traces' units instead of hardcoding it"],"tags":["validation","fixtures","reference-integrity"],"backgroundTag":"record-not-found","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"}