fix(r2): keep her self-narration and life within the approved room

Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
白栖知
2026-10-24 12:03:08 +08:00
co-authored by multica-agent
parent 64a47f35d0
commit c285a0fd07
11 changed files with 8311 additions and 5 deletions
+37 -2
View File
@@ -33,6 +33,7 @@ class R2LLM(ControlledLLM):
self.persona_result = PERSONA
self.reply = "我今天给一段旋律换了个结尾,停在了喜欢的那个音上。"
self.background_calls = []
self.background_messages = []
self.block = False
self.entered = None
self.release = None
@@ -51,6 +52,7 @@ class R2LLM(ControlledLLM):
return
prompt = messages[0].content
self.background_calls.append(prompt)
self.background_messages.append(messages)
if self.entered:
self.entered.set()
if self.block:
@@ -157,7 +159,8 @@ def test_new_persona_and_relation_do_not_mutate_any_layer(tmp_path):
assert c.post("/api/companion", json=CREATE).status_code == 409
def test_old_profile_never_auto_backfilled_and_explicit_backfill_is_honest(tmp_path):
@pytest.mark.parametrize("hour,life_count", [(11, 1), (12, 0)])
def test_old_profile_never_auto_backfilled_and_explicit_backfill_is_honest(tmp_path, monkeypatch, hour, life_count):
app, _ = app_for(tmp_path, enabled=False)
with TestClient(app) as c:
c.post("/api/companion", json=CREATE)
@@ -166,11 +169,20 @@ def test_old_profile_never_auto_backfilled_and_explicit_backfill_is_honest(tmp_p
birth = fingerprint(s)
raw = s.query_one("SELECT identity_json FROM companion")[0]
chats = [tuple(r) for r in s.query("SELECT * FROM chat_message")]
# Freeze only the runtime clock before lifespan starts, leaving the real loop enabled.
stamp = datetime(2026, 10, 24, hour, 11, tzinfo=ZoneInfo("Asia/Shanghai")).timestamp()
monkeypatch.setattr("cybergril.api.app.LifeRuntime",
lambda *args: LifeRuntime(*args, clock=lambda: stamp))
app2, p = app_for(tmp_path)
with TestClient(app2) as c:
c.portal.call(app2.state.life.tick) # settle the current slot; repeated ticks are idempotent
assert c.get("/api/persona").json()["state"] == "not_initialized"
assert c.post("/api/persona/retry").status_code == 409
assert not p.background_calls
assert not app2.state.store.query("SELECT * FROM persona_interp")
assert not app2.state.store.query("SELECT * FROM persona_job")
assert len(app2.state.store.query("SELECT * FROM life_event")) == life_count
assert len(p.background_calls) == life_count
assert all("本人生活自述" in prompt for prompt in p.background_calls)
persona = app2.state.persona
c.portal.call(lambda: persona.ensure(backfill=True))
view = c.get("/api/persona").json()
@@ -181,6 +193,29 @@ def test_old_profile_never_auto_backfilled_and_explicit_backfill_is_honest(tmp_p
assert [tuple(r) for r in app2.state.store.query("SELECT * FROM chat_message")] == chats
def test_generation_requests_carry_voice_and_current_world_without_rewriting_output(tmp_path):
# This verifies the actual provider boundary, not model content quality.
app, p = app_for(tmp_path)
with TestClient(app) as c:
c.post("/api/companion", json=CREATE)
runtime = setup_runtime(app, fixed_clock())
c.portal.call(runtime.tick)
persona_request = next(m for m in p.background_messages if "自主形成有张力" in m[0].content)
life_request = next(m for m in p.background_messages if "本人生活自述" in m[0].content)
for messages in (persona_request, life_request):
assert [m.role for m in messages] == ["system", "user"]
assert "第一人称“我”" in messages[0].content
assert "禁止用“它”指代她" in messages[0].content
assert '"pillars"' in persona_request[1].content
for constraint in ("高层卧室", "窗边", "慵懒沙发", "女性化装饰", "办公室", "茶水间",
"禁止编造无来源", "不默认每天读书", "雨夜等不能当作当前真实天气事实"):
assert constraint in life_request[0].content
assert INTERP in life_request[1].content
assert c.get("/api/persona").json()["interpretation"]["text"] == PERSONA["interpretation"]
row = app.state.store.query_one("SELECT content,place,told FROM life_event ORDER BY id DESC LIMIT 1")
assert tuple(row) == (LIFE["content"], LIFE["place"], 0)
def test_failed_persona_retry_has_no_template_or_half_seeds(tmp_path):
p = R2LLM()
p.persona_result = {"interpretation": "错误短标签", "habits": ["伪造"]}