All files / scripts/agentic/gate-checks pir-status.ts

93.18% Statements 41/44
92.1% Branches 35/38
100% Functions 3/3
93.18% Lines 41/44

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                                                                        2x       2x       2x           12x 12x   12x 2x         2x       10x 10x 10x   1x         1x     9x   9x                 9x 1x             9x 7x     9x 4x             9x       9x 9x 54x                 9x 1x                 7x   7x 1x             7x 21x 1x               7x 1x             7x 1x             7x 1x           7x 1x              
/**
 * @module scripts/agentic/gate-checks/pir-status
 * @description Check 9 — Validate pir-status.json sidecar file structure
 *              (schema version, cycle/date/subfolder consistency, per-PIR
 *              id/status/confidence/answer_summary rules).
 *
 * @see .github/prompts/05-analysis-gate.md §Check 9
 * @author Hack23 AB
 * @license Apache-2.0
 */
 
import { readFile } from 'node:fs/promises';
import { existsSync } from 'node:fs';
import { join } from 'node:path';
 
import type { GateCheckResult } from '../gate-shared/types.js';
 
/** PIR status JSON schema structure. */
interface PirStatusFile {
  readonly schema_version?: string;
  readonly cycle?: string;
  readonly date?: string;
  readonly subfolder?: string;
  readonly pirs?: readonly PirEntry[];
  readonly generated_at?: string;
}
 
/** A single PIR entry in the sidecar file. */
interface PirEntry {
  readonly pir_id?: string;
  readonly statement?: string;
  readonly status?: string;
  readonly confidence?: string;
  readonly answer_summary?: string;
}
 
const VALID_PIR_STATUSES = new Set([
  'open', 'answered', 'superseded', 'deferred', 'cancelled',
]);
 
const VALID_CONFIDENCE_LEVELS = new Set([
  'VERY HIGH', 'HIGH', 'MEDIUM', 'LOW', 'VERY LOW',
]);
 
const PIR_ID_PATTERN = /^PIR-[A-Za-z0-9]+(-[A-Za-z0-9]+)*$/;
 
/**
 * Validate pir-status.json exists and has valid structure.
 */
export async function checkPirStatus(analysisDir: string): Promise<GateCheckResult[]> {
  const results: GateCheckResult[] = [];
  const filePath = join(analysisDir, 'pir-status.json');
 
  if (!existsSync(filePath)) {
    results.push({
      checkId: 'pir-status',
      passed: false,
      message: 'pir-status.json missing or empty',
    });
    return results;
  }
 
  let data: PirStatusFile;
  try {
    const raw = await readFile(filePath, 'utf-8');
    data = JSON.parse(raw) as PirStatusFile;
  } catch {
    results.push({
      checkId: 'pir-status',
      passed: false,
      message: 'pir-status.json: invalid JSON',
    });
    return results;
  }
 
  validateTopLevel(data, results);
 
  Iif (!Array.isArray(data.pirs)) {
    results.push({
      checkId: 'pir-status',
      passed: false,
      message: "pir-status.json: 'pirs' field must be a JSON array",
    });
    return results;
  }
 
  if (data.subfolder !== data.cycle) {
    results.push({
      checkId: 'pir-status',
      passed: false,
      message: `pir-status.json: subfolder='${data.subfolder}' must equal cycle='${data.cycle}'`,
    });
  }
 
  for (const pir of data.pirs) {
    validatePirEntry(pir, results);
  }
 
  if (results.length === 0) {
    results.push({
      checkId: 'pir-status',
      passed: true,
      message: 'pir-status.json: valid',
    });
  }
 
  return results;
}
 
function validateTopLevel(data: PirStatusFile, results: GateCheckResult[]): void {
  const requiredFields = ['schema_version', 'cycle', 'date', 'subfolder', 'pirs', 'generated_at'] as const;
  for (const field of requiredFields) {
    Iif (!(field in data) || data[field as keyof PirStatusFile] === undefined) {
      results.push({
        checkId: 'pir-status',
        passed: false,
        message: `pir-status.json: missing required field '${field}'`,
      });
    }
  }
 
  if (data.schema_version !== '1.0') {
    results.push({
      checkId: 'pir-status',
      passed: false,
      message: "pir-status.json: schema_version must be '1.0'",
    });
  }
}
 
function validatePirEntry(pir: PirEntry, results: GateCheckResult[]): void {
  const pid = pir.pir_id ?? '(no id)';
 
  if (!pir.pir_id || !PIR_ID_PATTERN.test(pir.pir_id)) {
    results.push({
      checkId: 'pir-status',
      passed: false,
      message: `pir-status.json pir=${pid}: invalid pir_id format`,
    });
  }
 
  for (const field of ['statement', 'status', 'confidence'] as const) {
    if (!pir[field]) {
      results.push({
        checkId: 'pir-status',
        passed: false,
        message: `pir-status.json pir=${pid}: missing required field "${field}"`,
      });
    }
  }
 
  if (pir.status && !VALID_PIR_STATUSES.has(pir.status)) {
    results.push({
      checkId: 'pir-status',
      passed: false,
      message: `pir-status.json pir=${pid}: invalid status '${pir.status}'`,
    });
  }
 
  if (pir.confidence && !VALID_CONFIDENCE_LEVELS.has(pir.confidence)) {
    results.push({
      checkId: 'pir-status',
      passed: false,
      message: `pir-status.json pir=${pid}: invalid confidence '${pir.confidence}'`,
    });
  }
 
  if (pir.status === 'answered' && !pir.answer_summary) {
    results.push({
      checkId: 'pir-status',
      passed: false,
      message: `pir-status.json pir=${pid}: status=answered requires non-empty answer_summary`,
    });
  }
  if (pir.status !== 'answered' && pir.answer_summary !== undefined) {
    results.push({
      checkId: 'pir-status',
      passed: false,
      message: `pir-status.json pir=${pid}: status=${pir.status} must not carry answer_summary`,
    });
  }
}