第205章 ProtectionEngine——风险保护、保护动作与保护结果
205.1 ProtectionEngine的定义
在前面的风险与异常处理体系中,RiskEngine负责发现风险、计算风险、排序风险并形成风险处理候选;DiagnosisEngine负责分析已经发生的异常及其原因;RepairEngine负责异常发生后的修复。
但是,在风险已经被发现、而实际失败尚未发生时,系统还需要一个专门负责“保护”的计算机制。
因此,本章建立 ProtectionEngine(保护引擎)。
ProtectionEngine是WSaiOS-ICAI体系中负责根据当前风险状态、风险等级、对象状态、环境条件、规则与可用保护能力,计算保护方案、生成保护动作,并根据实际执行结果判断保护是否有效的Engine。
其核心定义为:
ProtectionEngine=RiskProtection+ProtectionAction+ProtectionResultProtectionEngine= RiskProtection+ ProtectionAction+ ProtectionResult
即:
ProtectionEngine = 风险保护计算 + 保护动作计算与执行协调 + 保护结果计算
ProtectionEngine解决三个核心问题:
- 当前风险应该采取什么保护方式?
- 应该执行什么具体保护动作?
- 保护动作执行以后,风险是否真正得到控制?
因此其基本计算路径为:
Risk → Protection Calculation → Protection Action → Execution → Result → Verification → Protection Result
ProtectionEngine并不是简单的“风险报警器”,也不是RepairEngine。
它的核心任务是:
在风险可能造成实际损害之前,或者风险尚未发展为不可逆失败之前,通过确定性的规则和计算建立保护措施,并通过实际执行结果验证保护是否有效。
整个过程不需要LLM、Transformer、Embedding、Vector Search、Prompt Engineering、神经网络或其他生成式模型机制。
205.2 Protection的定义
Protection表示针对已经识别出的Risk,为避免风险发生、降低风险影响、限制风险扩散或者维持系统安全状态而采取的保护机制。
保护模型定义为:
P=(R,C,A,S)P=(R,C,A,S)
其中:
- PP:Protection,保护对象
- RR:Risk,目标风险
- CC:Protection Condition,保护条件
- AA:Protection Action,保护动作
- SS:Protection State,保护状态
因此:
Protection=Risk+Condition+Action+StateProtection= Risk+ Condition+ Action+ State
Protection不是Risk本身。
Risk表示:
“可能发生什么问题。”
Protection表示:
“为了避免或者限制这个问题,系统准备采取什么措施。”
例如:
当前资源数量低于任务要求。
Risk:
Resource Insufficient
Protection:
Pause current task and switch to another available resource.
这里:
- Resource Insufficient = Risk
- Pause Task = Protection Action
- Switch Resource = Protection Action
- ProtectionEngine = 计算是否需要保护、采用什么保护动作以及保护是否有效
205.3 ProtectionEngine核心模型
ProtectionEngine可以定义为:
PE=(R,C,P,A,Ex,Ro,S,V,T)PE=(R,C,P,A,E_x,R_o,S,V,T)
其中:
- PEPE:ProtectionEngine
- RR:Risk
- CC:Protection Condition
- PP:Protection
- AA:Protection Action
- ExE_x:Execution,实际执行过程
- RoR_o:Protection Result,保护结果
- SS:State,当前状态
- VV:Verification,验证结果
- TT:Time,时间
其基本关系为:
Risk→Protection→Action→Execution→Result→VerificationRisk \rightarrow Protection \rightarrow Action \rightarrow Execution \rightarrow Result \rightarrow Verification
ProtectionEngine并不直接取代ActionEngine和ExecutionEngine。
其职责是:
计算应该保护什么、为什么保护、采取什么保护动作以及如何判断保护结果。
实际动作执行仍然由ActionEngine、ExecutionEngine等运行时Engine完成。
205.4 ProtectionEngine的输入
ProtectionEngine的输入不是单一Risk,而是完整的运行上下文。
定义:
InputP=(R,S,O,Sc,Ca,M,Ru,Ev,H,Ex,Env,T)Input_P= (R,S,O,Sc,Ca,M,Ru,Ev,H,Ex,Env,T)
其中:
- RR:Risk,当前风险
- SS:State,当前状态
- OO:Object,对象
- ScSc:Scene,场景
- CaCa:Capability,可用能力
- MM:Method,可用方法
- RuRu:Rule,保护规则
- EvEv:Evidence,证据
- HH:History,历史
- ExEx:Experience,经验
- EnvEnv:Environment,环境
- TT:Time,时间
其中:
Ev=(Source,Type,Value,T)Ev=(Source,Type,Value,T)
表示保护计算使用的证据。
保护Engine必须优先使用当前事实。
历史和经验只能作为辅助计算依据,不能覆盖当前实际状态。
205.5 风险保护计算
ProtectionEngine首先判断当前Risk是否已经达到需要保护的条件。
定义:
ProtectionRequired=RiskActive∧Condition∧ImpactProtectionRequired= RiskActive \land Condition \land Impact
其中:
- RiskActiveRiskActive:风险处于有效状态
- ConditionCondition:保护条件成立
- ImpactImpact:风险影响达到保护阈值
如果:
ProtectionRequired=TrueProtectionRequired=True
则系统进入保护计算。
如果:
ProtectionRequired=FalseProtectionRequired=False
则不执行保护动作。
因此基本流程为:
Risk Detection → Risk Evaluation → Protection Required → Protection Calculation
ProtectionEngine不会因为“存在Risk记录”就自动执行保护。
风险存在和保护需要是两个不同判断。
205.6 风险等级与保护等级
RiskEngine计算RiskScore:
RiskScore=Pr×IrRiskScore=P_r\times I_r
其中:
- PrP_r:Risk Probability,风险概率
- IrI_r:Risk Impact,风险影响
ProtectionEngine根据RiskScore计算ProtectionLevel。
例如:
ProtectionLevel=f(RiskScore,State,Condition,Rule)ProtectionLevel= f(RiskScore,State,Condition,Rule)
可以定义为:
| RiskScore | ProtectionLevel |
|---|---|
| 0 | None |
| 1–3 | Monitor |
| 4–6 | Warning |
| 7–12 | Restrict |
| 13–20 | Strong |
| >20 | Emergency |
这里的具体阈值属于系统规则,不是ProtectionEngine的固定数学常数。
实际工程中应当由Rule表或者配置数据提供。
因此:
ProtectionLevel≠RiskLevelProtectionLevel \neq RiskLevel
RiskLevel描述风险严重程度。
ProtectionLevel描述系统需要采取的保护强度。
205.7 保护候选计算
当保护条件成立后,ProtectionEngine不能立即执行任意动作,而应该先产生保护候选。
定义:
PCi=(R,A,C,S,E)PC_i=(R,A,C,S,E)
其中:
- PCiPC_i:第 ii 个Protection Candidate
- RR:目标Risk
- AA:Protection Action
- CC:执行条件
- SS:预期状态
- EE:Evidence
例如一个资源风险可能产生:
Candidate 1 → Pause
Candidate 2 → Reduce Load
Candidate 3 → Switch Resource
Candidate 4 → Change Method
Candidate 5 → Human Review
Candidate 6 → Cancel
ProtectionEngine负责计算这些候选。
当多个候选同时存在时,可以将候选交给DecisionEngine进行最终选择。
因此:
ProtectionEngine负责保护方案计算,DecisionEngine负责多个保护方案之间的最终选择。
205.8 保护动作定义
Protection Action表示ProtectionEngine针对风险采取的具体动作。
保护动作模型:
PA=(T,O,C,P,R,S)PA=(T,O,C,P,R,S)
其中:
- TT:Action Type,动作类型
- OO:Target Object,动作对象
- CC:Condition,动作条件
- PP:Parameters,动作参数
- RR:Expected Result,预期结果
- SS:Expected State,预期状态
常见保护动作包括:
Monitor
Warn
Pause
Restrict
Delay
Reduce
Switch
Block
Isolate
Rollback
Increase Verification
Change Method
Reduce Resource Exposure
Cancel
Human Review
保护动作不是固定的一组字符串,而是具有对象、条件、参数、预期结果和状态的完整Action对象。
205.9 保护动作与普通Action的区别
Protection Action本质上仍然属于Action体系,但其目的不同。
普通Action:
为完成Behavior而执行操作。
Protection Action:
为降低Risk而执行操作。
因此:
ActionGoal≠ProtectionGoalActionGoal \neq ProtectionGoal
例如:
Normal Action
→ Send Request
Protection Action
→ Stop Sending Request
普通Action可能推动任务继续。
Protection Action可能主动阻止任务继续。
因此ProtectionEngine不能简单等同于ActionEngine。
二者关系为:
ProtectionEngine→ProtectionAction→ActionEngine→ExecutionEngineProtectionEngine \rightarrow ProtectionAction \rightarrow ActionEngine \rightarrow ExecutionEngine
ProtectionEngine负责保护逻辑。
ActionEngine负责动作计算。
ExecutionEngine负责实际运行。
205.10 保护动作可执行条件
Protection Action不能直接执行。
定义:
ProtectionExecutable=R∧C∧O∧Ca∧S∧RuProtectionExecutable= R \land C \land O \land Ca \land S \land Ru
其中:
- RR:Risk有效
- CC:Protection Condition成立
- OO:目标对象存在且有效
- CaCa:所需Capability可用
- SS:当前State允许执行
- RuRu:Protection Rule允许执行
如果其中任何关键条件不成立:
ProtectionExecutable=FalseProtectionExecutable=False
此时不能直接执行保护动作。
系统可以:
重新计算 → 更换保护候选 → 请求Decision → 升级人工处理
而不是强行执行。
205.11 保护动作执行流程
ProtectionEngine自身负责保护动作的组织与执行协调。
完整流程为:
Risk
→ Protection Required
→ Protection Candidate
→ Protection Condition
→ Protection Action
→ ActionEngine
→ ExecutionEngine
→ Actual Result
→ FeedbackEngine
→ ProtectionEngine
→ Protection Result
这里必须保持三个层次:
第一层:ProtectionEngine
决定:
为什么保护、保护什么、采取什么保护措施。
第二层:ActionEngine
决定:
这个保护动作具体如何形成可执行Action。
第三层:ExecutionEngine
负责:
这个Action在运行环境中实际发生了什么。
因此:
ProtectionEngine≠ActionEngine≠ExecutionEngineProtectionEngine \neq ActionEngine \neq ExecutionEngine
205.12 保护结果
Protection Result表示保护动作实际执行后的结果。
定义:
PR=(P,A,Ex,R,S,C,V,T)PR=(P,A,E_x,R,S,C,V,T)
其中:
- PP:Protection
- AA:Protection Action
- ExE_x:实际Execution
- RR:Actual Result
- SS:Actual State
- CC:Comparison
- VV:Verification
- TT:Time
保护结果必须来源于实际执行。
不能因为Protection Action已经创建,就直接认为保护成功。
因此:
ProtectionCreated≠ProtectionExecuted≠ProtectionSuccessful≠ProtectionVerifiedProtectionCreated \neq ProtectionExecuted \neq ProtectionSuccessful \neq ProtectionVerified
这是ProtectionEngine非常重要的工程边界。
205.13 保护结果判断
保护结果的核心是比较:
保护前状态
与
保护后实际状态
定义:
ΔS=Compare(Sbefore,Safter)\Delta S=Compare(S_{before},S_{after})
其中:
- SbeforeS_{before}:保护动作执行前状态
- SafterS_{after}:保护动作执行后的实际状态
- ΔS\Delta S:状态变化
然后判断Risk:
Rafter=Evaluate(R,Safter,C)R_{after}=Evaluate(R,S_{after},C)
如果:
Riskafter<RiskbeforeRisk_{after}<Risk_{before}
说明风险得到降低。
如果:
Riskafter=RiskbeforeRisk_{after}=Risk_{before}
说明保护没有产生有效风险下降。
如果:
Riskafter>RiskbeforeRisk_{after}>Risk_{before}
说明保护失败或者产生了新的风险。
205.14 保护结果状态
ProtectionResult可以定义为:
Created
Ready
Running
Succeeded
PartiallySucceeded
Failed
Blocked
Cancelled
Expired
Ineffective
Verified
Rejected
其中尤其需要区分:
Succeeded
动作执行本身成功。
Effective
风险实际得到降低。
Verified
保护结果已经通过验证。
因此:
Executed≠SucceededExecuted \neq Succeeded Succeeded≠EffectiveSucceeded \neq Effective Effective≠VerifiedEffective \neq Verified
例如:
Pause Task
→ Action执行成功
→ Task确实暂停
→ 但Risk仍然存在
那么Action执行成功,但Protection可能是:
Ineffective
只有风险确实得到控制,并且结果经过验证,才可以进入Verified状态。
205.15 保护有效性计算
定义:
ProtectionEffective=RiskReduced∧StateSafe∧ConditionSatisfiedProtectionEffective= RiskReduced \land StateSafe \land ConditionSatisfied
其中:
- RiskReducedRiskReduced:风险已经降低
- StateSafeStateSafe:对象进入允许的安全状态
- ConditionSatisfiedConditionSatisfied:保护条件得到满足
进一步定义验证:
ProtectionVerified=Result∧State∧Condition∧EvidenceProtectionVerified= Result \land State \land Condition \land Evidence
其中:
- Result:存在实际结果
- State:存在实际状态
- Condition:保护条件成立
- Evidence:存在有效证据
因此:
ProtectionVerified=TrueProtectionVerified=True
才能将保护结果正式记录为Verified。
205.16 风险未降低时的处理
如果保护执行后风险仍然存在:
Riskafter≥RiskbeforeRisk_{after}\geq Risk_{before}
ProtectionEngine不能无限重复执行同一个Protection Action。
此时应该进入:
Protection Failed
→ Re-evaluation
→ New Protection Candidate
→ Decision
→ New Protection Action
如果连续保护无效:
Protection Failure
→ Risk Escalation
→ Diagnosis
→ Repair / Human Review
这可以避免形成:
Protection
→ Failure
→ Protection
→ Failure
→ Protection
→ Failure
的无限循环。
205.17 Protection与Risk的关系
RiskEngine负责:
RiskCalculation→RiskRankingRiskCalculation \rightarrow RiskRanking
ProtectionEngine负责:
Risk→ProtectionCalculation→ProtectionAction→ProtectionResultRisk \rightarrow ProtectionCalculation \rightarrow ProtectionAction \rightarrow ProtectionResult
因此:
RiskEngine
↓
Risk
↓
ProtectionEngine
↓
Protection
↓
Protection Action
↓
Execution
↓
Protection Result
RiskEngine告诉系统:
哪个风险重要。
ProtectionEngine告诉系统:
对这个风险采取什么保护。
205.18 Protection与Diagnosis的关系
Protection发生在风险控制阶段。
Diagnosis主要发生在异常已经出现之后。
因此:
Risk→ProtectionRisk \rightarrow Protection
而:
Failure→DiagnosisFailure \rightarrow Diagnosis
例如:
资源不足风险
→ Pause
→ Switch Resource
属于Protection。
如果资源切换以后依然失败:
Execution Failure
→ Abnormality
→ Diagnosis
→ Cause Analysis
→ Repair
属于Diagnosis与Repair。
因此:
Protection解决“如何避免问题继续发生”。
Diagnosis解决“问题为什么发生”。
Repair解决“发生以后如何恢复”。
205.19 Protection与Repair的区别
Protection与Repair不能混为一谈。
| 项目 | Protection | Repair |
|---|---|---|
| 时间 | 风险发生前/发展中 | 异常或失败发生后 |
| 目标 | 防止、限制、降低风险 | 恢复正常 |
| 输入 | Risk | Diagnosis |
| 核心对象 | Risk | Cause/Failure |
| 输出 | Protection Action | Repair Action |
| 判断 | Risk是否下降 | State是否恢复 |
| 后续 | Re-evaluation | Verification |
因此:
Risk→ProtectionRisk\rightarrow Protection
而:
Failure→Diagnosis→RepairFailure\rightarrow Diagnosis\rightarrow Repair
二者虽然都可能改变State,但触发条件不同。
205.20 Protection与Feedback
ProtectionEngine需要通过FeedbackEngine获取实际结果。
完整关系:
ProtectionAction→Execution→Result→Feedback→ProtectionResultProtectionAction \rightarrow Execution \rightarrow Result \rightarrow Feedback \rightarrow ProtectionResult
Feedback不是ProtectionResult本身。
Result表示:
实际发生了什么。
Feedback表示:
对实际发生结果进行结构化反馈。
ProtectionResult表示:
保护措施最终是否达到预期效果。
因此:
Result
→ Feedback
→ Protection Result
而不是:
Action
→ Protection Success
205.21 ProtectionEngine与StateEngine
保护动作通常会改变系统状态。
例如:
Active
→ Pause
→ Paused
或者:
Normal
→ Restricted
因此ProtectionEngine不能直接随意修改State。
正确关系:
ProtectionEngine→StateEngine→StateServiceProtectionEngine \rightarrow StateEngine \rightarrow StateService
ProtectionEngine产生:
StateChangeCandidateStateChangeCandidate
StateEngine负责验证:
Transition(St,Event,Co,Ru)→St+1Transition(S_t,Event,Co,Ru)\rightarrow S_{t+1}
只有合法状态转换才能正式保存。
205.22 ProtectionEngine与DecisionEngine
如果只有一个明显有效的保护动作,可以直接进入Action执行。
如果存在多个保护候选:
Pause
Reduce
Switch
Change Method
Cancel
Human Review
则:
ProtectionEngine→DecisionEngineProtectionEngine \rightarrow DecisionEngine
DecisionEngine根据:
- 风险等级
- 目标优先级
- 当前State
- Capability
- Method
- Resource
- Cost
- Protection effectiveness
- History
- Experience
进行候选选择。
因此:
ProtectionEngine产生保护候选,DecisionEngine负责多个候选之间的最终选择。
205.23 ProtectionEngine与Experience
历史保护结果可以形成Experience。
例如系统过去记录:
Risk:
Resource Shortage
Protection:
Switch Resource
Result:
Successful
Verification:
Verified
经过多次相同场景验证以后,可以形成:
Experience:
When Resource Shortage occurs under Condition X,
Switch Resource has a high verified success rate.
但Experience不是ProtectionEngine直接“学习”出来的模型参数。
它是:
History
→ Memory
→ Comparison
→ Experience
形成的结构化经验。
ProtectionEngine只读取Experience作为辅助计算依据。
205.24 ProtectionEngine完整PHP计算实现
以下实现采用PHP 5.6/7兼容写法,不使用类型化属性、箭头函数、现代语法,也不引入任何LLM机制。
<?php
abstract class Engine
{
public function calculate($input)
{
return array();
}
}
class ProtectionEngine extends Engine
{
public function calculate($input)
{
$risk = isset($input['risk']) ? $input['risk'] : array();
$state = isset($input['state']) ? $input['state'] : array();
$object = isset($input['object']) ? $input['object'] : array();
$condition = isset($input['condition']) ? $input['condition'] : array();
$rules = isset($input['rules']) ? $input['rules'] : array();
$evidence = isset($input['evidence']) ? $input['evidence'] : array();
$time = isset($input['time']) ? $input['time'] : time();
$riskResult = $this->calculateRiskProtection(
$risk,
$state,
$condition,
$rules
);
if (!$riskResult['required']) {
return array(
'status' => 'no_protection_required',
'risk' => $risk,
'protection_required' => false,
'protection_candidates' => array(),
'time' => $time
);
}
$candidates = $this->buildProtectionCandidates(
$risk,
$state,
$object,
$condition,
$rules
);
$ranked = $this->rankProtectionCandidates(
$candidates,
$risk,
$state
);
return array(
'status' => 'protection_candidates_created',
'risk' => $risk,
'protection_required' => true,
'risk_score' => $riskResult['risk_score'],
'protection_level' => $riskResult['protection_level'],
'protection_candidates' => $ranked,
'evidence' => $evidence,
'time' => $time
);
}
public function calculateRiskProtection(
$risk,
$state,
$condition,
$rules
) {
$probability = isset($risk['probability'])
? (float)$risk['probability']
: 0;
$impact = isset($risk['impact'])
? (float)$risk['impact']
: 0;
$riskScore = $probability * $impact;
$active = $this->isRiskActive($risk);
$conditionValid = $this->isConditionValid($condition);
$required = $active && $conditionValid && $riskScore > 0;
return array(
'required' => $required,
'risk_score' => $riskScore,
'protection_level' => $this->calculateProtectionLevel(
$riskScore
),
'risk_active' => $active,
'condition_valid' => $conditionValid
);
}
protected function isRiskActive($risk)
{
if (!isset($risk['state'])) {
return false;
}
$state = strtolower($risk['state']);
return in_array(
$state,
array(
'detected',
'evaluated',
'active',
'escalated'
)
);
}
protected function isConditionValid($condition)
{
if (isset($condition['valid'])) {
return (bool)$condition['valid'];
}
return true;
}
protected function calculateProtectionLevel($riskScore)
{
if ($riskScore <= 0) {
return 'none';
}
if ($riskScore <= 3) {
return 'monitor';
}
if ($riskScore <= 6) {
return 'warning';
}
if ($riskScore <= 12) {
return 'restrict';
}
if ($riskScore <= 20) {
return 'strong';
}
return 'emergency';
}
protected function buildProtectionCandidates(
$risk,
$state,
$object,
$condition,
$rules
) {
$candidates = array();
$riskType = isset($risk['type'])
? strtolower($risk['type'])
: '';
if ($riskType == 'resource') {
$candidates[] = $this->createCandidate(
'reduce_resource_exposure',
$risk,
$object,
'resource exposure reduction'
);
$candidates[] = $this->createCandidate(
'switch_resource',
$risk,
$object,
'switch to another resource'
);
}
if ($riskType == 'state') {
$candidates[] = $this->createCandidate(
'pause',
$risk,
$object,
'pause current behavior'
);
$candidates[] = $this->createCandidate(
'restrict',
$risk,
$object,
'restrict risky operation'
);
}
if ($riskType == 'method') {
$candidates[] = $this->createCandidate(
'change_method',
$risk,
$object,
'change current method'
);
}
if ($riskType == 'environment') {
$candidates[] = $this->createCandidate(
'delay',
$risk,
$object,
'delay execution until environment is safe'
);
}
$candidates[] = $this->createCandidate(
'increase_verification',
$risk,
$object,
'increase verification level'
);
return $candidates;
}
protected function createCandidate(
$type,
$risk,
$object,
$reason
) {
return array(
'type' => $type,
'risk_id' => isset($risk['id']) ? $risk['id'] : null,
'object_id' => isset($object['id'])
? $object['id']
: null,
'reason' => $reason,
'state' => 'candidate'
);
}
protected function rankProtectionCandidates(
$candidates,
$risk,
$state
) {
foreach ($candidates as $key => $candidate) {
$score = 0;
switch ($candidate['type']) {
case 'pause':
$score = 90;
break;
case 'restrict':
$score = 85;
break;
case 'switch_resource':
$score = 80;
break;
case 'change_method':
$score = 75;
break;
case 'reduce_resource_exposure':
$score = 70;
break;
case 'delay':
$score = 65;
break;
case 'increase_verification':
$score = 60;
break;
default:
$score = 50;
}
$candidates[$key]['score'] = $score;
}
usort($candidates, array($this, 'compareCandidateScore'));
return $candidates;
}
public function compareCandidateScore($a, $b)
{
if ($a['score'] == $b['score']) {
return 0;
}
return ($a['score'] > $b['score']) ? -1 : 1;
}
public function validateProtectionAction(
$risk,
$condition,
$object,
$capability,
$state,
$rule
) {
$validRisk = $this->isRiskActive($risk);
$validCondition = $this->isConditionValid($condition);
$validObject = !empty($object);
$validCapability = !empty($capability);
$validState = !empty($state);
$validRule = !empty($rule);
return
$validRisk &&
$validCondition &&
$validObject &&
$validCapability &&
$validState &&
$validRule;
}
public function calculateProtectionResult(
$protection,
$action,
$execution,
$actualResult,
$beforeState,
$afterState,
$riskBefore,
$riskAfter,
$evidence
) {
$stateChanged = $this->compareState(
$beforeState,
$afterState
);
$riskReduced = $this->isRiskReduced(
$riskBefore,
$riskAfter
);
$effective =
$riskReduced &&
$stateChanged;
$verified =
$effective &&
!empty($evidence);
if ($verified) {
$status = 'verified';
} elseif ($effective) {
$status = 'effective';
} else {
$status = 'ineffective';
}
return array(
'protection' => $protection,
'action' => $action,
'execution' => $execution,
'actual_result' => $actualResult,
'before_state' => $beforeState,
'after_state' => $afterState,
'state_changed' => $stateChanged,
'risk_reduced' => $riskReduced,
'effective' => $effective,
'verified' => $verified,
'status' => $status,
'evidence' => $evidence
);
}
protected function compareState(
$beforeState,
$afterState
) {
return $beforeState != $afterState;
}
protected function isRiskReduced(
$riskBefore,
$riskAfter
) {
$before = isset($riskBefore['score'])
? (float)$riskBefore['score']
: 0;
$after = isset($riskAfter['score'])
? (float)$riskAfter['score']
: 0;
return $after < $before;
}
}
上述代码实现了ProtectionEngine最基本的三个核心计算:
calculateRiskProtection()
负责风险保护判断。
buildProtectionCandidates()
负责产生保护动作候选。
calculateProtectionResult()
负责根据实际执行结果判断保护是否有效。
同时保留:
validateProtectionAction()
用于执行前验证。
205.25 ProtectionResult的进一步计算
为了让保护结果能够进入后续Feedback、Risk、Decision和Learning体系,可以进一步定义:
PR=(P,A,Ex,R,Sb,Sa,ΔS,Rb,Ra,V,T)PR= (P,A,E_x,R,S_b,S_a,\Delta S,R_b,R_a,V,T)
其中:
- PP:Protection
- AA:Action
- ExE_x:Execution
- RR:Actual Result
- SbS_b:Before State
- SaS_a:After State
- ΔS\Delta S:State Change
- RbR_b:Risk Before
- RaR_a:Risk After
- VV:Verification
- TT:Time
于是可以完整计算:
ΔRisk=Riskbefore−Riskafter\Delta Risk=Risk_{before}-Risk_{after}
如果:
ΔRisk>0\Delta Risk>0
表示风险下降。
如果:
ΔRisk=0\Delta Risk=0
表示风险没有变化。
如果:
ΔRisk<0\Delta Risk<0
表示风险反而上升。
这比简单使用:
success = true
更加符合ICAI的结构化认知体系。
205.26 ProtectionEngine数据库结构
ProtectionEngine对应的数据库可以设计为:
protections
保存保护对象。
id
risk_id
protection_type
condition
state
level
created_at
updated_at
protection_actions
保存保护动作。
id
protection_id
action_type
object_id
parameters
expected_result
expected_state
state
created_at
protection_executions
保存保护动作实际执行。
id
protection_action_id
execution_id
started_at
finished_at
state
protection_results
保存保护结果。
id
protection_id
action_id
execution_id
actual_result
before_state
after_state
risk_before
risk_after
risk_delta
effective
verified
status
created_at
protection_verifications
保存保护验证。
id
protection_result_id
verification_type
evidence
result
status
verified_at
protection_history
保存保护历史。
id
protection_id
event_type
old_state
new_state
reason
created_at
因此数据库结构形成:
Risk
↓
Protection
↓
Protection Action
↓
Protection Execution
↓
Protection Result
↓
Protection Verification
↓
Protection History
205.27 ProtectionEngine与Service层
ProtectionEngine负责计算。
ProtectionService负责业务编排。
架构为:
Controller
↓
ProtectionService
↓
ProtectionEngine
↓
ActionEngine
↓
ExecutionEngine
↓
FeedbackEngine
↓
ProtectionEngine
↓
ProtectionService
↓
Repository
↓
MySQL
ProtectionService负责:
- 加载Risk
- 加载Object
- 加载State
- 调用ProtectionEngine
- 保存Protection
- 调用DecisionService
- 调用ActionService
- 调用ExecutionService
- 接收Feedback
- 保存ProtectionResult
- 调用Verification
- 更新Risk状态
ProtectionEngine不应该直接负责SQL。
205.28 ProtectionEngine与Runtime
Protection必须基于当前Runtime。
定义:
Runtime=(I,O,S,C,T)Runtime=(I,O,S,C,T)
其中:
- II:Individual
- OO:Object
- SS:State
- CC:Condition
- TT:Time
因此:
ProtectionResultt≠ProtectionResultt+1ProtectionResult_t \neq ProtectionResult_{t+1}
同一个Risk在不同Runtime下可能产生不同保护动作。
例如:
Risk = Resource Insufficient
在Runtime A:
Alternative Resource Available
可能:
Switch Resource
在Runtime B:
No Alternative Resource
可能:
Pause
在Runtime C:
Risk Critical
Alternative Resource Unavailable
可能:
Cancel + Human Review
因此ProtectionEngine不是固定动作映射器,而是:
ProtectionAction=f(Risk,Runtime,Rule,Condition,Capability,Experience)ProtectionAction= f(Risk,Runtime,Rule,Condition,Capability,Experience)
205.29 ProtectionEngine的确定性原则
ProtectionEngine必须遵循确定性计算。
相同输入、相同规则、相同状态条件下,应得到相同的保护计算结果。
即:
PE(X,R)=PE(X,R)PE(X,R)=PE(X,R)
系统不能使用随机生成机制决定:
是否保护
也不能使用不可解释的模型参数决定:
为什么选择Pause
所有保护结果应该可以追溯到:
Risk
→ Rule
→ Condition
→ Calculation
→ Candidate
→ Action
→ Execution
→ Result
→ Verification
205.30 ProtectionEngine的解释链
ProtectionEngine必须提供完整解释链:
为什么触发保护?
↓
Risk成立
↓
RiskScore达到条件
↓
ProtectionCondition成立
↓
生成Protection Candidate
↓
为什么选择该Candidate?
↓
Rule + Condition + Capability + State
↓
产生Protection Action
↓
实际Execution
↓
Actual Result
↓
Risk重新计算
↓
Risk是否下降?
↓
Verification
这样系统能够回答:
为什么进行了保护?
为什么选择这个保护动作?
保护是否真的执行?
保护后风险是否下降?
为什么认为保护有效?
这正是ICAI Engine体系区别于简单条件脚本的重要地方。
205.31 ProtectionEngine完整运行链
正常保护流程:
Risk Detection
→ Risk Evaluation
→ Protection Required
→ Protection Level
→ Protection Candidate
→ Candidate Validation
→ Decision
→ Protection Action
→ ActionEngine
→ ExecutionEngine
→ Actual Result
→ FeedbackEngine
→ StateEngine
→ Risk Re-evaluation
→ Protection Result
→ Verification
→ History
→ Memory
→ Experience
→ Learning
如果保护成功:
Protection Result
→ Risk Controlled
→ Safe State
→ Continue Behavior
如果保护失败:
Protection Result
→ Risk Still Active
→ New Protection Candidate
→ Decision
→ New Protection
如果多次保护失败:
Protection Failure
→ Risk Escalation
→ DiagnosisEngine
→ RepairEngine
→ Verification
205.32 ProtectionEngine异常路径
完整异常路径:
Risk
↓
Protection
↓
Protection Action
↓
Execution
↓
Failure
↓
Feedback
↓
Protection Failure
↓
Risk Escalation
↓
Diagnosis
↓
Repair
↓
Verification
这里需要明确:
Protection失败不等于Diagnosis完成。
ProtectionEngine只说明:
当前保护措施没有达到预期。
DiagnosisEngine进一步分析:
为什么保护没有达到预期。
RepairEngine再进一步处理:
如何恢复系统。
205.33 ProtectionEngine与LearningEngine
保护历史可以成为LearningEngine的学习数据。
例如:
Risk Type:
Resource Shortage
Protection:
Switch Resource
Execution:
Success
Risk:
Reduced
Verification:
Passed
经过验证以后形成:
Learning Data
LearningEngine可以据此更新:
- Protection Rule
- Method
- Capability
- Risk Evaluation
- Decision Condition
但必须遵循:
Learning=Evidence+Comparison+Verification+UpdateLearning= Evidence+ Comparison+ Verification+ Update
而不是:
Protection Result
→ Automatically Modify Rule
未经验证的数据不能直接修改核心规则。
205.34 ProtectionEngine与MemoryEngine
Protection历史可以进入MemoryEngine。
Memory保存:
Protection Event
Protection Action
Execution Result
Risk Change
Verification
然后形成可检索记忆。
例如:
Memory:
某对象在某状态下发生资源不足风险时,
切换备用资源曾经成功。
MemoryEngine负责:
Memory Calculation
Memory Recall
Memory Update
ProtectionEngine负责使用这些已经存在的结构化信息。
205.35 ProtectionEngine与ExperienceEngine
Memory经过比较、关联和验证以后,可以形成Experience。
例如:
Experience=Condition+Risk+Protection+Result+VerificationExperience= Condition+ Risk+ Protection+ Result+ Verification
形成:
Condition A
→ Risk B
→ Protection C
→ Result Success
→ Verified
以后再次出现相似条件时,ProtectionEngine可以将该Experience作为候选保护依据。
但是:
Experience≠RuleExperience \neq Rule
经验不能自动变成绝对规则。
只有经过规则确认和验证后,才能成为正式Protection Rule。
205.36 ProtectionEngine的工程边界
ProtectionEngine不负责:
不负责风险发现的全部过程
由:
RiskEngine
负责。
不负责最终多候选决策
由:
DecisionEngine
负责。
不负责底层动作计算
由:
ActionEngine
负责。
不负责实际运行环境执行
由:
ExecutionEngine
负责。
不负责异常原因分析
由:
DiagnosisEngine
负责。
不负责修复
由:
RepairEngine
负责。
不负责最终学习更新
由:
LearningEngine
负责。
因此:
ProtectionEngine=Risk Protection+Protection Action+Protection ResultProtectionEngine = Risk\ Protection + Protection\ Action + Protection\ Result
而不是整个风险处理系统。
205.37 ProtectionEngine在ICAI中的位置
前面的Engine体系可以进一步形成:
ObjectEngine
↓
StateEngine
↓
RelationEngine
↓
SceneEngine
↓
KnowledgeEngine
↓
CapabilityEngine
↓
MatchingEngine
↓
MethodEngine
↓
DecisionEngine
↓
BehaviorEngine
↓
ActionEngine
↓
ExecutionEngine
↓
FeedbackEngine
↓
RiskEngine
↓
ConflictEngine
↓
ProtectionEngine
↓
DiagnosisEngine
↓
RepairEngine
↓
MemoryEngine
↓
ExperienceEngine
↓
LearningEngine
ProtectionEngine位于Risk与Diagnosis/Repair之间。
它形成了ICAI中的“风险预防层”。
其核心作用是:
Risk
→ Protection
→ Risk Reduction
而不是:
Risk
→ Failure
→ Diagnosis
205.38 ProtectionEngine核心数学模型
最终可以将ProtectionEngine统一表示为:
PE=f(R,S,O,Sc,Ca,M,Ru,Ev,H,Ex,Env,T)PE= f(R,S,O,Sc,Ca,M,Ru,Ev,H,Ex,Env,T)
其中:
- RR:风险
- SS:当前状态
- OO:对象
- ScSc:场景
- CaCa:能力
- MM:方法
- RuRu:规则
- EvEv:证据
- HH:历史
- ExEx:经验
- EnvEnv:环境
- TT:时间
输出:
PE→(P,A,PR,V)PE\rightarrow(P,A,PR,V)
其中:
- PP:Protection
- AA:Protection Action
- PRPR:Protection Result
- VV:Verification
因此:
Risk→ProtectionEngineProtection→ActionEngineAction→ExecutionEngineExecution→FeedbackEngineFeedback→ProtectionEngineProtectionResultRisk \xrightarrow{ProtectionEngine} Protection \xrightarrow{ActionEngine} Action \xrightarrow{ExecutionEngine} Execution \xrightarrow{FeedbackEngine} Feedback \xrightarrow{ProtectionEngine} ProtectionResult
最终形成闭环:
Risk→Protection→Action→Execution→Result→Feedback→Risk Re−evaluationRisk \rightarrow Protection \rightarrow Action \rightarrow Execution \rightarrow Result \rightarrow Feedback \rightarrow Risk\ Re-evaluation
205.39 ProtectionEngine与ICAI闭环
将ProtectionEngine加入完整ICAI认知运行体系后:
Need→Goal→Capability→Method→Decision→Behavior→Action→Execution→Result→Feedback→Risk→Protection→Verification→Memory→Experience→Learning→Knowledge/Capability/Method Update→DecisionNeed \rightarrow Goal \rightarrow Capability \rightarrow Method \rightarrow Decision \rightarrow Behavior \rightarrow Action \rightarrow Execution \rightarrow Result \rightarrow Feedback \rightarrow Risk \rightarrow Protection \rightarrow Verification \rightarrow Memory \rightarrow Experience \rightarrow Learning \rightarrow Knowledge/Capability/Method\ Update \rightarrow Decision
如果发生真正异常:
Risk→Protection→ProtectionFailure→Diagnosis→Repair→VerificationRisk \rightarrow Protection \rightarrow ProtectionFailure \rightarrow Diagnosis \rightarrow Repair \rightarrow Verification
因此ProtectionEngine补充了ICAI系统中非常关键的一层:
系统不仅能够在问题发生以后进行诊断和修复,也能够在风险形成过程中主动计算保护措施。
205.40 本章总结
ProtectionEngine是WSaiOS-ICAI风险控制体系中的保护计算Engine。
其核心模型为:
ProtectionEngine=RiskProtection+ProtectionAction+ProtectionResultProtectionEngine= RiskProtection+ ProtectionAction+ ProtectionResult
风险保护首先判断:
ProtectionRequired=RiskActive∧Condition∧ImpactProtectionRequired= RiskActive \land Condition \land Impact
然后形成:
Risk→ProtectionCandidate→ProtectionActionRisk \rightarrow ProtectionCandidate \rightarrow ProtectionAction
实际动作通过:
ProtectionEngine→ActionEngine→ExecutionEngineProtectionEngine \rightarrow ActionEngine \rightarrow ExecutionEngine
完成。
执行以后:
Execution→Result→Feedback→RiskRe−evaluationExecution \rightarrow Result \rightarrow Feedback \rightarrow RiskRe-evaluation
再计算:
ΔRisk=Riskbefore−Riskafter\Delta Risk= Risk_{before}-Risk_{after}
如果:
ΔRisk>0\Delta Risk>0
则说明风险下降。
最终:
ProtectionVerified=Result∧State∧Condition∧EvidenceProtectionVerified= Result \land State \land Condition \land Evidence
只有经过实际执行和证据验证,保护才能进入Verified状态。
因此,本章最终建立:
Risk
↓
Risk Protection
↓
Protection Candidate
↓
Decision
↓
Protection Action
↓
ActionEngine
↓
ExecutionEngine
↓
Actual Result
↓
FeedbackEngine
↓
Risk Re-evaluation
↓
Protection Result
↓
Verification
↓
Safe State / New Protection / Diagnosis
ProtectionEngine由此成为WSaiOS-ICAI中连接 Risk、Decision、Action、Execution、Feedback、State、Diagnosis、Repair、Memory、Experience与Learning 的关键风险保护Engine。
其核心原则可以概括为:
风险不是失败,保护不是修复,动作成功不是保护成功,保护成功也必须经过实际结果和证据验证。
最终形成:
Risk→Protection→Action→Execution→Result→Verification\boxed{ Risk \rightarrow Protection \rightarrow Action \rightarrow Execution \rightarrow Result \rightarrow Verification }
这构成了ICAI体系完整的风险保护闭环。