All files / scripts/agentic/gate-checks methodology-reflection.ts

98.07% Statements 51/52
93.33% Branches 28/30
100% Functions 13/13
100% Lines 46/46

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                                                        13x 13x 13x   11x   11x       11x       11x       11x 13x 8x 8x                     11x   65x 26x 11x 26x   11x       11x       11x                                                                                                                       13x 132x 26x   13x 6x             5x               11x               11x 11x 11x   11x 11x 9x     45x 9x 36x     11x 56x   11x 9x 9x 120x 24x 24x 24x         2x     7x    
/**
 * @module scripts/agentic/gate-checks/methodology-reflection
 * @description Check 7e — Validate methodology-reflection.md contains the
 *              full Pass-2 audit section contract (ICD 203 audit, DA KJ
 *              coverage matrix with 100% coverage, confidence distribution
 *              with filled Posterior column, Lagrådet/Statskontoret/SKR
 *              tracking, sibling-folder ingestion record, unified re-run
 *              log schema, banned-phrase audit grid, Pass 1 → Pass 2 delta
 *              table, improvement-opportunities → PIR roll-forward).
 *
 * @see .github/prompts/05-analysis-gate.md §Check 7 (methodology-reflection)
 * @author Hack23 AB
 * @license Apache-2.0
 */
 
import { readFile } from 'node:fs/promises';
import { existsSync } from 'node:fs';
import { join } from 'node:path';
 
import {
  extractSection,
  hasHeading,
} from '../gate-shared/markdown-helpers.js';
import type { GateCheckResult } from '../gate-shared/types.js';
 
export async function checkMethodologyReflection(
  analysisDir: string,
): Promise<GateCheckResult[]> {
  const results: GateCheckResult[] = [];
  const filePath = join(analysisDir, 'methodology-reflection.md');
  if (!existsSync(filePath)) return results;
 
  const content = await readFile(filePath, 'utf-8');
 
  const kjCoverageSection = extractSection(
    content,
    /Devil'?s[-\s]Advocate\s+Key\s+Judgment\s+Coverage\s+Matrix/i,
  );
  const confidenceSection = extractSection(
    content,
    /Confidence\s+Distribution\s+by\s+Key\s+Judgment/i,
  );
  const reRunSection = extractSection(content, /Re[-\s]?run\s+Log/i);
 
  // Re-run log unified column schema — must appear as a table header row
  // inside the Re-run Log section, not just anywhere in the file.
  const reRunColumnHeaderPresent = reRunSection.split('\n').some((line) => {
    if (!line.trim().startsWith('|')) return false;
    const lower = line.toLowerCase();
    return (
      lower.includes('run_id') &&
      lower.includes('attempt') &&
      /new[\s_]+dok_ids/.test(lower) &&
      /artifacts[\s_]+extended/.test(lower) &&
      /flags[\s_]+closed/.test(lower) &&
      /vintage[\s_]+refresh/.test(lower)
    );
  });
 
  // KJ-coverage content quality — every KJ row must be ✅ and CLOSED.
  const kjCoverageRows = kjCoverageSection
    .split('\n')
    .filter((line) => /^\|\s*KJ[-\s]?\d/i.test(line.trim()));
  const kjCoverageHasUncovered = kjCoverageRows.some((row) => /❌/.test(row));
  const kjCoverageHasOpenStatus = kjCoverageRows.some((row) =>
    /\bOPEN\b/i.test(row),
  );
  const kjCoverageRowsPresent = kjCoverageRows.length > 0;
 
  // Posterior content quality — every KJ row must have a filled Posterior
  // value (no `[REQUIRED]` placeholder, no empty cell).
  const confidencePosteriorFilledForAllKjs = checkConfidencePosteriorColumn(
    confidenceSection,
  );
 
  const requiredSectionChecks: Array<{ label: string; present: boolean }> = [
    {
      label: 'ICD 203 audit checklist (## heading)',
      present: hasHeading(content, /ICD\s+203/i),
    },
    {
      label: "Devil's-Advocate KJ coverage matrix (## heading)",
      present: hasHeading(
        content,
        /Devil'?s[-\s]Advocate\s+Key\s+Judgment\s+Coverage\s+Matrix/i,
      ),
    },
    {
      label: "Devil's-Advocate KJ coverage matrix: 100% coverage (no ❌ / OPEN rows)",
      present:
        kjCoverageRowsPresent &&
        !kjCoverageHasUncovered &&
        !kjCoverageHasOpenStatus,
    },
    {
      label: 'Confidence distribution (## heading)',
      present: hasHeading(content, /Confidence\s+Distribution\s+by\s+Key\s+Judgment/i),
    },
    {
      label: 'Confidence distribution: filled Posterior per KJ row',
      present: confidencePosteriorFilledForAllKjs,
    },
    {
      label: 'Lagrådet / Statskontoret / SKR tracking (## heading)',
      present: hasHeading(content, /Lagrådet.*Statskontoret.*SKR/i),
    },
    {
      label: 'Sibling-folder ingestion record (## heading)',
      present: hasHeading(content, /Sibling[-\s]?Folder\s+Ingestion/i),
    },
    {
      label: 'Re-run log (## heading)',
      present: hasHeading(content, /Re[-\s]?run\s+Log/i),
    },
    {
      label: 'Re-run log unified schema (header row inside Re-run Log section)',
      present: reRunColumnHeaderPresent,
    },
    {
      label: 'Banned-phrase audit grid (## heading)',
      present: hasHeading(content, /Banned[-\s]?Phrase\s+Audit/i),
    },
    {
      label: 'Pass 1 → Pass 2 delta table (## heading)',
      present: hasHeading(content, /Pass\s*1.*Pass\s*2.*Delta/i),
    },
    {
      label: 'Improvement opportunities → PIR roll-forward (## heading)',
      present: hasHeading(
        content,
        /Improvement\s+Opportunities.*PIR\s+Roll[-\s]?Forward/i,
      ),
    },
  ];
 
  const missingSections = requiredSectionChecks
    .filter(({ present }) => !present)
    .map(({ label }) => label);
 
  if (missingSections.length > 0) {
    results.push({
      checkId: 'family-c-structure',
      passed: false,
      message: `methodology-reflection.md: missing required section(s): ${missingSections.join(', ')}`,
      artifact: 'methodology-reflection.md',
    });
  } else {
    results.push({
      checkId: 'family-c-structure',
      passed: true,
      message: 'methodology-reflection.md: all required sections present',
      artifact: 'methodology-reflection.md',
    });
  }
 
  return results;
}
 
/**
 * Confidence distribution table audit — every KJ row must have a non-empty
 * Posterior cell (no `[REQUIRED]` placeholder, no dash, no blank).
 */
function checkConfidencePosteriorColumn(confidenceSection: string): boolean {
  const confidenceLines = confidenceSection.split('\n');
  const headerLineIndex = confidenceLines.findIndex(
    (line) => line.trim().startsWith('|') && /posterior/i.test(line),
  );
  let posteriorColumnIndex = -1;
  if (headerLineIndex >= 0) {
    const cells = confidenceLines[headerLineIndex]!
      .split('|')
      .slice(1, -1)
      .map((cell) => cell.trim().toLowerCase());
    posteriorColumnIndex = cells.findIndex((cell) =>
      cell.includes('posterior'),
    );
  }
  const confidenceKjRows = confidenceLines.filter((line) =>
    /^\|\s*KJ[-\s]?\d/i.test(line.trim()),
  );
  if (confidenceKjRows.length === 0) return false;
  Iif (posteriorColumnIndex < 0) return false;
  for (const row of confidenceKjRows) {
    const cells = row.split('|').slice(1, -1).map((c) => c.trim());
    const cell = cells[posteriorColumnIndex] ?? '';
    const stripped = cell.replace(/`/g, '').trim();
    if (
      stripped === '' ||
      /^\[\s*REQUIRED\s*\]?$/i.test(stripped) ||
      /^-+$/.test(stripped)
    ) {
      return false;
    }
  }
  return true;
}