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 | 1x 1x 1x 1x 1x 1x 1x 27x 27x 1x 26x 30x 30x 30x 18x 18x 18x 18x 18x 1x 17x 17x 17x 4x 17x 12x 12x 5x 5x 17x 106x 19x 17x 4x 17x 3x 17x 12x 12x 10x 10x 10x 10x 1x 10x 2x 17x 17x 1x 1x | /**
* @module validate-methodology-reflection
* @description Validator for `methodology-reflection.md` files in
* `analysis/daily/YYYY-MM-DD/<subfolder>/`.
*
* Enforces the 14-artefact §methodology-reflection contract documented in
* `.github/aw/SHARED_PROMPT_PATTERNS.md` Row 14 of the Tier-C table and the
* "Recent Daily Knowledge Base Synthesis" protocol:
*
* - **Required sections** (markdown H2 headings):
* - Purpose
* - Methodology Application Matrix
* - Upstream Watchpoint Reconciliation (aggregation / realtime / deep-inspection)
* - Uncertainty Hot-Spots
* - Known Limitations
* - Pass-1 → Pass-2 Improvement Evidence
* - Recommendations for Doctrine Codification
* - References
*
* - **Minimum byte threshold** of the baseline 4 000 B scaled by the
* period-scope multiplier (0.8× realtime, 0.9× evening-analysis,
* 1.0× week-ahead / weekly-review / deep-inspection,
* 1.3× month-ahead, 1.5× monthly-review).
*
* - **Confidence labels**: the file must use at least one of `[HIGH]`,
* `[MEDIUM]`, or `[LOW]` (case-insensitive) per the "every analytical
* claim carries a confidence label" rule.
*
* - **Upstream-watchpoint reconciliation table** (Tier-C only): a
* markdown table beneath the §Upstream Watchpoint Reconciliation heading
* with at least one watchpoint row and a Disposition column signalling
* `carried forward`, `retired`, `operationalised`, `continued`, or
* `extended` — the "zero silent drops" rule.
*
* - **Cross-references**: at least one relative-path link to a sibling
* run (e.g. `../../2026-04-18/weekly-review/`) so the continuity-of-
* intelligence chain is visible in the file itself.
*
* The validator exits non-zero when any violation is found so it can be
* wired into `validate-news-generation.sh` and the per-workflow validation
* gates without any further glue.
*
* @example
* npx tsx scripts/validate-methodology-reflection.ts \
* analysis/daily/2026-04-19/month-ahead/methodology-reflection.md
*
* @see analysis/agentic-workflow-quality-plan §P1-5
* @author Hack23 AB
* @license Apache-2.0
*/
import { readFile, stat } from 'node:fs/promises';
import { existsSync } from 'node:fs';
import { basename, dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
// ---------------------------------------------------------------------------
// Contract definitions
// ---------------------------------------------------------------------------
/** Period-scope multipliers sourced from SHARED_PROMPT_PATTERNS.md. */
export const PERIOD_SCOPE_MULTIPLIERS: Readonly<Record<string, number>> = Object.freeze({
'realtime-*': 0.8,
'evening-analysis': 0.9,
'week-ahead': 1.0,
'weekly-review': 1.0,
'deep-inspection': 1.0,
'month-ahead': 1.3,
'monthly-review': 1.5,
});
/** Baseline minimum byte threshold (weekly-review / week-ahead). */
export const BASELINE_MIN_BYTES = 4000;
/**
* Required markdown H2 section titles (order-insensitive).
*
* Real exemplars prefix H2 headings with 1–2 emoji, variation-selector
* codepoints, and sometimes bullet numbering, so the preamble class is
* permissive (up to ~20 non-newline chars). Each entry accepts the
* canonical name plus documented synonyms used by pre-contract exemplars.
*/
export const REQUIRED_SECTIONS: ReadonlyArray<RegExp> = [
// Purpose — present in every post-contract exemplar.
/^##[^\n]{0,30}\bPurpose\b/mi,
// Methodology Application Matrix — doc-type folders use "Pipeline Overview" +
// "Structured Analytic Techniques Applied" as the equivalent.
/^##[^\n]{0,30}\b(?:Methodology Application Matrix|Pipeline Overview|Structured Analytic Techniques(?:\s+Applied)?)\b/mi,
// Upstream Watchpoint Reconciliation — Tier-C only (enforced downstream).
/^##[^\n]{0,30}\bUpstream Watchpoint Reconciliation\b/mi,
// Uncertainty Hot-Spots — synonyms: "Known Biases".
/^##[^\n]{0,30}\b(?:Uncertainty Hot[-\s]?Spots|Known Biases(?:\s+and Mitigations)?)\b/mi,
// Known Limitations — synonyms: "Limitations and Caveats".
/^##[^\n]{0,30}\b(?:Known Limitations|Limitations(?:\s+and Caveats)?)\b/mi,
// Pass-1 → Pass-2 Improvement Evidence — synonyms: "AI-FIRST Iteration Log",
// "What Would Strengthen Future Runs".
/^##[^\n]{0,30}(?:Pass[-\s]?1\s*[→–-]+\s*Pass[-\s]?2|AI[-\s]?FIRST Iteration Log|What Would Strengthen(?:\s+Future Runs)?)\b/mi,
// Recommendations for Doctrine Codification — synonyms: "Recommendations
// for Codification", "Recommended Upstream Changes", "Lessons for Future".
/^##[^\n]{0,30}(?:Recommendations for(?:\s+Doctrine)?\s+Codification|Recommended Upstream Changes|Lessons for Future\b[^\n]*)\b/mi,
// References — synonyms: "Cross-References", "Cross References".
/^##[^\n]{0,30}\b(?:Cross[-\s]?)?References\b/mi,
];
/** Human-readable labels parallel to {@link REQUIRED_SECTIONS}. */
export const REQUIRED_SECTION_LABELS: ReadonlyArray<string> = [
'Purpose',
'Methodology Application Matrix',
'Upstream Watchpoint Reconciliation',
'Uncertainty Hot-Spots',
'Known Limitations',
'Pass-1 → Pass-2 Improvement Evidence',
'Recommendations for Doctrine Codification',
'References',
];
/** Confidence-label tokens — at least one must appear. */
const CONFIDENCE_TOKEN = /\[(?:VERY\s+)?(?:HIGH|MEDIUM|LOW)\]/i;
/** Sibling-run relative-link pattern (e.g. `../../2026-04-18/weekly-review/`). */
const SIBLING_LINK = /\]\((?:\.\.\/){1,3}(?:\d{4}-\d{2}-\d{2}\/)?[a-z0-9-]+\//i;
/** Watchpoint disposition keywords — at least one row must match. */
const WATCHPOINT_DISPOSITION = /\b(carried forward|retired|operationalised|operationalized|continued|extended|used as evidence base)\b/i;
// ---------------------------------------------------------------------------
// Classification
// ---------------------------------------------------------------------------
/**
* Derive the period-scope multiplier for a methodology-reflection file path.
*
* `analysis/daily/YYYY-MM-DD/<subfolder>/methodology-reflection.md` — the
* subfolder determines the Tier-C multiplier (0.8× through 1.5×). Runs
* whose subfolder begins with `realtime-` use the wildcard `realtime-*`
* bucket. Unknown subfolders default to 1.0 (the reference baseline).
*/
export function derivePeriodMultiplier(filePath: string): number {
const parent = basename(dirname(filePath));
if (parent.startsWith('realtime-')) {
return PERIOD_SCOPE_MULTIPLIERS['realtime-*'];
}
return PERIOD_SCOPE_MULTIPLIERS[parent] ?? 1.0;
}
/**
* Whether the subfolder is a Tier-C workflow (aggregation · realtime ·
* deep-inspection). Only Tier-C folders are required to carry the Upstream
* Watchpoint Reconciliation table — per-document-type folders
* (`propositions/`, `motions/`, `committeeReports/`, `interpellations/`)
* have methodology-reflection.md files but are not Tier-C.
*/
export function isTierCFolder(filePath: string): boolean {
const parent = basename(dirname(filePath));
const TIER_C = new Set([
'week-ahead',
'weekly-review',
'deep-inspection',
'month-ahead',
'monthly-review',
'evening-analysis',
]);
return TIER_C.has(parent) || parent.startsWith('realtime-');
}
// ---------------------------------------------------------------------------
// Report types
// ---------------------------------------------------------------------------
/** A single contract violation reported by the validator. */
export interface ValidationIssue {
readonly severity: 'error' | 'warning';
readonly rule: string;
readonly message: string;
}
/** Aggregated validation report for one methodology-reflection.md file. */
export interface ValidationReport {
readonly file: string;
readonly bytes: number;
readonly minBytes: number;
readonly isTierC: boolean;
readonly issues: ReadonlyArray<ValidationIssue>;
/** True when no `severity: 'error'` issues are present. */
readonly ok: boolean;
}
// ---------------------------------------------------------------------------
// Core validator
// ---------------------------------------------------------------------------
/**
* Validate a single `methodology-reflection.md` file against the Tier-C
* content contract.
*
* @param filePath - Absolute or repo-relative path to the file
* @returns Structured validation report. Callers decide exit code.
*/
export async function validateMethodologyReflection(filePath: string): Promise<ValidationReport> {
const issues: ValidationIssue[] = [];
const tierC = isTierCFolder(filePath);
const multiplier = derivePeriodMultiplier(filePath);
const minBytes = Math.round(BASELINE_MIN_BYTES * multiplier);
if (!existsSync(filePath)) {
return {
file: filePath,
bytes: 0,
minBytes,
isTierC: tierC,
issues: [
{ severity: 'error', rule: 'file-exists', message: `File not found: ${filePath}` },
],
ok: false,
};
}
const [content, statResult] = await Promise.all([
readFile(filePath, 'utf-8'),
stat(filePath),
]);
const bytes = statResult.size;
// Rule 1: size floor.
if (bytes < minBytes) {
issues.push({
severity: 'error',
rule: 'min-bytes',
message: `File is ${bytes} bytes; minimum ${minBytes} bytes required (baseline ${BASELINE_MIN_BYTES} × multiplier ${multiplier}).`,
});
}
// Rule 2: required sections. Tier-C requires all; per-document-type folders
// have a lighter contract — Pipeline Overview / Methodology equivalent,
// plus References. They are leaf workflows and document the pipeline that
// produced them rather than Tier-C's continuity-of-intelligence chain.
let requiredForThisFile: ReadonlyArray<RegExp>;
let requiredLabelsForThisFile: ReadonlyArray<string>;
if (tierC) {
requiredForThisFile = REQUIRED_SECTIONS;
requiredLabelsForThisFile = REQUIRED_SECTION_LABELS;
} else {
// Index 1 = Methodology Application Matrix / Pipeline Overview (allowed
// synonym); index 7 = References. These two rows are the doc-type floor.
requiredForThisFile = [REQUIRED_SECTIONS[1], REQUIRED_SECTIONS[7]];
requiredLabelsForThisFile = [REQUIRED_SECTION_LABELS[1], REQUIRED_SECTION_LABELS[7]];
}
for (let i = 0; i < requiredForThisFile.length; i++) {
if (!requiredForThisFile[i].test(content)) {
issues.push({
severity: 'error',
rule: 'required-section',
message: `Missing required §${requiredLabelsForThisFile[i]} section (H2 heading).`,
});
}
}
// Rule 3: at least one confidence label — analytical-quality rule that
// applies to every methodology-reflection file regardless of tier.
if (!CONFIDENCE_TOKEN.test(content)) {
issues.push({
severity: 'error',
rule: 'confidence-label',
message: 'No `[HIGH]` / `[MEDIUM]` / `[LOW]` confidence labels found. Every analytical claim must carry one.',
});
}
// Rule 4 (Tier-C only): at least one cross-reference link to a sibling
// run. Doc-type leaf workflows do not participate in the continuity-of-
// intelligence chain, so this rule does not apply to them.
if (tierC && !SIBLING_LINK.test(content)) {
issues.push({
severity: 'error',
rule: 'sibling-cross-reference',
message: 'No relative-path link to a sibling run found (e.g. `../../2026-04-18/weekly-review/`). Continuity-of-intelligence chain is not visible.',
});
}
// Rule 5 (Tier-C only): Upstream-Watchpoint Reconciliation must contain a
// markdown table whose rows carry a recognised disposition keyword. This
// is the "zero silent drops" rule — without this, Upstream Watchpoint
// Reconciliation is section-present-but-content-missing.
if (tierC) {
// Extract the section body until the next `## ` heading or EOF.
// The `(?=^##\s|$(?![\s\S]))` lookahead matches either the start of the
// next H2 heading or the absolute end of the input. `$(?![\s\S])` is
// the "no more characters after this point" anchor — equivalent to
// `\z` in regex flavors that support it; the RegExp engine in V8 does
// not, hence this explicit form.
const watchpointSectionMatch = /^##[^\n]{0,30}\bUpstream Watchpoint Reconciliation\b[\s\S]*?(?=^##\s|$(?![\s\S]))/mi.exec(content);
if (watchpointSectionMatch) {
const body = watchpointSectionMatch[0];
const hasTable = /^\s*\|.+\|$/m.test(body);
const hasDisposition = WATCHPOINT_DISPOSITION.test(body);
if (!hasTable) {
issues.push({
severity: 'error',
rule: 'watchpoint-table',
message: '§Upstream Watchpoint Reconciliation has no markdown table — the reconciliation evidence is missing.',
});
}
if (!hasDisposition) {
issues.push({
severity: 'error',
rule: 'watchpoint-disposition',
message: '§Upstream Watchpoint Reconciliation contains no recognised disposition keywords (carried forward · retired · operationalised · continued · extended). Every watchpoint must be explicitly disposed of (zero silent drops).',
});
}
}
}
const ok = issues.every((i) => i.severity !== 'error');
return { file: filePath, bytes, minBytes, isTierC: tierC, issues, ok };
}
// ---------------------------------------------------------------------------
// CLI
// ---------------------------------------------------------------------------
async function main(): Promise<void> {
const args = process.argv.slice(2);
if (args.length === 0) {
console.error('Usage: validate-methodology-reflection <file.md> [<file.md> …]');
process.exit(2);
}
let totalFailures = 0;
for (const arg of args) {
const report = await validateMethodologyReflection(resolve(arg));
const label = report.isTierC ? '[Tier-C]' : '[Doc-type]';
if (report.ok) {
console.log(`✅ ${label} ${report.file} (${report.bytes} B, min ${report.minBytes} B)`);
} else {
console.error(`❌ ${label} ${report.file} (${report.bytes} B, min ${report.minBytes} B)`);
for (const issue of report.issues) {
const icon = issue.severity === 'error' ? '🔴' : '⚠️';
console.error(` ${icon} [${issue.rule}] ${issue.message}`);
}
totalFailures++;
}
}
if (totalFailures > 0) {
console.error(`\n${totalFailures} methodology-reflection file(s) failed validation.`);
process.exit(1);
} else {
console.log('\n✅ All methodology-reflection files passed validation.');
}
}
// Run CLI when invoked directly.
const isMainModule =
typeof process !== 'undefined' &&
typeof process.argv?.[1] === 'string' &&
resolve(fileURLToPath(import.meta.url)) === resolve(process.argv[1]);
Iif (isMainModule) {
main().catch((err) => {
console.error(err);
process.exit(1);
});
}
|