Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 42x 3x 7x 7x 12x 12x 2x 2x 2x 4x 4x 4x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 6x 2x 4x 1x 3x 14x 14x 13x 13x 13x 17x 17x 17x 16x 16x 15x 15x 23x 23x 23x 22x 22x 22x 22x 2x 2x 2x 13x 18x 18x 18x 234x 234x 18x 7x 7x 7x 7x 15x 15x 12x 12x 143x 12x 7x 7x 5x 3x 1x 7x 11x 7x 217x 18x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x | #!/usr/bin/env node
// SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
/**
* @module scripts/discover-untranslated-briefs
* @description Discovery worker for the scheduled `news-translate` workflow.
*
* Scans every `analysis/daily/<date>/<slug>/executive-brief.md` source brief
* (including the legacy `extended/executive-brief.md` location) and computes,
* for each source, which of the 13 non-English translation companions
* (`executive-brief_<lang>.md`) are still missing.
*
* The script emits a prioritised JSON queue of work items that the AI
* translator should pick up, bounded by `--max-briefs` so a single workflow
* run cannot exceed its 60-minute budget. The default cap (2 briefs / run)
* comes from the sizing analysis recorded in the PR description: 2 briefs ×
* 13 languages × ~12 KB ≈ 26 markdown files / ~310 KB of generated output,
* comfortably inside the gh-aw safe-outputs 10 MB patch ceiling and the
* Claude Sonnet 4.6 60-minute wall-clock budget.
*
* Priority rules (newest-first, oldest-first within a brief):
*
* 1. Sort by `<date>` descending so today's briefs win.
* 2. Within the same date, briefs with *more* missing languages outrank
* briefs with fewer missing languages so partial coverage gets completed
* quickly.
* 3. Remaining ties sort by `<slug>` alphabetically so the run is
* deterministic and reviewers can predict which slugs land first.
*
* Invocation:
*
* node scripts/discover-untranslated-briefs.js \
* [--repo-root <path>] \
* [--max-briefs <n>] # default 2
* [--max-age-days <n>] # default 180; older briefs are skipped
* [--output <path>] # default stdout
* [--include-extended] # also scan extended/executive-brief.md
*
* Exit codes:
* 0 — queue successfully written (may be empty if everything is translated)
* 1 — unexpected error (I/O, invalid CLI input)
*
* Output JSON shape:
* {
* "generatedAt": "2026-05-16T08:24:16.909Z",
* "totals": {
* "sourcesScanned": 92,
* "sourcesWithGaps": 92,
* "translationsMissing": 1196,
* "queued": 2,
* "queuedTranslations": 26,
* "topMissingLangs": [
* { "lang": "ja", "count": 92 },
* { "lang": "ko", "count": 92 },
* { "lang": "zh", "count": 92 }
* ]
* },
* "queue": [
* {
* "date": "2026-05-15",
* "slug": "breaking",
* "sourcePath": "analysis/daily/2026-05-15/breaking/executive-brief.md",
* "missingLangs": ["sv","da","no","fi","de","fr","es","nl","ar","he","ja","ko","zh"],
* "missingCount": 13,
* "isExtended": false
* },
* ...
* ]
* }
*/
import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import { ALL_LANGUAGES } from './constants/language-core.js';
/** Canonical 13 non-English target language codes, derived from ALL_LANGUAGES. */
export const TARGET_LANGS = Object.freeze(ALL_LANGUAGES.filter((lang) => lang !== 'en'));
/** Manual-dispatch upper bound that keeps one 60-minute run inside budget. */
export const MAX_BRIEFS_LIMIT = 4;
/**
* Parse CLI argv into an options object. Exported for unit tests.
* @param {string[]} argv
*/
export function parseArgs(argv) {
const opts = {
repoRoot: process.cwd(),
maxBriefs: 2,
maxAgeDays: 180,
output: null,
includeExtended: false,
};
for (let i = 0; i < argv.length; i += 1) {
const arg = argv[i];
switch (arg) {
case '--repo-root':
opts.repoRoot = argv[i + 1];
i += 1;
break;
case '--max-briefs':
opts.maxBriefs = Number.parseInt(argv[i + 1], 10);
i += 1;
break;
case '--max-age-days':
opts.maxAgeDays = Number.parseInt(argv[i + 1], 10);
i += 1;
break;
case '--output':
opts.output = argv[i + 1];
i += 1;
break;
case '--include-extended':
opts.includeExtended = true;
break;
case '--help':
case '-h':
printHelp();
process.exit(0);
break;
default:
Eif (arg.startsWith('--')) {
throw new Error(`Unknown flag: ${arg}`);
}
}
}
if (
!Number.isFinite(opts.maxBriefs) ||
opts.maxBriefs < 1 ||
opts.maxBriefs > MAX_BRIEFS_LIMIT
) {
throw new Error(`--max-briefs must be an integer between 1 and ${MAX_BRIEFS_LIMIT}`);
}
if (!Number.isFinite(opts.maxAgeDays) || opts.maxAgeDays < 1) {
throw new Error('--max-age-days must be a positive integer');
}
return opts;
}
function printHelp() {
process.stdout.write(
'Usage: discover-untranslated-briefs.js [--repo-root <path>] ' +
'[--max-briefs <n>] [--max-age-days <n>] [--output <path>] ' +
'[--include-extended]\n'
);
}
/**
* Return the absolute path of every `executive-brief.md` (and optionally
* `extended/executive-brief.md`) under `analysis/daily/`. Sources are
* filtered to the last `maxAgeDays` so the queue stays focused on recent
* material.
*
* @param {string} repoRoot
* @param {{ includeExtended: boolean, maxAgeDays: number }} options
* @returns {Array<{ absPath: string, relPath: string, date: string, slug: string, isExtended: boolean }>}
*/
export function findExecutiveBriefSources(repoRoot, options) {
const dailyDir = path.join(repoRoot, 'analysis', 'daily');
if (!fs.existsSync(dailyDir)) return [];
const cutoffMs = Date.now() - options.maxAgeDays * 24 * 60 * 60 * 1000;
const sources = [];
for (const dateEntry of fs.readdirSync(dailyDir, { withFileTypes: true })) {
Iif (!dateEntry.isDirectory()) continue;
const date = dateEntry.name;
if (!/^\d{4}-\d{2}-\d{2}$/.test(date)) continue;
const dateMs = Date.parse(`${date}T00:00:00Z`);
if (Number.isFinite(dateMs) && dateMs < cutoffMs) continue;
const dateDir = path.join(dailyDir, date);
for (const slugEntry of fs.readdirSync(dateDir, { withFileTypes: true })) {
Iif (!slugEntry.isDirectory()) continue;
const slug = slugEntry.name;
// Skip translate-run* dirs (legacy artifacts from the previous workflow)
if (slug.startsWith('translate-run')) continue;
const direct = path.join(dateDir, slug, 'executive-brief.md');
Eif (fs.existsSync(direct)) {
sources.push({
absPath: direct,
relPath: path.relative(repoRoot, direct),
date,
slug,
isExtended: false,
});
}
if (options.includeExtended) {
const extended = path.join(dateDir, slug, 'extended', 'executive-brief.md');
Eif (fs.existsSync(extended)) {
sources.push({
absPath: extended,
relPath: path.relative(repoRoot, extended),
date,
slug,
isExtended: true,
});
}
}
}
}
return sources;
}
/**
* For a single source brief, compute which `executive-brief_<lang>.md`
* companions are missing in the same directory.
*
* @param {{ absPath: string }} source
* @returns {string[]} sorted list of missing language codes (subset of TARGET_LANGS)
*/
export function findMissingLangs(source) {
const dir = path.dirname(source.absPath);
const missing = [];
for (const lang of TARGET_LANGS) {
const target = path.join(dir, `executive-brief_${lang}.md`);
if (!fs.existsSync(target)) missing.push(lang);
}
return missing;
}
/**
* Build the prioritised queue. See module docstring for ordering rules.
*
* @param {ReturnType<typeof findExecutiveBriefSources>} sources
* @param {number} maxBriefs
*/
export function buildQueue(sources, maxBriefs) {
const withGaps = [];
let totalMissing = 0;
const missingByLang = new Map();
for (const source of sources) {
const missing = findMissingLangs(source);
if (missing.length === 0) continue;
totalMissing += missing.length;
for (const lang of missing) {
missingByLang.set(lang, (missingByLang.get(lang) || 0) + 1);
}
withGaps.push({
date: source.date,
slug: source.slug,
sourcePath: source.relPath,
missingLangs: missing,
missingCount: missing.length,
isExtended: source.isExtended,
});
}
// Sort: newest date first; then more-missing first (finish partial briefs);
// then slug alphabetical for determinism.
withGaps.sort((a, b) => {
if (a.date !== b.date) return a.date < b.date ? 1 : -1;
if (a.missingCount !== b.missingCount) return b.missingCount - a.missingCount;
if (a.slug !== b.slug) return a.slug < b.slug ? -1 : 1;
// Prefer non-extended over extended when both exist for the same slug
Eif (a.isExtended !== b.isExtended) return a.isExtended ? 1 : -1;
return 0;
});
const queue = withGaps.slice(0, maxBriefs);
const queuedTranslations = queue.reduce((sum, item) => sum + item.missingCount, 0);
// Top 3 most-blocked target languages across the entire backlog. Operators
// skim this to spot e.g. "Japanese keeps falling behind" without parsing
// the full queue. Sort by count desc, then language code asc for stable
// tie-breaking.
const topMissingLangs = [...missingByLang.entries()]
.sort((a, b) => (b[1] - a[1]) || (a[0] < b[0] ? -1 : 1))
.slice(0, 3)
.map(([lang, count]) => ({ lang, count }));
return {
totals: {
sourcesScanned: sources.length,
sourcesWithGaps: withGaps.length,
translationsMissing: totalMissing,
queued: queue.length,
queuedTranslations,
topMissingLangs,
},
queue,
};
}
/**
* Main entry point.
* @param {string[]} argv
*/
export function main(argv) {
const opts = parseArgs(argv);
const sources = findExecutiveBriefSources(opts.repoRoot, {
includeExtended: opts.includeExtended,
maxAgeDays: opts.maxAgeDays,
});
const { totals, queue } = buildQueue(sources, opts.maxBriefs);
const payload = {
generatedAt: new Date().toISOString(),
options: {
maxBriefs: opts.maxBriefs,
maxAgeDays: opts.maxAgeDays,
includeExtended: opts.includeExtended,
},
totals,
queue,
};
const out = `${JSON.stringify(payload, null, 2)}\n`;
if (opts.output) {
fs.mkdirSync(path.dirname(opts.output), { recursive: true });
fs.writeFileSync(opts.output, out);
} else E{
process.stdout.write(out);
}
return payload;
}
/* c8 ignore start */
if (import.meta.url === `file://${process.argv[1]}`) {
try {
main(process.argv.slice(2));
} catch (err) {
process.stderr.write(`discover-untranslated-briefs: ${err.message}\n`);
process.exit(1);
}
}
/* c8 ignore stop */
|