#!/usr/bin/env python3 """ 群星 P8 · Phase 4 闭环不变量测试 (宪章第 7/8 条: 机制可量化) 用法: python3 -m stellaris.test_closed_loop_phase4 断言 (对应文档 4.7 学习 + 4.8 元认知 + 9 认知循环 Phase 4): 1. 八引擎全部启动 (perception/state/memory/world_model/reasoning/cognitive_control/learning/metacognition) 2. 学习引擎始终在线 (stop 被拒), 元认知引擎始终在线 3. 学习次数 = 事件数 - 1 (因果错位一步, 首个事件无上一事件可学) 4. 学习: 策略权重归一化 (各 task_type 权重和 = 1) 5. 学习: validate 通过 (即时收益不为负) 6. 元认知: confidence = 1 - uncertainty (互补, 合法区间 [0,1]) 7. 元认知: capability_profile 三档任务类型齐全 8. 元认知: 能力缺口检测 (低能力任务类型被标记) 9. 元认知: 失败检测通路 (detect_failure 返回 None 或 Failure) 10. 元认知: 故障定位通路 (localize_fault 返回 fault_engine) 11. 主观时钟 = 事件数 12. 八引擎全部健康 (degradation_level=none) """ from __future__ import annotations import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) import yaml from stellaris.main_phase4 import run_closed_loop from stellaris.core.learning.learning_engine import LearningEngine from stellaris.core.metacognition.metacognition_engine import MetacognitionEngine from stellaris.core.blackboard import Blackboard def test_phase4() -> int: cfg = yaml.safe_load( (Path(__file__).resolve().parent / "configs" / "engine_config.yaml").read_text(encoding="utf-8") ) s = run_closed_loop(cfg, n_events=10) # 1. 八引擎全部启动 (health 字段确认) health_names = set(s["health"].keys()) for e in ("perception", "state", "memory", "world_model", "reasoning", "cognitive_control", "learning", "metacognition"): assert e in health_names, f"引擎 {e} 未启动 (health 缺失)" # 2. 学习/元认知始终在线 → healthy 且非 STOPPED assert s["health"]["learning"]["healthy"] is True assert s["health"]["metacognition"]["healthy"] is True # 3. 学习次数 = 事件数 - 1 (因果错位一步, 首事件无上一事件可学) n = 10 # run_closed_loop 默认 n_events=10 assert s["learning"]["n_learned"] == n - 1, \ f"学习次数 {s['learning']['n_learned']} != {n - 1} (事件数 {n} - 1)" # 4. 策略权重归一化 (各 task_type 权重和 ≈ 1) for task_type, weights in s["learning"]["policy_weights"].items(): wsum = sum(weights.values()) assert abs(wsum - 1.0) < 1e-6, \ f"task_type={task_type} 策略权重和 {wsum} != 1 (未归一化)" # 5. 学习验证: n_validated > 0 (validate 被调用) assert s["learning"]["n_validated"] >= 1, "学习验证未发生" # 6. 元认知: confidence + uncertainty = 1 (互补) mc = s["metacognition"] total = mc["confidence"] + mc["uncertainty"] assert abs(total - 1.0) < 1e-4, \ f"元认知 confidence+uncertainty={total} 应 ≈ 1" assert 0.0 <= mc["confidence"] <= 1.0, "confidence 越界" assert 0.0 <= mc["uncertainty"] <= 1.0, "uncertainty 越界" # 7. 能力画像三档齐全 for t in ("fact", "causal", "novel"): assert t in mc["capability_profile"], f"能力画像缺 {t}" # 8. 能力缺口检测 (低能力任务类型被标记, 可能为空列表) assert isinstance(mc["capability_gaps"], list) # 9. 失败检测通路 assert isinstance(mc["failure"]["engine"], str) assert mc["failure"]["severity"] >= 0.0 # 10. 故障定位通路 assert isinstance(mc["fault_location"]["fault_engine"], str) assert mc["fault_location"]["severity"] >= 0.0 # 11. 主观时钟 assert s["t_subj"] == float(10), f"主观时钟 {s['t_subj']} != 10" # 12. 八引擎全部健康 for name, h in s["health"].items(): assert h["healthy"] is True, f"引擎 {name} 不健康: {h}" assert h["degradation_level"] == "none", f"引擎 {name} 降级: {h}" # 额外: 信用分配通路 (credit_assign 可调用) learn = LearningEngine({}) credit = learn.credit_assign({"id": "e1", "task_type": "causal", "error": 0.5, "memory_hit_rate": 0.6}) assert credit.total_credit > 0.0, "信用分配 total_credit 应 > 0" assert credit.contributions.get("world_model", 0) > 0.0, "世界模型贡献度应 > 0" # 额外: 元认知 monitor/detect/assess/localize 四方法可调用 (走黑板, 无需绑定引擎) metacog = MetacognitionEngine({}, blackboard=Blackboard()) ms = metacog.monitor(None) assert 0.0 <= ms.uncertainty <= 1.0, "monitor 不确定性越界" cap = metacog.assess_capability({"type": "causal"}) assert "capability_score" in cap, "能力评估缺 capability_score" fault = metacog.localize_fault(0.3) assert "fault_engine" in fault, "故障定位缺 fault_engine" print("== P8 Phase 4 闭环不变量: 12/12 通过 ==") print(f" 学习次数={s['learning']['n_learned']}, 验证次数={s['learning']['n_validated']}, " f"元认知 confidence={mc['confidence']}, uncertainty={mc['uncertainty']}, " f"能力画像={mc['capability_profile']}") return 0 if __name__ == "__main__": sys.exit(test_phase4())