{"record":{"id":"77f05f2974cb56aa","repo":"JuliusBrussee/caveman","slug":"where-probability-b-given-a-relationship-probability-b-given","errorCode":null,"errorMessage":"${where} probability_b_given_a ${relationship.probability_b_given_a} != ${expectedBGivenA}","messagePattern":"(.+?) probability_b_given_a (.+?) != (.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/shared/contracts/scripts/validate-continuous-improvement.mjs","lineNumber":96,"sourceCode":"  const themeIds = new Set(report.themes.map((theme) => theme.id));\n  for (const relationship of report.relationships) {\n    const where = `report fixture ${reportPaths[index]}: relationship ${relationship.id}`;\n    if (!themeIds.has(relationship.theme_a_id) || !themeIds.has(relationship.theme_b_id)) {\n      throw new Error(`${where} references a theme that is not in this report`);\n    }\n    if (!(relationship.theme_a_id < relationship.theme_b_id)) {\n      throw new Error(`${where} theme ids are not in canonical order`);\n    }\n    if (relationship.shared_unit_count > relationship.theme_a_unit_count || relationship.shared_unit_count > relationship.theme_b_unit_count) {\n      throw new Error(`${where} shares more units than either theme has`);\n    }\n    if (relationship.shared_unit_ids.length > relationship.shared_unit_count) {\n      throw new Error(`${where} carries more shared unit ids than its shared unit count`);\n    }\n    const expectedBGivenA = relationship.shared_unit_count / relationship.theme_a_unit_count;\n    const expectedAGivenB = relationship.shared_unit_count / relationship.theme_b_unit_count;\n    if (Math.abs(relationship.probability_b_given_a - expectedBGivenA) > 1e-9) {\n      throw new Error(`${where} probability_b_given_a ${relationship.probability_b_given_a} != ${expectedBGivenA}`);\n    }\n    if (Math.abs(relationship.probability_a_given_b - expectedAGivenB) > 1e-9) {\n      throw new Error(`${where} probability_a_given_b ${relationship.probability_a_given_b} != ${expectedAGivenB}`);\n    }\n  }\n  relationshipCount += report.relationships.length;\n\n  // A motif is a structural count over variants this report carries, so every\n  // part of it must be re-derivable from those variants.\n  const variantsByID = new Map(report.workflow_variants.map((variant) => [variant.id, variant]));\n  const familyIDs = new Set(report.task_families.map((family) => family.id));\n  for (const motif of report.motifs) {\n    const where = `report fixture ${reportPaths[index]}: motif ${motif.id}`;\n    if (!familyIDs.has(motif.task_family_id)) throw new Error(`${where} references a task family that is not in this report`);\n    if (motif.support_variant_count !== motif.variant_ids.length) throw new Error(`${where} support variant count disagrees with its variant ids`);\n    let runs = 0;\n    let weighted = 0;\n    for (const variantID of motif.variant_ids) {","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/shared/contracts/scripts/validate-continuous-improvement.mjs#L78-L114","documentation":"The validator requires that conditional probabilities in a theme relationship equal the ratio shared_unit_count / theme_a_unit_count within 1e-9. probability_b_given_a must be the fraction of theme A's units shared with theme B. A mismatch means the fixture's probability was stale or hand-computed incorrectly.","triggerScenarios":"A relationship entry where |probability_b_given_a - (shared_unit_count / theme_a_unit_count)| > 1e-9, e.g. after changing shared_unit_count or theme_a_unit_count without updating the probability.","commonSituations":"Editing shared unit counts in a fixture without recomputing probabilities; regenerating themes with different unit totals; floating-point values rounded too coarsely by hand (e.g. 0.33 instead of 1/3).","solutions":["Recompute probability_b_given_a as shared_unit_count / theme_a_unit_count and update the fixture.","If the shared count is wrong, fix shared_unit_count (and shared_unit_ids) so the ratio matches the stored probability.","Store enough precision (avoid heavy rounding) so the value is within 1e-9 of the exact ratio."],"exampleFix":"// before\n{ \"shared_unit_count\": 2, \"theme_a_unit_count\": 4, \"probability_b_given_a\": 0.4 }\n// after\n{ \"shared_unit_count\": 2, \"theme_a_unit_count\": 4, \"probability_b_given_a\": 0.5 }","handlingStrategy":"validation","validationCode":"const expected = rel.shared_unit_count / rel.theme_a_unit_count;\nif (Math.abs(rel.probability_b_given_a - expected) > 1e-9) throw new Error(`probability_b_given_a must be ${expected}`);","typeGuard":"const probBGivenAValid = (rel) => Math.abs(rel.probability_b_given_a - rel.shared_unit_count / rel.theme_a_unit_count) <= 1e-9;","tryCatchPattern":"try { validateContinuousImprovement(report); } catch (e) { if (String(e.message).includes('probability_b_given_a')) { console.error('Recompute probability_b_given_a = shared_unit_count / theme_a_unit_count'); } else throw e; }","preventionTips":["Compute conditional probabilities in code from counts, never hard-code them","Keep full float precision (avoid rounding to 2 decimals)","Re-validate after any change to shared_unit_count or theme_a_unit_count"],"tags":["fixture","probability","validation"],"backgroundTag":"invalid-argument-value","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"}