koala73/worldmonitor · error · TypeError

Physical divergence composite repeats a metal weight

Error message

Physical divergence composite repeats a metal weight

What it means

canonicalWeights() validates a stored physical-divergence composite's weights against the PHYSICAL_DIVERGENCE_CONTRACT (exact per-metal weight, methodology version). After mapping, if the number of distinct metals (byMetal.size) is less than PHYSICAL_DIVERGENCE_METALS.length, two entries resolved to the same metal — meaning the stored composite repeats a metal weight and is structurally corrupt. It throws a TypeError.

Source

Thrown at server/_shared/physical-divergence-snapshot.ts:349

  };
}

function canonicalWeights(value: unknown): PhysicalDivergenceRawComposite['weights'] {
  if (!Array.isArray(value) || value.length !== PHYSICAL_DIVERGENCE_METALS.length) {
    throw new TypeError('Physical divergence composite has invalid weights');
  }
  const byMetal = new Map(value.map((entry) => {
    const raw = object(entry);
    const weightMetal = metal(raw.metal);
    if (
      !finite(raw.weight)
      || raw.weight !== PHYSICAL_DIVERGENCE_CONTRACT.metals[weightMetal].weight
      || methodology(raw.methodologyVersion) !== PHYSICAL_DIVERGENCE_METHODOLOGY_VERSION
    ) throw new TypeError('Physical divergence composite has an invalid weight');
    return [weightMetal, raw.weight] as const;
  }));
  if (byMetal.size !== PHYSICAL_DIVERGENCE_METALS.length) {
    throw new TypeError('Physical divergence composite repeats a metal weight');
  }
  return PHYSICAL_DIVERGENCE_METALS.map((weightMetal) => ({
    metal: weightMetal,
    weight: PHYSICAL_DIVERGENCE_CONTRACT.metals[weightMetal].weight,
    methodologyVersion: PHYSICAL_DIVERGENCE_METHODOLOGY_VERSION,
  }));
}

function validateStoredComposite(
  value: unknown,
  readings: PhysicalDivergenceRawReading[],
): void {
  const raw = object(value);
  methodology(raw.methodologyVersion);
  canonicalWeights(raw.weights);
  const expected = buildPhysicalStressComposite(readings);
  const actualState = state(raw.state);
  const actualIndex = nullableFinite(raw.index);

View on GitHub (pinned to 9361220cc0)

Solutions

  1. Delete/regenerate the stored composite (invalidate the cache key) so it is rebuilt from current readings under the current contract.
  2. Check PHYSICAL_DIVERGENCE_CONTRACT and PHYSICAL_DIVERGENCE_METHODOLOGY_VERSION: if the contract changed, migrate or purge old snapshots rather than re-validating them.
  3. Fix the writer that produced the duplicate-metal entry so new snapshots contain exactly one weight per metal.
  4. Update test fixtures / seed data to include exactly the metals in PHYSICAL_DIVERGENCE_METALS.

Example fix

// before (stored weights)
[{ metal: 'gold', weight: 0.6 }, { metal: 'gold', weight: 0.4 }]
// after
[{ metal: 'gold', weight: 0.6 }, { metal: 'silver', weight: 0.4 }]
Defensive patterns

Strategy: type-guard

Validate before calling

const metals = new Set(weights.map((w) => w.metal));
if (metals.size !== weights.length) throw new Error('duplicate metal in composite weights');

Type guard

function hasUniqueMetals(weights: ReadonlyArray<{ metal: string }>, expected: readonly string[]): boolean {
  const metals = new Set(weights.map((w) => w.metal));
  return metals.size === expected.length && expected.every((m) => metals.has(m));
}

Try / catch

try {
  const snapshot = normalizePhysicalDivergenceSnapshot(stored, Date.now());
} catch (err) {
  if (err instanceof TypeError && /repeats a metal weight|invalid weight/.test(err.message)) {
    stored = await rebuildSnapshotFromReadings(); // purge + regenerate corrupt cache entry
  } else throw err;
}

Prevention

When it happens

Trigger: Loading/normalizing a stored composite snapshot whose weights array contains two entries for the same metal (e.g. two gold entries, silver missing), typically produced by an older methodology version or hand-edited/corrupted cache data.

Common situations: Redis/seeded cache written by an earlier code version whose contract changed; a partially overwritten snapshot; a test fixture with duplicated metals; manual cache surgery during an incident.

Related errors


AI-assisted analysis of koala73/worldmonitor@9361220cc0 (2026-09-01). Data as JSON: /api/errors/e1cfaa738cc99f56. Report an issue: GitHub.