{"record":{"id":"905f2edbaf240caa","repo":"JuliusBrussee/caveman","slug":"where-arm-arm-carries-count-representativ","errorCode":null,"errorMessage":"${where} arm ${arm} carries ${count} representative traces, more than three","messagePattern":"(.+?) arm (.+?) carries (.+?) representative traces, more than three","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/shared/contracts/scripts/validate-continuous-improvement.mjs","lineNumber":168,"sourceCode":"      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    }\n    for (const nodeID of slice.node_chain) {\n      if (!sliceNodes.has(nodeID)) throw new Error(`${where} causal slice node ${nodeID} is not a node of variant ${sliceVariant.id}`);\n    }\n    if (slice.manifestation_node_id !== \"\" && !slice.node_chain.includes(slice.manifestation_node_id)) {\n      throw new Error(`${where} causal slice omits its own manifestation node`);","sourceCodeStart":150,"sourceCodeEnd":186,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/shared/contracts/scripts/validate-continuous-improvement.mjs#L150-L186","documentation":"The continuous-improvement report validator enforces that each cohort arm carries at most three representative traces. When a case's representative_traces list contains more than three traces for the same arm (baseline or alternative), the validator throws so reports stay compact and comparable. It is a report-authoring contract, not a runtime failure.","triggerScenarios":"Running `validate-continuous-improvement.mjs` against a report fixture where any single arm has 4+ entries in `case.representative_traces` (counted per `trace.arm` in selectedPerArm).","commonSituations":"Hand-editing report fixtures and appending traces; a report generator that dumps all traces instead of a sampled subset; merging two cohorts without deduplication.","solutions":["Trim representative_traces per arm to at most 3, keeping the most representative ones","Fix the report generator to cap per-arm selection at 3 traces","Verify each trace's `arm` label is not duplicated or mislabeled, inflating one arm's count"],"exampleFix":"// before\nrepresentative_traces: [t1, t2, t3, t4].map(t => ({...t, arm: 'baseline'}))\n// after\nrepresentative_traces: [t1, t2, t3].map(t => ({...t, arm: 'baseline'}))","handlingStrategy":"validation","validationCode":"const perArm = new Map();\nfor (const t of item.representative_traces) perArm.set(t.arm, (perArm.get(t.arm) ?? 0) + 1);\nif ([...perArm.values()].some(c => c > 3)) throw new Error('more than three representative traces in an arm');","typeGuard":"const hasAtMostThreePerArm = (traces) => {\n  const counts = {};\n  for (const t of traces) counts[t.arm] = (counts[t.arm] ?? 0) + 1;\n  return Object.values(counts).every(c => c <= 3);\n};","tryCatchPattern":"try { validateReport(report); } catch (e) { if (String(e.message).includes('more than three')) trimTraces(report); else throw e; }","preventionTips":["Cap per-arm trace sampling at 3 in the report generator","Assert per-arm counts in CI before committing fixtures","Deduplicate traces when merging cohorts"],"tags":["validation","report-schema","fixtures"],"backgroundTag":"value-out-of-range","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"}