Files
worldmonitor/scripts/regional-snapshot/scenario-builder.mjs
Elie Habib da45e830c9 fix(regional-snapshots): mirror shared/geography.js into scripts/shared (#2954)
The derived-signals seed bundle failed on Railway with:

  [Regional-Snapshots] Error [ERR_MODULE_NOT_FOUND]: Cannot find module
    '/shared/geography.js' imported from /app/seed-regional-snapshots.mjs

Root cause: the Railway cron service for derived-signals deploys with
rootDirectory=scripts, so scripts/ becomes the container /app root. The
repo-root shared/ folder is NOT copied into the container.

seed-regional-snapshots.mjs imported '../shared/geography.js' which
resolved to /shared/geography.js at runtime and blew up. Same for four
scripts/regional-snapshot/* compute modules using '../../shared/...'.

Fix: mirror the runtime shared assets into scripts/shared/ and update
the five import paths.

- Add scripts/shared/geography.js (byte-for-byte copy of shared/geography.js)
- Add scripts/shared/package.json with {"type": "module"} so Node
  parses geography.js as ESM inside the rootDirectory=scripts deploy.
  scripts/package.json does NOT set type:module.
- scripts/shared/iso2-to-region.json and iso3-to-iso2.json were already
  mirrored from prior PRs, no change needed.
- scripts/seed-regional-snapshots.mjs:
    '../shared/geography.js' -> './shared/geography.js'
- scripts/regional-snapshot/actor-scoring.mjs:
  scripts/regional-snapshot/evidence-collector.mjs:
  scripts/regional-snapshot/scenario-builder.mjs:
    '../../shared/geography.js' -> '../shared/geography.js'
- scripts/regional-snapshot/balance-vector.mjs:
    '../../shared/geography.js' -> '../shared/geography.js'
    '../../shared/iso3-to-iso2.json' -> '../shared/iso3-to-iso2.json'

JSDoc type imports like {import('../../shared/regions.types.js').X}
are intentionally NOT changed. They live inside /** */ comments,
Node ignores them at runtime, and tsc still resolves them correctly
from the repo root during local typecheck.

Regression coverage: new tests/scripts-shared-mirror.test.mjs
1. Asserts scripts/shared/{geography.js, iso2-to-region.json,
   iso3-to-iso2.json, un-to-iso2.json} are byte-for-byte identical
   to their shared/ canonical counterparts.
2. Asserts scripts/shared/package.json has type:module.
3. For each regional-snapshot seed file, resolves every runtime
   "import ... from ...shared/..." to an absolute path and asserts
   it lands inside scripts/shared/. A regression that reintroduces
   "../shared/" from a scripts/ file or "../../shared/" from a
   scripts/regional-snapshot/ file will fail this check.

Verified with a temp-dir simulation of rootDirectory=scripts: all
five modules load clean. Full npm run test:data suite passes 4298/4298.

Secondary observation (not in this PR): the same log shows
Cross-Source-Signals missing 4 upstream keys (supply_chain:shipping:v2,
gdelt:intel:tone:{military,nuclear,maritime}), producing 0 composite
escalation zones. That is an upstream data-freshness issue in the
supply-chain and gdelt-intel seeders, not in this bundle.
2026-04-11 20:19:01 +04:00

96 lines
3.7 KiB
JavaScript

// @ts-check
// Builds scenario sets per horizon, normalized so lane probabilities sum to 1.0.
// See docs/internal/pro-regional-intelligence-appendix-scoring.md
// "Scenario Set Normalization".
import { num } from './_helpers.mjs';
// Use scripts/shared mirror (not repo-root shared/): Railway service has
// rootDirectory=scripts so ../../shared/ escapes the deploy root.
import { REGIONS } from '../shared/geography.js';
/** @type {import('../../shared/regions.types.js').ScenarioHorizon[]} */
const HORIZONS = ['24h', '7d', '30d'];
/** @type {import('../../shared/regions.types.js').ScenarioName[]} */
const LANE_NAMES = ['base', 'escalation', 'containment', 'fragmentation'];
/**
* @param {string} regionId
* @param {Record<string, any>} sources
* @param {import('../../shared/regions.types.js').TriggerLadder} triggers
* @returns {import('../../shared/regions.types.js').ScenarioSet[]}
*/
export function buildScenarioSets(regionId, sources, triggers) {
const region = REGIONS.find((r) => r.id === regionId);
if (!region) return [];
const fc = sources['forecast:predictions:v2'];
const forecasts = Array.isArray(fc?.predictions) ? fc.predictions : [];
const inRegion = forecasts.filter((f) => {
const fRegion = String(f?.region ?? '').toLowerCase();
return fRegion.includes(region.forecastLabel.toLowerCase());
});
return HORIZONS.map((horizon) => {
const lanes = LANE_NAMES.map((name) => buildLane(name, horizon, inRegion, triggers));
return { horizon, lanes: normalize(lanes) };
});
}
function buildLane(name, horizon, forecasts, triggers) {
// Raw score sources:
// 1. Forecasts whose trend matches the lane direction in this horizon
// 2. Active trigger count for this lane (each adds 0.1 boost)
// 3. Default base case score for stability
let rawScore = name === 'base' ? 0.4 : 0.1;
for (const f of forecasts) {
const fHorizon = String(f?.timeHorizon ?? '').toLowerCase();
if (!matchesHorizon(fHorizon, horizon)) continue;
const trend = String(f?.trend ?? '').toLowerCase();
const prob = num(f?.probability, 0);
if (name === 'escalation' && (trend === 'rising' || trend === 'escalating')) rawScore += prob * 0.5;
if (name === 'containment' && (trend === 'falling' || trend === 'de-escalating')) rawScore += prob * 0.5;
if (name === 'base' && trend === 'stable') rawScore += prob * 0.3;
if (name === 'fragmentation') {
const cf = JSON.stringify(f?.caseFile ?? {}).toLowerCase();
if (/fragment|collapse|breakdown/.test(cf)) rawScore += prob * 0.4;
}
}
const activeForLane = triggers.active.filter((t) => t.scenario_lane === name).length;
rawScore += activeForLane * 0.1;
const triggerIds = [
...triggers.active.filter((t) => t.scenario_lane === name).map((t) => t.id),
...triggers.watching.filter((t) => t.scenario_lane === name).map((t) => t.id),
];
return {
name,
probability: Math.max(0, rawScore),
trigger_ids: triggerIds,
consequences: [],
transmissions: [],
};
}
function matchesHorizon(forecastHorizon, targetHorizon) {
if (!forecastHorizon) return targetHorizon === '7d';
if (targetHorizon === '24h') return /h24|24h|day|24h/.test(forecastHorizon);
if (targetHorizon === '7d') return /d7|7d|week|d7/.test(forecastHorizon);
if (targetHorizon === '30d') return /d30|30d|month|d30/.test(forecastHorizon);
return false;
}
function normalize(lanes) {
const total = lanes.reduce((sum, l) => sum + l.probability, 0);
if (total === 0) {
return lanes.map((l) => ({ ...l, probability: l.name === 'base' ? 1.0 : 0.0 }));
}
return lanes.map((l) => ({ ...l, probability: round(l.probability / total) }));
}
function round(n) {
return Math.round(n * 1000) / 1000;
}