HEL-916 rework: finite non-bool severity only; one-time chat-affect historical cutoff

Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
施工员2号
2026-11-03 11:26:11 +08:00
co-authored by multica-agent
parent adf4d5c4f7
commit 2e91a3c91e
5 changed files with 152 additions and 14 deletions
+95
View File
@@ -224,3 +224,98 @@ async def test_chat_affect_recovers_expired_lease_as_failed(tmp_path):
assert store.query_one("SELECT state FROM chat_affect_job")["state"] == "failed"
await app.state.life.tick()
assert not provider.affect_calls # expired lease is never re-run
# ---------------------------------------------------------------- rework (总工 HEL-917 两项)
@pytest.mark.asyncio
@pytest.mark.parametrize("bad", [True, False, float("nan"), float("inf"), float("-inf")])
async def test_invalid_severity_is_none_like_never_shocks(tmp_path, bad):
app, provider = h916_app(tmp_path, enabled=False)
with TestClient(app) as client:
client.post("/api/companion", json=CREATE)
provider.affect = {"kind": "hurt", "severity": bad}
turn(client, "这句话不该产生任何冲击")
mood, store = app.state.mood, app.state.store
dyad_before = app.state.dyad.load()
before = mood.state.v
await app.state.life.tick()
# job is done (terminal, never reclassified) but nothing moved
assert len(provider.affect_calls) == 1
assert store.query_one("SELECT state FROM chat_affect_job")["state"] == "done"
assert abs(mood.state.v - before) < 1e-3 # only the pre-existing tanh drift
after = app.state.dyad.load()
assert (after.trust, after.intimacy, after.debt, after.security) == \
(dyad_before.trust, dyad_before.intimacy, dyad_before.debt, dyad_before.security)
await app.state.life.tick()
assert len(provider.affect_calls) == 1 # no repeat classification
@pytest.mark.asyncio
async def test_valid_hurt_still_works_after_the_guard(tmp_path):
app, provider = h916_app(tmp_path, enabled=False)
with TestClient(app) as client:
client.post("/api/companion", json=CREATE)
provider.affect = {"kind": "hurt", "severity": 0.8}
turn(client, "我刚才说了很重的话")
before = app.state.mood.state.v
await app.state.life.tick()
assert app.state.mood.state.v < before - 0.1 # bounded but real
def seed_old_chat(db_path):
from cybergril.memory.store import Store
store = Store(db_path)
with store.conn:
store.execute("INSERT INTO chat_message(ts,role,text,turn_id,status) "
"VALUES(1,'user','很久以前的一句重话','old-1','sent')")
store.execute("INSERT INTO chat_message(ts,role,text,turn_id,status) "
"VALUES(2,'companion','很久以前的回复','old-1','sent')")
store.execute("INSERT INTO chat_message(ts,role,text,turn_id,status) "
"VALUES(3,'user','更久以前','old-2','sent')")
store.execute("INSERT INTO chat_message(ts,role,text,turn_id,status) "
"VALUES(4,'companion','更久以前的回复','old-2','sent')")
store.close()
@pytest.mark.asyncio
async def test_migrated_old_chat_never_classified_on_first_startup(tmp_path):
db = tmp_path / "h916.db"
seed_old_chat(db) # a populated database from before this wiring existed
app, provider = h916_app(tmp_path, enabled=False)
with TestClient(app) as client:
await app.state.life.tick()
assert not provider.affect_calls # no call, no job, no retro hurt
assert not app.state.store.query("SELECT * FROM chat_affect_job")
cutoff = app.state.store.query_one(
"SELECT value FROM life_control WHERE key='chat_affect_cutoff'")["value"]
assert int(cutoff) == 4 # pinned to the max pre-existing message id
@pytest.mark.asyncio
async def test_cutoff_persists_across_restart_and_only_new_turns_flow(tmp_path):
db = tmp_path / "h916.db"
app, provider = h916_app(tmp_path, enabled=False)
with TestClient(app) as client:
client.post("/api/companion", json=CREATE)
provider.affect = {"kind": "none", "severity": 0}
turn(client, "新完成的一轮")
await app.state.life.tick()
assert len(provider.affect_calls) == 1
cutoff = app.state.store.query_one(
"SELECT value FROM life_control WHERE key='chat_affect_cutoff'")["value"]
# restart on the same database: the cutoff must not move, the classified
# turn must not be re-run, and a turn completed after the restart flows
app2, provider2 = h916_app(tmp_path, enabled=False)
with TestClient(app2) as client2:
await app2.state.life.tick()
assert len(provider2.affect_calls) == 0
assert app2.state.store.query_one(
"SELECT value FROM life_control WHERE key='chat_affect_cutoff'")["value"] == cutoff
provider2.affect = {"kind": "repair", "severity": 0.6}
turn(client2, "和好如初的一轮")
before = app2.state.mood.state.v
await app2.state.life.tick()
assert len(provider2.affect_calls) == 1
assert app2.state.mood.state.v > before # positive, bounded repair impulse