JuliusBrussee/caveman · error · Error

structural_cost_share !=

Error message

${where} structural_cost_share ${motif.structural_cost_share} != ${expectedShare}

What it means

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.

Solutions

  1. Recompute structural_cost_share as the weighted ratio and store full precision.
  2. Regenerate the report so the share is derived from current variant signatures and eligible_runs.
  3. If runs is 0, set structural_cost_share to exactly 0.

Example fix

// before
ops 2, signature 4, eligible_runs 10 → structural_cost_share: 0.6
// after
structural_cost_share: 0.5
Defensive patterns

Strategy: validation

Validate before calling

const runs = motif.variant_ids.reduce((n, id) => n + v(id).eligible_runs, 0);
const weighted = motif.variant_ids.reduce((n, id) => n + (motif.operations.length / v(id).signature.length) * v(id).eligible_runs, 0);
const expected = runs > 0 ? weighted / runs : 0;
if (Math.abs(motif.structural_cost_share - expected) > 1e-6) throw new Error(`structural_cost_share must be ${expected}`);

Type guard

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; };

Try / catch

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; }

Prevention

When it happens

Trigger: |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.

Common situations: 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.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/9f3b56acf6548921. Report an issue: GitHub.

Appendix: source

Thrown at packages/shared/contracts/scripts/validate-continuous-improvement.mjs:127

    const where = `report fixture ${reportPaths[index]}: motif ${motif.id}`;
    if (!familyIDs.has(motif.task_family_id)) throw new Error(`${where} references a task family that is not in this report`);
    if (motif.support_variant_count !== motif.variant_ids.length) throw new Error(`${where} support variant count disagrees with its variant ids`);
    let runs = 0;
    let weighted = 0;
    for (const variantID of motif.variant_ids) {
      const variant = variantsByID.get(variantID);
      if (!variant) throw new Error(`${where} references workflow variant ${variantID} that is not in this report`);
      if (variant.task_family_id !== motif.task_family_id) throw new Error(`${where} supporting variant ${variantID} belongs to another task family`);
      if (!contiguouslyContains(variant.signature, motif.operations)) {
        throw new Error(`${where} operations are not a contiguous subsequence of variant ${variantID}'s signature`);
      }
      runs += variant.eligible_runs;
      weighted += (motif.operations.length / variant.signature.length) * variant.eligible_runs;
    }
    if (motif.support_run_count !== runs) throw new Error(`${where} support run count ${motif.support_run_count} != ${runs}`);
    const expectedShare = runs > 0 ? weighted / runs : 0;
    if (Math.abs(motif.structural_cost_share - expectedShare) > 1e-6) {
      throw new Error(`${where} structural_cost_share ${motif.structural_cost_share} != ${expectedShare}`);
    }
  }
  motifCount += report.motifs.length;

  // The causal investigation of every case: the cohort's arms, the traces it
  // selected from them, and the backward hard-dependency slice.
  const unitsByID = new Map(report.analysis_units.map((unit) => [unit.id, unit]));
  const familiesByID = new Map(report.task_families.map((family) => [family.id, family]));
  for (const item of report.cases) {
    const where = `report fixture ${reportPaths[index]}: case ${item.id}`;
    const family = familiesByID.get(item.cohort.task_family_id);
    if (!family) throw new Error(`${where} cohort references a task family that is not in this report`);
    const familyUnits = new Set(family.analysis_unit_ids);
    if (item.cohort.family_unit_count !== familyUnits.size) throw new Error(`${where} cohort family unit count disagrees with the task family`);
    const roles = item.cohort.arms.map((arm) => arm.role);
    if (roles.join(",") !== "baseline,alternative") throw new Error(`${where} cohort arms are not baseline then alternative`);
    const armVariants = new Map();
    let comparedUnits = 0;

View on GitHub (pinned to 3ee70a1026)