{"record":{"id":"b4ee4fa07b661e5d","repo":"JuliusBrussee/caveman","slug":"where-representative-trace-names-a-variant-that","errorCode":null,"errorMessage":"${where} representative trace names a variant that is not that arm's variant","messagePattern":"(.+?) representative trace names a variant that is not that arm's variant","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/shared/contracts/scripts/validate-continuous-improvement.mjs","lineNumber":161,"sourceCode":"    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`);\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) {","sourceCodeStart":143,"sourceCodeEnd":179,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/shared/contracts/scripts/validate-continuous-improvement.mjs#L143-L179","documentation":"This validator error fires when a representative trace's arm/variant pair is inconsistent with the cohort's declared arms. The validator builds armVariants (role -> variant) from cohort.arms, then for each trace requires that trace.variant_id equals the variant registered for trace.arm. A missing arm lookup or a mismatched variant id means the trace claims a workflow variant that its own arm does not use.","triggerScenarios":"A representative trace references an arm name that is not one of the cohort arms (variant lookup returns undefined) or carries a variant_id that differs from the variant assigned to that arm in cohort.arms — e.g. after renaming arm roles, swapping a variant id, or copying traces between cases.","commonSituations":"Hand-editing arm role strings ('control' vs 'baseline'); pointing an arm at a new variant id and forgetting to update existing representative traces; duplicating a case and leaving traces from the original cohort's variants; typos in trace.arm so armVariants.get returns undefined.","solutions":["Set each trace's arm and variant_id to match cohort.arms exactly (trace.arm resolves to an arm whose arm.variant_id === trace.variant_id)","Regenerate representative traces from the cohort's current arms rather than copying them from an older fixture","Grep the fixture for the variant id to find traces still pointing at a retired variant","Verify arm role strings are exactly baseline and alternative with no casing or spacing differences"],"exampleFix":"// before\n\"arms\": [{ \"role\": \"baseline\", \"variant_id\": \"v-new\" }],\n\"representative_traces\": [\n  { \"arm\": \"baseline\", \"variant_id\": \"v-old\", \"analysis_unit_id\": \"u1\" }\n]\n// after\n\"arms\": [{ \"role\": \"baseline\", \"variant_id\": \"v-new\" }],\n\"representative_traces\": [\n  { \"arm\": \"baseline\", \"variant_id\": \"v-new\", \"analysis_unit_id\": \"u1\" }\n]","handlingStrategy":"validation","validationCode":"const armVariants = new Map(cohort.arms.map((a) => [a.role, a.variant_id]));\nfor (const t of item.representative_traces) {\n  if (armVariants.get(t.arm) !== t.variant_id) {\n    throw new Error(`trace ${t.analysis_unit_id}: arm ${t.arm} does not use variant ${t.variant_id}`);\n  }\n}","typeGuard":"function traceMatchesArms(trace, arms) {\n  const arm = arms.find((a) => a.role === trace.arm);\n  return arm !== undefined && arm.variant_id === trace.variant_id;\n}","tryCatchPattern":"try {\n  validateReport(report);\n} catch (err) {\n  if (err.message.includes(\"not that arm's variant\")) {\n    console.error('Trace arm/variant mismatch; re-derive traces from cohort.arms.');\n    process.exitCode = 1;\n  } else throw err;\n}","preventionTips":["When swapping an arm's variant_id, update or regenerate all representative traces in the same change","Keep arm role strings canonical (baseline/alternative) and avoid manual renames","Copy cases only via the generator, never by hand-editing trace blocks"],"tags":["validation","fixtures","consistency"],"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"}