Skip to main content

aios_agent/backends/
fallback.rs

1//! FallbackNoOpBackend —— 熔断后的系统级安全兜底(采用 issue #10 方案 A)。
2//!
3//! 方案 A 语义:fallback 生成一个可审计的安全 NoOp,允许通过 PolicyEngine。
4//! 返回的 Idle/NoOp 意图固定 `confidence=1.0`、`risk_level=Low`,确保不会被
5//! PolicyEngine 的普通 confidence threshold(默认 0.3)或风险上限拦截。
6//! 后端层面的失败信号(如“circuit breaker engaged”)由
7//! `DecisionBackendResult::error` 单独承载,并随 audit record 的
8//! `backend_error` 字段进入 trace,与意图置信度解耦。
9//!
10//! 这与方案 B(fallback 不进入 action pipeline)不同:方案 A 保证每条动作
11//! 仍产出恰好一条终态审计记录,维持 lifecycle 不变量。
12
13use std::time::Instant;
14
15use aios_spec::{
16    ActionType, ActionUrgency, DecisionBackendResult, DecisionRoute, Intent, IntentBatch,
17    IntentType, RiskLevel, StructuredContext, SuggestedAction,
18};
19
20use crate::{new_id, DecisionBackend};
21
22pub struct FallbackNoOpBackend;
23
24impl DecisionBackend for FallbackNoOpBackend {
25    fn evaluate(&self, context: &StructuredContext) -> DecisionBackendResult {
26        let start = Instant::now();
27        let intent_batch = IntentBatch {
28            window_id: context.window_id.clone(),
29            intents: vec![Intent {
30                intent_id: new_id(),
31                intent_type: IntentType::Idle,
32                confidence: 1.0,
33                risk_level: RiskLevel::Low,
34                suggested_actions: vec![SuggestedAction {
35                    action_type: ActionType::NoOp,
36                    target: None,
37                    urgency: ActionUrgency::IdleTime,
38                }],
39                rationale_tags: vec!["fallback_noop".into()],
40            }],
41            generated_at_ms: context.window_end_ms,
42            model: "fallback-noop-v0.1".to_string(),
43        };
44
45        DecisionBackendResult {
46            route: DecisionRoute::FallbackNoOp,
47            intent_batch,
48            rationale_tags: vec!["fallback_noop".into()],
49            latency_us: start.elapsed().as_micros() as u64,
50            error: Some("circuit breaker engaged — falling back to no-op".into()),
51        }
52    }
53}