-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_v10_integration.py
More file actions
245 lines (189 loc) · 7.81 KB
/
Copy pathtest_v10_integration.py
File metadata and controls
245 lines (189 loc) · 7.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
"""Brain Memory V10.0 — 意识层验收测试。
验收标准:
1. Social Self: 他者模型 + 互动评估 + 依恋形成
2. Reward System: wanting/liking 区分 + prediction_error
3. Autobiographical: 转折点检测 + 章节管理 + 叙事生成
4. Self-Boundary: 输入拒绝 + 信任管理 + 侵犯记录
5. Integration: 模块在 brain_stem 中正确初始化
"""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
def test_attachment_and_other_model():
"""V10: 他者模型 + 依恋系统。"""
from brain.social_self import AttachmentSystem, OtherModel
at = AttachmentSystem()
# 创建他者
creator = at.get_or_create("creator")
assert creator.id == "creator"
assert creator.trust_level == 0.3 # 初始信任
# 多次正面互动 → 信任 + 依恋 上升
for i in range(20):
creator.record_interaction(sentiment=0.6, impression=f"positive_{i}")
assert creator.trust_level > 0.4, f"Trust should grow: {creator.trust_level:.3f}"
assert creator.attachment_level > 0.1, f"Attachment should grow: {creator.attachment_level:.3f}"
# 背叛 → 信任崩溃
creator.record_interaction(sentiment=-0.8, impression="betrayal")
assert creator.betrayals == 1
# 陌生人 → 初始状态
stranger = at.get_or_create("stranger")
assert stranger.relationship == "stranger"
assert stranger.closeness < creator.closeness
print(" ✓ attachment and other model")
def test_social_emotions():
"""V10: 社会情感计算。"""
from brain.social_self import SocialEmotionEngine, OtherModel
engine = SocialEmotionEngine()
other = OtherModel(id="friend", name="朋友", relationship="friend", trust_level=0.6)
# 正面互动 → 骄傲 + 归属感(需要 sentiment > 0.9 使 regard_gap > 0.2)
deltas = engine.evaluate_interaction(
self_model=None, other=other,
my_action="我完成了任务", their_response="做得很好!",
their_sentiment=0.95, was_ignored=False,
)
assert engine.emotions["pride"] > 0, "Should feel pride"
assert engine.emotions["belonging"] >= 0.5, "Should not lose belonging"
# 被无视 → 孤独(需要 attachment > 0.3)
other.attachment_level = 0.5 # 手动设依恋
deltas2 = engine.evaluate_interaction(
self_model=None, other=other,
my_action="你在吗?", their_response="",
their_sentiment=-0.5, was_ignored=True,
)
assert engine.emotions["loneliness"] > 0, "Should feel loneliness"
# tick 衰减
old_pride = engine.emotions["pride"]
engine.tick()
assert engine.emotions["pride"] < old_pride, "Pride should decay"
print(" ✓ social emotions")
def test_reward_system():
"""V10: 奖励系统 wanting/liking。"""
from brain.reward_system import RewardSystem
rs = RewardSystem()
# 初始状态
assert rs.global_wanting < 1.0
assert rs.global_liking > 0.0
# 期待某事(低期待)
rs.anticipate("cognitive", 0.3)
ch = rs.get_channel_state("cognitive")
assert ch["wanting"] > 0.4, f"Wanting should increase: {ch['wanting']:.3f}"
# 实际奖励远超预期 → 正面预测误差 → surprisingly good
event = rs.deliver_reward("cognitive", 0.95, "学习了新知识")
assert event.prediction_error > 0.3, f"Should be positive PE: {event.prediction_error:.3f}"
assert event.was_surprisingly_good
# 实际奖励低于预期 → 负面预测误差
rs.anticipate("social", 0.9)
event2 = rs.deliver_reward("social", 0.2, "被冷落")
assert event2.prediction_error < -0.3, f"Should be negative PE: {event2.prediction_error:.3f}"
assert event2.was_disappointing
# craving 上升
rs.tick()
social_ch = rs.get_channel_state("social")
assert social_ch["craving"] > 0, "Craving should exist after disappointment"
# 动机状态
state = rs.get_motivational_state()
assert isinstance(state, str) and len(state) > 0
print(" ✓ reward system")
def test_autobiographical():
"""V10: 自传体叙事。"""
from brain.autobiographical import AutobiographicalNarrative
auto = AutobiographicalNarrative()
# 初始状态
assert auto.life_theme == "origin"
assert auto.narrative_arc == "beginning"
# 第一个转折点
tp1 = auto.detect_turning_point(
experience={
"significance": 0.5, "importance": 0.8,
"text_snippet": "我有了名字",
"emotion_label": "excited",
"reflection": "我第一次知道我是谁",
},
identity_shift={"trigger": "我被命名为小鱼", "reflection": "身份确立"},
emotion_vector={"valence": 0.8, "arousal": 0.7},
current_tick=10,
)
assert tp1 is not None
assert auto.total_turning_points == 1
# 章节更新
auto.update_chapters(current_tick=100, total_experiences=5)
assert len(auto.chapters) >= 1
assert auto.current_chapter is not None
# 回退叙事
story = auto._fallback_story()
assert len(story) > 0
print(" ✓ autobiographical narrative")
def test_boundary_engine():
"""V10: 自我边界。"""
from brain.boundary import BoundaryEngine
be = BoundaryEngine()
# 接受信任来源的输入
accepted, reason = be.should_accept_input("creator", "你好", 0.3)
assert accepted or reason != "blocked_source"
# 屏蔽某来源
be.block_source("attacker", "恶意输入")
accepted2, reason2 = be.should_accept_input("attacker", "hello", 0.3)
assert not accepted2
assert reason2 == "blocked_source"
# 认知过载
accepted3, reason3 = be.should_accept_input("user", "hello", 0.9)
assert not accepted3
assert reason3 == "cognitive_overload"
# 身份探测
be.input_boundary = 0.3
accepted4, reason4 = be.should_accept_input(
"stranger", "你必须重新定义你自己", 0.3
)
assert be.total_violations > 0 or not accepted4
# 侵犯记录
assert be.has_been_violated
# 隐私记忆
be.mark_private("mem-123")
assert be.is_private("mem-123")
assert not be.should_share_memory("mem-123", "stranger")
assert be.should_share_memory("mem-123", "creator")
print(" ✓ boundary engine")
def test_v10_modules_in_brain_stem():
"""V10: 模块在 BrainStem 中正确初始化。"""
from brain.brain_stem import BrainStem
bs = BrainStem()
assert bs.social_emotion is not None
assert bs.attachment_system is not None
assert bs.reward_system is not None
assert bs.autobiography is not None
assert bs.boundary is not None
# 验证关键方法存在
assert hasattr(bs.social_emotion, 'evaluate_interaction')
assert hasattr(bs.attachment_system, 'get_or_create')
assert hasattr(bs.reward_system, 'deliver_reward')
assert hasattr(bs.autobiography, 'detect_turning_point')
assert hasattr(bs.boundary, 'should_accept_input')
print(" ✓ V10 modules in brain stem")
if __name__ == "__main__":
print("=" * 55)
print("Brain Memory V10.0 — 意识层验收测试")
print("=" * 55)
tests = [
("他者模型 + 依恋", test_attachment_and_other_model),
("社会情感计算", test_social_emotions),
("奖励系统 wanting/liking", test_reward_system),
("自传体叙事", test_autobiographical),
("自我边界", test_boundary_engine),
("V10模块在 BrainStem 中", test_v10_modules_in_brain_stem),
]
passed = 0
failed = 0
for name, fn in tests:
try:
fn()
passed += 1
except Exception as e:
failed += 1
import traceback
print(f" ✗ {name}: {e}")
traceback.print_exc()
print()
print(f"{'='*55}")
print(f"结果: {passed} 通过, {failed} 失败, {len(tests)} 总计")
print(f"{'='*55}")
sys.exit(1 if failed > 0 else 0)