{"record":{"id":"6e6a1e41f17de10c","repo":"JuliusBrussee/caveman","slug":"where-theme-ids-are-not-in-canonical-order","errorCode":null,"errorMessage":"${where} theme ids are not in canonical order","messagePattern":"(.+?) theme ids are not in canonical order","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/shared/contracts/scripts/validate-continuous-improvement.mjs","lineNumber":85,"sourceCode":"    if (opportunity.type === \"safety\") {\n      if (opportunity.alternative_variant_id) throw new Error(`${where} is a safety finding with an alternative variant`);\n      if (opportunity.expected_value !== 0) throw new Error(`${where} is a safety finding carrying expected value ${opportunity.expected_value}`);\n      if (opportunity.alternative_metrics.cost_per_outcome_usd !== null || opportunity.alternative_metrics.eligible_runs !== 0) {\n        throw new Error(`${where} is a safety finding carrying another workflow's metrics`);\n      }\n    }\n  }\n\n  // A relationship is a count, so it must be recomputable from the counts it\n  // carries. Anything a reader cannot re-derive is a claim, not evidence.\n  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","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/shared/contracts/scripts/validate-continuous-improvement.mjs#L67-L103","documentation":"Relationship endpoints must be stored in a canonical order: the validator requires relationship.theme_a_id < relationship.theme_b_id (lexicographic string comparison) so the same pair of themes always serializes one way and is not double-counted as two distinct relationships. It throws when the ids are equal or stored in descending order.","triggerScenarios":"Running the validator on a report where a relationship has theme_a_id >= theme_b_id, e.g. {\"theme_a_id\": \"theme-guard\", \"theme_b_id\": \"theme-retry\"} with \"theme-retry\" < \"theme-guard\", or both fields equal.","commonSituations":"Relationship emitted while iterating themes in discovery order instead of sorted order; deduplication logic absent so a pair appears as both (a,b) and (b,a); hand-written fixtures.","solutions":["Swap the two ids so theme_a_id is the lexicographically smaller one (keeping counts/probabilities attached to the correct endpoints).","Sort theme ids when emitting relationships in the report generator so ordering is deterministic.","Deduplicate reversed pairs in the generator before writing the report.","Re-run the validator to confirm canonical ordering."],"exampleFix":"// before\n{ \"theme_a_id\": \"theme-retry\", \"theme_b_id\": \"theme-guard\" }\n// after\n{ \"theme_a_id\": \"theme-guard\", \"theme_b_id\": \"theme-retry\" }","handlingStrategy":"validation","validationCode":"if (report.relationships.some((r) => !(r.theme_a_id < r.theme_b_id))) {\n  throw new Error(\"relationship endpoints not in canonical order\");\n}","typeGuard":"const isCanonicallyOrdered = (r) => r.theme_a_id < r.theme_b_id;","tryCatchPattern":"try {\n  await runValidator([reportPath, spansPath]);\n} catch (err) {\n  if (String(err.message).includes(\"theme ids are not in canonical order\")) {\n    console.error(\"Sort the pair lexicographically and swap counts accordingly.\");\n  }\n  throw err;\n}","preventionTips":["Sort theme ids ([a, b].sort()) before emitting any relationship.","Deduplicate pairs via a normalized `${min}:${max}` key.","Keep endpoint ordering logic in one shared helper used by all detectors."],"tags":["validation","fixtures","canonicalization","relationships"],"backgroundTag":"invalid-argument-format","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"}