feat(p4): isolate legacy relationship score writes
这个提交存在于:
父节点
daf9255810
当前提交
e922ac7210
共有 3 个文件被更改,包括 142 次插入 和 13 次删除
|
|
@ -9185,6 +9185,18 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"p4_b_safety_config": {
|
||||
"description": "P4-B 实效前置隔离门",
|
||||
"type": "object",
|
||||
"hint": "默认关闭以保持旧版本兼容。开启后只隔离旧 relationship_score 直写,不启用 P4 实效、禁闭或 owner 命令。",
|
||||
"items": {
|
||||
"enable_p4_b_legacy_score_isolation": {
|
||||
"description": "隔离旧关系分直写",
|
||||
"type": "bool",
|
||||
"default": false
|
||||
}
|
||||
}
|
||||
},
|
||||
"legacy_compat_config": {
|
||||
"description": "旧版兼容项",
|
||||
"type": "object",
|
||||
|
|
|
|||
60
main.py
60
main.py
|
|
@ -1933,6 +1933,10 @@ class PrivateCompanionPlugin(
|
|||
# separate audit ledger, but it never changes a live score, reply,
|
||||
# ignore gate, or management-command outcome.
|
||||
self.enable_p4_shadow_observer = self._cfg_bool(c, "enable_p4_shadow_observer", False)
|
||||
# This compatibility isolation does not enable P4-B. It only stops
|
||||
# legacy direct score writes while the separate effect ledger is
|
||||
# prepared and remains default-off for rollback compatibility.
|
||||
self.enable_p4_b_legacy_score_isolation = self._cfg_bool(c, "enable_p4_b_legacy_score_isolation", False)
|
||||
# P5-A only observes source metadata. It never rewrites a prompt,
|
||||
# changes a live tool set, or changes retrieval/response behaviour.
|
||||
self.enable_p5_source_observer = self._cfg_bool(c, "enable_p5_source_observer", False)
|
||||
|
|
@ -8150,10 +8154,10 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge
|
|||
user["reply_count"] = _safe_int(user.get("reply_count"), 0) + 1
|
||||
self._note_action_reply_feedback(
|
||||
user,
|
||||
str(user.get("last_proactive_action") or "message"),
|
||||
text,
|
||||
)
|
||||
user["relationship_score"] = _safe_int(user.get("relationship_score"), 0) + 2
|
||||
str(user.get("last_proactive_action") or "message"),
|
||||
text,
|
||||
)
|
||||
self._p4_b_apply_legacy_relationship_delta(user, 2, reason_code="proactive_reply")
|
||||
user["awaiting_reply_since"] = 0
|
||||
user["last_reply_at"] = received_ts
|
||||
user["last_private_reply_at"] = received_ts
|
||||
|
|
@ -9885,6 +9889,36 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge
|
|||
"""Expose the P5-D reject-only boundary without creating a live hook."""
|
||||
return p5d_security_event_allows(self, operation, *opaque)
|
||||
|
||||
def _p4_b_apply_legacy_relationship_delta(
|
||||
self,
|
||||
user: Any,
|
||||
delta: int,
|
||||
*,
|
||||
reason_code: str,
|
||||
) -> bool:
|
||||
"""Apply one old score mutation unless P4-B compatibility isolation is on.
|
||||
|
||||
This narrow seam does not inspect or record person IDs, messages,
|
||||
events, carriers, or authorization values. It can only suppress an
|
||||
existing direct score write; it cannot create a P4 effect or command
|
||||
exemption.
|
||||
"""
|
||||
if not isinstance(user, dict) or type(delta) is not int or not isinstance(reason_code, str):
|
||||
return False
|
||||
if bool(getattr(self, "enable_p4_b_legacy_score_isolation", False)):
|
||||
try:
|
||||
audit = getattr(self, "_p4_b_legacy_score_isolation_audit", None)
|
||||
if not isinstance(audit, list):
|
||||
audit = []
|
||||
setattr(self, "_p4_b_legacy_score_isolation_audit", audit)
|
||||
audit.append({"reason_code": reason_code, "delta": delta, "outcome": "isolated"})
|
||||
del audit[:-64]
|
||||
except Exception:
|
||||
pass
|
||||
return False
|
||||
user["relationship_score"] = _safe_int(user.get("relationship_score"), 0) + delta
|
||||
return True
|
||||
|
||||
def _p5_b1c_runtime_attestation_inputs(
|
||||
self,
|
||||
event: AstrMessageEvent,
|
||||
|
|
@ -11785,7 +11819,7 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge
|
|||
)
|
||||
rest_silence_applied = self._apply_user_rest_silence_from_message(fast_user, safe_text or text, now=received_ts)
|
||||
fast_user["inbound_count"] = _safe_int(fast_user.get("inbound_count"), 0) + 1
|
||||
fast_user["relationship_score"] = _safe_int(fast_user.get("relationship_score"), 0) + 1
|
||||
self._p4_b_apply_legacy_relationship_delta(fast_user, 1, reason_code="fast_inbound")
|
||||
fast_user["episode_message_count"] = _safe_int(fast_user.get("episode_message_count"), 0, 0) + 1
|
||||
if _safe_float(fast_user.get("awaiting_reply_since"), 0) > 0:
|
||||
fast_user["reply_count"] = _safe_int(fast_user.get("reply_count"), 0) + 1
|
||||
|
|
@ -11794,7 +11828,7 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge
|
|||
str(fast_user.get("last_proactive_action") or "message"),
|
||||
text,
|
||||
)
|
||||
fast_user["relationship_score"] = _safe_int(fast_user.get("relationship_score"), 0) + 2
|
||||
self._p4_b_apply_legacy_relationship_delta(fast_user, 2, reason_code="fast_proactive_reply")
|
||||
fast_user["awaiting_reply_since"] = 0
|
||||
fast_user["last_reply_at"] = received_ts
|
||||
fast_user["last_private_reply_at"] = received_ts
|
||||
|
|
@ -11807,7 +11841,7 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge
|
|||
if fast_user_is_owner:
|
||||
self._handle_meal_care_inbound(fast_user, safe_text or text, now=received_ts)
|
||||
if fast_user_is_owner and self._apply_interaction_warmth_to_state(text, fast_user):
|
||||
fast_user["relationship_score"] = _safe_int(fast_user.get("relationship_score"), 0) + 1
|
||||
self._p4_b_apply_legacy_relationship_delta(fast_user, 1, reason_code="fast_interaction_warmth")
|
||||
self._schedule_data_save()
|
||||
if (
|
||||
rest_silence_applied
|
||||
|
|
@ -12119,7 +12153,7 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge
|
|||
self._note_morning_greeting_reply(user, now=received_ts or _now_ts())
|
||||
if text:
|
||||
user["inbound_count"] = _safe_int(user.get("inbound_count"), 0) + 1
|
||||
user["relationship_score"] = _safe_int(user.get("relationship_score"), 0) + 1
|
||||
self._p4_b_apply_legacy_relationship_delta(user, 1, reason_code="inbound")
|
||||
suspended = user.get("suspended_proactive")
|
||||
if (
|
||||
isinstance(suspended, dict)
|
||||
|
|
@ -12139,7 +12173,7 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge
|
|||
str(user.get("last_proactive_action") or "message"),
|
||||
text,
|
||||
)
|
||||
user["relationship_score"] = _safe_int(user.get("relationship_score"), 0) + 2
|
||||
self._p4_b_apply_legacy_relationship_delta(user, 2, reason_code="proactive_reply")
|
||||
user["awaiting_reply_since"] = 0
|
||||
user["last_reply_at"] = _now_ts()
|
||||
user["last_private_reply_at"] = user["last_reply_at"]
|
||||
|
|
@ -12249,10 +12283,10 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge
|
|||
}
|
||||
care_feedback_applied = bool(text) and user_is_owner and self._apply_care_feedback_to_state(text)
|
||||
if care_feedback_applied:
|
||||
user["relationship_score"] = _safe_int(user.get("relationship_score"), 0) + 2
|
||||
self._p4_b_apply_legacy_relationship_delta(user, 2, reason_code="care_feedback")
|
||||
interaction_warmth_applied = bool(text) and is_target_user and user_is_owner and self._apply_interaction_warmth_to_state(text, user)
|
||||
if interaction_warmth_applied:
|
||||
user["relationship_score"] = _safe_int(user.get("relationship_score"), 0) + 1
|
||||
self._p4_b_apply_legacy_relationship_delta(user, 1, reason_code="interaction_warmth")
|
||||
schedule_adjustment_applied = (
|
||||
bool(text)
|
||||
and is_target_user
|
||||
|
|
@ -12260,9 +12294,9 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge
|
|||
and self._record_schedule_adjustment_from_interaction(text, user)
|
||||
)
|
||||
if schedule_adjustment_applied:
|
||||
user["relationship_score"] = _safe_int(user.get("relationship_score"), 0) + 1
|
||||
self._p4_b_apply_legacy_relationship_delta(user, 1, reason_code="schedule_adjustment")
|
||||
if food_feedback_applied:
|
||||
user["relationship_score"] = _safe_int(user.get("relationship_score"), 0) + 1
|
||||
self._p4_b_apply_legacy_relationship_delta(user, 1, reason_code="food_feedback")
|
||||
|
||||
response = ""
|
||||
if is_target_user:
|
||||
|
|
|
|||
|
|
@ -0,0 +1,83 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""P4-B legacy score isolation is a default-off compatibility seam."""
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib
|
||||
from pathlib import Path
|
||||
import sys
|
||||
from types import SimpleNamespace
|
||||
import types
|
||||
import unittest
|
||||
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
||||
import _host_stub # noqa: E402
|
||||
|
||||
_host_stub.install()
|
||||
event_module = importlib.import_module("astrbot.api.event")
|
||||
event_module.filter.EventMessageType = SimpleNamespace(ALL="ALL", PRIVATE_MESSAGE="PRIVATE_MESSAGE", GROUP_MESSAGE="GROUP_MESSAGE")
|
||||
|
||||
PACKAGE = "_p4_b_legacy_isolation_companion"
|
||||
if PACKAGE not in sys.modules:
|
||||
package = types.ModuleType(PACKAGE)
|
||||
package.__path__ = [str(Path(__file__).resolve().parents[1])]
|
||||
sys.modules[PACKAGE] = package
|
||||
|
||||
main = importlib.import_module(f"{PACKAGE}.main")
|
||||
|
||||
|
||||
class _Host:
|
||||
_p4_b_apply_legacy_relationship_delta = main.PrivateCompanionPlugin._p4_b_apply_legacy_relationship_delta
|
||||
|
||||
def __init__(self, enabled: bool) -> None:
|
||||
self.enable_p4_b_legacy_score_isolation = enabled
|
||||
|
||||
|
||||
class P4BLegacyIsolationTests(unittest.TestCase):
|
||||
def test_default_off_preserves_all_legacy_delta_shapes(self) -> None:
|
||||
host = _Host(False)
|
||||
for reason_code, delta in (
|
||||
("proactive_reply", 2),
|
||||
("fast_inbound", 1),
|
||||
("fast_proactive_reply", 2),
|
||||
("fast_interaction_warmth", 1),
|
||||
("inbound", 1),
|
||||
("proactive_reply", 2),
|
||||
("care_feedback", 2),
|
||||
("interaction_warmth", 1),
|
||||
("schedule_adjustment", 1),
|
||||
("food_feedback", 1),
|
||||
):
|
||||
with self.subTest(reason_code=reason_code):
|
||||
user = {"relationship_score": 7}
|
||||
self.assertTrue(host._p4_b_apply_legacy_relationship_delta(user, delta, reason_code=reason_code))
|
||||
self.assertEqual(7 + delta, user["relationship_score"])
|
||||
|
||||
def test_enabled_isolation_never_changes_score_or_records_user_content(self) -> None:
|
||||
host = _Host(True)
|
||||
user = {"relationship_score": 7, "message": "must not be audited", "user_id": "must not be audited"}
|
||||
self.assertFalse(host._p4_b_apply_legacy_relationship_delta(user, 2, reason_code="proactive_reply"))
|
||||
self.assertEqual(7, user["relationship_score"])
|
||||
self.assertEqual(
|
||||
[{"reason_code": "proactive_reply", "delta": 2, "outcome": "isolated"}],
|
||||
host._p4_b_legacy_score_isolation_audit,
|
||||
)
|
||||
|
||||
def test_invalid_inputs_cannot_mutate_or_create_audit(self) -> None:
|
||||
host = _Host(True)
|
||||
user = {"relationship_score": 7}
|
||||
self.assertFalse(host._p4_b_apply_legacy_relationship_delta(user, True, reason_code="bad"))
|
||||
self.assertFalse(host._p4_b_apply_legacy_relationship_delta(user, 1, reason_code=object()))
|
||||
self.assertEqual(7, user["relationship_score"])
|
||||
self.assertFalse(hasattr(host, "_p4_b_legacy_score_isolation_audit"))
|
||||
|
||||
def test_all_ten_legacy_direct_writes_use_the_narrow_helper(self) -> None:
|
||||
source = (Path(__file__).resolve().parents[1] / "main.py").read_text(encoding="utf-8")
|
||||
# The helper retains the sole legacy assignment for default-off
|
||||
# compatibility; all ten former call sites must route through it.
|
||||
self.assertEqual(1, source.count('relationship_score"] = _safe_int'))
|
||||
self.assertEqual(10, source.count("_p4_b_apply_legacy_relationship_delta(" ) - 1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
正在加载…
添加表格
在新工单中引用