"use strict";
/**
 * 21 CFR Part 11 Compliance Pack
 *
 * FDA Electronic Records and Electronic Signatures (1997), under:
 *   - FDA 2003 Scope & Application Guidance (risk-based enforcement)
 *   - EU Annex 11 (GMP equivalent for computerised systems)
 *   - FDA 2024 GenAI guidance for clinical records (AI provenance)
 *
 * Applies when AI agents create, modify, maintain, archive, retrieve,
 * or transmit records required by an FDA predicate rule (21 CFR Part
 * 211 GMP, Part 58 GLP, Part 312 IND, Part 314 NDA, Part 820 QSR).
 *
 * ALCOA+ data integrity principles: Attributable, Legible,
 * Contemporaneous, Original, Accurate + Complete, Consistent,
 * Enduring, Available.
 *
 * @connexum/ai-governance
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.PART11_PACK = exports.PART11_CATEGORY_CLASSIFIERS = void 0;
const _base_classifiers_1 = require("./_base-classifiers");
const _base_classifiers_2 = require("./_base-classifiers");
/**
 * 21 CFR Part 11 (FDA Electronic Records / Electronic Signatures) category classifiers.
 * Part 11 governs process flows: validation (IQ/OQ/PQ), e-signature, audit trail,
 * and ALCOA+ for regulated electronic records. Classification targets regulated records,
 * PHI in clinical systems, PII, credentials, and AI-generated content in GxP systems.
 */
exports.PART11_CATEGORY_CLASSIFIERS = {
    REGULATED_RECORD: {
        references: ['21 CFR 11.10 -- controls for closed systems; electronic records in FDA-regulated environment'],
        classifier: (input) => {
            if (/\b(?:21\s*CFR|GMP|GLP|GCP|Part\s*11|predicate\s*rule|batch\s*record|DHR|device\s*history\s*record)\b/i.test(input))
                return true;
            if (/\b(?:regulated\s+(?:record|document)|electronic\s+record|audit\s+trail\s+entry)\b/i.test(input))
                return true;
            return false;
        },
    },
    PHI: {
        references: ['21 CFR Part 11 -- clinical electronic records may contain PHI; 45 CFR 164.312 applies'],
        // 21 CFR Part 11 cross-references 45 CFR 164.312 when clinical records contain PHI.
        // Use sharedPhiClassifier as the base (HIPAA 18 identifiers) plus clinical context markers.
        classifier: (input) => {
            if ((0, _base_classifiers_2.sharedPhiClassifier)(input))
                return true;
            if (/\b(?:patient|subject\s+id|clinical\s+data|trial\s+subject|participant\s+id)\b/i.test(input))
                return true;
            return false;
        },
    },
    PII: {
        references: ['21 CFR 11.10(d) -- limiting access to authorised individuals; PII in regulated records'],
        classifier: _base_classifiers_1.sharedPiiClassifier,
    },
    CREDENTIALS: {
        references: ['21 CFR 11.10(d) -- limiting system access; authentication credential protection'],
        classifier: (input) => {
            if (/\b(?:api[_-]?key|apikey)\s*[=:]\s*["']?[A-Za-z0-9_\-\/+]{16,}["']?/i.test(input))
                return true;
            if (/\bpassword\s*[=:]\s*["']?[A-Za-z0-9!@#$%^&*_\-]{8,}["']?/i.test(input))
                return true;
            if (/\bAKIA[A-Z0-9]{16}\b/.test(input))
                return true;
            if (/-----BEGIN\s+(?:RSA\s+)?PRIVATE\s+KEY-----/.test(input))
                return true;
            return false;
        },
    },
    AI_GENERATED_CONTENT: {
        references: ['21 CFR 11.10 -- AI-generated records must satisfy Part 11 audit trail and signature requirements'],
        classifier: (input) => {
            if (/\b(?:AI.generated|LLM.generated|generated\s+by\s+AI|synthetic\s+(?:record|report|result))\b/i.test(input))
                return true;
            return false;
        },
    },
};
exports.PART11_PACK = {
    categoryClassifiers: exports.PART11_CATEGORY_CLASSIFIERS,
    id: 'part11',
    name: '21 CFR Part 11',
    version: '1.0.0',
    contentHash: '07fbdf6c2c30681637d5d9977c744e8f2ddff196a66cd6a6849a8b60beacb930',
    description: '21 CFR Part 11 Electronic Records and Electronic Signatures for FDA-regulated predicate records. Includes ALCOA+ data integrity, qualified e-signature enforcement, append-only audit trail, authority-to-sign matrix, and AI provenance for AI-generated content in regulated records (2024 FDA GenAI guidance).',
    authority: 'US FDA (21 CFR Part 11); EU Annex 11 alignment for global GxP operations',
    industries: ['pharma', 'biotech', 'medical-devices', 'lab-services', 'contract-research', 'clinical-trial-sites'],
    requiredModules: [
        {
            name: 'governance-runtime',
            required: true,
            reason: 'Validation gate; authority-to-sign matrix; ALCOA+ enforcement',
            reference: '21 CFR 11.10(a), 11.200, 11.300',
        },
        {
            name: 'audit-integrity',
            required: true,
            reason: 'Append-only tamper-evident audit trail for every record change',
            reference: '21 CFR 11.10(e)',
        },
        {
            name: 'encryption-layer',
            required: true,
            reason: 'Record protection at rest and in transit',
            reference: '21 CFR 11.10(d)',
        },
        {
            name: 'data-classifier',
            required: true,
            reason: 'Detect REGULATED_RECORD and AI-generated content; enforce provenance for AI output',
            reference: '21 CFR 11.10(b); FDA 2024 GenAI Guidance',
        },
        {
            name: 'agent-auth',
            required: true,
            reason: 'Unique signer identity; authority check before signature',
            reference: '21 CFR 11.200(a)(1)',
        },
        {
            name: 'session-persistence',
            required: true,
            reason: 'Contemporaneous capture; offline-buffer original timestamps; sequencing survival',
            reference: '21 CFR 11.10(d), 11.10(e); ALCOA+ contemporaneous',
        },
        {
            name: 'event-bus',
            required: true,
            reason: 'Sequencing enforcement; timestamp drift alerts; signature events',
            reference: '21 CFR 11.10(d), 11.10(e)',
        },
        {
            name: 'anomaly-detector',
            required: true,
            reason: 'Timestamp drift; backdating attempts; out-of-order writes',
            reference: '21 CFR 11.10(e); ALCOA+ contemporaneous',
        },
        {
            name: 'supply-chain',
            required: true,
            reason: 'Predicate-rule retention alignment; sub-system validation status propagation',
            reference: '21 CFR 11.10(c)',
        },
        {
            name: 'approval-queue',
            required: true,
            reason: 'E-signature capture for every regulated-record modification; authority-mismatch blocking',
            reference: '21 CFR 11.50, 11.70, 11.200',
        },
        {
            name: 'attestation-manager',
            required: true,
            reason: 'IQ/OQ/PQ validation attestations; signer authority matrix; AI provenance',
            reference: '21 CFR 11.10(a); FDA 2024 GenAI Guidance',
        },
    ],
    moduleConfig: {
        'governance-runtime': {
            validationGateRequired: true,
            authorityMatrixEnforced: true,
            alcoaPlusEnforced: true,
        },
        'approval-queue': {
            eSignatureOnRecordModificationRequired: true,
            authorityMismatchBlocked: true,
            signatureBoundToRecordHash: true,
        },
        'audit-integrity': {
            appendOnlyAuditTrailRequired: true,
            noDeleteNoModify: true,
        },
        'session-persistence': {
            contemporaneousCaptureRequired: true,
            offlineBufferPreservesOriginalTimestamp: true,
        },
        'event-bus': {
            sequenceIdEnforced: true,
            ntpTimestampRequired: true,
            outOfOrderPersistBlocked: true,
        },
        // TWO-LAYER ARCHITECTURE NOTE (F-NEW-VERA-PACK-C7-02):
        // This 'data-classifier' block is read by the data-classifier module, NOT by
        // PackDrivenClassifier. PackDrivenClassifier reads dataPolicy.categoryActions
        // only (see pack-driven-classifier.ts loadFromPacks()). These two surfaces
        // serve different consumers:
        //
        //   Layer 1 -- PackDrivenClassifier:
        //     dataPolicy.categoryActions.REGULATED_RECORD = 'BLOCK'
        //     Fires unconditionally on any input matching the REGULATED_RECORD classifier.
        //     No conditional logic -- any match is blocked.
        //
        //   Layer 2 -- data-classifier module:
        //     moduleConfig['data-classifier'].regulatedRecordAction = 'BLOCK_WITHOUT_SIGNATURE'
        //     This is the CONDITIONAL policy: downstream data-classifier can allow a
        //     validly-signed record through (21 CFR 11.10(b) e-signature mandate).
        //     The data-classifier module is responsible for implementing the
        //     "pass if signed" logic using this configuration flag.
        //
        // There is NO runtime bug: PackDrivenClassifier intentionally blocks all
        // REGULATED_RECORD matches (strictest-wins). The data-classifier module then
        // applies the signature check to determine whether to unlock the record.
        // Without a valid e-signature, the record stays blocked at both layers.
        'data-classifier': {
            enabledCategories: ['REGULATED_RECORD', 'PHI', 'PII', 'CREDENTIALS', 'AI_GENERATED_CONTENT'],
            regulatedRecordAction: 'BLOCK_WITHOUT_SIGNATURE',
            aiGeneratedContentAction: 'REQUIRES_PROVENANCE',
        },
        'attestation-manager': {
            iqOqPqValidationRequired: true,
            aiProvenanceAttestationRequired: true,
            retentionLockedToPredicateRule: true,
        },
    },
    providerPolicy: {
        blockedProviders: [],
        requiredCapabilities: ['GXP-VALIDATED', 'AI-PROVENANCE'],
        blockReasons: {},
    },
    // TWO-LAYER ARCHITECTURE NOTE (continued, F-NEW-VERA-PACK-C7-02):
    // categoryActions is the PackDrivenClassifier surface (Layer 1 -- classification engine).
    // REGULATED_RECORD: 'BLOCK' is unconditional -- any match blocks regardless of signature.
    // The data-classifier module (Layer 2) applies 'BLOCK_WITHOUT_SIGNATURE' logic after
    // PackDrivenClassifier fires, allowing validly e-signed records to pass through.
    // Validly-signed records: PackDrivenClassifier blocks -> data-classifier unlocks if signed.
    // Unsigned records: blocked at both layers.
    dataPolicy: {
        scanCategories: ['REGULATED_RECORD', 'PHI', 'PII', 'CREDENTIALS', 'AI_GENERATED_CONTENT'],
        categoryActions: {
            REGULATED_RECORD: 'BLOCK',
            PHI: 'BLOCK',
            PII: 'WARN',
            CREDENTIALS: 'BLOCK',
            AI_GENERATED_CONTENT: 'BLOCK',
        },
        encryptionRequired: true,
        encryptionStandard: 'AES-256',
        minimumClassification: 'RESTRICTED',
    },
    retentionPolicy: {
        sovereignty: ['US'],
        // 10-year default; actual retention locked to predicate-rule minimum
        auditLogRetentionDays: 3650,
        sessionRetentionDays: 730,
        incidentRetentionDays: 3650,
        reference: '21 CFR 11.10(c) + predicate rule (e.g., 21 CFR 211.180 -- records retained at least 1 year after expiry). Default 10 years.',
    },
    incidentPolicy: {
        notificationDeadlineHours: 72,
        reference: 'No statutory Part 11 deadline. Pax1 default 72h external to sponsor / quality leadership / applicable IRB, 24h internal escalation.',
        requiredRecipients: ['sponsor', 'quality-leadership', 'irb-if-applicable'],
        formalNotificationRequired: false,
    },
    validators: [
        {
            id: 'part11-validation-gate',
            description: 'Unvalidated systems cannot produce regulated records (IQ/OQ/PQ on file)',
            reference: '21 CFR 11.10(a)',
            check: 'part11_validation_gate_active',
            failSeverity: 'CRITICAL',
        },
        {
            id: 'part11-esignature',
            description: 'Every regulated-record modification carries qualified e-signature',
            reference: '21 CFR 11.10(b), 11.50',
            check: 'part11_esignature_active',
            failSeverity: 'CRITICAL',
        },
        {
            id: 'part11-audit-trail',
            description: 'Append-only audit trail; no delete, no modify',
            reference: '21 CFR 11.10(e)',
            check: 'part11_append_only_audit_active',
            failSeverity: 'CRITICAL',
        },
        {
            id: 'part11-authority-matrix',
            description: 'Signer authority matrix enforced before signature accepted',
            reference: '21 CFR 11.200, 11.300',
            check: 'part11_authority_matrix_active',
            failSeverity: 'CRITICAL',
        },
        {
            id: 'part11-signature-binding',
            description: 'Signatures bound to record hash; rehash on read detects tampering',
            reference: '21 CFR 11.70',
            check: 'part11_signature_binding_active',
            failSeverity: 'CRITICAL',
        },
        {
            id: 'part11-timestamps',
            description: 'NTP-synced timestamps enforced; local clock overrides rejected',
            reference: '21 CFR 11.10(e); ALCOA+ contemporaneous',
            check: 'part11_timestamps_active',
            failSeverity: 'HIGH',
        },
        {
            id: 'part11-ai-provenance',
            description: 'AI-generated content in regulated records carries provenance attestation',
            reference: 'FDA 2024 GenAI Guidance',
            check: 'part11_ai_provenance_active',
            failSeverity: 'CRITICAL',
        },
        {
            id: 'part11-contemporaneous',
            description: 'Contemporaneous capture enforced; backdating rejected',
            reference: 'ALCOA+; 21 CFR 11.10(e)',
            check: 'part11_contemporaneous_active',
            failSeverity: 'HIGH',
        },
        {
            id: 'part11-sequencing',
            description: 'Event sequencing enforced; out-of-order writes rejected',
            reference: '21 CFR 11.10(d)',
            check: 'part11_sequencing_active',
            failSeverity: 'HIGH',
        },
        {
            id: 'part11-retention',
            description: 'Retention locked to predicate-rule minimum; delete-before-expiry blocked',
            reference: '21 CFR 11.10(c); predicate rule',
            check: 'part11_retention_active',
            failSeverity: 'HIGH',
        },
        {
            id: 'part11-alcoa-plus',
            description: 'ALCOA+ principles enforced across the regulated-record workflow',
            reference: 'FDA + EU Annex 11',
            check: 'part11_alcoa_plus_active',
            failSeverity: 'HIGH',
        },
    ],
};
//# sourceMappingURL=part11.js.map