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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | 2x 2x 2x 2x 426x 2x 2x 2x 2x 2x 2x 2x 61x 61x 44x 44x 44x 44x 44x 44x 3x 41x 11x 41x 10x 44x 8x 44x 11x 44x 334x 3x 3x 44x 44x 44x 3x 41x 13x 41x 1x 41x 1x 1x 1x 1x 41x 465x 3x 3x 41x 1x 44x 2x | /**
* @module Infrastructure/BackfillLib/ContractChecker
* @category Intelligence Operations / Supporting Infrastructure
* @name SEO metadata contract checker (PR 2 of the 5-PR SEO rescue plan)
*
* @description
* Encodes every rule in `.github/prompts/seo-metadata-contract.md` as a
* machine-readable check that returns a `{ ok, violations[] }` result
* with per-field violation codes suitable for the CSV diff report
* consumed by PR 3 (Tier A regenerate), PR 4 (Tier B rewrite) and PR 5
* (Tier C translation repair + CI gate).
*
* ## Rules encoded
*
* | Contract § | Code | Field |
* | ---------- | ------------------------------- | ------------- |
* | §2.1/§4 | `TITLE_EMPTY` | `title` |
* | §2.1/§4 | `TITLE_TOO_SHORT` | `title` |
* | §2.1/§4 | `TITLE_TOO_LONG` | `title` |
* | §2.2 | `TITLE_HAS_ISO_DATE` | `title` |
* | §2.2 | `TITLE_HAS_BANNED_PHRASE` | `title` |
* | §2.2 | `TITLE_ENDS_WITH_BRAND` | `title` |
* | §3.1/§4 | `DESCRIPTION_EMPTY` | `description` |
* | §3.1/§4 | `DESCRIPTION_TOO_SHORT` | `description` |
* | §3.1/§4 | `DESCRIPTION_TOO_LONG` | `description` |
* | §3.1 | `DESCRIPTION_HAS_ADMIN_LEAK` | `description` |
* | §3.1 | `DESCRIPTION_NOT_TERMINATED` | `description` |
* | §3.1 | `DESCRIPTION_TRUNCATED_MIDWORD` | `description` |
* | §3.1 | `DESCRIPTION_GENERIC_FILLER` | `description` |
*
* All codes are stable and form the public CSV contract. Renaming a
* code is a breaking change for PRs 3-5.
*
* @author Hack23 AB (Infrastructure Team)
* @license Apache-2.0
*/
/** Language code as used in the `news/*-$LANG.html` filename suffix. */
export type LangCode =
| 'en' | 'sv' | 'da' | 'no' | 'nb' | 'fi' | 'de' | 'fr' | 'es' | 'nl'
| 'ar' | 'he' | 'ja' | 'ko' | 'zh';
/** Per-language character-count window (visual SERP width). */
export interface LangWindow {
readonly titleMin: number;
readonly titleMax: number;
readonly descriptionMin: number;
readonly descriptionMax: number;
}
/** Latin-script budgets (en, sv, da, no/nb, fi, de, fr, es, nl). */
const LATIN_WINDOW: LangWindow = {
titleMin: 55,
titleMax: 70,
descriptionMin: 140,
descriptionMax: 200,
};
/** RTL budgets (ar, he). */
const RTL_WINDOW: LangWindow = {
titleMin: 45,
titleMax: 60,
descriptionMin: 120,
descriptionMax: 170,
};
/** CJK budgets (ja, ko, zh). */
const CJK_WINDOW: LangWindow = {
titleMin: 30,
titleMax: 45,
descriptionMin: 70,
descriptionMax: 120,
};
/** Contract §4 per-language windows. Exported for tests + report writers. */
export const LANG_WINDOWS: Record<LangCode, LangWindow> = {
en: LATIN_WINDOW,
sv: LATIN_WINDOW,
da: LATIN_WINDOW,
no: LATIN_WINDOW,
nb: LATIN_WINDOW,
fi: LATIN_WINDOW,
de: LATIN_WINDOW,
fr: LATIN_WINDOW,
es: LATIN_WINDOW,
nl: LATIN_WINDOW,
ar: RTL_WINDOW,
he: RTL_WINDOW,
ja: CJK_WINDOW,
ko: CJK_WINDOW,
zh: CJK_WINDOW,
};
/** Visual-width counter. CJK glyphs count as 1 glyph here; the windows in
* `LANG_WINDOWS` already encode the SERP-width ratio by using smaller
* numbers for CJK languages. `Array.from` preserves surrogate pairs. */
function visualLength(text: string): number {
return Array.from(text).length;
}
/** Contract §2.2 banned phrases for `<title>`. */
export const BANNED_TITLE_PHRASES: readonly RegExp[] = [
/Executive\s+Brief\s*[—\-–]/i,
/AI-generated\s+political\s+intelligence/i,
/\bBrief\s*ID\s*:/i,
/\bClassification\s*:/i,
/\bPrepared\s*(?:by|at)\s*:/i,
/\bAnalyst\s*:/i,
/\b60[-\s]?second\s+read\s*:/i,
/\bAdmiralty(?:\s+baseline)?\s*:/i,
];
/** Contract §3.1 admin-metadata phrases that must never leak into a
* description. */
export const BANNED_DESCRIPTION_PHRASES: readonly RegExp[] = [
/\bBrief\s*ID\s*:/i,
/\bClassification\s*:/i,
/\bPrepared\s*(?:by|at)\s*:/i,
/\bAnalyst\s*:/i,
/\b60[-\s]?second\s+read\s*:/i,
/\bAdmiralty(?:\s+baseline)?\s*:/i,
/\bDistribution\s*:/i,
/\bMethodology\s*:/i,
/\bReviewed\s*by\s*:/i,
/\bReviewer\s*:/i,
/\bDisseminated\s*:/i,
/\bRun\s*ID\s*:/i,
];
/** Literal ISO 8601 date — `YYYY-MM-DD` or `YYYY/MM/DD`. */
export const ISO_DATE_RE = /\b\d{4}[-/]\d{2}[-/]\d{2}\b/;
/** Trailing brand suffix that doubles up with the chrome template.
* Contract §2.2: the raw editorial `<title>` must not end in
* ` — Riksdagsmonitor` because the chrome template appends it for
* og:title / twitter:title only. */
export const TRAILING_BRAND_RE = /\s*[—\-–]\s*Riksdagsmonitor\s*$/;
/** Generic fallback phrases that the aggregator emitted when it could
* not find a real description (contract §1, issue "Generic filler"). */
export const GENERIC_FILLER_RE =
/AI-generated\s+political\s+intelligence|Evidence-based\s+political\s+intelligence\s+analysis\s+for/i;
/** Sentence-terminator check — the final character must be one of these.
* Mirrors `SENTENCE_END_RE` in `scripts/render-lib/aggregator.ts` and
* tolerates the Unicode ellipsis (intentional truncation marker). */
export const SENTENCE_TERMINATOR_RE = /[.!?…。।]$/;
/** Mid-word truncation heuristic — a description that ends with a
* letter (any script) with no terminator is almost certainly a blind
* `.slice(0, 300)` cut. */
export const MIDWORD_TRUNCATION_RE = /[\p{Ll}\p{Lu}\p{Lo}]$/u;
/** One violation entry. Stable shape — CSV writer + PRs 3/4/5 depend on
* these field names. */
export interface ContractViolation {
/** Stable machine-readable code (see module JSDoc table). */
readonly code: string;
/** Which field the violation applies to. */
readonly field: 'title' | 'description';
/** Human-readable diagnostic, safe for CSV escaping. */
readonly message: string;
/** Observed offending value (may be empty for `TITLE_EMPTY` etc.). */
readonly value: string;
}
/** Input to `checkAgainstContract`. */
export interface ContractInput {
readonly title: string;
readonly description: string;
}
/** Result of `checkAgainstContract`. */
export interface ContractResult {
readonly ok: boolean;
readonly violations: readonly ContractViolation[];
/** The language window actually used (helpful for diagnostics). */
readonly window: LangWindow;
}
/** Resolve a language code to its window, with fall-back to Latin for
* unknown codes. */
export function windowFor(lang: string): LangWindow {
const key = lang.toLowerCase() as LangCode;
return LANG_WINDOWS[key] ?? LATIN_WINDOW;
}
/**
* Check `{title, description}` against every rule in the contract.
*
* `ok` is `true` iff `violations` is empty. Order is stable so two runs
* against the same input produce the same CSV row ordering.
*/
export function checkAgainstContract(
meta: ContractInput,
lang: string,
): ContractResult {
const violations: ContractViolation[] = [];
const window = windowFor(lang);
const title = meta.title ?? '';
const description = meta.description ?? '';
// --- Title checks -------------------------------------------------------
const titleLen = visualLength(title.trim());
if (titleLen === 0) {
violations.push({
code: 'TITLE_EMPTY',
field: 'title',
message: 'Document <title> is missing or empty.',
value: '',
});
} else {
if (titleLen < window.titleMin) {
violations.push({
code: 'TITLE_TOO_SHORT',
field: 'title',
message: `Title is ${titleLen} chars (min ${window.titleMin} for lang=${lang}).`,
value: title,
});
}
if (titleLen > window.titleMax) {
violations.push({
code: 'TITLE_TOO_LONG',
field: 'title',
message: `Title is ${titleLen} chars (max ${window.titleMax} for lang=${lang}).`,
value: title,
});
}
}
if (ISO_DATE_RE.test(title)) {
violations.push({
code: 'TITLE_HAS_ISO_DATE',
field: 'title',
message: 'Title contains a literal YYYY-MM-DD / YYYY/MM/DD date.',
value: title,
});
}
if (TRAILING_BRAND_RE.test(title)) {
violations.push({
code: 'TITLE_ENDS_WITH_BRAND',
field: 'title',
message: 'Title ends with " — Riksdagsmonitor" (chrome template double-brand).',
value: title,
});
}
for (const re of BANNED_TITLE_PHRASES) {
if (re.test(title)) {
violations.push({
code: 'TITLE_HAS_BANNED_PHRASE',
field: 'title',
message: `Title contains a contract §2.2 banned phrase (${re.source}).`,
value: title,
});
break;
}
}
// --- Description checks -------------------------------------------------
const descTrimmed = description.trim();
const descLen = visualLength(descTrimmed);
if (descLen === 0) {
violations.push({
code: 'DESCRIPTION_EMPTY',
field: 'description',
message: 'Meta description is missing or empty.',
value: '',
});
} else {
if (descLen < window.descriptionMin) {
violations.push({
code: 'DESCRIPTION_TOO_SHORT',
field: 'description',
message: `Description is ${descLen} chars (min ${window.descriptionMin} for lang=${lang}).`,
value: description,
});
}
if (descLen > window.descriptionMax) {
violations.push({
code: 'DESCRIPTION_TOO_LONG',
field: 'description',
message: `Description is ${descLen} chars (max ${window.descriptionMax} for lang=${lang}).`,
value: description,
});
}
if (!SENTENCE_TERMINATOR_RE.test(descTrimmed)) {
violations.push({
code: 'DESCRIPTION_NOT_TERMINATED',
field: 'description',
message: 'Description does not end on ., !, ?, …, 。, or ।.',
value: description,
});
const lastChar = descTrimmed.slice(-1);
Eif (MIDWORD_TRUNCATION_RE.test(lastChar)) {
violations.push({
code: 'DESCRIPTION_TRUNCATED_MIDWORD',
field: 'description',
message: 'Description ends on a letter — likely a blind .slice() cut.',
value: description,
});
}
}
for (const re of BANNED_DESCRIPTION_PHRASES) {
if (re.test(description)) {
violations.push({
code: 'DESCRIPTION_HAS_ADMIN_LEAK',
field: 'description',
message: `Description contains admin-metadata leak (${re.source}).`,
value: description,
});
break;
}
}
if (GENERIC_FILLER_RE.test(description)) {
violations.push({
code: 'DESCRIPTION_GENERIC_FILLER',
field: 'description',
message: 'Description is the aggregator generic-filler fallback.',
value: description,
});
}
}
return {
ok: violations.length === 0,
violations,
window,
};
}
export const __test__ = {
visualLength,
LATIN_WINDOW,
RTL_WINDOW,
CJK_WINDOW,
};
|