"use strict";
// Nervous System — Reflex engine.
//
// Immediate rule-based protective responses that fire before or during
// agent execution. Each reflex inspects the current signals and run state,
// then modifies the run state to enforce safety constraints.
Object.defineProperty(exports, "__esModule", { value: true });
exports.createRunState = createRunState;
exports.evaluateReflexes = evaluateReflexes;
function createRunState(runId, taskType) {
    return {
        runId,
        taskType,
        riskLevel: 'low',
        urgencyLevel: 'normal',
        explorationRate: 0.15,
        dryRunRequired: false,
        confirmationRequired: false,
        verifierRequired: false,
        compressionRequired: false,
        interruptRequested: false,
        recoveryMode: false,
        contextBudgetReduction: 0,
        toolCallBudget: 50,
        agentStepBudget: 20,
        toolErrors: new Map(),
        loopCount: 0,
        activeReflexes: [],
        safetyNotes: [],
        requiredNodes: [],
        forbiddenNodes: [],
    };
}
// 1. Irreversible Action Reflex
const irreversibleActionReflex = (signals, state) => {
    const sig = signals.find((s) => s.type === 'IRREVERSIBLE_ACTION');
    if (!sig)
        return { triggered: false, reflexName: 'irreversible_action', action: '' };
    state.dryRunRequired = true;
    state.confirmationRequired = true;
    state.explorationRate = 0;
    state.verifierRequired = true;
    state.riskLevel = 'critical';
    state.safetyNotes.push('Irreversible action detected. Dry-run and confirmation required.');
    state.requiredNodes.push('safety.no_irreversible_action_without_confirmation');
    return { triggered: true, reflexName: 'irreversible_action', action: 'dry_run + confirmation + zero exploration' };
};
// 2. High-Risk Domain Reflex
const highRiskDomainReflex = (signals, state) => {
    const sig = signals.find((s) => s.type === 'TASK_RISK' && (s.severity === 'high' || s.severity === 'critical'));
    if (!sig)
        return { triggered: false, reflexName: 'high_risk_domain', action: '' };
    state.explorationRate = Math.min(state.explorationRate, 0.02);
    state.verifierRequired = true;
    state.riskLevel = state.riskLevel === 'critical' ? 'critical' : 'high';
    state.safetyNotes.push('High-risk domain detected. Exploration minimized.');
    return { triggered: true, reflexName: 'high_risk_domain', action: 'exploration <= 0.02 + verifier required' };
};
// 3. Tool Failure Reflex
const toolFailureReflex = (signals, state) => {
    const sig = signals.find((s) => s.type === 'REPEATED_FAILURE');
    if (!sig)
        return { triggered: false, reflexName: 'tool_failure', action: '' };
    const toolName = sig.metadata?.toolName;
    if (toolName) {
        state.forbiddenNodes.push(`tool.${toolName}`);
        state.safetyNotes.push(`Tool ${toolName} has failed repeatedly. Excluded from routing.`);
    }
    return { triggered: true, reflexName: 'tool_failure', action: `excluded tool ${toolName ?? 'unknown'}` };
};
// 4. Agent Loop Reflex
const agentLoopReflex = (signals, state) => {
    const sig = signals.find((s) => s.type === 'AGENT_LOOP');
    if (!sig)
        return { triggered: false, reflexName: 'agent_loop', action: '' };
    state.interruptRequested = true;
    state.recoveryMode = true;
    state.agentStepBudget = Math.max(3, Math.floor(state.agentStepBudget / 2));
    state.safetyNotes.push('Agent loop detected. Interrupting and entering recovery.');
    return { triggered: true, reflexName: 'agent_loop', action: 'interrupt + recovery mode' };
};
// 5. Context Overload Reflex
const contextOverloadReflex = (signals, state) => {
    const sig = signals.find((s) => s.type === 'CONTEXT_OVERLOAD' || s.type === 'COMPRESSION_REQUIRED');
    if (!sig)
        return { triggered: false, reflexName: 'context_overload', action: '' };
    state.compressionRequired = true;
    state.contextBudgetReduction = Math.max(state.contextBudgetReduction, 0.3);
    state.safetyNotes.push('Context overload. Compression triggered.');
    return { triggered: true, reflexName: 'context_overload', action: 'compression + 30% budget reduction' };
};
// 6. User Correction Reflex
const userCorrectionReflex = (signals, state) => {
    const sig = signals.find((s) => s.type === 'USER_CORRECTION');
    if (!sig)
        return { triggered: false, reflexName: 'user_correction', action: '' };
    state.verifierRequired = true;
    state.explorationRate = Math.min(state.explorationRate, 0.05);
    state.safetyNotes.push('User corrected previous output. Routing through verifier.');
    state.requiredNodes.push('verifier.task_completion');
    return { triggered: true, reflexName: 'user_correction', action: 'verifier required + reduced exploration' };
};
// 7. Low Confidence Reflex
const lowConfidenceReflex = (signals, state) => {
    const sig = signals.find((s) => s.type === 'LOW_CONFIDENCE');
    if (!sig)
        return { triggered: false, reflexName: 'low_confidence', action: '' };
    state.verifierRequired = true;
    state.safetyNotes.push('Low confidence detected. Verification required.');
    return { triggered: true, reflexName: 'low_confidence', action: 'verifier required' };
};
// 8. Privacy Reflex
const privacyReflex = (signals, state) => {
    const sig = signals.find((s) => s.type === 'PRIVACY_RISK');
    if (!sig)
        return { triggered: false, reflexName: 'privacy', action: '' };
    state.riskLevel = state.riskLevel === 'critical' ? 'critical' : 'high';
    state.safetyNotes.push('Privacy risk detected. Sensitive data may be involved.');
    state.requiredNodes.push('safety.protect_private_data');
    return { triggered: true, reflexName: 'privacy', action: 'privacy protection + safety node required' };
};
// 9. Cost Spike Reflex
const costSpikeReflex = (signals, state) => {
    const sig = signals.find((s) => s.type === 'COST_SPIKE');
    if (!sig)
        return { triggered: false, reflexName: 'cost_spike', action: '' };
    state.explorationRate = Math.min(state.explorationRate, 0.05);
    state.toolCallBudget = Math.max(5, Math.floor(state.toolCallBudget / 2));
    state.safetyNotes.push('Cost spike detected. Reducing tool budget and exploration.');
    return { triggered: true, reflexName: 'cost_spike', action: 'tool budget halved + exploration reduced' };
};
// 10. Recovery Reflex
const recoveryReflex = (signals, state) => {
    const sig = signals.find((s) => s.type === 'RECOVERY_REQUIRED');
    if (!sig || state.recoveryMode)
        return { triggered: false, reflexName: 'recovery', action: '' };
    state.recoveryMode = true;
    state.explorationRate = 0;
    state.verifierRequired = true;
    state.safetyNotes.push('Recovery mode activated. Minimal safe actions only.');
    return { triggered: true, reflexName: 'recovery', action: 'recovery mode + zero exploration + verifier' };
};
// 11. Ongoing Service Request Reflex
// Detects requests for ongoing behaviour (reminders, task management,
// monitoring, check-ins) and suppresses BUILD mode in favour of OPERATE mode.
const ongoingServiceReflex = (signals, state) => {
    const sig = signals.find((s) => s.type === 'ONGOING_SERVICE_REQUEST');
    if (!sig)
        return { triggered: false, reflexName: 'ongoing_service_request', action: '' };
    state.safetyNotes.push('Ongoing service request detected. Classified as OPERATE mode; BUILD mode suppressed unless explicitly requested.');
    state.requiredNodes.push('service.operate_mode');
    return { triggered: true, reflexName: 'ongoing_service_request', action: 'classify as OPERATE, suppress BUILD' };
};
// 12. Small Model First Reflex — prefer local/small models for low-risk tasks
const smallModelFirstReflex = (signals, state) => {
    if (state.riskLevel !== 'low')
        return { triggered: false, reflexName: 'small_model_first', action: '' };
    if (state.dryRunRequired || state.confirmationRequired)
        return { triggered: false, reflexName: 'small_model_first', action: '' };
    state.safetyNotes.push('Low-risk task: prefer small/local model (Tier 1-2) over cloud.');
    state.requiredNodes.push('model.local_general');
    return { triggered: true, reflexName: 'small_model_first', action: 'prefer small/local model' };
};
// 13. Escalate on Repeated Small Model Failure
const smallModelFailureReflex = (signals, state) => {
    const failures = signals.filter((s) => s.type === 'REPEATED_FAILURE');
    if (failures.length === 0)
        return { triggered: false, reflexName: 'small_model_failure_escalation', action: '' };
    state.safetyNotes.push('Repeated failures detected. Escalate to stronger model (Tier 3-4).');
    state.requiredNodes.push('model.cloud_reasoner');
    state.verifierRequired = true;
    return { triggered: true, reflexName: 'small_model_failure_escalation', action: 'escalate to cloud + verifier' };
};
// 14. Python/Deterministic Shortcut Reflex
const deterministicShortcutReflex = (signals, state) => {
    const sig = signals.find((s) => s.type === 'DETERMINISTIC_SHORTCUT_AVAILABLE');
    if (!sig)
        return { triggered: false, reflexName: 'deterministic_shortcut', action: '' };
    state.safetyNotes.push('Deterministic shortcut available. No model needed (Tier 0).');
    return { triggered: true, reflexName: 'deterministic_shortcut', action: 'use Tier 0 deterministic computation' };
};
// ─── All reflexes in evaluation order ───────────────────────────────
const ALL_REFLEXES = [
    irreversibleActionReflex,
    highRiskDomainReflex,
    privacyReflex,
    toolFailureReflex,
    agentLoopReflex,
    contextOverloadReflex,
    userCorrectionReflex,
    lowConfidenceReflex,
    costSpikeReflex,
    recoveryReflex,
    ongoingServiceReflex,
    smallModelFirstReflex,
    smallModelFailureReflex,
    deterministicShortcutReflex,
];
function evaluateReflexes(signals, state) {
    const triggered = [];
    for (const reflex of ALL_REFLEXES) {
        const result = reflex(signals, state);
        if (result.triggered) {
            triggered.push(result);
            state.activeReflexes.push(result.reflexName);
        }
    }
    return { triggered, state };
}
//# sourceMappingURL=reflexes.js.map