All files / scripts pre-article-analysis.ts

0% Statements 0/364
0% Branches 0/261
0% Functions 0/36
0% Lines 0/339

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 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 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
#!/usr/bin/env tsx
/**
 * @module pre-article-analysis
 * @description Pre-article data download and deep analysis pipeline.
 *
 * Orchestrates all analysis steps before article generation:
 * 1. Download all relevant parliamentary documents from riksdag-regering-mcp
 * 2. Political classification — Classify each document by significance, impact, domain
 * 3. Risk assessment — Assess political risks (coalition stability, anomaly detection)
 * 4. SWOT analysis — Generate political SWOT for relevant actors
 * 5. Threat analysis — Identify threats from SWOT contributions
 * 6. Stakeholder perspective analysis — Run all 6 lenses
 * 7. Significance scoring — Score all documents (0–10)
 * 8. Cross-reference mapping — Identify relationships between documents
 * 9. Synthesis — Combined analysis summary integrating all methods
 * 10. Persist — Write structured markdown to analysis/daily/YYYY-MM-DD/
 *
 * Usage:
 *   npx tsx scripts/pre-article-analysis.ts [--date YYYY-MM-DD] [--limit N]
 *   npx tsx scripts/pre-article-analysis.ts --aggregate weekly [--date YYYY-WNN]
 *
 * @author Hack23 AB
 * @license Apache-2.0
 */
 
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
 
import { MCPClient } from './mcp-client/client.js';
import { calculateCoalitionRiskIndex, detectAnomalousPatterns } from './data-transformers/risk-analysis.js';
import type { RawDocument, CIAContext } from './data-transformers/types.js';
import { loadCIAContext } from './news-types/weekly-review/index.js';
import { normalizedCIAContext } from './news-types/weekly-review/data-loader.js';
import { detectPolicyDomainsWithConfidence } from './data-transformers/policy-analysis.js';
 
import {
  downloadAllDocuments,
  flattenDocuments,
  subtractBusinessDays,
  MAX_LOOKBACK_BUSINESS_DAYS,
} from './pre-article-analysis/data-downloader.js';
import type { DocumentTypeKey } from './pre-article-analysis/data-downloader.js';
 
import { persistDownloadedData } from './pre-article-analysis/data-persistence.js';
 
import type {
  SerializationContext,
  SignificanceEntry,
  RiskAssessmentResult,
  SwotSummary,
  CrossReferenceSummary,
  SynthesisSummary,
} from './pre-article-analysis/markdown-serializer.js';
 
import {
  serializeDataManifest,
  serializeClassificationResults,
  serializeRiskAssessment,
  serializeSwotAnalysis,
  serializeThreatAnalysis,
  serializeStakeholderPerspectives,
  serializeSignificanceScoring,
  serializeCrossReferenceMap,
  serializeSynthesisSummary,
  serializeDocumentAnalysis,
  sanitizeDokId,
  POLICY_DOMAIN_INSIGHT_PREFIX,
} from './pre-article-analysis/markdown-serializer.js';
 
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
 
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const REPO_ROOT = path.resolve(__dirname, '..');
const ANALYSIS_DIR = path.join(REPO_ROOT, 'analysis');
 
/**
 * Swedish phrase indicating a motion responds to a government proposition.
 * Documents from the Riksdag API always use Swedish titles regardless of
 * the target analysis language, so Swedish-only detection is correct here.
 */
const MOTION_PROPOSITION_RESPONSE_PREFIX = 'med anledning av prop';
 
function formatTimestampForMarkdown(date: Date = new Date()): string {
  return date.toISOString().slice(0, 16).replace('T', ' ') + ' UTC';
}
 
// ---------------------------------------------------------------------------
// CLI helpers
// ---------------------------------------------------------------------------
 
export function parseArgs(argv: string[]): {
  date: string;
  aggregate: boolean;
  limit: number;
  weekLabel: string | null;
  rm: string | null;
  docType: DocumentTypeKey | null;
  documentIds: string[];
} {
  const args = argv.slice(2);
  const get = (flag: string): string | null => {
    const idx = args.indexOf(flag);
    if (idx === -1) {
      return null;
    }
    const next = args[idx + 1];
    if (!next || next.startsWith('--')) {
      throw new Error(`Missing value for ${flag}.`);
    }
    return next;
  };
 
  const dateArg = get('--date');
  const aggregateArg = get('--aggregate');
  const aggregate = (() => {
    if (aggregateArg === null) {
      return false;
    }
    if (aggregateArg === 'weekly') {
      return true;
    }
    throw new Error(`Invalid --aggregate value: ${aggregateArg}. Supported value: 'weekly'.`);
  })();
 
  const now = new Date();
  const todayIso = now.toISOString().slice(0, 10);
 
  // When aggregate weekly, --date supplies the week label (YYYY-WNN), not a
  // calendar date.  `date` is always a YYYY-MM-DD value (defaults to today).
  const weekLabel = aggregate
    ? (dateArg || `${now.getUTCFullYear()}-W${isoWeekNumber(now).toString().padStart(2, '0')}`)
    : null;
  if (aggregate && weekLabel && !parseIsoWeekLabel(weekLabel)) {
    throw new Error(`Invalid weekly --date value: ${weekLabel}. Expected YYYY-WNN.`);
  }
 
  if (dateArg && dateArg !== 'today' && !aggregate && !parseAndValidateIsoDate(dateArg)) {
    throw new Error(`Invalid --date value: ${dateArg}. Expected YYYY-MM-DD or 'today'.`);
  }
 
  // In aggregate mode, date is always today; the week-specific field is weekLabel.
  const isoDate = aggregate
    ? todayIso
    : (dateArg === 'today' || !dateArg ? todayIso : dateArg);
 
  const limitArg = get('--limit');
  const DEFAULT_LIMIT = 20;
  const parsedLimit = limitArg ? Number(limitArg) : DEFAULT_LIMIT;
  if (!Number.isInteger(parsedLimit) || parsedLimit <= 0) {
    throw new Error(`Invalid --limit value: ${limitArg}. Expected a positive integer.`);
  }
  const limit = parsedLimit;
  const rm = get('--rm');
 
  const docTypeArg = get('--doc-type');
  const VALID_DOC_TYPES: readonly DocumentTypeKey[] = ['propositions', 'motions', 'committeeReports', 'votes', 'speeches', 'questions', 'interpellations'];
  const isDocumentTypeKey = (value: string): value is DocumentTypeKey =>
    VALID_DOC_TYPES.includes(value as DocumentTypeKey);
  let docType: DocumentTypeKey | null = null;
  if (docTypeArg !== null) {
    if (!isDocumentTypeKey(docTypeArg)) {
      throw new Error(
        `Invalid --doc-type value: ${docTypeArg}. Supported values: ${VALID_DOC_TYPES.join(', ')}.`,
      );
    }
    docType = docTypeArg;
  }
 
  // --document-ids: Comma-separated Riksdag dok_id values for deep-inspection.
  // When provided, these specific documents are fetched by ID and included in
  // analysis regardless of their date, ensuring deep-inspection batch analysis
  // files contain real content instead of "0 documents analyzed".
  const documentIdsArg = get('--document-ids');
  const DOK_ID_PATTERN = /^[A-Za-z0-9_-]+$/;
  const documentIds = documentIdsArg
    ? documentIdsArg.split(',').map(id => id.trim()).filter(id => {
        if (!id) return false;
        if (!DOK_ID_PATTERN.test(id)) {
          console.warn(`⚠️ Skipping invalid document ID: ${id} (must be alphanumeric/hyphens/underscores only)`);
          return false;
        }
        return true;
      })
    : [];
 
  return { date: isoDate, aggregate, limit, weekLabel, rm, docType, documentIds };
}
 
function isoWeekNumber(date: Date): number {
  const d = new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()));
  const dayNum = d.getUTCDay() || 7;
  d.setUTCDate(d.getUTCDate() + 4 - dayNum);
  const yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
  return Math.ceil((((d.getTime() - yearStart.getTime()) / 86400000) + 1) / 7);
}
 
function parseAndValidateIsoDate(dateStr: string): Date | null {
  const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(dateStr);
  if (!m) return null;
  const year = Number(m[1]);
  const month = Number(m[2]);
  const day = Number(m[3]);
  const d = new Date(Date.UTC(year, month - 1, day));
  if (Number.isNaN(d.getTime())) return null;
  if (d.getUTCFullYear() !== year || d.getUTCMonth() + 1 !== month || d.getUTCDate() !== day) return null;
  return d;
}
 
function riksMoteFromDate(dateStr: string): string {
  const parsed = parseAndValidateIsoDate(dateStr) ?? new Date();
  const year = parsed.getUTCFullYear();
  const month = parsed.getUTCMonth() + 1;
  if (month >= 10) return `${year}/${String(year + 1).slice(-2)}`;
  return `${year - 1}/${String(year).slice(-2)}`;
}
 
function parseIsoWeekLabel(label: string): { year: number; week: number } | null {
  const match = /^(\d{4})-W(\d{2})$/.exec(label);
  if (!match) return null;
  const year = Number(match[1]);
  const week = Number(match[2]);
  if (week < 1 || week > 53) return null;
  return { year, week };
}
 
function isDateInIsoWeek(dateStr: string, weekLabel: string): boolean {
  const parsedDate = parseAndValidateIsoDate(dateStr);
  const parsedWeek = parseIsoWeekLabel(weekLabel);
  if (!parsedDate || !parsedWeek) return false;
 
  const isoThursday = new Date(parsedDate);
  const dayNum = isoThursday.getUTCDay() || 7;
  isoThursday.setUTCDate(isoThursday.getUTCDate() + 4 - dayNum);
  const isoYear = isoThursday.getUTCFullYear();
  const isoWeek = isoWeekNumber(parsedDate);
 
  return isoYear === parsedWeek.year && isoWeek === parsedWeek.week;
}
 
// ---------------------------------------------------------------------------
// File utilities
// ---------------------------------------------------------------------------
 
function ensureDir(dir: string): void {
  fs.mkdirSync(dir, { recursive: true });
}
 
function writeAnalysis(dir: string, filename: string, content: string): void {
  const filePath = path.join(dir, filename);
  fs.writeFileSync(filePath, content, 'utf8');
  console.log(`  ✅ Written: ${path.relative(REPO_ROOT, filePath)}`);
}
 
// ---------------------------------------------------------------------------
// SWOT extraction from analysis results
// ---------------------------------------------------------------------------
 
// ---------------------------------------------------------------------------
// Stub analysis types — real analysis is now done by AI agent in workflows
// per ai-driven-analysis-guide.md Rule 2
// ---------------------------------------------------------------------------
 
/** Minimal batch result stub for compatibility */
interface StubBatchResult {
  results: StubDocumentResult[];
  crossDocumentLinks: Array<{ sourceId: string; targetId: string; type: string; description: string; confidence: number; reason: string }>;
}
interface StubDocumentResult {
  document: RawDocument;
  overallSignificance: number;
  perspectives: Array<{
    lens: string;
    summary: string;
    impact: string;
    sentiment: string;
    confidence: number;
    keyActors: string[];
    relatedPolicies: string[];
    swotContribution: Array<{ forStakeholder: string; quadrant: string; text: string }>;
    dashboardMetrics: Array<{ label: string; value: number | string; unit?: string; metricName: string }>;
    mindmapNodes: Array<{ label: string; children?: Array<{ label: string }> }>;
  }>;
  crossDocumentLinks: Array<{ sourceId: string; targetId: string; type: string; description: string; confidence: number; reason: string }>;
  keyInsights: string[];
  confidenceScore: number;
}
 
/**
 * Script-based document analysis using available metadata.
 *
 * This function provides automated data extraction and basic classification
 * from document metadata (doktyp, organ, title keywords). It does NOT perform
 * deep political intelligence analysis — that is the responsibility of AI agents
 * in agentic workflows following analysis/methodologies/ai-driven-analysis-guide.md.
 *
 * Significance scoring heuristics:
 * - propositions (prop): base 5, +2 if defence/migration/budget topic
 * - committee reports (bet): base 4, +2 if justice/defence committee
 * - interpellations (ip): base 3, +1 if targets senior minister
 * - motions (mot): base 2, +1 if responds to government proposition
 * - other: base 1
 */
function analyzeDocuments(docs: RawDocument[], _cia: unknown, _lang: string): StubBatchResult {
  return {
    results: docs.map(d => {
      const doktyp = (d.doktyp || '').toLowerCase();
      const title = (d.titel || d.title || '').toLowerCase();
      const organ = d.organ || d.committee || '';
 
      // Domain classification using committee→domain mapping
      const domainResult = detectPolicyDomainsWithConfidence(d, _lang);
      const domainConfidence = domainResult.confidence === 'HIGH' ? 70 : 30;
 
      // Significance scoring heuristics based on document type and content
      let significance = 1;
      if (doktyp === 'prop') {
        significance = 5;
        if (title.includes('försvar') || title.includes('nato') || title.includes('budget') ||
            title.includes('migration') || title.includes('utvisning') || title.includes('cyber')) {
          significance += 2;
        }
      } else if (doktyp === 'bet') {
        significance = 4;
        if (['JuU', 'FöU', 'UU', 'KU'].some(c => organ.includes(c))) {
          significance += 2;
        }
      } else if (doktyp === 'ip') {
        significance = 3;
        if (title.includes('statsråd') || title.includes('minister')) {
          significance += 1;
        }
      } else if (doktyp === 'mot') {
        significance = 2;
        if (title.includes(MOTION_PROPOSITION_RESPONSE_PREFIX)) {
          significance += 1; // Response to proposition = slightly more significant
        }
      }
 
      // Cap at 10
      significance = Math.min(significance, 10);
 
      return {
        document: d,
        overallSignificance: significance,
        perspectives: [],
        crossDocumentLinks: [],
        keyInsights: domainResult.domains.length > 0
          ? [`${POLICY_DOMAIN_INSIGHT_PREFIX} ${domainResult.domains.join(', ')} (${domainResult.confidence} confidence)`]
          : [],
        confidenceScore: domainConfidence,
      };
    }),
    crossDocumentLinks: [],
  };
}
 
function extractSwotSummaries(results: StubBatchResult['results']): SwotSummary[] {
  const map = new Map<string, SwotSummary>();
 
  for (const result of results) {
    for (const p of result.perspectives) {
      for (const swotC of p.swotContribution) {
        const key = swotC.forStakeholder;
        if (!map.has(key)) {
          map.set(key, { forStakeholder: key, strengths: [], weaknesses: [], opportunities: [], threats: [] });
        }
        const entry = map.get(key)!;
        switch (swotC.quadrant) {
          case 'strength': entry.strengths.push(swotC.text); break;
          case 'weakness': entry.weaknesses.push(swotC.text); break;
          case 'opportunity': entry.opportunities.push(swotC.text); break;
          case 'threat': entry.threats.push(swotC.text); break;
        }
      }
    }
  }
 
  // Deduplicate and cap
  return [...map.values()].map(s => ({
    ...s,
    strengths: [...new Set(s.strengths)].slice(0, 5),
    weaknesses: [...new Set(s.weaknesses)].slice(0, 5),
    opportunities: [...new Set(s.opportunities)].slice(0, 5),
    threats: [...new Set(s.threats)].slice(0, 5),
  }));
}
 
// ---------------------------------------------------------------------------
// Significance entries
// ---------------------------------------------------------------------------
 
function buildSignificanceEntries(results: StubBatchResult['results']): SignificanceEntry[] {
  return results.map(r => ({
    dok_id: r.document.dok_id || 'N/A',
    title: r.document.titel || r.document.title || r.document.dok_id || 'Unknown',
    score: r.overallSignificance,
    doctype: r.document.doktyp || 'unknown',
  }));
}
 
// ---------------------------------------------------------------------------
// Risk assessment
// ---------------------------------------------------------------------------
 
function buildRiskAssessment(docs: RawDocument[], ciaContext: CIAContext): RiskAssessmentResult {
  const normalizedContext = normalizedCIAContext(ciaContext);
  // Attempt to derive basic coalition signals from document data
  const riskIndex = calculateCoalitionRiskIndex(normalizedContext);
  const anomalies = detectAnomalousPatterns(normalizedContext);
 
  // Derive implications from document significance
  const highSignificance = docs.filter(d => {
    const titleText = (d.titel || d.title || '').toLowerCase();
    return (
      d.doktyp === 'prop' ||
      titleText.includes('budget') ||
      titleText.includes('försvar') ||
      titleText.includes('nato')
    );
  });
 
  const implications: string[] = [
    `${docs.length} documents analyzed for risk indicators`,
    `${highSignificance.length} high-significance documents identified`,
    riskIndex.level !== 'LOW'
      ? `Coalition stability at ${riskIndex.level} risk — monitor upcoming votes`
      : 'Coalition stability appears stable based on available data',
  ];
 
  return {
    coalitionRiskScore: riskIndex.score,
    riskLevel: riskIndex.level,
    riskSummary: riskIndex.summary,
    anomalyFlags: anomalies.map(a => ({
      type: a.type,
      severity: a.severity,
      description: a.description,
    })),
    implications,
  };
}
 
// ---------------------------------------------------------------------------
// Synthesis
// ---------------------------------------------------------------------------
 
function buildSynthesis(
  docs: RawDocument[],
  significanceEntries: SignificanceEntry[],
  riskResult: RiskAssessmentResult,
  dataFreshness?: string | null,
): SynthesisSummary {
  const totalDocs = docs.length;
  const topDocs = [...significanceEntries].sort((a, b) => b.score - a.score).slice(0, 10);
  const avgScore = significanceEntries.length > 0
    ? significanceEntries.reduce((s, e) => s + e.score, 0) / significanceEntries.length
    : 0;
 
  const overallConfidence: 'HIGH' | 'MEDIUM' | 'LOW' =
    totalDocs >= 20 ? 'HIGH' : totalDocs >= 10 ? 'MEDIUM' : 'LOW';
 
  const keyFindings: string[] = [
    `Analyzed ${totalDocs} parliamentary documents with avg significance ${avgScore.toFixed(1)}/10`,
    `Coalition risk level: ${riskResult.riskLevel} (score: ${riskResult.coalitionRiskScore}/100)`,
    `Top document: "${topDocs[0]?.title || 'N/A'}" (significance: ${topDocs[0]?.score ?? 0}/10)`,
  ];
 
  if (riskResult.anomalyFlags.length > 0) {
    keyFindings.push(`${riskResult.anomalyFlags.length} anomaly flag(s) detected requiring editorial attention`);
  }
  if (dataFreshness) {
    keyFindings.push(`Data sourced from ${dataFreshness} via lookback fallback — check freshness indicators`);
  }
 
  const executiveSummary = [
    `Pre-article analysis completed for ${totalDocs} documents.`,
    `Overall political risk: ${riskResult.riskLevel}.`,
    `Average document significance: ${avgScore.toFixed(1)}/10.`,
    overallConfidence === 'HIGH'
      ? 'High data coverage — analysis results are reliable for article generation.'
      : 'Partial data coverage — treat analysis as directional guidance.',
    dataFreshness ? `Note: Documents sourced from ${dataFreshness} via lookback (article date differs).` : '',
  ].filter(Boolean).join(' ');
 
  return {
    totalDocs,
    executiveSummary,
    keyFindings,
    topDocuments: topDocs,
    overallConfidence,
    aggregateRiskLevel: riskResult.riskLevel,
    dataFreshness: dataFreshness ?? null,
  };
}
 
// ---------------------------------------------------------------------------
// Weekly aggregation
// ---------------------------------------------------------------------------
 
function runWeeklyAggregation(weekLabel: string): void {
  const weekDir = path.join(ANALYSIS_DIR, 'weekly', weekLabel);
  ensureDir(weekDir);
 
  const dailyRoot = path.join(ANALYSIS_DIR, 'daily');
  let allSyntheses = '';
  let includedDays = 0;
  let aggregatedDocumentsAnalyzed = 0;
 
  const parsedWeek = parseIsoWeekLabel(weekLabel);
  if (!parsedWeek) {
    throw new Error(`Invalid ISO week label: ${weekLabel}. Expected format YYYY-WNN`);
  }
 
  if (fs.existsSync(dailyRoot)) {
    const dailyDirs = fs.readdirSync(dailyRoot).sort();
    const KNOWN_DOC_TYPES = new Set<string>([
      'propositions', 'motions', 'committeeReports', 'votes',
      'speeches', 'questions', 'interpellations',
    ]);
    for (const dir of dailyDirs) {
      if (!isDateInIsoWeek(dir, weekLabel)) continue;
      // Look for synthesis in unscoped path first, then in known doc-type subdirectories
      const unscopedSynth = path.join(dailyRoot, dir, 'synthesis-summary.md');
      const dayDir = path.join(dailyRoot, dir);
      const scopedSynthPaths: string[] = [];
      if (fs.existsSync(dayDir) && fs.statSync(dayDir).isDirectory()) {
        // Sort subdirectories for deterministic output across filesystems
        for (const sub of fs.readdirSync(dayDir).sort()) {
          if (!KNOWN_DOC_TYPES.has(sub)) continue;
          const subSynth = path.join(dayDir, sub, 'synthesis-summary.md');
          if (fs.existsSync(subSynth)) {
            scopedSynthPaths.push(subSynth);
          }
        }
      }
 
      // Prefer the unscoped synthesis when it exists (canonical copy created by
      // the copy-to-unscoped step).  Otherwise include all scoped syntheses so
      // no doc-type run is silently omitted.
      const hasUnscoped = fs.existsSync(unscopedSynth);
      const pathsToProcess = hasUnscoped ? [unscopedSynth] : scopedSynthPaths;
      let dayHasSynthesis = false;
 
      for (const synthPath of pathsToProcess) {
        if (!fs.existsSync(synthPath)) continue;
 
        const dailySynthesis = fs.readFileSync(synthPath, 'utf8');
        const subDir = path.basename(path.dirname(synthPath));
        const label = hasUnscoped ? dir : `${dir} (${subDir})`;
        allSyntheses += `\n\n---\n\n## Day: ${label}\n\n${dailySynthesis}`;
 
        if (!dayHasSynthesis) {
          includedDays += 1;
          dayHasSynthesis = true;
        }
 
        const docsMatch = /(?:^|\n)\*\*Documents Analyzed\*\*:\s*(\d+)/.exec(dailySynthesis);
        if (docsMatch?.[1]) {
          aggregatedDocumentsAnalyzed += Number(docsMatch[1]);
        } else {
          console.warn(`[pre-analysis] Could not parse Documents Analyzed from ${synthPath}`);
        }
      }
    }
  }
 
  const weeklyContent = buildWeeklySynthesisMarkdown({
    weekLabel,
    generatedAt: formatTimestampForMarkdown(),
    documentsAnalyzed: aggregatedDocumentsAnalyzed,
    daysIncluded: includedDays,
    allSyntheses,
  });
 
  writeAnalysis(weekDir, 'weekly-synthesis.md', weeklyContent);
  console.log(`\n✅ Weekly aggregation written to analysis/weekly/${weekLabel}/`);
}
 
export function buildWeeklySynthesisMarkdown(opts: {
  weekLabel: string;
  generatedAt: string;
  documentsAnalyzed: number;
  daysIncluded: number;
  allSyntheses: string;
}): string {
  const confidence = opts.documentsAnalyzed >= 20
    ? 'HIGH'
    : (opts.documentsAnalyzed >= 8 ? 'MEDIUM' : 'LOW');
 
  return [
    `# Weekly Analysis Aggregation — ${opts.weekLabel}`,
    '',
    `**Generated**: ${opts.generatedAt}`,
    '**Data Sources**: Aggregated from daily synthesis summaries',
    `**Documents Analyzed**: ${opts.documentsAnalyzed}`,
    `**Confidence**: ${confidence}`,
    `**Period**: ${opts.weekLabel}`,
    `**Days Included**: ${opts.daysIncluded}`,
    '',
    '## Summary',
    '',
    'Aggregation of daily analysis synthesis results for the week.',
    '',
    opts.allSyntheses || '_No daily analysis results found for this week._',
  ].join('\n');
}
 
// ---------------------------------------------------------------------------
// Main pipeline
// ---------------------------------------------------------------------------
 
async function runPreArticleAnalysis(opts: {
  date: string;
  limit: number;
  aggregate: boolean;
  weekLabel: string | null;
  rm: string | null;
  docType: DocumentTypeKey | null;
  documentIds: string[];
}): Promise<void> {
  const { date, limit, aggregate, weekLabel, rm, docType, documentIds } = opts;
 
  if (aggregate && weekLabel) {
    console.log(`\n📅 Running weekly aggregation for: ${weekLabel}`);
    runWeeklyAggregation(weekLabel);
    return;
  }
 
  console.log(`\n🚀 Pre-Article Analysis Pipeline — ${date}`);
  console.log('='.repeat(50));
 
  // When --doc-type is specified, scope output to a subdirectory to avoid
  // conflicts between parallel workflow runs (e.g. propositions vs committee-reports).
  const outputDir = docType
    ? path.join(ANALYSIS_DIR, 'daily', date, docType)
    : path.join(ANALYSIS_DIR, 'daily', date);
  ensureDir(outputDir);
 
  const generatedAt = formatTimestampForMarkdown();
 
  // ── Step 1: Download data ─────────────────────────────────────────────────
  console.log('\n📥 Step 1: Downloading documents from riksdag-regering-mcp...');
  if (docType) {
    console.log(`   📋 Scoped to document type: ${docType}`);
  }
  const client = new MCPClient();
  const resolvedRm = rm ?? riksMoteFromDate(date);
 
  const downloadOpts: { limit: number; rm: string; docTypes?: DocumentTypeKey[] } = { limit, rm: resolvedRm };
  if (docType) {
    downloadOpts.docTypes = [docType];
  }
 
  const { data, manifest } = await downloadAllDocuments(client, downloadOpts);
  const flattenedDocs = flattenDocuments(data);
 
  // Build a set of explicitly requested document IDs for deep-inspection bypass.
  const requestedIdSet = new Set(documentIds.map(id => id.toUpperCase()));
 
  const allDocs = flattenedDocs.filter((doc: RawDocument) => {
    // Documents explicitly requested by ID are ALWAYS included regardless of date.
    // This is critical for deep-inspection which targets specific documents that
    // may have been published on previous days.
    const docId = doc.dok_id ?? '';
    if (requestedIdSet.size > 0 && requestedIdSet.has(docId.toUpperCase())) {
      return true;
    }
    // Only keep documents whose datum matches the requested analysis date (YYYY-MM-DD).
    if (doc.datum && typeof doc.datum === 'string') {
      return doc.datum.slice(0, 10) === date;
    }
    return false;
  });
 
  // ── Lookback fallback: widen the date filter when no documents match ────
  // When no documents match the exact date (common on weekends, holidays, or
  // low-activity days), expand the filter to include previous business days
  // (up to MAX_LOOKBACK_BUSINESS_DAYS).  This prevents empty "Documents
  // Analyzed: 0" synthesis files from propagating to article generators.
  //
  // The requestedIdSet guard ensures lookback is ONLY used for generic
  // date-based runs. Deep-inspection runs (--document-ids) fetch specific
  // documents by ID in the block below, so lookback is unnecessary and would
  // introduce unrelated documents.
  let dataFreshness: string | null = null;
  if (allDocs.length === 0 && requestedIdSet.size === 0) {
    for (let lookback = 1; lookback <= MAX_LOOKBACK_BUSINESS_DAYS; lookback++) {
      const lookbackDate = subtractBusinessDays(date, lookback);
      const lookbackDocs = flattenedDocs.filter((doc: RawDocument) => {
        if (doc.datum && typeof doc.datum === 'string') {
          return doc.datum.slice(0, 10) === lookbackDate;
        }
        return false;
      });
      if (lookbackDocs.length > 0) {
        allDocs.push(...lookbackDocs);
        dataFreshness = lookbackDate;
        console.log(`   🔄 Lookback fallback: 0 documents for ${date}, using ${lookbackDocs.length} documents from ${lookbackDate} (${lookback} business day(s) back)`);
        break;
      }
    }
    if (allDocs.length === 0) {
      console.warn(`   ⚠️  Lookback exhausted (${MAX_LOOKBACK_BUSINESS_DAYS} business days) — no recent documents found in downloaded batch.`);
    }
  }
 
  // If document IDs were requested but not found in the bulk download, attempt
  // to fetch them individually via fetchDocumentDetails so deep-inspection always has data.
  if (requestedIdSet.size > 0) {
    const foundIds = new Set(allDocs.map((d: RawDocument) => (d.dok_id ?? '').toUpperCase()));
    const missingIds = documentIds.filter(id => !foundIds.has(id.toUpperCase()));
    if (missingIds.length > 0) {
      console.log(`   🔍 Fetching ${missingIds.length} targeted document(s) by ID: ${missingIds.join(', ')}`);
      for (const dokId of missingIds) {
        try {
          const result = await client.fetchDocumentDetails(dokId, false);
          if (result && typeof result === 'object') {
            const doc = result as unknown as RawDocument;
            if (!doc.dok_id) {
              (doc as Record<string, unknown>).dok_id = dokId;
            }
            allDocs.push(doc);
            console.log(`   ✅ Fetched document ${dokId}: ${(doc as Record<string, unknown>).titel ?? (doc as Record<string, unknown>).title ?? '(no title)'}`);
          }
        } catch (err) {
          console.warn(`   ⚠️ Failed to fetch document ${dokId}: ${err instanceof Error ? err.message : String(err)}`);
        }
      }
    }
  }
  // Compute excluded count from the original date-filtered set (before any
  // by-ID fetches were appended).  `Math.max(0, …)` guards against cases
  // where individually-fetched documents grow `allDocs` beyond `flattenedDocs`.
  const excludedDocsCount = Math.max(0, flattenedDocs.length - allDocs.length);
 
  console.log(`   Downloaded ${flattenedDocs.length} unique documents from ${manifest.dataSources.length} MCP tools`);
  console.log(
    `   Selected ${allDocs.length} documents for analysis for ${date} (${excludedDocsCount} with missing or non-matching dates excluded)`,
  );
  if (dataFreshness) {
    console.log(`   📅 Data freshness: documents sourced from ${dataFreshness} (lookback active)`);
  }
  console.log(`   Duration: ${manifest.durationMs}ms`);
  console.log(`   Riksmöte: ${resolvedRm}`);
 
  // ── Step 1a: Persist raw data to analysis/data/ for verification & reuse ──
  console.log('\n🗄️  Step 1a: Persisting raw MCP data to analysis/data/...');
  const persistResult = persistDownloadedData(data, resolvedRm);
  console.log(`   🗄️  Persisted data for ${persistResult.written} documents to ${path.relative(REPO_ROOT, persistResult.dataRoot)}/ (${persistResult.skipped} skipped)`);
 
  const ctx: SerializationContext = {
    date,
    generatedAt,
    dataSources: manifest.dataSources,
  };
 
  writeAnalysis(outputDir, 'data-download-manifest.md', serializeDataManifest(ctx, manifest.docCounts, allDocs.length));
 
  // ── Step 1b: Store each downloaded document as JSON ─────────────────────
  console.log('\n💾 Step 1b: Storing downloaded documents...');
  const documentsDir = path.join(outputDir, 'documents');
  ensureDir(documentsDir);
  let storedCount = 0;
  for (let i = 0; i < allDocs.length; i++) {
    const doc = allDocs[i];
    const dokId = doc.dok_id || doc.titel || doc.title || `unknown-doc-${i + 1}`;
    const baseName = sanitizeDokId(dokId) || `unknown-doc-${i + 1}`;
    let fileName = baseName;
    let attempt = 0;
    // Ensure no overwrite if two docs resolve to the same sanitised name.
    while (fs.existsSync(path.join(documentsDir, `${fileName}.json`))) {
      attempt++;
      fileName = `${baseName}-${attempt}`;
    }
    const docJson = JSON.stringify(doc, null, 2);
    fs.writeFileSync(path.join(documentsDir, `${fileName}.json`), docJson, 'utf8');
    storedCount++;
  }
  console.log(`   💾 Stored ${storedCount} documents as JSON in ${path.relative(REPO_ROOT, documentsDir)}/`);
 
  if (allDocs.length === 0) {
    console.warn('\n⚠️  No documents downloaded. Analysis will be minimal.');
  }
 
  // ── Step 2: Classification ────────────────────────────────────────────────
  console.log('\n🏷️  Step 2: Classifying documents...');
  const ciaContext = loadCIAContext();
  const batchResult = analyzeDocuments(allDocs, ciaContext, 'en');
  // Cast stub results to the serializer's expected type — structurally compatible
  // but TypeScript can't verify RawDocument against the index-signature document shape.
  const serializableResults = batchResult.results as unknown as Parameters<typeof serializeClassificationResults>[1];
  writeAnalysis(outputDir, 'classification-results.md', serializeClassificationResults(ctx, serializableResults));
 
  // ── Step 3: Risk assessment ───────────────────────────────────────────────
  console.log('\n⚠️  Step 3: Assessing political risks...');
  const riskResult = buildRiskAssessment(allDocs, ciaContext);
  writeAnalysis(outputDir, 'risk-assessment.md', serializeRiskAssessment(ctx, allDocs.length, riskResult));
 
  // ── Step 4: SWOT analysis ─────────────────────────────────────────────────
  console.log('\n📊 Step 4: Generating SWOT analysis...');
  const swots = extractSwotSummaries(batchResult.results);
  writeAnalysis(outputDir, 'swot-analysis.md', serializeSwotAnalysis(ctx, allDocs.length, swots));
 
  // ── Step 5: Threat analysis ───────────────────────────────────────────────
  console.log('\n🔴 Step 5: Analyzing threats...');
  writeAnalysis(outputDir, 'threat-analysis.md', serializeThreatAnalysis(ctx, serializableResults));
 
  // ── Step 6: Stakeholder perspectives ─────────────────────────────────────
  console.log('\n👥 Step 6: Running stakeholder perspective analysis...');
  writeAnalysis(outputDir, 'stakeholder-perspectives.md', serializeStakeholderPerspectives(ctx, serializableResults));
 
  // ── Step 7: Significance scoring ─────────────────────────────────────────
  console.log('\n📈 Step 7: Scoring document significance...');
  const significanceEntries = buildSignificanceEntries(batchResult.results);
  writeAnalysis(outputDir, 'significance-scoring.md', serializeSignificanceScoring(ctx, significanceEntries));
 
  // ── Step 8: Cross-reference mapping ──────────────────────────────────────
  console.log('\n🔗 Step 8: Mapping cross-document references...');
  const crossRefSummary: CrossReferenceSummary = {
    docCount: allDocs.length,
    totalLinks: batchResult.crossDocumentLinks.length,
    links: batchResult.crossDocumentLinks,
  };
  writeAnalysis(outputDir, 'cross-reference-map.md', serializeCrossReferenceMap(ctx, crossRefSummary));
 
  // ── Step 9: Synthesis ─────────────────────────────────────────────────────
  console.log('\n🧩 Step 9: Synthesizing all analysis...');
  const synthesis = buildSynthesis(allDocs, significanceEntries, riskResult, dataFreshness);
  writeAnalysis(outputDir, 'synthesis-summary.md', serializeSynthesisSummary(ctx, synthesis));
 
  // ── Step 10: Per-document analysis files ─────────────────────────────────
  console.log('\n📝 Step 10: Generating per-document analysis files...');
  let perDocCount = 0;
  for (let i = 0; i < batchResult.results.length; i++) {
    const result = batchResult.results[i];
    const dokId = result.document.dok_id || result.document.titel || result.document.title || `unknown-analysis-${i + 1}`;
    const baseName = sanitizeDokId(dokId) || `unknown-analysis-${i + 1}`;
    let fileName = `${baseName}-analysis.md`;
    let attempt = 0;
    // Ensure no overwrite if two docs resolve to the same sanitised name.
    while (fs.existsSync(path.join(documentsDir, fileName))) {
      attempt++;
      fileName = `${baseName}-${attempt}-analysis.md`;
    }
    writeAnalysis(documentsDir, fileName, serializeDocumentAnalysis(ctx, result as unknown as Parameters<typeof serializeDocumentAnalysis>[1]));
    perDocCount++;
  }
  console.log(`   📝 Generated ${perDocCount} per-document analysis files`);
 
  // ── Summary ───────────────────────────────────────────────────────────────
  // When --doc-type is used, batch artifacts stay ONLY in the scoped subdirectory
  // (e.g., analysis/daily/<date>/propositions/).  analysis-reader.ts scans
  // subdirectories automatically and prefers them over root-level files, so
  // downstream consumers will find them.
  //
  // Root-level copies are deliberately NOT created — this prevents low-quality
  // script-generated heuristic analysis from shadowing high-quality AI-generated
  // analysis produced by the agentic workflows.  AI workflows write to the same
  // article-type subfolders and follow analysis/methodologies/ai-driven-analysis-guide.md
  // for deep political intelligence with Mermaid diagrams and evidence tables.
 
  const totalFiles = 9 + perDocCount + storedCount;
  console.log(`\n✅ Analysis complete! Results in: ${path.relative(REPO_ROOT, outputDir)}/`);
  console.log(`   📄 ${totalFiles} total files written (9 batch + ${perDocCount} analyses + ${storedCount} documents)`);
  console.log(`   📊 ${allDocs.length} documents analyzed`);
  console.log(`   🎯 Overall confidence: ${synthesis.overallConfidence}`);
  console.log(`   ⚠️  Risk level: ${synthesis.aggregateRiskLevel}`);
  if (docType) {
    console.log(`   📋 Scoped to: ${docType}`);
  }
}
 
// ---------------------------------------------------------------------------
// Entry point
// ---------------------------------------------------------------------------
 
if (path.resolve(fileURLToPath(import.meta.url)) === path.resolve(process.argv[1] ?? '')) {
  const args = parseArgs(process.argv);
 
  runPreArticleAnalysis(args).catch((err: unknown) => {
    console.error('[pre-article-analysis] Fatal error:', err instanceof Error ? err.message : String(err));
    process.exit(1);
  });
}