{"record":{"id":"db12aaeb99b61b9f","repo":"JuliusBrussee/caveman","slug":"where-representative-trace-trace-analysis-uni","errorCode":null,"errorMessage":"${where} representative trace ${trace.analysis_unit_id} is not an analysis unit of this report","messagePattern":"(.+?) representative trace (.+?) is not an analysis unit of this report","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/shared/contracts/scripts/validate-continuous-improvement.mjs","lineNumber":162,"sourceCode":"    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) {\n      throw new Error(`${where} diagnosis manifestation node disagrees with its causal slice`);","sourceCodeStart":144,"sourceCodeEnd":180,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/shared/contracts/scripts/validate-continuous-improvement.mjs#L144-L180","documentation":"The validator rejects a representative trace whose analysis_unit_id does not appear in report.analysis_units. Every trace must reference an analysis unit that actually exists in the report's unit inventory; a dangling id means the fixture references a unit that was never registered or was deleted/renamed.","triggerScenarios":"A representative trace carries an analysis_unit_id that is absent from the report's analysis_units array — typically after deleting or re-id-ing analysis units without updating traces, or referencing a unit from a different report fixture.","commonSituations":"Hand-renaming unit ids in one section of the fixture but not the traces; copying a case into another report whose analysis_units list differs; a generator bug emitting stale unit ids after a dataset refresh.","solutions":["Add the missing unit to report.analysis_units or correct the trace's analysis_unit_id to an existing unit id","Regenerate the report fixture so traces and analysis_units come from the same snapshot","Cross-check every representative trace's analysis_unit_id against the analysis_units id set before validating"],"exampleFix":"// before\n\"representative_traces\": [{ \"arm\": \"baseline\", \"variant_id\": \"v1\", \"analysis_unit_id\": \"unit-99\" }]\n// analysis_units lacks unit-99\n// after\n\"representative_traces\": [{ \"arm\": \"baseline\", \"variant_id\": \"v1\", \"analysis_unit_id\": \"unit-07\" }]\n// unit-07 exists in analysis_units","handlingStrategy":"validation","validationCode":"const unitIds = new Set(report.analysis_units.map((u) => u.id));\nfor (const item of report.cases) {\n  for (const t of item.representative_traces) {\n    if (!unitIds.has(t.analysis_unit_id)) {\n      throw new Error(`trace references unknown analysis unit ${t.analysis_unit_id}`);\n    }\n  }\n}","typeGuard":"function unitExists(unitId, report) {\n  return report.analysis_units.some((u) => u.id === unitId);\n}","tryCatchPattern":"try {\n  validateReport(report);\n} catch (err) {\n  if (err.message.includes('is not an analysis unit of this report')) {\n    console.error('Dangling analysis_unit_id in a representative trace.');\n    process.exitCode = 1;\n  } else throw err;\n}","preventionTips":["Rename unit ids with a single mechanical find/replace across the whole fixture file","Validate referential integrity locally before committing fixture changes","Keep traces and analysis_units generated from the same snapshot"],"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"}