{"record":{"id":"c8f45544988bc502","repo":"koala73/worldmonitor","slug":"unknown-physical-divergence-state-string-readin","errorCode":null,"errorMessage":"Unknown physical divergence state: ${String(reading.state)}","messagePattern":"Unknown physical divergence state: (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"shared/physical-divergence-contract.js","lineNumber":140,"sourceCode":"export function isPhysicalDivergenceStoredCompositeReason(state, reason) {\n  if (state === 'ok') return reason === '';\n  return NON_OK_STATE_PRIORITY.includes(state)\n    && COMPOSITE_MEMBER_REASONS.includes(reason)\n    && reason.endsWith(`:${state}`);\n}\n\nexport function buildPhysicalStressComposite(readings) {\n  const values = Array.isArray(readings) ? readings : [];\n  const byMetal = new Map();\n  for (const reading of values) {\n    if (!METAL_ORDER.includes(reading?.metal)) {\n      throw new TypeError(`Unsupported physical divergence metal: ${String(reading?.metal)}`);\n    }\n    if (byMetal.has(reading.metal)) {\n      throw new TypeError(`Physical divergence composite repeats a metal: ${reading.metal}`);\n    }\n    if (!STATES.includes(reading.state)) {\n      throw new TypeError(`Unknown physical divergence state: ${String(reading.state)}`);\n    }\n    byMetal.set(reading.metal, reading);\n  }\n  const weights = METAL_ORDER.map((metal) => ({\n    metal,\n    weight: PHYSICAL_DIVERGENCE_CONTRACT.metals[metal].weight,\n    methodologyVersion: METHODOLOGY_VERSION,\n  }));\n  for (const metal of METAL_ORDER) {\n    if (!byMetal.has(metal)) {\n      return {\n        state: 'missing_input',\n        reason: `member_not_ok:${metal}:missing_input`,\n        index: null,\n        weights,\n        methodologyVersion: METHODOLOGY_VERSION,\n      };\n    }","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/koala73/worldmonitor/blob/9361220cc013571781071f0206e4d80fd14b2f7f/shared/physical-divergence-contract.js#L122-L158","documentation":"Each composite reading's state must be one of STATES ('ok','insufficient_history','stale_input','missing_input'); any other state string throws this TypeError, since state drives which reasons and weights are valid downstream.","triggerScenarios":"Passing a state not in the contract such as 'stale', 'unknown', 'error', a typo like 'stale_imput', or undefined state on malformed readings.","commonSituations":"Producers inventing ad-hoc state labels; version drift between a newer producer state and older shared contract; mapping code that transforms upstream statuses incorrectly.","solutions":["Use only the four contract states: 'ok','insufficient_history','stale_input','missing_input'","Derive states via contract helpers (e.g. physicalDivergenceStateForFreshnessReason) instead of hand-writing them","Pre-validate with STATES.includes(reading.state) before the call","Align producer/consumer contract versions; add genuinely new states to STATES explicitly"],"exampleFix":"// before\nbuildPhysicalStressComposite([{ metal: 'gold', state: 'stale', index: 1 }])\n// after\nbuildPhysicalStressComposite([{ metal: 'gold', state: 'stale_input', index: 1 }])","handlingStrategy":"type-guard","validationCode":"import { STATES } from './physical-divergence-contract.js';\nfunction hasValidStates(readings) {\n  return (Array.isArray(readings) ? readings : []).every((r) => STATES.includes(r?.state));\n}","typeGuard":"function isDivergenceState(v) {\n  return typeof v === 'string' && STATES.includes(v);\n}","tryCatchPattern":"try {\n  composite = buildPhysicalStressComposite(readings);\n} catch (e) {\n  if (e instanceof TypeError && e.message.startsWith('Unknown physical divergence state')) {\n    composite = null; // inspect reading.state values\n  } else throw e;\n}","preventionTips":["Only use 'ok','insufficient_history','stale_input','missing_input'","Derive states from contract helpers rather than raw upstream statuses","Share the contract module between producer and consumer","Add contract conformance tests for state values"],"tags":["contract","typeerror","data-contract"],"backgroundTag":"invalid-enum-value","analyzedSha":"9361220cc013571781071f0206e4d80fd14b2f7f","analyzedAt":"2026-09-01T10:32:37.851Z","contentChangedAt":"2026-09-01T10:32:37.851Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}