{"record":{"id":"9f3b56acf6548921","repo":"JuliusBrussee/caveman","slug":"where-structural-cost-share-motif-structural","errorCode":null,"errorMessage":"${where} structural_cost_share ${motif.structural_cost_share} != ${expectedShare}","messagePattern":"(.+?) structural_cost_share (.+?) != (.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/shared/contracts/scripts/validate-continuous-improvement.mjs","lineNumber":127,"sourceCode":"    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) {\n      const variant = variantsByID.get(variantID);\n      if (!variant) throw new Error(`${where} references workflow variant ${variantID} that is not in this report`);\n      if (variant.task_family_id !== motif.task_family_id) throw new Error(`${where} supporting variant ${variantID} belongs to another task family`);\n      if (!contiguouslyContains(variant.signature, motif.operations)) {\n        throw new Error(`${where} operations are not a contiguous subsequence of variant ${variantID}'s signature`);\n      }\n      runs += variant.eligible_runs;\n      weighted += (motif.operations.length / variant.signature.length) * variant.eligible_runs;\n    }\n    if (motif.support_run_count !== runs) throw new Error(`${where} support run count ${motif.support_run_count} != ${runs}`);\n    const expectedShare = runs > 0 ? weighted / runs : 0;\n    if (Math.abs(motif.structural_cost_share - expectedShare) > 1e-6) {\n      throw new Error(`${where} structural_cost_share ${motif.structural_cost_share} != ${expectedShare}`);\n    }\n  }\n  motifCount += report.motifs.length;\n\n  // The causal investigation of every case: the cohort's arms, the traces it\n  // selected from them, and the backward hard-dependency slice.\n  const unitsByID = new Map(report.analysis_units.map((unit) => [unit.id, unit]));\n  const familiesByID = new Map(report.task_families.map((family) => [family.id, family]));\n  for (const item of report.cases) {\n    const where = `report fixture ${reportPaths[index]}: case ${item.id}`;\n    const family = familiesByID.get(item.cohort.task_family_id);\n    if (!family) throw new Error(`${where} cohort references a task family that is not in this report`);\n    const familyUnits = new Set(family.analysis_unit_ids);\n    if (item.cohort.family_unit_count !== familyUnits.size) throw new Error(`${where} cohort family unit count disagrees with the task family`);\n    const roles = item.cohort.arms.map((arm) => arm.role);\n    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;","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/shared/contracts/scripts/validate-continuous-improvement.mjs#L109-L145","documentation":"structural_cost_share must equal the eligible-run-weighted average of (motif.operations.length / variant.signature.length) across supporting variants (0 when there are no runs), within 1e-6. The validator recomputes this ratio and throws on divergence, keeping cost-share figures derivable from variant data.","triggerScenarios":"|motif.structural_cost_share - (weighted / runs)| > 1e-6, where weighted = sum((operations.length / signature.length) * eligible_runs) and runs = sum(eligible_runs); or a nonzero share stored when runs is 0.","commonSituations":"Signatures lengthened or shortened after the share was computed; operations or eligible_runs edited without recomputation; hand-rounded share values (e.g. 0.33) exceeding the 1e-6 tolerance.","solutions":["Recompute structural_cost_share as the weighted ratio and store full precision.","Regenerate the report so the share is derived from current variant signatures and eligible_runs.","If runs is 0, set structural_cost_share to exactly 0."],"exampleFix":"// before\nops 2, signature 4, eligible_runs 10 → structural_cost_share: 0.6\n// after\nstructural_cost_share: 0.5","handlingStrategy":"validation","validationCode":"const runs = motif.variant_ids.reduce((n, id) => n + v(id).eligible_runs, 0);\nconst weighted = motif.variant_ids.reduce((n, id) => n + (motif.operations.length / v(id).signature.length) * v(id).eligible_runs, 0);\nconst expected = runs > 0 ? weighted / runs : 0;\nif (Math.abs(motif.structural_cost_share - expected) > 1e-6) throw new Error(`structural_cost_share must be ${expected}`);","typeGuard":"const costShareConsistent = (motif, report) => { const runs = motif.variant_ids.reduce((n, id) => n + (report.workflow_variants.find(v => v.id === id)?.eligible_runs ?? 0), 0); const weighted = motif.variant_ids.reduce((n, id) => { const v = report.workflow_variants.find(v => v.id === id); return n + (v ? (motif.operations.length / v.signature.length) * v.eligible_runs : 0); }, 0); const expected = runs > 0 ? weighted / runs : 0; return Math.abs(motif.structural_cost_share - expected) <= 1e-6; };","tryCatchPattern":"try { validateContinuousImprovement(report); } catch (e) { if (String(e.message).includes('structural_cost_share')) { console.error('Recompute structural_cost_share as the eligible-run-weighted ratio'); } else throw e; }","preventionTips":["Compute the share in the generator, storing full float precision","Recompute the share whenever signatures, operations, or eligible_runs change","Set the share to exactly 0 when there are no eligible runs"],"tags":["fixture","ratio","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"}