// tools/challenge-spec/platform-challenge-scenarios-b2.ts
// SPEC-015b: ML + IaC + IoT advanced challenge scenarios
// === SPEC-015b: ML distribution shift + bias challenges ===
export function buildMLAdvancedChallenges(spec, content) {
    const lower = content.toLowerCase();
    const hasMention = (keywords) => keywords.some((kw) => lower.includes(kw));
    void spec;
    const scenarios = [
        {
            scenario: 'Distribution shift: production data distribution differs significantly from training data',
            probability: 'high',
            impact: 'high',
            currentHandling: hasMention(['drift', 'monitoring', 'evidently', 'alibi'])
                ? 'Drift detection noted'
                : 'Not addressed',
            requiredHandling: 'Implement data drift detection (Evidently, Alibi-Detect, or custom KS-test). ' +
                'Set alert thresholds for feature distribution shift. ' +
                'Define retrain trigger policy: retrain when drift score > threshold or accuracy drops X%.',
            dataConsistency: 'Model predictions on drifted data must be flagged as low-confidence.',
            userExperience: 'Alert team when drift score exceeds threshold. Show confidence intervals in predictions.',
        },
        {
            scenario: 'Out-of-distribution input: model receives input outside training distribution and produces confidently wrong output',
            probability: 'medium',
            impact: 'high',
            currentHandling: hasMention(['ood', 'out-of-distribution', 'calibration', 'uncertainty'])
                ? 'OOD handling noted'
                : 'Not addressed',
            requiredHandling: 'Add OOD detection layer (e.g., Mahalanobis distance, ensemble disagreement). ' +
                'Apply temperature scaling for calibrated confidence. ' +
                'Return "uncertain" response rather than confident wrong answer when OOD detected.',
            dataConsistency: 'OOD inputs must be logged for later review and retrain dataset expansion.',
            userExperience: 'Return low-confidence flag + human review recommendation for OOD inputs.',
        },
        {
            scenario: 'Model bias: performance significantly worse for certain demographic subgroups',
            probability: 'medium',
            impact: 'critical',
            currentHandling: hasMention(['bias', 'fairness', 'demographic', 'subgroup'])
                ? 'Fairness testing noted'
                : 'Not addressed',
            requiredHandling: 'Run fairness metrics (demographic parity, equalized odds) on held-out subgroup test sets. ' +
                'Use tools: Fairlearn, AI Fairness 360. ' +
                'Add subgroup performance to model card. Define acceptable disparity thresholds per use case.',
            dataConsistency: 'Fairness metrics must be tracked alongside accuracy in ML registry (MLflow/W&B).',
            userExperience: 'Document model limitations and known bias in user-facing documentation.',
        },
        {
            scenario: 'Reproducibility failure: same code + seed produces different model results across environments',
            probability: 'medium',
            impact: 'medium',
            currentHandling: hasMention(['seed', 'reproducible', 'deterministic'])
                ? 'Seed management noted'
                : 'Not addressed',
            requiredHandling: 'Set random seeds globally: `random.seed(N)`, `np.random.seed(N)`, `torch.manual_seed(N)`. ' +
                'Pin library versions in requirements. Use deterministic CUDA ops: `torch.use_deterministic_algorithms(True)`. ' +
                'Store seed in experiment tracker alongside model artifact.',
            dataConsistency: 'Training run with same seed must produce bit-identical weights (or document where randomness is expected).',
            userExperience: 'Seed and environment specs published in model card for independent verification.',
        },
    ];
    return scenarios;
}
// === SPEC-015b: IaC apply-partial + state-locking + quota challenges ===
export function buildIaCAdvancedChallenges(spec, content) {
    const lower = content.toLowerCase();
    const hasMention = (keywords) => keywords.some((kw) => lower.includes(kw));
    void spec;
    const scenarios = [
        {
            scenario: 'Apply fails mid-execution: some resources created, others not — partial state',
            probability: 'medium',
            impact: 'critical',
            currentHandling: hasMention(['rollback', 'partial', 'drift'])
                ? 'Rollback strategy noted'
                : 'Not addressed',
            requiredHandling: 'Use Terraform plan files to preview changes. Add `lifecycle { prevent_destroy = true }` for critical resources. ' +
                'After failed apply: run `terraform plan` to identify drift. Never manually fix state — use `terraform import` or `state rm`. ' +
                'For CloudFormation: use stack rollback-on-failure + change sets.',
            dataConsistency: 'Terraform state file must always reflect actual cloud state. Use `terraform refresh` after partial apply.',
            userExperience: 'CI/CD pipeline must fail and notify team if apply exits non-zero. Include resource list in failure notification.',
        },
        {
            scenario: 'Concurrent state access: two pipeline runs try to modify Terraform state simultaneously',
            probability: 'medium',
            impact: 'high',
            currentHandling: hasMention(['locking', 'state lock', 'dynamo'])
                ? 'State locking configured'
                : 'Not addressed',
            requiredHandling: 'Enable DynamoDB-based state locking for S3 backend. For Terraform Cloud: state locking is automatic. ' +
                'Never run `terraform apply` from multiple CI jobs on the same workspace simultaneously. ' +
                'Use environment-based workspaces to isolate state.',
            dataConsistency: 'State lock must be acquired before any read-modify-write cycle on state file.',
            userExperience: 'CI job that cannot acquire lock should fail with "State locked by [run ID], try again in X minutes."',
        },
        {
            scenario: 'Accidental destroy of production resources with data (RDS instance, S3 bucket with data)',
            probability: 'low',
            impact: 'critical',
            currentHandling: hasMention(['prevent_destroy', 'lifecycle'])
                ? 'Destroy protection noted'
                : 'Not addressed',
            requiredHandling: 'Add `lifecycle { prevent_destroy = true }` to all stateful resources (RDS, S3, EFS). ' +
                'For S3: set versioning + MFA delete. For RDS: enable deletion protection. ' +
                'Require manual approval step in CI before any destroy operation.',
            dataConsistency: 'Data resources must have automated backups before any infrastructure change.',
            userExperience: 'Destroy of protected resources must fail with clear message and manual override instructions.',
        },
        {
            scenario: 'Cloud provider quota exceeded: resource creation fails due to account-level limits',
            probability: 'medium',
            impact: 'high',
            currentHandling: hasMention(['quota', 'limit', 'service limit'])
                ? 'Quota awareness noted'
                : 'Not addressed',
            requiredHandling: 'Check current quota usage before large deployments (aws service-quotas, gcloud quotas). ' +
                'Request quota increases in advance for production workloads. ' +
                'Add quota checks to IaC pre-flight validation.',
            dataConsistency: 'Quota errors during deployment must trigger rollback of successfully created resources.',
            userExperience: 'Pre-deploy check reports remaining quota headroom. Fail early with "Insufficient quota for X in region Y."',
        },
    ];
    return scenarios;
}
// === SPEC-015b: IoT power loss + OTA failure challenges ===
export function buildIoTAdvancedChallenges(spec, content) {
    const lower = content.toLowerCase();
    const hasMention = (keywords) => keywords.some((kw) => lower.includes(kw));
    void spec;
    const scenarios = [
        {
            scenario: 'Power loss during write to flash/EEPROM corrupts stored configuration or sensor data',
            probability: 'medium',
            impact: 'critical',
            currentHandling: hasMention(['atomic', 'commit', 'dual', 'crc'])
                ? 'Power-safe writes noted'
                : 'Not addressed',
            requiredHandling: 'Use copy-on-write pattern: write to temp sector, verify CRC, then update pointer atomically. ' +
                'Never erase + write in single step without fallback. ' +
                'For NVS (ESP32): use esp_nvs partition which handles power-loss atomicity. ' +
                'Test by cutting power at random write points.',
            dataConsistency: 'On boot, validate CRC of stored data. If corrupt, use defaults or last-known-good backup.',
            userExperience: 'Device boots into safe mode with defaults if storage is corrupt. LED indicator signals recovery mode.',
        },
        {
            scenario: 'OTA firmware update with corrupted image leaves device in unbootable partition',
            probability: 'low',
            impact: 'critical',
            currentHandling: hasMention(['dual partition', 'rollback', 'a/b', 'checksum'])
                ? 'OTA rollback strategy noted'
                : 'Not addressed',
            requiredHandling: 'Use dual-partition OTA (A/B scheme): write to inactive partition, verify SHA256 checksum, ' +
                'then atomically switch boot pointer. If new firmware fails to boot within N seconds, ' +
                'revert to previous partition automatically. ' +
                'ESP-IDF: use esp_ota_ops + rollback mark. Require minimum battery % before OTA.',
            dataConsistency: 'Active firmware partition must always contain a verified, bootable image.',
            userExperience: 'OTA progress: 0% → Download → Verify → Apply → Reboot. Failure triggers automatic rollback with cloud alert.',
        },
        {
            scenario: 'MQTT broker disconnected for >5 minutes: device queues messages but buffer overflows',
            probability: 'high',
            impact: 'high',
            currentHandling: hasMention(['buffer', 'queue', 'mqtt qos', 'persist'])
                ? 'Offline buffer noted'
                : 'Not addressed',
            requiredHandling: 'Use MQTT QoS 1 or 2 for critical messages. Implement local ring buffer in flash with defined max size. ' +
                'When buffer is full: drop oldest messages (LRU), log drop event, alert backend on reconnect. ' +
                'Use MQTT persistent sessions (cleanSession=false) to restore subscriptions on reconnect.',
            dataConsistency: 'Messages sent with QoS 1 must be acknowledged before removed from buffer. No duplicate delivery without deduplication.',
            userExperience: 'Dashboard shows "Device offline since X" and "N buffered messages pending sync" on reconnect.',
        },
        {
            scenario: 'Electromagnetic interference causes sensor readings to spike to impossible values',
            probability: 'medium',
            impact: 'medium',
            currentHandling: hasMention(['filter', 'kalman', 'median', 'emi', 'range'])
                ? 'EMI filtering noted'
                : 'Not addressed',
            requiredHandling: 'Apply median filter over N consecutive readings before using sensor values. ' +
                'Define physical bounds per sensor type (temperature: -40°C to 85°C, etc.). ' +
                'Flag values outside bounds as invalid. Apply hardware shielding for analog sensors near high-EMI sources.',
            dataConsistency: 'Invalid readings must be marked as NaN/null, not forwarded to cloud or averaged into data.',
            userExperience: 'Alert: "Sensor [ID] returned invalid reading (value: X, expected range: Y-Z). Check hardware."',
        },
    ];
    return scenarios;
}
//# sourceMappingURL=platform-challenge-scenarios-b2.js.map