File size: 3,719 Bytes
ee888e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
 * Dashboard-sized projection of the forecast feed (#5300).
 *
 * `forecast:predictions:v2` is 188 KB for 15 predictions, and **78% of it is
 * `caseFile`** β€” the per-forecast evidence dossier (~19,000 words of prose across
 * branches, actors, worldState, supporting/counter evidence, escalatory and
 * contrarian cases, triggers).
 *
 * That dossier is read in exactly ONE place: `ForecastPanel.renderDetailBody()`,
 * which mounts it eagerly into `<div class="fc-detail fc-hidden">` and reveals it
 * with a hover-only "Analysis" toggle. So today every visitor:
 *   - pulls 146 KB of dossiers out of Redis on every bootstrap origin miss,
 *   - downloads them on every page load, and
 *   - has ~19,000 words serialised to HTML and parsed into the DOM at panel
 *     render β€” on a priority-1 panel, i.e. inside the LCP/INP window β€”
 * for content the overwhelming majority never expand.
 *
 * The bootstrap key therefore carries the LIST the panel renders (188 KB -> 41 KB).
 * The canonical key keeps the dossiers and still serves the RPC, the MCP widget and
 * chat-analyst-context; the panel fetches a dossier lazily when someone actually
 * opens one.
 *
 * NOTE: this file must not import anything outside `scripts/` β€” Railway builds the
 * seeders from a scripts-only Nixpacks root, and a `../api/` import crashes the
 * container at startup (#5268 took the wildfire feed down for ~6h exactly that way).
 */

/** Fields dropped from the dashboard list. Only `caseFile` today β€” it is 78% of the payload. */
export const FORECAST_DETAIL_FIELDS = ['caseFile'];

/**
 * Top-level fields the dashboard key may carry. An ALLOWLIST, deliberately.
 *
 * This is the whole lesson of the incident below: `runSeed` feeds `publishTransform(data)`
 * to the canonical key but feeds RAW `data` to every extraKey transform
 * (scripts/_seed-utils.mjs β€” `publishData` at the canonical write vs `ekData` in the
 * extraKeys loop). For seed-forecasts, raw `data` is the full internal pipeline state:
 * fullRunPredictions, inputs, publishSelectionPool, situationClusters, situationFamilies,
 * stateUnits, enrichmentMeta, publishTelemetry, selection* β€” ~11 MB of trace.
 *
 * The first version of this function spread that object (`{ ...payload }`) and merely
 * deleted `caseFile` from each prediction. It therefore published an 11.5 MB dashboard
 * key β€” 66x LARGER than the 172 KB canonical key it was supposed to compact β€” and every
 * bootstrap origin miss pulled all of it. A denylist cannot be safe against an input
 * whose shape you do not control. Name what you keep, never what you drop.
 */
export const FORECAST_DASHBOARD_TOP_LEVEL_FIELDS = ['generatedAt', 'predictions'];

export function compactForecastDashboardPayload(payload) {
  if (!payload || typeof payload !== 'object' || !Array.isArray(payload.predictions)) return payload;

  let stripped = 0;
  const predictions = payload.predictions.map((prediction) => {
    if (!prediction || typeof prediction !== 'object') return prediction;
    let touched = false;
    const next = { ...prediction };
    for (const field of FORECAST_DETAIL_FIELDS) {
      if (next[field] !== undefined) {
        delete next[field];
        touched = true;
      }
    }
    if (touched) stripped += 1;
    // Tell the client the dossier exists but is not here, so it can lazily fetch
    // one instead of rendering an empty Analysis pane.
    return touched ? { ...next, hasCaseFile: true } : next;
  });

  const compact = { predictions, detailStripped: stripped };
  for (const field of FORECAST_DASHBOARD_TOP_LEVEL_FIELDS) {
    if (field !== 'predictions' && payload[field] !== undefined) compact[field] = payload[field];
  }
  return compact;
}