File size: 14,898 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/env node

import { createRequire } from 'node:module';
// #4919: story similarity is delegated to the shared story-identity
// module (scripts/shared mirror — same rootDirectory=scripts reason as
// the JSON requires below). The local Jaccard-0.5 matcher this file
// carried was one of three inconsistent "same story?" answers in the
// codebase; all three now share ONE definition and threshold.
import { clusterTexts } from './shared/story-identity.js';

const require = createRequire(import.meta.url);
const SOURCE_TIERS = require('./shared/source-tiers.json');
// scripts/shared/ mirror (NOT ../shared/): seed-insights.mjs deploys via
// nixpacks with rootDirectory=scripts, so the repo-root shared/ folder
// is not in the container. Matches the SOURCE_TIERS pattern above.
const DIPLOMACY_KEYWORDS_DATA = require('./shared/diplomacy-keywords.json');

const ENTITY_CORROBORATION_WINDOW_MS = 24 * 60 * 60 * 1000;


const MILITARY_KEYWORDS = [
  'war', 'armada', 'invasion', 'airstrike', 'strike', 'missile', 'troops',
  'deployed', 'offensive', 'artillery', 'bomb', 'combat', 'fleet', 'warship',
  'carrier', 'navy', 'airforce', 'deployment', 'mobilization', 'attack',
];

const VIOLENCE_KEYWORDS = [
  'killed', 'dead', 'death', 'shot', 'blood', 'massacre', 'slaughter',
  'fatalities', 'casualties', 'wounded', 'injured', 'murdered', 'execution',
  'crackdown', 'violent', 'clashes', 'gunfire', 'shooting',
];

const UNREST_KEYWORDS = [
  'protest', 'protests', 'uprising', 'revolt', 'revolution', 'riot', 'riots',
  'demonstration', 'unrest', 'dissent', 'rebellion', 'insurgent', 'overthrow',
  'coup', 'martial law', 'curfew', 'shutdown', 'blackout',
];

const FLASHPOINT_KEYWORDS = DIPLOMACY_KEYWORDS_DATA.flashpointKeywords;
export const DIPLOMACY_KEYWORDS = DIPLOMACY_KEYWORDS_DATA.diplomacyKeywords;
export const ENTITY_BIGRAMS = DIPLOMACY_KEYWORDS_DATA.diplomacyFlashpointPairs;

const CRISIS_KEYWORDS = [
  'crisis', 'emergency', 'catastrophe', 'disaster', 'collapse', 'humanitarian',
  'sanctions', 'ultimatum', 'threat', 'retaliation', 'escalation', 'tensions',
  'breaking', 'urgent', 'developing', 'exclusive',
];

const DEMOTE_KEYWORDS = [
  'ceo', 'earnings', 'stock', 'startup', 'data center', 'datacenter', 'revenue',
  'quarterly', 'profit', 'investor', 'ipo', 'funding', 'valuation',
];


function finiteNumber(value, fallback = 0) {
  const n = Number(value);
  return Number.isFinite(n) ? n : fallback;
}

function toFiniteMs(value) {
  if (typeof value === 'number' && Number.isFinite(value)) return value;
  if (typeof value === 'string' && value.trim().length > 0) {
    const ms = new Date(value).getTime();
    return Number.isFinite(ms) ? ms : 0;
  }
  return 0;
}

function getItemPubMs(item) {
  if (item?.pubDateMissing === true) return 0;
  return toFiniteMs(item?.pubDate ?? item?.publishedAt ?? item?.date);
}

function normalizeSourceName(source) {
  return typeof source === 'string' ? source.trim() : '';
}

function sourceTierFor(source) {
  const tier = SOURCE_TIERS[source];
  return Number.isFinite(tier) ? tier : 4;
}

function sourceTierForSources(sources) {
  if (!Array.isArray(sources) || sources.length === 0) return 4;
  return Math.min(...sources.map(sourceTierFor));
}

function normalizeThreatLevel(level) {
  if (typeof level !== 'string') return '';
  const upper = level.toUpperCase();
  if (upper.startsWith('THREAT_LEVEL_')) {
    const suffix = upper.slice('THREAT_LEVEL_'.length).toLowerCase();
    return suffix === 'unspecified' ? 'info' : suffix;
  }
  return level.toLowerCase();
}

function isLlmThreatSource(source) {
  return source === 'llm';
}

function normalizedMatchText(text) {
  return (text || '')
    .toLowerCase()
    .replace(/[^a-z0-9\s]/g, ' ')
    .replace(/\s+/g, ' ')
    .trim();
}

// Word-start containment in normalizedMatchText output. Mirrors
// shared/brief-filter.js:containsKeywordToken — prevents 'pact' inside
// 'impact' (false positive) while still matching 'iran' inside
// 'iranian' (demonym preserved). PR #3909 review (P2).
function containsKeywordToken(text, kw) {
  if (!kw) return false;
  const escaped = kw.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  return new RegExp(`(^|\\s)${escaped}`).test(text);
}

export function clusterItems(items) {
  if (items.length === 0) return [];

  // #4924 review (maintainability P1): delegate the clustering ALGORITHM
  // to the shared module too, not just the similarity function — a local
  // copy of the loop would let clustering semantics drift apart again,
  // one layer above the drift this PR removed.
  const clusters = clusterTexts(items.map(item => item.title || ''))
    .map(indices => indices.map(idx => items[idx]));

  return clusters.map(group => {
    const sorted = [...group].sort((a, b) => {
      const tierA = finiteNumber(a.tier, sourceTierFor(a.source));
      const tierB = finiteNumber(b.tier, sourceTierFor(b.source));
      const tierDiff = tierA - tierB;
      if (tierDiff !== 0) return tierDiff;
      return getItemPubMs(b) - getItemPubMs(a);
    });

    const primary = sorted[0];
    const sources = [...new Set(group.map(i => normalizeSourceName(i.source)).filter(Boolean))]
      .sort((a, b) => sourceTierFor(a) - sourceTierFor(b) || a.localeCompare(b));
    const sourceTier = sourceTierForSources(sources);
    const publishedTimes = group.map(getItemPubMs).filter(ms => ms > 0);
    const lastUpdatedMs = publishedTimes.length > 0 ? Math.max(...publishedTimes) : getItemPubMs(primary);
    const upstreamImportanceScore = group.reduce(
      (max, item) => Math.max(max, finiteNumber(item.importanceScore, 0)),
      0,
    );
    const corroborationCount = group.reduce((max, item) => {
      const itemCount = finiteNumber(item.corroborationCount ?? item.storyMeta?.sourceCount, 0);
      return Math.max(max, itemCount);
    }, 0);
    const threatItem = sorted.find(i => i.threat?.level && isLlmThreatSource(i.threat?.source));
    return {
      primaryTitle: primary.title,
      primarySource: primary.source,
      primaryLink: primary.link,
      pubDate: primary.pubDate,
      sourceCount: group.length,
      sources,
      lastUpdated: lastUpdatedMs > 0 ? new Date(lastUpdatedMs).toISOString() : primary.pubDate,
      memberTitles: group.map(i => i.title).filter(Boolean),
      sourceTier,
      upstreamImportanceScore,
      corroborationCount,
      isAlert: group.some(i => i.isAlert),
      threat: threatItem?.threat ? { ...threatItem.threat } : (primary.threat ? { ...primary.threat } : undefined),
    };
  });
}

function countMatches(text, keywords) {
  return keywords.filter(kw => text.includes(kw)).length;
}

function publisherCount(cluster) {
  return Math.max(
    Array.isArray(cluster.sources) ? cluster.sources.length : 0,
    finiteNumber(cluster.corroborationSourceCount, 0),
    finiteNumber(cluster.corroborationCount, 0),
    1,
  );
}

function hasStrongNonKeywordSignal(cluster) {
  const level = normalizeThreatLevel(cluster.threat?.level);
  return isLlmThreatSource(cluster.threat?.source) && (level === 'high' || level === 'critical');
}

/**
 * @param {object} cluster
 * @param {{ demoteFinance?: boolean }} [opts] #4922 (f): the ×0.35 finance
 *   demotion is correct for the geopolitical World Brief but backwards for
 *   a finance-focused ranking surface. Pass demoteFinance:false to rank
 *   finance neutrally. NOTE: the only live consumer today (seed-insights)
 *   runs the full variant and keeps the default — this parameter is the
 *   seam a finance-variant insights run plugs into (tracked in #4922).
 */
export function scoreImportance(cluster, opts = {}) {
  let score = 0;
  const titleLower = normalizedMatchText(cluster.primaryTitle);
  const upstream = finiteNumber(cluster.upstreamImportanceScore, 0);
  if (upstream > 0) score += upstream * 2.2;

  const level = normalizeThreatLevel(cluster.threat?.level);
  const threatScores = { critical: 220, high: 150, medium: 80, low: 20, info: 0 };
  if (level && isLlmThreatSource(cluster.threat?.source)) {
    score += threatScores[level] ?? 0;
  } else if (level && upstream > 0 && cluster.threat?.source !== 'keyword-historical-downgrade') {
    score += (threatScores[level] ?? 0) * 0.35;
  }

  const sourceTier = finiteNumber(cluster.sourceTier, sourceTierFor(cluster.primarySource));
  score += sourceTier === 1 ? 35 : sourceTier === 2 ? 20 : sourceTier === 3 ? 8 : 0;

  const sourcesN = publisherCount(cluster);
  score += Math.min(sourcesN, 6) * 12;
  if (cluster.entityCorroboration) score += 45;

  const violenceN = countMatches(titleLower, VIOLENCE_KEYWORDS);
  if (violenceN > 0) score += 50 + violenceN * 12;

  const militaryN = countMatches(titleLower, MILITARY_KEYWORDS);
  if (militaryN > 0) score += 40 + militaryN * 10;

  const unrestN = countMatches(titleLower, UNREST_KEYWORDS);
  if (unrestN > 0) score += 35 + unrestN * 9;

  const flashpointN = countMatches(titleLower, FLASHPOINT_KEYWORDS);
  if (flashpointN > 0) score += 30 + flashpointN * 8;

  const diplomacyN = countMatches(titleLower, DIPLOMACY_KEYWORDS);
  if (diplomacyN > 0) score += 35 + diplomacyN * 9;
  if ((violenceN > 0 || unrestN > 0 || diplomacyN > 0) && flashpointN > 0) score *= 1.25;

  const crisisN = countMatches(titleLower, CRISIS_KEYWORDS);
  if (crisisN > 0) score += 15 + crisisN * 5;

  const demoteN = countMatches(titleLower, DEMOTE_KEYWORDS);
  const demoteFinance = opts.demoteFinance !== false;
  if (demoteFinance && demoteN > 0 && !cluster.entityCorroboration && !hasStrongNonKeywordSignal(cluster)) score *= 0.35;

  return score;
}

export function recencyWeight(cluster, nowMs = Date.now()) {
  const updatedMs = toFiniteMs(cluster?.lastUpdated ?? cluster?.pubDate);
  if (updatedMs <= 0) return 1;
  const ageHours = Math.max(0, (nowMs - updatedMs) / 3600000);
  return Math.max(0.5, 1 - ageHours / 16);
}

export function isBriefLeadEligible(cluster) {
  const uniqueSources = Array.isArray(cluster?.sources)
    ? cluster.sources.filter(s => typeof s === 'string' && s.trim().length > 0).length
    : 0;
  return uniqueSources >= 2 || cluster?.entityCorroboration === true;
}

export function isTopStoriesAdmissible(cluster, score) {
  return isBriefLeadEligible(cluster) || cluster?.isAlert === true || score > 100;
}

function entityKeysForCluster(cluster) {
  const titles = Array.isArray(cluster.memberTitles) && cluster.memberTitles.length > 0
    ? cluster.memberTitles
    : [cluster.primaryTitle];
  const keys = new Set();
  for (const title of titles) {
    const text = normalizedMatchText(title);
    for (const [entity, action] of ENTITY_BIGRAMS) {
      if (containsKeywordToken(text, entity) && containsKeywordToken(text, action)) {
        keys.add(`${entity}:${action}`);
      }
    }
  }
  return keys;
}

export function computeEntityCorroboration(clusters, nowMs = Date.now()) {
  if (!Array.isArray(clusters) || clusters.length === 0) return clusters;
  const buckets = new Map();
  for (const cluster of clusters) {
    cluster.entityCorroboration = false;
    cluster.corroborationSourceCount = 0;
    const updatedMs = toFiniteMs(cluster.lastUpdated ?? cluster.pubDate);
    if (updatedMs <= 0 || nowMs - updatedMs > ENTITY_CORROBORATION_WINDOW_MS) continue;
    for (const key of entityKeysForCluster(cluster)) {
      let bucket = buckets.get(key);
      if (!bucket) {
        bucket = { clusters: [], sources: new Set() };
        buckets.set(key, bucket);
      }
      bucket.clusters.push(cluster);
      for (const source of cluster.sources ?? []) {
        const normalized = normalizeSourceName(source);
        if (normalized) bucket.sources.add(normalized);
      }
    }
  }

  for (const bucket of buckets.values()) {
    if (bucket.sources.size < 2) continue;
    for (const cluster of bucket.clusters) {
      cluster.entityCorroboration = true;
      cluster.corroborationSourceCount = Math.max(
        finiteNumber(cluster.corroborationSourceCount, 0),
        bucket.sources.size,
      );
    }
  }
  return clusters;
}

// Note: velocity filter omitted (vs frontend selectTopStories) because digest
// items lack velocity data. Phase B may add velocity when RPC provides it.
/**
 * @param {object[]} clusters
 * @param {number} [maxCount]
 * @param {{ considered?: number; admissibilityDropped?: number; sourceCapDropped?: number; overflowDropped?: number }} [stats]
 *   #4920 coverage ledger: when provided, populated with how many clusters
 *   each gate dropped — previously all three gates were silent.
 */
// biome-ignore lint/style/useDefaultParameterLast: maxCount's default predates the trailing params; reordering would break the (clusters, maxCount, stats) call shape
export function selectTopStories(clusters, maxCount = 8, stats, opts = {}) {
  // Positional-arg guard (#4929 external review): a caller passing
  // { demoteFinance } in the stats slot would silently get default
  // demotion AND a stats-shaped object mutated with counters. Detect the
  // opts shape and shift.
  if (stats && typeof stats === 'object' && 'demoteFinance' in stats
      && (!opts || Object.keys(opts).length === 0)) {
    opts = stats;
    stats = undefined;
  }
  const nowMs = Date.now();
  computeEntityCorroboration(clusters, nowMs);
  const admissible = [];
  let admissibilityDropped = 0;
  for (const c of clusters) {
    const score = scoreImportance(c, opts);
    if (isTopStoriesAdmissible(c, score)) {
      admissible.push({ cluster: c, score, effectiveScore: score * recencyWeight(c, nowMs) });
    } else {
      admissibilityDropped++;
    }
  }
  admissible.sort((a, b) => b.effectiveScore - a.effectiveScore || b.score - a.score);

  const selected = [];
  const sourceCount = new Map();
  const MAX_PER_SOURCE = 3;
  let sourceCapDropped = 0;
  let overflowDropped = 0;

  // #4927 review P2: classify EVERY admissible candidate — breaking at
  // maxCount lumped later same-source candidates into overflow arithmetic
  // even though the source cap would have rejected them regardless of
  // room. Cap-first attribution: a candidate whose source already hit the
  // per-source cap is a sourceCap drop; only genuinely rankable candidates
  // count as overflow.
  for (const { cluster, score, effectiveScore } of admissible) {
    const source = cluster.primarySource;
    const count = sourceCount.get(source) || 0;
    if (count >= MAX_PER_SOURCE) {
      sourceCapDropped++;
      continue;
    }
    if (selected.length >= maxCount) {
      overflowDropped++;
      continue;
    }
    selected.push({ ...cluster, importanceScore: score, effectiveImportanceScore: effectiveScore });
    sourceCount.set(source, count + 1);
  }

  if (stats && typeof stats === 'object') {
    stats.considered = clusters.length;
    stats.admissibilityDropped = admissibilityDropped;
    stats.sourceCapDropped = sourceCapDropped;
    stats.overflowDropped = overflowDropped;
  }

  return selected;
}